symtcl.c
15.7 KB
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
/*
* Copyright (C) 1996-1998 by the Board of Trustees
* of Leland Stanford Junior University.
*
* This file is part of the SimOS distribution.
* See LICENSE file for terms of the license.
*
*/
/* Tcl interface to symbol commands.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <netinet/in.h>
#include "tcl_init.h"
#include "cpu_interface.h"
#include "symexpr.h"
#include "symfile.h"
#include "symbols.h"
#include "sim_error.h"
#define OK 0
#define ERROR 1
#define NO 2
static int symTclLoad(Tcl_Interp *interp, int argc, char *argv[]);
static int symTclRead(Tcl_Interp *interp, int argc, char *argv[]);
static int symTclType(Tcl_Interp *interp, int argc, char *argv[]);
static int symTclSet(Tcl_Interp *interp, int argc, char *argv[]);
static int symTclFind(Tcl_Interp *interp, int argc, char *argv[]);
static tclcmd symcmds[] = {
{ "load", 4, symTclLoad, " load exeName path"},
{ "read", 3, symTclRead, " read symexp"},
{ "set", 4, symTclSet, " set symexp value"},
{ "type", 3, symTclType, " type symexp"},
{ "find", 4, symTclFind, " find execname addr"},
{ NULL, 0, NULL, NULL}
};
static int readstr(Tcl_Interp *interp, ParseInfo *pip);
void SymInit(Tcl_Interp *interp)
{
Tcl_CreateCommand(interp, "symbol", DispatchCmd, (ClientData) symcmds, NULL);
}
int symTclLoad(Tcl_Interp *interp, int argc, char *argv[])
{
#ifdef DWARF2__not
if (Dwarf_SymLoad(argv[3], argv[2]) != SYM_OK) {
return TCL_ERROR;
}
#else
if (SymLoad(argv[3], argv[2]) != SYM_OK) {
return TCL_ERROR;
}
#endif
return TCL_OK;
}
/*
* symTclRead
*/
#ifdef HOST_BIG_ENDIAN
#define GET_VALUE(_pi, _var, _type) \
if ((_pi).constant) { \
_var = (_type)(_pi).value; \
} else if ((_pi).internal) { \
_var = *(_type *)(_pi).value; \
} else { \
if (!CPUVec.GetMemory || \
CPUVec.GetMemory(CPUVec.CurrentCpuNum(), \
(_pi).value, sizeof(_type), (char*)&(_var)) \
!= SUCCESS) { \
goto error; \
} \
}
#endif
#ifdef HOST_LITTLE_ENDIAN
#define GET_VALUE(_pi, _var, _type) \
if ((_pi).constant) { \
_var = (_type)(_pi).value; \
} else if ((_pi).internal) { \
ASSERT (0); \
} else { \
if (!CPUVec.GetMemory || \
CPUVec.GetMemory(CPUVec.CurrentCpuNum(), \
(_pi).value, sizeof(_type), (char*)&(_var)) \
!= SUCCESS) { \
goto error; \
} \
if (sizeof(_type)==2) _var = BE2HO_2(_var); \
if (sizeof(_type)==4) _var = BE2HO_4(_var); \
if (sizeof(_type)==8) _var = BE2HO_8(_var); \
}
#endif
int readstr(Tcl_Interp *interp, ParseInfo *pip)
{
Tcl_DString dstr;
char c = '\0';
VA addr;
Symb tmp = pip->sym;
int st;
if (SymSubtype(&tmp, SYM_RESOLVE) != SYM_OK) {
ASSERT(0);
}
st = SymType(&tmp, SYM_RESOLVE);
if ((st != tChar) && (st != tUChar)) {
return NO;
}
GET_VALUE(*pip, addr, VA);
Tcl_DStringInit(&dstr);
while (1) {
if (pip->internal) {
#if defined(sgi) || defined(sun)
c = *(char*)addr;
#else
ASSERT (0);
#endif
} else if (CPUVec.GetMemory(CPUVec.CurrentCpuNum(), addr, 1,
(char *)&c) != SUCCESS) {
return ERROR;
}
if (!c) {
break;
}
Tcl_DStringAppend(&dstr, &c, 1);
addr++;
}
Tcl_DStringResult(interp, &dstr);
return OK;
error:
Tcl_DStringFree(&dstr);
return ERROR;
}
int symTclRead(Tcl_Interp *interp, int argc, char *argv[])
{
ParseInfo pi;
char buf[128];
if (SymParse(argv[2], &pi) != SYM_OK) {
#ifdef notdef
if ((pi.str - argv[2]) < 128) {
strncpy(buf, argv[2], pi.str - argv[2]);
buf[pi.str - argv[2]] = '\0';
Tcl_AppendResult(interp, "\n ", buf, NULL);
}
#endif
return TCL_ERROR;
}
switch (SymType(&pi.sym, SYM_RESOLVE)) {
case tLabel:
case tProc:
case tAddr: {
ASSERT(pi.constant);
PrintLLX(buf,(uint64)pi.value);
break;
}
case tChar: {
char c = 0;
GET_VALUE(pi, c, char);
sprintf(buf, "%d", (int)c);
break;
}
case tUChar: {
unsigned char c = 0;
GET_VALUE(pi, c, unsigned char);
sprintf(buf, "%u", (unsigned)c);
break;
}
case tShort: {
short s = 0;
GET_VALUE(pi, s, short);
sprintf(buf, "%d", (int)s);
break;
}
case tUShort: {
unsigned short s = 0;
GET_VALUE(pi, s, unsigned short);
sprintf(buf, "%u", (unsigned)s);
break;
}
case tInt: {
int i = 0;
GET_VALUE(pi, i, int);
sprintf(buf, "%d", i);
break;
}
case tLong: {
/*
* XXX Reg should be a long in the target system
*/
Reg_s i = 0;
GET_VALUE(pi, i, Reg_s);
PrintLLD(buf,(int64)i);
break;
}
case tUInt:{
unsigned i = 0;
GET_VALUE(pi, i, unsigned);
sprintf(buf, "%u", i);
break;
}
case tULong: {
Reg i = 0;
GET_VALUE(pi, i, Reg);
PrintLLU(buf,(uint64)i);
break;
}
case tFloat: {
uint f = 0;
float *fptr = (float*)&f;
GET_VALUE(pi, f, uint);
sprintf(buf, "%f", *fptr);
break;
}
case tDouble: {
int64 d = 0;
double *dptr = (double *)&d;
GET_VALUE(pi, d, int64);
sprintf(buf, "%f", *dptr);
break;
}
case tPtr: {
int code = readstr(interp, &pi);
if (code == OK) {
return TCL_OK;
} else if (code == NO) {
Reg p = 0;
GET_VALUE(pi, p, Reg);
PrintLLX(buf,(uint64)p);
} else if (code == ERROR) {
goto error;
}
break;
}
case tArray: {
int code;
ASSERT(pi.constant);
code = readstr(interp, &pi);
if (code == OK) {
return TCL_OK;
} else if (code == NO) {
PrintLLX(buf, (uint64)pi.value);
} else {
goto error;
}
break;
}
case tStruct:
case tUnion:
case tEnum: {
/* XXX change */
ASSERT(!pi.constant);
Tcl_AppendResult(interp, "type not impl yet", NULL);
return TCL_ERROR;
}
case tNil:
default:
SIM_DEBUG(('t', "offending type %d\n\r", SymType(&pi.sym, SYM_RESOLVE)));
CPUWarning("offending type %d\n\r", SymType(&pi.sym, SYM_RESOLVE));
ASSERT(0);
break;
}
Tcl_SetResult(interp, buf, TCL_VOLATILE);
return TCL_OK;
error:
SIM_DEBUG(('t', "addr1 0x%x\n\r", pi.value));
Tcl_AppendResult(interp, "addr not mapped", NULL);
return TCL_ERROR;
}
/*
* symTclSet
*/
#if SAME_ENDIAN
#define SET_VALUE(_pi, _var, _type) \
ASSERT(!(_pi).constant); \
ASSERT(!(_pi).internal); \
{ _type x = _var; \
if (sizeof(_type)==2) x = BE2HO_2(x); \
if (sizeof(_type)==4) x = BE2HO_4(x); \
if (sizeof(_type)==8) x = BE2HO_8(x); \
if (!CPUVec.PutMemory || \
CPUVec.PutMemory(CPUVec.CurrentCpuNum(), \
(_pi).value, sizeof(_type), (char*)&x) \
!= SUCCESS) { \
goto error; \
} \
}
#else
/*XXXblythe hack things up to handle float/double*/
#define SET_VALUE(_pi, _var, _type) \
ASSERT(!(_pi).constant); \
ASSERT(!(_pi).internal); \
{ _type x = _var; \
if (sizeof(_type)==2) { short* y = (short*)&x; *y = BE2HO_2(*y); } \
if (sizeof(_type)==4) { int* y = (int*)&x; *y = BE2HO_4(*y); } \
if (sizeof(_type)==8) { long long* y = (long long*)&x; *y = BE2HO_8(*y); } \
if (!CPUVec.PutMemory || \
CPUVec.PutMemory(CPUVec.CurrentCpuNum(), \
(_pi).value, sizeof(_type), (char*)&x) \
!= SUCCESS) { \
goto error; \
} \
}
#endif
int writestr(Tcl_Interp *interp, ParseInfo *pip, char *str)
{
int addr;
Symb tmp = pip->sym;
int st;
if (SymSubtype(&tmp, 0) != SYM_OK) {
ASSERT(0);
}
st = SymType(&tmp, SYM_RESOLVE);
if ((st != tChar) && (st != tUChar)) {
return NO;
}
GET_VALUE(*pip, addr, int);
while (1) {
if (CPUVec.PutMemory(CPUVec.CurrentCpuNum(), addr, 1,
(char *)str) != SUCCESS) {
return ERROR;
}
if (*str == '\0') break;
str++;
addr++;
}
return OK;
error:
return ERROR;
}
int symTclSet(Tcl_Interp *interp, int argc, char *argv[])
{
ParseInfo pi;
if (SymParse(argv[2], &pi) != SYM_OK) {
return TCL_ERROR;
}
if (pi.constant) {
Tcl_AppendResult(interp, "cannot set this type, its a constant", NULL);
return TCL_ERROR;
} else if (pi.internal) {
Tcl_AppendResult(interp, "can't write to simulator memory (?)", NULL);
return TCL_ERROR;
}
switch (SymType(&pi.sym, SYM_RESOLVE)) {
case tChar:
case tUChar: {
char c = 0;
int i = 0;
if (sscanf(argv[3], "%d", &i) != 1) {
Tcl_AppendResult(interp, "expected an int", NULL);
return TCL_ERROR;
}
c = (char)i;
SET_VALUE(pi, c, char);
break;
}
case tShort:
case tUShort: {
int i = 0;
short s;
if (sscanf(argv[3], "%i", &i) != 1) {
Tcl_AppendResult(interp, "expected an int", NULL);
return TCL_ERROR;
}
s = i;
SET_VALUE(pi, s, short);
break;
}
case tInt:
case tLong:
case tUInt:
case tULong: {
int i = 0;
if (sscanf(argv[3], "%i", &i) != 1) {
Tcl_AppendResult(interp, "expected an int", NULL);
return TCL_ERROR;
}
SET_VALUE(pi, i, int);
break;
}
case tFloat: {
float f = 0;
if (sscanf(argv[3], "%f", &f) != 1) {
Tcl_AppendResult(interp, "expected a float", NULL);
return TCL_ERROR;
}
SET_VALUE(pi, f, float);
break;
}
case tDouble: {
double d = 0;
if (sscanf(argv[3], "%lf", &d) != 1) {
Tcl_AppendResult(interp, "expected a double", NULL);
return TCL_ERROR;
}
SET_VALUE(pi, d, double);
break;
}
case tPtr: {
int code = writestr(interp, &pi, argv[3]);
if (code == OK) {
} else if (code == NO) {
int i = 0;
if (sscanf(argv[3], "%i", &i) != 1) {
Tcl_AppendResult(interp, "expected an int", NULL);
return TCL_ERROR;
}
SET_VALUE(pi, i, int);
} else {
goto error;
}
break;
}
case tArray: {
int code = writestr(interp, &pi, argv[3]);
if (code == OK) {
} else if (code == NO) {
/* XXX change ? */
Tcl_AppendResult(interp, "can't write an array", NULL);
return TCL_ERROR;
} else {
goto error;
}
break;
}
case tEnum: {
/* XXX change */
Tcl_AppendResult(interp, "cannot set this type", NULL);
return TCL_ERROR;
}
case tStruct:
case tUnion: {
Tcl_AppendResult(interp, "cannot set this type", NULL);
return TCL_ERROR;
}
case tLabel:
case tProc:
case tAddr:
case tNil:
default:
ASSERT(0);
break;
}
return TCL_OK;
error:
SIM_DEBUG(('t', "addr2 0x%x\n\r", pi.value));
Tcl_AppendResult(interp, "addr not mapped", NULL);
return TCL_ERROR;
}
/*
* symTclType
*/
int symTclType(Tcl_Interp *interp, int argc, char *argv[])
{
ParseInfo pi;
char *str;
if (SymParse(argv[2], &pi) != SYM_OK) {
return TCL_ERROR;
}
switch (SymType(&pi.sym, 0)) {
case tNil:
str = "nil";
break;
case tLabel:
str = "label";
break;
case tProc:
str = "proc";
break;
case tStruct:
/* XXX change */
str = "struct";
break;
case tUnion:
/* XXX change */
str = "union";
break;
case tEnum:
/* XXX change */
str = "enum";
break;
case tChar:
str = "char";
break;
case tUChar:
str = "uchar";
break;
case tShort:
str = "short";
break;
case tUShort:
str = "ushort";
break;
case tInt:
str = "int";
break;
case tUInt:
str = "uint";
break;
case tLong:
str = "long";
break;
case tULong:
str = "ulong";
break;
case tFloat:
str = "float";
break;
case tDouble:
str = "double";
break;
case tTypedef:
str = "typedef";
break;
case tAddr:
/* XXX change */
str = "addr";
break;
case tPtr:
/* XXX change */
str = "ptr";
break;
case tArray:
/* XXX change */
str = "array";
break;
default:
ASSERT(0);
str = ""; /* Quite compiler warning. */
break;
}
Tcl_SetResult(interp, str, TCL_STATIC);
return TCL_OK;
}
/*
* symInt
*/
int SymInt(Tcl_Interp *interp, char *expr, uint64 *val)
{
ParseInfo pi;
char op[16];
uint64 offset;
/* check for int first */
#ifdef __alpha
if (sscanf(expr, "%li", val) == 1) {
return TCL_OK;
}
if (sscanf(expr,"0x%lx%[-+]0x%lx",val,op,&offset)==3) {
if (*op == '+') {
*val += offset;
} else {
*val -= offset;
}
return TCL_OK;
}
if (sscanf(expr,"0x%lx%[-+]%li",val,op,&offset)==3) {
if (*op == '+') {
*val += offset;
} else {
*val -= offset;
}
return TCL_OK;
}
if (sscanf(expr,"0x%lx",val)==1) {
return TCL_OK;
}
#else
if (sscanf(expr, "%lli", val) == 1) {
return TCL_OK;
}
if (sscanf(expr,"0x%llx%[-+]0x%llx",val,op,&offset)==3) {
if (*op == '+') {
*val += offset;
} else {
*val -= offset;
}
return TCL_OK;
}
if (sscanf(expr,"0x%llx%[-+]%lli",val,op,&offset)==3) {
if (*op == '+') {
*val += offset;
} else {
*val -= offset;
}
return TCL_OK;
}
if (sscanf(expr, "0x%llx", val) == 1) {
return TCL_OK;
}
#endif
ASSERT(interp);
if (SymParse(expr, &pi) != SYM_OK) {
return TCL_ERROR;
}
switch (SymType(&pi.sym, SYM_RESOLVE)) {
case tLabel:
case tProc:
case tAddr:
case tArray: {
ASSERT(pi.constant);
*val = (uint64)pi.value;
break;
}
case tChar:
case tUChar: {
unsigned char c = 0;
GET_VALUE(pi, c, unsigned char);
*val = (uint64)c;
break;
}
case tShort:
case tUShort: {
unsigned short s = 0;
GET_VALUE(pi, s, unsigned short);
*val = (uint64)s;
break;
}
case tInt:
case tLong:
case tUInt:
case tULong:
case tPtr: {
uint i = 0;
GET_VALUE(pi, i, uint);
*val = (uint64)i;
break;
}
case tEnum: {
Tcl_AppendResult(interp, "type not supported yet", NULL);
return TCL_ERROR;
}
case tFloat:
case tDouble:
case tStruct:
case tUnion: {
Tcl_AppendResult(interp, "type not supported", NULL);
return TCL_ERROR;
}
case tNil:
default:
SIM_DEBUG(('t', "violating type = %d\n\r", SymType(&pi.sym, SYM_RESOLVE)));
ASSERT(0);
break;
}
return TCL_OK;
error:
Tcl_AppendResult(interp, "addr not mapped", NULL);
return TCL_ERROR;
}
/*
* symTclType
*/
int symTclFind(Tcl_Interp *interp, int argc, char *argv[])
{
Symb sym;
int addr;
char buf[32];
if (Tcl_GetInt(interp, argv[3], &addr) != TCL_OK) {
return TCL_ERROR;
}
if (SymAddr2Symbol(argv[2], addr, &sym) != SYM_OK) {
return TCL_ERROR;
}
Tcl_SetResult(interp, sym.name, TCL_STATIC);
if (sym.value != addr) {
sprintf(buf, "+%d", (uint)addr - (uint)sym.value);
Tcl_AppendResult(interp, buf, NULL);
}
return TCL_OK;
}
/* insultate rest of symbol stuff from tcl */
void SymReportError(char *first, ...)
{
va_list ap;
char *next;
Tcl_SetResult(TCLInterp, first, TCL_STATIC);
va_start(ap, first);
while ((next = va_arg(ap, char *))) {
Tcl_AppendResult(TCLInterp, next, NULL);
}
va_end(ap);
}