gdbstub.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. /*
  2. * Kernel Debug Core
  3. *
  4. * Maintainer: Jason Wessel <jason.wessel@windriver.com>
  5. *
  6. * Copyright (C) 2000-2001 VERITAS Software Corporation.
  7. * Copyright (C) 2002-2004 Timesys Corporation
  8. * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com>
  9. * Copyright (C) 2004 Pavel Machek <pavel@suse.cz>
  10. * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org>
  11. * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd.
  12. * Copyright (C) 2005-2009 Wind River Systems, Inc.
  13. * Copyright (C) 2007 MontaVista Software, Inc.
  14. * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  15. *
  16. * Contributors at various stages not listed above:
  17. * Jason Wessel ( jason.wessel@windriver.com )
  18. * George Anzinger <george@mvista.com>
  19. * Anurekh Saxena (anurekh.saxena@timesys.com)
  20. * Lake Stevens Instrument Division (Glenn Engel)
  21. * Jim Kingdon, Cygnus Support.
  22. *
  23. * Original KGDB stub: David Grothe <dave@gcom.com>,
  24. * Tigran Aivazian <tigran@sco.com>
  25. *
  26. * This file is licensed under the terms of the GNU General Public License
  27. * version 2. This program is licensed "as is" without any warranty of any
  28. * kind, whether express or implied.
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/kgdb.h>
  32. #include <linux/kdb.h>
  33. #include <linux/reboot.h>
  34. #include <linux/uaccess.h>
  35. #include <asm/cacheflush.h>
  36. #include <asm/unaligned.h>
  37. #include "debug_core.h"
  38. #define KGDB_MAX_THREAD_QUERY 17
  39. /* Our I/O buffers. */
  40. static char remcom_in_buffer[BUFMAX];
  41. static char remcom_out_buffer[BUFMAX];
  42. /* Storage for the registers, in GDB format. */
  43. static unsigned long gdb_regs[(NUMREGBYTES +
  44. sizeof(unsigned long) - 1) /
  45. sizeof(unsigned long)];
  46. /*
  47. * GDB remote protocol parser:
  48. */
  49. static int hex(char ch)
  50. {
  51. if ((ch >= 'a') && (ch <= 'f'))
  52. return ch - 'a' + 10;
  53. if ((ch >= '0') && (ch <= '9'))
  54. return ch - '0';
  55. if ((ch >= 'A') && (ch <= 'F'))
  56. return ch - 'A' + 10;
  57. return -1;
  58. }
  59. #ifdef CONFIG_KGDB_KDB
  60. static int gdbstub_read_wait(void)
  61. {
  62. int ret = -1;
  63. int i;
  64. /* poll any additional I/O interfaces that are defined */
  65. while (ret < 0)
  66. for (i = 0; kdb_poll_funcs[i] != NULL; i++) {
  67. ret = kdb_poll_funcs[i]();
  68. if (ret > 0)
  69. break;
  70. }
  71. return ret;
  72. }
  73. #else
  74. static int gdbstub_read_wait(void)
  75. {
  76. int ret = dbg_io_ops->read_char();
  77. while (ret == NO_POLL_CHAR)
  78. ret = dbg_io_ops->read_char();
  79. return ret;
  80. }
  81. #endif
  82. /* scan for the sequence $<data>#<checksum> */
  83. static void get_packet(char *buffer)
  84. {
  85. unsigned char checksum;
  86. unsigned char xmitcsum;
  87. int count;
  88. char ch;
  89. do {
  90. /*
  91. * Spin and wait around for the start character, ignore all
  92. * other characters:
  93. */
  94. while ((ch = (gdbstub_read_wait())) != '$')
  95. /* nothing */;
  96. kgdb_connected = 1;
  97. checksum = 0;
  98. xmitcsum = -1;
  99. count = 0;
  100. /*
  101. * now, read until a # or end of buffer is found:
  102. */
  103. while (count < (BUFMAX - 1)) {
  104. ch = gdbstub_read_wait();
  105. if (ch == '#')
  106. break;
  107. checksum = checksum + ch;
  108. buffer[count] = ch;
  109. count = count + 1;
  110. }
  111. buffer[count] = 0;
  112. if (ch == '#') {
  113. xmitcsum = hex(gdbstub_read_wait()) << 4;
  114. xmitcsum += hex(gdbstub_read_wait());
  115. if (checksum != xmitcsum)
  116. /* failed checksum */
  117. dbg_io_ops->write_char('-');
  118. else
  119. /* successful transfer */
  120. dbg_io_ops->write_char('+');
  121. if (dbg_io_ops->flush)
  122. dbg_io_ops->flush();
  123. }
  124. } while (checksum != xmitcsum);
  125. }
  126. /*
  127. * Send the packet in buffer.
  128. * Check for gdb connection if asked for.
  129. */
  130. static void put_packet(char *buffer)
  131. {
  132. unsigned char checksum;
  133. int count;
  134. char ch;
  135. /*
  136. * $<packet info>#<checksum>.
  137. */
  138. while (1) {
  139. dbg_io_ops->write_char('$');
  140. checksum = 0;
  141. count = 0;
  142. while ((ch = buffer[count])) {
  143. dbg_io_ops->write_char(ch);
  144. checksum += ch;
  145. count++;
  146. }
  147. dbg_io_ops->write_char('#');
  148. dbg_io_ops->write_char(hex_asc_hi(checksum));
  149. dbg_io_ops->write_char(hex_asc_lo(checksum));
  150. if (dbg_io_ops->flush)
  151. dbg_io_ops->flush();
  152. /* Now see what we get in reply. */
  153. ch = gdbstub_read_wait();
  154. if (ch == 3)
  155. ch = gdbstub_read_wait();
  156. /* If we get an ACK, we are done. */
  157. if (ch == '+')
  158. return;
  159. /*
  160. * If we get the start of another packet, this means
  161. * that GDB is attempting to reconnect. We will NAK
  162. * the packet being sent, and stop trying to send this
  163. * packet.
  164. */
  165. if (ch == '$') {
  166. dbg_io_ops->write_char('-');
  167. if (dbg_io_ops->flush)
  168. dbg_io_ops->flush();
  169. return;
  170. }
  171. }
  172. }
  173. static char gdbmsgbuf[BUFMAX + 1];
  174. void gdbstub_msg_write(const char *s, int len)
  175. {
  176. char *bufptr;
  177. int wcount;
  178. int i;
  179. if (len == 0)
  180. len = strlen(s);
  181. /* 'O'utput */
  182. gdbmsgbuf[0] = 'O';
  183. /* Fill and send buffers... */
  184. while (len > 0) {
  185. bufptr = gdbmsgbuf + 1;
  186. /* Calculate how many this time */
  187. if ((len << 1) > (BUFMAX - 2))
  188. wcount = (BUFMAX - 2) >> 1;
  189. else
  190. wcount = len;
  191. /* Pack in hex chars */
  192. for (i = 0; i < wcount; i++)
  193. bufptr = pack_hex_byte(bufptr, s[i]);
  194. *bufptr = '\0';
  195. /* Move up */
  196. s += wcount;
  197. len -= wcount;
  198. /* Write packet */
  199. put_packet(gdbmsgbuf);
  200. }
  201. }
  202. /*
  203. * Convert the memory pointed to by mem into hex, placing result in
  204. * buf. Return a pointer to the last char put in buf (null). May
  205. * return an error.
  206. */
  207. int kgdb_mem2hex(char *mem, char *buf, int count)
  208. {
  209. char *tmp;
  210. int err;
  211. /*
  212. * We use the upper half of buf as an intermediate buffer for the
  213. * raw memory copy. Hex conversion will work against this one.
  214. */
  215. tmp = buf + count;
  216. err = probe_kernel_read(tmp, mem, count);
  217. if (!err) {
  218. while (count > 0) {
  219. buf = pack_hex_byte(buf, *tmp);
  220. tmp++;
  221. count--;
  222. }
  223. *buf = 0;
  224. }
  225. return err;
  226. }
  227. /*
  228. * Convert the hex array pointed to by buf into binary to be placed in
  229. * mem. Return a pointer to the character AFTER the last byte
  230. * written. May return an error.
  231. */
  232. int kgdb_hex2mem(char *buf, char *mem, int count)
  233. {
  234. char *tmp_raw;
  235. char *tmp_hex;
  236. /*
  237. * We use the upper half of buf as an intermediate buffer for the
  238. * raw memory that is converted from hex.
  239. */
  240. tmp_raw = buf + count * 2;
  241. tmp_hex = tmp_raw - 1;
  242. while (tmp_hex >= buf) {
  243. tmp_raw--;
  244. *tmp_raw = hex(*tmp_hex--);
  245. *tmp_raw |= hex(*tmp_hex--) << 4;
  246. }
  247. return probe_kernel_write(mem, tmp_raw, count);
  248. }
  249. /*
  250. * While we find nice hex chars, build a long_val.
  251. * Return number of chars processed.
  252. */
  253. int kgdb_hex2long(char **ptr, unsigned long *long_val)
  254. {
  255. int hex_val;
  256. int num = 0;
  257. int negate = 0;
  258. *long_val = 0;
  259. if (**ptr == '-') {
  260. negate = 1;
  261. (*ptr)++;
  262. }
  263. while (**ptr) {
  264. hex_val = hex(**ptr);
  265. if (hex_val < 0)
  266. break;
  267. *long_val = (*long_val << 4) | hex_val;
  268. num++;
  269. (*ptr)++;
  270. }
  271. if (negate)
  272. *long_val = -*long_val;
  273. return num;
  274. }
  275. /*
  276. * Copy the binary array pointed to by buf into mem. Fix $, #, and
  277. * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success.
  278. * The input buf is overwitten with the result to write to mem.
  279. */
  280. static int kgdb_ebin2mem(char *buf, char *mem, int count)
  281. {
  282. int size = 0;
  283. char *c = buf;
  284. while (count-- > 0) {
  285. c[size] = *buf++;
  286. if (c[size] == 0x7d)
  287. c[size] = *buf++ ^ 0x20;
  288. size++;
  289. }
  290. return probe_kernel_write(mem, c, size);
  291. }
  292. /* Write memory due to an 'M' or 'X' packet. */
  293. static int write_mem_msg(int binary)
  294. {
  295. char *ptr = &remcom_in_buffer[1];
  296. unsigned long addr;
  297. unsigned long length;
  298. int err;
  299. if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' &&
  300. kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') {
  301. if (binary)
  302. err = kgdb_ebin2mem(ptr, (char *)addr, length);
  303. else
  304. err = kgdb_hex2mem(ptr, (char *)addr, length);
  305. if (err)
  306. return err;
  307. if (CACHE_FLUSH_IS_SAFE)
  308. flush_icache_range(addr, addr + length);
  309. return 0;
  310. }
  311. return -EINVAL;
  312. }
  313. static void error_packet(char *pkt, int error)
  314. {
  315. error = -error;
  316. pkt[0] = 'E';
  317. pkt[1] = hex_asc[(error / 10)];
  318. pkt[2] = hex_asc[(error % 10)];
  319. pkt[3] = '\0';
  320. }
  321. /*
  322. * Thread ID accessors. We represent a flat TID space to GDB, where
  323. * the per CPU idle threads (which under Linux all have PID 0) are
  324. * remapped to negative TIDs.
  325. */
  326. #define BUF_THREAD_ID_SIZE 16
  327. static char *pack_threadid(char *pkt, unsigned char *id)
  328. {
  329. char *limit;
  330. limit = pkt + BUF_THREAD_ID_SIZE;
  331. while (pkt < limit)
  332. pkt = pack_hex_byte(pkt, *id++);
  333. return pkt;
  334. }
  335. static void int_to_threadref(unsigned char *id, int value)
  336. {
  337. unsigned char *scan;
  338. int i = 4;
  339. scan = (unsigned char *)id;
  340. while (i--)
  341. *scan++ = 0;
  342. put_unaligned_be32(value, scan);
  343. }
  344. static struct task_struct *getthread(struct pt_regs *regs, int tid)
  345. {
  346. /*
  347. * Non-positive TIDs are remapped to the cpu shadow information
  348. */
  349. if (tid == 0 || tid == -1)
  350. tid = -atomic_read(&kgdb_active) - 2;
  351. if (tid < -1 && tid > -NR_CPUS - 2) {
  352. if (kgdb_info[-tid - 2].task)
  353. return kgdb_info[-tid - 2].task;
  354. else
  355. return idle_task(-tid - 2);
  356. }
  357. if (tid <= 0) {
  358. printk(KERN_ERR "KGDB: Internal thread select error\n");
  359. dump_stack();
  360. return NULL;
  361. }
  362. /*
  363. * find_task_by_pid_ns() does not take the tasklist lock anymore
  364. * but is nicely RCU locked - hence is a pretty resilient
  365. * thing to use:
  366. */
  367. return find_task_by_pid_ns(tid, &init_pid_ns);
  368. }
  369. /*
  370. * Remap normal tasks to their real PID,
  371. * CPU shadow threads are mapped to -CPU - 2
  372. */
  373. static inline int shadow_pid(int realpid)
  374. {
  375. if (realpid)
  376. return realpid;
  377. return -raw_smp_processor_id() - 2;
  378. }
  379. /*
  380. * All the functions that start with gdb_cmd are the various
  381. * operations to implement the handlers for the gdbserial protocol
  382. * where KGDB is communicating with an external debugger
  383. */
  384. /* Handle the '?' status packets */
  385. static void gdb_cmd_status(struct kgdb_state *ks)
  386. {
  387. /*
  388. * We know that this packet is only sent
  389. * during initial connect. So to be safe,
  390. * we clear out our breakpoints now in case
  391. * GDB is reconnecting.
  392. */
  393. dbg_remove_all_break();
  394. remcom_out_buffer[0] = 'S';
  395. pack_hex_byte(&remcom_out_buffer[1], ks->signo);
  396. }
  397. /* Handle the 'g' get registers request */
  398. static void gdb_cmd_getregs(struct kgdb_state *ks)
  399. {
  400. struct task_struct *thread;
  401. void *local_debuggerinfo;
  402. int i;
  403. thread = kgdb_usethread;
  404. if (!thread) {
  405. thread = kgdb_info[ks->cpu].task;
  406. local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
  407. } else {
  408. local_debuggerinfo = NULL;
  409. for_each_online_cpu(i) {
  410. /*
  411. * Try to find the task on some other
  412. * or possibly this node if we do not
  413. * find the matching task then we try
  414. * to approximate the results.
  415. */
  416. if (thread == kgdb_info[i].task)
  417. local_debuggerinfo = kgdb_info[i].debuggerinfo;
  418. }
  419. }
  420. /*
  421. * All threads that don't have debuggerinfo should be
  422. * in schedule() sleeping, since all other CPUs
  423. * are in kgdb_wait, and thus have debuggerinfo.
  424. */
  425. if (local_debuggerinfo) {
  426. pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
  427. } else {
  428. /*
  429. * Pull stuff saved during switch_to; nothing
  430. * else is accessible (or even particularly
  431. * relevant).
  432. *
  433. * This should be enough for a stack trace.
  434. */
  435. sleeping_thread_to_gdb_regs(gdb_regs, thread);
  436. }
  437. kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
  438. }
  439. /* Handle the 'G' set registers request */
  440. static void gdb_cmd_setregs(struct kgdb_state *ks)
  441. {
  442. kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
  443. if (kgdb_usethread && kgdb_usethread != current) {
  444. error_packet(remcom_out_buffer, -EINVAL);
  445. } else {
  446. gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
  447. strcpy(remcom_out_buffer, "OK");
  448. }
  449. }
  450. /* Handle the 'm' memory read bytes */
  451. static void gdb_cmd_memread(struct kgdb_state *ks)
  452. {
  453. char *ptr = &remcom_in_buffer[1];
  454. unsigned long length;
  455. unsigned long addr;
  456. int err;
  457. if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
  458. kgdb_hex2long(&ptr, &length) > 0) {
  459. err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
  460. if (err)
  461. error_packet(remcom_out_buffer, err);
  462. } else {
  463. error_packet(remcom_out_buffer, -EINVAL);
  464. }
  465. }
  466. /* Handle the 'M' memory write bytes */
  467. static void gdb_cmd_memwrite(struct kgdb_state *ks)
  468. {
  469. int err = write_mem_msg(0);
  470. if (err)
  471. error_packet(remcom_out_buffer, err);
  472. else
  473. strcpy(remcom_out_buffer, "OK");
  474. }
  475. /* Handle the 'X' memory binary write bytes */
  476. static void gdb_cmd_binwrite(struct kgdb_state *ks)
  477. {
  478. int err = write_mem_msg(1);
  479. if (err)
  480. error_packet(remcom_out_buffer, err);
  481. else
  482. strcpy(remcom_out_buffer, "OK");
  483. }
  484. /* Handle the 'D' or 'k', detach or kill packets */
  485. static void gdb_cmd_detachkill(struct kgdb_state *ks)
  486. {
  487. int error;
  488. /* The detach case */
  489. if (remcom_in_buffer[0] == 'D') {
  490. error = dbg_remove_all_break();
  491. if (error < 0) {
  492. error_packet(remcom_out_buffer, error);
  493. } else {
  494. strcpy(remcom_out_buffer, "OK");
  495. kgdb_connected = 0;
  496. }
  497. put_packet(remcom_out_buffer);
  498. } else {
  499. /*
  500. * Assume the kill case, with no exit code checking,
  501. * trying to force detach the debugger:
  502. */
  503. dbg_remove_all_break();
  504. kgdb_connected = 0;
  505. }
  506. }
  507. /* Handle the 'R' reboot packets */
  508. static int gdb_cmd_reboot(struct kgdb_state *ks)
  509. {
  510. /* For now, only honor R0 */
  511. if (strcmp(remcom_in_buffer, "R0") == 0) {
  512. printk(KERN_CRIT "Executing emergency reboot\n");
  513. strcpy(remcom_out_buffer, "OK");
  514. put_packet(remcom_out_buffer);
  515. /*
  516. * Execution should not return from
  517. * machine_emergency_restart()
  518. */
  519. machine_emergency_restart();
  520. kgdb_connected = 0;
  521. return 1;
  522. }
  523. return 0;
  524. }
  525. /* Handle the 'q' query packets */
  526. static void gdb_cmd_query(struct kgdb_state *ks)
  527. {
  528. struct task_struct *g;
  529. struct task_struct *p;
  530. unsigned char thref[8];
  531. char *ptr;
  532. int i;
  533. int cpu;
  534. int finished = 0;
  535. switch (remcom_in_buffer[1]) {
  536. case 's':
  537. case 'f':
  538. if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) {
  539. error_packet(remcom_out_buffer, -EINVAL);
  540. break;
  541. }
  542. i = 0;
  543. remcom_out_buffer[0] = 'm';
  544. ptr = remcom_out_buffer + 1;
  545. if (remcom_in_buffer[1] == 'f') {
  546. /* Each cpu is a shadow thread */
  547. for_each_online_cpu(cpu) {
  548. ks->thr_query = 0;
  549. int_to_threadref(thref, -cpu - 2);
  550. pack_threadid(ptr, thref);
  551. ptr += BUF_THREAD_ID_SIZE;
  552. *(ptr++) = ',';
  553. i++;
  554. }
  555. }
  556. do_each_thread(g, p) {
  557. if (i >= ks->thr_query && !finished) {
  558. int_to_threadref(thref, p->pid);
  559. pack_threadid(ptr, thref);
  560. ptr += BUF_THREAD_ID_SIZE;
  561. *(ptr++) = ',';
  562. ks->thr_query++;
  563. if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0)
  564. finished = 1;
  565. }
  566. i++;
  567. } while_each_thread(g, p);
  568. *(--ptr) = '\0';
  569. break;
  570. case 'C':
  571. /* Current thread id */
  572. strcpy(remcom_out_buffer, "QC");
  573. ks->threadid = shadow_pid(current->pid);
  574. int_to_threadref(thref, ks->threadid);
  575. pack_threadid(remcom_out_buffer + 2, thref);
  576. break;
  577. case 'T':
  578. if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16)) {
  579. error_packet(remcom_out_buffer, -EINVAL);
  580. break;
  581. }
  582. ks->threadid = 0;
  583. ptr = remcom_in_buffer + 17;
  584. kgdb_hex2long(&ptr, &ks->threadid);
  585. if (!getthread(ks->linux_regs, ks->threadid)) {
  586. error_packet(remcom_out_buffer, -EINVAL);
  587. break;
  588. }
  589. if ((int)ks->threadid > 0) {
  590. kgdb_mem2hex(getthread(ks->linux_regs,
  591. ks->threadid)->comm,
  592. remcom_out_buffer, 16);
  593. } else {
  594. static char tmpstr[23 + BUF_THREAD_ID_SIZE];
  595. sprintf(tmpstr, "shadowCPU%d",
  596. (int)(-ks->threadid - 2));
  597. kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
  598. }
  599. break;
  600. #ifdef CONFIG_KGDB_KDB
  601. case 'R':
  602. if (strncmp(remcom_in_buffer, "qRcmd,", 6) == 0) {
  603. int len = strlen(remcom_in_buffer + 6);
  604. if ((len % 2) != 0) {
  605. strcpy(remcom_out_buffer, "E01");
  606. break;
  607. }
  608. kgdb_hex2mem(remcom_in_buffer + 6,
  609. remcom_out_buffer, len);
  610. len = len / 2;
  611. remcom_out_buffer[len++] = 0;
  612. kdb_parse(remcom_out_buffer);
  613. strcpy(remcom_out_buffer, "OK");
  614. }
  615. break;
  616. #endif
  617. }
  618. }
  619. /* Handle the 'H' task query packets */
  620. static void gdb_cmd_task(struct kgdb_state *ks)
  621. {
  622. struct task_struct *thread;
  623. char *ptr;
  624. switch (remcom_in_buffer[1]) {
  625. case 'g':
  626. ptr = &remcom_in_buffer[2];
  627. kgdb_hex2long(&ptr, &ks->threadid);
  628. thread = getthread(ks->linux_regs, ks->threadid);
  629. if (!thread && ks->threadid > 0) {
  630. error_packet(remcom_out_buffer, -EINVAL);
  631. break;
  632. }
  633. kgdb_usethread = thread;
  634. ks->kgdb_usethreadid = ks->threadid;
  635. strcpy(remcom_out_buffer, "OK");
  636. break;
  637. case 'c':
  638. ptr = &remcom_in_buffer[2];
  639. kgdb_hex2long(&ptr, &ks->threadid);
  640. if (!ks->threadid) {
  641. kgdb_contthread = NULL;
  642. } else {
  643. thread = getthread(ks->linux_regs, ks->threadid);
  644. if (!thread && ks->threadid > 0) {
  645. error_packet(remcom_out_buffer, -EINVAL);
  646. break;
  647. }
  648. kgdb_contthread = thread;
  649. }
  650. strcpy(remcom_out_buffer, "OK");
  651. break;
  652. }
  653. }
  654. /* Handle the 'T' thread query packets */
  655. static void gdb_cmd_thread(struct kgdb_state *ks)
  656. {
  657. char *ptr = &remcom_in_buffer[1];
  658. struct task_struct *thread;
  659. kgdb_hex2long(&ptr, &ks->threadid);
  660. thread = getthread(ks->linux_regs, ks->threadid);
  661. if (thread)
  662. strcpy(remcom_out_buffer, "OK");
  663. else
  664. error_packet(remcom_out_buffer, -EINVAL);
  665. }
  666. /* Handle the 'z' or 'Z' breakpoint remove or set packets */
  667. static void gdb_cmd_break(struct kgdb_state *ks)
  668. {
  669. /*
  670. * Since GDB-5.3, it's been drafted that '0' is a software
  671. * breakpoint, '1' is a hardware breakpoint, so let's do that.
  672. */
  673. char *bpt_type = &remcom_in_buffer[1];
  674. char *ptr = &remcom_in_buffer[2];
  675. unsigned long addr;
  676. unsigned long length;
  677. int error = 0;
  678. if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
  679. /* Unsupported */
  680. if (*bpt_type > '4')
  681. return;
  682. } else {
  683. if (*bpt_type != '0' && *bpt_type != '1')
  684. /* Unsupported. */
  685. return;
  686. }
  687. /*
  688. * Test if this is a hardware breakpoint, and
  689. * if we support it:
  690. */
  691. if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
  692. /* Unsupported. */
  693. return;
  694. if (*(ptr++) != ',') {
  695. error_packet(remcom_out_buffer, -EINVAL);
  696. return;
  697. }
  698. if (!kgdb_hex2long(&ptr, &addr)) {
  699. error_packet(remcom_out_buffer, -EINVAL);
  700. return;
  701. }
  702. if (*(ptr++) != ',' ||
  703. !kgdb_hex2long(&ptr, &length)) {
  704. error_packet(remcom_out_buffer, -EINVAL);
  705. return;
  706. }
  707. if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
  708. error = dbg_set_sw_break(addr);
  709. else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
  710. error = dbg_remove_sw_break(addr);
  711. else if (remcom_in_buffer[0] == 'Z')
  712. error = arch_kgdb_ops.set_hw_breakpoint(addr,
  713. (int)length, *bpt_type - '0');
  714. else if (remcom_in_buffer[0] == 'z')
  715. error = arch_kgdb_ops.remove_hw_breakpoint(addr,
  716. (int) length, *bpt_type - '0');
  717. if (error == 0)
  718. strcpy(remcom_out_buffer, "OK");
  719. else
  720. error_packet(remcom_out_buffer, error);
  721. }
  722. /* Handle the 'C' signal / exception passing packets */
  723. static int gdb_cmd_exception_pass(struct kgdb_state *ks)
  724. {
  725. /* C09 == pass exception
  726. * C15 == detach kgdb, pass exception
  727. */
  728. if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
  729. ks->pass_exception = 1;
  730. remcom_in_buffer[0] = 'c';
  731. } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
  732. ks->pass_exception = 1;
  733. remcom_in_buffer[0] = 'D';
  734. dbg_remove_all_break();
  735. kgdb_connected = 0;
  736. return 1;
  737. } else {
  738. gdbstub_msg_write("KGDB only knows signal 9 (pass)"
  739. " and 15 (pass and disconnect)\n"
  740. "Executing a continue without signal passing\n", 0);
  741. remcom_in_buffer[0] = 'c';
  742. }
  743. /* Indicate fall through */
  744. return -1;
  745. }
  746. /*
  747. * This function performs all gdbserial command procesing
  748. */
  749. int gdb_serial_stub(struct kgdb_state *ks)
  750. {
  751. int error = 0;
  752. int tmp;
  753. /* Clear the out buffer. */
  754. memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
  755. if (kgdb_connected) {
  756. unsigned char thref[8];
  757. char *ptr;
  758. /* Reply to host that an exception has occurred */
  759. ptr = remcom_out_buffer;
  760. *ptr++ = 'T';
  761. ptr = pack_hex_byte(ptr, ks->signo);
  762. ptr += strlen(strcpy(ptr, "thread:"));
  763. int_to_threadref(thref, shadow_pid(current->pid));
  764. ptr = pack_threadid(ptr, thref);
  765. *ptr++ = ';';
  766. put_packet(remcom_out_buffer);
  767. }
  768. kgdb_usethread = kgdb_info[ks->cpu].task;
  769. ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
  770. ks->pass_exception = 0;
  771. while (1) {
  772. error = 0;
  773. /* Clear the out buffer. */
  774. memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
  775. get_packet(remcom_in_buffer);
  776. switch (remcom_in_buffer[0]) {
  777. case '?': /* gdbserial status */
  778. gdb_cmd_status(ks);
  779. break;
  780. case 'g': /* return the value of the CPU registers */
  781. gdb_cmd_getregs(ks);
  782. break;
  783. case 'G': /* set the value of the CPU registers - return OK */
  784. gdb_cmd_setregs(ks);
  785. break;
  786. case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
  787. gdb_cmd_memread(ks);
  788. break;
  789. case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
  790. gdb_cmd_memwrite(ks);
  791. break;
  792. case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
  793. gdb_cmd_binwrite(ks);
  794. break;
  795. /* kill or detach. KGDB should treat this like a
  796. * continue.
  797. */
  798. case 'D': /* Debugger detach */
  799. case 'k': /* Debugger detach via kill */
  800. gdb_cmd_detachkill(ks);
  801. goto default_handle;
  802. case 'R': /* Reboot */
  803. if (gdb_cmd_reboot(ks))
  804. goto default_handle;
  805. break;
  806. case 'q': /* query command */
  807. gdb_cmd_query(ks);
  808. break;
  809. case 'H': /* task related */
  810. gdb_cmd_task(ks);
  811. break;
  812. case 'T': /* Query thread status */
  813. gdb_cmd_thread(ks);
  814. break;
  815. case 'z': /* Break point remove */
  816. case 'Z': /* Break point set */
  817. gdb_cmd_break(ks);
  818. break;
  819. #ifdef CONFIG_KGDB_KDB
  820. case '3': /* Escape into back into kdb */
  821. if (remcom_in_buffer[1] == '\0') {
  822. gdb_cmd_detachkill(ks);
  823. return DBG_PASS_EVENT;
  824. }
  825. #endif
  826. case 'C': /* Exception passing */
  827. tmp = gdb_cmd_exception_pass(ks);
  828. if (tmp > 0)
  829. goto default_handle;
  830. if (tmp == 0)
  831. break;
  832. /* Fall through on tmp < 0 */
  833. case 'c': /* Continue packet */
  834. case 's': /* Single step packet */
  835. if (kgdb_contthread && kgdb_contthread != current) {
  836. /* Can't switch threads in kgdb */
  837. error_packet(remcom_out_buffer, -EINVAL);
  838. break;
  839. }
  840. dbg_activate_sw_breakpoints();
  841. /* Fall through to default processing */
  842. default:
  843. default_handle:
  844. error = kgdb_arch_handle_exception(ks->ex_vector,
  845. ks->signo,
  846. ks->err_code,
  847. remcom_in_buffer,
  848. remcom_out_buffer,
  849. ks->linux_regs);
  850. /*
  851. * Leave cmd processing on error, detach,
  852. * kill, continue, or single step.
  853. */
  854. if (error >= 0 || remcom_in_buffer[0] == 'D' ||
  855. remcom_in_buffer[0] == 'k') {
  856. error = 0;
  857. goto kgdb_exit;
  858. }
  859. }
  860. /* reply to the request */
  861. put_packet(remcom_out_buffer);
  862. }
  863. kgdb_exit:
  864. if (ks->pass_exception)
  865. error = 1;
  866. return error;
  867. }
  868. int gdbstub_state(struct kgdb_state *ks, char *cmd)
  869. {
  870. int error;
  871. switch (cmd[0]) {
  872. case 'e':
  873. error = kgdb_arch_handle_exception(ks->ex_vector,
  874. ks->signo,
  875. ks->err_code,
  876. remcom_in_buffer,
  877. remcom_out_buffer,
  878. ks->linux_regs);
  879. return error;
  880. case 's':
  881. case 'c':
  882. strcpy(remcom_in_buffer, cmd);
  883. return 0;
  884. case '?':
  885. gdb_cmd_status(ks);
  886. break;
  887. case '\0':
  888. strcpy(remcom_out_buffer, "");
  889. break;
  890. }
  891. dbg_io_ops->write_char('+');
  892. put_packet(remcom_out_buffer);
  893. return 0;
  894. }