0
|
1 /*
|
|
2 * Copyright 2002-2004 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.jdi;
|
|
26
|
|
27 import com.sun.jdi.connect.*;
|
|
28 import com.sun.jdi.Bootstrap;
|
|
29 import com.sun.jdi.VirtualMachine;
|
|
30 import com.sun.jdi.VirtualMachineManager;
|
|
31
|
|
32 import java.io.*;
|
|
33 import java.lang.reflect.*;
|
|
34 import java.net.*;
|
|
35 import java.util.*;
|
|
36
|
|
37 public class SACoreAttachingConnector extends ConnectorImpl implements AttachingConnector {
|
|
38
|
|
39 static final String ARG_COREFILE = "core";
|
|
40 static final String ARG_JAVA_EXECUTABLE = "javaExecutable";
|
|
41 private Transport transport;
|
|
42
|
|
43 public SACoreAttachingConnector(com.sun.tools.jdi.VirtualMachineManagerService ignored) {
|
|
44 this();
|
|
45 }
|
|
46
|
|
47 public SACoreAttachingConnector() {
|
|
48 super();
|
|
49 //fixme jjh Must create resources for these strings
|
|
50 addStringArgument(
|
|
51 ARG_JAVA_EXECUTABLE,
|
|
52 "Java Executable", //getString("sa.javaExecutable.label"),
|
|
53 "Pathname of Java Executable", //getString("sa.javaExecutable.description");
|
|
54 "",
|
|
55 true);
|
|
56
|
|
57 addStringArgument(
|
|
58 ARG_COREFILE,
|
|
59 "Corefile", // getString("sa.CoreFile.label"),
|
|
60 "Pathname of a corefile from a Java Process", //getString("sa.CoreFile.description"),
|
|
61 "core",
|
|
62 false);
|
|
63
|
|
64 transport = new Transport() {
|
|
65 public String name() {
|
|
66 return "filesystem";
|
|
67 }
|
|
68 };
|
|
69 }
|
|
70
|
|
71 // security check to see whether the caller can perform attach
|
|
72 private void checkCoreAttach(String corefile) {
|
|
73 SecurityManager sm = System.getSecurityManager();
|
|
74 if (sm != null) {
|
|
75 try {
|
|
76 // whether the caller can link against SA native library?
|
|
77 checkNativeLink(sm, System.getProperty("os.name"));
|
|
78 // check whether the caller can read the core file?
|
|
79 sm.checkRead(corefile);
|
|
80 } catch (SecurityException se) {
|
|
81 throw new SecurityException("permission denied to attach to " + corefile);
|
|
82 }
|
|
83 }
|
|
84 }
|
|
85
|
|
86 private VirtualMachine createVirtualMachine(Class vmImplClass,
|
|
87 String javaExec, String corefile)
|
|
88 throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
|
89 java.lang.reflect.Method connectByCoreMethod = vmImplClass.getMethod(
|
|
90 "createVirtualMachineForCorefile",
|
|
91 new Class[] {
|
|
92 VirtualMachineManager.class,
|
|
93 String.class, String.class,
|
|
94 Integer.TYPE
|
|
95 });
|
|
96 return (VirtualMachine) connectByCoreMethod.invoke(null,
|
|
97 new Object[] {
|
|
98 Bootstrap.virtualMachineManager(),
|
|
99 javaExec,
|
|
100 corefile,
|
|
101 new Integer(0)
|
|
102 });
|
|
103 }
|
|
104
|
|
105 public VirtualMachine attach(Map arguments) throws IOException,
|
|
106 IllegalConnectorArgumentsException {
|
|
107 String javaExec = argument(ARG_JAVA_EXECUTABLE, arguments).value();
|
|
108 if (javaExec == null || javaExec.equals("")) {
|
|
109 throw new IllegalConnectorArgumentsException("javaExec should be non-null and non-empty",
|
|
110 ARG_JAVA_EXECUTABLE);
|
|
111 }
|
|
112 String corefile = argument(ARG_COREFILE, arguments).value();
|
|
113 if (corefile == null || corefile.equals("")) {
|
|
114 throw new IllegalConnectorArgumentsException("corefile should be non-null and non-empty",
|
|
115 ARG_COREFILE);
|
|
116 }
|
|
117
|
|
118 checkCoreAttach(corefile);
|
|
119
|
|
120 VirtualMachine myVM = null;
|
|
121 try {
|
|
122 try {
|
|
123 Class vmImplClass = loadVirtualMachineImplClass();
|
|
124 myVM = createVirtualMachine(vmImplClass, javaExec, corefile);
|
|
125 } catch (InvocationTargetException ite) {
|
|
126 Class vmImplClass = handleVMVersionMismatch(ite);
|
|
127 if (vmImplClass != null) {
|
|
128 return createVirtualMachine(vmImplClass, javaExec, corefile);
|
|
129 } else {
|
|
130 throw ite;
|
|
131 }
|
|
132 }
|
|
133 } catch (Exception ee) {
|
|
134 if (DEBUG) {
|
|
135 System.out.println("VirtualMachineImpl() got an exception:");
|
|
136 ee.printStackTrace();
|
|
137 System.out.println("coreFile = " + corefile + ", javaExec = " + javaExec);
|
|
138 }
|
|
139 throw (IOException) new IOException().initCause(ee);
|
|
140 }
|
|
141 setVMDisposeObserver(myVM);
|
|
142 return myVM;
|
|
143 }
|
|
144
|
|
145 public String name() {
|
|
146 return "sun.jvm.hotspot.jdi.SACoreAttachingConnector";
|
|
147 }
|
|
148
|
|
149 public String description() {
|
|
150 return getString("This connector allows you to attach to a core file using the Serviceability Agent");
|
|
151 }
|
|
152
|
|
153 public Transport transport() {
|
|
154 return transport;
|
|
155 }
|
|
156 }
|