From c30355e26a82331867cfa85cdeeabc8566860256 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Fri, 27 Dec 2013 06:30:27 -0800 Subject: [PATCH] Fix recent pull by refactoring the code a bit and fixing failing tests. --- elftools/dwarf/die.py | 30 +++++++------------ examples/dwarf_die_tree.py | 2 +- examples/examine_dwarf_info.py | 2 +- examples/reference_output/dwarf_die_tree.out | 8 ++--- .../reference_output/examine_dwarf_info.out | 8 ++--- 5 files changed, 21 insertions(+), 29 deletions(-) diff --git a/elftools/dwarf/die.py b/elftools/dwarf/die.py index 282dc7c..7cdefb5 100644 --- a/elftools/dwarf/die.py +++ b/elftools/dwarf/die.py @@ -100,26 +100,18 @@ 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. + def get_full_path(self): + """ Return the full path filename for the DIE. + + The filename 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 + comp_dir_attr = self.attributes.get('DW_AT_comp_dir', None) + comp_dir = bytes2str(comp_dir_attr.value) if comp_dir_attr else '' + fname_attr = self.attributes.get('DW_AT_name', None) + fname = bytes2str(fname_attr.value) if fname_attr else '' return os.path.join(comp_dir, fname) def iter_children(self): diff --git a/examples/dwarf_die_tree.py b/examples/dwarf_die_tree.py index ba0e0e7..ef108c3 100644 --- a/examples/dwarf_die_tree.py +++ b/examples/dwarf_die_tree.py @@ -44,7 +44,7 @@ def process_file(filename): print(' Top DIE with tag=%s' % top_DIE.tag) # We're interested in the filename... - print(' name=%s' % top_DIE.get_filename()) + print(' name=%s' % top_DIE.get_full_path()) # 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 3a54848..0e1619e 100644 --- a/examples/examine_dwarf_info.py +++ b/examples/examine_dwarf_info.py @@ -43,7 +43,7 @@ def process_file(filename): print(' Top DIE with tag=%s' % top_DIE.tag) # We're interested in the filename... - print(' name=%s' % top_DIE.get_filename()) + print(' name=%s' % top_DIE.get_full_path()) if __name__ == '__main__': for filename in sys.argv[1:]: diff --git a/examples/reference_output/dwarf_die_tree.out b/examples/reference_output/dwarf_die_tree.out index 143cbbb..4a81a8e 100644 --- a/examples/reference_output/dwarf_die_tree.out +++ b/examples/reference_output/dwarf_die_tree.out @@ -1,11 +1,11 @@ Processing file: ./examples/sample_exe64.elf Found a compile unit at offset 0, length 115 Top DIE with tag=DW_TAG_compile_unit - name=../sysdeps/x86_64/elf/start.S + name=/usr/src/packages/BUILD/glibc-2.11.1/csu/../sysdeps/x86_64/elf/start.S DIE tag=DW_TAG_compile_unit Found a compile unit at offset 119, length 135 Top DIE with tag=DW_TAG_compile_unit - name=init.c + name=/usr/src/packages/BUILD/glibc-2.11.1/csu/init.c DIE tag=DW_TAG_compile_unit DIE tag=DW_TAG_base_type DIE tag=DW_TAG_base_type @@ -21,7 +21,7 @@ Processing file: ./examples/sample_exe64.elf DIE tag=DW_TAG_const_type Found a compile unit at offset 258, length 156 Top DIE with tag=DW_TAG_compile_unit - name=z.c + name=/tmp/ebenders/z.c DIE tag=DW_TAG_compile_unit DIE tag=DW_TAG_subprogram DIE tag=DW_TAG_formal_parameter @@ -33,7 +33,7 @@ Processing file: ./examples/sample_exe64.elf DIE tag=DW_TAG_variable Found a compile unit at offset 418, length 300 Top DIE with tag=DW_TAG_compile_unit - name=elf-init.c + name=/usr/src/packages/BUILD/glibc-2.11.1/csu/elf-init.c DIE tag=DW_TAG_compile_unit DIE tag=DW_TAG_base_type DIE tag=DW_TAG_typedef diff --git a/examples/reference_output/examine_dwarf_info.out b/examples/reference_output/examine_dwarf_info.out index 968be29..5114626 100644 --- a/examples/reference_output/examine_dwarf_info.out +++ b/examples/reference_output/examine_dwarf_info.out @@ -1,13 +1,13 @@ Processing file: ./examples/sample_exe64.elf Found a compile unit at offset 0, length 115 Top DIE with tag=DW_TAG_compile_unit - name=../sysdeps/x86_64/elf/start.S + name=/usr/src/packages/BUILD/glibc-2.11.1/csu/../sysdeps/x86_64/elf/start.S Found a compile unit at offset 119, length 135 Top DIE with tag=DW_TAG_compile_unit - name=init.c + name=/usr/src/packages/BUILD/glibc-2.11.1/csu/init.c Found a compile unit at offset 258, length 156 Top DIE with tag=DW_TAG_compile_unit - name=z.c + name=/tmp/ebenders/z.c Found a compile unit at offset 418, length 300 Top DIE with tag=DW_TAG_compile_unit - name=elf-init.c + name=/usr/src/packages/BUILD/glibc-2.11.1/csu/elf-init.c -- 2.30.2