162306a36Sopenharmony_ci.. highlight:: none
262306a36Sopenharmony_ci
362306a36Sopenharmony_ciDebugging kernel and modules via gdb
462306a36Sopenharmony_ci====================================
562306a36Sopenharmony_ci
662306a36Sopenharmony_ciThe kernel debugger kgdb, hypervisors like QEMU or JTAG-based hardware
762306a36Sopenharmony_ciinterfaces allow to debug the Linux kernel and its modules during runtime
862306a36Sopenharmony_ciusing gdb. Gdb comes with a powerful scripting interface for python. The
962306a36Sopenharmony_cikernel provides a collection of helper scripts that can simplify typical
1062306a36Sopenharmony_cikernel debugging steps. This is a short tutorial about how to enable and use
1162306a36Sopenharmony_cithem. It focuses on QEMU/KVM virtual machines as target, but the examples can
1262306a36Sopenharmony_cibe transferred to the other gdb stubs as well.
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ciRequirements
1662306a36Sopenharmony_ci------------
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci- gdb 7.2+ (recommended: 7.4+) with python support enabled (typically true
1962306a36Sopenharmony_ci  for distributions)
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ciSetup
2362306a36Sopenharmony_ci-----
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci- Create a virtual Linux machine for QEMU/KVM (see www.linux-kvm.org and
2662306a36Sopenharmony_ci  www.qemu.org for more details). For cross-development,
2762306a36Sopenharmony_ci  https://landley.net/aboriginal/bin keeps a pool of machine images and
2862306a36Sopenharmony_ci  toolchains that can be helpful to start from.
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci- Build the kernel with CONFIG_GDB_SCRIPTS enabled, but leave
3162306a36Sopenharmony_ci  CONFIG_DEBUG_INFO_REDUCED off. If your architecture supports
3262306a36Sopenharmony_ci  CONFIG_FRAME_POINTER, keep it enabled.
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci- Install that kernel on the guest, turn off KASLR if necessary by adding
3562306a36Sopenharmony_ci  "nokaslr" to the kernel command line.
3662306a36Sopenharmony_ci  Alternatively, QEMU allows to boot the kernel directly using -kernel,
3762306a36Sopenharmony_ci  -append, -initrd command line switches. This is generally only useful if
3862306a36Sopenharmony_ci  you do not depend on modules. See QEMU documentation for more details on
3962306a36Sopenharmony_ci  this mode. In this case, you should build the kernel with
4062306a36Sopenharmony_ci  CONFIG_RANDOMIZE_BASE disabled if the architecture supports KASLR.
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci- Build the gdb scripts (required on kernels v5.1 and above)::
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci    make scripts_gdb
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci- Enable the gdb stub of QEMU/KVM, either
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci    - at VM startup time by appending "-s" to the QEMU command line
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci  or
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci    - during runtime by issuing "gdbserver" from the QEMU monitor
5362306a36Sopenharmony_ci      console
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci- cd /path/to/linux-build
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci- Start gdb: gdb vmlinux
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci  Note: Some distros may restrict auto-loading of gdb scripts to known safe
6062306a36Sopenharmony_ci  directories. In case gdb reports to refuse loading vmlinux-gdb.py, add::
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci    add-auto-load-safe-path /path/to/linux-build
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci  to ~/.gdbinit. See gdb help for more details.
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci- Attach to the booted guest::
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci    (gdb) target remote :1234
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ciExamples of using the Linux-provided gdb helpers
7262306a36Sopenharmony_ci------------------------------------------------
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci- Load module (and main kernel) symbols::
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci    (gdb) lx-symbols
7762306a36Sopenharmony_ci    loading vmlinux
7862306a36Sopenharmony_ci    scanning for modules in /home/user/linux/build
7962306a36Sopenharmony_ci    loading @0xffffffffa0020000: /home/user/linux/build/net/netfilter/xt_tcpudp.ko
8062306a36Sopenharmony_ci    loading @0xffffffffa0016000: /home/user/linux/build/net/netfilter/xt_pkttype.ko
8162306a36Sopenharmony_ci    loading @0xffffffffa0002000: /home/user/linux/build/net/netfilter/xt_limit.ko
8262306a36Sopenharmony_ci    loading @0xffffffffa00ca000: /home/user/linux/build/net/packet/af_packet.ko
8362306a36Sopenharmony_ci    loading @0xffffffffa003c000: /home/user/linux/build/fs/fuse/fuse.ko
8462306a36Sopenharmony_ci    ...
8562306a36Sopenharmony_ci    loading @0xffffffffa0000000: /home/user/linux/build/drivers/ata/ata_generic.ko
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci- Set a breakpoint on some not yet loaded module function, e.g.::
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci    (gdb) b btrfs_init_sysfs
9062306a36Sopenharmony_ci    Function "btrfs_init_sysfs" not defined.
9162306a36Sopenharmony_ci    Make breakpoint pending on future shared library load? (y or [n]) y
9262306a36Sopenharmony_ci    Breakpoint 1 (btrfs_init_sysfs) pending.
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci- Continue the target::
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci    (gdb) c
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci- Load the module on the target and watch the symbols being loaded as well as
9962306a36Sopenharmony_ci  the breakpoint hit::
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci    loading @0xffffffffa0034000: /home/user/linux/build/lib/libcrc32c.ko
10262306a36Sopenharmony_ci    loading @0xffffffffa0050000: /home/user/linux/build/lib/lzo/lzo_compress.ko
10362306a36Sopenharmony_ci    loading @0xffffffffa006e000: /home/user/linux/build/lib/zlib_deflate/zlib_deflate.ko
10462306a36Sopenharmony_ci    loading @0xffffffffa01b1000: /home/user/linux/build/fs/btrfs/btrfs.ko
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci    Breakpoint 1, btrfs_init_sysfs () at /home/user/linux/fs/btrfs/sysfs.c:36
10762306a36Sopenharmony_ci    36              btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci- Dump the log buffer of the target kernel::
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci    (gdb) lx-dmesg
11262306a36Sopenharmony_ci    [     0.000000] Initializing cgroup subsys cpuset
11362306a36Sopenharmony_ci    [     0.000000] Initializing cgroup subsys cpu
11462306a36Sopenharmony_ci    [     0.000000] Linux version 3.8.0-rc4-dbg+ (...
11562306a36Sopenharmony_ci    [     0.000000] Command line: root=/dev/sda2 resume=/dev/sda1 vga=0x314
11662306a36Sopenharmony_ci    [     0.000000] e820: BIOS-provided physical RAM map:
11762306a36Sopenharmony_ci    [     0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
11862306a36Sopenharmony_ci    [     0.000000] BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
11962306a36Sopenharmony_ci    ....
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci- Examine fields of the current task struct(supported by x86 and arm64 only)::
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci    (gdb) p $lx_current().pid
12462306a36Sopenharmony_ci    $1 = 4998
12562306a36Sopenharmony_ci    (gdb) p $lx_current().comm
12662306a36Sopenharmony_ci    $2 = "modprobe\000\000\000\000\000\000\000"
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci- Make use of the per-cpu function for the current or a specified CPU::
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci    (gdb) p $lx_per_cpu("runqueues").nr_running
13162306a36Sopenharmony_ci    $3 = 1
13262306a36Sopenharmony_ci    (gdb) p $lx_per_cpu("runqueues", 2).nr_running
13362306a36Sopenharmony_ci    $4 = 0
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci- Dig into hrtimers using the container_of helper::
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci    (gdb) set $next = $lx_per_cpu("hrtimer_bases").clock_base[0].active.next
13862306a36Sopenharmony_ci    (gdb) p *$container_of($next, "struct hrtimer", "node")
13962306a36Sopenharmony_ci    $5 = {
14062306a36Sopenharmony_ci      node = {
14162306a36Sopenharmony_ci        node = {
14262306a36Sopenharmony_ci          __rb_parent_color = 18446612133355256072,
14362306a36Sopenharmony_ci          rb_right = 0x0 <irq_stack_union>,
14462306a36Sopenharmony_ci          rb_left = 0x0 <irq_stack_union>
14562306a36Sopenharmony_ci        },
14662306a36Sopenharmony_ci        expires = {
14762306a36Sopenharmony_ci          tv64 = 1835268000000
14862306a36Sopenharmony_ci        }
14962306a36Sopenharmony_ci      },
15062306a36Sopenharmony_ci      _softexpires = {
15162306a36Sopenharmony_ci        tv64 = 1835268000000
15262306a36Sopenharmony_ci      },
15362306a36Sopenharmony_ci      function = 0xffffffff81078232 <tick_sched_timer>,
15462306a36Sopenharmony_ci      base = 0xffff88003fd0d6f0,
15562306a36Sopenharmony_ci      state = 1,
15662306a36Sopenharmony_ci      start_pid = 0,
15762306a36Sopenharmony_ci      start_site = 0xffffffff81055c1f <hrtimer_start_range_ns+20>,
15862306a36Sopenharmony_ci      start_comm = "swapper/2\000\000\000\000\000\000"
15962306a36Sopenharmony_ci    }
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ciList of commands and functions
16362306a36Sopenharmony_ci------------------------------
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ciThe number of commands and convenience functions may evolve over the time,
16662306a36Sopenharmony_cithis is just a snapshot of the initial version::
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci (gdb) apropos lx
16962306a36Sopenharmony_ci function lx_current -- Return current task
17062306a36Sopenharmony_ci function lx_module -- Find module by name and return the module variable
17162306a36Sopenharmony_ci function lx_per_cpu -- Return per-cpu variable
17262306a36Sopenharmony_ci function lx_task_by_pid -- Find Linux task by PID and return the task_struct variable
17362306a36Sopenharmony_ci function lx_thread_info -- Calculate Linux thread_info from task variable
17462306a36Sopenharmony_ci lx-dmesg -- Print Linux kernel log buffer
17562306a36Sopenharmony_ci lx-lsmod -- List currently loaded modules
17662306a36Sopenharmony_ci lx-symbols -- (Re-)load symbols of Linux kernel and currently loaded modules
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ciDetailed help can be obtained via "help <command-name>" for commands and "help
17962306a36Sopenharmony_cifunction <function-name>" for convenience functions.
180