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