Remove unused import
[pyelftools.git] / examples / dwarf_decode_address.py
index 4b9ecdc3f5b8679a146270b65e21401721681ba2..80b53eefc897cdc7899dd3d2ab89f22c9d6d1a86 100644 (file)
 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[0:0] = ['.', '..']
+# 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.common.py3compat import maxint, bytes2str
 from elftools.elf.elffile import ELFFile
@@ -66,16 +63,18 @@ def decode_file_line(dwarfinfo, address):
     for CU in dwarfinfo.iter_CUs():
         # First, look at line programs to find the file/line for the address
         lineprog = dwarfinfo.line_program_for_CU(CU)
-        prevaddr = maxint
+        prevstate = None
         for entry in lineprog.get_entries():
             # We're interested in those entries where a new state is assigned
-            state = entry.state
-            if state is not None and not state.end_sequence:
-                if prevaddr <= address <= state.address:
-                    filename = lineprog['file_entry'][state.file - 1].name
-                    line = state.line
-                    return filename, line
-                prevaddr = state.address
+            if entry.state is None or entry.state.end_sequence:
+                continue
+            # Looking for a range of addresses in two consecutive states that
+            # contain the required address.
+            if prevstate and prevstate.address <= address < entry.state.address:
+                filename = lineprog['file_entry'][prevstate.file - 1].name
+                line = prevstate.line
+                return filename, line
+            prevstate = entry.state
     return None, None