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
/
Command.cs
3744 lines (3149 loc) · 100 KB
/
Command.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.Xml;
using System.Text;
using System.IO;
using System.Threading;
using System.Reflection;
using System.Collections;
using System.Globalization;
using System.Text.RegularExpressions;
using Mono.Debugger;
using Mono.Debugger.Languages;
namespace Mono.Debugger.Frontend
{
public class DebuggerEngine : Engine
{
public DebuggerEngine (Interpreter interpreter)
: base (interpreter)
{
RegisterCommand ("pwd", typeof (PwdCommand));
RegisterCommand ("cd", typeof (CdCommand));
RegisterCommand ("print", typeof (PrintExpressionCommand));
RegisterAlias ("p", typeof (PrintExpressionCommand));
RegisterCommand ("ptype", typeof (PrintTypeCommand));
RegisterCommand ("call", typeof (CallCommand));
RegisterCommand ("examine", typeof (ExamineCommand));
RegisterAlias ("x", typeof (ExamineCommand));
RegisterCommand ("file", typeof (FileCommand));
RegisterCommand ("frame", typeof (SelectFrameCommand));
RegisterAlias ("f", typeof (SelectFrameCommand));
RegisterCommand ("disassemble", typeof (DisassembleCommand));
RegisterAlias ("dis", typeof (DisassembleCommand));
RegisterCommand ("thread", typeof (SelectThreadCommand));
RegisterCommand ("background", typeof (BackgroundThreadCommand));
RegisterAlias ("bg", typeof (BackgroundThreadCommand));
RegisterCommand ("stop", typeof (StopThreadCommand));
RegisterCommand ("continue", typeof (ContinueCommand));
RegisterAlias ("cont", typeof (ContinueCommand));
RegisterAlias ("c", typeof (ContinueCommand));
RegisterCommand ("step", typeof (StepCommand));
RegisterAlias ("s", typeof (StepCommand));
RegisterCommand ("next", typeof (NextCommand));
RegisterAlias ("n", typeof (NextCommand));
RegisterCommand ("stepi", typeof (StepInstructionCommand));
RegisterAlias ("i", typeof (StepInstructionCommand));
RegisterCommand ("nexti", typeof (NextInstructionCommand));
RegisterAlias ("t", typeof (NextInstructionCommand));
RegisterCommand ("finish", typeof (FinishCommand));
RegisterAlias ("fin", typeof (FinishCommand));
RegisterCommand ("backtrace", typeof (BacktraceCommand));
RegisterAlias ("bt", typeof (BacktraceCommand));
RegisterAlias ("where", typeof (BacktraceCommand));
RegisterCommand ("up", typeof (UpCommand));
RegisterCommand ("down", typeof (DownCommand));
RegisterCommand ("kill", typeof (KillCommand));
RegisterAlias ("k", typeof (KillCommand));
RegisterCommand ("detach", typeof (DetachCommand));
RegisterCommand ("set", typeof (SetCommand));
RegisterCommand ("show", typeof (ShowCommand));
RegisterCommand ("info", typeof (ShowCommand)); /* for gdb users */
RegisterCommand ("threadgroup", typeof (ThreadGroupCommand));
RegisterCommand ("enable", typeof (BreakpointEnableCommand));
RegisterCommand ("disable", typeof (BreakpointDisableCommand));
RegisterCommand ("delete", typeof (BreakpointDeleteCommand));
RegisterCommand ("list", typeof (ListCommand));
RegisterAlias ("l", typeof (ListCommand));
RegisterCommand ("break", typeof (BreakCommand));
RegisterAlias ("b", typeof (BreakCommand));
RegisterCommand ("display", typeof (DisplayCommand));
RegisterAlias ("d", typeof (DisplayCommand));
RegisterCommand ("undisplay", typeof (UndisplayCommand));
RegisterCommand ("catch", typeof (CatchCommand));
RegisterCommand ("watch", typeof (WatchCommand));
RegisterCommand ("quit", typeof (QuitCommand));
RegisterAlias ("q", typeof (QuitCommand));
RegisterCommand ("dump", typeof (DumpCommand));
RegisterCommand ("help", typeof (HelpCommand));
RegisterCommand ("library", typeof (LibraryCommand));
RegisterCommand ("run", typeof (RunCommand));
RegisterAlias ("r", typeof (RunCommand));
RegisterCommand ("start", typeof (StartCommand));
RegisterCommand ("attach", typeof (AttachCommand));
RegisterCommand ("core", typeof (OpenCoreFileCommand));
RegisterCommand ("about", typeof (AboutCommand));
RegisterCommand ("lookup", typeof (LookupCommand));
RegisterCommand ("return", typeof (ReturnCommand));
RegisterCommand ("save", typeof (SaveCommand));
RegisterCommand ("load", typeof (LoadCommand));
RegisterCommand ("module", typeof (ModuleCommand));
RegisterCommand ("config", typeof (ConfigCommand));
}
}
public enum CommandFamily {
Running,
Breakpoints,
Catchpoints,
Threads,
Stack,
Files,
Data,
Internal,
Obscure,
Support
}
public interface IDocumentableCommand {
/* the class of this command (breakpoint, running, threads, etc) */
CommandFamily Family {get; }
/* a short blurb */
string Description {get; }
/* a long blurb */
string Documentation {get; }
}
public abstract class Command {
public ArrayList Args;
public string Argument {
get {
if (Args != null){
string [] s = (string []) Args.ToArray (typeof (string));
return String.Join (" ", s);
} else
return "";
}
}
public abstract object Execute (Engine e);
public virtual void Repeat (Engine e)
{
Execute (e);
}
/* override this to provide command specific completion */
public virtual void Complete (Engine e, string text, int start, int end)
{
if (text.StartsWith ("-")) {
/* do our super cool argument completion on the command's
* properties, if there are any.
*/
e.Completer.ArgumentCompleter (GetType(), text, start, end);
}
else {
/* otherwise punt */
e.Completer.NoopCompleter (text, start, end);
}
}
}
public class DisplayCommand : DebuggerCommand
{
protected override bool DoResolve (ScriptingContext context)
{
if (Argument == "")
throw new ScriptingException ("Argument expected");
Expression expression = context.Interpreter.ExpressionParser.Parse (Argument);
if (expression == null)
throw new ScriptingException ("Cannot parse expression `{0}'.", Argument);
return true;
}
protected override object DoExecute (ScriptingContext context)
{
Display display = context.Interpreter.Session.CreateDisplay (Argument);
context.Print ("Added display {0}.", display.Index);
return display;
}
// IDocumentableCommand
public CommandFamily Family { get { return CommandFamily.Data; } }
public string Description { get { return "Display the result of an expression"; } }
public string Documentation { get { return ""; } }
}
public class UndisplayCommand : DebuggerCommand
{
protected override bool DoResolve (ScriptingContext context)
{
return true;
}
protected override object DoExecute (ScriptingContext context)
{
string argument = Argument;
if (argument == null || argument == "")
throw new ScriptingException ("Display number expected");
int index;
try {
index = (int) UInt32.Parse (argument);
} catch {
throw new ScriptingException ("Cannot parse display number {0}", argument);
}
Display d = context.Interpreter.Session.GetDisplay (index);
if (d == null)
throw new ScriptingException ("Display {0} not found", argument);
context.Interpreter.Session.DeleteDisplay (d);
return null;
}
// IDocumentableCommand
public CommandFamily Family { get { return CommandFamily.Data; } }
public string Description { get { return "Delete a display"; } }
public string Documentation { get { return ""; } }
}
public abstract class DebuggerCommand : Command
{
protected bool Repeating;
protected bool Error;
private ScriptingContext context;
public override object Execute (Engine e)
{
DebuggerEngine engine = (DebuggerEngine) e;
context = new ScriptingContext (engine.Interpreter);
if (!Resolve (context)) {
Error = true;
return null;
}
return Execute (context);
}
public override void Repeat (Engine e)
{
Repeating = true;
Execute (e);
}
protected Expression ParseExpression (ScriptingContext context)
{
if (Argument == "")
throw new ScriptingException ("Argument expected");
return DoParseExpression (context, Argument);
}
protected Expression DoParseExpression (ScriptingContext context, string arg)
{
Expression expr = context.Interpreter.ExpressionParser.Parse (arg);
if (expr == null)
throw new ScriptingException ("Cannot parse arguments");
return expr;
}
protected abstract object DoExecute (ScriptingContext context);
protected virtual bool DoResolveBase (ScriptingContext context)
{
return true;
}
protected virtual bool DoResolve (ScriptingContext context)
{
if (Argument != "")
throw new ScriptingException ("This command doesn't take any arguments");
return true;
}
public bool Resolve (ScriptingContext context)
{
if (!DoResolveBase (context))
return false;
return DoResolve (context);
}
public object Execute (ScriptingContext context)
{
return DoExecute (context);
}
}
public abstract class ProcessCommand : DebuggerCommand
{
int index = -1;
Process process;
public int Process {
get { return index; }
set { index = value; }
}
protected override bool DoResolveBase (ScriptingContext context)
{
if (!base.DoResolveBase (context))
return false;
context.CurrentProcess = process = context.Interpreter.GetProcess (index);
return true;
}
public Process CurrentProcess {
get { return process; }
}
}
public abstract class ThreadCommand : ProcessCommand
{
int index = -1;
Thread thread;
public int Thread {
get { return index; }
set { index = value; }
}
protected override bool DoResolveBase (ScriptingContext context)
{
if (!base.DoResolveBase (context))
return false;
context.CurrentThread = thread = context.Interpreter.GetThread (index);
return true;
}
public Thread CurrentThread {
get { return thread; }
}
}
public abstract class FrameCommand : ThreadCommand
{
int index = -1;
StackFrame frame;
Backtrace backtrace;
public int Frame {
get { return index; }
set { index = value; }
}
protected override bool DoResolveBase (ScriptingContext context)
{
if (!base.DoResolveBase (context))
return false;
if (!CurrentThread.IsStopped)
throw new TargetException (TargetError.NotStopped);
backtrace = CurrentThread.GetBacktrace ();
if (index == -1)
frame = backtrace.CurrentFrame;
else {
if (index >= backtrace.Count)
throw new ScriptingException ("No such frame: {0}", index);
frame = backtrace [index];
}
context.CurrentFrame = frame;
return true;
}
public StackFrame CurrentFrame {
get { return frame; }
}
public Backtrace CurrentBacktrace {
get { return backtrace; }
}
}
public abstract class PrintCommand : FrameCommand
{
Expression expression;
DisplayFormat format = DisplayFormat.Object;
protected override bool DoResolve (ScriptingContext context)
{
if (Argument.StartsWith ("/")) {
int pos = Argument.IndexOfAny (new char[] { ' ', '\t' });
if (pos < 0)
throw new ScriptingException ("Syntax error.");
string fstring = Argument.Substring (1, pos-1);
string arg = Argument.Substring (pos + 1);
switch (fstring) {
case "o":
case "object":
format = DisplayFormat.Object;
break;
case "a":
case "address":
format = DisplayFormat.Address;
break;
case "x":
case "hex":
format = DisplayFormat.HexaDecimal;
break;
case "default":
format = DisplayFormat.Default;
break;
default:
throw new ScriptingException (
"Unknown format: `{0}'", fstring);
}
expression = DoParseExpression (context, arg);
} else
expression = ParseExpression (context);
if (expression == null)
return false;
if (this is PrintTypeCommand) {
Expression resolved = expression.TryResolveType (context);
if (resolved != null) {
expression = resolved;
return true;
}
}
expression = expression.Resolve (context);
return expression != null;
}
protected override object DoExecute (ScriptingContext context)
{
return Execute (context, expression, format);
}
protected abstract string Execute (ScriptingContext context,
Expression expression, DisplayFormat format);
}
public class PrintExpressionCommand : PrintCommand, IDocumentableCommand
{
protected override string Execute (ScriptingContext context,
Expression expression, DisplayFormat format)
{
if (expression is TypeExpression)
throw new ScriptingException (
"`{0}' is a type, not a variable.", expression.Name);
object retval = expression.Evaluate (context);
string text = context.FormatObject (retval, format);
context.Print (text);
return text;
}
// IDocumentableCommand
public CommandFamily Family { get { return CommandFamily.Data; } }
public string Description { get { return "Print the result of an expression"; } }
public string Documentation { get { return ""; } }
}
public class PrintTypeCommand : PrintCommand, IDocumentableCommand
{
protected override string Execute (ScriptingContext context,
Expression expression, DisplayFormat format)
{
TargetType type = expression.EvaluateType (context);
string text = context.FormatType (type);
context.Print (text);
return text;
}
// IDocumentableCommand
public CommandFamily Family { get { return CommandFamily.Data; } }
public string Description { get { return "Print the type of an expression."; } }
public string Documentation { get { return ""; } }
}
public class CallCommand : FrameCommand, IDocumentableCommand
{
InvocationExpression invocation;
protected override bool DoResolve (ScriptingContext context)
{
Expression expr = ParseExpression (context);
if (expr == null)
return false;
expr = expr.Resolve (context);
if (expr == null)
return false;
invocation = expr as InvocationExpression;
if (invocation == null)
throw new ScriptingException (
"Expression `{0}' is not a method.", expr.Name);
return true;
}
protected override object DoExecute (ScriptingContext context)
{
invocation.Invoke (context, true);
return null;
}
// IDocumentableCommand
public CommandFamily Family { get { return CommandFamily.Running; } }
public string Description { get { return "Invoke a function in the program being debugged."; } }
public string Documentation { get { return ""; } }
}
public class ExamineCommand : FrameCommand, IDocumentableCommand
{
TargetAddress start;
Expression expression;
char format = '_';
char size = '_';
int count = 1;
static Regex formatRegex = new Regex ("^/([0-9]*)([oxdutfc]?)([bhwga]?)$");
int BaseRepresentation () {
switch (format) {
case 'o':
return 8;
case 'x':
return 16;
case 'd':
case 'u':
return 10;
case 't':
return 2;
case 'f':
case 'c':
return -1;
default:
throw new ScriptingException ("Unknown format " + format);
}
}
char DefaultSize () {
switch (format) {
case 'o':
case 'x':
case 't':
case 'c':
return 'b';
case 'd':
case 'u':
return 'w';
case 'f':
return 'g';
default:
throw new ScriptingException ("Unknown format " + format);
}
}
char LeftPadding () {
switch (format) {
case 'o':
case 'x':
case 't':
return '0';
case 'c':
case 'd':
case 'u':
case 'f':
return ' ';
default:
throw new ScriptingException ("Unknown format " + format);
}
}
int SizeOfItem () {
switch (size) {
case 'b':
return 1;
case 'h':
return 2;
case 'w':
return 4;
case 'g':
return 8;
case 'a':
return IntPtr.Size;
default:
throw new ScriptingException ("Unknown size " + size);
}
}
int LengthOfByteItem () {
switch (format) {
case 'o':
return 3;
case 'x':
return 2;
case 'd':
return 4;
case 'u':
return 3;
case 't':
return 8;
case 'f':
return -1;
case 'c':
return 1;
default:
throw new ScriptingException ("Unknown format " + format);
}
}
int LengthOfHalfItem () {
switch (format) {
case 'o':
return 6;
case 'x':
return 4;
case 'd':
return 6;
case 'u':
return 5;
case 't':
return 16;
case 'f':
return -1;
case 'c':
return -1;
default:
throw new ScriptingException ("Unknown format " + format);
}
}
int LengthOfWordItem () {
switch (format) {
case 'o':
return 12;
case 'x':
return 8;
case 'd':
return 11;
case 'u':
return 10;
case 't':
return 32;
case 'f':
return 15;
case 'c':
return -1;
default:
throw new ScriptingException ("Unknown format " + format);
}
}
int LengthOfGiantItem () {
switch (format) {
case 'o':
return 24;
case 'x':
return 16;
case 'd':
return 21;
case 'u':
return 20;
case 't':
return 64;
case 'f':
return 15;
case 'c':
return -1;
default:
throw new ScriptingException ("Unknown format " + format);
}
}
int LengthOfAddressItem () {
if (IntPtr.Size == 4) {
return LengthOfWordItem ();
} else if (IntPtr.Size == 8) {
return LengthOfGiantItem ();
} else {
throw new ScriptingException ("Unsupported IntPtr.Size: " + IntPtr.Size);
}
}
int LengthOfItem () {
switch (size) {
case 'b':
return LengthOfByteItem ();
case 'h':
return LengthOfHalfItem ();
case 'w':
return LengthOfWordItem ();
case 'g':
return LengthOfGiantItem ();
case 'a':
return LengthOfAddressItem ();
default:
throw new ScriptingException ("Unknown size " + size);
}
}
int ItemsPerLine () {
int item_length = LengthOfItem ();
if (item_length > 0) {
int result = 60 / (item_length + 1);
for (int i = 16; i > 0; i >>= 1) {
if (result >= i) {
return i;
}
}
return 1;
} else {
throw new ScriptingException ("Unsupported format and size combination");
}
}
void PrintPaddedValue (StringBuilder output, int paddedLength, char padding, string valueString) {
if (paddedLength - valueString.Length > 0) {
output.Append (padding, paddedLength - valueString.Length);
}
output.Append (valueString);
}
long ReadIntegerValue (byte[] data, int startIndex) {
switch (size) {
case 'b':
return data [startIndex];
case 'h':
return new BinaryReader(new MemoryStream (data, startIndex, 2, false, false)).ReadInt16 ();
case 'w':
return new BinaryReader(new MemoryStream (data, startIndex, 4, false, false)).ReadInt32 ();
case 'g':
return new BinaryReader(new MemoryStream (data, startIndex, 8, false, false)).ReadInt64 ();
case 'a':
if (IntPtr.Size == 4) {
return new BinaryReader(new MemoryStream (data, startIndex, 4, false, false)).ReadUInt32 ();
} else if (IntPtr.Size == 8) {
return new BinaryReader(new MemoryStream (data, startIndex, 8, false, false)).ReadInt64 ();
} else {
throw new ScriptingException ("Unsupported IntPtr.Size " + IntPtr.Size);
}
default:
throw new ScriptingException ("Unknown size " + size);
}
}
double ReadDoubleValue (byte[] data, int startIndex) {
switch (size) {
case 'w':
return new BinaryReader(new MemoryStream (data, startIndex, 4, false, false)).ReadSingle ();
case 'g':
return new BinaryReader(new MemoryStream (data, startIndex, 8, false, false)).ReadDouble ();
default:
throw new ScriptingException ("Unknown size " + size);
}
}
string ConvertIntegerValue (long value) {
if (format == 'd') {
return System.Convert.ToString (value);
} else {
return System.Convert.ToString (value, BaseRepresentation ());
}
}
void PrintItem (StringBuilder output, byte[] data, int startIndex) {
switch (format) {
case 'o':
case 'x':
case 'd':
case 'u':
case 't':
PrintPaddedValue (output, LengthOfItem (), LeftPadding (), ConvertIntegerValue (ReadIntegerValue (data, startIndex)));
return;
case 'f':
output.AppendFormat ("{0}", ReadDoubleValue (data, startIndex));
return;
case 'c':
output.AppendFormat ("{0}", (char) data [startIndex]);
return;
default:
throw new ScriptingException ("Unknown format " + format);
}
}
public int Count {
get { return count; }
set { count = value; }
}
protected override bool DoResolve (ScriptingContext context)
{
if (Repeating)
return true;
if (Args == null)
throw new ScriptingException ("Argument expected");
if (Args.Count > 1) {
string formatArgument = (string) Args [0];
Match match = formatRegex.Match (formatArgument);
if (match.Success) {
string countString = match.Groups [1].Value;
string formatString = match.Groups [2].Value;
string sizeString = match.Groups [3].Value;
if (countString != null && countString != "") {
count = Int32.Parse (countString);
}
if (formatString != null && formatString != "") {
format = formatString [0];
}
if (sizeString != null && sizeString != "") {
size = sizeString [0];
}
//context.Print ("EXAMINE: count {0}, format{1}, size {2}", count, format, size);
Args.RemoveAt (0);
//context.Print ("EXAMINE: Argument now is '" + Argument + "'");
} else {
//context.Print ("EXAMINE: " + formatArgument + " did not match");
if (formatArgument.Length > 0 && formatArgument [0] == '/') {
throw new ScriptingException ("Invalid format " + formatArgument);
}
}
} else {
//context.Print ("EXAMINE: by default: count {0}, format{1}, size {2}", count, format, size);
}
if (format == '_') {
format = 'x';
}
if (size == '_') {
size = DefaultSize ();
}
//context.Print ("EXAMINE: finally: count {0}, format{1}, size {2}", count, format, size);
expression = ParseExpression (context);
if (expression == null)
return false;
expression = expression.Resolve (context);
return expression != null;
}
protected override object DoExecute (ScriptingContext context)
{
if (!Repeating) {
PointerExpression pexp = expression as PointerExpression;
if (pexp == null)
throw new ScriptingException (
"Expression `{0}' is not a pointer.",
expression.Name);
start = pexp.EvaluateAddress (context);
}
int itemSize = SizeOfItem ();
int itemsPerLine = ItemsPerLine ();
int itemsInCurrentLine = 0;
byte[] data = CurrentThread.ReadBuffer (start, count * itemSize);
StringBuilder output = new StringBuilder (120);
//context.Print ("EXAMINE: executing itemSize {0}, itemsPerLine {1}, data.Length {2}, count {3}",
// itemSize, itemsPerLine, data.Length, count);
for (int i = 0; i < count; i++) {
if (itemsInCurrentLine == 0) {
if (IntPtr.Size == 4) {
PrintPaddedValue (output, 8, '0', System.Convert.ToString (start.Address + (i * itemSize), 16));
} else if (IntPtr.Size == 8) {
PrintPaddedValue (output, 16, '0', System.Convert.ToString (start.Address + (i * itemSize), 16));
} else {
throw new ScriptingException ("Unsupported IntPtr.Size " + IntPtr.Size);
}
output.Append (": ");
}
PrintItem (output, data, i * itemSize);
itemsInCurrentLine ++;
if (itemsInCurrentLine < itemsPerLine) {
output.Append (' ');
} else {
context.Print (output);
output.Remove (0, output.Length);
itemsInCurrentLine = 0;
}
}
if (output.Length > 0) {
context.Print (output);
}
//context.Print (TargetBinaryReader.HexDump (start, data));
start += data.Length;
return data;
}
// IDocumentableCommand
public CommandFamily Family { get { return CommandFamily.Data; } }
public string Description { get { return "Examine memory."; } }
public string Documentation { get { return
"Option format: /[Items][Format][Size]\n" +
"Items is the number of items to print.\n" +
"Format specifiers are (similar to gdb): o(octal), x(hex), d(decimal),\n" +
"u(unsigned decimal), t(binary), f(float), c(ASCII char).\n" +
"Size specifiers are (again, similar to gdb): b(byte), h(halfword),\n" +
"w(word), g(giant, 8 bytes), a(address, 4 or 8 bytes).\n" ; } }
}
public class SelectFrameCommand : FrameCommand, IDocumentableCommand
{
int frameIndex = -1;
protected override bool DoResolve (ScriptingContext context)
{
if (Args == null || Args.Count != 1)
return true;
try {
frameIndex = (int) UInt32.Parse ((string) Args [0]);
} catch {
throw new ScriptingException (
"Argument must be a positive integer");
}
return true;
}
protected override object DoExecute (ScriptingContext context)
{
StackFrame frame;
if (frameIndex != -1) {
Backtrace backtrace = CurrentThread.GetBacktrace ();
try {
backtrace.CurrentFrameIndex = frameIndex;
} catch {
throw new ScriptingException ("Invalid frame.");
}
frame = backtrace.CurrentFrame;
} else
frame = CurrentFrame;
if (context.Interpreter.IsScript)
context.Print (frame);
else
context.Interpreter.Style.PrintFrame (context, frame);
return frame;
}
// IDocumentableCommand
public CommandFamily Family { get { return CommandFamily.Stack; } }
public string Description { get { return "Select and print a stack frame."; } }
public string Documentation { get { return ""; } }
}
public class DisassembleCommand : FrameCommand, IDocumentableCommand
{
bool do_method;
bool do_frame;
int count = -1;
TargetAddress address;
Method method;
Expression expression;
public bool Method {
get { return do_method; }
set { do_method = value; }
}
new public bool Frame {
get { return do_frame; }
set { do_frame = value; }
}
public int Count {
get { return count; }
set { count = value; }
}
protected override bool DoResolve (ScriptingContext context)
{
if (Repeating)
return true;
if (Argument != "") {
if (count < 0)
count = 1;
expression = ParseExpression (context);
if (expression == null)
return false;
expression = expression.Resolve (context);
return expression != null;
}
if (count < 0)
count = 10;
address = CurrentFrame.TargetAddress;
method = CurrentFrame.Method;
return true;
}
protected override object DoExecute (ScriptingContext context)