File: RASagent.java

package info (click to toggle)
openjdk-11 11.0.16%2B8-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 784,576 kB
  • sloc: java: 5,128,021; xml: 1,192,224; cpp: 1,124,021; ansic: 422,433; javascript: 155,577; sh: 17,084; objc: 13,327; python: 4,522; asm: 3,570; makefile: 2,858; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (376 lines) | stat: -rw-r--r-- 15,958 bytes parent folder | download | duplicates (20)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package nsk.share;

import java.io.*;
import java.lang.reflect.Method;
import java.util.*;

/**
 * Class used as an agent for Java serviceability reliability testing (RAS).
 * It sets different RAS options and/or modes for a special agent which
 * actually performs the specified RAS testing.<br>
 * The agent recognizes arguments, started with ''<code>-ras.</code>''. They
 * may be as follows:<p>
 * <li><code>-ras.help</code> - print usage message and exit
 * <li><code>-ras.verbose</code> - verbose mode
 * <li><code>-ras.invoke_run</code> - invoke the method <i>run(String[],PrintStream)</i>
 * of the test instead of <i>main(String[])</i> which is invoked by default.
 * <li><code>-ras.hotswap=&lt;stress_level&gt;</code> - enable JVMTI hotswap of
 * the currently running test classes. Here are the possible HotSwap stress
 * levels:<br>
 * 0 - HotSwap off<br>
 * 2 - HotSwap tested class in every JVMTI method entry event of running test
 * (default mode)<br>
 * 20 - HotSwap tested class in every JVMTI method entry event of every class<br>
 * 3 - HotSwap tested class in every JVMTI single step event of running test<br>
 * 4 - HotSwap tested class in every JVMTI exception event of running test<br>
 * 40 - HotSwap tested class in every JVMTI exception event of every class<p>
 */
public class RASagent {
    static final int HOTSWAP_OFF = 0;
    static final int HOTSWAP_EVERY_METHOD_ENTRY = 2;
    static final int HOTSWAP_EVERY_METHOD_ENTRY_FOR_EVERY_CLASS = 20;
    static final int HOTSWAP_EVERY_SINGLE_STEP = 3;
    static final int HOTSWAP_EVERY_EXCEPTION = 4;
    static final int HOTSWAP_EVERY_EXCEPTION_FOR_EVERY_CLASS = 40;

    // path to the directory with class files of the invoked test
    static String clfBasePath = null;

    private static boolean verbose = false;

    private static PrintStream out;

    native static int setHotSwapMode(boolean vrb, int stress_lev,
        String shortName);

    public static void main(String argv[]) {
        System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
    }

    public static int run(String argv[], PrintStream out) {
        return new RASagent().runThis(argv, out);
    }

    private int runThis(String argv[], PrintStream out) {
        int skipArgs = 1; // number of arguments which must be skipped
                          // for the invoked test
        boolean invokeRun = false; // invoke the method "main" by default
        int hotSwapMode = HOTSWAP_EVERY_METHOD_ENTRY; // HotSwap default stress level
        int res;
        String hotSwapModeName = "HOTSWAP_EVERY_METHOD_ENTRY";

        RASagent.out = out;

        if (argv.length != 0) {
            // parse arguments for the RASagent and then skip them
            while(argv[skipArgs-1].startsWith("-ras.")) {
                if (argv[skipArgs-1].equals("-ras.verbose")) {
                    verbose = true;
                } else if (argv[skipArgs-1].equals("-ras.help")) {
                    printHelp();
                    return Consts.TEST_FAILED;
                } else if (argv[skipArgs-1].equals("-ras.invoke_run")) {
                    invokeRun = true;
                } else if (argv[skipArgs-1].startsWith("-ras.hotswap=")) {
                    try {
                        hotSwapMode = Integer.parseInt(
                           argv[skipArgs-1].substring(argv[skipArgs-1].lastIndexOf("=")+1));
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                        out.println("\nERROR: RASagent: specified HotSwap mode \""
                            + hotSwapMode + "\" is not an integer");
                        printHelp();
                        return Consts.TEST_FAILED;
                    }
                    switch(hotSwapMode) {
                        case HOTSWAP_EVERY_METHOD_ENTRY:
                            hotSwapModeName = "HOTSWAP_EVERY_METHOD_ENTRY";
                            break;
                        case HOTSWAP_EVERY_METHOD_ENTRY_FOR_EVERY_CLASS:
                            hotSwapModeName = "HOTSWAP_EVERY_METHOD_ENTRY_FOR_EVERY_CLASS";
                            break;
                        case HOTSWAP_EVERY_SINGLE_STEP:
                            hotSwapModeName = "HOTSWAP_EVERY_SINGLE_STEP";
                            break;
                        case HOTSWAP_EVERY_EXCEPTION:
                            hotSwapModeName = "HOTSWAP_EVERY_EXCEPTION";
                            break;
                        case HOTSWAP_EVERY_EXCEPTION_FOR_EVERY_CLASS:
                            hotSwapModeName = "HOTSWAP_EVERY_EXCEPTION_FOR_EVERY_CLASS";
                            break;
                        default:
                            out.println("\nERROR: RASagent: specified HotSwap mode \""
                                + hotSwapMode + "\" is unrecognized");
                            printHelp();
                            return Consts.TEST_FAILED;
                    }
                }
                skipArgs++;
            }

            String shortTestName = getTestNameAndPath(argv[skipArgs-1]);

            display("\n#### RASagent: setting hotswap mode \""
                + hotSwapModeName + "\" for class \""
                + shortTestName + "\" ...");
            if ((res = setHotSwapMode(verbose, hotSwapMode, shortTestName)) != 0) {
                out.println("\nERROR: RASagent: unable to set HotSwap stress level for \""
                    + shortTestName + "\", exiting");
                return Consts.TEST_FAILED;
            }
            display("\n#### RASagent: ... setting hotswap mode done");

            try {
                Class testCls = Class.forName(argv[skipArgs-1]);
                display("\n#### RASagent: main class \""
                    + testCls.toString() + "\" loaded");

                // copy arguments for the invoked test
                String args[] = new String[argv.length-skipArgs];
                System.arraycopy(argv, skipArgs, args, 0, args.length);

                // invoke the test
                if (invokeRun)
                    return invokeRunMethod(testCls, args);
                else
                    return invokeMainMethod(testCls, args);
            } catch(ClassNotFoundException e) {
                // just pass: the invoked test is already a RAS specific one
                out.println("\nWARNING: the test was not really run due to the following error:"
                    + "\n\tunable to get the Class object for \""
                    + argv[skipArgs-1] + "\"\n\tcaught: " + e);
                return Consts.TEST_PASSED;
            }

        } else {
            out.println("\nERROR: RASagent: required test name is absent in parameters list");
            return Consts.TEST_FAILED;
        }
    }

    /**
     * Verify that test's class file exists with a path given as a parameter
     * and, if so, store that path in the static field "clfBasePath".
     */
    private boolean pathValid(String pathToCheck, String testName) {
        String fullPath = pathToCheck + File.separator
            + testName.replace('.', File.separatorChar) + ".class";
        File classFile = null;

        display("\n#### RASagent: verifying class path\n<RASagent>\t"
            + pathToCheck + " ...");
        try {
            classFile = new File(fullPath);
        } catch (NullPointerException e) {
            e.printStackTrace();
            out.println("\nERROR: RASagent: verification of class file "
                + fullPath + " failed: caught " + e);
            System.exit(Consts.JCK_STATUS_BASE + Consts.TEST_FAILED);
        }

        if (classFile.exists()) {
            clfBasePath = pathToCheck;
            display("<RASagent>\tthe class file exists:\n<RASagent>\t\t"
                + fullPath + "\n<RASagent>\tclass file base directory found:\n"
                + "<RASagent>\t\t" + clfBasePath
                + "\n#### RASagent: ... class path verification done\n");
            return true;
        }
        else {
            display("<RASagent>\tno class file at location :\n\t\t"
                + fullPath
                + "\n#### RASagent: ... class path verification done\n");
            return false;
        }
    }

    /**
     * Get short name of an invoked test (i.e. without package name) and
     * store path to the directory with the test's class files.
     */
    private String getTestNameAndPath(String testName) {
        String shortTestName = testName;
        String packageName = "";

        // if '.' occurs, it means that current test is inside a package
        if (testName.lastIndexOf(".") != -1) {
            shortTestName = testName.substring(testName.lastIndexOf(".")+1);
            packageName = testName.substring(0, testName.lastIndexOf("."));
        }

        StringTokenizer clPathes = new StringTokenizer(
            System.getProperty("java.class.path"), File.pathSeparator);

        while(clPathes.hasMoreTokens()) {
            String clPath = clPathes.nextToken();

            // trying to load a class file defining the current test from
            // this entry of "java.class.path": the class file may locate
            // at the test's work directory or if it's already compiled,
            // at any directory in classpath
            if (pathValid(clPath, testName))
                return shortTestName;
        }

        // directory with the test's class files was not found.
        // Actually, it means that the invoked test has own Java
        // options such as, for example, "-verify"
        out.println("\nWARNING: the test was not really run due to the following reason:"
            + "\n\tthe invoked test has the own Java option: "
            + testName);
        System.exit(Consts.JCK_STATUS_BASE + Consts.TEST_PASSED);

        return null; // fake return for too smart javac
    }

    /**
     * Invoke the method <i>main(String[])</i> of the test.
     */
    private int invokeMainMethod(Class testCls, String args[]) {
        Class[] methType = { String[].class };
        Object[] methArgs = { args };

        return invokeMethod(testCls, "main", methType, methArgs);
    }

    /**
     * Invoke the method <i>run(String[], PrintStream)</i> of the test.
     */
    private int invokeRunMethod(Class testCls, String args[]) {
        Class[] methType = { String[].class, PrintStream.class };
        Object[] methArgs = { args, out };

        return invokeMethod(testCls, "run", methType, methArgs);
    }

    /**
     * Low level invocation of the test.
     */
    private int invokeMethod(Class<?> testCls, String methodName,
            Class methType[], Object methArgs[]) {

        try {
            Method testMeth = testCls.getMethod(methodName, methType);
            display("\n#### RASagent: invoking method \""
                + testMeth.toString() + "\" ...");

            Object result = testMeth.invoke(null, methArgs);

            display("\n#### RASagent: ... invocation of \""
                + testMeth.toString() + "\" done");
            if (result instanceof Integer) {
                Integer retCode = (Integer) result;
                return retCode.intValue();
            }
        } catch(NoSuchMethodException e) {
            e.printStackTrace();
            out.println("\nFAILURE: RASagent: unable to get method \""
                + methodName + "\" in class "
                + testCls + "\n\tcaught " + e);
            return Consts.TEST_FAILED;
        } catch(Exception e) {
            e.printStackTrace();
            out.println("\nFAILURE: RASagent: caught during invokation of the test class "
                + testCls + " " + e);
            return Consts.TEST_FAILED;
        }

        return -1;
    }

    /**
     * Load class bytes for HotSwap.
     */
    static byte[] loadFromClassFile(String signature) {
        String testPath = clfBasePath + File.separator + signature.substring(
                1, signature.length()-1).replace('/', File.separatorChar) + ".class";
        File classFile = null;

        display("\n#### RASagent: looking for class file\n<RASagent>\t"
            + testPath + " ...");

        try {
            classFile = new File(testPath);
        } catch (NullPointerException e) {
            out.println("\nFAILURE: RASagent: path name to the redefining class file is null");
        }

        display("\n#### RASagent: loading " + classFile.length()
            + " bytes from class file "+ testPath + " ...");
        byte[] buf = new byte[(int) classFile.length()];
        try {
            InputStream in = new FileInputStream(classFile);
            in.read(buf);
            in.close();
        } catch(FileNotFoundException e) {
            e.printStackTrace();
            out.println("\nFAILURE: RASagent: loadFromClassFile: file " +
                classFile.getName() + " not found");
            System.exit(Consts.JCK_STATUS_BASE + Consts.TEST_FAILED);
        } catch (Exception e) {
            e.printStackTrace();
            out.println("\nFAILURE: RASagent: unable to load bytes from the file:\n");
            out.println("\t" + testPath + ": caught " + e);
            System.exit(Consts.JCK_STATUS_BASE + Consts.TEST_FAILED);
        }

        display("\n#### RASagent: ... " + classFile.length() + " bytes loaded");

        return buf;
    }

    /**
     * This method is used in verbose mode. It prints paramter string only
     * in case of verbose mode.
     */
    private static void display(String msg) {
        if (verbose)
            out.println(msg);
    }

    /**
     * This method prints out RASagent usage message.
     */
    private static void printHelp() {
        out.println("\nRASagent usage: RASagent [option, ...] test" +
            "\n\t-ras.help                 print this message and exit" +
            "\n\t-ras.verbose              verbose mode (off by default)" +
            "\n\t-ras.hotswap=mode         enable HotSwap of the running test classes" +
            "\n\t\twhere mode is:" +
            "\n\t\t\t" + HOTSWAP_EVERY_METHOD_ENTRY
                + " - hotswap tested class in its every method entry event" +
            "\n\t\t\t" + HOTSWAP_EVERY_METHOD_ENTRY_FOR_EVERY_CLASS
                + " - hotswap tested class in every method entry event for every class" +
            "\n\t\t\t" + HOTSWAP_EVERY_SINGLE_STEP
                + " - hotswap tested class in its every single step event" +
            "\n\t\t\t" + HOTSWAP_EVERY_EXCEPTION
                + " - hotswap tested class in its every exception event" +
            "\n\t\t\t" + HOTSWAP_EVERY_EXCEPTION_FOR_EVERY_CLASS
                + " - hotswap tested class in every exception event for every class\n" +
            "\n\t-ras.invoke_run           invoke the method run() of the test" +
            "\n\t\tinstead of main() by default");
    }
}