blob: b9aff86c984ddb6e11f469e867cd4255814b240b [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
Todd Fialaaf245d12014-06-30 21:05:18 +000048 static lldb_private::Error
49 LaunchProcess (
50 Module *exe_module,
51 ProcessLaunchInfo &launch_info,
52 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
53 NativeProcessProtocolSP &native_process_sp);
54
55 static lldb_private::Error
56 AttachToProcess (
57 lldb::pid_t pid,
58 lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate,
59 NativeProcessProtocolSP &native_process_sp);
60
61 // ---------------------------------------------------------------------
Todd Fialaaf245d12014-06-30 21:05:18 +000062 // NativeProcessProtocol Interface
63 // ---------------------------------------------------------------------
64 Error
65 Resume (const ResumeActionList &resume_actions) override;
66
67 Error
68 Halt () override;
69
70 Error
71 Detach () override;
72
73 Error
74 Signal (int signo) override;
75
76 Error
Chaoren Line9547b82015-02-03 01:51:00 +000077 Interrupt () override;
78
79 Error
Todd Fialaaf245d12014-06-30 21:05:18 +000080 Kill () override;
81
82 Error
83 GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override;
84
85 Error
86 ReadMemory (lldb::addr_t addr, void *buf, lldb::addr_t size, lldb::addr_t &bytes_read) override;
87
88 Error
89 WriteMemory (lldb::addr_t addr, const void *buf, lldb::addr_t size, lldb::addr_t &bytes_written) override;
90
91 Error
92 AllocateMemory (lldb::addr_t size, uint32_t permissions, lldb::addr_t &addr) override;
93
94 Error
95 DeallocateMemory (lldb::addr_t addr) override;
96
97 lldb::addr_t
98 GetSharedLibraryInfoAddress () override;
99
100 size_t
101 UpdateThreads () override;
102
103 bool
104 GetArchitecture (ArchSpec &arch) const override;
105
106 Error
107 SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override;
108
109 void
110 DoStopIDBumped (uint32_t newBumpId) override;
111
Oleksiy Vyalov8bc34f42015-02-19 17:58:04 +0000112 void
113 Terminate () override;
114
Todd Fialaaf245d12014-06-30 21:05:18 +0000115 // ---------------------------------------------------------------------
116 // Interface used by NativeRegisterContext-derived classes.
117 // ---------------------------------------------------------------------
118
119 /// Reads the contents from the register identified by the given (architecture
120 /// dependent) offset.
121 ///
122 /// This method is provided for use by RegisterContextLinux derivatives.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000123 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000124 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
125 unsigned size, lldb_private::RegisterValue &value);
126
127 /// Writes the given value to the register identified by the given
128 /// (architecture dependent) offset.
129 ///
130 /// This method is provided for use by RegisterContextLinux derivatives.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000131 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000132 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
133 const lldb_private::RegisterValue &value);
134
135 /// Reads all general purpose registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000136 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000137 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
138
139 /// Reads generic floating point registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000140 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000141 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
142
143 /// Reads the specified register set into the specified buffer.
144 /// For instance, the extended floating-point register set.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000145 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000146 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
147
148 /// Writes all general purpose registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000149 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000150 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
151
152 /// Writes generic floating point registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000153 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000154 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
155
156 /// Writes the specified register set into the specified buffer.
157 /// For instance, the extended floating-point register set.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000158 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000159 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +0000160
161 Error
162 GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) override;
163
Todd Fialaaf245d12014-06-30 21:05:18 +0000164 protected:
165 // ---------------------------------------------------------------------
166 // NativeProcessProtocol protected interface
167 // ---------------------------------------------------------------------
168 Error
169 GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override;
170
171 private:
172
173 lldb_private::ArchSpec m_arch;
174
Zachary Turner39de3112014-09-09 20:54:56 +0000175 HostThread m_operation_thread;
176 HostThread m_monitor_thread;
Todd Fialaaf245d12014-06-30 21:05:18 +0000177
178 // current operation which must be executed on the priviliged thread
179 void *m_operation;
180 lldb_private::Mutex m_operation_mutex;
181
182 // semaphores notified when Operation is ready to be processed and when
183 // the operation is complete.
184 sem_t m_operation_pending;
185 sem_t m_operation_done;
186
Todd Fialaaf245d12014-06-30 21:05:18 +0000187 lldb_private::LazyBool m_supports_mem_region;
188 std::vector<MemoryRegionInfo> m_mem_region_cache;
189 lldb_private::Mutex m_mem_region_cache_mutex;
190
Chaoren Linfa03ad22015-02-03 01:50:42 +0000191 std::unique_ptr<ThreadStateCoordinator> m_coordinator_up;
192 HostThread m_coordinator_thread;
Todd Fialaaf245d12014-06-30 21:05:18 +0000193
194 struct OperationArgs
195 {
196 OperationArgs(NativeProcessLinux *monitor);
197
198 ~OperationArgs();
199
200 NativeProcessLinux *m_monitor; // The monitor performing the attach.
201 sem_t m_semaphore; // Posted to once operation complete.
202 lldb_private::Error m_error; // Set if process operation failed.
203 };
204
205 /// @class LauchArgs
206 ///
207 /// @brief Simple structure to pass data to the thread responsible for
208 /// launching a child process.
209 struct LaunchArgs : OperationArgs
210 {
211 LaunchArgs(NativeProcessLinux *monitor,
212 lldb_private::Module *module,
213 char const **argv,
214 char const **envp,
Todd Fiala75f47c32014-10-11 21:42:09 +0000215 const std::string &stdin_path,
216 const std::string &stdout_path,
217 const std::string &stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000218 const char *working_dir,
219 const lldb_private::ProcessLaunchInfo &launch_info);
Todd Fialaaf245d12014-06-30 21:05:18 +0000220
221 ~LaunchArgs();
222
223 lldb_private::Module *m_module; // The executable image to launch.
224 char const **m_argv; // Process arguments.
225 char const **m_envp; // Process environment.
Todd Fiala75f47c32014-10-11 21:42:09 +0000226 const std::string &m_stdin_path; // Redirect stdin if not empty.
227 const std::string &m_stdout_path; // Redirect stdout if not empty.
228 const std::string &m_stderr_path; // Redirect stderr if not empty.
Todd Fialaaf245d12014-06-30 21:05:18 +0000229 const char *m_working_dir; // Working directory or NULL.
Todd Fiala0bce1b62014-08-17 00:10:50 +0000230 const lldb_private::ProcessLaunchInfo &m_launch_info;
Todd Fialaaf245d12014-06-30 21:05:18 +0000231 };
232
233 struct AttachArgs : OperationArgs
234 {
235 AttachArgs(NativeProcessLinux *monitor,
236 lldb::pid_t pid);
237
238 ~AttachArgs();
239
240 lldb::pid_t m_pid; // pid of the process to be attached.
241 };
242
243 // ---------------------------------------------------------------------
244 // Private Instance Methods
245 // ---------------------------------------------------------------------
246 NativeProcessLinux ();
247
248 /// Launches an inferior process ready for debugging. Forms the
249 /// implementation of Process::DoLaunch.
250 void
251 LaunchInferior (
252 Module *module,
253 char const *argv[],
254 char const *envp[],
Todd Fiala75f47c32014-10-11 21:42:09 +0000255 const std::string &stdin_path,
256 const std::string &stdout_path,
257 const std::string &stderr_path,
Todd Fialaaf245d12014-06-30 21:05:18 +0000258 const char *working_dir,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000259 const lldb_private::ProcessLaunchInfo &launch_info,
Todd Fialaaf245d12014-06-30 21:05:18 +0000260 Error &error);
261
262 /// Attaches to an existing process. Forms the
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +0000263 /// implementation of Process::DoAttach
Todd Fialaaf245d12014-06-30 21:05:18 +0000264 void
265 AttachToInferior (lldb::pid_t pid, Error &error);
266
267 void
268 StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error);
269
270 static void *
271 LaunchOpThread(void *arg);
272
273 static bool
274 Launch(LaunchArgs *args);
275
276 void
277 StartAttachOpThread(AttachArgs *args, lldb_private::Error &error);
278
279 static void *
280 AttachOpThread(void *args);
281
282 static bool
283 Attach(AttachArgs *args);
284
Chaoren Lin97ccc292015-02-03 01:51:12 +0000285 static Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000286 SetDefaultPtraceOpts(const lldb::pid_t);
287
288 static void
289 ServeOperation(OperationArgs *args);
290
291 static bool
292 DupDescriptor(const char *path, int fd, int flags);
293
294 static bool
295 MonitorCallback(void *callback_baton,
296 lldb::pid_t pid, bool exited, int signal, int status);
297
298 void
299 MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid);
300
301 void
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000302 MonitorTrace(lldb::pid_t pid, NativeThreadProtocolSP thread_sp);
303
304 void
305 MonitorBreakpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp);
306
307 void
308 MonitorWatchpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp, uint32_t wp_index);
309
310 void
Todd Fialaaf245d12014-06-30 21:05:18 +0000311 MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited);
312
313#if 0
314 static ::ProcessMessage::CrashReason
315 GetCrashReasonForSIGSEGV(const siginfo_t *info);
316
317 static ::ProcessMessage::CrashReason
318 GetCrashReasonForSIGILL(const siginfo_t *info);
319
320 static ::ProcessMessage::CrashReason
321 GetCrashReasonForSIGFPE(const siginfo_t *info);
322
323 static ::ProcessMessage::CrashReason
324 GetCrashReasonForSIGBUS(const siginfo_t *info);
325#endif
326
327 void
328 DoOperation(void *op);
329
330 /// Stops the child monitor thread.
331 void
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +0000332 StopMonitorThread();
Todd Fialaaf245d12014-06-30 21:05:18 +0000333
334 /// Stops the operation thread used to attach/launch a process.
335 void
336 StopOpThread();
337
Chaoren Linfa03ad22015-02-03 01:50:42 +0000338 Error
339 StartCoordinatorThread ();
340
341 static void*
342 CoordinatorThread (void *arg);
343
344 void
345 StopCoordinatorThread ();
346
Todd Fialaaf245d12014-06-30 21:05:18 +0000347 /// Stops monitoring the child process thread.
348 void
349 StopMonitor();
350
351 bool
352 HasThreadNoLock (lldb::tid_t thread_id);
353
354 NativeThreadProtocolSP
355 MaybeGetThreadNoLock (lldb::tid_t thread_id);
356
357 bool
358 StopTrackingThread (lldb::tid_t thread_id);
359
360 NativeThreadProtocolSP
361 AddThread (lldb::tid_t thread_id);
362
363 NativeThreadProtocolSP
364 GetOrCreateThread (lldb::tid_t thread_id, bool &created);
365
366 Error
367 GetSoftwareBreakpointSize (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size);
368
369 Error
370 FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp);
371
372 /// Writes a siginfo_t structure corresponding to the given thread ID to the
373 /// memory region pointed to by @p siginfo.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000374 Error
375 GetSignalInfo(lldb::tid_t tid, void *siginfo);
Todd Fialaaf245d12014-06-30 21:05:18 +0000376
377 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
378 /// corresponding to the given thread ID to the memory pointed to by @p
379 /// message.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000380 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000381 GetEventMessage(lldb::tid_t tid, unsigned long *message);
382
383 /// Resumes the given thread. If @p signo is anything but
384 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000385 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000386 Resume(lldb::tid_t tid, uint32_t signo);
387
388 /// Single steps the given thread. If @p signo is anything but
389 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000390 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000391 SingleStep(lldb::tid_t tid, uint32_t signo);
392
Chaoren Linfa03ad22015-02-03 01:50:42 +0000393 // ThreadStateCoordinator helper methods.
Todd Fiala511e5cd2014-09-11 23:29:14 +0000394 void
Chaoren Linfa03ad22015-02-03 01:50:42 +0000395 NotifyThreadCreateStopped (lldb::tid_t tid);
Todd Fiala511e5cd2014-09-11 23:29:14 +0000396
397 void
Chaoren Linfa03ad22015-02-03 01:50:42 +0000398 NotifyThreadCreateRunning (lldb::tid_t tid);
399
400 void
401 NotifyThreadDeath (lldb::tid_t tid);
402
403 void
404 NotifyThreadStop (lldb::tid_t tid);
405
406 void
407 CallAfterRunningThreadsStop (lldb::tid_t tid,
408 const std::function<void (lldb::tid_t tid)> &call_after_function);
Todd Fiala511e5cd2014-09-11 23:29:14 +0000409
Chaoren Lin03f12d62015-02-03 01:50:49 +0000410 void
411 CallAfterRunningThreadsStopWithSkipTID (lldb::tid_t deferred_signal_tid,
412 lldb::tid_t skip_stop_request_tid,
413 const std::function<void (lldb::tid_t tid)> &call_after_function);
414
Todd Fialaaf245d12014-06-30 21:05:18 +0000415 lldb_private::Error
416 Detach(lldb::tid_t tid);
Chaoren Lin86fd8e42015-02-03 01:51:15 +0000417
418 lldb_private::Error
419 RequestThreadStop (const lldb::pid_t pid, const lldb::tid_t tid);
Todd Fialaaf245d12014-06-30 21:05:18 +0000420 };
421} // End lldb_private namespace.
422
423#endif // #ifndef liblldb_NativeProcessLinux_H_