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