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.

相关推荐
databook3 分钟前
告别盲人摸象,数据分析的抽样方法总结
后端·python·数据分析
序属秋秋秋5 分钟前
《Linux系统编程之进程环境》【环境变量】
linux·运维·服务器·c语言·c++·操作系统·系统编程
全栈陈序员20 分钟前
【Python】基础语法入门(九)—— 代码规范、调试技巧与性能初探
开发语言·python·代码规范
nvd1123 分钟前
解决 Gemini API 连接卡住问题的方案
python
李剑一31 分钟前
Python学习笔记2
python
云计算练习生1 小时前
linux shell编程实战 10 Git工具详解与运维场景实战
linux·运维·git
晨非辰1 小时前
C++ 波澜壮阔 40 年:从基础I/O到函数重载与引用的完整构建
运维·c++·人工智能·后端·python·深度学习·c++40周年
有梦想的西瓜1 小时前
如何优化电力系统潮流分布:最优潮流(OPF)问题
python·电力·opf
虚伪的空想家3 小时前
KVM的ubuntu虚机如何关闭安全启动
linux·安全·ubuntu
DanCheng-studio7 小时前
网安毕业设计简单的方向答疑
python·毕业设计·毕设