kgdb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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 <kgdb.h>
  85. #include <command.h>
  86. #undef KGDB_DEBUG
  87. /*
  88. * BUFMAX defines the maximum number of characters in inbound/outbound buffers
  89. */
  90. #define BUFMAX 1024
  91. static char remcomInBuffer[BUFMAX];
  92. static char remcomOutBuffer[BUFMAX];
  93. static char remcomRegBuffer[BUFMAX];
  94. static int initialized = 0;
  95. static int kgdb_active;
  96. static struct pt_regs entry_regs;
  97. static long error_jmp_buf[BUFMAX/2];
  98. static int longjmp_on_fault = 0;
  99. #ifdef KGDB_DEBUG
  100. static int kdebug = 1;
  101. #endif
  102. static const char hexchars[]="0123456789abcdef";
  103. /* Convert ch from a hex digit to an int */
  104. static int
  105. hex(unsigned char ch)
  106. {
  107. if (ch >= 'a' && ch <= 'f')
  108. return ch-'a'+10;
  109. if (ch >= '0' && ch <= '9')
  110. return ch-'0';
  111. if (ch >= 'A' && ch <= 'F')
  112. return ch-'A'+10;
  113. return -1;
  114. }
  115. /* Convert the memory pointed to by mem into hex, placing result in buf.
  116. * Return a pointer to the last char put in buf (null).
  117. */
  118. static unsigned char *
  119. mem2hex(char *mem, char *buf, int count)
  120. {
  121. char *tmp;
  122. unsigned char ch;
  123. /*
  124. * We use the upper half of buf as an intermediate buffer for the
  125. * raw memory copy. Hex conversion will work against this one.
  126. */
  127. tmp = buf + count;
  128. longjmp_on_fault = 1;
  129. memcpy(tmp, mem, count);
  130. while (count-- > 0) {
  131. ch = *tmp++;
  132. *buf++ = hexchars[ch >> 4];
  133. *buf++ = hexchars[ch & 0xf];
  134. }
  135. *buf = 0;
  136. longjmp_on_fault = 0;
  137. return (unsigned char *)buf;
  138. }
  139. /* convert the hex array pointed to by buf into binary to be placed in mem
  140. * return a pointer to the character AFTER the last byte fetched from buf.
  141. */
  142. static char *
  143. hex2mem(char *buf, char *mem, int count)
  144. {
  145. int hexValue;
  146. char *tmp_raw, *tmp_hex;
  147. /*
  148. * We use the upper half of buf as an intermediate buffer for the
  149. * raw memory that is converted from hex.
  150. */
  151. tmp_raw = buf + count * 2;
  152. tmp_hex = tmp_raw - 1;
  153. longjmp_on_fault = 1;
  154. while (tmp_hex >= buf) {
  155. tmp_raw--;
  156. hexValue = hex(*tmp_hex--);
  157. if (hexValue < 0)
  158. kgdb_error(KGDBERR_NOTHEXDIG);
  159. *tmp_raw = hexValue;
  160. hexValue = hex(*tmp_hex--);
  161. if (hexValue < 0)
  162. kgdb_error(KGDBERR_NOTHEXDIG);
  163. *tmp_raw |= hexValue << 4;
  164. }
  165. memcpy(mem, tmp_raw, count);
  166. kgdb_flush_cache_range((void *)mem, (void *)(mem+count));
  167. longjmp_on_fault = 0;
  168. return buf;
  169. }
  170. /*
  171. * While we find nice hex chars, build an int.
  172. * Return number of chars processed.
  173. */
  174. static int
  175. hexToInt(char **ptr, int *intValue)
  176. {
  177. int numChars = 0;
  178. int hexValue;
  179. *intValue = 0;
  180. longjmp_on_fault = 1;
  181. while (**ptr) {
  182. hexValue = hex(**ptr);
  183. if (hexValue < 0)
  184. break;
  185. *intValue = (*intValue << 4) | hexValue;
  186. numChars ++;
  187. (*ptr)++;
  188. }
  189. longjmp_on_fault = 0;
  190. return (numChars);
  191. }
  192. /* scan for the sequence $<data>#<checksum> */
  193. static void
  194. getpacket(char *buffer)
  195. {
  196. unsigned char checksum;
  197. unsigned char xmitcsum;
  198. int i;
  199. int count;
  200. unsigned char ch;
  201. do {
  202. /* wait around for the start character, ignore all other
  203. * characters */
  204. while ((ch = (getDebugChar() & 0x7f)) != '$') {
  205. #ifdef KGDB_DEBUG
  206. if (kdebug)
  207. putc(ch);
  208. #endif
  209. ;
  210. }
  211. checksum = 0;
  212. xmitcsum = -1;
  213. count = 0;
  214. /* now, read until a # or end of buffer is found */
  215. while (count < BUFMAX) {
  216. ch = getDebugChar() & 0x7f;
  217. if (ch == '#')
  218. break;
  219. checksum = checksum + ch;
  220. buffer[count] = ch;
  221. count = count + 1;
  222. }
  223. if (count >= BUFMAX)
  224. continue;
  225. buffer[count] = 0;
  226. if (ch == '#') {
  227. xmitcsum = hex(getDebugChar() & 0x7f) << 4;
  228. xmitcsum |= hex(getDebugChar() & 0x7f);
  229. if (checksum != xmitcsum)
  230. putDebugChar('-'); /* failed checksum */
  231. else {
  232. putDebugChar('+'); /* successful transfer */
  233. /* if a sequence char is present, reply the ID */
  234. if (buffer[2] == ':') {
  235. putDebugChar(buffer[0]);
  236. putDebugChar(buffer[1]);
  237. /* remove sequence chars from buffer */
  238. count = strlen(buffer);
  239. for (i=3; i <= count; i++)
  240. buffer[i-3] = buffer[i];
  241. }
  242. }
  243. }
  244. } while (checksum != xmitcsum);
  245. }
  246. /* send the packet in buffer. */
  247. static void
  248. putpacket(unsigned char *buffer)
  249. {
  250. unsigned char checksum;
  251. int count;
  252. unsigned char ch, recv;
  253. /* $<packet info>#<checksum>. */
  254. do {
  255. putDebugChar('$');
  256. checksum = 0;
  257. count = 0;
  258. while ((ch = buffer[count])) {
  259. putDebugChar(ch);
  260. checksum += ch;
  261. count += 1;
  262. }
  263. putDebugChar('#');
  264. putDebugChar(hexchars[checksum >> 4]);
  265. putDebugChar(hexchars[checksum & 0xf]);
  266. recv = getDebugChar();
  267. } while ((recv & 0x7f) != '+');
  268. }
  269. /*
  270. * This function does all command processing for interfacing to gdb.
  271. */
  272. static int
  273. handle_exception (struct pt_regs *regs)
  274. {
  275. int addr;
  276. int length;
  277. char *ptr;
  278. kgdb_data kd;
  279. int i;
  280. if (!initialized) {
  281. printf("kgdb: exception before kgdb is initialized! huh?\n");
  282. return (0);
  283. }
  284. /* probably should check which exception occurred as well */
  285. if (longjmp_on_fault) {
  286. longjmp_on_fault = 0;
  287. kgdb_longjmp(error_jmp_buf, KGDBERR_MEMFAULT);
  288. panic("kgdb longjump failed!\n");
  289. }
  290. if (kgdb_active) {
  291. printf("kgdb: unexpected exception from within kgdb\n");
  292. return (0);
  293. }
  294. kgdb_active = 1;
  295. kgdb_interruptible(0);
  296. printf("kgdb: handle_exception; trap [0x%x]\n", kgdb_trap(regs));
  297. if (kgdb_setjmp(error_jmp_buf) != 0)
  298. panic("kgdb: error or fault in entry init!\n");
  299. kgdb_enter(regs, &kd);
  300. entry_regs = *regs;
  301. ptr = remcomOutBuffer;
  302. *ptr++ = 'T';
  303. *ptr++ = hexchars[kd.sigval >> 4];
  304. *ptr++ = hexchars[kd.sigval & 0xf];
  305. for (i = 0; i < kd.nregs; i++) {
  306. kgdb_reg *rp = &kd.regs[i];
  307. *ptr++ = hexchars[rp->num >> 4];
  308. *ptr++ = hexchars[rp->num & 0xf];
  309. *ptr++ = ':';
  310. ptr = (char *)mem2hex((char *)&rp->val, ptr, 4);
  311. *ptr++ = ';';
  312. }
  313. *ptr = 0;
  314. #ifdef KGDB_DEBUG
  315. if (kdebug)
  316. printf("kgdb: remcomOutBuffer: %s\n", remcomOutBuffer);
  317. #endif
  318. putpacket((unsigned char *)&remcomOutBuffer);
  319. while (1) {
  320. volatile int errnum;
  321. remcomOutBuffer[0] = 0;
  322. getpacket(remcomInBuffer);
  323. ptr = &remcomInBuffer[1];
  324. #ifdef KGDB_DEBUG
  325. if (kdebug)
  326. printf("kgdb: remcomInBuffer: %s\n", remcomInBuffer);
  327. #endif
  328. errnum = kgdb_setjmp(error_jmp_buf);
  329. if (errnum == 0) switch (remcomInBuffer[0]) {
  330. case '?': /* report most recent signal */
  331. remcomOutBuffer[0] = 'S';
  332. remcomOutBuffer[1] = hexchars[kd.sigval >> 4];
  333. remcomOutBuffer[2] = hexchars[kd.sigval & 0xf];
  334. remcomOutBuffer[3] = 0;
  335. break;
  336. #ifdef KGDB_DEBUG
  337. case 'd':
  338. /* toggle debug flag */
  339. kdebug ^= 1;
  340. break;
  341. #endif
  342. case 'g': /* return the value of the CPU registers. */
  343. length = kgdb_getregs(regs, remcomRegBuffer, BUFMAX);
  344. mem2hex(remcomRegBuffer, remcomOutBuffer, length);
  345. break;
  346. case 'G': /* set the value of the CPU registers */
  347. length = strlen(ptr);
  348. if ((length & 1) != 0) kgdb_error(KGDBERR_BADPARAMS);
  349. hex2mem(ptr, remcomRegBuffer, length/2);
  350. kgdb_putregs(regs, remcomRegBuffer, length/2);
  351. strcpy(remcomOutBuffer,"OK");
  352. break;
  353. case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
  354. /* Try to read %x,%x. */
  355. if (hexToInt(&ptr, &addr)
  356. && *ptr++ == ','
  357. && hexToInt(&ptr, &length)) {
  358. mem2hex((char *)addr, remcomOutBuffer, length);
  359. } else {
  360. kgdb_error(KGDBERR_BADPARAMS);
  361. }
  362. break;
  363. case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
  364. /* Try to read '%x,%x:'. */
  365. if (hexToInt(&ptr, &addr)
  366. && *ptr++ == ','
  367. && hexToInt(&ptr, &length)
  368. && *ptr++ == ':') {
  369. hex2mem(ptr, (char *)addr, length);
  370. strcpy(remcomOutBuffer, "OK");
  371. } else {
  372. kgdb_error(KGDBERR_BADPARAMS);
  373. }
  374. break;
  375. case 'k': /* kill the program, actually return to monitor */
  376. kd.extype = KGDBEXIT_KILL;
  377. *regs = entry_regs;
  378. goto doexit;
  379. case 'C': /* CSS continue with signal SS */
  380. *ptr = '\0'; /* ignore the signal number for now */
  381. /* fall through */
  382. case 'c': /* cAA..AA Continue; address AA..AA optional */
  383. /* try to read optional parameter, pc unchanged if no parm */
  384. kd.extype = KGDBEXIT_CONTINUE;
  385. if (hexToInt(&ptr, &addr)) {
  386. kd.exaddr = addr;
  387. kd.extype |= KGDBEXIT_WITHADDR;
  388. }
  389. goto doexit;
  390. case 'S': /* SSS single step with signal SS */
  391. *ptr = '\0'; /* ignore the signal number for now */
  392. /* fall through */
  393. case 's':
  394. kd.extype = KGDBEXIT_SINGLE;
  395. if (hexToInt(&ptr, &addr)) {
  396. kd.exaddr = addr;
  397. kd.extype |= KGDBEXIT_WITHADDR;
  398. }
  399. doexit:
  400. /* Need to flush the instruction cache here, as we may have deposited a
  401. * breakpoint, and the icache probably has no way of knowing that a data ref to
  402. * some location may have changed something that is in the instruction cache.
  403. */
  404. kgdb_flush_cache_all();
  405. kgdb_exit(regs, &kd);
  406. kgdb_active = 0;
  407. kgdb_interruptible(1);
  408. return (1);
  409. case 'r': /* Reset (if user process..exit ???)*/
  410. panic("kgdb reset.");
  411. break;
  412. case 'P': /* Pr=v set reg r to value v (r and v are hex) */
  413. if (hexToInt(&ptr, &addr)
  414. && *ptr++ == '='
  415. && ((length = strlen(ptr)) & 1) == 0) {
  416. hex2mem(ptr, remcomRegBuffer, length/2);
  417. kgdb_putreg(regs, addr,
  418. remcomRegBuffer, length/2);
  419. strcpy(remcomOutBuffer,"OK");
  420. } else {
  421. kgdb_error(KGDBERR_BADPARAMS);
  422. }
  423. break;
  424. } /* switch */
  425. if (errnum != 0)
  426. sprintf(remcomOutBuffer, "E%02d", errnum);
  427. #ifdef KGDB_DEBUG
  428. if (kdebug)
  429. printf("kgdb: remcomOutBuffer: %s\n", remcomOutBuffer);
  430. #endif
  431. /* reply to the request */
  432. putpacket((unsigned char *)&remcomOutBuffer);
  433. } /* while(1) */
  434. }
  435. /*
  436. * kgdb_init must be called *after* the
  437. * monitor is relocated into ram
  438. */
  439. void
  440. kgdb_init(void)
  441. {
  442. kgdb_serial_init();
  443. debugger_exception_handler = handle_exception;
  444. initialized = 1;
  445. putDebugStr("kgdb ready\n");
  446. puts("ready\n");
  447. }
  448. void
  449. kgdb_error(int errnum)
  450. {
  451. longjmp_on_fault = 0;
  452. kgdb_longjmp(error_jmp_buf, errnum);
  453. panic("kgdb_error: longjmp failed!\n");
  454. }
  455. /* Output string in GDB O-packet format if GDB has connected. If nothing
  456. output, returns 0 (caller must then handle output). */
  457. int
  458. kgdb_output_string (const char* s, unsigned int count)
  459. {
  460. char buffer[512];
  461. count = (count <= (sizeof(buffer) / 2 - 2))
  462. ? count : (sizeof(buffer) / 2 - 2);
  463. buffer[0] = 'O';
  464. mem2hex ((char *)s, &buffer[1], count);
  465. putpacket((unsigned char *)&buffer);
  466. return 1;
  467. }
  468. void
  469. breakpoint(void)
  470. {
  471. if (!initialized) {
  472. printf("breakpoint() called b4 kgdb init\n");
  473. return;
  474. }
  475. kgdb_breakpoint(0, 0);
  476. }
  477. int
  478. do_kgdb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  479. {
  480. printf("Entering KGDB mode via exception handler...\n\n");
  481. kgdb_breakpoint(argc - 1, argv + 1);
  482. printf("\nReturned from KGDB mode\n");
  483. return 0;
  484. }
  485. U_BOOT_CMD(
  486. kgdb, CONFIG_SYS_MAXARGS, 1, do_kgdb,
  487. "enter gdb remote debug mode",
  488. "[arg0 arg1 .. argN]\n"
  489. " - executes a breakpoint so that kgdb mode is\n"
  490. " entered via the exception handler. To return\n"
  491. " to the monitor, the remote gdb debugger must\n"
  492. " execute a \"continue\" or \"quit\" command.\n"
  493. "\n"
  494. " if a program is loaded by the remote gdb, any args\n"
  495. " passed to the kgdb command are given to the loaded\n"
  496. " program if it is executed (see the \"hello_world\"\n"
  497. " example program in the U-Boot examples directory)."
  498. );