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
|
/* $OpenBSD: kgdb_stub.c,v 1.5 2003/05/06 07:28:38 deraadt Exp $ */
/* $NetBSD: kgdb_stub.c,v 1.6 1998/08/30 20:30:57 scottr Exp $ */
/*
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
*
* All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Lawrence Berkeley Laboratories.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
*/
/*
* "Stub" to allow remote cpu to debug over a serial line using gdb.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kgdb.h>
/* #define DEBUG_KGDB XXX */
/* XXX: Maybe these should be in the MD files? */
#ifndef KGDBDEV
#define KGDBDEV -1
#endif
#ifndef KGDBRATE
#define KGDBRATE 19200
#endif
int kgdb_dev = KGDBDEV; /* remote debugging device (-1 if none) */
int kgdb_rate = KGDBRATE; /* remote debugging baud rate */
int kgdb_active = 0; /* remote debugging active if != 0 */
int kgdb_debug_init = 0; /* != 0 waits for remote at system init */
int kgdb_debug_panic = 0; /* != 0 waits for remote on panic */
label_t *kgdb_recover = 0;
static void kgdb_copy(void *, void *, int);
/* static void kgdb_zero(void *, int); */
static void kgdb_send(u_char *);
static int kgdb_recv(u_char *, int);
static int digit2i(u_char);
static u_char i2digit(int);
static void mem2hex(void *, void *, int);
static u_char *hex2mem(void *, u_char *, int);
static vaddr_t hex2i(u_char **);
static int (*kgdb_getc)(void *);
static void (*kgdb_putc)(void *, int);
static void *kgdb_ioarg;
static u_char buffer[KGDB_BUFLEN];
static kgdb_reg_t gdb_regs[KGDB_NUMREGS];
#define GETC() ((*kgdb_getc)(kgdb_ioarg))
#define PUTC(c) ((*kgdb_putc)(kgdb_ioarg, c))
/*
* This little routine exists simply so that bcopy() can be debugged.
*/
static void
kgdb_copy(vsrc, vdst, len)
void *vsrc, *vdst;
int len;
{
char *src = vsrc;
char *dst = vdst;
while (--len >= 0)
*dst++ = *src++;
}
#if 0
/* ditto for bzero */
static void
kgdb_zero(vptr, len)
void *vptr;
int len;
{
char *ptr = vptr;
while (--len >= 0)
*ptr++ = (char) 0;
}
#endif
/*
* Convert a hex digit into an integer.
* This returns -1 if the argument passed is no
* valid hex digit.
*/
static int
digit2i(c)
u_char c;
{
if (c >= '0' && c <= '9')
return (c - '0');
else if (c >= 'a' && c <= 'f')
return (c - 'a' + 10);
else if (c >= 'A' && c <= 'F')
return (c - 'A' + 10);
else
return (-1);
}
/*
* Convert the low 4 bits of an integer into
* an hex digit.
*/
static u_char
i2digit(n)
int n;
{
return ("0123456789abcdef"[n & 0x0f]);
}
/*
* Convert a byte array into an hex string.
*/
static void
mem2hex(vdst, vsrc, len)
void *vdst, *vsrc;
int len;
{
u_char *dst = vdst;
u_char *src = vsrc;
while (len--) {
*dst++ = i2digit(*src >> 4);
*dst++ = i2digit(*src++);
}
*dst = '\0';
}
/*
* Convert an hex string into a byte array.
* This returns a pointer to the character following
* the last valid hex digit. If the string ends in
* the middle of a byte, NULL is returned.
*/
static u_char *
hex2mem(vdst, src, maxlen)
void *vdst;
u_char *src;
int maxlen;
{
u_char *dst = vdst;
int msb, lsb;
while (*src && maxlen--) {
msb = digit2i(*src++);
if (msb < 0)
return (src - 1);
lsb = digit2i(*src++);
if (lsb < 0)
return (NULL);
*dst++ = (msb << 4) | lsb;
}
return (src);
}
/*
* Convert an hex string into an integer.
* This returns a pointer to the character following
* the last valid hex digit.
*/
static vaddr_t
hex2i(srcp)
u_char **srcp;
{
char *src = *srcp;
vaddr_t r = 0;
int nibble;
while ((nibble = digit2i(*src)) >= 0) {
r *= 16;
r += nibble;
src++;
}
*srcp = src;
return (r);
}
/*
* Send a packet.
*/
static void
kgdb_send(bp)
u_char *bp;
{
u_char *p;
u_char csum, c;
#ifdef DEBUG_KGDB
printf("kgdb_send: %s\n", bp);
#endif
do {
p = bp;
PUTC(KGDB_START);
for (csum = 0; (c = *p); p++) {
PUTC(c);
csum += c;
}
PUTC(KGDB_END);
PUTC(i2digit(csum >> 4));
PUTC(i2digit(csum));
} while ((c = GETC() & 0x7f) == KGDB_BADP);
}
/*
* Receive a packet.
*/
static int
kgdb_recv(bp, maxlen)
u_char *bp;
int maxlen;
{
u_char *p;
int c, csum;
int len;
do {
p = bp;
csum = len = 0;
while ((c = GETC()) != KGDB_START)
;
while ((c = GETC()) != KGDB_END && len < maxlen) {
c &= 0x7f;
csum += c;
*p++ = c;
len++;
}
csum &= 0xff;
*p = '\0';
if (len >= maxlen) {
PUTC(KGDB_BADP);
continue;
}
csum -= digit2i(GETC()) * 16;
csum -= digit2i(GETC());
if (csum == 0) {
PUTC(KGDB_GOODP);
/* Sequence present? */
if (bp[2] == ':') {
PUTC(bp[0]);
PUTC(bp[1]);
len -= 3;
kgdb_copy(bp + 3, bp, len);
}
break;
}
PUTC(KGDB_BADP);
} while (1);
#ifdef DEBUG_KGDB
printf("kgdb_recv: %s\n", bp);
#endif
return (len);
}
/*
* This is called by the appropriate tty driver.
*/
void
kgdb_attach(getfn, putfn, ioarg)
int (*getfn)(void *);
void (*putfn)(void *, int);
void *ioarg;
{
kgdb_getc = getfn;
kgdb_putc = putfn;
kgdb_ioarg = ioarg;
}
/*
* This function does all command processing for interfacing to
* a remote gdb. Note that the error codes are ignored by gdb
* at present, but might eventually become meaningful. (XXX)
* It might makes sense to use POSIX errno values, because
* that is what the gdb/remote.c functions want to return.
*/
int
kgdb_trap(type, regs)
int type;
db_regs_t *regs;
{
label_t jmpbuf;
vaddr_t addr;
size_t len;
u_char *p;
if (kgdb_dev < 0 || kgdb_getc == NULL) {
/* not debugging */
return (0);
}
/* Detect and recover from unexpected traps. */
if (kgdb_recover != 0) {
printf("kgdb: caught trap 0x%x at %p\n",
type, (void *)PC_REGS(regs));
kgdb_send("E0E"); /* 14==EFAULT */
longjmp(kgdb_recover);
}
/*
* The first entry to this function is normally through
* a breakpoint trap in kgdb_connect(), in which case we
* must advance past the breakpoint because gdb will not.
*
* Machines vary as to where they leave the PC after a
* breakpoint trap. Those that leave the PC set to the
* address of the trap instruction (i.e. pc532) will not
* define FIXUP_PC_AFTER_BREAK(), and therefore will just
* advance the PC. On machines that leave the PC set to
* the instruction after the trap, FIXUP_PC_AFTER_BREAK
* will be defined to back-up the PC, so that after the
* "first-time" part of the if statement below has run,
* the PC will be the same as it was on entry.
*
* On the first entry here, we expect that gdb is not yet
* listening to us, so just enter the interaction loop.
* After the debugger is "active" (connected) it will be
* waiting for a "signaled" message from us.
*/
if (kgdb_active == 0) {
if (!IS_BREAKPOINT_TRAP(type, 0)) {
/* No debugger active -- let trap handle this. */
return (0);
}
/* Make the PC point at the breakpoint... */
#ifdef FIXUP_PC_AFTER_BREAK
FIXUP_PC_AFTER_BREAK(regs);
#endif
/* ... and then advance past it. */
#ifdef PC_ADVANCE
PC_ADVANCE(regs);
#else
PC_REGS(regs) += BKPT_SIZE;
#endif
kgdb_active = 1;
} else {
/* Tell remote host that an exception has occurred. */
snprintf(buffer, sizeof buffer, "S%02x", kgdb_signal(type));
kgdb_send(buffer);
}
/* Stick frame regs into our reg cache. */
kgdb_getregs(regs, gdb_regs);
/*
* Interact with gdb until it lets us go.
* If we cause a trap, resume here.
*/
(void)setjmp((kgdb_recover = &jmpbuf));
for (;;) {
kgdb_recv(buffer, sizeof(buffer));
switch (buffer[0]) {
default:
/* Unknown command. */
kgdb_send("");
continue;
case KGDB_SIGNAL:
/*
* if this command came from a running gdb,
* answer it -- the other guy has no way of
* knowing if we're in or out of this loop
* when he issues a "remote-signal".
*/
snprintf(buffer, sizeof buffer, "S%02x", kgdb_signal(type));
kgdb_send(buffer);
continue;
case KGDB_REG_R:
mem2hex(buffer, gdb_regs, sizeof(gdb_regs));
kgdb_send(buffer);
continue;
case KGDB_REG_W:
p = hex2mem(gdb_regs, buffer + 1, sizeof(gdb_regs));
if (p == NULL || *p != '\0')
kgdb_send("E01");
else {
kgdb_setregs(regs, gdb_regs);
kgdb_send("OK");
}
continue;
case KGDB_MEM_R:
p = buffer + 1;
addr = hex2i(&p);
if (*p++ != ',') {
kgdb_send("E02");
continue;
}
len = hex2i(&p);
if (*p != '\0') {
kgdb_send("E03");
continue;
}
if (len > sizeof(buffer) / 2) {
kgdb_send("E04");
continue;
}
if (kgdb_acc(addr, len) == 0) {
kgdb_send("E05");
continue;
}
db_read_bytes(addr, (size_t)len,
(char *)buffer + sizeof(buffer) / 2);
mem2hex(buffer, buffer + sizeof(buffer) / 2, len);
kgdb_send(buffer);
continue;
case KGDB_MEM_W:
p = buffer + 1;
addr = hex2i(&p);
if (*p++ != ',') {
kgdb_send("E06");
continue;
}
len = hex2i(&p);
if (*p++ != ':') {
kgdb_send("E07");
continue;
}
if (len > (sizeof(buffer) - (p - buffer))) {
kgdb_send("E08");
continue;
}
p = hex2mem(buffer, p, sizeof(buffer));
if (p == NULL) {
kgdb_send("E09");
continue;
}
if (kgdb_acc(addr, len) == 0) {
kgdb_send("E0A");
continue;
}
db_write_bytes(addr, (size_t)len, (char *)buffer);
kgdb_send("OK");
continue;
case KGDB_DETACH:
kgdb_active = 0;
printf("kgdb detached\n");
db_clear_single_step(regs);
kgdb_send("OK");
goto out;
case KGDB_KILL:
kgdb_active = 0;
printf("kgdb detached\n");
db_clear_single_step(regs);
goto out;
case KGDB_CONT:
if (buffer[1]) {
p = buffer + 1;
addr = hex2i(&p);
if (*p) {
kgdb_send("E0B");
continue;
}
PC_REGS(regs) = addr;
}
db_clear_single_step(regs);
goto out;
case KGDB_STEP:
if (buffer[1]) {
p = buffer + 1;
addr = hex2i(&p);
if (*p) {
kgdb_send("E0B");
continue;
}
PC_REGS(regs) = addr;
}
db_set_single_step(regs);
goto out;
}
}
out:
kgdb_recover = 0;
return (1);
}
|