御风灰灰
发布于 2024-07-11 / 12 阅读
0
0

gdb工具编译

1.准备工作

1.1 下载GDB源码

GDB源码仓库

下载需要的的GDB版本,这里以gdb-14.2为例

wget https://ftp.gnu.org/gnu/gdb/gdb-14.2.tar.gz

1.2 下载需要的依赖库

依赖库的安装可参考 mingw库安装

1.2.1 gmp库下载

必须库,没有无法进行编译配置

![linux-gdbtool-build-libexpat异常.png](/upload/linux-gdbtool-build-libexpat异常.png)


wget  https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/mingw64/mingw-w64-i686-gmp-6.3.0-2-any.pkg.tar.zst

1.2.2 mpfr

必须库,没有无法进行编译配置

wget  https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/mingw64/mingw-w64-i686-mpfr-4.2.1-2-any.pkg.tar.zst

1.2.3 libexpat(xml库)

非常重要的库,可以解析gdbserver发送的xml并匹配架构,没有会报各种错误

Remote debugging using localhost:3333
Truncated register 16 in remote 'g' packet
wget  https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/mingw64/mingw-w64-x86_64-expat-2.6.1-2-any.pkg.tar.zst

1.2.4 python

可选库,可以扩展gdb的功能,作为gdb插件

wget https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/mingw64/mingw-w64-x86_64-python3.11-3.11.4-1-any.pkg.tar.zst

2. 开始编译

2.1 libexpat库问题解决

经实测发现libexpat链接到gdb时会编译出错, 因为libexpat库开启了堆栈调试,导致无法链接。需要下载源码进行编译

  1. 下载源代码
wget https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/sources/mingw-w64-expat-2.6.1-2.src.tar.zst
  1. 解压后配置编译

可参考压缩包内的PKGBUILD进行构建

mkdir build && cd build && ../configure \
    --prefix=/mingw64 \
    --host=x86_64-w64-mingw32 \
    --build=x86_64-w64-mingw32 \
    --enable-shared \
    --enable-static && make -j60 && make install

2.2 python支持

因为编译平台 build!=host,因此不能本地执行python指令获取配置参数,需要一个脚本实现

脚本主要根据参数输出 头文件,链接库等信息。参考文件pythonbuild.sh如下:

#!/bin/bash

if [ $2 == "--ldflags" ]
then
    echo "/mingw64/lib/libpython3.9.dll.a"
fi

if [ $2 == "--includes" ]
then
    echo "-I/mingw64/include/python3.9"
fi

2.3 构建gdb

  1. 解压源代码
tar -xvf gdb-14.2.tar.gz
  1. 编写构建脚本 buildconfig.sh

安装位置根据自己需要进行选择

MINGW_LIBS=/mingw64
CROSS_COMPILE=x86_64-w64-mingw32

OUTPUT=/home/dev/orin/libs/gdb-14.2/output
PYTHON_EXEC=/home/dev/orin/libs/gdb-14.2/pythonbuild.sh

../configure --host=$CROSS_COMPILE --target=arm-none-eabi \
    --program-prefix=arm-none-eabi- --enable-static --with-static-standard-libraries \
    --without-babeltrace --without-intel-pt --without-xxhash --without-debuginfod --without-libunwind-ia64 \
    --with-python=$PYTHON_EXEC \
    --with-libexpat-type=static --with-libexpat-prefix=$MINGW_LIBS \
    --with-libmpfr-type=static --with-mpfr=$MINGW_LIBS \
    --with-libgmp-type=static --with-gmp=$MINGW_LIBS \
    --prefix=$OUTPUT

make -j60 V=s && make install && $CROSS_COMPILE-strip $OUTPUT/bin/arm-none-eabi-gdb.exe
  1. 进入源码编译
cd gdb-14.2 && mkdir build && cd build && ../buildconfig.sh

3. 编译问题处理

2.1 无libexpat导致的错误

gdb链接时提示无法解析目标xml, 然后报Truncated register 16 in remote 'g' packet错误。

** 需要合入libexpat库**

2.2 libexpat提示找不到

看build的log可知,是libexpat的堆栈符号无法链接。

linux-gdbtool-build-libexpat异常-bicf.png

** 重新编译libexpat库进行安装 **


评论