gdbinit、美化打印和相对目录?

4 投票
2 回答
2354 浏览
提问于 2025-04-16 22:46

在我的 ~/.gdbinit 文件中,我想通过相对路径来引用安装在我家目录下的 GDB python 美化打印工具:

python
import sys
# 1, works
sys.path.insert(0, '/home/<username>/.gdb_viz')
# 2, doesn't work
# sys.path.insert(0, '~/.gdb_viz')
# 3, doesn't work
# sys.path.insert(0, '.gdb_viz')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end 

~/.gdb_viz 文件夹里有一个 libstdcxx 目录和相关的 python 文件。

我该怎么做才能让像 #2 或 #3 这样的方式工作呢?

编辑:除了每次都从我的家目录启动 gdb 之外 :)

常见的 GDB 信息/输出:

GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...

"正常工作" 的输出:

Reading symbols from <executable>...done.

"不工作" 的输出:

Traceback (most recent call last):
  File "<string>", line 3, in <module>
ImportError: No module named libstdcxx.v6.printers
/home/<username>/.gdbinit:6: Error in sourced command file:
Error while executing Python code.
Reading symbols from <executable>...done.

2 个回答

1

我把这个放在 path_setup.py 文件里,然后运行命令 source ~/.gdb_viz/path_setup.py,这个命令是从我的 .gdbinit 文件中执行的。我还没测试过第 3 个。

# Add the path of this script to pythons search path. 
import os, sys

sys.path.append(os.path.expanduser(os.path.dirname(__file__)))
4

这个代码没有经过测试,但应该可以正常运行:

python
import sys, os

sys.path.insert(0, os.getenv('HOME') + '/.gdb_viz')

撰写回答