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/clocksource.h>
32 #include <linux/interrupt.h>
33 #include <linux/spinlock.h>
34 #include <linux/console.h>
35 #include <linux/threads.h>
36 #include <linux/uaccess.h>
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/ptrace.h>
40 #include <linux/reboot.h>
41 #include <linux/string.h>
42 #include <linux/delay.h>
43 #include <linux/sched.h>
44 #include <linux/sysrq.h>
45 #include <linux/init.h>
46 #include <linux/kgdb.h>
47 #include <linux/pid.h>
48 #include <linux/smp.h>
51 #include <asm/cacheflush.h>
52 #include <asm/byteorder.h>
53 #include <asm/atomic.h>
54 #include <asm/system.h>
55 #include <asm/unaligned.h>
57 static int kgdb_break_asap
;
59 #define KGDB_MAX_THREAD_QUERY 17
66 unsigned long thr_query
;
67 unsigned long threadid
;
68 long kgdb_usethreadid
;
69 struct pt_regs
*linux_regs
;
72 static struct debuggerinfo_struct
{
74 struct task_struct
*task
;
78 * kgdb_connected - Is a host GDB connected to us?
81 EXPORT_SYMBOL_GPL(kgdb_connected
);
83 /* All the KGDB handlers are installed */
84 static int kgdb_io_module_registered
;
86 /* Guard for recursive entry */
87 static int exception_level
;
89 static struct kgdb_io
*kgdb_io_ops
;
90 static DEFINE_SPINLOCK(kgdb_registration_lock
);
92 /* kgdb console driver is loaded */
93 static int kgdb_con_registered
;
94 /* determine if kgdb console output should be used */
95 static int kgdb_use_con
;
97 static int __init
opt_kgdb_con(char *str
)
103 early_param("kgdbcon", opt_kgdb_con
);
105 module_param(kgdb_use_con
, int, 0644);
108 * Holds information about breakpoints in a kernel. These breakpoints are
109 * added and removed by gdb.
111 static struct kgdb_bkpt kgdb_break
[KGDB_MAX_BREAKPOINTS
] = {
112 [0 ... KGDB_MAX_BREAKPOINTS
-1] = { .state
= BP_UNDEFINED
}
116 * The CPU# of the active CPU, or -1 if none:
118 atomic_t kgdb_active
= ATOMIC_INIT(-1);
121 * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
122 * bootup code (which might not have percpu set up yet):
124 static atomic_t passive_cpu_wait
[NR_CPUS
];
125 static atomic_t cpu_in_kgdb
[NR_CPUS
];
126 atomic_t kgdb_setting_breakpoint
;
128 struct task_struct
*kgdb_usethread
;
129 struct task_struct
*kgdb_contthread
;
131 int kgdb_single_step
;
132 pid_t kgdb_sstep_pid
;
134 /* Our I/O buffers. */
135 static char remcom_in_buffer
[BUFMAX
];
136 static char remcom_out_buffer
[BUFMAX
];
138 /* Storage for the registers, in GDB format. */
139 static unsigned long gdb_regs
[(NUMREGBYTES
+
140 sizeof(unsigned long) - 1) /
141 sizeof(unsigned long)];
143 /* to keep track of the CPU which is doing the single stepping*/
144 atomic_t kgdb_cpu_doing_single_step
= ATOMIC_INIT(-1);
147 * If you are debugging a problem where roundup (the collection of
148 * all other CPUs) is a problem [this should be extremely rare],
149 * then use the nokgdbroundup option to avoid roundup. In that case
150 * the other CPUs might interfere with your debugging context, so
151 * use this with care:
153 static int kgdb_do_roundup
= 1;
155 static int __init
opt_nokgdbroundup(char *str
)
162 early_param("nokgdbroundup", opt_nokgdbroundup
);
165 * Finally, some KGDB code :-)
169 * Weak aliases for breakpoint management,
170 * can be overriden by architectures when needed:
172 int __weak
kgdb_arch_set_breakpoint(unsigned long addr
, char *saved_instr
)
176 err
= probe_kernel_read(saved_instr
, (char *)addr
, BREAK_INSTR_SIZE
);
180 return probe_kernel_write((char *)addr
, arch_kgdb_ops
.gdb_bpt_instr
,
184 int __weak
kgdb_arch_remove_breakpoint(unsigned long addr
, char *bundle
)
186 return probe_kernel_write((char *)addr
,
187 (char *)bundle
, BREAK_INSTR_SIZE
);
190 int __weak
kgdb_validate_break_address(unsigned long addr
)
192 char tmp_variable
[BREAK_INSTR_SIZE
];
194 /* Validate setting the breakpoint and then removing it. In the
195 * remove fails, the kernel needs to emit a bad message because we
196 * are deep trouble not being able to put things back the way we
199 err
= kgdb_arch_set_breakpoint(addr
, tmp_variable
);
202 err
= kgdb_arch_remove_breakpoint(addr
, tmp_variable
);
204 printk(KERN_ERR
"KGDB: Critical breakpoint error, kernel "
205 "memory destroyed at: %lx", addr
);
209 unsigned long __weak
kgdb_arch_pc(int exception
, struct pt_regs
*regs
)
211 return instruction_pointer(regs
);
214 int __weak
kgdb_arch_init(void)
219 int __weak
kgdb_skipexception(int exception
, struct pt_regs
*regs
)
225 kgdb_post_primary_code(struct pt_regs
*regs
, int e_vector
, int err_code
)
231 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
232 * @regs: Current &struct pt_regs.
234 * This function will be called if the particular architecture must
235 * disable hardware debugging while it is processing gdb packets or
236 * handling exception.
238 void __weak
kgdb_disable_hw_debug(struct pt_regs
*regs
)
243 * GDB remote protocol parser:
246 static int hex(char ch
)
248 if ((ch
>= 'a') && (ch
<= 'f'))
249 return ch
- 'a' + 10;
250 if ((ch
>= '0') && (ch
<= '9'))
252 if ((ch
>= 'A') && (ch
<= 'F'))
253 return ch
- 'A' + 10;
257 /* scan for the sequence $<data>#<checksum> */
258 static void get_packet(char *buffer
)
260 unsigned char checksum
;
261 unsigned char xmitcsum
;
267 * Spin and wait around for the start character, ignore all
270 while ((ch
= (kgdb_io_ops
->read_char())) != '$')
280 * now, read until a # or end of buffer is found:
282 while (count
< (BUFMAX
- 1)) {
283 ch
= kgdb_io_ops
->read_char();
286 checksum
= checksum
+ ch
;
293 xmitcsum
= hex(kgdb_io_ops
->read_char()) << 4;
294 xmitcsum
+= hex(kgdb_io_ops
->read_char());
296 if (checksum
!= xmitcsum
)
297 /* failed checksum */
298 kgdb_io_ops
->write_char('-');
300 /* successful transfer */
301 kgdb_io_ops
->write_char('+');
302 if (kgdb_io_ops
->flush
)
303 kgdb_io_ops
->flush();
305 } while (checksum
!= xmitcsum
);
309 * Send the packet in buffer.
310 * Check for gdb connection if asked for.
312 static void put_packet(char *buffer
)
314 unsigned char checksum
;
319 * $<packet info>#<checksum>.
322 kgdb_io_ops
->write_char('$');
326 while ((ch
= buffer
[count
])) {
327 kgdb_io_ops
->write_char(ch
);
332 kgdb_io_ops
->write_char('#');
333 kgdb_io_ops
->write_char(hex_asc_hi(checksum
));
334 kgdb_io_ops
->write_char(hex_asc_lo(checksum
));
335 if (kgdb_io_ops
->flush
)
336 kgdb_io_ops
->flush();
338 /* Now see what we get in reply. */
339 ch
= kgdb_io_ops
->read_char();
342 ch
= kgdb_io_ops
->read_char();
344 /* If we get an ACK, we are done. */
349 * If we get the start of another packet, this means
350 * that GDB is attempting to reconnect. We will NAK
351 * the packet being sent, and stop trying to send this
355 kgdb_io_ops
->write_char('-');
356 if (kgdb_io_ops
->flush
)
357 kgdb_io_ops
->flush();
364 * Convert the memory pointed to by mem into hex, placing result in buf.
365 * Return a pointer to the last char put in buf (null). May return an error.
367 int kgdb_mem2hex(char *mem
, char *buf
, int count
)
373 * We use the upper half of buf as an intermediate buffer for the
374 * raw memory copy. Hex conversion will work against this one.
378 err
= probe_kernel_read(tmp
, mem
, count
);
381 buf
= pack_hex_byte(buf
, *tmp
);
393 * Copy the binary array pointed to by buf into mem. Fix $, #, and
394 * 0x7d escaped with 0x7d. Return a pointer to the character after
395 * the last byte written.
397 static int kgdb_ebin2mem(char *buf
, char *mem
, int count
)
402 while (count
-- > 0) {
407 err
= probe_kernel_write(mem
, &c
, 1);
418 * Convert the hex array pointed to by buf into binary to be placed in mem.
419 * Return a pointer to the character AFTER the last byte written.
420 * May return an error.
422 int kgdb_hex2mem(char *buf
, char *mem
, int count
)
428 * We use the upper half of buf as an intermediate buffer for the
429 * raw memory that is converted from hex.
431 tmp_raw
= buf
+ count
* 2;
433 tmp_hex
= tmp_raw
- 1;
434 while (tmp_hex
>= buf
) {
436 *tmp_raw
= hex(*tmp_hex
--);
437 *tmp_raw
|= hex(*tmp_hex
--) << 4;
440 return probe_kernel_write(mem
, tmp_raw
, count
);
444 * While we find nice hex chars, build a long_val.
445 * Return number of chars processed.
447 int kgdb_hex2long(char **ptr
, unsigned long *long_val
)
460 hex_val
= hex(**ptr
);
464 *long_val
= (*long_val
<< 4) | hex_val
;
470 *long_val
= -*long_val
;
475 /* Write memory due to an 'M' or 'X' packet. */
476 static int write_mem_msg(int binary
)
478 char *ptr
= &remcom_in_buffer
[1];
480 unsigned long length
;
483 if (kgdb_hex2long(&ptr
, &addr
) > 0 && *(ptr
++) == ',' &&
484 kgdb_hex2long(&ptr
, &length
) > 0 && *(ptr
++) == ':') {
486 err
= kgdb_ebin2mem(ptr
, (char *)addr
, length
);
488 err
= kgdb_hex2mem(ptr
, (char *)addr
, length
);
491 if (CACHE_FLUSH_IS_SAFE
)
492 flush_icache_range(addr
, addr
+ length
);
499 static void error_packet(char *pkt
, int error
)
503 pkt
[1] = hex_asc
[(error
/ 10)];
504 pkt
[2] = hex_asc
[(error
% 10)];
509 * Thread ID accessors. We represent a flat TID space to GDB, where
510 * the per CPU idle threads (which under Linux all have PID 0) are
511 * remapped to negative TIDs.
514 #define BUF_THREAD_ID_SIZE 16
516 static char *pack_threadid(char *pkt
, unsigned char *id
)
520 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
522 pkt
= pack_hex_byte(pkt
, *id
++);
527 static void int_to_threadref(unsigned char *id
, int value
)
532 scan
= (unsigned char *)id
;
535 put_unaligned_be32(value
, scan
);
538 static struct task_struct
*getthread(struct pt_regs
*regs
, int tid
)
541 * Non-positive TIDs are remapped to the cpu shadow information
543 if (tid
== 0 || tid
== -1)
544 tid
= -atomic_read(&kgdb_active
) - 2;
545 if (tid
< -1 && tid
> -NR_CPUS
- 2) {
546 if (kgdb_info
[-tid
- 2].task
)
547 return kgdb_info
[-tid
- 2].task
;
549 return idle_task(-tid
- 2);
552 printk(KERN_ERR
"KGDB: Internal thread select error\n");
558 * find_task_by_pid_ns() does not take the tasklist lock anymore
559 * but is nicely RCU locked - hence is a pretty resilient
562 return find_task_by_pid_ns(tid
, &init_pid_ns
);
566 * CPU debug state control:
570 static void kgdb_wait(struct pt_regs
*regs
)
575 local_irq_save(flags
);
576 cpu
= raw_smp_processor_id();
577 kgdb_info
[cpu
].debuggerinfo
= regs
;
578 kgdb_info
[cpu
].task
= current
;
580 * Make sure the above info reaches the primary CPU before
581 * our cpu_in_kgdb[] flag setting does:
584 atomic_set(&cpu_in_kgdb
[cpu
], 1);
586 /* Disable any cpu specific hw breakpoints */
587 kgdb_disable_hw_debug(regs
);
589 /* Wait till primary CPU is done with debugging */
590 while (atomic_read(&passive_cpu_wait
[cpu
]))
593 kgdb_info
[cpu
].debuggerinfo
= NULL
;
594 kgdb_info
[cpu
].task
= NULL
;
596 /* fix up hardware debug registers on local cpu */
597 if (arch_kgdb_ops
.correct_hw_break
)
598 arch_kgdb_ops
.correct_hw_break();
600 /* Signal the primary CPU that we are done: */
601 atomic_set(&cpu_in_kgdb
[cpu
], 0);
602 touch_softlockup_watchdog_sync();
603 clocksource_touch_watchdog();
604 local_irq_restore(flags
);
609 * Some architectures need cache flushes when we set/clear a
612 static void kgdb_flush_swbreak_addr(unsigned long addr
)
614 if (!CACHE_FLUSH_IS_SAFE
)
617 if (current
->mm
&& current
->mm
->mmap_cache
) {
618 flush_cache_range(current
->mm
->mmap_cache
,
619 addr
, addr
+ BREAK_INSTR_SIZE
);
621 /* Force flush instruction cache if it was outside the mm */
622 flush_icache_range(addr
, addr
+ BREAK_INSTR_SIZE
);
626 * SW breakpoint management:
628 static int kgdb_activate_sw_breakpoints(void)
635 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
636 if (kgdb_break
[i
].state
!= BP_SET
)
639 addr
= kgdb_break
[i
].bpt_addr
;
640 error
= kgdb_arch_set_breakpoint(addr
,
641 kgdb_break
[i
].saved_instr
);
644 printk(KERN_INFO
"KGDB: BP install failed: %lx", addr
);
648 kgdb_flush_swbreak_addr(addr
);
649 kgdb_break
[i
].state
= BP_ACTIVE
;
654 static int kgdb_set_sw_break(unsigned long addr
)
656 int err
= kgdb_validate_break_address(addr
);
663 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
664 if ((kgdb_break
[i
].state
== BP_SET
) &&
665 (kgdb_break
[i
].bpt_addr
== addr
))
668 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
669 if (kgdb_break
[i
].state
== BP_REMOVED
&&
670 kgdb_break
[i
].bpt_addr
== addr
) {
677 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
678 if (kgdb_break
[i
].state
== BP_UNDEFINED
) {
688 kgdb_break
[breakno
].state
= BP_SET
;
689 kgdb_break
[breakno
].type
= BP_BREAKPOINT
;
690 kgdb_break
[breakno
].bpt_addr
= addr
;
695 static int kgdb_deactivate_sw_breakpoints(void)
702 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
703 if (kgdb_break
[i
].state
!= BP_ACTIVE
)
705 addr
= kgdb_break
[i
].bpt_addr
;
706 error
= kgdb_arch_remove_breakpoint(addr
,
707 kgdb_break
[i
].saved_instr
);
709 printk(KERN_INFO
"KGDB: BP remove failed: %lx\n", addr
);
713 kgdb_flush_swbreak_addr(addr
);
714 kgdb_break
[i
].state
= BP_SET
;
719 static int kgdb_remove_sw_break(unsigned long addr
)
723 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
724 if ((kgdb_break
[i
].state
== BP_SET
) &&
725 (kgdb_break
[i
].bpt_addr
== addr
)) {
726 kgdb_break
[i
].state
= BP_REMOVED
;
733 int kgdb_isremovedbreak(unsigned long addr
)
737 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
738 if ((kgdb_break
[i
].state
== BP_REMOVED
) &&
739 (kgdb_break
[i
].bpt_addr
== addr
))
745 static int remove_all_break(void)
751 /* Clear memory breakpoints. */
752 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
753 if (kgdb_break
[i
].state
!= BP_ACTIVE
)
755 addr
= kgdb_break
[i
].bpt_addr
;
756 error
= kgdb_arch_remove_breakpoint(addr
,
757 kgdb_break
[i
].saved_instr
);
759 printk(KERN_ERR
"KGDB: breakpoint remove failed: %lx\n",
762 kgdb_break
[i
].state
= BP_UNDEFINED
;
765 /* Clear hardware breakpoints. */
766 if (arch_kgdb_ops
.remove_all_hw_break
)
767 arch_kgdb_ops
.remove_all_hw_break();
773 * Remap normal tasks to their real PID,
774 * CPU shadow threads are mapped to -CPU - 2
776 static inline int shadow_pid(int realpid
)
781 return -raw_smp_processor_id() - 2;
784 static char gdbmsgbuf
[BUFMAX
+ 1];
786 static void kgdb_msg_write(const char *s
, int len
)
795 /* Fill and send buffers... */
797 bufptr
= gdbmsgbuf
+ 1;
799 /* Calculate how many this time */
800 if ((len
<< 1) > (BUFMAX
- 2))
801 wcount
= (BUFMAX
- 2) >> 1;
805 /* Pack in hex chars */
806 for (i
= 0; i
< wcount
; i
++)
807 bufptr
= pack_hex_byte(bufptr
, s
[i
]);
815 put_packet(gdbmsgbuf
);
820 * Return true if there is a valid kgdb I/O module. Also if no
821 * debugger is attached a message can be printed to the console about
822 * waiting for the debugger to attach.
824 * The print_wait argument is only to be true when called from inside
825 * the core kgdb_handle_exception, because it will wait for the
826 * debugger to attach.
828 static int kgdb_io_ready(int print_wait
)
834 if (atomic_read(&kgdb_setting_breakpoint
))
837 printk(KERN_CRIT
"KGDB: Waiting for remote debugger\n");
842 * All the functions that start with gdb_cmd are the various
843 * operations to implement the handlers for the gdbserial protocol
844 * where KGDB is communicating with an external debugger
847 /* Handle the '?' status packets */
848 static void gdb_cmd_status(struct kgdb_state
*ks
)
851 * We know that this packet is only sent
852 * during initial connect. So to be safe,
853 * we clear out our breakpoints now in case
854 * GDB is reconnecting.
858 remcom_out_buffer
[0] = 'S';
859 pack_hex_byte(&remcom_out_buffer
[1], ks
->signo
);
862 /* Handle the 'g' get registers request */
863 static void gdb_cmd_getregs(struct kgdb_state
*ks
)
865 struct task_struct
*thread
;
866 void *local_debuggerinfo
;
869 thread
= kgdb_usethread
;
871 thread
= kgdb_info
[ks
->cpu
].task
;
872 local_debuggerinfo
= kgdb_info
[ks
->cpu
].debuggerinfo
;
874 local_debuggerinfo
= NULL
;
875 for_each_online_cpu(i
) {
877 * Try to find the task on some other
878 * or possibly this node if we do not
879 * find the matching task then we try
880 * to approximate the results.
882 if (thread
== kgdb_info
[i
].task
)
883 local_debuggerinfo
= kgdb_info
[i
].debuggerinfo
;
888 * All threads that don't have debuggerinfo should be
889 * in schedule() sleeping, since all other CPUs
890 * are in kgdb_wait, and thus have debuggerinfo.
892 if (local_debuggerinfo
) {
893 pt_regs_to_gdb_regs(gdb_regs
, local_debuggerinfo
);
896 * Pull stuff saved during switch_to; nothing
897 * else is accessible (or even particularly
900 * This should be enough for a stack trace.
902 sleeping_thread_to_gdb_regs(gdb_regs
, thread
);
904 kgdb_mem2hex((char *)gdb_regs
, remcom_out_buffer
, NUMREGBYTES
);
907 /* Handle the 'G' set registers request */
908 static void gdb_cmd_setregs(struct kgdb_state
*ks
)
910 kgdb_hex2mem(&remcom_in_buffer
[1], (char *)gdb_regs
, NUMREGBYTES
);
912 if (kgdb_usethread
&& kgdb_usethread
!= current
) {
913 error_packet(remcom_out_buffer
, -EINVAL
);
915 gdb_regs_to_pt_regs(gdb_regs
, ks
->linux_regs
);
916 strcpy(remcom_out_buffer
, "OK");
920 /* Handle the 'm' memory read bytes */
921 static void gdb_cmd_memread(struct kgdb_state
*ks
)
923 char *ptr
= &remcom_in_buffer
[1];
924 unsigned long length
;
928 if (kgdb_hex2long(&ptr
, &addr
) > 0 && *ptr
++ == ',' &&
929 kgdb_hex2long(&ptr
, &length
) > 0) {
930 err
= kgdb_mem2hex((char *)addr
, remcom_out_buffer
, length
);
932 error_packet(remcom_out_buffer
, err
);
934 error_packet(remcom_out_buffer
, -EINVAL
);
938 /* Handle the 'M' memory write bytes */
939 static void gdb_cmd_memwrite(struct kgdb_state
*ks
)
941 int err
= write_mem_msg(0);
944 error_packet(remcom_out_buffer
, err
);
946 strcpy(remcom_out_buffer
, "OK");
949 /* Handle the 'X' memory binary write bytes */
950 static void gdb_cmd_binwrite(struct kgdb_state
*ks
)
952 int err
= write_mem_msg(1);
955 error_packet(remcom_out_buffer
, err
);
957 strcpy(remcom_out_buffer
, "OK");
960 /* Handle the 'D' or 'k', detach or kill packets */
961 static void gdb_cmd_detachkill(struct kgdb_state
*ks
)
965 /* The detach case */
966 if (remcom_in_buffer
[0] == 'D') {
967 error
= remove_all_break();
969 error_packet(remcom_out_buffer
, error
);
971 strcpy(remcom_out_buffer
, "OK");
974 put_packet(remcom_out_buffer
);
977 * Assume the kill case, with no exit code checking,
978 * trying to force detach the debugger:
985 /* Handle the 'R' reboot packets */
986 static int gdb_cmd_reboot(struct kgdb_state
*ks
)
988 /* For now, only honor R0 */
989 if (strcmp(remcom_in_buffer
, "R0") == 0) {
990 printk(KERN_CRIT
"Executing emergency reboot\n");
991 strcpy(remcom_out_buffer
, "OK");
992 put_packet(remcom_out_buffer
);
995 * Execution should not return from
996 * machine_emergency_restart()
998 machine_emergency_restart();
1006 /* Handle the 'q' query packets */
1007 static void gdb_cmd_query(struct kgdb_state
*ks
)
1009 struct task_struct
*g
;
1010 struct task_struct
*p
;
1011 unsigned char thref
[8];
1017 switch (remcom_in_buffer
[1]) {
1020 if (memcmp(remcom_in_buffer
+ 2, "ThreadInfo", 10)) {
1021 error_packet(remcom_out_buffer
, -EINVAL
);
1026 remcom_out_buffer
[0] = 'm';
1027 ptr
= remcom_out_buffer
+ 1;
1028 if (remcom_in_buffer
[1] == 'f') {
1029 /* Each cpu is a shadow thread */
1030 for_each_online_cpu(cpu
) {
1032 int_to_threadref(thref
, -cpu
- 2);
1033 pack_threadid(ptr
, thref
);
1034 ptr
+= BUF_THREAD_ID_SIZE
;
1040 do_each_thread(g
, p
) {
1041 if (i
>= ks
->thr_query
&& !finished
) {
1042 int_to_threadref(thref
, p
->pid
);
1043 pack_threadid(ptr
, thref
);
1044 ptr
+= BUF_THREAD_ID_SIZE
;
1047 if (ks
->thr_query
% KGDB_MAX_THREAD_QUERY
== 0)
1051 } while_each_thread(g
, p
);
1057 /* Current thread id */
1058 strcpy(remcom_out_buffer
, "QC");
1059 ks
->threadid
= shadow_pid(current
->pid
);
1060 int_to_threadref(thref
, ks
->threadid
);
1061 pack_threadid(remcom_out_buffer
+ 2, thref
);
1064 if (memcmp(remcom_in_buffer
+ 1, "ThreadExtraInfo,", 16)) {
1065 error_packet(remcom_out_buffer
, -EINVAL
);
1069 ptr
= remcom_in_buffer
+ 17;
1070 kgdb_hex2long(&ptr
, &ks
->threadid
);
1071 if (!getthread(ks
->linux_regs
, ks
->threadid
)) {
1072 error_packet(remcom_out_buffer
, -EINVAL
);
1075 if ((int)ks
->threadid
> 0) {
1076 kgdb_mem2hex(getthread(ks
->linux_regs
,
1077 ks
->threadid
)->comm
,
1078 remcom_out_buffer
, 16);
1080 static char tmpstr
[23 + BUF_THREAD_ID_SIZE
];
1082 sprintf(tmpstr
, "shadowCPU%d",
1083 (int)(-ks
->threadid
- 2));
1084 kgdb_mem2hex(tmpstr
, remcom_out_buffer
, strlen(tmpstr
));
1090 /* Handle the 'H' task query packets */
1091 static void gdb_cmd_task(struct kgdb_state
*ks
)
1093 struct task_struct
*thread
;
1096 switch (remcom_in_buffer
[1]) {
1098 ptr
= &remcom_in_buffer
[2];
1099 kgdb_hex2long(&ptr
, &ks
->threadid
);
1100 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
1101 if (!thread
&& ks
->threadid
> 0) {
1102 error_packet(remcom_out_buffer
, -EINVAL
);
1105 kgdb_usethread
= thread
;
1106 ks
->kgdb_usethreadid
= ks
->threadid
;
1107 strcpy(remcom_out_buffer
, "OK");
1110 ptr
= &remcom_in_buffer
[2];
1111 kgdb_hex2long(&ptr
, &ks
->threadid
);
1112 if (!ks
->threadid
) {
1113 kgdb_contthread
= NULL
;
1115 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
1116 if (!thread
&& ks
->threadid
> 0) {
1117 error_packet(remcom_out_buffer
, -EINVAL
);
1120 kgdb_contthread
= thread
;
1122 strcpy(remcom_out_buffer
, "OK");
1127 /* Handle the 'T' thread query packets */
1128 static void gdb_cmd_thread(struct kgdb_state
*ks
)
1130 char *ptr
= &remcom_in_buffer
[1];
1131 struct task_struct
*thread
;
1133 kgdb_hex2long(&ptr
, &ks
->threadid
);
1134 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
1136 strcpy(remcom_out_buffer
, "OK");
1138 error_packet(remcom_out_buffer
, -EINVAL
);
1141 /* Handle the 'z' or 'Z' breakpoint remove or set packets */
1142 static void gdb_cmd_break(struct kgdb_state
*ks
)
1145 * Since GDB-5.3, it's been drafted that '0' is a software
1146 * breakpoint, '1' is a hardware breakpoint, so let's do that.
1148 char *bpt_type
= &remcom_in_buffer
[1];
1149 char *ptr
= &remcom_in_buffer
[2];
1151 unsigned long length
;
1154 if (arch_kgdb_ops
.set_hw_breakpoint
&& *bpt_type
>= '1') {
1156 if (*bpt_type
> '4')
1159 if (*bpt_type
!= '0' && *bpt_type
!= '1')
1165 * Test if this is a hardware breakpoint, and
1168 if (*bpt_type
== '1' && !(arch_kgdb_ops
.flags
& KGDB_HW_BREAKPOINT
))
1172 if (*(ptr
++) != ',') {
1173 error_packet(remcom_out_buffer
, -EINVAL
);
1176 if (!kgdb_hex2long(&ptr
, &addr
)) {
1177 error_packet(remcom_out_buffer
, -EINVAL
);
1180 if (*(ptr
++) != ',' ||
1181 !kgdb_hex2long(&ptr
, &length
)) {
1182 error_packet(remcom_out_buffer
, -EINVAL
);
1186 if (remcom_in_buffer
[0] == 'Z' && *bpt_type
== '0')
1187 error
= kgdb_set_sw_break(addr
);
1188 else if (remcom_in_buffer
[0] == 'z' && *bpt_type
== '0')
1189 error
= kgdb_remove_sw_break(addr
);
1190 else if (remcom_in_buffer
[0] == 'Z')
1191 error
= arch_kgdb_ops
.set_hw_breakpoint(addr
,
1192 (int)length
, *bpt_type
- '0');
1193 else if (remcom_in_buffer
[0] == 'z')
1194 error
= arch_kgdb_ops
.remove_hw_breakpoint(addr
,
1195 (int) length
, *bpt_type
- '0');
1198 strcpy(remcom_out_buffer
, "OK");
1200 error_packet(remcom_out_buffer
, error
);
1203 /* Handle the 'C' signal / exception passing packets */
1204 static int gdb_cmd_exception_pass(struct kgdb_state
*ks
)
1206 /* C09 == pass exception
1207 * C15 == detach kgdb, pass exception
1209 if (remcom_in_buffer
[1] == '0' && remcom_in_buffer
[2] == '9') {
1211 ks
->pass_exception
= 1;
1212 remcom_in_buffer
[0] = 'c';
1214 } else if (remcom_in_buffer
[1] == '1' && remcom_in_buffer
[2] == '5') {
1216 ks
->pass_exception
= 1;
1217 remcom_in_buffer
[0] = 'D';
1223 kgdb_msg_write("KGDB only knows signal 9 (pass)"
1224 " and 15 (pass and disconnect)\n"
1225 "Executing a continue without signal passing\n", 0);
1226 remcom_in_buffer
[0] = 'c';
1229 /* Indicate fall through */
1234 * This function performs all gdbserial command procesing
1236 static int gdb_serial_stub(struct kgdb_state
*ks
)
1241 /* Clear the out buffer. */
1242 memset(remcom_out_buffer
, 0, sizeof(remcom_out_buffer
));
1244 if (kgdb_connected
) {
1245 unsigned char thref
[8];
1248 /* Reply to host that an exception has occurred */
1249 ptr
= remcom_out_buffer
;
1251 ptr
= pack_hex_byte(ptr
, ks
->signo
);
1252 ptr
+= strlen(strcpy(ptr
, "thread:"));
1253 int_to_threadref(thref
, shadow_pid(current
->pid
));
1254 ptr
= pack_threadid(ptr
, thref
);
1256 put_packet(remcom_out_buffer
);
1259 kgdb_usethread
= kgdb_info
[ks
->cpu
].task
;
1260 ks
->kgdb_usethreadid
= shadow_pid(kgdb_info
[ks
->cpu
].task
->pid
);
1261 ks
->pass_exception
= 0;
1266 /* Clear the out buffer. */
1267 memset(remcom_out_buffer
, 0, sizeof(remcom_out_buffer
));
1269 get_packet(remcom_in_buffer
);
1271 switch (remcom_in_buffer
[0]) {
1272 case '?': /* gdbserial status */
1275 case 'g': /* return the value of the CPU registers */
1276 gdb_cmd_getregs(ks
);
1278 case 'G': /* set the value of the CPU registers - return OK */
1279 gdb_cmd_setregs(ks
);
1281 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1282 gdb_cmd_memread(ks
);
1284 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1285 gdb_cmd_memwrite(ks
);
1287 case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1288 gdb_cmd_binwrite(ks
);
1290 /* kill or detach. KGDB should treat this like a
1293 case 'D': /* Debugger detach */
1294 case 'k': /* Debugger detach via kill */
1295 gdb_cmd_detachkill(ks
);
1296 goto default_handle
;
1297 case 'R': /* Reboot */
1298 if (gdb_cmd_reboot(ks
))
1299 goto default_handle
;
1301 case 'q': /* query command */
1304 case 'H': /* task related */
1307 case 'T': /* Query thread status */
1310 case 'z': /* Break point remove */
1311 case 'Z': /* Break point set */
1314 case 'C': /* Exception passing */
1315 tmp
= gdb_cmd_exception_pass(ks
);
1317 goto default_handle
;
1320 /* Fall through on tmp < 0 */
1321 case 'c': /* Continue packet */
1322 case 's': /* Single step packet */
1323 if (kgdb_contthread
&& kgdb_contthread
!= current
) {
1324 /* Can't switch threads in kgdb */
1325 error_packet(remcom_out_buffer
, -EINVAL
);
1328 kgdb_activate_sw_breakpoints();
1329 /* Fall through to default processing */
1332 error
= kgdb_arch_handle_exception(ks
->ex_vector
,
1339 * Leave cmd processing on error, detach,
1340 * kill, continue, or single step.
1342 if (error
>= 0 || remcom_in_buffer
[0] == 'D' ||
1343 remcom_in_buffer
[0] == 'k') {
1350 /* reply to the request */
1351 put_packet(remcom_out_buffer
);
1355 if (ks
->pass_exception
)
1360 static int kgdb_reenter_check(struct kgdb_state
*ks
)
1364 if (atomic_read(&kgdb_active
) != raw_smp_processor_id())
1367 /* Panic on recursive debugger calls: */
1369 addr
= kgdb_arch_pc(ks
->ex_vector
, ks
->linux_regs
);
1370 kgdb_deactivate_sw_breakpoints();
1373 * If the break point removed ok at the place exception
1374 * occurred, try to recover and print a warning to the end
1375 * user because the user planted a breakpoint in a place that
1376 * KGDB needs in order to function.
1378 if (kgdb_remove_sw_break(addr
) == 0) {
1379 exception_level
= 0;
1380 kgdb_skipexception(ks
->ex_vector
, ks
->linux_regs
);
1381 kgdb_activate_sw_breakpoints();
1382 printk(KERN_CRIT
"KGDB: re-enter error: breakpoint removed %lx\n",
1389 kgdb_skipexception(ks
->ex_vector
, ks
->linux_regs
);
1391 if (exception_level
> 1) {
1393 panic("Recursive entry to debugger");
1396 printk(KERN_CRIT
"KGDB: re-enter exception: ALL breakpoints killed\n");
1398 panic("Recursive entry to debugger");
1404 * kgdb_handle_exception() - main entry point from a kernel exception
1406 * Locking hierarchy:
1407 * interface locks, if any (begin_session)
1408 * kgdb lock (kgdb_active)
1411 kgdb_handle_exception(int evector
, int signo
, int ecode
, struct pt_regs
*regs
)
1413 struct kgdb_state kgdb_var
;
1414 struct kgdb_state
*ks
= &kgdb_var
;
1415 unsigned long flags
;
1416 int sstep_tries
= 100;
1420 ks
->cpu
= raw_smp_processor_id();
1421 ks
->ex_vector
= evector
;
1423 ks
->ex_vector
= evector
;
1424 ks
->err_code
= ecode
;
1425 ks
->kgdb_usethreadid
= 0;
1426 ks
->linux_regs
= regs
;
1428 if (kgdb_reenter_check(ks
))
1429 return 0; /* Ouch, double exception ! */
1433 * Interrupts will be restored by the 'trap return' code, except when
1436 local_irq_save(flags
);
1438 cpu
= raw_smp_processor_id();
1441 * Acquire the kgdb_active lock:
1443 while (atomic_cmpxchg(&kgdb_active
, -1, cpu
) != -1)
1447 * For single stepping, try to only enter on the processor
1448 * that was single stepping. To gaurd against a deadlock, the
1449 * kernel will only try for the value of sstep_tries before
1450 * giving up and continuing on.
1452 if (atomic_read(&kgdb_cpu_doing_single_step
) != -1 &&
1453 (kgdb_info
[cpu
].task
&&
1454 kgdb_info
[cpu
].task
->pid
!= kgdb_sstep_pid
) && --sstep_tries
) {
1455 atomic_set(&kgdb_active
, -1);
1456 touch_softlockup_watchdog_sync();
1457 clocksource_touch_watchdog();
1458 local_irq_restore(flags
);
1463 if (!kgdb_io_ready(1)) {
1465 goto kgdb_restore
; /* No I/O connection, so resume the system */
1469 * Don't enter if we have hit a removed breakpoint.
1471 if (kgdb_skipexception(ks
->ex_vector
, ks
->linux_regs
))
1474 /* Call the I/O driver's pre_exception routine */
1475 if (kgdb_io_ops
->pre_exception
)
1476 kgdb_io_ops
->pre_exception();
1478 kgdb_info
[ks
->cpu
].debuggerinfo
= ks
->linux_regs
;
1479 kgdb_info
[ks
->cpu
].task
= current
;
1481 kgdb_disable_hw_debug(ks
->linux_regs
);
1484 * Get the passive CPU lock which will hold all the non-primary
1485 * CPU in a spin state while the debugger is active
1487 if (!kgdb_single_step
) {
1488 for (i
= 0; i
< NR_CPUS
; i
++)
1489 atomic_set(&passive_cpu_wait
[i
], 1);
1493 * spin_lock code is good enough as a barrier so we don't
1496 atomic_set(&cpu_in_kgdb
[ks
->cpu
], 1);
1499 /* Signal the other CPUs to enter kgdb_wait() */
1500 if ((!kgdb_single_step
) && kgdb_do_roundup
)
1501 kgdb_roundup_cpus(flags
);
1505 * Wait for the other CPUs to be notified and be waiting for us:
1507 for_each_online_cpu(i
) {
1508 while (!atomic_read(&cpu_in_kgdb
[i
]))
1513 * At this point the primary processor is completely
1514 * in the debugger and all secondary CPUs are quiescent
1516 kgdb_post_primary_code(ks
->linux_regs
, ks
->ex_vector
, ks
->err_code
);
1517 kgdb_deactivate_sw_breakpoints();
1518 kgdb_single_step
= 0;
1519 kgdb_contthread
= current
;
1520 exception_level
= 0;
1522 /* Talk to debugger with gdbserial protocol */
1523 error
= gdb_serial_stub(ks
);
1525 /* Call the I/O driver's post_exception routine */
1526 if (kgdb_io_ops
->post_exception
)
1527 kgdb_io_ops
->post_exception();
1529 kgdb_info
[ks
->cpu
].debuggerinfo
= NULL
;
1530 kgdb_info
[ks
->cpu
].task
= NULL
;
1531 atomic_set(&cpu_in_kgdb
[ks
->cpu
], 0);
1533 if (!kgdb_single_step
) {
1534 for (i
= NR_CPUS
-1; i
>= 0; i
--)
1535 atomic_set(&passive_cpu_wait
[i
], 0);
1537 * Wait till all the CPUs have quit
1538 * from the debugger.
1540 for_each_online_cpu(i
) {
1541 while (atomic_read(&cpu_in_kgdb
[i
]))
1547 if (atomic_read(&kgdb_cpu_doing_single_step
) != -1) {
1548 int sstep_cpu
= atomic_read(&kgdb_cpu_doing_single_step
);
1549 if (kgdb_info
[sstep_cpu
].task
)
1550 kgdb_sstep_pid
= kgdb_info
[sstep_cpu
].task
->pid
;
1554 /* Free kgdb_active */
1555 atomic_set(&kgdb_active
, -1);
1556 touch_softlockup_watchdog_sync();
1557 clocksource_touch_watchdog();
1558 local_irq_restore(flags
);
1563 int kgdb_nmicallback(int cpu
, void *regs
)
1566 if (!atomic_read(&cpu_in_kgdb
[cpu
]) &&
1567 atomic_read(&kgdb_active
) != cpu
&&
1568 atomic_read(&cpu_in_kgdb
[atomic_read(&kgdb_active
)])) {
1569 kgdb_wait((struct pt_regs
*)regs
);
1576 static void kgdb_console_write(struct console
*co
, const char *s
,
1579 unsigned long flags
;
1581 /* If we're debugging, or KGDB has not connected, don't try
1583 if (!kgdb_connected
|| atomic_read(&kgdb_active
) != -1)
1586 local_irq_save(flags
);
1587 kgdb_msg_write(s
, count
);
1588 local_irq_restore(flags
);
1591 static struct console kgdbcons
= {
1593 .write
= kgdb_console_write
,
1594 .flags
= CON_PRINTBUFFER
| CON_ENABLED
,
1598 #ifdef CONFIG_MAGIC_SYSRQ
1599 static void sysrq_handle_gdb(int key
, struct tty_struct
*tty
)
1602 printk(KERN_CRIT
"ERROR: No KGDB I/O module available\n");
1605 if (!kgdb_connected
)
1606 printk(KERN_CRIT
"Entering KGDB\n");
1611 static struct sysrq_key_op sysrq_gdb_op
= {
1612 .handler
= sysrq_handle_gdb
,
1613 .help_msg
= "debug(G)",
1614 .action_msg
= "DEBUG",
1618 static void kgdb_register_callbacks(void)
1620 if (!kgdb_io_module_registered
) {
1621 kgdb_io_module_registered
= 1;
1623 #ifdef CONFIG_MAGIC_SYSRQ
1624 register_sysrq_key('g', &sysrq_gdb_op
);
1626 if (kgdb_use_con
&& !kgdb_con_registered
) {
1627 register_console(&kgdbcons
);
1628 kgdb_con_registered
= 1;
1633 static void kgdb_unregister_callbacks(void)
1636 * When this routine is called KGDB should unregister from the
1637 * panic handler and clean up, making sure it is not handling any
1638 * break exceptions at the time.
1640 if (kgdb_io_module_registered
) {
1641 kgdb_io_module_registered
= 0;
1643 #ifdef CONFIG_MAGIC_SYSRQ
1644 unregister_sysrq_key('g', &sysrq_gdb_op
);
1646 if (kgdb_con_registered
) {
1647 unregister_console(&kgdbcons
);
1648 kgdb_con_registered
= 0;
1653 static void kgdb_initial_breakpoint(void)
1655 kgdb_break_asap
= 0;
1657 printk(KERN_CRIT
"kgdb: Waiting for connection from remote gdb...\n");
1662 * kgdb_register_io_module - register KGDB IO module
1663 * @new_kgdb_io_ops: the io ops vector
1665 * Register it with the KGDB core.
1667 int kgdb_register_io_module(struct kgdb_io
*new_kgdb_io_ops
)
1671 spin_lock(&kgdb_registration_lock
);
1674 spin_unlock(&kgdb_registration_lock
);
1676 printk(KERN_ERR
"kgdb: Another I/O driver is already "
1677 "registered with KGDB.\n");
1681 if (new_kgdb_io_ops
->init
) {
1682 err
= new_kgdb_io_ops
->init();
1684 spin_unlock(&kgdb_registration_lock
);
1689 kgdb_io_ops
= new_kgdb_io_ops
;
1691 spin_unlock(&kgdb_registration_lock
);
1693 printk(KERN_INFO
"kgdb: Registered I/O driver %s.\n",
1694 new_kgdb_io_ops
->name
);
1697 kgdb_register_callbacks();
1699 if (kgdb_break_asap
)
1700 kgdb_initial_breakpoint();
1704 EXPORT_SYMBOL_GPL(kgdb_register_io_module
);
1707 * kkgdb_unregister_io_module - unregister KGDB IO module
1708 * @old_kgdb_io_ops: the io ops vector
1710 * Unregister it with the KGDB core.
1712 void kgdb_unregister_io_module(struct kgdb_io
*old_kgdb_io_ops
)
1714 BUG_ON(kgdb_connected
);
1717 * KGDB is no longer able to communicate out, so
1718 * unregister our callbacks and reset state.
1720 kgdb_unregister_callbacks();
1722 spin_lock(&kgdb_registration_lock
);
1724 WARN_ON_ONCE(kgdb_io_ops
!= old_kgdb_io_ops
);
1727 spin_unlock(&kgdb_registration_lock
);
1730 "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
1731 old_kgdb_io_ops
->name
);
1733 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module
);
1736 * kgdb_breakpoint - generate breakpoint exception
1738 * This function will generate a breakpoint exception. It is used at the
1739 * beginning of a program to sync up with a debugger and can be used
1740 * otherwise as a quick means to stop program execution and "break" into
1743 void kgdb_breakpoint(void)
1745 atomic_set(&kgdb_setting_breakpoint
, 1);
1746 wmb(); /* Sync point before breakpoint */
1747 arch_kgdb_breakpoint();
1748 wmb(); /* Sync point after breakpoint */
1749 atomic_set(&kgdb_setting_breakpoint
, 0);
1751 EXPORT_SYMBOL_GPL(kgdb_breakpoint
);
1753 static int __init
opt_kgdb_wait(char *str
)
1755 kgdb_break_asap
= 1;
1757 if (kgdb_io_module_registered
)
1758 kgdb_initial_breakpoint();
1763 early_param("kgdbwait", opt_kgdb_wait
);