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 25 package sun.jvm.hotspot; 26 27 import sun.jvm.hotspot.*; 28 import sun.jvm.hotspot.debugger.*; 29 30 import java.io.*; 31 import java.util.*; 32 33 public class CLHSDB { 34 public static void main(String[] args) { 35 new CLHSDB(args).run(); 36 } 37 38 private void run() { 39 // At this point, if pidText != null we are supposed to attach to it. 40 // Else, if execPath != null, it is the path of a jdk/bin/java 41 // and coreFilename is the pathname of a core file we are 42 // supposed to attach to. 43 44 agent = new HotSpotAgent(); 45 46 Runtime.getRuntime().addShutdownHook(new java.lang.Thread() { 47 public void run() { 48 detachDebugger(); 49 } 50 }); 51 52 if (pidText != null) { 53 attachDebugger(pidText); 54 } else if (execPath != null) { 55 attachDebugger(execPath, coreFilename); 56 } 57 58 59 CommandProcessor.DebuggerInterface di = new CommandProcessor.DebuggerInterface() { 60 public HotSpotAgent getAgent() { 61 return agent; 62 } 63 public boolean isAttached() { 64 return attached; 65 } 66 public void attach(String pid) { 67 attachDebugger(pid); 68 } 69 public void attach(String java, String core) { 70 attachDebugger(java, core); 71 } 72 public void detach() { 79 if (pidText != null) { 80 attach(pidText); 81 } else { 82 attach(execPath, coreFilename); 83 } 84 } 85 }; 86 87 88 BufferedReader in = 89 new BufferedReader(new InputStreamReader(System.in)); 90 CommandProcessor cp = new CommandProcessor(di, in, System.out, System.err); 91 cp.run(true); 92 93 } 94 95 //-------------------------------------------------------------------------------- 96 // Internals only below this point 97 // 98 private HotSpotAgent agent; 99 private boolean attached; 100 // These had to be made data members because they are referenced in inner classes. 101 private String pidText; 102 private int pid; 103 private String execPath; 104 private String coreFilename; 105 106 private void doUsage() { 107 System.out.println("Usage: java CLHSDB [[pid] | [path-to-java-executable [path-to-corefile]] | help ]"); 108 System.out.println(" pid: attach to the process whose id is 'pid'"); 109 System.out.println(" path-to-java-executable: Debug a core file produced by this program"); 110 System.out.println(" path-to-corefile: Debug this corefile. The default is 'core'"); 111 System.out.println(" If no arguments are specified, you can select what to do from the GUI.\n"); 112 HotSpotAgent.showUsage(); 113 } 114 115 private CLHSDB(String[] args) { 116 switch (args.length) { 117 case (0): 118 break; 119 120 case (1): 121 if (args[0].equals("help") || args[0].equals("-help")) { 122 doUsage(); 123 System.exit(0); 124 } 125 // If all numbers, it is a PID to attach to 126 // Else, it is a pathname to a .../bin/java for a core file. 127 try { 128 int unused = Integer.parseInt(args[0]); 129 // If we get here, we have a PID and not a core file name 130 pidText = args[0]; 131 } catch (NumberFormatException e) { 132 execPath = args[0]; 133 coreFilename = "core"; 134 } 135 break; 136 137 case (2): 138 execPath = args[0]; 139 coreFilename = args[1]; 140 break; 141 142 default: 143 System.out.println("HSDB Error: Too many options specified"); 144 doUsage(); 145 System.exit(1); 146 } 147 } 148 149 /** NOTE we are in a different thread here than either the main 150 thread or the Swing/AWT event handler thread, so we must be very 151 careful when creating or removing widgets */ 152 private void attachDebugger(String pidText) { 153 try { 154 this.pidText = pidText; 155 pid = Integer.parseInt(pidText); 156 } 157 catch (NumberFormatException e) { 158 System.err.print("Unable to parse process ID \"" + pidText + "\".\nPlease enter a number."); 159 } 160 161 try { 162 System.err.println("Attaching to process " + pid + ", please wait..."); 163 164 // FIXME: display exec'd debugger's output messages during this 165 // lengthy call 166 agent.attach(pid); | 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 25 package sun.jvm.hotspot; 26 27 import sun.jvm.hotspot.*; 28 import sun.jvm.hotspot.debugger.*; 29 30 import java.io.*; 31 import java.util.*; 32 33 public class CLHSDB { 34 35 public CLHSDB(JVMDebugger d) { 36 jvmDebugger = d; 37 } 38 39 public static void main(String[] args) { 40 new CLHSDB(args).run(); 41 } 42 43 public void run() { 44 // If jvmDebugger is already set, we have been given a JVMDebugger. 45 // Otherwise, if pidText != null we are supposed to attach to it. 46 // Finally, if execPath != null, it is the path of a jdk/bin/java 47 // and coreFilename is the pathname of a core file we are 48 // supposed to attach to. 49 50 agent = new HotSpotAgent(); 51 52 Runtime.getRuntime().addShutdownHook(new java.lang.Thread() { 53 public void run() { 54 detachDebugger(); 55 } 56 }); 57 58 if (jvmDebugger != null) { 59 attachDebugger(jvmDebugger); 60 } else if (pidText != null) { 61 attachDebugger(pidText); 62 } else if (execPath != null) { 63 attachDebugger(execPath, coreFilename); 64 } 65 66 67 CommandProcessor.DebuggerInterface di = new CommandProcessor.DebuggerInterface() { 68 public HotSpotAgent getAgent() { 69 return agent; 70 } 71 public boolean isAttached() { 72 return attached; 73 } 74 public void attach(String pid) { 75 attachDebugger(pid); 76 } 77 public void attach(String java, String core) { 78 attachDebugger(java, core); 79 } 80 public void detach() { 87 if (pidText != null) { 88 attach(pidText); 89 } else { 90 attach(execPath, coreFilename); 91 } 92 } 93 }; 94 95 96 BufferedReader in = 97 new BufferedReader(new InputStreamReader(System.in)); 98 CommandProcessor cp = new CommandProcessor(di, in, System.out, System.err); 99 cp.run(true); 100 101 } 102 103 //-------------------------------------------------------------------------------- 104 // Internals only below this point 105 // 106 private HotSpotAgent agent; 107 private JVMDebugger jvmDebugger; 108 private boolean attached; 109 // These had to be made data members because they are referenced in inner classes. 110 private String pidText; 111 private int pid; 112 private String execPath; 113 private String coreFilename; 114 115 private void doUsage() { 116 System.out.println("Usage: java CLHSDB [[pid] | [path-to-java-executable [path-to-corefile]] | help ]"); 117 System.out.println(" pid: attach to the process whose id is 'pid'"); 118 System.out.println(" path-to-java-executable: Debug a core file produced by this program"); 119 System.out.println(" path-to-corefile: Debug this corefile. The default is 'core'"); 120 System.out.println(" If no arguments are specified, you can select what to do from the GUI.\n"); 121 HotSpotAgent.showUsage(); 122 } 123 124 private CLHSDB(String[] args) { 125 switch (args.length) { 126 case (0): 127 break; 128 129 case (1): 130 if (args[0].equals("help") || args[0].equals("-help")) { 131 doUsage(); 132 return; 133 } 134 // If all numbers, it is a PID to attach to 135 // Else, it is a pathname to a .../bin/java for a core file. 136 try { 137 int unused = Integer.parseInt(args[0]); 138 // If we get here, we have a PID and not a core file name 139 pidText = args[0]; 140 } catch (NumberFormatException e) { 141 execPath = args[0]; 142 coreFilename = "core"; 143 } 144 break; 145 146 case (2): 147 execPath = args[0]; 148 coreFilename = args[1]; 149 break; 150 151 default: 152 System.out.println("HSDB Error: Too many options specified"); 153 doUsage(); 154 return; 155 } 156 } 157 158 private void attachDebugger(JVMDebugger d) { 159 agent.attach(d); 160 attached = true; 161 } 162 163 /** NOTE we are in a different thread here than either the main 164 thread or the Swing/AWT event handler thread, so we must be very 165 careful when creating or removing widgets */ 166 private void attachDebugger(String pidText) { 167 try { 168 this.pidText = pidText; 169 pid = Integer.parseInt(pidText); 170 } 171 catch (NumberFormatException e) { 172 System.err.print("Unable to parse process ID \"" + pidText + "\".\nPlease enter a number."); 173 } 174 175 try { 176 System.err.println("Attaching to process " + pid + ", please wait..."); 177 178 // FIXME: display exec'd debugger's output messages during this 179 // lengthy call 180 agent.attach(pid); |