目录
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