Thread.h revision 5042acfb254338f60ef8bc3a8715e229795d941f
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/Broadcaster.h"
16#include "lldb/Core/Event.h"
17#include "lldb/Core/UserID.h"
18#include "lldb/Core/UserSettingsController.h"
19#include "lldb/Target/ExecutionContextScope.h"
20#include "lldb/Target/StackFrameList.h"
21
22#define LLDB_THREAD_MAX_STOP_EXC_DATA 8
23
24namespace lldb_private {
25
26class ThreadProperties : public Properties
27{
28public:
29    ThreadProperties(bool is_global);
30
31    virtual
32    ~ThreadProperties();
33
34    //------------------------------------------------------------------
35    /// The regular expression returned determines symbols that this
36    /// thread won't stop in during "step-in" operations.
37    ///
38    /// @return
39    ///    A pointer to a regular expression to compare against symbols,
40    ///    or NULL if all symbols are allowed.
41    ///
42    //------------------------------------------------------------------
43    const RegularExpression *
44    GetSymbolsToAvoidRegexp();
45
46    bool
47    GetTraceEnabledState() const;
48};
49
50typedef std::shared_ptr<ThreadProperties> ThreadPropertiesSP;
51
52class Thread :
53    public std::enable_shared_from_this<Thread>,
54    public ThreadProperties,
55    public UserID,
56    public ExecutionContextScope,
57    public Broadcaster
58{
59public:
60    //------------------------------------------------------------------
61    /// Broadcaster event bits definitions.
62    //------------------------------------------------------------------
63    enum
64    {
65        eBroadcastBitStackChanged           = (1 << 0),
66        eBroadcastBitThreadSuspended        = (1 << 1),
67        eBroadcastBitThreadResumed          = (1 << 2),
68        eBroadcastBitSelectedFrameChanged   = (1 << 3),
69        eBroadcastBitThreadSelected         = (1 << 4)
70    };
71
72    static ConstString &GetStaticBroadcasterClass ();
73
74    virtual ConstString &GetBroadcasterClass() const
75    {
76        return GetStaticBroadcasterClass();
77    }
78
79    class ThreadEventData :
80        public EventData
81    {
82    public:
83        ThreadEventData (const lldb::ThreadSP thread_sp);
84
85        ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id);
86
87        ThreadEventData();
88
89        virtual ~ThreadEventData();
90
91        static const ConstString &
92        GetFlavorString ();
93
94        virtual const ConstString &
95        GetFlavor () const
96        {
97            return ThreadEventData::GetFlavorString ();
98        }
99
100        virtual void
101        Dump (Stream *s) const;
102
103        static const ThreadEventData *
104        GetEventDataFromEvent (const Event *event_ptr);
105
106        static lldb::ThreadSP
107        GetThreadFromEvent (const Event *event_ptr);
108
109        static StackID
110        GetStackIDFromEvent (const Event *event_ptr);
111
112        static lldb::StackFrameSP
113        GetStackFrameFromEvent (const Event *event_ptr);
114
115        lldb::ThreadSP
116        GetThread () const
117        {
118            return m_thread_sp;
119        }
120
121        StackID
122        GetStackID () const
123        {
124            return m_stack_id;
125        }
126
127    private:
128        lldb::ThreadSP m_thread_sp;
129        StackID        m_stack_id;
130    DISALLOW_COPY_AND_ASSIGN (ThreadEventData);
131    };
132
133    // TODO: You shouldn't just checkpoint the register state alone, so this should get
134    // moved to protected.  To do that ThreadStateCheckpoint needs to be returned as a token...
135    class RegisterCheckpoint
136    {
137    public:
138
139        RegisterCheckpoint() :
140            m_stack_id (),
141            m_data_sp ()
142        {
143        }
144
145        RegisterCheckpoint (const StackID &stack_id) :
146            m_stack_id (stack_id),
147            m_data_sp ()
148        {
149        }
150
151        ~RegisterCheckpoint()
152        {
153        }
154
155        const RegisterCheckpoint&
156        operator= (const RegisterCheckpoint &rhs)
157        {
158            if (this != &rhs)
159            {
160                this->m_stack_id = rhs.m_stack_id;
161                this->m_data_sp  = rhs.m_data_sp;
162            }
163            return *this;
164        }
165
166        RegisterCheckpoint (const RegisterCheckpoint &rhs) :
167            m_stack_id (rhs.m_stack_id),
168            m_data_sp (rhs.m_data_sp)
169        {
170        }
171
172        const StackID &
173        GetStackID()
174        {
175            return m_stack_id;
176        }
177
178        void
179        SetStackID (const StackID &stack_id)
180        {
181            m_stack_id = stack_id;
182        }
183
184        lldb::DataBufferSP &
185        GetData()
186        {
187            return m_data_sp;
188        }
189
190        const lldb::DataBufferSP &
191        GetData() const
192        {
193            return m_data_sp;
194        }
195
196    protected:
197        StackID m_stack_id;
198        lldb::DataBufferSP m_data_sp;
199    };
200
201    struct ThreadStateCheckpoint
202    {
203        uint32_t           orig_stop_id;  // Dunno if I need this yet but it is an interesting bit of data.
204        lldb::StopInfoSP   stop_info_sp;  // You have to restore the stop info or you might continue with the wrong signals.
205        RegisterCheckpoint register_backup;  // You need to restore the registers, of course...
206        uint32_t           current_inlined_depth;
207        lldb::addr_t       current_inlined_pc;
208    };
209
210    static void
211    SettingsInitialize ();
212
213    static void
214    SettingsTerminate ();
215
216    static const ThreadPropertiesSP &
217    GetGlobalProperties();
218
219    Thread (Process &process, lldb::tid_t tid);
220    virtual ~Thread();
221
222    lldb::ProcessSP
223    GetProcess() const
224    {
225        return m_process_wp.lock();
226    }
227
228    int
229    GetResumeSignal () const
230    {
231        return m_resume_signal;
232    }
233
234    void
235    SetResumeSignal (int signal)
236    {
237        m_resume_signal = signal;
238    }
239
240    lldb::StateType
241    GetState() const;
242
243    void
244    SetState (lldb::StateType state);
245
246    lldb::StateType
247    GetResumeState () const
248    {
249        return m_resume_state;
250    }
251
252    void
253    SetResumeState (lldb::StateType state)
254    {
255        m_resume_state = state;
256    }
257
258    // This function is called on all the threads before "WillResume" in case
259    // a thread needs to change its state before the ThreadList polls all the
260    // threads to figure out which ones actually will get to run and how.
261    void
262    SetupForResume ();
263
264    // Override this to do platform specific tasks before resume, but always
265    // call the Thread::WillResume at the end of your work.
266
267    virtual bool
268    WillResume (lldb::StateType resume_state);
269
270    // This clears generic thread state after a resume.  If you subclass this,
271    // be sure to call it.
272    virtual void
273    DidResume ();
274
275    virtual void
276    RefreshStateAfterStop() = 0;
277
278    void
279    WillStop ();
280
281    bool
282    ShouldStop (Event *event_ptr);
283
284    Vote
285    ShouldReportStop (Event *event_ptr);
286
287    Vote
288    ShouldReportRun (Event *event_ptr);
289
290    void
291    Flush ();
292
293    // Return whether this thread matches the specification in ThreadSpec.  This is a virtual
294    // method because at some point we may extend the thread spec with a platform specific
295    // dictionary of attributes, which then only the platform specific Thread implementation
296    // would know how to match.  For now, this just calls through to the ThreadSpec's
297    // ThreadPassesBasicTests method.
298    virtual bool
299    MatchesSpec (const ThreadSpec *spec);
300
301    lldb::StopInfoSP
302    GetStopInfo ();
303
304    lldb::StopReason
305    GetStopReason();
306
307    // This sets the stop reason to a "blank" stop reason, so you can call functions on the thread
308    // without having the called function run with whatever stop reason you stopped with.
309    void
310    SetStopInfoToNothing();
311
312    bool
313    ThreadStoppedForAReason ();
314
315    static const char *
316    RunModeAsCString (lldb::RunMode mode);
317
318    static const char *
319    StopReasonAsCString (lldb::StopReason reason);
320
321    virtual const char *
322    GetInfo ()
323    {
324        return NULL;
325    }
326
327    virtual const char *
328    GetName ()
329    {
330        return NULL;
331    }
332
333    virtual const char *
334    GetQueueName ()
335    {
336        return NULL;
337    }
338
339    virtual uint32_t
340    GetStackFrameCount()
341    {
342        return GetStackFrameList()->GetNumFrames();
343    }
344
345    virtual lldb::StackFrameSP
346    GetStackFrameAtIndex (uint32_t idx)
347    {
348        return GetStackFrameList()->GetFrameAtIndex(idx);
349    }
350
351    virtual lldb::StackFrameSP
352    GetFrameWithConcreteFrameIndex (uint32_t unwind_idx);
353
354    bool
355    DecrementCurrentInlinedDepth()
356    {
357        return GetStackFrameList()->DecrementCurrentInlinedDepth();
358    }
359
360    uint32_t
361    GetCurrentInlinedDepth()
362    {
363        return GetStackFrameList()->GetCurrentInlinedDepth();
364    }
365
366    Error
367    ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast = false);
368
369    Error
370    ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast = false);
371
372    virtual lldb::StackFrameSP
373    GetFrameWithStackID (const StackID &stack_id)
374    {
375        return GetStackFrameList()->GetFrameWithStackID (stack_id);
376    }
377
378    uint32_t
379    GetSelectedFrameIndex ()
380    {
381        return GetStackFrameList()->GetSelectedFrameIndex();
382    }
383
384    lldb::StackFrameSP
385    GetSelectedFrame ()
386    {
387        lldb::StackFrameListSP stack_frame_list_sp(GetStackFrameList());
388        return stack_frame_list_sp->GetFrameAtIndex (stack_frame_list_sp->GetSelectedFrameIndex());
389    }
390
391    uint32_t
392    SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast = false);
393
394
395    bool
396    SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast = false);
397
398    bool
399    SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream);
400
401    void
402    SetDefaultFileAndLineToSelectedFrame()
403    {
404        GetStackFrameList()->SetDefaultFileAndLineToSelectedFrame();
405    }
406
407    virtual lldb::RegisterContextSP
408    GetRegisterContext () = 0;
409
410    virtual lldb::RegisterContextSP
411    CreateRegisterContextForFrame (StackFrame *frame) = 0;
412
413    virtual void
414    ClearStackFrames ();
415
416    virtual bool
417    SetBackingThread (const lldb::ThreadSP &thread_sp)
418    {
419        return false;
420    }
421
422    virtual lldb::ThreadSP
423    GetBackingThread () const
424    {
425        return lldb::ThreadSP();
426    }
427
428    virtual void
429    ClearBackingThread ()
430    {
431        // Subclasses can use this function if a thread is actually backed by
432        // another thread. This is currently used for the OperatingSystem plug-ins
433        // where they might have a thread that is in memory, yet its registers
434        // are available through the lldb_private::Thread subclass for the current
435        // lldb_private::Process class. Since each time the process stops the backing
436        // threads for memory threads can change, we need a way to clear the backing
437        // thread for all memory threads each time we stop.
438    }
439
440    void
441    DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx);
442
443    //------------------------------------------------------------------
444    // Thread Plan Providers:
445    // This section provides the basic thread plans that the Process control
446    // machinery uses to run the target.  ThreadPlan.h provides more details on
447    // how this mechanism works.
448    // The thread provides accessors to a set of plans that perform basic operations.
449    // The idea is that particular Platform plugins can override these methods to
450    // provide the implementation of these basic operations appropriate to their
451    // environment.
452    //------------------------------------------------------------------
453
454    //------------------------------------------------------------------
455    /// Queues the base plan for a thread.
456    /// The version returned by Process does some things that are useful,
457    /// like handle breakpoints and signals, so if you return a plugin specific
458    /// one you probably want to call through to the Process one for anything
459    /// your plugin doesn't explicitly handle.
460    ///
461    /// @param[in] abort_other_plans
462    ///    \b true if we discard the currently queued plans and replace them with this one.
463    ///    Otherwise this plan will go on the end of the plan stack.
464    ///
465    /// @return
466    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
467    //------------------------------------------------------------------
468    virtual ThreadPlan *
469    QueueFundamentalPlan (bool abort_other_plans);
470
471    //------------------------------------------------------------------
472    /// Queues the plan used to step over a breakpoint at the current PC of \a thread.
473    /// The default version returned by Process handles trap based breakpoints, and
474    /// will disable the breakpoint, single step over it, then re-enable it.
475    ///
476    /// @param[in] abort_other_plans
477    ///    \b true if we discard the currently queued plans and replace them with this one.
478    ///    Otherwise this plan will go on the end of the plan stack.
479    ///
480    /// @return
481    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
482    //------------------------------------------------------------------
483    virtual ThreadPlan *
484    QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans);
485
486    //------------------------------------------------------------------
487    /// Queues the plan used to step one instruction from the current PC of \a thread.
488    ///
489    /// @param[in] step_over
490    ///    \b true if we step over calls to functions, false if we step in.
491    ///
492    /// @param[in] abort_other_plans
493    ///    \b true if we discard the currently queued plans and replace them with this one.
494    ///    Otherwise this plan will go on the end of the plan stack.
495    ///
496    /// @param[in] stop_other_threads
497    ///    \b true if we will stop other threads while we single step this one.
498    ///
499    /// @return
500    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
501    //------------------------------------------------------------------
502    virtual ThreadPlan *
503    QueueThreadPlanForStepSingleInstruction (bool step_over,
504                                             bool abort_other_plans,
505                                             bool stop_other_threads);
506
507    //------------------------------------------------------------------
508    /// Queues the plan used to step through an address range, stepping  over
509    /// function calls.
510    ///
511    /// @param[in] abort_other_plans
512    ///    \b true if we discard the currently queued plans and replace them with this one.
513    ///    Otherwise this plan will go on the end of the plan stack.
514    ///
515    /// @param[in] type
516    ///    Type of step to do, only eStepTypeInto and eStepTypeOver are supported by this plan.
517    ///
518    /// @param[in] range
519    ///    The address range to step through.
520    ///
521    /// @param[in] addr_context
522    ///    When dealing with stepping through inlined functions the current PC is not enough information to know
523    ///    what "step" means.  For instance a series of nested inline functions might start at the same address.
524    //     The \a addr_context provides the current symbol context the step
525    ///    is supposed to be out of.
526    //   FIXME: Currently unused.
527    ///
528    /// @param[in] stop_other_threads
529    ///    \b true if we will stop other threads while we single step this one.
530    ///
531    /// @return
532    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
533    //------------------------------------------------------------------
534    virtual ThreadPlan *
535    QueueThreadPlanForStepOverRange (bool abort_other_plans,
536                                 const AddressRange &range,
537                                 const SymbolContext &addr_context,
538                                 lldb::RunMode stop_other_threads);
539
540    //------------------------------------------------------------------
541    /// Queues the plan used to step through an address range, stepping into functions.
542    ///
543    /// @param[in] abort_other_plans
544    ///    \b true if we discard the currently queued plans and replace them with this one.
545    ///    Otherwise this plan will go on the end of the plan stack.
546    ///
547    /// @param[in] type
548    ///    Type of step to do, only eStepTypeInto and eStepTypeOver are supported by this plan.
549    ///
550    /// @param[in] range
551    ///    The address range to step through.
552    ///
553    /// @param[in] addr_context
554    ///    When dealing with stepping through inlined functions the current PC is not enough information to know
555    ///    what "step" means.  For instance a series of nested inline functions might start at the same address.
556    //     The \a addr_context provides the current symbol context the step
557    ///    is supposed to be out of.
558    //   FIXME: Currently unused.
559    ///
560    /// @param[in] step_in_target
561    ///    Name if function we are trying to step into.  We will step out if we don't land in that function.
562    ///
563    /// @param[in] stop_other_threads
564    ///    \b true if we will stop other threads while we single step this one.
565    ///
566    /// @param[in] avoid_code_without_debug_info
567    ///    If \b true we will step out if we step into code with no debug info.
568    ///
569    /// @return
570    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
571    //------------------------------------------------------------------
572    virtual ThreadPlan *
573    QueueThreadPlanForStepInRange (bool abort_other_plans,
574                                 const AddressRange &range,
575                                 const SymbolContext &addr_context,
576                                 const char *step_in_target,
577                                 lldb::RunMode stop_other_threads,
578                                 bool avoid_code_without_debug_info);
579
580    //------------------------------------------------------------------
581    /// Queue the plan used to step out of the function at the current PC of
582    /// \a thread.
583    ///
584    /// @param[in] abort_other_plans
585    ///    \b true if we discard the currently queued plans and replace them with this one.
586    ///    Otherwise this plan will go on the end of the plan stack.
587    ///
588    /// @param[in] addr_context
589    ///    When dealing with stepping through inlined functions the current PC is not enough information to know
590    ///    what "step" means.  For instance a series of nested inline functions might start at the same address.
591    //     The \a addr_context provides the current symbol context the step
592    ///    is supposed to be out of.
593    //   FIXME: Currently unused.
594    ///
595    /// @param[in] first_insn
596    ///     \b true if this is the first instruction of a function.
597    ///
598    /// @param[in] stop_other_threads
599    ///    \b true if we will stop other threads while we single step this one.
600    ///
601    /// @param[in] stop_vote
602    /// @param[in] run_vote
603    ///    See standard meanings for the stop & run votes in ThreadPlan.h.
604    ///
605    /// @return
606    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
607    //------------------------------------------------------------------
608    virtual ThreadPlan *
609    QueueThreadPlanForStepOut (bool abort_other_plans,
610                               SymbolContext *addr_context,
611                               bool first_insn,
612                               bool stop_other_threads,
613                               Vote stop_vote, // = eVoteYes,
614                               Vote run_vote, // = eVoteNoOpinion);
615                               uint32_t frame_idx);
616
617    //------------------------------------------------------------------
618    /// Gets the plan used to step through the code that steps from a function
619    /// call site at the current PC into the actual function call.
620    ///
621    ///
622    /// @param[in] return_stack_id
623    ///    The stack id that we will return to (by setting backstop breakpoints on the return
624    ///    address to that frame) if we fail to step through.
625    ///
626    /// @param[in] abort_other_plans
627    ///    \b true if we discard the currently queued plans and replace them with this one.
628    ///    Otherwise this plan will go on the end of the plan stack.
629    ///
630    /// @param[in] stop_other_threads
631    ///    \b true if we will stop other threads while we single step this one.
632    ///
633    /// @return
634    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
635    //------------------------------------------------------------------
636    virtual ThreadPlan *
637    QueueThreadPlanForStepThrough (StackID &return_stack_id,
638                                   bool abort_other_plans,
639                                   bool stop_other_threads);
640
641    //------------------------------------------------------------------
642    /// Gets the plan used to continue from the current PC.
643    /// This is a simple plan, mostly useful as a backstop when you are continuing
644    /// for some particular purpose.
645    ///
646    /// @param[in] abort_other_plans
647    ///    \b true if we discard the currently queued plans and replace them with this one.
648    ///    Otherwise this plan will go on the end of the plan stack.
649    ///
650    /// @param[in] target_addr
651    ///    The address to which we're running.
652    ///
653    /// @param[in] stop_other_threads
654    ///    \b true if we will stop other threads while we single step this one.
655    ///
656    /// @return
657    ///     A pointer to the newly queued thread plan, or NULL if the plan could not be queued.
658    //------------------------------------------------------------------
659    virtual ThreadPlan *
660    QueueThreadPlanForRunToAddress (bool abort_other_plans,
661                                    Address &target_addr,
662                                    bool stop_other_threads);
663
664    virtual ThreadPlan *
665    QueueThreadPlanForStepUntil (bool abort_other_plans,
666                                 lldb::addr_t *address_list,
667                                 size_t num_addresses,
668                                 bool stop_others,
669                                 uint32_t frame_idx);
670
671    virtual ThreadPlan *
672    QueueThreadPlanForCallFunction (bool abort_other_plans,
673                                    Address& function,
674                                    lldb::addr_t arg,
675                                    bool stop_other_threads,
676                                    bool unwind_on_error = false,
677                                    bool ignore_breakpoints = true);
678
679    //------------------------------------------------------------------
680    // Thread Plan accessors:
681    //------------------------------------------------------------------
682
683    //------------------------------------------------------------------
684    /// Gets the plan which will execute next on the plan stack.
685    ///
686    /// @return
687    ///     A pointer to the next executed plan.
688    //------------------------------------------------------------------
689    ThreadPlan *
690    GetCurrentPlan ();
691
692    //------------------------------------------------------------------
693    /// Unwinds the thread stack for the innermost expression plan currently
694    /// on the thread plan stack.
695    ///
696    /// @return
697    ///     An error if the thread plan could not be unwound.
698    //------------------------------------------------------------------
699
700    Error
701    UnwindInnermostExpression();
702
703private:
704    bool
705    PlanIsBasePlan (ThreadPlan *plan_ptr);
706
707    void
708    BroadcastSelectedFrameChange(StackID &new_frame_id);
709
710public:
711
712    //------------------------------------------------------------------
713    /// Gets the outer-most plan that was popped off the plan stack in the
714    /// most recent stop.  Useful for printing the stop reason accurately.
715    ///
716    /// @return
717    ///     A pointer to the last completed plan.
718    //------------------------------------------------------------------
719    lldb::ThreadPlanSP
720    GetCompletedPlan ();
721
722    //------------------------------------------------------------------
723    /// Gets the outer-most return value from the completed plans
724    ///
725    /// @return
726    ///     A ValueObjectSP, either empty if there is no return value,
727    ///     or containing the return value.
728    //------------------------------------------------------------------
729    lldb::ValueObjectSP
730    GetReturnValueObject ();
731
732    //------------------------------------------------------------------
733    ///  Checks whether the given plan is in the completed plans for this
734    ///  stop.
735    ///
736    /// @param[in] plan
737    ///     Pointer to the plan you're checking.
738    ///
739    /// @return
740    ///     Returns true if the input plan is in the completed plan stack,
741    ///     false otherwise.
742    //------------------------------------------------------------------
743    bool
744    IsThreadPlanDone (ThreadPlan *plan);
745
746    //------------------------------------------------------------------
747    ///  Checks whether the given plan is in the discarded plans for this
748    ///  stop.
749    ///
750    /// @param[in] plan
751    ///     Pointer to the plan you're checking.
752    ///
753    /// @return
754    ///     Returns true if the input plan is in the discarded plan stack,
755    ///     false otherwise.
756    //------------------------------------------------------------------
757    bool
758    WasThreadPlanDiscarded (ThreadPlan *plan);
759
760    //------------------------------------------------------------------
761    /// Queues a generic thread plan.
762    ///
763    /// @param[in] plan_sp
764    ///    The plan to queue.
765    ///
766    /// @param[in] abort_other_plans
767    ///    \b true if we discard the currently queued plans and replace them with this one.
768    ///    Otherwise this plan will go on the end of the plan stack.
769    ///
770    /// @return
771    ///     A pointer to the last completed plan.
772    //------------------------------------------------------------------
773    void
774    QueueThreadPlan (lldb::ThreadPlanSP &plan_sp, bool abort_other_plans);
775
776
777    //------------------------------------------------------------------
778    /// Discards the plans queued on the plan stack of the current thread.  This is
779    /// arbitrated by the "Master" ThreadPlans, using the "OkayToDiscard" call.
780    //  But if \a force is true, all thread plans are discarded.
781    //------------------------------------------------------------------
782    void
783    DiscardThreadPlans (bool force);
784
785    //------------------------------------------------------------------
786    /// Discards the plans queued on the plan stack of the current thread up to and
787    /// including up_to_plan_sp.
788    //
789    // @param[in] up_to_plan_sp
790    //   Discard all plans up to and including this one.
791    //------------------------------------------------------------------
792    void
793    DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp);
794
795    void
796    DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr);
797
798    //------------------------------------------------------------------
799    /// Prints the current plan stack.
800    ///
801    /// @param[in] s
802    ///    The stream to which to dump the plan stack info.
803    ///
804    //------------------------------------------------------------------
805    void
806    DumpThreadPlans (Stream *s) const;
807
808    virtual bool
809    CheckpointThreadState (ThreadStateCheckpoint &saved_state);
810
811    virtual bool
812    RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state);
813
814    virtual bool
815    RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state);
816
817    void
818    EnableTracer (bool value, bool single_step);
819
820    void
821    SetTracer (lldb::ThreadPlanTracerSP &tracer_sp);
822
823    // Get the thread index ID. The index ID that is guaranteed to not be
824    // re-used by a process. They start at 1 and increase with each new thread.
825    // This allows easy command line access by a unique ID that is easier to
826    // type than the actual system thread ID.
827    uint32_t
828    GetIndexID () const;
829
830    //------------------------------------------------------------------
831    // lldb::ExecutionContextScope pure virtual functions
832    //------------------------------------------------------------------
833    virtual lldb::TargetSP
834    CalculateTarget ();
835
836    virtual lldb::ProcessSP
837    CalculateProcess ();
838
839    virtual lldb::ThreadSP
840    CalculateThread ();
841
842    virtual lldb::StackFrameSP
843    CalculateStackFrame ();
844
845    virtual void
846    CalculateExecutionContext (ExecutionContext &exe_ctx);
847
848    lldb::StackFrameSP
849    GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr);
850
851    size_t
852    GetStatus (Stream &strm,
853               uint32_t start_frame,
854               uint32_t num_frames,
855               uint32_t num_frames_with_source);
856
857    size_t
858    GetStackFrameStatus (Stream& strm,
859                         uint32_t first_frame,
860                         uint32_t num_frames,
861                         bool show_frame_info,
862                         uint32_t num_frames_with_source);
863
864    // We need a way to verify that even though we have a thread in a shared
865    // pointer that the object itself is still valid. Currently this won't be
866    // the case if DestroyThread() was called. DestroyThread is called when
867    // a thread has been removed from the Process' thread list.
868    bool
869    IsValid () const
870    {
871        return !m_destroy_called;
872    }
873
874    // When you implement this method, make sure you don't overwrite the m_actual_stop_info if it claims to be
875    // valid.  The stop info may be a "checkpointed and restored" stop info, so if it is still around it is right
876    // even if you have not calculated this yourself, or if it disagrees with what you might have calculated.
877    virtual lldb::StopInfoSP
878    GetPrivateStopReason () = 0;
879
880    //----------------------------------------------------------------------
881    // Gets the temporary resume state for a thread.
882    //
883    // This value gets set in each thread by complex debugger logic in
884    // Thread::WillResume() and an appropriate thread resume state will get
885    // set in each thread every time the process is resumed prior to calling
886    // Process::DoResume(). The lldb_private::Process subclass should adhere
887    // to the thread resume state request which will be one of:
888    //
889    //  eStateRunning   - thread will resume when process is resumed
890    //  eStateStepping  - thread should step 1 instruction and stop when process
891    //                    is resumed
892    //  eStateSuspended - thread should not execute any instructions when
893    //                    process is resumed
894    //----------------------------------------------------------------------
895    lldb::StateType
896    GetTemporaryResumeState() const
897    {
898        return m_temporary_resume_state;
899    }
900
901protected:
902
903    friend class ThreadPlan;
904    friend class ThreadList;
905    friend class ThreadEventData;
906    friend class StackFrameList;
907    friend class StackFrame;
908
909    // This is necessary to make sure thread assets get destroyed while the thread is still in good shape
910    // to call virtual thread methods.  This must be called by classes that derive from Thread in their destructor.
911    virtual void DestroyThread ();
912
913    void
914    PushPlan (lldb::ThreadPlanSP &plan_sp);
915
916    void
917    PopPlan ();
918
919    void
920    DiscardPlan ();
921
922    ThreadPlan *GetPreviousPlan (ThreadPlan *plan);
923
924    typedef std::vector<lldb::ThreadPlanSP> plan_stack;
925
926    void
927    SetStopInfo (const lldb::StopInfoSP &stop_info_sp);
928
929    virtual bool
930    SaveFrameZeroState (RegisterCheckpoint &checkpoint);
931
932    virtual bool
933    RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint);
934
935    // register_data_sp must be a DataSP passed to ReadAllRegisterValues.
936    bool
937    ResetFrameZeroRegisters (lldb::DataBufferSP register_data_sp);
938
939    virtual lldb_private::Unwind *
940    GetUnwinder ();
941
942    // Check to see whether the thread is still at the last breakpoint hit that stopped it.
943    virtual bool
944    IsStillAtLastBreakpointHit();
945
946    lldb::StackFrameListSP
947    GetStackFrameList ();
948
949    struct ThreadState
950    {
951        uint32_t           orig_stop_id;
952        lldb::StopInfoSP   stop_info_sp;
953        RegisterCheckpoint register_backup;
954    };
955
956    //------------------------------------------------------------------
957    // Classes that inherit from Process can see and modify these
958    //------------------------------------------------------------------
959    lldb::ProcessWP     m_process_wp;           ///< The process that owns this thread.
960    lldb::StopInfoSP    m_actual_stop_info_sp;  ///< The private stop reason for this thread
961    const uint32_t      m_index_id;             ///< A unique 1 based index assigned to each thread for easy UI/command line access.
962    lldb::RegisterContextSP m_reg_context_sp;   ///< The register context for this thread's current register state.
963    lldb::StateType     m_state;                ///< The state of our process.
964    mutable Mutex       m_state_mutex;          ///< Multithreaded protection for m_state.
965    plan_stack          m_plan_stack;           ///< The stack of plans this thread is executing.
966    plan_stack          m_completed_plan_stack; ///< Plans that have been completed by this stop.  They get deleted when the thread resumes.
967    plan_stack          m_discarded_plan_stack; ///< Plans that have been discarded by this stop.  They get deleted when the thread resumes.
968    mutable Mutex       m_frame_mutex;          ///< Multithreaded protection for m_state.
969    lldb::StackFrameListSP m_curr_frames_sp;    ///< The stack frames that get lazily populated after a thread stops.
970    lldb::StackFrameListSP m_prev_frames_sp;    ///< The previous stack frames from the last time this thread stopped.
971    int                 m_resume_signal;        ///< The signal that should be used when continuing this thread.
972    lldb::StateType     m_resume_state;         ///< This state is used to force a thread to be suspended from outside the ThreadPlan logic.
973    lldb::StateType     m_temporary_resume_state; ///< This state records what the thread was told to do by the thread plan logic for the current resume.
974                                                  /// It gets set in Thread::WillResume.
975    std::unique_ptr<lldb_private::Unwind> m_unwinder_ap;
976    bool                m_destroy_called;       // This is used internally to make sure derived Thread classes call DestroyThread.
977    uint32_t m_thread_stop_reason_stop_id;      // This is the stop id for which the StopInfo is valid.  Can use this so you know that
978                                                // the thread's m_actual_stop_info_sp is current and you don't have to fetch it again
979
980private:
981    //------------------------------------------------------------------
982    // For Thread only
983    //------------------------------------------------------------------
984
985    DISALLOW_COPY_AND_ASSIGN (Thread);
986
987};
988
989} // namespace lldb_private
990
991#endif  // liblldb_Thread_h_
992