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

嵌入式开发-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$ 

相关文章:

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

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

Java测试(11) --- selenium

测试用例方法以teat_开头 运行脚本的时候默认自动会运行test_开头的方法普通方法不以test_开头 普通方法被test_开头的方法调用的时候才会运行测试套件&#xff0c;把不同文件里的不同类里面的不同的测试方法组织起来放在一起运行&#xff08;1&#xff09;addTest 把不同…...

vue3 defineExpose 显示的指定组件需要暴露的属性

简介&#xff1a; 是 vue3 新增的一个 api &#xff0c;用在 <script setup>中使用&#xff0c;用于显示的把组件的属性和方法暴露出来。可用于父子组件通信&#xff0c;子组件使用 defineExpose 将自身的方法或者属性暴露出去&#xff0c;父组件中通过 ref 获取子组件 D…...

算法通关村第十七关:黄金挑战-跳跃游戏问题

黄金挑战-跳跃游戏问题 1. 跳跃游戏 LeetCode 55 https://leetcode.cn/problems/jump-game/ 思路分析 关键是判断能否到达终点&#xff0c;不用管每一步跳跃到哪里&#xff0c;而是尽可能的跳跃到最远的位置 看最多能覆盖到哪里&#xff0c;只要不断更新能覆盖的距离&#x…...

Git GitHub GitLab

1、Git Git是一个开源的分布式版本控制系统&#xff0c;是一种工具软件&#xff0c;用于代码的存储和版本控制。 2、GitHub GitHub是一个基于Git实现的在线代码仓库&#xff0c;是目前全球最大的代码托管平台。 3、GitLab GitLab也是一个基于Git实现的在线代码仓库&#x…...

前端的规范

假如团队中的小伙伴在提交代码时没有遵循规范要求&#xff0c;只写了一个"fix"或"update&#xff0c;这会给其他小伙伴造成困扰&#xff0c;不得不花时间查看代码和推测逻辑。 不仅会浪费了时间和精力&#xff0c;可能会导致项目以下问题&#xff1a; 可读性差…...

嵌入式软件有限状态机的 C 语言实现

状态机模式是一种行为模式&#xff0c;通过多态实现不同状态的调转行为的确是一种很好的方法&#xff0c;只可惜在嵌入式环境下&#xff0c;有时只能写纯C代码&#xff0c;并且还需要考虑代码的重入和多任务请求跳转等情形&#xff0c;因此实现起来着实需要一番考虑。 近日在看…...

面试题常考:LRU缓存

题目&#xff1a; 请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。 实现 LRUCache 类&#xff1a; LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存 int get(int key) 如果关键字 key 存在于缓存中&#xff0c;则返回关键字的值&…...

Redis 教程 - 持久化

Redis 教程 - 持久化 在 Redis 中&#xff0c;持久化是指将数据从内存保存到磁盘上&#xff0c;以便在重启或服务器故障后仍能恢复数据。Redis 提供了两种持久化方式&#xff1a;RDB&#xff08;Redis Database&#xff09;和 AOF&#xff08;Append-Only File&#xff09;。本…...

2023 大学生数学建模竞赛-C题-第一问

题目&#xff1a; 在生鲜商超中&#xff0c;一般蔬菜类商品的保鲜期都比较短&#xff0c;且品相随销售时间的增加而变差&#xff0c; 大部分品种如当日未售出&#xff0c;隔日就无法再售。因此&#xff0c;商超通常会根据各商品的历史销售和需 求情况每天进行补货。 由于商超销…...

设计模式3 观察者模式

一 观察者模式 1.1 概述 观察者模式是一种行为模式&#xff0c;又称之为“发布/订阅”模式&#xff0c;在该模式中被观察的对象叫主题&#xff0c;依赖主题的对象被称为观察者&#xff0c;当主题发生改变时&#xff0c;会通知所有观察者进行更新。多个对象存在一对多的关系&a…...

如何防止网络安全攻击

为了防止网络安全攻击&#xff0c;以下是一些常见的防御措施和建议&#xff1a; 使用强密码&#xff1a;确保使用足够长、复杂且随机的密码&#xff0c;并定期更改密码。不要在多个账户中重复使用相同的密码。 更新和修补软件&#xff1a;定期更新操作系统、应用程序和安全补丁…...

怎么从0到1实现一个PHP框架?

写在前面 本人开发的框架在2021年年初开发完成&#xff0c;后面没有再做过任何维护和修改。是仅供大家参考交流的学习项目&#xff0c;请勿使用在生产环境&#xff0c;也勿用作商业用途。 框架地址&#xff1a; https://github.com/yijiebaiyi/fast_framework 整体思路 开发…...

脚本:python实现樱花树

文章目录 代码效果 代码 from turtle import * from random import * from math import * def tree(n, l):pd () # 下笔# 阴影效果t cos ( radians ( heading () 45 ) ) / 8 0.25pencolor ( t, t, t )pensize ( n / 3 )forward ( l ) # 画树枝if n > 0:b random () *…...

公司内部传文件怎么安全——「用绿盾透明加密软件」

为保证公司内部文件传递的安全性&#xff0c;可以使用天锐绿盾透明加密软件来进行保护。以下是具体的操作步骤&#xff1a; 在公司内部部署天锐绿盾加密软件&#xff0c;确保需要传递的文件都能受到加密保护。 在员工的工作电脑上安装天锐绿盾客户端&#xff0c;并设置好相关的…...

提高使用VS Code工作效率的技巧

提高使用VS Code工作效率的技巧 时间轴视图&#xff1a;本地源代码控制 时间轴视图为我们提供了内置的源代码控制。 我们中的许多人都知道 Git 和其他源代码控制工具有多么有用&#xff0c;它们可以帮助我们轻松跟踪文件更改并在需要时恢复到之前的状态。 因此&#xff0c;…...

软件系统兼容性测试都要注意哪些问题?

兼容性 软件兼容性测试具有相同的含义&#xff0c;它是任何第三方 Web 应用程序测试服务不可分割的一部分。在众多不同的设置中&#xff0c;最重要的是完全的客户满意度&#xff0c;并且可以通过全面的兼容性测试来达到最佳效果。众所周知&#xff0c;软件质量保证是克服 IT 挑…...

索尼 toio™应用创意开发征文|toio俄罗斯方块游戏

目录 引言 摘要 创意简述 准备工作&#xff5c;手工开始 代码编写&#xff5c;合理集成 使用体验&#xff5c;近乎奇妙 引言 索尼toio™编程机器人是一款引领技术创新的产品&#xff0c;为开发者提供了一个全新的编程和创造平台。toio™的设计旨在将技术、塑性和乐趣融为…...

C#事件event

事件模型的5个组成部分 事件拥有者&#xff08;event source&#xff09;&#xff08;类对象&#xff09;&#xff08;有些书将其称为事件发布者&#xff09; 事件成员&#xff08;event&#xff09;&#xff08;事件拥有者的成员&#xff09;&#xff08;事件成员就是事件本身…...

气传导耳机什么牌子好?盘点五款好用的气传导耳机分享

​对于气传导耳机&#xff0c;大家最关心的可能是佩戴会不会不舒服&#xff1f;音质好不好&#xff1f;会不会漏音&#xff1f;等问题。面对这些问题&#xff0c;今天我就为大家推荐几款市面上最好的气传导耳机&#xff0c;总有一款适合你的&#xff01; ①NANK南卡00压气传导…...

GME多模态向量模型实战部署:华为云ModelArts一键启动图文检索

GME多模态向量模型实战部署&#xff1a;华为云ModelArts一键启动图文检索 1. 引言&#xff1a;多模态检索的实用价值 想象一下&#xff0c;你正在管理一个大型数字资产库&#xff0c;里面有成千上万的图片和文档。当你想找"去年会议上讨论过的那张数据流程图"时&am…...

M2LOrder 情绪识别模型 Python 入门实战:快速搭建情感分析 WebUI

M2LOrder 情绪识别模型 Python 入门实战&#xff1a;快速搭建情感分析 WebUI 你是不是经常好奇&#xff0c;一段文字背后藏着怎样的情绪&#xff1f;是喜悦、愤怒&#xff0c;还是悲伤&#xff1f;以前&#xff0c;这可能需要专业的心理学知识去揣摩。但现在&#xff0c;借助A…...

从Android大神到AI先锋!10年程序员血泪转型路,AI工程师高薪秘诀全公开!

一眨眼&#xff0c;我已经工作 10 年了。 在 2022 年以前&#xff0c;我一直相信&#xff0c;在这个行业里&#xff0c;只要技术栈钻得深&#xff0c;比如精通三方框架、熟悉 Android Framework、搞定性能优化&#xff0c;就能端稳饭碗。 但从 2023 年开始&#xff0c;一切都变…...

HUNYUAN-MT惊艳翻译效果:专业领域长文档翻译案例集

HUNYUAN-MT惊艳翻译效果&#xff1a;专业领域长文档翻译案例集 最近在尝试各种翻译工具时&#xff0c;我偶然间用到了HUNYUAN-MT 7B模型来处理一些工作上的专业文档。说实话&#xff0c;一开始没抱太大期望&#xff0c;毕竟专业翻译的门槛不低&#xff0c;尤其是那些充满术语和…...

智能车调参手记:我用Kp=200, Ki=60, Kd=40让小车稳如老狗

智能车调参手记&#xff1a;我用Kp200, Ki60, Kd40让小车稳如老狗 凌晨三点的实验室里&#xff0c;咖啡杯已经见底&#xff0c;眼前的智能车在测试跑道上又一次冲出了弯道。这已经是本周第七次熬夜调试&#xff0c;上坡时的速度波动问题始终困扰着我们。就在准备放弃的时候&…...

STM32CubeIDE工程复制粘贴保姆级教程:告别重复配置,5分钟搞定新项目

STM32CubeIDE工程复制粘贴保姆级教程&#xff1a;告别重复配置&#xff0c;5分钟搞定新项目 每次启动新项目时&#xff0c;你是否还在重复那些繁琐的初始化步骤&#xff1f;从零开始配置时钟树、外设参数、中断优先级&#xff0c;不仅耗时费力&#xff0c;还容易出错。对于经验…...

Qwen3.5-9B-AWQ-4bit参数调优实战:温度=0.7时中文回答质量与响应速度平衡点

Qwen3.5-9B-AWQ-4bit参数调优实战&#xff1a;温度0.7时中文回答质量与响应速度平衡点 1. 模型概述与参数调优背景 Qwen3.5-9B-AWQ-4bit是一个支持图像理解的多模态模型&#xff0c;能够结合上传图片与文字提示词输出中文分析结果。在实际应用中&#xff0c;我们发现温度参数…...

基于Comsol相控阵技术的实用钢纵波超声波成像模型:单层缺陷TFM成像与压力声学仿真

comsol 相控阵 超声成像 此模型为压力声学仿真超声波&#xff0c;实用钢纵波速度6000 密度7.8e-9 单层缺陷TFM成像相控阵超声检测这玩意儿在工业NDT圈子里算是老熟人了&#xff0c;今天咱们拿COMSOL搞个钢材料缺陷成像的骚操作。模型基础是压力声学模块&#xff0c;材料参数先给…...

矩阵分解(1)-- 从高斯消元到对称正定:LU、LDLT与Cholesky分解的算法演进与应用场景

1. 矩阵分解&#xff1a;为什么我们需要它&#xff1f; 想象一下你面前有一堆积木&#xff0c;乱七八糟地堆在一起。如果你想快速找到其中某一块积木&#xff0c;可能需要翻找很久。但如果有人帮你把这些积木按照颜色、形状分类摆放整齐&#xff0c;找起来就会容易得多。矩阵分…...

Phi-4-mini-reasoning案例分享:用逻辑题测试模型对‘必要条件’的理解深度

Phi-4-mini-reasoning案例分享&#xff1a;用逻辑题测试模型对必要条件的理解深度 1. 模型能力定位 Phi-4-mini-reasoning是专为推理任务优化的文本生成模型&#xff0c;其核心优势在于处理需要多步逻辑推导的问题。与通用对话模型不同&#xff0c;它更擅长处理以下类型任务&…...