1*061da546Spatrick############################################################################# 2*061da546Spatrick# This script contains two trivial examples of simple "scripted step" classes. 3*061da546Spatrick# To fully understand how the lldb "Thread Plan" architecture works, read the 4*061da546Spatrick# comments at the beginning of ThreadPlan.h in the lldb sources. The python 5*061da546Spatrick# interface is a reduced version of the full internal mechanism, but captures 6*061da546Spatrick# most of the power with a much simpler interface. 7*061da546Spatrick# 8*061da546Spatrick# But I'll attempt a brief summary here. 9*061da546Spatrick# Stepping in lldb is done independently for each thread. Moreover, the stepping 10*061da546Spatrick# operations are stackable. So for instance if you did a "step over", and in 11*061da546Spatrick# the course of stepping over you hit a breakpoint, stopped and stepped again, 12*061da546Spatrick# the first "step-over" would be suspended, and the new step operation would 13*061da546Spatrick# be enqueued. Then if that step over caused the program to hit another breakpoint, 14*061da546Spatrick# lldb would again suspend the second step and return control to the user, so 15*061da546Spatrick# now there are two pending step overs. Etc. with all the other stepping 16*061da546Spatrick# operations. Then if you hit "continue" the bottom-most step-over would complete, 17*061da546Spatrick# and another continue would complete the first "step-over". 18*061da546Spatrick# 19*061da546Spatrick# lldb represents this system with a stack of "Thread Plans". Each time a new 20*061da546Spatrick# stepping operation is requested, a new plan is pushed on the stack. When the 21*061da546Spatrick# operation completes, it is pushed off the stack. 22*061da546Spatrick# 23*061da546Spatrick# The bottom-most plan in the stack is the immediate controller of stepping, 24*061da546Spatrick# most importantly, when the process resumes, the bottom most plan will get 25*061da546Spatrick# asked whether to set the program running freely, or to instruction-single-step 26*061da546Spatrick# the current thread. In the scripted interface, you indicate this by returning 27*061da546Spatrick# False or True respectively from the should_step method. 28*061da546Spatrick# 29*061da546Spatrick# Each time the process stops the thread plan stack for each thread that stopped 30*061da546Spatrick# "for a reason", Ii.e. a single-step completed on that thread, or a breakpoint 31*061da546Spatrick# was hit), is queried to determine how to proceed, starting from the most 32*061da546Spatrick# recently pushed plan, in two stages: 33*061da546Spatrick# 34*061da546Spatrick# 1) Each plan is asked if it "explains" the stop. The first plan to claim the 35*061da546Spatrick# stop wins. In scripted Thread Plans, this is done by returning True from 36*061da546Spatrick# the "explains_stop method. This is how, for instance, control is returned 37*061da546Spatrick# to the User when the "step-over" plan hits a breakpoint. The step-over 38*061da546Spatrick# plan doesn't explain the breakpoint stop, so it returns false, and the 39*061da546Spatrick# breakpoint hit is propagated up the stack to the "base" thread plan, which 40*061da546Spatrick# is the one that handles random breakpoint hits. 41*061da546Spatrick# 42*061da546Spatrick# 2) Then the plan that won the first round is asked if the process should stop. 43*061da546Spatrick# This is done in the "should_stop" method. The scripted plans actually do 44*061da546Spatrick# three jobs in should_stop: 45*061da546Spatrick# a) They determine if they have completed their job or not. If they have 46*061da546Spatrick# they indicate that by calling SetPlanComplete on their thread plan. 47*061da546Spatrick# b) They decide whether they want to return control to the user or not. 48*061da546Spatrick# They do this by returning True or False respectively. 49*061da546Spatrick# c) If they are not done, they set up whatever machinery they will use 50*061da546Spatrick# the next time the thread continues. 51*061da546Spatrick# 52*061da546Spatrick# Note that deciding to return control to the user, and deciding your plan 53*061da546Spatrick# is done, are orthgonal operations. You could set up the next phase of 54*061da546Spatrick# stepping, and then return True from should_stop, and when the user next 55*061da546Spatrick# "continued" the process your plan would resume control. Of course, the 56*061da546Spatrick# user might also "step-over" or some other operation that would push a 57*061da546Spatrick# different plan, which would take control till it was done. 58*061da546Spatrick# 59*061da546Spatrick# One other detail you should be aware of, if the plan below you on the 60*061da546Spatrick# stack was done, then it will be popped and the next plan will take control 61*061da546Spatrick# and its "should_stop" will be called. 62*061da546Spatrick# 63*061da546Spatrick# Note also, there should be another method called when your plan is popped, 64*061da546Spatrick# to allow you to do whatever cleanup is required. I haven't gotten to that 65*061da546Spatrick# yet. For now you should do that at the same time you mark your plan complete. 66*061da546Spatrick# 67*061da546Spatrick# 3) After the round of negotiation over whether to stop or not is done, all the 68*061da546Spatrick# plans get asked if they are "stale". If they are say they are stale 69*061da546Spatrick# then they will get popped. This question is asked with the "is_stale" method. 70*061da546Spatrick# 71*061da546Spatrick# This is useful, for instance, in the FinishPrintAndContinue plan. What might 72*061da546Spatrick# happen here is that after continuing but before the finish is done, the program 73*061da546Spatrick# could hit another breakpoint and stop. Then the user could use the step 74*061da546Spatrick# command repeatedly until they leave the frame of interest by stepping. 75*061da546Spatrick# In that case, the step plan is the one that will be responsible for stopping, 76*061da546Spatrick# and the finish plan won't be asked should_stop, it will just be asked if it 77*061da546Spatrick# is stale. In this case, if the step_out plan that the FinishPrintAndContinue 78*061da546Spatrick# plan is driving is stale, so is ours, and it is time to do our printing. 79*061da546Spatrick# 80*061da546Spatrick# Both examples show stepping through an address range for 20 bytes from the 81*061da546Spatrick# current PC. The first one does it by single stepping and checking a condition. 82*061da546Spatrick# It doesn't, however handle the case where you step into another frame while 83*061da546Spatrick# still in the current range in the starting frame. 84*061da546Spatrick# 85*061da546Spatrick# That is better handled in the second example by using the built-in StepOverRange 86*061da546Spatrick# thread plan. 87*061da546Spatrick# 88*061da546Spatrick# To use these stepping modes, you would do: 89*061da546Spatrick# 90*061da546Spatrick# (lldb) command script import scripted_step.py 91*061da546Spatrick# (lldb) thread step-scripted -C scripted_step.SimpleStep 92*061da546Spatrick# or 93*061da546Spatrick# 94*061da546Spatrick# (lldb) thread step-scripted -C scripted_step.StepWithPlan 95*061da546Spatrick 96*061da546Spatrickimport lldb 97*061da546Spatrick 98*061da546Spatrick 99*061da546Spatrickclass SimpleStep: 100*061da546Spatrick 101*061da546Spatrick def __init__(self, thread_plan, dict): 102*061da546Spatrick self.thread_plan = thread_plan 103*061da546Spatrick self.start_address = thread_plan.GetThread().GetFrameAtIndex(0).GetPC() 104*061da546Spatrick 105*061da546Spatrick def explains_stop(self, event): 106*061da546Spatrick # We are stepping, so if we stop for any other reason, it isn't 107*061da546Spatrick # because of us. 108*061da546Spatrick if self.thread_plan.GetThread().GetStopReason() == lldb.eStopReasonTrace: 109*061da546Spatrick return True 110*061da546Spatrick else: 111*061da546Spatrick return False 112*061da546Spatrick 113*061da546Spatrick def should_stop(self, event): 114*061da546Spatrick cur_pc = self.thread_plan.GetThread().GetFrameAtIndex(0).GetPC() 115*061da546Spatrick 116*061da546Spatrick if cur_pc < self.start_address or cur_pc >= self.start_address + 20: 117*061da546Spatrick self.thread_plan.SetPlanComplete(True) 118*061da546Spatrick return True 119*061da546Spatrick else: 120*061da546Spatrick return False 121*061da546Spatrick 122*061da546Spatrick def should_step(self): 123*061da546Spatrick return True 124*061da546Spatrick 125*061da546Spatrick 126*061da546Spatrickclass StepWithPlan: 127*061da546Spatrick 128*061da546Spatrick def __init__(self, thread_plan, dict): 129*061da546Spatrick self.thread_plan = thread_plan 130*061da546Spatrick self.start_address = thread_plan.GetThread().GetFrameAtIndex(0).GetPCAddress() 131*061da546Spatrick self.step_thread_plan = thread_plan.QueueThreadPlanForStepOverRange( 132*061da546Spatrick self.start_address, 20) 133*061da546Spatrick 134*061da546Spatrick def explains_stop(self, event): 135*061da546Spatrick # Since all I'm doing is running a plan, I will only ever get askedthis 136*061da546Spatrick # if myplan doesn't explain the stop, and in that caseI don'teither. 137*061da546Spatrick return False 138*061da546Spatrick 139*061da546Spatrick def should_stop(self, event): 140*061da546Spatrick if self.step_thread_plan.IsPlanComplete(): 141*061da546Spatrick self.thread_plan.SetPlanComplete(True) 142*061da546Spatrick return True 143*061da546Spatrick else: 144*061da546Spatrick return False 145*061da546Spatrick 146*061da546Spatrick def should_step(self): 147*061da546Spatrick return False 148*061da546Spatrick 149*061da546Spatrick# Here's another example which does "step over" through the current function, 150*061da546Spatrick# and when it stops at each line, it checks some condition (in this example the 151*061da546Spatrick# value of a variable) and stops if that condition is true. 152*061da546Spatrick 153*061da546Spatrick 154*061da546Spatrickclass StepCheckingCondition: 155*061da546Spatrick 156*061da546Spatrick def __init__(self, thread_plan, dict): 157*061da546Spatrick self.thread_plan = thread_plan 158*061da546Spatrick self.start_frame = thread_plan.GetThread().GetFrameAtIndex(0) 159*061da546Spatrick self.queue_next_plan() 160*061da546Spatrick 161*061da546Spatrick def queue_next_plan(self): 162*061da546Spatrick cur_frame = self.thread_plan.GetThread().GetFrameAtIndex(0) 163*061da546Spatrick cur_line_entry = cur_frame.GetLineEntry() 164*061da546Spatrick start_address = cur_line_entry.GetStartAddress() 165*061da546Spatrick end_address = cur_line_entry.GetEndAddress() 166*061da546Spatrick line_range = end_address.GetFileAddress() - start_address.GetFileAddress() 167*061da546Spatrick self.step_thread_plan = self.thread_plan.QueueThreadPlanForStepOverRange( 168*061da546Spatrick start_address, line_range) 169*061da546Spatrick 170*061da546Spatrick def explains_stop(self, event): 171*061da546Spatrick # We are stepping, so if we stop for any other reason, it isn't 172*061da546Spatrick # because of us. 173*061da546Spatrick return False 174*061da546Spatrick 175*061da546Spatrick def should_stop(self, event): 176*061da546Spatrick if not self.step_thread_plan.IsPlanComplete(): 177*061da546Spatrick return False 178*061da546Spatrick 179*061da546Spatrick frame = self.thread_plan.GetThread().GetFrameAtIndex(0) 180*061da546Spatrick if not self.start_frame.IsEqual(frame): 181*061da546Spatrick self.thread_plan.SetPlanComplete(True) 182*061da546Spatrick return True 183*061da546Spatrick 184*061da546Spatrick # This part checks the condition. In this case we are expecting 185*061da546Spatrick # some integer variable called "a", and will stop when it is 20. 186*061da546Spatrick a_var = frame.FindVariable("a") 187*061da546Spatrick 188*061da546Spatrick if not a_var.IsValid(): 189*061da546Spatrick print("A was not valid.") 190*061da546Spatrick return True 191*061da546Spatrick 192*061da546Spatrick error = lldb.SBError() 193*061da546Spatrick a_value = a_var.GetValueAsSigned(error) 194*061da546Spatrick if not error.Success(): 195*061da546Spatrick print("A value was not good.") 196*061da546Spatrick return True 197*061da546Spatrick 198*061da546Spatrick if a_value == 20: 199*061da546Spatrick self.thread_plan.SetPlanComplete(True) 200*061da546Spatrick return True 201*061da546Spatrick else: 202*061da546Spatrick self.queue_next_plan() 203*061da546Spatrick return False 204*061da546Spatrick 205*061da546Spatrick def should_step(self): 206*061da546Spatrick return True 207*061da546Spatrick 208*061da546Spatrick# Here's an example that steps out of the current frame, gathers some information 209*061da546Spatrick# and then continues. The information in this case is rax. Currently the thread 210*061da546Spatrick# plans are not a safe place to call lldb command-line commands, so the information 211*061da546Spatrick# is gathered through SB API calls. 212*061da546Spatrick 213*061da546Spatrick 214*061da546Spatrickclass FinishPrintAndContinue: 215*061da546Spatrick 216*061da546Spatrick def __init__(self, thread_plan, dict): 217*061da546Spatrick self.thread_plan = thread_plan 218*061da546Spatrick self.step_out_thread_plan = thread_plan.QueueThreadPlanForStepOut( 219*061da546Spatrick 0, True) 220*061da546Spatrick self.thread = self.thread_plan.GetThread() 221*061da546Spatrick 222*061da546Spatrick def is_stale(self): 223*061da546Spatrick if self.step_out_thread_plan.IsPlanStale(): 224*061da546Spatrick self.do_print() 225*061da546Spatrick return True 226*061da546Spatrick else: 227*061da546Spatrick return False 228*061da546Spatrick 229*061da546Spatrick def explains_stop(self, event): 230*061da546Spatrick return False 231*061da546Spatrick 232*061da546Spatrick def should_stop(self, event): 233*061da546Spatrick if self.step_out_thread_plan.IsPlanComplete(): 234*061da546Spatrick self.do_print() 235*061da546Spatrick self.thread_plan.SetPlanComplete(True) 236*061da546Spatrick return False 237*061da546Spatrick 238*061da546Spatrick def do_print(self): 239*061da546Spatrick frame_0 = self.thread.frames[0] 240*061da546Spatrick rax_value = frame_0.FindRegister("rax") 241*061da546Spatrick if rax_value.GetError().Success(): 242*061da546Spatrick print("RAX on exit: ", rax_value.GetValue()) 243*061da546Spatrick else: 244*061da546Spatrick print("Couldn't get rax value:", rax_value.GetError().GetCString()) 245