Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1 | //===-- NativeProcessLinux.cpp -------------------------------- -*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/lldb-python.h" |
| 11 | |
| 12 | #include "NativeProcessLinux.h" |
| 13 | |
| 14 | // C Includes |
| 15 | #include <errno.h> |
| 16 | #include <poll.h> |
| 17 | #include <string.h> |
| 18 | #include <stdint.h> |
| 19 | #include <unistd.h> |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 20 | |
| 21 | // C++ Includes |
| 22 | #include <fstream> |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 23 | #include <sstream> |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 24 | #include <string> |
| 25 | |
| 26 | // Other libraries and framework includes |
| 27 | #include "lldb/Core/Debugger.h" |
Tamas Berghammer | d8c338d | 2015-04-15 09:47:02 +0000 | [diff] [blame] | 28 | #include "lldb/Core/EmulateInstruction.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 29 | #include "lldb/Core/Error.h" |
| 30 | #include "lldb/Core/Module.h" |
Oleksiy Vyalov | 6edef20 | 2014-11-17 22:16:42 +0000 | [diff] [blame] | 31 | #include "lldb/Core/ModuleSpec.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 32 | #include "lldb/Core/RegisterValue.h" |
| 33 | #include "lldb/Core/Scalar.h" |
| 34 | #include "lldb/Core/State.h" |
Tamas Berghammer | 1e209fc | 2015-03-13 11:36:47 +0000 | [diff] [blame] | 35 | #include "lldb/Host/common/NativeBreakpoint.h" |
Tamas Berghammer | 0cbf0b1 | 2015-03-13 11:16:03 +0000 | [diff] [blame] | 36 | #include "lldb/Host/common/NativeRegisterContext.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 37 | #include "lldb/Host/Host.h" |
Zachary Turner | 13b1826 | 2014-08-20 16:42:51 +0000 | [diff] [blame] | 38 | #include "lldb/Host/HostInfo.h" |
Tamas Berghammer | 0cbf0b1 | 2015-03-13 11:16:03 +0000 | [diff] [blame] | 39 | #include "lldb/Host/HostNativeThread.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 40 | #include "lldb/Host/ThreadLauncher.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 41 | #include "lldb/Symbol/ObjectFile.h" |
Zachary Turner | 90aff47 | 2015-03-03 23:36:51 +0000 | [diff] [blame] | 42 | #include "lldb/Target/Process.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 43 | #include "lldb/Target/ProcessLaunchInfo.h" |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 44 | #include "lldb/Utility/LLDBAssert.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 45 | #include "lldb/Utility/PseudoTerminal.h" |
| 46 | |
Tamas Berghammer | 1e209fc | 2015-03-13 11:36:47 +0000 | [diff] [blame] | 47 | #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 48 | #include "Plugins/Process/Utility/LinuxSignals.h" |
Tamas Berghammer | 1e209fc | 2015-03-13 11:36:47 +0000 | [diff] [blame] | 49 | #include "Utility/StringExtractor.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 50 | #include "NativeThreadLinux.h" |
| 51 | #include "ProcFileReader.h" |
Tamas Berghammer | 1e209fc | 2015-03-13 11:36:47 +0000 | [diff] [blame] | 52 | #include "Procfs.h" |
Todd Fiala | cacde7d | 2014-09-27 16:54:22 +0000 | [diff] [blame] | 53 | |
Tamas Berghammer | d858487 | 2015-02-06 10:57:40 +0000 | [diff] [blame] | 54 | // System includes - They have to be included after framework includes because they define some |
| 55 | // macros which collide with variable names in other modules |
| 56 | #include <linux/unistd.h> |
Tamas Berghammer | d858487 | 2015-02-06 10:57:40 +0000 | [diff] [blame] | 57 | #include <sys/personality.h> |
| 58 | #include <sys/ptrace.h> |
| 59 | #include <sys/socket.h> |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 60 | #include <sys/signalfd.h> |
Tamas Berghammer | d858487 | 2015-02-06 10:57:40 +0000 | [diff] [blame] | 61 | #include <sys/syscall.h> |
| 62 | #include <sys/types.h> |
| 63 | #include <sys/uio.h> |
| 64 | #include <sys/user.h> |
| 65 | #include <sys/wait.h> |
| 66 | |
Tamas Berghammer | 1e209fc | 2015-03-13 11:36:47 +0000 | [diff] [blame] | 67 | #if defined (__arm64__) || defined (__aarch64__) |
| 68 | // NT_PRSTATUS and NT_FPREGSET definition |
| 69 | #include <elf.h> |
| 70 | #endif |
| 71 | |
Todd Fiala | cacde7d | 2014-09-27 16:54:22 +0000 | [diff] [blame] | 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 |
Chaoren Lin | c934659 | 2015-02-28 00:20:16 +0000 | [diff] [blame] | 128 | #define tgkill(pid, tid, sig) \ |
| 129 | syscall(SYS_tgkill, static_cast<::pid_t>(pid), static_cast<::pid_t>(tid), sig) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 130 | |
| 131 | // We disable the tracing of ptrace calls for integration builds to |
| 132 | // avoid the additional indirection and checks. |
| 133 | #ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 134 | #define PTRACE(req, pid, addr, data, data_size, error) \ |
| 135 | PtraceWrapper((req), (pid), (addr), (data), (data_size), (error), #req, __FILE__, __LINE__) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 136 | #else |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 137 | #define PTRACE(req, pid, addr, data, data_size, error) \ |
| 138 | PtraceWrapper((req), (pid), (addr), (data), (data_size), (error)) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 139 | #endif |
| 140 | |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 141 | using namespace lldb; |
| 142 | using namespace lldb_private; |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 143 | using namespace lldb_private::process_linux; |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 144 | using namespace llvm; |
| 145 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 146 | // Private bits we only need internally. |
| 147 | namespace |
| 148 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 149 | const UnixSignals& |
| 150 | GetUnixSignals () |
| 151 | { |
| 152 | static process_linux::LinuxSignals signals; |
| 153 | return signals; |
| 154 | } |
| 155 | |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 156 | NativeProcessLinux::LogFunction |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 157 | GetThreadLoggerFunction () |
| 158 | { |
| 159 | return [](const char *format, va_list args) |
| 160 | { |
| 161 | Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); |
| 162 | if (log) |
| 163 | log->VAPrintf (format, args); |
| 164 | }; |
| 165 | } |
| 166 | |
| 167 | void |
| 168 | CoordinatorErrorHandler (const std::string &error_message) |
| 169 | { |
| 170 | Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); |
| 171 | if (log) |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 172 | log->Printf ("NativeProcessLinux::%s %s", __FUNCTION__, error_message.c_str ()); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 173 | assert (false && "NativeProcessLinux error reported"); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 176 | Error |
| 177 | ResolveProcessArchitecture (lldb::pid_t pid, Platform &platform, ArchSpec &arch) |
| 178 | { |
| 179 | // Grab process info for the running process. |
| 180 | ProcessInstanceInfo process_info; |
| 181 | if (!platform.GetProcessInfo (pid, process_info)) |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 182 | return Error("failed to get process info"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 183 | |
| 184 | // Resolve the executable module. |
| 185 | ModuleSP exe_module_sp; |
Chaoren Lin | e56f6dc | 2015-03-01 04:31:16 +0000 | [diff] [blame] | 186 | ModuleSpec exe_module_spec(process_info.GetExecutableFile(), process_info.GetArchitecture()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 187 | FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths ()); |
| 188 | Error error = platform.ResolveExecutable( |
Oleksiy Vyalov | 5453933 | 2014-11-17 22:42:28 +0000 | [diff] [blame] | 189 | exe_module_spec, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 190 | exe_module_sp, |
| 191 | executable_search_paths.GetSize () ? &executable_search_paths : NULL); |
| 192 | |
| 193 | if (!error.Success ()) |
| 194 | return error; |
| 195 | |
| 196 | // Check if we've got our architecture from the exe_module. |
| 197 | arch = exe_module_sp->GetArchitecture (); |
| 198 | if (arch.IsValid ()) |
| 199 | return Error(); |
| 200 | else |
| 201 | return Error("failed to retrieve a valid architecture from the exe module"); |
| 202 | } |
| 203 | |
| 204 | void |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 205 | DisplayBytes (StreamString &s, void *bytes, uint32_t count) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 206 | { |
| 207 | uint8_t *ptr = (uint8_t *)bytes; |
| 208 | const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count); |
| 209 | for(uint32_t i=0; i<loop_count; i++) |
| 210 | { |
| 211 | s.Printf ("[%x]", *ptr); |
| 212 | ptr++; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void |
| 217 | PtraceDisplayBytes(int &req, void *data, size_t data_size) |
| 218 | { |
| 219 | StreamString buf; |
| 220 | Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet ( |
| 221 | POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE)); |
| 222 | |
| 223 | if (verbose_log) |
| 224 | { |
| 225 | switch(req) |
| 226 | { |
| 227 | case PTRACE_POKETEXT: |
| 228 | { |
| 229 | DisplayBytes(buf, &data, 8); |
| 230 | verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData()); |
| 231 | break; |
| 232 | } |
| 233 | case PTRACE_POKEDATA: |
| 234 | { |
| 235 | DisplayBytes(buf, &data, 8); |
| 236 | verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData()); |
| 237 | break; |
| 238 | } |
| 239 | case PTRACE_POKEUSER: |
| 240 | { |
| 241 | DisplayBytes(buf, &data, 8); |
| 242 | verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData()); |
| 243 | break; |
| 244 | } |
| 245 | case PTRACE_SETREGS: |
| 246 | { |
| 247 | DisplayBytes(buf, data, data_size); |
| 248 | verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData()); |
| 249 | break; |
| 250 | } |
| 251 | case PTRACE_SETFPREGS: |
| 252 | { |
| 253 | DisplayBytes(buf, data, data_size); |
| 254 | verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData()); |
| 255 | break; |
| 256 | } |
| 257 | case PTRACE_SETSIGINFO: |
| 258 | { |
| 259 | DisplayBytes(buf, data, sizeof(siginfo_t)); |
| 260 | verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData()); |
| 261 | break; |
| 262 | } |
| 263 | case PTRACE_SETREGSET: |
| 264 | { |
| 265 | // Extract iov_base from data, which is a pointer to the struct IOVEC |
| 266 | DisplayBytes(buf, *(void **)data, data_size); |
| 267 | verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData()); |
| 268 | break; |
| 269 | } |
| 270 | default: |
| 271 | { |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | // Wrapper for ptrace to catch errors and log calls. |
| 278 | // Note that ptrace sets errno on error because -1 can be a valid result (i.e. for PTRACE_PEEK*) |
| 279 | long |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 280 | PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, Error& error, |
| 281 | const char* reqName, const char* file, int line) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 282 | { |
| 283 | long int result; |
| 284 | |
| 285 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE)); |
| 286 | |
| 287 | PtraceDisplayBytes(req, data, data_size); |
| 288 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 289 | error.Clear(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 290 | errno = 0; |
| 291 | if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) |
Todd Fiala | 202ecd2 | 2014-07-10 04:39:13 +0000 | [diff] [blame] | 292 | 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] | 293 | else |
Todd Fiala | 202ecd2 | 2014-07-10 04:39:13 +0000 | [diff] [blame] | 294 | 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] | 295 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 296 | if (result == -1) |
| 297 | error.SetErrorToErrno(); |
| 298 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 299 | if (log) |
| 300 | log->Printf("ptrace(%s, %" PRIu64 ", %p, %p, %zu)=%lX called from file %s line %d", |
| 301 | reqName, pid, addr, data, data_size, result, file, line); |
| 302 | |
| 303 | PtraceDisplayBytes(req, data, data_size); |
| 304 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 305 | if (log && error.GetError() != 0) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 306 | { |
| 307 | const char* str; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 308 | switch (error.GetError()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 309 | { |
| 310 | case ESRCH: str = "ESRCH"; break; |
| 311 | case EINVAL: str = "EINVAL"; break; |
| 312 | case EBUSY: str = "EBUSY"; break; |
| 313 | case EPERM: str = "EPERM"; break; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 314 | default: str = error.AsCString(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 315 | } |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 316 | log->Printf("ptrace() failed; errno=%d (%s)", error.GetError(), str); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | return result; |
| 320 | } |
| 321 | |
| 322 | #ifdef LLDB_CONFIGURATION_BUILDANDINTEGRATION |
| 323 | // Wrapper for ptrace when logging is not required. |
| 324 | // Sets errno to 0 prior to calling ptrace. |
| 325 | long |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 326 | PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, Error& error) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 327 | { |
| 328 | long result = 0; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 329 | |
| 330 | error.Clear(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 331 | errno = 0; |
| 332 | if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) |
Todd Fiala | 202ecd2 | 2014-07-10 04:39:13 +0000 | [diff] [blame] | 333 | 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] | 334 | else |
Todd Fiala | 202ecd2 | 2014-07-10 04:39:13 +0000 | [diff] [blame] | 335 | result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), addr, data); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 336 | |
| 337 | if (result == -1) |
| 338 | error.SetErrorToErrno(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 339 | return result; |
| 340 | } |
| 341 | #endif |
| 342 | |
| 343 | //------------------------------------------------------------------------------ |
| 344 | // Static implementations of NativeProcessLinux::ReadMemory and |
| 345 | // NativeProcessLinux::WriteMemory. This enables mutual recursion between these |
| 346 | // functions without needed to go thru the thread funnel. |
| 347 | |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 348 | size_t |
| 349 | DoReadMemory( |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 350 | lldb::pid_t pid, |
| 351 | lldb::addr_t vm_addr, |
| 352 | void *buf, |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 353 | size_t size, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 354 | Error &error) |
| 355 | { |
| 356 | // ptrace word size is determined by the host, not the child |
| 357 | static const unsigned word_size = sizeof(void*); |
| 358 | unsigned char *dst = static_cast<unsigned char*>(buf); |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 359 | size_t bytes_read; |
| 360 | size_t remainder; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 361 | long data; |
| 362 | |
| 363 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); |
| 364 | if (log) |
| 365 | ProcessPOSIXLog::IncNestLevel(); |
| 366 | if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) |
| 367 | log->Printf ("NativeProcessLinux::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__, |
| 368 | pid, word_size, (void*)vm_addr, buf, size); |
| 369 | |
| 370 | assert(sizeof(data) >= word_size); |
| 371 | for (bytes_read = 0; bytes_read < size; bytes_read += remainder) |
| 372 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 373 | data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, nullptr, 0, error); |
| 374 | if (error.Fail()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 375 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 376 | if (log) |
| 377 | ProcessPOSIXLog::DecNestLevel(); |
| 378 | return bytes_read; |
| 379 | } |
| 380 | |
| 381 | remainder = size - bytes_read; |
| 382 | remainder = remainder > word_size ? word_size : remainder; |
| 383 | |
| 384 | // Copy the data into our buffer |
| 385 | for (unsigned i = 0; i < remainder; ++i) |
| 386 | dst[i] = ((data >> i*8) & 0xFF); |
| 387 | |
| 388 | if (log && ProcessPOSIXLog::AtTopNestLevel() && |
| 389 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || |
| 390 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && |
| 391 | size <= POSIX_LOG_MEMORY_SHORT_BYTES))) |
| 392 | { |
| 393 | uintptr_t print_dst = 0; |
| 394 | // Format bytes from data by moving into print_dst for log output |
| 395 | for (unsigned i = 0; i < remainder; ++i) |
| 396 | print_dst |= (((data >> i*8) & 0xFF) << i*8); |
| 397 | log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, |
| 398 | (void*)vm_addr, print_dst, (unsigned long)data); |
| 399 | } |
| 400 | |
| 401 | vm_addr += word_size; |
| 402 | dst += word_size; |
| 403 | } |
| 404 | |
| 405 | if (log) |
| 406 | ProcessPOSIXLog::DecNestLevel(); |
| 407 | return bytes_read; |
| 408 | } |
| 409 | |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 410 | size_t |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 411 | DoWriteMemory( |
| 412 | lldb::pid_t pid, |
| 413 | lldb::addr_t vm_addr, |
| 414 | const void *buf, |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 415 | size_t size, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 416 | Error &error) |
| 417 | { |
| 418 | // ptrace word size is determined by the host, not the child |
| 419 | static const unsigned word_size = sizeof(void*); |
| 420 | const unsigned char *src = static_cast<const unsigned char*>(buf); |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 421 | size_t bytes_written = 0; |
| 422 | size_t remainder; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 423 | |
| 424 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); |
| 425 | if (log) |
| 426 | ProcessPOSIXLog::IncNestLevel(); |
| 427 | if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) |
| 428 | log->Printf ("NativeProcessLinux::%s(%" PRIu64 ", %u, %p, %p, %" PRIu64 ")", __FUNCTION__, |
| 429 | pid, word_size, (void*)vm_addr, buf, size); |
| 430 | |
| 431 | for (bytes_written = 0; bytes_written < size; bytes_written += remainder) |
| 432 | { |
| 433 | remainder = size - bytes_written; |
| 434 | remainder = remainder > word_size ? word_size : remainder; |
| 435 | |
| 436 | if (remainder == word_size) |
| 437 | { |
| 438 | unsigned long data = 0; |
| 439 | assert(sizeof(data) >= word_size); |
| 440 | for (unsigned i = 0; i < word_size; ++i) |
| 441 | data |= (unsigned long)src[i] << i*8; |
| 442 | |
| 443 | if (log && ProcessPOSIXLog::AtTopNestLevel() && |
| 444 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || |
| 445 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && |
| 446 | size <= POSIX_LOG_MEMORY_SHORT_BYTES))) |
| 447 | log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, |
Tamas Berghammer | 1e209fc | 2015-03-13 11:36:47 +0000 | [diff] [blame] | 448 | (void*)vm_addr, *(const unsigned long*)src, data); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 449 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 450 | if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0, error)) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 451 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 452 | if (log) |
| 453 | ProcessPOSIXLog::DecNestLevel(); |
| 454 | return bytes_written; |
| 455 | } |
| 456 | } |
| 457 | else |
| 458 | { |
| 459 | unsigned char buff[8]; |
| 460 | if (DoReadMemory(pid, vm_addr, |
| 461 | buff, word_size, error) != word_size) |
| 462 | { |
| 463 | if (log) |
| 464 | ProcessPOSIXLog::DecNestLevel(); |
| 465 | return bytes_written; |
| 466 | } |
| 467 | |
| 468 | memcpy(buff, src, remainder); |
| 469 | |
| 470 | if (DoWriteMemory(pid, vm_addr, |
| 471 | buff, word_size, error) != word_size) |
| 472 | { |
| 473 | if (log) |
| 474 | ProcessPOSIXLog::DecNestLevel(); |
| 475 | return bytes_written; |
| 476 | } |
| 477 | |
| 478 | if (log && ProcessPOSIXLog::AtTopNestLevel() && |
| 479 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || |
| 480 | (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && |
| 481 | size <= POSIX_LOG_MEMORY_SHORT_BYTES))) |
| 482 | log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, |
Tamas Berghammer | 1e209fc | 2015-03-13 11:36:47 +0000 | [diff] [blame] | 483 | (void*)vm_addr, *(const unsigned long*)src, *(unsigned long*)buff); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | vm_addr += word_size; |
| 487 | src += word_size; |
| 488 | } |
| 489 | if (log) |
| 490 | ProcessPOSIXLog::DecNestLevel(); |
| 491 | return bytes_written; |
| 492 | } |
| 493 | |
| 494 | //------------------------------------------------------------------------------ |
| 495 | /// @class Operation |
| 496 | /// @brief Represents a NativeProcessLinux operation. |
| 497 | /// |
| 498 | /// Under Linux, it is not possible to ptrace() from any other thread but the |
| 499 | /// one that spawned or attached to the process from the start. Therefore, when |
| 500 | /// a NativeProcessLinux is asked to deliver or change the state of an inferior |
| 501 | /// process the operation must be "funneled" to a specific thread to perform the |
| 502 | /// task. The Operation class provides an abstract base for all services the |
| 503 | /// NativeProcessLinux must perform via the single virtual function Execute, thus |
| 504 | /// encapsulating the code that needs to run in the privileged context. |
| 505 | class Operation |
| 506 | { |
| 507 | public: |
| 508 | Operation () : m_error() { } |
| 509 | |
| 510 | virtual |
| 511 | ~Operation() {} |
| 512 | |
| 513 | virtual void |
| 514 | Execute (NativeProcessLinux *process) = 0; |
| 515 | |
| 516 | const Error & |
| 517 | GetError () const { return m_error; } |
| 518 | |
| 519 | protected: |
| 520 | Error m_error; |
| 521 | }; |
| 522 | |
| 523 | //------------------------------------------------------------------------------ |
| 524 | /// @class ReadOperation |
| 525 | /// @brief Implements NativeProcessLinux::ReadMemory. |
| 526 | class ReadOperation : public Operation |
| 527 | { |
| 528 | public: |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 529 | ReadOperation( |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 530 | lldb::addr_t addr, |
| 531 | void *buff, |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 532 | size_t size, |
| 533 | size_t &result) : |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 534 | Operation (), |
| 535 | m_addr (addr), |
| 536 | m_buff (buff), |
| 537 | m_size (size), |
| 538 | m_result (result) |
| 539 | { |
| 540 | } |
| 541 | |
| 542 | void Execute (NativeProcessLinux *process) override; |
| 543 | |
| 544 | private: |
| 545 | lldb::addr_t m_addr; |
| 546 | void *m_buff; |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 547 | size_t m_size; |
| 548 | size_t &m_result; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 549 | }; |
| 550 | |
| 551 | void |
| 552 | ReadOperation::Execute (NativeProcessLinux *process) |
| 553 | { |
| 554 | m_result = DoReadMemory (process->GetID (), m_addr, m_buff, m_size, m_error); |
| 555 | } |
| 556 | |
| 557 | //------------------------------------------------------------------------------ |
| 558 | /// @class WriteOperation |
| 559 | /// @brief Implements NativeProcessLinux::WriteMemory. |
| 560 | class WriteOperation : public Operation |
| 561 | { |
| 562 | public: |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 563 | WriteOperation( |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 564 | lldb::addr_t addr, |
| 565 | const void *buff, |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 566 | size_t size, |
| 567 | size_t &result) : |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 568 | Operation (), |
| 569 | m_addr (addr), |
| 570 | m_buff (buff), |
| 571 | m_size (size), |
| 572 | m_result (result) |
| 573 | { |
| 574 | } |
| 575 | |
| 576 | void Execute (NativeProcessLinux *process) override; |
| 577 | |
| 578 | private: |
| 579 | lldb::addr_t m_addr; |
| 580 | const void *m_buff; |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 581 | size_t m_size; |
| 582 | size_t &m_result; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 583 | }; |
| 584 | |
| 585 | void |
| 586 | WriteOperation::Execute(NativeProcessLinux *process) |
| 587 | { |
| 588 | m_result = DoWriteMemory (process->GetID (), m_addr, m_buff, m_size, m_error); |
| 589 | } |
| 590 | |
| 591 | //------------------------------------------------------------------------------ |
| 592 | /// @class ReadRegOperation |
| 593 | /// @brief Implements NativeProcessLinux::ReadRegisterValue. |
| 594 | class ReadRegOperation : public Operation |
| 595 | { |
| 596 | public: |
| 597 | ReadRegOperation(lldb::tid_t tid, uint32_t offset, const char *reg_name, |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 598 | RegisterValue &value) |
| 599 | : m_tid(tid), |
| 600 | m_offset(static_cast<uintptr_t> (offset)), |
| 601 | m_reg_name(reg_name), |
| 602 | m_value(value) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 603 | { } |
| 604 | |
Tamas Berghammer | d542efd | 2015-03-25 15:37:56 +0000 | [diff] [blame] | 605 | void Execute(NativeProcessLinux *monitor) override; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 606 | |
| 607 | private: |
| 608 | lldb::tid_t m_tid; |
| 609 | uintptr_t m_offset; |
| 610 | const char *m_reg_name; |
| 611 | RegisterValue &m_value; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 612 | }; |
| 613 | |
| 614 | void |
| 615 | ReadRegOperation::Execute(NativeProcessLinux *monitor) |
| 616 | { |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 617 | #if defined (__arm64__) || defined (__aarch64__) |
| 618 | if (m_offset > sizeof(struct user_pt_regs)) |
| 619 | { |
| 620 | uintptr_t offset = m_offset - sizeof(struct user_pt_regs); |
| 621 | if (offset > sizeof(struct user_fpsimd_state)) |
| 622 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 623 | m_error.SetErrorString("invalid offset value"); |
| 624 | return; |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 625 | } |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 626 | elf_fpregset_t regs; |
| 627 | int regset = NT_FPREGSET; |
| 628 | struct iovec ioVec; |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 629 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 630 | ioVec.iov_base = ®s; |
| 631 | ioVec.iov_len = sizeof regs; |
| 632 | PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); |
| 633 | if (m_error.Success()) |
| 634 | { |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 635 | ArchSpec arch; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 636 | if (monitor->GetArchitecture(arch)) |
| 637 | m_value.SetBytes((void *)(((unsigned char *)(®s)) + offset), 16, arch.GetByteOrder()); |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 638 | else |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 639 | m_error.SetErrorString("failed to get architecture"); |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | else |
| 643 | { |
| 644 | elf_gregset_t regs; |
| 645 | int regset = NT_PRSTATUS; |
| 646 | struct iovec ioVec; |
| 647 | |
| 648 | ioVec.iov_base = ®s; |
| 649 | ioVec.iov_len = sizeof regs; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 650 | PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); |
| 651 | if (m_error.Success()) |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 652 | { |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 653 | ArchSpec arch; |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 654 | if (monitor->GetArchitecture(arch)) |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 655 | m_value.SetBytes((void *)(((unsigned char *)(regs)) + m_offset), 8, arch.GetByteOrder()); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 656 | else |
| 657 | m_error.SetErrorString("failed to get architecture"); |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 658 | } |
| 659 | } |
Mohit K. Bhakkad | 09ba1a3 | 2015-03-31 12:01:27 +0000 | [diff] [blame] | 660 | #elif defined (__mips__) |
| 661 | elf_gregset_t regs; |
| 662 | PTRACE(PTRACE_GETREGS, m_tid, NULL, ®s, sizeof regs, m_error); |
| 663 | if (m_error.Success()) |
| 664 | { |
| 665 | lldb_private::ArchSpec arch; |
| 666 | if (monitor->GetArchitecture(arch)) |
| 667 | m_value.SetBytes((void *)(((unsigned char *)(regs)) + m_offset), 8, arch.GetByteOrder()); |
| 668 | else |
| 669 | m_error.SetErrorString("failed to get architecture"); |
| 670 | } |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 671 | #else |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 672 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS)); |
| 673 | |
Tamas Berghammer | adf8adb | 2015-03-25 10:14:19 +0000 | [diff] [blame] | 674 | lldb::addr_t data = static_cast<unsigned long>(PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, nullptr, 0, m_error)); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 675 | if (m_error.Success()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 676 | m_value = data; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 677 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 678 | if (log) |
| 679 | log->Printf ("NativeProcessLinux::%s() reg %s: 0x%" PRIx64, __FUNCTION__, |
| 680 | m_reg_name, data); |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 681 | #endif |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | //------------------------------------------------------------------------------ |
| 685 | /// @class WriteRegOperation |
| 686 | /// @brief Implements NativeProcessLinux::WriteRegisterValue. |
| 687 | class WriteRegOperation : public Operation |
| 688 | { |
| 689 | public: |
| 690 | WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name, |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 691 | const RegisterValue &value) |
| 692 | : m_tid(tid), |
| 693 | m_offset(offset), |
| 694 | m_reg_name(reg_name), |
| 695 | m_value(value) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 696 | { } |
| 697 | |
Tamas Berghammer | d542efd | 2015-03-25 15:37:56 +0000 | [diff] [blame] | 698 | void Execute(NativeProcessLinux *monitor) override; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 699 | |
| 700 | private: |
| 701 | lldb::tid_t m_tid; |
| 702 | uintptr_t m_offset; |
| 703 | const char *m_reg_name; |
| 704 | const RegisterValue &m_value; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 705 | }; |
| 706 | |
| 707 | void |
| 708 | WriteRegOperation::Execute(NativeProcessLinux *monitor) |
| 709 | { |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 710 | #if defined (__arm64__) || defined (__aarch64__) |
| 711 | if (m_offset > sizeof(struct user_pt_regs)) |
| 712 | { |
| 713 | uintptr_t offset = m_offset - sizeof(struct user_pt_regs); |
| 714 | if (offset > sizeof(struct user_fpsimd_state)) |
| 715 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 716 | m_error.SetErrorString("invalid offset value"); |
| 717 | return; |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 718 | } |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 719 | elf_fpregset_t regs; |
| 720 | int regset = NT_FPREGSET; |
| 721 | struct iovec ioVec; |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 722 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 723 | ioVec.iov_base = ®s; |
| 724 | ioVec.iov_len = sizeof regs; |
| 725 | PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); |
Bhushan D. Attarde | 9425b32 | 2015-03-12 09:17:22 +0000 | [diff] [blame] | 726 | if (m_error.Success()) |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 727 | { |
| 728 | ::memcpy((void *)(((unsigned char *)(®s)) + offset), m_value.GetBytes(), 16); |
| 729 | PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | else |
| 733 | { |
| 734 | elf_gregset_t regs; |
| 735 | int regset = NT_PRSTATUS; |
| 736 | struct iovec ioVec; |
| 737 | |
| 738 | ioVec.iov_base = ®s; |
| 739 | ioVec.iov_len = sizeof regs; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 740 | PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); |
Bhushan D. Attarde | 9425b32 | 2015-03-12 09:17:22 +0000 | [diff] [blame] | 741 | if (m_error.Success()) |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 742 | { |
| 743 | ::memcpy((void *)(((unsigned char *)(®s)) + m_offset), m_value.GetBytes(), 8); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 744 | PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 745 | } |
| 746 | } |
Mohit K. Bhakkad | 09ba1a3 | 2015-03-31 12:01:27 +0000 | [diff] [blame] | 747 | #elif defined (__mips__) |
| 748 | elf_gregset_t regs; |
| 749 | PTRACE(PTRACE_GETREGS, m_tid, NULL, ®s, sizeof regs, m_error); |
| 750 | if (m_error.Success()) |
| 751 | { |
| 752 | ::memcpy((void *)(((unsigned char *)(®s)) + m_offset), m_value.GetBytes(), 8); |
| 753 | PTRACE(PTRACE_SETREGS, m_tid, NULL, ®s, sizeof regs, m_error); |
| 754 | } |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 755 | #else |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 756 | void* buf; |
| 757 | Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS)); |
| 758 | |
| 759 | buf = (void*) m_value.GetAsUInt64(); |
| 760 | |
| 761 | if (log) |
| 762 | log->Printf ("NativeProcessLinux::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 763 | PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0, m_error); |
Todd Fiala | 0fceef8 | 2014-09-15 17:09:23 +0000 | [diff] [blame] | 764 | #endif |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | //------------------------------------------------------------------------------ |
| 768 | /// @class ReadGPROperation |
| 769 | /// @brief Implements NativeProcessLinux::ReadGPR. |
| 770 | class ReadGPROperation : public Operation |
| 771 | { |
| 772 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 773 | ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size) |
| 774 | : m_tid(tid), m_buf(buf), m_buf_size(buf_size) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 775 | { } |
| 776 | |
Tamas Berghammer | d542efd | 2015-03-25 15:37:56 +0000 | [diff] [blame] | 777 | void Execute(NativeProcessLinux *monitor) override; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 778 | |
| 779 | private: |
| 780 | lldb::tid_t m_tid; |
| 781 | void *m_buf; |
| 782 | size_t m_buf_size; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 783 | }; |
| 784 | |
| 785 | void |
| 786 | ReadGPROperation::Execute(NativeProcessLinux *monitor) |
| 787 | { |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 788 | #if defined (__arm64__) || defined (__aarch64__) |
| 789 | int regset = NT_PRSTATUS; |
| 790 | struct iovec ioVec; |
| 791 | |
| 792 | ioVec.iov_base = m_buf; |
| 793 | ioVec.iov_len = m_buf_size; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 794 | PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 795 | #else |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 796 | PTRACE(PTRACE_GETREGS, m_tid, nullptr, m_buf, m_buf_size, m_error); |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 797 | #endif |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | //------------------------------------------------------------------------------ |
| 801 | /// @class ReadFPROperation |
| 802 | /// @brief Implements NativeProcessLinux::ReadFPR. |
| 803 | class ReadFPROperation : public Operation |
| 804 | { |
| 805 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 806 | ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size) |
| 807 | : m_tid(tid), |
| 808 | m_buf(buf), |
| 809 | m_buf_size(buf_size) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 810 | { } |
| 811 | |
Tamas Berghammer | d542efd | 2015-03-25 15:37:56 +0000 | [diff] [blame] | 812 | void Execute(NativeProcessLinux *monitor) override; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 813 | |
| 814 | private: |
| 815 | lldb::tid_t m_tid; |
| 816 | void *m_buf; |
| 817 | size_t m_buf_size; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 818 | }; |
| 819 | |
| 820 | void |
| 821 | ReadFPROperation::Execute(NativeProcessLinux *monitor) |
| 822 | { |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 823 | #if defined (__arm64__) || defined (__aarch64__) |
| 824 | int regset = NT_FPREGSET; |
| 825 | struct iovec ioVec; |
| 826 | |
| 827 | ioVec.iov_base = m_buf; |
| 828 | ioVec.iov_len = m_buf_size; |
Tamas Berghammer | 1e209fc | 2015-03-13 11:36:47 +0000 | [diff] [blame] | 829 | PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 830 | #else |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 831 | PTRACE(PTRACE_GETFPREGS, m_tid, nullptr, m_buf, m_buf_size, m_error); |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 832 | #endif |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | //------------------------------------------------------------------------------ |
| 836 | /// @class ReadRegisterSetOperation |
| 837 | /// @brief Implements NativeProcessLinux::ReadRegisterSet. |
| 838 | class ReadRegisterSetOperation : public Operation |
| 839 | { |
| 840 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 841 | ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) |
| 842 | : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 843 | { } |
| 844 | |
Tamas Berghammer | d542efd | 2015-03-25 15:37:56 +0000 | [diff] [blame] | 845 | void Execute(NativeProcessLinux *monitor) override; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 846 | |
| 847 | private: |
| 848 | lldb::tid_t m_tid; |
| 849 | void *m_buf; |
| 850 | size_t m_buf_size; |
| 851 | const unsigned int m_regset; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 852 | }; |
| 853 | |
| 854 | void |
| 855 | ReadRegisterSetOperation::Execute(NativeProcessLinux *monitor) |
| 856 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 857 | PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size, m_error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | //------------------------------------------------------------------------------ |
| 861 | /// @class WriteGPROperation |
| 862 | /// @brief Implements NativeProcessLinux::WriteGPR. |
| 863 | class WriteGPROperation : public Operation |
| 864 | { |
| 865 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 866 | WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size) |
| 867 | : m_tid(tid), m_buf(buf), m_buf_size(buf_size) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 868 | { } |
| 869 | |
Tamas Berghammer | d542efd | 2015-03-25 15:37:56 +0000 | [diff] [blame] | 870 | void Execute(NativeProcessLinux *monitor) override; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 871 | |
| 872 | private: |
| 873 | lldb::tid_t m_tid; |
| 874 | void *m_buf; |
| 875 | size_t m_buf_size; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 876 | }; |
| 877 | |
| 878 | void |
| 879 | WriteGPROperation::Execute(NativeProcessLinux *monitor) |
| 880 | { |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 881 | #if defined (__arm64__) || defined (__aarch64__) |
| 882 | int regset = NT_PRSTATUS; |
| 883 | struct iovec ioVec; |
| 884 | |
| 885 | ioVec.iov_base = m_buf; |
| 886 | ioVec.iov_len = m_buf_size; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 887 | PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 888 | #else |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 889 | PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size, m_error); |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 890 | #endif |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | //------------------------------------------------------------------------------ |
| 894 | /// @class WriteFPROperation |
| 895 | /// @brief Implements NativeProcessLinux::WriteFPR. |
| 896 | class WriteFPROperation : public Operation |
| 897 | { |
| 898 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 899 | WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size) |
| 900 | : m_tid(tid), m_buf(buf), m_buf_size(buf_size) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 901 | { } |
| 902 | |
Tamas Berghammer | d542efd | 2015-03-25 15:37:56 +0000 | [diff] [blame] | 903 | void Execute(NativeProcessLinux *monitor) override; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 904 | |
| 905 | private: |
| 906 | lldb::tid_t m_tid; |
| 907 | void *m_buf; |
| 908 | size_t m_buf_size; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 909 | }; |
| 910 | |
| 911 | void |
| 912 | WriteFPROperation::Execute(NativeProcessLinux *monitor) |
| 913 | { |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 914 | #if defined (__arm64__) || defined (__aarch64__) |
| 915 | int regset = NT_FPREGSET; |
| 916 | struct iovec ioVec; |
| 917 | |
| 918 | ioVec.iov_base = m_buf; |
| 919 | ioVec.iov_len = m_buf_size; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 920 | PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 921 | #else |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 922 | PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size, m_error); |
Todd Fiala | 6ac1be4 | 2014-08-21 16:34:03 +0000 | [diff] [blame] | 923 | #endif |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | //------------------------------------------------------------------------------ |
| 927 | /// @class WriteRegisterSetOperation |
| 928 | /// @brief Implements NativeProcessLinux::WriteRegisterSet. |
| 929 | class WriteRegisterSetOperation : public Operation |
| 930 | { |
| 931 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 932 | WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) |
| 933 | : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 934 | { } |
| 935 | |
Tamas Berghammer | d542efd | 2015-03-25 15:37:56 +0000 | [diff] [blame] | 936 | void Execute(NativeProcessLinux *monitor) override; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 937 | |
| 938 | private: |
| 939 | lldb::tid_t m_tid; |
| 940 | void *m_buf; |
| 941 | size_t m_buf_size; |
| 942 | const unsigned int m_regset; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 943 | }; |
| 944 | |
| 945 | void |
| 946 | WriteRegisterSetOperation::Execute(NativeProcessLinux *monitor) |
| 947 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 948 | PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size, m_error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | //------------------------------------------------------------------------------ |
| 952 | /// @class ResumeOperation |
| 953 | /// @brief Implements NativeProcessLinux::Resume. |
| 954 | class ResumeOperation : public Operation |
| 955 | { |
| 956 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 957 | ResumeOperation(lldb::tid_t tid, uint32_t signo) : |
| 958 | m_tid(tid), m_signo(signo) { } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 959 | |
Tamas Berghammer | d542efd | 2015-03-25 15:37:56 +0000 | [diff] [blame] | 960 | void Execute(NativeProcessLinux *monitor) override; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 961 | |
| 962 | private: |
| 963 | lldb::tid_t m_tid; |
| 964 | uint32_t m_signo; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 965 | }; |
| 966 | |
| 967 | void |
| 968 | ResumeOperation::Execute(NativeProcessLinux *monitor) |
| 969 | { |
| 970 | intptr_t data = 0; |
| 971 | |
| 972 | if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) |
| 973 | data = m_signo; |
| 974 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 975 | PTRACE(PTRACE_CONT, m_tid, nullptr, (void*)data, 0, m_error); |
| 976 | if (m_error.Fail()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 977 | { |
| 978 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 979 | |
| 980 | if (log) |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 981 | log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, m_error.AsCString()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 982 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | //------------------------------------------------------------------------------ |
| 986 | /// @class SingleStepOperation |
| 987 | /// @brief Implements NativeProcessLinux::SingleStep. |
| 988 | class SingleStepOperation : public Operation |
| 989 | { |
| 990 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 991 | SingleStepOperation(lldb::tid_t tid, uint32_t signo) |
| 992 | : m_tid(tid), m_signo(signo) { } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 993 | |
Tamas Berghammer | d542efd | 2015-03-25 15:37:56 +0000 | [diff] [blame] | 994 | void Execute(NativeProcessLinux *monitor) override; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 995 | |
| 996 | private: |
| 997 | lldb::tid_t m_tid; |
| 998 | uint32_t m_signo; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 999 | }; |
| 1000 | |
| 1001 | void |
| 1002 | SingleStepOperation::Execute(NativeProcessLinux *monitor) |
| 1003 | { |
| 1004 | intptr_t data = 0; |
| 1005 | |
| 1006 | if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) |
| 1007 | data = m_signo; |
| 1008 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 1009 | PTRACE(PTRACE_SINGLESTEP, m_tid, nullptr, (void*)data, 0, m_error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | //------------------------------------------------------------------------------ |
| 1013 | /// @class SiginfoOperation |
| 1014 | /// @brief Implements NativeProcessLinux::GetSignalInfo. |
| 1015 | class SiginfoOperation : public Operation |
| 1016 | { |
| 1017 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 1018 | SiginfoOperation(lldb::tid_t tid, void *info) |
| 1019 | : m_tid(tid), m_info(info) { } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1020 | |
Tamas Berghammer | d542efd | 2015-03-25 15:37:56 +0000 | [diff] [blame] | 1021 | void Execute(NativeProcessLinux *monitor) override; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1022 | |
| 1023 | private: |
| 1024 | lldb::tid_t m_tid; |
| 1025 | void *m_info; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1026 | }; |
| 1027 | |
| 1028 | void |
| 1029 | SiginfoOperation::Execute(NativeProcessLinux *monitor) |
| 1030 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 1031 | PTRACE(PTRACE_GETSIGINFO, m_tid, nullptr, m_info, 0, m_error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | //------------------------------------------------------------------------------ |
| 1035 | /// @class EventMessageOperation |
| 1036 | /// @brief Implements NativeProcessLinux::GetEventMessage. |
| 1037 | class EventMessageOperation : public Operation |
| 1038 | { |
| 1039 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 1040 | EventMessageOperation(lldb::tid_t tid, unsigned long *message) |
| 1041 | : m_tid(tid), m_message(message) { } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1042 | |
Tamas Berghammer | d542efd | 2015-03-25 15:37:56 +0000 | [diff] [blame] | 1043 | void Execute(NativeProcessLinux *monitor) override; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1044 | |
| 1045 | private: |
| 1046 | lldb::tid_t m_tid; |
| 1047 | unsigned long *m_message; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1048 | }; |
| 1049 | |
| 1050 | void |
| 1051 | EventMessageOperation::Execute(NativeProcessLinux *monitor) |
| 1052 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 1053 | PTRACE(PTRACE_GETEVENTMSG, m_tid, nullptr, m_message, 0, m_error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | class DetachOperation : public Operation |
| 1057 | { |
| 1058 | public: |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 1059 | DetachOperation(lldb::tid_t tid) : m_tid(tid) { } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1060 | |
Tamas Berghammer | d542efd | 2015-03-25 15:37:56 +0000 | [diff] [blame] | 1061 | void Execute(NativeProcessLinux *monitor) override; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1062 | |
| 1063 | private: |
| 1064 | lldb::tid_t m_tid; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1065 | }; |
| 1066 | |
| 1067 | void |
| 1068 | DetachOperation::Execute(NativeProcessLinux *monitor) |
| 1069 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 1070 | PTRACE(PTRACE_DETACH, m_tid, nullptr, 0, 0, m_error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1071 | } |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1072 | } // end of anonymous namespace |
| 1073 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1074 | // Simple helper function to ensure flags are enabled on the given file |
| 1075 | // descriptor. |
| 1076 | static Error |
| 1077 | EnsureFDFlags(int fd, int flags) |
| 1078 | { |
| 1079 | Error error; |
| 1080 | |
| 1081 | int status = fcntl(fd, F_GETFL); |
| 1082 | if (status == -1) |
| 1083 | { |
| 1084 | error.SetErrorToErrno(); |
| 1085 | return error; |
| 1086 | } |
| 1087 | |
| 1088 | if (fcntl(fd, F_SETFL, status | flags) == -1) |
| 1089 | { |
| 1090 | error.SetErrorToErrno(); |
| 1091 | return error; |
| 1092 | } |
| 1093 | |
| 1094 | return error; |
| 1095 | } |
| 1096 | |
| 1097 | // This class encapsulates the privileged thread which performs all ptrace and wait operations on |
| 1098 | // the inferior. The thread consists of a main loop which waits for events and processes them |
| 1099 | // - SIGCHLD (delivered over a signalfd file descriptor): These signals notify us of events in |
| 1100 | // the inferior process. Upon receiving this signal we do a waitpid to get more information |
| 1101 | // and dispatch to NativeProcessLinux::MonitorCallback. |
| 1102 | // - requests for ptrace operations: These initiated via the DoOperation method, which funnels |
| 1103 | // them to the Monitor thread via m_operation member. The Monitor thread is signaled over a |
| 1104 | // pipe, and the completion of the operation is signalled over the semaphore. |
| 1105 | // - thread exit event: this is signaled from the Monitor destructor by closing the write end |
| 1106 | // of the command pipe. |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 1107 | class NativeProcessLinux::Monitor |
| 1108 | { |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1109 | private: |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1110 | // The initial monitor operation (launch or attach). It returns a inferior process id. |
| 1111 | std::unique_ptr<InitialOperation> m_initial_operation_up; |
| 1112 | |
| 1113 | ::pid_t m_child_pid = -1; |
| 1114 | NativeProcessLinux * m_native_process; |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1115 | |
| 1116 | enum { READ, WRITE }; |
| 1117 | int m_pipefd[2] = {-1, -1}; |
| 1118 | int m_signal_fd = -1; |
| 1119 | HostThread m_thread; |
| 1120 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1121 | // current operation which must be executed on the priviliged thread |
| 1122 | Mutex m_operation_mutex; |
| 1123 | Operation *m_operation = nullptr; |
| 1124 | sem_t m_operation_sem; |
| 1125 | Error m_operation_error; |
| 1126 | |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 1127 | unsigned m_operation_nesting_level = 0; |
| 1128 | |
| 1129 | static constexpr char operation_command = 'o'; |
| 1130 | static constexpr char begin_block_command = '{'; |
| 1131 | static constexpr char end_block_command = '}'; |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1132 | |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1133 | void |
| 1134 | HandleSignals(); |
| 1135 | |
| 1136 | void |
| 1137 | HandleWait(); |
| 1138 | |
| 1139 | // Returns true if the thread should exit. |
| 1140 | bool |
| 1141 | HandleCommands(); |
| 1142 | |
| 1143 | void |
| 1144 | MainLoop(); |
| 1145 | |
| 1146 | static void * |
| 1147 | RunMonitor(void *arg); |
| 1148 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1149 | Error |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 1150 | WaitForAck(); |
| 1151 | |
| 1152 | void |
| 1153 | BeginOperationBlock() |
| 1154 | { |
| 1155 | write(m_pipefd[WRITE], &begin_block_command, sizeof operation_command); |
| 1156 | WaitForAck(); |
| 1157 | } |
| 1158 | |
| 1159 | void |
| 1160 | EndOperationBlock() |
| 1161 | { |
| 1162 | write(m_pipefd[WRITE], &end_block_command, sizeof operation_command); |
| 1163 | WaitForAck(); |
| 1164 | } |
| 1165 | |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1166 | public: |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1167 | Monitor(const InitialOperation &initial_operation, |
| 1168 | NativeProcessLinux *native_process) |
| 1169 | : m_initial_operation_up(new InitialOperation(initial_operation)), |
| 1170 | m_native_process(native_process) |
| 1171 | { |
| 1172 | sem_init(&m_operation_sem, 0, 0); |
| 1173 | } |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1174 | |
| 1175 | ~Monitor(); |
| 1176 | |
| 1177 | Error |
| 1178 | Initialize(); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1179 | |
| 1180 | void |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 1181 | Terminate(); |
| 1182 | |
| 1183 | void |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1184 | DoOperation(Operation *op); |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 1185 | |
| 1186 | class ScopedOperationLock { |
| 1187 | Monitor &m_monitor; |
| 1188 | |
| 1189 | public: |
| 1190 | ScopedOperationLock(Monitor &monitor) |
| 1191 | : m_monitor(monitor) |
| 1192 | { m_monitor.BeginOperationBlock(); } |
| 1193 | |
| 1194 | ~ScopedOperationLock() |
| 1195 | { m_monitor.EndOperationBlock(); } |
| 1196 | }; |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1197 | }; |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1198 | constexpr char NativeProcessLinux::Monitor::operation_command; |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 1199 | constexpr char NativeProcessLinux::Monitor::begin_block_command; |
| 1200 | constexpr char NativeProcessLinux::Monitor::end_block_command; |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1201 | |
| 1202 | Error |
| 1203 | NativeProcessLinux::Monitor::Initialize() |
| 1204 | { |
| 1205 | Error error; |
| 1206 | |
| 1207 | // We get a SIGCHLD every time something interesting happens with the inferior. We shall be |
| 1208 | // listening for these signals over a signalfd file descriptors. This allows us to wait for |
| 1209 | // multiple kinds of events with select. |
| 1210 | sigset_t signals; |
| 1211 | sigemptyset(&signals); |
| 1212 | sigaddset(&signals, SIGCHLD); |
| 1213 | m_signal_fd = signalfd(-1, &signals, SFD_NONBLOCK | SFD_CLOEXEC); |
| 1214 | if (m_signal_fd < 0) |
| 1215 | { |
| 1216 | return Error("NativeProcessLinux::Monitor::%s failed due to signalfd failure. Monitoring the inferior will be impossible: %s", |
| 1217 | __FUNCTION__, strerror(errno)); |
| 1218 | |
| 1219 | } |
| 1220 | |
| 1221 | if (pipe2(m_pipefd, O_CLOEXEC) == -1) |
| 1222 | { |
| 1223 | error.SetErrorToErrno(); |
| 1224 | return error; |
| 1225 | } |
| 1226 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1227 | if ((error = EnsureFDFlags(m_pipefd[READ], O_NONBLOCK)).Fail()) { |
| 1228 | return error; |
| 1229 | } |
| 1230 | |
| 1231 | static const char g_thread_name[] = "lldb.process.nativelinux.monitor"; |
| 1232 | m_thread = ThreadLauncher::LaunchThread(g_thread_name, Monitor::RunMonitor, this, nullptr); |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1233 | if (!m_thread.IsJoinable()) |
| 1234 | return Error("Failed to create monitor thread for NativeProcessLinux."); |
| 1235 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1236 | // Wait for initial operation to complete. |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 1237 | return WaitForAck(); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | void |
| 1241 | NativeProcessLinux::Monitor::DoOperation(Operation *op) |
| 1242 | { |
| 1243 | if (m_thread.EqualsThread(pthread_self())) { |
| 1244 | // If we're on the Monitor thread, we can simply execute the operation. |
| 1245 | op->Execute(m_native_process); |
| 1246 | return; |
| 1247 | } |
| 1248 | |
| 1249 | // Otherwise we need to pass the operation to the Monitor thread so it can handle it. |
| 1250 | Mutex::Locker lock(m_operation_mutex); |
| 1251 | |
| 1252 | m_operation = op; |
| 1253 | |
| 1254 | // notify the thread that an operation is ready to be processed |
| 1255 | write(m_pipefd[WRITE], &operation_command, sizeof operation_command); |
| 1256 | |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 1257 | WaitForAck(); |
| 1258 | } |
| 1259 | |
| 1260 | void |
| 1261 | NativeProcessLinux::Monitor::Terminate() |
| 1262 | { |
| 1263 | if (m_pipefd[WRITE] >= 0) |
| 1264 | { |
| 1265 | close(m_pipefd[WRITE]); |
| 1266 | m_pipefd[WRITE] = -1; |
| 1267 | } |
| 1268 | if (m_thread.IsJoinable()) |
| 1269 | m_thread.Join(nullptr); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1272 | NativeProcessLinux::Monitor::~Monitor() |
| 1273 | { |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 1274 | Terminate(); |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1275 | if (m_pipefd[READ] >= 0) |
| 1276 | close(m_pipefd[READ]); |
| 1277 | if (m_signal_fd >= 0) |
| 1278 | close(m_signal_fd); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1279 | sem_destroy(&m_operation_sem); |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | void |
| 1283 | NativeProcessLinux::Monitor::HandleSignals() |
| 1284 | { |
| 1285 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 1286 | |
| 1287 | // We don't really care about the content of the SIGCHLD siginfo structure, as we will get |
| 1288 | // all the information from waitpid(). We just need to read all the signals so that we can |
| 1289 | // sleep next time we reach select(). |
| 1290 | while (true) |
| 1291 | { |
| 1292 | signalfd_siginfo info; |
| 1293 | ssize_t size = read(m_signal_fd, &info, sizeof info); |
| 1294 | if (size == -1) |
| 1295 | { |
| 1296 | if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 1297 | break; // We are done. |
| 1298 | if (errno == EINTR) |
| 1299 | continue; |
| 1300 | if (log) |
| 1301 | log->Printf("NativeProcessLinux::Monitor::%s reading from signalfd file descriptor failed: %s", |
| 1302 | __FUNCTION__, strerror(errno)); |
| 1303 | break; |
| 1304 | } |
| 1305 | if (size != sizeof info) |
| 1306 | { |
| 1307 | // We got incomplete information structure. This should not happen, let's just log |
| 1308 | // that. |
| 1309 | if (log) |
| 1310 | log->Printf("NativeProcessLinux::Monitor::%s reading from signalfd file descriptor returned incomplete data: " |
| 1311 | "structure size is %zd, read returned %zd bytes", |
| 1312 | __FUNCTION__, sizeof info, size); |
| 1313 | break; |
| 1314 | } |
| 1315 | if (log) |
| 1316 | log->Printf("NativeProcessLinux::Monitor::%s received signal %s(%d).", __FUNCTION__, |
| 1317 | Host::GetSignalAsCString(info.ssi_signo), info.ssi_signo); |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | void |
| 1322 | NativeProcessLinux::Monitor::HandleWait() |
| 1323 | { |
| 1324 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 1325 | // Process all pending waitpid notifications. |
| 1326 | while (true) |
| 1327 | { |
| 1328 | int status = -1; |
| 1329 | ::pid_t wait_pid = waitpid(m_child_pid, &status, __WALL | WNOHANG); |
| 1330 | |
| 1331 | if (wait_pid == 0) |
| 1332 | break; // We are done. |
| 1333 | |
| 1334 | if (wait_pid == -1) |
| 1335 | { |
| 1336 | if (errno == EINTR) |
| 1337 | continue; |
| 1338 | |
| 1339 | if (log) |
| 1340 | log->Printf("NativeProcessLinux::Monitor::%s waitpid (pid = %" PRIi32 ", &status, __WALL | WNOHANG) failed: %s", |
| 1341 | __FUNCTION__, m_child_pid, strerror(errno)); |
| 1342 | break; |
| 1343 | } |
| 1344 | |
| 1345 | bool exited = false; |
| 1346 | int signal = 0; |
| 1347 | int exit_status = 0; |
| 1348 | const char *status_cstr = NULL; |
| 1349 | if (WIFSTOPPED(status)) |
| 1350 | { |
| 1351 | signal = WSTOPSIG(status); |
| 1352 | status_cstr = "STOPPED"; |
| 1353 | } |
| 1354 | else if (WIFEXITED(status)) |
| 1355 | { |
| 1356 | exit_status = WEXITSTATUS(status); |
| 1357 | status_cstr = "EXITED"; |
| 1358 | exited = true; |
| 1359 | } |
| 1360 | else if (WIFSIGNALED(status)) |
| 1361 | { |
| 1362 | signal = WTERMSIG(status); |
| 1363 | status_cstr = "SIGNALED"; |
| 1364 | if (wait_pid == abs(m_child_pid)) { |
| 1365 | exited = true; |
| 1366 | exit_status = -1; |
| 1367 | } |
| 1368 | } |
| 1369 | else |
| 1370 | status_cstr = "(\?\?\?)"; |
| 1371 | |
| 1372 | if (log) |
| 1373 | log->Printf("NativeProcessLinux::Monitor::%s: waitpid (pid = %" PRIi32 ", &status, __WALL | WNOHANG)" |
| 1374 | "=> pid = %" PRIi32 ", status = 0x%8.8x (%s), signal = %i, exit_state = %i", |
| 1375 | __FUNCTION__, m_child_pid, wait_pid, status, status_cstr, signal, exit_status); |
| 1376 | |
| 1377 | m_native_process->MonitorCallback (wait_pid, exited, signal, exit_status); |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | bool |
| 1382 | NativeProcessLinux::Monitor::HandleCommands() |
| 1383 | { |
| 1384 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 1385 | |
| 1386 | while (true) |
| 1387 | { |
| 1388 | char command = 0; |
| 1389 | ssize_t size = read(m_pipefd[READ], &command, sizeof command); |
| 1390 | if (size == -1) |
| 1391 | { |
| 1392 | if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 1393 | return false; |
| 1394 | if (errno == EINTR) |
| 1395 | continue; |
| 1396 | if (log) |
| 1397 | log->Printf("NativeProcessLinux::Monitor::%s exiting because read from command file descriptor failed: %s", __FUNCTION__, strerror(errno)); |
| 1398 | return true; |
| 1399 | } |
| 1400 | if (size == 0) // end of file - write end closed |
| 1401 | { |
| 1402 | if (log) |
| 1403 | log->Printf("NativeProcessLinux::Monitor::%s exit command received, exiting...", __FUNCTION__); |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 1404 | assert(m_operation_nesting_level == 0 && "Unbalanced begin/end block commands detected"); |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1405 | return true; // We are done. |
| 1406 | } |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1407 | |
| 1408 | switch (command) |
| 1409 | { |
| 1410 | case operation_command: |
| 1411 | m_operation->Execute(m_native_process); |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 1412 | break; |
| 1413 | case begin_block_command: |
| 1414 | ++m_operation_nesting_level; |
| 1415 | break; |
| 1416 | case end_block_command: |
| 1417 | assert(m_operation_nesting_level > 0); |
| 1418 | --m_operation_nesting_level; |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1419 | break; |
| 1420 | default: |
| 1421 | if (log) |
| 1422 | log->Printf("NativeProcessLinux::Monitor::%s received unknown command '%c'", |
| 1423 | __FUNCTION__, command); |
| 1424 | } |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 1425 | |
| 1426 | // notify calling thread that the command has been processed |
| 1427 | sem_post(&m_operation_sem); |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | void |
| 1432 | NativeProcessLinux::Monitor::MainLoop() |
| 1433 | { |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1434 | ::pid_t child_pid = (*m_initial_operation_up)(m_operation_error); |
| 1435 | m_initial_operation_up.reset(); |
| 1436 | m_child_pid = -getpgid(child_pid), |
| 1437 | sem_post(&m_operation_sem); |
| 1438 | |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1439 | while (true) |
| 1440 | { |
| 1441 | fd_set fds; |
| 1442 | FD_ZERO(&fds); |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 1443 | // Only process waitpid events if we are outside of an operation block. Any pending |
| 1444 | // events will be processed after we leave the block. |
| 1445 | if (m_operation_nesting_level == 0) |
| 1446 | FD_SET(m_signal_fd, &fds); |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1447 | FD_SET(m_pipefd[READ], &fds); |
| 1448 | |
| 1449 | int max_fd = std::max(m_signal_fd, m_pipefd[READ]) + 1; |
| 1450 | int r = select(max_fd, &fds, nullptr, nullptr, nullptr); |
| 1451 | if (r < 0) |
| 1452 | { |
| 1453 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 1454 | if (log) |
| 1455 | log->Printf("NativeProcessLinux::Monitor::%s exiting because select failed: %s", |
| 1456 | __FUNCTION__, strerror(errno)); |
| 1457 | return; |
| 1458 | } |
| 1459 | |
| 1460 | if (FD_ISSET(m_pipefd[READ], &fds)) |
| 1461 | { |
| 1462 | if (HandleCommands()) |
| 1463 | return; |
| 1464 | } |
| 1465 | |
| 1466 | if (FD_ISSET(m_signal_fd, &fds)) |
| 1467 | { |
| 1468 | HandleSignals(); |
| 1469 | HandleWait(); |
| 1470 | } |
| 1471 | } |
| 1472 | } |
| 1473 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1474 | Error |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 1475 | NativeProcessLinux::Monitor::WaitForAck() |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1476 | { |
| 1477 | Error error; |
| 1478 | while (sem_wait(&m_operation_sem) != 0) |
| 1479 | { |
| 1480 | if (errno == EINTR) |
| 1481 | continue; |
| 1482 | |
| 1483 | error.SetErrorToErrno(); |
| 1484 | return error; |
| 1485 | } |
| 1486 | |
| 1487 | return m_operation_error; |
| 1488 | } |
| 1489 | |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1490 | void * |
| 1491 | NativeProcessLinux::Monitor::RunMonitor(void *arg) |
| 1492 | { |
| 1493 | static_cast<Monitor *>(arg)->MainLoop(); |
| 1494 | return nullptr; |
| 1495 | } |
| 1496 | |
| 1497 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1498 | NativeProcessLinux::LaunchArgs::LaunchArgs(Module *module, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1499 | char const **argv, |
| 1500 | char const **envp, |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1501 | const std::string &stdin_path, |
| 1502 | const std::string &stdout_path, |
| 1503 | const std::string &stderr_path, |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1504 | const char *working_dir, |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 1505 | const ProcessLaunchInfo &launch_info) |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1506 | : m_module(module), |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1507 | m_argv(argv), |
| 1508 | m_envp(envp), |
| 1509 | m_stdin_path(stdin_path), |
| 1510 | m_stdout_path(stdout_path), |
| 1511 | m_stderr_path(stderr_path), |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1512 | m_working_dir(working_dir), |
| 1513 | m_launch_info(launch_info) |
| 1514 | { |
| 1515 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1516 | |
| 1517 | NativeProcessLinux::LaunchArgs::~LaunchArgs() |
| 1518 | { } |
| 1519 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1520 | // ----------------------------------------------------------------------------- |
| 1521 | // Public Static Methods |
| 1522 | // ----------------------------------------------------------------------------- |
| 1523 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 1524 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1525 | NativeProcessLinux::LaunchProcess ( |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 1526 | Module *exe_module, |
| 1527 | ProcessLaunchInfo &launch_info, |
| 1528 | NativeProcessProtocol::NativeDelegate &native_delegate, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1529 | NativeProcessProtocolSP &native_process_sp) |
| 1530 | { |
| 1531 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1532 | |
| 1533 | Error error; |
| 1534 | |
| 1535 | // Verify the working directory is valid if one was specified. |
| 1536 | const char* working_dir = launch_info.GetWorkingDirectory (); |
| 1537 | if (working_dir) |
| 1538 | { |
| 1539 | FileSpec working_dir_fs (working_dir, true); |
| 1540 | if (!working_dir_fs || working_dir_fs.GetFileType () != FileSpec::eFileTypeDirectory) |
| 1541 | { |
| 1542 | error.SetErrorStringWithFormat ("No such file or directory: %s", working_dir); |
| 1543 | return error; |
| 1544 | } |
| 1545 | } |
| 1546 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 1547 | const FileAction *file_action; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1548 | |
| 1549 | // Default of NULL will mean to use existing open file descriptors. |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1550 | std::string stdin_path; |
| 1551 | std::string stdout_path; |
| 1552 | std::string stderr_path; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1553 | |
| 1554 | file_action = launch_info.GetFileActionForFD (STDIN_FILENO); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1555 | if (file_action) |
| 1556 | stdin_path = file_action->GetPath (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1557 | |
| 1558 | file_action = launch_info.GetFileActionForFD (STDOUT_FILENO); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1559 | if (file_action) |
| 1560 | stdout_path = file_action->GetPath (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1561 | |
| 1562 | file_action = launch_info.GetFileActionForFD (STDERR_FILENO); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1563 | if (file_action) |
| 1564 | stderr_path = file_action->GetPath (); |
| 1565 | |
| 1566 | if (log) |
| 1567 | { |
| 1568 | if (!stdin_path.empty ()) |
| 1569 | log->Printf ("NativeProcessLinux::%s setting STDIN to '%s'", __FUNCTION__, stdin_path.c_str ()); |
| 1570 | else |
| 1571 | log->Printf ("NativeProcessLinux::%s leaving STDIN as is", __FUNCTION__); |
| 1572 | |
| 1573 | if (!stdout_path.empty ()) |
| 1574 | log->Printf ("NativeProcessLinux::%s setting STDOUT to '%s'", __FUNCTION__, stdout_path.c_str ()); |
| 1575 | else |
| 1576 | log->Printf ("NativeProcessLinux::%s leaving STDOUT as is", __FUNCTION__); |
| 1577 | |
| 1578 | if (!stderr_path.empty ()) |
| 1579 | log->Printf ("NativeProcessLinux::%s setting STDERR to '%s'", __FUNCTION__, stderr_path.c_str ()); |
| 1580 | else |
| 1581 | log->Printf ("NativeProcessLinux::%s leaving STDERR as is", __FUNCTION__); |
| 1582 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1583 | |
| 1584 | // Create the NativeProcessLinux in launch mode. |
| 1585 | native_process_sp.reset (new NativeProcessLinux ()); |
| 1586 | |
| 1587 | if (log) |
| 1588 | { |
| 1589 | int i = 0; |
| 1590 | for (const char **args = launch_info.GetArguments ().GetConstArgumentVector (); *args; ++args, ++i) |
| 1591 | { |
| 1592 | log->Printf ("NativeProcessLinux::%s arg %d: \"%s\"", __FUNCTION__, i, *args ? *args : "nullptr"); |
| 1593 | ++i; |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | if (!native_process_sp->RegisterNativeDelegate (native_delegate)) |
| 1598 | { |
| 1599 | native_process_sp.reset (); |
| 1600 | error.SetErrorStringWithFormat ("failed to register the native delegate"); |
| 1601 | return error; |
| 1602 | } |
| 1603 | |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 1604 | std::static_pointer_cast<NativeProcessLinux> (native_process_sp)->LaunchInferior ( |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1605 | exe_module, |
| 1606 | launch_info.GetArguments ().GetConstArgumentVector (), |
| 1607 | launch_info.GetEnvironmentEntries ().GetConstArgumentVector (), |
| 1608 | stdin_path, |
| 1609 | stdout_path, |
| 1610 | stderr_path, |
| 1611 | working_dir, |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1612 | launch_info, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1613 | error); |
| 1614 | |
| 1615 | if (error.Fail ()) |
| 1616 | { |
| 1617 | native_process_sp.reset (); |
| 1618 | if (log) |
| 1619 | log->Printf ("NativeProcessLinux::%s failed to launch process: %s", __FUNCTION__, error.AsCString ()); |
| 1620 | return error; |
| 1621 | } |
| 1622 | |
| 1623 | launch_info.SetProcessID (native_process_sp->GetID ()); |
| 1624 | |
| 1625 | return error; |
| 1626 | } |
| 1627 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 1628 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1629 | NativeProcessLinux::AttachToProcess ( |
| 1630 | lldb::pid_t pid, |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 1631 | NativeProcessProtocol::NativeDelegate &native_delegate, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1632 | NativeProcessProtocolSP &native_process_sp) |
| 1633 | { |
| 1634 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1635 | if (log && log->GetMask ().Test (POSIX_LOG_VERBOSE)) |
| 1636 | log->Printf ("NativeProcessLinux::%s(pid = %" PRIi64 ")", __FUNCTION__, pid); |
| 1637 | |
| 1638 | // Grab the current platform architecture. This should be Linux, |
| 1639 | // since this code is only intended to run on a Linux host. |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 1640 | PlatformSP platform_sp (Platform::GetHostPlatform ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1641 | if (!platform_sp) |
| 1642 | return Error("failed to get a valid default platform"); |
| 1643 | |
| 1644 | // Retrieve the architecture for the running process. |
| 1645 | ArchSpec process_arch; |
| 1646 | Error error = ResolveProcessArchitecture (pid, *platform_sp.get (), process_arch); |
| 1647 | if (!error.Success ()) |
| 1648 | return error; |
| 1649 | |
Oleksiy Vyalov | 1339b5e | 2014-11-13 18:22:16 +0000 | [diff] [blame] | 1650 | std::shared_ptr<NativeProcessLinux> native_process_linux_sp (new NativeProcessLinux ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1651 | |
Oleksiy Vyalov | 1339b5e | 2014-11-13 18:22:16 +0000 | [diff] [blame] | 1652 | if (!native_process_linux_sp->RegisterNativeDelegate (native_delegate)) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1653 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1654 | error.SetErrorStringWithFormat ("failed to register the native delegate"); |
| 1655 | return error; |
| 1656 | } |
| 1657 | |
Oleksiy Vyalov | 1339b5e | 2014-11-13 18:22:16 +0000 | [diff] [blame] | 1658 | native_process_linux_sp->AttachToInferior (pid, error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1659 | if (!error.Success ()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1660 | return error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1661 | |
Oleksiy Vyalov | 1339b5e | 2014-11-13 18:22:16 +0000 | [diff] [blame] | 1662 | native_process_sp = native_process_linux_sp; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1663 | return error; |
| 1664 | } |
| 1665 | |
| 1666 | // ----------------------------------------------------------------------------- |
| 1667 | // Public Instance Methods |
| 1668 | // ----------------------------------------------------------------------------- |
| 1669 | |
| 1670 | NativeProcessLinux::NativeProcessLinux () : |
| 1671 | NativeProcessProtocol (LLDB_INVALID_PROCESS_ID), |
| 1672 | m_arch (), |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1673 | m_supports_mem_region (eLazyBoolCalculate), |
| 1674 | m_mem_region_cache (), |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1675 | m_mem_region_cache_mutex (), |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 1676 | m_log_function (GetThreadLoggerFunction()), |
| 1677 | m_tid_map (), |
| 1678 | m_log_event_processing (false) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1679 | { |
| 1680 | } |
| 1681 | |
| 1682 | //------------------------------------------------------------------------------ |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1683 | // NativeProcessLinux spawns a new thread which performs all operations on the inferior process. |
| 1684 | // Refer to Monitor and Operation classes to see why this is necessary. |
| 1685 | //------------------------------------------------------------------------------ |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1686 | void |
| 1687 | NativeProcessLinux::LaunchInferior ( |
| 1688 | Module *module, |
| 1689 | const char *argv[], |
| 1690 | const char *envp[], |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1691 | const std::string &stdin_path, |
| 1692 | const std::string &stdout_path, |
| 1693 | const std::string &stderr_path, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1694 | const char *working_dir, |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 1695 | const ProcessLaunchInfo &launch_info, |
| 1696 | Error &error) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1697 | { |
| 1698 | if (module) |
| 1699 | m_arch = module->GetArchitecture (); |
| 1700 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1701 | SetState (eStateLaunching); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1702 | |
| 1703 | std::unique_ptr<LaunchArgs> args( |
| 1704 | new LaunchArgs( |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1705 | module, argv, envp, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1706 | stdin_path, stdout_path, stderr_path, |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1707 | working_dir, launch_info)); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1708 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1709 | StartMonitorThread ([&] (Error &e) { return Launch(args.get(), e); }, error); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1710 | if (!error.Success ()) |
| 1711 | return; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | void |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 1715 | NativeProcessLinux::AttachToInferior (lldb::pid_t pid, Error &error) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1716 | { |
| 1717 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1718 | if (log) |
| 1719 | log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ")", __FUNCTION__, pid); |
| 1720 | |
| 1721 | // We can use the Host for everything except the ResolveExecutable portion. |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 1722 | PlatformSP platform_sp = Platform::GetHostPlatform (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1723 | if (!platform_sp) |
| 1724 | { |
| 1725 | if (log) |
| 1726 | log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 "): no default platform set", __FUNCTION__, pid); |
| 1727 | error.SetErrorString ("no default platform available"); |
Shawn Best | 50d60be | 2014-11-11 00:28:52 +0000 | [diff] [blame] | 1728 | return; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | // Gather info about the process. |
| 1732 | ProcessInstanceInfo process_info; |
Shawn Best | 50d60be | 2014-11-11 00:28:52 +0000 | [diff] [blame] | 1733 | if (!platform_sp->GetProcessInfo (pid, process_info)) |
| 1734 | { |
| 1735 | if (log) |
| 1736 | log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 "): failed to get process info", __FUNCTION__, pid); |
| 1737 | error.SetErrorString ("failed to get process info"); |
| 1738 | return; |
| 1739 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1740 | |
| 1741 | // Resolve the executable module |
| 1742 | ModuleSP exe_module_sp; |
| 1743 | FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths()); |
Chaoren Lin | e56f6dc | 2015-03-01 04:31:16 +0000 | [diff] [blame] | 1744 | ModuleSpec exe_module_spec(process_info.GetExecutableFile(), process_info.GetArchitecture()); |
Oleksiy Vyalov | 6edef20 | 2014-11-17 22:16:42 +0000 | [diff] [blame] | 1745 | error = platform_sp->ResolveExecutable(exe_module_spec, exe_module_sp, |
Zachary Turner | 13b1826 | 2014-08-20 16:42:51 +0000 | [diff] [blame] | 1746 | executable_search_paths.GetSize() ? &executable_search_paths : NULL); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1747 | if (!error.Success()) |
| 1748 | return; |
| 1749 | |
| 1750 | // Set the architecture to the exe architecture. |
| 1751 | m_arch = exe_module_sp->GetArchitecture(); |
| 1752 | if (log) |
| 1753 | log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ") detected architecture %s", __FUNCTION__, pid, m_arch.GetArchitectureName ()); |
| 1754 | |
| 1755 | m_pid = pid; |
| 1756 | SetState(eStateAttaching); |
| 1757 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1758 | StartMonitorThread ([=] (Error &e) { return Attach(pid, e); }, error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1759 | if (!error.Success ()) |
| 1760 | return; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Oleksiy Vyalov | 8bc34f4 | 2015-02-19 17:58:04 +0000 | [diff] [blame] | 1763 | void |
| 1764 | NativeProcessLinux::Terminate () |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1765 | { |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 1766 | m_monitor_up->Terminate(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1769 | ::pid_t |
| 1770 | NativeProcessLinux::Launch(LaunchArgs *args, Error &error) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1771 | { |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1772 | assert (args && "null args"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1773 | |
| 1774 | const char **argv = args->m_argv; |
| 1775 | const char **envp = args->m_envp; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1776 | const char *working_dir = args->m_working_dir; |
| 1777 | |
| 1778 | lldb_utility::PseudoTerminal terminal; |
| 1779 | const size_t err_len = 1024; |
| 1780 | char err_str[err_len]; |
| 1781 | lldb::pid_t pid; |
| 1782 | NativeThreadProtocolSP thread_sp; |
| 1783 | |
| 1784 | lldb::ThreadSP inferior; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1785 | |
| 1786 | // Propagate the environment if one is not supplied. |
| 1787 | if (envp == NULL || envp[0] == NULL) |
| 1788 | envp = const_cast<const char **>(environ); |
| 1789 | |
| 1790 | if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t> (-1)) |
| 1791 | { |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1792 | error.SetErrorToGenericError(); |
| 1793 | error.SetErrorStringWithFormat("Process fork failed: %s", err_str); |
| 1794 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | // Recognized child exit status codes. |
| 1798 | enum { |
| 1799 | ePtraceFailed = 1, |
| 1800 | eDupStdinFailed, |
| 1801 | eDupStdoutFailed, |
| 1802 | eDupStderrFailed, |
| 1803 | eChdirFailed, |
| 1804 | eExecFailed, |
| 1805 | eSetGidFailed |
| 1806 | }; |
| 1807 | |
| 1808 | // Child process. |
| 1809 | if (pid == 0) |
| 1810 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1811 | // FIXME consider opening a pipe between parent/child and have this forked child |
| 1812 | // 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] | 1813 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1814 | // Start tracing this child that is about to exec. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1815 | PTRACE(PTRACE_TRACEME, 0, nullptr, nullptr, 0, error); |
| 1816 | if (error.Fail()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1817 | exit(ePtraceFailed); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1818 | |
Pavel Labath | 493c3a1 | 2015-02-04 10:36:57 +0000 | [diff] [blame] | 1819 | // terminal has already dupped the tty descriptors to stdin/out/err. |
| 1820 | // This closes original fd from which they were copied (and avoids |
| 1821 | // leaking descriptors to the debugged process. |
| 1822 | terminal.CloseSlaveFileDescriptor(); |
| 1823 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1824 | // Do not inherit setgid powers. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1825 | if (setgid(getgid()) != 0) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1826 | exit(eSetGidFailed); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1827 | |
| 1828 | // Attempt to have our own process group. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1829 | if (setpgid(0, 0) != 0) |
| 1830 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1831 | // FIXME log that this failed. This is common. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1832 | // Don't allow this to prevent an inferior exec. |
| 1833 | } |
| 1834 | |
| 1835 | // Dup file descriptors if needed. |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1836 | if (!args->m_stdin_path.empty ()) |
| 1837 | if (!DupDescriptor(args->m_stdin_path.c_str (), STDIN_FILENO, O_RDONLY)) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1838 | exit(eDupStdinFailed); |
| 1839 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1840 | if (!args->m_stdout_path.empty ()) |
Tamas Berghammer | 14f4476 | 2015-02-25 13:21:45 +0000 | [diff] [blame] | 1841 | if (!DupDescriptor(args->m_stdout_path.c_str (), STDOUT_FILENO, O_WRONLY | O_CREAT | O_TRUNC)) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1842 | exit(eDupStdoutFailed); |
| 1843 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1844 | if (!args->m_stderr_path.empty ()) |
Tamas Berghammer | 14f4476 | 2015-02-25 13:21:45 +0000 | [diff] [blame] | 1845 | if (!DupDescriptor(args->m_stderr_path.c_str (), STDERR_FILENO, O_WRONLY | O_CREAT | O_TRUNC)) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1846 | exit(eDupStderrFailed); |
| 1847 | |
Chaoren Lin | 9cf4f2c | 2015-04-23 18:28:04 +0000 | [diff] [blame] | 1848 | // Close everything besides stdin, stdout, and stderr that has no file |
| 1849 | // action to avoid leaking |
| 1850 | for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd) |
| 1851 | if (!args->m_launch_info.GetFileActionForFD(fd)) |
| 1852 | close(fd); |
| 1853 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1854 | // Change working directory |
| 1855 | if (working_dir != NULL && working_dir[0]) |
| 1856 | if (0 != ::chdir(working_dir)) |
| 1857 | exit(eChdirFailed); |
| 1858 | |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1859 | // Disable ASLR if requested. |
| 1860 | if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR)) |
| 1861 | { |
| 1862 | const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS); |
| 1863 | if (old_personality == -1) |
| 1864 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1865 | // Can't retrieve Linux personality. Cannot disable ASLR. |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1866 | } |
| 1867 | else |
| 1868 | { |
| 1869 | const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality); |
| 1870 | if (new_personality == -1) |
| 1871 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1872 | // Disabling ASLR failed. |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1873 | } |
| 1874 | else |
| 1875 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1876 | // Disabling ASLR succeeded. |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1877 | } |
| 1878 | } |
| 1879 | } |
| 1880 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1881 | // Execute. We should never return... |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1882 | execve(argv[0], |
| 1883 | const_cast<char *const *>(argv), |
| 1884 | const_cast<char *const *>(envp)); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1885 | |
| 1886 | // ...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] | 1887 | exit(eExecFailed); |
| 1888 | } |
| 1889 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1890 | // |
| 1891 | // This is the parent code here. |
| 1892 | // |
| 1893 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1894 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1895 | // Wait for the child process to trap on its call to execve. |
| 1896 | ::pid_t wpid; |
| 1897 | int status; |
| 1898 | if ((wpid = waitpid(pid, &status, 0)) < 0) |
| 1899 | { |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1900 | error.SetErrorToErrno(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1901 | if (log) |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1902 | log->Printf ("NativeProcessLinux::%s waitpid for inferior failed with %s", |
| 1903 | __FUNCTION__, error.AsCString ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1904 | |
| 1905 | // Mark the inferior as invalid. |
| 1906 | // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1907 | SetState (StateType::eStateInvalid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1908 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1909 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1910 | } |
| 1911 | else if (WIFEXITED(status)) |
| 1912 | { |
| 1913 | // open, dup or execve likely failed for some reason. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1914 | error.SetErrorToGenericError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1915 | switch (WEXITSTATUS(status)) |
| 1916 | { |
| 1917 | case ePtraceFailed: |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1918 | error.SetErrorString("Child ptrace failed."); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1919 | break; |
| 1920 | case eDupStdinFailed: |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1921 | error.SetErrorString("Child open stdin failed."); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1922 | break; |
| 1923 | case eDupStdoutFailed: |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1924 | error.SetErrorString("Child open stdout failed."); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1925 | break; |
| 1926 | case eDupStderrFailed: |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1927 | error.SetErrorString("Child open stderr failed."); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1928 | break; |
| 1929 | case eChdirFailed: |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1930 | error.SetErrorString("Child failed to set working directory."); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1931 | break; |
| 1932 | case eExecFailed: |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1933 | error.SetErrorString("Child exec failed."); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1934 | break; |
| 1935 | case eSetGidFailed: |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1936 | error.SetErrorString("Child setgid failed."); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1937 | break; |
| 1938 | default: |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1939 | error.SetErrorString("Child returned unknown exit status."); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1940 | break; |
| 1941 | } |
| 1942 | |
| 1943 | if (log) |
| 1944 | { |
| 1945 | log->Printf ("NativeProcessLinux::%s inferior exited with status %d before issuing a STOP", |
| 1946 | __FUNCTION__, |
| 1947 | WEXITSTATUS(status)); |
| 1948 | } |
| 1949 | |
| 1950 | // Mark the inferior as invalid. |
| 1951 | // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1952 | SetState (StateType::eStateInvalid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1953 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1954 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1955 | } |
Todd Fiala | 202ecd2 | 2014-07-10 04:39:13 +0000 | [diff] [blame] | 1956 | assert(WIFSTOPPED(status) && (wpid == static_cast< ::pid_t> (pid)) && |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1957 | "Could not sync with inferior process."); |
| 1958 | |
| 1959 | if (log) |
| 1960 | log->Printf ("NativeProcessLinux::%s inferior started, now in stopped state", __FUNCTION__); |
| 1961 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1962 | error = SetDefaultPtraceOpts(pid); |
| 1963 | if (error.Fail()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1964 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1965 | if (log) |
| 1966 | log->Printf ("NativeProcessLinux::%s inferior failed to set default ptrace options: %s", |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1967 | __FUNCTION__, error.AsCString ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1968 | |
| 1969 | // Mark the inferior as invalid. |
| 1970 | // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1971 | SetState (StateType::eStateInvalid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1972 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1973 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | // Release the master terminal descriptor and pass it off to the |
| 1977 | // NativeProcessLinux instance. Similarly stash the inferior pid. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1978 | m_terminal_fd = terminal.ReleaseMasterFileDescriptor(); |
| 1979 | m_pid = pid; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1980 | |
| 1981 | // Set the terminal fd to be in non blocking mode (it simplifies the |
| 1982 | // implementation of ProcessLinux::GetSTDOUT to have a non-blocking |
| 1983 | // descriptor to read from). |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1984 | error = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); |
| 1985 | if (error.Fail()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1986 | { |
| 1987 | if (log) |
| 1988 | log->Printf ("NativeProcessLinux::%s inferior EnsureFDFlags failed for ensuring terminal O_NONBLOCK setting: %s", |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1989 | __FUNCTION__, error.AsCString ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1990 | |
| 1991 | // Mark the inferior as invalid. |
| 1992 | // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1993 | SetState (StateType::eStateInvalid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1994 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1995 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | if (log) |
| 1999 | log->Printf ("NativeProcessLinux::%s() adding pid = %" PRIu64, __FUNCTION__, pid); |
| 2000 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2001 | thread_sp = AddThread (pid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2002 | assert (thread_sp && "AddThread() returned a nullptr thread"); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2003 | NotifyThreadCreateStopped (pid); |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2004 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGSTOP); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2005 | |
| 2006 | // Let our process instance know the thread has stopped. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2007 | SetCurrentThreadID (thread_sp->GetID ()); |
| 2008 | SetState (StateType::eStateStopped); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2009 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2010 | if (log) |
| 2011 | { |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2012 | if (error.Success ()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2013 | { |
| 2014 | log->Printf ("NativeProcessLinux::%s inferior launching succeeded", __FUNCTION__); |
| 2015 | } |
| 2016 | else |
| 2017 | { |
| 2018 | log->Printf ("NativeProcessLinux::%s inferior launching failed: %s", |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2019 | __FUNCTION__, error.AsCString ()); |
| 2020 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2021 | } |
| 2022 | } |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2023 | return pid; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2026 | ::pid_t |
| 2027 | NativeProcessLinux::Attach(lldb::pid_t pid, Error &error) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2028 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2029 | lldb::ThreadSP inferior; |
| 2030 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 2031 | |
| 2032 | // Use a map to keep track of the threads which we have attached/need to attach. |
| 2033 | Host::TidMap tids_to_attach; |
| 2034 | if (pid <= 1) |
| 2035 | { |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2036 | error.SetErrorToGenericError(); |
| 2037 | error.SetErrorString("Attaching to process 1 is not allowed."); |
| 2038 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | while (Host::FindProcessThreads(pid, tids_to_attach)) |
| 2042 | { |
| 2043 | for (Host::TidMap::iterator it = tids_to_attach.begin(); |
| 2044 | it != tids_to_attach.end();) |
| 2045 | { |
| 2046 | if (it->second == false) |
| 2047 | { |
| 2048 | lldb::tid_t tid = it->first; |
| 2049 | |
| 2050 | // Attach to the requested process. |
| 2051 | // An attach will cause the thread to stop with a SIGSTOP. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2052 | PTRACE(PTRACE_ATTACH, tid, nullptr, nullptr, 0, error); |
| 2053 | if (error.Fail()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2054 | { |
| 2055 | // No such thread. The thread may have exited. |
| 2056 | // More error handling may be needed. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2057 | if (error.GetError() == ESRCH) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2058 | { |
| 2059 | it = tids_to_attach.erase(it); |
| 2060 | continue; |
| 2061 | } |
| 2062 | else |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2063 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | int status; |
| 2067 | // Need to use __WALL otherwise we receive an error with errno=ECHLD |
| 2068 | // At this point we should have a thread stopped if waitpid succeeds. |
| 2069 | if ((status = waitpid(tid, NULL, __WALL)) < 0) |
| 2070 | { |
| 2071 | // No such thread. The thread may have exited. |
| 2072 | // More error handling may be needed. |
| 2073 | if (errno == ESRCH) |
| 2074 | { |
| 2075 | it = tids_to_attach.erase(it); |
| 2076 | continue; |
| 2077 | } |
| 2078 | else |
| 2079 | { |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2080 | error.SetErrorToErrno(); |
| 2081 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2082 | } |
| 2083 | } |
| 2084 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2085 | error = SetDefaultPtraceOpts(tid); |
| 2086 | if (error.Fail()) |
| 2087 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2088 | |
| 2089 | if (log) |
| 2090 | log->Printf ("NativeProcessLinux::%s() adding tid = %" PRIu64, __FUNCTION__, tid); |
| 2091 | |
| 2092 | it->second = true; |
| 2093 | |
| 2094 | // Create the thread, mark it as stopped. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2095 | NativeThreadProtocolSP thread_sp (AddThread (static_cast<lldb::tid_t> (tid))); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2096 | assert (thread_sp && "AddThread() returned a nullptr"); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2097 | |
| 2098 | // This will notify this is a new thread and tell the system it is stopped. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2099 | NotifyThreadCreateStopped (tid); |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2100 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGSTOP); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2101 | SetCurrentThreadID (thread_sp->GetID ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | // move the loop forward |
| 2105 | ++it; |
| 2106 | } |
| 2107 | } |
| 2108 | |
| 2109 | if (tids_to_attach.size() > 0) |
| 2110 | { |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2111 | m_pid = pid; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2112 | // Let our process instance know the thread has stopped. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2113 | SetState (StateType::eStateStopped); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2114 | } |
| 2115 | else |
| 2116 | { |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2117 | error.SetErrorToGenericError(); |
| 2118 | error.SetErrorString("No such process."); |
| 2119 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2122 | return pid; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 2125 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2126 | NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) |
| 2127 | { |
| 2128 | long ptrace_opts = 0; |
| 2129 | |
| 2130 | // Have the child raise an event on exit. This is used to keep the child in |
| 2131 | // limbo until it is destroyed. |
| 2132 | ptrace_opts |= PTRACE_O_TRACEEXIT; |
| 2133 | |
| 2134 | // Have the tracer trace threads which spawn in the inferior process. |
| 2135 | // TODO: if we want to support tracing the inferiors' child, add the |
| 2136 | // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK) |
| 2137 | ptrace_opts |= PTRACE_O_TRACECLONE; |
| 2138 | |
| 2139 | // Have the tracer notify us before execve returns |
| 2140 | // (needed to disable legacy SIGTRAP generation) |
| 2141 | ptrace_opts |= PTRACE_O_TRACEEXEC; |
| 2142 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 2143 | Error error; |
| 2144 | PTRACE(PTRACE_SETOPTIONS, pid, nullptr, (void*)ptrace_opts, 0, error); |
| 2145 | return error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | static ExitType convert_pid_status_to_exit_type (int status) |
| 2149 | { |
| 2150 | if (WIFEXITED (status)) |
| 2151 | return ExitType::eExitTypeExit; |
| 2152 | else if (WIFSIGNALED (status)) |
| 2153 | return ExitType::eExitTypeSignal; |
| 2154 | else if (WIFSTOPPED (status)) |
| 2155 | return ExitType::eExitTypeStop; |
| 2156 | else |
| 2157 | { |
| 2158 | // We don't know what this is. |
| 2159 | return ExitType::eExitTypeInvalid; |
| 2160 | } |
| 2161 | } |
| 2162 | |
| 2163 | static int convert_pid_status_to_return_code (int status) |
| 2164 | { |
| 2165 | if (WIFEXITED (status)) |
| 2166 | return WEXITSTATUS (status); |
| 2167 | else if (WIFSIGNALED (status)) |
| 2168 | return WTERMSIG (status); |
| 2169 | else if (WIFSTOPPED (status)) |
| 2170 | return WSTOPSIG (status); |
| 2171 | else |
| 2172 | { |
| 2173 | // We don't know what this is. |
| 2174 | return ExitType::eExitTypeInvalid; |
| 2175 | } |
| 2176 | } |
| 2177 | |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2178 | // Handles all waitpid events from the inferior process. |
| 2179 | void |
| 2180 | NativeProcessLinux::MonitorCallback(lldb::pid_t pid, |
Tamas Berghammer | 1e209fc | 2015-03-13 11:36:47 +0000 | [diff] [blame] | 2181 | bool exited, |
| 2182 | int signal, |
| 2183 | int status) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2184 | { |
| 2185 | Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 2186 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2187 | // Certain activities differ based on whether the pid is the tid of the main thread. |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2188 | const bool is_main_thread = (pid == GetID ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2189 | |
| 2190 | // Handle when the thread exits. |
| 2191 | if (exited) |
| 2192 | { |
| 2193 | if (log) |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2194 | log->Printf ("NativeProcessLinux::%s() got exit signal(%d) , tid = %" PRIu64 " (%s main thread)", __FUNCTION__, signal, pid, is_main_thread ? "is" : "is not"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2195 | |
| 2196 | // This is a thread that exited. Ensure we're not tracking it anymore. |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2197 | const bool thread_found = StopTrackingThread (pid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2198 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2199 | // Make sure the thread state coordinator knows about this. |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2200 | NotifyThreadDeath (pid); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2201 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2202 | if (is_main_thread) |
| 2203 | { |
| 2204 | // We only set the exit status and notify the delegate if we haven't already set the process |
| 2205 | // state to an exited state. We normally should have received a SIGTRAP | (PTRACE_EVENT_EXIT << 8) |
| 2206 | // for the main thread. |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2207 | const bool already_notified = (GetState() == StateType::eStateExited) || (GetState () == StateType::eStateCrashed); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2208 | if (!already_notified) |
| 2209 | { |
| 2210 | if (log) |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2211 | 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 (GetState ())); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2212 | // The main thread exited. We're done monitoring. Report to delegate. |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2213 | SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2214 | |
| 2215 | // Notify delegate that our process has exited. |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2216 | SetState (StateType::eStateExited, true); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2217 | } |
| 2218 | else |
| 2219 | { |
| 2220 | if (log) |
| 2221 | log->Printf ("NativeProcessLinux::%s() tid = %" PRIu64 " main thread now exited (%s)", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found"); |
| 2222 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2223 | } |
| 2224 | else |
| 2225 | { |
| 2226 | // Do we want to report to the delegate in this case? I think not. If this was an orderly |
| 2227 | // thread exit, we would already have received the SIGTRAP | (PTRACE_EVENT_EXIT << 8) signal, |
| 2228 | // and we would have done an all-stop then. |
| 2229 | if (log) |
| 2230 | log->Printf ("NativeProcessLinux::%s() tid = %" PRIu64 " handling non-main thread exit (%s)", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2231 | } |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2232 | return; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | // Get details on the signal raised. |
| 2236 | siginfo_t info; |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2237 | const auto err = GetSignalInfo(pid, &info); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 2238 | if (err.Success()) |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2239 | { |
| 2240 | // We have retrieved the signal info. Dispatch appropriately. |
| 2241 | if (info.si_signo == SIGTRAP) |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2242 | MonitorSIGTRAP(&info, pid); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2243 | else |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2244 | MonitorSignal(&info, pid, exited); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2245 | } |
| 2246 | else |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2247 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 2248 | if (err.GetError() == EINVAL) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2249 | { |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2250 | // This is a group stop reception for this tid. |
| 2251 | if (log) |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2252 | log->Printf ("NativeThreadLinux::%s received a group stop for pid %" PRIu64 " tid %" PRIu64, __FUNCTION__, GetID (), pid); |
| 2253 | NotifyThreadStop (pid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2254 | } |
| 2255 | else |
| 2256 | { |
| 2257 | // ptrace(GETSIGINFO) failed (but not due to group-stop). |
| 2258 | |
| 2259 | // A return value of ESRCH means the thread/process is no longer on the system, |
| 2260 | // so it was killed somehow outside of our control. Either way, we can't do anything |
| 2261 | // with it anymore. |
| 2262 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2263 | // Stop tracking the metadata for the thread since it's entirely off the system now. |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2264 | const bool thread_found = StopTrackingThread (pid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2265 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2266 | // Make sure the thread state coordinator knows about this. |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2267 | NotifyThreadDeath (pid); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2268 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2269 | if (log) |
| 2270 | log->Printf ("NativeProcessLinux::%s GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d (%s, %s, %s)", |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 2271 | __FUNCTION__, err.AsCString(), pid, signal, status, err.GetError() == ESRCH ? "thread/process killed" : "unknown reason", is_main_thread ? "is main thread" : "is not main thread", thread_found ? "thread metadata removed" : "thread metadata not found"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2272 | |
| 2273 | if (is_main_thread) |
| 2274 | { |
| 2275 | // Notify the delegate - our process is not available but appears to have been killed outside |
| 2276 | // our control. Is eStateExited the right exit state in this case? |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2277 | SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); |
| 2278 | SetState (StateType::eStateExited, true); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2279 | } |
| 2280 | else |
| 2281 | { |
| 2282 | // This thread was pulled out from underneath us. Anything to do here? Do we want to do an all stop? |
| 2283 | if (log) |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2284 | 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__, GetID (), pid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2285 | } |
| 2286 | } |
| 2287 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | void |
Pavel Labath | 426bdf8 | 2015-04-28 07:51:52 +0000 | [diff] [blame] | 2291 | NativeProcessLinux::WaitForNewThread(::pid_t tid) |
| 2292 | { |
| 2293 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 2294 | |
| 2295 | NativeThreadProtocolSP new_thread_sp = GetThreadByID(tid); |
| 2296 | |
| 2297 | if (new_thread_sp) |
| 2298 | { |
| 2299 | // We are already tracking the thread - we got the event on the new thread (see |
| 2300 | // MonitorSignal) before this one. We are done. |
| 2301 | return; |
| 2302 | } |
| 2303 | |
| 2304 | // The thread is not tracked yet, let's wait for it to appear. |
| 2305 | int status = -1; |
| 2306 | ::pid_t wait_pid; |
| 2307 | do |
| 2308 | { |
| 2309 | if (log) |
| 2310 | log->Printf ("NativeProcessLinux::%s() received thread creation event for tid %" PRIu32 ". tid not tracked yet, waiting for thread to appear...", __FUNCTION__, tid); |
| 2311 | wait_pid = waitpid(tid, &status, __WALL); |
| 2312 | } |
| 2313 | while (wait_pid == -1 && errno == EINTR); |
| 2314 | // Since we are waiting on a specific tid, this must be the creation event. But let's do |
| 2315 | // some checks just in case. |
| 2316 | if (wait_pid != tid) { |
| 2317 | if (log) |
| 2318 | log->Printf ("NativeProcessLinux::%s() waiting for tid %" PRIu32 " failed. Assuming the thread has disappeared in the meantime", __FUNCTION__, tid); |
| 2319 | // The only way I know of this could happen is if the whole process was |
| 2320 | // SIGKILLed in the mean time. In any case, we can't do anything about that now. |
| 2321 | return; |
| 2322 | } |
| 2323 | if (WIFEXITED(status)) |
| 2324 | { |
| 2325 | if (log) |
| 2326 | log->Printf ("NativeProcessLinux::%s() waiting for tid %" PRIu32 " returned an 'exited' event. Not tracking the thread.", __FUNCTION__, tid); |
| 2327 | // Also a very improbable event. |
| 2328 | return; |
| 2329 | } |
| 2330 | |
| 2331 | siginfo_t info; |
| 2332 | Error error = GetSignalInfo(tid, &info); |
| 2333 | if (error.Fail()) |
| 2334 | { |
| 2335 | if (log) |
| 2336 | log->Printf ("NativeProcessLinux::%s() GetSignalInfo for tid %" PRIu32 " failed. Assuming the thread has disappeared in the meantime.", __FUNCTION__, tid); |
| 2337 | return; |
| 2338 | } |
| 2339 | |
| 2340 | if (((info.si_pid != 0) || (info.si_code != SI_USER)) && log) |
| 2341 | { |
| 2342 | // We should be getting a thread creation signal here, but we received something |
| 2343 | // else. There isn't much we can do about it now, so we will just log that. Since the |
| 2344 | // thread is alive and we are receiving events from it, we shall pretend that it was |
| 2345 | // created properly. |
| 2346 | log->Printf ("NativeProcessLinux::%s() GetSignalInfo for tid %" PRIu32 " received unexpected signal with code %d from pid %d.", __FUNCTION__, tid, info.si_code, info.si_pid); |
| 2347 | } |
| 2348 | |
| 2349 | if (log) |
| 2350 | log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 ": tracking new thread tid %" PRIu32, |
| 2351 | __FUNCTION__, GetID (), tid); |
| 2352 | |
| 2353 | new_thread_sp = AddThread(tid); |
| 2354 | std::static_pointer_cast<NativeThreadLinux> (new_thread_sp)->SetRunning (); |
| 2355 | Resume (tid, LLDB_INVALID_SIGNAL_NUMBER); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 2356 | NotifyThreadCreate (tid, false, CoordinatorErrorHandler); |
Pavel Labath | 426bdf8 | 2015-04-28 07:51:52 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
| 2359 | void |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2360 | NativeProcessLinux::MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid) |
| 2361 | { |
| 2362 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 2363 | const bool is_main_thread = (pid == GetID ()); |
| 2364 | |
| 2365 | assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!"); |
| 2366 | if (!info) |
| 2367 | return; |
| 2368 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2369 | Mutex::Locker locker (m_threads_mutex); |
| 2370 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2371 | // See if we can find a thread for this signal. |
| 2372 | NativeThreadProtocolSP thread_sp = GetThreadByID (pid); |
| 2373 | if (!thread_sp) |
| 2374 | { |
| 2375 | if (log) |
| 2376 | log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " no thread found for tid %" PRIu64, __FUNCTION__, GetID (), pid); |
| 2377 | } |
| 2378 | |
| 2379 | switch (info->si_code) |
| 2380 | { |
| 2381 | // TODO: these two cases are required if we want to support tracing of the inferiors' children. We'd need this to debug a monitor. |
| 2382 | // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)): |
| 2383 | // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)): |
| 2384 | |
| 2385 | case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)): |
| 2386 | { |
Pavel Labath | 5fd24c6 | 2015-04-23 09:04:35 +0000 | [diff] [blame] | 2387 | // This is the notification on the parent thread which informs us of new thread |
Pavel Labath | 426bdf8 | 2015-04-28 07:51:52 +0000 | [diff] [blame] | 2388 | // creation. |
| 2389 | // We don't want to do anything with the parent thread so we just resume it. In case we |
| 2390 | // want to implement "break on thread creation" functionality, we would need to stop |
| 2391 | // here. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2392 | |
Pavel Labath | 426bdf8 | 2015-04-28 07:51:52 +0000 | [diff] [blame] | 2393 | unsigned long event_message = 0; |
| 2394 | if (GetEventMessage (pid, &event_message).Fail()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2395 | { |
Pavel Labath | 426bdf8 | 2015-04-28 07:51:52 +0000 | [diff] [blame] | 2396 | if (log) |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2397 | log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " received thread creation event but GetEventMessage failed so we don't know the new tid", __FUNCTION__, pid); |
Pavel Labath | 426bdf8 | 2015-04-28 07:51:52 +0000 | [diff] [blame] | 2398 | } else |
| 2399 | WaitForNewThread(event_message); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2400 | |
Pavel Labath | 5fd24c6 | 2015-04-23 09:04:35 +0000 | [diff] [blame] | 2401 | Resume (pid, LLDB_INVALID_SIGNAL_NUMBER); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2402 | break; |
| 2403 | } |
| 2404 | |
| 2405 | case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2406 | { |
| 2407 | NativeThreadProtocolSP main_thread_sp; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2408 | if (log) |
| 2409 | 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] | 2410 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2411 | // The thread state coordinator needs to reset due to the exec. |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 2412 | ResetForExec (); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2413 | |
| 2414 | // Remove all but the main thread here. Linux fork creates a new process which only copies the main thread. Mutexes are in undefined state. |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2415 | if (log) |
| 2416 | log->Printf ("NativeProcessLinux::%s exec received, stop tracking all but main thread", __FUNCTION__); |
| 2417 | |
| 2418 | for (auto thread_sp : m_threads) |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2419 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2420 | const bool is_main_thread = thread_sp && thread_sp->GetID () == GetID (); |
| 2421 | if (is_main_thread) |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2422 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2423 | main_thread_sp = thread_sp; |
| 2424 | if (log) |
| 2425 | log->Printf ("NativeProcessLinux::%s found main thread with tid %" PRIu64 ", keeping", __FUNCTION__, main_thread_sp->GetID ()); |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2426 | } |
| 2427 | else |
| 2428 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2429 | // Tell thread coordinator this thread is dead. |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2430 | if (log) |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2431 | log->Printf ("NativeProcessLinux::%s discarding non-main-thread tid %" PRIu64 " due to exec", __FUNCTION__, thread_sp->GetID ()); |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2432 | } |
| 2433 | } |
| 2434 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2435 | m_threads.clear (); |
| 2436 | |
| 2437 | if (main_thread_sp) |
| 2438 | { |
| 2439 | m_threads.push_back (main_thread_sp); |
| 2440 | SetCurrentThreadID (main_thread_sp->GetID ()); |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2441 | std::static_pointer_cast<NativeThreadLinux> (main_thread_sp)->SetStoppedByExec (); |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2442 | } |
| 2443 | else |
| 2444 | { |
| 2445 | SetCurrentThreadID (LLDB_INVALID_THREAD_ID); |
| 2446 | if (log) |
| 2447 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 "no main thread found, discarded all threads, we're in a no-thread state!", __FUNCTION__, GetID ()); |
| 2448 | } |
| 2449 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2450 | // Tell coordinator about about the "new" (since exec) stopped main thread. |
| 2451 | const lldb::tid_t main_thread_tid = GetID (); |
| 2452 | NotifyThreadCreateStopped (main_thread_tid); |
| 2453 | |
| 2454 | // NOTE: ideally these next statements would execute at the same time as the coordinator thread create was executed. |
| 2455 | // Consider a handler that can execute when that happens. |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2456 | // Let our delegate know we have just exec'd. |
| 2457 | NotifyDidExec (); |
| 2458 | |
| 2459 | // If we have a main thread, indicate we are stopped. |
| 2460 | 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] | 2461 | |
| 2462 | // Let the process know we're stopped. |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 2463 | StopRunningThreads (pid); |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2464 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2465 | break; |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2466 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2467 | |
| 2468 | case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): |
| 2469 | { |
| 2470 | // The inferior process or one of its threads is about to exit. |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2471 | |
| 2472 | // This thread is currently stopped. It's not actually dead yet, just about to be. |
| 2473 | NotifyThreadStop (pid); |
| 2474 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2475 | unsigned long data = 0; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 2476 | if (GetEventMessage(pid, &data).Fail()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2477 | data = -1; |
| 2478 | |
| 2479 | if (log) |
| 2480 | { |
| 2481 | log->Printf ("NativeProcessLinux::%s() received PTRACE_EVENT_EXIT, data = %lx (WIFEXITED=%s,WIFSIGNALED=%s), pid = %" PRIu64 " (%s)", |
| 2482 | __FUNCTION__, |
| 2483 | data, WIFEXITED (data) ? "true" : "false", WIFSIGNALED (data) ? "true" : "false", |
| 2484 | pid, |
| 2485 | is_main_thread ? "is main thread" : "not main thread"); |
| 2486 | } |
| 2487 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2488 | if (is_main_thread) |
| 2489 | { |
| 2490 | 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] | 2491 | } |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 2492 | |
Chaoren Lin | 9d617ba | 2015-02-03 01:50:54 +0000 | [diff] [blame] | 2493 | const int signo = static_cast<int> (data); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 2494 | RequestThreadResume (pid, |
| 2495 | [=](lldb::tid_t tid_to_resume, bool supress_signal) |
| 2496 | { |
| 2497 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); |
| 2498 | return Resume (tid_to_resume, (supress_signal) ? LLDB_INVALID_SIGNAL_NUMBER : signo); |
| 2499 | }, |
| 2500 | CoordinatorErrorHandler); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2501 | |
| 2502 | break; |
| 2503 | } |
| 2504 | |
| 2505 | case 0: |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 2506 | case TRAP_TRACE: // We receive this on single stepping. |
| 2507 | case TRAP_HWBKPT: // We receive this on watchpoint hit |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2508 | if (thread_sp) |
| 2509 | { |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 2510 | // If a watchpoint was hit, report it |
| 2511 | uint32_t wp_index; |
| 2512 | Error error = thread_sp->GetRegisterContext()->GetWatchpointHitIndex(wp_index); |
| 2513 | if (error.Fail() && log) |
| 2514 | log->Printf("NativeProcessLinux::%s() " |
| 2515 | "received error while checking for watchpoint hits, " |
| 2516 | "pid = %" PRIu64 " error = %s", |
| 2517 | __FUNCTION__, pid, error.AsCString()); |
| 2518 | if (wp_index != LLDB_INVALID_INDEX32) |
| 2519 | { |
| 2520 | MonitorWatchpoint(pid, thread_sp, wp_index); |
| 2521 | break; |
| 2522 | } |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2523 | } |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 2524 | // Otherwise, report step over |
| 2525 | MonitorTrace(pid, thread_sp); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2526 | break; |
| 2527 | |
| 2528 | case SI_KERNEL: |
| 2529 | case TRAP_BRKPT: |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 2530 | MonitorBreakpoint(pid, thread_sp); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2531 | break; |
| 2532 | |
| 2533 | case SIGTRAP: |
| 2534 | case (SIGTRAP | 0x80): |
| 2535 | if (log) |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2536 | log->Printf ("NativeProcessLinux::%s() received unknown SIGTRAP system call stop event, pid %" PRIu64 "tid %" PRIu64 ", resuming", __FUNCTION__, GetID (), pid); |
| 2537 | |
| 2538 | // This thread is currently stopped. |
| 2539 | NotifyThreadStop (pid); |
| 2540 | if (thread_sp) |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2541 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGTRAP); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2542 | |
| 2543 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2544 | // Ignore these signals until we know more about them. |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 2545 | RequestThreadResume (pid, |
| 2546 | [=](lldb::tid_t tid_to_resume, bool supress_signal) |
| 2547 | { |
| 2548 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); |
| 2549 | return Resume (tid_to_resume, LLDB_INVALID_SIGNAL_NUMBER); |
| 2550 | }, |
| 2551 | CoordinatorErrorHandler); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2552 | break; |
| 2553 | |
| 2554 | default: |
| 2555 | assert(false && "Unexpected SIGTRAP code!"); |
| 2556 | if (log) |
| 2557 | 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))); |
| 2558 | break; |
| 2559 | |
| 2560 | } |
| 2561 | } |
| 2562 | |
| 2563 | void |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 2564 | NativeProcessLinux::MonitorTrace(lldb::pid_t pid, NativeThreadProtocolSP thread_sp) |
| 2565 | { |
| 2566 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 2567 | if (log) |
| 2568 | log->Printf("NativeProcessLinux::%s() received trace event, pid = %" PRIu64 " (single stepping)", |
| 2569 | __FUNCTION__, pid); |
| 2570 | |
| 2571 | if (thread_sp) |
| 2572 | std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByTrace(); |
| 2573 | |
| 2574 | // This thread is currently stopped. |
| 2575 | NotifyThreadStop(pid); |
| 2576 | |
| 2577 | // Here we don't have to request the rest of the threads to stop or request a deferred stop. |
| 2578 | // This would have already happened at the time the Resume() with step operation was signaled. |
| 2579 | // At this point, we just need to say we stopped, and the deferred notifcation will fire off |
| 2580 | // once all running threads have checked in as stopped. |
| 2581 | SetCurrentThreadID(pid); |
| 2582 | // Tell the process we have a stop (from software breakpoint). |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 2583 | StopRunningThreads(pid); |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | void |
| 2587 | NativeProcessLinux::MonitorBreakpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp) |
| 2588 | { |
| 2589 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); |
| 2590 | if (log) |
| 2591 | log->Printf("NativeProcessLinux::%s() received breakpoint event, pid = %" PRIu64, |
| 2592 | __FUNCTION__, pid); |
| 2593 | |
| 2594 | // This thread is currently stopped. |
| 2595 | NotifyThreadStop(pid); |
| 2596 | |
| 2597 | // Mark the thread as stopped at breakpoint. |
| 2598 | if (thread_sp) |
| 2599 | { |
| 2600 | std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByBreakpoint(); |
| 2601 | Error error = FixupBreakpointPCAsNeeded(thread_sp); |
| 2602 | if (error.Fail()) |
| 2603 | if (log) |
| 2604 | log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " fixup: %s", |
| 2605 | __FUNCTION__, pid, error.AsCString()); |
Tamas Berghammer | d8c338d | 2015-04-15 09:47:02 +0000 | [diff] [blame] | 2606 | |
| 2607 | auto it = m_threads_stepping_with_breakpoint.find(pid); |
| 2608 | if (it != m_threads_stepping_with_breakpoint.end()) |
| 2609 | { |
| 2610 | Error error = RemoveBreakpoint (it->second); |
| 2611 | if (error.Fail()) |
| 2612 | if (log) |
| 2613 | log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " remove stepping breakpoint: %s", |
| 2614 | __FUNCTION__, pid, error.AsCString()); |
| 2615 | |
| 2616 | m_threads_stepping_with_breakpoint.erase(it); |
| 2617 | std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByTrace(); |
| 2618 | } |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 2619 | } |
| 2620 | else |
| 2621 | if (log) |
| 2622 | log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 ": " |
| 2623 | "warning, cannot process software breakpoint since no thread metadata", |
| 2624 | __FUNCTION__, pid); |
| 2625 | |
| 2626 | |
| 2627 | // We need to tell all other running threads before we notify the delegate about this stop. |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 2628 | StopRunningThreads(pid); |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 2629 | } |
| 2630 | |
| 2631 | void |
| 2632 | NativeProcessLinux::MonitorWatchpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp, uint32_t wp_index) |
| 2633 | { |
| 2634 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_WATCHPOINTS)); |
| 2635 | if (log) |
| 2636 | log->Printf("NativeProcessLinux::%s() received watchpoint event, " |
| 2637 | "pid = %" PRIu64 ", wp_index = %" PRIu32, |
| 2638 | __FUNCTION__, pid, wp_index); |
| 2639 | |
| 2640 | // This thread is currently stopped. |
| 2641 | NotifyThreadStop(pid); |
| 2642 | |
| 2643 | // Mark the thread as stopped at watchpoint. |
| 2644 | // The address is at (lldb::addr_t)info->si_addr if we need it. |
| 2645 | lldbassert(thread_sp && "thread_sp cannot be NULL"); |
| 2646 | std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByWatchpoint(wp_index); |
| 2647 | |
| 2648 | // We need to tell all other running threads before we notify the delegate about this stop. |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 2649 | StopRunningThreads(pid); |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
| 2652 | void |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2653 | NativeProcessLinux::MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited) |
| 2654 | { |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 2655 | assert (info && "null info"); |
| 2656 | if (!info) |
| 2657 | return; |
| 2658 | |
| 2659 | const int signo = info->si_signo; |
| 2660 | const bool is_from_llgs = info->si_pid == getpid (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2661 | |
| 2662 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 2663 | |
| 2664 | // POSIX says that process behaviour is undefined after it ignores a SIGFPE, |
| 2665 | // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a |
| 2666 | // kill(2) or raise(3). Similarly for tgkill(2) on Linux. |
| 2667 | // |
| 2668 | // IOW, user generated signals never generate what we consider to be a |
| 2669 | // "crash". |
| 2670 | // |
| 2671 | // Similarly, ACK signals generated by this monitor. |
| 2672 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2673 | Mutex::Locker locker (m_threads_mutex); |
| 2674 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2675 | // See if we can find a thread for this signal. |
| 2676 | NativeThreadProtocolSP thread_sp = GetThreadByID (pid); |
| 2677 | if (!thread_sp) |
| 2678 | { |
| 2679 | if (log) |
| 2680 | log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " no thread found for tid %" PRIu64, __FUNCTION__, GetID (), pid); |
| 2681 | } |
| 2682 | |
| 2683 | // Handle the signal. |
| 2684 | if (info->si_code == SI_TKILL || info->si_code == SI_USER) |
| 2685 | { |
| 2686 | if (log) |
| 2687 | log->Printf ("NativeProcessLinux::%s() received signal %s (%d) with code %s, (siginfo pid = %d (%s), waitpid pid = %" PRIu64 ")", |
| 2688 | __FUNCTION__, |
| 2689 | GetUnixSignals ().GetSignalAsCString (signo), |
| 2690 | signo, |
| 2691 | (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"), |
| 2692 | info->si_pid, |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 2693 | is_from_llgs ? "from llgs" : "not from llgs", |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2694 | pid); |
Todd Fiala | 58a2f66 | 2014-08-12 17:02:07 +0000 | [diff] [blame] | 2695 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2696 | |
Todd Fiala | 58a2f66 | 2014-08-12 17:02:07 +0000 | [diff] [blame] | 2697 | // Check for new thread notification. |
| 2698 | if ((info->si_pid == 0) && (info->si_code == SI_USER)) |
| 2699 | { |
Pavel Labath | 426bdf8 | 2015-04-28 07:51:52 +0000 | [diff] [blame] | 2700 | // A new thread creation is being signaled. This is one of two parts that come in |
| 2701 | // a non-deterministic order. This code handles the case where the new thread event comes |
| 2702 | // before the event on the parent thread. For the opposite case see code in |
| 2703 | // MonitorSIGTRAP. |
Todd Fiala | 58a2f66 | 2014-08-12 17:02:07 +0000 | [diff] [blame] | 2704 | if (log) |
| 2705 | log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 " tid %" PRIu64 ": new thread notification", |
| 2706 | __FUNCTION__, GetID (), pid); |
| 2707 | |
Pavel Labath | 5fd24c6 | 2015-04-23 09:04:35 +0000 | [diff] [blame] | 2708 | thread_sp = AddThread(pid); |
| 2709 | assert (thread_sp.get() && "failed to create the tracking data for newly created inferior thread"); |
| 2710 | // We can now resume the newly created thread. |
| 2711 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); |
| 2712 | Resume (pid, LLDB_INVALID_SIGNAL_NUMBER); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 2713 | NotifyThreadCreate (pid, false, CoordinatorErrorHandler); |
Todd Fiala | 58a2f66 | 2014-08-12 17:02:07 +0000 | [diff] [blame] | 2714 | // Done handling. |
| 2715 | return; |
| 2716 | } |
| 2717 | |
| 2718 | // Check for thread stop notification. |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 2719 | if (is_from_llgs && (info->si_code == SI_TKILL) && (signo == SIGSTOP)) |
Todd Fiala | 58a2f66 | 2014-08-12 17:02:07 +0000 | [diff] [blame] | 2720 | { |
| 2721 | // This is a tgkill()-based stop. |
| 2722 | if (thread_sp) |
| 2723 | { |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2724 | if (log) |
| 2725 | log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 ", thread stopped", |
| 2726 | __FUNCTION__, |
| 2727 | GetID (), |
| 2728 | pid); |
| 2729 | |
Chaoren Lin | aab5863 | 2015-02-03 01:50:57 +0000 | [diff] [blame] | 2730 | // Check that we're not already marked with a stop reason. |
| 2731 | // Note this thread really shouldn't already be marked as stopped - if we were, that would imply that |
| 2732 | // the kernel signaled us with the thread stopping which we handled and marked as stopped, |
| 2733 | // and that, without an intervening resume, we received another stop. It is more likely |
| 2734 | // that we are missing the marking of a run state somewhere if we find that the thread was |
| 2735 | // marked as stopped. |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2736 | std::shared_ptr<NativeThreadLinux> linux_thread_sp = std::static_pointer_cast<NativeThreadLinux> (thread_sp); |
| 2737 | assert (linux_thread_sp && "linux_thread_sp is null!"); |
Chaoren Lin | aab5863 | 2015-02-03 01:50:57 +0000 | [diff] [blame] | 2738 | |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2739 | const StateType thread_state = linux_thread_sp->GetState (); |
Chaoren Lin | aab5863 | 2015-02-03 01:50:57 +0000 | [diff] [blame] | 2740 | if (!StateIsStoppedState (thread_state, false)) |
| 2741 | { |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 2742 | // An inferior thread has stopped because of a SIGSTOP we have sent it. |
| 2743 | // Generally, these are not important stops and we don't want to report them as |
| 2744 | // they are just used to stop other threads when one thread (the one with the |
| 2745 | // *real* stop reason) hits a breakpoint (watchpoint, etc...). However, in the |
| 2746 | // case of an asynchronous Interrupt(), this *is* the real stop reason, so we |
| 2747 | // leave the signal intact if this is the thread that was chosen as the |
| 2748 | // triggering thread. |
| 2749 | if (m_pending_notification_up && m_pending_notification_up->triggering_tid == pid) |
| 2750 | linux_thread_sp->SetStoppedBySignal(SIGSTOP); |
| 2751 | else |
| 2752 | linux_thread_sp->SetStoppedBySignal(0); |
| 2753 | |
Chaoren Lin | aab5863 | 2015-02-03 01:50:57 +0000 | [diff] [blame] | 2754 | SetCurrentThreadID (thread_sp->GetID ()); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 2755 | NotifyThreadStop (thread_sp->GetID (), true, CoordinatorErrorHandler); |
Chaoren Lin | aab5863 | 2015-02-03 01:50:57 +0000 | [diff] [blame] | 2756 | } |
| 2757 | else |
| 2758 | { |
| 2759 | if (log) |
| 2760 | { |
| 2761 | // Retrieve the signal name if the thread was stopped by a signal. |
| 2762 | int stop_signo = 0; |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2763 | const bool stopped_by_signal = linux_thread_sp->IsStopped (&stop_signo); |
Chaoren Lin | aab5863 | 2015-02-03 01:50:57 +0000 | [diff] [blame] | 2764 | const char *signal_name = stopped_by_signal ? GetUnixSignals ().GetSignalAsCString (stop_signo) : "<not stopped by signal>"; |
| 2765 | if (!signal_name) |
| 2766 | signal_name = "<no-signal-name>"; |
| 2767 | |
| 2768 | 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", |
| 2769 | __FUNCTION__, |
| 2770 | GetID (), |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2771 | linux_thread_sp->GetID (), |
Chaoren Lin | aab5863 | 2015-02-03 01:50:57 +0000 | [diff] [blame] | 2772 | StateAsCString (thread_state), |
| 2773 | stop_signo, |
| 2774 | signal_name); |
| 2775 | } |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2776 | // Tell the thread state coordinator about the stop. |
| 2777 | NotifyThreadStop (thread_sp->GetID ()); |
Chaoren Lin | aab5863 | 2015-02-03 01:50:57 +0000 | [diff] [blame] | 2778 | } |
Todd Fiala | 58a2f66 | 2014-08-12 17:02:07 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
| 2781 | // Done handling. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2782 | return; |
| 2783 | } |
| 2784 | |
| 2785 | if (log) |
| 2786 | log->Printf ("NativeProcessLinux::%s() received signal %s", __FUNCTION__, GetUnixSignals ().GetSignalAsCString (signo)); |
| 2787 | |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2788 | // This thread is stopped. |
| 2789 | NotifyThreadStop (pid); |
| 2790 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2791 | switch (signo) |
| 2792 | { |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 2793 | case SIGSTOP: |
| 2794 | { |
| 2795 | if (log) |
| 2796 | { |
| 2797 | if (is_from_llgs) |
| 2798 | log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " received SIGSTOP from llgs, most likely an interrupt", __FUNCTION__, GetID (), pid); |
| 2799 | else |
| 2800 | log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " received SIGSTOP from outside of debugger", __FUNCTION__, GetID (), pid); |
| 2801 | } |
| 2802 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2803 | // Resume this thread to get the group-stop mechanism to fire off the true group stops. |
| 2804 | // This thread will get stopped again as part of the group-stop completion. |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 2805 | RequestThreadResume (pid, |
| 2806 | [=](lldb::tid_t tid_to_resume, bool supress_signal) |
| 2807 | { |
| 2808 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); |
| 2809 | // Pass this signal number on to the inferior to handle. |
| 2810 | return Resume (tid_to_resume, (supress_signal) ? LLDB_INVALID_SIGNAL_NUMBER : signo); |
| 2811 | }, |
| 2812 | CoordinatorErrorHandler); |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 2813 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2814 | break; |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2815 | case SIGSEGV: |
| 2816 | case SIGILL: |
| 2817 | case SIGFPE: |
| 2818 | case SIGBUS: |
| 2819 | if (thread_sp) |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2820 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetCrashedWithException (*info); |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2821 | break; |
| 2822 | default: |
| 2823 | // This is just a pre-signal-delivery notification of the incoming signal. |
| 2824 | if (thread_sp) |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2825 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (signo); |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2826 | |
| 2827 | break; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2828 | } |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2829 | |
| 2830 | // Send a stop to the debugger after we get all other threads to stop. |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 2831 | StopRunningThreads (pid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2832 | } |
| 2833 | |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 2834 | namespace { |
| 2835 | |
| 2836 | struct EmulatorBaton |
| 2837 | { |
| 2838 | NativeProcessLinux* m_process; |
| 2839 | NativeRegisterContext* m_reg_context; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 2840 | |
Pavel Labath | 6648fcc | 2015-04-27 09:21:14 +0000 | [diff] [blame] | 2841 | // eRegisterKindDWARF -> RegsiterValue |
| 2842 | std::unordered_map<uint32_t, RegisterValue> m_register_values; |
| 2843 | |
| 2844 | EmulatorBaton(NativeProcessLinux* process, NativeRegisterContext* reg_context) : |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 2845 | m_process(process), m_reg_context(reg_context) {} |
| 2846 | }; |
| 2847 | |
| 2848 | } // anonymous namespace |
| 2849 | |
| 2850 | static size_t |
| 2851 | ReadMemoryCallback (EmulateInstruction *instruction, |
| 2852 | void *baton, |
| 2853 | const EmulateInstruction::Context &context, |
| 2854 | lldb::addr_t addr, |
| 2855 | void *dst, |
| 2856 | size_t length) |
| 2857 | { |
| 2858 | EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); |
| 2859 | |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 2860 | size_t bytes_read; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 2861 | emulator_baton->m_process->ReadMemory(addr, dst, length, bytes_read); |
| 2862 | return bytes_read; |
| 2863 | } |
| 2864 | |
| 2865 | static bool |
| 2866 | ReadRegisterCallback (EmulateInstruction *instruction, |
| 2867 | void *baton, |
| 2868 | const RegisterInfo *reg_info, |
| 2869 | RegisterValue ®_value) |
| 2870 | { |
| 2871 | EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); |
| 2872 | |
Pavel Labath | 6648fcc | 2015-04-27 09:21:14 +0000 | [diff] [blame] | 2873 | auto it = emulator_baton->m_register_values.find(reg_info->kinds[eRegisterKindDWARF]); |
| 2874 | if (it != emulator_baton->m_register_values.end()) |
| 2875 | { |
| 2876 | reg_value = it->second; |
| 2877 | return true; |
| 2878 | } |
| 2879 | |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 2880 | // The emulator only fill in the dwarf regsiter numbers (and in some case |
| 2881 | // the generic register numbers). Get the full register info from the |
| 2882 | // register context based on the dwarf register numbers. |
| 2883 | const RegisterInfo* full_reg_info = emulator_baton->m_reg_context->GetRegisterInfo( |
| 2884 | eRegisterKindDWARF, reg_info->kinds[eRegisterKindDWARF]); |
| 2885 | |
| 2886 | Error error = emulator_baton->m_reg_context->ReadRegister(full_reg_info, reg_value); |
Pavel Labath | 6648fcc | 2015-04-27 09:21:14 +0000 | [diff] [blame] | 2887 | if (error.Success()) |
Pavel Labath | 6648fcc | 2015-04-27 09:21:14 +0000 | [diff] [blame] | 2888 | return true; |
Mohit K. Bhakkad | cdc22a8 | 2015-05-07 05:56:27 +0000 | [diff] [blame^] | 2889 | |
Pavel Labath | 6648fcc | 2015-04-27 09:21:14 +0000 | [diff] [blame] | 2890 | return false; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
| 2893 | static bool |
| 2894 | WriteRegisterCallback (EmulateInstruction *instruction, |
| 2895 | void *baton, |
| 2896 | const EmulateInstruction::Context &context, |
| 2897 | const RegisterInfo *reg_info, |
| 2898 | const RegisterValue ®_value) |
| 2899 | { |
| 2900 | EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); |
Pavel Labath | 6648fcc | 2015-04-27 09:21:14 +0000 | [diff] [blame] | 2901 | emulator_baton->m_register_values[reg_info->kinds[eRegisterKindDWARF]] = reg_value; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 2902 | return true; |
| 2903 | } |
| 2904 | |
| 2905 | static size_t |
| 2906 | WriteMemoryCallback (EmulateInstruction *instruction, |
| 2907 | void *baton, |
| 2908 | const EmulateInstruction::Context &context, |
| 2909 | lldb::addr_t addr, |
| 2910 | const void *dst, |
| 2911 | size_t length) |
| 2912 | { |
| 2913 | return length; |
| 2914 | } |
| 2915 | |
| 2916 | static lldb::addr_t |
| 2917 | ReadFlags (NativeRegisterContext* regsiter_context) |
| 2918 | { |
| 2919 | const RegisterInfo* flags_info = regsiter_context->GetRegisterInfo( |
| 2920 | eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); |
| 2921 | return regsiter_context->ReadRegisterAsUnsigned(flags_info, LLDB_INVALID_ADDRESS); |
| 2922 | } |
| 2923 | |
| 2924 | Error |
| 2925 | NativeProcessLinux::SetupSoftwareSingleStepping(NativeThreadProtocolSP thread_sp) |
| 2926 | { |
| 2927 | Error error; |
| 2928 | NativeRegisterContextSP register_context_sp = thread_sp->GetRegisterContext(); |
| 2929 | |
| 2930 | std::unique_ptr<EmulateInstruction> emulator_ap( |
| 2931 | EmulateInstruction::FindPlugin(m_arch, eInstructionTypePCModifying, nullptr)); |
| 2932 | |
| 2933 | if (emulator_ap == nullptr) |
| 2934 | return Error("Instruction emulator not found!"); |
| 2935 | |
| 2936 | EmulatorBaton baton(this, register_context_sp.get()); |
| 2937 | emulator_ap->SetBaton(&baton); |
| 2938 | emulator_ap->SetReadMemCallback(&ReadMemoryCallback); |
| 2939 | emulator_ap->SetReadRegCallback(&ReadRegisterCallback); |
| 2940 | emulator_ap->SetWriteMemCallback(&WriteMemoryCallback); |
| 2941 | emulator_ap->SetWriteRegCallback(&WriteRegisterCallback); |
| 2942 | |
| 2943 | if (!emulator_ap->ReadInstruction()) |
| 2944 | return Error("Read instruction failed!"); |
| 2945 | |
Pavel Labath | 6648fcc | 2015-04-27 09:21:14 +0000 | [diff] [blame] | 2946 | bool emulation_result = emulator_ap->EvaluateInstruction(eEmulateInstructionOptionAutoAdvancePC); |
| 2947 | |
| 2948 | const RegisterInfo* reg_info_pc = register_context_sp->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); |
| 2949 | const RegisterInfo* reg_info_flags = register_context_sp->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); |
| 2950 | |
| 2951 | auto pc_it = baton.m_register_values.find(reg_info_pc->kinds[eRegisterKindDWARF]); |
| 2952 | auto flags_it = baton.m_register_values.find(reg_info_flags->kinds[eRegisterKindDWARF]); |
| 2953 | |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 2954 | lldb::addr_t next_pc; |
| 2955 | lldb::addr_t next_flags; |
Pavel Labath | 6648fcc | 2015-04-27 09:21:14 +0000 | [diff] [blame] | 2956 | if (emulation_result) |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 2957 | { |
Pavel Labath | 6648fcc | 2015-04-27 09:21:14 +0000 | [diff] [blame] | 2958 | assert(pc_it != baton.m_register_values.end() && "Emulation was successfull but PC wasn't updated"); |
| 2959 | next_pc = pc_it->second.GetAsUInt64(); |
| 2960 | |
| 2961 | if (flags_it != baton.m_register_values.end()) |
| 2962 | next_flags = flags_it->second.GetAsUInt64(); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 2963 | else |
| 2964 | next_flags = ReadFlags (register_context_sp.get()); |
| 2965 | } |
Pavel Labath | 6648fcc | 2015-04-27 09:21:14 +0000 | [diff] [blame] | 2966 | else if (pc_it == baton.m_register_values.end()) |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 2967 | { |
| 2968 | // Emulate instruction failed and it haven't changed PC. Advance PC |
| 2969 | // with the size of the current opcode because the emulation of all |
| 2970 | // PC modifying instruction should be successful. The failure most |
| 2971 | // likely caused by a not supported instruction which don't modify PC. |
| 2972 | next_pc = register_context_sp->GetPC() + emulator_ap->GetOpcode().GetByteSize(); |
| 2973 | next_flags = ReadFlags (register_context_sp.get()); |
| 2974 | } |
| 2975 | else |
| 2976 | { |
| 2977 | // The instruction emulation failed after it modified the PC. It is an |
| 2978 | // unknown error where we can't continue because the next instruction is |
| 2979 | // modifying the PC but we don't know how. |
| 2980 | return Error ("Instruction emulation failed unexpectedly."); |
| 2981 | } |
| 2982 | |
| 2983 | if (m_arch.GetMachine() == llvm::Triple::arm) |
| 2984 | { |
| 2985 | if (next_flags & 0x20) |
| 2986 | { |
| 2987 | // Thumb mode |
| 2988 | error = SetSoftwareBreakpoint(next_pc, 2); |
| 2989 | } |
| 2990 | else |
| 2991 | { |
| 2992 | // Arm mode |
| 2993 | error = SetSoftwareBreakpoint(next_pc, 4); |
| 2994 | } |
| 2995 | } |
Mohit K. Bhakkad | cdc22a8 | 2015-05-07 05:56:27 +0000 | [diff] [blame^] | 2996 | else if (m_arch.GetMachine() == llvm::Triple::mips64 |
| 2997 | || m_arch.GetMachine() == llvm::Triple::mips64el) |
| 2998 | error = SetSoftwareBreakpoint(next_pc, 4); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 2999 | else |
| 3000 | { |
| 3001 | // No size hint is given for the next breakpoint |
| 3002 | error = SetSoftwareBreakpoint(next_pc, 0); |
| 3003 | } |
| 3004 | |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 3005 | if (error.Fail()) |
| 3006 | return error; |
| 3007 | |
| 3008 | m_threads_stepping_with_breakpoint.insert({thread_sp->GetID(), next_pc}); |
| 3009 | |
| 3010 | return Error(); |
| 3011 | } |
| 3012 | |
| 3013 | bool |
| 3014 | NativeProcessLinux::SupportHardwareSingleStepping() const |
| 3015 | { |
Mohit K. Bhakkad | cdc22a8 | 2015-05-07 05:56:27 +0000 | [diff] [blame^] | 3016 | if (m_arch.GetMachine() == llvm::Triple::arm |
| 3017 | || m_arch.GetMachine() == llvm::Triple::mips64 || m_arch.GetMachine() == llvm::Triple::mips64el) |
| 3018 | return false; |
| 3019 | return true; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3022 | Error |
| 3023 | NativeProcessLinux::Resume (const ResumeActionList &resume_actions) |
| 3024 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3025 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); |
| 3026 | if (log) |
| 3027 | log->Printf ("NativeProcessLinux::%s called: pid %" PRIu64, __FUNCTION__, GetID ()); |
| 3028 | |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 3029 | lldb::tid_t deferred_signal_tid = LLDB_INVALID_THREAD_ID; |
| 3030 | lldb::tid_t deferred_signal_skip_tid = LLDB_INVALID_THREAD_ID; |
Chaoren Lin | ae29d39 | 2015-02-03 01:50:46 +0000 | [diff] [blame] | 3031 | int deferred_signo = 0; |
| 3032 | NativeThreadProtocolSP deferred_signal_thread_sp; |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 3033 | bool stepping = false; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 3034 | bool software_single_step = !SupportHardwareSingleStepping(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3035 | |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 3036 | Monitor::ScopedOperationLock monitor_lock(*m_monitor_up); |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3037 | Mutex::Locker locker (m_threads_mutex); |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 3038 | |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 3039 | if (software_single_step) |
| 3040 | { |
| 3041 | for (auto thread_sp : m_threads) |
| 3042 | { |
| 3043 | assert (thread_sp && "thread list should not contain NULL threads"); |
| 3044 | |
| 3045 | const ResumeAction *const action = resume_actions.GetActionForThread (thread_sp->GetID (), true); |
| 3046 | if (action == nullptr) |
| 3047 | continue; |
| 3048 | |
| 3049 | if (action->state == eStateStepping) |
| 3050 | { |
| 3051 | Error error = SetupSoftwareSingleStepping(thread_sp); |
| 3052 | if (error.Fail()) |
| 3053 | return error; |
| 3054 | } |
| 3055 | } |
| 3056 | } |
| 3057 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3058 | for (auto thread_sp : m_threads) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3059 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3060 | assert (thread_sp && "thread list should not contain NULL threads"); |
| 3061 | |
| 3062 | const ResumeAction *const action = resume_actions.GetActionForThread (thread_sp->GetID (), true); |
| 3063 | |
| 3064 | if (action == nullptr) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3065 | { |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3066 | if (log) |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3067 | log->Printf ("NativeProcessLinux::%s no action specified for pid %" PRIu64 " tid %" PRIu64, |
| 3068 | __FUNCTION__, GetID (), thread_sp->GetID ()); |
| 3069 | continue; |
| 3070 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3071 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3072 | if (log) |
| 3073 | { |
| 3074 | log->Printf ("NativeProcessLinux::%s processing resume action state %s for pid %" PRIu64 " tid %" PRIu64, |
| 3075 | __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); |
| 3076 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3077 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3078 | switch (action->state) |
| 3079 | { |
| 3080 | case eStateRunning: |
| 3081 | { |
| 3082 | // Run the thread, possibly feeding it the signal. |
| 3083 | const int signo = action->signal; |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 3084 | RequestThreadResumeAsNeeded (thread_sp->GetID (), |
| 3085 | [=](lldb::tid_t tid_to_resume, bool supress_signal) |
| 3086 | { |
| 3087 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); |
| 3088 | // Pass this signal number on to the inferior to handle. |
| 3089 | const auto resume_result = Resume (tid_to_resume, (signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER); |
| 3090 | if (resume_result.Success()) |
| 3091 | SetState(eStateRunning, true); |
| 3092 | return resume_result; |
| 3093 | }, |
| 3094 | CoordinatorErrorHandler); |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3095 | break; |
| 3096 | } |
| 3097 | |
| 3098 | case eStateStepping: |
| 3099 | { |
| 3100 | // Request the step. |
| 3101 | const int signo = action->signal; |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 3102 | RequestThreadResume (thread_sp->GetID (), |
| 3103 | [=](lldb::tid_t tid_to_step, bool supress_signal) |
| 3104 | { |
| 3105 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStepping (); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 3106 | |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 3107 | Error step_result; |
| 3108 | if (software_single_step) |
| 3109 | step_result = Resume (tid_to_step, (signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER); |
| 3110 | else |
| 3111 | step_result = SingleStep (tid_to_step,(signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 3112 | |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 3113 | assert (step_result.Success() && "SingleStep() failed"); |
| 3114 | if (step_result.Success()) |
| 3115 | SetState(eStateStepping, true); |
| 3116 | return step_result; |
| 3117 | }, |
| 3118 | CoordinatorErrorHandler); |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3119 | stepping = true; |
| 3120 | break; |
| 3121 | } |
| 3122 | |
| 3123 | case eStateSuspended: |
| 3124 | case eStateStopped: |
| 3125 | // if we haven't chosen a deferred signal tid yet, use this one. |
| 3126 | if (deferred_signal_tid == LLDB_INVALID_THREAD_ID) |
Chaoren Lin | ae29d39 | 2015-02-03 01:50:46 +0000 | [diff] [blame] | 3127 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3128 | deferred_signal_tid = thread_sp->GetID (); |
| 3129 | deferred_signal_thread_sp = thread_sp; |
| 3130 | deferred_signo = SIGSTOP; |
Chaoren Lin | ae29d39 | 2015-02-03 01:50:46 +0000 | [diff] [blame] | 3131 | } |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3132 | break; |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3133 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3134 | default: |
| 3135 | return Error ("NativeProcessLinux::%s (): unexpected state %s specified for pid %" PRIu64 ", tid %" PRIu64, |
| 3136 | __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3137 | } |
| 3138 | } |
| 3139 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3140 | // If we had any thread stopping, then do a deferred notification of the chosen stop thread id and signal |
| 3141 | // after all other running threads have stopped. |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 3142 | // If there is a stepping thread involved we'll be eventually stopped by SIGTRAP trace signal. |
| 3143 | if (deferred_signal_tid != LLDB_INVALID_THREAD_ID && !stepping) |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 3144 | StopRunningThreadsWithSkipTID(deferred_signal_tid, deferred_signal_skip_tid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3145 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3146 | return Error(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
| 3149 | Error |
| 3150 | NativeProcessLinux::Halt () |
| 3151 | { |
| 3152 | Error error; |
| 3153 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3154 | if (kill (GetID (), SIGSTOP) != 0) |
| 3155 | error.SetErrorToErrno (); |
| 3156 | |
| 3157 | return error; |
| 3158 | } |
| 3159 | |
| 3160 | Error |
| 3161 | NativeProcessLinux::Detach () |
| 3162 | { |
| 3163 | Error error; |
| 3164 | |
| 3165 | // Tell ptrace to detach from the process. |
| 3166 | if (GetID () != LLDB_INVALID_PROCESS_ID) |
| 3167 | error = Detach (GetID ()); |
| 3168 | |
| 3169 | // Stop monitoring the inferior. |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 3170 | m_monitor_up->Terminate(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3171 | |
| 3172 | // No error. |
| 3173 | return error; |
| 3174 | } |
| 3175 | |
| 3176 | Error |
| 3177 | NativeProcessLinux::Signal (int signo) |
| 3178 | { |
| 3179 | Error error; |
| 3180 | |
| 3181 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3182 | if (log) |
| 3183 | log->Printf ("NativeProcessLinux::%s: sending signal %d (%s) to pid %" PRIu64, |
| 3184 | __FUNCTION__, signo, GetUnixSignals ().GetSignalAsCString (signo), GetID ()); |
| 3185 | |
| 3186 | if (kill(GetID(), signo)) |
| 3187 | error.SetErrorToErrno(); |
| 3188 | |
| 3189 | return error; |
| 3190 | } |
| 3191 | |
| 3192 | Error |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 3193 | NativeProcessLinux::Interrupt () |
| 3194 | { |
| 3195 | // Pick a running thread (or if none, a not-dead stopped thread) as |
| 3196 | // the chosen thread that will be the stop-reason thread. |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 3197 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3198 | |
| 3199 | NativeThreadProtocolSP running_thread_sp; |
| 3200 | NativeThreadProtocolSP stopped_thread_sp; |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3201 | |
| 3202 | if (log) |
| 3203 | log->Printf ("NativeProcessLinux::%s selecting running thread for interrupt target", __FUNCTION__); |
| 3204 | |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 3205 | Monitor::ScopedOperationLock monitor_lock(*m_monitor_up); |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3206 | Mutex::Locker locker (m_threads_mutex); |
| 3207 | |
| 3208 | for (auto thread_sp : m_threads) |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 3209 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3210 | // The thread shouldn't be null but lets just cover that here. |
| 3211 | if (!thread_sp) |
| 3212 | continue; |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 3213 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3214 | // If we have a running or stepping thread, we'll call that the |
| 3215 | // target of the interrupt. |
| 3216 | const auto thread_state = thread_sp->GetState (); |
| 3217 | if (thread_state == eStateRunning || |
| 3218 | thread_state == eStateStepping) |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 3219 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3220 | running_thread_sp = thread_sp; |
| 3221 | break; |
| 3222 | } |
| 3223 | else if (!stopped_thread_sp && StateIsStoppedState (thread_state, true)) |
| 3224 | { |
| 3225 | // Remember the first non-dead stopped thread. We'll use that as a backup if there are no running threads. |
| 3226 | stopped_thread_sp = thread_sp; |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 3227 | } |
| 3228 | } |
| 3229 | |
| 3230 | if (!running_thread_sp && !stopped_thread_sp) |
| 3231 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3232 | Error error("found no running/stepping or live stopped threads as target for interrupt"); |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 3233 | if (log) |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 3234 | log->Printf ("NativeProcessLinux::%s skipping due to error: %s", __FUNCTION__, error.AsCString ()); |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3235 | |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 3236 | return error; |
| 3237 | } |
| 3238 | |
| 3239 | NativeThreadProtocolSP deferred_signal_thread_sp = running_thread_sp ? running_thread_sp : stopped_thread_sp; |
| 3240 | |
| 3241 | if (log) |
| 3242 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " %s tid %" PRIu64 " chosen for interrupt target", |
| 3243 | __FUNCTION__, |
| 3244 | GetID (), |
| 3245 | running_thread_sp ? "running" : "stopped", |
| 3246 | deferred_signal_thread_sp->GetID ()); |
| 3247 | |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 3248 | StopRunningThreads(deferred_signal_thread_sp->GetID()); |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 3249 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3250 | return Error(); |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 3251 | } |
| 3252 | |
| 3253 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3254 | NativeProcessLinux::Kill () |
| 3255 | { |
| 3256 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3257 | if (log) |
| 3258 | log->Printf ("NativeProcessLinux::%s called for PID %" PRIu64, __FUNCTION__, GetID ()); |
| 3259 | |
| 3260 | Error error; |
| 3261 | |
| 3262 | switch (m_state) |
| 3263 | { |
| 3264 | case StateType::eStateInvalid: |
| 3265 | case StateType::eStateExited: |
| 3266 | case StateType::eStateCrashed: |
| 3267 | case StateType::eStateDetached: |
| 3268 | case StateType::eStateUnloaded: |
| 3269 | // Nothing to do - the process is already dead. |
| 3270 | if (log) |
| 3271 | log->Printf ("NativeProcessLinux::%s ignored for PID %" PRIu64 " due to current state: %s", __FUNCTION__, GetID (), StateAsCString (m_state)); |
| 3272 | return error; |
| 3273 | |
| 3274 | case StateType::eStateConnected: |
| 3275 | case StateType::eStateAttaching: |
| 3276 | case StateType::eStateLaunching: |
| 3277 | case StateType::eStateStopped: |
| 3278 | case StateType::eStateRunning: |
| 3279 | case StateType::eStateStepping: |
| 3280 | case StateType::eStateSuspended: |
| 3281 | // We can try to kill a process in these states. |
| 3282 | break; |
| 3283 | } |
| 3284 | |
| 3285 | if (kill (GetID (), SIGKILL) != 0) |
| 3286 | { |
| 3287 | error.SetErrorToErrno (); |
| 3288 | return error; |
| 3289 | } |
| 3290 | |
| 3291 | return error; |
| 3292 | } |
| 3293 | |
| 3294 | static Error |
| 3295 | ParseMemoryRegionInfoFromProcMapsLine (const std::string &maps_line, MemoryRegionInfo &memory_region_info) |
| 3296 | { |
| 3297 | memory_region_info.Clear(); |
| 3298 | |
| 3299 | StringExtractor line_extractor (maps_line.c_str ()); |
| 3300 | |
| 3301 | // Format: {address_start_hex}-{address_end_hex} perms offset dev inode pathname |
| 3302 | // perms: rwxp (letter is present if set, '-' if not, final character is p=private, s=shared). |
| 3303 | |
| 3304 | // Parse out the starting address |
| 3305 | lldb::addr_t start_address = line_extractor.GetHexMaxU64 (false, 0); |
| 3306 | |
| 3307 | // Parse out hyphen separating start and end address from range. |
| 3308 | if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != '-')) |
| 3309 | return Error ("malformed /proc/{pid}/maps entry, missing dash between address range"); |
| 3310 | |
| 3311 | // Parse out the ending address |
| 3312 | lldb::addr_t end_address = line_extractor.GetHexMaxU64 (false, start_address); |
| 3313 | |
| 3314 | // Parse out the space after the address. |
| 3315 | if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != ' ')) |
| 3316 | return Error ("malformed /proc/{pid}/maps entry, missing space after range"); |
| 3317 | |
| 3318 | // Save the range. |
| 3319 | memory_region_info.GetRange ().SetRangeBase (start_address); |
| 3320 | memory_region_info.GetRange ().SetRangeEnd (end_address); |
| 3321 | |
| 3322 | // Parse out each permission entry. |
| 3323 | if (line_extractor.GetBytesLeft () < 4) |
| 3324 | return Error ("malformed /proc/{pid}/maps entry, missing some portion of permissions"); |
| 3325 | |
| 3326 | // Handle read permission. |
| 3327 | const char read_perm_char = line_extractor.GetChar (); |
| 3328 | if (read_perm_char == 'r') |
| 3329 | memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eYes); |
| 3330 | else |
| 3331 | { |
| 3332 | assert ( (read_perm_char == '-') && "unexpected /proc/{pid}/maps read permission char" ); |
| 3333 | memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); |
| 3334 | } |
| 3335 | |
| 3336 | // Handle write permission. |
| 3337 | const char write_perm_char = line_extractor.GetChar (); |
| 3338 | if (write_perm_char == 'w') |
| 3339 | memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eYes); |
| 3340 | else |
| 3341 | { |
| 3342 | assert ( (write_perm_char == '-') && "unexpected /proc/{pid}/maps write permission char" ); |
| 3343 | memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); |
| 3344 | } |
| 3345 | |
| 3346 | // Handle execute permission. |
| 3347 | const char exec_perm_char = line_extractor.GetChar (); |
| 3348 | if (exec_perm_char == 'x') |
| 3349 | memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eYes); |
| 3350 | else |
| 3351 | { |
| 3352 | assert ( (exec_perm_char == '-') && "unexpected /proc/{pid}/maps exec permission char" ); |
| 3353 | memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); |
| 3354 | } |
| 3355 | |
| 3356 | return Error (); |
| 3357 | } |
| 3358 | |
| 3359 | Error |
| 3360 | NativeProcessLinux::GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) |
| 3361 | { |
| 3362 | // FIXME review that the final memory region returned extends to the end of the virtual address space, |
| 3363 | // with no perms if it is not mapped. |
| 3364 | |
| 3365 | // Use an approach that reads memory regions from /proc/{pid}/maps. |
| 3366 | // Assume proc maps entries are in ascending order. |
| 3367 | // FIXME assert if we find differently. |
| 3368 | Mutex::Locker locker (m_mem_region_cache_mutex); |
| 3369 | |
| 3370 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3371 | Error error; |
| 3372 | |
| 3373 | if (m_supports_mem_region == LazyBool::eLazyBoolNo) |
| 3374 | { |
| 3375 | // We're done. |
| 3376 | error.SetErrorString ("unsupported"); |
| 3377 | return error; |
| 3378 | } |
| 3379 | |
| 3380 | // If our cache is empty, pull the latest. There should always be at least one memory region |
| 3381 | // if memory region handling is supported. |
| 3382 | if (m_mem_region_cache.empty ()) |
| 3383 | { |
| 3384 | error = ProcFileReader::ProcessLineByLine (GetID (), "maps", |
| 3385 | [&] (const std::string &line) -> bool |
| 3386 | { |
| 3387 | MemoryRegionInfo info; |
| 3388 | const Error parse_error = ParseMemoryRegionInfoFromProcMapsLine (line, info); |
| 3389 | if (parse_error.Success ()) |
| 3390 | { |
| 3391 | m_mem_region_cache.push_back (info); |
| 3392 | return true; |
| 3393 | } |
| 3394 | else |
| 3395 | { |
| 3396 | if (log) |
| 3397 | log->Printf ("NativeProcessLinux::%s failed to parse proc maps line '%s': %s", __FUNCTION__, line.c_str (), error.AsCString ()); |
| 3398 | return false; |
| 3399 | } |
| 3400 | }); |
| 3401 | |
| 3402 | // If we had an error, we'll mark unsupported. |
| 3403 | if (error.Fail ()) |
| 3404 | { |
| 3405 | m_supports_mem_region = LazyBool::eLazyBoolNo; |
| 3406 | return error; |
| 3407 | } |
| 3408 | else if (m_mem_region_cache.empty ()) |
| 3409 | { |
| 3410 | // No entries after attempting to read them. This shouldn't happen if /proc/{pid}/maps |
| 3411 | // is supported. Assume we don't support map entries via procfs. |
| 3412 | if (log) |
| 3413 | log->Printf ("NativeProcessLinux::%s failed to find any procfs maps entries, assuming no support for memory region metadata retrieval", __FUNCTION__); |
| 3414 | m_supports_mem_region = LazyBool::eLazyBoolNo; |
| 3415 | error.SetErrorString ("not supported"); |
| 3416 | return error; |
| 3417 | } |
| 3418 | |
| 3419 | if (log) |
| 3420 | log->Printf ("NativeProcessLinux::%s read %" PRIu64 " memory region entries from /proc/%" PRIu64 "/maps", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ()), GetID ()); |
| 3421 | |
| 3422 | // We support memory retrieval, remember that. |
| 3423 | m_supports_mem_region = LazyBool::eLazyBoolYes; |
| 3424 | } |
| 3425 | else |
| 3426 | { |
| 3427 | if (log) |
| 3428 | log->Printf ("NativeProcessLinux::%s reusing %" PRIu64 " cached memory region entries", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); |
| 3429 | } |
| 3430 | |
| 3431 | lldb::addr_t prev_base_address = 0; |
| 3432 | |
| 3433 | // FIXME start by finding the last region that is <= target address using binary search. Data is sorted. |
| 3434 | // There can be a ton of regions on pthreads apps with lots of threads. |
| 3435 | for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end (); ++it) |
| 3436 | { |
| 3437 | MemoryRegionInfo &proc_entry_info = *it; |
| 3438 | |
| 3439 | // Sanity check assumption that /proc/{pid}/maps entries are ascending. |
| 3440 | assert ((proc_entry_info.GetRange ().GetRangeBase () >= prev_base_address) && "descending /proc/pid/maps entries detected, unexpected"); |
| 3441 | prev_base_address = proc_entry_info.GetRange ().GetRangeBase (); |
| 3442 | |
| 3443 | // If the target address comes before this entry, indicate distance to next region. |
| 3444 | if (load_addr < proc_entry_info.GetRange ().GetRangeBase ()) |
| 3445 | { |
| 3446 | range_info.GetRange ().SetRangeBase (load_addr); |
| 3447 | range_info.GetRange ().SetByteSize (proc_entry_info.GetRange ().GetRangeBase () - load_addr); |
| 3448 | range_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); |
| 3449 | range_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); |
| 3450 | range_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); |
| 3451 | |
| 3452 | return error; |
| 3453 | } |
| 3454 | else if (proc_entry_info.GetRange ().Contains (load_addr)) |
| 3455 | { |
| 3456 | // The target address is within the memory region we're processing here. |
| 3457 | range_info = proc_entry_info; |
| 3458 | return error; |
| 3459 | } |
| 3460 | |
| 3461 | // The target memory address comes somewhere after the region we just parsed. |
| 3462 | } |
| 3463 | |
| 3464 | // If we made it here, we didn't find an entry that contained the given address. |
| 3465 | error.SetErrorString ("address comes after final region"); |
| 3466 | |
| 3467 | if (log) |
| 3468 | log->Printf ("NativeProcessLinux::%s failed to find map entry for address 0x%" PRIx64 ": %s", __FUNCTION__, load_addr, error.AsCString ()); |
| 3469 | |
| 3470 | return error; |
| 3471 | } |
| 3472 | |
| 3473 | void |
| 3474 | NativeProcessLinux::DoStopIDBumped (uint32_t newBumpId) |
| 3475 | { |
| 3476 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3477 | if (log) |
| 3478 | log->Printf ("NativeProcessLinux::%s(newBumpId=%" PRIu32 ") called", __FUNCTION__, newBumpId); |
| 3479 | |
| 3480 | { |
| 3481 | Mutex::Locker locker (m_mem_region_cache_mutex); |
| 3482 | if (log) |
| 3483 | log->Printf ("NativeProcessLinux::%s clearing %" PRIu64 " entries from the cache", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); |
| 3484 | m_mem_region_cache.clear (); |
| 3485 | } |
| 3486 | } |
| 3487 | |
| 3488 | Error |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 3489 | NativeProcessLinux::AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3490 | { |
| 3491 | // FIXME implementing this requires the equivalent of |
| 3492 | // InferiorCallPOSIX::InferiorCallMmap, which depends on |
| 3493 | // functional ThreadPlans working with Native*Protocol. |
| 3494 | #if 1 |
| 3495 | return Error ("not implemented yet"); |
| 3496 | #else |
| 3497 | addr = LLDB_INVALID_ADDRESS; |
| 3498 | |
| 3499 | unsigned prot = 0; |
| 3500 | if (permissions & lldb::ePermissionsReadable) |
| 3501 | prot |= eMmapProtRead; |
| 3502 | if (permissions & lldb::ePermissionsWritable) |
| 3503 | prot |= eMmapProtWrite; |
| 3504 | if (permissions & lldb::ePermissionsExecutable) |
| 3505 | prot |= eMmapProtExec; |
| 3506 | |
| 3507 | // TODO implement this directly in NativeProcessLinux |
| 3508 | // (and lift to NativeProcessPOSIX if/when that class is |
| 3509 | // refactored out). |
| 3510 | if (InferiorCallMmap(this, addr, 0, size, prot, |
| 3511 | eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { |
| 3512 | m_addr_to_mmap_size[addr] = size; |
| 3513 | return Error (); |
| 3514 | } else { |
| 3515 | addr = LLDB_INVALID_ADDRESS; |
| 3516 | return Error("unable to allocate %" PRIu64 " bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions)); |
| 3517 | } |
| 3518 | #endif |
| 3519 | } |
| 3520 | |
| 3521 | Error |
| 3522 | NativeProcessLinux::DeallocateMemory (lldb::addr_t addr) |
| 3523 | { |
| 3524 | // FIXME see comments in AllocateMemory - required lower-level |
| 3525 | // bits not in place yet (ThreadPlans) |
| 3526 | return Error ("not implemented"); |
| 3527 | } |
| 3528 | |
| 3529 | lldb::addr_t |
| 3530 | NativeProcessLinux::GetSharedLibraryInfoAddress () |
| 3531 | { |
| 3532 | #if 1 |
| 3533 | // punt on this for now |
| 3534 | return LLDB_INVALID_ADDRESS; |
| 3535 | #else |
| 3536 | // Return the image info address for the exe module |
| 3537 | #if 1 |
| 3538 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3539 | |
| 3540 | ModuleSP module_sp; |
| 3541 | Error error = GetExeModuleSP (module_sp); |
| 3542 | if (error.Fail ()) |
| 3543 | { |
| 3544 | if (log) |
| 3545 | log->Warning ("NativeProcessLinux::%s failed to retrieve exe module: %s", __FUNCTION__, error.AsCString ()); |
| 3546 | return LLDB_INVALID_ADDRESS; |
| 3547 | } |
| 3548 | |
| 3549 | if (module_sp == nullptr) |
| 3550 | { |
| 3551 | if (log) |
| 3552 | log->Warning ("NativeProcessLinux::%s exe module returned was NULL", __FUNCTION__); |
| 3553 | return LLDB_INVALID_ADDRESS; |
| 3554 | } |
| 3555 | |
| 3556 | ObjectFileSP object_file_sp = module_sp->GetObjectFile (); |
| 3557 | if (object_file_sp == nullptr) |
| 3558 | { |
| 3559 | if (log) |
| 3560 | log->Warning ("NativeProcessLinux::%s exe module returned a NULL object file", __FUNCTION__); |
| 3561 | return LLDB_INVALID_ADDRESS; |
| 3562 | } |
| 3563 | |
| 3564 | return obj_file_sp->GetImageInfoAddress(); |
| 3565 | #else |
| 3566 | Target *target = &GetTarget(); |
| 3567 | ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile(); |
| 3568 | Address addr = obj_file->GetImageInfoAddress(target); |
| 3569 | |
| 3570 | if (addr.IsValid()) |
| 3571 | return addr.GetLoadAddress(target); |
| 3572 | return LLDB_INVALID_ADDRESS; |
| 3573 | #endif |
| 3574 | #endif // punt on this for now |
| 3575 | } |
| 3576 | |
| 3577 | size_t |
| 3578 | NativeProcessLinux::UpdateThreads () |
| 3579 | { |
| 3580 | // The NativeProcessLinux monitoring threads are always up to date |
| 3581 | // with respect to thread state and they keep the thread list |
| 3582 | // populated properly. All this method needs to do is return the |
| 3583 | // thread count. |
| 3584 | Mutex::Locker locker (m_threads_mutex); |
| 3585 | return m_threads.size (); |
| 3586 | } |
| 3587 | |
| 3588 | bool |
| 3589 | NativeProcessLinux::GetArchitecture (ArchSpec &arch) const |
| 3590 | { |
| 3591 | arch = m_arch; |
| 3592 | return true; |
| 3593 | } |
| 3594 | |
| 3595 | Error |
Tamas Berghammer | 63c8be9 | 2015-04-15 09:38:48 +0000 | [diff] [blame] | 3596 | NativeProcessLinux::GetSoftwareBreakpointPCOffset (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3597 | { |
| 3598 | // FIXME put this behind a breakpoint protocol class that can be |
| 3599 | // set per architecture. Need ARM, MIPS support here. |
Todd Fiala | 2afc596 | 2014-08-21 16:42:31 +0000 | [diff] [blame] | 3600 | static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3601 | static const uint8_t g_i386_opcode [] = { 0xCC }; |
Mohit K. Bhakkad | e8659b5 | 2015-04-23 06:36:20 +0000 | [diff] [blame] | 3602 | static const uint8_t g_mips64_opcode[] = { 0x00, 0x00, 0x00, 0x0d }; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3603 | |
| 3604 | switch (m_arch.GetMachine ()) |
| 3605 | { |
Todd Fiala | 2afc596 | 2014-08-21 16:42:31 +0000 | [diff] [blame] | 3606 | case llvm::Triple::aarch64: |
| 3607 | actual_opcode_size = static_cast<uint32_t> (sizeof(g_aarch64_opcode)); |
| 3608 | return Error (); |
| 3609 | |
Tamas Berghammer | 63c8be9 | 2015-04-15 09:38:48 +0000 | [diff] [blame] | 3610 | case llvm::Triple::arm: |
| 3611 | actual_opcode_size = 0; // On arm the PC don't get updated for breakpoint hits |
| 3612 | return Error (); |
| 3613 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3614 | case llvm::Triple::x86: |
| 3615 | case llvm::Triple::x86_64: |
| 3616 | actual_opcode_size = static_cast<uint32_t> (sizeof(g_i386_opcode)); |
| 3617 | return Error (); |
| 3618 | |
Mohit K. Bhakkad | e8659b5 | 2015-04-23 06:36:20 +0000 | [diff] [blame] | 3619 | case llvm::Triple::mips64: |
| 3620 | case llvm::Triple::mips64el: |
| 3621 | actual_opcode_size = static_cast<uint32_t> (sizeof(g_mips64_opcode)); |
| 3622 | return Error (); |
| 3623 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3624 | default: |
| 3625 | assert(false && "CPU type not supported!"); |
| 3626 | return Error ("CPU type not supported"); |
| 3627 | } |
| 3628 | } |
| 3629 | |
| 3630 | Error |
| 3631 | NativeProcessLinux::SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) |
| 3632 | { |
| 3633 | if (hardware) |
| 3634 | return Error ("NativeProcessLinux does not support hardware breakpoints"); |
| 3635 | else |
| 3636 | return SetSoftwareBreakpoint (addr, size); |
| 3637 | } |
| 3638 | |
| 3639 | Error |
Tamas Berghammer | 63c8be9 | 2015-04-15 09:38:48 +0000 | [diff] [blame] | 3640 | NativeProcessLinux::GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, |
| 3641 | size_t &actual_opcode_size, |
| 3642 | const uint8_t *&trap_opcode_bytes) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3643 | { |
Tamas Berghammer | 63c8be9 | 2015-04-15 09:38:48 +0000 | [diff] [blame] | 3644 | // FIXME put this behind a breakpoint protocol class that can be set per |
| 3645 | // architecture. Need MIPS support here. |
Todd Fiala | 2afc596 | 2014-08-21 16:42:31 +0000 | [diff] [blame] | 3646 | static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; |
Tamas Berghammer | 63c8be9 | 2015-04-15 09:38:48 +0000 | [diff] [blame] | 3647 | // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the |
| 3648 | // linux kernel does otherwise. |
| 3649 | static const uint8_t g_arm_breakpoint_opcode[] = { 0xf0, 0x01, 0xf0, 0xe7 }; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3650 | static const uint8_t g_i386_opcode [] = { 0xCC }; |
Mohit K. Bhakkad | 3df471c | 2015-03-17 11:43:56 +0000 | [diff] [blame] | 3651 | static const uint8_t g_mips64_opcode[] = { 0x00, 0x00, 0x00, 0x0d }; |
Mohit K. Bhakkad | 2c2acf9 | 2015-04-09 07:12:15 +0000 | [diff] [blame] | 3652 | static const uint8_t g_mips64el_opcode[] = { 0x0d, 0x00, 0x00, 0x00 }; |
Tamas Berghammer | 63c8be9 | 2015-04-15 09:38:48 +0000 | [diff] [blame] | 3653 | static const uint8_t g_thumb_breakpoint_opcode[] = { 0x01, 0xde }; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3654 | |
| 3655 | switch (m_arch.GetMachine ()) |
| 3656 | { |
Todd Fiala | 2afc596 | 2014-08-21 16:42:31 +0000 | [diff] [blame] | 3657 | case llvm::Triple::aarch64: |
| 3658 | trap_opcode_bytes = g_aarch64_opcode; |
| 3659 | actual_opcode_size = sizeof(g_aarch64_opcode); |
| 3660 | return Error (); |
| 3661 | |
Tamas Berghammer | 63c8be9 | 2015-04-15 09:38:48 +0000 | [diff] [blame] | 3662 | case llvm::Triple::arm: |
| 3663 | switch (trap_opcode_size_hint) |
| 3664 | { |
| 3665 | case 2: |
| 3666 | trap_opcode_bytes = g_thumb_breakpoint_opcode; |
| 3667 | actual_opcode_size = sizeof(g_thumb_breakpoint_opcode); |
| 3668 | return Error (); |
| 3669 | case 4: |
| 3670 | trap_opcode_bytes = g_arm_breakpoint_opcode; |
| 3671 | actual_opcode_size = sizeof(g_arm_breakpoint_opcode); |
| 3672 | return Error (); |
| 3673 | default: |
| 3674 | assert(false && "Unrecognised trap opcode size hint!"); |
| 3675 | return Error ("Unrecognised trap opcode size hint!"); |
| 3676 | } |
| 3677 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3678 | case llvm::Triple::x86: |
| 3679 | case llvm::Triple::x86_64: |
| 3680 | trap_opcode_bytes = g_i386_opcode; |
| 3681 | actual_opcode_size = sizeof(g_i386_opcode); |
| 3682 | return Error (); |
| 3683 | |
Mohit K. Bhakkad | 3df471c | 2015-03-17 11:43:56 +0000 | [diff] [blame] | 3684 | case llvm::Triple::mips64: |
Mohit K. Bhakkad | 3df471c | 2015-03-17 11:43:56 +0000 | [diff] [blame] | 3685 | trap_opcode_bytes = g_mips64_opcode; |
| 3686 | actual_opcode_size = sizeof(g_mips64_opcode); |
| 3687 | return Error (); |
| 3688 | |
Mohit K. Bhakkad | 2c2acf9 | 2015-04-09 07:12:15 +0000 | [diff] [blame] | 3689 | case llvm::Triple::mips64el: |
| 3690 | trap_opcode_bytes = g_mips64el_opcode; |
| 3691 | actual_opcode_size = sizeof(g_mips64el_opcode); |
| 3692 | return Error (); |
| 3693 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3694 | default: |
| 3695 | assert(false && "CPU type not supported!"); |
| 3696 | return Error ("CPU type not supported"); |
| 3697 | } |
| 3698 | } |
| 3699 | |
| 3700 | #if 0 |
| 3701 | ProcessMessage::CrashReason |
| 3702 | NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info) |
| 3703 | { |
| 3704 | ProcessMessage::CrashReason reason; |
| 3705 | assert(info->si_signo == SIGSEGV); |
| 3706 | |
| 3707 | reason = ProcessMessage::eInvalidCrashReason; |
| 3708 | |
| 3709 | switch (info->si_code) |
| 3710 | { |
| 3711 | default: |
| 3712 | assert(false && "unexpected si_code for SIGSEGV"); |
| 3713 | break; |
| 3714 | case SI_KERNEL: |
| 3715 | // Linux will occasionally send spurious SI_KERNEL codes. |
| 3716 | // (this is poorly documented in sigaction) |
| 3717 | // One way to get this is via unaligned SIMD loads. |
| 3718 | reason = ProcessMessage::eInvalidAddress; // for lack of anything better |
| 3719 | break; |
| 3720 | case SEGV_MAPERR: |
| 3721 | reason = ProcessMessage::eInvalidAddress; |
| 3722 | break; |
| 3723 | case SEGV_ACCERR: |
| 3724 | reason = ProcessMessage::ePrivilegedAddress; |
| 3725 | break; |
| 3726 | } |
| 3727 | |
| 3728 | return reason; |
| 3729 | } |
| 3730 | #endif |
| 3731 | |
| 3732 | |
| 3733 | #if 0 |
| 3734 | ProcessMessage::CrashReason |
| 3735 | NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info) |
| 3736 | { |
| 3737 | ProcessMessage::CrashReason reason; |
| 3738 | assert(info->si_signo == SIGILL); |
| 3739 | |
| 3740 | reason = ProcessMessage::eInvalidCrashReason; |
| 3741 | |
| 3742 | switch (info->si_code) |
| 3743 | { |
| 3744 | default: |
| 3745 | assert(false && "unexpected si_code for SIGILL"); |
| 3746 | break; |
| 3747 | case ILL_ILLOPC: |
| 3748 | reason = ProcessMessage::eIllegalOpcode; |
| 3749 | break; |
| 3750 | case ILL_ILLOPN: |
| 3751 | reason = ProcessMessage::eIllegalOperand; |
| 3752 | break; |
| 3753 | case ILL_ILLADR: |
| 3754 | reason = ProcessMessage::eIllegalAddressingMode; |
| 3755 | break; |
| 3756 | case ILL_ILLTRP: |
| 3757 | reason = ProcessMessage::eIllegalTrap; |
| 3758 | break; |
| 3759 | case ILL_PRVOPC: |
| 3760 | reason = ProcessMessage::ePrivilegedOpcode; |
| 3761 | break; |
| 3762 | case ILL_PRVREG: |
| 3763 | reason = ProcessMessage::ePrivilegedRegister; |
| 3764 | break; |
| 3765 | case ILL_COPROC: |
| 3766 | reason = ProcessMessage::eCoprocessorError; |
| 3767 | break; |
| 3768 | case ILL_BADSTK: |
| 3769 | reason = ProcessMessage::eInternalStackError; |
| 3770 | break; |
| 3771 | } |
| 3772 | |
| 3773 | return reason; |
| 3774 | } |
| 3775 | #endif |
| 3776 | |
| 3777 | #if 0 |
| 3778 | ProcessMessage::CrashReason |
| 3779 | NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info) |
| 3780 | { |
| 3781 | ProcessMessage::CrashReason reason; |
| 3782 | assert(info->si_signo == SIGFPE); |
| 3783 | |
| 3784 | reason = ProcessMessage::eInvalidCrashReason; |
| 3785 | |
| 3786 | switch (info->si_code) |
| 3787 | { |
| 3788 | default: |
| 3789 | assert(false && "unexpected si_code for SIGFPE"); |
| 3790 | break; |
| 3791 | case FPE_INTDIV: |
| 3792 | reason = ProcessMessage::eIntegerDivideByZero; |
| 3793 | break; |
| 3794 | case FPE_INTOVF: |
| 3795 | reason = ProcessMessage::eIntegerOverflow; |
| 3796 | break; |
| 3797 | case FPE_FLTDIV: |
| 3798 | reason = ProcessMessage::eFloatDivideByZero; |
| 3799 | break; |
| 3800 | case FPE_FLTOVF: |
| 3801 | reason = ProcessMessage::eFloatOverflow; |
| 3802 | break; |
| 3803 | case FPE_FLTUND: |
| 3804 | reason = ProcessMessage::eFloatUnderflow; |
| 3805 | break; |
| 3806 | case FPE_FLTRES: |
| 3807 | reason = ProcessMessage::eFloatInexactResult; |
| 3808 | break; |
| 3809 | case FPE_FLTINV: |
| 3810 | reason = ProcessMessage::eFloatInvalidOperation; |
| 3811 | break; |
| 3812 | case FPE_FLTSUB: |
| 3813 | reason = ProcessMessage::eFloatSubscriptRange; |
| 3814 | break; |
| 3815 | } |
| 3816 | |
| 3817 | return reason; |
| 3818 | } |
| 3819 | #endif |
| 3820 | |
| 3821 | #if 0 |
| 3822 | ProcessMessage::CrashReason |
| 3823 | NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info) |
| 3824 | { |
| 3825 | ProcessMessage::CrashReason reason; |
| 3826 | assert(info->si_signo == SIGBUS); |
| 3827 | |
| 3828 | reason = ProcessMessage::eInvalidCrashReason; |
| 3829 | |
| 3830 | switch (info->si_code) |
| 3831 | { |
| 3832 | default: |
| 3833 | assert(false && "unexpected si_code for SIGBUS"); |
| 3834 | break; |
| 3835 | case BUS_ADRALN: |
| 3836 | reason = ProcessMessage::eIllegalAlignment; |
| 3837 | break; |
| 3838 | case BUS_ADRERR: |
| 3839 | reason = ProcessMessage::eIllegalAddress; |
| 3840 | break; |
| 3841 | case BUS_OBJERR: |
| 3842 | reason = ProcessMessage::eHardwareError; |
| 3843 | break; |
| 3844 | } |
| 3845 | |
| 3846 | return reason; |
| 3847 | } |
| 3848 | #endif |
| 3849 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3850 | Error |
Pavel Labath | 45f5cb3 | 2015-05-05 15:05:50 +0000 | [diff] [blame] | 3851 | NativeProcessLinux::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) |
| 3852 | { |
| 3853 | // The base SetWatchpoint will end up executing monitor operations. Let's lock the monitor |
| 3854 | // for it. |
| 3855 | Monitor::ScopedOperationLock monitor_lock(*m_monitor_up); |
| 3856 | return NativeProcessProtocol::SetWatchpoint(addr, size, watch_flags, hardware); |
| 3857 | } |
| 3858 | |
| 3859 | Error |
| 3860 | NativeProcessLinux::RemoveWatchpoint (lldb::addr_t addr) |
| 3861 | { |
| 3862 | // The base RemoveWatchpoint will end up executing monitor operations. Let's lock the monitor |
| 3863 | // for it. |
| 3864 | Monitor::ScopedOperationLock monitor_lock(*m_monitor_up); |
| 3865 | return NativeProcessProtocol::RemoveWatchpoint(addr); |
| 3866 | } |
| 3867 | |
| 3868 | Error |
Chaoren Lin | 26438d2 | 2015-05-05 17:50:53 +0000 | [diff] [blame] | 3869 | NativeProcessLinux::ReadMemory (lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3870 | { |
| 3871 | ReadOperation op(addr, buf, size, bytes_read); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3872 | m_monitor_up->DoOperation(&op); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3873 | return op.GetError (); |
| 3874 | } |
| 3875 | |
| 3876 | Error |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 3877 | NativeProcessLinux::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) |
| 3878 | { |
| 3879 | Error error = ReadMemory(addr, buf, size, bytes_read); |
| 3880 | if (error.Fail()) return error; |
| 3881 | return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); |
| 3882 | } |
| 3883 | |
| 3884 | Error |
| 3885 | NativeProcessLinux::WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3886 | { |
| 3887 | WriteOperation op(addr, buf, size, bytes_written); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3888 | m_monitor_up->DoOperation(&op); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3889 | return op.GetError (); |
| 3890 | } |
| 3891 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3892 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3893 | NativeProcessLinux::ReadRegisterValue(lldb::tid_t tid, uint32_t offset, const char* reg_name, |
Tamas Berghammer | 1e209fc | 2015-03-13 11:36:47 +0000 | [diff] [blame] | 3894 | uint32_t size, RegisterValue &value) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3895 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3896 | ReadRegOperation op(tid, offset, reg_name, value); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3897 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3898 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3899 | } |
| 3900 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3901 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3902 | NativeProcessLinux::WriteRegisterValue(lldb::tid_t tid, unsigned offset, |
| 3903 | const char* reg_name, const RegisterValue &value) |
| 3904 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3905 | WriteRegOperation op(tid, offset, reg_name, value); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3906 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3907 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3910 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3911 | NativeProcessLinux::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size) |
| 3912 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3913 | ReadGPROperation op(tid, buf, buf_size); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3914 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3915 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3916 | } |
| 3917 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3918 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3919 | NativeProcessLinux::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size) |
| 3920 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3921 | ReadFPROperation op(tid, buf, buf_size); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3922 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3923 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3926 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3927 | NativeProcessLinux::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) |
| 3928 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3929 | ReadRegisterSetOperation op(tid, buf, buf_size, regset); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3930 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3931 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3934 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3935 | NativeProcessLinux::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size) |
| 3936 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3937 | WriteGPROperation op(tid, buf, buf_size); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3938 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3939 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3940 | } |
| 3941 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3942 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3943 | NativeProcessLinux::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size) |
| 3944 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3945 | WriteFPROperation op(tid, buf, buf_size); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3946 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3947 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3948 | } |
| 3949 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3950 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3951 | NativeProcessLinux::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) |
| 3952 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3953 | WriteRegisterSetOperation op(tid, buf, buf_size, regset); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3954 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3955 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3956 | } |
| 3957 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3958 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3959 | NativeProcessLinux::Resume (lldb::tid_t tid, uint32_t signo) |
| 3960 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3961 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3962 | |
| 3963 | if (log) |
| 3964 | log->Printf ("NativeProcessLinux::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid, |
| 3965 | GetUnixSignals().GetSignalAsCString (signo)); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3966 | ResumeOperation op (tid, signo); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3967 | m_monitor_up->DoOperation (&op); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3968 | if (log) |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3969 | log->Printf ("NativeProcessLinux::%s() resuming thread = %" PRIu64 " result = %s", __FUNCTION__, tid, op.GetError().Success() ? "true" : "false"); |
| 3970 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3971 | } |
| 3972 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3973 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3974 | NativeProcessLinux::SingleStep(lldb::tid_t tid, uint32_t signo) |
| 3975 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3976 | SingleStepOperation op(tid, signo); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3977 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3978 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3979 | } |
| 3980 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3981 | Error |
| 3982 | NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3983 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3984 | SiginfoOperation op(tid, siginfo); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3985 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3986 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3987 | } |
| 3988 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3989 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3990 | NativeProcessLinux::GetEventMessage(lldb::tid_t tid, unsigned long *message) |
| 3991 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3992 | EventMessageOperation op(tid, message); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3993 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3994 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3995 | } |
| 3996 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 3997 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3998 | NativeProcessLinux::Detach(lldb::tid_t tid) |
| 3999 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 4000 | if (tid == LLDB_INVALID_THREAD_ID) |
| 4001 | return Error(); |
| 4002 | |
| 4003 | DetachOperation op(tid); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 4004 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 4005 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 4006 | } |
| 4007 | |
| 4008 | bool |
| 4009 | NativeProcessLinux::DupDescriptor(const char *path, int fd, int flags) |
| 4010 | { |
| 4011 | int target_fd = open(path, flags, 0666); |
| 4012 | |
| 4013 | if (target_fd == -1) |
| 4014 | return false; |
| 4015 | |
Pavel Labath | 493c3a1 | 2015-02-04 10:36:57 +0000 | [diff] [blame] | 4016 | if (dup2(target_fd, fd) == -1) |
| 4017 | return false; |
| 4018 | |
| 4019 | return (close(target_fd) == -1) ? false : true; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 4020 | } |
| 4021 | |
| 4022 | void |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 4023 | NativeProcessLinux::StartMonitorThread(const InitialOperation &initial_operation, Error &error) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 4024 | { |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 4025 | m_monitor_up.reset(new Monitor(initial_operation, this)); |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 4026 | error = m_monitor_up->Initialize(); |
| 4027 | if (error.Fail()) { |
| 4028 | m_monitor_up.reset(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 4029 | } |
| 4030 | } |
| 4031 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 4032 | bool |
| 4033 | NativeProcessLinux::HasThreadNoLock (lldb::tid_t thread_id) |
| 4034 | { |
| 4035 | for (auto thread_sp : m_threads) |
| 4036 | { |
| 4037 | assert (thread_sp && "thread list should not contain NULL threads"); |
| 4038 | if (thread_sp->GetID () == thread_id) |
| 4039 | { |
| 4040 | // We have this thread. |
| 4041 | return true; |
| 4042 | } |
| 4043 | } |
| 4044 | |
| 4045 | // We don't have this thread. |
| 4046 | return false; |
| 4047 | } |
| 4048 | |
| 4049 | NativeThreadProtocolSP |
| 4050 | NativeProcessLinux::MaybeGetThreadNoLock (lldb::tid_t thread_id) |
| 4051 | { |
| 4052 | // CONSIDER organize threads by map - we can do better than linear. |
| 4053 | for (auto thread_sp : m_threads) |
| 4054 | { |
| 4055 | if (thread_sp->GetID () == thread_id) |
| 4056 | return thread_sp; |
| 4057 | } |
| 4058 | |
| 4059 | // We don't have this thread. |
| 4060 | return NativeThreadProtocolSP (); |
| 4061 | } |
| 4062 | |
| 4063 | bool |
| 4064 | NativeProcessLinux::StopTrackingThread (lldb::tid_t thread_id) |
| 4065 | { |
| 4066 | Mutex::Locker locker (m_threads_mutex); |
| 4067 | for (auto it = m_threads.begin (); it != m_threads.end (); ++it) |
| 4068 | { |
| 4069 | if (*it && ((*it)->GetID () == thread_id)) |
| 4070 | { |
| 4071 | m_threads.erase (it); |
| 4072 | return true; |
| 4073 | } |
| 4074 | } |
| 4075 | |
| 4076 | // Didn't find it. |
| 4077 | return false; |
| 4078 | } |
| 4079 | |
| 4080 | NativeThreadProtocolSP |
| 4081 | NativeProcessLinux::AddThread (lldb::tid_t thread_id) |
| 4082 | { |
| 4083 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 4084 | |
| 4085 | Mutex::Locker locker (m_threads_mutex); |
| 4086 | |
| 4087 | if (log) |
| 4088 | { |
| 4089 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " adding thread with tid %" PRIu64, |
| 4090 | __FUNCTION__, |
| 4091 | GetID (), |
| 4092 | thread_id); |
| 4093 | } |
| 4094 | |
| 4095 | assert (!HasThreadNoLock (thread_id) && "attempted to add a thread by id that already exists"); |
| 4096 | |
| 4097 | // If this is the first thread, save it as the current thread |
| 4098 | if (m_threads.empty ()) |
| 4099 | SetCurrentThreadID (thread_id); |
| 4100 | |
| 4101 | NativeThreadProtocolSP thread_sp (new NativeThreadLinux (this, thread_id)); |
| 4102 | m_threads.push_back (thread_sp); |
| 4103 | |
| 4104 | return thread_sp; |
| 4105 | } |
| 4106 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 4107 | Error |
| 4108 | NativeProcessLinux::FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp) |
| 4109 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 4110 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 4111 | |
| 4112 | Error error; |
| 4113 | |
| 4114 | // Get a linux thread pointer. |
| 4115 | if (!thread_sp) |
| 4116 | { |
| 4117 | error.SetErrorString ("null thread_sp"); |
| 4118 | if (log) |
| 4119 | log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); |
| 4120 | return error; |
| 4121 | } |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 4122 | std::shared_ptr<NativeThreadLinux> linux_thread_sp = std::static_pointer_cast<NativeThreadLinux> (thread_sp); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 4123 | |
| 4124 | // Find out the size of a breakpoint (might depend on where we are in the code). |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 4125 | NativeRegisterContextSP context_sp = linux_thread_sp->GetRegisterContext (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 4126 | if (!context_sp) |
| 4127 | { |
| 4128 | error.SetErrorString ("cannot get a NativeRegisterContext for the thread"); |
| 4129 | if (log) |
| 4130 | log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); |
| 4131 | return error; |
| 4132 | } |
| 4133 | |
| 4134 | uint32_t breakpoint_size = 0; |
Tamas Berghammer | 63c8be9 | 2015-04-15 09:38:48 +0000 | [diff] [blame] | 4135 | error = GetSoftwareBreakpointPCOffset (context_sp, breakpoint_size); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 4136 | if (error.Fail ()) |
| 4137 | { |
| 4138 | if (log) |
| 4139 | log->Printf ("NativeProcessLinux::%s GetBreakpointSize() failed: %s", __FUNCTION__, error.AsCString ()); |
| 4140 | return error; |
| 4141 | } |
| 4142 | else |
| 4143 | { |
| 4144 | if (log) |
| 4145 | log->Printf ("NativeProcessLinux::%s breakpoint size: %" PRIu32, __FUNCTION__, breakpoint_size); |
| 4146 | } |
| 4147 | |
| 4148 | // First try probing for a breakpoint at a software breakpoint location: PC - breakpoint size. |
| 4149 | const lldb::addr_t initial_pc_addr = context_sp->GetPC (); |
| 4150 | lldb::addr_t breakpoint_addr = initial_pc_addr; |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 4151 | if (breakpoint_size > 0) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 4152 | { |
| 4153 | // Do not allow breakpoint probe to wrap around. |
Chaoren Lin | 3eb4b45 | 2015-04-29 17:24:48 +0000 | [diff] [blame] | 4154 | if (breakpoint_addr >= breakpoint_size) |
| 4155 | breakpoint_addr -= breakpoint_size; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | // Check if we stopped because of a breakpoint. |
| 4159 | NativeBreakpointSP breakpoint_sp; |
| 4160 | error = m_breakpoint_list.GetBreakpoint (breakpoint_addr, breakpoint_sp); |
| 4161 | if (!error.Success () || !breakpoint_sp) |
| 4162 | { |
| 4163 | // We didn't find one at a software probe location. Nothing to do. |
| 4164 | if (log) |
| 4165 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " no lldb breakpoint found at current pc with adjustment: 0x%" PRIx64, __FUNCTION__, GetID (), breakpoint_addr); |
| 4166 | return Error (); |
| 4167 | } |
| 4168 | |
| 4169 | // If the breakpoint is not a software breakpoint, nothing to do. |
| 4170 | if (!breakpoint_sp->IsSoftwareBreakpoint ()) |
| 4171 | { |
| 4172 | if (log) |
| 4173 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " breakpoint found at 0x%" PRIx64 ", not software, nothing to adjust", __FUNCTION__, GetID (), breakpoint_addr); |
| 4174 | return Error (); |
| 4175 | } |
| 4176 | |
| 4177 | // |
| 4178 | // We have a software breakpoint and need to adjust the PC. |
| 4179 | // |
| 4180 | |
| 4181 | // Sanity check. |
| 4182 | if (breakpoint_size == 0) |
| 4183 | { |
| 4184 | // Nothing to do! How did we get here? |
| 4185 | if (log) |
| 4186 | 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); |
| 4187 | return Error (); |
| 4188 | } |
| 4189 | |
| 4190 | // Change the program counter. |
| 4191 | if (log) |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 4192 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": changing PC from 0x%" PRIx64 " to 0x%" PRIx64, __FUNCTION__, GetID (), linux_thread_sp->GetID (), initial_pc_addr, breakpoint_addr); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 4193 | |
| 4194 | error = context_sp->SetPC (breakpoint_addr); |
| 4195 | if (error.Fail ()) |
| 4196 | { |
| 4197 | if (log) |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 4198 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": failed to set PC: %s", __FUNCTION__, GetID (), linux_thread_sp->GetID (), error.AsCString ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 4199 | return error; |
| 4200 | } |
| 4201 | |
| 4202 | return error; |
| 4203 | } |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 4204 | |
| 4205 | void |
| 4206 | NativeProcessLinux::NotifyThreadCreateStopped (lldb::tid_t tid) |
| 4207 | { |
| 4208 | const bool is_stopped = true; |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 4209 | NotifyThreadCreate (tid, is_stopped, CoordinatorErrorHandler); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 4210 | } |
| 4211 | |
| 4212 | void |
| 4213 | NativeProcessLinux::NotifyThreadDeath (lldb::tid_t tid) |
| 4214 | { |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 4215 | NotifyThreadDeath (tid, CoordinatorErrorHandler); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 4216 | } |
| 4217 | |
| 4218 | void |
| 4219 | NativeProcessLinux::NotifyThreadStop (lldb::tid_t tid) |
| 4220 | { |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 4221 | NotifyThreadStop (tid, false, CoordinatorErrorHandler); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 4222 | } |
| 4223 | |
| 4224 | void |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 4225 | NativeProcessLinux::StopRunningThreads(lldb::tid_t trigerring_tid) |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 4226 | { |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 4227 | Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 4228 | if (log) |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 4229 | log->Printf("NativeProcessLinux::%s tid %" PRIu64, __FUNCTION__, trigerring_tid); |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 4230 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 4231 | const lldb::pid_t pid = GetID (); |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 4232 | StopRunningThreads(trigerring_tid, |
| 4233 | [=](lldb::tid_t request_stop_tid) { return RequestThreadStop(pid, request_stop_tid); }, |
| 4234 | CoordinatorErrorHandler); |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 4235 | } |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 4236 | |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 4237 | void |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 4238 | NativeProcessLinux::StopRunningThreadsWithSkipTID(lldb::tid_t deferred_signal_tid, |
| 4239 | lldb::tid_t skip_stop_request_tid) |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 4240 | { |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 4241 | Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 4242 | if (log) |
| 4243 | log->Printf("NativeProcessLinux::%s deferred_signal_tid %" PRIu64 ", skip_stop_request_tid %" PRIu64, __FUNCTION__, deferred_signal_tid, skip_stop_request_tid); |
| 4244 | |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 4245 | const lldb::pid_t pid = GetID (); |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 4246 | StopRunningThreadsWithSkipTID(deferred_signal_tid, |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 4247 | skip_stop_request_tid != LLDB_INVALID_THREAD_ID ? NativeProcessLinux::ThreadIDSet {skip_stop_request_tid} : NativeProcessLinux::ThreadIDSet (), |
| 4248 | [=](lldb::tid_t request_stop_tid) { return RequestThreadStop(pid, request_stop_tid); }, |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 4249 | CoordinatorErrorHandler); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 4250 | } |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 4251 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 4252 | Error |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 4253 | NativeProcessLinux::RequestThreadStop (const lldb::pid_t pid, const lldb::tid_t tid) |
| 4254 | { |
| 4255 | Log* log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 4256 | if (log) |
| 4257 | log->Printf ("NativeProcessLinux::%s requesting thread stop(pid: %" PRIu64 ", tid: %" PRIu64 ")", __FUNCTION__, pid, tid); |
| 4258 | |
| 4259 | Error err; |
| 4260 | errno = 0; |
| 4261 | if (::tgkill (pid, tid, SIGSTOP) != 0) |
| 4262 | { |
| 4263 | err.SetErrorToErrno (); |
| 4264 | if (log) |
| 4265 | log->Printf ("NativeProcessLinux::%s tgkill(%" PRIu64 ", %" PRIu64 ", SIGSTOP) failed: %s", __FUNCTION__, pid, tid, err.AsCString ()); |
| 4266 | } |
| 4267 | |
| 4268 | return err; |
| 4269 | } |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 4270 | |
| 4271 | Error |
| 4272 | NativeProcessLinux::GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) |
| 4273 | { |
| 4274 | char maps_file_name[32]; |
| 4275 | snprintf(maps_file_name, sizeof(maps_file_name), "/proc/%" PRIu64 "/maps", GetID()); |
| 4276 | |
| 4277 | FileSpec maps_file_spec(maps_file_name, false); |
| 4278 | if (!maps_file_spec.Exists()) { |
| 4279 | file_spec.Clear(); |
| 4280 | return Error("/proc/%" PRIu64 "/maps file doesn't exists!", GetID()); |
| 4281 | } |
| 4282 | |
| 4283 | FileSpec module_file_spec(module_path, true); |
| 4284 | |
| 4285 | std::ifstream maps_file(maps_file_name); |
| 4286 | std::string maps_data_str((std::istreambuf_iterator<char>(maps_file)), std::istreambuf_iterator<char>()); |
| 4287 | StringRef maps_data(maps_data_str.c_str()); |
| 4288 | |
| 4289 | while (!maps_data.empty()) |
| 4290 | { |
| 4291 | StringRef maps_row; |
| 4292 | std::tie(maps_row, maps_data) = maps_data.split('\n'); |
| 4293 | |
| 4294 | SmallVector<StringRef, 16> maps_columns; |
| 4295 | maps_row.split(maps_columns, StringRef(" "), -1, false); |
| 4296 | |
| 4297 | if (maps_columns.size() >= 6) |
| 4298 | { |
| 4299 | file_spec.SetFile(maps_columns[5].str().c_str(), false); |
| 4300 | if (file_spec.GetFilename() == module_file_spec.GetFilename()) |
| 4301 | return Error(); |
| 4302 | } |
| 4303 | } |
| 4304 | |
| 4305 | file_spec.Clear(); |
| 4306 | return Error("Module file (%s) not found in /proc/%" PRIu64 "/maps file!", |
| 4307 | module_file_spec.GetFilename().AsCString(), GetID()); |
| 4308 | } |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 4309 | |
| 4310 | void |
| 4311 | NativeProcessLinux::DoResume( |
| 4312 | lldb::tid_t tid, |
| 4313 | ResumeThreadFunction request_thread_resume_function, |
| 4314 | ErrorFunction error_function, |
| 4315 | bool error_when_already_running) |
| 4316 | { |
| 4317 | // Ensure we know about the thread. |
| 4318 | auto find_it = m_tid_map.find (tid); |
| 4319 | if (find_it == m_tid_map.end ()) |
| 4320 | { |
| 4321 | // We don't know about this thread. This is an error condition. |
| 4322 | std::ostringstream error_message; |
| 4323 | error_message << "error: tid " << tid << " asked to resume but tid is unknown"; |
| 4324 | error_function (error_message.str ()); |
| 4325 | return; |
| 4326 | } |
| 4327 | auto& context = find_it->second; |
| 4328 | // Tell the thread to resume if we don't already think it is running. |
| 4329 | const bool is_stopped = context.m_state == ThreadState::Stopped; |
| 4330 | if (!is_stopped) |
| 4331 | { |
| 4332 | // It's not an error, just a log, if the error_when_already_running flag is not set. |
| 4333 | // This covers cases where, for instance, we're just trying to resume all threads |
| 4334 | // from the user side. |
| 4335 | if (!error_when_already_running) |
| 4336 | { |
| 4337 | TSCLog ("NativeProcessLinux::%s tid %" PRIu64 " optional resume skipped since it is already running", |
| 4338 | __FUNCTION__, |
| 4339 | tid); |
| 4340 | } |
| 4341 | else |
| 4342 | { |
| 4343 | // Skip the resume call - we have tracked it to be running. And we unconditionally |
| 4344 | // expected to resume this thread. Flag this as an error. |
| 4345 | std::ostringstream error_message; |
| 4346 | error_message << "error: tid " << tid << " asked to resume but we think it is already running"; |
| 4347 | error_function (error_message.str ()); |
| 4348 | } |
| 4349 | |
| 4350 | // Error or not, we're done. |
| 4351 | return; |
| 4352 | } |
| 4353 | |
| 4354 | // Before we do the resume below, first check if we have a pending |
| 4355 | // stop notification this is currently or was previously waiting for |
| 4356 | // this thread to stop. This is potentially a buggy situation since |
| 4357 | // we're ostensibly waiting for threads to stop before we send out the |
| 4358 | // pending notification, and here we are resuming one before we send |
| 4359 | // out the pending stop notification. |
| 4360 | if (m_pending_notification_up) |
| 4361 | { |
| 4362 | if (m_pending_notification_up->wait_for_stop_tids.count (tid) > 0) |
| 4363 | { |
| 4364 | TSCLog ("NativeProcessLinux::%s about to resume tid %" PRIu64 " per explicit request but we have a pending stop notification (tid %" PRIu64 ") that is actively waiting for this thread to stop. Valid sequence of events?", __FUNCTION__, tid, m_pending_notification_up->triggering_tid); |
| 4365 | } |
| 4366 | else if (m_pending_notification_up->original_wait_for_stop_tids.count (tid) > 0) |
| 4367 | { |
| 4368 | TSCLog ("NativeProcessLinux::%s about to resume tid %" PRIu64 " per explicit request but we have a pending stop notification (tid %" PRIu64 ") that hasn't fired yet and this is one of the threads we had been waiting on (and already marked satisfied for this tid). Valid sequence of events?", __FUNCTION__, tid, m_pending_notification_up->triggering_tid); |
| 4369 | for (auto tid : m_pending_notification_up->wait_for_stop_tids) |
| 4370 | { |
| 4371 | TSCLog ("NativeProcessLinux::%s tid %" PRIu64 " deferred stop notification still waiting on tid %" PRIu64, |
| 4372 | __FUNCTION__, |
| 4373 | m_pending_notification_up->triggering_tid, |
| 4374 | tid); |
| 4375 | } |
| 4376 | } |
| 4377 | } |
| 4378 | |
| 4379 | // Request a resume. We expect this to be synchronous and the system |
| 4380 | // to reflect it is running after this completes. |
| 4381 | const auto error = request_thread_resume_function (tid, false); |
| 4382 | if (error.Success ()) |
| 4383 | { |
| 4384 | // Now mark it is running. |
| 4385 | context.m_state = ThreadState::Running; |
| 4386 | context.m_request_resume_function = request_thread_resume_function; |
| 4387 | } |
| 4388 | else |
| 4389 | { |
| 4390 | TSCLog ("NativeProcessLinux::%s failed to resume thread tid %" PRIu64 ": %s", |
| 4391 | __FUNCTION__, tid, error.AsCString ()); |
| 4392 | } |
| 4393 | |
| 4394 | return; |
| 4395 | } |
| 4396 | |
| 4397 | //===----------------------------------------------------------------------===// |
| 4398 | |
| 4399 | void |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 4400 | NativeProcessLinux::StopThreads(const lldb::tid_t triggering_tid, |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 4401 | const ThreadIDSet &wait_for_stop_tids, |
| 4402 | const StopThreadFunction &request_thread_stop_function, |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 4403 | const ErrorFunction &error_function) |
| 4404 | { |
| 4405 | std::lock_guard<std::mutex> lock(m_event_mutex); |
| 4406 | |
| 4407 | if (m_log_event_processing) |
| 4408 | { |
| 4409 | TSCLog ("NativeProcessLinux::%s about to process event: (triggering_tid: %" PRIu64 ", wait_for_stop_tids.size(): %zd)", |
| 4410 | __FUNCTION__, triggering_tid, wait_for_stop_tids.size()); |
| 4411 | } |
| 4412 | |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 4413 | DoStopThreads(PendingNotificationUP(new PendingNotification( |
| 4414 | triggering_tid, wait_for_stop_tids, request_thread_stop_function, error_function))); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 4415 | |
| 4416 | if (m_log_event_processing) |
| 4417 | { |
| 4418 | TSCLog ("NativeProcessLinux::%s event processing done", __FUNCTION__); |
| 4419 | } |
| 4420 | } |
| 4421 | |
| 4422 | void |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 4423 | NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid, |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 4424 | const StopThreadFunction &request_thread_stop_function, |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 4425 | const ErrorFunction &error_function) |
| 4426 | { |
| 4427 | std::lock_guard<std::mutex> lock(m_event_mutex); |
| 4428 | |
| 4429 | if (m_log_event_processing) |
| 4430 | { |
| 4431 | TSCLog ("NativeProcessLinux::%s about to process event: (triggering_tid: %" PRIu64 ")", |
| 4432 | __FUNCTION__, triggering_tid); |
| 4433 | } |
| 4434 | |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 4435 | DoStopThreads(PendingNotificationUP(new PendingNotification( |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 4436 | triggering_tid, |
| 4437 | request_thread_stop_function, |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 4438 | error_function))); |
| 4439 | |
| 4440 | if (m_log_event_processing) |
| 4441 | { |
| 4442 | TSCLog ("NativeProcessLinux::%s event processing done", __FUNCTION__); |
| 4443 | } |
| 4444 | } |
| 4445 | |
| 4446 | void |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 4447 | NativeProcessLinux::StopRunningThreadsWithSkipTID(lldb::tid_t triggering_tid, |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 4448 | const ThreadIDSet &skip_stop_request_tids, |
| 4449 | const StopThreadFunction &request_thread_stop_function, |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 4450 | const ErrorFunction &error_function) |
| 4451 | { |
| 4452 | std::lock_guard<std::mutex> lock(m_event_mutex); |
| 4453 | |
| 4454 | if (m_log_event_processing) |
| 4455 | { |
| 4456 | TSCLog ("NativeProcessLinux::%s about to process event: (triggering_tid: %" PRIu64 ", skip_stop_request_tids.size(): %zd)", |
| 4457 | __FUNCTION__, triggering_tid, skip_stop_request_tids.size()); |
| 4458 | } |
| 4459 | |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 4460 | DoStopThreads(PendingNotificationUP(new PendingNotification( |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 4461 | triggering_tid, |
| 4462 | request_thread_stop_function, |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 4463 | skip_stop_request_tids, |
| 4464 | error_function))); |
| 4465 | |
| 4466 | if (m_log_event_processing) |
| 4467 | { |
| 4468 | TSCLog ("NativeProcessLinux::%s event processing done", __FUNCTION__); |
| 4469 | } |
| 4470 | } |
| 4471 | |
| 4472 | void |
| 4473 | NativeProcessLinux::SignalIfRequirementsSatisfied() |
| 4474 | { |
| 4475 | if (m_pending_notification_up && m_pending_notification_up->wait_for_stop_tids.empty ()) |
| 4476 | { |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 4477 | SetCurrentThreadID(m_pending_notification_up->triggering_tid); |
| 4478 | SetState(StateType::eStateStopped, true); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 4479 | m_pending_notification_up.reset(); |
| 4480 | } |
| 4481 | } |
| 4482 | |
| 4483 | bool |
| 4484 | NativeProcessLinux::RequestStopOnAllSpecifiedThreads() |
| 4485 | { |
| 4486 | // Request a stop for all the thread stops that need to be stopped |
| 4487 | // and are not already known to be stopped. Keep a list of all the |
| 4488 | // threads from which we still need to hear a stop reply. |
| 4489 | |
| 4490 | ThreadIDSet sent_tids; |
| 4491 | for (auto tid : m_pending_notification_up->wait_for_stop_tids) |
| 4492 | { |
| 4493 | // Validate we know about all tids for which we must first receive a stop before |
| 4494 | // triggering the deferred stop notification. |
| 4495 | auto find_it = m_tid_map.find (tid); |
| 4496 | if (find_it == m_tid_map.end ()) |
| 4497 | { |
| 4498 | // This is an error. We shouldn't be asking for waiting pids that aren't known. |
| 4499 | // NOTE: we may be stripping out the specification of wait tids and handle this |
| 4500 | // automatically, in which case this state can never occur. |
| 4501 | std::ostringstream error_message; |
| 4502 | error_message << "error: deferred notification for tid " << m_pending_notification_up->triggering_tid << " specified an unknown/untracked pending stop tid " << m_pending_notification_up->triggering_tid; |
| 4503 | m_pending_notification_up->error_function (error_message.str ()); |
| 4504 | |
| 4505 | // Bail out here. |
| 4506 | return false; |
| 4507 | } |
| 4508 | |
| 4509 | // If the pending stop thread is currently running, we need to send it a stop request. |
| 4510 | auto& context = find_it->second; |
| 4511 | if (context.m_state == ThreadState::Running) |
| 4512 | { |
| 4513 | RequestThreadStop (tid, context); |
| 4514 | sent_tids.insert (tid); |
| 4515 | } |
| 4516 | } |
| 4517 | // We only need to wait for the sent_tids - so swap our wait set |
| 4518 | // to the sent tids. The rest are already stopped and we won't |
| 4519 | // be receiving stop notifications for them. |
| 4520 | m_pending_notification_up->wait_for_stop_tids.swap (sent_tids); |
| 4521 | |
| 4522 | // Succeeded, keep running. |
| 4523 | return true; |
| 4524 | } |
| 4525 | |
| 4526 | void |
| 4527 | NativeProcessLinux::RequestStopOnAllRunningThreads() |
| 4528 | { |
| 4529 | // Request a stop for all the thread stops that need to be stopped |
| 4530 | // and are not already known to be stopped. Keep a list of all the |
| 4531 | // threads from which we still need to hear a stop reply. |
| 4532 | |
| 4533 | ThreadIDSet sent_tids; |
| 4534 | for (auto it = m_tid_map.begin(); it != m_tid_map.end(); ++it) |
| 4535 | { |
| 4536 | // We only care about threads not stopped. |
| 4537 | const bool running = it->second.m_state == ThreadState::Running; |
| 4538 | if (running) |
| 4539 | { |
| 4540 | const lldb::tid_t tid = it->first; |
| 4541 | |
| 4542 | // Request this thread stop if the tid stop request is not explicitly ignored. |
| 4543 | const bool skip_stop_request = m_pending_notification_up->skip_stop_request_tids.count (tid) > 0; |
| 4544 | if (!skip_stop_request) |
| 4545 | RequestThreadStop (tid, it->second); |
| 4546 | |
| 4547 | // Even if we skipped sending the stop request for other reasons (like stepping), |
| 4548 | // we still need to wait for that stepping thread to notify completion/stop. |
| 4549 | sent_tids.insert (tid); |
| 4550 | } |
| 4551 | } |
| 4552 | |
| 4553 | // Set the wait list to the set of tids for which we requested stops. |
| 4554 | m_pending_notification_up->wait_for_stop_tids.swap (sent_tids); |
| 4555 | } |
| 4556 | |
| 4557 | void |
| 4558 | NativeProcessLinux::RequestThreadStop (lldb::tid_t tid, ThreadContext& context) |
| 4559 | { |
| 4560 | const auto error = m_pending_notification_up->request_thread_stop_function (tid); |
| 4561 | if (error.Success ()) |
| 4562 | context.m_stop_requested = true; |
| 4563 | else |
| 4564 | { |
| 4565 | TSCLog ("NativeProcessLinux::%s failed to request thread stop tid %" PRIu64 ": %s", |
| 4566 | __FUNCTION__, tid, error.AsCString ()); |
| 4567 | } |
| 4568 | } |
| 4569 | |
| 4570 | |
| 4571 | void |
| 4572 | NativeProcessLinux::ThreadDidStop (lldb::tid_t tid, bool initiated_by_llgs, const ErrorFunction &error_function) |
| 4573 | { |
| 4574 | // Ensure we know about the thread. |
| 4575 | auto find_it = m_tid_map.find (tid); |
| 4576 | if (find_it == m_tid_map.end ()) |
| 4577 | { |
| 4578 | // We don't know about this thread. This is an error condition. |
| 4579 | std::ostringstream error_message; |
| 4580 | error_message << "error: tid " << tid << " asked to stop but tid is unknown"; |
| 4581 | error_function (error_message.str ()); |
| 4582 | return; |
| 4583 | } |
| 4584 | |
| 4585 | // Update the global list of known thread states. This one is definitely stopped. |
| 4586 | auto& context = find_it->second; |
| 4587 | const auto stop_was_requested = context.m_stop_requested; |
| 4588 | context.m_state = ThreadState::Stopped; |
| 4589 | context.m_stop_requested = false; |
| 4590 | |
| 4591 | // If we have a pending notification, remove this from the set. |
| 4592 | if (m_pending_notification_up) |
| 4593 | { |
| 4594 | m_pending_notification_up->wait_for_stop_tids.erase(tid); |
| 4595 | SignalIfRequirementsSatisfied(); |
| 4596 | } |
| 4597 | |
| 4598 | if (initiated_by_llgs && context.m_request_resume_function && !stop_was_requested) |
| 4599 | { |
| 4600 | // We can end up here if stop was initiated by LLGS but by this time a |
| 4601 | // thread stop has occurred - maybe initiated by another event. |
| 4602 | TSCLog ("Resuming thread %" PRIu64 " since stop wasn't requested", tid); |
| 4603 | const auto error = context.m_request_resume_function (tid, true); |
| 4604 | if (error.Success ()) |
| 4605 | { |
| 4606 | context.m_state = ThreadState::Running; |
| 4607 | } |
| 4608 | else |
| 4609 | { |
| 4610 | TSCLog ("NativeProcessLinux::%s failed to resume thread tid %" PRIu64 ": %s", |
| 4611 | __FUNCTION__, tid, error.AsCString ()); |
| 4612 | } |
| 4613 | } |
| 4614 | } |
| 4615 | |
| 4616 | void |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 4617 | NativeProcessLinux::DoStopThreads(PendingNotificationUP &¬ification_up) |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 4618 | { |
| 4619 | // Validate we know about the deferred trigger thread. |
| 4620 | if (!IsKnownThread (notification_up->triggering_tid)) |
| 4621 | { |
| 4622 | // We don't know about this thread. This is an error condition. |
| 4623 | std::ostringstream error_message; |
| 4624 | error_message << "error: deferred notification tid " << notification_up->triggering_tid << " is unknown"; |
| 4625 | notification_up->error_function (error_message.str ()); |
| 4626 | |
| 4627 | // We bail out here. |
| 4628 | return; |
| 4629 | } |
| 4630 | |
| 4631 | if (m_pending_notification_up) |
| 4632 | { |
| 4633 | // Yikes - we've already got a pending signal notification in progress. |
| 4634 | // Log this info. We lose the pending notification here. |
| 4635 | TSCLog ("NativeProcessLinux::%s dropping existing pending signal notification for tid %" PRIu64 ", to be replaced with signal for tid %" PRIu64, |
| 4636 | __FUNCTION__, |
| 4637 | m_pending_notification_up->triggering_tid, |
| 4638 | notification_up->triggering_tid); |
| 4639 | } |
| 4640 | m_pending_notification_up = std::move(notification_up); |
| 4641 | |
| 4642 | if (m_pending_notification_up->request_stop_on_all_unstopped_threads) |
| 4643 | RequestStopOnAllRunningThreads(); |
| 4644 | else |
| 4645 | { |
| 4646 | if (!RequestStopOnAllSpecifiedThreads()) |
| 4647 | return; |
| 4648 | } |
| 4649 | |
Pavel Labath | ed89c7f | 2015-05-06 12:22:37 +0000 | [diff] [blame] | 4650 | SignalIfRequirementsSatisfied(); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 4651 | } |
| 4652 | |
| 4653 | void |
| 4654 | NativeProcessLinux::ThreadWasCreated (lldb::tid_t tid, bool is_stopped, const ErrorFunction &error_function) |
| 4655 | { |
| 4656 | // Ensure we don't already know about the thread. |
| 4657 | auto find_it = m_tid_map.find (tid); |
| 4658 | if (find_it != m_tid_map.end ()) |
| 4659 | { |
| 4660 | // We already know about this thread. This is an error condition. |
| 4661 | std::ostringstream error_message; |
| 4662 | error_message << "error: notified tid " << tid << " created but we already know about this thread"; |
| 4663 | error_function (error_message.str ()); |
| 4664 | return; |
| 4665 | } |
| 4666 | |
| 4667 | // Add the new thread to the stop map. |
| 4668 | ThreadContext ctx; |
| 4669 | ctx.m_state = (is_stopped) ? ThreadState::Stopped : ThreadState::Running; |
| 4670 | m_tid_map[tid] = std::move(ctx); |
| 4671 | |
| 4672 | if (m_pending_notification_up && !is_stopped) |
| 4673 | { |
| 4674 | // We will need to wait for this new thread to stop as well before firing the |
| 4675 | // notification. |
| 4676 | m_pending_notification_up->wait_for_stop_tids.insert(tid); |
| 4677 | m_pending_notification_up->request_thread_stop_function(tid); |
| 4678 | } |
| 4679 | } |
| 4680 | |
| 4681 | void |
| 4682 | NativeProcessLinux::ThreadDidDie (lldb::tid_t tid, const ErrorFunction &error_function) |
| 4683 | { |
| 4684 | // Ensure we know about the thread. |
| 4685 | auto find_it = m_tid_map.find (tid); |
| 4686 | if (find_it == m_tid_map.end ()) |
| 4687 | { |
| 4688 | // We don't know about this thread. This is an error condition. |
| 4689 | std::ostringstream error_message; |
| 4690 | error_message << "error: notified tid " << tid << " died but tid is unknown"; |
| 4691 | error_function (error_message.str ()); |
| 4692 | return; |
| 4693 | } |
| 4694 | |
| 4695 | // Update the global list of known thread states. While this one is stopped, it is also dead. |
| 4696 | // So stop tracking it. We assume the user of this coordinator will not keep trying to add |
| 4697 | // dependencies on a thread after it is known to be dead. |
| 4698 | m_tid_map.erase (find_it); |
| 4699 | |
| 4700 | // If we have a pending notification, remove this from the set. |
| 4701 | if (m_pending_notification_up) |
| 4702 | { |
| 4703 | m_pending_notification_up->wait_for_stop_tids.erase(tid); |
| 4704 | SignalIfRequirementsSatisfied(); |
| 4705 | } |
| 4706 | } |
| 4707 | |
| 4708 | void |
| 4709 | NativeProcessLinux::TSCLog (const char *format, ...) |
| 4710 | { |
| 4711 | va_list args; |
| 4712 | va_start (args, format); |
| 4713 | |
| 4714 | m_log_function (format, args); |
| 4715 | |
| 4716 | va_end (args); |
| 4717 | } |
| 4718 | |
| 4719 | void |
| 4720 | NativeProcessLinux::NotifyThreadStop (lldb::tid_t tid, |
| 4721 | bool initiated_by_llgs, |
| 4722 | const ErrorFunction &error_function) |
| 4723 | { |
| 4724 | std::lock_guard<std::mutex> lock(m_event_mutex); |
| 4725 | |
| 4726 | if (m_log_event_processing) |
| 4727 | { |
| 4728 | TSCLog ("NativeProcessLinux::%s about to process event: (tid: %" PRIu64 ", %sinitiated by llgs)", |
| 4729 | __FUNCTION__, tid, initiated_by_llgs?"":"not "); |
| 4730 | } |
| 4731 | |
| 4732 | ThreadDidStop (tid, initiated_by_llgs, error_function); |
| 4733 | |
| 4734 | if (m_log_event_processing) |
| 4735 | { |
| 4736 | TSCLog ("NativeProcessLinux::%s event processing done", __FUNCTION__); |
| 4737 | } |
| 4738 | } |
| 4739 | |
| 4740 | void |
| 4741 | NativeProcessLinux::RequestThreadResume (lldb::tid_t tid, |
| 4742 | const ResumeThreadFunction &request_thread_resume_function, |
| 4743 | const ErrorFunction &error_function) |
| 4744 | { |
| 4745 | std::lock_guard<std::mutex> lock(m_event_mutex); |
| 4746 | |
| 4747 | if (m_log_event_processing) |
| 4748 | { |
| 4749 | TSCLog ("NativeProcessLinux::%s about to process event: (tid: %" PRIu64 ")", |
| 4750 | __FUNCTION__, tid); |
| 4751 | } |
| 4752 | |
| 4753 | DoResume(tid, request_thread_resume_function, error_function, true); |
| 4754 | |
| 4755 | if (m_log_event_processing) |
| 4756 | { |
| 4757 | TSCLog ("NativeProcessLinux::%s event processing done", __FUNCTION__); |
| 4758 | } |
| 4759 | } |
| 4760 | |
| 4761 | void |
| 4762 | NativeProcessLinux::RequestThreadResumeAsNeeded (lldb::tid_t tid, |
| 4763 | const ResumeThreadFunction &request_thread_resume_function, |
| 4764 | const ErrorFunction &error_function) |
| 4765 | { |
| 4766 | std::lock_guard<std::mutex> lock(m_event_mutex); |
| 4767 | |
| 4768 | if (m_log_event_processing) |
| 4769 | { |
| 4770 | TSCLog ("NativeProcessLinux::%s about to process event: (tid: %" PRIu64 ")", |
| 4771 | __FUNCTION__, tid); |
| 4772 | } |
| 4773 | |
| 4774 | DoResume (tid, request_thread_resume_function, error_function, false); |
| 4775 | |
| 4776 | if (m_log_event_processing) |
| 4777 | { |
| 4778 | TSCLog ("NativeProcessLinux::%s event processing done", __FUNCTION__); |
| 4779 | } |
| 4780 | } |
| 4781 | |
| 4782 | void |
| 4783 | NativeProcessLinux::NotifyThreadCreate (lldb::tid_t tid, |
| 4784 | bool is_stopped, |
| 4785 | const ErrorFunction &error_function) |
| 4786 | { |
| 4787 | std::lock_guard<std::mutex> lock(m_event_mutex); |
| 4788 | |
| 4789 | if (m_log_event_processing) |
| 4790 | { |
| 4791 | TSCLog ("NativeProcessLinux::%s about to process event: (tid: %" PRIu64 ", is %sstopped)", |
| 4792 | __FUNCTION__, tid, is_stopped?"":"not "); |
| 4793 | } |
| 4794 | |
| 4795 | ThreadWasCreated (tid, is_stopped, error_function); |
| 4796 | |
| 4797 | if (m_log_event_processing) |
| 4798 | { |
| 4799 | TSCLog ("NativeProcessLinux::%s event processing done", __FUNCTION__); |
| 4800 | } |
| 4801 | } |
| 4802 | |
| 4803 | void |
| 4804 | NativeProcessLinux::NotifyThreadDeath (lldb::tid_t tid, |
| 4805 | const ErrorFunction &error_function) |
| 4806 | { |
| 4807 | std::lock_guard<std::mutex> lock(m_event_mutex); |
| 4808 | |
| 4809 | if (m_log_event_processing) |
| 4810 | { |
| 4811 | TSCLog ("NativeProcessLinux::%s about to process event: (tid: %" PRIu64 ")", __FUNCTION__, tid); |
| 4812 | } |
| 4813 | |
| 4814 | ThreadDidDie(tid, error_function); |
| 4815 | |
| 4816 | if (m_log_event_processing) |
| 4817 | { |
| 4818 | TSCLog ("NativeProcessLinux::%s event processing done", __FUNCTION__); |
| 4819 | } |
| 4820 | } |
| 4821 | |
| 4822 | void |
| 4823 | NativeProcessLinux::ResetForExec () |
| 4824 | { |
| 4825 | std::lock_guard<std::mutex> lock(m_event_mutex); |
| 4826 | |
| 4827 | if (m_log_event_processing) |
| 4828 | { |
| 4829 | TSCLog ("NativeProcessLinux::%s about to process event", __FUNCTION__); |
| 4830 | } |
| 4831 | |
| 4832 | // Clear the pending notification if there was one. |
| 4833 | m_pending_notification_up.reset (); |
| 4834 | |
| 4835 | // Clear the stop map - we no longer know anything about any thread state. |
| 4836 | // The caller is expected to reset thread states for all threads, and we |
| 4837 | // will assume anything we haven't heard about is running and requires a |
| 4838 | // stop. |
| 4839 | m_tid_map.clear (); |
| 4840 | |
| 4841 | if (m_log_event_processing) |
| 4842 | { |
| 4843 | TSCLog ("NativeProcessLinux::%s event processing done", __FUNCTION__); |
| 4844 | } |
| 4845 | } |
| 4846 | void |
| 4847 | NativeProcessLinux::LogEnableEventProcessing (bool enabled) |
| 4848 | { |
| 4849 | m_log_event_processing = enabled; |
| 4850 | } |
| 4851 | |
| 4852 | bool |
| 4853 | NativeProcessLinux::IsKnownThread (lldb::tid_t tid) const |
| 4854 | { |
| 4855 | return m_tid_map.find (tid) != m_tid_map.end (); |
| 4856 | } |