jdk/src/share/back/standardHandlers.c
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * handlers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * The default event request handler functions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "eventHandler.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "threadControl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "eventHelper.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "classTrack.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "standardHandlers.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
handleClassPrepare(JNIEnv *env, EventInfo *evinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                   HandlerNode *node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                   struct bag *eventBag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    jthread thread = evinfo->thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /* We try hard to avoid class loads/prepares in debugger
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * threads, but it is still possible for them to happen (most
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * likely for exceptions that are thrown within JNI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * methods). If such an event occurs, we must report it, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * we cannot suspend the debugger thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * 1) We report the thread as NULL because we don't want the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     *    application to get hold of a debugger thread object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * 2) We try to do the right thing wrt to suspending
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *    threads without suspending debugger threads. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *    requested suspend policy is NONE, there's no problem. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *    the requested policy is ALL, we can just suspend all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *    application threads without producing any surprising
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *    results by leaving the debugger thread running. However,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *    if the requested policy is EVENT_THREAD, we are forced
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *    to do something different than requested. The most
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *    useful behavior is to suspend all application threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *    (just as if the policy was ALL). This allows the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *    application to operate on the class before it gets into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *    circulation and so it is preferable to the other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *    alternative of suspending no threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    if (threadControl_isDebugThread(thread)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        evinfo->thread = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (node->suspendPolicy == JDWP_SUSPEND_POLICY(EVENT_THREAD)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            node->suspendPolicy = JDWP_SUSPEND_POLICY(ALL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    eventHelper_recordEvent(evinfo, node->handlerID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                            node->suspendPolicy, eventBag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
handleGarbageCollectionFinish(JNIEnv *env, EventInfo *evinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                  HandlerNode *node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                  struct bag *eventBag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    JDI_ASSERT_MSG(JNI_FALSE, "Should never call handleGarbageCollectionFinish");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
handleFrameEvent(JNIEnv *env, EventInfo *evinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                 HandlerNode *node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                 struct bag *eventBag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * The frame id that comes with this event is very transient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * We can't send the frame to the helper thread because it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * might be useless by the time the helper thread can use it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * (if suspend policy is NONE). So, get the needed info from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * the frame and then use a special command to the helper
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    jmethodID method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    jlocation location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    jvmtiError error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    FrameNumber fnum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    jvalue returnValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    error = JVMTI_FUNC_PTR(gdata->jvmti,GetFrameLocation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            (gdata->jvmti, evinfo->thread, fnum, &method, &location);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    if (error != JVMTI_ERROR_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        location = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    returnValue = evinfo->u.method_exit.return_value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    eventHelper_recordFrameEvent(node->handlerID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                 node->suspendPolicy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                 evinfo->ei,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                 evinfo->thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                 evinfo->clazz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                 evinfo->method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                 location,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                 node->needReturnValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                 returnValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                 eventBag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
genericHandler(JNIEnv *env, EventInfo *evinfo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
               HandlerNode *node,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
               struct bag *eventBag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    eventHelper_recordEvent(evinfo, node->handlerID, node->suspendPolicy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                            eventBag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
HandlerFunction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
standardHandlers_defaultHandler(EventIndex ei)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    switch (ei) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        case EI_BREAKPOINT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        case EI_EXCEPTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        case EI_FIELD_ACCESS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        case EI_FIELD_MODIFICATION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        case EI_SINGLE_STEP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        case EI_THREAD_START:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        case EI_THREAD_END:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        case EI_VM_DEATH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        case EI_MONITOR_CONTENDED_ENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        case EI_MONITOR_CONTENDED_ENTERED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        case EI_MONITOR_WAIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        case EI_MONITOR_WAITED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return &genericHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        case EI_CLASS_PREPARE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return &handleClassPrepare;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        case EI_GC_FINISH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return &handleGarbageCollectionFinish;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        case EI_METHOD_ENTRY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        case EI_METHOD_EXIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return &handleFrameEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            /* This NULL will trigger a AGENT_ERROR_INVALID_EVENT_TYPE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
standardHandlers_onConnect(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    jboolean installed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /* always report VMDeath to a connected debugger */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    installed = (eventHandler_createPermanentInternal(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        EI_VM_DEATH, genericHandler) != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    if (!installed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        EXIT_ERROR(AGENT_ERROR_INVALID_EVENT_TYPE,"Unable to install VM Death event handler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
standardHandlers_onDisconnect(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
}