Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 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/lldb-private-forward.h" |
| 25 | #include "lldb/Host/common/NativeThreadProtocol.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 | { |
| 39 | friend class NativeProcessDarwin; |
| 40 | friend class NativeThreadListDarwin; |
| 41 | |
| 42 | public: |
| 43 | |
| 44 | static uint64_t |
| 45 | GetGloballyUniqueThreadIDForMachPortID(::thread_t mach_port_id); |
| 46 | |
| 47 | NativeThreadDarwin(NativeProcessDarwin *process, bool is_64_bit, |
| 48 | lldb::tid_t unique_thread_id = 0, |
| 49 | ::thread_t mach_thread_port = 0); |
| 50 | |
| 51 | // ----------------------------------------------------------------- |
| 52 | // NativeThreadProtocol Interface |
| 53 | // ----------------------------------------------------------------- |
| 54 | std::string |
| 55 | GetName() override; |
| 56 | |
| 57 | lldb::StateType |
| 58 | GetState () override; |
| 59 | |
| 60 | bool |
| 61 | GetStopReason(ThreadStopInfo &stop_info, |
| 62 | std::string& description) override; |
| 63 | |
| 64 | NativeRegisterContextSP |
| 65 | GetRegisterContext() override; |
| 66 | |
| 67 | Error |
| 68 | SetWatchpoint(lldb::addr_t addr, size_t size, |
| 69 | uint32_t watch_flags, bool hardware) override; |
| 70 | |
| 71 | Error |
| 72 | RemoveWatchpoint(lldb::addr_t addr) override; |
| 73 | |
| 74 | // ----------------------------------------------------------------- |
| 75 | // New methods that are fine for others to call. |
| 76 | // ----------------------------------------------------------------- |
| 77 | void |
| 78 | Dump(Stream &stream) const; |
| 79 | |
| 80 | private: |
| 81 | // ----------------------------------------------------------------- |
| 82 | // Interface for friend classes |
| 83 | // ----------------------------------------------------------------- |
| 84 | |
| 85 | /// Resumes the thread. If @p signo is anything but |
| 86 | /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. |
| 87 | Error |
| 88 | Resume(uint32_t signo); |
| 89 | |
| 90 | /// Single steps the thread. If @p signo is anything but |
| 91 | /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. |
| 92 | Error |
| 93 | SingleStep(uint32_t signo); |
| 94 | |
| 95 | bool |
| 96 | NotifyException(MachException::Data &exc); |
| 97 | |
| 98 | bool |
| 99 | ShouldStop(bool &step_more) const; |
| 100 | |
| 101 | void |
| 102 | ThreadDidStop(); |
| 103 | |
| 104 | void |
| 105 | SetStoppedBySignal(uint32_t signo, const siginfo_t *info = nullptr); |
| 106 | |
| 107 | /// Return true if the thread is stopped. |
| 108 | /// If stopped by a signal, indicate the signo in the signo |
| 109 | /// argument. Otherwise, return LLDB_INVALID_SIGNAL_NUMBER. |
| 110 | bool |
| 111 | IsStopped (int *signo); |
| 112 | |
| 113 | const struct thread_basic_info * |
| 114 | GetBasicInfo() const; |
| 115 | |
| 116 | static bool |
| 117 | GetBasicInfo(::thread_t thread, struct thread_basic_info *basicInfoPtr); |
| 118 | |
| 119 | bool |
| 120 | IsUserReady() const; |
| 121 | |
| 122 | void |
| 123 | SetStoppedByExec (); |
| 124 | |
| 125 | void |
| 126 | SetStoppedByBreakpoint (); |
| 127 | |
| 128 | void |
| 129 | SetStoppedByWatchpoint (uint32_t wp_index); |
| 130 | |
| 131 | bool |
| 132 | IsStoppedAtBreakpoint (); |
| 133 | |
| 134 | bool |
| 135 | IsStoppedAtWatchpoint (); |
| 136 | |
| 137 | void |
| 138 | SetStoppedByTrace (); |
| 139 | |
| 140 | void |
| 141 | SetStoppedWithNoReason (); |
| 142 | |
| 143 | void |
| 144 | SetExited (); |
| 145 | |
| 146 | Error |
| 147 | RequestStop (); |
| 148 | |
| 149 | // ------------------------------------------------------------------------- |
| 150 | /// Return the mach thread port number for this thread. |
| 151 | /// |
| 152 | /// @return |
| 153 | /// The mach port number for this thread. Returns NULL_THREAD |
| 154 | /// when the thread is invalid. |
| 155 | // ------------------------------------------------------------------------- |
| 156 | thread_t |
| 157 | GetMachPortNumber() const |
| 158 | { |
| 159 | return m_mach_thread_port; |
| 160 | } |
| 161 | |
| 162 | static bool |
| 163 | MachPortNumberIsValid(::thread_t thread); |
| 164 | |
| 165 | // --------------------------------------------------------------------- |
| 166 | // Private interface |
| 167 | // --------------------------------------------------------------------- |
| 168 | bool |
| 169 | GetIdentifierInfo(); |
| 170 | |
| 171 | void |
| 172 | MaybeLogStateChange (lldb::StateType new_state); |
| 173 | |
| 174 | NativeProcessDarwinSP |
| 175 | GetNativeProcessDarwinSP(); |
| 176 | |
| 177 | void |
| 178 | SetStopped(); |
| 179 | |
| 180 | inline void |
| 181 | MaybePrepareSingleStepWorkaround(); |
| 182 | |
| 183 | inline void |
| 184 | MaybeCleanupSingleStepWorkaround(); |
| 185 | |
| 186 | // ----------------------------------------------------------------- |
| 187 | // Member Variables |
| 188 | // ----------------------------------------------------------------- |
| 189 | |
| 190 | // The mach thread port for the thread. |
| 191 | ::thread_t m_mach_thread_port; |
| 192 | |
| 193 | // The most recently-retrieved thread basic info. |
| 194 | mutable ::thread_basic_info m_basic_info; |
| 195 | |
| 196 | struct proc_threadinfo m_proc_threadinfo; |
| 197 | |
| 198 | thread_identifier_info_data_t m_ident_info; |
| 199 | |
| 200 | #if 0 |
| 201 | lldb::StateType m_state; |
| 202 | ThreadStopInfo m_stop_info; |
| 203 | NativeRegisterContextSP m_reg_context_sp; |
| 204 | std::string m_stop_description; |
| 205 | using WatchpointIndexMap = std::map<lldb::addr_t, uint32_t>; |
| 206 | WatchpointIndexMap m_watchpoint_index_map; |
| 207 | // cpu_set_t m_original_cpu_set; // For single-step workaround. |
| 208 | #endif |
| 209 | }; |
| 210 | |
| 211 | typedef std::shared_ptr<NativeThreadDarwin> NativeThreadDarwinSP; |
| 212 | |
| 213 | } // namespace process_darwin |
| 214 | } // namespace lldb_private |
| 215 | |
| 216 | #endif // #ifndef NativeThreadDarwin_H |