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 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 10 | #include "NativeProcessLinux.h" |
| 11 | |
| 12 | // C Includes |
| 13 | #include <errno.h> |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 14 | #include <stdint.h> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 15 | #include <string.h> |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 16 | #include <unistd.h> |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 17 | |
| 18 | // C++ Includes |
| 19 | #include <fstream> |
Pavel Labath | df7c699 | 2015-06-17 18:38:49 +0000 | [diff] [blame] | 20 | #include <mutex> |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 21 | #include <sstream> |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 22 | #include <string> |
Pavel Labath | 5b981ab | 2015-05-29 12:53:54 +0000 | [diff] [blame] | 23 | #include <unordered_map> |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 24 | |
| 25 | // Other libraries and framework includes |
Tamas Berghammer | d8c338d | 2015-04-15 09:47:02 +0000 | [diff] [blame] | 26 | #include "lldb/Core/EmulateInstruction.h" |
Oleksiy Vyalov | 6edef20 | 2014-11-17 22:16:42 +0000 | [diff] [blame] | 27 | #include "lldb/Core/ModuleSpec.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 28 | #include "lldb/Core/RegisterValue.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 29 | #include "lldb/Core/State.h" |
| 30 | #include "lldb/Host/Host.h" |
Pavel Labath | 5ad891f | 2016-07-21 14:54:03 +0000 | [diff] [blame] | 31 | #include "lldb/Host/HostProcess.h" |
Zachary Turner | 24ae629 | 2017-02-16 19:38:21 +0000 | [diff] [blame] | 32 | #include "lldb/Host/PseudoTerminal.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 33 | #include "lldb/Host/ThreadLauncher.h" |
Pavel Labath | 2a86b55 | 2016-06-14 17:30:52 +0000 | [diff] [blame] | 34 | #include "lldb/Host/common/NativeBreakpoint.h" |
| 35 | #include "lldb/Host/common/NativeRegisterContext.h" |
Pavel Labath | 4ee1c95 | 2017-02-06 18:36:58 +0000 | [diff] [blame] | 36 | #include "lldb/Host/linux/Ptrace.h" |
| 37 | #include "lldb/Host/linux/Uio.h" |
Kamil Rytarowski | 816ae4b | 2017-02-01 14:30:40 +0000 | [diff] [blame] | 38 | #include "lldb/Host/posix/ProcessLauncherPosixFork.h" |
Pavel Labath | 2a86b55 | 2016-06-14 17:30:52 +0000 | [diff] [blame] | 39 | #include "lldb/Symbol/ObjectFile.h" |
Zachary Turner | 90aff47 | 2015-03-03 23:36:51 +0000 | [diff] [blame] | 40 | #include "lldb/Target/Process.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 41 | #include "lldb/Target/ProcessLaunchInfo.h" |
Pavel Labath | 5b981ab | 2015-05-29 12:53:54 +0000 | [diff] [blame] | 42 | #include "lldb/Target/Target.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 43 | #include "lldb/Utility/Error.h" |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 44 | #include "lldb/Utility/LLDBAssert.h" |
Pavel Labath | f805e19 | 2015-07-07 10:08:41 +0000 | [diff] [blame] | 45 | #include "lldb/Utility/StringExtractor.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 46 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 47 | #include "NativeThreadLinux.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 48 | #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" |
Tamas Berghammer | 1e209fc | 2015-03-13 11:36:47 +0000 | [diff] [blame] | 49 | #include "Procfs.h" |
Todd Fiala | cacde7d | 2014-09-27 16:54:22 +0000 | [diff] [blame] | 50 | |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 51 | #include "llvm/Support/FileSystem.h" |
Pavel Labath | 4ee1c95 | 2017-02-06 18:36:58 +0000 | [diff] [blame] | 52 | #include "llvm/Support/Threading.h" |
| 53 | |
Tamas Berghammer | d858487 | 2015-02-06 10:57:40 +0000 | [diff] [blame] | 54 | #include <linux/unistd.h> |
Tamas Berghammer | d858487 | 2015-02-06 10:57:40 +0000 | [diff] [blame] | 55 | #include <sys/socket.h> |
Pavel Labath | df7c699 | 2015-06-17 18:38:49 +0000 | [diff] [blame] | 56 | #include <sys/syscall.h> |
Tamas Berghammer | d858487 | 2015-02-06 10:57:40 +0000 | [diff] [blame] | 57 | #include <sys/types.h> |
Tamas Berghammer | d858487 | 2015-02-06 10:57:40 +0000 | [diff] [blame] | 58 | #include <sys/user.h> |
| 59 | #include <sys/wait.h> |
| 60 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 61 | // Support hardware breakpoints in case it has not been defined |
| 62 | #ifndef TRAP_HWBKPT |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | #define TRAP_HWBKPT 4 |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 64 | #endif |
| 65 | |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 66 | using namespace lldb; |
| 67 | using namespace lldb_private; |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 68 | using namespace lldb_private::process_linux; |
Tamas Berghammer | 7cb18bf | 2015-03-24 11:15:23 +0000 | [diff] [blame] | 69 | using namespace llvm; |
| 70 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 71 | // Private bits we only need internally. |
Pavel Labath | df7c699 | 2015-06-17 18:38:49 +0000 | [diff] [blame] | 72 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | static bool ProcessVmReadvSupported() { |
| 74 | static bool is_supported; |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 75 | static llvm::once_flag flag; |
Pavel Labath | df7c699 | 2015-06-17 18:38:49 +0000 | [diff] [blame] | 76 | |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 77 | llvm::call_once(flag, [] { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 78 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
Pavel Labath | 4abe5d6 | 2016-07-15 10:18:15 +0000 | [diff] [blame] | 79 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | uint32_t source = 0x47424742; |
| 81 | uint32_t dest = 0; |
Pavel Labath | 4abe5d6 | 2016-07-15 10:18:15 +0000 | [diff] [blame] | 82 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 83 | struct iovec local, remote; |
| 84 | remote.iov_base = &source; |
| 85 | local.iov_base = &dest; |
| 86 | remote.iov_len = local.iov_len = sizeof source; |
Pavel Labath | 4abe5d6 | 2016-07-15 10:18:15 +0000 | [diff] [blame] | 87 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 88 | // We shall try if cross-process-memory reads work by attempting to read a |
| 89 | // value from our own process. |
| 90 | ssize_t res = process_vm_readv(getpid(), &local, 1, &remote, 1, 0); |
| 91 | is_supported = (res == sizeof(source) && source == dest); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 92 | if (is_supported) |
| 93 | LLDB_LOG(log, |
| 94 | "Detected kernel support for process_vm_readv syscall. " |
| 95 | "Fast memory reads enabled."); |
| 96 | else |
| 97 | LLDB_LOG(log, |
| 98 | "syscall process_vm_readv failed (error: {0}). Fast memory " |
| 99 | "reads disabled.", |
| 100 | strerror(errno)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | }); |
Pavel Labath | 4abe5d6 | 2016-07-15 10:18:15 +0000 | [diff] [blame] | 102 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 103 | return is_supported; |
Pavel Labath | 4abe5d6 | 2016-07-15 10:18:15 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | namespace { |
| 107 | void MaybeLogLaunchInfo(const ProcessLaunchInfo &info) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 108 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 109 | if (!log) |
| 110 | return; |
| 111 | |
| 112 | if (const FileAction *action = info.GetFileActionForFD(STDIN_FILENO)) |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 113 | LLDB_LOG(log, "setting STDIN to '{0}'", action->GetFileSpec()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | else |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 115 | LLDB_LOG(log, "leaving STDIN as is"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | |
| 117 | if (const FileAction *action = info.GetFileActionForFD(STDOUT_FILENO)) |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 118 | LLDB_LOG(log, "setting STDOUT to '{0}'", action->GetFileSpec()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | else |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 120 | LLDB_LOG(log, "leaving STDOUT as is"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | |
| 122 | if (const FileAction *action = info.GetFileActionForFD(STDERR_FILENO)) |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 123 | LLDB_LOG(log, "setting STDERR to '{0}'", action->GetFileSpec()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | else |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 125 | LLDB_LOG(log, "leaving STDERR as is"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 126 | |
| 127 | int i = 0; |
| 128 | for (const char **args = info.GetArguments().GetConstArgumentVector(); *args; |
| 129 | ++args, ++i) |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 130 | LLDB_LOG(log, "arg {0}: '{1}'", i, *args); |
Pavel Labath | 4abe5d6 | 2016-07-15 10:18:15 +0000 | [diff] [blame] | 131 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 132 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 133 | void DisplayBytes(StreamString &s, void *bytes, uint32_t count) { |
| 134 | uint8_t *ptr = (uint8_t *)bytes; |
| 135 | const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count); |
| 136 | for (uint32_t i = 0; i < loop_count; i++) { |
| 137 | s.Printf("[%x]", *ptr); |
| 138 | ptr++; |
| 139 | } |
| 140 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 141 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 142 | void PtraceDisplayBytes(int &req, void *data, size_t data_size) { |
Pavel Labath | aafe053 | 2017-02-06 19:31:05 +0000 | [diff] [blame] | 143 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 144 | if (!log) |
| 145 | return; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 146 | StreamString buf; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 147 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 148 | switch (req) { |
| 149 | case PTRACE_POKETEXT: { |
| 150 | DisplayBytes(buf, &data, 8); |
Pavel Labath | aafe053 | 2017-02-06 19:31:05 +0000 | [diff] [blame] | 151 | LLDB_LOGV(log, "PTRACE_POKETEXT {0}", buf.GetData()); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 152 | break; |
| 153 | } |
| 154 | case PTRACE_POKEDATA: { |
| 155 | DisplayBytes(buf, &data, 8); |
Pavel Labath | aafe053 | 2017-02-06 19:31:05 +0000 | [diff] [blame] | 156 | LLDB_LOGV(log, "PTRACE_POKEDATA {0}", buf.GetData()); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | case PTRACE_POKEUSER: { |
| 160 | DisplayBytes(buf, &data, 8); |
Pavel Labath | aafe053 | 2017-02-06 19:31:05 +0000 | [diff] [blame] | 161 | LLDB_LOGV(log, "PTRACE_POKEUSER {0}", buf.GetData()); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 162 | break; |
| 163 | } |
| 164 | case PTRACE_SETREGS: { |
| 165 | DisplayBytes(buf, data, data_size); |
Pavel Labath | aafe053 | 2017-02-06 19:31:05 +0000 | [diff] [blame] | 166 | LLDB_LOGV(log, "PTRACE_SETREGS {0}", buf.GetData()); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 167 | break; |
| 168 | } |
| 169 | case PTRACE_SETFPREGS: { |
| 170 | DisplayBytes(buf, data, data_size); |
Pavel Labath | aafe053 | 2017-02-06 19:31:05 +0000 | [diff] [blame] | 171 | LLDB_LOGV(log, "PTRACE_SETFPREGS {0}", buf.GetData()); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 172 | break; |
| 173 | } |
| 174 | case PTRACE_SETSIGINFO: { |
| 175 | DisplayBytes(buf, data, sizeof(siginfo_t)); |
Pavel Labath | aafe053 | 2017-02-06 19:31:05 +0000 | [diff] [blame] | 176 | LLDB_LOGV(log, "PTRACE_SETSIGINFO {0}", buf.GetData()); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 177 | break; |
| 178 | } |
| 179 | case PTRACE_SETREGSET: { |
| 180 | // Extract iov_base from data, which is a pointer to the struct IOVEC |
| 181 | DisplayBytes(buf, *(void **)data, data_size); |
Pavel Labath | aafe053 | 2017-02-06 19:31:05 +0000 | [diff] [blame] | 182 | LLDB_LOGV(log, "PTRACE_SETREGSET {0}", buf.GetData()); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 183 | break; |
| 184 | } |
| 185 | default: {} |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 188 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 189 | static constexpr unsigned k_ptrace_word_size = sizeof(void *); |
| 190 | static_assert(sizeof(long) >= k_ptrace_word_size, |
| 191 | "Size of long must be larger than ptrace word size"); |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 192 | } // end of anonymous namespace |
| 193 | |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 194 | // Simple helper function to ensure flags are enabled on the given file |
| 195 | // descriptor. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 196 | static Error EnsureFDFlags(int fd, int flags) { |
| 197 | Error error; |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 198 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 199 | int status = fcntl(fd, F_GETFL); |
| 200 | if (status == -1) { |
| 201 | error.SetErrorToErrno(); |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 202 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | if (fcntl(fd, F_SETFL, status | flags) == -1) { |
| 206 | error.SetErrorToErrno(); |
| 207 | return error; |
| 208 | } |
| 209 | |
| 210 | return error; |
Pavel Labath | bd7cbc5 | 2015-04-20 13:53:49 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 213 | // ----------------------------------------------------------------------------- |
| 214 | // Public Static Methods |
| 215 | // ----------------------------------------------------------------------------- |
| 216 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 217 | Error NativeProcessProtocol::Launch( |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 218 | ProcessLaunchInfo &launch_info, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 219 | NativeProcessProtocol::NativeDelegate &native_delegate, MainLoop &mainloop, |
| 220 | NativeProcessProtocolSP &native_process_sp) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 221 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 222 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 223 | Error error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 224 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 225 | // Verify the working directory is valid if one was specified. |
| 226 | FileSpec working_dir{launch_info.GetWorkingDirectory()}; |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 227 | if (working_dir && (!working_dir.ResolvePath() || |
| 228 | !llvm::sys::fs::is_directory(working_dir.GetPath()))) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 229 | error.SetErrorStringWithFormat("No such file or directory: %s", |
| 230 | working_dir.GetCString()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 231 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | // Create the NativeProcessLinux in launch mode. |
| 235 | native_process_sp.reset(new NativeProcessLinux()); |
| 236 | |
| 237 | if (!native_process_sp->RegisterNativeDelegate(native_delegate)) { |
| 238 | native_process_sp.reset(); |
| 239 | error.SetErrorStringWithFormat("failed to register the native delegate"); |
| 240 | return error; |
| 241 | } |
| 242 | |
| 243 | error = std::static_pointer_cast<NativeProcessLinux>(native_process_sp) |
| 244 | ->LaunchInferior(mainloop, launch_info); |
| 245 | |
| 246 | if (error.Fail()) { |
| 247 | native_process_sp.reset(); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 248 | LLDB_LOG(log, "failed to launch process: {0}", error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 249 | return error; |
| 250 | } |
| 251 | |
| 252 | launch_info.SetProcessID(native_process_sp->GetID()); |
| 253 | |
| 254 | return error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 257 | Error NativeProcessProtocol::Attach( |
| 258 | lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, |
| 259 | MainLoop &mainloop, NativeProcessProtocolSP &native_process_sp) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 260 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 261 | LLDB_LOG(log, "pid = {0:x}", pid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 262 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 263 | // Retrieve the architecture for the running process. |
| 264 | ArchSpec process_arch; |
| 265 | Error error = ResolveProcessArchitecture(pid, process_arch); |
| 266 | if (!error.Success()) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 267 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 268 | |
| 269 | std::shared_ptr<NativeProcessLinux> native_process_linux_sp( |
| 270 | new NativeProcessLinux()); |
| 271 | |
| 272 | if (!native_process_linux_sp->RegisterNativeDelegate(native_delegate)) { |
| 273 | error.SetErrorStringWithFormat("failed to register the native delegate"); |
| 274 | return error; |
| 275 | } |
| 276 | |
| 277 | native_process_linux_sp->AttachToInferior(mainloop, pid, error); |
| 278 | if (!error.Success()) |
| 279 | return error; |
| 280 | |
| 281 | native_process_sp = native_process_linux_sp; |
| 282 | return error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | // ----------------------------------------------------------------------------- |
| 286 | // Public Instance Methods |
| 287 | // ----------------------------------------------------------------------------- |
| 288 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 289 | NativeProcessLinux::NativeProcessLinux() |
| 290 | : NativeProcessProtocol(LLDB_INVALID_PROCESS_ID), m_arch(), |
| 291 | m_supports_mem_region(eLazyBoolCalculate), m_mem_region_cache(), |
| 292 | m_pending_notification_tid(LLDB_INVALID_THREAD_ID) {} |
| 293 | |
| 294 | void NativeProcessLinux::AttachToInferior(MainLoop &mainloop, lldb::pid_t pid, |
| 295 | Error &error) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 296 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 297 | LLDB_LOG(log, "pid = {0:x}", pid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 298 | |
| 299 | m_sigchld_handle = mainloop.RegisterSignal( |
| 300 | SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, error); |
| 301 | if (!m_sigchld_handle) |
| 302 | return; |
| 303 | |
| 304 | error = ResolveProcessArchitecture(pid, m_arch); |
| 305 | if (!error.Success()) |
| 306 | return; |
| 307 | |
| 308 | // Set the architecture to the exe architecture. |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 309 | LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid, |
| 310 | m_arch.GetArchitectureName()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 311 | m_pid = pid; |
| 312 | SetState(eStateAttaching); |
| 313 | |
| 314 | Attach(pid, error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 317 | Error NativeProcessLinux::LaunchInferior(MainLoop &mainloop, |
| 318 | ProcessLaunchInfo &launch_info) { |
| 319 | Error error; |
| 320 | m_sigchld_handle = mainloop.RegisterSignal( |
| 321 | SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, error); |
| 322 | if (!m_sigchld_handle) |
| 323 | return error; |
| 324 | |
| 325 | SetState(eStateLaunching); |
| 326 | |
| 327 | MaybeLogLaunchInfo(launch_info); |
| 328 | |
| 329 | ::pid_t pid = |
Kamil Rytarowski | 816ae4b | 2017-02-01 14:30:40 +0000 | [diff] [blame] | 330 | ProcessLauncherPosixFork().LaunchProcess(launch_info, error).GetProcessId(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 331 | if (error.Fail()) |
| 332 | return error; |
| 333 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 334 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 335 | |
| 336 | // Wait for the child process to trap on its call to execve. |
| 337 | ::pid_t wpid; |
| 338 | int status; |
| 339 | if ((wpid = waitpid(pid, &status, 0)) < 0) { |
| 340 | error.SetErrorToErrno(); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 341 | LLDB_LOG(log, "waitpid for inferior failed with %s", error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 342 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 343 | // Mark the inferior as invalid. |
| 344 | // FIXME this could really use a new state - eStateLaunchFailure. For now, |
| 345 | // using eStateInvalid. |
| 346 | SetState(StateType::eStateInvalid); |
Pavel Labath | 19cbe96 | 2015-07-21 13:20:32 +0000 | [diff] [blame] | 347 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 348 | return error; |
| 349 | } |
| 350 | assert(WIFSTOPPED(status) && (wpid == static_cast<::pid_t>(pid)) && |
| 351 | "Could not sync with inferior process."); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 352 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 353 | LLDB_LOG(log, "inferior started, now in stopped state"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 354 | error = SetDefaultPtraceOpts(pid); |
| 355 | if (error.Fail()) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 356 | LLDB_LOG(log, "failed to set default ptrace options: {0}", error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 357 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 358 | // Mark the inferior as invalid. |
| 359 | // FIXME this could really use a new state - eStateLaunchFailure. For now, |
| 360 | // using eStateInvalid. |
| 361 | SetState(StateType::eStateInvalid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 362 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 363 | return error; |
| 364 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 365 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 366 | // Release the master terminal descriptor and pass it off to the |
| 367 | // NativeProcessLinux instance. Similarly stash the inferior pid. |
| 368 | m_terminal_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor(); |
| 369 | m_pid = pid; |
| 370 | launch_info.SetProcessID(pid); |
Pavel Labath | 4abe5d6 | 2016-07-15 10:18:15 +0000 | [diff] [blame] | 371 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 372 | if (m_terminal_fd != -1) { |
| 373 | error = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); |
| 374 | if (error.Fail()) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 375 | LLDB_LOG(log, |
| 376 | "inferior EnsureFDFlags failed for ensuring terminal " |
| 377 | "O_NONBLOCK setting: {0}", |
| 378 | error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 379 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 380 | // Mark the inferior as invalid. |
| 381 | // FIXME this could really use a new state - eStateLaunchFailure. For |
| 382 | // now, using eStateInvalid. |
| 383 | SetState(StateType::eStateInvalid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 384 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 385 | return error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 386 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 387 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 388 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 389 | LLDB_LOG(log, "adding pid = {0}", pid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 390 | ResolveProcessArchitecture(m_pid, m_arch); |
| 391 | NativeThreadLinuxSP thread_sp = AddThread(pid); |
| 392 | assert(thread_sp && "AddThread() returned a nullptr thread"); |
| 393 | thread_sp->SetStoppedBySignal(SIGSTOP); |
| 394 | ThreadWasCreated(*thread_sp); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 395 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 396 | // Let our process instance know the thread has stopped. |
| 397 | SetCurrentThreadID(thread_sp->GetID()); |
| 398 | SetState(StateType::eStateStopped); |
| 399 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 400 | if (error.Fail()) |
| 401 | LLDB_LOG(log, "inferior launching failed {0}", error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 402 | return error; |
| 403 | } |
| 404 | |
| 405 | ::pid_t NativeProcessLinux::Attach(lldb::pid_t pid, Error &error) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 406 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 407 | |
| 408 | // Use a map to keep track of the threads which we have attached/need to |
| 409 | // attach. |
| 410 | Host::TidMap tids_to_attach; |
| 411 | if (pid <= 1) { |
| 412 | error.SetErrorToGenericError(); |
| 413 | error.SetErrorString("Attaching to process 1 is not allowed."); |
| 414 | return -1; |
| 415 | } |
| 416 | |
| 417 | while (Host::FindProcessThreads(pid, tids_to_attach)) { |
| 418 | for (Host::TidMap::iterator it = tids_to_attach.begin(); |
| 419 | it != tids_to_attach.end();) { |
| 420 | if (it->second == false) { |
| 421 | lldb::tid_t tid = it->first; |
| 422 | |
| 423 | // Attach to the requested process. |
| 424 | // An attach will cause the thread to stop with a SIGSTOP. |
| 425 | error = PtraceWrapper(PTRACE_ATTACH, tid); |
| 426 | if (error.Fail()) { |
| 427 | // No such thread. The thread may have exited. |
| 428 | // More error handling may be needed. |
| 429 | if (error.GetError() == ESRCH) { |
| 430 | it = tids_to_attach.erase(it); |
| 431 | continue; |
| 432 | } else |
| 433 | return -1; |
| 434 | } |
| 435 | |
| 436 | int status; |
| 437 | // Need to use __WALL otherwise we receive an error with errno=ECHLD |
| 438 | // At this point we should have a thread stopped if waitpid succeeds. |
| 439 | if ((status = waitpid(tid, NULL, __WALL)) < 0) { |
| 440 | // No such thread. The thread may have exited. |
| 441 | // More error handling may be needed. |
| 442 | if (errno == ESRCH) { |
| 443 | it = tids_to_attach.erase(it); |
| 444 | continue; |
| 445 | } else { |
| 446 | error.SetErrorToErrno(); |
| 447 | return -1; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | error = SetDefaultPtraceOpts(tid); |
| 452 | if (error.Fail()) |
| 453 | return -1; |
| 454 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 455 | LLDB_LOG(log, "adding tid = {0}", tid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 456 | it->second = true; |
| 457 | |
| 458 | // Create the thread, mark it as stopped. |
| 459 | NativeThreadLinuxSP thread_sp(AddThread(static_cast<lldb::tid_t>(tid))); |
| 460 | assert(thread_sp && "AddThread() returned a nullptr"); |
| 461 | |
| 462 | // This will notify this is a new thread and tell the system it is |
| 463 | // stopped. |
| 464 | thread_sp->SetStoppedBySignal(SIGSTOP); |
| 465 | ThreadWasCreated(*thread_sp); |
| 466 | SetCurrentThreadID(thread_sp->GetID()); |
| 467 | } |
| 468 | |
| 469 | // move the loop forward |
| 470 | ++it; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | if (tids_to_attach.size() > 0) { |
| 475 | m_pid = pid; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 476 | // Let our process instance know the thread has stopped. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 477 | SetState(StateType::eStateStopped); |
| 478 | } else { |
| 479 | error.SetErrorToGenericError(); |
| 480 | error.SetErrorString("No such process."); |
| 481 | return -1; |
| 482 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 483 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 484 | return pid; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 487 | Error NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) { |
| 488 | long ptrace_opts = 0; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 489 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 490 | // Have the child raise an event on exit. This is used to keep the child in |
| 491 | // limbo until it is destroyed. |
| 492 | ptrace_opts |= PTRACE_O_TRACEEXIT; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 493 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 494 | // Have the tracer trace threads which spawn in the inferior process. |
| 495 | // TODO: if we want to support tracing the inferiors' child, add the |
| 496 | // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK) |
| 497 | ptrace_opts |= PTRACE_O_TRACECLONE; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 498 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 499 | // Have the tracer notify us before execve returns |
| 500 | // (needed to disable legacy SIGTRAP generation) |
| 501 | ptrace_opts |= PTRACE_O_TRACEEXEC; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 502 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 503 | return PtraceWrapper(PTRACE_SETOPTIONS, pid, nullptr, (void *)ptrace_opts); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 506 | static ExitType convert_pid_status_to_exit_type(int status) { |
| 507 | if (WIFEXITED(status)) |
| 508 | return ExitType::eExitTypeExit; |
| 509 | else if (WIFSIGNALED(status)) |
| 510 | return ExitType::eExitTypeSignal; |
| 511 | else if (WIFSTOPPED(status)) |
| 512 | return ExitType::eExitTypeStop; |
| 513 | else { |
| 514 | // We don't know what this is. |
| 515 | return ExitType::eExitTypeInvalid; |
| 516 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 519 | static int convert_pid_status_to_return_code(int status) { |
| 520 | if (WIFEXITED(status)) |
| 521 | return WEXITSTATUS(status); |
| 522 | else if (WIFSIGNALED(status)) |
| 523 | return WTERMSIG(status); |
| 524 | else if (WIFSTOPPED(status)) |
| 525 | return WSTOPSIG(status); |
| 526 | else { |
| 527 | // We don't know what this is. |
| 528 | return ExitType::eExitTypeInvalid; |
| 529 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Pavel Labath | 1107b5a | 2015-04-17 14:07:49 +0000 | [diff] [blame] | 532 | // Handles all waitpid events from the inferior process. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 533 | void NativeProcessLinux::MonitorCallback(lldb::pid_t pid, bool exited, |
| 534 | int signal, int status) { |
| 535 | Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 536 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 537 | // Certain activities differ based on whether the pid is the tid of the main |
| 538 | // thread. |
| 539 | const bool is_main_thread = (pid == GetID()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 540 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 541 | // Handle when the thread exits. |
| 542 | if (exited) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 543 | LLDB_LOG(log, "got exit signal({0}) , tid = {1} ({2} main thread)", signal, |
| 544 | pid, is_main_thread ? "is" : "is not"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 545 | |
| 546 | // This is a thread that exited. Ensure we're not tracking it anymore. |
| 547 | const bool thread_found = StopTrackingThread(pid); |
| 548 | |
| 549 | if (is_main_thread) { |
| 550 | // We only set the exit status and notify the delegate if we haven't |
| 551 | // already set the process |
| 552 | // state to an exited state. We normally should have received a SIGTRAP | |
| 553 | // (PTRACE_EVENT_EXIT << 8) |
| 554 | // for the main thread. |
| 555 | const bool already_notified = (GetState() == StateType::eStateExited) || |
| 556 | (GetState() == StateType::eStateCrashed); |
| 557 | if (!already_notified) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 558 | LLDB_LOG( |
| 559 | log, |
| 560 | "tid = {0} handling main thread exit ({1}), expected exit state " |
| 561 | "already set but state was {2} instead, setting exit state now", |
| 562 | pid, |
| 563 | thread_found ? "stopped tracking thread metadata" |
| 564 | : "thread metadata not found", |
Pavel Labath | 8198db3 | 2017-01-24 11:48:25 +0000 | [diff] [blame] | 565 | GetState()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 566 | // The main thread exited. We're done monitoring. Report to delegate. |
| 567 | SetExitStatus(convert_pid_status_to_exit_type(status), |
| 568 | convert_pid_status_to_return_code(status), nullptr, true); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 569 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 570 | // Notify delegate that our process has exited. |
| 571 | SetState(StateType::eStateExited, true); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 572 | } else |
| 573 | LLDB_LOG(log, "tid = {0} main thread now exited (%s)", pid, |
| 574 | thread_found ? "stopped tracking thread metadata" |
| 575 | : "thread metadata not found"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 576 | } else { |
| 577 | // Do we want to report to the delegate in this case? I think not. If |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 578 | // this was an orderly thread exit, we would already have received the |
| 579 | // SIGTRAP | (PTRACE_EVENT_EXIT << 8) signal, and we would have done an |
| 580 | // all-stop then. |
| 581 | LLDB_LOG(log, "tid = {0} handling non-main thread exit (%s)", pid, |
| 582 | thread_found ? "stopped tracking thread metadata" |
| 583 | : "thread metadata not found"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 584 | } |
| 585 | return; |
| 586 | } |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 587 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 588 | siginfo_t info; |
| 589 | const auto info_err = GetSignalInfo(pid, &info); |
| 590 | auto thread_sp = GetThreadByID(pid); |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 591 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 592 | if (!thread_sp) { |
| 593 | // Normally, the only situation when we cannot find the thread is if we have |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 594 | // just received a new thread notification. This is indicated by |
| 595 | // GetSignalInfo() returning si_code == SI_USER and si_pid == 0 |
| 596 | LLDB_LOG(log, "received notification about an unknown tid {0}.", pid); |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 597 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 598 | if (info_err.Fail()) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 599 | LLDB_LOG(log, |
| 600 | "(tid {0}) GetSignalInfo failed ({1}). " |
| 601 | "Ingoring this notification.", |
| 602 | pid, info_err); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 603 | return; |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 606 | LLDB_LOG(log, "tid {0}, si_code: {1}, si_pid: {2}", pid, info.si_code, |
| 607 | info.si_pid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 608 | |
| 609 | auto thread_sp = AddThread(pid); |
| 610 | // Resume the newly created thread. |
| 611 | ResumeThread(*thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); |
| 612 | ThreadWasCreated(*thread_sp); |
| 613 | return; |
| 614 | } |
| 615 | |
| 616 | // Get details on the signal raised. |
| 617 | if (info_err.Success()) { |
| 618 | // We have retrieved the signal info. Dispatch appropriately. |
| 619 | if (info.si_signo == SIGTRAP) |
| 620 | MonitorSIGTRAP(info, *thread_sp); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 621 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 622 | MonitorSignal(info, *thread_sp, exited); |
| 623 | } else { |
| 624 | if (info_err.GetError() == EINVAL) { |
| 625 | // This is a group stop reception for this tid. |
| 626 | // We can reach here if we reinject SIGSTOP, SIGSTP, SIGTTIN or SIGTTOU |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 627 | // into the tracee, triggering the group-stop mechanism. Normally |
| 628 | // receiving these would stop the process, pending a SIGCONT. Simulating |
| 629 | // this state in a debugger is hard and is generally not needed (one use |
| 630 | // case is debugging background task being managed by a shell). For |
| 631 | // general use, it is sufficient to stop the process in a signal-delivery |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 632 | // stop which happens before the group stop. This done by MonitorSignal |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 633 | // and works correctly for all signals. |
| 634 | LLDB_LOG(log, |
| 635 | "received a group stop for pid {0} tid {1}. Transparent " |
| 636 | "handling of group stops not supported, resuming the " |
| 637 | "thread.", |
| 638 | GetID(), pid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 639 | ResumeThread(*thread_sp, thread_sp->GetState(), |
| 640 | LLDB_INVALID_SIGNAL_NUMBER); |
| 641 | } else { |
| 642 | // ptrace(GETSIGINFO) failed (but not due to group-stop). |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 643 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 644 | // A return value of ESRCH means the thread/process is no longer on the |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 645 | // system, so it was killed somehow outside of our control. Either way, |
| 646 | // we can't do anything with it anymore. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 647 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 648 | // Stop tracking the metadata for the thread since it's entirely off the |
| 649 | // system now. |
| 650 | const bool thread_found = StopTrackingThread(pid); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 651 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 652 | LLDB_LOG(log, |
| 653 | "GetSignalInfo failed: {0}, tid = {1}, signal = {2}, " |
| 654 | "status = {3}, main_thread = {4}, thread_found: {5}", |
| 655 | info_err, pid, signal, status, is_main_thread, thread_found); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 656 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 657 | if (is_main_thread) { |
| 658 | // Notify the delegate - our process is not available but appears to |
| 659 | // have been killed outside |
| 660 | // our control. Is eStateExited the right exit state in this case? |
| 661 | SetExitStatus(convert_pid_status_to_exit_type(status), |
| 662 | convert_pid_status_to_return_code(status), nullptr, true); |
| 663 | SetState(StateType::eStateExited, true); |
| 664 | } else { |
| 665 | // This thread was pulled out from underneath us. Anything to do here? |
| 666 | // Do we want to do an all stop? |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 667 | LLDB_LOG(log, |
| 668 | "pid {0} tid {1} non-main thread exit occurred, didn't " |
| 669 | "tell delegate anything since thread disappeared out " |
| 670 | "from underneath us", |
| 671 | GetID(), pid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 672 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 673 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 674 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 677 | void NativeProcessLinux::WaitForNewThread(::pid_t tid) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 678 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
Pavel Labath | 426bdf8 | 2015-04-28 07:51:52 +0000 | [diff] [blame] | 679 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 680 | NativeThreadLinuxSP new_thread_sp = GetThreadByID(tid); |
Pavel Labath | 426bdf8 | 2015-04-28 07:51:52 +0000 | [diff] [blame] | 681 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 682 | if (new_thread_sp) { |
| 683 | // We are already tracking the thread - we got the event on the new thread |
| 684 | // (see |
| 685 | // MonitorSignal) before this one. We are done. |
| 686 | return; |
| 687 | } |
Pavel Labath | 426bdf8 | 2015-04-28 07:51:52 +0000 | [diff] [blame] | 688 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 689 | // The thread is not tracked yet, let's wait for it to appear. |
| 690 | int status = -1; |
| 691 | ::pid_t wait_pid; |
| 692 | do { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 693 | LLDB_LOG(log, |
| 694 | "received thread creation event for tid {0}. tid not tracked " |
| 695 | "yet, waiting for thread to appear...", |
| 696 | tid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 697 | wait_pid = waitpid(tid, &status, __WALL); |
| 698 | } while (wait_pid == -1 && errno == EINTR); |
| 699 | // Since we are waiting on a specific tid, this must be the creation event. |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 700 | // But let's do some checks just in case. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 701 | if (wait_pid != tid) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 702 | LLDB_LOG(log, |
| 703 | "waiting for tid {0} failed. Assuming the thread has " |
| 704 | "disappeared in the meantime", |
| 705 | tid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 706 | // The only way I know of this could happen is if the whole process was |
| 707 | // SIGKILLed in the mean time. In any case, we can't do anything about that |
| 708 | // now. |
| 709 | return; |
| 710 | } |
| 711 | if (WIFEXITED(status)) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 712 | LLDB_LOG(log, |
| 713 | "waiting for tid {0} returned an 'exited' event. Not " |
| 714 | "tracking the thread.", |
| 715 | tid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 716 | // Also a very improbable event. |
| 717 | return; |
| 718 | } |
Pavel Labath | 426bdf8 | 2015-04-28 07:51:52 +0000 | [diff] [blame] | 719 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 720 | LLDB_LOG(log, "pid = {0}: tracking new thread tid {1}", GetID(), tid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 721 | new_thread_sp = AddThread(tid); |
| 722 | ResumeThread(*new_thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); |
| 723 | ThreadWasCreated(*new_thread_sp); |
Pavel Labath | 426bdf8 | 2015-04-28 07:51:52 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 726 | void NativeProcessLinux::MonitorSIGTRAP(const siginfo_t &info, |
| 727 | NativeThreadLinux &thread) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 728 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 729 | const bool is_main_thread = (thread.GetID() == GetID()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 730 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 731 | assert(info.si_signo == SIGTRAP && "Unexpected child signal!"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 732 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 733 | switch (info.si_code) { |
| 734 | // TODO: these two cases are required if we want to support tracing of the |
| 735 | // inferiors' children. We'd need this to debug a monitor. |
| 736 | // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)): |
| 737 | // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)): |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 738 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 739 | case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)): { |
| 740 | // This is the notification on the parent thread which informs us of new |
| 741 | // thread |
| 742 | // creation. |
| 743 | // We don't want to do anything with the parent thread so we just resume it. |
| 744 | // In case we |
| 745 | // want to implement "break on thread creation" functionality, we would need |
| 746 | // to stop |
| 747 | // here. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 748 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 749 | unsigned long event_message = 0; |
| 750 | if (GetEventMessage(thread.GetID(), &event_message).Fail()) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 751 | LLDB_LOG(log, |
| 752 | "pid {0} received thread creation event but " |
| 753 | "GetEventMessage failed so we don't know the new tid", |
| 754 | thread.GetID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 755 | } else |
| 756 | WaitForNewThread(event_message); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 757 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 758 | ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); |
| 759 | break; |
| 760 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 761 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 762 | case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): { |
| 763 | NativeThreadLinuxSP main_thread_sp; |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 764 | LLDB_LOG(log, "received exec event, code = {0}", info.si_code ^ SIGTRAP); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 765 | |
| 766 | // Exec clears any pending notifications. |
| 767 | m_pending_notification_tid = LLDB_INVALID_THREAD_ID; |
| 768 | |
| 769 | // Remove all but the main thread here. Linux fork creates a new process |
| 770 | // which only copies the main thread. |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 771 | LLDB_LOG(log, "exec received, stop tracking all but main thread"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 772 | |
| 773 | for (auto thread_sp : m_threads) { |
| 774 | const bool is_main_thread = thread_sp && thread_sp->GetID() == GetID(); |
| 775 | if (is_main_thread) { |
| 776 | main_thread_sp = std::static_pointer_cast<NativeThreadLinux>(thread_sp); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 777 | LLDB_LOG(log, "found main thread with tid {0}, keeping", |
| 778 | main_thread_sp->GetID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 779 | } else { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 780 | LLDB_LOG(log, "discarding non-main-thread tid {0} due to exec", |
| 781 | thread_sp->GetID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 782 | } |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 783 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 784 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 785 | m_threads.clear(); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 786 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 787 | if (main_thread_sp) { |
| 788 | m_threads.push_back(main_thread_sp); |
| 789 | SetCurrentThreadID(main_thread_sp->GetID()); |
| 790 | main_thread_sp->SetStoppedByExec(); |
| 791 | } else { |
| 792 | SetCurrentThreadID(LLDB_INVALID_THREAD_ID); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 793 | LLDB_LOG(log, |
| 794 | "pid {0} no main thread found, discarded all threads, " |
| 795 | "we're in a no-thread state!", |
| 796 | GetID()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 799 | // Tell coordinator about about the "new" (since exec) stopped main thread. |
| 800 | ThreadWasCreated(*main_thread_sp); |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 801 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 802 | // Let our delegate know we have just exec'd. |
| 803 | NotifyDidExec(); |
| 804 | |
| 805 | // If we have a main thread, indicate we are stopped. |
| 806 | assert(main_thread_sp && "exec called during ptraced process but no main " |
| 807 | "thread metadata tracked"); |
| 808 | |
| 809 | // Let the process know we're stopped. |
| 810 | StopRunningThreads(main_thread_sp->GetID()); |
| 811 | |
| 812 | break; |
| 813 | } |
| 814 | |
| 815 | case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): { |
| 816 | // The inferior process or one of its threads is about to exit. |
| 817 | // We don't want to do anything with the thread so we just resume it. In |
| 818 | // case we |
| 819 | // want to implement "break on thread exit" functionality, we would need to |
| 820 | // stop |
| 821 | // here. |
| 822 | |
| 823 | unsigned long data = 0; |
| 824 | if (GetEventMessage(thread.GetID(), &data).Fail()) |
| 825 | data = -1; |
| 826 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 827 | LLDB_LOG(log, |
| 828 | "received PTRACE_EVENT_EXIT, data = {0:x}, WIFEXITED={1}, " |
| 829 | "WIFSIGNALED={2}, pid = {3}, main_thread = {4}", |
| 830 | data, WIFEXITED(data), WIFSIGNALED(data), thread.GetID(), |
| 831 | is_main_thread); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 832 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 833 | if (is_main_thread) { |
| 834 | SetExitStatus(convert_pid_status_to_exit_type(data), |
| 835 | convert_pid_status_to_return_code(data), nullptr, true); |
| 836 | } |
| 837 | |
| 838 | StateType state = thread.GetState(); |
| 839 | if (!StateIsRunningState(state)) { |
| 840 | // Due to a kernel bug, we may sometimes get this stop after the inferior |
| 841 | // gets a |
| 842 | // SIGKILL. This confuses our state tracking logic in ResumeThread(), |
| 843 | // since normally, |
| 844 | // we should not be receiving any ptrace events while the inferior is |
| 845 | // stopped. This |
| 846 | // makes sure that the inferior is resumed and exits normally. |
| 847 | state = eStateRunning; |
| 848 | } |
| 849 | ResumeThread(thread, state, LLDB_INVALID_SIGNAL_NUMBER); |
| 850 | |
| 851 | break; |
| 852 | } |
| 853 | |
| 854 | case 0: |
| 855 | case TRAP_TRACE: // We receive this on single stepping. |
| 856 | case TRAP_HWBKPT: // We receive this on watchpoint hit |
| 857 | { |
| 858 | // If a watchpoint was hit, report it |
| 859 | uint32_t wp_index; |
| 860 | Error error = thread.GetRegisterContext()->GetWatchpointHitIndex( |
| 861 | wp_index, (uintptr_t)info.si_addr); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 862 | if (error.Fail()) |
| 863 | LLDB_LOG(log, |
| 864 | "received error while checking for watchpoint hits, pid = " |
| 865 | "{0}, error = {1}", |
| 866 | thread.GetID(), error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 867 | if (wp_index != LLDB_INVALID_INDEX32) { |
| 868 | MonitorWatchpoint(thread, wp_index); |
| 869 | break; |
| 870 | } |
| 871 | |
Omair Javaid | d5ffbad | 2017-02-24 13:27:31 +0000 | [diff] [blame] | 872 | // If a breakpoint was hit, report it |
| 873 | uint32_t bp_index; |
| 874 | error = thread.GetRegisterContext()->GetHardwareBreakHitIndex( |
| 875 | bp_index, (uintptr_t)info.si_addr); |
| 876 | if (error.Fail()) |
| 877 | LLDB_LOG(log, "received error while checking for hardware " |
| 878 | "breakpoint hits, pid = {0}, error = {1}", |
| 879 | thread.GetID(), error); |
| 880 | if (bp_index != LLDB_INVALID_INDEX32) { |
| 881 | MonitorBreakpoint(thread); |
| 882 | break; |
| 883 | } |
| 884 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 885 | // Otherwise, report step over |
| 886 | MonitorTrace(thread); |
| 887 | break; |
| 888 | } |
| 889 | |
| 890 | case SI_KERNEL: |
Mohit K. Bhakkad | 3579996 | 2015-06-18 04:53:18 +0000 | [diff] [blame] | 891 | #if defined __mips__ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 892 | // For mips there is no special signal for watchpoint |
| 893 | // So we check for watchpoint in kernel trap |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 894 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 895 | // If a watchpoint was hit, report it |
| 896 | uint32_t wp_index; |
| 897 | Error error = thread.GetRegisterContext()->GetWatchpointHitIndex( |
| 898 | wp_index, LLDB_INVALID_ADDRESS); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 899 | if (error.Fail()) |
| 900 | LLDB_LOG(log, |
| 901 | "received error while checking for watchpoint hits, pid = " |
| 902 | "{0}, error = {1}", |
| 903 | thread.GetID(), error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 904 | if (wp_index != LLDB_INVALID_INDEX32) { |
| 905 | MonitorWatchpoint(thread, wp_index); |
| 906 | break; |
| 907 | } |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 908 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 909 | // NO BREAK |
Mohit K. Bhakkad | 3579996 | 2015-06-18 04:53:18 +0000 | [diff] [blame] | 910 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 911 | case TRAP_BRKPT: |
| 912 | MonitorBreakpoint(thread); |
| 913 | break; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 914 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 915 | case SIGTRAP: |
| 916 | case (SIGTRAP | 0x80): |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 917 | LLDB_LOG( |
| 918 | log, |
| 919 | "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming", |
| 920 | info.si_code, GetID(), thread.GetID()); |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 921 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 922 | // Ignore these signals until we know more about them. |
| 923 | ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); |
| 924 | break; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 925 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 926 | default: |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 927 | LLDB_LOG( |
| 928 | log, |
| 929 | "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming", |
| 930 | info.si_code, GetID(), thread.GetID()); |
| 931 | llvm_unreachable("Unexpected SIGTRAP code!"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 932 | break; |
| 933 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 936 | void NativeProcessLinux::MonitorTrace(NativeThreadLinux &thread) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 937 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 938 | LLDB_LOG(log, "received trace event, pid = {0}", thread.GetID()); |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 939 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 940 | // This thread is currently stopped. |
| 941 | thread.SetStoppedByTrace(); |
| 942 | |
| 943 | StopRunningThreads(thread.GetID()); |
| 944 | } |
| 945 | |
| 946 | void NativeProcessLinux::MonitorBreakpoint(NativeThreadLinux &thread) { |
| 947 | Log *log( |
| 948 | GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 949 | LLDB_LOG(log, "received breakpoint event, pid = {0}", thread.GetID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 950 | |
| 951 | // Mark the thread as stopped at breakpoint. |
| 952 | thread.SetStoppedByBreakpoint(); |
| 953 | Error error = FixupBreakpointPCAsNeeded(thread); |
| 954 | if (error.Fail()) |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 955 | LLDB_LOG(log, "pid = {0} fixup: {1}", thread.GetID(), error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 956 | |
| 957 | if (m_threads_stepping_with_breakpoint.find(thread.GetID()) != |
| 958 | m_threads_stepping_with_breakpoint.end()) |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 959 | thread.SetStoppedByTrace(); |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 960 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 961 | StopRunningThreads(thread.GetID()); |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 964 | void NativeProcessLinux::MonitorWatchpoint(NativeThreadLinux &thread, |
| 965 | uint32_t wp_index) { |
| 966 | Log *log( |
| 967 | GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_WATCHPOINTS)); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 968 | LLDB_LOG(log, "received watchpoint event, pid = {0}, wp_index = {1}", |
| 969 | thread.GetID(), wp_index); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 970 | |
| 971 | // Mark the thread as stopped at watchpoint. |
| 972 | // The address is at (lldb::addr_t)info->si_addr if we need it. |
| 973 | thread.SetStoppedByWatchpoint(wp_index); |
| 974 | |
| 975 | // We need to tell all other running threads before we notify the delegate |
| 976 | // about this stop. |
| 977 | StopRunningThreads(thread.GetID()); |
| 978 | } |
| 979 | |
| 980 | void NativeProcessLinux::MonitorSignal(const siginfo_t &info, |
| 981 | NativeThreadLinux &thread, bool exited) { |
| 982 | const int signo = info.si_signo; |
| 983 | const bool is_from_llgs = info.si_pid == getpid(); |
| 984 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 985 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 986 | |
| 987 | // POSIX says that process behaviour is undefined after it ignores a SIGFPE, |
| 988 | // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a |
| 989 | // kill(2) or raise(3). Similarly for tgkill(2) on Linux. |
| 990 | // |
| 991 | // IOW, user generated signals never generate what we consider to be a |
| 992 | // "crash". |
| 993 | // |
| 994 | // Similarly, ACK signals generated by this monitor. |
| 995 | |
| 996 | // Handle the signal. |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 997 | LLDB_LOG(log, |
| 998 | "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, " |
| 999 | "waitpid pid = {4})", |
| 1000 | Host::GetSignalAsCString(signo), signo, info.si_code, |
| 1001 | thread.GetID()); |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 1002 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1003 | // Check for thread stop notification. |
| 1004 | if (is_from_llgs && (info.si_code == SI_TKILL) && (signo == SIGSTOP)) { |
| 1005 | // This is a tgkill()-based stop. |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1006 | LLDB_LOG(log, "pid {0} tid {1}, thread stopped", GetID(), thread.GetID()); |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 1007 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1008 | // Check that we're not already marked with a stop reason. |
| 1009 | // Note this thread really shouldn't already be marked as stopped - if we |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1010 | // were, that would imply that the kernel signaled us with the thread |
| 1011 | // stopping which we handled and marked as stopped, and that, without an |
| 1012 | // intervening resume, we received another stop. It is more likely that we |
| 1013 | // are missing the marking of a run state somewhere if we find that the |
| 1014 | // thread was marked as stopped. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1015 | const StateType thread_state = thread.GetState(); |
| 1016 | if (!StateIsStoppedState(thread_state, false)) { |
| 1017 | // An inferior thread has stopped because of a SIGSTOP we have sent it. |
| 1018 | // Generally, these are not important stops and we don't want to report |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1019 | // them as they are just used to stop other threads when one thread (the |
| 1020 | // one with the *real* stop reason) hits a breakpoint (watchpoint, |
| 1021 | // etc...). However, in the case of an asynchronous Interrupt(), this *is* |
| 1022 | // the real stop reason, so we leave the signal intact if this is the |
| 1023 | // thread that was chosen as the triggering thread. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1024 | if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) { |
| 1025 | if (m_pending_notification_tid == thread.GetID()) |
| 1026 | thread.SetStoppedBySignal(SIGSTOP, &info); |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 1027 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1028 | thread.SetStoppedWithNoReason(); |
Pavel Labath | b9cc0c7 | 2015-08-24 09:22:04 +0000 | [diff] [blame] | 1029 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1030 | SetCurrentThreadID(thread.GetID()); |
| 1031 | SignalIfAllThreadsStopped(); |
| 1032 | } else { |
| 1033 | // We can end up here if stop was initiated by LLGS but by this time a |
| 1034 | // thread stop has occurred - maybe initiated by another event. |
| 1035 | Error error = ResumeThread(thread, thread.GetState(), 0); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1036 | if (error.Fail()) |
| 1037 | LLDB_LOG(log, "failed to resume thread {0}: {1}", thread.GetID(), |
| 1038 | error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1039 | } |
| 1040 | } else { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1041 | LLDB_LOG(log, |
| 1042 | "pid {0} tid {1}, thread was already marked as a stopped " |
| 1043 | "state (state={2}), leaving stop signal as is", |
Pavel Labath | 8198db3 | 2017-01-24 11:48:25 +0000 | [diff] [blame] | 1044 | GetID(), thread.GetID(), thread_state); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1045 | SignalIfAllThreadsStopped(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1048 | // Done handling. |
| 1049 | return; |
| 1050 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1051 | |
Pavel Labath | 4a705e7 | 2017-02-24 09:29:14 +0000 | [diff] [blame] | 1052 | // Check if debugger should stop at this signal or just ignore it |
| 1053 | // and resume the inferior. |
| 1054 | if (m_signals_to_ignore.find(signo) != m_signals_to_ignore.end()) { |
| 1055 | ResumeThread(thread, thread.GetState(), signo); |
| 1056 | return; |
| 1057 | } |
| 1058 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1059 | // This thread is stopped. |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1060 | LLDB_LOG(log, "received signal {0}", Host::GetSignalAsCString(signo)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1061 | thread.SetStoppedBySignal(signo, &info); |
| 1062 | |
| 1063 | // Send a stop to the debugger after we get all other threads to stop. |
| 1064 | StopRunningThreads(thread.GetID()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1067 | namespace { |
| 1068 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1069 | struct EmulatorBaton { |
| 1070 | NativeProcessLinux *m_process; |
| 1071 | NativeRegisterContext *m_reg_context; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1072 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1073 | // eRegisterKindDWARF -> RegsiterValue |
| 1074 | std::unordered_map<uint32_t, RegisterValue> m_register_values; |
Pavel Labath | 6648fcc | 2015-04-27 09:21:14 +0000 | [diff] [blame] | 1075 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1076 | EmulatorBaton(NativeProcessLinux *process, NativeRegisterContext *reg_context) |
| 1077 | : m_process(process), m_reg_context(reg_context) {} |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1078 | }; |
| 1079 | |
| 1080 | } // anonymous namespace |
| 1081 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1082 | static size_t ReadMemoryCallback(EmulateInstruction *instruction, void *baton, |
| 1083 | const EmulateInstruction::Context &context, |
| 1084 | lldb::addr_t addr, void *dst, size_t length) { |
| 1085 | EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1086 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1087 | size_t bytes_read; |
| 1088 | emulator_baton->m_process->ReadMemory(addr, dst, length, bytes_read); |
| 1089 | return bytes_read; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1092 | static bool ReadRegisterCallback(EmulateInstruction *instruction, void *baton, |
| 1093 | const RegisterInfo *reg_info, |
| 1094 | RegisterValue ®_value) { |
| 1095 | EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1096 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1097 | auto it = emulator_baton->m_register_values.find( |
| 1098 | reg_info->kinds[eRegisterKindDWARF]); |
| 1099 | if (it != emulator_baton->m_register_values.end()) { |
| 1100 | reg_value = it->second; |
| 1101 | return true; |
| 1102 | } |
| 1103 | |
| 1104 | // The emulator only fill in the dwarf regsiter numbers (and in some case |
| 1105 | // the generic register numbers). Get the full register info from the |
| 1106 | // register context based on the dwarf register numbers. |
| 1107 | const RegisterInfo *full_reg_info = |
| 1108 | emulator_baton->m_reg_context->GetRegisterInfo( |
| 1109 | eRegisterKindDWARF, reg_info->kinds[eRegisterKindDWARF]); |
| 1110 | |
| 1111 | Error error = |
| 1112 | emulator_baton->m_reg_context->ReadRegister(full_reg_info, reg_value); |
| 1113 | if (error.Success()) |
| 1114 | return true; |
| 1115 | |
| 1116 | return false; |
| 1117 | } |
| 1118 | |
| 1119 | static bool WriteRegisterCallback(EmulateInstruction *instruction, void *baton, |
| 1120 | const EmulateInstruction::Context &context, |
| 1121 | const RegisterInfo *reg_info, |
| 1122 | const RegisterValue ®_value) { |
| 1123 | EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); |
| 1124 | emulator_baton->m_register_values[reg_info->kinds[eRegisterKindDWARF]] = |
| 1125 | reg_value; |
| 1126 | return true; |
| 1127 | } |
| 1128 | |
| 1129 | static size_t WriteMemoryCallback(EmulateInstruction *instruction, void *baton, |
| 1130 | const EmulateInstruction::Context &context, |
| 1131 | lldb::addr_t addr, const void *dst, |
| 1132 | size_t length) { |
| 1133 | return length; |
| 1134 | } |
| 1135 | |
| 1136 | static lldb::addr_t ReadFlags(NativeRegisterContext *regsiter_context) { |
| 1137 | const RegisterInfo *flags_info = regsiter_context->GetRegisterInfo( |
| 1138 | eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); |
| 1139 | return regsiter_context->ReadRegisterAsUnsigned(flags_info, |
| 1140 | LLDB_INVALID_ADDRESS); |
| 1141 | } |
| 1142 | |
| 1143 | Error NativeProcessLinux::SetupSoftwareSingleStepping( |
| 1144 | NativeThreadLinux &thread) { |
| 1145 | Error error; |
| 1146 | NativeRegisterContextSP register_context_sp = thread.GetRegisterContext(); |
| 1147 | |
| 1148 | std::unique_ptr<EmulateInstruction> emulator_ap( |
| 1149 | EmulateInstruction::FindPlugin(m_arch, eInstructionTypePCModifying, |
| 1150 | nullptr)); |
| 1151 | |
| 1152 | if (emulator_ap == nullptr) |
| 1153 | return Error("Instruction emulator not found!"); |
| 1154 | |
| 1155 | EmulatorBaton baton(this, register_context_sp.get()); |
| 1156 | emulator_ap->SetBaton(&baton); |
| 1157 | emulator_ap->SetReadMemCallback(&ReadMemoryCallback); |
| 1158 | emulator_ap->SetReadRegCallback(&ReadRegisterCallback); |
| 1159 | emulator_ap->SetWriteMemCallback(&WriteMemoryCallback); |
| 1160 | emulator_ap->SetWriteRegCallback(&WriteRegisterCallback); |
| 1161 | |
| 1162 | if (!emulator_ap->ReadInstruction()) |
| 1163 | return Error("Read instruction failed!"); |
| 1164 | |
| 1165 | bool emulation_result = |
| 1166 | emulator_ap->EvaluateInstruction(eEmulateInstructionOptionAutoAdvancePC); |
| 1167 | |
| 1168 | const RegisterInfo *reg_info_pc = register_context_sp->GetRegisterInfo( |
| 1169 | eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); |
| 1170 | const RegisterInfo *reg_info_flags = register_context_sp->GetRegisterInfo( |
| 1171 | eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); |
| 1172 | |
| 1173 | auto pc_it = |
| 1174 | baton.m_register_values.find(reg_info_pc->kinds[eRegisterKindDWARF]); |
| 1175 | auto flags_it = |
| 1176 | baton.m_register_values.find(reg_info_flags->kinds[eRegisterKindDWARF]); |
| 1177 | |
| 1178 | lldb::addr_t next_pc; |
| 1179 | lldb::addr_t next_flags; |
| 1180 | if (emulation_result) { |
| 1181 | assert(pc_it != baton.m_register_values.end() && |
| 1182 | "Emulation was successfull but PC wasn't updated"); |
| 1183 | next_pc = pc_it->second.GetAsUInt64(); |
| 1184 | |
| 1185 | if (flags_it != baton.m_register_values.end()) |
| 1186 | next_flags = flags_it->second.GetAsUInt64(); |
| 1187 | else |
| 1188 | next_flags = ReadFlags(register_context_sp.get()); |
| 1189 | } else if (pc_it == baton.m_register_values.end()) { |
| 1190 | // Emulate instruction failed and it haven't changed PC. Advance PC |
| 1191 | // with the size of the current opcode because the emulation of all |
| 1192 | // PC modifying instruction should be successful. The failure most |
| 1193 | // likely caused by a not supported instruction which don't modify PC. |
| 1194 | next_pc = |
| 1195 | register_context_sp->GetPC() + emulator_ap->GetOpcode().GetByteSize(); |
| 1196 | next_flags = ReadFlags(register_context_sp.get()); |
| 1197 | } else { |
| 1198 | // The instruction emulation failed after it modified the PC. It is an |
| 1199 | // unknown error where we can't continue because the next instruction is |
| 1200 | // modifying the PC but we don't know how. |
| 1201 | return Error("Instruction emulation failed unexpectedly."); |
| 1202 | } |
| 1203 | |
| 1204 | if (m_arch.GetMachine() == llvm::Triple::arm) { |
| 1205 | if (next_flags & 0x20) { |
| 1206 | // Thumb mode |
| 1207 | error = SetSoftwareBreakpoint(next_pc, 2); |
| 1208 | } else { |
| 1209 | // Arm mode |
| 1210 | error = SetSoftwareBreakpoint(next_pc, 4); |
Pavel Labath | 6648fcc | 2015-04-27 09:21:14 +0000 | [diff] [blame] | 1211 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1212 | } else if (m_arch.GetMachine() == llvm::Triple::mips64 || |
| 1213 | m_arch.GetMachine() == llvm::Triple::mips64el || |
| 1214 | m_arch.GetMachine() == llvm::Triple::mips || |
| 1215 | m_arch.GetMachine() == llvm::Triple::mipsel) |
| 1216 | error = SetSoftwareBreakpoint(next_pc, 4); |
| 1217 | else { |
| 1218 | // No size hint is given for the next breakpoint |
| 1219 | error = SetSoftwareBreakpoint(next_pc, 0); |
| 1220 | } |
Pavel Labath | 6648fcc | 2015-04-27 09:21:14 +0000 | [diff] [blame] | 1221 | |
Pavel Labath | 42eb690 | 2016-10-26 11:13:56 +0000 | [diff] [blame] | 1222 | // If setting the breakpoint fails because next_pc is out of |
| 1223 | // the address space, ignore it and let the debugee segfault. |
| 1224 | if (error.GetError() == EIO || error.GetError() == EFAULT) { |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 1225 | return Error(); |
Pavel Labath | 42eb690 | 2016-10-26 11:13:56 +0000 | [diff] [blame] | 1226 | } else if (error.Fail()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1227 | return error; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1228 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1229 | m_threads_stepping_with_breakpoint.insert({thread.GetID(), next_pc}); |
Mohit K. Bhakkad | cdc22a8 | 2015-05-07 05:56:27 +0000 | [diff] [blame] | 1230 | |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 1231 | return Error(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | bool NativeProcessLinux::SupportHardwareSingleStepping() const { |
| 1235 | if (m_arch.GetMachine() == llvm::Triple::arm || |
| 1236 | m_arch.GetMachine() == llvm::Triple::mips64 || |
| 1237 | m_arch.GetMachine() == llvm::Triple::mips64el || |
| 1238 | m_arch.GetMachine() == llvm::Triple::mips || |
| 1239 | m_arch.GetMachine() == llvm::Triple::mipsel) |
Pavel Labath | 6648fcc | 2015-04-27 09:21:14 +0000 | [diff] [blame] | 1240 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1241 | return true; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1244 | Error NativeProcessLinux::Resume(const ResumeActionList &resume_actions) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1245 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 1246 | LLDB_LOG(log, "pid {0}", GetID()); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1247 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1248 | bool software_single_step = !SupportHardwareSingleStepping(); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1249 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1250 | if (software_single_step) { |
| 1251 | for (auto thread_sp : m_threads) { |
| 1252 | assert(thread_sp && "thread list should not contain NULL threads"); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1253 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1254 | const ResumeAction *const action = |
| 1255 | resume_actions.GetActionForThread(thread_sp->GetID(), true); |
| 1256 | if (action == nullptr) |
| 1257 | continue; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1258 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1259 | if (action->state == eStateStepping) { |
| 1260 | Error error = SetupSoftwareSingleStepping( |
| 1261 | static_cast<NativeThreadLinux &>(*thread_sp)); |
| 1262 | if (error.Fail()) |
| 1263 | return error; |
| 1264 | } |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1265 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | for (auto thread_sp : m_threads) { |
| 1269 | assert(thread_sp && "thread list should not contain NULL threads"); |
| 1270 | |
| 1271 | const ResumeAction *const action = |
| 1272 | resume_actions.GetActionForThread(thread_sp->GetID(), true); |
| 1273 | |
| 1274 | if (action == nullptr) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1275 | LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(), |
| 1276 | thread_sp->GetID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1277 | continue; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1280 | LLDB_LOG(log, "processing resume action state {0} for pid {1} tid {2}", |
Pavel Labath | 8198db3 | 2017-01-24 11:48:25 +0000 | [diff] [blame] | 1281 | action->state, GetID(), thread_sp->GetID()); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1282 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1283 | switch (action->state) { |
| 1284 | case eStateRunning: |
| 1285 | case eStateStepping: { |
| 1286 | // Run the thread, possibly feeding it the signal. |
| 1287 | const int signo = action->signal; |
| 1288 | ResumeThread(static_cast<NativeThreadLinux &>(*thread_sp), action->state, |
| 1289 | signo); |
| 1290 | break; |
| 1291 | } |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1292 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1293 | case eStateSuspended: |
| 1294 | case eStateStopped: |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1295 | llvm_unreachable("Unexpected state"); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1296 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1297 | default: |
| 1298 | return Error("NativeProcessLinux::%s (): unexpected state %s specified " |
| 1299 | "for pid %" PRIu64 ", tid %" PRIu64, |
| 1300 | __FUNCTION__, StateAsCString(action->state), GetID(), |
| 1301 | thread_sp->GetID()); |
| 1302 | } |
| 1303 | } |
| 1304 | |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 1305 | return Error(); |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1308 | Error NativeProcessLinux::Halt() { |
| 1309 | Error error; |
| 1310 | |
| 1311 | if (kill(GetID(), SIGSTOP) != 0) |
| 1312 | error.SetErrorToErrno(); |
| 1313 | |
| 1314 | return error; |
Tamas Berghammer | e770868 | 2015-04-22 10:00:23 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1317 | Error NativeProcessLinux::Detach() { |
| 1318 | Error error; |
| 1319 | |
| 1320 | // Stop monitoring the inferior. |
| 1321 | m_sigchld_handle.reset(); |
| 1322 | |
| 1323 | // Tell ptrace to detach from the process. |
| 1324 | if (GetID() == LLDB_INVALID_PROCESS_ID) |
| 1325 | return error; |
| 1326 | |
| 1327 | for (auto thread_sp : m_threads) { |
| 1328 | Error e = Detach(thread_sp->GetID()); |
| 1329 | if (e.Fail()) |
| 1330 | error = |
| 1331 | e; // Save the error, but still attempt to detach from other threads. |
| 1332 | } |
| 1333 | |
| 1334 | return error; |
| 1335 | } |
| 1336 | |
| 1337 | Error NativeProcessLinux::Signal(int signo) { |
| 1338 | Error error; |
| 1339 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1340 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 1341 | LLDB_LOG(log, "sending signal {0} ({1}) to pid {1}", signo, |
| 1342 | Host::GetSignalAsCString(signo), GetID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1343 | |
| 1344 | if (kill(GetID(), signo)) |
| 1345 | error.SetErrorToErrno(); |
| 1346 | |
| 1347 | return error; |
| 1348 | } |
| 1349 | |
| 1350 | Error NativeProcessLinux::Interrupt() { |
| 1351 | // Pick a running thread (or if none, a not-dead stopped thread) as |
| 1352 | // the chosen thread that will be the stop-reason thread. |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1353 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1354 | |
| 1355 | NativeThreadProtocolSP running_thread_sp; |
| 1356 | NativeThreadProtocolSP stopped_thread_sp; |
| 1357 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1358 | LLDB_LOG(log, "selecting running thread for interrupt target"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1359 | for (auto thread_sp : m_threads) { |
| 1360 | // The thread shouldn't be null but lets just cover that here. |
| 1361 | if (!thread_sp) |
| 1362 | continue; |
| 1363 | |
| 1364 | // If we have a running or stepping thread, we'll call that the |
| 1365 | // target of the interrupt. |
| 1366 | const auto thread_state = thread_sp->GetState(); |
| 1367 | if (thread_state == eStateRunning || thread_state == eStateStepping) { |
| 1368 | running_thread_sp = thread_sp; |
| 1369 | break; |
| 1370 | } else if (!stopped_thread_sp && StateIsStoppedState(thread_state, true)) { |
| 1371 | // Remember the first non-dead stopped thread. We'll use that as a backup |
| 1372 | // if there are no running threads. |
| 1373 | stopped_thread_sp = thread_sp; |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | if (!running_thread_sp && !stopped_thread_sp) { |
| 1378 | Error error("found no running/stepping or live stopped threads as target " |
| 1379 | "for interrupt"); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1380 | LLDB_LOG(log, "skipping due to error: {0}", error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1381 | |
| 1382 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | NativeThreadProtocolSP deferred_signal_thread_sp = |
| 1386 | running_thread_sp ? running_thread_sp : stopped_thread_sp; |
| 1387 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1388 | LLDB_LOG(log, "pid {0} {1} tid {2} chosen for interrupt target", GetID(), |
| 1389 | running_thread_sp ? "running" : "stopped", |
| 1390 | deferred_signal_thread_sp->GetID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1391 | |
| 1392 | StopRunningThreads(deferred_signal_thread_sp->GetID()); |
| 1393 | |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 1394 | return Error(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1397 | Error NativeProcessLinux::Kill() { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1398 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 1399 | LLDB_LOG(log, "pid {0}", GetID()); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1400 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1401 | Error error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1402 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1403 | switch (m_state) { |
| 1404 | case StateType::eStateInvalid: |
| 1405 | case StateType::eStateExited: |
| 1406 | case StateType::eStateCrashed: |
| 1407 | case StateType::eStateDetached: |
| 1408 | case StateType::eStateUnloaded: |
| 1409 | // Nothing to do - the process is already dead. |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1410 | LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(), |
Pavel Labath | 8198db3 | 2017-01-24 11:48:25 +0000 | [diff] [blame] | 1411 | m_state); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1412 | return error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1413 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1414 | case StateType::eStateConnected: |
| 1415 | case StateType::eStateAttaching: |
| 1416 | case StateType::eStateLaunching: |
| 1417 | case StateType::eStateStopped: |
| 1418 | case StateType::eStateRunning: |
| 1419 | case StateType::eStateStepping: |
| 1420 | case StateType::eStateSuspended: |
| 1421 | // We can try to kill a process in these states. |
| 1422 | break; |
| 1423 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1424 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1425 | if (kill(GetID(), SIGKILL) != 0) { |
| 1426 | error.SetErrorToErrno(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1427 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1428 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1429 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1430 | return error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | static Error |
Pavel Labath | 1593086 | 2017-03-21 13:49:45 +0000 | [diff] [blame^] | 1434 | ParseMemoryRegionInfoFromProcMapsLine(llvm::StringRef &maps_line, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1435 | MemoryRegionInfo &memory_region_info) { |
| 1436 | memory_region_info.Clear(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1437 | |
Pavel Labath | 1593086 | 2017-03-21 13:49:45 +0000 | [diff] [blame^] | 1438 | StringExtractor line_extractor(maps_line); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1439 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1440 | // Format: {address_start_hex}-{address_end_hex} perms offset dev inode |
| 1441 | // pathname |
| 1442 | // perms: rwxp (letter is present if set, '-' if not, final character is |
| 1443 | // p=private, s=shared). |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1444 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1445 | // Parse out the starting address |
| 1446 | lldb::addr_t start_address = line_extractor.GetHexMaxU64(false, 0); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1447 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1448 | // Parse out hyphen separating start and end address from range. |
| 1449 | if (!line_extractor.GetBytesLeft() || (line_extractor.GetChar() != '-')) |
| 1450 | return Error( |
| 1451 | "malformed /proc/{pid}/maps entry, missing dash between address range"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1452 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1453 | // Parse out the ending address |
| 1454 | lldb::addr_t end_address = line_extractor.GetHexMaxU64(false, start_address); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1455 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1456 | // Parse out the space after the address. |
| 1457 | if (!line_extractor.GetBytesLeft() || (line_extractor.GetChar() != ' ')) |
| 1458 | return Error("malformed /proc/{pid}/maps entry, missing space after range"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1459 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1460 | // Save the range. |
| 1461 | memory_region_info.GetRange().SetRangeBase(start_address); |
| 1462 | memory_region_info.GetRange().SetRangeEnd(end_address); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1463 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1464 | // Any memory region in /proc/{pid}/maps is by definition mapped into the |
| 1465 | // process. |
| 1466 | memory_region_info.SetMapped(MemoryRegionInfo::OptionalBool::eYes); |
Howard Hellyer | ad00756 | 2016-07-07 08:21:28 +0000 | [diff] [blame] | 1467 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1468 | // Parse out each permission entry. |
| 1469 | if (line_extractor.GetBytesLeft() < 4) |
| 1470 | return Error("malformed /proc/{pid}/maps entry, missing some portion of " |
| 1471 | "permissions"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1472 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1473 | // Handle read permission. |
| 1474 | const char read_perm_char = line_extractor.GetChar(); |
| 1475 | if (read_perm_char == 'r') |
| 1476 | memory_region_info.SetReadable(MemoryRegionInfo::OptionalBool::eYes); |
| 1477 | else if (read_perm_char == '-') |
| 1478 | memory_region_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); |
| 1479 | else |
| 1480 | return Error("unexpected /proc/{pid}/maps read permission char"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1481 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1482 | // Handle write permission. |
| 1483 | const char write_perm_char = line_extractor.GetChar(); |
| 1484 | if (write_perm_char == 'w') |
| 1485 | memory_region_info.SetWritable(MemoryRegionInfo::OptionalBool::eYes); |
| 1486 | else if (write_perm_char == '-') |
| 1487 | memory_region_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); |
| 1488 | else |
| 1489 | return Error("unexpected /proc/{pid}/maps write permission char"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1490 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1491 | // Handle execute permission. |
| 1492 | const char exec_perm_char = line_extractor.GetChar(); |
| 1493 | if (exec_perm_char == 'x') |
| 1494 | memory_region_info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes); |
| 1495 | else if (exec_perm_char == '-') |
| 1496 | memory_region_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); |
| 1497 | else |
| 1498 | return Error("unexpected /proc/{pid}/maps exec permission char"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1499 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1500 | line_extractor.GetChar(); // Read the private bit |
| 1501 | line_extractor.SkipSpaces(); // Skip the separator |
| 1502 | line_extractor.GetHexMaxU64(false, 0); // Read the offset |
| 1503 | line_extractor.GetHexMaxU64(false, 0); // Read the major device number |
| 1504 | line_extractor.GetChar(); // Read the device id separator |
| 1505 | line_extractor.GetHexMaxU64(false, 0); // Read the major device number |
| 1506 | line_extractor.SkipSpaces(); // Skip the separator |
| 1507 | line_extractor.GetU64(0, 10); // Read the inode number |
Tamas Berghammer | d7d69f8 | 2016-07-22 12:55:35 +0000 | [diff] [blame] | 1508 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1509 | line_extractor.SkipSpaces(); |
| 1510 | const char *name = line_extractor.Peek(); |
| 1511 | if (name) |
| 1512 | memory_region_info.SetName(name); |
Tamas Berghammer | d7d69f8 | 2016-07-22 12:55:35 +0000 | [diff] [blame] | 1513 | |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 1514 | return Error(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1517 | Error NativeProcessLinux::GetMemoryRegionInfo(lldb::addr_t load_addr, |
| 1518 | MemoryRegionInfo &range_info) { |
| 1519 | // FIXME review that the final memory region returned extends to the end of |
| 1520 | // the virtual address space, |
| 1521 | // with no perms if it is not mapped. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1522 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1523 | // Use an approach that reads memory regions from /proc/{pid}/maps. |
| 1524 | // Assume proc maps entries are in ascending order. |
| 1525 | // FIXME assert if we find differently. |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1526 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1527 | if (m_supports_mem_region == LazyBool::eLazyBoolNo) { |
| 1528 | // We're done. |
Tamas Berghammer | a6f5795 | 2017-01-03 16:29:43 +0000 | [diff] [blame] | 1529 | return Error("unsupported"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1530 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1531 | |
Tamas Berghammer | a6f5795 | 2017-01-03 16:29:43 +0000 | [diff] [blame] | 1532 | Error error = PopulateMemoryRegionCache(); |
| 1533 | if (error.Fail()) { |
| 1534 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | lldb::addr_t prev_base_address = 0; |
| 1538 | |
| 1539 | // FIXME start by finding the last region that is <= target address using |
| 1540 | // binary search. Data is sorted. |
| 1541 | // There can be a ton of regions on pthreads apps with lots of threads. |
| 1542 | for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end(); |
| 1543 | ++it) { |
Tamas Berghammer | a6f5795 | 2017-01-03 16:29:43 +0000 | [diff] [blame] | 1544 | MemoryRegionInfo &proc_entry_info = it->first; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1545 | |
| 1546 | // Sanity check assumption that /proc/{pid}/maps entries are ascending. |
| 1547 | assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && |
| 1548 | "descending /proc/pid/maps entries detected, unexpected"); |
| 1549 | prev_base_address = proc_entry_info.GetRange().GetRangeBase(); |
Hafiz Abid Qadeer | b155431 | 2017-01-20 10:24:03 +0000 | [diff] [blame] | 1550 | UNUSED_IF_ASSERT_DISABLED(prev_base_address); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1551 | |
| 1552 | // If the target address comes before this entry, indicate distance to next |
| 1553 | // region. |
| 1554 | if (load_addr < proc_entry_info.GetRange().GetRangeBase()) { |
| 1555 | range_info.GetRange().SetRangeBase(load_addr); |
| 1556 | range_info.GetRange().SetByteSize( |
| 1557 | proc_entry_info.GetRange().GetRangeBase() - load_addr); |
| 1558 | range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); |
| 1559 | range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); |
| 1560 | range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); |
| 1561 | range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); |
| 1562 | |
| 1563 | return error; |
| 1564 | } else if (proc_entry_info.GetRange().Contains(load_addr)) { |
| 1565 | // The target address is within the memory region we're processing here. |
| 1566 | range_info = proc_entry_info; |
| 1567 | return error; |
| 1568 | } |
| 1569 | |
| 1570 | // The target memory address comes somewhere after the region we just |
| 1571 | // parsed. |
| 1572 | } |
| 1573 | |
| 1574 | // If we made it here, we didn't find an entry that contained the given |
| 1575 | // address. Return the |
| 1576 | // load_addr as start and the amount of bytes betwwen load address and the end |
| 1577 | // of the memory as |
| 1578 | // size. |
| 1579 | range_info.GetRange().SetRangeBase(load_addr); |
| 1580 | range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); |
| 1581 | range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); |
| 1582 | range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); |
| 1583 | range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); |
| 1584 | range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); |
| 1585 | return error; |
| 1586 | } |
| 1587 | |
Tamas Berghammer | a6f5795 | 2017-01-03 16:29:43 +0000 | [diff] [blame] | 1588 | Error NativeProcessLinux::PopulateMemoryRegionCache() { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1589 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
Tamas Berghammer | a6f5795 | 2017-01-03 16:29:43 +0000 | [diff] [blame] | 1590 | |
| 1591 | // If our cache is empty, pull the latest. There should always be at least |
| 1592 | // one memory region if memory region handling is supported. |
| 1593 | if (!m_mem_region_cache.empty()) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1594 | LLDB_LOG(log, "reusing {0} cached memory region entries", |
| 1595 | m_mem_region_cache.size()); |
Tamas Berghammer | a6f5795 | 2017-01-03 16:29:43 +0000 | [diff] [blame] | 1596 | return Error(); |
| 1597 | } |
| 1598 | |
Pavel Labath | 1593086 | 2017-03-21 13:49:45 +0000 | [diff] [blame^] | 1599 | auto BufferOrError = getProcFile(GetID(), "maps"); |
| 1600 | if (!BufferOrError) { |
Tamas Berghammer | a6f5795 | 2017-01-03 16:29:43 +0000 | [diff] [blame] | 1601 | m_supports_mem_region = LazyBool::eLazyBoolNo; |
Pavel Labath | 1593086 | 2017-03-21 13:49:45 +0000 | [diff] [blame^] | 1602 | return BufferOrError.getError(); |
| 1603 | } |
| 1604 | StringRef Rest = BufferOrError.get()->getBuffer(); |
| 1605 | while (! Rest.empty()) { |
| 1606 | StringRef Line; |
| 1607 | std::tie(Line, Rest) = Rest.split('\n'); |
| 1608 | MemoryRegionInfo info; |
| 1609 | const Error parse_error = ParseMemoryRegionInfoFromProcMapsLine(Line, info); |
| 1610 | if (parse_error.Fail()) { |
| 1611 | LLDB_LOG(log, "failed to parse proc maps line '{0}': {1}", Line, |
| 1612 | parse_error); |
| 1613 | m_supports_mem_region = LazyBool::eLazyBoolNo; |
| 1614 | return parse_error; |
| 1615 | } |
| 1616 | m_mem_region_cache.emplace_back( |
| 1617 | info, FileSpec(info.GetName().GetCString(), true)); |
| 1618 | } |
| 1619 | |
| 1620 | if (m_mem_region_cache.empty()) { |
Tamas Berghammer | a6f5795 | 2017-01-03 16:29:43 +0000 | [diff] [blame] | 1621 | // No entries after attempting to read them. This shouldn't happen if |
| 1622 | // /proc/{pid}/maps is supported. Assume we don't support map entries |
| 1623 | // via procfs. |
Pavel Labath | 1593086 | 2017-03-21 13:49:45 +0000 | [diff] [blame^] | 1624 | m_supports_mem_region = LazyBool::eLazyBoolNo; |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1625 | LLDB_LOG(log, |
| 1626 | "failed to find any procfs maps entries, assuming no support " |
| 1627 | "for memory region metadata retrieval"); |
Pavel Labath | 1593086 | 2017-03-21 13:49:45 +0000 | [diff] [blame^] | 1628 | return Error("not supported"); |
Tamas Berghammer | a6f5795 | 2017-01-03 16:29:43 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1631 | LLDB_LOG(log, "read {0} memory region entries from /proc/{1}/maps", |
| 1632 | m_mem_region_cache.size(), GetID()); |
Tamas Berghammer | a6f5795 | 2017-01-03 16:29:43 +0000 | [diff] [blame] | 1633 | |
| 1634 | // We support memory retrieval, remember that. |
| 1635 | m_supports_mem_region = LazyBool::eLazyBoolYes; |
| 1636 | return Error(); |
| 1637 | } |
| 1638 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1639 | void NativeProcessLinux::DoStopIDBumped(uint32_t newBumpId) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1640 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 1641 | LLDB_LOG(log, "newBumpId={0}", newBumpId); |
| 1642 | LLDB_LOG(log, "clearing {0} entries from memory region cache", |
| 1643 | m_mem_region_cache.size()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1644 | m_mem_region_cache.clear(); |
| 1645 | } |
| 1646 | |
| 1647 | Error NativeProcessLinux::AllocateMemory(size_t size, uint32_t permissions, |
| 1648 | lldb::addr_t &addr) { |
| 1649 | // FIXME implementing this requires the equivalent of |
| 1650 | // InferiorCallPOSIX::InferiorCallMmap, which depends on |
| 1651 | // functional ThreadPlans working with Native*Protocol. |
| 1652 | #if 1 |
| 1653 | return Error("not implemented yet"); |
| 1654 | #else |
| 1655 | addr = LLDB_INVALID_ADDRESS; |
| 1656 | |
| 1657 | unsigned prot = 0; |
| 1658 | if (permissions & lldb::ePermissionsReadable) |
| 1659 | prot |= eMmapProtRead; |
| 1660 | if (permissions & lldb::ePermissionsWritable) |
| 1661 | prot |= eMmapProtWrite; |
| 1662 | if (permissions & lldb::ePermissionsExecutable) |
| 1663 | prot |= eMmapProtExec; |
| 1664 | |
| 1665 | // TODO implement this directly in NativeProcessLinux |
| 1666 | // (and lift to NativeProcessPOSIX if/when that class is |
| 1667 | // refactored out). |
| 1668 | if (InferiorCallMmap(this, addr, 0, size, prot, |
| 1669 | eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { |
| 1670 | m_addr_to_mmap_size[addr] = size; |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 1671 | return Error(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1672 | } else { |
| 1673 | addr = LLDB_INVALID_ADDRESS; |
| 1674 | return Error("unable to allocate %" PRIu64 |
| 1675 | " bytes of memory with permissions %s", |
| 1676 | size, GetPermissionsAsCString(permissions)); |
| 1677 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1678 | #endif |
| 1679 | } |
| 1680 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1681 | Error NativeProcessLinux::DeallocateMemory(lldb::addr_t addr) { |
| 1682 | // FIXME see comments in AllocateMemory - required lower-level |
| 1683 | // bits not in place yet (ThreadPlans) |
| 1684 | return Error("not implemented"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1687 | lldb::addr_t NativeProcessLinux::GetSharedLibraryInfoAddress() { |
| 1688 | // punt on this for now |
| 1689 | return LLDB_INVALID_ADDRESS; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1692 | size_t NativeProcessLinux::UpdateThreads() { |
| 1693 | // The NativeProcessLinux monitoring threads are always up to date |
| 1694 | // with respect to thread state and they keep the thread list |
| 1695 | // populated properly. All this method needs to do is return the |
| 1696 | // thread count. |
| 1697 | return m_threads.size(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1700 | bool NativeProcessLinux::GetArchitecture(ArchSpec &arch) const { |
| 1701 | arch = m_arch; |
| 1702 | return true; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1705 | Error NativeProcessLinux::GetSoftwareBreakpointPCOffset( |
| 1706 | uint32_t &actual_opcode_size) { |
| 1707 | // FIXME put this behind a breakpoint protocol class that can be |
| 1708 | // set per architecture. Need ARM, MIPS support here. |
| 1709 | static const uint8_t g_i386_opcode[] = {0xCC}; |
| 1710 | static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1711 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1712 | switch (m_arch.GetMachine()) { |
| 1713 | case llvm::Triple::x86: |
| 1714 | case llvm::Triple::x86_64: |
| 1715 | actual_opcode_size = static_cast<uint32_t>(sizeof(g_i386_opcode)); |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 1716 | return Error(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1717 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1718 | case llvm::Triple::systemz: |
| 1719 | actual_opcode_size = static_cast<uint32_t>(sizeof(g_s390x_opcode)); |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 1720 | return Error(); |
Ulrich Weigand | bb00d0b | 2016-04-14 14:28:34 +0000 | [diff] [blame] | 1721 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1722 | case llvm::Triple::arm: |
| 1723 | case llvm::Triple::aarch64: |
| 1724 | case llvm::Triple::mips64: |
| 1725 | case llvm::Triple::mips64el: |
| 1726 | case llvm::Triple::mips: |
| 1727 | case llvm::Triple::mipsel: |
| 1728 | // On these architectures the PC don't get updated for breakpoint hits |
| 1729 | actual_opcode_size = 0; |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 1730 | return Error(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1731 | |
| 1732 | default: |
| 1733 | assert(false && "CPU type not supported!"); |
| 1734 | return Error("CPU type not supported"); |
| 1735 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1738 | Error NativeProcessLinux::SetBreakpoint(lldb::addr_t addr, uint32_t size, |
| 1739 | bool hardware) { |
| 1740 | if (hardware) |
Omair Javaid | d5ffbad | 2017-02-24 13:27:31 +0000 | [diff] [blame] | 1741 | return SetHardwareBreakpoint(addr, size); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1742 | else |
| 1743 | return SetSoftwareBreakpoint(addr, size); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Omair Javaid | d5ffbad | 2017-02-24 13:27:31 +0000 | [diff] [blame] | 1746 | Error NativeProcessLinux::RemoveBreakpoint(lldb::addr_t addr, bool hardware) { |
| 1747 | if (hardware) |
| 1748 | return RemoveHardwareBreakpoint(addr); |
| 1749 | else |
| 1750 | return NativeProcessProtocol::RemoveBreakpoint(addr); |
| 1751 | } |
| 1752 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1753 | Error NativeProcessLinux::GetSoftwareBreakpointTrapOpcode( |
| 1754 | size_t trap_opcode_size_hint, size_t &actual_opcode_size, |
| 1755 | const uint8_t *&trap_opcode_bytes) { |
| 1756 | // FIXME put this behind a breakpoint protocol class that can be set per |
| 1757 | // architecture. Need MIPS support here. |
| 1758 | static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4}; |
| 1759 | // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the |
| 1760 | // linux kernel does otherwise. |
| 1761 | static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7}; |
| 1762 | static const uint8_t g_i386_opcode[] = {0xCC}; |
| 1763 | static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d}; |
| 1764 | static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00}; |
| 1765 | static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; |
| 1766 | static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde}; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1767 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1768 | switch (m_arch.GetMachine()) { |
| 1769 | case llvm::Triple::aarch64: |
| 1770 | trap_opcode_bytes = g_aarch64_opcode; |
| 1771 | actual_opcode_size = sizeof(g_aarch64_opcode); |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 1772 | return Error(); |
Todd Fiala | 2afc596 | 2014-08-21 16:42:31 +0000 | [diff] [blame] | 1773 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1774 | case llvm::Triple::arm: |
| 1775 | switch (trap_opcode_size_hint) { |
| 1776 | case 2: |
| 1777 | trap_opcode_bytes = g_thumb_breakpoint_opcode; |
| 1778 | actual_opcode_size = sizeof(g_thumb_breakpoint_opcode); |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 1779 | return Error(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1780 | case 4: |
| 1781 | trap_opcode_bytes = g_arm_breakpoint_opcode; |
| 1782 | actual_opcode_size = sizeof(g_arm_breakpoint_opcode); |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 1783 | return Error(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1784 | default: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1785 | assert(false && "Unrecognised trap opcode size hint!"); |
| 1786 | return Error("Unrecognised trap opcode size hint!"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1787 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1788 | |
| 1789 | case llvm::Triple::x86: |
| 1790 | case llvm::Triple::x86_64: |
| 1791 | trap_opcode_bytes = g_i386_opcode; |
| 1792 | actual_opcode_size = sizeof(g_i386_opcode); |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 1793 | return Error(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1794 | |
| 1795 | case llvm::Triple::mips: |
| 1796 | case llvm::Triple::mips64: |
| 1797 | trap_opcode_bytes = g_mips64_opcode; |
| 1798 | actual_opcode_size = sizeof(g_mips64_opcode); |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 1799 | return Error(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1800 | |
| 1801 | case llvm::Triple::mipsel: |
| 1802 | case llvm::Triple::mips64el: |
| 1803 | trap_opcode_bytes = g_mips64el_opcode; |
| 1804 | actual_opcode_size = sizeof(g_mips64el_opcode); |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 1805 | return Error(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1806 | |
| 1807 | case llvm::Triple::systemz: |
| 1808 | trap_opcode_bytes = g_s390x_opcode; |
| 1809 | actual_opcode_size = sizeof(g_s390x_opcode); |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 1810 | return Error(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1811 | |
| 1812 | default: |
| 1813 | assert(false && "CPU type not supported!"); |
| 1814 | return Error("CPU type not supported"); |
| 1815 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | #if 0 |
| 1819 | ProcessMessage::CrashReason |
| 1820 | NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info) |
| 1821 | { |
| 1822 | ProcessMessage::CrashReason reason; |
| 1823 | assert(info->si_signo == SIGSEGV); |
| 1824 | |
| 1825 | reason = ProcessMessage::eInvalidCrashReason; |
| 1826 | |
| 1827 | switch (info->si_code) |
| 1828 | { |
| 1829 | default: |
| 1830 | assert(false && "unexpected si_code for SIGSEGV"); |
| 1831 | break; |
| 1832 | case SI_KERNEL: |
| 1833 | // Linux will occasionally send spurious SI_KERNEL codes. |
| 1834 | // (this is poorly documented in sigaction) |
| 1835 | // One way to get this is via unaligned SIMD loads. |
| 1836 | reason = ProcessMessage::eInvalidAddress; // for lack of anything better |
| 1837 | break; |
| 1838 | case SEGV_MAPERR: |
| 1839 | reason = ProcessMessage::eInvalidAddress; |
| 1840 | break; |
| 1841 | case SEGV_ACCERR: |
| 1842 | reason = ProcessMessage::ePrivilegedAddress; |
| 1843 | break; |
| 1844 | } |
| 1845 | |
| 1846 | return reason; |
| 1847 | } |
| 1848 | #endif |
| 1849 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 1850 | #if 0 |
| 1851 | ProcessMessage::CrashReason |
| 1852 | NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info) |
| 1853 | { |
| 1854 | ProcessMessage::CrashReason reason; |
| 1855 | assert(info->si_signo == SIGILL); |
| 1856 | |
| 1857 | reason = ProcessMessage::eInvalidCrashReason; |
| 1858 | |
| 1859 | switch (info->si_code) |
| 1860 | { |
| 1861 | default: |
| 1862 | assert(false && "unexpected si_code for SIGILL"); |
| 1863 | break; |
| 1864 | case ILL_ILLOPC: |
| 1865 | reason = ProcessMessage::eIllegalOpcode; |
| 1866 | break; |
| 1867 | case ILL_ILLOPN: |
| 1868 | reason = ProcessMessage::eIllegalOperand; |
| 1869 | break; |
| 1870 | case ILL_ILLADR: |
| 1871 | reason = ProcessMessage::eIllegalAddressingMode; |
| 1872 | break; |
| 1873 | case ILL_ILLTRP: |
| 1874 | reason = ProcessMessage::eIllegalTrap; |
| 1875 | break; |
| 1876 | case ILL_PRVOPC: |
| 1877 | reason = ProcessMessage::ePrivilegedOpcode; |
| 1878 | break; |
| 1879 | case ILL_PRVREG: |
| 1880 | reason = ProcessMessage::ePrivilegedRegister; |
| 1881 | break; |
| 1882 | case ILL_COPROC: |
| 1883 | reason = ProcessMessage::eCoprocessorError; |
| 1884 | break; |
| 1885 | case ILL_BADSTK: |
| 1886 | reason = ProcessMessage::eInternalStackError; |
| 1887 | break; |
| 1888 | } |
| 1889 | |
| 1890 | return reason; |
| 1891 | } |
| 1892 | #endif |
| 1893 | |
| 1894 | #if 0 |
| 1895 | ProcessMessage::CrashReason |
| 1896 | NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info) |
| 1897 | { |
| 1898 | ProcessMessage::CrashReason reason; |
| 1899 | assert(info->si_signo == SIGFPE); |
| 1900 | |
| 1901 | reason = ProcessMessage::eInvalidCrashReason; |
| 1902 | |
| 1903 | switch (info->si_code) |
| 1904 | { |
| 1905 | default: |
| 1906 | assert(false && "unexpected si_code for SIGFPE"); |
| 1907 | break; |
| 1908 | case FPE_INTDIV: |
| 1909 | reason = ProcessMessage::eIntegerDivideByZero; |
| 1910 | break; |
| 1911 | case FPE_INTOVF: |
| 1912 | reason = ProcessMessage::eIntegerOverflow; |
| 1913 | break; |
| 1914 | case FPE_FLTDIV: |
| 1915 | reason = ProcessMessage::eFloatDivideByZero; |
| 1916 | break; |
| 1917 | case FPE_FLTOVF: |
| 1918 | reason = ProcessMessage::eFloatOverflow; |
| 1919 | break; |
| 1920 | case FPE_FLTUND: |
| 1921 | reason = ProcessMessage::eFloatUnderflow; |
| 1922 | break; |
| 1923 | case FPE_FLTRES: |
| 1924 | reason = ProcessMessage::eFloatInexactResult; |
| 1925 | break; |
| 1926 | case FPE_FLTINV: |
| 1927 | reason = ProcessMessage::eFloatInvalidOperation; |
| 1928 | break; |
| 1929 | case FPE_FLTSUB: |
| 1930 | reason = ProcessMessage::eFloatSubscriptRange; |
| 1931 | break; |
| 1932 | } |
| 1933 | |
| 1934 | return reason; |
| 1935 | } |
| 1936 | #endif |
| 1937 | |
| 1938 | #if 0 |
| 1939 | ProcessMessage::CrashReason |
| 1940 | NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info) |
| 1941 | { |
| 1942 | ProcessMessage::CrashReason reason; |
| 1943 | assert(info->si_signo == SIGBUS); |
| 1944 | |
| 1945 | reason = ProcessMessage::eInvalidCrashReason; |
| 1946 | |
| 1947 | switch (info->si_code) |
| 1948 | { |
| 1949 | default: |
| 1950 | assert(false && "unexpected si_code for SIGBUS"); |
| 1951 | break; |
| 1952 | case BUS_ADRALN: |
| 1953 | reason = ProcessMessage::eIllegalAlignment; |
| 1954 | break; |
| 1955 | case BUS_ADRERR: |
| 1956 | reason = ProcessMessage::eIllegalAddress; |
| 1957 | break; |
| 1958 | case BUS_OBJERR: |
| 1959 | reason = ProcessMessage::eHardwareError; |
| 1960 | break; |
| 1961 | } |
| 1962 | |
| 1963 | return reason; |
| 1964 | } |
| 1965 | #endif |
| 1966 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1967 | Error NativeProcessLinux::ReadMemory(lldb::addr_t addr, void *buf, size_t size, |
| 1968 | size_t &bytes_read) { |
| 1969 | if (ProcessVmReadvSupported()) { |
| 1970 | // The process_vm_readv path is about 50 times faster than ptrace api. We |
| 1971 | // want to use |
| 1972 | // this syscall if it is supported. |
Pavel Labath | df7c699 | 2015-06-17 18:38:49 +0000 | [diff] [blame] | 1973 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1974 | const ::pid_t pid = GetID(); |
Pavel Labath | df7c699 | 2015-06-17 18:38:49 +0000 | [diff] [blame] | 1975 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1976 | struct iovec local_iov, remote_iov; |
| 1977 | local_iov.iov_base = buf; |
| 1978 | local_iov.iov_len = size; |
| 1979 | remote_iov.iov_base = reinterpret_cast<void *>(addr); |
| 1980 | remote_iov.iov_len = size; |
Pavel Labath | df7c699 | 2015-06-17 18:38:49 +0000 | [diff] [blame] | 1981 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1982 | bytes_read = process_vm_readv(pid, &local_iov, 1, &remote_iov, 1, 0); |
| 1983 | const bool success = bytes_read == size; |
Pavel Labath | df7c699 | 2015-06-17 18:38:49 +0000 | [diff] [blame] | 1984 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1985 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
| 1986 | LLDB_LOG(log, |
| 1987 | "using process_vm_readv to read {0} bytes from inferior " |
| 1988 | "address {1:x}: {2}", |
| 1989 | size, addr, success ? "Success" : strerror(errno)); |
Pavel Labath | 19cbe96 | 2015-07-21 13:20:32 +0000 | [diff] [blame] | 1990 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1991 | if (success) |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 1992 | return Error(); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 1993 | // else the call failed for some reason, let's retry the read using ptrace |
| 1994 | // api. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1995 | } |
Pavel Labath | 19cbe96 | 2015-07-21 13:20:32 +0000 | [diff] [blame] | 1996 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1997 | unsigned char *dst = static_cast<unsigned char *>(buf); |
| 1998 | size_t remainder; |
| 1999 | long data; |
Pavel Labath | 19cbe96 | 2015-07-21 13:20:32 +0000 | [diff] [blame] | 2000 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2001 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); |
| 2002 | LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); |
Pavel Labath | 19cbe96 | 2015-07-21 13:20:32 +0000 | [diff] [blame] | 2003 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2004 | for (bytes_read = 0; bytes_read < size; bytes_read += remainder) { |
| 2005 | Error error = NativeProcessLinux::PtraceWrapper( |
| 2006 | PTRACE_PEEKDATA, GetID(), (void *)addr, nullptr, 0, &data); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2007 | if (error.Fail()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2008 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2009 | |
| 2010 | remainder = size - bytes_read; |
| 2011 | remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; |
| 2012 | |
| 2013 | // Copy the data into our buffer |
| 2014 | memcpy(dst, &data, remainder); |
| 2015 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2016 | LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2017 | addr += k_ptrace_word_size; |
| 2018 | dst += k_ptrace_word_size; |
| 2019 | } |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 2020 | return Error(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2023 | Error NativeProcessLinux::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, |
| 2024 | size_t size, |
| 2025 | size_t &bytes_read) { |
| 2026 | Error error = ReadMemory(addr, buf, size, bytes_read); |
| 2027 | if (error.Fail()) |
Pavel Labath | 19cbe96 | 2015-07-21 13:20:32 +0000 | [diff] [blame] | 2028 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2029 | return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2032 | Error NativeProcessLinux::WriteMemory(lldb::addr_t addr, const void *buf, |
| 2033 | size_t size, size_t &bytes_written) { |
| 2034 | const unsigned char *src = static_cast<const unsigned char *>(buf); |
| 2035 | size_t remainder; |
| 2036 | Error error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2037 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2038 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); |
| 2039 | LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2040 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2041 | for (bytes_written = 0; bytes_written < size; bytes_written += remainder) { |
| 2042 | remainder = size - bytes_written; |
| 2043 | remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; |
Chaoren Lin | 97ccc29 | 2015-02-03 01:51:12 +0000 | [diff] [blame] | 2044 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2045 | if (remainder == k_ptrace_word_size) { |
| 2046 | unsigned long data = 0; |
| 2047 | memcpy(&data, src, k_ptrace_word_size); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2048 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2049 | LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2050 | error = NativeProcessLinux::PtraceWrapper(PTRACE_POKEDATA, GetID(), |
| 2051 | (void *)addr, (void *)data); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2052 | if (error.Fail()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2053 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2054 | } else { |
| 2055 | unsigned char buff[8]; |
| 2056 | size_t bytes_read; |
| 2057 | error = ReadMemory(addr, buff, k_ptrace_word_size, bytes_read); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2058 | if (error.Fail()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2059 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2060 | |
| 2061 | memcpy(buff, src, remainder); |
| 2062 | |
| 2063 | size_t bytes_written_rec; |
| 2064 | error = WriteMemory(addr, buff, k_ptrace_word_size, bytes_written_rec); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2065 | if (error.Fail()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2066 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2067 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2068 | LLDB_LOG(log, "[{0:x}]:{1:x} ({2:x})", addr, *(const unsigned long *)src, |
| 2069 | *(unsigned long *)buff); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2072 | addr += k_ptrace_word_size; |
| 2073 | src += k_ptrace_word_size; |
| 2074 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2075 | return error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2078 | Error NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) { |
| 2079 | return PtraceWrapper(PTRACE_GETSIGINFO, tid, nullptr, siginfo); |
| 2080 | } |
Pavel Labath | 1dbc6c9 | 2015-05-12 08:35:33 +0000 | [diff] [blame] | 2081 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2082 | Error NativeProcessLinux::GetEventMessage(lldb::tid_t tid, |
| 2083 | unsigned long *message) { |
| 2084 | return PtraceWrapper(PTRACE_GETEVENTMSG, tid, nullptr, message); |
| 2085 | } |
Pavel Labath | 1dbc6c9 | 2015-05-12 08:35:33 +0000 | [diff] [blame] | 2086 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2087 | Error NativeProcessLinux::Detach(lldb::tid_t tid) { |
| 2088 | if (tid == LLDB_INVALID_THREAD_ID) |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 2089 | return Error(); |
Pavel Labath | 1dbc6c9 | 2015-05-12 08:35:33 +0000 | [diff] [blame] | 2090 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2091 | return PtraceWrapper(PTRACE_DETACH, tid); |
| 2092 | } |
| 2093 | |
| 2094 | bool NativeProcessLinux::HasThreadNoLock(lldb::tid_t thread_id) { |
| 2095 | for (auto thread_sp : m_threads) { |
| 2096 | assert(thread_sp && "thread list should not contain NULL threads"); |
| 2097 | if (thread_sp->GetID() == thread_id) { |
| 2098 | // We have this thread. |
| 2099 | return true; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2100 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2101 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2102 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2103 | // We don't have this thread. |
| 2104 | return false; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2107 | bool NativeProcessLinux::StopTrackingThread(lldb::tid_t thread_id) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2108 | Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); |
| 2109 | LLDB_LOG(log, "tid: {0})", thread_id); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2110 | |
| 2111 | bool found = false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2112 | for (auto it = m_threads.begin(); it != m_threads.end(); ++it) { |
| 2113 | if (*it && ((*it)->GetID() == thread_id)) { |
| 2114 | m_threads.erase(it); |
| 2115 | found = true; |
| 2116 | break; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2117 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2118 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2120 | SignalIfAllThreadsStopped(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2121 | return found; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2122 | } |
| 2123 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2124 | NativeThreadLinuxSP NativeProcessLinux::AddThread(lldb::tid_t thread_id) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2125 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); |
| 2126 | LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2127 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2128 | assert(!HasThreadNoLock(thread_id) && |
| 2129 | "attempted to add a thread by id that already exists"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2130 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2131 | // If this is the first thread, save it as the current thread |
| 2132 | if (m_threads.empty()) |
| 2133 | SetCurrentThreadID(thread_id); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2134 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2135 | auto thread_sp = std::make_shared<NativeThreadLinux>(this, thread_id); |
| 2136 | m_threads.push_back(thread_sp); |
| 2137 | return thread_sp; |
| 2138 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2139 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2140 | Error NativeProcessLinux::FixupBreakpointPCAsNeeded(NativeThreadLinux &thread) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2141 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS)); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2142 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2143 | Error error; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2144 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2145 | // Find out the size of a breakpoint (might depend on where we are in the |
| 2146 | // code). |
| 2147 | NativeRegisterContextSP context_sp = thread.GetRegisterContext(); |
| 2148 | if (!context_sp) { |
| 2149 | error.SetErrorString("cannot get a NativeRegisterContext for the thread"); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2150 | LLDB_LOG(log, "failed: {0}", error); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 2151 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2152 | } |
Chaoren Lin | fa03ad2 | 2015-02-03 01:50:42 +0000 | [diff] [blame] | 2153 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2154 | uint32_t breakpoint_size = 0; |
| 2155 | error = GetSoftwareBreakpointPCOffset(breakpoint_size); |
| 2156 | if (error.Fail()) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2157 | LLDB_LOG(log, "GetBreakpointSize() failed: {0}", error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2158 | return error; |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2159 | } else |
| 2160 | LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 2161 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2162 | // First try probing for a breakpoint at a software breakpoint location: PC - |
| 2163 | // breakpoint size. |
| 2164 | const lldb::addr_t initial_pc_addr = |
| 2165 | context_sp->GetPCfromBreakpointLocation(); |
| 2166 | lldb::addr_t breakpoint_addr = initial_pc_addr; |
| 2167 | if (breakpoint_size > 0) { |
| 2168 | // Do not allow breakpoint probe to wrap around. |
| 2169 | if (breakpoint_addr >= breakpoint_size) |
| 2170 | breakpoint_addr -= breakpoint_size; |
| 2171 | } |
| 2172 | |
| 2173 | // Check if we stopped because of a breakpoint. |
| 2174 | NativeBreakpointSP breakpoint_sp; |
| 2175 | error = m_breakpoint_list.GetBreakpoint(breakpoint_addr, breakpoint_sp); |
| 2176 | if (!error.Success() || !breakpoint_sp) { |
| 2177 | // We didn't find one at a software probe location. Nothing to do. |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2178 | LLDB_LOG(log, |
| 2179 | "pid {0} no lldb breakpoint found at current pc with " |
| 2180 | "adjustment: {1}", |
| 2181 | GetID(), breakpoint_addr); |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 2182 | return Error(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | // If the breakpoint is not a software breakpoint, nothing to do. |
| 2186 | if (!breakpoint_sp->IsSoftwareBreakpoint()) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2187 | LLDB_LOG( |
| 2188 | log, |
| 2189 | "pid {0} breakpoint found at {1:x}, not software, nothing to adjust", |
| 2190 | GetID(), breakpoint_addr); |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 2191 | return Error(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | // |
| 2195 | // We have a software breakpoint and need to adjust the PC. |
| 2196 | // |
| 2197 | |
| 2198 | // Sanity check. |
| 2199 | if (breakpoint_size == 0) { |
| 2200 | // Nothing to do! How did we get here? |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2201 | LLDB_LOG(log, |
| 2202 | "pid {0} breakpoint found at {1:x}, it is software, but the " |
| 2203 | "size is zero, nothing to do (unexpected)", |
| 2204 | GetID(), breakpoint_addr); |
Mehdi Amini | 665be50 | 2016-11-11 05:07:57 +0000 | [diff] [blame] | 2205 | return Error(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
| 2208 | // Change the program counter. |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2209 | LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(), |
| 2210 | thread.GetID(), initial_pc_addr, breakpoint_addr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2211 | |
| 2212 | error = context_sp->SetPC(breakpoint_addr); |
| 2213 | if (error.Fail()) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2214 | LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(), |
| 2215 | thread.GetID(), error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2216 | return error; |
| 2217 | } |
| 2218 | |
| 2219 | return error; |
| 2220 | } |
| 2221 | |
| 2222 | Error NativeProcessLinux::GetLoadedModuleFileSpec(const char *module_path, |
| 2223 | FileSpec &file_spec) { |
Tamas Berghammer | a6f5795 | 2017-01-03 16:29:43 +0000 | [diff] [blame] | 2224 | Error error = PopulateMemoryRegionCache(); |
| 2225 | if (error.Fail()) |
| 2226 | return error; |
| 2227 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2228 | FileSpec module_file_spec(module_path, true); |
| 2229 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2230 | file_spec.Clear(); |
Tamas Berghammer | a6f5795 | 2017-01-03 16:29:43 +0000 | [diff] [blame] | 2231 | for (const auto &it : m_mem_region_cache) { |
| 2232 | if (it.second.GetFilename() == module_file_spec.GetFilename()) { |
| 2233 | file_spec = it.second; |
| 2234 | return Error(); |
| 2235 | } |
| 2236 | } |
| 2237 | return Error("Module file (%s) not found in /proc/%" PRIu64 "/maps file!", |
| 2238 | module_file_spec.GetFilename().AsCString(), GetID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
| 2241 | Error NativeProcessLinux::GetFileLoadAddress(const llvm::StringRef &file_name, |
| 2242 | lldb::addr_t &load_addr) { |
| 2243 | load_addr = LLDB_INVALID_ADDRESS; |
Tamas Berghammer | a6f5795 | 2017-01-03 16:29:43 +0000 | [diff] [blame] | 2244 | Error error = PopulateMemoryRegionCache(); |
| 2245 | if (error.Fail()) |
| 2246 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2247 | |
Tamas Berghammer | a6f5795 | 2017-01-03 16:29:43 +0000 | [diff] [blame] | 2248 | FileSpec file(file_name, false); |
| 2249 | for (const auto &it : m_mem_region_cache) { |
| 2250 | if (it.second == file) { |
| 2251 | load_addr = it.first.GetRange().GetRangeBase(); |
| 2252 | return Error(); |
| 2253 | } |
| 2254 | } |
| 2255 | return Error("No load address found for specified file."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
| 2258 | NativeThreadLinuxSP NativeProcessLinux::GetThreadByID(lldb::tid_t tid) { |
| 2259 | return std::static_pointer_cast<NativeThreadLinux>( |
| 2260 | NativeProcessProtocol::GetThreadByID(tid)); |
| 2261 | } |
| 2262 | |
| 2263 | Error NativeProcessLinux::ResumeThread(NativeThreadLinux &thread, |
| 2264 | lldb::StateType state, int signo) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2265 | Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); |
| 2266 | LLDB_LOG(log, "tid: {0}", thread.GetID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2267 | |
| 2268 | // Before we do the resume below, first check if we have a pending |
| 2269 | // stop notification that is currently waiting for |
| 2270 | // all threads to stop. This is potentially a buggy situation since |
| 2271 | // we're ostensibly waiting for threads to stop before we send out the |
| 2272 | // pending notification, and here we are resuming one before we send |
| 2273 | // out the pending stop notification. |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2274 | if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) { |
| 2275 | LLDB_LOG(log, |
| 2276 | "about to resume tid {0} per explicit request but we have a " |
| 2277 | "pending stop notification (tid {1}) that is actively " |
| 2278 | "waiting for this thread to stop. Valid sequence of events?", |
| 2279 | thread.GetID(), m_pending_notification_tid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | // Request a resume. We expect this to be synchronous and the system |
| 2283 | // to reflect it is running after this completes. |
| 2284 | switch (state) { |
| 2285 | case eStateRunning: { |
| 2286 | const auto resume_result = thread.Resume(signo); |
| 2287 | if (resume_result.Success()) |
| 2288 | SetState(eStateRunning, true); |
| 2289 | return resume_result; |
| 2290 | } |
| 2291 | case eStateStepping: { |
| 2292 | const auto step_result = thread.SingleStep(signo); |
| 2293 | if (step_result.Success()) |
| 2294 | SetState(eStateRunning, true); |
| 2295 | return step_result; |
| 2296 | } |
| 2297 | default: |
Pavel Labath | 8198db3 | 2017-01-24 11:48:25 +0000 | [diff] [blame] | 2298 | LLDB_LOG(log, "Unhandled state {0}.", state); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2299 | llvm_unreachable("Unhandled state for resume"); |
| 2300 | } |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | //===----------------------------------------------------------------------===// |
| 2304 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2305 | void NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2306 | Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); |
| 2307 | LLDB_LOG(log, "about to process event: (triggering_tid: {0})", |
| 2308 | triggering_tid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2309 | |
| 2310 | m_pending_notification_tid = triggering_tid; |
| 2311 | |
| 2312 | // Request a stop for all the thread stops that need to be stopped |
| 2313 | // and are not already known to be stopped. |
| 2314 | for (const auto &thread_sp : m_threads) { |
| 2315 | if (StateIsRunningState(thread_sp->GetState())) |
| 2316 | static_pointer_cast<NativeThreadLinux>(thread_sp)->RequestStop(); |
| 2317 | } |
| 2318 | |
| 2319 | SignalIfAllThreadsStopped(); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2320 | LLDB_LOG(log, "event processing done"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2321 | } |
| 2322 | |
| 2323 | void NativeProcessLinux::SignalIfAllThreadsStopped() { |
| 2324 | if (m_pending_notification_tid == LLDB_INVALID_THREAD_ID) |
| 2325 | return; // No pending notification. Nothing to do. |
| 2326 | |
| 2327 | for (const auto &thread_sp : m_threads) { |
| 2328 | if (StateIsRunningState(thread_sp->GetState())) |
| 2329 | return; // Some threads are still running. Don't signal yet. |
| 2330 | } |
| 2331 | |
| 2332 | // We have a pending notification and all threads have stopped. |
| 2333 | Log *log( |
| 2334 | GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); |
| 2335 | |
| 2336 | // Clear any temporary breakpoints we used to implement software single |
| 2337 | // stepping. |
| 2338 | for (const auto &thread_info : m_threads_stepping_with_breakpoint) { |
| 2339 | Error error = RemoveBreakpoint(thread_info.second); |
| 2340 | if (error.Fail()) |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2341 | LLDB_LOG(log, "pid = {0} remove stepping breakpoint: {1}", |
| 2342 | thread_info.first, error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2343 | } |
| 2344 | m_threads_stepping_with_breakpoint.clear(); |
| 2345 | |
| 2346 | // Notify the delegate about the stop |
| 2347 | SetCurrentThreadID(m_pending_notification_tid); |
| 2348 | SetState(StateType::eStateStopped, true); |
| 2349 | m_pending_notification_tid = LLDB_INVALID_THREAD_ID; |
| 2350 | } |
| 2351 | |
| 2352 | void NativeProcessLinux::ThreadWasCreated(NativeThreadLinux &thread) { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2353 | Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); |
| 2354 | LLDB_LOG(log, "tid: {0}", thread.GetID()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2355 | |
| 2356 | if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID && |
| 2357 | StateIsRunningState(thread.GetState())) { |
| 2358 | // We will need to wait for this new thread to stop as well before firing |
| 2359 | // the |
| 2360 | // notification. |
| 2361 | thread.RequestStop(); |
| 2362 | } |
| 2363 | } |
| 2364 | |
| 2365 | void NativeProcessLinux::SigchldHandler() { |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2366 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2367 | // Process all pending waitpid notifications. |
| 2368 | while (true) { |
| 2369 | int status = -1; |
| 2370 | ::pid_t wait_pid = waitpid(-1, &status, __WALL | __WNOTHREAD | WNOHANG); |
| 2371 | |
| 2372 | if (wait_pid == 0) |
| 2373 | break; // We are done. |
| 2374 | |
| 2375 | if (wait_pid == -1) { |
| 2376 | if (errno == EINTR) |
| 2377 | continue; |
| 2378 | |
| 2379 | Error error(errno, eErrorTypePOSIX); |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2380 | LLDB_LOG(log, "waitpid (-1, &status, _) failed: {0}", error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2381 | break; |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2384 | bool exited = false; |
| 2385 | int signal = 0; |
| 2386 | int exit_status = 0; |
| 2387 | const char *status_cstr = nullptr; |
| 2388 | if (WIFSTOPPED(status)) { |
| 2389 | signal = WSTOPSIG(status); |
| 2390 | status_cstr = "STOPPED"; |
| 2391 | } else if (WIFEXITED(status)) { |
| 2392 | exit_status = WEXITSTATUS(status); |
| 2393 | status_cstr = "EXITED"; |
| 2394 | exited = true; |
| 2395 | } else if (WIFSIGNALED(status)) { |
| 2396 | signal = WTERMSIG(status); |
| 2397 | status_cstr = "SIGNALED"; |
| 2398 | if (wait_pid == static_cast<::pid_t>(GetID())) { |
| 2399 | exited = true; |
| 2400 | exit_status = -1; |
| 2401 | } |
| 2402 | } else |
| 2403 | status_cstr = "(\?\?\?)"; |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 2404 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2405 | LLDB_LOG(log, |
| 2406 | "waitpid (-1, &status, _) => pid = {0}, status = {1:x} " |
| 2407 | "({2}), signal = {3}, exit_state = {4}", |
| 2408 | wait_pid, status, status_cstr, signal, exit_status); |
Pavel Labath | c076559 | 2015-05-06 10:46:34 +0000 | [diff] [blame] | 2409 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2410 | MonitorCallback(wait_pid, exited, signal, exit_status); |
| 2411 | } |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | // Wrapper for ptrace to catch errors and log calls. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2415 | // Note that ptrace sets errno on error because -1 can be a valid result (i.e. |
| 2416 | // for PTRACE_PEEK*) |
| 2417 | Error NativeProcessLinux::PtraceWrapper(int req, lldb::pid_t pid, void *addr, |
| 2418 | void *data, size_t data_size, |
| 2419 | long *result) { |
| 2420 | Error error; |
| 2421 | long int ret; |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2422 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2423 | Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2424 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2425 | PtraceDisplayBytes(req, data, data_size); |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2426 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2427 | errno = 0; |
| 2428 | if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) |
| 2429 | ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), |
| 2430 | *(unsigned int *)addr, data); |
| 2431 | else |
| 2432 | ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), |
| 2433 | addr, data); |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2434 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2435 | if (ret == -1) |
| 2436 | error.SetErrorToErrno(); |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2437 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2438 | if (result) |
| 2439 | *result = ret; |
Pavel Labath | 4a9babb | 2015-06-30 17:04:49 +0000 | [diff] [blame] | 2440 | |
Pavel Labath | 2809620 | 2017-02-17 16:09:10 +0000 | [diff] [blame] | 2441 | LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3}, {4})={5:x}", req, pid, addr, data, |
| 2442 | data_size, ret); |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2443 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2444 | PtraceDisplayBytes(req, data, data_size); |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2445 | |
Pavel Labath | a6321a8 | 2017-01-19 15:26:04 +0000 | [diff] [blame] | 2446 | if (error.Fail()) |
| 2447 | LLDB_LOG(log, "ptrace() failed: {0}", error); |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2448 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2449 | return error; |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 2450 | } |