2 using System.Collections.Generic;
3 using System.Threading;
5 using System.Reflection;
8 using Mono.Debugger.Soft;
9 using Diag = System.Diagnostics;
12 using System.Security.Cryptography;
14 using NUnit.Framework;
16 #pragma warning disable 0219
22 public class DebuggerTests
25 MethodMirror entry_point;
26 StepEventRequest step_req;
29 void AssertThrows<ExType> (Action del) where ExType : Exception {
37 Assert.IsTrue (thrown);
40 // No other way to pass arguments to the tests ?
41 public static bool listening = Environment.GetEnvironmentVariable ("DBG_SUSPEND") != null;
42 public static string runtime = Environment.GetEnvironmentVariable ("DBG_RUNTIME");
43 public static string agent_args = Environment.GetEnvironmentVariable ("DBG_AGENT_ARGS");
45 Event GetNextEvent () {
46 var es = vm.GetNextEventSet ();
47 Assert.AreEqual (1, es.Events.Length);
51 void Start (params string[] args) {
55 void Start (bool forceExit, params string[] args) {
56 this.forceExit = forceExit;
59 var pi = new Diag.ProcessStartInfo ();
62 pi.FileName = runtime;
65 pi.Arguments = String.Join (" ", args);
66 vm = VirtualMachineManager.Launch (pi, new LaunchOptions { AgentArgs = agent_args });
68 var ep = new IPEndPoint (IPAddress.Any, 10000);
69 Console.WriteLine ("Listening on " + ep + "...");
70 vm = VirtualMachineManager.Listen (ep);
73 var load_req = vm.CreateAssemblyLoadRequest ();
76 Event vmstart = GetNextEvent ();
77 Assert.AreEqual (EventType.VMStart, vmstart.EventType);
86 /* Find out the entry point */
90 if (e is AssemblyLoadEvent) {
91 AssemblyLoadEvent ae = (AssemblyLoadEvent)e;
92 entry_point = ae.Assembly.EntryPoint;
93 if (entry_point != null)
103 BreakpointEvent run_until (string name) {
105 MethodMirror m = entry_point.DeclaringType.GetMethod (name);
106 Assert.IsNotNull (m);
107 //Console.WriteLine ("X: " + name + " " + m.ILOffsets.Count + " " + m.Locations.Count);
108 var req = vm.SetBreakpoint (m, m.ILOffsets [0]);
115 if (e is BreakpointEvent)
121 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
122 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
124 return (e as BreakpointEvent);
127 Event single_step (ThreadMirror t) {
128 var req = vm.CreateStepRequest (t);
132 Event e = GetNextEvent ();
133 Assert.IsTrue (e is StepEvent);
140 Event step_until (ThreadMirror t, string method_name) {
144 if ((e as StepEvent).Method.Name == method_name)
150 void check_arg_val (StackFrame frame, int pos, Type type, object eval) {
151 object val = frame.GetArgument (pos);
152 Assert.IsTrue (val is PrimitiveValue);
153 object v = (val as PrimitiveValue).Value;
154 Assert.AreEqual (type, v.GetType ());
156 Assert.IsTrue (Math.Abs ((float)eval - (float)v) < 0.0001);
157 else if (eval is double)
158 Assert.IsTrue (Math.Abs ((double)eval - (double)v) < 0.0001);
160 Assert.AreEqual (eval, v);
163 void AssertValue (object expected, object val) {
164 if (expected is string) {
165 Assert.IsTrue (val is StringMirror);
166 Assert.AreEqual (expected, (val as StringMirror).Value);
167 } else if (val is StructMirror && (val as StructMirror).Type.Name == "IntPtr") {
168 AssertValue (expected, (val as StructMirror).Fields [0]);
170 Assert.IsTrue (val is PrimitiveValue);
171 Assert.AreEqual (expected, (val as PrimitiveValue).Value);
176 public void SetUp () {
177 ThreadMirror.NativeTransitions = false;
178 Start (new string [] { "dtest-app.exe" });
182 public void TearDown () {
186 if (step_req != null)
194 Event e = GetNextEvent ();
196 if (e is VMDeathEvent)
205 public void SimpleBreakpoint () {
208 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp1");
209 Assert.IsNotNull (m);
211 vm.SetBreakpoint (m, 0);
216 Assert.AreEqual (EventType.Breakpoint, e.EventType);
217 Assert.IsTrue (e is BreakpointEvent);
218 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
221 AssertThrows<ArgumentException> (delegate {
223 vm.SetBreakpoint (m, 2);
228 public void BreakpointsSameLocation () {
229 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp2");
230 Assert.IsNotNull (m);
232 vm.SetBreakpoint (m, 0);
233 vm.SetBreakpoint (m, 0);
237 var es = vm.GetNextEventSet ();
238 Assert.AreEqual (2, es.Events.Length);
239 Assert.IsTrue (es [0] is BreakpointEvent);
240 Assert.AreEqual (m, (es [0] as BreakpointEvent).Method);
242 Assert.IsTrue (es [1] is BreakpointEvent);
243 Assert.AreEqual (m.Name, (es [1] as BreakpointEvent).Method.Name);
247 public void BreakpointAlreadyJITted () {
248 Event e = run_until ("bp1");
250 /* Place a breakpoint on bp3 */
251 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp3");
252 Assert.IsNotNull (m);
253 vm.SetBreakpoint (m, 0);
255 /* Same with generic instances */
256 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp7");
257 Assert.IsNotNull (m2);
258 vm.SetBreakpoint (m2, 0);
263 Assert.AreEqual (EventType.Breakpoint, e.EventType);
264 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
268 /* Non-shared instance */
270 Assert.AreEqual (EventType.Breakpoint, e.EventType);
271 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
275 /* Shared instance */
277 Assert.AreEqual (EventType.Breakpoint, e.EventType);
278 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
282 public void ClearBreakpoint () {
285 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
286 Assert.IsNotNull (m);
287 EventRequest req1 = vm.SetBreakpoint (m, 0);
288 EventRequest req2 = vm.SetBreakpoint (m, 0);
290 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
291 Assert.IsNotNull (m2);
292 vm.SetBreakpoint (m2, 0);
297 var es = vm.GetNextEventSet ();
298 Assert.AreEqual (2, es.Events.Length);
299 Assert.AreEqual (EventType.Breakpoint, es [0].EventType);
300 Assert.AreEqual (m.Name, (es [0] as BreakpointEvent).Method.Name);
301 Assert.AreEqual (EventType.Breakpoint, es [1].EventType);
302 Assert.AreEqual (m.Name, (es [1] as BreakpointEvent).Method.Name);
304 /* Clear one of them */
310 Assert.AreEqual (EventType.Breakpoint, e.EventType);
311 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
313 /* Clear the other */
319 Assert.AreEqual (EventType.Breakpoint, e.EventType);
320 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
324 public void ClearAllBreakpoints () {
327 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
328 Assert.IsNotNull (m);
329 vm.SetBreakpoint (m, 0);
331 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
332 Assert.IsNotNull (m2);
333 vm.SetBreakpoint (m2, 0);
335 vm.ClearAllBreakpoints ();
340 Assert.IsTrue (!(e is BreakpointEvent));
341 if (e is VMDeathEvent)
346 public void BreakpointOnGShared () {
349 MethodMirror m = entry_point.DeclaringType.GetMethod ("bp6");
350 Assert.IsNotNull (m);
352 vm.SetBreakpoint (m, 0);
357 Assert.AreEqual (EventType.Breakpoint, e.EventType);
358 Assert.IsTrue (e is BreakpointEvent);
359 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
361 // Breakpoint on an open generic method of a closed generic class (#3422)
362 var frame = e.Thread.GetFrames ()[0];
363 var ginst = frame.GetValue (frame.Method.GetLocal ("gc"));
364 var m2 = (ginst as ObjectMirror).Type.GetMethod ("bp");
365 vm.SetBreakpoint (m2, 0);
370 Assert.AreEqual (EventType.Breakpoint, e.EventType);
371 Assert.IsTrue (e is BreakpointEvent);
372 Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
375 void assert_location (Event e, string method) {
376 Assert.IsTrue (e is StepEvent);
377 Assert.AreEqual (method, (e as StepEvent).Method.Name);
380 StepEventRequest create_step (Event e) {
381 var req = vm.CreateStepRequest (e.Thread);
387 public void SingleStepping () {
388 Event e = run_until ("single_stepping");
390 var req = create_step (e);
393 // Step over 'bool b = true'
395 assert_location (e, "single_stepping");
402 assert_location (e, "ss1");
409 assert_location (e, "single_stepping");
413 assert_location (e, "single_stepping");
417 assert_location (e, "ss3");
419 // Step back into single_stepping
421 assert_location (e, "single_stepping");
423 // Step into ss3_2 ()
425 assert_location (e, "ss3_2");
427 // Step over ss3_2_2 ()
429 assert_location (e, "ss3_2");
431 // Recreate the request
438 // Step back into single_stepping () with the new request
440 assert_location (e, "single_stepping");
444 assert_location (e, "ss4");
449 // Change to StepSize.Line
451 req.Depth = StepDepth.Over;
452 req.Size = StepSize.Line;
455 // Step over ss1 (); ss1 ();
460 req.Depth = StepDepth.Into;
464 assert_location (e, "ss2");
469 e = run_until ("ss5");
471 // Add an assembly filter
472 req.AssemblyFilter = new AssemblyMirror [] { (e as BreakpointEvent).Method.DeclaringType.Assembly };
478 // Step into is_even, skipping the linq stuff
480 assert_location (e, "is_even");
482 // FIXME: Check that single stepping works with lock (obj)
486 e = run_until ("ss6");
488 req = create_step (e);
489 req.Depth = StepDepth.Over;
492 // Check that single stepping works in out-of-line bblocks
495 assert_location (e, "ss6");
498 // Check that a step over stops at an EH clause
499 e = run_until ("ss7_2");
500 req = create_step (e);
501 req.Depth = StepDepth.Out;
504 assert_location (e, "ss7");
506 req = create_step (e);
507 req.Depth = StepDepth.Over;
510 assert_location (e, "ss7");
513 // Check that stepping stops between nested calls
514 e = run_until ("ss_nested_2");
516 assert_location (e, "ss_nested");
518 assert_location (e, "ss_nested_1");
520 assert_location (e, "ss_nested");
521 // Check that step over steps over nested calls
523 assert_location (e, "ss_nested");
525 assert_location (e, "ss_nested_3");
528 // Check DebuggerStepThrough support
529 e = run_until ("ss_step_through");
530 req = create_step (e);
531 req.Filter = StepFilter.DebuggerStepThrough;
533 // Step through step_through_1 ()
535 assert_location (e, "ss_step_through");
536 // Step through StepThroughClass.step_through_2 ()
538 assert_location (e, "ss_step_through");
540 req.Filter = StepFilter.None;
542 assert_location (e, "step_through_3");
545 // Check DebuggerNonUserCode support
546 e = run_until ("ss_non_user_code");
547 req = create_step (e);
548 req.Filter = StepFilter.DebuggerNonUserCode;
550 // Step through non_user_code_1 ()
552 assert_location (e, "ss_non_user_code");
553 // Step through StepThroughClass.non_user_code_2 ()
555 assert_location (e, "ss_non_user_code");
557 req.Filter = StepFilter.None;
559 assert_location (e, "non_user_code_3");
562 // Check that step-over doesn't stop at inner frames with recursive functions
563 e = run_until ("ss_recursive");
564 req = create_step (e);
568 var f = e.Thread.GetFrames () [0];
569 assert_location (e, "ss_recursive");
570 AssertValue (1, f.GetValue (f.Method.GetLocal ("n")));
573 // Check that single stepping doesn't clobber fp values
574 e = run_until ("ss_fp_clobber");
575 req = create_step (e);
577 f = e.Thread.GetFrames ()[0];
579 if ((e as StepEvent).Method.Name == "ss_fp_clobber_2")
583 f = e.Thread.GetFrames ()[0];
584 AssertValue (7.0, f.GetValue (f.Method.GetParameters ()[0]));
589 public void MethodEntryExit () {
590 run_until ("single_stepping");
592 var req1 = vm.CreateMethodEntryRequest ();
593 var req2 = vm.CreateMethodExitRequest ();
599 Event e = GetNextEvent ();
600 Assert.IsTrue (e is MethodEntryEvent);
601 Assert.AreEqual ("ss1", (e as MethodEntryEvent).Method.Name);
605 Assert.IsTrue (e is MethodExitEvent);
606 Assert.AreEqual ("ss1", (e as MethodExitEvent).Method.Name);
613 public void CountFilter () {
614 run_until ("single_stepping");
616 MethodMirror m2 = entry_point.DeclaringType.GetMethod ("ss3");
617 Assert.IsNotNull (m2);
618 vm.SetBreakpoint (m2, 0);
620 var req1 = vm.CreateMethodEntryRequest ();
624 // Enter ss2, ss1 is skipped
626 Event e = GetNextEvent ();
627 Assert.IsTrue (e is MethodEntryEvent);
628 Assert.AreEqual ("ss2", (e as MethodEntryEvent).Method.Name);
630 // Breakpoint on ss3, the entry event is no longer reported
633 Assert.IsTrue (e is BreakpointEvent);
639 public void Arguments () {
642 var e = run_until ("arg1");
644 StackFrame frame = e.Thread.GetFrames () [0];
646 check_arg_val (frame, 0, typeof (sbyte), SByte.MaxValue - 5);
647 check_arg_val (frame, 1, typeof (byte), Byte.MaxValue - 5);
648 check_arg_val (frame, 2, typeof (bool), true);
649 check_arg_val (frame, 3, typeof (short), Int16.MaxValue - 5);
650 check_arg_val (frame, 4, typeof (ushort), UInt16.MaxValue - 5);
651 check_arg_val (frame, 5, typeof (char), 'F');
652 check_arg_val (frame, 6, typeof (int), Int32.MaxValue - 5);
653 check_arg_val (frame, 7, typeof (uint), UInt32.MaxValue - 5);
654 check_arg_val (frame, 8, typeof (long), Int64.MaxValue - 5);
655 check_arg_val (frame, 9, typeof (ulong), UInt64.MaxValue - 5);
656 check_arg_val (frame, 10, typeof (float), 1.2345f);
657 check_arg_val (frame, 11, typeof (double), 6.78910);
659 e = run_until ("arg2");
661 frame = e.Thread.GetFrames () [0];
664 val = frame.GetArgument (0);
665 AssertValue ("FOO", val);
666 Assert.AreEqual ("String", (val as ObjectMirror).Type.Name);
669 val = frame.GetArgument (1);
670 AssertValue (null, val);
673 val = frame.GetArgument (2);
674 AssertValue ("BLA", val);
677 val = frame.GetArgument (3);
678 AssertValue (42, val);
681 val = frame.GetArgument (4);
682 Assert.IsTrue (val is ObjectMirror);
683 Assert.AreEqual ("GClass`1", (val as ObjectMirror).Type.Name);
686 val = frame.GetArgument (5);
687 Assert.IsTrue (val is ObjectMirror);
688 Assert.AreEqual ("Object", (val as ObjectMirror).Type.Name);
690 // this on static methods
691 val = frame.GetThis ();
692 AssertValue (null, val);
694 e = run_until ("arg3");
696 frame = e.Thread.GetFrames () [0];
699 val = frame.GetThis ();
700 Assert.IsTrue (val is ObjectMirror);
701 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
703 // objref in register
704 val = frame.GetArgument (0);
705 AssertValue ("BLA", val);
709 public void Arrays () {
712 var e = run_until ("o2");
714 StackFrame frame = e.Thread.GetFrames () [0];
717 val = frame.GetArgument (0);
718 Assert.IsTrue (val is ArrayMirror);
719 ArrayMirror arr = val as ArrayMirror;
720 Assert.AreEqual (2, arr.Length);
721 AssertValue ("BAR", arr [0]);
722 AssertValue ("BAZ", arr [1]);
724 var vals = arr.GetValues (0, 2);
725 Assert.AreEqual (2, vals.Count);
726 AssertValue ("BAR", vals [0]);
727 AssertValue ("BAZ", vals [1]);
729 arr [0] = vm.RootDomain.CreateString ("ABC");
730 AssertValue ("ABC", arr [0]);
732 arr [0] = vm.CreateValue (null);
733 AssertValue (null, arr [0]);
735 arr.SetValues (0, new Value [] { vm.RootDomain.CreateString ("D1"), vm.RootDomain.CreateString ("D2") });
736 AssertValue ("D1", arr [0]);
737 AssertValue ("D2", arr [1]);
740 val = frame.GetArgument (1);
741 Assert.IsTrue (val is ArrayMirror);
742 arr = val as ArrayMirror;
743 Assert.AreEqual (2, arr.Length);
744 AssertValue (42, arr [0]);
745 AssertValue (43, arr [1]);
748 AssertThrows<IndexOutOfRangeException> (delegate () {
752 AssertThrows<IndexOutOfRangeException> (delegate () {
753 val = arr [Int32.MinValue];
756 AssertThrows<IndexOutOfRangeException> (delegate () {
757 vals = arr.GetValues (0, 3);
760 AssertThrows<IndexOutOfRangeException> (delegate () {
761 arr [2] = vm.CreateValue (null);
764 AssertThrows<IndexOutOfRangeException> (delegate () {
765 arr [Int32.MinValue] = vm.CreateValue (null);
768 AssertThrows<IndexOutOfRangeException> (delegate () {
769 arr.SetValues (0, new Value [] { null, null, null });
773 val = frame.GetArgument (2);
774 Assert.IsTrue (val is ArrayMirror);
775 arr = val as ArrayMirror;
776 Assert.AreEqual (2, arr.Rank);
777 Assert.AreEqual (4, arr.Length);
778 Assert.AreEqual (2, arr.GetLength (0));
779 Assert.AreEqual (2, arr.GetLength (1));
780 Assert.AreEqual (0, arr.GetLowerBound (0));
781 Assert.AreEqual (0, arr.GetLowerBound (1));
782 vals = arr.GetValues (0, 4);
783 AssertValue (1, vals [0]);
784 AssertValue (2, vals [1]);
785 AssertValue (3, vals [2]);
786 AssertValue (4, vals [3]);
788 val = frame.GetArgument (3);
789 Assert.IsTrue (val is ArrayMirror);
790 arr = val as ArrayMirror;
791 Assert.AreEqual (2, arr.Rank);
792 Assert.AreEqual (4, arr.Length);
793 Assert.AreEqual (2, arr.GetLength (0));
794 Assert.AreEqual (2, arr.GetLength (1));
795 Assert.AreEqual (1, arr.GetLowerBound (0));
796 Assert.AreEqual (3, arr.GetLowerBound (1));
798 AssertThrows<ArgumentOutOfRangeException> (delegate () {
801 AssertThrows<ArgumentOutOfRangeException> (delegate () {
805 AssertThrows<ArgumentOutOfRangeException> (delegate () {
806 arr.GetLowerBound (-1);
808 AssertThrows<ArgumentOutOfRangeException> (delegate () {
809 arr.GetLowerBound (2);
812 // arrays treated as generic collections
813 val = frame.GetArgument (4);
814 Assert.IsTrue (val is ArrayMirror);
815 arr = val as ArrayMirror;
819 public void Object_GetValue () {
820 var e = run_until ("o1");
821 var frame = e.Thread.GetFrames () [0];
823 object val = frame.GetThis ();
824 Assert.IsTrue (val is ObjectMirror);
825 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
826 ObjectMirror o = (val as ObjectMirror);
828 TypeMirror t = o.Type;
831 object f = o.GetValue (t.GetField ("field_i"));
833 f = o.GetValue (t.GetField ("field_s"));
834 AssertValue ("S", f);
835 f = o.GetValue (t.GetField ("field_enum"));
836 Assert.IsTrue (f is EnumMirror);
837 Assert.AreEqual (1, (f as EnumMirror).Value);
838 Assert.AreEqual ("B", (f as EnumMirror).StringValue);
840 // Inherited object fields
841 TypeMirror parent = t.BaseType;
842 f = o.GetValue (parent.GetField ("base_field_i"));
844 f = o.GetValue (parent.GetField ("base_field_s"));
845 AssertValue ("T", f);
848 f = o.GetValue (o.Type.GetField ("static_i"));
852 ObjectMirror o2 = frame.GetValue (frame.Method.GetParameters ()[1]) as ObjectMirror;
853 Assert.AreEqual ("GClass`1", o2.Type.Name);
854 TypeMirror t2 = o2.Type;
855 f = o2.GetValue (t2.GetField ("field"));
858 ObjectMirror o3 = frame.GetValue (frame.Method.GetParameters ()[2]) as ObjectMirror;
859 Assert.AreEqual ("GClass`1", o3.Type.Name);
860 TypeMirror t3 = o3.Type;
861 f = o3.GetValue (t3.GetField ("field"));
862 AssertValue ("FOO", f);
865 AssertThrows<ArgumentNullException> (delegate () {
871 public void Object_GetValues () {
872 var e = run_until ("o1");
873 var frame = e.Thread.GetFrames () [0];
875 object val = frame.GetThis ();
876 Assert.IsTrue (val is ObjectMirror);
877 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
878 ObjectMirror o = (val as ObjectMirror);
880 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
882 TypeMirror t = o.Type;
884 object[] vals = o.GetValues (new FieldInfoMirror [] { t.GetField ("field_i"), t.GetField ("field_s") });
888 AssertValue ("S", f);
891 AssertThrows<ArgumentNullException> (delegate () {
895 AssertThrows<ArgumentNullException> (delegate () {
896 o.GetValues (new FieldInfoMirror [] { null });
899 // field of another class
900 AssertThrows<ArgumentException> (delegate () {
901 o.GetValue (val2.Type.GetField ("field_j"));
905 void TestSetValue (ObjectMirror o, string field_name, object val) {
907 o.SetValue (o.Type.GetField (field_name), vm.RootDomain.CreateString ((string)val));
909 o.SetValue (o.Type.GetField (field_name), vm.CreateValue (val));
910 Value f = o.GetValue (o.Type.GetField (field_name));
911 AssertValue (val, f);
915 public void Object_SetValues () {
916 var e = run_until ("o1");
917 var frame = e.Thread.GetFrames () [0];
919 object val = frame.GetThis ();
920 Assert.IsTrue (val is ObjectMirror);
921 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
922 ObjectMirror o = (val as ObjectMirror);
924 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
926 TestSetValue (o, "field_i", 22);
927 TestSetValue (o, "field_bool1", false);
928 TestSetValue (o, "field_bool2", true);
929 TestSetValue (o, "field_char", 'B');
930 TestSetValue (o, "field_byte", (byte)129);
931 TestSetValue (o, "field_sbyte", (sbyte)-33);
932 TestSetValue (o, "field_short", (short)(Int16.MaxValue - 5));
933 TestSetValue (o, "field_ushort", (ushort)(UInt16.MaxValue - 5));
934 TestSetValue (o, "field_long", Int64.MaxValue - 5);
935 TestSetValue (o, "field_ulong", (ulong)(UInt64.MaxValue - 5));
936 TestSetValue (o, "field_float", 6.28f);
937 TestSetValue (o, "field_double", 6.28);
938 TestSetValue (o, "static_i", 23);
939 TestSetValue (o, "field_s", "CDEF");
944 f = o.GetValue (o.Type.GetField ("field_intptr"));
945 Assert.IsInstanceOfType (typeof (StructMirror), f);
946 AssertValue (Int32.MaxValue - 5, (f as StructMirror).Fields [0]);
949 FieldInfoMirror field = o.Type.GetField ("field_enum");
950 f = o.GetValue (field);
951 (f as EnumMirror).Value = 5;
952 o.SetValue (field, f);
953 f = o.GetValue (field);
954 Assert.AreEqual (5, (f as EnumMirror).Value);
957 o.SetValue (o.Type.GetField ("field_s"), vm.CreateValue (null));
958 f = o.GetValue (o.Type.GetField ("field_s"));
959 AssertValue (null, f);
962 field = o.Type.GetField ("generic_field_struct");
963 f = o.GetValue (field);
964 o.SetValue (field, f);
967 field = o.Type.GetField ("field_nullable");
968 f = o.GetValue (field);
969 AssertValue (0, (f as StructMirror).Fields [0]);
970 AssertValue (false, (f as StructMirror).Fields [1]);
971 o.SetValue (field, vm.CreateValue (6));
972 f = o.GetValue (field);
973 AssertValue (6, (f as StructMirror).Fields [0]);
974 AssertValue (true, (f as StructMirror).Fields [1]);
975 o.SetValue (field, vm.CreateValue (null));
976 f = o.GetValue (field);
977 AssertValue (0, (f as StructMirror).Fields [0]);
978 AssertValue (false, (f as StructMirror).Fields [1]);
981 AssertThrows<ArgumentNullException> (delegate () {
982 o.SetValues (null, new Value [0]);
985 AssertThrows<ArgumentNullException> (delegate () {
986 o.SetValues (new FieldInfoMirror [0], null);
989 AssertThrows<ArgumentNullException> (delegate () {
990 o.SetValues (new FieldInfoMirror [] { null }, new Value [1] { null });
993 // vtype with a wrong type
994 AssertThrows<ArgumentException> (delegate () {
995 o.SetValue (o.Type.GetField ("field_struct"), o.GetValue (o.Type.GetField ("field_enum")));
998 // reference type not assignment compatible
999 AssertThrows<ArgumentException> (delegate () {
1000 o.SetValue (o.Type.GetField ("field_class"), o);
1003 // field of another class
1004 AssertThrows<ArgumentException> (delegate () {
1005 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
1010 public void Type_SetValue () {
1011 var e = run_until ("o1");
1012 var frame = e.Thread.GetFrames () [0];
1015 object val = frame.GetThis ();
1016 Assert.IsTrue (val is ObjectMirror);
1017 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
1018 ObjectMirror o = (val as ObjectMirror);
1020 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1022 o.Type.SetValue (o.Type.GetField ("static_i"), vm.CreateValue (55));
1023 f = o.Type.GetValue (o.Type.GetField ("static_i"));
1024 AssertValue (55, f);
1026 o.Type.SetValue (o.Type.GetField ("static_s"), vm.RootDomain.CreateString ("B"));
1027 f = o.Type.GetValue (o.Type.GetField ("static_s"));
1028 AssertValue ("B", f);
1030 // Argument checking
1031 AssertThrows<ArgumentNullException> (delegate () {
1032 o.Type.SetValue (null, vm.CreateValue (0));
1035 AssertThrows<ArgumentNullException> (delegate () {
1036 o.Type.SetValue (o.Type.GetField ("static_i"), null);
1039 // field of another class
1040 AssertThrows<ArgumentException> (delegate () {
1041 o.SetValue (val2.Type.GetField ("field_j"), vm.CreateValue (1));
1046 public void TypeInfo () {
1047 Event e = run_until ("ti2");
1048 StackFrame frame = e.Thread.GetFrames () [0];
1053 t = frame.Method.GetParameters ()[0].ParameterType;
1055 Assert.AreEqual ("String[]", t.Name);
1056 Assert.AreEqual ("string[]", t.CSharpName);
1057 Assert.AreEqual ("Array", t.BaseType.Name);
1058 Assert.AreEqual (true, t.HasElementType);
1059 Assert.AreEqual (true, t.IsArray);
1060 Assert.AreEqual (1, t.GetArrayRank ());
1061 Assert.AreEqual ("String", t.GetElementType ().Name);
1063 t = frame.Method.GetParameters ()[2].ParameterType;
1065 Assert.AreEqual ("Int32[,]", t.Name);
1067 //Assert.AreEqual ("int[,]", t.CSharpName);
1068 Assert.AreEqual ("Array", t.BaseType.Name);
1069 Assert.AreEqual (true, t.HasElementType);
1070 Assert.AreEqual (true, t.IsArray);
1071 Assert.AreEqual (2, t.GetArrayRank ());
1072 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1075 t = frame.Method.GetParameters ()[3].ParameterType;
1077 //Assert.AreEqual ("Int32&", t.Name);
1078 //Assert.AreEqual (true, t.IsByRef);
1079 //Assert.AreEqual (true, t.HasElementType);
1082 t = frame.Method.GetParameters ()[4].ParameterType;
1084 //Assert.AreEqual ("Int32*", t.Name);
1085 Assert.AreEqual (true, t.IsPointer);
1086 Assert.AreEqual (true, t.HasElementType);
1087 Assert.AreEqual ("Int32", t.GetElementType ().Name);
1088 Assert.AreEqual (false, t.IsPrimitive);
1091 t = frame.Method.GetParameters ()[5].ParameterType;
1092 Assert.AreEqual (true, t.IsPrimitive);
1095 t = frame.Method.GetParameters ()[6].ParameterType;
1096 Assert.AreEqual ("AStruct", t.Name);
1097 Assert.AreEqual (false, t.IsPrimitive);
1098 Assert.AreEqual (true, t.IsValueType);
1099 Assert.AreEqual (false, t.IsClass);
1102 t = frame.Method.GetParameters ()[7].ParameterType;
1103 Assert.AreEqual ("Tests", t.Name);
1104 var nested = (from nt in t.GetNestedTypes () where nt.IsNestedPublic select nt).ToArray ();
1105 Assert.AreEqual (1, nested.Length);
1106 Assert.AreEqual ("NestedClass", nested [0].Name);
1107 Assert.IsTrue (t.BaseType.IsAssignableFrom (t));
1108 Assert.IsTrue (!t.IsAssignableFrom (t.BaseType));
1110 // generic instances
1111 t = frame.Method.GetParameters ()[9].ParameterType;
1112 Assert.AreEqual ("GClass`1", t.Name);
1113 Assert.IsTrue (t.IsGenericType);
1114 Assert.IsFalse (t.IsGenericTypeDefinition);
1116 var args = t.GetGenericArguments ();
1117 Assert.AreEqual (1, args.Length);
1118 Assert.AreEqual ("Int32", args [0].Name);
1120 // generic type definitions
1121 var gtd = t.GetGenericTypeDefinition ();
1122 Assert.AreEqual ("GClass`1", gtd.Name);
1123 Assert.IsTrue (gtd.IsGenericType);
1124 Assert.IsTrue (gtd.IsGenericTypeDefinition);
1125 Assert.AreEqual (gtd, gtd.GetGenericTypeDefinition ());
1127 args = gtd.GetGenericArguments ();
1128 Assert.AreEqual (1, args.Length);
1129 Assert.AreEqual ("T", args [0].Name);
1132 t = frame.Method.GetParameters ()[10].ParameterType;
1133 Assert.AreEqual ("AnEnum", t.Name);
1134 Assert.IsTrue (t.IsEnum);
1135 Assert.AreEqual ("Int32", t.EnumUnderlyingType.Name);
1138 t = frame.Method.GetParameters ()[7].ParameterType;
1140 var props = t.GetProperties ();
1141 Assert.AreEqual (3, props.Length);
1142 foreach (PropertyInfoMirror prop in props) {
1143 ParameterInfoMirror[] indexes = prop.GetIndexParameters ();
1145 if (prop.Name == "IntProperty") {
1146 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1147 Assert.AreEqual ("get_IntProperty", prop.GetGetMethod ().Name);
1148 Assert.AreEqual ("set_IntProperty", prop.GetSetMethod ().Name);
1149 Assert.AreEqual (0, indexes.Length);
1150 } else if (prop.Name == "ReadOnlyProperty") {
1151 Assert.AreEqual ("Int32", prop.PropertyType.Name);
1152 Assert.AreEqual ("get_ReadOnlyProperty", prop.GetGetMethod ().Name);
1153 Assert.AreEqual (null, prop.GetSetMethod ());
1154 Assert.AreEqual (0, indexes.Length);
1155 } else if (prop.Name == "IndexedProperty") {
1156 Assert.AreEqual (1, indexes.Length);
1157 Assert.AreEqual ("Int32", indexes [0].ParameterType.Name);
1161 // custom attributes
1162 t = frame.Method.GetParameters ()[8].ParameterType;
1163 Assert.AreEqual ("Tests2", t.Name);
1164 var attrs = t.GetCustomAttributes (true);
1165 Assert.AreEqual (5, attrs.Length);
1166 foreach (var attr in attrs) {
1167 if (attr.Constructor.DeclaringType.Name == "DebuggerDisplayAttribute") {
1168 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1169 Assert.AreEqual ("Tests", attr.ConstructorArguments [0].Value);
1170 Assert.AreEqual (2, attr.NamedArguments.Count);
1171 Assert.AreEqual ("Name", attr.NamedArguments [0].Property.Name);
1172 Assert.AreEqual ("FOO", attr.NamedArguments [0].TypedValue.Value);
1173 Assert.AreEqual ("Target", attr.NamedArguments [1].Property.Name);
1174 Assert.IsInstanceOfType (typeof (TypeMirror), attr.NamedArguments [1].TypedValue.Value);
1175 Assert.AreEqual ("Int32", (attr.NamedArguments [1].TypedValue.Value as TypeMirror).Name);
1176 } else if (attr.Constructor.DeclaringType.Name == "DebuggerTypeProxyAttribute") {
1177 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1178 Assert.IsInstanceOfType (typeof (TypeMirror), attr.ConstructorArguments [0].Value);
1179 Assert.AreEqual ("Tests", (attr.ConstructorArguments [0].Value as TypeMirror).Name);
1180 } else if (attr.Constructor.DeclaringType.Name == "BAttribute") {
1181 Assert.AreEqual (2, attr.NamedArguments.Count);
1182 Assert.AreEqual ("afield", attr.NamedArguments [0].Field.Name);
1183 Assert.AreEqual ("bfield", attr.NamedArguments [1].Field.Name);
1184 } else if (attr.Constructor.DeclaringType.Name == "ClassInterfaceAttribute") {
1185 // inherited from System.Object
1186 //} else if (attr.Constructor.DeclaringType.Name == "Serializable") {
1187 // inherited from System.Object
1188 } else if (attr.Constructor.DeclaringType.Name == "ComVisibleAttribute") {
1189 // inherited from System.Object
1191 Assert.Fail (attr.Constructor.DeclaringType.Name);
1195 var assembly = entry_point.DeclaringType.Assembly;
1196 var type = assembly.GetType ("Tests4");
1197 Assert.IsFalse (type.IsInitialized);
1201 public void FieldInfo () {
1202 Event e = run_until ("ti2");
1203 StackFrame frame = e.Thread.GetFrames () [0];
1207 t = frame.Method.GetParameters ()[8].ParameterType;
1208 Assert.AreEqual ("Tests2", t.Name);
1210 var fi = t.GetField ("field_j");
1211 var attrs = fi.GetCustomAttributes (true);
1212 Assert.AreEqual (1, attrs.Length);
1213 var attr = attrs [0];
1214 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1215 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1216 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1217 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1221 public void PropertyInfo () {
1222 Event e = run_until ("ti2");
1223 StackFrame frame = e.Thread.GetFrames () [0];
1227 t = frame.Method.GetParameters ()[8].ParameterType;
1228 Assert.AreEqual ("Tests2", t.Name);
1230 var pi = t.GetProperty ("AProperty");
1231 var attrs = pi.GetCustomAttributes (true);
1232 Assert.AreEqual (1, attrs.Length);
1233 var attr = attrs [0];
1234 Assert.AreEqual ("DebuggerBrowsableAttribute", attr.Constructor.DeclaringType.Name);
1235 Assert.AreEqual (1, attr.ConstructorArguments.Count);
1236 Assert.IsInstanceOfType (typeof (EnumMirror), attr.ConstructorArguments [0].Value);
1237 Assert.AreEqual ((int)System.Diagnostics.DebuggerBrowsableState.Collapsed, (attr.ConstructorArguments [0].Value as EnumMirror).Value);
1241 [Category ("only5")]
1242 public void Type_GetValue () {
1243 Event e = run_until ("o1");
1244 StackFrame frame = e.Thread.GetFrames () [0];
1246 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1248 TypeMirror t = o.Type;
1250 ObjectMirror val2 = frame.GetValue (frame.Method.GetParameters ()[0]) as ObjectMirror;
1253 object f = t.GetValue (o.Type.GetField ("static_i"));
1254 AssertValue (55, f);
1256 f = t.GetValue (o.Type.GetField ("static_s"));
1257 AssertValue ("A", f);
1259 // literal static fields
1260 f = t.GetValue (o.Type.GetField ("literal_i"));
1261 AssertValue (56, f);
1263 f = t.GetValue (o.Type.GetField ("literal_s"));
1264 AssertValue ("B", f);
1266 // Inherited static fields
1267 TypeMirror parent = t.BaseType;
1268 f = t.GetValue (parent.GetField ("base_static_i"));
1269 AssertValue (57, f);
1271 f = t.GetValue (parent.GetField ("base_static_s"));
1272 AssertValue ("C", f);
1274 // thread static field
1275 f = t.GetValue (t.GetField ("tls_i"), e.Thread);
1276 AssertValue (42, f);
1278 // Argument checking
1279 AssertThrows<ArgumentNullException> (delegate () {
1284 AssertThrows<ArgumentException> (delegate () {
1285 t.GetValue (o.Type.GetField ("field_i"));
1288 // field on another type
1289 AssertThrows<ArgumentException> (delegate () {
1290 t.GetValue (val2.Type.GetField ("static_field_j"));
1293 // special static field
1294 AssertThrows<ArgumentException> (delegate () {
1295 t.GetValue (t.GetField ("tls_i"));
1300 public void Type_GetValues () {
1301 Event e = run_until ("o1");
1302 StackFrame frame = e.Thread.GetFrames () [0];
1304 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1306 TypeMirror t = o.Type;
1309 object[] vals = t.GetValues (new FieldInfoMirror [] { t.GetField ("static_i"), t.GetField ("static_s") });
1310 object f = vals [0];
1311 AssertValue (55, f);
1314 AssertValue ("A", f);
1316 // Argument checking
1317 AssertThrows<ArgumentNullException> (delegate () {
1321 AssertThrows<ArgumentNullException> (delegate () {
1322 t.GetValues (new FieldInfoMirror [] { null });
1327 public void ObjRefs () {
1328 Event e = run_until ("objrefs1");
1329 StackFrame frame = e.Thread.GetFrames () [0];
1331 ObjectMirror o = frame.GetThis () as ObjectMirror;
1332 ObjectMirror child = o.GetValue (o.Type.GetField ("child")) as ObjectMirror;
1334 Assert.IsTrue (child.Address != 0);
1336 // Check that object references are internalized correctly
1337 Assert.AreEqual (o, frame.GetThis ());
1339 run_until ("objrefs2");
1341 // child should be gc'd now
1342 // This is not deterministic
1343 //Assert.IsTrue (child.IsCollected);
1346 * No longer works since Type is read eagerly
1349 AssertThrows<ObjectCollectedException> (delegate () {
1350 TypeMirror t = child.Type;
1354 AssertThrows<ObjectCollectedException> (delegate () {
1355 long addr = child.Address;
1361 public void Type_GetObject () {
1362 Event e = run_until ("o1");
1363 StackFrame frame = e.Thread.GetFrames () [0];
1365 ObjectMirror o = (frame.GetThis () as ObjectMirror);
1367 TypeMirror t = o.Type;
1369 Assert.AreEqual ("MonoType", t.GetTypeObject ().Type.Name);
1373 public void VTypes () {
1374 Event e = run_until ("vtypes1");
1375 StackFrame frame = e.Thread.GetFrames () [0];
1378 ObjectMirror o = frame.GetThis () as ObjectMirror;
1379 var obj = o.GetValue (o.Type.GetField ("field_struct"));
1380 Assert.IsTrue (obj is StructMirror);
1381 var s = obj as StructMirror;
1382 Assert.AreEqual ("AStruct", s.Type.Name);
1383 AssertValue (42, s ["i"]);
1385 AssertValue ("S", obj);
1386 AssertValue (43, s ["k"]);
1387 obj = o.GetValue (o.Type.GetField ("field_boxed_struct"));
1388 Assert.IsTrue (obj is StructMirror);
1389 s = obj as StructMirror;
1390 Assert.AreEqual ("AStruct", s.Type.Name);
1391 AssertValue (42, s ["i"]);
1393 // Check decoding of nested structs (#14942)
1394 obj = o.GetValue (o.Type.GetField ("nested_struct"));
1395 o.SetValue (o.Type.GetField ("nested_struct"), obj);
1397 // Check round tripping of boxed struct fields (#12354)
1398 obj = o.GetValue (o.Type.GetField ("boxed_struct_field"));
1399 o.SetValue (o.Type.GetField ("boxed_struct_field"), obj);
1400 obj = o.GetValue (o.Type.GetField ("boxed_struct_field"));
1401 s = obj as StructMirror;
1402 AssertValue (1, s ["key"]);
1404 Assert.IsTrue (obj is StructMirror);
1405 s = obj as StructMirror;
1406 AssertValue (42, s ["m_value"]);
1408 // vtypes as arguments
1409 s = frame.GetArgument (0) as StructMirror;
1410 AssertValue (44, s ["i"]);
1412 AssertValue ("T", obj);
1413 AssertValue (45, s ["k"]);
1415 // vtypes as array entries
1416 var arr = frame.GetArgument (1) as ArrayMirror;
1418 Assert.IsTrue (obj is StructMirror);
1419 s = obj as StructMirror;
1420 AssertValue (1, s ["i"]);
1421 AssertValue ("S1", s ["s"]);
1423 Assert.IsTrue (obj is StructMirror);
1424 s = obj as StructMirror;
1425 AssertValue (2, s ["i"]);
1426 AssertValue ("S2", s ["s"]);
1428 // Argument checking
1429 s = frame.GetArgument (0) as StructMirror;
1430 AssertThrows<ArgumentException> (delegate () {
1434 // generic vtype instances
1435 o = frame.GetThis () as ObjectMirror;
1436 obj = o.GetValue (o.Type.GetField ("generic_field_struct"));
1437 Assert.IsTrue (obj is StructMirror);
1438 s = obj as StructMirror;
1439 Assert.AreEqual ("GStruct`1", s.Type.Name);
1440 AssertValue (42, s ["i"]);
1442 // this on vtype methods
1443 e = run_until ("vtypes2");
1444 e = step_until (e.Thread, "foo");
1446 frame = e.Thread.GetFrames () [0];
1448 Assert.AreEqual ("foo", (e as StepEvent).Method.Name);
1449 obj = frame.GetThis ();
1451 Assert.IsTrue (obj is StructMirror);
1452 s = obj as StructMirror;
1453 AssertValue (44, s ["i"]);
1454 AssertValue ("T", s ["s"]);
1455 AssertValue (45, s ["k"]);
1457 // this on static vtype methods
1458 e = run_until ("vtypes3");
1459 e = step_until (e.Thread, "static_foo");
1461 frame = e.Thread.GetFrames () [0];
1463 Assert.AreEqual ("static_foo", (e as StepEvent).Method.Name);
1464 obj = frame.GetThis ();
1465 AssertValue (null, obj);
1467 // vtypes which reference themselves recursively
1468 e = run_until ("vtypes4_2");
1469 frame = e.Thread.GetFrames () [0];
1471 Assert.IsTrue (frame.GetArgument (0) is StructMirror);
1475 public void AssemblyInfo () {
1476 Event e = run_until ("single_stepping");
1478 StackFrame frame = e.Thread.GetFrames () [0];
1480 var aname = frame.Method.DeclaringType.Assembly.GetName ();
1481 Assert.AreEqual ("dtest-app, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", aname.ToString ());
1483 ModuleMirror m = frame.Method.DeclaringType.Module;
1485 Assert.AreEqual ("dtest-app.exe", m.Name);
1486 Assert.AreEqual ("dtest-app.exe", m.ScopeName);
1487 Assert.IsTrue (m.FullyQualifiedName.IndexOf ("dtest-app.exe") != -1);
1488 Guid guid = m.ModuleVersionId;
1489 Assert.AreEqual (frame.Method.DeclaringType.Assembly, m.Assembly);
1490 Assert.AreEqual (frame.Method.DeclaringType.Assembly.ManifestModule, m);
1492 // This is no longer true on 4.0
1493 //Assert.AreEqual ("Assembly", frame.Method.DeclaringType.Assembly.GetAssemblyObject ().Type.Name);
1495 TypeMirror t = vm.RootDomain.Corlib.GetType ("System.Diagnostics.DebuggerDisplayAttribute");
1496 Assert.AreEqual ("DebuggerDisplayAttribute", t.Name);
1500 public void LocalsInfo () {
1501 Event e = run_until ("locals2");
1503 StackFrame frame = e.Thread.GetFrames () [0];
1505 var locals = frame.Method.GetLocals ();
1506 Assert.AreEqual (8, locals.Length);
1507 for (int i = 0; i < 8; ++i) {
1508 if (locals [i].Name == "args") {
1509 Assert.IsTrue (locals [i].IsArg);
1510 Assert.AreEqual ("String[]", locals [i].Type.Name);
1511 } else if (locals [i].Name == "arg") {
1512 Assert.IsTrue (locals [i].IsArg);
1513 Assert.AreEqual ("Int32", locals [i].Type.Name);
1514 } else if (locals [i].Name == "i") {
1515 Assert.IsFalse (locals [i].IsArg);
1516 Assert.AreEqual ("Int64", locals [i].Type.Name);
1517 } else if (locals [i].Name == "j") {
1518 Assert.IsFalse (locals [i].IsArg);
1519 Assert.AreEqual ("Int32", locals [i].Type.Name);
1520 } else if (locals [i].Name == "s") {
1521 Assert.IsFalse (locals [i].IsArg);
1522 Assert.AreEqual ("String", locals [i].Type.Name);
1523 } else if (locals [i].Name == "t") {
1525 Assert.IsTrue (locals [i].IsArg);
1526 Assert.AreEqual ("String", locals [i].Type.Name);
1527 } else if (locals [i].Name == "rs") {
1528 Assert.IsTrue (locals [i].IsArg);
1529 Assert.AreEqual ("String", locals [i].Type.Name);
1530 } else if (locals [i].Name == "astruct") {
1537 Event step_once () {
1539 var e = GetNextEvent ();
1540 Assert.IsTrue (e is StepEvent);
1544 Event step_into () {
1545 step_req.Disable ();
1546 step_req.Depth = StepDepth.Into;
1548 return step_once ();
1551 Event step_over () {
1552 step_req.Disable ();
1553 step_req.Depth = StepDepth.Over;
1555 return step_once ();
1559 step_req.Disable ();
1560 step_req.Depth = StepDepth.Out;
1562 return step_once ();
1566 public void Locals () {
1567 var be = run_until ("locals1");
1569 StackFrame frame = be.Thread.GetFrames () [0];
1570 MethodMirror m1 = frame.Method;
1572 // Compiler generated byref local
1573 foreach (var l in m1.GetLocals ()) {
1574 // The byval flag is hidden from the type
1575 if (l.Name != "ri" && l.Type.Name == "Double")
1576 AssertValue (null, frame.GetValue (l));
1579 be = run_until ("locals2");
1581 frame = be.Thread.GetFrames () [0];
1583 object val = frame.GetValue (frame.Method.GetLocal ("i"));
1584 AssertValue (0, val);
1586 var req = create_step (be);
1593 var e = step_once ();
1594 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1596 // Execute s = "AB";
1598 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1600 frame = e.Thread.GetFrames () [0];
1602 val = frame.GetValue (frame.Method.GetLocal ("i"));
1603 AssertValue (42, val);
1605 LocalVariable[] locals = frame.Method.GetLocals ();
1606 var vals = frame.GetValues (locals);
1607 Assert.AreEqual (locals.Length, vals.Length);
1608 for (int i = 0; i < locals.Length; ++i) {
1609 if (locals [i].Name == "i")
1610 AssertValue (42, vals [i]);
1611 if (locals [i].Name == "s")
1612 AssertValue ("AB", vals [i]);
1613 if (locals [i].Name == "t")
1614 AssertValue ("ABC", vals [i]);
1617 // Argument checking
1620 AssertThrows<ArgumentNullException> (delegate () {
1621 frame.GetValue ((LocalVariable)null);
1623 // GetValue () local from another method
1624 AssertThrows<ArgumentException> (delegate () {
1625 frame.GetValue (m1.GetLocal ("foo"));
1629 AssertThrows<ArgumentNullException> (delegate () {
1630 frame.GetValue ((ParameterInfoMirror)null);
1632 // GetValue () local from another method
1633 AssertThrows<ArgumentException> (delegate () {
1634 frame.GetValue (m1.GetParameters ()[0]);
1637 // GetValues () null
1638 AssertThrows<ArgumentNullException> (delegate () {
1639 frame.GetValues (null);
1641 // GetValues () embedded null
1642 AssertThrows<ArgumentNullException> (delegate () {
1643 frame.GetValues (new LocalVariable [] { null });
1645 // GetValues () local from another method
1646 AssertThrows<ArgumentException> (delegate () {
1647 frame.GetValues (new LocalVariable [] { m1.GetLocal ("foo") });
1650 AssertThrows<ArgumentException> (delegate () {
1651 val = frame.GetValue (frame.Method.ReturnParameter);
1654 // invalid stack frames
1656 e = GetNextEvent ();
1657 Assert.IsTrue (e is StepEvent);
1658 Assert.AreEqual ("locals2", (e as StepEvent).Method.Name);
1660 AssertThrows<InvalidStackFrameException> (delegate () {
1661 frame.GetValue (frame.Method.GetLocal ("i"));
1667 be = run_until ("locals7");
1669 req = create_step (be);
1675 // Test that locals are initialized
1676 frame = e.Thread.GetFrames () [0];
1677 val = frame.GetValue (frame.Method.GetLocal ("t"));
1678 AssertValue (0, val);
1682 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
1686 Assert.AreEqual ("locals7", (e as StepEvent).Method.Name);
1688 frame = e.Thread.GetFrames () [0];
1689 val = frame.GetValue (frame.Method.GetParameters ()[0]);
1690 AssertValue (22, val);
1691 val = frame.GetValue (frame.Method.GetLocal ("t"));
1692 AssertValue (22, val);
1693 val = frame.GetValue (frame.Method.GetLocal ("t2"));
1694 AssertValue (22, val);
1698 public void GetVisibleVariables () {
1699 Event e = run_until ("locals4");
1702 var locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1703 Assert.AreEqual (2, locals.Count);
1704 var loc = locals.First (l => l.Name == "i");
1705 Assert.AreEqual ("Int64", loc.Type.Name);
1706 loc = locals.First (l => l.Name == "s");
1707 Assert.AreEqual ("String", loc.Type.Name);
1709 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1710 Assert.AreEqual ("i", loc.Name);
1711 Assert.AreEqual ("Int64", loc.Type.Name);
1713 e = run_until ("locals5");
1716 locals = e.Thread.GetFrames ()[1].GetVisibleVariables ();
1717 Assert.AreEqual (2, locals.Count);
1718 loc = locals.First (l => l.Name == "i");
1719 Assert.AreEqual ("String", loc.Type.Name);
1720 loc = locals.First (l => l.Name == "s");
1721 Assert.AreEqual ("String", loc.Type.Name);
1723 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("i");
1724 Assert.AreEqual ("i", loc.Name);
1725 Assert.AreEqual ("String", loc.Type.Name);
1727 // Variable in another scope
1728 loc = e.Thread.GetFrames ()[1].GetVisibleVariableByName ("j");
1729 Assert.IsNull (loc);
1733 public void Exit () {
1738 var e = GetNextEvent ();
1739 Assert.IsInstanceOfType (typeof (VMDeathEvent), e);
1741 Assert.AreEqual (5, (e as VMDeathEvent).ExitCode);
1744 /* Could be a remote vm with no process */
1747 Assert.AreEqual (5, p.ExitCode);
1750 AssertThrows<VMDisconnectedException> (delegate () {
1759 public void Dispose () {
1764 var e = GetNextEvent ();
1765 Assert.IsInstanceOfType (typeof (VMDisconnectEvent), e);
1768 /* Could be a remote vm with no process */
1771 Assert.AreEqual (3, p.ExitCode);
1774 AssertThrows<VMDisconnectedException> (delegate () {
1783 public void ColumnNumbers () {
1784 Event e = run_until ("line_numbers");
1786 // FIXME: Merge this with LineNumbers () when its fixed
1788 step_req = create_step (e);
1789 step_req.Depth = StepDepth.Into;
1797 e = GetNextEvent ();
1798 Assert.IsTrue (e is StepEvent);
1799 if (e.Thread.GetFrames ()[0].Method.Name == "ln1")
1803 // Do an additional step over so we are not on the beginning line of the method
1804 step_req.Disable ();
1805 step_req.Depth = StepDepth.Over;
1808 e = GetNextEvent ();
1809 Assert.IsTrue (e is StepEvent);
1811 l = e.Thread.GetFrames ()[0].Location;
1813 Assert.AreEqual (3, l.ColumnNumber);
1815 step_req.Disable ();
1819 // Broken by mcs+runtime changes (#5438)
1820 [Category("NotWorking")]
1821 public void LineNumbers () {
1822 Event e = run_until ("line_numbers");
1824 step_req = create_step (e);
1825 step_req.Depth = StepDepth.Into;
1832 e = GetNextEvent ();
1833 Assert.IsTrue (e is StepEvent);
1835 l = e.Thread.GetFrames ()[0].Location;
1837 Assert.IsTrue (l.SourceFile.IndexOf ("dtest-app.cs") != -1);
1838 Assert.AreEqual ("ln1", l.Method.Name);
1841 using (FileStream fs = new FileStream (l.SourceFile, FileMode.Open, FileAccess.Read)) {
1842 MD5 md5 = MD5.Create ();
1843 var hash = md5.ComputeHash (fs);
1845 for (int i = 0; i < 16; ++i)
1846 Assert.AreEqual (hash [i], l.SourceFileHash [i]);
1849 int line_base = l.LineNumber;
1852 e = GetNextEvent ();
1853 Assert.IsTrue (e is StepEvent);
1854 l = e.Thread.GetFrames ()[0].Location;
1855 Assert.AreEqual ("ln2", l.Method.Name);
1856 Assert.AreEqual (line_base + 6, l.LineNumber);
1859 e = GetNextEvent ();
1860 Assert.IsTrue (e is StepEvent);
1861 l = e.Thread.GetFrames ()[0].Location;
1862 Assert.AreEqual ("ln1", l.Method.Name);
1863 Assert.AreEqual (line_base + 1, l.LineNumber);
1866 e = GetNextEvent ();
1867 Assert.IsTrue (e is StepEvent);
1868 l = e.Thread.GetFrames ()[0].Location;
1869 Assert.AreEqual ("ln3", l.Method.Name);
1870 Assert.AreEqual (line_base + 11, l.LineNumber);
1873 e = GetNextEvent ();
1874 Assert.IsTrue (e is StepEvent);
1875 l = e.Thread.GetFrames ()[0].Location;
1876 Assert.AreEqual ("ln3", l.Method.Name);
1877 Assert.IsTrue (l.SourceFile.EndsWith ("FOO"));
1878 Assert.AreEqual (55, l.LineNumber);
1881 e = GetNextEvent ();
1882 Assert.IsTrue (e is StepEvent);
1883 l = e.Thread.GetFrames ()[0].Location;
1884 Assert.AreEqual ("ln1", l.Method.Name);
1885 Assert.AreEqual (line_base + 2, l.LineNumber);
1887 // GetSourceFiles ()
1888 string[] sources = l.Method.DeclaringType.GetSourceFiles ();
1889 Assert.AreEqual (2, sources.Length);
1890 Assert.AreEqual ("dtest-app.cs", sources [0]);
1891 Assert.AreEqual ("FOO", sources [1]);
1893 sources = l.Method.DeclaringType.GetSourceFiles (true);
1894 Assert.AreEqual (2, sources.Length);
1895 Assert.IsTrue (sources [0].EndsWith ("dtest-app.cs"));
1896 Assert.IsTrue (sources [1].EndsWith ("FOO"));
1900 public void Suspend () {
1903 Start (new string [] { "dtest-app.exe", "suspend-test" });
1905 Event e = run_until ("suspend");
1907 ThreadMirror main = e.Thread;
1915 // The debuggee should be suspended while it is running the infinite loop
1917 StackFrame frame = main.GetFrames ()[0];
1918 Assert.AreEqual ("suspend", frame.Method.Name);
1922 // resuming when not suspended
1923 AssertThrows<InvalidOperationException> (delegate () {
1933 public void AssemblyLoad () {
1934 Event e = run_until ("assembly_load");
1936 var load_req = vm.CreateAssemblyLoadRequest ();
1941 e = GetNextEvent ();
1942 Assert.IsInstanceOfType (typeof (AssemblyLoadEvent), e);
1943 Assert.IsTrue ((e as AssemblyLoadEvent).Assembly.Location.EndsWith ("System.dll"));
1945 var frames = e.Thread.GetFrames ();
1946 Assert.IsTrue (frames.Length > 0);
1947 Assert.AreEqual ("assembly_load", frames [0].Method.Name);
1951 public void CreateValue () {
1954 v = vm.CreateValue (1);
1955 Assert.AreEqual (vm, v.VirtualMachine);
1956 Assert.AreEqual (1, v.Value);
1958 v = vm.CreateValue (null);
1959 Assert.AreEqual (vm, v.VirtualMachine);
1960 Assert.AreEqual (null, v.Value);
1962 // Argument checking
1963 AssertThrows <ArgumentException> (delegate () {
1964 v = vm.CreateValue ("FOO");
1969 public void CreateString () {
1970 StringMirror s = vm.RootDomain.CreateString ("ABC");
1972 Assert.AreEqual (vm, s.VirtualMachine);
1973 Assert.AreEqual ("ABC", s.Value);
1974 Assert.AreEqual (vm.RootDomain, s.Domain);
1977 StringBuilder sb = new StringBuilder ();
1978 for (int i = 0; i < 1024; ++i)
1980 s = vm.RootDomain.CreateString (sb.ToString ());
1982 // Argument checking
1983 AssertThrows <ArgumentNullException> (delegate () {
1984 s = vm.RootDomain.CreateString (null);
1989 public void CreateBoxedValue () {
1990 ObjectMirror o = vm.RootDomain.CreateBoxedValue (new PrimitiveValue (vm, 42));
1992 Assert.AreEqual ("Int32", o.Type.Name);
1993 //AssertValue (42, m.GetValue (o.Type.GetField ("m_value")));
1995 // Argument checking
1996 AssertThrows <ArgumentNullException> (delegate () {
1997 vm.RootDomain.CreateBoxedValue (null);
2000 AssertThrows <ArgumentException> (delegate () {
2001 vm.RootDomain.CreateBoxedValue (o);
2006 public void Invoke () {
2007 Event e = run_until ("invoke1");
2009 StackFrame frame = e.Thread.GetFrames () [0];
2011 TypeMirror t = frame.Method.DeclaringType;
2012 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2014 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2020 m = t.GetMethod ("invoke_return_void");
2021 v = this_obj.InvokeMethod (e.Thread, m, null);
2025 m = t.GetMethod ("invoke_return_ref");
2026 v = this_obj.InvokeMethod (e.Thread, m, null);
2027 AssertValue ("ABC", v);
2030 m = t.GetMethod ("invoke_return_null");
2031 v = this_obj.InvokeMethod (e.Thread, m, null);
2032 AssertValue (null, v);
2035 m = t.GetMethod ("invoke_return_primitive");
2036 v = this_obj.InvokeMethod (e.Thread, m, null);
2037 AssertValue (42, v);
2040 m = t.GetMethod ("invoke_return_nullable");
2041 v = this_obj.InvokeMethod (e.Thread, m, null);
2042 Assert.IsInstanceOfType (typeof (StructMirror), v);
2043 var s = v as StructMirror;
2044 AssertValue (42, s.Fields [0]);
2045 AssertValue (true, s.Fields [1]);
2047 // pass nullable as this
2048 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2049 m = s.Type.GetMethod ("ToString");
2050 v = s.InvokeMethod (e.Thread, m, null);
2052 // return nullable null
2053 m = t.GetMethod ("invoke_return_nullable_null");
2054 v = this_obj.InvokeMethod (e.Thread, m, null);
2055 Assert.IsInstanceOfType (typeof (StructMirror), v);
2056 s = v as StructMirror;
2057 AssertValue (0, s.Fields [0]);
2058 AssertValue (false, s.Fields [1]);
2060 // pass nullable as this
2061 //m = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2062 m = s.Type.GetMethod ("ToString");
2063 v = s.InvokeMethod (e.Thread, m, null);
2066 m = t.GetMethod ("invoke_pass_primitive");
2067 Value[] args = new Value [] {
2068 vm.CreateValue ((byte)Byte.MaxValue),
2069 vm.CreateValue ((sbyte)SByte.MaxValue),
2070 vm.CreateValue ((short)1),
2071 vm.CreateValue ((ushort)1),
2072 vm.CreateValue ((int)1),
2073 vm.CreateValue ((uint)1),
2074 vm.CreateValue ((long)1),
2075 vm.CreateValue ((ulong)1),
2076 vm.CreateValue ('A'),
2077 vm.CreateValue (true),
2078 vm.CreateValue (3.14f),
2079 vm.CreateValue (3.14) };
2081 v = this_obj.InvokeMethod (e.Thread, m, args);
2082 AssertValue ((int)Byte.MaxValue + (int)SByte.MaxValue + 1 + 1 + 1 + 1 + 1 + 1 + 'A' + 1 + 3 + 3, v);
2085 m = t.GetMethod ("invoke_pass_ref");
2086 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2087 AssertValue ("ABC", v);
2090 m = t.GetMethod ("invoke_pass_ref");
2091 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (null) });
2092 AssertValue (null, v);
2095 m = t.GetMethod ("invoke_static_pass_ref");
2096 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2097 AssertValue ("ABC", v);
2099 // static invoked using ObjectMirror.InvokeMethod
2100 m = t.GetMethod ("invoke_static_pass_ref");
2101 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2102 AssertValue ("ABC", v);
2104 // method which throws an exception
2106 m = t.GetMethod ("invoke_throws");
2107 v = this_obj.InvokeMethod (e.Thread, m, null);
2109 } catch (InvocationException ex) {
2110 Assert.AreEqual ("Exception", ex.Exception.Type.Name);
2115 m = t.GetMethod ("invoke_out");
2116 var out_task = this_obj.InvokeMethodAsyncWithResult (e.Thread, m, new Value [] { vm.CreateValue (1), vm.CreateValue (null) }, InvokeOptions.ReturnOutArgs);
2117 var out_args = out_task.Result.OutArgs;
2118 AssertValue (5, out_args [0]);
2119 Assert.IsTrue (out_args [1] is ArrayMirror);
2120 Assert.AreEqual (10, (out_args [1] as ArrayMirror).Length);
2122 // without ReturnOutArgs flag
2123 out_task = this_obj.InvokeMethodAsyncWithResult (e.Thread, m, new Value [] { vm.CreateValue (1), vm.CreateValue (null) });
2124 out_args = out_task.Result.OutArgs;
2125 Assert.IsNull (out_args);
2129 m = t.GetMethod (".ctor");
2130 v = t.InvokeMethod (e.Thread, m, null);
2131 Assert.IsInstanceOfType (typeof (ObjectMirror), v);
2132 Assert.AreEqual ("Tests", (v as ObjectMirror).Type.Name);
2135 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("ITest2");
2136 m = cl1.GetMethod ("invoke_iface");
2137 v = this_obj.InvokeMethod (e.Thread, m, null);
2138 AssertValue (42, v);
2141 m = t.BaseType.GetMethod ("virtual_method");
2142 v = this_obj.InvokeMethod (e.Thread, m, null, InvokeOptions.Virtual);
2143 AssertValue ("V2", v);
2147 m = t.GetMethod ("invoke_pass_ref");
2148 var task = this_obj.InvokeMethodAsync (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2149 AssertValue ("ABC", task.Result);
2152 m = t.GetMethod ("invoke_static_pass_ref");
2153 task = t.InvokeMethodAsync (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2154 AssertValue ("ABC", task.Result);
2157 // Argument checking
2160 AssertThrows<ArgumentNullException> (delegate {
2161 m = t.GetMethod ("invoke_pass_ref");
2162 v = this_obj.InvokeMethod (null, m, new Value [] { vm.CreateValue (null) });
2166 AssertThrows<ArgumentNullException> (delegate {
2167 v = this_obj.InvokeMethod (e.Thread, null, new Value [] { vm.CreateValue (null) });
2170 // invalid number of arguments
2171 m = t.GetMethod ("invoke_pass_ref");
2172 AssertThrows<ArgumentException> (delegate {
2173 v = this_obj.InvokeMethod (e.Thread, m, null);
2176 // invalid type of argument (ref != primitive)
2177 m = t.GetMethod ("invoke_pass_ref");
2178 AssertThrows<ArgumentException> (delegate {
2179 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2182 // invalid type of argument (primitive != primitive)
2183 m = t.GetMethod ("invoke_pass_primitive_2");
2184 AssertThrows<ArgumentException> (delegate {
2185 v = this_obj.InvokeMethod (e.Thread, m, new Value [] { vm.CreateValue (1) });
2188 // invoking a non-static method as static
2189 m = t.GetMethod ("invoke_pass_ref");
2190 AssertThrows<ArgumentException> (delegate {
2191 v = t.InvokeMethod (e.Thread, m, new Value [] { vm.RootDomain.CreateString ("ABC") });
2194 // invoking a method defined in another class
2195 m = t2.GetMethod ("invoke");
2196 AssertThrows<ArgumentException> (delegate {
2197 v = this_obj.InvokeMethod (e.Thread, m, null);
2202 public void InvokeVType () {
2203 Event e = run_until ("invoke1");
2205 StackFrame frame = e.Thread.GetFrames () [0];
2207 var s = frame.GetArgument (1) as StructMirror;
2209 TypeMirror t = s.Type;
2214 // Pass struct as this, receive int
2215 m = t.GetMethod ("invoke_return_int");
2216 v = s.InvokeMethod (e.Thread, m, null);
2217 AssertValue (42, v);
2219 // Pass struct as this, receive intptr
2220 m = t.GetMethod ("invoke_return_intptr");
2221 v = s.InvokeMethod (e.Thread, m, null);
2222 AssertValue (43, v);
2225 m = t.GetMethod ("invoke_static");
2226 v = t.InvokeMethod (e.Thread, m, null);
2229 // Pass generic struct as this
2230 s = frame.GetArgument (2) as StructMirror;
2232 m = t.GetMethod ("invoke_return_int");
2233 v = s.InvokeMethod (e.Thread, m, null);
2234 AssertValue (42, v);
2237 // Invoke a method which changes state
2238 s = frame.GetArgument (1) as StructMirror;
2240 m = t.GetMethod ("invoke_mutate");
2241 var task = s.InvokeMethodAsyncWithResult (e.Thread, m, null, InvokeOptions.ReturnOutThis);
2242 var out_this = task.Result.OutThis as StructMirror;
2243 AssertValue (5, out_this ["l"]);
2245 // Without the ReturnOutThis flag
2246 s = frame.GetArgument (1) as StructMirror;
2248 m = t.GetMethod ("invoke_mutate");
2249 task = s.InvokeMethodAsyncWithResult (e.Thread, m, null);
2250 out_this = task.Result.OutThis as StructMirror;
2251 Assert.AreEqual (null, out_this);
2256 public void BreakpointDuringInvoke () {
2257 Event e = run_until ("invoke1");
2259 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke2");
2260 Assert.IsNotNull (m);
2261 vm.SetBreakpoint (m, 0);
2263 StackFrame frame = e.Thread.GetFrames () [0];
2264 var o = frame.GetThis () as ObjectMirror;
2266 bool failed = false;
2268 bool finished = false;
2269 object wait = new object ();
2271 // Have to invoke in a separate thread as the invoke is suspended until we
2272 // resume after the breakpoint
2273 Thread t = new Thread (delegate () {
2275 o.InvokeMethod (e.Thread, m, null);
2281 Monitor.Pulse (wait);
2287 StackFrame invoke_frame = null;
2290 e = GetNextEvent ();
2291 Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
2292 // Check stack trace support and invokes
2293 var frames = e.Thread.GetFrames ();
2294 invoke_frame = frames [0];
2295 Assert.AreEqual ("invoke2", frames [0].Method.Name);
2296 Assert.IsTrue (frames [0].IsDebuggerInvoke);
2297 Assert.AreEqual ("invoke1", frames [1].Method.Name);
2304 Monitor.Wait (wait);
2307 // Check that the invoke frames are no longer valid
2308 AssertThrows<InvalidStackFrameException> (delegate {
2309 invoke_frame.GetThis ();
2312 // Check InvokeOptions.DisableBreakpoints flag
2313 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2317 public void DisabledExceptionDuringInvoke () {
2318 Event e = run_until ("invoke_ex");
2320 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_ex_inner");
2322 StackFrame frame = e.Thread.GetFrames () [0];
2323 var o = frame.GetThis () as ObjectMirror;
2325 var req = vm.CreateExceptionRequest (null);
2328 // Check InvokeOptions.DisableBreakpoints flag
2329 o.InvokeMethod (e.Thread, m, null, InvokeOptions.DisableBreakpoints);
2335 public void InvokeSingleThreaded () {
2338 Start (new string [] { "dtest-app.exe", "invoke-single-threaded" });
2340 Event e = run_until ("invoke_single_threaded_2");
2342 StackFrame f = e.Thread.GetFrames ()[0];
2344 var obj = f.GetThis () as ObjectMirror;
2346 // Check that the counter value incremented by the other thread does not increase
2347 // during the invoke.
2348 object counter1 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2350 var m = obj.Type.GetMethod ("invoke_return_void");
2351 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2353 object counter2 = (obj.GetValue (obj.Type.GetField ("counter")) as PrimitiveValue).Value;
2355 Assert.AreEqual ((int)counter1, (int)counter2);
2357 // Test multiple invokes done in succession
2358 m = obj.Type.GetMethod ("invoke_return_void");
2359 obj.InvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded);
2361 // Test events during single-threaded invokes
2362 vm.EnableEvents (EventType.TypeLoad);
2363 m = obj.Type.GetMethod ("invoke_type_load");
2364 obj.BeginInvokeMethod (e.Thread, m, null, InvokeOptions.SingleThreaded, delegate {
2368 e = GetNextEvent ();
2369 Assert.AreEqual (EventType.TypeLoad, e.EventType);
2372 List<Value> invoke_results;
2375 public void InvokeMultiple () {
2376 Event e = run_until ("invoke1");
2378 StackFrame frame = e.Thread.GetFrames () [0];
2380 TypeMirror t = frame.Method.DeclaringType;
2381 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2383 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2385 var methods = new MethodMirror [2];
2386 methods [0] = t.GetMethod ("invoke_return_ref");
2387 methods [1] = t.GetMethod ("invoke_return_primitive");
2389 invoke_results = new List<Value> ();
2391 var r = this_obj.BeginInvokeMultiple (e.Thread, methods, null, InvokeOptions.SingleThreaded, invoke_multiple_cb, this_obj);
2392 WaitHandle.WaitAll (new WaitHandle[] { r.AsyncWaitHandle });
2393 this_obj.EndInvokeMultiple (r);
2394 // The callback might still be running
2395 while (invoke_results.Count < 2) {
2398 if (invoke_results [0] is PrimitiveValue) {
2399 AssertValue ("ABC", invoke_results [1]);
2400 AssertValue (42, invoke_results [0]);
2402 AssertValue ("ABC", invoke_results [0]);
2403 AssertValue (42, invoke_results [1]);
2407 void invoke_multiple_cb (IAsyncResult ar) {
2408 ObjectMirror this_obj = (ObjectMirror)ar.AsyncState;
2410 var res = this_obj.EndInvokeMethod (ar);
2411 lock (invoke_results)
2412 invoke_results.Add (res);
2416 public void GetThreads () {
2421 public void Threads () {
2422 Event e = run_until ("threads");
2424 Assert.AreEqual (ThreadState.Running, e.Thread.ThreadState);
2426 Assert.IsTrue (e.Thread.ThreadId > 0);
2428 Assert.AreEqual (e.Thread.TID, e.Thread.TID);
2430 vm.EnableEvents (EventType.ThreadStart, EventType.ThreadDeath);
2434 e = GetNextEvent ();
2435 Assert.IsInstanceOfType (typeof (ThreadStartEvent), e);
2436 var state = e.Thread.ThreadState;
2437 Assert.IsTrue (state == ThreadState.Running || state == ThreadState.Unstarted);
2441 e = GetNextEvent ();
2442 Assert.IsInstanceOfType (typeof (ThreadDeathEvent), e);
2443 Assert.AreEqual (ThreadState.Stopped, e.Thread.ThreadState);
2447 public void Frame_SetValue () {
2448 Event e = run_until ("locals2");
2450 StackFrame frame = e.Thread.GetFrames () [0];
2453 var l = frame.Method.GetLocal ("i");
2454 frame.SetValue (l, vm.CreateValue ((long)55));
2455 AssertValue (55, frame.GetValue (l));
2458 l = frame.Method.GetLocal ("s");
2459 frame.SetValue (l, vm.RootDomain.CreateString ("DEF"));
2460 AssertValue ("DEF", frame.GetValue (l));
2462 // argument as local
2463 l = frame.Method.GetLocal ("arg");
2464 frame.SetValue (l, vm.CreateValue (6));
2465 AssertValue (6, frame.GetValue (l));
2468 var p = frame.Method.GetParameters ()[1];
2469 frame.SetValue (p, vm.CreateValue (7));
2470 AssertValue (7, frame.GetValue (p));
2473 p = frame.Method.GetParameters ()[2];
2474 frame.SetValue (p, vm.RootDomain.CreateString ("DEF"));
2475 AssertValue ("DEF", frame.GetValue (p));
2478 p = frame.Method.GetParameters ()[3];
2479 frame.SetValue (p, vm.RootDomain.CreateString ("DEF2"));
2480 AssertValue ("DEF2", frame.GetValue (p));
2483 p = frame.Method.GetParameters ()[4];
2484 var v = frame.GetValue (p) as StructMirror;
2485 v ["i"] = vm.CreateValue (43);
2486 frame.SetValue (p, v);
2487 v = frame.GetValue (p) as StructMirror;
2488 AssertValue (43, v ["i"]);
2490 // argument checking
2493 AssertThrows<ArgumentNullException> (delegate () {
2494 frame.SetValue ((LocalVariable)null, vm.CreateValue (55));
2498 AssertThrows<ArgumentNullException> (delegate () {
2499 l = frame.Method.GetLocal ("i");
2500 frame.SetValue (l, null);
2503 // value of invalid type
2504 AssertThrows<ArgumentException> (delegate () {
2505 l = frame.Method.GetLocal ("i");
2506 frame.SetValue (l, vm.CreateValue (55));
2512 public void Frame_SetValue_Registers () {
2513 Event e = run_until ("locals6_1");
2515 StackFrame frame = e.Thread.GetFrames () [1];
2518 var l = frame.Method.GetLocal ("j");
2519 frame.SetValue (l, vm.CreateValue (99));
2520 AssertValue (99, frame.GetValue (l));
2522 // Check it during execution
2523 e = run_until ("locals6_2");
2524 frame = e.Thread.GetFrames () [0];
2525 AssertValue (99, frame.GetValue (frame.Method.GetParameters ()[0]));
2527 // Set it while in a frame which clobbers its register
2528 e = run_until ("locals6_3");
2529 frame = e.Thread.GetFrames () [1];
2530 frame.SetValue (l, vm.CreateValue (100));
2531 AssertValue (100, frame.GetValue (l));
2533 // Check it during execution
2534 e = run_until ("locals6_4");
2535 frame = e.Thread.GetFrames () [0];
2536 AssertValue (100, frame.GetValue (frame.Method.GetParameters ()[0]));
2538 // Signed byte value
2539 e = run_until ("locals6_5");
2540 frame = e.Thread.GetFrames () [1];
2541 var l2 = frame.Method.GetLocal ("sb");
2542 frame.SetValue (l2, vm.CreateValue ((sbyte)-99));
2543 AssertValue (-99, frame.GetValue (l2));
2545 // Check it during execution
2546 e = run_until ("locals6_6");
2547 frame = e.Thread.GetFrames () [0];
2548 AssertValue (-99, frame.GetValue (frame.Method.GetParameters ()[0]));
2552 public void InvokeRegress () {
2553 Event e = run_until ("invoke1");
2555 StackFrame frame = e.Thread.GetFrames () [0];
2557 TypeMirror t = frame.Method.DeclaringType;
2558 ObjectMirror this_obj = (ObjectMirror)frame.GetThis ();
2560 TypeMirror t2 = frame.Method.GetParameters ()[0].ParameterType;
2566 m = t.GetMethod ("invoke_return_void");
2567 v = this_obj.InvokeMethod (e.Thread, m, null);
2570 // Check that the stack frames remain valid during the invoke
2571 Assert.AreEqual ("Tests", (frame.GetThis () as ObjectMirror).Type.Name);
2573 // do another invoke
2574 m = t.GetMethod ("invoke_return_void");
2575 v = this_obj.InvokeMethod (e.Thread, m, null);
2578 // Try a single step after the invoke
2579 var req = create_step (e);
2580 req.Depth = StepDepth.Into;
2581 req.Size = StepSize.Line;
2587 // Step into invoke2
2589 e = GetNextEvent ();
2590 Assert.IsTrue (e is StepEvent);
2591 Assert.AreEqual ("invoke2", (e as StepEvent).Method.Name);
2595 frame = e.Thread.GetFrames () [0];
2599 public void Exceptions () {
2600 Event e = run_until ("exceptions");
2601 var req = vm.CreateExceptionRequest (null);
2606 e = GetNextEvent ();
2607 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2608 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2610 var frames = e.Thread.GetFrames ();
2611 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2614 // exception type filter
2616 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.ArgumentException"));
2619 // Skip the throwing of the second OverflowException
2622 e = GetNextEvent ();
2623 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2624 Assert.AreEqual ("ArgumentException", (e as ExceptionEvent).Exception.Type.Name);
2627 // exception type filter for subclasses
2628 req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.Exception"));
2633 e = GetNextEvent ();
2634 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2635 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2639 req.IncludeSubclasses = false;
2644 e = GetNextEvent ();
2645 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2646 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
2649 // Implicit exceptions
2650 req = vm.CreateExceptionRequest (null);
2655 e = GetNextEvent ();
2656 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2657 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2660 // Single stepping after an exception
2661 req = vm.CreateExceptionRequest (null);
2666 e = GetNextEvent ();
2667 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2668 Assert.AreEqual ("Exception", (e as ExceptionEvent).Exception.Type.Name);
2669 frames = e.Thread.GetFrames ();
2670 Assert.AreEqual ("exceptions2", frames [0].Method.Name);
2673 var sreq = create_step (e);
2674 sreq.Depth = StepDepth.Over;
2675 sreq.Size = StepSize.Line;
2679 e = GetNextEvent ();
2680 Assert.IsInstanceOfType (typeof (StepEvent), e);
2681 frames = e.Thread.GetFrames ();
2682 Assert.AreEqual ("exceptions", frames [0].Method.Name);
2685 // Argument checking
2686 AssertThrows<ArgumentException> (delegate {
2687 vm.CreateExceptionRequest (e.Thread.Type);
2692 public void ExceptionFilter () {
2693 Event e = run_until ("exception_filter");
2695 MethodMirror m = entry_point.DeclaringType.GetMethod ("exception_filter_filter");
2696 Assert.IsNotNull (m);
2698 vm.SetBreakpoint (m, 0);
2702 e = GetNextEvent ();
2703 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2704 Assert.IsTrue (e is BreakpointEvent);
2705 Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
2707 var frames = e.Thread.GetFrames ();
2709 Assert.IsTrue (frames [0].Location.SourceFile.IndexOf ("dtest-app.cs") != -1);
2710 Assert.AreEqual ("exception_filter_filter", frames [0].Location.Method.Name);
2712 Assert.AreEqual (0, frames [1].Location.Method.MetadataToken);
2713 Assert.AreEqual (0x0f, frames [1].Location.ILOffset);
2715 Assert.AreEqual ("exception_filter_method", frames [2].Location.Method.Name);
2716 Assert.AreEqual (0x06, frames [2].Location.ILOffset);
2718 Assert.AreEqual (0, frames [3].Location.Method.MetadataToken, 0);
2719 Assert.AreEqual (0, frames [3].Location.ILOffset);
2721 Assert.AreEqual ("exception_filter", frames [4].Location.Method.Name);
2725 public void ExceptionFilter2 () {
2728 Start (new string [] { "dtest-excfilter.exe" });
2730 MethodMirror filter_method = entry_point.DeclaringType.GetMethod ("Filter");
2731 Assert.IsNotNull (filter_method);
2733 MethodMirror test_method = entry_point.DeclaringType.GetMethod ("Test");
2734 Assert.IsNotNull (test_method);
2736 vm.SetBreakpoint (filter_method, 0);
2740 var e = GetNextEvent ();
2741 Assert.AreEqual (EventType.Breakpoint, e.EventType);
2742 Assert.IsTrue (e is BreakpointEvent);
2743 Assert.AreEqual (filter_method.Name, (e as BreakpointEvent).Method.Name);
2745 var frames = e.Thread.GetFrames ();
2747 Assert.AreEqual (4, frames.Count ());
2749 Assert.AreEqual (filter_method.Name, frames [0].Location.Method.Name);
2750 Assert.AreEqual (20, frames [0].Location.LineNumber);
2751 Assert.AreEqual (0, frames [0].Location.ILOffset);
2753 Assert.AreEqual (test_method.Name, frames [1].Location.Method.Name);
2754 Assert.AreEqual (37, frames [1].Location.LineNumber);
2755 Assert.AreEqual (0x0b, frames [1].Location.ILOffset);
2757 Assert.AreEqual (test_method.Name, frames [2].Location.Method.Name);
2758 Assert.AreEqual (33, frames [2].Location.LineNumber);
2759 Assert.AreEqual (0x05, frames [2].Location.ILOffset);
2761 Assert.AreEqual (entry_point.Name, frames [3].Location.Method.Name);
2762 Assert.AreEqual (14, frames [3].Location.LineNumber);
2763 Assert.AreEqual (0x00, frames [3].Location.ILOffset);
2771 public void EventSets () {
2773 // Create two filter which both match the same exception
2775 Event e = run_until ("exceptions");
2777 var req = vm.CreateExceptionRequest (null);
2780 var req2 = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.OverflowException"));
2785 var es = vm.GetNextEventSet ();
2786 Assert.AreEqual (2, es.Events.Length);
2789 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2790 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2793 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2794 Assert.AreEqual ("OverflowException", (e as ExceptionEvent).Exception.Type.Name);
2801 // Test single threaded invokes during processing of nullref exceptions.
2802 // These won't work if the exception handling is done from the sigsegv signal
2803 // handler, since the sigsegv signal is disabled until control returns from the
2807 [Category ("only3")]
2808 public void NullRefExceptionAndSingleThreadedInvoke () {
2809 Event e = run_until ("exceptions");
2810 var req = vm.CreateExceptionRequest (vm.RootDomain.Corlib.GetType ("System.NullReferenceException"));
2815 e = GetNextEvent ();
2816 Assert.IsInstanceOfType (typeof (ExceptionEvent), e);
2817 Assert.AreEqual ("NullReferenceException", (e as ExceptionEvent).Exception.Type.Name);
2819 var ex = (e as ExceptionEvent).Exception;
2820 var tostring_method = vm.RootDomain.Corlib.GetType ("System.Object").GetMethod ("ToString");
2821 ex.InvokeMethod (e.Thread, tostring_method, null, InvokeOptions.SingleThreaded);
2825 public void Domains () {
2828 Start (new string [] { "dtest-app.exe", "domain-test" });
2830 vm.EnableEvents (EventType.AppDomainCreate, EventType.AppDomainUnload, EventType.AssemblyUnload);
2832 Event e = run_until ("domains");
2836 e = GetNextEvent ();
2837 Assert.IsInstanceOfType (typeof (AppDomainCreateEvent), e);
2839 var domain = (e as AppDomainCreateEvent).Domain;
2841 // Check the object type
2842 e = run_until ("domains_2");
2843 var frame = e.Thread.GetFrames ()[0];
2844 var o = frame.GetArgument (0) as ObjectMirror;
2845 Assert.AreEqual ("CrossDomain", o.Type.Name);
2847 // Do a remoting invoke
2848 var cross_domain_type = o.Type;
2849 var v = o.InvokeMethod (e.Thread, cross_domain_type.GetMethod ("invoke_3"), null);
2850 AssertValue (42, v);
2852 // Run until the callback in the domain
2853 MethodMirror m = entry_point.DeclaringType.GetMethod ("invoke_in_domain");
2854 Assert.IsNotNull (m);
2855 vm.SetBreakpoint (m, 0);
2859 e = GetNextEvent ();
2860 if (e is BreakpointEvent)
2864 Assert.AreEqual ("invoke_in_domain", (e as BreakpointEvent).Method.Name);
2866 // d_method is from another domain
2867 MethodMirror d_method = (e as BreakpointEvent).Method;
2868 Assert.IsTrue (m != d_method);
2870 var frames = e.Thread.GetFrames ();
2871 Assert.AreEqual ("invoke_in_domain", frames [0].Method.Name);
2872 Assert.AreEqual ("invoke", frames [1].Method.Name);
2873 Assert.AreEqual ("domains", frames [2].Method.Name);
2875 // Test breakpoints on already JITted methods in other domains
2876 m = entry_point.DeclaringType.GetMethod ("invoke_in_domain_2");
2877 Assert.IsNotNull (m);
2878 vm.SetBreakpoint (m, 0);
2882 e = GetNextEvent ();
2883 if (e is BreakpointEvent)
2887 Assert.AreEqual ("invoke_in_domain_2", (e as BreakpointEvent).Method.Name);
2889 // This is empty when receiving the AppDomainCreateEvent
2890 Assert.AreEqual ("domain", domain.FriendlyName);
2892 // Run until the unload
2895 e = GetNextEvent ();
2896 if (e is AssemblyUnloadEvent) {
2902 Assert.IsInstanceOfType (typeof (AppDomainUnloadEvent), e);
2903 Assert.AreEqual (domain, (e as AppDomainUnloadEvent).Domain);
2905 // Run past the unload
2906 e = run_until ("domains_3");
2908 // Test access to unloaded types
2909 // FIXME: Add an exception type for this
2910 AssertThrows<Exception> (delegate {
2911 d_method.DeclaringType.GetValue (d_method.DeclaringType.GetField ("static_i"));
2916 public void DynamicMethods () {
2917 Event e = run_until ("dyn_call");
2919 var m = e.Thread.GetFrames ()[1].Method;
2920 Assert.AreEqual ("dyn_method", m.Name);
2922 // Test access to IL
2923 var body = m.GetMethodBody ();
2925 ILInstruction ins = body.Instructions [0];
2926 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2927 Assert.AreEqual ("FOO", ins.Operand);
2931 public void RefEmit () {
2934 Start (new string [] { "dtest-app.exe", "ref-emit-test" });
2936 Event e = run_until ("ref_emit_call");
2938 var m = e.Thread.GetFrames ()[1].Method;
2939 Assert.AreEqual ("ref_emit_method", m.Name);
2941 // Test access to IL
2942 var body = m.GetMethodBody ();
2946 ins = body.Instructions [0];
2947 Assert.AreEqual (OpCodes.Ldstr, ins.OpCode);
2948 Assert.AreEqual ("FOO", ins.Operand);
2950 ins = body.Instructions [1];
2951 Assert.AreEqual (OpCodes.Call, ins.OpCode);
2952 Assert.IsInstanceOfType (typeof (MethodMirror), ins.Operand);
2953 Assert.AreEqual ("ref_emit_call", (ins.Operand as MethodMirror).Name);
2957 public void IsAttached () {
2958 var f = entry_point.DeclaringType.GetField ("is_attached");
2960 Event e = run_until ("Main");
2962 AssertValue (true, entry_point.DeclaringType.GetValue (f));
2966 public void StackTraceInNative () {
2967 // Check that stack traces can be produced for threads in native code
2970 Start (new string [] { "dtest-app.exe", "frames-in-native" });
2972 var e = run_until ("frames_in_native");
2974 // FIXME: This is racy
2981 StackFrame[] frames = e.Thread.GetFrames ();
2983 int frame_index = -1;
2984 for (int i = 0; i < frames.Length; ++i) {
2985 if (frames [i].Method.Name == "Sleep") {
2991 Assert.IsTrue (frame_index != -1);
2992 Assert.AreEqual ("Sleep", frames [frame_index].Method.Name);
2993 Assert.AreEqual ("frames_in_native", frames [frame_index + 1].Method.Name);
2994 Assert.AreEqual ("Main", frames [frame_index + 2].Method.Name);
2996 // Check that invokes are disabled for such threads
2997 TypeMirror t = frames [frame_index + 1].Method.DeclaringType;
2999 var m = t.GetMethod ("invoke_static_return_void");
3000 AssertThrows<InvalidOperationException> (delegate {
3001 t.InvokeMethod (e.Thread, m, null);
3004 // Check that the frame info is invalidated
3005 run_until ("frames_in_native_2");
3007 AssertThrows<InvalidStackFrameException> (delegate {
3008 Console.WriteLine (frames [frame_index].GetThis ());
3013 public void VirtualMachine_CreateEnumMirror () {
3014 var e = run_until ("o1");
3015 var frame = e.Thread.GetFrames () [0];
3017 object val = frame.GetThis ();
3018 Assert.IsTrue (val is ObjectMirror);
3019 Assert.AreEqual ("Tests", (val as ObjectMirror).Type.Name);
3020 ObjectMirror o = (val as ObjectMirror);
3022 FieldInfoMirror field = o.Type.GetField ("field_enum");
3023 Value f = o.GetValue (field);
3024 TypeMirror enumType = (f as EnumMirror).Type;
3026 o.SetValue (field, vm.CreateEnumMirror (enumType, vm.CreateValue (1)));
3027 f = o.GetValue (field);
3028 Assert.AreEqual (1, (f as EnumMirror).Value);
3030 // Argument checking
3031 AssertThrows<ArgumentNullException> (delegate () {
3032 vm.CreateEnumMirror (enumType, null);
3035 AssertThrows<ArgumentNullException> (delegate () {
3036 vm.CreateEnumMirror (null, vm.CreateValue (1));
3040 AssertThrows<ArgumentException> (delegate () {
3041 vm.CreateEnumMirror (enumType, vm.CreateValue (null));
3044 // value of a wrong type
3045 AssertThrows<ArgumentException> (delegate () {
3046 vm.CreateEnumMirror (enumType, vm.CreateValue ((long)1));
3051 public void VirtualMachine_EnableEvents_Breakpoint () {
3052 AssertThrows<ArgumentException> (delegate () {
3053 vm.EnableEvents (EventType.Breakpoint);
3058 public void SingleStepRegress654694 () {
3061 MethodMirror m = entry_point.DeclaringType.GetMethod ("ss_regress_654694");
3062 foreach (Location l in m.Locations) {
3063 if (l.ILOffset > 0 && il_offset == -1)
3064 il_offset = l.ILOffset;
3067 Event e = run_until ("ss_regress_654694");
3069 Assert.IsNotNull (m);
3070 vm.SetBreakpoint (m, il_offset);
3074 e = GetNextEvent ();
3075 Assert.IsTrue (e is BreakpointEvent);
3077 var req = create_step (e);
3078 req.Depth = StepDepth.Over;
3079 req.Size = StepSize.Line;
3084 e = GetNextEvent ();
3085 Assert.IsTrue (e is StepEvent);
3091 public void DebugBreak () {
3092 vm.EnableEvents (EventType.UserBreak);
3097 var e = GetNextEvent ();
3098 Assert.IsTrue (e is UserBreakEvent);
3102 public void DebugLog () {
3103 vm.EnableEvents (EventType.UserLog);
3108 var e = GetNextEvent ();
3109 Assert.IsTrue (e is UserLogEvent);
3110 var le = e as UserLogEvent;
3112 Assert.AreEqual (5, le.Level);
3113 Assert.AreEqual ("A", le.Category);
3114 Assert.AreEqual ("B", le.Message);
3118 public void TypeGetMethodsByNameFlags () {
3120 var assembly = entry_point.DeclaringType.Assembly;
3121 var type = assembly.GetType ("Tests3");
3123 Assert.IsNotNull (type);
3125 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Public, false);
3126 Assert.AreEqual (1, mm.Length, "#1");
3127 Assert.AreEqual ("M1", mm[0].Name, "#2");
3129 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.NonPublic, false);
3130 Assert.AreEqual (1, mm.Length, "#3");
3131 Assert.AreEqual ("M2", mm[0].Name, "#4");
3133 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public, false);
3134 Assert.AreEqual (7, mm.Length, "#5"); //M3 plus Equals, GetHashCode, GetType, ToString, .ctor
3136 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, false);
3137 Assert.AreEqual (2, mm.Length, "#7");
3139 mm = type.GetMethodsByNameFlags (null, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3140 Assert.AreEqual (1, mm.Length, "#9");
3142 mm = type.GetMethodsByNameFlags (null, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, false);
3143 Assert.AreEqual (5, mm.Length, "#11");
3146 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Public, false);
3147 Assert.AreEqual (1, mm.Length, "#12");
3148 Assert.AreEqual ("M1", mm[0].Name, "#13");
3150 mm = type.GetMethodsByNameFlags ("m1", BindingFlags.Static | BindingFlags.Public, true);
3151 Assert.AreEqual (1, mm.Length, "#14");
3152 Assert.AreEqual ("M1", mm[0].Name, "#15");
3154 mm = type.GetMethodsByNameFlags ("M1", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, false);
3155 Assert.AreEqual (1, mm.Length, "#16");
3156 Assert.AreEqual ("M1", mm[0].Name, "#17");
3160 [Category ("only88")]
3161 public void TypeLoadSourceFileFilter () {
3162 Event e = run_until ("type_load");
3164 if (!vm.Version.AtLeast (2, 7))
3167 string srcfile = (e as BreakpointEvent).Method.DeclaringType.GetSourceFiles (true)[0];
3169 var req = vm.CreateTypeLoadRequest ();
3170 req.SourceFileFilter = new string [] { srcfile.ToUpper () };
3174 e = GetNextEvent ();
3175 Assert.IsTrue (e is TypeLoadEvent);
3176 Assert.AreEqual ("TypeLoadClass", (e as TypeLoadEvent).Type.FullName);
3180 public void TypeLoadTypeNameFilter () {
3181 Event e = run_until ("type_load");
3183 var req = vm.CreateTypeLoadRequest ();
3184 req.TypeNameFilter = new string [] { "TypeLoadClass2" };
3188 e = GetNextEvent ();
3189 Assert.IsTrue (e is TypeLoadEvent);
3190 Assert.AreEqual ("TypeLoadClass2", (e as TypeLoadEvent).Type.FullName);
3194 public void GetTypesForSourceFile () {
3197 var types = vm.GetTypesForSourceFile ("dtest-app.cs", false);
3198 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3199 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3201 types = vm.GetTypesForSourceFile ("DTEST-app.cs", true);
3202 Assert.IsTrue (types.Any (t => t.FullName == "Tests"));
3203 Assert.IsFalse (types.Any (t => t.FullName == "System.Int32"));
3207 public void GetTypesNamed () {
3210 var types = vm.GetTypes ("Tests", false);
3211 Assert.AreEqual (1, types.Count);
3212 Assert.AreEqual ("Tests", types [0].FullName);
3214 types = vm.GetTypes ("System.Exception", false);
3215 Assert.AreEqual (1, types.Count);
3216 Assert.AreEqual ("System.Exception", types [0].FullName);
3220 public void String_GetChars () {
3224 var e = run_until ("arg2");
3226 var frame = e.Thread.GetFrames () [0];
3228 val = frame.GetArgument (0);
3229 Assert.IsTrue (val is StringMirror);
3230 AssertValue ("FOO", val);
3231 var s = (val as StringMirror);
3232 Assert.AreEqual (3, s.Length);
3234 var c = s.GetChars (0, 2);
3235 Assert.AreEqual (2, c.Length);
3236 Assert.AreEqual ('F', c [0]);
3237 Assert.AreEqual ('O', c [1]);
3239 AssertThrows<ArgumentException> (delegate () {
3245 public void GetInterfaces () {
3246 var e = run_until ("arg2");
3248 var frame = e.Thread.GetFrames () [0];
3250 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3251 var ifaces = cl1.GetInterfaces ();
3252 Assert.AreEqual (1, ifaces.Length);
3253 Assert.AreEqual ("ITest", ifaces [0].Name);
3255 var cl2 = cl1.GetMethod ("Baz").ReturnType;
3256 var ifaces2 = cl2.GetInterfaces ();
3257 Assert.AreEqual (1, ifaces2.Length);
3258 Assert.AreEqual ("ITest`1", ifaces2 [0].Name);
3262 public void GetInterfaceMap () {
3263 var e = run_until ("arg2");
3265 var frame = e.Thread.GetFrames () [0];
3267 var cl1 = frame.Method.DeclaringType.Assembly.GetType ("TestIfaces");
3268 var iface = cl1.Assembly.GetType ("ITest");
3269 var map = cl1.GetInterfaceMap (iface);
3270 Assert.AreEqual (cl1, map.TargetType);
3271 Assert.AreEqual (iface, map.InterfaceType);
3272 Assert.AreEqual (2, map.InterfaceMethods.Length);
3273 Assert.AreEqual (2, map.TargetMethods.Length);
3277 public void StackAlloc_Breakpoints_Regress2775 () {
3278 // Check that breakpoints on arm don't overwrite stackalloc-ed memory
3279 var e = run_until ("regress_2755");
3281 var frame = e.Thread.GetFrames () [0];
3283 // This breaks at the call site
3284 vm.SetBreakpoint (m, m.Locations [2].ILOffset);
3287 var e2 = GetNextEvent ();
3288 Assert.IsTrue (e2 is BreakpointEvent);
3290 e = run_until ("regress_2755_3");
3291 frame = e.Thread.GetFrames () [1];
3292 var res = frame.GetValue (m.GetLocal ("sum"));
3293 AssertValue (0, res);
3297 public void MethodInfo () {
3298 Event e = run_until ("locals2");
3300 StackFrame frame = e.Thread.GetFrames () [0];
3301 var m = frame.Method;
3303 Assert.IsTrue (m.IsGenericMethod);
3304 Assert.IsFalse (m.IsGenericMethodDefinition);
3306 var args = m.GetGenericArguments ();
3307 Assert.AreEqual (1, args.Length);
3308 Assert.AreEqual ("String", args [0].Name);
3310 var gmd = m.GetGenericMethodDefinition ();
3311 Assert.IsTrue (gmd.IsGenericMethod);
3312 Assert.IsTrue (gmd.IsGenericMethodDefinition);
3313 Assert.AreEqual (gmd, gmd.GetGenericMethodDefinition ());
3315 args = gmd.GetGenericArguments ();
3316 Assert.AreEqual (1, args.Length);
3317 Assert.AreEqual ("T", args [0].Name);
3319 var attrs = m.GetCustomAttributes (true);
3320 Assert.AreEqual (1, attrs.Length);
3321 Assert.AreEqual ("StateMachineAttribute", attrs [0].Constructor.DeclaringType.Name);
3325 public void UnhandledException () {
3328 Start (new string [] { "dtest-app.exe", "unhandled-exception" });
3330 var req = vm.CreateExceptionRequest (null, false, true);
3333 var e = run_until ("unhandled_exception");
3336 var e2 = GetNextEvent ();
3337 Assert.IsTrue (e2 is ExceptionEvent);
3344 public void UnhandledException_2 () {
3347 Start (new string [] { "dtest-app.exe", "unhandled-exception-endinvoke" });
3349 var req = vm.CreateExceptionRequest (null, false, true);
3352 MethodMirror m = entry_point.DeclaringType.GetMethod ("unhandled_exception_endinvoke_2");
3353 Assert.IsNotNull (m);
3354 vm.SetBreakpoint (m, m.ILOffsets [0]);
3356 var e = run_until ("unhandled_exception_endinvoke");
3359 var e2 = GetNextEvent ();
3360 Assert.IsFalse (e2 is ExceptionEvent);
3368 public void UnhandledExceptionUserCode () {
3371 // Exceptions caught in non-user code are treated as unhandled
3372 Start (new string [] { "dtest-app.exe", "unhandled-exception-user" });
3374 var req = vm.CreateExceptionRequest (null, false, true);
3375 req.AssemblyFilter = new List<AssemblyMirror> () { entry_point.DeclaringType.Assembly };
3378 var e = run_until ("unhandled_exception_user");
3381 var e2 = GetNextEvent ();
3382 Assert.IsTrue (e2 is ExceptionEvent);
3390 public void GCWhileSuspended () {
3391 // Check that objects are kept alive during suspensions
3392 Event e = run_until ("gc_suspend_1");
3394 MethodMirror m = entry_point.DeclaringType.GetMethod ("gc_suspend_invoke");
3396 var o = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
3397 //Console.WriteLine (o);
3399 StackFrame frame = e.Thread.GetFrames () [0];
3400 TypeMirror t = frame.Method.DeclaringType;
3401 for (int i = 0; i < 10; ++i)
3402 t.InvokeMethod (e.Thread, m, new Value [] { });
3404 // This throws an exception if the object is collected
3405 long addr = o.Address;
3407 var o2 = entry_point.DeclaringType.GetValue (entry_point.DeclaringType.GetField ("gc_suspend_field")) as ObjectMirror;
3412 public void MakeGenericMethod () {
3413 Event e = run_until ("bp1");
3415 var intm = vm.RootDomain.GetCorrespondingType (typeof (int));
3416 var stringm = vm.RootDomain.GetCorrespondingType (typeof (string));
3417 var gm = entry_point.DeclaringType.GetMethod ("generic_method");
3418 var res = gm.MakeGenericMethod (new TypeMirror [] { stringm });
3419 var args = res.GetGenericArguments ();
3420 Assert.AreEqual (1, args.Length);
3421 Assert.AreEqual (stringm, args [0]);
3424 AssertThrows<ArgumentNullException> (delegate {
3425 gm.MakeGenericMethod (null);
3427 AssertThrows<ArgumentNullException> (delegate {
3428 gm.MakeGenericMethod (new TypeMirror [] { null });
3430 AssertThrows<ArgumentException> (delegate {
3431 gm.MakeGenericMethod (new TypeMirror [] { stringm, stringm });
3433 AssertThrows<InvalidOperationException> (delegate {
3434 gm.MakeGenericMethod (new TypeMirror [] { intm });
3436 AssertThrows<InvalidOperationException> (delegate {
3437 entry_point.DeclaringType.GetMethod ("Main").MakeGenericMethod (new TypeMirror [] { intm });
3442 public void InspectThreadSuspenedOnWaitOne () {
3444 Start (true, "dtest-app.exe", "wait-one" );
3446 ThreadMirror.NativeTransitions = true;
3448 var evt = run_until ("wait_one");
3449 Assert.IsNotNull (evt, "#1");
3451 var thread = evt.Thread;
3452 Assert.AreEqual (ThreadState.Running, thread.ThreadState, "#1.1");
3454 var frames = thread.GetFrames ();
3455 Assert.IsNotNull (frames, "#2");
3456 Assert.AreEqual (2, frames.Length, "#3");
3457 Assert.AreEqual ("wait_one", frames [0].Method.Name, "#4");
3458 Assert.AreEqual ("Main", frames [1].Method.Name, "#5");
3462 Thread.Sleep (500); //FIXME this is racy, maybe single step? or something?
3465 Assert.AreEqual (ThreadState.WaitSleepJoin, thread.ThreadState, "#6");
3467 frames = thread.GetFrames ();
3468 Assert.AreEqual (4, frames.Length, "#7");
3469 Assert.AreEqual ("WaitOne_internal", frames [0].Method.Name, "#8");
3470 Assert.AreEqual ("WaitOne", frames [1].Method.Name, "#8.1");
3471 Assert.AreEqual ("wait_one", frames [2].Method.Name, "#9");
3472 Assert.AreEqual ("Main", frames [3].Method.Name, "#10");
3475 var frame = frames [0];
3476 Assert.IsTrue (frame.IsNativeTransition, "#11.1");
3479 Assert.Fail ("Known limitation - can't get info from m2n frames");
3480 } catch (AbsentInformationException) {}
3483 Assert.IsFalse (frame.IsNativeTransition, "#12.1");
3484 var wait_one_this = frame.GetThis ();
3485 Assert.IsNotNull (wait_one_this, "#12.2");
3488 var locals = frame.GetVisibleVariables ();
3489 Assert.AreEqual (1, locals.Count, "#13.1");
3491 var local_0 = frame.GetValue (locals [0]);
3492 Assert.IsNotNull (local_0, "#13.2");
3494 Assert.AreEqual (wait_one_this, local_0, "#14.2");
3498 public void GetMethodBody () {
3499 var bevent = run_until ("Main");
3501 var m = bevent.Method.DeclaringType.GetMethod ("get_IntProperty");
3502 var body = m.GetMethodBody ();
3503 foreach (var ins in body.Instructions) {
3504 if (ins.OpCode == OpCodes.Ldfld) {
3505 var field = (FieldInfoMirror)ins.Operand;
3506 Assert.AreEqual ("field_i", field.Name);
3512 public void EvaluateMethod () {
3513 var bevent = run_until ("evaluate_method_2");
3515 var m = bevent.Method.DeclaringType.GetMethod ("get_IntProperty");
3517 var this_obj = bevent.Thread.GetFrames ()[0].GetThis ();
3518 var v = m.Evaluate (this_obj, null);
3519 AssertValue (42, v);
3523 public void SetIP () {
3524 var bevent = run_until ("set_ip_1");
3526 var invalid_loc = bevent.Thread.GetFrames ()[0].Location;
3528 var req = create_step (bevent);
3529 var e = step_out ();
3531 var frames = e.Thread.GetFrames ();
3532 var locs = frames [0].Method.Locations;
3533 var next_loc = locs.First (l => (l.LineNumber == frames [0].Location.LineNumber + 2));
3535 e.Thread.SetIP (next_loc);
3537 /* Check that i = 5; j = 5; was skipped */
3538 bevent = run_until ("set_ip_2");
3539 var f = bevent.Thread.GetFrames ()[1];
3540 AssertValue (1, f.GetValue (f.Method.GetLocal ("i")));
3541 AssertValue (0, f.GetValue (f.Method.GetLocal ("j")));
3544 AssertThrows<ArgumentNullException> (delegate {
3545 e.Thread.SetIP (null);
3548 AssertThrows<ArgumentException> (delegate {
3549 e.Thread.SetIP (invalid_loc);
3554 public void NewInstanceNoCtor () {
3555 var bevent = run_until ("Main");
3557 var stype = bevent.Method.DeclaringType.Assembly.GetType ("AStruct");
3558 var obj = stype.NewInstance ();
3559 Assert.IsTrue (obj is ObjectMirror);
3560 Assert.AreEqual ("AStruct", (obj as ObjectMirror).Type.Name);