brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.8 KiB · 4ec2753 Raw
87 lines · python
1"""2Use lldb Python API to disassemble raw machine code bytes3"""4 5import re6import lldb7from lldbsuite.test.decorators import *8from lldbsuite.test.lldbtest import *9from lldbsuite.test import lldbutil10 11 12class DisassembleRawDataTestCase(TestBase):13    @no_debug_info_test14    @skipIfRemote15    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])31        elif arch == "arm":32            target = self.dbg.CreateTargetWithFileAndTargetTriple("", "arm")33            raw_bytes = bytearray([0x63, 0x30, 0xA0, 0xE3])34        else:35            target = self.dbg.CreateTargetWithFileAndTargetTriple("", "x86_64")36            raw_bytes = bytearray([0x48, 0x89, 0xE5])37 38        self.assertTrue(target, VALID_TARGET)39        insts = target.GetInstructions(lldb.SBAddress(0, target), raw_bytes)40 41        inst = insts.GetInstructionAtIndex(0)42 43        if self.TraceOn():44            print()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")50            self.assertEqual(51                inst.GetControlFlowKind(target), lldb.eInstructionControlFlowKindUnknown52            )53        elif re.match("powerpc64le", arch):54            self.assertEqual(inst.GetMnemonic(target), "li")55            self.assertEqual(inst.GetOperands(target), "4, 0")56            self.assertEqual(57                inst.GetControlFlowKind(target), lldb.eInstructionControlFlowKindUnknown58            )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 ")63            self.assertEqual(64                inst.GetControlFlowKind(target), lldb.eInstructionControlFlowKindUnknown65            )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)74 75        elif arch == "arm":76            self.assertEqual(inst.GetMnemonic(target), "mov")77            self.assertEqual(inst.GetOperands(target), "r3, #99")78            self.assertEqual(79                inst.GetControlFlowKind(target), lldb.eInstructionControlFlowKindUnknown80            )81        else:82            self.assertEqual(inst.GetMnemonic(target), "movq")83            self.assertEqual(inst.GetOperands(target), "%" + "rsp, " + "%" + "rbp")84            self.assertEqual(85                inst.GetControlFlowKind(target), lldb.eInstructionControlFlowKindOther86            )87