blob: 7d95f2230d7502dd5a1b41ddfe8e81b94b476a75 [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
Todd Fialaaf245d12014-06-30 21:05:18 +000013// C++ Includes
14#include <unordered_set>
15
16// Other libraries and framework includes
17#include "lldb/Core/ArchSpec.h"
18#include "lldb/lldb-types.h"
19#include "lldb/Host/Debug.h"
Chaoren Lind3173f32015-05-29 19:52:29 +000020#include "lldb/Host/FileSpec.h"
Zachary Turner39de3112014-09-09 20:54:56 +000021#include "lldb/Host/HostThread.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000022#include "lldb/Host/Mutex.h"
23#include "lldb/Target/MemoryRegionInfo.h"
24
Chaoren Lin2fe1d0a2015-02-03 01:51:38 +000025#include "lldb/Host/common/NativeProcessProtocol.h"
Pavel Labath8c8ff7a2015-05-11 10:03:10 +000026#include "NativeThreadLinux.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000027
Tamas Berghammerdb264a62015-03-31 09:52:22 +000028namespace lldb_private {
Todd Fialaaf245d12014-06-30 21:05:18 +000029 class Error;
30 class Module;
31 class Scalar;
32
Tamas Berghammerdb264a62015-03-31 09:52:22 +000033namespace process_linux {
Todd Fialaaf245d12014-06-30 21:05:18 +000034 /// @class NativeProcessLinux
35 /// @brief Manages communication with the inferior (debugee) process.
36 ///
37 /// Upon construction, this class prepares and launches an inferior process for
38 /// debugging.
39 ///
40 /// Changes in the inferior process state are broadcasted.
41 class NativeProcessLinux: public NativeProcessProtocol
42 {
Pavel Labathd5b310f2015-07-09 11:51:11 +000043 friend Error
44 NativeProcessProtocol::Launch (ProcessLaunchInfo &launch_info,
45 NativeDelegate &native_delegate,
Pavel Labath19cbe962015-07-21 13:20:32 +000046 MainLoop &mainloop,
Pavel Labathd5b310f2015-07-09 11:51:11 +000047 NativeProcessProtocolSP &process_sp);
48
49 friend Error
50 NativeProcessProtocol::Attach (lldb::pid_t pid,
Pavel Labath19cbe962015-07-21 13:20:32 +000051 NativeProcessProtocol::NativeDelegate &native_delegate,
52 MainLoop &mainloop,
53 NativeProcessProtocolSP &process_sp);
Pavel Labathd5b310f2015-07-09 11:51:11 +000054
Sean Callananc307c272015-07-08 16:33:46 +000055 public:
Todd Fialaaf245d12014-06-30 21:05:18 +000056 // ---------------------------------------------------------------------
Todd Fialaaf245d12014-06-30 21:05:18 +000057 // NativeProcessProtocol Interface
58 // ---------------------------------------------------------------------
59 Error
60 Resume (const ResumeActionList &resume_actions) override;
61
62 Error
63 Halt () override;
64
65 Error
66 Detach () override;
67
68 Error
69 Signal (int signo) override;
70
71 Error
Chaoren Line9547b82015-02-03 01:51:00 +000072 Interrupt () override;
73
74 Error
Todd Fialaaf245d12014-06-30 21:05:18 +000075 Kill () override;
76
77 Error
78 GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override;
79
80 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +000081 ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000082
83 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +000084 ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000085
86 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +000087 WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) override;
88
89 Error
90 AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000091
92 Error
93 DeallocateMemory (lldb::addr_t addr) override;
94
95 lldb::addr_t
96 GetSharedLibraryInfoAddress () override;
97
98 size_t
99 UpdateThreads () override;
100
101 bool
102 GetArchitecture (ArchSpec &arch) const override;
103
104 Error
105 SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override;
106
107 void
108 DoStopIDBumped (uint32_t newBumpId) override;
109
Tamas Berghammer068f8a72015-05-26 11:58:52 +0000110 Error
111 GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) override;
112
Tamas Berghammer783bfc82015-06-18 20:43:56 +0000113 Error
114 GetFileLoadAddress(const llvm::StringRef& file_name, lldb::addr_t& load_addr) override;
115
Pavel Labathf9077782015-08-21 09:13:53 +0000116 NativeThreadLinuxSP
117 GetThreadByID(lldb::tid_t id);
118
Todd Fialaaf245d12014-06-30 21:05:18 +0000119 // ---------------------------------------------------------------------
120 // Interface used by NativeRegisterContext-derived classes.
121 // ---------------------------------------------------------------------
Pavel Labath4a9babb2015-06-30 17:04:49 +0000122 static Error
123 PtraceWrapper(int req,
124 lldb::pid_t pid,
125 void *addr = nullptr,
126 void *data = nullptr,
127 size_t data_size = 0,
128 long *result = nullptr);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +0000129
Todd Fialaaf245d12014-06-30 21:05:18 +0000130 protected:
131 // ---------------------------------------------------------------------
132 // NativeProcessProtocol protected interface
133 // ---------------------------------------------------------------------
134 Error
135 GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override;
136
137 private:
138
Pavel Labath19cbe962015-07-21 13:20:32 +0000139 MainLoop::SignalHandleUP m_sigchld_handle;
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000140 ArchSpec m_arch;
Todd Fialaaf245d12014-06-30 21:05:18 +0000141
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000142 LazyBool m_supports_mem_region;
Todd Fialaaf245d12014-06-30 21:05:18 +0000143 std::vector<MemoryRegionInfo> m_mem_region_cache;
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000144 Mutex m_mem_region_cache_mutex;
Todd Fialaaf245d12014-06-30 21:05:18 +0000145
Pavel Labath0e1d7292015-08-20 09:06:12 +0000146 lldb::tid_t m_pending_notification_tid;
147
Tamas Berghammerd8c338d2015-04-15 09:47:02 +0000148 // List of thread ids stepping with a breakpoint with the address of
149 // the relevan breakpoint
150 std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint;
151
Todd Fialaaf245d12014-06-30 21:05:18 +0000152 /// @class LauchArgs
153 ///
154 /// @brief Simple structure to pass data to the thread responsible for
155 /// launching a child process.
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000156 struct LaunchArgs
Todd Fialaaf245d12014-06-30 21:05:18 +0000157 {
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000158 LaunchArgs(Module *module,
Todd Fialaaf245d12014-06-30 21:05:18 +0000159 char const **argv,
160 char const **envp,
Chaoren Lind3173f32015-05-29 19:52:29 +0000161 const FileSpec &stdin_file_spec,
162 const FileSpec &stdout_file_spec,
163 const FileSpec &stderr_file_spec,
164 const FileSpec &working_dir,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000165 const ProcessLaunchInfo &launch_info);
Todd Fialaaf245d12014-06-30 21:05:18 +0000166
167 ~LaunchArgs();
168
Chaoren Lind3173f32015-05-29 19:52:29 +0000169 Module *m_module; // The executable image to launch.
170 char const **m_argv; // Process arguments.
171 char const **m_envp; // Process environment.
172 const FileSpec m_stdin_file_spec; // Redirect stdin if not empty.
173 const FileSpec m_stdout_file_spec; // Redirect stdout if not empty.
174 const FileSpec m_stderr_file_spec; // Redirect stderr if not empty.
175 const FileSpec m_working_dir; // Working directory or empty.
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000176 const ProcessLaunchInfo &m_launch_info;
Todd Fialaaf245d12014-06-30 21:05:18 +0000177 };
178
Omair Javaid20405482015-08-09 19:04:41 +0000179 typedef std::function< ::pid_t(Error &)> InitialOperation;
Todd Fialaaf245d12014-06-30 21:05:18 +0000180
181 // ---------------------------------------------------------------------
182 // Private Instance Methods
183 // ---------------------------------------------------------------------
184 NativeProcessLinux ();
185
186 /// Launches an inferior process ready for debugging. Forms the
187 /// implementation of Process::DoLaunch.
188 void
189 LaunchInferior (
Pavel Labath19cbe962015-07-21 13:20:32 +0000190 MainLoop &mainloop,
Todd Fialaaf245d12014-06-30 21:05:18 +0000191 Module *module,
192 char const *argv[],
193 char const *envp[],
Chaoren Lind3173f32015-05-29 19:52:29 +0000194 const FileSpec &stdin_file_spec,
195 const FileSpec &stdout_file_spec,
196 const FileSpec &stderr_file_spec,
197 const FileSpec &working_dir,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000198 const ProcessLaunchInfo &launch_info,
Todd Fialaaf245d12014-06-30 21:05:18 +0000199 Error &error);
200
201 /// Attaches to an existing process. Forms the
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +0000202 /// implementation of Process::DoAttach
Todd Fialaaf245d12014-06-30 21:05:18 +0000203 void
Pavel Labath19cbe962015-07-21 13:20:32 +0000204 AttachToInferior (MainLoop &mainloop, lldb::pid_t pid, Error &error);
Pavel Labath1107b5a2015-04-17 14:07:49 +0000205
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000206 ::pid_t
207 Launch(LaunchArgs *args, Error &error);
Todd Fialaaf245d12014-06-30 21:05:18 +0000208
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000209 ::pid_t
210 Attach(lldb::pid_t pid, Error &error);
Todd Fialaaf245d12014-06-30 21:05:18 +0000211
Chaoren Lin97ccc292015-02-03 01:51:12 +0000212 static Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000213 SetDefaultPtraceOpts(const lldb::pid_t);
214
Todd Fialaaf245d12014-06-30 21:05:18 +0000215 static bool
Chaoren Lind3173f32015-05-29 19:52:29 +0000216 DupDescriptor(const FileSpec &file_spec, int fd, int flags);
Todd Fialaaf245d12014-06-30 21:05:18 +0000217
Pavel Labath1107b5a2015-04-17 14:07:49 +0000218 static void *
219 MonitorThread(void *baton);
220
221 void
222 MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status);
Todd Fialaaf245d12014-06-30 21:05:18 +0000223
224 void
Pavel Labath426bdf82015-04-28 07:51:52 +0000225 WaitForNewThread(::pid_t tid);
226
227 void
Todd Fialaaf245d12014-06-30 21:05:18 +0000228 MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid);
229
230 void
Pavel Labathf9077782015-08-21 09:13:53 +0000231 MonitorTrace(lldb::pid_t pid, const NativeThreadLinuxSP &thread_sp);
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000232
233 void
Pavel Labathf9077782015-08-21 09:13:53 +0000234 MonitorBreakpoint(lldb::pid_t pid, const NativeThreadLinuxSP &thread_sp);
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000235
236 void
Pavel Labathf9077782015-08-21 09:13:53 +0000237 MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index);
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000238
239 void
Todd Fialaaf245d12014-06-30 21:05:18 +0000240 MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited);
241
Tamas Berghammere7708682015-04-22 10:00:23 +0000242 bool
243 SupportHardwareSingleStepping() const;
244
245 Error
246 SetupSoftwareSingleStepping(NativeThreadProtocolSP thread_sp);
247
Todd Fialaaf245d12014-06-30 21:05:18 +0000248#if 0
249 static ::ProcessMessage::CrashReason
250 GetCrashReasonForSIGSEGV(const siginfo_t *info);
251
252 static ::ProcessMessage::CrashReason
253 GetCrashReasonForSIGILL(const siginfo_t *info);
254
255 static ::ProcessMessage::CrashReason
256 GetCrashReasonForSIGFPE(const siginfo_t *info);
257
258 static ::ProcessMessage::CrashReason
259 GetCrashReasonForSIGBUS(const siginfo_t *info);
260#endif
261
Todd Fialaaf245d12014-06-30 21:05:18 +0000262 bool
263 HasThreadNoLock (lldb::tid_t thread_id);
264
265 NativeThreadProtocolSP
266 MaybeGetThreadNoLock (lldb::tid_t thread_id);
267
268 bool
269 StopTrackingThread (lldb::tid_t thread_id);
270
Pavel Labathf9077782015-08-21 09:13:53 +0000271 NativeThreadLinuxSP
Todd Fialaaf245d12014-06-30 21:05:18 +0000272 AddThread (lldb::tid_t thread_id);
273
Todd Fialaaf245d12014-06-30 21:05:18 +0000274 Error
Tamas Berghammer63c8be92015-04-15 09:38:48 +0000275 GetSoftwareBreakpointPCOffset (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size);
Todd Fialaaf245d12014-06-30 21:05:18 +0000276
277 Error
Pavel Labathf9077782015-08-21 09:13:53 +0000278 FixupBreakpointPCAsNeeded(const NativeThreadLinuxSP &thread_sp);
Todd Fialaaf245d12014-06-30 21:05:18 +0000279
280 /// Writes a siginfo_t structure corresponding to the given thread ID to the
281 /// memory region pointed to by @p siginfo.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000282 Error
283 GetSignalInfo(lldb::tid_t tid, void *siginfo);
Todd Fialaaf245d12014-06-30 21:05:18 +0000284
285 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
286 /// corresponding to the given thread ID to the memory pointed to by @p
287 /// message.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000288 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000289 GetEventMessage(lldb::tid_t tid, unsigned long *message);
290
291 /// Resumes the given thread. If @p signo is anything but
292 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000293 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000294 Resume(lldb::tid_t tid, uint32_t signo);
295
296 /// Single steps the given thread. If @p signo is anything but
297 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000298 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000299 SingleStep(lldb::tid_t tid, uint32_t signo);
300
Todd Fiala511e5cd2014-09-11 23:29:14 +0000301 void
Chaoren Linfa03ad22015-02-03 01:50:42 +0000302 NotifyThreadDeath (lldb::tid_t tid);
303
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000304 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000305 Detach(lldb::tid_t tid);
Chaoren Lin86fd8e42015-02-03 01:51:15 +0000306
Pavel Labathc0765592015-05-06 10:46:34 +0000307
Pavel Labath1dbc6c92015-05-12 08:35:33 +0000308 // This method is requests a stop on all threads which are still running. It sets up a
309 // deferred delegate notification, which will fire once threads report as stopped. The
310 // triggerring_tid will be set as the current thread (main stop reason).
Pavel Labathc0765592015-05-06 10:46:34 +0000311 void
Pavel Labath337f3eb2015-05-08 08:57:45 +0000312 StopRunningThreads(lldb::tid_t triggering_tid);
Pavel Labathc0765592015-05-06 10:46:34 +0000313
Pavel Labath9eb1ecb2015-05-15 13:49:01 +0000314 // Notify the delegate if all threads have stopped.
315 void SignalIfAllThreadsStopped();
Pavel Labathc0765592015-05-06 10:46:34 +0000316
Pavel Labath0e1d7292015-08-20 09:06:12 +0000317 // Resume the given thread, optionally passing it the given signal. The type of resume
318 // operation (continue, single-step) depends on the state parameter.
Pavel Labath5eb721e2015-05-07 08:30:31 +0000319 Error
Pavel Labath0e1d7292015-08-20 09:06:12 +0000320 ResumeThread(const NativeThreadLinuxSP &thread_sp, lldb::StateType state, int signo);
Pavel Labathc0765592015-05-06 10:46:34 +0000321
322 void
Pavel Labathf9077782015-08-21 09:13:53 +0000323 ThreadWasCreated(NativeThreadLinux &thread);
Pavel Labathc0765592015-05-06 10:46:34 +0000324
Pavel Labath19cbe962015-07-21 13:20:32 +0000325 void
326 SigchldHandler();
Todd Fialaaf245d12014-06-30 21:05:18 +0000327 };
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000328
329} // namespace process_linux
330} // namespace lldb_private
Todd Fialaaf245d12014-06-30 21:05:18 +0000331
332#endif // #ifndef liblldb_NativeProcessLinux_H_