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