summaryrefslogtreecommitdiffstats
path: root/lldb/include/lldb/Target/ThreadPlan.h
blob: 96a132647376030af58891dc323d813ed518ffe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//===-- ThreadPlan.h --------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_ThreadPlan_h_
#define liblldb_ThreadPlan_h_

// C Includes
// C++ Includes
#include <string>
// Other libraries and framework includes
// Project includes
#include "lldb/lldb-private.h"
#include "lldb/Core/UserID.h"
#include "lldb/Host/Mutex.h"

namespace lldb_private {

//------------------------------------------------------------------
//  ThreadPlan:
//  This is the pure virtual base class for thread plans.
//
//  The thread plans provide the "atoms" of behavior that
//  all the logical process control, either directly from commands or through
//  more complex composite plans will rely on.
//
//  Plan Stack:
//
//  The thread maintaining a thread plan stack, and you program the actions of a particular thread
//  by pushing plans onto the plan stack.
//  There is always a "Current" plan, which is the head of the plan stack, though in some cases
//  a plan may defer to plans higher in the stack for some piece of information.
//
//  The plan stack is never empty, there is always a Base Plan which persists through the life
//  of the running process.
//
//
//  DEPRECATED: This ended up causing a real hassle, too many cases where the immediate plan
//  got stranded.  So the better way to do this is to post any plans you need to do right before
//  running in the PrepareToResume method.
//f
//  Immediate Plans:
//
//  One other complexity of the plan stack is that sometimes you need to do a piece of work immediately
//  on resume, regardless of what other plans have been pushed on the stack while the process has
//  been stopped.  The classic example is stepping over a breakpoint.  To that end the plan stack is
//  actually two stacks, an "immediate" plan stack and the normal plan stack.  A plan can indicate that it
//  should go on the immediate plan stack by returning "true" from the IsImmediate method.
//
//  END DEPRECATED...
//
//  Creating Plans:
//
//  The thread plan is generally created and added to the plan stack through the QueueThreadPlanFor... API
//  in lldb::Thread.  Those API's will return the plan that performs the named operation in a manner
//  appropriate for the current process.  The plans in lldb/source/Target are generic
//  implementations, but a Process plugin can override them.
//
//  ValidatePlan is then called.  If it returns false, the plan is unshipped.  This is a little
//  convenience which keeps us from having to error out of the constructor.
//
//  Then the plan is added to the plan stack.  When the plan is added to the plan stack its DidPush
//  will get called.  This is useful if a plan wants to push any additional plans as it is constructed,
//  since you need to make sure you're already on the stack before you push additional plans.
//
//  Completed Plans:
//
//  When the target process stops the plans are queried, among other things, for whether their job is done.
//  If it is they are moved from the plan stack to the Completed Plan stack in reverse order from their position
//  on the plan stack (since multiple plans may be done at a given stop.)  This is used primarily so that
//  the lldb::Thread::StopInfo for the thread can be set properly.  If one plan pushes another to achieve part of
//  its job, but it doesn't want that sub-plan to be the one that sets the StopInfo, then call SetPrivate on the
//  sub-plan when you create it, and the Thread will pass over that plan in reporting the reason for the stop.
//
//  When the plan is moved from the plan stack to the completed plan stack its DidPop method is called.  You should
//  undo anything that affects target state in this method so the target state is clear for new plans.
//  But be sure to leave whatever state might be needed to correctly fill the StopInfo.
//
//  Over the lifetime of the plan, various methods of the ThreadPlan are then called in response to changes of state in
//  the process we are debugging as follows:
//
//  Resuming:
//
//  When the target process is about to be restarted, the plan's WillResume method is called,
//  giving the plan a chance to prepare for the run.  If WillResume returns false, then the
//  process is not restarted.  Be sure to set an appropriate error value in the Process if
//  you have to do this.
//  Next the "StopOthers" method of all the threads are polled, and if one thread's Current plan
//  returns "true" then only that thread gets to run.  If more than one returns "true" the threads that want to run solo
//  get run one by one round robin fashion.  Otherwise all are let to run.
//  Finally, for each thread that is running, it run state is set to the return of RunState from the
//  thread's Current plan.
//
//  Responding to a stop:
//
//  When the target process stops, the plan is called in the following stages:
//
//  First the thread asks the Current Plan if it can handle this stop by calling PlanExplainsStop.
//  If the Current plan answers "true" then it is asked if the stop should percolate all the way to the
//  user by calling the ShouldStop method.  If the current plan doesn't explain the stop, then we query down
//  the plan stack for a plan that does explain the stop.  The plan that does explain the stop then needs to
//  figure out what to do about the plans below it in the stack.  If the stop is recoverable, then the plan that
//  understands it can just do what it needs to set up to restart, and then continue.
//  Otherwise, the plan that understood the stop should call DiscardPlanStack to clean up the stack below it.
//  In the normal case, this will just collapse the plan stack up to the point of the plan that understood
//  the stop reason.  However, if a plan wishes to stay on the stack after an event it didn't directly handle
//  it can designate itself a "Master" plan by responding true to IsMasterPlan, and then if it wants not to be
//  discarded, it can return true to OkayToDiscard, and it and all its dependent plans will be preserved when
//  we resume execution.
//
//  Actually Stopping:
//
//  If a plan says responds "true" to ShouldStop, then it is asked if it's job is complete by calling
//  MischiefManaged.  If that returns true, the thread is popped from the plan stack and added to the
//  Completed Plan Stack.  Then the next plan in the stack is asked if it ShouldStop, and  it returns "true",
//  it is asked if it is done, and if yes popped, and so on till we reach a plan that is not done.
//
//  Since you often know in the ShouldStop method whether your plan is complete, as a convenience you can call
//  SetPlanComplete and the ThreadPlan implementation of MischiefManaged will return "true", without your having
//  to redo the calculation when your sub-classes MischiefManaged is called.  If you call SetPlanComplete, you can
//  later use IsPlanComplete to determine whether the plan is complete.  This is only a convenience for sub-classes,
//  the logic in lldb::Thread will only call MischiefManaged.
//
//  One slightly tricky point is you have to be careful using SetPlanComplete in PlanExplainsStop because you
//  are not guaranteed that PlanExplainsStop for a plan will get called before ShouldStop gets called.  If your sub-plan
//  explained the stop and then popped itself, only your ShouldStop will get called.
//
//  If ShouldStop for any thread returns "true", then the WillStop method of the Current plan of
//  all threads will be called, the stop event is placed on the Process's public broadcaster, and
//  control returns to the upper layers of the debugger.
//
//  Automatically Resuming:
//
//  If ShouldStop for all threads returns "false", then the target process will resume.  This then cycles back to
//  Resuming above.
//
//  Reporting eStateStopped events when the target is restarted:
//
//  If a plan decides to auto-continue the target by returning "false" from ShouldStop, then it will be asked
//  whether the Stopped event should still be reported.  For instance, if you hit a breakpoint that is a User set
//  breakpoint, but the breakpoint callback said to continue the target process, you might still want to inform
//  the upper layers of lldb that the stop had happened.
//  The way this works is every thread gets to vote on whether to report the stop.  If all votes are eVoteNoOpinion,
//  then the thread list will decide what to do (at present it will pretty much always suppress these stopped events.)
//  If there is an eVoteYes, then the event will be reported regardless of the other votes.  If there is an eVoteNo
//  and no eVoteYes's, then the event won't be reported.
//
//  One other little detail here, sometimes a plan will push another plan onto the plan stack to do some part of
//  the first plan's job, and it would be convenient to tell that plan how it should respond to ShouldReportStop.
//  You can do that by setting the stop_vote in the child plan when you create it.
//
//  Suppressing the initial eStateRunning event:
//
//  The private process running thread will take care of ensuring that only one "eStateRunning" event will be
//  delivered to the public Process broadcaster per public eStateStopped event.  However there are some cases
//  where the public state of this process is eStateStopped, but a thread plan needs to restart the target, but
//  doesn't want the running event to be publically broadcast.  The obvious example of this is running functions
//  by hand as part of expression evaluation.  To suppress the running event return eVoteNo from ShouldReportStop,
//  to force a running event to be reported return eVoteYes, in general though you should return eVoteNoOpinion
//  which will allow the ThreadList to figure out the right thing to do.
//  The run_vote argument to the constructor works like stop_vote, and is a way for a plan to instruct a sub-plan\
//  on how to respond to ShouldReportStop.
//
//------------------------------------------------------------------

class ThreadPlan:
    public UserID
{
public:
    typedef enum
    {
        eAllThreads,
        eSomeThreads,
        eThisThread
    } ThreadScope;

    //------------------------------------------------------------------
    // Constructors and Destructors
    //------------------------------------------------------------------
    ThreadPlan (const char *name,
                Thread &thread,
                lldb::Vote stop_vote,
                lldb::Vote run_vote);

    virtual
    ~ThreadPlan();

    //------------------------------------------------------------------
    /// Returns the name of this thread plan.
    ///
    /// @return
    ///   A const char * pointer to the thread plan's name.
    //------------------------------------------------------------------
    const char *
    GetName () const;

    //------------------------------------------------------------------
    /// Returns the Thread that is using this thread plan.
    ///
    /// @return
    ///   A  pointer to the thread plan's owning thread.
    //------------------------------------------------------------------
    Thread &
    GetThread();

    const Thread &
    GetThread() const;

    //------------------------------------------------------------------
    /// Print a description of this thread to the stream \a s.
    /// \a thread.
    ///
    /// @param[in] s
    ///    The stream to which to print the description.
    ///
    /// @param[in] level
    ///    The level of description desired.  Note that eDescriptionLevelBrief
    ///    will be used in the stop message printed when the plan is complete.
    //------------------------------------------------------------------
    virtual void
    GetDescription (Stream *s,
                    lldb::DescriptionLevel level) = 0;

    //------------------------------------------------------------------
    /// Returns whether this plan needs to be executed immediatly on resume.
    ///
    /// @return
    ///   \b true if the plan is immediate, \b false otherwise.
    //------------------------------------------------------------------
    virtual bool
    IsImmediate() const
    {
        return false;
    }

    //------------------------------------------------------------------
    /// Returns whether this plan could be successfully created.
    ///
    /// @param[in] error
    ///    A stream to which to print some reason why the plan could not be created.
    ///
    /// @return
    ///   \b true if the plan should be queued, \b false otherwise.
    //------------------------------------------------------------------
    virtual bool
    ValidatePlan (Stream *error) = 0;

    virtual bool
    PlanExplainsStop () = 0;


    virtual lldb::StateType
    RunState () = 0;

    virtual bool
    ShouldStop (Event *event_ptr) = 0;

    // Whether a "stop class" event should be reported to the "outside world".  In general
    // if a thread plan is active, events should not be reported.

    virtual lldb::Vote
    ShouldReportStop (Event *event_ptr);

    virtual lldb::Vote
    ShouldReportRun (Event *event_ptr);

    virtual bool
    StopOthers ();

    virtual bool
    WillResume (lldb::StateType resume_state, bool current_plan);

    virtual bool
    WillStop () = 0;

    virtual bool
    IsMasterPlan()
    {
        return false;
    }

    virtual bool
    OkayToDiscard();

    void
    SetOkayToDiscard (bool value)
    {
        m_okay_to_discard = value;
    }
    
    // The base class MischiefManaged does some cleanup - so you have to call it
    // in your MischiefManaged derived class.
    virtual bool
    MischiefManaged ();

    bool
    GetPrivate ();

    void
    SetPrivate (bool input);

    virtual void
    DidPush();

    virtual void
    WillPop();

    // This pushes \a plan onto the plan stack of the current plan's thread.
    void
    PushPlan (lldb::ThreadPlanSP &thread_plan_sp);

protected:
    //------------------------------------------------------------------
    // Classes that inherit from ThreadPlan can see and modify these
    //------------------------------------------------------------------

    bool
    IsPlanComplete();

    void
    SetPlanComplete ();

    // This gets the previous plan to the current plan (for forwarding requests).
    // This is mostly a formal requirement, it allows us to make the Thread's
    // GetPreviousPlan protected, but only friend ThreadPlan to thread.

    ThreadPlan *
    GetPreviousPlan ();

    Thread &m_thread;
    lldb::Vote m_stop_vote;
    lldb::Vote m_run_vote;

private:
    //------------------------------------------------------------------
    // For ThreadPlan only
    //------------------------------------------------------------------
    static lldb::user_id_t GetNextID ();

    std::string m_name;
    Mutex m_plan_complete_mutex;
    bool m_plan_complete;
    bool m_plan_private;
    bool m_okay_to_discard;

private:
    DISALLOW_COPY_AND_ASSIGN(ThreadPlan);
};


} // namespace lldb_private

#endif  // liblldb_ThreadPlan_h_
OpenPOWER on IntegriCloud