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 |
| 17 | def test_with_dsym(self): |
| 18 | """Exercise getting SBAddress objects, disassembly, and SBAddress APIs.""" |
| 19 | self.buildDsym() |
| 20 | self.disasm_and_address_api() |
| 21 | |
| 22 | @python_api_test |
| 23 | def test_with_dwarf(self): |
| 24 | """Exercise getting SBAddress objects, disassembly, and SBAddress APIs.""" |
| 25 | self.buildDwarf() |
| 26 | self.disasm_and_address_api() |
| 27 | |
| 28 | def setUp(self): |
| 29 | # Call super's setUp(). |
| 30 | TestBase.setUp(self) |
| 31 | # Find the line number to of function 'c'. |
| 32 | self.line1 = line_number('main.c', '// Find the line number for breakpoint 1 here.') |
| 33 | self.line2 = line_number('main.c', '// Find the line number for breakpoint 2 here.') |
| 34 | |
| 35 | def disasm_and_address_api(self): |
| 36 | """Exercise getting SBAddress objects, disassembly, and SBAddress APIs.""" |
| 37 | exe = os.path.join(os.getcwd(), "a.out") |
| 38 | |
| 39 | # Create a target by the debugger. |
| 40 | target = self.dbg.CreateTarget(exe) |
| 41 | self.assertTrue(target.IsValid(), VALID_TARGET) |
| 42 | |
| 43 | # Now create the two breakpoints inside function 'a'. |
| 44 | breakpoint1 = target.BreakpointCreateByLocation('main.c', self.line1) |
| 45 | breakpoint2 = target.BreakpointCreateByLocation('main.c', self.line2) |
| 46 | #print "breakpoint1:", breakpoint1 |
| 47 | #print "breakpoint2:", breakpoint2 |
| 48 | self.assertTrue(breakpoint1.IsValid() and |
| 49 | breakpoint1.GetNumLocations() == 1, |
| 50 | VALID_BREAKPOINT) |
| 51 | self.assertTrue(breakpoint2.IsValid() and |
| 52 | breakpoint2.GetNumLocations() == 1, |
| 53 | VALID_BREAKPOINT) |
| 54 | |
| 55 | # Now launch the process, and do not stop at entry point. |
Johnny Chen | 9efcb0e | 2011-04-19 19:44:26 +0000 | [diff] [blame] | 56 | self.process = target.LaunchSimple(None, None, os.getcwd()) |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 57 | |
| 58 | self.process = target.GetProcess() |
| 59 | self.assertTrue(self.process.IsValid(), PROCESS_IS_VALID) |
| 60 | |
| 61 | # Frame #0 should be on self.line1. |
| 62 | self.assertTrue(self.process.GetState() == lldb.eStateStopped) |
| 63 | thread = lldbutil.get_stopped_thread(self.process, lldb.eStopReasonBreakpoint) |
| 64 | self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition") |
| 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 | |
| 75 | self.assertTrue(context1.IsValid()) |
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. |
| 80 | self.process.Continue() |
| 81 | self.assertTrue(self.process.GetState() == lldb.eStateStopped) |
| 82 | thread = lldbutil.get_stopped_thread(self.process, lldb.eStopReasonBreakpoint) |
| 83 | self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition") |
| 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() |
| 91 | self.assertTrue(symbol.IsValid() and function.IsValid()) |
| 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 |
| 105 | #ea1 = symbol.GetEndAddress() |
| 106 | #print "ea1:", ea1 |
| 107 | sa2 = function.GetStartAddress() |
| 108 | #print "sa2:", sa2 |
| 109 | #ea2 = function.GetEndAddress() |
| 110 | #print "ea2:", ea2 |
| 111 | |
Johnny Chen | 9ae9820 | 2011-04-23 00:34:56 +0000 | [diff] [blame^] | 112 | from lldbutil import get_description |
| 113 | desc1 = get_description(sa1) |
| 114 | desc2 = get_description(sa2) |
| 115 | self.assertTrue(desc1 and desc2 and desc1 == desc2, |
| 116 | "The two starting addresses should be the same") |
Johnny Chen | ed40198 | 2011-03-03 19:14:00 +0000 | [diff] [blame] | 117 | |
| 118 | |
| 119 | if __name__ == '__main__': |
| 120 | import atexit |
| 121 | lldb.SBDebugger.Initialize() |
| 122 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 123 | unittest2.main() |