blob: 0b8e760b9a252d8e1ab487f20220f4acab485568 [file] [log] [blame]
Todd Fialaaf245d12014-06-30 21:05:18 +00001//===-- NativeProcessLinux.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 liblldb_NativeProcessLinux_H_
11#define liblldb_NativeProcessLinux_H_
12
13// C Includes
14#include <semaphore.h>
15#include <signal.h>
16
17// C++ Includes
18#include <unordered_set>
19
20// Other libraries and framework includes
21#include "lldb/Core/ArchSpec.h"
22#include "lldb/lldb-types.h"
23#include "lldb/Host/Debug.h"
Zachary Turner39de3112014-09-09 20:54:56 +000024#include "lldb/Host/HostThread.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000025#include "lldb/Host/Mutex.h"
26#include "lldb/Target/MemoryRegionInfo.h"
27
Chaoren Lin2fe1d0a2015-02-03 01:51:38 +000028#include "lldb/Host/common/NativeProcessProtocol.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000029
30namespace lldb_private
31{
32 class Error;
33 class Module;
Chaoren Linfa03ad22015-02-03 01:50:42 +000034 class ThreadStateCoordinator;
Todd Fialaaf245d12014-06-30 21:05:18 +000035 class Scalar;
36
37 /// @class NativeProcessLinux
38 /// @brief Manages communication with the inferior (debugee) process.
39 ///
40 /// Upon construction, this class prepares and launches an inferior process for
41 /// debugging.
42 ///
43 /// Changes in the inferior process state are broadcasted.
44 class NativeProcessLinux: public NativeProcessProtocol
45 {
46 public:
47
48 // ---------------------------------------------------------------------
49 // Public Static Methods
50 // ---------------------------------------------------------------------
51 static lldb_private::Error
52 LaunchProcess (
53 Module *exe_module,
54 ProcessLaunchInfo &launch_info,
55 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
56 NativeProcessProtocolSP &native_process_sp);
57
58 static lldb_private::Error
59 AttachToProcess (
60 lldb::pid_t pid,
61 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
62 NativeProcessProtocolSP &native_process_sp);
63
64 // ---------------------------------------------------------------------
65 // Public Instance Methods
66 // ---------------------------------------------------------------------
67
68 ~NativeProcessLinux() override;
69
70 // ---------------------------------------------------------------------
71 // NativeProcessProtocol Interface
72 // ---------------------------------------------------------------------
73 Error
74 Resume (const ResumeActionList &resume_actions) override;
75
76 Error
77 Halt () override;
78
79 Error
80 Detach () override;
81
82 Error
83 Signal (int signo) override;
84
85 Error
Chaoren Line9547b82015-02-03 01:51:00 +000086 Interrupt () override;
87
88 Error
Todd Fialaaf245d12014-06-30 21:05:18 +000089 Kill () override;
90
91 Error
92 GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override;
93
94 Error
95 ReadMemory (lldb::addr_t addr, void *buf, lldb::addr_t size, lldb::addr_t &bytes_read) override;
96
97 Error
98 WriteMemory (lldb::addr_t addr, const void *buf, lldb::addr_t size, lldb::addr_t &bytes_written) override;
99
100 Error
101 AllocateMemory (lldb::addr_t size, uint32_t permissions, lldb::addr_t &addr) override;
102
103 Error
104 DeallocateMemory (lldb::addr_t addr) override;
105
106 lldb::addr_t
107 GetSharedLibraryInfoAddress () override;
108
109 size_t
110 UpdateThreads () override;
111
112 bool
113 GetArchitecture (ArchSpec &arch) const override;
114
115 Error
116 SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override;
117
118 void
119 DoStopIDBumped (uint32_t newBumpId) override;
120
121 // ---------------------------------------------------------------------
122 // Interface used by NativeRegisterContext-derived classes.
123 // ---------------------------------------------------------------------
124
125 /// Reads the contents from the register identified by the given (architecture
126 /// dependent) offset.
127 ///
128 /// This method is provided for use by RegisterContextLinux derivatives.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000129 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000130 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
131 unsigned size, lldb_private::RegisterValue &value);
132
133 /// Writes the given value to the register identified by the given
134 /// (architecture dependent) offset.
135 ///
136 /// This method is provided for use by RegisterContextLinux derivatives.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000137 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000138 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
139 const lldb_private::RegisterValue &value);
140
141 /// Reads all general purpose registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000142 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000143 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
144
145 /// Reads generic floating point registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000146 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000147 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
148
149 /// Reads the specified register set into the specified buffer.
150 /// For instance, the extended floating-point register set.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000151 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000152 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
153
154 /// Writes all general purpose registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000155 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000156 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
157
158 /// Writes generic floating point registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000159 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000160 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
161
162 /// Writes the specified register set into the specified buffer.
163 /// For instance, the extended floating-point register set.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000164 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000165 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
166
167 protected:
168 // ---------------------------------------------------------------------
169 // NativeProcessProtocol protected interface
170 // ---------------------------------------------------------------------
171 Error
172 GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override;
173
174 private:
175
176 lldb_private::ArchSpec m_arch;
177
Zachary Turner39de3112014-09-09 20:54:56 +0000178 HostThread m_operation_thread;
179 HostThread m_monitor_thread;
Todd Fialaaf245d12014-06-30 21:05:18 +0000180
181 // current operation which must be executed on the priviliged thread
182 void *m_operation;
183 lldb_private::Mutex m_operation_mutex;
184
185 // semaphores notified when Operation is ready to be processed and when
186 // the operation is complete.
187 sem_t m_operation_pending;
188 sem_t m_operation_done;
189
Todd Fialaaf245d12014-06-30 21:05:18 +0000190 lldb_private::LazyBool m_supports_mem_region;
191 std::vector<MemoryRegionInfo> m_mem_region_cache;
192 lldb_private::Mutex m_mem_region_cache_mutex;
193
Chaoren Linfa03ad22015-02-03 01:50:42 +0000194 std::unique_ptr<ThreadStateCoordinator> m_coordinator_up;
195 HostThread m_coordinator_thread;
Todd Fialaaf245d12014-06-30 21:05:18 +0000196
197 struct OperationArgs
198 {
199 OperationArgs(NativeProcessLinux *monitor);
200
201 ~OperationArgs();
202
203 NativeProcessLinux *m_monitor; // The monitor performing the attach.
204 sem_t m_semaphore; // Posted to once operation complete.
205 lldb_private::Error m_error; // Set if process operation failed.
206 };
207
208 /// @class LauchArgs
209 ///
210 /// @brief Simple structure to pass data to the thread responsible for
211 /// launching a child process.
212 struct LaunchArgs : OperationArgs
213 {
214 LaunchArgs(NativeProcessLinux *monitor,
215 lldb_private::Module *module,
216 char const **argv,
217 char const **envp,
Todd Fiala75f47c32014-10-11 21:42:09 +0000218 const std::string &stdin_path,
219 const std::string &stdout_path,
220 const std::string &stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000221 const char *working_dir,
222 const lldb_private::ProcessLaunchInfo &launch_info);
Todd Fialaaf245d12014-06-30 21:05:18 +0000223
224 ~LaunchArgs();
225
226 lldb_private::Module *m_module; // The executable image to launch.
227 char const **m_argv; // Process arguments.
228 char const **m_envp; // Process environment.
Todd Fiala75f47c32014-10-11 21:42:09 +0000229 const std::string &m_stdin_path; // Redirect stdin if not empty.
230 const std::string &m_stdout_path; // Redirect stdout if not empty.
231 const std::string &m_stderr_path; // Redirect stderr if not empty.
Todd Fialaaf245d12014-06-30 21:05:18 +0000232 const char *m_working_dir; // Working directory or NULL.
Todd Fiala0bce1b62014-08-17 00:10:50 +0000233 const lldb_private::ProcessLaunchInfo &m_launch_info;
Todd Fialaaf245d12014-06-30 21:05:18 +0000234 };
235
236 struct AttachArgs : OperationArgs
237 {
238 AttachArgs(NativeProcessLinux *monitor,
239 lldb::pid_t pid);
240
241 ~AttachArgs();
242
243 lldb::pid_t m_pid; // pid of the process to be attached.
244 };
245
246 // ---------------------------------------------------------------------
247 // Private Instance Methods
248 // ---------------------------------------------------------------------
249 NativeProcessLinux ();
250
251 /// Launches an inferior process ready for debugging. Forms the
252 /// implementation of Process::DoLaunch.
253 void
254 LaunchInferior (
255 Module *module,
256 char const *argv[],
257 char const *envp[],
Todd Fiala75f47c32014-10-11 21:42:09 +0000258 const std::string &stdin_path,
259 const std::string &stdout_path,
260 const std::string &stderr_path,
Todd Fialaaf245d12014-06-30 21:05:18 +0000261 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000262 const lldb_private::ProcessLaunchInfo &launch_info,
Todd Fialaaf245d12014-06-30 21:05:18 +0000263 Error &error);
264
265 /// Attaches to an existing process. Forms the
266 /// implementation of Process::DoLaunch.
267 void
268 AttachToInferior (lldb::pid_t pid, Error &error);
269
270 void
271 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
272
273 static void *
274 LaunchOpThread(void *arg);
275
276 static bool
277 Launch(LaunchArgs *args);
278
279 void
280 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
281
282 static void *
283 AttachOpThread(void *args);
284
285 static bool
286 Attach(AttachArgs *args);
287
Chaoren Lin97ccc292015-02-03 01:51:12 +0000288 static Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000289 SetDefaultPtraceOpts(const lldb::pid_t);
290
291 static void
292 ServeOperation(OperationArgs *args);
293
294 static bool
295 DupDescriptor(const char *path, int fd, int flags);
296
297 static bool
298 MonitorCallback(void *callback_baton,
299 lldb::pid_t pid, bool exited, int signal, int status);
300
301 void
302 MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid);
303
304 void
305 MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited);
306
307#if 0
308 static ::ProcessMessage::CrashReason
309 GetCrashReasonForSIGSEGV(const siginfo_t *info);
310
311 static ::ProcessMessage::CrashReason
312 GetCrashReasonForSIGILL(const siginfo_t *info);
313
314 static ::ProcessMessage::CrashReason
315 GetCrashReasonForSIGFPE(const siginfo_t *info);
316
317 static ::ProcessMessage::CrashReason
318 GetCrashReasonForSIGBUS(const siginfo_t *info);
319#endif
320
321 void
322 DoOperation(void *op);
323
324 /// Stops the child monitor thread.
325 void
326 StopMonitoringChildProcess();
327
328 /// Stops the operation thread used to attach/launch a process.
329 void
330 StopOpThread();
331
Chaoren Linfa03ad22015-02-03 01:50:42 +0000332 Error
333 StartCoordinatorThread ();
334
335 static void*
336 CoordinatorThread (void *arg);
337
338 void
339 StopCoordinatorThread ();
340
Todd Fialaaf245d12014-06-30 21:05:18 +0000341 /// Stops monitoring the child process thread.
342 void
343 StopMonitor();
344
345 bool
346 HasThreadNoLock (lldb::tid_t thread_id);
347
348 NativeThreadProtocolSP
349 MaybeGetThreadNoLock (lldb::tid_t thread_id);
350
351 bool
352 StopTrackingThread (lldb::tid_t thread_id);
353
354 NativeThreadProtocolSP
355 AddThread (lldb::tid_t thread_id);
356
357 NativeThreadProtocolSP
358 GetOrCreateThread (lldb::tid_t thread_id, bool &created);
359
360 Error
361 GetSoftwareBreakpointSize (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size);
362
363 Error
364 FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp);
365
366 /// Writes a siginfo_t structure corresponding to the given thread ID to the
367 /// memory region pointed to by @p siginfo.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000368 Error
369 GetSignalInfo(lldb::tid_t tid, void *siginfo);
Todd Fialaaf245d12014-06-30 21:05:18 +0000370
371 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
372 /// corresponding to the given thread ID to the memory pointed to by @p
373 /// message.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000374 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000375 GetEventMessage(lldb::tid_t tid, unsigned long *message);
376
377 /// Resumes the given thread. If @p signo is anything but
378 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000379 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000380 Resume(lldb::tid_t tid, uint32_t signo);
381
382 /// Single steps the given thread. If @p signo is anything but
383 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000384 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000385 SingleStep(lldb::tid_t tid, uint32_t signo);
386
Chaoren Linfa03ad22015-02-03 01:50:42 +0000387 // ThreadStateCoordinator helper methods.
Todd Fiala511e5cd2014-09-11 23:29:14 +0000388 void
Chaoren Linfa03ad22015-02-03 01:50:42 +0000389 NotifyThreadCreateStopped (lldb::tid_t tid);
Todd Fiala511e5cd2014-09-11 23:29:14 +0000390
391 void
Chaoren Linfa03ad22015-02-03 01:50:42 +0000392 NotifyThreadCreateRunning (lldb::tid_t tid);
393
394 void
395 NotifyThreadDeath (lldb::tid_t tid);
396
397 void
398 NotifyThreadStop (lldb::tid_t tid);
399
400 void
401 CallAfterRunningThreadsStop (lldb::tid_t tid,
402 const std::function<void (lldb::tid_t tid)> &call_after_function);
Todd Fiala511e5cd2014-09-11 23:29:14 +0000403
Chaoren Lin03f12d62015-02-03 01:50:49 +0000404 void
405 CallAfterRunningThreadsStopWithSkipTID (lldb::tid_t deferred_signal_tid,
406 lldb::tid_t skip_stop_request_tid,
407 const std::function<void (lldb::tid_t tid)> &call_after_function);
408
Todd Fialaaf245d12014-06-30 21:05:18 +0000409 lldb_private::Error
410 Detach(lldb::tid_t tid);
Chaoren Lin86fd8e42015-02-03 01:51:15 +0000411
412 lldb_private::Error
413 RequestThreadStop (const lldb::pid_t pid, const lldb::tid_t tid);
Todd Fialaaf245d12014-06-30 21:05:18 +0000414 };
415} // End lldb_private namespace.
416
417#endif // #ifndef liblldb_NativeProcessLinux_H_