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