4 * Maintainer: Jason Wessel <jason.wessel@windriver.com>
6 * Copyright (C) 2000-2001 VERITAS Software Corporation.
7 * Copyright (C) 2002-2004 Timesys Corporation
8 * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com>
9 * Copyright (C) 2004 Pavel Machek <pavel@suse.cz>
10 * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org>
11 * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd.
12 * Copyright (C) 2005-2008 Wind River Systems, Inc.
13 * Copyright (C) 2007 MontaVista Software, Inc.
14 * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
16 * Contributors at various stages not listed above:
17 * Jason Wessel ( jason.wessel@windriver.com )
18 * George Anzinger <george@mvista.com>
19 * Anurekh Saxena (anurekh.saxena@timesys.com)
20 * Lake Stevens Instrument Division (Glenn Engel)
21 * Jim Kingdon, Cygnus Support.
23 * Original KGDB stub: David Grothe <dave@gcom.com>,
24 * Tigran Aivazian <tigran@sco.com>
26 * This file is licensed under the terms of the GNU General Public License
27 * version 2. This program is licensed "as is" without any warranty of any
28 * kind, whether express or implied.
30 #include <linux/pid_namespace.h>
31 #include <linux/clocksource.h>
32 #include <linux/interrupt.h>
33 #include <linux/spinlock.h>
34 #include <linux/console.h>
35 #include <linux/threads.h>
36 #include <linux/uaccess.h>
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/ptrace.h>
40 #include <linux/reboot.h>
41 #include <linux/string.h>
42 #include <linux/delay.h>
43 #include <linux/sched.h>
44 #include <linux/sysrq.h>
45 #include <linux/init.h>
46 #include <linux/kgdb.h>
47 #include <linux/pid.h>
48 #include <linux/smp.h>
51 #include <asm/cacheflush.h>
52 #include <asm/byteorder.h>
53 #include <asm/atomic.h>
54 #include <asm/system.h>
55 #include <asm/unaligned.h>
57 static int kgdb_break_asap
;
59 #define KGDB_MAX_THREAD_QUERY 17
66 unsigned long thr_query
;
67 unsigned long threadid
;
68 long kgdb_usethreadid
;
69 struct pt_regs
*linux_regs
;
72 static struct debuggerinfo_struct
{
74 struct task_struct
*task
;
78 * kgdb_connected - Is a host GDB connected to us?
81 EXPORT_SYMBOL_GPL(kgdb_connected
);
83 /* All the KGDB handlers are installed */
84 static int kgdb_io_module_registered
;
86 /* Guard for recursive entry */
87 static int exception_level
;
89 static struct kgdb_io
*kgdb_io_ops
;
90 static DEFINE_SPINLOCK(kgdb_registration_lock
);
92 /* kgdb console driver is loaded */
93 static int kgdb_con_registered
;
94 /* determine if kgdb console output should be used */
95 static int kgdb_use_con
;
97 static int __init
opt_kgdb_con(char *str
)
103 early_param("kgdbcon", opt_kgdb_con
);
105 module_param(kgdb_use_con
, int, 0644);
108 * Holds information about breakpoints in a kernel. These breakpoints are
109 * added and removed by gdb.
111 static struct kgdb_bkpt kgdb_break
[KGDB_MAX_BREAKPOINTS
] = {
112 [0 ... KGDB_MAX_BREAKPOINTS
-1] = { .state
= BP_UNDEFINED
}
116 * The CPU# of the active CPU, or -1 if none:
118 atomic_t kgdb_active
= ATOMIC_INIT(-1);
121 * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
122 * bootup code (which might not have percpu set up yet):
124 static atomic_t passive_cpu_wait
[NR_CPUS
];
125 static atomic_t cpu_in_kgdb
[NR_CPUS
];
126 atomic_t kgdb_setting_breakpoint
;
128 struct task_struct
*kgdb_usethread
;
129 struct task_struct
*kgdb_contthread
;
131 int kgdb_single_step
;
133 /* Our I/O buffers. */
134 static char remcom_in_buffer
[BUFMAX
];
135 static char remcom_out_buffer
[BUFMAX
];
137 /* Storage for the registers, in GDB format. */
138 static unsigned long gdb_regs
[(NUMREGBYTES
+
139 sizeof(unsigned long) - 1) /
140 sizeof(unsigned long)];
142 /* to keep track of the CPU which is doing the single stepping*/
143 atomic_t kgdb_cpu_doing_single_step
= ATOMIC_INIT(-1);
146 * If you are debugging a problem where roundup (the collection of
147 * all other CPUs) is a problem [this should be extremely rare],
148 * then use the nokgdbroundup option to avoid roundup. In that case
149 * the other CPUs might interfere with your debugging context, so
150 * use this with care:
152 static int kgdb_do_roundup
= 1;
154 static int __init
opt_nokgdbroundup(char *str
)
161 early_param("nokgdbroundup", opt_nokgdbroundup
);
164 * Finally, some KGDB code :-)
168 * Weak aliases for breakpoint management,
169 * can be overriden by architectures when needed:
171 int __weak
kgdb_arch_set_breakpoint(unsigned long addr
, char *saved_instr
)
175 err
= probe_kernel_read(saved_instr
, (char *)addr
, BREAK_INSTR_SIZE
);
179 return probe_kernel_write((char *)addr
, arch_kgdb_ops
.gdb_bpt_instr
,
183 int __weak
kgdb_arch_remove_breakpoint(unsigned long addr
, char *bundle
)
185 return probe_kernel_write((char *)addr
,
186 (char *)bundle
, BREAK_INSTR_SIZE
);
189 int __weak
kgdb_validate_break_address(unsigned long addr
)
191 char tmp_variable
[BREAK_INSTR_SIZE
];
193 /* Validate setting the breakpoint and then removing it. In the
194 * remove fails, the kernel needs to emit a bad message because we
195 * are deep trouble not being able to put things back the way we
198 err
= kgdb_arch_set_breakpoint(addr
, tmp_variable
);
201 err
= kgdb_arch_remove_breakpoint(addr
, tmp_variable
);
203 printk(KERN_ERR
"KGDB: Critical breakpoint error, kernel "
204 "memory destroyed at: %lx", addr
);
208 unsigned long __weak
kgdb_arch_pc(int exception
, struct pt_regs
*regs
)
210 return instruction_pointer(regs
);
213 int __weak
kgdb_arch_init(void)
218 int __weak
kgdb_skipexception(int exception
, struct pt_regs
*regs
)
224 kgdb_post_primary_code(struct pt_regs
*regs
, int e_vector
, int err_code
)
230 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
231 * @regs: Current &struct pt_regs.
233 * This function will be called if the particular architecture must
234 * disable hardware debugging while it is processing gdb packets or
235 * handling exception.
237 void __weak
kgdb_disable_hw_debug(struct pt_regs
*regs
)
242 * GDB remote protocol parser:
245 static int hex(char ch
)
247 if ((ch
>= 'a') && (ch
<= 'f'))
248 return ch
- 'a' + 10;
249 if ((ch
>= '0') && (ch
<= '9'))
251 if ((ch
>= 'A') && (ch
<= 'F'))
252 return ch
- 'A' + 10;
256 /* scan for the sequence $<data>#<checksum> */
257 static void get_packet(char *buffer
)
259 unsigned char checksum
;
260 unsigned char xmitcsum
;
266 * Spin and wait around for the start character, ignore all
269 while ((ch
= (kgdb_io_ops
->read_char())) != '$')
279 * now, read until a # or end of buffer is found:
281 while (count
< (BUFMAX
- 1)) {
282 ch
= kgdb_io_ops
->read_char();
285 checksum
= checksum
+ ch
;
292 xmitcsum
= hex(kgdb_io_ops
->read_char()) << 4;
293 xmitcsum
+= hex(kgdb_io_ops
->read_char());
295 if (checksum
!= xmitcsum
)
296 /* failed checksum */
297 kgdb_io_ops
->write_char('-');
299 /* successful transfer */
300 kgdb_io_ops
->write_char('+');
301 if (kgdb_io_ops
->flush
)
302 kgdb_io_ops
->flush();
304 } while (checksum
!= xmitcsum
);
308 * Send the packet in buffer.
309 * Check for gdb connection if asked for.
311 static void put_packet(char *buffer
)
313 unsigned char checksum
;
318 * $<packet info>#<checksum>.
321 kgdb_io_ops
->write_char('$');
325 while ((ch
= buffer
[count
])) {
326 kgdb_io_ops
->write_char(ch
);
331 kgdb_io_ops
->write_char('#');
332 kgdb_io_ops
->write_char(hex_asc_hi(checksum
));
333 kgdb_io_ops
->write_char(hex_asc_lo(checksum
));
334 if (kgdb_io_ops
->flush
)
335 kgdb_io_ops
->flush();
337 /* Now see what we get in reply. */
338 ch
= kgdb_io_ops
->read_char();
341 ch
= kgdb_io_ops
->read_char();
343 /* If we get an ACK, we are done. */
348 * If we get the start of another packet, this means
349 * that GDB is attempting to reconnect. We will NAK
350 * the packet being sent, and stop trying to send this
354 kgdb_io_ops
->write_char('-');
355 if (kgdb_io_ops
->flush
)
356 kgdb_io_ops
->flush();
363 * Convert the memory pointed to by mem into hex, placing result in buf.
364 * Return a pointer to the last char put in buf (null). May return an error.
366 int kgdb_mem2hex(char *mem
, char *buf
, int count
)
372 * We use the upper half of buf as an intermediate buffer for the
373 * raw memory copy. Hex conversion will work against this one.
377 err
= probe_kernel_read(tmp
, mem
, count
);
380 buf
= pack_hex_byte(buf
, *tmp
);
392 * Copy the binary array pointed to by buf into mem. Fix $, #, and
393 * 0x7d escaped with 0x7d. Return a pointer to the character after
394 * the last byte written.
396 static int kgdb_ebin2mem(char *buf
, char *mem
, int count
)
401 while (count
-- > 0) {
406 err
= probe_kernel_write(mem
, &c
, 1);
417 * Convert the hex array pointed to by buf into binary to be placed in mem.
418 * Return a pointer to the character AFTER the last byte written.
419 * May return an error.
421 int kgdb_hex2mem(char *buf
, char *mem
, int count
)
427 * We use the upper half of buf as an intermediate buffer for the
428 * raw memory that is converted from hex.
430 tmp_raw
= buf
+ count
* 2;
432 tmp_hex
= tmp_raw
- 1;
433 while (tmp_hex
>= buf
) {
435 *tmp_raw
= hex(*tmp_hex
--);
436 *tmp_raw
|= hex(*tmp_hex
--) << 4;
439 return probe_kernel_write(mem
, tmp_raw
, count
);
443 * While we find nice hex chars, build a long_val.
444 * Return number of chars processed.
446 int kgdb_hex2long(char **ptr
, unsigned long *long_val
)
459 hex_val
= hex(**ptr
);
463 *long_val
= (*long_val
<< 4) | hex_val
;
469 *long_val
= -*long_val
;
474 /* Write memory due to an 'M' or 'X' packet. */
475 static int write_mem_msg(int binary
)
477 char *ptr
= &remcom_in_buffer
[1];
479 unsigned long length
;
482 if (kgdb_hex2long(&ptr
, &addr
) > 0 && *(ptr
++) == ',' &&
483 kgdb_hex2long(&ptr
, &length
) > 0 && *(ptr
++) == ':') {
485 err
= kgdb_ebin2mem(ptr
, (char *)addr
, length
);
487 err
= kgdb_hex2mem(ptr
, (char *)addr
, length
);
490 if (CACHE_FLUSH_IS_SAFE
)
491 flush_icache_range(addr
, addr
+ length
);
498 static void error_packet(char *pkt
, int error
)
502 pkt
[1] = hex_asc
[(error
/ 10)];
503 pkt
[2] = hex_asc
[(error
% 10)];
508 * Thread ID accessors. We represent a flat TID space to GDB, where
509 * the per CPU idle threads (which under Linux all have PID 0) are
510 * remapped to negative TIDs.
513 #define BUF_THREAD_ID_SIZE 16
515 static char *pack_threadid(char *pkt
, unsigned char *id
)
519 limit
= pkt
+ BUF_THREAD_ID_SIZE
;
521 pkt
= pack_hex_byte(pkt
, *id
++);
526 static void int_to_threadref(unsigned char *id
, int value
)
531 scan
= (unsigned char *)id
;
534 put_unaligned_be32(value
, scan
);
537 static struct task_struct
*getthread(struct pt_regs
*regs
, int tid
)
540 * Non-positive TIDs are remapped to the cpu shadow information
542 if (tid
== 0 || tid
== -1)
543 tid
= -atomic_read(&kgdb_active
) - 2;
545 if (kgdb_info
[-tid
- 2].task
)
546 return kgdb_info
[-tid
- 2].task
;
548 return idle_task(-tid
- 2);
552 * find_task_by_pid_ns() does not take the tasklist lock anymore
553 * but is nicely RCU locked - hence is a pretty resilient
556 return find_task_by_pid_ns(tid
, &init_pid_ns
);
560 * CPU debug state control:
564 static void kgdb_wait(struct pt_regs
*regs
)
569 local_irq_save(flags
);
570 cpu
= raw_smp_processor_id();
571 kgdb_info
[cpu
].debuggerinfo
= regs
;
572 kgdb_info
[cpu
].task
= current
;
574 * Make sure the above info reaches the primary CPU before
575 * our cpu_in_kgdb[] flag setting does:
578 atomic_set(&cpu_in_kgdb
[cpu
], 1);
580 /* Wait till primary CPU is done with debugging */
581 while (atomic_read(&passive_cpu_wait
[cpu
]))
584 kgdb_info
[cpu
].debuggerinfo
= NULL
;
585 kgdb_info
[cpu
].task
= NULL
;
587 /* fix up hardware debug registers on local cpu */
588 if (arch_kgdb_ops
.correct_hw_break
)
589 arch_kgdb_ops
.correct_hw_break();
591 /* Signal the primary CPU that we are done: */
592 atomic_set(&cpu_in_kgdb
[cpu
], 0);
593 touch_softlockup_watchdog();
594 clocksource_touch_watchdog();
595 local_irq_restore(flags
);
600 * Some architectures need cache flushes when we set/clear a
603 static void kgdb_flush_swbreak_addr(unsigned long addr
)
605 if (!CACHE_FLUSH_IS_SAFE
)
608 if (current
->mm
&& current
->mm
->mmap_cache
) {
609 flush_cache_range(current
->mm
->mmap_cache
,
610 addr
, addr
+ BREAK_INSTR_SIZE
);
612 /* Force flush instruction cache if it was outside the mm */
613 flush_icache_range(addr
, addr
+ BREAK_INSTR_SIZE
);
617 * SW breakpoint management:
619 static int kgdb_activate_sw_breakpoints(void)
625 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
626 if (kgdb_break
[i
].state
!= BP_SET
)
629 addr
= kgdb_break
[i
].bpt_addr
;
630 error
= kgdb_arch_set_breakpoint(addr
,
631 kgdb_break
[i
].saved_instr
);
635 kgdb_flush_swbreak_addr(addr
);
636 kgdb_break
[i
].state
= BP_ACTIVE
;
641 static int kgdb_set_sw_break(unsigned long addr
)
643 int err
= kgdb_validate_break_address(addr
);
650 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
651 if ((kgdb_break
[i
].state
== BP_SET
) &&
652 (kgdb_break
[i
].bpt_addr
== addr
))
655 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
656 if (kgdb_break
[i
].state
== BP_REMOVED
&&
657 kgdb_break
[i
].bpt_addr
== addr
) {
664 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
665 if (kgdb_break
[i
].state
== BP_UNDEFINED
) {
675 kgdb_break
[breakno
].state
= BP_SET
;
676 kgdb_break
[breakno
].type
= BP_BREAKPOINT
;
677 kgdb_break
[breakno
].bpt_addr
= addr
;
682 static int kgdb_deactivate_sw_breakpoints(void)
688 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
689 if (kgdb_break
[i
].state
!= BP_ACTIVE
)
691 addr
= kgdb_break
[i
].bpt_addr
;
692 error
= kgdb_arch_remove_breakpoint(addr
,
693 kgdb_break
[i
].saved_instr
);
697 kgdb_flush_swbreak_addr(addr
);
698 kgdb_break
[i
].state
= BP_SET
;
703 static int kgdb_remove_sw_break(unsigned long addr
)
707 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
708 if ((kgdb_break
[i
].state
== BP_SET
) &&
709 (kgdb_break
[i
].bpt_addr
== addr
)) {
710 kgdb_break
[i
].state
= BP_REMOVED
;
717 int kgdb_isremovedbreak(unsigned long addr
)
721 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
722 if ((kgdb_break
[i
].state
== BP_REMOVED
) &&
723 (kgdb_break
[i
].bpt_addr
== addr
))
729 static int remove_all_break(void)
735 /* Clear memory breakpoints. */
736 for (i
= 0; i
< KGDB_MAX_BREAKPOINTS
; i
++) {
737 if (kgdb_break
[i
].state
!= BP_ACTIVE
)
739 addr
= kgdb_break
[i
].bpt_addr
;
740 error
= kgdb_arch_remove_breakpoint(addr
,
741 kgdb_break
[i
].saved_instr
);
743 printk(KERN_ERR
"KGDB: breakpoint remove failed: %lx\n",
746 kgdb_break
[i
].state
= BP_UNDEFINED
;
749 /* Clear hardware breakpoints. */
750 if (arch_kgdb_ops
.remove_all_hw_break
)
751 arch_kgdb_ops
.remove_all_hw_break();
757 * Remap normal tasks to their real PID,
758 * CPU shadow threads are mapped to -CPU - 2
760 static inline int shadow_pid(int realpid
)
765 return -raw_smp_processor_id() - 2;
768 static char gdbmsgbuf
[BUFMAX
+ 1];
770 static void kgdb_msg_write(const char *s
, int len
)
779 /* Fill and send buffers... */
781 bufptr
= gdbmsgbuf
+ 1;
783 /* Calculate how many this time */
784 if ((len
<< 1) > (BUFMAX
- 2))
785 wcount
= (BUFMAX
- 2) >> 1;
789 /* Pack in hex chars */
790 for (i
= 0; i
< wcount
; i
++)
791 bufptr
= pack_hex_byte(bufptr
, s
[i
]);
799 put_packet(gdbmsgbuf
);
804 * Return true if there is a valid kgdb I/O module. Also if no
805 * debugger is attached a message can be printed to the console about
806 * waiting for the debugger to attach.
808 * The print_wait argument is only to be true when called from inside
809 * the core kgdb_handle_exception, because it will wait for the
810 * debugger to attach.
812 static int kgdb_io_ready(int print_wait
)
818 if (atomic_read(&kgdb_setting_breakpoint
))
821 printk(KERN_CRIT
"KGDB: Waiting for remote debugger\n");
826 * All the functions that start with gdb_cmd are the various
827 * operations to implement the handlers for the gdbserial protocol
828 * where KGDB is communicating with an external debugger
831 /* Handle the '?' status packets */
832 static void gdb_cmd_status(struct kgdb_state
*ks
)
835 * We know that this packet is only sent
836 * during initial connect. So to be safe,
837 * we clear out our breakpoints now in case
838 * GDB is reconnecting.
842 remcom_out_buffer
[0] = 'S';
843 pack_hex_byte(&remcom_out_buffer
[1], ks
->signo
);
846 /* Handle the 'g' get registers request */
847 static void gdb_cmd_getregs(struct kgdb_state
*ks
)
849 struct task_struct
*thread
;
850 void *local_debuggerinfo
;
853 thread
= kgdb_usethread
;
855 thread
= kgdb_info
[ks
->cpu
].task
;
856 local_debuggerinfo
= kgdb_info
[ks
->cpu
].debuggerinfo
;
858 local_debuggerinfo
= NULL
;
859 for_each_online_cpu(i
) {
861 * Try to find the task on some other
862 * or possibly this node if we do not
863 * find the matching task then we try
864 * to approximate the results.
866 if (thread
== kgdb_info
[i
].task
)
867 local_debuggerinfo
= kgdb_info
[i
].debuggerinfo
;
872 * All threads that don't have debuggerinfo should be
873 * in __schedule() sleeping, since all other CPUs
874 * are in kgdb_wait, and thus have debuggerinfo.
876 if (local_debuggerinfo
) {
877 pt_regs_to_gdb_regs(gdb_regs
, local_debuggerinfo
);
880 * Pull stuff saved during switch_to; nothing
881 * else is accessible (or even particularly
884 * This should be enough for a stack trace.
886 sleeping_thread_to_gdb_regs(gdb_regs
, thread
);
888 kgdb_mem2hex((char *)gdb_regs
, remcom_out_buffer
, NUMREGBYTES
);
891 /* Handle the 'G' set registers request */
892 static void gdb_cmd_setregs(struct kgdb_state
*ks
)
894 kgdb_hex2mem(&remcom_in_buffer
[1], (char *)gdb_regs
, NUMREGBYTES
);
896 if (kgdb_usethread
&& kgdb_usethread
!= current
) {
897 error_packet(remcom_out_buffer
, -EINVAL
);
899 gdb_regs_to_pt_regs(gdb_regs
, ks
->linux_regs
);
900 strcpy(remcom_out_buffer
, "OK");
904 /* Handle the 'm' memory read bytes */
905 static void gdb_cmd_memread(struct kgdb_state
*ks
)
907 char *ptr
= &remcom_in_buffer
[1];
908 unsigned long length
;
912 if (kgdb_hex2long(&ptr
, &addr
) > 0 && *ptr
++ == ',' &&
913 kgdb_hex2long(&ptr
, &length
) > 0) {
914 err
= kgdb_mem2hex((char *)addr
, remcom_out_buffer
, length
);
916 error_packet(remcom_out_buffer
, err
);
918 error_packet(remcom_out_buffer
, -EINVAL
);
922 /* Handle the 'M' memory write bytes */
923 static void gdb_cmd_memwrite(struct kgdb_state
*ks
)
925 int err
= write_mem_msg(0);
928 error_packet(remcom_out_buffer
, err
);
930 strcpy(remcom_out_buffer
, "OK");
933 /* Handle the 'X' memory binary write bytes */
934 static void gdb_cmd_binwrite(struct kgdb_state
*ks
)
936 int err
= write_mem_msg(1);
939 error_packet(remcom_out_buffer
, err
);
941 strcpy(remcom_out_buffer
, "OK");
944 /* Handle the 'D' or 'k', detach or kill packets */
945 static void gdb_cmd_detachkill(struct kgdb_state
*ks
)
949 /* The detach case */
950 if (remcom_in_buffer
[0] == 'D') {
951 error
= remove_all_break();
953 error_packet(remcom_out_buffer
, error
);
955 strcpy(remcom_out_buffer
, "OK");
958 put_packet(remcom_out_buffer
);
961 * Assume the kill case, with no exit code checking,
962 * trying to force detach the debugger:
969 /* Handle the 'R' reboot packets */
970 static int gdb_cmd_reboot(struct kgdb_state
*ks
)
972 /* For now, only honor R0 */
973 if (strcmp(remcom_in_buffer
, "R0") == 0) {
974 printk(KERN_CRIT
"Executing emergency reboot\n");
975 strcpy(remcom_out_buffer
, "OK");
976 put_packet(remcom_out_buffer
);
979 * Execution should not return from
980 * machine_emergency_restart()
982 machine_emergency_restart();
990 /* Handle the 'q' query packets */
991 static void gdb_cmd_query(struct kgdb_state
*ks
)
993 struct task_struct
*g
;
994 struct task_struct
*p
;
995 unsigned char thref
[8];
1001 switch (remcom_in_buffer
[1]) {
1004 if (memcmp(remcom_in_buffer
+ 2, "ThreadInfo", 10)) {
1005 error_packet(remcom_out_buffer
, -EINVAL
);
1010 remcom_out_buffer
[0] = 'm';
1011 ptr
= remcom_out_buffer
+ 1;
1012 if (remcom_in_buffer
[1] == 'f') {
1013 /* Each cpu is a shadow thread */
1014 for_each_online_cpu(cpu
) {
1016 int_to_threadref(thref
, -cpu
- 2);
1017 pack_threadid(ptr
, thref
);
1018 ptr
+= BUF_THREAD_ID_SIZE
;
1024 do_each_thread(g
, p
) {
1025 if (i
>= ks
->thr_query
&& !finished
) {
1026 int_to_threadref(thref
, p
->pid
);
1027 pack_threadid(ptr
, thref
);
1028 ptr
+= BUF_THREAD_ID_SIZE
;
1031 if (ks
->thr_query
% KGDB_MAX_THREAD_QUERY
== 0)
1035 } while_each_thread(g
, p
);
1041 /* Current thread id */
1042 strcpy(remcom_out_buffer
, "QC");
1043 ks
->threadid
= shadow_pid(current
->pid
);
1044 int_to_threadref(thref
, ks
->threadid
);
1045 pack_threadid(remcom_out_buffer
+ 2, thref
);
1048 if (memcmp(remcom_in_buffer
+ 1, "ThreadExtraInfo,", 16)) {
1049 error_packet(remcom_out_buffer
, -EINVAL
);
1053 ptr
= remcom_in_buffer
+ 17;
1054 kgdb_hex2long(&ptr
, &ks
->threadid
);
1055 if (!getthread(ks
->linux_regs
, ks
->threadid
)) {
1056 error_packet(remcom_out_buffer
, -EINVAL
);
1059 if ((int)ks
->threadid
> 0) {
1060 kgdb_mem2hex(getthread(ks
->linux_regs
,
1061 ks
->threadid
)->comm
,
1062 remcom_out_buffer
, 16);
1064 static char tmpstr
[23 + BUF_THREAD_ID_SIZE
];
1066 sprintf(tmpstr
, "shadowCPU%d",
1067 (int)(-ks
->threadid
- 2));
1068 kgdb_mem2hex(tmpstr
, remcom_out_buffer
, strlen(tmpstr
));
1074 /* Handle the 'H' task query packets */
1075 static void gdb_cmd_task(struct kgdb_state
*ks
)
1077 struct task_struct
*thread
;
1080 switch (remcom_in_buffer
[1]) {
1082 ptr
= &remcom_in_buffer
[2];
1083 kgdb_hex2long(&ptr
, &ks
->threadid
);
1084 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
1085 if (!thread
&& ks
->threadid
> 0) {
1086 error_packet(remcom_out_buffer
, -EINVAL
);
1089 kgdb_usethread
= thread
;
1090 ks
->kgdb_usethreadid
= ks
->threadid
;
1091 strcpy(remcom_out_buffer
, "OK");
1094 ptr
= &remcom_in_buffer
[2];
1095 kgdb_hex2long(&ptr
, &ks
->threadid
);
1096 if (!ks
->threadid
) {
1097 kgdb_contthread
= NULL
;
1099 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
1100 if (!thread
&& ks
->threadid
> 0) {
1101 error_packet(remcom_out_buffer
, -EINVAL
);
1104 kgdb_contthread
= thread
;
1106 strcpy(remcom_out_buffer
, "OK");
1111 /* Handle the 'T' thread query packets */
1112 static void gdb_cmd_thread(struct kgdb_state
*ks
)
1114 char *ptr
= &remcom_in_buffer
[1];
1115 struct task_struct
*thread
;
1117 kgdb_hex2long(&ptr
, &ks
->threadid
);
1118 thread
= getthread(ks
->linux_regs
, ks
->threadid
);
1120 strcpy(remcom_out_buffer
, "OK");
1122 error_packet(remcom_out_buffer
, -EINVAL
);
1125 /* Handle the 'z' or 'Z' breakpoint remove or set packets */
1126 static void gdb_cmd_break(struct kgdb_state
*ks
)
1129 * Since GDB-5.3, it's been drafted that '0' is a software
1130 * breakpoint, '1' is a hardware breakpoint, so let's do that.
1132 char *bpt_type
= &remcom_in_buffer
[1];
1133 char *ptr
= &remcom_in_buffer
[2];
1135 unsigned long length
;
1138 if (arch_kgdb_ops
.set_hw_breakpoint
&& *bpt_type
>= '1') {
1140 if (*bpt_type
> '4')
1143 if (*bpt_type
!= '0' && *bpt_type
!= '1')
1149 * Test if this is a hardware breakpoint, and
1152 if (*bpt_type
== '1' && !(arch_kgdb_ops
.flags
& KGDB_HW_BREAKPOINT
))
1156 if (*(ptr
++) != ',') {
1157 error_packet(remcom_out_buffer
, -EINVAL
);
1160 if (!kgdb_hex2long(&ptr
, &addr
)) {
1161 error_packet(remcom_out_buffer
, -EINVAL
);
1164 if (*(ptr
++) != ',' ||
1165 !kgdb_hex2long(&ptr
, &length
)) {
1166 error_packet(remcom_out_buffer
, -EINVAL
);
1170 if (remcom_in_buffer
[0] == 'Z' && *bpt_type
== '0')
1171 error
= kgdb_set_sw_break(addr
);
1172 else if (remcom_in_buffer
[0] == 'z' && *bpt_type
== '0')
1173 error
= kgdb_remove_sw_break(addr
);
1174 else if (remcom_in_buffer
[0] == 'Z')
1175 error
= arch_kgdb_ops
.set_hw_breakpoint(addr
,
1176 (int)length
, *bpt_type
- '0');
1177 else if (remcom_in_buffer
[0] == 'z')
1178 error
= arch_kgdb_ops
.remove_hw_breakpoint(addr
,
1179 (int) length
, *bpt_type
- '0');
1182 strcpy(remcom_out_buffer
, "OK");
1184 error_packet(remcom_out_buffer
, error
);
1187 /* Handle the 'C' signal / exception passing packets */
1188 static int gdb_cmd_exception_pass(struct kgdb_state
*ks
)
1190 /* C09 == pass exception
1191 * C15 == detach kgdb, pass exception
1193 if (remcom_in_buffer
[1] == '0' && remcom_in_buffer
[2] == '9') {
1195 ks
->pass_exception
= 1;
1196 remcom_in_buffer
[0] = 'c';
1198 } else if (remcom_in_buffer
[1] == '1' && remcom_in_buffer
[2] == '5') {
1200 ks
->pass_exception
= 1;
1201 remcom_in_buffer
[0] = 'D';
1207 error_packet(remcom_out_buffer
, -EINVAL
);
1211 /* Indicate fall through */
1216 * This function performs all gdbserial command procesing
1218 static int gdb_serial_stub(struct kgdb_state
*ks
)
1223 /* Clear the out buffer. */
1224 memset(remcom_out_buffer
, 0, sizeof(remcom_out_buffer
));
1226 if (kgdb_connected
) {
1227 unsigned char thref
[8];
1230 /* Reply to host that an exception has occurred */
1231 ptr
= remcom_out_buffer
;
1233 ptr
= pack_hex_byte(ptr
, ks
->signo
);
1234 ptr
+= strlen(strcpy(ptr
, "thread:"));
1235 int_to_threadref(thref
, shadow_pid(current
->pid
));
1236 ptr
= pack_threadid(ptr
, thref
);
1238 put_packet(remcom_out_buffer
);
1241 kgdb_usethread
= kgdb_info
[ks
->cpu
].task
;
1242 ks
->kgdb_usethreadid
= shadow_pid(kgdb_info
[ks
->cpu
].task
->pid
);
1243 ks
->pass_exception
= 0;
1248 /* Clear the out buffer. */
1249 memset(remcom_out_buffer
, 0, sizeof(remcom_out_buffer
));
1251 get_packet(remcom_in_buffer
);
1253 switch (remcom_in_buffer
[0]) {
1254 case '?': /* gdbserial status */
1257 case 'g': /* return the value of the CPU registers */
1258 gdb_cmd_getregs(ks
);
1260 case 'G': /* set the value of the CPU registers - return OK */
1261 gdb_cmd_setregs(ks
);
1263 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1264 gdb_cmd_memread(ks
);
1266 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1267 gdb_cmd_memwrite(ks
);
1269 case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1270 gdb_cmd_binwrite(ks
);
1272 /* kill or detach. KGDB should treat this like a
1275 case 'D': /* Debugger detach */
1276 case 'k': /* Debugger detach via kill */
1277 gdb_cmd_detachkill(ks
);
1278 goto default_handle
;
1279 case 'R': /* Reboot */
1280 if (gdb_cmd_reboot(ks
))
1281 goto default_handle
;
1283 case 'q': /* query command */
1286 case 'H': /* task related */
1289 case 'T': /* Query thread status */
1292 case 'z': /* Break point remove */
1293 case 'Z': /* Break point set */
1296 case 'C': /* Exception passing */
1297 tmp
= gdb_cmd_exception_pass(ks
);
1299 goto default_handle
;
1302 /* Fall through on tmp < 0 */
1303 case 'c': /* Continue packet */
1304 case 's': /* Single step packet */
1305 if (kgdb_contthread
&& kgdb_contthread
!= current
) {
1306 /* Can't switch threads in kgdb */
1307 error_packet(remcom_out_buffer
, -EINVAL
);
1310 kgdb_activate_sw_breakpoints();
1311 /* Fall through to default processing */
1314 error
= kgdb_arch_handle_exception(ks
->ex_vector
,
1321 * Leave cmd processing on error, detach,
1322 * kill, continue, or single step.
1324 if (error
>= 0 || remcom_in_buffer
[0] == 'D' ||
1325 remcom_in_buffer
[0] == 'k') {
1332 /* reply to the request */
1333 put_packet(remcom_out_buffer
);
1337 if (ks
->pass_exception
)
1342 static int kgdb_reenter_check(struct kgdb_state
*ks
)
1346 if (atomic_read(&kgdb_active
) != raw_smp_processor_id())
1349 /* Panic on recursive debugger calls: */
1351 addr
= kgdb_arch_pc(ks
->ex_vector
, ks
->linux_regs
);
1352 kgdb_deactivate_sw_breakpoints();
1355 * If the break point removed ok at the place exception
1356 * occurred, try to recover and print a warning to the end
1357 * user because the user planted a breakpoint in a place that
1358 * KGDB needs in order to function.
1360 if (kgdb_remove_sw_break(addr
) == 0) {
1361 exception_level
= 0;
1362 kgdb_skipexception(ks
->ex_vector
, ks
->linux_regs
);
1363 kgdb_activate_sw_breakpoints();
1364 printk(KERN_CRIT
"KGDB: re-enter error: breakpoint removed %lx\n",
1371 kgdb_skipexception(ks
->ex_vector
, ks
->linux_regs
);
1373 if (exception_level
> 1) {
1375 panic("Recursive entry to debugger");
1378 printk(KERN_CRIT
"KGDB: re-enter exception: ALL breakpoints killed\n");
1380 panic("Recursive entry to debugger");
1386 * kgdb_handle_exception() - main entry point from a kernel exception
1388 * Locking hierarchy:
1389 * interface locks, if any (begin_session)
1390 * kgdb lock (kgdb_active)
1393 kgdb_handle_exception(int evector
, int signo
, int ecode
, struct pt_regs
*regs
)
1395 struct kgdb_state kgdb_var
;
1396 struct kgdb_state
*ks
= &kgdb_var
;
1397 unsigned long flags
;
1401 ks
->cpu
= raw_smp_processor_id();
1402 ks
->ex_vector
= evector
;
1404 ks
->ex_vector
= evector
;
1405 ks
->err_code
= ecode
;
1406 ks
->kgdb_usethreadid
= 0;
1407 ks
->linux_regs
= regs
;
1409 if (kgdb_reenter_check(ks
))
1410 return 0; /* Ouch, double exception ! */
1414 * Interrupts will be restored by the 'trap return' code, except when
1417 local_irq_save(flags
);
1419 cpu
= raw_smp_processor_id();
1422 * Acquire the kgdb_active lock:
1424 while (atomic_cmpxchg(&kgdb_active
, -1, cpu
) != -1)
1428 * Do not start the debugger connection on this CPU if the last
1429 * instance of the exception handler wanted to come into the
1430 * debugger on a different CPU via a single step
1432 if (atomic_read(&kgdb_cpu_doing_single_step
) != -1 &&
1433 atomic_read(&kgdb_cpu_doing_single_step
) != cpu
) {
1435 atomic_set(&kgdb_active
, -1);
1436 touch_softlockup_watchdog();
1437 clocksource_touch_watchdog();
1438 local_irq_restore(flags
);
1443 if (!kgdb_io_ready(1)) {
1445 goto kgdb_restore
; /* No I/O connection, so resume the system */
1449 * Don't enter if we have hit a removed breakpoint.
1451 if (kgdb_skipexception(ks
->ex_vector
, ks
->linux_regs
))
1454 /* Call the I/O driver's pre_exception routine */
1455 if (kgdb_io_ops
->pre_exception
)
1456 kgdb_io_ops
->pre_exception();
1458 kgdb_info
[ks
->cpu
].debuggerinfo
= ks
->linux_regs
;
1459 kgdb_info
[ks
->cpu
].task
= current
;
1461 kgdb_disable_hw_debug(ks
->linux_regs
);
1464 * Get the passive CPU lock which will hold all the non-primary
1465 * CPU in a spin state while the debugger is active
1467 if (!kgdb_single_step
) {
1468 for (i
= 0; i
< NR_CPUS
; i
++)
1469 atomic_set(&passive_cpu_wait
[i
], 1);
1473 * spin_lock code is good enough as a barrier so we don't
1476 atomic_set(&cpu_in_kgdb
[ks
->cpu
], 1);
1479 /* Signal the other CPUs to enter kgdb_wait() */
1480 if ((!kgdb_single_step
) && kgdb_do_roundup
)
1481 kgdb_roundup_cpus(flags
);
1485 * Wait for the other CPUs to be notified and be waiting for us:
1487 for_each_online_cpu(i
) {
1488 while (!atomic_read(&cpu_in_kgdb
[i
]))
1493 * At this point the primary processor is completely
1494 * in the debugger and all secondary CPUs are quiescent
1496 kgdb_post_primary_code(ks
->linux_regs
, ks
->ex_vector
, ks
->err_code
);
1497 kgdb_deactivate_sw_breakpoints();
1498 kgdb_single_step
= 0;
1499 kgdb_contthread
= current
;
1500 exception_level
= 0;
1502 /* Talk to debugger with gdbserial protocol */
1503 error
= gdb_serial_stub(ks
);
1505 /* Call the I/O driver's post_exception routine */
1506 if (kgdb_io_ops
->post_exception
)
1507 kgdb_io_ops
->post_exception();
1509 kgdb_info
[ks
->cpu
].debuggerinfo
= NULL
;
1510 kgdb_info
[ks
->cpu
].task
= NULL
;
1511 atomic_set(&cpu_in_kgdb
[ks
->cpu
], 0);
1513 if (!kgdb_single_step
) {
1514 for (i
= NR_CPUS
-1; i
>= 0; i
--)
1515 atomic_set(&passive_cpu_wait
[i
], 0);
1517 * Wait till all the CPUs have quit
1518 * from the debugger.
1520 for_each_online_cpu(i
) {
1521 while (atomic_read(&cpu_in_kgdb
[i
]))
1527 /* Free kgdb_active */
1528 atomic_set(&kgdb_active
, -1);
1529 touch_softlockup_watchdog();
1530 clocksource_touch_watchdog();
1531 local_irq_restore(flags
);
1536 int kgdb_nmicallback(int cpu
, void *regs
)
1539 if (!atomic_read(&cpu_in_kgdb
[cpu
]) &&
1540 atomic_read(&kgdb_active
) != cpu
&&
1541 atomic_read(&cpu_in_kgdb
[atomic_read(&kgdb_active
)])) {
1542 kgdb_wait((struct pt_regs
*)regs
);
1549 static void kgdb_console_write(struct console
*co
, const char *s
,
1552 unsigned long flags
;
1554 /* If we're debugging, or KGDB has not connected, don't try
1556 if (!kgdb_connected
|| atomic_read(&kgdb_active
) != -1)
1559 local_irq_save(flags
);
1560 kgdb_msg_write(s
, count
);
1561 local_irq_restore(flags
);
1564 static struct console kgdbcons
= {
1566 .write
= kgdb_console_write
,
1567 .flags
= CON_PRINTBUFFER
| CON_ENABLED
,
1571 #ifdef CONFIG_MAGIC_SYSRQ
1572 static void sysrq_handle_gdb(int key
, struct tty_struct
*tty
)
1575 printk(KERN_CRIT
"ERROR: No KGDB I/O module available\n");
1578 if (!kgdb_connected
)
1579 printk(KERN_CRIT
"Entering KGDB\n");
1584 static struct sysrq_key_op sysrq_gdb_op
= {
1585 .handler
= sysrq_handle_gdb
,
1586 .help_msg
= "debug(G)",
1587 .action_msg
= "DEBUG",
1591 static void kgdb_register_callbacks(void)
1593 if (!kgdb_io_module_registered
) {
1594 kgdb_io_module_registered
= 1;
1596 #ifdef CONFIG_MAGIC_SYSRQ
1597 register_sysrq_key('g', &sysrq_gdb_op
);
1599 if (kgdb_use_con
&& !kgdb_con_registered
) {
1600 register_console(&kgdbcons
);
1601 kgdb_con_registered
= 1;
1606 static void kgdb_unregister_callbacks(void)
1609 * When this routine is called KGDB should unregister from the
1610 * panic handler and clean up, making sure it is not handling any
1611 * break exceptions at the time.
1613 if (kgdb_io_module_registered
) {
1614 kgdb_io_module_registered
= 0;
1616 #ifdef CONFIG_MAGIC_SYSRQ
1617 unregister_sysrq_key('g', &sysrq_gdb_op
);
1619 if (kgdb_con_registered
) {
1620 unregister_console(&kgdbcons
);
1621 kgdb_con_registered
= 0;
1626 static void kgdb_initial_breakpoint(void)
1628 kgdb_break_asap
= 0;
1630 printk(KERN_CRIT
"kgdb: Waiting for connection from remote gdb...\n");
1635 * kgdb_register_io_module - register KGDB IO module
1636 * @new_kgdb_io_ops: the io ops vector
1638 * Register it with the KGDB core.
1640 int kgdb_register_io_module(struct kgdb_io
*new_kgdb_io_ops
)
1644 spin_lock(&kgdb_registration_lock
);
1647 spin_unlock(&kgdb_registration_lock
);
1649 printk(KERN_ERR
"kgdb: Another I/O driver is already "
1650 "registered with KGDB.\n");
1654 if (new_kgdb_io_ops
->init
) {
1655 err
= new_kgdb_io_ops
->init();
1657 spin_unlock(&kgdb_registration_lock
);
1662 kgdb_io_ops
= new_kgdb_io_ops
;
1664 spin_unlock(&kgdb_registration_lock
);
1666 printk(KERN_INFO
"kgdb: Registered I/O driver %s.\n",
1667 new_kgdb_io_ops
->name
);
1670 kgdb_register_callbacks();
1672 if (kgdb_break_asap
)
1673 kgdb_initial_breakpoint();
1677 EXPORT_SYMBOL_GPL(kgdb_register_io_module
);
1680 * kkgdb_unregister_io_module - unregister KGDB IO module
1681 * @old_kgdb_io_ops: the io ops vector
1683 * Unregister it with the KGDB core.
1685 void kgdb_unregister_io_module(struct kgdb_io
*old_kgdb_io_ops
)
1687 BUG_ON(kgdb_connected
);
1690 * KGDB is no longer able to communicate out, so
1691 * unregister our callbacks and reset state.
1693 kgdb_unregister_callbacks();
1695 spin_lock(&kgdb_registration_lock
);
1697 WARN_ON_ONCE(kgdb_io_ops
!= old_kgdb_io_ops
);
1700 spin_unlock(&kgdb_registration_lock
);
1703 "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
1704 old_kgdb_io_ops
->name
);
1706 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module
);
1709 * kgdb_breakpoint - generate breakpoint exception
1711 * This function will generate a breakpoint exception. It is used at the
1712 * beginning of a program to sync up with a debugger and can be used
1713 * otherwise as a quick means to stop program execution and "break" into
1716 void kgdb_breakpoint(void)
1718 atomic_set(&kgdb_setting_breakpoint
, 1);
1719 wmb(); /* Sync point before breakpoint */
1720 arch_kgdb_breakpoint();
1721 wmb(); /* Sync point after breakpoint */
1722 atomic_set(&kgdb_setting_breakpoint
, 0);
1724 EXPORT_SYMBOL_GPL(kgdb_breakpoint
);
1726 static int __init
opt_kgdb_wait(char *str
)
1728 kgdb_break_asap
= 1;
1730 if (kgdb_io_module_registered
)
1731 kgdb_initial_breakpoint();
1736 early_param("kgdbwait", opt_kgdb_wait
);