Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1 | //===-- NativeProcessLinux.cpp -------------------------------- -*- 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 | #include "lldb/lldb-python.h" |
| 11 | |
| 12 | #include "NativeProcessLinux.h" |
| 13 | |
| 14 | // C Includes |
| 15 | #include <errno.h> |
| 16 | #include <poll.h> |
| 17 | #include <string.h> |
| 18 | #include <stdint.h> |
| 19 | #include <unistd.h> |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 20 | |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 21 | #if defined (__arm64__) || defined (__aarch64__) |
| 22 | // NT_PRSTATUS and NT_FPREGSET definition |
| 23 | #include <elf.h> |
| 24 | #endif |
| 25 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 26 | // C++ Includes |
| 27 | #include <fstream> |
| 28 | #include <string> |
| 29 | |
| 30 | // Other libraries and framework includes |
| 31 | #include "lldb/Core/Debugger.h" |
| 32 | #include "lldb/Core/Error.h" |
| 33 | #include "lldb/Core/Module.h" |
Oleksiy Vyalov | 6edef20 | 2014-11-17 22:16:42 +0000 | [diff] [blame] | 34 | #include "lldb/Core/ModuleSpec.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 35 | #include "lldb/Core/RegisterValue.h" |
| 36 | #include "lldb/Core/Scalar.h" |
| 37 | #include "lldb/Core/State.h" |
| 38 | #include "lldb/Host/Host.h" |
Zachary Turner | 13b1826 | 2014-08-20 16:42:51 +0000 | [diff] [blame] | 39 | #include "lldb/Host/HostInfo.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 40 | #include "lldb/Host/ThreadLauncher.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 41 | #include "lldb/Symbol/ObjectFile.h" |
Chaoren Lin | 2fe1d0a | 2015-02-03 01:51:38 +0000 | [diff] [blame] | 42 | #include "lldb/Host/common/NativeRegisterContext.h" |
Zachary Turner | 90aff47 | 2015-03-03 23:36:51 +0000 | [diff] [blame^] | 43 | #include "lldb/Target/Process.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 44 | #include "lldb/Target/ProcessLaunchInfo.h" |
| 45 | #include "lldb/Utility/PseudoTerminal.h" |
| 46 | |
Chaoren Lin | 2fe1d0a | 2015-02-03 01:51:38 +0000 | [diff] [blame] | 47 | #include "lldb/Host/common/NativeBreakpoint.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 48 | #include "Utility/StringExtractor.h" |
| 49 | |
| 50 | #include "Plugins/Process/Utility/LinuxSignals.h" |
| 51 | #include "NativeThreadLinux.h" |
| 52 | #include "ProcFileReader.h" |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 53 | #include "ThreadStateCoordinator.h" |
Todd Fiala | cacde7d | 2014-09-27 16:54:22 +0000 | [diff] [blame] | 54 | #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" |
| 55 | |
Tamas Berghammer | d858487 | 2015-02-06 10:57:40 +0000 | [diff] [blame] | 56 | // System includes - They have to be included after framework includes because they define some |
| 57 | // macros which collide with variable names in other modules |
| 58 | #include <linux/unistd.h> |
| 59 | #ifndef __ANDROID__ |
| 60 | #include <sys/procfs.h> |
| 61 | #endif |
| 62 | #include <sys/personality.h> |
| 63 | #include <sys/ptrace.h> |
| 64 | #include <sys/socket.h> |
| 65 | #include <sys/syscall.h> |
| 66 | #include <sys/types.h> |
| 67 | #include <sys/uio.h> |
| 68 | #include <sys/user.h> |
| 69 | #include <sys/wait.h> |
| 70 | |
Todd Fiala | cacde7d | 2014-09-27 16:54:22 +0000 | [diff] [blame] | 71 | #ifdef __ANDROID__ |
| 72 | #define __ptrace_request int |
| 73 | #define PT_DETACH PTRACE_DETACH |
| 74 | #endif |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 75 | |
| 76 | #define DEBUG_PTRACE_MAXBYTES 20 |
| 77 | |
| 78 | // Support ptrace extensions even when compiled without required kernel support |
Todd Fiala | dda6194 | 2014-07-02 21:34:04 +0000 | [diff] [blame] | 79 | #ifndef PT_GETREGS |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 80 | #ifndef PTRACE_GETREGS |
Todd Fiala | dda6194 | 2014-07-02 21:34:04 +0000 | [diff] [blame] | 81 | #define PTRACE_GETREGS 12 |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 82 | #endif |
Todd Fiala | dda6194 | 2014-07-02 21:34:04 +0000 | [diff] [blame] | 83 | #endif |
| 84 | #ifndef PT_SETREGS |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 85 | #ifndef PTRACE_SETREGS |
| 86 | #define PTRACE_SETREGS 13 |
| 87 | #endif |
Todd Fiala | dda6194 | 2014-07-02 21:34:04 +0000 | [diff] [blame] | 88 | #endif |
| 89 | #ifndef PT_GETFPREGS |
| 90 | #ifndef PTRACE_GETFPREGS |
| 91 | #define PTRACE_GETFPREGS 14 |
| 92 | #endif |
| 93 | #endif |
| 94 | #ifndef PT_SETFPREGS |
| 95 | #ifndef PTRACE_SETFPREGS |
| 96 | #define PTRACE_SETFPREGS 15 |
| 97 | #endif |
| 98 | #endif |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 99 | #ifndef PTRACE_GETREGSET |
| 100 | #define PTRACE_GETREGSET 0x4204 |
| 101 | #endif |
| 102 | #ifndef PTRACE_SETREGSET |
| 103 | #define PTRACE_SETREGSET 0x4205 |
| 104 | #endif |
| 105 | #ifndef PTRACE_GET_THREAD_AREA |
| 106 | #define PTRACE_GET_THREAD_AREA 25 |
| 107 | #endif |
| 108 | #ifndef PTRACE_ARCH_PRCTL |
| 109 | #define PTRACE_ARCH_PRCTL 30 |
| 110 | #endif |
| 111 | #ifndef ARCH_GET_FS |
| 112 | #define ARCH_SET_GS 0x1001 |
| 113 | #define ARCH_SET_FS 0x1002 |
| 114 | #define ARCH_GET_FS 0x1003 |
| 115 | #define ARCH_GET_GS 0x1004 |
| 116 | #endif |
| 117 | |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 118 | #define LLDB_PERSONALITY_GET_CURRENT_SETTINGS 0xffffffff |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 119 | |
| 120 | // Support hardware breakpoints in case it has not been defined |
| 121 | #ifndef TRAP_HWBKPT |
| 122 | #define TRAP_HWBKPT 4 |
| 123 | #endif |
| 124 | |
| 125 | // Try to define a macro to encapsulate the tgkill syscall |
| 126 | // fall back on kill() if tgkill isn't available |
Chaoren Lin | c934659 | 2015-02-28 00:20:16 +0000 | [diff] [blame] | 127 | #define tgkill(pid, tid, sig) \ |
| 128 | syscall(SYS_tgkill, static_cast<::pid_t>(pid), static_cast<::pid_t>(tid), sig) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 129 | |
| 130 | // We disable the tracing of ptrace calls for integration builds to |
| 131 | // avoid the additional indirection and checks. |
| 132 | #ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 133 | #define PTRACE(req, pid, addr, data, data_size, error) \ |
| 134 | PtraceWrapper((req), (pid), (addr), (data), (data_size), (error), #req, __FILE__, __LINE__) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 135 | #else |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 136 | #define PTRACE(req, pid, addr, data, data_size, error) \ |
| 137 | PtraceWrapper((req), (pid), (addr), (data), (data_size), (error)) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 138 | #endif |
| 139 | |
| 140 | // Private bits we only need internally. |
| 141 | namespace |
| 142 | { |
| 143 | using namespace lldb; |
| 144 | using namespace lldb_private; |
| 145 | |
| 146 | const UnixSignals& |
| 147 | GetUnixSignals () |
| 148 | { |
| 149 | static process_linux::LinuxSignals signals; |
| 150 | return signals; |
| 151 | } |
| 152 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 153 | ThreadStateCoordinator::LogFunction |
| 154 | GetThreadLoggerFunction () |
| 155 | { |
| 156 | return [](const char *format, va_list args) |
| 157 | { |
| 158 | Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); |
| 159 | if (log) |
| 160 | log->VAPrintf (format, args); |
| 161 | }; |
| 162 | } |
| 163 | |
| 164 | void |
| 165 | CoordinatorErrorHandler (const std::string &error_message) |
| 166 | { |
| 167 | Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); |
| 168 | if (log) |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 169 | log->Printf ("NativeProcessLinux::%s %s", __FUNCTION__, error_message.c_str ()); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 170 | assert (false && "ThreadStateCoordinator error reported"); |
| 171 | } |
| 172 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 173 | Error |
| 174 | ResolveProcessArchitecture (lldb::pid_t pid, Platform &platform, ArchSpec &arch) |
| 175 | { |
| 176 | // Grab process info for the running process. |
| 177 | ProcessInstanceInfo process_info; |
| 178 | if (!platform.GetProcessInfo (pid, process_info)) |
| 179 | return lldb_private::Error("failed to get process info"); |
| 180 | |
| 181 | // Resolve the executable module. |
| 182 | ModuleSP exe_module_sp; |
Chaoren Lin | e56f6dc | 2015-03-01 04:31:16 +0000 | [diff] [blame] | 183 | ModuleSpec exe_module_spec(process_info.GetExecutableFile(), process_info.GetArchitecture()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 184 | FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths ()); |
| 185 | Error error = platform.ResolveExecutable( |
Oleksiy Vyalov | 5453933 | 2014-11-17 22:42:28 +0000 | [diff] [blame] | 186 | exe_module_spec, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 187 | exe_module_sp, |
| 188 | executable_search_paths.GetSize () ? &executable_search_paths : NULL); |
| 189 | |
| 190 | if (!error.Success ()) |
| 191 | return error; |
| 192 | |
| 193 | // Check if we've got our architecture from the exe_module. |
| 194 | arch = exe_module_sp->GetArchitecture (); |
| 195 | if (arch.IsValid ()) |
| 196 | return Error(); |
| 197 | else |
| 198 | return Error("failed to retrieve a valid architecture from the exe module"); |
| 199 | } |
| 200 | |
| 201 | void |
| 202 | DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count) |
| 203 | { |
| 204 | uint8_t *ptr = (uint8_t *)bytes; |
| 205 | const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count); |
| 206 | for(uint32_t i=0; i<loop_count; i++) |
| 207 | { |
| 208 | s.Printf ("[%x]", *ptr); |
| 209 | ptr++; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | PtraceDisplayBytes(int &req, void *data, size_t data_size) |
| 215 | { |
| 216 | StreamString buf; |
| 217 | Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet ( |
| 218 | POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE)); |
| 219 | |
| 220 | if (verbose_log) |
| 221 | { |
| 222 | switch(req) |
| 223 | { |
| 224 | case PTRACE_POKETEXT: |
| 225 | { |
| 226 | DisplayBytes(buf, &data, 8); |
| 227 | verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData()); |
| 228 | break; |
| 229 | } |
| 230 | case PTRACE_POKEDATA: |
| 231 | { |
| 232 | DisplayBytes(buf, &data, 8); |
| 233 | verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData()); |
| 234 | break; |
| 235 | } |
| 236 | case PTRACE_POKEUSER: |
| 237 | { |
| 238 | DisplayBytes(buf, &data, 8); |
| 239 | verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData()); |
| 240 | break; |
| 241 | } |
| 242 | case PTRACE_SETREGS: |
| 243 | { |
| 244 | DisplayBytes(buf, data, data_size); |
| 245 | verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData()); |
| 246 | break; |
| 247 | } |
| 248 | case PTRACE_SETFPREGS: |
| 249 | { |
| 250 | DisplayBytes(buf, data, data_size); |
| 251 | verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData()); |
| 252 | break; |
| 253 | } |
| 254 | case PTRACE_SETSIGINFO: |
| 255 | { |
| 256 | DisplayBytes(buf, data, sizeof(siginfo_t)); |
| 257 | verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData()); |
| 258 | break; |
| 259 | } |
| 260 | case PTRACE_SETREGSET: |
| 261 | { |
| 262 | // Extract iov_base from data, which is a pointer to the struct IOVEC |
| 263 | DisplayBytes(buf, *(void **)data, data_size); |
| 264 | verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData()); |
| 265 | break; |
| 266 | } |
| 267 | default: |
| 268 | { |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // Wrapper for ptrace to catch errors and log calls. |
| 275 | // Note that ptrace sets errno on error because -1 can be a valid result (i.e. for PTRACE_PEEK*) |
| 276 | long |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 277 | PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, Error& error, |
| 278 | const char* reqName, const char* file, int line) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 279 | { |
| 280 | long int result; |
| 281 | |
| 282 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE)); |
| 283 | |
| 284 | PtraceDisplayBytes(req, data, data_size); |
| 285 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 286 | error.Clear(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 287 | errno = 0; |
| 288 | if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) |
Todd Fiala | 202ecd2 | 2014-07-10 04:39:13 +0000 | [diff] [blame] | 289 | result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), *(unsigned int *)addr, data); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 290 | else |
Todd Fiala | 202ecd2 | 2014-07-10 04:39:13 +0000 | [diff] [blame] | 291 | result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), addr, data); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 292 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 293 | if (result == -1) |
| 294 | error.SetErrorToErrno(); |
| 295 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 296 | if (log) |
| 297 | log->Printf("ptrace(%s, %" PRIu64 ", %p, %p, %zu)=%lX called from file %s line %d", |
| 298 | reqName, pid, addr, data, data_size, result, file, line); |
| 299 | |
| 300 | PtraceDisplayBytes(req, data, data_size); |
| 301 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 302 | if (log && error.GetError() != 0) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 303 | { |
| 304 | const char* str; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 305 | switch (error.GetError()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 306 | { |
| 307 | case ESRCH: str = "ESRCH"; break; |
| 308 | case EINVAL: str = "EINVAL"; break; |
| 309 | case EBUSY: str = "EBUSY"; break; |
| 310 | case EPERM: str = "EPERM"; break; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 311 | default: str = error.AsCString(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 312 | } |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 313 | log->Printf("ptrace() failed; errno=%d (%s)", error.GetError(), str); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | return result; |
| 317 | } |
| 318 | |
| 319 | #ifdef LLDB_CONFIGURATION_BUILDANDINTEGRATION |
| 320 | // Wrapper for ptrace when logging is not required. |
| 321 | // Sets errno to 0 prior to calling ptrace. |
| 322 | long |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 323 | PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, Error& error) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 324 | { |
| 325 | long result = 0; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 326 | |
| 327 | error.Clear(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 328 | errno = 0; |
| 329 | if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) |
Todd Fiala | 202ecd2 | 2014-07-10 04:39:13 +0000 | [diff] [blame] | 330 | result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), *(unsigned int *)addr, data); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 331 | else |
Todd Fiala | 202ecd2 | 2014-07-10 04:39:13 +0000 | [diff] [blame] | 332 | result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), addr, data); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 333 | |
| 334 | if (result == -1) |
| 335 | error.SetErrorToErrno(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 336 | return result; |
| 337 | } |
| 338 | #endif |
| 339 | |
| 340 | //------------------------------------------------------------------------------ |
| 341 | // Static implementations of NativeProcessLinux::ReadMemory and |
| 342 | // NativeProcessLinux::WriteMemory. This enables mutual recursion between these |
| 343 | // functions without needed to go thru the thread funnel. |
| 344 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 345 | lldb::addr_t |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 346 | DoReadMemory ( |
| 347 | lldb::pid_t pid, |
| 348 | lldb::addr_t vm_addr, |
| 349 | void *buf, |
| 350 | lldb::addr_t size, |
| 351 | Error &error) |
| 352 | { |
| 353 | // ptrace word size is determined by the host, not the child |
| 354 | static const unsigned word_size = sizeof(void*); |
| 355 | unsigned char *dst = static_cast<unsigned char*>(buf); |
| 356 | lldb::addr_t bytes_read; |
| 357 | lldb::addr_t remainder; |
| 358 | long data; |
| 359 | |
| 360 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); |
| 361 | if (log) |
| 362 | ProcessPOSIXLog::IncNestLevel(); |
| 363 | if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) |
| 364 | log->Printf ("NativeProcessLinux::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__, |
| 365 | pid, word_size, (void*)vm_addr, buf, size); |
| 366 | |
| 367 | assert(sizeof(data) >= word_size); |
| 368 | for (bytes_read = 0; bytes_read < size; bytes_read += remainder) |
| 369 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 370 | data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, nullptr, 0, error); |
| 371 | if (error.Fail()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 372 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 373 | if (log) |
| 374 | ProcessPOSIXLog::DecNestLevel(); |
| 375 | return bytes_read; |
| 376 | } |
| 377 | |
| 378 | remainder = size - bytes_read; |
| 379 | remainder = remainder > word_size ? word_size : remainder; |
| 380 | |
| 381 | // Copy the data into our buffer |
| 382 | for (unsigned i = 0; i < remainder; ++i) |
| 383 | dst[i] = ((data >> i*8) & 0xFF); |
| 384 | |
| 385 | if (log && ProcessPOSIXLog::AtTopNestLevel() && |
| 386 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || |
| 387 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && |
| 388 | size <= POSIX_LOG_MEMORY_SHORT_BYTES))) |
| 389 | { |
| 390 | uintptr_t print_dst = 0; |
| 391 | // Format bytes from data by moving into print_dst for log output |
| 392 | for (unsigned i = 0; i < remainder; ++i) |
| 393 | print_dst |= (((data >> i*8) & 0xFF) << i*8); |
| 394 | log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, |
| 395 | (void*)vm_addr, print_dst, (unsigned long)data); |
| 396 | } |
| 397 | |
| 398 | vm_addr += word_size; |
| 399 | dst += word_size; |
| 400 | } |
| 401 | |
| 402 | if (log) |
| 403 | ProcessPOSIXLog::DecNestLevel(); |
| 404 | return bytes_read; |
| 405 | } |
| 406 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 407 | lldb::addr_t |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 408 | DoWriteMemory( |
| 409 | lldb::pid_t pid, |
| 410 | lldb::addr_t vm_addr, |
| 411 | const void *buf, |
| 412 | lldb::addr_t size, |
| 413 | Error &error) |
| 414 | { |
| 415 | // ptrace word size is determined by the host, not the child |
| 416 | static const unsigned word_size = sizeof(void*); |
| 417 | const unsigned char *src = static_cast<const unsigned char*>(buf); |
| 418 | lldb::addr_t bytes_written = 0; |
| 419 | lldb::addr_t remainder; |
| 420 | |
| 421 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); |
| 422 | if (log) |
| 423 | ProcessPOSIXLog::IncNestLevel(); |
| 424 | if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) |
| 425 | log->Printf ("NativeProcessLinux::%s(%" PRIu64 ", %u, %p, %p, %" PRIu64 ")", __FUNCTION__, |
| 426 | pid, word_size, (void*)vm_addr, buf, size); |
| 427 | |
| 428 | for (bytes_written = 0; bytes_written < size; bytes_written += remainder) |
| 429 | { |
| 430 | remainder = size - bytes_written; |
| 431 | remainder = remainder > word_size ? word_size : remainder; |
| 432 | |
| 433 | if (remainder == word_size) |
| 434 | { |
| 435 | unsigned long data = 0; |
| 436 | assert(sizeof(data) >= word_size); |
| 437 | for (unsigned i = 0; i < word_size; ++i) |
| 438 | data |= (unsigned long)src[i] << i*8; |
| 439 | |
| 440 | if (log && ProcessPOSIXLog::AtTopNestLevel() && |
| 441 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || |
| 442 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && |
| 443 | size <= POSIX_LOG_MEMORY_SHORT_BYTES))) |
| 444 | log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, |
| 445 | (void*)vm_addr, *(unsigned long*)src, data); |
| 446 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 447 | if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0, error)) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 448 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 449 | if (log) |
| 450 | ProcessPOSIXLog::DecNestLevel(); |
| 451 | return bytes_written; |
| 452 | } |
| 453 | } |
| 454 | else |
| 455 | { |
| 456 | unsigned char buff[8]; |
| 457 | if (DoReadMemory(pid, vm_addr, |
| 458 | buff, word_size, error) != word_size) |
| 459 | { |
| 460 | if (log) |
| 461 | ProcessPOSIXLog::DecNestLevel(); |
| 462 | return bytes_written; |
| 463 | } |
| 464 | |
| 465 | memcpy(buff, src, remainder); |
| 466 | |
| 467 | if (DoWriteMemory(pid, vm_addr, |
| 468 | buff, word_size, error) != word_size) |
| 469 | { |
| 470 | if (log) |
| 471 | ProcessPOSIXLog::DecNestLevel(); |
| 472 | return bytes_written; |
| 473 | } |
| 474 | |
| 475 | if (log && ProcessPOSIXLog::AtTopNestLevel() && |
| 476 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || |
| 477 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && |
| 478 | size <= POSIX_LOG_MEMORY_SHORT_BYTES))) |
| 479 | log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, |
| 480 | (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff); |
| 481 | } |
| 482 | |
| 483 | vm_addr += word_size; |
| 484 | src += word_size; |
| 485 | } |
| 486 | if (log) |
| 487 | ProcessPOSIXLog::DecNestLevel(); |
| 488 | return bytes_written; |
| 489 | } |
| 490 | |
| 491 | //------------------------------------------------------------------------------ |
| 492 | /// @class Operation |
| 493 | /// @brief Represents a NativeProcessLinux operation. |
| 494 | /// |
| 495 | /// Under Linux, it is not possible to ptrace() from any other thread but the |
| 496 | /// one that spawned or attached to the process from the start. Therefore, when |
| 497 | /// a NativeProcessLinux is asked to deliver or change the state of an inferior |
| 498 | /// process the operation must be "funneled" to a specific thread to perform the |
| 499 | /// task. The Operation class provides an abstract base for all services the |
| 500 | /// NativeProcessLinux must perform via the single virtual function Execute, thus |
| 501 | /// encapsulating the code that needs to run in the privileged context. |
| 502 | class Operation |
| 503 | { |
| 504 | public: |
| 505 | Operation () : m_error() { } |
| 506 | |
| 507 | virtual |
| 508 | ~Operation() {} |
| 509 | |
| 510 | virtual void |
| 511 | Execute (NativeProcessLinux *process) = 0; |
| 512 | |
| 513 | const Error & |
| 514 | GetError () const { return m_error; } |
| 515 | |
| 516 | protected: |
| 517 | Error m_error; |
| 518 | }; |
| 519 | |
| 520 | //------------------------------------------------------------------------------ |
| 521 | /// @class ReadOperation |
| 522 | /// @brief Implements NativeProcessLinux::ReadMemory. |
| 523 | class ReadOperation : public Operation |
| 524 | { |
| 525 | public: |
| 526 | ReadOperation ( |
| 527 | lldb::addr_t addr, |
| 528 | void *buff, |
| 529 | lldb::addr_t size, |
Todd Fiala | b35103e | 2014-07-10 05:25:39 +0000 | [diff] [blame] | 530 | lldb::addr_t &result) : |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 531 | Operation (), |
| 532 | m_addr (addr), |
| 533 | m_buff (buff), |
| 534 | m_size (size), |
| 535 | m_result (result) |
| 536 | { |
| 537 | } |
| 538 | |
| 539 | void Execute (NativeProcessLinux *process) override; |
| 540 | |
| 541 | private: |
| 542 | lldb::addr_t m_addr; |
| 543 | void *m_buff; |
| 544 | lldb::addr_t m_size; |
| 545 | lldb::addr_t &m_result; |
| 546 | }; |
| 547 | |
| 548 | void |
| 549 | ReadOperation::Execute (NativeProcessLinux *process) |
| 550 | { |
| 551 | m_result = DoReadMemory (process->GetID (), m_addr, m_buff, m_size, m_error); |
| 552 | } |
| 553 | |
| 554 | //------------------------------------------------------------------------------ |
| 555 | /// @class WriteOperation |
| 556 | /// @brief Implements NativeProcessLinux::WriteMemory. |
| 557 | class WriteOperation : public Operation |
| 558 | { |
| 559 | public: |
| 560 | WriteOperation ( |
| 561 | lldb::addr_t addr, |
| 562 | const void *buff, |
| 563 | lldb::addr_t size, |
| 564 | lldb::addr_t &result) : |
| 565 | Operation (), |
| 566 | m_addr (addr), |
| 567 | m_buff (buff), |
| 568 | m_size (size), |
| 569 | m_result (result) |
| 570 | { |
| 571 | } |
| 572 | |
| 573 | void Execute (NativeProcessLinux *process) override; |
| 574 | |
| 575 | private: |
| 576 | lldb::addr_t m_addr; |
| 577 | const void *m_buff; |
| 578 | lldb::addr_t m_size; |
| 579 | lldb::addr_t &m_result; |
| 580 | }; |
| 581 | |
| 582 | void |
| 583 | WriteOperation::Execute(NativeProcessLinux *process) |
| 584 | { |
| 585 | m_result = DoWriteMemory (process->GetID (), m_addr, m_buff, m_size, m_error); |
| 586 | } |
| 587 | |
| 588 | //------------------------------------------------------------------------------ |
| 589 | /// @class ReadRegOperation |
| 590 | /// @brief Implements NativeProcessLinux::ReadRegisterValue. |
| 591 | class ReadRegOperation : public Operation |
| 592 | { |
| 593 | public: |
| 594 | ReadRegOperation(lldb::tid_t tid, uint32_t offset, const char *reg_name, |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 595 | RegisterValue &value) |
| 596 | : m_tid(tid), |
| 597 | m_offset(static_cast<uintptr_t> (offset)), |
| 598 | m_reg_name(reg_name), |
| 599 | m_value(value) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 600 | { } |
| 601 | |
| 602 | void Execute(NativeProcessLinux *monitor); |
| 603 | |
| 604 | private: |
| 605 | lldb::tid_t m_tid; |
| 606 | uintptr_t m_offset; |
| 607 | const char *m_reg_name; |
| 608 | RegisterValue &m_value; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 609 | }; |
| 610 | |
| 611 | void |
| 612 | ReadRegOperation::Execute(NativeProcessLinux *monitor) |
| 613 | { |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 614 | #if defined (__arm64__) || defined (__aarch64__) |
| 615 | if (m_offset > sizeof(struct user_pt_regs)) |
| 616 | { |
| 617 | uintptr_t offset = m_offset - sizeof(struct user_pt_regs); |
| 618 | if (offset > sizeof(struct user_fpsimd_state)) |
| 619 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 620 | m_error.SetErrorString("invalid offset value"); |
| 621 | return; |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 622 | } |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 623 | elf_fpregset_t regs; |
| 624 | int regset = NT_FPREGSET; |
| 625 | struct iovec ioVec; |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 626 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 627 | ioVec.iov_base = ®s; |
| 628 | ioVec.iov_len = sizeof regs; |
| 629 | PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); |
| 630 | if (m_error.Success()) |
| 631 | { |
| 632 | lldb_private::ArchSpec arch; |
| 633 | if (monitor->GetArchitecture(arch)) |
| 634 | m_value.SetBytes((void *)(((unsigned char *)(®s)) + offset), 16, arch.GetByteOrder()); |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 635 | else |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 636 | m_error.SetErrorString("failed to get architecture"); |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | else |
| 640 | { |
| 641 | elf_gregset_t regs; |
| 642 | int regset = NT_PRSTATUS; |
| 643 | struct iovec ioVec; |
| 644 | |
| 645 | ioVec.iov_base = ®s; |
| 646 | ioVec.iov_len = sizeof regs; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 647 | PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); |
| 648 | if (m_error.Success()) |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 649 | { |
| 650 | lldb_private::ArchSpec arch; |
| 651 | if (monitor->GetArchitecture(arch)) |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 652 | m_value.SetBytes((void *)(((unsigned char *)(regs)) + m_offset), 8, arch.GetByteOrder()); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 653 | else |
| 654 | m_error.SetErrorString("failed to get architecture"); |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | #else |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 658 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS)); |
| 659 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 660 | lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, nullptr, 0, m_error); |
| 661 | if (m_error.Success()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 662 | m_value = data; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 663 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 664 | if (log) |
| 665 | log->Printf ("NativeProcessLinux::%s() reg %s: 0x%" PRIx64, __FUNCTION__, |
| 666 | m_reg_name, data); |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 667 | #endif |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | //------------------------------------------------------------------------------ |
| 671 | /// @class WriteRegOperation |
| 672 | /// @brief Implements NativeProcessLinux::WriteRegisterValue. |
| 673 | class WriteRegOperation : public Operation |
| 674 | { |
| 675 | public: |
| 676 | WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name, |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 677 | const RegisterValue &value) |
| 678 | : m_tid(tid), |
| 679 | m_offset(offset), |
| 680 | m_reg_name(reg_name), |
| 681 | m_value(value) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 682 | { } |
| 683 | |
| 684 | void Execute(NativeProcessLinux *monitor); |
| 685 | |
| 686 | private: |
| 687 | lldb::tid_t m_tid; |
| 688 | uintptr_t m_offset; |
| 689 | const char *m_reg_name; |
| 690 | const RegisterValue &m_value; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 691 | }; |
| 692 | |
| 693 | void |
| 694 | WriteRegOperation::Execute(NativeProcessLinux *monitor) |
| 695 | { |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 696 | #if defined (__arm64__) || defined (__aarch64__) |
| 697 | if (m_offset > sizeof(struct user_pt_regs)) |
| 698 | { |
| 699 | uintptr_t offset = m_offset - sizeof(struct user_pt_regs); |
| 700 | if (offset > sizeof(struct user_fpsimd_state)) |
| 701 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 702 | m_error.SetErrorString("invalid offset value"); |
| 703 | return; |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 704 | } |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 705 | elf_fpregset_t regs; |
| 706 | int regset = NT_FPREGSET; |
| 707 | struct iovec ioVec; |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 708 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 709 | ioVec.iov_base = ®s; |
| 710 | ioVec.iov_len = sizeof regs; |
| 711 | PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); |
| 712 | if (m_error.Sucess()) |
| 713 | { |
| 714 | ::memcpy((void *)(((unsigned char *)(®s)) + offset), m_value.GetBytes(), 16); |
| 715 | PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | else |
| 719 | { |
| 720 | elf_gregset_t regs; |
| 721 | int regset = NT_PRSTATUS; |
| 722 | struct iovec ioVec; |
| 723 | |
| 724 | ioVec.iov_base = ®s; |
| 725 | ioVec.iov_len = sizeof regs; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 726 | PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); |
| 727 | if (m_error.Sucess()) |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 728 | { |
| 729 | ::memcpy((void *)(((unsigned char *)(®s)) + m_offset), m_value.GetBytes(), 8); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 730 | PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | #else |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 734 | void* buf; |
| 735 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS)); |
| 736 | |
| 737 | buf = (void*) m_value.GetAsUInt64(); |
| 738 | |
| 739 | if (log) |
| 740 | log->Printf ("NativeProcessLinux::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 741 | PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0, m_error); |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 742 | #endif |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | //------------------------------------------------------------------------------ |
| 746 | /// @class ReadGPROperation |
| 747 | /// @brief Implements NativeProcessLinux::ReadGPR. |
| 748 | class ReadGPROperation : public Operation |
| 749 | { |
| 750 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 751 | ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size) |
| 752 | : m_tid(tid), m_buf(buf), m_buf_size(buf_size) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 753 | { } |
| 754 | |
| 755 | void Execute(NativeProcessLinux *monitor); |
| 756 | |
| 757 | private: |
| 758 | lldb::tid_t m_tid; |
| 759 | void *m_buf; |
| 760 | size_t m_buf_size; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 761 | }; |
| 762 | |
| 763 | void |
| 764 | ReadGPROperation::Execute(NativeProcessLinux *monitor) |
| 765 | { |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 766 | #if defined (__arm64__) || defined (__aarch64__) |
| 767 | int regset = NT_PRSTATUS; |
| 768 | struct iovec ioVec; |
| 769 | |
| 770 | ioVec.iov_base = m_buf; |
| 771 | ioVec.iov_len = m_buf_size; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 772 | PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 773 | #else |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 774 | PTRACE(PTRACE_GETREGS, m_tid, nullptr, m_buf, m_buf_size, m_error); |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 775 | #endif |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | //------------------------------------------------------------------------------ |
| 779 | /// @class ReadFPROperation |
| 780 | /// @brief Implements NativeProcessLinux::ReadFPR. |
| 781 | class ReadFPROperation : public Operation |
| 782 | { |
| 783 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 784 | ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size) |
| 785 | : m_tid(tid), |
| 786 | m_buf(buf), |
| 787 | m_buf_size(buf_size) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 788 | { } |
| 789 | |
| 790 | void Execute(NativeProcessLinux *monitor); |
| 791 | |
| 792 | private: |
| 793 | lldb::tid_t m_tid; |
| 794 | void *m_buf; |
| 795 | size_t m_buf_size; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 796 | }; |
| 797 | |
| 798 | void |
| 799 | ReadFPROperation::Execute(NativeProcessLinux *monitor) |
| 800 | { |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 801 | #if defined (__arm64__) || defined (__aarch64__) |
| 802 | int regset = NT_FPREGSET; |
| 803 | struct iovec ioVec; |
| 804 | |
| 805 | ioVec.iov_base = m_buf; |
| 806 | ioVec.iov_len = m_buf_size; |
| 807 | if (PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, m_buf_size) < 0) |
| 808 | m_result = false; |
| 809 | else |
| 810 | m_result = true; |
| 811 | #else |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 812 | PTRACE(PTRACE_GETFPREGS, m_tid, nullptr, m_buf, m_buf_size, m_error); |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 813 | #endif |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | //------------------------------------------------------------------------------ |
| 817 | /// @class ReadRegisterSetOperation |
| 818 | /// @brief Implements NativeProcessLinux::ReadRegisterSet. |
| 819 | class ReadRegisterSetOperation : public Operation |
| 820 | { |
| 821 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 822 | ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) |
| 823 | : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 824 | { } |
| 825 | |
| 826 | void Execute(NativeProcessLinux *monitor); |
| 827 | |
| 828 | private: |
| 829 | lldb::tid_t m_tid; |
| 830 | void *m_buf; |
| 831 | size_t m_buf_size; |
| 832 | const unsigned int m_regset; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 833 | }; |
| 834 | |
| 835 | void |
| 836 | ReadRegisterSetOperation::Execute(NativeProcessLinux *monitor) |
| 837 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 838 | PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size, m_error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | //------------------------------------------------------------------------------ |
| 842 | /// @class WriteGPROperation |
| 843 | /// @brief Implements NativeProcessLinux::WriteGPR. |
| 844 | class WriteGPROperation : public Operation |
| 845 | { |
| 846 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 847 | WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size) |
| 848 | : m_tid(tid), m_buf(buf), m_buf_size(buf_size) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 849 | { } |
| 850 | |
| 851 | void Execute(NativeProcessLinux *monitor); |
| 852 | |
| 853 | private: |
| 854 | lldb::tid_t m_tid; |
| 855 | void *m_buf; |
| 856 | size_t m_buf_size; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 857 | }; |
| 858 | |
| 859 | void |
| 860 | WriteGPROperation::Execute(NativeProcessLinux *monitor) |
| 861 | { |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 862 | #if defined (__arm64__) || defined (__aarch64__) |
| 863 | int regset = NT_PRSTATUS; |
| 864 | struct iovec ioVec; |
| 865 | |
| 866 | ioVec.iov_base = m_buf; |
| 867 | ioVec.iov_len = m_buf_size; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 868 | PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 869 | #else |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 870 | PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size, m_error); |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 871 | #endif |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | //------------------------------------------------------------------------------ |
| 875 | /// @class WriteFPROperation |
| 876 | /// @brief Implements NativeProcessLinux::WriteFPR. |
| 877 | class WriteFPROperation : public Operation |
| 878 | { |
| 879 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 880 | WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size) |
| 881 | : m_tid(tid), m_buf(buf), m_buf_size(buf_size) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 882 | { } |
| 883 | |
| 884 | void Execute(NativeProcessLinux *monitor); |
| 885 | |
| 886 | private: |
| 887 | lldb::tid_t m_tid; |
| 888 | void *m_buf; |
| 889 | size_t m_buf_size; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 890 | }; |
| 891 | |
| 892 | void |
| 893 | WriteFPROperation::Execute(NativeProcessLinux *monitor) |
| 894 | { |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 895 | #if defined (__arm64__) || defined (__aarch64__) |
| 896 | int regset = NT_FPREGSET; |
| 897 | struct iovec ioVec; |
| 898 | |
| 899 | ioVec.iov_base = m_buf; |
| 900 | ioVec.iov_len = m_buf_size; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 901 | PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 902 | #else |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 903 | PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size, m_error); |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 904 | #endif |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | //------------------------------------------------------------------------------ |
| 908 | /// @class WriteRegisterSetOperation |
| 909 | /// @brief Implements NativeProcessLinux::WriteRegisterSet. |
| 910 | class WriteRegisterSetOperation : public Operation |
| 911 | { |
| 912 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 913 | WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) |
| 914 | : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 915 | { } |
| 916 | |
| 917 | void Execute(NativeProcessLinux *monitor); |
| 918 | |
| 919 | private: |
| 920 | lldb::tid_t m_tid; |
| 921 | void *m_buf; |
| 922 | size_t m_buf_size; |
| 923 | const unsigned int m_regset; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 924 | }; |
| 925 | |
| 926 | void |
| 927 | WriteRegisterSetOperation::Execute(NativeProcessLinux *monitor) |
| 928 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 929 | PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size, m_error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | //------------------------------------------------------------------------------ |
| 933 | /// @class ResumeOperation |
| 934 | /// @brief Implements NativeProcessLinux::Resume. |
| 935 | class ResumeOperation : public Operation |
| 936 | { |
| 937 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 938 | ResumeOperation(lldb::tid_t tid, uint32_t signo) : |
| 939 | m_tid(tid), m_signo(signo) { } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 940 | |
| 941 | void Execute(NativeProcessLinux *monitor); |
| 942 | |
| 943 | private: |
| 944 | lldb::tid_t m_tid; |
| 945 | uint32_t m_signo; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 946 | }; |
| 947 | |
| 948 | void |
| 949 | ResumeOperation::Execute(NativeProcessLinux *monitor) |
| 950 | { |
| 951 | intptr_t data = 0; |
| 952 | |
| 953 | if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) |
| 954 | data = m_signo; |
| 955 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 956 | PTRACE(PTRACE_CONT, m_tid, nullptr, (void*)data, 0, m_error); |
| 957 | if (m_error.Fail()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 958 | { |
| 959 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 960 | |
| 961 | if (log) |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 962 | log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, m_error.AsCString()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 963 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | //------------------------------------------------------------------------------ |
| 967 | /// @class SingleStepOperation |
| 968 | /// @brief Implements NativeProcessLinux::SingleStep. |
| 969 | class SingleStepOperation : public Operation |
| 970 | { |
| 971 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 972 | SingleStepOperation(lldb::tid_t tid, uint32_t signo) |
| 973 | : m_tid(tid), m_signo(signo) { } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 974 | |
| 975 | void Execute(NativeProcessLinux *monitor); |
| 976 | |
| 977 | private: |
| 978 | lldb::tid_t m_tid; |
| 979 | uint32_t m_signo; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 980 | }; |
| 981 | |
| 982 | void |
| 983 | SingleStepOperation::Execute(NativeProcessLinux *monitor) |
| 984 | { |
| 985 | intptr_t data = 0; |
| 986 | |
| 987 | if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) |
| 988 | data = m_signo; |
| 989 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 990 | PTRACE(PTRACE_SINGLESTEP, m_tid, nullptr, (void*)data, 0, m_error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | //------------------------------------------------------------------------------ |
| 994 | /// @class SiginfoOperation |
| 995 | /// @brief Implements NativeProcessLinux::GetSignalInfo. |
| 996 | class SiginfoOperation : public Operation |
| 997 | { |
| 998 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 999 | SiginfoOperation(lldb::tid_t tid, void *info) |
| 1000 | : m_tid(tid), m_info(info) { } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1001 | |
| 1002 | void Execute(NativeProcessLinux *monitor); |
| 1003 | |
| 1004 | private: |
| 1005 | lldb::tid_t m_tid; |
| 1006 | void *m_info; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1007 | }; |
| 1008 | |
| 1009 | void |
| 1010 | SiginfoOperation::Execute(NativeProcessLinux *monitor) |
| 1011 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 1012 | PTRACE(PTRACE_GETSIGINFO, m_tid, nullptr, m_info, 0, m_error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | //------------------------------------------------------------------------------ |
| 1016 | /// @class EventMessageOperation |
| 1017 | /// @brief Implements NativeProcessLinux::GetEventMessage. |
| 1018 | class EventMessageOperation : public Operation |
| 1019 | { |
| 1020 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 1021 | EventMessageOperation(lldb::tid_t tid, unsigned long *message) |
| 1022 | : m_tid(tid), m_message(message) { } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1023 | |
| 1024 | void Execute(NativeProcessLinux *monitor); |
| 1025 | |
| 1026 | private: |
| 1027 | lldb::tid_t m_tid; |
| 1028 | unsigned long *m_message; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1029 | }; |
| 1030 | |
| 1031 | void |
| 1032 | EventMessageOperation::Execute(NativeProcessLinux *monitor) |
| 1033 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 1034 | PTRACE(PTRACE_GETEVENTMSG, m_tid, nullptr, m_message, 0, m_error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | class DetachOperation : public Operation |
| 1038 | { |
| 1039 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 1040 | DetachOperation(lldb::tid_t tid) : m_tid(tid) { } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1041 | |
| 1042 | void Execute(NativeProcessLinux *monitor); |
| 1043 | |
| 1044 | private: |
| 1045 | lldb::tid_t m_tid; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1046 | }; |
| 1047 | |
| 1048 | void |
| 1049 | DetachOperation::Execute(NativeProcessLinux *monitor) |
| 1050 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 1051 | PTRACE(PTRACE_DETACH, m_tid, nullptr, 0, 0, m_error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | } |
| 1055 | |
| 1056 | using namespace lldb_private; |
| 1057 | |
| 1058 | // Simple helper function to ensure flags are enabled on the given file |
| 1059 | // descriptor. |
| 1060 | static bool |
| 1061 | EnsureFDFlags(int fd, int flags, Error &error) |
| 1062 | { |
| 1063 | int status; |
| 1064 | |
| 1065 | if ((status = fcntl(fd, F_GETFL)) == -1) |
| 1066 | { |
| 1067 | error.SetErrorToErrno(); |
| 1068 | return false; |
| 1069 | } |
| 1070 | |
| 1071 | if (fcntl(fd, F_SETFL, status | flags) == -1) |
| 1072 | { |
| 1073 | error.SetErrorToErrno(); |
| 1074 | return false; |
| 1075 | } |
| 1076 | |
| 1077 | return true; |
| 1078 | } |
| 1079 | |
| 1080 | NativeProcessLinux::OperationArgs::OperationArgs(NativeProcessLinux *monitor) |
| 1081 | : m_monitor(monitor) |
| 1082 | { |
| 1083 | sem_init(&m_semaphore, 0, 0); |
| 1084 | } |
| 1085 | |
| 1086 | NativeProcessLinux::OperationArgs::~OperationArgs() |
| 1087 | { |
| 1088 | sem_destroy(&m_semaphore); |
| 1089 | } |
| 1090 | |
| 1091 | NativeProcessLinux::LaunchArgs::LaunchArgs(NativeProcessLinux *monitor, |
| 1092 | lldb_private::Module *module, |
| 1093 | char const **argv, |
| 1094 | char const **envp, |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1095 | const std::string &stdin_path, |
| 1096 | const std::string &stdout_path, |
| 1097 | const std::string &stderr_path, |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1098 | const char *working_dir, |
| 1099 | const lldb_private::ProcessLaunchInfo &launch_info) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1100 | : OperationArgs(monitor), |
| 1101 | m_module(module), |
| 1102 | m_argv(argv), |
| 1103 | m_envp(envp), |
| 1104 | m_stdin_path(stdin_path), |
| 1105 | m_stdout_path(stdout_path), |
| 1106 | m_stderr_path(stderr_path), |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1107 | m_working_dir(working_dir), |
| 1108 | m_launch_info(launch_info) |
| 1109 | { |
| 1110 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1111 | |
| 1112 | NativeProcessLinux::LaunchArgs::~LaunchArgs() |
| 1113 | { } |
| 1114 | |
| 1115 | NativeProcessLinux::AttachArgs::AttachArgs(NativeProcessLinux *monitor, |
| 1116 | lldb::pid_t pid) |
| 1117 | : OperationArgs(monitor), m_pid(pid) { } |
| 1118 | |
| 1119 | NativeProcessLinux::AttachArgs::~AttachArgs() |
| 1120 | { } |
| 1121 | |
| 1122 | // ----------------------------------------------------------------------------- |
| 1123 | // Public Static Methods |
| 1124 | // ----------------------------------------------------------------------------- |
| 1125 | |
| 1126 | lldb_private::Error |
| 1127 | NativeProcessLinux::LaunchProcess ( |
| 1128 | lldb_private::Module *exe_module, |
| 1129 | lldb_private::ProcessLaunchInfo &launch_info, |
| 1130 | lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate, |
| 1131 | NativeProcessProtocolSP &native_process_sp) |
| 1132 | { |
| 1133 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1134 | |
| 1135 | Error error; |
| 1136 | |
| 1137 | // Verify the working directory is valid if one was specified. |
| 1138 | const char* working_dir = launch_info.GetWorkingDirectory (); |
| 1139 | if (working_dir) |
| 1140 | { |
| 1141 | FileSpec working_dir_fs (working_dir, true); |
| 1142 | if (!working_dir_fs || working_dir_fs.GetFileType () != FileSpec::eFileTypeDirectory) |
| 1143 | { |
| 1144 | error.SetErrorStringWithFormat ("No such file or directory: %s", working_dir); |
| 1145 | return error; |
| 1146 | } |
| 1147 | } |
| 1148 | |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 1149 | const lldb_private::FileAction *file_action; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1150 | |
| 1151 | // Default of NULL will mean to use existing open file descriptors. |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1152 | std::string stdin_path; |
| 1153 | std::string stdout_path; |
| 1154 | std::string stderr_path; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1155 | |
| 1156 | file_action = launch_info.GetFileActionForFD (STDIN_FILENO); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1157 | if (file_action) |
| 1158 | stdin_path = file_action->GetPath (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1159 | |
| 1160 | file_action = launch_info.GetFileActionForFD (STDOUT_FILENO); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1161 | if (file_action) |
| 1162 | stdout_path = file_action->GetPath (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1163 | |
| 1164 | file_action = launch_info.GetFileActionForFD (STDERR_FILENO); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1165 | if (file_action) |
| 1166 | stderr_path = file_action->GetPath (); |
| 1167 | |
| 1168 | if (log) |
| 1169 | { |
| 1170 | if (!stdin_path.empty ()) |
| 1171 | log->Printf ("NativeProcessLinux::%s setting STDIN to '%s'", __FUNCTION__, stdin_path.c_str ()); |
| 1172 | else |
| 1173 | log->Printf ("NativeProcessLinux::%s leaving STDIN as is", __FUNCTION__); |
| 1174 | |
| 1175 | if (!stdout_path.empty ()) |
| 1176 | log->Printf ("NativeProcessLinux::%s setting STDOUT to '%s'", __FUNCTION__, stdout_path.c_str ()); |
| 1177 | else |
| 1178 | log->Printf ("NativeProcessLinux::%s leaving STDOUT as is", __FUNCTION__); |
| 1179 | |
| 1180 | if (!stderr_path.empty ()) |
| 1181 | log->Printf ("NativeProcessLinux::%s setting STDERR to '%s'", __FUNCTION__, stderr_path.c_str ()); |
| 1182 | else |
| 1183 | log->Printf ("NativeProcessLinux::%s leaving STDERR as is", __FUNCTION__); |
| 1184 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1185 | |
| 1186 | // Create the NativeProcessLinux in launch mode. |
| 1187 | native_process_sp.reset (new NativeProcessLinux ()); |
| 1188 | |
| 1189 | if (log) |
| 1190 | { |
| 1191 | int i = 0; |
| 1192 | for (const char **args = launch_info.GetArguments ().GetConstArgumentVector (); *args; ++args, ++i) |
| 1193 | { |
| 1194 | log->Printf ("NativeProcessLinux::%s arg %d: \"%s\"", __FUNCTION__, i, *args ? *args : "nullptr"); |
| 1195 | ++i; |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | if (!native_process_sp->RegisterNativeDelegate (native_delegate)) |
| 1200 | { |
| 1201 | native_process_sp.reset (); |
| 1202 | error.SetErrorStringWithFormat ("failed to register the native delegate"); |
| 1203 | return error; |
| 1204 | } |
| 1205 | |
| 1206 | reinterpret_cast<NativeProcessLinux*> (native_process_sp.get ())->LaunchInferior ( |
| 1207 | exe_module, |
| 1208 | launch_info.GetArguments ().GetConstArgumentVector (), |
| 1209 | launch_info.GetEnvironmentEntries ().GetConstArgumentVector (), |
| 1210 | stdin_path, |
| 1211 | stdout_path, |
| 1212 | stderr_path, |
| 1213 | working_dir, |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1214 | launch_info, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1215 | error); |
| 1216 | |
| 1217 | if (error.Fail ()) |
| 1218 | { |
| 1219 | native_process_sp.reset (); |
| 1220 | if (log) |
| 1221 | log->Printf ("NativeProcessLinux::%s failed to launch process: %s", __FUNCTION__, error.AsCString ()); |
| 1222 | return error; |
| 1223 | } |
| 1224 | |
| 1225 | launch_info.SetProcessID (native_process_sp->GetID ()); |
| 1226 | |
| 1227 | return error; |
| 1228 | } |
| 1229 | |
| 1230 | lldb_private::Error |
| 1231 | NativeProcessLinux::AttachToProcess ( |
| 1232 | lldb::pid_t pid, |
| 1233 | lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate, |
| 1234 | NativeProcessProtocolSP &native_process_sp) |
| 1235 | { |
| 1236 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1237 | if (log && log->GetMask ().Test (POSIX_LOG_VERBOSE)) |
| 1238 | log->Printf ("NativeProcessLinux::%s(pid = %" PRIi64 ")", __FUNCTION__, pid); |
| 1239 | |
| 1240 | // Grab the current platform architecture. This should be Linux, |
| 1241 | // since this code is only intended to run on a Linux host. |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 1242 | PlatformSP platform_sp (Platform::GetHostPlatform ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1243 | if (!platform_sp) |
| 1244 | return Error("failed to get a valid default platform"); |
| 1245 | |
| 1246 | // Retrieve the architecture for the running process. |
| 1247 | ArchSpec process_arch; |
| 1248 | Error error = ResolveProcessArchitecture (pid, *platform_sp.get (), process_arch); |
| 1249 | if (!error.Success ()) |
| 1250 | return error; |
| 1251 | |
Oleksiy Vyalov | 1339b5e | 2014-11-13 18:22:16 +0000 | [diff] [blame] | 1252 | std::shared_ptr<NativeProcessLinux> native_process_linux_sp (new NativeProcessLinux ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1253 | |
Oleksiy Vyalov | 1339b5e | 2014-11-13 18:22:16 +0000 | [diff] [blame] | 1254 | if (!native_process_linux_sp->RegisterNativeDelegate (native_delegate)) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1255 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1256 | error.SetErrorStringWithFormat ("failed to register the native delegate"); |
| 1257 | return error; |
| 1258 | } |
| 1259 | |
Oleksiy Vyalov | 1339b5e | 2014-11-13 18:22:16 +0000 | [diff] [blame] | 1260 | native_process_linux_sp->AttachToInferior (pid, error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1261 | if (!error.Success ()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1262 | return error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1263 | |
Oleksiy Vyalov | 1339b5e | 2014-11-13 18:22:16 +0000 | [diff] [blame] | 1264 | native_process_sp = native_process_linux_sp; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1265 | return error; |
| 1266 | } |
| 1267 | |
| 1268 | // ----------------------------------------------------------------------------- |
| 1269 | // Public Instance Methods |
| 1270 | // ----------------------------------------------------------------------------- |
| 1271 | |
| 1272 | NativeProcessLinux::NativeProcessLinux () : |
| 1273 | NativeProcessProtocol (LLDB_INVALID_PROCESS_ID), |
| 1274 | m_arch (), |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1275 | m_operation_thread (), |
| 1276 | m_monitor_thread (), |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1277 | m_operation (nullptr), |
| 1278 | m_operation_mutex (), |
| 1279 | m_operation_pending (), |
| 1280 | m_operation_done (), |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1281 | m_supports_mem_region (eLazyBoolCalculate), |
| 1282 | m_mem_region_cache (), |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1283 | m_mem_region_cache_mutex (), |
| 1284 | m_coordinator_up (new ThreadStateCoordinator (GetThreadLoggerFunction ())), |
| 1285 | m_coordinator_thread () |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1286 | { |
| 1287 | } |
| 1288 | |
| 1289 | //------------------------------------------------------------------------------ |
| 1290 | /// The basic design of the NativeProcessLinux is built around two threads. |
| 1291 | /// |
| 1292 | /// One thread (@see SignalThread) simply blocks on a call to waitpid() looking |
| 1293 | /// for changes in the debugee state. When a change is detected a |
| 1294 | /// ProcessMessage is sent to the associated ProcessLinux instance. This thread |
| 1295 | /// "drives" state changes in the debugger. |
| 1296 | /// |
| 1297 | /// The second thread (@see OperationThread) is responsible for two things 1) |
| 1298 | /// launching or attaching to the inferior process, and then 2) servicing |
| 1299 | /// operations such as register reads/writes, stepping, etc. See the comments |
| 1300 | /// on the Operation class for more info as to why this is needed. |
| 1301 | void |
| 1302 | NativeProcessLinux::LaunchInferior ( |
| 1303 | Module *module, |
| 1304 | const char *argv[], |
| 1305 | const char *envp[], |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1306 | const std::string &stdin_path, |
| 1307 | const std::string &stdout_path, |
| 1308 | const std::string &stderr_path, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1309 | const char *working_dir, |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1310 | const lldb_private::ProcessLaunchInfo &launch_info, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1311 | lldb_private::Error &error) |
| 1312 | { |
| 1313 | if (module) |
| 1314 | m_arch = module->GetArchitecture (); |
| 1315 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1316 | SetState (eStateLaunching); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1317 | |
| 1318 | std::unique_ptr<LaunchArgs> args( |
| 1319 | new LaunchArgs( |
| 1320 | this, module, argv, envp, |
| 1321 | stdin_path, stdout_path, stderr_path, |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1322 | working_dir, launch_info)); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1323 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1324 | sem_init (&m_operation_pending, 0, 0); |
| 1325 | sem_init (&m_operation_done, 0, 0); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1326 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1327 | StartLaunchOpThread (args.get(), error); |
| 1328 | if (!error.Success ()) |
| 1329 | return; |
| 1330 | |
| 1331 | error = StartCoordinatorThread (); |
| 1332 | if (!error.Success ()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1333 | return; |
| 1334 | |
| 1335 | WAIT_AGAIN: |
| 1336 | // Wait for the operation thread to initialize. |
| 1337 | if (sem_wait(&args->m_semaphore)) |
| 1338 | { |
| 1339 | if (errno == EINTR) |
| 1340 | goto WAIT_AGAIN; |
| 1341 | else |
| 1342 | { |
| 1343 | error.SetErrorToErrno(); |
| 1344 | return; |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | // Check that the launch was a success. |
| 1349 | if (!args->m_error.Success()) |
| 1350 | { |
| 1351 | StopOpThread(); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1352 | StopCoordinatorThread (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1353 | error = args->m_error; |
| 1354 | return; |
| 1355 | } |
| 1356 | |
| 1357 | // Finally, start monitoring the child process for change in state. |
| 1358 | m_monitor_thread = Host::StartMonitoringChildProcess( |
| 1359 | NativeProcessLinux::MonitorCallback, this, GetID(), true); |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 1360 | if (!m_monitor_thread.IsJoinable()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1361 | { |
| 1362 | error.SetErrorToGenericError(); |
| 1363 | error.SetErrorString ("Process attach failed to create monitor thread for NativeProcessLinux::MonitorCallback."); |
| 1364 | return; |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | void |
| 1369 | NativeProcessLinux::AttachToInferior (lldb::pid_t pid, lldb_private::Error &error) |
| 1370 | { |
| 1371 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1372 | if (log) |
| 1373 | log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ")", __FUNCTION__, pid); |
| 1374 | |
| 1375 | // We can use the Host for everything except the ResolveExecutable portion. |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 1376 | PlatformSP platform_sp = Platform::GetHostPlatform (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1377 | if (!platform_sp) |
| 1378 | { |
| 1379 | if (log) |
| 1380 | log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 "): no default platform set", __FUNCTION__, pid); |
| 1381 | error.SetErrorString ("no default platform available"); |
Shawn Best | 50d60be | 2014-11-11 00:28:52 +0000 | [diff] [blame] | 1382 | return; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | // Gather info about the process. |
| 1386 | ProcessInstanceInfo process_info; |
Shawn Best | 50d60be | 2014-11-11 00:28:52 +0000 | [diff] [blame] | 1387 | if (!platform_sp->GetProcessInfo (pid, process_info)) |
| 1388 | { |
| 1389 | if (log) |
| 1390 | log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 "): failed to get process info", __FUNCTION__, pid); |
| 1391 | error.SetErrorString ("failed to get process info"); |
| 1392 | return; |
| 1393 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1394 | |
| 1395 | // Resolve the executable module |
| 1396 | ModuleSP exe_module_sp; |
| 1397 | FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths()); |
Chaoren Lin | e56f6dc | 2015-03-01 04:31:16 +0000 | [diff] [blame] | 1398 | ModuleSpec exe_module_spec(process_info.GetExecutableFile(), process_info.GetArchitecture()); |
Oleksiy Vyalov | 6edef20 | 2014-11-17 22:16:42 +0000 | [diff] [blame] | 1399 | error = platform_sp->ResolveExecutable(exe_module_spec, exe_module_sp, |
Zachary Turner | 13b1826 | 2014-08-20 16:42:51 +0000 | [diff] [blame] | 1400 | executable_search_paths.GetSize() ? &executable_search_paths : NULL); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1401 | if (!error.Success()) |
| 1402 | return; |
| 1403 | |
| 1404 | // Set the architecture to the exe architecture. |
| 1405 | m_arch = exe_module_sp->GetArchitecture(); |
| 1406 | if (log) |
| 1407 | log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ") detected architecture %s", __FUNCTION__, pid, m_arch.GetArchitectureName ()); |
| 1408 | |
| 1409 | m_pid = pid; |
| 1410 | SetState(eStateAttaching); |
| 1411 | |
| 1412 | sem_init (&m_operation_pending, 0, 0); |
| 1413 | sem_init (&m_operation_done, 0, 0); |
| 1414 | |
| 1415 | std::unique_ptr<AttachArgs> args (new AttachArgs (this, pid)); |
| 1416 | |
| 1417 | StartAttachOpThread(args.get (), error); |
| 1418 | if (!error.Success ()) |
| 1419 | return; |
| 1420 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1421 | error = StartCoordinatorThread (); |
| 1422 | if (!error.Success ()) |
| 1423 | return; |
| 1424 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1425 | WAIT_AGAIN: |
| 1426 | // Wait for the operation thread to initialize. |
| 1427 | if (sem_wait (&args->m_semaphore)) |
| 1428 | { |
| 1429 | if (errno == EINTR) |
| 1430 | goto WAIT_AGAIN; |
| 1431 | else |
| 1432 | { |
| 1433 | error.SetErrorToErrno (); |
| 1434 | return; |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | // Check that the attach was a success. |
| 1439 | if (!args->m_error.Success ()) |
| 1440 | { |
| 1441 | StopOpThread (); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1442 | StopCoordinatorThread (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1443 | error = args->m_error; |
| 1444 | return; |
| 1445 | } |
| 1446 | |
| 1447 | // Finally, start monitoring the child process for change in state. |
| 1448 | m_monitor_thread = Host::StartMonitoringChildProcess ( |
| 1449 | NativeProcessLinux::MonitorCallback, this, GetID (), true); |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 1450 | if (!m_monitor_thread.IsJoinable()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1451 | { |
| 1452 | error.SetErrorToGenericError (); |
| 1453 | error.SetErrorString ("Process attach failed to create monitor thread for NativeProcessLinux::MonitorCallback."); |
| 1454 | return; |
| 1455 | } |
| 1456 | } |
| 1457 | |
Oleksiy Vyalov | 8bc34f4 | 2015-02-19 17:58:04 +0000 | [diff] [blame] | 1458 | void |
| 1459 | NativeProcessLinux::Terminate () |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1460 | { |
| 1461 | StopMonitor(); |
| 1462 | } |
| 1463 | |
| 1464 | //------------------------------------------------------------------------------ |
| 1465 | // Thread setup and tear down. |
| 1466 | |
| 1467 | void |
| 1468 | NativeProcessLinux::StartLaunchOpThread(LaunchArgs *args, Error &error) |
| 1469 | { |
| 1470 | static const char *g_thread_name = "lldb.process.nativelinux.operation"; |
| 1471 | |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 1472 | if (m_operation_thread.IsJoinable()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1473 | return; |
| 1474 | |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 1475 | m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | void * |
| 1479 | NativeProcessLinux::LaunchOpThread(void *arg) |
| 1480 | { |
| 1481 | LaunchArgs *args = static_cast<LaunchArgs*>(arg); |
| 1482 | |
| 1483 | if (!Launch(args)) { |
| 1484 | sem_post(&args->m_semaphore); |
| 1485 | return NULL; |
| 1486 | } |
| 1487 | |
| 1488 | ServeOperation(args); |
| 1489 | return NULL; |
| 1490 | } |
| 1491 | |
| 1492 | bool |
| 1493 | NativeProcessLinux::Launch(LaunchArgs *args) |
| 1494 | { |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1495 | assert (args && "null args"); |
| 1496 | if (!args) |
| 1497 | return false; |
| 1498 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1499 | NativeProcessLinux *monitor = args->m_monitor; |
| 1500 | assert (monitor && "monitor is NULL"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1501 | |
| 1502 | const char **argv = args->m_argv; |
| 1503 | const char **envp = args->m_envp; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1504 | const char *working_dir = args->m_working_dir; |
| 1505 | |
| 1506 | lldb_utility::PseudoTerminal terminal; |
| 1507 | const size_t err_len = 1024; |
| 1508 | char err_str[err_len]; |
| 1509 | lldb::pid_t pid; |
| 1510 | NativeThreadProtocolSP thread_sp; |
| 1511 | |
| 1512 | lldb::ThreadSP inferior; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1513 | |
| 1514 | // Propagate the environment if one is not supplied. |
| 1515 | if (envp == NULL || envp[0] == NULL) |
| 1516 | envp = const_cast<const char **>(environ); |
| 1517 | |
| 1518 | if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t> (-1)) |
| 1519 | { |
| 1520 | args->m_error.SetErrorToGenericError(); |
| 1521 | args->m_error.SetErrorString("Process fork failed."); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1522 | return false; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | // Recognized child exit status codes. |
| 1526 | enum { |
| 1527 | ePtraceFailed = 1, |
| 1528 | eDupStdinFailed, |
| 1529 | eDupStdoutFailed, |
| 1530 | eDupStderrFailed, |
| 1531 | eChdirFailed, |
| 1532 | eExecFailed, |
| 1533 | eSetGidFailed |
| 1534 | }; |
| 1535 | |
| 1536 | // Child process. |
| 1537 | if (pid == 0) |
| 1538 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1539 | // FIXME consider opening a pipe between parent/child and have this forked child |
| 1540 | // send log info to parent re: launch status, in place of the log lines removed here. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1541 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1542 | // Start tracing this child that is about to exec. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 1543 | PTRACE(PTRACE_TRACEME, 0, nullptr, nullptr, 0, args->m_error); |
| 1544 | if (args->m_error.Fail()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1545 | exit(ePtraceFailed); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1546 | |
Pavel Labath | 493c3a1 | 2015-02-04 10:36:57 +0000 | [diff] [blame] | 1547 | // terminal has already dupped the tty descriptors to stdin/out/err. |
| 1548 | // This closes original fd from which they were copied (and avoids |
| 1549 | // leaking descriptors to the debugged process. |
| 1550 | terminal.CloseSlaveFileDescriptor(); |
| 1551 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1552 | // Do not inherit setgid powers. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1553 | if (setgid(getgid()) != 0) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1554 | exit(eSetGidFailed); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1555 | |
| 1556 | // Attempt to have our own process group. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1557 | if (setpgid(0, 0) != 0) |
| 1558 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1559 | // FIXME log that this failed. This is common. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1560 | // Don't allow this to prevent an inferior exec. |
| 1561 | } |
| 1562 | |
| 1563 | // Dup file descriptors if needed. |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1564 | if (!args->m_stdin_path.empty ()) |
| 1565 | if (!DupDescriptor(args->m_stdin_path.c_str (), STDIN_FILENO, O_RDONLY)) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1566 | exit(eDupStdinFailed); |
| 1567 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1568 | if (!args->m_stdout_path.empty ()) |
Tamas Berghammer | 14f4476 | 2015-02-25 13:21:45 +0000 | [diff] [blame] | 1569 | if (!DupDescriptor(args->m_stdout_path.c_str (), STDOUT_FILENO, O_WRONLY | O_CREAT | O_TRUNC)) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1570 | exit(eDupStdoutFailed); |
| 1571 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1572 | if (!args->m_stderr_path.empty ()) |
Tamas Berghammer | 14f4476 | 2015-02-25 13:21:45 +0000 | [diff] [blame] | 1573 | if (!DupDescriptor(args->m_stderr_path.c_str (), STDERR_FILENO, O_WRONLY | O_CREAT | O_TRUNC)) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1574 | exit(eDupStderrFailed); |
| 1575 | |
| 1576 | // Change working directory |
| 1577 | if (working_dir != NULL && working_dir[0]) |
| 1578 | if (0 != ::chdir(working_dir)) |
| 1579 | exit(eChdirFailed); |
| 1580 | |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1581 | // Disable ASLR if requested. |
| 1582 | if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR)) |
| 1583 | { |
| 1584 | const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS); |
| 1585 | if (old_personality == -1) |
| 1586 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1587 | // Can't retrieve Linux personality. Cannot disable ASLR. |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1588 | } |
| 1589 | else |
| 1590 | { |
| 1591 | const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality); |
| 1592 | if (new_personality == -1) |
| 1593 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1594 | // Disabling ASLR failed. |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1595 | } |
| 1596 | else |
| 1597 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1598 | // Disabling ASLR succeeded. |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1599 | } |
| 1600 | } |
| 1601 | } |
| 1602 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1603 | // Execute. We should never return... |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1604 | execve(argv[0], |
| 1605 | const_cast<char *const *>(argv), |
| 1606 | const_cast<char *const *>(envp)); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1607 | |
| 1608 | // ...unless exec fails. In which case we definitely need to end the child here. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1609 | exit(eExecFailed); |
| 1610 | } |
| 1611 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1612 | // |
| 1613 | // This is the parent code here. |
| 1614 | // |
| 1615 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1616 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1617 | // Wait for the child process to trap on its call to execve. |
| 1618 | ::pid_t wpid; |
| 1619 | int status; |
| 1620 | if ((wpid = waitpid(pid, &status, 0)) < 0) |
| 1621 | { |
| 1622 | args->m_error.SetErrorToErrno(); |
| 1623 | |
| 1624 | if (log) |
| 1625 | log->Printf ("NativeProcessLinux::%s waitpid for inferior failed with %s", __FUNCTION__, args->m_error.AsCString ()); |
| 1626 | |
| 1627 | // Mark the inferior as invalid. |
| 1628 | // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. |
| 1629 | monitor->SetState (StateType::eStateInvalid); |
| 1630 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1631 | return false; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1632 | } |
| 1633 | else if (WIFEXITED(status)) |
| 1634 | { |
| 1635 | // open, dup or execve likely failed for some reason. |
| 1636 | args->m_error.SetErrorToGenericError(); |
| 1637 | switch (WEXITSTATUS(status)) |
| 1638 | { |
| 1639 | case ePtraceFailed: |
| 1640 | args->m_error.SetErrorString("Child ptrace failed."); |
| 1641 | break; |
| 1642 | case eDupStdinFailed: |
| 1643 | args->m_error.SetErrorString("Child open stdin failed."); |
| 1644 | break; |
| 1645 | case eDupStdoutFailed: |
| 1646 | args->m_error.SetErrorString("Child open stdout failed."); |
| 1647 | break; |
| 1648 | case eDupStderrFailed: |
| 1649 | args->m_error.SetErrorString("Child open stderr failed."); |
| 1650 | break; |
| 1651 | case eChdirFailed: |
| 1652 | args->m_error.SetErrorString("Child failed to set working directory."); |
| 1653 | break; |
| 1654 | case eExecFailed: |
| 1655 | args->m_error.SetErrorString("Child exec failed."); |
| 1656 | break; |
| 1657 | case eSetGidFailed: |
| 1658 | args->m_error.SetErrorString("Child setgid failed."); |
| 1659 | break; |
| 1660 | default: |
| 1661 | args->m_error.SetErrorString("Child returned unknown exit status."); |
| 1662 | break; |
| 1663 | } |
| 1664 | |
| 1665 | if (log) |
| 1666 | { |
| 1667 | log->Printf ("NativeProcessLinux::%s inferior exited with status %d before issuing a STOP", |
| 1668 | __FUNCTION__, |
| 1669 | WEXITSTATUS(status)); |
| 1670 | } |
| 1671 | |
| 1672 | // Mark the inferior as invalid. |
| 1673 | // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. |
| 1674 | monitor->SetState (StateType::eStateInvalid); |
| 1675 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1676 | return false; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1677 | } |
Todd Fiala | 202ecd2 | 2014-07-10 04:39:13 +0000 | [diff] [blame] | 1678 | assert(WIFSTOPPED(status) && (wpid == static_cast< ::pid_t> (pid)) && |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1679 | "Could not sync with inferior process."); |
| 1680 | |
| 1681 | if (log) |
| 1682 | log->Printf ("NativeProcessLinux::%s inferior started, now in stopped state", __FUNCTION__); |
| 1683 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 1684 | args->m_error = SetDefaultPtraceOpts(pid); |
| 1685 | if (args->m_error.Fail()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1686 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1687 | if (log) |
| 1688 | log->Printf ("NativeProcessLinux::%s inferior failed to set default ptrace options: %s", |
| 1689 | __FUNCTION__, |
| 1690 | args->m_error.AsCString ()); |
| 1691 | |
| 1692 | // Mark the inferior as invalid. |
| 1693 | // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. |
| 1694 | monitor->SetState (StateType::eStateInvalid); |
| 1695 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1696 | return false; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | // Release the master terminal descriptor and pass it off to the |
| 1700 | // NativeProcessLinux instance. Similarly stash the inferior pid. |
| 1701 | monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor(); |
| 1702 | monitor->m_pid = pid; |
| 1703 | |
| 1704 | // Set the terminal fd to be in non blocking mode (it simplifies the |
| 1705 | // implementation of ProcessLinux::GetSTDOUT to have a non-blocking |
| 1706 | // descriptor to read from). |
| 1707 | if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error)) |
| 1708 | { |
| 1709 | if (log) |
| 1710 | log->Printf ("NativeProcessLinux::%s inferior EnsureFDFlags failed for ensuring terminal O_NONBLOCK setting: %s", |
| 1711 | __FUNCTION__, |
| 1712 | args->m_error.AsCString ()); |
| 1713 | |
| 1714 | // Mark the inferior as invalid. |
| 1715 | // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. |
| 1716 | monitor->SetState (StateType::eStateInvalid); |
| 1717 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1718 | return false; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | if (log) |
| 1722 | log->Printf ("NativeProcessLinux::%s() adding pid = %" PRIu64, __FUNCTION__, pid); |
| 1723 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1724 | thread_sp = monitor->AddThread (pid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1725 | assert (thread_sp && "AddThread() returned a nullptr thread"); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1726 | monitor->NotifyThreadCreateStopped (pid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1727 | reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedBySignal (SIGSTOP); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1728 | |
| 1729 | // Let our process instance know the thread has stopped. |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1730 | monitor->SetCurrentThreadID (thread_sp->GetID ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1731 | monitor->SetState (StateType::eStateStopped); |
| 1732 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1733 | if (log) |
| 1734 | { |
| 1735 | if (args->m_error.Success ()) |
| 1736 | { |
| 1737 | log->Printf ("NativeProcessLinux::%s inferior launching succeeded", __FUNCTION__); |
| 1738 | } |
| 1739 | else |
| 1740 | { |
| 1741 | log->Printf ("NativeProcessLinux::%s inferior launching failed: %s", |
| 1742 | __FUNCTION__, |
| 1743 | args->m_error.AsCString ()); |
| 1744 | } |
| 1745 | } |
| 1746 | return args->m_error.Success(); |
| 1747 | } |
| 1748 | |
| 1749 | void |
| 1750 | NativeProcessLinux::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error) |
| 1751 | { |
| 1752 | static const char *g_thread_name = "lldb.process.linux.operation"; |
| 1753 | |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 1754 | if (m_operation_thread.IsJoinable()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1755 | return; |
| 1756 | |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 1757 | m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | void * |
| 1761 | NativeProcessLinux::AttachOpThread(void *arg) |
| 1762 | { |
| 1763 | AttachArgs *args = static_cast<AttachArgs*>(arg); |
| 1764 | |
| 1765 | if (!Attach(args)) { |
| 1766 | sem_post(&args->m_semaphore); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1767 | return nullptr; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | ServeOperation(args); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1771 | return nullptr; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | bool |
| 1775 | NativeProcessLinux::Attach(AttachArgs *args) |
| 1776 | { |
| 1777 | lldb::pid_t pid = args->m_pid; |
| 1778 | |
| 1779 | NativeProcessLinux *monitor = args->m_monitor; |
| 1780 | lldb::ThreadSP inferior; |
| 1781 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1782 | |
| 1783 | // Use a map to keep track of the threads which we have attached/need to attach. |
| 1784 | Host::TidMap tids_to_attach; |
| 1785 | if (pid <= 1) |
| 1786 | { |
| 1787 | args->m_error.SetErrorToGenericError(); |
| 1788 | args->m_error.SetErrorString("Attaching to process 1 is not allowed."); |
| 1789 | goto FINISH; |
| 1790 | } |
| 1791 | |
| 1792 | while (Host::FindProcessThreads(pid, tids_to_attach)) |
| 1793 | { |
| 1794 | for (Host::TidMap::iterator it = tids_to_attach.begin(); |
| 1795 | it != tids_to_attach.end();) |
| 1796 | { |
| 1797 | if (it->second == false) |
| 1798 | { |
| 1799 | lldb::tid_t tid = it->first; |
| 1800 | |
| 1801 | // Attach to the requested process. |
| 1802 | // An attach will cause the thread to stop with a SIGSTOP. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 1803 | PTRACE(PTRACE_ATTACH, tid, nullptr, nullptr, 0, args->m_error); |
| 1804 | if (args->m_error.Fail()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1805 | { |
| 1806 | // No such thread. The thread may have exited. |
| 1807 | // More error handling may be needed. |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 1808 | if (args->m_error.GetError() == ESRCH) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1809 | { |
| 1810 | it = tids_to_attach.erase(it); |
| 1811 | continue; |
| 1812 | } |
| 1813 | else |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1814 | goto FINISH; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | int status; |
| 1818 | // Need to use __WALL otherwise we receive an error with errno=ECHLD |
| 1819 | // At this point we should have a thread stopped if waitpid succeeds. |
| 1820 | if ((status = waitpid(tid, NULL, __WALL)) < 0) |
| 1821 | { |
| 1822 | // No such thread. The thread may have exited. |
| 1823 | // More error handling may be needed. |
| 1824 | if (errno == ESRCH) |
| 1825 | { |
| 1826 | it = tids_to_attach.erase(it); |
| 1827 | continue; |
| 1828 | } |
| 1829 | else |
| 1830 | { |
| 1831 | args->m_error.SetErrorToErrno(); |
| 1832 | goto FINISH; |
| 1833 | } |
| 1834 | } |
| 1835 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 1836 | args->m_error = SetDefaultPtraceOpts(tid); |
| 1837 | if (args->m_error.Fail()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1838 | goto FINISH; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1839 | |
| 1840 | |
| 1841 | if (log) |
| 1842 | log->Printf ("NativeProcessLinux::%s() adding tid = %" PRIu64, __FUNCTION__, tid); |
| 1843 | |
| 1844 | it->second = true; |
| 1845 | |
| 1846 | // Create the thread, mark it as stopped. |
| 1847 | NativeThreadProtocolSP thread_sp (monitor->AddThread (static_cast<lldb::tid_t> (tid))); |
| 1848 | assert (thread_sp && "AddThread() returned a nullptr"); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1849 | |
| 1850 | // This will notify this is a new thread and tell the system it is stopped. |
| 1851 | monitor->NotifyThreadCreateStopped (tid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1852 | reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedBySignal (SIGSTOP); |
| 1853 | monitor->SetCurrentThreadID (thread_sp->GetID ()); |
| 1854 | } |
| 1855 | |
| 1856 | // move the loop forward |
| 1857 | ++it; |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | if (tids_to_attach.size() > 0) |
| 1862 | { |
| 1863 | monitor->m_pid = pid; |
| 1864 | // Let our process instance know the thread has stopped. |
| 1865 | monitor->SetState (StateType::eStateStopped); |
| 1866 | } |
| 1867 | else |
| 1868 | { |
| 1869 | args->m_error.SetErrorToGenericError(); |
| 1870 | args->m_error.SetErrorString("No such process."); |
| 1871 | } |
| 1872 | |
| 1873 | FINISH: |
| 1874 | return args->m_error.Success(); |
| 1875 | } |
| 1876 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 1877 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1878 | NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) |
| 1879 | { |
| 1880 | long ptrace_opts = 0; |
| 1881 | |
| 1882 | // Have the child raise an event on exit. This is used to keep the child in |
| 1883 | // limbo until it is destroyed. |
| 1884 | ptrace_opts |= PTRACE_O_TRACEEXIT; |
| 1885 | |
| 1886 | // Have the tracer trace threads which spawn in the inferior process. |
| 1887 | // TODO: if we want to support tracing the inferiors' child, add the |
| 1888 | // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK) |
| 1889 | ptrace_opts |= PTRACE_O_TRACECLONE; |
| 1890 | |
| 1891 | // Have the tracer notify us before execve returns |
| 1892 | // (needed to disable legacy SIGTRAP generation) |
| 1893 | ptrace_opts |= PTRACE_O_TRACEEXEC; |
| 1894 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 1895 | Error error; |
| 1896 | PTRACE(PTRACE_SETOPTIONS, pid, nullptr, (void*)ptrace_opts, 0, error); |
| 1897 | return error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | static ExitType convert_pid_status_to_exit_type (int status) |
| 1901 | { |
| 1902 | if (WIFEXITED (status)) |
| 1903 | return ExitType::eExitTypeExit; |
| 1904 | else if (WIFSIGNALED (status)) |
| 1905 | return ExitType::eExitTypeSignal; |
| 1906 | else if (WIFSTOPPED (status)) |
| 1907 | return ExitType::eExitTypeStop; |
| 1908 | else |
| 1909 | { |
| 1910 | // We don't know what this is. |
| 1911 | return ExitType::eExitTypeInvalid; |
| 1912 | } |
| 1913 | } |
| 1914 | |
| 1915 | static int convert_pid_status_to_return_code (int status) |
| 1916 | { |
| 1917 | if (WIFEXITED (status)) |
| 1918 | return WEXITSTATUS (status); |
| 1919 | else if (WIFSIGNALED (status)) |
| 1920 | return WTERMSIG (status); |
| 1921 | else if (WIFSTOPPED (status)) |
| 1922 | return WSTOPSIG (status); |
| 1923 | else |
| 1924 | { |
| 1925 | // We don't know what this is. |
| 1926 | return ExitType::eExitTypeInvalid; |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | // Main process monitoring waitpid-loop handler. |
| 1931 | bool |
| 1932 | NativeProcessLinux::MonitorCallback(void *callback_baton, |
| 1933 | lldb::pid_t pid, |
| 1934 | bool exited, |
| 1935 | int signal, |
| 1936 | int status) |
| 1937 | { |
| 1938 | Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1939 | |
| 1940 | NativeProcessLinux *const process = static_cast<NativeProcessLinux*>(callback_baton); |
| 1941 | assert (process && "process is null"); |
| 1942 | if (!process) |
| 1943 | { |
| 1944 | if (log) |
| 1945 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " callback_baton was null, can't determine process to use", __FUNCTION__, pid); |
| 1946 | return true; |
| 1947 | } |
| 1948 | |
| 1949 | // Certain activities differ based on whether the pid is the tid of the main thread. |
| 1950 | const bool is_main_thread = (pid == process->GetID ()); |
| 1951 | |
| 1952 | // Assume we keep monitoring by default. |
| 1953 | bool stop_monitoring = false; |
| 1954 | |
| 1955 | // Handle when the thread exits. |
| 1956 | if (exited) |
| 1957 | { |
| 1958 | if (log) |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 1959 | log->Printf ("NativeProcessLinux::%s() got exit signal(%d) , tid = %" PRIu64 " (%s main thread)", __FUNCTION__, signal, pid, is_main_thread ? "is" : "is not"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1960 | |
| 1961 | // This is a thread that exited. Ensure we're not tracking it anymore. |
| 1962 | const bool thread_found = process->StopTrackingThread (pid); |
| 1963 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1964 | // Make sure the thread state coordinator knows about this. |
| 1965 | process->NotifyThreadDeath (pid); |
| 1966 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1967 | if (is_main_thread) |
| 1968 | { |
| 1969 | // We only set the exit status and notify the delegate if we haven't already set the process |
| 1970 | // state to an exited state. We normally should have received a SIGTRAP | (PTRACE_EVENT_EXIT << 8) |
| 1971 | // for the main thread. |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1972 | const bool already_notified = (process->GetState() == StateType::eStateExited) || (process->GetState () == StateType::eStateCrashed); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1973 | if (!already_notified) |
| 1974 | { |
| 1975 | if (log) |
| 1976 | log->Printf ("NativeProcessLinux::%s() tid = %" PRIu64 " handling main thread exit (%s), expected exit state already set but state was %s instead, setting exit state now", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found", StateAsCString (process->GetState ())); |
| 1977 | // The main thread exited. We're done monitoring. Report to delegate. |
| 1978 | process->SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); |
| 1979 | |
| 1980 | // Notify delegate that our process has exited. |
| 1981 | process->SetState (StateType::eStateExited, true); |
| 1982 | } |
| 1983 | else |
| 1984 | { |
| 1985 | if (log) |
| 1986 | log->Printf ("NativeProcessLinux::%s() tid = %" PRIu64 " main thread now exited (%s)", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found"); |
| 1987 | } |
| 1988 | return true; |
| 1989 | } |
| 1990 | else |
| 1991 | { |
| 1992 | // Do we want to report to the delegate in this case? I think not. If this was an orderly |
| 1993 | // thread exit, we would already have received the SIGTRAP | (PTRACE_EVENT_EXIT << 8) signal, |
| 1994 | // and we would have done an all-stop then. |
| 1995 | if (log) |
| 1996 | log->Printf ("NativeProcessLinux::%s() tid = %" PRIu64 " handling non-main thread exit (%s)", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found"); |
| 1997 | |
| 1998 | // Not the main thread, we keep going. |
| 1999 | return false; |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | // Get details on the signal raised. |
| 2004 | siginfo_t info; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 2005 | const auto err = process->GetSignalInfo(pid, &info); |
| 2006 | if (err.Success()) |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2007 | { |
| 2008 | // We have retrieved the signal info. Dispatch appropriately. |
| 2009 | if (info.si_signo == SIGTRAP) |
| 2010 | process->MonitorSIGTRAP(&info, pid); |
| 2011 | else |
| 2012 | process->MonitorSignal(&info, pid, exited); |
| 2013 | |
| 2014 | stop_monitoring = false; |
| 2015 | } |
| 2016 | else |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2017 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 2018 | if (err.GetError() == EINVAL) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2019 | { |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2020 | // This is a group stop reception for this tid. |
| 2021 | if (log) |
| 2022 | log->Printf ("NativeThreadLinux::%s received a group stop for pid %" PRIu64 " tid %" PRIu64, __FUNCTION__, process->GetID (), pid); |
| 2023 | process->NotifyThreadStop (pid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2024 | } |
| 2025 | else |
| 2026 | { |
| 2027 | // ptrace(GETSIGINFO) failed (but not due to group-stop). |
| 2028 | |
| 2029 | // A return value of ESRCH means the thread/process is no longer on the system, |
| 2030 | // so it was killed somehow outside of our control. Either way, we can't do anything |
| 2031 | // with it anymore. |
| 2032 | |
| 2033 | // We stop monitoring if it was the main thread. |
| 2034 | stop_monitoring = is_main_thread; |
| 2035 | |
| 2036 | // Stop tracking the metadata for the thread since it's entirely off the system now. |
| 2037 | const bool thread_found = process->StopTrackingThread (pid); |
| 2038 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2039 | // Make sure the thread state coordinator knows about this. |
| 2040 | process->NotifyThreadDeath (pid); |
| 2041 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2042 | if (log) |
| 2043 | log->Printf ("NativeProcessLinux::%s GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d (%s, %s, %s)", |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 2044 | __FUNCTION__, err.AsCString(), pid, signal, status, err.GetError() == ESRCH ? "thread/process killed" : "unknown reason", is_main_thread ? "is main thread" : "is not main thread", thread_found ? "thread metadata removed" : "thread metadata not found"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2045 | |
| 2046 | if (is_main_thread) |
| 2047 | { |
| 2048 | // Notify the delegate - our process is not available but appears to have been killed outside |
| 2049 | // our control. Is eStateExited the right exit state in this case? |
| 2050 | process->SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); |
| 2051 | process->SetState (StateType::eStateExited, true); |
| 2052 | } |
| 2053 | else |
| 2054 | { |
| 2055 | // This thread was pulled out from underneath us. Anything to do here? Do we want to do an all stop? |
| 2056 | if (log) |
| 2057 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 " non-main thread exit occurred, didn't tell delegate anything since thread disappeared out from underneath us", __FUNCTION__, process->GetID (), pid); |
| 2058 | } |
| 2059 | } |
| 2060 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2061 | |
| 2062 | return stop_monitoring; |
| 2063 | } |
| 2064 | |
| 2065 | void |
| 2066 | NativeProcessLinux::MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid) |
| 2067 | { |
| 2068 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 2069 | const bool is_main_thread = (pid == GetID ()); |
| 2070 | |
| 2071 | assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!"); |
| 2072 | if (!info) |
| 2073 | return; |
| 2074 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2075 | Mutex::Locker locker (m_threads_mutex); |
| 2076 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2077 | // See if we can find a thread for this signal. |
| 2078 | NativeThreadProtocolSP thread_sp = GetThreadByID (pid); |
| 2079 | if (!thread_sp) |
| 2080 | { |
| 2081 | if (log) |
| 2082 | log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " no thread found for tid %" PRIu64, __FUNCTION__, GetID (), pid); |
| 2083 | } |
| 2084 | |
| 2085 | switch (info->si_code) |
| 2086 | { |
| 2087 | // TODO: these two cases are required if we want to support tracing of the inferiors' children. We'd need this to debug a monitor. |
| 2088 | // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)): |
| 2089 | // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)): |
| 2090 | |
| 2091 | case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)): |
| 2092 | { |
| 2093 | lldb::tid_t tid = LLDB_INVALID_THREAD_ID; |
| 2094 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2095 | // The main thread is stopped here. |
| 2096 | if (thread_sp) |
| 2097 | reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedBySignal (SIGTRAP); |
| 2098 | NotifyThreadStop (pid); |
| 2099 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2100 | unsigned long event_message = 0; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 2101 | if (GetEventMessage (pid, &event_message).Success()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2102 | { |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2103 | tid = static_cast<lldb::tid_t> (event_message); |
| 2104 | if (log) |
| 2105 | log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " received thread creation event for tid %" PRIu64, __FUNCTION__, pid, tid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2106 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2107 | // If we don't track the thread yet: create it, mark as stopped. |
| 2108 | // If we do track it, this is the wait we needed. Now resume the new thread. |
| 2109 | // In all cases, resume the current (i.e. main process) thread. |
| 2110 | bool created_now = false; |
| 2111 | NativeThreadProtocolSP new_thread_sp = GetOrCreateThread (tid, created_now); |
| 2112 | assert (new_thread_sp.get() && "failed to get or create the tracking data for newly created inferior thread"); |
| 2113 | |
| 2114 | // If the thread was already tracked, it means the created thread already received its SI_USER notification of creation. |
| 2115 | if (!created_now) |
| 2116 | { |
| 2117 | // We can now resume the newly created thread since it is fully created. |
| 2118 | NotifyThreadCreateStopped (tid); |
| 2119 | m_coordinator_up->RequestThreadResume (tid, |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2120 | [=](lldb::tid_t tid_to_resume, bool supress_signal) |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2121 | { |
| 2122 | reinterpret_cast<NativeThreadLinux*> (new_thread_sp.get ())->SetRunning (); |
Chaoren Lin | 37c768c | 2015-02-03 01:51:30 +0000 | [diff] [blame] | 2123 | return Resume (tid_to_resume, LLDB_INVALID_SIGNAL_NUMBER); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2124 | }, |
| 2125 | CoordinatorErrorHandler); |
| 2126 | } |
| 2127 | else |
| 2128 | { |
| 2129 | // Mark the thread as currently launching. Need to wait for SIGTRAP clone on the main thread before |
| 2130 | // this thread is ready to go. |
| 2131 | reinterpret_cast<NativeThreadLinux*> (new_thread_sp.get ())->SetLaunching (); |
| 2132 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2133 | } |
| 2134 | else |
| 2135 | { |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2136 | if (log) |
| 2137 | log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " received thread creation event but GetEventMessage failed so we don't know the new tid", __FUNCTION__, pid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | // In all cases, we can resume the main thread here. |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2141 | m_coordinator_up->RequestThreadResume (pid, |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2142 | [=](lldb::tid_t tid_to_resume, bool supress_signal) |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2143 | { |
| 2144 | reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetRunning (); |
Chaoren Lin | 37c768c | 2015-02-03 01:51:30 +0000 | [diff] [blame] | 2145 | return Resume (tid_to_resume, LLDB_INVALID_SIGNAL_NUMBER); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2146 | }, |
| 2147 | CoordinatorErrorHandler); |
| 2148 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2149 | break; |
| 2150 | } |
| 2151 | |
| 2152 | case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2153 | { |
| 2154 | NativeThreadProtocolSP main_thread_sp; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2155 | if (log) |
| 2156 | log->Printf ("NativeProcessLinux::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP); |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2157 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2158 | // The thread state coordinator needs to reset due to the exec. |
| 2159 | m_coordinator_up->ResetForExec (); |
| 2160 | |
| 2161 | // Remove all but the main thread here. Linux fork creates a new process which only copies the main thread. Mutexes are in undefined state. |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2162 | if (log) |
| 2163 | log->Printf ("NativeProcessLinux::%s exec received, stop tracking all but main thread", __FUNCTION__); |
| 2164 | |
| 2165 | for (auto thread_sp : m_threads) |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2166 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2167 | const bool is_main_thread = thread_sp && thread_sp->GetID () == GetID (); |
| 2168 | if (is_main_thread) |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2169 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2170 | main_thread_sp = thread_sp; |
| 2171 | if (log) |
| 2172 | log->Printf ("NativeProcessLinux::%s found main thread with tid %" PRIu64 ", keeping", __FUNCTION__, main_thread_sp->GetID ()); |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2173 | } |
| 2174 | else |
| 2175 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2176 | // Tell thread coordinator this thread is dead. |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2177 | if (log) |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2178 | log->Printf ("NativeProcessLinux::%s discarding non-main-thread tid %" PRIu64 " due to exec", __FUNCTION__, thread_sp->GetID ()); |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2179 | } |
| 2180 | } |
| 2181 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2182 | m_threads.clear (); |
| 2183 | |
| 2184 | if (main_thread_sp) |
| 2185 | { |
| 2186 | m_threads.push_back (main_thread_sp); |
| 2187 | SetCurrentThreadID (main_thread_sp->GetID ()); |
| 2188 | reinterpret_cast<NativeThreadLinux*>(main_thread_sp.get())->SetStoppedByExec (); |
| 2189 | } |
| 2190 | else |
| 2191 | { |
| 2192 | SetCurrentThreadID (LLDB_INVALID_THREAD_ID); |
| 2193 | if (log) |
| 2194 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 "no main thread found, discarded all threads, we're in a no-thread state!", __FUNCTION__, GetID ()); |
| 2195 | } |
| 2196 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2197 | // Tell coordinator about about the "new" (since exec) stopped main thread. |
| 2198 | const lldb::tid_t main_thread_tid = GetID (); |
| 2199 | NotifyThreadCreateStopped (main_thread_tid); |
| 2200 | |
| 2201 | // NOTE: ideally these next statements would execute at the same time as the coordinator thread create was executed. |
| 2202 | // Consider a handler that can execute when that happens. |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2203 | // Let our delegate know we have just exec'd. |
| 2204 | NotifyDidExec (); |
| 2205 | |
| 2206 | // If we have a main thread, indicate we are stopped. |
| 2207 | assert (main_thread_sp && "exec called during ptraced process but no main thread metadata tracked"); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2208 | |
| 2209 | // Let the process know we're stopped. |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2210 | CallAfterRunningThreadsStop (pid, |
| 2211 | [=] (lldb::tid_t signaling_tid) |
| 2212 | { |
| 2213 | SetState (StateType::eStateStopped, true); |
| 2214 | }); |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2215 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2216 | break; |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2217 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2218 | |
| 2219 | case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): |
| 2220 | { |
| 2221 | // The inferior process or one of its threads is about to exit. |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2222 | |
| 2223 | // This thread is currently stopped. It's not actually dead yet, just about to be. |
| 2224 | NotifyThreadStop (pid); |
| 2225 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2226 | unsigned long data = 0; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 2227 | if (GetEventMessage(pid, &data).Fail()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2228 | data = -1; |
| 2229 | |
| 2230 | if (log) |
| 2231 | { |
| 2232 | log->Printf ("NativeProcessLinux::%s() received PTRACE_EVENT_EXIT, data = %lx (WIFEXITED=%s,WIFSIGNALED=%s), pid = %" PRIu64 " (%s)", |
| 2233 | __FUNCTION__, |
| 2234 | data, WIFEXITED (data) ? "true" : "false", WIFSIGNALED (data) ? "true" : "false", |
| 2235 | pid, |
| 2236 | is_main_thread ? "is main thread" : "not main thread"); |
| 2237 | } |
| 2238 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2239 | if (is_main_thread) |
| 2240 | { |
| 2241 | SetExitStatus (convert_pid_status_to_exit_type (data), convert_pid_status_to_return_code (data), nullptr, true); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2242 | } |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 2243 | |
Chaoren Lin | 9d617ba | 2015-02-03 01:50:54 +0000 | [diff] [blame] | 2244 | const int signo = static_cast<int> (data); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2245 | m_coordinator_up->RequestThreadResume (pid, |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2246 | [=](lldb::tid_t tid_to_resume, bool supress_signal) |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2247 | { |
| 2248 | reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetRunning (); |
Chaoren Lin | 37c768c | 2015-02-03 01:51:30 +0000 | [diff] [blame] | 2249 | return Resume (tid_to_resume, (supress_signal) ? LLDB_INVALID_SIGNAL_NUMBER : signo); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2250 | }, |
| 2251 | CoordinatorErrorHandler); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2252 | |
| 2253 | break; |
| 2254 | } |
| 2255 | |
| 2256 | case 0: |
| 2257 | case TRAP_TRACE: |
| 2258 | // We receive this on single stepping. |
| 2259 | if (log) |
| 2260 | log->Printf ("NativeProcessLinux::%s() received trace event, pid = %" PRIu64 " (single stepping)", __FUNCTION__, pid); |
| 2261 | |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2262 | if (thread_sp) |
| 2263 | { |
Chaoren Lin | 28e5742 | 2015-02-03 01:51:25 +0000 | [diff] [blame] | 2264 | reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedByTrace (); |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2267 | // This thread is currently stopped. |
| 2268 | NotifyThreadStop (pid); |
| 2269 | |
Chaoren Lin | ae29d39 | 2015-02-03 01:50:46 +0000 | [diff] [blame] | 2270 | // Here we don't have to request the rest of the threads to stop or request a deferred stop. |
| 2271 | // This would have already happened at the time the Resume() with step operation was signaled. |
| 2272 | // At this point, we just need to say we stopped, and the deferred notifcation will fire off |
| 2273 | // once all running threads have checked in as stopped. |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2274 | SetCurrentThreadID (pid); |
| 2275 | // Tell the process we have a stop (from software breakpoint). |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2276 | CallAfterRunningThreadsStop (pid, |
| 2277 | [=] (lldb::tid_t signaling_tid) |
| 2278 | { |
| 2279 | SetState (StateType::eStateStopped, true); |
| 2280 | }); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2281 | break; |
| 2282 | |
| 2283 | case SI_KERNEL: |
| 2284 | case TRAP_BRKPT: |
| 2285 | if (log) |
| 2286 | log->Printf ("NativeProcessLinux::%s() received breakpoint event, pid = %" PRIu64, __FUNCTION__, pid); |
| 2287 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2288 | // This thread is currently stopped. |
| 2289 | NotifyThreadStop (pid); |
| 2290 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2291 | // Mark the thread as stopped at breakpoint. |
| 2292 | if (thread_sp) |
| 2293 | { |
Chaoren Lin | 28e5742 | 2015-02-03 01:51:25 +0000 | [diff] [blame] | 2294 | reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedByBreakpoint (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2295 | Error error = FixupBreakpointPCAsNeeded (thread_sp); |
| 2296 | if (error.Fail ()) |
| 2297 | { |
| 2298 | if (log) |
| 2299 | log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 " fixup: %s", __FUNCTION__, pid, error.AsCString ()); |
| 2300 | } |
| 2301 | } |
| 2302 | else |
| 2303 | { |
| 2304 | if (log) |
| 2305 | log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 ": warning, cannot process software breakpoint since no thread metadata", __FUNCTION__, pid); |
| 2306 | } |
| 2307 | |
| 2308 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2309 | // We need to tell all other running threads before we notify the delegate about this stop. |
| 2310 | CallAfterRunningThreadsStop (pid, |
| 2311 | [=](lldb::tid_t deferred_notification_tid) |
| 2312 | { |
| 2313 | SetCurrentThreadID (deferred_notification_tid); |
| 2314 | // Tell the process we have a stop (from software breakpoint). |
| 2315 | SetState (StateType::eStateStopped, true); |
| 2316 | }); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2317 | break; |
| 2318 | |
| 2319 | case TRAP_HWBKPT: |
| 2320 | if (log) |
| 2321 | log->Printf ("NativeProcessLinux::%s() received watchpoint event, pid = %" PRIu64, __FUNCTION__, pid); |
| 2322 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2323 | // This thread is currently stopped. |
| 2324 | NotifyThreadStop (pid); |
| 2325 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2326 | // Mark the thread as stopped at watchpoint. |
| 2327 | // The address is at (lldb::addr_t)info->si_addr if we need it. |
| 2328 | if (thread_sp) |
Chaoren Lin | 18fe640 | 2015-02-03 01:51:47 +0000 | [diff] [blame] | 2329 | reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedByWatchpoint (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2330 | else |
| 2331 | { |
| 2332 | if (log) |
| 2333 | log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 ": warning, cannot process hardware breakpoint since no thread metadata", __FUNCTION__, GetID (), pid); |
| 2334 | } |
| 2335 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2336 | // We need to tell all other running threads before we notify the delegate about this stop. |
| 2337 | CallAfterRunningThreadsStop (pid, |
| 2338 | [=](lldb::tid_t deferred_notification_tid) |
| 2339 | { |
| 2340 | SetCurrentThreadID (deferred_notification_tid); |
| 2341 | // Tell the process we have a stop (from hardware breakpoint). |
| 2342 | SetState (StateType::eStateStopped, true); |
| 2343 | }); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2344 | break; |
| 2345 | |
| 2346 | case SIGTRAP: |
| 2347 | case (SIGTRAP | 0x80): |
| 2348 | if (log) |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2349 | log->Printf ("NativeProcessLinux::%s() received unknown SIGTRAP system call stop event, pid %" PRIu64 "tid %" PRIu64 ", resuming", __FUNCTION__, GetID (), pid); |
| 2350 | |
| 2351 | // This thread is currently stopped. |
| 2352 | NotifyThreadStop (pid); |
| 2353 | if (thread_sp) |
| 2354 | reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedBySignal (SIGTRAP); |
| 2355 | |
| 2356 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2357 | // Ignore these signals until we know more about them. |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2358 | m_coordinator_up->RequestThreadResume (pid, |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2359 | [=](lldb::tid_t tid_to_resume, bool supress_signal) |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2360 | { |
| 2361 | reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetRunning (); |
Chaoren Lin | 37c768c | 2015-02-03 01:51:30 +0000 | [diff] [blame] | 2362 | return Resume (tid_to_resume, LLDB_INVALID_SIGNAL_NUMBER); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2363 | }, |
| 2364 | CoordinatorErrorHandler); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2365 | break; |
| 2366 | |
| 2367 | default: |
| 2368 | assert(false && "Unexpected SIGTRAP code!"); |
| 2369 | if (log) |
| 2370 | log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 "tid %" PRIu64 " received unhandled SIGTRAP code: 0x%" PRIx64, __FUNCTION__, GetID (), pid, static_cast<uint64_t> (SIGTRAP | (PTRACE_EVENT_CLONE << 8))); |
| 2371 | break; |
| 2372 | |
| 2373 | } |
| 2374 | } |
| 2375 | |
| 2376 | void |
| 2377 | NativeProcessLinux::MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited) |
| 2378 | { |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 2379 | assert (info && "null info"); |
| 2380 | if (!info) |
| 2381 | return; |
| 2382 | |
| 2383 | const int signo = info->si_signo; |
| 2384 | const bool is_from_llgs = info->si_pid == getpid (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2385 | |
| 2386 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 2387 | |
| 2388 | // POSIX says that process behaviour is undefined after it ignores a SIGFPE, |
| 2389 | // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a |
| 2390 | // kill(2) or raise(3). Similarly for tgkill(2) on Linux. |
| 2391 | // |
| 2392 | // IOW, user generated signals never generate what we consider to be a |
| 2393 | // "crash". |
| 2394 | // |
| 2395 | // Similarly, ACK signals generated by this monitor. |
| 2396 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2397 | Mutex::Locker locker (m_threads_mutex); |
| 2398 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2399 | // See if we can find a thread for this signal. |
| 2400 | NativeThreadProtocolSP thread_sp = GetThreadByID (pid); |
| 2401 | if (!thread_sp) |
| 2402 | { |
| 2403 | if (log) |
| 2404 | log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " no thread found for tid %" PRIu64, __FUNCTION__, GetID (), pid); |
| 2405 | } |
| 2406 | |
| 2407 | // Handle the signal. |
| 2408 | if (info->si_code == SI_TKILL || info->si_code == SI_USER) |
| 2409 | { |
| 2410 | if (log) |
| 2411 | log->Printf ("NativeProcessLinux::%s() received signal %s (%d) with code %s, (siginfo pid = %d (%s), waitpid pid = %" PRIu64 ")", |
| 2412 | __FUNCTION__, |
| 2413 | GetUnixSignals ().GetSignalAsCString (signo), |
| 2414 | signo, |
| 2415 | (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"), |
| 2416 | info->si_pid, |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 2417 | is_from_llgs ? "from llgs" : "not from llgs", |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2418 | pid); |
Todd Fiala | 58a2f66 | 2014-08-12 17:02:07 +0000 | [diff] [blame] | 2419 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2420 | |
Todd Fiala | 58a2f66 | 2014-08-12 17:02:07 +0000 | [diff] [blame] | 2421 | // Check for new thread notification. |
| 2422 | if ((info->si_pid == 0) && (info->si_code == SI_USER)) |
| 2423 | { |
| 2424 | // A new thread creation is being signaled. This is one of two parts that come in |
| 2425 | // a non-deterministic order. pid is the thread id. |
| 2426 | if (log) |
| 2427 | log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 " tid %" PRIu64 ": new thread notification", |
| 2428 | __FUNCTION__, GetID (), pid); |
| 2429 | |
| 2430 | // Did we already create the thread? |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 2431 | bool created_now = false; |
| 2432 | thread_sp = GetOrCreateThread (pid, created_now); |
Todd Fiala | 58a2f66 | 2014-08-12 17:02:07 +0000 | [diff] [blame] | 2433 | assert (thread_sp.get() && "failed to get or create the tracking data for newly created inferior thread"); |
| 2434 | |
| 2435 | // If the thread was already tracked, it means the main thread already received its SIGTRAP for the create. |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 2436 | if (!created_now) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2437 | { |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2438 | // We can now resume the newly created thread since it is fully created. |
| 2439 | NotifyThreadCreateStopped (pid); |
| 2440 | m_coordinator_up->RequestThreadResume (pid, |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2441 | [=](lldb::tid_t tid_to_resume, bool supress_signal) |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2442 | { |
| 2443 | reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetRunning (); |
Chaoren Lin | 37c768c | 2015-02-03 01:51:30 +0000 | [diff] [blame] | 2444 | return Resume (tid_to_resume, LLDB_INVALID_SIGNAL_NUMBER); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2445 | }, |
| 2446 | CoordinatorErrorHandler); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2447 | } |
| 2448 | else |
| 2449 | { |
Todd Fiala | 58a2f66 | 2014-08-12 17:02:07 +0000 | [diff] [blame] | 2450 | // Mark the thread as currently launching. Need to wait for SIGTRAP clone on the main thread before |
| 2451 | // this thread is ready to go. |
| 2452 | reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetLaunching (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
Todd Fiala | 58a2f66 | 2014-08-12 17:02:07 +0000 | [diff] [blame] | 2455 | // Done handling. |
| 2456 | return; |
| 2457 | } |
| 2458 | |
| 2459 | // Check for thread stop notification. |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 2460 | if (is_from_llgs && (info->si_code == SI_TKILL) && (signo == SIGSTOP)) |
Todd Fiala | 58a2f66 | 2014-08-12 17:02:07 +0000 | [diff] [blame] | 2461 | { |
| 2462 | // This is a tgkill()-based stop. |
| 2463 | if (thread_sp) |
| 2464 | { |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2465 | if (log) |
| 2466 | log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 ", thread stopped", |
| 2467 | __FUNCTION__, |
| 2468 | GetID (), |
| 2469 | pid); |
| 2470 | |
Chaoren Lin | aab5863 | 2015-02-03 01:50:57 +0000 | [diff] [blame] | 2471 | // Check that we're not already marked with a stop reason. |
| 2472 | // Note this thread really shouldn't already be marked as stopped - if we were, that would imply that |
| 2473 | // the kernel signaled us with the thread stopping which we handled and marked as stopped, |
| 2474 | // and that, without an intervening resume, we received another stop. It is more likely |
| 2475 | // that we are missing the marking of a run state somewhere if we find that the thread was |
| 2476 | // marked as stopped. |
| 2477 | NativeThreadLinux *const linux_thread_p = reinterpret_cast<NativeThreadLinux*> (thread_sp.get ()); |
| 2478 | assert (linux_thread_p && "linux_thread_p is null!"); |
| 2479 | |
| 2480 | const StateType thread_state = linux_thread_p->GetState (); |
| 2481 | if (!StateIsStoppedState (thread_state, false)) |
| 2482 | { |
| 2483 | // An inferior thread just stopped, but was not the primary cause of the process stop. |
| 2484 | // Instead, something else (like a breakpoint or step) caused the stop. Mark the |
| 2485 | // stop signal as 0 to let lldb know this isn't the important stop. |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2486 | linux_thread_p->SetStoppedBySignal (0); |
Chaoren Lin | aab5863 | 2015-02-03 01:50:57 +0000 | [diff] [blame] | 2487 | SetCurrentThreadID (thread_sp->GetID ()); |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2488 | m_coordinator_up->NotifyThreadStop (thread_sp->GetID (), true, CoordinatorErrorHandler); |
Chaoren Lin | aab5863 | 2015-02-03 01:50:57 +0000 | [diff] [blame] | 2489 | } |
| 2490 | else |
| 2491 | { |
| 2492 | if (log) |
| 2493 | { |
| 2494 | // Retrieve the signal name if the thread was stopped by a signal. |
| 2495 | int stop_signo = 0; |
| 2496 | const bool stopped_by_signal = linux_thread_p->IsStopped (&stop_signo); |
| 2497 | const char *signal_name = stopped_by_signal ? GetUnixSignals ().GetSignalAsCString (stop_signo) : "<not stopped by signal>"; |
| 2498 | if (!signal_name) |
| 2499 | signal_name = "<no-signal-name>"; |
| 2500 | |
| 2501 | log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 ", thread was already marked as a stopped state (state=%s, signal=%d (%s)), leaving stop signal as is", |
| 2502 | __FUNCTION__, |
| 2503 | GetID (), |
| 2504 | linux_thread_p->GetID (), |
| 2505 | StateAsCString (thread_state), |
| 2506 | stop_signo, |
| 2507 | signal_name); |
| 2508 | } |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2509 | // Tell the thread state coordinator about the stop. |
| 2510 | NotifyThreadStop (thread_sp->GetID ()); |
Chaoren Lin | aab5863 | 2015-02-03 01:50:57 +0000 | [diff] [blame] | 2511 | } |
Todd Fiala | 58a2f66 | 2014-08-12 17:02:07 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
| 2514 | // Done handling. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2515 | return; |
| 2516 | } |
| 2517 | |
| 2518 | if (log) |
| 2519 | log->Printf ("NativeProcessLinux::%s() received signal %s", __FUNCTION__, GetUnixSignals ().GetSignalAsCString (signo)); |
| 2520 | |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2521 | // This thread is stopped. |
| 2522 | NotifyThreadStop (pid); |
| 2523 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2524 | switch (signo) |
| 2525 | { |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 2526 | case SIGSTOP: |
| 2527 | { |
| 2528 | if (log) |
| 2529 | { |
| 2530 | if (is_from_llgs) |
| 2531 | log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " received SIGSTOP from llgs, most likely an interrupt", __FUNCTION__, GetID (), pid); |
| 2532 | else |
| 2533 | log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " received SIGSTOP from outside of debugger", __FUNCTION__, GetID (), pid); |
| 2534 | } |
| 2535 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2536 | // Resume this thread to get the group-stop mechanism to fire off the true group stops. |
| 2537 | // This thread will get stopped again as part of the group-stop completion. |
| 2538 | m_coordinator_up->RequestThreadResume (pid, |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2539 | [=](lldb::tid_t tid_to_resume, bool supress_signal) |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2540 | { |
| 2541 | reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetRunning (); |
| 2542 | // Pass this signal number on to the inferior to handle. |
Chaoren Lin | 37c768c | 2015-02-03 01:51:30 +0000 | [diff] [blame] | 2543 | return Resume (tid_to_resume, (supress_signal) ? LLDB_INVALID_SIGNAL_NUMBER : signo); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2544 | }, |
| 2545 | CoordinatorErrorHandler); |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 2546 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2547 | break; |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2548 | case SIGSEGV: |
| 2549 | case SIGILL: |
| 2550 | case SIGFPE: |
| 2551 | case SIGBUS: |
| 2552 | if (thread_sp) |
Chaoren Lin | 28e5742 | 2015-02-03 01:51:25 +0000 | [diff] [blame] | 2553 | reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetCrashedWithException (*info); |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2554 | break; |
| 2555 | default: |
| 2556 | // This is just a pre-signal-delivery notification of the incoming signal. |
| 2557 | if (thread_sp) |
| 2558 | reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedBySignal (signo); |
| 2559 | |
| 2560 | break; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2561 | } |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2562 | |
| 2563 | // Send a stop to the debugger after we get all other threads to stop. |
| 2564 | CallAfterRunningThreadsStop (pid, |
| 2565 | [=] (lldb::tid_t signaling_tid) |
| 2566 | { |
| 2567 | SetCurrentThreadID (signaling_tid); |
| 2568 | SetState (StateType::eStateStopped, true); |
| 2569 | }); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
| 2572 | Error |
| 2573 | NativeProcessLinux::Resume (const ResumeActionList &resume_actions) |
| 2574 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2575 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); |
| 2576 | if (log) |
| 2577 | log->Printf ("NativeProcessLinux::%s called: pid %" PRIu64, __FUNCTION__, GetID ()); |
| 2578 | |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 2579 | lldb::tid_t deferred_signal_tid = LLDB_INVALID_THREAD_ID; |
| 2580 | lldb::tid_t deferred_signal_skip_tid = LLDB_INVALID_THREAD_ID; |
Chaoren Lin | ae29d39 | 2015-02-03 01:50:46 +0000 | [diff] [blame] | 2581 | int deferred_signo = 0; |
| 2582 | NativeThreadProtocolSP deferred_signal_thread_sp; |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2583 | bool stepping = false; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2584 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2585 | Mutex::Locker locker (m_threads_mutex); |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 2586 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2587 | for (auto thread_sp : m_threads) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2588 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2589 | assert (thread_sp && "thread list should not contain NULL threads"); |
| 2590 | |
| 2591 | const ResumeAction *const action = resume_actions.GetActionForThread (thread_sp->GetID (), true); |
| 2592 | |
| 2593 | if (action == nullptr) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2594 | { |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2595 | if (log) |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2596 | log->Printf ("NativeProcessLinux::%s no action specified for pid %" PRIu64 " tid %" PRIu64, |
| 2597 | __FUNCTION__, GetID (), thread_sp->GetID ()); |
| 2598 | continue; |
| 2599 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2600 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2601 | if (log) |
| 2602 | { |
| 2603 | log->Printf ("NativeProcessLinux::%s processing resume action state %s for pid %" PRIu64 " tid %" PRIu64, |
| 2604 | __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); |
| 2605 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2606 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2607 | switch (action->state) |
| 2608 | { |
| 2609 | case eStateRunning: |
| 2610 | { |
| 2611 | // Run the thread, possibly feeding it the signal. |
| 2612 | const int signo = action->signal; |
| 2613 | m_coordinator_up->RequestThreadResumeAsNeeded (thread_sp->GetID (), |
| 2614 | [=](lldb::tid_t tid_to_resume, bool supress_signal) |
| 2615 | { |
| 2616 | reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetRunning (); |
| 2617 | // Pass this signal number on to the inferior to handle. |
| 2618 | const auto resume_result = Resume (tid_to_resume, (signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER); |
| 2619 | if (resume_result.Success()) |
| 2620 | SetState(eStateRunning, true); |
| 2621 | return resume_result; |
| 2622 | }, |
| 2623 | CoordinatorErrorHandler); |
| 2624 | break; |
| 2625 | } |
| 2626 | |
| 2627 | case eStateStepping: |
| 2628 | { |
| 2629 | // Request the step. |
| 2630 | const int signo = action->signal; |
| 2631 | m_coordinator_up->RequestThreadResume (thread_sp->GetID (), |
| 2632 | [=](lldb::tid_t tid_to_step, bool supress_signal) |
| 2633 | { |
| 2634 | reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStepping (); |
| 2635 | const auto step_result = SingleStep (tid_to_step,(signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER); |
| 2636 | assert (step_result.Success() && "SingleStep() failed"); |
| 2637 | if (step_result.Success()) |
| 2638 | SetState(eStateStepping, true); |
| 2639 | return step_result; |
| 2640 | }, |
| 2641 | CoordinatorErrorHandler); |
| 2642 | stepping = true; |
| 2643 | break; |
| 2644 | } |
| 2645 | |
| 2646 | case eStateSuspended: |
| 2647 | case eStateStopped: |
| 2648 | // if we haven't chosen a deferred signal tid yet, use this one. |
| 2649 | if (deferred_signal_tid == LLDB_INVALID_THREAD_ID) |
Chaoren Lin | ae29d39 | 2015-02-03 01:50:46 +0000 | [diff] [blame] | 2650 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2651 | deferred_signal_tid = thread_sp->GetID (); |
| 2652 | deferred_signal_thread_sp = thread_sp; |
| 2653 | deferred_signo = SIGSTOP; |
Chaoren Lin | ae29d39 | 2015-02-03 01:50:46 +0000 | [diff] [blame] | 2654 | } |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2655 | break; |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2656 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2657 | default: |
| 2658 | return Error ("NativeProcessLinux::%s (): unexpected state %s specified for pid %" PRIu64 ", tid %" PRIu64, |
| 2659 | __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2660 | } |
| 2661 | } |
| 2662 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2663 | // If we had any thread stopping, then do a deferred notification of the chosen stop thread id and signal |
| 2664 | // after all other running threads have stopped. |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2665 | // If there is a stepping thread involved we'll be eventually stopped by SIGTRAP trace signal. |
| 2666 | if (deferred_signal_tid != LLDB_INVALID_THREAD_ID && !stepping) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2667 | { |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 2668 | CallAfterRunningThreadsStopWithSkipTID (deferred_signal_tid, |
| 2669 | deferred_signal_skip_tid, |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2670 | [=](lldb::tid_t deferred_notification_tid) |
| 2671 | { |
Chaoren Lin | ae29d39 | 2015-02-03 01:50:46 +0000 | [diff] [blame] | 2672 | // Set the signal thread to the current thread. |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2673 | SetCurrentThreadID (deferred_notification_tid); |
Chaoren Lin | ae29d39 | 2015-02-03 01:50:46 +0000 | [diff] [blame] | 2674 | |
| 2675 | // Set the thread state as stopped by the deferred signo. |
| 2676 | reinterpret_cast<NativeThreadLinux*> (deferred_signal_thread_sp.get ())->SetStoppedBySignal (deferred_signo); |
| 2677 | |
| 2678 | // Tell the process delegate that the process is in a stopped state. |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2679 | SetState (StateType::eStateStopped, true); |
| 2680 | }); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2683 | return Error(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
| 2686 | Error |
| 2687 | NativeProcessLinux::Halt () |
| 2688 | { |
| 2689 | Error error; |
| 2690 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2691 | if (kill (GetID (), SIGSTOP) != 0) |
| 2692 | error.SetErrorToErrno (); |
| 2693 | |
| 2694 | return error; |
| 2695 | } |
| 2696 | |
| 2697 | Error |
| 2698 | NativeProcessLinux::Detach () |
| 2699 | { |
| 2700 | Error error; |
| 2701 | |
| 2702 | // Tell ptrace to detach from the process. |
| 2703 | if (GetID () != LLDB_INVALID_PROCESS_ID) |
| 2704 | error = Detach (GetID ()); |
| 2705 | |
| 2706 | // Stop monitoring the inferior. |
| 2707 | StopMonitor (); |
| 2708 | |
| 2709 | // No error. |
| 2710 | return error; |
| 2711 | } |
| 2712 | |
| 2713 | Error |
| 2714 | NativeProcessLinux::Signal (int signo) |
| 2715 | { |
| 2716 | Error error; |
| 2717 | |
| 2718 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 2719 | if (log) |
| 2720 | log->Printf ("NativeProcessLinux::%s: sending signal %d (%s) to pid %" PRIu64, |
| 2721 | __FUNCTION__, signo, GetUnixSignals ().GetSignalAsCString (signo), GetID ()); |
| 2722 | |
| 2723 | if (kill(GetID(), signo)) |
| 2724 | error.SetErrorToErrno(); |
| 2725 | |
| 2726 | return error; |
| 2727 | } |
| 2728 | |
| 2729 | Error |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 2730 | NativeProcessLinux::Interrupt () |
| 2731 | { |
| 2732 | // Pick a running thread (or if none, a not-dead stopped thread) as |
| 2733 | // the chosen thread that will be the stop-reason thread. |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 2734 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 2735 | |
| 2736 | NativeThreadProtocolSP running_thread_sp; |
| 2737 | NativeThreadProtocolSP stopped_thread_sp; |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2738 | |
| 2739 | if (log) |
| 2740 | log->Printf ("NativeProcessLinux::%s selecting running thread for interrupt target", __FUNCTION__); |
| 2741 | |
| 2742 | Mutex::Locker locker (m_threads_mutex); |
| 2743 | |
| 2744 | for (auto thread_sp : m_threads) |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 2745 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2746 | // The thread shouldn't be null but lets just cover that here. |
| 2747 | if (!thread_sp) |
| 2748 | continue; |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 2749 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2750 | // If we have a running or stepping thread, we'll call that the |
| 2751 | // target of the interrupt. |
| 2752 | const auto thread_state = thread_sp->GetState (); |
| 2753 | if (thread_state == eStateRunning || |
| 2754 | thread_state == eStateStepping) |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 2755 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2756 | running_thread_sp = thread_sp; |
| 2757 | break; |
| 2758 | } |
| 2759 | else if (!stopped_thread_sp && StateIsStoppedState (thread_state, true)) |
| 2760 | { |
| 2761 | // Remember the first non-dead stopped thread. We'll use that as a backup if there are no running threads. |
| 2762 | stopped_thread_sp = thread_sp; |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 2763 | } |
| 2764 | } |
| 2765 | |
| 2766 | if (!running_thread_sp && !stopped_thread_sp) |
| 2767 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2768 | Error error("found no running/stepping or live stopped threads as target for interrupt"); |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 2769 | if (log) |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 2770 | log->Printf ("NativeProcessLinux::%s skipping due to error: %s", __FUNCTION__, error.AsCString ()); |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2771 | |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 2772 | return error; |
| 2773 | } |
| 2774 | |
| 2775 | NativeThreadProtocolSP deferred_signal_thread_sp = running_thread_sp ? running_thread_sp : stopped_thread_sp; |
| 2776 | |
| 2777 | if (log) |
| 2778 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " %s tid %" PRIu64 " chosen for interrupt target", |
| 2779 | __FUNCTION__, |
| 2780 | GetID (), |
| 2781 | running_thread_sp ? "running" : "stopped", |
| 2782 | deferred_signal_thread_sp->GetID ()); |
| 2783 | |
| 2784 | CallAfterRunningThreadsStop (deferred_signal_thread_sp->GetID (), |
| 2785 | [=](lldb::tid_t deferred_notification_tid) |
| 2786 | { |
| 2787 | // Set the signal thread to the current thread. |
| 2788 | SetCurrentThreadID (deferred_notification_tid); |
| 2789 | |
| 2790 | // Set the thread state as stopped by the deferred signo. |
| 2791 | reinterpret_cast<NativeThreadLinux*> (deferred_signal_thread_sp.get ())->SetStoppedBySignal (SIGSTOP); |
| 2792 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2793 | // Tell the process delegate that the process is in a stopped state. |
| 2794 | SetState (StateType::eStateStopped, true); |
| 2795 | }); |
| 2796 | return Error(); |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
| 2799 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2800 | NativeProcessLinux::Kill () |
| 2801 | { |
| 2802 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 2803 | if (log) |
| 2804 | log->Printf ("NativeProcessLinux::%s called for PID %" PRIu64, __FUNCTION__, GetID ()); |
| 2805 | |
| 2806 | Error error; |
| 2807 | |
| 2808 | switch (m_state) |
| 2809 | { |
| 2810 | case StateType::eStateInvalid: |
| 2811 | case StateType::eStateExited: |
| 2812 | case StateType::eStateCrashed: |
| 2813 | case StateType::eStateDetached: |
| 2814 | case StateType::eStateUnloaded: |
| 2815 | // Nothing to do - the process is already dead. |
| 2816 | if (log) |
| 2817 | log->Printf ("NativeProcessLinux::%s ignored for PID %" PRIu64 " due to current state: %s", __FUNCTION__, GetID (), StateAsCString (m_state)); |
| 2818 | return error; |
| 2819 | |
| 2820 | case StateType::eStateConnected: |
| 2821 | case StateType::eStateAttaching: |
| 2822 | case StateType::eStateLaunching: |
| 2823 | case StateType::eStateStopped: |
| 2824 | case StateType::eStateRunning: |
| 2825 | case StateType::eStateStepping: |
| 2826 | case StateType::eStateSuspended: |
| 2827 | // We can try to kill a process in these states. |
| 2828 | break; |
| 2829 | } |
| 2830 | |
| 2831 | if (kill (GetID (), SIGKILL) != 0) |
| 2832 | { |
| 2833 | error.SetErrorToErrno (); |
| 2834 | return error; |
| 2835 | } |
| 2836 | |
| 2837 | return error; |
| 2838 | } |
| 2839 | |
| 2840 | static Error |
| 2841 | ParseMemoryRegionInfoFromProcMapsLine (const std::string &maps_line, MemoryRegionInfo &memory_region_info) |
| 2842 | { |
| 2843 | memory_region_info.Clear(); |
| 2844 | |
| 2845 | StringExtractor line_extractor (maps_line.c_str ()); |
| 2846 | |
| 2847 | // Format: {address_start_hex}-{address_end_hex} perms offset dev inode pathname |
| 2848 | // perms: rwxp (letter is present if set, '-' if not, final character is p=private, s=shared). |
| 2849 | |
| 2850 | // Parse out the starting address |
| 2851 | lldb::addr_t start_address = line_extractor.GetHexMaxU64 (false, 0); |
| 2852 | |
| 2853 | // Parse out hyphen separating start and end address from range. |
| 2854 | if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != '-')) |
| 2855 | return Error ("malformed /proc/{pid}/maps entry, missing dash between address range"); |
| 2856 | |
| 2857 | // Parse out the ending address |
| 2858 | lldb::addr_t end_address = line_extractor.GetHexMaxU64 (false, start_address); |
| 2859 | |
| 2860 | // Parse out the space after the address. |
| 2861 | if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != ' ')) |
| 2862 | return Error ("malformed /proc/{pid}/maps entry, missing space after range"); |
| 2863 | |
| 2864 | // Save the range. |
| 2865 | memory_region_info.GetRange ().SetRangeBase (start_address); |
| 2866 | memory_region_info.GetRange ().SetRangeEnd (end_address); |
| 2867 | |
| 2868 | // Parse out each permission entry. |
| 2869 | if (line_extractor.GetBytesLeft () < 4) |
| 2870 | return Error ("malformed /proc/{pid}/maps entry, missing some portion of permissions"); |
| 2871 | |
| 2872 | // Handle read permission. |
| 2873 | const char read_perm_char = line_extractor.GetChar (); |
| 2874 | if (read_perm_char == 'r') |
| 2875 | memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eYes); |
| 2876 | else |
| 2877 | { |
| 2878 | assert ( (read_perm_char == '-') && "unexpected /proc/{pid}/maps read permission char" ); |
| 2879 | memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); |
| 2880 | } |
| 2881 | |
| 2882 | // Handle write permission. |
| 2883 | const char write_perm_char = line_extractor.GetChar (); |
| 2884 | if (write_perm_char == 'w') |
| 2885 | memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eYes); |
| 2886 | else |
| 2887 | { |
| 2888 | assert ( (write_perm_char == '-') && "unexpected /proc/{pid}/maps write permission char" ); |
| 2889 | memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); |
| 2890 | } |
| 2891 | |
| 2892 | // Handle execute permission. |
| 2893 | const char exec_perm_char = line_extractor.GetChar (); |
| 2894 | if (exec_perm_char == 'x') |
| 2895 | memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eYes); |
| 2896 | else |
| 2897 | { |
| 2898 | assert ( (exec_perm_char == '-') && "unexpected /proc/{pid}/maps exec permission char" ); |
| 2899 | memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); |
| 2900 | } |
| 2901 | |
| 2902 | return Error (); |
| 2903 | } |
| 2904 | |
| 2905 | Error |
| 2906 | NativeProcessLinux::GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) |
| 2907 | { |
| 2908 | // FIXME review that the final memory region returned extends to the end of the virtual address space, |
| 2909 | // with no perms if it is not mapped. |
| 2910 | |
| 2911 | // Use an approach that reads memory regions from /proc/{pid}/maps. |
| 2912 | // Assume proc maps entries are in ascending order. |
| 2913 | // FIXME assert if we find differently. |
| 2914 | Mutex::Locker locker (m_mem_region_cache_mutex); |
| 2915 | |
| 2916 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 2917 | Error error; |
| 2918 | |
| 2919 | if (m_supports_mem_region == LazyBool::eLazyBoolNo) |
| 2920 | { |
| 2921 | // We're done. |
| 2922 | error.SetErrorString ("unsupported"); |
| 2923 | return error; |
| 2924 | } |
| 2925 | |
| 2926 | // If our cache is empty, pull the latest. There should always be at least one memory region |
| 2927 | // if memory region handling is supported. |
| 2928 | if (m_mem_region_cache.empty ()) |
| 2929 | { |
| 2930 | error = ProcFileReader::ProcessLineByLine (GetID (), "maps", |
| 2931 | [&] (const std::string &line) -> bool |
| 2932 | { |
| 2933 | MemoryRegionInfo info; |
| 2934 | const Error parse_error = ParseMemoryRegionInfoFromProcMapsLine (line, info); |
| 2935 | if (parse_error.Success ()) |
| 2936 | { |
| 2937 | m_mem_region_cache.push_back (info); |
| 2938 | return true; |
| 2939 | } |
| 2940 | else |
| 2941 | { |
| 2942 | if (log) |
| 2943 | log->Printf ("NativeProcessLinux::%s failed to parse proc maps line '%s': %s", __FUNCTION__, line.c_str (), error.AsCString ()); |
| 2944 | return false; |
| 2945 | } |
| 2946 | }); |
| 2947 | |
| 2948 | // If we had an error, we'll mark unsupported. |
| 2949 | if (error.Fail ()) |
| 2950 | { |
| 2951 | m_supports_mem_region = LazyBool::eLazyBoolNo; |
| 2952 | return error; |
| 2953 | } |
| 2954 | else if (m_mem_region_cache.empty ()) |
| 2955 | { |
| 2956 | // No entries after attempting to read them. This shouldn't happen if /proc/{pid}/maps |
| 2957 | // is supported. Assume we don't support map entries via procfs. |
| 2958 | if (log) |
| 2959 | log->Printf ("NativeProcessLinux::%s failed to find any procfs maps entries, assuming no support for memory region metadata retrieval", __FUNCTION__); |
| 2960 | m_supports_mem_region = LazyBool::eLazyBoolNo; |
| 2961 | error.SetErrorString ("not supported"); |
| 2962 | return error; |
| 2963 | } |
| 2964 | |
| 2965 | if (log) |
| 2966 | log->Printf ("NativeProcessLinux::%s read %" PRIu64 " memory region entries from /proc/%" PRIu64 "/maps", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ()), GetID ()); |
| 2967 | |
| 2968 | // We support memory retrieval, remember that. |
| 2969 | m_supports_mem_region = LazyBool::eLazyBoolYes; |
| 2970 | } |
| 2971 | else |
| 2972 | { |
| 2973 | if (log) |
| 2974 | log->Printf ("NativeProcessLinux::%s reusing %" PRIu64 " cached memory region entries", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); |
| 2975 | } |
| 2976 | |
| 2977 | lldb::addr_t prev_base_address = 0; |
| 2978 | |
| 2979 | // FIXME start by finding the last region that is <= target address using binary search. Data is sorted. |
| 2980 | // There can be a ton of regions on pthreads apps with lots of threads. |
| 2981 | for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end (); ++it) |
| 2982 | { |
| 2983 | MemoryRegionInfo &proc_entry_info = *it; |
| 2984 | |
| 2985 | // Sanity check assumption that /proc/{pid}/maps entries are ascending. |
| 2986 | assert ((proc_entry_info.GetRange ().GetRangeBase () >= prev_base_address) && "descending /proc/pid/maps entries detected, unexpected"); |
| 2987 | prev_base_address = proc_entry_info.GetRange ().GetRangeBase (); |
| 2988 | |
| 2989 | // If the target address comes before this entry, indicate distance to next region. |
| 2990 | if (load_addr < proc_entry_info.GetRange ().GetRangeBase ()) |
| 2991 | { |
| 2992 | range_info.GetRange ().SetRangeBase (load_addr); |
| 2993 | range_info.GetRange ().SetByteSize (proc_entry_info.GetRange ().GetRangeBase () - load_addr); |
| 2994 | range_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); |
| 2995 | range_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); |
| 2996 | range_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); |
| 2997 | |
| 2998 | return error; |
| 2999 | } |
| 3000 | else if (proc_entry_info.GetRange ().Contains (load_addr)) |
| 3001 | { |
| 3002 | // The target address is within the memory region we're processing here. |
| 3003 | range_info = proc_entry_info; |
| 3004 | return error; |
| 3005 | } |
| 3006 | |
| 3007 | // The target memory address comes somewhere after the region we just parsed. |
| 3008 | } |
| 3009 | |
| 3010 | // If we made it here, we didn't find an entry that contained the given address. |
| 3011 | error.SetErrorString ("address comes after final region"); |
| 3012 | |
| 3013 | if (log) |
| 3014 | log->Printf ("NativeProcessLinux::%s failed to find map entry for address 0x%" PRIx64 ": %s", __FUNCTION__, load_addr, error.AsCString ()); |
| 3015 | |
| 3016 | return error; |
| 3017 | } |
| 3018 | |
| 3019 | void |
| 3020 | NativeProcessLinux::DoStopIDBumped (uint32_t newBumpId) |
| 3021 | { |
| 3022 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3023 | if (log) |
| 3024 | log->Printf ("NativeProcessLinux::%s(newBumpId=%" PRIu32 ") called", __FUNCTION__, newBumpId); |
| 3025 | |
| 3026 | { |
| 3027 | Mutex::Locker locker (m_mem_region_cache_mutex); |
| 3028 | if (log) |
| 3029 | log->Printf ("NativeProcessLinux::%s clearing %" PRIu64 " entries from the cache", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); |
| 3030 | m_mem_region_cache.clear (); |
| 3031 | } |
| 3032 | } |
| 3033 | |
| 3034 | Error |
| 3035 | NativeProcessLinux::AllocateMemory ( |
| 3036 | lldb::addr_t size, |
| 3037 | uint32_t permissions, |
| 3038 | lldb::addr_t &addr) |
| 3039 | { |
| 3040 | // FIXME implementing this requires the equivalent of |
| 3041 | // InferiorCallPOSIX::InferiorCallMmap, which depends on |
| 3042 | // functional ThreadPlans working with Native*Protocol. |
| 3043 | #if 1 |
| 3044 | return Error ("not implemented yet"); |
| 3045 | #else |
| 3046 | addr = LLDB_INVALID_ADDRESS; |
| 3047 | |
| 3048 | unsigned prot = 0; |
| 3049 | if (permissions & lldb::ePermissionsReadable) |
| 3050 | prot |= eMmapProtRead; |
| 3051 | if (permissions & lldb::ePermissionsWritable) |
| 3052 | prot |= eMmapProtWrite; |
| 3053 | if (permissions & lldb::ePermissionsExecutable) |
| 3054 | prot |= eMmapProtExec; |
| 3055 | |
| 3056 | // TODO implement this directly in NativeProcessLinux |
| 3057 | // (and lift to NativeProcessPOSIX if/when that class is |
| 3058 | // refactored out). |
| 3059 | if (InferiorCallMmap(this, addr, 0, size, prot, |
| 3060 | eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { |
| 3061 | m_addr_to_mmap_size[addr] = size; |
| 3062 | return Error (); |
| 3063 | } else { |
| 3064 | addr = LLDB_INVALID_ADDRESS; |
| 3065 | return Error("unable to allocate %" PRIu64 " bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions)); |
| 3066 | } |
| 3067 | #endif |
| 3068 | } |
| 3069 | |
| 3070 | Error |
| 3071 | NativeProcessLinux::DeallocateMemory (lldb::addr_t addr) |
| 3072 | { |
| 3073 | // FIXME see comments in AllocateMemory - required lower-level |
| 3074 | // bits not in place yet (ThreadPlans) |
| 3075 | return Error ("not implemented"); |
| 3076 | } |
| 3077 | |
| 3078 | lldb::addr_t |
| 3079 | NativeProcessLinux::GetSharedLibraryInfoAddress () |
| 3080 | { |
| 3081 | #if 1 |
| 3082 | // punt on this for now |
| 3083 | return LLDB_INVALID_ADDRESS; |
| 3084 | #else |
| 3085 | // Return the image info address for the exe module |
| 3086 | #if 1 |
| 3087 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3088 | |
| 3089 | ModuleSP module_sp; |
| 3090 | Error error = GetExeModuleSP (module_sp); |
| 3091 | if (error.Fail ()) |
| 3092 | { |
| 3093 | if (log) |
| 3094 | log->Warning ("NativeProcessLinux::%s failed to retrieve exe module: %s", __FUNCTION__, error.AsCString ()); |
| 3095 | return LLDB_INVALID_ADDRESS; |
| 3096 | } |
| 3097 | |
| 3098 | if (module_sp == nullptr) |
| 3099 | { |
| 3100 | if (log) |
| 3101 | log->Warning ("NativeProcessLinux::%s exe module returned was NULL", __FUNCTION__); |
| 3102 | return LLDB_INVALID_ADDRESS; |
| 3103 | } |
| 3104 | |
| 3105 | ObjectFileSP object_file_sp = module_sp->GetObjectFile (); |
| 3106 | if (object_file_sp == nullptr) |
| 3107 | { |
| 3108 | if (log) |
| 3109 | log->Warning ("NativeProcessLinux::%s exe module returned a NULL object file", __FUNCTION__); |
| 3110 | return LLDB_INVALID_ADDRESS; |
| 3111 | } |
| 3112 | |
| 3113 | return obj_file_sp->GetImageInfoAddress(); |
| 3114 | #else |
| 3115 | Target *target = &GetTarget(); |
| 3116 | ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile(); |
| 3117 | Address addr = obj_file->GetImageInfoAddress(target); |
| 3118 | |
| 3119 | if (addr.IsValid()) |
| 3120 | return addr.GetLoadAddress(target); |
| 3121 | return LLDB_INVALID_ADDRESS; |
| 3122 | #endif |
| 3123 | #endif // punt on this for now |
| 3124 | } |
| 3125 | |
| 3126 | size_t |
| 3127 | NativeProcessLinux::UpdateThreads () |
| 3128 | { |
| 3129 | // The NativeProcessLinux monitoring threads are always up to date |
| 3130 | // with respect to thread state and they keep the thread list |
| 3131 | // populated properly. All this method needs to do is return the |
| 3132 | // thread count. |
| 3133 | Mutex::Locker locker (m_threads_mutex); |
| 3134 | return m_threads.size (); |
| 3135 | } |
| 3136 | |
| 3137 | bool |
| 3138 | NativeProcessLinux::GetArchitecture (ArchSpec &arch) const |
| 3139 | { |
| 3140 | arch = m_arch; |
| 3141 | return true; |
| 3142 | } |
| 3143 | |
| 3144 | Error |
| 3145 | NativeProcessLinux::GetSoftwareBreakpointSize (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size) |
| 3146 | { |
| 3147 | // FIXME put this behind a breakpoint protocol class that can be |
| 3148 | // set per architecture. Need ARM, MIPS support here. |
Todd Fiala | 2afc596 | 2014-08-21 16:42:31 +0000 | [diff] [blame] | 3149 | static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3150 | static const uint8_t g_i386_opcode [] = { 0xCC }; |
| 3151 | |
| 3152 | switch (m_arch.GetMachine ()) |
| 3153 | { |
Todd Fiala | 2afc596 | 2014-08-21 16:42:31 +0000 | [diff] [blame] | 3154 | case llvm::Triple::aarch64: |
| 3155 | actual_opcode_size = static_cast<uint32_t> (sizeof(g_aarch64_opcode)); |
| 3156 | return Error (); |
| 3157 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3158 | case llvm::Triple::x86: |
| 3159 | case llvm::Triple::x86_64: |
| 3160 | actual_opcode_size = static_cast<uint32_t> (sizeof(g_i386_opcode)); |
| 3161 | return Error (); |
| 3162 | |
| 3163 | default: |
| 3164 | assert(false && "CPU type not supported!"); |
| 3165 | return Error ("CPU type not supported"); |
| 3166 | } |
| 3167 | } |
| 3168 | |
| 3169 | Error |
| 3170 | NativeProcessLinux::SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) |
| 3171 | { |
| 3172 | if (hardware) |
| 3173 | return Error ("NativeProcessLinux does not support hardware breakpoints"); |
| 3174 | else |
| 3175 | return SetSoftwareBreakpoint (addr, size); |
| 3176 | } |
| 3177 | |
| 3178 | Error |
| 3179 | NativeProcessLinux::GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) |
| 3180 | { |
| 3181 | // FIXME put this behind a breakpoint protocol class that can be |
| 3182 | // set per architecture. Need ARM, MIPS support here. |
Todd Fiala | 2afc596 | 2014-08-21 16:42:31 +0000 | [diff] [blame] | 3183 | static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3184 | static const uint8_t g_i386_opcode [] = { 0xCC }; |
| 3185 | |
| 3186 | switch (m_arch.GetMachine ()) |
| 3187 | { |
Todd Fiala | 2afc596 | 2014-08-21 16:42:31 +0000 | [diff] [blame] | 3188 | case llvm::Triple::aarch64: |
| 3189 | trap_opcode_bytes = g_aarch64_opcode; |
| 3190 | actual_opcode_size = sizeof(g_aarch64_opcode); |
| 3191 | return Error (); |
| 3192 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3193 | case llvm::Triple::x86: |
| 3194 | case llvm::Triple::x86_64: |
| 3195 | trap_opcode_bytes = g_i386_opcode; |
| 3196 | actual_opcode_size = sizeof(g_i386_opcode); |
| 3197 | return Error (); |
| 3198 | |
| 3199 | default: |
| 3200 | assert(false && "CPU type not supported!"); |
| 3201 | return Error ("CPU type not supported"); |
| 3202 | } |
| 3203 | } |
| 3204 | |
| 3205 | #if 0 |
| 3206 | ProcessMessage::CrashReason |
| 3207 | NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info) |
| 3208 | { |
| 3209 | ProcessMessage::CrashReason reason; |
| 3210 | assert(info->si_signo == SIGSEGV); |
| 3211 | |
| 3212 | reason = ProcessMessage::eInvalidCrashReason; |
| 3213 | |
| 3214 | switch (info->si_code) |
| 3215 | { |
| 3216 | default: |
| 3217 | assert(false && "unexpected si_code for SIGSEGV"); |
| 3218 | break; |
| 3219 | case SI_KERNEL: |
| 3220 | // Linux will occasionally send spurious SI_KERNEL codes. |
| 3221 | // (this is poorly documented in sigaction) |
| 3222 | // One way to get this is via unaligned SIMD loads. |
| 3223 | reason = ProcessMessage::eInvalidAddress; // for lack of anything better |
| 3224 | break; |
| 3225 | case SEGV_MAPERR: |
| 3226 | reason = ProcessMessage::eInvalidAddress; |
| 3227 | break; |
| 3228 | case SEGV_ACCERR: |
| 3229 | reason = ProcessMessage::ePrivilegedAddress; |
| 3230 | break; |
| 3231 | } |
| 3232 | |
| 3233 | return reason; |
| 3234 | } |
| 3235 | #endif |
| 3236 | |
| 3237 | |
| 3238 | #if 0 |
| 3239 | ProcessMessage::CrashReason |
| 3240 | NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info) |
| 3241 | { |
| 3242 | ProcessMessage::CrashReason reason; |
| 3243 | assert(info->si_signo == SIGILL); |
| 3244 | |
| 3245 | reason = ProcessMessage::eInvalidCrashReason; |
| 3246 | |
| 3247 | switch (info->si_code) |
| 3248 | { |
| 3249 | default: |
| 3250 | assert(false && "unexpected si_code for SIGILL"); |
| 3251 | break; |
| 3252 | case ILL_ILLOPC: |
| 3253 | reason = ProcessMessage::eIllegalOpcode; |
| 3254 | break; |
| 3255 | case ILL_ILLOPN: |
| 3256 | reason = ProcessMessage::eIllegalOperand; |
| 3257 | break; |
| 3258 | case ILL_ILLADR: |
| 3259 | reason = ProcessMessage::eIllegalAddressingMode; |
| 3260 | break; |
| 3261 | case ILL_ILLTRP: |
| 3262 | reason = ProcessMessage::eIllegalTrap; |
| 3263 | break; |
| 3264 | case ILL_PRVOPC: |
| 3265 | reason = ProcessMessage::ePrivilegedOpcode; |
| 3266 | break; |
| 3267 | case ILL_PRVREG: |
| 3268 | reason = ProcessMessage::ePrivilegedRegister; |
| 3269 | break; |
| 3270 | case ILL_COPROC: |
| 3271 | reason = ProcessMessage::eCoprocessorError; |
| 3272 | break; |
| 3273 | case ILL_BADSTK: |
| 3274 | reason = ProcessMessage::eInternalStackError; |
| 3275 | break; |
| 3276 | } |
| 3277 | |
| 3278 | return reason; |
| 3279 | } |
| 3280 | #endif |
| 3281 | |
| 3282 | #if 0 |
| 3283 | ProcessMessage::CrashReason |
| 3284 | NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info) |
| 3285 | { |
| 3286 | ProcessMessage::CrashReason reason; |
| 3287 | assert(info->si_signo == SIGFPE); |
| 3288 | |
| 3289 | reason = ProcessMessage::eInvalidCrashReason; |
| 3290 | |
| 3291 | switch (info->si_code) |
| 3292 | { |
| 3293 | default: |
| 3294 | assert(false && "unexpected si_code for SIGFPE"); |
| 3295 | break; |
| 3296 | case FPE_INTDIV: |
| 3297 | reason = ProcessMessage::eIntegerDivideByZero; |
| 3298 | break; |
| 3299 | case FPE_INTOVF: |
| 3300 | reason = ProcessMessage::eIntegerOverflow; |
| 3301 | break; |
| 3302 | case FPE_FLTDIV: |
| 3303 | reason = ProcessMessage::eFloatDivideByZero; |
| 3304 | break; |
| 3305 | case FPE_FLTOVF: |
| 3306 | reason = ProcessMessage::eFloatOverflow; |
| 3307 | break; |
| 3308 | case FPE_FLTUND: |
| 3309 | reason = ProcessMessage::eFloatUnderflow; |
| 3310 | break; |
| 3311 | case FPE_FLTRES: |
| 3312 | reason = ProcessMessage::eFloatInexactResult; |
| 3313 | break; |
| 3314 | case FPE_FLTINV: |
| 3315 | reason = ProcessMessage::eFloatInvalidOperation; |
| 3316 | break; |
| 3317 | case FPE_FLTSUB: |
| 3318 | reason = ProcessMessage::eFloatSubscriptRange; |
| 3319 | break; |
| 3320 | } |
| 3321 | |
| 3322 | return reason; |
| 3323 | } |
| 3324 | #endif |
| 3325 | |
| 3326 | #if 0 |
| 3327 | ProcessMessage::CrashReason |
| 3328 | NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info) |
| 3329 | { |
| 3330 | ProcessMessage::CrashReason reason; |
| 3331 | assert(info->si_signo == SIGBUS); |
| 3332 | |
| 3333 | reason = ProcessMessage::eInvalidCrashReason; |
| 3334 | |
| 3335 | switch (info->si_code) |
| 3336 | { |
| 3337 | default: |
| 3338 | assert(false && "unexpected si_code for SIGBUS"); |
| 3339 | break; |
| 3340 | case BUS_ADRALN: |
| 3341 | reason = ProcessMessage::eIllegalAlignment; |
| 3342 | break; |
| 3343 | case BUS_ADRERR: |
| 3344 | reason = ProcessMessage::eIllegalAddress; |
| 3345 | break; |
| 3346 | case BUS_OBJERR: |
| 3347 | reason = ProcessMessage::eHardwareError; |
| 3348 | break; |
| 3349 | } |
| 3350 | |
| 3351 | return reason; |
| 3352 | } |
| 3353 | #endif |
| 3354 | |
| 3355 | void |
| 3356 | NativeProcessLinux::ServeOperation(OperationArgs *args) |
| 3357 | { |
| 3358 | NativeProcessLinux *monitor = args->m_monitor; |
| 3359 | |
| 3360 | // We are finised with the arguments and are ready to go. Sync with the |
| 3361 | // parent thread and start serving operations on the inferior. |
| 3362 | sem_post(&args->m_semaphore); |
| 3363 | |
| 3364 | for(;;) |
| 3365 | { |
| 3366 | // wait for next pending operation |
| 3367 | if (sem_wait(&monitor->m_operation_pending)) |
| 3368 | { |
| 3369 | if (errno == EINTR) |
| 3370 | continue; |
| 3371 | assert(false && "Unexpected errno from sem_wait"); |
| 3372 | } |
| 3373 | |
Tamas Berghammer | 6806fde | 2015-03-02 11:04:03 +0000 | [diff] [blame] | 3374 | // nullptr as operation means the operation thread should exit. Cancel() can't be used |
| 3375 | // because it is not supported on android. |
| 3376 | if (!monitor->m_operation) |
| 3377 | { |
| 3378 | // notify calling thread that operation is complete |
| 3379 | sem_post(&monitor->m_operation_done); |
| 3380 | break; |
| 3381 | } |
| 3382 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3383 | reinterpret_cast<Operation*>(monitor->m_operation)->Execute(monitor); |
| 3384 | |
| 3385 | // notify calling thread that operation is complete |
| 3386 | sem_post(&monitor->m_operation_done); |
| 3387 | } |
| 3388 | } |
| 3389 | |
| 3390 | void |
| 3391 | NativeProcessLinux::DoOperation(void *op) |
| 3392 | { |
| 3393 | Mutex::Locker lock(m_operation_mutex); |
| 3394 | |
| 3395 | m_operation = op; |
| 3396 | |
| 3397 | // notify operation thread that an operation is ready to be processed |
| 3398 | sem_post(&m_operation_pending); |
| 3399 | |
| 3400 | // wait for operation to complete |
| 3401 | while (sem_wait(&m_operation_done)) |
| 3402 | { |
| 3403 | if (errno == EINTR) |
| 3404 | continue; |
| 3405 | assert(false && "Unexpected errno from sem_wait"); |
| 3406 | } |
| 3407 | } |
| 3408 | |
| 3409 | Error |
| 3410 | NativeProcessLinux::ReadMemory (lldb::addr_t addr, void *buf, lldb::addr_t size, lldb::addr_t &bytes_read) |
| 3411 | { |
| 3412 | ReadOperation op(addr, buf, size, bytes_read); |
| 3413 | DoOperation(&op); |
| 3414 | return op.GetError (); |
| 3415 | } |
| 3416 | |
| 3417 | Error |
| 3418 | NativeProcessLinux::WriteMemory (lldb::addr_t addr, const void *buf, lldb::addr_t size, lldb::addr_t &bytes_written) |
| 3419 | { |
| 3420 | WriteOperation op(addr, buf, size, bytes_written); |
| 3421 | DoOperation(&op); |
| 3422 | return op.GetError (); |
| 3423 | } |
| 3424 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3425 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3426 | NativeProcessLinux::ReadRegisterValue(lldb::tid_t tid, uint32_t offset, const char* reg_name, |
| 3427 | uint32_t size, RegisterValue &value) |
| 3428 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3429 | ReadRegOperation op(tid, offset, reg_name, value); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3430 | DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3431 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3432 | } |
| 3433 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3434 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3435 | NativeProcessLinux::WriteRegisterValue(lldb::tid_t tid, unsigned offset, |
| 3436 | const char* reg_name, const RegisterValue &value) |
| 3437 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3438 | WriteRegOperation op(tid, offset, reg_name, value); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3439 | DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3440 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3443 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3444 | NativeProcessLinux::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size) |
| 3445 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3446 | ReadGPROperation op(tid, buf, buf_size); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3447 | DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3448 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3449 | } |
| 3450 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3451 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3452 | NativeProcessLinux::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size) |
| 3453 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3454 | ReadFPROperation op(tid, buf, buf_size); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3455 | DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3456 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3457 | } |
| 3458 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3459 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3460 | NativeProcessLinux::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) |
| 3461 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3462 | ReadRegisterSetOperation op(tid, buf, buf_size, regset); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3463 | DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3464 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3465 | } |
| 3466 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3467 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3468 | NativeProcessLinux::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size) |
| 3469 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3470 | WriteGPROperation op(tid, buf, buf_size); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3471 | DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3472 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3473 | } |
| 3474 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3475 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3476 | NativeProcessLinux::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size) |
| 3477 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3478 | WriteFPROperation op(tid, buf, buf_size); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3479 | DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3480 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3481 | } |
| 3482 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3483 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3484 | NativeProcessLinux::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) |
| 3485 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3486 | WriteRegisterSetOperation op(tid, buf, buf_size, regset); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3487 | DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3488 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3489 | } |
| 3490 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3491 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3492 | NativeProcessLinux::Resume (lldb::tid_t tid, uint32_t signo) |
| 3493 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3494 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3495 | |
| 3496 | if (log) |
| 3497 | log->Printf ("NativeProcessLinux::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid, |
| 3498 | GetUnixSignals().GetSignalAsCString (signo)); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3499 | ResumeOperation op (tid, signo); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3500 | DoOperation (&op); |
| 3501 | if (log) |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3502 | log->Printf ("NativeProcessLinux::%s() resuming thread = %" PRIu64 " result = %s", __FUNCTION__, tid, op.GetError().Success() ? "true" : "false"); |
| 3503 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3504 | } |
| 3505 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3506 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3507 | NativeProcessLinux::SingleStep(lldb::tid_t tid, uint32_t signo) |
| 3508 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3509 | SingleStepOperation op(tid, signo); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3510 | DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3511 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3512 | } |
| 3513 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3514 | Error |
| 3515 | NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3516 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3517 | SiginfoOperation op(tid, siginfo); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3518 | DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3519 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3520 | } |
| 3521 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3522 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3523 | NativeProcessLinux::GetEventMessage(lldb::tid_t tid, unsigned long *message) |
| 3524 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3525 | EventMessageOperation op(tid, message); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3526 | DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3527 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3528 | } |
| 3529 | |
| 3530 | lldb_private::Error |
| 3531 | NativeProcessLinux::Detach(lldb::tid_t tid) |
| 3532 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3533 | if (tid == LLDB_INVALID_THREAD_ID) |
| 3534 | return Error(); |
| 3535 | |
| 3536 | DetachOperation op(tid); |
| 3537 | DoOperation(&op); |
| 3538 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
| 3541 | bool |
| 3542 | NativeProcessLinux::DupDescriptor(const char *path, int fd, int flags) |
| 3543 | { |
| 3544 | int target_fd = open(path, flags, 0666); |
| 3545 | |
| 3546 | if (target_fd == -1) |
| 3547 | return false; |
| 3548 | |
Pavel Labath | 493c3a1 | 2015-02-04 10:36:57 +0000 | [diff] [blame] | 3549 | if (dup2(target_fd, fd) == -1) |
| 3550 | return false; |
| 3551 | |
| 3552 | return (close(target_fd) == -1) ? false : true; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3553 | } |
| 3554 | |
| 3555 | void |
| 3556 | NativeProcessLinux::StopMonitoringChildProcess() |
| 3557 | { |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 3558 | if (m_monitor_thread.IsJoinable()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3559 | { |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 3560 | m_monitor_thread.Cancel(); |
| 3561 | m_monitor_thread.Join(nullptr); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3562 | } |
| 3563 | } |
| 3564 | |
| 3565 | void |
| 3566 | NativeProcessLinux::StopMonitor() |
| 3567 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3568 | StopOpThread(); |
Tamas Berghammer | 6806fde | 2015-03-02 11:04:03 +0000 | [diff] [blame] | 3569 | StopMonitoringChildProcess(); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3570 | StopCoordinatorThread (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3571 | sem_destroy(&m_operation_pending); |
| 3572 | sem_destroy(&m_operation_done); |
| 3573 | |
| 3574 | // TODO: validate whether this still holds, fix up comment. |
| 3575 | // Note: ProcessPOSIX passes the m_terminal_fd file descriptor to |
| 3576 | // Process::SetSTDIOFileDescriptor, which in turn transfers ownership of |
| 3577 | // the descriptor to a ConnectionFileDescriptor object. Consequently |
| 3578 | // even though still has the file descriptor, we shouldn't close it here. |
| 3579 | } |
| 3580 | |
| 3581 | void |
| 3582 | NativeProcessLinux::StopOpThread() |
| 3583 | { |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 3584 | if (!m_operation_thread.IsJoinable()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3585 | return; |
| 3586 | |
Tamas Berghammer | 6806fde | 2015-03-02 11:04:03 +0000 | [diff] [blame] | 3587 | DoOperation(nullptr); // nullptr as operation ask the operation thread to exit |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 3588 | m_operation_thread.Join(nullptr); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3589 | } |
| 3590 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3591 | Error |
| 3592 | NativeProcessLinux::StartCoordinatorThread () |
| 3593 | { |
| 3594 | Error error; |
| 3595 | static const char *g_thread_name = "lldb.process.linux.ts_coordinator"; |
| 3596 | Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 3597 | |
| 3598 | // Skip if thread is already running |
| 3599 | if (m_coordinator_thread.IsJoinable()) |
| 3600 | { |
| 3601 | error.SetErrorString ("ThreadStateCoordinator's run loop is already running"); |
| 3602 | if (log) |
| 3603 | log->Printf ("NativeProcessLinux::%s %s", __FUNCTION__, error.AsCString ()); |
| 3604 | return error; |
| 3605 | } |
| 3606 | |
| 3607 | // Enable verbose logging if lldb thread logging is enabled. |
| 3608 | m_coordinator_up->LogEnableEventProcessing (log != nullptr); |
| 3609 | |
| 3610 | if (log) |
| 3611 | log->Printf ("NativeProcessLinux::%s launching ThreadStateCoordinator thread for pid %" PRIu64, __FUNCTION__, GetID ()); |
| 3612 | m_coordinator_thread = ThreadLauncher::LaunchThread(g_thread_name, CoordinatorThread, this, &error); |
| 3613 | return error; |
| 3614 | } |
| 3615 | |
| 3616 | void * |
| 3617 | NativeProcessLinux::CoordinatorThread (void *arg) |
| 3618 | { |
| 3619 | Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 3620 | |
| 3621 | NativeProcessLinux *const process = static_cast<NativeProcessLinux*> (arg); |
| 3622 | assert (process && "null process passed to CoordinatorThread"); |
| 3623 | if (!process) |
| 3624 | { |
| 3625 | if (log) |
| 3626 | log->Printf ("NativeProcessLinux::%s null process, exiting ThreadStateCoordinator processing loop", __FUNCTION__); |
| 3627 | return nullptr; |
| 3628 | } |
| 3629 | |
| 3630 | // Run the thread state coordinator loop until it is done. This call uses |
| 3631 | // efficient waiting for an event to be ready. |
| 3632 | while (process->m_coordinator_up->ProcessNextEvent () == ThreadStateCoordinator::eventLoopResultContinue) |
| 3633 | { |
| 3634 | } |
| 3635 | |
| 3636 | if (log) |
| 3637 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " exiting ThreadStateCoordinator processing loop due to coordinator indicating completion", __FUNCTION__, process->GetID ()); |
| 3638 | |
| 3639 | return nullptr; |
| 3640 | } |
| 3641 | |
| 3642 | void |
| 3643 | NativeProcessLinux::StopCoordinatorThread() |
| 3644 | { |
| 3645 | Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 3646 | if (log) |
| 3647 | log->Printf ("NativeProcessLinux::%s requesting ThreadStateCoordinator stop for pid %" PRIu64, __FUNCTION__, GetID ()); |
| 3648 | |
| 3649 | // Tell the coordinator we're done. This will cause the coordinator |
| 3650 | // run loop thread to exit when the processing queue hits this message. |
| 3651 | m_coordinator_up->StopCoordinator (); |
Oleksiy Vyalov | 8bc34f4 | 2015-02-19 17:58:04 +0000 | [diff] [blame] | 3652 | m_coordinator_thread.Join (nullptr); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3653 | } |
| 3654 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3655 | bool |
| 3656 | NativeProcessLinux::HasThreadNoLock (lldb::tid_t thread_id) |
| 3657 | { |
| 3658 | for (auto thread_sp : m_threads) |
| 3659 | { |
| 3660 | assert (thread_sp && "thread list should not contain NULL threads"); |
| 3661 | if (thread_sp->GetID () == thread_id) |
| 3662 | { |
| 3663 | // We have this thread. |
| 3664 | return true; |
| 3665 | } |
| 3666 | } |
| 3667 | |
| 3668 | // We don't have this thread. |
| 3669 | return false; |
| 3670 | } |
| 3671 | |
| 3672 | NativeThreadProtocolSP |
| 3673 | NativeProcessLinux::MaybeGetThreadNoLock (lldb::tid_t thread_id) |
| 3674 | { |
| 3675 | // CONSIDER organize threads by map - we can do better than linear. |
| 3676 | for (auto thread_sp : m_threads) |
| 3677 | { |
| 3678 | if (thread_sp->GetID () == thread_id) |
| 3679 | return thread_sp; |
| 3680 | } |
| 3681 | |
| 3682 | // We don't have this thread. |
| 3683 | return NativeThreadProtocolSP (); |
| 3684 | } |
| 3685 | |
| 3686 | bool |
| 3687 | NativeProcessLinux::StopTrackingThread (lldb::tid_t thread_id) |
| 3688 | { |
| 3689 | Mutex::Locker locker (m_threads_mutex); |
| 3690 | for (auto it = m_threads.begin (); it != m_threads.end (); ++it) |
| 3691 | { |
| 3692 | if (*it && ((*it)->GetID () == thread_id)) |
| 3693 | { |
| 3694 | m_threads.erase (it); |
| 3695 | return true; |
| 3696 | } |
| 3697 | } |
| 3698 | |
| 3699 | // Didn't find it. |
| 3700 | return false; |
| 3701 | } |
| 3702 | |
| 3703 | NativeThreadProtocolSP |
| 3704 | NativeProcessLinux::AddThread (lldb::tid_t thread_id) |
| 3705 | { |
| 3706 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 3707 | |
| 3708 | Mutex::Locker locker (m_threads_mutex); |
| 3709 | |
| 3710 | if (log) |
| 3711 | { |
| 3712 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " adding thread with tid %" PRIu64, |
| 3713 | __FUNCTION__, |
| 3714 | GetID (), |
| 3715 | thread_id); |
| 3716 | } |
| 3717 | |
| 3718 | assert (!HasThreadNoLock (thread_id) && "attempted to add a thread by id that already exists"); |
| 3719 | |
| 3720 | // If this is the first thread, save it as the current thread |
| 3721 | if (m_threads.empty ()) |
| 3722 | SetCurrentThreadID (thread_id); |
| 3723 | |
| 3724 | NativeThreadProtocolSP thread_sp (new NativeThreadLinux (this, thread_id)); |
| 3725 | m_threads.push_back (thread_sp); |
| 3726 | |
| 3727 | return thread_sp; |
| 3728 | } |
| 3729 | |
| 3730 | NativeThreadProtocolSP |
| 3731 | NativeProcessLinux::GetOrCreateThread (lldb::tid_t thread_id, bool &created) |
| 3732 | { |
| 3733 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 3734 | |
| 3735 | Mutex::Locker locker (m_threads_mutex); |
| 3736 | if (log) |
| 3737 | { |
| 3738 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " get/create thread with tid %" PRIu64, |
| 3739 | __FUNCTION__, |
| 3740 | GetID (), |
| 3741 | thread_id); |
| 3742 | } |
| 3743 | |
| 3744 | // Retrieve the thread if it is already getting tracked. |
| 3745 | NativeThreadProtocolSP thread_sp = MaybeGetThreadNoLock (thread_id); |
| 3746 | if (thread_sp) |
| 3747 | { |
| 3748 | if (log) |
| 3749 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": thread already tracked, returning", |
| 3750 | __FUNCTION__, |
| 3751 | GetID (), |
| 3752 | thread_id); |
| 3753 | created = false; |
| 3754 | return thread_sp; |
| 3755 | |
| 3756 | } |
| 3757 | |
| 3758 | // Create the thread metadata since it isn't being tracked. |
| 3759 | if (log) |
| 3760 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": thread didn't exist, tracking now", |
| 3761 | __FUNCTION__, |
| 3762 | GetID (), |
| 3763 | thread_id); |
| 3764 | |
| 3765 | thread_sp.reset (new NativeThreadLinux (this, thread_id)); |
| 3766 | m_threads.push_back (thread_sp); |
| 3767 | created = true; |
| 3768 | |
| 3769 | return thread_sp; |
| 3770 | } |
| 3771 | |
| 3772 | Error |
| 3773 | NativeProcessLinux::FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp) |
| 3774 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 3775 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3776 | |
| 3777 | Error error; |
| 3778 | |
| 3779 | // Get a linux thread pointer. |
| 3780 | if (!thread_sp) |
| 3781 | { |
| 3782 | error.SetErrorString ("null thread_sp"); |
| 3783 | if (log) |
| 3784 | log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); |
| 3785 | return error; |
| 3786 | } |
| 3787 | NativeThreadLinux *const linux_thread_p = reinterpret_cast<NativeThreadLinux*> (thread_sp.get()); |
| 3788 | |
| 3789 | // Find out the size of a breakpoint (might depend on where we are in the code). |
| 3790 | NativeRegisterContextSP context_sp = linux_thread_p->GetRegisterContext (); |
| 3791 | if (!context_sp) |
| 3792 | { |
| 3793 | error.SetErrorString ("cannot get a NativeRegisterContext for the thread"); |
| 3794 | if (log) |
| 3795 | log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); |
| 3796 | return error; |
| 3797 | } |
| 3798 | |
| 3799 | uint32_t breakpoint_size = 0; |
| 3800 | error = GetSoftwareBreakpointSize (context_sp, breakpoint_size); |
| 3801 | if (error.Fail ()) |
| 3802 | { |
| 3803 | if (log) |
| 3804 | log->Printf ("NativeProcessLinux::%s GetBreakpointSize() failed: %s", __FUNCTION__, error.AsCString ()); |
| 3805 | return error; |
| 3806 | } |
| 3807 | else |
| 3808 | { |
| 3809 | if (log) |
| 3810 | log->Printf ("NativeProcessLinux::%s breakpoint size: %" PRIu32, __FUNCTION__, breakpoint_size); |
| 3811 | } |
| 3812 | |
| 3813 | // First try probing for a breakpoint at a software breakpoint location: PC - breakpoint size. |
| 3814 | const lldb::addr_t initial_pc_addr = context_sp->GetPC (); |
| 3815 | lldb::addr_t breakpoint_addr = initial_pc_addr; |
| 3816 | if (breakpoint_size > static_cast<lldb::addr_t> (0)) |
| 3817 | { |
| 3818 | // Do not allow breakpoint probe to wrap around. |
| 3819 | if (breakpoint_addr >= static_cast<lldb::addr_t> (breakpoint_size)) |
| 3820 | breakpoint_addr -= static_cast<lldb::addr_t> (breakpoint_size); |
| 3821 | } |
| 3822 | |
| 3823 | // Check if we stopped because of a breakpoint. |
| 3824 | NativeBreakpointSP breakpoint_sp; |
| 3825 | error = m_breakpoint_list.GetBreakpoint (breakpoint_addr, breakpoint_sp); |
| 3826 | if (!error.Success () || !breakpoint_sp) |
| 3827 | { |
| 3828 | // We didn't find one at a software probe location. Nothing to do. |
| 3829 | if (log) |
| 3830 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " no lldb breakpoint found at current pc with adjustment: 0x%" PRIx64, __FUNCTION__, GetID (), breakpoint_addr); |
| 3831 | return Error (); |
| 3832 | } |
| 3833 | |
| 3834 | // If the breakpoint is not a software breakpoint, nothing to do. |
| 3835 | if (!breakpoint_sp->IsSoftwareBreakpoint ()) |
| 3836 | { |
| 3837 | if (log) |
| 3838 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " breakpoint found at 0x%" PRIx64 ", not software, nothing to adjust", __FUNCTION__, GetID (), breakpoint_addr); |
| 3839 | return Error (); |
| 3840 | } |
| 3841 | |
| 3842 | // |
| 3843 | // We have a software breakpoint and need to adjust the PC. |
| 3844 | // |
| 3845 | |
| 3846 | // Sanity check. |
| 3847 | if (breakpoint_size == 0) |
| 3848 | { |
| 3849 | // Nothing to do! How did we get here? |
| 3850 | if (log) |
| 3851 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " breakpoint found at 0x%" PRIx64 ", it is software, but the size is zero, nothing to do (unexpected)", __FUNCTION__, GetID (), breakpoint_addr); |
| 3852 | return Error (); |
| 3853 | } |
| 3854 | |
| 3855 | // Change the program counter. |
| 3856 | if (log) |
| 3857 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": changing PC from 0x%" PRIx64 " to 0x%" PRIx64, __FUNCTION__, GetID (), linux_thread_p->GetID (), initial_pc_addr, breakpoint_addr); |
| 3858 | |
| 3859 | error = context_sp->SetPC (breakpoint_addr); |
| 3860 | if (error.Fail ()) |
| 3861 | { |
| 3862 | if (log) |
| 3863 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": failed to set PC: %s", __FUNCTION__, GetID (), linux_thread_p->GetID (), error.AsCString ()); |
| 3864 | return error; |
| 3865 | } |
| 3866 | |
| 3867 | return error; |
| 3868 | } |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3869 | |
| 3870 | void |
| 3871 | NativeProcessLinux::NotifyThreadCreateStopped (lldb::tid_t tid) |
| 3872 | { |
| 3873 | const bool is_stopped = true; |
| 3874 | m_coordinator_up->NotifyThreadCreate (tid, is_stopped, CoordinatorErrorHandler); |
| 3875 | } |
| 3876 | |
| 3877 | void |
| 3878 | NativeProcessLinux::NotifyThreadDeath (lldb::tid_t tid) |
| 3879 | { |
| 3880 | m_coordinator_up->NotifyThreadDeath (tid, CoordinatorErrorHandler); |
| 3881 | } |
| 3882 | |
| 3883 | void |
| 3884 | NativeProcessLinux::NotifyThreadStop (lldb::tid_t tid) |
| 3885 | { |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 3886 | m_coordinator_up->NotifyThreadStop (tid, false, CoordinatorErrorHandler); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3887 | } |
| 3888 | |
| 3889 | void |
| 3890 | NativeProcessLinux::CallAfterRunningThreadsStop (lldb::tid_t tid, |
| 3891 | const std::function<void (lldb::tid_t tid)> &call_after_function) |
| 3892 | { |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 3893 | Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 3894 | if (log) |
| 3895 | log->Printf("NativeProcessLinux::%s tid %" PRIu64, __FUNCTION__, tid); |
| 3896 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3897 | const lldb::pid_t pid = GetID (); |
Chaoren Lin | 938fcf6 | 2015-02-03 01:50:44 +0000 | [diff] [blame] | 3898 | m_coordinator_up->CallAfterRunningThreadsStop (tid, |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3899 | [=](lldb::tid_t request_stop_tid) |
| 3900 | { |
Chaoren Lin | 37c768c | 2015-02-03 01:51:30 +0000 | [diff] [blame] | 3901 | return RequestThreadStop(pid, request_stop_tid); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3902 | }, |
| 3903 | call_after_function, |
| 3904 | CoordinatorErrorHandler); |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 3905 | } |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3906 | |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 3907 | void |
| 3908 | NativeProcessLinux::CallAfterRunningThreadsStopWithSkipTID (lldb::tid_t deferred_signal_tid, |
| 3909 | lldb::tid_t skip_stop_request_tid, |
| 3910 | const std::function<void (lldb::tid_t tid)> &call_after_function) |
| 3911 | { |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 3912 | Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 3913 | if (log) |
| 3914 | log->Printf("NativeProcessLinux::%s deferred_signal_tid %" PRIu64 ", skip_stop_request_tid %" PRIu64, __FUNCTION__, deferred_signal_tid, skip_stop_request_tid); |
| 3915 | |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 3916 | const lldb::pid_t pid = GetID (); |
| 3917 | m_coordinator_up->CallAfterRunningThreadsStopWithSkipTIDs (deferred_signal_tid, |
| 3918 | skip_stop_request_tid != LLDB_INVALID_THREAD_ID ? ThreadStateCoordinator::ThreadIDSet {skip_stop_request_tid} : ThreadStateCoordinator::ThreadIDSet (), |
| 3919 | [=](lldb::tid_t request_stop_tid) |
| 3920 | { |
Chaoren Lin | 37c768c | 2015-02-03 01:51:30 +0000 | [diff] [blame] | 3921 | return RequestThreadStop(pid, request_stop_tid); |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 3922 | }, |
| 3923 | call_after_function, |
| 3924 | CoordinatorErrorHandler); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3925 | } |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 3926 | |
| 3927 | lldb_private::Error |
| 3928 | NativeProcessLinux::RequestThreadStop (const lldb::pid_t pid, const lldb::tid_t tid) |
| 3929 | { |
| 3930 | Log* log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 3931 | if (log) |
| 3932 | log->Printf ("NativeProcessLinux::%s requesting thread stop(pid: %" PRIu64 ", tid: %" PRIu64 ")", __FUNCTION__, pid, tid); |
| 3933 | |
| 3934 | Error err; |
| 3935 | errno = 0; |
| 3936 | if (::tgkill (pid, tid, SIGSTOP) != 0) |
| 3937 | { |
| 3938 | err.SetErrorToErrno (); |
| 3939 | if (log) |
| 3940 | log->Printf ("NativeProcessLinux::%s tgkill(%" PRIu64 ", %" PRIu64 ", SIGSTOP) failed: %s", __FUNCTION__, pid, tid, err.AsCString ()); |
| 3941 | } |
| 3942 | |
| 3943 | return err; |
| 3944 | } |