2 Use lldb Python API to disassemble raw machine code bytes
5 from __future__ import print_function
7 from io import StringIO
11 from lldbsuite.test.decorators import *
12 from lldbsuite.test.lldbtest import *
13 from lldbsuite.test import lldbutil
16 class Disassemble_VST1_64(TestBase):
18 mydir = TestBase.compute_mydir(__file__)
20 @add_test_categories(['pyapi'])
22 @skipIfLLVMTargetMissing("ARM")
23 def test_disassemble_invalid_vst_1_64_raw_data(self):
24 """Test disassembling invalid vst1.64 raw bytes with the API."""
25 # Create a target from the debugger.
26 target = self.dbg.CreateTargetWithFileAndTargetTriple("", "thumbv7-apple-macosx")
27 self.assertTrue(target, VALID_TARGET)
29 raw_bytes = bytearray([0xf0, 0xb5, 0x03, 0xaf,
30 0x2d, 0xe9, 0x00, 0x0d,
31 0xad, 0xf1, 0x40, 0x04,
32 0x24, 0xf0, 0x0f, 0x04,
36 push {r4, r5, r6, r7, lr}
44 return [x.strip() for x in s.strip().splitlines()]
46 insts = target.GetInstructions(lldb.SBAddress(), raw_bytes)
51 print("Disassembled %s" % str(i))
53 if sys.version_info.major >= 3:
56 self.assertEqual(split(assembly), split(sio.getvalue()))
58 self.assertEqual(insts.GetSize(), len(split(assembly)))
60 if sys.version_info.major >= 3:
61 for i,asm in enumerate(split(assembly)):
62 inst = insts.GetInstructionAtIndex(i)
65 self.assertEqual(asm, sio.getvalue().strip())
67 raw_bytes = bytearray([0x04, 0xf9, 0xed, 0x82])
69 insts = target.GetInstructions(lldb.SBAddress(), raw_bytes)
71 inst = insts.GetInstructionAtIndex(0)
75 print("Raw bytes: ", [hex(x) for x in raw_bytes])
76 print("Disassembled%s" % str(inst))
78 self.assertTrue(inst.GetMnemonic(target) == "vst1.64")