src/jdk.jdwp.agent/share/native/libjdwp/debugLoop.c
author rkennke
Fri, 12 Oct 2018 16:25:24 +0200
changeset 52107 0c1e44da019c
parent 47216 71c04702a3d5
permissions -rw-r--r--
8212053: A few more missing object equals barriers Reviewed-by: shade, zgu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
46841
76a15bd1de0a 8134103: JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents: 41565
diff changeset
     2
 * Copyright (c) 1998, 2017, 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 "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;
27739
d185cfc165df 6988950: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112)
sspitsyn
parents: 25859
diff changeset
    48
static jrawMonitorID vmDeathLock;
2
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
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
debugLoop_initialize(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
{
27739
d185cfc165df 6988950: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112)
sspitsyn
parents: 25859
diff changeset
    66
    vmDeathLock = debugMonitorCreate("JDWP VM_DEATH Lock");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
debugLoop_sync(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
{
27739
d185cfc165df 6988950: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112)
sspitsyn
parents: 25859
diff changeset
    72
    debugMonitorEnter(vmDeathLock);
d185cfc165df 6988950: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112)
sspitsyn
parents: 25859
diff changeset
    73
    debugMonitorExit(vmDeathLock);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * This is where all the work gets done.
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_run(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    jboolean shouldListen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    jdwpPacket p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    jvmtiStartFunction func;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /* Initialize all statics */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /* We may be starting a new connection after an error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    cmdQueue = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    cmdQueueLock = debugMonitorCreate("JDWP Command Queue Lock");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    transportError = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    shouldListen = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    func = &reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    (void)spawnNewThread(func, NULL, "JDWP Command Reader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    standardHandlers_onConnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    threadControl_onConnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /* Okay, start reading cmds! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    while (shouldListen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (!dequeue(&p)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (p.type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
             * Its a reply packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
           continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
             * Its a cmd packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            jdwpCmdPacket *cmd = &p.type.cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            PacketInputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            PacketOutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            CommandHandler func;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            /* Should reply be sent to sender.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
             * For error handling, assume yes, since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
             * only VM/exit does not reply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            jboolean replyToSender = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            /*
46841
76a15bd1de0a 8134103: JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents: 41565
diff changeset
   128
             * For all commands we hold the vmDeathLock
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
             * while executing and replying to the command. This ensures
46841
76a15bd1de0a 8134103: JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents: 41565
diff changeset
   130
             * that a command after VM_DEATH will be allowed to complete
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
             * before the thread posting the VM_DEATH continues VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
             * termination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
             */
46841
76a15bd1de0a 8134103: JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents: 41565
diff changeset
   134
            debugMonitorEnter(vmDeathLock);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            /* Initialize the input and output streams */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            inStream_init(&in, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            outStream_initReply(&out, inStream_id(&in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            LOG_MISC(("Command set %d, command %d", cmd->cmdSet, cmd->cmd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            func = debugDispatch_getHandler(cmd->cmdSet,cmd->cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (func == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                /* we've never heard of this, so I guess we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                 * haven't implemented it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                 * Handle gracefully for future expansion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                 * and platform / vendor expansion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                outStream_setError(&out, JDWP_ERROR(NOT_IMPLEMENTED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            } else if (gdata->vmDead &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
             ((cmd->cmdSet) != JDWP_COMMAND_SET(VirtualMachine))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                /* Protect the VM from calls while dead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                 * VirtualMachine cmdSet quietly ignores some cmds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                 * after VM death, so, it sends it's own errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                outStream_setError(&out, JDWP_ERROR(VM_DEAD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                /* Call the command handler */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                replyToSender = func(&in, &out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            /* Reply to the sender */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (replyToSender) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                if (inStream_error(&in)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    outStream_setError(&out, inStream_error(&in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                outStream_sendReply(&out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            /*
27739
d185cfc165df 6988950: JDWP exit error JVMTI_ERROR_WRONG_PHASE(112)
sspitsyn
parents: 25859
diff changeset
   171
             * Release the vmDeathLock as the reply has been posted.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
             */
46841
76a15bd1de0a 8134103: JVMTI_ERROR_WRONG_PHASE(112): on checking for an interface
sspitsyn
parents: 41565
diff changeset
   173
            debugMonitorExit(vmDeathLock);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            inStream_destroy(&in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            outStream_destroy(&out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            shouldListen = !lastCommand(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    threadControl_onDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    standardHandlers_onDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Cut off the transport immediately. This has the effect of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * cutting off any events that the eventHelper thread might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * be trying to send.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    transport_close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    debugMonitorDestroy(cmdQueueLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /* Reset for a new connection to this VM if it's still alive */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    if ( ! gdata->vmDead ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        debugInit_reset(getEnv());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
/* Command reader */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
reader(jvmtiEnv* jvmti_env, JNIEnv* jni_env, void* arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    jdwpPacket packet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    jdwpCmdPacket *cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    jboolean shouldListen = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    LOG_MISC(("Begin reader thread"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    while (shouldListen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        jint rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        rc = transport_receivePacket(&packet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        /* I/O error or EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (rc != 0 || (rc == 0 && packet.type.cmd.len == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            shouldListen = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            notifyTransportError();
41565
31d2aac15a77 8159519: Reformat JDWP messages
dcubed
parents: 27739
diff changeset
   217
        } else if (packet.type.cmd.flags != JDWPTRANSPORT_FLAGS_NONE) {
31d2aac15a77 8159519: Reformat JDWP messages
dcubed
parents: 27739
diff changeset
   218
            /*
31d2aac15a77 8159519: Reformat JDWP messages
dcubed
parents: 27739
diff changeset
   219
             * Close the connection when we get a jdwpCmdPacket with an
31d2aac15a77 8159519: Reformat JDWP messages
dcubed
parents: 27739
diff changeset
   220
             * invalid flags field value. This is a protocol violation
31d2aac15a77 8159519: Reformat JDWP messages
dcubed
parents: 27739
diff changeset
   221
             * so we drop the connection. Also this could be a web
31d2aac15a77 8159519: Reformat JDWP messages
dcubed
parents: 27739
diff changeset
   222
             * browser generating an HTTP request that passes the JDWP
31d2aac15a77 8159519: Reformat JDWP messages
dcubed
parents: 27739
diff changeset
   223
             * handshake. HTTP requests requires that everything be in
31d2aac15a77 8159519: Reformat JDWP messages
dcubed
parents: 27739
diff changeset
   224
             * the ASCII printable range so a flags value of
31d2aac15a77 8159519: Reformat JDWP messages
dcubed
parents: 27739
diff changeset
   225
             * JDWPTRANSPORT_FLAGS_NONE(0) cannot be generated via HTTP.
31d2aac15a77 8159519: Reformat JDWP messages
dcubed
parents: 27739
diff changeset
   226
             */
31d2aac15a77 8159519: Reformat JDWP messages
dcubed
parents: 27739
diff changeset
   227
            ERROR_MESSAGE(("Received jdwpPacket with flags != 0x%d (actual=0x%x) when a jdwpCmdPacket was expected.",
31d2aac15a77 8159519: Reformat JDWP messages
dcubed
parents: 27739
diff changeset
   228
                           JDWPTRANSPORT_FLAGS_NONE, packet.type.cmd.flags));
31d2aac15a77 8159519: Reformat JDWP messages
dcubed
parents: 27739
diff changeset
   229
            shouldListen = JNI_FALSE;
31d2aac15a77 8159519: Reformat JDWP messages
dcubed
parents: 27739
diff changeset
   230
            notifyTransportError();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            cmd = &packet.type.cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            LOG_MISC(("Command set %d, command %d", cmd->cmdSet, cmd->cmd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
             * FIXME! We need to deal with high priority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
             * packets and queue flushes!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            enqueue(&packet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            shouldListen = !lastCommand(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    LOG_MISC(("End reader thread"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 * The current system for queueing packets is highly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 * inefficient, and should be rewritten! It'd be nice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 * to avoid any additional memory allocations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
enqueue(jdwpPacket *packet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    struct PacketList *pL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    struct PacketList *walker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    pL = jvmtiAllocate((jint)sizeof(struct PacketList));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    if (pL == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"packet list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    pL->packet = *packet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    pL->next = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    debugMonitorEnter(cmdQueueLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    if (cmdQueue == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        cmdQueue = pL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        debugMonitorNotify(cmdQueueLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        walker = (struct PacketList *)cmdQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        while (walker->next != NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            walker = walker->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        walker->next = pL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    debugMonitorExit(cmdQueueLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
static jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
dequeue(jdwpPacket *packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    struct PacketList *node = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    debugMonitorEnter(cmdQueueLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    while (!transportError && (cmdQueue == NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        debugMonitorWait(cmdQueueLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    if (cmdQueue != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        node = (struct PacketList *)cmdQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        cmdQueue = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    debugMonitorExit(cmdQueueLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    if (node != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        *packet = node->packet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        jvmtiDeallocate(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    return (node != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
notifyTransportError(void) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    debugMonitorEnter(cmdQueueLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    transportError = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    debugMonitorNotify(cmdQueueLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    debugMonitorExit(cmdQueueLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
}