Remove unused import
[pyelftools.git] / examples / dwarf_location_lists.py
1 #-------------------------------------------------------------------------------
2 # elftools example: dwarf_location_lists.py
3 #
4 # Examine DIE entries which have location list values, and decode these
5 # location lists.
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
12
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] = ['.', '..']
16
17
18 from elftools.common.py3compat import itervalues
19 from elftools.elf.elffile import ELFFile
20 from elftools.dwarf.descriptions import (
21 describe_DWARF_expr, set_global_machine_arch)
22 from elftools.dwarf.locationlists import LocationEntry
23
24
25 def process_file(filename):
26 print('Processing file:', filename)
27 with open(filename, 'rb') as f:
28 elffile = ELFFile(f)
29
30 if not elffile.has_dwarf_info():
31 print(' file has no DWARF info')
32 return
33
34 # get_dwarf_info returns a DWARFInfo context object, which is the
35 # starting point for all DWARF-based processing in pyelftools.
36 dwarfinfo = elffile.get_dwarf_info()
37
38 # The location lists are extracted by DWARFInfo from the .debug_loc
39 # section, and returned here as a LocationLists object.
40 location_lists = dwarfinfo.location_lists()
41
42 # This is required for the descriptions module to correctly decode
43 # register names contained in DWARF expressions.
44 set_global_machine_arch(elffile.get_machine_arch())
45
46 for CU in dwarfinfo.iter_CUs():
47 # DWARFInfo allows to iterate over the compile units contained in
48 # the .debug_info section. CU is a CompileUnit object, with some
49 # computed attributes (such as its offset in the section) and
50 # a header which conforms to the DWARF standard. The access to
51 # header elements is, as usual, via item-lookup.
52 print(' Found a compile unit at offset %s, length %s' % (
53 CU.cu_offset, CU['unit_length']))
54
55 # A CU provides a simple API to iterate over all the DIEs in it.
56 for DIE in CU.iter_DIEs():
57 # Go over all attributes of the DIE. Each attribute is an
58 # AttributeValue object (from elftools.dwarf.die), which we
59 # can examine.
60 for attr in itervalues(DIE.attributes):
61 if attribute_has_location_list(attr):
62 # This is a location list. Its value is an offset into
63 # the .debug_loc section, so we can use the location
64 # lists object to decode it.
65 loclist = location_lists.get_location_list_at_offset(
66 attr.value)
67
68 print(' DIE %s. attr %s.\n%s' % (
69 DIE.tag,
70 attr.name,
71 show_loclist(loclist, dwarfinfo, indent=' ')))
72
73
74 def show_loclist(loclist, dwarfinfo, indent):
75 """ Display a location list nicely, decoding the DWARF expressions
76 contained within.
77 """
78 d = []
79 for loc_entity in loclist:
80 if isinstance(loc_entity, LocationEntry):
81 d.append('%s <<%s>>' % (
82 loc_entity,
83 describe_DWARF_expr(loc_entity.loc_expr, dwarfinfo.structs)))
84 else:
85 d.append(str(loc_entity))
86 return '\n'.join(indent + s for s in d)
87
88
89 def attribute_has_location_list(attr):
90 """ Only some attributes can have location list values, if they have the
91 required DW_FORM (loclistptr "class" in DWARF spec v3)
92 """
93 if (attr.name in ( 'DW_AT_location', 'DW_AT_string_length',
94 'DW_AT_const_value', 'DW_AT_return_addr',
95 'DW_AT_data_member_location', 'DW_AT_frame_base',
96 'DW_AT_segment', 'DW_AT_static_link',
97 'DW_AT_use_location', 'DW_AT_vtable_elem_location')):
98 if attr.form in ('DW_FORM_data4', 'DW_FORM_data8'):
99 return True
100 return False
101
102
103 if __name__ == '__main__':
104 for filename in sys.argv[1:]:
105 process_file(filename)
106
107
108
109
110
111