文章

Gdb使用笔记

Gdb使用笔记

常用命令

1
2
gcc -g a.c
gdb a.out
  • 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 “help info” followed by info subcommand name for full documentation.
  • Type “apropos word” to search for commands related to “word”.
  • Command name abbreviations are allowed if unambiguous.

——引用自 GDB 中的h命令

ShortestWholeUsageDescriptionExample
hhelphelp [all|CLASS|COMMAND]|[CLASS COMMAND]Print help.h info b
qquitquitExit gdb.q
llistlist [-][LINENUM|FILE:LINENUM|FUNCTION|FILE:FUNCTION|*ADDRESS]List specified function or line.l main
bbreakbreak [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]Set breakpoint at specified line or function.b 1
wawatchwatch [-l|-location] EXPRESSIONSet a watchpoint for an expression.wa -l a
dispdisplaydisplay /FMT EXPPrint value of expression EXP each time the program stops.disp /3xb arr
disadisabledisable [NUM]|[SUBCOMMAND [args...]]Disable some breakpoints.disa 1
deldeletedelete [NUM]|[SUBCOMMAND [args...]]Delete some breakpoints or auto-display expressions.del 1
rrunrun [args...] [<|>|>>]Start debugged program.r arg1 arg2
btbacktracebt [full] [no-filters] [-][NUM]Print backtrace of all stack frames, or innermost COUNT frames.bt f
nnextnext [N]Step program, proceeding through subroutine calls.n 3
sstepstep [N]Step program until it reaches a different source line.s 3
ccontinuecontinue [N]Continue program being debugged, after signal or breakpoint.c 3
pprintprint EXPPrint value of expression EXP.p main
xexaminex/FMT ADDRESSExamine memory.x/3xb a
setsetset VAR=EXP|SUBCOMMANDWith a subcommand, this command modifies parts of the gdb environment.set var a=1
infoinfoinfo SUBCOMMANDGeneric command for showing things about the program being debugged.info b
shoshowshow [SUBCOMMAND args...]Generic command for showing things about the debugger.sho
filfilefile FILEUse FILE as program to be debugged.fil a.out
finfinishfinishExecute until selected stack frame returns.fin
untuntilsame as breakExecute until the program reaches a source line greater than the current or a specified locationunt 35

更多命令参见:gdb调试命令的使用及总结

使用技巧

进入 GDB 时不提示

~/.bashrc中添加:alias gdb='gdb -q'

退出 GDB时不提示

~/.gdbinit中添加:set confirm off

GDB中打印出链表的值

使用GDB的自定义函数功能:

1
2
3
(gdb)help user-defined #显示自定义了哪些函数
(gdb)show user [command-name] #显示函数定义,若不指定名称,则全打印
(gdb)source file-name #从文件中导入命令

比如对于如下定义的C语言的单链表:

1
2
3
4
typedef struct LNode{
    ElemType data;
    struct LNode *next;
}*Link, *Position, LNode;

可在~/.gdbinit中定义如下函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#定义函数dump_list
define dump_list
  set $_node = (Link)$arg0->next
	set $i=1
    while $_node
        printf "%3d:%7s \n", $i++, $_node->data.xianshi
        set $_node = $_node->next
    end
end

#为函数dump_list加入文档说明
document dump_list
    This is a command to dump all elements in Linked List
    arg0 is the head
end

遇到过的问题

What does <value optimized out> mean in gdb?

It means you compiled with e.g. gcc -O3 and the gcc optimiser found that some of your variables were redundant in some way that allowed them to be optimised away. In this particular case you appear to have three variables a, b, c with the same value and presumably they can all be aliassed to a single variable. Compile with optimisation disabled, e.g. gcc -O0, if you want to see such variables (this is generally a good idea for debug builds in any case).

——引用自https://stackoverflow.com/a/5497906

How to view a pointer like an array in GDB?

1
2
3
p *array@len
*(T (*)[N])p
x/100w a

参见 c - How to view a pointer like an array in GDB? - Stack Overflow

给变量赋值?

1
set var a=1

参见h set var

参考链接

  1. gdb在线文档
  2. 100个gdb小技巧
  3. GDB中打印出链表的值
  4. gdb调试命令的使用及总结
本文由作者按照 CC BY 4.0 进行授权