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