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