1""" 2Use lldb Python API to disassemble raw machine code bytes 3""" 4 5import os, time 6import re 7import unittest2 8import lldb, lldbutil 9from lldbtest import * 10 11class DisassembleRawDataTestCase(TestBase): 12 13 mydir = os.path.join("python_api", "disassemble-raw-data") 14 15 @python_api_test 16 def test_disassemble_raw_data(self): 17 """Test disassembling raw bytes with the API.""" 18 self.disassemble_raw_data() 19 20 def disassemble_raw_data(self): 21 """Test disassembling raw bytes with the API.""" 22 # Create a target from the debugger. 23 24 target = self.dbg.CreateTargetWithFileAndTargetTriple ("", "x86_64") 25 self.assertTrue(target, VALID_TARGET) 26 27 raw_bytes = bytearray([0x48, 0x89, 0xe5]) 28 29 insts = target.GetInstructions(lldb.SBAddress(), raw_bytes) 30 31 inst = insts.GetInstructionAtIndex(0) 32 33 if self.TraceOn(): 34 print 35 print "Raw bytes: ", [hex(x) for x in raw_bytes] 36 print "Disassembled%s" % str(inst) 37 38 self.assertTrue (inst.GetMnemonic(target) == "movq") 39 self.assertTrue (inst.GetOperands(target) == '%' + "rsp, " + '%' + "rbp") 40 41if __name__ == '__main__': 42 import atexit 43 lldb.SBDebugger.Initialize() 44 atexit.register(lambda: lldb.SBDebugger.Terminate()) 45 unittest2.main() 46