10fbf3af3SJim Ingham############################################################################# 20fbf3af3SJim Ingham# This script contains two trivial examples of simple "scripted step" classes. 30fbf3af3SJim Ingham# To fully understand how the lldb "Thread Plan" architecture works, read the 40fbf3af3SJim Ingham# comments at the beginning of ThreadPlan.h in the lldb sources. The python 50fbf3af3SJim Ingham# interface is a reduced version of the full internal mechanism, but captures 60fbf3af3SJim Ingham# most of the power with a much simpler interface. 70fbf3af3SJim Ingham# 80fbf3af3SJim Ingham# But I'll attempt a brief summary here. 90fbf3af3SJim Ingham# Stepping in lldb is done independently for each thread. Moreover, the stepping 100fbf3af3SJim Ingham# operations are stackable. So for instance if you did a "step over", and in 110fbf3af3SJim Ingham# the course of stepping over you hit a breakpoint, stopped and stepped again, 120fbf3af3SJim Ingham# the first "step-over" would be suspended, and the new step operation would 130fbf3af3SJim Ingham# be enqueued. Then if that step over caused the program to hit another breakpoint, 140fbf3af3SJim Ingham# lldb would again suspend the second step and return control to the user, so 150fbf3af3SJim Ingham# now there are two pending step overs. Etc. with all the other stepping 160fbf3af3SJim Ingham# operations. Then if you hit "continue" the bottom-most step-over would complete, 170fbf3af3SJim Ingham# and another continue would complete the first "step-over". 180fbf3af3SJim Ingham# 190fbf3af3SJim Ingham# lldb represents this system with a stack of "Thread Plans". Each time a new 200fbf3af3SJim Ingham# stepping operation is requested, a new plan is pushed on the stack. When the 210fbf3af3SJim Ingham# operation completes, it is pushed off the stack. 220fbf3af3SJim Ingham# 230fbf3af3SJim Ingham# The bottom-most plan in the stack is the immediate controller of stepping, 240fbf3af3SJim Ingham# most importantly, when the process resumes, the bottom most plan will get 250fbf3af3SJim Ingham# asked whether to set the program running freely, or to instruction-single-step 260fbf3af3SJim Ingham# the current thread. In the scripted interface, you indicate this by returning 270fbf3af3SJim Ingham# False or True respectively from the should_step method. 280fbf3af3SJim Ingham# 290fbf3af3SJim Ingham# Each time the process stops the thread plan stack for each thread that stopped 300fbf3af3SJim Ingham# "for a reason", Ii.e. a single-step completed on that thread, or a breakpoint 310fbf3af3SJim Ingham# was hit), is queried to determine how to proceed, starting from the most 320fbf3af3SJim Ingham# recently pushed plan, in two stages: 330fbf3af3SJim Ingham# 340fbf3af3SJim Ingham# 1) Each plan is asked if it "explains" the stop. The first plan to claim the 350fbf3af3SJim Ingham# stop wins. In scripted Thread Plans, this is done by returning True from 360fbf3af3SJim Ingham# the "explains_stop method. This is how, for instance, control is returned 370fbf3af3SJim Ingham# to the User when the "step-over" plan hits a breakpoint. The step-over 380fbf3af3SJim Ingham# plan doesn't explain the breakpoint stop, so it returns false, and the 390fbf3af3SJim Ingham# breakpoint hit is propagated up the stack to the "base" thread plan, which 400fbf3af3SJim Ingham# is the one that handles random breakpoint hits. 410fbf3af3SJim Ingham# 420fbf3af3SJim Ingham# 2) Then the plan that won the first round is asked if the process should stop. 430fbf3af3SJim Ingham# This is done in the "should_stop" method. The scripted plans actually do 440fbf3af3SJim Ingham# three jobs in should_stop: 450fbf3af3SJim Ingham# a) They determine if they have completed their job or not. If they have 460fbf3af3SJim Ingham# they indicate that by calling SetPlanComplete on their thread plan. 470fbf3af3SJim Ingham# b) They decide whether they want to return control to the user or not. 480fbf3af3SJim Ingham# They do this by returning True or False respectively. 490fbf3af3SJim Ingham# c) If they are not done, they set up whatever machinery they will use 500fbf3af3SJim Ingham# the next time the thread continues. 510fbf3af3SJim Ingham# 520fbf3af3SJim Ingham# Note that deciding to return control to the user, and deciding your plan 530fbf3af3SJim Ingham# is done, are orthgonal operations. You could set up the next phase of 540fbf3af3SJim Ingham# stepping, and then return True from should_stop, and when the user next 550fbf3af3SJim Ingham# "continued" the process your plan would resume control. Of course, the 560fbf3af3SJim Ingham# user might also "step-over" or some other operation that would push a 570fbf3af3SJim Ingham# different plan, which would take control till it was done. 580fbf3af3SJim Ingham# 590fbf3af3SJim Ingham# One other detail you should be aware of, if the plan below you on the 600fbf3af3SJim Ingham# stack was done, then it will be popped and the next plan will take control 610fbf3af3SJim Ingham# and its "should_stop" will be called. 620fbf3af3SJim Ingham# 630fbf3af3SJim Ingham# Note also, there should be another method called when your plan is popped, 640fbf3af3SJim Ingham# to allow you to do whatever cleanup is required. I haven't gotten to that 650fbf3af3SJim Ingham# yet. For now you should do that at the same time you mark your plan complete. 660fbf3af3SJim Ingham# 67fd0dbab2SJim Ingham# 3) After the round of negotiation over whether to stop or not is done, all the 68fd0dbab2SJim Ingham# plans get asked if they are "stale". If they are say they are stale 69fd0dbab2SJim Ingham# then they will get popped. This question is asked with the "is_stale" method. 70fd0dbab2SJim Ingham# 71fd0dbab2SJim Ingham# This is useful, for instance, in the FinishPrintAndContinue plan. What might 72fd0dbab2SJim Ingham# happen here is that after continuing but before the finish is done, the program 73fd0dbab2SJim Ingham# could hit another breakpoint and stop. Then the user could use the step 74fd0dbab2SJim Ingham# command repeatedly until they leave the frame of interest by stepping. 75fd0dbab2SJim Ingham# In that case, the step plan is the one that will be responsible for stopping, 76fd0dbab2SJim Ingham# and the finish plan won't be asked should_stop, it will just be asked if it 77fd0dbab2SJim Ingham# is stale. In this case, if the step_out plan that the FinishPrintAndContinue 78fd0dbab2SJim Ingham# plan is driving is stale, so is ours, and it is time to do our printing. 79fd0dbab2SJim Ingham# 800fbf3af3SJim Ingham# Both examples show stepping through an address range for 20 bytes from the 810fbf3af3SJim Ingham# current PC. The first one does it by single stepping and checking a condition. 820fbf3af3SJim Ingham# It doesn't, however handle the case where you step into another frame while 830fbf3af3SJim Ingham# still in the current range in the starting frame. 840fbf3af3SJim Ingham# 850fbf3af3SJim Ingham# That is better handled in the second example by using the built-in StepOverRange 860fbf3af3SJim Ingham# thread plan. 870fbf3af3SJim Ingham# 880fbf3af3SJim Ingham# To use these stepping modes, you would do: 890fbf3af3SJim Ingham# 900fbf3af3SJim Ingham# (lldb) command script import scripted_step.py 910fbf3af3SJim Ingham# (lldb) thread step-scripted -C scripted_step.SimpleStep 920fbf3af3SJim Ingham# or 930fbf3af3SJim Ingham# 940fbf3af3SJim Ingham# (lldb) thread step-scripted -C scripted_step.StepWithPlan 950fbf3af3SJim Ingham 96*525cd59fSSerge Gueltonfrom __future__ import print_function 97*525cd59fSSerge Guelton 980fbf3af3SJim Inghamimport lldb 990fbf3af3SJim Ingham 100b9c1b51eSKate Stone 1010fbf3af3SJim Inghamclass SimpleStep: 102b9c1b51eSKate Stone 1030fbf3af3SJim Ingham def __init__(self, thread_plan, dict): 1040fbf3af3SJim Ingham self.thread_plan = thread_plan 1050fbf3af3SJim Ingham self.start_address = thread_plan.GetThread().GetFrameAtIndex(0).GetPC() 1060fbf3af3SJim Ingham 1070fbf3af3SJim Ingham def explains_stop(self, event): 1080fbf3af3SJim Ingham # We are stepping, so if we stop for any other reason, it isn't 1090fbf3af3SJim Ingham # because of us. 1100fbf3af3SJim Ingham if self.thread_plan.GetThread().GetStopReason() == lldb.eStopReasonTrace: 1110fbf3af3SJim Ingham return True 1120fbf3af3SJim Ingham else: 1130fbf3af3SJim Ingham return False 1140fbf3af3SJim Ingham 1150fbf3af3SJim Ingham def should_stop(self, event): 1160fbf3af3SJim Ingham cur_pc = self.thread_plan.GetThread().GetFrameAtIndex(0).GetPC() 1170fbf3af3SJim Ingham 1180fbf3af3SJim Ingham if cur_pc < self.start_address or cur_pc >= self.start_address + 20: 1190fbf3af3SJim Ingham self.thread_plan.SetPlanComplete(True) 1200fbf3af3SJim Ingham return True 1210fbf3af3SJim Ingham else: 1220fbf3af3SJim Ingham return False 1230fbf3af3SJim Ingham 1240fbf3af3SJim Ingham def should_step(self): 1250fbf3af3SJim Ingham return True 1260fbf3af3SJim Ingham 127b9c1b51eSKate Stone 1280fbf3af3SJim Inghamclass StepWithPlan: 129b9c1b51eSKate Stone 1300fbf3af3SJim Ingham def __init__(self, thread_plan, dict): 1310fbf3af3SJim Ingham self.thread_plan = thread_plan 1320fbf3af3SJim Ingham self.start_address = thread_plan.GetThread().GetFrameAtIndex(0).GetPCAddress() 133b9c1b51eSKate Stone self.step_thread_plan = thread_plan.QueueThreadPlanForStepOverRange( 134b9c1b51eSKate Stone self.start_address, 20) 1350fbf3af3SJim Ingham 1360fbf3af3SJim Ingham def explains_stop(self, event): 1370fbf3af3SJim Ingham # Since all I'm doing is running a plan, I will only ever get askedthis 1380fbf3af3SJim Ingham # if myplan doesn't explain the stop, and in that caseI don'teither. 1390fbf3af3SJim Ingham return False 1400fbf3af3SJim Ingham 1410fbf3af3SJim Ingham def should_stop(self, event): 1420fbf3af3SJim Ingham if self.step_thread_plan.IsPlanComplete(): 1430fbf3af3SJim Ingham self.thread_plan.SetPlanComplete(True) 1440fbf3af3SJim Ingham return True 1450fbf3af3SJim Ingham else: 1460fbf3af3SJim Ingham return False 1470fbf3af3SJim Ingham 1480fbf3af3SJim Ingham def should_step(self): 1490fbf3af3SJim Ingham return False 1500fbf3af3SJim Ingham 151a2baa0d9SJim Ingham# Here's another example which does "step over" through the current function, 152a2baa0d9SJim Ingham# and when it stops at each line, it checks some condition (in this example the 153a2baa0d9SJim Ingham# value of a variable) and stops if that condition is true. 154a2baa0d9SJim Ingham 155b9c1b51eSKate Stone 156a2baa0d9SJim Inghamclass StepCheckingCondition: 157b9c1b51eSKate Stone 158a2baa0d9SJim Ingham def __init__(self, thread_plan, dict): 159a2baa0d9SJim Ingham self.thread_plan = thread_plan 160a2baa0d9SJim Ingham self.start_frame = thread_plan.GetThread().GetFrameAtIndex(0) 161a2baa0d9SJim Ingham self.queue_next_plan() 162a2baa0d9SJim Ingham 163a2baa0d9SJim Ingham def queue_next_plan(self): 164a2baa0d9SJim Ingham cur_frame = self.thread_plan.GetThread().GetFrameAtIndex(0) 165a2baa0d9SJim Ingham cur_line_entry = cur_frame.GetLineEntry() 166a2baa0d9SJim Ingham start_address = cur_line_entry.GetStartAddress() 167a2baa0d9SJim Ingham end_address = cur_line_entry.GetEndAddress() 168a2baa0d9SJim Ingham line_range = end_address.GetFileAddress() - start_address.GetFileAddress() 169b9c1b51eSKate Stone self.step_thread_plan = self.thread_plan.QueueThreadPlanForStepOverRange( 170b9c1b51eSKate Stone start_address, line_range) 171a2baa0d9SJim Ingham 172a2baa0d9SJim Ingham def explains_stop(self, event): 173a2baa0d9SJim Ingham # We are stepping, so if we stop for any other reason, it isn't 174a2baa0d9SJim Ingham # because of us. 175a2baa0d9SJim Ingham return False 176a2baa0d9SJim Ingham 177a2baa0d9SJim Ingham def should_stop(self, event): 178a2baa0d9SJim Ingham if not self.step_thread_plan.IsPlanComplete(): 179a2baa0d9SJim Ingham return False 180a2baa0d9SJim Ingham 181a2baa0d9SJim Ingham frame = self.thread_plan.GetThread().GetFrameAtIndex(0) 182a2baa0d9SJim Ingham if not self.start_frame.IsEqual(frame): 183a2baa0d9SJim Ingham self.thread_plan.SetPlanComplete(True) 184a2baa0d9SJim Ingham return True 185a2baa0d9SJim Ingham 186a2baa0d9SJim Ingham # This part checks the condition. In this case we are expecting 187a2baa0d9SJim Ingham # some integer variable called "a", and will stop when it is 20. 188a2baa0d9SJim Ingham a_var = frame.FindVariable("a") 189a2baa0d9SJim Ingham 190a2baa0d9SJim Ingham if not a_var.IsValid(): 191*525cd59fSSerge Guelton print("A was not valid.") 192a2baa0d9SJim Ingham return True 193a2baa0d9SJim Ingham 194a2baa0d9SJim Ingham error = lldb.SBError() 195a2baa0d9SJim Ingham a_value = a_var.GetValueAsSigned(error) 196a2baa0d9SJim Ingham if not error.Success(): 197*525cd59fSSerge Guelton print("A value was not good.") 198a2baa0d9SJim Ingham return True 199a2baa0d9SJim Ingham 200a2baa0d9SJim Ingham if a_value == 20: 201a2baa0d9SJim Ingham self.thread_plan.SetPlanComplete(True) 202a2baa0d9SJim Ingham return True 203a2baa0d9SJim Ingham else: 204a2baa0d9SJim Ingham self.queue_next_plan() 205a2baa0d9SJim Ingham return False 206a2baa0d9SJim Ingham 207a2baa0d9SJim Ingham def should_step(self): 208a2baa0d9SJim Ingham return True 209a2baa0d9SJim Ingham 210c7468b7bSJim Ingham# Here's an example that steps out of the current frame, gathers some information 211c7468b7bSJim Ingham# and then continues. The information in this case is rax. Currently the thread 212c7468b7bSJim Ingham# plans are not a safe place to call lldb command-line commands, so the information 213c7468b7bSJim Ingham# is gathered through SB API calls. 214c7468b7bSJim Ingham 215b9c1b51eSKate Stone 216c7468b7bSJim Inghamclass FinishPrintAndContinue: 217b9c1b51eSKate Stone 218c7468b7bSJim Ingham def __init__(self, thread_plan, dict): 219c7468b7bSJim Ingham self.thread_plan = thread_plan 220b9c1b51eSKate Stone self.step_out_thread_plan = thread_plan.QueueThreadPlanForStepOut( 221b9c1b51eSKate Stone 0, True) 222c7468b7bSJim Ingham self.thread = self.thread_plan.GetThread() 223c7468b7bSJim Ingham 224fd0dbab2SJim Ingham def is_stale(self): 225fd0dbab2SJim Ingham if self.step_out_thread_plan.IsPlanStale(): 226fd0dbab2SJim Ingham self.do_print() 227fd0dbab2SJim Ingham return True 228fd0dbab2SJim Ingham else: 229fd0dbab2SJim Ingham return False 230fd0dbab2SJim Ingham 231c7468b7bSJim Ingham def explains_stop(self, event): 232c7468b7bSJim Ingham return False 233c7468b7bSJim Ingham 234c7468b7bSJim Ingham def should_stop(self, event): 235c7468b7bSJim Ingham if self.step_out_thread_plan.IsPlanComplete(): 236fd0dbab2SJim Ingham self.do_print() 237fd0dbab2SJim Ingham self.thread_plan.SetPlanComplete(True) 238fd0dbab2SJim Ingham return False 239fd0dbab2SJim Ingham 240fd0dbab2SJim Ingham def do_print(self): 241c7468b7bSJim Ingham frame_0 = self.thread.frames[0] 242c7468b7bSJim Ingham rax_value = frame_0.FindRegister("rax") 243c7468b7bSJim Ingham if rax_value.GetError().Success(): 244*525cd59fSSerge Guelton print("RAX on exit: ", rax_value.GetValue()) 245c7468b7bSJim Ingham else: 246*525cd59fSSerge Guelton print("Couldn't get rax value:", rax_value.GetError().GetCString()) 247