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

news2024/9/24 11:33:30

目录

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:]function
           Set a breakpoint at function (in file).断点
       run [arglist]
           Start your program (with arglist, if specified).
       bt  Backtrace: display the program stack.显示程序堆栈
       print expr
           Display the value of an expression.打印
       c   Continue running your program (after stopping, e.g. at a
           breakpoint).继续
       next
           Execute next program line (after stopping); step over any function
           calls 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 is
           presently stopped.

       step 
           Execute next program line (after stopping); step into any function
           calls in the line.  执行下一行

       help [name]
           Show information about GDB command name, or general information
           about using GDB.

       quit
           Exit 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 1234
               gdb -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 4849

warning: 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$ 

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

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

相关文章

从Matrix-ResourceCanary看内存快照生成-ForkAnalyseProcessor(1)

前文看到AutoDumpProcessor的处理逻辑主要是生成&#xff0c;裁剪hprof文件并回调到PluginListener中&#xff0c;接下来我们来看下ForkAnalyseProcessor的处理逻辑。 ForkAnalyseProcessor public class ForkAnalyseProcessor extends BaseLeakProcessor {private static fi…

$nextTick属性使用与介绍

属性介绍 $nextTick 是 Vue.js 中的一个重要方法&#xff0c;之前我们也说过$ref 等一些重要的属性&#xff0c;这次我们说$nextTick&#xff0c;$nextTick用于在 DOM 更新后执行回调函数。它通常用于处理 DOM 更新后的操作&#xff0c;因为 Vue 在更新 DOM 后不会立即触发回调…

【0906 C高级day1】 Linux中的文件相关指令

一、使用cut截取出Ubuntu用户的家目录&#xff0c;要求&#xff1a;不能使用":"作为分割 grep "ubuntu" /etc/passwd | cut -d "/" -n -f 2-3 | cut -c 1-11 二、思维导图 文件相关指令&#xff1a;

【Java Web】Servlet规范讲解

目录 一、前言 二、Servlet规范介绍 2.1 常见版本及新功能 2.2 Servlet的作用 2.3 Servlet的本质 三、Servlet接口和实现类 3.1 Servlet接口 3.2 Servlet接口实现类示例 3.3 Servlet接口实现类开发步骤 3.3.1 关键点 3.3.2 引入Servlet源码包 1、描述 Servlet接口…

LeetCode 725. Split Linked List in Parts【链表】中等

本文属于「征服LeetCode」系列文章之一&#xff0c;这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁&#xff0c;本系列将至少持续到刷完所有无锁题之日为止&#xff1b;由于LeetCode还在不断地创建新题&#xff0c;本系列的终止日期可能是永远。在这一系列刷题文章…

【python手写算法】逻辑回归实现分类(含公式推导)

公式推导&#xff1a; 代码实现&#xff1a; # codingutf-8 import matplotlib.pyplot as plt import numpy as npdef f(w1,x1,w2,x2,b):zw1*x1w2*x2breturn 1/(1np.exp(-z)) if __name__ __main__:X1 [12.46, 0.25, 5.22, 11.3, 6.81, 4.59, 0.66, 14.53, 15.49, 14.43,2.1…

优先发展非化石能源

生态兴则文明兴。面对气候变化、环境风险挑战、能源资源约束等日益严峻的全球问题&#xff0c;中国树立人类命运共同体理念&#xff0c;促进经济社会发展全面绿色转型&#xff0c;努力推动本国能源清洁低碳发展。 智慧光伏遮阳伞&#xff0c;搭配座椅设置智能补给休息区&#x…

数据结构--- 树

(一)知识补充 定义 树是一种数据结构,它是由n(n≥0)个有限节点组成一个具有层次关系的集合。把它叫做“树”是因为它看起来像一棵倒挂的树,也就是说它是根朝上,而叶朝下的。​ 它具有以下的特点: 每个节点有零个或多个子节点; 没有父节点的节点称为根节点;每一个非根…

unipush2.0实现APP消息推送(1)

一、 基础概念 1.1 首先理解什么是消息推送&#xff1f; 比如&#xff1a;你手机收到未读消息后会在通知栏显示&#xff0c;该条消息便是某APP的消息推送。多用于有需要从服务端主动发出消息提醒到客户端的场景。 1.2 uniPush & 个推 & 厂商推送的区别 方式个推uni…

js摄像头动态检测

利用摄像头每一秒截图一次图像。然后计算2次图像之间的相似度。 如果相似度低于98%就会报警。 var video document.getElementsByClassName(inputvideo)[0]; video.innerHTML "<video classinput_video idcamera autoplay width640px height380px></video>…

LeetCode 1123. Lowest Common Ancestor of Deepest Leaves【树,DFS,BFS,哈希表】1607

本文属于「征服LeetCode」系列文章之一&#xff0c;这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁&#xff0c;本系列将至少持续到刷完所有无锁题之日为止&#xff1b;由于LeetCode还在不断地创建新题&#xff0c;本系列的终止日期可能是永远。在这一系列刷题文章…

2023高教社杯数学建模C题思路模型 - 蔬菜类商品的自动定价与补货决策

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

X86_64函数调用汇编程序分析

X86_64函数调用汇编程序分析 1 X86_64寄存器使用标准2 对应代码的分析2.1 main函数及其对应的汇编程序2.1.1 main的C代码实现2.1.2 main函数对应汇编及其分析2.1.3 执行完成之后栈的存放情况 2.2 test_fun_a函数及其对应的汇编程序2.2.1 test_fun_a函数的C实现2.2.2 test_fun_a…

【工作技术栈】【源码解读】一次springboot注入bean失败问题的排查过程

目录 前言现象分析原因解决方法思考感悟 前言 对这次的过程排查如果要形容的话&#xff0c;我觉得更像是悬疑剧&#xff0c;bean not found 这种错误&#xff0c;已经看腻了&#xff0c;甚至有时候都看不起这种错误&#xff0c;但是似乎这个想法被springboot听见了&#xff0c…

spring-security-源码解析+自定义拓展

1.参考文档 https://docs.spring.io/spring-security/reference/5.7/servlet/architecture.html 1.1.各种filterchain 1.1.1.SecurityFilterChain 1.1.2.springSecurityFilterChain 1.1.3.Security Filters 2.几个重要的注解 2.1.EnableXXX EnableWebMvcSecurity–deprecate…

C语言之初阶总结篇

目录 NO.1 NO.2 NO.3 NO.4 NO.5 NO.6 NO.7 NO.8 NO.9 NO.10 NO.11 NO.12.概念tips NO.13.求最小公倍数 NO.14.最大公因数 NO.15.输入读取字符串 NO.16.倒置字符串 今天是一些C语言题目&#xff0c;最近天气炎热&#xff0c;多喝水。 NO.1 下面程序执行后&am…

浅谈泛在电力物联网、能源互联网与虚拟电厂

导读&#xff1a;从能源互联网推进受阻&#xff0c;到泛在电力物联网名噪一时&#xff0c;到虚拟电厂再次走向火爆&#xff0c;能源领域亟需更进一步的数智化发展。如今&#xff0c;随着新型电力系统建设推进&#xff0c;虚拟电厂有望迎来快速发展。除了国网和南网公司下属的电…

LLMs之Baichuan 2:《Baichuan 2: Open Large-scale Language Models》翻译与解读

LLMs之Baichuan 2&#xff1a;《Baichuan 2: Open Large-scale Language Models》翻译与解读 导读&#xff1a;2023年9月6日&#xff0c;百川智能重磅发布Baichuan 2。科技论文主要介绍了Baichuan 2&#xff0c;一个开源的大规模语言模型&#xff0c;以及其在多个领域的性能表现…

与 vmx86 驱动程序的版本不匹配: 预期为 410.0,实际为 401.0

与 vmx86 驱动程序的版本不匹配: 预期为 410.0&#xff0c;实际为 401.0。 驱动程序“vmx86.sys”的版本不正确。请尝试重新安装 VMware Workstation。 我电脑历史上装过几个版本的vmware workstation: 怀疑是不兼容版本生成的vmx.86.sys 在系统中和该软件冲突&#xff0c;又没…

【完整代码】2023数学建模国赛C题代码--蔬菜类商品的自动定价与补货决策

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