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# 67*fd0dbab2SJim Ingham# 3) After the round of negotiation over whether to stop or not is done, all the 68*fd0dbab2SJim Ingham# plans get asked if they are "stale". If they are say they are stale 69*fd0dbab2SJim Ingham# then they will get popped. This question is asked with the "is_stale" method. 70*fd0dbab2SJim Ingham# 71*fd0dbab2SJim Ingham# This is useful, for instance, in the FinishPrintAndContinue plan. What might 72*fd0dbab2SJim Ingham# happen here is that after continuing but before the finish is done, the program 73*fd0dbab2SJim Ingham# could hit another breakpoint and stop. Then the user could use the step 74*fd0dbab2SJim Ingham# command repeatedly until they leave the frame of interest by stepping. 75*fd0dbab2SJim Ingham# In that case, the step plan is the one that will be responsible for stopping, 76*fd0dbab2SJim Ingham# and the finish plan won't be asked should_stop, it will just be asked if it 77*fd0dbab2SJim Ingham# is stale. In this case, if the step_out plan that the FinishPrintAndContinue 78*fd0dbab2SJim Ingham# plan is driving is stale, so is ours, and it is time to do our printing. 79*fd0dbab2SJim 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 960fbf3af3SJim Inghamimport lldb 970fbf3af3SJim Ingham 980fbf3af3SJim Inghamclass SimpleStep: 990fbf3af3SJim Ingham def __init__ (self, thread_plan, dict): 1000fbf3af3SJim Ingham self.thread_plan = thread_plan 1010fbf3af3SJim Ingham self.start_address = thread_plan.GetThread().GetFrameAtIndex(0).GetPC() 1020fbf3af3SJim Ingham 1030fbf3af3SJim Ingham def explains_stop (self, event): 1040fbf3af3SJim Ingham # We are stepping, so if we stop for any other reason, it isn't 1050fbf3af3SJim Ingham # because of us. 1060fbf3af3SJim Ingham if self.thread_plan.GetThread().GetStopReason()== lldb.eStopReasonTrace: 1070fbf3af3SJim Ingham return True 1080fbf3af3SJim Ingham else: 1090fbf3af3SJim Ingham return False 1100fbf3af3SJim Ingham 1110fbf3af3SJim Ingham def should_stop (self, event): 1120fbf3af3SJim Ingham cur_pc = self.thread_plan.GetThread().GetFrameAtIndex(0).GetPC() 1130fbf3af3SJim Ingham 1140fbf3af3SJim Ingham if cur_pc < self.start_address or cur_pc >= self.start_address + 20: 1150fbf3af3SJim Ingham self.thread_plan.SetPlanComplete(True) 1160fbf3af3SJim Ingham return True 1170fbf3af3SJim Ingham else: 1180fbf3af3SJim Ingham return False 1190fbf3af3SJim Ingham 1200fbf3af3SJim Ingham def should_step (self): 1210fbf3af3SJim Ingham return True 1220fbf3af3SJim Ingham 1230fbf3af3SJim Inghamclass StepWithPlan: 1240fbf3af3SJim Ingham def __init__ (self, thread_plan, dict): 1250fbf3af3SJim Ingham self.thread_plan = thread_plan 1260fbf3af3SJim Ingham self.start_address = thread_plan.GetThread().GetFrameAtIndex(0).GetPCAddress() 1270fbf3af3SJim Ingham self.step_thread_plan =thread_plan.QueueThreadPlanForStepOverRange(self.start_address, 20); 1280fbf3af3SJim Ingham 1290fbf3af3SJim Ingham def explains_stop (self, event): 1300fbf3af3SJim Ingham # Since all I'm doing is running a plan, I will only ever get askedthis 1310fbf3af3SJim Ingham # if myplan doesn't explain the stop, and in that caseI don'teither. 1320fbf3af3SJim Ingham return False 1330fbf3af3SJim Ingham 1340fbf3af3SJim Ingham def should_stop (self, event): 1350fbf3af3SJim Ingham if self.step_thread_plan.IsPlanComplete(): 1360fbf3af3SJim Ingham self.thread_plan.SetPlanComplete(True) 1370fbf3af3SJim Ingham return True 1380fbf3af3SJim Ingham else: 1390fbf3af3SJim Ingham return False 1400fbf3af3SJim Ingham 1410fbf3af3SJim Ingham def should_step (self): 1420fbf3af3SJim Ingham return False 1430fbf3af3SJim Ingham 144a2baa0d9SJim Ingham# Here's another example which does "step over" through the current function, 145a2baa0d9SJim Ingham# and when it stops at each line, it checks some condition (in this example the 146a2baa0d9SJim Ingham# value of a variable) and stops if that condition is true. 147a2baa0d9SJim Ingham 148a2baa0d9SJim Inghamclass StepCheckingCondition: 149a2baa0d9SJim Ingham def __init__ (self, thread_plan, dict): 150a2baa0d9SJim Ingham self.thread_plan = thread_plan 151a2baa0d9SJim Ingham self.start_frame = thread_plan.GetThread().GetFrameAtIndex(0) 152a2baa0d9SJim Ingham self.queue_next_plan() 153a2baa0d9SJim Ingham 154a2baa0d9SJim Ingham def queue_next_plan (self): 155a2baa0d9SJim Ingham cur_frame = self.thread_plan.GetThread().GetFrameAtIndex(0) 156a2baa0d9SJim Ingham cur_line_entry = cur_frame.GetLineEntry() 157a2baa0d9SJim Ingham start_address = cur_line_entry.GetStartAddress() 158a2baa0d9SJim Ingham end_address = cur_line_entry.GetEndAddress() 159a2baa0d9SJim Ingham line_range = end_address.GetFileAddress() - start_address.GetFileAddress() 160a2baa0d9SJim Ingham self.step_thread_plan = self.thread_plan.QueueThreadPlanForStepOverRange(start_address, line_range) 161a2baa0d9SJim Ingham 162a2baa0d9SJim Ingham def explains_stop (self, event): 163a2baa0d9SJim Ingham # We are stepping, so if we stop for any other reason, it isn't 164a2baa0d9SJim Ingham # because of us. 165a2baa0d9SJim Ingham return False 166a2baa0d9SJim Ingham 167a2baa0d9SJim Ingham def should_stop (self, event): 168a2baa0d9SJim Ingham if not self.step_thread_plan.IsPlanComplete(): 169a2baa0d9SJim Ingham return False 170a2baa0d9SJim Ingham 171a2baa0d9SJim Ingham frame = self.thread_plan.GetThread().GetFrameAtIndex(0) 172a2baa0d9SJim Ingham if not self.start_frame.IsEqual(frame): 173a2baa0d9SJim Ingham self.thread_plan.SetPlanComplete(True) 174a2baa0d9SJim Ingham return True 175a2baa0d9SJim Ingham 176a2baa0d9SJim Ingham # This part checks the condition. In this case we are expecting 177a2baa0d9SJim Ingham # some integer variable called "a", and will stop when it is 20. 178a2baa0d9SJim Ingham a_var = frame.FindVariable("a") 179a2baa0d9SJim Ingham 180a2baa0d9SJim Ingham if not a_var.IsValid(): 181a2baa0d9SJim Ingham print "A was not valid." 182a2baa0d9SJim Ingham return True 183a2baa0d9SJim Ingham 184a2baa0d9SJim Ingham error = lldb.SBError() 185a2baa0d9SJim Ingham a_value = a_var.GetValueAsSigned (error) 186a2baa0d9SJim Ingham if not error.Success(): 187a2baa0d9SJim Ingham print "A value was not good." 188a2baa0d9SJim Ingham return True 189a2baa0d9SJim Ingham 190a2baa0d9SJim Ingham if a_value == 20: 191a2baa0d9SJim Ingham self.thread_plan.SetPlanComplete(True) 192a2baa0d9SJim Ingham return True 193a2baa0d9SJim Ingham else: 194a2baa0d9SJim Ingham self.queue_next_plan() 195a2baa0d9SJim Ingham return False 196a2baa0d9SJim Ingham 197a2baa0d9SJim Ingham def should_step (self): 198a2baa0d9SJim Ingham return True 199a2baa0d9SJim Ingham 200c7468b7bSJim Ingham# Here's an example that steps out of the current frame, gathers some information 201c7468b7bSJim Ingham# and then continues. The information in this case is rax. Currently the thread 202c7468b7bSJim Ingham# plans are not a safe place to call lldb command-line commands, so the information 203c7468b7bSJim Ingham# is gathered through SB API calls. 204c7468b7bSJim Ingham 205c7468b7bSJim Inghamclass FinishPrintAndContinue: 206c7468b7bSJim Ingham def __init__ (self, thread_plan, dict): 207c7468b7bSJim Ingham self.thread_plan = thread_plan 208c7468b7bSJim Ingham self.step_out_thread_plan = thread_plan.QueueThreadPlanForStepOut(0, True) 209c7468b7bSJim Ingham self.thread = self.thread_plan.GetThread() 210c7468b7bSJim Ingham 211*fd0dbab2SJim Ingham def is_stale (self): 212*fd0dbab2SJim Ingham if self.step_out_thread_plan.IsPlanStale(): 213*fd0dbab2SJim Ingham self.do_print() 214*fd0dbab2SJim Ingham return True 215*fd0dbab2SJim Ingham else: 216*fd0dbab2SJim Ingham return False 217*fd0dbab2SJim Ingham 218c7468b7bSJim Ingham def explains_stop (self, event): 219c7468b7bSJim Ingham return False 220c7468b7bSJim Ingham 221c7468b7bSJim Ingham def should_stop (self, event): 222c7468b7bSJim Ingham if self.step_out_thread_plan.IsPlanComplete(): 223*fd0dbab2SJim Ingham self.do_print() 224*fd0dbab2SJim Ingham self.thread_plan.SetPlanComplete(True) 225*fd0dbab2SJim Ingham return False 226*fd0dbab2SJim Ingham 227*fd0dbab2SJim Ingham def do_print (self): 228c7468b7bSJim Ingham frame_0 = self.thread.frames[0] 229c7468b7bSJim Ingham rax_value = frame_0.FindRegister("rax") 230c7468b7bSJim Ingham if rax_value.GetError().Success(): 231c7468b7bSJim Ingham print "RAX on exit: ", rax_value.GetValue() 232c7468b7bSJim Ingham else: 233c7468b7bSJim Ingham print "Couldn't get rax value:", rax_value.GetError().GetCString() 234