gdbstub.c 25 KB

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