Eli Bendersky | 1a516a3 | 2011-12-22 15:22:00 +0200 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
| 2 | # elftools example: dwarf_die_tree.py |
| 3 | # |
| 4 | # In the .debug_info section, Dwarf Information Entries (DIEs) form a tree. |
| 5 | # pyelftools provides easy access to this tree, as demonstrated here. |
| 6 | # |
| 7 | # Eli Bendersky (eliben@gmail.com) |
| 8 | # This code is in the public domain |
| 9 | #------------------------------------------------------------------------------- |
| 10 | from __future__ import print_function |
| 11 | import sys |
Eli Bendersky | ce5d187 | 2011-12-22 20:03:06 +0200 | [diff] [blame] | 12 | |
Eli Bendersky | cc1e557 | 2013-04-09 21:25:54 -0700 | [diff] [blame] | 13 | # If pyelftools is not installed, the example can also run from the root or |
| 14 | # examples/ dir of the source distribution. |
| 15 | sys.path[0:0] = ['.', '..'] |
Eli Bendersky | ce5d187 | 2011-12-22 20:03:06 +0200 | [diff] [blame] | 16 | |
Eli Bendersky | 1a516a3 | 2011-12-22 15:22:00 +0200 | [diff] [blame] | 17 | from elftools.elf.elffile import ELFFile |
| 18 | |
| 19 | |
| 20 | def process_file(filename): |
| 21 | print('Processing file:', filename) |
eli.bendersky | 3bd3ecc | 2012-01-11 15:56:41 +0200 | [diff] [blame] | 22 | with open(filename, 'rb') as f: |
Eli Bendersky | 1a516a3 | 2011-12-22 15:22:00 +0200 | [diff] [blame] | 23 | elffile = ELFFile(f) |
| 24 | |
| 25 | if not elffile.has_dwarf_info(): |
| 26 | print(' file has no DWARF info') |
| 27 | return |
| 28 | |
| 29 | # get_dwarf_info returns a DWARFInfo context object, which is the |
| 30 | # starting point for all DWARF-based processing in pyelftools. |
| 31 | dwarfinfo = elffile.get_dwarf_info() |
| 32 | |
| 33 | for CU in dwarfinfo.iter_CUs(): |
| 34 | # DWARFInfo allows to iterate over the compile units contained in |
| 35 | # the .debug_info section. CU is a CompileUnit object, with some |
| 36 | # computed attributes (such as its offset in the section) and |
| 37 | # a header which conforms to the DWARF standard. The access to |
| 38 | # header elements is, as usual, via item-lookup. |
| 39 | print(' Found a compile unit at offset %s, length %s' % ( |
| 40 | CU.cu_offset, CU['unit_length'])) |
| 41 | |
| 42 | # Start with the top DIE, the root for this CU's DIE tree |
| 43 | top_DIE = CU.get_top_DIE() |
| 44 | print(' Top DIE with tag=%s' % top_DIE.tag) |
| 45 | |
Shaheed Haque | 7d8885f | 2013-12-27 12:36:34 +0000 | [diff] [blame] | 46 | # We're interested in the filename... |
Eli Bendersky | c30355e | 2013-12-27 06:30:27 -0800 | [diff] [blame] | 47 | print(' name=%s' % top_DIE.get_full_path()) |
Eli Bendersky | 1a516a3 | 2011-12-22 15:22:00 +0200 | [diff] [blame] | 48 | |
| 49 | # Display DIEs recursively starting with top_DIE |
| 50 | die_info_rec(top_DIE) |
| 51 | |
| 52 | |
| 53 | def die_info_rec(die, indent_level=' '): |
| 54 | """ A recursive function for showing information about a DIE and its |
| 55 | children. |
| 56 | """ |
| 57 | print(indent_level + 'DIE tag=%s' % die.tag) |
| 58 | child_indent = indent_level + ' ' |
| 59 | for child in die.iter_children(): |
| 60 | die_info_rec(child, child_indent) |
| 61 | |
| 62 | |
| 63 | if __name__ == '__main__': |
| 64 | for filename in sys.argv[1:]: |
| 65 | process_file(filename) |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |