J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1998-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 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 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | /* |
| 26 | * eventHandler |
| 27 | * |
| 28 | * This module handles events as they come in directly from JVMTI |
| 29 | * and also maps them to JDI events. JDI events are those requested |
| 30 | * at the JDI or JDWP level and seen on those levels. Mapping is |
| 31 | * one-to-many, a JVMTI event may map to several JDI events, or |
| 32 | * to none. Part of that mapping process is filteration, which |
| 33 | * eventFilter sub-module handles. A JDI EventRequest corresponds |
| 34 | * to a HandlerNode and a JDI filter to the hidden HandlerNode data |
| 35 | * used by eventFilter. For example, if at the JDI level the user |
| 36 | * executed: |
| 37 | * |
| 38 | * EventRequestManager erm = vm.eventRequestManager(); |
| 39 | * BreakpointRequest bp = erm.createBreakpointRequest(); |
| 40 | * bp.enable(); |
| 41 | * ClassPrepareRequest req = erm.createClassPrepareRequest(); |
| 42 | * req.enable(); |
| 43 | * req = erm.createClassPrepareRequest(); |
| 44 | * req.addClassFilter("Foo*"); |
| 45 | * req.enable(); |
| 46 | * |
| 47 | * Three handlers would be created, the first with a LocationOnly |
| 48 | * filter and the last with a ClassMatch filter. |
| 49 | * When a JVMTI class prepare event for "Foobar" |
| 50 | * comes in, the second handler will create one JDI event, the |
| 51 | * third handler will compare the class signature, and since |
| 52 | * it matchs create a second event. There may also be internal |
| 53 | * events as there are in this case, one created by the front-end |
| 54 | * and one by the back-end. |
| 55 | * |
| 56 | * Each event kind has a handler chain, which is a doublely linked |
| 57 | * list of handlers for that kind of event. |
| 58 | */ |
| 59 | #include "util.h" |
| 60 | #include "eventHandler.h" |
| 61 | #include "eventHandlerRestricted.h" |
| 62 | #include "eventFilter.h" |
| 63 | #include "eventFilterRestricted.h" |
| 64 | #include "standardHandlers.h" |
| 65 | #include "threadControl.h" |
| 66 | #include "eventHelper.h" |
| 67 | #include "classTrack.h" |
| 68 | #include "commonRef.h" |
| 69 | #include "debugLoop.h" |
| 70 | |
| 71 | static HandlerID requestIdCounter; |
| 72 | static jbyte currentSessionID; |
| 73 | |
| 74 | /* Counter of active callbacks and flag for vm_death */ |
| 75 | static int active_callbacks = 0; |
| 76 | static jboolean vm_death_callback_active = JNI_FALSE; |
| 77 | static jrawMonitorID callbackLock; |
| 78 | static jrawMonitorID callbackBlock; |
| 79 | |
| 80 | /* Macros to surround callback code (non-VM_DEATH callbacks). |
| 81 | * Note that this just keeps a count of the non-VM_DEATH callbacks that |
| 82 | * are currently active, it does not prevent these callbacks from |
| 83 | * operating in parallel. It's the VM_DEATH callback that will wait |
| 84 | * for all these callbacks to finish up, so that it can report the |
| 85 | * VM_DEATH in a clean state. |
| 86 | * If the VM_DEATH callback is active in the BEGIN macro then this |
| 87 | * callback just blocks until released by the VM_DEATH callback. |
| 88 | * If the VM_DEATH callback is active in the END macro, then this |
| 89 | * callback will notify the VM_DEATH callback if it's the last one, |
| 90 | * and then block until released by the VM_DEATH callback. |
| 91 | * Why block? These threads are often the threads of the Java program, |
| 92 | * not blocking might mean that a return would continue execution of |
| 93 | * some java thread in the middle of VM_DEATH, this seems troubled. |
| 94 | * |
| 95 | * WARNING: No not 'return' or 'goto' out of the BEGIN_CALLBACK/END_CALLBACK |
| 96 | * block, this will mess up the count. |
| 97 | */ |
| 98 | |
| 99 | #define BEGIN_CALLBACK() \ |
| 100 | { /* BEGIN OF CALLBACK */ \ |
| 101 | jboolean bypass = JNI_TRUE; \ |
| 102 | debugMonitorEnter(callbackLock); { \ |
| 103 | if (vm_death_callback_active) { \ |
| 104 | /* allow VM_DEATH callback to finish */ \ |
| 105 | debugMonitorExit(callbackLock); \ |
| 106 | /* Now block because VM is about to die */ \ |
| 107 | debugMonitorEnter(callbackBlock); \ |
| 108 | debugMonitorExit(callbackBlock); \ |
| 109 | } else { \ |
| 110 | active_callbacks++; \ |
| 111 | bypass = JNI_FALSE; \ |
| 112 | debugMonitorExit(callbackLock); \ |
| 113 | } \ |
| 114 | } \ |
| 115 | if ( !bypass ) { \ |
| 116 | /* BODY OF CALLBACK CODE */ |
| 117 | |
| 118 | #define END_CALLBACK() /* Part of bypass if body */ \ |
| 119 | debugMonitorEnter(callbackLock); { \ |
| 120 | active_callbacks--; \ |
| 121 | if (active_callbacks < 0) { \ |
| 122 | EXIT_ERROR(0, "Problems tracking active callbacks"); \ |
| 123 | } \ |
| 124 | if (vm_death_callback_active) { \ |
| 125 | if (active_callbacks == 0) { \ |
| 126 | debugMonitorNotifyAll(callbackLock); \ |
| 127 | } \ |
| 128 | /* allow VM_DEATH callback to finish */ \ |
| 129 | debugMonitorExit(callbackLock); \ |
| 130 | /* Now block because VM is about to die */ \ |
| 131 | debugMonitorEnter(callbackBlock); \ |
| 132 | debugMonitorExit(callbackBlock); \ |
| 133 | } else { \ |
| 134 | debugMonitorExit(callbackLock); \ |
| 135 | } \ |
| 136 | } \ |
| 137 | } \ |
| 138 | } /* END OF CALLBACK */ |
| 139 | |
| 140 | /* |
| 141 | * We are starting with a very simple locking scheme |
| 142 | * for event handling. All readers and writers of data in |
| 143 | * the handlers[] chain must own this lock for the duration |
| 144 | * of its use. If contention becomes a problem, we can: |
| 145 | * |
| 146 | * 1) create a lock per event type. |
| 147 | * 2) move to a readers/writers approach where multiple threads |
| 148 | * can access the chains simultaneously while reading (the |
| 149 | * normal activity of an event callback). |
| 150 | */ |
| 151 | static jrawMonitorID handlerLock; |
| 152 | |
| 153 | typedef struct HandlerChain_ { |
| 154 | HandlerNode *first; |
| 155 | /* add lock here */ |
| 156 | } HandlerChain; |
| 157 | |
| 158 | /* |
| 159 | * This array maps event kinds to handler chains. |
| 160 | * Protected by handlerLock. |
| 161 | */ |
| 162 | |
| 163 | static HandlerChain __handlers[EI_max-EI_min+1]; |
| 164 | |
| 165 | /* Given a HandlerNode, these access our private data. |
| 166 | */ |
| 167 | #define PRIVATE_DATA(node) \ |
| 168 | (&(((EventHandlerRestricted_HandlerNode*)(void*)(node))->private_ehpd)) |
| 169 | |
| 170 | #define NEXT(node) (PRIVATE_DATA(node)->private_next) |
| 171 | #define PREV(node) (PRIVATE_DATA(node)->private_prev) |
| 172 | #define CHAIN(node) (PRIVATE_DATA(node)->private_chain) |
| 173 | #define HANDLER_FUNCTION(node) (PRIVATE_DATA(node)->private_handlerFunction) |
| 174 | |
| 175 | static jclass getObjectClass(jobject object); |
| 176 | static jvmtiError freeHandler(HandlerNode *node); |
| 177 | |
| 178 | static jvmtiError freeHandlerChain(HandlerChain *chain); |
| 179 | |
| 180 | static HandlerChain * |
| 181 | getHandlerChain(EventIndex i) |
| 182 | { |
| 183 | if ( i < EI_min || i > EI_max ) { |
| 184 | EXIT_ERROR(AGENT_ERROR_INVALID_EVENT_TYPE,"bad index for handler"); |
| 185 | } |
| 186 | return &(__handlers[i-EI_min]); |
| 187 | } |
| 188 | |
| 189 | static void |
| 190 | insert(HandlerChain *chain, HandlerNode *node) |
| 191 | { |
| 192 | HandlerNode *oldHead = chain->first; |
| 193 | NEXT(node) = oldHead; |
| 194 | PREV(node) = NULL; |
| 195 | CHAIN(node) = chain; |
| 196 | if (oldHead != NULL) { |
| 197 | PREV(oldHead) = node; |
| 198 | } |
| 199 | chain->first = node; |
| 200 | } |
| 201 | |
| 202 | static HandlerNode * |
| 203 | findInChain(HandlerChain *chain, HandlerID handlerID) |
| 204 | { |
| 205 | HandlerNode *node = chain->first; |
| 206 | while (node != NULL) { |
| 207 | if (node->handlerID == handlerID) { |
| 208 | return node; |
| 209 | } |
| 210 | node = NEXT(node); |
| 211 | } |
| 212 | return NULL; |
| 213 | } |
| 214 | |
| 215 | static HandlerNode * |
| 216 | find(EventIndex ei, HandlerID handlerID) |
| 217 | { |
| 218 | return findInChain(getHandlerChain(ei), handlerID); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Deinsert. Safe for non-inserted nodes. |
| 223 | */ |
| 224 | static void |
| 225 | deinsert(HandlerNode *node) |
| 226 | { |
| 227 | HandlerChain *chain = CHAIN(node); |
| 228 | |
| 229 | if (chain == NULL) { |
| 230 | return; |
| 231 | } |
| 232 | if (chain->first == node) { |
| 233 | chain->first = NEXT(node); |
| 234 | } |
| 235 | if (NEXT(node) != NULL) { |
| 236 | PREV(NEXT(node)) = PREV(node); |
| 237 | } |
| 238 | if (PREV(node) != NULL) { |
| 239 | NEXT(PREV(node)) = NEXT(node); |
| 240 | } |
| 241 | CHAIN(node) = NULL; |
| 242 | } |
| 243 | |
| 244 | jboolean |
| 245 | eventHandlerRestricted_iterator(EventIndex ei, |
| 246 | IteratorFunction func, void *arg) |
| 247 | { |
| 248 | HandlerChain *chain; |
| 249 | HandlerNode *node; |
| 250 | JNIEnv *env; |
| 251 | |
| 252 | chain = getHandlerChain(ei); |
| 253 | node = chain->first; |
| 254 | env = getEnv(); |
| 255 | |
| 256 | if ( func == NULL ) { |
| 257 | EXIT_ERROR(AGENT_ERROR_INTERNAL,"iterator function NULL"); |
| 258 | } |
| 259 | |
| 260 | while (node != NULL) { |
| 261 | if (((func)(env, node, arg))) { |
| 262 | return JNI_TRUE; |
| 263 | } |
| 264 | node = NEXT(node); |
| 265 | } |
| 266 | return JNI_FALSE; |
| 267 | } |
| 268 | |
| 269 | /* BREAKPOINT, METHOD_ENTRY and SINGLE_STEP events are covered by |
| 270 | * the co-location of events policy. Of these three co-located |
| 271 | * events, METHOD_ENTRY is always reported first and BREAKPOINT |
| 272 | * is always reported last. Here are the possible combinations and |
| 273 | * their order: |
| 274 | * |
| 275 | * (p1) METHOD_ENTRY, BREAKPOINT (existing) |
| 276 | * (p2) METHOD_ENTRY, BREAKPOINT (new) |
| 277 | * (p1) METHOD_ENTRY, SINGLE_STEP |
| 278 | * (p1) METHOD_ENTRY, SINGLE_STEP, BREAKPOINT (existing) |
| 279 | * (p1/p2) METHOD_ENTRY, SINGLE_STEP, BREAKPOINT (new) |
| 280 | * (p1) SINGLE_STEP, BREAKPOINT (existing) |
| 281 | * (p2) SINGLE_STEP, BREAKPOINT (new) |
| 282 | * |
| 283 | * BREAKPOINT (existing) indicates a BREAKPOINT that is set before |
| 284 | * the other co-located event is posted. BREAKPOINT (new) indicates |
| 285 | * a BREAKPOINT that is set after the other co-located event is |
| 286 | * posted and before the thread has resumed execution. |
| 287 | * |
| 288 | * Co-location of events policy used to be implemented via |
| 289 | * temporary BREAKPOINTs along with deferring the reporting of |
| 290 | * non-BREAKPOINT co-located events, but the temporary BREAKPOINTs |
| 291 | * caused performance problems on VMs where setting or clearing |
| 292 | * BREAKPOINTs is expensive, e.g., HotSpot. |
| 293 | * |
| 294 | * The policy is now implemented in two phases. Phase 1: when a |
| 295 | * METHOD_ENTRY or SINGLE_STEP event is received, if there is an |
| 296 | * existing co-located BREAKPOINT, then the current event is |
| 297 | * deferred. When the BREAKPOINT event is processed, the event |
| 298 | * bag will contain the deferred METHOD_ENTRY and/or SINGLE_STEP |
| 299 | * events along with the BREAKPOINT event. For a METHOD_ENTRY |
| 300 | * event where there is not an existing co-located BREAKPOINT, |
| 301 | * if SINGLE_STEP events are also enabled for the thread, then |
| 302 | * the METHOD_ENTRY event is deferred. When the SINGLE_STEP event |
| 303 | * is processed, the event bag will also contain the deferred |
| 304 | * METHOD_ENTRY event. This covers each of the combinations |
| 305 | * marked with 'p1' above. |
| 306 | * |
| 307 | * Phase 2: if there is no existing co-located BREAKPOINT, then the |
| 308 | * location information for the METHOD_ENTRY or SINGLE_STEP event |
| 309 | * is recorded in the ThreadNode. If the next event for the thread |
| 310 | * is a co-located BREAKPOINT, then the first BREAKPOINT event will |
| 311 | * be skipped since it cannot be delivered in the same event set. |
| 312 | * This covers each of the combinations marked with 'p2' above. |
| 313 | * |
| 314 | * For the combination marked p1/p2, part of the case is handled |
| 315 | * during phase 1 and the rest is handled during phase 2. |
| 316 | * |
| 317 | * The recording of information in the ThreadNode is handled in |
| 318 | * this routine. The special handling of the next event for the |
| 319 | * thread is handled in skipEventReport(). |
| 320 | */ |
| 321 | |
| 322 | static jboolean |
| 323 | deferEventReport(JNIEnv *env, jthread thread, |
| 324 | EventIndex ei, jclass clazz, jmethodID method, jlocation location) |
| 325 | { |
| 326 | jboolean deferring = JNI_FALSE; |
| 327 | |
| 328 | switch (ei) { |
| 329 | case EI_METHOD_ENTRY: |
| 330 | if (!isMethodNative(method)) { |
| 331 | jvmtiError error; |
| 332 | jlocation start; |
| 333 | jlocation end; |
| 334 | error = methodLocation(method, &start, &end); |
| 335 | if (error == JVMTI_ERROR_NONE) { |
| 336 | deferring = isBreakpointSet(clazz, method, start) || |
| 337 | threadControl_getInstructionStepMode(thread) |
| 338 | == JVMTI_ENABLE; |
| 339 | if (!deferring) { |
| 340 | threadControl_saveCLEInfo(env, thread, ei, |
| 341 | clazz, method, start); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | break; |
| 346 | case EI_SINGLE_STEP: |
| 347 | deferring = isBreakpointSet(clazz, method, location); |
| 348 | if (!deferring) { |
| 349 | threadControl_saveCLEInfo(env, thread, ei, |
| 350 | clazz, method, location); |
| 351 | } |
| 352 | break; |
| 353 | default: |
| 354 | break; |
| 355 | } |
| 356 | /* TO DO: Once JVMTI supports a way to know if we're |
| 357 | * at the end of a method, we should check here for |
| 358 | * break and step events which precede a method exit |
| 359 | * event. |
| 360 | */ |
| 361 | return deferring; |
| 362 | } |
| 363 | |
| 364 | /* Handle phase 2 of the co-located events policy. See detailed |
| 365 | * comments in deferEventReport() above. |
| 366 | */ |
| 367 | static jboolean |
| 368 | skipEventReport(JNIEnv *env, jthread thread, EventIndex ei, |
| 369 | jclass clazz, jmethodID method, jlocation location) |
| 370 | { |
| 371 | jboolean skipping = JNI_FALSE; |
| 372 | |
| 373 | if (ei == EI_BREAKPOINT) { |
| 374 | if (threadControl_cmpCLEInfo(env, thread, clazz, method, location)) { |
| 375 | LOG_MISC(("Co-located breakpoint event found: " |
| 376 | "%s,thread=%p,clazz=%p,method=%p,location=%d", |
| 377 | eventText(ei), thread, clazz, method, location)); |
| 378 | skipping = JNI_TRUE; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | threadControl_clearCLEInfo(env, thread); |
| 383 | |
| 384 | return skipping; |
| 385 | } |
| 386 | |
| 387 | static void |
| 388 | reportEvents(JNIEnv *env, jbyte sessionID, jthread thread, EventIndex ei, |
| 389 | jclass clazz, jmethodID method, jlocation location, |
| 390 | struct bag *eventBag) |
| 391 | { |
| 392 | jbyte suspendPolicy; |
| 393 | jboolean invoking; |
| 394 | |
| 395 | if (bagSize(eventBag) < 1) { |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Never report events before initialization completes |
| 401 | */ |
| 402 | if (!debugInit_isInitComplete()) { |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * Check to see if we should skip reporting this event due to |
| 408 | * co-location of events policy. |
| 409 | */ |
| 410 | if (thread != NULL && |
| 411 | skipEventReport(env, thread, ei, clazz, method, location)) { |
| 412 | LOG_MISC(("event report being skipped: " |
| 413 | "ei=%s,thread=%p,clazz=%p,method=%p,location=%d", |
| 414 | eventText(ei), thread, clazz, method, location)); |
| 415 | bagDeleteAll(eventBag); |
| 416 | return; |
| 417 | } |
| 418 | |
| 419 | /* We delay the reporting of some events so that they can be |
| 420 | * properly grouped into event sets with upcoming events. If |
| 421 | * the reporting is to be deferred, the event commands remain |
| 422 | * in the event bag until a subsequent event occurs. Event is |
| 423 | * NULL for synthetic events (e.g. unload). |
| 424 | */ |
| 425 | if (thread == NULL |
| 426 | || !deferEventReport(env, thread, ei, |
| 427 | clazz, method, location)) { |
| 428 | struct bag *completedBag = bagDup(eventBag); |
| 429 | bagDeleteAll(eventBag); |
| 430 | if (completedBag == NULL) { |
| 431 | /* |
| 432 | * TO DO: Report, but don't terminate? |
| 433 | */ |
| 434 | return; |
| 435 | } else { |
| 436 | suspendPolicy = eventHelper_reportEvents(sessionID, completedBag); |
| 437 | if (thread != NULL && suspendPolicy != JDWP_SUSPEND_POLICY(NONE)) { |
| 438 | do { |
| 439 | /* The events have been reported and this |
| 440 | * thread is about to continue, but it may |
| 441 | * have been started up up just to perform a |
| 442 | * requested method invocation. If so, we do |
| 443 | * the invoke now and then stop again waiting |
| 444 | * for another continue. By then another |
| 445 | * invoke request can be in place, so there is |
| 446 | * a loop around this code. |
| 447 | */ |
| 448 | invoking = invoker_doInvoke(thread); |
| 449 | if (invoking) { |
| 450 | eventHelper_reportInvokeDone(sessionID, thread); |
| 451 | } |
| 452 | } while (invoking); |
| 453 | } |
| 454 | bagDestroyBag(completedBag); |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | /* A bagEnumerateFunction. Create a synthetic class unload event |
| 460 | * for every class no longer present. Analogous to event_callback |
| 461 | * combined with a handler in a unload specific (no event |
| 462 | * structure) kind of way. |
| 463 | */ |
| 464 | static jboolean |
| 465 | synthesizeUnloadEvent(void *signatureVoid, void *envVoid) |
| 466 | { |
| 467 | JNIEnv *env = (JNIEnv *)envVoid; |
| 468 | char *signature = *(char **)signatureVoid; |
| 469 | char *classname; |
| 470 | HandlerNode *node; |
| 471 | jbyte eventSessionID = currentSessionID; |
| 472 | struct bag *eventBag = eventHelper_createEventBag(); |
| 473 | |
| 474 | if (eventBag == NULL) { |
| 475 | /* TO DO: Report, but don't die |
| 476 | */ |
| 477 | JDI_ASSERT(eventBag != NULL); |
| 478 | } |
| 479 | |
| 480 | /* Signature needs to last, so convert extra copy to |
| 481 | * classname |
| 482 | */ |
| 483 | classname = jvmtiAllocate((int)strlen(signature)+1); |
| 484 | (void)strcpy(classname, signature); |
| 485 | convertSignatureToClassname(classname); |
| 486 | |
| 487 | debugMonitorEnter(handlerLock); |
| 488 | |
| 489 | node = getHandlerChain(EI_GC_FINISH)->first; |
| 490 | while (node != NULL) { |
| 491 | /* save next so handlers can remove themselves */ |
| 492 | HandlerNode *next = NEXT(node); |
| 493 | jboolean shouldDelete; |
| 494 | |
| 495 | if (eventFilterRestricted_passesUnloadFilter(env, classname, |
| 496 | node, |
| 497 | &shouldDelete)) { |
| 498 | /* There may be multiple handlers, the signature will |
| 499 | * be freed when the event helper thread has written |
| 500 | * it. So each event needs a separate allocation. |
| 501 | */ |
| 502 | char *durableSignature = jvmtiAllocate((int)strlen(signature)+1); |
| 503 | (void)strcpy(durableSignature, signature); |
| 504 | |
| 505 | eventHelper_recordClassUnload(node->handlerID, |
| 506 | durableSignature, |
| 507 | eventBag); |
| 508 | } |
| 509 | if (shouldDelete) { |
| 510 | /* We can safely free the node now that we are done |
| 511 | * using it. |
| 512 | */ |
| 513 | (void)freeHandler(node); |
| 514 | } |
| 515 | node = next; |
| 516 | } |
| 517 | |
| 518 | debugMonitorExit(handlerLock); |
| 519 | |
| 520 | if (eventBag != NULL) { |
| 521 | reportEvents(env, eventSessionID, (jthread)NULL, 0, |
| 522 | (jclass)NULL, (jmethodID)NULL, 0, eventBag); |
| 523 | |
| 524 | /* |
| 525 | * bag was created locally, destroy it here. |
| 526 | */ |
| 527 | bagDestroyBag(eventBag); |
| 528 | } |
| 529 | |
| 530 | jvmtiDeallocate(signature); |
| 531 | jvmtiDeallocate(classname); |
| 532 | |
| 533 | return JNI_TRUE; |
| 534 | } |
| 535 | |
| 536 | /* Garbage Collection Happened */ |
| 537 | static unsigned int garbageCollected = 0; |
| 538 | |
| 539 | /* The JVMTI generic event callback. Each event is passed to a sequence of |
| 540 | * handlers in a chain until the chain ends or one handler |
| 541 | * consumes the event. |
| 542 | */ |
| 543 | static void |
| 544 | event_callback(JNIEnv *env, EventInfo *evinfo) |
| 545 | { |
| 546 | struct bag *eventBag; |
| 547 | jbyte eventSessionID = currentSessionID; /* session could change */ |
| 548 | jthrowable currentException; |
| 549 | jthread thread; |
| 550 | |
| 551 | LOG_MISC(("event_callback(): ei=%s", eventText(evinfo->ei))); |
| 552 | log_debugee_location("event_callback()", evinfo->thread, evinfo->method, evinfo->location); |
| 553 | |
| 554 | /* We want to preserve any current exception that might get |
| 555 | * wiped out during event handling (e.g. JNI calls). We have |
| 556 | * to rely on space for the local reference on the current |
| 557 | * frame because doing a PushLocalFrame here might itself |
| 558 | * generate an exception. |
| 559 | */ |
| 560 | currentException = JNI_FUNC_PTR(env,ExceptionOccurred)(env); |
| 561 | JNI_FUNC_PTR(env,ExceptionClear)(env); |
| 562 | |
| 563 | /* See if a garbage collection finish event happened earlier. |
| 564 | * |
| 565 | * Note: The "if" is an optimization to avoid entering the lock on every |
| 566 | * event; garbageCollected may be zapped before we enter |
| 567 | * the lock but then this just becomes one big no-op. |
| 568 | */ |
| 569 | if ( garbageCollected > 0 ) { |
| 570 | struct bag *unloadedSignatures = NULL; |
| 571 | |
| 572 | /* We want to compact the hash table of all |
| 573 | * objects sent to the front end by removing objects that have |
| 574 | * been collected. |
| 575 | */ |
| 576 | commonRef_compact(); |
| 577 | |
| 578 | /* We also need to simulate the class unload events. */ |
| 579 | |
| 580 | debugMonitorEnter(handlerLock); |
| 581 | |
| 582 | /* Clear garbage collection counter */ |
| 583 | garbageCollected = 0; |
| 584 | |
| 585 | /* Analyze which class unloads occurred */ |
| 586 | unloadedSignatures = classTrack_processUnloads(env); |
| 587 | |
| 588 | debugMonitorExit(handlerLock); |
| 589 | |
| 590 | /* Generate the synthetic class unload events and/or just cleanup. */ |
| 591 | if ( unloadedSignatures != NULL ) { |
| 592 | (void)bagEnumerateOver(unloadedSignatures, synthesizeUnloadEvent, |
| 593 | (void *)env); |
| 594 | bagDestroyBag(unloadedSignatures); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | thread = evinfo->thread; |
| 599 | if (thread != NULL) { |
| 600 | /* |
| 601 | * Record the fact that we're entering an event |
| 602 | * handler so that thread operations (status, interrupt, |
| 603 | * stop) can be done correctly and so that thread |
| 604 | * resources can be allocated. This must be done before |
| 605 | * grabbing any locks. |
| 606 | */ |
| 607 | eventBag = threadControl_onEventHandlerEntry(eventSessionID, |
| 608 | evinfo->ei, thread, currentException); |
| 609 | if ( eventBag == NULL ) { |
| 610 | jboolean invoking; |
| 611 | do { |
| 612 | /* The event has been 'handled' and this |
| 613 | * thread is about to continue, but it may |
| 614 | * have been started up just to perform a |
| 615 | * requested method invocation. If so, we do |
| 616 | * the invoke now and then stop again waiting |
| 617 | * for another continue. By then another |
| 618 | * invoke request can be in place, so there is |
| 619 | * a loop around this code. |
| 620 | */ |
| 621 | invoking = invoker_doInvoke(thread); |
| 622 | if (invoking) { |
| 623 | eventHelper_reportInvokeDone(eventSessionID, thread); |
| 624 | } |
| 625 | } while (invoking); |
| 626 | return; /* Do nothing, event was consumed */ |
| 627 | } |
| 628 | } else { |
| 629 | eventBag = eventHelper_createEventBag(); |
| 630 | if (eventBag == NULL) { |
| 631 | /* |
| 632 | * TO DO: Report, but don't die |
| 633 | */ |
| 634 | eventBag = NULL; /* to shut up lint */ |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | debugMonitorEnter(handlerLock); |
| 639 | { |
| 640 | HandlerNode *node; |
| 641 | char *classname; |
| 642 | |
| 643 | /* We must keep track of all classes prepared to know what's unloaded */ |
| 644 | if (evinfo->ei == EI_CLASS_PREPARE) { |
| 645 | classTrack_addPreparedClass(env, evinfo->clazz); |
| 646 | } |
| 647 | |
| 648 | node = getHandlerChain(evinfo->ei)->first; |
| 649 | classname = getClassname(evinfo->clazz); |
| 650 | |
| 651 | while (node != NULL) { |
| 652 | /* save next so handlers can remove themselves */ |
| 653 | HandlerNode *next = NEXT(node); |
| 654 | jboolean shouldDelete; |
| 655 | |
| 656 | if (eventFilterRestricted_passesFilter(env, classname, |
| 657 | evinfo, node, |
| 658 | &shouldDelete)) { |
| 659 | HandlerFunction func; |
| 660 | |
| 661 | func = HANDLER_FUNCTION(node); |
| 662 | if ( func == NULL ) { |
| 663 | EXIT_ERROR(AGENT_ERROR_INTERNAL,"handler function NULL"); |
| 664 | } |
| 665 | (*func)(env, evinfo, node, eventBag); |
| 666 | } |
| 667 | if (shouldDelete) { |
| 668 | /* We can safely free the node now that we are done |
| 669 | * using it. |
| 670 | */ |
| 671 | (void)freeHandler(node); |
| 672 | } |
| 673 | node = next; |
| 674 | } |
| 675 | jvmtiDeallocate(classname); |
| 676 | } |
| 677 | debugMonitorExit(handlerLock); |
| 678 | |
| 679 | if (eventBag != NULL) { |
| 680 | reportEvents(env, eventSessionID, thread, evinfo->ei, |
| 681 | evinfo->clazz, evinfo->method, evinfo->location, eventBag); |
| 682 | } |
| 683 | |
| 684 | /* we are continuing after VMDeathEvent - now we are dead */ |
| 685 | if (evinfo->ei == EI_VM_DEATH) { |
| 686 | gdata->vmDead = JNI_TRUE; |
| 687 | } |
| 688 | |
| 689 | /* |
| 690 | * If the bag was created locally, destroy it here. |
| 691 | */ |
| 692 | if (thread == NULL) { |
| 693 | bagDestroyBag(eventBag); |
| 694 | } |
| 695 | |
| 696 | /* Always restore any exception that was set beforehand. If |
| 697 | * there is a pending async exception, StopThread will be |
| 698 | * called from threadControl_onEventHandlerExit immediately |
| 699 | * below. Depending on VM implementation and state, the async |
| 700 | * exception might immediately overwrite the currentException, |
| 701 | * or it might be delayed until later. */ |
| 702 | if (currentException != NULL) { |
| 703 | JNI_FUNC_PTR(env,Throw)(env, currentException); |
| 704 | } else { |
| 705 | JNI_FUNC_PTR(env,ExceptionClear)(env); |
| 706 | } |
| 707 | |
| 708 | /* |
| 709 | * Release thread resources and perform any delayed operations. |
| 710 | */ |
| 711 | if (thread != NULL) { |
| 712 | threadControl_onEventHandlerExit(evinfo->ei, thread, eventBag); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | /* Returns a local ref to the declaring class for an object. */ |
| 717 | static jclass |
| 718 | getObjectClass(jobject object) |
| 719 | { |
| 720 | jclass clazz; |
| 721 | JNIEnv *env = getEnv(); |
| 722 | |
| 723 | clazz = JNI_FUNC_PTR(env,GetObjectClass)(env, object); |
| 724 | |
| 725 | return clazz; |
| 726 | } |
| 727 | |
| 728 | /* Returns a local ref to the declaring class for a method, or NULL. */ |
| 729 | jclass |
| 730 | getMethodClass(jvmtiEnv *jvmti_env, jmethodID method) |
| 731 | { |
| 732 | jclass clazz = NULL; |
| 733 | jvmtiError error; |
| 734 | |
| 735 | if ( method == NULL ) { |
| 736 | return NULL; |
| 737 | } |
| 738 | error = methodClass(method, &clazz); |
| 739 | if ( error != JVMTI_ERROR_NONE ) { |
| 740 | EXIT_ERROR(error,"Can't get jclass for a methodID, invalid?"); |
| 741 | return NULL; |
| 742 | } |
| 743 | return clazz; |
| 744 | } |
| 745 | |
| 746 | /* Event callback for JVMTI_EVENT_SINGLE_STEP */ |
| 747 | static void JNICALL |
| 748 | cbSingleStep(jvmtiEnv *jvmti_env, JNIEnv *env, |
| 749 | jthread thread, jmethodID method, jlocation location) |
| 750 | { |
| 751 | EventInfo info; |
| 752 | |
| 753 | LOG_CB(("cbSingleStep: thread=%p", thread)); |
| 754 | |
| 755 | BEGIN_CALLBACK() { |
| 756 | (void)memset(&info,0,sizeof(info)); |
| 757 | info.ei = EI_SINGLE_STEP; |
| 758 | info.thread = thread; |
| 759 | info.clazz = getMethodClass(jvmti_env, method); |
| 760 | info.method = method; |
| 761 | info.location = location; |
| 762 | event_callback(env, &info); |
| 763 | } END_CALLBACK(); |
| 764 | |
| 765 | LOG_MISC(("END cbSingleStep")); |
| 766 | } |
| 767 | |
| 768 | /* Event callback for JVMTI_EVENT_BREAKPOINT */ |
| 769 | static void JNICALL |
| 770 | cbBreakpoint(jvmtiEnv *jvmti_env, JNIEnv *env, |
| 771 | jthread thread, jmethodID method, jlocation location) |
| 772 | { |
| 773 | EventInfo info; |
| 774 | |
| 775 | LOG_CB(("cbBreakpoint: thread=%p", thread)); |
| 776 | |
| 777 | BEGIN_CALLBACK() { |
| 778 | (void)memset(&info,0,sizeof(info)); |
| 779 | info.ei = EI_BREAKPOINT; |
| 780 | info.thread = thread; |
| 781 | info.clazz = getMethodClass(jvmti_env, method); |
| 782 | info.method = method; |
| 783 | info.location = location; |
| 784 | event_callback(env, &info); |
| 785 | } END_CALLBACK(); |
| 786 | |
| 787 | LOG_MISC(("END cbBreakpoint")); |
| 788 | } |
| 789 | |
| 790 | /* Event callback for JVMTI_EVENT_FRAME_POP */ |
| 791 | static void JNICALL |
| 792 | cbFramePop(jvmtiEnv *jvmti_env, JNIEnv *env, |
| 793 | jthread thread, jmethodID method, |
| 794 | jboolean wasPoppedByException) |
| 795 | { |
| 796 | EventInfo info; |
| 797 | |
| 798 | /* JDWP does not return these events when popped due to an exception. */ |
| 799 | if ( wasPoppedByException ) { |
| 800 | return; |
| 801 | } |
| 802 | |
| 803 | LOG_CB(("cbFramePop: thread=%p", thread)); |
| 804 | |
| 805 | BEGIN_CALLBACK() { |
| 806 | (void)memset(&info,0,sizeof(info)); |
| 807 | info.ei = EI_FRAME_POP; |
| 808 | info.thread = thread; |
| 809 | info.clazz = getMethodClass(jvmti_env, method); |
| 810 | info.method = method; |
| 811 | event_callback(env, &info); |
| 812 | } END_CALLBACK(); |
| 813 | |
| 814 | LOG_MISC(("END cbFramePop")); |
| 815 | } |
| 816 | |
| 817 | /* Event callback for JVMTI_EVENT_EXCEPTION */ |
| 818 | static void JNICALL |
| 819 | cbException(jvmtiEnv *jvmti_env, JNIEnv *env, |
| 820 | jthread thread, jmethodID method, |
| 821 | jlocation location, jobject exception, |
| 822 | jmethodID catch_method, jlocation catch_location) |
| 823 | { |
| 824 | EventInfo info; |
| 825 | |
| 826 | LOG_CB(("cbException: thread=%p", thread)); |
| 827 | |
| 828 | BEGIN_CALLBACK() { |
| 829 | (void)memset(&info,0,sizeof(info)); |
| 830 | info.ei = EI_EXCEPTION; |
| 831 | info.thread = thread; |
| 832 | info.clazz = getMethodClass(jvmti_env, method); |
| 833 | info.method = method; |
| 834 | info.location = location; |
| 835 | info.object = exception; |
| 836 | info.u.exception.catch_clazz = getMethodClass(jvmti_env, catch_method); |
| 837 | info.u.exception.catch_method = catch_method; |
| 838 | info.u.exception.catch_location = catch_location; |
| 839 | event_callback(env, &info); |
| 840 | } END_CALLBACK(); |
| 841 | |
| 842 | LOG_MISC(("END cbException")); |
| 843 | } |
| 844 | |
| 845 | /* Event callback for JVMTI_EVENT_THREAD_START */ |
| 846 | static void JNICALL |
| 847 | cbThreadStart(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread) |
| 848 | { |
| 849 | EventInfo info; |
| 850 | |
| 851 | LOG_CB(("cbThreadStart: thread=%p", thread)); |
| 852 | |
| 853 | BEGIN_CALLBACK() { |
| 854 | (void)memset(&info,0,sizeof(info)); |
| 855 | info.ei = EI_THREAD_START; |
| 856 | info.thread = thread; |
| 857 | event_callback(env, &info); |
| 858 | } END_CALLBACK(); |
| 859 | |
| 860 | LOG_MISC(("END cbThreadStart")); |
| 861 | } |
| 862 | |
| 863 | /* Event callback for JVMTI_EVENT_THREAD_END */ |
| 864 | static void JNICALL |
| 865 | cbThreadEnd(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread) |
| 866 | { |
| 867 | EventInfo info; |
| 868 | |
| 869 | LOG_CB(("cbThreadEnd: thread=%p", thread)); |
| 870 | |
| 871 | BEGIN_CALLBACK() { |
| 872 | (void)memset(&info,0,sizeof(info)); |
| 873 | info.ei = EI_THREAD_END; |
| 874 | info.thread = thread; |
| 875 | event_callback(env, &info); |
| 876 | } END_CALLBACK(); |
| 877 | |
| 878 | LOG_MISC(("END cbThreadEnd")); |
| 879 | } |
| 880 | |
| 881 | /* Event callback for JVMTI_EVENT_CLASS_PREPARE */ |
| 882 | static void JNICALL |
| 883 | cbClassPrepare(jvmtiEnv *jvmti_env, JNIEnv *env, |
| 884 | jthread thread, jclass klass) |
| 885 | { |
| 886 | EventInfo info; |
| 887 | |
| 888 | LOG_CB(("cbClassPrepare: thread=%p", thread)); |
| 889 | |
| 890 | BEGIN_CALLBACK() { |
| 891 | (void)memset(&info,0,sizeof(info)); |
| 892 | info.ei = EI_CLASS_PREPARE; |
| 893 | info.thread = thread; |
| 894 | info.clazz = klass; |
| 895 | event_callback(env, &info); |
| 896 | } END_CALLBACK(); |
| 897 | |
| 898 | LOG_MISC(("END cbClassPrepare")); |
| 899 | } |
| 900 | |
| 901 | /* Event callback for JVMTI_EVENT_GARBAGE_COLLECTION_FINISH */ |
| 902 | static void JNICALL |
| 903 | cbGarbageCollectionFinish(jvmtiEnv *jvmti_env) |
| 904 | { |
| 905 | LOG_CB(("cbGarbageCollectionFinish")); |
| 906 | ++garbageCollected; |
| 907 | LOG_MISC(("END cbGarbageCollectionFinish")); |
| 908 | } |
| 909 | |
| 910 | /* Event callback for JVMTI_EVENT_CLASS_LOAD */ |
| 911 | static void JNICALL |
| 912 | cbClassLoad(jvmtiEnv *jvmti_env, JNIEnv *env, |
| 913 | jthread thread, jclass klass) |
| 914 | { |
| 915 | EventInfo info; |
| 916 | |
| 917 | LOG_CB(("cbClassLoad: thread=%p", thread)); |
| 918 | |
| 919 | BEGIN_CALLBACK() { |
| 920 | (void)memset(&info,0,sizeof(info)); |
| 921 | info.ei = EI_CLASS_LOAD; |
| 922 | info.thread = thread; |
| 923 | info.clazz = klass; |
| 924 | event_callback(env, &info); |
| 925 | } END_CALLBACK(); |
| 926 | |
| 927 | LOG_MISC(("END cbClassLoad")); |
| 928 | } |
| 929 | |
| 930 | /* Event callback for JVMTI_EVENT_FIELD_ACCESS */ |
| 931 | static void JNICALL |
| 932 | cbFieldAccess(jvmtiEnv *jvmti_env, JNIEnv *env, |
| 933 | jthread thread, jmethodID method, |
| 934 | jlocation location, jclass field_klass, |
| 935 | jobject object, jfieldID field) |
| 936 | { |
| 937 | EventInfo info; |
| 938 | |
| 939 | LOG_CB(("cbFieldAccess: thread=%p", thread)); |
| 940 | |
| 941 | BEGIN_CALLBACK() { |
| 942 | (void)memset(&info,0,sizeof(info)); |
| 943 | info.ei = EI_FIELD_ACCESS; |
| 944 | info.thread = thread; |
| 945 | info.clazz = getMethodClass(jvmti_env, method); |
| 946 | info.method = method; |
| 947 | info.location = location; |
| 948 | info.u.field_access.field_clazz = field_klass; |
| 949 | info.object = object; |
| 950 | info.u.field_access.field = field; |
| 951 | event_callback(env, &info); |
| 952 | } END_CALLBACK(); |
| 953 | |
| 954 | LOG_MISC(("END cbFieldAccess")); |
| 955 | } |
| 956 | |
| 957 | /* Event callback for JVMTI_EVENT_FIELD_MODIFICATION */ |
| 958 | static void JNICALL |
| 959 | cbFieldModification(jvmtiEnv *jvmti_env, JNIEnv *env, |
| 960 | jthread thread, jmethodID method, |
| 961 | jlocation location, jclass field_klass, jobject object, jfieldID field, |
| 962 | char signature_type, jvalue new_value) |
| 963 | { |
| 964 | EventInfo info; |
| 965 | |
| 966 | LOG_CB(("cbFieldModification: thread=%p", thread)); |
| 967 | |
| 968 | BEGIN_CALLBACK() { |
| 969 | (void)memset(&info,0,sizeof(info)); |
| 970 | info.ei = EI_FIELD_MODIFICATION; |
| 971 | info.thread = thread; |
| 972 | info.clazz = getMethodClass(jvmti_env, method); |
| 973 | info.method = method; |
| 974 | info.location = location; |
| 975 | info.u.field_modification.field = field; |
| 976 | info.u.field_modification.field_clazz = field_klass; |
| 977 | info.object = object; |
| 978 | info.u.field_modification.signature_type= signature_type; |
| 979 | info.u.field_modification.new_value = new_value; |
| 980 | event_callback(env, &info); |
| 981 | } END_CALLBACK(); |
| 982 | |
| 983 | LOG_MISC(("END cbFieldModification")); |
| 984 | } |
| 985 | |
| 986 | /* Event callback for JVMTI_EVENT_EXCEPTION_CATCH */ |
| 987 | static void JNICALL |
| 988 | cbExceptionCatch(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread, |
| 989 | jmethodID method, jlocation location, jobject exception) |
| 990 | { |
| 991 | EventInfo info; |
| 992 | |
| 993 | LOG_CB(("cbExceptionCatch: thread=%p", thread)); |
| 994 | |
| 995 | BEGIN_CALLBACK() { |
| 996 | (void)memset(&info,0,sizeof(info)); |
| 997 | info.ei = EI_EXCEPTION_CATCH; |
| 998 | info.thread = thread; |
| 999 | info.clazz = getMethodClass(jvmti_env, method); |
| 1000 | info.method = method; |
| 1001 | info.location = location; |
| 1002 | info.object = exception; |
| 1003 | event_callback(env, &info); |
| 1004 | } END_CALLBACK(); |
| 1005 | |
| 1006 | LOG_MISC(("END cbExceptionCatch")); |
| 1007 | } |
| 1008 | |
| 1009 | /* Event callback for JVMTI_EVENT_METHOD_ENTRY */ |
| 1010 | static void JNICALL |
| 1011 | cbMethodEntry(jvmtiEnv *jvmti_env, JNIEnv *env, |
| 1012 | jthread thread, jmethodID method) |
| 1013 | { |
| 1014 | EventInfo info; |
| 1015 | |
| 1016 | LOG_CB(("cbMethodEntry: thread=%p", thread)); |
| 1017 | |
| 1018 | BEGIN_CALLBACK() { |
| 1019 | (void)memset(&info,0,sizeof(info)); |
| 1020 | info.ei = EI_METHOD_ENTRY; |
| 1021 | info.thread = thread; |
| 1022 | info.clazz = getMethodClass(jvmti_env, method); |
| 1023 | info.method = method; |
| 1024 | event_callback(env, &info); |
| 1025 | } END_CALLBACK(); |
| 1026 | |
| 1027 | LOG_MISC(("END cbMethodEntry")); |
| 1028 | } |
| 1029 | |
| 1030 | /* Event callback for JVMTI_EVENT_METHOD_EXIT */ |
| 1031 | static void JNICALL |
| 1032 | cbMethodExit(jvmtiEnv *jvmti_env, JNIEnv *env, |
| 1033 | jthread thread, jmethodID method, |
| 1034 | jboolean wasPoppedByException, jvalue return_value) |
| 1035 | { |
| 1036 | EventInfo info; |
| 1037 | |
| 1038 | /* JDWP does not return these events when popped due to an exception. */ |
| 1039 | if ( wasPoppedByException ) { |
| 1040 | return; |
| 1041 | } |
| 1042 | |
| 1043 | LOG_CB(("cbMethodExit: thread=%p", thread)); |
| 1044 | |
| 1045 | BEGIN_CALLBACK() { |
| 1046 | (void)memset(&info,0,sizeof(info)); |
| 1047 | info.ei = EI_METHOD_EXIT; |
| 1048 | info.thread = thread; |
| 1049 | info.clazz = getMethodClass(jvmti_env, method); |
| 1050 | info.method = method; |
| 1051 | info.u.method_exit.return_value = return_value; |
| 1052 | event_callback(env, &info); |
| 1053 | } END_CALLBACK(); |
| 1054 | |
| 1055 | LOG_MISC(("END cbMethodExit")); |
| 1056 | } |
| 1057 | |
| 1058 | /* Event callback for JVMTI_EVENT_MONITOR_CONTENDED_ENTER */ |
| 1059 | static void JNICALL |
| 1060 | cbMonitorContendedEnter(jvmtiEnv *jvmti_env, JNIEnv *env, |
| 1061 | jthread thread, jobject object) |
| 1062 | { |
| 1063 | EventInfo info; |
| 1064 | jvmtiError error; |
| 1065 | jmethodID method; |
| 1066 | jlocation location; |
| 1067 | |
| 1068 | LOG_CB(("cbMonitorContendedEnter: thread=%p", thread)); |
| 1069 | |
| 1070 | BEGIN_CALLBACK() { |
| 1071 | (void)memset(&info,0,sizeof(info)); |
| 1072 | info.ei = EI_MONITOR_CONTENDED_ENTER; |
| 1073 | info.thread = thread; |
| 1074 | info.object = object; |
| 1075 | /* get current location of contended monitor enter */ |
| 1076 | error = JVMTI_FUNC_PTR(gdata->jvmti,GetFrameLocation) |
| 1077 | (gdata->jvmti, thread, 0, &method, &location); |
| 1078 | if (error == JVMTI_ERROR_NONE) { |
| 1079 | info.location = location; |
| 1080 | info.method = method; |
| 1081 | info.clazz = getMethodClass(jvmti_env, method); |
| 1082 | } else { |
| 1083 | info.location = -1; |
| 1084 | } |
| 1085 | event_callback(env, &info); |
| 1086 | } END_CALLBACK(); |
| 1087 | |
| 1088 | LOG_MISC(("END cbMonitorContendedEnter")); |
| 1089 | } |
| 1090 | |
| 1091 | /* Event callback for JVMTI_EVENT_MONITOR_CONTENDED_ENTERED */ |
| 1092 | static void JNICALL |
| 1093 | cbMonitorContendedEntered(jvmtiEnv *jvmti_env, JNIEnv *env, |
| 1094 | jthread thread, jobject object) |
| 1095 | { |
| 1096 | EventInfo info; |
| 1097 | jvmtiError error; |
| 1098 | jmethodID method; |
| 1099 | jlocation location; |
| 1100 | |
| 1101 | LOG_CB(("cbMonitorContendedEntered: thread=%p", thread)); |
| 1102 | |
| 1103 | BEGIN_CALLBACK() { |
| 1104 | (void)memset(&info,0,sizeof(info)); |
| 1105 | info.ei = EI_MONITOR_CONTENDED_ENTERED; |
| 1106 | info.thread = thread; |
| 1107 | info.object = object; |
| 1108 | /* get current location of contended monitor enter */ |
| 1109 | error = JVMTI_FUNC_PTR(gdata->jvmti,GetFrameLocation) |
| 1110 | (gdata->jvmti, thread, 0, &method, &location); |
| 1111 | if (error == JVMTI_ERROR_NONE) { |
| 1112 | info.location = location; |
| 1113 | info.method = method; |
| 1114 | info.clazz = getMethodClass(jvmti_env, method); |
| 1115 | } else { |
| 1116 | info.location = -1; |
| 1117 | } |
| 1118 | event_callback(env, &info); |
| 1119 | } END_CALLBACK(); |
| 1120 | |
| 1121 | LOG_MISC(("END cbMonitorContendedEntered")); |
| 1122 | } |
| 1123 | |
| 1124 | /* Event callback for JVMTI_EVENT_MONITOR_WAIT */ |
| 1125 | static void JNICALL |
| 1126 | cbMonitorWait(jvmtiEnv *jvmti_env, JNIEnv *env, |
| 1127 | jthread thread, jobject object, |
| 1128 | jlong timeout) |
| 1129 | { |
| 1130 | EventInfo info; |
| 1131 | jvmtiError error; |
| 1132 | jmethodID method; |
| 1133 | jlocation location; |
| 1134 | |
| 1135 | LOG_CB(("cbMonitorWait: thread=%p", thread)); |
| 1136 | |
| 1137 | BEGIN_CALLBACK() { |
| 1138 | (void)memset(&info,0,sizeof(info)); |
| 1139 | info.ei = EI_MONITOR_WAIT; |
| 1140 | info.thread = thread; |
| 1141 | info.object = object; |
| 1142 | /* The info.clazz is used for both class filtering and for location info. |
| 1143 | * For monitor wait event the class filtering is done for class of monitor |
| 1144 | * object. So here info.clazz is set to class of monitor object here and it |
| 1145 | * is reset to class of method before writing location info. |
| 1146 | * See writeMonitorEvent in eventHelper.c |
| 1147 | */ |
| 1148 | info.clazz = getObjectClass(object); |
| 1149 | info.u.monitor.timeout = timeout; |
| 1150 | |
| 1151 | /* get location of monitor wait() method. */ |
| 1152 | error = JVMTI_FUNC_PTR(gdata->jvmti,GetFrameLocation) |
| 1153 | (gdata->jvmti, thread, 0, &method, &location); |
| 1154 | if (error == JVMTI_ERROR_NONE) { |
| 1155 | info.location = location; |
| 1156 | info.method = method; |
| 1157 | } else { |
| 1158 | info.location = -1; |
| 1159 | } |
| 1160 | event_callback(env, &info); |
| 1161 | } END_CALLBACK(); |
| 1162 | |
| 1163 | LOG_MISC(("END cbMonitorWait")); |
| 1164 | } |
| 1165 | |
| 1166 | /* Event callback for JVMTI_EVENT_MONITOR_WAIT */ |
| 1167 | static void JNICALL |
| 1168 | cbMonitorWaited(jvmtiEnv *jvmti_env, JNIEnv *env, |
| 1169 | jthread thread, jobject object, |
| 1170 | jboolean timed_out) |
| 1171 | { |
| 1172 | EventInfo info; |
| 1173 | jvmtiError error; |
| 1174 | jmethodID method; |
| 1175 | jlocation location; |
| 1176 | |
| 1177 | LOG_CB(("cbMonitorWaited: thread=%p", thread)); |
| 1178 | |
| 1179 | BEGIN_CALLBACK() { |
| 1180 | (void)memset(&info,0,sizeof(info)); |
| 1181 | info.ei = EI_MONITOR_WAITED; |
| 1182 | info.thread = thread; |
| 1183 | info.object = object; |
| 1184 | /* The info.clazz is used for both class filtering and for location info. |
| 1185 | * For monitor waited event the class filtering is done for class of monitor |
| 1186 | * object. So here info.clazz is set to class of monitor object here and it |
| 1187 | * is reset to class of method before writing location info. |
| 1188 | * See writeMonitorEvent in eventHelper.c |
| 1189 | */ |
| 1190 | info.clazz = getObjectClass(object); |
| 1191 | info.u.monitor.timed_out = timed_out; |
| 1192 | |
| 1193 | /* get location of monitor wait() method */ |
| 1194 | error = JVMTI_FUNC_PTR(gdata->jvmti,GetFrameLocation) |
| 1195 | (gdata->jvmti, thread, 0, &method, &location); |
| 1196 | if (error == JVMTI_ERROR_NONE) { |
| 1197 | info.location = location; |
| 1198 | info.method = method; |
| 1199 | } else { |
| 1200 | info.location = -1; |
| 1201 | } |
| 1202 | event_callback(env, &info); |
| 1203 | } END_CALLBACK(); |
| 1204 | |
| 1205 | LOG_MISC(("END cbMonitorWaited")); |
| 1206 | } |
| 1207 | |
| 1208 | /* Event callback for JVMTI_EVENT_VM_INIT */ |
| 1209 | static void JNICALL |
| 1210 | cbVMInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread) |
| 1211 | { |
| 1212 | EventInfo info; |
| 1213 | |
| 1214 | LOG_CB(("cbVMInit")); |
| 1215 | |
| 1216 | BEGIN_CALLBACK() { |
| 1217 | (void)memset(&info,0,sizeof(info)); |
| 1218 | info.ei = EI_VM_INIT; |
| 1219 | info.thread = thread; |
| 1220 | event_callback(env, &info); |
| 1221 | } END_CALLBACK(); |
| 1222 | |
| 1223 | LOG_MISC(("END cbVMInit")); |
| 1224 | } |
| 1225 | |
| 1226 | /* Event callback for JVMTI_EVENT_VM_DEATH */ |
| 1227 | static void JNICALL |
| 1228 | cbVMDeath(jvmtiEnv *jvmti_env, JNIEnv *env) |
| 1229 | { |
| 1230 | jvmtiError error; |
| 1231 | EventInfo info; |
| 1232 | LOG_CB(("cbVMDeath")); |
| 1233 | |
| 1234 | /* Clear out ALL callbacks at this time, we don't want any more. */ |
| 1235 | /* This should prevent any new BEGIN_CALLBACK() calls. */ |
| 1236 | (void)memset(&(gdata->callbacks),0,sizeof(gdata->callbacks)); |
| 1237 | error = JVMTI_FUNC_PTR(gdata->jvmti,SetEventCallbacks) |
| 1238 | (gdata->jvmti, &(gdata->callbacks), sizeof(gdata->callbacks)); |
| 1239 | if (error != JVMTI_ERROR_NONE) { |
| 1240 | EXIT_ERROR(error,"Can't clear event callbacks on vm death"); |
| 1241 | } |
| 1242 | |
| 1243 | /* Now that no new callbacks will be made, we need to wait for the ones |
| 1244 | * that are still active to complete. |
| 1245 | * The BEGIN_CALLBACK/END_CALLBACK macros implement the VM_DEATH |
| 1246 | * callback protocol. Once the callback table is cleared (above), |
| 1247 | * we can have callback threads in different stages: |
| 1248 | * 1) after callback function entry and before BEGIN_CALLBACK |
| 1249 | * macro; we catch these threads with callbackBlock in the |
| 1250 | * BEGIN_CALLBACK macro |
| 1251 | * 2) after BEGIN_CALLBACK macro and before END_CALLBACK macro; we |
| 1252 | * catch these threads with callbackBlock in the END_CALLBACK |
| 1253 | * macro |
| 1254 | * 3) after END_CALLBACK macro; these threads have made it past |
| 1255 | * callbackBlock and callbackLock and don't count as active |
| 1256 | * |
| 1257 | * Since some of the callback threads could be blocked or suspended |
| 1258 | * we will resume all threads suspended by the debugger for a short |
| 1259 | * time to flush out all callbacks. Note that the callback threads |
| 1260 | * will block from returning to the VM in both macros. Some threads |
| 1261 | * not associated with callbacks, but suspended by the debugger may |
| 1262 | * continue on, but not for long. |
| 1263 | * Once the last callback finishes, it will notify this thread and |
| 1264 | * we fall out of the loop below and actually process the VM_DEATH |
| 1265 | * event. |
| 1266 | */ |
| 1267 | debugMonitorEnter(callbackBlock); { |
| 1268 | debugMonitorEnter(callbackLock); { |
| 1269 | vm_death_callback_active = JNI_TRUE; |
| 1270 | (void)threadControl_resumeAll(); |
| 1271 | while (active_callbacks > 0) { |
| 1272 | /* wait for active CALLBACKs to check in (and block) */ |
| 1273 | debugMonitorWait(callbackLock); |
| 1274 | } |
| 1275 | } debugMonitorExit(callbackLock); |
| 1276 | |
| 1277 | /* Only now should we actually process the VM death event */ |
| 1278 | (void)memset(&info,0,sizeof(info)); |
| 1279 | info.ei = EI_VM_DEATH; |
| 1280 | event_callback(env, &info); |
| 1281 | |
| 1282 | /* Here we unblock all the callbacks and let them return to the |
| 1283 | * VM. It's not clear this is necessary, but leaving threads |
| 1284 | * blocked doesn't seem like a good idea. They don't have much |
| 1285 | * life left anyway. |
| 1286 | */ |
| 1287 | } debugMonitorExit(callbackBlock); |
| 1288 | |
| 1289 | /* |
| 1290 | * The VM will die soon after the completion of this callback - we |
| 1291 | * may need to do a final synchronization with the command loop to |
| 1292 | * avoid the VM terminating with replying to the final (resume) |
| 1293 | * command. |
| 1294 | */ |
| 1295 | debugLoop_sync(); |
| 1296 | |
| 1297 | LOG_MISC(("END cbVMDeath")); |
| 1298 | } |
| 1299 | |
| 1300 | /** |
| 1301 | * Delete this handler (do not delete permanent handlers): |
| 1302 | * Deinsert handler from active list, |
| 1303 | * make it inactive, and free it's memory |
| 1304 | * Assumes handlerLock held. |
| 1305 | */ |
| 1306 | static jvmtiError |
| 1307 | freeHandler(HandlerNode *node) { |
| 1308 | jvmtiError error = JVMTI_ERROR_NONE; |
| 1309 | |
| 1310 | /* deinsert the handler node before disableEvents() to make |
| 1311 | * sure the event will be disabled when no other event |
| 1312 | * handlers are installed. |
| 1313 | */ |
| 1314 | if (node != NULL && (!node->permanent)) { |
| 1315 | deinsert(node); |
| 1316 | error = eventFilterRestricted_deinstall(node); |
| 1317 | jvmtiDeallocate(node); |
| 1318 | } |
| 1319 | |
| 1320 | return error; |
| 1321 | } |
| 1322 | |
| 1323 | /** |
| 1324 | * Delete all the handlers on this chain (do not delete permanent handlers). |
| 1325 | * Assumes handlerLock held. |
| 1326 | */ |
| 1327 | static jvmtiError |
| 1328 | freeHandlerChain(HandlerChain *chain) |
| 1329 | { |
| 1330 | HandlerNode *node; |
| 1331 | jvmtiError error; |
| 1332 | |
| 1333 | error = JVMTI_ERROR_NONE; |
| 1334 | node = chain->first; |
| 1335 | while ( node != NULL ) { |
| 1336 | HandlerNode *next; |
| 1337 | jvmtiError singleError; |
| 1338 | |
| 1339 | next = NEXT(node); |
| 1340 | singleError = freeHandler(node); |
| 1341 | if ( singleError != JVMTI_ERROR_NONE ) { |
| 1342 | error = singleError; |
| 1343 | } |
| 1344 | node = next; |
| 1345 | } |
| 1346 | return error; |
| 1347 | } |
| 1348 | |
| 1349 | /** |
| 1350 | * Deinsert and free all memory. Safe for non-inserted nodes. |
| 1351 | */ |
| 1352 | jvmtiError |
| 1353 | eventHandler_free(HandlerNode *node) |
| 1354 | { |
| 1355 | jvmtiError error; |
| 1356 | |
| 1357 | debugMonitorEnter(handlerLock); |
| 1358 | |
| 1359 | error = freeHandler(node); |
| 1360 | |
| 1361 | debugMonitorExit(handlerLock); |
| 1362 | |
| 1363 | return error; |
| 1364 | } |
| 1365 | |
| 1366 | /** |
| 1367 | * Free all handlers of this kind created by the JDWP client, |
| 1368 | * that is, doesn't free handlers internally created by back-end. |
| 1369 | */ |
| 1370 | jvmtiError |
| 1371 | eventHandler_freeAll(EventIndex ei) |
| 1372 | { |
| 1373 | jvmtiError error = JVMTI_ERROR_NONE; |
| 1374 | HandlerNode *node; |
| 1375 | |
| 1376 | debugMonitorEnter(handlerLock); |
| 1377 | node = getHandlerChain(ei)->first; |
| 1378 | while (node != NULL) { |
| 1379 | HandlerNode *next = NEXT(node); /* allows node removal */ |
| 1380 | if (node->handlerID != 0) { /* don't free internal handlers */ |
| 1381 | error = freeHandler(node); |
| 1382 | if (error != JVMTI_ERROR_NONE) { |
| 1383 | break; |
| 1384 | } |
| 1385 | } |
| 1386 | node = next; |
| 1387 | } |
| 1388 | debugMonitorExit(handlerLock); |
| 1389 | return error; |
| 1390 | } |
| 1391 | |
| 1392 | /*** |
| 1393 | * Delete all breakpoints on "clazz". |
| 1394 | */ |
| 1395 | void |
| 1396 | eventHandler_freeClassBreakpoints(jclass clazz) |
| 1397 | { |
| 1398 | HandlerNode *node; |
| 1399 | JNIEnv *env = getEnv(); |
| 1400 | |
| 1401 | debugMonitorEnter(handlerLock); |
| 1402 | node = getHandlerChain(EI_BREAKPOINT)->first; |
| 1403 | while (node != NULL) { |
| 1404 | HandlerNode *next = NEXT(node); /* allows node removal */ |
| 1405 | if (eventFilterRestricted_isBreakpointInClass(env, clazz, |
| 1406 | node)) { |
| 1407 | (void)freeHandler(node); |
| 1408 | } |
| 1409 | node = next; |
| 1410 | } |
| 1411 | debugMonitorExit(handlerLock); |
| 1412 | } |
| 1413 | |
| 1414 | jvmtiError |
| 1415 | eventHandler_freeByID(EventIndex ei, HandlerID handlerID) |
| 1416 | { |
| 1417 | jvmtiError error; |
| 1418 | HandlerNode *node; |
| 1419 | |
| 1420 | debugMonitorEnter(handlerLock); |
| 1421 | node = find(ei, handlerID); |
| 1422 | if (node != NULL) { |
| 1423 | error = freeHandler(node); |
| 1424 | } else { |
| 1425 | /* already freed */ |
| 1426 | error = JVMTI_ERROR_NONE; |
| 1427 | } |
| 1428 | debugMonitorExit(handlerLock); |
| 1429 | return error; |
| 1430 | } |
| 1431 | |
| 1432 | void |
| 1433 | eventHandler_initialize(jbyte sessionID) |
| 1434 | { |
| 1435 | jvmtiError error; |
| 1436 | jint i; |
| 1437 | |
| 1438 | requestIdCounter = 1; |
| 1439 | currentSessionID = sessionID; |
| 1440 | |
| 1441 | /* This is for BEGIN_CALLBACK/END_CALLBACK handling, make sure this |
| 1442 | * is done while none of these callbacks are active. |
| 1443 | */ |
| 1444 | active_callbacks = 0; |
| 1445 | vm_death_callback_active = JNI_FALSE; |
| 1446 | callbackLock = debugMonitorCreate("JDWP Callback Lock"); |
| 1447 | callbackBlock = debugMonitorCreate("JDWP Callback Block"); |
| 1448 | |
| 1449 | handlerLock = debugMonitorCreate("JDWP Event Handler Lock"); |
| 1450 | |
| 1451 | for (i = EI_min; i <= EI_max; ++i) { |
| 1452 | getHandlerChain(i)->first = NULL; |
| 1453 | } |
| 1454 | |
| 1455 | /* |
| 1456 | * Permanently enabled some events. |
| 1457 | */ |
| 1458 | error = threadControl_setEventMode(JVMTI_ENABLE, |
| 1459 | EI_VM_INIT, NULL); |
| 1460 | if (error != JVMTI_ERROR_NONE) { |
| 1461 | EXIT_ERROR(error,"Can't enable vm init events"); |
| 1462 | } |
| 1463 | error = threadControl_setEventMode(JVMTI_ENABLE, |
| 1464 | EI_VM_DEATH, NULL); |
| 1465 | if (error != JVMTI_ERROR_NONE) { |
| 1466 | EXIT_ERROR(error,"Can't enable vm death events"); |
| 1467 | } |
| 1468 | error = threadControl_setEventMode(JVMTI_ENABLE, |
| 1469 | EI_THREAD_START, NULL); |
| 1470 | if (error != JVMTI_ERROR_NONE) { |
| 1471 | EXIT_ERROR(error,"Can't enable thread start events"); |
| 1472 | } |
| 1473 | error = threadControl_setEventMode(JVMTI_ENABLE, |
| 1474 | EI_THREAD_END, NULL); |
| 1475 | if (error != JVMTI_ERROR_NONE) { |
| 1476 | EXIT_ERROR(error,"Can't enable thread end events"); |
| 1477 | } |
| 1478 | error = threadControl_setEventMode(JVMTI_ENABLE, |
| 1479 | EI_CLASS_PREPARE, NULL); |
| 1480 | if (error != JVMTI_ERROR_NONE) { |
| 1481 | EXIT_ERROR(error,"Can't enable class prepare events"); |
| 1482 | } |
| 1483 | error = threadControl_setEventMode(JVMTI_ENABLE, |
| 1484 | EI_GC_FINISH, NULL); |
| 1485 | if (error != JVMTI_ERROR_NONE) { |
| 1486 | EXIT_ERROR(error,"Can't enable garbage collection finish events"); |
| 1487 | } |
| 1488 | |
| 1489 | (void)memset(&(gdata->callbacks),0,sizeof(gdata->callbacks)); |
| 1490 | /* Event callback for JVMTI_EVENT_SINGLE_STEP */ |
| 1491 | gdata->callbacks.SingleStep = &cbSingleStep; |
| 1492 | /* Event callback for JVMTI_EVENT_BREAKPOINT */ |
| 1493 | gdata->callbacks.Breakpoint = &cbBreakpoint; |
| 1494 | /* Event callback for JVMTI_EVENT_FRAME_POP */ |
| 1495 | gdata->callbacks.FramePop = &cbFramePop; |
| 1496 | /* Event callback for JVMTI_EVENT_EXCEPTION */ |
| 1497 | gdata->callbacks.Exception = &cbException; |
| 1498 | /* Event callback for JVMTI_EVENT_THREAD_START */ |
| 1499 | gdata->callbacks.ThreadStart = &cbThreadStart; |
| 1500 | /* Event callback for JVMTI_EVENT_THREAD_END */ |
| 1501 | gdata->callbacks.ThreadEnd = &cbThreadEnd; |
| 1502 | /* Event callback for JVMTI_EVENT_CLASS_PREPARE */ |
| 1503 | gdata->callbacks.ClassPrepare = &cbClassPrepare; |
| 1504 | /* Event callback for JVMTI_EVENT_CLASS_LOAD */ |
| 1505 | gdata->callbacks.ClassLoad = &cbClassLoad; |
| 1506 | /* Event callback for JVMTI_EVENT_FIELD_ACCESS */ |
| 1507 | gdata->callbacks.FieldAccess = &cbFieldAccess; |
| 1508 | /* Event callback for JVMTI_EVENT_FIELD_MODIFICATION */ |
| 1509 | gdata->callbacks.FieldModification = &cbFieldModification; |
| 1510 | /* Event callback for JVMTI_EVENT_EXCEPTION_CATCH */ |
| 1511 | gdata->callbacks.ExceptionCatch = &cbExceptionCatch; |
| 1512 | /* Event callback for JVMTI_EVENT_METHOD_ENTRY */ |
| 1513 | gdata->callbacks.MethodEntry = &cbMethodEntry; |
| 1514 | /* Event callback for JVMTI_EVENT_METHOD_EXIT */ |
| 1515 | gdata->callbacks.MethodExit = &cbMethodExit; |
| 1516 | /* Event callback for JVMTI_EVENT_MONITOR_CONTENDED_ENTER */ |
| 1517 | gdata->callbacks.MonitorContendedEnter = &cbMonitorContendedEnter; |
| 1518 | /* Event callback for JVMTI_EVENT_MONITOR_CONTENDED_ENTERED */ |
| 1519 | gdata->callbacks.MonitorContendedEntered = &cbMonitorContendedEntered; |
| 1520 | /* Event callback for JVMTI_EVENT_MONITOR_WAIT */ |
| 1521 | gdata->callbacks.MonitorWait = &cbMonitorWait; |
| 1522 | /* Event callback for JVMTI_EVENT_MONITOR_WAITED */ |
| 1523 | gdata->callbacks.MonitorWaited = &cbMonitorWaited; |
| 1524 | /* Event callback for JVMTI_EVENT_VM_INIT */ |
| 1525 | gdata->callbacks.VMInit = &cbVMInit; |
| 1526 | /* Event callback for JVMTI_EVENT_VM_DEATH */ |
| 1527 | gdata->callbacks.VMDeath = &cbVMDeath; |
| 1528 | /* Event callback for JVMTI_EVENT_GARBAGE_COLLECTION_FINISH */ |
| 1529 | gdata->callbacks.GarbageCollectionFinish = &cbGarbageCollectionFinish; |
| 1530 | |
| 1531 | error = JVMTI_FUNC_PTR(gdata->jvmti,SetEventCallbacks) |
| 1532 | (gdata->jvmti, &(gdata->callbacks), sizeof(gdata->callbacks)); |
| 1533 | if (error != JVMTI_ERROR_NONE) { |
| 1534 | EXIT_ERROR(error,"Can't set event callbacks"); |
| 1535 | } |
| 1536 | |
| 1537 | /* Notify other modules that the event callbacks are in place */ |
| 1538 | threadControl_onHook(); |
| 1539 | |
| 1540 | /* Get the event helper thread initialized */ |
| 1541 | eventHelper_initialize(sessionID); |
| 1542 | } |
| 1543 | |
| 1544 | void |
| 1545 | eventHandler_reset(jbyte sessionID) |
| 1546 | { |
| 1547 | int i; |
| 1548 | |
| 1549 | debugMonitorEnter(handlerLock); |
| 1550 | |
| 1551 | /* We must do this first so that if any invokes complete, |
| 1552 | * there will be no attempt to send them to the front |
| 1553 | * end. Waiting for threadControl_reset leaves a window where |
| 1554 | * the invoke completions can sneak through. |
| 1555 | */ |
| 1556 | threadControl_detachInvokes(); |
| 1557 | |
| 1558 | /* Reset the event helper thread, purging all queued and |
| 1559 | * in-process commands. |
| 1560 | */ |
| 1561 | eventHelper_reset(sessionID); |
| 1562 | |
| 1563 | /* delete all handlers */ |
| 1564 | for (i = EI_min; i <= EI_max; i++) { |
| 1565 | (void)freeHandlerChain(getHandlerChain(i)); |
| 1566 | } |
| 1567 | |
| 1568 | requestIdCounter = 1; |
| 1569 | currentSessionID = sessionID; |
| 1570 | |
| 1571 | debugMonitorExit(handlerLock); |
| 1572 | } |
| 1573 | |
| 1574 | void |
| 1575 | eventHandler_lock(void) |
| 1576 | { |
| 1577 | debugMonitorEnter(handlerLock); |
| 1578 | } |
| 1579 | |
| 1580 | void |
| 1581 | eventHandler_unlock(void) |
| 1582 | { |
| 1583 | debugMonitorExit(handlerLock); |
| 1584 | } |
| 1585 | |
| 1586 | /***** handler creation *****/ |
| 1587 | |
| 1588 | HandlerNode * |
| 1589 | eventHandler_alloc(jint filterCount, EventIndex ei, jbyte suspendPolicy) |
| 1590 | { |
| 1591 | HandlerNode *node = eventFilterRestricted_alloc(filterCount); |
| 1592 | |
| 1593 | if (node != NULL) { |
| 1594 | node->ei = ei; |
| 1595 | node->suspendPolicy = suspendPolicy; |
| 1596 | node->permanent = JNI_FALSE; |
| 1597 | } |
| 1598 | |
| 1599 | return node; |
| 1600 | } |
| 1601 | |
| 1602 | |
| 1603 | HandlerID |
| 1604 | eventHandler_allocHandlerID(void) |
| 1605 | { |
| 1606 | jint handlerID; |
| 1607 | debugMonitorEnter(handlerLock); |
| 1608 | handlerID = ++requestIdCounter; |
| 1609 | debugMonitorExit(handlerLock); |
| 1610 | return handlerID; |
| 1611 | } |
| 1612 | |
| 1613 | |
| 1614 | static jvmtiError |
| 1615 | installHandler(HandlerNode *node, |
| 1616 | HandlerFunction func, |
| 1617 | jboolean external) |
| 1618 | { |
| 1619 | jvmtiError error; |
| 1620 | |
| 1621 | if ( func == NULL ) { |
| 1622 | return AGENT_ERROR_INVALID_EVENT_TYPE; |
| 1623 | } |
| 1624 | |
| 1625 | debugMonitorEnter(handlerLock); |
| 1626 | |
| 1627 | HANDLER_FUNCTION(node) = func; |
| 1628 | |
| 1629 | node->handlerID = external? ++requestIdCounter : 0; |
| 1630 | error = eventFilterRestricted_install(node); |
| 1631 | if (error == JVMTI_ERROR_NONE) { |
| 1632 | insert(getHandlerChain(node->ei), node); |
| 1633 | } |
| 1634 | |
| 1635 | debugMonitorExit(handlerLock); |
| 1636 | |
| 1637 | return error; |
| 1638 | } |
| 1639 | |
| 1640 | static HandlerNode * |
| 1641 | createInternal(EventIndex ei, HandlerFunction func, |
| 1642 | jthread thread, jclass clazz, jmethodID method, |
| 1643 | jlocation location, jboolean permanent) |
| 1644 | { |
| 1645 | jint index = 0; |
| 1646 | jvmtiError error = JVMTI_ERROR_NONE; |
| 1647 | HandlerNode *node; |
| 1648 | |
| 1649 | /* |
| 1650 | * Start with necessary allocations |
| 1651 | */ |
| 1652 | node = eventHandler_alloc( |
| 1653 | ((thread == NULL)? 0 : 1) + ((clazz == NULL)? 0 : 1), |
| 1654 | ei, JDWP_SUSPEND_POLICY(NONE)); |
| 1655 | if (node == NULL) { |
| 1656 | return NULL; |
| 1657 | } |
| 1658 | |
| 1659 | node->permanent = permanent; |
| 1660 | |
| 1661 | if (thread != NULL) { |
| 1662 | error = eventFilter_setThreadOnlyFilter(node, index++, thread); |
| 1663 | } |
| 1664 | |
| 1665 | if ((error == JVMTI_ERROR_NONE) && (clazz != NULL)) { |
| 1666 | error = eventFilter_setLocationOnlyFilter(node, index++, clazz, |
| 1667 | method, location); |
| 1668 | } |
| 1669 | /* |
| 1670 | * Create the new handler node |
| 1671 | */ |
| 1672 | error = installHandler(node, func, JNI_FALSE); |
| 1673 | |
| 1674 | if (error != JVMTI_ERROR_NONE) { |
| 1675 | (void)eventHandler_free(node); |
| 1676 | node = NULL; |
| 1677 | } |
| 1678 | return node; |
| 1679 | } |
| 1680 | |
| 1681 | HandlerNode * |
| 1682 | eventHandler_createPermanentInternal(EventIndex ei, HandlerFunction func) |
| 1683 | { |
| 1684 | return createInternal(ei, func, NULL, |
| 1685 | NULL, NULL, (jlocation)NULL, JNI_TRUE); |
| 1686 | } |
| 1687 | |
| 1688 | HandlerNode * |
| 1689 | eventHandler_createInternalThreadOnly(EventIndex ei, |
| 1690 | HandlerFunction func, |
| 1691 | jthread thread) |
| 1692 | { |
| 1693 | return createInternal(ei, func, thread, |
| 1694 | NULL, NULL, (jlocation)NULL, JNI_FALSE); |
| 1695 | } |
| 1696 | |
| 1697 | HandlerNode * |
| 1698 | eventHandler_createInternalBreakpoint(HandlerFunction func, |
| 1699 | jthread thread, |
| 1700 | jclass clazz, |
| 1701 | jmethodID method, |
| 1702 | jlocation location) |
| 1703 | { |
| 1704 | return createInternal(EI_BREAKPOINT, func, thread, |
| 1705 | clazz, method, location, JNI_FALSE); |
| 1706 | } |
| 1707 | |
| 1708 | jvmtiError |
| 1709 | eventHandler_installExternal(HandlerNode *node) |
| 1710 | { |
| 1711 | return installHandler(node, |
| 1712 | standardHandlers_defaultHandler(node->ei), |
| 1713 | JNI_TRUE); |
| 1714 | } |