blob: b8d9089e673e65c9d1651961aeff5d94ba6ef7cb [file] [log] [blame]
Todd Fialae77fce02016-09-04 00:18:56 +00001//===-- 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 Fialae77fce02016-09-04 00:18:56 +000024#include "lldb/Host/common/NativeThreadProtocol.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000025#include "lldb/lldb-private-forward.h"
Todd Fialae77fce02016-09-04 00:18:56 +000026
27#include "MachException.h"
28
29namespace lldb_private {
30namespace process_darwin {
31
32class NativeProcessDarwin;
33using NativeProcessDarwinSP = std::shared_ptr<NativeProcessDarwin>;
34
35class NativeThreadListDarwin;
36
Kate Stoneb9c1b512016-09-06 20:57:50 +000037class NativeThreadDarwin : public NativeThreadProtocol {
38 friend class NativeProcessDarwin;
39 friend class NativeThreadListDarwin;
Todd Fialae77fce02016-09-04 00:18:56 +000040
41public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000042 static uint64_t
43 GetGloballyUniqueThreadIDForMachPortID(::thread_t mach_port_id);
Todd Fialae77fce02016-09-04 00:18:56 +000044
Kate Stoneb9c1b512016-09-06 20:57:50 +000045 NativeThreadDarwin(NativeProcessDarwin *process, bool is_64_bit,
46 lldb::tid_t unique_thread_id = 0,
47 ::thread_t mach_thread_port = 0);
Todd Fialae77fce02016-09-04 00:18:56 +000048
Kate Stoneb9c1b512016-09-06 20:57:50 +000049 // -----------------------------------------------------------------
50 // NativeThreadProtocol Interface
51 // -----------------------------------------------------------------
52 std::string GetName() override;
Todd Fialae77fce02016-09-04 00:18:56 +000053
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 lldb::StateType GetState() override;
Todd Fialae77fce02016-09-04 00:18:56 +000055
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 bool GetStopReason(ThreadStopInfo &stop_info,
57 std::string &description) override;
Todd Fialae77fce02016-09-04 00:18:56 +000058
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 NativeRegisterContextSP GetRegisterContext() override;
Todd Fialae77fce02016-09-04 00:18:56 +000060
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 Error SetWatchpoint(lldb::addr_t addr, size_t size, uint32_t watch_flags,
62 bool hardware) override;
Todd Fialae77fce02016-09-04 00:18:56 +000063
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 Error RemoveWatchpoint(lldb::addr_t addr) override;
Todd Fialae77fce02016-09-04 00:18:56 +000065
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 // -----------------------------------------------------------------
67 // New methods that are fine for others to call.
68 // -----------------------------------------------------------------
69 void Dump(Stream &stream) const;
Todd Fialae77fce02016-09-04 00:18:56 +000070
71private:
Kate Stoneb9c1b512016-09-06 20:57:50 +000072 // -----------------------------------------------------------------
73 // Interface for friend classes
74 // -----------------------------------------------------------------
Todd Fialae77fce02016-09-04 00:18:56 +000075
Kate Stoneb9c1b512016-09-06 20:57:50 +000076 /// 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 Fialae77fce02016-09-04 00:18:56 +000079
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 /// 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 Fialae77fce02016-09-04 00:18:56 +000083
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 bool NotifyException(MachException::Data &exc);
Todd Fialae77fce02016-09-04 00:18:56 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 bool ShouldStop(bool &step_more) const;
Todd Fialae77fce02016-09-04 00:18:56 +000087
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 void ThreadDidStop();
Todd Fialae77fce02016-09-04 00:18:56 +000089
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 void SetStoppedBySignal(uint32_t signo, const siginfo_t *info = nullptr);
Todd Fialae77fce02016-09-04 00:18:56 +000091
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 /// 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 Fialae77fce02016-09-04 00:18:56 +000096
Kate Stoneb9c1b512016-09-06 20:57:50 +000097 const struct thread_basic_info *GetBasicInfo() const;
Todd Fialae77fce02016-09-04 00:18:56 +000098
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 static bool GetBasicInfo(::thread_t thread,
100 struct thread_basic_info *basicInfoPtr);
Todd Fialae77fce02016-09-04 00:18:56 +0000101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 bool IsUserReady() const;
Todd Fialae77fce02016-09-04 00:18:56 +0000103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104 void SetStoppedByExec();
Todd Fialae77fce02016-09-04 00:18:56 +0000105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 void SetStoppedByBreakpoint();
Todd Fialae77fce02016-09-04 00:18:56 +0000107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 void SetStoppedByWatchpoint(uint32_t wp_index);
Todd Fialae77fce02016-09-04 00:18:56 +0000109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 bool IsStoppedAtBreakpoint();
Todd Fialae77fce02016-09-04 00:18:56 +0000111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 bool IsStoppedAtWatchpoint();
Todd Fialae77fce02016-09-04 00:18:56 +0000113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 void SetStoppedByTrace();
Todd Fialae77fce02016-09-04 00:18:56 +0000115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 void SetStoppedWithNoReason();
Todd Fialae77fce02016-09-04 00:18:56 +0000117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 void SetExited();
Todd Fialae77fce02016-09-04 00:18:56 +0000119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 Error RequestStop();
Todd Fialae77fce02016-09-04 00:18:56 +0000121
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122 // -------------------------------------------------------------------------
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 Fialae77fce02016-09-04 00:18:56 +0000130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 static bool MachPortNumberIsValid(::thread_t thread);
Todd Fialae77fce02016-09-04 00:18:56 +0000132
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 // ---------------------------------------------------------------------
134 // Private interface
135 // ---------------------------------------------------------------------
136 bool GetIdentifierInfo();
Todd Fialae77fce02016-09-04 00:18:56 +0000137
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 void MaybeLogStateChange(lldb::StateType new_state);
Todd Fialae77fce02016-09-04 00:18:56 +0000139
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 NativeProcessDarwinSP GetNativeProcessDarwinSP();
Todd Fialae77fce02016-09-04 00:18:56 +0000141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 void SetStopped();
Todd Fialae77fce02016-09-04 00:18:56 +0000143
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 inline void MaybePrepareSingleStepWorkaround();
Todd Fialae77fce02016-09-04 00:18:56 +0000145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 inline void MaybeCleanupSingleStepWorkaround();
Todd Fialae77fce02016-09-04 00:18:56 +0000147
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 // -----------------------------------------------------------------
149 // Member Variables
150 // -----------------------------------------------------------------
Todd Fialae77fce02016-09-04 00:18:56 +0000151
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152 // The mach thread port for the thread.
153 ::thread_t m_mach_thread_port;
Todd Fialae77fce02016-09-04 00:18:56 +0000154
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155 // The most recently-retrieved thread basic info.
156 mutable ::thread_basic_info m_basic_info;
Todd Fialae77fce02016-09-04 00:18:56 +0000157
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158 struct proc_threadinfo m_proc_threadinfo;
Todd Fialae77fce02016-09-04 00:18:56 +0000159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 thread_identifier_info_data_t m_ident_info;
Todd Fialae77fce02016-09-04 00:18:56 +0000161
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
173typedef std::shared_ptr<NativeThreadDarwin> NativeThreadDarwinSP;
174
175} // namespace process_darwin
176} // namespace lldb_private
177
178#endif // #ifndef NativeThreadDarwin_H