在gdb中加载python支持

vir*_*tor 5 python linux gdb archlinux

我在gdb中使用特定于python的命令时遇到了问题。我已经包括了一般的python支持:

(gdb) python print(True)
True
Run Code Online (Sandbox Code Playgroud)

我已经安装了标准脚本:

$ 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
Run Code Online (Sandbox Code Playgroud)

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

$ 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
Run Code Online (Sandbox Code Playgroud)

但是由于某种原因,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.
Run Code Online (Sandbox Code Playgroud)

我想py-bt在加载后使命令正常工作gdb。

Pet*_*sta 7

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

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

(gdb) set debug auto-load
Run Code Online (Sandbox Code Playgroud)

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

(gdb) file python3
Run Code Online (Sandbox Code Playgroud)

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

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".
Run Code Online (Sandbox Code Playgroud)

GDB 用于查找脚本的规则auto-load描述如下:

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

在简化的形式中,它在跟随符号链接后获取加载的目标文件的绝对文件路径,为其附加-gdb.(gdb|py|scm)扩展名,并尝试查找其路径以其中一个auto-load scripts-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.
Run Code Online (Sandbox Code Playgroud)

并在必要时更新它们。在auto-load启用调试的情况下加载目标文件时,以下行:

auto-load: Expanded $-variables to "/usr/lib/debug:/usr/share/gdb/auto-load".
Run Code Online (Sandbox Code Playgroud)

显示 的扩展值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.
Run Code Online (Sandbox Code Playgroud)

并在必要时更新它。

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