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