gdbstub.c 25 KB

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