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.

相关推荐
爱编程的小金毛球球13 分钟前
-bash: /home/xxx/anaconda3/bin/conda: No such file or directory
linux·conda·bash
denghai邓海21 分钟前
红黑树删除之向上调整
python·b+树
封步宇AIGC1 小时前
量化交易系统开发-实时行情自动化交易-3.4.1.2.A股交易数据
人工智能·python·机器学习·数据挖掘
何曾参静谧1 小时前
「Py」Python基础篇 之 Python都可以做哪些自动化?
开发语言·python·自动化
Prejudices1 小时前
C++如何调用Python脚本
开发语言·c++·python
我狠狠地刷刷刷刷刷1 小时前
中文分词模拟器
开发语言·python·算法
Jam-Young1 小时前
Python的装饰器
开发语言·python
Mr.咕咕2 小时前
Django 搭建数据管理web——商品管理
前端·python·django
Biomamba生信基地2 小时前
Linux也有百度云喔~
linux·运维·服务器·百度云
AnFany2 小时前
LeetCode【0028】找出字符串中第一个匹配项的下标
python·算法·leetcode·字符串·kmp·字符串匹配