Skip to content
Permalink
Browse files

8240990: convert clhsdb "dumpclass" command from javascript to java

Reviewed-by: sspitsyn, ysuenaga
  • Loading branch information
plummercj committed Apr 9, 2020
1 parent cd8be04 commit d1091364aa7f5faf4ad447a83b03ef169946ad2b
@@ -27,6 +27,7 @@
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -80,6 +81,7 @@
import sun.jvm.hotspot.tools.StackTrace;
import sun.jvm.hotspot.tools.jcore.ClassDump;
import sun.jvm.hotspot.tools.jcore.ClassFilter;
import sun.jvm.hotspot.tools.jcore.ClassWriter;
import sun.jvm.hotspot.types.CIntegerType;
import sun.jvm.hotspot.types.Field;
import sun.jvm.hotspot.types.Type;
@@ -1703,6 +1705,66 @@ public void doit(Tokens t) {
}
}
},
new Command("dumpclass", "dumpclass {address | name} [directory]", false) {
public void doit(Tokens t) {
int tokenCount = t.countTokens();
if (tokenCount != 1 && tokenCount != 2) {
usage();
return;
}

/* Find the InstanceKlass for specified class name or class address. */
InstanceKlass ik = null;
String classname = t.nextToken();
if (classname.startsWith("0x")) {
// treat it as address
VM vm = VM.getVM();
Address addr = vm.getDebugger().parseAddress(classname);
Metadata metadata = Metadata.instantiateWrapperFor(addr.addOffsetTo(0));
if (metadata instanceof InstanceKlass) {
ik = (InstanceKlass) metadata;
} else {
System.out.println("Specified address is not an InstanceKlass");
return;
}
} else {
ik = SystemDictionaryHelper.findInstanceKlass(classname);
if (ik == null) {
System.out.println("class not found: " + classname);
return;
}
}

/* Compute filename for class. */
StringBuffer buf = new StringBuffer();
if (tokenCount > 1) {
buf.append(t.nextToken());
} else {
buf.append('.');
}
buf.append(File.separatorChar);
buf.append(ik.getName().asString().replace('/', File.separatorChar));
buf.append(".class");
String fileName = buf.toString();
File file = new File(fileName);

/* Dump the class file. */
try {
int index = fileName.lastIndexOf(File.separatorChar);
File dir = new File(fileName.substring(0, index));
dir.mkdirs();
try (FileOutputStream fos = new FileOutputStream(file)) {
ClassWriter cw = new ClassWriter(ik, fos);
cw.write();
}
} catch (Exception e) {
err.println("Error: " + e);
if (verboseExceptions) {
e.printStackTrace(err);
}
}
}
},
new Command("dumpheap", "dumpheap [filename]", false) {
public void doit(Tokens t) {
if (t.countTokens() > 1) {
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2020, 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.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jdk.test.lib.apps.LingeredApp;
import jdk.test.lib.JDKToolLauncher;
import jdk.test.lib.JDKToolFinder;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.SA.SATestUtils;
import jtreg.SkippedException;

/**
* @test
* @bug 8240990
* @summary Test clhsdb dumpclass command
* @requires vm.hasSA
* @library /test/lib
* @run main/othervm ClhsdbDumpclass
*/

public class ClhsdbDumpclass {
static final String APP_DOT_CLASSNAME = LingeredApp.class.getName();
static final String APP_SLASH_CLASSNAME = APP_DOT_CLASSNAME.replace('.', '/');

public static void main(String[] args) throws Exception {
System.out.println("Starting ClhsdbDumpclass test");

LingeredApp theApp = null;
try {
ClhsdbLauncher test = new ClhsdbLauncher();

theApp = LingeredApp.startApp();
System.out.println("Started LingeredApp with pid " + theApp.getPid());

// Run "dumpclass jdk/test/lib/apps/LingeredApp"
String cmd = "dumpclass " + APP_DOT_CLASSNAME;
List<String> cmds = List.of(cmd);
Map<String, List<String>> unExpStrMap = new HashMap<>();
unExpStrMap.put(cmd, List.of("class not found"));
test.run(theApp.getPid(), cmds, null, unExpStrMap);
File classFile = new File(APP_SLASH_CLASSNAME + ".class");
if (!classFile.exists()) {
throw new RuntimeException("FAILED: Cannot find dumped .class file");
}

// Run javap on the generated class file to make sure it's valid.
JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("javap");
launcher.addToolArg(APP_DOT_CLASSNAME);
System.out.println("> javap " + APP_DOT_CLASSNAME);
List<String> cmdStringList = Arrays.asList(launcher.getCommand());
ProcessBuilder pb = new ProcessBuilder(cmdStringList);
Process javap = pb.start();
OutputAnalyzer out = new OutputAnalyzer(javap);
javap.waitFor();
System.out.println(out.getStdout());
System.err.println(out.getStderr());
out.shouldHaveExitValue(0);
out.shouldMatch("public class " + APP_DOT_CLASSNAME);
} catch (SkippedException se) {
throw se;
} catch (Exception ex) {
throw new RuntimeException("Test ERROR " + ex, ex);
} finally {
LingeredApp.stopApp(theApp);
}
System.out.println("Test PASSED");
}
}

0 comments on commit d109136

Please sign in to comment.