This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Inferior.cs
1208 lines (1007 loc) · 30.3 KB
/
Inferior.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.Specialized;
using System.Runtime.InteropServices;
using Mono.Debugger;
using Mono.Debugger.Architecture;
namespace Mono.Debugger.Backends
{
internal delegate void ChildOutputHandler (string output);
internal abstract class Inferior : ITargetAccess, ITargetNotification, IDisposable
{
protected IntPtr server_handle;
protected Bfd bfd;
protected BfdDisassembler bfd_disassembler;
protected AddressDomain address_domain;
protected ThreadManager thread_manager;
protected readonly ProcessStart start;
protected readonly BfdContainer bfd_container;
protected readonly IArchitecture arch;
protected readonly SymbolTableCollection symtab_collection;
protected readonly DebuggerBackend backend;
protected readonly DebuggerErrorHandler error_handler;
protected readonly BreakpointManager breakpoint_manager;
protected readonly AddressDomain global_address_domain;
protected readonly bool native;
int child_pid, tid;
bool initialized;
ITargetInfo target_info;
public bool HasTarget {
get {
return initialized;
}
}
public int PID {
get {
check_disposed ();
return child_pid;
}
}
public int TID {
get {
check_disposed ();
return tid;
}
}
public event TargetExitedHandler TargetExited;
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_spawn (IntPtr handle, string working_directory, string[] argv, string[] envp, out int child_pid, ChildOutputHandler stdout_handler, ChildOutputHandler stderr_handler, out IntPtr error);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_attach (IntPtr handle, int child_pid, out int tid);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_get_pc (IntPtr handle, out long pc);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_current_insn_is_bpt (IntPtr handle, out int is_breakpoint);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_step (IntPtr handle);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_continue (IntPtr handle);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_detach (IntPtr handle);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_finalize (IntPtr handle);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_read_memory (IntPtr handle, long start, int size, IntPtr data);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_write_memory (IntPtr handle, IntPtr data, long start, int size);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_get_target_info (IntPtr handle, out int target_int_size, out int target_long_size, out int target_address_size);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_call_method (IntPtr handle, long method_address, long method_argument1, long method_argument2, long callback_argument);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_call_method_1 (IntPtr handle, long method_address, long method_argument, string string_argument, long callback_argument);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_call_method_invoke (IntPtr handle, long invoke_method, long method_address, long object_address, int num_params, IntPtr param_array, long callback_argument);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_insert_breakpoint (IntPtr handle, long address, out int breakpoint);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_insert_hw_breakpoint (IntPtr handle, int index, long address, out int breakpoint);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_remove_breakpoint (IntPtr handle, int breakpoint);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_enable_breakpoint (IntPtr handle, int breakpoint);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_disable_breakpoint (IntPtr handle, int breakpoint);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_get_registers (IntPtr handle, int count, IntPtr registers, IntPtr values);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_set_registers (IntPtr handle, int count, IntPtr registers, IntPtr values);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_get_backtrace (IntPtr handle, int max_frames, long stop_address, out int count, out IntPtr data);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_get_ret_address (IntPtr handle, out long retval);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_stop (IntPtr handle);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_stop_and_wait (IntPtr handle, out int status);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_set_signal (IntPtr handle, int signal, int send_it);
[DllImport("monodebuggerserver")]
static extern CommandError mono_debugger_server_kill (IntPtr handle);
[DllImport("monodebuggerserver")]
static extern IntPtr mono_debugger_server_initialize (IntPtr breakpoint_manager);
internal enum ChildEventType {
NONE = 0,
UNKNOWN_ERROR = 1,
CHILD_EXITED,
CHILD_STOPPED,
CHILD_SIGNALED,
CHILD_CALLBACK,
CHILD_HIT_BREAKPOINT,
CHILD_MEMORY_CHANGED,
CHILD_CREATED_THREAD
}
internal delegate void ChildEventHandler (ChildEventType message, int arg);
internal sealed class ChildEvent
{
public readonly ChildEventType Type;
public readonly long Argument;
public readonly long Data1;
public readonly long Data2;
public ChildEvent (ChildEventType type, long arg,
long data1, long data2)
{
this.Type = type;
this.Argument = arg;
this.Data1 = data1;
this.Data2 = data2;
}
public override string ToString ()
{
return String.Format ("ChildEvent ({0}:{1}:{2:x}:{3:x})",
Type, Argument, Data1, Data2);
}
}
protected enum CommandError {
None = 0,
Unknown,
NoInferior,
AlreadyHaveInferior,
Fork,
NotStopped,
AlreadyStopped,
RecursiveCall,
NoSuchBreakpoint,
UnknownRegister,
DrOccupied,
MemoryAccess
}
protected Inferior (DebuggerBackend backend, ProcessStart start,
BreakpointManager bpm,
DebuggerErrorHandler error_handler,
AddressDomain global_address_domain)
{
this.backend = backend;
this.start = start;
this.native = !(start is ManagedProcessStart);
this.bfd_container = backend.BfdContainer;
this.error_handler = error_handler;
this.breakpoint_manager = bpm;
this.global_address_domain = global_address_domain;
arch = new ArchitectureI386 ();
server_handle = mono_debugger_server_initialize (breakpoint_manager.Manager);
if (server_handle == IntPtr.Zero)
throw new InternalError ("mono_debugger_server_initialize() failed.");
}
public static Inferior CreateInferior (ThreadManager thread_manager,
ProcessStart start)
{
return new PTraceInferior (
thread_manager.DebuggerBackend, start,
thread_manager.BreakpointManager, null,
thread_manager.AddressDomain, null);
}
public abstract Inferior CreateThread ();
[DllImport("glib-2.0")]
extern static void g_free (IntPtr data);
protected void check_error (CommandError error)
{
if (error == CommandError.None)
return;
handle_error (error);
}
void handle_error (CommandError error)
{
switch (error) {
case CommandError.NotStopped:
throw new TargetNotStoppedException ();
case CommandError.NoInferior:
throw new NoTargetException ();
case CommandError.AlreadyHaveInferior:
throw new AlreadyHaveTargetException ();
case CommandError.Fork:
throw new CannotStartTargetException ();
case CommandError.NoSuchBreakpoint:
throw new NoSuchBreakpointException ();
case CommandError.UnknownRegister:
throw new NoSuchRegisterException ();
default:
throw new InternalError ("Got unknown error condition from inferior: {0}",
error);
}
}
public void CallMethod (TargetAddress method, long data1, long data2,
long callback_arg)
{
check_disposed ();
TargetState old_state = change_target_state (TargetState.BUSY);
try {
check_error (mono_debugger_server_call_method (
server_handle, method.Address, data1, data2,
callback_arg));
} catch {
change_target_state (old_state);
}
}
public void CallMethod (TargetAddress method, long arg1, string arg2,
long callback_arg)
{
check_disposed ();
TargetState old_state = change_target_state (TargetState.RUNNING);
try {
check_error (mono_debugger_server_call_method_1 (
server_handle, method.Address, arg1,
arg2, callback_arg));
} catch {
change_target_state (old_state);
}
}
public void RuntimeInvoke (TargetAddress invoke_method, TargetAddress method_argument,
TargetAddress object_argument, TargetAddress[] param_objects,
long callback_arg)
{
check_disposed ();
int size = param_objects.Length;
long[] param_addresses = new long [size];
for (int i = 0; i < param_objects.Length; i++)
param_addresses [i] = param_objects [i].Address;
IntPtr data = IntPtr.Zero;
try {
if (size > 0) {
data = Marshal.AllocHGlobal (size * 8);
Marshal.Copy (param_addresses, 0, data, size);
}
check_error (mono_debugger_server_call_method_invoke (
server_handle, invoke_method.Address, method_argument.Address,
object_argument.Address, size, data, callback_arg));
} finally {
if (data != IntPtr.Zero)
Marshal.FreeHGlobal (data);
}
}
public int InsertBreakpoint (TargetAddress address)
{
int retval;
check_error (mono_debugger_server_insert_breakpoint (
server_handle, address.Address, out retval));
return retval;
}
public int InsertHardwareBreakpoint (TargetAddress address, int index)
{
int retval;
check_error (mono_debugger_server_insert_hw_breakpoint (
server_handle, index, address.Address, out retval));
return retval;
}
public void RemoveBreakpoint (int breakpoint)
{
check_error (mono_debugger_server_remove_breakpoint (
server_handle, breakpoint));
}
public void EnableBreakpoint (int breakpoint)
{
check_error (mono_debugger_server_enable_breakpoint (
server_handle, breakpoint));
}
public void DisableBreakpoint (int breakpoint)
{
check_error (mono_debugger_server_disable_breakpoint (
server_handle, breakpoint));
}
public DebuggerBackend DebuggerBackend {
get {
return backend;
}
}
public ProcessStart ProcessStart {
get {
return start;
}
}
public BfdContainer BfdContainer {
get {
return bfd_container;
}
}
public void Run (bool redirect_fds)
{
if (initialized)
throw new AlreadyHaveTargetException ();
initialized = true;
IntPtr error;
CommandError result = mono_debugger_server_spawn (
server_handle, start.WorkingDirectory, start.CommandLineArguments,
start.Environment, out child_pid,
new ChildOutputHandler (inferior_stdout_handler),
new ChildOutputHandler (inferior_stderr_handler),
out error);
if (result != CommandError.None) {
string message = Marshal.PtrToStringAuto (error);
g_free (error);
throw new CannotStartTargetException (message);
}
SetupInferior ();
change_target_state (TargetState.STOPPED, 0);
}
public void Attach (int pid)
{
if (initialized)
throw new AlreadyHaveTargetException ();
initialized = true;
check_error (mono_debugger_server_attach (server_handle, pid, out tid));
this.child_pid = pid;
SetupInferior ();
change_target_state (TargetState.STOPPED, 0);
}
public abstract ChildEvent Wait ();
public abstract ChildEvent ProcessEvent (long status);
protected virtual void SetupInferior ()
{
address_domain = new AddressDomain (String.Format ("ptrace ({0})", child_pid));
int target_int_size, target_long_size, target_address_size;
check_error (mono_debugger_server_get_target_info
(server_handle, out target_int_size, out target_long_size,
out target_address_size));
target_info = new TargetInfo (target_int_size, target_long_size, target_address_size);
try {
bfd = bfd_container.AddFile (this, start.TargetApplication,
start.LoadNativeSymtab);
} catch (Exception e) {
error_handler (this, String.Format (
"Can't read symbol file {0}", start.TargetApplication), e);
return;
}
bfd_disassembler = bfd.GetDisassembler (this);
}
public void UpdateModules ()
{
bfd.UpdateSharedLibraryInfo (this);
}
public Bfd Bfd {
get { return bfd; }
}
public TargetAddress SimpleLookup (string name)
{
return bfd [name];
}
public TargetAddress MainMethodAddress {
get {
return bfd ["main"];
}
}
protected void child_exited ()
{
child_pid = 0;
Dispose ();
if (TargetExited != null)
TargetExited ();
}
void inferior_stdout_handler (string line)
{
if (TargetOutput != null)
TargetOutput (false, line);
}
void inferior_stderr_handler (string line)
{
if (TargetOutput != null)
TargetOutput (true, line);
}
void debugger_output (string line)
{
if (DebuggerOutput != null)
DebuggerOutput (line);
}
//
// ITargetInfo
//
public int TargetIntegerSize {
get {
return target_info.TargetIntegerSize;
}
}
public int TargetLongIntegerSize {
get {
return target_info.TargetLongIntegerSize;
}
}
public int TargetAddressSize {
get {
return target_info.TargetAddressSize;
}
}
//
// ITargetMemoryAccess
//
public AddressDomain AddressDomain {
get {
if (address_domain == null)
throw new NoTargetException ();
return address_domain;
}
}
public AddressDomain GlobalAddressDomain {
get {
return global_address_domain;
}
}
IntPtr read_buffer (TargetAddress address, int size)
{
IntPtr data = Marshal.AllocHGlobal (size);
CommandError result = mono_debugger_server_read_memory (
server_handle, address.Address, size, data);
if (result == CommandError.MemoryAccess) {
Marshal.FreeHGlobal (data);
throw new TargetMemoryException (address, size);
} else if (result != CommandError.None) {
Marshal.FreeHGlobal (data);
handle_error (result);
throw new Exception ("Internal error: this line will never be reached");
}
return data;
}
public byte[] ReadBuffer (TargetAddress address, int size)
{
check_disposed ();
if (size == 0)
return new byte [0];
IntPtr data = IntPtr.Zero;
try {
data = read_buffer (address, size);
byte[] retval = new byte [size];
Marshal.Copy (data, retval, 0, size);
return retval;
} finally {
Marshal.FreeHGlobal (data);
}
}
public byte ReadByte (TargetAddress address)
{
check_disposed ();
IntPtr data = IntPtr.Zero;
try {
data = read_buffer (address, 1);
return Marshal.ReadByte (data);
} finally {
Marshal.FreeHGlobal (data);
}
}
public int ReadInteger (TargetAddress address)
{
check_disposed ();
IntPtr data = IntPtr.Zero;
try {
data = read_buffer (address, 4);
return Marshal.ReadInt32 (data);
} finally {
Marshal.FreeHGlobal (data);
}
}
public long ReadLongInteger (TargetAddress address)
{
check_disposed ();
IntPtr data = IntPtr.Zero;
try {
data = read_buffer (address, 8);
return Marshal.ReadInt64 (data);
} finally {
Marshal.FreeHGlobal (data);
}
}
public TargetAddress ReadAddress (TargetAddress address)
{
check_disposed ();
switch (TargetAddressSize) {
case 4:
return new TargetAddress (AddressDomain, (uint) ReadInteger (address));
case 8:
return new TargetAddress (AddressDomain, ReadLongInteger (address));
default:
throw new TargetMemoryException (
"Unknown target address size " + TargetAddressSize);
}
}
public TargetAddress ReadGlobalAddress (TargetAddress address)
{
check_disposed ();
switch (TargetAddressSize) {
case 4:
return new TargetAddress (GlobalAddressDomain, (uint) ReadInteger (address));
case 8:
return new TargetAddress (GlobalAddressDomain, ReadLongInteger (address));
default:
throw new TargetMemoryException (
"Unknown target address size " + TargetAddressSize);
}
}
public string ReadString (TargetAddress address)
{
check_disposed ();
StringBuilder sb = new StringBuilder ();
while (true) {
byte b = ReadByte (address);
address++;
if (b == 0)
return sb.ToString ();
sb.Append ((char) b);
}
}
public ITargetMemoryReader ReadMemory (TargetAddress address, int size)
{
check_disposed ();
byte [] retval = ReadBuffer (address, size);
return new TargetReader (retval, this);
}
public ITargetMemoryReader ReadMemory (byte[] buffer)
{
check_disposed ();
return new TargetReader (buffer, this);
}
public bool CanWrite {
get {
return true;
}
}
public void WriteBuffer (TargetAddress address, byte[] buffer)
{
check_disposed ();
IntPtr data = IntPtr.Zero;
try {
int size = buffer.Length;
data = Marshal.AllocHGlobal (size);
Marshal.Copy (buffer, 0, data, size);
check_error (mono_debugger_server_write_memory (
server_handle, data, address.Address, size));
} finally {
if (data != IntPtr.Zero)
Marshal.FreeHGlobal (data);
OnMemoryChanged ();
}
}
public void WriteByte (TargetAddress address, byte value)
{
check_disposed ();
IntPtr data = IntPtr.Zero;
try {
data = Marshal.AllocHGlobal (1);
Marshal.WriteByte (data, value);
check_error (mono_debugger_server_write_memory (
server_handle, data, address.Address, 1));
} finally {
if (data != IntPtr.Zero)
Marshal.FreeHGlobal (data);
OnMemoryChanged ();
}
}
public void WriteInteger (TargetAddress address, int value)
{
check_disposed ();
IntPtr data = IntPtr.Zero;
try {
data = Marshal.AllocHGlobal (4);
Marshal.WriteInt32 (data, value);
check_error (mono_debugger_server_write_memory (
server_handle, data, address.Address, 4));
} finally {
if (data != IntPtr.Zero)
Marshal.FreeHGlobal (data);
OnMemoryChanged ();
}
}
public void WriteLongInteger (TargetAddress address, long value)
{
check_disposed ();
IntPtr data = IntPtr.Zero;
try {
data = Marshal.AllocHGlobal (8);
Marshal.WriteInt64 (data, value);
check_error (mono_debugger_server_write_memory (
server_handle, data, address.Address, 8));
} finally {
if (data != IntPtr.Zero)
Marshal.FreeHGlobal (data);
OnMemoryChanged ();
}
}
public void WriteAddress (TargetAddress address, TargetAddress value)
{
check_disposed ();
switch (TargetAddressSize) {
case 4:
WriteInteger (address, (int) value.Address);
break;
case 8:
WriteLongInteger (address, value.Address);
break;
default:
throw new TargetMemoryException (
"Unknown target address size " + TargetAddressSize);
}
}
//
// IInferior
//
public event TargetOutputHandler TargetOutput;
public event DebuggerOutputHandler DebuggerOutput;
public event DebuggerErrorHandler DebuggerError;
public event StateChangedHandler StateChanged;
TargetState target_state = TargetState.NO_TARGET;
public TargetState State {
get {
check_disposed ();
return target_state;
}
}
protected TargetState change_target_state (TargetState new_state)
{
return change_target_state (new_state, 0);
}
TargetState change_target_state (TargetState new_state, int arg)
{
if (new_state == target_state)
return target_state;
TargetState old_state = target_state;
target_state = new_state;
if (StateChanged != null)
StateChanged (target_state, arg);
return old_state;
}
public void Step ()
{
check_disposed ();
TargetState old_state = change_target_state (TargetState.RUNNING);
try {
check_error (mono_debugger_server_step (server_handle));
} catch {
change_target_state (old_state);
}
}
public void Continue ()
{
check_disposed ();
TargetState old_state = change_target_state (TargetState.RUNNING);
try {
check_error (mono_debugger_server_continue (server_handle));
} catch {
change_target_state (old_state);
}
}
// <summary>
// Stop the inferior.
// Returns true if it actually stopped the inferior and false if it was
// already stopped.
// Note that the target may have stopped abnormally in the meantime, in
// this case we return the corresponding ChildEvent.
// </summary>
public bool Stop (out ChildEvent new_event)
{
check_disposed ();
int status;
CommandError error = mono_debugger_server_stop_and_wait (server_handle, out status);
if (error != CommandError.None) {
new_event = null;
return false;
} else if (status == 0) {
new_event = null;
return true;
}
new_event = ProcessEvent (status);
return true;
}
// <summary>
// Just send the inferior a stop signal, but don't wait for it to stop.
// Returns true if it actually sent the signal and false if the target
// was already stopped.
// </summary>
public bool Stop ()
{
check_disposed ();
CommandError error = mono_debugger_server_stop (server_handle);
return error == CommandError.None;
}
public void SetSignal (int signal, bool send_it)
{
check_disposed ();
int do_send = send_it ? 1 : 0;
check_error (mono_debugger_server_set_signal (server_handle, signal, do_send));
}
public void Detach ()
{
check_disposed ();
check_error (mono_debugger_server_detach (server_handle));
}
public void Shutdown ()
{
mono_debugger_server_kill (server_handle);
}
public void Kill ()
{
if (!disposed)
mono_debugger_server_kill (server_handle);
}
public TargetAddress CurrentFrame {
get {
long pc;
check_disposed ();
CommandError result = mono_debugger_server_get_pc (server_handle, out pc);
if (result != CommandError.None)
throw new NoStackException ();
return new TargetAddress (GlobalAddressDomain, pc);
}
}
public bool CurrentInstructionIsBreakpoint {
get {
check_disposed ();
int is_breakpoint;
CommandError result = mono_debugger_server_current_insn_is_bpt (
server_handle, out is_breakpoint);
if (result != CommandError.None)
throw new NoStackException ();
return is_breakpoint != 0;
}
}
public IDisassembler Disassembler {
get {
check_disposed ();
return bfd_disassembler;
}
}
public IArchitecture Architecture {
get {
check_disposed ();
return arch;
}
}
public Module[] Modules {
get {
return new Module[] { bfd.Module };
}
}
public Register GetRegister (int register)
{
Register[] retval = GetRegisters (new int[] { register });
return retval [0];
}
public Register[] GetRegisters ()
{
return GetRegisters (arch.AllRegisterIndices);
}
public Register[] GetRegisters (int[] registers)
{
IntPtr data = IntPtr.Zero, buffer = IntPtr.Zero;
try {
int size = registers.Length * 4;
int buffer_size = registers.Length * 8;
data = Marshal.AllocHGlobal (size);
Marshal.Copy (registers, 0, data, registers.Length);
buffer = Marshal.AllocHGlobal (buffer_size);
CommandError result = mono_debugger_server_get_registers (
server_handle, registers.Length, data, buffer);
check_error (result);
long[] retval = new long [registers.Length];
Marshal.Copy (buffer, retval, 0, registers.Length);
Register[] regs = new Register [registers.Length];
for (int i = 0; i < registers.Length; i++)
regs [i] = new Register (registers [i], retval [i]);
return regs;
} finally {
if (data != IntPtr.Zero)
Marshal.FreeHGlobal (data);
if (buffer != IntPtr.Zero)
Marshal.FreeHGlobal (buffer);
}
}
public void SetRegister (int register, long value)
{
SetRegisters (new int[] { register }, new long[] { value });
}
public void SetRegisters (int[] registers, long[] values)
{
IntPtr data = IntPtr.Zero, buffer = IntPtr.Zero;
try {
int size = registers.Length * 4;
int buffer_size = registers.Length * 8;
data = Marshal.AllocHGlobal (size);
Marshal.Copy (registers, 0, data, registers.Length);
buffer = Marshal.AllocHGlobal (buffer_size);
Marshal.Copy (values, 0, buffer, registers.Length);
CommandError result = mono_debugger_server_set_registers (
server_handle, registers.Length, data, buffer);
check_error (result);
} finally {
if (data != IntPtr.Zero)
Marshal.FreeHGlobal (data);
if (buffer != IntPtr.Zero)
Marshal.FreeHGlobal (buffer);
}
}
internal struct ServerStackFrame
{
public long Address;
public long FrameAddress;
}
internal class StackFrame
{
Inferior inferior;
ServerStackFrame frame;
internal StackFrame (Inferior inferior, ServerStackFrame frame)
{
this.inferior = inferior;
this.frame = frame;
}
public TargetAddress Address {