author | ihse |
Sat, 03 Mar 2018 08:21:47 +0100 | |
branch | ihse-warnings-cflags-branch |
changeset 56230 | 489867818774 |
parent 47525 | e05aff6beada |
permissions | -rw-r--r-- |
2 | 1 |
/* |
46841
76a15bd1de0a
8134103: JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 1998, 2017, 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 "util.h" |
|
27 |
#include "outStream.h" |
|
28 |
#include "eventHandler.h" |
|
29 |
#include "threadControl.h" |
|
30 |
#include "invoker.h" |
|
31 |
||
47525
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
32 |
|
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
33 |
#define COMMAND_LOOP_THREAD_NAME "JDWP Event Helper Thread" |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
34 |
|
2 | 35 |
/* |
36 |
* Event helper thread command commandKinds |
|
37 |
*/ |
|
38 |
#define COMMAND_REPORT_EVENT_COMPOSITE 1 |
|
39 |
#define COMMAND_REPORT_INVOKE_DONE 2 |
|
40 |
#define COMMAND_REPORT_VM_INIT 3 |
|
41 |
#define COMMAND_SUSPEND_THREAD 4 |
|
42 |
||
43 |
/* |
|
44 |
* Event helper thread command singleKinds |
|
45 |
*/ |
|
46 |
#define COMMAND_SINGLE_EVENT 11 |
|
47 |
#define COMMAND_SINGLE_UNLOAD 12 |
|
48 |
#define COMMAND_SINGLE_FRAME_EVENT 13 |
|
49 |
||
50 |
typedef struct EventCommandSingle { |
|
51 |
jbyte suspendPolicy; /* NOTE: Must be the first field */ |
|
52 |
jint id; |
|
53 |
EventInfo info; |
|
54 |
} EventCommandSingle; |
|
55 |
||
56 |
typedef struct UnloadCommandSingle { |
|
57 |
char *classSignature; |
|
58 |
jint id; |
|
59 |
} UnloadCommandSingle; |
|
60 |
||
61 |
typedef struct FrameEventCommandSingle { |
|
62 |
jbyte suspendPolicy; /* NOTE: Must be the first field */ |
|
63 |
jint id; |
|
64 |
EventIndex ei; |
|
65 |
jthread thread; |
|
66 |
jclass clazz; |
|
67 |
jmethodID method; |
|
68 |
jlocation location; |
|
69 |
char typeKey; /* Not used for method entry events */ |
|
70 |
/* If typeKey is 0, then no return value is needed */ |
|
71 |
jvalue returnValue; /* Not used for method entry events */ |
|
72 |
} FrameEventCommandSingle; |
|
73 |
||
74 |
typedef struct CommandSingle { |
|
75 |
jint singleKind; |
|
76 |
union { |
|
77 |
EventCommandSingle eventCommand; |
|
78 |
UnloadCommandSingle unloadCommand; |
|
79 |
FrameEventCommandSingle frameEventCommand; |
|
80 |
} u; |
|
81 |
} CommandSingle; |
|
82 |
||
83 |
typedef struct ReportInvokeDoneCommand { |
|
84 |
jthread thread; |
|
85 |
} ReportInvokeDoneCommand; |
|
86 |
||
87 |
typedef struct ReportVMInitCommand { |
|
88 |
jbyte suspendPolicy; /* NOTE: Must be the first field */ |
|
89 |
jthread thread; |
|
90 |
} ReportVMInitCommand; |
|
91 |
||
92 |
typedef struct SuspendThreadCommand { |
|
93 |
jthread thread; |
|
94 |
} SuspendThreadCommand; |
|
95 |
||
96 |
typedef struct ReportEventCompositeCommand { |
|
97 |
jbyte suspendPolicy; /* NOTE: Must be the first field */ |
|
98 |
jint eventCount; |
|
99 |
CommandSingle singleCommand[1]; /* variable length */ |
|
100 |
} ReportEventCompositeCommand; |
|
101 |
||
102 |
typedef struct HelperCommand { |
|
103 |
jint commandKind; |
|
104 |
jboolean done; |
|
105 |
jboolean waiting; |
|
106 |
jbyte sessionID; |
|
107 |
struct HelperCommand *next; |
|
108 |
union { |
|
109 |
/* NOTE: Each of the structs below must have the same first field */ |
|
110 |
ReportEventCompositeCommand reportEventComposite; |
|
111 |
ReportInvokeDoneCommand reportInvokeDone; |
|
112 |
ReportVMInitCommand reportVMInit; |
|
113 |
SuspendThreadCommand suspendThread; |
|
114 |
} u; |
|
115 |
/* composite array expand out, put nothing after */ |
|
116 |
} HelperCommand; |
|
117 |
||
118 |
typedef struct { |
|
119 |
HelperCommand *head; |
|
120 |
HelperCommand *tail; |
|
121 |
} CommandQueue; |
|
122 |
||
123 |
static CommandQueue commandQueue; |
|
124 |
static jrawMonitorID commandQueueLock; |
|
125 |
static jrawMonitorID commandCompleteLock; |
|
126 |
static jrawMonitorID blockCommandLoopLock; |
|
47525
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
127 |
static jrawMonitorID vmDeathLock; |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
128 |
static volatile jboolean commandLoopEnteredVmDeathLock = JNI_FALSE; |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
129 |
|
2 | 130 |
static jint maxQueueSize = 50 * 1024; /* TO DO: Make this configurable */ |
131 |
static jboolean holdEvents; |
|
132 |
static jint currentQueueSize = 0; |
|
133 |
static jint currentSessionID; |
|
134 |
||
135 |
static void saveEventInfoRefs(JNIEnv *env, EventInfo *evinfo); |
|
136 |
static void tossEventInfoRefs(JNIEnv *env, EventInfo *evinfo); |
|
137 |
||
138 |
static jint |
|
139 |
commandSize(HelperCommand *command) |
|
140 |
{ |
|
141 |
jint size = sizeof(HelperCommand); |
|
142 |
if (command->commandKind == COMMAND_REPORT_EVENT_COMPOSITE) { |
|
143 |
/* |
|
144 |
* One event is accounted for in the Helper Command. If there are |
|
145 |
* more, add to size here. |
|
146 |
*/ |
|
147 |
/*LINTED*/ |
|
148 |
size += ((int)sizeof(CommandSingle) * |
|
149 |
(command->u.reportEventComposite.eventCount - 1)); |
|
150 |
} |
|
151 |
return size; |
|
152 |
} |
|
153 |
||
154 |
static void |
|
155 |
freeCommand(HelperCommand *command) |
|
156 |
{ |
|
157 |
if ( command == NULL ) |
|
158 |
return; |
|
159 |
jvmtiDeallocate(command); |
|
160 |
} |
|
161 |
||
162 |
static void |
|
163 |
enqueueCommand(HelperCommand *command, |
|
164 |
jboolean wait, jboolean reportingVMDeath) |
|
165 |
{ |
|
166 |
static jboolean vmDeathReported = JNI_FALSE; |
|
167 |
CommandQueue *queue = &commandQueue; |
|
168 |
jint size = commandSize(command); |
|
169 |
||
170 |
command->done = JNI_FALSE; |
|
171 |
command->waiting = wait; |
|
172 |
command->next = NULL; |
|
173 |
||
174 |
debugMonitorEnter(commandQueueLock); |
|
175 |
while (size + currentQueueSize > maxQueueSize) { |
|
176 |
debugMonitorWait(commandQueueLock); |
|
177 |
} |
|
178 |
log_debugee_location("enqueueCommand(): HelperCommand being processed", NULL, NULL, 0); |
|
179 |
if (vmDeathReported) { |
|
180 |
/* send no more events after VMDeath and don't wait */ |
|
181 |
wait = JNI_FALSE; |
|
182 |
} else { |
|
183 |
currentQueueSize += size; |
|
184 |
||
185 |
if (queue->head == NULL) { |
|
186 |
queue->head = command; |
|
187 |
} else { |
|
188 |
queue->tail->next = command; |
|
189 |
} |
|
190 |
queue->tail = command; |
|
191 |
||
192 |
if (reportingVMDeath) { |
|
193 |
vmDeathReported = JNI_TRUE; |
|
194 |
} |
|
195 |
} |
|
196 |
debugMonitorNotifyAll(commandQueueLock); |
|
197 |
debugMonitorExit(commandQueueLock); |
|
198 |
||
199 |
if (wait) { |
|
200 |
debugMonitorEnter(commandCompleteLock); |
|
201 |
while (!command->done) { |
|
202 |
log_debugee_location("enqueueCommand(): HelperCommand wait", NULL, NULL, 0); |
|
203 |
debugMonitorWait(commandCompleteLock); |
|
204 |
} |
|
205 |
freeCommand(command); |
|
206 |
debugMonitorExit(commandCompleteLock); |
|
207 |
} |
|
208 |
} |
|
209 |
||
210 |
static void |
|
211 |
completeCommand(HelperCommand *command) |
|
212 |
{ |
|
213 |
if (command->waiting) { |
|
214 |
debugMonitorEnter(commandCompleteLock); |
|
215 |
command->done = JNI_TRUE; |
|
216 |
log_debugee_location("completeCommand(): HelperCommand done waiting", NULL, NULL, 0); |
|
217 |
debugMonitorNotifyAll(commandCompleteLock); |
|
218 |
debugMonitorExit(commandCompleteLock); |
|
219 |
} else { |
|
220 |
freeCommand(command); |
|
221 |
} |
|
222 |
} |
|
223 |
||
224 |
static HelperCommand * |
|
225 |
dequeueCommand(void) |
|
226 |
{ |
|
227 |
HelperCommand *command = NULL; |
|
228 |
CommandQueue *queue = &commandQueue; |
|
229 |
jint size; |
|
230 |
||
231 |
debugMonitorEnter(commandQueueLock); |
|
232 |
||
233 |
while (command == NULL) { |
|
234 |
while (holdEvents || (queue->head == NULL)) { |
|
235 |
debugMonitorWait(commandQueueLock); |
|
236 |
} |
|
237 |
||
238 |
JDI_ASSERT(queue->head); |
|
239 |
command = queue->head; |
|
240 |
queue->head = command->next; |
|
241 |
if (queue->tail == command) { |
|
242 |
queue->tail = NULL; |
|
243 |
} |
|
244 |
||
245 |
log_debugee_location("dequeueCommand(): command being dequeued", NULL, NULL, 0); |
|
246 |
||
247 |
size = commandSize(command); |
|
248 |
/* |
|
46841
76a15bd1de0a
8134103: JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
25859
diff
changeset
|
249 |
* Immediately close out any commands enqueued from |
76a15bd1de0a
8134103: JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
25859
diff
changeset
|
250 |
* a dead VM or a previously attached debugger. |
2 | 251 |
*/ |
46841
76a15bd1de0a
8134103: JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
25859
diff
changeset
|
252 |
if (gdata->vmDead || command->sessionID != currentSessionID) { |
2 | 253 |
log_debugee_location("dequeueCommand(): command session removal", NULL, NULL, 0); |
254 |
completeCommand(command); |
|
255 |
command = NULL; |
|
256 |
} |
|
257 |
||
258 |
/* |
|
259 |
* There's room in the queue for more. |
|
260 |
*/ |
|
261 |
currentQueueSize -= size; |
|
262 |
debugMonitorNotifyAll(commandQueueLock); |
|
263 |
} |
|
264 |
||
265 |
debugMonitorExit(commandQueueLock); |
|
266 |
||
267 |
return command; |
|
268 |
} |
|
269 |
||
270 |
void eventHelper_holdEvents(void) |
|
271 |
{ |
|
272 |
debugMonitorEnter(commandQueueLock); |
|
273 |
holdEvents = JNI_TRUE; |
|
274 |
debugMonitorNotifyAll(commandQueueLock); |
|
275 |
debugMonitorExit(commandQueueLock); |
|
276 |
} |
|
277 |
||
278 |
void eventHelper_releaseEvents(void) |
|
279 |
{ |
|
280 |
debugMonitorEnter(commandQueueLock); |
|
281 |
holdEvents = JNI_FALSE; |
|
282 |
debugMonitorNotifyAll(commandQueueLock); |
|
283 |
debugMonitorExit(commandQueueLock); |
|
284 |
} |
|
285 |
||
286 |
static void |
|
287 |
writeSingleStepEvent(JNIEnv *env, PacketOutputStream *out, EventInfo *evinfo) |
|
288 |
{ |
|
289 |
(void)outStream_writeObjectRef(env, out, evinfo->thread); |
|
290 |
writeCodeLocation(out, evinfo->clazz, evinfo->method, evinfo->location); |
|
291 |
} |
|
292 |
||
293 |
static void |
|
294 |
writeBreakpointEvent(JNIEnv *env, PacketOutputStream *out, EventInfo *evinfo) |
|
295 |
{ |
|
296 |
(void)outStream_writeObjectRef(env, out, evinfo->thread); |
|
297 |
writeCodeLocation(out, evinfo->clazz, evinfo->method, evinfo->location); |
|
298 |
} |
|
299 |
||
300 |
static void |
|
301 |
writeFieldAccessEvent(JNIEnv *env, PacketOutputStream *out, EventInfo *evinfo) |
|
302 |
{ |
|
303 |
jbyte fieldClassTag; |
|
304 |
||
305 |
fieldClassTag = referenceTypeTag(evinfo->u.field_access.field_clazz); |
|
306 |
||
307 |
(void)outStream_writeObjectRef(env, out, evinfo->thread); |
|
308 |
writeCodeLocation(out, evinfo->clazz, evinfo->method, evinfo->location); |
|
309 |
(void)outStream_writeByte(out, fieldClassTag); |
|
310 |
(void)outStream_writeObjectRef(env, out, evinfo->u.field_access.field_clazz); |
|
311 |
(void)outStream_writeFieldID(out, evinfo->u.field_access.field); |
|
312 |
(void)outStream_writeObjectTag(env, out, evinfo->object); |
|
313 |
(void)outStream_writeObjectRef(env, out, evinfo->object); |
|
314 |
} |
|
315 |
||
316 |
static void |
|
317 |
writeFieldModificationEvent(JNIEnv *env, PacketOutputStream *out, |
|
318 |
EventInfo *evinfo) |
|
319 |
{ |
|
320 |
jbyte fieldClassTag; |
|
321 |
||
322 |
fieldClassTag = referenceTypeTag(evinfo->u.field_modification.field_clazz); |
|
323 |
||
324 |
(void)outStream_writeObjectRef(env, out, evinfo->thread); |
|
325 |
writeCodeLocation(out, evinfo->clazz, evinfo->method, evinfo->location); |
|
326 |
(void)outStream_writeByte(out, fieldClassTag); |
|
327 |
(void)outStream_writeObjectRef(env, out, evinfo->u.field_modification.field_clazz); |
|
328 |
(void)outStream_writeFieldID(out, evinfo->u.field_modification.field); |
|
329 |
(void)outStream_writeObjectTag(env, out, evinfo->object); |
|
330 |
(void)outStream_writeObjectRef(env, out, evinfo->object); |
|
331 |
(void)outStream_writeValue(env, out, (jbyte)evinfo->u.field_modification.signature_type, |
|
332 |
evinfo->u.field_modification.new_value); |
|
333 |
} |
|
334 |
||
335 |
static void |
|
336 |
writeExceptionEvent(JNIEnv *env, PacketOutputStream *out, EventInfo *evinfo) |
|
337 |
{ |
|
338 |
(void)outStream_writeObjectRef(env, out, evinfo->thread); |
|
339 |
writeCodeLocation(out, evinfo->clazz, evinfo->method, evinfo->location); |
|
340 |
(void)outStream_writeObjectTag(env, out, evinfo->object); |
|
341 |
(void)outStream_writeObjectRef(env, out, evinfo->object); |
|
342 |
writeCodeLocation(out, evinfo->u.exception.catch_clazz, |
|
343 |
evinfo->u.exception.catch_method, evinfo->u.exception.catch_location); |
|
344 |
} |
|
345 |
||
346 |
static void |
|
347 |
writeThreadEvent(JNIEnv *env, PacketOutputStream *out, EventInfo *evinfo) |
|
348 |
{ |
|
349 |
(void)outStream_writeObjectRef(env, out, evinfo->thread); |
|
350 |
} |
|
351 |
||
352 |
static void |
|
353 |
writeMonitorEvent(JNIEnv *env, PacketOutputStream *out, EventInfo *evinfo) |
|
354 |
{ |
|
355 |
jclass klass; |
|
356 |
(void)outStream_writeObjectRef(env, out, evinfo->thread); |
|
357 |
(void)outStream_writeObjectTag(env, out, evinfo->object); |
|
358 |
(void)outStream_writeObjectRef(env, out, evinfo->object); |
|
359 |
if (evinfo->ei == EI_MONITOR_WAIT || evinfo->ei == EI_MONITOR_WAITED) { |
|
360 |
/* clazz of evinfo was set to class of monitor object for monitor wait event class filtering. |
|
361 |
* So get the method class to write location info. |
|
362 |
* See cbMonitorWait() and cbMonitorWaited() function in eventHandler.c. |
|
363 |
*/ |
|
364 |
klass=getMethodClass(gdata->jvmti, evinfo->method); |
|
365 |
writeCodeLocation(out, klass, evinfo->method, evinfo->location); |
|
366 |
if (evinfo->ei == EI_MONITOR_WAIT) { |
|
367 |
(void)outStream_writeLong(out, evinfo->u.monitor.timeout); |
|
368 |
} else if (evinfo->ei == EI_MONITOR_WAITED) { |
|
369 |
(void)outStream_writeBoolean(out, evinfo->u.monitor.timed_out); |
|
370 |
} |
|
371 |
/* This runs in a command loop and this thread may not return to java. |
|
372 |
* So we need to delete the local ref created by jvmti GetMethodDeclaringClass. |
|
373 |
*/ |
|
374 |
JNI_FUNC_PTR(env,DeleteLocalRef)(env, klass); |
|
375 |
} else { |
|
376 |
writeCodeLocation(out, evinfo->clazz, evinfo->method, evinfo->location); |
|
377 |
} |
|
378 |
} |
|
379 |
||
380 |
static void |
|
381 |
writeClassEvent(JNIEnv *env, PacketOutputStream *out, EventInfo *evinfo) |
|
382 |
{ |
|
383 |
jbyte classTag; |
|
384 |
jint status; |
|
385 |
char *signature = NULL; |
|
386 |
jvmtiError error; |
|
387 |
||
388 |
classTag = referenceTypeTag(evinfo->clazz); |
|
389 |
error = classSignature(evinfo->clazz, &signature, NULL); |
|
390 |
if (error != JVMTI_ERROR_NONE) { |
|
391 |
EXIT_ERROR(error,"signature"); |
|
392 |
} |
|
393 |
status = classStatus(evinfo->clazz); |
|
394 |
||
395 |
(void)outStream_writeObjectRef(env, out, evinfo->thread); |
|
396 |
(void)outStream_writeByte(out, classTag); |
|
397 |
(void)outStream_writeObjectRef(env, out, evinfo->clazz); |
|
398 |
(void)outStream_writeString(out, signature); |
|
399 |
(void)outStream_writeInt(out, map2jdwpClassStatus(status)); |
|
400 |
jvmtiDeallocate(signature); |
|
401 |
} |
|
402 |
||
403 |
static void |
|
404 |
writeVMDeathEvent(JNIEnv *env, PacketOutputStream *out, EventInfo *evinfo) |
|
405 |
{ |
|
406 |
} |
|
407 |
||
408 |
static void |
|
409 |
handleEventCommandSingle(JNIEnv *env, PacketOutputStream *out, |
|
410 |
EventCommandSingle *command) |
|
411 |
{ |
|
412 |
EventInfo *evinfo = &command->info; |
|
413 |
||
414 |
(void)outStream_writeByte(out, eventIndex2jdwp(evinfo->ei)); |
|
415 |
(void)outStream_writeInt(out, command->id); |
|
416 |
||
417 |
switch (evinfo->ei) { |
|
418 |
case EI_SINGLE_STEP: |
|
419 |
writeSingleStepEvent(env, out, evinfo); |
|
420 |
break; |
|
421 |
case EI_BREAKPOINT: |
|
422 |
writeBreakpointEvent(env, out, evinfo); |
|
423 |
break; |
|
424 |
case EI_FIELD_ACCESS: |
|
425 |
writeFieldAccessEvent(env, out, evinfo); |
|
426 |
break; |
|
427 |
case EI_FIELD_MODIFICATION: |
|
428 |
writeFieldModificationEvent(env, out, evinfo); |
|
429 |
break; |
|
430 |
case EI_EXCEPTION: |
|
431 |
writeExceptionEvent(env, out, evinfo); |
|
432 |
break; |
|
433 |
case EI_THREAD_START: |
|
434 |
case EI_THREAD_END: |
|
435 |
writeThreadEvent(env, out, evinfo); |
|
436 |
break; |
|
437 |
case EI_CLASS_LOAD: |
|
438 |
case EI_CLASS_PREPARE: |
|
439 |
writeClassEvent(env, out, evinfo); |
|
440 |
break; |
|
441 |
case EI_MONITOR_CONTENDED_ENTER: |
|
442 |
case EI_MONITOR_CONTENDED_ENTERED: |
|
443 |
case EI_MONITOR_WAIT: |
|
444 |
case EI_MONITOR_WAITED: |
|
445 |
writeMonitorEvent(env, out, evinfo); |
|
446 |
break; |
|
447 |
case EI_VM_DEATH: |
|
448 |
writeVMDeathEvent(env, out, evinfo); |
|
449 |
break; |
|
450 |
default: |
|
451 |
EXIT_ERROR(AGENT_ERROR_INVALID_EVENT_TYPE,"unknown event index"); |
|
452 |
break; |
|
453 |
} |
|
454 |
tossEventInfoRefs(env, evinfo); |
|
455 |
} |
|
456 |
||
457 |
static void |
|
458 |
handleUnloadCommandSingle(JNIEnv* env, PacketOutputStream *out, |
|
459 |
UnloadCommandSingle *command) |
|
460 |
{ |
|
461 |
(void)outStream_writeByte(out, JDWP_EVENT(CLASS_UNLOAD)); |
|
462 |
(void)outStream_writeInt(out, command->id); |
|
463 |
(void)outStream_writeString(out, command->classSignature); |
|
464 |
jvmtiDeallocate(command->classSignature); |
|
465 |
command->classSignature = NULL; |
|
466 |
} |
|
467 |
||
468 |
static void |
|
469 |
handleFrameEventCommandSingle(JNIEnv* env, PacketOutputStream *out, |
|
470 |
FrameEventCommandSingle *command) |
|
471 |
{ |
|
472 |
if (command->typeKey) { |
|
473 |
(void)outStream_writeByte(out, JDWP_EVENT(METHOD_EXIT_WITH_RETURN_VALUE)); |
|
474 |
} else { |
|
475 |
(void)outStream_writeByte(out, eventIndex2jdwp(command->ei)); |
|
476 |
} |
|
477 |
(void)outStream_writeInt(out, command->id); |
|
478 |
(void)outStream_writeObjectRef(env, out, command->thread); |
|
479 |
writeCodeLocation(out, command->clazz, command->method, command->location); |
|
480 |
if (command->typeKey) { |
|
481 |
(void)outStream_writeValue(env, out, command->typeKey, command->returnValue); |
|
482 |
if (isObjectTag(command->typeKey) && |
|
483 |
command->returnValue.l != NULL) { |
|
484 |
tossGlobalRef(env, &(command->returnValue.l)); |
|
485 |
} |
|
486 |
} |
|
487 |
tossGlobalRef(env, &(command->thread)); |
|
488 |
tossGlobalRef(env, &(command->clazz)); |
|
489 |
} |
|
490 |
||
491 |
static void |
|
492 |
suspendWithInvokeEnabled(jbyte policy, jthread thread) |
|
493 |
{ |
|
494 |
invoker_enableInvokeRequests(thread); |
|
495 |
||
496 |
if (policy == JDWP_SUSPEND_POLICY(ALL)) { |
|
497 |
(void)threadControl_suspendAll(); |
|
498 |
} else { |
|
499 |
(void)threadControl_suspendThread(thread, JNI_FALSE); |
|
500 |
} |
|
501 |
} |
|
502 |
||
503 |
static void |
|
504 |
handleReportEventCompositeCommand(JNIEnv *env, |
|
505 |
ReportEventCompositeCommand *recc) |
|
506 |
{ |
|
507 |
PacketOutputStream out; |
|
508 |
jint count = recc->eventCount; |
|
509 |
jint i; |
|
510 |
||
511 |
if (recc->suspendPolicy != JDWP_SUSPEND_POLICY(NONE)) { |
|
512 |
/* must determine thread to interrupt before writing */ |
|
513 |
/* since writing destroys it */ |
|
514 |
jthread thread = NULL; |
|
515 |
for (i = 0; i < count; i++) { |
|
516 |
CommandSingle *single = &(recc->singleCommand[i]); |
|
517 |
switch (single->singleKind) { |
|
518 |
case COMMAND_SINGLE_EVENT: |
|
519 |
thread = single->u.eventCommand.info.thread; |
|
520 |
break; |
|
521 |
case COMMAND_SINGLE_FRAME_EVENT: |
|
522 |
thread = single->u.frameEventCommand.thread; |
|
523 |
break; |
|
524 |
} |
|
525 |
if (thread != NULL) { |
|
526 |
break; |
|
527 |
} |
|
528 |
} |
|
529 |
||
530 |
if (thread == NULL) { |
|
531 |
(void)threadControl_suspendAll(); |
|
532 |
} else { |
|
533 |
suspendWithInvokeEnabled(recc->suspendPolicy, thread); |
|
534 |
} |
|
535 |
} |
|
536 |
||
537 |
outStream_initCommand(&out, uniqueID(), 0x0, |
|
538 |
JDWP_COMMAND_SET(Event), |
|
539 |
JDWP_COMMAND(Event, Composite)); |
|
540 |
(void)outStream_writeByte(&out, recc->suspendPolicy); |
|
541 |
(void)outStream_writeInt(&out, count); |
|
542 |
||
543 |
for (i = 0; i < count; i++) { |
|
544 |
CommandSingle *single = &(recc->singleCommand[i]); |
|
545 |
switch (single->singleKind) { |
|
546 |
case COMMAND_SINGLE_EVENT: |
|
547 |
handleEventCommandSingle(env, &out, |
|
548 |
&single->u.eventCommand); |
|
549 |
break; |
|
550 |
case COMMAND_SINGLE_UNLOAD: |
|
551 |
handleUnloadCommandSingle(env, &out, |
|
552 |
&single->u.unloadCommand); |
|
553 |
break; |
|
554 |
case COMMAND_SINGLE_FRAME_EVENT: |
|
555 |
handleFrameEventCommandSingle(env, &out, |
|
556 |
&single->u.frameEventCommand); |
|
557 |
break; |
|
558 |
} |
|
559 |
} |
|
560 |
||
561 |
outStream_sendCommand(&out); |
|
562 |
outStream_destroy(&out); |
|
563 |
} |
|
564 |
||
565 |
static void |
|
566 |
handleReportInvokeDoneCommand(JNIEnv* env, ReportInvokeDoneCommand *command) |
|
567 |
{ |
|
568 |
invoker_completeInvokeRequest(command->thread); |
|
569 |
tossGlobalRef(env, &(command->thread)); |
|
570 |
} |
|
571 |
||
572 |
static void |
|
573 |
handleReportVMInitCommand(JNIEnv* env, ReportVMInitCommand *command) |
|
574 |
{ |
|
575 |
PacketOutputStream out; |
|
576 |
||
577 |
if (command->suspendPolicy == JDWP_SUSPEND_POLICY(ALL)) { |
|
578 |
(void)threadControl_suspendAll(); |
|
579 |
} else if (command->suspendPolicy == JDWP_SUSPEND_POLICY(EVENT_THREAD)) { |
|
580 |
(void)threadControl_suspendThread(command->thread, JNI_FALSE); |
|
581 |
} |
|
582 |
||
583 |
outStream_initCommand(&out, uniqueID(), 0x0, |
|
584 |
JDWP_COMMAND_SET(Event), |
|
585 |
JDWP_COMMAND(Event, Composite)); |
|
586 |
(void)outStream_writeByte(&out, command->suspendPolicy); |
|
587 |
(void)outStream_writeInt(&out, 1); /* Always one component */ |
|
588 |
(void)outStream_writeByte(&out, JDWP_EVENT(VM_INIT)); |
|
589 |
(void)outStream_writeInt(&out, 0); /* Not in response to an event req. */ |
|
590 |
||
591 |
(void)outStream_writeObjectRef(env, &out, command->thread); |
|
592 |
||
593 |
outStream_sendCommand(&out); |
|
594 |
outStream_destroy(&out); |
|
595 |
/* Why aren't we tossing this: tossGlobalRef(env, &(command->thread)); */ |
|
596 |
} |
|
597 |
||
598 |
static void |
|
599 |
handleSuspendThreadCommand(JNIEnv* env, SuspendThreadCommand *command) |
|
600 |
{ |
|
601 |
/* |
|
602 |
* For the moment, there's nothing that can be done with the |
|
603 |
* return code, so we don't check it here. |
|
604 |
*/ |
|
605 |
(void)threadControl_suspendThread(command->thread, JNI_TRUE); |
|
606 |
tossGlobalRef(env, &(command->thread)); |
|
607 |
} |
|
608 |
||
609 |
static void |
|
610 |
handleCommand(JNIEnv *env, HelperCommand *command) |
|
611 |
{ |
|
612 |
switch (command->commandKind) { |
|
613 |
case COMMAND_REPORT_EVENT_COMPOSITE: |
|
614 |
handleReportEventCompositeCommand(env, |
|
615 |
&command->u.reportEventComposite); |
|
616 |
break; |
|
617 |
case COMMAND_REPORT_INVOKE_DONE: |
|
618 |
handleReportInvokeDoneCommand(env, &command->u.reportInvokeDone); |
|
619 |
break; |
|
620 |
case COMMAND_REPORT_VM_INIT: |
|
621 |
handleReportVMInitCommand(env, &command->u.reportVMInit); |
|
622 |
break; |
|
623 |
case COMMAND_SUSPEND_THREAD: |
|
624 |
handleSuspendThreadCommand(env, &command->u.suspendThread); |
|
625 |
break; |
|
626 |
default: |
|
627 |
EXIT_ERROR(AGENT_ERROR_INVALID_EVENT_TYPE,"Event Helper Command"); |
|
628 |
break; |
|
629 |
} |
|
630 |
} |
|
631 |
||
632 |
/* |
|
633 |
* There was an assumption that only one event with a suspend-all |
|
634 |
* policy could be processed by commandLoop() at one time. It was |
|
635 |
* assumed that native thread suspension from the first suspend-all |
|
636 |
* event would prevent the second suspend-all event from making it |
|
637 |
* into the command queue. For the Classic VM, this was a reasonable |
|
638 |
* assumption. However, in HotSpot all thread suspension requires a |
|
639 |
* VM operation and VM operations take time. |
|
640 |
* |
|
641 |
* The solution is to add a mechanism to prevent commandLoop() from |
|
642 |
* processing more than one event with a suspend-all policy. This is |
|
643 |
* accomplished by forcing commandLoop() to wait for either |
|
644 |
* ThreadReferenceImpl.c: resume() or VirtualMachineImpl.c: resume() |
|
645 |
* when an event with a suspend-all policy has been completed. |
|
646 |
*/ |
|
647 |
static jboolean blockCommandLoop = JNI_FALSE; |
|
648 |
||
649 |
/* |
|
650 |
* We wait for either ThreadReferenceImpl.c: resume() or |
|
651 |
* VirtualMachineImpl.c: resume() to be called. |
|
652 |
*/ |
|
653 |
static void |
|
654 |
doBlockCommandLoop(void) { |
|
655 |
debugMonitorEnter(blockCommandLoopLock); |
|
656 |
while (blockCommandLoop == JNI_TRUE) { |
|
657 |
debugMonitorWait(blockCommandLoopLock); |
|
658 |
} |
|
659 |
debugMonitorExit(blockCommandLoopLock); |
|
660 |
} |
|
661 |
||
662 |
/* |
|
663 |
* If the command that we are about to execute has a suspend-all |
|
664 |
* policy, then prepare for either ThreadReferenceImpl.c: resume() |
|
665 |
* or VirtualMachineImpl.c: resume() to be called. |
|
666 |
*/ |
|
667 |
static jboolean |
|
668 |
needBlockCommandLoop(HelperCommand *cmd) { |
|
669 |
if (cmd->commandKind == COMMAND_REPORT_EVENT_COMPOSITE |
|
670 |
&& cmd->u.reportEventComposite.suspendPolicy == JDWP_SUSPEND_POLICY(ALL)) { |
|
671 |
debugMonitorEnter(blockCommandLoopLock); |
|
672 |
blockCommandLoop = JNI_TRUE; |
|
673 |
debugMonitorExit(blockCommandLoopLock); |
|
674 |
||
675 |
return JNI_TRUE; |
|
676 |
} |
|
677 |
||
678 |
return JNI_FALSE; |
|
679 |
} |
|
680 |
||
681 |
/* |
|
682 |
* Used by either ThreadReferenceImpl.c: resume() or |
|
683 |
* VirtualMachineImpl.c: resume() to resume commandLoop(). |
|
684 |
*/ |
|
685 |
void |
|
686 |
unblockCommandLoop(void) { |
|
687 |
debugMonitorEnter(blockCommandLoopLock); |
|
688 |
blockCommandLoop = JNI_FALSE; |
|
689 |
debugMonitorNotifyAll(blockCommandLoopLock); |
|
690 |
debugMonitorExit(blockCommandLoopLock); |
|
691 |
} |
|
692 |
||
693 |
/* |
|
694 |
* The event helper thread. Dequeues commands and processes them. |
|
695 |
*/ |
|
696 |
static void JNICALL |
|
697 |
commandLoop(jvmtiEnv* jvmti_env, JNIEnv* jni_env, void* arg) |
|
698 |
{ |
|
699 |
LOG_MISC(("Begin command loop thread")); |
|
700 |
||
701 |
while (JNI_TRUE) { |
|
702 |
HelperCommand *command = dequeueCommand(); |
|
703 |
if (command != NULL) { |
|
704 |
/* |
|
705 |
* Setup for a potential doBlockCommand() call before calling |
|
706 |
* handleCommand() to prevent any races. |
|
707 |
*/ |
|
708 |
jboolean doBlock = needBlockCommandLoop(command); |
|
47525
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
709 |
debugMonitorEnter(vmDeathLock); |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
710 |
commandLoopEnteredVmDeathLock = JNI_TRUE; |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
711 |
if (!gdata->vmDead) { |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
712 |
log_debugee_location("commandLoop(): command being handled", NULL, NULL, 0); |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
713 |
handleCommand(jni_env, command); |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
714 |
} |
2 | 715 |
completeCommand(command); |
47525
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
716 |
debugMonitorExit(vmDeathLock); |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
717 |
commandLoopEnteredVmDeathLock = JNI_FALSE; |
2 | 718 |
/* if we just finished a suspend-all cmd, then we block here */ |
719 |
if (doBlock) { |
|
720 |
doBlockCommandLoop(); |
|
721 |
} |
|
722 |
} |
|
723 |
} |
|
724 |
/* This loop never ends, even as connections come and go with server=y */ |
|
725 |
} |
|
726 |
||
727 |
void |
|
728 |
eventHelper_initialize(jbyte sessionID) |
|
729 |
{ |
|
730 |
jvmtiStartFunction func; |
|
731 |
||
732 |
currentSessionID = sessionID; |
|
733 |
holdEvents = JNI_FALSE; |
|
734 |
commandQueue.head = NULL; |
|
735 |
commandQueue.tail = NULL; |
|
736 |
||
737 |
commandQueueLock = debugMonitorCreate("JDWP Event Helper Queue Monitor"); |
|
738 |
commandCompleteLock = debugMonitorCreate("JDWP Event Helper Completion Monitor"); |
|
739 |
blockCommandLoopLock = debugMonitorCreate("JDWP Event Block CommandLoop Monitor"); |
|
47525
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
740 |
vmDeathLock = debugMonitorCreate("JDWP VM_DEATH CommandLoop Monitor"); |
2 | 741 |
|
742 |
/* Start the event handler thread */ |
|
743 |
func = &commandLoop; |
|
47525
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
744 |
(void)spawnNewThread(func, NULL, COMMAND_LOOP_THREAD_NAME); |
2 | 745 |
} |
746 |
||
747 |
void |
|
748 |
eventHelper_reset(jbyte newSessionID) |
|
749 |
{ |
|
750 |
debugMonitorEnter(commandQueueLock); |
|
751 |
currentSessionID = newSessionID; |
|
752 |
holdEvents = JNI_FALSE; |
|
753 |
debugMonitorNotifyAll(commandQueueLock); |
|
754 |
debugMonitorExit(commandQueueLock); |
|
755 |
} |
|
756 |
||
757 |
/* |
|
758 |
* Provide a means for threadControl to ensure that crucial locks are not |
|
759 |
* held by suspended threads. |
|
760 |
*/ |
|
761 |
void |
|
762 |
eventHelper_lock(void) |
|
763 |
{ |
|
764 |
debugMonitorEnter(commandQueueLock); |
|
765 |
debugMonitorEnter(commandCompleteLock); |
|
766 |
} |
|
767 |
||
768 |
void |
|
769 |
eventHelper_unlock(void) |
|
770 |
{ |
|
771 |
debugMonitorExit(commandCompleteLock); |
|
772 |
debugMonitorExit(commandQueueLock); |
|
773 |
} |
|
774 |
||
56230 | 775 |
void commandLoop_exitVmDeathLockOnError(void) |
47525
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
776 |
{ |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
777 |
const char* MSG_BASE = "exitVmDeathLockOnError: error in JVMTI %s: %d\n"; |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
778 |
jthread cur_thread = NULL; |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
779 |
jvmtiThreadInfo thread_info; |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
780 |
jvmtiError err = JVMTI_ERROR_NONE; |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
781 |
|
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
782 |
err = JVMTI_FUNC_PTR(gdata->jvmti, GetCurrentThread) |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
783 |
(gdata->jvmti, &cur_thread); |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
784 |
if (err != JVMTI_ERROR_NONE) { |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
785 |
LOG_ERROR((MSG_BASE, "GetCurrentThread", err)); |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
786 |
return; |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
787 |
} |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
788 |
|
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
789 |
err = JVMTI_FUNC_PTR(gdata->jvmti, GetThreadInfo) |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
790 |
(gdata->jvmti, cur_thread, &thread_info); |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
791 |
if (err != JVMTI_ERROR_NONE) { |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
792 |
LOG_ERROR((MSG_BASE, "GetThreadInfo", err)); |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
793 |
return; |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
794 |
} |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
795 |
if (strcmp(thread_info.name, COMMAND_LOOP_THREAD_NAME) != 0) { |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
796 |
return; |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
797 |
} |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
798 |
if (commandLoopEnteredVmDeathLock == JNI_TRUE) { |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
799 |
debugMonitorExit(vmDeathLock); |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
800 |
commandLoopEnteredVmDeathLock = JNI_FALSE; |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
801 |
} |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
802 |
} |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
803 |
|
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
804 |
void |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
805 |
commandLoop_sync(void) |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
806 |
{ |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
807 |
debugMonitorEnter(vmDeathLock); |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
808 |
debugMonitorExit(vmDeathLock); |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
809 |
} |
e05aff6beada
8177901: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents:
47216
diff
changeset
|
810 |
|
2 | 811 |
/* Change all references to global in the EventInfo struct */ |
812 |
static void |
|
813 |
saveEventInfoRefs(JNIEnv *env, EventInfo *evinfo) |
|
814 |
{ |
|
815 |
jthread *pthread; |
|
816 |
jclass *pclazz; |
|
817 |
jobject *pobject; |
|
818 |
jthread thread; |
|
819 |
jclass clazz; |
|
820 |
jobject object; |
|
821 |
char sig; |
|
822 |
||
823 |
JNI_FUNC_PTR(env,ExceptionClear)(env); |
|
824 |
||
825 |
if ( evinfo->thread != NULL ) { |
|
826 |
pthread = &(evinfo->thread); |
|
827 |
thread = *pthread; |
|
828 |
*pthread = NULL; |
|
829 |
saveGlobalRef(env, thread, pthread); |
|
830 |
} |
|
831 |
if ( evinfo->clazz != NULL ) { |
|
832 |
pclazz = &(evinfo->clazz); |
|
833 |
clazz = *pclazz; |
|
834 |
*pclazz = NULL; |
|
835 |
saveGlobalRef(env, clazz, pclazz); |
|
836 |
} |
|
837 |
if ( evinfo->object != NULL ) { |
|
838 |
pobject = &(evinfo->object); |
|
839 |
object = *pobject; |
|
840 |
*pobject = NULL; |
|
841 |
saveGlobalRef(env, object, pobject); |
|
842 |
} |
|
843 |
||
844 |
switch (evinfo->ei) { |
|
845 |
case EI_FIELD_MODIFICATION: |
|
846 |
if ( evinfo->u.field_modification.field_clazz != NULL ) { |
|
847 |
pclazz = &(evinfo->u.field_modification.field_clazz); |
|
848 |
clazz = *pclazz; |
|
849 |
*pclazz = NULL; |
|
850 |
saveGlobalRef(env, clazz, pclazz); |
|
851 |
} |
|
852 |
sig = evinfo->u.field_modification.signature_type; |
|
853 |
if ((sig == JDWP_TAG(ARRAY)) || (sig == JDWP_TAG(OBJECT))) { |
|
854 |
if ( evinfo->u.field_modification.new_value.l != NULL ) { |
|
855 |
pobject = &(evinfo->u.field_modification.new_value.l); |
|
856 |
object = *pobject; |
|
857 |
*pobject = NULL; |
|
858 |
saveGlobalRef(env, object, pobject); |
|
859 |
} |
|
860 |
} |
|
861 |
break; |
|
862 |
case EI_FIELD_ACCESS: |
|
863 |
if ( evinfo->u.field_access.field_clazz != NULL ) { |
|
864 |
pclazz = &(evinfo->u.field_access.field_clazz); |
|
865 |
clazz = *pclazz; |
|
866 |
*pclazz = NULL; |
|
867 |
saveGlobalRef(env, clazz, pclazz); |
|
868 |
} |
|
869 |
break; |
|
870 |
case EI_EXCEPTION: |
|
871 |
if ( evinfo->u.exception.catch_clazz != NULL ) { |
|
872 |
pclazz = &(evinfo->u.exception.catch_clazz); |
|
873 |
clazz = *pclazz; |
|
874 |
*pclazz = NULL; |
|
875 |
saveGlobalRef(env, clazz, pclazz); |
|
876 |
} |
|
877 |
break; |
|
878 |
default: |
|
879 |
break; |
|
880 |
} |
|
881 |
||
882 |
if (JNI_FUNC_PTR(env,ExceptionOccurred)(env)) { |
|
883 |
EXIT_ERROR(AGENT_ERROR_INVALID_EVENT_TYPE,"ExceptionOccurred"); |
|
884 |
} |
|
885 |
} |
|
886 |
||
887 |
static void |
|
888 |
tossEventInfoRefs(JNIEnv *env, EventInfo *evinfo) |
|
889 |
{ |
|
890 |
char sig; |
|
891 |
if ( evinfo->thread != NULL ) { |
|
892 |
tossGlobalRef(env, &(evinfo->thread)); |
|
893 |
} |
|
894 |
if ( evinfo->clazz != NULL ) { |
|
895 |
tossGlobalRef(env, &(evinfo->clazz)); |
|
896 |
} |
|
897 |
if ( evinfo->object != NULL ) { |
|
898 |
tossGlobalRef(env, &(evinfo->object)); |
|
899 |
} |
|
900 |
switch (evinfo->ei) { |
|
901 |
case EI_FIELD_MODIFICATION: |
|
902 |
if ( evinfo->u.field_modification.field_clazz != NULL ) { |
|
903 |
tossGlobalRef(env, &(evinfo->u.field_modification.field_clazz)); |
|
904 |
} |
|
905 |
sig = evinfo->u.field_modification.signature_type; |
|
906 |
if ((sig == JDWP_TAG(ARRAY)) || (sig == JDWP_TAG(OBJECT))) { |
|
907 |
if ( evinfo->u.field_modification.new_value.l != NULL ) { |
|
908 |
tossGlobalRef(env, &(evinfo->u.field_modification.new_value.l)); |
|
909 |
} |
|
910 |
} |
|
911 |
break; |
|
912 |
case EI_FIELD_ACCESS: |
|
913 |
if ( evinfo->u.field_access.field_clazz != NULL ) { |
|
914 |
tossGlobalRef(env, &(evinfo->u.field_access.field_clazz)); |
|
915 |
} |
|
916 |
break; |
|
917 |
case EI_EXCEPTION: |
|
918 |
if ( evinfo->u.exception.catch_clazz != NULL ) { |
|
919 |
tossGlobalRef(env, &(evinfo->u.exception.catch_clazz)); |
|
920 |
} |
|
921 |
break; |
|
922 |
default: |
|
923 |
break; |
|
924 |
} |
|
925 |
} |
|
926 |
||
927 |
struct bag * |
|
928 |
eventHelper_createEventBag(void) |
|
929 |
{ |
|
930 |
return bagCreateBag(sizeof(CommandSingle), 5 /* events */ ); |
|
931 |
} |
|
932 |
||
933 |
/* Return the combined suspend policy for the event set |
|
934 |
*/ |
|
935 |
static jboolean |
|
936 |
enumForCombinedSuspendPolicy(void *cv, void *arg) |
|
937 |
{ |
|
938 |
CommandSingle *command = cv; |
|
939 |
jbyte thisPolicy; |
|
940 |
jbyte *policy = arg; |
|
941 |
||
942 |
switch(command->singleKind) { |
|
943 |
case COMMAND_SINGLE_EVENT: |
|
944 |
thisPolicy = command->u.eventCommand.suspendPolicy; |
|
945 |
break; |
|
946 |
case COMMAND_SINGLE_FRAME_EVENT: |
|
947 |
thisPolicy = command->u.frameEventCommand.suspendPolicy; |
|
948 |
break; |
|
949 |
default: |
|
950 |
thisPolicy = JDWP_SUSPEND_POLICY(NONE); |
|
951 |
} |
|
952 |
/* Expand running policy value if this policy demands it */ |
|
953 |
if (*policy == JDWP_SUSPEND_POLICY(NONE)) { |
|
954 |
*policy = thisPolicy; |
|
955 |
} else if (*policy == JDWP_SUSPEND_POLICY(EVENT_THREAD)) { |
|
956 |
*policy = (thisPolicy == JDWP_SUSPEND_POLICY(ALL))? |
|
957 |
thisPolicy : *policy; |
|
958 |
} |
|
959 |
||
960 |
/* Short circuit if we reached maximal suspend policy */ |
|
961 |
if (*policy == JDWP_SUSPEND_POLICY(ALL)) { |
|
962 |
return JNI_FALSE; |
|
963 |
} else { |
|
964 |
return JNI_TRUE; |
|
965 |
} |
|
966 |
} |
|
967 |
||
968 |
/* Determine whether we are reporting VM death |
|
969 |
*/ |
|
970 |
static jboolean |
|
971 |
enumForVMDeath(void *cv, void *arg) |
|
972 |
{ |
|
973 |
CommandSingle *command = cv; |
|
974 |
jboolean *reportingVMDeath = arg; |
|
975 |
||
976 |
if (command->singleKind == COMMAND_SINGLE_EVENT) { |
|
977 |
if (command->u.eventCommand.info.ei == EI_VM_DEATH) { |
|
978 |
*reportingVMDeath = JNI_TRUE; |
|
979 |
return JNI_FALSE; |
|
980 |
} |
|
981 |
} |
|
982 |
return JNI_TRUE; |
|
983 |
} |
|
984 |
||
985 |
struct singleTracker { |
|
986 |
ReportEventCompositeCommand *recc; |
|
987 |
int index; |
|
988 |
}; |
|
989 |
||
990 |
static jboolean |
|
991 |
enumForCopyingSingles(void *command, void *tv) |
|
992 |
{ |
|
993 |
struct singleTracker *tracker = (struct singleTracker *)tv; |
|
994 |
(void)memcpy(&tracker->recc->singleCommand[tracker->index++], |
|
995 |
command, |
|
996 |
sizeof(CommandSingle)); |
|
997 |
return JNI_TRUE; |
|
998 |
} |
|
999 |
||
1000 |
jbyte |
|
1001 |
eventHelper_reportEvents(jbyte sessionID, struct bag *eventBag) |
|
1002 |
{ |
|
1003 |
int size = bagSize(eventBag); |
|
1004 |
jbyte suspendPolicy = JDWP_SUSPEND_POLICY(NONE); |
|
1005 |
jboolean reportingVMDeath = JNI_FALSE; |
|
1006 |
jboolean wait; |
|
1007 |
int command_size; |
|
1008 |
||
1009 |
HelperCommand *command; |
|
1010 |
ReportEventCompositeCommand *recc; |
|
1011 |
struct singleTracker tracker; |
|
1012 |
||
1013 |
if (size == 0) { |
|
1014 |
return suspendPolicy; |
|
1015 |
} |
|
1016 |
(void)bagEnumerateOver(eventBag, enumForCombinedSuspendPolicy, &suspendPolicy); |
|
1017 |
(void)bagEnumerateOver(eventBag, enumForVMDeath, &reportingVMDeath); |
|
1018 |
||
1019 |
/*LINTED*/ |
|
1020 |
command_size = (int)(sizeof(HelperCommand) + |
|
1021 |
sizeof(CommandSingle)*(size-1)); |
|
1022 |
command = jvmtiAllocate(command_size); |
|
1023 |
(void)memset(command, 0, command_size); |
|
1024 |
command->commandKind = COMMAND_REPORT_EVENT_COMPOSITE; |
|
1025 |
command->sessionID = sessionID; |
|
1026 |
recc = &command->u.reportEventComposite; |
|
1027 |
recc->suspendPolicy = suspendPolicy; |
|
1028 |
recc->eventCount = size; |
|
1029 |
tracker.recc = recc; |
|
1030 |
tracker.index = 0; |
|
1031 |
(void)bagEnumerateOver(eventBag, enumForCopyingSingles, &tracker); |
|
1032 |
||
1033 |
/* |
|
1034 |
* We must wait if this thread (the event thread) is to be |
|
1035 |
* suspended or if the VM is about to die. (Waiting in the latter |
|
1036 |
* case ensures that we get the event out before the process dies.) |
|
1037 |
*/ |
|
1038 |
wait = (jboolean)((suspendPolicy != JDWP_SUSPEND_POLICY(NONE)) || |
|
1039 |
reportingVMDeath); |
|
1040 |
enqueueCommand(command, wait, reportingVMDeath); |
|
1041 |
return suspendPolicy; |
|
1042 |
} |
|
1043 |
||
1044 |
void |
|
1045 |
eventHelper_recordEvent(EventInfo *evinfo, jint id, jbyte suspendPolicy, |
|
1046 |
struct bag *eventBag) |
|
1047 |
{ |
|
1048 |
JNIEnv *env = getEnv(); |
|
1049 |
CommandSingle *command = bagAdd(eventBag); |
|
1050 |
if (command == NULL) { |
|
1051 |
EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"badAdd(eventBag)"); |
|
1052 |
} |
|
1053 |
||
1054 |
command->singleKind = COMMAND_SINGLE_EVENT; |
|
1055 |
command->u.eventCommand.suspendPolicy = suspendPolicy; |
|
1056 |
command->u.eventCommand.id = id; |
|
1057 |
||
1058 |
/* |
|
1059 |
* Copy the event into the command so that it can be used |
|
1060 |
* asynchronously by the event helper thread. |
|
1061 |
*/ |
|
1062 |
(void)memcpy(&command->u.eventCommand.info, evinfo, sizeof(*evinfo)); |
|
1063 |
saveEventInfoRefs(env, &command->u.eventCommand.info); |
|
1064 |
} |
|
1065 |
||
1066 |
void |
|
1067 |
eventHelper_recordClassUnload(jint id, char *signature, struct bag *eventBag) |
|
1068 |
{ |
|
1069 |
CommandSingle *command = bagAdd(eventBag); |
|
1070 |
if (command == NULL) { |
|
1071 |
EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"bagAdd(eventBag)"); |
|
1072 |
} |
|
1073 |
command->singleKind = COMMAND_SINGLE_UNLOAD; |
|
1074 |
command->u.unloadCommand.id = id; |
|
1075 |
command->u.unloadCommand.classSignature = signature; |
|
1076 |
} |
|
1077 |
||
1078 |
void |
|
1079 |
eventHelper_recordFrameEvent(jint id, jbyte suspendPolicy, EventIndex ei, |
|
1080 |
jthread thread, jclass clazz, |
|
1081 |
jmethodID method, jlocation location, |
|
1082 |
int needReturnValue, |
|
1083 |
jvalue returnValue, |
|
1084 |
struct bag *eventBag) |
|
1085 |
{ |
|
1086 |
JNIEnv *env = getEnv(); |
|
1087 |
FrameEventCommandSingle *frameCommand; |
|
1088 |
CommandSingle *command = bagAdd(eventBag); |
|
1089 |
jvmtiError err = JVMTI_ERROR_NONE; |
|
1090 |
if (command == NULL) { |
|
1091 |
EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"bagAdd(eventBag)"); |
|
1092 |
} |
|
1093 |
||
1094 |
command->singleKind = COMMAND_SINGLE_FRAME_EVENT; |
|
1095 |
frameCommand = &command->u.frameEventCommand; |
|
1096 |
frameCommand->suspendPolicy = suspendPolicy; |
|
1097 |
frameCommand->id = id; |
|
1098 |
frameCommand->ei = ei; |
|
1099 |
saveGlobalRef(env, thread, &(frameCommand->thread)); |
|
1100 |
saveGlobalRef(env, clazz, &(frameCommand->clazz)); |
|
1101 |
frameCommand->method = method; |
|
1102 |
frameCommand->location = location; |
|
1103 |
if (needReturnValue) { |
|
1104 |
err = methodReturnType(method, &frameCommand->typeKey); |
|
1105 |
JDI_ASSERT(err == JVMTI_ERROR_NONE); |
|
1106 |
||
1107 |
/* |
|
1108 |
* V or B C D F I J S Z L <classname> ; [ ComponentType |
|
1109 |
*/ |
|
1110 |
if (isObjectTag(frameCommand->typeKey) && |
|
1111 |
returnValue.l != NULL) { |
|
1112 |
saveGlobalRef(env, returnValue.l, &(frameCommand->returnValue.l)); |
|
1113 |
} else { |
|
1114 |
frameCommand->returnValue = returnValue; |
|
1115 |
} |
|
1116 |
} else { |
|
1117 |
/* This is not a JDWP METHOD_EXIT_WITH_RETURN_VALUE request, |
|
1118 |
* so signal this by setting typeKey = 0 which is not |
|
1119 |
* a legal typekey. |
|
1120 |
*/ |
|
1121 |
frameCommand->typeKey = 0; |
|
1122 |
} |
|
1123 |
} |
|
1124 |
||
1125 |
void |
|
1126 |
eventHelper_reportInvokeDone(jbyte sessionID, jthread thread) |
|
1127 |
{ |
|
1128 |
JNIEnv *env = getEnv(); |
|
1129 |
HelperCommand *command = jvmtiAllocate(sizeof(*command)); |
|
1130 |
if (command == NULL) { |
|
1131 |
EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"HelperCommand"); |
|
1132 |
} |
|
1133 |
(void)memset(command, 0, sizeof(*command)); |
|
1134 |
command->commandKind = COMMAND_REPORT_INVOKE_DONE; |
|
1135 |
command->sessionID = sessionID; |
|
1136 |
saveGlobalRef(env, thread, &(command->u.reportInvokeDone.thread)); |
|
1137 |
enqueueCommand(command, JNI_TRUE, JNI_FALSE); |
|
1138 |
} |
|
1139 |
||
1140 |
/* |
|
1141 |
* This, currently, cannot go through the normal event handling code |
|
1142 |
* because the JVMTI event does not contain a thread. |
|
1143 |
*/ |
|
1144 |
void |
|
1145 |
eventHelper_reportVMInit(JNIEnv *env, jbyte sessionID, jthread thread, jbyte suspendPolicy) |
|
1146 |
{ |
|
1147 |
HelperCommand *command = jvmtiAllocate(sizeof(*command)); |
|
1148 |
if (command == NULL) { |
|
1149 |
EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"HelperCommmand"); |
|
1150 |
} |
|
1151 |
(void)memset(command, 0, sizeof(*command)); |
|
1152 |
command->commandKind = COMMAND_REPORT_VM_INIT; |
|
1153 |
command->sessionID = sessionID; |
|
1154 |
saveGlobalRef(env, thread, &(command->u.reportVMInit.thread)); |
|
1155 |
command->u.reportVMInit.suspendPolicy = suspendPolicy; |
|
1156 |
enqueueCommand(command, JNI_TRUE, JNI_FALSE); |
|
1157 |
} |
|
1158 |
||
1159 |
void |
|
1160 |
eventHelper_suspendThread(jbyte sessionID, jthread thread) |
|
1161 |
{ |
|
1162 |
JNIEnv *env = getEnv(); |
|
1163 |
HelperCommand *command = jvmtiAllocate(sizeof(*command)); |
|
1164 |
if (command == NULL) { |
|
1165 |
EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"HelperCommmand"); |
|
1166 |
} |
|
1167 |
(void)memset(command, 0, sizeof(*command)); |
|
1168 |
command->commandKind = COMMAND_SUSPEND_THREAD; |
|
1169 |
command->sessionID = sessionID; |
|
1170 |
saveGlobalRef(env, thread, &(command->u.suspendThread.thread)); |
|
1171 |
enqueueCommand(command, JNI_TRUE, JNI_FALSE); |
|
1172 |
} |