gdbstub.c 22 KB

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