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