Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/DebugServer.java
41159 views
/*1* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot;2526import sun.jvm.hotspot.debugger.*;27import sun.jvm.hotspot.runtime.*;28import sun.jvm.hotspot.oops.*;2930public class DebugServer {31private void usage() {32System.out.println("usage: java " + getClass().getName() + " <pid> [server id]");33System.out.println(" or: java " + getClass().getName() + " <executable> <core> [server id]");34System.out.println("\"pid\" must be the process ID of a HotSpot process.");35System.out.println("If reading a core file, \"executable\" must (currently) be the");36System.out.println("full path name to the precise java executable which generated");37System.out.println("the core file (not, on Solaris, the \"java\" wrapper script in");38System.out.println("the \"bin\" subdirectory of the JDK.)");39System.out.println("The \"server id\" is a unique name for a specific remote debuggee.");40System.exit(1);41}4243public static void main(String[] args) {44new DebugServer().run(args);45}4647private void run(String[] args) {48if ((args.length < 1) || (args.length > 3)) {49usage();50}5152// Attempt to handle "-h" or "-help"53if (args[0].startsWith("-")) {54usage();55}5657int pid = 0;58boolean usePid = false;59String coreFileName = null;60// FIXME: would be nice to pick this up from the core file61// somehow, but that doesn't look possible. Should at least figure62// it out from a path to the JDK.63String javaExecutableName = null;64String serverID = null;6566switch (args.length) {67case 1:68try {69pid = Integer.parseInt(args[0]);70usePid = true;71} catch (NumberFormatException e) {72usage();73}74break;7576case 2:77// either we have pid and server id or exec file and core file78try {79pid = Integer.parseInt(args[0]);80usePid = true;81serverID = args[1];82} catch (NumberFormatException e) {83pid = -1;84usePid = false;85javaExecutableName = args[0];86coreFileName = args[1];87}88break;8990case 3:91javaExecutableName = args[0];92coreFileName = args[1];93serverID = args[2];94break;9596default:97// should not happend, taken care already.98break;99}100101final HotSpotAgent agent = new HotSpotAgent();102try {103if (usePid) {104System.err.println("Attaching to process ID " + pid + " and starting RMI services, please wait...");105agent.startServer(pid, serverID, null);106} else {107System.err.println("Attaching to core " + coreFileName +108" from executable " + javaExecutableName + " and starting RMI services, please wait...");109agent.startServer(javaExecutableName, coreFileName, serverID, null);110}111}112catch (DebuggerException e) {113if (usePid) {114System.err.print("Error attaching to process or starting server: ");115} else {116System.err.print("Error attaching to core file or starting server: ");117}118e.printStackTrace();119System.exit(1);120}121122// shutdown hook to clean-up the server in case of forced exit.123Runtime.getRuntime().addShutdownHook(new java.lang.Thread(124new Runnable() {125public void run() {126agent.shutdownServer();127}128}));129System.err.println("Debugger attached and RMI services started.");130}131}132133134