author | katleman |
Thu, 06 Oct 2011 14:01:37 -0700 | |
changeset 10648 | 8b314218e3ab |
parent 9035 | 1255eb81cc2f |
child 14698 | 9294fcf94c46 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
7978
diff
changeset
|
2 |
* Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
#include <ctype.h> |
|
27 |
||
28 |
#include "util.h" |
|
29 |
#include "commonRef.h" |
|
30 |
#include "debugDispatch.h" |
|
31 |
#include "eventHandler.h" |
|
32 |
#include "eventHelper.h" |
|
33 |
#include "threadControl.h" |
|
34 |
#include "stepControl.h" |
|
35 |
#include "transport.h" |
|
36 |
#include "classTrack.h" |
|
37 |
#include "debugLoop.h" |
|
38 |
#include "bag.h" |
|
39 |
#include "invoker.h" |
|
40 |
||
41 |
/* How the options get to OnLoad: */ |
|
42 |
#define XDEBUG "-Xdebug" |
|
43 |
#define XRUN "-Xrunjdwp" |
|
44 |
#define AGENTLIB "-agentlib:jdwp" |
|
45 |
||
46 |
/* Debug version defaults */ |
|
47 |
#ifdef DEBUG |
|
48 |
#define DEFAULT_ASSERT_ON JNI_TRUE |
|
49 |
#define DEFAULT_ASSERT_FATAL JNI_TRUE |
|
50 |
#define DEFAULT_LOGFILE "jdwp.log" |
|
51 |
#else |
|
52 |
#define DEFAULT_ASSERT_ON JNI_FALSE |
|
53 |
#define DEFAULT_ASSERT_FATAL JNI_FALSE |
|
54 |
#define DEFAULT_LOGFILE NULL |
|
55 |
#endif |
|
56 |
||
57 |
static jboolean vmInitialized; |
|
58 |
static jrawMonitorID initMonitor; |
|
59 |
static jboolean initComplete; |
|
60 |
static jbyte currentSessionID; |
|
61 |
||
62 |
/* |
|
63 |
* Options set through the OnLoad options string. All of these values |
|
64 |
* are set once at VM startup and never reset. |
|
65 |
*/ |
|
66 |
static jboolean isServer = JNI_FALSE; /* Listens for connecting debuggers? */ |
|
67 |
static jboolean isStrict = JNI_FALSE; /* Unused */ |
|
68 |
static jboolean useStandardAlloc = JNI_FALSE; /* Use standard malloc/free? */ |
|
69 |
static struct bag *transports; /* of TransportSpec */ |
|
70 |
||
71 |
static jboolean initOnStartup = JNI_TRUE; /* init immediately */ |
|
72 |
static char *initOnException = NULL; /* init when this exception thrown */ |
|
73 |
static jboolean initOnUncaught = JNI_FALSE; /* init when uncaught exc thrown */ |
|
74 |
||
75 |
static char *launchOnInit = NULL; /* launch this app during init */ |
|
76 |
static jboolean suspendOnInit = JNI_TRUE; /* suspend all app threads after init */ |
|
77 |
static jboolean dopause = JNI_FALSE; /* pause for debugger attach */ |
|
78 |
static jboolean docoredump = JNI_FALSE; /* core dump on exit */ |
|
79 |
static char *logfile = NULL; /* Name of logfile (if logging) */ |
|
80 |
static unsigned logflags = 0; /* Log flags */ |
|
81 |
||
82 |
static char *names; /* strings derived from OnLoad options */ |
|
83 |
||
84 |
/* |
|
85 |
* Elements of the transports bag |
|
86 |
*/ |
|
87 |
typedef struct TransportSpec { |
|
88 |
char *name; |
|
89 |
char *address; |
|
90 |
long timeout; |
|
91 |
} TransportSpec; |
|
92 |
||
93 |
/* |
|
94 |
* Forward Refs |
|
95 |
*/ |
|
96 |
static void JNICALL cbEarlyVMInit(jvmtiEnv*, JNIEnv *, jthread); |
|
97 |
static void JNICALL cbEarlyVMDeath(jvmtiEnv*, JNIEnv *); |
|
98 |
static void JNICALL cbEarlyException(jvmtiEnv*, JNIEnv *, |
|
99 |
jthread, jmethodID, jlocation, jobject, jmethodID, jlocation); |
|
100 |
||
101 |
static void initialize(JNIEnv *env, jthread thread, EventIndex triggering_ei); |
|
102 |
static jboolean parseOptions(char *str); |
|
103 |
||
104 |
/* |
|
105 |
* Phase 1: Initial load. |
|
106 |
* |
|
107 |
* OnLoad is called by the VM immediately after the back-end |
|
108 |
* library is loaded. We can do very little in this function since |
|
109 |
* the VM has not completed initialization. So, we parse the JDWP |
|
110 |
* options and set up a simple initial event callbacks for JVMTI events. |
|
111 |
* When a triggering event occurs, that callback will begin debugger initialization. |
|
112 |
*/ |
|
113 |
||
114 |
/* Get a static area to hold the Global Data */ |
|
115 |
static BackendGlobalData * |
|
116 |
get_gdata(void) |
|
117 |
{ |
|
118 |
static BackendGlobalData s; |
|
119 |
(void)memset(&s, 0, sizeof(BackendGlobalData)); |
|
120 |
return &s; |
|
121 |
} |
|
122 |
||
123 |
static jvmtiError |
|
124 |
set_event_notification(jvmtiEventMode mode, EventIndex ei) |
|
125 |
{ |
|
126 |
jvmtiError error; |
|
127 |
error = JVMTI_FUNC_PTR(gdata->jvmti,SetEventNotificationMode) |
|
128 |
(gdata->jvmti, mode, eventIndex2jvmti(ei), NULL); |
|
129 |
if (error != JVMTI_ERROR_NONE) { |
|
130 |
ERROR_MESSAGE(("JDWP unable to configure initial JVMTI event %s: %s(%d)", |
|
131 |
eventText(ei), jvmtiErrorText(error), error)); |
|
132 |
} |
|
133 |
return error; |
|
134 |
} |
|
135 |
||
7978
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
136 |
typedef struct { |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
137 |
int major; |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
138 |
int minor; |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
139 |
} version_type; |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
140 |
|
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
141 |
typedef struct { |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
142 |
version_type runtime; |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
143 |
version_type compiletime; |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
144 |
} compatible_versions_type; |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
145 |
|
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
146 |
/* |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
147 |
* List of explicitly compatible JVMTI versions, specified as |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
148 |
* { runtime version, compile-time version } pairs. -1 is a wildcard. |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
149 |
*/ |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
150 |
static int nof_compatible_versions = 3; |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
151 |
static compatible_versions_type compatible_versions_list[] = { |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
152 |
/* |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
153 |
* FIXUP: Allow version 0 to be compatible with anything |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
154 |
* Special check for FCS of 1.0. |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
155 |
*/ |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
156 |
{ { 0, -1 }, { -1, -1 } }, |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
157 |
{ { -1, -1 }, { 0, -1 } }, |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
158 |
/* |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
159 |
* 1.2 is runtime compatible with 1.1 -- just make sure to check the |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
160 |
* version before using any new 1.2 features |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
161 |
*/ |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
162 |
{ { 1, 1 }, { 1, 2 } } |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
163 |
}; |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
164 |
|
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
165 |
|
2 | 166 |
/* Logic to determine JVMTI version compatibility */ |
167 |
static jboolean |
|
168 |
compatible_versions(jint major_runtime, jint minor_runtime, |
|
169 |
jint major_compiletime, jint minor_compiletime) |
|
170 |
{ |
|
7978
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
171 |
/* |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
172 |
* First check to see if versions are explicitly compatible via the |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
173 |
* list specified above. |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
174 |
*/ |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
175 |
int i; |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
176 |
for (i = 0; i < nof_compatible_versions; ++i) { |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
177 |
version_type runtime = compatible_versions_list[i].runtime; |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
178 |
version_type comptime = compatible_versions_list[i].compiletime; |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
179 |
|
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
180 |
if ((major_runtime == runtime.major || runtime.major == -1) && |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
181 |
(minor_runtime == runtime.minor || runtime.minor == -1) && |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
182 |
(major_compiletime == comptime.major || comptime.major == -1) && |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
183 |
(minor_compiletime == comptime.minor || comptime.minor == -1)) { |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
184 |
return JNI_TRUE; |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
185 |
} |
2 | 186 |
} |
7978
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
187 |
|
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
188 |
return major_runtime == major_compiletime && |
120267233d5e
6436034: Instance filter doesn't filter event if it occurs in native method
kamg
parents:
5506
diff
changeset
|
189 |
minor_runtime >= minor_compiletime; |
2 | 190 |
} |
191 |
||
192 |
/* OnLoad startup: |
|
193 |
* Returning JNI_ERR will cause the java_g VM to core dump, be careful. |
|
194 |
*/ |
|
195 |
JNIEXPORT jint JNICALL |
|
196 |
Agent_OnLoad(JavaVM *vm, char *options, void *reserved) |
|
197 |
{ |
|
198 |
jvmtiError error; |
|
199 |
jvmtiCapabilities needed_capabilities; |
|
200 |
jvmtiCapabilities potential_capabilities; |
|
201 |
jint jvmtiCompileTimeMajorVersion; |
|
202 |
jint jvmtiCompileTimeMinorVersion; |
|
203 |
jint jvmtiCompileTimeMicroVersion; |
|
204 |
||
205 |
/* See if it's already loaded */ |
|
206 |
if ( gdata!=NULL && gdata->isLoaded==JNI_TRUE ) { |
|
207 |
ERROR_MESSAGE(("Cannot load this JVM TI agent twice, check your java command line for duplicate jdwp options.")); |
|
208 |
return JNI_ERR; |
|
209 |
} |
|
210 |
||
211 |
/* If gdata is defined and the VM died, why are we here? */ |
|
212 |
if ( gdata!=NULL && gdata->vmDead ) { |
|
213 |
ERROR_MESSAGE(("JDWP unable to load, VM died")); |
|
214 |
return JNI_ERR; |
|
215 |
} |
|
216 |
||
217 |
/* Get global data area */ |
|
218 |
gdata = get_gdata(); |
|
219 |
if (gdata == NULL) { |
|
220 |
ERROR_MESSAGE(("JDWP unable to allocate memory")); |
|
221 |
return JNI_ERR; |
|
222 |
} |
|
223 |
gdata->isLoaded = JNI_TRUE; |
|
224 |
||
225 |
/* Start filling in gdata */ |
|
226 |
gdata->jvm = vm; |
|
227 |
vmInitialized = JNI_FALSE; |
|
228 |
gdata->vmDead = JNI_FALSE; |
|
229 |
||
230 |
/* Npt and Utf function init */ |
|
231 |
NPT_INITIALIZE(&(gdata->npt), NPT_VERSION, NULL); |
|
232 |
if (gdata->npt == NULL) { |
|
233 |
ERROR_MESSAGE(("JDWP: unable to initialize NPT library")); |
|
234 |
return JNI_ERR; |
|
235 |
} |
|
236 |
gdata->npt->utf = (gdata->npt->utfInitialize)(NULL); |
|
237 |
if (gdata->npt->utf == NULL) { |
|
238 |
ERROR_MESSAGE(("JDWP: UTF function initialization failed")); |
|
239 |
return JNI_ERR; |
|
240 |
} |
|
241 |
||
242 |
/* Get the JVMTI Env, IMPORTANT: Do this first! For jvmtiAllocate(). */ |
|
243 |
error = JVM_FUNC_PTR(vm,GetEnv) |
|
244 |
(vm, (void **)&(gdata->jvmti), JVMTI_VERSION_1); |
|
245 |
if (error != JNI_OK) { |
|
246 |
ERROR_MESSAGE(("JDWP unable to access JVMTI Version 1 (0x%x)," |
|
247 |
" is your J2SE a 1.5 or newer version?" |
|
248 |
" JNIEnv's GetEnv() returned %d", |
|
249 |
JVMTI_VERSION_1, error)); |
|
250 |
forceExit(1); /* Kill entire process, no core dump */ |
|
251 |
} |
|
252 |
||
253 |
/* Check to make sure the version of jvmti.h we compiled with |
|
254 |
* matches the runtime version we are using. |
|
255 |
*/ |
|
256 |
jvmtiCompileTimeMajorVersion = ( JVMTI_VERSION & JVMTI_VERSION_MASK_MAJOR ) |
|
257 |
>> JVMTI_VERSION_SHIFT_MAJOR; |
|
258 |
jvmtiCompileTimeMinorVersion = ( JVMTI_VERSION & JVMTI_VERSION_MASK_MINOR ) |
|
259 |
>> JVMTI_VERSION_SHIFT_MINOR; |
|
260 |
jvmtiCompileTimeMicroVersion = ( JVMTI_VERSION & JVMTI_VERSION_MASK_MICRO ) |
|
261 |
>> JVMTI_VERSION_SHIFT_MICRO; |
|
262 |
||
263 |
/* Check for compatibility */ |
|
264 |
if ( !compatible_versions(jvmtiMajorVersion(), jvmtiMinorVersion(), |
|
265 |
jvmtiCompileTimeMajorVersion, jvmtiCompileTimeMinorVersion) ) { |
|
266 |
||
267 |
ERROR_MESSAGE(("This jdwp native library will not work with this VM's " |
|
268 |
"version of JVMTI (%d.%d.%d), it needs JVMTI %d.%d[.%d].", |
|
269 |
jvmtiMajorVersion(), |
|
270 |
jvmtiMinorVersion(), |
|
271 |
jvmtiMicroVersion(), |
|
272 |
jvmtiCompileTimeMajorVersion, |
|
273 |
jvmtiCompileTimeMinorVersion, |
|
274 |
jvmtiCompileTimeMicroVersion)); |
|
275 |
||
276 |
/* Do not let VM get a fatal error, we don't want a core dump here. */ |
|
277 |
forceExit(1); /* Kill entire process, no core dump wanted */ |
|
278 |
} |
|
279 |
||
280 |
/* Parse input options */ |
|
281 |
if (!parseOptions(options)) { |
|
282 |
/* No message necessary, should have been printed out already */ |
|
283 |
/* Do not let VM get a fatal error, we don't want a core dump here. */ |
|
284 |
forceExit(1); /* Kill entire process, no core dump wanted */ |
|
285 |
} |
|
286 |
||
287 |
LOG_MISC(("Onload: %s", options)); |
|
288 |
||
289 |
/* Get potential capabilities */ |
|
290 |
(void)memset(&potential_capabilities,0,sizeof(potential_capabilities)); |
|
291 |
error = JVMTI_FUNC_PTR(gdata->jvmti,GetPotentialCapabilities) |
|
292 |
(gdata->jvmti, &potential_capabilities); |
|
293 |
if (error != JVMTI_ERROR_NONE) { |
|
294 |
ERROR_MESSAGE(("JDWP unable to get potential JVMTI capabilities: %s(%d)", |
|
295 |
jvmtiErrorText(error), error)); |
|
296 |
return JNI_ERR; |
|
297 |
} |
|
298 |
||
299 |
/* Fill in ones that we must have */ |
|
300 |
(void)memset(&needed_capabilities,0,sizeof(needed_capabilities)); |
|
301 |
needed_capabilities.can_access_local_variables = 1; |
|
302 |
needed_capabilities.can_generate_single_step_events = 1; |
|
303 |
needed_capabilities.can_generate_exception_events = 1; |
|
304 |
needed_capabilities.can_generate_frame_pop_events = 1; |
|
305 |
needed_capabilities.can_generate_breakpoint_events = 1; |
|
306 |
needed_capabilities.can_suspend = 1; |
|
307 |
needed_capabilities.can_generate_method_entry_events = 1; |
|
308 |
needed_capabilities.can_generate_method_exit_events = 1; |
|
309 |
needed_capabilities.can_generate_garbage_collection_events = 1; |
|
310 |
needed_capabilities.can_maintain_original_method_order = 1; |
|
311 |
needed_capabilities.can_generate_monitor_events = 1; |
|
312 |
needed_capabilities.can_tag_objects = 1; |
|
313 |
||
314 |
/* And what potential ones that would be nice to have */ |
|
315 |
needed_capabilities.can_force_early_return |
|
316 |
= potential_capabilities.can_force_early_return; |
|
317 |
needed_capabilities.can_generate_field_modification_events |
|
318 |
= potential_capabilities.can_generate_field_modification_events; |
|
319 |
needed_capabilities.can_generate_field_access_events |
|
320 |
= potential_capabilities.can_generate_field_access_events; |
|
321 |
needed_capabilities.can_get_bytecodes |
|
322 |
= potential_capabilities.can_get_bytecodes; |
|
323 |
needed_capabilities.can_get_synthetic_attribute |
|
324 |
= potential_capabilities.can_get_synthetic_attribute; |
|
325 |
needed_capabilities.can_get_owned_monitor_info |
|
326 |
= potential_capabilities.can_get_owned_monitor_info; |
|
327 |
needed_capabilities.can_get_current_contended_monitor |
|
328 |
= potential_capabilities.can_get_current_contended_monitor; |
|
329 |
needed_capabilities.can_get_monitor_info |
|
330 |
= potential_capabilities.can_get_monitor_info; |
|
331 |
needed_capabilities.can_pop_frame |
|
332 |
= potential_capabilities.can_pop_frame; |
|
333 |
needed_capabilities.can_redefine_classes |
|
334 |
= potential_capabilities.can_redefine_classes; |
|
335 |
needed_capabilities.can_redefine_any_class |
|
336 |
= potential_capabilities.can_redefine_any_class; |
|
337 |
needed_capabilities.can_get_owned_monitor_stack_depth_info |
|
338 |
= potential_capabilities.can_get_owned_monitor_stack_depth_info; |
|
339 |
needed_capabilities.can_get_constant_pool |
|
340 |
= potential_capabilities.can_get_constant_pool; |
|
341 |
{ |
|
342 |
needed_capabilities.can_get_source_debug_extension = 1; |
|
343 |
needed_capabilities.can_get_source_file_name = 1; |
|
344 |
needed_capabilities.can_get_line_numbers = 1; |
|
345 |
needed_capabilities.can_signal_thread |
|
346 |
= potential_capabilities.can_signal_thread; |
|
347 |
} |
|
348 |
||
349 |
/* Add the capabilities */ |
|
350 |
error = JVMTI_FUNC_PTR(gdata->jvmti,AddCapabilities) |
|
351 |
(gdata->jvmti, &needed_capabilities); |
|
352 |
if (error != JVMTI_ERROR_NONE) { |
|
353 |
ERROR_MESSAGE(("JDWP unable to get necessary JVMTI capabilities.")); |
|
354 |
forceExit(1); /* Kill entire process, no core dump wanted */ |
|
355 |
} |
|
356 |
||
357 |
/* Initialize event number mapping tables */ |
|
358 |
eventIndexInit(); |
|
359 |
||
360 |
/* Set the initial JVMTI event notifications */ |
|
361 |
error = set_event_notification(JVMTI_ENABLE, EI_VM_DEATH); |
|
362 |
if (error != JVMTI_ERROR_NONE) { |
|
363 |
return JNI_ERR; |
|
364 |
} |
|
365 |
error = set_event_notification(JVMTI_ENABLE, EI_VM_INIT); |
|
366 |
if (error != JVMTI_ERROR_NONE) { |
|
367 |
return JNI_ERR; |
|
368 |
} |
|
369 |
if (initOnUncaught || (initOnException != NULL)) { |
|
370 |
error = set_event_notification(JVMTI_ENABLE, EI_EXCEPTION); |
|
371 |
if (error != JVMTI_ERROR_NONE) { |
|
372 |
return JNI_ERR; |
|
373 |
} |
|
374 |
} |
|
375 |
||
376 |
/* Set callbacks just for 3 functions */ |
|
377 |
(void)memset(&(gdata->callbacks),0,sizeof(gdata->callbacks)); |
|
378 |
gdata->callbacks.VMInit = &cbEarlyVMInit; |
|
379 |
gdata->callbacks.VMDeath = &cbEarlyVMDeath; |
|
380 |
gdata->callbacks.Exception = &cbEarlyException; |
|
381 |
error = JVMTI_FUNC_PTR(gdata->jvmti,SetEventCallbacks) |
|
382 |
(gdata->jvmti, &(gdata->callbacks), sizeof(gdata->callbacks)); |
|
383 |
if (error != JVMTI_ERROR_NONE) { |
|
384 |
ERROR_MESSAGE(("JDWP unable to set JVMTI event callbacks: %s(%d)", |
|
385 |
jvmtiErrorText(error), error)); |
|
386 |
return JNI_ERR; |
|
387 |
} |
|
388 |
||
389 |
LOG_MISC(("OnLoad: DONE")); |
|
390 |
return JNI_OK; |
|
391 |
} |
|
392 |
||
393 |
JNIEXPORT void JNICALL |
|
394 |
Agent_OnUnload(JavaVM *vm) |
|
395 |
{ |
|
396 |
||
397 |
gdata->isLoaded = JNI_FALSE; |
|
398 |
||
399 |
/* Cleanup, but make sure VM is alive before using JNI, and |
|
400 |
* make sure JVMTI environment is ok before deallocating |
|
401 |
* memory allocated through JVMTI, which all of it is. |
|
402 |
*/ |
|
403 |
||
404 |
/* |
|
405 |
* Close transport before exit |
|
406 |
*/ |
|
407 |
if (transport_is_open()) { |
|
408 |
transport_close(); |
|
409 |
} |
|
410 |
} |
|
411 |
||
412 |
/* |
|
413 |
* Phase 2: Initial events. Phase 2 consists of waiting for the |
|
414 |
* event that triggers full initialization. Under normal circumstances |
|
415 |
* (initOnStartup == TRUE) this is the JVMTI_EVENT_VM_INIT event. |
|
416 |
* Otherwise, we delay initialization until the app throws a |
|
417 |
* particular exception. The triggering event invokes |
|
418 |
* the bulk of the initialization, including creation of threads and |
|
419 |
* monitors, transport setup, and installation of a new event callback which |
|
420 |
* handles the complete set of events. |
|
421 |
* |
|
422 |
* Since the triggering event comes in on an application thread, some of the |
|
423 |
* initialization is difficult to do here. Specifically, this thread along |
|
424 |
* with all other app threads may need to be suspended until a debugger |
|
425 |
* connects. These kinds of tasks are left to the third phase which is |
|
426 |
* invoked by one of the spawned debugger threads, the event handler. |
|
427 |
*/ |
|
428 |
||
429 |
/* |
|
430 |
* Wait for a triggering event; then kick off debugger |
|
431 |
* initialization. A different event callback will be installed by |
|
432 |
* debugger initialization, and this function will not be called |
|
433 |
* again. |
|
434 |
*/ |
|
435 |
||
436 |
/* |
|
437 |
* TO DO: Decide whether we need to protect this code with |
|
438 |
* a lock. It might be too early to create a monitor safely (?). |
|
439 |
*/ |
|
440 |
||
441 |
static void JNICALL |
|
442 |
cbEarlyVMInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread) |
|
443 |
{ |
|
444 |
LOG_CB(("cbEarlyVMInit")); |
|
445 |
if ( gdata->vmDead ) { |
|
446 |
EXIT_ERROR(AGENT_ERROR_INTERNAL,"VM dead at VM_INIT time"); |
|
447 |
} |
|
448 |
if (initOnStartup) |
|
449 |
initialize(env, thread, EI_VM_INIT); |
|
450 |
vmInitialized = JNI_TRUE; |
|
451 |
LOG_MISC(("END cbEarlyVMInit")); |
|
452 |
} |
|
453 |
||
454 |
static void |
|
455 |
disposeEnvironment(jvmtiEnv *jvmti_env) |
|
456 |
{ |
|
457 |
jvmtiError error; |
|
458 |
||
459 |
error = JVMTI_FUNC_PTR(jvmti_env,DisposeEnvironment)(jvmti_env); |
|
460 |
if ( error == JVMTI_ERROR_MUST_POSSESS_CAPABILITY ) |
|
461 |
error = JVMTI_ERROR_NONE; /* Hack! FIXUP when JVMTI has disposeEnv */ |
|
462 |
/* What should error return say? */ |
|
463 |
if (error != JVMTI_ERROR_NONE) { |
|
464 |
ERROR_MESSAGE(("JDWP unable to dispose of JVMTI environment: %s(%d)", |
|
465 |
jvmtiErrorText(error), error)); |
|
466 |
} |
|
467 |
gdata->jvmti = NULL; |
|
468 |
} |
|
469 |
||
470 |
static void JNICALL |
|
471 |
cbEarlyVMDeath(jvmtiEnv *jvmti_env, JNIEnv *env) |
|
472 |
{ |
|
473 |
LOG_CB(("cbEarlyVMDeath")); |
|
474 |
if ( gdata->vmDead ) { |
|
475 |
EXIT_ERROR(AGENT_ERROR_INTERNAL,"VM died more than once"); |
|
476 |
} |
|
477 |
disposeEnvironment(jvmti_env); |
|
478 |
gdata->jvmti = NULL; |
|
479 |
gdata->jvm = NULL; |
|
480 |
gdata->vmDead = JNI_TRUE; |
|
481 |
LOG_MISC(("END cbEarlyVMDeath")); |
|
482 |
} |
|
483 |
||
484 |
static void JNICALL |
|
485 |
cbEarlyException(jvmtiEnv *jvmti_env, JNIEnv *env, |
|
486 |
jthread thread, jmethodID method, jlocation location, |
|
487 |
jobject exception, |
|
488 |
jmethodID catch_method, jlocation catch_location) |
|
489 |
{ |
|
490 |
jvmtiError error; |
|
491 |
jthrowable currentException; |
|
492 |
||
493 |
LOG_CB(("cbEarlyException: thread=%p", thread)); |
|
494 |
||
495 |
if ( gdata->vmDead ) { |
|
496 |
EXIT_ERROR(AGENT_ERROR_INTERNAL,"VM dead at initial Exception event"); |
|
497 |
} |
|
498 |
if (!vmInitialized) { |
|
499 |
LOG_MISC(("VM is not initialized yet")); |
|
500 |
return; |
|
501 |
} |
|
502 |
||
503 |
/* |
|
504 |
* We want to preserve any current exception that might get wiped |
|
505 |
* out during event handling (e.g. JNI calls). We have to rely on |
|
506 |
* space for the local reference on the current frame because |
|
507 |
* doing a PushLocalFrame here might itself generate an exception. |
|
508 |
*/ |
|
509 |
||
510 |
currentException = JNI_FUNC_PTR(env,ExceptionOccurred)(env); |
|
511 |
JNI_FUNC_PTR(env,ExceptionClear)(env); |
|
512 |
||
513 |
if (initOnUncaught && catch_method == NULL) { |
|
514 |
||
515 |
LOG_MISC(("Initializing on uncaught exception")); |
|
516 |
initialize(env, thread, EI_EXCEPTION); |
|
517 |
||
518 |
} else if (initOnException != NULL) { |
|
519 |
||
520 |
jclass clazz; |
|
521 |
||
522 |
/* Get class of exception thrown */ |
|
523 |
clazz = JNI_FUNC_PTR(env,GetObjectClass)(env, exception); |
|
524 |
if ( clazz != NULL ) { |
|
525 |
char *signature = NULL; |
|
526 |
/* initing on throw, check */ |
|
527 |
error = classSignature(clazz, &signature, NULL); |
|
528 |
LOG_MISC(("Checking specific exception: looking for %s, got %s", |
|
529 |
initOnException, signature)); |
|
530 |
if ( (error==JVMTI_ERROR_NONE) && |
|
531 |
(strcmp(signature, initOnException) == 0)) { |
|
532 |
LOG_MISC(("Initializing on specific exception")); |
|
533 |
initialize(env, thread, EI_EXCEPTION); |
|
534 |
} else { |
|
535 |
error = AGENT_ERROR_INTERNAL; /* Just to cause restore */ |
|
536 |
} |
|
537 |
if ( signature != NULL ) { |
|
538 |
jvmtiDeallocate(signature); |
|
539 |
} |
|
540 |
} else { |
|
541 |
error = AGENT_ERROR_INTERNAL; /* Just to cause restore */ |
|
542 |
} |
|
543 |
||
544 |
/* If initialize didn't happen, we need to restore things */ |
|
545 |
if ( error != JVMTI_ERROR_NONE ) { |
|
546 |
/* |
|
547 |
* Restore exception state from before callback call |
|
548 |
*/ |
|
549 |
LOG_MISC(("No initialization, didn't find right exception")); |
|
550 |
if (currentException != NULL) { |
|
551 |
JNI_FUNC_PTR(env,Throw)(env, currentException); |
|
552 |
} else { |
|
553 |
JNI_FUNC_PTR(env,ExceptionClear)(env); |
|
554 |
} |
|
555 |
} |
|
556 |
||
557 |
} |
|
558 |
||
559 |
LOG_MISC(("END cbEarlyException")); |
|
560 |
||
561 |
} |
|
562 |
||
563 |
typedef struct EnumerateArg { |
|
564 |
jboolean isServer; |
|
565 |
jdwpError error; |
|
566 |
jint startCount; |
|
567 |
} EnumerateArg; |
|
568 |
||
569 |
static jboolean |
|
570 |
startTransport(void *item, void *arg) |
|
571 |
{ |
|
572 |
TransportSpec *transport = item; |
|
573 |
EnumerateArg *enumArg = arg; |
|
574 |
jdwpError serror; |
|
575 |
||
576 |
LOG_MISC(("Begin startTransport")); |
|
577 |
serror = transport_startTransport(enumArg->isServer, transport->name, |
|
578 |
transport->address, transport->timeout); |
|
579 |
if (serror != JDWP_ERROR(NONE)) { |
|
580 |
ERROR_MESSAGE(("JDWP Transport %s failed to initialize, %s(%d)", |
|
581 |
transport->name, jdwpErrorText(serror), serror)); |
|
582 |
enumArg->error = serror; |
|
583 |
} else { |
|
584 |
/* (Don't overwrite any previous error) */ |
|
585 |
||
586 |
enumArg->startCount++; |
|
587 |
} |
|
588 |
||
589 |
LOG_MISC(("End startTransport")); |
|
590 |
||
591 |
return JNI_TRUE; /* Always continue, even if there was an error */ |
|
592 |
} |
|
593 |
||
594 |
static void |
|
595 |
signalInitComplete(void) |
|
596 |
{ |
|
597 |
/* |
|
598 |
* Initialization is complete |
|
599 |
*/ |
|
600 |
LOG_MISC(("signal initialization complete")); |
|
601 |
debugMonitorEnter(initMonitor); |
|
602 |
initComplete = JNI_TRUE; |
|
603 |
debugMonitorNotifyAll(initMonitor); |
|
604 |
debugMonitorExit(initMonitor); |
|
605 |
} |
|
606 |
||
607 |
/* |
|
608 |
* Determine if initialization is complete. |
|
609 |
*/ |
|
610 |
jboolean |
|
611 |
debugInit_isInitComplete(void) |
|
612 |
{ |
|
613 |
return initComplete; |
|
614 |
} |
|
615 |
||
616 |
/* |
|
617 |
* Wait for all initialization to complete. |
|
618 |
*/ |
|
619 |
void |
|
620 |
debugInit_waitInitComplete(void) |
|
621 |
{ |
|
622 |
debugMonitorEnter(initMonitor); |
|
623 |
while (!initComplete) { |
|
624 |
debugMonitorWait(initMonitor); |
|
625 |
} |
|
626 |
debugMonitorExit(initMonitor); |
|
627 |
} |
|
628 |
||
629 |
/* All process exit() calls come from here */ |
|
630 |
void |
|
631 |
forceExit(int exit_code) |
|
632 |
{ |
|
633 |
/* make sure the transport is closed down before we exit() */ |
|
634 |
transport_close(); |
|
635 |
exit(exit_code); |
|
636 |
} |
|
637 |
||
638 |
/* All JVM fatal error exits lead here (e.g. we need to kill the VM). */ |
|
639 |
static void |
|
640 |
jniFatalError(JNIEnv *env, const char *msg, jvmtiError error, int exit_code) |
|
641 |
{ |
|
642 |
JavaVM *vm; |
|
643 |
char buf[512]; |
|
644 |
||
645 |
gdata->vmDead = JNI_TRUE; |
|
646 |
if ( msg==NULL ) |
|
647 |
msg = "UNKNOWN REASON"; |
|
648 |
vm = gdata->jvm; |
|
649 |
if ( env==NULL && vm!=NULL ) { |
|
650 |
jint rc = (*((*vm)->GetEnv))(vm, (void **)&env, JNI_VERSION_1_2); |
|
651 |
if (rc != JNI_OK ) { |
|
652 |
env = NULL; |
|
653 |
} |
|
654 |
} |
|
655 |
if ( error != JVMTI_ERROR_NONE ) { |
|
656 |
(void)snprintf(buf, sizeof(buf), "JDWP %s, jvmtiError=%s(%d)", |
|
657 |
msg, jvmtiErrorText(error), error); |
|
658 |
} else { |
|
659 |
(void)snprintf(buf, sizeof(buf), "JDWP %s", buf); |
|
660 |
} |
|
661 |
if (env != NULL) { |
|
662 |
(*((*env)->FatalError))(env, buf); |
|
663 |
} else { |
|
664 |
/* Should rarely ever reach here, means VM is really dead */ |
|
665 |
print_message(stderr, "ERROR: JDWP: ", "\n", |
|
666 |
"Can't call JNI FatalError(NULL, \"%s\")", buf); |
|
667 |
} |
|
668 |
forceExit(exit_code); |
|
669 |
} |
|
670 |
||
671 |
/* |
|
672 |
* Initialize debugger back end modules |
|
673 |
*/ |
|
674 |
static void |
|
675 |
initialize(JNIEnv *env, jthread thread, EventIndex triggering_ei) |
|
676 |
{ |
|
677 |
jvmtiError error; |
|
678 |
EnumerateArg arg; |
|
679 |
jbyte suspendPolicy; |
|
680 |
||
681 |
LOG_MISC(("Begin initialize()")); |
|
682 |
currentSessionID = 0; |
|
683 |
initComplete = JNI_FALSE; |
|
684 |
||
685 |
if ( gdata->vmDead ) { |
|
686 |
EXIT_ERROR(AGENT_ERROR_INTERNAL,"VM dead at initialize() time"); |
|
687 |
} |
|
688 |
||
689 |
/* Turn off the initial JVMTI event notifications */ |
|
690 |
error = set_event_notification(JVMTI_DISABLE, EI_EXCEPTION); |
|
691 |
if (error != JVMTI_ERROR_NONE) { |
|
692 |
EXIT_ERROR(error, "unable to disable JVMTI event notification"); |
|
693 |
} |
|
694 |
error = set_event_notification(JVMTI_DISABLE, EI_VM_INIT); |
|
695 |
if (error != JVMTI_ERROR_NONE) { |
|
696 |
EXIT_ERROR(error, "unable to disable JVMTI event notification"); |
|
697 |
} |
|
698 |
error = set_event_notification(JVMTI_DISABLE, EI_VM_DEATH); |
|
699 |
if (error != JVMTI_ERROR_NONE) { |
|
700 |
EXIT_ERROR(error, "unable to disable JVMTI event notification"); |
|
701 |
} |
|
702 |
||
703 |
/* Remove initial event callbacks */ |
|
704 |
(void)memset(&(gdata->callbacks),0,sizeof(gdata->callbacks)); |
|
705 |
error = JVMTI_FUNC_PTR(gdata->jvmti,SetEventCallbacks) |
|
706 |
(gdata->jvmti, &(gdata->callbacks), sizeof(gdata->callbacks)); |
|
707 |
if (error != JVMTI_ERROR_NONE) { |
|
708 |
EXIT_ERROR(error, "unable to clear JVMTI callbacks"); |
|
709 |
} |
|
710 |
||
711 |
commonRef_initialize(); |
|
712 |
util_initialize(env); |
|
713 |
threadControl_initialize(); |
|
714 |
stepControl_initialize(); |
|
715 |
invoker_initialize(); |
|
716 |
debugDispatch_initialize(); |
|
717 |
classTrack_initialize(env); |
|
718 |
debugLoop_initialize(); |
|
719 |
||
720 |
initMonitor = debugMonitorCreate("JDWP Initialization Monitor"); |
|
721 |
||
722 |
||
723 |
/* |
|
724 |
* Initialize transports |
|
725 |
*/ |
|
726 |
arg.isServer = isServer; |
|
727 |
arg.error = JDWP_ERROR(NONE); |
|
728 |
arg.startCount = 0; |
|
729 |
||
730 |
transport_initialize(); |
|
731 |
(void)bagEnumerateOver(transports, startTransport, &arg); |
|
732 |
||
733 |
/* |
|
734 |
* Exit with an error only if |
|
735 |
* 1) none of the transports was successfully started, and |
|
736 |
* 2) the application has not yet started running |
|
737 |
*/ |
|
738 |
if ((arg.error != JDWP_ERROR(NONE)) && |
|
739 |
(arg.startCount == 0) && |
|
740 |
initOnStartup) { |
|
741 |
EXIT_ERROR(map2jvmtiError(arg.error), "No transports initialized"); |
|
742 |
} |
|
743 |
||
744 |
eventHandler_initialize(currentSessionID); |
|
745 |
||
746 |
signalInitComplete(); |
|
747 |
||
748 |
transport_waitForConnection(); |
|
749 |
||
750 |
suspendPolicy = suspendOnInit ? JDWP_SUSPEND_POLICY(ALL) |
|
751 |
: JDWP_SUSPEND_POLICY(NONE); |
|
752 |
if (triggering_ei == EI_VM_INIT) { |
|
753 |
LOG_MISC(("triggering_ei == EI_VM_INIT")); |
|
754 |
eventHelper_reportVMInit(env, currentSessionID, thread, suspendPolicy); |
|
755 |
} else { |
|
756 |
/* |
|
757 |
* TO DO: Kludgy way of getting the triggering event to the |
|
758 |
* just-attached debugger. It would be nice to make this a little |
|
759 |
* cleaner. There is also a race condition where other events |
|
760 |
* can get in the queue (from other not-yet-suspended threads) |
|
761 |
* before this one does. (Also need to handle allocation error below?) |
|
762 |
*/ |
|
763 |
EventInfo info; |
|
764 |
struct bag *initEventBag; |
|
765 |
LOG_MISC(("triggering_ei != EI_VM_INIT")); |
|
766 |
initEventBag = eventHelper_createEventBag(); |
|
767 |
(void)memset(&info,0,sizeof(info)); |
|
768 |
info.ei = triggering_ei; |
|
769 |
eventHelper_recordEvent(&info, 0, suspendPolicy, initEventBag); |
|
770 |
(void)eventHelper_reportEvents(currentSessionID, initEventBag); |
|
771 |
bagDestroyBag(initEventBag); |
|
772 |
} |
|
773 |
||
774 |
if ( gdata->vmDead ) { |
|
775 |
EXIT_ERROR(AGENT_ERROR_INTERNAL,"VM dead before initialize() completes"); |
|
776 |
} |
|
777 |
LOG_MISC(("End initialize()")); |
|
778 |
} |
|
779 |
||
780 |
/* |
|
781 |
* Restore all static data to the initialized state so that another |
|
782 |
* debugger can connect properly later. |
|
783 |
*/ |
|
784 |
void |
|
785 |
debugInit_reset(JNIEnv *env) |
|
786 |
{ |
|
787 |
EnumerateArg arg; |
|
788 |
||
789 |
LOG_MISC(("debugInit_reset() beginning")); |
|
790 |
||
791 |
currentSessionID++; |
|
792 |
initComplete = JNI_FALSE; |
|
793 |
||
794 |
eventHandler_reset(currentSessionID); |
|
795 |
transport_reset(); |
|
796 |
debugDispatch_reset(); |
|
797 |
invoker_reset(); |
|
798 |
stepControl_reset(); |
|
799 |
threadControl_reset(); |
|
800 |
util_reset(); |
|
801 |
commonRef_reset(env); |
|
802 |
classTrack_reset(); |
|
803 |
||
804 |
/* |
|
805 |
* If this is a server, we are now ready to accept another connection. |
|
806 |
* If it's a client, then we've cleaned up some (more should be added |
|
807 |
* later) and we're done. |
|
808 |
*/ |
|
809 |
if (isServer) { |
|
810 |
arg.isServer = JNI_TRUE; |
|
811 |
arg.error = JDWP_ERROR(NONE); |
|
812 |
arg.startCount = 0; |
|
813 |
(void)bagEnumerateOver(transports, startTransport, &arg); |
|
814 |
||
815 |
signalInitComplete(); |
|
816 |
||
817 |
transport_waitForConnection(); |
|
818 |
} else { |
|
819 |
signalInitComplete(); /* Why? */ |
|
820 |
} |
|
821 |
||
822 |
LOG_MISC(("debugInit_reset() completed.")); |
|
823 |
} |
|
824 |
||
825 |
||
826 |
char * |
|
827 |
debugInit_launchOnInit(void) |
|
828 |
{ |
|
829 |
return launchOnInit; |
|
830 |
} |
|
831 |
||
832 |
jboolean |
|
833 |
debugInit_suspendOnInit(void) |
|
834 |
{ |
|
835 |
return suspendOnInit; |
|
836 |
} |
|
837 |
||
838 |
/* |
|
839 |
* code below is shamelessly swiped from hprof. |
|
840 |
*/ |
|
841 |
||
842 |
static int |
|
843 |
get_tok(char **src, char *buf, int buflen, char sep) |
|
844 |
{ |
|
845 |
int i; |
|
846 |
char *p = *src; |
|
847 |
for (i = 0; i < buflen; i++) { |
|
848 |
if (p[i] == 0 || p[i] == sep) { |
|
849 |
buf[i] = 0; |
|
850 |
if (p[i] == sep) { |
|
851 |
i++; |
|
852 |
} |
|
853 |
*src += i; |
|
854 |
return i; |
|
855 |
} |
|
856 |
buf[i] = p[i]; |
|
857 |
} |
|
858 |
/* overflow */ |
|
859 |
return 0; |
|
860 |
} |
|
861 |
||
862 |
static void |
|
863 |
printUsage(void) |
|
864 |
{ |
|
865 |
TTY_MESSAGE(( |
|
866 |
" Java Debugger JDWP Agent Library\n" |
|
867 |
" --------------------------------\n" |
|
868 |
"\n" |
|
869 |
" (see http://java.sun.com/products/jpda for more information)\n" |
|
870 |
"\n" |
|
871 |
"jdwp usage: java " AGENTLIB "=[help]|[<option>=<value>, ...]\n" |
|
872 |
"\n" |
|
873 |
"Option Name and Value Description Default\n" |
|
874 |
"--------------------- ----------- -------\n" |
|
875 |
"suspend=y|n wait on startup? y\n" |
|
876 |
"transport=<name> transport spec none\n" |
|
877 |
"address=<listen/attach address> transport spec \"\"\n" |
|
878 |
"server=y|n listen for debugger? n\n" |
|
879 |
"launch=<command line> run debugger on event none\n" |
|
880 |
"onthrow=<exception name> debug on throw none\n" |
|
881 |
"onuncaught=y|n debug on any uncaught? n\n" |
|
882 |
"timeout=<timeout value> for listen/attach in milliseconds n\n" |
|
883 |
"mutf8=y|n output modified utf-8 n\n" |
|
884 |
"quiet=y|n control over terminal messages n\n" |
|
885 |
"\n" |
|
886 |
"Obsolete Options\n" |
|
887 |
"----------------\n" |
|
888 |
"strict=y|n\n" |
|
889 |
"stdalloc=y|n\n" |
|
890 |
"\n" |
|
891 |
"Examples\n" |
|
892 |
"--------\n" |
|
893 |
" - Using sockets connect to a debugger at a specific address:\n" |
|
894 |
" java " AGENTLIB "=transport=dt_socket,address=localhost:8000 ...\n" |
|
895 |
" - Using sockets listen for a debugger to attach:\n" |
|
896 |
" java " AGENTLIB "=transport=dt_socket,server=y,suspend=y ...\n" |
|
897 |
"\n" |
|
898 |
"Notes\n" |
|
899 |
"-----\n" |
|
900 |
" - A timeout value of 0 (the default) is no timeout.\n" |
|
901 |
"\n" |
|
902 |
"Warnings\n" |
|
903 |
"--------\n" |
|
904 |
" - The older " XRUN " interface can still be used, but will be removed in\n" |
|
905 |
" a future release, for example:\n" |
|
906 |
" java " XDEBUG " " XRUN ":[help]|[<option>=<value>, ...]\n" |
|
907 |
)); |
|
908 |
||
909 |
#ifdef DEBUG |
|
910 |
||
911 |
TTY_MESSAGE(( |
|
912 |
"\n" |
|
913 |
"Debugging Options Description Default\n" |
|
914 |
"----------------- ----------- -------\n" |
|
915 |
"pause=y|n pause to debug PID n\n" |
|
916 |
"coredump=y|n coredump at exit n\n" |
|
917 |
"errorexit=y|n exit on any error n\n" |
|
918 |
"logfile=filename name of log file none\n" |
|
919 |
"logflags=flags log flags (bitmask) none\n" |
|
920 |
" JVM calls = 0x001\n" |
|
921 |
" JNI calls = 0x002\n" |
|
922 |
" JVMTI calls = 0x004\n" |
|
923 |
" misc events = 0x008\n" |
|
924 |
" step logs = 0x010\n" |
|
925 |
" locations = 0x020\n" |
|
926 |
" callbacks = 0x040\n" |
|
927 |
" errors = 0x080\n" |
|
928 |
" everything = 0xfff\n" |
|
929 |
"debugflags=flags debug flags (bitmask) none\n" |
|
930 |
" USE_ITERATE_THROUGH_HEAP 0x01\n" |
|
931 |
"\n" |
|
932 |
"Environment Variables\n" |
|
933 |
"---------------------\n" |
|
934 |
"_JAVA_JDWP_OPTIONS\n" |
|
935 |
" Options can be added externally via this environment variable.\n" |
|
936 |
" Anything contained in it will get a comma prepended to it (if needed),\n" |
|
937 |
" then it will be added to the end of the options supplied via the\n" |
|
938 |
" " XRUN " or " AGENTLIB " command line option.\n" |
|
939 |
)); |
|
940 |
||
941 |
#endif |
|
942 |
||
943 |
||
944 |
||
945 |
} |
|
946 |
||
947 |
static jboolean checkAddress(void *bagItem, void *arg) |
|
948 |
{ |
|
949 |
TransportSpec *spec = (TransportSpec *)bagItem; |
|
950 |
if (spec->address == NULL) { |
|
951 |
ERROR_MESSAGE(("JDWP Non-server transport %s must have a connection " |
|
952 |
"address specified through the 'address=' option", |
|
953 |
spec->name)); |
|
954 |
return JNI_FALSE; |
|
955 |
} else { |
|
956 |
return JNI_TRUE; |
|
957 |
} |
|
958 |
} |
|
959 |
||
960 |
static char * |
|
961 |
add_to_options(char *options, char *new_options) |
|
962 |
{ |
|
963 |
size_t originalLength; |
|
964 |
char *combinedOptions; |
|
965 |
||
966 |
/* |
|
967 |
* Allocate enough space for both strings and |
|
968 |
* comma in between. |
|
969 |
*/ |
|
970 |
originalLength = strlen(options); |
|
971 |
combinedOptions = jvmtiAllocate((jint)originalLength + 1 + |
|
972 |
(jint)strlen(new_options) + 1); |
|
973 |
if (combinedOptions == NULL) { |
|
974 |
return NULL; |
|
975 |
} |
|
976 |
||
977 |
(void)strcpy(combinedOptions, options); |
|
978 |
(void)strcat(combinedOptions, ","); |
|
979 |
(void)strcat(combinedOptions, new_options); |
|
980 |
||
981 |
return combinedOptions; |
|
982 |
} |
|
983 |
||
984 |
static jboolean |
|
985 |
get_boolean(char **pstr, jboolean *answer) |
|
986 |
{ |
|
987 |
char buf[80]; |
|
988 |
*answer = JNI_FALSE; |
|
989 |
/*LINTED*/ |
|
990 |
if (get_tok(pstr, buf, (int)sizeof(buf), ',')) { |
|
991 |
if (strcmp(buf, "y") == 0) { |
|
992 |
*answer = JNI_TRUE; |
|
993 |
return JNI_TRUE; |
|
994 |
} else if (strcmp(buf, "n") == 0) { |
|
995 |
*answer = JNI_FALSE; |
|
996 |
return JNI_TRUE; |
|
997 |
} |
|
998 |
} |
|
999 |
return JNI_FALSE; |
|
1000 |
} |
|
1001 |
||
1002 |
/* atexit() callback */ |
|
1003 |
static void |
|
1004 |
atexit_finish_logging(void) |
|
1005 |
{ |
|
1006 |
/* Normal exit(0) (not _exit()) may only reach here */ |
|
1007 |
finish_logging(0); /* Only first call matters */ |
|
1008 |
} |
|
1009 |
||
1010 |
static jboolean |
|
1011 |
parseOptions(char *options) |
|
1012 |
{ |
|
1013 |
TransportSpec *currentTransport = NULL; |
|
1014 |
char *end; |
|
1015 |
char *current; |
|
1016 |
int length; |
|
1017 |
char *str; |
|
1018 |
char *errmsg; |
|
1019 |
||
1020 |
/* Set defaults */ |
|
1021 |
gdata->assertOn = DEFAULT_ASSERT_ON; |
|
1022 |
gdata->assertFatal = DEFAULT_ASSERT_FATAL; |
|
1023 |
logfile = DEFAULT_LOGFILE; |
|
1024 |
||
1025 |
/* Options being NULL will end up being an error. */ |
|
1026 |
if (options == NULL) { |
|
1027 |
options = ""; |
|
1028 |
} |
|
1029 |
||
1030 |
/* Check for "help" BEFORE we add any environmental settings */ |
|
1031 |
if ((strcmp(options, "help")) == 0) { |
|
1032 |
printUsage(); |
|
1033 |
forceExit(0); /* Kill entire process, no core dump wanted */ |
|
1034 |
} |
|
1035 |
||
1036 |
/* These buffers are never freed */ |
|
1037 |
{ |
|
1038 |
char *envOptions; |
|
1039 |
||
1040 |
/* |
|
1041 |
* Add environmentally specified options. |
|
1042 |
*/ |
|
1043 |
envOptions = getenv("_JAVA_JDWP_OPTIONS"); |
|
1044 |
if (envOptions != NULL) { |
|
1045 |
options = add_to_options(options, envOptions); |
|
1046 |
if ( options==NULL ) { |
|
1047 |
EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"options"); |
|
1048 |
} |
|
1049 |
} |
|
1050 |
||
1051 |
/* |
|
1052 |
* Allocate a buffer for names derived from option strings. It should |
|
1053 |
* never be longer than the original options string itself. |
|
1054 |
* Also keep a copy of the options in gdata->options. |
|
1055 |
*/ |
|
1056 |
length = (int)strlen(options); |
|
1057 |
gdata->options = jvmtiAllocate(length + 1); |
|
1058 |
if (gdata->options == NULL) { |
|
1059 |
EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"options"); |
|
1060 |
} |
|
1061 |
(void)strcpy(gdata->options, options); |
|
1062 |
names = jvmtiAllocate(length + 1); |
|
1063 |
if (names == NULL) { |
|
1064 |
EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"options"); |
|
1065 |
} |
|
1066 |
||
1067 |
transports = bagCreateBag(sizeof(TransportSpec), 3); |
|
1068 |
if (transports == NULL) { |
|
1069 |
EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"transports"); |
|
1070 |
} |
|
1071 |
||
1072 |
} |
|
1073 |
||
1074 |
current = names; |
|
1075 |
end = names + length; |
|
1076 |
str = options; |
|
1077 |
||
1078 |
while (*str) { |
|
1079 |
char buf[100]; |
|
1080 |
/*LINTED*/ |
|
1081 |
if (!get_tok(&str, buf, (int)sizeof(buf), '=')) { |
|
1082 |
goto syntax_error; |
|
1083 |
} |
|
1084 |
if (strcmp(buf, "transport") == 0) { |
|
1085 |
currentTransport = bagAdd(transports); |
|
1086 |
/*LINTED*/ |
|
1087 |
if (!get_tok(&str, current, (int)(end - current), ',')) { |
|
1088 |
goto syntax_error; |
|
1089 |
} |
|
1090 |
currentTransport->name = current; |
|
1091 |
current += strlen(current) + 1; |
|
1092 |
} else if (strcmp(buf, "address") == 0) { |
|
1093 |
if (currentTransport == NULL) { |
|
1094 |
errmsg = "address specified without transport"; |
|
1095 |
goto bad_option_with_errmsg; |
|
1096 |
} |
|
1097 |
/*LINTED*/ |
|
1098 |
if (!get_tok(&str, current, (int)(end - current), ',')) { |
|
1099 |
goto syntax_error; |
|
1100 |
} |
|
1101 |
currentTransport->address = current; |
|
1102 |
current += strlen(current) + 1; |
|
1103 |
} else if (strcmp(buf, "timeout") == 0) { |
|
1104 |
if (currentTransport == NULL) { |
|
1105 |
errmsg = "timeout specified without transport"; |
|
1106 |
goto bad_option_with_errmsg; |
|
1107 |
} |
|
1108 |
/*LINTED*/ |
|
1109 |
if (!get_tok(&str, current, (int)(end - current), ',')) { |
|
1110 |
goto syntax_error; |
|
1111 |
} |
|
1112 |
currentTransport->timeout = atol(current); |
|
1113 |
current += strlen(current) + 1; |
|
1114 |
} else if (strcmp(buf, "launch") == 0) { |
|
1115 |
/*LINTED*/ |
|
1116 |
if (!get_tok(&str, current, (int)(end - current), ',')) { |
|
1117 |
goto syntax_error; |
|
1118 |
} |
|
1119 |
launchOnInit = current; |
|
1120 |
current += strlen(current) + 1; |
|
1121 |
} else if (strcmp(buf, "onthrow") == 0) { |
|
1122 |
/* Read class name and convert in place to a signature */ |
|
1123 |
*current = 'L'; |
|
1124 |
/*LINTED*/ |
|
1125 |
if (!get_tok(&str, current + 1, (int)(end - current - 1), ',')) { |
|
1126 |
goto syntax_error; |
|
1127 |
} |
|
1128 |
initOnException = current; |
|
1129 |
while (*current != '\0') { |
|
1130 |
if (*current == '.') { |
|
1131 |
*current = '/'; |
|
1132 |
} |
|
1133 |
current++; |
|
1134 |
} |
|
1135 |
*current++ = ';'; |
|
1136 |
*current++ = '\0'; |
|
1137 |
} else if (strcmp(buf, "assert") == 0) { |
|
1138 |
/*LINTED*/ |
|
1139 |
if (!get_tok(&str, current, (int)(end - current), ',')) { |
|
1140 |
goto syntax_error; |
|
1141 |
} |
|
1142 |
if (strcmp(current, "y") == 0) { |
|
1143 |
gdata->assertOn = JNI_TRUE; |
|
1144 |
gdata->assertFatal = JNI_FALSE; |
|
1145 |
} else if (strcmp(current, "fatal") == 0) { |
|
1146 |
gdata->assertOn = JNI_TRUE; |
|
1147 |
gdata->assertFatal = JNI_TRUE; |
|
1148 |
} else if (strcmp(current, "n") == 0) { |
|
1149 |
gdata->assertOn = JNI_FALSE; |
|
1150 |
gdata->assertFatal = JNI_FALSE; |
|
1151 |
} else { |
|
1152 |
goto syntax_error; |
|
1153 |
} |
|
1154 |
current += strlen(current) + 1; |
|
1155 |
} else if (strcmp(buf, "pause") == 0) { |
|
1156 |
if ( !get_boolean(&str, &dopause) ) { |
|
1157 |
goto syntax_error; |
|
1158 |
} |
|
1159 |
if ( dopause ) { |
|
1160 |
do_pause(); |
|
1161 |
} |
|
1162 |
} else if (strcmp(buf, "coredump") == 0) { |
|
1163 |
if ( !get_boolean(&str, &docoredump) ) { |
|
1164 |
goto syntax_error; |
|
1165 |
} |
|
1166 |
} else if (strcmp(buf, "errorexit") == 0) { |
|
1167 |
if ( !get_boolean(&str, &(gdata->doerrorexit)) ) { |
|
1168 |
goto syntax_error; |
|
1169 |
} |
|
1170 |
} else if (strcmp(buf, "exitpause") == 0) { |
|
1171 |
errmsg = "The exitpause option removed, use -XX:OnError"; |
|
1172 |
goto bad_option_with_errmsg; |
|
1173 |
} else if (strcmp(buf, "precrash") == 0) { |
|
1174 |
errmsg = "The precrash option removed, use -XX:OnError"; |
|
1175 |
goto bad_option_with_errmsg; |
|
1176 |
} else if (strcmp(buf, "logfile") == 0) { |
|
1177 |
/*LINTED*/ |
|
1178 |
if (!get_tok(&str, current, (int)(end - current), ',')) { |
|
1179 |
goto syntax_error; |
|
1180 |
} |
|
1181 |
logfile = current; |
|
1182 |
current += strlen(current) + 1; |
|
1183 |
} else if (strcmp(buf, "logflags") == 0) { |
|
1184 |
/*LINTED*/ |
|
1185 |
if (!get_tok(&str, current, (int)(end - current), ',')) { |
|
1186 |
goto syntax_error; |
|
1187 |
} |
|
1188 |
/*LINTED*/ |
|
1189 |
logflags = (unsigned)strtol(current, NULL, 0); |
|
1190 |
} else if (strcmp(buf, "debugflags") == 0) { |
|
1191 |
/*LINTED*/ |
|
1192 |
if (!get_tok(&str, current, (int)(end - current), ',')) { |
|
1193 |
goto syntax_error; |
|
1194 |
} |
|
1195 |
/*LINTED*/ |
|
1196 |
gdata->debugflags = (unsigned)strtol(current, NULL, 0); |
|
1197 |
} else if ( strcmp(buf, "suspend")==0 ) { |
|
1198 |
if ( !get_boolean(&str, &suspendOnInit) ) { |
|
1199 |
goto syntax_error; |
|
1200 |
} |
|
1201 |
} else if ( strcmp(buf, "server")==0 ) { |
|
1202 |
if ( !get_boolean(&str, &isServer) ) { |
|
1203 |
goto syntax_error; |
|
1204 |
} |
|
1205 |
} else if ( strcmp(buf, "strict")==0 ) { /* Obsolete, but accept it */ |
|
1206 |
if ( !get_boolean(&str, &isStrict) ) { |
|
1207 |
goto syntax_error; |
|
1208 |
} |
|
1209 |
} else if ( strcmp(buf, "quiet")==0 ) { |
|
1210 |
if ( !get_boolean(&str, &(gdata->quiet)) ) { |
|
1211 |
goto syntax_error; |
|
1212 |
} |
|
1213 |
} else if ( strcmp(buf, "onuncaught")==0 ) { |
|
1214 |
if ( !get_boolean(&str, &initOnUncaught) ) { |
|
1215 |
goto syntax_error; |
|
1216 |
} |
|
1217 |
} else if ( strcmp(buf, "mutf8")==0 ) { |
|
1218 |
if ( !get_boolean(&str, &(gdata->modifiedUtf8)) ) { |
|
1219 |
goto syntax_error; |
|
1220 |
} |
|
1221 |
} else if ( strcmp(buf, "stdalloc")==0 ) { /* Obsolete, but accept it */ |
|
1222 |
if ( !get_boolean(&str, &useStandardAlloc) ) { |
|
1223 |
goto syntax_error; |
|
1224 |
} |
|
1225 |
} else { |
|
1226 |
goto syntax_error; |
|
1227 |
} |
|
1228 |
} |
|
1229 |
||
1230 |
/* Setup logging now */ |
|
1231 |
if ( logfile!=NULL ) { |
|
1232 |
setup_logging(logfile, logflags); |
|
1233 |
(void)atexit(&atexit_finish_logging); |
|
1234 |
} |
|
1235 |
||
1236 |
if (bagSize(transports) == 0) { |
|
1237 |
errmsg = "no transport specified"; |
|
1238 |
goto bad_option_with_errmsg; |
|
1239 |
} |
|
1240 |
||
1241 |
/* |
|
1242 |
* TO DO: Remove when multiple transports are allowed. (replace with |
|
1243 |
* check below. |
|
1244 |
*/ |
|
1245 |
if (bagSize(transports) > 1) { |
|
1246 |
errmsg = "multiple transports are not supported in this release"; |
|
1247 |
goto bad_option_with_errmsg; |
|
1248 |
} |
|
1249 |
||
1250 |
||
1251 |
if (!isServer) { |
|
1252 |
jboolean specified = bagEnumerateOver(transports, checkAddress, NULL); |
|
1253 |
if (!specified) { |
|
1254 |
/* message already printed */ |
|
1255 |
goto bad_option_no_msg; |
|
1256 |
} |
|
1257 |
} |
|
1258 |
||
1259 |
/* |
|
1260 |
* The user has selected to wait for an exception before init happens |
|
1261 |
*/ |
|
1262 |
if ((initOnException != NULL) || (initOnUncaught)) { |
|
1263 |
initOnStartup = JNI_FALSE; |
|
1264 |
||
1265 |
if (launchOnInit == NULL) { |
|
1266 |
/* |
|
1267 |
* These rely on the launch=/usr/bin/foo |
|
1268 |
* suboption, so it is an error if user did not |
|
1269 |
* provide one. |
|
1270 |
*/ |
|
1271 |
errmsg = "Specify launch=<command line> when using onthrow or onuncaught suboption"; |
|
1272 |
goto bad_option_with_errmsg; |
|
1273 |
} |
|
1274 |
} |
|
1275 |
||
1276 |
return JNI_TRUE; |
|
1277 |
||
1278 |
syntax_error: |
|
1279 |
ERROR_MESSAGE(("JDWP option syntax error: %s=%s", AGENTLIB, options)); |
|
1280 |
return JNI_FALSE; |
|
1281 |
||
1282 |
bad_option_with_errmsg: |
|
1283 |
ERROR_MESSAGE(("JDWP %s: %s=%s", errmsg, AGENTLIB, options)); |
|
1284 |
return JNI_FALSE; |
|
1285 |
||
1286 |
bad_option_no_msg: |
|
1287 |
ERROR_MESSAGE(("JDWP %s: %s=%s", "invalid option", AGENTLIB, options)); |
|
1288 |
return JNI_FALSE; |
|
1289 |
} |
|
1290 |
||
1291 |
/* All normal exit doors lead here */ |
|
1292 |
void |
|
1293 |
debugInit_exit(jvmtiError error, const char *msg) |
|
1294 |
{ |
|
1295 |
int exit_code = 0; |
|
1296 |
||
1297 |
/* Pick an error code */ |
|
1298 |
if ( error != JVMTI_ERROR_NONE ) { |
|
1299 |
exit_code = 1; |
|
1300 |
if ( docoredump ) { |
|
1301 |
finish_logging(exit_code); |
|
1302 |
abort(); |
|
1303 |
} |
|
1304 |
} |
|
1305 |
if ( msg==NULL ) { |
|
1306 |
msg = ""; |
|
1307 |
} |
|
1308 |
||
1309 |
LOG_MISC(("Exiting with error %s(%d): %s", jvmtiErrorText(error), error, msg)); |
|
1310 |
||
1311 |
gdata->vmDead = JNI_TRUE; |
|
1312 |
||
1313 |
/* Let's try and cleanup the JVMTI, if we even have one */ |
|
1314 |
if ( gdata->jvmti != NULL ) { |
|
1315 |
/* Dispose of jvmti (gdata->jvmti becomes NULL) */ |
|
1316 |
disposeEnvironment(gdata->jvmti); |
|
1317 |
} |
|
1318 |
||
1319 |
/* Finish up logging. We reach here if JDWP is doing the exiting. */ |
|
1320 |
finish_logging(exit_code); /* Only first call matters */ |
|
1321 |
||
1322 |
/* Let's give the JNI a FatalError if non-exit 0, which is historic way */ |
|
1323 |
if ( exit_code != 0 ) { |
|
1324 |
JNIEnv *env = NULL; |
|
1325 |
jniFatalError(env, msg, error, exit_code); |
|
1326 |
} |
|
1327 |
||
1328 |
/* Last chance to die, this kills the entire process. */ |
|
1329 |
forceExit(exit_code); |
|
1330 |
} |