GDB 使用python

Please read the user spec Debugging with GDB (Tenth Edition, for GDB version 13.0.50.20221115-git)chapter 23.3 Extending GDB using Python.

Please notice that: if the GDB version which you are using is old (maybe version 6 or older), maybe it does not support python. Update your GDB.

1. Check PYTHONDIR

复制代码
(gdb) python
> print(gdb.PYTHONDIR)
> end
/usr/bin/share/gdb/python

By the command print(gdb.PYTHONDIR) , we can get the directory of python that GDB are using.

2. Config ~/.gdbinit

复制代码
python
import sys
sys.path.insert(0, "/usr/share/gcc-8/python")
from libstdcxx.v6 import register_libstdcxx_printers
register_libstdcxx_printers(None)
end

Insert python path and register the pretty printers for printing c++ containers.

3. Example

复制代码
(gdb) python
>print(gdb.parse_and_eval("1+1"))
>end
2

You can parse and evalute the value of expression by using command gdb.parse_and_eval("expression")). The "expression" can be a pointer or an object in c++ context, and python wil evalute the value automatically. The feature is very useful, and you can wrap a function in ~/.gdbinit in order to impove debugging experience.

At the end, Suggest you oftern read GDB user spec.

相关推荐
七夜zippoe4 分钟前
Python网络编程实战:从TCP/IP到WebSocket的协议演进与核心技术解析
网络·python·websocket·tcp/ip·socket·心跳机制
jj008u4 分钟前
Garmin 中国区活动同步到国际区的一个简单实现方案
python·ai编程
济6176 分钟前
linux 系统移植(第十五期)---Linux 内核移植(4)-- 修改 EMMC 驱动--- Ubuntu20.04
linux·嵌入式硬件
礼拜天没时间.10 分钟前
《Docker实战入门与部署指南:从核心概念到网络与数据管理》:初识Docker——概念与优势
linux·运维·网络·docker·容器·centos
阿钱真强道11 分钟前
05 thingsboard-4.3-ubuntu20-rk3588-部署
linux·运维·服务器·鸿蒙
夜颂春秋13 分钟前
重温Linux命令
linux·运维·职场和发展·单元测试
weixin_4624462314 分钟前
Hive 4.0.1 自动安装脚本详解:一键部署 + 环境变量配置(适用于 Linux)
linux·hive·hadoop
Sheep Shaun14 分钟前
Linux 环境搭建终极指南(Ubuntu 版):2026 最新版,适配 Ubuntu 24.04 LTS
linux·运维·服务器·ubuntu
GG向前冲19 分钟前
【Python 金融量化】线性模型在AAPL股票数据的分析研究
大数据·python·机器学习·ai·金融
程序猿202320 分钟前
Java Thread
java·开发语言·python