test/hotspot/jtreg/vmTestbase/nsk/share/jdi/SerialExecutionDebugger.java
changeset 49934 44839fbb20db
equal deleted inserted replaced
49933:c63bdf53a1a7 49934:44839fbb20db
       
     1 /*
       
     2  * Copyright (c) 2006, 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 package nsk.share.jdi;
       
    24 
       
    25 import nsk.share.Consts;
       
    26 import nsk.share.TestBug;
       
    27 import nsk.share.jpda.AbstractDebuggeeTest;
       
    28 import java.io.*;
       
    29 import java.util.*;
       
    30 
       
    31 /*
       
    32  * This class serial executes several JDI tests based on nsk.share.jdi.TestDebuggerType2 in single VM
       
    33  * SerialExecutionDebugger is used together with SerialExecutionDebuggee, execution process is following:
       
    34  *
       
    35  *  - SerialExecutionDebugger reads tests to execute from input file, test description is debugger class name and test's parameters,
       
    36  *  if 'shuffle' option is specified in input file debugger executes tests in random order (input file should contain line "OPTIONS:shuffle").
       
    37  *  SerialExecutionDebugger can execute tests several times in loop, number of iterations should be specified in input file in following manner:
       
    38  *  OPTIONS:iterations <iterations_number>.
       
    39  *
       
    40  *  - SerialExecutionDebugger starts debuggee VM with main class SerialExecutionDebuggee,
       
    41  *  initializes IOPipe and 'debuggee' object which represents debuggee VM
       
    42  *
       
    43  *  - for each test from input file:
       
    44  *
       
    45  *      - SerialExecutionDebugger creates object of current debugger and initializes it with already created pipe and debuggee
       
    46  *      - SerialExecutionDebugger sends command to SerialExecutionDebuggee:  'COMMAND_EXECUTE_DEBUGGEE <CurrentDebuggeeName>'
       
    47  *      (CurrentDebuggeeName name should provide current debugger), and waits READY signal from debuggee
       
    48  *      - SerialExecutionDebuggee parses received command, extracts debugee name, creates object of current debuggee, which should be
       
    49  *      subclass of nsk.share.jpda.AbstractDebuggeeTestName
       
    50  *      - SerialExecutionDebuggee executes current debuggee's method 'doTest()', in this method debuggee sends signal READY
       
    51  *      and waits debugger command
       
    52  *      - SerialExecutionDebugger receives signal READY and executes current debugger's method 'doTest()', in
       
    53  *      this method debugger should perform test
       
    54  *      - when debugger method doTest() finishes SerialExecutionDebugger checks is this test passed or failed and
       
    55  *      sends command QUIT to the current debuggee, and when current debuggee finishes sends command 'COMMAND_CLEAR_DEBUGGEE' to the SerialExecutionDebuggee,
       
    56  *      after this command SerialExecutionDebugger and SerialExecutionDebuggee ready to execute next test
       
    57  *
       
    58  *  - when all tests was executed SerialExecutionDebugger sends command QUIT to the SerialExecutionDebuggee and exits
       
    59  *
       
    60  * SerialExecutionDebugger requires "-configFile <ini-file>" parameter, <ini-file> - file with list of tests for execution
       
    61  */
       
    62 public class SerialExecutionDebugger extends TestDebuggerType2 {
       
    63     static public void main(String[] args) {
       
    64         System.exit(Consts.JCK_STATUS_BASE + new SerialExecutionDebugger().runIt(args, System.out));
       
    65     }
       
    66 
       
    67     public String debuggeeClassName() {
       
    68         return SerialExecutionDebuggee.class.getName();
       
    69     }
       
    70 
       
    71     // contains test's debugger class name and test parameters
       
    72     static class Test {
       
    73         public Test(String debuggerClassName, String[] arguments) {
       
    74             this.debuggerClassName = debuggerClassName;
       
    75             this.arguments = arguments;
       
    76         }
       
    77 
       
    78         public String argumentsToString() {
       
    79             String result = "";
       
    80 
       
    81             for (String argument : arguments)
       
    82                 result += argument + " ";
       
    83 
       
    84             return result;
       
    85         }
       
    86 
       
    87         String debuggerClassName;
       
    88 
       
    89         String arguments[];
       
    90     }
       
    91 
       
    92     private Test tests[];
       
    93 
       
    94     // how many times execute tests
       
    95     private int iterations = 1;
       
    96 
       
    97     // requires "-configFile <ini-file>" parameter, <ini-file> - file with list
       
    98     // of tests for execution
       
    99     protected String[] doInit(String args[], PrintStream out) {
       
   100         args = super.doInit(args, out);
       
   101 
       
   102         String configFileName = null;
       
   103 
       
   104         ArrayList<String> standardArgs = new ArrayList<String>();
       
   105 
       
   106         for (int i = 0; i < args.length; i++) {
       
   107             if (args[i].equals("-configFile") && (i < args.length - 1)) {
       
   108                 configFileName = args[i + 1];
       
   109                 i++;
       
   110             } else
       
   111                 standardArgs.add(args[i]);
       
   112         }
       
   113 
       
   114         if (configFileName == null) {
       
   115             throw new TestBug("Config file wasn't specified (use option -configFile <file name>)");
       
   116         }
       
   117 
       
   118         tests = parseConfigFile(configFileName);
       
   119 
       
   120         if (tests.length == 0)
       
   121             throw new TestBug("Tests to run were not specified");
       
   122 
       
   123         return standardArgs.toArray(new String[] {});
       
   124     }
       
   125 
       
   126     // read test names and test parameters from ini-file
       
   127     private Test[] parseConfigFile(String fileName) {
       
   128         List<Test> result = new ArrayList<Test>();
       
   129 
       
   130         boolean shuffle = false;
       
   131 
       
   132         try {
       
   133             File file = new File(fileName);
       
   134 
       
   135             LineNumberReader lineReader = new LineNumberReader(new FileReader(file));
       
   136 
       
   137             String line = null;
       
   138 
       
   139             while ((line = lineReader.readLine()) != null) {
       
   140                 // skip empty lines and comments started with '#"
       
   141                 if (line.length() == 0 || line.startsWith("#"))
       
   142                     continue;
       
   143 
       
   144                 if (line.startsWith("OPTIONS:")) {
       
   145                     String arguments[] = line.substring(8).split(" ");
       
   146 
       
   147                     for (int i = 0; i < arguments.length; i++) {
       
   148                         if (arguments[i].equalsIgnoreCase("shuffle"))
       
   149                             shuffle = true;
       
   150                         else if (arguments[i].equalsIgnoreCase("iterations") && (i < (arguments.length - 1))) {
       
   151                             iterations = Integer.parseInt(arguments[i + 1]);
       
   152                             i++;
       
   153                         }
       
   154                     }
       
   155 
       
   156                     continue;
       
   157                 }
       
   158 
       
   159                 StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(line));
       
   160                 tokenizer.resetSyntax();
       
   161                 tokenizer.wordChars(Integer.MIN_VALUE, Integer.MAX_VALUE);
       
   162                 tokenizer.whitespaceChars(' ', ' ');
       
   163 
       
   164                 if (tokenizer.nextToken() != StreamTokenizer.TT_WORD)
       
   165                     throw new TestBug("Invalid ini file format");
       
   166 
       
   167                 String testClassName = tokenizer.sval;
       
   168                 List<String> parameters = new ArrayList<String>();
       
   169 
       
   170                 int token;
       
   171                 while ((token = tokenizer.nextToken()) != StreamTokenizer.TT_EOF) {
       
   172                     if (token == StreamTokenizer.TT_WORD) {
       
   173                         if (tokenizer.sval.equals("$CLASSPATH"))
       
   174                             parameters.add(classpath);
       
   175                         else
       
   176                             parameters.add(tokenizer.sval);
       
   177                     }
       
   178 
       
   179                     if (token == StreamTokenizer.TT_NUMBER) {
       
   180                         parameters.add("" + tokenizer.nval);
       
   181                     }
       
   182                 }
       
   183 
       
   184                 result.add(new Test(testClassName, parameters.toArray(new String[] {})));
       
   185             }
       
   186 
       
   187         } catch (IOException e) {
       
   188             throw new TestBug("Exception during config file parsing: " + e);
       
   189         }
       
   190 
       
   191         if (shuffle) {
       
   192             if (testWorkDir == null)
       
   193                 throw new TestBug("Debugger requires -testWorkDir parameter");
       
   194 
       
   195             Collections.shuffle(result);
       
   196 
       
   197             // save resulted tests sequence in file (to simplify reproducing)
       
   198             try {
       
   199                 File file = new File(testWorkDir + File.separator + "run.tests");
       
   200                 file.createNewFile();
       
   201 
       
   202                 PrintWriter writer = new PrintWriter(new FileWriter(file));
       
   203 
       
   204                 for (Test test : result) {
       
   205                     writer.println(test.debuggerClassName + " " + test.argumentsToString());
       
   206                 }
       
   207 
       
   208                 writer.close();
       
   209             } catch (IOException e) {
       
   210                 throw new TestBug("Unexpected IOException: " + e);
       
   211             }
       
   212         }
       
   213 
       
   214         System.out.println("Tests execution order: ");
       
   215         for (Test test : result) {
       
   216             System.out.println(test.debuggerClassName + " " + test.argumentsToString());
       
   217         }
       
   218 
       
   219         return result.toArray(new Test[] {});
       
   220     }
       
   221 
       
   222     public void doTest() {
       
   223 
       
   224         stresser.start(iterations);
       
   225 
       
   226         try {
       
   227             if (iterations == 1) {
       
   228                 /*
       
   229                  * Since many test couldn't be run in single VM twice and test config specifies only 1 iteration don't
       
   230                  * multiple iterations by iterations factor and execute tests once (not depending on iterations factor)
       
   231                  */
       
   232                 executeTests();
       
   233             } else {
       
   234                 while (stresser.continueExecution()) {
       
   235                     if (!executeTests()) {
       
   236                         // if error occured stop execution
       
   237                         break;
       
   238                     }
       
   239                 }
       
   240             }
       
   241         } finally {
       
   242             stresser.finish();
       
   243         }
       
   244     }
       
   245 
       
   246     boolean executeTests() {
       
   247         // maximum execution time of single test
       
   248         long maxExecutionTime = 0;
       
   249 
       
   250         for (Test test : tests) {
       
   251             long testStartTime = System.currentTimeMillis();
       
   252 
       
   253             TestDebuggerType2 debugger = null;
       
   254 
       
   255             try {
       
   256                 // create debugger object
       
   257                 Class debuggerClass = Class.forName(test.debuggerClassName);
       
   258 
       
   259                 if (!TestDebuggerType2.class.isAssignableFrom(debuggerClass)) {
       
   260                     setSuccess(false);
       
   261                     log.complain("Invalid debugger class: " + debuggerClass);
       
   262                     return false;
       
   263                 }
       
   264 
       
   265                 // init test debugger, pass to the debugger already created
       
   266                 // objects: argHandler, log, pipe, debuggee, vm
       
   267                 debugger = (TestDebuggerType2) debuggerClass.newInstance();
       
   268                 debugger.initDebugger(argHandler, log, pipe, debuggee, vm);
       
   269                 debugger.doInit(test.arguments, System.out);
       
   270             } catch (Exception e) {
       
   271                 setSuccess(false);
       
   272                 log.complain("Unexpected exception during debugger initialization: " + e);
       
   273                 e.printStackTrace(log.getOutStream());
       
   274 
       
   275                 return false;
       
   276             }
       
   277 
       
   278             log.display("Execute debugger: " + debugger);
       
   279 
       
   280             // send command to the SerialExecutionDebuggee (create debuggee
       
   281             // object)
       
   282             pipe.println(SerialExecutionDebuggee.COMMAND_EXECUTE_DEBUGGEE + ":" + debugger.debuggeeClassName());
       
   283 
       
   284             // wait first READY from AbstractDebuggeeTest.doTest() (debuggee
       
   285             // sends this command when it was initialized and ready for
       
   286             // test)
       
   287             if (!isDebuggeeReady())
       
   288                 return false;
       
   289 
       
   290             try {
       
   291                 // here debuggee should be ready for test and current
       
   292                 // debugger may perform test
       
   293                 debugger.doTest();
       
   294 
       
   295                 if (debugger.getSuccess()) {
       
   296                     log.display("Debugger " + debugger + " finished successfully");
       
   297                 } else {
       
   298                     setSuccess(false);
       
   299                     log.complain("Debugger " + debugger + " finished with errors");
       
   300                 }
       
   301             } catch (TestBug testBug) {
       
   302                 setSuccess(false);
       
   303                 log.complain("Test bug in " + debugger + ": " + testBug);
       
   304                 testBug.printStackTrace(log.getOutStream());
       
   305             } catch (Throwable t) {
       
   306                 setSuccess(false);
       
   307                 log.complain("Unexpected exception during test execution(debugger: " + debugger + "): " + t);
       
   308                 t.printStackTrace(log.getOutStream());
       
   309             }
       
   310 
       
   311             // send QUIT command to the current debuggee
       
   312             pipe.println(AbstractDebuggeeTest.COMMAND_QUIT);
       
   313 
       
   314             if (!isDebuggeeReady())
       
   315                 return false;
       
   316 
       
   317             // send command to the SerialExecutionDebuggee
       
   318             pipe.println(SerialExecutionDebuggee.COMMAND_CLEAR_DEBUGGEE);
       
   319 
       
   320             if (!isDebuggeeReady())
       
   321                 return false;
       
   322 
       
   323             long testExecutionTime = System.currentTimeMillis() - testStartTime;
       
   324 
       
   325             if (testExecutionTime > maxExecutionTime)
       
   326                 maxExecutionTime = testExecutionTime;
       
   327 
       
   328             if (maxExecutionTime > stresser.getTimeLeft()) {
       
   329                 log.display("WARNING: stop test execution because of timeout " +
       
   330                                 "(max execution time for single test: " + maxExecutionTime + ", time left: " + stresser.getTimeLeft() + ")");
       
   331                 return false;
       
   332             }
       
   333         }
       
   334 
       
   335         return true;
       
   336     }
       
   337 }