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 <mutex> 19 #include <unordered_map> 20 #include <unordered_set> 21 22 // Other libraries and framework includes 23 #include "lldb/Core/ArchSpec.h" 24 #include "lldb/lldb-types.h" 25 #include "lldb/Host/Debug.h" 26 #include "lldb/Host/HostThread.h" 27 #include "lldb/Host/Mutex.h" 28 #include "lldb/Target/MemoryRegionInfo.h" 29 30 #include "lldb/Host/common/NativeProcessProtocol.h" 31 #include "NativeThreadLinux.h" 32 33 namespace lldb_private { 34 class Error; 35 class Module; 36 class Scalar; 37 38 namespace process_linux { 39 /// @class NativeProcessLinux 40 /// @brief Manages communication with the inferior (debugee) process. 41 /// 42 /// Upon construction, this class prepares and launches an inferior process for 43 /// debugging. 44 /// 45 /// Changes in the inferior process state are broadcasted. 46 class NativeProcessLinux: public NativeProcessProtocol 47 { 48 public: 49 50 static Error 51 LaunchProcess ( 52 Module *exe_module, 53 ProcessLaunchInfo &launch_info, 54 NativeProcessProtocol::NativeDelegate &native_delegate, 55 NativeProcessProtocolSP &native_process_sp); 56 57 static Error 58 AttachToProcess ( 59 lldb::pid_t pid, 60 NativeProcessProtocol::NativeDelegate &native_delegate, 61 NativeProcessProtocolSP &native_process_sp); 62 63 //------------------------------------------------------------------------------ 64 /// @class Operation 65 /// @brief Represents a NativeProcessLinux operation. 66 /// 67 /// Under Linux, it is not possible to ptrace() from any other thread but the 68 /// one that spawned or attached to the process from the start. Therefore, when 69 /// a NativeProcessLinux is asked to deliver or change the state of an inferior 70 /// process the operation must be "funneled" to a specific thread to perform the 71 /// task. The Operation class provides an abstract base for all services the 72 /// NativeProcessLinux must perform via the single virtual function Execute, thus 73 /// encapsulating the code that needs to run in the privileged context. 74 class Operation 75 { 76 public: 77 Operation () : m_error() { } 78 79 virtual 80 ~Operation() {} 81 82 virtual void 83 Execute (NativeProcessLinux *process) = 0; 84 85 const Error & 86 GetError () const { return m_error; } 87 88 protected: 89 Error m_error; 90 }; 91 92 typedef std::unique_ptr<Operation> OperationUP; 93 94 // --------------------------------------------------------------------- 95 // NativeProcessProtocol Interface 96 // --------------------------------------------------------------------- 97 Error 98 Resume (const ResumeActionList &resume_actions) override; 99 100 Error 101 Halt () override; 102 103 Error 104 Detach () override; 105 106 Error 107 Signal (int signo) override; 108 109 Error 110 Interrupt () override; 111 112 Error 113 Kill () override; 114 115 Error 116 GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override; 117 118 Error 119 ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override; 120 121 Error 122 ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override; 123 124 Error 125 WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) override; 126 127 Error 128 AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) override; 129 130 Error 131 DeallocateMemory (lldb::addr_t addr) override; 132 133 lldb::addr_t 134 GetSharedLibraryInfoAddress () override; 135 136 size_t 137 UpdateThreads () override; 138 139 bool 140 GetArchitecture (ArchSpec &arch) const override; 141 142 Error 143 SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override; 144 145 Error 146 SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) override; 147 148 Error 149 RemoveWatchpoint (lldb::addr_t addr) override; 150 151 void 152 DoStopIDBumped (uint32_t newBumpId) override; 153 154 void 155 Terminate () override; 156 157 Error 158 GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) override; 159 160 // --------------------------------------------------------------------- 161 // Interface used by NativeRegisterContext-derived classes. 162 // --------------------------------------------------------------------- 163 Error 164 DoOperation(Operation* op); 165 166 Error 167 DoOperation(OperationUP op) { return DoOperation(op.get()); } 168 169 static long 170 PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, Error& error); 171 172 protected: 173 // --------------------------------------------------------------------- 174 // NativeProcessProtocol protected interface 175 // --------------------------------------------------------------------- 176 Error 177 GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override; 178 179 private: 180 181 class Monitor; 182 183 ArchSpec m_arch; 184 185 std::unique_ptr<Monitor> m_monitor_up; 186 187 LazyBool m_supports_mem_region; 188 std::vector<MemoryRegionInfo> m_mem_region_cache; 189 Mutex m_mem_region_cache_mutex; 190 191 // List of thread ids stepping with a breakpoint with the address of 192 // the relevan breakpoint 193 std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint; 194 195 /// @class LauchArgs 196 /// 197 /// @brief Simple structure to pass data to the thread responsible for 198 /// launching a child process. 199 struct LaunchArgs 200 { 201 LaunchArgs(Module *module, 202 char const **argv, 203 char const **envp, 204 const std::string &stdin_path, 205 const std::string &stdout_path, 206 const std::string &stderr_path, 207 const char *working_dir, 208 const ProcessLaunchInfo &launch_info); 209 210 ~LaunchArgs(); 211 212 Module *m_module; // The executable image to launch. 213 char const **m_argv; // Process arguments. 214 char const **m_envp; // Process environment. 215 const std::string &m_stdin_path; // Redirect stdin if not empty. 216 const std::string &m_stdout_path; // Redirect stdout if not empty. 217 const std::string &m_stderr_path; // Redirect stderr if not empty. 218 const char *m_working_dir; // Working directory or NULL. 219 const ProcessLaunchInfo &m_launch_info; 220 }; 221 222 typedef std::function<::pid_t(Error &)> InitialOperation; 223 224 // --------------------------------------------------------------------- 225 // Private Instance Methods 226 // --------------------------------------------------------------------- 227 NativeProcessLinux (); 228 229 /// Launches an inferior process ready for debugging. Forms the 230 /// implementation of Process::DoLaunch. 231 void 232 LaunchInferior ( 233 Module *module, 234 char const *argv[], 235 char const *envp[], 236 const std::string &stdin_path, 237 const std::string &stdout_path, 238 const std::string &stderr_path, 239 const char *working_dir, 240 const ProcessLaunchInfo &launch_info, 241 Error &error); 242 243 /// Attaches to an existing process. Forms the 244 /// implementation of Process::DoAttach 245 void 246 AttachToInferior (lldb::pid_t pid, Error &error); 247 248 void 249 StartMonitorThread(const InitialOperation &operation, Error &error); 250 251 ::pid_t 252 Launch(LaunchArgs *args, Error &error); 253 254 ::pid_t 255 Attach(lldb::pid_t pid, Error &error); 256 257 static Error 258 SetDefaultPtraceOpts(const lldb::pid_t); 259 260 static bool 261 DupDescriptor(const char *path, int fd, int flags); 262 263 static void * 264 MonitorThread(void *baton); 265 266 void 267 MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status); 268 269 void 270 WaitForNewThread(::pid_t tid); 271 272 void 273 MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid); 274 275 void 276 MonitorTrace(lldb::pid_t pid, NativeThreadProtocolSP thread_sp); 277 278 void 279 MonitorBreakpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp); 280 281 void 282 MonitorWatchpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp, uint32_t wp_index); 283 284 void 285 MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited); 286 287 bool 288 SupportHardwareSingleStepping() const; 289 290 Error 291 SetupSoftwareSingleStepping(NativeThreadProtocolSP thread_sp); 292 293 #if 0 294 static ::ProcessMessage::CrashReason 295 GetCrashReasonForSIGSEGV(const siginfo_t *info); 296 297 static ::ProcessMessage::CrashReason 298 GetCrashReasonForSIGILL(const siginfo_t *info); 299 300 static ::ProcessMessage::CrashReason 301 GetCrashReasonForSIGFPE(const siginfo_t *info); 302 303 static ::ProcessMessage::CrashReason 304 GetCrashReasonForSIGBUS(const siginfo_t *info); 305 #endif 306 307 bool 308 HasThreadNoLock (lldb::tid_t thread_id); 309 310 NativeThreadProtocolSP 311 MaybeGetThreadNoLock (lldb::tid_t thread_id); 312 313 bool 314 StopTrackingThread (lldb::tid_t thread_id); 315 316 NativeThreadProtocolSP 317 AddThread (lldb::tid_t thread_id); 318 319 Error 320 GetSoftwareBreakpointPCOffset (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size); 321 322 Error 323 FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp); 324 325 /// Writes a siginfo_t structure corresponding to the given thread ID to the 326 /// memory region pointed to by @p siginfo. 327 Error 328 GetSignalInfo(lldb::tid_t tid, void *siginfo); 329 330 /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) 331 /// corresponding to the given thread ID to the memory pointed to by @p 332 /// message. 333 Error 334 GetEventMessage(lldb::tid_t tid, unsigned long *message); 335 336 /// Resumes the given thread. If @p signo is anything but 337 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. 338 Error 339 Resume(lldb::tid_t tid, uint32_t signo); 340 341 /// Single steps the given thread. If @p signo is anything but 342 /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. 343 Error 344 SingleStep(lldb::tid_t tid, uint32_t signo); 345 346 void 347 NotifyThreadDeath (lldb::tid_t tid); 348 349 Error 350 Detach(lldb::tid_t tid); 351 352 353 // Typedefs. 354 typedef std::unordered_set<lldb::tid_t> ThreadIDSet; 355 356 // This method is requests a stop on all threads which are still running. It sets up a 357 // deferred delegate notification, which will fire once threads report as stopped. The 358 // triggerring_tid will be set as the current thread (main stop reason). 359 void 360 StopRunningThreads(lldb::tid_t triggering_tid); 361 362 struct PendingNotification 363 { 364 PendingNotification (lldb::tid_t triggering_tid): 365 triggering_tid (triggering_tid), 366 wait_for_stop_tids () 367 { 368 } 369 370 const lldb::tid_t triggering_tid; 371 ThreadIDSet wait_for_stop_tids; 372 }; 373 typedef std::unique_ptr<PendingNotification> PendingNotificationUP; 374 375 // Notify the delegate if all threads have stopped. 376 void SignalIfAllThreadsStopped(); 377 378 void 379 RequestStopOnAllRunningThreads(); 380 381 Error 382 ThreadDidStop(lldb::tid_t tid, bool initiated_by_llgs); 383 384 // Resume the thread with the given thread id using the request_thread_resume_function 385 // called. If error_when_already_running is then then an error is raised if we think this 386 // thread is already running. 387 Error 388 ResumeThread(lldb::tid_t tid, NativeThreadLinux::ResumeThreadFunction request_thread_resume_function, 389 bool error_when_already_running); 390 391 void 392 DoStopThreads(PendingNotificationUP &¬ification_up); 393 394 void 395 ThreadWasCreated (lldb::tid_t tid); 396 397 // Member variables. 398 PendingNotificationUP m_pending_notification_up; 399 }; 400 401 } // namespace process_linux 402 } // namespace lldb_private 403 404 #endif // #ifndef liblldb_NativeProcessLinux_H_ 405