[Linux] gdb 单步执行和跟踪函数调用

本文介绍了Linux下调试工具gdb的使用,通过一个test.c程序示例,详细讲解了如何进行单步执行、跟踪函数调用,以及gdb中的基本命令,如list、start、next、step、backtrace、info、frame、print和finish等,帮助开发者掌握调试技巧。

一、前言

在写程序时,不能保证程序总是正确的,特别是运行时出错的情况。在 Linux 中有一个很强大的调试工具 gdb ,可以完全操控程序的运行,能够随时查看程序中所有的内部状态,比如各变量的值、传给函数的参数、 当前执行的代码行等。
无论是否有这样的调试工具,调试的基本思想仍然是 分析现象->假设错误原因->产生新的现象去验证假设 的循环,根据现象来假设错误原因,以及如何设计新的数据去验证假设,这都需要非常严密的分析和思考,如果因为滥用强大的工具而忽略了分析的过程,往往会治标不治本,导致一个错误现象消失了但 Bug 仍然存在,甚至是把程序越改越错。

二、gdb 单步执行和跟踪函数调用

test.c 如下:

#include <stdio.h>

int add_range(int low, int high)
{
        int i, sum;
        for (i = low; i <= high; i++)
                sum = sum + i;
        return sum;
}

int main(void)
{
        int result[100] = { 0 };
        result[0] = add_range(1, 10);
        result[1] = add_range(1, 100);
        printf("result[0] = %d\nresult[1] = %d\n", result[0], result[1]);
        return 0;
}

如果当前文件夹存在一个 test.c 文件和 test 程序,那么可以通过
$ gdb test 来使用 gdb

[test@localhost MyCode]$ gdb test
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-94.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/test/practice/MyCode/test...done.
(gdb) 

注意必须使用 $ gcc -g test.c -o test 来生成 test 程序。 gcc -g 选项的作用是在可执行文件中加入源代码的信息,比如可执行文件中第几条机器指令对应源代码的第几行,但并不是把整个源文件嵌入到可执行文件中,所以在调试时必须保证 gdb 能找到源文件。
gdb 提供一个类似 Shell 的命令行环境,上面的 (gdb) 就是提示符,在这个提示符下输入 help 可以查看命令的类别:

(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.

也可以进一步查看某一类别中有哪些命令,例如查看 files 中有哪些命令:

(gdb) help files 
Specifying and examining files.

List of commands:

add-symbol-file -- Load symbols from FILE
add-symbol-file-from-memory -- Load the symbols out of memory from a dynamically loaded object file
cd -- Set working directory to DIR for debugger and program being debugged
core-file -- Use FILE as core dump for examining memory and registers
directory -- Add directory DIR to beginning of search path for source files
edit -- Edit specified file or function
exec-file -- Use FILE as program for getting contents of pure memory
file -- Use FILE as program to be debugged
forward-search -- Search for regular expression (see regex(3)) from last line listed
generate-core-file -- Save a core file with the current state of the debugged process
list -- List specified function or line
load -- Dynamically load FILE into the running program
nosharedlibrary -- Unload all shared object library symbols
path -- Add directory DIR(s) to beginning of search path for object files
pwd -- Print working directory
remote -- Manipulate files on the remote system
remote delete -- Delete a remote file
remote get -- Copy a remote file to the local system
remote put -- Copy a local file to the remote system
reverse-search -- Search backward for regular expression (see regex(3)) from last line listed
search -- Search for regular expression (see regex(3)) from last line listed
section -- Change the base address of section SECTION of the exec file to ADDR
---Type <return> to continue, or q <return> to quit---

试着使用 list 命令从第一行开始列出源代码

(gdb) list 1
1 #include <stdio.h>
2 
3 int add_range(int low, int high)
4 {
5  	int i, sum;
6  	for (i = low; i <= high; i++)
7   		sum = sum + i;
8  	return sum;
9 }
10

一次只列10行,如果要从第11行开始继续列源代码可以输入

(gdb) list

也可以什么都不输直接敲回车,gdb 提供了一个很方便的功能,在提示符下直接敲回车表示重复上一条命令。

(gdb) (直接回车)
11 int main(void)
12 {
13  	int result[100];
14  	result[0] = add_range(1, 10);
15  	result[1] = add_range(1, 100);
16  	printf("result[0] = %d\nresult[1] = %d\n", result[0], result[1]);
17  	return 0;
18 }

gdb 的很多常用命令有简写形式,例如 list 命令可以写成 l,要列一个函数的源代码也可以用函数名做参数:

(gdb) l add_range 
1 #include <stdio.h>
2 
3 int add_range(int low, int high)
4 {
5  	int i, sum;
6  	for (i = low; i <= high; i++)
7  	 	sum = sum + i;
8  	return sum;
9 }
10 

现在退出 gdb 的环境:

(gdb) quit

把源代码改名或移到别处再用 gdb 调试,这样就列不出源代码了。这里我们将 test.c 改名为 test1.c

[test@localhost MyCode]$ mv test.c test1.c
[test@localhost MyCode]$ gdb test
...
(gdb) list
4 test.c: 没有那个文件或目录.

可见 gcc-g 选项并不是把源代码嵌入到可执行文件中的,在调试时也需要源文件。
现在把源代码恢复原样,我们继续调试。首先用 start 命令开始执行程序:

[test@localhost MyCode]$ gdb test
...
(gdb) start
Temporary breakpoint 1 at 0x400568: file test.c, line 13.
Starting program: /home/test/practice/MyCode/test

Temporary breakpoint 1, main () at test.c:13
13  int result[100] = { 0 };
Missing separate debuginfos, use: debuginfo-install glibc-2.17-260.el7_6.3.x86_64
(gdb)

gdb 停在 main 函数中变量定义之后的第一条语句处等待我们发命令,gdb 列出的这条语句是即将执行的下一条语句。我们可以用 next 命令(简写为 n)控制这些语句一条一条地执行:

(gdb) next
14  result[0] = add_range(1, 10);
(gdb) n
15  result[1] = add_range(1, 100);
(gdb) (直接回车)
16  printf("result[0] = %d\nresult[1] = %d\n", result[0], result[1]);
(gdb) (直接回车)
result[0] = 55
result[1] = 5050
17  return 0;

n 命令依次执行两行赋值语句和一行打印语句,在执行打印语句时结果立刻打出来了,然后停在 return 语句之前等待我们发命令。虽然我们完全控制了程序的执行,但仍然看不出哪里错了,因为错误不在 main 函数中而在 add_range 函数中,现在用 start 命令重新来过,这次用 step 命令(简写为 s)钻进 add_range 函数中去跟踪执行:

(gdb) start
Temporary breakpoint 1 at 0x400561: file test.c, line 13.
Starting program: /home/test/practice/MyCode/test

Temporary breakpoint 1, main () at test.c:13
13  int result[100] = { 0 };
Missing separate debuginfos, use: debuginfo-install glibc-2.17-260.el7_6.3.x86_64
(gdb) s
14  result[0] = add_range(1, 10);
(gdb) (直接回车)
add_range (low=1, high=10) at test.c:6
6  for (i = low; i <= high; i++)

这次停在了 add_range 函数中变量定义之后的第一条语句处。在函数中有几种查看状态的办 法,backtrace 命令(简写为 bt )可以查看函数调用的栈帧:

(gdb) bt
#0  add_range (low=1, high=10) at test.c:6
#1  0x000000000040058a in main () at test.c:14

可见当前的 add_range 函数是被 main 函数调用的,main 传进来的参数是 low=1, high=10。main 函数的栈帧编号为 1,add_range 的栈帧编号为 0。现在可以用 info 命令(简写为 i )查看 add_range 函数局部变量的值:

(gdb) i locals
i = 0
sum = 0

如果想查看 main 函数当前局部变量的值也可以做到,先用 frame 命令(简写为 f )选择 1 号栈帧然后再查看局部变量:

(gdb) f 1
#1  0x000000000040058a in main () at test.c:14
14  result[0] = add_range(1, 10);
(gdb) i locals
result = {0 <repeats 100 times>}

注意到 result 数组已经成功初始化为全 0 。到目前为止一切正常。用 sn 往下走几步,然后用 print 命令(简写为p )打印出变量 sum 的值:

(gdb) s
7   sum = sum + i;
(gdb) (直接回车)
6  for (i = low; i <= high; i++)
(gdb) (直接回车)
7   sum = sum + i;
(gdb) (直接回车)
6  for (i = low; i <= high; i++)
(gdb) p sum
$1 = 3

第一次循环 i 是 1,第二次循环 i 是 2,加起来是 3,没错。这里的 $1 表示 gdb 保存着这些中间结果,$ 后面的编号会自动增长,在命令中可以用 $1$2$3 等编号代替相应的值。由于我们本来就知道第一次调用的结果是正确的,再往下跟也没意义了,可以用 finish 命令让程序一直运行到从当前函数返回为止:

(gdb) finish
Run till exit from #0  add_range (low=1, high=10) at test.c:6
0x000000000040058a in main () at test.c:14
14  result[0] = add_range(1, 10);
Value returned is $2 = 55

返回值是 55,当前正准备执行赋值操作,用 s 命令赋值,然后查看result 数组:

(gdb) s
15  result[1] = add_range(1, 100);
(gdb) p result
$3 = {55, 0 <repeats 99 times>}

第一个值 55 确实赋给了 result 数组的第 0 个元素。下面用 s 命令进入第二次 add_range 调用,进入之后首先查看参数和局部变量:

(gdb) s
add_range (low=1, high=100) at test.c:6
6  for (i = low; i <= high; i++)
(gdb) bt
#0  add_range (low=1, high=100) at test.c:6
#1  0x000000000040059f in main () at test.c:15
(gdb) i locals
i = 11
sum = 55

由于局部变量i和sum没初始化,所以具有不确定的值,又由于两次调用是挨着的, 这时 i 和 sum 正好取了上次调用时的值。事实上 i 的初值不是0倒没关系,在 for 循环 中会赋值为 0 的,但 sum 如果初值不是 0,累加得到的结果就错了。好了,我们已经找到错误原因, 可以退出 gdb 修改源代码了。如果我们不想浪费这次调试机会,可以在 gdb 中马上把 sum 的初值改为 0 继续运行,看看这一处改了之后还有没有别的 Bug

(gdb) set var sum=0
(gdb) finish
Run till exit from #0  add_range (low=1, high=100) at test.c:6
0x000000000040059f in main () at test.c:15
15  result[1] = add_range(1, 100);
Value returned is $4 = 5050
(gdb) n
16  printf("result[0] = %d\nresult[1] = %d\n", result[0], result[1]);
(gdb) (直接回车)
result[0] = 55
result[1] = 5050
17  return 0;

这样结果就对了。修改变量的值除了用 set 命令之外也可以用 print 命令,因为 print 命令后面跟的是表达式,而我们知道赋值和函数调用也都是表达式,所以也可以用 print 命令修改变量的值或者 调用函数:

(gdb) p result[2]=33
$5 = 33
(gdb) p printf("result[2]=%d\n", result[2])
result[2]=33
$6 = 13

我们讲过,printf 的返回值表示实际打印的字符数,所以 $6 的结果是 13。

三、本节用到的 gdb 基本命令

命令描述
backtrace(或 bt)查看各级函数调用及参数
finish连续运行到当前函数返回为止,然后停下来等待命令
frame(或 f)帧编号选择栈帧
info(或 i) locals查看当前栈帧局部变量的值
list(或 l)列出源代码,接着上次的位置往下列,每次列10行
list 行号列出从第几行开始的源代码
list 函数名列出某个函数的源代码
next(或 n)执行下一行语句
print(或 p)打印表达式的值,通过表达式可以修改变量的值或者调用函数
quit(或 q)退出gdb调试环境
set var修改变量的值
start开始执行程序,停在main函数第一行语句前面等待命令
step(或 s)执行下一行语句,如果有函数调用则进入到函数中
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值