blob: ef9927b100892f5d60b80bc3b031511b7376a4e4 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1998-1999 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package com.sun.tools.example.debug.bdi;
27
28import com.sun.jdi.VirtualMachine;
29import com.sun.jdi.VMDisconnectedException;
30import com.sun.jdi.event.EventSet;
31
32/**
33 * Our repository of what we know about the state of one
34 * running VM.
35 */
36class Session {
37
38 final VirtualMachine vm;
39 final ExecutionManager runtime;
40 final OutputListener diagnostics;
41
42 boolean running = true; // Set false by JDIEventSource
43 boolean interrupted = false; // Set false by JDIEventSource
44
45 private JDIEventSource eventSourceThread = null;
46 private int traceFlags;
47 private boolean dead = false;
48
49 public Session(VirtualMachine vm, ExecutionManager runtime,
50 OutputListener diagnostics) {
51 this.vm = vm;
52 this.runtime = runtime;
53 this.diagnostics = diagnostics;
54 this.traceFlags = VirtualMachine.TRACE_NONE;
55 }
56
57 /**
58 * Determine if VM is interrupted, i.e, present and not running.
59 */
60 public boolean isInterrupted() {
61 return interrupted;
62 }
63
64 public void setTraceMode(int traceFlags) {
65 this.traceFlags = traceFlags;
66 if (!dead) {
67 vm.setDebugTraceMode(traceFlags);
68 }
69 }
70
71 public boolean attach() {
72 vm.setDebugTraceMode(traceFlags);
73 diagnostics.putString("Connected to VM");
74 eventSourceThread = new JDIEventSource(this);
75 eventSourceThread.start();
76 return true;
77 }
78
79 public void detach() {
80 if (!dead) {
81 eventSourceThread.interrupt();
82 eventSourceThread = null;
83 //### The VM may already be disconnected
84 //### if the debuggee did a System.exit().
85 //### Exception handler here is a kludge,
86 //### Rather, there are many other places
87 //### where we need to handle this exception,
88 //### and initiate a detach due to an error
89 //### condition, e.g., connection failure.
90 try {
91 vm.dispose();
92 } catch (VMDisconnectedException ee) {}
93 dead = true;
94 diagnostics.putString("Disconnected from VM");
95 }
96 }
97}