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