This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
/
SingleSteppingEngine.cs
4965 lines (4065 loc) · 138 KB
/
SingleSteppingEngine.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.IO;
using System.Text;
using System.Threading;
using System.Configuration;
using System.Globalization;
using System.Reflection;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Runtime.Remoting.Messaging;
using Mono.Debugger.Languages;
using Mono.Debugger.Languages.Mono;
using Mono.Debugger.Architectures;
using Mono.Debugger.Backend.Mono;
namespace Mono.Debugger.Backend
{
// <summary>
// The single stepping engine is responsible for doing all the stepping
// operations.
//
// sse - short for single stepping engine.
//
// stepping operation - an operation which has been invoked by the user such
// as StepLine(), NextLine() etc.
//
// atomic operation - an operation which the sse invokes on the target
// such as stepping one machine instruction or resuming
// the target until a breakpoint is hit.
//
// step frame - an address range; the sse invokes atomic operations
// until the target hit a breakpoint, received a signal
// or stopped at an address outside this range.
//
// temporary breakpoint - a breakpoint which is automatically removed the next
// time the target stopped; it is used to step over
// method calls.
//
// source stepping op - stepping operation based on the program's source code,
// such as StepLine() or NextLine().
//
// native stepping op - stepping operation based on the machine code such as
// StepInstruction() or NextInstruction().
//
// The SingleSteppingEngine supports both synchronous and asynchronous
// operations; in synchronous mode, the engine waits until the child has stopped
// before returning. In either case, the step commands return true on success
// and false an error.
//
// Since the SingleSteppingEngine can be used from multiple threads at the same
// time, you can no longer safely use the `State' property to find out whether
// the target is stopped or not. It is safe to call all the step commands from
// multiple threads, but for obvious reasons only one command can run at a
// time. So if you attempt to issue a step command while the engine is still
// busy, the step command will return false to signal this error.
// </summary>
// <summary>
// The ThreadManager creates one SingleSteppingEngine instance for each thread
// in the target.
//
// The `SingleSteppingEngine' class is basically just responsible for whatever happens
// in the background thread: processing commands and events. Their methods
// are just meant to be called from the SingleSteppingEngine (since it's a
// protected nested class they can't actually be called from anywhere else).
//
// See the `Thread' class for the "user interface".
// </summary>
internal class SingleSteppingEngine : ThreadServant
{
// <summary>
// This is invoked after compiling a trampoline - it returns whether or
// not we should enter that trampoline.
// </summary>
internal delegate bool TrampolineHandler (Method method);
internal delegate bool CheckBreakpointHandler ();
protected SingleSteppingEngine (ThreadManager manager, ProcessServant process)
: base (manager, process)
{
Report.Debug (DebugFlags.Threads, "New SSE ({0}): {1}",
DebuggerWaitHandle.CurrentThread, this);
}
public SingleSteppingEngine (ThreadManager manager, ProcessServant process,
ProcessStart start)
: this (manager, process)
{
inferior = Inferior.CreateInferior (manager, process, start);
if (start.PID != 0) {
this.pid = start.PID;
inferior.Attach (pid);
} else {
pid = inferior.Run ();
}
manager.AddEngine (this);
}
public SingleSteppingEngine (ThreadManager manager, ProcessServant process,
Inferior inferior, int pid)
: this (manager, process)
{
this.inferior = inferior;
this.pid = pid;
engine_stopped = true;
manager.AddEngine (this);
}
public CommandResult StartApplication (CommandResult result)
{
engine_stopped = false;
current_operation = new OperationStart (this, result);
current_operation.Execute ();
return result;
}
public CommandResult StartExecedChild (CommandResult result)
{
engine_stopped = false;
current_operation = new OperationStart (this, result);
current_operation.Execute ();
return result;
}
public CommandResult StartThread (CommandResult result)
{
engine_stopped = false;
current_operation = new OperationStep (this, StepMode.Run, result);
current_operation.Execute ();
return current_operation.Result;
}
public CommandResult StartForkedChild (CommandResult result)
{
engine_stopped = false;
current_operation = new OperationStep (this, StepMode.Run, result);
PushOperation (new OperationInitAfterFork (this));
return result;
}
#region child event processing
// <summary>
// This is called from the SingleSteppingEngine's main event loop to give
// us the next event - `status' has no meaning to us, it's just meant to
// be passed to inferior.ProcessEvent() to get the actual event.
// </summary>
// <remarks>
// Actually, `status' is the waitpid() status code. In Linux 2.6.x, you
// can call waitpid() from any thread in the debugger, but we need to get
// the target's registers to find out whether it's a breakpoint etc.
// That's done in inferior.ProcessEvent() - which must always be called
// from the engine's thread.
// </remarks>
public void ProcessEvent (int status)
{
if (inferior == null)
return;
ProcessEvent (inferior.ProcessEvent (status));
}
public bool ProcessEvent (Inferior.ChildEvent cevent)
{
Report.Debug (DebugFlags.EventLoop, "{0} received event {1}",
this, cevent);
if (killed) {
if (cevent.Type == Inferior.ChildEventType.CHILD_INTERRUPTED) {
inferior.Continue ();
return true;
} else if (cevent.Type != Inferior.ChildEventType.CHILD_EXITED) {
Report.Debug (DebugFlags.EventLoop,
"{0} received event {1} when already killed",
this, cevent);
return true;
}
}
if ((cevent.Type == Inferior.ChildEventType.CHILD_EXITED) ||
(cevent.Type == Inferior.ChildEventType.CHILD_SIGNALED)) {
Report.Debug (DebugFlags.SSE, "{0} received {1} while running {2}",
this, cevent, current_operation);
// we can't remove the breakpoint anymore after
// the target exited, but we need to clear this id.
temp_breakpoint = null;
dead = true;
} else {
string frame_text = "";
Inferior.StackFrame iframe = inferior.GetCurrentFrame (true);
if (iframe != null)
frame_text = " at " + iframe.Address.ToString ();
string running_text;
if (HasThreadLock)
running_text = String.Format ("being thread-locked ({0})", thread_lock);
else
running_text = String.Format ("running {0}", current_operation);
string event_text;
if (cevent.Type == Inferior.ChildEventType.CHILD_NOTIFICATION)
event_text = String.Format ("notification {0} ({1})", cevent, (NotificationType) cevent.Argument);
else
event_text = "event " + cevent.ToString ();
Report.Debug (DebugFlags.EventLoop, "{0} received {1}{2} while {3}",
this, event_text, frame_text, running_text);
if (HasThreadLock) {
thread_lock.SetStopEvent (cevent);
return false;
}
}
if (ProcessServant.IsAttached && !attach_initialized) {
attach_initialized = true;
if (cevent.Type == Inferior.ChildEventType.CHILD_INTERRUPTED)
cevent = new Inferior.ChildEvent (Inferior.ChildEventType.CHILD_STOPPED, 0, 0, 0);
}
bool resume_target;
if (manager.HandleChildEvent (this, inferior, ref cevent, out resume_target)) {
Report.Debug (DebugFlags.EventLoop,
"{0} done handling event: {1}{2}{3}{4}",
this, cevent, resume_target ? " resume-target" : "" ,
stop_requested ? " stop-requested" : "",
HasThreadLock ? " thread-lock" : "");
if (stop_requested) {
OperationInterrupted ();
} else if (resume_target) {
if (!current_operation.ResumeOperation ())
inferior.Continue ();
}
return true;
}
Inferior.ChildEventType message = cevent.Type;
int arg = (int) cevent.Argument;
switch (message) {
case Inferior.ChildEventType.CHILD_INTERRUPTED:
if (current_operation != null)
OperationInterrupted ();
return true;
case Inferior.ChildEventType.CHILD_SIGNALED:
if (killed)
OperationCompleted (new TargetEventArgs (TargetEventType.TargetExited, 0));
else
OperationCompleted (new TargetEventArgs (TargetEventType.TargetSignaled, arg));
return true;
case Inferior.ChildEventType.INTERNAL_ERROR:
frame_changed (inferior.CurrentFrame, null);
Report.Error ("{0} got {1} at {2} while executing {3}", this, message,
inferior.CurrentFrame, current_operation);
OperationCompleted (new TargetEventArgs (TargetEventType.TargetSignaled, -1));
return true;
case Inferior.ChildEventType.CHILD_EXITED:
OperationCompleted (new TargetEventArgs (TargetEventType.TargetExited, arg));
return true;
case Inferior.ChildEventType.CHILD_CALLBACK_COMPLETED:
frame_changed (inferior.CurrentFrame, null);
OperationCompleted (new TargetEventArgs (TargetEventType.TargetStopped, 0, current_frame));
return true;
case Inferior.ChildEventType.RUNTIME_INVOKE_DONE:
OperationRuntimeInvoke rti = rti_stack.Pop ();
if (rti.ID != cevent.Argument)
throw new InternalError ("{0} got unknown RUNTIME_INVOKE_DONE: {1} {2}", this, rti.ID, cevent);
frame_changed (inferior.CurrentFrame, null);
rti.Completed (cevent.Data1, cevent.Data2);
if (rti.IsSuspended) {
InterruptibleOperation io = nested_break_stack.Pop ();
if (io != rti)
throw new InternalError ("{0} unexpected item on nested break state stack: {1}", this, io);
process.Debugger.OnLeaveNestedBreakState (this);
}
if (current_operation != rti)
current_operation.Result.Completed ();
current_operation = rti;
TargetEventArgs args = rti.OperationCompleted (current_frame, false);
OperationCompleted (args);
return true;
}
if (stop_requested) {
switch (message) {
case Inferior.ChildEventType.CHILD_STOPPED:
case Inferior.ChildEventType.CHILD_CALLBACK:
case Inferior.ChildEventType.CHILD_HIT_BREAKPOINT:
OperationInterrupted ();
return true;
case Inferior.ChildEventType.UNHANDLED_EXCEPTION:
case Inferior.ChildEventType.THROW_EXCEPTION:
case Inferior.ChildEventType.HANDLE_EXCEPTION:
case Inferior.ChildEventType.CHILD_NOTIFICATION:
inferior.RestartNotification ();
OperationInterrupted ();
return true;
default:
OperationInterrupted ();
return false;
}
}
DoProcessEvent (cevent);
return true;
}
protected void DoProcessEvent (Inferior.ChildEvent cevent)
{
Inferior.ChildEventType message = cevent.Type;
int arg = (int) cevent.Argument;
if (message == Inferior.ChildEventType.THROW_EXCEPTION) {
TargetAddress info = new TargetAddress (inferior.AddressDomain, cevent.Data1);
TargetAddress ip = new TargetAddress (manager.AddressDomain, cevent.Data2);
Report.Debug (DebugFlags.EventLoop,
"{0} received exception: {1} {2} {3}", this, message, info, ip);
TargetAddress stack = inferior.ReadAddress (info);
TargetAddress exc = inferior.ReadAddress (info + inferior.TargetAddressSize);
ExceptionAction action = throw_exception (stack, exc, ip);
Report.Debug (DebugFlags.SSE,
"{0} throw exception ({1}:{2}:{3}) - {4} - {5} - {6}",
this, stack, exc, ip, action, current_operation, temp_breakpoint);
switch (action) {
case ExceptionAction.None:
do_continue ();
return;
case ExceptionAction.Stop:
inferior.WriteInteger (info + 2 * inferior.TargetAddressSize, 1);
PushOperation (new OperationException (this, ip, exc, false));
return;
case ExceptionAction.StopUnhandled:
if (!check_runtime_version (81, 1) && !check_runtime_version (80, 1))
goto case ExceptionAction.Stop;
inferior.WriteInteger (info + 4 + 2 * inferior.TargetAddressSize, 1);
do_continue ();
return;
}
}
if (message == Inferior.ChildEventType.HANDLE_EXCEPTION) {
TargetAddress info = new TargetAddress (inferior.AddressDomain, cevent.Data1);
TargetAddress ip = new TargetAddress (manager.AddressDomain, cevent.Data2);
Report.Debug (DebugFlags.EventLoop,
"{0} received exception: {1} {2} {3}", this, message, info, ip);
TargetAddress stack = inferior.ReadAddress (info);
TargetAddress exc = inferior.ReadAddress (info + inferior.TargetAddressSize);
bool stop = handle_exception (stack, exc, ip);
Report.Debug (DebugFlags.SSE,
"{0} {1}stopping at exception handler ({2}:{3}:{4}) - {4} - {5}",
this, stop ? "" : "not ", stack, exc, ip, current_operation, temp_breakpoint);
if (stop) {
inferior.WriteInteger (info + 2 * inferior.TargetAddressSize, 1);
PushOperation (new OperationException (this, ip, exc, false));
return;
}
do_continue ();
return;
}
if (lmf_breakpoint != null) {
if ((message == Inferior.ChildEventType.CHILD_HIT_BREAKPOINT) &&
(arg == lmf_breakpoint.Breakpoint.ID)) {
remove_lmf_breakpoint ();
Report.Debug (DebugFlags.SSE, "{0} back in managed land: {1}",
this, inferior.CurrentFrame);
Method method = Lookup (inferior.CurrentFrame);
bool is_managed = (method != null) && method.Module.Language.IsManaged;
Report.Debug (DebugFlags.SSE, "{0} back in managed land #1: {1}", this, is_managed);
Queue<ManagedCallbackData> queue = process.MonoManager.ClearManagedCallbacks (inferior);
if (!OnManagedCallback (queue))
do_continue ();
return;
}
}
// To step over a method call, the sse inserts a temporary
// breakpoint immediately after the call instruction and then
// resumes the target.
//
// If the target stops and we have such a temporary breakpoint, we
// need to distinguish a few cases:
//
// a) we may have received a signal
// b) we may have hit another breakpoint
// c) we actually hit the temporary breakpoint
//
// In either case, we need to remove the temporary breakpoint if
// the target is to remain stopped. Note that this piece of code
// here only deals with the temporary breakpoint, the handling of
// a signal or another breakpoint is done later.
if ((temp_breakpoint != null) &&
(message == Inferior.ChildEventType.CHILD_HIT_BREAKPOINT) && (arg == temp_breakpoint.ID)) {
// we hit the temporary breakpoint; this'll always
// happen in the `correct' thread since the
// `temp_breakpoint_id' is only set in this
// SingleSteppingEngine and not in any other thread's.
remove_temporary_breakpoint ();
Breakpoint bpt = lookup_breakpoint (arg);
Report.Debug (DebugFlags.SSE,
"{0} hit temporary breakpoint {1} at {2} {3}",
this, arg, inferior.CurrentFrame, bpt);
if ((bpt == null) || !bpt.Breaks (thread.ID) || bpt.HideFromUser) {
message = Inferior.ChildEventType.CHILD_STOPPED;
arg = 0;
cevent = new Inferior.ChildEvent (Inferior.ChildEventType.CHILD_STOPPED, 0, 0, 0);
} else {
ProcessOperationEvent (cevent);
return;
}
}
if (message == Inferior.ChildEventType.UNHANDLED_EXCEPTION) {
TargetAddress exc = new TargetAddress (manager.AddressDomain, cevent.Data1);
TargetAddress ip = new TargetAddress (manager.AddressDomain, cevent.Data2);
PushOperation (new OperationException (this, ip, exc, true));
return;
} else if (message == Inferior.ChildEventType.CHILD_HIT_BREAKPOINT) {
// Ok, the next thing we need to check is whether this is actually "our"
// breakpoint or whether it belongs to another thread. In this case,
// `step_over_breakpoint' does everything for us and we can just continue
// execution.
Breakpoint bpt;
bool remain_stopped = child_breakpoint (cevent, arg, out bpt);
if (!remain_stopped) {
do_continue ();
return;
}
}
ProcessOperationEvent (cevent);
}
protected void ProcessOperationEvent (Inferior.ChildEvent cevent)
{
TargetEventArgs result = null;
Inferior.ChildEventType message = cevent.Type;
int arg = (int) cevent.Argument;
//
// Sometimes, we need to do just one atomic operation - in all
// other cases, `current_operation' is the current stepping
// operation.
//
// ProcessEvent() will either start another atomic operation
// (and return false) or tell us the stepping operation is
// completed by returning true.
//
if (current_operation == null)
throw new InternalError ("SSE {0} has no current operation, but received event {1}", this, cevent);
Report.Debug (DebugFlags.EventLoop, "{0} process operation event: {1} {2}", this, current_operation, cevent);
Operation.EventResult status = current_operation.ProcessEvent (cevent, out result);
Report.Debug (DebugFlags.EventLoop, "{0} processed operation event: {1} {2} {3} {4}", this,
current_operation, cevent, status, result);
switch (status) {
case Operation.EventResult.Running:
return;
case Operation.EventResult.Completed:
case Operation.EventResult.SuspendOperation: {
Operation.EventResult new_status = current_operation.CompletedOperation (cevent, status, ref result);
if (new_status == Operation.EventResult.Running)
return;
else if (new_status == Operation.EventResult.Completed)
OperationCompleted (result);
else if (new_status == Operation.EventResult.SuspendOperation)
OperationCompleted (result, true);
else
throw new InternalError ("Got unexpected event result: {0}", new_status);
return;
}
case Operation.EventResult.CompletedCallback:
OperationCompleted (result);
return;
case Operation.EventResult.ResumeOperation:
if (current_operation.ResumeOperation ())
return;
status = Operation.EventResult.Completed;
goto case Operation.EventResult.Completed;
default:
throw new InternalError ("Got unexpected event result: {0}", status);
}
}
bool check_runtime_version (int major, int minor)
{
if (MonoDebuggerInfo.MajorVersion < major)
return false;
if (MonoDebuggerInfo.MajorVersion > major)
return true;
return MonoDebuggerInfo.MinorVersion >= minor;
}
#endregion
void OperationInterrupted ()
{
frame_changed (inferior.CurrentFrame, null);
long abort_rti = -1;
lock (this) {
abort_rti = abort_requested;
abort_requested = -1;
}
Report.Debug (DebugFlags.SSE, "{0} operation interrupted: {1} - {2}", this, abort_rti, current_frame);
if (abort_rti >= 0) {
DoAbortInvocation (abort_rti);
} else {
if (stop_requested)
OperationCompleted (null);
else
OperationCompleted (new TargetEventArgs (TargetEventType.TargetInterrupted, 0, current_frame));
}
}
OperationRuntimeInvoke AbortRuntimeInvoke (long rti_id)
{
OperationRuntimeInvoke rti = rti_stack.Pop ();
if (rti.ID != rti_id)
throw new InternalError ("{0} aborting rti failed: {1} {2}", this, rti.ID, rti_id);
rti.AbortInvoke ();
if (rti.IsSuspended) {
InterruptibleOperation io = nested_break_stack.Pop ();
if (io != rti)
throw new InternalError ("{0} aborting rti failed: {1}", this, io);
process.Debugger.OnLeaveNestedBreakState (this);
}
return rti;
}
void OperationCompleted (TargetEventArgs args)
{
OperationCompleted (args, false);
}
void OperationCompleted (TargetEventArgs args, bool suspended)
{
lock (this) {
remove_temporary_breakpoint ();
engine_stopped = true;
stop_requested = false;
last_target_event = args;
OperationCommandResult result = current_operation.Result as OperationCommandResult;
Report.Debug (DebugFlags.EventLoop, "{0} {1} operation {2}: {3} {4}",
this, suspended ? "suspending" : "terminating", current_operation,
result != null, args);
if (result != null)
result.Completed (this, args);
operation_completed_event.Set ();
if (suspended) {
process.Debugger.OnEnterNestedBreakState (this);
((InterruptibleOperation) current_operation).IsSuspended = true;
nested_break_stack.Push ((InterruptibleOperation) current_operation);
current_operation.CompletedOperation (true);
current_operation = null;
} else {
current_operation.CompletedOperation (false);
current_operation = null;
}
}
}
internal void OnManagedThreadCreated (TargetAddress end_stack_address)
{
this.end_stack_address = end_stack_address;
}
internal void SetTID (long tid)
{
this.tid = tid;
}
internal void SetManagedThreadData (TargetAddress lmf_address,
TargetAddress extended_notifications_addr)
{
this.lmf_address = lmf_address;
this.extended_notifications_addr = extended_notifications_addr;
}
internal void SetMainReturnAddress (TargetAddress main_ret)
{
if (main_ret.IsNull) {
this.main_retaddr = TargetAddress.Null;
this.reached_main = false;
} else {
this.main_retaddr = main_ret + inferior.TargetAddressSize;
this.reached_main = true;
}
}
internal void OnManagedThreadExited ()
{
this.end_stack_address = TargetAddress.Null;
process.OnManagedThreadExitedEvent (this);
}
internal void OnThreadExited (Inferior.ChildEvent cevent)
{
TargetEventArgs result;
int arg = (int) cevent.Argument;
if (killed)
result = new TargetEventArgs (TargetEventType.TargetExited, 0);
else if (cevent.Type == Inferior.ChildEventType.CHILD_SIGNALED)
result = new TargetEventArgs (TargetEventType.TargetSignaled, arg);
else
result = new TargetEventArgs (TargetEventType.TargetExited, arg);
temp_breakpoint = null;
dead = true;
if (current_operation != null)
OperationCompleted (result);
else
process.Debugger.SendTargetEvent (this, result);
process.OnThreadExitedEvent (this);
Dispose ();
}
Breakpoint lookup_breakpoint (int index)
{
BreakpointHandle handle = process.BreakpointManager.LookupBreakpoint (index);
if (handle == null)
return null;
return handle.Breakpoint;
}
void set_registers (Registers registers)
{
if (!registers.FromCurrentFrame)
throw new InvalidOperationException ();
this.registers = registers;
inferior.SetRegisters (registers);
}
// <summary>
// Start a new stepping operation.
//
// All stepping operations are done asynchronously.
//
// The inferior basically just knows two kinds of stepping operations:
// there is do_continue() to continue execution (until a breakpoint is
// hit or the target receives a signal or exits) and there is do_step_native()
// to single-step one machine instruction. There's also a version of
// do_continue() which takes an address - it inserts a temporary breakpoint
// on that address and calls do_continue().
//
// Let's call these "atomic operations" while a "stepping operation" is
// something like stepping until the next source line. We normally need to
// do several atomic operations for each stepping operation.
//
// We start a new stepping operation here, but what we actually do is
// starting an atomic operation on the target. Note that we just start it,
// but don't wait until is completed. Once the target is running, we go
// back to the main event loop and wait for it (or another thread) to stop
// (or to get another command from the user).
// </summary>
void StartOperation ()
{
lock (this) {
Report.Debug (DebugFlags.SSE, "{0} start operation: {1} {2}",
this, engine_stopped, HasThreadLock);
if (!engine_stopped || HasThreadLock) {
Report.Debug (DebugFlags.Wait, "{0} not stopped: {1} {2}",
this, engine_stopped, HasThreadLock);
throw new TargetException (TargetError.NotStopped);
}
engine_stopped = false;
last_target_event = null;
operation_completed_event.Reset ();
}
}
object SendCommand (TargetAccessDelegate target)
{
if (inferior == null)
throw new TargetException (TargetError.NoTarget);
if (ThreadManager.InBackgroundThread)
return target (thread, null);
else
return manager.SendCommand (this, target, null);
}
CommandResult StartOperation (Operation operation)
{
StartOperation ();
return (CommandResult) SendCommand (delegate {
return ProcessOperation (operation);
});
}
CommandResult ProcessOperation (Operation operation)
{
Report.Debug (DebugFlags.SSE, "{0} starting {1}", this, operation);
PushOperation (operation);
return operation.Result;
}
void PushOperationNoExec (Operation operation)
{
if (current_operation != null)
current_operation.PushOperation (operation);
else
current_operation = operation;
}
void PushOperation (Operation operation)
{
PushOperationNoExec (operation);
ExecuteOperation (operation);
}
void ExecuteOperation (Operation operation)
{
try {
check_inferior ();
InterruptibleOperation iop = operation as InterruptibleOperation;
if ((iop != null) && iop.IsSuspended) {
iop.IsSuspended = false;
do_continue ();
return;
} else {
operation.Execute ();
}
} catch (Exception ex) {
Report.Debug (DebugFlags.SSE, "{0} caught exception while " +
"processing operation {1}: {2}", this, operation, ex);
operation.Result.Result = ex;
OperationCompleted (null);
}
}
public override TargetEventArgs LastTargetEvent {
get { return last_target_event; }
}
public override Method Lookup (TargetAddress address)
{
process.UpdateSymbolTable (inferior);
Method method = process.SymbolTableManager.Lookup (address);
Report.Debug (DebugFlags.JitSymtab, "{0} lookup {1}: {2}",
this, address, method);
return method;
}
public override Symbol SimpleLookup (TargetAddress address, bool exact_match)
{
return process.SymbolTableManager.SimpleLookup (address, exact_match);
}
#region public properties
internal Inferior Inferior {
get { return inferior; }
}
internal override Architecture Architecture {
get { return inferior.Architecture; }
}
public Thread Thread {
get { return thread; }
}
public override int PID {
get { return pid; }
}
public override long TID {
get { return tid; }
}
public override bool IsAlive {
get { return !dead && !killed && (inferior != null); }
}
public override TargetAddress LMFAddress {
get { return lmf_address; }
}
public override bool CanRun {
get { return true; }
}
public override bool CanStep {
get { return true; }
}
public override bool IsStopped {
get { return engine_stopped; }
}
public override WaitHandle WaitHandle {
get { return operation_completed_event; }
}
internal override ProcessServant ProcessServant {
get { return process; }
}
internal override ThreadManager ThreadManager {
get { return manager; }
}
public override Backtrace CurrentBacktrace {
get { return current_backtrace; }
}
public override StackFrame CurrentFrame {
get { return current_frame; }
}
public override Method CurrentMethod {
get { return current_method; }
}
public override TargetAddress CurrentFrameAddress {
get { return inferior.CurrentFrame; }
}
protected MonoDebuggerInfo MonoDebuggerInfo {
get { return process.MonoManager.MonoDebuggerInfo; }
}
public override TargetState State {
get {
if (inferior == null)
return TargetState.NoTarget;
else if (HasThreadLock)
return TargetState.Busy;
else
return inferior.State;
}
}
#endregion
internal bool HasThreadLock {
get { return thread_lock != null; }
}
protected TargetAddress EndStackAddress {
get { return end_stack_address; }
}
public override TargetMemoryInfo TargetMemoryInfo {
get {
check_inferior ();
return inferior.TargetMemoryInfo;
}
}
public override TargetMemoryArea[] GetMemoryMaps ()
{
check_inferior ();
return inferior.GetMemoryMaps ();
}
public override void Kill ()
{
killed = true;
SendCommand (delegate {
Inferior.ChildEvent stop_event;
Report.Debug (DebugFlags.SSE, "{0} kill: {1}", this, engine_stopped);
if (!engine_stopped) {
bool stopped = inferior.Stop (out stop_event);
Report.Debug (DebugFlags.SSE, "{0} kill #1: {1} {2} {3}",
this, engine_stopped, stopped, stop_event);
}
inferior.Kill ();
Report.Debug (DebugFlags.SSE, "{0} kill #2", this);
return null;
});
}
public void SetKilledFlag ()
{
killed = true;
}
internal override object DoTargetAccess (TargetAccessHandler func)
{
return SendCommand (delegate {
return func (inferior);
});
}
public override void Detach ()
{
SendCommand (delegate {
AcquireThreadLock ();
process.AcquireGlobalThreadLock (this);
process.BreakpointManager.RemoveAllBreakpoints (inferior);
if (process.MonoManager != null)
process.MonoManager.Detach (inferior);
DoDetach ();
process.DropGlobalThreadLock ();
return null;
});
}
protected void DoDetach ()
{
foreach (ThreadServant servant in process.ThreadServants)
servant.DetachThread ();
}
internal override void DetachThread ()
{
if (inferior != null) {
inferior.Detach ();
inferior.Dispose ();
inferior = null;
}
TargetEventArgs result = new TargetEventArgs (TargetEventType.TargetExited, 0);
if (current_operation != null)
OperationCompleted (result);
else
process.Debugger.SendTargetEvent (this, result);
process.OnThreadExitedEvent (this);
Dispose ();
}
public override void Stop ()
{
lock (this) {
Report.Debug (DebugFlags.EventLoop, "{0} interrupt: {1} {2}",
this, engine_stopped, current_operation);
if (engine_stopped)
return;
bool stopped = inferior.Stop ();
if (!Inferior.HasThreadEvents && !stopped)
OperationInterrupted ();