Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1 | //===-- 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 |
| 18 | #include <unordered_set> |
| 19 | |
| 20 | // Other libraries and framework includes |
| 21 | #include "lldb/Core/ArchSpec.h" |
| 22 | #include "lldb/lldb-types.h" |
| 23 | #include "lldb/Host/Debug.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 24 | #include "lldb/Host/HostThread.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 25 | #include "lldb/Host/Mutex.h" |
| 26 | #include "lldb/Target/MemoryRegionInfo.h" |
| 27 | |
Chaoren Lin | 2fe1d0a | 2015-02-03 01:51:38 +0000 | [diff] [blame^] | 28 | #include "lldb/Host/common/NativeProcessProtocol.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 29 | |
| 30 | namespace lldb_private |
| 31 | { |
| 32 | class Error; |
| 33 | class Module; |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 34 | class ThreadStateCoordinator; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 35 | class Scalar; |
| 36 | |
| 37 | /// @class NativeProcessLinux |
| 38 | /// @brief Manages communication with the inferior (debugee) process. |
| 39 | /// |
| 40 | /// Upon construction, this class prepares and launches an inferior process for |
| 41 | /// debugging. |
| 42 | /// |
| 43 | /// Changes in the inferior process state are broadcasted. |
| 44 | class NativeProcessLinux: public NativeProcessProtocol |
| 45 | { |
| 46 | public: |
| 47 | |
| 48 | // --------------------------------------------------------------------- |
| 49 | // Public Static Methods |
| 50 | // --------------------------------------------------------------------- |
| 51 | static lldb_private::Error |
| 52 | LaunchProcess ( |
| 53 | Module *exe_module, |
| 54 | ProcessLaunchInfo &launch_info, |
| 55 | lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate, |
| 56 | NativeProcessProtocolSP &native_process_sp); |
| 57 | |
| 58 | static lldb_private::Error |
| 59 | AttachToProcess ( |
| 60 | lldb::pid_t pid, |
| 61 | lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate, |
| 62 | NativeProcessProtocolSP &native_process_sp); |
| 63 | |
| 64 | // --------------------------------------------------------------------- |
| 65 | // Public Instance Methods |
| 66 | // --------------------------------------------------------------------- |
| 67 | |
| 68 | ~NativeProcessLinux() override; |
| 69 | |
| 70 | // --------------------------------------------------------------------- |
| 71 | // NativeProcessProtocol Interface |
| 72 | // --------------------------------------------------------------------- |
| 73 | Error |
| 74 | Resume (const ResumeActionList &resume_actions) override; |
| 75 | |
| 76 | Error |
| 77 | Halt () override; |
| 78 | |
| 79 | Error |
| 80 | Detach () override; |
| 81 | |
| 82 | Error |
| 83 | Signal (int signo) override; |
| 84 | |
| 85 | Error |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 86 | Interrupt () override; |
| 87 | |
| 88 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 89 | Kill () override; |
| 90 | |
| 91 | Error |
| 92 | GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override; |
| 93 | |
| 94 | Error |
| 95 | ReadMemory (lldb::addr_t addr, void *buf, lldb::addr_t size, lldb::addr_t &bytes_read) override; |
| 96 | |
| 97 | Error |
| 98 | WriteMemory (lldb::addr_t addr, const void *buf, lldb::addr_t size, lldb::addr_t &bytes_written) override; |
| 99 | |
| 100 | Error |
| 101 | AllocateMemory (lldb::addr_t size, uint32_t permissions, lldb::addr_t &addr) override; |
| 102 | |
| 103 | Error |
| 104 | DeallocateMemory (lldb::addr_t addr) override; |
| 105 | |
| 106 | lldb::addr_t |
| 107 | GetSharedLibraryInfoAddress () override; |
| 108 | |
| 109 | size_t |
| 110 | UpdateThreads () override; |
| 111 | |
| 112 | bool |
| 113 | GetArchitecture (ArchSpec &arch) const override; |
| 114 | |
| 115 | Error |
| 116 | SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override; |
| 117 | |
| 118 | void |
| 119 | DoStopIDBumped (uint32_t newBumpId) override; |
| 120 | |
| 121 | // --------------------------------------------------------------------- |
| 122 | // Interface used by NativeRegisterContext-derived classes. |
| 123 | // --------------------------------------------------------------------- |
| 124 | |
| 125 | /// Reads the contents from the register identified by the given (architecture |
| 126 | /// dependent) offset. |
| 127 | /// |
| 128 | /// This method is provided for use by RegisterContextLinux derivatives. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 129 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 130 | ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name, |
| 131 | unsigned size, lldb_private::RegisterValue &value); |
| 132 | |
| 133 | /// Writes the given value to the register identified by the given |
| 134 | /// (architecture dependent) offset. |
| 135 | /// |
| 136 | /// This method is provided for use by RegisterContextLinux derivatives. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 137 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 138 | WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name, |
| 139 | const lldb_private::RegisterValue &value); |
| 140 | |
| 141 | /// Reads all general purpose registers into the specified buffer. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 142 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 143 | ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size); |
| 144 | |
| 145 | /// Reads generic floating point registers into the specified buffer. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 146 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 147 | ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size); |
| 148 | |
| 149 | /// Reads the specified register set into the specified buffer. |
| 150 | /// For instance, the extended floating-point register set. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 151 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 152 | ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset); |
| 153 | |
| 154 | /// Writes all general purpose registers into the specified buffer. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 155 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 156 | WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size); |
| 157 | |
| 158 | /// Writes generic floating point registers into the specified buffer. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 159 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 160 | WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size); |
| 161 | |
| 162 | /// Writes the specified register set into the specified buffer. |
| 163 | /// For instance, the extended floating-point register set. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 164 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 165 | WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset); |
| 166 | |
| 167 | protected: |
| 168 | // --------------------------------------------------------------------- |
| 169 | // NativeProcessProtocol protected interface |
| 170 | // --------------------------------------------------------------------- |
| 171 | Error |
| 172 | GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override; |
| 173 | |
| 174 | private: |
| 175 | |
| 176 | lldb_private::ArchSpec m_arch; |
| 177 | |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 178 | HostThread m_operation_thread; |
| 179 | HostThread m_monitor_thread; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 180 | |
| 181 | // current operation which must be executed on the priviliged thread |
| 182 | void *m_operation; |
| 183 | lldb_private::Mutex m_operation_mutex; |
| 184 | |
| 185 | // semaphores notified when Operation is ready to be processed and when |
| 186 | // the operation is complete. |
| 187 | sem_t m_operation_pending; |
| 188 | sem_t m_operation_done; |
| 189 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 190 | lldb_private::LazyBool m_supports_mem_region; |
| 191 | std::vector<MemoryRegionInfo> m_mem_region_cache; |
| 192 | lldb_private::Mutex m_mem_region_cache_mutex; |
| 193 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 194 | std::unique_ptr<ThreadStateCoordinator> m_coordinator_up; |
| 195 | HostThread m_coordinator_thread; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 196 | |
| 197 | struct OperationArgs |
| 198 | { |
| 199 | OperationArgs(NativeProcessLinux *monitor); |
| 200 | |
| 201 | ~OperationArgs(); |
| 202 | |
| 203 | NativeProcessLinux *m_monitor; // The monitor performing the attach. |
| 204 | sem_t m_semaphore; // Posted to once operation complete. |
| 205 | lldb_private::Error m_error; // Set if process operation failed. |
| 206 | }; |
| 207 | |
| 208 | /// @class LauchArgs |
| 209 | /// |
| 210 | /// @brief Simple structure to pass data to the thread responsible for |
| 211 | /// launching a child process. |
| 212 | struct LaunchArgs : OperationArgs |
| 213 | { |
| 214 | LaunchArgs(NativeProcessLinux *monitor, |
| 215 | lldb_private::Module *module, |
| 216 | char const **argv, |
| 217 | char const **envp, |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 218 | const std::string &stdin_path, |
| 219 | const std::string &stdout_path, |
| 220 | const std::string &stderr_path, |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 221 | const char *working_dir, |
| 222 | const lldb_private::ProcessLaunchInfo &launch_info); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 223 | |
| 224 | ~LaunchArgs(); |
| 225 | |
| 226 | lldb_private::Module *m_module; // The executable image to launch. |
| 227 | char const **m_argv; // Process arguments. |
| 228 | char const **m_envp; // Process environment. |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 229 | 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. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 232 | const char *m_working_dir; // Working directory or NULL. |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 233 | const lldb_private::ProcessLaunchInfo &m_launch_info; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | struct AttachArgs : OperationArgs |
| 237 | { |
| 238 | AttachArgs(NativeProcessLinux *monitor, |
| 239 | lldb::pid_t pid); |
| 240 | |
| 241 | ~AttachArgs(); |
| 242 | |
| 243 | lldb::pid_t m_pid; // pid of the process to be attached. |
| 244 | }; |
| 245 | |
| 246 | // --------------------------------------------------------------------- |
| 247 | // Private Instance Methods |
| 248 | // --------------------------------------------------------------------- |
| 249 | NativeProcessLinux (); |
| 250 | |
| 251 | /// Launches an inferior process ready for debugging. Forms the |
| 252 | /// implementation of Process::DoLaunch. |
| 253 | void |
| 254 | LaunchInferior ( |
| 255 | Module *module, |
| 256 | char const *argv[], |
| 257 | char const *envp[], |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 258 | const std::string &stdin_path, |
| 259 | const std::string &stdout_path, |
| 260 | const std::string &stderr_path, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 261 | const char *working_dir, |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 262 | const lldb_private::ProcessLaunchInfo &launch_info, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 263 | Error &error); |
| 264 | |
| 265 | /// Attaches to an existing process. Forms the |
| 266 | /// implementation of Process::DoLaunch. |
| 267 | void |
| 268 | AttachToInferior (lldb::pid_t pid, Error &error); |
| 269 | |
| 270 | void |
| 271 | StartLaunchOpThread(LaunchArgs *args, lldb_private::Error &error); |
| 272 | |
| 273 | static void * |
| 274 | LaunchOpThread(void *arg); |
| 275 | |
| 276 | static bool |
| 277 | Launch(LaunchArgs *args); |
| 278 | |
| 279 | void |
| 280 | StartAttachOpThread(AttachArgs *args, lldb_private::Error &error); |
| 281 | |
| 282 | static void * |
| 283 | AttachOpThread(void *args); |
| 284 | |
| 285 | static bool |
| 286 | Attach(AttachArgs *args); |
| 287 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 288 | static Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 289 | SetDefaultPtraceOpts(const lldb::pid_t); |
| 290 | |
| 291 | static void |
| 292 | ServeOperation(OperationArgs *args); |
| 293 | |
| 294 | static bool |
| 295 | DupDescriptor(const char *path, int fd, int flags); |
| 296 | |
| 297 | static bool |
| 298 | MonitorCallback(void *callback_baton, |
| 299 | lldb::pid_t pid, bool exited, int signal, int status); |
| 300 | |
| 301 | void |
| 302 | MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid); |
| 303 | |
| 304 | void |
| 305 | MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited); |
| 306 | |
| 307 | #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 | |
| 321 | void |
| 322 | DoOperation(void *op); |
| 323 | |
| 324 | /// Stops the child monitor thread. |
| 325 | void |
| 326 | StopMonitoringChildProcess(); |
| 327 | |
| 328 | /// Stops the operation thread used to attach/launch a process. |
| 329 | void |
| 330 | StopOpThread(); |
| 331 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 332 | Error |
| 333 | StartCoordinatorThread (); |
| 334 | |
| 335 | static void* |
| 336 | CoordinatorThread (void *arg); |
| 337 | |
| 338 | void |
| 339 | StopCoordinatorThread (); |
| 340 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 341 | /// Stops monitoring the child process thread. |
| 342 | void |
| 343 | StopMonitor(); |
| 344 | |
| 345 | bool |
| 346 | HasThreadNoLock (lldb::tid_t thread_id); |
| 347 | |
| 348 | NativeThreadProtocolSP |
| 349 | MaybeGetThreadNoLock (lldb::tid_t thread_id); |
| 350 | |
| 351 | bool |
| 352 | StopTrackingThread (lldb::tid_t thread_id); |
| 353 | |
| 354 | NativeThreadProtocolSP |
| 355 | AddThread (lldb::tid_t thread_id); |
| 356 | |
| 357 | NativeThreadProtocolSP |
| 358 | GetOrCreateThread (lldb::tid_t thread_id, bool &created); |
| 359 | |
| 360 | Error |
| 361 | GetSoftwareBreakpointSize (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size); |
| 362 | |
| 363 | Error |
| 364 | FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp); |
| 365 | |
| 366 | /// Writes a siginfo_t structure corresponding to the given thread ID to the |
| 367 | /// memory region pointed to by @p siginfo. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 368 | Error |
| 369 | GetSignalInfo(lldb::tid_t tid, void *siginfo); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 370 | |
| 371 | /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) |
| 372 | /// corresponding to the given thread ID to the memory pointed to by @p |
| 373 | /// message. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 374 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 375 | GetEventMessage(lldb::tid_t tid, unsigned long *message); |
| 376 | |
| 377 | /// Resumes the given thread. If @p signo is anything but |
| 378 | /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 379 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 380 | Resume(lldb::tid_t tid, uint32_t signo); |
| 381 | |
| 382 | /// Single steps the given thread. If @p signo is anything but |
| 383 | /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 384 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 385 | SingleStep(lldb::tid_t tid, uint32_t signo); |
| 386 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 387 | // ThreadStateCoordinator helper methods. |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 388 | void |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 389 | NotifyThreadCreateStopped (lldb::tid_t tid); |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 390 | |
| 391 | void |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 392 | NotifyThreadCreateRunning (lldb::tid_t tid); |
| 393 | |
| 394 | void |
| 395 | NotifyThreadDeath (lldb::tid_t tid); |
| 396 | |
| 397 | void |
| 398 | NotifyThreadStop (lldb::tid_t tid); |
| 399 | |
| 400 | void |
| 401 | CallAfterRunningThreadsStop (lldb::tid_t tid, |
| 402 | const std::function<void (lldb::tid_t tid)> &call_after_function); |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 403 | |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 404 | void |
| 405 | CallAfterRunningThreadsStopWithSkipTID (lldb::tid_t deferred_signal_tid, |
| 406 | lldb::tid_t skip_stop_request_tid, |
| 407 | const std::function<void (lldb::tid_t tid)> &call_after_function); |
| 408 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 409 | lldb_private::Error |
| 410 | Detach(lldb::tid_t tid); |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 411 | |
| 412 | lldb_private::Error |
| 413 | RequestThreadStop (const lldb::pid_t pid, const lldb::tid_t tid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 414 | }; |
| 415 | } // End lldb_private namespace. |
| 416 | |
| 417 | #endif // #ifndef liblldb_NativeProcessLinux_H_ |