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#
670fbf3af3SJim Ingham# Both examples show stepping through an address range for 20 bytes from the
680fbf3af3SJim Ingham# current PC.  The first one does it by single stepping and checking a condition.
690fbf3af3SJim Ingham# It doesn't, however handle the case where you step into another frame while
700fbf3af3SJim Ingham# still in the current range in the starting frame.
710fbf3af3SJim Ingham#
720fbf3af3SJim Ingham# That is better handled in the second example by using the built-in StepOverRange
730fbf3af3SJim Ingham# thread plan.
740fbf3af3SJim Ingham#
750fbf3af3SJim Ingham# To use these stepping modes, you would do:
760fbf3af3SJim Ingham#
770fbf3af3SJim Ingham#     (lldb) command script import scripted_step.py
780fbf3af3SJim Ingham#     (lldb) thread step-scripted -C scripted_step.SimpleStep
790fbf3af3SJim Ingham# or
800fbf3af3SJim Ingham#
810fbf3af3SJim Ingham#     (lldb) thread step-scripted -C scripted_step.StepWithPlan
820fbf3af3SJim Ingham
830fbf3af3SJim Inghamimport lldb
840fbf3af3SJim Ingham
850fbf3af3SJim Inghamclass SimpleStep:
860fbf3af3SJim Ingham    def __init__ (self, thread_plan, dict):
870fbf3af3SJim Ingham        self.thread_plan = thread_plan
880fbf3af3SJim Ingham        self.start_address = thread_plan.GetThread().GetFrameAtIndex(0).GetPC()
890fbf3af3SJim Ingham
900fbf3af3SJim Ingham    def explains_stop (self, event):
910fbf3af3SJim Ingham        # We are stepping, so if we stop for any other reason, it isn't
920fbf3af3SJim Ingham        # because of us.
930fbf3af3SJim Ingham        if self.thread_plan.GetThread().GetStopReason()== lldb.eStopReasonTrace:
940fbf3af3SJim Ingham            return True
950fbf3af3SJim Ingham        else:
960fbf3af3SJim Ingham            return False
970fbf3af3SJim Ingham
980fbf3af3SJim Ingham    def should_stop (self, event):
990fbf3af3SJim Ingham        cur_pc = self.thread_plan.GetThread().GetFrameAtIndex(0).GetPC()
1000fbf3af3SJim Ingham
1010fbf3af3SJim Ingham        if cur_pc < self.start_address or cur_pc >= self.start_address + 20:
1020fbf3af3SJim Ingham            self.thread_plan.SetPlanComplete(True)
1030fbf3af3SJim Ingham            return True
1040fbf3af3SJim Ingham        else:
1050fbf3af3SJim Ingham            return False
1060fbf3af3SJim Ingham
1070fbf3af3SJim Ingham    def should_step (self):
1080fbf3af3SJim Ingham        return True
1090fbf3af3SJim Ingham
1100fbf3af3SJim Inghamclass StepWithPlan:
1110fbf3af3SJim Ingham    def __init__ (self, thread_plan, dict):
1120fbf3af3SJim Ingham        self.thread_plan = thread_plan
1130fbf3af3SJim Ingham        self.start_address = thread_plan.GetThread().GetFrameAtIndex(0).GetPCAddress()
1140fbf3af3SJim Ingham        self.step_thread_plan =thread_plan.QueueThreadPlanForStepOverRange(self.start_address, 20);
1150fbf3af3SJim Ingham
1160fbf3af3SJim Ingham    def explains_stop (self, event):
1170fbf3af3SJim Ingham        # Since all I'm doing is running a plan, I will only ever get askedthis
1180fbf3af3SJim Ingham        # if myplan doesn't explain the stop, and in that caseI don'teither.
1190fbf3af3SJim Ingham        return False
1200fbf3af3SJim Ingham
1210fbf3af3SJim Ingham    def should_stop (self, event):
1220fbf3af3SJim Ingham        if self.step_thread_plan.IsPlanComplete():
1230fbf3af3SJim Ingham            self.thread_plan.SetPlanComplete(True)
1240fbf3af3SJim Ingham            return True
1250fbf3af3SJim Ingham        else:
1260fbf3af3SJim Ingham            return False
1270fbf3af3SJim Ingham
1280fbf3af3SJim Ingham    def should_step (self):
1290fbf3af3SJim Ingham        return False
1300fbf3af3SJim Ingham
131*a2baa0d9SJim Ingham# Here's another example which does "step over" through the current function,
132*a2baa0d9SJim Ingham# and when it stops at each line, it checks some condition (in this example the
133*a2baa0d9SJim Ingham# value of a variable) and stops if that condition is true.
134*a2baa0d9SJim Ingham
135*a2baa0d9SJim Inghamclass StepCheckingCondition:
136*a2baa0d9SJim Ingham    def __init__ (self, thread_plan, dict):
137*a2baa0d9SJim Ingham        self.thread_plan = thread_plan
138*a2baa0d9SJim Ingham        self.start_frame = thread_plan.GetThread().GetFrameAtIndex(0)
139*a2baa0d9SJim Ingham        self.queue_next_plan()
140*a2baa0d9SJim Ingham
141*a2baa0d9SJim Ingham    def queue_next_plan (self):
142*a2baa0d9SJim Ingham        cur_frame = self.thread_plan.GetThread().GetFrameAtIndex(0)
143*a2baa0d9SJim Ingham        cur_line_entry = cur_frame.GetLineEntry()
144*a2baa0d9SJim Ingham        start_address = cur_line_entry.GetStartAddress()
145*a2baa0d9SJim Ingham        end_address = cur_line_entry.GetEndAddress()
146*a2baa0d9SJim Ingham        line_range = end_address.GetFileAddress() - start_address.GetFileAddress()
147*a2baa0d9SJim Ingham        self.step_thread_plan = self.thread_plan.QueueThreadPlanForStepOverRange(start_address, line_range)
148*a2baa0d9SJim Ingham
149*a2baa0d9SJim Ingham    def explains_stop (self, event):
150*a2baa0d9SJim Ingham        # We are stepping, so if we stop for any other reason, it isn't
151*a2baa0d9SJim Ingham        # because of us.
152*a2baa0d9SJim Ingham        return False
153*a2baa0d9SJim Ingham
154*a2baa0d9SJim Ingham    def should_stop (self, event):
155*a2baa0d9SJim Ingham        if not self.step_thread_plan.IsPlanComplete():
156*a2baa0d9SJim Ingham            return False
157*a2baa0d9SJim Ingham
158*a2baa0d9SJim Ingham        frame = self.thread_plan.GetThread().GetFrameAtIndex(0)
159*a2baa0d9SJim Ingham        if not self.start_frame.IsEqual(frame):
160*a2baa0d9SJim Ingham            self.thread_plan.SetPlanComplete(True)
161*a2baa0d9SJim Ingham            return True
162*a2baa0d9SJim Ingham
163*a2baa0d9SJim Ingham        # This part checks the condition.  In this case we are expecting
164*a2baa0d9SJim Ingham        # some integer variable called "a", and will stop when it is 20.
165*a2baa0d9SJim Ingham        a_var = frame.FindVariable("a")
166*a2baa0d9SJim Ingham
167*a2baa0d9SJim Ingham        if not a_var.IsValid():
168*a2baa0d9SJim Ingham            print "A was not valid."
169*a2baa0d9SJim Ingham            return True
170*a2baa0d9SJim Ingham
171*a2baa0d9SJim Ingham        error = lldb.SBError()
172*a2baa0d9SJim Ingham        a_value = a_var.GetValueAsSigned (error)
173*a2baa0d9SJim Ingham        if not error.Success():
174*a2baa0d9SJim Ingham            print "A value was not good."
175*a2baa0d9SJim Ingham            return True
176*a2baa0d9SJim Ingham
177*a2baa0d9SJim Ingham        if a_value == 20:
178*a2baa0d9SJim Ingham            self.thread_plan.SetPlanComplete(True)
179*a2baa0d9SJim Ingham            return True
180*a2baa0d9SJim Ingham        else:
181*a2baa0d9SJim Ingham            self.queue_next_plan()
182*a2baa0d9SJim Ingham            return False
183*a2baa0d9SJim Ingham
184*a2baa0d9SJim Ingham    def should_step (self):
185*a2baa0d9SJim Ingham        return True
186*a2baa0d9SJim Ingham
187