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行加断点)

condition (cond)  设置条件断点,也可以在break时候直接设置,比如 break 20 if ...

ignore             忽略断点N次命中次数,比如:ignore 2 N, 忽略2号断点N次

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

你可能感兴趣的:(list,command,gcc,UP,BT,debugging)