gdbHandler.c
16.5 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
/*====================================================================
*
* File: gdbStep.c
* Author: Steve Shepard (sjs@sgi.com)
*
* GDB exception handling rountines for the Nintendo64
*
* To do:
* - bullet proof mem reads and writes
* - implement "G"
* - thread announcement & handling
*
* The following gdb commands are supported:
* See gdb/remote.c in gdb source for details
*
* command function Return value
*
* Hct... set thread c = 'c' or 'g' OK or ENN
* g return the value of the CPU registers hex data or ENN
* G set the value of the CPU registers OK or ENN
*
* mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
* MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
*
* c Resume at current address SNN ( signal NN)
* cAA..AA Continue at address AA..AA SNN
*
* s Step one instruction SNN
* sAA..AA Step one instruction from AA..AA SNN
*
* k kill
*
* ? What was the last sigval ? SNN (signal NN)
*
* All commands and responses are sent with a packet which includes a
* checksum. A packet consists of
*
* $<packet info>#<checksum>.
*
* where
* <packet info> :: <characters representing the command or response>
* <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
*
* When a packet is received, it is first acknowledged with either '+' or '-'.
* '+' indicates a successful transfer. '-' indicates a failed transfer.
*
* Example:
*
* Host: Reply:
* $m0,10#2a +$00010203040506070809101112131415#42
*
*====================================================================*/
#include <R4300.h>
#include <rdb.h>
#include <os_internal.h>
#ifdef __sgi__
#include <sys/signal.h>
#else
#define SIGHUP 1 /* Hangup (POSIX). */
#define SIGINT 2 /* Interrupt (ANSI). */
#define SIGQUIT 3 /* Quit (POSIX). */
#define SIGILL 4 /* Illegal instruction (ANSI). */
#define SIGTRAP 5 /* Trace trap (POSIX). */
#define SIGABRT 6 /* Abort (ANSI). */
#define SIGIOT 6 /* IOT trap (4.2 BSD). */
#define SIGBUS 7 /* BUS error (4.2 BSD). */
#define SIGFPE 8 /* Floating-point exception (ANSI). */
#define SIGKILL 9 /* Kill, unblockable (POSIX). */
#define SIGUSR1 10 /* User-defined signal 1 (POSIX). */
#define SIGSEGV 11 /* Segmentation violation (ANSI). */
#define SIGUSR2 12 /* User-defined signal 2 (POSIX). */
#define SIGPIPE 13 /* Broken pipe (POSIX). */
#define SIGALRM 14 /* Alarm clock (POSIX). */
#define SIGTERM 15 /* Termination (ANSI). */
#endif
#include "ultragdb.h"
Registers reg;
/*
* Save room for all four argument registers even though there is only
* a single argument just in case the app accidentally declares more.
*/
#define ARGSAVE 16
#define GDB_STACKSIZE 2048
u64 gdbStack[GDB_STACKSIZE/sizeof(u64)];
u64 *gdbStackPtr = gdbStack + (GDB_STACKSIZE-ARGSAVE)/sizeof(u64);
#define BUFMAX 2048
static char inBuffer[BUFMAX];
static char outBuffer[BUFMAX];
int gdbDebug = 0;
static const char hexchars[]="0123456789abcdef";
extern OSThread __osThreadSave;
extern OSThread *__osRunningThread;
extern OSThread *__osActiveQueue;
extern int strlen(const char *);
static OSThread *curThread = (OSThread *)-1;
char *strcpy (char *s1, const char *s2)
{
char *dest=s1;
char c;
do {
c = *s2++;
*dest++ = c;
} while (c != 0);
return s1;
}
/* convert the memory pointed to by mem into hex, placing result in buf */
/* return a pointer to the last char put in buf (null) */
char* mem2hex(char *mem, char *buf, int count)
{
int i;
unsigned char ch;
for (i=0;i<count;i++) {
ch = *mem++;
*buf++ = hexchars[ch >> 4];
*buf++ = hexchars[ch % 16];
}
*buf = 0;
return(buf);
}
static void send_packet(char *s, int n)
{ /* 1 <= n <= 3 */
rdbPacket packet;
int i;
packet.type = RDB_TYPE_GtoH_DEBUG;
packet.length = n;
for (i = 0; i < n; i++)
packet.buf[i] = s[i];
/* send out packets and wait for done */
*((vu32 *) RDB_BASE_REG) = *((vu32 *) &packet);
while ((__osGetCause() & CAUSE_IP6) == 0);
*((vu32 *) RDB_READ_INTR_REG) = 0;
}
static void send(char *s, int n)
{
int i, j, k;
/* wait until port is free */
if (!__osRdbWriteOK) {
while ((__osGetCause() & CAUSE_IP6) == 0);
*((vu32 *) RDB_READ_INTR_REG) = 0;
__osRdbWriteOK = 1;
}
k = n % 3;
j = n - k;
for (i = 0; i < j; i += 3)
send_packet(&s[i], 3);
if (k > 0)
send_packet(&s[j], k);
}
int getDebugChar(void)
{
char c = 0;
static rdbPacket packet = {0,0,{0,0,0}};
static int cnt = 0;
if (cnt >= packet.length) {
/* get the next packet */
while ((__osGetCause() & CAUSE_IP7) == 0);
*((vu32 *) &packet) = *((vu32 *) RDB_BASE_REG) ;
*((vu32 *) RDB_WRITE_INTR_REG) = 0;
cnt = 0;
}
if (packet.length)
c = packet.buf[cnt++];
return c;
}
char putDebugChar(char c)
{
send(&c, 1);
return c;
}
int hex(char ch)
{
if ((ch >= 'a') && (ch <= 'f')) return (ch-'a'+10);
if ((ch >= '0') && (ch <= '9')) return (ch-'0');
if ((ch >= 'A') && (ch <= 'F')) return (ch-'A'+10);
return (-1);
}
/**********************************************/
/* WHILE WE FIND NICE HEX CHARS, BUILD AN INT */
/* RETURN NUMBER OF CHARS PROCESSED */
/**********************************************/
int hexToInt(char **ptr, int *intValue)
{
int numChars = 0;
int hexValue;
*intValue = 0;
while (**ptr)
{
hexValue = hex(**ptr);
if (hexValue >=0)
{
*intValue = (*intValue <<4) | hexValue;
numChars ++;
}
else
break;
(*ptr)++;
}
return (numChars);
}
/* convert the hex array pointed to by buf into binary to be placed in mem */
/* return a pointer to the character AFTER the last byte written */
char* hex2mem(char *buf, char *mem, int count)
{
int i;
unsigned char ch;
for (i=0;i<count;i++) {
ch = hex(*buf++) << 4;
ch = ch + hex(*buf++);
*mem++ = ch;
}
return(mem);
}
/* scan for the sequence $<data>#<checksum> */
void getpacket(char *buffer)
{
unsigned char checksum;
unsigned char xmitcsum;
int i;
int count;
char ch;
do {
/* wait around for the start character, ignore all other characters */
while ((ch = (getDebugChar() & 0x7f)) != '$');
checksum = 0;
xmitcsum = -1;
count = 0;
/* now, read until a # or end of buffer is found */
while (count < BUFMAX) {
ch = getDebugChar() & 0x7f;
if (ch == '#') break;
checksum = checksum + ch;
buffer[count] = ch;
count = count + 1;
}
buffer[count] = 0;
if (ch == '#') {
xmitcsum = hex(getDebugChar() & 0x7f) << 4;
xmitcsum += hex(getDebugChar() & 0x7f);
if ((gdbDebug ) && (checksum != xmitcsum)) {
/* error */
}
if (checksum != xmitcsum)
putDebugChar('-'); /* failed checksum */
else {
putDebugChar('+'); /* successful transfer */
/* if a sequence char is present, reply the sequence ID */
if (buffer[2] == ':') {
putDebugChar( buffer[0] );
putDebugChar( buffer[1] );
/* remove sequence chars from buffer */
count = strlen(buffer);
for (i=3; i <= count; i++) buffer[i-3] = buffer[i];
}
}
}
} while (checksum != xmitcsum);
}
void putpacket(char *buffer)
{
unsigned char checksum;
int count;
char ch;
/* $<packet info>#<checksum>. */
do {
putDebugChar('$');
checksum = 0;
count = 0;
while ((ch=buffer[count])) {
if (! putDebugChar(ch)) return;
checksum += ch;
count += 1;
}
putDebugChar('#');
putDebugChar(hexchars[checksum >> 4]);
putDebugChar(hexchars[checksum % 16]);
} while (1 == 0); /* (getDebugChar() != '+'); */
}
int computeSig( u32 cause )
{
int sig;
switch (cause & CAUSE_EXCMASK) {
case (EXC_INT): sig = SIGINT; break; /* interrupt */
case (EXC_BREAK): sig = SIGTRAP; break; /* BREAKpoint */
case (EXC_RADE): sig = SIGSEGV; break; /* Read Address Error */
case (EXC_WADE): sig = SIGSEGV; break; /* Write Address Error */
case (EXC_IBE): sig = SIGBUS; break; /* Instruction Bus Error */
case (EXC_DBE): sig = SIGBUS; break; /* Data Bus Error */
case (EXC_II): sig = SIGILL; break; /* Illegal Instruction */
case (EXC_FPE): sig = SIGFPE; break; /* Floating Point Exception */
default: sig = 7; /* software generated */
}
return sig;
}
void assembleRegisters(char *ptr)
{
u32 *src;
u32 *dest;
int i;
__OSThreadContext *c = &curThread->context;
reg.zero = 0;
reg.at = c->at;
reg.v0 = c->v0;
reg.v1 = c->v1;
reg.a0 = c->a0;
reg.a1 = c->a1;
reg.a2 = c->a2;
reg.a3 = c->a3;
reg.t0 = c->t0;
reg.t1 = c->t1;
reg.t2 = c->t2;
reg.t3 = c->t3;
reg.t4 = c->t4;
reg.t5 = c->t5;
reg.t6 = c->t6;
reg.t7 = c->t7;
reg.s0 = c->s0;
reg.s1 = c->s1;
reg.s2 = c->s2;
reg.s3 = c->s3;
reg.s4 = c->s4;
reg.s5 = c->s5;
reg.s6 = c->s6;
reg.s7 = c->s7;
reg.t8 = c->t8;
reg.t9 = c->t9;
reg.gp = c->gp;
reg.sp = c->sp;
reg.s8 = c->s8;
reg.ra = c->ra;
reg.lo = c->lo;
reg.hi = c->hi;
reg.bad = c->badvaddr;
reg.cause = c->cause;
reg.pc = c->pc;
dest = (u32 *)®.f0;
src = (u32 *)&c->fp0;
for (i = 0; i < 32; i++)
*dest++ = *src++;
#if 0
reg.f0 = c->fp0.f.f_even;
reg.f1 = c->fp0.f.f_odd;
reg.f2 = c->fp0.f.f_even;
reg.f3 = c->fp0.f.f_odd;
reg.f4 = c->fp0.f.f_even;
reg.f5 = c->fp0.f.f_odd;
reg.f6 = c->fp0.f.f_even;
reg.f7 = c->fp0.f.f_odd;
reg.f8 = c->fp0.f.f_even;
reg.f9 = c->fp0.f.f_odd;
reg.f10 = c->fp0.f.f_even;
reg.f11 = c->fp0.f.f_odd;
reg.f12 = c->fp0.f.f_even;
reg.f13 = c->fp0.f.f_odd;
reg.f14 = c->fp0.f.f_even;
reg.f15 = c->fp0.f.f_odd;
reg.f16 = c->fp0.f.f_even;
reg.f17 = c->fp0.f.f_odd;
reg.f18 = c->fp0.f.f_even;
reg.f19 = c->fp0.f.f_odd;
reg.f20 = c->fp0.f.f_even;
reg.f21 = c->fp0.f.f_odd;
reg.f22 = c->fp0.f.f_even;
reg.f23 = c->fp0.f.f_odd;
reg.f24 = c->fp0.f.f_even;
reg.f25 = c->fp0.f.f_odd;
reg.f26 = c->fp0.f.f_even;
reg.f27 = c->fp0.f.f_odd;
reg.f28 = c->fp0.f.f_even;
reg.f29 = c->fp0.f.f_odd;
reg.f30 = c->fp0.f.f_even;
reg.f31 = c->fp0.f.f_odd;
#endif
mem2hex((char *)®, ptr, sizeof(Registers));
}
OSThread *idToThread(int threadID)
{
OSThread *t;
/* ### should make sure that the thread is a valid addr */
/* loop through the list of current threads */
for (t = __osActiveQueue; t != NULL; t = t->tlnext) {
if (t->id == threadID) {
break;
}
}
return t;
}
void writeCause(char *buf, int sigval, int threadID)
{
*buf++ = 'T';
*buf++ = hexchars[sigval >> 4];
*buf++ = hexchars[sigval % 16];
*buf++ = 't';
*buf++ = 'h';
*buf++ = 'r';
*buf++ = 'e';
*buf++ = 'a';
*buf++ = 'd';
*buf++ = ':';
*buf++ = hexchars[threadID >> 4];
*buf++ = hexchars[threadID % 16];
*buf++ = ';';
*buf++ = 0;
}
void handleException(u32 cause)
{
int addr, length;
char *ptr;
OSThread *t;
int sigval;
if (gdbDebug)
osSyncPrintf("enter\n");
#if 1
sigval = computeSig(cause);
#else
sigval = 5;
#endif
if (cause) {
if (gdbDebug)
osSyncPrintf("ex: cause = 0x%x\n", cause);
curThread = __osRunningThread;
/* if it was a step command remove tmp breakpoints */
if ((cause & CAUSE_EXCMASK) == EXC_BREAK) {
/* ### check valid */
u32 inst = *(u32 *)__osRunningThread->context.pc;
if (inst == TMP_BREAK) { /* temp break point */
removeBP();
} else if (inst == CNX_BREAK) { /* gdbBreak() */
/* skip past break instruction */
__osRunningThread->context.pc += 4;
}
}
writeCause(outBuffer, sigval, __osRunningThread->id);
putpacket(outBuffer);
} else { /* debugger interrupt */
curThread = &__osThreadSave;
getpacket(inBuffer); /* this is usually Hc-1 */
if (gdbDebug)
osSyncPrintf("got: %s\n", inBuffer);
outBuffer[0] = 'O';
outBuffer[1] = 'K';
outBuffer[2] = 0;
putpacket(outBuffer);
}
while (1) {
outBuffer[0] = 0;
getpacket(inBuffer);
if (gdbDebug)
osSyncPrintf("got: %s\n", inBuffer);
switch(inBuffer[0]) {
case ('H'): /* Hct... or Hgt... set thread for conintue & general*/
if (inBuffer[1] == 'g') {
ptr = &inBuffer[2];
if (hexToInt(&ptr, &addr)) /* get t */
{
t = idToThread(addr);
if (t) {
curThread = t;
strcpy(outBuffer, "OK");
} else {
strcpy(outBuffer, "E02");
}
}
}
break;
case ('?'):
writeCause(outBuffer, sigval, __osRunningThread->id);
break;
case ('g'):
assembleRegisters(outBuffer);
break;
case ('s'):
/* there is no hardware support for single step, so we
install tmp breakpoints, and remove them when the
step is complete */
installBP();
if (gdbDebug)
osSyncPrintf("exit\n");
return;
case ('c'): /* cAA..AA Continue at address AA..AA(optional) */
ptr = &inBuffer[1];
if (hexToInt(&ptr, &addr))
{
#if 0
registers[PC] = addr;
registers[NPC] = addr + 4;
#endif
}
/* ### need to flush the cache !*/
if (gdbDebug)
osSyncPrintf("exit\n");
return;
case ('M'):
/* MAA..AA,LLLL: Write LLLL bytes at addr AA.AA return
OK */
/* Try to read '%x,%x:'. */
ptr = &inBuffer[1];
if (hexToInt(&ptr, &addr)
&& *ptr++ == ','
&& hexToInt(&ptr, &length)
&& *ptr++ == ':')
{
if (hex2mem(ptr, (char *)addr, length)) {
gdbWritebackDCache((void *)addr, length);
gdbInvalICache((void *)addr, length);
strcpy(outBuffer, "OK");
} else
strcpy(outBuffer, "E03");
}
else
strcpy(outBuffer, "E02");
break;
case ('m'): /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
/* Try to read %x,%x. */
/* ### need to handle read fault! */
ptr = &inBuffer[1];
if (hexToInt(&ptr, &addr)
&& *ptr++ == ','
&& hexToInt(&ptr, &length))
{
if (mem2hex((char *)addr, outBuffer, length))
break;
strcpy (outBuffer, "E03");
}
else
strcpy(outBuffer,"E01");
break;
}
putpacket(outBuffer);
}
}