kgdb_stub.c revision 1.1 1 /* $NetBSD: kgdb_stub.c,v 1.1 1997/02/12 00:51:47 gwr 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 vm_offset_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 #define PUTESC(c) do { \
92 if (c == FRAME_END) { \
93 PUTC(FRAME_ESCAPE); \
94 c = TRANS_FRAME_END; \
95 } else if (c == FRAME_ESCAPE) { \
96 PUTC(FRAME_ESCAPE); \
97 c = TRANS_FRAME_ESCAPE; \
98 } else if (c == FRAME_START) { \
99 PUTC(FRAME_ESCAPE); \
100 c = TRANS_FRAME_START; \
101 } \
102 PUTC(c); \
103 } while (0)
104
105 /*
106 * This little routine exists simply so that bcopy() can be debugged.
107 */
108 static void
109 kgdb_copy(vsrc, vdst, len)
110 void *vsrc, *vdst;
111 int len;
112 {
113 char *src = vsrc;
114 char *dst = vdst;
115
116 while (--len >= 0)
117 *dst++ = *src++;
118 }
119
120 #if 0
121 /* ditto for bzero */
122 static void
123 kgdb_zero(vptr, len)
124 void *vptr;
125 int len;
126 {
127 char *ptr = vptr;
128
129 while (--len >= 0)
130 *ptr++ = (char) 0;
131 }
132 #endif
133
134 /*
135 * Convert a hex digit into an integer.
136 * This returns -1 if the argument passed is no
137 * valid hex digit.
138 */
139 static int
140 digit2i(c)
141 u_char c;
142 {
143 if (c >= '0' && c <= '9')
144 return(c - '0');
145 else if (c >= 'a' && c <= 'f')
146 return(c - 'a' + 10);
147 else if (c >= 'A' && c <= 'F')
148
149 return(c - 'A' + 10);
150 else
151 return(-1);
152 }
153
154 /*
155 * Convert the low 4 bits of an integer into
156 * an hex digit.
157 */
158 static u_char
159 i2digit(n)
160 int n;
161 {
162 return("0123456789abcdef"[n & 0x0f]);
163 }
164
165 /*
166 * Convert a byte array into an hex string.
167 */
168 static void
169 mem2hex(vdst, vsrc, len)
170 void *vdst, *vsrc;
171 int len;
172 {
173 u_char *dst = vdst;
174 u_char *src = vsrc;
175
176 while (len--) {
177 *dst++ = i2digit(*src >> 4);
178 *dst++ = i2digit(*src++);
179 }
180 *dst = '\0';
181 }
182
183 /*
184 * Convert an hex string into a byte array.
185 * This returns a pointer to the character following
186 * the last valid hex digit. If the string ends in
187 * the middle of a byte, NULL is returned.
188 */
189 static u_char *
190 hex2mem(vdst, src, maxlen)
191 void *vdst;
192 u_char *src;
193 int maxlen;
194 {
195 u_char *dst = vdst;
196 int msb, lsb;
197
198 while (*src && maxlen--) {
199 msb = digit2i(*src++);
200 if (msb < 0)
201 return(src - 1);
202 lsb = digit2i(*src++);
203 if (lsb < 0)
204 return(NULL);
205 *dst++ = (msb << 4) | lsb;
206 }
207 return(src);
208 }
209
210 /*
211 * Convert an hex string into an integer.
212 * This returns a pointer to the character following
213 * the last valid hex digit.
214 */
215 static vm_offset_t
216 hex2i(srcp)
217 u_char **srcp;
218 {
219 char *src = *srcp;
220 vm_offset_t r = 0;
221 int nibble;
222
223 while ((nibble = digit2i(*src)) >= 0) {
224 r *= 16;
225 r += nibble;
226 src++;
227 }
228 *srcp = src;
229 return(r);
230 }
231
232 /*
233 * Send a packet.
234 */
235 static void
236 kgdb_send(bp)
237 u_char *bp;
238 {
239 u_char *p;
240 u_char csum, c;
241
242 #ifdef DEBUG_KGDB
243 printf("kgdb_send: %s\n", bp);
244 #endif
245 do {
246 p = bp;
247 PUTC(KGDB_START);
248 for (csum = 0; (c = *p); p++) {
249 PUTC(c);
250 csum += c;
251 }
252 PUTC(KGDB_END);
253 PUTC(i2digit(csum >> 4));
254 PUTC(i2digit(csum));
255 } while ((c = GETC() & 0x7f) == KGDB_BADP);
256 }
257
258 /*
259 * Receive a packet.
260 */
261 static int
262 kgdb_recv(bp, maxlen)
263 u_char *bp;
264 int maxlen;
265 {
266 u_char *p;
267 int c, csum;
268 int len;
269
270 do {
271 p = bp;
272 csum = len = 0;
273 while ((c = GETC()) != KGDB_START)
274 ;
275
276 while ((c = GETC()) != KGDB_END && len < maxlen) {
277 c &= 0x7f;
278 csum += c;
279 *p++ = c;
280 len++;
281 }
282 csum &= 0xff;
283 *p = '\0';
284
285 if (len >= maxlen) {
286 PUTC(KGDB_BADP);
287 continue;
288 }
289
290 csum -= digit2i(GETC()) * 16;
291 csum -= digit2i(GETC());
292
293 if (csum == 0) {
294 PUTC(KGDB_GOODP);
295 /* Sequence present? */
296 if (bp[2] == ':') {
297 PUTC(bp[0]);
298 PUTC(bp[1]);
299 len -= 3;
300 kgdb_copy(bp + 3, bp, len);
301 }
302 break;
303 }
304 PUTC(KGDB_BADP);
305 } while (1);
306 #ifdef DEBUG_KGDB
307 printf("kgdb_recv: %s\n", bp);
308 #endif
309 return(len);
310 }
311
312 /*
313 * This is called by the approprite tty driver.
314 * In our case, by dev/scn.c:scn_kgdb_init()
315 */
316 void
317 kgdb_attach(getfn, putfn, ioarg)
318 int (*getfn) __P((void *));
319 void (*putfn) __P((void *, int));
320 void *ioarg;
321 {
322 kgdb_getc = getfn;
323 kgdb_putc = putfn;
324 kgdb_ioarg = ioarg;
325 }
326
327 /*
328 * This function does all command procesing for interfacing to
329 * a remote gdb. Note that the error codes are ignored by gdb
330 * at present, but might eventually become meaningful. (XXX)
331 * It might makes sense to use POSIX errno values, because
332 * that is what the gdb/remote.c functions want to return.
333 */
334 int
335 kgdb_trap(type, regs)
336 int type;
337 db_regs_t *regs;
338 {
339 label_t jmpbuf;
340 vm_offset_t addr;
341 size_t len;
342 u_char *p;
343
344 if (kgdb_dev < 0 || kgdb_getc == NULL) {
345 /* not debugging */
346 return (0);
347 }
348
349 /* Detect and recover from unexpected traps. */
350 if (kgdb_recover != 0) {
351 printf("kgdb: caught trap 0x%x at %p\n",
352 type, (void*)PC_REGS(regs));
353 kgdb_send("E0E"); /* 14==EFAULT */
354 longjmp(kgdb_recover);
355 }
356
357 /*
358 * On the first entry here, we expect that gdb is not yet
359 * listening to us, so just enter the interaction loop.
360 * After the debugger is "active" (connected) it wil be
361 * waiting for a "signaled" message from us.
362 */
363 if (kgdb_active == 0) {
364 if (!IS_BREAKPOINT_TRAP(type, 0)) {
365 /* No debugger active -- let trap handle this. */
366 return (0);
367 }
368 kgdb_active = 1;
369 } else {
370 /* Tell remote host that an exception has occured. */
371 sprintf(buffer, "S%02x", kgdb_signal(type));
372 kgdb_send(buffer);
373 }
374
375 /* Stick frame regs into our reg cache. */
376 kgdb_getregs(regs, gdb_regs);
377
378 /*
379 * Interact with gdb until it lets us go.
380 * If we cause a trap, resume here.
381 */
382 (void) setjmp((kgdb_recover = &jmpbuf));
383 for (;;) {
384 kgdb_recv(buffer, sizeof(buffer));
385 switch (buffer[0]) {
386
387 default:
388 /* Unknown command. */
389 kgdb_send("");
390 continue;
391
392 case KGDB_SIGNAL:
393 /*
394 * if this command came from a running gdb,
395 * answer it -- the other guy has no way of
396 * knowing if we're in or out of this loop
397 * when he issues a "remote-signal".
398 */
399 sprintf(buffer, "S%02x", kgdb_signal(type));
400 kgdb_send(buffer);
401 continue;
402
403 case KGDB_REG_R:
404 mem2hex(buffer, gdb_regs, sizeof(gdb_regs));
405 kgdb_send(buffer);
406 continue;
407
408 case KGDB_REG_W:
409 p = hex2mem(gdb_regs, buffer + 1, sizeof(gdb_regs));
410 if (p == NULL || *p != '\0')
411 kgdb_send("E01");
412 else {
413 kgdb_setregs(regs, gdb_regs);
414 kgdb_send("OK");
415 }
416 continue;
417
418 case KGDB_MEM_R:
419 p = buffer + 1;
420 addr = hex2i(&p);
421 if (*p++ != ',') {
422 kgdb_send("E02");
423 continue;
424 }
425 len = hex2i(&p);
426 if (*p != '\0') {
427 kgdb_send("E03");
428 continue;
429 }
430 if (len > sizeof(buffer) / 2) {
431 kgdb_send("E04");
432 continue;
433 }
434 if (kgdb_acc(addr, len) == 0) {
435 kgdb_send("E05");
436 continue;
437 }
438 db_read_bytes(addr, (size_t)len,
439 (char *)buffer + sizeof(buffer) / 2);
440 mem2hex(buffer, buffer + sizeof(buffer) / 2, len);
441 kgdb_send(buffer);
442 continue;
443
444 case KGDB_MEM_W:
445 p = buffer + 1;
446 addr = hex2i(&p);
447 if (*p++ != ',') {
448 kgdb_send("E06");
449 continue;
450 }
451 len = hex2i(&p);
452 if (*p++ != ':') {
453 kgdb_send("E07");
454 continue;
455 }
456 if (len > (sizeof(buffer) - (p - buffer))) {
457 kgdb_send("E08");
458 continue;
459 }
460 p = hex2mem(buffer, p, sizeof(buffer));
461 if (p == NULL) {
462 kgdb_send("E09");
463 continue;
464 }
465 if (kgdb_acc(addr, len) == 0) {
466 kgdb_send("E0A");
467 continue;
468 }
469 db_write_bytes(addr, (size_t)len, (char *)buffer);
470 kgdb_send("OK");
471 continue;
472
473 case KGDB_KILL:
474 kgdb_active = 0;
475 printf("kgdb detached\n");
476 db_clear_single_step(regs);
477 goto out;
478
479 case KGDB_CONT:
480 if (buffer[1]) {
481 p = buffer + 1;
482 addr = hex2i(&p);
483 if (*p) {
484 kgdb_send("E0B");
485 continue;
486 }
487 PC_REGS(regs) = addr;
488 }
489 db_clear_single_step(regs);
490 goto out;
491
492 case KGDB_STEP:
493 if (buffer[1]) {
494 p = buffer + 1;
495 addr = hex2i(&p);
496 if (*p) {
497 kgdb_send("E0B");
498 continue;
499 }
500 PC_REGS(regs) = addr;
501 }
502 db_set_single_step(regs);
503 goto out;
504 }
505 }
506 out:
507 kgdb_recover = 0;
508 return (1);
509 }
510