kgdb.c 14 KB

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