GDB学习笔记(一):GDB的常用命令(部分)

GDB主要用来调试C/C++程序。首先,编译时,需要把调试信息加到可执行文件中。可使用编译器的-g参数来实现这一点。

如:gcc -g -o hello hello.c

编译成功后,启动GDB进行调试。

 

启动GDB的方法:

1.gdb <program> program是编译后的可执行文件。

2.gdb<program> PID PID为正在运行的程序进程号,可能根用户才有此权限

3.先用gdb<program>运行,在GDB环境中用attach命令来挂接进程PID,用detach命令可取消挂接的程序

 

启动GDB后,可用help查看GDB命令

(gdb) help

List of classes of commands:

 

aliases -- Aliases of other commands

breakpoints -- Making program stop at certain points

data -- Examining data

files -- Specifying and examining files

internals -- Maintenance commands

obscure -- Obscure features

running -- Running the program

stack -- Examining the stack

status -- Status inquiries

support -- Support facilities

tracepoints -- Tracing of program execution without stopping the program

user-defined -- User-defined commands

 

Type "help" followed by a class name for a list of commands in that class.

Type "help all" for the list of all commands.

Type "help" followed by command name for full documentation.

Type "apropos word" to search for commands related to "word".

Command name abbreviations are allowed if unambiguous.

 

help命令只是列出GDB命令种类,

要查看指定的操作类可用help<class>

如:help breakpoint

要查看指定的命令可使用help <command>, 

如:help break

 

 

list:列出代码并显示行数,简称为l

break  <line>:设置断点,简称为b

info:为查看信息

run:运行程序

next:单条语句执行,简称为n

continue:继续运行程序,简称为c

print:打印信息,如:变量

bt:查看函数堆栈

quit:退出GDB,简称为q

直接回车表示重复上一次命令

 

在输入命令时,只需输入命令的前几个字符并敲击两次TAB键补齐。

如果只记得函数前缀,可输入函数名前面部分,用TAB键补齐。

 

GDB环境下可使用cd命令切换目录,用pwd显示当前目录,用重定向定向程序输出,如:run>oufile

 

在调试程序中,暂停进程运行是经常发生的,当进程被GDB停住时,可以使用

info program

来查看程序是否运行、进程号及被暂停的原因。

GDB中,有几种暂停方式:断点(breakpoint)、观察点(watchpoint)、捕捉点(catchpoint)、信号(signals)、线程停止(thread stops)

如果要恢复程序运行,可以使用continue命令。

 

 

 

一、设置断点

break <function> 在进入指定函数时停住

break <linenum>  在指定行号停住

 

break +offset

break -offset

在当前行号的前面或后面的offset行停住。

 

break filename:linenum

在源文件filenamelinenum行处停住

break filename:function

在源文件filenamefunction函数的入口处停住

 

break命令没有参数时,表示在下一条指令处停住

查看断点时,可用info break[n]n表示断点号)

 

 

二、设置捕捉点

catch <event>

Event:

1.throwC++抛出的异常

2.catchC++捕捉到的异常

其余功能在HP-UX下才有用,所有不一一列出

 

 

三、维护停止点

clear<function>

clear<fliename:function>

清除所有设置在函数上的停止点

 

clear<linenum>

clear<fliename:linenum>

清除所有设置在指定行上的停止点

 

delete 表示删除所有的断点

delete <breakpoints> 删除指定的断点。Breakpoints为断点号。

posted on 2012-08-31 16:00  Chris-Lin  阅读(301)  评论(0编辑  收藏  举报

导航