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.

相关推荐
EXI-小洲几秒前
2025年度总结 EXI-小洲:技术与生活两手抓
java·python·生活·年度总结·ai开发
Lupino3 分钟前
从隔离到原生:利用 Monty 构建 AI 原生的 IoT 安全沙箱
python
bepeater123414 分钟前
Linux安装Redis以及Redis三种启动方式
linux·redis·bootstrap
njtong24 分钟前
Ubuntu系统apache2网站的Let‘s Encrypt证书问题
linux·运维·ubuntu
aHais_25 分钟前
PWM子系统杂项笔记
linux
~央千澈~33 分钟前
抖音弹幕游戏开发之第9集:pyautogui进阶 - 模拟鼠标操作·优雅草云桧·卓伊凡
开发语言·python·游戏
量化分析36 分钟前
腾讯云迁移上云功能
linux·云计算·腾讯云
啊阿狸不会拉杆37 分钟前
《机器学习导论》第 19 章 - 机器学习实验的设计与分析
人工智能·python·算法·决策树·机器学习·统计检验·评估方法
张3蜂44 分钟前
python知识点点亮
开发语言·python
好学且牛逼的马1 小时前
【Hot100|26-LeetCode 21. 合并两个有序链表 - 完整解法详解】
开发语言·python