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> |
| 23 | #include <string> |
| 24 | |
| 25 | // Other libraries and framework includes |
| 26 | #include "lldb/Core/Debugger.h" |
Tamas Berghammer | d8c338d | 2015-04-15 09:47:02 +0000 | [diff] [blame] | 27 | #include "lldb/Core/EmulateInstruction.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 28 | #include "lldb/Core/Error.h" |
| 29 | #include "lldb/Core/Module.h" |
Oleksiy Vyalov | 6edef20 | 2014-11-17 22:16:42 +0000 | [diff] [blame] | 30 | #include "lldb/Core/ModuleSpec.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 31 | #include "lldb/Core/RegisterValue.h" |
| 32 | #include "lldb/Core/Scalar.h" |
| 33 | #include "lldb/Core/State.h" |
Tamas Berghammer | 1e209fc | 2015-03-13 11:36:47 +0000 | [diff] [blame] | 34 | #include "lldb/Host/common/NativeBreakpoint.h" |
Tamas Berghammer | 0cbf0b1 | 2015-03-13 11:16:03 +0000 | [diff] [blame] | 35 | #include "lldb/Host/common/NativeRegisterContext.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 36 | #include "lldb/Host/Host.h" |
Zachary Turner | 13b1826 | 2014-08-20 16:42:51 +0000 | [diff] [blame] | 37 | #include "lldb/Host/HostInfo.h" |
Tamas Berghammer | 0cbf0b1 | 2015-03-13 11:16:03 +0000 | [diff] [blame] | 38 | #include "lldb/Host/HostNativeThread.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 39 | #include "lldb/Host/ThreadLauncher.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 40 | #include "lldb/Symbol/ObjectFile.h" |
Zachary Turner | 90aff47 | 2015-03-03 23:36:51 +0000 | [diff] [blame] | 41 | #include "lldb/Target/Process.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 42 | #include "lldb/Target/ProcessLaunchInfo.h" |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 43 | #include "lldb/Utility/LLDBAssert.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 44 | #include "lldb/Utility/PseudoTerminal.h" |
| 45 | |
Tamas Berghammer | 1e209fc | 2015-03-13 11:36:47 +0000 | [diff] [blame] | 46 | #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 47 | #include "Plugins/Process/Utility/LinuxSignals.h" |
Tamas Berghammer | 1e209fc | 2015-03-13 11:36:47 +0000 | [diff] [blame] | 48 | #include "Utility/StringExtractor.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 49 | #include "NativeThreadLinux.h" |
| 50 | #include "ProcFileReader.h" |
Tamas Berghammer | 1e209fc | 2015-03-13 11:36:47 +0000 | [diff] [blame] | 51 | #include "Procfs.h" |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 52 | #include "ThreadStateCoordinator.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 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 156 | ThreadStateCoordinator::LogFunction |
| 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 ()); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 173 | assert (false && "ThreadStateCoordinator error reported"); |
| 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 | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 348 | lldb::addr_t |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 349 | DoReadMemory ( |
| 350 | lldb::pid_t pid, |
| 351 | lldb::addr_t vm_addr, |
| 352 | void *buf, |
| 353 | lldb::addr_t size, |
| 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); |
| 359 | lldb::addr_t bytes_read; |
| 360 | lldb::addr_t remainder; |
| 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 | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 410 | lldb::addr_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, |
| 415 | lldb::addr_t size, |
| 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); |
| 421 | lldb::addr_t bytes_written = 0; |
| 422 | lldb::addr_t remainder; |
| 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: |
| 529 | ReadOperation ( |
| 530 | lldb::addr_t addr, |
| 531 | void *buff, |
| 532 | lldb::addr_t size, |
Todd Fiala | b35103e | 2014-07-10 05:25:39 +0000 | [diff] [blame] | 533 | lldb::addr_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; |
| 547 | lldb::addr_t m_size; |
| 548 | lldb::addr_t &m_result; |
| 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: |
| 563 | WriteOperation ( |
| 564 | lldb::addr_t addr, |
| 565 | const void *buff, |
| 566 | lldb::addr_t size, |
| 567 | lldb::addr_t &result) : |
| 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; |
| 581 | lldb::addr_t m_size; |
| 582 | lldb::addr_t &m_result; |
| 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 | } |
| 1072 | |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1073 | } // end of anonymous namespace |
| 1074 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1075 | // Simple helper function to ensure flags are enabled on the given file |
| 1076 | // descriptor. |
| 1077 | static Error |
| 1078 | EnsureFDFlags(int fd, int flags) |
| 1079 | { |
| 1080 | Error error; |
| 1081 | |
| 1082 | int status = fcntl(fd, F_GETFL); |
| 1083 | if (status == -1) |
| 1084 | { |
| 1085 | error.SetErrorToErrno(); |
| 1086 | return error; |
| 1087 | } |
| 1088 | |
| 1089 | if (fcntl(fd, F_SETFL, status | flags) == -1) |
| 1090 | { |
| 1091 | error.SetErrorToErrno(); |
| 1092 | return error; |
| 1093 | } |
| 1094 | |
| 1095 | return error; |
| 1096 | } |
| 1097 | |
| 1098 | // This class encapsulates the privileged thread which performs all ptrace and wait operations on |
| 1099 | // the inferior. The thread consists of a main loop which waits for events and processes them |
| 1100 | // - SIGCHLD (delivered over a signalfd file descriptor): These signals notify us of events in |
| 1101 | // the inferior process. Upon receiving this signal we do a waitpid to get more information |
| 1102 | // and dispatch to NativeProcessLinux::MonitorCallback. |
| 1103 | // - requests for ptrace operations: These initiated via the DoOperation method, which funnels |
| 1104 | // them to the Monitor thread via m_operation member. The Monitor thread is signaled over a |
| 1105 | // pipe, and the completion of the operation is signalled over the semaphore. |
| 1106 | // - thread exit event: this is signaled from the Monitor destructor by closing the write end |
| 1107 | // of the command pipe. |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1108 | class NativeProcessLinux::Monitor { |
| 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 | |
| 1127 | static constexpr char operation_command = 'o'; |
| 1128 | |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1129 | void |
| 1130 | HandleSignals(); |
| 1131 | |
| 1132 | void |
| 1133 | HandleWait(); |
| 1134 | |
| 1135 | // Returns true if the thread should exit. |
| 1136 | bool |
| 1137 | HandleCommands(); |
| 1138 | |
| 1139 | void |
| 1140 | MainLoop(); |
| 1141 | |
| 1142 | static void * |
| 1143 | RunMonitor(void *arg); |
| 1144 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1145 | Error |
| 1146 | WaitForOperation(); |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1147 | public: |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1148 | Monitor(const InitialOperation &initial_operation, |
| 1149 | NativeProcessLinux *native_process) |
| 1150 | : m_initial_operation_up(new InitialOperation(initial_operation)), |
| 1151 | m_native_process(native_process) |
| 1152 | { |
| 1153 | sem_init(&m_operation_sem, 0, 0); |
| 1154 | } |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1155 | |
| 1156 | ~Monitor(); |
| 1157 | |
| 1158 | Error |
| 1159 | Initialize(); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1160 | |
| 1161 | void |
| 1162 | DoOperation(Operation *op); |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1163 | }; |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1164 | constexpr char NativeProcessLinux::Monitor::operation_command; |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1165 | |
| 1166 | Error |
| 1167 | NativeProcessLinux::Monitor::Initialize() |
| 1168 | { |
| 1169 | Error error; |
| 1170 | |
| 1171 | // We get a SIGCHLD every time something interesting happens with the inferior. We shall be |
| 1172 | // listening for these signals over a signalfd file descriptors. This allows us to wait for |
| 1173 | // multiple kinds of events with select. |
| 1174 | sigset_t signals; |
| 1175 | sigemptyset(&signals); |
| 1176 | sigaddset(&signals, SIGCHLD); |
| 1177 | m_signal_fd = signalfd(-1, &signals, SFD_NONBLOCK | SFD_CLOEXEC); |
| 1178 | if (m_signal_fd < 0) |
| 1179 | { |
| 1180 | return Error("NativeProcessLinux::Monitor::%s failed due to signalfd failure. Monitoring the inferior will be impossible: %s", |
| 1181 | __FUNCTION__, strerror(errno)); |
| 1182 | |
| 1183 | } |
| 1184 | |
| 1185 | if (pipe2(m_pipefd, O_CLOEXEC) == -1) |
| 1186 | { |
| 1187 | error.SetErrorToErrno(); |
| 1188 | return error; |
| 1189 | } |
| 1190 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1191 | if ((error = EnsureFDFlags(m_pipefd[READ], O_NONBLOCK)).Fail()) { |
| 1192 | return error; |
| 1193 | } |
| 1194 | |
| 1195 | static const char g_thread_name[] = "lldb.process.nativelinux.monitor"; |
| 1196 | m_thread = ThreadLauncher::LaunchThread(g_thread_name, Monitor::RunMonitor, this, nullptr); |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1197 | if (!m_thread.IsJoinable()) |
| 1198 | return Error("Failed to create monitor thread for NativeProcessLinux."); |
| 1199 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1200 | // Wait for initial operation to complete. |
| 1201 | return WaitForOperation(); |
| 1202 | } |
| 1203 | |
| 1204 | void |
| 1205 | NativeProcessLinux::Monitor::DoOperation(Operation *op) |
| 1206 | { |
| 1207 | if (m_thread.EqualsThread(pthread_self())) { |
| 1208 | // If we're on the Monitor thread, we can simply execute the operation. |
| 1209 | op->Execute(m_native_process); |
| 1210 | return; |
| 1211 | } |
| 1212 | |
| 1213 | // Otherwise we need to pass the operation to the Monitor thread so it can handle it. |
| 1214 | Mutex::Locker lock(m_operation_mutex); |
| 1215 | |
| 1216 | m_operation = op; |
| 1217 | |
| 1218 | // notify the thread that an operation is ready to be processed |
| 1219 | write(m_pipefd[WRITE], &operation_command, sizeof operation_command); |
| 1220 | |
| 1221 | WaitForOperation(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1224 | NativeProcessLinux::Monitor::~Monitor() |
| 1225 | { |
| 1226 | if (m_pipefd[WRITE] >= 0) |
| 1227 | close(m_pipefd[WRITE]); |
| 1228 | if (m_thread.IsJoinable()) |
| 1229 | m_thread.Join(nullptr); |
| 1230 | if (m_pipefd[READ] >= 0) |
| 1231 | close(m_pipefd[READ]); |
| 1232 | if (m_signal_fd >= 0) |
| 1233 | close(m_signal_fd); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1234 | sem_destroy(&m_operation_sem); |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | void |
| 1238 | NativeProcessLinux::Monitor::HandleSignals() |
| 1239 | { |
| 1240 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 1241 | |
| 1242 | // We don't really care about the content of the SIGCHLD siginfo structure, as we will get |
| 1243 | // all the information from waitpid(). We just need to read all the signals so that we can |
| 1244 | // sleep next time we reach select(). |
| 1245 | while (true) |
| 1246 | { |
| 1247 | signalfd_siginfo info; |
| 1248 | ssize_t size = read(m_signal_fd, &info, sizeof info); |
| 1249 | if (size == -1) |
| 1250 | { |
| 1251 | if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 1252 | break; // We are done. |
| 1253 | if (errno == EINTR) |
| 1254 | continue; |
| 1255 | if (log) |
| 1256 | log->Printf("NativeProcessLinux::Monitor::%s reading from signalfd file descriptor failed: %s", |
| 1257 | __FUNCTION__, strerror(errno)); |
| 1258 | break; |
| 1259 | } |
| 1260 | if (size != sizeof info) |
| 1261 | { |
| 1262 | // We got incomplete information structure. This should not happen, let's just log |
| 1263 | // that. |
| 1264 | if (log) |
| 1265 | log->Printf("NativeProcessLinux::Monitor::%s reading from signalfd file descriptor returned incomplete data: " |
| 1266 | "structure size is %zd, read returned %zd bytes", |
| 1267 | __FUNCTION__, sizeof info, size); |
| 1268 | break; |
| 1269 | } |
| 1270 | if (log) |
| 1271 | log->Printf("NativeProcessLinux::Monitor::%s received signal %s(%d).", __FUNCTION__, |
| 1272 | Host::GetSignalAsCString(info.ssi_signo), info.ssi_signo); |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | void |
| 1277 | NativeProcessLinux::Monitor::HandleWait() |
| 1278 | { |
| 1279 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 1280 | // Process all pending waitpid notifications. |
| 1281 | while (true) |
| 1282 | { |
| 1283 | int status = -1; |
| 1284 | ::pid_t wait_pid = waitpid(m_child_pid, &status, __WALL | WNOHANG); |
| 1285 | |
| 1286 | if (wait_pid == 0) |
| 1287 | break; // We are done. |
| 1288 | |
| 1289 | if (wait_pid == -1) |
| 1290 | { |
| 1291 | if (errno == EINTR) |
| 1292 | continue; |
| 1293 | |
| 1294 | if (log) |
| 1295 | log->Printf("NativeProcessLinux::Monitor::%s waitpid (pid = %" PRIi32 ", &status, __WALL | WNOHANG) failed: %s", |
| 1296 | __FUNCTION__, m_child_pid, strerror(errno)); |
| 1297 | break; |
| 1298 | } |
| 1299 | |
| 1300 | bool exited = false; |
| 1301 | int signal = 0; |
| 1302 | int exit_status = 0; |
| 1303 | const char *status_cstr = NULL; |
| 1304 | if (WIFSTOPPED(status)) |
| 1305 | { |
| 1306 | signal = WSTOPSIG(status); |
| 1307 | status_cstr = "STOPPED"; |
| 1308 | } |
| 1309 | else if (WIFEXITED(status)) |
| 1310 | { |
| 1311 | exit_status = WEXITSTATUS(status); |
| 1312 | status_cstr = "EXITED"; |
| 1313 | exited = true; |
| 1314 | } |
| 1315 | else if (WIFSIGNALED(status)) |
| 1316 | { |
| 1317 | signal = WTERMSIG(status); |
| 1318 | status_cstr = "SIGNALED"; |
| 1319 | if (wait_pid == abs(m_child_pid)) { |
| 1320 | exited = true; |
| 1321 | exit_status = -1; |
| 1322 | } |
| 1323 | } |
| 1324 | else |
| 1325 | status_cstr = "(\?\?\?)"; |
| 1326 | |
| 1327 | if (log) |
| 1328 | log->Printf("NativeProcessLinux::Monitor::%s: waitpid (pid = %" PRIi32 ", &status, __WALL | WNOHANG)" |
| 1329 | "=> pid = %" PRIi32 ", status = 0x%8.8x (%s), signal = %i, exit_state = %i", |
| 1330 | __FUNCTION__, m_child_pid, wait_pid, status, status_cstr, signal, exit_status); |
| 1331 | |
| 1332 | m_native_process->MonitorCallback (wait_pid, exited, signal, exit_status); |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | bool |
| 1337 | NativeProcessLinux::Monitor::HandleCommands() |
| 1338 | { |
| 1339 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 1340 | |
| 1341 | while (true) |
| 1342 | { |
| 1343 | char command = 0; |
| 1344 | ssize_t size = read(m_pipefd[READ], &command, sizeof command); |
| 1345 | if (size == -1) |
| 1346 | { |
| 1347 | if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 1348 | return false; |
| 1349 | if (errno == EINTR) |
| 1350 | continue; |
| 1351 | if (log) |
| 1352 | log->Printf("NativeProcessLinux::Monitor::%s exiting because read from command file descriptor failed: %s", __FUNCTION__, strerror(errno)); |
| 1353 | return true; |
| 1354 | } |
| 1355 | if (size == 0) // end of file - write end closed |
| 1356 | { |
| 1357 | if (log) |
| 1358 | log->Printf("NativeProcessLinux::Monitor::%s exit command received, exiting...", __FUNCTION__); |
| 1359 | return true; // We are done. |
| 1360 | } |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1361 | |
| 1362 | switch (command) |
| 1363 | { |
| 1364 | case operation_command: |
| 1365 | m_operation->Execute(m_native_process); |
| 1366 | |
| 1367 | // notify calling thread that operation is complete |
| 1368 | sem_post(&m_operation_sem); |
| 1369 | break; |
| 1370 | default: |
| 1371 | if (log) |
| 1372 | log->Printf("NativeProcessLinux::Monitor::%s received unknown command '%c'", |
| 1373 | __FUNCTION__, command); |
| 1374 | } |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | void |
| 1379 | NativeProcessLinux::Monitor::MainLoop() |
| 1380 | { |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1381 | ::pid_t child_pid = (*m_initial_operation_up)(m_operation_error); |
| 1382 | m_initial_operation_up.reset(); |
| 1383 | m_child_pid = -getpgid(child_pid), |
| 1384 | sem_post(&m_operation_sem); |
| 1385 | |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1386 | while (true) |
| 1387 | { |
| 1388 | fd_set fds; |
| 1389 | FD_ZERO(&fds); |
| 1390 | FD_SET(m_signal_fd, &fds); |
| 1391 | FD_SET(m_pipefd[READ], &fds); |
| 1392 | |
| 1393 | int max_fd = std::max(m_signal_fd, m_pipefd[READ]) + 1; |
| 1394 | int r = select(max_fd, &fds, nullptr, nullptr, nullptr); |
| 1395 | if (r < 0) |
| 1396 | { |
| 1397 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 1398 | if (log) |
| 1399 | log->Printf("NativeProcessLinux::Monitor::%s exiting because select failed: %s", |
| 1400 | __FUNCTION__, strerror(errno)); |
| 1401 | return; |
| 1402 | } |
| 1403 | |
| 1404 | if (FD_ISSET(m_pipefd[READ], &fds)) |
| 1405 | { |
| 1406 | if (HandleCommands()) |
| 1407 | return; |
| 1408 | } |
| 1409 | |
| 1410 | if (FD_ISSET(m_signal_fd, &fds)) |
| 1411 | { |
| 1412 | HandleSignals(); |
| 1413 | HandleWait(); |
| 1414 | } |
| 1415 | } |
| 1416 | } |
| 1417 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1418 | Error |
| 1419 | NativeProcessLinux::Monitor::WaitForOperation() |
| 1420 | { |
| 1421 | Error error; |
| 1422 | while (sem_wait(&m_operation_sem) != 0) |
| 1423 | { |
| 1424 | if (errno == EINTR) |
| 1425 | continue; |
| 1426 | |
| 1427 | error.SetErrorToErrno(); |
| 1428 | return error; |
| 1429 | } |
| 1430 | |
| 1431 | return m_operation_error; |
| 1432 | } |
| 1433 | |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 1434 | void * |
| 1435 | NativeProcessLinux::Monitor::RunMonitor(void *arg) |
| 1436 | { |
| 1437 | static_cast<Monitor *>(arg)->MainLoop(); |
| 1438 | return nullptr; |
| 1439 | } |
| 1440 | |
| 1441 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1442 | NativeProcessLinux::LaunchArgs::LaunchArgs(Module *module, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1443 | char const **argv, |
| 1444 | char const **envp, |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1445 | const std::string &stdin_path, |
| 1446 | const std::string &stdout_path, |
| 1447 | const std::string &stderr_path, |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1448 | const char *working_dir, |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 1449 | const ProcessLaunchInfo &launch_info) |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1450 | : m_module(module), |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1451 | m_argv(argv), |
| 1452 | m_envp(envp), |
| 1453 | m_stdin_path(stdin_path), |
| 1454 | m_stdout_path(stdout_path), |
| 1455 | m_stderr_path(stderr_path), |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1456 | m_working_dir(working_dir), |
| 1457 | m_launch_info(launch_info) |
| 1458 | { |
| 1459 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1460 | |
| 1461 | NativeProcessLinux::LaunchArgs::~LaunchArgs() |
| 1462 | { } |
| 1463 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1464 | // ----------------------------------------------------------------------------- |
| 1465 | // Public Static Methods |
| 1466 | // ----------------------------------------------------------------------------- |
| 1467 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 1468 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1469 | NativeProcessLinux::LaunchProcess ( |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 1470 | Module *exe_module, |
| 1471 | ProcessLaunchInfo &launch_info, |
| 1472 | NativeProcessProtocol::NativeDelegate &native_delegate, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1473 | NativeProcessProtocolSP &native_process_sp) |
| 1474 | { |
| 1475 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1476 | |
| 1477 | Error error; |
| 1478 | |
| 1479 | // Verify the working directory is valid if one was specified. |
| 1480 | const char* working_dir = launch_info.GetWorkingDirectory (); |
| 1481 | if (working_dir) |
| 1482 | { |
| 1483 | FileSpec working_dir_fs (working_dir, true); |
| 1484 | if (!working_dir_fs || working_dir_fs.GetFileType () != FileSpec::eFileTypeDirectory) |
| 1485 | { |
| 1486 | error.SetErrorStringWithFormat ("No such file or directory: %s", working_dir); |
| 1487 | return error; |
| 1488 | } |
| 1489 | } |
| 1490 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 1491 | const FileAction *file_action; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1492 | |
| 1493 | // Default of NULL will mean to use existing open file descriptors. |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1494 | std::string stdin_path; |
| 1495 | std::string stdout_path; |
| 1496 | std::string stderr_path; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1497 | |
| 1498 | file_action = launch_info.GetFileActionForFD (STDIN_FILENO); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1499 | if (file_action) |
| 1500 | stdin_path = file_action->GetPath (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1501 | |
| 1502 | file_action = launch_info.GetFileActionForFD (STDOUT_FILENO); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1503 | if (file_action) |
| 1504 | stdout_path = file_action->GetPath (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1505 | |
| 1506 | file_action = launch_info.GetFileActionForFD (STDERR_FILENO); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1507 | if (file_action) |
| 1508 | stderr_path = file_action->GetPath (); |
| 1509 | |
| 1510 | if (log) |
| 1511 | { |
| 1512 | if (!stdin_path.empty ()) |
| 1513 | log->Printf ("NativeProcessLinux::%s setting STDIN to '%s'", __FUNCTION__, stdin_path.c_str ()); |
| 1514 | else |
| 1515 | log->Printf ("NativeProcessLinux::%s leaving STDIN as is", __FUNCTION__); |
| 1516 | |
| 1517 | if (!stdout_path.empty ()) |
| 1518 | log->Printf ("NativeProcessLinux::%s setting STDOUT to '%s'", __FUNCTION__, stdout_path.c_str ()); |
| 1519 | else |
| 1520 | log->Printf ("NativeProcessLinux::%s leaving STDOUT as is", __FUNCTION__); |
| 1521 | |
| 1522 | if (!stderr_path.empty ()) |
| 1523 | log->Printf ("NativeProcessLinux::%s setting STDERR to '%s'", __FUNCTION__, stderr_path.c_str ()); |
| 1524 | else |
| 1525 | log->Printf ("NativeProcessLinux::%s leaving STDERR as is", __FUNCTION__); |
| 1526 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1527 | |
| 1528 | // Create the NativeProcessLinux in launch mode. |
| 1529 | native_process_sp.reset (new NativeProcessLinux ()); |
| 1530 | |
| 1531 | if (log) |
| 1532 | { |
| 1533 | int i = 0; |
| 1534 | for (const char **args = launch_info.GetArguments ().GetConstArgumentVector (); *args; ++args, ++i) |
| 1535 | { |
| 1536 | log->Printf ("NativeProcessLinux::%s arg %d: \"%s\"", __FUNCTION__, i, *args ? *args : "nullptr"); |
| 1537 | ++i; |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | if (!native_process_sp->RegisterNativeDelegate (native_delegate)) |
| 1542 | { |
| 1543 | native_process_sp.reset (); |
| 1544 | error.SetErrorStringWithFormat ("failed to register the native delegate"); |
| 1545 | return error; |
| 1546 | } |
| 1547 | |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 1548 | std::static_pointer_cast<NativeProcessLinux> (native_process_sp)->LaunchInferior ( |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1549 | exe_module, |
| 1550 | launch_info.GetArguments ().GetConstArgumentVector (), |
| 1551 | launch_info.GetEnvironmentEntries ().GetConstArgumentVector (), |
| 1552 | stdin_path, |
| 1553 | stdout_path, |
| 1554 | stderr_path, |
| 1555 | working_dir, |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1556 | launch_info, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1557 | error); |
| 1558 | |
| 1559 | if (error.Fail ()) |
| 1560 | { |
| 1561 | native_process_sp.reset (); |
| 1562 | if (log) |
| 1563 | log->Printf ("NativeProcessLinux::%s failed to launch process: %s", __FUNCTION__, error.AsCString ()); |
| 1564 | return error; |
| 1565 | } |
| 1566 | |
| 1567 | launch_info.SetProcessID (native_process_sp->GetID ()); |
| 1568 | |
| 1569 | return error; |
| 1570 | } |
| 1571 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 1572 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1573 | NativeProcessLinux::AttachToProcess ( |
| 1574 | lldb::pid_t pid, |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 1575 | NativeProcessProtocol::NativeDelegate &native_delegate, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1576 | NativeProcessProtocolSP &native_process_sp) |
| 1577 | { |
| 1578 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1579 | if (log && log->GetMask ().Test (POSIX_LOG_VERBOSE)) |
| 1580 | log->Printf ("NativeProcessLinux::%s(pid = %" PRIi64 ")", __FUNCTION__, pid); |
| 1581 | |
| 1582 | // Grab the current platform architecture. This should be Linux, |
| 1583 | // since this code is only intended to run on a Linux host. |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 1584 | PlatformSP platform_sp (Platform::GetHostPlatform ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1585 | if (!platform_sp) |
| 1586 | return Error("failed to get a valid default platform"); |
| 1587 | |
| 1588 | // Retrieve the architecture for the running process. |
| 1589 | ArchSpec process_arch; |
| 1590 | Error error = ResolveProcessArchitecture (pid, *platform_sp.get (), process_arch); |
| 1591 | if (!error.Success ()) |
| 1592 | return error; |
| 1593 | |
Oleksiy Vyalov | 1339b5e | 2014-11-13 18:22:16 +0000 | [diff] [blame] | 1594 | std::shared_ptr<NativeProcessLinux> native_process_linux_sp (new NativeProcessLinux ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1595 | |
Oleksiy Vyalov | 1339b5e | 2014-11-13 18:22:16 +0000 | [diff] [blame] | 1596 | if (!native_process_linux_sp->RegisterNativeDelegate (native_delegate)) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1597 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1598 | error.SetErrorStringWithFormat ("failed to register the native delegate"); |
| 1599 | return error; |
| 1600 | } |
| 1601 | |
Oleksiy Vyalov | 1339b5e | 2014-11-13 18:22:16 +0000 | [diff] [blame] | 1602 | native_process_linux_sp->AttachToInferior (pid, error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1603 | if (!error.Success ()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1604 | return error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1605 | |
Oleksiy Vyalov | 1339b5e | 2014-11-13 18:22:16 +0000 | [diff] [blame] | 1606 | native_process_sp = native_process_linux_sp; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1607 | return error; |
| 1608 | } |
| 1609 | |
| 1610 | // ----------------------------------------------------------------------------- |
| 1611 | // Public Instance Methods |
| 1612 | // ----------------------------------------------------------------------------- |
| 1613 | |
| 1614 | NativeProcessLinux::NativeProcessLinux () : |
| 1615 | NativeProcessProtocol (LLDB_INVALID_PROCESS_ID), |
| 1616 | m_arch (), |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1617 | m_supports_mem_region (eLazyBoolCalculate), |
| 1618 | m_mem_region_cache (), |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1619 | m_mem_region_cache_mutex (), |
| 1620 | m_coordinator_up (new ThreadStateCoordinator (GetThreadLoggerFunction ())), |
| 1621 | m_coordinator_thread () |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1622 | { |
| 1623 | } |
| 1624 | |
| 1625 | //------------------------------------------------------------------------------ |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1626 | // NativeProcessLinux spawns a new thread which performs all operations on the inferior process. |
| 1627 | // Refer to Monitor and Operation classes to see why this is necessary. |
| 1628 | //------------------------------------------------------------------------------ |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1629 | void |
| 1630 | NativeProcessLinux::LaunchInferior ( |
| 1631 | Module *module, |
| 1632 | const char *argv[], |
| 1633 | const char *envp[], |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1634 | const std::string &stdin_path, |
| 1635 | const std::string &stdout_path, |
| 1636 | const std::string &stderr_path, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1637 | const char *working_dir, |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 1638 | const ProcessLaunchInfo &launch_info, |
| 1639 | Error &error) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1640 | { |
| 1641 | if (module) |
| 1642 | m_arch = module->GetArchitecture (); |
| 1643 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1644 | SetState (eStateLaunching); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1645 | |
| 1646 | std::unique_ptr<LaunchArgs> args( |
| 1647 | new LaunchArgs( |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1648 | module, argv, envp, |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1649 | stdin_path, stdout_path, stderr_path, |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1650 | working_dir, launch_info)); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1651 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1652 | StartMonitorThread ([&] (Error &e) { return Launch(args.get(), e); }, error); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1653 | if (!error.Success ()) |
| 1654 | return; |
| 1655 | |
| 1656 | error = StartCoordinatorThread (); |
| 1657 | if (!error.Success ()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1658 | return; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | void |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 1662 | NativeProcessLinux::AttachToInferior (lldb::pid_t pid, Error &error) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1663 | { |
| 1664 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1665 | if (log) |
| 1666 | log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ")", __FUNCTION__, pid); |
| 1667 | |
| 1668 | // We can use the Host for everything except the ResolveExecutable portion. |
Greg Clayton | 615eb7e | 2014-09-19 20:11:50 +0000 | [diff] [blame] | 1669 | PlatformSP platform_sp = Platform::GetHostPlatform (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1670 | if (!platform_sp) |
| 1671 | { |
| 1672 | if (log) |
| 1673 | log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 "): no default platform set", __FUNCTION__, pid); |
| 1674 | error.SetErrorString ("no default platform available"); |
Shawn Best | 50d60be | 2014-11-11 00:28:52 +0000 | [diff] [blame] | 1675 | return; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | // Gather info about the process. |
| 1679 | ProcessInstanceInfo process_info; |
Shawn Best | 50d60be | 2014-11-11 00:28:52 +0000 | [diff] [blame] | 1680 | if (!platform_sp->GetProcessInfo (pid, process_info)) |
| 1681 | { |
| 1682 | if (log) |
| 1683 | log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 "): failed to get process info", __FUNCTION__, pid); |
| 1684 | error.SetErrorString ("failed to get process info"); |
| 1685 | return; |
| 1686 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1687 | |
| 1688 | // Resolve the executable module |
| 1689 | ModuleSP exe_module_sp; |
| 1690 | FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths()); |
Chaoren Lin | e56f6dc | 2015-03-01 04:31:16 +0000 | [diff] [blame] | 1691 | ModuleSpec exe_module_spec(process_info.GetExecutableFile(), process_info.GetArchitecture()); |
Oleksiy Vyalov | 6edef20 | 2014-11-17 22:16:42 +0000 | [diff] [blame] | 1692 | error = platform_sp->ResolveExecutable(exe_module_spec, exe_module_sp, |
Zachary Turner | 13b1826 | 2014-08-20 16:42:51 +0000 | [diff] [blame] | 1693 | executable_search_paths.GetSize() ? &executable_search_paths : NULL); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1694 | if (!error.Success()) |
| 1695 | return; |
| 1696 | |
| 1697 | // Set the architecture to the exe architecture. |
| 1698 | m_arch = exe_module_sp->GetArchitecture(); |
| 1699 | if (log) |
| 1700 | log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ") detected architecture %s", __FUNCTION__, pid, m_arch.GetArchitectureName ()); |
| 1701 | |
| 1702 | m_pid = pid; |
| 1703 | SetState(eStateAttaching); |
| 1704 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1705 | StartMonitorThread ([=] (Error &e) { return Attach(pid, e); }, error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1706 | if (!error.Success ()) |
| 1707 | return; |
| 1708 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 1709 | error = StartCoordinatorThread (); |
| 1710 | if (!error.Success ()) |
| 1711 | return; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
Oleksiy Vyalov | 8bc34f4 | 2015-02-19 17:58:04 +0000 | [diff] [blame] | 1714 | void |
| 1715 | NativeProcessLinux::Terminate () |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1716 | { |
| 1717 | StopMonitor(); |
| 1718 | } |
| 1719 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1720 | ::pid_t |
| 1721 | NativeProcessLinux::Launch(LaunchArgs *args, Error &error) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1722 | { |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1723 | assert (args && "null args"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1724 | |
| 1725 | const char **argv = args->m_argv; |
| 1726 | const char **envp = args->m_envp; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1727 | const char *working_dir = args->m_working_dir; |
| 1728 | |
| 1729 | lldb_utility::PseudoTerminal terminal; |
| 1730 | const size_t err_len = 1024; |
| 1731 | char err_str[err_len]; |
| 1732 | lldb::pid_t pid; |
| 1733 | NativeThreadProtocolSP thread_sp; |
| 1734 | |
| 1735 | lldb::ThreadSP inferior; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1736 | |
| 1737 | // Propagate the environment if one is not supplied. |
| 1738 | if (envp == NULL || envp[0] == NULL) |
| 1739 | envp = const_cast<const char **>(environ); |
| 1740 | |
| 1741 | if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t> (-1)) |
| 1742 | { |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1743 | error.SetErrorToGenericError(); |
| 1744 | error.SetErrorStringWithFormat("Process fork failed: %s", err_str); |
| 1745 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | // Recognized child exit status codes. |
| 1749 | enum { |
| 1750 | ePtraceFailed = 1, |
| 1751 | eDupStdinFailed, |
| 1752 | eDupStdoutFailed, |
| 1753 | eDupStderrFailed, |
| 1754 | eChdirFailed, |
| 1755 | eExecFailed, |
| 1756 | eSetGidFailed |
| 1757 | }; |
| 1758 | |
| 1759 | // Child process. |
| 1760 | if (pid == 0) |
| 1761 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1762 | // FIXME consider opening a pipe between parent/child and have this forked child |
| 1763 | // 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] | 1764 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1765 | // Start tracing this child that is about to exec. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1766 | PTRACE(PTRACE_TRACEME, 0, nullptr, nullptr, 0, error); |
| 1767 | if (error.Fail()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1768 | exit(ePtraceFailed); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1769 | |
Pavel Labath | 493c3a1 | 2015-02-04 10:36:57 +0000 | [diff] [blame] | 1770 | // terminal has already dupped the tty descriptors to stdin/out/err. |
| 1771 | // This closes original fd from which they were copied (and avoids |
| 1772 | // leaking descriptors to the debugged process. |
| 1773 | terminal.CloseSlaveFileDescriptor(); |
| 1774 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1775 | // Do not inherit setgid powers. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1776 | if (setgid(getgid()) != 0) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1777 | exit(eSetGidFailed); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1778 | |
| 1779 | // Attempt to have our own process group. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1780 | if (setpgid(0, 0) != 0) |
| 1781 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1782 | // FIXME log that this failed. This is common. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1783 | // Don't allow this to prevent an inferior exec. |
| 1784 | } |
| 1785 | |
| 1786 | // Dup file descriptors if needed. |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1787 | if (!args->m_stdin_path.empty ()) |
| 1788 | if (!DupDescriptor(args->m_stdin_path.c_str (), STDIN_FILENO, O_RDONLY)) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1789 | exit(eDupStdinFailed); |
| 1790 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1791 | if (!args->m_stdout_path.empty ()) |
Tamas Berghammer | 14f4476 | 2015-02-25 13:21:45 +0000 | [diff] [blame] | 1792 | 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] | 1793 | exit(eDupStdoutFailed); |
| 1794 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1795 | if (!args->m_stderr_path.empty ()) |
Tamas Berghammer | 14f4476 | 2015-02-25 13:21:45 +0000 | [diff] [blame] | 1796 | 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] | 1797 | exit(eDupStderrFailed); |
| 1798 | |
| 1799 | // Change working directory |
| 1800 | if (working_dir != NULL && working_dir[0]) |
| 1801 | if (0 != ::chdir(working_dir)) |
| 1802 | exit(eChdirFailed); |
| 1803 | |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1804 | // Disable ASLR if requested. |
| 1805 | if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR)) |
| 1806 | { |
| 1807 | const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS); |
| 1808 | if (old_personality == -1) |
| 1809 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1810 | // Can't retrieve Linux personality. Cannot disable ASLR. |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1811 | } |
| 1812 | else |
| 1813 | { |
| 1814 | const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality); |
| 1815 | if (new_personality == -1) |
| 1816 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1817 | // Disabling ASLR failed. |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1818 | } |
| 1819 | else |
| 1820 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1821 | // Disabling ASLR succeeded. |
Todd Fiala | 0bce1b6 | 2014-08-17 00:10:50 +0000 | [diff] [blame] | 1822 | } |
| 1823 | } |
| 1824 | } |
| 1825 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1826 | // Execute. We should never return... |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1827 | execve(argv[0], |
| 1828 | const_cast<char *const *>(argv), |
| 1829 | const_cast<char *const *>(envp)); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1830 | |
| 1831 | // ...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] | 1832 | exit(eExecFailed); |
| 1833 | } |
| 1834 | |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 1835 | // |
| 1836 | // This is the parent code here. |
| 1837 | // |
| 1838 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1839 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1840 | // Wait for the child process to trap on its call to execve. |
| 1841 | ::pid_t wpid; |
| 1842 | int status; |
| 1843 | if ((wpid = waitpid(pid, &status, 0)) < 0) |
| 1844 | { |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1845 | error.SetErrorToErrno(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1846 | if (log) |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1847 | log->Printf ("NativeProcessLinux::%s waitpid for inferior failed with %s", |
| 1848 | __FUNCTION__, error.AsCString ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1849 | |
| 1850 | // Mark the inferior as invalid. |
| 1851 | // 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] | 1852 | SetState (StateType::eStateInvalid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1853 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1854 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1855 | } |
| 1856 | else if (WIFEXITED(status)) |
| 1857 | { |
| 1858 | // open, dup or execve likely failed for some reason. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1859 | error.SetErrorToGenericError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1860 | switch (WEXITSTATUS(status)) |
| 1861 | { |
| 1862 | case ePtraceFailed: |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1863 | error.SetErrorString("Child ptrace failed."); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1864 | break; |
| 1865 | case eDupStdinFailed: |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1866 | error.SetErrorString("Child open stdin failed."); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1867 | break; |
| 1868 | case eDupStdoutFailed: |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1869 | error.SetErrorString("Child open stdout failed."); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1870 | break; |
| 1871 | case eDupStderrFailed: |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1872 | error.SetErrorString("Child open stderr failed."); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1873 | break; |
| 1874 | case eChdirFailed: |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1875 | error.SetErrorString("Child failed to set working directory."); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1876 | break; |
| 1877 | case eExecFailed: |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1878 | error.SetErrorString("Child exec failed."); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1879 | break; |
| 1880 | case eSetGidFailed: |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1881 | error.SetErrorString("Child setgid failed."); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1882 | break; |
| 1883 | default: |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1884 | error.SetErrorString("Child returned unknown exit status."); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1885 | break; |
| 1886 | } |
| 1887 | |
| 1888 | if (log) |
| 1889 | { |
| 1890 | log->Printf ("NativeProcessLinux::%s inferior exited with status %d before issuing a STOP", |
| 1891 | __FUNCTION__, |
| 1892 | WEXITSTATUS(status)); |
| 1893 | } |
| 1894 | |
| 1895 | // Mark the inferior as invalid. |
| 1896 | // 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] | 1897 | SetState (StateType::eStateInvalid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1898 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1899 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1900 | } |
Todd Fiala | 202ecd2 | 2014-07-10 04:39:13 +0000 | [diff] [blame] | 1901 | assert(WIFSTOPPED(status) && (wpid == static_cast< ::pid_t> (pid)) && |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1902 | "Could not sync with inferior process."); |
| 1903 | |
| 1904 | if (log) |
| 1905 | log->Printf ("NativeProcessLinux::%s inferior started, now in stopped state", __FUNCTION__); |
| 1906 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1907 | error = SetDefaultPtraceOpts(pid); |
| 1908 | if (error.Fail()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1909 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1910 | if (log) |
| 1911 | log->Printf ("NativeProcessLinux::%s inferior failed to set default ptrace options: %s", |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1912 | __FUNCTION__, error.AsCString ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1913 | |
| 1914 | // Mark the inferior as invalid. |
| 1915 | // 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] | 1916 | SetState (StateType::eStateInvalid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1917 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1918 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | // Release the master terminal descriptor and pass it off to the |
| 1922 | // NativeProcessLinux instance. Similarly stash the inferior pid. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1923 | m_terminal_fd = terminal.ReleaseMasterFileDescriptor(); |
| 1924 | m_pid = pid; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1925 | |
| 1926 | // Set the terminal fd to be in non blocking mode (it simplifies the |
| 1927 | // implementation of ProcessLinux::GetSTDOUT to have a non-blocking |
| 1928 | // descriptor to read from). |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1929 | error = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); |
| 1930 | if (error.Fail()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1931 | { |
| 1932 | if (log) |
| 1933 | 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] | 1934 | __FUNCTION__, error.AsCString ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1935 | |
| 1936 | // Mark the inferior as invalid. |
| 1937 | // 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] | 1938 | SetState (StateType::eStateInvalid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1939 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1940 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | if (log) |
| 1944 | log->Printf ("NativeProcessLinux::%s() adding pid = %" PRIu64, __FUNCTION__, pid); |
| 1945 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1946 | thread_sp = AddThread (pid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1947 | assert (thread_sp && "AddThread() returned a nullptr thread"); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1948 | NotifyThreadCreateStopped (pid); |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 1949 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGSTOP); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1950 | |
| 1951 | // Let our process instance know the thread has stopped. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1952 | SetCurrentThreadID (thread_sp->GetID ()); |
| 1953 | SetState (StateType::eStateStopped); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1954 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1955 | if (log) |
| 1956 | { |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1957 | if (error.Success ()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1958 | { |
| 1959 | log->Printf ("NativeProcessLinux::%s inferior launching succeeded", __FUNCTION__); |
| 1960 | } |
| 1961 | else |
| 1962 | { |
| 1963 | log->Printf ("NativeProcessLinux::%s inferior launching failed: %s", |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1964 | __FUNCTION__, error.AsCString ()); |
| 1965 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1966 | } |
| 1967 | } |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1968 | return pid; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1971 | ::pid_t |
| 1972 | NativeProcessLinux::Attach(lldb::pid_t pid, Error &error) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1973 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1974 | lldb::ThreadSP inferior; |
| 1975 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 1976 | |
| 1977 | // Use a map to keep track of the threads which we have attached/need to attach. |
| 1978 | Host::TidMap tids_to_attach; |
| 1979 | if (pid <= 1) |
| 1980 | { |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1981 | error.SetErrorToGenericError(); |
| 1982 | error.SetErrorString("Attaching to process 1 is not allowed."); |
| 1983 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | while (Host::FindProcessThreads(pid, tids_to_attach)) |
| 1987 | { |
| 1988 | for (Host::TidMap::iterator it = tids_to_attach.begin(); |
| 1989 | it != tids_to_attach.end();) |
| 1990 | { |
| 1991 | if (it->second == false) |
| 1992 | { |
| 1993 | lldb::tid_t tid = it->first; |
| 1994 | |
| 1995 | // Attach to the requested process. |
| 1996 | // An attach will cause the thread to stop with a SIGSTOP. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 1997 | PTRACE(PTRACE_ATTACH, tid, nullptr, nullptr, 0, error); |
| 1998 | if (error.Fail()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1999 | { |
| 2000 | // No such thread. The thread may have exited. |
| 2001 | // More error handling may be needed. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2002 | if (error.GetError() == ESRCH) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2003 | { |
| 2004 | it = tids_to_attach.erase(it); |
| 2005 | continue; |
| 2006 | } |
| 2007 | else |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2008 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | int status; |
| 2012 | // Need to use __WALL otherwise we receive an error with errno=ECHLD |
| 2013 | // At this point we should have a thread stopped if waitpid succeeds. |
| 2014 | if ((status = waitpid(tid, NULL, __WALL)) < 0) |
| 2015 | { |
| 2016 | // No such thread. The thread may have exited. |
| 2017 | // More error handling may be needed. |
| 2018 | if (errno == ESRCH) |
| 2019 | { |
| 2020 | it = tids_to_attach.erase(it); |
| 2021 | continue; |
| 2022 | } |
| 2023 | else |
| 2024 | { |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2025 | error.SetErrorToErrno(); |
| 2026 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2027 | } |
| 2028 | } |
| 2029 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2030 | error = SetDefaultPtraceOpts(tid); |
| 2031 | if (error.Fail()) |
| 2032 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2033 | |
| 2034 | if (log) |
| 2035 | log->Printf ("NativeProcessLinux::%s() adding tid = %" PRIu64, __FUNCTION__, tid); |
| 2036 | |
| 2037 | it->second = true; |
| 2038 | |
| 2039 | // Create the thread, mark it as stopped. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2040 | NativeThreadProtocolSP thread_sp (AddThread (static_cast<lldb::tid_t> (tid))); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2041 | assert (thread_sp && "AddThread() returned a nullptr"); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2042 | |
| 2043 | // 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] | 2044 | NotifyThreadCreateStopped (tid); |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2045 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGSTOP); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2046 | SetCurrentThreadID (thread_sp->GetID ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | // move the loop forward |
| 2050 | ++it; |
| 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | if (tids_to_attach.size() > 0) |
| 2055 | { |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2056 | m_pid = pid; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2057 | // Let our process instance know the thread has stopped. |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2058 | SetState (StateType::eStateStopped); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2059 | } |
| 2060 | else |
| 2061 | { |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2062 | error.SetErrorToGenericError(); |
| 2063 | error.SetErrorString("No such process."); |
| 2064 | return -1; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 2067 | return pid; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 2070 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2071 | NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) |
| 2072 | { |
| 2073 | long ptrace_opts = 0; |
| 2074 | |
| 2075 | // Have the child raise an event on exit. This is used to keep the child in |
| 2076 | // limbo until it is destroyed. |
| 2077 | ptrace_opts |= PTRACE_O_TRACEEXIT; |
| 2078 | |
| 2079 | // Have the tracer trace threads which spawn in the inferior process. |
| 2080 | // TODO: if we want to support tracing the inferiors' child, add the |
| 2081 | // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK) |
| 2082 | ptrace_opts |= PTRACE_O_TRACECLONE; |
| 2083 | |
| 2084 | // Have the tracer notify us before execve returns |
| 2085 | // (needed to disable legacy SIGTRAP generation) |
| 2086 | ptrace_opts |= PTRACE_O_TRACEEXEC; |
| 2087 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 2088 | Error error; |
| 2089 | PTRACE(PTRACE_SETOPTIONS, pid, nullptr, (void*)ptrace_opts, 0, error); |
| 2090 | return error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | static ExitType convert_pid_status_to_exit_type (int status) |
| 2094 | { |
| 2095 | if (WIFEXITED (status)) |
| 2096 | return ExitType::eExitTypeExit; |
| 2097 | else if (WIFSIGNALED (status)) |
| 2098 | return ExitType::eExitTypeSignal; |
| 2099 | else if (WIFSTOPPED (status)) |
| 2100 | return ExitType::eExitTypeStop; |
| 2101 | else |
| 2102 | { |
| 2103 | // We don't know what this is. |
| 2104 | return ExitType::eExitTypeInvalid; |
| 2105 | } |
| 2106 | } |
| 2107 | |
| 2108 | static int convert_pid_status_to_return_code (int status) |
| 2109 | { |
| 2110 | if (WIFEXITED (status)) |
| 2111 | return WEXITSTATUS (status); |
| 2112 | else if (WIFSIGNALED (status)) |
| 2113 | return WTERMSIG (status); |
| 2114 | else if (WIFSTOPPED (status)) |
| 2115 | return WSTOPSIG (status); |
| 2116 | else |
| 2117 | { |
| 2118 | // We don't know what this is. |
| 2119 | return ExitType::eExitTypeInvalid; |
| 2120 | } |
| 2121 | } |
| 2122 | |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2123 | // Handles all waitpid events from the inferior process. |
| 2124 | void |
| 2125 | NativeProcessLinux::MonitorCallback(lldb::pid_t pid, |
Tamas Berghammer | 1e209fc | 2015-03-13 11:36:47 +0000 | [diff] [blame] | 2126 | bool exited, |
| 2127 | int signal, |
| 2128 | int status) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2129 | { |
| 2130 | Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 2131 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2132 | // 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] | 2133 | const bool is_main_thread = (pid == GetID ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2134 | |
| 2135 | // Handle when the thread exits. |
| 2136 | if (exited) |
| 2137 | { |
| 2138 | if (log) |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2139 | 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] | 2140 | |
| 2141 | // 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] | 2142 | const bool thread_found = StopTrackingThread (pid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2143 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2144 | // Make sure the thread state coordinator knows about this. |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2145 | NotifyThreadDeath (pid); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2146 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2147 | if (is_main_thread) |
| 2148 | { |
| 2149 | // We only set the exit status and notify the delegate if we haven't already set the process |
| 2150 | // state to an exited state. We normally should have received a SIGTRAP | (PTRACE_EVENT_EXIT << 8) |
| 2151 | // for the main thread. |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2152 | const bool already_notified = (GetState() == StateType::eStateExited) || (GetState () == StateType::eStateCrashed); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2153 | if (!already_notified) |
| 2154 | { |
| 2155 | if (log) |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2156 | 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] | 2157 | // The main thread exited. We're done monitoring. Report to delegate. |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2158 | 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] | 2159 | |
| 2160 | // Notify delegate that our process has exited. |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2161 | SetState (StateType::eStateExited, true); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2162 | } |
| 2163 | else |
| 2164 | { |
| 2165 | if (log) |
| 2166 | log->Printf ("NativeProcessLinux::%s() tid = %" PRIu64 " main thread now exited (%s)", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found"); |
| 2167 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2168 | } |
| 2169 | else |
| 2170 | { |
| 2171 | // Do we want to report to the delegate in this case? I think not. If this was an orderly |
| 2172 | // thread exit, we would already have received the SIGTRAP | (PTRACE_EVENT_EXIT << 8) signal, |
| 2173 | // and we would have done an all-stop then. |
| 2174 | if (log) |
| 2175 | 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] | 2176 | } |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2177 | return; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
| 2180 | // Get details on the signal raised. |
| 2181 | siginfo_t info; |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2182 | const auto err = GetSignalInfo(pid, &info); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 2183 | if (err.Success()) |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2184 | { |
| 2185 | // We have retrieved the signal info. Dispatch appropriately. |
| 2186 | if (info.si_signo == SIGTRAP) |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2187 | MonitorSIGTRAP(&info, pid); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2188 | else |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2189 | MonitorSignal(&info, pid, exited); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2190 | } |
| 2191 | else |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2192 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 2193 | if (err.GetError() == EINVAL) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2194 | { |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2195 | // This is a group stop reception for this tid. |
| 2196 | if (log) |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2197 | log->Printf ("NativeThreadLinux::%s received a group stop for pid %" PRIu64 " tid %" PRIu64, __FUNCTION__, GetID (), pid); |
| 2198 | NotifyThreadStop (pid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2199 | } |
| 2200 | else |
| 2201 | { |
| 2202 | // ptrace(GETSIGINFO) failed (but not due to group-stop). |
| 2203 | |
| 2204 | // A return value of ESRCH means the thread/process is no longer on the system, |
| 2205 | // so it was killed somehow outside of our control. Either way, we can't do anything |
| 2206 | // with it anymore. |
| 2207 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2208 | // 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] | 2209 | const bool thread_found = StopTrackingThread (pid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2210 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2211 | // Make sure the thread state coordinator knows about this. |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2212 | NotifyThreadDeath (pid); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2213 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2214 | if (log) |
| 2215 | 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] | 2216 | __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] | 2217 | |
| 2218 | if (is_main_thread) |
| 2219 | { |
| 2220 | // Notify the delegate - our process is not available but appears to have been killed outside |
| 2221 | // our control. Is eStateExited the right exit state in this case? |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2222 | SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); |
| 2223 | SetState (StateType::eStateExited, true); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2224 | } |
| 2225 | else |
| 2226 | { |
| 2227 | // This thread was pulled out from underneath us. Anything to do here? Do we want to do an all stop? |
| 2228 | if (log) |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 2229 | 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] | 2230 | } |
| 2231 | } |
| 2232 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | void |
| 2236 | NativeProcessLinux::MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid) |
| 2237 | { |
| 2238 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 2239 | const bool is_main_thread = (pid == GetID ()); |
| 2240 | |
| 2241 | assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!"); |
| 2242 | if (!info) |
| 2243 | return; |
| 2244 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2245 | Mutex::Locker locker (m_threads_mutex); |
| 2246 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2247 | // See if we can find a thread for this signal. |
| 2248 | NativeThreadProtocolSP thread_sp = GetThreadByID (pid); |
| 2249 | if (!thread_sp) |
| 2250 | { |
| 2251 | if (log) |
| 2252 | log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " no thread found for tid %" PRIu64, __FUNCTION__, GetID (), pid); |
| 2253 | } |
| 2254 | |
| 2255 | switch (info->si_code) |
| 2256 | { |
| 2257 | // TODO: these two cases are required if we want to support tracing of the inferiors' children. We'd need this to debug a monitor. |
| 2258 | // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)): |
| 2259 | // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)): |
| 2260 | |
| 2261 | case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)): |
| 2262 | { |
Pavel Labath | 5fd24c6 | 2015-04-23 09:04:35 +0000 | [diff] [blame^] | 2263 | // This is the notification on the parent thread which informs us of new thread |
| 2264 | // creation. We are not interested in these events at this point (an interesting use |
| 2265 | // case would be to stop the process upon thread creation), so we just resume the thread. |
| 2266 | // We will pickup the new thread when we get its SIGSTOP notification. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2267 | |
Pavel Labath | 5fd24c6 | 2015-04-23 09:04:35 +0000 | [diff] [blame^] | 2268 | if (log) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2269 | { |
Pavel Labath | 5fd24c6 | 2015-04-23 09:04:35 +0000 | [diff] [blame^] | 2270 | unsigned long event_message = 0; |
| 2271 | if (GetEventMessage (pid, &event_message).Success()) |
| 2272 | { |
| 2273 | lldb::tid_t tid = static_cast<lldb::tid_t> (event_message); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2274 | log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " received thread creation event for tid %" PRIu64, __FUNCTION__, pid, tid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2275 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2276 | } |
| 2277 | else |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2278 | log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " received thread creation event but GetEventMessage failed so we don't know the new tid", __FUNCTION__, pid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
Pavel Labath | 5fd24c6 | 2015-04-23 09:04:35 +0000 | [diff] [blame^] | 2281 | Resume (pid, LLDB_INVALID_SIGNAL_NUMBER); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2282 | break; |
| 2283 | } |
| 2284 | |
| 2285 | case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2286 | { |
| 2287 | NativeThreadProtocolSP main_thread_sp; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2288 | if (log) |
| 2289 | 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] | 2290 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2291 | // The thread state coordinator needs to reset due to the exec. |
| 2292 | m_coordinator_up->ResetForExec (); |
| 2293 | |
| 2294 | // 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] | 2295 | if (log) |
| 2296 | log->Printf ("NativeProcessLinux::%s exec received, stop tracking all but main thread", __FUNCTION__); |
| 2297 | |
| 2298 | for (auto thread_sp : m_threads) |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2299 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2300 | const bool is_main_thread = thread_sp && thread_sp->GetID () == GetID (); |
| 2301 | if (is_main_thread) |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2302 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2303 | main_thread_sp = thread_sp; |
| 2304 | if (log) |
| 2305 | 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] | 2306 | } |
| 2307 | else |
| 2308 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2309 | // Tell thread coordinator this thread is dead. |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2310 | if (log) |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2311 | 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] | 2312 | } |
| 2313 | } |
| 2314 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2315 | m_threads.clear (); |
| 2316 | |
| 2317 | if (main_thread_sp) |
| 2318 | { |
| 2319 | m_threads.push_back (main_thread_sp); |
| 2320 | SetCurrentThreadID (main_thread_sp->GetID ()); |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2321 | std::static_pointer_cast<NativeThreadLinux> (main_thread_sp)->SetStoppedByExec (); |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2322 | } |
| 2323 | else |
| 2324 | { |
| 2325 | SetCurrentThreadID (LLDB_INVALID_THREAD_ID); |
| 2326 | if (log) |
| 2327 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 "no main thread found, discarded all threads, we're in a no-thread state!", __FUNCTION__, GetID ()); |
| 2328 | } |
| 2329 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2330 | // Tell coordinator about about the "new" (since exec) stopped main thread. |
| 2331 | const lldb::tid_t main_thread_tid = GetID (); |
| 2332 | NotifyThreadCreateStopped (main_thread_tid); |
| 2333 | |
| 2334 | // NOTE: ideally these next statements would execute at the same time as the coordinator thread create was executed. |
| 2335 | // Consider a handler that can execute when that happens. |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2336 | // Let our delegate know we have just exec'd. |
| 2337 | NotifyDidExec (); |
| 2338 | |
| 2339 | // If we have a main thread, indicate we are stopped. |
| 2340 | 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] | 2341 | |
| 2342 | // Let the process know we're stopped. |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2343 | CallAfterRunningThreadsStop (pid, |
| 2344 | [=] (lldb::tid_t signaling_tid) |
| 2345 | { |
| 2346 | SetState (StateType::eStateStopped, true); |
| 2347 | }); |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2348 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2349 | break; |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 2350 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2351 | |
| 2352 | case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): |
| 2353 | { |
| 2354 | // The inferior process or one of its threads is about to exit. |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2355 | |
| 2356 | // This thread is currently stopped. It's not actually dead yet, just about to be. |
| 2357 | NotifyThreadStop (pid); |
| 2358 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2359 | unsigned long data = 0; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 2360 | if (GetEventMessage(pid, &data).Fail()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2361 | data = -1; |
| 2362 | |
| 2363 | if (log) |
| 2364 | { |
| 2365 | log->Printf ("NativeProcessLinux::%s() received PTRACE_EVENT_EXIT, data = %lx (WIFEXITED=%s,WIFSIGNALED=%s), pid = %" PRIu64 " (%s)", |
| 2366 | __FUNCTION__, |
| 2367 | data, WIFEXITED (data) ? "true" : "false", WIFSIGNALED (data) ? "true" : "false", |
| 2368 | pid, |
| 2369 | is_main_thread ? "is main thread" : "not main thread"); |
| 2370 | } |
| 2371 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2372 | if (is_main_thread) |
| 2373 | { |
| 2374 | 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] | 2375 | } |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 2376 | |
Chaoren Lin | 9d617ba | 2015-02-03 01:50:54 +0000 | [diff] [blame] | 2377 | const int signo = static_cast<int> (data); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2378 | m_coordinator_up->RequestThreadResume (pid, |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2379 | [=](lldb::tid_t tid_to_resume, bool supress_signal) |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2380 | { |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2381 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); |
Chaoren Lin | 37c768c | 2015-02-03 01:51:30 +0000 | [diff] [blame] | 2382 | return Resume (tid_to_resume, (supress_signal) ? LLDB_INVALID_SIGNAL_NUMBER : signo); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2383 | }, |
| 2384 | CoordinatorErrorHandler); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2385 | |
| 2386 | break; |
| 2387 | } |
| 2388 | |
| 2389 | case 0: |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 2390 | case TRAP_TRACE: // We receive this on single stepping. |
| 2391 | case TRAP_HWBKPT: // We receive this on watchpoint hit |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2392 | if (thread_sp) |
| 2393 | { |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 2394 | // If a watchpoint was hit, report it |
| 2395 | uint32_t wp_index; |
| 2396 | Error error = thread_sp->GetRegisterContext()->GetWatchpointHitIndex(wp_index); |
| 2397 | if (error.Fail() && log) |
| 2398 | log->Printf("NativeProcessLinux::%s() " |
| 2399 | "received error while checking for watchpoint hits, " |
| 2400 | "pid = %" PRIu64 " error = %s", |
| 2401 | __FUNCTION__, pid, error.AsCString()); |
| 2402 | if (wp_index != LLDB_INVALID_INDEX32) |
| 2403 | { |
| 2404 | MonitorWatchpoint(pid, thread_sp, wp_index); |
| 2405 | break; |
| 2406 | } |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2407 | } |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 2408 | // Otherwise, report step over |
| 2409 | MonitorTrace(pid, thread_sp); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2410 | break; |
| 2411 | |
| 2412 | case SI_KERNEL: |
| 2413 | case TRAP_BRKPT: |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 2414 | MonitorBreakpoint(pid, thread_sp); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2415 | break; |
| 2416 | |
| 2417 | case SIGTRAP: |
| 2418 | case (SIGTRAP | 0x80): |
| 2419 | if (log) |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2420 | log->Printf ("NativeProcessLinux::%s() received unknown SIGTRAP system call stop event, pid %" PRIu64 "tid %" PRIu64 ", resuming", __FUNCTION__, GetID (), pid); |
| 2421 | |
| 2422 | // This thread is currently stopped. |
| 2423 | NotifyThreadStop (pid); |
| 2424 | if (thread_sp) |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2425 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGTRAP); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2426 | |
| 2427 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2428 | // Ignore these signals until we know more about them. |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2429 | m_coordinator_up->RequestThreadResume (pid, |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2430 | [=](lldb::tid_t tid_to_resume, bool supress_signal) |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2431 | { |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2432 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); |
Chaoren Lin | 37c768c | 2015-02-03 01:51:30 +0000 | [diff] [blame] | 2433 | return Resume (tid_to_resume, LLDB_INVALID_SIGNAL_NUMBER); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2434 | }, |
| 2435 | CoordinatorErrorHandler); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2436 | break; |
| 2437 | |
| 2438 | default: |
| 2439 | assert(false && "Unexpected SIGTRAP code!"); |
| 2440 | if (log) |
| 2441 | 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))); |
| 2442 | break; |
| 2443 | |
| 2444 | } |
| 2445 | } |
| 2446 | |
| 2447 | void |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 2448 | NativeProcessLinux::MonitorTrace(lldb::pid_t pid, NativeThreadProtocolSP thread_sp) |
| 2449 | { |
| 2450 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 2451 | if (log) |
| 2452 | log->Printf("NativeProcessLinux::%s() received trace event, pid = %" PRIu64 " (single stepping)", |
| 2453 | __FUNCTION__, pid); |
| 2454 | |
| 2455 | if (thread_sp) |
| 2456 | std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByTrace(); |
| 2457 | |
| 2458 | // This thread is currently stopped. |
| 2459 | NotifyThreadStop(pid); |
| 2460 | |
| 2461 | // Here we don't have to request the rest of the threads to stop or request a deferred stop. |
| 2462 | // This would have already happened at the time the Resume() with step operation was signaled. |
| 2463 | // At this point, we just need to say we stopped, and the deferred notifcation will fire off |
| 2464 | // once all running threads have checked in as stopped. |
| 2465 | SetCurrentThreadID(pid); |
| 2466 | // Tell the process we have a stop (from software breakpoint). |
| 2467 | CallAfterRunningThreadsStop(pid, |
| 2468 | [=](lldb::tid_t signaling_tid) |
| 2469 | { |
| 2470 | SetState(StateType::eStateStopped, true); |
| 2471 | }); |
| 2472 | } |
| 2473 | |
| 2474 | void |
| 2475 | NativeProcessLinux::MonitorBreakpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp) |
| 2476 | { |
| 2477 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); |
| 2478 | if (log) |
| 2479 | log->Printf("NativeProcessLinux::%s() received breakpoint event, pid = %" PRIu64, |
| 2480 | __FUNCTION__, pid); |
| 2481 | |
| 2482 | // This thread is currently stopped. |
| 2483 | NotifyThreadStop(pid); |
| 2484 | |
| 2485 | // Mark the thread as stopped at breakpoint. |
| 2486 | if (thread_sp) |
| 2487 | { |
| 2488 | std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByBreakpoint(); |
| 2489 | Error error = FixupBreakpointPCAsNeeded(thread_sp); |
| 2490 | if (error.Fail()) |
| 2491 | if (log) |
| 2492 | log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " fixup: %s", |
| 2493 | __FUNCTION__, pid, error.AsCString()); |
Tamas Berghammer | d8c338d | 2015-04-15 09:47:02 +0000 | [diff] [blame] | 2494 | |
| 2495 | auto it = m_threads_stepping_with_breakpoint.find(pid); |
| 2496 | if (it != m_threads_stepping_with_breakpoint.end()) |
| 2497 | { |
| 2498 | Error error = RemoveBreakpoint (it->second); |
| 2499 | if (error.Fail()) |
| 2500 | if (log) |
| 2501 | log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " remove stepping breakpoint: %s", |
| 2502 | __FUNCTION__, pid, error.AsCString()); |
| 2503 | |
| 2504 | m_threads_stepping_with_breakpoint.erase(it); |
| 2505 | std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByTrace(); |
| 2506 | } |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 2507 | } |
| 2508 | else |
| 2509 | if (log) |
| 2510 | log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 ": " |
| 2511 | "warning, cannot process software breakpoint since no thread metadata", |
| 2512 | __FUNCTION__, pid); |
| 2513 | |
| 2514 | |
| 2515 | // We need to tell all other running threads before we notify the delegate about this stop. |
| 2516 | CallAfterRunningThreadsStop(pid, |
| 2517 | [=](lldb::tid_t deferred_notification_tid) |
| 2518 | { |
| 2519 | SetCurrentThreadID(deferred_notification_tid); |
| 2520 | // Tell the process we have a stop (from software breakpoint). |
| 2521 | SetState(StateType::eStateStopped, true); |
| 2522 | }); |
| 2523 | } |
| 2524 | |
| 2525 | void |
| 2526 | NativeProcessLinux::MonitorWatchpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp, uint32_t wp_index) |
| 2527 | { |
| 2528 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_WATCHPOINTS)); |
| 2529 | if (log) |
| 2530 | log->Printf("NativeProcessLinux::%s() received watchpoint event, " |
| 2531 | "pid = %" PRIu64 ", wp_index = %" PRIu32, |
| 2532 | __FUNCTION__, pid, wp_index); |
| 2533 | |
| 2534 | // This thread is currently stopped. |
| 2535 | NotifyThreadStop(pid); |
| 2536 | |
| 2537 | // Mark the thread as stopped at watchpoint. |
| 2538 | // The address is at (lldb::addr_t)info->si_addr if we need it. |
| 2539 | lldbassert(thread_sp && "thread_sp cannot be NULL"); |
| 2540 | std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByWatchpoint(wp_index); |
| 2541 | |
| 2542 | // We need to tell all other running threads before we notify the delegate about this stop. |
| 2543 | CallAfterRunningThreadsStop(pid, |
| 2544 | [=](lldb::tid_t deferred_notification_tid) |
| 2545 | { |
| 2546 | SetCurrentThreadID(deferred_notification_tid); |
| 2547 | // Tell the process we have a stop (from watchpoint). |
| 2548 | SetState(StateType::eStateStopped, true); |
| 2549 | }); |
| 2550 | } |
| 2551 | |
| 2552 | void |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2553 | NativeProcessLinux::MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited) |
| 2554 | { |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 2555 | assert (info && "null info"); |
| 2556 | if (!info) |
| 2557 | return; |
| 2558 | |
| 2559 | const int signo = info->si_signo; |
| 2560 | const bool is_from_llgs = info->si_pid == getpid (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2561 | |
| 2562 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 2563 | |
| 2564 | // POSIX says that process behaviour is undefined after it ignores a SIGFPE, |
| 2565 | // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a |
| 2566 | // kill(2) or raise(3). Similarly for tgkill(2) on Linux. |
| 2567 | // |
| 2568 | // IOW, user generated signals never generate what we consider to be a |
| 2569 | // "crash". |
| 2570 | // |
| 2571 | // Similarly, ACK signals generated by this monitor. |
| 2572 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2573 | Mutex::Locker locker (m_threads_mutex); |
| 2574 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2575 | // See if we can find a thread for this signal. |
| 2576 | NativeThreadProtocolSP thread_sp = GetThreadByID (pid); |
| 2577 | if (!thread_sp) |
| 2578 | { |
| 2579 | if (log) |
| 2580 | log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " no thread found for tid %" PRIu64, __FUNCTION__, GetID (), pid); |
| 2581 | } |
| 2582 | |
| 2583 | // Handle the signal. |
| 2584 | if (info->si_code == SI_TKILL || info->si_code == SI_USER) |
| 2585 | { |
| 2586 | if (log) |
| 2587 | log->Printf ("NativeProcessLinux::%s() received signal %s (%d) with code %s, (siginfo pid = %d (%s), waitpid pid = %" PRIu64 ")", |
| 2588 | __FUNCTION__, |
| 2589 | GetUnixSignals ().GetSignalAsCString (signo), |
| 2590 | signo, |
| 2591 | (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"), |
| 2592 | info->si_pid, |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 2593 | is_from_llgs ? "from llgs" : "not from llgs", |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2594 | pid); |
Todd Fiala | 58a2f66 | 2014-08-12 17:02:07 +0000 | [diff] [blame] | 2595 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2596 | |
Todd Fiala | 58a2f66 | 2014-08-12 17:02:07 +0000 | [diff] [blame] | 2597 | // Check for new thread notification. |
| 2598 | if ((info->si_pid == 0) && (info->si_code == SI_USER)) |
| 2599 | { |
| 2600 | // A new thread creation is being signaled. This is one of two parts that come in |
| 2601 | // a non-deterministic order. pid is the thread id. |
| 2602 | if (log) |
| 2603 | log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 " tid %" PRIu64 ": new thread notification", |
| 2604 | __FUNCTION__, GetID (), pid); |
| 2605 | |
Pavel Labath | 5fd24c6 | 2015-04-23 09:04:35 +0000 | [diff] [blame^] | 2606 | thread_sp = AddThread(pid); |
| 2607 | assert (thread_sp.get() && "failed to create the tracking data for newly created inferior thread"); |
| 2608 | // We can now resume the newly created thread. |
| 2609 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); |
| 2610 | Resume (pid, LLDB_INVALID_SIGNAL_NUMBER); |
| 2611 | m_coordinator_up->NotifyThreadCreate (pid, false, CoordinatorErrorHandler); |
Todd Fiala | 58a2f66 | 2014-08-12 17:02:07 +0000 | [diff] [blame] | 2612 | // Done handling. |
| 2613 | return; |
| 2614 | } |
| 2615 | |
| 2616 | // Check for thread stop notification. |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 2617 | if (is_from_llgs && (info->si_code == SI_TKILL) && (signo == SIGSTOP)) |
Todd Fiala | 58a2f66 | 2014-08-12 17:02:07 +0000 | [diff] [blame] | 2618 | { |
| 2619 | // This is a tgkill()-based stop. |
| 2620 | if (thread_sp) |
| 2621 | { |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2622 | if (log) |
| 2623 | log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 ", thread stopped", |
| 2624 | __FUNCTION__, |
| 2625 | GetID (), |
| 2626 | pid); |
| 2627 | |
Chaoren Lin | aab5863 | 2015-02-03 01:50:57 +0000 | [diff] [blame] | 2628 | // Check that we're not already marked with a stop reason. |
| 2629 | // Note this thread really shouldn't already be marked as stopped - if we were, that would imply that |
| 2630 | // the kernel signaled us with the thread stopping which we handled and marked as stopped, |
| 2631 | // and that, without an intervening resume, we received another stop. It is more likely |
| 2632 | // that we are missing the marking of a run state somewhere if we find that the thread was |
| 2633 | // marked as stopped. |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2634 | std::shared_ptr<NativeThreadLinux> linux_thread_sp = std::static_pointer_cast<NativeThreadLinux> (thread_sp); |
| 2635 | assert (linux_thread_sp && "linux_thread_sp is null!"); |
Chaoren Lin | aab5863 | 2015-02-03 01:50:57 +0000 | [diff] [blame] | 2636 | |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2637 | const StateType thread_state = linux_thread_sp->GetState (); |
Chaoren Lin | aab5863 | 2015-02-03 01:50:57 +0000 | [diff] [blame] | 2638 | if (!StateIsStoppedState (thread_state, false)) |
| 2639 | { |
| 2640 | // An inferior thread just stopped, but was not the primary cause of the process stop. |
| 2641 | // Instead, something else (like a breakpoint or step) caused the stop. Mark the |
| 2642 | // stop signal as 0 to let lldb know this isn't the important stop. |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2643 | linux_thread_sp->SetStoppedBySignal (0); |
Chaoren Lin | aab5863 | 2015-02-03 01:50:57 +0000 | [diff] [blame] | 2644 | SetCurrentThreadID (thread_sp->GetID ()); |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2645 | m_coordinator_up->NotifyThreadStop (thread_sp->GetID (), true, CoordinatorErrorHandler); |
Chaoren Lin | aab5863 | 2015-02-03 01:50:57 +0000 | [diff] [blame] | 2646 | } |
| 2647 | else |
| 2648 | { |
| 2649 | if (log) |
| 2650 | { |
| 2651 | // Retrieve the signal name if the thread was stopped by a signal. |
| 2652 | int stop_signo = 0; |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2653 | const bool stopped_by_signal = linux_thread_sp->IsStopped (&stop_signo); |
Chaoren Lin | aab5863 | 2015-02-03 01:50:57 +0000 | [diff] [blame] | 2654 | const char *signal_name = stopped_by_signal ? GetUnixSignals ().GetSignalAsCString (stop_signo) : "<not stopped by signal>"; |
| 2655 | if (!signal_name) |
| 2656 | signal_name = "<no-signal-name>"; |
| 2657 | |
| 2658 | 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", |
| 2659 | __FUNCTION__, |
| 2660 | GetID (), |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2661 | linux_thread_sp->GetID (), |
Chaoren Lin | aab5863 | 2015-02-03 01:50:57 +0000 | [diff] [blame] | 2662 | StateAsCString (thread_state), |
| 2663 | stop_signo, |
| 2664 | signal_name); |
| 2665 | } |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2666 | // Tell the thread state coordinator about the stop. |
| 2667 | NotifyThreadStop (thread_sp->GetID ()); |
Chaoren Lin | aab5863 | 2015-02-03 01:50:57 +0000 | [diff] [blame] | 2668 | } |
Todd Fiala | 58a2f66 | 2014-08-12 17:02:07 +0000 | [diff] [blame] | 2669 | } |
| 2670 | |
| 2671 | // Done handling. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2672 | return; |
| 2673 | } |
| 2674 | |
| 2675 | if (log) |
| 2676 | log->Printf ("NativeProcessLinux::%s() received signal %s", __FUNCTION__, GetUnixSignals ().GetSignalAsCString (signo)); |
| 2677 | |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2678 | // This thread is stopped. |
| 2679 | NotifyThreadStop (pid); |
| 2680 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2681 | switch (signo) |
| 2682 | { |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 2683 | case SIGSTOP: |
| 2684 | { |
| 2685 | if (log) |
| 2686 | { |
| 2687 | if (is_from_llgs) |
| 2688 | log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " received SIGSTOP from llgs, most likely an interrupt", __FUNCTION__, GetID (), pid); |
| 2689 | else |
| 2690 | log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " received SIGSTOP from outside of debugger", __FUNCTION__, GetID (), pid); |
| 2691 | } |
| 2692 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2693 | // Resume this thread to get the group-stop mechanism to fire off the true group stops. |
| 2694 | // This thread will get stopped again as part of the group-stop completion. |
| 2695 | m_coordinator_up->RequestThreadResume (pid, |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2696 | [=](lldb::tid_t tid_to_resume, bool supress_signal) |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2697 | { |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2698 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2699 | // Pass this signal number on to the inferior to handle. |
Chaoren Lin | 37c768c | 2015-02-03 01:51:30 +0000 | [diff] [blame] | 2700 | return Resume (tid_to_resume, (supress_signal) ? LLDB_INVALID_SIGNAL_NUMBER : signo); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2701 | }, |
| 2702 | CoordinatorErrorHandler); |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 2703 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2704 | break; |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2705 | case SIGSEGV: |
| 2706 | case SIGILL: |
| 2707 | case SIGFPE: |
| 2708 | case SIGBUS: |
| 2709 | if (thread_sp) |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2710 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetCrashedWithException (*info); |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2711 | break; |
| 2712 | default: |
| 2713 | // This is just a pre-signal-delivery notification of the incoming signal. |
| 2714 | if (thread_sp) |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2715 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (signo); |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2716 | |
| 2717 | break; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2718 | } |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2719 | |
| 2720 | // Send a stop to the debugger after we get all other threads to stop. |
| 2721 | CallAfterRunningThreadsStop (pid, |
| 2722 | [=] (lldb::tid_t signaling_tid) |
| 2723 | { |
| 2724 | SetCurrentThreadID (signaling_tid); |
| 2725 | SetState (StateType::eStateStopped, true); |
| 2726 | }); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2727 | } |
| 2728 | |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 2729 | namespace { |
| 2730 | |
| 2731 | struct EmulatorBaton |
| 2732 | { |
| 2733 | NativeProcessLinux* m_process; |
| 2734 | NativeRegisterContext* m_reg_context; |
| 2735 | RegisterValue m_pc; |
| 2736 | RegisterValue m_flags; |
| 2737 | |
| 2738 | EmulatorBaton(NativeProcessLinux* process, NativeRegisterContext* reg_context) : |
| 2739 | m_process(process), m_reg_context(reg_context) {} |
| 2740 | }; |
| 2741 | |
| 2742 | } // anonymous namespace |
| 2743 | |
| 2744 | static size_t |
| 2745 | ReadMemoryCallback (EmulateInstruction *instruction, |
| 2746 | void *baton, |
| 2747 | const EmulateInstruction::Context &context, |
| 2748 | lldb::addr_t addr, |
| 2749 | void *dst, |
| 2750 | size_t length) |
| 2751 | { |
| 2752 | EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); |
| 2753 | |
| 2754 | lldb::addr_t bytes_read; |
| 2755 | emulator_baton->m_process->ReadMemory(addr, dst, length, bytes_read); |
| 2756 | return bytes_read; |
| 2757 | } |
| 2758 | |
| 2759 | static bool |
| 2760 | ReadRegisterCallback (EmulateInstruction *instruction, |
| 2761 | void *baton, |
| 2762 | const RegisterInfo *reg_info, |
| 2763 | RegisterValue ®_value) |
| 2764 | { |
| 2765 | EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); |
| 2766 | |
| 2767 | // The emulator only fill in the dwarf regsiter numbers (and in some case |
| 2768 | // the generic register numbers). Get the full register info from the |
| 2769 | // register context based on the dwarf register numbers. |
| 2770 | const RegisterInfo* full_reg_info = emulator_baton->m_reg_context->GetRegisterInfo( |
| 2771 | eRegisterKindDWARF, reg_info->kinds[eRegisterKindDWARF]); |
| 2772 | |
| 2773 | Error error = emulator_baton->m_reg_context->ReadRegister(full_reg_info, reg_value); |
| 2774 | return error.Success(); |
| 2775 | } |
| 2776 | |
| 2777 | static bool |
| 2778 | WriteRegisterCallback (EmulateInstruction *instruction, |
| 2779 | void *baton, |
| 2780 | const EmulateInstruction::Context &context, |
| 2781 | const RegisterInfo *reg_info, |
| 2782 | const RegisterValue ®_value) |
| 2783 | { |
| 2784 | EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); |
| 2785 | |
| 2786 | switch (reg_info->kinds[eRegisterKindGeneric]) |
| 2787 | { |
| 2788 | case LLDB_REGNUM_GENERIC_PC: |
| 2789 | emulator_baton->m_pc = reg_value; |
| 2790 | break; |
| 2791 | case LLDB_REGNUM_GENERIC_FLAGS: |
| 2792 | emulator_baton->m_flags = reg_value; |
| 2793 | break; |
| 2794 | } |
| 2795 | |
| 2796 | return true; |
| 2797 | } |
| 2798 | |
| 2799 | static size_t |
| 2800 | WriteMemoryCallback (EmulateInstruction *instruction, |
| 2801 | void *baton, |
| 2802 | const EmulateInstruction::Context &context, |
| 2803 | lldb::addr_t addr, |
| 2804 | const void *dst, |
| 2805 | size_t length) |
| 2806 | { |
| 2807 | return length; |
| 2808 | } |
| 2809 | |
| 2810 | static lldb::addr_t |
| 2811 | ReadFlags (NativeRegisterContext* regsiter_context) |
| 2812 | { |
| 2813 | const RegisterInfo* flags_info = regsiter_context->GetRegisterInfo( |
| 2814 | eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); |
| 2815 | return regsiter_context->ReadRegisterAsUnsigned(flags_info, LLDB_INVALID_ADDRESS); |
| 2816 | } |
| 2817 | |
| 2818 | Error |
| 2819 | NativeProcessLinux::SetupSoftwareSingleStepping(NativeThreadProtocolSP thread_sp) |
| 2820 | { |
| 2821 | Error error; |
| 2822 | NativeRegisterContextSP register_context_sp = thread_sp->GetRegisterContext(); |
| 2823 | |
| 2824 | std::unique_ptr<EmulateInstruction> emulator_ap( |
| 2825 | EmulateInstruction::FindPlugin(m_arch, eInstructionTypePCModifying, nullptr)); |
| 2826 | |
| 2827 | if (emulator_ap == nullptr) |
| 2828 | return Error("Instruction emulator not found!"); |
| 2829 | |
| 2830 | EmulatorBaton baton(this, register_context_sp.get()); |
| 2831 | emulator_ap->SetBaton(&baton); |
| 2832 | emulator_ap->SetReadMemCallback(&ReadMemoryCallback); |
| 2833 | emulator_ap->SetReadRegCallback(&ReadRegisterCallback); |
| 2834 | emulator_ap->SetWriteMemCallback(&WriteMemoryCallback); |
| 2835 | emulator_ap->SetWriteRegCallback(&WriteRegisterCallback); |
| 2836 | |
| 2837 | if (!emulator_ap->ReadInstruction()) |
| 2838 | return Error("Read instruction failed!"); |
| 2839 | |
| 2840 | lldb::addr_t next_pc; |
| 2841 | lldb::addr_t next_flags; |
| 2842 | if (emulator_ap->EvaluateInstruction(eEmulateInstructionOptionAutoAdvancePC)) |
| 2843 | { |
| 2844 | next_pc = baton.m_pc.GetAsUInt64(); |
| 2845 | if (baton.m_flags.GetType() != RegisterValue::eTypeInvalid) |
| 2846 | next_flags = baton.m_flags.GetAsUInt32(); |
| 2847 | else |
| 2848 | next_flags = ReadFlags (register_context_sp.get()); |
| 2849 | } |
| 2850 | else if (baton.m_pc.GetType() == RegisterValue::eTypeInvalid) |
| 2851 | { |
| 2852 | // Emulate instruction failed and it haven't changed PC. Advance PC |
| 2853 | // with the size of the current opcode because the emulation of all |
| 2854 | // PC modifying instruction should be successful. The failure most |
| 2855 | // likely caused by a not supported instruction which don't modify PC. |
| 2856 | next_pc = register_context_sp->GetPC() + emulator_ap->GetOpcode().GetByteSize(); |
| 2857 | next_flags = ReadFlags (register_context_sp.get()); |
| 2858 | } |
| 2859 | else |
| 2860 | { |
| 2861 | // The instruction emulation failed after it modified the PC. It is an |
| 2862 | // unknown error where we can't continue because the next instruction is |
| 2863 | // modifying the PC but we don't know how. |
| 2864 | return Error ("Instruction emulation failed unexpectedly."); |
| 2865 | } |
| 2866 | |
| 2867 | if (m_arch.GetMachine() == llvm::Triple::arm) |
| 2868 | { |
| 2869 | if (next_flags & 0x20) |
| 2870 | { |
| 2871 | // Thumb mode |
| 2872 | error = SetSoftwareBreakpoint(next_pc, 2); |
| 2873 | } |
| 2874 | else |
| 2875 | { |
| 2876 | // Arm mode |
| 2877 | error = SetSoftwareBreakpoint(next_pc, 4); |
| 2878 | } |
| 2879 | } |
| 2880 | else |
| 2881 | { |
| 2882 | // No size hint is given for the next breakpoint |
| 2883 | error = SetSoftwareBreakpoint(next_pc, 0); |
| 2884 | } |
| 2885 | |
| 2886 | |
| 2887 | if (error.Fail()) |
| 2888 | return error; |
| 2889 | |
| 2890 | m_threads_stepping_with_breakpoint.insert({thread_sp->GetID(), next_pc}); |
| 2891 | |
| 2892 | return Error(); |
| 2893 | } |
| 2894 | |
| 2895 | bool |
| 2896 | NativeProcessLinux::SupportHardwareSingleStepping() const |
| 2897 | { |
| 2898 | return m_arch.GetMachine() != llvm::Triple::arm; |
| 2899 | } |
| 2900 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2901 | Error |
| 2902 | NativeProcessLinux::Resume (const ResumeActionList &resume_actions) |
| 2903 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2904 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); |
| 2905 | if (log) |
| 2906 | log->Printf ("NativeProcessLinux::%s called: pid %" PRIu64, __FUNCTION__, GetID ()); |
| 2907 | |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 2908 | lldb::tid_t deferred_signal_tid = LLDB_INVALID_THREAD_ID; |
| 2909 | lldb::tid_t deferred_signal_skip_tid = LLDB_INVALID_THREAD_ID; |
Chaoren Lin | ae29d39 | 2015-02-03 01:50:46 +0000 | [diff] [blame] | 2910 | int deferred_signo = 0; |
| 2911 | NativeThreadProtocolSP deferred_signal_thread_sp; |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 2912 | bool stepping = false; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 2913 | bool software_single_step = !SupportHardwareSingleStepping(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2914 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2915 | Mutex::Locker locker (m_threads_mutex); |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 2916 | |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 2917 | if (software_single_step) |
| 2918 | { |
| 2919 | for (auto thread_sp : m_threads) |
| 2920 | { |
| 2921 | assert (thread_sp && "thread list should not contain NULL threads"); |
| 2922 | |
| 2923 | const ResumeAction *const action = resume_actions.GetActionForThread (thread_sp->GetID (), true); |
| 2924 | if (action == nullptr) |
| 2925 | continue; |
| 2926 | |
| 2927 | if (action->state == eStateStepping) |
| 2928 | { |
| 2929 | Error error = SetupSoftwareSingleStepping(thread_sp); |
| 2930 | if (error.Fail()) |
| 2931 | return error; |
| 2932 | } |
| 2933 | } |
| 2934 | } |
| 2935 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2936 | for (auto thread_sp : m_threads) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2937 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2938 | assert (thread_sp && "thread list should not contain NULL threads"); |
| 2939 | |
| 2940 | const ResumeAction *const action = resume_actions.GetActionForThread (thread_sp->GetID (), true); |
| 2941 | |
| 2942 | if (action == nullptr) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2943 | { |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2944 | if (log) |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2945 | log->Printf ("NativeProcessLinux::%s no action specified for pid %" PRIu64 " tid %" PRIu64, |
| 2946 | __FUNCTION__, GetID (), thread_sp->GetID ()); |
| 2947 | continue; |
| 2948 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2949 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2950 | if (log) |
| 2951 | { |
| 2952 | log->Printf ("NativeProcessLinux::%s processing resume action state %s for pid %" PRIu64 " tid %" PRIu64, |
| 2953 | __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); |
| 2954 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2955 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2956 | switch (action->state) |
| 2957 | { |
| 2958 | case eStateRunning: |
| 2959 | { |
| 2960 | // Run the thread, possibly feeding it the signal. |
| 2961 | const int signo = action->signal; |
| 2962 | m_coordinator_up->RequestThreadResumeAsNeeded (thread_sp->GetID (), |
| 2963 | [=](lldb::tid_t tid_to_resume, bool supress_signal) |
| 2964 | { |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2965 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2966 | // Pass this signal number on to the inferior to handle. |
| 2967 | const auto resume_result = Resume (tid_to_resume, (signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER); |
| 2968 | if (resume_result.Success()) |
| 2969 | SetState(eStateRunning, true); |
| 2970 | return resume_result; |
| 2971 | }, |
| 2972 | CoordinatorErrorHandler); |
| 2973 | break; |
| 2974 | } |
| 2975 | |
| 2976 | case eStateStepping: |
| 2977 | { |
| 2978 | // Request the step. |
| 2979 | const int signo = action->signal; |
| 2980 | m_coordinator_up->RequestThreadResume (thread_sp->GetID (), |
| 2981 | [=](lldb::tid_t tid_to_step, bool supress_signal) |
| 2982 | { |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 2983 | std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStepping (); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 2984 | |
| 2985 | Error step_result; |
| 2986 | if (software_single_step) |
| 2987 | step_result = Resume (tid_to_step, (signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER); |
| 2988 | else |
| 2989 | step_result = SingleStep (tid_to_step,(signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER); |
| 2990 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 2991 | assert (step_result.Success() && "SingleStep() failed"); |
| 2992 | if (step_result.Success()) |
| 2993 | SetState(eStateStepping, true); |
| 2994 | return step_result; |
| 2995 | }, |
| 2996 | CoordinatorErrorHandler); |
| 2997 | stepping = true; |
| 2998 | break; |
| 2999 | } |
| 3000 | |
| 3001 | case eStateSuspended: |
| 3002 | case eStateStopped: |
| 3003 | // if we haven't chosen a deferred signal tid yet, use this one. |
| 3004 | if (deferred_signal_tid == LLDB_INVALID_THREAD_ID) |
Chaoren Lin | ae29d39 | 2015-02-03 01:50:46 +0000 | [diff] [blame] | 3005 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3006 | deferred_signal_tid = thread_sp->GetID (); |
| 3007 | deferred_signal_thread_sp = thread_sp; |
| 3008 | deferred_signo = SIGSTOP; |
Chaoren Lin | ae29d39 | 2015-02-03 01:50:46 +0000 | [diff] [blame] | 3009 | } |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3010 | break; |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3011 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3012 | default: |
| 3013 | return Error ("NativeProcessLinux::%s (): unexpected state %s specified for pid %" PRIu64 ", tid %" PRIu64, |
| 3014 | __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3015 | } |
| 3016 | } |
| 3017 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3018 | // If we had any thread stopping, then do a deferred notification of the chosen stop thread id and signal |
| 3019 | // after all other running threads have stopped. |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 3020 | // If there is a stepping thread involved we'll be eventually stopped by SIGTRAP trace signal. |
| 3021 | if (deferred_signal_tid != LLDB_INVALID_THREAD_ID && !stepping) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3022 | { |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 3023 | CallAfterRunningThreadsStopWithSkipTID (deferred_signal_tid, |
| 3024 | deferred_signal_skip_tid, |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3025 | [=](lldb::tid_t deferred_notification_tid) |
| 3026 | { |
Chaoren Lin | ae29d39 | 2015-02-03 01:50:46 +0000 | [diff] [blame] | 3027 | // Set the signal thread to the current thread. |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3028 | SetCurrentThreadID (deferred_notification_tid); |
Chaoren Lin | ae29d39 | 2015-02-03 01:50:46 +0000 | [diff] [blame] | 3029 | |
| 3030 | // Set the thread state as stopped by the deferred signo. |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 3031 | std::static_pointer_cast<NativeThreadLinux> (deferred_signal_thread_sp)->SetStoppedBySignal (deferred_signo); |
Chaoren Lin | ae29d39 | 2015-02-03 01:50:46 +0000 | [diff] [blame] | 3032 | |
| 3033 | // Tell the process delegate that the process is in a stopped state. |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3034 | SetState (StateType::eStateStopped, true); |
| 3035 | }); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3038 | return Error(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | Error |
| 3042 | NativeProcessLinux::Halt () |
| 3043 | { |
| 3044 | Error error; |
| 3045 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3046 | if (kill (GetID (), SIGSTOP) != 0) |
| 3047 | error.SetErrorToErrno (); |
| 3048 | |
| 3049 | return error; |
| 3050 | } |
| 3051 | |
| 3052 | Error |
| 3053 | NativeProcessLinux::Detach () |
| 3054 | { |
| 3055 | Error error; |
| 3056 | |
| 3057 | // Tell ptrace to detach from the process. |
| 3058 | if (GetID () != LLDB_INVALID_PROCESS_ID) |
| 3059 | error = Detach (GetID ()); |
| 3060 | |
| 3061 | // Stop monitoring the inferior. |
| 3062 | StopMonitor (); |
| 3063 | |
| 3064 | // No error. |
| 3065 | return error; |
| 3066 | } |
| 3067 | |
| 3068 | Error |
| 3069 | NativeProcessLinux::Signal (int signo) |
| 3070 | { |
| 3071 | Error error; |
| 3072 | |
| 3073 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3074 | if (log) |
| 3075 | log->Printf ("NativeProcessLinux::%s: sending signal %d (%s) to pid %" PRIu64, |
| 3076 | __FUNCTION__, signo, GetUnixSignals ().GetSignalAsCString (signo), GetID ()); |
| 3077 | |
| 3078 | if (kill(GetID(), signo)) |
| 3079 | error.SetErrorToErrno(); |
| 3080 | |
| 3081 | return error; |
| 3082 | } |
| 3083 | |
| 3084 | Error |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 3085 | NativeProcessLinux::Interrupt () |
| 3086 | { |
| 3087 | // Pick a running thread (or if none, a not-dead stopped thread) as |
| 3088 | // the chosen thread that will be the stop-reason thread. |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 3089 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3090 | |
| 3091 | NativeThreadProtocolSP running_thread_sp; |
| 3092 | NativeThreadProtocolSP stopped_thread_sp; |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3093 | |
| 3094 | if (log) |
| 3095 | log->Printf ("NativeProcessLinux::%s selecting running thread for interrupt target", __FUNCTION__); |
| 3096 | |
| 3097 | Mutex::Locker locker (m_threads_mutex); |
| 3098 | |
| 3099 | for (auto thread_sp : m_threads) |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 3100 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3101 | // The thread shouldn't be null but lets just cover that here. |
| 3102 | if (!thread_sp) |
| 3103 | continue; |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 3104 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3105 | // If we have a running or stepping thread, we'll call that the |
| 3106 | // target of the interrupt. |
| 3107 | const auto thread_state = thread_sp->GetState (); |
| 3108 | if (thread_state == eStateRunning || |
| 3109 | thread_state == eStateStepping) |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 3110 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3111 | running_thread_sp = thread_sp; |
| 3112 | break; |
| 3113 | } |
| 3114 | else if (!stopped_thread_sp && StateIsStoppedState (thread_state, true)) |
| 3115 | { |
| 3116 | // Remember the first non-dead stopped thread. We'll use that as a backup if there are no running threads. |
| 3117 | stopped_thread_sp = thread_sp; |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 3118 | } |
| 3119 | } |
| 3120 | |
| 3121 | if (!running_thread_sp && !stopped_thread_sp) |
| 3122 | { |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3123 | 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] | 3124 | if (log) |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 3125 | log->Printf ("NativeProcessLinux::%s skipping due to error: %s", __FUNCTION__, error.AsCString ()); |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3126 | |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 3127 | return error; |
| 3128 | } |
| 3129 | |
| 3130 | NativeThreadProtocolSP deferred_signal_thread_sp = running_thread_sp ? running_thread_sp : stopped_thread_sp; |
| 3131 | |
| 3132 | if (log) |
| 3133 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " %s tid %" PRIu64 " chosen for interrupt target", |
| 3134 | __FUNCTION__, |
| 3135 | GetID (), |
| 3136 | running_thread_sp ? "running" : "stopped", |
| 3137 | deferred_signal_thread_sp->GetID ()); |
| 3138 | |
| 3139 | CallAfterRunningThreadsStop (deferred_signal_thread_sp->GetID (), |
| 3140 | [=](lldb::tid_t deferred_notification_tid) |
| 3141 | { |
| 3142 | // Set the signal thread to the current thread. |
| 3143 | SetCurrentThreadID (deferred_notification_tid); |
| 3144 | |
| 3145 | // Set the thread state as stopped by the deferred signo. |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 3146 | std::static_pointer_cast<NativeThreadLinux> (deferred_signal_thread_sp)->SetStoppedBySignal (SIGSTOP); |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 3147 | |
Tamas Berghammer | 5830aa7 | 2015-02-06 10:42:33 +0000 | [diff] [blame] | 3148 | // Tell the process delegate that the process is in a stopped state. |
| 3149 | SetState (StateType::eStateStopped, true); |
| 3150 | }); |
| 3151 | return Error(); |
Chaoren Lin | e9547b8 | 2015-02-03 01:51:00 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3155 | NativeProcessLinux::Kill () |
| 3156 | { |
| 3157 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3158 | if (log) |
| 3159 | log->Printf ("NativeProcessLinux::%s called for PID %" PRIu64, __FUNCTION__, GetID ()); |
| 3160 | |
| 3161 | Error error; |
| 3162 | |
| 3163 | switch (m_state) |
| 3164 | { |
| 3165 | case StateType::eStateInvalid: |
| 3166 | case StateType::eStateExited: |
| 3167 | case StateType::eStateCrashed: |
| 3168 | case StateType::eStateDetached: |
| 3169 | case StateType::eStateUnloaded: |
| 3170 | // Nothing to do - the process is already dead. |
| 3171 | if (log) |
| 3172 | log->Printf ("NativeProcessLinux::%s ignored for PID %" PRIu64 " due to current state: %s", __FUNCTION__, GetID (), StateAsCString (m_state)); |
| 3173 | return error; |
| 3174 | |
| 3175 | case StateType::eStateConnected: |
| 3176 | case StateType::eStateAttaching: |
| 3177 | case StateType::eStateLaunching: |
| 3178 | case StateType::eStateStopped: |
| 3179 | case StateType::eStateRunning: |
| 3180 | case StateType::eStateStepping: |
| 3181 | case StateType::eStateSuspended: |
| 3182 | // We can try to kill a process in these states. |
| 3183 | break; |
| 3184 | } |
| 3185 | |
| 3186 | if (kill (GetID (), SIGKILL) != 0) |
| 3187 | { |
| 3188 | error.SetErrorToErrno (); |
| 3189 | return error; |
| 3190 | } |
| 3191 | |
| 3192 | return error; |
| 3193 | } |
| 3194 | |
| 3195 | static Error |
| 3196 | ParseMemoryRegionInfoFromProcMapsLine (const std::string &maps_line, MemoryRegionInfo &memory_region_info) |
| 3197 | { |
| 3198 | memory_region_info.Clear(); |
| 3199 | |
| 3200 | StringExtractor line_extractor (maps_line.c_str ()); |
| 3201 | |
| 3202 | // Format: {address_start_hex}-{address_end_hex} perms offset dev inode pathname |
| 3203 | // perms: rwxp (letter is present if set, '-' if not, final character is p=private, s=shared). |
| 3204 | |
| 3205 | // Parse out the starting address |
| 3206 | lldb::addr_t start_address = line_extractor.GetHexMaxU64 (false, 0); |
| 3207 | |
| 3208 | // Parse out hyphen separating start and end address from range. |
| 3209 | if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != '-')) |
| 3210 | return Error ("malformed /proc/{pid}/maps entry, missing dash between address range"); |
| 3211 | |
| 3212 | // Parse out the ending address |
| 3213 | lldb::addr_t end_address = line_extractor.GetHexMaxU64 (false, start_address); |
| 3214 | |
| 3215 | // Parse out the space after the address. |
| 3216 | if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != ' ')) |
| 3217 | return Error ("malformed /proc/{pid}/maps entry, missing space after range"); |
| 3218 | |
| 3219 | // Save the range. |
| 3220 | memory_region_info.GetRange ().SetRangeBase (start_address); |
| 3221 | memory_region_info.GetRange ().SetRangeEnd (end_address); |
| 3222 | |
| 3223 | // Parse out each permission entry. |
| 3224 | if (line_extractor.GetBytesLeft () < 4) |
| 3225 | return Error ("malformed /proc/{pid}/maps entry, missing some portion of permissions"); |
| 3226 | |
| 3227 | // Handle read permission. |
| 3228 | const char read_perm_char = line_extractor.GetChar (); |
| 3229 | if (read_perm_char == 'r') |
| 3230 | memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eYes); |
| 3231 | else |
| 3232 | { |
| 3233 | assert ( (read_perm_char == '-') && "unexpected /proc/{pid}/maps read permission char" ); |
| 3234 | memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); |
| 3235 | } |
| 3236 | |
| 3237 | // Handle write permission. |
| 3238 | const char write_perm_char = line_extractor.GetChar (); |
| 3239 | if (write_perm_char == 'w') |
| 3240 | memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eYes); |
| 3241 | else |
| 3242 | { |
| 3243 | assert ( (write_perm_char == '-') && "unexpected /proc/{pid}/maps write permission char" ); |
| 3244 | memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); |
| 3245 | } |
| 3246 | |
| 3247 | // Handle execute permission. |
| 3248 | const char exec_perm_char = line_extractor.GetChar (); |
| 3249 | if (exec_perm_char == 'x') |
| 3250 | memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eYes); |
| 3251 | else |
| 3252 | { |
| 3253 | assert ( (exec_perm_char == '-') && "unexpected /proc/{pid}/maps exec permission char" ); |
| 3254 | memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); |
| 3255 | } |
| 3256 | |
| 3257 | return Error (); |
| 3258 | } |
| 3259 | |
| 3260 | Error |
| 3261 | NativeProcessLinux::GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) |
| 3262 | { |
| 3263 | // FIXME review that the final memory region returned extends to the end of the virtual address space, |
| 3264 | // with no perms if it is not mapped. |
| 3265 | |
| 3266 | // Use an approach that reads memory regions from /proc/{pid}/maps. |
| 3267 | // Assume proc maps entries are in ascending order. |
| 3268 | // FIXME assert if we find differently. |
| 3269 | Mutex::Locker locker (m_mem_region_cache_mutex); |
| 3270 | |
| 3271 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3272 | Error error; |
| 3273 | |
| 3274 | if (m_supports_mem_region == LazyBool::eLazyBoolNo) |
| 3275 | { |
| 3276 | // We're done. |
| 3277 | error.SetErrorString ("unsupported"); |
| 3278 | return error; |
| 3279 | } |
| 3280 | |
| 3281 | // If our cache is empty, pull the latest. There should always be at least one memory region |
| 3282 | // if memory region handling is supported. |
| 3283 | if (m_mem_region_cache.empty ()) |
| 3284 | { |
| 3285 | error = ProcFileReader::ProcessLineByLine (GetID (), "maps", |
| 3286 | [&] (const std::string &line) -> bool |
| 3287 | { |
| 3288 | MemoryRegionInfo info; |
| 3289 | const Error parse_error = ParseMemoryRegionInfoFromProcMapsLine (line, info); |
| 3290 | if (parse_error.Success ()) |
| 3291 | { |
| 3292 | m_mem_region_cache.push_back (info); |
| 3293 | return true; |
| 3294 | } |
| 3295 | else |
| 3296 | { |
| 3297 | if (log) |
| 3298 | log->Printf ("NativeProcessLinux::%s failed to parse proc maps line '%s': %s", __FUNCTION__, line.c_str (), error.AsCString ()); |
| 3299 | return false; |
| 3300 | } |
| 3301 | }); |
| 3302 | |
| 3303 | // If we had an error, we'll mark unsupported. |
| 3304 | if (error.Fail ()) |
| 3305 | { |
| 3306 | m_supports_mem_region = LazyBool::eLazyBoolNo; |
| 3307 | return error; |
| 3308 | } |
| 3309 | else if (m_mem_region_cache.empty ()) |
| 3310 | { |
| 3311 | // No entries after attempting to read them. This shouldn't happen if /proc/{pid}/maps |
| 3312 | // is supported. Assume we don't support map entries via procfs. |
| 3313 | if (log) |
| 3314 | log->Printf ("NativeProcessLinux::%s failed to find any procfs maps entries, assuming no support for memory region metadata retrieval", __FUNCTION__); |
| 3315 | m_supports_mem_region = LazyBool::eLazyBoolNo; |
| 3316 | error.SetErrorString ("not supported"); |
| 3317 | return error; |
| 3318 | } |
| 3319 | |
| 3320 | if (log) |
| 3321 | log->Printf ("NativeProcessLinux::%s read %" PRIu64 " memory region entries from /proc/%" PRIu64 "/maps", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ()), GetID ()); |
| 3322 | |
| 3323 | // We support memory retrieval, remember that. |
| 3324 | m_supports_mem_region = LazyBool::eLazyBoolYes; |
| 3325 | } |
| 3326 | else |
| 3327 | { |
| 3328 | if (log) |
| 3329 | log->Printf ("NativeProcessLinux::%s reusing %" PRIu64 " cached memory region entries", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); |
| 3330 | } |
| 3331 | |
| 3332 | lldb::addr_t prev_base_address = 0; |
| 3333 | |
| 3334 | // FIXME start by finding the last region that is <= target address using binary search. Data is sorted. |
| 3335 | // There can be a ton of regions on pthreads apps with lots of threads. |
| 3336 | for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end (); ++it) |
| 3337 | { |
| 3338 | MemoryRegionInfo &proc_entry_info = *it; |
| 3339 | |
| 3340 | // Sanity check assumption that /proc/{pid}/maps entries are ascending. |
| 3341 | assert ((proc_entry_info.GetRange ().GetRangeBase () >= prev_base_address) && "descending /proc/pid/maps entries detected, unexpected"); |
| 3342 | prev_base_address = proc_entry_info.GetRange ().GetRangeBase (); |
| 3343 | |
| 3344 | // If the target address comes before this entry, indicate distance to next region. |
| 3345 | if (load_addr < proc_entry_info.GetRange ().GetRangeBase ()) |
| 3346 | { |
| 3347 | range_info.GetRange ().SetRangeBase (load_addr); |
| 3348 | range_info.GetRange ().SetByteSize (proc_entry_info.GetRange ().GetRangeBase () - load_addr); |
| 3349 | range_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); |
| 3350 | range_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); |
| 3351 | range_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); |
| 3352 | |
| 3353 | return error; |
| 3354 | } |
| 3355 | else if (proc_entry_info.GetRange ().Contains (load_addr)) |
| 3356 | { |
| 3357 | // The target address is within the memory region we're processing here. |
| 3358 | range_info = proc_entry_info; |
| 3359 | return error; |
| 3360 | } |
| 3361 | |
| 3362 | // The target memory address comes somewhere after the region we just parsed. |
| 3363 | } |
| 3364 | |
| 3365 | // If we made it here, we didn't find an entry that contained the given address. |
| 3366 | error.SetErrorString ("address comes after final region"); |
| 3367 | |
| 3368 | if (log) |
| 3369 | log->Printf ("NativeProcessLinux::%s failed to find map entry for address 0x%" PRIx64 ": %s", __FUNCTION__, load_addr, error.AsCString ()); |
| 3370 | |
| 3371 | return error; |
| 3372 | } |
| 3373 | |
| 3374 | void |
| 3375 | NativeProcessLinux::DoStopIDBumped (uint32_t newBumpId) |
| 3376 | { |
| 3377 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3378 | if (log) |
| 3379 | log->Printf ("NativeProcessLinux::%s(newBumpId=%" PRIu32 ") called", __FUNCTION__, newBumpId); |
| 3380 | |
| 3381 | { |
| 3382 | Mutex::Locker locker (m_mem_region_cache_mutex); |
| 3383 | if (log) |
| 3384 | log->Printf ("NativeProcessLinux::%s clearing %" PRIu64 " entries from the cache", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); |
| 3385 | m_mem_region_cache.clear (); |
| 3386 | } |
| 3387 | } |
| 3388 | |
| 3389 | Error |
| 3390 | NativeProcessLinux::AllocateMemory ( |
| 3391 | lldb::addr_t size, |
| 3392 | uint32_t permissions, |
| 3393 | lldb::addr_t &addr) |
| 3394 | { |
| 3395 | // FIXME implementing this requires the equivalent of |
| 3396 | // InferiorCallPOSIX::InferiorCallMmap, which depends on |
| 3397 | // functional ThreadPlans working with Native*Protocol. |
| 3398 | #if 1 |
| 3399 | return Error ("not implemented yet"); |
| 3400 | #else |
| 3401 | addr = LLDB_INVALID_ADDRESS; |
| 3402 | |
| 3403 | unsigned prot = 0; |
| 3404 | if (permissions & lldb::ePermissionsReadable) |
| 3405 | prot |= eMmapProtRead; |
| 3406 | if (permissions & lldb::ePermissionsWritable) |
| 3407 | prot |= eMmapProtWrite; |
| 3408 | if (permissions & lldb::ePermissionsExecutable) |
| 3409 | prot |= eMmapProtExec; |
| 3410 | |
| 3411 | // TODO implement this directly in NativeProcessLinux |
| 3412 | // (and lift to NativeProcessPOSIX if/when that class is |
| 3413 | // refactored out). |
| 3414 | if (InferiorCallMmap(this, addr, 0, size, prot, |
| 3415 | eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { |
| 3416 | m_addr_to_mmap_size[addr] = size; |
| 3417 | return Error (); |
| 3418 | } else { |
| 3419 | addr = LLDB_INVALID_ADDRESS; |
| 3420 | return Error("unable to allocate %" PRIu64 " bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions)); |
| 3421 | } |
| 3422 | #endif |
| 3423 | } |
| 3424 | |
| 3425 | Error |
| 3426 | NativeProcessLinux::DeallocateMemory (lldb::addr_t addr) |
| 3427 | { |
| 3428 | // FIXME see comments in AllocateMemory - required lower-level |
| 3429 | // bits not in place yet (ThreadPlans) |
| 3430 | return Error ("not implemented"); |
| 3431 | } |
| 3432 | |
| 3433 | lldb::addr_t |
| 3434 | NativeProcessLinux::GetSharedLibraryInfoAddress () |
| 3435 | { |
| 3436 | #if 1 |
| 3437 | // punt on this for now |
| 3438 | return LLDB_INVALID_ADDRESS; |
| 3439 | #else |
| 3440 | // Return the image info address for the exe module |
| 3441 | #if 1 |
| 3442 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3443 | |
| 3444 | ModuleSP module_sp; |
| 3445 | Error error = GetExeModuleSP (module_sp); |
| 3446 | if (error.Fail ()) |
| 3447 | { |
| 3448 | if (log) |
| 3449 | log->Warning ("NativeProcessLinux::%s failed to retrieve exe module: %s", __FUNCTION__, error.AsCString ()); |
| 3450 | return LLDB_INVALID_ADDRESS; |
| 3451 | } |
| 3452 | |
| 3453 | if (module_sp == nullptr) |
| 3454 | { |
| 3455 | if (log) |
| 3456 | log->Warning ("NativeProcessLinux::%s exe module returned was NULL", __FUNCTION__); |
| 3457 | return LLDB_INVALID_ADDRESS; |
| 3458 | } |
| 3459 | |
| 3460 | ObjectFileSP object_file_sp = module_sp->GetObjectFile (); |
| 3461 | if (object_file_sp == nullptr) |
| 3462 | { |
| 3463 | if (log) |
| 3464 | log->Warning ("NativeProcessLinux::%s exe module returned a NULL object file", __FUNCTION__); |
| 3465 | return LLDB_INVALID_ADDRESS; |
| 3466 | } |
| 3467 | |
| 3468 | return obj_file_sp->GetImageInfoAddress(); |
| 3469 | #else |
| 3470 | Target *target = &GetTarget(); |
| 3471 | ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile(); |
| 3472 | Address addr = obj_file->GetImageInfoAddress(target); |
| 3473 | |
| 3474 | if (addr.IsValid()) |
| 3475 | return addr.GetLoadAddress(target); |
| 3476 | return LLDB_INVALID_ADDRESS; |
| 3477 | #endif |
| 3478 | #endif // punt on this for now |
| 3479 | } |
| 3480 | |
| 3481 | size_t |
| 3482 | NativeProcessLinux::UpdateThreads () |
| 3483 | { |
| 3484 | // The NativeProcessLinux monitoring threads are always up to date |
| 3485 | // with respect to thread state and they keep the thread list |
| 3486 | // populated properly. All this method needs to do is return the |
| 3487 | // thread count. |
| 3488 | Mutex::Locker locker (m_threads_mutex); |
| 3489 | return m_threads.size (); |
| 3490 | } |
| 3491 | |
| 3492 | bool |
| 3493 | NativeProcessLinux::GetArchitecture (ArchSpec &arch) const |
| 3494 | { |
| 3495 | arch = m_arch; |
| 3496 | return true; |
| 3497 | } |
| 3498 | |
| 3499 | Error |
Tamas Berghammer | 63c8be9 | 2015-04-15 09:38:48 +0000 | [diff] [blame] | 3500 | NativeProcessLinux::GetSoftwareBreakpointPCOffset (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3501 | { |
| 3502 | // FIXME put this behind a breakpoint protocol class that can be |
| 3503 | // set per architecture. Need ARM, MIPS support here. |
Todd Fiala | 2afc596 | 2014-08-21 16:42:31 +0000 | [diff] [blame] | 3504 | static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3505 | static const uint8_t g_i386_opcode [] = { 0xCC }; |
Mohit K. Bhakkad | e8659b5 | 2015-04-23 06:36:20 +0000 | [diff] [blame] | 3506 | static const uint8_t g_mips64_opcode[] = { 0x00, 0x00, 0x00, 0x0d }; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3507 | |
| 3508 | switch (m_arch.GetMachine ()) |
| 3509 | { |
Todd Fiala | 2afc596 | 2014-08-21 16:42:31 +0000 | [diff] [blame] | 3510 | case llvm::Triple::aarch64: |
| 3511 | actual_opcode_size = static_cast<uint32_t> (sizeof(g_aarch64_opcode)); |
| 3512 | return Error (); |
| 3513 | |
Tamas Berghammer | 63c8be9 | 2015-04-15 09:38:48 +0000 | [diff] [blame] | 3514 | case llvm::Triple::arm: |
| 3515 | actual_opcode_size = 0; // On arm the PC don't get updated for breakpoint hits |
| 3516 | return Error (); |
| 3517 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3518 | case llvm::Triple::x86: |
| 3519 | case llvm::Triple::x86_64: |
| 3520 | actual_opcode_size = static_cast<uint32_t> (sizeof(g_i386_opcode)); |
| 3521 | return Error (); |
| 3522 | |
Mohit K. Bhakkad | e8659b5 | 2015-04-23 06:36:20 +0000 | [diff] [blame] | 3523 | case llvm::Triple::mips64: |
| 3524 | case llvm::Triple::mips64el: |
| 3525 | actual_opcode_size = static_cast<uint32_t> (sizeof(g_mips64_opcode)); |
| 3526 | return Error (); |
| 3527 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3528 | default: |
| 3529 | assert(false && "CPU type not supported!"); |
| 3530 | return Error ("CPU type not supported"); |
| 3531 | } |
| 3532 | } |
| 3533 | |
| 3534 | Error |
| 3535 | NativeProcessLinux::SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) |
| 3536 | { |
| 3537 | if (hardware) |
| 3538 | return Error ("NativeProcessLinux does not support hardware breakpoints"); |
| 3539 | else |
| 3540 | return SetSoftwareBreakpoint (addr, size); |
| 3541 | } |
| 3542 | |
| 3543 | Error |
Tamas Berghammer | 63c8be9 | 2015-04-15 09:38:48 +0000 | [diff] [blame] | 3544 | NativeProcessLinux::GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, |
| 3545 | size_t &actual_opcode_size, |
| 3546 | const uint8_t *&trap_opcode_bytes) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3547 | { |
Tamas Berghammer | 63c8be9 | 2015-04-15 09:38:48 +0000 | [diff] [blame] | 3548 | // FIXME put this behind a breakpoint protocol class that can be set per |
| 3549 | // architecture. Need MIPS support here. |
Todd Fiala | 2afc596 | 2014-08-21 16:42:31 +0000 | [diff] [blame] | 3550 | static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; |
Tamas Berghammer | 63c8be9 | 2015-04-15 09:38:48 +0000 | [diff] [blame] | 3551 | // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the |
| 3552 | // linux kernel does otherwise. |
| 3553 | static const uint8_t g_arm_breakpoint_opcode[] = { 0xf0, 0x01, 0xf0, 0xe7 }; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3554 | static const uint8_t g_i386_opcode [] = { 0xCC }; |
Mohit K. Bhakkad | 3df471c | 2015-03-17 11:43:56 +0000 | [diff] [blame] | 3555 | static const uint8_t g_mips64_opcode[] = { 0x00, 0x00, 0x00, 0x0d }; |
Mohit K. Bhakkad | 2c2acf9 | 2015-04-09 07:12:15 +0000 | [diff] [blame] | 3556 | static const uint8_t g_mips64el_opcode[] = { 0x0d, 0x00, 0x00, 0x00 }; |
Tamas Berghammer | 63c8be9 | 2015-04-15 09:38:48 +0000 | [diff] [blame] | 3557 | static const uint8_t g_thumb_breakpoint_opcode[] = { 0x01, 0xde }; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3558 | |
| 3559 | switch (m_arch.GetMachine ()) |
| 3560 | { |
Todd Fiala | 2afc596 | 2014-08-21 16:42:31 +0000 | [diff] [blame] | 3561 | case llvm::Triple::aarch64: |
| 3562 | trap_opcode_bytes = g_aarch64_opcode; |
| 3563 | actual_opcode_size = sizeof(g_aarch64_opcode); |
| 3564 | return Error (); |
| 3565 | |
Tamas Berghammer | 63c8be9 | 2015-04-15 09:38:48 +0000 | [diff] [blame] | 3566 | case llvm::Triple::arm: |
| 3567 | switch (trap_opcode_size_hint) |
| 3568 | { |
| 3569 | case 2: |
| 3570 | trap_opcode_bytes = g_thumb_breakpoint_opcode; |
| 3571 | actual_opcode_size = sizeof(g_thumb_breakpoint_opcode); |
| 3572 | return Error (); |
| 3573 | case 4: |
| 3574 | trap_opcode_bytes = g_arm_breakpoint_opcode; |
| 3575 | actual_opcode_size = sizeof(g_arm_breakpoint_opcode); |
| 3576 | return Error (); |
| 3577 | default: |
| 3578 | assert(false && "Unrecognised trap opcode size hint!"); |
| 3579 | return Error ("Unrecognised trap opcode size hint!"); |
| 3580 | } |
| 3581 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3582 | case llvm::Triple::x86: |
| 3583 | case llvm::Triple::x86_64: |
| 3584 | trap_opcode_bytes = g_i386_opcode; |
| 3585 | actual_opcode_size = sizeof(g_i386_opcode); |
| 3586 | return Error (); |
| 3587 | |
Mohit K. Bhakkad | 3df471c | 2015-03-17 11:43:56 +0000 | [diff] [blame] | 3588 | case llvm::Triple::mips64: |
Mohit K. Bhakkad | 3df471c | 2015-03-17 11:43:56 +0000 | [diff] [blame] | 3589 | trap_opcode_bytes = g_mips64_opcode; |
| 3590 | actual_opcode_size = sizeof(g_mips64_opcode); |
| 3591 | return Error (); |
| 3592 | |
Mohit K. Bhakkad | 2c2acf9 | 2015-04-09 07:12:15 +0000 | [diff] [blame] | 3593 | case llvm::Triple::mips64el: |
| 3594 | trap_opcode_bytes = g_mips64el_opcode; |
| 3595 | actual_opcode_size = sizeof(g_mips64el_opcode); |
| 3596 | return Error (); |
| 3597 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3598 | default: |
| 3599 | assert(false && "CPU type not supported!"); |
| 3600 | return Error ("CPU type not supported"); |
| 3601 | } |
| 3602 | } |
| 3603 | |
| 3604 | #if 0 |
| 3605 | ProcessMessage::CrashReason |
| 3606 | NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info) |
| 3607 | { |
| 3608 | ProcessMessage::CrashReason reason; |
| 3609 | assert(info->si_signo == SIGSEGV); |
| 3610 | |
| 3611 | reason = ProcessMessage::eInvalidCrashReason; |
| 3612 | |
| 3613 | switch (info->si_code) |
| 3614 | { |
| 3615 | default: |
| 3616 | assert(false && "unexpected si_code for SIGSEGV"); |
| 3617 | break; |
| 3618 | case SI_KERNEL: |
| 3619 | // Linux will occasionally send spurious SI_KERNEL codes. |
| 3620 | // (this is poorly documented in sigaction) |
| 3621 | // One way to get this is via unaligned SIMD loads. |
| 3622 | reason = ProcessMessage::eInvalidAddress; // for lack of anything better |
| 3623 | break; |
| 3624 | case SEGV_MAPERR: |
| 3625 | reason = ProcessMessage::eInvalidAddress; |
| 3626 | break; |
| 3627 | case SEGV_ACCERR: |
| 3628 | reason = ProcessMessage::ePrivilegedAddress; |
| 3629 | break; |
| 3630 | } |
| 3631 | |
| 3632 | return reason; |
| 3633 | } |
| 3634 | #endif |
| 3635 | |
| 3636 | |
| 3637 | #if 0 |
| 3638 | ProcessMessage::CrashReason |
| 3639 | NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info) |
| 3640 | { |
| 3641 | ProcessMessage::CrashReason reason; |
| 3642 | assert(info->si_signo == SIGILL); |
| 3643 | |
| 3644 | reason = ProcessMessage::eInvalidCrashReason; |
| 3645 | |
| 3646 | switch (info->si_code) |
| 3647 | { |
| 3648 | default: |
| 3649 | assert(false && "unexpected si_code for SIGILL"); |
| 3650 | break; |
| 3651 | case ILL_ILLOPC: |
| 3652 | reason = ProcessMessage::eIllegalOpcode; |
| 3653 | break; |
| 3654 | case ILL_ILLOPN: |
| 3655 | reason = ProcessMessage::eIllegalOperand; |
| 3656 | break; |
| 3657 | case ILL_ILLADR: |
| 3658 | reason = ProcessMessage::eIllegalAddressingMode; |
| 3659 | break; |
| 3660 | case ILL_ILLTRP: |
| 3661 | reason = ProcessMessage::eIllegalTrap; |
| 3662 | break; |
| 3663 | case ILL_PRVOPC: |
| 3664 | reason = ProcessMessage::ePrivilegedOpcode; |
| 3665 | break; |
| 3666 | case ILL_PRVREG: |
| 3667 | reason = ProcessMessage::ePrivilegedRegister; |
| 3668 | break; |
| 3669 | case ILL_COPROC: |
| 3670 | reason = ProcessMessage::eCoprocessorError; |
| 3671 | break; |
| 3672 | case ILL_BADSTK: |
| 3673 | reason = ProcessMessage::eInternalStackError; |
| 3674 | break; |
| 3675 | } |
| 3676 | |
| 3677 | return reason; |
| 3678 | } |
| 3679 | #endif |
| 3680 | |
| 3681 | #if 0 |
| 3682 | ProcessMessage::CrashReason |
| 3683 | NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info) |
| 3684 | { |
| 3685 | ProcessMessage::CrashReason reason; |
| 3686 | assert(info->si_signo == SIGFPE); |
| 3687 | |
| 3688 | reason = ProcessMessage::eInvalidCrashReason; |
| 3689 | |
| 3690 | switch (info->si_code) |
| 3691 | { |
| 3692 | default: |
| 3693 | assert(false && "unexpected si_code for SIGFPE"); |
| 3694 | break; |
| 3695 | case FPE_INTDIV: |
| 3696 | reason = ProcessMessage::eIntegerDivideByZero; |
| 3697 | break; |
| 3698 | case FPE_INTOVF: |
| 3699 | reason = ProcessMessage::eIntegerOverflow; |
| 3700 | break; |
| 3701 | case FPE_FLTDIV: |
| 3702 | reason = ProcessMessage::eFloatDivideByZero; |
| 3703 | break; |
| 3704 | case FPE_FLTOVF: |
| 3705 | reason = ProcessMessage::eFloatOverflow; |
| 3706 | break; |
| 3707 | case FPE_FLTUND: |
| 3708 | reason = ProcessMessage::eFloatUnderflow; |
| 3709 | break; |
| 3710 | case FPE_FLTRES: |
| 3711 | reason = ProcessMessage::eFloatInexactResult; |
| 3712 | break; |
| 3713 | case FPE_FLTINV: |
| 3714 | reason = ProcessMessage::eFloatInvalidOperation; |
| 3715 | break; |
| 3716 | case FPE_FLTSUB: |
| 3717 | reason = ProcessMessage::eFloatSubscriptRange; |
| 3718 | break; |
| 3719 | } |
| 3720 | |
| 3721 | return reason; |
| 3722 | } |
| 3723 | #endif |
| 3724 | |
| 3725 | #if 0 |
| 3726 | ProcessMessage::CrashReason |
| 3727 | NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info) |
| 3728 | { |
| 3729 | ProcessMessage::CrashReason reason; |
| 3730 | assert(info->si_signo == SIGBUS); |
| 3731 | |
| 3732 | reason = ProcessMessage::eInvalidCrashReason; |
| 3733 | |
| 3734 | switch (info->si_code) |
| 3735 | { |
| 3736 | default: |
| 3737 | assert(false && "unexpected si_code for SIGBUS"); |
| 3738 | break; |
| 3739 | case BUS_ADRALN: |
| 3740 | reason = ProcessMessage::eIllegalAlignment; |
| 3741 | break; |
| 3742 | case BUS_ADRERR: |
| 3743 | reason = ProcessMessage::eIllegalAddress; |
| 3744 | break; |
| 3745 | case BUS_OBJERR: |
| 3746 | reason = ProcessMessage::eHardwareError; |
| 3747 | break; |
| 3748 | } |
| 3749 | |
| 3750 | return reason; |
| 3751 | } |
| 3752 | #endif |
| 3753 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3754 | Error |
| 3755 | NativeProcessLinux::ReadMemory (lldb::addr_t addr, void *buf, lldb::addr_t size, lldb::addr_t &bytes_read) |
| 3756 | { |
| 3757 | ReadOperation op(addr, buf, size, bytes_read); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3758 | m_monitor_up->DoOperation(&op); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3759 | return op.GetError (); |
| 3760 | } |
| 3761 | |
| 3762 | Error |
| 3763 | NativeProcessLinux::WriteMemory (lldb::addr_t addr, const void *buf, lldb::addr_t size, lldb::addr_t &bytes_written) |
| 3764 | { |
| 3765 | WriteOperation op(addr, buf, size, bytes_written); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3766 | m_monitor_up->DoOperation(&op); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3767 | return op.GetError (); |
| 3768 | } |
| 3769 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3770 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3771 | 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] | 3772 | uint32_t size, RegisterValue &value) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3773 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3774 | ReadRegOperation op(tid, offset, reg_name, value); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3775 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3776 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3777 | } |
| 3778 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3779 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3780 | NativeProcessLinux::WriteRegisterValue(lldb::tid_t tid, unsigned offset, |
| 3781 | const char* reg_name, const RegisterValue &value) |
| 3782 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3783 | WriteRegOperation op(tid, offset, reg_name, value); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3784 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3785 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3786 | } |
| 3787 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3788 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3789 | NativeProcessLinux::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size) |
| 3790 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3791 | ReadGPROperation op(tid, buf, buf_size); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3792 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3793 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3794 | } |
| 3795 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3796 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3797 | NativeProcessLinux::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size) |
| 3798 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3799 | ReadFPROperation op(tid, buf, buf_size); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3800 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3801 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3802 | } |
| 3803 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3804 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3805 | NativeProcessLinux::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) |
| 3806 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3807 | ReadRegisterSetOperation op(tid, buf, buf_size, regset); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3808 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3809 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3810 | } |
| 3811 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3812 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3813 | NativeProcessLinux::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size) |
| 3814 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3815 | WriteGPROperation op(tid, buf, buf_size); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3816 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3817 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3818 | } |
| 3819 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3820 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3821 | NativeProcessLinux::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size) |
| 3822 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3823 | WriteFPROperation op(tid, buf, buf_size); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3824 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3825 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3826 | } |
| 3827 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3828 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3829 | NativeProcessLinux::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) |
| 3830 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3831 | WriteRegisterSetOperation op(tid, buf, buf_size, regset); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3832 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3833 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3834 | } |
| 3835 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3836 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3837 | NativeProcessLinux::Resume (lldb::tid_t tid, uint32_t signo) |
| 3838 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3839 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); |
| 3840 | |
| 3841 | if (log) |
| 3842 | log->Printf ("NativeProcessLinux::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid, |
| 3843 | GetUnixSignals().GetSignalAsCString (signo)); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3844 | ResumeOperation op (tid, signo); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3845 | m_monitor_up->DoOperation (&op); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3846 | if (log) |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3847 | log->Printf ("NativeProcessLinux::%s() resuming thread = %" PRIu64 " result = %s", __FUNCTION__, tid, op.GetError().Success() ? "true" : "false"); |
| 3848 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3849 | } |
| 3850 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3851 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3852 | NativeProcessLinux::SingleStep(lldb::tid_t tid, uint32_t signo) |
| 3853 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3854 | SingleStepOperation op(tid, signo); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3855 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3856 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3857 | } |
| 3858 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3859 | Error |
| 3860 | NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3861 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3862 | SiginfoOperation op(tid, siginfo); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3863 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3864 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3867 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3868 | NativeProcessLinux::GetEventMessage(lldb::tid_t tid, unsigned long *message) |
| 3869 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3870 | EventMessageOperation op(tid, message); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3871 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3872 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3873 | } |
| 3874 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 3875 | Error |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3876 | NativeProcessLinux::Detach(lldb::tid_t tid) |
| 3877 | { |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3878 | if (tid == LLDB_INVALID_THREAD_ID) |
| 3879 | return Error(); |
| 3880 | |
| 3881 | DetachOperation op(tid); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3882 | m_monitor_up->DoOperation(&op); |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 3883 | return op.GetError(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
| 3886 | bool |
| 3887 | NativeProcessLinux::DupDescriptor(const char *path, int fd, int flags) |
| 3888 | { |
| 3889 | int target_fd = open(path, flags, 0666); |
| 3890 | |
| 3891 | if (target_fd == -1) |
| 3892 | return false; |
| 3893 | |
Pavel Labath | 493c3a1 | 2015-02-04 10:36:57 +0000 | [diff] [blame] | 3894 | if (dup2(target_fd, fd) == -1) |
| 3895 | return false; |
| 3896 | |
| 3897 | return (close(target_fd) == -1) ? false : true; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
| 3900 | void |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3901 | NativeProcessLinux::StartMonitorThread(const InitialOperation &initial_operation, Error &error) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3902 | { |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3903 | m_monitor_up.reset(new Monitor(initial_operation, this)); |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 3904 | error = m_monitor_up->Initialize(); |
| 3905 | if (error.Fail()) { |
| 3906 | m_monitor_up.reset(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3907 | } |
| 3908 | } |
| 3909 | |
| 3910 | void |
| 3911 | NativeProcessLinux::StopMonitor() |
| 3912 | { |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3913 | StopCoordinatorThread (); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 3914 | m_monitor_up.reset(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3915 | } |
| 3916 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3917 | Error |
| 3918 | NativeProcessLinux::StartCoordinatorThread () |
| 3919 | { |
| 3920 | Error error; |
| 3921 | static const char *g_thread_name = "lldb.process.linux.ts_coordinator"; |
| 3922 | Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 3923 | |
| 3924 | // Skip if thread is already running |
| 3925 | if (m_coordinator_thread.IsJoinable()) |
| 3926 | { |
| 3927 | error.SetErrorString ("ThreadStateCoordinator's run loop is already running"); |
| 3928 | if (log) |
| 3929 | log->Printf ("NativeProcessLinux::%s %s", __FUNCTION__, error.AsCString ()); |
| 3930 | return error; |
| 3931 | } |
| 3932 | |
| 3933 | // Enable verbose logging if lldb thread logging is enabled. |
| 3934 | m_coordinator_up->LogEnableEventProcessing (log != nullptr); |
| 3935 | |
| 3936 | if (log) |
| 3937 | log->Printf ("NativeProcessLinux::%s launching ThreadStateCoordinator thread for pid %" PRIu64, __FUNCTION__, GetID ()); |
| 3938 | m_coordinator_thread = ThreadLauncher::LaunchThread(g_thread_name, CoordinatorThread, this, &error); |
| 3939 | return error; |
| 3940 | } |
| 3941 | |
| 3942 | void * |
| 3943 | NativeProcessLinux::CoordinatorThread (void *arg) |
| 3944 | { |
| 3945 | Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 3946 | |
| 3947 | NativeProcessLinux *const process = static_cast<NativeProcessLinux*> (arg); |
| 3948 | assert (process && "null process passed to CoordinatorThread"); |
| 3949 | if (!process) |
| 3950 | { |
| 3951 | if (log) |
| 3952 | log->Printf ("NativeProcessLinux::%s null process, exiting ThreadStateCoordinator processing loop", __FUNCTION__); |
| 3953 | return nullptr; |
| 3954 | } |
| 3955 | |
| 3956 | // Run the thread state coordinator loop until it is done. This call uses |
| 3957 | // efficient waiting for an event to be ready. |
| 3958 | while (process->m_coordinator_up->ProcessNextEvent () == ThreadStateCoordinator::eventLoopResultContinue) |
| 3959 | { |
| 3960 | } |
| 3961 | |
| 3962 | if (log) |
| 3963 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " exiting ThreadStateCoordinator processing loop due to coordinator indicating completion", __FUNCTION__, process->GetID ()); |
| 3964 | |
| 3965 | return nullptr; |
| 3966 | } |
| 3967 | |
| 3968 | void |
| 3969 | NativeProcessLinux::StopCoordinatorThread() |
| 3970 | { |
| 3971 | Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 3972 | if (log) |
| 3973 | log->Printf ("NativeProcessLinux::%s requesting ThreadStateCoordinator stop for pid %" PRIu64, __FUNCTION__, GetID ()); |
| 3974 | |
| 3975 | // Tell the coordinator we're done. This will cause the coordinator |
| 3976 | // run loop thread to exit when the processing queue hits this message. |
| 3977 | m_coordinator_up->StopCoordinator (); |
Oleksiy Vyalov | 8bc34f4 | 2015-02-19 17:58:04 +0000 | [diff] [blame] | 3978 | m_coordinator_thread.Join (nullptr); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 3979 | } |
| 3980 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 3981 | bool |
| 3982 | NativeProcessLinux::HasThreadNoLock (lldb::tid_t thread_id) |
| 3983 | { |
| 3984 | for (auto thread_sp : m_threads) |
| 3985 | { |
| 3986 | assert (thread_sp && "thread list should not contain NULL threads"); |
| 3987 | if (thread_sp->GetID () == thread_id) |
| 3988 | { |
| 3989 | // We have this thread. |
| 3990 | return true; |
| 3991 | } |
| 3992 | } |
| 3993 | |
| 3994 | // We don't have this thread. |
| 3995 | return false; |
| 3996 | } |
| 3997 | |
| 3998 | NativeThreadProtocolSP |
| 3999 | NativeProcessLinux::MaybeGetThreadNoLock (lldb::tid_t thread_id) |
| 4000 | { |
| 4001 | // CONSIDER organize threads by map - we can do better than linear. |
| 4002 | for (auto thread_sp : m_threads) |
| 4003 | { |
| 4004 | if (thread_sp->GetID () == thread_id) |
| 4005 | return thread_sp; |
| 4006 | } |
| 4007 | |
| 4008 | // We don't have this thread. |
| 4009 | return NativeThreadProtocolSP (); |
| 4010 | } |
| 4011 | |
| 4012 | bool |
| 4013 | NativeProcessLinux::StopTrackingThread (lldb::tid_t thread_id) |
| 4014 | { |
| 4015 | Mutex::Locker locker (m_threads_mutex); |
| 4016 | for (auto it = m_threads.begin (); it != m_threads.end (); ++it) |
| 4017 | { |
| 4018 | if (*it && ((*it)->GetID () == thread_id)) |
| 4019 | { |
| 4020 | m_threads.erase (it); |
| 4021 | return true; |
| 4022 | } |
| 4023 | } |
| 4024 | |
| 4025 | // Didn't find it. |
| 4026 | return false; |
| 4027 | } |
| 4028 | |
| 4029 | NativeThreadProtocolSP |
| 4030 | NativeProcessLinux::AddThread (lldb::tid_t thread_id) |
| 4031 | { |
| 4032 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 4033 | |
| 4034 | Mutex::Locker locker (m_threads_mutex); |
| 4035 | |
| 4036 | if (log) |
| 4037 | { |
| 4038 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " adding thread with tid %" PRIu64, |
| 4039 | __FUNCTION__, |
| 4040 | GetID (), |
| 4041 | thread_id); |
| 4042 | } |
| 4043 | |
| 4044 | assert (!HasThreadNoLock (thread_id) && "attempted to add a thread by id that already exists"); |
| 4045 | |
| 4046 | // If this is the first thread, save it as the current thread |
| 4047 | if (m_threads.empty ()) |
| 4048 | SetCurrentThreadID (thread_id); |
| 4049 | |
| 4050 | NativeThreadProtocolSP thread_sp (new NativeThreadLinux (this, thread_id)); |
| 4051 | m_threads.push_back (thread_sp); |
| 4052 | |
| 4053 | return thread_sp; |
| 4054 | } |
| 4055 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 4056 | Error |
| 4057 | NativeProcessLinux::FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp) |
| 4058 | { |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 4059 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 4060 | |
| 4061 | Error error; |
| 4062 | |
| 4063 | // Get a linux thread pointer. |
| 4064 | if (!thread_sp) |
| 4065 | { |
| 4066 | error.SetErrorString ("null thread_sp"); |
| 4067 | if (log) |
| 4068 | log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); |
| 4069 | return error; |
| 4070 | } |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 4071 | 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] | 4072 | |
| 4073 | // 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] | 4074 | NativeRegisterContextSP context_sp = linux_thread_sp->GetRegisterContext (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 4075 | if (!context_sp) |
| 4076 | { |
| 4077 | error.SetErrorString ("cannot get a NativeRegisterContext for the thread"); |
| 4078 | if (log) |
| 4079 | log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); |
| 4080 | return error; |
| 4081 | } |
| 4082 | |
| 4083 | uint32_t breakpoint_size = 0; |
Tamas Berghammer | 63c8be9 | 2015-04-15 09:38:48 +0000 | [diff] [blame] | 4084 | error = GetSoftwareBreakpointPCOffset (context_sp, breakpoint_size); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 4085 | if (error.Fail ()) |
| 4086 | { |
| 4087 | if (log) |
| 4088 | log->Printf ("NativeProcessLinux::%s GetBreakpointSize() failed: %s", __FUNCTION__, error.AsCString ()); |
| 4089 | return error; |
| 4090 | } |
| 4091 | else |
| 4092 | { |
| 4093 | if (log) |
| 4094 | log->Printf ("NativeProcessLinux::%s breakpoint size: %" PRIu32, __FUNCTION__, breakpoint_size); |
| 4095 | } |
| 4096 | |
| 4097 | // First try probing for a breakpoint at a software breakpoint location: PC - breakpoint size. |
| 4098 | const lldb::addr_t initial_pc_addr = context_sp->GetPC (); |
| 4099 | lldb::addr_t breakpoint_addr = initial_pc_addr; |
| 4100 | if (breakpoint_size > static_cast<lldb::addr_t> (0)) |
| 4101 | { |
| 4102 | // Do not allow breakpoint probe to wrap around. |
| 4103 | if (breakpoint_addr >= static_cast<lldb::addr_t> (breakpoint_size)) |
| 4104 | breakpoint_addr -= static_cast<lldb::addr_t> (breakpoint_size); |
| 4105 | } |
| 4106 | |
| 4107 | // Check if we stopped because of a breakpoint. |
| 4108 | NativeBreakpointSP breakpoint_sp; |
| 4109 | error = m_breakpoint_list.GetBreakpoint (breakpoint_addr, breakpoint_sp); |
| 4110 | if (!error.Success () || !breakpoint_sp) |
| 4111 | { |
| 4112 | // We didn't find one at a software probe location. Nothing to do. |
| 4113 | if (log) |
| 4114 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " no lldb breakpoint found at current pc with adjustment: 0x%" PRIx64, __FUNCTION__, GetID (), breakpoint_addr); |
| 4115 | return Error (); |
| 4116 | } |
| 4117 | |
| 4118 | // If the breakpoint is not a software breakpoint, nothing to do. |
| 4119 | if (!breakpoint_sp->IsSoftwareBreakpoint ()) |
| 4120 | { |
| 4121 | if (log) |
| 4122 | log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " breakpoint found at 0x%" PRIx64 ", not software, nothing to adjust", __FUNCTION__, GetID (), breakpoint_addr); |
| 4123 | return Error (); |
| 4124 | } |
| 4125 | |
| 4126 | // |
| 4127 | // We have a software breakpoint and need to adjust the PC. |
| 4128 | // |
| 4129 | |
| 4130 | // Sanity check. |
| 4131 | if (breakpoint_size == 0) |
| 4132 | { |
| 4133 | // Nothing to do! How did we get here? |
| 4134 | if (log) |
| 4135 | 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); |
| 4136 | return Error (); |
| 4137 | } |
| 4138 | |
| 4139 | // Change the program counter. |
| 4140 | if (log) |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 4141 | 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] | 4142 | |
| 4143 | error = context_sp->SetPC (breakpoint_addr); |
| 4144 | if (error.Fail ()) |
| 4145 | { |
| 4146 | if (log) |
Tamas Berghammer | cb84eeb | 2015-03-17 15:05:31 +0000 | [diff] [blame] | 4147 | 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] | 4148 | return error; |
| 4149 | } |
| 4150 | |
| 4151 | return error; |
| 4152 | } |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 4153 | |
| 4154 | void |
| 4155 | NativeProcessLinux::NotifyThreadCreateStopped (lldb::tid_t tid) |
| 4156 | { |
| 4157 | const bool is_stopped = true; |
| 4158 | m_coordinator_up->NotifyThreadCreate (tid, is_stopped, CoordinatorErrorHandler); |
| 4159 | } |
| 4160 | |
| 4161 | void |
| 4162 | NativeProcessLinux::NotifyThreadDeath (lldb::tid_t tid) |
| 4163 | { |
| 4164 | m_coordinator_up->NotifyThreadDeath (tid, CoordinatorErrorHandler); |
| 4165 | } |
| 4166 | |
| 4167 | void |
| 4168 | NativeProcessLinux::NotifyThreadStop (lldb::tid_t tid) |
| 4169 | { |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 4170 | m_coordinator_up->NotifyThreadStop (tid, false, CoordinatorErrorHandler); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 4171 | } |
| 4172 | |
| 4173 | void |
| 4174 | NativeProcessLinux::CallAfterRunningThreadsStop (lldb::tid_t tid, |
| 4175 | const std::function<void (lldb::tid_t tid)> &call_after_function) |
| 4176 | { |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 4177 | Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 4178 | if (log) |
| 4179 | log->Printf("NativeProcessLinux::%s tid %" PRIu64, __FUNCTION__, tid); |
| 4180 | |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 4181 | const lldb::pid_t pid = GetID (); |
Chaoren Lin | 938fcf6 | 2015-02-03 01:50:44 +0000 | [diff] [blame] | 4182 | m_coordinator_up->CallAfterRunningThreadsStop (tid, |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 4183 | [=](lldb::tid_t request_stop_tid) |
| 4184 | { |
Chaoren Lin | 37c768c | 2015-02-03 01:51:30 +0000 | [diff] [blame] | 4185 | return RequestThreadStop(pid, request_stop_tid); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 4186 | }, |
| 4187 | call_after_function, |
| 4188 | CoordinatorErrorHandler); |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 4189 | } |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 4190 | |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 4191 | void |
| 4192 | NativeProcessLinux::CallAfterRunningThreadsStopWithSkipTID (lldb::tid_t deferred_signal_tid, |
| 4193 | lldb::tid_t skip_stop_request_tid, |
| 4194 | const std::function<void (lldb::tid_t tid)> &call_after_function) |
| 4195 | { |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 4196 | Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 4197 | if (log) |
| 4198 | log->Printf("NativeProcessLinux::%s deferred_signal_tid %" PRIu64 ", skip_stop_request_tid %" PRIu64, __FUNCTION__, deferred_signal_tid, skip_stop_request_tid); |
| 4199 | |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 4200 | const lldb::pid_t pid = GetID (); |
| 4201 | m_coordinator_up->CallAfterRunningThreadsStopWithSkipTIDs (deferred_signal_tid, |
| 4202 | skip_stop_request_tid != LLDB_INVALID_THREAD_ID ? ThreadStateCoordinator::ThreadIDSet {skip_stop_request_tid} : ThreadStateCoordinator::ThreadIDSet (), |
| 4203 | [=](lldb::tid_t request_stop_tid) |
| 4204 | { |
Chaoren Lin | 37c768c | 2015-02-03 01:51:30 +0000 | [diff] [blame] | 4205 | return RequestThreadStop(pid, request_stop_tid); |
Chaoren Lin | 03f12d6 | 2015-02-03 01:50:49 +0000 | [diff] [blame] | 4206 | }, |
| 4207 | call_after_function, |
| 4208 | CoordinatorErrorHandler); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 4209 | } |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 4210 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 4211 | Error |
Chaoren Lin | 86fd8e4 | 2015-02-03 01:51:15 +0000 | [diff] [blame] | 4212 | NativeProcessLinux::RequestThreadStop (const lldb::pid_t pid, const lldb::tid_t tid) |
| 4213 | { |
| 4214 | Log* log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 4215 | if (log) |
| 4216 | log->Printf ("NativeProcessLinux::%s requesting thread stop(pid: %" PRIu64 ", tid: %" PRIu64 ")", __FUNCTION__, pid, tid); |
| 4217 | |
| 4218 | Error err; |
| 4219 | errno = 0; |
| 4220 | if (::tgkill (pid, tid, SIGSTOP) != 0) |
| 4221 | { |
| 4222 | err.SetErrorToErrno (); |
| 4223 | if (log) |
| 4224 | log->Printf ("NativeProcessLinux::%s tgkill(%" PRIu64 ", %" PRIu64 ", SIGSTOP) failed: %s", __FUNCTION__, pid, tid, err.AsCString ()); |
| 4225 | } |
| 4226 | |
| 4227 | return err; |
| 4228 | } |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 4229 | |
| 4230 | Error |
| 4231 | NativeProcessLinux::GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) |
| 4232 | { |
| 4233 | char maps_file_name[32]; |
| 4234 | snprintf(maps_file_name, sizeof(maps_file_name), "/proc/%" PRIu64 "/maps", GetID()); |
| 4235 | |
| 4236 | FileSpec maps_file_spec(maps_file_name, false); |
| 4237 | if (!maps_file_spec.Exists()) { |
| 4238 | file_spec.Clear(); |
| 4239 | return Error("/proc/%" PRIu64 "/maps file doesn't exists!", GetID()); |
| 4240 | } |
| 4241 | |
| 4242 | FileSpec module_file_spec(module_path, true); |
| 4243 | |
| 4244 | std::ifstream maps_file(maps_file_name); |
| 4245 | std::string maps_data_str((std::istreambuf_iterator<char>(maps_file)), std::istreambuf_iterator<char>()); |
| 4246 | StringRef maps_data(maps_data_str.c_str()); |
| 4247 | |
| 4248 | while (!maps_data.empty()) |
| 4249 | { |
| 4250 | StringRef maps_row; |
| 4251 | std::tie(maps_row, maps_data) = maps_data.split('\n'); |
| 4252 | |
| 4253 | SmallVector<StringRef, 16> maps_columns; |
| 4254 | maps_row.split(maps_columns, StringRef(" "), -1, false); |
| 4255 | |
| 4256 | if (maps_columns.size() >= 6) |
| 4257 | { |
| 4258 | file_spec.SetFile(maps_columns[5].str().c_str(), false); |
| 4259 | if (file_spec.GetFilename() == module_file_spec.GetFilename()) |
| 4260 | return Error(); |
| 4261 | } |
| 4262 | } |
| 4263 | |
| 4264 | file_spec.Clear(); |
| 4265 | return Error("Module file (%s) not found in /proc/%" PRIu64 "/maps file!", |
| 4266 | module_file_spec.GetFilename().AsCString(), GetID()); |
| 4267 | } |