This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
/
Thread.cs
922 lines (778 loc) · 19.2 KB
/
Thread.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
using System;
using System.IO;
using System.Text;
using ST = System.Threading;
using System.Configuration;
using System.Globalization;
using System.Reflection;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Messaging;
using Mono.Debugger.Backend;
using Mono.Debugger.Languages;
namespace Mono.Debugger
{
[Serializable]
internal delegate object TargetAccessDelegate (Thread target, object user_data);
public class Thread : DebuggerMarshalByRefObject
{
[Flags]
public enum Flags {
None = 0x0000,
Daemon = 0x0001,
Immutable = 0x0002,
Background = 0x0004,
AutoRun = 0x0008
}
internal Thread (ThreadServant servant, int id)
{
this.id = id;
this.servant = servant;
}
int id;
Flags flags;
ThreadServant servant;
public ST.WaitHandle WaitHandle {
get { return servant.WaitHandle; }
}
protected internal Language NativeLanguage {
get {
check_servant ();
return servant.NativeLanguage;
}
}
internal TargetAddress LMFAddress {
get {
check_servant ();
return servant.LMFAddress;
}
}
public override string ToString ()
{
return Name;
}
// <summary>
// The single-stepping engine's target state. This will be
// TargetState.RUNNING while the engine is stepping.
// </summary>
public TargetState State {
get {
if (servant == null)
return TargetState.NoTarget;
else
return servant.State;
}
}
public TargetEventArgs GetLastTargetEvent ()
{
check_servant ();
lock (this) {
TargetEventArgs args = servant.LastTargetEvent;
if (args != null) {
flags &= ~Flags.Background;
return args;
}
}
return null;
}
public bool IsRunning {
get { return (servant != null) && !servant.IsStopped; }
}
public bool IsAlive {
get { return (servant != null) && servant.IsAlive; }
}
public int ID {
get { return id; }
}
public Flags ThreadFlags {
get { return flags; }
}
public string Name {
get {
if ((flags & Flags.Daemon) != 0)
return String.Format ("Daemon thread @{0}", id);
else
return String.Format ("Thread @{0}", id);
}
}
public int PID {
get {
check_servant ();
return servant.PID;
}
}
public long TID {
get {
check_servant ();
return servant.TID;
}
}
internal Architecture Architecture {
get {
check_servant ();
return servant.Architecture;
}
}
internal ProcessServant ProcessServant {
get {
check_servant ();
return servant.ProcessServant;
}
}
public Process Process {
get {
check_servant ();
return ProcessServant.Client;
}
}
internal ThreadManager ThreadManager {
get {
check_servant ();
return servant.ThreadManager;
}
}
internal ThreadServant ThreadServant {
get {
check_servant ();
return servant;
}
}
internal void SetThreadFlags (Flags flags)
{
this.flags = flags;
}
public ThreadGroup ThreadGroup {
get {
check_servant ();
return servant.ThreadGroup;
}
}
void check_servant ()
{
if (servant == null)
throw new TargetException (TargetError.NoTarget);
}
void check_alive ()
{
if ((servant == null) || !servant.IsAlive)
throw new TargetException (TargetError.NoTarget);
}
// <summary>
// The current stack frame. May only be used when the engine is stopped
// (State == TargetState.STOPPED). The single stepping engine
// automatically computes the current frame and current method each time
// a stepping operation is completed. This ensures that we do not
// unnecessarily compute this several times if more than one client
// accesses this property.
// </summary>
public StackFrame CurrentFrame {
get {
check_servant ();
return servant.CurrentFrame;
}
}
public TargetAddress CurrentFrameAddress {
get {
check_servant ();
return servant.CurrentFrameAddress;
}
}
// <summary>
// The current stack frame. May only be used when the engine is stopped
// (State == TargetState.STOPPED). The backtrace is generated on
// demand, when this function is called. However, the single stepping
// engine will compute this only once each time a stepping operation is
// completed. This means that if you call this function several times
// without doing any stepping operations in the meantime, you'll always
// get the same backtrace.
// </summary>
public Backtrace GetBacktrace (Backtrace.Mode mode, int max_frames)
{
check_servant ();
return servant.GetBacktrace (mode, max_frames);
}
public Backtrace GetBacktrace (int max_frames)
{
return GetBacktrace (Backtrace.Mode.Default, max_frames);
}
public Backtrace GetBacktrace ()
{
check_servant ();
Backtrace bt = servant.CurrentBacktrace;
if (bt != null)
return bt;
return GetBacktrace (Backtrace.Mode.Default, -1);
}
public Backtrace CurrentBacktrace {
get {
check_servant ();
return servant.CurrentBacktrace;
}
}
public Registers GetRegisters ()
{
check_servant ();
return servant.GetRegisters ();
}
public void SetRegisters (Registers registers)
{
check_alive ();
servant.SetRegisters (registers);
}
public TargetMemoryArea[] GetMemoryMaps ()
{
check_servant ();
return servant.GetMemoryMaps ();
}
public Method Lookup (TargetAddress address)
{
check_servant ();
return servant.Lookup (address);
}
public Symbol SimpleLookup (TargetAddress address, bool exact_match)
{
check_servant ();
return servant.SimpleLookup (address, exact_match);
}
// <summary>
// The current method May only be used when the engine is stopped
// (State == TargetState.STOPPED). The single stepping engine
// automatically computes the current frame and current method each time
// a stepping operation is completed. This ensures that we do not
// unnecessarily compute this several times if more than one client
// accesses this property.
// </summary>
public Method CurrentMethod {
get {
check_servant ();
return servant.CurrentMethod;
}
}
// <summary>
// Step one machine instruction, but don't step into trampolines.
// </summary>
public ThreadCommandResult StepInstruction ()
{
lock (this) {
check_alive ();
ThreadCommandResult result = new OperationCommandResult (this);
servant.StepInstruction (result);
return result;
}
}
// <summary>
// Step one machine instruction, always step into method calls.
// </summary>
public ThreadCommandResult StepNativeInstruction ()
{
lock (this) {
check_alive ();
ThreadCommandResult result = new OperationCommandResult (this);
servant.StepNativeInstruction (result);
return result;
}
}
// <summary>
// Step one machine instruction, but step over method calls.
// </summary>
public ThreadCommandResult NextInstruction ()
{
lock (this) {
check_alive ();
ThreadCommandResult result = new OperationCommandResult (this);
servant.NextInstruction (result);
return result;
}
}
// <summary>
// Step one source line.
// </summary>
public ThreadCommandResult StepLine ()
{
lock (this) {
check_alive ();
ThreadCommandResult result = new OperationCommandResult (this);
servant.StepLine (result);
return result;
}
}
// <summary>
// Step one source line, but step over method calls.
// </summary>
public ThreadCommandResult NextLine ()
{
lock (this) {
check_alive ();
ThreadCommandResult result = new OperationCommandResult (this);
servant.NextLine (result);
return result;
}
}
// <summary>
// Continue until leaving the current method.
// </summary>
public ThreadCommandResult Finish (bool native)
{
lock (this) {
check_alive ();
ThreadCommandResult result = new OperationCommandResult (this);
servant.Finish (native, result);
return result;
}
}
public ThreadCommandResult Continue ()
{
return Continue (TargetAddress.Null);
}
public ThreadCommandResult Continue (TargetAddress until)
{
lock (this) {
check_alive ();
ThreadCommandResult result = new OperationCommandResult (this);
servant.Continue (until, result);
return result;
}
}
public ThreadCommandResult Background ()
{
return Background (TargetAddress.Null);
}
public ThreadCommandResult Background (TargetAddress until)
{
lock (this) {
check_alive ();
flags |= Flags.Background;
ThreadCommandResult result = new OperationCommandResult (this);
servant.Background (until, result);
return result;
}
}
internal void Kill ()
{
if (servant != null)
servant.Kill ();
Dispose ();
}
internal void Detach ()
{
if (servant != null)
servant.Detach ();
Dispose ();
}
public void Stop ()
{
check_alive ();
servant.Stop ();
}
public void AutoStop ()
{
check_alive ();
servant.Stop ();
flags |= Flags.AutoRun;
}
public ThreadCommandResult GetWaitHandle ()
{
return new ThreadCommandResult (this);
}
// <summary>
// Insert a breakpoint at address @address.
//
// Returns a number which may be passed to RemoveBreakpoint() to remove
// the breakpoint.
// </summary>
internal void InsertBreakpoint (BreakpointHandle handle,
TargetAddress address, int domain)
{
check_alive ();
servant.InsertBreakpoint (handle, address, domain);
}
// <summary>
// Remove breakpoint @index. @index is the breakpoint number which has
// been returned by InsertBreakpoint().
// </summary>
internal void RemoveBreakpoint (BreakpointHandle handle)
{
check_disposed ();
if (servant != null)
servant.RemoveBreakpoint (handle);
}
// <summary>
// Add an event handler.
//
// Returns a number which may be passed to RemoveEventHandler() to remove
// the event handler.
// </summary>
public int AddEventHandler (Event handle)
{
check_alive ();
return servant.AddEventHandler (handle);
}
// <summary>
// Remove event handler @index. @index is the event handler number which has
// been returned by AddEventHandler().
// </summary>
public void RemoveEventHandler (int index)
{
check_disposed ();
if (servant != null)
servant.RemoveEventHandler (index);
}
public string PrintObject (Style style, TargetObject obj, DisplayFormat format)
{
check_alive ();
return servant.PrintObject (style, obj, format);
}
public string PrintType (Style style, TargetType type)
{
check_alive ();
return servant.PrintType (style, type);
}
//
// Disassembling.
//
public int GetInstructionSize (TargetAddress address)
{
check_alive ();
return servant.GetInstructionSize (address);
}
public AssemblerLine DisassembleInstruction (Method method, TargetAddress address)
{
check_alive ();
return servant.DisassembleInstruction (method, address);
}
public AssemblerMethod DisassembleMethod (Method method)
{
check_alive ();
return servant.DisassembleMethod (method);
}
[Obsolete]
public RuntimeInvokeResult RuntimeInvoke (TargetFunctionType function,
TargetStructObject object_argument,
TargetObject[] param_objects,
bool is_virtual, bool debug)
{
RuntimeInvokeFlags flags = RuntimeInvokeFlags.None;
if (is_virtual)
flags |= RuntimeInvokeFlags.VirtualMethod;
if (debug)
flags |= RuntimeInvokeFlags.BreakOnEntry;
return RuntimeInvoke (function, object_argument, param_objects, flags);
}
public RuntimeInvokeResult RuntimeInvoke (TargetFunctionType function,
TargetStructObject object_argument,
TargetObject[] param_objects,
RuntimeInvokeFlags flags)
{
lock (this) {
check_alive ();
RuntimeInvokeResult result = new RuntimeInvokeResult (this);
servant.RuntimeInvoke (
function, object_argument, param_objects,
flags, result);
return result;
}
}
public TargetAddress CallMethod (TargetAddress method, long arg1, long arg2)
{
CommandResult result;
lock (this) {
check_alive ();
result = servant.CallMethod (method, arg1, arg2);
}
result.Wait ();
if (result.Result == null)
throw new TargetException (TargetError.UnknownError);
return (TargetAddress) result.Result;
}
public TargetAddress CallMethod (TargetAddress method, TargetAddress arg1,
long arg2)
{
CommandResult result;
lock (this) {
check_alive ();
result = servant.CallMethod (method, arg1.Address, arg2);
}
result.Wait ();
if (result.Result == null)
throw new TargetException (TargetError.UnknownError);
return (TargetAddress) result.Result;
}
public TargetAddress CallMethod (TargetAddress method, TargetAddress arg1,
long arg2, long arg3, string string_arg)
{
CommandResult result;
lock (this) {
check_alive ();
result = servant.CallMethod (
method, arg1.Address, arg2, arg3, string_arg);
}
result.Wait ();
if (result.Result == null)
throw new TargetException (TargetError.UnknownError);
return (TargetAddress) result.Result;
}
public TargetAddress CallMethod (TargetAddress method, TargetAddress method_argument,
TargetObject object_argument)
{
CommandResult result;
lock (this) {
check_alive ();
result = servant.CallMethod (
method, method_argument, object_argument);
}
result.Wait ();
if (result.Result == null)
throw new TargetException (TargetError.UnknownError);
return (TargetAddress) result.Result;
}
public void Return (ReturnMode mode)
{
CommandResult result;
lock (this) {
check_alive ();
result = servant.Return (mode);
if (result == null)
return;
}
result.Wait ();
}
internal void AbortInvocation (long rti_id)
{
lock (this) {
check_alive ();
servant.AbortInvocation (rti_id);
}
}
public string PrintRegisters (StackFrame frame)
{
return Architecture.PrintRegisters (frame);
}
public bool HasTarget {
get { return servant != null; }
}
public bool CanRun {
get { return true; }
}
public bool CanStep {
get { return true; }
}
public bool IsStopped {
get { return (servant != null) && servant.IsStopped; }
}
public TargetMemoryInfo TargetMemoryInfo {
get {
check_servant ();
return servant.TargetMemoryInfo;
}
}
#region ITargetInfo implementation
public int TargetAddressSize {
get { return TargetMemoryInfo.TargetAddressSize; }
}
public int TargetIntegerSize {
get { return TargetMemoryInfo.TargetIntegerSize; }
}
public int TargetLongIntegerSize {
get { return TargetMemoryInfo.TargetLongIntegerSize; }
}
public bool IsBigEndian {
get { return TargetMemoryInfo.IsBigEndian; }
}
#endregion
#region TargetMemoryAccess implementation
void write_memory (TargetAddress address, byte[] buffer)
{
check_alive ();
servant.WriteBuffer (address, buffer);
}
public AddressDomain AddressDomain {
get {
return TargetMemoryInfo.AddressDomain;
}
}
public byte ReadByte (TargetAddress address)
{
check_alive ();
return servant.ReadByte (address);
}
public int ReadInteger (TargetAddress address)
{
check_alive ();
return servant.ReadInteger (address);
}
public long ReadLongInteger (TargetAddress address)
{
check_alive ();
return servant.ReadLongInteger (address);
}
public TargetAddress ReadAddress (TargetAddress address)
{
check_alive ();
return servant.ReadAddress (address);
}
public string ReadString (TargetAddress address)
{
check_alive ();
return servant.ReadString (address);
}
public TargetBlob ReadMemory (TargetAddress address, int size)
{
check_alive ();
byte[] buffer = servant.ReadBuffer (address, size);
return new TargetBlob (buffer, TargetMemoryInfo);
}
public byte[] ReadBuffer (TargetAddress address, int size)
{
check_alive ();
return servant.ReadBuffer (address, size);
}
public bool CanWrite {
get {
check_servant ();
return servant.CanWrite;
}
}
public void WriteBuffer (TargetAddress address, byte[] buffer)
{
write_memory (address, buffer);
}
public void WriteByte (TargetAddress address, byte value)
{
throw new InvalidOperationException ();
}
public void WriteInteger (TargetAddress address, int value)
{
throw new InvalidOperationException ();
}
public void WriteLongInteger (TargetAddress address, long value)
{
throw new InvalidOperationException ();
}
public void WriteAddress (TargetAddress address, TargetAddress value)
{
check_alive ();
TargetBinaryWriter writer = new TargetBinaryWriter (
TargetMemoryInfo.TargetAddressSize, TargetMemoryInfo);
writer.WriteAddress (value);
write_memory (address, writer.Contents);
}
#endregion
#region IDisposable implementation
private bool disposed = false;
protected void check_disposed ()
{
if (disposed)
throw new ObjectDisposedException ("Thread");
}
protected virtual void DoDispose ()
{
if (servant != null) {
servant.Dispose ();
servant = null;
}
}
protected virtual void Dispose (bool disposing)
{
// Check to see if Dispose has already been called.
if (disposed)
return;
disposed = true;
// If this is a call to Dispose, dispose all managed resources.
if (disposing) {
DoDispose ();
}
}
public void Dispose ()
{
Dispose (true);
// Take yourself off the Finalization queue
GC.SuppressFinalize (this);
}
#endregion
~Thread ()
{
Dispose (false);
}
}
public abstract class CommandResult : DebuggerMarshalByRefObject
{
public object Result;
public abstract ST.WaitHandle CompletedEvent {
get;
}
internal abstract void Completed ();
public abstract void Abort ();
public void Wait ()
{
CompletedEvent.WaitOne ();
if (Result is Exception)
throw (Exception) Result;
}
}
public class ThreadCommandResult : CommandResult
{
Thread thread;
internal ThreadCommandResult (Thread thread)
{
this.thread = thread;
}
public Thread Thread {
get { return thread; }
}
public override ST.WaitHandle CompletedEvent {
get { return thread.WaitHandle; }
}
internal override void Completed ()
{ }
public override void Abort ()
{
thread.Stop ();
}
}
public class OperationCommandResult : ThreadCommandResult
{
protected ST.ManualResetEvent completed_event = new ST.ManualResetEvent (false);
internal OperationCommandResult (Thread thread)
: base (thread)
{ }
public override ST.WaitHandle CompletedEvent {
get { return completed_event; }
}
internal override void Completed ()
{
completed_event.Set ();
}
}
public class RuntimeInvokeResult : OperationCommandResult
{
internal RuntimeInvokeResult (Thread thread)
: base (thread)
{ }
public override void Abort ()
{
Thread.AbortInvocation (ID);
completed_event.WaitOne ();
}
public long ID;
public bool InvocationCompleted;
public TargetObject ReturnObject;
public string ExceptionMessage;
}
[Flags]
public enum RuntimeInvokeFlags
{
None = 0,
NoSideEffects = 1,
NestedBreakStates = 2,
BreakOnEntry = 4,
VirtualMethod = 8,
SendEventOnCompletion = 16
}
public enum ReturnMode
{
Managed,
Native,
Invocation
}
}