kgdb_stub.c revision 1.6.12.1 1 /* $NetBSD: kgdb_stub.c,v 1.6.12.1 2000/11/20 18:09:06 bouyer Exp $ */
2
3 /*
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 * This product includes software developed by the University of
14 * California, Lawrence Berkeley Laboratories.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 *
44 * @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
45 */
46
47 /*
48 * "Stub" to allow remote cpu to debug over a serial line using gdb.
49 */
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kgdb.h>
54
55 /* #define DEBUG_KGDB XXX */
56
57 /* XXX: Maybe these should be in the MD files? */
58 #ifndef KGDBDEV
59 #define KGDBDEV -1
60 #endif
61 #ifndef KGDBRATE
62 #define KGDBRATE 19200
63 #endif
64
65 int kgdb_dev = KGDBDEV; /* remote debugging device (-1 if none) */
66 int kgdb_rate = KGDBRATE; /* remote debugging baud rate */
67 int kgdb_active = 0; /* remote debugging active if != 0 */
68 int kgdb_debug_init = 0; /* != 0 waits for remote at system init */
69 int kgdb_debug_panic = 0; /* != 0 waits for remote on panic */
70 label_t *kgdb_recover = 0;
71
72 static void kgdb_copy __P((void *, void *, int));
73 /* static void kgdb_zero __P((void *, int)); */
74 static void kgdb_send __P((u_char *));
75 static int kgdb_recv __P((u_char *, int));
76 static int digit2i __P((u_char));
77 static u_char i2digit __P((int));
78 static void mem2hex __P((void *, void *, int));
79 static u_char *hex2mem __P((void *, u_char *, int));
80 static vaddr_t hex2i __P((u_char **));
81
82 static int (*kgdb_getc) __P((void *));
83 static void (*kgdb_putc) __P((void *, int));
84 static void *kgdb_ioarg;
85
86 static u_char buffer[KGDB_BUFLEN];
87 static kgdb_reg_t gdb_regs[KGDB_NUMREGS];
88
89 #define GETC() ((*kgdb_getc)(kgdb_ioarg))
90 #define PUTC(c) ((*kgdb_putc)(kgdb_ioarg, c))
91
92 /*
93 * db_trap_callback can be hooked by MD port code to handle special
94 * cases such as disabling hardware watchdogs while in kgdb. Name
95 * is shared with DDB.
96 */
97 void (*db_trap_callback)(int);
98
99 /*
100 * This little routine exists simply so that bcopy() can be debugged.
101 */
102 static void
103 kgdb_copy(vsrc, vdst, len)
104 void *vsrc, *vdst;
105 int len;
106 {
107 char *src = vsrc;
108 char *dst = vdst;
109
110 while (--len >= 0)
111 *dst++ = *src++;
112 }
113
114 #if 0
115 /* ditto for bzero */
116 static void
117 kgdb_zero(vptr, len)
118 void *vptr;
119 int len;
120 {
121 char *ptr = vptr;
122
123 while (--len >= 0)
124 *ptr++ = (char) 0;
125 }
126 #endif
127
128 /*
129 * Convert a hex digit into an integer.
130 * This returns -1 if the argument passed is no
131 * valid hex digit.
132 */
133 static int
134 digit2i(c)
135 u_char c;
136 {
137 if (c >= '0' && c <= '9')
138 return (c - '0');
139 else if (c >= 'a' && c <= 'f')
140 return (c - 'a' + 10);
141 else if (c >= 'A' && c <= 'F')
142
143 return (c - 'A' + 10);
144 else
145 return (-1);
146 }
147
148 /*
149 * Convert the low 4 bits of an integer into
150 * an hex digit.
151 */
152 static u_char
153 i2digit(n)
154 int n;
155 {
156 return ("0123456789abcdef"[n & 0x0f]);
157 }
158
159 /*
160 * Convert a byte array into an hex string.
161 */
162 static void
163 mem2hex(vdst, vsrc, len)
164 void *vdst, *vsrc;
165 int len;
166 {
167 u_char *dst = vdst;
168 u_char *src = vsrc;
169
170 while (len--) {
171 *dst++ = i2digit(*src >> 4);
172 *dst++ = i2digit(*src++);
173 }
174 *dst = '\0';
175 }
176
177 /*
178 * Convert an hex string into a byte array.
179 * This returns a pointer to the character following
180 * the last valid hex digit. If the string ends in
181 * the middle of a byte, NULL is returned.
182 */
183 static u_char *
184 hex2mem(vdst, src, maxlen)
185 void *vdst;
186 u_char *src;
187 int maxlen;
188 {
189 u_char *dst = vdst;
190 int msb, lsb;
191
192 while (*src && maxlen--) {
193 msb = digit2i(*src++);
194 if (msb < 0)
195 return (src - 1);
196 lsb = digit2i(*src++);
197 if (lsb < 0)
198 return (NULL);
199 *dst++ = (msb << 4) | lsb;
200 }
201 return (src);
202 }
203
204 /*
205 * Convert an hex string into an integer.
206 * This returns a pointer to the character following
207 * the last valid hex digit.
208 */
209 static vaddr_t
210 hex2i(srcp)
211 u_char **srcp;
212 {
213 char *src = *srcp;
214 vaddr_t r = 0;
215 int nibble;
216
217 while ((nibble = digit2i(*src)) >= 0) {
218 r *= 16;
219 r += nibble;
220 src++;
221 }
222 *srcp = src;
223 return (r);
224 }
225
226 /*
227 * Send a packet.
228 */
229 static void
230 kgdb_send(bp)
231 u_char *bp;
232 {
233 u_char *p;
234 u_char csum, c;
235
236 #ifdef DEBUG_KGDB
237 printf("kgdb_send: %s\n", bp);
238 #endif
239 do {
240 p = bp;
241 PUTC(KGDB_START);
242 for (csum = 0; (c = *p); p++) {
243 PUTC(c);
244 csum += c;
245 }
246 PUTC(KGDB_END);
247 PUTC(i2digit(csum >> 4));
248 PUTC(i2digit(csum));
249 } while ((c = GETC() & 0x7f) == KGDB_BADP);
250 }
251
252 /*
253 * Receive a packet.
254 */
255 static int
256 kgdb_recv(bp, maxlen)
257 u_char *bp;
258 int maxlen;
259 {
260 u_char *p;
261 int c, csum;
262 int len;
263
264 do {
265 p = bp;
266 csum = len = 0;
267 while ((c = GETC()) != KGDB_START)
268 ;
269
270 while ((c = GETC()) != KGDB_END && len < maxlen) {
271 c &= 0x7f;
272 csum += c;
273 *p++ = c;
274 len++;
275 }
276 csum &= 0xff;
277 *p = '\0';
278
279 if (len >= maxlen) {
280 PUTC(KGDB_BADP);
281 continue;
282 }
283
284 csum -= digit2i(GETC()) * 16;
285 csum -= digit2i(GETC());
286
287 if (csum == 0) {
288 PUTC(KGDB_GOODP);
289 /* Sequence present? */
290 if (bp[2] == ':') {
291 PUTC(bp[0]);
292 PUTC(bp[1]);
293 len -= 3;
294 kgdb_copy(bp + 3, bp, len);
295 }
296 break;
297 }
298 PUTC(KGDB_BADP);
299 } while (1);
300 #ifdef DEBUG_KGDB
301 printf("kgdb_recv: %s\n", bp);
302 #endif
303 return (len);
304 }
305
306 /*
307 * This is called by the appropriate tty driver.
308 */
309 void
310 kgdb_attach(getfn, putfn, ioarg)
311 int (*getfn) __P((void *));
312 void (*putfn) __P((void *, int));
313 void *ioarg;
314 {
315 kgdb_getc = getfn;
316 kgdb_putc = putfn;
317 kgdb_ioarg = ioarg;
318 }
319
320 /*
321 * This function does all command processing for interfacing to
322 * a remote gdb. Note that the error codes are ignored by gdb
323 * at present, but might eventually become meaningful. (XXX)
324 * It might makes sense to use POSIX errno values, because
325 * that is what the gdb/remote.c functions want to return.
326 */
327 int
328 kgdb_trap(type, regs)
329 int type;
330 db_regs_t *regs;
331 {
332 label_t jmpbuf;
333 vaddr_t addr;
334 size_t len;
335 u_char *p;
336
337 if (kgdb_dev < 0 || kgdb_getc == NULL) {
338 /* not debugging */
339 return (0);
340 }
341
342 if (db_trap_callback) db_trap_callback(1);
343
344 /* Detect and recover from unexpected traps. */
345 if (kgdb_recover != 0) {
346 printf("kgdb: caught trap 0x%x at %p\n",
347 type, (void*)PC_REGS(regs));
348 kgdb_send("E0E"); /* 14==EFAULT */
349 longjmp(kgdb_recover);
350 }
351
352 /*
353 * The first entry to this function is normally through
354 * a breakpoint trap in kgdb_connect(), in which case we
355 * must advance past the breakpoint because gdb will not.
356 *
357 * Machines vary as to where they leave the PC after a
358 * breakpoint trap. Those that leave the PC set to the
359 * address of the trap instruction (i.e. pc532) will not
360 * define FIXUP_PC_AFTER_BREAK(), and therefore will just
361 * advance the PC. On machines that leave the PC set to
362 * the instruction after the trap, FIXUP_PC_AFTER_BREAK
363 * will be defined to back-up the PC, so that after the
364 * "first-time" part of the if statement below has run,
365 * the PC will be the same as it was on entry.
366 *
367 * On the first entry here, we expect that gdb is not yet
368 * listening to us, so just enter the interaction loop.
369 * After the debugger is "active" (connected) it will be
370 * waiting for a "signaled" message from us.
371 */
372 if (kgdb_active == 0) {
373 if (!IS_BREAKPOINT_TRAP(type, 0)) {
374 /* No debugger active -- let trap handle this. */
375 if (db_trap_callback) db_trap_callback(0);
376 return (0);
377 }
378 /* Make the PC point at the breakpoint... */
379 #ifdef FIXUP_PC_AFTER_BREAK
380 FIXUP_PC_AFTER_BREAK(regs);
381 #endif
382 /* ... and then advance past it. */
383 #ifdef PC_ADVANCE
384 PC_ADVANCE(regs);
385 #else
386 PC_REGS(regs) += BKPT_SIZE;
387 #endif
388 kgdb_active = 1;
389 } else {
390 /* Tell remote host that an exception has occured. */
391 sprintf(buffer, "S%02x", kgdb_signal(type));
392 kgdb_send(buffer);
393 }
394
395 /* Stick frame regs into our reg cache. */
396 kgdb_getregs(regs, gdb_regs);
397
398 /*
399 * Interact with gdb until it lets us go.
400 * If we cause a trap, resume here.
401 */
402 (void)setjmp((kgdb_recover = &jmpbuf));
403 for (;;) {
404 kgdb_recv(buffer, sizeof(buffer));
405 switch (buffer[0]) {
406
407 default:
408 /* Unknown command. */
409 kgdb_send("");
410 continue;
411
412 case KGDB_SIGNAL:
413 /*
414 * if this command came from a running gdb,
415 * answer it -- the other guy has no way of
416 * knowing if we're in or out of this loop
417 * when he issues a "remote-signal".
418 */
419 sprintf(buffer, "S%02x", kgdb_signal(type));
420 kgdb_send(buffer);
421 continue;
422
423 case KGDB_REG_R:
424 mem2hex(buffer, gdb_regs, sizeof(gdb_regs));
425 kgdb_send(buffer);
426 continue;
427
428 case KGDB_REG_W:
429 p = hex2mem(gdb_regs, buffer + 1, sizeof(gdb_regs));
430 if (p == NULL || *p != '\0')
431 kgdb_send("E01");
432 else {
433 kgdb_setregs(regs, gdb_regs);
434 kgdb_send("OK");
435 }
436 continue;
437
438 case KGDB_MEM_R:
439 p = buffer + 1;
440 addr = hex2i(&p);
441 if (*p++ != ',') {
442 kgdb_send("E02");
443 continue;
444 }
445 len = hex2i(&p);
446 if (*p != '\0') {
447 kgdb_send("E03");
448 continue;
449 }
450 if (len > sizeof(buffer) / 2) {
451 kgdb_send("E04");
452 continue;
453 }
454 if (kgdb_acc(addr, len) == 0) {
455 kgdb_send("E05");
456 continue;
457 }
458 db_read_bytes(addr, (size_t)len,
459 (char *)buffer + sizeof(buffer) / 2);
460 mem2hex(buffer, buffer + sizeof(buffer) / 2, len);
461 kgdb_send(buffer);
462 continue;
463
464 case KGDB_MEM_W:
465 p = buffer + 1;
466 addr = hex2i(&p);
467 if (*p++ != ',') {
468 kgdb_send("E06");
469 continue;
470 }
471 len = hex2i(&p);
472 if (*p++ != ':') {
473 kgdb_send("E07");
474 continue;
475 }
476 if (len > (sizeof(buffer) - (p - buffer))) {
477 kgdb_send("E08");
478 continue;
479 }
480 p = hex2mem(buffer, p, sizeof(buffer));
481 if (p == NULL) {
482 kgdb_send("E09");
483 continue;
484 }
485 if (kgdb_acc(addr, len) == 0) {
486 kgdb_send("E0A");
487 continue;
488 }
489 db_write_bytes(addr, (size_t)len, (char *)buffer);
490 kgdb_send("OK");
491 continue;
492
493 case KGDB_KILL:
494 kgdb_active = 0;
495 printf("kgdb detached\n");
496 db_clear_single_step(regs);
497 goto out;
498
499 case KGDB_CONT:
500 if (buffer[1]) {
501 p = buffer + 1;
502 addr = hex2i(&p);
503 if (*p) {
504 kgdb_send("E0B");
505 continue;
506 }
507 PC_REGS(regs) = addr;
508 }
509 db_clear_single_step(regs);
510 goto out;
511
512 case KGDB_STEP:
513 if (buffer[1]) {
514 p = buffer + 1;
515 addr = hex2i(&p);
516 if (*p) {
517 kgdb_send("E0B");
518 continue;
519 }
520 PC_REGS(regs) = addr;
521 }
522 db_set_single_step(regs);
523 goto out;
524 }
525 }
526 out:
527 if (db_trap_callback) db_trap_callback(0);
528 kgdb_recover = 0;
529 return (1);
530 }
531