C++ GDB调试器

What is GDB?

GDB, the GNU Project debugger, allows you to see what is going on `inside’ another program while it executes – or what another program was doing at the moment it crashed.

GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

  • Start your program, specifying anything that might affect its behavior.
  • Make your program stop on specified conditions.
  • Examine what has happened, when your program has stopped.
  • Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.

What Languages does GDB Support?

AssemblyCC++Fortran
GoObjective-COpenCLRust

How to install?

安装GDB

yum install gdb

查看GBD版本

gdb --version

Quick Start

以下使用 森林中的兔子 代码:

#include<bits/stdc++.h>

using namespace std;

int main() {
    unordered_map<int, int> mp;
    int n, t, ans = 0;
    cin >> n;
    while (n--) {
        cin >> t;
        ++mp[t];
    }
    for (auto& [x, y]:mp) {
        ans += (x + y) / (x + 1) * (x + 1);
    }
    cout << ans << endl;
    return 0;
}

以下是该程序的输入与输出示例:

(base) cilinyan@amax:~/clyan/xshell$ g++ -std=c++17 main.cpp -o main
(base) cilinyan@amax:~/clyan/xshell$ ./main
3 10 10 10
11

使用GDB调试程序需要使用g++ -g main.cpp 命令:

g++ -std=c++17 -g main.cpp -o main

使用 gdb ./main 运行程序:

(base) cilinyan@amax:~/clyan/xshell$ gdb ./main
GNU gdb (Ubuntu 9.1-0ubuntu1) 9.1
Copyright (C) 2020 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-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://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 ./main...
(gdb) 

使用 r(un) 运行程序,使用 q(uit) 退出程序

(gdb) run
Starting program: /data2/cilinyan/clyan/xshell/main 
3 10 10 10
11
[Inferior 1 (process 1735585) exited normally]
(gdb) quit
(base) cilinyan@amax:~/clyan/xshell$

使用 man gdb 查看帮助

Here are some of the most frequently needed GDB commands:

break [file:]function
	Set a breakpoint at function (in file).

run [arglist]
	Start your program (with arglist, if specified).

bt  Backtrace: display the program stack.

print expr
	Display the value of an expression.

c   Continue running your program (after stopping, e.g. at a breakpoint).

next
	Execute next program line (after stopping); step over any function calls in the line.

edit [file:]function
	look at the program line where it is presently stopped.

list [file:]function
     type the text of the program in the vicinity of where it is presently stopped.

step
     Execute next program line (after stopping); step into any function calls in the line. (退出使用finish)

help [name]
	Show information about GDB command name, or general information about using GDB.

quit
	Exit from GDB.

常用命令:

b [line] 打断点

(gdb) b 17
Breakpoint 3 at 0x555555555451: file main.cpp, line 17.

info b 查看断点信息

(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000555555555354 in main() at main.cpp:9
	breakpoint already hit 3 times
2       breakpoint     keep y   0x00005555555553a1 in main() at main.cpp:14
3       breakpoint     keep y   0x0000555555555451 in main() at main.cpp:17

p [param] 查看变量

(gdb) p mp
$3 = std::unordered_map with 1 element = {[10] = 2}

c Continue running your program (after stopping, e.g. at a breakpoint).

next Execute next program line (after stopping); step over any function calls in the line.

step Execute next program line (after stopping); step into any function calls in the line.

shell [command] 使用 shell 命令

set logging on 使用日志功能

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值