src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/DebugServer.java
author jjg
Mon, 20 May 2019 10:57:57 -0700
changeset 54950 46ae54c3026d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8223663: Update links for tool guides Reviewed-by: alanb, erikj, darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
10547
ea4a2ec31ae2 7088955: add C2 IR support to the SA
never
parents: 5547
diff changeset
     2
 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
package sun.jvm.hotspot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import sun.jvm.hotspot.debugger.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
import sun.jvm.hotspot.runtime.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
import sun.jvm.hotspot.oops.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
public class DebugServer {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  private void usage() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
    System.out.println("usage: java " + getClass().getName() + " <pid> [server id]");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
    System.out.println("   or: java " + getClass().getName() + " <executable> <core> [server id]");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
    System.out.println("\"pid\" must be the process ID of a HotSpot process.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
    System.out.println("If reading a core file, \"executable\" must (currently) be the");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
    System.out.println("full path name to the precise java executable which generated");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
    System.out.println("the core file (not, on Solaris, the \"java\" wrapper script in");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
    System.out.println("the \"bin\" subdirectory of the JDK.)");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
    System.out.println("The \"server id\" is a unique name for a specific remote debuggee.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
    System.exit(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  public static void main(String[] args) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
    new DebugServer().run(args);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  private void run(String[] args) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
    if ((args.length < 1) || (args.length > 3)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
      usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    // Attempt to handle "-h" or "-help"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
    if (args[0].startsWith("-")) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
      usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
    int pid = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
    boolean usePid = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
    String coreFileName = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
    // FIXME: would be nice to pick this up from the core file
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
    // somehow, but that doesn't look possible. Should at least figure
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
    // it out from a path to the JDK.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
    String javaExecutableName = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
    String serverID = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
    switch (args.length) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
       case 1:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
         try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
           pid = Integer.parseInt(args[0]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
           usePid = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
         } catch (NumberFormatException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
           usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
         break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
       case 2:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
         // either we have pid and server id or exec file and core file
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
         try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
           pid = Integer.parseInt(args[0]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
           usePid = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
           serverID = args[1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
         } catch (NumberFormatException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
           pid = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
           usePid = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
           javaExecutableName = args[0];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
           coreFileName = args[1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
         break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
       case 3:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
         javaExecutableName = args[0];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
         coreFileName = args[1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
         serverID = args[2];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
         break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
       default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
         // should not happend, taken care already.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
         break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
    final HotSpotAgent agent = new HotSpotAgent();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
    try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
      if (usePid) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
        System.err.println("Attaching to process ID " + pid + " and starting RMI services, please wait...");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
        agent.startServer(pid, serverID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
        System.err.println("Attaching to core " + coreFileName +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
                           " from executable " + javaExecutableName + " and starting RMI services, please wait...");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
        agent.startServer(javaExecutableName, coreFileName, serverID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
    catch (DebuggerException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
      if (usePid) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
        System.err.print("Error attaching to process or starting server: ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
        System.err.print("Error attaching to core file or starting server: ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
      e.printStackTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
      System.exit(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
    // shutdown hook to clean-up the server in case of forced exit.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
    Runtime.getRuntime().addShutdownHook(new java.lang.Thread(
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
                          new Runnable() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
                             public void run() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
                                agent.shutdownServer();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
                             }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
                          }));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    System.err.println("Debugger attached and RMI services started.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
}