def test_lldb_iter_module(self): """Test module_iter works correctly for SBTarget -> SBModule.""" self.build() exe = os.path.join(os.getcwd(), "a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line1) self.assertTrue(breakpoint, VALID_BREAKPOINT) # Now launch the process, and do not stop at entry point. process = target.LaunchSimple( None, None, self.get_process_working_directory()) if not process: self.fail("SBTarget.LaunchProcess() failed") from lldbsuite.test.lldbutil import get_description yours = [] for i in range(target.GetNumModules()): yours.append(target.GetModuleAtIndex(i)) mine = [] for m in target.module_iter(): mine.append(m) self.assertTrue(len(yours) == len(mine)) for i in range(len(yours)): if self.TraceOn(): print("yours[%d]='%s'" % (i, get_description(yours[i]))) print("mine[%d]='%s'" % (i, get_description(mine[i]))) self.assertTrue( yours[i] == mine[i], "UUID+FileSpec of yours[{0}] and mine[{0}] matches".format(i))
def test_lldb_iter_breakpoint(self): """Test breakpoint_iter works correctly for SBTarget -> SBBreakpoint.""" self.build() exe = os.path.join(os.getcwd(), "a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line1) self.assertTrue(breakpoint, VALID_BREAKPOINT) breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line2) self.assertTrue(breakpoint, VALID_BREAKPOINT) self.assertTrue(target.GetNumBreakpoints() == 2) from lldbsuite.test.lldbutil import get_description yours = [] for i in range(target.GetNumBreakpoints()): yours.append(target.GetBreakpointAtIndex(i)) mine = [] for b in target.breakpoint_iter(): mine.append(b) self.assertTrue(len(yours) == len(mine)) for i in range(len(yours)): if self.TraceOn(): print("yours[%d]='%s'" % (i, get_description(yours[i]))) print("mine[%d]='%s'" % (i, get_description(mine[i]))) self.assertTrue(yours[i] == mine[i], "ID of yours[{0}] and mine[{0}] matches".format(i))
def get_description(self): """Exercise SBTarget.GetDescription() API.""" exe = self.getBuildArtifact("a.out") # Create a target by the debugger. target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) from lldbsuite.test.lldbutil import get_description # get_description() allows no option to mean # lldb.eDescriptionLevelBrief. desc = get_description(target) #desc = get_description(target, option=lldb.eDescriptionLevelBrief) if not desc: self.fail("SBTarget.GetDescription() failed") self.expect(desc, exe=False, substrs=['a.out']) self.expect(desc, exe=False, matching=False, substrs=['Target', 'Module', 'Breakpoint']) desc = get_description(target, option=lldb.eDescriptionLevelFull) if not desc: self.fail("SBTarget.GetDescription() failed") self.expect(desc, exe=False, substrs=['Target', 'Module', 'a.out', 'Breakpoint'])
def get_description(self): """Exercise SBTaget.GetDescription() API.""" exe = os.path.join(os.getcwd(), "a.out") # Create a target by the debugger. target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) from lldbsuite.test.lldbutil import get_description # get_description() allows no option to mean # lldb.eDescriptionLevelBrief. desc = get_description(target) #desc = get_description(target, option=lldb.eDescriptionLevelBrief) if not desc: self.fail("SBTarget.GetDescription() failed") self.expect(desc, exe=False, substrs=['a.out']) self.expect(desc, exe=False, matching=False, substrs=['Target', 'Module', 'Breakpoint']) desc = get_description(target, option=lldb.eDescriptionLevelFull) if not desc: self.fail("SBTarget.GetDescription() failed") self.expect(desc, exe=False, substrs=['a.out', 'Target', 'Module', 'Breakpoint'])
def describe_threads(self): ret = [] for x in self.inferior_process: id = x.GetIndexID() reason = x.GetStopReason() status = "stopped" if x.IsStopped() else "running" reason_str = lldbutil.stop_reason_to_str(reason) if reason == lldb.eStopReasonBreakpoint: bpid = x.GetStopReasonDataAtIndex(0) bp = self.inferior_target.FindBreakpointByID(bpid) reason_str = "%s hit %d times" % ( lldbutil.get_description(bp), bp.GetHitCount()) elif reason == lldb.eStopReasonWatchpoint: watchid = x.GetStopReasonDataAtIndex(0) watch = self.inferior_target.FindWatchpointByID(watchid) reason_str = "%s hit %d times" % ( lldbutil.get_description(watch), watch.GetHitCount()) elif reason == lldb.eStopReasonSignal: signals = self.inferior_process.GetUnixSignals() signal_name = signals.GetSignalAsCString( x.GetStopReasonDataAtIndex(0)) reason_str = "signal %s" % signal_name location = "\t".join([lldbutil.get_description( x.GetFrameAtIndex(i)) for i in range(x.GetNumFrames())]) ret.append( "thread %d %s due to %s at\n\t%s" % (id, status, reason_str, location)) return ret
def test_lldb_iter_module(self): """Test module_iter works correctly for SBTarget -> SBModule.""" self.build() exe = self.getBuildArtifact("a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line1) self.assertTrue(breakpoint, VALID_BREAKPOINT) # Now launch the process, and do not stop at entry point. process = target.LaunchSimple( None, None, self.get_process_working_directory()) if not process: self.fail("SBTarget.LaunchProcess() failed") from lldbsuite.test.lldbutil import get_description yours = [] for i in range(target.GetNumModules()): yours.append(target.GetModuleAtIndex(i)) mine = [] for m in target.module_iter(): mine.append(m) self.assertTrue(len(yours) == len(mine)) for i in range(len(yours)): if self.TraceOn(): print("yours[%d]='%s'" % (i, get_description(yours[i]))) print("mine[%d]='%s'" % (i, get_description(mine[i]))) self.assertTrue( yours[i] == mine[i], "UUID+FileSpec of yours[{0}] and mine[{0}] matches".format(i))
def test_lldb_iter_breakpoint(self): """Test breakpoint_iter works correctly for SBTarget -> SBBreakpoint.""" self.build() exe = self.getBuildArtifact("a.out") target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line1) self.assertTrue(breakpoint, VALID_BREAKPOINT) breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line2) self.assertTrue(breakpoint, VALID_BREAKPOINT) self.assertTrue(target.GetNumBreakpoints() == 2) from lldbsuite.test.lldbutil import get_description yours = [] for i in range(target.GetNumBreakpoints()): yours.append(target.GetBreakpointAtIndex(i)) mine = [] for b in target.breakpoint_iter(): mine.append(b) self.assertTrue(len(yours) == len(mine)) for i in range(len(yours)): if self.TraceOn(): print("yours[%d]='%s'" % (i, get_description(yours[i]))) print("mine[%d]='%s'" % (i, get_description(mine[i]))) self.assertTrue(yours[i] == mine[i], "ID of yours[{0}] and mine[{0}] matches".format(i))
def test_unused_inlined_parameters(self): self.build() lldbutil.run_to_source_breakpoint(self, "// break here", lldb.SBFileSpec("main.c")) # For the unused parameters, only check the types. self.assertIn( "(void *) unused1 = <no location, value may have been optimized out>", lldbutil.get_description(self.frame().FindVariable("unused1"))) self.assertEqual( 42, self.frame().FindVariable("used").GetValueAsUnsigned()) self.assertIn( "(int) unused2 = <no location, value may have been optimized out>", lldbutil.get_description(self.frame().FindVariable("unused2")))
def run(self): event = lldb.SBEvent() try: timeout_count = 0 # Wait up to 4 times for the event to arrive. while not self.done(timeout_count): if self.trace_on: print("Calling wait for event...") if self.listener.WaitForEvent(self.wait_seconds, event): while event.IsValid(): # Check if it's a process event. if SBProcess.EventIsStructuredDataEvent(event): self.handle_structured_data_event(event) else: if self.trace_on: print("ignoring unexpected event:", lldbutil.get_description(event)) # Grab the next event, if there is one. event.Clear() if not self.listener.GetNextEvent(event): if self.trace_on: print("listener has no more events " "available at this time") else: if self.trace_on: print("timeout occurred waiting for event...") timeout_count += 1 self.listener.Clear() except Exception as e: self.exception = e
def step_out_with_python(self): self.step_out_thread.StepOut() reason = self.step_out_thread.GetStopReason() self.assertEqual(lldb.eStopReasonPlanComplete, reason, "Expected thread stop reason 'plancomplete', but got '%s'" % lldbutil.stop_reason_to_str(reason)) # Verify location after stepping out frame = self.step_out_thread.GetFrameAtIndex(0) desc = lldbutil.get_description(frame.GetLineEntry()) expect = "main.cpp:%d" % self.step_out_destination self.assertTrue(expect in desc, "Expected %s but thread stopped at %s" % (expect, desc))
def describe_threads(self): desc = [] for x in self.inferior_process: id = x.GetIndexID() reason_str = lldbutil.stop_reason_to_str(x.GetStopReason()) location = "\t".join([lldbutil.get_description( x.GetFrameAtIndex(i)) for i in range(x.GetNumFrames())]) desc.append( "thread %d: %s (queue id: %s) at\n\t%s" % (id, reason_str, x.GetQueueID(), location)) print('\n'.join(desc))
def step_out_with_python(self): self.step_out_thread.StepOut() reason = self.step_out_thread.GetStopReason() self.assertEqual( lldb.eStopReasonPlanComplete, reason, "Expected thread stop reason 'plancomplete', but got '%s'" % lldbutil.stop_reason_to_str(reason)) # Verify location after stepping out frame = self.step_out_thread.GetFrameAtIndex(0) desc = lldbutil.get_description(frame.GetLineEntry()) expect = "main.cpp:%d" % self.step_out_destination self.assertTrue( expect in desc, "Expected %s but thread stopped at %s" % (expect, desc))
def run(self): count = 0 # Let's only try at most 4 times to retrieve any kind of event. # After that, the thread exits. while not count > 3: if traceOn: print("Try wait for event...") if listener.WaitForEvent(5, event): if traceOn: desc = lldbutil.get_description(event) print("Event description:", desc) print("Event data flavor:", event.GetDataFlavor()) print("Process state:", lldbutil.state_type_to_str(process.GetState())) print() else: if traceOn: print("timeout occurred waiting for event...") count = count + 1 return
def run(self): #print("Running MyListeningThread:", self) # Regular expression pattern for the event description. pattern = re.compile("data = {.*, state = (.*)}$") # Let's only try at most 6 times to retrieve our events. count = 0 while True: if listener.WaitForEvent(5, event): desc = lldbutil.get_description(event) #print("Event description:", desc) match = pattern.search(desc) if not match: break if match.group(1) == 'connected': # When debugging remote targets with lldb-server, we # first get the 'connected' event. self.context.assertTrue(self.context.state is None) self.context.state = 'connected' continue elif match.group(1) == 'running': self.context.assertTrue( self.context.state is None or self.context.state == 'connected') self.context.state = 'running' continue elif match.group(1) == 'stopped': self.context.assertTrue( self.context.state == 'running') # Whoopee, both events have been received! self.context.state = 'stopped' break else: break print("Timeout: listener.WaitForEvent") count = count + 1 if count > 6: break listener.Clear() return
def test(self): """Exercise getting SBAddress objects, disassembly, and SBAddress APIs.""" self.build() exe = self.getBuildArtifact("a.out") # Create a target by the debugger. target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Now create the two breakpoints inside function 'a'. breakpoint1 = target.BreakpointCreateByLocation('main.c', self.line1) breakpoint2 = target.BreakpointCreateByLocation('main.c', self.line2) #print("breakpoint1:", breakpoint1) #print("breakpoint2:", breakpoint2) self.assertTrue(breakpoint1 and breakpoint1.GetNumLocations() == 1, VALID_BREAKPOINT) self.assertTrue(breakpoint2 and breakpoint2.GetNumLocations() == 1, VALID_BREAKPOINT) # Now launch the process, and do not stop at entry point. process = target.LaunchSimple( None, None, self.get_process_working_directory()) self.assertTrue(process, PROCESS_IS_VALID) # Frame #0 should be on self.line1. self.assertTrue(process.GetState() == lldb.eStateStopped) thread = lldbutil.get_stopped_thread( process, lldb.eStopReasonBreakpoint) self.assertTrue( thread.IsValid(), "There should be a thread stopped due to breakpoint condition") frame0 = thread.GetFrameAtIndex(0) lineEntry = frame0.GetLineEntry() self.assertTrue(lineEntry.GetLine() == self.line1) address1 = lineEntry.GetStartAddress() #print("address1:", address1) # Now call SBTarget.ResolveSymbolContextForAddress() with address1. context1 = target.ResolveSymbolContextForAddress( address1, lldb.eSymbolContextEverything) self.assertTrue(context1) if self.TraceOn(): print("context1:", context1) # Continue the inferior, the breakpoint 2 should be hit. process.Continue() self.assertTrue(process.GetState() == lldb.eStateStopped) thread = lldbutil.get_stopped_thread( process, lldb.eStopReasonBreakpoint) self.assertTrue( thread.IsValid(), "There should be a thread stopped due to breakpoint condition") frame0 = thread.GetFrameAtIndex(0) lineEntry = frame0.GetLineEntry() self.assertTrue(lineEntry.GetLine() == self.line2) # Verify that the symbol and the function has the same address range # per function 'a'. symbol = context1.GetSymbol() function = frame0.GetFunction() self.assertTrue(symbol and function) disasm_output = lldbutil.disassemble(target, symbol) if self.TraceOn(): print("symbol:", symbol) print("disassembly=>\n", disasm_output) disasm_output = lldbutil.disassemble(target, function) if self.TraceOn(): print("function:", function) print("disassembly=>\n", disasm_output) sa1 = symbol.GetStartAddress() #print("sa1:", sa1) #print("sa1.GetFileAddress():", hex(sa1.GetFileAddress())) #ea1 = symbol.GetEndAddress() #print("ea1:", ea1) sa2 = function.GetStartAddress() #print("sa2:", sa2) #print("sa2.GetFileAddress():", hex(sa2.GetFileAddress())) #ea2 = function.GetEndAddress() #print("ea2:", ea2) self.assertTrue(sa1 and sa2 and sa1 == sa2, "The two starting addresses should be the same") from lldbsuite.test.lldbutil import get_description desc1 = get_description(sa1) desc2 = get_description(sa2) self.assertTrue( desc1 and desc2 and desc1 == desc2, "SBAddress.GetDescription() API of sa1 and sa2 should return the same string")
def test_watch_iter(self): """Exercise SBTarget.watchpoint_iter() API to iterate on the available watchpoints.""" self.build() exe = self.getBuildArtifact("a.out") # Create a target by the debugger. target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Create a breakpoint on main.c in order to set our watchpoint later. breakpoint = target.BreakpointCreateByLocation(self.source, self.line) self.assertTrue(breakpoint and breakpoint.GetNumLocations() == 1, VALID_BREAKPOINT) # Now launch the process, and do not stop at the entry point. process = target.LaunchSimple(None, None, self.get_process_working_directory()) # We should be stopped due to the breakpoint. Get frame #0. process = target.GetProcess() self.assertEqual(process.GetState(), lldb.eStateStopped, PROCESS_STOPPED) thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) frame0 = thread.GetFrameAtIndex(0) # Watch 'global' for read and write. value = frame0.FindValue('global', lldb.eValueTypeVariableGlobal) error = lldb.SBError() watchpoint = value.Watch(True, False, True, error) self.assertTrue( value and watchpoint, "Successfully found the variable and set a watchpoint") self.DebugSBValue(value) # Hide stdout if not running with '-t' option. if not self.TraceOn(): self.HideStdout() # There should be only 1 watchpoint location under the target. self.assertEqual(target.GetNumWatchpoints(), 1) self.assertTrue(watchpoint.IsEnabled()) watch_id = watchpoint.GetID() self.assertTrue(watch_id != 0) # Continue. Expect the program to stop due to the variable being # written to. process.Continue() # Hide stdout if not running with '-t' option. if not self.TraceOn(): self.HideStdout() # Print the stack traces. lldbutil.print_stacktraces(process) thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonWatchpoint) self.assertTrue(thread, "The thread stopped due to watchpoint") self.DebugSBValue(value) # We currently only support hardware watchpoint. Verify that we have a # meaningful hardware index at this point. Exercise the printed repr of # SBWatchpointLocation. print(watchpoint) if not self.affected_by_radar_34564183(): self.assertTrue(watchpoint.GetHardwareIndex() != -1) # SBWatchpoint.GetDescription() takes a description level arg. print(lldbutil.get_description(watchpoint, lldb.eDescriptionLevelFull)) # Now disable the 'rw' watchpoint. The program won't stop when it reads # 'global' next. watchpoint.SetEnabled(False) self.assertEqual(watchpoint.GetHardwareIndex(), -1) self.assertFalse(watchpoint.IsEnabled()) # Continue. The program does not stop again when the variable is being # read from because the watchpoint location has been disabled. process.Continue() # At this point, the inferior process should have exited. self.assertEqual(process.GetState(), lldb.eStateExited, PROCESS_EXITED) # Verify some vital statistics and exercise the iterator API. for watchpoint in target.watchpoint_iter(): self.assertTrue(watchpoint) self.assertEqual(watchpoint.GetWatchSize(), 4) self.assertEqual(watchpoint.GetHitCount(), 1) print(watchpoint)
def test_print_obj(self): """ Test "print object" where another thread blocks the print object from making progress. Set a breakpoint on the line in my_pthread_routine. Then switch threads to the main thread, and do print the lock_me object. Since that will try to get the lock already gotten by my_pthread_routime thread, it will have to switch to running all threads, and that should then succeed. """ d = {'EXE': 'b.out'} self.build(dictionary=d) self.setTearDownCleanup(dictionary=d) exe = self.getBuildArtifact('b.out') target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) breakpoint = target.BreakpointCreateByLocation(self.source, self.line) self.assertTrue(breakpoint, VALID_BREAKPOINT) self.runCmd("breakpoint list") # Launch the process, and do not stop at the entry point. process = target.LaunchSimple( None, None, self.get_process_working_directory()) self.runCmd("thread backtrace all") # Let's get the current stopped thread. We'd like to switch to the # other thread to issue our 'po lock_me' command. import lldbsuite.test.lldbutil as lldbutil this_thread = lldbutil.get_stopped_thread( process, lldb.eStopReasonBreakpoint) self.assertTrue(this_thread) # Find the other thread. The iteration protocol of SBProcess and the # rich comparison methods (__eq__/__ne__) of SBThread come in handy. other_thread = None for t in process: if t != this_thread: other_thread = t break # Set the other thread as the selected thread to issue our 'po' # command.other self.assertTrue(other_thread) process.SetSelectedThread(other_thread) if self.TraceOn(): print("selected thread:" + lldbutil.get_description(other_thread)) self.runCmd("thread backtrace") # We want to traverse the frame to the one corresponding to blocked.m to # issue our 'po lock_me' command. depth = other_thread.GetNumFrames() for i in range(depth): frame = other_thread.GetFrameAtIndex(i) name = frame.GetFunctionName() if name == 'main': other_thread.SetSelectedFrame(i) if self.TraceOn(): print("selected frame:" + lldbutil.get_description(frame)) break self.expect("po lock_me", OBJECT_PRINTED_CORRECTLY, substrs=['I am pretty special.'])
def test_watch_iter(self): """Exercise SBTarget.watchpoint_iter() API to iterate on the available watchpoints.""" self.build() exe = os.path.join(os.getcwd(), "a.out") # Create a target by the debugger. target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Create a breakpoint on main.c in order to set our watchpoint later. breakpoint = target.BreakpointCreateByLocation(self.source, self.line) self.assertTrue(breakpoint and breakpoint.GetNumLocations() == 1, VALID_BREAKPOINT) # Now launch the process, and do not stop at the entry point. process = target.LaunchSimple (None, None, self.get_process_working_directory()) # We should be stopped due to the breakpoint. Get frame #0. process = target.GetProcess() self.assertTrue(process.GetState() == lldb.eStateStopped, PROCESS_STOPPED) thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) frame0 = thread.GetFrameAtIndex(0) # Watch 'global' for read and write. value = frame0.FindValue('global', lldb.eValueTypeVariableGlobal) error = lldb.SBError(); watchpoint = value.Watch(True, True, True, error) self.assertTrue(value and watchpoint, "Successfully found the variable and set a watchpoint") self.DebugSBValue(value) # Hide stdout if not running with '-t' option. if not self.TraceOn(): self.HideStdout() # There should be only 1 watchpoint location under the target. self.assertTrue(target.GetNumWatchpoints() == 1) self.assertTrue(watchpoint.IsEnabled()) watch_id = watchpoint.GetID() self.assertTrue(watch_id != 0) # Continue. Expect the program to stop due to the variable being written to. process.Continue() # Hide stdout if not running with '-t' option. if not self.TraceOn(): self.HideStdout() # Print the stack traces. lldbutil.print_stacktraces(process) thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonWatchpoint) self.assertTrue(thread, "The thread stopped due to watchpoint") self.DebugSBValue(value) # We currently only support hardware watchpoint. Verify that we have a # meaningful hardware index at this point. Exercise the printed repr of # SBWatchpointLocation. print(watchpoint) self.assertTrue(watchpoint.GetHardwareIndex() != -1) # SBWatchpoint.GetDescription() takes a description level arg. print(lldbutil.get_description(watchpoint, lldb.eDescriptionLevelFull)) # Now disable the 'rw' watchpoint. The program won't stop when it reads # 'global' next. watchpoint.SetEnabled(False) self.assertTrue(watchpoint.GetHardwareIndex() == -1) self.assertFalse(watchpoint.IsEnabled()) # Continue. The program does not stop again when the variable is being # read from because the watchpoint location has been disabled. process.Continue() # At this point, the inferior process should have exited. self.assertTrue(process.GetState() == lldb.eStateExited, PROCESS_EXITED) # Verify some vital statistics and exercise the iterator API. for watchpoint in target.watchpoint_iter(): self.assertTrue(watchpoint) self.assertTrue(watchpoint.GetWatchSize() == 4) self.assertTrue(watchpoint.GetHitCount() == 1) print(watchpoint)
def resolve_symbol_context_with_address(self): """Exercise SBTaget.ResolveSymbolContextForAddress() API.""" exe = os.path.join(os.getcwd(), "a.out") # Create a target by the debugger. target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Now create the two breakpoints inside function 'a'. breakpoint1 = target.BreakpointCreateByLocation('main.c', self.line1) breakpoint2 = target.BreakpointCreateByLocation('main.c', self.line2) #print("breakpoint1:", breakpoint1) #print("breakpoint2:", breakpoint2) self.assertTrue(breakpoint1 and breakpoint1.GetNumLocations() == 1, VALID_BREAKPOINT) self.assertTrue(breakpoint2 and breakpoint2.GetNumLocations() == 1, VALID_BREAKPOINT) # Now launch the process, and do not stop at entry point. process = target.LaunchSimple( None, None, self.get_process_working_directory()) self.assertTrue(process, PROCESS_IS_VALID) # Frame #0 should be on self.line1. self.assertTrue(process.GetState() == lldb.eStateStopped) thread = lldbutil.get_stopped_thread( process, lldb.eStopReasonBreakpoint) self.assertTrue( thread.IsValid(), "There should be a thread stopped due to breakpoint condition") #self.runCmd("process status") frame0 = thread.GetFrameAtIndex(0) lineEntry = frame0.GetLineEntry() self.assertTrue(lineEntry.GetLine() == self.line1) address1 = lineEntry.GetStartAddress() # Continue the inferior, the breakpoint 2 should be hit. process.Continue() self.assertTrue(process.GetState() == lldb.eStateStopped) thread = lldbutil.get_stopped_thread( process, lldb.eStopReasonBreakpoint) self.assertTrue( thread.IsValid(), "There should be a thread stopped due to breakpoint condition") #self.runCmd("process status") frame0 = thread.GetFrameAtIndex(0) lineEntry = frame0.GetLineEntry() self.assertTrue(lineEntry.GetLine() == self.line2) address2 = lineEntry.GetStartAddress() #print("address1:", address1) #print("address2:", address2) # Now call SBTarget.ResolveSymbolContextForAddress() with the addresses # from our line entry. context1 = target.ResolveSymbolContextForAddress( address1, lldb.eSymbolContextEverything) context2 = target.ResolveSymbolContextForAddress( address2, lldb.eSymbolContextEverything) self.assertTrue(context1 and context2) #print("context1:", context1) #print("context2:", context2) # Verify that the context point to the same function 'a'. symbol1 = context1.GetSymbol() symbol2 = context2.GetSymbol() self.assertTrue(symbol1 and symbol2) #print("symbol1:", symbol1) #print("symbol2:", symbol2) from lldbsuite.test.lldbutil import get_description desc1 = get_description(symbol1) desc2 = get_description(symbol2) self.assertTrue(desc1 and desc2 and desc1 == desc2, "The two addresses should resolve to the same symbol")
def resolve_symbol_context_with_address(self): """Exercise SBTarget.ResolveSymbolContextForAddress() API.""" exe = self.getBuildArtifact("a.out") # Create a target by the debugger. target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Now create the two breakpoints inside function 'a'. breakpoint1 = target.BreakpointCreateByLocation('main.c', self.line1) breakpoint2 = target.BreakpointCreateByLocation('main.c', self.line2) self.trace("breakpoint1:", breakpoint1) self.trace("breakpoint2:", breakpoint2) self.assertTrue(breakpoint1 and breakpoint1.GetNumLocations() == 1, VALID_BREAKPOINT) self.assertTrue(breakpoint2 and breakpoint2.GetNumLocations() == 1, VALID_BREAKPOINT) # Now launch the process, and do not stop at entry point. process = target.LaunchSimple(None, None, self.get_process_working_directory()) self.assertTrue(process, PROCESS_IS_VALID) # Frame #0 should be on self.line1. self.assertEqual(process.GetState(), lldb.eStateStopped) thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) self.assertTrue( thread.IsValid(), "There should be a thread stopped due to breakpoint condition") #self.runCmd("process status") frame0 = thread.GetFrameAtIndex(0) lineEntry = frame0.GetLineEntry() self.assertEqual(lineEntry.GetLine(), self.line1) address1 = lineEntry.GetStartAddress() # Continue the inferior, the breakpoint 2 should be hit. process.Continue() self.assertEqual(process.GetState(), lldb.eStateStopped) thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) self.assertTrue( thread.IsValid(), "There should be a thread stopped due to breakpoint condition") #self.runCmd("process status") frame0 = thread.GetFrameAtIndex(0) lineEntry = frame0.GetLineEntry() self.assertEqual(lineEntry.GetLine(), self.line2) address2 = lineEntry.GetStartAddress() self.trace("address1:", address1) self.trace("address2:", address2) # Now call SBTarget.ResolveSymbolContextForAddress() with the addresses # from our line entry. context1 = target.ResolveSymbolContextForAddress( address1, lldb.eSymbolContextEverything) context2 = target.ResolveSymbolContextForAddress( address2, lldb.eSymbolContextEverything) self.assertTrue(context1 and context2) self.trace("context1:", context1) self.trace("context2:", context2) # Verify that the context point to the same function 'a'. symbol1 = context1.GetSymbol() symbol2 = context2.GetSymbol() self.assertTrue(symbol1 and symbol2) self.trace("symbol1:", symbol1) self.trace("symbol2:", symbol2) from lldbsuite.test.lldbutil import get_description desc1 = get_description(symbol1) desc2 = get_description(symbol2) self.assertTrue(desc1 and desc2 and desc1 == desc2, "The two addresses should resolve to the same symbol")
def test(self): """Exercise SBSymbolContext API extensively.""" self.build() exe = os.path.join(os.getcwd(), "a.out") # Create a target by the debugger. target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Now create a breakpoint on main.c by name 'c'. breakpoint = target.BreakpointCreateByName('c', 'a.out') #print("breakpoint:", breakpoint) self.assertTrue(breakpoint and breakpoint.GetNumLocations() == 1, VALID_BREAKPOINT) # Now launch the process, and do not stop at entry point. process = target.LaunchSimple (None, None, self.get_process_working_directory()) self.assertTrue(process, PROCESS_IS_VALID) # Frame #0 should be on self.line. from lldbsuite.test.lldbutil import get_stopped_thread thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint") frame0 = thread.GetFrameAtIndex(0) self.assertTrue(frame0.GetLineEntry().GetLine() == self.line) # Now get the SBSymbolContext from this frame. We want everything. :-) context = frame0.GetSymbolContext(lldb.eSymbolContextEverything) self.assertTrue(context) # Get the description of this module. module = context.GetModule() desc = lldbutil.get_description(module) self.expect(desc, "The module should match", exe=False, substrs = [os.path.join(self.mydir, 'a.out')]) compileUnit = context.GetCompileUnit() self.expect(str(compileUnit), "The compile unit should match", exe=False, substrs = [os.path.join(self.mydir, 'main.c')]) function = context.GetFunction() self.assertTrue(function) #print("function:", function) block = context.GetBlock() self.assertTrue(block) #print("block:", block) lineEntry = context.GetLineEntry() #print("line entry:", lineEntry) self.expect(lineEntry.GetFileSpec().GetDirectory(), "The line entry should have the correct directory", exe=False, substrs = [self.mydir]) self.expect(lineEntry.GetFileSpec().GetFilename(), "The line entry should have the correct filename", exe=False, substrs = ['main.c']) self.assertTrue(lineEntry.GetLine() == self.line, "The line entry's line number should match ") symbol = context.GetSymbol() self.assertTrue(function.GetName() == symbol.GetName() and symbol.GetName() == 'c', "The symbol name should be 'c'")
def test(self): """Exercise SBSymbolContext API extensively.""" self.build() exe = os.path.join(os.getcwd(), "a.out") # Create a target by the debugger. target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) # Now create a breakpoint on main.c by name 'c'. breakpoint = target.BreakpointCreateByName('c', 'a.out') #print("breakpoint:", breakpoint) self.assertTrue(breakpoint and breakpoint.GetNumLocations() == 1, VALID_BREAKPOINT) # Now launch the process, and do not stop at entry point. process = target.LaunchSimple(None, None, self.get_process_working_directory()) self.assertTrue(process, PROCESS_IS_VALID) # Frame #0 should be on self.line. from lldbsuite.test.lldbutil import get_stopped_thread thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint) self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint") frame0 = thread.GetFrameAtIndex(0) self.assertTrue(frame0.GetLineEntry().GetLine() == self.line) # Now get the SBSymbolContext from this frame. We want everything. :-) context = frame0.GetSymbolContext(lldb.eSymbolContextEverything) self.assertTrue(context) # Get the description of this module. module = context.GetModule() desc = lldbutil.get_description(module) self.expect(desc, "The module should match", exe=False, substrs=[os.path.join(self.mydir, 'a.out')]) compileUnit = context.GetCompileUnit() self.expect(str(compileUnit), "The compile unit should match", exe=False, substrs=[os.path.join(self.mydir, 'main.c')]) function = context.GetFunction() self.assertTrue(function) #print("function:", function) block = context.GetBlock() self.assertTrue(block) #print("block:", block) lineEntry = context.GetLineEntry() #print("line entry:", lineEntry) self.expect(lineEntry.GetFileSpec().GetDirectory(), "The line entry should have the correct directory", exe=False, substrs=[self.mydir]) self.expect(lineEntry.GetFileSpec().GetFilename(), "The line entry should have the correct filename", exe=False, substrs=['main.c']) self.assertTrue(lineEntry.GetLine() == self.line, "The line entry's line number should match ") symbol = context.GetSymbol() self.assertTrue( function.GetName() == symbol.GetName() and symbol.GetName() == 'c', "The symbol name should be 'c'")
def test_print_obj(self): """ Test "print object" where another thread blocks the print object from making progress. Set a breakpoint on the line in my_pthread_routine. Then switch threads to the main thread, and do print the lock_me object. Since that will try to get the lock already gotten by my_pthread_routime thread, it will have to switch to running all threads, and that should then succeed. """ d = {'EXE': 'b.out'} self.build(dictionary=d) self.setTearDownCleanup(dictionary=d) exe = os.path.join(os.getcwd(), 'b.out') target = self.dbg.CreateTarget(exe) self.assertTrue(target, VALID_TARGET) breakpoint = target.BreakpointCreateByLocation(self.source, self.line) self.assertTrue(breakpoint, VALID_BREAKPOINT) self.runCmd("breakpoint list") # Launch the process, and do not stop at the entry point. process = target.LaunchSimple (None, None, self.get_process_working_directory()) self.runCmd("thread backtrace all") # Let's get the current stopped thread. We'd like to switch to the # other thread to issue our 'po lock_me' command. import lldbsuite.test.lldbutil as lldbutil this_thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) self.assertTrue(this_thread) # Find the other thread. The iteration protocol of SBProcess and the # rich comparison methods (__eq__/__ne__) of SBThread come in handy. other_thread = None for t in process: if t != this_thread: other_thread = t break # Set the other thread as the selected thread to issue our 'po' command.other self.assertTrue(other_thread) process.SetSelectedThread(other_thread) if self.TraceOn(): print("selected thread:" + lldbutil.get_description(other_thread)) self.runCmd("thread backtrace") # We want to traverse the frame to the one corresponding to blocked.m to # issue our 'po lock_me' command. depth = other_thread.GetNumFrames() for i in range(depth): frame = other_thread.GetFrameAtIndex(i) name = frame.GetFunctionName() if name == 'main': other_thread.SetSelectedFrame(i) if self.TraceOn(): print("selected frame:" + lldbutil.get_description(frame)) break self.expect("po lock_me", OBJECT_PRINTED_CORRECTLY, substrs = ['I am pretty special.'])