gdbstub.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. /* 'O'utput */
  180. gdbmsgbuf[0] = 'O';
  181. /* Fill and send buffers... */
  182. while (len > 0) {
  183. bufptr = gdbmsgbuf + 1;
  184. /* Calculate how many this time */
  185. if ((len << 1) > (BUFMAX - 2))
  186. wcount = (BUFMAX - 2) >> 1;
  187. else
  188. wcount = len;
  189. /* Pack in hex chars */
  190. for (i = 0; i < wcount; i++)
  191. bufptr = pack_hex_byte(bufptr, s[i]);
  192. *bufptr = '\0';
  193. /* Move up */
  194. s += wcount;
  195. len -= wcount;
  196. /* Write packet */
  197. put_packet(gdbmsgbuf);
  198. }
  199. }
  200. /*
  201. * Convert the memory pointed to by mem into hex, placing result in
  202. * buf. Return a pointer to the last char put in buf (null). May
  203. * return an error.
  204. */
  205. int kgdb_mem2hex(char *mem, char *buf, int count)
  206. {
  207. char *tmp;
  208. int err;
  209. /*
  210. * We use the upper half of buf as an intermediate buffer for the
  211. * raw memory copy. Hex conversion will work against this one.
  212. */
  213. tmp = buf + count;
  214. err = probe_kernel_read(tmp, mem, count);
  215. if (!err) {
  216. while (count > 0) {
  217. buf = pack_hex_byte(buf, *tmp);
  218. tmp++;
  219. count--;
  220. }
  221. *buf = 0;
  222. }
  223. return err;
  224. }
  225. /*
  226. * Convert the hex array pointed to by buf into binary to be placed in
  227. * mem. Return a pointer to the character AFTER the last byte
  228. * written. May return an error.
  229. */
  230. int kgdb_hex2mem(char *buf, char *mem, int count)
  231. {
  232. char *tmp_raw;
  233. char *tmp_hex;
  234. /*
  235. * We use the upper half of buf as an intermediate buffer for the
  236. * raw memory that is converted from hex.
  237. */
  238. tmp_raw = buf + count * 2;
  239. tmp_hex = tmp_raw - 1;
  240. while (tmp_hex >= buf) {
  241. tmp_raw--;
  242. *tmp_raw = hex(*tmp_hex--);
  243. *tmp_raw |= hex(*tmp_hex--) << 4;
  244. }
  245. return probe_kernel_write(mem, tmp_raw, count);
  246. }
  247. /*
  248. * While we find nice hex chars, build a long_val.
  249. * Return number of chars processed.
  250. */
  251. int kgdb_hex2long(char **ptr, unsigned long *long_val)
  252. {
  253. int hex_val;
  254. int num = 0;
  255. int negate = 0;
  256. *long_val = 0;
  257. if (**ptr == '-') {
  258. negate = 1;
  259. (*ptr)++;
  260. }
  261. while (**ptr) {
  262. hex_val = hex(**ptr);
  263. if (hex_val < 0)
  264. break;
  265. *long_val = (*long_val << 4) | hex_val;
  266. num++;
  267. (*ptr)++;
  268. }
  269. if (negate)
  270. *long_val = -*long_val;
  271. return num;
  272. }
  273. /*
  274. * Copy the binary array pointed to by buf into mem. Fix $, #, and
  275. * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success.
  276. * The input buf is overwitten with the result to write to mem.
  277. */
  278. static int kgdb_ebin2mem(char *buf, char *mem, int count)
  279. {
  280. int size = 0;
  281. char *c = buf;
  282. while (count-- > 0) {
  283. c[size] = *buf++;
  284. if (c[size] == 0x7d)
  285. c[size] = *buf++ ^ 0x20;
  286. size++;
  287. }
  288. return probe_kernel_write(mem, c, size);
  289. }
  290. /* Write memory due to an 'M' or 'X' packet. */
  291. static int write_mem_msg(int binary)
  292. {
  293. char *ptr = &remcom_in_buffer[1];
  294. unsigned long addr;
  295. unsigned long length;
  296. int err;
  297. if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' &&
  298. kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') {
  299. if (binary)
  300. err = kgdb_ebin2mem(ptr, (char *)addr, length);
  301. else
  302. err = kgdb_hex2mem(ptr, (char *)addr, length);
  303. if (err)
  304. return err;
  305. if (CACHE_FLUSH_IS_SAFE)
  306. flush_icache_range(addr, addr + length);
  307. return 0;
  308. }
  309. return -EINVAL;
  310. }
  311. static void error_packet(char *pkt, int error)
  312. {
  313. error = -error;
  314. pkt[0] = 'E';
  315. pkt[1] = hex_asc[(error / 10)];
  316. pkt[2] = hex_asc[(error % 10)];
  317. pkt[3] = '\0';
  318. }
  319. /*
  320. * Thread ID accessors. We represent a flat TID space to GDB, where
  321. * the per CPU idle threads (which under Linux all have PID 0) are
  322. * remapped to negative TIDs.
  323. */
  324. #define BUF_THREAD_ID_SIZE 16
  325. static char *pack_threadid(char *pkt, unsigned char *id)
  326. {
  327. char *limit;
  328. limit = pkt + BUF_THREAD_ID_SIZE;
  329. while (pkt < limit)
  330. pkt = pack_hex_byte(pkt, *id++);
  331. return pkt;
  332. }
  333. static void int_to_threadref(unsigned char *id, int value)
  334. {
  335. unsigned char *scan;
  336. int i = 4;
  337. scan = (unsigned char *)id;
  338. while (i--)
  339. *scan++ = 0;
  340. put_unaligned_be32(value, scan);
  341. }
  342. static struct task_struct *getthread(struct pt_regs *regs, int tid)
  343. {
  344. /*
  345. * Non-positive TIDs are remapped to the cpu shadow information
  346. */
  347. if (tid == 0 || tid == -1)
  348. tid = -atomic_read(&kgdb_active) - 2;
  349. if (tid < -1 && tid > -NR_CPUS - 2) {
  350. if (kgdb_info[-tid - 2].task)
  351. return kgdb_info[-tid - 2].task;
  352. else
  353. return idle_task(-tid - 2);
  354. }
  355. if (tid <= 0) {
  356. printk(KERN_ERR "KGDB: Internal thread select error\n");
  357. dump_stack();
  358. return NULL;
  359. }
  360. /*
  361. * find_task_by_pid_ns() does not take the tasklist lock anymore
  362. * but is nicely RCU locked - hence is a pretty resilient
  363. * thing to use:
  364. */
  365. return find_task_by_pid_ns(tid, &init_pid_ns);
  366. }
  367. /*
  368. * Remap normal tasks to their real PID,
  369. * CPU shadow threads are mapped to -CPU - 2
  370. */
  371. static inline int shadow_pid(int realpid)
  372. {
  373. if (realpid)
  374. return realpid;
  375. return -raw_smp_processor_id() - 2;
  376. }
  377. /*
  378. * All the functions that start with gdb_cmd are the various
  379. * operations to implement the handlers for the gdbserial protocol
  380. * where KGDB is communicating with an external debugger
  381. */
  382. /* Handle the '?' status packets */
  383. static void gdb_cmd_status(struct kgdb_state *ks)
  384. {
  385. /*
  386. * We know that this packet is only sent
  387. * during initial connect. So to be safe,
  388. * we clear out our breakpoints now in case
  389. * GDB is reconnecting.
  390. */
  391. dbg_remove_all_break();
  392. remcom_out_buffer[0] = 'S';
  393. pack_hex_byte(&remcom_out_buffer[1], ks->signo);
  394. }
  395. /* Handle the 'g' get registers request */
  396. static void gdb_cmd_getregs(struct kgdb_state *ks)
  397. {
  398. struct task_struct *thread;
  399. void *local_debuggerinfo;
  400. int i;
  401. thread = kgdb_usethread;
  402. if (!thread) {
  403. thread = kgdb_info[ks->cpu].task;
  404. local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
  405. } else {
  406. local_debuggerinfo = NULL;
  407. for_each_online_cpu(i) {
  408. /*
  409. * Try to find the task on some other
  410. * or possibly this node if we do not
  411. * find the matching task then we try
  412. * to approximate the results.
  413. */
  414. if (thread == kgdb_info[i].task)
  415. local_debuggerinfo = kgdb_info[i].debuggerinfo;
  416. }
  417. }
  418. /*
  419. * All threads that don't have debuggerinfo should be
  420. * in schedule() sleeping, since all other CPUs
  421. * are in kgdb_wait, and thus have debuggerinfo.
  422. */
  423. if (local_debuggerinfo) {
  424. pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
  425. } else {
  426. /*
  427. * Pull stuff saved during switch_to; nothing
  428. * else is accessible (or even particularly
  429. * relevant).
  430. *
  431. * This should be enough for a stack trace.
  432. */
  433. sleeping_thread_to_gdb_regs(gdb_regs, thread);
  434. }
  435. kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
  436. }
  437. /* Handle the 'G' set registers request */
  438. static void gdb_cmd_setregs(struct kgdb_state *ks)
  439. {
  440. kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
  441. if (kgdb_usethread && kgdb_usethread != current) {
  442. error_packet(remcom_out_buffer, -EINVAL);
  443. } else {
  444. gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
  445. strcpy(remcom_out_buffer, "OK");
  446. }
  447. }
  448. /* Handle the 'm' memory read bytes */
  449. static void gdb_cmd_memread(struct kgdb_state *ks)
  450. {
  451. char *ptr = &remcom_in_buffer[1];
  452. unsigned long length;
  453. unsigned long addr;
  454. int err;
  455. if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
  456. kgdb_hex2long(&ptr, &length) > 0) {
  457. err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
  458. if (err)
  459. error_packet(remcom_out_buffer, err);
  460. } else {
  461. error_packet(remcom_out_buffer, -EINVAL);
  462. }
  463. }
  464. /* Handle the 'M' memory write bytes */
  465. static void gdb_cmd_memwrite(struct kgdb_state *ks)
  466. {
  467. int err = write_mem_msg(0);
  468. if (err)
  469. error_packet(remcom_out_buffer, err);
  470. else
  471. strcpy(remcom_out_buffer, "OK");
  472. }
  473. /* Handle the 'X' memory binary write bytes */
  474. static void gdb_cmd_binwrite(struct kgdb_state *ks)
  475. {
  476. int err = write_mem_msg(1);
  477. if (err)
  478. error_packet(remcom_out_buffer, err);
  479. else
  480. strcpy(remcom_out_buffer, "OK");
  481. }
  482. /* Handle the 'D' or 'k', detach or kill packets */
  483. static void gdb_cmd_detachkill(struct kgdb_state *ks)
  484. {
  485. int error;
  486. /* The detach case */
  487. if (remcom_in_buffer[0] == 'D') {
  488. error = dbg_remove_all_break();
  489. if (error < 0) {
  490. error_packet(remcom_out_buffer, error);
  491. } else {
  492. strcpy(remcom_out_buffer, "OK");
  493. kgdb_connected = 0;
  494. }
  495. put_packet(remcom_out_buffer);
  496. } else {
  497. /*
  498. * Assume the kill case, with no exit code checking,
  499. * trying to force detach the debugger:
  500. */
  501. dbg_remove_all_break();
  502. kgdb_connected = 0;
  503. }
  504. }
  505. /* Handle the 'R' reboot packets */
  506. static int gdb_cmd_reboot(struct kgdb_state *ks)
  507. {
  508. /* For now, only honor R0 */
  509. if (strcmp(remcom_in_buffer, "R0") == 0) {
  510. printk(KERN_CRIT "Executing emergency reboot\n");
  511. strcpy(remcom_out_buffer, "OK");
  512. put_packet(remcom_out_buffer);
  513. /*
  514. * Execution should not return from
  515. * machine_emergency_restart()
  516. */
  517. machine_emergency_restart();
  518. kgdb_connected = 0;
  519. return 1;
  520. }
  521. return 0;
  522. }
  523. /* Handle the 'q' query packets */
  524. static void gdb_cmd_query(struct kgdb_state *ks)
  525. {
  526. struct task_struct *g;
  527. struct task_struct *p;
  528. unsigned char thref[8];
  529. char *ptr;
  530. int i;
  531. int cpu;
  532. int finished = 0;
  533. switch (remcom_in_buffer[1]) {
  534. case 's':
  535. case 'f':
  536. if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) {
  537. error_packet(remcom_out_buffer, -EINVAL);
  538. break;
  539. }
  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. error_packet(remcom_out_buffer, -EINVAL);
  578. break;
  579. }
  580. ks->threadid = 0;
  581. ptr = remcom_in_buffer + 17;
  582. kgdb_hex2long(&ptr, &ks->threadid);
  583. if (!getthread(ks->linux_regs, ks->threadid)) {
  584. error_packet(remcom_out_buffer, -EINVAL);
  585. break;
  586. }
  587. if ((int)ks->threadid > 0) {
  588. kgdb_mem2hex(getthread(ks->linux_regs,
  589. ks->threadid)->comm,
  590. remcom_out_buffer, 16);
  591. } else {
  592. static char tmpstr[23 + BUF_THREAD_ID_SIZE];
  593. sprintf(tmpstr, "shadowCPU%d",
  594. (int)(-ks->threadid - 2));
  595. kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
  596. }
  597. break;
  598. }
  599. }
  600. /* Handle the 'H' task query packets */
  601. static void gdb_cmd_task(struct kgdb_state *ks)
  602. {
  603. struct task_struct *thread;
  604. char *ptr;
  605. switch (remcom_in_buffer[1]) {
  606. case 'g':
  607. ptr = &remcom_in_buffer[2];
  608. kgdb_hex2long(&ptr, &ks->threadid);
  609. thread = getthread(ks->linux_regs, ks->threadid);
  610. if (!thread && ks->threadid > 0) {
  611. error_packet(remcom_out_buffer, -EINVAL);
  612. break;
  613. }
  614. kgdb_usethread = thread;
  615. ks->kgdb_usethreadid = ks->threadid;
  616. strcpy(remcom_out_buffer, "OK");
  617. break;
  618. case 'c':
  619. ptr = &remcom_in_buffer[2];
  620. kgdb_hex2long(&ptr, &ks->threadid);
  621. if (!ks->threadid) {
  622. kgdb_contthread = NULL;
  623. } else {
  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_contthread = thread;
  630. }
  631. strcpy(remcom_out_buffer, "OK");
  632. break;
  633. }
  634. }
  635. /* Handle the 'T' thread query packets */
  636. static void gdb_cmd_thread(struct kgdb_state *ks)
  637. {
  638. char *ptr = &remcom_in_buffer[1];
  639. struct task_struct *thread;
  640. kgdb_hex2long(&ptr, &ks->threadid);
  641. thread = getthread(ks->linux_regs, ks->threadid);
  642. if (thread)
  643. strcpy(remcom_out_buffer, "OK");
  644. else
  645. error_packet(remcom_out_buffer, -EINVAL);
  646. }
  647. /* Handle the 'z' or 'Z' breakpoint remove or set packets */
  648. static void gdb_cmd_break(struct kgdb_state *ks)
  649. {
  650. /*
  651. * Since GDB-5.3, it's been drafted that '0' is a software
  652. * breakpoint, '1' is a hardware breakpoint, so let's do that.
  653. */
  654. char *bpt_type = &remcom_in_buffer[1];
  655. char *ptr = &remcom_in_buffer[2];
  656. unsigned long addr;
  657. unsigned long length;
  658. int error = 0;
  659. if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
  660. /* Unsupported */
  661. if (*bpt_type > '4')
  662. return;
  663. } else {
  664. if (*bpt_type != '0' && *bpt_type != '1')
  665. /* Unsupported. */
  666. return;
  667. }
  668. /*
  669. * Test if this is a hardware breakpoint, and
  670. * if we support it:
  671. */
  672. if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
  673. /* Unsupported. */
  674. return;
  675. if (*(ptr++) != ',') {
  676. error_packet(remcom_out_buffer, -EINVAL);
  677. return;
  678. }
  679. if (!kgdb_hex2long(&ptr, &addr)) {
  680. error_packet(remcom_out_buffer, -EINVAL);
  681. return;
  682. }
  683. if (*(ptr++) != ',' ||
  684. !kgdb_hex2long(&ptr, &length)) {
  685. error_packet(remcom_out_buffer, -EINVAL);
  686. return;
  687. }
  688. if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
  689. error = dbg_set_sw_break(addr);
  690. else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
  691. error = dbg_remove_sw_break(addr);
  692. else if (remcom_in_buffer[0] == 'Z')
  693. error = arch_kgdb_ops.set_hw_breakpoint(addr,
  694. (int)length, *bpt_type - '0');
  695. else if (remcom_in_buffer[0] == 'z')
  696. error = arch_kgdb_ops.remove_hw_breakpoint(addr,
  697. (int) length, *bpt_type - '0');
  698. if (error == 0)
  699. strcpy(remcom_out_buffer, "OK");
  700. else
  701. error_packet(remcom_out_buffer, error);
  702. }
  703. /* Handle the 'C' signal / exception passing packets */
  704. static int gdb_cmd_exception_pass(struct kgdb_state *ks)
  705. {
  706. /* C09 == pass exception
  707. * C15 == detach kgdb, pass exception
  708. */
  709. if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
  710. ks->pass_exception = 1;
  711. remcom_in_buffer[0] = 'c';
  712. } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
  713. ks->pass_exception = 1;
  714. remcom_in_buffer[0] = 'D';
  715. dbg_remove_all_break();
  716. kgdb_connected = 0;
  717. return 1;
  718. } else {
  719. gdbstub_msg_write("KGDB only knows signal 9 (pass)"
  720. " and 15 (pass and disconnect)\n"
  721. "Executing a continue without signal passing\n", 0);
  722. remcom_in_buffer[0] = 'c';
  723. }
  724. /* Indicate fall through */
  725. return -1;
  726. }
  727. /*
  728. * This function performs all gdbserial command procesing
  729. */
  730. int gdb_serial_stub(struct kgdb_state *ks)
  731. {
  732. int error = 0;
  733. int tmp;
  734. /* Clear the out buffer. */
  735. memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
  736. if (kgdb_connected) {
  737. unsigned char thref[8];
  738. char *ptr;
  739. /* Reply to host that an exception has occurred */
  740. ptr = remcom_out_buffer;
  741. *ptr++ = 'T';
  742. ptr = pack_hex_byte(ptr, ks->signo);
  743. ptr += strlen(strcpy(ptr, "thread:"));
  744. int_to_threadref(thref, shadow_pid(current->pid));
  745. ptr = pack_threadid(ptr, thref);
  746. *ptr++ = ';';
  747. put_packet(remcom_out_buffer);
  748. }
  749. kgdb_usethread = kgdb_info[ks->cpu].task;
  750. ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
  751. ks->pass_exception = 0;
  752. while (1) {
  753. error = 0;
  754. /* Clear the out buffer. */
  755. memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
  756. get_packet(remcom_in_buffer);
  757. switch (remcom_in_buffer[0]) {
  758. case '?': /* gdbserial status */
  759. gdb_cmd_status(ks);
  760. break;
  761. case 'g': /* return the value of the CPU registers */
  762. gdb_cmd_getregs(ks);
  763. break;
  764. case 'G': /* set the value of the CPU registers - return OK */
  765. gdb_cmd_setregs(ks);
  766. break;
  767. case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
  768. gdb_cmd_memread(ks);
  769. break;
  770. case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
  771. gdb_cmd_memwrite(ks);
  772. break;
  773. case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
  774. gdb_cmd_binwrite(ks);
  775. break;
  776. /* kill or detach. KGDB should treat this like a
  777. * continue.
  778. */
  779. case 'D': /* Debugger detach */
  780. case 'k': /* Debugger detach via kill */
  781. gdb_cmd_detachkill(ks);
  782. goto default_handle;
  783. case 'R': /* Reboot */
  784. if (gdb_cmd_reboot(ks))
  785. goto default_handle;
  786. break;
  787. case 'q': /* query command */
  788. gdb_cmd_query(ks);
  789. break;
  790. case 'H': /* task related */
  791. gdb_cmd_task(ks);
  792. break;
  793. case 'T': /* Query thread status */
  794. gdb_cmd_thread(ks);
  795. break;
  796. case 'z': /* Break point remove */
  797. case 'Z': /* Break point set */
  798. gdb_cmd_break(ks);
  799. break;
  800. #ifdef CONFIG_KGDB_KDB
  801. case '3': /* Escape into back into kdb */
  802. if (remcom_in_buffer[1] == '\0') {
  803. gdb_cmd_detachkill(ks);
  804. return DBG_PASS_EVENT;
  805. }
  806. #endif
  807. case 'C': /* Exception passing */
  808. tmp = gdb_cmd_exception_pass(ks);
  809. if (tmp > 0)
  810. goto default_handle;
  811. if (tmp == 0)
  812. break;
  813. /* Fall through on tmp < 0 */
  814. case 'c': /* Continue packet */
  815. case 's': /* Single step packet */
  816. if (kgdb_contthread && kgdb_contthread != current) {
  817. /* Can't switch threads in kgdb */
  818. error_packet(remcom_out_buffer, -EINVAL);
  819. break;
  820. }
  821. dbg_activate_sw_breakpoints();
  822. /* Fall through to default processing */
  823. default:
  824. default_handle:
  825. error = kgdb_arch_handle_exception(ks->ex_vector,
  826. ks->signo,
  827. ks->err_code,
  828. remcom_in_buffer,
  829. remcom_out_buffer,
  830. ks->linux_regs);
  831. /*
  832. * Leave cmd processing on error, detach,
  833. * kill, continue, or single step.
  834. */
  835. if (error >= 0 || remcom_in_buffer[0] == 'D' ||
  836. remcom_in_buffer[0] == 'k') {
  837. error = 0;
  838. goto kgdb_exit;
  839. }
  840. }
  841. /* reply to the request */
  842. put_packet(remcom_out_buffer);
  843. }
  844. kgdb_exit:
  845. if (ks->pass_exception)
  846. error = 1;
  847. return error;
  848. }
  849. int gdbstub_state(struct kgdb_state *ks, char *cmd)
  850. {
  851. int error;
  852. switch (cmd[0]) {
  853. case 'e':
  854. error = kgdb_arch_handle_exception(ks->ex_vector,
  855. ks->signo,
  856. ks->err_code,
  857. remcom_in_buffer,
  858. remcom_out_buffer,
  859. ks->linux_regs);
  860. return error;
  861. case 's':
  862. case 'c':
  863. strcpy(remcom_in_buffer, cmd);
  864. return 0;
  865. case '?':
  866. gdb_cmd_status(ks);
  867. break;
  868. case '\0':
  869. strcpy(remcom_out_buffer, "");
  870. break;
  871. }
  872. dbg_io_ops->write_char('+');
  873. put_packet(remcom_out_buffer);
  874. return 0;
  875. }