History

kgdb: core changes to support kdb

These are the minimum changes to the kgdb core in order to enable an API to connect a new front end (kdb) to the debug core. This patch introduces the dbg_kdb_mode variable controls where the user level I/O is routed. It will be routed to the gdbstub (kgdb) or to the kdb front end which is a simple shell available over the kgdboc connection. You can switch back and forth between kdb or the gdb stub mode of operation dynamically. From gdb stub mode you can blindly type "$3#33", or from the kdb mode you can enter "kgdb" to switch to the gdb stub. The logic in the debug core depends on kdb to look for the typical gdb connection sequences and return immediately with KGDB_PASS_EVENT if a gdb serial command sequence is detected. That should allow a reasonably seamless transition between kdb -> gdb without leaving the kernel exception state. The two gdb serial queries that kdb is responsible for detecting are the "?" and "qSupported" packets.

Jason Wessel committed 16 years ago

Commit dcc78711 parent 67fc4e09 files · +186 −20Browse files

Showing the changes to kernel/debug (3 of 9 files). Show all

Unified · Split

3 files
  1. kernel/debug/debug_core.c +90 −17
  2. kernel/debug/debug_core.h +24 −0
  3. kernel/debug/gdbstub.c +36 −0
kernel/debug/debug_core.c+90−17
@@ -43,6 +43,7 @@ · 42 unmodified lines
4343 #include <linux/sysrq.h>
4444 #include <linux/init.h>
4545 #include <linux/kgdb.h>
46+#include <linux/kdb.h>
4647 #include <linux/pid.h>
4748 #include <linux/smp.h>
4849 #include <linux/mm.h>
@@ -77,6 +78,11 @@ static DEFINE_SPINLOCK(kgdb_registration_lock); · 28 unmodified lines
7778 static int kgdb_con_registered;
7879 /* determine if kgdb console output should be used */
7980 static int kgdb_use_con;
81+/* Next cpu to become the master debug core */
82+int dbg_switch_cpu;
83+
84+/* Use kdb or gdbserver mode */
85+static int dbg_kdb_mode = 1;
8086
8187 static int __init opt_kgdb_con(char *str)
8288 {
@@ -100,6 +106,7 @@ static struct kgdb_bkpt kgdb_break[KGDB_MAX_BREAKPOINTS] = { · 17 unmodified lines
100106 * The CPU# of the active CPU, or -1 if none:
101107 */
102108 atomic_t kgdb_active = ATOMIC_INIT(-1);
109+EXPORT_SYMBOL_GPL(kgdb_active);
103110
104111 /*
105112 * We use NR_CPUs not PERCPU, in case kgdb is used to debug early
@@ -301,7 +308,7 @@ int dbg_set_sw_break(unsigned long addr) · 195 unmodified lines
301308 return 0;
302309 }
303310
304−static int kgdb_deactivate_sw_breakpoints(void)
311+int dbg_deactivate_sw_breakpoints(void)
305312 {
306313 unsigned long addr;
307314 int error;
@@ -395,8 +402,14 @@ static int kgdb_io_ready(int print_wait) · 87 unmodified lines
395402 return 1;
396403 if (atomic_read(&kgdb_setting_breakpoint))
397404 return 1;
398− if (print_wait)
405+ if (print_wait) {
406+#ifdef CONFIG_KGDB_KDB
407+ if (!dbg_kdb_mode)
408+ printk(KERN_CRIT "KGDB: waiting... or $3#33 for KDB\n");
409+#else
399410 printk(KERN_CRIT "KGDB: Waiting for remote debugger\n");
411+#endif
412+ }
400413 return 1;
401414 }
402415
@@ -410,7 +423,7 @@ static int kgdb_reenter_check(struct kgdb_state *ks) · 7 unmodified lines
410423 /* Panic on recursive debugger calls: */
411424 exception_level++;
412425 addr = kgdb_arch_pc(ks->ex_vector, ks->linux_regs);
413− kgdb_deactivate_sw_breakpoints();
426+ dbg_deactivate_sw_breakpoints();
414427
415428 /*
416429 * If the break point removed ok at the place exception
@@ -443,11 +456,24 @@ static int kgdb_reenter_check(struct kgdb_state *ks) · 26 unmodified lines
443456 return 1;
444457 }
445458
459+static void dbg_cpu_switch(int cpu, int next_cpu)
460+{
461+ /* Mark the cpu we are switching away from as a slave when it
462+ * holds the kgdb_active token. This must be done so that the
463+ * that all the cpus wait in for the debug core will not enter
464+ * again as the master. */
465+ if (cpu == atomic_read(&kgdb_active)) {
466+ kgdb_info[cpu].exception_state |= DCPU_IS_SLAVE;
467+ kgdb_info[cpu].exception_state &= ~DCPU_WANT_MASTER;
468+ }
469+ kgdb_info[next_cpu].exception_state |= DCPU_NEXT_MASTER;
470+}
471+
446472 static int kgdb_cpu_enter(struct kgdb_state *ks, struct pt_regs *regs)
447473 {
448474 unsigned long flags;
449475 int sstep_tries = 100;
450− int error = 0;
476+ int error;
451477 int i, cpu;
452478 int trace_on = 0;
453479 acquirelock:
@@ -460,6 +486,8 @@ acquirelock: · 6 unmodified lines
460486 cpu = ks->cpu;
461487 kgdb_info[cpu].debuggerinfo = regs;
462488 kgdb_info[cpu].task = current;
489+ kgdb_info[cpu].ret_state = 0;
490+ kgdb_info[cpu].irq_depth = hardirq_count() >> HARDIRQ_SHIFT;
463491 /*
464492 * Make sure the above info reaches the primary CPU before
465493 * our cpu_in_kgdb[] flag setting does:
@@ -471,7 +499,11 @@ acquirelock: · 5 unmodified lines
471499 * master cpu and acquire the kgdb_active lock:
472500 */
473501 while (1) {
474− if (kgdb_info[cpu].exception_state & DCPU_WANT_MASTER) {
502+cpu_loop:
503+ if (kgdb_info[cpu].exception_state & DCPU_NEXT_MASTER) {
504+ kgdb_info[cpu].exception_state &= ~DCPU_NEXT_MASTER;
505+ goto cpu_master_loop;
506+ } else if (kgdb_info[cpu].exception_state & DCPU_WANT_MASTER) {
475507 if (atomic_cmpxchg(&kgdb_active, -1, cpu) == cpu)
476508 break;
477509 } else if (kgdb_info[cpu].exception_state & DCPU_IS_SLAVE) {
@@ -513,7 +545,7 @@ return_normal: · 35 unmodified lines
513545 }
514546
515547 if (!kgdb_io_ready(1)) {
516− error = 1;
548+ kgdb_info[cpu].ret_state = 1;
517549 goto kgdb_restore; /* No I/O connection, resume the system */
518550 }
519551
@@ -548,7 +580,7 @@ return_normal: · 28 unmodified lines
548580 * Wait for the other CPUs to be notified and be waiting for us:
549581 */
550582 for_each_online_cpu(i) {
551− while (!atomic_read(&cpu_in_kgdb[i]))
583+ while (kgdb_do_roundup && !atomic_read(&cpu_in_kgdb[i]))
552584 cpu_relax();
553585 }
554586
@@ -557,7 +589,7 @@ return_normal: · 2 unmodified lines
557589 * in the debugger and all secondary CPUs are quiescent
558590 */
559591 kgdb_post_primary_code(ks->linux_regs, ks->ex_vector, ks->err_code);
560− kgdb_deactivate_sw_breakpoints();
592+ dbg_deactivate_sw_breakpoints();
561593 kgdb_single_step = 0;
562594 kgdb_contthread = current;
563595 exception_level = 0;
@@ -565,8 +597,26 @@ return_normal: · 1 unmodified line
565597 if (trace_on)
566598 tracing_off();
567599
568− /* Talk to debugger with gdbserial protocol */
569− error = gdb_serial_stub(ks);
600+ while (1) {
601+cpu_master_loop:
602+ if (dbg_kdb_mode) {
603+ kgdb_connected = 1;
604+ error = kdb_stub(ks);
605+ } else {
606+ error = gdb_serial_stub(ks);
607+ }
608+
609+ if (error == DBG_PASS_EVENT) {
610+ dbg_kdb_mode = !dbg_kdb_mode;
611+ kgdb_connected = 0;
612+ } else if (error == DBG_SWITCH_CPU_EVENT) {
613+ dbg_cpu_switch(cpu, dbg_switch_cpu);
614+ goto cpu_loop;
615+ } else {
616+ kgdb_info[cpu].ret_state = error;
617+ break;
618+ }
619+ }
570620
571621 /* Call the I/O driver's post_exception routine */
572622 if (dbg_io_ops->post_exception)
@@ -578,11 +628,16 @@ return_normal: · 5 unmodified lines
578628 for (i = NR_CPUS-1; i >= 0; i--)
579629 atomic_dec(&passive_cpu_wait[i]);
580630 /*
581− * Wait till all the CPUs have quit
582− * from the debugger.
631+ * Wait till all the CPUs have quit from the debugger,
632+ * but allow a CPU that hit an exception and is
633+ * waiting to become the master to remain in the debug
634+ * core.
583635 */
584636 for_each_online_cpu(i) {
585− while (atomic_read(&cpu_in_kgdb[i]))
637+ while (kgdb_do_roundup &&
638+ atomic_read(&cpu_in_kgdb[i]) &&
639+ !(kgdb_info[i].exception_state &
640+ DCPU_WANT_MASTER))
586641 cpu_relax();
587642 }
588643 }
@@ -603,7 +658,7 @@ kgdb_restore: · 14 unmodified lines
603658 clocksource_touch_watchdog();
604659 local_irq_restore(flags);
605660
606− return error;
661+ return kgdb_info[cpu].ret_state;
607662 }
608663
609664 /*
@@ -632,7 +687,8 @@ kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs) · 22 unmodified lines
632687 return 0; /* Ouch, double exception ! */
633688 kgdb_info[ks->cpu].exception_state |= DCPU_WANT_MASTER;
634689 ret = kgdb_cpu_enter(ks, regs);
635− kgdb_info[ks->cpu].exception_state &= ~DCPU_WANT_MASTER;
690+ kgdb_info[ks->cpu].exception_state &= ~(DCPU_WANT_MASTER |
691+ DCPU_IS_SLAVE);
636692 return ret;
637693 }
638694
@@ -665,7 +721,7 @@ static void kgdb_console_write(struct console *co, const char *s, · 26 unmodified lines
665721
666722 /* If we're debugging, or KGDB has not connected, don't try
667723 * and print. */
668− if (!kgdb_connected || atomic_read(&kgdb_active) != -1)
724+ if (!kgdb_connected || atomic_read(&kgdb_active) != -1 || dbg_kdb_mode)
669725 return;
670726
671727 local_irq_save(flags);
@@ -687,8 +743,14 @@ static void sysrq_handle_dbg(int key, struct tty_struct *tty) · 15 unmodified lines
687743 printk(KERN_CRIT "ERROR: No KGDB I/O module available\n");
688744 return;
689745 }
690− if (!kgdb_connected)
746+ if (!kgdb_connected) {
747+#ifdef CONFIG_KGDB_KDB
748+ if (!dbg_kdb_mode)
749+ printk(KERN_CRIT "KGDB or $3#33 for KDB\n");
750+#else
691751 printk(KERN_CRIT "Entering KGDB\n");
752+#endif
753+ }
692754
693755 kgdb_breakpoint();
694756 }
@@ -817,6 +879,16 @@ void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops) · 122 unmodified lines
817879 }
818880 EXPORT_SYMBOL_GPL(kgdb_unregister_io_module);
819881
882+int dbg_io_get_char(void)
883+{
884+ int ret = dbg_io_ops->read_char();
885+ if (!dbg_kdb_mode)
886+ return ret;
887+ if (ret == 127)
888+ return 8;
889+ return ret;
890+}
891+
820892 /**
821893 * kgdb_breakpoint - generate breakpoint exception
822894 *
@@ -839,6 +911,7 @@ static int __init opt_kgdb_wait(char *str) · 16 unmodified lines
839911 {
840912 kgdb_break_asap = 1;
841913
914+ kdb_init(KDB_INIT_EARLY);
842915 if (kgdb_io_module_registered)
843916 kgdb_initial_breakpoint();
844917
kernel/debug/debug_core.h+24−0
@@ -38,6 +38,8 @@ struct debuggerinfo_struct { · 37 unmodified lines
3838 void *debuggerinfo;
3939 struct task_struct *task;
4040 int exception_state;
41+ int ret_state;
42+ int irq_depth;
4143 };
4244
4345 extern struct debuggerinfo_struct kgdb_info[];
@@ -47,9 +49,31 @@ extern int dbg_remove_all_break(void); · 3 unmodified lines
4749 extern int dbg_set_sw_break(unsigned long addr);
4850 extern int dbg_remove_sw_break(unsigned long addr);
4951 extern int dbg_activate_sw_breakpoints(void);
52+extern int dbg_deactivate_sw_breakpoints(void);
53+
54+/* polled character access to i/o module */
55+extern int dbg_io_get_char(void);
56+
57+/* stub return value for switching between the gdbstub and kdb */
58+#define DBG_PASS_EVENT -12345
59+/* Switch from one cpu to another */
60+#define DBG_SWITCH_CPU_EVENT -123456
61+extern int dbg_switch_cpu;
5062
5163 /* gdbstub interface functions */
5264 extern int gdb_serial_stub(struct kgdb_state *ks);
5365 extern void gdbstub_msg_write(const char *s, int len);
5466
67+/* gdbstub functions used for kdb <-> gdbstub transition */
68+extern int gdbstub_state(struct kgdb_state *ks, char *cmd);
69+
70+#ifdef CONFIG_KGDB_KDB
71+extern int kdb_stub(struct kgdb_state *ks);
72+#else /* ! CONFIG_KGDB_KDB */
73+static inline int kdb_stub(struct kgdb_state *ks)
74+{
75+ return DBG_PASS_EVENT;
76+}
77+#endif /* CONFIG_KGDB_KDB */
78+
5579 #endif /* _DEBUG_CORE_H_ */
kernel/debug/gdbstub.c+36−0
@@ -887,6 +887,13 @@ int gdb_serial_stub(struct kgdb_state *ks) · 886 unmodified lines
887887 case 'Z': /* Break point set */
888888 gdb_cmd_break(ks);
889889 break;
890+#ifdef CONFIG_KGDB_KDB
891+ case '3': /* Escape into back into kdb */
892+ if (remcom_in_buffer[1] == '\0') {
893+ gdb_cmd_detachkill(ks);
894+ return DBG_PASS_EVENT;
895+ }
896+#endif
890897 case 'C': /* Exception passing */
891898 tmp = gdb_cmd_exception_pass(ks);
892899 if (tmp > 0)
@@ -932,3 +939,32 @@ kgdb_exit: · 39 unmodified lines
932939 error = 1;
933940 return error;
934941 }
942+
943+int gdbstub_state(struct kgdb_state *ks, char *cmd)
944+{
945+ int error;
946+
947+ switch (cmd[0]) {
948+ case 'e':
949+ error = kgdb_arch_handle_exception(ks->ex_vector,
950+ ks->signo,
951+ ks->err_code,
952+ remcom_in_buffer,
953+ remcom_out_buffer,
954+ ks->linux_regs);
955+ return error;
956+ case 's':
957+ case 'c':
958+ strcpy(remcom_in_buffer, cmd);
959+ return 0;
960+ case '?':
961+ gdb_cmd_status(ks);
962+ break;
963+ case '\0':
964+ strcpy(remcom_out_buffer, "");
965+ break;
966+ }
967+ dbg_io_ops->write_char('+');
968+ put_packet(remcom_out_buffer);
969+ return 0;
970+}