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;
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 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)
204 * kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
205 * @regs: Current &struct pt_regs.
207 * This function will be called if the particular architecture must
208 * disable hardware debugging while it is processing gdb packets or
209 * handling exception.
211 void __weak kgdb_disable_hw_debug(struct pt_regs *regs)
216 * GDB remote protocol parser:
219 static const char hexchars[] = "0123456789abcdef";
221 static int hex(char ch)
223 if ((ch >= 'a') && (ch <= 'f'))
224 return ch - 'a' + 10;
225 if ((ch >= '0') && (ch <= '9'))
227 if ((ch >= 'A') && (ch <= 'F'))
228 return ch - 'A' + 10;
232 /* scan for the sequence $<data>#<checksum> */
233 static void get_packet(char *buffer)
235 unsigned char checksum;
236 unsigned char xmitcsum;
242 * Spin and wait around for the start character, ignore all
245 while ((ch = (kgdb_io_ops->read_char())) != '$')
255 * now, read until a # or end of buffer is found:
257 while (count < (BUFMAX - 1)) {
258 ch = kgdb_io_ops->read_char();
261 checksum = checksum + ch;
268 xmitcsum = hex(kgdb_io_ops->read_char()) << 4;
269 xmitcsum += hex(kgdb_io_ops->read_char());
271 if (checksum != xmitcsum)
272 /* failed checksum */
273 kgdb_io_ops->write_char('-');
275 /* successful transfer */
276 kgdb_io_ops->write_char('+');
277 if (kgdb_io_ops->flush)
278 kgdb_io_ops->flush();
280 } while (checksum != xmitcsum);
284 * Send the packet in buffer.
285 * Check for gdb connection if asked for.
287 static void put_packet(char *buffer)
289 unsigned char checksum;
294 * $<packet info>#<checksum>.
297 kgdb_io_ops->write_char('$');
301 while ((ch = buffer[count])) {
302 kgdb_io_ops->write_char(ch);
307 kgdb_io_ops->write_char('#');
308 kgdb_io_ops->write_char(hexchars[checksum >> 4]);
309 kgdb_io_ops->write_char(hexchars[checksum & 0xf]);
310 if (kgdb_io_ops->flush)
311 kgdb_io_ops->flush();
313 /* Now see what we get in reply. */
314 ch = kgdb_io_ops->read_char();
317 ch = kgdb_io_ops->read_char();
319 /* If we get an ACK, we are done. */
324 * If we get the start of another packet, this means
325 * that GDB is attempting to reconnect. We will NAK
326 * the packet being sent, and stop trying to send this
330 kgdb_io_ops->write_char('-');
331 if (kgdb_io_ops->flush)
332 kgdb_io_ops->flush();
338 static char *pack_hex_byte(char *pkt, u8 byte)
340 *pkt++ = hexchars[byte >> 4];
341 *pkt++ = hexchars[byte & 0xf];
347 * Convert the memory pointed to by mem into hex, placing result in buf.
348 * Return a pointer to the last char put in buf (null). May return an error.
350 int kgdb_mem2hex(char *mem, char *buf, int count)
356 * We use the upper half of buf as an intermediate buffer for the
357 * raw memory copy. Hex conversion will work against this one.
361 err = probe_kernel_read(tmp, mem, count);
364 buf = pack_hex_byte(buf, *tmp);
376 * Copy the binary array pointed to by buf into mem. Fix $, #, and
377 * 0x7d escaped with 0x7d. Return a pointer to the character after
378 * the last byte written.
380 static int kgdb_ebin2mem(char *buf, char *mem, int count)
385 while (count-- > 0) {
390 err = probe_kernel_write(mem, &c, 1);
401 * Convert the hex array pointed to by buf into binary to be placed in mem.
402 * Return a pointer to the character AFTER the last byte written.
403 * May return an error.
405 int kgdb_hex2mem(char *buf, char *mem, int count)
411 * We use the upper half of buf as an intermediate buffer for the
412 * raw memory that is converted from hex.
414 tmp_raw = buf + count * 2;
416 tmp_hex = tmp_raw - 1;
417 while (tmp_hex >= buf) {
419 *tmp_raw = hex(*tmp_hex--);
420 *tmp_raw |= hex(*tmp_hex--) << 4;
423 return probe_kernel_write(mem, tmp_raw, count);
427 * While we find nice hex chars, build a long_val.
428 * Return number of chars processed.
430 int kgdb_hex2long(char **ptr, long *long_val)
438 hex_val = hex(**ptr);
442 *long_val = (*long_val << 4) | hex_val;
450 /* Write memory due to an 'M' or 'X' packet. */
451 static int write_mem_msg(int binary)
453 char *ptr = &remcom_in_buffer[1];
455 unsigned long length;
458 if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' &&
459 kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') {
461 err = kgdb_ebin2mem(ptr, (char *)addr, length);
463 err = kgdb_hex2mem(ptr, (char *)addr, length);
466 if (CACHE_FLUSH_IS_SAFE)
467 flush_icache_range(addr, addr + length + 1);
474 static void error_packet(char *pkt, int error)
478 pkt[1] = hexchars[(error / 10)];
479 pkt[2] = hexchars[(error % 10)];
484 * Thread ID accessors. We represent a flat TID space to GDB, where
485 * the per CPU idle threads (which under Linux all have PID 0) are
486 * remapped to negative TIDs.
489 #define BUF_THREAD_ID_SIZE 16
491 static char *pack_threadid(char *pkt, unsigned char *id)
495 limit = pkt + BUF_THREAD_ID_SIZE;
497 pkt = pack_hex_byte(pkt, *id++);
502 static void int_to_threadref(unsigned char *id, int value)
507 scan = (unsigned char *)id;
510 *scan++ = (value >> 24) & 0xff;
511 *scan++ = (value >> 16) & 0xff;
512 *scan++ = (value >> 8) & 0xff;
513 *scan++ = (value & 0xff);
516 static struct task_struct *getthread(struct pt_regs *regs, int tid)
519 * Non-positive TIDs are remapped idle tasks:
522 return idle_task(-tid);
525 * find_task_by_pid_ns() does not take the tasklist lock anymore
526 * but is nicely RCU locked - hence is a pretty resilient
529 return find_task_by_pid_ns(tid, &init_pid_ns);
533 * CPU debug state control:
537 static void kgdb_wait(struct pt_regs *regs)
542 local_irq_save(flags);
543 cpu = raw_smp_processor_id();
544 kgdb_info[cpu].debuggerinfo = regs;
545 kgdb_info[cpu].task = current;
547 * Make sure the above info reaches the primary CPU before
548 * our cpu_in_kgdb[] flag setting does:
551 atomic_set(&cpu_in_kgdb[cpu], 1);
554 * The primary CPU must be active to enter here, but this is
555 * guard in case the primary CPU had not been selected if
556 * this was an entry via nmi.
558 while (atomic_read(&kgdb_active) == -1)
561 /* Wait till primary CPU goes completely into the debugger. */
562 while (!atomic_read(&cpu_in_kgdb[atomic_read(&kgdb_active)]))
565 /* Wait till primary CPU is done with debugging */
566 while (atomic_read(&passive_cpu_wait[cpu]))
569 kgdb_info[cpu].debuggerinfo = NULL;
570 kgdb_info[cpu].task = NULL;
572 /* fix up hardware debug registers on local cpu */
573 if (arch_kgdb_ops.correct_hw_break)
574 arch_kgdb_ops.correct_hw_break();
576 /* Signal the primary CPU that we are done: */
577 atomic_set(&cpu_in_kgdb[cpu], 0);
578 clocksource_touch_watchdog();
579 local_irq_restore(flags);
584 * Some architectures need cache flushes when we set/clear a
587 static void kgdb_flush_swbreak_addr(unsigned long addr)
589 if (!CACHE_FLUSH_IS_SAFE)
593 flush_cache_range(current->mm->mmap_cache,
594 addr, addr + BREAK_INSTR_SIZE);
596 flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
601 * SW breakpoint management:
603 static int kgdb_activate_sw_breakpoints(void)
609 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
610 if (kgdb_break[i].state != BP_SET)
613 addr = kgdb_break[i].bpt_addr;
614 error = kgdb_arch_set_breakpoint(addr,
615 kgdb_break[i].saved_instr);
619 kgdb_flush_swbreak_addr(addr);
620 kgdb_break[i].state = BP_ACTIVE;
625 static int kgdb_set_sw_break(unsigned long addr)
627 int err = kgdb_validate_break_address(addr);
634 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
635 if ((kgdb_break[i].state == BP_SET) &&
636 (kgdb_break[i].bpt_addr == addr))
639 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
640 if (kgdb_break[i].state == BP_REMOVED &&
641 kgdb_break[i].bpt_addr == addr) {
648 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
649 if (kgdb_break[i].state == BP_UNDEFINED) {
659 kgdb_break[breakno].state = BP_SET;
660 kgdb_break[breakno].type = BP_BREAKPOINT;
661 kgdb_break[breakno].bpt_addr = addr;
666 static int kgdb_deactivate_sw_breakpoints(void)
672 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
673 if (kgdb_break[i].state != BP_ACTIVE)
675 addr = kgdb_break[i].bpt_addr;
676 error = kgdb_arch_remove_breakpoint(addr,
677 kgdb_break[i].saved_instr);
681 kgdb_flush_swbreak_addr(addr);
682 kgdb_break[i].state = BP_SET;
687 static int kgdb_remove_sw_break(unsigned long addr)
691 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
692 if ((kgdb_break[i].state == BP_SET) &&
693 (kgdb_break[i].bpt_addr == addr)) {
694 kgdb_break[i].state = BP_REMOVED;
701 int kgdb_isremovedbreak(unsigned long addr)
705 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
706 if ((kgdb_break[i].state == BP_REMOVED) &&
707 (kgdb_break[i].bpt_addr == addr))
713 int remove_all_break(void)
719 /* Clear memory breakpoints. */
720 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
721 if (kgdb_break[i].state != BP_SET)
723 addr = kgdb_break[i].bpt_addr;
724 error = kgdb_arch_remove_breakpoint(addr,
725 kgdb_break[i].saved_instr);
728 kgdb_break[i].state = BP_REMOVED;
731 /* Clear hardware breakpoints. */
732 if (arch_kgdb_ops.remove_all_hw_break)
733 arch_kgdb_ops.remove_all_hw_break();
739 * Remap normal tasks to their real PID, idle tasks to -1 ... -NR_CPUs:
741 static inline int shadow_pid(int realpid)
746 return -1-raw_smp_processor_id();
749 static char gdbmsgbuf[BUFMAX + 1];
751 static void kgdb_msg_write(const char *s, int len)
760 /* Fill and send buffers... */
762 bufptr = gdbmsgbuf + 1;
764 /* Calculate how many this time */
765 if ((len << 1) > (BUFMAX - 2))
766 wcount = (BUFMAX - 2) >> 1;
770 /* Pack in hex chars */
771 for (i = 0; i < wcount; i++)
772 bufptr = pack_hex_byte(bufptr, s[i]);
780 put_packet(gdbmsgbuf);
785 * Return true if there is a valid kgdb I/O module. Also if no
786 * debugger is attached a message can be printed to the console about
787 * waiting for the debugger to attach.
789 * The print_wait argument is only to be true when called from inside
790 * the core kgdb_handle_exception, because it will wait for the
791 * debugger to attach.
793 static int kgdb_io_ready(int print_wait)
799 if (atomic_read(&kgdb_setting_breakpoint))
802 printk(KERN_CRIT "KGDB: Waiting for remote debugger\n");
807 * All the functions that start with gdb_cmd are the various
808 * operations to implement the handlers for the gdbserial protocol
809 * where KGDB is communicating with an external debugger
812 /* Handle the '?' status packets */
813 static void gdb_cmd_status(struct kgdb_state *ks)
816 * We know that this packet is only sent
817 * during initial connect. So to be safe,
818 * we clear out our breakpoints now in case
819 * GDB is reconnecting.
823 remcom_out_buffer[0] = 'S';
824 pack_hex_byte(&remcom_out_buffer[1], ks->signo);
827 /* Handle the 'g' get registers request */
828 static void gdb_cmd_getregs(struct kgdb_state *ks)
830 struct task_struct *thread;
831 void *local_debuggerinfo;
834 thread = kgdb_usethread;
836 thread = kgdb_info[ks->cpu].task;
837 local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
839 local_debuggerinfo = NULL;
840 for (i = 0; i < NR_CPUS; i++) {
842 * Try to find the task on some other
843 * or possibly this node if we do not
844 * find the matching task then we try
845 * to approximate the results.
847 if (thread == kgdb_info[i].task)
848 local_debuggerinfo = kgdb_info[i].debuggerinfo;
853 * All threads that don't have debuggerinfo should be
854 * in __schedule() sleeping, since all other CPUs
855 * are in kgdb_wait, and thus have debuggerinfo.
857 if (local_debuggerinfo) {
858 pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
861 * Pull stuff saved during switch_to; nothing
862 * else is accessible (or even particularly
865 * This should be enough for a stack trace.
867 sleeping_thread_to_gdb_regs(gdb_regs, thread);
869 kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
872 /* Handle the 'G' set registers request */
873 static void gdb_cmd_setregs(struct kgdb_state *ks)
875 kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
877 if (kgdb_usethread && kgdb_usethread != current) {
878 error_packet(remcom_out_buffer, -EINVAL);
880 gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
881 strcpy(remcom_out_buffer, "OK");
885 /* Handle the 'm' memory read bytes */
886 static void gdb_cmd_memread(struct kgdb_state *ks)
888 char *ptr = &remcom_in_buffer[1];
889 unsigned long length;
893 if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
894 kgdb_hex2long(&ptr, &length) > 0) {
895 err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
897 error_packet(remcom_out_buffer, err);
899 error_packet(remcom_out_buffer, -EINVAL);
903 /* Handle the 'M' memory write bytes */
904 static void gdb_cmd_memwrite(struct kgdb_state *ks)
906 int err = write_mem_msg(0);
909 error_packet(remcom_out_buffer, err);
911 strcpy(remcom_out_buffer, "OK");
914 /* Handle the 'X' memory binary write bytes */
915 static void gdb_cmd_binwrite(struct kgdb_state *ks)
917 int err = write_mem_msg(1);
920 error_packet(remcom_out_buffer, err);
922 strcpy(remcom_out_buffer, "OK");
925 /* Handle the 'D' or 'k', detach or kill packets */
926 static void gdb_cmd_detachkill(struct kgdb_state *ks)
930 /* The detach case */
931 if (remcom_in_buffer[0] == 'D') {
932 error = remove_all_break();
934 error_packet(remcom_out_buffer, error);
936 strcpy(remcom_out_buffer, "OK");
939 put_packet(remcom_out_buffer);
942 * Assume the kill case, with no exit code checking,
943 * trying to force detach the debugger:
950 /* Handle the 'R' reboot packets */
951 static int gdb_cmd_reboot(struct kgdb_state *ks)
953 /* For now, only honor R0 */
954 if (strcmp(remcom_in_buffer, "R0") == 0) {
955 printk(KERN_CRIT "Executing emergency reboot\n");
956 strcpy(remcom_out_buffer, "OK");
957 put_packet(remcom_out_buffer);
960 * Execution should not return from
961 * machine_emergency_restart()
963 machine_emergency_restart();
971 /* Handle the 'q' query packets */
972 static void gdb_cmd_query(struct kgdb_state *ks)
974 struct task_struct *thread;
975 unsigned char thref[8];
979 switch (remcom_in_buffer[1]) {
982 if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) {
983 error_packet(remcom_out_buffer, -EINVAL);
987 if (remcom_in_buffer[1] == 'f')
990 remcom_out_buffer[0] = 'm';
991 ptr = remcom_out_buffer + 1;
993 for (i = 0; i < 17; ks->threadid++) {
994 thread = getthread(ks->linux_regs, ks->threadid);
996 int_to_threadref(thref, ks->threadid);
997 pack_threadid(ptr, thref);
998 ptr += BUF_THREAD_ID_SIZE;
1007 /* Current thread id */
1008 strcpy(remcom_out_buffer, "QC");
1009 ks->threadid = shadow_pid(current->pid);
1010 int_to_threadref(thref, ks->threadid);
1011 pack_threadid(remcom_out_buffer + 2, thref);
1014 if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16)) {
1015 error_packet(remcom_out_buffer, -EINVAL);
1019 ptr = remcom_in_buffer + 17;
1020 kgdb_hex2long(&ptr, &ks->threadid);
1021 if (!getthread(ks->linux_regs, ks->threadid)) {
1022 error_packet(remcom_out_buffer, -EINVAL);
1025 if (ks->threadid > 0) {
1026 kgdb_mem2hex(getthread(ks->linux_regs,
1027 ks->threadid)->comm,
1028 remcom_out_buffer, 16);
1030 static char tmpstr[23 + BUF_THREAD_ID_SIZE];
1032 sprintf(tmpstr, "Shadow task %d for pid 0",
1033 (int)(-ks->threadid-1));
1034 kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
1040 /* Handle the 'H' task query packets */
1041 static void gdb_cmd_task(struct kgdb_state *ks)
1043 struct task_struct *thread;
1046 switch (remcom_in_buffer[1]) {
1048 ptr = &remcom_in_buffer[2];
1049 kgdb_hex2long(&ptr, &ks->threadid);
1050 thread = getthread(ks->linux_regs, ks->threadid);
1051 if (!thread && ks->threadid > 0) {
1052 error_packet(remcom_out_buffer, -EINVAL);
1055 kgdb_usethread = thread;
1056 ks->kgdb_usethreadid = ks->threadid;
1057 strcpy(remcom_out_buffer, "OK");
1060 ptr = &remcom_in_buffer[2];
1061 kgdb_hex2long(&ptr, &ks->threadid);
1062 if (!ks->threadid) {
1063 kgdb_contthread = NULL;
1065 thread = getthread(ks->linux_regs, ks->threadid);
1066 if (!thread && ks->threadid > 0) {
1067 error_packet(remcom_out_buffer, -EINVAL);
1070 kgdb_contthread = thread;
1072 strcpy(remcom_out_buffer, "OK");
1077 /* Handle the 'T' thread query packets */
1078 static void gdb_cmd_thread(struct kgdb_state *ks)
1080 char *ptr = &remcom_in_buffer[1];
1081 struct task_struct *thread;
1083 kgdb_hex2long(&ptr, &ks->threadid);
1084 thread = getthread(ks->linux_regs, ks->threadid);
1086 strcpy(remcom_out_buffer, "OK");
1088 error_packet(remcom_out_buffer, -EINVAL);
1091 /* Handle the 'z' or 'Z' breakpoint remove or set packets */
1092 static void gdb_cmd_break(struct kgdb_state *ks)
1095 * Since GDB-5.3, it's been drafted that '0' is a software
1096 * breakpoint, '1' is a hardware breakpoint, so let's do that.
1098 char *bpt_type = &remcom_in_buffer[1];
1099 char *ptr = &remcom_in_buffer[2];
1101 unsigned long length;
1104 if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
1106 if (*bpt_type > '4')
1109 if (*bpt_type != '0' && *bpt_type != '1')
1115 * Test if this is a hardware breakpoint, and
1118 if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
1122 if (*(ptr++) != ',') {
1123 error_packet(remcom_out_buffer, -EINVAL);
1126 if (!kgdb_hex2long(&ptr, &addr)) {
1127 error_packet(remcom_out_buffer, -EINVAL);
1130 if (*(ptr++) != ',' ||
1131 !kgdb_hex2long(&ptr, &length)) {
1132 error_packet(remcom_out_buffer, -EINVAL);
1136 if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
1137 error = kgdb_set_sw_break(addr);
1138 else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
1139 error = kgdb_remove_sw_break(addr);
1140 else if (remcom_in_buffer[0] == 'Z')
1141 error = arch_kgdb_ops.set_hw_breakpoint(addr,
1142 (int)length, *bpt_type);
1143 else if (remcom_in_buffer[0] == 'z')
1144 error = arch_kgdb_ops.remove_hw_breakpoint(addr,
1145 (int) length, *bpt_type);
1148 strcpy(remcom_out_buffer, "OK");
1150 error_packet(remcom_out_buffer, error);
1153 /* Handle the 'C' signal / exception passing packets */
1154 static int gdb_cmd_exception_pass(struct kgdb_state *ks)
1156 /* C09 == pass exception
1157 * C15 == detach kgdb, pass exception
1159 if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
1161 ks->pass_exception = 1;
1162 remcom_in_buffer[0] = 'c';
1164 } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
1166 ks->pass_exception = 1;
1167 remcom_in_buffer[0] = 'D';
1173 error_packet(remcom_out_buffer, -EINVAL);
1177 /* Indicate fall through */
1182 * This function performs all gdbserial command procesing
1184 static int gdb_serial_stub(struct kgdb_state *ks)
1189 /* Clear the out buffer. */
1190 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
1192 if (kgdb_connected) {
1193 unsigned char thref[8];
1196 /* Reply to host that an exception has occurred */
1197 ptr = remcom_out_buffer;
1199 ptr = pack_hex_byte(ptr, ks->signo);
1200 ptr += strlen(strcpy(ptr, "thread:"));
1201 int_to_threadref(thref, shadow_pid(current->pid));
1202 ptr = pack_threadid(ptr, thref);
1204 put_packet(remcom_out_buffer);
1207 kgdb_usethread = kgdb_info[ks->cpu].task;
1208 ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
1209 ks->pass_exception = 0;
1214 /* Clear the out buffer. */
1215 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
1217 get_packet(remcom_in_buffer);
1219 switch (remcom_in_buffer[0]) {
1220 case '?': /* gdbserial status */
1223 case 'g': /* return the value of the CPU registers */
1224 gdb_cmd_getregs(ks);
1226 case 'G': /* set the value of the CPU registers - return OK */
1227 gdb_cmd_setregs(ks);
1229 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1230 gdb_cmd_memread(ks);
1232 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1233 gdb_cmd_memwrite(ks);
1235 case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
1236 gdb_cmd_binwrite(ks);
1238 /* kill or detach. KGDB should treat this like a
1241 case 'D': /* Debugger detach */
1242 case 'k': /* Debugger detach via kill */
1243 gdb_cmd_detachkill(ks);
1244 goto default_handle;
1245 case 'R': /* Reboot */
1246 if (gdb_cmd_reboot(ks))
1247 goto default_handle;
1249 case 'q': /* query command */
1252 case 'H': /* task related */
1255 case 'T': /* Query thread status */
1258 case 'z': /* Break point remove */
1259 case 'Z': /* Break point set */
1262 case 'C': /* Exception passing */
1263 tmp = gdb_cmd_exception_pass(ks);
1265 goto default_handle;
1268 /* Fall through on tmp < 0 */
1269 case 'c': /* Continue packet */
1270 case 's': /* Single step packet */
1271 if (kgdb_contthread && kgdb_contthread != current) {
1272 /* Can't switch threads in kgdb */
1273 error_packet(remcom_out_buffer, -EINVAL);
1276 kgdb_activate_sw_breakpoints();
1277 /* Fall through to default processing */
1280 error = kgdb_arch_handle_exception(ks->ex_vector,
1287 * Leave cmd processing on error, detach,
1288 * kill, continue, or single step.
1290 if (error >= 0 || remcom_in_buffer[0] == 'D' ||
1291 remcom_in_buffer[0] == 'k') {
1298 /* reply to the request */
1299 put_packet(remcom_out_buffer);
1303 if (ks->pass_exception)
1308 static int kgdb_reenter_check(struct kgdb_state *ks)
1312 if (atomic_read(&kgdb_active) != raw_smp_processor_id())
1315 /* Panic on recursive debugger calls: */
1317 addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
1318 kgdb_deactivate_sw_breakpoints();
1321 * If the break point removed ok at the place exception
1322 * occurred, try to recover and print a warning to the end
1323 * user because the user planted a breakpoint in a place that
1324 * KGDB needs in order to function.
1326 if (kgdb_remove_sw_break(addr) == 0) {
1327 exception_level = 0;
1328 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
1329 kgdb_activate_sw_breakpoints();
1330 printk(KERN_CRIT "KGDB: re-enter error: breakpoint removed %lx\n",
1337 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
1339 if (exception_level > 1) {
1341 panic("Recursive entry to debugger");
1344 printk(KERN_CRIT "KGDB: re-enter exception: ALL breakpoints killed\n");
1346 panic("Recursive entry to debugger");
1352 * kgdb_handle_exception() - main entry point from a kernel exception
1354 * Locking hierarchy:
1355 * interface locks, if any (begin_session)
1356 * kgdb lock (kgdb_active)
1359 kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
1361 struct kgdb_state kgdb_var;
1362 struct kgdb_state *ks = &kgdb_var;
1363 unsigned long flags;
1367 ks->cpu = raw_smp_processor_id();
1368 ks->ex_vector = evector;
1370 ks->ex_vector = evector;
1371 ks->err_code = ecode;
1372 ks->kgdb_usethreadid = 0;
1373 ks->linux_regs = regs;
1375 if (kgdb_reenter_check(ks))
1376 return 0; /* Ouch, double exception ! */
1380 * Interrupts will be restored by the 'trap return' code, except when
1383 local_irq_save(flags);
1385 cpu = raw_smp_processor_id();
1388 * Acquire the kgdb_active lock:
1390 while (atomic_cmpxchg(&kgdb_active, -1, cpu) != -1)
1394 * Do not start the debugger connection on this CPU if the last
1395 * instance of the exception handler wanted to come into the
1396 * debugger on a different CPU via a single step
1398 if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
1399 atomic_read(&kgdb_cpu_doing_single_step) != cpu) {
1401 atomic_set(&kgdb_active, -1);
1402 clocksource_touch_watchdog();
1403 local_irq_restore(flags);
1408 if (!kgdb_io_ready(1)) {
1410 goto kgdb_restore; /* No I/O connection, so resume the system */
1414 * Don't enter if we have hit a removed breakpoint.
1416 if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
1419 /* Call the I/O driver's pre_exception routine */
1420 if (kgdb_io_ops->pre_exception)
1421 kgdb_io_ops->pre_exception();
1423 kgdb_info[ks->cpu].debuggerinfo = ks->linux_regs;
1424 kgdb_info[ks->cpu].task = current;
1426 kgdb_disable_hw_debug(ks->linux_regs);
1429 * Get the passive CPU lock which will hold all the non-primary
1430 * CPU in a spin state while the debugger is active
1432 if (!kgdb_single_step || !kgdb_contthread) {
1433 for (i = 0; i < NR_CPUS; i++)
1434 atomic_set(&passive_cpu_wait[i], 1);
1438 /* Signal the other CPUs to enter kgdb_wait() */
1439 if ((!kgdb_single_step || !kgdb_contthread) && kgdb_do_roundup)
1440 kgdb_roundup_cpus(flags);
1444 * spin_lock code is good enough as a barrier so we don't
1447 atomic_set(&cpu_in_kgdb[ks->cpu], 1);
1450 * Wait for the other CPUs to be notified and be waiting for us:
1452 for_each_online_cpu(i) {
1453 while (!atomic_read(&cpu_in_kgdb[i]))
1458 * At this point the primary processor is completely
1459 * in the debugger and all secondary CPUs are quiescent
1461 kgdb_post_primary_code(ks->linux_regs, ks->ex_vector, ks->err_code);
1462 kgdb_deactivate_sw_breakpoints();
1463 kgdb_single_step = 0;
1464 kgdb_contthread = NULL;
1465 exception_level = 0;
1467 /* Talk to debugger with gdbserial protocol */
1468 error = gdb_serial_stub(ks);
1470 /* Call the I/O driver's post_exception routine */
1471 if (kgdb_io_ops->post_exception)
1472 kgdb_io_ops->post_exception();
1474 kgdb_info[ks->cpu].debuggerinfo = NULL;
1475 kgdb_info[ks->cpu].task = NULL;
1476 atomic_set(&cpu_in_kgdb[ks->cpu], 0);
1478 if (!kgdb_single_step || !kgdb_contthread) {
1479 for (i = NR_CPUS-1; i >= 0; i--)
1480 atomic_set(&passive_cpu_wait[i], 0);
1482 * Wait till all the CPUs have quit
1483 * from the debugger.
1485 for_each_online_cpu(i) {
1486 while (atomic_read(&cpu_in_kgdb[i]))
1492 /* Free kgdb_active */
1493 atomic_set(&kgdb_active, -1);
1494 clocksource_touch_watchdog();
1495 local_irq_restore(flags);
1500 int kgdb_nmicallback(int cpu, void *regs)
1503 if (!atomic_read(&cpu_in_kgdb[cpu]) &&
1504 atomic_read(&kgdb_active) != cpu) {
1505 kgdb_wait((struct pt_regs *)regs);
1512 void kgdb_console_write(struct console *co, const char *s, unsigned count)
1514 unsigned long flags;
1516 /* If we're debugging, or KGDB has not connected, don't try
1518 if (!kgdb_connected || atomic_read(&kgdb_active) != -1)
1521 local_irq_save(flags);
1522 kgdb_msg_write(s, count);
1523 local_irq_restore(flags);
1526 static struct console kgdbcons = {
1528 .write = kgdb_console_write,
1529 .flags = CON_PRINTBUFFER | CON_ENABLED,
1533 #ifdef CONFIG_MAGIC_SYSRQ
1534 static void sysrq_handle_gdb(int key, struct tty_struct *tty)
1537 printk(KERN_CRIT "ERROR: No KGDB I/O module available\n");
1540 if (!kgdb_connected)
1541 printk(KERN_CRIT "Entering KGDB\n");
1546 static struct sysrq_key_op sysrq_gdb_op = {
1547 .handler = sysrq_handle_gdb,
1549 .action_msg = "GDB",
1553 static void kgdb_register_callbacks(void)
1555 if (!kgdb_io_module_registered) {
1556 kgdb_io_module_registered = 1;
1558 #ifdef CONFIG_MAGIC_SYSRQ
1559 register_sysrq_key('g', &sysrq_gdb_op);
1561 if (kgdb_use_con && !kgdb_con_registered) {
1562 register_console(&kgdbcons);
1563 kgdb_con_registered = 1;
1568 static void kgdb_unregister_callbacks(void)
1571 * When this routine is called KGDB should unregister from the
1572 * panic handler and clean up, making sure it is not handling any
1573 * break exceptions at the time.
1575 if (kgdb_io_module_registered) {
1576 kgdb_io_module_registered = 0;
1578 #ifdef CONFIG_MAGIC_SYSRQ
1579 unregister_sysrq_key('g', &sysrq_gdb_op);
1581 if (kgdb_con_registered) {
1582 unregister_console(&kgdbcons);
1583 kgdb_con_registered = 0;
1588 static void kgdb_initial_breakpoint(void)
1590 kgdb_break_asap = 0;
1592 printk(KERN_CRIT "kgdb: Waiting for connection from remote gdb...\n");
1597 * kkgdb_register_io_module - register KGDB IO module
1598 * @new_kgdb_io_ops: the io ops vector
1600 * Register it with the KGDB core.
1602 int kgdb_register_io_module(struct kgdb_io *new_kgdb_io_ops)
1606 spin_lock(&kgdb_registration_lock);
1609 spin_unlock(&kgdb_registration_lock);
1611 printk(KERN_ERR "kgdb: Another I/O driver is already "
1612 "registered with KGDB.\n");
1616 if (new_kgdb_io_ops->init) {
1617 err = new_kgdb_io_ops->init();
1619 spin_unlock(&kgdb_registration_lock);
1624 kgdb_io_ops = new_kgdb_io_ops;
1626 spin_unlock(&kgdb_registration_lock);
1628 printk(KERN_INFO "kgdb: Registered I/O driver %s.\n",
1629 new_kgdb_io_ops->name);
1632 kgdb_register_callbacks();
1634 if (kgdb_break_asap)
1635 kgdb_initial_breakpoint();
1639 EXPORT_SYMBOL_GPL(kgdb_register_io_module);
1642 * kkgdb_unregister_io_module - unregister KGDB IO module
1643 * @old_kgdb_io_ops: the io ops vector
1645 * Unregister it with the KGDB core.
1647 void kgdb_unregister_io_module(struct kgdb_io *old_kgdb_io_ops)
1649 BUG_ON(kgdb_connected);
1652 * KGDB is no longer able to communicate out, so
1653 * unregister our callbacks and reset state.
1655 kgdb_unregister_callbacks();
1657 spin_lock(&kgdb_registration_lock);
1659 WARN_ON_ONCE(kgdb_io_ops != old_kgdb_io_ops);
1662 spin_unlock(&kgdb_registration_lock);
1665 "kgdb: Unregistered I/O driver %s, debugger disabled.\n",
1666 old_kgdb_io_ops->name);
1668 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
1671 * kgdb_breakpoint - generate breakpoint exception
1673 * This function will generate a breakpoint exception. It is used at the
1674 * beginning of a program to sync up with a debugger and can be used
1675 * otherwise as a quick means to stop program execution and "break" into
1678 void kgdb_breakpoint(void)
1680 atomic_set(&kgdb_setting_breakpoint, 1);
1681 wmb(); /* Sync point before breakpoint */
1682 arch_kgdb_breakpoint();
1683 wmb(); /* Sync point after breakpoint */
1684 atomic_set(&kgdb_setting_breakpoint, 0);
1686 EXPORT_SYMBOL_GPL(kgdb_breakpoint);
1688 static int __init opt_kgdb_wait(char *str)
1690 kgdb_break_asap = 1;
1692 if (kgdb_io_module_registered)
1693 kgdb_initial_breakpoint();
1698 early_param("kgdbwait", opt_kgdb_wait);