Skip to content

Commit 2366e04

Browse files
committed
kgdb,debug-core,gdbstub: Hook the reboot notifier for debugger detach
The gdbstub and kdb should get detached if the system is rebooting. Calling gdbstub_exit() will set the proper debug core state and send a message to any debugger that is connected to correctly detach. An attached debugger will receive the exit code from include/linux/reboot.h based on SYS_HALT, SYS_REBOOT, etc... Reported-by: Jan Kiszka <[email protected]> Signed-off-by: Jason Wessel <[email protected]>
1 parent 9fbe465 commit 2366e04

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

kernel/debug/debug_core.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <linux/delay.h>
4242
#include <linux/sched.h>
4343
#include <linux/sysrq.h>
44+
#include <linux/reboot.h>
4445
#include <linux/init.h>
4546
#include <linux/kgdb.h>
4647
#include <linux/kdb.h>
@@ -784,13 +785,28 @@ void __init dbg_late_init(void)
784785
kdb_init(KDB_INIT_FULL);
785786
}
786787

788+
static int
789+
dbg_notify_reboot(struct notifier_block *this, unsigned long code, void *x)
790+
{
791+
if (!dbg_kdb_mode)
792+
gdbstub_exit(code);
793+
return NOTIFY_DONE;
794+
}
795+
796+
static struct notifier_block dbg_reboot_notifier = {
797+
.notifier_call = dbg_notify_reboot,
798+
.next = NULL,
799+
.priority = INT_MAX,
800+
};
801+
787802
static void kgdb_register_callbacks(void)
788803
{
789804
if (!kgdb_io_module_registered) {
790805
kgdb_io_module_registered = 1;
791806
kgdb_arch_init();
792807
if (!dbg_is_early)
793808
kgdb_arch_late();
809+
register_reboot_notifier(&dbg_reboot_notifier);
794810
atomic_notifier_chain_register(&panic_notifier_list,
795811
&kgdb_panic_event_nb);
796812
#ifdef CONFIG_MAGIC_SYSRQ
@@ -812,6 +828,7 @@ static void kgdb_unregister_callbacks(void)
812828
*/
813829
if (kgdb_io_module_registered) {
814830
kgdb_io_module_registered = 0;
831+
unregister_reboot_notifier(&dbg_reboot_notifier);
815832
atomic_notifier_chain_unregister(&panic_notifier_list,
816833
&kgdb_panic_event_nb);
817834
kgdb_arch_exit();

kernel/debug/gdbstub.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,13 @@ void gdbstub_exit(int status)
11111111
unsigned char checksum, ch, buffer[3];
11121112
int loop;
11131113

1114+
if (!kgdb_connected)
1115+
return;
1116+
kgdb_connected = 0;
1117+
1118+
if (!dbg_io_ops || dbg_kdb_mode)
1119+
return;
1120+
11141121
buffer[0] = 'W';
11151122
buffer[1] = hex_asc_hi(status);
11161123
buffer[2] = hex_asc_lo(status);

0 commit comments

Comments
 (0)