gdbstub.c 24 KB

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