在 Linux 上的 gdb 中加载核心文件时,如何在库路径前面添加目录

发布于 2024-07-04 08:31:37 字数 311 浏览 6 评论 0原文

我在远程系统上生成了一个核心文件,但我无法直接访问该系统。 我还有来自远程系统的库文件的本地副本,以及崩溃程序的可执行文件。

我想在 gdb 中分析这个核心转储。

例如:

gdb path/to/executable path/to/corefile

我的库位于当前目录中。

过去我见过调试器通过提供选项“-p”来实现这一点。 或“-p /=。”; 所以我的问题是:

在 gdb 中分析 corefile 时,如何指定首先从相对于当前目录的路径加载库?

I have a core file generated on a remote system that I don't have direct access to. I also have local copies of the library files from the remote system, and the executable file for the crashing program.

I'd like to analyse this core dump in gdb.

For example:

gdb path/to/executable path/to/corefile

My libraries are in the current directory.

In the past I've seen debuggers implement this by supplying the option "-p ." or "-p /=."; so my question is:

How can I specify that libraries be loaded first from paths relative to my current directory when analysing a corefile in gdb?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清引 2024-07-11 08:31:37

启动 gdb,而不指定可执行文件或核心文件,然后键入以下命令:

set solib-absolute-prefix ./usr
file path/to/executable
core-file path/to/corefile

您需要确保从目标系统准确镜像库路径。 上面的内容适用于调试与您的主机不匹配的目标,这就是为什么复制包含库的根文件系统结构很重要。

如果您正在远程调试与主机具有相同架构和 Linux/glibc 版本的服务器,那么您可以执行 fd 建议:

set solib-search-path <path>

如果您尝试覆盖某些库,但不是全部,那么您可以将目标库目录结构复制到临时位置并使用上述的 solib-absolute-prefix 解决方案。

Start gdb without specifying the executable or core file, then type the following commands:

set solib-absolute-prefix ./usr
file path/to/executable
core-file path/to/corefile

You will need to make sure to mirror your library path exactly from the target system. The above is meant for debugging targets that don't match your host, that is why it's important to replicate your root filesystem structure containing your libraries.

If you are remote debugging a server that is the same architecture and Linux/glibc version as your host, then you can do as fd suggested:

set solib-search-path <path>

If you are trying to override some of the libraries, but not all then you can copy the target library directory structure into a temporary place and use the solib-absolute-prefix solution described above.

橘虞初梦 2024-07-11 08:31:37

我不确定这在 gdb 中是否可行,但我不是专家。

不过,我可以对 Linux 动态链接器发表评论。 以下内容应打印所有已解析共享库和未解析共享库的路径。

ldd path/to/executable

我们需要知道您的共享库是如何与可执行文件链接的。 为此,请使用以下命令:

readelf -d path/to/executable | grep RPATH
  • 如果命令不打印任何内容,动态链接器将使用标准位置加上 LD_LIBRARY_PATH 环境变量来查找共享库。

  • 如果命令打印一些行,动态链接器将忽略 LD_LIBRARY_PATH 并使用硬编码的 rpath。

    如果列出的 rpath 是绝对的,我知道的唯一解决方案是将您的库复制(或符号链接)到列出的位置。

    如果列出的 rpath 是相对的,它们将包含 $ORIGIN,该 $ORIGIN 将在运行时替换为可执行文件的路径。 移动可执行文件或库以匹配。

要了解更多信息,您可以从以下开始:

man ld.so

I'm not sure this is possible at all within gdb but then I'm no expert.

However I can comment on the Linux dynamic linker. The following should print the path of all resolved shared libraries and the unresolved ones.

ldd path/to/executable

We need to know how your shared libraries were linked with your executable. To do this, use the following command:

readelf -d path/to/executable | grep RPATH
  • Should the command print nothing, the dynamic linker will use standard locations plus the LD_LIBRARY_PATH environment variable to find the shared libraries.

  • If the command prints some lines, the dynamic linker will ignore LD_LIBRARY_PATH and use the hardcoded rpaths instead.

    If the listed rpaths are absolute, the only solution I know is to copy (or symlink) your libraries to the listed locations.

    If the listed rpaths are relative, they will contain a $ORIGIN which will be replaced at run time by the path of the executable. Move either the executable or the libraries to match.

For further informations, you could start with:

man ld.so
梦幻的心爱 2024-07-11 08:31:37

我在 developer.apple.com 上找到了这段摘录

设置solib-search-path路径 
  

如果设置了此变量,则路径是
以冒号分隔的目录列表
搜索共享库。
solib-search-path' 在之后使用
solib-absolute-prefix' 失败
找到该库,或者如果路径
图书馆是相对的而不是
绝对。 如果你想使用
solib-search-path' 而不是
solib-absolute-prefix',请务必
将 `solib-absolute-prefix' 设置为
不存在的目录以阻止GDB
通过查找主机的库。

编辑:

我不认为使用上述设置会在我添加的目录前面添加,但它似乎会附加它们,因此当前系统中缺少的文件会在我添加的路径中拾取。 我想将 solib-absolute-prefix 设置为虚假的东西并按照我需要的顺序在 solib-search-path 中添加目录可能是一个完整的解决方案。

I found this excerpt on developer.apple.com

set solib-search-path path

If this variable is set, path is a
colon-separated list of directories to
search for shared libraries.
solib-search-path' is used after
solib-absolute-prefix' fails to
locate the library, or if the path to
the library is relative instead of
absolute. If you want to use
solib-search-path' instead of
solib-absolute-prefix', be sure to
set `solib-absolute-prefix' to a
nonexistant directory to prevent GDB
from finding your host's libraries.

EDIT:

I don't think using the above setting prepends the directories I added, but it does seem to append them, so files missing from my current system are picked up in the paths I added. I guess setting the solib-absolute-prefix to something bogus and adding directories in the solib-search-path in the order I need might be a full solution.

黯淡〆 2024-07-11 08:31:37

您还可以在调用 gdb 时将 LD_PRELOAD 设置为每个库,或将 LD_LIBRARY_PATH 设置为当前目录。 仅当 gdb 本身尝试使用您预加载的任何库时,这才会导致问题。

You can also just set LD_PRELOAD to each of the libraries or LD_LIBRARY_PATH to the current directory when invoking gdb. This will only cause problems if gdb itself tries to use any of the libraries you're preloading.

倾`听者〃 2024-07-11 08:31:37

一个重要的注意事项:

如果您正在进行交叉编译并尝试使用 gdb 进行调试,那么
完成
file EECUTABLE_NAME后,如果您看到 smth. 就像:

Using host libthread_db library "/lib/libthread_db.so.1"

然后检查你的目标系统是否有 libthread_db 。 我在网上发现了很多类似的问题。 仅使用“set solib-”无法解决此类问题,您还必须使用交叉编译器构建libthread_db。

One important note:

if you're doing a cross compiling and trying to debug with gdb, then
after you've done
file ECECUTABLE_NAME if you see smth. like :

Using host libthread_db library "/lib/libthread_db.so.1"

then check whether you have libthread_db for your target system. I found a lot of similar problems on the web. Such problem cannot be solved just using "set solib-", you has to build libthread_db using your cross-compiler as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文