GDB调试

news2024/11/18 9:49:12

文章目录

    • 1.什么是GDB
    • 2. 准备工作
    • 3.GDB命令-启动、退出、查看代码
    • 4.设置断点
    • 5.GDB命令-调试命令


1.什么是GDB

在这里插入图片描述

2. 准备工作

  • 通常,在为调试而编译时,我们会关掉编译器的优化选项"-o",并打开调试选选项“-g”,另外,“-wall” 在尽量不影响程序行为的情况下选项打开所有warning,也可以发现许多问题,避免一些不必要的bug
  • gcc -g -wall program.cpp -o paogram
  • “-g” 选项的作用是在可执行文件中加入源码的信息,比如可执行文件中第几条机器指令对影院吗中的第几行,但并不是把整个源文件嵌入到可执行文件中,所以在调试时必须保证gdb可以找到源文件。

3.GDB命令-启动、退出、查看代码

  • 启动和退出
gdb 可执行程序
quit
  • 给程序设置参数/获取设置参数
set args 10 20
show args
  • GDB使用帮助
help
  • 查看当前文件代码
list/l         #从默认位置显示
list/l 行号     #从指定的行号显示
lsit/l 函数名   #从指定的函数显示
  • 查看非当前文件代码
lsit/l 文件名:行号
list/l 文件名:函数名
  • 设置显示的行数
show list/listsize
set list/listsize 行数

示例

#include <stdio.h>
#include <stdlib.h>

int test(int a);

int main(int argc, char* argv[])
{
	int a, b;
	printf("argc = %d\n", argc);

	if (argc < 3)
	{
		a = 10;
		b = 30;
	}
	else
	{
		a = atoi(argv[1]);
		b = atoi(argv[2]);
	}
	printf("a = %d,b = %d\n", a, b);
	printf("a + b = %d\n", a + b);

	for (int i = 0; i < a; i++)
	{
		printf("i = %d\n",i);
		//函数调用
		int res = test(i);
		printf("res value:%d\n", res);

	}

	printf("THE END!!!\n");
	return 0;
}

int test(int a)
{
	int num = 0;
	for (int i = 0; i < a; i++)
	{
		num += i;
	}
	return num;
}

在这里插入图片描述

[mytest@redhat7g gdbtest]$ ll
total 4
-rw-rw-r-- 1 mytest mytest 601 Dec  7 18:33 1207-gdb_test_mian.cpp
[mytest@redhat7g gdbtest]$ gcc 1207-gdb_test_mian.cpp -o test -g
[mytest@redhat7g gdbtest]$ ll
total 16
-rw-rw-r-- 1 mytest mytest  601 Dec  7 18:33 1207-gdb_test_mian.cpp
-rwxrwxr-x 1 mytest mytest 9976 Dec  7 18:34 test
[mytest@redhat7g gdbtest]$ gcc 1207-gdb_test_mian.cpp -o test1
[mytest@redhat7g gdbtest]$ ll -h test test1
-rwxrwxr-x 1 mytest mytest 9.8K Dec  7 18:34 test
-rwxrwxr-x 1 mytest mytest 8.4K Dec  7 18:34 test1
[mytest@redhat7g gdbtest]$ rm test1 
[mytest@redhat7g gdbtest]$ ll
total 16
-rw-rw-r-- 1 mytest mytest  601 Dec  7 18:33 1207-gdb_test_mian.cpp
-rwxrwxr-x 1 mytest mytest 9976 Dec  7 18:34 test
[mytest@redhat7g gdbtest]$ gdb test 
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 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".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/mytest/gdbtest/test...done.
(gdb) set args 34 34
(gdb) show args 
Argument list to give program being debugged when it is started is "34 34".
(gdb) help
List of classes of commands:

aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands

Type "help" followed by a class name for a list of commands in that class.
Type "help all" for the list of all commands.
Type "help" followed by command name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.
(gdb) l
1	#include <stdio.h>
2	#include <stdlib.h>
3	
4	int test(int a);
5	
6	int main(int argc, char* argv[])
7	{
8		int a, b;
9		printf("argc = %d\n", argc);
10	
(gdb) l 24
19			b = atoi(argv[2]);
20		}
21		printf("a = %d,b = %d\n", a, b);
22		printf("a + b = %d\n", a + b);
23	
24		for (int i = 0; i < a; i++)
25		{
26			printf("i = %d\n",i);
27			//º¯˽µ
28			int res = test(i);
(gdb) show list test
Number of source lines gdb will list by default is 10.
(gdb) help set
Evaluate expression EXP and assign result to variable VAR, using assignment
syntax appropriate for the current language (VAR = EXP or VAR := EXP for
example).  VAR may be a debugger "convenience" variable (names starting
with $), a register (a few standard names starting with $), or an actual
variable in the program being debugged.  EXP is any valid expression.
Use "set variable" for variables with names identical to set subcommands.

With a subcommand, this command modifies parts of the gdb environment.
You can see these environment settings with the "show" command.

List of set subcommands:

set ada -- Prefix command for changing Ada-specfic settings
set agent -- Set debugger's willingness to use agent as a helper
set annotate -- Set annotation_level
set architecture -- Set architecture of target
set args -- Set argument list to give program being debugged when it is started
set auto-load -- Auto-loading specific settings
set auto-load-scripts -- Set the debugger's behaviour regarding auto-loaded Python scripts
set auto-solib-add -- Set autoloading of shared library symbols
set backtrace -- Set backtrace specific variables
set basenames-may-differ -- Set whether a source file may have multiple base names
set breakpoint -- Breakpoint specific settings
set build-id-core-loads -- Set whether CORE-FILE loads the build-id associated files automatically
set build-id-verbose -- Set debugging level of the build-id locator
set can-use-hw-watchpoints -- Set debugger's willingness to use watchpoint hardware
set case-sensitive -- Set case sensitivity in name search
set charset -- Set the host and target character sets

4.设置断点

  • 设置断点
b/break 行号
b/break 函数名
b/break 文件名:行号
b/break 文件名:函数
  • 查看断点
i/info b/break
  • 删除断点
d/del/delete b/break
  • 设置断点无效
dis.disbale 断点编号
  • 设置断点生效
ena 、enable 断点编号
  • 设置条件断点
b/break 10 if i == 6
(gdb) b 18
Breakpoint 1 at 0x4005d6: file 1207-gdb_test_mian.cpp, line 18.
(gdb) b 42
Breakpoint 2 at 0x4006a4: file 1207-gdb_test_mian.cpp, line 42.
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005d6 in main(int, char**) at 1207-gdb_test_mian.cpp:18
2       breakpoint     keep y   0x00000000004006a4 in test(int) at 1207-gdb_test_mian.cpp:42
(gdb) b 39
Breakpoint 3 at 0x400694: file 1207-gdb_test_mian.cpp, line 39.
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005d6 in main(int, char**) at 1207-gdb_test_mian.cpp:18
2       breakpoint     keep y   0x00000000004006a4 in test(int) at 1207-gdb_test_mian.cpp:42
3       breakpoint     keep y   0x0000000000400694 in test(int) at 1207-gdb_test_mian.cpp:39
(gdb) b test
Note: breakpoint 3 also set at pc 0x400694.
Breakpoint 4 at 0x400694: file 1207-gdb_test_mian.cpp, line 39.
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005d6 in main(int, char**) at 1207-gdb_test_mian.cpp:18
2       breakpoint     keep y   0x00000000004006a4 in test(int) at 1207-gdb_test_mian.cpp:42
3       breakpoint     keep y   0x0000000000400694 in test(int) at 1207-gdb_test_mian.cpp:39
4       breakpoint     keep y   0x0000000000400694 in test(int) at 1207-gdb_test_mian.cpp:39
(gdb) del 3
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005d6 in main(int, char**) at 1207-gdb_test_mian.cpp:18
2       breakpoint     keep y   0x00000000004006a4 in test(int) at 1207-gdb_test_mian.cpp:42
4       breakpoint     keep y   0x0000000000400694 in test(int) at 1207-gdb_test_mian.cpp:39
(gdb) dis 2
(gdb) info b 
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005d6 in main(int, char**) at 1207-gdb_test_mian.cpp:18
2       breakpoint     keep n   0x00000000004006a4 in test(int) at 1207-gdb_test_mian.cpp:42
4       breakpoint     keep y   0x0000000000400694 in test(int) at 1207-gdb_test_mian.cpp:39
(gdb) enable 2
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005d6 in main(int, char**) at 1207-gdb_test_mian.cpp:18
2       breakpoint     keep y   0x00000000004006a4 in test(int) at 1207-gdb_test_mian.cpp:42
4       breakpoint     keep y   0x0000000000400694 in test(int) at 1207-gdb_test_mian.cpp:39
(gdb) 

5.GDB命令-调试命令

  • 运行GDB程序
start 		#程序停在第一行
run			#程序遇到断点才停
  • 继续运行,到下一个断点
c/continue
  • 向下执行一行代码(不会进入函数体)
n/next
  • 变量操作
p/print 变量名		#打印变量值
ptype 变量名			#打印变量类型
  • 向下单步调试(遇到函数进入函数体)
s/step
finish		#跳出函数体
  • 自动变量操作
display num		#自动打印指定变量的值
i/info display
undisplay 编号
  • 其他操作
set vat 变量名 = 变量值
until 		#跳出循环
[mytest@redhat7g gdbtest]$ gdb test 
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 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".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/mytest/gdbtest/test...done.
(gdb) lsit
Undefined command: "lsit".  Try "help".
(gdb) list
1	#include <stdio.h>
2	#include <stdlib.h>
3	
4	int test(int a);
5	
6	int main(int argc, char* argv[])
7	{
8		int a, b;
9		printf("argc = %d\n", argc);
10	
(gdb) list main
2	#include <stdlib.h>
3	
4	int test(int a);
5	
6	int main(int argc, char* argv[])
7	{
8		int a, b;
9		printf("argc = %d\n", argc);
10	
11		if (argc < 3)
(gdb) b 9
Breakpoint 1 at 0x4005ac: file 1207-gdb_test_mian.cpp, line 9.
(gdb) list test
33		printf("THE END!!!\n");
34		return 0;
35	}
36	
37	int test(int a)
38	{
39		int num = 0;
40		for (int i = 0; i < a; i++)
41		{
42			num += i;
(gdb) b 42
Breakpoint 2 at 0x4006a4: file 1207-gdb_test_mian.cpp, line 42.
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004005ac in main(int, char**) at 1207-gdb_test_mian.cpp:9
2       breakpoint     keep y   0x00000000004006a4 in test(int) at 1207-gdb_test_mian.cpp:42
(gdb) start
Temporary breakpoint 3 at 0x4005ac: file 1207-gdb_test_mian.cpp, line 9.
Starting program: /home/mytest/gdbtest/test 

Breakpoint 1, main (argc=1, argv=0x7fffffffe4b8) at 1207-gdb_test_mian.cpp:9
9		printf("argc = %d\n", argc);
Missing separate debuginfos, use: debuginfo-install glibc-2.17-325.el7_9.x86_64
(gdb) n
argc = 1
11		if (argc < 3)
(gdb) n
13			a = 10;
(gdb) n
14			b = 30;
(gdb) n
21		printf("a = %d,b = %d\n", a, b);
(gdb) n
a = 10,b = 30
22		printf("a + b = %d\n", a + b);
(gdb) pa
Ambiguous command "pa": pahole, passcount, path.
(gdb) p a
$1 = 10
(gdb) p b
$2 = 30
(gdb) p argc
$3 = 1
(gdb) s
a + b = 40
24		for (int i = 0; i < a; i++)
(gdb) s
26			printf("i = %d\n",i);
(gdb) s
i = 0
28			int res = test(i);
(gdb) s
test (a=0) at 1207-gdb_test_mian.cpp:39
39		int num = 0;
(gdb) s
40		for (int i = 0; i < a; i++)
(gdb) s
44		return num;
(gdb) s
45	}(gdb) finish 
Run till exit from #0  test (a=0) at 1207-gdb_test_mian.cpp:45
0x0000000000400659 in main (argc=1, argv=0x7fffffffe4b8) at 1207-gdb_test_mian.cpp:28
28			int res = test(i);
Value returned is $4 = 0
(gdb) s
29			printf("res value:%d\n", res);
(gdb) n
res value:0
24		for (int i = 0; i < a; i++)
(gdb) n
26			printf("i = %d\n",i);
(gdb) set i = 990
Ambiguous set command "i = 990": inferior-tty, input-radix, interactive-mode.
(gdb) n
i = 1
28			int res = test(i);
(gdb) n

Breakpoint 2, test (a=1) at 1207-gdb_test_mian.cpp:42
42			num += i;
(gdb) n
40		for (int i = 0; i < a; i++)
(gdb) n
44		return num;
(gdb) n
45	}(gdb) n
main (argc=1, argv=0x7fffffffe4b8) at 1207-gdb_test_mian.cpp:29
29			printf("res value:%d\n", res);
(gdb) n
res value:0
24		for (int i = 0; i < a; i++)
(gdb) n
26			printf("i = %d\n",i);
(gdb) n
i = 2
28			int res = test(i);
(gdb) n

Breakpoint 2, test (a=2) at 1207-gdb_test_mian.cpp:42
42			num += i;
(gdb) n
40		for (int i = 0; i < a; i++)
(gdb) p i 
$5 = 0
(gdb) c
Continuing.

Breakpoint 2, test (a=2) at 1207-gdb_test_mian.cpp:42
42			num += i;
(gdb) c
Continuing.
res value:1
i = 3
(gdb) display a
1: a = 8
(gdb) display b
No symbol "b" in current context.
(gdb) n
Breakpoint 2, test (a=8) at 1207-gdb_test_mian.cpp:42
42			num += i;
1: a = 8
(gdb) n
40		for (int i = 0; i < a; i++)
1: a = 8

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/70232.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【第一章 Linux目录结构,网络连接模式,vi和vim,Linux关机重启命令,Linux用户管理】

第一章 Linux目录结构&#xff0c;网络连接模式&#xff0c;vi和vim&#xff0c;Linux关机&重启命令&#xff0c;Linux用户管理 1.Linux和Unix&#xff1a; ①Unix针对于大型&#xff0c;高性能主机或服务器&#xff1b; ②Linux适用于个人计算机。 2.网络连接的三种模式…

图解pytorch里面的torch.gather()

在 Dim1 的情况下应用 torch.gather() 上图显示了 torch gather() 函数在 dim1 的二维张量上的工作。 这里索引张量的行对应于输入张量的行&#xff08;用灰色阴影突出显示&#xff09;。现在对于索引张量中的每个索引值&#xff0c;从该行和输入张量的索引中选取相应的值。 让…

LEADTOOLS 22-23 .Net/NetCore/JS/JAVA/Win/Linux

破解版功能齐全&#xff1a;LEADTOOLS 是一系列综合工具包&#xff0c;旨在帮助程序员将光栅、文档、医学、多媒体和矢量图像集成到他们的桌面、服务器、平板电脑和移动应用程序中。LEADTOOLS 为开发人员提供最灵活、最强大的成像技术&#xff0c;为 OCR、条形码、表单识别、PD…

推荐大家一些CTF的网站和工具

一.网站 1.攻防世界 网址&#xff1a;攻防世界 这是一个有好多题目的网站 主要有Misc、Pwn、Web、Reverse、Crypto、Mobile几种题型 不会的问题还可以查题解 好用度 9星 2.BUUCTF 网址&#xff1a;BUUCTF在线评测 也有很多ctf的题目 逆向、网络等等...... 比攻防世界…

最近火爆了的对话ChatGPT

前言 相信最近小伙伴们已经被ChatGPT的惊艳效果刷屏了&#xff0c;之前笔者也介绍过一些对话方向的工作&#xff0c;感兴趣的小伙伴可以穿梭&#xff1a; 对话系统最新综述II https://zhuanlan.zhihu.com/p/446760658 在对话系统中建模意图、情感: https://zhuanlan.zhihu.com/…

Nacos是什么?

摘要&#xff1a;Nacos是 Dynamic Naming and Configuration Service的首字母简称&#xff0c;相较之下&#xff0c;它更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。本文分享自华为云社区《Nacos入门指南 - Nacos是什么》&#xff0c;作者&#xff1a;华为云P…

.gitlab-ci.yml文件常用规则说明

我自己整理了一份yml文件&#xff0c;里面包含了分支触发&#xff0c;和tag触发&#xff0c;还有缓存等&#xff1a; stages:- install- build- deploycache:key: nodeModulespaths:- node_modules- distjob_install:stage: installtags:- cvtagsonly:refs:- devscript:- npm …

基于LLVM的Fortran编译器分析

简介 本文内容基于LLVM 13.0.0。 目前基于LLVM的Fortran编译器&#xff08;或者驱动&#xff09;有3种&#xff0c;分别是flang、f18和flang-new。 flang是pgfortran的开源版本&#xff0c;基于PGI/NVIDIA的商业Fortran 编译器&#xff0c;它并不从属于LLVM项目。NVIDIA团队…

LabVIEW编程LabVIEW开发 ADAM 4015热电阻输入模块例程与相关资料

LabVIEW编程LabVIEW开发 ADAM 4015热电阻输入模块例程与相关资料 ​研华公司的ADAM 4015是6通道热电阻输入模块&#xff0c;可以采集2线或3线热电阻输入信号&#xff0c;ADAM4015T课题采集热敏电阻的输入信号。模块在工业测量和监控的有着广泛的应用&#xff0c;它既可以支持A…

Web3中文|苹果想对以太坊征税

虽然Web3是非常新的技术&#xff0c;但是似乎已经遇到了非常多“劲敌”。 这些“敌人”正在阻碍web3应用程序和区块链游戏的发展&#xff0c;因为在web3里&#xff0c;应用程序和游戏将允许用户自主相互交易数字资产所有权。 所以&#xff0c;那些大公司&#xff0c;如任天堂…

最近全网爆火的黑科技,叫做chatGPT

AI神器ChatGPT 火了。 能直接生成代码、会自动修复bug、在线问诊、模仿莎士比亚风格写作……各种话题都能hold住&#xff0c;它就是OpenAI刚刚推出的——ChatGPT。 有脑洞大开的网友甚至用它来设计游戏&#xff1a;先用ChatGPT生成游戏设定&#xff0c;再用Midjourney出图&…

vue 数据手写分页,定时展示

我们在业务之中&#xff0c;其实会常常用到一些数据的分段展示 &#xff0c; 比如数据量过大导致echarts无法展示&#xff0c;我们就可以将数据进行算法分页 &#xff0c; 然后套用定时器实时更新分段数据&#xff1b; 例子展示 &#xff1a; 将下列数组截取成每页5条数据的分…

观察者模式(python)

一、模式定义 1.观察者模式(Observer Pattern)&#xff1a;定义对象间的一种一对多依赖关系&#xff0c;使得每当一个对象状态发生改变时&#xff0c;其相关依赖对象皆得到通知并被自动更新。 2.观察者模式又叫做发布-订阅&#xff08;Publish/Subscribe&#xff09;模式、模…

SpringBoot微服务的发布与部署(3种方式)

基于 SpringBoot 的微服务开发完成之后&#xff0c;现在到了把它们发布并部署到相应的环境去运行的时候了。 SpringBoot 框架只提供了一套基于可执行 jar 包&#xff08;executable jar&#xff09;格式的标准发布形式&#xff0c;但并没有对部署做过多的界定&#xff0c;而且…

2022年Python面试题汇总【面试官爱问】

2022年Python面试题汇总【常问】1、请你讲讲python获取输入的方式&#xff0c;以及python如何打开文件2、Python数据处理的常用函数3、请你说说python传参传引用4、请你说说python和java的区别5、Python你常用的包有哪些&#xff1f;6、简单说明如何选择正确的Python版本。7、简…

Qt动态库

QT带界面的动态库 创建动态库 一、新建一个C的动态库的项目 选择C的动态库的项目&#xff0c;进行下一步 修改项目的名字和项目的保存的路径。 选着编译的方式&#xff0c;不需要改&#xff0c;进行下一步。 选着动态库&#xff0c;编译成动态库&#xff0c;进行下一步。 项目…

[附源码]JAVA毕业设计社区生活超市管理系统(系统+LW)

[附源码]JAVA毕业设计社区生活超市管理系统&#xff08;系统LW&#xff09; 项目运行 环境项配置&#xff1a; Jdk1.8 Tomcat8.5 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目…

[附源码]Python计算机毕业设计SSM计算机学院课程设计管理系统(程序+LW)

[附源码]Python计算机毕业设计SSM计算机学院课程设计管理系统&#xff08;程序LW) 项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。…

基于安卓的课程管理系统app设计

目 录 第1章 绪论 1 1.1 什么是电子课程管理 1 1.2 系统开发的背景 1 1.3 本文主要工作 1 1.4 系统设计目标分析 2 第2章 基本技术方案 3 2.1 Android概述 3 2.2 Android系统的四大组件 3 2.3 Android中的相关技术介绍及分析 5 2.3.1 Android系统架构研究 5 2.3.2 Android架构分…

LeetCode 167. 两数之和 II - 输入有序数组

&#x1f308;&#x1f308;&#x1f604;&#x1f604; 欢迎来到茶色岛独家岛屿&#xff0c;本期将为大家揭晓LeetCode 167. 两数之和 II - 输入有序数组&#xff0c;做好准备了么&#xff0c;那么开始吧。 &#x1f332;&#x1f332;&#x1f434;&#x1f434; 一、题目名称…