SlideShare a Scribd company logo
3
Most read
4
Most read
5
Most read
* Based on kernel 5.11 (x86_64) – QEMU
* 2-socket CPUs (4 cores/socket)
* 16GB memory
* Kernel parameter: nokaslr norandmaps
* KASAN: disabled
* Userspace: ASLR is disabled
* Host OS: Ubuntu 20.04.1
qemu + gdb: The efficient way to
understand/debug Linux kernel code/data
structure
Adrian Huang | Aug, 2022
Environment Preparation
• Repo: https://github.com/AdrianHuang/gdb-linux-real-mode.git
Environment Preparation
• Repo: https://github.com/AdrianHuang/gdb-linux-real-mode.git
o ./scripts/build.sh
▪ Download/build kernel and busybox: Make sure if your machine can access the Internet
o ./scripts/launch-vm.sh
▪ Launch a guest OS (QEMU) and wait for gdb connection
o ./scripts/launch-gdb.sh
▪ Launch gdb debugger and connect to the QEMU guest OS
• Steps:
$ git clone https://github.com/AdrianHuang/gdb-linux-real-mode.git
Cloning into 'gdb-linux-real-mode’...
$ cd gdb-linux-real-mode/
$ ./scripts/build.sh
# After build.sh is done, you’re all set!
QEMU/gdb at a glance: Console #1
Console #1: wait for gdb debugger
QEMU/gdb at a glance: Console #2: Real-mode entry point
Console #2: gdb debugger – breakpoint @0x10200 (real-mode entry point)
Kernel boot section
0x10000
0x10200
Physical Memory
QEMU loader loads ‘setup.bin’ at address 0x10000
0
ds = es = fs = gs = ss
cs
stack
sp = 0x1FFF0 (ss:0xFFF0)
protected mode
real mode
Kernel setup
code
gdb command file: Add any gdb commands in this file
Reference (Real-mode entry point): Vmlinux: anatomy
of bzimage and how x86 64 processor is booted
Console #2: step & continue
1
2
Console #1: QEMU: Guest OS
3
QEMU/gdb at a glance: Console #1/#2
Reference (Real-mode entry point): Vmlinux: anatomy
of bzimage and how x86 64 processor is booted
Note: Debug the decompressed vmlinux (generic kernel)
$ head -n 12 gdb-files/gdb-linux-kernel-real-mode.txt
# debug info about real-mode code of Linux kernel
add-symbol-file /home/adrian/work/gdb-linux-real-mode/out/obj/linux/arch/x86/boot/setup.elf 0x103ff -s .bstext
0x10000 -s .bsdata 0x1002d -s .header 0x101ef -s .entrytext 0x1026c -s .inittext 0x102d4 -s .initdata 0x103e1 -s .text32
0x130ce -s .bss 0x136e0 -s .data 0x13660
# debug info about compressed vmlinux
add-symbol-file /home/adrian/work/gdb-linux-real-mode/out/obj/linux/arch/x86/boot/compressed/vmlinux 0x3ce4f0 -
s .head.text 0x100000 -s .data 0x3d5b90 -s .bss 0x3d5e40 -s .pgtable 0x3f6000
target remote :1234
# Uncomment the following line if you want to debug the decompressed vmlinux
add-symbol-file /home/adrian/work/gdb-linux-real-mode/out/obj/linux/vmlinux
set print pretty on
Kernel supported gdb functions and commands
Console #2: gdb console
Console #1: QEMU: Guest OS
Example #1: Conditional breakpoint
gdb-files/gdb-linux-kernel-real-mode.txt
Example #2: watchpoint
Only one pte mapped
pte: physical address
1
2
3
gdb command: (gdb) watch *(0xffff8881809c0028)
4KB
Disk
Mapping Layer: file system
Generic Block Layer sector size
bi_size = 1024
bvec_iter
bi_sector
bv_len = 1024
bio_vec
bv_page
bv_offset
bio
Example #3: Data structure examination
Page Map
Level-4 Table
40
CR3 init_top_pgt = swapper_pg_dir
Sign-extend
Page Map
Level-4 Offset Physical Page Offset
0
30 21
39 20
38 29
47
48
63
Page Directory
Pointer Offset
Page Directory
Offset
Page Directory
Pointer Table
Page Directory
Table
level3_kernel_pgt
PDPTE #511
PDPTE #510 PDE #506
PDE #507
PDE #505
Direct Mapping Region
Kernel Code & fixmap
cpu_entry_area: 0.5TB
vmalloc: 32TB
PDE #13
PML4E #402
PML4E #273
…
PML4E #465
PML4E #468
PML4E #508
PML4E #511
vmemmap (page
descriptor)
PDPTE #0
Page Table Offset
1211
PTE #82 = 0
PTE #83 = 0
Page Table
Physical Memory
page frame
Example #4: Page Table Examination
[Linear Address] 0xffff_c900_01a5_2000, 0xffff_c900_01a5_3000
1
2
3
4
5
Example #5: ptype: print data type
Example #6: macro

More Related Content

What's hot (20)

PDF
Page cache in Linux kernel
Adrian Huang
 
PDF
Physical Memory Management.pdf
Adrian Huang
 
PDF
BPF Internals (eBPF)
Brendan Gregg
 
PPTX
Slab Allocator in Linux Kernel
Adrian Huang
 
PDF
Physical Memory Models.pdf
Adrian Huang
 
PPTX
qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...
Adrian Huang
 
PDF
Memory Compaction in Linux Kernel.pdf
Adrian Huang
 
PDF
Memory Mapping Implementation (mmap) in Linux Kernel
Adrian Huang
 
PDF
semaphore & mutex.pdf
Adrian Huang
 
PPTX
Linux Initialization Process (2)
shimosawa
 
PPTX
Linux Initialization Process (1)
shimosawa
 
PDF
Meet cute-between-ebpf-and-tracing
Viller Hsiao
 
PDF
Memory Management with Page Folios
Adrian Huang
 
PDF
Kernel Recipes 2017 - Understanding the Linux kernel via ftrace - Steven Rostedt
Anne Nicolas
 
PDF
Ixgbe internals
SUSE Labs Taipei
 
PDF
Arm device tree and linux device drivers
Houcheng Lin
 
PDF
Embedded_Linux_Booting
Rashila Rr
 
PPTX
U-Boot Porting on New Hardware
RuggedBoardGroup
 
PDF
Linux Synchronization Mechanism: RCU (Read Copy Update)
Adrian Huang
 
PPTX
Linux MMAP & Ioremap introduction
Gene Chang
 
Page cache in Linux kernel
Adrian Huang
 
Physical Memory Management.pdf
Adrian Huang
 
BPF Internals (eBPF)
Brendan Gregg
 
Slab Allocator in Linux Kernel
Adrian Huang
 
Physical Memory Models.pdf
Adrian Huang
 
qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...
Adrian Huang
 
Memory Compaction in Linux Kernel.pdf
Adrian Huang
 
Memory Mapping Implementation (mmap) in Linux Kernel
Adrian Huang
 
semaphore & mutex.pdf
Adrian Huang
 
Linux Initialization Process (2)
shimosawa
 
Linux Initialization Process (1)
shimosawa
 
Meet cute-between-ebpf-and-tracing
Viller Hsiao
 
Memory Management with Page Folios
Adrian Huang
 
Kernel Recipes 2017 - Understanding the Linux kernel via ftrace - Steven Rostedt
Anne Nicolas
 
Ixgbe internals
SUSE Labs Taipei
 
Arm device tree and linux device drivers
Houcheng Lin
 
Embedded_Linux_Booting
Rashila Rr
 
U-Boot Porting on New Hardware
RuggedBoardGroup
 
Linux Synchronization Mechanism: RCU (Read Copy Update)
Adrian Huang
 
Linux MMAP & Ioremap introduction
Gene Chang
 

Similar to qemu + gdb: The efficient way to understand/debug Linux kernel code/data structure (20)

PDF
LAS16-403: GDB Linux Kernel Awareness
Linaro
 
PDF
LAS16-403 - GDB Linux Kernel Awareness
Peter Griffin
 
PDF
ELC-E Linux Awareness
Peter Griffin
 
PDF
Development platform virtualization using qemu
Premjith Achemveettil
 
PDF
Let's trace Linux Lernel with KGDB @ COSCUP 2021
Jian-Hong Pan
 
PPT
Linux Kernel Debugging
GlobalLogic Ukraine
 
PPTX
Beyond printk: Efficient Zynq UltraScale+ MPSoC Linux Debugging and Development
Zach Pfeffer
 
PDF
Im trying to run make qemu-nox In a putty terminal but it.pdf
maheshkumar12354
 
PPT
Troubleshooting linux-kernel-modules-and-device-drivers-1233050713693744-1
Jagadisha Maiya
 
PPT
Troubleshooting Linux Kernel Modules And Device Drivers
Satpal Parmar
 
PDF
From printk to QEMU: Xen/Linux Kernel debugging
The Linux Foundation
 
PPTX
The n00bs guide to ovs dpdk
markdgray
 
PDF
Lecture 6 Kernel Debugging + Ports Development
Mohammed Farrag
 
PDF
Linux: the first second
Alison Chaiken
 
ZIP
Embedded Linux Odp
ghessler
 
PDF
XS Boston 2008 Debugging Xen
The Linux Foundation
 
PDF
Kernel Recipes 2015: Speed up your kernel development cycle with QEMU
Anne Nicolas
 
PDF
Linux kernel debugging
libfetion
 
PDF
Grub2 Booting Process
Mike Wang
 
PDF
MIPS-X
Zoltan Balazs
 
LAS16-403: GDB Linux Kernel Awareness
Linaro
 
LAS16-403 - GDB Linux Kernel Awareness
Peter Griffin
 
ELC-E Linux Awareness
Peter Griffin
 
Development platform virtualization using qemu
Premjith Achemveettil
 
Let's trace Linux Lernel with KGDB @ COSCUP 2021
Jian-Hong Pan
 
Linux Kernel Debugging
GlobalLogic Ukraine
 
Beyond printk: Efficient Zynq UltraScale+ MPSoC Linux Debugging and Development
Zach Pfeffer
 
Im trying to run make qemu-nox In a putty terminal but it.pdf
maheshkumar12354
 
Troubleshooting linux-kernel-modules-and-device-drivers-1233050713693744-1
Jagadisha Maiya
 
Troubleshooting Linux Kernel Modules And Device Drivers
Satpal Parmar
 
From printk to QEMU: Xen/Linux Kernel debugging
The Linux Foundation
 
The n00bs guide to ovs dpdk
markdgray
 
Lecture 6 Kernel Debugging + Ports Development
Mohammed Farrag
 
Linux: the first second
Alison Chaiken
 
Embedded Linux Odp
ghessler
 
XS Boston 2008 Debugging Xen
The Linux Foundation
 
Kernel Recipes 2015: Speed up your kernel development cycle with QEMU
Anne Nicolas
 
Linux kernel debugging
libfetion
 
Grub2 Booting Process
Mike Wang
 
Ad

Recently uploaded (20)

PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PDF
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PPTX
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Human Resources Information System (HRIS)
Amity University, Patna
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
Ad

qemu + gdb: The efficient way to understand/debug Linux kernel code/data structure

  • 1. * Based on kernel 5.11 (x86_64) – QEMU * 2-socket CPUs (4 cores/socket) * 16GB memory * Kernel parameter: nokaslr norandmaps * KASAN: disabled * Userspace: ASLR is disabled * Host OS: Ubuntu 20.04.1 qemu + gdb: The efficient way to understand/debug Linux kernel code/data structure Adrian Huang | Aug, 2022
  • 2. Environment Preparation • Repo: https://github.com/AdrianHuang/gdb-linux-real-mode.git
  • 3. Environment Preparation • Repo: https://github.com/AdrianHuang/gdb-linux-real-mode.git o ./scripts/build.sh ▪ Download/build kernel and busybox: Make sure if your machine can access the Internet o ./scripts/launch-vm.sh ▪ Launch a guest OS (QEMU) and wait for gdb connection o ./scripts/launch-gdb.sh ▪ Launch gdb debugger and connect to the QEMU guest OS • Steps: $ git clone https://github.com/AdrianHuang/gdb-linux-real-mode.git Cloning into 'gdb-linux-real-mode’... $ cd gdb-linux-real-mode/ $ ./scripts/build.sh # After build.sh is done, you’re all set!
  • 4. QEMU/gdb at a glance: Console #1 Console #1: wait for gdb debugger
  • 5. QEMU/gdb at a glance: Console #2: Real-mode entry point Console #2: gdb debugger – breakpoint @0x10200 (real-mode entry point) Kernel boot section 0x10000 0x10200 Physical Memory QEMU loader loads ‘setup.bin’ at address 0x10000 0 ds = es = fs = gs = ss cs stack sp = 0x1FFF0 (ss:0xFFF0) protected mode real mode Kernel setup code gdb command file: Add any gdb commands in this file Reference (Real-mode entry point): Vmlinux: anatomy of bzimage and how x86 64 processor is booted
  • 6. Console #2: step & continue 1 2 Console #1: QEMU: Guest OS 3 QEMU/gdb at a glance: Console #1/#2 Reference (Real-mode entry point): Vmlinux: anatomy of bzimage and how x86 64 processor is booted
  • 7. Note: Debug the decompressed vmlinux (generic kernel) $ head -n 12 gdb-files/gdb-linux-kernel-real-mode.txt # debug info about real-mode code of Linux kernel add-symbol-file /home/adrian/work/gdb-linux-real-mode/out/obj/linux/arch/x86/boot/setup.elf 0x103ff -s .bstext 0x10000 -s .bsdata 0x1002d -s .header 0x101ef -s .entrytext 0x1026c -s .inittext 0x102d4 -s .initdata 0x103e1 -s .text32 0x130ce -s .bss 0x136e0 -s .data 0x13660 # debug info about compressed vmlinux add-symbol-file /home/adrian/work/gdb-linux-real-mode/out/obj/linux/arch/x86/boot/compressed/vmlinux 0x3ce4f0 - s .head.text 0x100000 -s .data 0x3d5b90 -s .bss 0x3d5e40 -s .pgtable 0x3f6000 target remote :1234 # Uncomment the following line if you want to debug the decompressed vmlinux add-symbol-file /home/adrian/work/gdb-linux-real-mode/out/obj/linux/vmlinux set print pretty on
  • 8. Kernel supported gdb functions and commands
  • 9. Console #2: gdb console Console #1: QEMU: Guest OS Example #1: Conditional breakpoint gdb-files/gdb-linux-kernel-real-mode.txt
  • 10. Example #2: watchpoint Only one pte mapped pte: physical address 1 2 3 gdb command: (gdb) watch *(0xffff8881809c0028)
  • 11. 4KB Disk Mapping Layer: file system Generic Block Layer sector size bi_size = 1024 bvec_iter bi_sector bv_len = 1024 bio_vec bv_page bv_offset bio Example #3: Data structure examination
  • 12. Page Map Level-4 Table 40 CR3 init_top_pgt = swapper_pg_dir Sign-extend Page Map Level-4 Offset Physical Page Offset 0 30 21 39 20 38 29 47 48 63 Page Directory Pointer Offset Page Directory Offset Page Directory Pointer Table Page Directory Table level3_kernel_pgt PDPTE #511 PDPTE #510 PDE #506 PDE #507 PDE #505 Direct Mapping Region Kernel Code & fixmap cpu_entry_area: 0.5TB vmalloc: 32TB PDE #13 PML4E #402 PML4E #273 … PML4E #465 PML4E #468 PML4E #508 PML4E #511 vmemmap (page descriptor) PDPTE #0 Page Table Offset 1211 PTE #82 = 0 PTE #83 = 0 Page Table Physical Memory page frame Example #4: Page Table Examination [Linear Address] 0xffff_c900_01a5_2000, 0xffff_c900_01a5_3000 1 2 3 4 5
  • 13. Example #5: ptype: print data type