introduce how to use gdb for debugging
prepare
you have to use ‘-g’ when compiling your project for gdb, ‘-g’ means that the executable file will import source code information so that gdb can find source files.
commands
run
1 | (gdb) set args [your_args] |
set args for your program, the args will be delivered to ‘main’ function
1 | (gdb) start |
start executing program and pause at the first line in ‘main’ function to wait for commands
1 | (gdb) run |
just run program util a break point is triggered
break point
1 | (gdb) break X |
add a break point at No.x line of current file
1 | (gdb) break func |
add a break point at the function named ‘func’ of current file
1 | (gdb) break dir/file.c:X |
add a break point at NO.X line of ‘dir/file.c’
1 | (gdb) break dir/file.c:func |
add a break point at the function named ‘func’ of ‘dir/file.c’
control
1 | (gdb) next |
execute the next line, and if it is a function invokation, gdb just executes, not enters it
1 | (gdb) step |
execute the next line, and if it is a function invokation, gdb will enter the function
1 | (gdb) finish |
run util current function returns1
2
3
``` bash
(gdb) return
force to exit current function
information
1 | (gdb) list |
list 10 lines source code
1 | (gdb) list X |
list X lines source code
1 | (gdb) list func |
list source code of the function named ‘func’
1 | (gdb) print v |
print the value of the variable named ‘v’
1 | (gdb) backtrace |
show all function invokations with args
1 | (gdb) frame X |
show NO.X stack frame
1 | (gdb) info |
show current stack frame
quit
1 | (gdb) quit |
exit gdb