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