blob: 44fc3c381205c98360d7ce28023001826464e4c9 [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
Johnny Chen1917bc82011-12-15 19:56:28 +000033 if self.TraceOn():
34 print
35 print "Raw bytes: ", [hex(x) for x in raw_bytes]
36 print "Disassembled:", inst
Sean Callanan50952e92011-12-14 23:49:37 +000037
Johnny Chene20e9ae2012-01-23 19:37:53 +000038 self.assertTrue (inst.GetMnemonic(target) == "movq")
39 self.assertTrue (inst.GetOperands(target) == '%' + "rsp, " + '%' + "rbp")
40
Sean Callanan50952e92011-12-14 23:49:37 +000041if __name__ == '__main__':
42 import atexit
43 lldb.SBDebugger.Initialize()
44 atexit.register(lambda: lldb.SBDebugger.Terminate())
45 unittest2.main()