forked from adobe-flash/avmplus
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDebugCLI.cpp
1098 lines (988 loc) · 34.2 KB
/
DebugCLI.cpp
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
/* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
/* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "avmshell.h"
#ifdef DEBUGGER
namespace avmshell
{
/**
* The array of top level commands that we support.
* They are placed into a Nx2 array, whereby the first component
* is a String which is the command and the 2nd component is the
* integer identifier for the command.
*
* NOTE: order matters! For the case of a single character
* match, we let the first hit act like an unambiguous match.
*/
DebugCLI::StringInt DebugCLI::commandArray[] =
{
{ "break", CMD_BREAK },
{ "bt", INFO_STACK_CMD },
{ "continue", CMD_CONTINUE },
{ "delete", CMD_DELETE },
{ "finish", CMD_FINISH },
{ "help", CMD_HELP },
{ "?", CMD_HELP },
{ "info", CMD_INFO },
{ "list", CMD_LIST },
{ "next", CMD_NEXT },
{ "print", CMD_PRINT },
{ "quit", CMD_QUIT },
{ "step", CMD_STEP },
{ "set", CMD_SET },
{ "where", INFO_STACK_CMD },
{ NULL, 0 }
};
/**
* Info sub-commands
*/
DebugCLI::StringInt DebugCLI::infoCommandArray[] =
{
{ "arguments", INFO_ARGS_CMD },
{ "breakpoints", INFO_BREAK_CMD },
{ "files", INFO_FILES_CMD },
{ "functions", INFO_FUNCTIONS_CMD },
{ "locals", INFO_LOCALS_CMD },
{ "stack", INFO_STACK_CMD },
{ NULL, 0 }
};
DebugCLI::DebugCLI(avmplus::AvmCore *core, avmplus::Debugger::TraceLevel tracelevel)
: Debugger(core, tracelevel)
{
currentSourceLen = 0;
warnMissingSource = true;
}
DebugCLI::~DebugCLI()
{
if (currentSource) {
delete [] currentSource;
currentSource = NULL;
}
}
char* DebugCLI::nextToken()
{
char *out = currentToken;
if (currentToken) {
while (*currentToken) {
if (*currentToken == ' ' || *currentToken == '\r' || *currentToken == '\n' || *currentToken == '\t') {
*currentToken++ = 0;
break;
}
currentToken++;
}
currentToken = *currentToken ? currentToken : NULL;
}
return out;
}
/**
* Attempt to match given the given string against our set of commands
* @return the command code that was hit.
*/
int DebugCLI::determineCommand(DebugCLI::StringInt cmdList[],
const char *input,
int defCmd)
{
if (!input) return INFO_UNKNOWN_CMD;
int inputLen = (int)VMPI_strlen(input);
// first check for a comment
if (input[0] == '#') {
return CMD_COMMENT;
}
int match = -1;
bool ambiguous = false;
for (int i=0; cmdList[i].text; i++) {
if (!VMPI_strncmp(input, cmdList[i].text, inputLen)) {
if (match != -1) {
ambiguous = true;
break;
}
match = i;
}
}
/**
* 3 cases:
* - No hits, return unknown and let our caller
* dump the error.
* - We match unambiguously or we have 1 or more matches
* and the input is a single character. We then take the
* first hit as our command.
* - If we have multiple hits then we dump a 'ambiguous' message
* and puke quietly.
*/
if (match == -1) {
// no command match return unknown
return defCmd;
}
// only 1 match or our input is 1 character or first match is exact
else if (!ambiguous || inputLen == 1 || !VMPI_strcmp(cmdList[match].text, input)) {
return cmdList[match].id;
}
else {
// matches more than one command dump message and go
core->console << "Ambiguous command '" << input << "': ";
bool first = true;
for (int i=0; cmdList[i].text; i++) {
if (!VMPI_strncmp(cmdList[i].text, input, inputLen)) {
if (!first) {
core->console << ", ";
} else {
first = false;
}
core->console << cmdList[i].text;
}
}
core->console << ".\n";
return -1;
}
}
const char* DebugCLI::commandNumberToCommandName(DebugCLI::StringInt cmdList[],
int cmdNumber)
{
for (int i = 0; cmdList[i].text; i++)
{
if (cmdList[i].id == cmdNumber)
return cmdList[i].text;
}
return "?";
}
bool DebugCLI::printFrame(int k)
{
avmplus::Atom* ptr;
int count, line = -1;
avmplus::SourceInfo* src = NULL;
avmplus::AvmCore* core = avmplus::AvmCore::getActiveCore();
avmplus::DebugFrame* frame = core->debugger()->frameAt(k);
if (frame == NULL) return false;
// source information
frame->sourceLocation(src, line);
core->console << "#" << k << " ";
// this
avmplus::Atom a = avmplus::nullObjectAtom;
frame->dhis(a);
core->console << avmplus::asAtom(a) << ".";
// method
avmplus::MethodInfo* info = functionFor(src, line);
if (info) {
core->console << info->getMethodName();
} else {
avmplus::Stringp name = NULL;
if (frame->methodName(name))
core->console << name;
else
core->console << "???";
}
core->console << "(";
// dump args
frame->arguments(ptr, count);
for (int i=0; i<count; i++)
{
// write out the name
if (info)
{
avmplus::Stringp nm = info->getArgName(i);
if (nm != core->kundefined)
core->console << nm << "=";
}
core->console << avmplus::asAtom(*ptr++);
if (i<count-1)
core->console << ",";
}
core->console << ") at ";
if (src)
core->console << src->name() << ":" << (line) << "\n";
else
core->console << "???\n";
return true;
}
void DebugCLI::bt()
{
avmplus::AvmCore* core = avmplus::AvmCore::getActiveCore();
//core->stackTrace->dump(core->console);
//core->console << '\n';
// obtain information about each frame
int frameCount = core->debugger()->frameCount();
for(int k=0; k<frameCount; k++)
{
printFrame(k);
}
}
void DebugCLI::help()
{
StringInt* cmd;
// "help info" shows sub-commands of the "info" command
char* next = nextToken();
if (next && commandFor(next) == CMD_INFO)
cmd = infoCommandArray;
else
cmd = commandArray;
while (cmd->text)
{
core->console << cmd->text;
if (cmd->id == CMD_INFO)
core->console << " (see 'help " << cmd->text << "' for sub-commands)";
core->console << '\n';
cmd++;
}
}
avmplus::MethodInfo* DebugCLI::functionFor(avmplus::SourceInfo* src, int line)
{
avmplus::MethodInfo* info = NULL;
if (src)
{
// find the function at this location
int size = src->functionCount();
for(int i=0; i<size; i++)
{
avmplus::MethodInfo* m = src->functionAt(i);
if (line >= m->firstSourceLine() && line <= m->lastSourceLine())
{
info = m;
break;
}
}
}
return info;
}
// zero based
char* DebugCLI::lineStart(int linenum)
{
if (!currentSource && currentFile)
setCurrentSource(currentFile);
if (!currentSource) {
return NULL;
}
// linenumbers map to zero based array entry
char *ptr = currentSource;
for (int i=0; i<linenum; i++) {
// Scan to end of line
while (*ptr != '\n') {
if (!*ptr) {
return NULL;
}
ptr++;
}
// Advance past newline
ptr++;
}
return ptr;
}
void DebugCLI::displayLines(int line, int count)
{
if (!lineStart(0)) {
if (currentFile)
core->console << currentFile;
else
core->console << "<unknown>";
core->console <<":"<<line<<" ";
} else {
int lineAt = line;
while(count-- > 0)
{
char* ptr = lineStart(lineAt-1);
if (!ptr)
{
#if WRAP_AROUND
lineAt = 1;
count++; // reset line number to beginning skip this iteration
#else
break;
#endif
}
else
{
core->console << (lineAt) << ": ";
while (*ptr && *ptr != '\n')
core->console << *ptr++;
core->console << '\n';
lineAt++;
}
}
}
}
void DebugCLI::list(const char* line)
{
int currentLine = (core->callStack) ? core->callStack->linenum() : 0;
int linenum = (line) ? VMPI_atoi(line) : currentLine;
displayLines(linenum, 10);
}
void DebugCLI::printIP()
{
int line = (core->callStack) ? core->callStack->linenum() : 0;
displayLines(line, 1);
}
void DebugCLI::breakpoint(char *location)
{
if (!location)
{
core->console << "Bad format, should be: 'break [filename:]line'\n";
return;
}
avmplus::Stringp filename = currentFile;
char *colon = VMPI_strchr(location, ':');
if (colon) {
*colon = 0;
filename = core->internStringLatin1(location);
location = colon+1;
}
if (abcCount() == 0) {
core->console << "No abc file loaded\n";
return;
}
avmplus::SourceFile* sourceFile = NULL;
for (int i = 0, n = abcCount(); i < n; ++i)
{
avmplus::AbcFile* abcFile = (avmplus::AbcFile*)abcAt(i);
sourceFile = abcFile->sourceNamed(filename);
if (sourceFile)
break;
}
if (sourceFile == NULL) {
core->console << "No source available; can't set breakpoint.\n";
return;
}
int targetLine = VMPI_atoi(location);
if (breakpointSet(sourceFile, targetLine)) {
int breakpointId = ++breakpointCount;
core->console << "Breakpoint " << breakpointId << ": file "
<< filename
<< ", " << (targetLine) << ".\n";
BreakAction *breakAction = new (core->GetGC()) BreakAction(sourceFile,
breakpointId,
filename,
targetLine);
breakAction->prev = lastBreakAction;
if (lastBreakAction) {
lastBreakAction->next = breakAction;
} else {
firstBreakAction = breakAction;
}
lastBreakAction = breakAction;
} else {
core->console << "Could not locate specified line.\n";
}
}
void DebugCLI::showBreakpoints()
{
BreakAction *breakAction = firstBreakAction;
while (breakAction) {
breakAction->print(core->console);
breakAction = breakAction->next;
}
}
void DebugCLI::deleteBreakpoint(char *idstr)
{
int id = VMPI_atoi(idstr);
BreakAction *breakAction = firstBreakAction;
while (breakAction) {
if (breakAction->id == id) {
break;
}
breakAction = breakAction->next;
}
if (breakAction) {
if (breakAction->prev) {
breakAction->prev->next = breakAction->next;
} else {
firstBreakAction = breakAction->next;
}
if (breakAction->next) {
breakAction->next->prev = breakAction->prev;
} else {
lastBreakAction = breakAction->prev;
}
if (breakpointClear(breakAction->sourceFile, breakAction->linenum)) {
core->console << "Breakpoint " << id << " deleted.\n";
} else {
core->console << "Internal error; could not delete breakpoint.\n";
}
} else {
core->console << "Could not find breakpoint.\n";
}
}
void DebugCLI::locals(int frameNumber)
{
avmplus::Atom* ptr;
int count, line;
avmplus::SourceInfo* src = NULL;
avmplus::AvmCore* core = avmplus::AvmCore::getActiveCore();
avmplus::DebugFrame* frame = core->debugger()->frameAt(frameNumber);
if (frame == NULL) return;
// source information
frame->sourceLocation(src, line);
// method
avmplus::MethodInfo* info = functionFor(src, line);
if (info) {
frame->locals(ptr, count);
for(int i=0; i<count; i++) {
// write out the name
avmplus::Stringp nm = info->getLocalName(i);
core->console << i << ": ";
if (nm != core->kundefined)
core->console << nm;
else
core->console << "<local_" << i << ">";
core->console << " = " << avmplus::asAtom(*ptr++) << "\n";
}
}
}
void DebugCLI::arguments(int frameNumber)
{
int count;
avmplus::Atom* arr;
avmplus::AvmCore* core = avmplus::AvmCore::getActiveCore();
avmplus::DebugFrame* frame = core->debugger()->frameAt(frameNumber);
if (frame && frame->arguments(arr, count)) {
for (int i = 0; i < count; i++) {
avmplus::Stringp nm = NULL;
frame->argumentName(i, nm);
core->console << i << ": " << nm << " = " << avmplus::asAtom(*arr++) << "\n";
}
}
}
/**
* Given a pointer, trims leading and trailing whitespace. Note, this will
* modify the buffer -- it trims trailing whitespace by writing a null
* character.
*
* Returns the first offset past leading whitespace.
*/
static char* trim(char* ptr)
{
while (*ptr && (*ptr==' ' || *ptr=='\t'))
++ptr;
char* start = ptr;
while (*ptr)
++ptr;
while (ptr>start && (ptr[-1]==' ' || ptr[-1]=='\t'))
--ptr;
*ptr = '\0';
return start;
}
void DebugCLI::set()
{
char* line = currentToken;
char* expr = line;
char* equ = VMPI_strchr(currentToken, '=');
char* value = NULL;
if (equ)
{
*equ = '\0';
value = equ+1;
expr = trim(expr);
value = trim(value);
}
if (!expr || !*expr || !value || !*value)
{
core->console << " Bad format, should be: 'set {variable} = {value}' \n";
}
else
{
bool sawTrailingDot = false;
IValue* lvalue = evaluate(expr, &sawTrailingDot);
IValue* rvalue = evaluate(value, &sawTrailingDot);
AvmAssert(lvalue != NULL);
AvmAssert(rvalue != NULL);
if (!lvalue->isLValue())
core->console << " Can't assign to " << expr << '\n';
else
lvalue->set(rvalue->get());
}
}
class ScopeBuilder : public avmplus::CallStackNode::IScopeChainEnumerator
{
public:
ScopeBuilder(MMgc::GC* gc) : scopeChain(gc, avmplus::kListInitialCapacity) { }
/*virtual*/ void addScope(avmplus::Atom scope)
{
// null/undefined are not legal entries for scope, but
// enumerateScopeChainAtoms() can hand them to us, for entries
// within the max_scope range that aren't in use. just ignore 'em.
if (avmplus::AvmCore::isNullOrUndefined(scope))
return;
scopeChain.add(scope);
}
avmplus::AtomList scopeChain;
};
void DebugCLI::throwUndefinedVarError(const char* name)
{
avmplus::Stringp errorMessage = core->newStringLatin1("Error: Variable ");
errorMessage = errorMessage->appendLatin1(name);
errorMessage = errorMessage->appendLatin1(" is not defined.");
core->throwAtom(errorMessage->atom());
}
IValue* DebugCLI::getValue(const char *name)
{
avmplus::DebugStackFrame* frame = (avmplus::DebugStackFrame*)frameAt(0);
avmplus::Atom thisAtom;
frame->dhis(thisAtom);
avmplus::Stringp namestr = core->internStringLatin1(name, -1);
if (VMPI_strcmp(name, "this") == 0)
return new (core->gc) ConstantValue(thisAtom);
if (VMPI_strcmp(name, "NaN") == 0)
return new (core->gc) ConstantValue(core->kNaN);
if (namestr == core->kfalse)
return new (core->gc) ConstantValue(falseAtom);
if (namestr == core->ktrue)
return new (core->gc) ConstantValue(trueAtom);
if (namestr == core->knull)
return new (core->gc) ConstantValue(nullObjectAtom);
if (namestr == core->kundefined)
return new (core->gc) ConstantValue(undefinedAtom);
if (name[0] == '-' || VMPI_isdigit(name[0]))
return new (core->gc) ConstantValue(core->numberAtom(namestr->atom()));
if (name[0] == '\'' || name[0] == '"') // String literal
{
int32_t length = namestr->length();
if (length >= 2 && namestr->charAt(length-1) == namestr->charAt(0))
{
// Note, this doesn't do any escaping
return new (core->gc) ConstantValue(namestr->substr(1, length-2)->atom());
}
}
if (name[0] == '<') // XML or XMLList literal
{
if (avmplus::AvmCore::isObject(thisAtom))
{
avmplus::Toplevel* toplevel = avmplus::AvmCore::atomToScriptObject(thisAtom)->toplevel();
if (name[1] == '>')
return new (core->gc) ConstantValue(toplevel->xmlListClass()->ToXMLList(namestr->atom()));
else
return new (core->gc) ConstantValue(toplevel->xmlClass()->ToXML(namestr->atom()));
}
}
avmplus::Atom* ptr;
int count, line;
avmplus::SourceInfo* src;
// See if the name matches a function argument or local
frame->sourceLocation(src, line);
if (src)
{
avmplus::MethodInfo* info = functionFor(src, line);
if (info)
{
// see if the name matches a function argument
frame->arguments(ptr, count);
for(int i=0; i<count; i++)
{
avmplus::Stringp arg = info->getArgName(i);
if (arg->equalsLatin1(name))
{
// match!
return new (core->gc) ArgumentValue(frame, i);
}
}
// see if the name matches a local
frame->locals(ptr, count);
for(int i=0; i<count; i++)
{
avmplus::Stringp local = info->getLocalName(i);
if ( local->equalsLatin1(name))
{
// match!
return new (core->gc) LocalValue(frame, i);
}
}
}
}
// See if the name matches a property on the scope chain (which includes
// the 'this' object)
if (frame->trace)
{
avmplus::MethodEnv* env = frame->trace->env();
if (env)
{
ScopeBuilder scopeBuilder(core->GetGC());
frame->trace->enumerateScopeChainAtoms(scopeBuilder);
for (uint32_t i=0, n=scopeBuilder.scopeChain.length(); i<n; ++i)
{
StPropertyFinder finder(core, scopeBuilder.scopeChain.get(i), name);
finder.iterate();
if (finder.value)
return finder.value;
}
}
}
// Look for globals like 'Number'
avmplus::MethodEnv* env = frame->trace->env();
if (env)
{
avmplus::Multiname mn(core->getAnyPublicNamespace(),
core->internStringLatin1(name));
avmplus::ScriptEnv* script = core->domainMgr()->findScriptEnvInDomainEnvByMultiname(env->domainEnv(), mn);
if (script != (avmplus::ScriptEnv*)avmplus::BIND_NONE && script != (avmplus::ScriptEnv*)avmplus::BIND_AMBIGUOUS)
{
avmplus::ScriptObject* global = script->global;
if (global)
{
return new (core->gc) PropertyValue(global, mn);
}
}
}
throwUndefinedVarError(name);
return NULL; // unreachable
}
/**
* Accepts expressions of these forms:
* foo
* foo.bar
* foo.bar.baz
*
* Also allows literals: boolean, string, numeric, XML/XMLList,
* null, undefined.
*
* If the expression ends with a dot, e.g. "foo.bar.", then
* *outSawTrailingDot is set to true. This is used by the
* "print" command to allow "print foo." to print all members
* of variable "foo".
*/
IValue* DebugCLI::evaluate(char *expr, bool* outSawTrailingDot)
{
const char *name;
IValue* value = NULL;
bool firstPart = true;
*outSawTrailingDot = false;
while (expr)
{
name = expr;
char *dot = VMPI_strchr(expr, '.');
if (dot)
{
*dot = '\0';
name = expr;
expr = dot+1;
}
else
{
name = expr;
expr = NULL;
}
if (firstPart)
{
value = getValue(name);
AvmAssert(value != NULL);
}
else
{
if (*name)
{
// "foo.bar" -- find property "bar" of object "foo"
avmplus::Atom parent = value->get();
StPropertyFinder finder(core, parent, name);
finder.iterate();
value = finder.value;
if (!value)
{
if (avmplus::AvmCore::isObject(parent))
{
// Since we didn't find an existing property, we'll just construct
// one. If the parent object is dynamic, then calling get() on the
// PropertyVaulue we create will return undefined, and calling set()
// on it will define a new property. If the parent object is not
// dynamic, then get() and set() will throw exceptions. Either way,
// that's the correct behavior.
avmplus::Multiname mn(core->getAnyPublicNamespace(),
core->internStringLatin1(name));
value = new (core->gc) PropertyValue(avmplus::AvmCore::atomToScriptObject(parent), mn);
}
else
{
if (dot) *dot = '.'; // restore for our caller
throwUndefinedVarError(name);
return NULL; // unreachable
}
}
}
else
{
// "foo."
*outSawTrailingDot = true;
}
}
if (dot) *dot = '.'; // restore for our caller
firstPart = false;
}
return value;
}
/**
* Accepts expressions of these forms:
* foo
* foo.bar
* foo.bar.baz
* Also, allows a trailing dot, e.g. "foo.bar.", in which case all properties
* of foo.bar will be displayed.
*/
void DebugCLI::print(char *expr)
{
bool sawTrailingDot = false;
IValue* value = evaluate(expr, &sawTrailingDot);
AvmAssert(value != NULL);
if (sawTrailingDot)
{
// "foo." -- print all of object foo's properties
PropertyPrinter printer(core, this, value->get());
printer.iterate();
}
else
{
core->console << formatValue(value->get()) << '\n';
}
}
bool DebugCLI::filterException(avmplus::Exception *exception, bool /*willBeCaught*/)
{
// Filter exceptions when -d switch specified
if (activeFlag) {
core->console << "Exception has been thrown:\n"
<< core->string(exception->atom)
<< '\n';
enterDebugger();
return true;
}
return false;
}
void DebugCLI::info()
{
char *command = nextToken();
int cmd = infoCommandFor(command);
switch (cmd) {
case -1:
// ambiguous, we already printed error message
break;
case INFO_ARGS_CMD:
arguments(0);
break;
case INFO_LOCALS_CMD:
locals(0);
break;
case INFO_BREAK_CMD:
showBreakpoints();
break;
case INFO_UNKNOWN_CMD:
core->console << "Unknown command.\n";
break;
default:
core->console << "Command not implemented.\n";
break;
}
}
bool DebugCLI::debuggerInterruptOnEnter = false;
void DebugCLI::enterDebugger()
{
setCurrentSource( (core->callStack) ? (core->callStack->filename()) : 0 );
if (debuggerInterruptOnEnter) {
VMPI_debugBreak();
return;
}
for (;;) {
printIP();
core->console << "(asdb) ";
if (Platform::GetInstance()->getUserInput(commandLine, kMaxCommandLine) == NULL)
Platform::GetInstance()->exit(0);
commandLine[VMPI_strlen(commandLine)-1] = 0;
if (!commandLine[0]) {
VMPI_strcpy(commandLine, lastCommand);
} else {
VMPI_strcpy(lastCommand, commandLine);
}
currentToken = commandLine;
char *command = nextToken();
TRY(core, avmplus::kCatchAction_Ignore)
{
int cmd = commandFor(command);
switch (cmd) {
case -1:
// ambiguous, we already printed error message
break;
case CMD_COMMENT:
break;
case CMD_INFO:
info();
break;
case CMD_BREAK:
breakpoint(nextToken());
break;
case CMD_DELETE:
deleteBreakpoint(nextToken());
break;
case CMD_LIST:
list(nextToken());
break;
case CMD_UNKNOWN:
core->console << "Unknown command.\n";
break;
case CMD_QUIT:
Platform::GetInstance()->exit(0);
break;
case CMD_CONTINUE:
return;
case CMD_PRINT:
print(nextToken());
break;
case CMD_NEXT:
stepOver();
return;
case INFO_STACK_CMD:
bt();
break;
case CMD_FINISH:
stepOut();
return;
case CMD_HELP:
help();
break;
case CMD_STEP:
stepInto();
return;
case CMD_SET:
set();
break;
default:
core->console << "Command not implemented.\n";
break;
}
}
CATCH(avmplus::Exception *exception)
{
core->console << core->string(exception->atom) << '\n';
}
END_CATCH
END_TRY
}
}
void DebugCLI::setCurrentSource(avmplus::Stringp file)
{
if (!file)
return;
currentFile = file;
if (currentSource) {
delete [] currentSource;
currentSource = NULL;
currentSourceLen = 0;
}
// Open this file and suck it into memory
avmplus::StUTF8String currentFileUTF8(currentFile);
FileInputStream f(currentFileUTF8.c_str());
if (f.valid() && ((uint64_t)file->length() < UINT32_T_MAX)) { //cannot handle files > 4GB
currentSourceLen = (uint32_t) f.available();
currentSource = new char[currentSourceLen+1];
f.read(currentSource, currentSourceLen);
currentSource[currentSourceLen] = 0;
// whip through converting \r\n to space \n
for(int64_t i=0; i<currentSourceLen-1;i++) {
if (currentSource[i] == '\r' && currentSource[i+1] == '\n')
currentSource[i] = ' ';
}
} else if (warnMissingSource) {
core->console << "Could not find '" << currentFile << "'. Try running in the same directory as the .as file.\n";
warnMissingSource = false;
}
}
avmplus::Stringp DebugCLI::formatValue(avmplus::Atom value)
{
// An experiment: Printing XML and XMLList variables with their full toXMLString() form.
// if (core->isXMLorXMLList(value))
// {
// ScriptObject* object = AvmCore::atomToScriptObject(value);
// Multiname mn(core->getAnyPublicNamespace(), core->internStringLatin1("toXMLString"));
// Atom thisAtom = undefinedAtom;
// return core->string(object->toplevel()->callproperty(value, &mn, 1, &thisAtom, object->vtable));
// }
avmplus::StringBuffer sb(core);
sb << avmplus::asAtom(value);
return sb.toString();
}
//
// BreakAction
//
void BreakAction::print(avmplus::PrintWriter& out)
{
out << id << " at "