2 import unittest2 as unittest
7 sys.path.extend(['.', '..'])
8 from elftools.common.py3compat import BytesIO, iteritems
9 from elftools.dwarf.lineprogram import LineProgram, LineState, LineProgramEntry
10 from elftools.dwarf.structs import DWARFStructs
11 from elftools.dwarf.constants import *
14 class TestLineProgram(unittest.TestCase):
15 def _make_program_in_stream(self, stream):
16 """ Create a LineProgram from the given program encoded in a stream
18 ds = DWARFStructs(little_endian=True, dwarf_format=32, address_size=4)
19 header = ds.Dwarf_lineprog_header.parse(
20 b'\x04\x10\x00\x00' + # initial lenght
21 b'\x03\x00' + # version
22 b'\x20\x00\x00\x00' + # header length
23 b'\x01\x01\x01\x0F' + # flags
24 b'\x0A' + # opcode_base
25 b'\x00\x01\x04\x08\x0C\x01\x01\x01\x00' + # standard_opcode_lengths
26 # 2 dir names followed by a NULL
27 b'\x61\x62\x00\x70\x00\x00' +
29 b'\x61\x72\x00\x0C\x0D\x0F' +
31 b'\x45\x50\x51\x00\x86\x12\x07\x08' +
35 lp = LineProgram(header, stream, ds, 0, len(stream.getvalue()))
38 def assertLineState(self, state, **kwargs):
39 """ Assert that the state attributes specified in kwargs have the given
40 values (the rest are default).
42 for k, v in iteritems(kwargs):
43 self.assertEqual(getattr(state, k), v)
45 def test_spec_sample_59(self):
46 # Sample in figure 59 of DWARFv3
57 lp = self._make_program_in_stream(s)
58 linetable = lp.get_entries()
60 self.assertEqual(len(linetable), 7)
61 self.assertIs(linetable[0].state, None) # doesn't modify state
62 self.assertEqual(linetable[0].command, DW_LNS_advance_pc)
63 self.assertEqual(linetable[0].args, [0x239])
64 self.assertLineState(linetable[1].state, address=0x239, line=3)
65 self.assertEqual(linetable[1].command, 0xb)
66 self.assertEqual(linetable[1].args, [2, 0])
67 self.assertLineState(linetable[2].state, address=0x23c, line=5)
68 self.assertLineState(linetable[3].state, address=0x244, line=6)
69 self.assertLineState(linetable[4].state, address=0x24b, line=7, end_sequence=False)
70 self.assertEqual(linetable[5].command, DW_LNS_advance_pc)
71 self.assertEqual(linetable[5].args, [2])
72 self.assertLineState(linetable[6].state, address=0x24d, line=7, end_sequence=True)
74 def test_spec_sample_60(self):
75 # Sample in figure 60 of DWARFv3
89 lp = self._make_program_in_stream(s)
90 linetable = lp.get_entries()
92 self.assertEqual(len(linetable), 10)
93 self.assertIs(linetable[0].state, None) # doesn't modify state
94 self.assertEqual(linetable[0].command, DW_LNS_fixed_advance_pc)
95 self.assertEqual(linetable[0].args, [0x239])
96 self.assertLineState(linetable[1].state, address=0x239, line=3)
97 self.assertLineState(linetable[3].state, address=0x23c, line=5)
98 self.assertLineState(linetable[5].state, address=0x244, line=6)
99 self.assertLineState(linetable[7].state, address=0x24b, line=7, end_sequence=False)
100 self.assertLineState(linetable[9].state, address=0x24d, line=7, end_sequence=True)
103 if __name__ == '__main__':