如果要进行gdb调试,需要在编译的时候加上-g或-ggdb参数。
调试步骤举例:
1、生成调试的对象:g++ -g -o a.out test.cpp
2、启动gdb的方式:
gdb [-help] [-nx] [-q] [-batch] [-cd=dir] [-f] [-b bps] [-tty=dev] [-s symfile] [-e prog] [-se prog] [-c core] [-x cmds] [-d dir] [prog[core|procID]]
-q:‘‘Quiet’’. Do not print the introductory and copyright messages. These messages are also suppressed in batch mode
-core=file/-c file:Use file file as a core dump to examine,指定core文件
-directory=directory/-d directory:Add directory to the path to search for source files,指定的路径搜索源文件
启动例子:
gdb a.out
gdb a.out a.core
gdb a.out pid(进程号)
gdb的命令:
break [file:]function:Set a breakpoint at function (in file),简写为b,设置断点,例如:b test.cpp:Output
run arglist:以arglist启动程序,也可以不要arglist,例如:run -O “/data/bin_data/”
backtrace: display the program stack,简写bt,打印栈信息
print expr:Display the value of an expression,简写为p,打印表达式值
c:Continue running your program (after stopping, e.g. at a breakpoint),运行到下一个断点
next:Execute next program line (after stopping); step over any function calls in the line,可以简写为n,下一行
list [file:]function:type the text of the program in the vicinity of where it is presently stopped.打印函数上下的代码
step:Execute next program line (after stopping); step into any function calls in the line.单步调试,一行中有函数,会跟进去
set var:设置变量的值
start:执行程序,停在main第一行语句前等待
quit:退出gdb调试,简写q
调试core文件:
gdb exe文件 -c core文件
更多的gdb命令可以在进入gdb后,输入help去查找。//TODO
原文:http://www.cnblogs.com/yuanbuku/p/6416768.html