blob: fe5fdf0a3cec1c6a52ccb93c934ba1f9de63b680 [file] [log] [blame]
Sean Callanan50952e92011-12-14 23:49:37 +00001"""
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
Johnny Chen1917bc82011-12-15 19:56:28 +000013 mydir = os.path.join("python_api", "disassemble-raw-data")
Sean Callanan50952e92011-12-14 23:49:37 +000014
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-apple-darwin")
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 self.assertTrue (inst.GetMnemonic(target) == "movq")
34 self.assertTrue (inst.GetOperands(target) == '%' + "rsp, " + '%' + "rbp")
Johnny Chen1917bc82011-12-15 19:56:28 +000035
36 if self.TraceOn():
37 print
38 print "Raw bytes: ", [hex(x) for x in raw_bytes]
39 print "Disassembled:", inst
Sean Callanan50952e92011-12-14 23:49:37 +000040
41if __name__ == '__main__':
42 import atexit
43 lldb.SBDebugger.Initialize()
44 atexit.register(lambda: lldb.SBDebugger.Terminate())
45 unittest2.main()