gdbstub.c 26 KB

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