1*0fbf3af3SJim Ingham#############################################################################
2*0fbf3af3SJim Ingham# This script contains two trivial examples of simple "scripted step" classes.
3*0fbf3af3SJim Ingham# To fully understand how the lldb "Thread Plan" architecture works, read the
4*0fbf3af3SJim Ingham# comments at the beginning of ThreadPlan.h in the lldb sources.  The python
5*0fbf3af3SJim Ingham# interface is a reduced version of the full internal mechanism, but captures
6*0fbf3af3SJim Ingham# most of the power with a much simpler interface.
7*0fbf3af3SJim Ingham#
8*0fbf3af3SJim Ingham# But I'll attempt a brief summary here.
9*0fbf3af3SJim Ingham# Stepping in lldb is done independently for each thread.  Moreover, the stepping
10*0fbf3af3SJim Ingham# operations are stackable.  So for instance if you did a "step over", and in
11*0fbf3af3SJim Ingham# the course of stepping over you hit a breakpoint, stopped and stepped again,
12*0fbf3af3SJim Ingham# the first "step-over" would be suspended, and the new step operation would
13*0fbf3af3SJim Ingham# be enqueued.  Then if that step over caused the program to hit another breakpoint,
14*0fbf3af3SJim Ingham# lldb would again suspend the second step and return control to the user, so
15*0fbf3af3SJim Ingham# now there are two pending step overs.  Etc. with all the other stepping
16*0fbf3af3SJim Ingham# operations.  Then if you hit "continue" the bottom-most step-over would complete,
17*0fbf3af3SJim Ingham# and another continue would complete the first "step-over".
18*0fbf3af3SJim Ingham#
19*0fbf3af3SJim Ingham# lldb represents this system with a stack of "Thread Plans".  Each time a new
20*0fbf3af3SJim Ingham# stepping operation is requested, a new plan is pushed on the stack.  When the
21*0fbf3af3SJim Ingham# operation completes, it is pushed off the stack.
22*0fbf3af3SJim Ingham#
23*0fbf3af3SJim Ingham# The bottom-most plan in the stack is the immediate controller of stepping,
24*0fbf3af3SJim Ingham# most importantly, when the process resumes, the bottom most plan will get
25*0fbf3af3SJim Ingham# asked whether to set the program running freely, or to instruction-single-step
26*0fbf3af3SJim Ingham# the current thread.  In the scripted interface, you indicate this by returning
27*0fbf3af3SJim Ingham# False or True respectively from the should_step method.
28*0fbf3af3SJim Ingham#
29*0fbf3af3SJim Ingham# Each time the process stops the thread plan stack for each thread that stopped
30*0fbf3af3SJim Ingham# "for a reason", Ii.e. a single-step completed on that thread, or a breakpoint
31*0fbf3af3SJim Ingham# was hit), is queried to determine how to proceed, starting from the most
32*0fbf3af3SJim Ingham# recently pushed plan, in two stages:
33*0fbf3af3SJim Ingham#
34*0fbf3af3SJim Ingham# 1) Each plan is asked if it "explains" the stop.  The first plan to claim the
35*0fbf3af3SJim Ingham#    stop wins.  In scripted Thread Plans, this is done by returning True from
36*0fbf3af3SJim Ingham#    the "explains_stop method.  This is how, for instance, control is returned
37*0fbf3af3SJim Ingham#    to the User when the "step-over" plan hits a breakpoint.  The step-over
38*0fbf3af3SJim Ingham#    plan doesn't explain the breakpoint stop, so it returns false, and the
39*0fbf3af3SJim Ingham#    breakpoint hit is propagated up the stack to the "base" thread plan, which
40*0fbf3af3SJim Ingham#    is the one that handles random breakpoint hits.
41*0fbf3af3SJim Ingham#
42*0fbf3af3SJim Ingham# 2) Then the plan that won the first round is asked if the process should stop.
43*0fbf3af3SJim Ingham#    This is done in the "should_stop" method.  The scripted plans actually do
44*0fbf3af3SJim Ingham#    three jobs in should_stop:
45*0fbf3af3SJim Ingham#      a) They determine if they have completed their job or not.  If they have
46*0fbf3af3SJim Ingham#         they indicate that by calling SetPlanComplete on their thread plan.
47*0fbf3af3SJim Ingham#      b) They decide whether they want to return control to the user or not.
48*0fbf3af3SJim Ingham#         They do this by returning True or False respectively.
49*0fbf3af3SJim Ingham#      c) If they are not done, they set up whatever machinery they will use
50*0fbf3af3SJim Ingham#         the next time the thread continues.
51*0fbf3af3SJim Ingham#
52*0fbf3af3SJim Ingham#    Note that deciding to return control to the user, and deciding your plan
53*0fbf3af3SJim Ingham#    is done, are orthgonal operations.  You could set up the next phase of
54*0fbf3af3SJim Ingham#    stepping, and then return True from should_stop, and when the user next
55*0fbf3af3SJim Ingham#    "continued" the process your plan would resume control.  Of course, the
56*0fbf3af3SJim Ingham#    user might also "step-over" or some other operation that would push a
57*0fbf3af3SJim Ingham#    different plan, which would take control till it was done.
58*0fbf3af3SJim Ingham#
59*0fbf3af3SJim Ingham#    One other detail you should be aware of, if the plan below you on the
60*0fbf3af3SJim Ingham#    stack was done, then it will be popped and the next plan will take control
61*0fbf3af3SJim Ingham#    and its "should_stop" will be called.
62*0fbf3af3SJim Ingham#
63*0fbf3af3SJim Ingham#    Note also, there should be another method called when your plan is popped,
64*0fbf3af3SJim Ingham#    to allow you to do whatever cleanup is required.  I haven't gotten to that
65*0fbf3af3SJim Ingham#    yet.  For now you should do that at the same time you mark your plan complete.
66*0fbf3af3SJim Ingham#
67*0fbf3af3SJim Ingham# Both examples show stepping through an address range for 20 bytes from the
68*0fbf3af3SJim Ingham# current PC.  The first one does it by single stepping and checking a condition.
69*0fbf3af3SJim Ingham# It doesn't, however handle the case where you step into another frame while
70*0fbf3af3SJim Ingham# still in the current range in the starting frame.
71*0fbf3af3SJim Ingham#
72*0fbf3af3SJim Ingham# That is better handled in the second example by using the built-in StepOverRange
73*0fbf3af3SJim Ingham# thread plan.
74*0fbf3af3SJim Ingham#
75*0fbf3af3SJim Ingham# To use these stepping modes, you would do:
76*0fbf3af3SJim Ingham#
77*0fbf3af3SJim Ingham#     (lldb) command script import scripted_step.py
78*0fbf3af3SJim Ingham#     (lldb) thread step-scripted -C scripted_step.SimpleStep
79*0fbf3af3SJim Ingham# or
80*0fbf3af3SJim Ingham#
81*0fbf3af3SJim Ingham#     (lldb) thread step-scripted -C scripted_step.StepWithPlan
82*0fbf3af3SJim Ingham
83*0fbf3af3SJim Inghamimport lldb
84*0fbf3af3SJim Ingham
85*0fbf3af3SJim Inghamclass SimpleStep:
86*0fbf3af3SJim Ingham    def __init__ (self, thread_plan, dict):
87*0fbf3af3SJim Ingham        self.thread_plan = thread_plan
88*0fbf3af3SJim Ingham        self.start_address = thread_plan.GetThread().GetFrameAtIndex(0).GetPC()
89*0fbf3af3SJim Ingham
90*0fbf3af3SJim Ingham    def explains_stop (self, event):
91*0fbf3af3SJim Ingham        # We are stepping, so if we stop for any other reason, it isn't
92*0fbf3af3SJim Ingham        # because of us.
93*0fbf3af3SJim Ingham        if self.thread_plan.GetThread().GetStopReason()== lldb.eStopReasonTrace:
94*0fbf3af3SJim Ingham            return True
95*0fbf3af3SJim Ingham        else:
96*0fbf3af3SJim Ingham            return False
97*0fbf3af3SJim Ingham
98*0fbf3af3SJim Ingham    def should_stop (self, event):
99*0fbf3af3SJim Ingham        cur_pc = self.thread_plan.GetThread().GetFrameAtIndex(0).GetPC()
100*0fbf3af3SJim Ingham
101*0fbf3af3SJim Ingham        if cur_pc < self.start_address or cur_pc >= self.start_address + 20:
102*0fbf3af3SJim Ingham            self.thread_plan.SetPlanComplete(True)
103*0fbf3af3SJim Ingham                return True
104*0fbf3af3SJim Ingham        else:
105*0fbf3af3SJim Ingham            return False
106*0fbf3af3SJim Ingham
107*0fbf3af3SJim Ingham    def should_step (self):
108*0fbf3af3SJim Ingham        return True
109*0fbf3af3SJim Ingham
110*0fbf3af3SJim Inghamclass StepWithPlan:
111*0fbf3af3SJim Ingham    def__init__ (self,thread_plan, dict):
112*0fbf3af3SJim Ingham    self.thread_plan = thread_plan
113*0fbf3af3SJim Ingham    self.start_address = thread_plan.GetThread().GetFrameAtIndex(0).GetPCAddress()
114*0fbf3af3SJim Ingham    self.step_thread_plan =thread_plan.QueueThreadPlanForStepOverRange(self.start_address, 20);
115*0fbf3af3SJim Ingham
116*0fbf3af3SJim Ingham    defexplains_stop (self, event):
117*0fbf3af3SJim Ingham    # Since all I'm doing is running a plan, I will only ever get askedthis
118*0fbf3af3SJim Ingham    # if myplan doesn't explain the stop, and in that caseI don'teither.
119*0fbf3af3SJim Ingham        return False
120*0fbf3af3SJim Ingham
121*0fbf3af3SJim Inghamdefshould_stop (self, event):
122*0fbf3af3SJim Inghamif self.step_thread_plan.IsPlanComplete():
123*0fbf3af3SJim Ingham            self.thread_plan.SetPlanComplete(True)
124*0fbf3af3SJim Ingham            return True
125*0fbf3af3SJim Inghamelse:
126*0fbf3af3SJim Ingham        return False
127*0fbf3af3SJim Ingham
128*0fbf3af3SJim Inghamdefshould_step (self):
129*0fbf3af3SJim Inghamreturn False
130*0fbf3af3SJim Ingham
131