blob: c335f86aab0c7910828a60a47fb95d2ce8cd9adc [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;
Todd Fialaaf245d12014-06-30 21:05:18 +000029 class Scalar;
30
Tamas Berghammerdb264a62015-03-31 09:52:22 +000031namespace process_linux {
Todd Fialaaf245d12014-06-30 21:05:18 +000032 /// @class NativeProcessLinux
33 /// @brief Manages communication with the inferior (debugee) process.
34 ///
35 /// Upon construction, this class prepares and launches an inferior process for
36 /// debugging.
37 ///
38 /// Changes in the inferior process state are broadcasted.
39 class NativeProcessLinux: public NativeProcessProtocol
40 {
Pavel Labathd5b310f2015-07-09 11:51:11 +000041 friend Error
42 NativeProcessProtocol::Launch (ProcessLaunchInfo &launch_info,
43 NativeDelegate &native_delegate,
Pavel Labath19cbe962015-07-21 13:20:32 +000044 MainLoop &mainloop,
Pavel Labathd5b310f2015-07-09 11:51:11 +000045 NativeProcessProtocolSP &process_sp);
46
47 friend Error
48 NativeProcessProtocol::Attach (lldb::pid_t pid,
Pavel Labath19cbe962015-07-21 13:20:32 +000049 NativeProcessProtocol::NativeDelegate &native_delegate,
50 MainLoop &mainloop,
51 NativeProcessProtocolSP &process_sp);
Pavel Labathd5b310f2015-07-09 11:51:11 +000052
Sean Callananc307c272015-07-08 16:33:46 +000053 public:
Todd Fialaaf245d12014-06-30 21:05:18 +000054 // ---------------------------------------------------------------------
Todd Fialaaf245d12014-06-30 21:05:18 +000055 // NativeProcessProtocol Interface
56 // ---------------------------------------------------------------------
57 Error
58 Resume (const ResumeActionList &resume_actions) override;
59
60 Error
61 Halt () override;
62
63 Error
64 Detach () override;
65
66 Error
67 Signal (int signo) override;
68
69 Error
Chaoren Line9547b82015-02-03 01:51:00 +000070 Interrupt () override;
71
72 Error
Todd Fialaaf245d12014-06-30 21:05:18 +000073 Kill () override;
74
75 Error
76 GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override;
77
78 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +000079 ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000080
81 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +000082 ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000083
84 Error
Chaoren Lin3eb4b452015-04-29 17:24:48 +000085 WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) override;
86
87 Error
88 AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) override;
Todd Fialaaf245d12014-06-30 21:05:18 +000089
90 Error
91 DeallocateMemory (lldb::addr_t addr) override;
92
93 lldb::addr_t
94 GetSharedLibraryInfoAddress () override;
95
96 size_t
97 UpdateThreads () override;
98
99 bool
100 GetArchitecture (ArchSpec &arch) const override;
101
102 Error
103 SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override;
104
105 void
106 DoStopIDBumped (uint32_t newBumpId) override;
107
Tamas Berghammer068f8a72015-05-26 11:58:52 +0000108 Error
109 GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) override;
110
Tamas Berghammer783bfc82015-06-18 20:43:56 +0000111 Error
112 GetFileLoadAddress(const llvm::StringRef& file_name, lldb::addr_t& load_addr) override;
113
Pavel Labathf9077782015-08-21 09:13:53 +0000114 NativeThreadLinuxSP
115 GetThreadByID(lldb::tid_t id);
116
Todd Fialaaf245d12014-06-30 21:05:18 +0000117 // ---------------------------------------------------------------------
118 // Interface used by NativeRegisterContext-derived classes.
119 // ---------------------------------------------------------------------
Pavel Labath4a9babb2015-06-30 17:04:49 +0000120 static Error
121 PtraceWrapper(int req,
122 lldb::pid_t pid,
123 void *addr = nullptr,
124 void *data = nullptr,
125 size_t data_size = 0,
126 long *result = nullptr);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +0000127
Pavel Labath605b51b2016-02-23 13:56:30 +0000128 bool
129 SupportHardwareSingleStepping() const;
130
Todd Fialaaf245d12014-06-30 21:05:18 +0000131 protected:
132 // ---------------------------------------------------------------------
133 // NativeProcessProtocol protected interface
134 // ---------------------------------------------------------------------
135 Error
136 GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override;
137
138 private:
139
Pavel Labath19cbe962015-07-21 13:20:32 +0000140 MainLoop::SignalHandleUP m_sigchld_handle;
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000141 ArchSpec m_arch;
Todd Fialaaf245d12014-06-30 21:05:18 +0000142
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000143 LazyBool m_supports_mem_region;
Todd Fialaaf245d12014-06-30 21:05:18 +0000144 std::vector<MemoryRegionInfo> m_mem_region_cache;
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 Labath2a86b552016-06-14 17:30:52 +0000158 LaunchArgs(char const **argv, char const **envp, const FileSpec &stdin_file_spec,
159 const FileSpec &stdout_file_spec, const FileSpec &stderr_file_spec, const FileSpec &working_dir,
160 const ProcessLaunchInfo &launch_info);
Todd Fialaaf245d12014-06-30 21:05:18 +0000161
162 ~LaunchArgs();
163
Chaoren Lind3173f32015-05-29 19:52:29 +0000164 char const **m_argv; // Process arguments.
165 char const **m_envp; // Process environment.
166 const FileSpec m_stdin_file_spec; // Redirect stdin if not empty.
167 const FileSpec m_stdout_file_spec; // Redirect stdout if not empty.
168 const FileSpec m_stderr_file_spec; // Redirect stderr if not empty.
169 const FileSpec m_working_dir; // Working directory or empty.
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000170 const ProcessLaunchInfo &m_launch_info;
Todd Fialaaf245d12014-06-30 21:05:18 +0000171 };
172
Omair Javaid20405482015-08-09 19:04:41 +0000173 typedef std::function< ::pid_t(Error &)> InitialOperation;
Todd Fialaaf245d12014-06-30 21:05:18 +0000174
175 // ---------------------------------------------------------------------
176 // Private Instance Methods
177 // ---------------------------------------------------------------------
178 NativeProcessLinux ();
179
180 /// Launches an inferior process ready for debugging. Forms the
181 /// implementation of Process::DoLaunch.
182 void
183 LaunchInferior (
Pavel Labath19cbe962015-07-21 13:20:32 +0000184 MainLoop &mainloop,
Todd Fialaaf245d12014-06-30 21:05:18 +0000185 char const *argv[],
186 char const *envp[],
Chaoren Lind3173f32015-05-29 19:52:29 +0000187 const FileSpec &stdin_file_spec,
188 const FileSpec &stdout_file_spec,
189 const FileSpec &stderr_file_spec,
190 const FileSpec &working_dir,
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000191 const ProcessLaunchInfo &launch_info,
Todd Fialaaf245d12014-06-30 21:05:18 +0000192 Error &error);
193
194 /// Attaches to an existing process. Forms the
Tamas Berghammer0cbf0b12015-03-13 11:16:03 +0000195 /// implementation of Process::DoAttach
Todd Fialaaf245d12014-06-30 21:05:18 +0000196 void
Pavel Labath19cbe962015-07-21 13:20:32 +0000197 AttachToInferior (MainLoop &mainloop, lldb::pid_t pid, Error &error);
Pavel Labath1107b5a2015-04-17 14:07:49 +0000198
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000199 ::pid_t
200 Launch(LaunchArgs *args, Error &error);
Todd Fialaaf245d12014-06-30 21:05:18 +0000201
Pavel Labathbd7cbc52015-04-20 13:53:49 +0000202 ::pid_t
203 Attach(lldb::pid_t pid, Error &error);
Todd Fialaaf245d12014-06-30 21:05:18 +0000204
Chaoren Lin97ccc292015-02-03 01:51:12 +0000205 static Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000206 SetDefaultPtraceOpts(const lldb::pid_t);
207
Todd Fialaaf245d12014-06-30 21:05:18 +0000208 static bool
Chaoren Lind3173f32015-05-29 19:52:29 +0000209 DupDescriptor(const FileSpec &file_spec, int fd, int flags);
Todd Fialaaf245d12014-06-30 21:05:18 +0000210
Pavel Labath1107b5a2015-04-17 14:07:49 +0000211 static void *
212 MonitorThread(void *baton);
213
214 void
215 MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status);
Todd Fialaaf245d12014-06-30 21:05:18 +0000216
217 void
Pavel Labath426bdf82015-04-28 07:51:52 +0000218 WaitForNewThread(::pid_t tid);
219
220 void
Pavel Labathb9cc0c72015-08-24 09:22:04 +0000221 MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread);
Todd Fialaaf245d12014-06-30 21:05:18 +0000222
223 void
Pavel Labathb9cc0c72015-08-24 09:22:04 +0000224 MonitorTrace(NativeThreadLinux &thread);
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000225
226 void
Pavel Labathb9cc0c72015-08-24 09:22:04 +0000227 MonitorBreakpoint(NativeThreadLinux &thread);
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000228
229 void
Pavel Labathf9077782015-08-21 09:13:53 +0000230 MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index);
Chaoren Linc16f5dc2015-03-19 23:28:10 +0000231
232 void
Pavel Labathb9cc0c72015-08-24 09:22:04 +0000233 MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread, bool exited);
Todd Fialaaf245d12014-06-30 21:05:18 +0000234
Tamas Berghammere7708682015-04-22 10:00:23 +0000235 Error
Pavel Labathb9cc0c72015-08-24 09:22:04 +0000236 SetupSoftwareSingleStepping(NativeThreadLinux &thread);
Tamas Berghammere7708682015-04-22 10:00:23 +0000237
Todd Fialaaf245d12014-06-30 21:05:18 +0000238#if 0
239 static ::ProcessMessage::CrashReason
240 GetCrashReasonForSIGSEGV(const siginfo_t *info);
241
242 static ::ProcessMessage::CrashReason
243 GetCrashReasonForSIGILL(const siginfo_t *info);
244
245 static ::ProcessMessage::CrashReason
246 GetCrashReasonForSIGFPE(const siginfo_t *info);
247
248 static ::ProcessMessage::CrashReason
249 GetCrashReasonForSIGBUS(const siginfo_t *info);
250#endif
251
Todd Fialaaf245d12014-06-30 21:05:18 +0000252 bool
253 HasThreadNoLock (lldb::tid_t thread_id);
254
Todd Fialaaf245d12014-06-30 21:05:18 +0000255 bool
256 StopTrackingThread (lldb::tid_t thread_id);
257
Pavel Labathf9077782015-08-21 09:13:53 +0000258 NativeThreadLinuxSP
Todd Fialaaf245d12014-06-30 21:05:18 +0000259 AddThread (lldb::tid_t thread_id);
260
Todd Fialaaf245d12014-06-30 21:05:18 +0000261 Error
Pavel Labathb9cc0c72015-08-24 09:22:04 +0000262 GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size);
Todd Fialaaf245d12014-06-30 21:05:18 +0000263
264 Error
Pavel Labathb9cc0c72015-08-24 09:22:04 +0000265 FixupBreakpointPCAsNeeded(NativeThreadLinux &thread);
Todd Fialaaf245d12014-06-30 21:05:18 +0000266
267 /// Writes a siginfo_t structure corresponding to the given thread ID to the
268 /// memory region pointed to by @p siginfo.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000269 Error
270 GetSignalInfo(lldb::tid_t tid, void *siginfo);
Todd Fialaaf245d12014-06-30 21:05:18 +0000271
272 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG)
273 /// corresponding to the given thread ID to the memory pointed to by @p
274 /// message.
Chaoren Lin97ccc292015-02-03 01:51:12 +0000275 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000276 GetEventMessage(lldb::tid_t tid, unsigned long *message);
277
Todd Fiala511e5cd2014-09-11 23:29:14 +0000278 void
Chaoren Linfa03ad22015-02-03 01:50:42 +0000279 NotifyThreadDeath (lldb::tid_t tid);
280
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000281 Error
Todd Fialaaf245d12014-06-30 21:05:18 +0000282 Detach(lldb::tid_t tid);
Chaoren Lin86fd8e42015-02-03 01:51:15 +0000283
Pavel Labathc0765592015-05-06 10:46:34 +0000284
Pavel Labath1dbc6c92015-05-12 08:35:33 +0000285 // This method is requests a stop on all threads which are still running. It sets up a
286 // deferred delegate notification, which will fire once threads report as stopped. The
287 // triggerring_tid will be set as the current thread (main stop reason).
Pavel Labathc0765592015-05-06 10:46:34 +0000288 void
Pavel Labath337f3eb2015-05-08 08:57:45 +0000289 StopRunningThreads(lldb::tid_t triggering_tid);
Pavel Labathc0765592015-05-06 10:46:34 +0000290
Pavel Labath9eb1ecb2015-05-15 13:49:01 +0000291 // Notify the delegate if all threads have stopped.
292 void SignalIfAllThreadsStopped();
Pavel Labathc0765592015-05-06 10:46:34 +0000293
Pavel Labath0e1d7292015-08-20 09:06:12 +0000294 // Resume the given thread, optionally passing it the given signal. The type of resume
295 // operation (continue, single-step) depends on the state parameter.
Pavel Labath5eb721e2015-05-07 08:30:31 +0000296 Error
Pavel Labathb9cc0c72015-08-24 09:22:04 +0000297 ResumeThread(NativeThreadLinux &thread, lldb::StateType state, int signo);
Pavel Labathc0765592015-05-06 10:46:34 +0000298
299 void
Pavel Labathf9077782015-08-21 09:13:53 +0000300 ThreadWasCreated(NativeThreadLinux &thread);
Pavel Labathc0765592015-05-06 10:46:34 +0000301
Pavel Labath19cbe962015-07-21 13:20:32 +0000302 void
303 SigchldHandler();
Todd Fialaaf245d12014-06-30 21:05:18 +0000304 };
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000305
306} // namespace process_linux
307} // namespace lldb_private
Todd Fialaaf245d12014-06-30 21:05:18 +0000308
309#endif // #ifndef liblldb_NativeProcessLinux_H_