1//===-- NativeThreadDarwin.h ---------------------------------- -*- C++ -*-===//
2//
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
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
23#include "lldb/Host/common/NativeThreadProtocol.h"
24#include "lldb/lldb-private-forward.h"
25
26#include "MachException.h"
27
28namespace lldb_private {
29namespace process_darwin {
30
31class NativeProcessDarwin;
32using NativeProcessDarwinSP = std::shared_ptr<NativeProcessDarwin>;
33
34class NativeThreadListDarwin;
35
36class NativeThreadDarwin : public NativeThreadProtocol {
37  friend class NativeProcessDarwin;
38  friend class NativeThreadListDarwin;
39
40public:
41  static uint64_t
42  GetGloballyUniqueThreadIDForMachPortID(::thread_t mach_port_id);
43
44  NativeThreadDarwin(NativeProcessDarwin *process, bool is_64_bit,
45                     lldb::tid_t unique_thread_id = 0,
46                     ::thread_t mach_thread_port = 0);
47
48  // NativeThreadProtocol Interface
49  std::string GetName() override;
50
51  lldb::StateType GetState() override;
52
53  bool GetStopReason(ThreadStopInfo &stop_info,
54                     std::string &description) override;
55
56  NativeRegisterContextSP GetRegisterContext() override;
57
58  Status SetWatchpoint(lldb::addr_t addr, size_t size, uint32_t watch_flags,
59                       bool hardware) override;
60
61  Status RemoveWatchpoint(lldb::addr_t addr) override;
62
63  // New methods that are fine for others to call.
64  void Dump(Stream &stream) const;
65
66private:
67  // Interface for friend classes
68
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);
72
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);
76
77  bool NotifyException(MachException::Data &exc);
78
79  bool ShouldStop(bool &step_more) const;
80
81  void ThreadDidStop();
82
83  void SetStoppedBySignal(uint32_t signo, const siginfo_t *info = nullptr);
84
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);
89
90  const struct thread_basic_info *GetBasicInfo() const;
91
92  static bool GetBasicInfo(::thread_t thread,
93                           struct thread_basic_info *basicInfoPtr);
94
95  bool IsUserReady() const;
96
97  void SetStoppedByExec();
98
99  void SetStoppedByBreakpoint();
100
101  void SetStoppedByWatchpoint(uint32_t wp_index);
102
103  bool IsStoppedAtBreakpoint();
104
105  bool IsStoppedAtWatchpoint();
106
107  void SetStoppedByTrace();
108
109  void SetStoppedWithNoReason();
110
111  void SetExited();
112
113  Status RequestStop();
114
115  /// Return the mach thread port number for this thread.
116  ///
117  /// \return
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; }
121
122  static bool MachPortNumberIsValid(::thread_t thread);
123
124  // Private interface
125  bool GetIdentifierInfo();
126
127  void MaybeLogStateChange(lldb::StateType new_state);
128
129  NativeProcessDarwinSP GetNativeProcessDarwinSP();
130
131  void SetStopped();
132
133  inline void MaybePrepareSingleStepWorkaround();
134
135  inline void MaybeCleanupSingleStepWorkaround();
136
137  // Member Variables
138
139  // The mach thread port for the thread.
140  ::thread_t m_mach_thread_port;
141
142  // The most recently-retrieved thread basic info.
143  mutable ::thread_basic_info m_basic_info;
144
145  struct proc_threadinfo m_proc_threadinfo;
146
147  thread_identifier_info_data_t m_ident_info;
148
149#if 0
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.
157#endif
158};
159
160typedef std::shared_ptr<NativeThreadDarwin> NativeThreadDarwinSP;
161
162} // namespace process_darwin
163} // namespace lldb_private
164
165#endif // #ifndef NativeThreadDarwin_H
166