Thread.h revision 33ed170599d41fe407a4dcf5f0875c75e1ad1375
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    }
334
335    void
336    DumpInfo (Stream &strm,
337              bool show_stop_reason,
338              bool show_name,
339              bool show_queue,
340              uint32_t frame_idx);// = UINT32_MAX);
341
342    //------------------------------------------------------------------
343    // Thread Plan Providers:
344    // This section provides the basic thread plans that the Process control
345    // machinery uses to run the target.  ThreadPlan.h provides more details on
346    // how this mechanism works.
347    // The thread provides accessors to a set of plans that perform basic operations.
348    // The idea is that particular Platform plugins can override these methods to
349    // provide the implementation of these basic operations appropriate to their
350    // environment.
351    //------------------------------------------------------------------
352
353    //------------------------------------------------------------------
354    /// Queues the base plan for a thread.
355    /// The version returned by Process does some things that are useful,
356    /// like handle breakpoints and signals, so if you return a plugin specific
357    /// one you probably want to call through to the Process one for anything
358    /// your plugin doesn't explicitly handle.
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    QueueFundamentalPlan (bool abort_other_plans);
369
370    //------------------------------------------------------------------
371    /// Queues the plan used to step over a breakpoint at the current PC of \a thread.
372    /// The default version returned by Process handles trap based breakpoints, and
373    /// will disable the breakpoint, single step over it, then re-enable it.
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    /// @return
380    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
381    //------------------------------------------------------------------
382    virtual ThreadPlan *
383    QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans);
384
385    //------------------------------------------------------------------
386    /// Queues the plan used to step one instruction from the current PC of \a thread.
387    ///
388    /// @param[in] step_over
389    ///    \b true if we step over calls to functions, false if we step in.
390    ///
391    /// @param[in] abort_other_plans
392    ///    \b true if we discard the currently queued plans and replace them with this one.
393    ///    Otherwise this plan will go on the end of the plan stack.
394    ///
395    /// @param[in] stop_other_threads
396    ///    \b true if we will stop other threads while we single step this one.
397    ///
398    /// @return
399    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
400    //------------------------------------------------------------------
401    virtual ThreadPlan *
402    QueueThreadPlanForStepSingleInstruction (bool step_over,
403                                             bool abort_other_plans,
404                                             bool stop_other_threads);
405
406    //------------------------------------------------------------------
407    /// Queues the plan used to step through an address range, stepping into or over
408    /// function calls depending on the value of StepType.
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] type
415    ///    Type of step to do, only eStepTypeInto and eStepTypeOver are supported by this plan.
416    ///
417    /// @param[in] range
418    ///    The address range to step through.
419    ///
420    /// @param[in] addr_context
421    ///    When dealing with stepping through inlined functions the current PC is not enough information to know
422    ///    what "step" means.  For instance a series of nested inline functions might start at the same address.
423    //     The \a addr_context provides the current symbol context the step
424    ///    is supposed to be out of.
425    //   FIXME: Currently unused.
426    ///
427    /// @param[in] stop_other_threads
428    ///    \b true if we will stop other threads while we single step this one.
429    ///
430    /// @return
431    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
432    //------------------------------------------------------------------
433    virtual ThreadPlan *
434    QueueThreadPlanForStepRange (bool abort_other_plans,
435                                 lldb::StepType type,
436                                 const AddressRange &range,
437                                 const SymbolContext &addr_context,
438                                 lldb::RunMode stop_other_threads,
439                                 bool avoid_code_without_debug_info);
440
441    //------------------------------------------------------------------
442    /// Queue the plan used to step out of the function at the current PC of
443    /// \a thread.
444    ///
445    /// @param[in] abort_other_plans
446    ///    \b true if we discard the currently queued plans and replace them with this one.
447    ///    Otherwise this plan will go on the end of the plan stack.
448    ///
449    /// @param[in] addr_context
450    ///    When dealing with stepping through inlined functions the current PC is not enough information to know
451    ///    what "step" means.  For instance a series of nested inline functions might start at the same address.
452    //     The \a addr_context provides the current symbol context the step
453    ///    is supposed to be out of.
454    //   FIXME: Currently unused.
455    ///
456    /// @param[in] first_insn
457    ///     \b true if this is the first instruction of a function.
458    ///
459    /// @param[in] stop_other_threads
460    ///    \b true if we will stop other threads while we single step this one.
461    ///
462    /// @param[in] stop_vote
463    /// @param[in] run_vote
464    ///    See standard meanings for the stop & run votes in ThreadPlan.h.
465    ///
466    /// @return
467    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
468    //------------------------------------------------------------------
469    virtual ThreadPlan *
470    QueueThreadPlanForStepOut (bool abort_other_plans,
471                               SymbolContext *addr_context,
472                               bool first_insn,
473                               bool stop_other_threads,
474                               lldb::Vote stop_vote = lldb::eVoteYes,
475                               lldb::Vote run_vote = lldb::eVoteNoOpinion);
476
477    //------------------------------------------------------------------
478    /// Gets the plan used to step through the code that steps from a function
479    /// call site at the current PC into the actual function call.
480    ///
481    /// @param[in] abort_other_plans
482    ///    \b true if we discard the currently queued plans and replace them with this one.
483    ///    Otherwise this plan will go on the end of the plan stack.
484    ///
485    /// @param[in] stop_other_threads
486    ///    \b true if we will stop other threads while we single step this one.
487    ///
488    /// @return
489    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
490    //------------------------------------------------------------------
491    virtual ThreadPlan *
492    QueueThreadPlanForStepThrough (bool abort_other_plans,
493                                   bool stop_other_threads);
494
495    //------------------------------------------------------------------
496    /// Gets the plan used to continue from the current PC.
497    /// This is a simple plan, mostly useful as a backstop when you are continuing
498    /// for some particular purpose.
499    ///
500    /// @param[in] abort_other_plans
501    ///    \b true if we discard the currently queued plans and replace them with this one.
502    ///    Otherwise this plan will go on the end of the plan stack.
503    ///
504    /// @param[in] target_addr
505    ///    The address to which we're running.
506    ///
507    /// @param[in] stop_other_threads
508    ///    \b true if we will stop other threads while we single step this one.
509    ///
510    /// @return
511    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
512    //------------------------------------------------------------------
513    virtual ThreadPlan *
514    QueueThreadPlanForRunToAddress (bool abort_other_plans,
515                                    Address &target_addr,
516                                    bool stop_other_threads);
517
518    virtual ThreadPlan *
519    QueueThreadPlanForStepUntil (bool abort_other_plans,
520                               lldb::addr_t *address_list,
521                               size_t num_addresses,
522                               bool stop_others);
523
524    virtual ThreadPlan *
525    QueueThreadPlanForCallFunction (bool abort_other_plans,
526                                    Address& function,
527                                    lldb::addr_t arg,
528                                    bool stop_other_threads,
529                                    bool discard_on_error = false);
530
531    virtual ThreadPlan *
532    QueueThreadPlanForCallFunction (bool abort_other_plans,
533                                    Address& function,
534                                    ValueList &args,
535                                    bool stop_other_threads,
536                                    bool discard_on_error = false);
537
538    //------------------------------------------------------------------
539    // Thread Plan accessors:
540    //------------------------------------------------------------------
541
542    //------------------------------------------------------------------
543    /// Gets the plan which will execute next on the plan stack.
544    ///
545    /// @return
546    ///     A pointer to the next executed plan.
547    //------------------------------------------------------------------
548    ThreadPlan *
549    GetCurrentPlan ();
550
551    //------------------------------------------------------------------
552    /// Gets the inner-most plan that was popped off the plan stack in the
553    /// most recent stop.  Useful for printing the stop reason accurately.
554    ///
555    /// @return
556    ///     A pointer to the last completed plan.
557    //------------------------------------------------------------------
558    lldb::ThreadPlanSP
559    GetCompletedPlan ();
560
561    //------------------------------------------------------------------
562    ///  Checks whether the given plan is in the completed plans for this
563    ///  stop.
564    ///
565    /// @param[in] plan
566    ///     Pointer to the plan you're checking.
567    ///
568    /// @return
569    ///     Returns true if the input plan is in the completed plan stack,
570    ///     false otherwise.
571    //------------------------------------------------------------------
572    bool
573    IsThreadPlanDone (ThreadPlan *plan);
574
575    //------------------------------------------------------------------
576    ///  Checks whether the given plan is in the discarded plans for this
577    ///  stop.
578    ///
579    /// @param[in] plan
580    ///     Pointer to the plan you're checking.
581    ///
582    /// @return
583    ///     Returns true if the input plan is in the discarded plan stack,
584    ///     false otherwise.
585    //------------------------------------------------------------------
586    bool
587    WasThreadPlanDiscarded (ThreadPlan *plan);
588
589    //------------------------------------------------------------------
590    /// Queues a generic thread plan.
591    ///
592    /// @param[in] plan_sp
593    ///    The plan to queue.
594    ///
595    /// @param[in] abort_other_plans
596    ///    \b true if we discard the currently queued plans and replace them with this one.
597    ///    Otherwise this plan will go on the end of the plan stack.
598    ///
599    /// @return
600    ///     A pointer to the last completed plan.
601    //------------------------------------------------------------------
602    void
603    QueueThreadPlan (lldb::ThreadPlanSP &plan_sp, bool abort_other_plans);
604
605
606    //------------------------------------------------------------------
607    /// Discards the plans queued on the plan stack of the current thread.  This is
608    /// arbitrated by the "Master" ThreadPlans, using the "OkayToDiscard" call.
609    //  But if \a force is true, all thread plans are discarded.
610    //------------------------------------------------------------------
611    void
612    DiscardThreadPlans (bool force);
613
614    //------------------------------------------------------------------
615    /// Prints the current plan stack.
616    ///
617    /// @param[in] s
618    ///    The stream to which to dump the plan stack info.
619    ///
620    //------------------------------------------------------------------
621    void
622    DumpThreadPlans (Stream *s) const;
623
624    // Get the thread index ID. The index ID that is guaranteed to not be
625    // re-used by a process. They start at 1 and increase with each new thread.
626    // This allows easy command line access by a unique ID that is easier to
627    // type than the actual system thread ID.
628    uint32_t
629    GetIndexID () const;
630
631    //------------------------------------------------------------------
632    // lldb::ExecutionContextScope pure virtual functions
633    //------------------------------------------------------------------
634    virtual Target *
635    CalculateTarget ();
636
637    virtual Process *
638    CalculateProcess ();
639
640    virtual Thread *
641    CalculateThread ();
642
643    virtual StackFrame *
644    CalculateStackFrame ();
645
646    virtual void
647    Calculate (ExecutionContext &exe_ctx);
648
649protected:
650    void
651    PushPlan (lldb::ThreadPlanSP &plan_sp);
652
653    void
654    PopPlan ();
655
656    void
657    DiscardPlan ();
658
659    ThreadPlan *GetPreviousPlan (ThreadPlan *plan);
660
661    virtual lldb::StopInfoSP
662    GetPrivateStopReason () = 0;
663
664    typedef std::vector<lldb::ThreadPlanSP> plan_stack;
665
666    virtual lldb_private::Unwind *
667    GetUnwinder () = 0;
668
669    typedef struct InlinedFrameInfo
670    {
671        uint32_t concrete_frame_index;
672        uint32_t inline_height;
673        Block *block;
674    } InlinedFrameInfo;
675    typedef std::vector<InlinedFrameInfo> InlinedFrameInfoCollection;
676    //------------------------------------------------------------------
677    // Classes that inherit from Process can see and modify these
678    //------------------------------------------------------------------
679    Process &           m_process;          ///< The process that owns this thread.
680    lldb::StopInfoSP    m_public_stop_info_sp;     ///< The public stop reason for this thread
681    lldb::StopInfoSP    m_actual_stop_info_sp;     ///< The private stop reason for this thread
682    const uint32_t      m_index_id;         ///< A unique 1 based index assigned to each thread for easy UI/command line access.
683    lldb::RegisterContextSP   m_reg_context_sp;   ///< The register context for this thread's current register state.
684    lldb::StateType     m_state;            ///< The state of our process.
685    plan_stack          m_plan_stack;       ///< The stack of plans this thread is executing.
686    plan_stack          m_immediate_plan_stack; ///< The plans that need to get executed before any other work gets done.
687    plan_stack          m_completed_plan_stack;  ///< Plans that have been completed by this stop.  They get deleted when the thread resumes.
688    plan_stack          m_discarded_plan_stack;  ///< Plans that have been discarded by this stop.  They get deleted when the thread resumes.
689    mutable Mutex       m_state_mutex;      ///< Multithreaded protection for m_state.
690    StackFrameList      m_concrete_frames;  ///< The stack frames that get lazily populated after a thread stops.
691    StackFrameList      m_inlined_frames;   ///< The stack frames that get lazily populated after a thread stops.
692    InlinedFrameInfoCollection m_inlined_frame_info;
693    bool                m_show_inlined_frames;
694    int                 m_resume_signal;    ///< The signal that should be used when continuing this thread.
695    lldb::StateType     m_resume_state;     ///< The state that indicates what this thread should do when the process is resumed.
696    std::auto_ptr<lldb_private::Unwind> m_unwinder_ap;
697private:
698    //------------------------------------------------------------------
699    // For Thread only
700    //------------------------------------------------------------------
701    DISALLOW_COPY_AND_ASSIGN (Thread);
702
703};
704
705} // namespace lldb_private
706
707#endif  // liblldb_Thread_h_
708