1 /*
   2  * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 package sun.jvm.hotspot;
  26 
  27 import java.io.PrintStream;
  28 import java.net.*;
  29 import java.rmi.*;
  30 import sun.jvm.hotspot.debugger.*;
  31 import sun.jvm.hotspot.debugger.proc.*;
  32 import sun.jvm.hotspot.debugger.remote.*;
  33 import sun.jvm.hotspot.debugger.win32.*;
  34 import sun.jvm.hotspot.debugger.windbg.*;
  35 import sun.jvm.hotspot.debugger.linux.*;
  36 import sun.jvm.hotspot.memory.*;
  37 import sun.jvm.hotspot.oops.*;
  38 import sun.jvm.hotspot.runtime.*;
  39 import sun.jvm.hotspot.types.*;
  40 import sun.jvm.hotspot.utilities.*;
  41 
  42 /** <P> This class wraps much of the basic functionality and is the
  43  * highest-level factory for VM data structures. It makes it simple
  44  * to start up the debugging system. </P>
  45  *
  46  * <P> FIXME: especially with the addition of remote debugging, this
  47  * has turned into a mess; needs rethinking. </P>
  48  */
  49 
  50 public class HotSpotAgent {
  51     private JVMDebugger debugger;
  52     private MachineDescription machDesc;
  53     private TypeDataBase db;
  54 
  55     private String os;
  56     private String cpu;
  57     private String fileSep;
  58 
  59     // The system can work in several ways:
  60     //  - Attaching to local process
  61     //  - Attaching to local core file
  62     //  - Connecting to remote debug server
  63     //  - Starting debug server for process
  64     //  - Starting debug server for core file
  65 
  66     // These are options for the "client" side of things
  67     private static final int PROCESS_MODE   = 0;
  68     private static final int CORE_FILE_MODE = 1;
  69     private static final int REMOTE_MODE    = 2;
  70     private int startupMode;
  71 
  72     // This indicates whether we are really starting a server or not
  73     private boolean isServer;
  74 
  75     // All possible required information for connecting
  76     private int pid;
  77     private String javaExecutableName;
  78     private String coreFileName;
  79     private String debugServerID;
  80 
  81     // All needed information for server side
  82     private String serverID;
  83 
  84     private String[] jvmLibNames;
  85 
  86     static void showUsage() {
  87     }
  88 
  89     public HotSpotAgent() {
  90         // for non-server add shutdown hook to clean-up debugger in case
  91         // of forced exit. For remote server, shutdown hook is added by
  92         // DebugServer.
  93         Runtime.getRuntime().addShutdownHook(new java.lang.Thread(
  94         new Runnable() {
  95             public void run() {
  96                 synchronized (HotSpotAgent.this) {
  97                     if (!isServer) {
  98                         detach();
  99                     }
 100                 }
 101             }
 102         }));
 103     }
 104 
 105     //--------------------------------------------------------------------------------
 106     // Accessors (once the system is set up)
 107     //
 108 
 109     public synchronized Debugger getDebugger() {
 110         return debugger;
 111     }
 112 
 113     public synchronized TypeDataBase getTypeDataBase() {
 114         return db;
 115     }
 116 
 117     //--------------------------------------------------------------------------------
 118     // Client-side operations
 119     //
 120 
 121     /** This attaches to a process running on the local machine. */
 122     public synchronized void attach(int processID)
 123     throws DebuggerException {
 124         if (debugger != null) {
 125             throw new DebuggerException("Already attached");
 126         }
 127         pid = processID;
 128         startupMode = PROCESS_MODE;
 129         isServer = false;
 130         go();
 131     }
 132 
 133     /** This opens a core file on the local machine */
 134     public synchronized void attach(String javaExecutableName, String coreFileName)
 135     throws DebuggerException {
 136         if (debugger != null) {
 137             throw new DebuggerException("Already attached");
 138         }
 139         if ((javaExecutableName == null) || (coreFileName == null)) {
 140             throw new DebuggerException("Both the core file name and Java executable name must be specified");
 141         }
 142         this.javaExecutableName = javaExecutableName;
 143         this.coreFileName = coreFileName;
 144         startupMode = CORE_FILE_MODE;
 145         isServer = false;
 146         go();
 147     }
 148 
 149     /** This attaches to a "debug server" on a remote machine; this
 150       remote server has already attached to a process or opened a
 151       core file and is waiting for RMI calls on the Debugger object to
 152       come in. */
 153     public synchronized void attach(String remoteServerID)
 154     throws DebuggerException {
 155         if (debugger != null) {
 156             throw new DebuggerException("Already attached to a process");
 157         }
 158         if (remoteServerID == null) {
 159             throw new DebuggerException("Debug server id must be specified");
 160         }
 161 
 162         debugServerID = remoteServerID;
 163         startupMode = REMOTE_MODE;
 164         isServer = false;
 165         go();
 166     }
 167 
 168     /** This should only be called by the user on the client machine,
 169       not the server machine */
 170     public synchronized boolean detach() throws DebuggerException {
 171         if (isServer) {
 172             throw new DebuggerException("Should not call detach() for server configuration");
 173         }
 174         return detachInternal();
 175     }
 176 
 177     //--------------------------------------------------------------------------------
 178     // Server-side operations
 179     //
 180 
 181     /** This attaches to a process running on the local machine and
 182       starts a debug server, allowing remote machines to connect and
 183       examine this process. Uses specified name to uniquely identify a
 184       specific debuggee on the server */
 185     public synchronized void startServer(int processID, String uniqueID) {
 186         if (debugger != null) {
 187             throw new DebuggerException("Already attached");
 188         }
 189         pid = processID;
 190         startupMode = PROCESS_MODE;
 191         isServer = true;
 192         serverID = uniqueID;
 193         go();
 194     }
 195 
 196     /** This attaches to a process running on the local machine and
 197       starts a debug server, allowing remote machines to connect and
 198       examine this process. */
 199     public synchronized void startServer(int processID)
 200     throws DebuggerException {
 201         startServer(processID, null);
 202     }
 203 
 204     /** This opens a core file on the local machine and starts a debug
 205       server, allowing remote machines to connect and examine this
 206       core file. Uses supplied uniqueID to uniquely identify a specific
 207       debugee */
 208     public synchronized void startServer(String javaExecutableName,
 209     String coreFileName,
 210     String uniqueID) {
 211         if (debugger != null) {
 212             throw new DebuggerException("Already attached");
 213         }
 214         if ((javaExecutableName == null) || (coreFileName == null)) {
 215             throw new DebuggerException("Both the core file name and Java executable name must be specified");
 216         }
 217         this.javaExecutableName = javaExecutableName;
 218         this.coreFileName = coreFileName;
 219         startupMode = CORE_FILE_MODE;
 220         isServer = true;
 221         serverID = uniqueID;
 222         go();
 223     }
 224 
 225     /** This opens a core file on the local machine and starts a debug
 226       server, allowing remote machines to connect and examine this
 227       core file. */
 228     public synchronized void startServer(String javaExecutableName, String coreFileName)
 229     throws DebuggerException {
 230         startServer(javaExecutableName, coreFileName, null);
 231     }
 232 
 233     /** This may only be called on the server side after startServer()
 234       has been called */
 235     public synchronized boolean shutdownServer() throws DebuggerException {
 236         if (!isServer) {
 237             throw new DebuggerException("Should not call shutdownServer() for client configuration");
 238         }
 239         return detachInternal();
 240     }
 241 
 242 
 243     //--------------------------------------------------------------------------------
 244     // Internals only below this point
 245     //
 246 
 247     private boolean detachInternal() {
 248         if (debugger == null) {
 249             return false;
 250         }
 251         boolean retval = true;
 252         if (!isServer) {
 253             VM.shutdown();
 254         }
 255         // We must not call detach() if we are a client and are connected
 256         // to a remote debugger
 257         Debugger dbg = null;
 258         DebuggerException ex = null;
 259         if (isServer) {
 260             try {
 261                 RMIHelper.unbind(serverID);
 262             }
 263             catch (DebuggerException de) {
 264                 ex = de;
 265             }
 266             dbg = debugger;
 267         } else {
 268             if (startupMode != REMOTE_MODE) {
 269                 dbg = debugger;
 270             }
 271         }
 272         if (dbg != null) {
 273             retval = dbg.detach();
 274         }
 275 
 276         debugger = null;
 277         machDesc = null;
 278         db = null;
 279         if (ex != null) {
 280             throw(ex);
 281         }
 282         return retval;
 283     }
 284 
 285     private void go() {
 286         setupDebugger();
 287         setupVM();
 288     }
 289 
 290     private void setupDebugger() {
 291         if (startupMode != REMOTE_MODE) {
 292             //
 293             // Local mode (client attaching to local process or setting up
 294             // server, but not client attaching to server)
 295             //
 296 
 297             try {
 298                 os  = PlatformInfo.getOS();
 299                 cpu = PlatformInfo.getCPU();
 300             }
 301             catch (UnsupportedPlatformException e) {
 302                 throw new DebuggerException(e);
 303             }
 304             fileSep = System.getProperty("file.separator");
 305 
 306             if (os.equals("solaris")) {
 307                 setupDebuggerSolaris();
 308             } else if (os.equals("win32")) {
 309                 setupDebuggerWin32();
 310             } else if (os.equals("linux")) {
 311                 setupDebuggerLinux();
 312             } else {
 313                 // Add support for more operating systems here
 314                 throw new DebuggerException("Operating system " + os + " not yet supported");
 315             }
 316 
 317             if (isServer) {
 318                 RemoteDebuggerServer remote = null;
 319                 try {
 320                     remote = new RemoteDebuggerServer(debugger);
 321                 }
 322                 catch (RemoteException rem) {
 323                     throw new DebuggerException(rem);
 324                 }
 325                 RMIHelper.rebind(serverID, remote);
 326             }
 327         } else {
 328             //
 329             // Remote mode (client attaching to server)
 330             //
 331 
 332             // Create and install a security manager
 333 
 334             // FIXME: currently commented out because we were having
 335             // security problems since we're "in the sun.* hierarchy" here.
 336             // Perhaps a permissive policy file would work around this. In
 337             // the long run, will probably have to move into com.sun.*.
 338 
 339             //    if (System.getSecurityManager() == null) {
 340             //      System.setSecurityManager(new RMISecurityManager());
 341             //    }
 342 
 343             connectRemoteDebugger();
 344         }
 345     }
 346 
 347     private void setupVM() {
 348         // We need to instantiate a HotSpotTypeDataBase on both the client
 349         // and server machine. On the server it is only currently used to
 350         // configure the Java primitive type sizes (which we should
 351         // consider making constant). On the client it is used to
 352         // configure the VM.
 353 
 354         try {
 355             if (os.equals("solaris")) {
 356                 db = new HotSpotTypeDataBase(machDesc,
 357                 new HotSpotSolarisVtblAccess(debugger, jvmLibNames),
 358                 debugger, jvmLibNames);
 359             } else if (os.equals("win32")) {
 360                 db = new HotSpotTypeDataBase(machDesc,
 361                 new Win32VtblAccess(debugger, jvmLibNames),
 362                 debugger, jvmLibNames);
 363             } else if (os.equals("linux")) {
 364                 db = new HotSpotTypeDataBase(machDesc,
 365                 new LinuxVtblAccess(debugger, jvmLibNames),
 366                 debugger, jvmLibNames);
 367             } else {
 368                 throw new DebuggerException("OS \"" + os + "\" not yet supported (no VtblAccess yet)");
 369             }
 370         }
 371         catch (NoSuchSymbolException e) {
 372             throw new DebuggerException("Doesn't appear to be a HotSpot VM (could not find symbol \"" +
 373             e.getSymbol() + "\" in remote process)");
 374         }
 375 
 376         if (startupMode != REMOTE_MODE) {
 377             // Configure the debugger with the primitive type sizes just obtained from the VM
 378             debugger.configureJavaPrimitiveTypeSizes(db.getJBooleanType().getSize(),
 379             db.getJByteType().getSize(),
 380             db.getJCharType().getSize(),
 381             db.getJDoubleType().getSize(),
 382             db.getJFloatType().getSize(),
 383             db.getJIntType().getSize(),
 384             db.getJLongType().getSize(),
 385             db.getJShortType().getSize());
 386         }
 387 
 388         if (!isServer) {
 389             // Do not initialize the VM on the server (unnecessary, since it's
 390             // instantiated on the client)
 391             try {
 392                 VM.initialize(db, debugger);
 393             } catch (DebuggerException e) {
 394                 throw (e);
 395             } catch (Exception e) {
 396                 throw new DebuggerException(e);
 397             }
 398         }
 399     }
 400 
 401     //--------------------------------------------------------------------------------
 402     // OS-specific debugger setup/connect routines
 403     //
 404 
 405     //
 406     // Solaris
 407     //
 408 
 409     private void setupDebuggerSolaris() {
 410         setupJVMLibNamesSolaris();
 411         ProcDebuggerLocal dbg = new ProcDebuggerLocal(null, true);
 412         debugger = dbg;
 413         attachDebugger();
 414 
 415             // Set up CPU-dependent stuff
 416         if (cpu.equals("x86")) {
 417             machDesc = new MachineDescriptionIntelX86();
 418         } else if (cpu.equals("sparc")) {
 419             int addressSize = dbg.getRemoteProcessAddressSize();
 420             if (addressSize == -1) {
 421                 throw new DebuggerException("Error occurred while trying to determine the remote process's " +
 422                                             "address size");
 423             }
 424 
 425             if (addressSize == 32) {
 426                 machDesc = new MachineDescriptionSPARC32Bit();
 427             } else if (addressSize == 64) {
 428                 machDesc = new MachineDescriptionSPARC64Bit();
 429             } else {
 430                 throw new DebuggerException("Address size " + addressSize + " is not supported on SPARC");
 431             }
 432         } else if (cpu.equals("amd64")) {
 433             machDesc = new MachineDescriptionAMD64();
 434         } else {
 435             throw new DebuggerException("Solaris only supported on sparc/sparcv9/x86/amd64");
 436         }
 437 
 438         dbg.setMachineDescription(machDesc);
 439     }
 440 
 441     private void connectRemoteDebugger() throws DebuggerException {
 442         RemoteDebugger remote =
 443         (RemoteDebugger) RMIHelper.lookup(debugServerID);
 444         debugger = new RemoteDebuggerClient(remote);
 445         machDesc = ((RemoteDebuggerClient) debugger).getMachineDescription();
 446         os = debugger.getOS();
 447         if (os.equals("solaris")) {
 448             setupJVMLibNamesSolaris();
 449         } else if (os.equals("win32")) {
 450             setupJVMLibNamesWin32();
 451         } else if (os.equals("linux")) {
 452             setupJVMLibNamesLinux();
 453         } else {
 454             throw new RuntimeException("Unknown OS type");
 455         }
 456 
 457         cpu = debugger.getCPU();
 458     }
 459 
 460     private void setupJVMLibNamesSolaris() {
 461         jvmLibNames = new String[] { "libjvm.so", "libjvm_g.so", "gamma_g" };
 462     }
 463 
 464     //
 465     // Win32
 466     //
 467 
 468     private void setupDebuggerWin32() {
 469         setupJVMLibNamesWin32();
 470 
 471         if (cpu.equals("x86")) {
 472             machDesc = new MachineDescriptionIntelX86();
 473         } else if (cpu.equals("amd64")) {
 474             machDesc = new MachineDescriptionAMD64();
 475         } else if (cpu.equals("ia64")) {
 476             machDesc = new MachineDescriptionIA64();
 477         } else {
 478             throw new DebuggerException("Win32 supported under x86, amd64 and ia64 only");
 479         }
 480 
 481         // Note we do not use a cache for the local debugger in server
 482         // mode; it will be taken care of on the client side (once remote
 483         // debugging is implemented).
 484 
 485         if (System.getProperty("sun.jvm.hotspot.debugger.useWindbgDebugger") != null) {
 486             debugger = new WindbgDebuggerLocal(machDesc, !isServer);
 487         } else {
 488             debugger = new Win32DebuggerLocal(machDesc, !isServer);
 489         }
 490 
 491         attachDebugger();
 492 
 493         // FIXME: add support for server mode
 494     }
 495 
 496     private void setupJVMLibNamesWin32() {
 497         jvmLibNames = new String[] { "jvm.dll", "jvm_g.dll" };
 498     }
 499 
 500     //
 501     // Linux
 502     //
 503 
 504     private void setupDebuggerLinux() {
 505         setupJVMLibNamesLinux();
 506 
 507         if (cpu.equals("x86")) {
 508             machDesc = new MachineDescriptionIntelX86();
 509         } else if (cpu.equals("ia64")) {
 510             machDesc = new MachineDescriptionIA64();
 511         } else if (cpu.equals("amd64")) {
 512             machDesc = new MachineDescriptionAMD64();
 513         } else if (cpu.equals("sparc")) {
 514             if (LinuxDebuggerLocal.getAddressSize()==8) {
 515                     machDesc = new MachineDescriptionSPARC64Bit();
 516             } else {
 517                     machDesc = new MachineDescriptionSPARC32Bit();
 518             }
 519         } else {
 520             throw new DebuggerException("Linux only supported on x86/ia64/amd64/sparc/sparc64");
 521         }
 522 
 523         LinuxDebuggerLocal dbg =
 524         new LinuxDebuggerLocal(machDesc, !isServer);
 525         debugger = dbg;
 526 
 527         attachDebugger();
 528     }
 529 
 530     private void setupJVMLibNamesLinux() {
 531         jvmLibNames = new String[] { "libjvm.so", "libjvm_g.so" };
 532     }
 533 
 534     /** Convenience routine which should be called by per-platform
 535       debugger setup. Should not be called when startupMode is
 536       REMOTE_MODE. */
 537     private void attachDebugger() {
 538         if (startupMode == PROCESS_MODE) {
 539             debugger.attach(pid);
 540         } else if (startupMode == CORE_FILE_MODE) {
 541             debugger.attach(javaExecutableName, coreFileName);
 542         } else {
 543             throw new DebuggerException("Should not call attach() for startupMode == " + startupMode);
 544         }
 545     }
 546 }