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>
56 static int kgdb_break_asap
;
64 unsigned long threadid
;
65 long kgdb_usethreadid
;
66 struct pt_regs
*linux_regs
;
69 static struct debuggerinfo_struct
{
71 struct task_struct
*task
;
75 * kgdb_connected - Is a host GDB connected to us?
78 EXPORT_SYMBOL_GPL(kgdb_connected
);
80 /* All the KGDB handlers are installed */
81 static int kgdb_io_module_registered
;
83 /* Guard for recursive entry */
84 static int exception_level
;
86 static struct kgdb_io
*kgdb_io_ops
;
87 static DEFINE_SPINLOCK(kgdb_registration_lock
);
89 /* kgdb console driver is loaded */
90 static int kgdb_con_registered
;
91 /* determine if kgdb console output should be used */
92 static int kgdb_use_con
;
94 static int __init
opt_kgdb_con(char *str
)
100 early_param("kgdbcon", opt_kgdb_con
);
102 module_param(kgdb_use_con
, int, 0644);
105 * Holds information about breakpoints in a kernel. These breakpoints are
106 * added and removed by gdb.
108 static struct kgdb_bkpt kgdb_break
[KGDB_MAX_BREAKPOINTS
] = {
109 [0 ... KGDB_MAX_BREAKPOINTS
-1] = { .state
= BP_UNDEFINED
}
113 * The CPU# of the active CPU, or -1 if none:
115 atomic_t kgdb_active
= ATOMIC_INIT(-1);
118 * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
119 * bootup code (which might not have percpu set up yet):
121 static atomic_t passive_cpu_wait
[NR_CPUS
];
122 static atomic_t cpu_in_kgdb
[NR_CPUS
];
123 atomic_t kgdb_setting_breakpoint
;
125 struct task_struct
*kgdb_usethread
;
126 struct task_struct
*kgdb_contthread
;
128 int kgdb_single_step
;
130 /* Our I/O buffers. */
131 static char remcom_in_buffer
[BUFMAX
];
132 static char remcom_out_buffer
[BUFMAX
];
134 /* Storage for the registers, in GDB format. */
135 static unsigned long gdb_regs
[(NUMREGBYTES
+
136 sizeof(unsigned long) - 1) /
137 sizeof(unsigned long)];
139 /* to keep track of the CPU which is doing the single stepping*/
140 atomic_t kgdb_cpu_doing_single_step
= ATOMIC_INIT(-1);
143 * If you are debugging a problem where roundup (the collection of
144 * all other CPUs) is a problem [this should be extremely rare],
145 * then use the nokgdbroundup option to avoid roundup. In that case
146 * the other CPUs might interfere with your debugging context, so
147 * use this with care:
149 static int kgdb_do_roundup
= 1;
151 static int __init
opt_nokgdbroundup(char *str
)
158 early_param("nokgdbroundup", opt_nokgdbroundup
);
161 * Finally, some KGDB code :-)
165 * Weak aliases for breakpoint management,
166 * can be overriden by architectures when needed:
168 int __weak
kgdb_validate_break_address(unsigned long addr
)
170 char tmp_variable
[BREAK_INSTR_SIZE
];
172 return probe_kernel_read(tmp_variable
, (char *)addr
, BREAK_INSTR_SIZE
);
175 int __weak
kgdb_arch_set_breakpoint(unsigned long addr
, char *saved_instr
)
179 err
= probe_kernel_read(saved_instr
, (char *)addr
, BREAK_INSTR_SIZE
);
183 return probe_kernel_write((char *)addr
, arch_kgdb_ops
.gdb_bpt_instr
,
187 int __weak
kgdb_arch_remove_breakpoint(unsigned long addr
, char *bundle
)
189 return probe_kernel_write((char *)addr
,
190 (char *)bundle
, BREAK_INSTR_SIZE
);
193 unsigned long __weak
kgdb_arch_pc(int exception
, struct pt_regs
*regs
)
195 return instruction_pointer(regs
);
198 int __weak
kgdb_arch_init(void)
203 int __weak
kgdb_skipexception(int exception
, struct pt_regs
*regs
)
209 kgdb_post_primary_code(struct pt_regs
*regs
, int e_vector
, int err_code
)
215 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
216 * @regs: Current &struct pt_regs.
218 * This function will be called if the particular architecture must
219 * disable hardware debugging while it is processing gdb packets or
220 * handling exception.
222 void __weak
kgdb_disable_hw_debug(struct pt_regs
*regs
)
227 * GDB remote protocol parser:
230 static const char hexchars
[] = "0123456789abcdef";
232 static int hex(char ch
)
234 if ((ch
>= 'a') && (ch
<= 'f'))
235 return ch
- 'a' + 10;
236 if ((ch
>= '0') && (ch
<= '9'))
238 if ((ch
>= 'A') && (ch
<= 'F'))
239 return ch
- 'A' + 10;
243 /* scan for the sequence $<data>#<checksum> */
244 static void get_packet(char *buffer
)
246 unsigned char checksum
;
247 unsigned char xmitcsum
;
253 * Spin and wait around for the start character, ignore all
256 while ((ch
= (kgdb_io_ops
->read_char())) != '$')
266 * now, read until a # or end of buffer is found:
268 while (count
< (BUFMAX
- 1)) {
269 ch
= kgdb_io_ops
->read_char();
272 checksum
= checksum
+ ch
;
279 xmitcsum
= hex(kgdb_io_ops
->read_char()) << 4;
280 xmitcsum
+= hex(kgdb_io_ops
->read_char());
282 if (checksum
!= xmitcsum
)
283 /* failed checksum */
284 kgdb_io_ops
->write_char('-');
286 /* successful transfer */
287 kgdb_io_ops
->write_char('+');
288 if (kgdb_io_ops
->flush
)
289 kgdb_io_ops
->flush();
291 } while (checksum
!= xmitcsum
);
295 * Send the packet in buffer.
296 * Check for gdb connection if asked for.
298 static void put_packet(char *buffer
)
300 unsigned char checksum
;
305 * $<packet info>#<checksum>.
308 kgdb_io_ops
->write_char('$');
312 while ((ch
= buffer
[count
])) {
313 kgdb_io_ops
->write_char(ch
);
318 kgdb_io_ops
->write_char('#');
319 kgdb_io_ops
->write_char(hexchars
[checksum
>> 4]);
320 kgdb_io_ops
->write_char(hexchars
[checksum
& 0xf]);
321 if (kgdb_io_ops
->flush
)
322 kgdb_io_ops
->flush();
324 /* Now see what we get in reply. */
325 ch
= kgdb_io_ops
->read_char();
328 ch
= kgdb_io_ops
->read_char();
330 /* If we get an ACK, we are done. */
335 * If we get the start of another packet, this means
336 * that GDB is attempting to reconnect. We will NAK
337 * the packet being sent, and stop trying to send this
341 kgdb_io_ops
->write_char('-');
342 if (kgdb_io_ops
->flush
)
343 kgdb_io_ops
->flush();
350 * Convert the memory pointed to by mem into hex, placing result in buf.
351 * Return a pointer to the last char put in buf (null). May return an error.
353 int kgdb_mem2hex(char *mem
, char *buf
, int count
)
359 * We use the upper half of buf as an intermediate buffer for the
360 * raw memory copy. Hex conversion will work against this one.
364 err
= probe_kernel_read(tmp
, mem
, count
);
367 buf
= pack_hex_byte(buf
, *tmp
);
379 * Copy the binary array pointed to by buf into mem. Fix $, #, and
380 * 0x7d escaped with 0x7d. Return a pointer to the character after
381 * the last byte written.
383 static int kgdb_ebin2mem(char *buf
, char *mem
, int count
)
388 while (count
-- > 0) {
393 err
= probe_kernel_write(mem
, &c
, 1);
404 * Convert the hex array pointed to by buf into binary to be placed in mem.
405 * Return a pointer to the character AFTER the last byte written.
406 * May return an error.
408 int kgdb_hex2mem(char *buf
, char *mem
, int count
)
414 * We use the upper half of buf as an intermediate buffer for the
415 * raw memory that is converted from hex.
417 tmp_raw
= buf
+ count
* 2;
419 tmp_hex
= tmp_raw
- 1;
420 while (tmp_hex
>= buf
) {
422 *tmp_raw
= hex(*tmp_hex
--);
423 *tmp_raw
|= hex(*tmp_hex
--) << 4;
426 return probe_kernel_write(mem
, tmp_raw
, count
);
430 * While we find nice hex chars, build a long_val.
431 * Return number of chars processed.
433 int kgdb_hex2long(char **ptr
, unsigned long *long_val
)
441 hex_val
= hex(**ptr
);
445 *long_val
= (*long_val
<< 4) | hex_val
;
453 /* Write memory due to an 'M' or 'X' packet. */
454 static int write_mem_msg(int binary
)
456 char *ptr
= &remcom_in_buffer
[1];
458 unsigned long length
;
461 if (kgdb_hex2long(&ptr
, &addr
) > 0 && *(ptr
++) == ',' &&
462 kgdb_hex2long(&ptr
, &length
) > 0 && *(ptr
++) == ':') {
464 err
= kgdb_ebin2mem(ptr
, (char *)addr
, length
);
466 err
= kgdb_hex2mem(ptr
, (char *)addr
, length
);
469 if (CACHE_FLUSH_IS_SAFE
)
470 flush_icache_range(addr
, addr
+ length
+ 1);
477 static void error_packet(char *pkt
, int error
)
481 pkt
[1] = hexchars
[(error
/ 10)];
482 pkt
[2] = hexchars
[(error
% 10)];
487 * Thread ID accessors. We represent a flat TID space to GDB, where
488 * the per CPU idle threads (which under Linux all have PID 0) are
489 * remapped to negative TIDs.
492 #define BUF_THREAD_ID_SIZE 16
494 static char *pack_threadid(char *pkt
, unsigned char *id
)
498 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
500 pkt
= pack_hex_byte(pkt
, *id
++);
505 static void int_to_threadref(unsigned char *id
, int value
)
510 scan
= (unsigned char *)id
;
513 *scan
++ = (value
>> 24) & 0xff;
514 *scan
++ = (value
>> 16) & 0xff;
515 *scan
++ = (value
>> 8) & 0xff;
516 *scan
++ = (value
& 0xff);
519 static struct task_struct
*getthread(struct pt_regs
*regs
, int tid
)
522 * Non-positive TIDs are remapped idle tasks:
525 return idle_task(-tid
);
528 * find_task_by_pid_ns() does not take the tasklist lock anymore
529 * but is nicely RCU locked - hence is a pretty resilient
532 return find_task_by_pid_ns(tid
, &init_pid_ns
);
536 * CPU debug state control:
540 static void kgdb_wait(struct pt_regs
*regs
)
545 local_irq_save(flags
);
546 cpu
= raw_smp_processor_id();
547 kgdb_info
[cpu
].debuggerinfo
= regs
;
548 kgdb_info
[cpu
].task
= current
;
550 * Make sure the above info reaches the primary CPU before
551 * our cpu_in_kgdb[] flag setting does:
554 atomic_set(&cpu_in_kgdb
[cpu
], 1);
556 /* Wait till primary CPU is done with debugging */
557 while (atomic_read(&passive_cpu_wait
[cpu
]))
560 kgdb_info
[cpu
].debuggerinfo
= NULL
;
561 kgdb_info
[cpu
].task
= NULL
;
563 /* fix up hardware debug registers on local cpu */
564 if (arch_kgdb_ops
.correct_hw_break
)
565 arch_kgdb_ops
.correct_hw_break();
567 /* Signal the primary CPU that we are done: */
568 atomic_set(&cpu_in_kgdb
[cpu
], 0);
569 clocksource_touch_watchdog();
570 local_irq_restore(flags
);
575 * Some architectures need cache flushes when we set/clear a
578 static void kgdb_flush_swbreak_addr(unsigned long addr
)
580 if (!CACHE_FLUSH_IS_SAFE
)
583 if (current
->mm
&& current
->mm
->mmap_cache
) {
584 flush_cache_range(current
->mm
->mmap_cache
,
585 addr
, addr
+ BREAK_INSTR_SIZE
);
587 /* Force flush instruction cache if it was outside the mm */
588 flush_icache_range(addr
, addr
+ BREAK_INSTR_SIZE
);
592 * SW breakpoint management:
594 static int kgdb_activate_sw_breakpoints(void)
600 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
601 if (kgdb_break
[i
].state
!= BP_SET
)
604 addr
= kgdb_break
[i
].bpt_addr
;
605 error
= kgdb_arch_set_breakpoint(addr
,
606 kgdb_break
[i
].saved_instr
);
610 kgdb_flush_swbreak_addr(addr
);
611 kgdb_break
[i
].state
= BP_ACTIVE
;
616 static int kgdb_set_sw_break(unsigned long addr
)
618 int err
= kgdb_validate_break_address(addr
);
625 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
626 if ((kgdb_break
[i
].state
== BP_SET
) &&
627 (kgdb_break
[i
].bpt_addr
== addr
))
630 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
631 if (kgdb_break
[i
].state
== BP_REMOVED
&&
632 kgdb_break
[i
].bpt_addr
== addr
) {
639 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
640 if (kgdb_break
[i
].state
== BP_UNDEFINED
) {
650 kgdb_break
[breakno
].state
= BP_SET
;
651 kgdb_break
[breakno
].type
= BP_BREAKPOINT
;
652 kgdb_break
[breakno
].bpt_addr
= addr
;
657 static int kgdb_deactivate_sw_breakpoints(void)
663 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
664 if (kgdb_break
[i
].state
!= BP_ACTIVE
)
666 addr
= kgdb_break
[i
].bpt_addr
;
667 error
= kgdb_arch_remove_breakpoint(addr
,
668 kgdb_break
[i
].saved_instr
);
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, idle tasks to -1 ... -NR_CPUs:
734 static inline int shadow_pid(int realpid
)
739 return -1-raw_smp_processor_id();
742 static char gdbmsgbuf
[BUFMAX
+ 1];
744 static void kgdb_msg_write(const char *s
, int len
)
753 /* Fill and send buffers... */
755 bufptr
= gdbmsgbuf
+ 1;
757 /* Calculate how many this time */
758 if ((len
<< 1) > (BUFMAX
- 2))
759 wcount
= (BUFMAX
- 2) >> 1;
763 /* Pack in hex chars */
764 for (i
= 0; i
< wcount
; i
++)
765 bufptr
= pack_hex_byte(bufptr
, s
[i
]);
773 put_packet(gdbmsgbuf
);
778 * Return true if there is a valid kgdb I/O module. Also if no
779 * debugger is attached a message can be printed to the console about
780 * waiting for the debugger to attach.
782 * The print_wait argument is only to be true when called from inside
783 * the core kgdb_handle_exception, because it will wait for the
784 * debugger to attach.
786 static int kgdb_io_ready(int print_wait
)
792 if (atomic_read(&kgdb_setting_breakpoint
))
795 printk(KERN_CRIT
"KGDB: Waiting for remote debugger\n");
800 * All the functions that start with gdb_cmd are the various
801 * operations to implement the handlers for the gdbserial protocol
802 * where KGDB is communicating with an external debugger
805 /* Handle the '?' status packets */
806 static void gdb_cmd_status(struct kgdb_state
*ks
)
809 * We know that this packet is only sent
810 * during initial connect. So to be safe,
811 * we clear out our breakpoints now in case
812 * GDB is reconnecting.
816 remcom_out_buffer
[0] = 'S';
817 pack_hex_byte(&remcom_out_buffer
[1], ks
->signo
);
820 /* Handle the 'g' get registers request */
821 static void gdb_cmd_getregs(struct kgdb_state
*ks
)
823 struct task_struct
*thread
;
824 void *local_debuggerinfo
;
827 thread
= kgdb_usethread
;
829 thread
= kgdb_info
[ks
->cpu
].task
;
830 local_debuggerinfo
= kgdb_info
[ks
->cpu
].debuggerinfo
;
832 local_debuggerinfo
= NULL
;
833 for (i
= 0; i
< NR_CPUS
; i
++) {
835 * Try to find the task on some other
836 * or possibly this node if we do not
837 * find the matching task then we try
838 * to approximate the results.
840 if (thread
== kgdb_info
[i
].task
)
841 local_debuggerinfo
= kgdb_info
[i
].debuggerinfo
;
846 * All threads that don't have debuggerinfo should be
847 * in __schedule() sleeping, since all other CPUs
848 * are in kgdb_wait, and thus have debuggerinfo.
850 if (local_debuggerinfo
) {
851 pt_regs_to_gdb_regs(gdb_regs
, local_debuggerinfo
);
854 * Pull stuff saved during switch_to; nothing
855 * else is accessible (or even particularly
858 * This should be enough for a stack trace.
860 sleeping_thread_to_gdb_regs(gdb_regs
, thread
);
862 kgdb_mem2hex((char *)gdb_regs
, remcom_out_buffer
, NUMREGBYTES
);
865 /* Handle the 'G' set registers request */
866 static void gdb_cmd_setregs(struct kgdb_state
*ks
)
868 kgdb_hex2mem(&remcom_in_buffer
[1], (char *)gdb_regs
, NUMREGBYTES
);
870 if (kgdb_usethread
&& kgdb_usethread
!= current
) {
871 error_packet(remcom_out_buffer
, -EINVAL
);
873 gdb_regs_to_pt_regs(gdb_regs
, ks
->linux_regs
);
874 strcpy(remcom_out_buffer
, "OK");
878 /* Handle the 'm' memory read bytes */
879 static void gdb_cmd_memread(struct kgdb_state
*ks
)
881 char *ptr
= &remcom_in_buffer
[1];
882 unsigned long length
;
886 if (kgdb_hex2long(&ptr
, &addr
) > 0 && *ptr
++ == ',' &&
887 kgdb_hex2long(&ptr
, &length
) > 0) {
888 err
= kgdb_mem2hex((char *)addr
, remcom_out_buffer
, length
);
890 error_packet(remcom_out_buffer
, err
);
892 error_packet(remcom_out_buffer
, -EINVAL
);
896 /* Handle the 'M' memory write bytes */
897 static void gdb_cmd_memwrite(struct kgdb_state
*ks
)
899 int err
= write_mem_msg(0);
902 error_packet(remcom_out_buffer
, err
);
904 strcpy(remcom_out_buffer
, "OK");
907 /* Handle the 'X' memory binary write bytes */
908 static void gdb_cmd_binwrite(struct kgdb_state
*ks
)
910 int err
= write_mem_msg(1);
913 error_packet(remcom_out_buffer
, err
);
915 strcpy(remcom_out_buffer
, "OK");
918 /* Handle the 'D' or 'k', detach or kill packets */
919 static void gdb_cmd_detachkill(struct kgdb_state
*ks
)
923 /* The detach case */
924 if (remcom_in_buffer
[0] == 'D') {
925 error
= remove_all_break();
927 error_packet(remcom_out_buffer
, error
);
929 strcpy(remcom_out_buffer
, "OK");
932 put_packet(remcom_out_buffer
);
935 * Assume the kill case, with no exit code checking,
936 * trying to force detach the debugger:
943 /* Handle the 'R' reboot packets */
944 static int gdb_cmd_reboot(struct kgdb_state
*ks
)
946 /* For now, only honor R0 */
947 if (strcmp(remcom_in_buffer
, "R0") == 0) {
948 printk(KERN_CRIT
"Executing emergency reboot\n");
949 strcpy(remcom_out_buffer
, "OK");
950 put_packet(remcom_out_buffer
);
953 * Execution should not return from
954 * machine_emergency_restart()
956 machine_emergency_restart();
964 /* Handle the 'q' query packets */
965 static void gdb_cmd_query(struct kgdb_state
*ks
)
967 struct task_struct
*thread
;
968 unsigned char thref
[8];
972 switch (remcom_in_buffer
[1]) {
975 if (memcmp(remcom_in_buffer
+ 2, "ThreadInfo", 10)) {
976 error_packet(remcom_out_buffer
, -EINVAL
);
980 if (remcom_in_buffer
[1] == 'f')
983 remcom_out_buffer
[0] = 'm';
984 ptr
= remcom_out_buffer
+ 1;
986 for (i
= 0; i
< 17; ks
->threadid
++) {
987 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
989 int_to_threadref(thref
, ks
->threadid
);
990 pack_threadid(ptr
, thref
);
991 ptr
+= BUF_THREAD_ID_SIZE
;
1000 /* Current thread id */
1001 strcpy(remcom_out_buffer
, "QC");
1002 ks
->threadid
= shadow_pid(current
->pid
);
1003 int_to_threadref(thref
, ks
->threadid
);
1004 pack_threadid(remcom_out_buffer
+ 2, thref
);
1007 if (memcmp(remcom_in_buffer
+ 1, "ThreadExtraInfo,", 16)) {
1008 error_packet(remcom_out_buffer
, -EINVAL
);
1012 ptr
= remcom_in_buffer
+ 17;
1013 kgdb_hex2long(&ptr
, &ks
->threadid
);
1014 if (!getthread(ks
->linux_regs
, ks
->threadid
)) {
1015 error_packet(remcom_out_buffer
, -EINVAL
);
1018 if (ks
->threadid
> 0) {
1019 kgdb_mem2hex(getthread(ks
->linux_regs
,
1020 ks
->threadid
)->comm
,
1021 remcom_out_buffer
, 16);
1023 static char tmpstr
[23 + BUF_THREAD_ID_SIZE
];
1025 sprintf(tmpstr
, "Shadow task %d for pid 0",
1026 (int)(-ks
->threadid
-1));
1027 kgdb_mem2hex(tmpstr
, remcom_out_buffer
, strlen(tmpstr
));
1033 /* Handle the 'H' task query packets */
1034 static void gdb_cmd_task(struct kgdb_state
*ks
)
1036 struct task_struct
*thread
;
1039 switch (remcom_in_buffer
[1]) {
1041 ptr
= &remcom_in_buffer
[2];
1042 kgdb_hex2long(&ptr
, &ks
->threadid
);
1043 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
1044 if (!thread
&& ks
->threadid
> 0) {
1045 error_packet(remcom_out_buffer
, -EINVAL
);
1048 kgdb_usethread
= thread
;
1049 ks
->kgdb_usethreadid
= ks
->threadid
;
1050 strcpy(remcom_out_buffer
, "OK");
1053 ptr
= &remcom_in_buffer
[2];
1054 kgdb_hex2long(&ptr
, &ks
->threadid
);
1055 if (!ks
->threadid
) {
1056 kgdb_contthread
= NULL
;
1058 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
1059 if (!thread
&& ks
->threadid
> 0) {
1060 error_packet(remcom_out_buffer
, -EINVAL
);
1063 kgdb_contthread
= thread
;
1065 strcpy(remcom_out_buffer
, "OK");
1070 /* Handle the 'T' thread query packets */
1071 static void gdb_cmd_thread(struct kgdb_state
*ks
)
1073 char *ptr
= &remcom_in_buffer
[1];
1074 struct task_struct
*thread
;
1076 kgdb_hex2long(&ptr
, &ks
->threadid
);
1077 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
1079 strcpy(remcom_out_buffer
, "OK");
1081 error_packet(remcom_out_buffer
, -EINVAL
);
1084 /* Handle the 'z' or 'Z' breakpoint remove or set packets */
1085 static void gdb_cmd_break(struct kgdb_state
*ks
)
1088 * Since GDB-5.3, it's been drafted that '0' is a software
1089 * breakpoint, '1' is a hardware breakpoint, so let's do that.
1091 char *bpt_type
= &remcom_in_buffer
[1];
1092 char *ptr
= &remcom_in_buffer
[2];
1094 unsigned long length
;
1097 if (arch_kgdb_ops
.set_hw_breakpoint
&& *bpt_type
>= '1') {
1099 if (*bpt_type
> '4')
1102 if (*bpt_type
!= '0' && *bpt_type
!= '1')
1108 * Test if this is a hardware breakpoint, and
1111 if (*bpt_type
== '1' && !(arch_kgdb_ops
.flags
& KGDB_HW_BREAKPOINT
))
1115 if (*(ptr
++) != ',') {
1116 error_packet(remcom_out_buffer
, -EINVAL
);
1119 if (!kgdb_hex2long(&ptr
, &addr
)) {
1120 error_packet(remcom_out_buffer
, -EINVAL
);
1123 if (*(ptr
++) != ',' ||
1124 !kgdb_hex2long(&ptr
, &length
)) {
1125 error_packet(remcom_out_buffer
, -EINVAL
);
1129 if (remcom_in_buffer
[0] == 'Z' && *bpt_type
== '0')
1130 error
= kgdb_set_sw_break(addr
);
1131 else if (remcom_in_buffer
[0] == 'z' && *bpt_type
== '0')
1132 error
= kgdb_remove_sw_break(addr
);
1133 else if (remcom_in_buffer
[0] == 'Z')
1134 error
= arch_kgdb_ops
.set_hw_breakpoint(addr
,
1135 (int)length
, *bpt_type
- '0');
1136 else if (remcom_in_buffer
[0] == 'z')
1137 error
= arch_kgdb_ops
.remove_hw_breakpoint(addr
,
1138 (int) length
, *bpt_type
- '0');
1141 strcpy(remcom_out_buffer
, "OK");
1143 error_packet(remcom_out_buffer
, error
);
1146 /* Handle the 'C' signal / exception passing packets */
1147 static int gdb_cmd_exception_pass(struct kgdb_state
*ks
)
1149 /* C09 == pass exception
1150 * C15 == detach kgdb, pass exception
1152 if (remcom_in_buffer
[1] == '0' && remcom_in_buffer
[2] == '9') {
1154 ks
->pass_exception
= 1;
1155 remcom_in_buffer
[0] = 'c';
1157 } else if (remcom_in_buffer
[1] == '1' && remcom_in_buffer
[2] == '5') {
1159 ks
->pass_exception
= 1;
1160 remcom_in_buffer
[0] = 'D';
1166 error_packet(remcom_out_buffer
, -EINVAL
);
1170 /* Indicate fall through */
1175 * This function performs all gdbserial command procesing
1177 static int gdb_serial_stub(struct kgdb_state
*ks
)
1182 /* Clear the out buffer. */
1183 memset(remcom_out_buffer
, 0, sizeof(remcom_out_buffer
));
1185 if (kgdb_connected
) {
1186 unsigned char thref
[8];
1189 /* Reply to host that an exception has occurred */
1190 ptr
= remcom_out_buffer
;
1192 ptr
= pack_hex_byte(ptr
, ks
->signo
);
1193 ptr
+= strlen(strcpy(ptr
, "thread:"));
1194 int_to_threadref(thref
, shadow_pid(current
->pid
));
1195 ptr
= pack_threadid(ptr
, thref
);
1197 put_packet(remcom_out_buffer
);
1200 kgdb_usethread
= kgdb_info
[ks
->cpu
].task
;
1201 ks
->kgdb_usethreadid
= shadow_pid(kgdb_info
[ks
->cpu
].task
->pid
);
1202 ks
->pass_exception
= 0;
1207 /* Clear the out buffer. */
1208 memset(remcom_out_buffer
, 0, sizeof(remcom_out_buffer
));
1210 get_packet(remcom_in_buffer
);
1212 switch (remcom_in_buffer
[0]) {
1213 case '?': /* gdbserial status */
1216 case 'g': /* return the value of the CPU registers */
1217 gdb_cmd_getregs(ks
);
1219 case 'G': /* set the value of the CPU registers - return OK */
1220 gdb_cmd_setregs(ks
);
1222 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1223 gdb_cmd_memread(ks
);
1225 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1226 gdb_cmd_memwrite(ks
);
1228 case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1229 gdb_cmd_binwrite(ks
);
1231 /* kill or detach. KGDB should treat this like a
1234 case 'D': /* Debugger detach */
1235 case 'k': /* Debugger detach via kill */
1236 gdb_cmd_detachkill(ks
);
1237 goto default_handle
;
1238 case 'R': /* Reboot */
1239 if (gdb_cmd_reboot(ks
))
1240 goto default_handle
;
1242 case 'q': /* query command */
1245 case 'H': /* task related */
1248 case 'T': /* Query thread status */
1251 case 'z': /* Break point remove */
1252 case 'Z': /* Break point set */
1255 case 'C': /* Exception passing */
1256 tmp
= gdb_cmd_exception_pass(ks
);
1258 goto default_handle
;
1261 /* Fall through on tmp < 0 */
1262 case 'c': /* Continue packet */
1263 case 's': /* Single step packet */
1264 if (kgdb_contthread
&& kgdb_contthread
!= current
) {
1265 /* Can't switch threads in kgdb */
1266 error_packet(remcom_out_buffer
, -EINVAL
);
1269 kgdb_activate_sw_breakpoints();
1270 /* Fall through to default processing */
1273 error
= kgdb_arch_handle_exception(ks
->ex_vector
,
1280 * Leave cmd processing on error, detach,
1281 * kill, continue, or single step.
1283 if (error
>= 0 || remcom_in_buffer
[0] == 'D' ||
1284 remcom_in_buffer
[0] == 'k') {
1291 /* reply to the request */
1292 put_packet(remcom_out_buffer
);
1296 if (ks
->pass_exception
)
1301 static int kgdb_reenter_check(struct kgdb_state
*ks
)
1305 if (atomic_read(&kgdb_active
) != raw_smp_processor_id())
1308 /* Panic on recursive debugger calls: */
1310 addr
= kgdb_arch_pc(ks
->ex_vector
, ks
->linux_regs
);
1311 kgdb_deactivate_sw_breakpoints();
1314 * If the break point removed ok at the place exception
1315 * occurred, try to recover and print a warning to the end
1316 * user because the user planted a breakpoint in a place that
1317 * KGDB needs in order to function.
1319 if (kgdb_remove_sw_break(addr
) == 0) {
1320 exception_level
= 0;
1321 kgdb_skipexception(ks
->ex_vector
, ks
->linux_regs
);
1322 kgdb_activate_sw_breakpoints();
1323 printk(KERN_CRIT
"KGDB: re-enter error: breakpoint removed %lx\n",
1330 kgdb_skipexception(ks
->ex_vector
, ks
->linux_regs
);
1332 if (exception_level
> 1) {
1334 panic("Recursive entry to debugger");
1337 printk(KERN_CRIT
"KGDB: re-enter exception: ALL breakpoints killed\n");
1339 panic("Recursive entry to debugger");
1345 * kgdb_handle_exception() - main entry point from a kernel exception
1347 * Locking hierarchy:
1348 * interface locks, if any (begin_session)
1349 * kgdb lock (kgdb_active)
1352 kgdb_handle_exception(int evector
, int signo
, int ecode
, struct pt_regs
*regs
)
1354 struct kgdb_state kgdb_var
;
1355 struct kgdb_state
*ks
= &kgdb_var
;
1356 unsigned long flags
;
1360 ks
->cpu
= raw_smp_processor_id();
1361 ks
->ex_vector
= evector
;
1363 ks
->ex_vector
= evector
;
1364 ks
->err_code
= ecode
;
1365 ks
->kgdb_usethreadid
= 0;
1366 ks
->linux_regs
= regs
;
1368 if (kgdb_reenter_check(ks
))
1369 return 0; /* Ouch, double exception ! */
1373 * Interrupts will be restored by the 'trap return' code, except when
1376 local_irq_save(flags
);
1378 cpu
= raw_smp_processor_id();
1381 * Acquire the kgdb_active lock:
1383 while (atomic_cmpxchg(&kgdb_active
, -1, cpu
) != -1)
1387 * Do not start the debugger connection on this CPU if the last
1388 * instance of the exception handler wanted to come into the
1389 * debugger on a different CPU via a single step
1391 if (atomic_read(&kgdb_cpu_doing_single_step
) != -1 &&
1392 atomic_read(&kgdb_cpu_doing_single_step
) != cpu
) {
1394 atomic_set(&kgdb_active
, -1);
1395 clocksource_touch_watchdog();
1396 local_irq_restore(flags
);
1401 if (!kgdb_io_ready(1)) {
1403 goto kgdb_restore
; /* No I/O connection, so resume the system */
1407 * Don't enter if we have hit a removed breakpoint.
1409 if (kgdb_skipexception(ks
->ex_vector
, ks
->linux_regs
))
1412 /* Call the I/O driver's pre_exception routine */
1413 if (kgdb_io_ops
->pre_exception
)
1414 kgdb_io_ops
->pre_exception();
1416 kgdb_info
[ks
->cpu
].debuggerinfo
= ks
->linux_regs
;
1417 kgdb_info
[ks
->cpu
].task
= current
;
1419 kgdb_disable_hw_debug(ks
->linux_regs
);
1422 * Get the passive CPU lock which will hold all the non-primary
1423 * CPU in a spin state while the debugger is active
1425 if (!kgdb_single_step
|| !kgdb_contthread
) {
1426 for (i
= 0; i
< NR_CPUS
; i
++)
1427 atomic_set(&passive_cpu_wait
[i
], 1);
1431 * spin_lock code is good enough as a barrier so we don't
1434 atomic_set(&cpu_in_kgdb
[ks
->cpu
], 1);
1437 /* Signal the other CPUs to enter kgdb_wait() */
1438 if ((!kgdb_single_step
|| !kgdb_contthread
) && kgdb_do_roundup
)
1439 kgdb_roundup_cpus(flags
);
1443 * Wait for the other CPUs to be notified and be waiting for us:
1445 for_each_online_cpu(i
) {
1446 while (!atomic_read(&cpu_in_kgdb
[i
]))
1451 * At this point the primary processor is completely
1452 * in the debugger and all secondary CPUs are quiescent
1454 kgdb_post_primary_code(ks
->linux_regs
, ks
->ex_vector
, ks
->err_code
);
1455 kgdb_deactivate_sw_breakpoints();
1456 kgdb_single_step
= 0;
1457 kgdb_contthread
= NULL
;
1458 exception_level
= 0;
1460 /* Talk to debugger with gdbserial protocol */
1461 error
= gdb_serial_stub(ks
);
1463 /* Call the I/O driver's post_exception routine */
1464 if (kgdb_io_ops
->post_exception
)
1465 kgdb_io_ops
->post_exception();
1467 kgdb_info
[ks
->cpu
].debuggerinfo
= NULL
;
1468 kgdb_info
[ks
->cpu
].task
= NULL
;
1469 atomic_set(&cpu_in_kgdb
[ks
->cpu
], 0);
1471 if (!kgdb_single_step
|| !kgdb_contthread
) {
1472 for (i
= NR_CPUS
-1; i
>= 0; i
--)
1473 atomic_set(&passive_cpu_wait
[i
], 0);
1475 * Wait till all the CPUs have quit
1476 * from the debugger.
1478 for_each_online_cpu(i
) {
1479 while (atomic_read(&cpu_in_kgdb
[i
]))
1485 /* Free kgdb_active */
1486 atomic_set(&kgdb_active
, -1);
1487 clocksource_touch_watchdog();
1488 local_irq_restore(flags
);
1493 int kgdb_nmicallback(int cpu
, void *regs
)
1496 if (!atomic_read(&cpu_in_kgdb
[cpu
]) &&
1497 atomic_read(&kgdb_active
) != cpu
&&
1498 atomic_read(&cpu_in_kgdb
[atomic_read(&kgdb_active
)])) {
1499 kgdb_wait((struct pt_regs
*)regs
);
1506 void kgdb_console_write(struct console
*co
, const char *s
, unsigned count
)
1508 unsigned long flags
;
1510 /* If we're debugging, or KGDB has not connected, don't try
1512 if (!kgdb_connected
|| atomic_read(&kgdb_active
) != -1)
1515 local_irq_save(flags
);
1516 kgdb_msg_write(s
, count
);
1517 local_irq_restore(flags
);
1520 static struct console kgdbcons
= {
1522 .write
= kgdb_console_write
,
1523 .flags
= CON_PRINTBUFFER
| CON_ENABLED
,
1527 #ifdef CONFIG_MAGIC_SYSRQ
1528 static void sysrq_handle_gdb(int key
, struct tty_struct
*tty
)
1531 printk(KERN_CRIT
"ERROR: No KGDB I/O module available\n");
1534 if (!kgdb_connected
)
1535 printk(KERN_CRIT
"Entering KGDB\n");
1540 static struct sysrq_key_op sysrq_gdb_op
= {
1541 .handler
= sysrq_handle_gdb
,
1543 .action_msg
= "GDB",
1547 static void kgdb_register_callbacks(void)
1549 if (!kgdb_io_module_registered
) {
1550 kgdb_io_module_registered
= 1;
1552 #ifdef CONFIG_MAGIC_SYSRQ
1553 register_sysrq_key('g', &sysrq_gdb_op
);
1555 if (kgdb_use_con
&& !kgdb_con_registered
) {
1556 register_console(&kgdbcons
);
1557 kgdb_con_registered
= 1;
1562 static void kgdb_unregister_callbacks(void)
1565 * When this routine is called KGDB should unregister from the
1566 * panic handler and clean up, making sure it is not handling any
1567 * break exceptions at the time.
1569 if (kgdb_io_module_registered
) {
1570 kgdb_io_module_registered
= 0;
1572 #ifdef CONFIG_MAGIC_SYSRQ
1573 unregister_sysrq_key('g', &sysrq_gdb_op
);
1575 if (kgdb_con_registered
) {
1576 unregister_console(&kgdbcons
);
1577 kgdb_con_registered
= 0;
1582 static void kgdb_initial_breakpoint(void)
1584 kgdb_break_asap
= 0;
1586 printk(KERN_CRIT
"kgdb: Waiting for connection from remote gdb...\n");
1591 * kgdb_register_io_module - register KGDB IO module
1592 * @new_kgdb_io_ops: the io ops vector
1594 * Register it with the KGDB core.
1596 int kgdb_register_io_module(struct kgdb_io
*new_kgdb_io_ops
)
1600 spin_lock(&kgdb_registration_lock
);
1603 spin_unlock(&kgdb_registration_lock
);
1605 printk(KERN_ERR
"kgdb: Another I/O driver is already "
1606 "registered with KGDB.\n");
1610 if (new_kgdb_io_ops
->init
) {
1611 err
= new_kgdb_io_ops
->init();
1613 spin_unlock(&kgdb_registration_lock
);
1618 kgdb_io_ops
= new_kgdb_io_ops
;
1620 spin_unlock(&kgdb_registration_lock
);
1622 printk(KERN_INFO
"kgdb: Registered I/O driver %s.\n",
1623 new_kgdb_io_ops
->name
);
1626 kgdb_register_callbacks();
1628 if (kgdb_break_asap
)
1629 kgdb_initial_breakpoint();
1633 EXPORT_SYMBOL_GPL(kgdb_register_io_module
);
1636 * kkgdb_unregister_io_module - unregister KGDB IO module
1637 * @old_kgdb_io_ops: the io ops vector
1639 * Unregister it with the KGDB core.
1641 void kgdb_unregister_io_module(struct kgdb_io
*old_kgdb_io_ops
)
1643 BUG_ON(kgdb_connected
);
1646 * KGDB is no longer able to communicate out, so
1647 * unregister our callbacks and reset state.
1649 kgdb_unregister_callbacks();
1651 spin_lock(&kgdb_registration_lock
);
1653 WARN_ON_ONCE(kgdb_io_ops
!= old_kgdb_io_ops
);
1656 spin_unlock(&kgdb_registration_lock
);
1659 "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
1660 old_kgdb_io_ops
->name
);
1662 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module
);
1665 * kgdb_breakpoint - generate breakpoint exception
1667 * This function will generate a breakpoint exception. It is used at the
1668 * beginning of a program to sync up with a debugger and can be used
1669 * otherwise as a quick means to stop program execution and "break" into
1672 void kgdb_breakpoint(void)
1674 atomic_set(&kgdb_setting_breakpoint
, 1);
1675 wmb(); /* Sync point before breakpoint */
1676 arch_kgdb_breakpoint();
1677 wmb(); /* Sync point after breakpoint */
1678 atomic_set(&kgdb_setting_breakpoint
, 0);
1680 EXPORT_SYMBOL_GPL(kgdb_breakpoint
);
1682 static int __init
opt_kgdb_wait(char *str
)
1684 kgdb_break_asap
= 1;
1686 if (kgdb_io_module_registered
)
1687 kgdb_initial_breakpoint();
1692 early_param("kgdbwait", opt_kgdb_wait
);