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 /* Exception state values */
73 #define DCPU_WANT_MASTER 0x1 /* Waiting to become a master kgdb cpu */
74 #define DCPU_NEXT_MASTER 0x2 /* Transition from one master cpu to another */
75 #define DCPU_IS_SLAVE 0x4 /* Slave cpu enter exception */
76 #define DCPU_SSTEP 0x8 /* CPU is single stepping */
78 static struct debuggerinfo_struct
{
80 struct task_struct
*task
;
85 * kgdb_connected - Is a host GDB connected to us?
88 EXPORT_SYMBOL_GPL(kgdb_connected
);
90 /* All the KGDB handlers are installed */
91 static int kgdb_io_module_registered
;
93 /* Guard for recursive entry */
94 static int exception_level
;
96 static struct kgdb_io
*kgdb_io_ops
;
97 static DEFINE_SPINLOCK(kgdb_registration_lock
);
99 /* kgdb console driver is loaded */
100 static int kgdb_con_registered
;
101 /* determine if kgdb console output should be used */
102 static int kgdb_use_con
;
104 static int __init
opt_kgdb_con(char *str
)
110 early_param("kgdbcon", opt_kgdb_con
);
112 module_param(kgdb_use_con
, int, 0644);
115 * Holds information about breakpoints in a kernel. These breakpoints are
116 * added and removed by gdb.
118 static struct kgdb_bkpt kgdb_break
[KGDB_MAX_BREAKPOINTS
] = {
119 [0 ... KGDB_MAX_BREAKPOINTS
-1] = { .state
= BP_UNDEFINED
}
123 * The CPU# of the active CPU, or -1 if none:
125 atomic_t kgdb_active
= ATOMIC_INIT(-1);
128 * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
129 * bootup code (which might not have percpu set up yet):
131 static atomic_t passive_cpu_wait
[NR_CPUS
];
132 static atomic_t cpu_in_kgdb
[NR_CPUS
];
133 atomic_t kgdb_setting_breakpoint
;
135 struct task_struct
*kgdb_usethread
;
136 struct task_struct
*kgdb_contthread
;
138 int kgdb_single_step
;
139 pid_t kgdb_sstep_pid
;
141 /* Our I/O buffers. */
142 static char remcom_in_buffer
[BUFMAX
];
143 static char remcom_out_buffer
[BUFMAX
];
145 /* Storage for the registers, in GDB format. */
146 static unsigned long gdb_regs
[(NUMREGBYTES
+
147 sizeof(unsigned long) - 1) /
148 sizeof(unsigned long)];
150 /* to keep track of the CPU which is doing the single stepping*/
151 atomic_t kgdb_cpu_doing_single_step
= ATOMIC_INIT(-1);
154 * If you are debugging a problem where roundup (the collection of
155 * all other CPUs) is a problem [this should be extremely rare],
156 * then use the nokgdbroundup option to avoid roundup. In that case
157 * the other CPUs might interfere with your debugging context, so
158 * use this with care:
160 static int kgdb_do_roundup
= 1;
162 static int __init
opt_nokgdbroundup(char *str
)
169 early_param("nokgdbroundup", opt_nokgdbroundup
);
172 * Finally, some KGDB code :-)
176 * Weak aliases for breakpoint management,
177 * can be overriden by architectures when needed:
179 int __weak
kgdb_arch_set_breakpoint(unsigned long addr
, char *saved_instr
)
183 err
= probe_kernel_read(saved_instr
, (char *)addr
, BREAK_INSTR_SIZE
);
187 return probe_kernel_write((char *)addr
, arch_kgdb_ops
.gdb_bpt_instr
,
191 int __weak
kgdb_arch_remove_breakpoint(unsigned long addr
, char *bundle
)
193 return probe_kernel_write((char *)addr
,
194 (char *)bundle
, BREAK_INSTR_SIZE
);
197 int __weak
kgdb_validate_break_address(unsigned long addr
)
199 char tmp_variable
[BREAK_INSTR_SIZE
];
201 /* Validate setting the breakpoint and then removing it. In the
202 * remove fails, the kernel needs to emit a bad message because we
203 * are deep trouble not being able to put things back the way we
206 err
= kgdb_arch_set_breakpoint(addr
, tmp_variable
);
209 err
= kgdb_arch_remove_breakpoint(addr
, tmp_variable
);
211 printk(KERN_ERR
"KGDB: Critical breakpoint error, kernel "
212 "memory destroyed at: %lx", addr
);
216 unsigned long __weak
kgdb_arch_pc(int exception
, struct pt_regs
*regs
)
218 return instruction_pointer(regs
);
221 int __weak
kgdb_arch_init(void)
226 int __weak
kgdb_skipexception(int exception
, struct pt_regs
*regs
)
232 kgdb_post_primary_code(struct pt_regs
*regs
, int e_vector
, int err_code
)
238 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
239 * @regs: Current &struct pt_regs.
241 * This function will be called if the particular architecture must
242 * disable hardware debugging while it is processing gdb packets or
243 * handling exception.
245 void __weak
kgdb_disable_hw_debug(struct pt_regs
*regs
)
250 * GDB remote protocol parser:
253 static int hex(char ch
)
255 if ((ch
>= 'a') && (ch
<= 'f'))
256 return ch
- 'a' + 10;
257 if ((ch
>= '0') && (ch
<= '9'))
259 if ((ch
>= 'A') && (ch
<= 'F'))
260 return ch
- 'A' + 10;
264 /* scan for the sequence $<data>#<checksum> */
265 static void get_packet(char *buffer
)
267 unsigned char checksum
;
268 unsigned char xmitcsum
;
274 * Spin and wait around for the start character, ignore all
277 while ((ch
= (kgdb_io_ops
->read_char())) != '$')
287 * now, read until a # or end of buffer is found:
289 while (count
< (BUFMAX
- 1)) {
290 ch
= kgdb_io_ops
->read_char();
293 checksum
= checksum
+ ch
;
300 xmitcsum
= hex(kgdb_io_ops
->read_char()) << 4;
301 xmitcsum
+= hex(kgdb_io_ops
->read_char());
303 if (checksum
!= xmitcsum
)
304 /* failed checksum */
305 kgdb_io_ops
->write_char('-');
307 /* successful transfer */
308 kgdb_io_ops
->write_char('+');
309 if (kgdb_io_ops
->flush
)
310 kgdb_io_ops
->flush();
312 } while (checksum
!= xmitcsum
);
316 * Send the packet in buffer.
317 * Check for gdb connection if asked for.
319 static void put_packet(char *buffer
)
321 unsigned char checksum
;
326 * $<packet info>#<checksum>.
329 kgdb_io_ops
->write_char('$');
333 while ((ch
= buffer
[count
])) {
334 kgdb_io_ops
->write_char(ch
);
339 kgdb_io_ops
->write_char('#');
340 kgdb_io_ops
->write_char(hex_asc_hi(checksum
));
341 kgdb_io_ops
->write_char(hex_asc_lo(checksum
));
342 if (kgdb_io_ops
->flush
)
343 kgdb_io_ops
->flush();
345 /* Now see what we get in reply. */
346 ch
= kgdb_io_ops
->read_char();
349 ch
= kgdb_io_ops
->read_char();
351 /* If we get an ACK, we are done. */
356 * If we get the start of another packet, this means
357 * that GDB is attempting to reconnect. We will NAK
358 * the packet being sent, and stop trying to send this
362 kgdb_io_ops
->write_char('-');
363 if (kgdb_io_ops
->flush
)
364 kgdb_io_ops
->flush();
371 * Convert the memory pointed to by mem into hex, placing result in buf.
372 * Return a pointer to the last char put in buf (null). May return an error.
374 int kgdb_mem2hex(char *mem
, char *buf
, int count
)
380 * We use the upper half of buf as an intermediate buffer for the
381 * raw memory copy. Hex conversion will work against this one.
385 err
= probe_kernel_read(tmp
, mem
, count
);
388 buf
= pack_hex_byte(buf
, *tmp
);
400 * Copy the binary array pointed to by buf into mem. Fix $, #, and
401 * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success.
402 * The input buf is overwitten with the result to write to mem.
404 static int kgdb_ebin2mem(char *buf
, char *mem
, int count
)
409 while (count
-- > 0) {
412 c
[size
] = *buf
++ ^ 0x20;
416 return probe_kernel_write(mem
, c
, size
);
420 * Convert the hex array pointed to by buf into binary to be placed in mem.
421 * Return a pointer to the character AFTER the last byte written.
422 * May return an error.
424 int kgdb_hex2mem(char *buf
, char *mem
, int count
)
430 * We use the upper half of buf as an intermediate buffer for the
431 * raw memory that is converted from hex.
433 tmp_raw
= buf
+ count
* 2;
435 tmp_hex
= tmp_raw
- 1;
436 while (tmp_hex
>= buf
) {
438 *tmp_raw
= hex(*tmp_hex
--);
439 *tmp_raw
|= hex(*tmp_hex
--) << 4;
442 return probe_kernel_write(mem
, tmp_raw
, count
);
446 * While we find nice hex chars, build a long_val.
447 * Return number of chars processed.
449 int kgdb_hex2long(char **ptr
, unsigned long *long_val
)
462 hex_val
= hex(**ptr
);
466 *long_val
= (*long_val
<< 4) | hex_val
;
472 *long_val
= -*long_val
;
477 /* Write memory due to an 'M' or 'X' packet. */
478 static int write_mem_msg(int binary
)
480 char *ptr
= &remcom_in_buffer
[1];
482 unsigned long length
;
485 if (kgdb_hex2long(&ptr
, &addr
) > 0 && *(ptr
++) == ',' &&
486 kgdb_hex2long(&ptr
, &length
) > 0 && *(ptr
++) == ':') {
488 err
= kgdb_ebin2mem(ptr
, (char *)addr
, length
);
490 err
= kgdb_hex2mem(ptr
, (char *)addr
, length
);
493 if (CACHE_FLUSH_IS_SAFE
)
494 flush_icache_range(addr
, addr
+ length
);
501 static void error_packet(char *pkt
, int error
)
505 pkt
[1] = hex_asc
[(error
/ 10)];
506 pkt
[2] = hex_asc
[(error
% 10)];
511 * Thread ID accessors. We represent a flat TID space to GDB, where
512 * the per CPU idle threads (which under Linux all have PID 0) are
513 * remapped to negative TIDs.
516 #define BUF_THREAD_ID_SIZE 16
518 static char *pack_threadid(char *pkt
, unsigned char *id
)
522 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
524 pkt
= pack_hex_byte(pkt
, *id
++);
529 static void int_to_threadref(unsigned char *id
, int value
)
534 scan
= (unsigned char *)id
;
537 put_unaligned_be32(value
, scan
);
540 static struct task_struct
*getthread(struct pt_regs
*regs
, int tid
)
543 * Non-positive TIDs are remapped to the cpu shadow information
545 if (tid
== 0 || tid
== -1)
546 tid
= -atomic_read(&kgdb_active
) - 2;
547 if (tid
< -1 && tid
> -NR_CPUS
- 2) {
548 if (kgdb_info
[-tid
- 2].task
)
549 return kgdb_info
[-tid
- 2].task
;
551 return idle_task(-tid
- 2);
554 printk(KERN_ERR
"KGDB: Internal thread select error\n");
560 * find_task_by_pid_ns() does not take the tasklist lock anymore
561 * but is nicely RCU locked - hence is a pretty resilient
564 return find_task_by_pid_ns(tid
, &init_pid_ns
);
568 * Some architectures need cache flushes when we set/clear a
571 static void kgdb_flush_swbreak_addr(unsigned long addr
)
573 if (!CACHE_FLUSH_IS_SAFE
)
576 if (current
->mm
&& current
->mm
->mmap_cache
) {
577 flush_cache_range(current
->mm
->mmap_cache
,
578 addr
, addr
+ BREAK_INSTR_SIZE
);
580 /* Force flush instruction cache if it was outside the mm */
581 flush_icache_range(addr
, addr
+ BREAK_INSTR_SIZE
);
585 * SW breakpoint management:
587 static int kgdb_activate_sw_breakpoints(void)
594 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
595 if (kgdb_break
[i
].state
!= BP_SET
)
598 addr
= kgdb_break
[i
].bpt_addr
;
599 error
= kgdb_arch_set_breakpoint(addr
,
600 kgdb_break
[i
].saved_instr
);
603 printk(KERN_INFO
"KGDB: BP install failed: %lx", addr
);
607 kgdb_flush_swbreak_addr(addr
);
608 kgdb_break
[i
].state
= BP_ACTIVE
;
613 static int kgdb_set_sw_break(unsigned long addr
)
615 int err
= kgdb_validate_break_address(addr
);
622 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
623 if ((kgdb_break
[i
].state
== BP_SET
) &&
624 (kgdb_break
[i
].bpt_addr
== addr
))
627 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
628 if (kgdb_break
[i
].state
== BP_REMOVED
&&
629 kgdb_break
[i
].bpt_addr
== addr
) {
636 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
637 if (kgdb_break
[i
].state
== BP_UNDEFINED
) {
647 kgdb_break
[breakno
].state
= BP_SET
;
648 kgdb_break
[breakno
].type
= BP_BREAKPOINT
;
649 kgdb_break
[breakno
].bpt_addr
= addr
;
654 static int kgdb_deactivate_sw_breakpoints(void)
661 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
662 if (kgdb_break
[i
].state
!= BP_ACTIVE
)
664 addr
= kgdb_break
[i
].bpt_addr
;
665 error
= kgdb_arch_remove_breakpoint(addr
,
666 kgdb_break
[i
].saved_instr
);
668 printk(KERN_INFO
"KGDB: BP remove failed: %lx\n", addr
);
672 kgdb_flush_swbreak_addr(addr
);
673 kgdb_break
[i
].state
= BP_SET
;
678 static int kgdb_remove_sw_break(unsigned long addr
)
682 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
683 if ((kgdb_break
[i
].state
== BP_SET
) &&
684 (kgdb_break
[i
].bpt_addr
== addr
)) {
685 kgdb_break
[i
].state
= BP_REMOVED
;
692 int kgdb_isremovedbreak(unsigned long addr
)
696 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
697 if ((kgdb_break
[i
].state
== BP_REMOVED
) &&
698 (kgdb_break
[i
].bpt_addr
== addr
))
704 static int remove_all_break(void)
710 /* Clear memory breakpoints. */
711 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
712 if (kgdb_break
[i
].state
!= BP_ACTIVE
)
714 addr
= kgdb_break
[i
].bpt_addr
;
715 error
= kgdb_arch_remove_breakpoint(addr
,
716 kgdb_break
[i
].saved_instr
);
718 printk(KERN_ERR
"KGDB: breakpoint remove failed: %lx\n",
721 kgdb_break
[i
].state
= BP_UNDEFINED
;
724 /* Clear hardware breakpoints. */
725 if (arch_kgdb_ops
.remove_all_hw_break
)
726 arch_kgdb_ops
.remove_all_hw_break();
732 * Remap normal tasks to their real PID,
733 * CPU shadow threads are mapped to -CPU - 2
735 static inline int shadow_pid(int realpid
)
740 return -raw_smp_processor_id() - 2;
743 static char gdbmsgbuf
[BUFMAX
+ 1];
745 static void kgdb_msg_write(const char *s
, int len
)
754 /* Fill and send buffers... */
756 bufptr
= gdbmsgbuf
+ 1;
758 /* Calculate how many this time */
759 if ((len
<< 1) > (BUFMAX
- 2))
760 wcount
= (BUFMAX
- 2) >> 1;
764 /* Pack in hex chars */
765 for (i
= 0; i
< wcount
; i
++)
766 bufptr
= pack_hex_byte(bufptr
, s
[i
]);
774 put_packet(gdbmsgbuf
);
779 * Return true if there is a valid kgdb I/O module. Also if no
780 * debugger is attached a message can be printed to the console about
781 * waiting for the debugger to attach.
783 * The print_wait argument is only to be true when called from inside
784 * the core kgdb_handle_exception, because it will wait for the
785 * debugger to attach.
787 static int kgdb_io_ready(int print_wait
)
793 if (atomic_read(&kgdb_setting_breakpoint
))
796 printk(KERN_CRIT
"KGDB: Waiting for remote debugger\n");
801 * All the functions that start with gdb_cmd are the various
802 * operations to implement the handlers for the gdbserial protocol
803 * where KGDB is communicating with an external debugger
806 /* Handle the '?' status packets */
807 static void gdb_cmd_status(struct kgdb_state
*ks
)
810 * We know that this packet is only sent
811 * during initial connect. So to be safe,
812 * we clear out our breakpoints now in case
813 * GDB is reconnecting.
817 remcom_out_buffer
[0] = 'S';
818 pack_hex_byte(&remcom_out_buffer
[1], ks
->signo
);
821 /* Handle the 'g' get registers request */
822 static void gdb_cmd_getregs(struct kgdb_state
*ks
)
824 struct task_struct
*thread
;
825 void *local_debuggerinfo
;
828 thread
= kgdb_usethread
;
830 thread
= kgdb_info
[ks
->cpu
].task
;
831 local_debuggerinfo
= kgdb_info
[ks
->cpu
].debuggerinfo
;
833 local_debuggerinfo
= NULL
;
834 for_each_online_cpu(i
) {
836 * Try to find the task on some other
837 * or possibly this node if we do not
838 * find the matching task then we try
839 * to approximate the results.
841 if (thread
== kgdb_info
[i
].task
)
842 local_debuggerinfo
= kgdb_info
[i
].debuggerinfo
;
847 * All threads that don't have debuggerinfo should be
848 * in schedule() sleeping, since all other CPUs
849 * are in kgdb_wait, and thus have debuggerinfo.
851 if (local_debuggerinfo
) {
852 pt_regs_to_gdb_regs(gdb_regs
, local_debuggerinfo
);
855 * Pull stuff saved during switch_to; nothing
856 * else is accessible (or even particularly
859 * This should be enough for a stack trace.
861 sleeping_thread_to_gdb_regs(gdb_regs
, thread
);
863 kgdb_mem2hex((char *)gdb_regs
, remcom_out_buffer
, NUMREGBYTES
);
866 /* Handle the 'G' set registers request */
867 static void gdb_cmd_setregs(struct kgdb_state
*ks
)
869 kgdb_hex2mem(&remcom_in_buffer
[1], (char *)gdb_regs
, NUMREGBYTES
);
871 if (kgdb_usethread
&& kgdb_usethread
!= current
) {
872 error_packet(remcom_out_buffer
, -EINVAL
);
874 gdb_regs_to_pt_regs(gdb_regs
, ks
->linux_regs
);
875 strcpy(remcom_out_buffer
, "OK");
879 /* Handle the 'm' memory read bytes */
880 static void gdb_cmd_memread(struct kgdb_state
*ks
)
882 char *ptr
= &remcom_in_buffer
[1];
883 unsigned long length
;
887 if (kgdb_hex2long(&ptr
, &addr
) > 0 && *ptr
++ == ',' &&
888 kgdb_hex2long(&ptr
, &length
) > 0) {
889 err
= kgdb_mem2hex((char *)addr
, remcom_out_buffer
, length
);
891 error_packet(remcom_out_buffer
, err
);
893 error_packet(remcom_out_buffer
, -EINVAL
);
897 /* Handle the 'M' memory write bytes */
898 static void gdb_cmd_memwrite(struct kgdb_state
*ks
)
900 int err
= write_mem_msg(0);
903 error_packet(remcom_out_buffer
, err
);
905 strcpy(remcom_out_buffer
, "OK");
908 /* Handle the 'X' memory binary write bytes */
909 static void gdb_cmd_binwrite(struct kgdb_state
*ks
)
911 int err
= write_mem_msg(1);
914 error_packet(remcom_out_buffer
, err
);
916 strcpy(remcom_out_buffer
, "OK");
919 /* Handle the 'D' or 'k', detach or kill packets */
920 static void gdb_cmd_detachkill(struct kgdb_state
*ks
)
924 /* The detach case */
925 if (remcom_in_buffer
[0] == 'D') {
926 error
= remove_all_break();
928 error_packet(remcom_out_buffer
, error
);
930 strcpy(remcom_out_buffer
, "OK");
933 put_packet(remcom_out_buffer
);
936 * Assume the kill case, with no exit code checking,
937 * trying to force detach the debugger:
944 /* Handle the 'R' reboot packets */
945 static int gdb_cmd_reboot(struct kgdb_state
*ks
)
947 /* For now, only honor R0 */
948 if (strcmp(remcom_in_buffer
, "R0") == 0) {
949 printk(KERN_CRIT
"Executing emergency reboot\n");
950 strcpy(remcom_out_buffer
, "OK");
951 put_packet(remcom_out_buffer
);
954 * Execution should not return from
955 * machine_emergency_restart()
957 machine_emergency_restart();
965 /* Handle the 'q' query packets */
966 static void gdb_cmd_query(struct kgdb_state
*ks
)
968 struct task_struct
*g
;
969 struct task_struct
*p
;
970 unsigned char thref
[8];
976 switch (remcom_in_buffer
[1]) {
979 if (memcmp(remcom_in_buffer
+ 2, "ThreadInfo", 10))
983 remcom_out_buffer
[0] = 'm';
984 ptr
= remcom_out_buffer
+ 1;
985 if (remcom_in_buffer
[1] == 'f') {
986 /* Each cpu is a shadow thread */
987 for_each_online_cpu(cpu
) {
989 int_to_threadref(thref
, -cpu
- 2);
990 pack_threadid(ptr
, thref
);
991 ptr
+= BUF_THREAD_ID_SIZE
;
997 do_each_thread(g
, p
) {
998 if (i
>= ks
->thr_query
&& !finished
) {
999 int_to_threadref(thref
, p
->pid
);
1000 pack_threadid(ptr
, thref
);
1001 ptr
+= BUF_THREAD_ID_SIZE
;
1004 if (ks
->thr_query
% KGDB_MAX_THREAD_QUERY
== 0)
1008 } while_each_thread(g
, p
);
1014 /* Current thread id */
1015 strcpy(remcom_out_buffer
, "QC");
1016 ks
->threadid
= shadow_pid(current
->pid
);
1017 int_to_threadref(thref
, ks
->threadid
);
1018 pack_threadid(remcom_out_buffer
+ 2, thref
);
1021 if (memcmp(remcom_in_buffer
+ 1, "ThreadExtraInfo,", 16))
1025 ptr
= remcom_in_buffer
+ 17;
1026 kgdb_hex2long(&ptr
, &ks
->threadid
);
1027 if (!getthread(ks
->linux_regs
, ks
->threadid
)) {
1028 error_packet(remcom_out_buffer
, -EINVAL
);
1031 if ((int)ks
->threadid
> 0) {
1032 kgdb_mem2hex(getthread(ks
->linux_regs
,
1033 ks
->threadid
)->comm
,
1034 remcom_out_buffer
, 16);
1036 static char tmpstr
[23 + BUF_THREAD_ID_SIZE
];
1038 sprintf(tmpstr
, "shadowCPU%d",
1039 (int)(-ks
->threadid
- 2));
1040 kgdb_mem2hex(tmpstr
, remcom_out_buffer
, strlen(tmpstr
));
1046 /* Handle the 'H' task query packets */
1047 static void gdb_cmd_task(struct kgdb_state
*ks
)
1049 struct task_struct
*thread
;
1052 switch (remcom_in_buffer
[1]) {
1054 ptr
= &remcom_in_buffer
[2];
1055 kgdb_hex2long(&ptr
, &ks
->threadid
);
1056 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
1057 if (!thread
&& ks
->threadid
> 0) {
1058 error_packet(remcom_out_buffer
, -EINVAL
);
1061 kgdb_usethread
= thread
;
1062 ks
->kgdb_usethreadid
= ks
->threadid
;
1063 strcpy(remcom_out_buffer
, "OK");
1066 ptr
= &remcom_in_buffer
[2];
1067 kgdb_hex2long(&ptr
, &ks
->threadid
);
1068 if (!ks
->threadid
) {
1069 kgdb_contthread
= NULL
;
1071 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
1072 if (!thread
&& ks
->threadid
> 0) {
1073 error_packet(remcom_out_buffer
, -EINVAL
);
1076 kgdb_contthread
= thread
;
1078 strcpy(remcom_out_buffer
, "OK");
1083 /* Handle the 'T' thread query packets */
1084 static void gdb_cmd_thread(struct kgdb_state
*ks
)
1086 char *ptr
= &remcom_in_buffer
[1];
1087 struct task_struct
*thread
;
1089 kgdb_hex2long(&ptr
, &ks
->threadid
);
1090 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
1092 strcpy(remcom_out_buffer
, "OK");
1094 error_packet(remcom_out_buffer
, -EINVAL
);
1097 /* Handle the 'z' or 'Z' breakpoint remove or set packets */
1098 static void gdb_cmd_break(struct kgdb_state
*ks
)
1101 * Since GDB-5.3, it's been drafted that '0' is a software
1102 * breakpoint, '1' is a hardware breakpoint, so let's do that.
1104 char *bpt_type
= &remcom_in_buffer
[1];
1105 char *ptr
= &remcom_in_buffer
[2];
1107 unsigned long length
;
1110 if (arch_kgdb_ops
.set_hw_breakpoint
&& *bpt_type
>= '1') {
1112 if (*bpt_type
> '4')
1115 if (*bpt_type
!= '0' && *bpt_type
!= '1')
1121 * Test if this is a hardware breakpoint, and
1124 if (*bpt_type
== '1' && !(arch_kgdb_ops
.flags
& KGDB_HW_BREAKPOINT
))
1128 if (*(ptr
++) != ',') {
1129 error_packet(remcom_out_buffer
, -EINVAL
);
1132 if (!kgdb_hex2long(&ptr
, &addr
)) {
1133 error_packet(remcom_out_buffer
, -EINVAL
);
1136 if (*(ptr
++) != ',' ||
1137 !kgdb_hex2long(&ptr
, &length
)) {
1138 error_packet(remcom_out_buffer
, -EINVAL
);
1142 if (remcom_in_buffer
[0] == 'Z' && *bpt_type
== '0')
1143 error
= kgdb_set_sw_break(addr
);
1144 else if (remcom_in_buffer
[0] == 'z' && *bpt_type
== '0')
1145 error
= kgdb_remove_sw_break(addr
);
1146 else if (remcom_in_buffer
[0] == 'Z')
1147 error
= arch_kgdb_ops
.set_hw_breakpoint(addr
,
1148 (int)length
, *bpt_type
- '0');
1149 else if (remcom_in_buffer
[0] == 'z')
1150 error
= arch_kgdb_ops
.remove_hw_breakpoint(addr
,
1151 (int) length
, *bpt_type
- '0');
1154 strcpy(remcom_out_buffer
, "OK");
1156 error_packet(remcom_out_buffer
, error
);
1159 /* Handle the 'C' signal / exception passing packets */
1160 static int gdb_cmd_exception_pass(struct kgdb_state
*ks
)
1162 /* C09 == pass exception
1163 * C15 == detach kgdb, pass exception
1165 if (remcom_in_buffer
[1] == '0' && remcom_in_buffer
[2] == '9') {
1167 ks
->pass_exception
= 1;
1168 remcom_in_buffer
[0] = 'c';
1170 } else if (remcom_in_buffer
[1] == '1' && remcom_in_buffer
[2] == '5') {
1172 ks
->pass_exception
= 1;
1173 remcom_in_buffer
[0] = 'D';
1179 kgdb_msg_write("KGDB only knows signal 9 (pass)"
1180 " and 15 (pass and disconnect)\n"
1181 "Executing a continue without signal passing\n", 0);
1182 remcom_in_buffer
[0] = 'c';
1185 /* Indicate fall through */
1190 * This function performs all gdbserial command procesing
1192 static int gdb_serial_stub(struct kgdb_state
*ks
)
1197 /* Clear the out buffer. */
1198 memset(remcom_out_buffer
, 0, sizeof(remcom_out_buffer
));
1200 if (kgdb_connected
) {
1201 unsigned char thref
[8];
1204 /* Reply to host that an exception has occurred */
1205 ptr
= remcom_out_buffer
;
1207 ptr
= pack_hex_byte(ptr
, ks
->signo
);
1208 ptr
+= strlen(strcpy(ptr
, "thread:"));
1209 int_to_threadref(thref
, shadow_pid(current
->pid
));
1210 ptr
= pack_threadid(ptr
, thref
);
1212 put_packet(remcom_out_buffer
);
1215 kgdb_usethread
= kgdb_info
[ks
->cpu
].task
;
1216 ks
->kgdb_usethreadid
= shadow_pid(kgdb_info
[ks
->cpu
].task
->pid
);
1217 ks
->pass_exception
= 0;
1222 /* Clear the out buffer. */
1223 memset(remcom_out_buffer
, 0, sizeof(remcom_out_buffer
));
1225 get_packet(remcom_in_buffer
);
1227 switch (remcom_in_buffer
[0]) {
1228 case '?': /* gdbserial status */
1231 case 'g': /* return the value of the CPU registers */
1232 gdb_cmd_getregs(ks
);
1234 case 'G': /* set the value of the CPU registers - return OK */
1235 gdb_cmd_setregs(ks
);
1237 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1238 gdb_cmd_memread(ks
);
1240 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1241 gdb_cmd_memwrite(ks
);
1243 case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1244 gdb_cmd_binwrite(ks
);
1246 /* kill or detach. KGDB should treat this like a
1249 case 'D': /* Debugger detach */
1250 case 'k': /* Debugger detach via kill */
1251 gdb_cmd_detachkill(ks
);
1252 goto default_handle
;
1253 case 'R': /* Reboot */
1254 if (gdb_cmd_reboot(ks
))
1255 goto default_handle
;
1257 case 'q': /* query command */
1260 case 'H': /* task related */
1263 case 'T': /* Query thread status */
1266 case 'z': /* Break point remove */
1267 case 'Z': /* Break point set */
1270 case 'C': /* Exception passing */
1271 tmp
= gdb_cmd_exception_pass(ks
);
1273 goto default_handle
;
1276 /* Fall through on tmp < 0 */
1277 case 'c': /* Continue packet */
1278 case 's': /* Single step packet */
1279 if (kgdb_contthread
&& kgdb_contthread
!= current
) {
1280 /* Can't switch threads in kgdb */
1281 error_packet(remcom_out_buffer
, -EINVAL
);
1284 kgdb_activate_sw_breakpoints();
1285 /* Fall through to default processing */
1288 error
= kgdb_arch_handle_exception(ks
->ex_vector
,
1295 * Leave cmd processing on error, detach,
1296 * kill, continue, or single step.
1298 if (error
>= 0 || remcom_in_buffer
[0] == 'D' ||
1299 remcom_in_buffer
[0] == 'k') {
1306 /* reply to the request */
1307 put_packet(remcom_out_buffer
);
1311 if (ks
->pass_exception
)
1316 static int kgdb_reenter_check(struct kgdb_state
*ks
)
1320 if (atomic_read(&kgdb_active
) != raw_smp_processor_id())
1323 /* Panic on recursive debugger calls: */
1325 addr
= kgdb_arch_pc(ks
->ex_vector
, ks
->linux_regs
);
1326 kgdb_deactivate_sw_breakpoints();
1329 * If the break point removed ok at the place exception
1330 * occurred, try to recover and print a warning to the end
1331 * user because the user planted a breakpoint in a place that
1332 * KGDB needs in order to function.
1334 if (kgdb_remove_sw_break(addr
) == 0) {
1335 exception_level
= 0;
1336 kgdb_skipexception(ks
->ex_vector
, ks
->linux_regs
);
1337 kgdb_activate_sw_breakpoints();
1338 printk(KERN_CRIT
"KGDB: re-enter error: breakpoint removed %lx\n",
1345 kgdb_skipexception(ks
->ex_vector
, ks
->linux_regs
);
1347 if (exception_level
> 1) {
1349 panic("Recursive entry to debugger");
1352 printk(KERN_CRIT
"KGDB: re-enter exception: ALL breakpoints killed\n");
1354 panic("Recursive entry to debugger");
1359 static int kgdb_cpu_enter(struct kgdb_state
*ks
, struct pt_regs
*regs
)
1361 unsigned long flags
;
1362 int sstep_tries
= 100;
1368 * Interrupts will be restored by the 'trap return' code, except when
1371 local_irq_save(flags
);
1374 kgdb_info
[cpu
].debuggerinfo
= regs
;
1375 kgdb_info
[cpu
].task
= current
;
1377 * Make sure the above info reaches the primary CPU before
1378 * our cpu_in_kgdb[] flag setting does:
1380 atomic_inc(&cpu_in_kgdb
[cpu
]);
1383 * CPU will loop if it is a slave or request to become a kgdb
1384 * master cpu and acquire the kgdb_active lock:
1387 if (kgdb_info
[cpu
].exception_state
& DCPU_WANT_MASTER
) {
1388 if (atomic_cmpxchg(&kgdb_active
, -1, cpu
) == cpu
)
1390 } else if (kgdb_info
[cpu
].exception_state
& DCPU_IS_SLAVE
) {
1391 if (!atomic_read(&passive_cpu_wait
[cpu
]))
1395 /* Return to normal operation by executing any
1396 * hw breakpoint fixup.
1398 if (arch_kgdb_ops
.correct_hw_break
)
1399 arch_kgdb_ops
.correct_hw_break();
1402 atomic_dec(&cpu_in_kgdb
[cpu
]);
1403 touch_softlockup_watchdog_sync();
1404 clocksource_touch_watchdog();
1405 local_irq_restore(flags
);
1412 * For single stepping, try to only enter on the processor
1413 * that was single stepping. To gaurd against a deadlock, the
1414 * kernel will only try for the value of sstep_tries before
1415 * giving up and continuing on.
1417 if (atomic_read(&kgdb_cpu_doing_single_step
) != -1 &&
1418 (kgdb_info
[cpu
].task
&&
1419 kgdb_info
[cpu
].task
->pid
!= kgdb_sstep_pid
) && --sstep_tries
) {
1420 atomic_set(&kgdb_active
, -1);
1421 touch_softlockup_watchdog_sync();
1422 clocksource_touch_watchdog();
1423 local_irq_restore(flags
);
1428 if (!kgdb_io_ready(1)) {
1430 goto kgdb_restore
; /* No I/O connection, so resume the system */
1434 * Don't enter if we have hit a removed breakpoint.
1436 if (kgdb_skipexception(ks
->ex_vector
, ks
->linux_regs
))
1439 /* Call the I/O driver's pre_exception routine */
1440 if (kgdb_io_ops
->pre_exception
)
1441 kgdb_io_ops
->pre_exception();
1443 kgdb_disable_hw_debug(ks
->linux_regs
);
1446 * Get the passive CPU lock which will hold all the non-primary
1447 * CPU in a spin state while the debugger is active
1449 if (!kgdb_single_step
) {
1450 for (i
= 0; i
< NR_CPUS
; i
++)
1451 atomic_inc(&passive_cpu_wait
[i
]);
1455 /* Signal the other CPUs to enter kgdb_wait() */
1456 if ((!kgdb_single_step
) && kgdb_do_roundup
)
1457 kgdb_roundup_cpus(flags
);
1461 * Wait for the other CPUs to be notified and be waiting for us:
1463 for_each_online_cpu(i
) {
1464 while (!atomic_read(&cpu_in_kgdb
[i
]))
1469 * At this point the primary processor is completely
1470 * in the debugger and all secondary CPUs are quiescent
1472 kgdb_post_primary_code(ks
->linux_regs
, ks
->ex_vector
, ks
->err_code
);
1473 kgdb_deactivate_sw_breakpoints();
1474 kgdb_single_step
= 0;
1475 kgdb_contthread
= current
;
1476 exception_level
= 0;
1477 trace_on
= tracing_is_on();
1481 /* Talk to debugger with gdbserial protocol */
1482 error
= gdb_serial_stub(ks
);
1484 /* Call the I/O driver's post_exception routine */
1485 if (kgdb_io_ops
->post_exception
)
1486 kgdb_io_ops
->post_exception();
1488 atomic_dec(&cpu_in_kgdb
[ks
->cpu
]);
1490 if (!kgdb_single_step
) {
1491 for (i
= NR_CPUS
-1; i
>= 0; i
--)
1492 atomic_dec(&passive_cpu_wait
[i
]);
1494 * Wait till all the CPUs have quit
1495 * from the debugger.
1497 for_each_online_cpu(i
) {
1498 while (atomic_read(&cpu_in_kgdb
[i
]))
1504 if (atomic_read(&kgdb_cpu_doing_single_step
) != -1) {
1505 int sstep_cpu
= atomic_read(&kgdb_cpu_doing_single_step
);
1506 if (kgdb_info
[sstep_cpu
].task
)
1507 kgdb_sstep_pid
= kgdb_info
[sstep_cpu
].task
->pid
;
1513 /* Free kgdb_active */
1514 atomic_set(&kgdb_active
, -1);
1515 touch_softlockup_watchdog_sync();
1516 clocksource_touch_watchdog();
1517 local_irq_restore(flags
);
1523 * kgdb_handle_exception() - main entry point from a kernel exception
1525 * Locking hierarchy:
1526 * interface locks, if any (begin_session)
1527 * kgdb lock (kgdb_active)
1530 kgdb_handle_exception(int evector
, int signo
, int ecode
, struct pt_regs
*regs
)
1532 struct kgdb_state kgdb_var
;
1533 struct kgdb_state
*ks
= &kgdb_var
;
1536 ks
->cpu
= raw_smp_processor_id();
1537 ks
->ex_vector
= evector
;
1539 ks
->ex_vector
= evector
;
1540 ks
->err_code
= ecode
;
1541 ks
->kgdb_usethreadid
= 0;
1542 ks
->linux_regs
= regs
;
1544 if (kgdb_reenter_check(ks
))
1545 return 0; /* Ouch, double exception ! */
1546 kgdb_info
[ks
->cpu
].exception_state
|= DCPU_WANT_MASTER
;
1547 ret
= kgdb_cpu_enter(ks
, regs
);
1548 kgdb_info
[ks
->cpu
].exception_state
&= ~DCPU_WANT_MASTER
;
1552 int kgdb_nmicallback(int cpu
, void *regs
)
1555 struct kgdb_state kgdb_var
;
1556 struct kgdb_state
*ks
= &kgdb_var
;
1558 memset(ks
, 0, sizeof(struct kgdb_state
));
1560 ks
->linux_regs
= regs
;
1562 if (!atomic_read(&cpu_in_kgdb
[cpu
]) &&
1563 atomic_read(&kgdb_active
) != -1 &&
1564 atomic_read(&kgdb_active
) != cpu
) {
1565 kgdb_info
[cpu
].exception_state
|= DCPU_IS_SLAVE
;
1566 kgdb_cpu_enter(ks
, regs
);
1567 kgdb_info
[cpu
].exception_state
&= ~DCPU_IS_SLAVE
;
1574 static void kgdb_console_write(struct console
*co
, const char *s
,
1577 unsigned long flags
;
1579 /* If we're debugging, or KGDB has not connected, don't try
1581 if (!kgdb_connected
|| atomic_read(&kgdb_active
) != -1)
1584 local_irq_save(flags
);
1585 kgdb_msg_write(s
, count
);
1586 local_irq_restore(flags
);
1589 static struct console kgdbcons
= {
1591 .write
= kgdb_console_write
,
1592 .flags
= CON_PRINTBUFFER
| CON_ENABLED
,
1596 #ifdef CONFIG_MAGIC_SYSRQ
1597 static void sysrq_handle_gdb(int key
, struct tty_struct
*tty
)
1600 printk(KERN_CRIT
"ERROR: No KGDB I/O module available\n");
1603 if (!kgdb_connected
)
1604 printk(KERN_CRIT
"Entering KGDB\n");
1609 static struct sysrq_key_op sysrq_gdb_op
= {
1610 .handler
= sysrq_handle_gdb
,
1611 .help_msg
= "debug(G)",
1612 .action_msg
= "DEBUG",
1616 static void kgdb_register_callbacks(void)
1618 if (!kgdb_io_module_registered
) {
1619 kgdb_io_module_registered
= 1;
1621 #ifdef CONFIG_MAGIC_SYSRQ
1622 register_sysrq_key('g', &sysrq_gdb_op
);
1624 if (kgdb_use_con
&& !kgdb_con_registered
) {
1625 register_console(&kgdbcons
);
1626 kgdb_con_registered
= 1;
1631 static void kgdb_unregister_callbacks(void)
1634 * When this routine is called KGDB should unregister from the
1635 * panic handler and clean up, making sure it is not handling any
1636 * break exceptions at the time.
1638 if (kgdb_io_module_registered
) {
1639 kgdb_io_module_registered
= 0;
1641 #ifdef CONFIG_MAGIC_SYSRQ
1642 unregister_sysrq_key('g', &sysrq_gdb_op
);
1644 if (kgdb_con_registered
) {
1645 unregister_console(&kgdbcons
);
1646 kgdb_con_registered
= 0;
1651 static void kgdb_initial_breakpoint(void)
1653 kgdb_break_asap
= 0;
1655 printk(KERN_CRIT
"kgdb: Waiting for connection from remote gdb...\n");
1660 * kgdb_register_io_module - register KGDB IO module
1661 * @new_kgdb_io_ops: the io ops vector
1663 * Register it with the KGDB core.
1665 int kgdb_register_io_module(struct kgdb_io
*new_kgdb_io_ops
)
1669 spin_lock(&kgdb_registration_lock
);
1672 spin_unlock(&kgdb_registration_lock
);
1674 printk(KERN_ERR
"kgdb: Another I/O driver is already "
1675 "registered with KGDB.\n");
1679 if (new_kgdb_io_ops
->init
) {
1680 err
= new_kgdb_io_ops
->init();
1682 spin_unlock(&kgdb_registration_lock
);
1687 kgdb_io_ops
= new_kgdb_io_ops
;
1689 spin_unlock(&kgdb_registration_lock
);
1691 printk(KERN_INFO
"kgdb: Registered I/O driver %s.\n",
1692 new_kgdb_io_ops
->name
);
1695 kgdb_register_callbacks();
1697 if (kgdb_break_asap
)
1698 kgdb_initial_breakpoint();
1702 EXPORT_SYMBOL_GPL(kgdb_register_io_module
);
1705 * kkgdb_unregister_io_module - unregister KGDB IO module
1706 * @old_kgdb_io_ops: the io ops vector
1708 * Unregister it with the KGDB core.
1710 void kgdb_unregister_io_module(struct kgdb_io
*old_kgdb_io_ops
)
1712 BUG_ON(kgdb_connected
);
1715 * KGDB is no longer able to communicate out, so
1716 * unregister our callbacks and reset state.
1718 kgdb_unregister_callbacks();
1720 spin_lock(&kgdb_registration_lock
);
1722 WARN_ON_ONCE(kgdb_io_ops
!= old_kgdb_io_ops
);
1725 spin_unlock(&kgdb_registration_lock
);
1728 "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
1729 old_kgdb_io_ops
->name
);
1731 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module
);
1734 * kgdb_breakpoint - generate breakpoint exception
1736 * This function will generate a breakpoint exception. It is used at the
1737 * beginning of a program to sync up with a debugger and can be used
1738 * otherwise as a quick means to stop program execution and "break" into
1741 void kgdb_breakpoint(void)
1743 atomic_inc(&kgdb_setting_breakpoint
);
1744 wmb(); /* Sync point before breakpoint */
1745 arch_kgdb_breakpoint();
1746 wmb(); /* Sync point after breakpoint */
1747 atomic_dec(&kgdb_setting_breakpoint
);
1749 EXPORT_SYMBOL_GPL(kgdb_breakpoint
);
1751 static int __init
opt_kgdb_wait(char *str
)
1753 kgdb_break_asap
= 1;
1755 if (kgdb_io_module_registered
)
1756 kgdb_initial_breakpoint();
1761 early_param("kgdbwait", opt_kgdb_wait
);