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