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