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