Johnny Chen | df2963e | 2011-04-01 00:35:55 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test newly added SBSymbol 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 SymbolAPITestCase(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 some SBSymbol and SBAddress APIs.""" |
| 19 | self.buildDsym() |
| 20 | self.symbol_and_address_api() |
| 21 | |
| 22 | @python_api_test |
| 23 | def test_with_dwarf(self): |
| 24 | """Exercise some SBSymbol and SBAddress APIs.""" |
| 25 | self.buildDwarf() |
| 26 | self.symbol_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 symbol_and_address_api(self): |
| 36 | """Exercise some SBSymbol 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) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 41 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | df2963e | 2011-04-01 00:35:55 +0000 | [diff] [blame] | 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 |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 48 | self.assertTrue(breakpoint1 and |
Johnny Chen | df2963e | 2011-04-01 00:35:55 +0000 | [diff] [blame] | 49 | breakpoint1.GetNumLocations() == 1, |
| 50 | VALID_BREAKPOINT) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 51 | self.assertTrue(breakpoint2 and |
Johnny Chen | df2963e | 2011-04-01 00:35:55 +0000 | [diff] [blame] | 52 | breakpoint2.GetNumLocations() == 1, |
| 53 | VALID_BREAKPOINT) |
| 54 | |
| 55 | # Now launch the process, and do not stop at entry point. |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 56 | process = target.LaunchSimple(None, None, os.getcwd()) |
| 57 | self.assertTrue(process, PROCESS_IS_VALID) |
Johnny Chen | df2963e | 2011-04-01 00:35:55 +0000 | [diff] [blame] | 58 | |
| 59 | # Frame #0 should be on self.line1. |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 60 | self.assertTrue(process.GetState() == lldb.eStateStopped) |
| 61 | thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) |
Johnny Chen | df2963e | 2011-04-01 00:35:55 +0000 | [diff] [blame] | 62 | self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition") |
| 63 | frame0 = thread.GetFrameAtIndex(0) |
| 64 | symbol_line1 = frame0.GetSymbol() |
| 65 | # We should have a symbol type of code. |
| 66 | self.assertTrue(symbol_line1.GetType() == lldb.eSymbolTypeCode) |
| 67 | addr_line1 = symbol_line1.GetStartAddress() |
| 68 | # And a section type of code, too. |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 69 | self.assertTrue(addr_line1.GetSection().GetSectionType() == lldb.eSectionTypeCode) |
Johnny Chen | df2963e | 2011-04-01 00:35:55 +0000 | [diff] [blame] | 70 | |
| 71 | # Continue the inferior, the breakpoint 2 should be hit. |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 72 | process.Continue() |
| 73 | self.assertTrue(process.GetState() == lldb.eStateStopped) |
| 74 | thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) |
Johnny Chen | df2963e | 2011-04-01 00:35:55 +0000 | [diff] [blame] | 75 | self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition") |
| 76 | frame0 = thread.GetFrameAtIndex(0) |
| 77 | symbol_line2 = frame0.GetSymbol() |
| 78 | # We should have a symbol type of code. |
| 79 | self.assertTrue(symbol_line2.GetType() == lldb.eSymbolTypeCode) |
| 80 | addr_line2 = symbol_line2.GetStartAddress() |
| 81 | # And a section type of code, too. |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 82 | self.assertTrue(addr_line2.GetSection().GetSectionType() == lldb.eSectionTypeCode) |
Johnny Chen | df2963e | 2011-04-01 00:35:55 +0000 | [diff] [blame] | 83 | |
| 84 | # Now verify that both addresses point to the same module. |
Johnny Chen | 9efcb0e | 2011-04-19 19:44:26 +0000 | [diff] [blame] | 85 | if self.TraceOn(): |
| 86 | print "UUID:", addr_line1.GetModule().GetUUIDString() |
Johnny Chen | df2963e | 2011-04-01 00:35:55 +0000 | [diff] [blame] | 87 | self.assertTrue(addr_line1.GetModule().GetUUIDString() == addr_line2.GetModule().GetUUIDString()) |
| 88 | |
| 89 | |
| 90 | if __name__ == '__main__': |
| 91 | import atexit |
| 92 | lldb.SBDebugger.Initialize() |
| 93 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 94 | unittest2.main() |