gdb调试

gdb是一个用来调试C和C++程序的功能强大的调试器,它可以在程序运行时,观查程序内部结构和内存的使用情况。

主要功能
(1) 监视程序中的变量。
(2) 设置断点,让程序在指定的代码行暂停。
(3) 单步执行。
(4) 分析崩溃程序产生的核文件。

gdb调试小例
使用gdb调试一个非常简单的c++程序,使用gdb的几个常用的主要功能监视变量设置断点单步执行
(1)源文件:helloworld.cpp

#include<iostream>
using namespace std;

int main()
{
    int counter=0;
    int i=0;
    for(;counter<10;++counter)
    {
        cin>>i;
        cout<<"i is: "<<i<<endl;
    }
    return 0;
}

(2)编译源文件
使用 “-ggdb3”选项编译,编译后“gdb”可以判断编译后的代码和源程序代码的关系。否则,gdb无法判断源程序中的哪一行代码被执行。

g++ -ggdb3 -o helloworld helloworld.cpp

(3)调试程序
gdb+程序名

adver@adver-VirtualBox:~/cproject$ gdb helloworld
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 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-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.
(gdb) 

设置断点:在main函数处,设置断点

(gdb) break main
Breakpoint 1 at 0x400936: file helloworld.cpp, line 6.

设置断点:在xxx.cpp的第100行设置断点

(gdb) b xxx.cpp:100

执行程序:run。程序运行到断点处(第6行)停止。

(gdb) run
Starting program: /home/adver/cproject/helloworld 

Breakpoint 1, main () at helloworld.cpp:6
6           int counter=0;

单步执行:step或s

 s
7           int i=0;
(gdb) s
8           for(;counter<10;++counter)

监视变量:display。使用该命令观测变量后,程序每次暂停时(断点或单步执行),会显示变量的值。

(gdb)display counter
1: counter = 0

(gdb) display i
2: i = 0

(gdb) s
Breakpoint 2, main () at helloworld.cpp:10
10              cin>>i;
2: i = 0
1: counter = 0

(gdb) s
10
11              cout<<"i is: "<<i<<endl;
2: i = 10
1: counter = 0

注意:上面最后一个“s”输入后,会执行”cin>>i”,因此输入10给变量i。

(gdb) s
i is: 10
8           for(;counter<10;++counter)
2: i = 10
1: counter = 0

(gdb) s
10              cin>>i;
2: i = 10
1: counter = 1

(gdb) s
2
11              cout<<"i is: "<<i<<endl;
2: i = 2
1: counter = 1

(gdb) s
i is: 2
8           for(;counter<10;++counter)
2: i = 2
1: counter = 1

next命令:它的功能和step类似,但它不会进入到程序里面(如:调试执行自己写的某函数时,step会进入该函数里,next则不会),本例没有这种情况,所以next和step效果看起来一样。

(gdb) n
10              cin>>i;
2: i = 2
1: counter = 2
(gdb) n
20
11              cout<<"i is: "<<i<<endl;
2: i = 20
1: counter = 2
(gdb) next
i is: 20
8           for(;counter<10;++counter)
2: i = 20
1: counter = 2

输出调用栈:在程序崩溃、异常退出时,输出函数调用栈,对查找Bug很有帮助。

# 你也可以在函数中,加入abort()函数,让程序运行到此处时,主动崩溃。
# 输出调用栈
gdb ./youProgram
# 程序崩溃后输入如下命令,显示函数调用栈
bt

退出调试

(gdb) q
A debugging session is active.

        Inferior 1 [process 3451] will be killed.

Quit anyway? (y or n) y
adver@adver-VirtualBox:~/cproject$
### GDB调试基础 GDB 是 GNU 调试器 (GNU Debugger),用于调试 C 和 C++ 程序。它允许开发者分析崩溃原因并逐步跟踪程序运行过程中的状态变化。 #### 编译带有调试信息的程序 为了能够使用 GDB 进行有效的调试,必须在编译阶段加入 `-g` 参数以包含调试符号表。这可以通过 GCC 或其他支持此选项的编译器完成[^2]。例如: ```bash gcc -g -std=c99 -o bug bug.c ``` 上述命令会生成名为 `bug` 的可执行文件,并附带完整的调试信息。 #### 启动 GDB 并加载目标程序 通过指定目标二进制文件作为参数来启动 GDB。如果成功读取到符号信息,则会在终端显示类似于 “Reading symbols from...done.” 的消息[^1]。以下是具体操作方式: ```bash gdb demo1 ``` 此时进入交互模式,在这里可以输入各种指令控制被调试的应用进程行为。 #### 基本GDB命令概览 - **run**: 开始或重新开始程序执行。可以选择提供命令行参数给待测应用。 ```bash run arg1 arg2 ... ``` - **breakpoint 设置断点**: 可以按函数名或者源码行号设置停止点以便更细致地观察变量值的变化情况。 ```bash break main # 在main函数入口处设停靠位 break filename.c:line_number # 定义特定位置上的暂停标记 ``` - **step/next 单步执行**: step 会深入调用子功能内部;而 next 则仅跨越当前语句而不展开其细节。 ```bash step # 步入下一个逻辑单元(可能跳转至另一方法) next # 继续直到下一条顶层表达式被执行完毕 ``` - **print 查看变量值**: 动态获取内存中存储的数据实例。 ```bash print variable_name # 输出某个已声明对象的内容 ``` - **continue 恢复流程直至下一断点到达前继续正常运转** ```bash continue # 让应用程序按照常规顺序前进,除非遇到新的障碍物才会再次中断下来等待进一步指示 ``` - **quit 结束会话退出工具界面返回shell环境当中去** ```bash quit # 关闭整个工作区离开debugger回到操作系统提示符之下 ``` 以上只是部分常用特性介绍而已,实际上还有许多高级技巧可供探索学习。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林多

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值