kgdb_stub.c 12 KB

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