Merge from mainline
[official-gcc.git] / libjava / classpath / gnu / classpath / jdwp / Jdwp.java
blob43a37de2435beb8fceb028c61124d06b89952019
1 /* Jdwp.java -- Virtual machine to JDWP back-end programming interface
2 Copyright (C) 2005 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.id.ThreadId;
47 import gnu.classpath.jdwp.processor.PacketProcessor;
48 import gnu.classpath.jdwp.transport.ITransport;
49 import gnu.classpath.jdwp.transport.JdwpConnection;
50 import gnu.classpath.jdwp.transport.TransportException;
51 import gnu.classpath.jdwp.transport.TransportFactory;
53 import java.io.IOException;
54 import java.security.AccessController;
55 import java.util.HashMap;
57 /**
58 * Main interface from the virtual machine to the JDWP back-end.
60 * @author Keith Seitz (keiths@redhat.com)
62 public class Jdwp
63 extends Thread
65 // The single instance of the back-end
66 private static Jdwp _instance = null;
68 /**
69 * Are we debugging?
71 public static boolean isDebugging = false;
73 // Packet processor
74 private PacketProcessor _packetProcessor;
75 private Thread _ppThread;
77 // JDWP configuration properties
78 private HashMap _properties;
80 // The suspend property of the configure string
81 // (-Xrunjdwp:..suspend=<boolean>)
82 private static final String _PROPERTY_SUSPEND = "suspend";
84 // User's main application thread
85 private Thread _mainThread;
87 // Connection to debugger
88 private JdwpConnection _connection;
90 // Are we shutting down the current session?
91 private boolean _shutdown;
93 // A thread group for the JDWP threads
94 private ThreadGroup _group;
96 /**
97 * constructor
99 public Jdwp ()
101 _shutdown = false;
102 isDebugging = true;
103 _instance = this;
107 * Returns the JDWP back-end, creating an instance of it
108 * if one does not already exist.
110 public static Jdwp getDefault ()
112 return _instance;
116 * Should the virtual machine suspend on startup?
118 public static boolean suspendOnStartup ()
120 Jdwp jdwp = getDefault ();
121 if (jdwp != null)
123 String suspend = (String) jdwp._properties.get (_PROPERTY_SUSPEND);
124 if (suspend != null && suspend.equals ("y"))
125 return true;
128 return false;
132 * Configures the back-end
134 * @param configArgs a string of configury options
135 * @param mainThread the main application thread
137 public void configure (String configArgs, Thread mainThread)
139 _mainThread = mainThread;
140 _processConfigury (configArgs);
143 // A helper function to initialize the transport layer
144 private void _doInitialization ()
145 throws TransportException
147 _group = new ThreadGroup ("JDWP threads");
148 // initialize transport
149 ITransport transport = TransportFactory.newInstance (_properties);
150 _connection = new JdwpConnection (_group, transport);
151 _connection.initialize ();
152 _connection.start ();
154 // Create processor
155 _packetProcessor = new PacketProcessor (_connection);
156 _ppThread = new Thread (_group, new Runnable ()
158 public void run ()
160 AccessController.doPrivileged (_packetProcessor);
163 _ppThread.start ();
167 * Shutdown the JDWP back-end
169 * NOTE: This does not quite work properly. See notes in
170 * run() on this subject (catch of InterruptedException).
172 public void shutdown ()
174 if (!_shutdown)
176 _packetProcessor.shutdown ();
177 _ppThread.interrupt ();
178 _connection.shutdown ();
179 _shutdown = true;
180 isDebugging = false;
182 /* FIXME: probably need to check state of user's
183 program -- if it is suspended, we need to either
184 resume or kill them. */
186 interrupt ();
191 * Notify the debugger of an event. This method should not
192 * be called if debugging is not active (but it would not
193 * cause any harm). Places where event notifications occur
194 * should check isDebugging before doing anything.
196 * The event is filtered through the event manager before being
197 * sent.
199 * FIXME: Probably need logic to send multiple events
200 * @param event the event to report
202 public static void notify (Event event)
204 Jdwp jdwp = getDefault ();
205 if (jdwp != null)
207 EventManager em = EventManager.getDefault ();
208 EventRequest request = em.getEventRequest (event);
209 if (request != null)
213 System.out.println ("Jdwp.notify: sending event " + event);
214 sendEvent (request, event);
215 jdwp._enforceSuspendPolicy (request.getSuspendPolicy ());
217 catch (Exception e)
219 /* Really not much we can do. For now, just print out
220 a warning to the user. */
221 System.out.println ("Jdwp.notify: caught exception: " + e);
228 * Sends the event to the debugger.
230 * This method bypasses the event manager's filtering.
232 * @param request the debugger request for the event
233 * @param event the event to send
234 * @throws IOException if a communications failure occurs
236 public static void sendEvent (EventRequest request, Event event)
237 throws IOException
239 Jdwp jdwp = getDefault ();
240 if (jdwp != null)
242 // !! May need to implement send queue?
243 synchronized (jdwp._connection)
245 jdwp._connection.sendEvent (request, event);
250 // Helper function to enforce suspend policies on event notification
251 private void _enforceSuspendPolicy (byte suspendPolicy)
252 throws JdwpException
254 switch (suspendPolicy)
256 case EventRequest.SUSPEND_NONE:
257 // do nothing
258 break;
260 case EventRequest.SUSPEND_THREAD:
261 VMVirtualMachine.suspendThread (this);
262 break;
264 case EventRequest.SUSPEND_ALL:
265 VMVirtualMachine.suspendAllThreads ();
266 break;
270 public void run ()
274 _doInitialization ();
276 _mainThread.start ();
278 _mainThread.join ();
280 catch (InterruptedException ie)
282 /* Shutting down. If we're in server mode, we should
283 prepare for a new connection. Otherwise, we should
284 simply exit. */
285 // FIXME
287 catch (Throwable t)
289 System.out.println ("Exception in JDWP back-end: " + t);
290 System.exit (1);
294 // A helper function to process the configure string "-Xrunjdwp:..."
295 private void _processConfigury (String configString)
297 // Loop through configuration arguments looking for a
298 // transport name
299 _properties = new HashMap ();
300 String[] options = configString.split (",");
301 for (int i = 0; i < options.length; ++i)
303 String[] property = options[i].split ("=");
304 if (property.length == 2)
305 _properties.put (property[0], property[1]);
306 // ignore malformed options