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$ 

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

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

相关文章

【leetcode 力扣刷题】回文串相关题目(KMP、动态规划)

回文串相关题目 5. 最长回文子串动态规划中心扩展算法 214. 最短回文串336. 回文对 5. 最长回文子串 题目链接&#xff1a;5. 最长回文子串 题目内容&#xff1a; 题目就是要我们找s中的回文子串&#xff0c;还要是最长的。其实想想&#xff0c;暴力求解也行……就是遍历所有的…

Python编程的八大魔法库

Python是一门广受欢迎的编程语言&#xff0c;其生态系统丰富多彩&#xff0c;拥有许多令人惊叹的依赖库&#xff0c;可以帮助程序员们在各种领域中创造出令人瞠目结舌的应用。在这篇文章中&#xff0c;我们将探讨Python编程的十大神奇依赖库&#xff0c;它们像魔法一样&#xf…

springfox及springdoc

open api 简介 OpenApi是一个业界的 API 文档标准&#xff0c;一个规范&#xff0c;提供了一种标准的方式来描述API的结构、参数、响应等信息。springfox和springdoc是用在Spring框架中生成和展示OpenApi文档的工具 springfox springfox基于Spring框架的库&#xff0c;提供注…

jmeter 计数器Counter

计数器可以用于生成动态的数值或字符串&#xff0c;以模拟不同的用户或数据。 计数器通常与用户线程组结合使用&#xff0c;以生成不同的变量值并在测试中应用。以下是计数器的几个常用属性&#xff1a; 变量前缀&#xff08;Variable Name Prefix&#xff09;&#xff1a;定义…

G. The Morning Star

Problem - G - Codeforces 思路&#xff1a;想了挺长时间的&#xff0c;一直没想到一个简便的方法在瞎搞。我们发现对于某个点来说&#xff0c;其他的点如果能够跟他匹配&#xff0c;那么一定在这8个方向上&#xff0c;而同时这8个方向其实对应这4条直线&#xff0c;假设点为(x…

静态路由 网络实验

静态路由 网络实验 拓扑图初步配置R1 ip 配置R2 ip 配置R3 ip 配置查看当前的路由表信息查看路由表信息配置静态路由测试 拓扑图 需求&#xff1a;实现 ip 192.168.1.1 到 192.168.2.1 的通信。 初步配置 R1 ip 配置 system-view sysname R1 undo info-center enable # 忽略…

C语言经典100例题(55)--从一个整数a中把从右端开始的4-7位取出来

目录 题目 问题分析 右移操作符 左移操作符 方法一 方法二 运行结果 题目 用c语言从一个整数a中把从右端开始的4-7位取出来 问题分析 右移操作符 右移操作符是一种位运算符&#xff0c;用于将二进制数向右移动指定的位数。它通常用符号" >> "表示…

W11下CMake MinGW配置OpenCV和Qt

&#x1f482; 个人主页:风间琉璃&#x1f91f; 版权: 本文由【风间琉璃】原创、在CSDN首发、需要转载请联系博主&#x1f4ac; 如果文章对你有帮助、欢迎关注、点赞、收藏(一键三连)和订阅专栏哦 前言 前几天将cuda版本的opencv给编译成功了&#xff0c;当时用的VS的MSVC&…

python包导入原理解析

原文链接&#xff1a; https://www.cnblogs.com/hi3254014978/p/15317976.html 根据编程经验的不同&#xff0c;我们在运行程序时可能经常或者偶尔碰到下面这些问题&#xff0c;仔细观察后会发现这些问题无一例外都出现了一个相同的短语&#xff0c;很容易就可以发现&#xff0…

day37 线程

一、线程安全 二、多线程并发的安全问题 当多个线程并发操作同一临界资源 由于线程切换实际不确定 导致操作顺序出现混乱 产生的程序bug 严重时出现系统瘫痪 临界资源 &#xff1a;操作该资源的完整流程同一时间只能被单一线程操作的资源 多线程并发会出现的各种问题、 如…

聊聊 HTMX 吧

写在前面 最近看了几篇关于 htmx 的文章&#xff0c;自己也去看了一眼官网&#xff0c;也去油管看了一下当时 htmx 发布会的时候他们的演示&#xff0c;下面说几点我对这个所谓的新型起来的技术的看法&#xff0c; 他的来源是什么 首先说一下他虽然是一个新型的技术&#xff0c…

jQuery选择器参考手册

选择器实例选取*$(“*”)所有元素#id$(“#lastname”)id“lastname” 的元素.class$(“.intro”)class“intro” 的所有元素.class,.class$(“.intro,.demo”)class 为 “intro” 或 “demo” 的所有元素element$(“p”)所有 <p> 元素el1,el2,el3$(“h1,div,p”)所有 <…

数据库管理系统,数据库,sql的基本介绍以及它们之间的关系

数据库管理系统&#xff08;Database Management System&#xff0c;简称DBMS&#xff09;是一种软件工具或系统&#xff0c;用于管理和维护数据库的创建、访问、更新和管理。DBMS允许用户在数据库中存储、检索和操作数据&#xff0c;同时提供了数据安全性、完整性和一致性的控…

Java 多线程系列Ⅶ(线程安全集合类)

线程安全集合类 前言一、多线程使用线性表二、多线程使用栈和队列三、多线程下使用哈希表 前言 在数据结构中&#xff0c;我们学习过 Java 的内置集合&#xff0c;但是我们知道&#xff0c;我们学过的大多数集合类都是线程不安全的&#xff0c;少数如 Vector&#xff0c;Stack…

小程序分销机制介绍,小程序二级分销功能有哪些?

为什么有越来越多的用户选择使用小程序&#xff1f;跟“高大上”的APP相比&#xff0c;小程序不仅可以减少下载安装的复杂流程&#xff0c;还具备操作便捷、沉淀私域数据的优势。蚓链分销小程序具备裂变二维码、实时分佣、分销身份升级、层级分佣、商品个性化佣金设定等功能&am…

TortoiseSVN 详细操作指南

文章底部有个人公众号&#xff1a;热爱技术的小郑。主要分享开发知识、有兴趣的可以关注一下。为何分享&#xff1f; 踩过的坑没必要让别人在再踩&#xff0c;自己复盘也能加深记忆。利己利人、所谓双赢。 热爱技术的小郑 1、引言 考虑以下几种情况&#xff1a; 你是否在一个…

设计模式-代理模式Proxy

代理模式Proxy 代理模式 (Proxy)1) 静态代理1.a) 原理解析1.b) 使用场景1.c) 静态代理步骤总结 2) 动态代理2.a) 基于 JDK 的动态代理实现步骤2.b) 基于 CGLIB 的动态代理实现步骤2.c) Spring中aop的使用步骤 代理模式 (Proxy) 代理设计模式&#xff08;Proxy Design Pattern&…

golang面试题:json包变量不加tag会怎么样?

问题 json包里使用的时候&#xff0c;结构体里的变量不加tag能不能正常转成json里的字段&#xff1f; 怎么答 如果变量首字母小写&#xff0c;则为private。无论如何不能转&#xff0c;因为取不到反射信息。如果变量首字母大写&#xff0c;则为public。 不加tag&#xff0c…

【Spring Cloud系统】- 轻量级高可用工具Keepalive详解

【Spring Cloud系统】- 轻量级高可用工具Keepalive详解 文章目录 【Spring Cloud系统】- 轻量级高可用工具Keepalive详解一、概述二、Keepalive分类2.1 TCP的keepalive2.2 HTTP的keep-alive2.3 TCP的 KeepAlive 和 HTTP的 Keep-Alive区别 三、nginx的keepalive配置3.1 nginx保持…

在Docker中运行PostgreSQL数据库

1.下载Docker 2.设置DockerHub账号 3.运行Docker并下载Image 4.启动PostgreSQL Image 5.连接到数据库运行SQL docker run --name some-postgres -p 5432:5432 -e POSTGRES_PASSWORDmysecretpassword -d postgres 开放端口从Docker容器到主操作系统&#xff0c;这将允许我们…