Common GDB Commands for Linux

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 enable
    

    Or 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 +1
    

    Or:

    (gdb) winheight src -1
    

1.9.5. View Current Source Commands

  1. View Source of Current Line:

    list
    

    This command will display several lines of code around the current execution position.

  2. View Source of Specific Function:

    list function_name
    

    This command will display the source code of the specified function.

  3. View Source of Specific File:

    list file_name:line_number
    

    This command will start displaying source code from a specific line in the specified file.

  4. Continue Displaying Next Segment of Source Code:

    list
    

    After the <span>list</span> command, entering <span>list</span> again will continue displaying the next segment of source code.

  5. View Current Execution Position:

    frame
    
  6. View Disassembled Code and Corresponding Source of Current Instruction:

    disassemble /m $pc
    

    This command will display the disassembled code of the current instruction along with the corresponding source code.

  7. 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:

  1. View Content of a Single Memory Address:
    1. <span>x</span> (examine) command can be used to view memory content.
    2. Syntax: <span>x/<format> <address></span>
    3. For example: <span>x/4xb 0x601000</span> will display 4 bytes of content starting from address <span>0x601000</span>, in hexadecimal format.
  2. Common Format Specifiers:
    1. <span>x</span>: Display content in hexadecimal
    2. <span>d</span>: Display content in decimal
    3. <span>u</span>: Display content in unsigned decimal
    4. <span>o</span>: Display content in octal
    5. <span>t</span>: Display content in binary
    6. <span>a</span>: Display content in address
    7. <span>i</span>: Display content in instruction
    8. <span>c</span>: Display content in character
    9. <span>s</span>: Display content in string
  3. Common Units:
    1. <span>b</span>: Byte
    2. <span>h</span>: Halfword (2 bytes)
    3. <span>w</span>: Word (4 bytes)
    4. <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>:

  1. View 4 bytes in hexadecimal:
    (gdb) x/4xb 0x601000
    
  2. View 4 bytes in decimal:
    (gdb) x/4dw 0x601000
    
  3. View a string:
    (gdb) x/s 0x601000
    
  4. 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

  1. Basic Stack Trace:

    bt
    

    This command will print the current stack trace, showing the function call chain.

  2. Full Stack Trace:

    bt full
    

    This command will print the full stack trace, including all local variables and parameters for each frame.

3.5. Switch and View Specific Stack Frame

  1. Print Current Stack Frame:

    frame
    

    This command will print information about the current stack frame.

  2. Switch to Specific Stack Frame:

    frame frame_number
    

    This command will switch to the specified stack frame and print its information.

  3. View Previous Stack Frame:

    up
    

    This command will switch to the previous stack frame.

  4. View Next Stack Frame:

    down
    

    This command will switch to the next stack frame.

3.6. Print Local Variables and Parameters

  1. Print Local Variables of Current Stack Frame:

    info locals
    

    This command will print all local variables of the current stack frame.

  2. Print Parameters of Current Function:

    info args
    

    This 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

Leave a Comment