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.

相关推荐
ydl112837 分钟前
机器学习基础知识【 激活函数、损失函数、优化器、 正则化、调度器、指标函数】
python·机器学习
程序员黄老师1 小时前
Ubuntu 24.04上安装 Intelligent Pinyin 中文输入法
linux·运维·ubuntu
HONG_YANG1 小时前
ERPNext 介绍、安装和维护建议
linux
HONG_YANG1 小时前
ERPNext 搭建教程:Linux 一键部署与维护
linux
chao_7891 小时前
CSS表达式——下篇【selenium】
css·python·selenium·算法
Enti7c2 小时前
什么是void,什么时候使用void类型?never和void的区别
linux·运维·ubuntu
倔强青铜三2 小时前
苦练Python第10天:for 循环与 range() 函数
人工智能·python·面试
5172 小时前
django中如何使用Django REST Framework
后端·python·django
程序员的世界你不懂2 小时前
(20)Java+Playwright自动化测试- 操作鼠标拖拽 - 上篇
java·python·计算机外设
心 一2 小时前
Python 类型注解实战:`Optional` 与安全数据处理的艺术
服务器·python·安全