GDB调试总结

GDB调试总结

系统环境设置

被调试可执行文件编译

​ 编译是添加调试选项-g,以gcc/g++编译为例,如下:

gcc  -g  hello.c -o hello_test
g++  -g hello.cpp -o hello_test 

core文件设置

在启动的配置文件或者终端中执行如下命令

ulimit -c unlimited

如果要设置永久有效,可以修改/etc/security/limits.conf 文件, 找到 * soft core 0,修改为* soft core unlimited保存,这样每次启动时都会去读取这个配置文件。

#        - rtprio - max realtime priority
#        - chroot - change root to directory (Debian-specific)
#
#<domain>      <type>  <item>         <value>
#

*               soft    core            unlimited

基本命令

gdb常用启动参数说明

wangchenxi@2019-07-19|~$ gdb  --help
This is the GNU debugger.  Usage:

    gdb [options] [executable-file [core-file or process-id]]
    gdb [options] --args executable-file [inferior-arguments ...]

Selection of debuggee and its files:
# 指定可执行文件的参数
  --args             Arguments after executable-file are passed to inferior
  # 指定core文件路径
  --core=COREFILE    Analyze the core dump COREFILE.
  # 指定可执行文件路径
  --exec=EXECFILE    Use EXECFILE as the executable.
  # 指定程序pid
  --pid=PID          Attach to running process PID.
  # 指定源码路径
  --directory=DIR    Search for source files in DIR.
  ...
Initial commands and command files:
  --command=FILE, -x Execute GDB commands from FILE.

gdb基本调试命令

启动后设置启动参数

1、程序运行参数。
set args 可指定运行时参数。(如:set args 10 20 30 40 50)
show args 命令可以查看设置好的运行参数。

2、 运行环境。
path

可设定程序的运行路径。
show paths 查看程序的运行路径。
set environment varname [=value] 设置环境变量。如:set env USER=hchen
show environment [varname] 查看环境变量。

3、工作目录。
cd

相当于shell的cd命令。
pwd 显示当前的所在目录。

4、dir :加一个源文件路径到当前路径的前面。指定多个路径,可以使用”:”
show dir:显示定义了的源文件搜索路径。

(gdb) dir /opt:/usr/include/
Source directories searched: /opt:/usr/include:$cdir:$cwd
(gdb) show dir
Source directories searched: /opt:/usr/include:$cdir:$cwd

5、set substitute-path
这个其实很好理解,就是替换规则,你如果想查看当前的替换规则,你可以

show substitute-path
比如此时我们需要qstring.cpp这个文件,但由于某种原因,目前我们不能在/home/rickyk/qt-4.8.6/src/corelib/tools/qstring.cpp中找到,但我们确可以在/home/rickyk/qt-everywhere-opensource-src-4.8.6/src/corelib/tools/qstring.cpp中找到,我们就

set substitute-path /home/rickyk/qt-4.8.6 /home/rickyk/qt-everywhere-opensource-src-4.8.6

这是什么意思?其实就是让gdb在看到/home/rickyk/qt-4.8.6的时候他会做自动替换成/home/rickyk/qt-everywhere-opensource-src.4.8.6,也就是说gdb可以正确知道这个文件了。此时我们再show substitute-path可以看到此时的转换规则已经被我们加进去了

gdb调试参数
run命令
开始执行被调试可执行程序
list 命令
简写 l
详情查看: (gdb) help l
Lines can be specified in these ways:
#直接跟数字 默认当前入口文件或这但前所在文件
# 如: l  50    # 表示从当前文件的50行开始,列出10行代码
  LINENUM, to list around that line in current file,
  
  # 查看指定文件的指定行号附近的代码
  FILE:LINENUM, to list around that line in that file,
  
  # 查看指定函数的代码,默认当前文件
  FUNCTION, to list around beginning of that function,

# 查看指定文件的指定函数代码
 FILE:FUNCTION, to distinguish among like-named static functions.

# 根据地址查看代码
*ADDRESS, to list around the line containing that address.

With two args, if one is empty, it stands for ten lines away from
the other arg.
break命令
简写 b  或  br
查看命令帮助:(gdb)  help  b
(gdb) help break
Set breakpoint at specified location.
# break  [探测类型]  [位置描述]  [线程描述]  [条件断点]
break [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]

PROBE_MODIFIER shall be present if the command is to be placed in a
probe point.  Accepted values are `-probe' (for a generic, automatically
guessed probe type), `-probe-stap' (for a SystemTap probe) or 
`-probe-dtrace' (for a DTrace probe).
LOCATION may be a linespec, address, or explicit location as described
below.
With no LOCATION, uses current execution address of the selected
stack frame.  This is useful for breaking on return to a stack frame.
THREADNUM is the number from "info threads".
CONDITION is a boolean expression.

# 位置描述
Linespecs are colon-separated lists of location parameters,
# 文件名、函数名、标签名、行号
such as source filename, function name, label name, and line number.
Example: To specify the start of a label named "the_top" in the
function "fact" in the file "factorial.c", use
"factorial.c:fact:the_top".
Address locations begin with "*" and specify an exact address in the
program.  Example: To specify the fourth byte past the start function
"main", use "*main + 4".

Explicit locations are similar to linespecs but use an option/argument
syntax to specify location parameters.
Example: To specify the start of the label named "the_top" in the
function "fact" in the file "factorial.c", use "-source factorial.c
-function fact -label the_top".

By default, a specified function is matched against the program's
functions in all scopes.  For C++, this means in all namespaces and
classes.  For Ada, this means in all packages.  E.g., in C++,
"func()" matches "A::func()", "A::B::func()", etc.  The
"-qualified" flag overrides this behavior, making GDB interpret the
specified name as a complete fully-qualified name instead.

Multiple breakpoints at one place are permitted, and useful if their
conditions are different.


watch命令
简写 
命令帮助 (gdb) help watch
Set a watchpoint for an expression.
# watch  [位置描述]   变量名
# 当被监听位置的变量名发生值改变的时候执行断点
Usage: watch [-l|-location] EXPRESSION
A watchpoint stops execution of your program whenever the value of
an expression changes.
If -l or -location is given, this evaluates EXPRESSION and watches
the memory to which it refers.

next命令
简写 n
命令帮助 (gdb) help next
Step program, proceeding through subroutine calls.
Usage: next [N]
# 以代码行为单位,执行下N行代码
Unlike "step", if the current source line calls a subroutine,
this command does not enter the subroutine, but instead steps over
the call, in effect treating it as a single source line.


step命令
简写 s
命令帮助(gdb) help step
Step program until it reaches a different source line.
Usage: step [N]
# 以机器指令行为单位,向下执行N行(次),一般用来进入可函数代码,继续调试
Argument N means step N times (or till program stops for another reason).

finish命令
简写 不是f
命令帮助:(gdb) help finish
Execute until selected stack frame returns.
Usage: finish
# 一般用来完成函数调用,返回
Upon return, the value returned is printed and put in the value history.


x命令
命令帮助(gdb) help x
# 查看内存指令
Examine memory: x/FMT ADDRESS.
# 例如 (gdb) x/2xb   address
# 表示 以16进制(x:表示16进制,同时支持 0表示八进制,d表示十进制,u表示无符号整形)的形式
# 从地址address向后
# 读取2(2表示数量)个
# 字节b(b表示字节bytes,h表示半字halfword(2bytes),w表示word(4bytes), g表示8bytes)
ADDRESS is an expression for the memory address to examine.
FMT is a repeat count followed by a format letter and a size letter.
Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),
  t(binary), f(float), a(address), i(instruction), c(char), s(string)
  and z(hex, zero padded on the left).
Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).
The specified number of objects of the specified size are printed
according to the format.  If a negative number is specified, memory is
examined backward from the address.
Defaults for format and size letters are those previously used.
Default count is 1.  Default address is following last thing printed
with this command or "print".

bt命令
# 查看堆栈信息
(gdb) help bt
Print backtrace of all stack frames, or innermost COUNT frames.
With a negative argument, print outermost -COUNT frames.
Use of the 'full' qualifier also prints the values of the local variables.
Use of the 'no-filters' qualifier prohibits frame filters from executing
on this backtrace.

(gdb) help f
# 切换到指定的堆栈点
Select and print a stack frame.
With no argument, print the selected stack frame.  (See also "info frame").
An argument specifies the frame to select.
It can be a stack frame number or the address of the frame.


多线程调试命令

1、info threads:
  这条命令显示的是当前可调试的所有线程,GDB会给每一个线程都分配一个ID。前面有*的线程是当前正在调试的线程。
2、thread ID:
  切换到当前调试的线程为指定为ID的线程。
3、thread apply all command:
  让所有被调试的线程都执行command命令
4、thread apply ID1 ID2 … command:
  这条命令是让线程编号是ID1,ID2…等等的线程都执行command命令
5、set scheduler-locking off|on|step:
  在使用step或continue命令调试当前被调试线程的时候,其他线程也是同时执行的,如果我们只想要被调试的线程执行,而其他线程停止等待,那就要锁定要调试的线程,只让它运行。
  off:不锁定任何线程,所有线程都执行。
  on:只有当前被调试的线程会执行。
  step:阻止其他线程在当前线程单步调试的时候抢占当前线程。只有当next、continue、util以及finish的时候,其他线程才会获得重新运行的。
6、show scheduler-locking:
  这条命令是为了查看当前锁定线程的模式。

查看线程

(gdb)  info  threads

线程切换

(gdb) thread ID

锁定线程

(gdb) set scheduler-locking  on

多进程调试命令

查看进程

(gdb) info  inferiors

进程跟踪

默认设置下, 在调试多进程程序时 GDB 只会调试主进程. 但是 GDB > V7.0 支持多进程的分别以及同时调试, 换句话说, GDB 可以同时调试多个程序. 只需要设置 follow-fork-mode (默认值 parent) 和 detach-on-fork (默认值 on )即可.

follow-fork-modedetach-on-fork说明
parenton只调试主进程(GDB默认)
childon只调试子进程
parentoff同时调试两个进程,gdb 跟主进程, 子进程 block 在 fork 位置
childoff同时调试两个进程, gdb 跟子进程, 主进程 block 在 fork 位置

引用说明

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值