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 def setUp(self): 13 # Call super's setUp(). 14 TestBase.setUp(self) 15 # Find the line number to of function 'c'. 16 self.line1 = line_number( 17 "main.c", "// Find the line number for breakpoint 1 here." 18 ) 19 self.line2 = line_number( 20 "main.c", "// Find the line number for breakpoint 2 here." 21 ) 22 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( 38 breakpoint1 and breakpoint1.GetNumLocations() == 1, VALID_BREAKPOINT 39 ) 40 self.assertTrue( 41 breakpoint2 and breakpoint2.GetNumLocations() == 1, VALID_BREAKPOINT 42 ) 43 44 # Now launch the process, and do not stop at entry point. 45 process = target.LaunchSimple(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(process, lldb.eStopReasonBreakpoint) 51 self.assertTrue( 52 thread.IsValid(), 53 "There should be a thread stopped due to breakpoint condition", 54 ) 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 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(process, lldb.eStopReasonBreakpoint) 75 self.assertTrue( 76 thread.IsValid(), 77 "There should be a thread stopped due to breakpoint condition", 78 ) 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( 106 sa1 and sa2 and sa1 == sa2, "The two starting addresses should be the same" 107 ) 108 109 from lldbsuite.test.lldbutil import get_description 110 111 desc1 = get_description(sa1) 112 desc2 = get_description(sa2) 113 self.assertTrue( 114 desc1 and desc2 and desc1 == desc2, 115 "SBAddress.GetDescription() API of sa1 and sa2 should return the same string", 116 ) 117