< prev index next >

test/jdk/com/sun/jdi/TestScaffold.java

Print this page

   1 /*
   2  * Copyright (c) 2001, 2018, 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 import com.sun.jdi.*;
  25 import com.sun.jdi.request.*;
  26 import com.sun.jdi.event.*;

  27 import java.util.*;
  28 import java.io.*;
  29 
  30 /**
  31  * Framework used by all JDI regression tests
  32  */
  33 abstract public class TestScaffold extends TargetAdapter {
  34     private boolean shouldTrace = false;
  35     private VMConnection connection;
  36     private VirtualMachine vm;
  37     private EventRequestManager requestManager;
  38     private List listeners = Collections.synchronizedList(new LinkedList());
  39     private boolean redefineAtStart = false;
  40     private boolean redefineAtEvents = false;
  41     private boolean redefineAsynchronously = false;
  42     private ReferenceType mainStartClass = null;
  43 
  44     ThreadReference mainThread;
  45     /**
  46      * We create a VMDeathRequest, SUSPEND_ALL, to sync the BE and FE.

 439         System.err.println("[" + elapsed + "ms] " + str);
 440     }
 441 
 442     protected void print(String str) {
 443         System.err.print(str);
 444     }
 445 
 446     protected void traceln(String str) {
 447         if (shouldTrace) {
 448             println(str);
 449         }
 450     }
 451 
 452     protected void failure(String str) {
 453         println(str);
 454         testFailed = true;
 455     }
 456 
 457     private ArgInfo parseArgs(String args[]) {
 458         ArgInfo argInfo = new ArgInfo();




 459         for (int i = 0; i < args.length; i++) {
 460             if (args[i].equals("-connect")) {
 461                 i++;
 462                 argInfo.connectorSpec = args[i];
 463             } else if (args[i].equals("-trace")) {
 464                 i++;
 465                 argInfo.traceFlags = Integer.decode(args[i]).intValue();
 466             } else if (args[i].equals("-redefstart")) {
 467                 redefineAtStart = true;
 468             } else if (args[i].equals("-redefevent")) {
 469                 redefineAtEvents = true;
 470             } else if (args[i].equals("-redefasync")) {
 471                 redefineAsynchronously = true;
 472             } else if (args[i].startsWith("-J")) {
 473                 argInfo.targetVMArgs += (args[i].substring(2) + ' ');
 474 
 475                 /*
 476                  * classpath can span two arguments so we need to handle
 477                  * it specially.
 478                  */

 923                  ", vmDisconnected= " + vmDisconnected +
 924                  ", connection = " + connection);
 925 
 926         if ((connection != null)) {
 927             try {
 928                 connection.disposeVM();
 929              } catch (VMDisconnectedException e) {
 930                 // Shutting down after the VM has gone away. This is
 931                 // not an error, and we just ignore it.
 932             }
 933         } else {
 934             traceln("TS: shutdown: disposeVM not called");
 935         }
 936         if (message != null) {
 937             println(message);
 938         }
 939 
 940         vmDied = true;
 941         vmDisconnected = true;
 942     }







































































 943 }

   1 /*
   2  * Copyright (c) 2001, 2021, 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 import com.sun.jdi.*;
  25 import com.sun.jdi.request.*;
  26 import com.sun.jdi.event.*;
  27 import java.lang.reflect.InvocationTargetException;
  28 import java.util.*;
  29 import java.io.*;
  30 
  31 /**
  32  * Framework used by all JDI regression tests
  33  */
  34 abstract public class TestScaffold extends TargetAdapter {
  35     private boolean shouldTrace = false;
  36     private VMConnection connection;
  37     private VirtualMachine vm;
  38     private EventRequestManager requestManager;
  39     private List listeners = Collections.synchronizedList(new LinkedList());
  40     private boolean redefineAtStart = false;
  41     private boolean redefineAtEvents = false;
  42     private boolean redefineAsynchronously = false;
  43     private ReferenceType mainStartClass = null;
  44 
  45     ThreadReference mainThread;
  46     /**
  47      * We create a VMDeathRequest, SUSPEND_ALL, to sync the BE and FE.

 440         System.err.println("[" + elapsed + "ms] " + str);
 441     }
 442 
 443     protected void print(String str) {
 444         System.err.print(str);
 445     }
 446 
 447     protected void traceln(String str) {
 448         if (shouldTrace) {
 449             println(str);
 450         }
 451     }
 452 
 453     protected void failure(String str) {
 454         println(str);
 455         testFailed = true;
 456     }
 457 
 458     private ArgInfo parseArgs(String args[]) {
 459         ArgInfo argInfo = new ArgInfo();
 460         if (System.getProperty("main.wrapper") != null) {
 461             argInfo.targetAppCommandLine  = TestScaffold.class.getName() + " " + System.getProperty("main.wrapper") + " ";
 462         }
 463 
 464         for (int i = 0; i < args.length; i++) {
 465             if (args[i].equals("-connect")) {
 466                 i++;
 467                 argInfo.connectorSpec = args[i];
 468             } else if (args[i].equals("-trace")) {
 469                 i++;
 470                 argInfo.traceFlags = Integer.decode(args[i]).intValue();
 471             } else if (args[i].equals("-redefstart")) {
 472                 redefineAtStart = true;
 473             } else if (args[i].equals("-redefevent")) {
 474                 redefineAtEvents = true;
 475             } else if (args[i].equals("-redefasync")) {
 476                 redefineAsynchronously = true;
 477             } else if (args[i].startsWith("-J")) {
 478                 argInfo.targetVMArgs += (args[i].substring(2) + ' ');
 479 
 480                 /*
 481                  * classpath can span two arguments so we need to handle
 482                  * it specially.
 483                  */

 928                  ", vmDisconnected= " + vmDisconnected +
 929                  ", connection = " + connection);
 930 
 931         if ((connection != null)) {
 932             try {
 933                 connection.disposeVM();
 934              } catch (VMDisconnectedException e) {
 935                 // Shutting down after the VM has gone away. This is
 936                 // not an error, and we just ignore it.
 937             }
 938         } else {
 939             traceln("TS: shutdown: disposeVM not called");
 940         }
 941         if (message != null) {
 942             println(message);
 943         }
 944 
 945         vmDied = true;
 946         vmDisconnected = true;
 947     }
 948 
 949     public static void main(String[] args) throws Throwable {
 950         String wrapper = args[0];
 951         String className = args[1];
 952         String[] classArgs = new String[args.length - 2];
 953         System.arraycopy(args, 2, classArgs, 0, args.length - 2);
 954         Class c = Class.forName(className);
 955         java.lang.reflect.Method mainMethod = c.getMethod("main", new Class[] { String[].class });
 956         mainMethod.setAccessible(true);
 957 
 958         if (wrapper.equals("Virtual")) {
 959             MainThreadGroup tg = new MainThreadGroup();
 960             // TODO fix to set virtual scheduler group when become available
 961             Thread vthread = startVirtualThread(() -> {
 962                 try {
 963                     mainMethod.invoke(null, new Object[] { classArgs });
 964                 } catch (InvocationTargetException e) {
 965                     tg.uncaughtThrowable = e.getCause();
 966                 } catch (Throwable error) {
 967                     tg.uncaughtThrowable = error;
 968                 }
 969             });
 970             vthread.join();
 971         } else if (wrapper.equals("Kernel")) {
 972             MainThreadGroup tg = new MainThreadGroup();
 973             Thread t = new Thread(tg, () -> {
 974                 try {
 975                     mainMethod.invoke(null, new Object[] { classArgs });
 976                 } catch (InvocationTargetException e) {
 977                     tg.uncaughtThrowable = e.getCause();
 978                 } catch (Throwable error) {
 979                     tg.uncaughtThrowable = error;
 980                 }
 981             });
 982             t.start();
 983             t.join();
 984             if (tg.uncaughtThrowable != null) {
 985                 throw new RuntimeException(tg.uncaughtThrowable);
 986             }
 987         } else {
 988             mainMethod.invoke(null, new Object[] { classArgs });
 989         }
 990     }
 991 
 992     static class MainThreadGroup extends ThreadGroup {
 993         MainThreadGroup() {
 994             super("MainThreadGroup");
 995         }
 996 
 997         public void uncaughtException(Thread t, Throwable e) {
 998             if (e instanceof ThreadDeath) {
 999                 return;
1000             }
1001             e.printStackTrace(System.err);
1002             uncaughtThrowable = e;
1003         }
1004         Throwable uncaughtThrowable = null;
1005     }
1006 
1007     static Thread startVirtualThread(Runnable task) {
1008         try {
1009             Object builder = Thread.class.getMethod("ofVirtual").invoke(null);
1010             Class<?> clazz = Class.forName("java.lang.Thread$Builder");
1011             java.lang.reflect.Method start = clazz.getMethod("start", Runnable.class);
1012             return (Thread) start.invoke(builder, task);
1013         } catch (RuntimeException | Error e) {
1014             throw e;
1015         } catch (Exception e) {
1016             throw new RuntimeException(e);
1017         }
1018     }
1019 }
< prev index next >