首页 > 其他分享 >gdb&gdb远程调试

gdb&gdb远程调试

时间:2022-12-26 11:46:40浏览次数:40  
标签:keep gdb gdbDebug breakpoint cpp 断点 远程 调试

目录

1.环境准备

# yum -y install gdb gdbserver

1.1 C++示例代码

/*!
* Copyright (c) 2021,ZYF.
* All Rights Reserved.
*
* \file main.cpp
* \brief 
*
* \author ZYF
* \date 2021/1/27 22:49:13
* \version 1.0.0
*/
#include <stdio.h>

void Demo1()
{
	int nSum = 0;
	for (int i = 0; i < 10; i++)
	{
		nSum += i;
	}

	printf("Demo1 sum is %d\n", nSum);
}

void Demo2()
{
	int nSum = 0;
	for (int i = 0; i < 5; i++)
	{
		nSum += i;
	}

	printf("Demo2 sum is %d\n", nSum);
}

int main(void)
{

	Demo1();

	Demo2();

	int nSum = 0;
	for (int i = 0; i < 5; i++)
	{
		nSum += i;
	}

	printf("Main sum is %d\n", nSum);

	return 0;
}

1.2 编译代码

[root@bogon GCC]# gcc -g gdbDebug.cpp -o gdbDebug
[root@bogon GCC]# ./gdbDebug 
Demo1 sum is 45
Demo2 sum is 10
Main sum is 10
[root@bogon GCC]# 

1.3 调试代码

[root@bogon GCC]# gdb -q gdbDebug
Reading symbols from /GCC/gdbDebug...done.
(gdb) r
Starting program: /GCC/gdbDebug 
Demo1 sum is 45
Demo2 sum is 10
Main sum is 10
[Inferior 1 (process 37875) exited normally]
Missing separate debuginfos, use: debuginfo-install glibc-2.17-157.el7.x86_64
(gdb) 

2. gdb断点调试

2.1 设置断点

2.1.1 指定行处设置断点

b 指定行n:如“b 39”则代表在代码的第39行设置断点

(gdb) b 39
Breakpoint 1 at 0x4005b1: file gdbDebug.cpp, line 39.
(gdb) b 41
Breakpoint 2 at 0x4005b6: file gdbDebug.cpp, line 41.
(gdb) b 49
Breakpoint 3 at 0x4005db: file gdbDebug.cpp, line 49.
(gdb) b 22 
Breakpoint 4 at 0x400555: file gdbDebug.cpp, line 22.
(gdb) b 33
Breakpoint 5 at 0x400593: file gdbDebug.cpp, line 33.
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005b1 in main() at gdbDebug.cpp:39
2       breakpoint     keep y   0x00000000004005b6 in main() at gdbDebug.cpp:41
3       breakpoint     keep y   0x00000000004005db in main() at gdbDebug.cpp:49
4       breakpoint     keep y   0x0000000000400555 in Demo1() at gdbDebug.cpp:22
5       breakpoint     keep y   0x0000000000400593 in Demo2() at gdbDebug.cpp:33
(gdb) 

2.1.2、指定函数处设置断点

b 指定函数名称:如“b Demo1”则代表在函数Demo1处设置断点

(gdb) b Demo1
Breakpoint 6 at 0x400535: file gdbDebug.cpp, line 16.
(gdb) b Demo2
Breakpoint 7 at 0x400573: file gdbDebug.cpp, line 27.
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005b1 in main() at gdbDebug.cpp:39
2       breakpoint     keep y   0x00000000004005b6 in main() at gdbDebug.cpp:41
3       breakpoint     keep y   0x00000000004005db in main() at gdbDebug.cpp:49
4       breakpoint     keep y   0x0000000000400555 in Demo1() at gdbDebug.cpp:22
5       breakpoint     keep y   0x0000000000400593 in Demo2() at gdbDebug.cpp:33
6       breakpoint     keep y   0x0000000000400535 in Demo1() at gdbDebug.cpp:16
7       breakpoint     keep y   0x0000000000400573 in Demo2() at gdbDebug.cpp:27
(gdb) 

2.1.3、设置条件断点

b 行号n if 断点条件:如“b 1 if i=30”则代表当i等于30时,在源码的第1行处设置断点

(gdb) b 32 if nTest=2
Breakpoint 2 at 0x400590: file gdbDebug.cpp, line 32.
(gdb) i b
Num     Type           Disp Enb Address            What
2       breakpoint     keep y   0x0000000000400590 in Demo2() at gdbDebug.cpp:32
	stop only if nTest=2
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /GDB/gdbDebug 
Demo1 sum is 45

Breakpoint 2, Demo2 () at gdbDebug.cpp:32
32			printf("Demo2 nTest is %d\n",nTest);
(gdb) n
Demo2 nTest is 2
33			nSum += i;
(gdb) print nTest
$2 = 2
(gdb) 

2.2、查看断点信息

i b:查看断点信息

(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005b1 in main() at gdbDebug.cpp:39
2       breakpoint     keep y   0x00000000004005b6 in main() at gdbDebug.cpp:41
3       breakpoint     keep y   0x00000000004005db in main() at gdbDebug.cpp:49
4       breakpoint     keep y   0x0000000000400555 in Demo1() at gdbDebug.cpp:22
5       breakpoint     keep y   0x0000000000400593 in Demo2() at gdbDebug.cpp:33
6       breakpoint     keep y   0x0000000000400535 in Demo1() at gdbDebug.cpp:16
7       breakpoint     keep y   0x0000000000400573 in Demo2() at gdbDebug.cpp:27
(gdb) 

2.3、禁用断点

dis 断点编号n:如“dis 7”则代表禁用第7个编号的断点

(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005b1 in main() at gdbDebug.cpp:39
2       breakpoint     keep y   0x00000000004005b6 in main() at gdbDebug.cpp:41
3       breakpoint     keep y   0x00000000004005db in main() at gdbDebug.cpp:49
5       breakpoint     keep n   0x0000000000400593 in Demo2() at gdbDebug.cpp:33
6       breakpoint     keep y   0x0000000000400535 in Demo1() at gdbDebug.cpp:16
7       breakpoint     keep y   0x0000000000400573 in Demo2() at gdbDebug.cpp:27
(gdb) dis 7
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005b1 in main() at gdbDebug.cpp:39
2       breakpoint     keep y   0x00000000004005b6 in main() at gdbDebug.cpp:41
3       breakpoint     keep y   0x00000000004005db in main() at gdbDebug.cpp:49
5       breakpoint     keep n   0x0000000000400593 in Demo2() at gdbDebug.cpp:33
6       breakpoint     keep y   0x0000000000400535 in Demo1() at gdbDebug.cpp:16
7       breakpoint     keep n   0x0000000000400573 in Demo2() at gdbDebug.cpp:27
(gdb) 

2.4、删除断点

2.4.1、根据断点编号删除断点

delete 断点编号n:如“delete 3”则代表删除第3个编号的断点

(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005b6 in main() at gdbDebug.cpp:41
	breakpoint already hit 1 time
2       breakpoint     keep y   0x0000000000400535 in Demo1() at gdbDebug.cpp:16
	breakpoint already hit 1 time
3       breakpoint     keep y   0x0000000000400535 in Demo1() at gdbDebug.cpp:16
(gdb) delete 3
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005b6 in main() at gdbDebug.cpp:41
	breakpoint already hit 1 time
2       breakpoint     keep y   0x0000000000400535 in Demo1() at gdbDebug.cpp:16
	breakpoint already hit 1 time
(gdb) 

2.4.2、根据行号删除断点

clear 断点行号n:如“clear 16”则代表删除第16行所在的断点

(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005b6 in main() at gdbDebug.cpp:41
	breakpoint already hit 1 time
2       breakpoint     keep y   0x0000000000400535 in Demo1() at gdbDebug.cpp:16
	breakpoint already hit 1 time
(gdb) clear 16
Deleted breakpoint 2 
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005b6 in main() at gdbDebug.cpp:41
	breakpoint already hit 1 time
(gdb) 

2.4.3、根据起始点编号删除起始点间所有断点(包括起始点)

delete 断点起点编号n-断点终点编号n:如“delete 4-8”则代表删除第4到第8个断点编号之间的所有编号

(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005b6 in main() at gdbDebug.cpp:41
	breakpoint already hit 1 time
4       breakpoint     keep y   0x00000000004005b6 in main() at gdbDebug.cpp:41
	breakpoint already hit 1 time
5       breakpoint     keep y   0x00000000004005bb in main() at gdbDebug.cpp:43
	breakpoint already hit 1 time
6       breakpoint     keep y   0x00000000004005c2 in main() at gdbDebug.cpp:44
	breakpoint already hit 1 time
7       breakpoint     keep n   0x00000000004005cb in main() at gdbDebug.cpp:46
	breakpoint already hit 4 times
8       breakpoint     keep y   0x00000000004005db in main() at gdbDebug.cpp:49
	breakpoint already hit 1 time
9       breakpoint     keep y   0x00000000004005ef in main() at gdbDebug.cpp:51
	breakpoint already hit 1 time
(gdb) delete 4-8
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005b6 in main() at gdbDebug.cpp:41
	breakpoint already hit 1 time
9       breakpoint     keep y   0x00000000004005ef in main() at gdbDebug.cpp:51
	breakpoint already hit 1 time
(gdb) 

3. 远程调试

3.1 被控端

启动gdbserver

[root@30-202-150 ~]# gdbserver localhost:4242 gdbDebug

3.2 控制端

启动服务

[root@localhost ~]# gdb

3.3 控制端启动连接

(gdb) target remote 172.30.202.150:4242

此时被控端

[root@30-202-150 test]# gdbserver localhost:4242 gdbDebug
Process /root/test/gdbDebug created; pid = 1068017
Listening on port 4242
Remote debugging from host ::ffff:172.30.202.158, port 49410

标签:keep,gdb,gdbDebug,breakpoint,cpp,断点,远程,调试
From: https://www.cnblogs.com/comecc/p/17005355.html

相关文章

  • vs2008调试 Release 工程
    VS2008Release工程调试修改方式:1、项目——》属性——》C/C++——》常规——》调试信息格式——》用于“编辑并继续”的程序数据库(/ZI)2、项目——》属性——》C/......
  • Redux DevTools:Redux调试工具redux-devtools-extension的使用介绍
    调试redux代码的工具,官方推荐的是redux-devtools-extension,安装好了之后,我们还需要在代码中配置一下才可以在浏览器中调试代码。**一,我们安装redux调试工具,是在Chrome中去安......
  • Android真机调试手动添加程序包的LogCat
    android真机调试有时候看LogCat 时,有时候那个跑的本程序的LogCat 没有出现而是 出现的是"All messages(nofilters)"。此时的Log显示是显示的所有的信息,有时候需......
  • vs2010下设置release版本调试设置
    设置在Release模式下调试的方法:1.工程项目上右键->属性2.c++->常规-〉调试信息格式   选 程序数据库(/Zi)或(/ZI),注意:如果是库的话,只能(Zi)3.c++->优化......
  • git 用远程强制覆盖本地分支的方法
     使用场景 有次合并了别的分支代码,但是想合并单个文件,结果试一下全合并了,那么不想要这次合并,需要用远程强制覆盖本地分支的方法。另外合并单个文件用cherry-pick方法......
  • IDEA之远程调试代码
    第一步:新建SpringBoot项目,创建测试的Controllerpackagecom.zhixi.controller;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframew......
  • gdb使用流程
    进入GDB——gdbtest查看文件——l设置断点——b6查看断点处情况——infob运行代码——r查看变量值——pn/pi单步运行——n恢复程序运行——c观察变......
  • gdbserver远程调试
    在嵌入式系统开发中,我们经常通过使用交叉调试工具实现远程调试。采用远程调试的主要原因是大多数嵌入式平台不太适合进行本地调试,在很多嵌入式平台上内存等资源受限制,并且......
  • 安装GDB-ImageWatch ,在QT中查看图像
    GDB_ImageWatch是在Linux下基于QT编写图像处理程序的调试程序。由于并非像ImageWatch一样由官方提供,而是在github上以代码的方式进行提供,我们在使用的时候需要自己编......
  • NUC980开源项目40-PLC远程下载/内网穿透(非技术)
     1.网穿介绍在淘宝上有一个产品,叫做PLC远程下载模块,随便找了一个。 它的核心功能,是让客户在外网可以控制内网的设备。关于内网穿透,有几种搞法第一种是远程控制,常见的就是......