1 // SPDX-License-Identifier: GPL-2.0-only
5 * Maintainer: Jason Wessel <jason.wessel@windriver.com>
7 * Copyright (C) 2000-2001 VERITAS Software Corporation.
8 * Copyright (C) 2002-2004 Timesys Corporation
9 * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com>
10 * Copyright (C) 2004 Pavel Machek <pavel@ucw.cz>
11 * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org>
12 * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd.
13 * Copyright (C) 2005-2009 Wind River Systems, Inc.
14 * Copyright (C) 2007 MontaVista Software, Inc.
15 * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
17 * Contributors at various stages not listed above:
18 * Jason Wessel ( jason.wessel@windriver.com )
19 * George Anzinger <george@mvista.com>
20 * Anurekh Saxena (anurekh.saxena@timesys.com)
21 * Lake Stevens Instrument Division (Glenn Engel)
22 * Jim Kingdon, Cygnus Support.
24 * Original KGDB stub: David Grothe <dave@gcom.com>,
25 * Tigran Aivazian <tigran@sco.com>
28 #include <linux/kernel.h>
29 #include <linux/sched/signal.h>
30 #include <linux/kgdb.h>
31 #include <linux/kdb.h>
32 #include <linux/serial_core.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"
39 #define KGDB_MAX_THREAD_QUERY 17
41 /* Our I/O buffers. */
42 static char remcom_in_buffer[BUFMAX];
43 static char remcom_out_buffer[BUFMAX];
44 static int gdbstub_use_prev_in_buf;
45 static int gdbstub_prev_in_buf_pos;
47 /* Storage for the registers, in GDB format. */
48 static unsigned long gdb_regs[(NUMREGBYTES +
49 sizeof(unsigned long) - 1) /
50 sizeof(unsigned long)];
53 * GDB remote protocol parser:
56 #ifdef CONFIG_KGDB_KDB
57 static int gdbstub_read_wait(void)
62 if (unlikely(gdbstub_use_prev_in_buf)) {
63 if (gdbstub_prev_in_buf_pos < gdbstub_use_prev_in_buf)
64 return remcom_in_buffer[gdbstub_prev_in_buf_pos++];
66 gdbstub_use_prev_in_buf = 0;
69 /* poll any additional I/O interfaces that are defined */
71 for (i = 0; kdb_poll_funcs[i] != NULL; i++) {
72 ret = kdb_poll_funcs[i]();
79 static int gdbstub_read_wait(void)
81 int ret = dbg_io_ops->read_char();
82 while (ret == NO_POLL_CHAR)
83 ret = dbg_io_ops->read_char();
87 /* scan for the sequence $<data>#<checksum> */
88 static void get_packet(char *buffer)
90 unsigned char checksum;
91 unsigned char xmitcsum;
97 * Spin and wait around for the start character, ignore all
100 while ((ch = (gdbstub_read_wait())) != '$')
110 * now, read until a # or end of buffer is found:
112 while (count < (BUFMAX - 1)) {
113 ch = gdbstub_read_wait();
116 checksum = checksum + ch;
122 xmitcsum = hex_to_bin(gdbstub_read_wait()) << 4;
123 xmitcsum += hex_to_bin(gdbstub_read_wait());
125 if (checksum != xmitcsum)
126 /* failed checksum */
127 dbg_io_ops->write_char('-');
129 /* successful transfer */
130 dbg_io_ops->write_char('+');
131 if (dbg_io_ops->flush)
135 } while (checksum != xmitcsum);
139 * Send the packet in buffer.
140 * Check for gdb connection if asked for.
142 static void put_packet(char *buffer)
144 unsigned char checksum;
149 * $<packet info>#<checksum>.
152 dbg_io_ops->write_char('$');
156 while ((ch = buffer[count])) {
157 dbg_io_ops->write_char(ch);
162 dbg_io_ops->write_char('#');
163 dbg_io_ops->write_char(hex_asc_hi(checksum));
164 dbg_io_ops->write_char(hex_asc_lo(checksum));
165 if (dbg_io_ops->flush)
168 /* Now see what we get in reply. */
169 ch = gdbstub_read_wait();
172 ch = gdbstub_read_wait();
174 /* If we get an ACK, we are done. */
179 * If we get the start of another packet, this means
180 * that GDB is attempting to reconnect. We will NAK
181 * the packet being sent, and stop trying to send this
185 dbg_io_ops->write_char('-');
186 if (dbg_io_ops->flush)
193 static char gdbmsgbuf[BUFMAX + 1];
195 void gdbstub_msg_write(const char *s, int len)
207 /* Fill and send buffers... */
209 bufptr = gdbmsgbuf + 1;
211 /* Calculate how many this time */
212 if ((len << 1) > (BUFMAX - 2))
213 wcount = (BUFMAX - 2) >> 1;
217 /* Pack in hex chars */
218 for (i = 0; i < wcount; i++)
219 bufptr = hex_byte_pack(bufptr, s[i]);
227 put_packet(gdbmsgbuf);
232 * Convert the memory pointed to by mem into hex, placing result in
233 * buf. Return a pointer to the last char put in buf (null). May
236 char *kgdb_mem2hex(char *mem, char *buf, int count)
242 * We use the upper half of buf as an intermediate buffer for the
243 * raw memory copy. Hex conversion will work against this one.
247 err = copy_from_kernel_nofault(tmp, mem, count);
251 buf = hex_byte_pack(buf, *tmp);
261 * Convert the hex array pointed to by buf into binary to be placed in
262 * mem. Return a pointer to the character AFTER the last byte
263 * written. May return an error.
265 int kgdb_hex2mem(char *buf, char *mem, int count)
271 * We use the upper half of buf as an intermediate buffer for the
272 * raw memory that is converted from hex.
274 tmp_raw = buf + count * 2;
276 tmp_hex = tmp_raw - 1;
277 while (tmp_hex >= buf) {
279 *tmp_raw = hex_to_bin(*tmp_hex--);
280 *tmp_raw |= hex_to_bin(*tmp_hex--) << 4;
283 return copy_to_kernel_nofault(mem, tmp_raw, count);
287 * While we find nice hex chars, build a long_val.
288 * Return number of chars processed.
290 int kgdb_hex2long(char **ptr, unsigned long *long_val)
303 hex_val = hex_to_bin(**ptr);
307 *long_val = (*long_val << 4) | hex_val;
313 *long_val = -*long_val;
319 * Copy the binary array pointed to by buf into mem. Fix $, #, and
320 * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success.
321 * The input buf is overwritten with the result to write to mem.
323 static int kgdb_ebin2mem(char *buf, char *mem, int count)
328 while (count-- > 0) {
331 c[size] = *buf++ ^ 0x20;
335 return copy_to_kernel_nofault(mem, c, size);
338 #if DBG_MAX_REG_NUM > 0
339 void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
343 char *ptr = (char *)gdb_regs;
345 for (i = 0; i < DBG_MAX_REG_NUM; i++) {
346 dbg_get_reg(i, ptr + idx, regs);
347 idx += dbg_reg_def[i].size;
351 void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
355 char *ptr = (char *)gdb_regs;
357 for (i = 0; i < DBG_MAX_REG_NUM; i++) {
358 dbg_set_reg(i, ptr + idx, regs);
359 idx += dbg_reg_def[i].size;
362 #endif /* DBG_MAX_REG_NUM > 0 */
364 /* Write memory due to an 'M' or 'X' packet. */
365 static int write_mem_msg(int binary)
367 char *ptr = &remcom_in_buffer[1];
369 unsigned long length;
372 if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' &&
373 kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') {
375 err = kgdb_ebin2mem(ptr, (char *)addr, length);
377 err = kgdb_hex2mem(ptr, (char *)addr, length);
380 if (CACHE_FLUSH_IS_SAFE)
381 flush_icache_range(addr, addr + length);
388 static void error_packet(char *pkt, int error)
392 pkt[1] = hex_asc[(error / 10)];
393 pkt[2] = hex_asc[(error % 10)];
398 * Thread ID accessors. We represent a flat TID space to GDB, where
399 * the per CPU idle threads (which under Linux all have PID 0) are
400 * remapped to negative TIDs.
403 #define BUF_THREAD_ID_SIZE 8
405 static char *pack_threadid(char *pkt, unsigned char *id)
407 unsigned char *limit;
410 limit = id + (BUF_THREAD_ID_SIZE / 2);
412 if (!lzero || *id != 0) {
413 pkt = hex_byte_pack(pkt, *id);
420 pkt = hex_byte_pack(pkt, 0);
425 static void int_to_threadref(unsigned char *id, int value)
427 put_unaligned_be32(value, id);
430 static struct task_struct *getthread(struct pt_regs *regs, int tid)
433 * Non-positive TIDs are remapped to the cpu shadow information
435 if (tid == 0 || tid == -1)
436 tid = -atomic_read(&kgdb_active) - 2;
437 if (tid < -1 && tid > -NR_CPUS - 2) {
438 if (kgdb_info[-tid - 2].task)
439 return kgdb_info[-tid - 2].task;
441 return idle_task(-tid - 2);
444 printk(KERN_ERR "KGDB: Internal thread select error\n");
450 * find_task_by_pid_ns() does not take the tasklist lock anymore
451 * but is nicely RCU locked - hence is a pretty resilient
454 return find_task_by_pid_ns(tid, &init_pid_ns);
459 * Remap normal tasks to their real PID,
460 * CPU shadow threads are mapped to -CPU - 2
462 static inline int shadow_pid(int realpid)
467 return -raw_smp_processor_id() - 2;
471 * All the functions that start with gdb_cmd are the various
472 * operations to implement the handlers for the gdbserial protocol
473 * where KGDB is communicating with an external debugger
476 /* Handle the '?' status packets */
477 static void gdb_cmd_status(struct kgdb_state *ks)
480 * We know that this packet is only sent
481 * during initial connect. So to be safe,
482 * we clear out our breakpoints now in case
483 * GDB is reconnecting.
485 dbg_remove_all_break();
487 remcom_out_buffer[0] = 'S';
488 hex_byte_pack(&remcom_out_buffer[1], ks->signo);
491 static void gdb_get_regs_helper(struct kgdb_state *ks)
493 struct task_struct *thread;
494 void *local_debuggerinfo;
497 thread = kgdb_usethread;
499 thread = kgdb_info[ks->cpu].task;
500 local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
502 local_debuggerinfo = NULL;
503 for_each_online_cpu(i) {
505 * Try to find the task on some other
506 * or possibly this node if we do not
507 * find the matching task then we try
508 * to approximate the results.
510 if (thread == kgdb_info[i].task)
511 local_debuggerinfo = kgdb_info[i].debuggerinfo;
516 * All threads that don't have debuggerinfo should be
517 * in schedule() sleeping, since all other CPUs
518 * are in kgdb_wait, and thus have debuggerinfo.
520 if (local_debuggerinfo) {
521 pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
524 * Pull stuff saved during switch_to; nothing
525 * else is accessible (or even particularly
528 * This should be enough for a stack trace.
530 sleeping_thread_to_gdb_regs(gdb_regs, thread);
534 /* Handle the 'g' get registers request */
535 static void gdb_cmd_getregs(struct kgdb_state *ks)
537 gdb_get_regs_helper(ks);
538 kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
541 /* Handle the 'G' set registers request */
542 static void gdb_cmd_setregs(struct kgdb_state *ks)
544 kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
546 if (kgdb_usethread && kgdb_usethread != current) {
547 error_packet(remcom_out_buffer, -EINVAL);
549 gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
550 strcpy(remcom_out_buffer, "OK");
554 /* Handle the 'm' memory read bytes */
555 static void gdb_cmd_memread(struct kgdb_state *ks)
557 char *ptr = &remcom_in_buffer[1];
558 unsigned long length;
562 if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
563 kgdb_hex2long(&ptr, &length) > 0) {
564 err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
566 error_packet(remcom_out_buffer, -EINVAL);
568 error_packet(remcom_out_buffer, -EINVAL);
572 /* Handle the 'M' memory write bytes */
573 static void gdb_cmd_memwrite(struct kgdb_state *ks)
575 int err = write_mem_msg(0);
578 error_packet(remcom_out_buffer, err);
580 strcpy(remcom_out_buffer, "OK");
583 #if DBG_MAX_REG_NUM > 0
584 static char *gdb_hex_reg_helper(int regnum, char *out)
589 for (i = 0; i < regnum; i++)
590 offset += dbg_reg_def[i].size;
591 return kgdb_mem2hex((char *)gdb_regs + offset, out,
592 dbg_reg_def[i].size);
595 /* Handle the 'p' individual register get */
596 static void gdb_cmd_reg_get(struct kgdb_state *ks)
598 unsigned long regnum;
599 char *ptr = &remcom_in_buffer[1];
601 kgdb_hex2long(&ptr, ®num);
602 if (regnum >= DBG_MAX_REG_NUM) {
603 error_packet(remcom_out_buffer, -EINVAL);
606 gdb_get_regs_helper(ks);
607 gdb_hex_reg_helper(regnum, remcom_out_buffer);
610 /* Handle the 'P' individual register set */
611 static void gdb_cmd_reg_set(struct kgdb_state *ks)
613 unsigned long regnum;
614 char *ptr = &remcom_in_buffer[1];
617 kgdb_hex2long(&ptr, ®num);
619 !(!kgdb_usethread || kgdb_usethread == current) ||
620 !dbg_get_reg(regnum, gdb_regs, ks->linux_regs)) {
621 error_packet(remcom_out_buffer, -EINVAL);
624 memset(gdb_regs, 0, sizeof(gdb_regs));
625 while (i < sizeof(gdb_regs) * 2)
626 if (hex_to_bin(ptr[i]) >= 0)
631 kgdb_hex2mem(ptr, (char *)gdb_regs, i);
632 dbg_set_reg(regnum, gdb_regs, ks->linux_regs);
633 strcpy(remcom_out_buffer, "OK");
635 #endif /* DBG_MAX_REG_NUM > 0 */
637 /* Handle the 'X' memory binary write bytes */
638 static void gdb_cmd_binwrite(struct kgdb_state *ks)
640 int err = write_mem_msg(1);
643 error_packet(remcom_out_buffer, err);
645 strcpy(remcom_out_buffer, "OK");
648 /* Handle the 'D' or 'k', detach or kill packets */
649 static void gdb_cmd_detachkill(struct kgdb_state *ks)
653 /* The detach case */
654 if (remcom_in_buffer[0] == 'D') {
655 error = dbg_remove_all_break();
657 error_packet(remcom_out_buffer, error);
659 strcpy(remcom_out_buffer, "OK");
662 put_packet(remcom_out_buffer);
665 * Assume the kill case, with no exit code checking,
666 * trying to force detach the debugger:
668 dbg_remove_all_break();
673 /* Handle the 'R' reboot packets */
674 static int gdb_cmd_reboot(struct kgdb_state *ks)
676 /* For now, only honor R0 */
677 if (strcmp(remcom_in_buffer, "R0") == 0) {
678 printk(KERN_CRIT "Executing emergency reboot\n");
679 strcpy(remcom_out_buffer, "OK");
680 put_packet(remcom_out_buffer);
683 * Execution should not return from
684 * machine_emergency_restart()
686 machine_emergency_restart();
694 /* Handle the 'q' query packets */
695 static void gdb_cmd_query(struct kgdb_state *ks)
697 struct task_struct *g;
698 struct task_struct *p;
699 unsigned char thref[BUF_THREAD_ID_SIZE];
705 switch (remcom_in_buffer[1]) {
708 if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10))
712 remcom_out_buffer[0] = 'm';
713 ptr = remcom_out_buffer + 1;
714 if (remcom_in_buffer[1] == 'f') {
715 /* Each cpu is a shadow thread */
716 for_each_online_cpu(cpu) {
718 int_to_threadref(thref, -cpu - 2);
719 ptr = pack_threadid(ptr, thref);
725 for_each_process_thread(g, p) {
726 if (i >= ks->thr_query && !finished) {
727 int_to_threadref(thref, p->pid);
728 ptr = pack_threadid(ptr, thref);
731 if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0)
741 /* Current thread id */
742 strcpy(remcom_out_buffer, "QC");
743 ks->threadid = shadow_pid(current->pid);
744 int_to_threadref(thref, ks->threadid);
745 pack_threadid(remcom_out_buffer + 2, thref);
748 if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16))
752 ptr = remcom_in_buffer + 17;
753 kgdb_hex2long(&ptr, &ks->threadid);
754 if (!getthread(ks->linux_regs, ks->threadid)) {
755 error_packet(remcom_out_buffer, -EINVAL);
758 if ((int)ks->threadid > 0) {
759 kgdb_mem2hex(getthread(ks->linux_regs,
761 remcom_out_buffer, 16);
763 static char tmpstr[23 + BUF_THREAD_ID_SIZE];
765 sprintf(tmpstr, "shadowCPU%d",
766 (int)(-ks->threadid - 2));
767 kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
770 #ifdef CONFIG_KGDB_KDB
772 if (strncmp(remcom_in_buffer, "qRcmd,", 6) == 0) {
773 int len = strlen(remcom_in_buffer + 6);
775 if ((len % 2) != 0) {
776 strcpy(remcom_out_buffer, "E01");
779 kgdb_hex2mem(remcom_in_buffer + 6,
780 remcom_out_buffer, len);
782 remcom_out_buffer[len++] = 0;
784 kdb_common_init_state(ks);
785 kdb_parse(remcom_out_buffer);
786 kdb_common_deinit_state();
788 strcpy(remcom_out_buffer, "OK");
792 #ifdef CONFIG_HAVE_ARCH_KGDB_QXFER_PKT
794 if (!strncmp(remcom_in_buffer, "qSupported:", 11))
795 strcpy(remcom_out_buffer, kgdb_arch_gdb_stub_feature);
798 if (!strncmp(remcom_in_buffer, "qXfer:", 6))
799 kgdb_arch_handle_qxfer_pkt(remcom_in_buffer,
808 /* Handle the 'H' task query packets */
809 static void gdb_cmd_task(struct kgdb_state *ks)
811 struct task_struct *thread;
814 switch (remcom_in_buffer[1]) {
816 ptr = &remcom_in_buffer[2];
817 kgdb_hex2long(&ptr, &ks->threadid);
818 thread = getthread(ks->linux_regs, ks->threadid);
819 if (!thread && ks->threadid > 0) {
820 error_packet(remcom_out_buffer, -EINVAL);
823 kgdb_usethread = thread;
824 ks->kgdb_usethreadid = ks->threadid;
825 strcpy(remcom_out_buffer, "OK");
828 ptr = &remcom_in_buffer[2];
829 kgdb_hex2long(&ptr, &ks->threadid);
831 kgdb_contthread = NULL;
833 thread = getthread(ks->linux_regs, ks->threadid);
834 if (!thread && ks->threadid > 0) {
835 error_packet(remcom_out_buffer, -EINVAL);
838 kgdb_contthread = thread;
840 strcpy(remcom_out_buffer, "OK");
845 /* Handle the 'T' thread query packets */
846 static void gdb_cmd_thread(struct kgdb_state *ks)
848 char *ptr = &remcom_in_buffer[1];
849 struct task_struct *thread;
851 kgdb_hex2long(&ptr, &ks->threadid);
852 thread = getthread(ks->linux_regs, ks->threadid);
854 strcpy(remcom_out_buffer, "OK");
856 error_packet(remcom_out_buffer, -EINVAL);
859 /* Handle the 'z' or 'Z' breakpoint remove or set packets */
860 static void gdb_cmd_break(struct kgdb_state *ks)
863 * Since GDB-5.3, it's been drafted that '0' is a software
864 * breakpoint, '1' is a hardware breakpoint, so let's do that.
866 char *bpt_type = &remcom_in_buffer[1];
867 char *ptr = &remcom_in_buffer[2];
869 unsigned long length;
872 if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
877 if (*bpt_type != '0' && *bpt_type != '1')
883 * Test if this is a hardware breakpoint, and
886 if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
890 if (*(ptr++) != ',') {
891 error_packet(remcom_out_buffer, -EINVAL);
894 if (!kgdb_hex2long(&ptr, &addr)) {
895 error_packet(remcom_out_buffer, -EINVAL);
898 if (*(ptr++) != ',' ||
899 !kgdb_hex2long(&ptr, &length)) {
900 error_packet(remcom_out_buffer, -EINVAL);
904 if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
905 error = dbg_set_sw_break(addr);
906 else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
907 error = dbg_remove_sw_break(addr);
908 else if (remcom_in_buffer[0] == 'Z')
909 error = arch_kgdb_ops.set_hw_breakpoint(addr,
910 (int)length, *bpt_type - '0');
911 else if (remcom_in_buffer[0] == 'z')
912 error = arch_kgdb_ops.remove_hw_breakpoint(addr,
913 (int) length, *bpt_type - '0');
916 strcpy(remcom_out_buffer, "OK");
918 error_packet(remcom_out_buffer, error);
921 /* Handle the 'C' signal / exception passing packets */
922 static int gdb_cmd_exception_pass(struct kgdb_state *ks)
924 /* C09 == pass exception
925 * C15 == detach kgdb, pass exception
927 if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
929 ks->pass_exception = 1;
930 remcom_in_buffer[0] = 'c';
932 } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
934 ks->pass_exception = 1;
935 remcom_in_buffer[0] = 'D';
936 dbg_remove_all_break();
941 gdbstub_msg_write("KGDB only knows signal 9 (pass)"
942 " and 15 (pass and disconnect)\n"
943 "Executing a continue without signal passing\n", 0);
944 remcom_in_buffer[0] = 'c';
947 /* Indicate fall through */
952 * This function performs all gdbserial command processing
954 int gdb_serial_stub(struct kgdb_state *ks)
959 /* Initialize comm buffer and globals. */
960 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
961 kgdb_usethread = kgdb_info[ks->cpu].task;
962 ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
963 ks->pass_exception = 0;
965 if (kgdb_connected) {
966 unsigned char thref[BUF_THREAD_ID_SIZE];
969 /* Reply to host that an exception has occurred */
970 ptr = remcom_out_buffer;
972 ptr = hex_byte_pack(ptr, ks->signo);
973 ptr += strlen(strcpy(ptr, "thread:"));
974 int_to_threadref(thref, shadow_pid(current->pid));
975 ptr = pack_threadid(ptr, thref);
977 put_packet(remcom_out_buffer);
983 /* Clear the out buffer. */
984 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
986 get_packet(remcom_in_buffer);
988 switch (remcom_in_buffer[0]) {
989 case '?': /* gdbserial status */
992 case 'g': /* return the value of the CPU registers */
995 case 'G': /* set the value of the CPU registers - return OK */
998 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1001 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1002 gdb_cmd_memwrite(ks);
1004 #if DBG_MAX_REG_NUM > 0
1005 case 'p': /* pXX Return gdb register XX (in hex) */
1006 gdb_cmd_reg_get(ks);
1008 case 'P': /* PXX=aaaa Set gdb register XX to aaaa (in hex) */
1009 gdb_cmd_reg_set(ks);
1011 #endif /* DBG_MAX_REG_NUM > 0 */
1012 case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1013 gdb_cmd_binwrite(ks);
1015 /* kill or detach. KGDB should treat this like a
1018 case 'D': /* Debugger detach */
1019 case 'k': /* Debugger detach via kill */
1020 gdb_cmd_detachkill(ks);
1021 goto default_handle;
1022 case 'R': /* Reboot */
1023 if (gdb_cmd_reboot(ks))
1024 goto default_handle;
1026 case 'q': /* query command */
1029 case 'H': /* task related */
1032 case 'T': /* Query thread status */
1035 case 'z': /* Break point remove */
1036 case 'Z': /* Break point set */
1039 #ifdef CONFIG_KGDB_KDB
1040 case '3': /* Escape into back into kdb */
1041 if (remcom_in_buffer[1] == '\0') {
1042 gdb_cmd_detachkill(ks);
1043 return DBG_PASS_EVENT;
1047 case 'C': /* Exception passing */
1048 tmp = gdb_cmd_exception_pass(ks);
1050 goto default_handle;
1053 fallthrough; /* on tmp < 0 */
1054 case 'c': /* Continue packet */
1055 case 's': /* Single step packet */
1056 if (kgdb_contthread && kgdb_contthread != current) {
1057 /* Can't switch threads in kgdb */
1058 error_packet(remcom_out_buffer, -EINVAL);
1061 fallthrough; /* to default processing */
1064 error = kgdb_arch_handle_exception(ks->ex_vector,
1071 * Leave cmd processing on error, detach,
1072 * kill, continue, or single step.
1074 if (error >= 0 || remcom_in_buffer[0] == 'D' ||
1075 remcom_in_buffer[0] == 'k') {
1082 /* reply to the request */
1083 put_packet(remcom_out_buffer);
1087 if (ks->pass_exception)
1092 int gdbstub_state(struct kgdb_state *ks, char *cmd)
1098 error = kgdb_arch_handle_exception(ks->ex_vector,
1107 strscpy(remcom_in_buffer, cmd, sizeof(remcom_in_buffer));
1110 strscpy(remcom_in_buffer, cmd, sizeof(remcom_in_buffer));
1111 gdbstub_use_prev_in_buf = strlen(remcom_in_buffer);
1112 gdbstub_prev_in_buf_pos = 0;
1115 dbg_io_ops->write_char('+');
1116 put_packet(remcom_out_buffer);
1121 * gdbstub_exit - Send an exit message to GDB
1122 * @status: The exit code to report.
1124 void gdbstub_exit(int status)
1126 unsigned char checksum, ch, buffer[3];
1129 if (!kgdb_connected)
1133 if (!dbg_io_ops || dbg_kdb_mode)
1137 buffer[1] = hex_asc_hi(status);
1138 buffer[2] = hex_asc_lo(status);
1140 dbg_io_ops->write_char('$');
1143 for (loop = 0; loop < 3; loop++) {
1146 dbg_io_ops->write_char(ch);
1149 dbg_io_ops->write_char('#');
1150 dbg_io_ops->write_char(hex_asc_hi(checksum));
1151 dbg_io_ops->write_char(hex_asc_lo(checksum));
1153 /* make sure the output is flushed, lest the bootloader clobber it */
1154 if (dbg_io_ops->flush)
1155 dbg_io_ops->flush();