当前位置: 首页 > article >正文

GDB 1、超详细的GDB入门笔记,包含演示代码,快速入门

01、程序

#include<iostream>
#include<vector>
#include<string>
using namespace std;

void printVector(vector<string>& v){
    cout<<"size = "<<v.size()<<endl;
    cout<<"print begin..."<<endl;
    for (int i = 0; i < v.size(); i++){
        cout<<v[i]<<" ";
    }
    cout<<endl<<"print end..."<<endl;
    cout<<endl;
}

int main(){

    vector<string> v(10, "mhzzj");

	//修改指定下标的数据
	v[6] = "MHZZJ";

	//删除尾部数据(v[n-1])
	v.pop_back();

	//添加数据到尾部(v[n-1])
	v.push_back("zzjmhz");

	printVector(v);

	//删除下标为v[0]的数据,头部
	v.erase(v.begin());
	//边迭代边删除
	vector<string>::iterator it = v.begin();
	while (it != v.end()) {
		if (*it == "zzjmhz") it = v.erase(it);
		else it++;
	}
	printVector(v);
    cout<<"++++++++++++++++++++endl+++++++++++++++++++++++"<<endl;
}

02、GDB调试

生成调试程序

mhzzj@mhzzj-virtual-machine:~/codes/C++$ g++ gdbdemo.cpp -o demo -g

如果是c语言可以使用gcc gdbdemo.cpp -o demo -g

进入gdb调试

gdb demo

mhzzj@mhzzj-virtual-machine:~/codes/C++$ gdb demo
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 demo...done.

显示源码

list:现实源码,可以简写为:l

(gdb) l
2	#include<vector>
3	#include<string>
4	using namespace std;
5	
6	void printVector(vector<string>& v){
7	    cout<<"size = "<<v.size()<<endl;
8	    cout<<"print begin..."<<endl;
9	    for (int i = 0; i < v.size(); i++){
10	        cout<<v[i]<<" ";
11	    }
(gdb) 

如果想查看第n行前后的代码,可以使用l n命令

设置断点

break设置断点,可以简写为:b

(gdb) b 29 #在第29行设置断点
Breakpoint 1 at 0x4011e5: file gdbdemo.cpp, line 29.
(gdb) b printVector
Breakpoint 2 at 0x401003: file gdbdemo.cpp, line 7.

查看断点

(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004011e5 in main() at gdbdemo.cpp:29
2       breakpoint     keep y   0x0000000000401003 in printVector(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&) at gdbdemo.cpp:7

删除断点

(gdb) d 2 #删除第2个断点
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004011e5 in main() at gdbdemo.cpp:29

启动程序

启动程序run,可以简写为:r

(gdb) r
Starting program: /home/mhzzj/codes/C++/demo 

Breakpoint 1, main () at gdbdemo.cpp:29
29		printVector(v);

使用run命令可以直接运行到断点的位置,如果想从mian函数开始执行,可以使用start指令

查看变量

查看变量指令为print,可以简写为p

Breakpoint 1, main () at gdbdemo.cpp:29
29		printVector(v);
(gdb) p v
$1 = std::vector of length 10, capacity 10 = {"mhzzj", "mhzzj", "mhzzj", "mhzzj", "mhzzj", "mhzzj", "MHZZJ", "mhzzj", "mhzzj", "zzjmhz"}

监控某个变量

(gdb) display v
1: v = std::vector of length 8, capacity 10 = {"mhzzj", "mhzzj", "mhzzj", "mhzzj", "mhzzj", "MHZZJ", "mhzzj", "mhzzj"}
(gdb) n
size = 8
8	    cout<<"print begin..."<<endl;
1: v = std::vector of length 8, capacity 10 = {"mhzzj", "mhzzj", "mhzzj", "mhzzj", "mhzzj", "MHZZJ", "mhzzj", "mhzzj"}
(gdb) 

可以使用undispla命令取消监控

执行下一行

执行下一行next,可简写为:n

Breakpoint 1, main () at gdbdemo.cpp:29
29		printVector(v);
(gdb) display v
1: v = std::vector of length 10, capacity 10 = {"mhzzj", "mhzzj", "mhzzj", "mhzzj", "mhzzj", "mhzzj", "MHZZJ", "mhzzj", "mhzzj", "zzjmhz"}
(gdb) n
size = 10
print begin...
mhzzj mhzzj mhzzj mhzzj mhzzj mhzzj MHZZJ mhzzj mhzzj zzjmhz 
print end...

32		v.erase(v.begin());
1: v = std::vector of length 10, capacity 10 = {"mhzzj", "mhzzj", "mhzzj", "mhzzj", "mhzzj", "mhzzj", "MHZZJ", "mhzzj", "mhzzj", "zzjmhz"}
(gdb) n
34		vector<string>::iterator it = v.begin();
1: v = std::vector of length 9, capacity 10 = {"mhzzj", "mhzzj", "mhzzj", "mhzzj", "mhzzj", "MHZZJ", "mhzzj", "mhzzj", "zzjmhz"}
(gdb) 

执行到下一个断点

continue 继续运行程序,停在下一个断点的位置,可简写为:c

(gdb) b 39 #在第39行设置断点
Breakpoint 3 at 0x40128f: file gdbdemo.cpp, line 39.
(gdb) c #执行到下一个断点
Continuing.

Breakpoint 3, main () at gdbdemo.cpp:39
39		printVector(v);

进入函数内部

如果执行到某个函数,想进入函数内部,可以使用step指令,简写为s

如果在改行为函数调用,则进入函数内部,否则为执行下一行,效果如图n指令

Breakpoint 3, main () at gdbdemo.cpp:39
39		printVector(v);

(gdb) s
printVector (v=std::vector of length 8, capacity 10 = {...}) at gdbdemo.cpp:7
7	    cout<<"size = "<<v.size()<<endl;

查看栈帧

(gdb) bt
#0  printVector (v=std::vector of length 8, capacity 10 = {...}) at gdbdemo.cpp:7
#1  0x000000000040129b in main () at gdbdemo.cpp:39

切换栈帧

(gdb) frame 1
#1  0x000000000040129b in main () at gdbdemo.cpp:39
39		printVector(v);
(gdb) 

退出进入的函数

退出进入的函数:finish

(gdb) frame 1
#1  0x000000000040129b in main () at gdbdemo.cpp:39
39		printVector(v);
(gdb) frame 0
#0  printVector (v=std::vector of length 8, capacity 10 = {...}) at gdbdemo.cpp:7
7	    cout<<"size = "<<v.size()<<endl;
(gdb) finish
Run till exit from #0  printVector (v=std::vector of length 8, capacity 10 = {...}) at gdbdemo.cpp:7
size = 8
print begin...
mhzzj mhzzj mhzzj mhzzj mhzzj MHZZJ mhzzj mhzzj 
print end...

main () at gdbdemo.cpp:40
40	    cout<<"++++++++++++++++++++endl+++++++++++++++++++++++"<<endl;
1: v = std::vector of length 8, capacity 10 = {"mhzzj", "mhzzj", "mhzzj", "mhzzj", "mhzzj", "MHZZJ", "mhzzj", "mhzzj"}


http://www.kler.cn/a/15916.html

相关文章:

  • 支持向量机SVM——基于分类问题的监督学习算法
  • 云计算复习文档
  • 金山云Java 开发面试题及参考答案
  • 【2025最新计算机毕业设计】基于SpringBoot+Vue电脑在线装机指南教程网站【源码+文档】
  • 如何保证MySQL与Redis缓存的数据一致性?
  • Python调用API翻译Excel中的英语句子并回填数据
  • Prompt炼丹炉——一系列Prompt自动优化的实践记录
  • python使用公共api下载狗狗图片
  • Cadence基础操作:Schematic编辑
  • vue+element Ui 树型组件tree懒加载+搜索框远程请求数据为平铺类型
  • TF-IDF (BigData, Data Mining)
  • 【LeetCode: 62. 不同路径 | 暴力递归=>记忆化搜索=>动态规划 】
  • 设计模式-适配器模式
  • RabbitMQ 工作队列模式 Work Queue Demo
  • C S S
  • 制药专业转行软件测试,带我的师傅在这干了两年半,最终还是跑路了......
  • 第11届蓝桥杯省赛真题剖析-2020年6月21日Scratch编程初中级组
  • 渗透测试 | 目录扫描
  • 英语基础-名词
  • 基于Mediapipe手势识别
  • 一文吃透Http协议
  • 国民技术N32G430开发笔记(8)- 内部Flash的读写操作
  • Python程序员如何写简历容易找到好工作?分享4个制作简历的细节
  • API接口安全—webservice、Swagger、WEBpack
  • 【Hello Network】网络编程套接字(三)
  • 颜色空间转换RGB-YCbCr