GDB 指南

生成调试信息

一般地,只需要添加-g编译器参数即可,如g++ -g -std=c++17 main.cpp -o prog

参数 说明
-g Produce debugging information in the operating system's native format (stabs, COFF, XCOFF, or DWARF).
-ggdb Produce debugging information for use by GDB.
-g1 Level 1 produces minimal information, enough for making backtraces in parts of the program that you don't plan to debug.
-g2 The default level is 2
-g3 Level 3 includes extra information, such as all the macro definitions present in the program.

GDB TUI (Text User Interface)

命令:gdb --tui
也可以使用gdb --tui --slient减少不必要的信息

关键命令

命令 短命令 说明
start 启动程序并在main函数暂停
run r 启动程序
next n 下一步
step s 进入函数
up 跳出函数
break br or b 断点,可以指定行号br n或者函数名 br fun
continue c or fg 继续运行直到触发断点
info locals 查看本地变量
print p 打印变量print var
watch 监控变量值的变化watch var
backtrace or where bt 打印调用栈
layout 切换源码视图layout src或者汇编视图layout asm

附加到进程

步骤:

  1. 通过ps -elf | grep program_nameps aux | grep program_name获得进程PID
  2. 使用命令sudo gdb attach pid or gdb --pid=pid or gdb -p pid

回退

在程序运行后使用target record-full
需要时使用命令reverse-nextreverse-step 回退

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在gdb中键入help all可以看到所有gdb-peda组件的命令。复制粘贴如下以备查: Command cla...
    飞熊先生阅读 2,586评论 0 0
  • GDB快速指南 版本 2 (2009 6月 14) 原文 这是一个非常快速的GDB指南,旨在让你开始在终端的命令行...
    Leon_Geo阅读 986评论 0 2
  • gdb 的组成架构 gdb 基本工作原理 gdb 通过系统调用 ptrace 来接管一个进程的执行。ptrace ...
    与狼共舞666阅读 1,866评论 0 3
  • GDB gdb调试中直接输入回车是重复上一步命令 start:临时断点打在main函数处,等候进一步指令 r(ru...
    廖少少阅读 742评论 0 0
  • //联系人:石虎QQ: 1224614774昵称:嗡嘛呢叭咪哄 一、概念 英文版:详细链接:点击跳转链接 Debu...
    石虎132阅读 547评论 1 4