2 using System.Collections.Generic;
3 using System.Runtime.Remoting.Messaging;
4 using System.Threading;
6 using System.Threading.Tasks;
9 namespace Mono.Debugger.Soft
11 public class ObjectMirror : Value {
13 AppDomainMirror domain;
15 internal ObjectMirror (VirtualMachine vm, long id) : base (vm, id) {
18 internal ObjectMirror (VirtualMachine vm, long id, TypeMirror type, AppDomainMirror domain) : base (vm, id) {
24 var info = vm.conn.Object_GetInfo (id);
25 type = vm.GetType (info.type_id);
26 domain = vm.GetDomain (info.domain_id);
29 public TypeMirror Type {
32 if (vm.conn.Version.AtLeast (2, 5))
35 type = vm.GetType (vm.conn.Object_GetType (id));
41 public AppDomainMirror Domain {
44 if (vm.conn.Version.AtLeast (2, 5))
47 domain = vm.GetDomain (vm.conn.Object_GetDomain (id));
53 public bool IsCollected {
55 return vm.conn.Object_IsCollected (id);
59 public Value GetValue (FieldInfoMirror field) {
60 return GetValues (new FieldInfoMirror [] { field }) [0];
63 public Value[] GetValues (IList<FieldInfoMirror> fields) {
65 throw new ArgumentNullException ("fields");
66 foreach (FieldInfoMirror f in fields) {
68 throw new ArgumentNullException ("field");
71 long[] ids = new long [fields.Count];
72 for (int i = 0; i < fields.Count; ++i)
73 ids [i] = fields [i].Id;
75 return vm.DecodeValues (vm.conn.Object_GetValues (id, ids));
76 } catch (CommandException ex) {
77 if (ex.ErrorCode == ErrorCode.INVALID_FIELDID)
78 throw new ArgumentException ("One of the fields is not valid for this type.", "fields");
84 public void SetValues (IList<FieldInfoMirror> fields, Value[] values) {
86 throw new ArgumentNullException ("fields");
88 throw new ArgumentNullException ("values");
89 foreach (FieldInfoMirror f in fields) {
91 throw new ArgumentNullException ("field");
94 foreach (Value v in values) {
96 throw new ArgumentNullException ("values");
99 long[] ids = new long [fields.Count];
100 for (int i = 0; i < fields.Count; ++i)
101 ids [i] = fields [i].Id;
103 vm.conn.Object_SetValues (id, ids, vm.EncodeValues (values));
104 } catch (CommandException ex) {
105 if (ex.ErrorCode == ErrorCode.INVALID_FIELDID)
106 throw new ArgumentException ("One of the fields is not valid for this type.", "fields");
107 else if (ex.ErrorCode == ErrorCode.INVALID_ARGUMENT)
108 throw new ArgumentException ("One of the values is not valid for its field.", "values");
114 public void SetValue (FieldInfoMirror field, Value value) {
115 SetValues (new FieldInfoMirror [] { field }, new Value [] { value });
119 * The current address of the object. It can change during garbage
120 * collections. Use a long since the debuggee might have a different
123 public long Address {
125 return vm.conn.Object_GetAddress (id);
129 public Value InvokeMethod (ThreadMirror thread, MethodMirror method, IList<Value> arguments) {
130 return InvokeMethod (vm, thread, method, this, arguments, InvokeOptions.None);
133 public Value InvokeMethod (ThreadMirror thread, MethodMirror method, IList<Value> arguments, InvokeOptions options) {
134 return InvokeMethod (vm, thread, method, this, arguments, options);
137 [Obsolete ("Use the overload without the 'vm' argument")]
138 public IAsyncResult BeginInvokeMethod (VirtualMachine vm, ThreadMirror thread, MethodMirror method, IList<Value> arguments, InvokeOptions options, AsyncCallback callback, object state) {
139 return BeginInvokeMethod (vm, thread, method, this, arguments, options, callback, state);
142 public IAsyncResult BeginInvokeMethod (ThreadMirror thread, MethodMirror method, IList<Value> arguments, InvokeOptions options, AsyncCallback callback, object state) {
143 return BeginInvokeMethod (vm, thread, method, this, arguments, options, callback, state);
146 public Value EndInvokeMethod (IAsyncResult asyncResult) {
147 return EndInvokeMethodInternal (asyncResult);
151 public Task<Value> InvokeMethodAsync (ThreadMirror thread, MethodMirror method, IList<Value> arguments, InvokeOptions options = InvokeOptions.None) {
152 var tcs = new TaskCompletionSource<Value> ();
153 BeginInvokeMethod (thread, method, arguments, options, iar =>
156 tcs.SetResult (EndInvokeMethod (iar));
157 } catch (OperationCanceledException) {
158 tcs.TrySetCanceled ();
159 } catch (Exception ex) {
160 tcs.TrySetException (ex);
168 // Invoke the members of METHODS one-by-one, calling CALLBACK after each invoke was finished. The IAsyncResult will be marked as completed after all invokes have
169 // finished. The callback will be called with a different IAsyncResult that represents one method invocation.
170 // From protocol version 2.22.
172 public IAsyncResult BeginInvokeMultiple (ThreadMirror thread, MethodMirror[] methods, IList<IList<Value>> arguments, InvokeOptions options, AsyncCallback callback, object state) {
173 return BeginInvokeMultiple (vm, thread, methods, this, arguments, options, callback, state);
176 public void EndInvokeMultiple (IAsyncResult asyncResult) {
177 EndInvokeMultipleInternal (asyncResult);
181 * Common implementation for invokes
184 class InvokeAsyncResult : IInvokeAsyncResult {
186 public object AsyncState {
190 public WaitHandle AsyncWaitHandle {
194 public bool CompletedSynchronously {
200 public bool IsCompleted {
204 public AsyncCallback Callback {
208 public ErrorCode ErrorCode {
212 public VirtualMachine VM {
216 public ThreadMirror Thread {
220 public ValueImpl Value {
224 public ValueImpl Exception {
232 public bool IsMultiple {
236 public int NumPending;
240 if (ID == 0) // Ooops
243 ObjectMirror.AbortInvoke (VM, Thread, ID);
247 internal static IInvokeAsyncResult BeginInvokeMethod (VirtualMachine vm, ThreadMirror thread, MethodMirror method, Value this_obj, IList<Value> arguments, InvokeOptions options, AsyncCallback callback, object state) {
249 throw new ArgumentNullException ("thread");
251 throw new ArgumentNullException ("method");
252 if (arguments == null)
253 arguments = new Value [0];
255 InvokeFlags f = InvokeFlags.NONE;
257 if ((options & InvokeOptions.DisableBreakpoints) != 0)
258 f |= InvokeFlags.DISABLE_BREAKPOINTS;
259 if ((options & InvokeOptions.SingleThreaded) != 0)
260 f |= InvokeFlags.SINGLE_THREADED;
262 InvokeAsyncResult r = new InvokeAsyncResult { AsyncState = state, AsyncWaitHandle = new ManualResetEvent (false), VM = vm, Thread = thread, Callback = callback };
264 r.ID = vm.conn.VM_BeginInvokeMethod (thread.Id, method.Id, this_obj != null ? vm.EncodeValue (this_obj) : vm.EncodeValue (vm.CreateValue (null)), vm.EncodeValues (arguments), f, InvokeCB, r);
269 // This is called when the result of an invoke is received
270 static void InvokeCB (ValueImpl v, ValueImpl exc, ErrorCode error, object state) {
271 InvokeAsyncResult r = (InvokeAsyncResult)state;
280 r.IsCompleted = true;
281 ((ManualResetEvent)r.AsyncWaitHandle).Set ();
283 if (r.Callback != null)
284 r.Callback.BeginInvoke (r, null, null);
287 internal static Value EndInvokeMethodInternal (IAsyncResult asyncResult) {
288 if (asyncResult == null)
289 throw new ArgumentNullException ("asyncResult");
291 InvokeAsyncResult r = (InvokeAsyncResult)asyncResult;
294 r.AsyncWaitHandle.WaitOne ();
296 if (r.ErrorCode != 0) {
298 r.VM.ErrorHandler (null, new ErrorHandlerEventArgs () { ErrorCode = r.ErrorCode });
299 } catch (CommandException ex) {
300 if (ex.ErrorCode == ErrorCode.INVALID_ARGUMENT)
301 throw new ArgumentException ("Incorrect number or types of arguments", "arguments");
305 throw new NotImplementedException ();
307 if (r.Exception != null)
308 throw new InvocationException ((ObjectMirror)r.VM.DecodeValue (r.Exception));
310 return r.VM.DecodeValue (r.Value);
314 internal static void EndInvokeMultipleInternal (IAsyncResult asyncResult) {
315 if (asyncResult == null)
316 throw new ArgumentNullException ("asyncResult");
318 InvokeAsyncResult r = (InvokeAsyncResult)asyncResult;
321 r.AsyncWaitHandle.WaitOne ();
324 internal static Value InvokeMethod (VirtualMachine vm, ThreadMirror thread, MethodMirror method, Value this_obj, IList<Value> arguments, InvokeOptions options) {
325 return EndInvokeMethodInternal (BeginInvokeMethod (vm, thread, method, this_obj, arguments, options, null, null));
328 internal static void AbortInvoke (VirtualMachine vm, ThreadMirror thread, int id)
330 vm.conn.VM_AbortInvoke (thread.Id, id);
334 // Implementation of InvokeMultiple
337 internal static IInvokeAsyncResult BeginInvokeMultiple (VirtualMachine vm, ThreadMirror thread, MethodMirror[] methods, Value this_obj, IList<IList<Value>> arguments, InvokeOptions options, AsyncCallback callback, object state) {
339 throw new ArgumentNullException ("thread");
341 throw new ArgumentNullException ("methods");
342 foreach (var m in methods)
344 throw new ArgumentNullException ("method");
345 if (arguments == null) {
346 arguments = new List<IList<Value>> ();
347 for (int i = 0; i < methods.Length; ++i)
348 arguments.Add (new Value [0]);
350 // FIXME: Not needed for property evaluation
351 throw new NotImplementedException ();
353 if (callback == null)
354 throw new ArgumentException ("A callback argument is required for this method.", "callback");
356 InvokeFlags f = InvokeFlags.NONE;
358 if ((options & InvokeOptions.DisableBreakpoints) != 0)
359 f |= InvokeFlags.DISABLE_BREAKPOINTS;
360 if ((options & InvokeOptions.SingleThreaded) != 0)
361 f |= InvokeFlags.SINGLE_THREADED;
363 InvokeAsyncResult r = new InvokeAsyncResult { AsyncState = state, AsyncWaitHandle = new ManualResetEvent (false), VM = vm, Thread = thread, Callback = callback, NumPending = methods.Length, IsMultiple = true };
365 var mids = new long [methods.Length];
366 for (int i = 0; i < methods.Length; ++i)
367 mids [i] = methods [i].Id;
368 var args = new List<ValueImpl[]> ();
369 for (int i = 0; i < methods.Length; ++i)
370 args.Add (vm.EncodeValues (arguments [i]));
371 r.ID = vm.conn.VM_BeginInvokeMethods (thread.Id, mids, this_obj != null ? vm.EncodeValue (this_obj) : vm.EncodeValue (vm.CreateValue (null)), args, f, InvokeMultipleCB, r);
376 // This is called when the result of an invoke is received
377 static void InvokeMultipleCB (ValueImpl v, ValueImpl exc, ErrorCode error, object state) {
378 var r = (InvokeAsyncResult)state;
380 Interlocked.Decrement (ref r.NumPending);
382 if (r.NumPending == 0) {
383 r.IsCompleted = true;
384 ((ManualResetEvent)r.AsyncWaitHandle).Set ();
387 // Have to pass another asyncresult to the callback since multiple threads can execute it concurrently with results of multiple invocations
388 var r2 = new InvokeAsyncResult { AsyncState = r.AsyncState, AsyncWaitHandle = null, VM = r.VM, Thread = r.Thread, Callback = r.Callback, IsCompleted = true };
391 r2.ErrorCode = error;
397 r.Callback.BeginInvoke (r2, null, null);