gdb是一个在Linux系统上非常实用的调试工具,虽然没有像Visual Studio一样的UI,但是功能并不缺失,甚至更加强大。如果熟悉了gdb的操作,在Linux下编程、调试可以事半功倍。
以下是我在开发过程中常用到的一些gdb命令,简单做了一下整理放在这,以后有遇到更多的再加,但是不会像gdb文档一样把所有可能用法全部列上来。
预备知识:
gdb会启动一个命令行进程,在命令行里输入命令并等待返回结果。它必须要attach到一个调试目标进程才能正常工作,如果还没有启动进程或者进程已经结束,则大部分命令都会无效。
如果被调试的程序还未启动,仅指定了可执行文件的路径,可以使用start命令启动,启动后自动断在main函数的第一行代码位置。如果是针对一个已经运行的进程,使用attach命令指定pid,目标进程会在当前位置停下来,执行到哪算哪,此时可以使用c命令让进程继续执行。
arguments
gdb attach <pid>
gdb --args ./bubble -dispno 2 -> execute command/arguments
stop/breakpoints
b <file>:<line> -> set breakpoint
b *<addr> -> set breakpoint at address
watch expr -> data breakpoint(writing)
rwatch expr -> data breakpoint(reading)
awatch expr -> data breakpoint(writing + reading)
<Ctrl> + C -> debug break now
catch syscall <type> -> break when syscall invoked, type: clone / stat
cond <number> <expr> -> set conditional breakpoint
cond <number> -> cancel condition
delete <number> -> delete breakpoint
disable <number> -> disable breakpoint
enable <number> -> enable breakpoint
debug/steps
next -> step over
step -> step into
finish -> step out
p <expression> -> watch variable
ptype <expression> -> watch value type
p/x <expression> -> watch in hex
x <addr> -> view memory
frames/threads
thread 2 -> switch to thread 2
bt -> back trace
f <number> -> set stack frame
thread apply all bt -> back trace of all threads
view asm
layout next -> next layout
layout prev -> previous layout
layout asm -> view assembly
<Ctrl> + X, A -> quit layout
show info
info breakpoints -> list all breakpoints
info threads
info sharedlibrary -> show libraries
info handle -> show signal handles information
info registers
set global info
set env DISPLAY ":0" -> environment
set solib-search-path /home/ -> set local symbol dir
set follow-fork-mode child -> track child
set backtrace past-main on -> set to show backtrace out of main function
start/finish commands
attach <pid>
file <file>
start
si -> terminate process
c -> continue
signal SIGTRAP -> continue with signal
handle SIGTRAP nostop -> do not stop at SIGTRAP
q -> quit
网友评论