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