1""" 2Test newly added SBSymbol and SBAddress APIs. 3""" 4 5import os, time 6import re 7import unittest2 8import lldb, lldbutil 9from lldbtest import * 10 11class 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 @dsym_test 18 def test_with_dsym(self): 19 """Exercise some SBSymbol and SBAddress APIs.""" 20 self.buildDsym() 21 self.symbol_and_address_api() 22 23 @python_api_test 24 @dwarf_test 25 def test_with_dwarf(self): 26 """Exercise some SBSymbol and SBAddress APIs.""" 27 self.buildDwarf() 28 self.symbol_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 symbol_and_address_api(self): 38 """Exercise some SBSymbol 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) 43 self.assertTrue(target, VALID_TARGET) 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 50 self.assertTrue(breakpoint1 and 51 breakpoint1.GetNumLocations() == 1, 52 VALID_BREAKPOINT) 53 self.assertTrue(breakpoint2 and 54 breakpoint2.GetNumLocations() == 1, 55 VALID_BREAKPOINT) 56 57 # Now launch the process, and do not stop at entry point. 58 process = target.LaunchSimple(None, None, os.getcwd()) 59 self.assertTrue(process, PROCESS_IS_VALID) 60 61 # Frame #0 should be on self.line1. 62 self.assertTrue(process.GetState() == lldb.eStateStopped) 63 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) 64 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint condition") 65 frame0 = thread.GetFrameAtIndex(0) 66 symbol_line1 = frame0.GetSymbol() 67 # We should have a symbol type of code. 68 self.assertTrue(symbol_line1.GetType() == lldb.eSymbolTypeCode) 69 addr_line1 = symbol_line1.GetStartAddress() 70 # And a section type of code, too. 71 self.assertTrue(addr_line1.GetSection().GetSectionType() == lldb.eSectionTypeCode) 72 73 # Continue the inferior, the breakpoint 2 should be hit. 74 process.Continue() 75 self.assertTrue(process.GetState() == lldb.eStateStopped) 76 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) 77 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint condition") 78 frame0 = thread.GetFrameAtIndex(0) 79 symbol_line2 = frame0.GetSymbol() 80 # We should have a symbol type of code. 81 self.assertTrue(symbol_line2.GetType() == lldb.eSymbolTypeCode) 82 addr_line2 = symbol_line2.GetStartAddress() 83 # And a section type of code, too. 84 self.assertTrue(addr_line2.GetSection().GetSectionType() == lldb.eSectionTypeCode) 85 86 # Now verify that both addresses point to the same module. 87 if self.TraceOn(): 88 print "UUID:", addr_line1.GetModule().GetUUIDString() 89 self.assertTrue(addr_line1.GetModule().GetUUIDString() == addr_line2.GetModule().GetUUIDString()) 90 91 92if __name__ == '__main__': 93 import atexit 94 lldb.SBDebugger.Initialize() 95 atexit.register(lambda: lldb.SBDebugger.Terminate()) 96 unittest2.main() 97