1 /* Jdwp.java -- Virtual machine to JDWP back-end programming interface
2 Copyright (C) 2005, 2006 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
40 package gnu
.classpath
.jdwp
;
42 import gnu
.classpath
.jdwp
.event
.Event
;
43 import gnu
.classpath
.jdwp
.event
.EventManager
;
44 import gnu
.classpath
.jdwp
.event
.EventRequest
;
45 import gnu
.classpath
.jdwp
.exception
.JdwpException
;
46 import gnu
.classpath
.jdwp
.processor
.PacketProcessor
;
47 import gnu
.classpath
.jdwp
.transport
.ITransport
;
48 import gnu
.classpath
.jdwp
.transport
.JdwpConnection
;
49 import gnu
.classpath
.jdwp
.transport
.TransportException
;
50 import gnu
.classpath
.jdwp
.transport
.TransportFactory
;
52 import java
.io
.IOException
;
53 import java
.security
.AccessController
;
54 import java
.util
.HashMap
;
57 * Main interface from the virtual machine to the JDWP back-end.
59 * @author Keith Seitz (keiths@redhat.com)
64 // The single instance of the back-end
65 private static Jdwp _instance
= null;
70 public static boolean isDebugging
= false;
73 private PacketProcessor _packetProcessor
;
74 private Thread _ppThread
;
76 // JDWP configuration properties
77 private HashMap _properties
;
79 // The suspend property of the configure string
80 // (-Xrunjdwp:..suspend=<boolean>)
81 private static final String _PROPERTY_SUSPEND
= "suspend";
83 // Connection to debugger
84 private JdwpConnection _connection
;
86 // Are we shutting down the current session?
87 private boolean _shutdown
;
89 // A thread group for the JDWP threads
90 private ThreadGroup _group
;
103 * Returns the JDWP back-end, creating an instance of it
104 * if one does not already exist.
106 public static Jdwp
getDefault ()
112 * Get the thread group used by JDWP threads
114 * @return the thread group
116 public ThreadGroup
getJdwpThreadGroup()
122 * Should the virtual machine suspend on startup?
124 public static boolean suspendOnStartup ()
126 Jdwp jdwp
= getDefault ();
129 String suspend
= (String
) jdwp
._properties
.get (_PROPERTY_SUSPEND
);
130 if (suspend
!= null && suspend
.equals ("y"))
138 * Configures the back-end
140 * @param configArgs a string of configury options
142 public void configure (String configArgs
)
144 _processConfigury (configArgs
);
147 // A helper function to initialize the transport layer
148 private void _doInitialization ()
149 throws TransportException
151 _group
= new ThreadGroup ("JDWP threads");
152 // initialize transport
153 ITransport transport
= TransportFactory
.newInstance (_properties
);
154 _connection
= new JdwpConnection (_group
, transport
);
155 _connection
.initialize ();
156 _connection
.start ();
159 _packetProcessor
= new PacketProcessor (_connection
);
160 _ppThread
= new Thread (_group
, new Runnable ()
164 AccessController
.doPrivileged (_packetProcessor
);
166 }, "packet processor");
171 * Shutdown the JDWP back-end
173 * NOTE: This does not quite work properly. See notes in
174 * run() on this subject (catch of InterruptedException).
176 public void shutdown ()
180 _packetProcessor
.shutdown ();
181 _ppThread
.interrupt ();
182 _connection
.shutdown ();
186 /* FIXME: probably need to check state of user's
187 program -- if it is suspended, we need to either
188 resume or kill them. */
195 * Notify the debugger of an event. This method should not
196 * be called if debugging is not active (but it would not
197 * cause any harm). Places where event notifications occur
198 * should check isDebugging before doing anything.
200 * The event is filtered through the event manager before being
203 * FIXME: Probably need logic to send multiple events
204 * @param event the event to report
206 public static void notify (Event event
)
208 Jdwp jdwp
= getDefault ();
211 EventManager em
= EventManager
.getDefault ();
212 EventRequest request
= em
.getEventRequest (event
);
217 System
.out
.println ("Jdwp.notify: sending event " + event
);
218 sendEvent (request
, event
);
219 jdwp
._enforceSuspendPolicy (request
.getSuspendPolicy ());
223 /* Really not much we can do. For now, just print out
224 a warning to the user. */
225 System
.out
.println ("Jdwp.notify: caught exception: " + e
);
232 * Sends the event to the debugger.
234 * This method bypasses the event manager's filtering.
236 * @param request the debugger request for the event
237 * @param event the event to send
238 * @throws IOException if a communications failure occurs
240 public static void sendEvent (EventRequest request
, Event event
)
243 Jdwp jdwp
= getDefault ();
246 // !! May need to implement send queue?
247 synchronized (jdwp
._connection
)
249 jdwp
._connection
.sendEvent (request
, event
);
254 // Helper function to enforce suspend policies on event notification
255 private void _enforceSuspendPolicy (byte suspendPolicy
)
258 switch (suspendPolicy
)
260 case EventRequest
.SUSPEND_NONE
:
264 case EventRequest
.SUSPEND_THREAD
:
265 VMVirtualMachine
.suspendThread (Thread
.currentThread ());
268 case EventRequest
.SUSPEND_ALL
:
269 VMVirtualMachine
.suspendAllThreads ();
278 _doInitialization ();
282 System
.out
.println ("Exception in JDWP back-end: " + t
);
287 // A helper function to process the configure string "-Xrunjdwp:..."
288 private void _processConfigury (String configString
)
290 // Loop through configuration arguments looking for a
292 _properties
= new HashMap ();
293 String
[] options
= configString
.split (",");
294 for (int i
= 0; i
< options
.length
; ++i
)
296 String
[] property
= options
[i
].split ("=");
297 if (property
.length
== 2)
298 _properties
.put (property
[0], property
[1]);
299 // ignore malformed options