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