GDB commands by function

本文详细介绍GDB调试器的使用方法,包括启动程序、设置断点、运行程序、查看堆栈跟踪等关键操作。通过本文,读者可以快速掌握GDB的基本用法及高级技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • Startup
gdb -help print startup help, show switches
gdb object normal debug
gdb object core core debug (must specify core file)
gdb object pid attach to running process
gdb use file command to load object
  • Help
(gdb) help list command classes
(gdb) help running list commands in one command class
(gdb) help run bottom-level help for a command "run"
(gdb) help info list info commands (running program state)
(gdb) help info line help for a particular info command
(gdb) help show list show commands (gdb state)
(gdb) help show commands specific help for a show command
  • Breakpoints
(gdb) break funcname set a breakpoint on a function
(gdb) break linenum set a breakpoint on a line number
(gdb) break filename:funcname set breakpoint at file and function
(gdb) break filename:linenum set breakpoint at file and line
(gdb) info breakpoints show breakpoints
(gdb) delete breakpointnum delete a breakpoint by number
(gdb) delete delete all breakpoints
(gdb) clear delete breakpoints at current line
(gdb) clear funcname delete breakpoints at function
(gdb) clear linenum delete breakpoints at line
(gdb) disable breakpointnum turn a breakpoint off, but don't remove it
(gdb) enable breakpointnum turn disabled breakpoint back on
(gdb) tbreak funcname\linenum set a temporary breakpoint
(gdb) commands break-no … end set gdb commands with breakpoint
(gdb) ignore break-no count ignore bpt N-1 times before activation
(gdb) condition break-no expression break only if condition is true
(gdb) condition 2 i == 20 example: break on breakpoint 2 if i equals 20
(gdb) watch expression set software watchpoint on variable
(gdb) info watchpoints show current watchpoints
  • Running the program
(gdb) run run the program with current arguments
(gdb) run args redirection run with args and redirection
(gdb) set args args… set arguments for run
(gdb) show args show current arguments to run
(gdb) cont continue the program
(gdb) step single step the program; step into functions
(gdb) step count single step count times
(gdb) next step but step over functions
(gdb) next count next count times
(gdb) CTRL-C actually SIGINT, stop execution of current program
(gdb) attach process-id attach to running program
(gdb) detach detach from running program
(gdb) finish finish current function's execution
(gdb) kill kill current executing program
  • Stack backtrace
(gdb) bt print stack backtrace
(gdb) frame show current execution position
(gdb) up move up stack trace (towards main)
(gdb) down move down stack trace (away from main)
(gdb) info locals print automatic variables in frame
(gdb) info args print function parameters
在使用gdb调试时,可以使用commands命令来设置断点,并指定在每次到达该断点时要执行的一组命令。该命令的使用方法如下: 1. 首先,使用gdb启动程序,例如:gdb ./gdbdebug 2. 接下来,使用break命令设置一个断点,例如:b abc.cpp:10 3. 然后,使用commands命令将以下命令添加到指定的断点上: commands breakpoint-number 命令1 命令2 ... end 这里的breakpoint-number是断点号,表示将以下命令添加到指定的断点上。可以添加任何有效的GDB命令,每行一个命令,以end结束。 例如,假设我们有以下示例代码: ```cpp using namespace std; void func1() // 行号为10 { cout << "before" <<endl; } int main() { func1(); return 0; } ``` 我们可以使用以下命令来设置断点并指定commands: ```bash gdb a.out b abc.cpp:10 commands 1 silent prints "after" return c end run ``` 这样,在运行程序时,当程序执行到这个断点时,GDB会执行commands中指定的命令列表,然后继续执行程序。在这个例子中,commands命令会将输出修改为"after",然后继续执行程序,最终输出结果为"after"。 参考资料: 1. [GDB]断点(breakpoint)命令列表:commands、silent 2. 书籍《软件调试的艺术.pdf》2.11节 3. gdb commands命令用法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [GDG调试技巧之命令列表(commands)](https://blog.youkuaiyun.com/warden007/article/details/82888035)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [GDB Commands用法](https://blog.youkuaiyun.com/qq_33726635/article/details/117199722)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值