GDB 远程调试简介

文章目录

  • 1. 前言
  • 2. GDB 远程调试
    • 2.1 准备工作
      • 2.1.1 准备 `客户端 gdb` 程序
      • 2.1.2 准备 `服务端 gdbserver`
      • 2.1.3 准备 被调试程序
    • 2.2 调试
      • 2.2.1 通过网络远程调试
        • 2.2.1.1 通过 gdbserver 直接启动程序调试
        • 2.2.1.2 通过 gdbserver 挂接到已运行程序调试
      • 2.2.2 通过串口远程调试
        • 2.2.1.1 通过 gdbserver 直接启动程序调试
        • 2.2.1.2 通过 gdbserver 挂接到已运行程序调试
  • 3. 参考资料

1. 前言

限于作者能力水平,本文可能存在谬误,因此而给读者带来的损失,作者不做任何承诺。

2. GDB 远程调试

GDB 远程调试同时需要 服务端 gdbserver客户端 gdb 两个程序。本文以 ARM32 嵌入式设备内程序的调试为例,对 GDB 远程调试方法进行说明。

先对 服务端 gdbserver客户端 gdb 连接拓扑做一个梗概描绘:
在这里插入图片描述

2.1 准备工作

2.1.1 准备 客户端 gdb 程序

客户端 gdb 通过和 服务端 gdbserver 的交互,对运行于目标机器的程序进行调试。笔者使用 Ubuntu 系统作为调试客户端的宿主机,即 客户端 gdb 运行于 Ubuntu 下。

笔者测试使用的嵌入式设备 SDK 的交叉编译工具链已经包含了 客户端 gdb 程序,无需额外准备:

$ arm-linux-gnueabihf-gdb --version
GNU gdb (Linaro GDB 2016.02) 7.10.1.20160210-cvs
Copyright (C) 2015 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 "--host=x86_64-unknown-linux-gnu --target=arm-linux-gnueabihf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://bugs.launchpad.net/gcc-linaro>.
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".

如果读者的交叉编译工具链没有包含 gdb,在 Ubuntu 下可通过命令安装适用于多架构平台的 gdb 客户端程序

sudo apt-get install gdb-multiarch

2.1.2 准备 服务端 gdbserver

gdbserver 运行于被调试的目标机器,在本文中目标机器指 ARM 嵌入式设备。笔者是通过 buildroot 构建的 gdbserver,不想使用 buildroot 的读者,可自行查阅相关资料进行 gdbserver 的构建。

构建好 gdbserver 后,将其拷贝到目标机器。笔者是将其拷贝到目标机器/usr/bin 目录,这样可以不用指定路径运行。

2.1.3 准备 被调试程序

/* test.c */

#include <unistd.h>

int main(void)
{
	while (1)
		sleep(5);

	return 0;
}

编译,然后将测试程序 test 拷贝到目标机器

$ arm-linux-gnueabihf-gcc -o test test.c

2.2 调试

2.2.1 通过网络远程调试

在这里插入图片描述

2.2.1.1 通过 gdbserver 直接启动程序调试

首先,在目标机器(本文是指 ARM32 嵌入式设备)上运行命令:

# gdbserver :2345 test 
Process test created; pid = 440
Listening on port 2345

然后,在调试器客户端机器(本文是指 Ubuntu)上运行下列命令:

$ arm-linux-gnueabihf-gdb
GNU gdb (Linaro GDB 2016.02) 7.10.1.20160210-cvs
Copyright (C) 2015 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 "--host=x86_64-unknown-linux-gnu --target=arm-linux-gnueabihf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://bugs.launchpad.net/gcc-linaro>.
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".
(gdb) target remote 192.168.3.8:2345
Remote debugging using 192.168.3.8:2345
Reading /root/test from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
Reading /root/test from remote target...
Reading symbols from target:/root/test...done.
Reading /lib/ld-linux-armhf.so.3 from remote target...
Reading /lib/ld-linux-armhf.so.3 from remote target...
Reading symbols from target:/lib/ld-linux-armhf.so.3...(no debugging symbols found)...done.
0xb6fd7a00 in _start () from target:/lib/ld-linux-armhf.so.3
(gdb)

客户端宿主机(本文指 Ubuntu)启动 arm-linux-gnueabihf-gdb 后,通过命令 target remote 192.168.3.8:2345 连接到目标机器。其中,192.168.3.8 是目标机器的的 IP。

这时候,目标机器(本文是指 ARM 嵌入设备)会输出 gdb 客户端连接的消息 Remote debugging from host 192.168.3.168

# gdbserver :2345 test 
Process test created; pid = 440
Listening on port 2345
Remote debugging from host 192.168.3.168

到此,就可以通过 gdb 在客户端宿主机(本文指 Ubuntu),调试目标机器(本文是指 ARM 嵌入设备)的 test 程序了。

2.2.1.2 通过 gdbserver 挂接到已运行程序调试

如果 test 已经运行,可以通过 attach 方式启动 gdbserver,挂接到 test 程序进行调试。

先在目标机器(本文是指 ARM32 嵌入式设备)上启动 test 程序,放入后台运行:

# ./test &
# ps -ef | grep -v grep | grep "test"
  445 root     ./test

gdbserver 挂接到 test 进程:

# gdbserver --attach :2345 445
Attached; pid = 445
Listening on port 2345

客户端宿主机(本文指 Ubuntu)启动 arm-linux-gnueabihf-gdb,然后通过命令 target remote 192.168.3.8:2345 连接到目标机器调试程序:

$ arm-linux-gnueabihf-gdb
GNU gdb (Linaro GDB 2016.02) 7.10.1.20160210-cvs
Copyright (C) 2015 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 "--host=x86_64-unknown-linux-gnu --target=arm-linux-gnueabihf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://bugs.launchpad.net/gcc-linaro>.
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".
(gdb) target remote 192.168.3.8:2345
Remote debugging using 192.168.3.8:2345
Reading /root/test from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
Reading /root/test from remote target...
Reading symbols from target:/root/test...done.
Reading /lib/libc.so.6 from remote target...
Reading /lib/ld-linux-armhf.so.3 from remote target...
Reading symbols from target:/lib/libc.so.6...(no debugging symbols found)...done.
Reading symbols from target:/lib/ld-linux-armhf.so.3...(no debugging symbols found)...done.
Reading /lib/ld-linux-armhf.so.3 from remote target...
0xb6e9e134 in nanosleep () from target:/lib/libc.so.6
(gdb)

2.2.2 通过串口远程调试

在这里插入图片描述

2.2.1.1 通过 gdbserver 直接启动程序调试

目标机运行命令:

gdbserver /dev/ttyS1 test

其中,/dev/ttyS1目标机上和客户端宿主机连接的串口设备对象。

客户端宿主机运行命令:

$ arm-linux-gnueabihf-gdb
[......]
(gdb) target remote /dev/ttyUSB0

其中,/dev/ttyUSB0客户端宿主机上和目标机连接的串口设备对象。

2.2.1.2 通过 gdbserver 挂接到已运行程序调试

目标机上依次运行下列命令:

# test &
# ps -ef | grep -v grep | grep "test" ## 查询 test 进程 PID
# gdbserver --attach /dev/ttyS1 445

客户端宿主机运行命令:

$ arm-linux-gnueabihf-gdb
[......]
(gdb) target remote /dev/ttyUSB0

3. 参考资料

[1] Using the gdbserver program
[2] gdbserver(1) — Linux manual page

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

紫鸟浏览器搭配IPXProxy代理IP的高效使用指南

​紫鸟指纹浏览器一款专门为跨境电商而生的防关联浏览器&#xff0c;能够帮助跨境电商卖家解决多店铺管理问题。紫鸟指纹浏览器为跨境电商卖家提供稳定的登录环境&#xff0c;并且搭配IP代理&#xff0c;能够解决浏览器指纹记录问题&#xff0c;提高操作的安全性。那如何利用紫…

广州AI绘图模型训练外包定制公司

&#x1f680;设计公司如何借助AI人工智能降本增效&#xff0c;广州这家AI公司值得借鉴— 触站AI&#xff0c;智能图像的创新引擎 &#x1f31f; &#x1f3a8; 触站AI&#xff0c;绘制设计界的未来蓝图 &#x1f3a8;在AI技术的浪潮中&#xff0c;触站AI以其前沿的AI图像技术…

RK3568驱动指南|第十六篇 SPI-第188章 mcp2515驱动编写:复位函数

瑞芯微RK3568芯片是一款定位中高端的通用型SOC&#xff0c;采用22nm制程工艺&#xff0c;搭载一颗四核Cortex-A55处理器和Mali G52 2EE 图形处理器。RK3568 支持4K 解码和 1080P 编码&#xff0c;支持SATA/PCIE/USB3.0 外围接口。RK3568内置独立NPU&#xff0c;可用于轻量级人工…

Redux 使用及基本原理

什么是Redux Redux 是用于js应用的状态管理库&#xff0c;通常和React一起用。帮助开发者管理应用中各个组件之间的状态&#xff0c;使得状态的变化变得更加可预测和易于调试。 Redu也可以不和React组合使用。&#xff08;通常一起使用&#xff09; Redux 三大原则 单一数据源…

在uni-app使用vue3使用vuex

在uni-app使用vue3使用vuex 1.在项目目录中新建一个store目录&#xff0c;并且新建一个index.js文件 import { createStore } from vuex;export default createStore({//数据&#xff0c;相当于datastate: {count:1,list: [{name: 测试1, value: test1},{name: 测试2, value: …

从hugging face 下模型

支持国内下载hugging face 的东西 下模型权重 model_id 是红色圈复制的 代码 记得设置下载的存储位置 import os from pathlib import Path from huggingface_hub import hf_hub_download from huggingface_hub import snapshot_downloadmodel_id"llava-hf/llava-v1…

Swift 中强大的 Key Paths(键路径)机制趣谈(下)

概览 在上一篇博文 Swift 中强大的 Key Paths(键路径)机制趣谈(上)中,我们介绍了 Swift 语言中键路径机制的基础知识,并举了若干例子讨论了它的一些用武之地。 而在本文中我们将再接再厉,继续有趣的键路径大冒险,为 KeyPaths 画上一个圆满的句号。 在本篇博文中,您将…

C++:二维数组的遍历

方式一&#xff1a; #include <vector> #include <iostream> int main() { // 初始化一个2x3的二维向量&#xff08;矩阵&#xff09; std::vector<std::vector<float>> matrix { {1.0, 2.0, 3.0}, // 第一行 {4.0, 5.0, 6.0} // 第二行 };…

企业备份NAS存储一体机

企业文件服务器上的数据、员工电脑里的数据以及NAS存储内数据&#xff0c;需要及时备份&#xff0c;Inforternd存储设备内置了强大的备份服务器功能&#xff0c;无需额外费用&#xff0c;就能轻松将重要数据备份至安全可靠的存储空间中。 无论是GS或GSe 统一存储产品&#xff0…

开放式耳机怎么选?五大2024年口碑销量爆棚机型力荐!

在选购开放式耳机的时候&#xff0c;我们总会因为有太多的选择而陷入两难&#xff0c;又想要一个颜值比较高的&#xff0c;又想要同时兼顾性能还不错的&#xff0c;所以作为测评博主&#xff0c;今天我们就给大家带来自己的一些选购技巧和自己觉得还不错开放式耳机&#xff0c;…

不同行业如何选择适合自己行业的项目管理工具?

在当今的信息化时代&#xff0c;项目管理软件已成为各行各业不可或缺的工具。然而&#xff0c;由于各行业具有不同的特点和需求&#xff0c;因此选择合适的项目管理软件成为了一个重要问题。本文将探讨不同行业在选择项目管理软件时需要考虑的因素&#xff0c;希望能帮助大家更…

python-图像模糊处理(赛氪OJ)

[题目描述] 给定 n 行 m 列的图像各像素点的灰度值&#xff0c;要求用如下方法对其进行模糊化处理&#xff1a; 1. 四周最外侧的像素点灰度值不变。 2. 中间各像素点新灰度值为该像素点及其上下左右相邻四个像素点原灰度值的平均&#xff08;四舍五入&#xff09;输入&#xff…

安卓微商大师V3.4.0/高级版一键群发僵尸粉检测

一款高效获取客源&#xff0c;备受好评的微商工具&#xff0c;资源丰富&#xff0c;秒速获得客源&#xff0c;大量群客源&#xff0c;都是散客&#xff0c;携手创业&#xff0c;是做微商生意的首选工具。打开即是黑钻高级会员 赶快体验吧 很强大 链接&#xff1a;https://pan.…

针对 Windows 10 的功能更新,版本 22H2 - 错误 0xc1900204

最近想帮女朋友生win11发现她电脑安装更新总是卡到安装%10这里失败 原来是安装路径被修改过了&#xff0c;改回c盘 win R → 输入regedit 计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion

分布式日志采集 Loki 配置及部署详细

分布式日志采集 Loki 配置及部署详细 Loki 部署模式Loki 读写分离部署配置Loki 配置大全 Loki 部署模式 &#xff08;1&#xff09;可扩展部署模式 Loki 的简单可扩展部署模式是最简单的部署方式、首选方式。可扩展到每天几TB的日志&#xff0c;但是如果超出这个范围&#xff…

线下生鲜蔬果店做小程序有什么方法

生鲜蔬果是生活所需&#xff0c;大小商家众多&#xff0c;零售批发各种经营模式&#xff0c;小摊贩或是超市门店都有着目标客户或准属性群体。竞争和获客转化也促进着商家寻找客源和加快线上进程。 尤其是以微信社交为主的私域场景&#xff0c;普客/会员都需要精细化管理营收和…

WebSocket解决方案(springboot 基于Redis发布订阅)

WebSocket 因为一般的请求都是HTTP请求&#xff08;单向通信&#xff09;&#xff0c;HTTP是一个短连接&#xff08;非持久化&#xff09;&#xff0c;且通信只能由客户端发起&#xff0c;HTTP协议做不到服务器主动向客户端推送消息。WebSocket确能很好的解决这个问题&…

携手共筑爱的桥梁:引导接纳自闭症同学

在孩子的班级中&#xff0c;当自闭症儿童成为我们共同的一员时&#xff0c;作为老师和家长&#xff0c;我们肩负着特别的责任——引导孩子们以开放的心态接纳、善待并关爱他们。 首先&#xff0c;我们要以身作则&#xff0c;展现接纳与尊重。无论是老师还是家长&#xff0c;都…

【计算机网络】计算机网络的分类

计算机网络的分类 导读一、按分布范围分类1.1 广域网&#xff08;WAN&#xff09;。1.2 城域网&#xff08;MAN&#xff09;1.3 局域网&#xff08;LAN&#xff09;1.4 个人区域网&#xff08;PAN&#xff09;1.5 多处理器系统 二、按传输技术分类2.1 广播式网络2.2 点对点网络…

Ajax异步请求 axios

Ajax异步请求 axios 1 axios介绍 原生ajax请求的代码编写太过繁琐,我们可以使用axios这个库来简化操作&#xff01; 在后续学习的Vue(前端框架)中发送异步请求,使用的就是axios. 需要注意的是axios不是vue的插件,它可以独立使用. axios说明网站&#xff1a;(https://www.kancl…