sesl_tcl.c
16.8 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
761
762
763
764
765
/*
* 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 "sesl_expr.h"
#include "sesl_lib.h"
#include "symfile.h"
#include "symbols.h"
#include "sim_error.h"
#include "symfile.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[]);
extern int SymDeref(ParseInfo *pip);
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);
SESL_Init(CPUPrint, CPUWarning, CPUError);
}
int symTclLoad(Tcl_Interp *interp, int argc, char *argv[])
{
if (!SESL_LoadFile(argv[3], argv[2])) {
return TCL_ERROR;
}
return TCL_OK;
}
/*
* symTclRead
*/
#ifdef SAME_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; \
} \
}
#else
#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;
int st;
if (!pip->sym) {
ASSERT(0);
}
st = SESL_GetTargetType(pip->sym);
if ((st != SESL_TYPE_CHAR) && (st != SESL_TYPE_UCHAR)) {
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;
uint64 value;
char buf[128];
bzero((char*)&pi, sizeof(pi));
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
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
}
pi.value = SESL_GetAddress(pi.sym);
switch (SESL_GetType(pi.sym)) {
case SESL_TYPE_LABEL:
case SESL_TYPE_FUNC:
{
ASSERT(pi.constant);
value = pi.value;
PrintLLX(buf,value);
break;
}
case SESL_TYPE_CHAR: {
char c = 0;
GET_VALUE(pi, c, char);
sprintf(buf, "%d", (int)c);
break;
}
case SESL_TYPE_UCHAR: {
unsigned char c = 0;
GET_VALUE(pi, c, unsigned char);
sprintf(buf, "%u", (unsigned)c);
break;
}
case SESL_TYPE_SHORT: {
short s = 0;
GET_VALUE(pi, s, short);
sprintf(buf, "%d", (int)s);
break;
}
case SESL_TYPE_USHORT: {
unsigned short s = 0;
GET_VALUE(pi, s, unsigned short);
sprintf(buf, "%u", (unsigned)s);
break;
}
case SESL_TYPE_INT: {
int i = 0;
GET_VALUE(pi, i, int);
sprintf(buf, "%d", i);
break;
}
case SESL_TYPE_LONG: {
int32 i32 = 0;
int64 i64 = 0;
int size;
switch ((size = (int)SESL_GetSize(pi.sym)))
{
case 4:
GET_VALUE(pi, i32, int32);
i64 = i32;
break;
case 8:
GET_VALUE(pi, i64, int64);
break;
default:
CPUWarning("SYMBOLS: Size of long is unsupported '%d' bytes (assume 4)\n",
size);
GET_VALUE(pi, i32, int32);
i64 = i32;
break;
}
PrintLLD(buf,i64);
break;
}
case SESL_TYPE_LONGLONG: {
int64 i = 0;
GET_VALUE(pi, i, int64);
PrintLLD(buf,(int64)i);
break;
}
case SESL_TYPE_UINT:{
unsigned i = 0;
GET_VALUE(pi, i, unsigned);
sprintf(buf, "%u", i);
break;
}
case SESL_TYPE_ULONG: {
Reg i = 0;
GET_VALUE(pi, i, Reg);
PrintLLU(buf,(uint64)i);
break;
}
case SESL_TYPE_ULONGLONG: {
uint64 i = 0;
GET_VALUE(pi, i, uint64);
PrintLLU(buf,(uint64)i);
break;
}
case SESL_TYPE_FLOAT: {
uint f = 0;
float *fptr = (float*)&f;
GET_VALUE(pi, f, uint);
sprintf(buf, "%f", *fptr);
break;
}
case SESL_TYPE_DOUBLE: {
int64 d = 0;
double *dptr = (double *)&d;
GET_VALUE(pi, d, int64);
sprintf(buf, "%f", *dptr);
break;
}
case SESL_TYPE_PTR:
{
int code = readstr(interp, &pi);
if (code == OK) {
return TCL_OK;
} else if (code == NO) {
uint32 p32 = 0;
uint64 p64 = 0;
int size;
switch ((size = (int)SESL_GetSize(pi.sym)))
{
case 4:
GET_VALUE(pi, p32, uint32);
p64 = p32;
break;
case 8:
GET_VALUE(pi, p64, uint64);
break;
default:
CPUWarning("SYMBOLS: Size of ptr is unsupported '%d' bytes (assume 4)\n",
size);
GET_VALUE(pi, p32, uint32);
p64 = p32;
break;
}
PrintLLX(buf,p64);
} else if (code == ERROR) {
goto error;
}
break;
}
case SESL_TYPE_ARRAY: {
int code;
ASSERT(pi.constant);
code = readstr(interp, &pi);
if (code == OK) {
return TCL_OK;
} else if (code == NO) {
value = pi.value;
PrintLLX(buf, value);
} else {
goto error;
}
break;
}
case SESL_TYPE_STRUCT:
case SESL_TYPE_UNION:
case SESL_TYPE_ENUM: {
/* XXX change */
ASSERT(!pi.constant);
Tcl_AppendResult(interp, "type not impl yet", NULL);
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
}
case SESL_TYPE_UNKNOWN:
default:
SIM_DEBUG(('t', "offending type %s (%d)\n\r",
SESL_GetTypeName(pi.sym), SESL_GetType(pi.sym)));
CPUWarning("offending type %s (%d)\n\r",
SESL_GetTypeName(pi.sym), SESL_GetType(pi.sym));
SESL_FreeSymbol(pi.sym);
ASSERT(0);
break;
}
Tcl_SetResult(interp, buf, TCL_VOLATILE);
SESL_FreeSymbol(pi.sym);
return TCL_OK;
error:
SIM_DEBUG(('t', "addr1 0x%x\n\r", pi.value));
Tcl_AppendResult(interp, "addr not mapped", NULL);
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
}
/*
* symTclSet
*/
#ifdef SAME_ENDIAN
#define SET_VALUE(_pi, _var, _type) \
ASSERT(!(_pi).constant); \
ASSERT(!(_pi).internal); \
{ _type x = _var; \
if (!CPUVec.PutMemory || \
CPUVec.PutMemory(CPUVec.CurrentCpuNum(), \
(_pi).value, sizeof(_type), (char*)&x) \
!= SUCCESS) { \
goto error; \
} \
}
#else
#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; \
} \
}
#endif
int writestr(Tcl_Interp *interp, ParseInfo *pip, char *str)
{
VA addr;
int st;
if (!pip->sym) {
ASSERT(0);
}
st = SESL_GetTargetType(pip->sym);
if ((st != SESL_TYPE_CHAR) && (st != SESL_TYPE_UCHAR)) {
return NO;
}
GET_VALUE(*pip, addr, VA);
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;
bzero((char*)&pi, sizeof(pi));
if (SymParse(argv[2], &pi) != SYM_OK) {
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
}
if (pi.constant) {
Tcl_AppendResult(interp, "cannot set this type, its a constant", NULL);
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
} else if (pi.internal) {
Tcl_AppendResult(interp, "can't write to simulator memory (?)", NULL);
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
}
switch (SESL_GetType(pi.sym)) {
case SESL_TYPE_CHAR:
case SESL_TYPE_UCHAR: {
char c = 0;
int i = 0;
if (sscanf(argv[3], "%d", &i) != 1) {
Tcl_AppendResult(interp, "expected an int", NULL);
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
}
c = (char)i;
SET_VALUE(pi, c, char);
break;
}
case SESL_TYPE_SHORT:
case SESL_TYPE_USHORT: {
int i = 0;
short s;
if (sscanf(argv[3], "%i", &i) != 1) {
Tcl_AppendResult(interp, "expected an int", NULL);
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
}
s = i;
SET_VALUE(pi, s, short);
break;
}
case SESL_TYPE_INT:
case SESL_TYPE_LONG:
case SESL_TYPE_UINT:
case SESL_TYPE_ULONG: {
int i = 0;
if (sscanf(argv[3], "%i", &i) != 1) {
Tcl_AppendResult(interp, "expected an int", NULL);
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
}
SET_VALUE(pi, i, int);
break;
}
case SESL_TYPE_FLOAT: {
float f = 0;
if (sscanf(argv[3], "%f", &f) != 1) {
Tcl_AppendResult(interp, "expected a float", NULL);
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
}
SET_VALUE(pi, f, float);
break;
}
case SESL_TYPE_DOUBLE: {
double d = 0;
if (sscanf(argv[3], "%lf", &d) != 1) {
Tcl_AppendResult(interp, "expected a double", NULL);
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
}
SET_VALUE(pi, d, double);
break;
}
case SESL_TYPE_PTR: {
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);
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
}
SET_VALUE(pi, i, int);
} else {
goto error;
}
break;
}
case SESL_TYPE_ARRAY: {
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);
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
} else {
goto error;
}
break;
}
case SESL_TYPE_ENUM: {
/* XXX change */
Tcl_AppendResult(interp, "cannot set this type", NULL);
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
}
case SESL_TYPE_STRUCT:
case SESL_TYPE_UNION: {
Tcl_AppendResult(interp, "cannot set this type", NULL);
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
}
case SESL_TYPE_LABEL:
case SESL_TYPE_FUNC:
case SESL_TYPE_UNKNOWN:
default:
SESL_FreeSymbol(pi.sym);
ASSERT(0);
break;
}
SESL_FreeSymbol(pi.sym);
return TCL_OK;
error:
SIM_DEBUG(('t', "addr2 0x%x\n\r", pi.value));
Tcl_AppendResult(interp, "addr not mapped", NULL);
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
}
/*
* symTclType
*/
int symTclType(Tcl_Interp *interp, int argc, char *argv[])
{
ParseInfo pi;
char *str;
bzero((char*)&pi, sizeof(pi));
if (SymParse(argv[2], &pi) != SYM_OK) {
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
}
str = SESL_GetTypeName(pi.sym);
Tcl_SetResult(interp, str, TCL_STATIC);
SESL_FreeSymbol(pi.sym);
return TCL_OK;
}
/*
* symInt
*/
int SymInt(Tcl_Interp *interp, char *expr, uint64 *val)
{
ParseInfo pi;
char op[16];
uint64 offset;
char *str;
bzero((char*)&pi, sizeof(pi));
/* 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) {
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
}
switch (SESL_GetType(pi.sym)) {
case SESL_TYPE_LABEL:
case SESL_TYPE_FUNC:
case SESL_TYPE_ARRAY:
{
ASSERT(pi.constant);
*val = (uint64)pi.value;
break;
}
case SESL_TYPE_CHAR:
case SESL_TYPE_UCHAR: {
unsigned char c = 0;
GET_VALUE(pi, c, unsigned char);
*val = (uint64)c;
break;
}
case SESL_TYPE_SHORT:
case SESL_TYPE_USHORT: {
unsigned short s = 0;
GET_VALUE(pi, s, unsigned short);
*val = (uint64)s;
break;
}
case SESL_TYPE_INT:
case SESL_TYPE_LONG:
case SESL_TYPE_UINT:
case SESL_TYPE_ULONG:
case SESL_TYPE_PTR:
{
uint i = 0;
GET_VALUE(pi, i, uint);
*val = (uint64)i;
break;
}
case SESL_TYPE_ENUM: {
Tcl_AppendResult(interp, "type not supported yet", NULL);
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
}
case SESL_TYPE_FLOAT:
case SESL_TYPE_DOUBLE:
case SESL_TYPE_STRUCT:
case SESL_TYPE_UNION: {
Tcl_AppendResult(interp, "type not supported", NULL);
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
}
case SESL_TYPE_UNKNOWN:
default:
SIM_DEBUG(('t', "violating type = %d\n\r", SESL_GetType(pi.sym)));
SESL_FreeSymbol(pi.sym);
ASSERT(0);
break;
}
SESL_FreeSymbol(pi.sym);
return TCL_OK;
error:
Tcl_AppendResult(interp, "addr not mapped", NULL);
SESL_FreeSymbol(pi.sym);
return TCL_ERROR;
}
/*
* symTclType
*/
int symTclFind(Tcl_Interp *interp, int argc, char *argv[])
{
Reg addr;
char *cptr;
if (String2Reg(interp, argv[3], &addr) != TCL_OK) {
return TCL_ERROR;
}
if (!(cptr = SESL_Addr2Symbolic(argv[2], addr))) {
return TCL_ERROR;
}
Tcl_SetResult(interp, cptr, TCL_STATIC);
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);
}