blob: 9dcb6b658ce49dca3e0f9d3da08dbc9f2e2645c0 [file] [log] [blame]
Eli Bendersky1a516a32011-12-22 15:22:00 +02001#-------------------------------------------------------------------------------
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#-------------------------------------------------------------------------------
10from __future__ import print_function
11import sys
Eli Benderskyce5d1872011-12-22 20:03:06 +020012
Eli Benderskycc1e5572013-04-09 21:25:54 -070013# If pyelftools is not installed, the example can also run from the root or
14# examples/ dir of the source distribution.
15sys.path[0:0] = ['.', '..']
Eli Benderskyce5d1872011-12-22 20:03:06 +020016
Eli Bendersky79271e92012-01-27 10:25:47 +020017from elftools.common.py3compat import bytes2str
Eli Bendersky1a516a32011-12-22 15:22:00 +020018from elftools.elf.elffile import ELFFile
19
20
21def process_file(filename):
22 print('Processing file:', filename)
eli.bendersky3bd3ecc2012-01-11 15:56:41 +020023 with open(filename, 'rb') as f:
Eli Bendersky1a516a32011-12-22 15:22:00 +020024 elffile = ELFFile(f)
25
26 if not elffile.has_dwarf_info():
27 print(' file has no DWARF info')
28 return
29
30 # get_dwarf_info returns a DWARFInfo context object, which is the
31 # starting point for all DWARF-based processing in pyelftools.
32 dwarfinfo = elffile.get_dwarf_info()
33
34 for CU in dwarfinfo.iter_CUs():
35 # DWARFInfo allows to iterate over the compile units contained in
36 # the .debug_info section. CU is a CompileUnit object, with some
37 # computed attributes (such as its offset in the section) and
38 # a header which conforms to the DWARF standard. The access to
39 # header elements is, as usual, via item-lookup.
40 print(' Found a compile unit at offset %s, length %s' % (
41 CU.cu_offset, CU['unit_length']))
42
43 # Start with the top DIE, the root for this CU's DIE tree
44 top_DIE = CU.get_top_DIE()
45 print(' Top DIE with tag=%s' % top_DIE.tag)
46
47 # Each DIE holds an OrderedDict of attributes, mapping names to
48 # values. Values are represented by AttributeValue objects in
49 # elftools/dwarf/die.py
50 # We're interested in the DW_AT_name attribute. Note that its value
51 # is usually a string taken from the .debug_string section. This
52 # is done transparently by the library, and such a value will be
53 # simply given as a string.
54 name_attr = top_DIE.attributes['DW_AT_name']
Eli Bendersky79271e92012-01-27 10:25:47 +020055 print(' name=%s' % bytes2str(name_attr.value))
Eli Bendersky1a516a32011-12-22 15:22:00 +020056
57 # Display DIEs recursively starting with top_DIE
58 die_info_rec(top_DIE)
59
60
61def die_info_rec(die, indent_level=' '):
62 """ A recursive function for showing information about a DIE and its
63 children.
64 """
65 print(indent_level + 'DIE tag=%s' % die.tag)
66 child_indent = indent_level + ' '
67 for child in die.iter_children():
68 die_info_rec(child, child_indent)
69
70
71if __name__ == '__main__':
72 for filename in sys.argv[1:]:
73 process_file(filename)
74
75
76
77
78
79