--- old/src/share/vm/utilities/vmError.cpp Sun Aug 11 16:03:13 2013 +++ new/src/share/vm/utilities/vmError.cpp Sun Aug 11 16:03:12 2013 @@ -1063,6 +1063,9 @@ if (!skip_os_abort) { skip_os_abort = true; bool dump_core = should_report_bug(first_error->_id); +#ifndef __APPLE__ + if (dump_core) dump_loaded_classes(); +#endif os::abort(dump_core); } @@ -1070,6 +1073,42 @@ os::die(); } } + +bool file_exists(char* filename) { + if (FILE * fp = fopen(filename, "r")) { + fclose(fp); + return true; + } + return false; +} + +// try to use SA to dump loaded java classes +void VMError::dump_loaded_classes() { + int pid = os::current_process_id(); + char cmd[JVM_MAXPATHLEN]; + char *javahome = Arguments::get_java_home(); + const char* sep = os::file_separator(); + char sajdi[JVM_MAXPATHLEN]; + sprintf(sajdi, "%s%s%s%s%s%s%s", javahome, sep, "..", sep, "lib", + sep, "sa-jdi.jar"); + if (!file_exists(sajdi)) { + out.print_cr("jre does not contain sa-jdi.jar\n" + "Can not dump loaded classes into jar file!"); + return; + } +#ifdef _LP64 + sprintf(cmd, "%s%s%s%s%s%s%s%d", javahome, sep, "bin", sep, "java -d64 -cp ", sajdi, + " sun.jvm.hotspot.DumpLoadedClasses ", pid); +#else + sprintf(cmd, "%s%s%s%s%s%s%s%d", javahome, sep, "bin", sep, "java -cp ", sajdi, + " sun.jvm.hotspot.DumpLoadedClasses ", pid); +#endif + if (os::fork_and_exec(cmd) == 0) { + out.print_cr("pid%d_boot.jar and pid%d_app.jar contain loaded classes", pid, pid); + } else { + out.print_cr("Can not dump loaded classes into jar file!"); + } +} /* * OnOutOfMemoryError scripts/commands executed while VM is a safepoint - this --- old/src/share/vm/utilities/vmError.hpp Sun Aug 11 16:03:14 2013 +++ new/src/share/vm/utilities/vmError.hpp Sun Aug 11 16:03:13 2013 @@ -96,6 +96,8 @@ return (id != OOM_MALLOC_ERROR) && (id != OOM_MMAP_ERROR); } + void dump_loaded_classes(); + static fdStream out; static fdStream log; // error log used by VMError::report_and_die() --- /dev/null Sun Aug 11 16:03:14 2013 +++ new/agent/src/share/classes/sun/jvm/hotspot/DumpLoadedClasses.java Sun Aug 11 16:03:14 2013 @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2013, 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 sun.jvm.hotspot; + +import sun.jvm.hotspot.*; +import sun.jvm.hotspot.debugger.*; + +import java.io.*; +import java.util.*; +import sun.jvm.hotspot.tools.jcore.ClassFilter; +import sun.jvm.hotspot.tools.jcore.ClassDump; +import sun.jvm.hotspot.CommandProcessor.NonBootFilter; +import sun.jvm.hotspot.CommandProcessor.BootFilter; +public class DumpLoadedClasses { + + public DumpLoadedClasses(JVMDebugger d) { + jvmDebugger = d; + } + + public static void main(String[] args) { + new DumpLoadedClasses(args).run(); + } + + public void run() { + agent = new HotSpotAgent(); + if (jvmDebugger != null) { + attachDebugger(jvmDebugger); + } else if (pidText != null) { + attachDebugger(pidText); + } else if (execPath != null) { + attachDebugger(execPath, coreFilename); + } + if (!attached) { + exitValue = -1; + System.out.println("Attaching to target failed!"); + return; + } + // Dump loaded classes by using of ClassDump + try { + ClassDump cd = new ClassDump(); + cd.setClassFilter(new BootFilter()); + cd.setJarOutput(pidText != null ? "pid" + pidText + "_boot.jar" : + coreFilename + "_boot.jar"); + cd.run(); + + cd.setClassFilter(new NonBootFilter()); + cd.setJarOutput(pidText != null ? "pid" + pidText + "_app.jar" : + coreFilename + "_app.jar"); + cd.run(); + } catch (Exception e) { + exitValue = -1; + e.printStackTrace(); + } + if (attached) detach(); + // exit here, return exitValue + System.exit(exitValue); + } + + //-------------------------------------------------------------------------------- + // Internals only below this point + // + private HotSpotAgent agent; + private JVMDebugger jvmDebugger; + private boolean attached; + // These had to be made data members because they are referenced in inner classes. + private String pidText; + private String execPath, coreFilename; + private int pid; + private int exitValue = 0; // Error report of jvm need a return vaule + // 0 --- success + // -1 -- fail + private DumpLoadedClasses(String[] args) { + switch (args.length) { + case (0): + break; + + case (1): // has to be pid + try { + int unused = Integer.parseInt(args[0]); + // If we get here, we have a PID and not a core file name + pidText = args[0]; + } catch (NumberFormatException e) { + } + break; + case (2): + execPath = args[0]; + coreFilename = args[1]; + break; + + default: + System.out.println("HSDB Error: Too many options specified"); + return; + } + } + + private void attachDebugger(JVMDebugger d) { + agent.attach(d); + attached = true; + } + + /** NOTE, following two attachDebuger(s) + we are in a different thread here than either the main + thread or the Swing/AWT event handler thread, so we must be very + careful when creating or removing widgets */ + private void attachDebugger(String pidText) { + try { + this.pidText = pidText; + pid = Integer.parseInt(pidText); + } + catch (NumberFormatException e) { + System.err.print("Unable to parse process ID \"" + pidText + "\".\nPlease enter a number."); + return; + } + + try { + // lengthy call + agent.attach(pid); + attached = true; + } + catch (DebuggerException e) { + return; + } + } + + private void attachDebugger(final String executablePath, final String corePath) { + // Try to open this core file + try { + // lengthy call + agent.attach(executablePath, corePath); + attached = true; + } + catch (DebuggerException e) { + return; + } + } + + + private void detachDebugger() { + if (!attached) { + return; + } + agent.detach(); + attached = false; + } + + private void detach() { + detachDebugger(); + } + + +}