0%

搭建gdb python 脚本开发环境

厂商或者系统继承的交叉工具链中的 gdb 一般不支持 python.
或者即使支持python, 由于路径问题, 很难与当前系统中使用的python 配合. 如

  • 出现python 第三方库不兼容问题等
  • 缺少第三方库, 使用系统下载的第三方库不能嵌入到 gdb 中使用等问题.

因此, 我们在需要定制gdb脚本时往往需要自己动手编译交叉工具链的gdb.

下载编译gdb源码

https://ftp.gnu.org/gnu/gdb/gdb-9.2.tar.xz

1
2
3
4
5
6
7
8
9
10
11
tar gdb-9.2.tar.xz
cd gdb-9.2
mkdir build
../configure --target=mips-mti-elf --prefix=/home/mi/program/mips-gdb \
--enable-unicode=ucs4 \
--with-python=/usr \
--with-auto-load-dir=$debugdir:$datadir/auto-load \
--with-auto-load-safe-path=$debugdir:$datadir/auto-load \
--with-expat --without-libunwind-ia64 --without-lzma --without-babeltrace --without-intel-pt --disable-libmcheck --without-mpfr --without-guile
make
make install

这里注意的地方, target prefix 参数需要自己填

  1. target 这个该怎么填, 可以用这个简单方法, 用系统继承或厂商提供的gdb, 执行下, 在gdb环境下执行show configuration, 看target是什么, 这里就填什么
  2. prefix, 填编译gdb完成后安装到的位置, 添加到环境变量中
  3. with-python 选项是查找 python 的库的位置, 简单来说就是你想定制的 gdb 用的 python 环境是哪个 python 环境, 这里就填哪个目录, ubuntu 等这类 linux 一般会在/usr 下默认配置好 python2, 你如果想使用自己定制的 python, 如使用 pyenvconda``virtualenv 等定制的 python, 则需要填响应的 python 所在的目录. 后面在 gdb 环境下使用 python 报缺少三方库, 则需要用对应环境的 pip 安装对应的库/组件.

使用 gdb 定制 python 脚本

使用上述步骤编译安装的gdb即可, 再进行gdb 环境下定制脚本的开发