blob: 62ff0619a245525fed46dbf4b9cade1208dcad9a [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
Pavel Labathc0765592015-05-06 10:46:34 +000018#include <mutex>
19#include <unordered_map>
Todd Fialaaf245d12014-06-30 21:05:18 +000020#include <unordered_set>
21
22// Other libraries and framework includes
23#include "lldb/Core/ArchSpec.h"
24#include "lldb/lldb-types.h"
25#include "lldb/Host/Debug.h"
Zachary Turner39de3112014-09-09 20:54:56 +000026#include "lldb/Host/HostThread.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000027#include "lldb/Host/Mutex.h"
28#include "lldb/Target/MemoryRegionInfo.h"
29
Chaoren Lin2fe1d0a2015-02-03 01:51:38 +000030#include "lldb/Host/common/NativeProcessProtocol.h"
Pavel Labath8c8ff7a2015-05-11 10:03:10 +000031#include "NativeThreadLinux.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000032
Tamas Berghammerdb264a62015-03-31 09:52:22 +000033namespace lldb_private {
Todd Fialaaf245d12014-06-30 21:05:18 +000034 class Error;
35 class Module;
36 class Scalar;
37
Tamas Berghammerdb264a62015-03-31 09:52:22 +000038namespace process_linux {
Todd Fialaaf245d12014-06-30 21:05:18 +000039 /// @class NativeProcessLinux
40 /// @brief Manages communication with the inferior (debugee) process.
41 ///
42 /// Upon construction, this class prepares and launches an inferior process for
43 /// debugging.
44 ///
45 /// Changes in the inferior process state are broadcasted.
46 class NativeProcessLinux: public NativeProcessProtocol
47 {
48 public:
49
Tamas Berghammerdb264a62015-03-31 09:52:22 +000050 static Error
Todd Fialaaf245d12014-06-30 21:05:18 +000051 LaunchProcess (
52 Module *exe_module,
53 ProcessLaunchInfo &launch_info,
Tamas Berghammerdb264a62015-03-31 09:52:22 +000054 NativeProcessProtocol::NativeDelegate &native_delegate,
Todd Fialaaf245d12014-06-30 21:05:18 +000055 NativeProcessProtocolSP &native_process_sp);
56
Tamas Berghammerdb264a62015-03-31 09:52:22 +000057 static Error
Todd Fialaaf245d12014-06-30 21:05:18 +000058 AttachToProcess (
59 lldb::pid_t pid,
Tamas Berghammerdb264a62015-03-31 09:52:22 +000060 NativeProcessProtocol::NativeDelegate &native_delegate,
Todd Fialaaf245d12014-06-30 21:05:18 +000061 NativeProcessProtocolSP &native_process_sp);
62
63 // ---------------------------------------------------------------------
Todd Fialaaf245d12014-06-30 21:05:18 +000064 // NativeProcessProtocol Interface
65 // ---------------------------------------------------------------------
66 Error
67 Resume (const ResumeActionList &resume_actions) override;
68
69 Error
70 Halt () override;
71
72 Error
73 Detach () override;
74
75 Error
76 Signal (int signo) override;
77
78 Error
Chaoren Line9547b82015-02-03 01:51:00 +000079 Interrupt () override;
80
81 Error
Todd Fialaaf245d12014-06-30 21:05:18 +000082 Kill () override;
83
84 Error
85 GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override;
86
87 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +000088 ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000089
90 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +000091 ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000092
93 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +000094 WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) override;
95
96 Error
97 AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000098
99 Error
100 DeallocateMemory (lldb::addr_t addr) override;
101
102 lldb::addr_t
103 GetSharedLibraryInfoAddress () override;
104
105 size_t
106 UpdateThreads () override;
107
108 bool
109 GetArchitecture (ArchSpec &arch) const override;
110
111 Error
112 SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override;
113
Pavel Labath45f5cb32015-05-05 15:05:50 +0000114 Error
115 SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) override;
116
117 Error
118 RemoveWatchpoint (lldb::addr_t addr) override;
119
Todd Fialaaf245d12014-06-30 21:05:18 +0000120 void
121 DoStopIDBumped (uint32_t newBumpId) override;
122
Oleksiy Vyalov8bc34f42015-02-19 17:58:04 +0000123 void
124 Terminate () override;
125
Todd Fialaaf245d12014-06-30 21:05:18 +0000126 // ---------------------------------------------------------------------
127 // Interface used by NativeRegisterContext-derived classes.
128 // ---------------------------------------------------------------------
129
130 /// Reads the contents from the register identified by the given (architecture
131 /// dependent) offset.
132 ///
133 /// This method is provided for use by RegisterContextLinux derivatives.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000134 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000135 ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000136 unsigned size, RegisterValue &value);
Todd Fialaaf245d12014-06-30 21:05:18 +0000137
138 /// Writes the given value to the register identified by the given
139 /// (architecture dependent) offset.
140 ///
141 /// This method is provided for use by RegisterContextLinux derivatives.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000142 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000143 WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000144 const RegisterValue &value);
Todd Fialaaf245d12014-06-30 21:05:18 +0000145
146 /// Reads all general purpose registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000147 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000148 ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size);
149
150 /// Reads generic floating point registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000151 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000152 ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size);
153
154 /// Reads the specified register set into the specified buffer.
155 /// For instance, the extended floating-point register set.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000156 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000157 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
158
159 /// Writes all general purpose registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000160 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000161 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
162
163 /// Writes generic floating point registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000164 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000165 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
166
167 /// Writes the specified register set into the specified buffer.
168 /// For instance, the extended floating-point register set.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000169 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000170 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +0000171
172 Error
173 GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) override;
174
Todd Fialaaf245d12014-06-30 21:05:18 +0000175 protected:
176 // ---------------------------------------------------------------------
177 // NativeProcessProtocol protected interface
178 // ---------------------------------------------------------------------
179 Error
180 GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override;
181
182 private:
183
Pavel Labath1107b5a2015-04-17 14:07:49 +0000184 class Monitor;
185
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000186 ArchSpec m_arch;
Todd Fialaaf245d12014-06-30 21:05:18 +0000187
Pavel Labath1107b5a2015-04-17 14:07:49 +0000188 std::unique_ptr<Monitor> m_monitor_up;
Todd Fialaaf245d12014-06-30 21:05:18 +0000189
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000190 LazyBool m_supports_mem_region;
Todd Fialaaf245d12014-06-30 21:05:18 +0000191 std::vector<MemoryRegionInfo> m_mem_region_cache;
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000192 Mutex m_mem_region_cache_mutex;
Todd Fialaaf245d12014-06-30 21:05:18 +0000193
Tamas Berghammerd8c338d2015-04-15 09:47:02 +0000194 // List of thread ids stepping with a breakpoint with the address of
195 // the relevan breakpoint
196 std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint;
197
Todd Fialaaf245d12014-06-30 21:05:18 +0000198 /// @class LauchArgs
199 ///
200 /// @brief Simple structure to pass data to the thread responsible for
201 /// launching a child process.
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000202 struct LaunchArgs
Todd Fialaaf245d12014-06-30 21:05:18 +0000203 {
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000204 LaunchArgs(Module *module,
Todd Fialaaf245d12014-06-30 21:05:18 +0000205 char const **argv,
206 char const **envp,
Todd Fiala75f47c32014-10-11 21:42:09 +0000207 const std::string &stdin_path,
208 const std::string &stdout_path,
209 const std::string &stderr_path,
Todd Fiala0bce1b62014-08-17 00:10:50 +0000210 const char *working_dir,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000211 const ProcessLaunchInfo &launch_info);
Todd Fialaaf245d12014-06-30 21:05:18 +0000212
213 ~LaunchArgs();
214
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000215 Module *m_module; // The executable image to launch.
216 char const **m_argv; // Process arguments.
217 char const **m_envp; // Process environment.
Todd Fiala75f47c32014-10-11 21:42:09 +0000218 const std::string &m_stdin_path; // Redirect stdin if not empty.
219 const std::string &m_stdout_path; // Redirect stdout if not empty.
220 const std::string &m_stderr_path; // Redirect stderr if not empty.
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000221 const char *m_working_dir; // Working directory or NULL.
222 const ProcessLaunchInfo &m_launch_info;
Todd Fialaaf245d12014-06-30 21:05:18 +0000223 };
224
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000225 typedef std::function<::pid_t(Error &)> InitialOperation;
Todd Fialaaf245d12014-06-30 21:05:18 +0000226
227 // ---------------------------------------------------------------------
228 // Private Instance Methods
229 // ---------------------------------------------------------------------
230 NativeProcessLinux ();
231
232 /// Launches an inferior process ready for debugging. Forms the
233 /// implementation of Process::DoLaunch.
234 void
235 LaunchInferior (
236 Module *module,
237 char const *argv[],
238 char const *envp[],
Todd Fiala75f47c32014-10-11 21:42:09 +0000239 const std::string &stdin_path,
240 const std::string &stdout_path,
241 const std::string &stderr_path,
Todd Fialaaf245d12014-06-30 21:05:18 +0000242 const char *working_dir,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000243 const ProcessLaunchInfo &launch_info,
Todd Fialaaf245d12014-06-30 21:05:18 +0000244 Error &error);
245
246 /// Attaches to an existing process. Forms the
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +0000247 /// implementation of Process::DoAttach
Todd Fialaaf245d12014-06-30 21:05:18 +0000248 void
249 AttachToInferior (lldb::pid_t pid, Error &error);
250
251 void
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000252 StartMonitorThread(const InitialOperation &operation, Error &error);
Pavel Labath1107b5a2015-04-17 14:07:49 +0000253
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000254 ::pid_t
255 Launch(LaunchArgs *args, Error &error);
Todd Fialaaf245d12014-06-30 21:05:18 +0000256
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000257 ::pid_t
258 Attach(lldb::pid_t pid, Error &error);
Todd Fialaaf245d12014-06-30 21:05:18 +0000259
Chaoren Lin97ccc292015-02-03 01:51:12 +0000260 static Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000261 SetDefaultPtraceOpts(const lldb::pid_t);
262
Todd Fialaaf245d12014-06-30 21:05:18 +0000263 static bool
264 DupDescriptor(const char *path, int fd, int flags);
265
Pavel Labath1107b5a2015-04-17 14:07:49 +0000266 static void *
267 MonitorThread(void *baton);
268
269 void
270 MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status);
Todd Fialaaf245d12014-06-30 21:05:18 +0000271
272 void
Pavel Labath426bdf82015-04-28 07:51:52 +0000273 WaitForNewThread(::pid_t tid);
274
275 void
Todd Fialaaf245d12014-06-30 21:05:18 +0000276 MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid);
277
278 void
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000279 MonitorTrace(lldb::pid_t pid, NativeThreadProtocolSP thread_sp);
280
281 void
282 MonitorBreakpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp);
283
284 void
285 MonitorWatchpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp, uint32_t wp_index);
286
287 void
Todd Fialaaf245d12014-06-30 21:05:18 +0000288 MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited);
289
Tamas Berghammere7708682015-04-22 10:00:23 +0000290 bool
291 SupportHardwareSingleStepping() const;
292
293 Error
294 SetupSoftwareSingleStepping(NativeThreadProtocolSP thread_sp);
295
Todd Fialaaf245d12014-06-30 21:05:18 +0000296#if 0
297 static ::ProcessMessage::CrashReason
298 GetCrashReasonForSIGSEGV(const siginfo_t *info);
299
300 static ::ProcessMessage::CrashReason
301 GetCrashReasonForSIGILL(const siginfo_t *info);
302
303 static ::ProcessMessage::CrashReason
304 GetCrashReasonForSIGFPE(const siginfo_t *info);
305
306 static ::ProcessMessage::CrashReason
307 GetCrashReasonForSIGBUS(const siginfo_t *info);
308#endif
309
Todd Fialaaf245d12014-06-30 21:05:18 +0000310 bool
311 HasThreadNoLock (lldb::tid_t thread_id);
312
313 NativeThreadProtocolSP
314 MaybeGetThreadNoLock (lldb::tid_t thread_id);
315
316 bool
317 StopTrackingThread (lldb::tid_t thread_id);
318
319 NativeThreadProtocolSP
320 AddThread (lldb::tid_t thread_id);
321
Todd Fialaaf245d12014-06-30 21:05:18 +0000322 Error
Tamas Berghammer63c8be92015-04-15 09:38:48 +0000323 GetSoftwareBreakpointPCOffset (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size);
Todd Fialaaf245d12014-06-30 21:05:18 +0000324
325 Error
326 FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp);
327
328 /// Writes a siginfo_t structure corresponding to the given thread ID to the
329 /// memory region pointed to by @p siginfo.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000330 Error
331 GetSignalInfo(lldb::tid_t tid, void *siginfo);
Todd Fialaaf245d12014-06-30 21:05:18 +0000332
333 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
334 /// corresponding to the given thread ID to the memory pointed to by @p
335 /// message.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000336 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000337 GetEventMessage(lldb::tid_t tid, unsigned long *message);
338
339 /// Resumes the given thread. If @p signo is anything but
340 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000341 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000342 Resume(lldb::tid_t tid, uint32_t signo);
343
344 /// Single steps the given thread. If @p signo is anything but
345 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000346 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000347 SingleStep(lldb::tid_t tid, uint32_t signo);
348
Todd Fiala511e5cd2014-09-11 23:29:14 +0000349 void
Chaoren Linfa03ad22015-02-03 01:50:42 +0000350 NotifyThreadDeath (lldb::tid_t tid);
351
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000352 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000353 Detach(lldb::tid_t tid);
Chaoren Lin86fd8e42015-02-03 01:51:15 +0000354
Pavel Labathc0765592015-05-06 10:46:34 +0000355
Pavel Labathc0765592015-05-06 10:46:34 +0000356 // Typedefs.
357 typedef std::unordered_set<lldb::tid_t> ThreadIDSet;
358
Pavel Labath1dbc6c92015-05-12 08:35:33 +0000359 // This method is requests a stop on all threads which are still running. It sets up a
360 // deferred delegate notification, which will fire once threads report as stopped. The
361 // triggerring_tid will be set as the current thread (main stop reason).
Pavel Labathc0765592015-05-06 10:46:34 +0000362 void
Pavel Labath337f3eb2015-05-08 08:57:45 +0000363 StopRunningThreads(lldb::tid_t triggering_tid);
Pavel Labathc0765592015-05-06 10:46:34 +0000364
Pavel Labathc0765592015-05-06 10:46:34 +0000365 struct PendingNotification
366 {
Pavel Labath337f3eb2015-05-08 08:57:45 +0000367 PendingNotification (lldb::tid_t triggering_tid):
368 triggering_tid (triggering_tid),
Pavel Labath108c3252015-05-12 09:03:18 +0000369 wait_for_stop_tids ()
Pavel Labathc0765592015-05-06 10:46:34 +0000370 {
371 }
372
373 const lldb::tid_t triggering_tid;
374 ThreadIDSet wait_for_stop_tids;
Pavel Labathc0765592015-05-06 10:46:34 +0000375 };
376 typedef std::unique_ptr<PendingNotification> PendingNotificationUP;
377
378 // Fire pending notification if no pending thread stops remain.
379 void SignalIfRequirementsSatisfied();
380
Pavel Labathc0765592015-05-06 10:46:34 +0000381 void
382 RequestStopOnAllRunningThreads();
383
Pavel Labath5eb721e2015-05-07 08:30:31 +0000384 Error
385 ThreadDidStop(lldb::tid_t tid, bool initiated_by_llgs);
Pavel Labathc0765592015-05-06 10:46:34 +0000386
Pavel Labath1dbc6c92015-05-12 08:35:33 +0000387 // Resume the thread with the given thread id using the request_thread_resume_function
388 // called. If error_when_already_running is then then an error is raised if we think this
389 // thread is already running.
Pavel Labath5eb721e2015-05-07 08:30:31 +0000390 Error
Pavel Labath1dbc6c92015-05-12 08:35:33 +0000391 ResumeThread(lldb::tid_t tid, NativeThreadLinux::ResumeThreadFunction request_thread_resume_function,
Pavel Labath5eb721e2015-05-07 08:30:31 +0000392 bool error_when_already_running);
Pavel Labathc0765592015-05-06 10:46:34 +0000393
394 void
Pavel Labathed89c7f2015-05-06 12:22:37 +0000395 DoStopThreads(PendingNotificationUP &&notification_up);
Pavel Labathc0765592015-05-06 10:46:34 +0000396
397 void
Pavel Labath8c8ff7a2015-05-11 10:03:10 +0000398 ThreadWasCreated (lldb::tid_t tid);
Pavel Labathc0765592015-05-06 10:46:34 +0000399
Pavel Labathc0765592015-05-06 10:46:34 +0000400 // Member variables.
Pavel Labathc0765592015-05-06 10:46:34 +0000401 PendingNotificationUP m_pending_notification_up;
Todd Fialaaf245d12014-06-30 21:05:18 +0000402 };
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000403
404} // namespace process_linux
405} // namespace lldb_private
Todd Fialaaf245d12014-06-30 21:05:18 +0000406
407#endif // #ifndef liblldb_NativeProcessLinux_H_