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 |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 24 | #include "lldb/Host/common/NativeThreadProtocol.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 25 | #include "lldb/lldb-private-forward.h" |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 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 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 37 | class NativeThreadDarwin : public NativeThreadProtocol { |
| 38 | friend class NativeProcessDarwin; |
| 39 | friend class NativeThreadListDarwin; |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 40 | |
| 41 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 42 | static uint64_t |
| 43 | GetGloballyUniqueThreadIDForMachPortID(::thread_t mach_port_id); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 44 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 45 | NativeThreadDarwin(NativeProcessDarwin *process, bool is_64_bit, |
| 46 | lldb::tid_t unique_thread_id = 0, |
| 47 | ::thread_t mach_thread_port = 0); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 48 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 49 | // ----------------------------------------------------------------- |
| 50 | // NativeThreadProtocol Interface |
| 51 | // ----------------------------------------------------------------- |
| 52 | std::string GetName() override; |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 53 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 54 | lldb::StateType GetState() override; |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 55 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 56 | bool GetStopReason(ThreadStopInfo &stop_info, |
| 57 | std::string &description) override; |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 58 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 59 | NativeRegisterContextSP GetRegisterContext() override; |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 60 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 61 | Error SetWatchpoint(lldb::addr_t addr, size_t size, uint32_t watch_flags, |
| 62 | bool hardware) override; |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 63 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 64 | Error RemoveWatchpoint(lldb::addr_t addr) override; |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 65 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 66 | // ----------------------------------------------------------------- |
| 67 | // New methods that are fine for others to call. |
| 68 | // ----------------------------------------------------------------- |
| 69 | void Dump(Stream &stream) const; |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 70 | |
| 71 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 72 | // ----------------------------------------------------------------- |
| 73 | // Interface for friend classes |
| 74 | // ----------------------------------------------------------------- |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 75 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 76 | /// Resumes the thread. If @p signo is anything but |
| 77 | /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. |
| 78 | Error Resume(uint32_t signo); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 79 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 80 | /// Single steps the thread. If @p signo is anything but |
| 81 | /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. |
| 82 | Error SingleStep(uint32_t signo); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 83 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 84 | bool NotifyException(MachException::Data &exc); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 85 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 86 | bool ShouldStop(bool &step_more) const; |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 87 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 88 | void ThreadDidStop(); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 89 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 90 | void SetStoppedBySignal(uint32_t signo, const siginfo_t *info = nullptr); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 91 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 96 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 97 | const struct thread_basic_info *GetBasicInfo() const; |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 98 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 99 | static bool GetBasicInfo(::thread_t thread, |
| 100 | struct thread_basic_info *basicInfoPtr); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 102 | bool IsUserReady() const; |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 103 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 104 | void SetStoppedByExec(); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 105 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 106 | void SetStoppedByBreakpoint(); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 107 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 108 | void SetStoppedByWatchpoint(uint32_t wp_index); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 109 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 110 | bool IsStoppedAtBreakpoint(); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 111 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 112 | bool IsStoppedAtWatchpoint(); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 113 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 114 | void SetStoppedByTrace(); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 115 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 116 | void SetStoppedWithNoReason(); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 117 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 118 | void SetExited(); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 120 | Error RequestStop(); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 121 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 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 | // ------------------------------------------------------------------------- |
| 129 | thread_t GetMachPortNumber() const { return m_mach_thread_port; } |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 130 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 131 | static bool MachPortNumberIsValid(::thread_t thread); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 132 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 133 | // --------------------------------------------------------------------- |
| 134 | // Private interface |
| 135 | // --------------------------------------------------------------------- |
| 136 | bool GetIdentifierInfo(); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 137 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 138 | void MaybeLogStateChange(lldb::StateType new_state); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 139 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 140 | NativeProcessDarwinSP GetNativeProcessDarwinSP(); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 141 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 142 | void SetStopped(); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 143 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 144 | inline void MaybePrepareSingleStepWorkaround(); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 145 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 146 | inline void MaybeCleanupSingleStepWorkaround(); |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 147 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 148 | // ----------------------------------------------------------------- |
| 149 | // Member Variables |
| 150 | // ----------------------------------------------------------------- |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 151 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 152 | // The mach thread port for the thread. |
| 153 | ::thread_t m_mach_thread_port; |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 154 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 155 | // The most recently-retrieved thread basic info. |
| 156 | mutable ::thread_basic_info m_basic_info; |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 157 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 158 | struct proc_threadinfo m_proc_threadinfo; |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 159 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 160 | thread_identifier_info_data_t m_ident_info; |
Todd Fiala | e77fce0 | 2016-09-04 00:18:56 +0000 | [diff] [blame] | 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 |