Eli Bendersky | 1a516a3 | 2011-12-22 15:22:00 +0200 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
| 2 | # elftools example: examine_dwarf_info.py |
| 3 | # |
| 4 | # An example of examining information in the .debug_info section of an ELF file. |
| 5 | # |
| 6 | # Eli Bendersky (eliben@gmail.com) |
| 7 | # This code is in the public domain |
| 8 | #------------------------------------------------------------------------------- |
| 9 | from __future__ import print_function |
| 10 | import sys |
Eli Bendersky | ce5d187 | 2011-12-22 20:03:06 +0200 | [diff] [blame] | 11 | |
| 12 | # If elftools is not installed, maybe we're running from the root or examples |
| 13 | # dir of the source distribution |
| 14 | try: |
| 15 | import elftools |
| 16 | except ImportError: |
Eli Bendersky | dd71c43 | 2013-04-08 06:38:57 -0700 | [diff] [blame] | 17 | sys.path[0:0] = ['.', '..'] |
Eli Bendersky | ce5d187 | 2011-12-22 20:03:06 +0200 | [diff] [blame] | 18 | |
Eli Bendersky | 79271e9 | 2012-01-27 10:25:47 +0200 | [diff] [blame] | 19 | from elftools.common.py3compat import bytes2str |
Eli Bendersky | 1a516a3 | 2011-12-22 15:22:00 +0200 | [diff] [blame] | 20 | from elftools.elf.elffile import ELFFile |
| 21 | |
| 22 | |
| 23 | def process_file(filename): |
| 24 | print('Processing file:', filename) |
eli.bendersky | 3bd3ecc | 2012-01-11 15:56:41 +0200 | [diff] [blame] | 25 | with open(filename, 'rb') as f: |
Eli Bendersky | 1a516a3 | 2011-12-22 15:22:00 +0200 | [diff] [blame] | 26 | elffile = ELFFile(f) |
| 27 | |
| 28 | if not elffile.has_dwarf_info(): |
| 29 | print(' file has no DWARF info') |
| 30 | return |
| 31 | |
| 32 | # get_dwarf_info returns a DWARFInfo context object, which is the |
| 33 | # starting point for all DWARF-based processing in pyelftools. |
| 34 | dwarfinfo = elffile.get_dwarf_info() |
| 35 | |
| 36 | for CU in dwarfinfo.iter_CUs(): |
| 37 | # DWARFInfo allows to iterate over the compile units contained in |
| 38 | # the .debug_info section. CU is a CompileUnit object, with some |
| 39 | # computed attributes (such as its offset in the section) and |
| 40 | # a header which conforms to the DWARF standard. The access to |
| 41 | # header elements is, as usual, via item-lookup. |
| 42 | print(' Found a compile unit at offset %s, length %s' % ( |
| 43 | CU.cu_offset, CU['unit_length'])) |
| 44 | |
| 45 | # The first DIE in each compile unit describes it. |
| 46 | top_DIE = CU.get_top_DIE() |
| 47 | print(' Top DIE with tag=%s' % top_DIE.tag) |
| 48 | |
| 49 | # Each DIE holds an OrderedDict of attributes, mapping names to |
| 50 | # values. Values are represented by AttributeValue objects in |
| 51 | # elftools/dwarf/die.py |
| 52 | # We're interested in the DW_AT_name attribute. Note that its value |
Eli Bendersky | 40545e9 | 2011-12-22 15:53:52 +0200 | [diff] [blame] | 53 | # is usually a string taken from the .debug_str section. This |
Eli Bendersky | 1a516a3 | 2011-12-22 15:22:00 +0200 | [diff] [blame] | 54 | # is done transparently by the library, and such a value will be |
| 55 | # simply given as a string. |
| 56 | name_attr = top_DIE.attributes['DW_AT_name'] |
Eli Bendersky | 79271e9 | 2012-01-27 10:25:47 +0200 | [diff] [blame] | 57 | print(' name=%s' % bytes2str(name_attr.value)) |
Eli Bendersky | 1a516a3 | 2011-12-22 15:22:00 +0200 | [diff] [blame] | 58 | |
| 59 | if __name__ == '__main__': |
| 60 | for filename in sys.argv[1:]: |
| 61 | process_file(filename) |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |