Debugging with gdb

使用-g选项来在编译后的程序中加入调试信息。

gcc -g -o debug debug.c

然后就可以启动gdb进行调试了。

gdb debug

一些常用的命令:

help 帮助信息

run (r) 运行程序

cont (c) 继续运行

step (s) 步进到下一行(遇到函数会进入函数)

next (n) 继续运行直到下一行(会跳过函数)

finish 继续运行直到当前栈中函数返回

break (b) 设置断点(breakpoint),参数可以是函数名或行号 (break 20 表示在第20行加断点)

watch 设置观察点(watchpoint),参数是变量名,变量值改变时调试器会停下来

info (i) 查看断点和显示(display) (info display 或者 info break)

backtrace 栈回溯 (也可以用 bt, where)

up 移向高栈帧(move to higher stack frame)

down 移向低栈帧(move to higher stack frame)

print (p) 查看变量(可以查看局部变量,全局变量,函数参数,数组,指针等。

print array[0]@5 可以列出数组array里面从第一个元素开始的五个元素)

list 查看源代码

display 程序在断点处停止后自动显示信息

commands 程序在断点处停止后执行一些命令, 比如改变某个变量的值,继续运行等。

(commands 2表示在第2个断点处加命令,如显示某个变量之类)

disable 禁用断点和显示(display)

disable 1 // 禁用第一个断点

disable display 1 //禁用第一个显示


要保存当前设置的断点,方便以后调试,参考:

To save breakpoints, use this command:
save breakpoints [filename]


To use saved breakpoints, use this command to run gdb:
gdb --command [filename]
or use this command after run gdb:
source [filename]


check this webpage for detail:
http://sourceware.org/gdb/current/onlinedocs/gdb/Save-Breakpoints.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值