[bootlin/training-materials updates] master: debugging: lab-data: add test module for KGDB and add lab for it (b394ab47)

Clément Léger clement.leger at bootlin.com
Fri Nov 25 12:19:24 CET 2022


Repository : https://github.com/bootlin/training-materials
On branch  : master
Link       : https://github.com/bootlin/training-materials/commit/b394ab47f3814925d450366cb8bd35ef70d60a9e

>---------------------------------------------------------------

commit b394ab47f3814925d450366cb8bd35ef70d60a9e
Author: Clément Léger <clement.leger at bootlin.com>
Date:   Fri Nov 25 12:09:44 2022 +0100

    debugging: lab-data: add test module for KGDB and add lab for it
    
    Signed-off-by: Clément Léger <clement.leger at bootlin.com>


>---------------------------------------------------------------

b394ab47f3814925d450366cb8bd35ef70d60a9e
 lab-data/debugging/nfsroot/root/kgdb/Makefile      | 10 +++++
 lab-data/debugging/nfsroot/root/kgdb/kgdb_test.c   | 41 ++++++++++++++++++++
 .../debugging-kernel-debugging.tex                 | 45 ++++++++++++++++++++++
 3 files changed, 96 insertions(+)

diff --git a/lab-data/debugging/nfsroot/root/kgdb/Makefile b/lab-data/debugging/nfsroot/root/kgdb/Makefile
new file mode 100644
index 00000000..11dc0ade
--- /dev/null
+++ b/lab-data/debugging/nfsroot/root/kgdb/Makefile
@@ -0,0 +1,10 @@
+ifneq ($(KERNELRELEASE),)
+obj-m := kgdb_test.o
+else
+KDIR ?= $(HOME)/debugging-labs/src/linux
+all:
+	$(MAKE) -C $(KDIR) M=$$PWD
+
+clean:
+	$(MAKE) -C $(KDIR) M=$$PWD clean
+endif
diff --git a/lab-data/debugging/nfsroot/root/kgdb/kgdb_test.c b/lab-data/debugging/nfsroot/root/kgdb/kgdb_test.c
new file mode 100644
index 00000000..9355f5b7
--- /dev/null
+++ b/lab-data/debugging/nfsroot/root/kgdb/kgdb_test.c
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/module.h>
+#include <linux/delay.h>
+#include <linux/kthread.h>
+
+static struct task_struct *kthread;
+
+static int kgdb_test_thread_routine(void *data)
+{
+	while(1) {
+		usleep_range(1000, 2000);
+
+		pr_debug("I'm awake !\n");
+
+		if (kthread_should_stop())
+			break;
+	}
+
+	return 0;
+}
+
+static int kgdb_test_init(void)
+{
+	kthread = kthread_create(kgdb_test_thread_routine, NULL, "kgdb_test");
+	if (!kthread)
+		return -EINVAL;
+
+	wake_up_process(kthread);
+
+	return 0;
+}
+
+static void kgdb_test_exit(void)
+{
+	kthread_stop(kthread);
+}
+
+module_init(kgdb_test_init);
+module_exit(kgdb_test_exit);
+MODULE_LICENSE("GPL");
\ No newline at end of file
diff --git a/labs/debugging-kernel-debugging/debugging-kernel-debugging.tex b/labs/debugging-kernel-debugging/debugging-kernel-debugging.tex
index 748d2273..8ea81165 100644
--- a/labs/debugging-kernel-debugging/debugging-kernel-debugging.tex
+++ b/labs/debugging-kernel-debugging/debugging-kernel-debugging.tex
@@ -214,6 +214,51 @@ the problem using the \code{watchdog} command.
 was modified but the arm32 platforms do not provide watchpoints support with
 KGDB.}
 
+\subsection{Debugging a module}
+
+KGDB also allows to debug modules and thanks to the GDB python scripts
+(\code{lx-symbols}) mainly, it is as easy as debugging kernel core code. In
+order to test that feature, we are going to compile a test module and break on
+it.
+
+\begin{bashinput}
+$ cd /root/kgdb
+$ make
+\end{bashinput}
+
+Then on the target, insert the module using insmod:
+\begin{bashinput}
+# cd /root/kgdb_test
+# insmod kgdb_test.ko
+\end{bashinput}
+
+If KGDB was connected and the lx scripts were loaded, then it will be detected
+automatically and the symbols will be loaded:
+
+\begin{bashinput}
+# scanning for modules in /home/<user>/debugging-labs/nfsroot/root
+# loading @0xbf000000: /home/<user>/debugging-labs/nfsroot/root/kgdb_test/kgdb_test.ko
+\end{bashinput}
+
+If you attach KGDB after module loading, then you will need to execute the \code{lx-symbols}
+command in GDB:
+\begin{bashinput}
+(gdb) lx-symbols
+loading vmlinux
+# scanning for modules in /home/<user>/debugging-labs/nfsroot/root
+# loading @0xbf000000: /home/<user>/debugging-labs/nfsroot/root/kgdb_test/kgdb_test.ko
+\end{bashinput}
+
+Finally, add a breakpoint right after the \code{pr_debug()} call continue the
+execution to trigger it.
+
+Note: Due to a GDB bug, the execution after the breakpoint will crash. You can
+use a temporary breakpoint using \code{tbreak} command to workaround this
+problem.
+
+Note: a side quest you can also try to enable the \code{pr_debug()} call using
+the dynamic debug feature of the kernel.
+
 \section{kdump \& kexec}
 
 As presented in the course, kdump/kexec allows to boot a new kernel and dump a




More information about the training-materials-updates mailing list