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