gdbstub.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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@ucw.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. break;
  540. i = 0;
  541. remcom_out_buffer[0] = 'm';
  542. ptr = remcom_out_buffer + 1;
  543. if (remcom_in_buffer[1] == 'f') {
  544. /* Each cpu is a shadow thread */
  545. for_each_online_cpu(cpu) {
  546. ks->thr_query = 0;
  547. int_to_threadref(thref, -cpu - 2);
  548. pack_threadid(ptr, thref);
  549. ptr += BUF_THREAD_ID_SIZE;
  550. *(ptr++) = ',';
  551. i++;
  552. }
  553. }
  554. do_each_thread(g, p) {
  555. if (i >= ks->thr_query && !finished) {
  556. int_to_threadref(thref, p->pid);
  557. pack_threadid(ptr, thref);
  558. ptr += BUF_THREAD_ID_SIZE;
  559. *(ptr++) = ',';
  560. ks->thr_query++;
  561. if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0)
  562. finished = 1;
  563. }
  564. i++;
  565. } while_each_thread(g, p);
  566. *(--ptr) = '\0';
  567. break;
  568. case 'C':
  569. /* Current thread id */
  570. strcpy(remcom_out_buffer, "QC");
  571. ks->threadid = shadow_pid(current->pid);
  572. int_to_threadref(thref, ks->threadid);
  573. pack_threadid(remcom_out_buffer + 2, thref);
  574. break;
  575. case 'T':
  576. if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16))
  577. break;
  578. ks->threadid = 0;
  579. ptr = remcom_in_buffer + 17;
  580. kgdb_hex2long(&ptr, &ks->threadid);
  581. if (!getthread(ks->linux_regs, ks->threadid)) {
  582. error_packet(remcom_out_buffer, -EINVAL);
  583. break;
  584. }
  585. if ((int)ks->threadid > 0) {
  586. kgdb_mem2hex(getthread(ks->linux_regs,
  587. ks->threadid)->comm,
  588. remcom_out_buffer, 16);
  589. } else {
  590. static char tmpstr[23 + BUF_THREAD_ID_SIZE];
  591. sprintf(tmpstr, "shadowCPU%d",
  592. (int)(-ks->threadid - 2));
  593. kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
  594. }
  595. break;
  596. #ifdef CONFIG_KGDB_KDB
  597. case 'R':
  598. if (strncmp(remcom_in_buffer, "qRcmd,", 6) == 0) {
  599. int len = strlen(remcom_in_buffer + 6);
  600. if ((len % 2) != 0) {
  601. strcpy(remcom_out_buffer, "E01");
  602. break;
  603. }
  604. kgdb_hex2mem(remcom_in_buffer + 6,
  605. remcom_out_buffer, len);
  606. len = len / 2;
  607. remcom_out_buffer[len++] = 0;
  608. kdb_parse(remcom_out_buffer);
  609. strcpy(remcom_out_buffer, "OK");
  610. }
  611. break;
  612. #endif
  613. }
  614. }
  615. /* Handle the 'H' task query packets */
  616. static void gdb_cmd_task(struct kgdb_state *ks)
  617. {
  618. struct task_struct *thread;
  619. char *ptr;
  620. switch (remcom_in_buffer[1]) {
  621. case 'g':
  622. ptr = &remcom_in_buffer[2];
  623. kgdb_hex2long(&ptr, &ks->threadid);
  624. thread = getthread(ks->linux_regs, ks->threadid);
  625. if (!thread && ks->threadid > 0) {
  626. error_packet(remcom_out_buffer, -EINVAL);
  627. break;
  628. }
  629. kgdb_usethread = thread;
  630. ks->kgdb_usethreadid = ks->threadid;
  631. strcpy(remcom_out_buffer, "OK");
  632. break;
  633. case 'c':
  634. ptr = &remcom_in_buffer[2];
  635. kgdb_hex2long(&ptr, &ks->threadid);
  636. if (!ks->threadid) {
  637. kgdb_contthread = NULL;
  638. } else {
  639. thread = getthread(ks->linux_regs, ks->threadid);
  640. if (!thread && ks->threadid > 0) {
  641. error_packet(remcom_out_buffer, -EINVAL);
  642. break;
  643. }
  644. kgdb_contthread = thread;
  645. }
  646. strcpy(remcom_out_buffer, "OK");
  647. break;
  648. }
  649. }
  650. /* Handle the 'T' thread query packets */
  651. static void gdb_cmd_thread(struct kgdb_state *ks)
  652. {
  653. char *ptr = &remcom_in_buffer[1];
  654. struct task_struct *thread;
  655. kgdb_hex2long(&ptr, &ks->threadid);
  656. thread = getthread(ks->linux_regs, ks->threadid);
  657. if (thread)
  658. strcpy(remcom_out_buffer, "OK");
  659. else
  660. error_packet(remcom_out_buffer, -EINVAL);
  661. }
  662. /* Handle the 'z' or 'Z' breakpoint remove or set packets */
  663. static void gdb_cmd_break(struct kgdb_state *ks)
  664. {
  665. /*
  666. * Since GDB-5.3, it's been drafted that '0' is a software
  667. * breakpoint, '1' is a hardware breakpoint, so let's do that.
  668. */
  669. char *bpt_type = &remcom_in_buffer[1];
  670. char *ptr = &remcom_in_buffer[2];
  671. unsigned long addr;
  672. unsigned long length;
  673. int error = 0;
  674. if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
  675. /* Unsupported */
  676. if (*bpt_type > '4')
  677. return;
  678. } else {
  679. if (*bpt_type != '0' && *bpt_type != '1')
  680. /* Unsupported. */
  681. return;
  682. }
  683. /*
  684. * Test if this is a hardware breakpoint, and
  685. * if we support it:
  686. */
  687. if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
  688. /* Unsupported. */
  689. return;
  690. if (*(ptr++) != ',') {
  691. error_packet(remcom_out_buffer, -EINVAL);
  692. return;
  693. }
  694. if (!kgdb_hex2long(&ptr, &addr)) {
  695. error_packet(remcom_out_buffer, -EINVAL);
  696. return;
  697. }
  698. if (*(ptr++) != ',' ||
  699. !kgdb_hex2long(&ptr, &length)) {
  700. error_packet(remcom_out_buffer, -EINVAL);
  701. return;
  702. }
  703. if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
  704. error = dbg_set_sw_break(addr);
  705. else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
  706. error = dbg_remove_sw_break(addr);
  707. else if (remcom_in_buffer[0] == 'Z')
  708. error = arch_kgdb_ops.set_hw_breakpoint(addr,
  709. (int)length, *bpt_type - '0');
  710. else if (remcom_in_buffer[0] == 'z')
  711. error = arch_kgdb_ops.remove_hw_breakpoint(addr,
  712. (int) length, *bpt_type - '0');
  713. if (error == 0)
  714. strcpy(remcom_out_buffer, "OK");
  715. else
  716. error_packet(remcom_out_buffer, error);
  717. }
  718. /* Handle the 'C' signal / exception passing packets */
  719. static int gdb_cmd_exception_pass(struct kgdb_state *ks)
  720. {
  721. /* C09 == pass exception
  722. * C15 == detach kgdb, pass exception
  723. */
  724. if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
  725. ks->pass_exception = 1;
  726. remcom_in_buffer[0] = 'c';
  727. } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
  728. ks->pass_exception = 1;
  729. remcom_in_buffer[0] = 'D';
  730. dbg_remove_all_break();
  731. kgdb_connected = 0;
  732. return 1;
  733. } else {
  734. gdbstub_msg_write("KGDB only knows signal 9 (pass)"
  735. " and 15 (pass and disconnect)\n"
  736. "Executing a continue without signal passing\n", 0);
  737. remcom_in_buffer[0] = 'c';
  738. }
  739. /* Indicate fall through */
  740. return -1;
  741. }
  742. /*
  743. * This function performs all gdbserial command procesing
  744. */
  745. int gdb_serial_stub(struct kgdb_state *ks)
  746. {
  747. int error = 0;
  748. int tmp;
  749. /* Clear the out buffer. */
  750. memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
  751. if (kgdb_connected) {
  752. unsigned char thref[8];
  753. char *ptr;
  754. /* Reply to host that an exception has occurred */
  755. ptr = remcom_out_buffer;
  756. *ptr++ = 'T';
  757. ptr = pack_hex_byte(ptr, ks->signo);
  758. ptr += strlen(strcpy(ptr, "thread:"));
  759. int_to_threadref(thref, shadow_pid(current->pid));
  760. ptr = pack_threadid(ptr, thref);
  761. *ptr++ = ';';
  762. put_packet(remcom_out_buffer);
  763. }
  764. kgdb_usethread = kgdb_info[ks->cpu].task;
  765. ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
  766. ks->pass_exception = 0;
  767. while (1) {
  768. error = 0;
  769. /* Clear the out buffer. */
  770. memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
  771. get_packet(remcom_in_buffer);
  772. switch (remcom_in_buffer[0]) {
  773. case '?': /* gdbserial status */
  774. gdb_cmd_status(ks);
  775. break;
  776. case 'g': /* return the value of the CPU registers */
  777. gdb_cmd_getregs(ks);
  778. break;
  779. case 'G': /* set the value of the CPU registers - return OK */
  780. gdb_cmd_setregs(ks);
  781. break;
  782. case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
  783. gdb_cmd_memread(ks);
  784. break;
  785. case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
  786. gdb_cmd_memwrite(ks);
  787. break;
  788. case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
  789. gdb_cmd_binwrite(ks);
  790. break;
  791. /* kill or detach. KGDB should treat this like a
  792. * continue.
  793. */
  794. case 'D': /* Debugger detach */
  795. case 'k': /* Debugger detach via kill */
  796. gdb_cmd_detachkill(ks);
  797. goto default_handle;
  798. case 'R': /* Reboot */
  799. if (gdb_cmd_reboot(ks))
  800. goto default_handle;
  801. break;
  802. case 'q': /* query command */
  803. gdb_cmd_query(ks);
  804. break;
  805. case 'H': /* task related */
  806. gdb_cmd_task(ks);
  807. break;
  808. case 'T': /* Query thread status */
  809. gdb_cmd_thread(ks);
  810. break;
  811. case 'z': /* Break point remove */
  812. case 'Z': /* Break point set */
  813. gdb_cmd_break(ks);
  814. break;
  815. #ifdef CONFIG_KGDB_KDB
  816. case '3': /* Escape into back into kdb */
  817. if (remcom_in_buffer[1] == '\0') {
  818. gdb_cmd_detachkill(ks);
  819. return DBG_PASS_EVENT;
  820. }
  821. #endif
  822. case 'C': /* Exception passing */
  823. tmp = gdb_cmd_exception_pass(ks);
  824. if (tmp > 0)
  825. goto default_handle;
  826. if (tmp == 0)
  827. break;
  828. /* Fall through on tmp < 0 */
  829. case 'c': /* Continue packet */
  830. case 's': /* Single step packet */
  831. if (kgdb_contthread && kgdb_contthread != current) {
  832. /* Can't switch threads in kgdb */
  833. error_packet(remcom_out_buffer, -EINVAL);
  834. break;
  835. }
  836. dbg_activate_sw_breakpoints();
  837. /* Fall through to default processing */
  838. default:
  839. default_handle:
  840. error = kgdb_arch_handle_exception(ks->ex_vector,
  841. ks->signo,
  842. ks->err_code,
  843. remcom_in_buffer,
  844. remcom_out_buffer,
  845. ks->linux_regs);
  846. /*
  847. * Leave cmd processing on error, detach,
  848. * kill, continue, or single step.
  849. */
  850. if (error >= 0 || remcom_in_buffer[0] == 'D' ||
  851. remcom_in_buffer[0] == 'k') {
  852. error = 0;
  853. goto kgdb_exit;
  854. }
  855. }
  856. /* reply to the request */
  857. put_packet(remcom_out_buffer);
  858. }
  859. kgdb_exit:
  860. if (ks->pass_exception)
  861. error = 1;
  862. return error;
  863. }
  864. int gdbstub_state(struct kgdb_state *ks, char *cmd)
  865. {
  866. int error;
  867. switch (cmd[0]) {
  868. case 'e':
  869. error = kgdb_arch_handle_exception(ks->ex_vector,
  870. ks->signo,
  871. ks->err_code,
  872. remcom_in_buffer,
  873. remcom_out_buffer,
  874. ks->linux_regs);
  875. return error;
  876. case 's':
  877. case 'c':
  878. strcpy(remcom_in_buffer, cmd);
  879. return 0;
  880. case '?':
  881. gdb_cmd_status(ks);
  882. break;
  883. case '\0':
  884. strcpy(remcom_out_buffer, "");
  885. break;
  886. }
  887. dbg_io_ops->write_char('+');
  888. put_packet(remcom_out_buffer);
  889. return 0;
  890. }