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

lv3 嵌入式开发-11 Linux下GDB调试工具

目录

1 GDB简介

2 GDB基本命令

3 GDB调试程序


1 GDB简介

GDB是GNU开源组织发布的一个强大的Linux下的程序调试工具。 一般来说,GDB主要帮助你完成下面四个方面的功能:

  • 1、启动你的程序,可以按照你的自定义的要求随心所欲的运行程序(按着自己的想法运行)。
  • 2、可让被调试的程序在你所指定的调置的断点处停住。(断点可以是条件表达式)
  • 3、当程序被停住时,可以检查此时你的程序中所发生的事。
  • 4、你可以改变你的程序,将一个BUG产生的影响修正从而测试其他BUG。

2 GDB基本命令

Here are some of the most frequently needed GDB commands:break [file:]functionSet a breakpoint at function (in file).断点run [arglist]Start your program (with arglist, if specified).bt  Backtrace: display the program stack.显示程序堆栈print exprDisplay the value of an expression.打印c   Continue running your program (after stopping, e.g. at abreakpoint).继续nextExecute next program line (after stopping); step over any functioncalls 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 ispresently stopped.step Execute next program line (after stopping); step into any functioncalls in the line.  执行下一行help [name]Show information about GDB command name, or general informationabout using GDB.quitExit from GDB.You can, instead, specify a process ID as a second argument or use option "-p", if you want to debug a running process:gdb program 1234gdb -p 1234

示例 

linux@linux:~/Desktop$ ls
a.out  gdb.c
linux@linux:~/Desktop$ gcc -g gdb.c
linux@linux:~/Desktop$ ./a.out 
0
1
2
3
4
hello world
linux@linux:~/Desktop$ gdb a.out
GNU gdb (Ubuntu 7.7-0ubuntu3.1) 7.7
Copyright (C) 2014 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 "i686-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 a.out...done.
(gdb) l
2	
3	void print()
4	{
5		printf("hello world\n");
6	}
7	int main(int argc, const char *argv[])
8	{
9		int i;
10	
11		for (i = 0; i < 5; i++)
(gdb) b main
Breakpoint 1 at 0x804846a: file gdb.c, line 11.
(gdb) r
Starting program: /home/linux/Desktop/a.out Breakpoint 1, main (argc=1, argv=0xbffff164) at gdb.c:11
11		for (i = 0; i < 5; i++)
(gdb) c
Continuing.
0
1
2
3
4
hello world
[Inferior 1 (process 5010) exited normally]
(gdb) b 10
Note: breakpoint 1 also set at pc 0x804846a.
Breakpoint 2 at 0x804846a: file gdb.c, line 10.
(gdb) r
Starting program: /home/linux/Desktop/a.out Breakpoint 1, main (argc=1, argv=0xbffff164) at gdb.c:11
11		for (i = 0; i < 5; i++)
(gdb) c
Continuing.
0
1
2
3
4
hello world
[Inferior 1 (process 5113) exited normally]
(gdb) r
Starting program: /home/linux/Desktop/a.out Breakpoint 1, main (argc=1, argv=0xbffff164) at gdb.c:11
11		for (i = 0; i < 5; i++)
(gdb) n
12			printf("%d\n",i);
(gdb) n
0
11		for (i = 0; i < 5; i++)
(gdb) n
12			printf("%d\n",i);
(gdb) n
1
11		for (i = 0; i < 5; i++)
(gdb) p &i
$1 = (int *) 0xbffff0bc
(gdb) p i
$2 = 1
(gdb) n
12			printf("%d\n",i);
(gdb) p i
$3 = 2
(gdb) n
2
11		for (i = 0; i < 5; i++)
(gdb) n
12			printf("%d\n",i);
(gdb) n
3
11		for (i = 0; i < 5; i++)
(gdb) n
12			printf("%d\n",i);
(gdb) p i
$4 = 4
(gdb) n
4
11		for (i = 0; i < 5; i++)
(gdb) n
14		print();
(gdb) s
print () at gdb.c:5
5		printf("hello world\n");
(gdb) n
hello world
6	}
(gdb) n
main (argc=1, argv=0xbffff164) at gdb.c:15
15		return 0;
(gdb) 

3 GDB调试程序

示例:定位错误

代码

#include <stdio.h>#ifndef _CORE_void print()
{printf("hello world\n");
}
int main(int argc, const char *argv[])
{int i;for (i = 0; i < 5; i++)printf("%d\n",i);print();return 0;
}#else
int main(int argc,const char *argv[])
{int *temp = NULL;*temp = 10;   //没有分配内存空间,直接会出错return 0;
}#endif

定位错误位置 

linux@linux:~/Desktop$ ls
a.out  gdb.c
linux@linux:~/Desktop$ gcc -g gdb.c -D _CORE_
linux@linux:~/Desktop$ ./a.out 
Segmentation fault (core dumped)
linux@linux:~/Desktop$ ls
a.out  core  gdb.c
linux@linux:~/Desktop$ gdb a.out core
GNU gdb (Ubuntu 7.7-0ubuntu3.1) 7.7
Copyright (C) 2014 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 "i686-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 a.out...done.
[New LWP 5904]
Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x080483fd in main (argc=1, argv=0xbfea3544) at gdb.c:24
24		*temp = 10;   //没有分配内存空间,直接会出错
(gdb) 

如何调试正在运行的进程?

源码

linux@linux:~/Desktop$ cat gdb.c 
#include <stdio.h>
#include <unistd.h>int main(int argc, const char *argv[])
{while(1){int i;i++;printf("%d\n",i);sleep(1);}return 0;
}linux@linux:~/Desktop$ gcc -g gdb.c 
linux@linux:~/Desktop$ ./a.out 
-1217503231
-1217503230
-1217503229
-1217503228...

再开一个终端

linux@linux:~$ ps aux | grep a.out
linux     6291  0.0  0.0   2028   280 pts/0    S+   11:47   0:00 ./a.out
linux     6293  0.0  0.0   4680   832 pts/3    S+   11:47   0:00 grep --color=auto a.out
linux@linux:~$ cd /home/linux/
.bakvim/              .gconf/               .sogouinput/
.cache/               .local/               Templates/
.config/              .mozilla/             tftpboot/
.dbus/                Music/                Videos/
Desktop/              Pictures/             .vim/
Documents/            .pki/                 vmware-tools-distrib/
Downloads/            Public/               
linux@linux:~$ cd /home/linux/Desktop/
linux@linux:~/Desktop$ ls
a.out  core  gdb.c
linux@linux:~/Desktop$ gdb a.out -p 4849
GNU gdb (Ubuntu 7.7-0ubuntu3.1) 7.7
Copyright (C) 2014 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 "i686-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 a.out...done.
Attaching to program: /home/linux/Desktop/a.out, process 4849warning: unable to open /proc file '/proc/4849/status'warning: unable to open /proc file '/proc/4849/status'
ptrace: No such process.
(gdb) b main
Breakpoint 1 at 0x8048456: file gdb.c, line 9.
(gdb) n
The program is not being run.
(gdb) r
Starting program: /home/linux/Desktop/a.out Breakpoint 1, main (argc=1, argv=0xbffff0f4) at gdb.c:9
9			i++;
(gdb) n
10			printf("%d\n",i);
(gdb) n
-1208209407
11			sleep(1);
(gdb) n
12		}
(gdb) q
A debugging session is active.Inferior 1 [process 6317] will be killed.Quit anyway? (y or n) y
linux@linux:~/Desktop$ 

相关文章:

lv3 嵌入式开发-11 Linux下GDB调试工具

目录 1 GDB简介 2 GDB基本命令 3 GDB调试程序 1 GDB简介 GDB是GNU开源组织发布的一个强大的Linux下的程序调试工具。 一般来说&#xff0c;GDB主要帮助你完成下面四个方面的功能&#xff1a; 1、启动你的程序&#xff0c;可以按照你的自定义的要求随心所欲的运行程序&#…...

Zabbix监控平台概念

1.概念 Zabbix是一款开源的、免费的、分布式监控平台支持web管理&#xff0c;WEB界面可以方便管理员使用可以监控硬件服务器CPU温度、风扇转速、操作系统CPU、EME、DISK、I/O、流量宽带、负载、端口、进程等Zabbix是C/S架构&#xff0c;Client客户端和Server端组成 2.Zabbix可…...

【javaSE】 枚举与枚举的使用

文章目录 &#x1f384;枚举的背景及定义⚾枚举特性总结&#xff1a; &#x1f332;枚举的使用&#x1f6a9;switch语句&#x1f6a9;常用方法&#x1f4cc;示例一&#x1f4cc;示例二 &#x1f38d;枚举优点缺点&#x1f334;枚举和反射&#x1f6a9;枚举是否可以通过反射&…...

NetSuite知识会汇编-管理员篇顾问篇2023

本月初&#xff0c;开学之际&#xff0c;我们发布了《NetSuite知识会汇编-用户篇 2023》&#xff0c;这次发布《NetSuite知识会汇编-管理员篇&顾问篇2023》。本篇挑选了近两年NetSuite知识会中的一些文章&#xff0c;涉及开发、权限、系统管理等较深的内容&#xff0c;共19…...

根号分治与多项式的巧妙结合:GYM-104386G

使用范围&#xff1a;序列上对于每种数的计数问题 考虑对每种数的出现次数进行根号分治 如果出现次数很少&#xff0c;直接平方暴力即可 如果很大考虑任意 ( i , j ) (i,j) (i,j)&#xff0c;我们拆一下&#xff0c;再移一下&#xff0c;然后就变成了卷积形式...

通过FTP高速下载几百G数据

基因组下载 (FTP) 常见问题解答 基因组FTP站点有哪些亮点?下载多个基因组组装数据的最简单方法是什么?下载大型数据集的最佳协议是什么?为什么 NCBI 基因组 FTP 站点要重组?我如何及时了解 NCBI 基因组 FTP 站点的变化?...

cudnn-windows-x86_64-8.6.0.163_cuda11-archive 下载

网址不太好访问的话,请从下面我提供的分享下载 Download cuDNN v8.6.0 (October 3rd, 2022), for CUDA 11.x 此资源适配 cuda11.x 将bin和include文件夹里的文件&#xff0c;分别复制到C盘安装CUDA目录的对应文件夹里 安装cuda时自动设置了 CUDA_PATH_V11_8 及path C:\Progra…...

多线程案例(1) - 单例模式

目录 单例模式 饿汉模式 懒汉模式 前言 多线程中有许多非常经典的设计模式&#xff08;这就类似于围棋的棋谱&#xff09;&#xff0c;这是用来解决我们在开发中遇到很多 "经典场景"&#xff0c;简单来说&#xff0c;设计模式就是一份模板&#xff0c;可以套用。…...

Arduino驱动TCS34725传感器(颜色传感器篇)

目录 1、传感器特性 2、硬件原理图 3、控制器和传感器连线图 4、驱动程序 TCS34725是一款低成本,高性价比的RGB全彩颜色识别传感器,传感器通过光学感应来识别物体的表面颜色。...

知识库网站如何搭建?需要注意这五个要点!

正因为知识库提供结构化知识库来记载信息和知识&#xff0c;便于团队沉淀经验、共享资源&#xff0c;形成完整的知识体系并持续进化​&#xff0c;使得它成为当前企业发展新宠。 构建自己/团队的知识库是一个良好的习惯&#xff0c;可以提高工作和学习效率&#xff0c;以下是一…...

【UE虚幻引擎】UE源码版编译、Andorid配置、打包

首先是要下载源码版的UE&#xff0c;我这里下载的是5.2.1 首先要安装Git 在你准备放代码的文件夹下右键点击Git Bash Here 然后可以直接git clone https://github.com/EpicGames/UnrealEngine 不行的话可以直接去官方的Github上下载Zip压缩包后解压 运行里面的Setup.bat&a…...

树和二叉树的相关概念及结构

目录 1.树的概念及结构 1.1 树的概念 1.2 树的相关概念 1.3 树的表示 1.3.1 孩子兄弟表示法 1.3.2 双亲表示法 1.4 树的实际应用 2.二叉树的概念及结构 2.1 二叉树的概念 2.2 特殊的二叉树 2.3 二叉树的性质 2.4 二叉树的存储 2.4.1 顺序存储 2.4.2 链式存储 1.树…...

MySQL安装validate_password_policy插件

功能介绍 validate_password_policy 是插件用于验证密码强度的策略。该参数可以设定三种级别&#xff1a;0代表低&#xff0c;1代表中&#xff0c;2代表高。 validate_password_policy 主要影响密码的强度检查级别&#xff1a; 0/LOW&#xff1a;只检查密码长度。 1/MEDIUM&am…...

数据在内存中的存储——练习3

题目&#xff1a; 3.1 #include <stdio.h> int main() {char a -128;printf("%u\n",a);return 0; }3.2 #include <stdio.h> int main() {char a 128;printf("%u\n",a);return 0; }思路分析&#xff1a; 首先二者极其相似%u是无符号格式进行…...

web-案例

分页插件 登录 事务...

第一章 JAVA入门

文章目录 1.2 Java 的特点1.2.1 简单1.2.2 面向对象1.2.3 与平台无关① 平台与机器指令② C/C程序依赖平台③ Java 虚拟机与字节码1.2.4 多线程1.2.5 动态1.30安装 JDK1.3.1 平台简介0 Java SE②Java EE1.4 Java 程序的开发步骤②保存源文件1.5.2 编译1.8 Java之父-James Gosli…...

二叉树详解(求二叉树的结点个数、深度、第k层的个数、遍历等)

二叉树&#xff0c;是一种特殊的树&#xff0c;特点是树的度小于等于2&#xff08;树的度是整个树的结点的度的最大值&#xff09;&#xff0c;由于该特性&#xff0c;构建二叉树的结点只有三个成员&#xff0c;结点的值和指向结点左、右子树的指针。 typedef int DateType; t…...

Apollo配置中心及Python连接

本文将会介绍如何启动Apollo&#xff0c;在Apollo中配置参数&#xff0c;以及如何使用Python连接Apollo. Apollo介绍 在文章Python之读取配置文件和文章Python之配置文件处理中&#xff0c;笔者分别介绍了如何使用Python来处理ini, yaml, conf等配置文件。这种配置方式比较方便…...

LuatOS-SOC接口文档(air780E)--audio - 多媒体音频

常量 常量 类型 解释 audio.PCM number PCM格式&#xff0c;即原始ADC数据 audio.MORE_DATA number audio.on回调函数传入参数的值&#xff0c;表示底层播放完一段数据&#xff0c;可以传入更多数据 audio.DONE number audio.on回调函数传入参数的值&#xff0c;表示…...

Golang gorm manytomany 多对多 更新、删除、替换

Delete 移除 只删除中间表的数据 删除原有的 var a Article1db.Preload("Tag1s").Take(&a, 1)fmt.Printf("%v", a) {1 k8s [{1 cloud []} {2 linux []}]}mysql> select * from article1; ------------ | id | title | ------------ | 1 | k8s …...

iOS 26 携众系统重磅更新,但“苹果智能”仍与国行无缘

美国西海岸的夏天&#xff0c;再次被苹果点燃。一年一度的全球开发者大会 WWDC25 如期而至&#xff0c;这不仅是开发者的盛宴&#xff0c;更是全球数亿苹果用户翘首以盼的科技春晚。今年&#xff0c;苹果依旧为我们带来了全家桶式的系统更新&#xff0c;包括 iOS 26、iPadOS 26…...

python/java环境配置

环境变量放一起 python&#xff1a; 1.首先下载Python Python下载地址&#xff1a;Download Python | Python.org downloads ---windows -- 64 2.安装Python 下面两个&#xff0c;然后自定义&#xff0c;全选 可以把前4个选上 3.环境配置 1&#xff09;搜高级系统设置 2…...

跨链模式:多链互操作架构与性能扩展方案

跨链模式&#xff1a;多链互操作架构与性能扩展方案 ——构建下一代区块链互联网的技术基石 一、跨链架构的核心范式演进 1. 分层协议栈&#xff1a;模块化解耦设计 现代跨链系统采用分层协议栈实现灵活扩展&#xff08;H2Cross架构&#xff09;&#xff1a; 适配层&#xf…...

css3笔记 (1) 自用

outline: none 用于移除元素获得焦点时默认的轮廓线 broder:0 用于移除边框 font-size&#xff1a;0 用于设置字体不显示 list-style: none 消除<li> 标签默认样式 margin: xx auto 版心居中 width:100% 通栏 vertical-align 作用于行内元素 / 表格单元格&#xff…...

【学习笔记】深入理解Java虚拟机学习笔记——第4章 虚拟机性能监控,故障处理工具

第2章 虚拟机性能监控&#xff0c;故障处理工具 4.1 概述 略 4.2 基础故障处理工具 4.2.1 jps:虚拟机进程状况工具 命令&#xff1a;jps [options] [hostid] 功能&#xff1a;本地虚拟机进程显示进程ID&#xff08;与ps相同&#xff09;&#xff0c;可同时显示主类&#x…...

SAP学习笔记 - 开发26 - 前端Fiori开发 OData V2 和 V4 的差异 (Deepseek整理)

上一章用到了V2 的概念&#xff0c;其实 Fiori当中还有 V4&#xff0c;咱们这一章来总结一下 V2 和 V4。 SAP学习笔记 - 开发25 - 前端Fiori开发 Remote OData Service(使用远端Odata服务)&#xff0c;代理中间件&#xff08;ui5-middleware-simpleproxy&#xff09;-CSDN博客…...

10-Oracle 23 ai Vector Search 概述和参数

一、Oracle AI Vector Search 概述 企业和个人都在尝试各种AI&#xff0c;使用客户端或是内部自己搭建集成大模型的终端&#xff0c;加速与大型语言模型&#xff08;LLM&#xff09;的结合&#xff0c;同时使用检索增强生成&#xff08;Retrieval Augmented Generation &#…...

Linux nano命令的基本使用

参考资料 GNU nanoを使いこなすnano基础 目录 一. 简介二. 文件打开2.1 普通方式打开文件2.2 只读方式打开文件 三. 文件查看3.1 打开文件时&#xff0c;显示行号3.2 翻页查看 四. 文件编辑4.1 Ctrl K 复制 和 Ctrl U 粘贴4.2 Alt/Esc U 撤回 五. 文件保存与退出5.1 Ctrl …...

OD 算法题 B卷【正整数到Excel编号之间的转换】

文章目录 正整数到Excel编号之间的转换 正整数到Excel编号之间的转换 excel的列编号是这样的&#xff1a;a b c … z aa ab ac… az ba bb bc…yz za zb zc …zz aaa aab aac…; 分别代表以下的编号1 2 3 … 26 27 28 29… 52 53 54 55… 676 677 678 679 … 702 703 704 705;…...

深度剖析 DeepSeek 开源模型部署与应用:策略、权衡与未来走向

在人工智能技术呈指数级发展的当下&#xff0c;大模型已然成为推动各行业变革的核心驱动力。DeepSeek 开源模型以其卓越的性能和灵活的开源特性&#xff0c;吸引了众多企业与开发者的目光。如何高效且合理地部署与运用 DeepSeek 模型&#xff0c;成为释放其巨大潜力的关键所在&…...