gdb_interface.c
17.1 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
/*
* 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.
*
*/
/*****************************************************************
* Interface to GDB. For debugging OS or Apps ontop of simulator.
*****************************************************************/
#ifndef _LONGLONG
#define _LONGLONG
#endif
#define R4000 1
#include <sys/types.h>
#include <sys/file.h>
#include <unistd.h>
#include <sys/socket.h>
#include <memory.h>
#include <sys/mman.h>
#include <sys/signal.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <string.h>
#include "simutil.h"
#include <setjmp.h>
#include "sim.h"
#include "simtypes.h"
#include "syslimits.h"
#include "arch_specifics.h"
#include "machine_params.h"
#include "cpu_interface.h"
#include "sim_error.h"
#include "gdb_interface.h"
static int sfd; /* Open socket for connection */
static int rfd = -1; /* Open fd for connection */
static int debug_from_poll = 0; /* we entered the debugger because the
* poll on the socket showed something
*/
static int remote_debug = 0;
static int AttachGdb(int sfd, int fdactive);
static int read_cpu(char* buf);
static int fromhex(int);
static int tohex(int);
static int putpkt(int fd, char *buf);
static int readchar(int fd);
static int getpkt(int fd, char *buf);
static void write_ok(char *buf);
static void write_enn(char *buf);
static void convert_int_to_ascii(char *from, char *to, int n);
static void convert_ascii_to_int(char *from, char *to, int n);
static int read_memory(char* buf, VA memaddr, unsigned int len, uint cpuno);
static int write_memory(char* buf, VA memaddr, unsigned int len, uint cpuno);
static char* findchar(char* s, int c);
static void prepare_resume_reply(char *buf, unsigned int sig, unsigned int cpuno);
int DebugPort; /* default value */
#ifdef __alpha
#define MEMORY_SCANF "%x,%lx,%x"
#else
#define MEMORY_SCANF "%x,%llx,%x"
#endif
/*****************************************************************
* Simdebug_init
*
* Initialize the debugger c module.
*****************************************************************/
void
Simdebug_init(void)
{
struct sockaddr_in sockaddr;
int tmp;
if (!DebugPort) {
return;
}
sfd = socket (PF_INET, SOCK_STREAM, 0);
if (sfd < 0) {
CPUWarning("Can't open debug socket");
ASSERT(0);
return;
}
/* Allow rapid reuse of this port. */
tmp = 1;
if (setsockopt (sfd, SOL_SOCKET, SO_REUSEADDR, (char *)&tmp, sizeof(tmp)) < 0) {
CPUWarning("simdebug setsockopt SO_REUSEADDR");
/* Not fatal */
}
bzero((char *)&sockaddr, sizeof(struct sockaddr_in));
sockaddr.sin_family = PF_INET;
sockaddr.sin_port = htons(DebugPort);
sockaddr.sin_addr.s_addr = INADDR_ANY;
while (bind (sfd, (struct sockaddr *)&sockaddr, sizeof (sockaddr)) || listen (sfd, 1)) {
DebugPort++;
sockaddr.sin_port = htons(DebugPort);
}
}
/****************************************************************
* AttachGdb
*
****************************************************************/
static int
AttachGdb(int fd, int fdactive)
{
struct sockaddr_in sockaddr;
int tmp;
int gdb_fd;
if (!fdactive)
CPUWarning("\nEnter debugger, port = %d, waiting...\n ", DebugPort);
tmp = sizeof (sockaddr);
gdb_fd = accept (fd, (struct sockaddr *)&sockaddr, &tmp);
if (gdb_fd == -1) {
CPUWarning("Debugger accept failed\n");
return gdb_fd;
}
if (!fdactive)
CPUWarning("attached\n");
/* Tell TCP not to delay small packets. This greatly speeds up
interactive response. */
tmp = 1;
setsockopt (gdb_fd, IPPROTO_TCP, TCP_NODELAY,
(char *)&tmp, sizeof(tmp));
return gdb_fd;
}
/*****************************************************************
* Simdebug_pollfd and Simdebug_fdactive
*
* someone needs to poll the gdb connection if it is open in case
* the user types ^C. This is currently piggy-backed onto the
* select polling done by the Visual control subsystem
* (see visual.c::VisualControl).
*
* when the gdb connection is closed, we poll the listener fd (sfd)
* so a gdb attach request causes us to enter the debugger immediately.
*
*****************************************************************/
int
Simdebug_pollfd(void)
{
if (rfd != -1) {
return rfd;
} else {
return sfd;
}
}
void Simdebug_fdactive(void)
{
/* gdb sent us something. This won't get called in the
* middle of Simdebug_run because the debug run loop owns
* the system during that time, so the visual callback won't
* happen.
*
* That's good, because on embra this passes through a few
* functions and calls right into simdebug_run.
*/
if (remote_debug) {
CPUWarning("Simdebug_fdactive: poll saw data on debugger port\n");
}
debug_from_poll = 1;
CPUVec.Handle_Debug_Signal(-1, 0); /* -1 => any cpu, 0 => this
* is a direct debug request,
* not a signal that should
* fire the sigusr annotation
*/
}
static int CanSingleStep(CPUType cpuType)
{
#if defined(SIM_MIPS32) || defined(SIM_MIPS64)
return (cpuType == MIPSY);
#elif defined(SIM_X86)
return (cpuType == X86SIM);
#else
return 0;
#endif
}
/*****************************************************************
* Simdebug_run
*
* Main call to handle the simulator side of a gdb session.
*****************************************************************/
Simdebug_result
Simdebug_run(int sig, int start_cpuno)
{
char *b;
static char cmd_buf[BUFSIZ+1];
int i = 0;
Simdebug_result ret = SD_CONTINUE;
int pid;
unsigned int cpuno = 0; /* current cpu number */
if (remote_debug) {
CPUWarning("Simdebug_run: sig %d, start_cpuno %d\n", sig, start_cpuno);
}
if (rfd <= 0) {
/* establishing a new connection. GDB does not expect to see
* a reason-for-stop packet 'S' in this initial setup transaction.
*/
rfd = AttachGdb(sfd, debug_from_poll);
if (rfd < 0) {
return SD_CONTINUE; /* could not get debugger */
}
debug_from_poll = 0; /* whether or not we entered from poll doesn't
* matter this time, but we'd better reset it
* before next time this function runs
*/
} else {
/* reestablish an old connection. We might get here either
* for internal reasons (hitting a breakpoint) or for external
* reasons (gdb sent us a ^C). If external, we need to swallow
* the break packet. Then in either case, let GDB know why
* we stopped.
*/
if (debug_from_poll) {
debug_from_poll = 0;
if (getpkt (rfd, cmd_buf) < 0) goto errout;
/* the only thing we expect to see when polling is the
* break instruction
*/
if (cmd_buf[0] != 'b') {
CPUWarning("Simdebug_run: unexpected command from gdb, expected ^C\n");
}
/* make gdb happy: it sent a ^C so it should get one back */
sig = SIGINT;
}
/* Inform the remote debugger we have entered debugging mode. */
sprintf (cmd_buf, "S%02x%02x", sig, start_cpuno);
if (putpkt (rfd, cmd_buf) < 0) goto errout;
}
while (1) {
char ch;
i = 0;
if (getpkt (rfd, cmd_buf) < 0) goto errout;
#if 0
fprintf(stderr, "GETPKT %s\r\n", cmd_buf);
#endif
if (remote_debug) {
CPUWarning( "GETPKT: %s\n", cmd_buf);
}
ch = cmd_buf[i++];
SIM_DEBUG(('g', "gdb command %c \n",ch));
switch (ch) {
case '?':
prepare_resume_reply (cmd_buf, sig, start_cpuno);
break;
case 'b':
/* nop: this is the packet sent when the user types ^C. No
* effect during main loop
*/
write_enn (cmd_buf);
break;
case 'g': {
int i;
cpuno = read_cpu(cmd_buf+1);
if (cpuno >= TOTAL_CPUS) {
write_enn (cmd_buf);
} else {
Reg r=0;
b = cmd_buf;
for (i = 0; i < GDB_NUM_REGS; i++) {
if (CPUVec.GetRegister(cpuno, i, &r) == FAILURE) {
r = 0;
}
/*
* flip the bytes around
*/
#if defined(SIM_ALPHA) || defined(SIM_MIPS64)
/*
* bugnion:
* 64 bit support
* actually, the ifdef should be based on which remote gdb we
* are talking to. Maybe a good thing to have an exchange protocol
* at startup to make sure that gdb is not going to be confused
*/
r = HO2MO_8(r);
convert_int_to_ascii ((char *) &r, b, 4*2);
b += 8*2;
#else
r = HO2MO_4((int)r);
convert_int_to_ascii ((char *) &r, b, 4);
b += 8;
#endif
}
*b = 0;
}
break;
}
case 'G': {
cpuno = read_cpu(cmd_buf+1);
if (cpuno >= TOTAL_CPUS) {
write_enn (cmd_buf);
} else {
int succeeded = 1;
b = cmd_buf + 3;
for (i = 0; i < GDB_NUM_REGS; i++) {
unsigned int reg = 0;
#if defined(SIM_ALPHA) || defined(SIM_MIPS64)
convert_ascii_to_int (b, (char *) ®, 4*2);
reg = MO2HO_8(reg);
if (CPUVec.PutRegister(cpuno, i, reg) == FAILURE) {
}
b += 8*2;
#else
convert_ascii_to_int (b, (char *) ®, 4);
reg = MO2HO_4(reg);
if (CPUVec.PutRegister(cpuno, i, (Reg)(Reg32_s)reg) == FAILURE) {
}
b += 8;
#endif
}
if (succeeded) {
write_ok (cmd_buf);
} else {
write_enn (cmd_buf);
}
}
break;
}
case 'c':
ret = SD_CONTINUE;
goto out;
case 'C':
close(rfd);
rfd = -1;
ret = SD_CONTINUE;
goto out;
case 's':
case 'S':
cpuno = read_cpu(cmd_buf+1);
if (cpuno < TOTAL_CPUS && CanSingleStep(simosCPUType)) {
ret = (ch == 'S') ? SD_NEXTI_ANYCPU : (Simdebug_result) cpuno;
} else {
CPUWarning("Single-step request for bad cpu or SS not implemented, continuing...\n");
ret = SD_CONTINUE;
}
goto out;
case 'k':
exit(0);
break;
case 'p':
pid = (int)getpid();
sprintf(cmd_buf, "%8u,%02x", pid, TOTAL_CPUS);
break;
case 'm': {
unsigned int cno;
Reg64 memaddr;
unsigned int len;
if (sscanf(cmd_buf+1, MEMORY_SCANF,&cno,&memaddr,&len) != 3) {
write_enn (cmd_buf);
} else {
if (read_memory(cmd_buf, _SEXT(memaddr), len, cno)) {
write_enn (cmd_buf);
}
}
break;
}
case 'M': {
unsigned int cno;
Reg64 memaddr;
unsigned int len;
char* spkt = findchar(cmd_buf, ':');
if ((*spkt != ':')
|| (sscanf(cmd_buf+1, MEMORY_SCANF,&cno,&memaddr,&len) != 3)
|| write_memory(spkt+1, _SEXT(memaddr), len, cno)) {
write_enn (cmd_buf);
} else {
write_ok (cmd_buf);
}
break;
}
default:
write_enn (cmd_buf);
break;
}
if (putpkt (rfd, cmd_buf) < 0) goto errout;
}
out:
return ret;
errout:
/* the connection was terminated prematurely. Reset */
close(rfd);
rfd = -1;
CPUWarning("Remote debugger connection lost, continuing...\n");
return SD_CONTINUE;
}
static int
read_cpu(char* buf)
{
return (fromhex (buf[0]) << 4) + fromhex (buf[1]);
}
#define MAX_XFER (8*1024)
static int
read_memory(char* buf, VA memaddr, unsigned int len, unsigned int cpuno)
{
char readout_buf[MAX_XFER];
ASSERT( cpuno < TOTAL_CPUS);
if (len > MAX_XFER) {
CPUError("simdebug.c::read_memory: too many bytes requested");
}
if (CPUVec.GetMemory(cpuno, memaddr, len, readout_buf) == SUCCESS) {
convert_int_to_ascii(readout_buf, buf, len);
return 0;
}
return 1;
}
static int
write_memory(char* buf, VA memaddr, unsigned int len, unsigned int cpuno)
{
char readout_buf[MAX_XFER];
ASSERT( cpuno < TOTAL_CPUS);
if (len > MAX_XFER) {
CPUError("simdebug.c::write_memory: too many bytes requested");
}
convert_ascii_to_int(buf, readout_buf, len);
if (CPUVec.PutMemory(cpuno, memaddr, len, readout_buf) == SUCCESS) {
return 0;
}
return 1;
}
/* Convert hex digit A to a number. */
static int
fromhex (int a)
{
if (a >= '0' && a <= '9')
return a - '0';
else if (a >= 'a' && a <= 'f')
return a - 'a' + 10;
else {
CPUError ("Request contains invalid hex digit");
return 0;
}
}
/* Convert number NIB to a hex digit. */
static int
tohex (int nib)
{
if (nib < 10)
return '0' + nib;
else
return 'a' + nib - 10;
}
/* Send a packet to the remote machine, with error checking.
The data of the packet is in BUF. Returns >= 0 on success, -1 otherwise. */
static int
putpkt (int fd, char *buf)
{
int i;
unsigned char csum = 0;
char buf2[2000];
char buf3[1];
int cnt = strlen (buf);
char *p;
/* Copy the packet into buffer BUF2, encapsulating it
and giving it a checksum. */
p = buf2;
*p++ = '$';
for (i = 0; i < cnt; i++) {
csum += buf[i];
*p++ = buf[i];
}
*p++ = '#';
*p++ = tohex ((csum >> 4) & 0xf);
*p++ = tohex (csum & 0xf);
/* Send it over and over until we get a positive ack. */
do {
int cc;
#if 0
fprintf(stderr, "PUTPKT %s\r\n", buf);
#endif
if (remote_debug)
CPUWarning( "PUTPKT: %s\n", buf);
if (write (fd, buf2, p - buf2) != p - buf2) {
CPUWarning("putpkt(write)");
goto errout;
}
cc = read (fd, buf3, 1);
if (cc <= 0) {
CPUWarning("putpkt(read)");
goto errout;
}
} while (buf3[0] != '+');
return 1; /* Success! */
errout:
return -1;
}
/*****************************************************************
* readchar
* Returns next char from remote GDB. -1 if error.
*****************************************************************/
static int
readchar (int fd)
{
static char buf[BUFSIZ];
static int bufcnt = 0;
static char *bufp;
if (bufcnt-- > 0)
return *bufp++ & 0x7f;
bufcnt = read (fd, buf, BUFSIZ);
if (bufcnt <= 0) {
CPUWarning("Simdebug readchar");
return -1;
}
bufp = buf;
bufcnt--;
return *bufp++ & 0x7f;
}
/*****************************************************************
* getpkt
*
* Read a packet from the remote machine, with error checking,
* and store it in BUF. Returns length of packet, or negative if error.
*****************************************************************/
static int
getpkt (int fd, char *buf)
{
char *bp;
unsigned char csum, c1, c2;
int c;
bp = buf;
while (1) {
csum = 0;
while (1) {
c = readchar (fd);
if (c == '$')
break;
if (c < 0)
goto errout;
}
bp = buf;
while (1) {
c = readchar (fd);
if (c < 0)
goto errout;
if (c == '#')
break;
*bp++ = c;
csum += c;
}
*bp = 0;
c = readchar (fd);
if (c < 0) goto errout;
c1 = fromhex (c);
c = readchar (fd);
if (c < 0) goto errout;
c2 = fromhex (c);
if (csum == (c1 << 4) + c2)
break;
write (fd, "-", 1);
}
write (fd, "+", 1);
return bp - buf;
errout:
return -1;
}
static void
write_ok (char *buf)
{
buf[0] = 'O';
buf[1] = 'k';
buf[2] = '\0';
}
static void
write_enn (char *buf)
{
buf[0] = 'E';
buf[1] = 'N';
buf[2] = 'N';
buf[3] = '\0';
}
static void
convert_int_to_ascii (char *from, char *to, int n)
{
int nib;
char ch;
while (n--) {
ch = *from++;
nib = ((ch & 0xf0) >> 4) & 0x0f;
*to++ = tohex (nib);
nib = ch & 0x0f;
*to++ = tohex (nib);
}
*to++ = 0;
}
static void
convert_ascii_to_int (char *from, char *to, int n)
{
int nib1, nib2;
while (n--) {
nib1 = fromhex (*from++);
nib2 = fromhex (*from++);
*to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f);
}
}
static char*
findchar(char* s, int c)
{
int c1;
while (1) {
c1 = *s;
if ((c1 == c) || (! c1))
return s;
s ++;
}
/* JeffG: This never gets executed (assuming s is NULL-terminated),
but this is necessary to get it to compile without warnings */
return NULL;
}
static void
prepare_resume_reply (char *buf,unsigned int sig, unsigned int cpuno)
{
sprintf(buf, "S%02x%02x", sig, cpuno);
}