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