Thread.h revision cdea2365030d283cd9082fa0b80bead79a104e55
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/Core/UserSettingsController.h"
17#include "lldb/Target/ExecutionContextScope.h"
18#include "lldb/Target/StackFrameList.h"
19#include "libunwind/include/libunwind.h"
20
21#define LLDB_THREAD_MAX_STOP_EXC_DATA 8
22
23namespace lldb_private {
24
25class ThreadInstanceSettings : public InstanceSettings
26{
27public:
28
29    ThreadInstanceSettings (UserSettingsController &owner, bool live_instance = true, const char *name = NULL);
30
31    ThreadInstanceSettings (const ThreadInstanceSettings &rhs);
32
33    virtual
34    ~ThreadInstanceSettings ();
35
36    ThreadInstanceSettings&
37    operator= (const ThreadInstanceSettings &rhs);
38
39
40    void
41    UpdateInstanceSettingsVariable (const ConstString &var_name,
42                                    const char *index_value,
43                                    const char *value,
44                                    const ConstString &instance_name,
45                                    const SettingEntry &entry,
46                                    lldb::VarSetOperationType op,
47                                    Error &err,
48                                    bool pending);
49
50    bool
51    GetInstanceSettingsValue (const SettingEntry &entry,
52                              const ConstString &var_name,
53                              StringList &value,
54                              Error *err);
55
56    RegularExpression *
57    GetSymbolsToAvoidRegexp()
58    {
59        return m_avoid_regexp_ap.get();
60    }
61
62    static const ConstString &
63    StepAvoidRegexpVarName ();
64
65    bool
66    GetTraceEnabledState()
67    {
68        return m_trace_enabled;
69    }
70    static const ConstString &
71    GetTraceThreadVarName ();
72
73protected:
74
75    void
76    CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings,
77                          bool pending);
78
79    const ConstString
80    CreateInstanceName ();
81
82private:
83
84    std::auto_ptr<RegularExpression> m_avoid_regexp_ap;
85    bool m_trace_enabled;
86};
87
88class Thread :
89    public UserID,
90    public ExecutionContextScope,
91    public ThreadInstanceSettings
92{
93public:
94
95    class ThreadSettingsController : public UserSettingsController
96    {
97    public:
98
99        ThreadSettingsController ();
100
101        virtual
102        ~ThreadSettingsController ();
103
104        static SettingEntry global_settings_table[];
105        static SettingEntry instance_settings_table[];
106
107    protected:
108
109        lldb::InstanceSettingsSP
110        CreateInstanceSettings (const char *instance_name);
111
112    private:
113
114        // Class-wide settings.
115
116        DISALLOW_COPY_AND_ASSIGN (ThreadSettingsController);
117    };
118
119    class RegisterCheckpoint
120    {
121    public:
122
123        RegisterCheckpoint() :
124            m_stack_id (),
125            m_data_sp ()
126        {
127        }
128
129        RegisterCheckpoint (const StackID &stack_id) :
130            m_stack_id (stack_id),
131            m_data_sp ()
132        {
133        }
134
135        ~RegisterCheckpoint()
136        {
137        }
138
139        const StackID &
140        GetStackID()
141        {
142            return m_stack_id;
143        }
144
145        void
146        SetStackID (const StackID &stack_id)
147        {
148            m_stack_id = stack_id;
149        }
150
151        lldb::DataBufferSP &
152        GetData()
153        {
154            return m_data_sp;
155        }
156
157        const lldb::DataBufferSP &
158        GetData() const
159        {
160            return m_data_sp;
161        }
162
163    protected:
164        StackID m_stack_id;
165        lldb::DataBufferSP m_data_sp;
166    };
167
168    void
169    UpdateInstanceName ();
170
171    static lldb::UserSettingsControllerSP
172    GetSettingsController (bool finish = false);
173
174    Thread (Process &process, lldb::tid_t tid);
175    virtual ~Thread();
176
177    Process &
178    GetProcess() { return m_process; }
179
180    const Process &
181    GetProcess() const { return m_process; }
182
183    int
184    GetResumeSignal () const;
185
186    void
187    SetResumeSignal (int signal);
188
189    lldb::StateType
190    GetState() const;
191
192    lldb::ThreadSP
193    GetSP ();
194
195    void
196    SetState (lldb::StateType state);
197
198    lldb::StateType
199    GetResumeState () const;
200
201    void
202    SetResumeState (lldb::StateType state);
203
204    // This function is called on all the threads before "WillResume" in case
205    // a thread needs to change its state before the ThreadList polls all the
206    // threads to figure out which ones actually will get to run and how.
207    void
208    SetupForResume ();
209
210    // Override this to do platform specific tasks before resume, but always
211    // call the Thread::WillResume at the end of your work.
212
213    virtual bool
214    WillResume (lldb::StateType resume_state);
215
216    // This clears generic thread state after a resume.  If you subclass this,
217    // be sure to call it.
218    virtual void
219    DidResume ();
220
221    virtual void
222    RefreshStateAfterStop() = 0;
223
224    void
225    WillStop ();
226
227    bool
228    ShouldStop (Event *event_ptr);
229
230    lldb::Vote
231    ShouldReportStop (Event *event_ptr);
232
233    lldb::Vote
234    ShouldReportRun (Event *event_ptr);
235
236    // Return whether this thread matches the specification in ThreadSpec.  This is a virtual
237    // method because at some point we may extend the thread spec with a platform specific
238    // dictionary of attributes, which then only the platform specific Thread implementation
239    // would know how to match.  For now, this just calls through to the ThreadSpec's
240    // ThreadPassesBasicTests method.
241    virtual bool
242    MatchesSpec (const ThreadSpec *spec);
243
244    lldb::StopInfoSP
245    GetStopInfo ();
246
247    bool
248    ThreadStoppedForAReason ();
249
250    static const char *
251    RunModeAsCString (lldb::RunMode mode);
252
253    static const char *
254    StopReasonAsCString (lldb::StopReason reason);
255
256    virtual const char *
257    GetInfo () = 0;
258
259    virtual const char *
260    GetName ()
261    {
262        return NULL;
263    }
264
265    virtual const char *
266    GetQueueName ()
267    {
268        return NULL;
269    }
270
271    virtual uint32_t
272    GetStackFrameCount();
273
274    virtual lldb::StackFrameSP
275    GetStackFrameAtIndex (uint32_t idx);
276
277    uint32_t
278    GetSelectedFrameIndex ();
279
280    lldb::StackFrameSP
281    GetSelectedFrame ();
282
283    uint32_t
284    SetSelectedFrame (lldb_private::StackFrame *frame);
285
286    void
287    SetSelectedFrameByIndex (uint32_t frame_idx);
288
289    virtual RegisterContext *
290    GetRegisterContext () = 0;
291
292    virtual bool
293    SaveFrameZeroState (RegisterCheckpoint &checkpoint) = 0;
294
295    virtual bool
296    RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint) = 0;
297
298    virtual RegisterContext *
299    CreateRegisterContextForFrame (StackFrame *frame) = 0;
300
301    virtual void
302    ClearStackFrames ();
303
304    void
305    DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx);
306
307    //------------------------------------------------------------------
308    // Thread Plan Providers:
309    // This section provides the basic thread plans that the Process control
310    // machinery uses to run the target.  ThreadPlan.h provides more details on
311    // how this mechanism works.
312    // The thread provides accessors to a set of plans that perform basic operations.
313    // The idea is that particular Platform plugins can override these methods to
314    // provide the implementation of these basic operations appropriate to their
315    // environment.
316    //------------------------------------------------------------------
317
318    //------------------------------------------------------------------
319    /// Queues the base plan for a thread.
320    /// The version returned by Process does some things that are useful,
321    /// like handle breakpoints and signals, so if you return a plugin specific
322    /// one you probably want to call through to the Process one for anything
323    /// your plugin doesn't explicitly handle.
324    ///
325    /// @param[in] abort_other_plans
326    ///    \b true if we discard the currently queued plans and replace them with this one.
327    ///    Otherwise this plan will go on the end of the plan stack.
328    ///
329    /// @return
330    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
331    //------------------------------------------------------------------
332    virtual ThreadPlan *
333    QueueFundamentalPlan (bool abort_other_plans);
334
335    //------------------------------------------------------------------
336    /// Queues the plan used to step over a breakpoint at the current PC of \a thread.
337    /// The default version returned by Process handles trap based breakpoints, and
338    /// will disable the breakpoint, single step over it, then re-enable it.
339    ///
340    /// @param[in] abort_other_plans
341    ///    \b true if we discard the currently queued plans and replace them with this one.
342    ///    Otherwise this plan will go on the end of the plan stack.
343    ///
344    /// @return
345    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
346    //------------------------------------------------------------------
347    virtual ThreadPlan *
348    QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans);
349
350    //------------------------------------------------------------------
351    /// Queues the plan used to step one instruction from the current PC of \a thread.
352    ///
353    /// @param[in] step_over
354    ///    \b true if we step over calls to functions, false if we step in.
355    ///
356    /// @param[in] abort_other_plans
357    ///    \b true if we discard the currently queued plans and replace them with this one.
358    ///    Otherwise this plan will go on the end of the plan stack.
359    ///
360    /// @param[in] stop_other_threads
361    ///    \b true if we will stop other threads while we single step this one.
362    ///
363    /// @return
364    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
365    //------------------------------------------------------------------
366    virtual ThreadPlan *
367    QueueThreadPlanForStepSingleInstruction (bool step_over,
368                                             bool abort_other_plans,
369                                             bool stop_other_threads);
370
371    //------------------------------------------------------------------
372    /// Queues the plan used to step through an address range, stepping into or over
373    /// function calls depending on the value of StepType.
374    ///
375    /// @param[in] abort_other_plans
376    ///    \b true if we discard the currently queued plans and replace them with this one.
377    ///    Otherwise this plan will go on the end of the plan stack.
378    ///
379    /// @param[in] type
380    ///    Type of step to do, only eStepTypeInto and eStepTypeOver are supported by this plan.
381    ///
382    /// @param[in] range
383    ///    The address range to step through.
384    ///
385    /// @param[in] addr_context
386    ///    When dealing with stepping through inlined functions the current PC is not enough information to know
387    ///    what "step" means.  For instance a series of nested inline functions might start at the same address.
388    //     The \a addr_context provides the current symbol context the step
389    ///    is supposed to be out of.
390    //   FIXME: Currently unused.
391    ///
392    /// @param[in] stop_other_threads
393    ///    \b true if we will stop other threads while we single step this one.
394    ///
395    /// @return
396    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
397    //------------------------------------------------------------------
398    virtual ThreadPlan *
399    QueueThreadPlanForStepRange (bool abort_other_plans,
400                                 lldb::StepType type,
401                                 const AddressRange &range,
402                                 const SymbolContext &addr_context,
403                                 lldb::RunMode stop_other_threads,
404                                 bool avoid_code_without_debug_info);
405
406    //------------------------------------------------------------------
407    /// Queue the plan used to step out of the function at the current PC of
408    /// \a thread.
409    ///
410    /// @param[in] abort_other_plans
411    ///    \b true if we discard the currently queued plans and replace them with this one.
412    ///    Otherwise this plan will go on the end of the plan stack.
413    ///
414    /// @param[in] addr_context
415    ///    When dealing with stepping through inlined functions the current PC is not enough information to know
416    ///    what "step" means.  For instance a series of nested inline functions might start at the same address.
417    //     The \a addr_context provides the current symbol context the step
418    ///    is supposed to be out of.
419    //   FIXME: Currently unused.
420    ///
421    /// @param[in] first_insn
422    ///     \b true if this is the first instruction of a function.
423    ///
424    /// @param[in] stop_other_threads
425    ///    \b true if we will stop other threads while we single step this one.
426    ///
427    /// @param[in] stop_vote
428    /// @param[in] run_vote
429    ///    See standard meanings for the stop & run votes in ThreadPlan.h.
430    ///
431    /// @return
432    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
433    //------------------------------------------------------------------
434    virtual ThreadPlan *
435    QueueThreadPlanForStepOut (bool abort_other_plans,
436                               SymbolContext *addr_context,
437                               bool first_insn,
438                               bool stop_other_threads,
439                               lldb::Vote stop_vote = lldb::eVoteYes,
440                               lldb::Vote run_vote = lldb::eVoteNoOpinion);
441
442    //------------------------------------------------------------------
443    /// Gets the plan used to step through the code that steps from a function
444    /// call site at the current PC into the actual function call.
445    ///
446    /// @param[in] abort_other_plans
447    ///    \b true if we discard the currently queued plans and replace them with this one.
448    ///    Otherwise this plan will go on the end of the plan stack.
449    ///
450    /// @param[in] stop_other_threads
451    ///    \b true if we will stop other threads while we single step this one.
452    ///
453    /// @return
454    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
455    //------------------------------------------------------------------
456    virtual ThreadPlan *
457    QueueThreadPlanForStepThrough (bool abort_other_plans,
458                                   bool stop_other_threads);
459
460    //------------------------------------------------------------------
461    /// Gets the plan used to continue from the current PC.
462    /// This is a simple plan, mostly useful as a backstop when you are continuing
463    /// for some particular purpose.
464    ///
465    /// @param[in] abort_other_plans
466    ///    \b true if we discard the currently queued plans and replace them with this one.
467    ///    Otherwise this plan will go on the end of the plan stack.
468    ///
469    /// @param[in] target_addr
470    ///    The address to which we're running.
471    ///
472    /// @param[in] stop_other_threads
473    ///    \b true if we will stop other threads while we single step this one.
474    ///
475    /// @return
476    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
477    //------------------------------------------------------------------
478    virtual ThreadPlan *
479    QueueThreadPlanForRunToAddress (bool abort_other_plans,
480                                    Address &target_addr,
481                                    bool stop_other_threads);
482
483    virtual ThreadPlan *
484    QueueThreadPlanForStepUntil (bool abort_other_plans,
485                               lldb::addr_t *address_list,
486                               size_t num_addresses,
487                               bool stop_others);
488
489    virtual ThreadPlan *
490    QueueThreadPlanForCallFunction (bool abort_other_plans,
491                                    Address& function,
492                                    lldb::addr_t arg,
493                                    bool stop_other_threads,
494                                    bool discard_on_error = false);
495
496    //------------------------------------------------------------------
497    // Thread Plan accessors:
498    //------------------------------------------------------------------
499
500    //------------------------------------------------------------------
501    /// Gets the plan which will execute next on the plan stack.
502    ///
503    /// @return
504    ///     A pointer to the next executed plan.
505    //------------------------------------------------------------------
506    ThreadPlan *
507    GetCurrentPlan ();
508
509    //------------------------------------------------------------------
510    /// Gets the inner-most plan that was popped off the plan stack in the
511    /// most recent stop.  Useful for printing the stop reason accurately.
512    ///
513    /// @return
514    ///     A pointer to the last completed plan.
515    //------------------------------------------------------------------
516    lldb::ThreadPlanSP
517    GetCompletedPlan ();
518
519    //------------------------------------------------------------------
520    ///  Checks whether the given plan is in the completed plans for this
521    ///  stop.
522    ///
523    /// @param[in] plan
524    ///     Pointer to the plan you're checking.
525    ///
526    /// @return
527    ///     Returns true if the input plan is in the completed plan stack,
528    ///     false otherwise.
529    //------------------------------------------------------------------
530    bool
531    IsThreadPlanDone (ThreadPlan *plan);
532
533    //------------------------------------------------------------------
534    ///  Checks whether the given plan is in the discarded plans for this
535    ///  stop.
536    ///
537    /// @param[in] plan
538    ///     Pointer to the plan you're checking.
539    ///
540    /// @return
541    ///     Returns true if the input plan is in the discarded plan stack,
542    ///     false otherwise.
543    //------------------------------------------------------------------
544    bool
545    WasThreadPlanDiscarded (ThreadPlan *plan);
546
547    //------------------------------------------------------------------
548    /// Queues a generic thread plan.
549    ///
550    /// @param[in] plan_sp
551    ///    The plan to queue.
552    ///
553    /// @param[in] abort_other_plans
554    ///    \b true if we discard the currently queued plans and replace them with this one.
555    ///    Otherwise this plan will go on the end of the plan stack.
556    ///
557    /// @return
558    ///     A pointer to the last completed plan.
559    //------------------------------------------------------------------
560    void
561    QueueThreadPlan (lldb::ThreadPlanSP &plan_sp, bool abort_other_plans);
562
563
564    //------------------------------------------------------------------
565    /// Discards the plans queued on the plan stack of the current thread.  This is
566    /// arbitrated by the "Master" ThreadPlans, using the "OkayToDiscard" call.
567    //  But if \a force is true, all thread plans are discarded.
568    //------------------------------------------------------------------
569    void
570    DiscardThreadPlans (bool force);
571
572    //------------------------------------------------------------------
573    /// Discards the plans queued on the plan stack of the current thread up to and
574    /// including up_to_plan_sp.
575    //
576    // @param[in] up_to_plan_sp
577    //   Discard all plans up to and including this one.
578    //------------------------------------------------------------------
579    void
580    DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp);
581
582    //------------------------------------------------------------------
583    /// Prints the current plan stack.
584    ///
585    /// @param[in] s
586    ///    The stream to which to dump the plan stack info.
587    ///
588    //------------------------------------------------------------------
589    void
590    DumpThreadPlans (Stream *s) const;
591
592    void
593    EnableTracer (bool value, bool single_step);
594
595    void
596    SetTracer (lldb::ThreadPlanTracerSP &tracer_sp);
597
598    //------------------------------------------------------------------
599    /// The regular expression returned determines symbols that this
600    /// thread won't stop in during "step-in" operations.
601    ///
602    /// @return
603    ///    A pointer to a regular expression to compare against symbols,
604    ///    or NULL if all symbols are allowed.
605    ///
606    //------------------------------------------------------------------
607    RegularExpression *
608    GetSymbolsToAvoidRegexp()
609    {
610        return ThreadInstanceSettings::GetSymbolsToAvoidRegexp();
611    }
612
613    // Get the thread index ID. The index ID that is guaranteed to not be
614    // re-used by a process. They start at 1 and increase with each new thread.
615    // This allows easy command line access by a unique ID that is easier to
616    // type than the actual system thread ID.
617    uint32_t
618    GetIndexID () const;
619
620    //------------------------------------------------------------------
621    // lldb::ExecutionContextScope pure virtual functions
622    //------------------------------------------------------------------
623    virtual Target *
624    CalculateTarget ();
625
626    virtual Process *
627    CalculateProcess ();
628
629    virtual Thread *
630    CalculateThread ();
631
632    virtual StackFrame *
633    CalculateStackFrame ();
634
635    virtual void
636    CalculateExecutionContext (ExecutionContext &exe_ctx);
637
638    lldb::StackFrameSP
639    GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr);
640
641protected:
642
643    friend class ThreadPlan;
644    friend class StackFrameList;
645
646    // This is necessary to make sure thread assets get destroyed while the thread is still in good shape
647    // to call virtual thread methods.  This must be called by classes that derive from Thread in their destructor.
648    virtual void DestroyThread ();
649
650    void
651    PushPlan (lldb::ThreadPlanSP &plan_sp);
652
653    void
654    PopPlan ();
655
656    void
657    DiscardPlan ();
658
659    ThreadPlan *GetPreviousPlan (ThreadPlan *plan);
660
661    virtual lldb::StopInfoSP
662    GetPrivateStopReason () = 0;
663
664    typedef std::vector<lldb::ThreadPlanSP> plan_stack;
665
666    virtual lldb_private::Unwind *
667    GetUnwinder () = 0;
668
669    StackFrameList &
670    GetStackFrameList ();
671
672    void
673    SetStopInfo (lldb::StopInfoSP stop_info_sp)
674    {
675        m_actual_stop_info_sp = stop_info_sp;
676    }
677
678    //------------------------------------------------------------------
679    // Classes that inherit from Process can see and modify these
680    //------------------------------------------------------------------
681    Process &           m_process;          ///< The process that owns this thread.
682    lldb::StopInfoSP    m_actual_stop_info_sp;     ///< The private stop reason for this thread
683    const uint32_t      m_index_id;         ///< A unique 1 based index assigned to each thread for easy UI/command line access.
684    lldb::RegisterContextSP   m_reg_context_sp;   ///< The register context for this thread's current register state.
685    lldb::StateType     m_state;            ///< The state of our process.
686    mutable Mutex       m_state_mutex;      ///< Multithreaded protection for m_state.
687    plan_stack          m_plan_stack;       ///< The stack of plans this thread is executing.
688    plan_stack          m_completed_plan_stack;  ///< Plans that have been completed by this stop.  They get deleted when the thread resumes.
689    plan_stack          m_discarded_plan_stack;  ///< Plans that have been discarded by this stop.  They get deleted when the thread resumes.
690    std::auto_ptr<StackFrameList> m_curr_frames_ap; ///< The stack frames that get lazily populated after a thread stops.
691    lldb::StackFrameListSP m_prev_frames_sp; ///< The previous stack frames from the last time this thread stopped.
692    int                 m_resume_signal;    ///< The signal that should be used when continuing this thread.
693    lldb::StateType     m_resume_state;     ///< The state that indicates what this thread should do when the process is resumed.
694    std::auto_ptr<lldb_private::Unwind> m_unwinder_ap;
695    bool                m_destroy_called;    // This is used internally to make sure derived Thread classes call DestroyThread.
696private:
697    //------------------------------------------------------------------
698    // For Thread only
699    //------------------------------------------------------------------
700    DISALLOW_COPY_AND_ASSIGN (Thread);
701
702};
703
704} // namespace lldb_private
705
706#endif  // liblldb_Thread_h_
707