26

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.

Craig McQueen
  • 41,871
  • 30
  • 130
  • 181
  • You might find useful informations here: - [David A's DWARF Page](http://reality.sgiweb.org/davea/dwarf.html) - [SO question](https://stackoverflow.com/questions/45954/python-libdwarf-module) – Nick Dandoulakis Jul 09 '09 at 01:12

4 Answers4

30

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 :-)

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
12

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.

newbie
  • 1,230
  • 1
  • 12
  • 21
Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
  • 1
    Is libdwarf cross-platform, do you know? The page doesn't say, but seems to have an overall Unix flavour to it. – Craig McQueen Jul 14 '09 at 07:59
  • I haven't tried porting it. It seems that it has some Unix specificisms in it (e.g. in the darfdump executable); it's also based on libelf. However, porting it to a different system should be straight-forward. – Martin v. Löwis Jul 14 '09 at 12:11
  • I've done some minor work with libdwarf. It's dependencies outside of the C standard library is libelf, which is currently available for Windows. It should be a fairly (for some definition of "fairly") easy task to compile it for Windows in Cygwin. – Falaina Jul 14 '09 at 23:13
  • 3
    I successfully ported libdwarf to compile on windows with visual studio 2008. – Torleif Sep 23 '09 at 06:00
  • @Torleif hi, since you've ported libdwarf successfully can you take a look at my post and give me some pointers? http://stackoverflow.com/questions/3581931/trying-to-compile-libdwarf-gives-undefined-errors – greatwolf Aug 28 '10 at 01:28
  • 1
    @Torleif how about sharing your ported version? That would help a lot! – buc030 Mar 11 '14 at 13:25
7

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
[...]
emilmont
  • 783
  • 8
  • 5
  • That's great to know. Couple of questions: 1) What platforms are supported (Windows, Linux)? 2) Can you put it on the [PyPI](http://pypi.python.org/pypi)? – Craig McQueen Sep 06 '10 at 00:28
  • Great! The documentation doesn't mention supported platforms. Does it work on Windows? – Craig McQueen Nov 01 '11 at 20:52
  • I've had a look at it, and it doesn't read my ELF files (from GCC compiler targeting embedded ARM). I guess it's a partial implementation. Should I submit sample ELF files to you? – Craig McQueen Dec 01 '11 at 00:44
5

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.

Martin Atkins
  • 62,420
  • 8
  • 120
  • 138
Falaina
  • 6,625
  • 29
  • 31