8184982: SA: Running ClassDump on a simple java program generates NullPointerException
authorsballal
Thu, 30 Nov 2017 14:58:41 +0530
changeset 48158 66622fc2e247
parent 48157 7c4d43c26352
child 48159 11b6d69215ec
8184982: SA: Running ClassDump on a simple java program generates NullPointerException Reviewed-by: sundar, jgeorge
src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/tools/jcore/PackageNameFilter.java
test/hotspot/jtreg/serviceability/sa/TestClassDump.java
--- a/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/tools/jcore/PackageNameFilter.java	Tue Nov 28 21:43:45 2017 +0100
+++ b/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/tools/jcore/PackageNameFilter.java	Thu Nov 30 14:58:41 2017 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2017, 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
@@ -50,10 +50,14 @@
     }
 
     public boolean canInclude(InstanceKlass kls) {
-        String klassName = kls.getName().asString().replace('/', '.');
+        if (pkgList == null) {
+            // Dump everything
+            return true;
+        }
         final int len = pkgList.length;
         if (len == 0)
             return true;
+        String klassName = kls.getName().asString().replace('/', '.');
         for (int i=0; i < len; i++)
             if (klassName.startsWith((String) pkgList[i] )) return true;
         return false;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/serviceability/sa/TestClassDump.java	Thu Nov 30 14:58:41 2017 +0530
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2017, 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.
+ */
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+import jdk.test.lib.apps.LingeredApp;
+import jdk.test.lib.Platform;
+import jdk.test.lib.process.OutputAnalyzer;
+import jdk.test.lib.process.ProcessTools;
+
+/*
+ * @test
+ * @bug 8184982
+ * @summary Test ClassDump tool
+ * @library /test/lib
+ * @run main/othervm TestClassDump
+ */
+
+public class TestClassDump {
+
+    private static void dumpClass(long lingeredAppPid)
+        throws IOException {
+
+        ProcessBuilder pb;
+        OutputAnalyzer output;
+
+        pb = ProcessTools.createJavaProcessBuilder(
+                "-Dsun.jvm.hotspot.tools.jcore.outputDir=jtreg_classes",
+                "-m", "jdk.hotspot.agent/sun.jvm.hotspot.tools.jcore.ClassDump", String.valueOf(lingeredAppPid));
+        output = new OutputAnalyzer(pb.start());
+        output.shouldHaveExitValue(0);
+        if (!Files.isDirectory(Paths.get("jtreg_classes"))) {
+            throw new RuntimeException("jtreg_classes directory not found");
+        }
+        if (Files.notExists(Paths.get("jtreg_classes", "java", "lang", "Integer.class"))) {
+            throw new RuntimeException("jtreg_classes/java/lang/Integer.class not found");
+        }
+        if (Files.notExists(Paths.get("jtreg_classes", "jdk", "test", "lib", "apps", "LingeredApp.class"))) {
+            throw new RuntimeException("jtreg_classes/jdk/test/lib/apps/LingeredApp.class not found");
+        }
+        if (Files.notExists(Paths.get("jtreg_classes", "sun", "net", "util", "URLUtil.class"))) {
+            throw new RuntimeException("jtreg_classes/sun/net/util/URLUtil.class not found");
+        }
+
+        pb = ProcessTools.createJavaProcessBuilder(
+                "-Dsun.jvm.hotspot.tools.jcore.outputDir=jtreg_classes2",
+                "-Dsun.jvm.hotspot.tools.jcore.PackageNameFilter.pkgList=jdk,sun",
+                "-m", "jdk.hotspot.agent/sun.jvm.hotspot.tools.jcore.ClassDump", String.valueOf(lingeredAppPid));
+        output = new OutputAnalyzer(pb.start());
+        output.shouldHaveExitValue(0);
+        if (Files.exists(Paths.get("jtreg_classes2", "java", "math", "BigInteger.class"))) {
+            throw new RuntimeException("jtreg_classes2/java/math/BigInteger.class not expected");
+        }
+        if (Files.notExists(Paths.get("jtreg_classes2", "sun", "util", "calendar", "BaseCalendar.class"))) {
+            throw new RuntimeException("jtreg_classes2/sun/util/calendar/BaseCalendar.class not found");
+        }
+        if (Files.notExists(Paths.get("jtreg_classes2", "jdk", "internal", "vm", "PostVMInitHook.class"))) {
+            throw new RuntimeException("jtreg_classes2/jdk/internal/vm/PostVMInitHook.class not found");
+        }
+    }
+
+    public static void main(String[] args) throws Exception {
+        if (!Platform.shouldSAAttach()) {
+            // Silently skip the test if we don't have enough permissions to attach
+            System.out.println("SA attach not expected to work - test skipped.");
+            return;
+        }
+
+        LingeredApp theApp = null;
+        try {
+            theApp = LingeredApp.startApp();
+            long pid = theApp.getPid();
+            System.out.println("Started LingeredApp with pid " + pid);
+            dumpClass(pid);
+        } catch (Exception ex) {
+            throw new RuntimeException("Test ERROR " + ex, ex);
+        } finally {
+            LingeredApp.stopApp(theApp);
+        }
+        System.out.println("Test PASSED");
+    }
+}