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.

相关推荐
小北方城市网4 分钟前
RabbitMQ 生产级实战:可靠性投递、高并发优化与问题排查
开发语言·分布式·python·缓存·性能优化·rabbitmq·ruby
曲幽6 分钟前
FastAPI部署中间件实战:从CORS到自定义,让你的API更健壮
python·fastapi·web·cors·starlette·middleware·call_next
郝学胜-神的一滴10 分钟前
Python中的bisect模块:优雅处理有序序列的艺术
开发语言·数据结构·python·程序人生·算法
JiMoKuangXiangQu11 分钟前
Linux 内存:kasan 监测访问越界案例
linux·kasan
怣5012 分钟前
Linux创意命令组合:让终端变得有趣又高效
linux·运维·服务器
L_090717 分钟前
【Linux】进程状态
linux·开发语言·c++
jackylzh18 分钟前
PyTorch 2.x 中 `torch.load` 的 `FutureWarning` 与 `weights_only=False` 参数分析
人工智能·pytorch·python
小生不才yz21 分钟前
shell编程 - 数据流指南
linux
lisanmengmeng26 分钟前
添加ceph节点
linux·服务器·ceph
MACKEI28 分钟前
服务器流式传输接口问题排查与解决方案
python·nginx·流式