Gdb使用笔记

Posted by wsxq2 on 2018-03-30
TAGS:  GDB

本文最后一次编辑时间:2019-11-21 12:27:19 +0800

常用命令

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命令

Shortest Whole Usage Description Example
h help help [all|CLASS|COMMAND]|[CLASS COMMAND] Print help. h info b
q quit quit Exit gdb. q
l list list [-][LINENUM|FILE:LINENUM|FUNCTION|FILE:FUNCTION|*ADDRESS] List specified function or line. l main
b break break [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION] Set breakpoint at specified line or function. b 1
wa watch watch [-l|-location] EXPRESSION Set a watchpoint for an expression. wa -l a
disp display display /FMT EXP Print value of expression EXP each time the program stops. disp /3xb arr
disa disable disable [NUM]|[SUBCOMMAND [args...]] Disable some breakpoints. disa 1
del delete delete [NUM]|[SUBCOMMAND [args...]] Delete some breakpoints or auto-display expressions. del 1
r run run [args...] [<|>|>>] Start debugged program. r arg1 arg2
bt backtrace bt [full] [no-filters] [-][NUM] Print backtrace of all stack frames, or innermost COUNT frames. bt f
n next next [N] Step program, proceeding through subroutine calls. n 3
s step step [N] Step program until it reaches a different source line. s 3
c continue continue [N] Continue program being debugged, after signal or breakpoint. c 3
p print print EXP Print value of expression EXP. p main
x examine x/FMT ADDRESS Examine memory. x/3xb a
set set set VAR=EXP|SUBCOMMAND With a subcommand, this command modifies parts of the gdb environment. set var a=1
info info info SUBCOMMAND Generic command for showing things about the program being debugged. info b
sho show show [SUBCOMMAND args...] Generic command for showing things about the debugger. sho
fil file file FILE Use FILE as program to be debugged. fil a.out
fin finish finish Execute until selected stack frame returns. fin
unt until same as break Execute until the program reaches a source line greater than the current or a specified location unt 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调试命令的使用及总结