From: Shaheed Haque Date: Fri, 27 Dec 2013 12:36:34 +0000 (+0000) Subject: 1. Centralise the logic for getting the filename for a DIE. X-Git-Tag: v0.22~22^2 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=7d8885f90325713dcf5b21a85e1beecdf6fb5df6;p=pyelftools.git 1. Centralise the logic for getting the filename for a DIE. 2. Use the form of logic suggested by Philippe Ombredanne (thanks!). --- diff --git a/elftools/dwarf/die.py b/elftools/dwarf/die.py index cb5d050..282dc7c 100644 --- a/elftools/dwarf/die.py +++ b/elftools/dwarf/die.py @@ -7,8 +7,9 @@ # This code is in the public domain #------------------------------------------------------------------------------- from collections import namedtuple +import os -from ..common.py3compat import OrderedDict +from ..common.py3compat import OrderedDict, bytes2str from ..common.utils import struct_parse, preserve_stream_pos @@ -99,6 +100,28 @@ class DIE(object): """ return self._parent + def get_filename(self): + """Return the filename for the DIE. + The filename, which is the join of 'DW_AT_comp_dir' and 'DW_AT_name', + either of which may be missing in practice. Note that its value + is usually a string taken from the .debug_string section and the + returned value will be a string. + """ + comp_dir = '' + try: + comp_dir_attr = self.attributes['DW_AT_comp_dir'] + comp_dir = bytes2str(comp_dir_attr.value) + except KeyError: + pass + + fname = '' + try: + fname_attr = self.attributes['DW_AT_name'] + fname = bytes2str(fname_attr.value) + except KeyError: + pass + return os.path.join(comp_dir, fname) + def iter_children(self): """ Yield all children of this DIE """ diff --git a/examples/dwarf_die_tree.py b/examples/dwarf_die_tree.py index 9dcb6b6..ba0e0e7 100644 --- a/examples/dwarf_die_tree.py +++ b/examples/dwarf_die_tree.py @@ -14,7 +14,6 @@ import sys # examples/ dir of the source distribution. sys.path[0:0] = ['.', '..'] -from elftools.common.py3compat import bytes2str from elftools.elf.elffile import ELFFile @@ -44,15 +43,8 @@ def process_file(filename): 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_string 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' % bytes2str(name_attr.value)) + # We're interested in the filename... + print(' name=%s' % top_DIE.get_filename()) # Display DIEs recursively starting with top_DIE die_info_rec(top_DIE) diff --git a/examples/examine_dwarf_info.py b/examples/examine_dwarf_info.py index 1aa28c6..3a54848 100644 --- a/examples/examine_dwarf_info.py +++ b/examples/examine_dwarf_info.py @@ -13,7 +13,6 @@ import sys # examples/ dir of the source distribution. sys.path[0:0] = ['.', '..'] -from elftools.common.py3compat import bytes2str from elftools.elf.elffile import ELFFile @@ -43,15 +42,8 @@ def process_file(filename): 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' % bytes2str(name_attr.value)) + # We're interested in the filename... + print(' name=%s' % top_DIE.get_filename()) if __name__ == '__main__': for filename in sys.argv[1:]: