Search code examples
linuxubuntulinux-kernelubuntu-12.04linux-device-driver

Kernel Code : where can I find and how to debug the kernel


Recently I have installed Ubuntu 12.04 LTS ISO image in my desktop. Below is the output of the kernel version I have installed:

# uname -r
3.5.0-41-generic

I am trying to develop a VFS and want the kernel source code version '3.5.0-41-generic' for reference purpose - where can I find the same?

What are the excellent kernel debugging options looking at logs and mapping them to kernel code?

How and which debugger I can use to debug a live kernel flow execution?

Are there ways I can add more printk methods and re-modify the modules? Say I want to know how a FS mount method works - I can modify the required FS code (adding more printk functions) re-compile and reload the modules. Now with aid of my new printk functions I can understand the flow


Solution

  • Find and download kernel source code:

    • For Ubuntu-specific kernel sources, use apt cmd:
    sudo apt-get install linux-source
    

    This fetches the generic kernel source.

    To fetch specific version use:

    sudo apt-get source linux-image-6.11.8-generic
    
    • Also you can download vanilla/original kernel source from kernel.org.

    Kernel debugging options:

    • Read logs: Use dmesg cmd to view kernel logs.
    • Search in source: Match log messages with code by searching for keywords in source code files (like grep -r "log text" /path/to/source).
    • Tracing tools:
      • Use ftrace, perf, or systemtap cmds for tracing execution paths.
      • For filesystem-specific debugging, use tracepoints cmd.

    Live kernel debugging options:

    • Use KGDB:
      • Configure KGDB in your kernel config (CONFIG_KGDB=y and CONFIG_GDBSTUB=y).
      • Attach it via gdb using:
        target remote /dev/ttyS0
        
    • Explore it by advanced tracing tools like bpftrace.

    Adding printk and reloading modules:

    • Modify kernel modules:
      1. Locate the kernel source file for FS subsystem, like fs/super.c.
      2. Add printk() to inside functions of your targets (like mount function).
      3. Rebuild and install module:
        make modules
        sudo insmod your_module.ko
        
      4. Use dmesg to watch new logs.

    Note: Live kernel debugging means inspecting and modifying the Linux kernel while it is actively running.