some unit tests for describe_CFI_instructions
authorEli Bendersky <eliben@gmail.com>
Thu, 15 Dec 2011 07:04:42 +0000 (09:04 +0200)
committerEli Bendersky <eliben@gmail.com>
Thu, 15 Dec 2011 07:04:42 +0000 (09:04 +0200)
elftools/dwarf/callframe.py
elftools/dwarf/descriptions.py
tests/test_callframe.py

index 09f80d44988220cc5b7b65d90bf63eec3e69c167..3f6b735262d79dea60c567e838715eae2cfae4d0 100644 (file)
@@ -212,7 +212,7 @@ class CallFrameInfo(object):
                 args = [struct_parse(
                     structs.Dwarf_dw_form['DW_FORM_block'], self.stream)]
             elif opcode in (DW_CFA_expression, DW_CFA_val_expression):
-                arsg = [
+                args = [
                     struct_parse(structs.Dwarf_uleb128(''), self.stream),
                     struct_parse(
                         structs.Dwarf_dw_form['DW_FORM_block'], self.stream)]
index 91037d3d4f24066a8b40f95d9c07ee5149d98e4f..0a45065d501975d09152f8b3f04dca0a2561fbb1 100644 (file)
@@ -94,13 +94,13 @@ def describe_CFI_instructions(entry):
             s += '  %s: %s\n' % (name, instr.args[0])
         elif name == 'DW_CFA_def_cfa_expression':
             expr_dumper = ExprDumper(entry.structs)
-            expr_str = dumper.process_expr(instr.args[0])
-            s += '  %s: (%s)\n' % (name, expr_str)
+            expr_dumper.process_expr(instr.args[0])
+            s += '  %s: (%s)\n' % (name, expr_dumper.get_str())
         elif name == 'DW_CFA_expression':
             expr_dumper = ExprDumper(entry.structs)
-            expr_str = dumper.process_expr(instr.args[1])
+            expr_dumper.process_expr(instr.args[1])
             s += '  %s: %s (%s)\n' % (
-                name, _full_reg_name(instr.args[0]), expr_str)
+                name, _full_reg_name(instr.args[0]), expr_dumper.get_str())
         else:
             s += '  %s: <??>\n' % name
 
index 839b1188b7bb6dbf2746cbb9d59dd6d8e9716e5d..5f7b00ced3025b9c0b4124eb1e3a3d07ceaf5d37 100644 (file)
@@ -5,6 +5,8 @@ sys.path.extend(['.', '..'])
 from elftools.dwarf.callframe import (
     CallFrameInfo, CIE, FDE, instruction_name, CallFrameInstruction)
 from elftools.dwarf.structs import DWARFStructs
+from elftools.dwarf.descriptions import (describe_CFI_instructions,
+    set_global_machine_arch)
 
 
 class TestCallFrame(unittest.TestCase):
@@ -12,7 +14,7 @@ class TestCallFrame(unittest.TestCase):
         self.assertIsInstance(instr, CallFrameInstruction)
         self.assertEqual(instruction_name(instr.opcode), name)
         self.assertEqual(instr.args, args)
-        
+       
     def test_spec_sample_d6(self):
         # D.6 sample in DWARFv3
         s = StringIO()
@@ -88,6 +90,27 @@ class TestCallFrame(unittest.TestCase):
         self.assertInstruction(entries[1].instructions[20],
             'DW_CFA_nop', [])
 
+    def test_describe_CFI_instructions(self):
+        # The data here represents a single CIE 
+        data = ('' +
+            '\x16\x00\x00\x00' +        # length
+            '\xff\xff\xff\xff' +        # CIE_id
+            '\x03\x00\x04\x7c' +        # version, augmentation, caf, daf
+            '\x08' +                    # return address
+            '\x0c\x07\x02' +
+            '\x10\x02\x07\x03\x01\x02\x00\x00\x06\x06')
+        s = StringIO(data)
+
+        structs = DWARFStructs(little_endian=True, dwarf_format=32, address_size=4)
+        cfi = CallFrameInfo(s, len(data), structs)
+        entries = cfi.get_entries()
+
+        set_global_machine_arch('x86')
+        self.assertEqual(describe_CFI_instructions(entries[0]),
+            (   '  DW_CFA_def_cfa: r7 (edi) ofs 2\n' + 
+                '  DW_CFA_expression: r2 (edx) (DW_OP_addr: 201; DW_OP_deref; DW_OP_deref)\n'))
+
+
 if __name__ == '__main__':
     unittest.main()