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