linux 在gdb中加载python支持

yjghlzjz  于 2023-03-01  发布在  Linux
关注(0)|答案(3)|浏览(192)

我在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) 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命令正常工作。

yks3o0rb

yks3o0rb1#

py-bt和相关命令通常在GDB脚本文件python*-gdb.py中定义,而python*-gdb.py通常出现在/usr/share/gdb/auto-load/usr/bin/中。如果在调试Python对象文件时这些命令在GDB中不可用,则意味着包含它们的脚本没有自动加载。
要找出原因,请启用auto-load调试:

(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查找auto-load脚本的规则如下所述:
https://sourceware.org/gdb/current/onlinedocs/gdb/Auto_002dloading-extensions.html
在简化的形式中,它在遵循符号链接之后获取加载的目标文件的绝对文件路径,向其附加-gdb.(gdb|py|scm)扩展名,并尝试查找路径以auto-loadscripts-directory目录之一开始并以构造的脚本名称结束的文件。
如果找不到这样的脚本,检查哪些脚本在您的系统上是可用的。可能您正在调试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调试的目标文件时,以下行:

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

显示scripts-directory的展开值。
最后,检查auto-load安全路径的值:

(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-directoryadd-auto-load-safe-path命令,因为默认的auto-load脚本目录和安全路径通常包括$datadir/auto-load,它通常扩展到/usr/share/gdb/auto-load,这通常是系统范围的GDB脚本的默认位置,在那里最有可能找到usr/bin/python*-gdb.py文件。

drkbr07n

drkbr07n2#

如果自动加载失败,请使用source命令显式加载相关脚本。例如:

sudo gdb python
source /usr/share/gdb/auto-load/usr/bin/python3.9-dbg-gdb.py
attach 123
pt-bt
9nvpjoqh

9nvpjoqh3#

在Debian 11上也有同样的问题。使用

(gdb) set debug auto-load
(gdb) file python3

我发现gdb试图在/usr/share/gdb/auto-load/usr/bin中查找python3.9d-gdb.py,但实际文件名是python3.9m-gdb.py。修复方法是添加另一个链接:

cd /usr/share/gdb/auto-load/usr/bin
sudo ln -s python3.9m-gdb.py python3.9d-gdb.py

相关问题