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@suse.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-2008 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.
30 #include <linux/pid_namespace.h>
31 #include <linux/interrupt.h>
32 #include <linux/spinlock.h>
33 #include <linux/console.h>
34 #include <linux/threads.h>
35 #include <linux/uaccess.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/ptrace.h>
39 #include <linux/reboot.h>
40 #include <linux/string.h>
41 #include <linux/delay.h>
42 #include <linux/sched.h>
43 #include <linux/sysrq.h>
44 #include <linux/init.h>
45 #include <linux/kgdb.h>
46 #include <linux/pid.h>
47 #include <linux/smp.h>
50 #include <asm/cacheflush.h>
51 #include <asm/byteorder.h>
52 #include <asm/atomic.h>
53 #include <asm/system.h>
55 static int kgdb_break_asap
;
64 long kgdb_usethreadid
;
65 struct pt_regs
*linux_regs
;
68 static struct debuggerinfo_struct
{
70 struct task_struct
*task
;
74 * kgdb_connected - Is a host GDB connected to us?
77 EXPORT_SYMBOL_GPL(kgdb_connected
);
79 /* All the KGDB handlers are installed */
80 static int kgdb_io_module_registered
;
82 /* Guard for recursive entry */
83 static int exception_level
;
85 static struct kgdb_io
*kgdb_io_ops
;
86 static DEFINE_SPINLOCK(kgdb_registration_lock
);
88 /* kgdb console driver is loaded */
89 static int kgdb_con_registered
;
90 /* determine if kgdb console output should be used */
91 static int kgdb_use_con
;
93 static int __init
opt_kgdb_con(char *str
)
99 early_param("kgdbcon", opt_kgdb_con
);
101 module_param(kgdb_use_con
, int, 0644);
104 * Holds information about breakpoints in a kernel. These breakpoints are
105 * added and removed by gdb.
107 static struct kgdb_bkpt kgdb_break
[KGDB_MAX_BREAKPOINTS
] = {
108 [0 ... KGDB_MAX_BREAKPOINTS
-1] = { .state
= BP_UNDEFINED
}
112 * The CPU# of the active CPU, or -1 if none:
114 atomic_t kgdb_active
= ATOMIC_INIT(-1);
117 * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
118 * bootup code (which might not have percpu set up yet):
120 static atomic_t passive_cpu_wait
[NR_CPUS
];
121 static atomic_t cpu_in_kgdb
[NR_CPUS
];
122 atomic_t kgdb_setting_breakpoint
;
124 struct task_struct
*kgdb_usethread
;
125 struct task_struct
*kgdb_contthread
;
127 int kgdb_single_step
;
129 /* Our I/O buffers. */
130 static char remcom_in_buffer
[BUFMAX
];
131 static char remcom_out_buffer
[BUFMAX
];
133 /* Storage for the registers, in GDB format. */
134 static unsigned long gdb_regs
[(NUMREGBYTES
+
135 sizeof(unsigned long) - 1) /
136 sizeof(unsigned long)];
138 /* to keep track of the CPU which is doing the single stepping*/
139 atomic_t kgdb_cpu_doing_single_step
= ATOMIC_INIT(-1);
142 * If you are debugging a problem where roundup (the collection of
143 * all other CPUs) is a problem [this should be extremely rare],
144 * then use the nokgdbroundup option to avoid roundup. In that case
145 * the other CPUs might interfere with your debugging context, so
146 * use this with care:
148 int kgdb_do_roundup
= 1;
150 static int __init
opt_nokgdbroundup(char *str
)
157 early_param("nokgdbroundup", opt_nokgdbroundup
);
160 * Finally, some KGDB code :-)
164 * Weak aliases for breakpoint management,
165 * can be overriden by architectures when needed:
167 int __weak
kgdb_validate_break_address(unsigned long addr
)
169 char tmp_variable
[BREAK_INSTR_SIZE
];
171 return probe_kernel_read(tmp_variable
, (char *)addr
, BREAK_INSTR_SIZE
);
174 int __weak
kgdb_arch_set_breakpoint(unsigned long addr
, char *saved_instr
)
178 err
= probe_kernel_read(saved_instr
, (char *)addr
, BREAK_INSTR_SIZE
);
182 return probe_kernel_write((char *)addr
, arch_kgdb_ops
.gdb_bpt_instr
,
186 int __weak
kgdb_arch_remove_breakpoint(unsigned long addr
, char *bundle
)
188 return probe_kernel_write((char *)addr
,
189 (char *)bundle
, BREAK_INSTR_SIZE
);
192 unsigned long __weak
kgdb_arch_pc(int exception
, struct pt_regs
*regs
)
194 return instruction_pointer(regs
);
197 int __weak
kgdb_arch_init(void)
203 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
204 * @regs: Current &struct pt_regs.
206 * This function will be called if the particular architecture must
207 * disable hardware debugging while it is processing gdb packets or
208 * handling exception.
210 void __weak
kgdb_disable_hw_debug(struct pt_regs
*regs
)
215 * GDB remote protocol parser:
218 static const char hexchars
[] = "0123456789abcdef";
220 static int hex(char ch
)
222 if ((ch
>= 'a') && (ch
<= 'f'))
223 return ch
- 'a' + 10;
224 if ((ch
>= '0') && (ch
<= '9'))
226 if ((ch
>= 'A') && (ch
<= 'F'))
227 return ch
- 'A' + 10;
231 /* scan for the sequence $<data>#<checksum> */
232 static void get_packet(char *buffer
)
234 unsigned char checksum
;
235 unsigned char xmitcsum
;
241 * Spin and wait around for the start character, ignore all
244 while ((ch
= (kgdb_io_ops
->read_char())) != '$')
254 * now, read until a # or end of buffer is found:
256 while (count
< (BUFMAX
- 1)) {
257 ch
= kgdb_io_ops
->read_char();
260 checksum
= checksum
+ ch
;
267 xmitcsum
= hex(kgdb_io_ops
->read_char()) << 4;
268 xmitcsum
+= hex(kgdb_io_ops
->read_char());
270 if (checksum
!= xmitcsum
)
271 /* failed checksum */
272 kgdb_io_ops
->write_char('-');
274 /* successful transfer */
275 kgdb_io_ops
->write_char('+');
276 if (kgdb_io_ops
->flush
)
277 kgdb_io_ops
->flush();
279 } while (checksum
!= xmitcsum
);
283 * Send the packet in buffer.
284 * Check for gdb connection if asked for.
286 static void put_packet(char *buffer
)
288 unsigned char checksum
;
293 * $<packet info>#<checksum>.
296 kgdb_io_ops
->write_char('$');
300 while ((ch
= buffer
[count
])) {
301 kgdb_io_ops
->write_char(ch
);
306 kgdb_io_ops
->write_char('#');
307 kgdb_io_ops
->write_char(hexchars
[checksum
>> 4]);
308 kgdb_io_ops
->write_char(hexchars
[checksum
& 0xf]);
309 if (kgdb_io_ops
->flush
)
310 kgdb_io_ops
->flush();
312 /* Now see what we get in reply. */
313 ch
= kgdb_io_ops
->read_char();
316 ch
= kgdb_io_ops
->read_char();
318 /* If we get an ACK, we are done. */
323 * If we get the start of another packet, this means
324 * that GDB is attempting to reconnect. We will NAK
325 * the packet being sent, and stop trying to send this
329 kgdb_io_ops
->write_char('-');
330 if (kgdb_io_ops
->flush
)
331 kgdb_io_ops
->flush();
337 static char *pack_hex_byte(char *pkt
, u8 byte
)
339 *pkt
++ = hexchars
[byte
>> 4];
340 *pkt
++ = hexchars
[byte
& 0xf];
346 * Convert the memory pointed to by mem into hex, placing result in buf.
347 * Return a pointer to the last char put in buf (null). May return an error.
349 int kgdb_mem2hex(char *mem
, char *buf
, int count
)
355 * We use the upper half of buf as an intermediate buffer for the
356 * raw memory copy. Hex conversion will work against this one.
360 err
= probe_kernel_read(tmp
, mem
, count
);
363 buf
= pack_hex_byte(buf
, *tmp
);
375 * Copy the binary array pointed to by buf into mem. Fix $, #, and
376 * 0x7d escaped with 0x7d. Return a pointer to the character after
377 * the last byte written.
379 static int kgdb_ebin2mem(char *buf
, char *mem
, int count
)
384 while (count
-- > 0) {
389 err
= probe_kernel_write(mem
, &c
, 1);
400 * Convert the hex array pointed to by buf into binary to be placed in mem.
401 * Return a pointer to the character AFTER the last byte written.
402 * May return an error.
404 int kgdb_hex2mem(char *buf
, char *mem
, int count
)
410 * We use the upper half of buf as an intermediate buffer for the
411 * raw memory that is converted from hex.
413 tmp_raw
= buf
+ count
* 2;
415 tmp_hex
= tmp_raw
- 1;
416 while (tmp_hex
>= buf
) {
418 *tmp_raw
= hex(*tmp_hex
--);
419 *tmp_raw
|= hex(*tmp_hex
--) << 4;
422 return probe_kernel_write(mem
, tmp_raw
, count
);
426 * While we find nice hex chars, build a long_val.
427 * Return number of chars processed.
429 int kgdb_hex2long(char **ptr
, long *long_val
)
437 hex_val
= hex(**ptr
);
441 *long_val
= (*long_val
<< 4) | hex_val
;
449 /* Write memory due to an 'M' or 'X' packet. */
450 static int write_mem_msg(int binary
)
452 char *ptr
= &remcom_in_buffer
[1];
454 unsigned long length
;
457 if (kgdb_hex2long(&ptr
, &addr
) > 0 && *(ptr
++) == ',' &&
458 kgdb_hex2long(&ptr
, &length
) > 0 && *(ptr
++) == ':') {
460 err
= kgdb_ebin2mem(ptr
, (char *)addr
, length
);
462 err
= kgdb_hex2mem(ptr
, (char *)addr
, length
);
465 if (CACHE_FLUSH_IS_SAFE
)
466 flush_icache_range(addr
, addr
+ length
+ 1);
473 static void error_packet(char *pkt
, int error
)
477 pkt
[1] = hexchars
[(error
/ 10)];
478 pkt
[2] = hexchars
[(error
% 10)];
483 * Thread ID accessors. We represent a flat TID space to GDB, where
484 * the per CPU idle threads (which under Linux all have PID 0) are
485 * remapped to negative TIDs.
488 #define BUF_THREAD_ID_SIZE 16
490 static char *pack_threadid(char *pkt
, unsigned char *id
)
494 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
496 pkt
= pack_hex_byte(pkt
, *id
++);
501 static void int_to_threadref(unsigned char *id
, int value
)
506 scan
= (unsigned char *)id
;
509 *scan
++ = (value
>> 24) & 0xff;
510 *scan
++ = (value
>> 16) & 0xff;
511 *scan
++ = (value
>> 8) & 0xff;
512 *scan
++ = (value
& 0xff);
515 static struct task_struct
*getthread(struct pt_regs
*regs
, int tid
)
518 * Non-positive TIDs are remapped idle tasks:
521 return idle_task(-tid
);
524 * find_task_by_pid_ns() does not take the tasklist lock anymore
525 * but is nicely RCU locked - hence is a pretty resilient
528 return find_task_by_pid_ns(tid
, &init_pid_ns
);
532 * CPU debug state control:
536 static void kgdb_wait(struct pt_regs
*regs
)
541 local_irq_save(flags
);
542 cpu
= raw_smp_processor_id();
543 kgdb_info
[cpu
].debuggerinfo
= regs
;
544 kgdb_info
[cpu
].task
= current
;
546 * Make sure the above info reaches the primary CPU before
547 * our cpu_in_kgdb[] flag setting does:
550 atomic_set(&cpu_in_kgdb
[cpu
], 1);
553 * The primary CPU must be active to enter here, but this is
554 * guard in case the primary CPU had not been selected if
555 * this was an entry via nmi.
557 while (atomic_read(&kgdb_active
) == -1)
560 /* Wait till primary CPU goes completely into the debugger. */
561 while (!atomic_read(&cpu_in_kgdb
[atomic_read(&kgdb_active
)]))
564 /* Wait till primary CPU is done with debugging */
565 while (atomic_read(&passive_cpu_wait
[cpu
]))
568 kgdb_info
[cpu
].debuggerinfo
= NULL
;
569 kgdb_info
[cpu
].task
= NULL
;
571 /* fix up hardware debug registers on local cpu */
572 if (arch_kgdb_ops
.correct_hw_break
)
573 arch_kgdb_ops
.correct_hw_break();
575 /* Signal the primary CPU that we are done: */
576 atomic_set(&cpu_in_kgdb
[cpu
], 0);
577 local_irq_restore(flags
);
582 * Some architectures need cache flushes when we set/clear a
585 static void kgdb_flush_swbreak_addr(unsigned long addr
)
587 if (!CACHE_FLUSH_IS_SAFE
)
591 flush_cache_range(current
->mm
->mmap_cache
,
592 addr
, addr
+ BREAK_INSTR_SIZE
);
594 flush_icache_range(addr
, addr
+ BREAK_INSTR_SIZE
);
599 * SW breakpoint management:
601 static int kgdb_activate_sw_breakpoints(void)
607 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
608 if (kgdb_break
[i
].state
!= BP_SET
)
611 addr
= kgdb_break
[i
].bpt_addr
;
612 error
= kgdb_arch_set_breakpoint(addr
,
613 kgdb_break
[i
].saved_instr
);
617 kgdb_flush_swbreak_addr(addr
);
618 kgdb_break
[i
].state
= BP_ACTIVE
;
623 static int kgdb_set_sw_break(unsigned long addr
)
625 int err
= kgdb_validate_break_address(addr
);
632 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
633 if ((kgdb_break
[i
].state
== BP_SET
) &&
634 (kgdb_break
[i
].bpt_addr
== addr
))
637 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
638 if (kgdb_break
[i
].state
== BP_REMOVED
&&
639 kgdb_break
[i
].bpt_addr
== addr
) {
646 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
647 if (kgdb_break
[i
].state
== BP_UNDEFINED
) {
657 kgdb_break
[breakno
].state
= BP_SET
;
658 kgdb_break
[breakno
].type
= BP_BREAKPOINT
;
659 kgdb_break
[breakno
].bpt_addr
= addr
;
664 static int kgdb_deactivate_sw_breakpoints(void)
670 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
671 if (kgdb_break
[i
].state
!= BP_ACTIVE
)
673 addr
= kgdb_break
[i
].bpt_addr
;
674 error
= kgdb_arch_remove_breakpoint(addr
,
675 kgdb_break
[i
].saved_instr
);
679 kgdb_flush_swbreak_addr(addr
);
680 kgdb_break
[i
].state
= BP_SET
;
685 static int kgdb_remove_sw_break(unsigned long addr
)
689 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
690 if ((kgdb_break
[i
].state
== BP_SET
) &&
691 (kgdb_break
[i
].bpt_addr
== addr
)) {
692 kgdb_break
[i
].state
= BP_REMOVED
;
699 int kgdb_isremovedbreak(unsigned long addr
)
703 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
704 if ((kgdb_break
[i
].state
== BP_REMOVED
) &&
705 (kgdb_break
[i
].bpt_addr
== addr
))
711 int remove_all_break(void)
717 /* Clear memory breakpoints. */
718 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
719 if (kgdb_break
[i
].state
!= BP_SET
)
721 addr
= kgdb_break
[i
].bpt_addr
;
722 error
= kgdb_arch_remove_breakpoint(addr
,
723 kgdb_break
[i
].saved_instr
);
726 kgdb_break
[i
].state
= BP_REMOVED
;
729 /* Clear hardware breakpoints. */
730 if (arch_kgdb_ops
.remove_all_hw_break
)
731 arch_kgdb_ops
.remove_all_hw_break();
737 * Remap normal tasks to their real PID, idle tasks to -1 ... -NR_CPUs:
739 static inline int shadow_pid(int realpid
)
744 return -1-raw_smp_processor_id();
747 static char gdbmsgbuf
[BUFMAX
+ 1];
749 static void kgdb_msg_write(const char *s
, int len
)
758 /* Fill and send buffers... */
760 bufptr
= gdbmsgbuf
+ 1;
762 /* Calculate how many this time */
763 if ((len
<< 1) > (BUFMAX
- 2))
764 wcount
= (BUFMAX
- 2) >> 1;
768 /* Pack in hex chars */
769 for (i
= 0; i
< wcount
; i
++)
770 bufptr
= pack_hex_byte(bufptr
, s
[i
]);
778 put_packet(gdbmsgbuf
);
783 * Return true if there is a valid kgdb I/O module. Also if no
784 * debugger is attached a message can be printed to the console about
785 * waiting for the debugger to attach.
787 * The print_wait argument is only to be true when called from inside
788 * the core kgdb_handle_exception, because it will wait for the
789 * debugger to attach.
791 static int kgdb_io_ready(int print_wait
)
797 if (atomic_read(&kgdb_setting_breakpoint
))
800 printk(KERN_CRIT
"KGDB: Waiting for remote debugger\n");
805 * All the functions that start with gdb_cmd are the various
806 * operations to implement the handlers for the gdbserial protocol
807 * where KGDB is communicating with an external debugger
810 /* Handle the '?' status packets */
811 static void gdb_cmd_status(struct kgdb_state
*ks
)
814 * We know that this packet is only sent
815 * during initial connect. So to be safe,
816 * we clear out our breakpoints now in case
817 * GDB is reconnecting.
821 remcom_out_buffer
[0] = 'S';
822 pack_hex_byte(&remcom_out_buffer
[1], ks
->signo
);
825 /* Handle the 'g' get registers request */
826 static void gdb_cmd_getregs(struct kgdb_state
*ks
)
828 struct task_struct
*thread
;
829 void *local_debuggerinfo
;
832 thread
= kgdb_usethread
;
834 thread
= kgdb_info
[ks
->cpu
].task
;
835 local_debuggerinfo
= kgdb_info
[ks
->cpu
].debuggerinfo
;
837 local_debuggerinfo
= NULL
;
838 for (i
= 0; i
< NR_CPUS
; i
++) {
840 * Try to find the task on some other
841 * or possibly this node if we do not
842 * find the matching task then we try
843 * to approximate the results.
845 if (thread
== kgdb_info
[i
].task
)
846 local_debuggerinfo
= kgdb_info
[i
].debuggerinfo
;
851 * All threads that don't have debuggerinfo should be
852 * in __schedule() sleeping, since all other CPUs
853 * are in kgdb_wait, and thus have debuggerinfo.
855 if (local_debuggerinfo
) {
856 pt_regs_to_gdb_regs(gdb_regs
, local_debuggerinfo
);
859 * Pull stuff saved during switch_to; nothing
860 * else is accessible (or even particularly
863 * This should be enough for a stack trace.
865 sleeping_thread_to_gdb_regs(gdb_regs
, thread
);
867 kgdb_mem2hex((char *)gdb_regs
, remcom_out_buffer
, NUMREGBYTES
);
870 /* Handle the 'G' set registers request */
871 static void gdb_cmd_setregs(struct kgdb_state
*ks
)
873 kgdb_hex2mem(&remcom_in_buffer
[1], (char *)gdb_regs
, NUMREGBYTES
);
875 if (kgdb_usethread
&& kgdb_usethread
!= current
) {
876 error_packet(remcom_out_buffer
, -EINVAL
);
878 gdb_regs_to_pt_regs(gdb_regs
, ks
->linux_regs
);
879 strcpy(remcom_out_buffer
, "OK");
883 /* Handle the 'm' memory read bytes */
884 static void gdb_cmd_memread(struct kgdb_state
*ks
)
886 char *ptr
= &remcom_in_buffer
[1];
887 unsigned long length
;
891 if (kgdb_hex2long(&ptr
, &addr
) > 0 && *ptr
++ == ',' &&
892 kgdb_hex2long(&ptr
, &length
) > 0) {
893 err
= kgdb_mem2hex((char *)addr
, remcom_out_buffer
, length
);
895 error_packet(remcom_out_buffer
, err
);
897 error_packet(remcom_out_buffer
, -EINVAL
);
901 /* Handle the 'M' memory write bytes */
902 static void gdb_cmd_memwrite(struct kgdb_state
*ks
)
904 int err
= write_mem_msg(0);
907 error_packet(remcom_out_buffer
, err
);
909 strcpy(remcom_out_buffer
, "OK");
912 /* Handle the 'X' memory binary write bytes */
913 static void gdb_cmd_binwrite(struct kgdb_state
*ks
)
915 int err
= write_mem_msg(1);
918 error_packet(remcom_out_buffer
, err
);
920 strcpy(remcom_out_buffer
, "OK");
923 /* Handle the 'D' or 'k', detach or kill packets */
924 static void gdb_cmd_detachkill(struct kgdb_state
*ks
)
928 /* The detach case */
929 if (remcom_in_buffer
[0] == 'D') {
930 error
= remove_all_break();
932 error_packet(remcom_out_buffer
, error
);
934 strcpy(remcom_out_buffer
, "OK");
937 put_packet(remcom_out_buffer
);
940 * Assume the kill case, with no exit code checking,
941 * trying to force detach the debugger:
948 /* Handle the 'R' reboot packets */
949 static int gdb_cmd_reboot(struct kgdb_state
*ks
)
951 /* For now, only honor R0 */
952 if (strcmp(remcom_in_buffer
, "R0") == 0) {
953 printk(KERN_CRIT
"Executing emergency reboot\n");
954 strcpy(remcom_out_buffer
, "OK");
955 put_packet(remcom_out_buffer
);
958 * Execution should not return from
959 * machine_emergency_restart()
961 machine_emergency_restart();
969 /* Handle the 'q' query packets */
970 static void gdb_cmd_query(struct kgdb_state
*ks
)
972 struct task_struct
*thread
;
973 unsigned char thref
[8];
977 switch (remcom_in_buffer
[1]) {
980 if (memcmp(remcom_in_buffer
+ 2, "ThreadInfo", 10)) {
981 error_packet(remcom_out_buffer
, -EINVAL
);
985 if (remcom_in_buffer
[1] == 'f')
988 remcom_out_buffer
[0] = 'm';
989 ptr
= remcom_out_buffer
+ 1;
991 for (i
= 0; i
< 17; ks
->threadid
++) {
992 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
994 int_to_threadref(thref
, ks
->threadid
);
995 pack_threadid(ptr
, thref
);
996 ptr
+= BUF_THREAD_ID_SIZE
;
1005 /* Current thread id */
1006 strcpy(remcom_out_buffer
, "QC");
1007 ks
->threadid
= shadow_pid(current
->pid
);
1008 int_to_threadref(thref
, ks
->threadid
);
1009 pack_threadid(remcom_out_buffer
+ 2, thref
);
1012 if (memcmp(remcom_in_buffer
+ 1, "ThreadExtraInfo,", 16)) {
1013 error_packet(remcom_out_buffer
, -EINVAL
);
1017 ptr
= remcom_in_buffer
+ 17;
1018 kgdb_hex2long(&ptr
, &ks
->threadid
);
1019 if (!getthread(ks
->linux_regs
, ks
->threadid
)) {
1020 error_packet(remcom_out_buffer
, -EINVAL
);
1023 if (ks
->threadid
> 0) {
1024 kgdb_mem2hex(getthread(ks
->linux_regs
,
1025 ks
->threadid
)->comm
,
1026 remcom_out_buffer
, 16);
1028 static char tmpstr
[23 + BUF_THREAD_ID_SIZE
];
1030 sprintf(tmpstr
, "Shadow task %d for pid 0",
1031 (int)(-ks
->threadid
-1));
1032 kgdb_mem2hex(tmpstr
, remcom_out_buffer
, strlen(tmpstr
));
1038 /* Handle the 'H' task query packets */
1039 static void gdb_cmd_task(struct kgdb_state
*ks
)
1041 struct task_struct
*thread
;
1044 switch (remcom_in_buffer
[1]) {
1046 ptr
= &remcom_in_buffer
[2];
1047 kgdb_hex2long(&ptr
, &ks
->threadid
);
1048 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
1049 if (!thread
&& ks
->threadid
> 0) {
1050 error_packet(remcom_out_buffer
, -EINVAL
);
1053 kgdb_usethread
= thread
;
1054 ks
->kgdb_usethreadid
= ks
->threadid
;
1055 strcpy(remcom_out_buffer
, "OK");
1058 ptr
= &remcom_in_buffer
[2];
1059 kgdb_hex2long(&ptr
, &ks
->threadid
);
1060 if (!ks
->threadid
) {
1061 kgdb_contthread
= NULL
;
1063 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
1064 if (!thread
&& ks
->threadid
> 0) {
1065 error_packet(remcom_out_buffer
, -EINVAL
);
1068 kgdb_contthread
= thread
;
1070 strcpy(remcom_out_buffer
, "OK");
1075 /* Handle the 'T' thread query packets */
1076 static void gdb_cmd_thread(struct kgdb_state
*ks
)
1078 char *ptr
= &remcom_in_buffer
[1];
1079 struct task_struct
*thread
;
1081 kgdb_hex2long(&ptr
, &ks
->threadid
);
1082 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
1084 strcpy(remcom_out_buffer
, "OK");
1086 error_packet(remcom_out_buffer
, -EINVAL
);
1089 /* Handle the 'z' or 'Z' breakpoint remove or set packets */
1090 static void gdb_cmd_break(struct kgdb_state
*ks
)
1093 * Since GDB-5.3, it's been drafted that '0' is a software
1094 * breakpoint, '1' is a hardware breakpoint, so let's do that.
1096 char *bpt_type
= &remcom_in_buffer
[1];
1097 char *ptr
= &remcom_in_buffer
[2];
1099 unsigned long length
;
1102 if (arch_kgdb_ops
.set_hw_breakpoint
&& *bpt_type
>= '1') {
1104 if (*bpt_type
> '4')
1107 if (*bpt_type
!= '0' && *bpt_type
!= '1')
1113 * Test if this is a hardware breakpoint, and
1116 if (*bpt_type
== '1' && !(arch_kgdb_ops
.flags
& KGDB_HW_BREAKPOINT
))
1120 if (*(ptr
++) != ',') {
1121 error_packet(remcom_out_buffer
, -EINVAL
);
1124 if (!kgdb_hex2long(&ptr
, &addr
)) {
1125 error_packet(remcom_out_buffer
, -EINVAL
);
1128 if (*(ptr
++) != ',' ||
1129 !kgdb_hex2long(&ptr
, &length
)) {
1130 error_packet(remcom_out_buffer
, -EINVAL
);
1134 if (remcom_in_buffer
[0] == 'Z' && *bpt_type
== '0')
1135 error
= kgdb_set_sw_break(addr
);
1136 else if (remcom_in_buffer
[0] == 'z' && *bpt_type
== '0')
1137 error
= kgdb_remove_sw_break(addr
);
1138 else if (remcom_in_buffer
[0] == 'Z')
1139 error
= arch_kgdb_ops
.set_hw_breakpoint(addr
,
1140 (int)length
, *bpt_type
);
1141 else if (remcom_in_buffer
[0] == 'z')
1142 error
= arch_kgdb_ops
.remove_hw_breakpoint(addr
,
1143 (int) length
, *bpt_type
);
1146 strcpy(remcom_out_buffer
, "OK");
1148 error_packet(remcom_out_buffer
, error
);
1151 /* Handle the 'C' signal / exception passing packets */
1152 static int gdb_cmd_exception_pass(struct kgdb_state
*ks
)
1154 /* C09 == pass exception
1155 * C15 == detach kgdb, pass exception
1157 if (remcom_in_buffer
[1] == '0' && remcom_in_buffer
[2] == '9') {
1159 ks
->pass_exception
= 1;
1160 remcom_in_buffer
[0] = 'c';
1162 } else if (remcom_in_buffer
[1] == '1' && remcom_in_buffer
[2] == '5') {
1164 ks
->pass_exception
= 1;
1165 remcom_in_buffer
[0] = 'D';
1171 error_packet(remcom_out_buffer
, -EINVAL
);
1175 /* Indicate fall through */
1180 * This function performs all gdbserial command procesing
1182 static int gdb_serial_stub(struct kgdb_state
*ks
)
1187 /* Clear the out buffer. */
1188 memset(remcom_out_buffer
, 0, sizeof(remcom_out_buffer
));
1190 if (kgdb_connected
) {
1191 unsigned char thref
[8];
1194 /* Reply to host that an exception has occurred */
1195 ptr
= remcom_out_buffer
;
1197 ptr
= pack_hex_byte(ptr
, ks
->signo
);
1198 ptr
+= strlen(strcpy(ptr
, "thread:"));
1199 int_to_threadref(thref
, shadow_pid(current
->pid
));
1200 ptr
= pack_threadid(ptr
, thref
);
1202 put_packet(remcom_out_buffer
);
1205 kgdb_usethread
= kgdb_info
[ks
->cpu
].task
;
1206 ks
->kgdb_usethreadid
= shadow_pid(kgdb_info
[ks
->cpu
].task
->pid
);
1207 ks
->pass_exception
= 0;
1212 /* Clear the out buffer. */
1213 memset(remcom_out_buffer
, 0, sizeof(remcom_out_buffer
));
1215 get_packet(remcom_in_buffer
);
1217 switch (remcom_in_buffer
[0]) {
1218 case '?': /* gdbserial status */
1221 case 'g': /* return the value of the CPU registers */
1222 gdb_cmd_getregs(ks
);
1224 case 'G': /* set the value of the CPU registers - return OK */
1225 gdb_cmd_setregs(ks
);
1227 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1228 gdb_cmd_memread(ks
);
1230 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1231 gdb_cmd_memwrite(ks
);
1233 case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1234 gdb_cmd_binwrite(ks
);
1236 /* kill or detach. KGDB should treat this like a
1239 case 'D': /* Debugger detach */
1240 case 'k': /* Debugger detach via kill */
1241 gdb_cmd_detachkill(ks
);
1242 goto default_handle
;
1243 case 'R': /* Reboot */
1244 if (gdb_cmd_reboot(ks
))
1245 goto default_handle
;
1247 case 'q': /* query command */
1250 case 'H': /* task related */
1253 case 'T': /* Query thread status */
1256 case 'z': /* Break point remove */
1257 case 'Z': /* Break point set */
1260 case 'C': /* Exception passing */
1261 tmp
= gdb_cmd_exception_pass(ks
);
1263 goto default_handle
;
1266 /* Fall through on tmp < 0 */
1267 case 'c': /* Continue packet */
1268 case 's': /* Single step packet */
1269 if (kgdb_contthread
&& kgdb_contthread
!= current
) {
1270 /* Can't switch threads in kgdb */
1271 error_packet(remcom_out_buffer
, -EINVAL
);
1274 kgdb_activate_sw_breakpoints();
1275 /* Fall through to default processing */
1278 error
= kgdb_arch_handle_exception(ks
->ex_vector
,
1285 * Leave cmd processing on error, detach,
1286 * kill, continue, or single step.
1288 if (error
>= 0 || remcom_in_buffer
[0] == 'D' ||
1289 remcom_in_buffer
[0] == 'k') {
1296 /* reply to the request */
1297 put_packet(remcom_out_buffer
);
1301 if (ks
->pass_exception
)
1306 static int kgdb_reenter_check(struct kgdb_state
*ks
)
1310 if (atomic_read(&kgdb_active
) != raw_smp_processor_id())
1313 /* Panic on recursive debugger calls: */
1315 addr
= kgdb_arch_pc(ks
->ex_vector
, ks
->linux_regs
);
1316 kgdb_deactivate_sw_breakpoints();
1319 * If the break point removed ok at the place exception
1320 * occurred, try to recover and print a warning to the end
1321 * user because the user planted a breakpoint in a place that
1322 * KGDB needs in order to function.
1324 if (kgdb_remove_sw_break(addr
) == 0) {
1325 exception_level
= 0;
1326 kgdb_skipexception(ks
->ex_vector
, ks
->linux_regs
);
1327 kgdb_activate_sw_breakpoints();
1328 printk(KERN_CRIT
"KGDB: re-enter error: breakpoint removed\n");
1334 kgdb_skipexception(ks
->ex_vector
, ks
->linux_regs
);
1336 if (exception_level
> 1) {
1338 panic("Recursive entry to debugger");
1341 printk(KERN_CRIT
"KGDB: re-enter exception: ALL breakpoints killed\n");
1343 panic("Recursive entry to debugger");
1349 * kgdb_handle_exception() - main entry point from a kernel exception
1351 * Locking hierarchy:
1352 * interface locks, if any (begin_session)
1353 * kgdb lock (kgdb_active)
1356 kgdb_handle_exception(int evector
, int signo
, int ecode
, struct pt_regs
*regs
)
1358 struct kgdb_state kgdb_var
;
1359 struct kgdb_state
*ks
= &kgdb_var
;
1360 unsigned long flags
;
1364 ks
->cpu
= raw_smp_processor_id();
1365 ks
->ex_vector
= evector
;
1367 ks
->ex_vector
= evector
;
1368 ks
->err_code
= ecode
;
1369 ks
->kgdb_usethreadid
= 0;
1370 ks
->linux_regs
= regs
;
1372 if (kgdb_reenter_check(ks
))
1373 return 0; /* Ouch, double exception ! */
1377 * Interrupts will be restored by the 'trap return' code, except when
1380 local_irq_save(flags
);
1382 cpu
= raw_smp_processor_id();
1385 * Acquire the kgdb_active lock:
1387 while (atomic_cmpxchg(&kgdb_active
, -1, cpu
) != -1)
1391 * Do not start the debugger connection on this CPU if the last
1392 * instance of the exception handler wanted to come into the
1393 * debugger on a different CPU via a single step
1395 if (atomic_read(&kgdb_cpu_doing_single_step
) != -1 &&
1396 atomic_read(&kgdb_cpu_doing_single_step
) != cpu
) {
1398 atomic_set(&kgdb_active
, -1);
1399 local_irq_restore(flags
);
1404 if (!kgdb_io_ready(1)) {
1406 goto kgdb_restore
; /* No I/O connection, so resume the system */
1410 * Don't enter if we have hit a removed breakpoint.
1412 if (kgdb_skipexception(ks
->ex_vector
, ks
->linux_regs
))
1415 /* Call the I/O driver's pre_exception routine */
1416 if (kgdb_io_ops
->pre_exception
)
1417 kgdb_io_ops
->pre_exception();
1419 kgdb_info
[ks
->cpu
].debuggerinfo
= ks
->linux_regs
;
1420 kgdb_info
[ks
->cpu
].task
= current
;
1422 kgdb_disable_hw_debug(ks
->linux_regs
);
1425 * Get the passive CPU lock which will hold all the non-primary
1426 * CPU in a spin state while the debugger is active
1428 if (!kgdb_single_step
|| !kgdb_contthread
) {
1429 for (i
= 0; i
< NR_CPUS
; i
++)
1430 atomic_set(&passive_cpu_wait
[i
], 1);
1434 /* Signal the other CPUs to enter kgdb_wait() */
1435 if ((!kgdb_single_step
|| !kgdb_contthread
) && kgdb_do_roundup
)
1436 kgdb_roundup_cpus(flags
);
1440 * spin_lock code is good enough as a barrier so we don't
1443 atomic_set(&cpu_in_kgdb
[ks
->cpu
], 1);
1446 * Wait for the other CPUs to be notified and be waiting for us:
1448 for_each_online_cpu(i
) {
1449 while (!atomic_read(&cpu_in_kgdb
[i
]))
1454 * At this point the primary processor is completely
1455 * in the debugger and all secondary CPUs are quiescent
1457 kgdb_post_primary_code(ks
->linux_regs
, ks
->ex_vector
, ks
->err_code
);
1458 kgdb_deactivate_sw_breakpoints();
1459 kgdb_single_step
= 0;
1460 kgdb_contthread
= NULL
;
1461 exception_level
= 0;
1463 /* Talk to debugger with gdbserial protocol */
1464 error
= gdb_serial_stub(ks
);
1466 /* Call the I/O driver's post_exception routine */
1467 if (kgdb_io_ops
->post_exception
)
1468 kgdb_io_ops
->post_exception();
1470 kgdb_info
[ks
->cpu
].debuggerinfo
= NULL
;
1471 kgdb_info
[ks
->cpu
].task
= NULL
;
1472 atomic_set(&cpu_in_kgdb
[ks
->cpu
], 0);
1474 if (!kgdb_single_step
|| !kgdb_contthread
) {
1475 for (i
= NR_CPUS
-1; i
>= 0; i
--)
1476 atomic_set(&passive_cpu_wait
[i
], 0);
1478 * Wait till all the CPUs have quit
1479 * from the debugger.
1481 for_each_online_cpu(i
) {
1482 while (atomic_read(&cpu_in_kgdb
[i
]))
1488 /* Free kgdb_active */
1489 atomic_set(&kgdb_active
, -1);
1490 local_irq_restore(flags
);
1495 int kgdb_nmicallback(int cpu
, void *regs
)
1498 if (!atomic_read(&cpu_in_kgdb
[cpu
]) &&
1499 atomic_read(&kgdb_active
) != cpu
) {
1500 kgdb_wait((struct pt_regs
*)regs
);
1507 void kgdb_console_write(struct console
*co
, const char *s
, unsigned count
)
1509 unsigned long flags
;
1511 /* If we're debugging, or KGDB has not connected, don't try
1513 if (!kgdb_connected
|| atomic_read(&kgdb_active
) != -1)
1516 local_irq_save(flags
);
1517 kgdb_msg_write(s
, count
);
1518 local_irq_restore(flags
);
1521 static struct console kgdbcons
= {
1523 .write
= kgdb_console_write
,
1524 .flags
= CON_PRINTBUFFER
| CON_ENABLED
,
1528 #ifdef CONFIG_MAGIC_SYSRQ
1529 static void sysrq_handle_gdb(int key
, struct tty_struct
*tty
)
1532 printk(KERN_CRIT
"ERROR: No KGDB I/O module available\n");
1535 if (!kgdb_connected
)
1536 printk(KERN_CRIT
"Entering KGDB\n");
1541 static struct sysrq_key_op sysrq_gdb_op
= {
1542 .handler
= sysrq_handle_gdb
,
1544 .action_msg
= "GDB",
1548 static void kgdb_register_callbacks(void)
1550 if (!kgdb_io_module_registered
) {
1551 kgdb_io_module_registered
= 1;
1553 #ifdef CONFIG_MAGIC_SYSRQ
1554 register_sysrq_key('g', &sysrq_gdb_op
);
1556 if (kgdb_use_con
&& !kgdb_con_registered
) {
1557 register_console(&kgdbcons
);
1558 kgdb_con_registered
= 1;
1563 static void kgdb_unregister_callbacks(void)
1566 * When this routine is called KGDB should unregister from the
1567 * panic handler and clean up, making sure it is not handling any
1568 * break exceptions at the time.
1570 if (kgdb_io_module_registered
) {
1571 kgdb_io_module_registered
= 0;
1573 #ifdef CONFIG_MAGIC_SYSRQ
1574 unregister_sysrq_key('g', &sysrq_gdb_op
);
1576 if (kgdb_con_registered
) {
1577 unregister_console(&kgdbcons
);
1578 kgdb_con_registered
= 0;
1583 static void kgdb_initial_breakpoint(void)
1585 kgdb_break_asap
= 0;
1587 printk(KERN_CRIT
"kgdb: Waiting for connection from remote gdb...\n");
1592 * kkgdb_register_io_module - register KGDB IO module
1593 * @new_kgdb_io_ops: the io ops vector
1595 * Register it with the KGDB core.
1597 int kgdb_register_io_module(struct kgdb_io
*new_kgdb_io_ops
)
1601 spin_lock(&kgdb_registration_lock
);
1604 spin_unlock(&kgdb_registration_lock
);
1606 printk(KERN_ERR
"kgdb: Another I/O driver is already "
1607 "registered with KGDB.\n");
1611 if (new_kgdb_io_ops
->init
) {
1612 err
= new_kgdb_io_ops
->init();
1614 spin_unlock(&kgdb_registration_lock
);
1619 kgdb_io_ops
= new_kgdb_io_ops
;
1621 spin_unlock(&kgdb_registration_lock
);
1623 printk(KERN_INFO
"kgdb: Registered I/O driver %s.\n",
1624 new_kgdb_io_ops
->name
);
1627 kgdb_register_callbacks();
1629 if (kgdb_break_asap
)
1630 kgdb_initial_breakpoint();
1634 EXPORT_SYMBOL_GPL(kgdb_register_io_module
);
1637 * kkgdb_unregister_io_module - unregister KGDB IO module
1638 * @old_kgdb_io_ops: the io ops vector
1640 * Unregister it with the KGDB core.
1642 void kgdb_unregister_io_module(struct kgdb_io
*old_kgdb_io_ops
)
1644 BUG_ON(kgdb_connected
);
1647 * KGDB is no longer able to communicate out, so
1648 * unregister our callbacks and reset state.
1650 kgdb_unregister_callbacks();
1652 spin_lock(&kgdb_registration_lock
);
1654 WARN_ON_ONCE(kgdb_io_ops
!= old_kgdb_io_ops
);
1657 spin_unlock(&kgdb_registration_lock
);
1660 "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
1661 old_kgdb_io_ops
->name
);
1663 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module
);
1666 * kgdb_breakpoint - generate breakpoint exception
1668 * This function will generate a breakpoint exception. It is used at the
1669 * beginning of a program to sync up with a debugger and can be used
1670 * otherwise as a quick means to stop program execution and "break" into
1673 void kgdb_breakpoint(void)
1675 atomic_set(&kgdb_setting_breakpoint
, 1);
1676 wmb(); /* Sync point before breakpoint */
1677 arch_kgdb_breakpoint();
1678 wmb(); /* Sync point after breakpoint */
1679 atomic_set(&kgdb_setting_breakpoint
, 0);
1681 EXPORT_SYMBOL_GPL(kgdb_breakpoint
);
1683 static int __init
opt_kgdb_wait(char *str
)
1685 kgdb_break_asap
= 1;
1687 if (kgdb_io_module_registered
)
1688 kgdb_initial_breakpoint();
1693 early_param("kgdbwait", opt_kgdb_wait
);