Add unit test for AggregateException.GetBaseException that works on .net but is broke...
[mono.git] / mcs / class / Mono.Debugger.Soft / Mono.Debugger.Soft / VirtualMachineManager.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics;
4 using System.IO;
5 using System.Net;
6 using System.Net.Sockets;
7 using System.Runtime.Remoting.Messaging;
8
9 namespace Mono.Debugger.Soft
10 {
11         public class LaunchOptions {
12                 public string AgentArgs {
13                         get; set;
14                 }
15
16                 public bool Valgrind {
17                         get; set;
18                 }
19                 
20                 public ProcessLauncher CustomProcessLauncher {
21                         get; set;
22                 }
23
24                 public TargetProcessLauncher CustomTargetProcessLauncher {
25                         get; set;
26                 }
27
28                 public delegate Process ProcessLauncher (ProcessStartInfo info);
29                 public delegate ITargetProcess TargetProcessLauncher (ProcessStartInfo info);
30         }
31
32         public class VirtualMachineManager
33         {
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); 
37
38                 internal VirtualMachineManager () {
39                 }
40
41                 public static VirtualMachine LaunchInternal (Process p, ProcessStartInfo info, Socket socket)
42                 {
43                         return LaunchInternal (new ProcessWrapper (p), info, socket);
44                 }
45                         
46                 public static VirtualMachine LaunchInternal (ITargetProcess p, ProcessStartInfo info, Socket socket) {
47                         Socket accepted = null;
48                         try {
49                                 accepted = socket.Accept ();
50                         } catch (Exception) {
51                                 throw;
52                         }
53
54                         Connection conn = new TcpConnection (accepted);
55
56                         VirtualMachine vm = new VirtualMachine (p, conn);
57
58                         if (info.RedirectStandardOutput)
59                                 vm.StandardOutput = p.StandardOutput;
60                         
61                         if (info.RedirectStandardError)
62                                 vm.StandardError = p.StandardError;
63
64                         conn.EventHandler = new EventHandler (vm);
65
66                         vm.connect ();
67
68                         return vm;
69                 }
70
71                 public static IAsyncResult BeginLaunch (ProcessStartInfo info, AsyncCallback callback)
72                 {
73                         return BeginLaunch (info, callback, null);
74                 }
75
76                 public static IAsyncResult BeginLaunch (ProcessStartInfo info, AsyncCallback callback, LaunchOptions options)
77                 {
78                         if (info == null)
79                                 throw new ArgumentNullException ("info");
80
81                         Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
82                         socket.Bind (new IPEndPoint (IPAddress.Loopback, 0));
83                         socket.Listen (1000);
84                         IPEndPoint ep = (IPEndPoint) socket.LocalEndPoint;
85
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,
89                                                                 ep.Address,
90                                                                 ep.Port,
91                                                                 options == null || options.AgentArgs == null ? "" : "," + options.AgentArgs,
92                                                                 info.Arguments);
93
94                         if (options != null && options.Valgrind)
95                                 info.FileName = "valgrind";
96                                 
97                         ITargetProcess p;
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);
102                         else
103                                 p = new ProcessWrapper (Process.Start (info));
104                         
105                         p.Exited += delegate (object sender, EventArgs eargs) {
106                                 socket.Close ();
107                         };
108
109                         LaunchCallback c = new LaunchCallback (LaunchInternal);
110                         return c.BeginInvoke (p, info, socket, callback, socket);
111                 }
112
113                 public static VirtualMachine EndLaunch (IAsyncResult asyncResult) {
114                         if (asyncResult == null)
115                                 throw new ArgumentNullException ("asyncResult");
116
117                         if (!asyncResult.IsCompleted)
118                                 asyncResult.AsyncWaitHandle.WaitOne ();
119
120                         AsyncResult result = (AsyncResult) asyncResult;
121                         LaunchCallback cb = (LaunchCallback) result.AsyncDelegate;
122                         return cb.EndInvoke (asyncResult);
123                 }
124
125                 public static VirtualMachine Launch (ProcessStartInfo info)
126                 {
127                         return Launch (info, null);
128                 }
129
130                 public static VirtualMachine Launch (ProcessStartInfo info, LaunchOptions options)
131                 {
132                         return EndLaunch (BeginLaunch (info, null, options));
133                 }
134
135                 public static VirtualMachine Launch (string[] args)
136                 {
137                         return Launch (args, null);
138                 }
139
140                 public static VirtualMachine Launch (string[] args, LaunchOptions options)
141                 {
142                         ProcessStartInfo pi = new ProcessStartInfo ("mono");
143                         pi.Arguments = String.Join (" ", args);
144
145                         return Launch (pi, options);
146                 }
147                         
148                 public static VirtualMachine ListenInternal (Socket dbg_sock, Socket con_sock) {
149                         Socket con_acc = null;
150                         Socket dbg_acc = null;
151
152                         if (con_sock != null) {
153                                 try {
154                                         con_acc = con_sock.Accept ();
155                                 } catch (Exception) {
156                                         try {
157                                                 dbg_sock.Close ();
158                                         } catch {}
159                                         throw;
160                                 }
161                         }
162                                                 
163                         try {
164                                 dbg_acc = dbg_sock.Accept ();
165                         } catch (Exception) {
166                                 if (con_sock != null) {
167                                         try {
168                                                 con_sock.Close ();
169                                                 con_acc.Close ();
170                                         } catch {}
171                                 }
172                                 throw;
173                         }
174
175                         if (con_sock != null) {
176                                 if (con_sock.Connected)
177                                         con_sock.Disconnect (false);
178                                 con_sock.Close ();
179                         }
180
181                         if (dbg_sock.Connected)
182                                 dbg_sock.Disconnect (false);
183                         dbg_sock.Close ();
184
185                         Connection transport = new TcpConnection (dbg_acc);
186                         StreamReader console = con_acc != null? new StreamReader (new NetworkStream (con_acc)) : null;
187                         
188                         return Connect (transport, console, null);
189                 }
190
191                 public static IAsyncResult BeginListen (IPEndPoint dbg_ep, AsyncCallback callback) {
192                         return BeginListen (dbg_ep, null, callback);
193                 }
194                 
195                 public static IAsyncResult BeginListen (IPEndPoint dbg_ep, IPEndPoint con_ep, AsyncCallback callback)
196                 {
197                         int dbg_port, con_port;
198                         return BeginListen (dbg_ep, con_ep, callback, out dbg_port, out con_port);
199                 }
200
201                 public static IAsyncResult BeginListen (IPEndPoint dbg_ep, IPEndPoint con_ep, AsyncCallback callback,
202                         out int dbg_port, out int con_port)
203                 {
204                         dbg_port = con_port = 0;
205                         
206                         Socket dbg_sock = null;
207                         Socket con_sock = null;
208
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;
213
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;
219                         }
220                         
221                         ListenCallback c = new ListenCallback (ListenInternal);
222                         return c.BeginInvoke (dbg_sock, con_sock, callback, con_sock ?? dbg_sock);
223                 }
224
225                 public static VirtualMachine EndListen (IAsyncResult asyncResult) {
226                         if (asyncResult == null)
227                                 throw new ArgumentNullException ("asyncResult");
228
229                         if (!asyncResult.IsCompleted)
230                                 asyncResult.AsyncWaitHandle.WaitOne ();
231
232                         AsyncResult result = (AsyncResult) asyncResult;
233                         ListenCallback cb = (ListenCallback) result.AsyncDelegate;
234                         return cb.EndInvoke (asyncResult);
235                 }
236
237                 public static VirtualMachine Listen (IPEndPoint dbg_ep)
238                 {
239                         return Listen (dbg_ep, null);
240                 }
241
242                 public static VirtualMachine Listen (IPEndPoint dbg_ep, IPEndPoint con_ep)
243                 {
244                         return EndListen (BeginListen (dbg_ep, con_ep, null));
245                 }
246
247                 /*
248                  * Connect to a virtual machine listening at the specified address.
249                  */
250                 public static VirtualMachine Connect (IPEndPoint endpoint) {
251                         return Connect (endpoint, null);
252                 }
253
254                 public static VirtualMachine Connect (IPEndPoint endpoint, IPEndPoint consoleEndpoint) { 
255                         if (endpoint == null)
256                                 throw new ArgumentNullException ("endpoint");
257
258                         return EndConnect (BeginConnect (endpoint, consoleEndpoint, null));
259                 }
260
261                 public static VirtualMachine ConnectInternal (Socket dbg_sock, Socket con_sock, IPEndPoint dbg_ep, IPEndPoint con_ep) {
262                         if (con_sock != null) {
263                                 try {
264                                         con_sock.Connect (con_ep);
265                                 } catch (Exception) {
266                                         try {
267                                                 dbg_sock.Close ();
268                                         } catch {}
269                                         throw;
270                                 }
271                         }
272                                                 
273                         try {
274                                 dbg_sock.Connect (dbg_ep);
275                         } catch (Exception) {
276                                 if (con_sock != null) {
277                                         try {
278                                                 con_sock.Close ();
279                                         } catch {}
280                                 }
281                                 throw;
282                         }
283                         
284                         Connection transport = new TcpConnection (dbg_sock);
285                         StreamReader console = con_sock != null? new StreamReader (new NetworkStream (con_sock)) : null;
286                         
287                         return Connect (transport, console, null);
288                 }
289
290                 public static IAsyncResult BeginConnect (IPEndPoint dbg_ep, AsyncCallback callback) {
291                         return BeginConnect (dbg_ep, null, callback);
292                 }
293
294                 public static IAsyncResult BeginConnect (IPEndPoint dbg_ep, IPEndPoint con_ep, AsyncCallback callback) {
295                         Socket dbg_sock = null;
296                         Socket con_sock = null;
297
298                         dbg_sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
299
300                         if (con_ep != null) {
301                                 con_sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
302                         }
303                         
304                         ConnectCallback c = new ConnectCallback (ConnectInternal);
305                         return c.BeginInvoke (dbg_sock, con_sock, dbg_ep, con_ep, callback, con_sock ?? dbg_sock);
306                 }
307
308                 public static VirtualMachine EndConnect (IAsyncResult asyncResult) {
309                         if (asyncResult == null)
310                                 throw new ArgumentNullException ("asyncResult");
311
312                         if (!asyncResult.IsCompleted)
313                                 asyncResult.AsyncWaitHandle.WaitOne ();
314
315                         AsyncResult result = (AsyncResult) asyncResult;
316                         ConnectCallback cb = (ConnectCallback) result.AsyncDelegate;
317                         return cb.EndInvoke (asyncResult);
318                 }
319
320                 public static void CancelConnection (IAsyncResult asyncResult)
321                 {
322                         ((Socket)asyncResult.AsyncState).Close ();
323                 }
324                 
325                 public static VirtualMachine Connect (Connection transport, StreamReader standardOutput, StreamReader standardError)
326                 {
327                         VirtualMachine vm = new VirtualMachine (null, transport);
328                         
329                         vm.StandardOutput = standardOutput;
330                         vm.StandardError = standardError;
331                         
332                         transport.EventHandler = new EventHandler (vm);
333
334                         vm.connect ();
335
336                         return vm;
337                 }
338         }
339 }