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 SAPIDAttachingConnector extends ConnectorImpl implements AttachingConnector {
|
|
38 static final String ARG_PID = "pid";
|
|
39 private Transport transport;
|
|
40
|
|
41 public SAPIDAttachingConnector(com.sun.tools.jdi.VirtualMachineManagerService ignored) {
|
|
42 this();
|
|
43 }
|
|
44
|
|
45 public SAPIDAttachingConnector() {
|
|
46 super();
|
|
47 // fixme jjh: create resources for the these strings,
|
|
48 addStringArgument(
|
|
49 ARG_PID,
|
|
50 "PID", //getString("sa.pid.label"),
|
|
51 "PID of a Java process", //getString("sa.pid.description");
|
|
52 "",
|
|
53 true);
|
|
54 transport = new Transport() {
|
|
55 public String name() {
|
|
56 return "local process";
|
|
57 }
|
|
58 };
|
|
59 }
|
|
60
|
|
61 // security check to see whether the caller can perform attach
|
|
62 private void checkProcessAttach(int pid) {
|
|
63 SecurityManager sm = System.getSecurityManager();
|
|
64 if (sm != null) {
|
|
65 String os = System.getProperty("os.name");
|
|
66 try {
|
|
67 // Whether the caller can perform link against SA native library?
|
|
68 checkNativeLink(sm, os);
|
|
69 if (os.equals("SunOS") || os.equals("Linux")) {
|
|
70 // Whether the caller can read /proc/<pid> file?
|
|
71 sm.checkRead("/proc/" + pid);
|
|
72 }
|
|
73 } catch (SecurityException se) {
|
|
74 throw new SecurityException("permission denied to attach to " + pid);
|
|
75 }
|
|
76 }
|
|
77 }
|
|
78
|
|
79 private VirtualMachine createVirtualMachine(Class virtualMachineImplClass, int pid)
|
|
80 throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
|
81 java.lang.reflect.Method createByPIDMethod
|
|
82 = virtualMachineImplClass.getMethod("createVirtualMachineForPID",
|
|
83 new Class[] {
|
|
84 VirtualMachineManager.class,
|
|
85 Integer.TYPE, Integer.TYPE
|
|
86 });
|
|
87 return (VirtualMachine) createByPIDMethod.invoke(null,
|
|
88 new Object[] {
|
|
89 Bootstrap.virtualMachineManager(),
|
|
90 new Integer(pid),
|
|
91 new Integer(0)
|
|
92 });
|
|
93 }
|
|
94
|
|
95 public VirtualMachine attach(Map arguments) throws IOException,
|
|
96 IllegalConnectorArgumentsException {
|
|
97 int pid = 0;
|
|
98 try {
|
|
99 pid = Integer.parseInt(argument(ARG_PID, arguments).value());
|
|
100 } catch (NumberFormatException nfe) {
|
|
101 throw (IllegalConnectorArgumentsException) new IllegalConnectorArgumentsException
|
|
102 (nfe.getMessage(), ARG_PID).initCause(nfe);
|
|
103 }
|
|
104
|
|
105 checkProcessAttach(pid);
|
|
106
|
|
107 VirtualMachine myVM = null;
|
|
108 try {
|
|
109 try {
|
|
110 Class vmImplClass = loadVirtualMachineImplClass();
|
|
111 myVM = createVirtualMachine(vmImplClass, pid);
|
|
112 } catch (InvocationTargetException ite) {
|
|
113 Class vmImplClass = handleVMVersionMismatch(ite);
|
|
114 if (vmImplClass != null) {
|
|
115 return createVirtualMachine(vmImplClass, pid);
|
|
116 } else {
|
|
117 throw ite;
|
|
118 }
|
|
119 }
|
|
120 } catch (Exception ee) {
|
|
121 if (DEBUG) {
|
|
122 System.out.println("VirtualMachineImpl() got an exception:");
|
|
123 ee.printStackTrace();
|
|
124 System.out.println("pid = " + pid);
|
|
125 }
|
|
126 throw (IOException) new IOException().initCause(ee);
|
|
127 }
|
|
128 setVMDisposeObserver(myVM);
|
|
129 return myVM;
|
|
130 }
|
|
131
|
|
132 public String name() {
|
|
133 return "sun.jvm.hotspot.jdi.SAPIDAttachingConnector";
|
|
134 }
|
|
135
|
|
136 public String description() {
|
|
137 return getString("This connector allows you to attach to a Java process using the Serviceability Agent");
|
|
138 }
|
|
139
|
|
140 public Transport transport() {
|
|
141 return transport;
|
|
142 }
|
|
143 }
|