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@ucw.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-2009 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.
31 #define pr_fmt(fmt) "KGDB: " fmt
33 #include <linux/pid_namespace.h>
34 #include <linux/clocksource.h>
35 #include <linux/serial_core.h>
36 #include <linux/interrupt.h>
37 #include <linux/spinlock.h>
38 #include <linux/console.h>
39 #include <linux/threads.h>
40 #include <linux/uaccess.h>
41 #include <linux/kernel.h>
42 #include <linux/module.h>
43 #include <linux/ptrace.h>
44 #include <linux/string.h>
45 #include <linux/delay.h>
46 #include <linux/sched.h>
47 #include <linux/sysrq.h>
48 #include <linux/reboot.h>
49 #include <linux/init.h>
50 #include <linux/kgdb.h>
51 #include <linux/kdb.h>
52 #include <linux/nmi.h>
53 #include <linux/pid.h>
54 #include <linux/smp.h>
56 #include <linux/vmacache.h>
57 #include <linux/rcupdate.h>
58 #include <linux/irq.h>
59 #include <linux/security.h>
61 #include <asm/cacheflush.h>
62 #include <asm/byteorder.h>
63 #include <linux/atomic.h>
65 #include "debug_core.h"
67 static int kgdb_break_asap;
69 struct debuggerinfo_struct kgdb_info[NR_CPUS];
72 * kgdb_connected - Is a host GDB connected to us?
75 EXPORT_SYMBOL_GPL(kgdb_connected);
77 /* All the KGDB handlers are installed */
78 int kgdb_io_module_registered;
80 /* Guard for recursive entry */
81 static int exception_level;
83 struct kgdb_io *dbg_io_ops;
84 static DEFINE_SPINLOCK(kgdb_registration_lock);
86 /* Action for the reboot notifiter, a global allow kdb to change it */
87 static int kgdbreboot;
88 /* kgdb console driver is loaded */
89 static int kgdb_con_registered;
90 /* determine if kgdb console output should be used */
91 static int kgdb_use_con;
92 /* Flag for alternate operations for early debugging */
93 bool dbg_is_early = true;
94 /* Next cpu to become the master debug core */
97 /* Use kdb or gdbserver mode */
100 module_param(kgdb_use_con, int, 0644);
101 module_param(kgdbreboot, int, 0644);
104 * Holds information about breakpoints in a kernel. These breakpoints are
105 * added and removed by gdb.
107 static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = {
108 [0 ... KGDB_MAX_BREAKPOINTS-1] = { .state = BP_UNDEFINED }
112 * The CPU# of the active CPU, or -1 if none:
114 atomic_t kgdb_active = ATOMIC_INIT(-1);
115 EXPORT_SYMBOL_GPL(kgdb_active);
116 static DEFINE_RAW_SPINLOCK(dbg_master_lock);
117 static DEFINE_RAW_SPINLOCK(dbg_slave_lock);
120 * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
121 * bootup code (which might not have percpu set up yet):
123 static atomic_t masters_in_kgdb;
124 static atomic_t slaves_in_kgdb;
125 static atomic_t kgdb_break_tasklet_var;
126 atomic_t kgdb_setting_breakpoint;
128 struct task_struct *kgdb_usethread;
129 struct task_struct *kgdb_contthread;
131 int kgdb_single_step;
132 static pid_t kgdb_sstep_pid;
134 /* to keep track of the CPU which is doing the single stepping*/
135 atomic_t kgdb_cpu_doing_single_step = ATOMIC_INIT(-1);
138 * If you are debugging a problem where roundup (the collection of
139 * all other CPUs) is a problem [this should be extremely rare],
140 * then use the nokgdbroundup option to avoid roundup. In that case
141 * the other CPUs might interfere with your debugging context, so
142 * use this with care:
144 static int kgdb_do_roundup = 1;
146 static int __init opt_nokgdbroundup(char *str)
153 early_param("nokgdbroundup", opt_nokgdbroundup);
156 * Finally, some KGDB code :-)
160 * Weak aliases for breakpoint management,
161 * can be overriden by architectures when needed:
163 int __weak kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
167 err = probe_kernel_read(bpt->saved_instr, (char *)bpt->bpt_addr,
171 err = probe_kernel_write((char *)bpt->bpt_addr,
172 arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE);
176 int __weak kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
178 return probe_kernel_write((char *)bpt->bpt_addr,
179 (char *)bpt->saved_instr, BREAK_INSTR_SIZE);
182 int __weak kgdb_validate_break_address(unsigned long addr)
184 struct kgdb_bkpt tmp;
186 /* Validate setting the breakpoint and then removing it. If the
187 * remove fails, the kernel needs to emit a bad message because we
188 * are deep trouble not being able to put things back the way we
192 err = kgdb_arch_set_breakpoint(&tmp);
195 err = kgdb_arch_remove_breakpoint(&tmp);
197 pr_err("Critical breakpoint error, kernel memory destroyed at: %lx\n",
202 unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
204 return instruction_pointer(regs);
207 int __weak kgdb_arch_init(void)
212 int __weak kgdb_skipexception(int exception, struct pt_regs *regs)
220 * Default (weak) implementation for kgdb_roundup_cpus
223 static DEFINE_PER_CPU(call_single_data_t, kgdb_roundup_csd);
225 void __weak kgdb_call_nmi_hook(void *ignored)
228 * NOTE: get_irq_regs() is supposed to get the registers from
229 * before the IPI interrupt happened and so is supposed to
230 * show where the processor was. In some situations it's
231 * possible we might be called without an IPI, so it might be
232 * safer to figure out how to make kgdb_breakpoint() work
235 kgdb_nmicallback(raw_smp_processor_id(), get_irq_regs());
238 void __weak kgdb_roundup_cpus(void)
240 call_single_data_t *csd;
241 int this_cpu = raw_smp_processor_id();
245 for_each_online_cpu(cpu) {
246 /* No need to roundup ourselves */
250 csd = &per_cpu(kgdb_roundup_csd, cpu);
253 * If it didn't round up last time, don't try again
254 * since smp_call_function_single_async() will block.
256 * If rounding_up is false then we know that the
257 * previous call must have at least started and that
258 * means smp_call_function_single_async() won't block.
260 if (kgdb_info[cpu].rounding_up)
262 kgdb_info[cpu].rounding_up = true;
264 csd->func = kgdb_call_nmi_hook;
265 ret = smp_call_function_single_async(cpu, csd);
267 kgdb_info[cpu].rounding_up = false;
274 * Some architectures need cache flushes when we set/clear a
277 static void kgdb_flush_swbreak_addr(unsigned long addr)
279 if (!CACHE_FLUSH_IS_SAFE)
285 for (i = 0; i < VMACACHE_SIZE; i++) {
286 if (!current->vmacache.vmas[i])
288 flush_cache_range(current->vmacache.vmas[i],
289 addr, addr + BREAK_INSTR_SIZE);
293 /* Force flush instruction cache if it was outside the mm */
294 flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
298 * SW breakpoint management:
300 int dbg_activate_sw_breakpoints(void)
306 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
307 if (kgdb_break[i].state != BP_SET)
310 error = kgdb_arch_set_breakpoint(&kgdb_break[i]);
313 pr_info("BP install failed: %lx\n",
314 kgdb_break[i].bpt_addr);
318 kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr);
319 kgdb_break[i].state = BP_ACTIVE;
324 int dbg_set_sw_break(unsigned long addr)
326 int err = kgdb_validate_break_address(addr);
333 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
334 if ((kgdb_break[i].state == BP_SET) &&
335 (kgdb_break[i].bpt_addr == addr))
338 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
339 if (kgdb_break[i].state == BP_REMOVED &&
340 kgdb_break[i].bpt_addr == addr) {
347 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
348 if (kgdb_break[i].state == BP_UNDEFINED) {
358 kgdb_break[breakno].state = BP_SET;
359 kgdb_break[breakno].type = BP_BREAKPOINT;
360 kgdb_break[breakno].bpt_addr = addr;
365 int dbg_deactivate_sw_breakpoints(void)
371 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
372 if (kgdb_break[i].state != BP_ACTIVE)
374 error = kgdb_arch_remove_breakpoint(&kgdb_break[i]);
376 pr_info("BP remove failed: %lx\n",
377 kgdb_break[i].bpt_addr);
381 kgdb_flush_swbreak_addr(kgdb_break[i].bpt_addr);
382 kgdb_break[i].state = BP_SET;
387 int dbg_remove_sw_break(unsigned long addr)
391 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
392 if ((kgdb_break[i].state == BP_SET) &&
393 (kgdb_break[i].bpt_addr == addr)) {
394 kgdb_break[i].state = BP_REMOVED;
401 int kgdb_isremovedbreak(unsigned long addr)
405 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
406 if ((kgdb_break[i].state == BP_REMOVED) &&
407 (kgdb_break[i].bpt_addr == addr))
413 int dbg_remove_all_break(void)
418 /* Clear memory breakpoints. */
419 for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
420 if (kgdb_break[i].state != BP_ACTIVE)
422 error = kgdb_arch_remove_breakpoint(&kgdb_break[i]);
424 pr_err("breakpoint remove failed: %lx\n",
425 kgdb_break[i].bpt_addr);
427 kgdb_break[i].state = BP_UNDEFINED;
430 /* Clear hardware breakpoints. */
431 if (arch_kgdb_ops.remove_all_hw_break)
432 arch_kgdb_ops.remove_all_hw_break();
438 * Return true if there is a valid kgdb I/O module. Also if no
439 * debugger is attached a message can be printed to the console about
440 * waiting for the debugger to attach.
442 * The print_wait argument is only to be true when called from inside
443 * the core kgdb_handle_exception, because it will wait for the
444 * debugger to attach.
446 static int kgdb_io_ready(int print_wait)
452 if (atomic_read(&kgdb_setting_breakpoint))
455 #ifdef CONFIG_KGDB_KDB
457 pr_crit("waiting... or $3#33 for KDB\n");
459 pr_crit("Waiting for remote debugger\n");
465 static int kgdb_reenter_check(struct kgdb_state *ks)
469 if (atomic_read(&kgdb_active) != raw_smp_processor_id())
472 /* Panic on recursive debugger calls: */
474 addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
475 dbg_deactivate_sw_breakpoints();
478 * If the break point removed ok at the place exception
479 * occurred, try to recover and print a warning to the end
480 * user because the user planted a breakpoint in a place that
481 * KGDB needs in order to function.
483 if (dbg_remove_sw_break(addr) == 0) {
485 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
486 dbg_activate_sw_breakpoints();
487 pr_crit("re-enter error: breakpoint removed %lx\n", addr);
492 dbg_remove_all_break();
493 kgdb_skipexception(ks->ex_vector, ks->linux_regs);
495 if (exception_level > 1) {
497 kgdb_io_module_registered = false;
498 panic("Recursive entry to debugger");
501 pr_crit("re-enter exception: ALL breakpoints killed\n");
502 #ifdef CONFIG_KGDB_KDB
503 /* Allow kdb to debug itself one level */
507 panic("Recursive entry to debugger");
512 static void dbg_touch_watchdogs(void)
514 touch_softlockup_watchdog_sync();
515 clocksource_touch_watchdog();
516 rcu_cpu_stall_reset();
519 static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs,
523 int sstep_tries = 100;
527 int online_cpus = num_online_cpus();
530 kgdb_info[ks->cpu].enter_kgdb++;
531 kgdb_info[ks->cpu].exception_state |= exception_state;
533 if (exception_state == DCPU_WANT_MASTER)
534 atomic_inc(&masters_in_kgdb);
536 atomic_inc(&slaves_in_kgdb);
538 if (arch_kgdb_ops.disable_hw_break)
539 arch_kgdb_ops.disable_hw_break(regs);
544 * Interrupts will be restored by the 'trap return' code, except when
547 local_irq_save(flags);
550 kgdb_info[cpu].debuggerinfo = regs;
551 kgdb_info[cpu].task = current;
552 kgdb_info[cpu].ret_state = 0;
553 kgdb_info[cpu].irq_depth = hardirq_count() >> HARDIRQ_SHIFT;
555 /* Make sure the above info reaches the primary CPU */
558 if (exception_level == 1) {
559 if (raw_spin_trylock(&dbg_master_lock))
560 atomic_xchg(&kgdb_active, cpu);
561 goto cpu_master_loop;
565 * CPU will loop if it is a slave or request to become a kgdb
566 * master cpu and acquire the kgdb_active lock:
570 if (kgdb_info[cpu].exception_state & DCPU_NEXT_MASTER) {
571 kgdb_info[cpu].exception_state &= ~DCPU_NEXT_MASTER;
572 goto cpu_master_loop;
573 } else if (kgdb_info[cpu].exception_state & DCPU_WANT_MASTER) {
574 if (raw_spin_trylock(&dbg_master_lock)) {
575 atomic_xchg(&kgdb_active, cpu);
578 } else if (kgdb_info[cpu].exception_state & DCPU_IS_SLAVE) {
579 if (!raw_spin_is_locked(&dbg_slave_lock))
583 /* Return to normal operation by executing any
584 * hw breakpoint fixup.
586 if (arch_kgdb_ops.correct_hw_break)
587 arch_kgdb_ops.correct_hw_break();
590 kgdb_info[cpu].debuggerinfo = NULL;
591 kgdb_info[cpu].task = NULL;
592 kgdb_info[cpu].exception_state &=
593 ~(DCPU_WANT_MASTER | DCPU_IS_SLAVE);
594 kgdb_info[cpu].enter_kgdb--;
595 smp_mb__before_atomic();
596 atomic_dec(&slaves_in_kgdb);
597 dbg_touch_watchdogs();
598 local_irq_restore(flags);
606 * For single stepping, try to only enter on the processor
607 * that was single stepping. To guard against a deadlock, the
608 * kernel will only try for the value of sstep_tries before
609 * giving up and continuing on.
611 if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
612 (kgdb_info[cpu].task &&
613 kgdb_info[cpu].task->pid != kgdb_sstep_pid) && --sstep_tries) {
614 atomic_set(&kgdb_active, -1);
615 raw_spin_unlock(&dbg_master_lock);
616 dbg_touch_watchdogs();
617 local_irq_restore(flags);
623 if (!kgdb_io_ready(1)) {
624 kgdb_info[cpu].ret_state = 1;
625 goto kgdb_restore; /* No I/O connection, resume the system */
629 * Don't enter if we have hit a removed breakpoint.
631 if (kgdb_skipexception(ks->ex_vector, ks->linux_regs))
634 atomic_inc(&ignore_console_lock_warning);
636 /* Call the I/O driver's pre_exception routine */
637 if (dbg_io_ops->pre_exception)
638 dbg_io_ops->pre_exception();
641 * Get the passive CPU lock which will hold all the non-primary
642 * CPU in a spin state while the debugger is active
644 if (!kgdb_single_step)
645 raw_spin_lock(&dbg_slave_lock);
648 /* If send_ready set, slaves are already waiting */
650 atomic_set(ks->send_ready, 1);
652 /* Signal the other CPUs to enter kgdb_wait() */
653 else if ((!kgdb_single_step) && kgdb_do_roundup)
658 * Wait for the other CPUs to be notified and be waiting for us:
660 time_left = MSEC_PER_SEC;
661 while (kgdb_do_roundup && --time_left &&
662 (atomic_read(&masters_in_kgdb) + atomic_read(&slaves_in_kgdb)) !=
666 pr_crit("Timed out waiting for secondary CPUs.\n");
669 * At this point the primary processor is completely
670 * in the debugger and all secondary CPUs are quiescent
672 dbg_deactivate_sw_breakpoints();
673 kgdb_single_step = 0;
674 kgdb_contthread = current;
676 trace_on = tracing_is_on();
684 error = kdb_stub(ks);
690 * This is a brutal way to interfere with the debugger
691 * and prevent gdb being used to poke at kernel memory.
692 * This could cause trouble if lockdown is applied when
693 * there is already an active gdb session. For now the
694 * answer is simply "don't do that". Typically lockdown
695 * *will* be applied before the debug core gets started
696 * so only developers using kgdb for fairly advanced
697 * early kernel debug can be biten by this. Hopefully
698 * they are sophisticated enough to take care of
699 * themselves, especially with help from the lockdown
700 * message printed on the console!
702 if (security_locked_down(LOCKDOWN_DBG_WRITE_KERNEL)) {
703 if (IS_ENABLED(CONFIG_KGDB_KDB)) {
704 /* Switch back to kdb if possible... */
708 /* ... otherwise just bail */
712 error = gdb_serial_stub(ks);
715 if (error == DBG_PASS_EVENT) {
716 dbg_kdb_mode = !dbg_kdb_mode;
717 } else if (error == DBG_SWITCH_CPU_EVENT) {
718 kgdb_info[dbg_switch_cpu].exception_state |=
722 kgdb_info[cpu].ret_state = error;
727 /* Call the I/O driver's post_exception routine */
728 if (dbg_io_ops->post_exception)
729 dbg_io_ops->post_exception();
731 atomic_dec(&ignore_console_lock_warning);
733 if (!kgdb_single_step) {
734 raw_spin_unlock(&dbg_slave_lock);
735 /* Wait till all the CPUs have quit from the debugger. */
736 while (kgdb_do_roundup && atomic_read(&slaves_in_kgdb))
741 if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
742 int sstep_cpu = atomic_read(&kgdb_cpu_doing_single_step);
743 if (kgdb_info[sstep_cpu].task)
744 kgdb_sstep_pid = kgdb_info[sstep_cpu].task->pid;
748 if (arch_kgdb_ops.correct_hw_break)
749 arch_kgdb_ops.correct_hw_break();
753 kgdb_info[cpu].debuggerinfo = NULL;
754 kgdb_info[cpu].task = NULL;
755 kgdb_info[cpu].exception_state &=
756 ~(DCPU_WANT_MASTER | DCPU_IS_SLAVE);
757 kgdb_info[cpu].enter_kgdb--;
758 smp_mb__before_atomic();
759 atomic_dec(&masters_in_kgdb);
760 /* Free kgdb_active */
761 atomic_set(&kgdb_active, -1);
762 raw_spin_unlock(&dbg_master_lock);
763 dbg_touch_watchdogs();
764 local_irq_restore(flags);
767 return kgdb_info[cpu].ret_state;
771 * kgdb_handle_exception() - main entry point from a kernel exception
774 * interface locks, if any (begin_session)
775 * kgdb lock (kgdb_active)
778 kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
780 struct kgdb_state kgdb_var;
781 struct kgdb_state *ks = &kgdb_var;
784 if (arch_kgdb_ops.enable_nmi)
785 arch_kgdb_ops.enable_nmi(0);
787 * Avoid entering the debugger if we were triggered due to an oops
788 * but panic_timeout indicates the system should automatically
789 * reboot on panic. We don't want to get stuck waiting for input
790 * on such systems, especially if its "just" an oops.
792 if (signo != SIGTRAP && panic_timeout)
795 memset(ks, 0, sizeof(struct kgdb_state));
796 ks->cpu = raw_smp_processor_id();
797 ks->ex_vector = evector;
799 ks->err_code = ecode;
800 ks->linux_regs = regs;
802 if (kgdb_reenter_check(ks))
803 goto out; /* Ouch, double exception ! */
804 if (kgdb_info[ks->cpu].enter_kgdb != 0)
807 ret = kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER);
809 if (arch_kgdb_ops.enable_nmi)
810 arch_kgdb_ops.enable_nmi(1);
815 * GDB places a breakpoint at this function to know dynamically loaded objects.
817 static int module_event(struct notifier_block *self, unsigned long val,
823 static struct notifier_block dbg_module_load_nb = {
824 .notifier_call = module_event,
827 int kgdb_nmicallback(int cpu, void *regs)
830 struct kgdb_state kgdb_var;
831 struct kgdb_state *ks = &kgdb_var;
833 kgdb_info[cpu].rounding_up = false;
835 memset(ks, 0, sizeof(struct kgdb_state));
837 ks->linux_regs = regs;
839 if (kgdb_info[ks->cpu].enter_kgdb == 0 &&
840 raw_spin_is_locked(&dbg_master_lock)) {
841 kgdb_cpu_enter(ks, regs, DCPU_IS_SLAVE);
848 int kgdb_nmicallin(int cpu, int trapnr, void *regs, int err_code,
849 atomic_t *send_ready)
852 if (!kgdb_io_ready(0) || !send_ready)
855 if (kgdb_info[cpu].enter_kgdb == 0) {
856 struct kgdb_state kgdb_var;
857 struct kgdb_state *ks = &kgdb_var;
859 memset(ks, 0, sizeof(struct kgdb_state));
861 ks->ex_vector = trapnr;
863 ks->err_code = err_code;
864 ks->linux_regs = regs;
865 ks->send_ready = send_ready;
866 kgdb_cpu_enter(ks, regs, DCPU_WANT_MASTER);
873 static void kgdb_console_write(struct console *co, const char *s,
878 /* If we're debugging, or KGDB has not connected, don't try
880 if (!kgdb_connected || atomic_read(&kgdb_active) != -1 || dbg_kdb_mode)
883 local_irq_save(flags);
884 gdbstub_msg_write(s, count);
885 local_irq_restore(flags);
888 static struct console kgdbcons = {
890 .write = kgdb_console_write,
891 .flags = CON_PRINTBUFFER | CON_ENABLED,
895 static int __init opt_kgdb_con(char *str)
899 if (kgdb_io_module_registered && !kgdb_con_registered) {
900 register_console(&kgdbcons);
901 kgdb_con_registered = 1;
907 early_param("kgdbcon", opt_kgdb_con);
909 #ifdef CONFIG_MAGIC_SYSRQ
910 static void sysrq_handle_dbg(int key)
913 pr_crit("ERROR: No KGDB I/O module available\n");
916 if (!kgdb_connected) {
917 #ifdef CONFIG_KGDB_KDB
919 pr_crit("KGDB or $3#33 for KDB\n");
921 pr_crit("Entering KGDB\n");
928 static struct sysrq_key_op sysrq_dbg_op = {
929 .handler = sysrq_handle_dbg,
930 .help_msg = "debug(g)",
931 .action_msg = "DEBUG",
935 void kgdb_panic(const char *msg)
937 if (!kgdb_io_module_registered)
941 * We don't want to get stuck waiting for input from user if
942 * "panic_timeout" indicates the system should automatically
949 kdb_printf("PANIC: %s\n", msg);
954 void __weak kgdb_arch_late(void)
958 void __init dbg_late_init(void)
960 dbg_is_early = false;
961 if (kgdb_io_module_registered)
963 kdb_init(KDB_INIT_FULL);
967 dbg_notify_reboot(struct notifier_block *this, unsigned long code, void *x)
970 * Take the following action on reboot notify depending on value:
971 * 1 == Enter debugger
972 * 0 == [the default] detatch debug client
973 * -1 == Do nothing... and use this until the board resets
975 switch (kgdbreboot) {
987 static struct notifier_block dbg_reboot_notifier = {
988 .notifier_call = dbg_notify_reboot,
993 static void kgdb_register_callbacks(void)
995 if (!kgdb_io_module_registered) {
996 kgdb_io_module_registered = 1;
1000 register_module_notifier(&dbg_module_load_nb);
1001 register_reboot_notifier(&dbg_reboot_notifier);
1002 #ifdef CONFIG_MAGIC_SYSRQ
1003 register_sysrq_key('g', &sysrq_dbg_op);
1005 if (kgdb_use_con && !kgdb_con_registered) {
1006 register_console(&kgdbcons);
1007 kgdb_con_registered = 1;
1012 static void kgdb_unregister_callbacks(void)
1015 * When this routine is called KGDB should unregister from
1016 * handlers and clean up, making sure it is not handling any
1017 * break exceptions at the time.
1019 if (kgdb_io_module_registered) {
1020 kgdb_io_module_registered = 0;
1021 unregister_reboot_notifier(&dbg_reboot_notifier);
1022 unregister_module_notifier(&dbg_module_load_nb);
1024 #ifdef CONFIG_MAGIC_SYSRQ
1025 unregister_sysrq_key('g', &sysrq_dbg_op);
1027 if (kgdb_con_registered) {
1028 unregister_console(&kgdbcons);
1029 kgdb_con_registered = 0;
1035 * There are times a tasklet needs to be used vs a compiled in
1036 * break point so as to cause an exception outside a kgdb I/O module,
1037 * such as is the case with kgdboe, where calling a breakpoint in the
1038 * I/O driver itself would be fatal.
1040 static void kgdb_tasklet_bpt(unsigned long ing)
1043 atomic_set(&kgdb_break_tasklet_var, 0);
1046 static DECLARE_TASKLET_OLD(kgdb_tasklet_breakpoint, kgdb_tasklet_bpt);
1048 void kgdb_schedule_breakpoint(void)
1050 if (atomic_read(&kgdb_break_tasklet_var) ||
1051 atomic_read(&kgdb_active) != -1 ||
1052 atomic_read(&kgdb_setting_breakpoint))
1054 atomic_inc(&kgdb_break_tasklet_var);
1055 tasklet_schedule(&kgdb_tasklet_breakpoint);
1057 EXPORT_SYMBOL_GPL(kgdb_schedule_breakpoint);
1059 static void kgdb_initial_breakpoint(void)
1061 kgdb_break_asap = 0;
1063 pr_crit("Waiting for connection from remote gdb...\n");
1068 * kgdb_register_io_module - register KGDB IO module
1069 * @new_dbg_io_ops: the io ops vector
1071 * Register it with the KGDB core.
1073 int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops)
1077 spin_lock(&kgdb_registration_lock);
1080 spin_unlock(&kgdb_registration_lock);
1082 pr_err("Another I/O driver is already registered with KGDB\n");
1086 if (new_dbg_io_ops->init) {
1087 err = new_dbg_io_ops->init();
1089 spin_unlock(&kgdb_registration_lock);
1094 dbg_io_ops = new_dbg_io_ops;
1096 spin_unlock(&kgdb_registration_lock);
1098 pr_info("Registered I/O driver %s\n", new_dbg_io_ops->name);
1101 kgdb_register_callbacks();
1103 if (kgdb_break_asap)
1104 kgdb_initial_breakpoint();
1108 EXPORT_SYMBOL_GPL(kgdb_register_io_module);
1111 * kkgdb_unregister_io_module - unregister KGDB IO module
1112 * @old_dbg_io_ops: the io ops vector
1114 * Unregister it with the KGDB core.
1116 void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops)
1118 BUG_ON(kgdb_connected);
1121 * KGDB is no longer able to communicate out, so
1122 * unregister our callbacks and reset state.
1124 kgdb_unregister_callbacks();
1126 spin_lock(&kgdb_registration_lock);
1128 WARN_ON_ONCE(dbg_io_ops != old_dbg_io_ops);
1131 spin_unlock(&kgdb_registration_lock);
1133 pr_info("Unregistered I/O driver %s, debugger disabled\n",
1134 old_dbg_io_ops->name);
1136 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
1138 int dbg_io_get_char(void)
1140 int ret = dbg_io_ops->read_char();
1141 if (ret == NO_POLL_CHAR)
1151 * kgdb_breakpoint - generate breakpoint exception
1153 * This function will generate a breakpoint exception. It is used at the
1154 * beginning of a program to sync up with a debugger and can be used
1155 * otherwise as a quick means to stop program execution and "break" into
1158 noinline void kgdb_breakpoint(void)
1160 atomic_inc(&kgdb_setting_breakpoint);
1161 wmb(); /* Sync point before breakpoint */
1162 arch_kgdb_breakpoint();
1163 wmb(); /* Sync point after breakpoint */
1164 atomic_dec(&kgdb_setting_breakpoint);
1166 EXPORT_SYMBOL_GPL(kgdb_breakpoint);
1168 static int __init opt_kgdb_wait(char *str)
1170 kgdb_break_asap = 1;
1172 kdb_init(KDB_INIT_EARLY);
1173 if (kgdb_io_module_registered)
1174 kgdb_initial_breakpoint();
1179 early_param("kgdbwait", opt_kgdb_wait);