gdb 调试入门(二):使用终端进行 gdb 调试

在安装完 gdb 后,就可以尝试使用 gdb 在终端中调试程序了,如果对安装 gdb 存在疑惑,请参考 gdb 调试入门(一):Windows/Linux/Ubuntu 下安装 gdb

1. 创建或定位到需要调试的 .c 文件

首先编写一个简单的 helloworld.c:

#include<stdio.h>
int main()
{
    printf("hello world! \n");
    return 0;
}

你也可以定位到你需要调试的 .c 程序,然后开始下一步。

2. 编译文件

在含 helloworld.c 的目录终端下输入:

gcc -g helloworld.c -o helloworld

然后可以发现在此目录下生成了一个名为 helloworld 的可执行文件。

3. 启动 gdb 调试

在上节的终端里继续输入 gdb + 上节生成的要调试程序的可执行文件名:

gdb helloworld

可以发现终端打印了以下 log 信息:

GNU gdb (GDB) 7.12
Copyright (C) 2016 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-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from helloworld...done.

然后就可以进行调试了,比如输入 list,可以看到如下 log:

(gdb) list
1	#include<stdio.h>
2	int main()
3	{
4	    printf("hello world! \n");
5	    return 0;
6	}

开始尽情调试吧~

发布了56 篇原创文章 · 获赞 22 · 访问量 8470

猜你喜欢

转载自blog.csdn.net/zztiger123/article/details/105544003