from __future__ import print_function
import sys
-# If elftools is not installed, maybe we're running from the root or examples
-# dir of the source distribution
-try:
- import elftools
-except ImportError:
- sys.path.extend(['.', '..'])
+# If pyelftools is not installed, the example can also run from the root or
+# examples/ dir of the source distribution.
+sys.path[0:0] = ['.', '..']
from elftools.elf.elffile import ELFFile
def process_file(filename):
print('Processing file:', filename)
- with open(filename) as f:
+ with open(filename, 'rb') as f:
elffile = ELFFile(f)
if not elffile.has_dwarf_info():
top_DIE = CU.get_top_DIE()
print(' Top DIE with tag=%s' % top_DIE.tag)
- # Each DIE holds an OrderedDict of attributes, mapping names to
- # values. Values are represented by AttributeValue objects in
- # elftools/dwarf/die.py
- # We're interested in the DW_AT_name attribute. Note that its value
- # is usually a string taken from the .debug_str section. This
- # is done transparently by the library, and such a value will be
- # simply given as a string.
- name_attr = top_DIE.attributes['DW_AT_name']
- print(' name=%s' % name_attr.value)
+ # We're interested in the filename...
+ print(' name=%s' % top_DIE.get_full_path())
if __name__ == '__main__':
for filename in sys.argv[1:]: