1 /*
2  * Copyright (c) 2017, 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 java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Paths;
27 
28 import jdk.test.lib.apps.LingeredApp;
29 import jdk.test.lib.Platform;
30 import jdk.test.lib.process.OutputAnalyzer;
31 import jdk.test.lib.process.ProcessTools;
32 
33 /**
34  * @test
35  * @bug 8184982
36  * @summary Test ClassDump tool
37  * @requires vm.hasSAandCanAttach
38  * @library /test/lib
39  * @run main/othervm TestClassDump
40  */
41 
42 public class TestClassDump {
43 
dumpClass(long lingeredAppPid)44     private static void dumpClass(long lingeredAppPid)
45         throws IOException {
46 
47         ProcessBuilder pb;
48         OutputAnalyzer output;
49 
50         pb = ProcessTools.createJavaProcessBuilder(
51                 "-Dsun.jvm.hotspot.tools.jcore.outputDir=jtreg_classes",
52                 "-m", "jdk.hotspot.agent/sun.jvm.hotspot.tools.jcore.ClassDump", String.valueOf(lingeredAppPid));
53         output = new OutputAnalyzer(pb.start());
54         output.shouldHaveExitValue(0);
55         if (!Files.isDirectory(Paths.get("jtreg_classes"))) {
56             throw new RuntimeException("jtreg_classes directory not found");
57         }
58         if (Files.notExists(Paths.get("jtreg_classes", "java", "lang", "Integer.class"))) {
59             throw new RuntimeException("jtreg_classes/java/lang/Integer.class not found");
60         }
61         if (Files.notExists(Paths.get("jtreg_classes", "jdk", "test", "lib", "apps", "LingeredApp.class"))) {
62             throw new RuntimeException("jtreg_classes/jdk/test/lib/apps/LingeredApp.class not found");
63         }
64         if (Files.notExists(Paths.get("jtreg_classes", "sun", "net", "util", "URLUtil.class"))) {
65             throw new RuntimeException("jtreg_classes/sun/net/util/URLUtil.class not found");
66         }
67 
68         pb = ProcessTools.createJavaProcessBuilder(
69                 "-Dsun.jvm.hotspot.tools.jcore.outputDir=jtreg_classes2",
70                 "-Dsun.jvm.hotspot.tools.jcore.PackageNameFilter.pkgList=jdk,sun",
71                 "-m", "jdk.hotspot.agent/sun.jvm.hotspot.tools.jcore.ClassDump", String.valueOf(lingeredAppPid));
72         output = new OutputAnalyzer(pb.start());
73         output.shouldHaveExitValue(0);
74         if (Files.exists(Paths.get("jtreg_classes2", "java", "math", "BigInteger.class"))) {
75             throw new RuntimeException("jtreg_classes2/java/math/BigInteger.class not expected");
76         }
77         if (Files.notExists(Paths.get("jtreg_classes2", "sun", "util", "calendar", "BaseCalendar.class"))) {
78             throw new RuntimeException("jtreg_classes2/sun/util/calendar/BaseCalendar.class not found");
79         }
80         if (Files.notExists(Paths.get("jtreg_classes2", "jdk", "internal", "loader", "BootLoader.class"))) {
81             throw new RuntimeException("jtreg_classes2/jdk/internal/loader/BootLoader.class not found");
82         }
83     }
84 
main(String[] args)85     public static void main(String[] args) throws Exception {
86         LingeredApp theApp = null;
87         try {
88             theApp = LingeredApp.startApp();
89             long pid = theApp.getPid();
90             System.out.println("Started LingeredApp with pid " + pid);
91             dumpClass(pid);
92         } catch (Exception ex) {
93             throw new RuntimeException("Test ERROR " + ex, ex);
94         } finally {
95             LingeredApp.stopApp(theApp);
96         }
97         System.out.println("Test PASSED");
98     }
99 }
100