• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 namespace lldb_private {
25 
26 class ThreadProperties : public Properties
27 {
28 public:
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 
50 typedef std::shared_ptr<ThreadProperties> ThreadPropertiesSP;
51 
52 class Thread :
53     public std::enable_shared_from_this<Thread>,
54     public ThreadProperties,
55     public UserID,
56     public ExecutionContextScope,
57     public Broadcaster
58 {
59 public:
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 
GetBroadcasterClass()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 &
GetFlavor()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
GetThread()116         GetThread () const
117         {
118             return m_thread_sp;
119         }
120 
121         StackID
GetStackID()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 
RegisterCheckpoint()139         RegisterCheckpoint() :
140             m_stack_id (),
141             m_data_sp ()
142         {
143         }
144 
RegisterCheckpoint(const StackID & stack_id)145         RegisterCheckpoint (const StackID &stack_id) :
146             m_stack_id (stack_id),
147             m_data_sp ()
148         {
149         }
150 
~RegisterCheckpoint()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 
RegisterCheckpoint(const RegisterCheckpoint & rhs)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 &
GetStackID()173         GetStackID()
174         {
175             return m_stack_id;
176         }
177 
178         void
SetStackID(const StackID & stack_id)179         SetStackID (const StackID &stack_id)
180         {
181             m_stack_id = stack_id;
182         }
183 
184         lldb::DataBufferSP &
GetData()185         GetData()
186         {
187             return m_data_sp;
188         }
189 
190         const lldb::DataBufferSP &
GetData()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
GetProcess()223     GetProcess() const
224     {
225         return m_process_wp.lock();
226     }
227 
228     int
GetResumeSignal()229     GetResumeSignal () const
230     {
231         return m_resume_signal;
232     }
233 
234     void
SetResumeSignal(int signal)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
GetResumeState()247     GetResumeState () const
248     {
249         return m_resume_state;
250     }
251 
252     void
SetResumeState(lldb::StateType state)253     SetResumeState (lldb::StateType state)
254     {
255         m_resume_state = state;
256     }
257 
258     // This function is called on all the threads before "ShouldResume" and
259     // "WillResume" in case a thread needs to change its state before the
260     // ThreadList polls all the threads to figure out which ones actually
261     // will get to run and how.
262     void
263     SetupForResume ();
264 
265     // Do not override this function, it is for thread plan logic only
266     bool
267     ShouldResume (lldb::StateType resume_state);
268 
269     // Override this to do platform specific tasks before resume.
270     virtual void
WillResume(lldb::StateType resume_state)271     WillResume (lldb::StateType resume_state)
272     {
273     }
274 
275     // This clears generic thread state after a resume.  If you subclass this,
276     // be sure to call it.
277     virtual void
278     DidResume ();
279 
280     // This notifies the thread when a private stop occurs.
281     virtual void
282     DidStop ();
283 
284     virtual void
285     RefreshStateAfterStop() = 0;
286 
287     void
288     WillStop ();
289 
290     bool
291     ShouldStop (Event *event_ptr);
292 
293     Vote
294     ShouldReportStop (Event *event_ptr);
295 
296     Vote
297     ShouldReportRun (Event *event_ptr);
298 
299     void
300     Flush ();
301 
302     // Return whether this thread matches the specification in ThreadSpec.  This is a virtual
303     // method because at some point we may extend the thread spec with a platform specific
304     // dictionary of attributes, which then only the platform specific Thread implementation
305     // would know how to match.  For now, this just calls through to the ThreadSpec's
306     // ThreadPassesBasicTests method.
307     virtual bool
308     MatchesSpec (const ThreadSpec *spec);
309 
310     lldb::StopInfoSP
311     GetStopInfo ();
312 
313     lldb::StopReason
314     GetStopReason();
315 
316     // This sets the stop reason to a "blank" stop reason, so you can call functions on the thread
317     // without having the called function run with whatever stop reason you stopped with.
318     void
319     SetStopInfoToNothing();
320 
321     bool
322     ThreadStoppedForAReason ();
323 
324     static const char *
325     RunModeAsCString (lldb::RunMode mode);
326 
327     static const char *
328     StopReasonAsCString (lldb::StopReason reason);
329 
330     virtual const char *
GetInfo()331     GetInfo ()
332     {
333         return NULL;
334     }
335 
336     virtual const char *
GetName()337     GetName ()
338     {
339         return NULL;
340     }
341 
342     virtual const char *
GetQueueName()343     GetQueueName ()
344     {
345         return NULL;
346     }
347 
348     virtual uint32_t
GetStackFrameCount()349     GetStackFrameCount()
350     {
351         return GetStackFrameList()->GetNumFrames();
352     }
353 
354     virtual lldb::StackFrameSP
GetStackFrameAtIndex(uint32_t idx)355     GetStackFrameAtIndex (uint32_t idx)
356     {
357         return GetStackFrameList()->GetFrameAtIndex(idx);
358     }
359 
360     virtual lldb::StackFrameSP
361     GetFrameWithConcreteFrameIndex (uint32_t unwind_idx);
362 
363     bool
DecrementCurrentInlinedDepth()364     DecrementCurrentInlinedDepth()
365     {
366         return GetStackFrameList()->DecrementCurrentInlinedDepth();
367     }
368 
369     uint32_t
GetCurrentInlinedDepth()370     GetCurrentInlinedDepth()
371     {
372         return GetStackFrameList()->GetCurrentInlinedDepth();
373     }
374 
375     Error
376     ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast = false);
377 
378     Error
379     ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast = false);
380 
381     virtual lldb::StackFrameSP
GetFrameWithStackID(const StackID & stack_id)382     GetFrameWithStackID (const StackID &stack_id)
383     {
384         if (stack_id.IsValid())
385             return GetStackFrameList()->GetFrameWithStackID (stack_id);
386         return lldb::StackFrameSP();
387     }
388 
389     uint32_t
GetSelectedFrameIndex()390     GetSelectedFrameIndex ()
391     {
392         return GetStackFrameList()->GetSelectedFrameIndex();
393     }
394 
395     lldb::StackFrameSP
GetSelectedFrame()396     GetSelectedFrame ()
397     {
398         lldb::StackFrameListSP stack_frame_list_sp(GetStackFrameList());
399         return stack_frame_list_sp->GetFrameAtIndex (stack_frame_list_sp->GetSelectedFrameIndex());
400     }
401 
402     uint32_t
403     SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast = false);
404 
405 
406     bool
407     SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast = false);
408 
409     bool
410     SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream);
411 
412     void
SetDefaultFileAndLineToSelectedFrame()413     SetDefaultFileAndLineToSelectedFrame()
414     {
415         GetStackFrameList()->SetDefaultFileAndLineToSelectedFrame();
416     }
417 
418     virtual lldb::RegisterContextSP
419     GetRegisterContext () = 0;
420 
421     virtual lldb::RegisterContextSP
422     CreateRegisterContextForFrame (StackFrame *frame) = 0;
423 
424     virtual void
425     ClearStackFrames ();
426 
427     virtual bool
SetBackingThread(const lldb::ThreadSP & thread_sp)428     SetBackingThread (const lldb::ThreadSP &thread_sp)
429     {
430         return false;
431     }
432 
433     virtual lldb::ThreadSP
GetBackingThread()434     GetBackingThread () const
435     {
436         return lldb::ThreadSP();
437     }
438 
439     virtual void
ClearBackingThread()440     ClearBackingThread ()
441     {
442         // Subclasses can use this function if a thread is actually backed by
443         // another thread. This is currently used for the OperatingSystem plug-ins
444         // where they might have a thread that is in memory, yet its registers
445         // are available through the lldb_private::Thread subclass for the current
446         // lldb_private::Process class. Since each time the process stops the backing
447         // threads for memory threads can change, we need a way to clear the backing
448         // thread for all memory threads each time we stop.
449     }
450 
451     void
452     DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx);
453 
454     //------------------------------------------------------------------
455     // Thread Plan Providers:
456     // This section provides the basic thread plans that the Process control
457     // machinery uses to run the target.  ThreadPlan.h provides more details on
458     // how this mechanism works.
459     // The thread provides accessors to a set of plans that perform basic operations.
460     // The idea is that particular Platform plugins can override these methods to
461     // provide the implementation of these basic operations appropriate to their
462     // environment.
463     //
464     // NB: All the QueueThreadPlanXXX providers return Shared Pointers to
465     // Thread plans.  This is useful so that you can modify the plans after
466     // creation in ways specific to that plan type.  Also, it is often necessary for
467     // ThreadPlans that utilize other ThreadPlans to implement their task to keep a shared
468     // pointer to the sub-plan.
469     // But besides that, the shared pointers should only be held onto by entities who live no longer
470     // than the thread containing the ThreadPlan.
471     // FIXME: If this becomes a problem, we can make a version that just returns a pointer,
472     // which it is clearly unsafe to hold onto, and a shared pointer version, and only allow
473     // ThreadPlan and Co. to use the latter.  That is made more annoying to do because there's
474     // no elegant way to friend a method to all sub-classes of a given class.
475     //
476     //------------------------------------------------------------------
477 
478     //------------------------------------------------------------------
479     /// Queues the base plan for a thread.
480     /// The version returned by Process does some things that are useful,
481     /// like handle breakpoints and signals, so if you return a plugin specific
482     /// one you probably want to call through to the Process one for anything
483     /// your plugin doesn't explicitly handle.
484     ///
485     /// @param[in] abort_other_plans
486     ///    \b true if we discard the currently queued plans and replace them with this one.
487     ///    Otherwise this plan will go on the end of the plan stack.
488     ///
489     /// @return
490     ///     A shared pointer to the newly queued thread plan, or NULL if the plan could not be queued.
491     //------------------------------------------------------------------
492     virtual lldb::ThreadPlanSP
493     QueueFundamentalPlan (bool abort_other_plans);
494 
495     //------------------------------------------------------------------
496     /// Queues the plan used to step over a breakpoint at the current PC of \a thread.
497     /// The default version returned by Process handles trap based breakpoints, and
498     /// will disable the breakpoint, single step over it, then re-enable it.
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     /// @return
505     ///     A shared pointer to the newly queued thread plan, or NULL if the plan could not be queued.
506     //------------------------------------------------------------------
507     virtual lldb::ThreadPlanSP
508     QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans);
509 
510     //------------------------------------------------------------------
511     /// Queues the plan used to step one instruction from the current PC of \a thread.
512     ///
513     /// @param[in] step_over
514     ///    \b true if we step over calls to functions, false if we step in.
515     ///
516     /// @param[in] abort_other_plans
517     ///    \b true if we discard the currently queued plans and replace them with this one.
518     ///    Otherwise this plan will go on the end of the plan stack.
519     ///
520     /// @param[in] stop_other_threads
521     ///    \b true if we will stop other threads while we single step this one.
522     ///
523     /// @return
524     ///     A shared pointer to the newly queued thread plan, or NULL if the plan could not be queued.
525     //------------------------------------------------------------------
526     virtual lldb::ThreadPlanSP
527     QueueThreadPlanForStepSingleInstruction (bool step_over,
528                                              bool abort_other_plans,
529                                              bool stop_other_threads);
530 
531     //------------------------------------------------------------------
532     /// Queues the plan used to step through an address range, stepping  over
533     /// function calls.
534     ///
535     /// @param[in] abort_other_plans
536     ///    \b true if we discard the currently queued plans and replace them with this one.
537     ///    Otherwise this plan will go on the end of the plan stack.
538     ///
539     /// @param[in] type
540     ///    Type of step to do, only eStepTypeInto and eStepTypeOver are supported by this plan.
541     ///
542     /// @param[in] range
543     ///    The address range to step through.
544     ///
545     /// @param[in] addr_context
546     ///    When dealing with stepping through inlined functions the current PC is not enough information to know
547     ///    what "step" means.  For instance a series of nested inline functions might start at the same address.
548     //     The \a addr_context provides the current symbol context the step
549     ///    is supposed to be out of.
550     //   FIXME: Currently unused.
551     ///
552     /// @param[in] stop_other_threads
553     ///    \b true if we will stop other threads while we single step this one.
554     ///
555     /// @return
556     ///     A shared pointer to the newly queued thread plan, or NULL if the plan could not be queued.
557     //------------------------------------------------------------------
558     virtual lldb::ThreadPlanSP
559     QueueThreadPlanForStepOverRange (bool abort_other_plans,
560                                  const AddressRange &range,
561                                  const SymbolContext &addr_context,
562                                  lldb::RunMode stop_other_threads);
563 
564     //------------------------------------------------------------------
565     /// Queues the plan used to step through an address range, stepping into functions.
566     ///
567     /// @param[in] abort_other_plans
568     ///    \b true if we discard the currently queued plans and replace them with this one.
569     ///    Otherwise this plan will go on the end of the plan stack.
570     ///
571     /// @param[in] type
572     ///    Type of step to do, only eStepTypeInto and eStepTypeOver are supported by this plan.
573     ///
574     /// @param[in] range
575     ///    The address range to step through.
576     ///
577     /// @param[in] addr_context
578     ///    When dealing with stepping through inlined functions the current PC is not enough information to know
579     ///    what "step" means.  For instance a series of nested inline functions might start at the same address.
580     //     The \a addr_context provides the current symbol context the step
581     ///    is supposed to be out of.
582     //   FIXME: Currently unused.
583     ///
584     /// @param[in] step_in_target
585     ///    Name if function we are trying to step into.  We will step out if we don't land in that function.
586     ///
587     /// @param[in] stop_other_threads
588     ///    \b true if we will stop other threads while we single step this one.
589     ///
590     /// @param[in] avoid_code_without_debug_info
591     ///    If \b true we will step out if we step into code with no debug info.
592     ///
593     /// @return
594     ///     A shared pointer to the newly queued thread plan, or NULL if the plan could not be queued.
595     //------------------------------------------------------------------
596     virtual lldb::ThreadPlanSP
597     QueueThreadPlanForStepInRange (bool abort_other_plans,
598                                  const AddressRange &range,
599                                  const SymbolContext &addr_context,
600                                  const char *step_in_target,
601                                  lldb::RunMode stop_other_threads,
602                                  bool avoid_code_without_debug_info);
603 
604     //------------------------------------------------------------------
605     /// Queue the plan used to step out of the function at the current PC of
606     /// \a thread.
607     ///
608     /// @param[in] abort_other_plans
609     ///    \b true if we discard the currently queued plans and replace them with this one.
610     ///    Otherwise this plan will go on the end of the plan stack.
611     ///
612     /// @param[in] addr_context
613     ///    When dealing with stepping through inlined functions the current PC is not enough information to know
614     ///    what "step" means.  For instance a series of nested inline functions might start at the same address.
615     //     The \a addr_context provides the current symbol context the step
616     ///    is supposed to be out of.
617     //   FIXME: Currently unused.
618     ///
619     /// @param[in] first_insn
620     ///     \b true if this is the first instruction of a function.
621     ///
622     /// @param[in] stop_other_threads
623     ///    \b true if we will stop other threads while we single step this one.
624     ///
625     /// @param[in] stop_vote
626     /// @param[in] run_vote
627     ///    See standard meanings for the stop & run votes in ThreadPlan.h.
628     ///
629     /// @return
630     ///     A shared pointer to the newly queued thread plan, or NULL if the plan could not be queued.
631     //------------------------------------------------------------------
632     virtual lldb::ThreadPlanSP
633     QueueThreadPlanForStepOut (bool abort_other_plans,
634                                SymbolContext *addr_context,
635                                bool first_insn,
636                                bool stop_other_threads,
637                                Vote stop_vote, // = eVoteYes,
638                                Vote run_vote, // = eVoteNoOpinion);
639                                uint32_t frame_idx);
640 
641     //------------------------------------------------------------------
642     /// Gets the plan used to step through the code that steps from a function
643     /// call site at the current PC into the actual function call.
644     ///
645     ///
646     /// @param[in] return_stack_id
647     ///    The stack id that we will return to (by setting backstop breakpoints on the return
648     ///    address to that frame) if we fail to step through.
649     ///
650     /// @param[in] abort_other_plans
651     ///    \b true if we discard the currently queued plans and replace them with this one.
652     ///    Otherwise this plan will go on the end of the plan stack.
653     ///
654     /// @param[in] stop_other_threads
655     ///    \b true if we will stop other threads while we single step this one.
656     ///
657     /// @return
658     ///     A shared pointer to the newly queued thread plan, or NULL if the plan could not be queued.
659     //------------------------------------------------------------------
660     virtual lldb::ThreadPlanSP
661     QueueThreadPlanForStepThrough (StackID &return_stack_id,
662                                    bool abort_other_plans,
663                                    bool stop_other_threads);
664 
665     //------------------------------------------------------------------
666     /// Gets the plan used to continue from the current PC.
667     /// This is a simple plan, mostly useful as a backstop when you are continuing
668     /// for some particular purpose.
669     ///
670     /// @param[in] abort_other_plans
671     ///    \b true if we discard the currently queued plans and replace them with this one.
672     ///    Otherwise this plan will go on the end of the plan stack.
673     ///
674     /// @param[in] target_addr
675     ///    The address to which we're running.
676     ///
677     /// @param[in] stop_other_threads
678     ///    \b true if we will stop other threads while we single step this one.
679     ///
680     /// @return
681     ///     A shared pointer to the newly queued thread plan, or NULL if the plan could not be queued.
682     //------------------------------------------------------------------
683     virtual lldb::ThreadPlanSP
684     QueueThreadPlanForRunToAddress (bool abort_other_plans,
685                                     Address &target_addr,
686                                     bool stop_other_threads);
687 
688     virtual lldb::ThreadPlanSP
689     QueueThreadPlanForStepUntil (bool abort_other_plans,
690                                  lldb::addr_t *address_list,
691                                  size_t num_addresses,
692                                  bool stop_others,
693                                  uint32_t frame_idx);
694 
695     virtual lldb::ThreadPlanSP
696     QueueThreadPlanForCallFunction (bool abort_other_plans,
697                                     Address& function,
698                                     lldb::addr_t arg,
699                                     bool stop_other_threads,
700                                     bool unwind_on_error = false,
701                                     bool ignore_breakpoints = true);
702 
703     //------------------------------------------------------------------
704     // Thread Plan accessors:
705     //------------------------------------------------------------------
706 
707     //------------------------------------------------------------------
708     /// Gets the plan which will execute next on the plan stack.
709     ///
710     /// @return
711     ///     A pointer to the next executed plan.
712     //------------------------------------------------------------------
713     ThreadPlan *
714     GetCurrentPlan ();
715 
716     //------------------------------------------------------------------
717     /// Unwinds the thread stack for the innermost expression plan currently
718     /// on the thread plan stack.
719     ///
720     /// @return
721     ///     An error if the thread plan could not be unwound.
722     //------------------------------------------------------------------
723 
724     Error
725     UnwindInnermostExpression();
726 
727 private:
728     bool
729     PlanIsBasePlan (ThreadPlan *plan_ptr);
730 
731     void
732     BroadcastSelectedFrameChange(StackID &new_frame_id);
733 
734 public:
735 
736     //------------------------------------------------------------------
737     /// Gets the outer-most plan that was popped off the plan stack in the
738     /// most recent stop.  Useful for printing the stop reason accurately.
739     ///
740     /// @return
741     ///     A pointer to the last completed plan.
742     //------------------------------------------------------------------
743     lldb::ThreadPlanSP
744     GetCompletedPlan ();
745 
746     //------------------------------------------------------------------
747     /// Gets the outer-most return value from the completed plans
748     ///
749     /// @return
750     ///     A ValueObjectSP, either empty if there is no return value,
751     ///     or containing the return value.
752     //------------------------------------------------------------------
753     lldb::ValueObjectSP
754     GetReturnValueObject ();
755 
756     //------------------------------------------------------------------
757     ///  Checks whether the given plan is in the completed plans for this
758     ///  stop.
759     ///
760     /// @param[in] plan
761     ///     Pointer to the plan you're checking.
762     ///
763     /// @return
764     ///     Returns true if the input plan is in the completed plan stack,
765     ///     false otherwise.
766     //------------------------------------------------------------------
767     bool
768     IsThreadPlanDone (ThreadPlan *plan);
769 
770     //------------------------------------------------------------------
771     ///  Checks whether the given plan is in the discarded plans for this
772     ///  stop.
773     ///
774     /// @param[in] plan
775     ///     Pointer to the plan you're checking.
776     ///
777     /// @return
778     ///     Returns true if the input plan is in the discarded plan stack,
779     ///     false otherwise.
780     //------------------------------------------------------------------
781     bool
782     WasThreadPlanDiscarded (ThreadPlan *plan);
783 
784     //------------------------------------------------------------------
785     /// Queues a generic thread plan.
786     ///
787     /// @param[in] plan_sp
788     ///    The plan to queue.
789     ///
790     /// @param[in] abort_other_plans
791     ///    \b true if we discard the currently queued plans and replace them with this one.
792     ///    Otherwise this plan will go on the end of the plan stack.
793     ///
794     /// @return
795     ///     A pointer to the last completed plan.
796     //------------------------------------------------------------------
797     void
798     QueueThreadPlan (lldb::ThreadPlanSP &plan_sp, bool abort_other_plans);
799 
800 
801     //------------------------------------------------------------------
802     /// Discards the plans queued on the plan stack of the current thread.  This is
803     /// arbitrated by the "Master" ThreadPlans, using the "OkayToDiscard" call.
804     //  But if \a force is true, all thread plans are discarded.
805     //------------------------------------------------------------------
806     void
807     DiscardThreadPlans (bool force);
808 
809     //------------------------------------------------------------------
810     /// Discards the plans queued on the plan stack of the current thread up to and
811     /// including up_to_plan_sp.
812     //
813     // @param[in] up_to_plan_sp
814     //   Discard all plans up to and including this one.
815     //------------------------------------------------------------------
816     void
817     DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp);
818 
819     void
820     DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr);
821 
822     //------------------------------------------------------------------
823     /// Prints the current plan stack.
824     ///
825     /// @param[in] s
826     ///    The stream to which to dump the plan stack info.
827     ///
828     //------------------------------------------------------------------
829     void
830     DumpThreadPlans (Stream *s) const;
831 
832     virtual bool
833     CheckpointThreadState (ThreadStateCheckpoint &saved_state);
834 
835     virtual bool
836     RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state);
837 
838     virtual bool
839     RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state);
840 
841     void
842     EnableTracer (bool value, bool single_step);
843 
844     void
845     SetTracer (lldb::ThreadPlanTracerSP &tracer_sp);
846 
847     //------------------------------------------------------------------
848     // Get the thread index ID. The index ID that is guaranteed to not
849     // be re-used by a process. They start at 1 and increase with each
850     // new thread. This allows easy command line access by a unique ID
851     // that is easier to type than the actual system thread ID.
852     //------------------------------------------------------------------
853     uint32_t
854     GetIndexID () const;
855 
856 
857     //------------------------------------------------------------------
858     // The API ID is often the same as the Thread::GetID(), but not in
859     // all cases. Thread::GetID() is the user visible thread ID that
860     // clients would want to see. The API thread ID is the thread ID
861     // that is used when sending data to/from the debugging protocol.
862     //------------------------------------------------------------------
863     virtual lldb::user_id_t
GetProtocolID()864     GetProtocolID () const
865     {
866         return GetID();
867     }
868 
869     //------------------------------------------------------------------
870     // lldb::ExecutionContextScope pure virtual functions
871     //------------------------------------------------------------------
872     virtual lldb::TargetSP
873     CalculateTarget ();
874 
875     virtual lldb::ProcessSP
876     CalculateProcess ();
877 
878     virtual lldb::ThreadSP
879     CalculateThread ();
880 
881     virtual lldb::StackFrameSP
882     CalculateStackFrame ();
883 
884     virtual void
885     CalculateExecutionContext (ExecutionContext &exe_ctx);
886 
887     lldb::StackFrameSP
888     GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr);
889 
890     size_t
891     GetStatus (Stream &strm,
892                uint32_t start_frame,
893                uint32_t num_frames,
894                uint32_t num_frames_with_source);
895 
896     size_t
897     GetStackFrameStatus (Stream& strm,
898                          uint32_t first_frame,
899                          uint32_t num_frames,
900                          bool show_frame_info,
901                          uint32_t num_frames_with_source);
902 
903     // We need a way to verify that even though we have a thread in a shared
904     // pointer that the object itself is still valid. Currently this won't be
905     // the case if DestroyThread() was called. DestroyThread is called when
906     // a thread has been removed from the Process' thread list.
907     bool
IsValid()908     IsValid () const
909     {
910         return !m_destroy_called;
911     }
912 
913     // Sets and returns a valid stop info based on the process stop ID and the
914     // current thread plan. If the thread stop ID does not match the process'
915     // stop ID, the private stop reason is not set and an invalid StopInfoSP may
916     // be returned.
917     //
918     // NOTE: This function must be called before the current thread plan is
919     // moved to the completed plan stack (in Thread::ShouldStop()).
920     //
921     // NOTE: If subclasses override this function, ensure they do not overwrite
922     // the m_actual_stop_info if it is valid.  The stop info may be a
923     // "checkpointed and restored" stop info, so if it is still around it is
924     // right even if you have not calculated this yourself, or if it disagrees
925     // with what you might have calculated.
926     virtual lldb::StopInfoSP
927     GetPrivateStopInfo ();
928 
929     //----------------------------------------------------------------------
930     // Ask the thread subclass to set its stop info.
931     //
932     // Thread subclasses should call Thread::SetStopInfo(...) with the
933     // reason the thread stopped.
934     //
935     // @return
936     //      True if Thread::SetStopInfo(...) was called, false otherwise.
937     //----------------------------------------------------------------------
938     virtual bool
939     CalculateStopInfo () = 0;
940 
941     //----------------------------------------------------------------------
942     // Gets the temporary resume state for a thread.
943     //
944     // This value gets set in each thread by complex debugger logic in
945     // Thread::ShouldResume() and an appropriate thread resume state will get
946     // set in each thread every time the process is resumed prior to calling
947     // Process::DoResume(). The lldb_private::Process subclass should adhere
948     // to the thread resume state request which will be one of:
949     //
950     //  eStateRunning   - thread will resume when process is resumed
951     //  eStateStepping  - thread should step 1 instruction and stop when process
952     //                    is resumed
953     //  eStateSuspended - thread should not execute any instructions when
954     //                    process is resumed
955     //----------------------------------------------------------------------
956     lldb::StateType
GetTemporaryResumeState()957     GetTemporaryResumeState() const
958     {
959         return m_temporary_resume_state;
960     }
961 
962     void
963     SetStopInfo (const lldb::StopInfoSP &stop_info_sp);
964 
965     void
966     SetShouldReportStop (Vote vote);
967 
968 protected:
969 
970     friend class ThreadPlan;
971     friend class ThreadList;
972     friend class ThreadEventData;
973     friend class StackFrameList;
974     friend class StackFrame;
975     friend class OperatingSystem;
976 
977     // This is necessary to make sure thread assets get destroyed while the thread is still in good shape
978     // to call virtual thread methods.  This must be called by classes that derive from Thread in their destructor.
979     virtual void DestroyThread ();
980 
981     void
982     PushPlan (lldb::ThreadPlanSP &plan_sp);
983 
984     void
985     PopPlan ();
986 
987     void
988     DiscardPlan ();
989 
990     ThreadPlan *GetPreviousPlan (ThreadPlan *plan);
991 
992     typedef std::vector<lldb::ThreadPlanSP> plan_stack;
993 
994     virtual bool
995     SaveFrameZeroState (RegisterCheckpoint &checkpoint);
996 
997     virtual bool
998     RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint);
999 
1000     // register_data_sp must be a DataSP passed to ReadAllRegisterValues.
1001     bool
1002     ResetFrameZeroRegisters (lldb::DataBufferSP register_data_sp);
1003 
1004     virtual lldb_private::Unwind *
1005     GetUnwinder ();
1006 
1007     // Check to see whether the thread is still at the last breakpoint hit that stopped it.
1008     virtual bool
1009     IsStillAtLastBreakpointHit();
1010 
1011     // Some threads are threads that are made up by OperatingSystem plugins that
1012     // are threads that exist and are context switched out into memory. The
1013     // OperatingSystem plug-in need a ways to know if a thread is "real" or made
1014     // up.
1015     virtual bool
IsOperatingSystemPluginThread()1016     IsOperatingSystemPluginThread () const
1017     {
1018         return false;
1019     }
1020 
1021 
1022     lldb::StackFrameListSP
1023     GetStackFrameList ();
1024 
1025     struct ThreadState
1026     {
1027         uint32_t           orig_stop_id;
1028         lldb::StopInfoSP   stop_info_sp;
1029         RegisterCheckpoint register_backup;
1030     };
1031 
1032     //------------------------------------------------------------------
1033     // Classes that inherit from Process can see and modify these
1034     //------------------------------------------------------------------
1035     lldb::ProcessWP     m_process_wp;           ///< The process that owns this thread.
1036     lldb::StopInfoSP    m_stop_info_sp;         ///< The private stop reason for this thread
1037     uint32_t            m_stop_info_stop_id;    // This is the stop id for which the StopInfo is valid.  Can use this so you know that
1038     // the thread's m_stop_info_sp is current and you don't have to fetch it again
1039     const uint32_t      m_index_id;             ///< A unique 1 based index assigned to each thread for easy UI/command line access.
1040     lldb::RegisterContextSP m_reg_context_sp;   ///< The register context for this thread's current register state.
1041     lldb::StateType     m_state;                ///< The state of our process.
1042     mutable Mutex       m_state_mutex;          ///< Multithreaded protection for m_state.
1043     plan_stack          m_plan_stack;           ///< The stack of plans this thread is executing.
1044     plan_stack          m_completed_plan_stack; ///< Plans that have been completed by this stop.  They get deleted when the thread resumes.
1045     plan_stack          m_discarded_plan_stack; ///< Plans that have been discarded by this stop.  They get deleted when the thread resumes.
1046     mutable Mutex       m_frame_mutex;          ///< Multithreaded protection for m_state.
1047     lldb::StackFrameListSP m_curr_frames_sp;    ///< The stack frames that get lazily populated after a thread stops.
1048     lldb::StackFrameListSP m_prev_frames_sp;    ///< The previous stack frames from the last time this thread stopped.
1049     int                 m_resume_signal;        ///< The signal that should be used when continuing this thread.
1050     lldb::StateType     m_resume_state;         ///< This state is used to force a thread to be suspended from outside the ThreadPlan logic.
1051     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.
1052                                                   /// It gets set in Thread::ShoudResume.
1053     std::unique_ptr<lldb_private::Unwind> m_unwinder_ap;
1054     bool                m_destroy_called;       // This is used internally to make sure derived Thread classes call DestroyThread.
1055     LazyBool            m_override_should_notify;
1056 private:
1057     //------------------------------------------------------------------
1058     // For Thread only
1059     //------------------------------------------------------------------
1060 
1061     DISALLOW_COPY_AND_ASSIGN (Thread);
1062 
1063 };
1064 
1065 } // namespace lldb_private
1066 
1067 #endif  // liblldb_Thread_h_
1068