kgdb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /* taken from arch/powerpc/kernel/ppc-stub.c */
  2. /****************************************************************************
  3. THIS SOFTWARE IS NOT COPYRIGHTED
  4. HP offers the following for use in the public domain. HP makes no
  5. warranty with regard to the software or its performance and the
  6. user accepts the software "AS IS" with all faults.
  7. HP DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD
  8. TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  9. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  10. ****************************************************************************/
  11. /****************************************************************************
  12. * Header: remcom.c,v 1.34 91/03/09 12:29:49 glenne Exp $
  13. *
  14. * Module name: remcom.c $
  15. * Revision: 1.34 $
  16. * Date: 91/03/09 12:29:49 $
  17. * Contributor: Lake Stevens Instrument Division$
  18. *
  19. * Description: low level support for gdb debugger. $
  20. *
  21. * Considerations: only works on target hardware $
  22. *
  23. * Written by: Glenn Engel $
  24. * ModuleState: Experimental $
  25. *
  26. * NOTES: See Below $
  27. *
  28. * Modified for SPARC by Stu Grossman, Cygnus Support.
  29. *
  30. * This code has been extensively tested on the Fujitsu SPARClite demo board.
  31. *
  32. * To enable debugger support, two things need to happen. One, a
  33. * call to set_debug_traps() is necessary in order to allow any breakpoints
  34. * or error conditions to be properly intercepted and reported to gdb.
  35. * Two, a breakpoint needs to be generated to begin communication. This
  36. * is most easily accomplished by a call to breakpoint(). Breakpoint()
  37. * simulates a breakpoint by executing a trap #1.
  38. *
  39. *************
  40. *
  41. * The following gdb commands are supported:
  42. *
  43. * command function Return value
  44. *
  45. * g return the value of the CPU registers hex data or ENN
  46. * G set the value of the CPU registers OK or ENN
  47. * qOffsets Get section offsets. Reply is Text=xxx;Data=yyy;Bss=zzz
  48. *
  49. * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
  50. * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
  51. *
  52. * c Resume at current address SNN ( signal NN)
  53. * cAA..AA Continue at address AA..AA SNN
  54. *
  55. * s Step one instruction SNN
  56. * sAA..AA Step one instruction from AA..AA SNN
  57. *
  58. * k kill
  59. *
  60. * ? What was the last sigval ? SNN (signal NN)
  61. *
  62. * bBB..BB Set baud rate to BB..BB OK or BNN, then sets
  63. * baud rate
  64. *
  65. * All commands and responses are sent with a packet which includes a
  66. * checksum. A packet consists of
  67. *
  68. * $<packet info>#<checksum>.
  69. *
  70. * where
  71. * <packet info> :: <characters representing the command or response>
  72. * <checksum> :: <two hex digits computed as modulo 256 sum of <packetinfo>>
  73. *
  74. * When a packet is received, it is first acknowledged with either '+' or '-'.
  75. * '+' indicates a successful transfer. '-' indicates a failed transfer.
  76. *
  77. * Example:
  78. *
  79. * Host: Reply:
  80. * $m0,10#2a +$00010203040506070809101112131415#42
  81. *
  82. ****************************************************************************/
  83. #include <common.h>
  84. #include <asm/ptrace.h>
  85. #include <kgdb.h>
  86. #include <command.h>
  87. #undef KGDB_DEBUG
  88. /*
  89. * BUFMAX defines the maximum number of characters in inbound/outbound buffers
  90. */
  91. #define BUFMAX 1024
  92. static char remcomInBuffer[BUFMAX];
  93. static char remcomOutBuffer[BUFMAX];
  94. static char remcomRegBuffer[BUFMAX];
  95. static int initialized = 0;
  96. static int kgdb_active;
  97. static struct pt_regs entry_regs;
  98. static long error_jmp_buf[BUFMAX/2];
  99. static int longjmp_on_fault = 0;
  100. #ifdef KGDB_DEBUG
  101. static int kdebug = 1;
  102. #endif
  103. static const char hexchars[]="0123456789abcdef";
  104. /* Convert ch from a hex digit to an int */
  105. static int
  106. hex(unsigned char ch)
  107. {
  108. if (ch >= 'a' && ch <= 'f')
  109. return ch-'a'+10;
  110. if (ch >= '0' && ch <= '9')
  111. return ch-'0';
  112. if (ch >= 'A' && ch <= 'F')
  113. return ch-'A'+10;
  114. return -1;
  115. }
  116. /* Convert the memory pointed to by mem into hex, placing result in buf.
  117. * Return a pointer to the last char put in buf (null).
  118. */
  119. static unsigned char *
  120. mem2hex(char *mem, char *buf, int count)
  121. {
  122. char *tmp;
  123. unsigned char ch;
  124. /*
  125. * We use the upper half of buf as an intermediate buffer for the
  126. * raw memory copy. Hex conversion will work against this one.
  127. */
  128. tmp = buf + count;
  129. longjmp_on_fault = 1;
  130. memcpy(tmp, mem, count);
  131. while (count-- > 0) {
  132. ch = *tmp++;
  133. *buf++ = hexchars[ch >> 4];
  134. *buf++ = hexchars[ch & 0xf];
  135. }
  136. *buf = 0;
  137. longjmp_on_fault = 0;
  138. return (unsigned char *)buf;
  139. }
  140. /* convert the hex array pointed to by buf into binary to be placed in mem
  141. * return a pointer to the character AFTER the last byte fetched from buf.
  142. */
  143. static char *
  144. hex2mem(char *buf, char *mem, int count)
  145. {
  146. int hexValue;
  147. char *tmp_raw, *tmp_hex;
  148. /*
  149. * We use the upper half of buf as an intermediate buffer for the
  150. * raw memory that is converted from hex.
  151. */
  152. tmp_raw = buf + count * 2;
  153. tmp_hex = tmp_raw - 1;
  154. longjmp_on_fault = 1;
  155. while (tmp_hex >= buf) {
  156. tmp_raw--;
  157. hexValue = hex(*tmp_hex--);
  158. if (hexValue < 0)
  159. kgdb_error(KGDBERR_NOTHEXDIG);
  160. *tmp_raw = hexValue;
  161. hexValue = hex(*tmp_hex--);
  162. if (hexValue < 0)
  163. kgdb_error(KGDBERR_NOTHEXDIG);
  164. *tmp_raw |= hexValue << 4;
  165. }
  166. memcpy(mem, tmp_raw, count);
  167. kgdb_flush_cache_range((void *)mem, (void *)(mem+count));
  168. longjmp_on_fault = 0;
  169. return buf;
  170. }
  171. /*
  172. * While we find nice hex chars, build an int.
  173. * Return number of chars processed.
  174. */
  175. static int
  176. hexToInt(char **ptr, int *intValue)
  177. {
  178. int numChars = 0;
  179. int hexValue;
  180. *intValue = 0;
  181. longjmp_on_fault = 1;
  182. while (**ptr) {
  183. hexValue = hex(**ptr);
  184. if (hexValue < 0)
  185. break;
  186. *intValue = (*intValue << 4) | hexValue;
  187. numChars ++;
  188. (*ptr)++;
  189. }
  190. longjmp_on_fault = 0;
  191. return (numChars);
  192. }
  193. /* scan for the sequence $<data>#<checksum> */
  194. static void
  195. getpacket(char *buffer)
  196. {
  197. unsigned char checksum;
  198. unsigned char xmitcsum;
  199. int i;
  200. int count;
  201. unsigned char ch;
  202. do {
  203. /* wait around for the start character, ignore all other
  204. * characters */
  205. while ((ch = (getDebugChar() & 0x7f)) != '$') {
  206. #ifdef KGDB_DEBUG
  207. if (kdebug)
  208. putc(ch);
  209. #endif
  210. ;
  211. }
  212. checksum = 0;
  213. xmitcsum = -1;
  214. count = 0;
  215. /* now, read until a # or end of buffer is found */
  216. while (count < BUFMAX) {
  217. ch = getDebugChar() & 0x7f;
  218. if (ch == '#')
  219. break;
  220. checksum = checksum + ch;
  221. buffer[count] = ch;
  222. count = count + 1;
  223. }
  224. if (count >= BUFMAX)
  225. continue;
  226. buffer[count] = 0;
  227. if (ch == '#') {
  228. xmitcsum = hex(getDebugChar() & 0x7f) << 4;
  229. xmitcsum |= hex(getDebugChar() & 0x7f);
  230. if (checksum != xmitcsum)
  231. putDebugChar('-'); /* failed checksum */
  232. else {
  233. putDebugChar('+'); /* successful transfer */
  234. /* if a sequence char is present, reply the ID */
  235. if (buffer[2] == ':') {
  236. putDebugChar(buffer[0]);
  237. putDebugChar(buffer[1]);
  238. /* remove sequence chars from buffer */
  239. count = strlen(buffer);
  240. for (i=3; i <= count; i++)
  241. buffer[i-3] = buffer[i];
  242. }
  243. }
  244. }
  245. } while (checksum != xmitcsum);
  246. }
  247. /* send the packet in buffer. */
  248. static void
  249. putpacket(unsigned char *buffer)
  250. {
  251. unsigned char checksum;
  252. int count;
  253. unsigned char ch, recv;
  254. /* $<packet info>#<checksum>. */
  255. do {
  256. putDebugChar('$');
  257. checksum = 0;
  258. count = 0;
  259. while ((ch = buffer[count])) {
  260. putDebugChar(ch);
  261. checksum += ch;
  262. count += 1;
  263. }
  264. putDebugChar('#');
  265. putDebugChar(hexchars[checksum >> 4]);
  266. putDebugChar(hexchars[checksum & 0xf]);
  267. recv = getDebugChar();
  268. } while ((recv & 0x7f) != '+');
  269. }
  270. /*
  271. * This function does all command processing for interfacing to gdb.
  272. */
  273. static int
  274. handle_exception (struct pt_regs *regs)
  275. {
  276. int addr;
  277. int length;
  278. char *ptr;
  279. kgdb_data kd;
  280. int i;
  281. if (!initialized) {
  282. printf("kgdb: exception before kgdb is initialized! huh?\n");
  283. return (0);
  284. }
  285. /* probably should check which exception occurred as well */
  286. if (longjmp_on_fault) {
  287. longjmp_on_fault = 0;
  288. kgdb_longjmp(error_jmp_buf, KGDBERR_MEMFAULT);
  289. panic("kgdb longjump failed!\n");
  290. }
  291. if (kgdb_active) {
  292. printf("kgdb: unexpected exception from within kgdb\n");
  293. return (0);
  294. }
  295. kgdb_active = 1;
  296. kgdb_interruptible(0);
  297. printf("kgdb: handle_exception; trap [0x%x]\n", kgdb_trap(regs));
  298. if (kgdb_setjmp(error_jmp_buf) != 0)
  299. panic("kgdb: error or fault in entry init!\n");
  300. kgdb_enter(regs, &kd);
  301. entry_regs = *regs;
  302. ptr = remcomOutBuffer;
  303. *ptr++ = 'T';
  304. *ptr++ = hexchars[kd.sigval >> 4];
  305. *ptr++ = hexchars[kd.sigval & 0xf];
  306. for (i = 0; i < kd.nregs; i++) {
  307. kgdb_reg *rp = &kd.regs[i];
  308. *ptr++ = hexchars[rp->num >> 4];
  309. *ptr++ = hexchars[rp->num & 0xf];
  310. *ptr++ = ':';
  311. ptr = (char *)mem2hex((char *)&rp->val, ptr, 4);
  312. *ptr++ = ';';
  313. }
  314. *ptr = 0;
  315. #ifdef KGDB_DEBUG
  316. if (kdebug)
  317. printf("kgdb: remcomOutBuffer: %s\n", remcomOutBuffer);
  318. #endif
  319. putpacket((unsigned char *)&remcomOutBuffer);
  320. while (1) {
  321. volatile int errnum;
  322. remcomOutBuffer[0] = 0;
  323. getpacket(remcomInBuffer);
  324. ptr = &remcomInBuffer[1];
  325. #ifdef KGDB_DEBUG
  326. if (kdebug)
  327. printf("kgdb: remcomInBuffer: %s\n", remcomInBuffer);
  328. #endif
  329. errnum = kgdb_setjmp(error_jmp_buf);
  330. if (errnum == 0) switch (remcomInBuffer[0]) {
  331. case '?': /* report most recent signal */
  332. remcomOutBuffer[0] = 'S';
  333. remcomOutBuffer[1] = hexchars[kd.sigval >> 4];
  334. remcomOutBuffer[2] = hexchars[kd.sigval & 0xf];
  335. remcomOutBuffer[3] = 0;
  336. break;
  337. #ifdef KGDB_DEBUG
  338. case 'd':
  339. /* toggle debug flag */
  340. kdebug ^= 1;
  341. break;
  342. #endif
  343. case 'g': /* return the value of the CPU registers. */
  344. length = kgdb_getregs(regs, remcomRegBuffer, BUFMAX);
  345. mem2hex(remcomRegBuffer, remcomOutBuffer, length);
  346. break;
  347. case 'G': /* set the value of the CPU registers */
  348. length = strlen(ptr);
  349. if ((length & 1) != 0) kgdb_error(KGDBERR_BADPARAMS);
  350. hex2mem(ptr, remcomRegBuffer, length/2);
  351. kgdb_putregs(regs, remcomRegBuffer, length/2);
  352. strcpy(remcomOutBuffer,"OK");
  353. break;
  354. case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
  355. /* Try to read %x,%x. */
  356. if (hexToInt(&ptr, &addr)
  357. && *ptr++ == ','
  358. && hexToInt(&ptr, &length)) {
  359. mem2hex((char *)addr, remcomOutBuffer, length);
  360. } else {
  361. kgdb_error(KGDBERR_BADPARAMS);
  362. }
  363. break;
  364. case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
  365. /* Try to read '%x,%x:'. */
  366. if (hexToInt(&ptr, &addr)
  367. && *ptr++ == ','
  368. && hexToInt(&ptr, &length)
  369. && *ptr++ == ':') {
  370. hex2mem(ptr, (char *)addr, length);
  371. strcpy(remcomOutBuffer, "OK");
  372. } else {
  373. kgdb_error(KGDBERR_BADPARAMS);
  374. }
  375. break;
  376. case 'k': /* kill the program, actually return to monitor */
  377. kd.extype = KGDBEXIT_KILL;
  378. *regs = entry_regs;
  379. goto doexit;
  380. case 'C': /* CSS continue with signal SS */
  381. *ptr = '\0'; /* ignore the signal number for now */
  382. /* fall through */
  383. case 'c': /* cAA..AA Continue; address AA..AA optional */
  384. /* try to read optional parameter, pc unchanged if no parm */
  385. kd.extype = KGDBEXIT_CONTINUE;
  386. if (hexToInt(&ptr, &addr)) {
  387. kd.exaddr = addr;
  388. kd.extype |= KGDBEXIT_WITHADDR;
  389. }
  390. goto doexit;
  391. case 'S': /* SSS single step with signal SS */
  392. *ptr = '\0'; /* ignore the signal number for now */
  393. /* fall through */
  394. case 's':
  395. kd.extype = KGDBEXIT_SINGLE;
  396. if (hexToInt(&ptr, &addr)) {
  397. kd.exaddr = addr;
  398. kd.extype |= KGDBEXIT_WITHADDR;
  399. }
  400. doexit:
  401. /* Need to flush the instruction cache here, as we may have deposited a
  402. * breakpoint, and the icache probably has no way of knowing that a data ref to
  403. * some location may have changed something that is in the instruction cache.
  404. */
  405. kgdb_flush_cache_all();
  406. kgdb_exit(regs, &kd);
  407. kgdb_active = 0;
  408. kgdb_interruptible(1);
  409. return (1);
  410. case 'r': /* Reset (if user process..exit ???)*/
  411. panic("kgdb reset.");
  412. break;
  413. case 'P': /* Pr=v set reg r to value v (r and v are hex) */
  414. if (hexToInt(&ptr, &addr)
  415. && *ptr++ == '='
  416. && ((length = strlen(ptr)) & 1) == 0) {
  417. hex2mem(ptr, remcomRegBuffer, length/2);
  418. kgdb_putreg(regs, addr,
  419. remcomRegBuffer, length/2);
  420. strcpy(remcomOutBuffer,"OK");
  421. } else {
  422. kgdb_error(KGDBERR_BADPARAMS);
  423. }
  424. break;
  425. } /* switch */
  426. if (errnum != 0)
  427. sprintf(remcomOutBuffer, "E%02d", errnum);
  428. #ifdef KGDB_DEBUG
  429. if (kdebug)
  430. printf("kgdb: remcomOutBuffer: %s\n", remcomOutBuffer);
  431. #endif
  432. /* reply to the request */
  433. putpacket((unsigned char *)&remcomOutBuffer);
  434. } /* while(1) */
  435. }
  436. /*
  437. * kgdb_init must be called *after* the
  438. * monitor is relocated into ram
  439. */
  440. void
  441. kgdb_init(void)
  442. {
  443. kgdb_serial_init();
  444. debugger_exception_handler = handle_exception;
  445. initialized = 1;
  446. putDebugStr("kgdb ready\n");
  447. puts("ready\n");
  448. }
  449. void
  450. kgdb_error(int errnum)
  451. {
  452. longjmp_on_fault = 0;
  453. kgdb_longjmp(error_jmp_buf, errnum);
  454. panic("kgdb_error: longjmp failed!\n");
  455. }
  456. /* Output string in GDB O-packet format if GDB has connected. If nothing
  457. output, returns 0 (caller must then handle output). */
  458. int
  459. kgdb_output_string (const char* s, unsigned int count)
  460. {
  461. char buffer[512];
  462. count = (count <= (sizeof(buffer) / 2 - 2))
  463. ? count : (sizeof(buffer) / 2 - 2);
  464. buffer[0] = 'O';
  465. mem2hex ((char *)s, &buffer[1], count);
  466. putpacket((unsigned char *)&buffer);
  467. return 1;
  468. }
  469. void
  470. breakpoint(void)
  471. {
  472. if (!initialized) {
  473. printf("breakpoint() called b4 kgdb init\n");
  474. return;
  475. }
  476. kgdb_breakpoint(0, 0);
  477. }
  478. int
  479. do_kgdb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  480. {
  481. printf("Entering KGDB mode via exception handler...\n\n");
  482. kgdb_breakpoint(argc - 1, argv + 1);
  483. printf("\nReturned from KGDB mode\n");
  484. return 0;
  485. }
  486. U_BOOT_CMD(
  487. kgdb, CONFIG_SYS_MAXARGS, 1, do_kgdb,
  488. "enter gdb remote debug mode",
  489. "[arg0 arg1 .. argN]\n"
  490. " - executes a breakpoint so that kgdb mode is\n"
  491. " entered via the exception handler. To return\n"
  492. " to the monitor, the remote gdb debugger must\n"
  493. " execute a \"continue\" or \"quit\" command.\n"
  494. "\n"
  495. " if a program is loaded by the remote gdb, any args\n"
  496. " passed to the kgdb command are given to the loaded\n"
  497. " program if it is executed (see the \"hello_world\"\n"
  498. " example program in the U-Boot examples directory)."
  499. );