2 using System.Collections.Generic;
3 using System.Diagnostics;
6 using System.Net.Sockets;
7 using System.Runtime.Remoting.Messaging;
9 namespace Mono.Debugger.Soft
11 public class LaunchOptions {
12 public string AgentArgs {
16 public bool Valgrind {
20 public ProcessLauncher CustomProcessLauncher {
24 public TargetProcessLauncher CustomTargetProcessLauncher {
28 public delegate Process ProcessLauncher (ProcessStartInfo info);
29 public delegate ITargetProcess TargetProcessLauncher (ProcessStartInfo info);
32 public class VirtualMachineManager
34 private delegate VirtualMachine LaunchCallback (ITargetProcess p, ProcessStartInfo info, Socket socket);
35 private delegate VirtualMachine ListenCallback (Socket dbg_sock, Socket con_sock);
36 private delegate VirtualMachine ConnectCallback (Socket dbg_sock, Socket con_sock, IPEndPoint dbg_ep, IPEndPoint con_ep);
38 internal VirtualMachineManager () {
41 public static VirtualMachine LaunchInternal (Process p, ProcessStartInfo info, Socket socket)
43 return LaunchInternal (new ProcessWrapper (p), info, socket);
46 public static VirtualMachine LaunchInternal (ITargetProcess p, ProcessStartInfo info, Socket socket) {
47 Socket accepted = null;
49 accepted = socket.Accept ();
54 Connection conn = new TcpConnection (accepted);
56 VirtualMachine vm = new VirtualMachine (p, conn);
58 if (info.RedirectStandardOutput)
59 vm.StandardOutput = p.StandardOutput;
61 if (info.RedirectStandardError)
62 vm.StandardError = p.StandardError;
64 conn.EventHandler = new EventHandler (vm);
71 public static IAsyncResult BeginLaunch (ProcessStartInfo info, AsyncCallback callback)
73 return BeginLaunch (info, callback, null);
76 public static IAsyncResult BeginLaunch (ProcessStartInfo info, AsyncCallback callback, LaunchOptions options)
79 throw new ArgumentNullException ("info");
81 Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
82 socket.Bind (new IPEndPoint (IPAddress.Loopback, 0));
84 IPEndPoint ep = (IPEndPoint) socket.LocalEndPoint;
86 // We need to inject our arguments into the psi
87 info.Arguments = string.Format ("{0} --debug --debugger-agent=transport=dt_socket,address={1}:{2}{3} {4}",
88 options == null || !options.Valgrind ? "" : info.FileName,
91 options == null || options.AgentArgs == null ? "" : "," + options.AgentArgs,
94 if (options != null && options.Valgrind)
95 info.FileName = "valgrind";
98 if (options != null && options.CustomProcessLauncher != null)
99 p = new ProcessWrapper (options.CustomProcessLauncher (info));
100 else if (options != null && options.CustomTargetProcessLauncher != null)
101 p = options.CustomTargetProcessLauncher (info);
103 p = new ProcessWrapper (Process.Start (info));
105 p.Exited += delegate (object sender, EventArgs eargs) {
109 LaunchCallback c = new LaunchCallback (LaunchInternal);
110 return c.BeginInvoke (p, info, socket, callback, socket);
113 public static VirtualMachine EndLaunch (IAsyncResult asyncResult) {
114 if (asyncResult == null)
115 throw new ArgumentNullException ("asyncResult");
117 if (!asyncResult.IsCompleted)
118 asyncResult.AsyncWaitHandle.WaitOne ();
120 AsyncResult result = (AsyncResult) asyncResult;
121 LaunchCallback cb = (LaunchCallback) result.AsyncDelegate;
122 return cb.EndInvoke (asyncResult);
125 public static VirtualMachine Launch (ProcessStartInfo info)
127 return Launch (info, null);
130 public static VirtualMachine Launch (ProcessStartInfo info, LaunchOptions options)
132 return EndLaunch (BeginLaunch (info, null, options));
135 public static VirtualMachine Launch (string[] args)
137 return Launch (args, null);
140 public static VirtualMachine Launch (string[] args, LaunchOptions options)
142 ProcessStartInfo pi = new ProcessStartInfo ("mono");
143 pi.Arguments = String.Join (" ", args);
145 return Launch (pi, options);
148 public static VirtualMachine ListenInternal (Socket dbg_sock, Socket con_sock) {
149 Socket con_acc = null;
150 Socket dbg_acc = null;
152 if (con_sock != null) {
154 con_acc = con_sock.Accept ();
155 } catch (Exception) {
164 dbg_acc = dbg_sock.Accept ();
165 } catch (Exception) {
166 if (con_sock != null) {
175 if (con_sock != null) {
176 if (con_sock.Connected)
177 con_sock.Disconnect (false);
181 if (dbg_sock.Connected)
182 dbg_sock.Disconnect (false);
185 Connection transport = new TcpConnection (dbg_acc);
186 StreamReader console = con_acc != null? new StreamReader (new NetworkStream (con_acc)) : null;
188 return Connect (transport, console, null);
191 public static IAsyncResult BeginListen (IPEndPoint dbg_ep, AsyncCallback callback) {
192 return BeginListen (dbg_ep, null, callback);
195 public static IAsyncResult BeginListen (IPEndPoint dbg_ep, IPEndPoint con_ep, AsyncCallback callback)
197 int dbg_port, con_port;
198 return BeginListen (dbg_ep, con_ep, callback, out dbg_port, out con_port);
201 public static IAsyncResult BeginListen (IPEndPoint dbg_ep, IPEndPoint con_ep, AsyncCallback callback,
202 out int dbg_port, out int con_port)
204 dbg_port = con_port = 0;
206 Socket dbg_sock = null;
207 Socket con_sock = null;
209 dbg_sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
210 dbg_sock.Bind (dbg_ep);
211 dbg_sock.Listen (1000);
212 dbg_port = ((IPEndPoint) dbg_sock.LocalEndPoint).Port;
214 if (con_ep != null) {
215 con_sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
216 con_sock.Bind (con_ep);
217 con_sock.Listen (1000);
218 con_port = ((IPEndPoint) con_sock.LocalEndPoint).Port;
221 ListenCallback c = new ListenCallback (ListenInternal);
222 return c.BeginInvoke (dbg_sock, con_sock, callback, con_sock ?? dbg_sock);
225 public static VirtualMachine EndListen (IAsyncResult asyncResult) {
226 if (asyncResult == null)
227 throw new ArgumentNullException ("asyncResult");
229 if (!asyncResult.IsCompleted)
230 asyncResult.AsyncWaitHandle.WaitOne ();
232 AsyncResult result = (AsyncResult) asyncResult;
233 ListenCallback cb = (ListenCallback) result.AsyncDelegate;
234 return cb.EndInvoke (asyncResult);
237 public static VirtualMachine Listen (IPEndPoint dbg_ep)
239 return Listen (dbg_ep, null);
242 public static VirtualMachine Listen (IPEndPoint dbg_ep, IPEndPoint con_ep)
244 return EndListen (BeginListen (dbg_ep, con_ep, null));
248 * Connect to a virtual machine listening at the specified address.
250 public static VirtualMachine Connect (IPEndPoint endpoint) {
251 return Connect (endpoint, null);
254 public static VirtualMachine Connect (IPEndPoint endpoint, IPEndPoint consoleEndpoint) {
255 if (endpoint == null)
256 throw new ArgumentNullException ("endpoint");
258 return EndConnect (BeginConnect (endpoint, consoleEndpoint, null));
261 public static VirtualMachine ConnectInternal (Socket dbg_sock, Socket con_sock, IPEndPoint dbg_ep, IPEndPoint con_ep) {
262 if (con_sock != null) {
264 con_sock.Connect (con_ep);
265 } catch (Exception) {
274 dbg_sock.Connect (dbg_ep);
275 } catch (Exception) {
276 if (con_sock != null) {
284 Connection transport = new TcpConnection (dbg_sock);
285 StreamReader console = con_sock != null? new StreamReader (new NetworkStream (con_sock)) : null;
287 return Connect (transport, console, null);
290 public static IAsyncResult BeginConnect (IPEndPoint dbg_ep, AsyncCallback callback) {
291 return BeginConnect (dbg_ep, null, callback);
294 public static IAsyncResult BeginConnect (IPEndPoint dbg_ep, IPEndPoint con_ep, AsyncCallback callback) {
295 Socket dbg_sock = null;
296 Socket con_sock = null;
298 dbg_sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
300 if (con_ep != null) {
301 con_sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
304 ConnectCallback c = new ConnectCallback (ConnectInternal);
305 return c.BeginInvoke (dbg_sock, con_sock, dbg_ep, con_ep, callback, con_sock ?? dbg_sock);
308 public static VirtualMachine EndConnect (IAsyncResult asyncResult) {
309 if (asyncResult == null)
310 throw new ArgumentNullException ("asyncResult");
312 if (!asyncResult.IsCompleted)
313 asyncResult.AsyncWaitHandle.WaitOne ();
315 AsyncResult result = (AsyncResult) asyncResult;
316 ConnectCallback cb = (ConnectCallback) result.AsyncDelegate;
317 return cb.EndInvoke (asyncResult);
320 public static void CancelConnection (IAsyncResult asyncResult)
322 ((Socket)asyncResult.AsyncState).Close ();
325 public static VirtualMachine Connect (Connection transport, StreamReader standardOutput, StreamReader standardError)
327 VirtualMachine vm = new VirtualMachine (null, transport);
329 vm.StandardOutput = standardOutput;
330 vm.StandardError = standardError;
332 transport.EventHandler = new EventHandler (vm);