-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlite.c
2261 lines (1981 loc) · 59.5 KB
/
dlite.c
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
/* dlite.c - DLite, the lite debugger, routines */
/* SimpleScalar(TM) Tool Suite
* Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.
* All Rights Reserved.
*
* THIS IS A LEGAL DOCUMENT, BY USING SIMPLESCALAR,
* YOU ARE AGREEING TO THESE TERMS AND CONDITIONS.
*
* No portion of this work may be used by any commercial entity, or for any
* commercial purpose, without the prior, written permission of SimpleScalar,
* LLC ([email protected]). Nonprofit and noncommercial use is permitted
* as described below.
*
* 1. SimpleScalar is provided AS IS, with no warranty of any kind, express
* or implied. The user of the program accepts full responsibility for the
* application of the program and the use of any results.
*
* 2. Nonprofit and noncommercial use is encouraged. SimpleScalar may be
* downloaded, compiled, executed, copied, and modified solely for nonprofit,
* educational, noncommercial research, and noncommercial scholarship
* purposes provided that this notice in its entirety accompanies all copies.
* Copies of the modified software can be delivered to persons who use it
* solely for nonprofit, educational, noncommercial research, and
* noncommercial scholarship purposes provided that this notice in its
* entirety accompanies all copies.
*
* 3. ALL COMMERCIAL USE, AND ALL USE BY FOR PROFIT ENTITIES, IS EXPRESSLY
* PROHIBITED WITHOUT A LICENSE FROM SIMPLESCALAR, LLC ([email protected]).
*
* 4. No nonprofit user may place any restrictions on the use of this software,
* including as modified by the user, by any other authorized user.
*
* 5. Noncommercial and nonprofit users may distribute copies of SimpleScalar
* in compiled or executable form as set forth in Section 2, provided that
* either: (A) it is accompanied by the corresponding machine-readable source
* code, or (B) it is accompanied by a written offer, with no time limit, to
* give anyone a machine-readable copy of the corresponding source code in
* return for reimbursement of the cost of distribution. This written offer
* must permit verbatim duplication by anyone, or (C) it is distributed by
* someone who received only the executable form, and is accompanied by a
* copy of the written offer of source code.
*
* 6. SimpleScalar was developed by Todd M. Austin, Ph.D. The tool suite is
* currently maintained by SimpleScalar LLC ([email protected]). US Mail:
* 2395 Timbercrest Court, Ann Arbor, MI 48105.
*
* Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "host.h"
#include "misc.h"
#include "machine.h"
#include "version.h"
#include "eval.h"
#include "regs.h"
#include "memory.h"
#include "sim.h"
#include "symbol.h"
#include "loader.h"
#include "options.h"
#include "stats.h"
#include "range.h"
#include "dlite.h"
/* architected state accessors, initialized by dlite_init() */
static dlite_reg_obj_t f_dlite_reg_obj = NULL;
static dlite_mem_obj_t f_dlite_mem_obj = NULL;
static dlite_mstate_obj_t f_dlite_mstate_obj = NULL;
/* set non-zero to enter DLite after next instruction */
int dlite_active = FALSE;
/* non-zero to force a check for a break */
int dlite_check = FALSE;
/* set non-zero to exit DLite command loop */
static int dlite_return = FALSE;
/* size modifier mask bit definitions */
#define MOD_BYTE 0x0001 /* b - print a byte */
#define MOD_HALF 0x0002 /* h - print a half (short) */
#define MOD_WORD 0x0004 /* w - print a word */
#define MOD_QWORD 0x0008 /* q - print a qword */
#define MOD_FLOAT 0x0010 /* F - print a float */
#define MOD_DOUBLE 0x0020 /* f - print a double */
#define MOD_CHAR 0x0040 /* c - print a character */
#define MOD_STRING 0x0080 /* s - print a string */
#define MOD_SIZES \
(MOD_BYTE|MOD_HALF|MOD_WORD|MOD_QWORD \
|MOD_FLOAT|MOD_DOUBLE|MOD_CHAR|MOD_STRING)
/* format modifier mask bit definitions */
#define MOD_DECIMAL 0x0100 /* d - print in decimal format */
#define MOD_UNSIGNED 0x0200 /* u - print in unsigned format */
#define MOD_OCTAL 0x0400 /* o - print in octal format */
#define MOD_HEX 0x0800 /* x - print in hex format */
#define MOD_BINARY 0x1000 /* 1 - print in binary format */
#define MOD_FORMATS \
(MOD_DECIMAL|MOD_UNSIGNED|MOD_OCTAL|MOD_HEX|MOD_BINARY)
/* DLite modifier parser, transforms /<mods> strings to modifier mask */
static char * /* error string, NULL for no err */
modifier_parser(char *p, /* ptr to /<mods> string */
char **endp, /* ptr to first byte not consumed */
int *pmod) /* modifier mask written to *PMOD */
{
int modifiers = 0;
/* default modifiers */
*pmod = 0;
/* is this a valid modifier? */
if (*p == '/')
{
p++;
/* parse modifiers until end-of-string or whitespace is found */
while (*p != '\0' && *p != '\n' && *p != ' ' && *p != '\t')
{
switch (*p)
{
case 'b':
modifiers |= MOD_BYTE;
break;
case 'h':
modifiers |= MOD_HALF;
break;
case 'w':
modifiers |= MOD_WORD;
break;
case 'q':
modifiers |= MOD_QWORD;
break;
case 'd':
modifiers |= MOD_DECIMAL;
break;
case 'u':
modifiers |= MOD_UNSIGNED;
break;
case 'o':
modifiers |= MOD_OCTAL;
break;
case 'x':
modifiers |= MOD_HEX;
break;
case '1':
modifiers |= MOD_BINARY;
break;
case 'F':
modifiers |= MOD_FLOAT;
break;
case 'f':
modifiers |= MOD_DOUBLE;
break;
case 'c':
modifiers |= MOD_CHAR;
break;
case 's':
modifiers |= MOD_STRING;
break;
default:
return "bad modifier (use one or more of /bhwqduox1fdcs)";
}
p++;
}
}
/* no error, return end of string and modifier mask */
*endp = p;
*pmod = modifiers;
return NULL;
}
/* DLite default expression evaluator */
static struct eval_state_t *dlite_evaluator = NULL;
static struct regs_t *local_regs = NULL;
/* DLite identifier evaluator, used by the expression evaluator, returns
the value of the ident in ES->TOK_BUF, sets eval_error to value other
than ERR_NOERR if an error is encountered */
static struct eval_value_t /* value of identifier */
ident_evaluator(struct eval_state_t *es) /* expression evaluator */
{
int i;
char *err_str;
struct eval_value_t val;
struct stat_stat_t *stat;
struct sym_sym_t *sym;
static struct eval_value_t err_value = { et_int, { 0 } };
/* is this a builtin register definition? */
for (i=0; md_reg_names[i].str != NULL; i++)
{
if (!mystricmp(es->tok_buf, md_reg_names[i].str))
{
err_str =
f_dlite_reg_obj(local_regs, /* !is_write */FALSE,
md_reg_names[i].file, md_reg_names[i].reg, &val);
if (err_str)
{
eval_error = ERR_UNDEFVAR;
val = err_value;
}
return val;
}
}
/* else, try to locate a program symbol */
sym_loadsyms(ld_prog_fname, /* load locals */TRUE);
sym = sym_bind_name(es->tok_buf, NULL, sdb_any);
if (sym)
{
/* found a symbol with this name, return it's (address) value */
val.type = et_addr;
val.value.as_addr = sym->addr;
return val;
}
/* else, try to locate a statistical value symbol */
stat = stat_find_stat(sim_sdb, es->tok_buf);
if (stat)
{
/* found it, convert stat value to an eval_value_t value */
switch (stat->sc)
{
case sc_int:
val.type = et_int;
val.value.as_int = *stat->variant.for_int.var;
break;
case sc_uint:
val.type = et_uint;
val.value.as_uint = *stat->variant.for_uint.var;
break;
#ifdef HOST_HAS_QWORD
case sc_qword:
val.type = et_qword;
val.value.as_qword = *stat->variant.for_qword.var;
break;
#endif /* HOST_HAS_QWORD */
case sc_float:
val.type = et_float;
val.value.as_float = *stat->variant.for_float.var;
break;
case sc_double:
val.type = et_double;
val.value.as_double = *stat->variant.for_double.var;
break;
case sc_dist:
case sc_sdist:
eval_error = ERR_BADEXPR;
val = err_value;
break;
case sc_formula:
{
/* instantiate a new evaluator to avoid recursion problems */
struct eval_state_t *es = eval_new(ident_evaluator, sim_sdb);
char *endp;
val = eval_expr(es, stat->variant.for_formula.formula, &endp);
if (eval_error != ERR_NOERR || *endp != '\0')
{
/* pass through eval_error */
val = err_value;
}
/* else, use value returned */
eval_delete(es);
}
break;
default:
panic("bogus stat class");
}
return val;
}
/* else, not found */
/* else, this is a bogus symbol */
eval_error = ERR_UNDEFVAR;
val = err_value;
return val;
}
/* maximum number of arguments that can passed to a dlite command handler */
#define MAX_ARGS 4
/* maximum length of a dlite command string argument */
#define MAX_STR 128
/* argument array entry, argument arrays are passed to command handlers */
union arg_val_t {
int as_modifier;
struct eval_value_t as_value;
int as_access;
char as_str[MAX_STR];
};
/* a DLite command handler function pointer */
typedef char * /* err str, NULL for no err */
(*cmd_fn_t)(int nargs, /* number of arguments */
union arg_val_t args[], /* argument vector */
struct regs_t *regs, /* registers to access */
struct mem_t *mem); /* memory to access */
/* DLite command descriptor, fully describes a command supported by the
DLite debugger command handler */
struct dlite_cmd_t {
char *cmd_str; /* DLite command string */
char *arg_strs[MAX_ARGS]; /* NULL-terminated cmd args (? - optional):
m - size/type modifiers
a - address expression
c - count expression
e - any expression
s - any string
t - access type {r|w|x}
i - breakpoint id */
cmd_fn_t cmd_fn; /* implementing function */
char *help_str; /* DLite command help string */
};
/* forward handler decls */
static char *
dlite_help(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_version(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_terminate(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_quit(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_cont(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_step(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_print(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_options(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_option(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_stats(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_stat(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_whatis(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_regs(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_iregs(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_fpregs(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_cregs(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_mstate(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_display(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_dump(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_dis(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_break(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_dbreak(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_rbreak(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_breaks(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_delete(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_clear(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_symbols(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_tsymbols(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_dsymbols(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
static char *
dlite_symbol(int nargs, union arg_val_t args[],
struct regs_t *regs, struct mem_t *mem);
/* DLite debugger command parser command definitions, NOTE: optional
arguments must be trailing arguments, otherwise the command parser will
break (modifiers are an exception to this rule) */
static struct dlite_cmd_t cmd_db[] =
{
{ "help", { "s?", NULL }, dlite_help,
"print command reference" },
{ "version", { NULL }, dlite_version,
"print DLite version information" },
{ "terminate", { NULL }, dlite_terminate,
"terminate the simulation with statistics" },
{ "quit", { NULL }, dlite_quit,
"exit the simulator" },
{ "cont", { "a?", NULL }, dlite_cont,
"continue program execution (optionally at <addr>)" },
{ "step", { NULL }, dlite_step,
"step program one instruction" },
#if 0 /* NYI */
{ "next", { NULL }, dlite_next,
"step program one instruction in current procedure" },
#endif
{ "print", { "m?", "e", NULL }, dlite_print,
"print the value of <expr> using format <modifiers>" },
{ "options", { NULL }, dlite_options,
"print the value of all options" },
{ "option", { "s", NULL }, dlite_option,
"print the value of an option" },
{ "stats", { NULL }, dlite_stats,
"print the value of all statistical variables" },
{ "stat", { "s", NULL }, dlite_stat,
"print the value of a statistical variable" },
{ "whatis", { "e", NULL }, dlite_whatis,
"print the type of expression <expr>" },
{ "---", { NULL }, NULL, NULL },
{ "regs", { NULL }, dlite_regs,
"print all register contents" },
{ "iregs", { NULL }, dlite_iregs,
"print integer register contents" },
{ "fpregs", { NULL }, dlite_fpregs,
"print floating point register contents" },
{ "cregs", { NULL }, dlite_cregs,
"print control register contents" },
{ "mstate", { "s?", NULL }, dlite_mstate,
"print machine specific state (simulator dependent)" },
{ "display", { "m?", "a", NULL }, dlite_display,
"display the value at memory location <addr> using format <modifiers>" },
{ "dump", { "a?", "c?", NULL }, dlite_dump,
"dump memory at <addr> (optionally for <cnt> words)" },
{ "dis", { "a?", "c?", NULL }, dlite_dis,
"disassemble instructions at <addr> (for <cnt> insts)" },
{ "break", { "a", NULL }, dlite_break,
"set breakpoint at <addr>, returns <id> of breakpoint" },
{ "dbreak", { "a", "t?", NULL }, dlite_dbreak,
"set data breakpoint at <addr> (for (r)ead, (w)rite,\n"
" and/or e(x)ecute, returns <id> of breakpoint" },
{ "rbreak", { "s", "t?", NULL }, dlite_rbreak,
"set read/write/exec breakpoint at <range> (for (r)ead, (w)rite,\n"
" and/or e(x)ecute, returns <id> of breakpoint" },
{ "breaks", { NULL }, dlite_breaks,
"list active code and data breakpoints" },
{ "delete", { "i", NULL }, dlite_delete,
"delete breakpoint <id>" },
{ "clear", { NULL }, dlite_clear,
"clear all breakpoints (code and data)" },
{ "---", { NULL }, NULL, NULL },
{ "symbols", { NULL }, dlite_symbols,
"print the value of all program symbols" },
{ "tsymbols", { NULL }, dlite_tsymbols,
"print the value of all program text symbols" },
{ "dsymbols", { NULL }, dlite_dsymbols,
"print the value of all program data symbols" },
{ "symbol", { "s", NULL }, dlite_symbol,
"print the value of a symbol" },
{ "---", { NULL }, NULL, NULL },
/* list terminator */
{ NULL, { NULL }, NULL, NULL }
};
/* help command trailing text */
static char *dlite_help_tail =
"Arguments <addr>, <cnt>, <expr>, and <id> are any legal expression:\n"
" <expr> <- <factor> +|- <expr>\n"
" <factor> <- <term> *|/ <factor>\n"
" <term> <- ( <expr> )\n"
" | - <term>\n"
" | <const>\n"
" | <symbol>\n"
" | <file:loc>\n"
"\n"
"Command modifiers <mods> are any of the following:\n"
"\n"
" b - print a byte\n"
" h - print a half (short)\n"
" w - print a word (default)\n"
#ifdef HOST_HAS_QWORD
" q - print a qword\n"
#endif /* HOST_HAS_QWORD */
" F - print a float\n"
" f - print a double\n"
" c - print a character\n"
" s - print a string\n"
" d - print in decimal format (default)\n"
" u - print in unsigned decimal format\n"
" o - print in octal format\n"
" x - print in hex format\n"
" 1 - print in binary format\n";
/* execute DLite command string CMD */
static char * /* err str, NULL if no err */
dlite_exec(char *cmd_str, /* command string */
struct regs_t *regs, /* registers to access */
struct mem_t *mem) /* memory to access */
{
int i, arg_cnt;
struct dlite_cmd_t *cmd;
char cmd_buf[512], *p, *q, *endp;
union arg_val_t args[MAX_ARGS];
p = cmd_str;
q = cmd_buf;
/* skip any whitespace before argument */
while (*p == ' ' || *p == '\t' || *p == '\n')
p++;
/* anything left? */
if (*p == '\0')
{
/* NOP, no error */
return NULL;
}
/* copy out command name string */
while (*p != '\0' && *p != '\n' && *p != ' ' && *p != '\t' && *p != '/')
*q++ = *p++;
*q = '\0';
/* find matching command */
for (cmd=cmd_db; cmd->cmd_str != NULL; cmd++)
{
if (!strcmp(cmd->cmd_str, cmd_buf))
break;
}
if (cmd->cmd_str == NULL)
return "unknown command";
/* match arguments for *CMD */
for (i=0, arg_cnt=0; i<MAX_ARGS && cmd->arg_strs[i] != NULL; i++, arg_cnt++)
{
int optional, access, modifiers;
char *arg, arg_type, *err_str;
struct eval_value_t val;
/* skip any whitespace before argument */
while (*p == ' ' || *p == '\t' || *p == '\n')
p++;
arg = cmd->arg_strs[i];
arg_type = arg[0];
optional = (arg[1] == '?');
if (*p == '\0')
{
if (optional)
{
/* all arguments parsed */
break;
}
else
return "missing an argument";
}
endp = p;
switch (arg_type)
{
case 'm':
err_str = modifier_parser(p, &endp, &modifiers);
if (err_str)
return err_str;
args[arg_cnt].as_modifier = modifiers;
break;
case 'a':
local_regs = regs;
val = eval_expr(dlite_evaluator, p, &endp);
if (eval_error)
return eval_err_str[eval_error];
args[arg_cnt].as_value = val;
break;
case 'c':
local_regs = regs;
val = eval_expr(dlite_evaluator, p, &endp);
if (eval_error)
return eval_err_str[eval_error];
args[arg_cnt].as_value = val;
break;
case 'e':
local_regs = regs;
val = eval_expr(dlite_evaluator, p, &endp);
if (eval_error)
return eval_err_str[eval_error];
args[arg_cnt].as_value = val;
break;
case 't':
access = 0;
while (*p != '\0' && *p != '\n' && *p != ' ' && *p != '\t')
{
switch (*p)
{
case 'r':
access |= ACCESS_READ;
break;
case 'w':
access |= ACCESS_WRITE;
break;
case 'x':
access |= ACCESS_EXEC;
break;
default:
return "bad access type specifier (use r|w|x)";
}
p++;
}
endp = p;
args[arg_cnt].as_access = access;
break;
case 'i':
local_regs = regs;
val = eval_expr(dlite_evaluator, p, &endp);
if (eval_error)
return eval_err_str[eval_error];
args[arg_cnt].as_value = val;
break;
case 's':
q = args[arg_cnt].as_str;
while (*p != ' ' && *p != '\t' && *p != '\0')
*q++ = *p++;
*q = '\0';
endp = p;
break;
default:
panic("bogus argument type: `%c'", arg_type);
}
p = endp;
}
/* skip any whitespace before any trailing argument */
while (*p == ' ' || *p == '\t' || *p == '\n')
p++;
/* check for any orphan arguments */
if (*p != '\0')
return "too many arguments";
/* if we reach here, all arguments were parsed correctly, call handler */
return cmd->cmd_fn(arg_cnt, args, regs, mem);
}
/* print expression value VAL using modifiers MODIFIERS */
static char * /* err str, NULL for no err */
print_val(int modifiers, /* print modifiers */
struct eval_value_t val) /* expr value to print */
{
char *format = "", *prefix = "", radix, buf[512];
/* fill in any default size */
if ((modifiers & MOD_SIZES) == 0)
{
/* compute default size */
switch (val.type)
{
case et_int: modifiers |= MOD_WORD; break;
case et_uint: modifiers |= MOD_WORD; break;
case et_addr:
#ifdef HOST_HAS_QWORD
if (sizeof(md_addr_t) > 4)
modifiers |= MOD_QWORD;
else
#endif /* HOST_HAS_QWORD */
modifiers |= MOD_WORD;
break;
#ifdef HOST_HAS_QWORD
case et_qword: modifiers |= MOD_QWORD; break;
case et_sqword: modifiers |= MOD_QWORD; break;
#endif /* HOST_HAS_QWORD */
case et_float: modifiers |= MOD_FLOAT; break;
case et_double: modifiers |= MOD_DOUBLE; break;
case et_symbol:
default: return "bad print value";
}
}
if (((modifiers & MOD_SIZES) & ((modifiers & MOD_SIZES) - 1)) != 0)
return "multiple size specifiers";
/* fill in any default format */
if ((modifiers & MOD_FORMATS) == 0)
{
/* compute default size */
switch (val.type)
{
case et_int: modifiers |= MOD_DECIMAL; break;
case et_uint: modifiers |= MOD_UNSIGNED; break;
case et_addr: modifiers |= MOD_HEX; break;
#ifdef HOST_HAS_QWORD
case et_qword: modifiers |= MOD_UNSIGNED; break;
case et_sqword: modifiers |= MOD_DECIMAL; break;
#endif /* HOST_HAS_QWORD */
case et_float: /* use default format */break;
case et_double: /* use default format */break;
case et_symbol:
default: return "bad print value";
}
}
if (((modifiers & MOD_FORMATS) & ((modifiers & MOD_FORMATS) - 1)) != 0)
return "multiple format specifiers";
/* decode modifiers */
if (modifiers & (MOD_BYTE|MOD_HALF|MOD_WORD|MOD_QWORD))
{
if (modifiers & MOD_DECIMAL)
radix = 'd';
else if (modifiers & MOD_UNSIGNED)
radix = 'u';
else if (modifiers & MOD_OCTAL)
radix = 'o';
else if (modifiers & MOD_HEX)
radix = 'x';
else if (modifiers & MOD_BINARY)
return "binary format not yet implemented";
else
panic("no default integer format");
if (modifiers & MOD_BYTE)
{
if (modifiers & MOD_OCTAL)
{
prefix = "0";
format = "03";
}
else if (modifiers & MOD_HEX)
{
prefix = "0x";
format = "02";
}
else
{
prefix = "";
format = "";
}
sprintf(buf, "%s%%%s%c", prefix, format, radix);
myfprintf(stdout, buf, eval_as_uint(val));
}
else if (modifiers & MOD_HALF)
{
if (modifiers & MOD_OCTAL)
{
prefix = "0";
format = "06";
}
else if (modifiers & MOD_HEX)
{
prefix = "0x";
format = "04";
}
else
{
prefix = "";
format = "";
}
sprintf(buf, "%s%%%s%c", prefix, format, radix);
myfprintf(stdout, buf, eval_as_uint(val));
}
else if (modifiers & MOD_WORD)
{
if (modifiers & MOD_OCTAL)
{
prefix = "0";
format = "011";
}
else if (modifiers & MOD_HEX)
{
prefix = "0x";
format = "08";
}
else
{
prefix = "";
format = "";
}
sprintf(buf, "%s%%%s%c", prefix, format, radix);
myfprintf(stdout, buf, eval_as_uint(val));
}
#ifdef HOST_HAS_QWORD
else if (modifiers & MOD_QWORD)
{
if (modifiers & MOD_OCTAL)
{
prefix = "0";
format = "022";
}
else if (modifiers & MOD_HEX)
{
prefix = "0x";
format = "016";
}
else
{
prefix = "";
format = "";
}
sprintf(buf, "%s%%%sl%c", prefix, format, radix);
myfprintf(stdout, buf, eval_as_qword(val));
}
#endif /* HOST_HAS_QWORD */
}
else if (modifiers & MOD_FLOAT)
fprintf(stdout, "%f", (double)eval_as_float(val));
else if (modifiers & MOD_DOUBLE)
fprintf(stdout, "%f", eval_as_double(val));
else if (modifiers & MOD_CHAR)
fprintf(stdout, "`%c'", eval_as_uint(val));
else if (modifiers & MOD_STRING)
return "string format not yet implemented";
else /* no format specified, default to value type format */
panic("no default format");
/* no error */
return NULL;
}
/* default memory state accessor */
char * /* err str, NULL for no err */
dlite_mem_obj(struct mem_t *mem, /* memory space to access */
int is_write, /* access type */
md_addr_t addr, /* address to access */
char *p, /* input/output buffer */
int nbytes) /* size of access */
{
enum mem_cmd cmd;
if (!is_write)
cmd = Read;
else
cmd = Write;
#if 0
char *errstr;
errstr = mem_valid(cmd, addr, nbytes, /* !declare */FALSE);
if (errstr)
return errstr;
#endif
/* else, no error, access memory */
mem_access(mem, cmd, addr, p, nbytes);
/* no error */
return NULL;
}
/* default machine state accessor */
char * /* err str, NULL for no err */
dlite_mstate_obj(FILE *stream, /* output stream */
char *cmd, /* optional command string */
struct regs_t *regs, /* registers to access */
struct mem_t *mem) /* memory to access */
{
/* nada */
fprintf(stream, "No machine state.\n");
/* no error */
return NULL;
}
/* scroll terminator, wait for user to press return */
static void
dlite_pause(void)
{
char buf[512];
fprintf(stdout, "Press <return> to continue...");
fflush(stdout);
fgets(buf, 512, stdin);
}
/* print help information for DLite command CMD */
static void
print_help(struct dlite_cmd_t *cmd) /* command to describe */
{
int i;
/* print command name */
fprintf(stdout, " %s ", cmd->cmd_str);
/* print arguments of command */
for (i=0; i < MAX_ARGS && cmd->arg_strs[i] != NULL; i++)
{
int optional;
char *arg, arg_type;
arg = cmd->arg_strs[i];
arg_type = arg[0];
optional = (arg[1] == '?');
if (optional)
fprintf(stdout, "{");
else
fprintf(stdout, "<");
switch (arg_type)
{
case 'm':
fprintf(stdout, "/modifiers");
break;
case 'a':
fprintf(stdout, "addr");
break;
case 'c':
fprintf(stdout, "count");
break;
case 'e':
fprintf(stdout, "expr");
break;
case 't':
fprintf(stdout, "r|w|x");
break;
case 'i':
fprintf(stdout, "id");
break;
case 's':
fprintf(stdout, "string");
break;
default:
panic("bogus argument type: `%c'", arg_type);
}
if (optional)
fprintf(stdout, "}");
else
fprintf(stdout, ">");
fprintf(stdout, " ");
}
fprintf(stdout, "\n");
/* print command description */
fprintf(stdout, " %s\n", cmd->help_str);
}
/* print help messages for all (or single) DLite debugger commands */
static char * /* err str, NULL for no err */
dlite_help(int nargs, union arg_val_t args[], /* command arguments */
struct regs_t *regs, /* registers to access */
struct mem_t *mem) /* memory to access */
{
struct dlite_cmd_t *cmd;
if (nargs != 0 && nargs != 1)
return "too many arguments";
if (nargs == 1)
{
/* print help for specified commands */
for (cmd=cmd_db; cmd->cmd_str != NULL; cmd++)
{
if (!strcmp(cmd->cmd_str, args[0].as_str))
break;
}
if (!cmd->cmd_str)
return "command unknown";
print_help(cmd);
}
else
{
/* print help for all commands */
for (cmd=cmd_db; cmd->cmd_str != NULL; cmd++)