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/kgdb.h>
33 #include <linux/kdb.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"
40 #define KGDB_MAX_THREAD_QUERY 17
42 /* Our I/O buffers. */
43 static char remcom_in_buffer
[BUFMAX
];
44 static char remcom_out_buffer
[BUFMAX
];
45 static int gdbstub_use_prev_in_buf
;
46 static int gdbstub_prev_in_buf_pos
;
48 /* Storage for the registers, in GDB format. */
49 static unsigned long gdb_regs
[(NUMREGBYTES
+
50 sizeof(unsigned long) - 1) /
51 sizeof(unsigned long)];
54 * GDB remote protocol parser:
57 #ifdef CONFIG_KGDB_KDB
58 static int gdbstub_read_wait(void)
63 if (unlikely(gdbstub_use_prev_in_buf
)) {
64 if (gdbstub_prev_in_buf_pos
< gdbstub_use_prev_in_buf
)
65 return remcom_in_buffer
[gdbstub_prev_in_buf_pos
++];
67 gdbstub_use_prev_in_buf
= 0;
70 /* poll any additional I/O interfaces that are defined */
72 for (i
= 0; kdb_poll_funcs
[i
] != NULL
; i
++) {
73 ret
= kdb_poll_funcs
[i
]();
80 static int gdbstub_read_wait(void)
82 int ret
= dbg_io_ops
->read_char();
83 while (ret
== NO_POLL_CHAR
)
84 ret
= dbg_io_ops
->read_char();
88 /* scan for the sequence $<data>#<checksum> */
89 static void get_packet(char *buffer
)
91 unsigned char checksum
;
92 unsigned char xmitcsum
;
98 * Spin and wait around for the start character, ignore all
101 while ((ch
= (gdbstub_read_wait())) != '$')
111 * now, read until a # or end of buffer is found:
113 while (count
< (BUFMAX
- 1)) {
114 ch
= gdbstub_read_wait();
117 checksum
= checksum
+ ch
;
123 xmitcsum
= hex_to_bin(gdbstub_read_wait()) << 4;
124 xmitcsum
+= hex_to_bin(gdbstub_read_wait());
126 if (checksum
!= xmitcsum
)
127 /* failed checksum */
128 dbg_io_ops
->write_char('-');
130 /* successful transfer */
131 dbg_io_ops
->write_char('+');
132 if (dbg_io_ops
->flush
)
136 } while (checksum
!= xmitcsum
);
140 * Send the packet in buffer.
141 * Check for gdb connection if asked for.
143 static void put_packet(char *buffer
)
145 unsigned char checksum
;
150 * $<packet info>#<checksum>.
153 dbg_io_ops
->write_char('$');
157 while ((ch
= buffer
[count
])) {
158 dbg_io_ops
->write_char(ch
);
163 dbg_io_ops
->write_char('#');
164 dbg_io_ops
->write_char(hex_asc_hi(checksum
));
165 dbg_io_ops
->write_char(hex_asc_lo(checksum
));
166 if (dbg_io_ops
->flush
)
169 /* Now see what we get in reply. */
170 ch
= gdbstub_read_wait();
173 ch
= gdbstub_read_wait();
175 /* If we get an ACK, we are done. */
180 * If we get the start of another packet, this means
181 * that GDB is attempting to reconnect. We will NAK
182 * the packet being sent, and stop trying to send this
186 dbg_io_ops
->write_char('-');
187 if (dbg_io_ops
->flush
)
194 static char gdbmsgbuf
[BUFMAX
+ 1];
196 void gdbstub_msg_write(const char *s
, int len
)
208 /* Fill and send buffers... */
210 bufptr
= gdbmsgbuf
+ 1;
212 /* Calculate how many this time */
213 if ((len
<< 1) > (BUFMAX
- 2))
214 wcount
= (BUFMAX
- 2) >> 1;
218 /* Pack in hex chars */
219 for (i
= 0; i
< wcount
; i
++)
220 bufptr
= hex_byte_pack(bufptr
, s
[i
]);
228 put_packet(gdbmsgbuf
);
233 * Convert the memory pointed to by mem into hex, placing result in
234 * buf. Return a pointer to the last char put in buf (null). May
237 char *kgdb_mem2hex(char *mem
, char *buf
, int count
)
243 * We use the upper half of buf as an intermediate buffer for the
244 * raw memory copy. Hex conversion will work against this one.
248 err
= probe_kernel_read(tmp
, mem
, count
);
252 buf
= hex_byte_pack(buf
, *tmp
);
262 * Convert the hex array pointed to by buf into binary to be placed in
263 * mem. Return a pointer to the character AFTER the last byte
264 * written. May return an error.
266 int kgdb_hex2mem(char *buf
, char *mem
, int count
)
272 * We use the upper half of buf as an intermediate buffer for the
273 * raw memory that is converted from hex.
275 tmp_raw
= buf
+ count
* 2;
277 tmp_hex
= tmp_raw
- 1;
278 while (tmp_hex
>= buf
) {
280 *tmp_raw
= hex_to_bin(*tmp_hex
--);
281 *tmp_raw
|= hex_to_bin(*tmp_hex
--) << 4;
284 return probe_kernel_write(mem
, tmp_raw
, count
);
288 * While we find nice hex chars, build a long_val.
289 * Return number of chars processed.
291 int kgdb_hex2long(char **ptr
, unsigned long *long_val
)
304 hex_val
= hex_to_bin(**ptr
);
308 *long_val
= (*long_val
<< 4) | hex_val
;
314 *long_val
= -*long_val
;
320 * Copy the binary array pointed to by buf into mem. Fix $, #, and
321 * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success.
322 * The input buf is overwitten with the result to write to mem.
324 static int kgdb_ebin2mem(char *buf
, char *mem
, int count
)
329 while (count
-- > 0) {
332 c
[size
] = *buf
++ ^ 0x20;
336 return probe_kernel_write(mem
, c
, size
);
339 #if DBG_MAX_REG_NUM > 0
340 void pt_regs_to_gdb_regs(unsigned long *gdb_regs
, struct pt_regs
*regs
)
344 char *ptr
= (char *)gdb_regs
;
346 for (i
= 0; i
< DBG_MAX_REG_NUM
; i
++) {
347 dbg_get_reg(i
, ptr
+ idx
, regs
);
348 idx
+= dbg_reg_def
[i
].size
;
352 void gdb_regs_to_pt_regs(unsigned long *gdb_regs
, struct pt_regs
*regs
)
356 char *ptr
= (char *)gdb_regs
;
358 for (i
= 0; i
< DBG_MAX_REG_NUM
; i
++) {
359 dbg_set_reg(i
, ptr
+ idx
, regs
);
360 idx
+= dbg_reg_def
[i
].size
;
363 #endif /* DBG_MAX_REG_NUM > 0 */
365 /* Write memory due to an 'M' or 'X' packet. */
366 static int write_mem_msg(int binary
)
368 char *ptr
= &remcom_in_buffer
[1];
370 unsigned long length
;
373 if (kgdb_hex2long(&ptr
, &addr
) > 0 && *(ptr
++) == ',' &&
374 kgdb_hex2long(&ptr
, &length
) > 0 && *(ptr
++) == ':') {
376 err
= kgdb_ebin2mem(ptr
, (char *)addr
, length
);
378 err
= kgdb_hex2mem(ptr
, (char *)addr
, length
);
381 if (CACHE_FLUSH_IS_SAFE
)
382 flush_icache_range(addr
, addr
+ length
);
389 static void error_packet(char *pkt
, int error
)
393 pkt
[1] = hex_asc
[(error
/ 10)];
394 pkt
[2] = hex_asc
[(error
% 10)];
399 * Thread ID accessors. We represent a flat TID space to GDB, where
400 * the per CPU idle threads (which under Linux all have PID 0) are
401 * remapped to negative TIDs.
404 #define BUF_THREAD_ID_SIZE 8
406 static char *pack_threadid(char *pkt
, unsigned char *id
)
408 unsigned char *limit
;
411 limit
= id
+ (BUF_THREAD_ID_SIZE
/ 2);
413 if (!lzero
|| *id
!= 0) {
414 pkt
= hex_byte_pack(pkt
, *id
);
421 pkt
= hex_byte_pack(pkt
, 0);
426 static void int_to_threadref(unsigned char *id
, int value
)
428 put_unaligned_be32(value
, id
);
431 static struct task_struct
*getthread(struct pt_regs
*regs
, int tid
)
434 * Non-positive TIDs are remapped to the cpu shadow information
436 if (tid
== 0 || tid
== -1)
437 tid
= -atomic_read(&kgdb_active
) - 2;
438 if (tid
< -1 && tid
> -NR_CPUS
- 2) {
439 if (kgdb_info
[-tid
- 2].task
)
440 return kgdb_info
[-tid
- 2].task
;
442 return idle_task(-tid
- 2);
445 printk(KERN_ERR
"KGDB: Internal thread select error\n");
451 * find_task_by_pid_ns() does not take the tasklist lock anymore
452 * but is nicely RCU locked - hence is a pretty resilient
455 return find_task_by_pid_ns(tid
, &init_pid_ns
);
460 * Remap normal tasks to their real PID,
461 * CPU shadow threads are mapped to -CPU - 2
463 static inline int shadow_pid(int realpid
)
468 return -raw_smp_processor_id() - 2;
472 * All the functions that start with gdb_cmd are the various
473 * operations to implement the handlers for the gdbserial protocol
474 * where KGDB is communicating with an external debugger
477 /* Handle the '?' status packets */
478 static void gdb_cmd_status(struct kgdb_state
*ks
)
481 * We know that this packet is only sent
482 * during initial connect. So to be safe,
483 * we clear out our breakpoints now in case
484 * GDB is reconnecting.
486 dbg_remove_all_break();
488 remcom_out_buffer
[0] = 'S';
489 hex_byte_pack(&remcom_out_buffer
[1], ks
->signo
);
492 static void gdb_get_regs_helper(struct kgdb_state
*ks
)
494 struct task_struct
*thread
;
495 void *local_debuggerinfo
;
498 thread
= kgdb_usethread
;
500 thread
= kgdb_info
[ks
->cpu
].task
;
501 local_debuggerinfo
= kgdb_info
[ks
->cpu
].debuggerinfo
;
503 local_debuggerinfo
= NULL
;
504 for_each_online_cpu(i
) {
506 * Try to find the task on some other
507 * or possibly this node if we do not
508 * find the matching task then we try
509 * to approximate the results.
511 if (thread
== kgdb_info
[i
].task
)
512 local_debuggerinfo
= kgdb_info
[i
].debuggerinfo
;
517 * All threads that don't have debuggerinfo should be
518 * in schedule() sleeping, since all other CPUs
519 * are in kgdb_wait, and thus have debuggerinfo.
521 if (local_debuggerinfo
) {
522 pt_regs_to_gdb_regs(gdb_regs
, local_debuggerinfo
);
525 * Pull stuff saved during switch_to; nothing
526 * else is accessible (or even particularly
529 * This should be enough for a stack trace.
531 sleeping_thread_to_gdb_regs(gdb_regs
, thread
);
535 /* Handle the 'g' get registers request */
536 static void gdb_cmd_getregs(struct kgdb_state
*ks
)
538 gdb_get_regs_helper(ks
);
539 kgdb_mem2hex((char *)gdb_regs
, remcom_out_buffer
, NUMREGBYTES
);
542 /* Handle the 'G' set registers request */
543 static void gdb_cmd_setregs(struct kgdb_state
*ks
)
545 kgdb_hex2mem(&remcom_in_buffer
[1], (char *)gdb_regs
, NUMREGBYTES
);
547 if (kgdb_usethread
&& kgdb_usethread
!= current
) {
548 error_packet(remcom_out_buffer
, -EINVAL
);
550 gdb_regs_to_pt_regs(gdb_regs
, ks
->linux_regs
);
551 strcpy(remcom_out_buffer
, "OK");
555 /* Handle the 'm' memory read bytes */
556 static void gdb_cmd_memread(struct kgdb_state
*ks
)
558 char *ptr
= &remcom_in_buffer
[1];
559 unsigned long length
;
563 if (kgdb_hex2long(&ptr
, &addr
) > 0 && *ptr
++ == ',' &&
564 kgdb_hex2long(&ptr
, &length
) > 0) {
565 err
= kgdb_mem2hex((char *)addr
, remcom_out_buffer
, length
);
567 error_packet(remcom_out_buffer
, -EINVAL
);
569 error_packet(remcom_out_buffer
, -EINVAL
);
573 /* Handle the 'M' memory write bytes */
574 static void gdb_cmd_memwrite(struct kgdb_state
*ks
)
576 int err
= write_mem_msg(0);
579 error_packet(remcom_out_buffer
, err
);
581 strcpy(remcom_out_buffer
, "OK");
584 #if DBG_MAX_REG_NUM > 0
585 static char *gdb_hex_reg_helper(int regnum
, char *out
)
590 for (i
= 0; i
< regnum
; i
++)
591 offset
+= dbg_reg_def
[i
].size
;
592 return kgdb_mem2hex((char *)gdb_regs
+ offset
, out
,
593 dbg_reg_def
[i
].size
);
596 /* Handle the 'p' individual regster get */
597 static void gdb_cmd_reg_get(struct kgdb_state
*ks
)
599 unsigned long regnum
;
600 char *ptr
= &remcom_in_buffer
[1];
602 kgdb_hex2long(&ptr
, ®num
);
603 if (regnum
>= DBG_MAX_REG_NUM
) {
604 error_packet(remcom_out_buffer
, -EINVAL
);
607 gdb_get_regs_helper(ks
);
608 gdb_hex_reg_helper(regnum
, remcom_out_buffer
);
611 /* Handle the 'P' individual regster set */
612 static void gdb_cmd_reg_set(struct kgdb_state
*ks
)
614 unsigned long regnum
;
615 char *ptr
= &remcom_in_buffer
[1];
618 kgdb_hex2long(&ptr
, ®num
);
620 !(!kgdb_usethread
|| kgdb_usethread
== current
) ||
621 !dbg_get_reg(regnum
, gdb_regs
, ks
->linux_regs
)) {
622 error_packet(remcom_out_buffer
, -EINVAL
);
625 memset(gdb_regs
, 0, sizeof(gdb_regs
));
626 while (i
< sizeof(gdb_regs
) * 2)
627 if (hex_to_bin(ptr
[i
]) >= 0)
632 kgdb_hex2mem(ptr
, (char *)gdb_regs
, i
);
633 dbg_set_reg(regnum
, gdb_regs
, ks
->linux_regs
);
634 strcpy(remcom_out_buffer
, "OK");
636 #endif /* DBG_MAX_REG_NUM > 0 */
638 /* Handle the 'X' memory binary write bytes */
639 static void gdb_cmd_binwrite(struct kgdb_state
*ks
)
641 int err
= write_mem_msg(1);
644 error_packet(remcom_out_buffer
, err
);
646 strcpy(remcom_out_buffer
, "OK");
649 /* Handle the 'D' or 'k', detach or kill packets */
650 static void gdb_cmd_detachkill(struct kgdb_state
*ks
)
654 /* The detach case */
655 if (remcom_in_buffer
[0] == 'D') {
656 error
= dbg_remove_all_break();
658 error_packet(remcom_out_buffer
, error
);
660 strcpy(remcom_out_buffer
, "OK");
663 put_packet(remcom_out_buffer
);
666 * Assume the kill case, with no exit code checking,
667 * trying to force detach the debugger:
669 dbg_remove_all_break();
674 /* Handle the 'R' reboot packets */
675 static int gdb_cmd_reboot(struct kgdb_state
*ks
)
677 /* For now, only honor R0 */
678 if (strcmp(remcom_in_buffer
, "R0") == 0) {
679 printk(KERN_CRIT
"Executing emergency reboot\n");
680 strcpy(remcom_out_buffer
, "OK");
681 put_packet(remcom_out_buffer
);
684 * Execution should not return from
685 * machine_emergency_restart()
687 machine_emergency_restart();
695 /* Handle the 'q' query packets */
696 static void gdb_cmd_query(struct kgdb_state
*ks
)
698 struct task_struct
*g
;
699 struct task_struct
*p
;
700 unsigned char thref
[BUF_THREAD_ID_SIZE
];
706 switch (remcom_in_buffer
[1]) {
709 if (memcmp(remcom_in_buffer
+ 2, "ThreadInfo", 10))
713 remcom_out_buffer
[0] = 'm';
714 ptr
= remcom_out_buffer
+ 1;
715 if (remcom_in_buffer
[1] == 'f') {
716 /* Each cpu is a shadow thread */
717 for_each_online_cpu(cpu
) {
719 int_to_threadref(thref
, -cpu
- 2);
720 ptr
= pack_threadid(ptr
, thref
);
726 do_each_thread(g
, p
) {
727 if (i
>= ks
->thr_query
&& !finished
) {
728 int_to_threadref(thref
, p
->pid
);
729 ptr
= pack_threadid(ptr
, thref
);
732 if (ks
->thr_query
% KGDB_MAX_THREAD_QUERY
== 0)
736 } while_each_thread(g
, p
);
742 /* Current thread id */
743 strcpy(remcom_out_buffer
, "QC");
744 ks
->threadid
= shadow_pid(current
->pid
);
745 int_to_threadref(thref
, ks
->threadid
);
746 pack_threadid(remcom_out_buffer
+ 2, thref
);
749 if (memcmp(remcom_in_buffer
+ 1, "ThreadExtraInfo,", 16))
753 ptr
= remcom_in_buffer
+ 17;
754 kgdb_hex2long(&ptr
, &ks
->threadid
);
755 if (!getthread(ks
->linux_regs
, ks
->threadid
)) {
756 error_packet(remcom_out_buffer
, -EINVAL
);
759 if ((int)ks
->threadid
> 0) {
760 kgdb_mem2hex(getthread(ks
->linux_regs
,
762 remcom_out_buffer
, 16);
764 static char tmpstr
[23 + BUF_THREAD_ID_SIZE
];
766 sprintf(tmpstr
, "shadowCPU%d",
767 (int)(-ks
->threadid
- 2));
768 kgdb_mem2hex(tmpstr
, remcom_out_buffer
, strlen(tmpstr
));
771 #ifdef CONFIG_KGDB_KDB
773 if (strncmp(remcom_in_buffer
, "qRcmd,", 6) == 0) {
774 int len
= strlen(remcom_in_buffer
+ 6);
776 if ((len
% 2) != 0) {
777 strcpy(remcom_out_buffer
, "E01");
780 kgdb_hex2mem(remcom_in_buffer
+ 6,
781 remcom_out_buffer
, len
);
783 remcom_out_buffer
[len
++] = 0;
785 kdb_parse(remcom_out_buffer
);
786 strcpy(remcom_out_buffer
, "OK");
793 /* Handle the 'H' task query packets */
794 static void gdb_cmd_task(struct kgdb_state
*ks
)
796 struct task_struct
*thread
;
799 switch (remcom_in_buffer
[1]) {
801 ptr
= &remcom_in_buffer
[2];
802 kgdb_hex2long(&ptr
, &ks
->threadid
);
803 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
804 if (!thread
&& ks
->threadid
> 0) {
805 error_packet(remcom_out_buffer
, -EINVAL
);
808 kgdb_usethread
= thread
;
809 ks
->kgdb_usethreadid
= ks
->threadid
;
810 strcpy(remcom_out_buffer
, "OK");
813 ptr
= &remcom_in_buffer
[2];
814 kgdb_hex2long(&ptr
, &ks
->threadid
);
816 kgdb_contthread
= NULL
;
818 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
819 if (!thread
&& ks
->threadid
> 0) {
820 error_packet(remcom_out_buffer
, -EINVAL
);
823 kgdb_contthread
= thread
;
825 strcpy(remcom_out_buffer
, "OK");
830 /* Handle the 'T' thread query packets */
831 static void gdb_cmd_thread(struct kgdb_state
*ks
)
833 char *ptr
= &remcom_in_buffer
[1];
834 struct task_struct
*thread
;
836 kgdb_hex2long(&ptr
, &ks
->threadid
);
837 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
839 strcpy(remcom_out_buffer
, "OK");
841 error_packet(remcom_out_buffer
, -EINVAL
);
844 /* Handle the 'z' or 'Z' breakpoint remove or set packets */
845 static void gdb_cmd_break(struct kgdb_state
*ks
)
848 * Since GDB-5.3, it's been drafted that '0' is a software
849 * breakpoint, '1' is a hardware breakpoint, so let's do that.
851 char *bpt_type
= &remcom_in_buffer
[1];
852 char *ptr
= &remcom_in_buffer
[2];
854 unsigned long length
;
857 if (arch_kgdb_ops
.set_hw_breakpoint
&& *bpt_type
>= '1') {
862 if (*bpt_type
!= '0' && *bpt_type
!= '1')
868 * Test if this is a hardware breakpoint, and
871 if (*bpt_type
== '1' && !(arch_kgdb_ops
.flags
& KGDB_HW_BREAKPOINT
))
875 if (*(ptr
++) != ',') {
876 error_packet(remcom_out_buffer
, -EINVAL
);
879 if (!kgdb_hex2long(&ptr
, &addr
)) {
880 error_packet(remcom_out_buffer
, -EINVAL
);
883 if (*(ptr
++) != ',' ||
884 !kgdb_hex2long(&ptr
, &length
)) {
885 error_packet(remcom_out_buffer
, -EINVAL
);
889 if (remcom_in_buffer
[0] == 'Z' && *bpt_type
== '0')
890 error
= dbg_set_sw_break(addr
);
891 else if (remcom_in_buffer
[0] == 'z' && *bpt_type
== '0')
892 error
= dbg_remove_sw_break(addr
);
893 else if (remcom_in_buffer
[0] == 'Z')
894 error
= arch_kgdb_ops
.set_hw_breakpoint(addr
,
895 (int)length
, *bpt_type
- '0');
896 else if (remcom_in_buffer
[0] == 'z')
897 error
= arch_kgdb_ops
.remove_hw_breakpoint(addr
,
898 (int) length
, *bpt_type
- '0');
901 strcpy(remcom_out_buffer
, "OK");
903 error_packet(remcom_out_buffer
, error
);
906 /* Handle the 'C' signal / exception passing packets */
907 static int gdb_cmd_exception_pass(struct kgdb_state
*ks
)
909 /* C09 == pass exception
910 * C15 == detach kgdb, pass exception
912 if (remcom_in_buffer
[1] == '0' && remcom_in_buffer
[2] == '9') {
914 ks
->pass_exception
= 1;
915 remcom_in_buffer
[0] = 'c';
917 } else if (remcom_in_buffer
[1] == '1' && remcom_in_buffer
[2] == '5') {
919 ks
->pass_exception
= 1;
920 remcom_in_buffer
[0] = 'D';
921 dbg_remove_all_break();
926 gdbstub_msg_write("KGDB only knows signal 9 (pass)"
927 " and 15 (pass and disconnect)\n"
928 "Executing a continue without signal passing\n", 0);
929 remcom_in_buffer
[0] = 'c';
932 /* Indicate fall through */
937 * This function performs all gdbserial command procesing
939 int gdb_serial_stub(struct kgdb_state
*ks
)
944 /* Initialize comm buffer and globals. */
945 memset(remcom_out_buffer
, 0, sizeof(remcom_out_buffer
));
946 kgdb_usethread
= kgdb_info
[ks
->cpu
].task
;
947 ks
->kgdb_usethreadid
= shadow_pid(kgdb_info
[ks
->cpu
].task
->pid
);
948 ks
->pass_exception
= 0;
950 if (kgdb_connected
) {
951 unsigned char thref
[BUF_THREAD_ID_SIZE
];
954 /* Reply to host that an exception has occurred */
955 ptr
= remcom_out_buffer
;
957 ptr
= hex_byte_pack(ptr
, ks
->signo
);
958 ptr
+= strlen(strcpy(ptr
, "thread:"));
959 int_to_threadref(thref
, shadow_pid(current
->pid
));
960 ptr
= pack_threadid(ptr
, thref
);
962 put_packet(remcom_out_buffer
);
968 /* Clear the out buffer. */
969 memset(remcom_out_buffer
, 0, sizeof(remcom_out_buffer
));
971 get_packet(remcom_in_buffer
);
973 switch (remcom_in_buffer
[0]) {
974 case '?': /* gdbserial status */
977 case 'g': /* return the value of the CPU registers */
980 case 'G': /* set the value of the CPU registers - return OK */
983 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
986 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
987 gdb_cmd_memwrite(ks
);
989 #if DBG_MAX_REG_NUM > 0
990 case 'p': /* pXX Return gdb register XX (in hex) */
993 case 'P': /* PXX=aaaa Set gdb register XX to aaaa (in hex) */
996 #endif /* DBG_MAX_REG_NUM > 0 */
997 case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
998 gdb_cmd_binwrite(ks
);
1000 /* kill or detach. KGDB should treat this like a
1003 case 'D': /* Debugger detach */
1004 case 'k': /* Debugger detach via kill */
1005 gdb_cmd_detachkill(ks
);
1006 goto default_handle
;
1007 case 'R': /* Reboot */
1008 if (gdb_cmd_reboot(ks
))
1009 goto default_handle
;
1011 case 'q': /* query command */
1014 case 'H': /* task related */
1017 case 'T': /* Query thread status */
1020 case 'z': /* Break point remove */
1021 case 'Z': /* Break point set */
1024 #ifdef CONFIG_KGDB_KDB
1025 case '3': /* Escape into back into kdb */
1026 if (remcom_in_buffer
[1] == '\0') {
1027 gdb_cmd_detachkill(ks
);
1028 return DBG_PASS_EVENT
;
1031 case 'C': /* Exception passing */
1032 tmp
= gdb_cmd_exception_pass(ks
);
1034 goto default_handle
;
1037 /* Fall through on tmp < 0 */
1038 case 'c': /* Continue packet */
1039 case 's': /* Single step packet */
1040 if (kgdb_contthread
&& kgdb_contthread
!= current
) {
1041 /* Can't switch threads in kgdb */
1042 error_packet(remcom_out_buffer
, -EINVAL
);
1045 dbg_activate_sw_breakpoints();
1046 /* Fall through to default processing */
1049 error
= kgdb_arch_handle_exception(ks
->ex_vector
,
1056 * Leave cmd processing on error, detach,
1057 * kill, continue, or single step.
1059 if (error
>= 0 || remcom_in_buffer
[0] == 'D' ||
1060 remcom_in_buffer
[0] == 'k') {
1067 /* reply to the request */
1068 put_packet(remcom_out_buffer
);
1072 if (ks
->pass_exception
)
1077 int gdbstub_state(struct kgdb_state
*ks
, char *cmd
)
1083 error
= kgdb_arch_handle_exception(ks
->ex_vector
,
1092 strcpy(remcom_in_buffer
, cmd
);
1095 strcpy(remcom_in_buffer
, cmd
);
1096 gdbstub_use_prev_in_buf
= strlen(remcom_in_buffer
);
1097 gdbstub_prev_in_buf_pos
= 0;
1100 dbg_io_ops
->write_char('+');
1101 put_packet(remcom_out_buffer
);
1106 * gdbstub_exit - Send an exit message to GDB
1107 * @status: The exit code to report.
1109 void gdbstub_exit(int status
)
1111 unsigned char checksum
, ch
, buffer
[3];
1114 if (!kgdb_connected
)
1118 if (!dbg_io_ops
|| dbg_kdb_mode
)
1122 buffer
[1] = hex_asc_hi(status
);
1123 buffer
[2] = hex_asc_lo(status
);
1125 dbg_io_ops
->write_char('$');
1128 for (loop
= 0; loop
< 3; loop
++) {
1131 dbg_io_ops
->write_char(ch
);
1134 dbg_io_ops
->write_char('#');
1135 dbg_io_ops
->write_char(hex_asc_hi(checksum
));
1136 dbg_io_ops
->write_char(hex_asc_lo(checksum
));
1138 /* make sure the output is flushed, lest the bootloader clobber it */
1139 if (dbg_io_ops
->flush
)
1140 dbg_io_ops
->flush();