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