diff agent/src/share/classes/sun/jvm/hotspot/HotSpotAgent.java @ 11054:38ea2efa32a7

8010278: SA: provide mechanism for using an alternative SA debugger back-end. Reviewed-by: sla, dsamersoff
author kevinw
date Wed, 26 Jun 2013 00:01:20 +0100
parents f6a055fcf47d
children
line wrap: on
line diff
--- a/agent/src/share/classes/sun/jvm/hotspot/HotSpotAgent.java	Tue Jun 25 14:11:57 2013 +0200
+++ b/agent/src/share/classes/sun/jvm/hotspot/HotSpotAgent.java	Wed Jun 26 00:01:20 2013 +0100
@@ -25,6 +25,8 @@
 package sun.jvm.hotspot;
 
 import java.rmi.RemoteException;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
 
 import sun.jvm.hotspot.debugger.Debugger;
 import sun.jvm.hotspot.debugger.DebuggerException;
@@ -63,7 +65,6 @@
 
     private String os;
     private String cpu;
-    private String fileSep;
 
     // The system can work in several ways:
     //  - Attaching to local process
@@ -155,6 +156,14 @@
         go();
     }
 
+    /** This uses a JVMDebugger that is already attached to the core or process */
+    public synchronized void attach(JVMDebugger d)
+    throws DebuggerException {
+        debugger = d;
+        isServer = false;
+        go();
+    }
+
     /** This attaches to a "debug server" on a remote machine; this
       remote server has already attached to a process or opened a
       core file and is waiting for RMI calls on the Debugger object to
@@ -303,28 +312,37 @@
             // server, but not client attaching to server)
             //
 
-            try {
-                os  = PlatformInfo.getOS();
-                cpu = PlatformInfo.getCPU();
-            }
-            catch (UnsupportedPlatformException e) {
-                throw new DebuggerException(e);
-            }
-            fileSep = System.getProperty("file.separator");
+            // Handle existing or alternate JVMDebugger:
+            // these will set os, cpu independently of our PlatformInfo implementation.
+            String alternateDebugger = System.getProperty("sa.altDebugger");
+            if (debugger != null) {
+                setupDebuggerExisting();
+
+            } else if (alternateDebugger != null) {
+                setupDebuggerAlternate(alternateDebugger);
 
-            if (os.equals("solaris")) {
-                setupDebuggerSolaris();
-            } else if (os.equals("win32")) {
-                setupDebuggerWin32();
-            } else if (os.equals("linux")) {
-                setupDebuggerLinux();
-            } else if (os.equals("bsd")) {
-                setupDebuggerBsd();
-            } else if (os.equals("darwin")) {
-                setupDebuggerDarwin();
             } else {
-                // Add support for more operating systems here
-                throw new DebuggerException("Operating system " + os + " not yet supported");
+                // Otherwise, os, cpu are those of our current platform:
+                try {
+                    os  = PlatformInfo.getOS();
+                    cpu = PlatformInfo.getCPU();
+                } catch (UnsupportedPlatformException e) {
+                   throw new DebuggerException(e);
+                }
+                if (os.equals("solaris")) {
+                    setupDebuggerSolaris();
+                } else if (os.equals("win32")) {
+                    setupDebuggerWin32();
+                } else if (os.equals("linux")) {
+                    setupDebuggerLinux();
+                } else if (os.equals("bsd")) {
+                    setupDebuggerBsd();
+                } else if (os.equals("darwin")) {
+                    setupDebuggerDarwin();
+                } else {
+                    // Add support for more operating systems here
+                    throw new DebuggerException("Operating system " + os + " not yet supported");
+                }
             }
 
             if (isServer) {
@@ -423,6 +441,41 @@
     // OS-specific debugger setup/connect routines
     //
 
+    // Use the existing JVMDebugger, as passed to our constructor.
+    // Retrieve os and cpu from that debugger, not the current platform.
+    private void setupDebuggerExisting() {
+
+        os = debugger.getOS();
+        cpu = debugger.getCPU();
+        setupJVMLibNames(os);
+        machDesc = debugger.getMachineDescription();
+    }
+
+    // Given a classname, load an alternate implementation of JVMDebugger.
+    private void setupDebuggerAlternate(String alternateName) {
+
+        try {
+            Class c = Class.forName(alternateName);
+            Constructor cons = c.getConstructor();
+            debugger = (JVMDebugger) cons.newInstance();
+            attachDebugger();
+            setupDebuggerExisting();
+
+        } catch (ClassNotFoundException cnfe) {
+            throw new DebuggerException("Cannot find alternate SA Debugger: '" + alternateName + "'");
+        } catch (NoSuchMethodException nsme) {
+            throw new DebuggerException("Alternate SA Debugger: '" + alternateName + "' has missing constructor.");
+        } catch (InstantiationException ie) {
+            throw new DebuggerException("Alternate SA Debugger: '" + alternateName + "' fails to initialise: ", ie);
+        } catch (IllegalAccessException iae) {
+            throw new DebuggerException("Alternate SA Debugger: '" + alternateName + "' fails to initialise: ", iae);
+        } catch (InvocationTargetException iae) {
+            throw new DebuggerException("Alternate SA Debugger: '" + alternateName + "' fails to initialise: ", iae);
+        }
+
+        System.err.println("Loaded alternate HotSpot SA Debugger: " + alternateName);
+    }
+
     //
     // Solaris
     //
@@ -466,6 +519,11 @@
         debugger = new RemoteDebuggerClient(remote);
         machDesc = ((RemoteDebuggerClient) debugger).getMachineDescription();
         os = debugger.getOS();
+        setupJVMLibNames(os);
+        cpu = debugger.getCPU();
+    }
+
+    private void setupJVMLibNames(String os) {
         if (os.equals("solaris")) {
             setupJVMLibNamesSolaris();
         } else if (os.equals("win32")) {
@@ -479,8 +537,6 @@
         } else {
             throw new RuntimeException("Unknown OS type");
         }
-
-        cpu = debugger.getCPU();
     }
 
     private void setupJVMLibNamesSolaris() {