summaryrefslogtreecommitdiff
path: root/source/Plugins/Process/Darwin/NativeProcessDarwin.h
blob: 9abdd5360eba5be32b366833e9a091b036dca17a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//===-- NativeProcessDarwin.h --------------------------------- -*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef NativeProcessDarwin_h
#define NativeProcessDarwin_h

// NOTE: this code should only be compiled on Apple Darwin systems.  It is
// not cross-platform code and is not intended to build on any other platform.
// Therefore, platform-specific headers and code are okay here.

// C includes
#include <mach/mach_types.h>

// C++ includes
#include <mutex>
#include <unordered_set>

#include "lldb/Host/Debug.h"
#include "lldb/Host/HostThread.h"
#include "lldb/Host/Pipe.h"
#include "lldb/Host/common/NativeProcessProtocol.h"
#include "lldb/Target/MemoryRegionInfo.h"
#include "lldb/Utility/ArchSpec.h"
#include "lldb/Utility/FileSpec.h"
#include "lldb/lldb-types.h"

#include "LaunchFlavor.h"
#include "MachException.h"
#include "NativeThreadDarwin.h"
#include "NativeThreadListDarwin.h"

namespace lldb_private {
class Status;
class Scalar;

namespace process_darwin {

/// @class NativeProcessDarwin
/// Manages communication with the inferior (debugee) process.
///
/// Upon construction, this class prepares and launches an inferior process
/// for debugging.
///
/// Changes in the inferior process state are broadcasted.
class NativeProcessDarwin : public NativeProcessProtocol {
  friend Status NativeProcessProtocol::Launch(
      ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate,
      MainLoop &mainloop, NativeProcessProtocolSP &process_sp);

  friend Status NativeProcessProtocol::Attach(
      lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate,
      MainLoop &mainloop, NativeProcessProtocolSP &process_sp);

public:
  ~NativeProcessDarwin() override;

  // -----------------------------------------------------------------
  // NativeProcessProtocol Interface
  // -----------------------------------------------------------------
  Status Resume(const ResumeActionList &resume_actions) override;

  Status Halt() override;

  Status Detach() override;

  Status Signal(int signo) override;

  Status Interrupt() override;

  Status Kill() override;

  Status GetMemoryRegionInfo(lldb::addr_t load_addr,
                             MemoryRegionInfo &range_info) override;

  Status ReadMemory(lldb::addr_t addr, void *buf, size_t size,
                    size_t &bytes_read) override;

  Status ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size,
                               size_t &bytes_read) override;

  Status WriteMemory(lldb::addr_t addr, const void *buf, size_t size,
                     size_t &bytes_written) override;

  Status AllocateMemory(size_t size, uint32_t permissions,
                        lldb::addr_t &addr) override;

  Status DeallocateMemory(lldb::addr_t addr) override;

  lldb::addr_t GetSharedLibraryInfoAddress() override;

  size_t UpdateThreads() override;

  bool GetArchitecture(ArchSpec &arch) const override;

  Status SetBreakpoint(lldb::addr_t addr, uint32_t size,
                       bool hardware) override;

  void DoStopIDBumped(uint32_t newBumpId) override;

  Status GetLoadedModuleFileSpec(const char *module_path,
                                 FileSpec &file_spec) override;

  Status GetFileLoadAddress(const llvm::StringRef &file_name,
                            lldb::addr_t &load_addr) override;

  NativeThreadDarwinSP GetThreadByID(lldb::tid_t id);

  task_t GetTask() const { return m_task; }

  // -----------------------------------------------------------------
  // Interface used by NativeRegisterContext-derived classes.
  // -----------------------------------------------------------------
  static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr = nullptr,
                              void *data = nullptr, size_t data_size = 0,
                              long *result = nullptr);

  bool SupportHardwareSingleStepping() const;

protected:
  // -----------------------------------------------------------------
  // NativeProcessProtocol protected interface
  // -----------------------------------------------------------------
  Status
  GetSoftwareBreakpointTrapOpcode(size_t trap_opcode_size_hint,
                                  size_t &actual_opcode_size,
                                  const uint8_t *&trap_opcode_bytes) override;

private:
  // -----------------------------------------------------------------
  /// Mach task-related Member Variables
  // -----------------------------------------------------------------

  // The task port for the inferior process.
  mutable task_t m_task;

  // True if the inferior process did an exec since we started
  // monitoring it.
  bool m_did_exec;

  // The CPU type of this process.
  mutable cpu_type_t m_cpu_type;

  // -----------------------------------------------------------------
  /// Exception/Signal Handling Member Variables
  // -----------------------------------------------------------------

  // Exception port on which we will receive child exceptions
  mach_port_t m_exception_port;

  // Saved state of the child exception port prior to us installing
  // our own intercepting port.
  MachException::PortInfo m_exc_port_info;

  // The thread that runs the Mach exception read and reply handler.
  pthread_t m_exception_thread;

  // TODO see if we can remove this if we get the exception collection
  // and distribution to happen in a single-threaded fashion.
  std::recursive_mutex m_exception_messages_mutex;

  // A collection of exception messages caught when listening to the
  // exception port.
  MachException::Message::collection m_exception_messages;

  // When we call MachProcess::Interrupt(), we want to send this
  // signal (if non-zero).
  int m_sent_interrupt_signo;

  // If we resume the process and still haven't received our
  // interrupt signal (if this is non-zero).
  int m_auto_resume_signo;

  // -----------------------------------------------------------------
  /// Thread-related Member Variables
  // -----------------------------------------------------------------
  NativeThreadListDarwin m_thread_list;
  ResumeActionList m_thread_actions;

  // -----------------------------------------------------------------
  /// Process Lifetime Member Variable
  // -----------------------------------------------------------------

  // The pipe over which the waitpid thread and the main loop will
  // communicate.
  Pipe m_waitpid_pipe;

  // The thread that runs the waitpid handler.
  pthread_t m_waitpid_thread;

  // waitpid reader callback handle.
  MainLoop::ReadHandleUP m_waitpid_reader_handle;

  // -----------------------------------------------------------------
  // Private Instance Methods
  // -----------------------------------------------------------------
  NativeProcessDarwin(lldb::pid_t pid, int pty_master_fd);

  // -----------------------------------------------------------------
  /// Finalize the launch.
  ///
  /// This method associates the NativeProcessDarwin instance with the host
  /// process that was just launched.  It peforms actions like attaching a
  /// listener to the inferior exception port, ptracing the process, and the
  /// like.
  ///
  /// @param[in] launch_flavor
  ///     The launch flavor that was used to launch the process.
  ///
  /// @param[in] main_loop
  ///     The main loop that will run the process monitor.  Work
  ///     that needs to be done (e.g. reading files) gets registered
  ///     here along with callbacks to process the work.
  ///
  /// @return
  ///     Any error that occurred during the aforementioned
  ///     operations.  Failure here will force termination of the
  ///     launched process and debugging session.
  // -----------------------------------------------------------------
  Status FinalizeLaunch(LaunchFlavor launch_flavor, MainLoop &main_loop);

  Status SaveExceptionPortInfo();

  void ExceptionMessageReceived(const MachException::Message &message);

  void MaybeRaiseThreadPriority();

  Status StartExceptionThread();

  Status SendInferiorExitStatusToMainLoop(::pid_t pid, int status);

  Status HandleWaitpidResult();

  bool ProcessUsingSpringBoard() const;

  bool ProcessUsingBackBoard() const;

  static void *ExceptionThread(void *arg);

  void *DoExceptionThread();

  lldb::addr_t GetDYLDAllImageInfosAddress(Status &error) const;

  static uint32_t GetCPUTypeForLocalProcess(::pid_t pid);

  uint32_t GetCPUType() const;

  task_t ExceptionMessageBundleComplete();

  void StartSTDIOThread();

  Status StartWaitpidThread(MainLoop &main_loop);

  static void *WaitpidThread(void *arg);

  void *DoWaitpidThread();

  task_t TaskPortForProcessID(Status &error, bool force = false) const;

  /// Attaches to an existing process.  Forms the implementation of
  /// Process::DoAttach.
  void AttachToInferior(MainLoop &mainloop, lldb::pid_t pid, Status &error);

  ::pid_t Attach(lldb::pid_t pid, Status &error);

  Status PrivateResume();

  Status ReplyToAllExceptions();

  Status ResumeTask();

  bool IsTaskValid() const;

  bool IsTaskValid(task_t task) const;

  mach_port_t GetExceptionPort() const;

  bool IsExceptionPortValid() const;

  Status GetTaskBasicInfo(task_t task, struct task_basic_info *info) const;

  Status SuspendTask();

  static Status SetDefaultPtraceOpts(const lldb::pid_t);

  static void *MonitorThread(void *baton);

  void MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status);

  void WaitForNewThread(::pid_t tid);

  void MonitorSIGTRAP(const siginfo_t &info, NativeThreadDarwin &thread);

  void MonitorTrace(NativeThreadDarwin &thread);

  void MonitorBreakpoint(NativeThreadDarwin &thread);

  void MonitorWatchpoint(NativeThreadDarwin &thread, uint32_t wp_index);

  void MonitorSignal(const siginfo_t &info, NativeThreadDarwin &thread,
                     bool exited);

  Status SetupSoftwareSingleStepping(NativeThreadDarwin &thread);

  bool HasThreadNoLock(lldb::tid_t thread_id);

  bool StopTrackingThread(lldb::tid_t thread_id);

  NativeThreadDarwinSP AddThread(lldb::tid_t thread_id);

  Status GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size);

  Status FixupBreakpointPCAsNeeded(NativeThreadDarwin &thread);

  /// Writes a siginfo_t structure corresponding to the given thread
  /// ID to the memory region pointed to by @p siginfo.
  Status GetSignalInfo(lldb::tid_t tid, void *siginfo);

  /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
  /// corresponding to the given thread ID to the memory pointed to by @p
  /// message.
  Status GetEventMessage(lldb::tid_t tid, unsigned long *message);

  void NotifyThreadDeath(lldb::tid_t tid);

  Status Detach(lldb::tid_t tid);

  // This method is requests a stop on all threads which are still
  // running. It sets up a deferred delegate notification, which will
  // fire once threads report as stopped. The triggerring_tid will be
  // set as the current thread (main stop reason).
  void StopRunningThreads(lldb::tid_t triggering_tid);

  // Notify the delegate if all threads have stopped.
  void SignalIfAllThreadsStopped();

  // Resume the given thread, optionally passing it the given signal.
  // The type of resume operation (continue, single-step) depends on
  // the state parameter.
  Status ResumeThread(NativeThreadDarwin &thread, lldb::StateType state,
                      int signo);

  void ThreadWasCreated(NativeThreadDarwin &thread);

  void SigchldHandler();
};

} // namespace process_darwin
} // namespace lldb_private

#endif /* NativeProcessDarwin_h */