GDB常用调试命令

在程序编译时增加-g选项以支持gdb调试

如:

$ gcc -g example.c -o example.x

通过上述命令对example.c编译之后,使用下列命令进入到gdb调试:

$ gdb example.x

在gdb调试中,常用的命令有以下几个:

$ list 缩略为 l

列出程序源码,每次列出10行,按回车重复运行上一命令;

$ run 缩略为 r

程序开始运行,在r后可以加入程序启动参数,程序运行到断点处暂停;

$ continue 缩略为 c

程序继续运行,到下一断点处暂停;

单步调试

$ step 缩略为s

$ next 缩略为 n

程序继续运行到下一断点;

$ break 缩略为 b

在程序某一位置设置断点;

$ info break 缩略为 i b

查看断点信息;

设置/查看运行参数

$ set args ---/show args

加载运行中的进程进行调试(Attach to the running process to be debugged.):

$ gdb attatch pid

Specifying source directories

$ dir dirname …

以十六进制输出内存块数据

$ x/28hx ---

段错误调试,core文件样例

通过ulimit命令查看一下系统是否配置支持了dump core的功能。通过ulimit -c或ulimit -a,可以查看core file大小的配置情况,如果为0,则表示系统关闭了dump core;可以通过ulimit -c unlimited来打开。若发生了段错误,但没有core dump,是由于系统禁止core文件的生成。

$ gdb [exec file] [core file] | gdb -c corefile execfile

查看堆栈信息:

$ bt

查看堆栈中某一层的信息:

$ f $num

PS:对于python程序在linux下可以使用pdb做调试!

Published by Windows Livewriter.

原文地址:https://www.cnblogs.com/berlin-sun/p/debugwihgdb.html