GDB 常用命令 跳出函数 打印变量

在这里插入图片描述

1 设置断点

b function_name
b main.cpp:122 
条件触发
b function_name if i==2

2 显示所有断点信息

i b

help一下, 查看命令详细用法

info命令还有很多功能,怎么进一步了解info呢:用到help命令
help 命令

(gdb) help info
Generic command for showing things about the program being debugged.

List of info subcommands:

info address -- Describe where symbol SYM is stored
info all-registers -- List of all registers and their contents
info args -- Argument variables of current stack frame
info auto-load -- Print current status of auto-loaded files
info auxv -- Display the inferior's auxiliary vector
info bookmarks -- Status of user-settable bookmarks
info breakpoints -- Status of specified breakpoints (all user-settable breakpoints if no argument)
info checkpoints -- IDs of currently known checkpoints
info classes -- All Objective-C classes
info common -- Print out the values contained in a Fortran COMMON block
info copying -- Conditions for redistributing copies of GDB
info dcache -- Print information on the dcache performance
info display -- Expressions to display when program stops
info exceptions -- List all Ada exception names
info extensions -- All filename extensions associated with a source language
info files -- Names of targets and files being debugged
info float -- Print the status of the floating point unit
info frame -- All about selected stack frame
info functions -- All function names
info guile -- Prefix command for Guile info displays
info handle -- What debugger does when program gets various signals
info inferiors -- IDs of specified inferiors (all inferiors if no argument)
info line -- Core addresses of the code for a source line
info locals -- Local variables of current stack frame
info macro -- Show the definition of MACRO
info macros -- Show the definitions of all macros at LINESPEC
info mem -- Memory region attributes
info os -- Show OS data ARG
info probes -- Show available static probes
info proc -- Show /proc process information about any running process
info program -- Execution status of the program
info record -- Info record options
info registers -- List of integer registers and their contents
info scope -- List the variables local to a scope
info selectors -- All Objective-C selectors
info set -- Show all GDB settings
info sharedlibrary -- Status of loaded shared object libraries
info signals -- What debugger does when program gets various signals
info skip -- Display the status of skips
info source -- Information about the current source file
info sources -- Source files in the program
info stack -- Backtrace of the stack
info static-tracepoint-markers -- List target static tracepoints markers
info symbol -- Describe what symbol is at location ADDR
info target -- Names of targets and files being debugged
info tasks -- Provide information about all known Ada tasks
info terminal -- Print inferior's saved terminal status
---Type <return> to continue, or q <return> to quit---
info threads -- Display currently known threads
info tracepoints -- Status of specified tracepoints (all tracepoints if no argument)
info tvariables -- Status of trace state variables and their values
info types -- All type names
info variables -- All global and static variable names
info vector -- Print the status of the vector unit
info vtbl -- Show the virtual function table for a C++ object
info warranty -- Various kinds of warranty you do not have
info watchpoints -- Status of specified watchpoints (all watchpoints if no argument)

3 使能/取消断点

dis 3               # 将第3个断点设置为无效
ena 2               # 将第2个断点设置有效 

4 删除断点

d 断点id

5 控制

c/continue         # 向下运行到下一个断点处
n/next             # 执行下一行代码,不进入调用的函数,直接返回结果
s/step             # 执行下一行代码,进入调用的函数
finish             # 跳出函数体
until    		   # 跳出当前循环 在执行完循环体内的最后一条语句之后执行 until,才可以跳出循环

6 变量

p  变量名                      # 打印变量值,可以答应在当前作用域之内的变量名
ptype 变量名                   # 打印变量类型

display 变量名                  # 自动打印指定变量的值
i display         			   # 查看所有设置自动打印的变量值
undisplay 编号     			   # 取消自动打印变量值

set var 变量名=变量值           # 可以临时改变该变量的值               

7 格式打印变量

一般来说,GDB 会根据变量的类型输出变量的值。但你也可以自定义 GDB 的输出的格式。例 如,你想输出一个整数的十六进制,或是二进制来查看这个整型变量的中的位的情况。要做 到这样,你可以使用 GDB 的数据显示格式:
x 按十六进制格式显示变量
d 按十进制格式显示变量
u 按十六进制格式显示无符号整型
o 按八进制格式显示变量
t 按二进制格式显示变量
a 按十六进制格式显示变量
c 按字符格式显示变量
f 按浮点数格式显示变量

p   i     #正常打印变量i
p/a i     #16进制打印

8 跳转命令 jump

一般来说,被调试程序会按照程序代码的运行顺序依次执行。GDB 提供了乱序执行的功能, 也就是说,GDB可以修改程序的执行顺序,可以让程序执行随意跳跃。这个功能可以由 GDB 的 jump 命令来完: jump指定下一条语句的运行点。可以是文件的行号,可以是 file:line 格式,可以是+num 这种偏移量格式。表式着下一条运行语句从哪里开始。

jump *address

这里的 是代码行的内存地址。 注意,jump 命令不会改变当前的程序栈中的内容,所以,当你从一个函数跳到另一个函数时,当函数运行完返回时进行弹栈操作时必然会发生错误,可能结果还是非常奇怪的,甚至 于产生程序 CoreDump。所以最好是同一个函数中进行跳转。 熟悉汇编的人都知道,程序运行时,有一个寄存器用于保存当前代码所在的内存地址。所 以,jump命令也就是改变了这个寄存器中的值。于是,你可以使用“set $pc”来更改跳转执 行的地址。如:

set $pc = 0x485

9 产生信号

使用 singal 命令,可以产生一个信号量给被调试的程序。如:中断信号 Ctrl+C。这非常方 便于程序的调试,可以在程序运行的任意位置设置断点,并在该断点用 GDB 产生一个信号 量,这种精确地在某处产生信号非常有利程序的调试。 语法是:signal ,UNIX 的系统信号量通常从 1 到 15。所以取值也在这个范围。 single 命令和 shell 的 kill 命令不同,系统的 kill 命令发信号给被调试程序时,是由 GDB 截获的,而 single 命令所发出一信号则是直接发给被调试程序的

10 强制函数返回

return
或者
return 返回值

11 强制调用函数

call expr

表达式中可以一是函数,以此达到强制调用函数的目的。并显示函数的返回值,如果函数返 回值是 void,那么就不显示。

print expr

另一个相似的命令也可以完成这一功能——print,print 后面可以跟表达式,所以也可以 用他来调用函数,print 和 call 的不同是,如果函数返回 void,call 则不显示,print 则 显示函数返回值,并把该值存入历史数据中

12 backtrace

(gdb) backtrace 
#0 0x00007ffff7b3d1e7 in nanosleep () from /lib64/libc.so.6 
#1 0x00007ffff7b3d11e in sleep () from /lib64/libc.so.6 
#2 0x000000000040113f in deadlock () at test.cc:13

***************************** GDB官方文档 ***************************

这里只是记录了一些简单的命令,更多详细的手法可以在官网找到.
GDB官网—用户操作手册
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值