1 //===-- NativeThreadDarwin.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 NativeThreadDarwin_H
11 #define NativeThreadDarwin_H
12 
13 // C includes
14 #include <mach/mach_types.h>
15 #include <sched.h>
16 #include <sys/proc_info.h>
17 
18 // C++ includes
19 #include <map>
20 #include <memory>
21 #include <string>
22 
23 // LLDB includes
24 #include "lldb/Host/common/NativeThreadProtocol.h"
25 #include "lldb/lldb-private-forward.h"
26 
27 #include "MachException.h"
28 
29 namespace lldb_private {
30 namespace process_darwin {
31 
32 class NativeProcessDarwin;
33 using NativeProcessDarwinSP = std::shared_ptr<NativeProcessDarwin>;
34 
35 class NativeThreadListDarwin;
36 
37 class NativeThreadDarwin : public NativeThreadProtocol {
38   friend class NativeProcessDarwin;
39   friend class NativeThreadListDarwin;
40 
41 public:
42   static uint64_t
43   GetGloballyUniqueThreadIDForMachPortID(::thread_t mach_port_id);
44 
45   NativeThreadDarwin(NativeProcessDarwin *process, bool is_64_bit,
46                      lldb::tid_t unique_thread_id = 0,
47                      ::thread_t mach_thread_port = 0);
48 
49   // -----------------------------------------------------------------
50   // NativeThreadProtocol Interface
51   // -----------------------------------------------------------------
52   std::string GetName() override;
53 
54   lldb::StateType GetState() override;
55 
56   bool GetStopReason(ThreadStopInfo &stop_info,
57                      std::string &description) override;
58 
59   NativeRegisterContextSP GetRegisterContext() override;
60 
61   Status SetWatchpoint(lldb::addr_t addr, size_t size, uint32_t watch_flags,
62                        bool hardware) override;
63 
64   Status RemoveWatchpoint(lldb::addr_t addr) override;
65 
66   // -----------------------------------------------------------------
67   // New methods that are fine for others to call.
68   // -----------------------------------------------------------------
69   void Dump(Stream &stream) const;
70 
71 private:
72   // -----------------------------------------------------------------
73   // Interface for friend classes
74   // -----------------------------------------------------------------
75 
76   /// Resumes the thread.  If @p signo is anything but
77   /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
78   Status Resume(uint32_t signo);
79 
80   /// Single steps the thread.  If @p signo is anything but
81   /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
82   Status SingleStep(uint32_t signo);
83 
84   bool NotifyException(MachException::Data &exc);
85 
86   bool ShouldStop(bool &step_more) const;
87 
88   void ThreadDidStop();
89 
90   void SetStoppedBySignal(uint32_t signo, const siginfo_t *info = nullptr);
91 
92   /// Return true if the thread is stopped.
93   /// If stopped by a signal, indicate the signo in the signo
94   /// argument.  Otherwise, return LLDB_INVALID_SIGNAL_NUMBER.
95   bool IsStopped(int *signo);
96 
97   const struct thread_basic_info *GetBasicInfo() const;
98 
99   static bool GetBasicInfo(::thread_t thread,
100                            struct thread_basic_info *basicInfoPtr);
101 
102   bool IsUserReady() const;
103 
104   void SetStoppedByExec();
105 
106   void SetStoppedByBreakpoint();
107 
108   void SetStoppedByWatchpoint(uint32_t wp_index);
109 
110   bool IsStoppedAtBreakpoint();
111 
112   bool IsStoppedAtWatchpoint();
113 
114   void SetStoppedByTrace();
115 
116   void SetStoppedWithNoReason();
117 
118   void SetExited();
119 
120   Status RequestStop();
121 
122   // -------------------------------------------------------------------------
123   /// Return the mach thread port number for this thread.
124   ///
125   /// @return
126   ///     The mach port number for this thread.  Returns NULL_THREAD
127   ///     when the thread is invalid.
128   // -------------------------------------------------------------------------
GetMachPortNumber()129   thread_t GetMachPortNumber() const { return m_mach_thread_port; }
130 
131   static bool MachPortNumberIsValid(::thread_t thread);
132 
133   // ---------------------------------------------------------------------
134   // Private interface
135   // ---------------------------------------------------------------------
136   bool GetIdentifierInfo();
137 
138   void MaybeLogStateChange(lldb::StateType new_state);
139 
140   NativeProcessDarwinSP GetNativeProcessDarwinSP();
141 
142   void SetStopped();
143 
144   inline void MaybePrepareSingleStepWorkaround();
145 
146   inline void MaybeCleanupSingleStepWorkaround();
147 
148   // -----------------------------------------------------------------
149   // Member Variables
150   // -----------------------------------------------------------------
151 
152   // The mach thread port for the thread.
153   ::thread_t m_mach_thread_port;
154 
155   // The most recently-retrieved thread basic info.
156   mutable ::thread_basic_info m_basic_info;
157 
158   struct proc_threadinfo m_proc_threadinfo;
159 
160   thread_identifier_info_data_t m_ident_info;
161 
162 #if 0
163     lldb::StateType m_state;
164     ThreadStopInfo m_stop_info;
165     NativeRegisterContextSP m_reg_context_sp;
166     std::string m_stop_description;
167     using WatchpointIndexMap = std::map<lldb::addr_t, uint32_t>;
168     WatchpointIndexMap m_watchpoint_index_map;
169     // cpu_set_t m_original_cpu_set; // For single-step workaround.
170 #endif
171 };
172 
173 typedef std::shared_ptr<NativeThreadDarwin> NativeThreadDarwinSP;
174 
175 } // namespace process_darwin
176 } // namespace lldb_private
177 
178 #endif // #ifndef NativeThreadDarwin_H
179