2 Use lldb Python API to disassemble raw machine code bytes
5 from __future__ import print_function
10 from lldbsuite.test.decorators import *
11 from lldbsuite.test.lldbtest import *
12 from lldbsuite.test import lldbutil
15 class DisassembleRawDataTestCase(TestBase):
17 mydir = TestBase.compute_mydir(__file__)
19 @add_test_categories(['pyapi'])
22 def test_disassemble_raw_data(self):
23 """Test disassembling raw bytes with the API."""
24 # Create a target from the debugger.
25 arch = self.getArchitecture()
26 if re.match("mips*el", arch):
27 target = self.dbg.CreateTargetWithFileAndTargetTriple("", "mipsel")
28 raw_bytes = bytearray([0x21, 0xf0, 0xa0, 0x03])
29 elif re.match("mips", arch):
30 target = self.dbg.CreateTargetWithFileAndTargetTriple("", "mips")
31 raw_bytes = bytearray([0x03, 0xa0, 0xf0, 0x21])
32 elif re.match("powerpc64le", arch):
33 target = self.dbg.CreateTargetWithFileAndTargetTriple("", "powerpc64le")
34 raw_bytes = bytearray([0x00, 0x00, 0x80, 0x38])
36 target = self.dbg.CreateTargetWithFileAndTargetTriple("", "x86_64")
37 raw_bytes = bytearray([0x48, 0x89, 0xe5])
39 self.assertTrue(target, VALID_TARGET)
40 insts = target.GetInstructions(lldb.SBAddress(0, target), raw_bytes)
42 inst = insts.GetInstructionAtIndex(0)
46 print("Raw bytes: ", [hex(x) for x in raw_bytes])
47 print("Disassembled%s" % str(inst))
48 if re.match("mips", arch):
49 self.assertTrue(inst.GetMnemonic(target) == "move")
50 self.assertTrue(inst.GetOperands(target) ==
51 '$' + "fp, " + '$' + "sp")
52 elif re.match("powerpc64le", arch):
53 self.assertTrue(inst.GetMnemonic(target) == "li")
54 self.assertTrue(inst.GetOperands(target) == "4, 0")
56 self.assertTrue(inst.GetMnemonic(target) == "movq")
57 self.assertTrue(inst.GetOperands(target) ==
58 '%' + "rsp, " + '%' + "rbp")