Thread.h revision c833295baeec641086f536e78050388af36784f8
1//===-- Thread.h ------------------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_Thread_h_
11#define liblldb_Thread_h_
12
13#include "lldb/lldb-private.h"
14#include "lldb/Host/Mutex.h"
15#include "lldb/Core/UserID.h"
16#include "lldb/Target/ExecutionContextScope.h"
17#include "lldb/Target/StackFrameList.h"
18#include "libunwind/include/libunwind.h"
19
20#define LLDB_THREAD_MAX_STOP_EXC_DATA 8
21
22// I forward declare these here so I don't have to #include ThreadPlan, so in turn I
23// can use Thread.h in ThreadPlan.h.
24
25namespace lldb_private {
26
27class Thread :
28    public UserID,
29    public ExecutionContextScope
30{
31public:
32
33    class RegisterCheckpoint
34    {
35    public:
36
37        RegisterCheckpoint() :
38            m_stack_id (),
39            m_data_sp ()
40        {
41        }
42
43        RegisterCheckpoint (const StackID &stack_id) :
44            m_stack_id (stack_id),
45            m_data_sp ()
46        {
47        }
48
49        ~RegisterCheckpoint()
50        {
51        }
52
53        const StackID &
54        GetStackID()
55        {
56            return m_stack_id;
57        }
58
59        void
60        SetStackID (const StackID &stack_id)
61        {
62            m_stack_id = stack_id;
63        }
64
65        lldb::DataBufferSP &
66        GetData()
67        {
68            return m_data_sp;
69        }
70
71        const lldb::DataBufferSP &
72        GetData() const
73        {
74            return m_data_sp;
75        }
76
77    protected:
78        StackID m_stack_id;
79        lldb::DataBufferSP m_data_sp;
80    };
81
82    Thread (Process &process, lldb::tid_t tid);
83    virtual ~Thread();
84
85    Process &
86    GetProcess() { return m_process; }
87
88    const Process &
89    GetProcess() const { return m_process; }
90
91    int
92    GetResumeSignal () const;
93
94    void
95    SetResumeSignal (int signal);
96
97    lldb::StateType
98    GetState() const;
99
100    lldb::ThreadSP
101    GetSP ();
102
103    void
104    SetState (lldb::StateType state);
105
106    lldb::StateType
107    GetResumeState () const;
108
109    void
110    SetResumeState (lldb::StateType state);
111
112    // This function is called on all the threads before "WillResume" in case
113    // a thread needs to change its state before the ThreadList polls all the
114    // threads to figure out which ones actually will get to run and how.
115    void
116    SetupForResume ();
117
118    // Override this to do platform specific tasks before resume, but always
119    // call the Thread::WillResume at the end of your work.
120
121    virtual bool
122    WillResume (lldb::StateType resume_state);
123
124    // This clears generic thread state after a resume.  If you subclass this,
125    // be sure to call it.
126    virtual void
127    DidResume ();
128
129    virtual void
130    RefreshStateAfterStop() = 0;
131
132    void
133    WillStop ();
134
135    bool
136    ShouldStop (Event *event_ptr);
137
138    lldb::Vote
139    ShouldReportStop (Event *event_ptr);
140
141    lldb::Vote
142    ShouldReportRun (Event *event_ptr);
143
144    // Return whether this thread matches the specification in ThreadSpec.  This is a virtual
145    // method because at some point we may extend the thread spec with a platform specific
146    // dictionary of attributes, which then only the platform specific Thread implementation
147    // would know how to match.  For now, this just calls through to the ThreadSpec's
148    // ThreadPassesBasicTests method.
149    virtual bool
150    MatchesSpec (const ThreadSpec *spec);
151
152    StopInfo *
153    GetStopInfo ();
154
155    bool
156    ThreadStoppedForAReason ();
157
158    virtual const char *
159    GetInfo () = 0;
160
161    virtual const char *
162    GetName ()
163    {
164        return NULL;
165    }
166
167    virtual const char *
168    GetQueueName ()
169    {
170        return NULL;
171    }
172
173    virtual uint32_t
174    GetStackFrameCount();
175
176    virtual lldb::StackFrameSP
177    GetStackFrameAtIndex (uint32_t idx);
178
179    lldb::StackFrameSP
180    GetSelectedFrame ();
181
182    uint32_t
183    SetSelectedFrame (lldb_private::StackFrame *frame);
184
185    void
186    SetSelectedFrameByIndex (uint32_t frame_idx);
187
188    virtual RegisterContext *
189    GetRegisterContext () = 0;
190
191    virtual bool
192    SaveFrameZeroState (RegisterCheckpoint &checkpoint) = 0;
193
194    virtual bool
195    RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint) = 0;
196
197    virtual RegisterContext *
198    CreateRegisterContextForFrame (StackFrame *frame) = 0;
199
200    virtual void
201    ClearStackFrames ();
202
203    void
204    DumpInfo (Stream &strm,
205              bool show_stop_reason,
206              bool show_name,
207              bool show_queue,
208              uint32_t frame_idx);// = UINT32_MAX);
209
210    //------------------------------------------------------------------
211    // Thread Plan Providers:
212    // This section provides the basic thread plans that the Process control
213    // machinery uses to run the target.  ThreadPlan.h provides more details on
214    // how this mechanism works.
215    // The thread provides accessors to a set of plans that perform basic operations.
216    // The idea is that particular Platform plugins can override these methods to
217    // provide the implementation of these basic operations appropriate to their
218    // environment.
219    //------------------------------------------------------------------
220
221    //------------------------------------------------------------------
222    /// Queues the base plan for a thread.
223    /// The version returned by Process does some things that are useful,
224    /// like handle breakpoints and signals, so if you return a plugin specific
225    /// one you probably want to call through to the Process one for anything
226    /// your plugin doesn't explicitly handle.
227    ///
228    /// @param[in] abort_other_plans
229    ///    \b true if we discard the currently queued plans and replace them with this one.
230    ///    Otherwise this plan will go on the end of the plan stack.
231    ///
232    /// @return
233    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
234    //------------------------------------------------------------------
235    virtual ThreadPlan *
236    QueueFundamentalPlan (bool abort_other_plans);
237
238    //------------------------------------------------------------------
239    /// Queues the plan used to step over a breakpoint at the current PC of \a thread.
240    /// The default version returned by Process handles trap based breakpoints, and
241    /// will disable the breakpoint, single step over it, then re-enable it.
242    ///
243    /// @param[in] abort_other_plans
244    ///    \b true if we discard the currently queued plans and replace them with this one.
245    ///    Otherwise this plan will go on the end of the plan stack.
246    ///
247    /// @return
248    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
249    //------------------------------------------------------------------
250    virtual ThreadPlan *
251    QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans);
252
253    //------------------------------------------------------------------
254    /// Queues the plan used to step one instruction from the current PC of \a thread.
255    ///
256    /// @param[in] step_over
257    ///    \b true if we step over calls to functions, false if we step in.
258    ///
259    /// @param[in] abort_other_plans
260    ///    \b true if we discard the currently queued plans and replace them with this one.
261    ///    Otherwise this plan will go on the end of the plan stack.
262    ///
263    /// @param[in] stop_other_threads
264    ///    \b true if we will stop other threads while we single step this one.
265    ///
266    /// @return
267    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
268    //------------------------------------------------------------------
269    virtual ThreadPlan *
270    QueueThreadPlanForStepSingleInstruction (bool step_over,
271                                             bool abort_other_plans,
272                                             bool stop_other_threads);
273
274    //------------------------------------------------------------------
275    /// Queues the plan used to step through an address range, stepping into or over
276    /// function calls depending on the value of StepType.
277    ///
278    /// @param[in] abort_other_plans
279    ///    \b true if we discard the currently queued plans and replace them with this one.
280    ///    Otherwise this plan will go on the end of the plan stack.
281    ///
282    /// @param[in] type
283    ///    Type of step to do, only eStepTypeInto and eStepTypeOver are supported by this plan.
284    ///
285    /// @param[in] range
286    ///    The address range to step through.
287    ///
288    /// @param[in] addr_context
289    ///    When dealing with stepping through inlined functions the current PC is not enough information to know
290    ///    what "step" means.  For instance a series of nested inline functions might start at the same address.
291    //     The \a addr_context provides the current symbol context the step
292    ///    is supposed to be out of.
293    //   FIXME: Currently unused.
294    ///
295    /// @param[in] stop_other_threads
296    ///    \b true if we will stop other threads while we single step this one.
297    ///
298    /// @return
299    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
300    //------------------------------------------------------------------
301    virtual ThreadPlan *
302    QueueThreadPlanForStepRange (bool abort_other_plans,
303                                 lldb::StepType type,
304                                 const AddressRange &range,
305                                 const SymbolContext &addr_context,
306                                 lldb::RunMode stop_other_threads,
307                                 bool avoid_code_without_debug_info);
308
309    //------------------------------------------------------------------
310    /// Queue the plan used to step out of the function at the current PC of
311    /// \a thread.
312    ///
313    /// @param[in] abort_other_plans
314    ///    \b true if we discard the currently queued plans and replace them with this one.
315    ///    Otherwise this plan will go on the end of the plan stack.
316    ///
317    /// @param[in] addr_context
318    ///    When dealing with stepping through inlined functions the current PC is not enough information to know
319    ///    what "step" means.  For instance a series of nested inline functions might start at the same address.
320    //     The \a addr_context provides the current symbol context the step
321    ///    is supposed to be out of.
322    //   FIXME: Currently unused.
323    ///
324    /// @param[in] first_insn
325    ///     \b true if this is the first instruction of a function.
326    ///
327    /// @param[in] stop_other_threads
328    ///    \b true if we will stop other threads while we single step this one.
329    ///
330    /// @param[in] stop_vote
331    /// @param[in] run_vote
332    ///    See standard meanings for the stop & run votes in ThreadPlan.h.
333    ///
334    /// @return
335    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
336    //------------------------------------------------------------------
337    virtual ThreadPlan *
338    QueueThreadPlanForStepOut (bool abort_other_plans,
339                               SymbolContext *addr_context,
340                               bool first_insn,
341                               bool stop_other_threads,
342                               lldb::Vote stop_vote = lldb::eVoteYes,
343                               lldb::Vote run_vote = lldb::eVoteNoOpinion);
344
345    //------------------------------------------------------------------
346    /// Gets the plan used to step through the code that steps from a function
347    /// call site at the current PC into the actual function call.
348    ///
349    /// @param[in] abort_other_plans
350    ///    \b true if we discard the currently queued plans and replace them with this one.
351    ///    Otherwise this plan will go on the end of the plan stack.
352    ///
353    /// @param[in] stop_other_threads
354    ///    \b true if we will stop other threads while we single step this one.
355    ///
356    /// @return
357    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
358    //------------------------------------------------------------------
359    virtual ThreadPlan *
360    QueueThreadPlanForStepThrough (bool abort_other_plans,
361                                   bool stop_other_threads);
362
363    //------------------------------------------------------------------
364    /// Gets the plan used to continue from the current PC.
365    /// This is a simple plan, mostly useful as a backstop when you are continuing
366    /// for some particular purpose.
367    ///
368    /// @param[in] abort_other_plans
369    ///    \b true if we discard the currently queued plans and replace them with this one.
370    ///    Otherwise this plan will go on the end of the plan stack.
371    ///
372    /// @param[in] target_addr
373    ///    The address to which we're running.
374    ///
375    /// @param[in] stop_other_threads
376    ///    \b true if we will stop other threads while we single step this one.
377    ///
378    /// @return
379    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
380    //------------------------------------------------------------------
381    virtual ThreadPlan *
382    QueueThreadPlanForRunToAddress (bool abort_other_plans,
383                                    Address &target_addr,
384                                    bool stop_other_threads);
385
386    virtual ThreadPlan *
387    QueueThreadPlanForStepUntil (bool abort_other_plans,
388                               lldb::addr_t *address_list,
389                               size_t num_addresses,
390                               bool stop_others);
391
392    virtual ThreadPlan *
393    QueueThreadPlanForCallFunction (bool abort_other_plans,
394                                    Address& function,
395                                    lldb::addr_t arg,
396                                    bool stop_other_threads,
397                                    bool discard_on_error = false);
398
399    virtual ThreadPlan *
400    QueueThreadPlanForCallFunction (bool abort_other_plans,
401                                    Address& function,
402                                    ValueList &args,
403                                    bool stop_other_threads,
404                                    bool discard_on_error = false);
405
406    //------------------------------------------------------------------
407    // Thread Plan accessors:
408    //------------------------------------------------------------------
409
410    //------------------------------------------------------------------
411    /// Gets the plan which will execute next on the plan stack.
412    ///
413    /// @return
414    ///     A pointer to the next executed plan.
415    //------------------------------------------------------------------
416    ThreadPlan *
417    GetCurrentPlan ();
418
419    //------------------------------------------------------------------
420    /// Gets the inner-most plan that was popped off the plan stack in the
421    /// most recent stop.  Useful for printing the stop reason accurately.
422    ///
423    /// @return
424    ///     A pointer to the last completed plan.
425    //------------------------------------------------------------------
426    lldb::ThreadPlanSP
427    GetCompletedPlan ();
428
429    //------------------------------------------------------------------
430    ///  Checks whether the given plan is in the completed plans for this
431    ///  stop.
432    ///
433    /// @param[in] plan
434    ///     Pointer to the plan you're checking.
435    ///
436    /// @return
437    ///     Returns true if the input plan is in the completed plan stack,
438    ///     false otherwise.
439    //------------------------------------------------------------------
440    bool
441    IsThreadPlanDone (ThreadPlan *plan);
442
443    //------------------------------------------------------------------
444    ///  Checks whether the given plan is in the discarded plans for this
445    ///  stop.
446    ///
447    /// @param[in] plan
448    ///     Pointer to the plan you're checking.
449    ///
450    /// @return
451    ///     Returns true if the input plan is in the discarded plan stack,
452    ///     false otherwise.
453    //------------------------------------------------------------------
454    bool
455    WasThreadPlanDiscarded (ThreadPlan *plan);
456
457    //------------------------------------------------------------------
458    /// Queues a generic thread plan.
459    ///
460    /// @param[in] plan_sp
461    ///    The plan to queue.
462    ///
463    /// @param[in] abort_other_plans
464    ///    \b true if we discard the currently queued plans and replace them with this one.
465    ///    Otherwise this plan will go on the end of the plan stack.
466    ///
467    /// @return
468    ///     A pointer to the last completed plan.
469    //------------------------------------------------------------------
470    void
471    QueueThreadPlan (lldb::ThreadPlanSP &plan_sp, bool abort_other_plans);
472
473
474    //------------------------------------------------------------------
475    /// Discards the plans queued on the plan stack of the current thread.  This is
476    /// arbitrated by the "Master" ThreadPlans, using the "OkayToDiscard" call.
477    //  But if \a force is true, all thread plans are discarded.
478    //------------------------------------------------------------------
479    void
480    DiscardThreadPlans (bool force);
481
482    //------------------------------------------------------------------
483    /// Prints the current plan stack.
484    ///
485    /// @param[in] s
486    ///    The stream to which to dump the plan stack info.
487    ///
488    //------------------------------------------------------------------
489    void
490    DumpThreadPlans (Stream *s) const;
491
492    // Get the thread index ID. The index ID that is guaranteed to not be
493    // re-used by a process. They start at 1 and increase with each new thread.
494    // This allows easy command line access by a unique ID that is easier to
495    // type than the actual system thread ID.
496    uint32_t
497    GetIndexID () const;
498
499    //------------------------------------------------------------------
500    // lldb::ExecutionContextScope pure virtual functions
501    //------------------------------------------------------------------
502    virtual Target *
503    CalculateTarget ();
504
505    virtual Process *
506    CalculateProcess ();
507
508    virtual Thread *
509    CalculateThread ();
510
511    virtual StackFrame *
512    CalculateStackFrame ();
513
514    virtual void
515    Calculate (ExecutionContext &exe_ctx);
516
517protected:
518
519    friend class ThreadPlan;
520    friend class StackFrameList;
521
522    void
523    PushPlan (lldb::ThreadPlanSP &plan_sp);
524
525    void
526    PopPlan ();
527
528    void
529    DiscardPlan ();
530
531    ThreadPlan *GetPreviousPlan (ThreadPlan *plan);
532
533    virtual lldb::StopInfoSP
534    GetPrivateStopReason () = 0;
535
536    typedef std::vector<lldb::ThreadPlanSP> plan_stack;
537
538    virtual lldb_private::Unwind *
539    GetUnwinder () = 0;
540
541    StackFrameList &
542    GetStackFrameList ();
543
544    //------------------------------------------------------------------
545    // Classes that inherit from Process can see and modify these
546    //------------------------------------------------------------------
547    Process &           m_process;          ///< The process that owns this thread.
548    lldb::StopInfoSP    m_public_stop_info_sp;     ///< The public stop reason for this thread
549    lldb::StopInfoSP    m_actual_stop_info_sp;     ///< The private stop reason for this thread
550    const uint32_t      m_index_id;         ///< A unique 1 based index assigned to each thread for easy UI/command line access.
551    lldb::RegisterContextSP   m_reg_context_sp;   ///< The register context for this thread's current register state.
552    lldb::StateType     m_state;            ///< The state of our process.
553    mutable Mutex       m_state_mutex;      ///< Multithreaded protection for m_state.
554    plan_stack          m_plan_stack;       ///< The stack of plans this thread is executing.
555    plan_stack          m_immediate_plan_stack; ///< The plans that need to get executed before any other work gets done.
556    plan_stack          m_completed_plan_stack;  ///< Plans that have been completed by this stop.  They get deleted when the thread resumes.
557    plan_stack          m_discarded_plan_stack;  ///< Plans that have been discarded by this stop.  They get deleted when the thread resumes.
558    std::auto_ptr<StackFrameList> m_curr_frames_ap; ///< The stack frames that get lazily populated after a thread stops.
559    std::auto_ptr<StackFrameList> m_prev_frames_ap; ///< The previous stack frames from the last time this thread stopped.
560    int                 m_resume_signal;    ///< The signal that should be used when continuing this thread.
561    lldb::StateType     m_resume_state;     ///< The state that indicates what this thread should do when the process is resumed.
562    std::auto_ptr<lldb_private::Unwind> m_unwinder_ap;
563private:
564    //------------------------------------------------------------------
565    // For Thread only
566    //------------------------------------------------------------------
567    DISALLOW_COPY_AND_ASSIGN (Thread);
568
569};
570
571} // namespace lldb_private
572
573#endif  // liblldb_Thread_h_
574