当前位置: 首页 > news >正文

GDB 调试入门教程

GDB 调试入门教程

  • 1. `sample.cpp`
    • 1.1. Compile and Run
  • 2. GDB 调试
  • 3. GDB commands
  • References

GDB is a command line debugger. It is a good choice on Linux or WSL. On macOS, use LLDB instead.

1. sample.cpp

(base) yongqiang@yongqiang:~/workspace/yongqiang$ ls -l
total 4
-rwxr-xr-x 1 yongqiang yongqiang 365 Feb 16 22:52 sample.cpp
(base) yongqiang@yongqiang:~/workspace/yongqiang$
#include <iostream>
#include <vector>

double Sum(const std::vector<double> &data) {
	double total = 0;
	for (size_t i = 0; i < data.size(); ++i) {
		total += data[i];
	}

	return total;
}

int main() {
	std::vector<double> data;
	data.push_back(10);
	data.push_back(20);
	data.push_back(30);

	std::cout << "Sum(data) = " << Sum(data) << std::endl;

	return 0;
}

1.1. Compile and Run

(base) yongqiang@yongqiang:~/workspace/yongqiang$ uname -a
Linux yongqiang 5.15.167.4-microsoft-standard-WSL2 #1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
(base) yongqiang@yongqiang:~/workspace/yongqiang$

Compile your program with the -g flag and start GDB in text user interface (TUI) mode.

(base) yongqiang@yongqiang:~/workspace/yongqiang$ ls -l
total 4
-rwxr-xr-x 1 yongqiang yongqiang 365 Feb 16 22:52 sample.cpp
(base) yongqiang@yongqiang:~/workspace/yongqiang$

(base) yongqiang@yongqiang:~/workspace/yongqiang$ g++ -g --std=c++17 sample.cpp -o sample
(base) yongqiang@yongqiang:~/workspace/yongqiang$ ls -l
total 144
-rwxr-xr-x 1 yongqiang yongqiang 142488 Feb 16 22:58 sample
-rwxr-xr-x 1 yongqiang yongqiang    365 Feb 16 22:52 sample.cpp
(base) yongqiang@yongqiang:~/workspace/yongqiang$

(base) yongqiang@yongqiang:~/workspace/yongqiang$ ./sample
Sum(data) = 60
(base) yongqiang@yongqiang:~/workspace/yongqiang$

2. GDB 调试

  • Run program with GDB: $ gdb sample

  • Set a breakpoint on main function: (gdb) break main or (gdb) b main

As breakpoing was set on main function, program will stop at main function and wait for gdb command.

  • Run program: (gdb) run or (gdb) r

  • Print value of total: (gdb) print total or (gdb) p total

(base) yongqiang@yongqiang:~/workspace/yongqiang$ gdb sample
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.2) 9.2
Copyright (C) 2020 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 sample...
(gdb) break main
Breakpoint 1 at 0x1314: file sample.cpp, line 13.
(gdb) run
Starting program: /home/yongqiang/workspace/yongqiang/sample

Breakpoint 1, main () at sample.cpp:13
13      int main() {
(gdb) list
8               }
9
10              return total;
11      }
12
13      int main() {
14              std::vector<double> data;
15              data.push_back(10);
16              data.push_back(20);
17              data.push_back(30);
(gdb) next
14              std::vector<double> data;
(gdb) next
15              data.push_back(10);
(gdb) next
16              data.push_back(20);
(gdb) next
17              data.push_back(30);
(gdb) break Sum
Breakpoint 2 at 0x5555555552a9: file sample.cpp, line 4.
(gdb) next
19              std::cout << "Sum(data) = " << Sum(data) << std::endl;
(gdb) next

Breakpoint 2, Sum (data=std::vector of length -840921094004155616, capacity -196785920380801129 = {...}) at sample.cpp:4
4       double Sum(const std::vector<double> &data) {
(gdb) next
5               double total = 0;
(gdb) print total
$1 = 6.9533558072559594e-310
(gdb) next
6               for (size_t i = 0; i < data.size(); ++i) {
(gdb) next
7                       total += data[i];
(gdb) next
6               for (size_t i = 0; i < data.size(); ++i) {
(gdb) next
7                       total += data[i];
(gdb) next
6               for (size_t i = 0; i < data.size(); ++i) {
(gdb) next
7                       total += data[i];
(gdb) next
6               for (size_t i = 0; i < data.size(); ++i) {
(gdb) next
10              return total;
(gdb) print total
$2 = 60
(gdb) next
11      }
(gdb) next
Sum(data) = 60
main () at sample.cpp:21
21              return 0;
(gdb) clear
No breakpoint at this line.
(gdb) quit
A debugging session is active.

        Inferior 1 [process 26162] will be killed.

Quit anyway? (y or n) y
(base) yongqiang@yongqiang:~/workspace/yongqiang$

3. GDB commands

CommandDescription
run or rExecutes the program from start to end
break or bSets a breakpoint on a particular line
disableDisables a breakpoint
enableEnables a disabled breakpoint
next or nExecutes the next line of code without diving into functions; Step over functions
stepGoes to the next instruction, diving into the function; Step into functions
list or lDisplays the code
print or pDisplays the value of a variable
quit or qExits out of GDB
clearClears all breakpoints
continue or cContinue execution until the next breakpoint

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] GDB Tutorial, https://www.gdbtutorial.com/

相关文章:

  • Python中数学问题1--lcm、gcd
  • SpringBoot整合easy-es
  • AIP-145 范围
  • el与data的2种写法
  • 图像生成GAN和风格迁移
  • React Hooks 的两个坑点
  • React.memo 使用详解与最佳实践
  • Java中对象序列化机制的优化研究
  • C++ std::atomic可以使用复杂类型(类和结构体)吗
  • 【C++】vector的使用练习 + 模拟实现
  • pnpm和npm安装TailwindCss
  • 【C++】34.智能指针(1)
  • 2025年免费量化交易软件——PTrade(含开通攻略)
  • JavaScript 中的“无限套娃”与“魔法优化”:递归与尾调用优化(TCO)
  • 2025年前端工程师职业发展的系统性应聘规划
  • 【效率技巧】怎么做思维导图||数学思维||费曼学习法
  • 算法笔记——字典树
  • Leetcode 712. Minimum ASCII Delete Sum for Two Strings
  • 机器学习 - 学习线性模型的重要性
  • 智能交通路线规划:让 AI 帮你躲避拥堵
  • 梅花奖在上海|舞剧《朱鹮》,剧里剧外都是生命的赞歌
  • 讲武谈兵|视距外的狙杀:从印巴空战谈谈超视距空战
  • 奥迪车加油时频繁“跳枪”维修两年未解决,4S店拒退换:可延长质保
  • 广东省原省长卢瑞华逝世,享年88岁
  • 在笔墨金石间,看胡问遂与梅舒适的艺术对话
  • 人才争夺战,二三线城市和一线城市拼什么?洛阳官方调研剖析