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.

相关推荐
@forever@7 小时前
【JAVA】LinkedList与链表
java·python·链表
安审若无7 小时前
图数据库neoj4安装部署使用
linux·运维·数据库
程序员爱钓鱼7 小时前
Python编程实战:面向对象与进阶语法——类型注解与代码规范(PEP 8)
后端·python·ipython
程序员爱钓鱼7 小时前
Python实战:用高德地图API批量获取地址所属街道并写回Excel
后端·python·ipython
做运维的阿瑞7 小时前
CentOS DNS故障排查完整解决方案:从症状到根因的系统化诊断
linux·运维·centos
QT 小鲜肉8 小时前
【个人成长笔记】在 Linux 系统下撰写老化测试脚本以实现自动压测效果(亲测有效)
linux·开发语言·笔记·单片机·压力测试
itachi-uchiha8 小时前
head和tail命令使用
linux·运维·服务器
reasonsummer8 小时前
【教学类-97-06】20251105“葡萄”橡皮泥黏贴(小班主题《苹果与橘子》)
python
馨谙9 小时前
SELinux 故障排除完全指南:从拒绝访问到快速修复
linux
卖个几把萌9 小时前
【16】Selenium+Python 接管已打开谷歌浏览器
python·selenium·测试工具