在gdb中加载python支持

2024-09-27 04:28:45 发布

您现在位置:Python中文网/ 问答频道 /正文

在gdb中使用python特定的命令时遇到了一个问题。我提供了对python的一般支持:

(gdb) python print(True)
True

我已经安装了标准脚本:

^{pr2}$

我确保加载已启用所有路径,我可以:

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


Tags: pathno命令addtrueshareautodb
1条回答
网友
1楼 · 发布于 2024-09-27 04:28:45

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

要找出原因,请启用auto-load调试:

(gdb) set debug auto-load

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

^{pr2}$

您应该会看到类似的输出:

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文件。在

相关问题 更多 >

    热门问题