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