xref: /llvm-project/lldb/test/API/python_api/disassemble-raw-data/TestDisassemble_VST1_64.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""
2Use lldb Python API to disassemble raw machine code bytes
3"""
4
5from io import StringIO
6import sys
7
8import lldb
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12
13
14class Disassemble_VST1_64(TestBase):
15    @no_debug_info_test
16    @skipIfLLVMTargetMissing("ARM")
17    def test_disassemble_invalid_vst_1_64_raw_data(self):
18        """Test disassembling invalid vst1.64 raw bytes with the API."""
19        # Create a target from the debugger.
20        target = self.dbg.CreateTargetWithFileAndTargetTriple(
21            "", "thumbv7-apple-macosx"
22        )
23        self.assertTrue(target, VALID_TARGET)
24
25        raw_bytes = bytearray(
26            [
27                0xF0,
28                0xB5,
29                0x03,
30                0xAF,
31                0x2D,
32                0xE9,
33                0x00,
34                0x0D,
35                0xAD,
36                0xF1,
37                0x40,
38                0x04,
39                0x24,
40                0xF0,
41                0x0F,
42                0x04,
43                0xA5,
44                0x46,
45            ]
46        )
47
48        assembly = """
49        push   {r4, r5, r6, r7, lr}
50        add    r7, sp, #0xc
51        push.w {r8, r10, r11}
52        sub.w  r4, sp, #0x40
53        bic    r4, r4, #0xf
54        mov    sp, r4
55        """
56
57        def split(s):
58            return [x.strip() for x in s.strip().splitlines()]
59
60        insts = target.GetInstructions(lldb.SBAddress(), raw_bytes)
61
62        if self.TraceOn():
63            print()
64            for i in insts:
65                print("Disassembled %s" % str(i))
66
67        sio = StringIO()
68        insts.Print(sio)
69        self.assertEqual(split(assembly), split(sio.getvalue()))
70
71        self.assertEqual(insts.GetSize(), len(split(assembly)))
72
73        for i, asm in enumerate(split(assembly)):
74            inst = insts.GetInstructionAtIndex(i)
75            sio = StringIO()
76            inst.Print(sio)
77            self.assertEqual(asm, sio.getvalue().strip())
78
79        raw_bytes = bytearray([0x04, 0xF9, 0xED, 0x82])
80
81        insts = target.GetInstructions(lldb.SBAddress(), raw_bytes)
82
83        inst = insts.GetInstructionAtIndex(0)
84
85        if self.TraceOn():
86            print()
87            print("Raw bytes:    ", [hex(x) for x in raw_bytes])
88            print("Disassembled%s" % str(inst))
89
90        self.assertEqual(inst.GetMnemonic(target), "vst1.64")
91