交叉编译构建GDB和GDBServer

2023-05-16

1. Problem statement

“I have a ARM GNU/Linux board, and I want to be able to debug programs running in it, from the comfort of my x86_64 GNU/Linux machine. How do I build GDB and GDBserver for this scenario? I’m confused with the configure options I need to specify. Do I need to cross compile GDB for ARM too? Help!”

2. build, host, target, what??

As most GNU programs, GDB uses autoconf as part of its build system.

Here’s the chapter of the GNU autoconf, automake and libtool book everyone building cross tools should read (do follow the right arrows):

“Cross Compilation with GNU Autotools”

Specifically, understanding the distinction between build, host and target is crucial.

Autotools Mythbuster has a nice explanation. Simplified:

When using autoconf, there are three system definitions (or machine
definitions) that are used to identify the “actors” in the build
process; (…) These three definitions are:

  • host
    The system that is going to run the software once it is built. Once the software
    has been built, it will execute on this particular system.

  • build
    The system where the build process is being executed. For most uses this
    would be the same as the host system, but in case of cross-compilation
    the two obviously differ.

  • target
    The system against which the software being built will run on. This only exists, or rather
    has a meaning, when the software being built may interact specifically with a
    system that differs from the one it’s being executed on (our host). This is the case
    for compilers, debuggers, profilers and analyzers and other tools in general.

Go on read those linked chapters please; I’ll wait. 😃 Alright, onwards.

When thinking in terms of “parked in front of a x86_64 machine”, we say GDB runs on your host system, the x86_64 GNU/Linux machine, while GDBserver runs on the target, the ARM GNU/Linux board/machine.

However, GDB and GDBserver are separate programs. They get built separately, so we get to apply the build/host/target distinction for each separate build step.

3. Okay, how does that apply to GDB and GDBserver then?

3.1. For GDB

You need a GDB that runs on x86. With autoconf/configure, you use --host to specify where the program runs. So in your case that would be --host=x86_64-unknown-linux-gnu. However, if the program is meant to be run on the same system you are building from, then you don’t need to specify that, as configure will pick your system as default for --host automatically. What you need is to make it so that the built GDB understands how to debug ARM GNU/Linux programs. This is where --target comes along. Say you have a arm-linux-gnueabi-gcc etc. cross toolchain available on your x86_64 development host, used for building programs that run on your ARM board (file names might vary, even for ARM GNU/Linux). From the compiler’s file name, or from running arm-linux-gnueabi-gcc -v (look for the “Target:” line in the output), we see that gcc was configured with --target=arm-linux-gnueabi. We pass the same --target switch to GDB’s configure. Done. That’s it, really.

You’ll see something like this:

$ /path/to/gdb-src/configure --target=arm-linux-gnueabi
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... arm-unknown-linux-gnueabi

If you don’t specify --target, then the configure script defaults the target system to the same system you’re building on. In this case, that would be x86_64-unknown-linux-gnu, which is not what we want.

Now run ‘make all-gdb’. If you run just ‘make’, this will build all the top level components, including gdb, gdbserver, binutils, ld, gas, etc., all for the host. Alternatively, you can pass --disable-foo to configure to disable top level components, and then ‘make’ will build only the enabled ones. For example, ‘…/configure --disable-ld --disable-gas --disable-sim && make’.

To install, do ‘make install-gdb’, or ‘make install’ if you disabled components you don’t want at configure time.

3.2. For GDBserver

So again, GDBserver runs on the “target” machine, the ARM GNU/Linux one. You could copy the sources over the the ARM GNU/Linux machine, and build it there. The standard /path/to/gdb-src/configure && make && make install will work fine.

However, you want to build GDBserver in the comfort of your x86_64 GNU/Linux host. You have an ARM cross compiler handy after all, right? Recapping from autoconf’s build/host/target distinction, --host is used to specify where the program runs. Bingo! Since GDBserver will run on ARM, you pass --host=arm-linux-gnueabi to configure. You’ll get something like:

$ /path/to/gdb-src/configure --host=arm-linux-gnueabi
checking build system type... x86_64-unknown-linux-gnu
checking host system type... arm-unknown-linux-gnueabi
checking target system type... arm-unknown-linux-gnueabi

(“But why not --target?” Okay, you need to go read the build/host/target distinction again. 😉 --target doesn’t really make sense for GDBserver, because it not a “cross” tool – it knows how to debug the programs on its own system. It’s no different from most other programs running on the ARM system, like grep, sed, etc, so you cross build it just like all other autoconf/configure programs.)

Note, you run the same toplevel configure for GDBserver as you did for GDB (/path/to/gdb-src/configure). Now do ‘make all-gdbserver’. (In GDB/GDBserver versions prior to 10.1, GDBserver was not a top level component, and you would need to run GDBserver’s configure instead (/path/to/gdb-src/gdb/gdbserver/configure), and do ‘make’.)

Same as when building GDB, you can pass --disable-foo to disable top level components, you don’t need on your target machine, including GDB, and then you can just use ‘make’. For example, ‘…/configure --disable-gdb --disable-ld --disable-gas --disable-sim --disable-gprofng && make’.

To install, do ‘make install-gdbserver’, or ‘make install’ if you disabled components you don’t want at configure time.

4. Troubleshooting

4.1. make uses system compiler?

If you’ve configured with --host=$host, but make uses the system’s gcc instead of $host-gcc, make sure the latter can be found in your PATH, and that you can execute it (e.g., try $ arm-unknown-linux-gnueabi-gcc -v), and start over. The reason is that if configure doesn’t find a usable $host-gcc, then it falls back to “gcc”. See here for more info.

5. On separate build dirs

Final note: You don’t have to, but I recommend building on a separate build directory from the sources. Like,

Sources in gdb/src/

Build directory for gdb in gdb/build-gdb/

Build directory for gdbserver in gdb/build-gdbserver/

Cd into gdb/build-gdb/, and do ‘…/src/configure …’

Cd into gdb/build-gdbserver/, and do ‘…/src/configure …’

This way, at any time you want to do a build from scratch, you just need to wipe (rm -rf) the build directories.

6. TL;DR

It’s not that long! Learning this stuff properly will translate into cross building all sorts of GNU autoconf based programs and libraries, so it’s a skill worth having.

Anyway, to recap:

For GDB, do ‘configure --target=arm-linux-gnueabi’. Run the toplevel configure for that (/path/to/gdb-src/configure). Do ‘make all-gdb’ and ‘make install-gdb’.

For GDBserver do ‘configure --host=arm-linux-gnueabi’. Starting with GDB 10.1, run the toplevel configure for that (/path/to/gdb-src/configure). Do ‘make all-gdbserver’ and ‘make install-gdbserver’. For older versions, run GDBserver’s configure instead (/path/to/gdb-src/gdb/gdbserver/configure), and do ‘make’ and ‘make install’.

原文链接

  1. Building GDB and GDBserver for cross debugging
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

交叉编译构建GDB和GDBServer 的相关文章

  • GDB:警告:在重载方法上设置了多个断点

    anisha linux dopx gt g Wall pedantic breakpoints cpp g anisha linux dopx gt gdb a out gdb b X X Breakpoint 1 at 0x400ac1
  • 显示 GDB 中当前的汇编指令

    我正在 GDB 中进行一些汇编级调试 有没有办法让 GDB 以与显示当前源代码行相同的方式显示当前的汇编指令 每个命令后的默认输出如下所示 0x0001433f 990 Foo bar p 这给了我当前指令的地址 但我必须继续参考disas
  • 是什么让GDB拒绝崩溃?

    我在这里不知所措 我正在用 C 编写一个编译器 出于爱好 并使用 GDB 7 3 在 amd64 Linux 2 6 32 上使用 GCC 4 6 1 进行编译 除了通常的 I 等之外 标志还有 Wall Wextra O0 g 我有一个函
  • 如何在 Xcode 4 中从断点操作打印字符串值?

    我有一个断点操作 并且正在使用下拉列表中的 日志 选项 我想打印出字符串 摘要 值 我正在这样做 the person name is p name 但这会打印内存地址 我可以切换到调试器命令选项并执行以下操作 po f name 但后来我
  • OSX Lion 上的 GDB 7.3.1

    我正在尝试使用 macports 中的 GDB v 7 3 1 来调试用以下命令编译的可执行文件 g 4 7 也来自 macports 但是 我启动调试器 得到以下输出 GNU gdb GDB 7 3 1 版权所有 C 2011 自由软件基
  • windows下无法使用mingw/gdb在eclipse helios cdt上调试小程序,控制台冻结

    我一直在尝试使用 Eclipse CDT 来做一些 C 示例 我可以使用 run 命令很好地运行它们 但是每当我尝试调试时 控制台窗口就会冻结 我可以输入 但程序不运行不继续了 当我调试时 我在控制台窗口上得到以下输出 没有断点 但由于默认
  • 如何将 gdb 附加到 docker 容器中运行的进程?

    我在 docker 容器中有一个长时间运行的进程 我想将 gdb 附加到该进程以查看正在运行的线程并获取堆栈跟踪 我可以从主机附加到进程 但无法解析任何符号 因为可执行文件位于文件系统中的不同位置 位于 docker 安装的卷中 并且共享系
  • 如何在 gdb 中附加进程

    我有一个简单的 C 程序 它分叉一个进程 然后运行一个可执行文件 我想将子进程附加到 gdb 我在控制台中运行主程序并打开另一个控制台来查找子进程的 pid 然后使用以下命令启动 gdb gdb attach 12271 where 122
  • 将大核心文件转换为“minicore”文件

    如何将核心文件减少到仅线程堆栈 我希望能够运行 gdbthread apply all bt在迷你核心上 仅此而已 我正在处理大型 gt 4GB 多线程 Linux ELF 核心文件 这些文件太大而无法返回进行分析 我见过谷歌断点器 htt
  • gdb:“未加载符号表”

    尝试在 gdb 中添加断点时 我不断收到此错误消息 我使用这些命令来编译 gcc g main c utmpib2 c o main o and cc g main c utmpib2 c o main o and also g g mai
  • Go:使用 gdb 打印变量

    在此程序中 如何使用调试器中断执行并打印 i 的值 package main import fmt func main x abc i 3 fmt Println i fmt Println x 我无法打印我 不过我可以打印 x go bu
  • 断点改变程序流程

    我正在尝试分析和逆向我拥有的 Objective C 程序 我通过手动更改一些操作码对可执行文件进行了一些修改 然而 当我测试修改后的软件时 我得到 死亡人数 9 没关系 我想我触碰了不该触碰的东西 我当时就推出了gdb myprogram
  • 在 gdb 中设置应用程序关联

    有没有一种简单的方法可以设置我正在调试的应用程序的亲和力 而无需将 gdb 锁定到同一核心 我问的原因是应用程序以实时优先级运行 并且需要在单核上运行 目前我使用这个命令行 taskset c 3 gdbserver 1234 app ou
  • 在 GDB 中显示结构体值

    在 GDB 中 给定一个指向结构体的变量 print将显示原始指针值并x将显示指向的原始字节 有什么方法可以显示指向该结构的数据 即字段及其值的列表 print variable 如果这样做 它将在 GDB 中显示该变量的值 您还可以选择显
  • 从命令输出中设置 GDB 中的环境变量

    我试图在挑战中利用缓冲区溢出 缓冲区从环境变量中获取其值 在 GDB 中 我知道您可以使用以下命令设置环境变量 set environment username test 但是我需要传递用户名变量特殊字符 所以我需要执行以下操作 set e
  • GDB错误:“进程记录:当前架构不支持记录功能”

    我正在尝试在 GDB 中进行反向执行 特别是target record按照说明在 gdb 中运行我的程序后here https stackoverflow com questions 1206872 go to previous line
  • 观察点固定地址

    对于我当前的嵌入式应用程序 我尝试将 GDB 观察点放在固定的内存地址处 例如 我的应用程序更新以下地址 0x10793ad0 为了确定代码的哪一部分破坏了值 我尝试了 watch 0x10793ad0 即使 GDB 在此之后不会打印任何错
  • gcc 中 -g 选项的作用是什么

    我看到很多关于 gdb 的教程要求在编译 c 程序时使用 g 选项 我无法理解 g 选项的实际作用 它使编译器将调试信息添加到生成的二进制文件中 此信息允许调试器将代码中的指令与源代码文件和行号相关联 拥有调试符号可以使某些类型的调试 例如
  • GDB单步汇编并显示下一条将要执行的指令。 [复制]

    这个问题在这里已经有答案了 使用 gdb 调试器可以执行什么命令来单步执行并显示将要执行的下一条指令 我熟悉windbg这个操作非常简单 例如 我有以下函数 当我通过以下方式进入代码时si我想显示将要执行的下一条指令 而无需通过反汇编进行完
  • 在 C++ 代码 gdb 中回溯指针

    我在运行 C 应用程序时遇到段错误 在 gdb 中 它显示我的一个指针位置已损坏 但我在应用程序期间创建了 10 万个这样的对象指针 我怎样才能看到导致崩溃的一个 我可以在 bt 命令中执行任何操作来查看该指针的生命周期吗 谢谢 鲁奇 据我

随机推荐

  • libcurl异步方式使用总结

    原文链接 xff1a https www cnblogs com Newdawn p 10051231 html libcurl这个库的同步方式很简单 xff0c 不做介绍 xff0c 而异步方式很难理解 xff0c 本博客参考官网的dem
  • 开源项目中的法律风险

    引言 写这篇博客的契机是我厂刚好开了一次这样的培训 xff0c 听了以后觉得很有收获 碰巧自己最近也在写开源项目 xff0c 因此觉得还是有必要写一下 有小伙伴提到 xff0c 这种问题 xff0c 去网上找那个指导你如何选择 LICENS
  • UTF8中文编码范围

    简介 UTF 8有点类似于Haffman编码 xff0c 它将Unicode编码为 xff1a 00000000 0000007F的字符 xff0c 用单个字节来表示 xff1b 00000080 000007FF的字符用两个字节表示 xf
  • MLO/uboot-spl.bin和uboot.img/uboot.bin

    前段时间使用TI的am4378芯片 xff0c 发现系统在SD卡启动的时候 xff0c 启动文件使用的是MLO和uboot img xff1b 而Norflash和eMMC启动的时候使用的是 uboot spl bin和uboot bin
  • 身份证校验码规则

    背景 项目中有部分功能需要验证用户身份 为了防止用户随便输入身份信息 因此要对输入数据进行验证 于是参照百科提供的规则进行了实现 公民身份号码是特征组合码 xff0c 由十七位数字本体码和一位数字校验码组成 排列顺序从左至右依次为 xff1
  • 【C语言】代码分析--条件编译及编译预处理阶段

    来自博客园 Rusty 39 s code 一 C语言由源代码生成的各阶段如下 xff1a C源程序 xff0d gt 编译预处理 xff0d gt 编译 xff0d gt 优化程序 xff0d gt 汇编程序 xff0d gt 链接程序
  • GB2132转UTF-8

    背景 单片机端常用的中文显示字符集是GB2312 相对于UTF 8表示中文时更节省空间 但是Linux端为了通用及兼容性常采用UTF 8作为字符编码 为了保持编码的的统一 网络通信时单片机内部将GB2312转为UTF 8发送给Linux 于
  • 操作系统中C程序内存分布

    memory management is one of the most important topics for a Programmer and so understanding the Memory Layout of a C Pro
  • win下使用Python获取串口列表

    背景 一个工具需要使用串口 可是计算机中有时候又不仅有一个串口接口 因此需要获取串口列表并且区分那个是串口接口 代码 span class token comment coding utf 8 span span class token k
  • Vim快捷键-键位图

    背景 嵌入式linux开发中 经常接触linux环境 最方便的莫过于使用vi 功能强大 适用范围广 因此了解一些vim中常见的命令对于日常工作学习大有裨益 针对于不同阶段 可以参考学习一下vim中的快捷键 提升工作效率 版本一 版本二 版本
  • P通道MOSFET简介

    A P Channel MOSFET is a type of MOSFET in which the channel of the MOSFET is composed of a majority of holes as current
  • Gcc编译优化等级介绍

    Gcc 编译优化简介 gcc 提供了为了满足用户不同程度的的优化需要 xff0c 提供了近百种优化选项 xff0c 用来对 编译时间 xff0c 目标文件长度 xff0c 执行效率 这个三维模型进行不同的取舍和平衡 优化的方法不一而足 xf
  • inline关键字的用法

    C代码可以在代码大小和执行时间两个方便优化 inline函数 gcc gnu org 这样描述 By declaring a function inline you can direct GCC to make calls to that
  • printf使用占位符控制输出格式

    printf 函数提供丰富的占位符参数以便精细地控制输出格式 xff0c 再进行字符操作的时候我们可能会使用到sprintf类函数进行处理 xff0c 因此这里对printf 函数的格式化输出控制进行较为 详细 讨论 简单的printf 语
  • 单片机main函数在中断函数里执行?

    引言 为什么复位中断服务程序里面直接调用的main函数 xff0c 难道所有程序都在复位中断里面执行的 xff1f 首先 xff0c Reset Handler 是单片机的一个中断 xff0c 其次 xff0c main 函数也确实被 Re
  • linux应用移植问题

    背景 公司设备降成本 xff0c 设备运行平台从armv7架构mpu换成了armv5架构的mpu xff0c 应用移植过程都挺顺利的 xff0c 只是牵涉到一个引用外部库的应用时 xff0c 运行该应用到引用库中的函数时 xff0c 应用抛
  • VSCode中格式化代码快捷键

    Shift 43 Alt 43 F
  • openwrt 时区设置无效问题

    最近在使用 openwrt 时发现在 web 控制台设置好时区后 xff0c 系统日志依然显示的是 UTC 时间 xff0c 慢了 8 小时 查了下原来是 openwrt 默认没有安装 zoneinfo xff0c 安装后即可 首先在 we
  • 修改weston桌面背景

    背景 linux开发板采用weston桌面系统 xff0c 希望修改默认桌面图片 xff1b 结果 在 etc xdg weston weston ini中新增 xff1a span class token punctuation span
  • 交叉编译构建GDB和GDBServer

    1 Problem statement I have a ARM GNU Linux board and I want to be able to debug programs running in it from the comfort