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