Sean Callanan | 50952e9 | 2011-12-14 23:49:37 +0000 | [diff] [blame] | 1 | """ |
| 2 | Use lldb Python API to disassemble raw machine code bytes |
| 3 | """ |
| 4 | |
| 5 | import os, time |
| 6 | import re |
| 7 | import unittest2 |
| 8 | import lldb, lldbutil |
| 9 | from lldbtest import * |
| 10 | |
| 11 | class DisassembleRawDataTestCase(TestBase): |
| 12 | |
Johnny Chen | 1917bc8 | 2011-12-15 19:56:28 +0000 | [diff] [blame] | 13 | mydir = os.path.join("python_api", "disassemble-raw-data") |
Sean Callanan | 50952e9 | 2011-12-14 23:49:37 +0000 | [diff] [blame] | 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-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 Chen | 1917bc8 | 2011-12-15 19:56:28 +0000 | [diff] [blame] | 33 | if self.TraceOn(): |
| 34 | print |
| 35 | print "Raw bytes: ", [hex(x) for x in raw_bytes] |
| 36 | print "Disassembled:", inst |
Sean Callanan | 50952e9 | 2011-12-14 23:49:37 +0000 | [diff] [blame] | 37 | |
Johnny Chen | e20e9ae | 2012-01-23 19:37:53 +0000 | [diff] [blame^] | 38 | self.assertTrue (inst.GetMnemonic(target) == "movq") |
| 39 | self.assertTrue (inst.GetOperands(target) == '%' + "rsp, " + '%' + "rbp") |
| 40 | |
Sean Callanan | 50952e9 | 2011-12-14 23:49:37 +0000 | [diff] [blame] | 41 | if __name__ == '__main__': |
| 42 | import atexit |
| 43 | lldb.SBDebugger.Initialize() |
| 44 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 45 | unittest2.main() |