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