blob: 4a9cd7a258db06102a1e103140288fad1441e9ee [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
Greg Clayton4570d3e2013-12-10 23:19:29 +000013 mydir = TestBase.compute_mydir(__file__)
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
Greg Clayton70512312012-05-08 01:45:38 +000024 target = self.dbg.CreateTargetWithFileAndTargetTriple ("", "x86_64")
Sean Callanan50952e92011-12-14 23:49:37 +000025 self.assertTrue(target, VALID_TARGET)
26
27 raw_bytes = bytearray([0x48, 0x89, 0xe5])
28
Greg Clayton199e7c12015-01-27 00:22:36 +000029 insts = target.GetInstructions(lldb.SBAddress(0, target), raw_bytes)
Sean Callanan50952e92011-12-14 23:49:37 +000030
31 inst = insts.GetInstructionAtIndex(0)
32
Johnny Chen1917bc82011-12-15 19:56:28 +000033 if self.TraceOn():
34 print
Johnny Chenf087c822012-03-15 18:23:59 +000035 print "Raw bytes: ", [hex(x) for x in raw_bytes]
36 print "Disassembled%s" % str(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()