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