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