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