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 |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 18 | #include <mutex> |
| 19 | #include <unordered_map> |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 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" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 26 | #include "lldb/Host/HostThread.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 27 | #include "lldb/Host/Mutex.h" |
| 28 | #include "lldb/Target/MemoryRegionInfo.h" |
| 29 | |
Chaoren Lin | 2fe1d0a | 2015-02-03 01:51:38 +0000 | [diff] [blame] | 30 | #include "lldb/Host/common/NativeProcessProtocol.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 31 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 32 | namespace lldb_private { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 33 | class Error; |
| 34 | class Module; |
| 35 | class Scalar; |
| 36 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 37 | namespace process_linux { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 38 | /// @class NativeProcessLinux |
| 39 | /// @brief Manages communication with the inferior (debugee) process. |
| 40 | /// |
| 41 | /// Upon construction, this class prepares and launches an inferior process for |
| 42 | /// debugging. |
| 43 | /// |
| 44 | /// Changes in the inferior process state are broadcasted. |
| 45 | class NativeProcessLinux: public NativeProcessProtocol |
| 46 | { |
| 47 | public: |
| 48 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 49 | static Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 50 | LaunchProcess ( |
| 51 | Module *exe_module, |
| 52 | ProcessLaunchInfo &launch_info, |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 53 | NativeProcessProtocol::NativeDelegate &native_delegate, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 54 | NativeProcessProtocolSP &native_process_sp); |
| 55 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 56 | static Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 57 | AttachToProcess ( |
| 58 | lldb::pid_t pid, |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 59 | NativeProcessProtocol::NativeDelegate &native_delegate, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 60 | NativeProcessProtocolSP &native_process_sp); |
| 61 | |
| 62 | // --------------------------------------------------------------------- |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 63 | // NativeProcessProtocol Interface |
| 64 | // --------------------------------------------------------------------- |
| 65 | Error |
| 66 | Resume (const ResumeActionList &resume_actions) override; |
| 67 | |
| 68 | Error |
| 69 | Halt () override; |
| 70 | |
| 71 | Error |
| 72 | Detach () override; |
| 73 | |
| 74 | Error |
| 75 | Signal (int signo) override; |
| 76 | |
| 77 | Error |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 78 | Interrupt () override; |
| 79 | |
| 80 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 81 | Kill () override; |
| 82 | |
| 83 | Error |
| 84 | GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override; |
| 85 | |
| 86 | Error |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 87 | ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 88 | |
| 89 | Error |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 90 | ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 91 | |
| 92 | Error |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 93 | WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) override; |
| 94 | |
| 95 | Error |
| 96 | AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) override; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 97 | |
| 98 | Error |
| 99 | DeallocateMemory (lldb::addr_t addr) override; |
| 100 | |
| 101 | lldb::addr_t |
| 102 | GetSharedLibraryInfoAddress () override; |
| 103 | |
| 104 | size_t |
| 105 | UpdateThreads () override; |
| 106 | |
| 107 | bool |
| 108 | GetArchitecture (ArchSpec &arch) const override; |
| 109 | |
| 110 | Error |
| 111 | SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override; |
| 112 | |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 113 | Error |
| 114 | SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) override; |
| 115 | |
| 116 | Error |
| 117 | RemoveWatchpoint (lldb::addr_t addr) override; |
| 118 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 119 | void |
| 120 | DoStopIDBumped (uint32_t newBumpId) override; |
| 121 | |
Oleksiy Vyalov | 8bc34f4 | 2015-02-19 17:58:04 +0000 | [diff] [blame] | 122 | void |
| 123 | Terminate () override; |
| 124 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 125 | // --------------------------------------------------------------------- |
| 126 | // Interface used by NativeRegisterContext-derived classes. |
| 127 | // --------------------------------------------------------------------- |
| 128 | |
| 129 | /// Reads the contents from the register identified by the given (architecture |
| 130 | /// dependent) offset. |
| 131 | /// |
| 132 | /// This method is provided for use by RegisterContextLinux derivatives. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 133 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 134 | ReadRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name, |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 135 | unsigned size, RegisterValue &value); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 136 | |
| 137 | /// Writes the given value to the register identified by the given |
| 138 | /// (architecture dependent) offset. |
| 139 | /// |
| 140 | /// This method is provided for use by RegisterContextLinux derivatives. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 141 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 142 | WriteRegisterValue(lldb::tid_t tid, unsigned offset, const char *reg_name, |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 143 | const RegisterValue &value); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 144 | |
| 145 | /// Reads all general purpose 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 | ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size); |
| 148 | |
| 149 | /// Reads generic floating point registers into the specified buffer. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 150 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 151 | ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size); |
| 152 | |
| 153 | /// Reads the specified register set into the specified buffer. |
| 154 | /// For instance, the extended floating-point register set. |
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 | ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset); |
| 157 | |
| 158 | /// Writes all general purpose 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 | WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size); |
| 161 | |
| 162 | /// Writes generic floating point registers into the specified buffer. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 163 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 164 | WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size); |
| 165 | |
| 166 | /// Writes the specified register set into the specified buffer. |
| 167 | /// For instance, the extended floating-point register set. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 168 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 169 | WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset); |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 170 | |
| 171 | Error |
| 172 | GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) override; |
| 173 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 174 | protected: |
| 175 | // --------------------------------------------------------------------- |
| 176 | // NativeProcessProtocol protected interface |
| 177 | // --------------------------------------------------------------------- |
| 178 | Error |
| 179 | GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override; |
| 180 | |
| 181 | private: |
| 182 | |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 183 | class Monitor; |
| 184 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 185 | ArchSpec m_arch; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 186 | |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 187 | std::unique_ptr<Monitor> m_monitor_up; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 188 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 189 | LazyBool m_supports_mem_region; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 190 | std::vector<MemoryRegionInfo> m_mem_region_cache; |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 191 | Mutex m_mem_region_cache_mutex; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 192 | |
Tamas Berghammer | d8c338d | 2015-04-15 09:47:02 +0000 | [diff] [blame] | 193 | // List of thread ids stepping with a breakpoint with the address of |
| 194 | // the relevan breakpoint |
| 195 | std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint; |
| 196 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 197 | /// @class LauchArgs |
| 198 | /// |
| 199 | /// @brief Simple structure to pass data to the thread responsible for |
| 200 | /// launching a child process. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 201 | struct LaunchArgs |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 202 | { |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 203 | LaunchArgs(Module *module, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 204 | char const **argv, |
| 205 | char const **envp, |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 206 | const std::string &stdin_path, |
| 207 | const std::string &stdout_path, |
| 208 | const std::string &stderr_path, |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 209 | const char *working_dir, |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 210 | const ProcessLaunchInfo &launch_info); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 211 | |
| 212 | ~LaunchArgs(); |
| 213 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 214 | Module *m_module; // The executable image to launch. |
| 215 | char const **m_argv; // Process arguments. |
| 216 | char const **m_envp; // Process environment. |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 217 | const std::string &m_stdin_path; // Redirect stdin if not empty. |
| 218 | const std::string &m_stdout_path; // Redirect stdout if not empty. |
| 219 | const std::string &m_stderr_path; // Redirect stderr if not empty. |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 220 | const char *m_working_dir; // Working directory or NULL. |
| 221 | const ProcessLaunchInfo &m_launch_info; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 222 | }; |
| 223 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 224 | typedef std::function<::pid_t(Error &)> InitialOperation; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 225 | |
| 226 | // --------------------------------------------------------------------- |
| 227 | // Private Instance Methods |
| 228 | // --------------------------------------------------------------------- |
| 229 | NativeProcessLinux (); |
| 230 | |
| 231 | /// Launches an inferior process ready for debugging. Forms the |
| 232 | /// implementation of Process::DoLaunch. |
| 233 | void |
| 234 | LaunchInferior ( |
| 235 | Module *module, |
| 236 | char const *argv[], |
| 237 | char const *envp[], |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 238 | const std::string &stdin_path, |
| 239 | const std::string &stdout_path, |
| 240 | const std::string &stderr_path, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 241 | const char *working_dir, |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 242 | const ProcessLaunchInfo &launch_info, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 243 | Error &error); |
| 244 | |
| 245 | /// Attaches to an existing process. Forms the |
Tamas Berghammer | 0cbf0b1 | 2015-03-13 11:16:03 +0000 | [diff] [blame] | 246 | /// implementation of Process::DoAttach |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 247 | void |
| 248 | AttachToInferior (lldb::pid_t pid, Error &error); |
| 249 | |
| 250 | void |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 251 | StartMonitorThread(const InitialOperation &operation, Error &error); |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 252 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 253 | ::pid_t |
| 254 | Launch(LaunchArgs *args, Error &error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 255 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 256 | ::pid_t |
| 257 | Attach(lldb::pid_t pid, Error &error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 258 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 259 | static Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 260 | SetDefaultPtraceOpts(const lldb::pid_t); |
| 261 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 262 | static bool |
| 263 | DupDescriptor(const char *path, int fd, int flags); |
| 264 | |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 265 | static void * |
| 266 | MonitorThread(void *baton); |
| 267 | |
| 268 | void |
| 269 | MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 270 | |
| 271 | void |
Pavel Labath | 426bdf8 | 2015-04-28 07:51:52 +0000 | [diff] [blame] | 272 | WaitForNewThread(::pid_t tid); |
| 273 | |
| 274 | void |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 275 | MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid); |
| 276 | |
| 277 | void |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 278 | MonitorTrace(lldb::pid_t pid, NativeThreadProtocolSP thread_sp); |
| 279 | |
| 280 | void |
| 281 | MonitorBreakpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp); |
| 282 | |
| 283 | void |
| 284 | MonitorWatchpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp, uint32_t wp_index); |
| 285 | |
| 286 | void |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 287 | MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited); |
| 288 | |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 289 | bool |
| 290 | SupportHardwareSingleStepping() const; |
| 291 | |
| 292 | Error |
| 293 | SetupSoftwareSingleStepping(NativeThreadProtocolSP thread_sp); |
| 294 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 295 | #if 0 |
| 296 | static ::ProcessMessage::CrashReason |
| 297 | GetCrashReasonForSIGSEGV(const siginfo_t *info); |
| 298 | |
| 299 | static ::ProcessMessage::CrashReason |
| 300 | GetCrashReasonForSIGILL(const siginfo_t *info); |
| 301 | |
| 302 | static ::ProcessMessage::CrashReason |
| 303 | GetCrashReasonForSIGFPE(const siginfo_t *info); |
| 304 | |
| 305 | static ::ProcessMessage::CrashReason |
| 306 | GetCrashReasonForSIGBUS(const siginfo_t *info); |
| 307 | #endif |
| 308 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 309 | bool |
| 310 | HasThreadNoLock (lldb::tid_t thread_id); |
| 311 | |
| 312 | NativeThreadProtocolSP |
| 313 | MaybeGetThreadNoLock (lldb::tid_t thread_id); |
| 314 | |
| 315 | bool |
| 316 | StopTrackingThread (lldb::tid_t thread_id); |
| 317 | |
| 318 | NativeThreadProtocolSP |
| 319 | AddThread (lldb::tid_t thread_id); |
| 320 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 321 | Error |
Tamas Berghammer | 63c8be9 | 2015-04-15 09:38:48 +0000 | [diff] [blame] | 322 | GetSoftwareBreakpointPCOffset (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 323 | |
| 324 | Error |
| 325 | FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp); |
| 326 | |
| 327 | /// Writes a siginfo_t structure corresponding to the given thread ID to the |
| 328 | /// memory region pointed to by @p siginfo. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 329 | Error |
| 330 | GetSignalInfo(lldb::tid_t tid, void *siginfo); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 331 | |
| 332 | /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) |
| 333 | /// corresponding to the given thread ID to the memory pointed to by @p |
| 334 | /// message. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 335 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 336 | GetEventMessage(lldb::tid_t tid, unsigned long *message); |
| 337 | |
| 338 | /// Resumes the given thread. If @p signo is anything but |
| 339 | /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 340 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 341 | Resume(lldb::tid_t tid, uint32_t signo); |
| 342 | |
| 343 | /// Single steps the given thread. If @p signo is anything but |
| 344 | /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 345 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 346 | SingleStep(lldb::tid_t tid, uint32_t signo); |
| 347 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 348 | // ThreadStateCoordinator helper methods. |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 349 | void |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 350 | NotifyThreadDeath (lldb::tid_t tid); |
| 351 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 352 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 353 | Detach(lldb::tid_t tid); |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 354 | |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 355 | |
| 356 | public: |
| 357 | // Typedefs. |
| 358 | typedef std::unordered_set<lldb::tid_t> ThreadIDSet; |
| 359 | |
| 360 | // Callback/block definitions. |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 361 | typedef std::function<Error (lldb::tid_t tid, bool supress_signal)> ResumeThreadFunction; |
| 362 | |
| 363 | private: |
Pavel Labath | 337f3eb | 2015-05-08 08:57:45 +0000 | [diff] [blame] | 364 | enum class ThreadState |
| 365 | { |
| 366 | Running, |
| 367 | Stopped |
| 368 | }; |
| 369 | |
| 370 | // Notify that a thread is created and/or starting to be |
| 371 | // tracked. The state parameter should reflect whether the thread is created in a running |
| 372 | // or stopped state. |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 373 | void |
Pavel Labath | 337f3eb | 2015-05-08 08:57:45 +0000 | [diff] [blame] | 374 | NotifyThreadCreate(lldb::tid_t tid, ThreadState state); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 375 | |
| 376 | |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 377 | // Notify the delegate after a given set of threads stops. The triggering_tid will be set |
| 378 | // as the current thread. The error_function will be fired if either the triggering tid |
| 379 | // or any of the wait_for_stop_tids are unknown. |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 380 | void |
Pavel Labath | 337f3eb | 2015-05-08 08:57:45 +0000 | [diff] [blame] | 381 | StopThreads(lldb::tid_t triggering_tid, const ThreadIDSet &wait_for_stop_tids); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 382 | |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 383 | // Notify the delegate after all non-stopped threads stop. The triggering_tid will be set |
| 384 | // as the current thread. The error_function will be fired if the triggering tid |
| 385 | // is unknown. |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 386 | void |
Pavel Labath | 337f3eb | 2015-05-08 08:57:45 +0000 | [diff] [blame] | 387 | StopRunningThreads(lldb::tid_t triggering_tid); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 388 | |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 389 | // Notify the delegate after all non-stopped threads stop. The triggering_tid will be set |
| 390 | // as the current thread. The error_function will be fired if either the triggering tid |
| 391 | // or any of the wait_for_stop_tids are unknown. This variant will send stop requests to |
Pavel Labath | 337f3eb | 2015-05-08 08:57:45 +0000 | [diff] [blame] | 392 | // all non-stopped threads except skip_stop_request_tid. |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 393 | void |
Pavel Labath | 337f3eb | 2015-05-08 08:57:45 +0000 | [diff] [blame] | 394 | StopRunningThreadsWithSkipTID(lldb::tid_t triggering_tid, lldb::tid_t skip_stop_request_tid); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 395 | |
| 396 | // Notify the thread stopped. Will trigger error at time of execution if we |
| 397 | // already think it is stopped. |
Pavel Labath | 5eb721e | 2015-05-07 08:30:31 +0000 | [diff] [blame] | 398 | Error |
| 399 | NotifyThreadStop(lldb::tid_t tid, bool initiated_by_llgs); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 400 | |
| 401 | // Request that the given thread id should have the request_thread_resume_function |
Pavel Labath | 5eb721e | 2015-05-07 08:30:31 +0000 | [diff] [blame] | 402 | // called. This call signals an error if the thread resume is for |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 403 | // a thread that is already in a running state. |
Pavel Labath | 5eb721e | 2015-05-07 08:30:31 +0000 | [diff] [blame] | 404 | Error |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 405 | RequestThreadResume (lldb::tid_t tid, |
Pavel Labath | 5eb721e | 2015-05-07 08:30:31 +0000 | [diff] [blame] | 406 | const ResumeThreadFunction &request_thread_resume_function); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 407 | |
| 408 | // Request that the given thread id should have the request_thread_resume_function |
Pavel Labath | 5eb721e | 2015-05-07 08:30:31 +0000 | [diff] [blame] | 409 | // called. This call ignores threads that are already running and |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 410 | // does not trigger an error in that case. |
Pavel Labath | 5eb721e | 2015-05-07 08:30:31 +0000 | [diff] [blame] | 411 | Error |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 412 | RequestThreadResumeAsNeeded (lldb::tid_t tid, |
Pavel Labath | 5eb721e | 2015-05-07 08:30:31 +0000 | [diff] [blame] | 413 | const ResumeThreadFunction &request_thread_resume_function); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 414 | |
| 415 | // Indicate the calling process did an exec and that the thread state |
| 416 | // should be 100% cleared. |
| 417 | void |
| 418 | ResetForExec (); |
| 419 | |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 420 | private: |
| 421 | |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 422 | struct ThreadContext |
| 423 | { |
| 424 | ThreadState m_state; |
| 425 | bool m_stop_requested = false; |
| 426 | ResumeThreadFunction m_request_resume_function; |
Pavel Labath | 337f3eb | 2015-05-08 08:57:45 +0000 | [diff] [blame] | 427 | |
| 428 | explicit ThreadContext(ThreadState state) |
| 429 | : m_state(state) |
| 430 | {} |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 431 | }; |
| 432 | typedef std::unordered_map<lldb::tid_t, ThreadContext> TIDContextMap; |
| 433 | |
| 434 | struct PendingNotification |
| 435 | { |
| 436 | PendingNotification (lldb::tid_t triggering_tid, |
Pavel Labath | 337f3eb | 2015-05-08 08:57:45 +0000 | [diff] [blame] | 437 | const ThreadIDSet &wait_for_stop_tids, |
| 438 | const ThreadIDSet &skip_stop_request_tids): |
| 439 | triggering_tid (triggering_tid), |
| 440 | wait_for_stop_tids (wait_for_stop_tids), |
| 441 | original_wait_for_stop_tids (wait_for_stop_tids), |
| 442 | request_stop_on_all_unstopped_threads (false), |
| 443 | skip_stop_request_tids (skip_stop_request_tids) |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 444 | { |
| 445 | } |
| 446 | |
Pavel Labath | 337f3eb | 2015-05-08 08:57:45 +0000 | [diff] [blame] | 447 | PendingNotification (lldb::tid_t triggering_tid): |
| 448 | triggering_tid (triggering_tid), |
| 449 | wait_for_stop_tids (), |
| 450 | original_wait_for_stop_tids (), |
| 451 | request_stop_on_all_unstopped_threads (true), |
| 452 | skip_stop_request_tids () |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 453 | { |
| 454 | } |
| 455 | |
| 456 | const lldb::tid_t triggering_tid; |
| 457 | ThreadIDSet wait_for_stop_tids; |
| 458 | const ThreadIDSet original_wait_for_stop_tids; |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 459 | const bool request_stop_on_all_unstopped_threads; |
| 460 | ThreadIDSet skip_stop_request_tids; |
| 461 | }; |
| 462 | typedef std::unique_ptr<PendingNotification> PendingNotificationUP; |
| 463 | |
| 464 | // Fire pending notification if no pending thread stops remain. |
| 465 | void SignalIfRequirementsSatisfied(); |
| 466 | |
| 467 | bool |
| 468 | RequestStopOnAllSpecifiedThreads(); |
| 469 | |
| 470 | void |
| 471 | RequestStopOnAllRunningThreads(); |
| 472 | |
Pavel Labath | 337f3eb | 2015-05-08 08:57:45 +0000 | [diff] [blame] | 473 | Error |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 474 | RequestThreadStop (lldb::tid_t tid, ThreadContext& context); |
| 475 | |
| 476 | std::mutex m_event_mutex; // Serializes execution of ProcessEvent. XXX |
| 477 | |
Pavel Labath | 5eb721e | 2015-05-07 08:30:31 +0000 | [diff] [blame] | 478 | Error |
| 479 | ThreadDidStop(lldb::tid_t tid, bool initiated_by_llgs); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 480 | |
Pavel Labath | 5eb721e | 2015-05-07 08:30:31 +0000 | [diff] [blame] | 481 | Error |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 482 | DoResume(lldb::tid_t tid, ResumeThreadFunction request_thread_resume_function, |
Pavel Labath | 5eb721e | 2015-05-07 08:30:31 +0000 | [diff] [blame] | 483 | bool error_when_already_running); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 484 | |
| 485 | void |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 486 | DoStopThreads(PendingNotificationUP &¬ification_up); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 487 | |
| 488 | void |
Pavel Labath | 337f3eb | 2015-05-08 08:57:45 +0000 | [diff] [blame] | 489 | ThreadWasCreated (lldb::tid_t tid, ThreadState state); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 490 | |
| 491 | void |
Pavel Labath | 5eb721e | 2015-05-07 08:30:31 +0000 | [diff] [blame] | 492 | ThreadDidDie (lldb::tid_t tid); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 493 | |
| 494 | bool |
| 495 | IsKnownThread(lldb::tid_t tid) const; |
| 496 | |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 497 | // Member variables. |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 498 | PendingNotificationUP m_pending_notification_up; |
| 499 | |
| 500 | // Maps known TIDs to ThreadContext. |
| 501 | TIDContextMap m_tid_map; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 502 | }; |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 503 | |
| 504 | } // namespace process_linux |
| 505 | } // namespace lldb_private |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 506 | |
| 507 | #endif // #ifndef liblldb_NativeProcessLinux_H_ |