1 /*
   2  * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 4500906 4433599 4740097
  27  * @summary vmexec= debug java fails for SunCommandLineLauncher
  28  * @author jjh
  29  *
  30  * @run build TestScaffold VMConnection TargetListener TargetAdapter
  31  * @run compile -g Java_gTest.java
  32  * @run driver Java_gTest
  33  */
  34 import com.sun.jdi.*;
  35 import com.sun.jdi.event.*;
  36 import com.sun.jdi.request.*;
  37 
  38 import java.util.*;
  39 import java.io.File;
  40 
  41     /********** target program **********/
  42 
  43 class Java_gTarg {
  44     public static void main(String[] args){
  45         System.out.println("Howdy!");
  46         System.out.println("Goodbye from Java_gTarg!");
  47     }
  48 }
  49 
  50     /********** test program **********/
  51 
  52 public class Java_gTest extends TestScaffold {
  53     ReferenceType targetClass;
  54     ThreadReference mainThread;
  55 
  56     Java_gTest (String args[]) {
  57         super(args);
  58     }
  59 
  60     public static void main(String[] args)      throws Exception {
  61         /*
  62          * On Windows, this test needs msvcrtd.dll which is installed
  63          * as part of Vis C++.  We don't want this test to fail
  64          * if msvcrtd.dll is not present
  65          */
  66         String mslibName = System.mapLibraryName("msvcrtd");
  67         if (mslibName.equals("msvcrtd.dll")) {
  68             try {
  69                 System.loadLibrary("msvcrtd");
  70             } catch (Throwable ee) {
  71                 // If it isn't there, just pass
  72                 System.out.println("Exception looking for msvcrtd.dll: " + ee);
  73                 System.out.println("msvcrtd.dll does not exist.  Let the test pass");
  74                 return;
  75             }
  76         }
  77 
  78         /*
  79          * This test would like to run the debug (java) executable.
  80          * If java is not found, we don't want a spurious test failure,
  81          * so check before attempting to run.
  82          *
  83          * We can't catch the IOException because it is thrown
  84          * on a separate thread (com.sun.tools.jdi.AbstractLauncher$Helper),
  85          * so check for the expected executable before attempting to launch.
  86          */
  87 
  88         String specialExec = "java";
  89         String sep = System.getProperty("file.separator");
  90         String jhome =  System.getProperty("java.home");
  91         String jbin = jhome + sep + "bin";
  92         File binDir = new File(jbin);
  93         if ((new File(binDir, specialExec).exists()) ||
  94             (new File(binDir, specialExec + ".exe").exists())) {
  95             /*
  96              * A java executable does in fact exist in the
  97              * expected location.  Run the real test.
  98              */
  99             args = new String[2];
 100             args[0] = "-connect";
 101             args[1] = "com.sun.jdi.CommandLineLaunch:vmexec=" + specialExec;
 102             new Java_gTest(args).startTests();
 103         } else {
 104             System.out.println("No java executable exists.  Let the test pass.");
 105         }
 106     }
 107 
 108     /********** event handlers **********/
 109 
 110     /********** test core **********/
 111 
 112     protected void runTests() throws Exception {
 113         /*
 114          * Get to the top of main()
 115          * to determine targetClass and mainThread
 116          */
 117         BreakpointEvent bpe = startToMain("Java_gTarg");
 118         listenUntilVMDisconnect();
 119 
 120         /*
 121          * deal with results of test
 122          * if anything has called failure("foo") testFailed will be true
 123          */
 124         if (!testFailed) {
 125             println("Java_gTest: passed");
 126         } else {
 127             throw new Exception("Java_gTest: failed");
 128         }
 129     }
 130 }