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