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.

相关推荐
用户8356290780519 分钟前
使用 Python 对 PDF 文档进行数字签名的方法
后端·python
李可以量化40 分钟前
PTrade 量化入门必学:get\_trading\_day 交易日函数全解与实战(下)
python
柒和远方41 分钟前
LeetCode 139. 单词拆分 —— 从暴力回溯到 DP 完全背包
javascript·python·算法
半兽先生1 小时前
大模型实现金融文本分类
人工智能·python·金融
汤姆小白1 小时前
06-LoRA参数高效微调
人工智能·python·机器学习·transformer
铅笔侠_小龙虾2 小时前
Rust 学习(6)-所有权规则、移动语义、Clone 与 Copy
python·学习·rust
码云骑士2 小时前
65-AI-Agent基础原理-AutoGPT-四大架构全景解读
人工智能·python·架构
Ulyanov2 小时前
刚体动力学方程——牛顿-欧拉与陀螺效应的奥秘
开发语言·python·目标跟踪·雷达电子对抗·导引头
观察员2 小时前
用 Streamlit + 智谱 AI 打造虚拟伴侣聊天机器人
python
小樱花的樱花3 小时前
Linux 多线程编程:互斥锁(Mutex)详解
linux·c语言·开发语言