-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
dtest.cs
4832 lines (3828 loc) · 137 KB
/
dtest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Threading;
using System.Net;
using System.Reflection;
using System.Text;
using Mono.Cecil.Cil;
using Mono.Debugger.Soft;
using Diag = System.Diagnostics;
using System.Linq;
using System.IO;
using System.Security.Cryptography;
using System.Threading.Tasks;
using NUnit.Framework;
#pragma warning disable 0219
namespace MonoTests
{
[TestFixture]
public class DebuggerTests
{
VirtualMachine vm;
MethodMirror entry_point;
StepEventRequest step_req;
bool forceExit;
void AssertThrows<ExType> (Action del) where ExType : Exception {
bool thrown = false;
try {
del ();
} catch (ExType) {
thrown = true;
}
Assert.IsTrue (thrown);
}
// No other way to pass arguments to the tests ?
#if MONODROID_TEST
public static bool listening = true;
#else
public static bool listening = Environment.GetEnvironmentVariable ("DBG_SUSPEND") != null;
#endif
public static string runtime = Environment.GetEnvironmentVariable ("DBG_RUNTIME");
public static string runtime_args = Environment.GetEnvironmentVariable ("DBG_RUNTIME_ARGS");
public static string agent_args = Environment.GetEnvironmentVariable ("DBG_AGENT_ARGS");
#if MONODROID_TEST
// AssemblyInfo.Location doesn't work on Android, we expect test .exe's to be in the working dir
public static string this_assembly_path = "";
#else
// expect test .exe's to be next to this assembly
public static string this_assembly_path = Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location);
#endif
public static string dtest_app_path = Path.Combine (this_assembly_path, "dtest-app.exe");
// Not currently used, but can be useful when debugging individual tests.
void StackTraceDump (Event e)
{
int i = 0;
foreach (var frame in e.Thread.GetFrames ())
{
i++;
Console.WriteLine ("Frame " + i + ", " + frame.Method.Name);
}
}
Event GetNextEvent () {
var es = vm.GetNextEventSet ();
Assert.AreEqual (1, es.Events.Length);
return es [0];
}
void Start (params string[] args) {
Start (false, args);
}
Diag.ProcessStartInfo CreateStartInfo (string[] args) {
var pi = new Diag.ProcessStartInfo ();
pi.RedirectStandardOutput = true;
pi.RedirectStandardError = true;
if (runtime != null) {
pi.FileName = runtime;
} else {
string processExe = Diag.Process.GetCurrentProcess ().MainModule.FileName;
if (processExe != null) {
string fileName = Path.GetFileName (processExe);
if (fileName.StartsWith ("mono"))
pi.FileName = processExe;
}
}
if (string.IsNullOrEmpty (pi.FileName))
throw new ArgumentException ("Couldn't find mono runtime.");
pi.Arguments = String.Join (" ", args);
if (runtime_args != null)
pi.Arguments = runtime_args + " " + pi.Arguments;
return pi;
}
void Start (bool forceExit, params string[] args) {
this.forceExit = forceExit;
if (!listening) {
var pi = CreateStartInfo (args);
vm = VirtualMachineManager.Launch (pi, new LaunchOptions { AgentArgs = agent_args });
} else {
#if MONODROID_TEST
System.Diagnostics.Process.Start("/usr/bin/make", "-C ../android dirty-run-debugger-test");
#endif
var ep = new IPEndPoint (IPAddress.Any, 6100);
Console.WriteLine ("Listening on " + ep + "...");
vm = VirtualMachineManager.Listen (ep);
}
var load_req = vm.CreateAssemblyLoadRequest ();
load_req.Enable ();
Event vmstart = GetNextEvent ();
Assert.AreEqual (EventType.VMStart, vmstart.EventType);
vm.Resume ();
entry_point = null;
step_req = null;
Event e;
/* Find out the entry point */
while (true) {
e = GetNextEvent ();
if (e is AssemblyLoadEvent) {
AssemblyLoadEvent ae = (AssemblyLoadEvent)e;
entry_point = ae.Assembly.EntryPoint;
if (entry_point != null)
break;
}
vm.Resume ();
}
load_req.Disable ();
if (args.Length == 2) {
var this_type = entry_point.DeclaringType;
var str = vm.RootDomain.CreateString (args [1]);
var slot = this_type.GetField ("arg");
if (slot == null)
throw new Exception ("Missing slot");
if (str == null)
throw new Exception ("Bug in createstring");
this_type.SetValue (slot, str);
} else if (args.Length > 2) {
throw new Exception (String.Format ("Fixme {0}", args [2]));
}
}
BreakpointEvent run_until (string name) {
// String
MethodMirror m = entry_point.DeclaringType.GetMethod (name);
Assert.IsNotNull (m);
//Console.WriteLine ("X: " + name + " " + m.ILOffsets.Count + " " + m.Locations.Count);
var req = vm.SetBreakpoint (m, m.ILOffsets [0]);
Event e = null;
while (true) {
vm.Resume ();
e = GetNextEvent ();
if (e is BreakpointEvent)
break;
}
req.Disable ();
Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
return (e as BreakpointEvent);
}
class ReusableBreakpoint {
DebuggerTests owner;
public string method_name;
public BreakpointEventRequest req;
public BreakpointEvent lastEvent = null;
public ReusableBreakpoint (DebuggerTests owner, string method_name)
{
this.owner = owner;
this.method_name = method_name;
MethodMirror m = owner.entry_point.DeclaringType.GetMethod (method_name);
Assert.IsNotNull (m);
req = owner.vm.SetBreakpoint (m, m.ILOffsets [0]);
}
public void Continue ()
{
bool survived = false;
try {
Event e = null;
while (true) {
owner.vm.Resume ();
e = owner.GetNextEvent ();
if (e is BreakpointEvent)
break;
}
Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
Assert.AreEqual (method_name, (e as BreakpointEvent).Method.Name);
lastEvent = e as BreakpointEvent;
survived = true;
} finally {
if (!survived) { // Ensure cleanup if we triggered an assert
Disable ();
}
}
}
public void Disable ()
{
req.Disable ();
}
}
/* One of the tests executes a complex tree of recursive functions.
The only good way to specify how its behavior should appear from this side
is to just run the function tree once over here and record what it does. */
public struct RecursiveChaoticPoint
{
public bool breakpoint;
public string name;
public int depth;
public RecursiveChaoticPoint (bool breakpoint, string name, int depth)
{
this.breakpoint = breakpoint;
this.name = name;
this.depth = depth;
}
}
// The breakpoint is placed here in dtest-app.cs
public static void ss_recursive_chaotic_trap (int n, List<RecursiveChaoticPoint> trace, ref bool didLast, ref bool didAny)
{
// Depth is calculated as:
// Main + single_stepping + ss_recursive_chaotic + (n is 5 at outermost frame and 0 at innermost frame) + ss_recursive_chaotic_trap
trace.Add (new RecursiveChaoticPoint (true, "ss_recursive_chaotic_trap", 5 - n + 5));
didLast = true;
}
public static void ss_recursive_chaotic_at (string at, int n, List<RecursiveChaoticPoint> trace, ref bool didLast, ref bool didAny)
{
// This will be called after every return from a function. The other function will return whether "step out" is currently active, and it will be passed in here as didLast.
if (didLast) {
// Depth is calculated as:
// Main + single_stepping + ss_recursive_chaotic + (n is 5 at outermost frame and 0 at innermost frame)
trace.Add (new RecursiveChaoticPoint (false, "ss_recursive_chaotic_" + at, 5 - n + 4));
didAny = true;
didLast = false;
}
}
public static bool ss_recursive_chaotic_fizz (int n, List<RecursiveChaoticPoint> trace)
{
bool didLast = false, didAny = false;
if (n > 0) {
int next = n - 1;
didLast = ss_recursive_chaotic_buzz (next, trace);
ss_recursive_chaotic_at ("fizz", n, trace, ref didLast, ref didAny);
didLast = ss_recursive_chaotic_fizzbuzz (next, trace);
ss_recursive_chaotic_at ("fizz", n, trace, ref didLast, ref didAny);
} else {
ss_recursive_chaotic_trap (n, trace, ref didLast, ref didAny);
ss_recursive_chaotic_at ("fizz", n, trace, ref didLast, ref didAny);
}
return didAny;
}
public static bool ss_recursive_chaotic_buzz (int n, List<RecursiveChaoticPoint> trace)
{
bool didLast = false, didAny = false;
if (n > 0) {
int next = n - 1;
didLast = ss_recursive_chaotic_fizz (next, trace);
ss_recursive_chaotic_at ("buzz", n, trace, ref didLast, ref didAny);
didLast = ss_recursive_chaotic_fizzbuzz (next, trace);
ss_recursive_chaotic_at ("buzz", n, trace, ref didLast, ref didAny);
}
return didAny;
}
public static bool ss_recursive_chaotic_fizzbuzz (int n, List<RecursiveChaoticPoint> trace)
{
bool didLast = false, didAny = false;
if (n > 0) {
int next = n - 1;
didLast = ss_recursive_chaotic_fizz (next, trace);
ss_recursive_chaotic_at ("fizzbuzz", n, trace, ref didLast, ref didAny);
didLast = ss_recursive_chaotic_buzz (next, trace);
ss_recursive_chaotic_at ("fizzbuzz", n, trace, ref didLast, ref didAny);
didLast = ss_recursive_chaotic_fizzbuzz (next, trace);
ss_recursive_chaotic_at ("fizzbuzz", n, trace, ref didLast, ref didAny);
}
return didAny;
}
public static void trace_ss_recursive_chaotic (List<RecursiveChaoticPoint> trace)
{
ss_recursive_chaotic_fizz (5, trace);
}
Event single_step (ThreadMirror t) {
var req = vm.CreateStepRequest (t);
req.Enable ();
vm.Resume ();
Event e = GetNextEvent ();
Assert.IsTrue (e is StepEvent);
req.Disable ();
return e;
}
Event step_until (ThreadMirror t, string method_name) {
Event e;
while (true) {
e = single_step (t);
if ((e as StepEvent).Method.Name == method_name)
break;
}
return e;
}
void check_arg_val (StackFrame frame, int pos, Type type, object eval) {
object val = frame.GetArgument (pos);
Assert.IsTrue (val is PrimitiveValue);
object v = (val as PrimitiveValue).Value;
Assert.AreEqual (type, v.GetType ());
if (eval is float)
Assert.IsTrue (Math.Abs ((float)eval - (float)v) < 0.0001);
else if (eval is double)
Assert.IsTrue (Math.Abs ((double)eval - (double)v) < 0.0001);
else
Assert.AreEqual (eval, v);
}
void AssertValue (object expected, object val) {
if (expected is string) {
Assert.IsTrue (val is StringMirror);
Assert.AreEqual (expected, (val as StringMirror).Value);
} else if (val is StructMirror && (val as StructMirror).Type.Name == "IntPtr") {
AssertValue (expected, (val as StructMirror).Fields [0]);
} else if (val is PointerValue) {
Assert.AreEqual (expected, (val as PointerValue).Address);
} else {
Assert.IsTrue (val is PrimitiveValue);
Assert.AreEqual (expected, (val as PrimitiveValue).Value);
}
}
[SetUp]
public void SetUp () {
ThreadMirror.NativeTransitions = false;
Start (new string [] { dtest_app_path });
}
[TearDown]
public void TearDown () {
if (vm == null)
return;
if (step_req != null)
step_req.Disable ();
vm.Resume ();
if (forceExit)
vm.Exit (0);
while (true) {
Event e = GetNextEvent ();
if (e is VMDeathEvent)
break;
vm.Resume ();
}
vm = null;
}
[Test]
public void SimpleBreakpoint () {
Event e;
MethodMirror m = entry_point.DeclaringType.GetMethod ("bp1");
Assert.IsNotNull (m);
vm.SetBreakpoint (m, 0);
vm.Resume ();
e = GetNextEvent ();
Assert.AreEqual (EventType.Breakpoint, e.EventType);
Assert.IsTrue (e is BreakpointEvent);
Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
// Argument checking
AssertThrows<ArgumentException> (delegate {
// Invalid IL offset
vm.SetBreakpoint (m, 2);
});
}
[Test]
public void BreakpointsSameLocation () {
MethodMirror m = entry_point.DeclaringType.GetMethod ("bp2");
Assert.IsNotNull (m);
vm.SetBreakpoint (m, 0);
vm.SetBreakpoint (m, 0);
vm.Resume ();
var es = vm.GetNextEventSet ();
Assert.AreEqual (2, es.Events.Length);
Assert.IsTrue (es [0] is BreakpointEvent);
Assert.AreEqual (m, (es [0] as BreakpointEvent).Method);
Assert.IsTrue (es [1] is BreakpointEvent);
Assert.AreEqual (m.Name, (es [1] as BreakpointEvent).Method.Name);
}
[Test]
public void MetadataAndPdbTest () {
run_until ("bp1");
var assemblyMirrors = entry_point.DeclaringType.Assembly.Domain.GetAssemblies ();
Assert.IsTrue (assemblyMirrors.Length > 0);
foreach (var assemblyMirror in assemblyMirrors) {
assemblyMirror.GetMetadata ();
var metadata = assemblyMirror.Metadata;
Assert.AreEqual (metadata.MainModule.Mvid, assemblyMirror.ManifestModule.ModuleVersionId);
var location = assemblyMirror.Location;
if (!File.Exists (location))
continue;
Assert.IsFalse (assemblyMirror.IsDynamic);
var rawDataFromMemory = assemblyMirror.GetMetadataBlob ();
var rawDataFromDisk = File.ReadAllBytes (location);
Assert.IsTrue (rawDataFromMemory.SequenceEqual (rawDataFromDisk));
string pdbPath = Path.ChangeExtension (location, "pdb");
Assert.IsFalse (assemblyMirror.HasPdb);
Assert.IsFalse (assemblyMirror.HasFetchedPdb);
var pdbFromMemory = assemblyMirror.GetPdbBlob ();
if (!File.Exists (pdbPath)) {
Assert.IsFalse (assemblyMirror.HasPdb);
Assert.IsTrue (assemblyMirror.HasFetchedPdb);
continue;
}
Assert.IsTrue (pdbFromMemory != null && pdbFromMemory.Length > 0);
var pdbFromDisk = File.ReadAllBytes (pdbPath);
Assert.IsTrue (pdbFromMemory.SequenceEqual (pdbFromDisk));
Assert.IsTrue (assemblyMirror.HasPdb);
Assert.IsTrue (assemblyMirror.HasFetchedPdb);
foreach (var type in metadata.Modules.SelectMany (x => x.GetTypes ())) {
var typeMirror = assemblyMirror.GetType (type.MetadataToken.ToUInt32 ());
Assert.AreEqual (type.Name, typeMirror.Name);
Assert.AreEqual (type.MetadataToken.ToInt32 (), typeMirror.MetadataToken);
int methodCount = 0;
foreach (var method in type.Methods) {
var methodMirror = assemblyMirror.GetMethod (method.MetadataToken.ToUInt32 ());
Assert.AreEqual (method.Name, methodMirror.Name);
Assert.AreEqual (method.MetadataToken.ToInt32 (), methodMirror.MetadataToken);
if (methodCount++ > 10)
break;
}
if (type.IsNested)
break;
}
}
}
[Test]
public void IsDynamicAssembly () {
vm.Detach ();
Start (new string[] { dtest_app_path, "ref-emit-test"});
run_until ("ref_emit_call");
var assemblyMirrors = entry_point.DeclaringType.Assembly.Domain.GetAssemblies ();
bool dynamicAssemblyFound = false;
foreach (var assemblyMirror in assemblyMirrors) {
if (assemblyMirror.GetName ().Name == "foo") {
dynamicAssemblyFound = true;
Assert.IsTrue (assemblyMirror.IsDynamic);
}
else
Assert.IsFalse (assemblyMirror.IsDynamic);
}
Assert.IsTrue (dynamicAssemblyFound);
}
[Test]
public void BreakpointAlreadyJITted () {
Event e = run_until ("bp1");
/* Place a breakpoint on bp3 */
MethodMirror m = entry_point.DeclaringType.GetMethod ("bp3");
Assert.IsNotNull (m);
vm.SetBreakpoint (m, 0);
/* Same with generic instances */
MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp7");
Assert.IsNotNull (m2);
vm.SetBreakpoint (m2, 0);
vm.Resume ();
e = GetNextEvent ();
Assert.AreEqual (EventType.Breakpoint, e.EventType);
Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
vm.Resume ();
/* Non-shared instance */
e = GetNextEvent ();
Assert.AreEqual (EventType.Breakpoint, e.EventType);
Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
vm.Resume ();
/* Shared instance */
e = GetNextEvent ();
Assert.AreEqual (EventType.Breakpoint, e.EventType);
Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
}
[Test]
public void ClearBreakpoint () {
Event e;
MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
Assert.IsNotNull (m);
EventRequest req1 = vm.SetBreakpoint (m, 0);
EventRequest req2 = vm.SetBreakpoint (m, 0);
MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
Assert.IsNotNull (m2);
vm.SetBreakpoint (m2, 0);
/* Run until bp4 */
vm.Resume ();
var es = vm.GetNextEventSet ();
Assert.AreEqual (2, es.Events.Length);
Assert.AreEqual (EventType.Breakpoint, es [0].EventType);
Assert.AreEqual (m.Name, (es [0] as BreakpointEvent).Method.Name);
Assert.AreEqual (EventType.Breakpoint, es [1].EventType);
Assert.AreEqual (m.Name, (es [1] as BreakpointEvent).Method.Name);
/* Clear one of them */
req1.Disable ();
vm.Resume ();
e = GetNextEvent ();
Assert.AreEqual (EventType.Breakpoint, e.EventType);
Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
/* Clear the other */
req2.Disable ();
vm.Resume ();
e = GetNextEvent ();
Assert.AreEqual (EventType.Breakpoint, e.EventType);
Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
}
[Test]
public void ClearAllBreakpoints () {
Event e;
MethodMirror m = entry_point.DeclaringType.GetMethod ("bp4");
Assert.IsNotNull (m);
vm.SetBreakpoint (m, 0);
MethodMirror m2 = entry_point.DeclaringType.GetMethod ("bp5");
Assert.IsNotNull (m2);
vm.SetBreakpoint (m2, 0);
vm.ClearAllBreakpoints ();
vm.Resume ();
e = GetNextEvent ();
Assert.IsTrue (!(e is BreakpointEvent));
if (e is VMDeathEvent)
vm = null;
}
[Test]
public void BreakpointOnGShared () {
Event e;
MethodMirror m = entry_point.DeclaringType.GetMethod ("bp6");
Assert.IsNotNull (m);
vm.SetBreakpoint (m, 0);
vm.Resume ();
e = GetNextEvent ();
Assert.AreEqual (EventType.Breakpoint, e.EventType);
Assert.IsTrue (e is BreakpointEvent);
Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
// Breakpoint on an open generic method of a closed generic class (#3422)
var frame = e.Thread.GetFrames ()[0];
var ginst = frame.GetValue (frame.Method.GetLocal ("gc"));
var m2 = (ginst as ObjectMirror).Type.GetMethod ("bp");
vm.SetBreakpoint (m2, 0);
vm.Resume ();
e = GetNextEvent ();
Assert.AreEqual (EventType.Breakpoint, e.EventType);
Assert.IsTrue (e is BreakpointEvent);
Assert.AreEqual (m2.Name, (e as BreakpointEvent).Method.Name);
}
// Assert we have stepped to a location
void assert_location (Event e, string method) {
Assert.IsTrue (e is StepEvent);
Assert.AreEqual (method, (e as StepEvent).Method.Name);
}
// Assert we have breakpointed at a location
void assert_location_at_breakpoint (Event e, string method) {
Assert.IsTrue (e is BreakpointEvent);
Assert.AreEqual (method, (e as BreakpointEvent).Method.Name);
}
// Assert we have stepped to or breakpointed at a location
void assert_location_allow_breakpoint (Event e, string method) {
if (e is StepEvent)
Assert.AreEqual (method, (e as StepEvent).Method.Name);
else if (e is BreakpointEvent)
Assert.AreEqual (method, (e as BreakpointEvent).Method.Name);
else
Assert.Fail ("Neither step nor breakpoint event");
}
StepEventRequest create_step (Event e) {
var req = vm.CreateStepRequest (e.Thread);
step_req = req;
return req;
}
[Test]
public void ClassLocalReflection () {
vm.Detach ();
Start (new string [] { dtest_app_path, "local-reflect" });
MethodMirror m = entry_point.DeclaringType.Assembly.GetType ("LocalReflectClass").GetMethod ("RunMe");
Assert.IsNotNull (m);
// foreach (var x in m.Locations) {
// Console.WriteLine (x);
// }
var offset = -1;
int method_base_linum = m.Locations [0].LineNumber;
foreach (var location in m.Locations)
if (location.LineNumber == method_base_linum + 2) {
offset = location.ILOffset;
break;
}
var req = vm.SetBreakpoint (m, offset);
Event e = null;
while (true) {
vm.Resume ();
e = GetNextEvent ();
if (e is BreakpointEvent)
break;
}
req.Disable ();
Assert.IsInstanceOfType (typeof (BreakpointEvent), e);
Assert.AreEqual (m.Name, (e as BreakpointEvent).Method.Name);
e = single_step (e.Thread);
var frame = e.Thread.GetFrames ()[0];
Assert.IsNotNull (frame);
var field = frame.Method.GetLocal ("reflectMe");
Assert.IsNotNull (field);
Value variable = frame.GetValue (field);
ObjectMirror thisObj = (ObjectMirror)variable;
TypeMirror thisType = thisObj.Type;
FieldInfoMirror thisFi = null;
foreach (var fi in thisType.GetFields ())
if (fi.Name == "someField")
thisFi = fi;
var gotVal = thisObj.GetValue (thisFi);
// If we got this far, we're good.
}
[Test]
public void SingleStepping () {
Event e = run_until ("single_stepping");
var req = create_step (e);
req.Enable ();
// Step over 'bool b = true'
e = step_once ();
assert_location (e, "single_stepping");
// Skip nop
step_once ();
// Step into ss1
e = step_once ();
assert_location (e, "ss1");
// Skip }
e = step_once ();
// Step out of ss1
e = step_once ();
assert_location (e, "single_stepping");
// Step over ss2
e = step_over ();
assert_location (e, "single_stepping");
// Step into ss3
e = step_into ();
assert_location (e, "ss3");
// Step back into single_stepping
e = step_out ();
assert_location (e, "single_stepping");
// Step into next line
e = step_into ();
assert_location (e, "single_stepping");
// Step into ss3_2 ()
e = step_into ();
assert_location (e, "ss3_2");
// Step over ss3_2_2 ()
e = step_over ();
assert_location (e, "ss3_2");
// Recreate the request
req.Disable ();
req.Enable ();
// Skip }
e = step_once ();
// Step back into single_stepping () with the new request
e = step_once ();
assert_location (e, "single_stepping");
// Step into ss4 ()
e = step_into ();
assert_location (e, "ss4");
// Skip nop
e = step_once ();
// Change to StepSize.Line
req.Disable ();
req.Depth = StepDepth.Over;
req.Size = StepSize.Line;
req.Enable ();
// Step over ss1 (); ss1 ();
e = step_once ();
// Step into ss2 ()
req.Disable ();
req.Depth = StepDepth.Into;
req.Enable ();
e = step_once ();
assert_location (e, "ss2");
req.Disable ();
// Run until ss5
e = run_until ("ss5");
// Add an assembly filter
req.AssemblyFilter = new AssemblyMirror [] { (e as BreakpointEvent).Method.DeclaringType.Assembly };
req.Enable ();
// Skip nop
e = step_once ();
// Step into is_even, skipping the linq stuff
e = step_once ();
assert_location (e, "is_even");
// FIXME: Check that single stepping works with lock (obj)
req.Disable ();
// Run until ss6
e = run_until ("ss6");
req = create_step (e);
req.Depth = StepDepth.Over;
req.Enable ();
// Check that single stepping works in out-of-line bblocks
e = step_once ();
e = step_once ();
assert_location (e, "ss6");
req.Disable ();
// Testing stepping in, over and out with exception handlers in same or caller method
//stepout in ss7_2, which may not go to catch(instead out to ss7)
e = run_until ("ss7_2");
create_step (e);
assert_location (step_out(), "ss7");
//stepout in ss7_2_1, which must go to catch
run_until ("ss7_2_1");
assert_location (step_out (), "ss7_2");
//stepover over ss7_2, which must go to catch
run_until ("ss7_2");
assert_location (step_over (), "ss7_2");//move to "try {" line
assert_location (step_over (), "ss7_2");//move to "ss7_2_2();" line
assert_location (step_over (), "ss7_2");//step over ss7_2_2();, assume we are at "catch" now
assert_location (step_over (), "ss7_2");//move to { of catch
assert_location (step_over (), "ss7_2");//move to } of catch
assert_location (step_over (), "ss7_2");//move to } of method
assert_location (step_over (), "ss7");//finish method
//stepover over ss7_2_1, which must go to catch
run_until ("ss7_2_1");
assert_location (step_over (), "ss7_2_1");//move from { of method to "throw new Exception ();"
assert_location (step_over (), "ss7_2");//step over exception, being in ss7_2 means we are at catch
//stepin in ss7_3, which must go to catch
run_until ("ss7_3");
assert_location (step_into (), "ss7_3");//move to "try {"
assert_location (step_into (), "ss7_3");//move to "throw new Exception ();"
step_req.Disable ();
step_req.AssemblyFilter = new AssemblyMirror [] { (e as BreakpointEvent).Method.DeclaringType.Assembly };
assert_location (step_into (), "ss7_3");//call "throw new Exception ();", we assume we end up at "catch"
assert_location (step_into (), "ss7_3");//move to { of catch
assert_location (step_into (), "ss7_3");//move to } of catch
assert_location (step_into (), "ss7_3");//move to } of method
assert_location (step_into (), "ss7");//move out to ss7
//stepover in ss7_2_1, which must go to catch
run_until ("ss7_2_1");
assert_location (step_into (), "ss7_2_1");//move from { of method to "throw new Exception ();"
assert_location (step_into (), "ss7_2");//step in exception, being in ss7_2 means we are at catch
step_req.Disable ();
// Check that stepping stops between nested calls
e = run_until ("ss_nested_2");
req = create_step (e);
e = step_out ();
assert_location (e, "ss_nested");
e = step_into ();
assert_location (e, "ss_nested_1");
e = step_out ();
assert_location (e, "ss_nested");
// Check that step over steps over nested calls
e = step_over ();
assert_location (e, "ss_nested");
e = step_into ();
assert_location (e, "ss_nested_2");
e = step_into ();
assert_location (e, "ss_nested_2");
e = step_into ();
assert_location (e, "ss_nested_2");
e = step_into ();
assert_location (e, "ss_nested");
e = step_into ();
assert_location (e, "ss_nested_1");
e = step_into ();
assert_location (e, "ss_nested_1");
e = step_into ();
assert_location (e, "ss_nested");
req.Disable ();
// Check DebuggerStepThrough support
e = run_until ("ss_step_through");
req = create_step (e);
req.Filter = StepFilter.DebuggerStepThrough;
e = step_into ();
// Step through step_through_1 ()
e = step_into ();
assert_location (e, "ss_step_through");
// Step through StepThroughClass.step_through_2 ()
e = step_into ();
assert_location (e, "ss_step_through");
req.Disable ();
req.Filter = StepFilter.None;
e = step_into ();
assert_location (e, "step_through_3");
req.Disable ();
// Check DebuggerNonUserCode support
e = run_until ("ss_non_user_code");
req = create_step (e);
req.Filter = StepFilter.DebuggerNonUserCode;
e = step_into ();
// Step through non_user_code_1 ()
e = step_into ();
assert_location (e, "ss_non_user_code");
// Step through StepThroughClass.non_user_code_2 ()
e = step_into ();
assert_location (e, "ss_non_user_code");
req.Disable ();
req.Filter = StepFilter.None;
e = step_into ();
assert_location (e, "non_user_code_3");
req.Disable ();
// Check that step-over doesn't stop at inner frames with recursive functions
e = run_until ("ss_recursive");
req = create_step (e);
e = step_over ();
e = step_over ();
e = step_over ();
var f = e.Thread.GetFrames () [0];
assert_location (e, "ss_recursive");
AssertValue (1, f.GetValue (f.Method.GetLocal ("n")));
req.Disable ();
// Check that step-over stops correctly when inner frames with recursive functions contain breakpoints
e = run_until ("ss_recursive2");
ReusableBreakpoint breakpoint = new ReusableBreakpoint (this, "ss_recursive2_trap");
try {
breakpoint.Continue ();
e = breakpoint.lastEvent;
req = create_step (e);
for (int c = 1; c <= 4; c++) {
// The first five times we try to step over this function, the breakpoint will stop us
assert_location_at_breakpoint (e, "ss_recursive2_trap");
req.Disable ();
req = create_step (e);
req.Size = StepSize.Line;
e = step_out ();
e = step_over ();//Stepout gets us to ss_recursive2_trap ();, move to ss_recursive2 (next); line
assert_location (e, "ss_recursive2");
// Stack should consist of Main + single_stepping + (1 ss_recursive2 frame per loop iteration)
Assert.AreEqual (c+2, e.Thread.GetFrames ().Length);
e = step_over_or_breakpoint ();
}
// At this point we should have escaped the breakpoints and this will be a normal step stop
assert_location (e, "ss_recursive2");
Assert.AreEqual (6, e.Thread.GetFrames ().Length);
} finally {
req.Disable ();
breakpoint.Disable ();