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.

相关推荐
REDcker1 分钟前
Linux Core Dump 配置与分析指南
linux·运维·服务器
纤纡.8 分钟前
Python 实战:基于朴素贝叶斯的苏宁易购评价情感分析
开发语言·python·机器学习
前端付豪11 分钟前
AI Tutor v5:自动出卷系统
前端·python·llm
IMPYLH12 分钟前
Linux 的 chcon 命令
linux·运维·服务器
叶子20242215 分钟前
韧性,任性
python
阿贵---16 分钟前
使用Fabric自动化你的部署流程
jvm·数据库·python
前端付豪17 分钟前
AI Tutor v4:学习路径推荐(Learning Path)
前端·python·llm
苦逼IT运维27 分钟前
SVN 仓库目录迁移,仓库 “降级” 成子目录实战
linux·运维·ci/cd·svn·运维开发
阿拉斯攀登38 分钟前
第 13 篇 输入设备驱动(触摸屏 / 按键)开发详解,Linux input 子系统全解析
android·linux·运维·驱动开发·rk3568·瑞芯微·rk安卓驱动
2401_8732046539 分钟前
使用Scrapy框架构建分布式爬虫
jvm·数据库·python