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
;
65 unsigned long threadid
;
66 long kgdb_usethreadid
;
67 struct pt_regs
*linux_regs
;
70 static struct debuggerinfo_struct
{
72 struct task_struct
*task
;
76 * kgdb_connected - Is a host GDB connected to us?
79 EXPORT_SYMBOL_GPL(kgdb_connected
);
81 /* All the KGDB handlers are installed */
82 static int kgdb_io_module_registered
;
84 /* Guard for recursive entry */
85 static int exception_level
;
87 static struct kgdb_io
*kgdb_io_ops
;
88 static DEFINE_SPINLOCK(kgdb_registration_lock
);
90 /* kgdb console driver is loaded */
91 static int kgdb_con_registered
;
92 /* determine if kgdb console output should be used */
93 static int kgdb_use_con
;
95 static int __init
opt_kgdb_con(char *str
)
101 early_param("kgdbcon", opt_kgdb_con
);
103 module_param(kgdb_use_con
, int, 0644);
106 * Holds information about breakpoints in a kernel. These breakpoints are
107 * added and removed by gdb.
109 static struct kgdb_bkpt kgdb_break
[KGDB_MAX_BREAKPOINTS
] = {
110 [0 ... KGDB_MAX_BREAKPOINTS
-1] = { .state
= BP_UNDEFINED
}
114 * The CPU# of the active CPU, or -1 if none:
116 atomic_t kgdb_active
= ATOMIC_INIT(-1);
119 * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
120 * bootup code (which might not have percpu set up yet):
122 static atomic_t passive_cpu_wait
[NR_CPUS
];
123 static atomic_t cpu_in_kgdb
[NR_CPUS
];
124 atomic_t kgdb_setting_breakpoint
;
126 struct task_struct
*kgdb_usethread
;
127 struct task_struct
*kgdb_contthread
;
129 int kgdb_single_step
;
131 /* Our I/O buffers. */
132 static char remcom_in_buffer
[BUFMAX
];
133 static char remcom_out_buffer
[BUFMAX
];
135 /* Storage for the registers, in GDB format. */
136 static unsigned long gdb_regs
[(NUMREGBYTES
+
137 sizeof(unsigned long) - 1) /
138 sizeof(unsigned long)];
140 /* to keep track of the CPU which is doing the single stepping*/
141 atomic_t kgdb_cpu_doing_single_step
= ATOMIC_INIT(-1);
144 * If you are debugging a problem where roundup (the collection of
145 * all other CPUs) is a problem [this should be extremely rare],
146 * then use the nokgdbroundup option to avoid roundup. In that case
147 * the other CPUs might interfere with your debugging context, so
148 * use this with care:
150 static int kgdb_do_roundup
= 1;
152 static int __init
opt_nokgdbroundup(char *str
)
159 early_param("nokgdbroundup", opt_nokgdbroundup
);
162 * Finally, some KGDB code :-)
166 * Weak aliases for breakpoint management,
167 * can be overriden by architectures when needed:
169 int __weak
kgdb_validate_break_address(unsigned long addr
)
171 char tmp_variable
[BREAK_INSTR_SIZE
];
173 return probe_kernel_read(tmp_variable
, (char *)addr
, BREAK_INSTR_SIZE
);
176 int __weak
kgdb_arch_set_breakpoint(unsigned long addr
, char *saved_instr
)
180 err
= probe_kernel_read(saved_instr
, (char *)addr
, BREAK_INSTR_SIZE
);
184 return probe_kernel_write((char *)addr
, arch_kgdb_ops
.gdb_bpt_instr
,
188 int __weak
kgdb_arch_remove_breakpoint(unsigned long addr
, char *bundle
)
190 return probe_kernel_write((char *)addr
,
191 (char *)bundle
, BREAK_INSTR_SIZE
);
194 unsigned long __weak
kgdb_arch_pc(int exception
, struct pt_regs
*regs
)
196 return instruction_pointer(regs
);
199 int __weak
kgdb_arch_init(void)
204 int __weak
kgdb_skipexception(int exception
, struct pt_regs
*regs
)
210 kgdb_post_primary_code(struct pt_regs
*regs
, int e_vector
, int err_code
)
216 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
217 * @regs: Current &struct pt_regs.
219 * This function will be called if the particular architecture must
220 * disable hardware debugging while it is processing gdb packets or
221 * handling exception.
223 void __weak
kgdb_disable_hw_debug(struct pt_regs
*regs
)
228 * GDB remote protocol parser:
231 static int hex(char ch
)
233 if ((ch
>= 'a') && (ch
<= 'f'))
234 return ch
- 'a' + 10;
235 if ((ch
>= '0') && (ch
<= '9'))
237 if ((ch
>= 'A') && (ch
<= 'F'))
238 return ch
- 'A' + 10;
242 /* scan for the sequence $<data>#<checksum> */
243 static void get_packet(char *buffer
)
245 unsigned char checksum
;
246 unsigned char xmitcsum
;
252 * Spin and wait around for the start character, ignore all
255 while ((ch
= (kgdb_io_ops
->read_char())) != '$')
265 * now, read until a # or end of buffer is found:
267 while (count
< (BUFMAX
- 1)) {
268 ch
= kgdb_io_ops
->read_char();
271 checksum
= checksum
+ ch
;
278 xmitcsum
= hex(kgdb_io_ops
->read_char()) << 4;
279 xmitcsum
+= hex(kgdb_io_ops
->read_char());
281 if (checksum
!= xmitcsum
)
282 /* failed checksum */
283 kgdb_io_ops
->write_char('-');
285 /* successful transfer */
286 kgdb_io_ops
->write_char('+');
287 if (kgdb_io_ops
->flush
)
288 kgdb_io_ops
->flush();
290 } while (checksum
!= xmitcsum
);
294 * Send the packet in buffer.
295 * Check for gdb connection if asked for.
297 static void put_packet(char *buffer
)
299 unsigned char checksum
;
304 * $<packet info>#<checksum>.
307 kgdb_io_ops
->write_char('$');
311 while ((ch
= buffer
[count
])) {
312 kgdb_io_ops
->write_char(ch
);
317 kgdb_io_ops
->write_char('#');
318 kgdb_io_ops
->write_char(hex_asc_hi(checksum
));
319 kgdb_io_ops
->write_char(hex_asc_lo(checksum
));
320 if (kgdb_io_ops
->flush
)
321 kgdb_io_ops
->flush();
323 /* Now see what we get in reply. */
324 ch
= kgdb_io_ops
->read_char();
327 ch
= kgdb_io_ops
->read_char();
329 /* If we get an ACK, we are done. */
334 * If we get the start of another packet, this means
335 * that GDB is attempting to reconnect. We will NAK
336 * the packet being sent, and stop trying to send this
340 kgdb_io_ops
->write_char('-');
341 if (kgdb_io_ops
->flush
)
342 kgdb_io_ops
->flush();
349 * Convert the memory pointed to by mem into hex, placing result in buf.
350 * Return a pointer to the last char put in buf (null). May return an error.
352 int kgdb_mem2hex(char *mem
, char *buf
, int count
)
358 * We use the upper half of buf as an intermediate buffer for the
359 * raw memory copy. Hex conversion will work against this one.
363 err
= probe_kernel_read(tmp
, mem
, count
);
366 buf
= pack_hex_byte(buf
, *tmp
);
378 * Copy the binary array pointed to by buf into mem. Fix $, #, and
379 * 0x7d escaped with 0x7d. Return a pointer to the character after
380 * the last byte written.
382 static int kgdb_ebin2mem(char *buf
, char *mem
, int count
)
387 while (count
-- > 0) {
392 err
= probe_kernel_write(mem
, &c
, 1);
403 * Convert the hex array pointed to by buf into binary to be placed in mem.
404 * Return a pointer to the character AFTER the last byte written.
405 * May return an error.
407 int kgdb_hex2mem(char *buf
, char *mem
, int count
)
413 * We use the upper half of buf as an intermediate buffer for the
414 * raw memory that is converted from hex.
416 tmp_raw
= buf
+ count
* 2;
418 tmp_hex
= tmp_raw
- 1;
419 while (tmp_hex
>= buf
) {
421 *tmp_raw
= hex(*tmp_hex
--);
422 *tmp_raw
|= hex(*tmp_hex
--) << 4;
425 return probe_kernel_write(mem
, tmp_raw
, count
);
429 * While we find nice hex chars, build a long_val.
430 * Return number of chars processed.
432 int kgdb_hex2long(char **ptr
, unsigned long *long_val
)
440 hex_val
= hex(**ptr
);
444 *long_val
= (*long_val
<< 4) | hex_val
;
452 /* Write memory due to an 'M' or 'X' packet. */
453 static int write_mem_msg(int binary
)
455 char *ptr
= &remcom_in_buffer
[1];
457 unsigned long length
;
460 if (kgdb_hex2long(&ptr
, &addr
) > 0 && *(ptr
++) == ',' &&
461 kgdb_hex2long(&ptr
, &length
) > 0 && *(ptr
++) == ':') {
463 err
= kgdb_ebin2mem(ptr
, (char *)addr
, length
);
465 err
= kgdb_hex2mem(ptr
, (char *)addr
, length
);
468 if (CACHE_FLUSH_IS_SAFE
)
469 flush_icache_range(addr
, addr
+ length
+ 1);
476 static void error_packet(char *pkt
, int error
)
480 pkt
[1] = hex_asc
[(error
/ 10)];
481 pkt
[2] = hex_asc
[(error
% 10)];
486 * Thread ID accessors. We represent a flat TID space to GDB, where
487 * the per CPU idle threads (which under Linux all have PID 0) are
488 * remapped to negative TIDs.
491 #define BUF_THREAD_ID_SIZE 16
493 static char *pack_threadid(char *pkt
, unsigned char *id
)
497 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
499 pkt
= pack_hex_byte(pkt
, *id
++);
504 static void int_to_threadref(unsigned char *id
, int value
)
509 scan
= (unsigned char *)id
;
512 put_unaligned_be32(value
, scan
);
515 static struct task_struct
*getthread(struct pt_regs
*regs
, int tid
)
518 * Non-positive TIDs are remapped idle tasks:
521 return idle_task(-tid
);
524 * find_task_by_pid_ns() does not take the tasklist lock anymore
525 * but is nicely RCU locked - hence is a pretty resilient
528 return find_task_by_pid_ns(tid
, &init_pid_ns
);
532 * CPU debug state control:
536 static void kgdb_wait(struct pt_regs
*regs
)
541 local_irq_save(flags
);
542 cpu
= raw_smp_processor_id();
543 kgdb_info
[cpu
].debuggerinfo
= regs
;
544 kgdb_info
[cpu
].task
= current
;
546 * Make sure the above info reaches the primary CPU before
547 * our cpu_in_kgdb[] flag setting does:
550 atomic_set(&cpu_in_kgdb
[cpu
], 1);
552 /* Wait till primary CPU is done with debugging */
553 while (atomic_read(&passive_cpu_wait
[cpu
]))
556 kgdb_info
[cpu
].debuggerinfo
= NULL
;
557 kgdb_info
[cpu
].task
= NULL
;
559 /* fix up hardware debug registers on local cpu */
560 if (arch_kgdb_ops
.correct_hw_break
)
561 arch_kgdb_ops
.correct_hw_break();
563 /* Signal the primary CPU that we are done: */
564 atomic_set(&cpu_in_kgdb
[cpu
], 0);
565 clocksource_touch_watchdog();
566 local_irq_restore(flags
);
571 * Some architectures need cache flushes when we set/clear a
574 static void kgdb_flush_swbreak_addr(unsigned long addr
)
576 if (!CACHE_FLUSH_IS_SAFE
)
579 if (current
->mm
&& current
->mm
->mmap_cache
) {
580 flush_cache_range(current
->mm
->mmap_cache
,
581 addr
, addr
+ BREAK_INSTR_SIZE
);
583 /* Force flush instruction cache if it was outside the mm */
584 flush_icache_range(addr
, addr
+ BREAK_INSTR_SIZE
);
588 * SW breakpoint management:
590 static int kgdb_activate_sw_breakpoints(void)
596 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
597 if (kgdb_break
[i
].state
!= BP_SET
)
600 addr
= kgdb_break
[i
].bpt_addr
;
601 error
= kgdb_arch_set_breakpoint(addr
,
602 kgdb_break
[i
].saved_instr
);
606 kgdb_flush_swbreak_addr(addr
);
607 kgdb_break
[i
].state
= BP_ACTIVE
;
612 static int kgdb_set_sw_break(unsigned long addr
)
614 int err
= kgdb_validate_break_address(addr
);
621 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
622 if ((kgdb_break
[i
].state
== BP_SET
) &&
623 (kgdb_break
[i
].bpt_addr
== addr
))
626 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
627 if (kgdb_break
[i
].state
== BP_REMOVED
&&
628 kgdb_break
[i
].bpt_addr
== addr
) {
635 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
636 if (kgdb_break
[i
].state
== BP_UNDEFINED
) {
646 kgdb_break
[breakno
].state
= BP_SET
;
647 kgdb_break
[breakno
].type
= BP_BREAKPOINT
;
648 kgdb_break
[breakno
].bpt_addr
= addr
;
653 static int kgdb_deactivate_sw_breakpoints(void)
659 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
660 if (kgdb_break
[i
].state
!= BP_ACTIVE
)
662 addr
= kgdb_break
[i
].bpt_addr
;
663 error
= kgdb_arch_remove_breakpoint(addr
,
664 kgdb_break
[i
].saved_instr
);
668 kgdb_flush_swbreak_addr(addr
);
669 kgdb_break
[i
].state
= BP_SET
;
674 static int kgdb_remove_sw_break(unsigned long addr
)
678 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
679 if ((kgdb_break
[i
].state
== BP_SET
) &&
680 (kgdb_break
[i
].bpt_addr
== addr
)) {
681 kgdb_break
[i
].state
= BP_REMOVED
;
688 int kgdb_isremovedbreak(unsigned long addr
)
692 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
693 if ((kgdb_break
[i
].state
== BP_REMOVED
) &&
694 (kgdb_break
[i
].bpt_addr
== addr
))
700 static int remove_all_break(void)
706 /* Clear memory breakpoints. */
707 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
708 if (kgdb_break
[i
].state
!= BP_ACTIVE
)
710 addr
= kgdb_break
[i
].bpt_addr
;
711 error
= kgdb_arch_remove_breakpoint(addr
,
712 kgdb_break
[i
].saved_instr
);
714 printk(KERN_ERR
"KGDB: breakpoint remove failed: %lx\n",
717 kgdb_break
[i
].state
= BP_UNDEFINED
;
720 /* Clear hardware breakpoints. */
721 if (arch_kgdb_ops
.remove_all_hw_break
)
722 arch_kgdb_ops
.remove_all_hw_break();
728 * Remap normal tasks to their real PID, idle tasks to -1 ... -NR_CPUs:
730 static inline int shadow_pid(int realpid
)
735 return -1-raw_smp_processor_id();
738 static char gdbmsgbuf
[BUFMAX
+ 1];
740 static void kgdb_msg_write(const char *s
, int len
)
749 /* Fill and send buffers... */
751 bufptr
= gdbmsgbuf
+ 1;
753 /* Calculate how many this time */
754 if ((len
<< 1) > (BUFMAX
- 2))
755 wcount
= (BUFMAX
- 2) >> 1;
759 /* Pack in hex chars */
760 for (i
= 0; i
< wcount
; i
++)
761 bufptr
= pack_hex_byte(bufptr
, s
[i
]);
769 put_packet(gdbmsgbuf
);
774 * Return true if there is a valid kgdb I/O module. Also if no
775 * debugger is attached a message can be printed to the console about
776 * waiting for the debugger to attach.
778 * The print_wait argument is only to be true when called from inside
779 * the core kgdb_handle_exception, because it will wait for the
780 * debugger to attach.
782 static int kgdb_io_ready(int print_wait
)
788 if (atomic_read(&kgdb_setting_breakpoint
))
791 printk(KERN_CRIT
"KGDB: Waiting for remote debugger\n");
796 * All the functions that start with gdb_cmd are the various
797 * operations to implement the handlers for the gdbserial protocol
798 * where KGDB is communicating with an external debugger
801 /* Handle the '?' status packets */
802 static void gdb_cmd_status(struct kgdb_state
*ks
)
805 * We know that this packet is only sent
806 * during initial connect. So to be safe,
807 * we clear out our breakpoints now in case
808 * GDB is reconnecting.
812 remcom_out_buffer
[0] = 'S';
813 pack_hex_byte(&remcom_out_buffer
[1], ks
->signo
);
816 /* Handle the 'g' get registers request */
817 static void gdb_cmd_getregs(struct kgdb_state
*ks
)
819 struct task_struct
*thread
;
820 void *local_debuggerinfo
;
823 thread
= kgdb_usethread
;
825 thread
= kgdb_info
[ks
->cpu
].task
;
826 local_debuggerinfo
= kgdb_info
[ks
->cpu
].debuggerinfo
;
828 local_debuggerinfo
= NULL
;
829 for (i
= 0; i
< NR_CPUS
; i
++) {
831 * Try to find the task on some other
832 * or possibly this node if we do not
833 * find the matching task then we try
834 * to approximate the results.
836 if (thread
== kgdb_info
[i
].task
)
837 local_debuggerinfo
= kgdb_info
[i
].debuggerinfo
;
842 * All threads that don't have debuggerinfo should be
843 * in __schedule() sleeping, since all other CPUs
844 * are in kgdb_wait, and thus have debuggerinfo.
846 if (local_debuggerinfo
) {
847 pt_regs_to_gdb_regs(gdb_regs
, local_debuggerinfo
);
850 * Pull stuff saved during switch_to; nothing
851 * else is accessible (or even particularly
854 * This should be enough for a stack trace.
856 sleeping_thread_to_gdb_regs(gdb_regs
, thread
);
858 kgdb_mem2hex((char *)gdb_regs
, remcom_out_buffer
, NUMREGBYTES
);
861 /* Handle the 'G' set registers request */
862 static void gdb_cmd_setregs(struct kgdb_state
*ks
)
864 kgdb_hex2mem(&remcom_in_buffer
[1], (char *)gdb_regs
, NUMREGBYTES
);
866 if (kgdb_usethread
&& kgdb_usethread
!= current
) {
867 error_packet(remcom_out_buffer
, -EINVAL
);
869 gdb_regs_to_pt_regs(gdb_regs
, ks
->linux_regs
);
870 strcpy(remcom_out_buffer
, "OK");
874 /* Handle the 'm' memory read bytes */
875 static void gdb_cmd_memread(struct kgdb_state
*ks
)
877 char *ptr
= &remcom_in_buffer
[1];
878 unsigned long length
;
882 if (kgdb_hex2long(&ptr
, &addr
) > 0 && *ptr
++ == ',' &&
883 kgdb_hex2long(&ptr
, &length
) > 0) {
884 err
= kgdb_mem2hex((char *)addr
, remcom_out_buffer
, length
);
886 error_packet(remcom_out_buffer
, err
);
888 error_packet(remcom_out_buffer
, -EINVAL
);
892 /* Handle the 'M' memory write bytes */
893 static void gdb_cmd_memwrite(struct kgdb_state
*ks
)
895 int err
= write_mem_msg(0);
898 error_packet(remcom_out_buffer
, err
);
900 strcpy(remcom_out_buffer
, "OK");
903 /* Handle the 'X' memory binary write bytes */
904 static void gdb_cmd_binwrite(struct kgdb_state
*ks
)
906 int err
= write_mem_msg(1);
909 error_packet(remcom_out_buffer
, err
);
911 strcpy(remcom_out_buffer
, "OK");
914 /* Handle the 'D' or 'k', detach or kill packets */
915 static void gdb_cmd_detachkill(struct kgdb_state
*ks
)
919 /* The detach case */
920 if (remcom_in_buffer
[0] == 'D') {
921 error
= remove_all_break();
923 error_packet(remcom_out_buffer
, error
);
925 strcpy(remcom_out_buffer
, "OK");
928 put_packet(remcom_out_buffer
);
931 * Assume the kill case, with no exit code checking,
932 * trying to force detach the debugger:
939 /* Handle the 'R' reboot packets */
940 static int gdb_cmd_reboot(struct kgdb_state
*ks
)
942 /* For now, only honor R0 */
943 if (strcmp(remcom_in_buffer
, "R0") == 0) {
944 printk(KERN_CRIT
"Executing emergency reboot\n");
945 strcpy(remcom_out_buffer
, "OK");
946 put_packet(remcom_out_buffer
);
949 * Execution should not return from
950 * machine_emergency_restart()
952 machine_emergency_restart();
960 /* Handle the 'q' query packets */
961 static void gdb_cmd_query(struct kgdb_state
*ks
)
963 struct task_struct
*thread
;
964 unsigned char thref
[8];
968 switch (remcom_in_buffer
[1]) {
971 if (memcmp(remcom_in_buffer
+ 2, "ThreadInfo", 10)) {
972 error_packet(remcom_out_buffer
, -EINVAL
);
976 if (remcom_in_buffer
[1] == 'f')
979 remcom_out_buffer
[0] = 'm';
980 ptr
= remcom_out_buffer
+ 1;
982 for (i
= 0; i
< 17; ks
->threadid
++) {
983 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
985 int_to_threadref(thref
, ks
->threadid
);
986 pack_threadid(ptr
, thref
);
987 ptr
+= BUF_THREAD_ID_SIZE
;
996 /* Current thread id */
997 strcpy(remcom_out_buffer
, "QC");
998 ks
->threadid
= shadow_pid(current
->pid
);
999 int_to_threadref(thref
, ks
->threadid
);
1000 pack_threadid(remcom_out_buffer
+ 2, thref
);
1003 if (memcmp(remcom_in_buffer
+ 1, "ThreadExtraInfo,", 16)) {
1004 error_packet(remcom_out_buffer
, -EINVAL
);
1008 ptr
= remcom_in_buffer
+ 17;
1009 kgdb_hex2long(&ptr
, &ks
->threadid
);
1010 if (!getthread(ks
->linux_regs
, ks
->threadid
)) {
1011 error_packet(remcom_out_buffer
, -EINVAL
);
1014 if (ks
->threadid
> 0) {
1015 kgdb_mem2hex(getthread(ks
->linux_regs
,
1016 ks
->threadid
)->comm
,
1017 remcom_out_buffer
, 16);
1019 static char tmpstr
[23 + BUF_THREAD_ID_SIZE
];
1021 sprintf(tmpstr
, "Shadow task %d for pid 0",
1022 (int)(-ks
->threadid
-1));
1023 kgdb_mem2hex(tmpstr
, remcom_out_buffer
, strlen(tmpstr
));
1029 /* Handle the 'H' task query packets */
1030 static void gdb_cmd_task(struct kgdb_state
*ks
)
1032 struct task_struct
*thread
;
1035 switch (remcom_in_buffer
[1]) {
1037 ptr
= &remcom_in_buffer
[2];
1038 kgdb_hex2long(&ptr
, &ks
->threadid
);
1039 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
1040 if (!thread
&& ks
->threadid
> 0) {
1041 error_packet(remcom_out_buffer
, -EINVAL
);
1044 kgdb_usethread
= thread
;
1045 ks
->kgdb_usethreadid
= ks
->threadid
;
1046 strcpy(remcom_out_buffer
, "OK");
1049 ptr
= &remcom_in_buffer
[2];
1050 kgdb_hex2long(&ptr
, &ks
->threadid
);
1051 if (!ks
->threadid
) {
1052 kgdb_contthread
= NULL
;
1054 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
1055 if (!thread
&& ks
->threadid
> 0) {
1056 error_packet(remcom_out_buffer
, -EINVAL
);
1059 kgdb_contthread
= thread
;
1061 strcpy(remcom_out_buffer
, "OK");
1066 /* Handle the 'T' thread query packets */
1067 static void gdb_cmd_thread(struct kgdb_state
*ks
)
1069 char *ptr
= &remcom_in_buffer
[1];
1070 struct task_struct
*thread
;
1072 kgdb_hex2long(&ptr
, &ks
->threadid
);
1073 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
1075 strcpy(remcom_out_buffer
, "OK");
1077 error_packet(remcom_out_buffer
, -EINVAL
);
1080 /* Handle the 'z' or 'Z' breakpoint remove or set packets */
1081 static void gdb_cmd_break(struct kgdb_state
*ks
)
1084 * Since GDB-5.3, it's been drafted that '0' is a software
1085 * breakpoint, '1' is a hardware breakpoint, so let's do that.
1087 char *bpt_type
= &remcom_in_buffer
[1];
1088 char *ptr
= &remcom_in_buffer
[2];
1090 unsigned long length
;
1093 if (arch_kgdb_ops
.set_hw_breakpoint
&& *bpt_type
>= '1') {
1095 if (*bpt_type
> '4')
1098 if (*bpt_type
!= '0' && *bpt_type
!= '1')
1104 * Test if this is a hardware breakpoint, and
1107 if (*bpt_type
== '1' && !(arch_kgdb_ops
.flags
& KGDB_HW_BREAKPOINT
))
1111 if (*(ptr
++) != ',') {
1112 error_packet(remcom_out_buffer
, -EINVAL
);
1115 if (!kgdb_hex2long(&ptr
, &addr
)) {
1116 error_packet(remcom_out_buffer
, -EINVAL
);
1119 if (*(ptr
++) != ',' ||
1120 !kgdb_hex2long(&ptr
, &length
)) {
1121 error_packet(remcom_out_buffer
, -EINVAL
);
1125 if (remcom_in_buffer
[0] == 'Z' && *bpt_type
== '0')
1126 error
= kgdb_set_sw_break(addr
);
1127 else if (remcom_in_buffer
[0] == 'z' && *bpt_type
== '0')
1128 error
= kgdb_remove_sw_break(addr
);
1129 else if (remcom_in_buffer
[0] == 'Z')
1130 error
= arch_kgdb_ops
.set_hw_breakpoint(addr
,
1131 (int)length
, *bpt_type
- '0');
1132 else if (remcom_in_buffer
[0] == 'z')
1133 error
= arch_kgdb_ops
.remove_hw_breakpoint(addr
,
1134 (int) length
, *bpt_type
- '0');
1137 strcpy(remcom_out_buffer
, "OK");
1139 error_packet(remcom_out_buffer
, error
);
1142 /* Handle the 'C' signal / exception passing packets */
1143 static int gdb_cmd_exception_pass(struct kgdb_state
*ks
)
1145 /* C09 == pass exception
1146 * C15 == detach kgdb, pass exception
1148 if (remcom_in_buffer
[1] == '0' && remcom_in_buffer
[2] == '9') {
1150 ks
->pass_exception
= 1;
1151 remcom_in_buffer
[0] = 'c';
1153 } else if (remcom_in_buffer
[1] == '1' && remcom_in_buffer
[2] == '5') {
1155 ks
->pass_exception
= 1;
1156 remcom_in_buffer
[0] = 'D';
1162 error_packet(remcom_out_buffer
, -EINVAL
);
1166 /* Indicate fall through */
1171 * This function performs all gdbserial command procesing
1173 static int gdb_serial_stub(struct kgdb_state
*ks
)
1178 /* Clear the out buffer. */
1179 memset(remcom_out_buffer
, 0, sizeof(remcom_out_buffer
));
1181 if (kgdb_connected
) {
1182 unsigned char thref
[8];
1185 /* Reply to host that an exception has occurred */
1186 ptr
= remcom_out_buffer
;
1188 ptr
= pack_hex_byte(ptr
, ks
->signo
);
1189 ptr
+= strlen(strcpy(ptr
, "thread:"));
1190 int_to_threadref(thref
, shadow_pid(current
->pid
));
1191 ptr
= pack_threadid(ptr
, thref
);
1193 put_packet(remcom_out_buffer
);
1196 kgdb_usethread
= kgdb_info
[ks
->cpu
].task
;
1197 ks
->kgdb_usethreadid
= shadow_pid(kgdb_info
[ks
->cpu
].task
->pid
);
1198 ks
->pass_exception
= 0;
1203 /* Clear the out buffer. */
1204 memset(remcom_out_buffer
, 0, sizeof(remcom_out_buffer
));
1206 get_packet(remcom_in_buffer
);
1208 switch (remcom_in_buffer
[0]) {
1209 case '?': /* gdbserial status */
1212 case 'g': /* return the value of the CPU registers */
1213 gdb_cmd_getregs(ks
);
1215 case 'G': /* set the value of the CPU registers - return OK */
1216 gdb_cmd_setregs(ks
);
1218 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1219 gdb_cmd_memread(ks
);
1221 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1222 gdb_cmd_memwrite(ks
);
1224 case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1225 gdb_cmd_binwrite(ks
);
1227 /* kill or detach. KGDB should treat this like a
1230 case 'D': /* Debugger detach */
1231 case 'k': /* Debugger detach via kill */
1232 gdb_cmd_detachkill(ks
);
1233 goto default_handle
;
1234 case 'R': /* Reboot */
1235 if (gdb_cmd_reboot(ks
))
1236 goto default_handle
;
1238 case 'q': /* query command */
1241 case 'H': /* task related */
1244 case 'T': /* Query thread status */
1247 case 'z': /* Break point remove */
1248 case 'Z': /* Break point set */
1251 case 'C': /* Exception passing */
1252 tmp
= gdb_cmd_exception_pass(ks
);
1254 goto default_handle
;
1257 /* Fall through on tmp < 0 */
1258 case 'c': /* Continue packet */
1259 case 's': /* Single step packet */
1260 if (kgdb_contthread
&& kgdb_contthread
!= current
) {
1261 /* Can't switch threads in kgdb */
1262 error_packet(remcom_out_buffer
, -EINVAL
);
1265 kgdb_activate_sw_breakpoints();
1266 /* Fall through to default processing */
1269 error
= kgdb_arch_handle_exception(ks
->ex_vector
,
1276 * Leave cmd processing on error, detach,
1277 * kill, continue, or single step.
1279 if (error
>= 0 || remcom_in_buffer
[0] == 'D' ||
1280 remcom_in_buffer
[0] == 'k') {
1287 /* reply to the request */
1288 put_packet(remcom_out_buffer
);
1292 if (ks
->pass_exception
)
1297 static int kgdb_reenter_check(struct kgdb_state
*ks
)
1301 if (atomic_read(&kgdb_active
) != raw_smp_processor_id())
1304 /* Panic on recursive debugger calls: */
1306 addr
= kgdb_arch_pc(ks
->ex_vector
, ks
->linux_regs
);
1307 kgdb_deactivate_sw_breakpoints();
1310 * If the break point removed ok at the place exception
1311 * occurred, try to recover and print a warning to the end
1312 * user because the user planted a breakpoint in a place that
1313 * KGDB needs in order to function.
1315 if (kgdb_remove_sw_break(addr
) == 0) {
1316 exception_level
= 0;
1317 kgdb_skipexception(ks
->ex_vector
, ks
->linux_regs
);
1318 kgdb_activate_sw_breakpoints();
1319 printk(KERN_CRIT
"KGDB: re-enter error: breakpoint removed %lx\n",
1326 kgdb_skipexception(ks
->ex_vector
, ks
->linux_regs
);
1328 if (exception_level
> 1) {
1330 panic("Recursive entry to debugger");
1333 printk(KERN_CRIT
"KGDB: re-enter exception: ALL breakpoints killed\n");
1335 panic("Recursive entry to debugger");
1341 * kgdb_handle_exception() - main entry point from a kernel exception
1343 * Locking hierarchy:
1344 * interface locks, if any (begin_session)
1345 * kgdb lock (kgdb_active)
1348 kgdb_handle_exception(int evector
, int signo
, int ecode
, struct pt_regs
*regs
)
1350 struct kgdb_state kgdb_var
;
1351 struct kgdb_state
*ks
= &kgdb_var
;
1352 unsigned long flags
;
1356 ks
->cpu
= raw_smp_processor_id();
1357 ks
->ex_vector
= evector
;
1359 ks
->ex_vector
= evector
;
1360 ks
->err_code
= ecode
;
1361 ks
->kgdb_usethreadid
= 0;
1362 ks
->linux_regs
= regs
;
1364 if (kgdb_reenter_check(ks
))
1365 return 0; /* Ouch, double exception ! */
1369 * Interrupts will be restored by the 'trap return' code, except when
1372 local_irq_save(flags
);
1374 cpu
= raw_smp_processor_id();
1377 * Acquire the kgdb_active lock:
1379 while (atomic_cmpxchg(&kgdb_active
, -1, cpu
) != -1)
1383 * Do not start the debugger connection on this CPU if the last
1384 * instance of the exception handler wanted to come into the
1385 * debugger on a different CPU via a single step
1387 if (atomic_read(&kgdb_cpu_doing_single_step
) != -1 &&
1388 atomic_read(&kgdb_cpu_doing_single_step
) != cpu
) {
1390 atomic_set(&kgdb_active
, -1);
1391 clocksource_touch_watchdog();
1392 local_irq_restore(flags
);
1397 if (!kgdb_io_ready(1)) {
1399 goto kgdb_restore
; /* No I/O connection, so resume the system */
1403 * Don't enter if we have hit a removed breakpoint.
1405 if (kgdb_skipexception(ks
->ex_vector
, ks
->linux_regs
))
1408 /* Call the I/O driver's pre_exception routine */
1409 if (kgdb_io_ops
->pre_exception
)
1410 kgdb_io_ops
->pre_exception();
1412 kgdb_info
[ks
->cpu
].debuggerinfo
= ks
->linux_regs
;
1413 kgdb_info
[ks
->cpu
].task
= current
;
1415 kgdb_disable_hw_debug(ks
->linux_regs
);
1418 * Get the passive CPU lock which will hold all the non-primary
1419 * CPU in a spin state while the debugger is active
1421 if (!kgdb_single_step
|| !kgdb_contthread
) {
1422 for (i
= 0; i
< NR_CPUS
; i
++)
1423 atomic_set(&passive_cpu_wait
[i
], 1);
1427 * spin_lock code is good enough as a barrier so we don't
1430 atomic_set(&cpu_in_kgdb
[ks
->cpu
], 1);
1433 /* Signal the other CPUs to enter kgdb_wait() */
1434 if ((!kgdb_single_step
|| !kgdb_contthread
) && kgdb_do_roundup
)
1435 kgdb_roundup_cpus(flags
);
1439 * Wait for the other CPUs to be notified and be waiting for us:
1441 for_each_online_cpu(i
) {
1442 while (!atomic_read(&cpu_in_kgdb
[i
]))
1447 * At this point the primary processor is completely
1448 * in the debugger and all secondary CPUs are quiescent
1450 kgdb_post_primary_code(ks
->linux_regs
, ks
->ex_vector
, ks
->err_code
);
1451 kgdb_deactivate_sw_breakpoints();
1452 kgdb_single_step
= 0;
1453 kgdb_contthread
= NULL
;
1454 exception_level
= 0;
1456 /* Talk to debugger with gdbserial protocol */
1457 error
= gdb_serial_stub(ks
);
1459 /* Call the I/O driver's post_exception routine */
1460 if (kgdb_io_ops
->post_exception
)
1461 kgdb_io_ops
->post_exception();
1463 kgdb_info
[ks
->cpu
].debuggerinfo
= NULL
;
1464 kgdb_info
[ks
->cpu
].task
= NULL
;
1465 atomic_set(&cpu_in_kgdb
[ks
->cpu
], 0);
1467 if (!kgdb_single_step
|| !kgdb_contthread
) {
1468 for (i
= NR_CPUS
-1; i
>= 0; i
--)
1469 atomic_set(&passive_cpu_wait
[i
], 0);
1471 * Wait till all the CPUs have quit
1472 * from the debugger.
1474 for_each_online_cpu(i
) {
1475 while (atomic_read(&cpu_in_kgdb
[i
]))
1481 /* Free kgdb_active */
1482 atomic_set(&kgdb_active
, -1);
1483 clocksource_touch_watchdog();
1484 local_irq_restore(flags
);
1489 int kgdb_nmicallback(int cpu
, void *regs
)
1492 if (!atomic_read(&cpu_in_kgdb
[cpu
]) &&
1493 atomic_read(&kgdb_active
) != cpu
&&
1494 atomic_read(&cpu_in_kgdb
[atomic_read(&kgdb_active
)])) {
1495 kgdb_wait((struct pt_regs
*)regs
);
1502 void kgdb_console_write(struct console
*co
, const char *s
, unsigned count
)
1504 unsigned long flags
;
1506 /* If we're debugging, or KGDB has not connected, don't try
1508 if (!kgdb_connected
|| atomic_read(&kgdb_active
) != -1)
1511 local_irq_save(flags
);
1512 kgdb_msg_write(s
, count
);
1513 local_irq_restore(flags
);
1516 static struct console kgdbcons
= {
1518 .write
= kgdb_console_write
,
1519 .flags
= CON_PRINTBUFFER
| CON_ENABLED
,
1523 #ifdef CONFIG_MAGIC_SYSRQ
1524 static void sysrq_handle_gdb(int key
, struct tty_struct
*tty
)
1527 printk(KERN_CRIT
"ERROR: No KGDB I/O module available\n");
1530 if (!kgdb_connected
)
1531 printk(KERN_CRIT
"Entering KGDB\n");
1536 static struct sysrq_key_op sysrq_gdb_op
= {
1537 .handler
= sysrq_handle_gdb
,
1539 .action_msg
= "GDB",
1543 static void kgdb_register_callbacks(void)
1545 if (!kgdb_io_module_registered
) {
1546 kgdb_io_module_registered
= 1;
1548 #ifdef CONFIG_MAGIC_SYSRQ
1549 register_sysrq_key('g', &sysrq_gdb_op
);
1551 if (kgdb_use_con
&& !kgdb_con_registered
) {
1552 register_console(&kgdbcons
);
1553 kgdb_con_registered
= 1;
1558 static void kgdb_unregister_callbacks(void)
1561 * When this routine is called KGDB should unregister from the
1562 * panic handler and clean up, making sure it is not handling any
1563 * break exceptions at the time.
1565 if (kgdb_io_module_registered
) {
1566 kgdb_io_module_registered
= 0;
1568 #ifdef CONFIG_MAGIC_SYSRQ
1569 unregister_sysrq_key('g', &sysrq_gdb_op
);
1571 if (kgdb_con_registered
) {
1572 unregister_console(&kgdbcons
);
1573 kgdb_con_registered
= 0;
1578 static void kgdb_initial_breakpoint(void)
1580 kgdb_break_asap
= 0;
1582 printk(KERN_CRIT
"kgdb: Waiting for connection from remote gdb...\n");
1587 * kgdb_register_io_module - register KGDB IO module
1588 * @new_kgdb_io_ops: the io ops vector
1590 * Register it with the KGDB core.
1592 int kgdb_register_io_module(struct kgdb_io
*new_kgdb_io_ops
)
1596 spin_lock(&kgdb_registration_lock
);
1599 spin_unlock(&kgdb_registration_lock
);
1601 printk(KERN_ERR
"kgdb: Another I/O driver is already "
1602 "registered with KGDB.\n");
1606 if (new_kgdb_io_ops
->init
) {
1607 err
= new_kgdb_io_ops
->init();
1609 spin_unlock(&kgdb_registration_lock
);
1614 kgdb_io_ops
= new_kgdb_io_ops
;
1616 spin_unlock(&kgdb_registration_lock
);
1618 printk(KERN_INFO
"kgdb: Registered I/O driver %s.\n",
1619 new_kgdb_io_ops
->name
);
1622 kgdb_register_callbacks();
1624 if (kgdb_break_asap
)
1625 kgdb_initial_breakpoint();
1629 EXPORT_SYMBOL_GPL(kgdb_register_io_module
);
1632 * kkgdb_unregister_io_module - unregister KGDB IO module
1633 * @old_kgdb_io_ops: the io ops vector
1635 * Unregister it with the KGDB core.
1637 void kgdb_unregister_io_module(struct kgdb_io
*old_kgdb_io_ops
)
1639 BUG_ON(kgdb_connected
);
1642 * KGDB is no longer able to communicate out, so
1643 * unregister our callbacks and reset state.
1645 kgdb_unregister_callbacks();
1647 spin_lock(&kgdb_registration_lock
);
1649 WARN_ON_ONCE(kgdb_io_ops
!= old_kgdb_io_ops
);
1652 spin_unlock(&kgdb_registration_lock
);
1655 "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
1656 old_kgdb_io_ops
->name
);
1658 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module
);
1661 * kgdb_breakpoint - generate breakpoint exception
1663 * This function will generate a breakpoint exception. It is used at the
1664 * beginning of a program to sync up with a debugger and can be used
1665 * otherwise as a quick means to stop program execution and "break" into
1668 void kgdb_breakpoint(void)
1670 atomic_set(&kgdb_setting_breakpoint
, 1);
1671 wmb(); /* Sync point before breakpoint */
1672 arch_kgdb_breakpoint();
1673 wmb(); /* Sync point after breakpoint */
1674 atomic_set(&kgdb_setting_breakpoint
, 0);
1676 EXPORT_SYMBOL_GPL(kgdb_breakpoint
);
1678 static int __init
opt_kgdb_wait(char *str
)
1680 kgdb_break_asap
= 1;
1682 if (kgdb_io_module_registered
)
1683 kgdb_initial_breakpoint();
1688 early_param("kgdbwait", opt_kgdb_wait
);