eventhandler.c

来自「This is a resource based on j2me embedde」· C语言 代码 · 共 1,675 行 · 第 1/4 页

C
1,675
字号
/* @(#)eventHandler.c	1.96 06/10/25 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation.  *  * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt).  *  * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA  *  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions.  *//* * eventHandler * * This module handles events as they come in directly from JVMTI * and also maps them to JDI events.  JDI events are those requested * at the JDI or JDWP level and seen on those levels.  Mapping is * one-to-many, a JVMTI event may map to several JDI events, or * to none.  Part of that mapping process is filteration, which  * eventFilter sub-module handles.  A JDI EventRequest corresponds * to a HandlerNode and a JDI filter to the hidden HandlerNode data * used by eventFilter.  For example, if at the JDI level the user * executed: *    *   EventRequestManager erm = vm.eventRequestManager(); *   BreakpointRequest bp = erm.createBreakpointRequest(); *   bp.enable(); *   ClassPrepareRequest req = erm.createClassPrepareRequest(); *   req.enable(); *   req = erm.createClassPrepareRequest(); *   req.addClassFilter("Foo*"); *   req.enable(); * * Three handlers would be created, the first with a LocationOnly * filter and the last with a ClassMatch  filter.  * When a JVMTI class prepare event for "Foobar" * comes in, the second handler will create one JDI event, the * third handler will compare the class signature, and since * it matchs create a second event.  There may also be internal * events as there are in this case, one created by the front-end * and one by the back-end. * * Each event kind has a handler chain, which is a doublely linked * list of handlers for that kind of event. */#include "util.h"#include "eventHandler.h"#include "eventHandlerRestricted.h"#include "eventFilter.h"#include "eventFilterRestricted.h"#include "standardHandlers.h"#include "threadControl.h"#include "eventHelper.h"#include "classTrack.h"#include "commonRef.h"#include "debugLoop.h"static HandlerID requestIdCounter;static jbyte currentSessionID;/* Counter of active callbacks and flag for vm_death */static int      active_callbacks   = 0;static jboolean vm_death_callback_active = JNI_FALSE;static jrawMonitorID callbackLock;static jrawMonitorID callbackBlock;/* Macros to surround callback code (non-VM_DEATH callbacks). *   Note that this just keeps a count of the non-VM_DEATH callbacks that  *   are currently active, it does not prevent these callbacks from *   operating in parallel. It's the VM_DEATH callback that will wait *   for all these callbacks to finish up, so that it can report the *   VM_DEATH in a clean state. *   If the VM_DEATH callback is active in the BEGIN macro then this *   callback just blocks until released by the VM_DEATH callback. *   If the VM_DEATH callback is active in the END macro, then this *   callback will notify the VM_DEATH callback if it's the last one, *   and then block until released by the VM_DEATH callback. *   Why block? These threads are often the threads of the Java program, *   not blocking might mean that a return would continue execution of *   some java thread in the middle of VM_DEATH, this seems troubled. * *   WARNING: No not 'return' or 'goto' out of the BEGIN_CALLBACK/END_CALLBACK *            block, this will mess up the count. */#define BEGIN_CALLBACK()                                                \{ /* BEGIN OF CALLBACK */                                               \    jboolean bypass = JNI_TRUE;                                         \    debugMonitorEnter(callbackLock); {                                  \        if (vm_death_callback_active) {                                 \            /* allow VM_DEATH callback to finish */                     \            debugMonitorExit(callbackLock);                             \            /* Now block because VM is about to die */                  \            debugMonitorEnter(callbackBlock);                           \            debugMonitorExit(callbackBlock);                            \        } else {                                                        \            active_callbacks++;                                         \            bypass = JNI_FALSE;                                         \            debugMonitorExit(callbackLock);                             \        }                                                               \    }                                                                   \    if ( !bypass ) {                                                    \        /* BODY OF CALLBACK CODE */#define END_CALLBACK() /* Part of bypass if body */                     \        debugMonitorEnter(callbackLock); {                              \            active_callbacks--;                                         \            if (active_callbacks < 0) {                                 \                EXIT_ERROR(0, "Problems tracking active callbacks");    \            }                                                           \            if (vm_death_callback_active) {                             \                if (active_callbacks == 0) {                            \                    debugMonitorNotifyAll(callbackLock);                \                }                                                       \                /* allow VM_DEATH callback to finish */                 \                debugMonitorExit(callbackLock);                         \                /* Now block because VM is about to die */              \                debugMonitorEnter(callbackBlock);                       \                debugMonitorExit(callbackBlock);                        \            } else {                                                    \                debugMonitorExit(callbackLock);                         \            }                                                           \        }                                                               \    }                                                                   \} /* END OF CALLBACK *//* * We are starting with a very simple locking scheme * for event handling.  All readers and writers of data in * the handlers[] chain must own this lock for the duration * of its use. If contention becomes a problem, we can: *  * 1) create a lock per event type.  * 2) move to a readers/writers approach where multiple threads  * can access the chains simultaneously while reading (the * normal activity of an event callback).  */static jrawMonitorID handlerLock;typedef struct HandlerChain_ {    HandlerNode *first;    /* add lock here */} HandlerChain;/* * This array maps event kinds to handler chains. * Protected by handlerLock. */static HandlerChain __handlers[EI_max-EI_min+1];/* Given a HandlerNode, these access our private data. */#define PRIVATE_DATA(node) \       (&(((EventHandlerRestricted_HandlerNode*)(void*)(node))->private_ehpd))#define NEXT(node) (PRIVATE_DATA(node)->private_next)#define PREV(node) (PRIVATE_DATA(node)->private_prev)#define CHAIN(node) (PRIVATE_DATA(node)->private_chain)#define HANDLER_FUNCTION(node) (PRIVATE_DATA(node)->private_handlerFunction)static jclass getMethodClass(jvmtiEnv *jvmti_env, jmethodID method);static jvmtiError freeHandler(HandlerNode *node);static jvmtiError freeHandlerChain(HandlerChain *chain);static HandlerChain *getHandlerChain(EventIndex i){    if ( i < EI_min || i > EI_max ) {        EXIT_ERROR(AGENT_ERROR_INVALID_EVENT_TYPE,"bad index for handler");    }    return &(__handlers[i-EI_min]);}static voidinsert(HandlerChain *chain, HandlerNode *node){    HandlerNode *oldHead = chain->first;    NEXT(node) = oldHead;    PREV(node) = NULL;    CHAIN(node) = chain;    if (oldHead != NULL) {        PREV(oldHead) = node;    }    chain->first = node;}static HandlerNode *findInChain(HandlerChain *chain, HandlerID handlerID){    HandlerNode *node = chain->first;    while (node != NULL) {        if (node->handlerID == handlerID) {            return node;        }        node = NEXT(node);    }    return NULL;}static HandlerNode *find(EventIndex ei, HandlerID handlerID){    return findInChain(getHandlerChain(ei), handlerID);}/** * Deinsert.  Safe for non-inserted nodes. */static voiddeinsert(HandlerNode *node){    HandlerChain *chain = CHAIN(node);    if (chain == NULL) {        return;    }    if (chain->first == node) {        chain->first = NEXT(node);    }    if (NEXT(node) != NULL) {        PREV(NEXT(node)) = PREV(node);    }    if (PREV(node) != NULL) {        NEXT(PREV(node)) = NEXT(node);    }    CHAIN(node) = NULL;}jboolean eventHandlerRestricted_iterator(EventIndex ei,                              IteratorFunction func, void *arg){    HandlerChain *chain;    HandlerNode *node;    JNIEnv *env;        chain = getHandlerChain(ei);    node = chain->first;    env = getEnv();    if ( func == NULL ) {        EXIT_ERROR(AGENT_ERROR_INTERNAL,"iterator function NULL");    }        while (node != NULL) {        if (((func)(env, node, arg))) {            return JNI_TRUE;        }        node = NEXT(node);    }    return JNI_FALSE;}/* BREAKPOINT, METHOD_ENTRY and SINGLE_STEP events are covered by * the co-location of events policy. Of these three co-located * events, METHOD_ENTRY is  always reported first and BREAKPOINT * is always reported last. Here are the possible combinations and * their order: * * (p1) METHOD_ENTRY, BREAKPOINT (existing) * (p2) METHOD_ENTRY, BREAKPOINT (new) * (p1) METHOD_ENTRY, SINGLE_STEP * (p1) METHOD_ENTRY, SINGLE_STEP, BREAKPOINT (existing) * (p1/p2) METHOD_ENTRY, SINGLE_STEP, BREAKPOINT (new) * (p1) SINGLE_STEP, BREAKPOINT (existing) * (p2) SINGLE_STEP, BREAKPOINT (new) * * BREAKPOINT (existing) indicates a BREAKPOINT that is set before * the other co-located event is posted. BREAKPOINT (new) indicates * a BREAKPOINT that is set after the other co-located event is * posted and before the thread has resumed execution. * * Co-location of events policy used to be implemented via * temporary BREAKPOINTs along with deferring the reporting of * non-BREAKPOINT co-located events, but the temporary BREAKPOINTs * caused performance problems on VMs where setting or clearing * BREAKPOINTs is expensive, e.g., HotSpot. * * The policy is now implemented in two phases. Phase 1: when a * METHOD_ENTRY or SINGLE_STEP event is received, if there is an * existing co-located BREAKPOINT, then the current event is * deferred. When the BREAKPOINT event is processed, the event * bag will contain the deferred METHOD_ENTRY and/or SINGLE_STEP * events along with the BREAKPOINT event. For a METHOD_ENTRY * event where there is not an existing co-located BREAKPOINT, * if SINGLE_STEP events are also enabled for the thread, then * the METHOD_ENTRY event is deferred. When the SINGLE_STEP event * is processed, the event bag will also contain the deferred * METHOD_ENTRY event. This covers each of the combinations * marked with 'p1' above. * * Phase 2: if there is no existing co-located BREAKPOINT, then the * location information for the METHOD_ENTRY or SINGLE_STEP event * is recorded in the ThreadNode. If the next event for the thread * is a co-located BREAKPOINT, then the first BREAKPOINT event will * be skipped since it cannot be delivered in the same event set. * This covers each of the combinations marked with 'p2' above. * * For the combination marked p1/p2, part of the case is handled * during phase 1 and the rest is handled during phase 2. * * The recording of information in the ThreadNode is handled in * this routine. The special handling of the next event for the * thread is handled in skipEventReport(). */static jbooleandeferEventReport(JNIEnv *env, jthread thread,             EventIndex ei, jclass clazz, jmethodID method, jlocation location){    jboolean deferring = JNI_FALSE;    switch (ei) {        case EI_METHOD_ENTRY:            if (!isMethodNative(method)) {                jvmtiError error;                jlocation start;                jlocation end;                error = methodLocation(method, &start, &end);                if (error == JVMTI_ERROR_NONE) {                    deferring = isBreakpointSet(clazz, method, start) ||                                threadControl_getInstructionStepMode(thread)                                    == JVMTI_ENABLE;                    if (!deferring) {                        threadControl_saveCLEInfo(env, thread, ei,                                                  clazz, method, start);                    }                }            }            break;        case EI_SINGLE_STEP:            deferring = isBreakpointSet(clazz, method, location);            if (!deferring) {                threadControl_saveCLEInfo(env, thread, ei,                                          clazz, method, location);            }            break;        default:            break;    }    /* TO DO: Once JVMTI supports a way to know if we're     * at the end of a method, we should check here for     * break and step events which precede a method exit     * event.       */    return deferring;}/* Handle phase 2 of the co-located events policy. See detailed * comments in deferEventReport() above. */static jbooleanskipEventReport(JNIEnv *env, jthread thread, EventIndex ei,                        jclass clazz, jmethodID method, jlocation location){     jboolean skipping = JNI_FALSE;    if (ei == EI_BREAKPOINT) {        if (threadControl_cmpCLEInfo(env, thread, clazz, method, location)) {            LOG_MISC(("Co-located breakpoint event found: "                "%s,thread=%p,clazz=%p,method=%p,location=%d",                eventText(ei), thread, clazz, method, location));            skipping = JNI_TRUE;        }    }    threadControl_clearCLEInfo(env, thread);    return skipping;}static void reportEvents(JNIEnv *env, jbyte sessionID, jthread thread, EventIndex ei,             jclass clazz, jmethodID method, jlocation location,             struct bag *eventBag){    jbyte suspendPolicy;    jboolean invoking;    if (bagSize(eventBag) < 1) {        return;    }    /*     * Never report events before initialization completes     */    if (!debugInit_isInitComplete()) {        return;    }    /*     * Check to see if we should skip reporting this event due to     * co-location of events policy.     */    if (thread != NULL &&            skipEventReport(env, thread, ei, clazz, method, location)) {        LOG_MISC(("event report being skipped: "            "ei=%s,thread=%p,clazz=%p,method=%p,location=%d",            eventText(ei), thread, clazz, method, location));        bagDeleteAll(eventBag);        return;    }    /* We delay the reporting of some events so that they can be

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?