加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

在gdb中加载python支持

发布时间:2020-12-20 13:14:52 所属栏目:Python 来源:网络整理
导读:我遇到了在gdb中使用特定于 python的命令的问题.我有一般的python支持包括: (gdb) python print(True)True 我已经安装了标准脚本: $ls /usr/share/gdb/python/gdb/command FrameDecorator.py FrameIterator.py frames.py function __init__.py printer pri
我遇到了在gdb中使用特定于 python的命令的问题.我有一般的python支持包括:

(gdb) python print(True)
True

我已经安装了标准脚本:

$ls /usr/share/gdb/python/gdb/
command  FrameDecorator.py  FrameIterator.py  frames.py  function  __init__.py  printer  printing.py  prompt.py  __pycache__  types.py  unwinder.py  xmethod.py

我确保使用所有路径启用加载:

$cat ~/.gdbinit 
add-auto-load-safe-path /usr/share/gdb/python/gdb/
add-auto-load-safe-path /usr/share/gdb/python/
add-auto-load-safe-path /usr/share/gdb/
set auto-load python-scripts on

但由于某种原因,gdb仍然不喜欢这样:

(gdb) info auto-load 
gdb-scripts:  No auto-load scripts.
guile-scripts:  No auto-load scripts.
libthread-db:  No auto-loaded libthread-db.
local-gdbinit:  Local .gdbinit file was not found.
python-scripts:  No auto-load scripts.

我想在加载gdb后让py-bt命令工作.

解决方法

py-bt和相关命令通常在GDB脚本文件python * -gdb.py中定义,该文件往往存在于/usr/share / gdb / auto-load /usr/bin /中.如果在调试Python对象文件时GDB中没有这些命令,则表示包含它们的脚本未自动加载.

要找出原因,请启用自动加载调试:

(gdb) set debug auto-load

并尝试加载Python可执行文件:

(gdb) file python3

您应该看到与此类似的输出:

Reading symbols from python3...Reading symbols from /usr/lib/debug/.build-id/58/bce0c98a07039868053ed4b27e79959caadb9d.debug...done.
auto-load: Attempted file "/usr/lib/debug/.build-id/58/bce0c98a07039868053ed4b27e79959caadb9d.debug-gdb.gdb" does not exist.
auto-load: Expanded $-variables to "/usr/lib/debug:/usr/share/gdb/auto-load".
auto-load: Searching 'set auto-load scripts-directory' path "$debugdir:$datadir/auto-load".
auto-load: Attempted file "/usr/lib/debug/usr/lib/debug/.build-id/58/bce0c98a07039868053ed4b27e79959caadb9d.debug-gdb.gdb" does not exist.
auto-load: Attempted file "/usr/share/gdb/auto-load/usr/lib/debug/.build-id/58/bce0c98a07039868053ed4b27e79959caadb9d.debug-gdb.gdb" does not exist.
auto-load: Attempted file "/usr/lib/debug/.build-id/58/bce0c98a07039868053ed4b27e79959caadb9d.debug-gdb.py" does not exist.
auto-load: Expanded $-variables to "/usr/lib/debug:/usr/share/gdb/auto-load".
auto-load: Searching 'set auto-load scripts-directory' path "$debugdir:$datadir/auto-load".
auto-load: Attempted file "/usr/lib/debug/usr/lib/debug/.build-id/58/bce0c98a07039868053ed4b27e79959caadb9d.debug-gdb.py" does not exist.
auto-load: Attempted file "/usr/share/gdb/auto-load/usr/lib/debug/.build-id/58/bce0c98a07039868053ed4b27e79959caadb9d.debug-gdb.py" does not exist.
done.
auto-load: Attempted file "/usr/bin/python3.6-gdb.gdb" does not exist.
auto-load: Expanded $-variables to "/usr/lib/debug:/usr/share/gdb/auto-load".
auto-load: Searching 'set auto-load scripts-directory' path "$debugdir:$datadir/auto-load".
auto-load: Attempted file "/usr/lib/debug/usr/bin/python3.6-gdb.gdb" does not exist.
auto-load: Attempted file "/usr/share/gdb/auto-load/usr/bin/python3.6-gdb.gdb" does not exist.
auto-load: Attempted file "/usr/bin/python3.6-gdb.py" does not exist.
auto-load: Expanded $-variables to "/usr/lib/debug:/usr/share/gdb/auto-load".
auto-load: Searching 'set auto-load scripts-directory' path "$debugdir:$datadir/auto-load".
auto-load: Attempted file "/usr/lib/debug/usr/bin/python3.6-gdb.py" does not exist.
auto-load: Attempted file "/usr/share/gdb/auto-load/usr/bin/python3.6-gdb.py" exists.
auto-load: Loading python script "/usr/share/gdb/auto-load/usr/bin/python3.6-gdb.py" by extension for objfile "/usr/bin/python3.6".
auto-load: Matching file "/usr/share/gdb/auto-load/usr/bin/python3.6-gdb.py" to pattern "/usr/lib/debug"
auto-load: Not matched - pattern "/usr/lib/debug".
auto-load: Matching file "/usr/share/gdb/auto-load/usr/bin/python3.6-gdb.py" to pattern "/usr/share/gdb/auto-load"
auto-load: Matched - file "/usr/share/gdb/auto-load" to pattern "/usr/share/gdb/auto-load".
auto-load: File "/usr/share/gdb/auto-load/usr/bin/python3.6-gdb.py" matches directory "/usr/share/gdb/auto-load".

GDB用于查找要自动加载的脚本的规则如下所述:

https://sourceware.org/gdb/current/onlinedocs/gdb/Auto_002dloading-extensions.html

在简化的形式中,它在遵循符号链接后获取加载的目标文件的绝对文件路径,向其附加-gdb.(gdb | py | scm)扩展名并尝试查找其路径以其中一个开头的文件开头的文件.加载脚本 – 目录目录,并以构造的脚本名称结束.

如果找不到这样的脚本,请检查系统上实际可用的脚本.您可能正在尝试调试,例如一个python3可执行文件,它是python3.6的符号链接,而你的系统的GDB安装可能只提供python3.5可执行文件的GDB脚本.由于脚本名称的构造方式,可执行文件的名称实际上很重要.

另外,检查GDB是否使用了相应的脚本目录:

(gdb) show auto-load scripts-directory
List of directories from which to load auto-loaded scripts is $debugdir:$datadir/auto-load.

并在必要时更新它们.加载启用了自动加载调试的目标文件时,请执行以下操作:

auto-load: Expanded $-variables to "/usr/lib/debug:/usr/share/gdb/auto-load".

显示scripts-directory的扩展值.

最后,检查自动加载安全路径的值:

(gdb) show auto-load safe-path
List of directories from which it is safe to auto-load files is $debugdir:$datadir/auto-load.

并在必要时进行更新.

顺便说一下,你的?/ .gdbinit文件中可能不需要任何额外的add-auto-load-scripts-directory或add-auto-load-safe-path命令,因为默认的自动加载脚本目录和安全路径通常是包括$datadir / auto-load,它通常扩展到/usr/share / gdb / auto-load,这通常是系统范围的GDB脚本的默认位置,其中usr / bin / python * -gdb.py文件最多可能会被发现.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读