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