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