GDB/CGDB 调试器学习笔记


🚀 前言

GDB 是 GNU 项目下功能强大的命令行调试器,适用于 C/C++ 等多种语言。CGDB 则是在 GDB 之上构建的轻量级 curses 界面,适合喜欢终端操作且习惯 vi 风格的人。


一、GDB 入门篇

1. 编译时带调试信息

gcc -g -O0 -Wall -o myprog myprog.c

其中:

  • -g:生成调试信息;
  • -O0:关闭优化,调试体验更准确 ([Red Hat Developers][1])。

2. 启动 GDB

最基本的启动方式:

gdb myprog
(gdb) run

也可以附加参数:

gdb --args myprog arg1 arg2

3. 常用命令

命令作用
help / apropos查看命令帮助
break LOCATION (b)设置断点
run (r)运行程序
next (n)单步,不进入函数
step (s)单步,进入函数
continue (c)继续运行到下一个断点
print VAR (p)打印变量值
info locals/args/breakpoints查看本地变量、参数、断点信息
watch VAR监视变量读写 ([TechBeamers][2], [Stanford University][3])
  • list (l): 显示当前源代码附近几行 ([Stanford University][3])。
  • p arr@count: 打印数组或指针连续元素 ([Stanford University][3])。

4. 多线程 & 回溯调试

  • 调试多线程:使用 info threads 查看线程;结合 watch 可观察线程变量读写 。
  • 可逆调试(reverse debugging):支持 reverse-continue (rc)reverse-step (rs) 等命令,回溯执行路径 ([Stack Overflow][4])。

二、CGDB:GDB 的终端 GUI 增强

1. CGDB 简介

CGDB 是基于 curses 的终端调试界面,提供源代码与 GDB 窗口的分屏显示,键盘操作类似 vi ([cgdb.github.io][5])。

2. 安装方法

一般用源码编译:

./configure
make
sudo make install

依赖 ncursesreadline ([heather.cs.ucdavis.edu][6])。

3. 快速入门操作

  • 启动:cgdb myprog

  • 窗口之间切换:

    • 源码窗:按 Esc
    • GDB 窗:按 i ([cgdb.github.io][5], [Siddharth’s Blog][7])。
  • 设置断点:源码窗中移动光标,按 space

  • 调整源码窗大小:用 -=

  • 常用 GDB 命令:在 GDB 窗中使用 b, n, s, c, bt, info threads 等 ([cseweb.ucsd.edu][8], [Android blog][9])。

4. 为什么使用 CGDB?

  • 比 GDB 的 TUI 模式更稳定、有颜色显示 ([heather.cs.ucdavis.edu][6])。
  • 操作流畅、界面简洁,适合 SSH 终端环境。
  • 支持正则搜索、语法高亮、滚动命令历史、Tab 补全等 ([cgdb.github.io][5])。

三、高级技巧与定制篇

1. .gdbinit 与自动加载

  • 启用历史记录:

    set history save on
    set history size 10000
    set history filename ~/.gdb_history
    
  • 支持自动加载本地文件:

    add-auto-load-safe-path .
    

  • 项目级 .gdbinit:通过 gdb -x project.gdbinit 加载,方便团队共享 ([Interrupt][10])。

2. 条件断点、命令、watchpoint

  • 条件断点:

    break foo if count > 100
    
  • 在断点上附加命令:

    break foo
    commandsprint xcontinue
    end
    
  • 动态监视变量读写:watch varwatch -l var ([Reddit][11])。

3. Python 脚本 & 自定义宏

  • 使用 GDB Python API,编写自定义命令和输出美化功能 ([Reddit][12])。
  • 示例:定义函数 my_function 自动打印 $arg0,能在断点时执行一系列操作 ([Medium][13])。

4. Pretty Printers & 插件

  • 利用 pretty-printer 美化 STL、protobuf 等复杂结构 。
  • 安装插件如 GEF、pwndbg 来扩展内存剖析、反汇编能力 。

5. 核心转储 & 远程调试

  • 使用 gdb myprog --core core.1234 查看崩溃现场 ([Red Hat Developers][1])。
  • 使用 gdbserver + target remote 实现远端目标调试。

6. 脚本化 & 批处理

  • 启动时预设命令:

    gdb -ex "break main" -ex run myprog
    
  • 使用 -batch 模式运行脚本并自动退出,适用于 CI 环境 。


四、延伸

以下是关于 Red Hat 系列 GDB 教程Interrupt(由 Memfault 发布)的进阶技巧与 .gdbinit 配置 的详细介绍。


4.1 Red Hat 系列 GDB 教程

Red Hat 提供了一套结构清晰、由浅入深的 GDB 教学系列,适合从入门到进阶的用户。

1. 初学者指南 ▶ “Getting started with the debugger”(第一篇)
  • 作者:Keith Seitz,发表于 2021 年4月 30日。

  • 涵盖 GDB 的基础使用流程,包括:

    • 使用 gcc -g3 -O0 编译以生成完整调试信息;
    • 通过 helpapropos 探索 GDB 命令;
    • 帮助新用户移除命令分页、打开历史记录等设置 ([Red Hat Developers][1], [Stanford University][2])。
2. Printf 样式调试 ▶ “Printf‑style debugging using GDB, Part 3”
  • 作者:Kevin Buettner,发表于 2021 年12月 9日。

  • 展示如何使用断点触发函数调用,例如:

    • 设置 break insert
    • 使用 GDB 命令在每次断点触发时自动调用程序内部的打印函数,实现“打印式”调试 ([Red Hat Developers][3])。
3. 逆向执行与时间旅行 ▶ “Using GDB to time travel” 和 “Advanced time manipulation with GDB”
  • 最新一篇发表于 2025 年6月 4日,由 Guinevere Larsen 撰写。
  • 展示 GDB 的逆向调试功能 —— 如 record stopreverse-continue 等命令。
  • 介绍使用“时间循环”调试随机行为程序(例如游戏中的 hit/miss 逻辑) ([Red Hat Developers][4])。

总结:Red Hat 教程覆盖了从编译、断点与打印调试、命令自动化,再到逆向调试的完整流程,非常适合构建扎实调试能力。


4.2 Interrupt:Memfault 的进阶 GDB 使用技巧 & .gdbinit

由 Memfault 发布的文章“Advanced GDB Usage”深入提升调试效率,提供大量技巧和 .gdbinit 配置建议 ([Interrupt][5])。

核心亮点包括:
1. apropos 搜索命令

通过:

(gdb) apropos regex

快速查找相关命令,尤其适用于超过1500条 GDB 指令环境 。

2. 启用命令历史记录

.gdbinit 中加入:

set history save on
set history size 10000
set history filename ~/.gdb_history

支持 Ctrl+R 进行交互式命令搜索 ([Interrupt][5])。

3. 项目级 .gdbinit 自动加载

建议为团队创建统一的 .gdbinit,并通过脚本或 CLI 工具加载:

gdb -ix project.gdbinit myprog

便于共享宏、插件、加载配置 ([Interrupt][5])。

4. 源码路径映射

在代码路径不一致时,使用:

set substitute-path <orig> <local>
directory /local/src/…

确保调试器能正确定位源代码 。

5. 条件断点与 watchpoint
  • 条件断点示例:

    break foo if count > 100
    
  • watchpoint 示例:

    watch var
    watch -l var
    

可指定变量被 “读/写” 时触发 ([Stack Overflow][6])。

6. Pretty‑printers 与 Python 脚本
  • 自定义打印复杂结构(如 STL、protobuf);
  • 使用 Python API 编写命令或自动化脚本。
7. 多线程调试 & Backtrace for All Threads

使用 thread apply all backtrace 展示所有线程堆栈;适合 RTOS / 嵌入式环境调试 ([Medium][7], [Interrupt][8])。

8. 插件支持

推荐使用 GEF、pwndbg 等插件增强 GDB,包括内存剖析、汇编视图等功能。


4.3 .gdbinit 示例模板

# 启用历史记录
set history save on
set history size 10000
set history filename ~/.gdb_history# 禁用分页
set pagination off# 自动加载本地 init 文件
add-auto-load-safe-path .# 源码路径映射
set substitute-path /build/dir /home/dev/project
directory /home/dev/project/src# 常用断点宏
define bpfuncbreak $arg0commandssilentbacktracecontinueend
end# Condition breakpoint 示例
# break process_data if data_size > 1024# Watchpoint 示例
# watch -l config# 加载 pretty‑printer 和插件
# source ~/gef/gef.py
# source ~/pwndbg/gdbinit.py

可以将此内容保存为 ~/.gdbinit(或项目下的 project.gdbinit),并通过 gdb -ix project.gdbinit 加载。


4.4 小结

资源内容重点
Red Hat GDB 教程从基础编译、printf 风格调试到逆向调试,内容循序渐进。
Interrupt (Memfault)高效调试实用技巧:命令历史、源码映射、watchpoint、Python 自动化、插件支持等全面覆盖。

推荐做法:

  1. 先阅读 Red Hat 系列建立基础;
  2. 再应用 Interrupt 的 .gdbinit 和高级操作;
  3. 最后通过 Python 脚本 + 插件(如 GEF/pwndbg)定制调试工具链。

5 参考资料

[1]: https://developers.redhat.com/articles/the-gdb-developers-gnu-debugger-tutorial-part-1-getting-started-with-the-debugger?utm_source=chatgpt.com "Get Started with our GNU Debugger Tutorial - Red Hat Developer"
[2]: https://web.stanford.edu/class/archive/cs/cs107/cs107.1194/resources/gdb?utm_source=chatgpt.com "CS107 GDB and Debugging"
[3]: https://developers.redhat.com/articles/2021/12/09/printf-style-debugging-using-gdb-part-3?utm_source=chatgpt.com "Printf-style debugging using GDB, Part 3 - Red Hat Developer"
[4]: https://developers.redhat.com/articles/2025/06/04/advanced-time-manipulation-gdb?utm_source=chatgpt.com "Advanced time manipulation with GDB - Red Hat Developer"
[5]: https://interrupt.memfault.com/blog/advanced-gdb?utm_source=chatgpt.com "Advanced GDB Usage - Interrupt - Memfault"
[6]: https://stackoverflow.com/questions/71966464/gdbs-gdbinit-issues-annoying-feedback-when-focus-cmd-is-used?utm_source=chatgpt.com "GDB's .gdbinit issues annoying feedback when \"focus cmd\" is used"
[7]: https://olof-astrand.medium.com/advanced-debugging-with-gdb-reverse-execution-pretty-printer-and-asan-b27ef335d036?utm_source=chatgpt.com "Advanced debugging with gdb (Reverse execution , Pretty-Printer ..."
[8]: https://community.memfault.com/t/advanced-gdb-usage-interrupt/284?utm_source=chatgpt.com "Advanced GDB Usage | Interrupt - Blog"[1]: https://developers.redhat.com/articles/the-gdb-developers-gnu-debugger-tutorial-part-1-getting-started-with-the-debugger?utm_source=chatgpt.com "Get Started with our GNU Debugger Tutorial - Red Hat Developer"
[2]: https://techbeamers.com/how-to-use-gdb-top-debugging-tips/?utm_source=chatgpt.com "GDB Tutorial: Essential GDB Tips to Learn Debugging - TechBeamers"
[3]: https://web.stanford.edu/class/archive/cs/cs107/cs107.1258/resources/gdb.html?utm_source=chatgpt.com "CS107 GDB and Debugging - Stanford University"
[4]: https://stackoverflow.com/questions/1471226/most-tricky-useful-commands-for-gdb-debugger?utm_source=chatgpt.com "Most tricky/useful commands for gdb debugger - Stack Overflow"
[5]: https://cgdb.github.io/docs/cgdb.html?utm_source=chatgpt.com "CGDB Manual 0.8.0"
[6]: https://heather.cs.ucdavis.edu/~matloff/cgdb.html?utm_source=chatgpt.com "A Quick-Start Tutorial on the CGDB Debugging Interface"
[7]: https://r3x.github.io/posts/gdb_advanced/?utm_source=chatgpt.com "Advanced GDB Debugging - Siddharth Muralee"
[8]: https://cseweb.ucsd.edu/classes/fa09/cse141/tutorial_gcc_gdb.html?utm_source=chatgpt.com "Tutorial of gcc and gdb - UCSD CSE"
[9]: https://mhandroid.wordpress.com/2011/01/23/using-cgdb-with-ndk-debug-and-cgdb-tutorial/?utm_source=chatgpt.com "Using cgdb with ndk-debug (and cgdb tutorial) - Android blog"
[10]: https://interrupt.memfault.com/blog/advanced-gdb?utm_source=chatgpt.com "Advanced GDB Usage - Interrupt - Memfault"
[11]: https://www.reddit.com/r/C_Programming/comments/13tyt0z/a_quick_intro_to_gdb/?utm_source=chatgpt.com "A quick intro to gdb. : r/C_Programming - Reddit"
[12]: https://www.reddit.com/r/cpp/comments/iw3h9w/protips_for_gdb/?utm_source=chatgpt.com "Protips for GDB? : r/cpp - Reddit"
[13]: https://altmannmarcelo.medium.com/gdb-advanced-techniques-expanding-gdb-functionality-with-custom-function-execution-ccf50894f61b?utm_source=chatgpt.com "GDB Advanced Techniques: Expanding GDB Functionality with ..."

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/news/914107.shtml
繁体地址,请注明出处:http://hk.pswp.cn/news/914107.shtml
英文地址,请注明出处:http://en.pswp.cn/news/914107.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

链接代理后无法访问网络

路由方向的问题 cmd 输入 route print 查看路由多了一个不是你网络的路由 我的嘎嘎好用直接那都通 route add -p 0.0.0.0 mask 0.0.0.0 0.0.0.0 参考这个 固定ip if是代理链路的 链路口又敏感词这个文章不合规两次评论区问我

day37 早停策略和模型权重的保存

DAY 37 我今天的笔记是用cpu训练的&#xff0c;请自行修改为gpu训练 仍然是循序渐进&#xff0c;先复习之前的代码 import torch import torch.nn as nn import torch.optim as optim from sklearn.datasets import load_iris from sklearn.model_selection import train_test_…

网络爬虫分类全解析

网络爬虫作为数据获取的重要工具,其分类方式多样,不同类型的爬虫在技术实现、应用场景和功能特性上存在显著差异。深入理解这些分类,有助于开发者根据实际需求选择合适的爬虫方案。本文将从技术特性、应用场景和架构设计三个维度,系统介绍网络爬虫的主要分类。 一、按技术…

ECR仓库CloudFormation模板完整指南

概述 本文档详细介绍了一个通用的Amazon ECR(Elastic Container Registry)仓库CloudFormation模板,该模板支持多业务组、参数化配置,并包含完整的安全策略、生命周期管理和监控功能。 模板特性 核心功能 ✅ 支持4个业务组:app、ai、mall、frontend✅ 灵活的服务名手动输…

C++(STL源码刨析/List)

一 List 核心字段和接口1. 节点字段template<class T> struct __list_node {typedef void* void_pointer;void_pointer prev;void_pointer next;T data; }由于 链表 不是连续的内存块&#xff0c;所以对每一个申请到的内存块要进行统一组织&#xff0c;也就是封装成一个类…

苹果App上架流程:不用Mac也可以上架的方法

iOS App 的上架流程一直被认为是门槛最高、流程最繁琐的移动端工作之一。对很多使用 Windows 或 Linux 进行开发的跨平台团队来说&#xff0c;Mac 的缺位更放大了每一步的难度。 在我们近期为一款本地生活类 App 进行 iOS 上架时&#xff0c;团队成员几乎没有配备本地 Mac&…

【爬虫】- 爬虫原理及其入门

爬虫01 - 爬虫原理及其入门 文章目录爬虫01 - 爬虫原理及其入门一&#xff1a;爬虫原理1&#xff1a;爬虫的优势‌2&#xff1a;爬虫的核心库3&#xff1a;经典举例4&#xff1a;合规问题一&#xff1a;爬虫原理 学习爬虫之前前置知识需要了解这些&#xff1a; 我的HTTP介绍, 了…

G5打卡——Pix2Pix算法

&#x1f368; 本文为&#x1f517;365天深度学习训练营中的学习记录博客&#x1f356; 原作者&#xff1a;K同学啊 Pix2Pix 是一种基于条件生成对抗网络&#xff08;cGANs&#xff09;的图像到图像翻译算法&#xff0c;由 Phillip Isola 等人在 2016 年提出。该算法的核心思想…

动力系统模拟与推导-AI云计算数值分析和代码验证

当系统是连续的&#xff0c;并且其状态变量不仅随时间变化&#xff0c;而且随空间维度变化时&#xff0c;需要使用偏微分方程&#xff08;PDEs&#xff09;来推导运动方程。偏微分方程提供了描述这些空间分布属性如何相互作用和演化的数学框架。 选择使用常微分方程&#xff08…

P4597 序列 sequence题解

P4597 序列 sequence 给定一个数列&#xff0c;每次操作可以使任意一个数1或-1&#xff0c;求小的操作次数&#xff0c;使得数列变成不降数列. 1.对于前面比当前位的数字大的数&#xff0c;设最大数为 xxx &#xff0c;当前的数为 yyy ,则对于 xxx 到 yyy 中间的任意数&#xf…

雨污管网智慧监测系统网络建设方案:基于SD-WAN混合架构的最佳实践

随着城市化的快速推进&#xff0c;雨污管网的管理与运行面临着日益复杂的挑战&#xff0c;例如内涝、污水溢流、非法排污等问题频发。为了更高效地管理分布广泛的监测点&#xff0c;保障系统运行稳定性&#xff0c;构建一套高效、低成本、易运维的网络架构至关重要。本文将分享…

世俱杯直播数据源通过反汇编获取到

在当今的互联网体育赛事直播中&#xff0c;许多平台为了保护其直播资源&#xff0c;会采用加密、混淆或动态加载等方式隐藏真实的视频流地址&#xff08;如 .m3u8 或 .flv&#xff09;。对于普通用户和开发者来说&#xff0c;直接通过网页源码或浏览器调试器难以快速定位这些关…

字节豆包又一个新功能,超级实用,4 种玩法,你肯定用得上!(建议收藏)

前段时间&#xff0c;分享了一个非常好用的视频总结工具——百度网盘和百度文库联合推出的「AI 笔记」。它能自动根据视频内容&#xff0c;生成图文视频总结、表格总结、思维导图等。关键是带时间戳&#xff0c;能直接跳转到视频的位置。但这个功能隐藏在百度网盘里&#xff0c…

AI进化论08:机器学习的崛起——数据和算法的“二人转”,AI“闷声发大财”

上回咱们聊了第二次AI寒冬&#xff0c;AI为了“活下去”&#xff0c;不得不“改头换面”&#xff0c;从“AI”变成了“机器学习”。结果你猜怎么着&#xff1f;这“机器学习”啊&#xff0c;还真就“闷声发大财”了&#xff01;它不再执着于模拟人类的“思维过程”&#xff0c;…

【MySQL】———— 索引

作者主页&#xff1a; 作者主页 本篇博客专栏&#xff1a;Linux 创作时间 &#xff1a;2025年7月11日 Mysql索引 索引介绍 索引是什么 根据官方对索引的介绍&#xff0c;索引是帮助MySQL高效的获取数据的数据结构&#xff0c;在我看来&#xff0c;索引就相当于一本书的目…

页面html,当鼠标点击图标,移开图标,颜色方块消失

html页面代码&#xff1a;<!DOCTYPE html> <html lang"zh-CN"> <head><meta charset"UTF-8"><title>颜色选择器</title><style>body {font-family: "Microsoft YaHei", sans-serif;padding: 20px;}.c…

netdxf—— CAD c#二次开发之(netDxf 处理 DXF 文件)

1.创建新项目打开 VS2022&#xff0c;选择 "创建新项目"搜索 "控制台应用"&#xff0c;选择 ".NET 6.0 (C#)" 模板&#xff0c;点击 "下一步"项目名称&#xff1a;"DxfProcessor"&#xff0c;位置&#xff1a;自选&#xff…

如何将一个本地的jar包安装到 Maven 仓库中

我们需要执行以下步骤&#xff1a; 首先&#xff0c;打开命令提示符&#xff08;CMD&#xff09;或 PowerShell&#xff0c;执行以下命令&#xff1a; mvn install:install-file ^ -Dfile"你的jar包路径" ^ -DgroupId"组织ID" ^ -DartifactId"项目ID&…

AI赋能的企业音频智能中枢:重构会议价值提升决策效率的数字化转型实践

在当今快节奏的商业环境中&#xff0c;企业管理者每天都要处理海量信息&#xff0c;其中音频内容占据了重要位置。你是否经常遇到这样的困扰&#xff1a;重要会议结束后&#xff0c;录音文件静静躺在设备里&#xff0c;迟迟无法变成可用的会议纪要跨部门协作时&#xff0c;收到…

医学+AI!湖北中医药大学信息工程学院与和鲸科技签约101数智领航计划

为积极推动人工智能与中医药信息化深度融合&#xff0c;着力培育既精通中医药理论又掌握人工智能技术的复合型人才&#xff0c;6 月 27 日&#xff0c;湖北中医药大学信息工程学院与上海和今信息科技有限公司&#xff08;以下简称 “和鲸科技”&#xff09;召开校企合作座谈会&…