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