gdbstub.c 24 KB

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