KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > example > debug > bdi > Session


1 /*
2  * @(#)Session.java 1.14 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Copyright (c) 1997-1999 by Sun Microsystems, Inc. All Rights Reserved.
9  *
10  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
11  * modify and redistribute this software in source and binary code form,
12  * provided that i) this copyright notice and license appear on all copies of
13  * the software; and ii) Licensee does not utilize the software in a manner
14  * which is disparaging to Sun.
15  *
16  * This software is provided "AS IS," without a warranty of any kind. ALL
17  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
18  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
19  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
20  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
21  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
22  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
23  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
24  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
25  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGES.
27  *
28  * This software is not designed or intended for use in on-line control of
29  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
30  * the design, construction, operation or maintenance of any nuclear
31  * facility. Licensee represents and warrants that it will not use or
32  * redistribute the Software for such purposes.
33  */

34
35 package com.sun.tools.example.debug.bdi;
36
37 import com.sun.jdi.VirtualMachine;
38 import com.sun.jdi.VMDisconnectedException;
39 import com.sun.jdi.event.EventSet;
40
41 /**
42  * Our repository of what we know about the state of one
43  * running VM.
44  */

45 class Session {
46
47     final VirtualMachine vm;
48     final ExecutionManager runtime;
49     final OutputListener diagnostics;
50
51     boolean running = true; // Set false by JDIEventSource
52
boolean interrupted = false; // Set false by JDIEventSource
53

54     private JDIEventSource eventSourceThread = null;
55     private int traceFlags;
56     private boolean dead = false;
57
58     public Session(VirtualMachine vm, ExecutionManager runtime,
59            OutputListener diagnostics) {
60     this.vm = vm;
61     this.runtime = runtime;
62     this.diagnostics = diagnostics;
63     this.traceFlags = VirtualMachine.TRACE_NONE;
64     }
65
66     /**
67      * Determine if VM is interrupted, i.e, present and not running.
68      */

69     public boolean isInterrupted() {
70     return interrupted;
71     }
72     
73     public void setTraceMode(int traceFlags) {
74         this.traceFlags = traceFlags;
75         if (!dead) {
76             vm.setDebugTraceMode(traceFlags);
77         }
78     }
79
80     public boolean attach() {
81         vm.setDebugTraceMode(traceFlags);
82         diagnostics.putString("Connected to VM");
83     eventSourceThread = new JDIEventSource(this);
84     eventSourceThread.start();
85         return true;
86     }
87     
88     public void detach() {
89         if (!dead) {
90         eventSourceThread.interrupt();
91         eventSourceThread = null;
92         //### The VM may already be disconnected
93
//### if the debuggee did a System.exit().
94
//### Exception handler here is a kludge,
95
//### Rather, there are many other places
96
//### where we need to handle this exception,
97
//### and initiate a detach due to an error
98
//### condition, e.g., connection failure.
99
try {
100         vm.dispose();
101         } catch (VMDisconnectedException ee) {}
102             dead = true;
103         diagnostics.putString("Disconnected from VM");
104         }
105     }
106 }
107
Popular Tags