读取ELF文件DWARF调试信息的库

发布于 2024-07-26 06:37:02 字数 75 浏览 5 评论 0 原文

有没有推荐一个好的跨平台库来读取 DWARF 格式的 ELF 文件调试信息? 我想读取 Python 程序中的 DWARF 调试信息。

Any recommendations for a good cross-platform library for reading ELF file debug information in DWARF format? I'd like to read the DWARF debug info in a Python program.

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

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

发布评论

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

评论(4

箜明 2024-08-02 06:37:02

有一个新的东西 - pyelftools - 一个用于解析 ELF 和 DWARF 格式的纯 Python 库。 试一试。

它的目标是功能完整,目前正在积极开发中,因此任何问题都应该快速而热情地处理:-)

There's a new kid on the block - pyelftools - a pure Python library for parsing the ELF and DWARF formats. Give it a try.

It aims to be feature-complete and is currently in active development, so any problems should be handled quickly and enthusiastically :-)

平安喜乐 2024-08-02 06:37:02

“ELF 调试信息”的概念实际上并不存在:ELF 规范故意未指定 .debug 部分的内容。

常见的调试格式是 STAB 和 DWARF。 读取 DWARF 的库是 libdwarf

The concept of "ELF debug info" doesn't really exist: the ELF specification leaves the content of the .debug section deliberately unspecified.

Common debug formats are STAB and DWARF. A library to read DWARF is libdwarf.

未央 2024-08-02 06:37:02

您可能对 pydevtools 中的 DWARF 库感兴趣:

>>> from devtools.dwarf import DWARF
>>> dwarf = DWARF('test/test')
>>> dwarf.get_loc_by_addr(0x8048475)
('/home/emilmont/Workspace/dbg/test/main.c', 36, 0)
>>> print dwarf
.debug_info
COMPILE_UNIT<header overall offset = 0>
<0><11> compile_unit
producer: GNU C 4.4.3
language: C89
name: a/test.c
comp_dir: /home/emilmont/Workspace/dbg/test
low_pc: 0x080483e4
high_pc: 0x08048410
stmt_list: 0
[...]

You might be interested in the DWARF library from pydevtools:

>>> from devtools.dwarf import DWARF
>>> dwarf = DWARF('test/test')
>>> dwarf.get_loc_by_addr(0x8048475)
('/home/emilmont/Workspace/dbg/test/main.c', 36, 0)
>>> print dwarf
.debug_info
COMPILE_UNIT<header overall offset = 0>
<0><11> compile_unit
producer: GNU C 4.4.3
language: C89
name: a/test.c
comp_dir: /home/emilmont/Workspace/dbg/test
low_pc: 0x080483e4
high_pc: 0x08048410
stmt_list: 0
[...]
郁金香雨 2024-08-02 06:37:02

遗憾的是,您读取 DWARF 调试信息的选项非常有限。

据我所知,只有一个用于解析 DWARF 调试信息的通用库,那就是 libdwarf. 不幸的是,没有人为 libdwarf 编写 Python 绑定(也许您可以自己承担并与其他人分享:))您当然可以尝试使用 ctypesPython C API

然而,一个不太优雅的解决方案是使用现有的 DWARF 解析器并解析它输出的文本信息。 您对此的选择(在 Linux 上)是

objdump -W
readelf --debug-dump=[OPTIONS]

我目前使用一个基于 readelf 构建的项目,它对 DWARF 调试信息的支持功能非常齐全。 您可以简单地使用 Python 在 shell 中执行任一命令,然后根据需要解析信息。 当然不如图书馆那么理想,但应该可以解决问题。

编辑:我注意到您在之前的评论中提到了 Windows。 这两个程序(objdump 和 readelf)都是 GNU-binutils 的一部分,因此它们应该可以在 Cygwin 或 mingw 中使用。

Your options for reading the DWARF debugging information are unfortunately quite limited.

As far as I know there is only one general purpose library for parsing DWARF debugging information and that is libdwarf. Unfortunately no one has written Python bindings for libdwarf (maybe you could take it up upon yourself and share it with everyone else :) ) You could certainly attempt to access the library's functions using ctypes or the Python C API.

A much less elegant solution, however, is to use an existing DWARF parser and parse the textual information it outputs. Your options for this (on Linux) are

objdump -W
readelf --debug-dump=[OPTIONS]

I currently use a project that builds off of readelf and it's support for the DWARF debugging information is very full featured. You could simply use Python to execute either command in the shell and then parse the information as you need. Certainly not as ideal as a library, but should do the trick.

EDIT: I noticed in a previous comment you mentioned Windows. Both of these programs(objdump and readelf) are part of GNU-binutils, so they should be available with Cygwin or mingw.

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