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.

相关推荐
言乐67 小时前
Python基于关键词分拣快递(适用于电商与货运代理等)
开发语言·python·django·virtualenv·pygame
代码方舟7 小时前
Python数据工程:利用天远名下车辆车牌查询A优化个人债务重组与清偿能力评估合规体验
人工智能·python
卷无止境7 小时前
当AI写代码遇见Rust为什么会卡壳
后端·python
栈溢出了7 小时前
LangGraph State、节点与动态路由学习笔记
python
用户019027581617 小时前
A 股代码后缀 .SH/.SZ/.BJ 怎么区分?Python 怎么统一处理沪深京港美股代码?
python
迅猛龙办公室8 小时前
python实现简单进度条
java·前端·python
郝学胜_神的一滴8 小时前
AI 编程智能体 06:用Anaconda搞定Python多环境,彻底告别版本兼容灾难
人工智能·python
清水白石0088 小时前
Python 对象模型深度解析:从“一切皆对象”到 id、type、isinstance 底层机制与小整数缓存原理
开发语言·python·缓存
墨家句子8 小时前
4G 显存老显卡怎么跑模型:llama.cpp 加 GGUF 配置
linux·后端
for_ever_love__8 小时前
Python 进阶——迭代器、生成器、装饰器与 asyncio
python·装饰器·生成器·大模型开发