1. Common GDB Commands
1. Basic Commands
1.1. Start Debugging
gdb test.elf
1.2. View Disassembly
# Set style to view disassembly
set disassembly-floavor intel
disassemble
# Disassemble current instruction
disassemble $pc, $pc+0x100
1.3. View Functions
info functions
1.4. Print Addresses
# Print variable address Print function address
print &var
print test_function
1.5. GDB Remote Connection
Add the following to the QEMU startup script:
-gdb tcp::11110 -S
<span>-gdb tcp::11110</span>: Enables GDB debugging, listening on TCP port <span>11110</span>, which means you can connect to this port for remote debugging.
Note: The port can be replaced.
sudo apt-get -y install gdb-multiarch
gdb-multiarch vmlinux
target remote 172.17.0.5:1234
1.6. Execute Program
Step through one line of code:
s
Step through one line of code (without entering function):
n
Run program:
c
View current pc value during execution
stepi
1.7. Breakpoint Related
Set breakpoint:
Set breakpoint at function entry:
b function_name
Set breakpoint at specific line:
b file_name:line_number
Delete breakpoint:
d breakpoint_number
View breakpoint information:
info b
1.8. View Registers
View all registers
i r
View a specific register
x/i $pc
If using print
p/X $cpsr
1.9. View Source Code (Detailed TUI Mode)
In GDB, TUI (Text User Interface) mode provides a more intuitive way to debug programs by displaying source code, assembly code, and register information in a split-screen format. Here are some commonly used GDB TUI mode commands:
1.9.1. Start and Exit TUI Mode
-
Start TUI Mode:
(gdb) tui enableOr start GDB directly with the
<span>-tui</span>option:gdb -tui <program> -
Exit TUI Mode:
(gdb) tui disable
1.9.2. Window Management
- Show or Hide Windows:
- Show source window:
(gdb) layout src - Show assembly window:
(gdb) layout asm - Show source and assembly windows:
(gdb) layout split - Restore to single window mode:
(gdb) layout single - Switch Windows:
Use <span>Ctrl-x o</span> to switch focus between different windows.
1.9.3. Scrolling and Navigation
- Scroll Source Window:
- Scroll up:
(gdb) scroll up - Scroll down:
(gdb) scroll down - Scroll left:
(gdb) scroll left - Scroll right:
(gdb) scroll right - Jump to Specific Line:
(gdb) winheight src 10
1.9.4. Adjust Window Height
-
Adjust Source Window Height:
(gdb) winheight src +1Or:
(gdb) winheight src -1
1.9.5. View Current Source Commands
-
View Source of Current Line:
listThis command will display several lines of code around the current execution position.
-
View Source of Specific Function:
list function_nameThis command will display the source code of the specified function.
-
View Source of Specific File:
list file_name:line_numberThis command will start displaying source code from a specific line in the specified file.
-
Continue Displaying Next Segment of Source Code:
listAfter the
<span>list</span>command, entering<span>list</span>again will continue displaying the next segment of source code. -
View Current Execution Position:
frame -
View Disassembled Code and Corresponding Source of Current Instruction:
disassemble /m $pcThis command will display the disassembled code of the current instruction along with the corresponding source code.
-
Exit TUI Mode:
tui disable
2. View Memory Information
In GDB (GNU Debugger), you can use the following commands to view information at a specified memory address:
- View Content of a Single Memory Address:
<span>x</span>(examine) command can be used to view memory content.- Syntax:
<span>x/<format> <address></span> - For example:
<span>x/4xb 0x601000</span>will display 4 bytes of content starting from address<span>0x601000</span>, in hexadecimal format. - Common Format Specifiers:
<span>x</span>: Display content in hexadecimal<span>d</span>: Display content in decimal<span>u</span>: Display content in unsigned decimal<span>o</span>: Display content in octal<span>t</span>: Display content in binary<span>a</span>: Display content in address<span>i</span>: Display content in instruction<span>c</span>: Display content in character<span>s</span>: Display content in string- Common Units:
<span>b</span>: Byte<span>h</span>: Halfword (2 bytes)<span>w</span>: Word (4 bytes)<span>g</span>: Giant word (8 bytes)
2.1. Example Operations
Assuming we want to view the memory content starting from address <span>0x601000</span>:
- View 4 bytes in hexadecimal:
(gdb) x/4xb 0x601000 - View 4 bytes in decimal:
(gdb) x/4dw 0x601000 - View a string:
(gdb) x/s 0x601000 - View 4 instructions:
(gdb) x/4i 0x601000 # First get the address of the variable print &var x/4b 0x7ffd48e5b0ec # Use the following to examine four bytes, cannot see little-endian storage (gdb) x/4w 0x7ffd48e5b0ec 0x7ffd48e5b0ec: 0x11223344 0x00000001 0x00000000 0xb0429d90
3. View Stack Information
3.1. ARM64 Stack Registers
In EL0 (user mode), use <span>SP_EL0</span>.
In EL1 (kernel mode), use <span>SP_EL1</span>.
In EL2 (virtualization layer), use <span>SP_EL2</span>.
In EL3 (secure monitor layer), use <span>SP_EL3</span>.
3.2. Print Stack Trace
Use bt to view stack trace
3.3. Switch Stack Frame
f frame_number
In GDB, various commands can be used to print and view the current stack information. These commands help understand function call stacks, local variables, function parameters, and more. Here are some commonly used commands and examples:
3.4. Print Current Stack Trace
-
Basic Stack Trace:
btThis command will print the current stack trace, showing the function call chain.
-
Full Stack Trace:
bt fullThis command will print the full stack trace, including all local variables and parameters for each frame.
3.5. Switch and View Specific Stack Frame
-
Print Current Stack Frame:
frameThis command will print information about the current stack frame.
-
Switch to Specific Stack Frame:
frame frame_numberThis command will switch to the specified stack frame and print its information.
-
View Previous Stack Frame:
upThis command will switch to the previous stack frame.
-
View Next Stack Frame:
downThis command will switch to the next stack frame.
3.6. Print Local Variables and Parameters
-
Print Local Variables of Current Stack Frame:
info localsThis command will print all local variables of the current stack frame.
-
Print Parameters of Current Function:
info argsThis command will print all parameters of the current function.
4. Using GDB Watch to Monitor Variables:
https://www.cse.unsw.edu.au/~learn/debugging/modules/gdb_watch_display/
5. Using GDB to Debug Memory Management Information:
1. Related NUMA Information
NUMA node information is stored in the variable node_data, and after using watch to track it:
<span>drivers/base/arch_numa.c</span>
The stack trace is as follows:
#0 0xffff800008d69028 in setup_node_data (end_pfn=<optimized out>, start_pfn=262144, nid=0) at drivers/base/arch_numa.c:244
#1 numa_register_nodes () at drivers/base/arch_numa.c:366
#2 numa_init (init_func=init_func@entry=0xffff800008d68b98 <dummy_numa_init>) at drivers/base/arch_numa.c:398
#3 0xffff800008d691a8 in arch_numa_init () at drivers/base/arch_numa.c:476
#4 0xffff800008d37d00 in bootmem_init () at arch/arm64/mm/init.c:410
#5 0xffff800008d34998 in setup_arch (cmdline_p=<optimized out>) at arch/arm64/kernel/setup.c:346
#6 0xffff800008d30978 in start_kernel () at init/main.c:962
The function that finally registers the NUMA node is as follows: numa_register_nodes