2 Use lldb Python API to disassemble raw machine code bytes
7 from lldbsuite
.test
.decorators
import *
8 from lldbsuite
.test
.lldbtest
import *
9 from lldbsuite
.test
import lldbutil
12 class DisassembleRawDataTestCase(TestBase
):
15 def test_disassemble_raw_data(self
):
16 """Test disassembling raw bytes with the API."""
17 # Create a target from the debugger.
18 arch
= self
.getArchitecture()
19 if re
.match("mips*el", arch
):
20 target
= self
.dbg
.CreateTargetWithFileAndTargetTriple("", "mipsel")
21 raw_bytes
= bytearray([0x21, 0xF0, 0xA0, 0x03])
22 elif re
.match("mips", arch
):
23 target
= self
.dbg
.CreateTargetWithFileAndTargetTriple("", "mips")
24 raw_bytes
= bytearray([0x03, 0xA0, 0xF0, 0x21])
25 elif re
.match("powerpc64le", arch
):
26 target
= self
.dbg
.CreateTargetWithFileAndTargetTriple("", "powerpc64le")
27 raw_bytes
= bytearray([0x00, 0x00, 0x80, 0x38])
28 elif arch
in ("aarch64", "arm64"):
29 target
= self
.dbg
.CreateTargetWithFileAndTargetTriple("", "aarch64")
30 raw_bytes
= bytearray([0x60, 0x0C, 0x80, 0x52])
32 target
= self
.dbg
.CreateTargetWithFileAndTargetTriple("", "arm")
33 raw_bytes
= bytearray([0x63, 0x30, 0xA0, 0xE3])
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
.assertEqual(inst
.GetMnemonic(target
), "move")
49 self
.assertEqual(inst
.GetOperands(target
), "$" + "fp, " + "$" + "sp")
51 inst
.GetControlFlowKind(target
), lldb
.eInstructionControlFlowKindUnknown
53 elif re
.match("powerpc64le", arch
):
54 self
.assertEqual(inst
.GetMnemonic(target
), "li")
55 self
.assertEqual(inst
.GetOperands(target
), "4, 0")
57 inst
.GetControlFlowKind(target
), lldb
.eInstructionControlFlowKindUnknown
59 elif arch
in ("aarch64", "arm64"):
60 self
.assertEqual(inst
.GetMnemonic(target
), "mov")
61 self
.assertEqual(inst
.GetOperands(target
), "w0, #0x63")
62 self
.assertEqual(inst
.GetComment(target
), "=99 ")
64 inst
.GetControlFlowKind(target
), lldb
.eInstructionControlFlowKindUnknown
66 # Make sure that using colors doesn't affect the output here.
67 res
= lldb
.SBCommandReturnObject()
68 ci
= self
.dbg
.GetCommandInterpreter()
69 ci
.HandleCommand("settings set use-color true", res
)
70 self
.assertEqual(inst
.GetOperands(target
), "w0, #0x63")
71 self
.assertEqual(inst
.GetMnemonic(target
), "mov")
72 self
.assertEqual(inst
.GetComment(target
), "=99 ")
73 ci
.HandleCommand("settings set use-color false", res
)
76 self
.assertEqual(inst
.GetMnemonic(target
), "mov")
77 self
.assertEqual(inst
.GetOperands(target
), "r3, #99")
79 inst
.GetControlFlowKind(target
), lldb
.eInstructionControlFlowKindUnknown
82 self
.assertEqual(inst
.GetMnemonic(target
), "movq")
83 self
.assertEqual(inst
.GetOperands(target
), "%" + "rsp, " + "%" + "rbp")
85 inst
.GetControlFlowKind(target
), lldb
.eInstructionControlFlowKindOther