jdk/src/share/classes/com/sun/tools/jdi/TargetVM.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 715 f16baef3a20e
child 11277 e3a1c90dd439
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
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: 715
diff changeset
     2
 * Copyright (c) 1998, 2008, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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
package com.sun.tools.jdi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.jdi.connect.spi.Connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.jdi.event.EventSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class TargetVM implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private Map<String, Packet> waitingQueue = new HashMap<String, Packet>(32,0.75f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private boolean shouldListen = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private List<EventQueue> eventQueues = Collections.synchronizedList(new ArrayList<EventQueue>(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private VirtualMachineImpl vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private Connection connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private Thread readerThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private EventController eventController = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private boolean eventsHeld = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * TO DO: The limit numbers below are somewhat arbitrary and should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * be configurable in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static private final int OVERLOADED_QUEUE = 2000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static private final int UNDERLOADED_QUEUE = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    TargetVM(VirtualMachineImpl vm, Connection connection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        this.vm = vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        this.connection = connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        this.readerThread = new Thread(vm.threadGroupForJDI(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                                       this, "JDI Target VM Interface");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.readerThread.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    void start() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        readerThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private void dumpPacket(Packet packet, boolean sending) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        String direction = sending ? "Sending" : "Receiving";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (sending) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            vm.printTrace(direction + " Command. id=" + packet.id +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                          ", length=" + packet.data.length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                          ", commandSet=" + packet.cmdSet +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                          ", command=" + packet.cmd +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                          ", flags=" + packet.flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            String type = (packet.flags & Packet.Reply) != 0 ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                          "Reply" : "Event";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            vm.printTrace(direction + " " + type + ". id=" + packet.id +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                          ", length=" + packet.data.length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                          ", errorCode=" + packet.errorCode +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                          ", flags=" + packet.flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        StringBuffer line = new StringBuffer(80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        line.append("0000: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        for (int i = 0; i < packet.data.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if ((i > 0) && (i % 16 == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                vm.printTrace(line.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                line.setLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                line.append(String.valueOf(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                line.append(": ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                int len = line.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                for (int j = 0; j < 6 - len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    line.insert(0, '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            int val = 0xff & packet.data[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            String str = Integer.toHexString(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (str.length() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                line.append('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            line.append(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            line.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (line.length() > 6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            vm.printTrace(line.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if ((vm.traceFlags & VirtualMachine.TRACE_SENDS) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            vm.printTrace("Target VM interface thread running");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        Packet p=null,p2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        String idString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        while(shouldListen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                byte b[] = connection.readPacket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                if (b.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                p = Packet.fromByteArray(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                shouldListen = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    connection.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                } catch (IOException ioe) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if ((vm.traceFlags & VirtualMachineImpl.TRACE_RAW_RECEIVES) != 0)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                dumpPacket(p, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if((p.flags & Packet.Reply) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                // It's a command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                handleVMCommand(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                /*if(p.errorCode != Packet.ReplyNoError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    System.err.println("Packet " + p.id + " returned failure = " + p.errorCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                }*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                vm.state().notifyCommandComplete(p.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                idString = String.valueOf(p.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                synchronized(waitingQueue) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   151
                    p2 = waitingQueue.get(idString);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    if (p2 != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        waitingQueue.remove(idString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                if(p2 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    // Whoa! a reply without a sender. Problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    // FIX ME! Need to post an error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    System.err.println("Recieved reply with no sender!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                p2.errorCode = p.errorCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                p2.data = p.data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                p2.replied = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                synchronized(p2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    p2.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // inform the VM mamager that this VM is history
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        vm.vmManager.disposeVirtualMachine(vm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // close down all the event queues
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        // Closing a queue causes a VMDisconnectEvent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // be put onto the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        synchronized(eventQueues) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            Iterator iter = eventQueues.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                ((EventQueueImpl)iter.next()).close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        // indirectly throw VMDisconnectedException to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // command requesters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        synchronized(waitingQueue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            Iterator iter = waitingQueue.values().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                Packet packet = (Packet)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                synchronized(packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    packet.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            waitingQueue.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if ((vm.traceFlags & VirtualMachine.TRACE_SENDS) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            vm.printTrace("Target VM interface thread exiting");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    protected void handleVMCommand(Packet p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        switch (p.cmdSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            case JDWP.Event.COMMAND_SET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                handleEventCmdSet(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                System.err.println("Ignoring cmd " + p.id + "/" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                   p.cmdSet + "/" + p.cmd + " from the VM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                return;
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
    /* Events should not be constructed on this thread (the thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * which reads all data from the transport). This means that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * packet cannot be converted to real JDI objects as that may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * involve further communications with the back end which would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * deadlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Instead the whole packet is passed for lazy eval by a queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * reading thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    protected void handleEventCmdSet(Packet p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        EventSet eventSet = new EventSetImpl(vm, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (eventSet != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            queueEventSet(eventSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    private EventController eventController() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (eventController == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            eventController = new EventController(vm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return eventController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    private synchronized void controlEventFlow(int maxQueueSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (!eventsHeld && (maxQueueSize > OVERLOADED_QUEUE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            eventController().hold();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            eventsHeld = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } else if (eventsHeld && (maxQueueSize < UNDERLOADED_QUEUE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            eventController().release();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            eventsHeld = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    void notifyDequeueEventSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        int maxQueueSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        synchronized(eventQueues) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            Iterator iter = eventQueues.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                EventQueueImpl queue = (EventQueueImpl)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                maxQueueSize = Math.max(maxQueueSize, queue.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        controlEventFlow(maxQueueSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    private void queueEventSet(EventSet eventSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        int maxQueueSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        synchronized(eventQueues) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            Iterator iter = eventQueues.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                EventQueueImpl queue = (EventQueueImpl)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                queue.enqueue(eventSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                maxQueueSize = Math.max(maxQueueSize, queue.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        controlEventFlow(maxQueueSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    void send(Packet packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        String id = String.valueOf(packet.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        synchronized(waitingQueue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            waitingQueue.put(id, packet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if ((vm.traceFlags & VirtualMachineImpl.TRACE_RAW_SENDS) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            dumpPacket(packet, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            connection.writePacket(packet.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            throw new VMDisconnectedException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    void waitForReply(Packet packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        synchronized(packet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            while ((!packet.replied) && shouldListen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                try { packet.wait(); } catch (InterruptedException e) {;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (!packet.replied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                throw new VMDisconnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    void addEventQueue(EventQueueImpl queue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if ((vm.traceFlags & VirtualMachine.TRACE_EVENTS) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            vm.printTrace("New event queue added");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        eventQueues.add(queue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    void stopListening() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if ((vm.traceFlags & VirtualMachine.TRACE_EVENTS) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            vm.printTrace("Target VM i/f closing event queues");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        shouldListen = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            connection.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        } catch (IOException ioe) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    static private class EventController extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        VirtualMachineImpl vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        int controlRequest = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        EventController(VirtualMachineImpl vm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            super(vm.threadGroupForJDI(), "JDI Event Control Thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            this.vm = vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            setPriority((MAX_PRIORITY + NORM_PRIORITY)/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            super.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        synchronized void hold() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            controlRequest++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        synchronized void release() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            controlRequest--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            while(true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                int currentRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    while (controlRequest == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        try {wait();} catch (InterruptedException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    currentRequest = controlRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    controlRequest = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    if (currentRequest > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        JDWP.VirtualMachine.HoldEvents.process(vm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        JDWP.VirtualMachine.ReleaseEvents.process(vm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                } catch (JDWPException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                     * Don't want to terminate the thread, so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                     * stack trace is printed and we continue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    e.toJDIException().printStackTrace(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                }
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
}