GDB 指南

2022-10-25  本文已影响0人  Wancho

生成调试信息

一般地,只需要添加-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 回退

上一篇下一篇

猜你喜欢

热点阅读