annotate src/share/back/eventHandler.c @ 0:37a05a11f281

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