Reset branch to trunk.
[official-gcc.git] / trunk / libjava / classpath / gnu / classpath / jdwp / Jdwp.java
blob1d2329255cb318728b1870cb688aef52d28daf5d
1 /* Jdwp.java -- Virtual machine to JDWP back-end programming interface
2 Copyright (C) 2005, 2006, 2007 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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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.ArrayList;
55 import java.util.HashMap;
57 /**
58 * Main interface from the virtual machine to the JDWP back-end.
60 * The thread created by this class is only used for initialization.
61 * Once it exits, the JDWP backend is fully initialized.
63 * @author Keith Seitz (keiths@redhat.com)
65 public class Jdwp
66 extends Thread
68 // The single instance of the back-end
69 private static Jdwp _instance = null;
71 /**
72 * Are we debugging? Only true if debugging
73 * *and* initialized.
75 public static boolean isDebugging = false;
77 // Packet processor
78 private PacketProcessor _packetProcessor;
79 private Thread _ppThread;
81 // JDWP configuration properties
82 private HashMap _properties;
84 // The suspend property of the configure string
85 // (-Xrunjdwp:..suspend=<boolean>)
86 private static final String _PROPERTY_SUSPEND = "suspend";
88 // Connection to debugger
89 private JdwpConnection _connection;
91 // Are we shutting down the current session?
92 private boolean _shutdown;
94 // A thread group for the JDWP threads
95 private ThreadGroup _group;
97 // Initialization synchronization
98 private Object _initLock = new Object ();
99 private int _initCount = 0;
102 * constructor
104 public Jdwp ()
106 _shutdown = false;
107 _instance = this;
111 * Returns the JDWP back-end, creating an instance of it
112 * if one does not already exist.
114 public static Jdwp getDefault ()
116 return _instance;
120 * Get the thread group used by JDWP threads
122 * @return the thread group
124 public ThreadGroup getJdwpThreadGroup()
126 return _group;
130 * Should the virtual machine suspend on startup?
132 public static boolean suspendOnStartup ()
134 Jdwp jdwp = getDefault ();
135 if (jdwp != null)
137 String suspend = (String) jdwp._properties.get (_PROPERTY_SUSPEND);
138 if (suspend != null && suspend.equals ("y"))
139 return true;
142 return false;
146 * Configures the back-end
148 * @param configArgs a string of configury options
150 public void configure (String configArgs)
152 _processConfigury (configArgs);
155 // A helper function to initialize the transport layer
156 private void _doInitialization ()
157 throws TransportException
159 _group = new ThreadGroup ("JDWP threads");
160 // initialize transport
161 ITransport transport = TransportFactory.newInstance (_properties);
162 _connection = new JdwpConnection (_group, transport);
163 _connection.initialize ();
164 _connection.start ();
166 // Create processor
167 _packetProcessor = new PacketProcessor (_connection);
168 _ppThread = new Thread (_group, new Runnable ()
170 public void run ()
172 AccessController.doPrivileged (_packetProcessor);
174 }, "packet processor");
175 _ppThread.start ();
179 * Shutdown the JDWP back-end
181 * NOTE: This does not quite work properly. See notes in
182 * run() on this subject (catch of InterruptedException).
184 public void shutdown ()
186 if (!_shutdown)
188 _packetProcessor.shutdown ();
189 _ppThread.interrupt ();
190 _connection.shutdown ();
191 _shutdown = true;
192 isDebugging = false;
194 /* FIXME: probably need to check state of user's
195 program -- if it is suspended, we need to either
196 resume or kill them. */
198 interrupt ();
203 * Notify the debugger of an event. This method should not
204 * be called if debugging is not active (but it would not
205 * cause any harm). Places where event notifications occur
206 * should check isDebugging before doing anything.
208 * The event is filtered through the event manager before being
209 * sent.
211 * @param event the event to report
213 public static void notify(Event event)
215 Jdwp jdwp = getDefault();
216 if (jdwp != null)
218 EventManager em = EventManager.getDefault();
219 EventRequest[] requests = em.getEventRequests(event);
220 for (int i = 0; i < requests.length; ++i)
224 sendEvent(requests[i], event);
225 jdwp._enforceSuspendPolicy(requests[i].getSuspendPolicy());
227 catch (Exception e)
229 /* Really not much we can do. For now, just print out
230 a warning to the user. */
231 System.out.println ("Jdwp.notify: caught exception: " + e);
238 * Notify the debugger of "co-located" events. This method should
239 * not be called if debugging is not active (but it would not
240 * cause any harm). Places where event notifications occur
241 * should check isDebugging before doing anything.
243 * The events are filtered through the event manager before being
244 * sent.
246 * @param events the events to report
248 public static void notify(Event[] events)
250 Jdwp jdwp = getDefault();
252 if (jdwp != null)
254 byte suspendPolicy = JdwpConstants.SuspendPolicy.NONE;
255 EventManager em = EventManager.getDefault();
256 ArrayList allEvents = new ArrayList ();
257 ArrayList allRequests = new ArrayList ();
258 for (int i = 0; i < events.length; ++i)
260 EventRequest[] r = em.getEventRequests(events[i]);
261 for (int j = 0; j < r.length; ++j)
263 /* This is hacky, but it's not clear whether this
264 can really happen, and if it does, what should
265 occur. */
266 allEvents.add (events[i]);
267 allRequests.add (r[j]);
269 // Perhaps this is overkill?
270 if (r[j].getSuspendPolicy() > suspendPolicy)
271 suspendPolicy = r[j].getSuspendPolicy();
277 Event[] e = new Event[allEvents.size()];
278 allEvents.toArray(e);
279 EventRequest[] r = new EventRequest[allRequests.size()];
280 allRequests.toArray(r);
281 sendEvents(r, e, suspendPolicy);
282 jdwp._enforceSuspendPolicy(suspendPolicy);
284 catch (Exception e)
286 /* Really not much we can do. For now, just print out
287 a warning to the user. */
288 System.out.println ("Jdwp.notify: caught exception: " + e);
294 * Sends the event to the debugger.
296 * This method bypasses the event manager's filtering.
298 * @param request the debugger request for the event
299 * @param event the event to send
300 * @throws IOException if a communications failure occurs
302 public static void sendEvent (EventRequest request, Event event)
303 throws IOException
305 sendEvents (new EventRequest[] { request }, new Event[] { event },
306 request.getSuspendPolicy());
310 * Sends the events to the debugger.
312 * This method bypasses the event manager's filtering.
314 * @param requests list of debugger requests for the events
315 * @param events the events to send
316 * @param suspendPolicy the suspendPolicy enforced by the VM
317 * @throws IOException if a communications failure occurs
319 public static void sendEvents (EventRequest[] requests, Event[] events,
320 byte suspendPolicy)
321 throws IOException
323 Jdwp jdwp = getDefault();
324 if (jdwp != null)
326 synchronized (jdwp._connection)
328 jdwp._connection.sendEvents (requests, events, suspendPolicy);
333 // Helper function to enforce suspend policies on event notification
334 private void _enforceSuspendPolicy (byte suspendPolicy)
335 throws JdwpException
337 switch (suspendPolicy)
339 case EventRequest.SUSPEND_NONE:
340 // do nothing
341 break;
343 case EventRequest.SUSPEND_THREAD:
344 VMVirtualMachine.suspendThread (Thread.currentThread ());
345 break;
347 case EventRequest.SUSPEND_ALL:
348 VMVirtualMachine.suspendAllThreads ();
349 break;
354 * Allows subcomponents to specify that they are
355 * initialized.
357 * Subcomponents include JdwpConnection and PacketProcessor.
359 public void subcomponentInitialized ()
361 synchronized (_initLock)
363 ++_initCount;
364 _initLock.notify ();
368 public void run ()
372 _doInitialization ();
374 /* We need a little internal synchronization here, so that
375 when this thread dies, the back-end will be fully initialized,
376 ready to start servicing the VM and debugger. */
377 synchronized (_initLock)
379 while (_initCount != 2)
380 _initLock.wait ();
382 _initLock = null;
384 catch (Throwable t)
386 System.out.println ("Exception in JDWP back-end: " + t);
387 System.exit (1);
390 /* Force creation of the EventManager. If the event manager
391 has not been created when isDebugging is set, it is possible
392 that the VM will call Jdwp.notify (which uses EventManager)
393 while the EventManager is being created (or at least this is
394 a problem with gcj/gij). */
395 EventManager.getDefault();
397 // Now we are finally ready and initialized
398 isDebugging = true;
401 // A helper function to process the configure string "-Xrunjdwp:..."
402 private void _processConfigury (String configString)
404 // Loop through configuration arguments looking for a
405 // transport name
406 _properties = new HashMap ();
407 String[] options = configString.split (",");
408 for (int i = 0; i < options.length; ++i)
410 String[] property = options[i].split ("=");
411 if (property.length == 2)
412 _properties.put (property[0], property[1]);
413 // ignore malformed options