4 * Maintainer: Jason Wessel <jason.wessel@windriver.com>
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>
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.
23 * Original KGDB stub: David Grothe <dave@gcom.com>,
24 * Tigran Aivazian <tigran@sco.com>
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.
31 #include <linux/kernel.h>
32 #include <linux/sched/signal.h>
33 #include <linux/kgdb.h>
34 #include <linux/kdb.h>
35 #include <linux/serial_core.h>
36 #include <linux/reboot.h>
37 #include <linux/uaccess.h>
38 #include <asm/cacheflush.h>
39 #include <asm/unaligned.h>
40 #include "debug_core.h"
42 #define KGDB_MAX_THREAD_QUERY 17
44 /* Our I/O buffers. */
45 static char remcom_in_buffer[BUFMAX];
46 static char remcom_out_buffer[BUFMAX];
47 static int gdbstub_use_prev_in_buf;
48 static int gdbstub_prev_in_buf_pos;
50 /* Storage for the registers, in GDB format. */
51 static unsigned long gdb_regs[(NUMREGBYTES +
52 sizeof(unsigned long) - 1) /
53 sizeof(unsigned long)];
56 * GDB remote protocol parser:
59 #ifdef CONFIG_KGDB_KDB
60 static int gdbstub_read_wait(void)
65 if (unlikely(gdbstub_use_prev_in_buf)) {
66 if (gdbstub_prev_in_buf_pos < gdbstub_use_prev_in_buf)
67 return remcom_in_buffer[gdbstub_prev_in_buf_pos++];
69 gdbstub_use_prev_in_buf = 0;
72 /* poll any additional I/O interfaces that are defined */
74 for (i = 0; kdb_poll_funcs[i] != NULL; i++) {
75 ret = kdb_poll_funcs[i]();
82 static int gdbstub_read_wait(void)
84 int ret = dbg_io_ops->read_char();
85 while (ret == NO_POLL_CHAR)
86 ret = dbg_io_ops->read_char();
90 /* scan for the sequence $<data>#<checksum> */
91 static void get_packet(char *buffer)
93 unsigned char checksum;
94 unsigned char xmitcsum;
100 * Spin and wait around for the start character, ignore all
103 while ((ch = (gdbstub_read_wait())) != '$')
113 * now, read until a # or end of buffer is found:
115 while (count < (BUFMAX - 1)) {
116 ch = gdbstub_read_wait();
119 checksum = checksum + ch;
125 xmitcsum = hex_to_bin(gdbstub_read_wait()) << 4;
126 xmitcsum += hex_to_bin(gdbstub_read_wait());
128 if (checksum != xmitcsum)
129 /* failed checksum */
130 dbg_io_ops->write_char('-');
132 /* successful transfer */
133 dbg_io_ops->write_char('+');
134 if (dbg_io_ops->flush)
138 } while (checksum != xmitcsum);
142 * Send the packet in buffer.
143 * Check for gdb connection if asked for.
145 static void put_packet(char *buffer)
147 unsigned char checksum;
152 * $<packet info>#<checksum>.
155 dbg_io_ops->write_char('$');
159 while ((ch = buffer[count])) {
160 dbg_io_ops->write_char(ch);
165 dbg_io_ops->write_char('#');
166 dbg_io_ops->write_char(hex_asc_hi(checksum));
167 dbg_io_ops->write_char(hex_asc_lo(checksum));
168 if (dbg_io_ops->flush)
171 /* Now see what we get in reply. */
172 ch = gdbstub_read_wait();
175 ch = gdbstub_read_wait();
177 /* If we get an ACK, we are done. */
182 * If we get the start of another packet, this means
183 * that GDB is attempting to reconnect. We will NAK
184 * the packet being sent, and stop trying to send this
188 dbg_io_ops->write_char('-');
189 if (dbg_io_ops->flush)
196 static char gdbmsgbuf[BUFMAX + 1];
198 void gdbstub_msg_write(const char *s, int len)
210 /* Fill and send buffers... */
212 bufptr = gdbmsgbuf + 1;
214 /* Calculate how many this time */
215 if ((len << 1) > (BUFMAX - 2))
216 wcount = (BUFMAX - 2) >> 1;
220 /* Pack in hex chars */
221 for (i = 0; i < wcount; i++)
222 bufptr = hex_byte_pack(bufptr, s[i]);
230 put_packet(gdbmsgbuf);
235 * Convert the memory pointed to by mem into hex, placing result in
236 * buf. Return a pointer to the last char put in buf (null). May
239 char *kgdb_mem2hex(char *mem, char *buf, int count)
245 * We use the upper half of buf as an intermediate buffer for the
246 * raw memory copy. Hex conversion will work against this one.
250 err = copy_from_kernel_nofault(tmp, mem, count);
254 buf = hex_byte_pack(buf, *tmp);
264 * Convert the hex array pointed to by buf into binary to be placed in
265 * mem. Return a pointer to the character AFTER the last byte
266 * written. May return an error.
268 int kgdb_hex2mem(char *buf, char *mem, int count)
274 * We use the upper half of buf as an intermediate buffer for the
275 * raw memory that is converted from hex.
277 tmp_raw = buf + count * 2;
279 tmp_hex = tmp_raw - 1;
280 while (tmp_hex >= buf) {
282 *tmp_raw = hex_to_bin(*tmp_hex--);
283 *tmp_raw |= hex_to_bin(*tmp_hex--) << 4;
286 return copy_to_kernel_nofault(mem, tmp_raw, count);
290 * While we find nice hex chars, build a long_val.
291 * Return number of chars processed.
293 int kgdb_hex2long(char **ptr, unsigned long *long_val)
306 hex_val = hex_to_bin(**ptr);
310 *long_val = (*long_val << 4) | hex_val;
316 *long_val = -*long_val;
322 * Copy the binary array pointed to by buf into mem. Fix $, #, and
323 * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success.
324 * The input buf is overwritten with the result to write to mem.
326 static int kgdb_ebin2mem(char *buf, char *mem, int count)
331 while (count-- > 0) {
334 c[size] = *buf++ ^ 0x20;
338 return copy_to_kernel_nofault(mem, c, size);
341 #if DBG_MAX_REG_NUM > 0
342 void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
346 char *ptr = (char *)gdb_regs;
348 for (i = 0; i < DBG_MAX_REG_NUM; i++) {
349 dbg_get_reg(i, ptr + idx, regs);
350 idx += dbg_reg_def[i].size;
354 void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
358 char *ptr = (char *)gdb_regs;
360 for (i = 0; i < DBG_MAX_REG_NUM; i++) {
361 dbg_set_reg(i, ptr + idx, regs);
362 idx += dbg_reg_def[i].size;
365 #endif /* DBG_MAX_REG_NUM > 0 */
367 /* Write memory due to an 'M' or 'X' packet. */
368 static int write_mem_msg(int binary)
370 char *ptr = &remcom_in_buffer[1];
372 unsigned long length;
375 if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' &&
376 kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') {
378 err = kgdb_ebin2mem(ptr, (char *)addr, length);
380 err = kgdb_hex2mem(ptr, (char *)addr, length);
383 if (CACHE_FLUSH_IS_SAFE)
384 flush_icache_range(addr, addr + length);
391 static void error_packet(char *pkt, int error)
395 pkt[1] = hex_asc[(error / 10)];
396 pkt[2] = hex_asc[(error % 10)];
401 * Thread ID accessors. We represent a flat TID space to GDB, where
402 * the per CPU idle threads (which under Linux all have PID 0) are
403 * remapped to negative TIDs.
406 #define BUF_THREAD_ID_SIZE 8
408 static char *pack_threadid(char *pkt, unsigned char *id)
410 unsigned char *limit;
413 limit = id + (BUF_THREAD_ID_SIZE / 2);
415 if (!lzero || *id != 0) {
416 pkt = hex_byte_pack(pkt, *id);
423 pkt = hex_byte_pack(pkt, 0);
428 static void int_to_threadref(unsigned char *id, int value)
430 put_unaligned_be32(value, id);
433 static struct task_struct *getthread(struct pt_regs *regs, int tid)
436 * Non-positive TIDs are remapped to the cpu shadow information
438 if (tid == 0 || tid == -1)
439 tid = -atomic_read(&kgdb_active) - 2;
440 if (tid < -1 && tid > -NR_CPUS - 2) {
441 if (kgdb_info[-tid - 2].task)
442 return kgdb_info[-tid - 2].task;
444 return idle_task(-tid - 2);
447 printk(KERN_ERR "KGDB: Internal thread select error\n");
453 * find_task_by_pid_ns() does not take the tasklist lock anymore
454 * but is nicely RCU locked - hence is a pretty resilient
457 return find_task_by_pid_ns(tid, &init_pid_ns);
462 * Remap normal tasks to their real PID,
463 * CPU shadow threads are mapped to -CPU - 2
465 static inline int shadow_pid(int realpid)
470 return -raw_smp_processor_id() - 2;
474 * All the functions that start with gdb_cmd are the various
475 * operations to implement the handlers for the gdbserial protocol
476 * where KGDB is communicating with an external debugger
479 /* Handle the '?' status packets */
480 static void gdb_cmd_status(struct kgdb_state *ks)
483 * We know that this packet is only sent
484 * during initial connect. So to be safe,
485 * we clear out our breakpoints now in case
486 * GDB is reconnecting.
488 dbg_remove_all_break();
490 remcom_out_buffer[0] = 'S';
491 hex_byte_pack(&remcom_out_buffer[1], ks->signo);
494 static void gdb_get_regs_helper(struct kgdb_state *ks)
496 struct task_struct *thread;
497 void *local_debuggerinfo;
500 thread = kgdb_usethread;
502 thread = kgdb_info[ks->cpu].task;
503 local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
505 local_debuggerinfo = NULL;
506 for_each_online_cpu(i) {
508 * Try to find the task on some other
509 * or possibly this node if we do not
510 * find the matching task then we try
511 * to approximate the results.
513 if (thread == kgdb_info[i].task)
514 local_debuggerinfo = kgdb_info[i].debuggerinfo;
519 * All threads that don't have debuggerinfo should be
520 * in schedule() sleeping, since all other CPUs
521 * are in kgdb_wait, and thus have debuggerinfo.
523 if (local_debuggerinfo) {
524 pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
527 * Pull stuff saved during switch_to; nothing
528 * else is accessible (or even particularly
531 * This should be enough for a stack trace.
533 sleeping_thread_to_gdb_regs(gdb_regs, thread);
537 /* Handle the 'g' get registers request */
538 static void gdb_cmd_getregs(struct kgdb_state *ks)
540 gdb_get_regs_helper(ks);
541 kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
544 /* Handle the 'G' set registers request */
545 static void gdb_cmd_setregs(struct kgdb_state *ks)
547 kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
549 if (kgdb_usethread && kgdb_usethread != current) {
550 error_packet(remcom_out_buffer, -EINVAL);
552 gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
553 strcpy(remcom_out_buffer, "OK");
557 /* Handle the 'm' memory read bytes */
558 static void gdb_cmd_memread(struct kgdb_state *ks)
560 char *ptr = &remcom_in_buffer[1];
561 unsigned long length;
565 if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
566 kgdb_hex2long(&ptr, &length) > 0) {
567 err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
569 error_packet(remcom_out_buffer, -EINVAL);
571 error_packet(remcom_out_buffer, -EINVAL);
575 /* Handle the 'M' memory write bytes */
576 static void gdb_cmd_memwrite(struct kgdb_state *ks)
578 int err = write_mem_msg(0);
581 error_packet(remcom_out_buffer, err);
583 strcpy(remcom_out_buffer, "OK");
586 #if DBG_MAX_REG_NUM > 0
587 static char *gdb_hex_reg_helper(int regnum, char *out)
592 for (i = 0; i < regnum; i++)
593 offset += dbg_reg_def[i].size;
594 return kgdb_mem2hex((char *)gdb_regs + offset, out,
595 dbg_reg_def[i].size);
598 /* Handle the 'p' individual register get */
599 static void gdb_cmd_reg_get(struct kgdb_state *ks)
601 unsigned long regnum;
602 char *ptr = &remcom_in_buffer[1];
604 kgdb_hex2long(&ptr, ®num);
605 if (regnum >= DBG_MAX_REG_NUM) {
606 error_packet(remcom_out_buffer, -EINVAL);
609 gdb_get_regs_helper(ks);
610 gdb_hex_reg_helper(regnum, remcom_out_buffer);
613 /* Handle the 'P' individual register set */
614 static void gdb_cmd_reg_set(struct kgdb_state *ks)
616 unsigned long regnum;
617 char *ptr = &remcom_in_buffer[1];
620 kgdb_hex2long(&ptr, ®num);
622 !(!kgdb_usethread || kgdb_usethread == current) ||
623 !dbg_get_reg(regnum, gdb_regs, ks->linux_regs)) {
624 error_packet(remcom_out_buffer, -EINVAL);
627 memset(gdb_regs, 0, sizeof(gdb_regs));
628 while (i < sizeof(gdb_regs) * 2)
629 if (hex_to_bin(ptr[i]) >= 0)
634 kgdb_hex2mem(ptr, (char *)gdb_regs, i);
635 dbg_set_reg(regnum, gdb_regs, ks->linux_regs);
636 strcpy(remcom_out_buffer, "OK");
638 #endif /* DBG_MAX_REG_NUM > 0 */
640 /* Handle the 'X' memory binary write bytes */
641 static void gdb_cmd_binwrite(struct kgdb_state *ks)
643 int err = write_mem_msg(1);
646 error_packet(remcom_out_buffer, err);
648 strcpy(remcom_out_buffer, "OK");
651 /* Handle the 'D' or 'k', detach or kill packets */
652 static void gdb_cmd_detachkill(struct kgdb_state *ks)
656 /* The detach case */
657 if (remcom_in_buffer[0] == 'D') {
658 error = dbg_remove_all_break();
660 error_packet(remcom_out_buffer, error);
662 strcpy(remcom_out_buffer, "OK");
665 put_packet(remcom_out_buffer);
668 * Assume the kill case, with no exit code checking,
669 * trying to force detach the debugger:
671 dbg_remove_all_break();
676 /* Handle the 'R' reboot packets */
677 static int gdb_cmd_reboot(struct kgdb_state *ks)
679 /* For now, only honor R0 */
680 if (strcmp(remcom_in_buffer, "R0") == 0) {
681 printk(KERN_CRIT "Executing emergency reboot\n");
682 strcpy(remcom_out_buffer, "OK");
683 put_packet(remcom_out_buffer);
686 * Execution should not return from
687 * machine_emergency_restart()
689 machine_emergency_restart();
697 /* Handle the 'q' query packets */
698 static void gdb_cmd_query(struct kgdb_state *ks)
700 struct task_struct *g;
701 struct task_struct *p;
702 unsigned char thref[BUF_THREAD_ID_SIZE];
708 switch (remcom_in_buffer[1]) {
711 if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10))
715 remcom_out_buffer[0] = 'm';
716 ptr = remcom_out_buffer + 1;
717 if (remcom_in_buffer[1] == 'f') {
718 /* Each cpu is a shadow thread */
719 for_each_online_cpu(cpu) {
721 int_to_threadref(thref, -cpu - 2);
722 ptr = pack_threadid(ptr, thref);
728 for_each_process_thread(g, p) {
729 if (i >= ks->thr_query && !finished) {
730 int_to_threadref(thref, p->pid);
731 ptr = pack_threadid(ptr, thref);
734 if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0)
744 /* Current thread id */
745 strcpy(remcom_out_buffer, "QC");
746 ks->threadid = shadow_pid(current->pid);
747 int_to_threadref(thref, ks->threadid);
748 pack_threadid(remcom_out_buffer + 2, thref);
751 if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16))
755 ptr = remcom_in_buffer + 17;
756 kgdb_hex2long(&ptr, &ks->threadid);
757 if (!getthread(ks->linux_regs, ks->threadid)) {
758 error_packet(remcom_out_buffer, -EINVAL);
761 if ((int)ks->threadid > 0) {
762 kgdb_mem2hex(getthread(ks->linux_regs,
764 remcom_out_buffer, 16);
766 static char tmpstr[23 + BUF_THREAD_ID_SIZE];
768 sprintf(tmpstr, "shadowCPU%d",
769 (int)(-ks->threadid - 2));
770 kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
773 #ifdef CONFIG_KGDB_KDB
775 if (strncmp(remcom_in_buffer, "qRcmd,", 6) == 0) {
776 int len = strlen(remcom_in_buffer + 6);
778 if ((len % 2) != 0) {
779 strcpy(remcom_out_buffer, "E01");
782 kgdb_hex2mem(remcom_in_buffer + 6,
783 remcom_out_buffer, len);
785 remcom_out_buffer[len++] = 0;
787 kdb_common_init_state(ks);
788 kdb_parse(remcom_out_buffer);
789 kdb_common_deinit_state();
791 strcpy(remcom_out_buffer, "OK");
795 #ifdef CONFIG_HAVE_ARCH_KGDB_QXFER_PKT
797 if (!strncmp(remcom_in_buffer, "qSupported:", 11))
798 strcpy(remcom_out_buffer, kgdb_arch_gdb_stub_feature);
801 if (!strncmp(remcom_in_buffer, "qXfer:", 6))
802 kgdb_arch_handle_qxfer_pkt(remcom_in_buffer,
811 /* Handle the 'H' task query packets */
812 static void gdb_cmd_task(struct kgdb_state *ks)
814 struct task_struct *thread;
817 switch (remcom_in_buffer[1]) {
819 ptr = &remcom_in_buffer[2];
820 kgdb_hex2long(&ptr, &ks->threadid);
821 thread = getthread(ks->linux_regs, ks->threadid);
822 if (!thread && ks->threadid > 0) {
823 error_packet(remcom_out_buffer, -EINVAL);
826 kgdb_usethread = thread;
827 ks->kgdb_usethreadid = ks->threadid;
828 strcpy(remcom_out_buffer, "OK");
831 ptr = &remcom_in_buffer[2];
832 kgdb_hex2long(&ptr, &ks->threadid);
834 kgdb_contthread = NULL;
836 thread = getthread(ks->linux_regs, ks->threadid);
837 if (!thread && ks->threadid > 0) {
838 error_packet(remcom_out_buffer, -EINVAL);
841 kgdb_contthread = thread;
843 strcpy(remcom_out_buffer, "OK");
848 /* Handle the 'T' thread query packets */
849 static void gdb_cmd_thread(struct kgdb_state *ks)
851 char *ptr = &remcom_in_buffer[1];
852 struct task_struct *thread;
854 kgdb_hex2long(&ptr, &ks->threadid);
855 thread = getthread(ks->linux_regs, ks->threadid);
857 strcpy(remcom_out_buffer, "OK");
859 error_packet(remcom_out_buffer, -EINVAL);
862 /* Handle the 'z' or 'Z' breakpoint remove or set packets */
863 static void gdb_cmd_break(struct kgdb_state *ks)
866 * Since GDB-5.3, it's been drafted that '0' is a software
867 * breakpoint, '1' is a hardware breakpoint, so let's do that.
869 char *bpt_type = &remcom_in_buffer[1];
870 char *ptr = &remcom_in_buffer[2];
872 unsigned long length;
875 if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
880 if (*bpt_type != '0' && *bpt_type != '1')
886 * Test if this is a hardware breakpoint, and
889 if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
893 if (*(ptr++) != ',') {
894 error_packet(remcom_out_buffer, -EINVAL);
897 if (!kgdb_hex2long(&ptr, &addr)) {
898 error_packet(remcom_out_buffer, -EINVAL);
901 if (*(ptr++) != ',' ||
902 !kgdb_hex2long(&ptr, &length)) {
903 error_packet(remcom_out_buffer, -EINVAL);
907 if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
908 error = dbg_set_sw_break(addr);
909 else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
910 error = dbg_remove_sw_break(addr);
911 else if (remcom_in_buffer[0] == 'Z')
912 error = arch_kgdb_ops.set_hw_breakpoint(addr,
913 (int)length, *bpt_type - '0');
914 else if (remcom_in_buffer[0] == 'z')
915 error = arch_kgdb_ops.remove_hw_breakpoint(addr,
916 (int) length, *bpt_type - '0');
919 strcpy(remcom_out_buffer, "OK");
921 error_packet(remcom_out_buffer, error);
924 /* Handle the 'C' signal / exception passing packets */
925 static int gdb_cmd_exception_pass(struct kgdb_state *ks)
927 /* C09 == pass exception
928 * C15 == detach kgdb, pass exception
930 if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
932 ks->pass_exception = 1;
933 remcom_in_buffer[0] = 'c';
935 } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
937 ks->pass_exception = 1;
938 remcom_in_buffer[0] = 'D';
939 dbg_remove_all_break();
944 gdbstub_msg_write("KGDB only knows signal 9 (pass)"
945 " and 15 (pass and disconnect)\n"
946 "Executing a continue without signal passing\n", 0);
947 remcom_in_buffer[0] = 'c';
950 /* Indicate fall through */
955 * This function performs all gdbserial command processing
957 int gdb_serial_stub(struct kgdb_state *ks)
962 /* Initialize comm buffer and globals. */
963 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
964 kgdb_usethread = kgdb_info[ks->cpu].task;
965 ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
966 ks->pass_exception = 0;
968 if (kgdb_connected) {
969 unsigned char thref[BUF_THREAD_ID_SIZE];
972 /* Reply to host that an exception has occurred */
973 ptr = remcom_out_buffer;
975 ptr = hex_byte_pack(ptr, ks->signo);
976 ptr += strlen(strcpy(ptr, "thread:"));
977 int_to_threadref(thref, shadow_pid(current->pid));
978 ptr = pack_threadid(ptr, thref);
980 put_packet(remcom_out_buffer);
986 /* Clear the out buffer. */
987 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
989 get_packet(remcom_in_buffer);
991 switch (remcom_in_buffer[0]) {
992 case '?': /* gdbserial status */
995 case 'g': /* return the value of the CPU registers */
998 case 'G': /* set the value of the CPU registers - return OK */
1001 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1002 gdb_cmd_memread(ks);
1004 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1005 gdb_cmd_memwrite(ks);
1007 #if DBG_MAX_REG_NUM > 0
1008 case 'p': /* pXX Return gdb register XX (in hex) */
1009 gdb_cmd_reg_get(ks);
1011 case 'P': /* PXX=aaaa Set gdb register XX to aaaa (in hex) */
1012 gdb_cmd_reg_set(ks);
1014 #endif /* DBG_MAX_REG_NUM > 0 */
1015 case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1016 gdb_cmd_binwrite(ks);
1018 /* kill or detach. KGDB should treat this like a
1021 case 'D': /* Debugger detach */
1022 case 'k': /* Debugger detach via kill */
1023 gdb_cmd_detachkill(ks);
1024 goto default_handle;
1025 case 'R': /* Reboot */
1026 if (gdb_cmd_reboot(ks))
1027 goto default_handle;
1029 case 'q': /* query command */
1032 case 'H': /* task related */
1035 case 'T': /* Query thread status */
1038 case 'z': /* Break point remove */
1039 case 'Z': /* Break point set */
1042 #ifdef CONFIG_KGDB_KDB
1043 case '3': /* Escape into back into kdb */
1044 if (remcom_in_buffer[1] == '\0') {
1045 gdb_cmd_detachkill(ks);
1046 return DBG_PASS_EVENT;
1050 case 'C': /* Exception passing */
1051 tmp = gdb_cmd_exception_pass(ks);
1053 goto default_handle;
1056 fallthrough; /* on tmp < 0 */
1057 case 'c': /* Continue packet */
1058 case 's': /* Single step packet */
1059 if (kgdb_contthread && kgdb_contthread != current) {
1060 /* Can't switch threads in kgdb */
1061 error_packet(remcom_out_buffer, -EINVAL);
1064 fallthrough; /* to default processing */
1067 error = kgdb_arch_handle_exception(ks->ex_vector,
1074 * Leave cmd processing on error, detach,
1075 * kill, continue, or single step.
1077 if (error >= 0 || remcom_in_buffer[0] == 'D' ||
1078 remcom_in_buffer[0] == 'k') {
1085 /* reply to the request */
1086 put_packet(remcom_out_buffer);
1090 if (ks->pass_exception)
1095 int gdbstub_state(struct kgdb_state *ks, char *cmd)
1101 error = kgdb_arch_handle_exception(ks->ex_vector,
1110 strscpy(remcom_in_buffer, cmd, sizeof(remcom_in_buffer));
1113 strscpy(remcom_in_buffer, cmd, sizeof(remcom_in_buffer));
1114 gdbstub_use_prev_in_buf = strlen(remcom_in_buffer);
1115 gdbstub_prev_in_buf_pos = 0;
1118 dbg_io_ops->write_char('+');
1119 put_packet(remcom_out_buffer);
1124 * gdbstub_exit - Send an exit message to GDB
1125 * @status: The exit code to report.
1127 void gdbstub_exit(int status)
1129 unsigned char checksum, ch, buffer[3];
1132 if (!kgdb_connected)
1136 if (!dbg_io_ops || dbg_kdb_mode)
1140 buffer[1] = hex_asc_hi(status);
1141 buffer[2] = hex_asc_lo(status);
1143 dbg_io_ops->write_char('$');
1146 for (loop = 0; loop < 3; loop++) {
1149 dbg_io_ops->write_char(ch);
1152 dbg_io_ops->write_char('#');
1153 dbg_io_ops->write_char(hex_asc_hi(checksum));
1154 dbg_io_ops->write_char(hex_asc_lo(checksum));
1156 /* make sure the output is flushed, lest the bootloader clobber it */
1157 if (dbg_io_ops->flush)
1158 dbg_io_ops->flush();