Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test retrieval of SBAddress from function/symbol, disassembly, and SBAddress APIs. |
| 3 | """ |
| 4 | |
| 5 | import os, time |
| 6 | import re |
| 7 | import unittest2 |
| 8 | import lldb, lldbutil |
| 9 | from lldbtest import * |
| 10 | |
| 11 | class DisasmAPITestCase(TestBase): |
| 12 | |
Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 13 | mydir = TestBase.compute_mydir(__file__) |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 14 | |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 15 | def setUp(self): |
| 16 | # Call super's setUp(). |
| 17 | TestBase.setUp(self) |
| 18 | # Find the line number to of function 'c'. |
| 19 | self.line1 = line_number('main.c', '// Find the line number for breakpoint 1 here.') |
| 20 | self.line2 = line_number('main.c', '// Find the line number for breakpoint 2 here.') |
| 21 | |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame^] | 22 | @python_api_test |
| 23 | def test(self): |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 24 | """Exercise getting SBAddress objects, disassembly, and SBAddress APIs.""" |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame^] | 25 | self.build() |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 26 | exe = os.path.join(os.getcwd(), "a.out") |
| 27 | |
| 28 | # Create a target by the debugger. |
| 29 | target = self.dbg.CreateTarget(exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 30 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 31 | |
| 32 | # Now create the two breakpoints inside function 'a'. |
| 33 | breakpoint1 = target.BreakpointCreateByLocation('main.c', self.line1) |
| 34 | breakpoint2 = target.BreakpointCreateByLocation('main.c', self.line2) |
| 35 | #print "breakpoint1:", breakpoint1 |
| 36 | #print "breakpoint2:", breakpoint2 |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 37 | self.assertTrue(breakpoint1 and |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 38 | breakpoint1.GetNumLocations() == 1, |
| 39 | VALID_BREAKPOINT) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 40 | self.assertTrue(breakpoint2 and |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 41 | breakpoint2.GetNumLocations() == 1, |
| 42 | VALID_BREAKPOINT) |
| 43 | |
| 44 | # Now launch the process, and do not stop at entry point. |
Greg Clayton | c694751 | 2013-12-13 19:18:59 +0000 | [diff] [blame] | 45 | process = target.LaunchSimple (None, None, self.get_process_working_directory()) |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 46 | self.assertTrue(process, PROCESS_IS_VALID) |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 47 | |
| 48 | # Frame #0 should be on self.line1. |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 49 | self.assertTrue(process.GetState() == lldb.eStateStopped) |
| 50 | thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) |
Greg Clayton | 53c5ddf | 2013-03-19 17:59:30 +0000 | [diff] [blame] | 51 | self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint condition") |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 52 | frame0 = thread.GetFrameAtIndex(0) |
| 53 | lineEntry = frame0.GetLineEntry() |
| 54 | self.assertTrue(lineEntry.GetLine() == self.line1) |
| 55 | |
| 56 | address1 = lineEntry.GetStartAddress() |
| 57 | #print "address1:", address1 |
| 58 | |
| 59 | # Now call SBTarget.ResolveSymbolContextForAddress() with address1. |
| 60 | context1 = target.ResolveSymbolContextForAddress(address1, lldb.eSymbolContextEverything) |
| 61 | |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 62 | self.assertTrue(context1) |
Johnny Chen | 9efcb0e | 2011-04-19 19:44:26 +0000 | [diff] [blame] | 63 | if self.TraceOn(): |
| 64 | print "context1:", context1 |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 65 | |
| 66 | # Continue the inferior, the breakpoint 2 should be hit. |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 67 | process.Continue() |
| 68 | self.assertTrue(process.GetState() == lldb.eStateStopped) |
| 69 | thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) |
Greg Clayton | 53c5ddf | 2013-03-19 17:59:30 +0000 | [diff] [blame] | 70 | self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint condition") |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 71 | frame0 = thread.GetFrameAtIndex(0) |
| 72 | lineEntry = frame0.GetLineEntry() |
| 73 | self.assertTrue(lineEntry.GetLine() == self.line2) |
| 74 | |
| 75 | # Verify that the symbol and the function has the same address range per function 'a'. |
| 76 | symbol = context1.GetSymbol() |
| 77 | function = frame0.GetFunction() |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 78 | self.assertTrue(symbol and function) |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 79 | |
Johnny Chen | 9efcb0e | 2011-04-19 19:44:26 +0000 | [diff] [blame] | 80 | disasm_output = lldbutil.disassemble(target, symbol) |
| 81 | if self.TraceOn(): |
| 82 | print "symbol:", symbol |
| 83 | print "disassembly=>\n", disasm_output |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 84 | |
Johnny Chen | 9efcb0e | 2011-04-19 19:44:26 +0000 | [diff] [blame] | 85 | disasm_output = lldbutil.disassemble(target, function) |
| 86 | if self.TraceOn(): |
| 87 | print "function:", function |
| 88 | print "disassembly=>\n", disasm_output |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 89 | |
| 90 | sa1 = symbol.GetStartAddress() |
| 91 | #print "sa1:", sa1 |
Johnny Chen | 4b6fed4 | 2011-06-09 22:09:52 +0000 | [diff] [blame] | 92 | #print "sa1.GetFileAddress():", hex(sa1.GetFileAddress()) |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 93 | #ea1 = symbol.GetEndAddress() |
| 94 | #print "ea1:", ea1 |
| 95 | sa2 = function.GetStartAddress() |
| 96 | #print "sa2:", sa2 |
Johnny Chen | 4b6fed4 | 2011-06-09 22:09:52 +0000 | [diff] [blame] | 97 | #print "sa2.GetFileAddress():", hex(sa2.GetFileAddress()) |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 98 | #ea2 = function.GetEndAddress() |
| 99 | #print "ea2:", ea2 |
Johnny Chen | e2b5cfd | 2011-06-09 22:04:56 +0000 | [diff] [blame] | 100 | self.assertTrue(sa1 and sa2 and sa1 == sa2, |
| 101 | "The two starting addresses should be the same") |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 102 | |
Johnny Chen | 9ae9820 | 2011-04-23 00:34:56 +0000 | [diff] [blame] | 103 | from lldbutil import get_description |
| 104 | desc1 = get_description(sa1) |
| 105 | desc2 = get_description(sa2) |
| 106 | self.assertTrue(desc1 and desc2 and desc1 == desc2, |
Johnny Chen | e2b5cfd | 2011-06-09 22:04:56 +0000 | [diff] [blame] | 107 | "SBAddress.GetDescription() API of sa1 and sa2 should return the same string") |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 108 | |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 109 | if __name__ == '__main__': |
| 110 | import atexit |
| 111 | lldb.SBDebugger.Initialize() |
| 112 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 113 | unittest2.main() |