1 //===-- NativeThreadDarwin.h ---------------------------------- -*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef NativeThreadDarwin_H
10 #define NativeThreadDarwin_H
13 #include <mach/mach_types.h>
15 #include <sys/proc_info.h>
23 #include "lldb/Host/common/NativeThreadProtocol.h"
24 #include "lldb/lldb-private-forward.h"
26 #include "MachException.h"
28 namespace lldb_private {
29 namespace process_darwin {
31 class NativeProcessDarwin;
32 using NativeProcessDarwinSP = std::shared_ptr<NativeProcessDarwin>;
34 class NativeThreadListDarwin;
36 class NativeThreadDarwin : public NativeThreadProtocol {
37 friend class NativeProcessDarwin;
38 friend class NativeThreadListDarwin;
42 GetGloballyUniqueThreadIDForMachPortID(::thread_t mach_port_id);
44 NativeThreadDarwin(NativeProcessDarwin *process, bool is_64_bit,
45 lldb::tid_t unique_thread_id = 0,
46 ::thread_t mach_thread_port = 0);
48 // NativeThreadProtocol Interface
49 std::string GetName() override;
51 lldb::StateType GetState() override;
53 bool GetStopReason(ThreadStopInfo &stop_info,
54 std::string &description) override;
56 NativeRegisterContextSP GetRegisterContext() override;
58 Status SetWatchpoint(lldb::addr_t addr, size_t size, uint32_t watch_flags,
59 bool hardware) override;
61 Status RemoveWatchpoint(lldb::addr_t addr) override;
63 // New methods that are fine for others to call.
64 void Dump(Stream &stream) const;
67 // Interface for friend classes
69 /// Resumes the thread. If \p signo is anything but
70 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
71 Status Resume(uint32_t signo);
73 /// Single steps the thread. If \p signo is anything but
74 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
75 Status SingleStep(uint32_t signo);
77 bool NotifyException(MachException::Data &exc);
79 bool ShouldStop(bool &step_more) const;
83 void SetStoppedBySignal(uint32_t signo, const siginfo_t *info = nullptr);
85 /// Return true if the thread is stopped.
86 /// If stopped by a signal, indicate the signo in the signo
87 /// argument. Otherwise, return LLDB_INVALID_SIGNAL_NUMBER.
88 bool IsStopped(int *signo);
90 const struct thread_basic_info *GetBasicInfo() const;
92 static bool GetBasicInfo(::thread_t thread,
93 struct thread_basic_info *basicInfoPtr);
95 bool IsUserReady() const;
97 void SetStoppedByExec();
99 void SetStoppedByBreakpoint();
101 void SetStoppedByWatchpoint(uint32_t wp_index);
103 bool IsStoppedAtBreakpoint();
105 bool IsStoppedAtWatchpoint();
107 void SetStoppedByTrace();
109 void SetStoppedWithNoReason();
113 Status RequestStop();
115 /// Return the mach thread port number for this thread.
118 /// The mach port number for this thread. Returns NULL_THREAD
119 /// when the thread is invalid.
120 thread_t GetMachPortNumber() const { return m_mach_thread_port; }
122 static bool MachPortNumberIsValid(::thread_t thread);
125 bool GetIdentifierInfo();
127 void MaybeLogStateChange(lldb::StateType new_state);
129 NativeProcessDarwinSP GetNativeProcessDarwinSP();
133 inline void MaybePrepareSingleStepWorkaround();
135 inline void MaybeCleanupSingleStepWorkaround();
139 // The mach thread port for the thread.
140 ::thread_t m_mach_thread_port;
142 // The most recently-retrieved thread basic info.
143 mutable ::thread_basic_info m_basic_info;
145 struct proc_threadinfo m_proc_threadinfo;
147 thread_identifier_info_data_t m_ident_info;
150 lldb::StateType m_state;
151 ThreadStopInfo m_stop_info;
152 NativeRegisterContextSP m_reg_context_sp;
153 std::string m_stop_description;
154 using WatchpointIndexMap = std::map<lldb::addr_t, uint32_t>;
155 WatchpointIndexMap m_watchpoint_index_map;
156 // cpu_set_t m_original_cpu_set; // For single-step workaround.
160 typedef std::shared_ptr<NativeThreadDarwin> NativeThreadDarwinSP;
162 } // namespace process_darwin
163 } // namespace lldb_private
165 #endif // #ifndef NativeThreadDarwin_H