blob: 4041c14395955d75d93357773b8d826d8d04188d [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
Omair Javaidea8c25a802015-05-15 06:29:58 +0000154#if defined (__arm64__) || defined (__aarch64__)
155 /// Reads hardware breakpoints and watchpoints capability information.
156 Error
157 ReadHardwareDebugInfo (lldb::tid_t tid, unsigned int &watch_count ,
158 unsigned int &break_count);
159
160 /// Write hardware breakpoint/watchpoint control and address registers.
161 Error
162 WriteHardwareDebugRegs (lldb::tid_t tid, lldb::addr_t *addr_buf,
163 uint32_t *cntrl_buf, int type, int count);
164#endif
Todd Fialaaf245d12014-06-30 21:05:18 +0000165 /// Reads the specified register set into the specified buffer.
166 /// For instance, the extended floating-point register set.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000167 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000168 ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
169
170 /// Writes all general purpose registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000171 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000172 WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size);
173
174 /// Writes generic floating point registers into the specified buffer.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000175 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000176 WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size);
177
178 /// Writes the specified register set into the specified buffer.
179 /// For instance, the extended floating-point register set.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000180 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000181 WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +0000182
183 Error
184 GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) override;
185
Todd Fialaaf245d12014-06-30 21:05:18 +0000186 protected:
187 // ---------------------------------------------------------------------
188 // NativeProcessProtocol protected interface
189 // ---------------------------------------------------------------------
190 Error
191 GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override;
192
193 private:
194
Pavel Labath1107b5a2015-04-17 14:07:49 +0000195 class Monitor;
196
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000197 ArchSpec m_arch;
Todd Fialaaf245d12014-06-30 21:05:18 +0000198
Pavel Labath1107b5a2015-04-17 14:07:49 +0000199 std::unique_ptr<Monitor> m_monitor_up;
Todd Fialaaf245d12014-06-30 21:05:18 +0000200
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000201 LazyBool m_supports_mem_region;
Todd Fialaaf245d12014-06-30 21:05:18 +0000202 std::vector<MemoryRegionInfo> m_mem_region_cache;
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000203 Mutex m_mem_region_cache_mutex;
Todd Fialaaf245d12014-06-30 21:05:18 +0000204
Tamas Berghammerd8c338d2015-04-15 09:47:02 +0000205 // List of thread ids stepping with a breakpoint with the address of
206 // the relevan breakpoint
207 std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint;
208
Todd Fialaaf245d12014-06-30 21:05:18 +0000209 /// @class LauchArgs
210 ///
211 /// @brief Simple structure to pass data to the thread responsible for
212 /// launching a child process.
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000213 struct LaunchArgs
Todd Fialaaf245d12014-06-30 21:05:18 +0000214 {
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000215 LaunchArgs(Module *module,
Todd Fialaaf245d12014-06-30 21:05:18 +0000216 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,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000222 const ProcessLaunchInfo &launch_info);
Todd Fialaaf245d12014-06-30 21:05:18 +0000223
224 ~LaunchArgs();
225
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000226 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.
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000232 const char *m_working_dir; // Working directory or NULL.
233 const ProcessLaunchInfo &m_launch_info;
Todd Fialaaf245d12014-06-30 21:05:18 +0000234 };
235
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000236 typedef std::function<::pid_t(Error &)> InitialOperation;
Todd Fialaaf245d12014-06-30 21:05:18 +0000237
238 // ---------------------------------------------------------------------
239 // Private Instance Methods
240 // ---------------------------------------------------------------------
241 NativeProcessLinux ();
242
243 /// Launches an inferior process ready for debugging. Forms the
244 /// implementation of Process::DoLaunch.
245 void
246 LaunchInferior (
247 Module *module,
248 char const *argv[],
249 char const *envp[],
Todd Fiala75f47c32014-10-11 21:42:09 +0000250 const std::string &stdin_path,
251 const std::string &stdout_path,
252 const std::string &stderr_path,
Todd Fialaaf245d12014-06-30 21:05:18 +0000253 const char *working_dir,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000254 const ProcessLaunchInfo &launch_info,
Todd Fialaaf245d12014-06-30 21:05:18 +0000255 Error &error);
256
257 /// Attaches to an existing process. Forms the
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +0000258 /// implementation of Process::DoAttach
Todd Fialaaf245d12014-06-30 21:05:18 +0000259 void
260 AttachToInferior (lldb::pid_t pid, Error &error);
261
262 void
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000263 StartMonitorThread(const InitialOperation &operation, Error &error);
Pavel Labath1107b5a2015-04-17 14:07:49 +0000264
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000265 ::pid_t
266 Launch(LaunchArgs *args, Error &error);
Todd Fialaaf245d12014-06-30 21:05:18 +0000267
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000268 ::pid_t
269 Attach(lldb::pid_t pid, Error &error);
Todd Fialaaf245d12014-06-30 21:05:18 +0000270
Chaoren Lin97ccc292015-02-03 01:51:12 +0000271 static Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000272 SetDefaultPtraceOpts(const lldb::pid_t);
273
Todd Fialaaf245d12014-06-30 21:05:18 +0000274 static bool
275 DupDescriptor(const char *path, int fd, int flags);
276
Pavel Labath1107b5a2015-04-17 14:07:49 +0000277 static void *
278 MonitorThread(void *baton);
279
280 void
281 MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status);
Todd Fialaaf245d12014-06-30 21:05:18 +0000282
283 void
Pavel Labath426bdf82015-04-28 07:51:52 +0000284 WaitForNewThread(::pid_t tid);
285
286 void
Todd Fialaaf245d12014-06-30 21:05:18 +0000287 MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid);
288
289 void
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000290 MonitorTrace(lldb::pid_t pid, NativeThreadProtocolSP thread_sp);
291
292 void
293 MonitorBreakpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp);
294
295 void
296 MonitorWatchpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp, uint32_t wp_index);
297
298 void
Todd Fialaaf245d12014-06-30 21:05:18 +0000299 MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited);
300
Tamas Berghammere7708682015-04-22 10:00:23 +0000301 bool
302 SupportHardwareSingleStepping() const;
303
304 Error
305 SetupSoftwareSingleStepping(NativeThreadProtocolSP thread_sp);
306
Todd Fialaaf245d12014-06-30 21:05:18 +0000307#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
Todd Fialaaf245d12014-06-30 21:05:18 +0000321 bool
322 HasThreadNoLock (lldb::tid_t thread_id);
323
324 NativeThreadProtocolSP
325 MaybeGetThreadNoLock (lldb::tid_t thread_id);
326
327 bool
328 StopTrackingThread (lldb::tid_t thread_id);
329
330 NativeThreadProtocolSP
331 AddThread (lldb::tid_t thread_id);
332
Todd Fialaaf245d12014-06-30 21:05:18 +0000333 Error
Tamas Berghammer63c8be92015-04-15 09:38:48 +0000334 GetSoftwareBreakpointPCOffset (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size);
Todd Fialaaf245d12014-06-30 21:05:18 +0000335
336 Error
337 FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp);
338
339 /// Writes a siginfo_t structure corresponding to the given thread ID to the
340 /// memory region pointed to by @p siginfo.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000341 Error
342 GetSignalInfo(lldb::tid_t tid, void *siginfo);
Todd Fialaaf245d12014-06-30 21:05:18 +0000343
344 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
345 /// corresponding to the given thread ID to the memory pointed to by @p
346 /// message.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000347 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000348 GetEventMessage(lldb::tid_t tid, unsigned long *message);
349
350 /// Resumes the given thread. If @p signo is anything but
351 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000352 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000353 Resume(lldb::tid_t tid, uint32_t signo);
354
355 /// Single steps the given thread. If @p signo is anything but
356 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000357 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000358 SingleStep(lldb::tid_t tid, uint32_t signo);
359
Todd Fiala511e5cd2014-09-11 23:29:14 +0000360 void
Chaoren Linfa03ad22015-02-03 01:50:42 +0000361 NotifyThreadDeath (lldb::tid_t tid);
362
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000363 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000364 Detach(lldb::tid_t tid);
Chaoren Lin86fd8e42015-02-03 01:51:15 +0000365
Pavel Labathc0765592015-05-06 10:46:34 +0000366
Pavel Labathc0765592015-05-06 10:46:34 +0000367 // Typedefs.
368 typedef std::unordered_set<lldb::tid_t> ThreadIDSet;
369
Pavel Labath1dbc6c92015-05-12 08:35:33 +0000370 // This method is requests a stop on all threads which are still running. It sets up a
371 // deferred delegate notification, which will fire once threads report as stopped. The
372 // triggerring_tid will be set as the current thread (main stop reason).
Pavel Labathc0765592015-05-06 10:46:34 +0000373 void
Pavel Labath337f3eb2015-05-08 08:57:45 +0000374 StopRunningThreads(lldb::tid_t triggering_tid);
Pavel Labathc0765592015-05-06 10:46:34 +0000375
Pavel Labathc0765592015-05-06 10:46:34 +0000376 struct PendingNotification
377 {
Pavel Labath337f3eb2015-05-08 08:57:45 +0000378 PendingNotification (lldb::tid_t triggering_tid):
379 triggering_tid (triggering_tid),
Pavel Labath108c3252015-05-12 09:03:18 +0000380 wait_for_stop_tids ()
Pavel Labathc0765592015-05-06 10:46:34 +0000381 {
382 }
383
384 const lldb::tid_t triggering_tid;
385 ThreadIDSet wait_for_stop_tids;
Pavel Labathc0765592015-05-06 10:46:34 +0000386 };
387 typedef std::unique_ptr<PendingNotification> PendingNotificationUP;
388
389 // Fire pending notification if no pending thread stops remain.
390 void SignalIfRequirementsSatisfied();
391
Pavel Labathc0765592015-05-06 10:46:34 +0000392 void
393 RequestStopOnAllRunningThreads();
394
Pavel Labath5eb721e2015-05-07 08:30:31 +0000395 Error
396 ThreadDidStop(lldb::tid_t tid, bool initiated_by_llgs);
Pavel Labathc0765592015-05-06 10:46:34 +0000397
Pavel Labath1dbc6c92015-05-12 08:35:33 +0000398 // Resume the thread with the given thread id using the request_thread_resume_function
399 // called. If error_when_already_running is then then an error is raised if we think this
400 // thread is already running.
Pavel Labath5eb721e2015-05-07 08:30:31 +0000401 Error
Pavel Labath1dbc6c92015-05-12 08:35:33 +0000402 ResumeThread(lldb::tid_t tid, NativeThreadLinux::ResumeThreadFunction request_thread_resume_function,
Pavel Labath5eb721e2015-05-07 08:30:31 +0000403 bool error_when_already_running);
Pavel Labathc0765592015-05-06 10:46:34 +0000404
405 void
Pavel Labathed89c7f2015-05-06 12:22:37 +0000406 DoStopThreads(PendingNotificationUP &&notification_up);
Pavel Labathc0765592015-05-06 10:46:34 +0000407
408 void
Pavel Labath8c8ff7a2015-05-11 10:03:10 +0000409 ThreadWasCreated (lldb::tid_t tid);
Pavel Labathc0765592015-05-06 10:46:34 +0000410
Pavel Labathc0765592015-05-06 10:46:34 +0000411 // Member variables.
Pavel Labathc0765592015-05-06 10:46:34 +0000412 PendingNotificationUP m_pending_notification_up;
Todd Fialaaf245d12014-06-30 21:05:18 +0000413 };
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000414
415} // namespace process_linux
416} // namespace lldb_private
Todd Fialaaf245d12014-06-30 21:05:18 +0000417
418#endif // #ifndef liblldb_NativeProcessLinux_H_