blob: c19477540d16fa09ec6c934a7468956f41b77bba [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/Target/MemoryRegionInfo.h"
23
Chaoren Lin2fe1d0a2015-02-03 01:51:38 +000024#include "lldb/Host/common/NativeProcessProtocol.h"
Pavel Labath8c8ff7a2015-05-11 10:03:10 +000025#include "NativeThreadLinux.h"
Todd Fialaaf245d12014-06-30 21:05:18 +000026
Tamas Berghammerdb264a62015-03-31 09:52:22 +000027namespace lldb_private {
Todd Fialaaf245d12014-06-30 21:05:18 +000028 class Error;
29 class Module;
30 class Scalar;
31
Tamas Berghammerdb264a62015-03-31 09:52:22 +000032namespace process_linux {
Todd Fialaaf245d12014-06-30 21:05:18 +000033 /// @class NativeProcessLinux
34 /// @brief Manages communication with the inferior (debugee) process.
35 ///
36 /// Upon construction, this class prepares and launches an inferior process for
37 /// debugging.
38 ///
39 /// Changes in the inferior process state are broadcasted.
40 class NativeProcessLinux: public NativeProcessProtocol
41 {
Pavel Labathd5b310f2015-07-09 11:51:11 +000042 friend Error
43 NativeProcessProtocol::Launch (ProcessLaunchInfo &launch_info,
44 NativeDelegate &native_delegate,
Pavel Labath19cbe962015-07-21 13:20:32 +000045 MainLoop &mainloop,
Pavel Labathd5b310f2015-07-09 11:51:11 +000046 NativeProcessProtocolSP &process_sp);
47
48 friend Error
49 NativeProcessProtocol::Attach (lldb::pid_t pid,
Pavel Labath19cbe962015-07-21 13:20:32 +000050 NativeProcessProtocol::NativeDelegate &native_delegate,
51 MainLoop &mainloop,
52 NativeProcessProtocolSP &process_sp);
Pavel Labathd5b310f2015-07-09 11:51:11 +000053
Sean Callananc307c272015-07-08 16:33:46 +000054 public:
Todd Fialaaf245d12014-06-30 21:05:18 +000055 // ---------------------------------------------------------------------
Todd Fialaaf245d12014-06-30 21:05:18 +000056 // NativeProcessProtocol Interface
57 // ---------------------------------------------------------------------
58 Error
59 Resume (const ResumeActionList &resume_actions) override;
60
61 Error
62 Halt () override;
63
64 Error
65 Detach () override;
66
67 Error
68 Signal (int signo) override;
69
70 Error
Chaoren Line9547b82015-02-03 01:51:00 +000071 Interrupt () override;
72
73 Error
Todd Fialaaf245d12014-06-30 21:05:18 +000074 Kill () override;
75
76 Error
77 GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override;
78
79 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +000080 ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000081
82 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +000083 ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000084
85 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +000086 WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) override;
87
88 Error
89 AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000090
91 Error
92 DeallocateMemory (lldb::addr_t addr) override;
93
94 lldb::addr_t
95 GetSharedLibraryInfoAddress () override;
96
97 size_t
98 UpdateThreads () override;
99
100 bool
101 GetArchitecture (ArchSpec &arch) const override;
102
103 Error
104 SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override;
105
106 void
107 DoStopIDBumped (uint32_t newBumpId) override;
108
Tamas Berghammer068f8a72015-05-26 11:58:52 +0000109 Error
110 GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) override;
111
Tamas Berghammer783bfc82015-06-18 20:43:56 +0000112 Error
113 GetFileLoadAddress(const llvm::StringRef& file_name, lldb::addr_t& load_addr) override;
114
Pavel Labathf9077782015-08-21 09:13:53 +0000115 NativeThreadLinuxSP
116 GetThreadByID(lldb::tid_t id);
117
Todd Fialaaf245d12014-06-30 21:05:18 +0000118 // ---------------------------------------------------------------------
119 // Interface used by NativeRegisterContext-derived classes.
120 // ---------------------------------------------------------------------
Pavel Labath4a9babb2015-06-30 17:04:49 +0000121 static Error
122 PtraceWrapper(int req,
123 lldb::pid_t pid,
124 void *addr = nullptr,
125 void *data = nullptr,
126 size_t data_size = 0,
127 long *result = nullptr);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +0000128
Pavel Labath605b51b2016-02-23 13:56:30 +0000129 bool
130 SupportHardwareSingleStepping() const;
131
Todd Fialaaf245d12014-06-30 21:05:18 +0000132 protected:
133 // ---------------------------------------------------------------------
134 // NativeProcessProtocol protected interface
135 // ---------------------------------------------------------------------
136 Error
137 GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override;
138
139 private:
140
Pavel Labath19cbe962015-07-21 13:20:32 +0000141 MainLoop::SignalHandleUP m_sigchld_handle;
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000142 ArchSpec m_arch;
Todd Fialaaf245d12014-06-30 21:05:18 +0000143
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000144 LazyBool m_supports_mem_region;
Todd Fialaaf245d12014-06-30 21:05:18 +0000145 std::vector<MemoryRegionInfo> m_mem_region_cache;
Todd Fialaaf245d12014-06-30 21:05:18 +0000146
Pavel Labath0e1d7292015-08-20 09:06:12 +0000147 lldb::tid_t m_pending_notification_tid;
148
Tamas Berghammerd8c338d2015-04-15 09:47:02 +0000149 // List of thread ids stepping with a breakpoint with the address of
150 // the relevan breakpoint
151 std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint;
152
Todd Fialaaf245d12014-06-30 21:05:18 +0000153 /// @class LauchArgs
154 ///
155 /// @brief Simple structure to pass data to the thread responsible for
156 /// launching a child process.
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000157 struct LaunchArgs
Todd Fialaaf245d12014-06-30 21:05:18 +0000158 {
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000159 LaunchArgs(Module *module,
Todd Fialaaf245d12014-06-30 21:05:18 +0000160 char const **argv,
161 char const **envp,
Chaoren Lind3173f32015-05-29 19:52:29 +0000162 const FileSpec &stdin_file_spec,
163 const FileSpec &stdout_file_spec,
164 const FileSpec &stderr_file_spec,
165 const FileSpec &working_dir,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000166 const ProcessLaunchInfo &launch_info);
Todd Fialaaf245d12014-06-30 21:05:18 +0000167
168 ~LaunchArgs();
169
Chaoren Lind3173f32015-05-29 19:52:29 +0000170 Module *m_module; // The executable image to launch.
171 char const **m_argv; // Process arguments.
172 char const **m_envp; // Process environment.
173 const FileSpec m_stdin_file_spec; // Redirect stdin if not empty.
174 const FileSpec m_stdout_file_spec; // Redirect stdout if not empty.
175 const FileSpec m_stderr_file_spec; // Redirect stderr if not empty.
176 const FileSpec m_working_dir; // Working directory or empty.
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000177 const ProcessLaunchInfo &m_launch_info;
Todd Fialaaf245d12014-06-30 21:05:18 +0000178 };
179
Omair Javaid20405482015-08-09 19:04:41 +0000180 typedef std::function< ::pid_t(Error &)> InitialOperation;
Todd Fialaaf245d12014-06-30 21:05:18 +0000181
182 // ---------------------------------------------------------------------
183 // Private Instance Methods
184 // ---------------------------------------------------------------------
185 NativeProcessLinux ();
186
187 /// Launches an inferior process ready for debugging. Forms the
188 /// implementation of Process::DoLaunch.
189 void
190 LaunchInferior (
Pavel Labath19cbe962015-07-21 13:20:32 +0000191 MainLoop &mainloop,
Todd Fialaaf245d12014-06-30 21:05:18 +0000192 Module *module,
193 char const *argv[],
194 char const *envp[],
Chaoren Lind3173f32015-05-29 19:52:29 +0000195 const FileSpec &stdin_file_spec,
196 const FileSpec &stdout_file_spec,
197 const FileSpec &stderr_file_spec,
198 const FileSpec &working_dir,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000199 const ProcessLaunchInfo &launch_info,
Todd Fialaaf245d12014-06-30 21:05:18 +0000200 Error &error);
201
202 /// Attaches to an existing process. Forms the
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +0000203 /// implementation of Process::DoAttach
Todd Fialaaf245d12014-06-30 21:05:18 +0000204 void
Pavel Labath19cbe962015-07-21 13:20:32 +0000205 AttachToInferior (MainLoop &mainloop, lldb::pid_t pid, Error &error);
Pavel Labath1107b5a2015-04-17 14:07:49 +0000206
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000207 ::pid_t
208 Launch(LaunchArgs *args, Error &error);
Todd Fialaaf245d12014-06-30 21:05:18 +0000209
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000210 ::pid_t
211 Attach(lldb::pid_t pid, Error &error);
Todd Fialaaf245d12014-06-30 21:05:18 +0000212
Chaoren Lin97ccc292015-02-03 01:51:12 +0000213 static Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000214 SetDefaultPtraceOpts(const lldb::pid_t);
215
Todd Fialaaf245d12014-06-30 21:05:18 +0000216 static bool
Chaoren Lind3173f32015-05-29 19:52:29 +0000217 DupDescriptor(const FileSpec &file_spec, int fd, int flags);
Todd Fialaaf245d12014-06-30 21:05:18 +0000218
Pavel Labath1107b5a2015-04-17 14:07:49 +0000219 static void *
220 MonitorThread(void *baton);
221
222 void
223 MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status);
Todd Fialaaf245d12014-06-30 21:05:18 +0000224
225 void
Pavel Labath426bdf82015-04-28 07:51:52 +0000226 WaitForNewThread(::pid_t tid);
227
228 void
Pavel Labathb9cc0c72015-08-24 09:22:04 +0000229 MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread);
Todd Fialaaf245d12014-06-30 21:05:18 +0000230
231 void
Pavel Labathb9cc0c72015-08-24 09:22:04 +0000232 MonitorTrace(NativeThreadLinux &thread);
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000233
234 void
Pavel Labathb9cc0c72015-08-24 09:22:04 +0000235 MonitorBreakpoint(NativeThreadLinux &thread);
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000236
237 void
Pavel Labathf9077782015-08-21 09:13:53 +0000238 MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index);
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000239
240 void
Pavel Labathb9cc0c72015-08-24 09:22:04 +0000241 MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread, bool exited);
Todd Fialaaf245d12014-06-30 21:05:18 +0000242
Tamas Berghammere7708682015-04-22 10:00:23 +0000243 Error
Pavel Labathb9cc0c72015-08-24 09:22:04 +0000244 SetupSoftwareSingleStepping(NativeThreadLinux &thread);
Tamas Berghammere7708682015-04-22 10:00:23 +0000245
Todd Fialaaf245d12014-06-30 21:05:18 +0000246#if 0
247 static ::ProcessMessage::CrashReason
248 GetCrashReasonForSIGSEGV(const siginfo_t *info);
249
250 static ::ProcessMessage::CrashReason
251 GetCrashReasonForSIGILL(const siginfo_t *info);
252
253 static ::ProcessMessage::CrashReason
254 GetCrashReasonForSIGFPE(const siginfo_t *info);
255
256 static ::ProcessMessage::CrashReason
257 GetCrashReasonForSIGBUS(const siginfo_t *info);
258#endif
259
Todd Fialaaf245d12014-06-30 21:05:18 +0000260 bool
261 HasThreadNoLock (lldb::tid_t thread_id);
262
Todd Fialaaf245d12014-06-30 21:05:18 +0000263 bool
264 StopTrackingThread (lldb::tid_t thread_id);
265
Pavel Labathf9077782015-08-21 09:13:53 +0000266 NativeThreadLinuxSP
Todd Fialaaf245d12014-06-30 21:05:18 +0000267 AddThread (lldb::tid_t thread_id);
268
Todd Fialaaf245d12014-06-30 21:05:18 +0000269 Error
Pavel Labathb9cc0c72015-08-24 09:22:04 +0000270 GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size);
Todd Fialaaf245d12014-06-30 21:05:18 +0000271
272 Error
Pavel Labathb9cc0c72015-08-24 09:22:04 +0000273 FixupBreakpointPCAsNeeded(NativeThreadLinux &thread);
Todd Fialaaf245d12014-06-30 21:05:18 +0000274
275 /// Writes a siginfo_t structure corresponding to the given thread ID to the
276 /// memory region pointed to by @p siginfo.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000277 Error
278 GetSignalInfo(lldb::tid_t tid, void *siginfo);
Todd Fialaaf245d12014-06-30 21:05:18 +0000279
280 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
281 /// corresponding to the given thread ID to the memory pointed to by @p
282 /// message.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000283 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000284 GetEventMessage(lldb::tid_t tid, unsigned long *message);
285
Todd Fiala511e5cd2014-09-11 23:29:14 +0000286 void
Chaoren Linfa03ad22015-02-03 01:50:42 +0000287 NotifyThreadDeath (lldb::tid_t tid);
288
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000289 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000290 Detach(lldb::tid_t tid);
Chaoren Lin86fd8e42015-02-03 01:51:15 +0000291
Pavel Labathc0765592015-05-06 10:46:34 +0000292
Pavel Labath1dbc6c92015-05-12 08:35:33 +0000293 // This method is requests a stop on all threads which are still running. It sets up a
294 // deferred delegate notification, which will fire once threads report as stopped. The
295 // triggerring_tid will be set as the current thread (main stop reason).
Pavel Labathc0765592015-05-06 10:46:34 +0000296 void
Pavel Labath337f3eb2015-05-08 08:57:45 +0000297 StopRunningThreads(lldb::tid_t triggering_tid);
Pavel Labathc0765592015-05-06 10:46:34 +0000298
Pavel Labath9eb1ecb2015-05-15 13:49:01 +0000299 // Notify the delegate if all threads have stopped.
300 void SignalIfAllThreadsStopped();
Pavel Labathc0765592015-05-06 10:46:34 +0000301
Pavel Labath0e1d7292015-08-20 09:06:12 +0000302 // Resume the given thread, optionally passing it the given signal. The type of resume
303 // operation (continue, single-step) depends on the state parameter.
Pavel Labath5eb721e2015-05-07 08:30:31 +0000304 Error
Pavel Labathb9cc0c72015-08-24 09:22:04 +0000305 ResumeThread(NativeThreadLinux &thread, lldb::StateType state, int signo);
Pavel Labathc0765592015-05-06 10:46:34 +0000306
307 void
Pavel Labathf9077782015-08-21 09:13:53 +0000308 ThreadWasCreated(NativeThreadLinux &thread);
Pavel Labathc0765592015-05-06 10:46:34 +0000309
Pavel Labath19cbe962015-07-21 13:20:32 +0000310 void
311 SigchldHandler();
Todd Fialaaf245d12014-06-30 21:05:18 +0000312 };
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000313
314} // namespace process_linux
315} // namespace lldb_private
Todd Fialaaf245d12014-06-30 21:05:18 +0000316
317#endif // #ifndef liblldb_NativeProcessLinux_H_