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