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