gdb入门

单文件调试–斐波那契数列

简单起见:不引入其他头文件。源码 fabonaci.cpp如下:

int fabonaci(int n) {
    if (n == 0 || n == 1) {
        return n;
    } else {
        return fabonaci(n - 1) + fabonaci(n - 2);
    }
}

int main() {
    fabonaci(10);

    return 0;
}

编译,注意加上-g参数。

g++ -g -o fabonaci fabonaci.cpp

得到可执行文件后,通过 gdb <exec-file>命令进入:

[root@localhost tmp]# gdb fabonaci
GNU gdb (GDB) Red Hat Enterprise Linux 10.2-6.el7
Copyright (C) 2021 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-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://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 fabonaci...
(gdb) 

开始程序

接下来可以通过runstart命令运行程序。两者区别:

  • run:会运行到第一个断点,没有断点的话,直接运行到程序结束。
  • start:一般会运行到main函数的起始位置

断点

打断点的命令是break,缩写为b

有多种形式。注:otherfile 为其他文件,例如 sort.cpp,而 n 代表行号。

  • break otherfile:func
  • break otherfile:n

当文件是当前文件时,只需行号或者函数名即可

(gdb) b main
Breakpoint 1 at 0x401168: file fabonaci.cpp, line 10.
(gdb) b 10
Note: breakpoint 1 also set at pc 0x401168.
Breakpoint 2 at 0x401168: file fabonaci.cpp, line 10.

info b查看断点

(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000401168 in main() at fabonaci.cpp:10
2       breakpoint     keep y   0x0000000000401168 in main() at fabonaci.cpp:10

delete删除断点

  • delete // 删除全部断点
  • delete n // 删除指定断点,n 表示断点号
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000401168 in main() at fabonaci.cpp:10
2       breakpoint     keep y   0x0000000000401168 in main() at fabonaci.cpp:10
(gdb) del 2
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000401168 in main() at fabonaci.cpp:10

使断点生效/失效

  • disable n
  • enable n

运行程序

在fabonaci函数处再加一个断点

(gdb) b 5
Breakpoint 3 at 0x40113f: file fabonaci.cpp, line 5.
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000401168 in main() at fabonaci.cpp:10
3       breakpoint     keep y   0x000000000040113f in fabonaci(int) at fabonaci.cpp:5
  • next :类比于 IDE 不进入函数的单步调试,next 可以用 n 缩写替代
  • step: 类比于 IDE 进入函数的单步调试,step 可以用 s 缩写替代
  • continue:继续执行直到遇到下一个断点停下,没有断点直接结束,可以用 c 缩写替代
  • until n: 直接跳转到指定行号程序,n 表示行号
  • finish:当程序运行在函数中时,直接执行完当前函数并打印函数返回信息

开始程序

(gdb) run
Starting program: /root/cpp/tmp/fabonaci 
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_9.x86_64

Breakpoint 1, main () at fabonaci.cpp:10
10          fabonaci(10);

不进入函数单步调试next

(gdb) n

Breakpoint 3, fabonaci (n=10) at fabonaci.cpp:5
5               return fabonaci(n - 1) + fabonaci(n - 2);
(gdb) n

Breakpoint 3, fabonaci (n=9) at fabonaci.cpp:5
5               return fabonaci(n - 1) + fabonaci(n - 2);

查看调用栈backtrace,缩写bt

(gdb) bt
#0  fabonaci (n=9) at fabonaci.cpp:5
#1  0x000000000040114c in fabonaci (n=10) at fabonaci.cpp:5
#2  0x0000000000401172 in main () at fabonaci.cpp:10
(gdb) n

Breakpoint 3, fabonaci (n=8) at fabonaci.cpp:5
5               return fabonaci(n - 1) + fabonaci(n - 2);
(gdb) bt
#0  fabonaci (n=8) at fabonaci.cpp:5
#1  0x000000000040114c in fabonaci (n=9) at fabonaci.cpp:5
#2  0x000000000040114c in fabonaci (n=10) at fabonaci.cpp:5
#3  0x0000000000401172 in main () at fabonaci.cpp:10

进入函数的单步调试step或s

(gdb) step
fabonaci (n=7) at fabonaci.cpp:2
2           if (n == 0 || n == 1) {
(gdb) s

Breakpoint 3, fabonaci (n=7) at fabonaci.cpp:5
5               return fabonaci(n - 1) + fabonaci(n - 2);

使断点失效

(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000401168 in main() at fabonaci.cpp:10
        breakpoint already hit 1 time
3       breakpoint     keep y   0x000000000040113f in fabonaci(int) at fabonaci.cpp:5
        breakpoint already hit 5 times
(gdb) disable 3

直接跳转until

(gdb) until 11
0x000000000040114c in fabonaci (n=7) at fabonaci.cpp:5
5               return fabonaci(n - 1) + fabonaci(n - 2);
(gdb) until 11
0x000000000040114c in fabonaci (n=8) at fabonaci.cpp:5
5               return fabonaci(n - 1) + fabonaci(n - 2);
(gdb) until 11
0x000000000040114c in fabonaci (n=9) at fabonaci.cpp:5
5               return fabonaci(n - 1) + fabonaci(n - 2);
(gdb) until 11
0x000000000040114c in fabonaci (n=10) at fabonaci.cpp:5
5               return fabonaci(n - 1) + fabonaci(n - 2);
(gdb) until 11
main () at fabonaci.cpp:12
12          return 0;

查看信息info

info address -- Describe where symbol SYM is stored.
info all-registers -- List of all registers and their contents, for selected stack frame.
info args -- All argument variables of current stack frame or those matching REGEXPs.
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, info b -- Status of specified breakpoints (all user-settable breakpoints if no argument).
info checkpoints -- IDs of currently known checkpoints.
info classes -- All Objective-C classes, or those matching REGEXP.
info common -- Print out the values contained in a Fortran COMMON block.
info connections -- Target connections in use.
info copying -- Conditions for redistributing copies of GDB.
info dcache -- Print information on the dcache performance.
info display -- Expressions to display when program stops, with code numbers.
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, info f -- All about the selected stack frame.
info frame-filter -- List all registered Python frame-filters.
info functions -- All function names or those matching REGEXPs.
info guile, info gu -- Prefix command for Guile info displays.
info inferiors -- Print a list of inferiors being managed.
info line -- Core addresses of the code for a source line.
--Type <RET> for more, q to quit, c to continue without paging--
info locals -- All local variables of current stack frame or those matching REGEXPs.
info macro -- Show the definition of MACRO, and it's source location.
info macros -- Show the definitions of all macros at LINESPEC, or the current source location.
info mem -- Memory region attributes.
info module -- Print information about modules.
info modules -- All module names, or those matching REGEXP.
info os -- Show OS data ARG.
info pretty-printer -- GDB command to list all registered pretty-printers.
info probes -- Show available static probes.
info proc -- Show additional information about a process.
info program -- Execution status of the program.
info record, info rec -- Info record options.
info registers, info r -- List of integer registers and their contents, for selected stack frame.
info scope -- List the variables local to a scope.
info selectors -- All Objective-C selectors, or those matching REGEXP.
info sharedlibrary, info dll -- Status of loaded shared object libraries.
info signals, info handle -- 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 -- All source files in the program or those matching REGEXP.
info stack, info s -- Backtrace of the stack, or innermost COUNT frames.
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 <RET> for more, q to quit, c to continue without paging--
info threads -- Display currently known threads.
info tracepoints, info tp -- Status of specified tracepoints (all tracepoints if no argument).
info tvariables -- Status of trace state variables and their values.
info type-printers -- GDB command to list all registered type-printers.
info types -- All type names, or those matching REGEXP.
info unwinder -- GDB command to list unwinders.
info variables -- All global and static variable names or those matching REGEXPs.
info vector -- Print the status of the vector unit.
info vtbl -- Show the virtual function table for a cpp object.
info warranty -- Various kinds of warranty you do not have.
info watchpoints -- Status of specified watchpoints (all watchpoints if no argument).
info win -- List of all displayed windows.
info xmethod -- GDB command to list registered xmethod matchers.

查看参数(info args)

(gdb) n

Breakpoint 3, fabonaci (n=8) at fabonaci.cpp:5
5               return fabonaci(n - 1) + fabonaci(n - 2);
(gdb) info args
n = 8

查看符号地址(info address < symbol >)

(gdb) info address fabonaci(int) 
Symbol "fabonaci(int)" is a function at address 0x401122.
(gdb) info address main
Symbol "main()" is a function at address 0x401164.

查看地址对应的符号(info symbol < address >)

(gdb) info address main
Symbol "main()" is a function at address 0x401164.
(gdb) info symbol
Argument required (address).
(gdb) info symbol 0x401164
main in section .text of /root/cpp/tmp/fabonaci

查看进程信息(info proc)

(gdb) info proc
process 87334
cmdline = '/root/cpp/tmp/fabonaci'
cwd = '/root/cpp/tmp'
exe = '/root/cpp/tmp/fabonaci

查看所有寄存器内容(info all-registers)

(gdb) info all-registers 
rax            0x8                 8
rbx            0x0                 0
rcx            0x40                64
rdx            0x7fffffffd7a8      140737488345000
rsi            0x7fffffffd798      140737488344984

........

查看所有全局变量以及静态变量(info variables)

(gdb) info variables
All defined variables:

Non-debugging symbols:
0x0000000000402000  _IO_stdin_used
0x0000000000402008  __dso_handle
0x0000000000402010  __GNU_EH_FRAME_HDR
0x000000000040215c  __FRAME_END__
0x0000000000403de8  __frame_dummy_init_array_entry
0x0000000000403de8  __init_array_start
0x0000000000403df0  __do_global_dt
....

查看当前源文件信息(info source)

(gdb) info source
Current source file is fabonaci.cpp
Compilation directory is /root/cpp/tmp
Located in /root/cpp/tmp/fabonaci.cpp
Contains 13 lines.
Source language is cpp.
Producer is GNU cpp14 9.3.1 20200408 (Red Hat 9.3.1-2) -mtune=generic -march=x86-64 -g.
Compiled with DWARF 2 debugging format.
Does not include preprocessor macro info.

查看动态库(info sharedlibrary)

(gdb) info sharedlibrary
From                To                  Syms Read   Shared Object Library
0x00007ffff7ddbaf0  0x00007ffff7df7060  Yes (*)     /lib64/ld-linux-x86-64.so.2
0x00007ffff7b2e4e0  0x00007ffff7b9559a  Yes (*)     /lib64/libstdcpp.so.6
0x00007ffff77d6350  0x00007ffff7841336  Yes (*)     /lib64/libm.so.6
0x00007ffff75bdad0  0x00007ffff75cd285  Yes (*)     /lib64/libgcc_s.so.1
0x00007ffff720c9f0  0x00007ffff735cb6f  Yes (*)     /lib64/libc.so.6
(*): Shared library is missing debugging information.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值