jdk/src/share/back/debugLoop.c
author ohair
Fri, 22 Aug 2008 12:24:27 -0700
changeset 1090 c5805b1672a6
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6732421: Removed old javavm and Classic VM files from the jdk7 sources Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "transport.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "debugLoop.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "debugDispatch.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "standardHandlers.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "inStream.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "outStream.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "threadControl.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
static void JNICALL reader(jvmtiEnv* jvmti_env, JNIEnv* jni_env, void* arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
static void enqueue(jdwpPacket *p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
static jboolean dequeue(jdwpPacket *p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
static void notifyTransportError(void);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
struct PacketList {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    jdwpPacket packet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    struct PacketList *next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
static volatile struct PacketList *cmdQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
static jrawMonitorID cmdQueueLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
static jrawMonitorID resumeLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
static jboolean transportError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
static jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
lastCommand(jdwpCmdPacket *cmd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    if ((cmd->cmdSet == JDWP_COMMAND_SET(VirtualMachine)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        ((cmd->cmd == JDWP_COMMAND(VirtualMachine, Dispose)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
         (cmd->cmd == JDWP_COMMAND(VirtualMachine, Exit)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
static jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
resumeCommand(jdwpCmdPacket *cmd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    if ( (cmd->cmdSet == JDWP_COMMAND_SET(VirtualMachine)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
         (cmd->cmd == JDWP_COMMAND(VirtualMachine, Resume)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
debugLoop_initialize(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    resumeLock = debugMonitorCreate("JDWP Resume Lock");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
debugLoop_sync(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    debugMonitorEnter(resumeLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    debugMonitorExit(resumeLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * This is where all the work gets done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
debugLoop_run(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    jboolean shouldListen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    jdwpPacket p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    jvmtiStartFunction func;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /* Initialize all statics */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /* We may be starting a new connection after an error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    cmdQueue = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    cmdQueueLock = debugMonitorCreate("JDWP Command Queue Lock");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    transportError = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    shouldListen = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    func = &reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    (void)spawnNewThread(func, NULL, "JDWP Command Reader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    standardHandlers_onConnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    threadControl_onConnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /* Okay, start reading cmds! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    while (shouldListen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (!dequeue(&p)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (p.type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
             * Its a reply packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
           continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
             * Its a cmd packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            jdwpCmdPacket *cmd = &p.type.cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            PacketInputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            PacketOutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            CommandHandler func;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            /* Should reply be sent to sender.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
             * For error handling, assume yes, since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
             * only VM/exit does not reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            jboolean replyToSender = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
             * For VirtualMachine.Resume commands we hold the resumeLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
             * while executing and replying to the command. This ensures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
             * that a Resume after VM_DEATH will be allowed to complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
             * before the thread posting the VM_DEATH continues VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
             * termination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (resumeCommand(cmd)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                debugMonitorEnter(resumeLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            /* Initialize the input and output streams */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            inStream_init(&in, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            outStream_initReply(&out, inStream_id(&in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            LOG_MISC(("Command set %d, command %d", cmd->cmdSet, cmd->cmd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            func = debugDispatch_getHandler(cmd->cmdSet,cmd->cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (func == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                /* we've never heard of this, so I guess we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                 * haven't implemented it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                 * Handle gracefully for future expansion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                 * and platform / vendor expansion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                outStream_setError(&out, JDWP_ERROR(NOT_IMPLEMENTED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            } else if (gdata->vmDead &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
             ((cmd->cmdSet) != JDWP_COMMAND_SET(VirtualMachine))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                /* Protect the VM from calls while dead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                 * VirtualMachine cmdSet quietly ignores some cmds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                 * after VM death, so, it sends it's own errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                outStream_setError(&out, JDWP_ERROR(VM_DEAD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                /* Call the command handler */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                replyToSender = func(&in, &out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            /* Reply to the sender */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (replyToSender) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                if (inStream_error(&in)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    outStream_setError(&out, inStream_error(&in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                outStream_sendReply(&out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
             * Release the resumeLock as the reply has been posted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if (resumeCommand(cmd)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                debugMonitorExit(resumeLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            inStream_destroy(&in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            outStream_destroy(&out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            shouldListen = !lastCommand(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    threadControl_onDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    standardHandlers_onDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Cut off the transport immediately. This has the effect of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * cutting off any events that the eventHelper thread might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * be trying to send.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    transport_close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    debugMonitorDestroy(cmdQueueLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /* Reset for a new connection to this VM if it's still alive */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    if ( ! gdata->vmDead ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        debugInit_reset(getEnv());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
/* Command reader */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
reader(jvmtiEnv* jvmti_env, JNIEnv* jni_env, void* arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    jdwpPacket packet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    jdwpCmdPacket *cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    jboolean shouldListen = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    LOG_MISC(("Begin reader thread"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    while (shouldListen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        jint rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        rc = transport_receivePacket(&packet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        /* I/O error or EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (rc != 0 || (rc == 0 && packet.type.cmd.len == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            shouldListen = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            notifyTransportError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            cmd = &packet.type.cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            LOG_MISC(("Command set %d, command %d", cmd->cmdSet, cmd->cmd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
             * FIXME! We need to deal with high priority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
             * packets and queue flushes!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            enqueue(&packet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            shouldListen = !lastCommand(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    LOG_MISC(("End reader thread"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 * The current system for queueing packets is highly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 * inefficient, and should be rewritten! It'd be nice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 * to avoid any additional memory allocations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
enqueue(jdwpPacket *packet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    struct PacketList *pL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    struct PacketList *walker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    pL = jvmtiAllocate((jint)sizeof(struct PacketList));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    if (pL == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"packet list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    pL->packet = *packet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    pL->next = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    debugMonitorEnter(cmdQueueLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    if (cmdQueue == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        cmdQueue = pL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        debugMonitorNotify(cmdQueueLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        walker = (struct PacketList *)cmdQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        while (walker->next != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            walker = walker->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        walker->next = pL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    debugMonitorExit(cmdQueueLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
static jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
dequeue(jdwpPacket *packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    struct PacketList *node = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    debugMonitorEnter(cmdQueueLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    while (!transportError && (cmdQueue == NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        debugMonitorWait(cmdQueueLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    if (cmdQueue != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        node = (struct PacketList *)cmdQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        cmdQueue = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    debugMonitorExit(cmdQueueLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    if (node != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        *packet = node->packet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        jvmtiDeallocate(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    return (node != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
notifyTransportError(void) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    debugMonitorEnter(cmdQueueLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    transportError = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    debugMonitorNotify(cmdQueueLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    debugMonitorExit(cmdQueueLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
}