| File: | build/source/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp |
| Warning: | line 143, column 5 2nd function call argument is an uninitialized value |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | //===-- NativeProcessLinux.cpp --------------------------------------------===// | |||
| 2 | // | |||
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
| 4 | // See https://llvm.org/LICENSE.txt for license information. | |||
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
| 6 | // | |||
| 7 | //===----------------------------------------------------------------------===// | |||
| 8 | ||||
| 9 | #include "NativeProcessLinux.h" | |||
| 10 | ||||
| 11 | #include <cerrno> | |||
| 12 | #include <cstdint> | |||
| 13 | #include <cstring> | |||
| 14 | #include <unistd.h> | |||
| 15 | ||||
| 16 | #include <fstream> | |||
| 17 | #include <mutex> | |||
| 18 | #include <optional> | |||
| 19 | #include <sstream> | |||
| 20 | #include <string> | |||
| 21 | #include <unordered_map> | |||
| 22 | ||||
| 23 | #include "NativeThreadLinux.h" | |||
| 24 | #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" | |||
| 25 | #include "Plugins/Process/Utility/LinuxProcMaps.h" | |||
| 26 | #include "Procfs.h" | |||
| 27 | #include "lldb/Core/ModuleSpec.h" | |||
| 28 | #include "lldb/Host/Host.h" | |||
| 29 | #include "lldb/Host/HostProcess.h" | |||
| 30 | #include "lldb/Host/ProcessLaunchInfo.h" | |||
| 31 | #include "lldb/Host/PseudoTerminal.h" | |||
| 32 | #include "lldb/Host/ThreadLauncher.h" | |||
| 33 | #include "lldb/Host/common/NativeRegisterContext.h" | |||
| 34 | #include "lldb/Host/linux/Host.h" | |||
| 35 | #include "lldb/Host/linux/Ptrace.h" | |||
| 36 | #include "lldb/Host/linux/Uio.h" | |||
| 37 | #include "lldb/Host/posix/ProcessLauncherPosixFork.h" | |||
| 38 | #include "lldb/Symbol/ObjectFile.h" | |||
| 39 | #include "lldb/Target/Process.h" | |||
| 40 | #include "lldb/Target/Target.h" | |||
| 41 | #include "lldb/Utility/LLDBAssert.h" | |||
| 42 | #include "lldb/Utility/LLDBLog.h" | |||
| 43 | #include "lldb/Utility/State.h" | |||
| 44 | #include "lldb/Utility/Status.h" | |||
| 45 | #include "lldb/Utility/StringExtractor.h" | |||
| 46 | #include "llvm/ADT/ScopeExit.h" | |||
| 47 | #include "llvm/Support/Errno.h" | |||
| 48 | #include "llvm/Support/Error.h" | |||
| 49 | #include "llvm/Support/FileSystem.h" | |||
| 50 | #include "llvm/Support/Threading.h" | |||
| 51 | ||||
| 52 | #include <linux/unistd.h> | |||
| 53 | #include <sys/socket.h> | |||
| 54 | #include <sys/syscall.h> | |||
| 55 | #include <sys/types.h> | |||
| 56 | #include <sys/user.h> | |||
| 57 | #include <sys/wait.h> | |||
| 58 | ||||
| 59 | #ifdef __aarch64__ | |||
| 60 | #include <asm/hwcap.h> | |||
| 61 | #include <sys/auxv.h> | |||
| 62 | #endif | |||
| 63 | ||||
| 64 | // Support hardware breakpoints in case it has not been defined | |||
| 65 | #ifndef TRAP_HWBKPTTRAP_HWBKPT | |||
| 66 | #define TRAP_HWBKPTTRAP_HWBKPT 4 | |||
| 67 | #endif | |||
| 68 | ||||
| 69 | #ifndef HWCAP2_MTE(1 << 18) | |||
| 70 | #define HWCAP2_MTE(1 << 18) (1 << 18) | |||
| 71 | #endif | |||
| 72 | ||||
| 73 | using namespace lldb; | |||
| 74 | using namespace lldb_private; | |||
| 75 | using namespace lldb_private::process_linux; | |||
| 76 | using namespace llvm; | |||
| 77 | ||||
| 78 | // Private bits we only need internally. | |||
| 79 | ||||
| 80 | static bool ProcessVmReadvSupported() { | |||
| 81 | static bool is_supported; | |||
| 82 | static llvm::once_flag flag; | |||
| 83 | ||||
| 84 | llvm::call_once(flag, [] { | |||
| 85 | Log *log = GetLog(POSIXLog::Process); | |||
| 86 | ||||
| 87 | uint32_t source = 0x47424742; | |||
| 88 | uint32_t dest = 0; | |||
| 89 | ||||
| 90 | struct iovec local, remote; | |||
| 91 | remote.iov_base = &source; | |||
| 92 | local.iov_base = &dest; | |||
| 93 | remote.iov_len = local.iov_len = sizeof source; | |||
| 94 | ||||
| 95 | // We shall try if cross-process-memory reads work by attempting to read a | |||
| 96 | // value from our own process. | |||
| 97 | ssize_t res = process_vm_readv(getpid(), &local, 1, &remote, 1, 0); | |||
| 98 | is_supported = (res == sizeof(source) && source == dest); | |||
| 99 | if (is_supported) | |||
| 100 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Detected kernel support for process_vm_readv syscall. " "Fast memory reads enabled."); } while (0) | |||
| 101 | "Detected kernel support for process_vm_readv syscall. "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Detected kernel support for process_vm_readv syscall. " "Fast memory reads enabled."); } while (0) | |||
| 102 | "Fast memory reads enabled.")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Detected kernel support for process_vm_readv syscall. " "Fast memory reads enabled."); } while (0); | |||
| 103 | else | |||
| 104 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "syscall process_vm_readv failed (error: {0}). Fast memory " "reads disabled.", llvm::sys::StrError()); } while (0) | |||
| 105 | "syscall process_vm_readv failed (error: {0}). Fast memory "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "syscall process_vm_readv failed (error: {0}). Fast memory " "reads disabled.", llvm::sys::StrError()); } while (0) | |||
| 106 | "reads disabled.",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "syscall process_vm_readv failed (error: {0}). Fast memory " "reads disabled.", llvm::sys::StrError()); } while (0) | |||
| 107 | llvm::sys::StrError())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "syscall process_vm_readv failed (error: {0}). Fast memory " "reads disabled.", llvm::sys::StrError()); } while (0); | |||
| 108 | }); | |||
| 109 | ||||
| 110 | return is_supported; | |||
| 111 | } | |||
| 112 | ||||
| 113 | static void MaybeLogLaunchInfo(const ProcessLaunchInfo &info) { | |||
| 114 | Log *log = GetLog(POSIXLog::Process); | |||
| 115 | if (!log) | |||
| 116 | return; | |||
| 117 | ||||
| 118 | if (const FileAction *action = info.GetFileActionForFD(STDIN_FILENO0)) | |||
| 119 | LLDB_LOG(log, "setting STDIN to '{0}'", action->GetFileSpec())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "setting STDIN to '{0}'", action->GetFileSpec( )); } while (0); | |||
| 120 | else | |||
| 121 | LLDB_LOG(log, "leaving STDIN as is")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "leaving STDIN as is"); } while (0); | |||
| 122 | ||||
| 123 | if (const FileAction *action = info.GetFileActionForFD(STDOUT_FILENO1)) | |||
| 124 | LLDB_LOG(log, "setting STDOUT to '{0}'", action->GetFileSpec())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "setting STDOUT to '{0}'", action->GetFileSpec ()); } while (0); | |||
| 125 | else | |||
| 126 | LLDB_LOG(log, "leaving STDOUT as is")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "leaving STDOUT as is"); } while (0); | |||
| 127 | ||||
| 128 | if (const FileAction *action = info.GetFileActionForFD(STDERR_FILENO2)) | |||
| 129 | LLDB_LOG(log, "setting STDERR to '{0}'", action->GetFileSpec())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "setting STDERR to '{0}'", action->GetFileSpec ()); } while (0); | |||
| 130 | else | |||
| 131 | LLDB_LOG(log, "leaving STDERR as is")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "leaving STDERR as is"); } while (0); | |||
| 132 | ||||
| 133 | int i = 0; | |||
| 134 | for (const char **args = info.GetArguments().GetConstArgumentVector(); *args; | |||
| 135 | ++args, ++i) | |||
| 136 | LLDB_LOG(log, "arg {0}: '{1}'", i, *args)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "arg {0}: '{1}'", i, *args); } while (0); | |||
| 137 | } | |||
| 138 | ||||
| 139 | static void DisplayBytes(StreamString &s, void *bytes, uint32_t count) { | |||
| 140 | uint8_t *ptr = (uint8_t *)bytes; | |||
| 141 | const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES20, count); | |||
| 142 | for (uint32_t i = 0; i < loop_count; i++) { | |||
| 143 | s.Printf("[%x]", *ptr); | |||
| ||||
| 144 | ptr++; | |||
| 145 | } | |||
| 146 | } | |||
| 147 | ||||
| 148 | static void PtraceDisplayBytes(int &req, void *data, size_t data_size) { | |||
| 149 | Log *log = GetLog(POSIXLog::Ptrace); | |||
| 150 | if (!log
| |||
| 151 | return; | |||
| 152 | StreamString buf; | |||
| 153 | ||||
| 154 | switch (req) { | |||
| 155 | case PTRACE_POKETEXT: { | |||
| 156 | DisplayBytes(buf, &data, 8); | |||
| 157 | LLDB_LOGV(log, "PTRACE_POKETEXT {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private && log_private->GetVerbose()) log_private->Format ("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", __func__ , "PTRACE_POKETEXT {0}", buf.GetData()); } while (0); | |||
| 158 | break; | |||
| 159 | } | |||
| 160 | case PTRACE_POKEDATA: { | |||
| 161 | DisplayBytes(buf, &data, 8); | |||
| 162 | LLDB_LOGV(log, "PTRACE_POKEDATA {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private && log_private->GetVerbose()) log_private->Format ("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", __func__ , "PTRACE_POKEDATA {0}", buf.GetData()); } while (0); | |||
| 163 | break; | |||
| 164 | } | |||
| 165 | case PTRACE_POKEUSER: { | |||
| 166 | DisplayBytes(buf, &data, 8); | |||
| 167 | LLDB_LOGV(log, "PTRACE_POKEUSER {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private && log_private->GetVerbose()) log_private->Format ("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", __func__ , "PTRACE_POKEUSER {0}", buf.GetData()); } while (0); | |||
| 168 | break; | |||
| 169 | } | |||
| 170 | case PTRACE_SETREGS13: { | |||
| 171 | DisplayBytes(buf, data, data_size); | |||
| 172 | LLDB_LOGV(log, "PTRACE_SETREGS {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private && log_private->GetVerbose()) log_private->Format ("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", __func__ , "PTRACE_SETREGS {0}", buf.GetData()); } while (0); | |||
| 173 | break; | |||
| 174 | } | |||
| 175 | case PTRACE_SETFPREGS15: { | |||
| 176 | DisplayBytes(buf, data, data_size); | |||
| 177 | LLDB_LOGV(log, "PTRACE_SETFPREGS {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private && log_private->GetVerbose()) log_private->Format ("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", __func__ , "PTRACE_SETFPREGS {0}", buf.GetData()); } while (0); | |||
| 178 | break; | |||
| 179 | } | |||
| 180 | case PTRACE_SETSIGINFO: { | |||
| 181 | DisplayBytes(buf, data, sizeof(siginfo_t)); | |||
| 182 | LLDB_LOGV(log, "PTRACE_SETSIGINFO {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private && log_private->GetVerbose()) log_private->Format ("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", __func__ , "PTRACE_SETSIGINFO {0}", buf.GetData()); } while (0); | |||
| 183 | break; | |||
| 184 | } | |||
| 185 | case PTRACE_SETREGSETPTRACE_SETREGSET: { | |||
| 186 | // Extract iov_base from data, which is a pointer to the struct iovec | |||
| 187 | DisplayBytes(buf, *(void **)data, data_size); | |||
| 188 | LLDB_LOGV(log, "PTRACE_SETREGSET {0}", buf.GetData())do { ::lldb_private::Log *log_private = (log); if (log_private && log_private->GetVerbose()) log_private->Format ("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", __func__ , "PTRACE_SETREGSET {0}", buf.GetData()); } while (0); | |||
| 189 | break; | |||
| 190 | } | |||
| 191 | default: {} | |||
| 192 | } | |||
| 193 | } | |||
| 194 | ||||
| 195 | static constexpr unsigned k_ptrace_word_size = sizeof(void *); | |||
| 196 | static_assert(sizeof(long) >= k_ptrace_word_size, | |||
| 197 | "Size of long must be larger than ptrace word size"); | |||
| 198 | ||||
| 199 | // Simple helper function to ensure flags are enabled on the given file | |||
| 200 | // descriptor. | |||
| 201 | static Status EnsureFDFlags(int fd, int flags) { | |||
| 202 | Status error; | |||
| 203 | ||||
| 204 | int status = fcntl(fd, F_GETFL3); | |||
| 205 | if (status == -1) { | |||
| 206 | error.SetErrorToErrno(); | |||
| 207 | return error; | |||
| 208 | } | |||
| 209 | ||||
| 210 | if (fcntl(fd, F_SETFL4, status | flags) == -1) { | |||
| 211 | error.SetErrorToErrno(); | |||
| 212 | return error; | |||
| 213 | } | |||
| 214 | ||||
| 215 | return error; | |||
| 216 | } | |||
| 217 | ||||
| 218 | static llvm::Error AddPtraceScopeNote(llvm::Error original_error) { | |||
| 219 | Expected<int> ptrace_scope = GetPtraceScope(); | |||
| 220 | if (auto E = ptrace_scope.takeError()) { | |||
| 221 | Log *log = GetLog(POSIXLog::Process); | |||
| 222 | LLDB_LOG(log, "error reading value of ptrace_scope: {0}", E)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "error reading value of ptrace_scope: {0}", E); } while (0); | |||
| 223 | ||||
| 224 | // The original error is probably more interesting than not being able to | |||
| 225 | // read or interpret ptrace_scope. | |||
| 226 | return original_error; | |||
| 227 | } | |||
| 228 | ||||
| 229 | // We only have suggestions to provide for 1-3. | |||
| 230 | switch (*ptrace_scope) { | |||
| 231 | case 1: | |||
| 232 | case 2: | |||
| 233 | return llvm::createStringError( | |||
| 234 | std::error_code(errno(*__errno_location ()), std::generic_category()), | |||
| 235 | "The current value of ptrace_scope is %d, which can cause ptrace to " | |||
| 236 | "fail to attach to a running process. To fix this, run:\n" | |||
| 237 | "\tsudo sysctl -w kernel.yama.ptrace_scope=0\n" | |||
| 238 | "For more information, see: " | |||
| 239 | "https://www.kernel.org/doc/Documentation/security/Yama.txt.", | |||
| 240 | *ptrace_scope); | |||
| 241 | case 3: | |||
| 242 | return llvm::createStringError( | |||
| 243 | std::error_code(errno(*__errno_location ()), std::generic_category()), | |||
| 244 | "The current value of ptrace_scope is 3, which will cause ptrace to " | |||
| 245 | "fail to attach to a running process. This value cannot be changed " | |||
| 246 | "without rebooting.\n" | |||
| 247 | "For more information, see: " | |||
| 248 | "https://www.kernel.org/doc/Documentation/security/Yama.txt."); | |||
| 249 | case 0: | |||
| 250 | default: | |||
| 251 | return original_error; | |||
| 252 | } | |||
| 253 | } | |||
| 254 | ||||
| 255 | NativeProcessLinux::Manager::Manager(MainLoop &mainloop) | |||
| 256 | : NativeProcessProtocol::Manager(mainloop) { | |||
| 257 | Status status; | |||
| 258 | m_sigchld_handle = mainloop.RegisterSignal( | |||
| 259 | SIGCHLD17, [this](MainLoopBase &) { SigchldHandler(); }, status); | |||
| 260 | assert(m_sigchld_handle && status.Success())(static_cast <bool> (m_sigchld_handle && status .Success()) ? void (0) : __assert_fail ("m_sigchld_handle && status.Success()" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 260, __extension__ __PRETTY_FUNCTION__)); | |||
| 261 | } | |||
| 262 | ||||
| 263 | llvm::Expected<std::unique_ptr<NativeProcessProtocol>> | |||
| 264 | NativeProcessLinux::Manager::Launch(ProcessLaunchInfo &launch_info, | |||
| 265 | NativeDelegate &native_delegate) { | |||
| 266 | Log *log = GetLog(POSIXLog::Process); | |||
| 267 | ||||
| 268 | MaybeLogLaunchInfo(launch_info); | |||
| 269 | ||||
| 270 | Status status; | |||
| 271 | ::pid_t pid = ProcessLauncherPosixFork() | |||
| 272 | .LaunchProcess(launch_info, status) | |||
| 273 | .GetProcessId(); | |||
| 274 | LLDB_LOG(log, "pid = {0:x}", pid)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid = {0:x}", pid); } while (0); | |||
| 275 | if (status.Fail()) { | |||
| 276 | LLDB_LOG(log, "failed to launch process: {0}", status)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "failed to launch process: {0}", status); } while (0); | |||
| 277 | return status.ToError(); | |||
| 278 | } | |||
| 279 | ||||
| 280 | // Wait for the child process to trap on its call to execve. | |||
| 281 | int wstatus = 0; | |||
| 282 | ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, ::waitpid, pid, &wstatus, 0); | |||
| 283 | assert(wpid == pid)(static_cast <bool> (wpid == pid) ? void (0) : __assert_fail ("wpid == pid", "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , 283, __extension__ __PRETTY_FUNCTION__)); | |||
| 284 | (void)wpid; | |||
| 285 | if (!WIFSTOPPED(wstatus)(((wstatus) & 0xff) == 0x7f)) { | |||
| 286 | LLDB_LOG(log, "Could not sync with inferior process: wstatus={1}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Could not sync with inferior process: wstatus={1}" , WaitStatus::Decode(wstatus)); } while (0) | |||
| 287 | WaitStatus::Decode(wstatus))do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Could not sync with inferior process: wstatus={1}" , WaitStatus::Decode(wstatus)); } while (0); | |||
| 288 | return llvm::make_error<StringError>("Could not sync with inferior process", | |||
| 289 | llvm::inconvertibleErrorCode()); | |||
| 290 | } | |||
| 291 | LLDB_LOG(log, "inferior started, now in stopped state")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "inferior started, now in stopped state"); } while (0); | |||
| 292 | ||||
| 293 | status = SetDefaultPtraceOpts(pid); | |||
| 294 | if (status.Fail()) { | |||
| 295 | LLDB_LOG(log, "failed to set default ptrace options: {0}", status)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "failed to set default ptrace options: {0}", status ); } while (0); | |||
| 296 | return status.ToError(); | |||
| 297 | } | |||
| 298 | ||||
| 299 | llvm::Expected<ArchSpec> arch_or = | |||
| 300 | NativeRegisterContextLinux::DetermineArchitecture(pid); | |||
| 301 | if (!arch_or) | |||
| 302 | return arch_or.takeError(); | |||
| 303 | ||||
| 304 | return std::unique_ptr<NativeProcessLinux>(new NativeProcessLinux( | |||
| 305 | pid, launch_info.GetPTY().ReleasePrimaryFileDescriptor(), native_delegate, | |||
| 306 | *arch_or, *this, {pid})); | |||
| 307 | } | |||
| 308 | ||||
| 309 | llvm::Expected<std::unique_ptr<NativeProcessProtocol>> | |||
| 310 | NativeProcessLinux::Manager::Attach( | |||
| 311 | lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate) { | |||
| 312 | Log *log = GetLog(POSIXLog::Process); | |||
| 313 | LLDB_LOG(log, "pid = {0:x}", pid)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid = {0:x}", pid); } while (0); | |||
| 314 | ||||
| 315 | auto tids_or = NativeProcessLinux::Attach(pid); | |||
| 316 | if (!tids_or) | |||
| 317 | return tids_or.takeError(); | |||
| 318 | ArrayRef<::pid_t> tids = *tids_or; | |||
| 319 | llvm::Expected<ArchSpec> arch_or = | |||
| 320 | NativeRegisterContextLinux::DetermineArchitecture(tids[0]); | |||
| 321 | if (!arch_or) | |||
| 322 | return arch_or.takeError(); | |||
| 323 | ||||
| 324 | return std::unique_ptr<NativeProcessLinux>( | |||
| 325 | new NativeProcessLinux(pid, -1, native_delegate, *arch_or, *this, tids)); | |||
| 326 | } | |||
| 327 | ||||
| 328 | NativeProcessLinux::Extension | |||
| 329 | NativeProcessLinux::Manager::GetSupportedExtensions() const { | |||
| 330 | NativeProcessLinux::Extension supported = | |||
| 331 | Extension::multiprocess | Extension::fork | Extension::vfork | | |||
| 332 | Extension::pass_signals | Extension::auxv | Extension::libraries_svr4 | | |||
| 333 | Extension::siginfo_read; | |||
| 334 | ||||
| 335 | #ifdef __aarch64__ | |||
| 336 | // At this point we do not have a process so read auxv directly. | |||
| 337 | if ((getauxval(AT_HWCAP2) & HWCAP2_MTE(1 << 18))) | |||
| 338 | supported |= Extension::memory_tagging; | |||
| 339 | #endif | |||
| 340 | ||||
| 341 | return supported; | |||
| 342 | } | |||
| 343 | ||||
| 344 | static std::optional<std::pair<lldb::pid_t, WaitStatus>> WaitPid() { | |||
| 345 | Log *log = GetLog(POSIXLog::Process); | |||
| 346 | ||||
| 347 | int status; | |||
| 348 | ::pid_t wait_pid = llvm::sys::RetryAfterSignal( | |||
| 349 | -1, ::waitpid, -1, &status, __WALL0x40000000 | __WNOTHREAD0x20000000 | WNOHANG1); | |||
| 350 | ||||
| 351 | if (wait_pid == 0) | |||
| 352 | return std::nullopt; | |||
| 353 | ||||
| 354 | if (wait_pid == -1) { | |||
| 355 | Status error(errno(*__errno_location ()), eErrorTypePOSIX); | |||
| 356 | LLDB_LOG(log, "waitpid(-1, &status, _) failed: {1}", error)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "waitpid(-1, &status, _) failed: {1}", error) ; } while (0); | |||
| 357 | return std::nullopt; | |||
| 358 | } | |||
| 359 | ||||
| 360 | WaitStatus wait_status = WaitStatus::Decode(status); | |||
| 361 | ||||
| 362 | LLDB_LOG(log, "waitpid(-1, &status, _) = {0}, status = {1}", wait_pid,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "waitpid(-1, &status, _) = {0}, status = {1}" , wait_pid, wait_status); } while (0) | |||
| 363 | wait_status)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "waitpid(-1, &status, _) = {0}, status = {1}" , wait_pid, wait_status); } while (0); | |||
| 364 | return std::make_pair(wait_pid, wait_status); | |||
| 365 | } | |||
| 366 | ||||
| 367 | void NativeProcessLinux::Manager::SigchldHandler() { | |||
| 368 | Log *log = GetLog(POSIXLog::Process); | |||
| 369 | while (true) { | |||
| 370 | auto wait_result = WaitPid(); | |||
| 371 | if (!wait_result) | |||
| 372 | return; | |||
| 373 | lldb::pid_t pid = wait_result->first; | |||
| 374 | WaitStatus status = wait_result->second; | |||
| 375 | ||||
| 376 | // Ask each process whether it wants to handle the event. Each event should | |||
| 377 | // be handled by exactly one process, but thread creation events require | |||
| 378 | // special handling. | |||
| 379 | // Thread creation consists of two events (one on the parent and one on the | |||
| 380 | // child thread) and they can arrive in any order nondeterministically. The | |||
| 381 | // parent event carries the information about the child thread, but not | |||
| 382 | // vice-versa. This means that if the child event arrives first, it may not | |||
| 383 | // be handled by any process (because it doesn't know the thread belongs to | |||
| 384 | // it). | |||
| 385 | bool handled = llvm::any_of(m_processes, [&](NativeProcessLinux *process) { | |||
| 386 | return process->TryHandleWaitStatus(pid, status); | |||
| 387 | }); | |||
| 388 | if (!handled) { | |||
| 389 | if (status.type == WaitStatus::Stop && status.status == SIGSTOP19) { | |||
| 390 | // Store the thread creation event for later collection. | |||
| 391 | m_unowned_threads.insert(pid); | |||
| 392 | } else { | |||
| 393 | LLDB_LOG(log, "Ignoring waitpid event {0} for pid {1}", status, pid)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Ignoring waitpid event {0} for pid {1}", status, pid); } while (0); | |||
| 394 | } | |||
| 395 | } | |||
| 396 | } | |||
| 397 | } | |||
| 398 | ||||
| 399 | void NativeProcessLinux::Manager::CollectThread(::pid_t tid) { | |||
| 400 | Log *log = GetLog(POSIXLog::Process); | |||
| 401 | ||||
| 402 | if (m_unowned_threads.erase(tid)) | |||
| 403 | return; // We've encountered this thread already. | |||
| 404 | ||||
| 405 | // The TID is not tracked yet, let's wait for it to appear. | |||
| 406 | int status = -1; | |||
| 407 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received clone event for tid {0}. tid not tracked yet, " "waiting for it to appear...", tid); } while (0) | |||
| 408 | "received clone event for tid {0}. tid not tracked yet, "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received clone event for tid {0}. tid not tracked yet, " "waiting for it to appear...", tid); } while (0) | |||
| 409 | "waiting for it to appear...",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received clone event for tid {0}. tid not tracked yet, " "waiting for it to appear...", tid); } while (0) | |||
| 410 | tid)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received clone event for tid {0}. tid not tracked yet, " "waiting for it to appear...", tid); } while (0); | |||
| 411 | ::pid_t wait_pid = | |||
| 412 | llvm::sys::RetryAfterSignal(-1, ::waitpid, tid, &status, __WALL0x40000000); | |||
| 413 | ||||
| 414 | // It's theoretically possible to get other events if the entire process was | |||
| 415 | // SIGKILLed before we got a chance to check this. In that case, we'll just | |||
| 416 | // clean everything up when we get the process exit event. | |||
| 417 | ||||
| 418 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "waitpid({0}, &status, __WALL) => {1} (errno: {2}, status = {3})" , tid, wait_pid, (*__errno_location ()), WaitStatus::Decode(status )); } while (0) | |||
| 419 | "waitpid({0}, &status, __WALL) => {1} (errno: {2}, status = {3})",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "waitpid({0}, &status, __WALL) => {1} (errno: {2}, status = {3})" , tid, wait_pid, (*__errno_location ()), WaitStatus::Decode(status )); } while (0) | |||
| 420 | tid, wait_pid, errno, WaitStatus::Decode(status))do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "waitpid({0}, &status, __WALL) => {1} (errno: {2}, status = {3})" , tid, wait_pid, (*__errno_location ()), WaitStatus::Decode(status )); } while (0); | |||
| 421 | } | |||
| 422 | ||||
| 423 | // Public Instance Methods | |||
| 424 | ||||
| 425 | NativeProcessLinux::NativeProcessLinux(::pid_t pid, int terminal_fd, | |||
| 426 | NativeDelegate &delegate, | |||
| 427 | const ArchSpec &arch, Manager &manager, | |||
| 428 | llvm::ArrayRef<::pid_t> tids) | |||
| 429 | : NativeProcessELF(pid, terminal_fd, delegate), m_manager(manager), | |||
| 430 | m_arch(arch), m_intel_pt_collector(*this) { | |||
| 431 | manager.AddProcess(*this); | |||
| 432 | if (m_terminal_fd != -1) { | |||
| 433 | Status status = EnsureFDFlags(m_terminal_fd, O_NONBLOCK04000); | |||
| 434 | assert(status.Success())(static_cast <bool> (status.Success()) ? void (0) : __assert_fail ("status.Success()", "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , 434, __extension__ __PRETTY_FUNCTION__)); | |||
| 435 | } | |||
| 436 | ||||
| 437 | for (const auto &tid : tids) { | |||
| 438 | NativeThreadLinux &thread = AddThread(tid, /*resume*/ false); | |||
| 439 | ThreadWasCreated(thread); | |||
| 440 | } | |||
| 441 | ||||
| 442 | // Let our process instance know the thread has stopped. | |||
| 443 | SetCurrentThreadID(tids[0]); | |||
| 444 | SetState(StateType::eStateStopped, false); | |||
| 445 | } | |||
| 446 | ||||
| 447 | llvm::Expected<std::vector<::pid_t>> NativeProcessLinux::Attach(::pid_t pid) { | |||
| 448 | Log *log = GetLog(POSIXLog::Process); | |||
| 449 | ||||
| 450 | Status status; | |||
| 451 | // Use a map to keep track of the threads which we have attached/need to | |||
| 452 | // attach. | |||
| 453 | Host::TidMap tids_to_attach; | |||
| 454 | while (Host::FindProcessThreads(pid, tids_to_attach)) { | |||
| 455 | for (Host::TidMap::iterator it = tids_to_attach.begin(); | |||
| 456 | it != tids_to_attach.end();) { | |||
| 457 | if (it->second == false) { | |||
| 458 | lldb::tid_t tid = it->first; | |||
| 459 | ||||
| 460 | // Attach to the requested process. | |||
| 461 | // An attach will cause the thread to stop with a SIGSTOP. | |||
| 462 | if ((status = PtraceWrapper(PTRACE_ATTACH, tid)).Fail()) { | |||
| 463 | // No such thread. The thread may have exited. More error handling | |||
| 464 | // may be needed. | |||
| 465 | if (status.GetError() == ESRCH3) { | |||
| 466 | it = tids_to_attach.erase(it); | |||
| 467 | continue; | |||
| 468 | } | |||
| 469 | if (status.GetError() == EPERM1) { | |||
| 470 | // Depending on the value of ptrace_scope, we can return a different | |||
| 471 | // error that suggests how to fix it. | |||
| 472 | return AddPtraceScopeNote(status.ToError()); | |||
| 473 | } | |||
| 474 | return status.ToError(); | |||
| 475 | } | |||
| 476 | ||||
| 477 | int wpid = | |||
| 478 | llvm::sys::RetryAfterSignal(-1, ::waitpid, tid, nullptr, __WALL0x40000000); | |||
| 479 | // Need to use __WALL otherwise we receive an error with errno=ECHLD At | |||
| 480 | // this point we should have a thread stopped if waitpid succeeds. | |||
| 481 | if (wpid < 0) { | |||
| 482 | // No such thread. The thread may have exited. More error handling | |||
| 483 | // may be needed. | |||
| 484 | if (errno(*__errno_location ()) == ESRCH3) { | |||
| 485 | it = tids_to_attach.erase(it); | |||
| 486 | continue; | |||
| 487 | } | |||
| 488 | return llvm::errorCodeToError( | |||
| 489 | std::error_code(errno(*__errno_location ()), std::generic_category())); | |||
| 490 | } | |||
| 491 | ||||
| 492 | if ((status = SetDefaultPtraceOpts(tid)).Fail()) | |||
| 493 | return status.ToError(); | |||
| 494 | ||||
| 495 | LLDB_LOG(log, "adding tid = {0}", tid)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "adding tid = {0}", tid); } while (0); | |||
| 496 | it->second = true; | |||
| 497 | } | |||
| 498 | ||||
| 499 | // move the loop forward | |||
| 500 | ++it; | |||
| 501 | } | |||
| 502 | } | |||
| 503 | ||||
| 504 | size_t tid_count = tids_to_attach.size(); | |||
| 505 | if (tid_count == 0) | |||
| 506 | return llvm::make_error<StringError>("No such process", | |||
| 507 | llvm::inconvertibleErrorCode()); | |||
| 508 | ||||
| 509 | std::vector<::pid_t> tids; | |||
| 510 | tids.reserve(tid_count); | |||
| 511 | for (const auto &p : tids_to_attach) | |||
| 512 | tids.push_back(p.first); | |||
| 513 | return std::move(tids); | |||
| 514 | } | |||
| 515 | ||||
| 516 | Status NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) { | |||
| 517 | long ptrace_opts = 0; | |||
| 518 | ||||
| 519 | // Have the child raise an event on exit. This is used to keep the child in | |||
| 520 | // limbo until it is destroyed. | |||
| 521 | ptrace_opts |= PTRACE_O_TRACEEXIT; | |||
| 522 | ||||
| 523 | // Have the tracer trace threads which spawn in the inferior process. | |||
| 524 | ptrace_opts |= PTRACE_O_TRACECLONE; | |||
| 525 | ||||
| 526 | // Have the tracer notify us before execve returns (needed to disable legacy | |||
| 527 | // SIGTRAP generation) | |||
| 528 | ptrace_opts |= PTRACE_O_TRACEEXEC; | |||
| 529 | ||||
| 530 | // Have the tracer trace forked children. | |||
| 531 | ptrace_opts |= PTRACE_O_TRACEFORK; | |||
| 532 | ||||
| 533 | // Have the tracer trace vforks. | |||
| 534 | ptrace_opts |= PTRACE_O_TRACEVFORK; | |||
| 535 | ||||
| 536 | // Have the tracer trace vfork-done in order to restore breakpoints after | |||
| 537 | // the child finishes sharing memory. | |||
| 538 | ptrace_opts |= PTRACE_O_TRACEVFORKDONE; | |||
| 539 | ||||
| 540 | return PtraceWrapper(PTRACE_SETOPTIONS, pid, nullptr, (void *)ptrace_opts); | |||
| 541 | } | |||
| 542 | ||||
| 543 | bool NativeProcessLinux::TryHandleWaitStatus(lldb::pid_t pid, | |||
| 544 | WaitStatus status) { | |||
| 545 | if (pid == GetID() && | |||
| 546 | (status.type == WaitStatus::Exit || status.type == WaitStatus::Signal)) { | |||
| 547 | // The process exited. We're done monitoring. Report to delegate. | |||
| 548 | SetExitStatus(status, true); | |||
| 549 | return true; | |||
| 550 | } | |||
| 551 | if (NativeThreadLinux *thread = GetThreadByID(pid)) { | |||
| 552 | MonitorCallback(*thread, status); | |||
| 553 | return true; | |||
| 554 | } | |||
| 555 | return false; | |||
| 556 | } | |||
| 557 | ||||
| 558 | void NativeProcessLinux::MonitorCallback(NativeThreadLinux &thread, | |||
| 559 | WaitStatus status) { | |||
| 560 | Log *log = GetLog(LLDBLog::Process); | |||
| 561 | ||||
| 562 | // Certain activities differ based on whether the pid is the tid of the main | |||
| 563 | // thread. | |||
| 564 | const bool is_main_thread = (thread.GetID() == GetID()); | |||
| 565 | ||||
| 566 | // Handle when the thread exits. | |||
| 567 | if (status.type == WaitStatus::Exit || status.type == WaitStatus::Signal) { | |||
| 568 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "got exit status({0}) , tid = {1} ({2} main thread), process " "state = {3}", status, thread.GetID(), is_main_thread ? "is" : "is not", GetState()); } while (0) | |||
| 569 | "got exit status({0}) , tid = {1} ({2} main thread), process "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "got exit status({0}) , tid = {1} ({2} main thread), process " "state = {3}", status, thread.GetID(), is_main_thread ? "is" : "is not", GetState()); } while (0) | |||
| 570 | "state = {3}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "got exit status({0}) , tid = {1} ({2} main thread), process " "state = {3}", status, thread.GetID(), is_main_thread ? "is" : "is not", GetState()); } while (0) | |||
| 571 | status, thread.GetID(), is_main_thread ? "is" : "is not",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "got exit status({0}) , tid = {1} ({2} main thread), process " "state = {3}", status, thread.GetID(), is_main_thread ? "is" : "is not", GetState()); } while (0) | |||
| 572 | GetState())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "got exit status({0}) , tid = {1} ({2} main thread), process " "state = {3}", status, thread.GetID(), is_main_thread ? "is" : "is not", GetState()); } while (0); | |||
| 573 | ||||
| 574 | // This is a thread that exited. Ensure we're not tracking it anymore. | |||
| 575 | StopTrackingThread(thread); | |||
| 576 | ||||
| 577 | assert(!is_main_thread && "Main thread exits handled elsewhere")(static_cast <bool> (!is_main_thread && "Main thread exits handled elsewhere" ) ? void (0) : __assert_fail ("!is_main_thread && \"Main thread exits handled elsewhere\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 577, __extension__ __PRETTY_FUNCTION__)); | |||
| 578 | return; | |||
| 579 | } | |||
| 580 | ||||
| 581 | siginfo_t info; | |||
| 582 | const auto info_err = GetSignalInfo(thread.GetID(), &info); | |||
| 583 | ||||
| 584 | // Get details on the signal raised. | |||
| 585 | if (info_err.Success()) { | |||
| 586 | // We have retrieved the signal info. Dispatch appropriately. | |||
| 587 | if (info.si_signo == SIGTRAP5) | |||
| 588 | MonitorSIGTRAP(info, thread); | |||
| 589 | else | |||
| 590 | MonitorSignal(info, thread); | |||
| 591 | } else { | |||
| 592 | if (info_err.GetError() == EINVAL22) { | |||
| 593 | // This is a group stop reception for this tid. We can reach here if we | |||
| 594 | // reinject SIGSTOP, SIGSTP, SIGTTIN or SIGTTOU into the tracee, | |||
| 595 | // triggering the group-stop mechanism. Normally receiving these would | |||
| 596 | // stop the process, pending a SIGCONT. Simulating this state in a | |||
| 597 | // debugger is hard and is generally not needed (one use case is | |||
| 598 | // debugging background task being managed by a shell). For general use, | |||
| 599 | // it is sufficient to stop the process in a signal-delivery stop which | |||
| 600 | // happens before the group stop. This done by MonitorSignal and works | |||
| 601 | // correctly for all signals. | |||
| 602 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received a group stop for pid {0} tid {1}. Transparent " "handling of group stops not supported, resuming the " "thread." , GetID(), thread.GetID()); } while (0) | |||
| 603 | "received a group stop for pid {0} tid {1}. Transparent "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received a group stop for pid {0} tid {1}. Transparent " "handling of group stops not supported, resuming the " "thread." , GetID(), thread.GetID()); } while (0) | |||
| 604 | "handling of group stops not supported, resuming the "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received a group stop for pid {0} tid {1}. Transparent " "handling of group stops not supported, resuming the " "thread." , GetID(), thread.GetID()); } while (0) | |||
| 605 | "thread.",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received a group stop for pid {0} tid {1}. Transparent " "handling of group stops not supported, resuming the " "thread." , GetID(), thread.GetID()); } while (0) | |||
| 606 | GetID(), thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received a group stop for pid {0} tid {1}. Transparent " "handling of group stops not supported, resuming the " "thread." , GetID(), thread.GetID()); } while (0); | |||
| 607 | ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER(2147483647)); | |||
| 608 | } else { | |||
| 609 | // ptrace(GETSIGINFO) failed (but not due to group-stop). | |||
| 610 | ||||
| 611 | // A return value of ESRCH means the thread/process has died in the mean | |||
| 612 | // time. This can (e.g.) happen when another thread does an exit_group(2) | |||
| 613 | // or the entire process get SIGKILLed. | |||
| 614 | // We can't do anything with this thread anymore, but we keep it around | |||
| 615 | // until we get the WIFEXITED event. | |||
| 616 | ||||
| 617 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "GetSignalInfo({0}) failed: {1}, status = {2}, main_thread = " "{3}. Expecting WIFEXITED soon.", thread.GetID(), info_err, status , is_main_thread); } while (0) | |||
| 618 | "GetSignalInfo({0}) failed: {1}, status = {2}, main_thread = "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "GetSignalInfo({0}) failed: {1}, status = {2}, main_thread = " "{3}. Expecting WIFEXITED soon.", thread.GetID(), info_err, status , is_main_thread); } while (0) | |||
| 619 | "{3}. Expecting WIFEXITED soon.",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "GetSignalInfo({0}) failed: {1}, status = {2}, main_thread = " "{3}. Expecting WIFEXITED soon.", thread.GetID(), info_err, status , is_main_thread); } while (0) | |||
| 620 | thread.GetID(), info_err, status, is_main_thread)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "GetSignalInfo({0}) failed: {1}, status = {2}, main_thread = " "{3}. Expecting WIFEXITED soon.", thread.GetID(), info_err, status , is_main_thread); } while (0); | |||
| 621 | } | |||
| 622 | } | |||
| 623 | } | |||
| 624 | ||||
| 625 | void NativeProcessLinux::MonitorSIGTRAP(const siginfo_t &info, | |||
| 626 | NativeThreadLinux &thread) { | |||
| 627 | Log *log = GetLog(POSIXLog::Process); | |||
| 628 | const bool is_main_thread = (thread.GetID() == GetID()); | |||
| 629 | ||||
| 630 | assert(info.si_signo == SIGTRAP && "Unexpected child signal!")(static_cast <bool> (info.si_signo == 5 && "Unexpected child signal!" ) ? void (0) : __assert_fail ("info.si_signo == SIGTRAP && \"Unexpected child signal!\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 630, __extension__ __PRETTY_FUNCTION__)); | |||
| 631 | ||||
| 632 | switch (info.si_code) { | |||
| 633 | case (SIGTRAP5 | (PTRACE_EVENT_FORK << 8)): | |||
| 634 | case (SIGTRAP5 | (PTRACE_EVENT_VFORK << 8)): | |||
| 635 | case (SIGTRAP5 | (PTRACE_EVENT_CLONE << 8)): { | |||
| 636 | // This can either mean a new thread or a new process spawned via | |||
| 637 | // clone(2) without SIGCHLD or CLONE_VFORK flag. Note that clone(2) | |||
| 638 | // can also cause PTRACE_EVENT_FORK and PTRACE_EVENT_VFORK if one | |||
| 639 | // of these flags are passed. | |||
| 640 | ||||
| 641 | unsigned long event_message = 0; | |||
| 642 | if (GetEventMessage(thread.GetID(), &event_message).Fail()) { | |||
| 643 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} received clone() event but GetEventMessage failed " "so we don't know the new pid/tid", thread.GetID()); } while (0) | |||
| 644 | "pid {0} received clone() event but GetEventMessage failed "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} received clone() event but GetEventMessage failed " "so we don't know the new pid/tid", thread.GetID()); } while (0) | |||
| 645 | "so we don't know the new pid/tid",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} received clone() event but GetEventMessage failed " "so we don't know the new pid/tid", thread.GetID()); } while (0) | |||
| 646 | thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} received clone() event but GetEventMessage failed " "so we don't know the new pid/tid", thread.GetID()); } while (0); | |||
| 647 | ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER(2147483647)); | |||
| 648 | } else { | |||
| 649 | MonitorClone(thread, event_message, info.si_code >> 8); | |||
| 650 | } | |||
| 651 | ||||
| 652 | break; | |||
| 653 | } | |||
| 654 | ||||
| 655 | case (SIGTRAP5 | (PTRACE_EVENT_EXEC << 8)): { | |||
| 656 | LLDB_LOG(log, "received exec event, code = {0}", info.si_code ^ SIGTRAP)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received exec event, code = {0}", info.si_code ^ 5); } while (0); | |||
| 657 | ||||
| 658 | // Exec clears any pending notifications. | |||
| 659 | m_pending_notification_tid = LLDB_INVALID_THREAD_ID0; | |||
| 660 | ||||
| 661 | // Remove all but the main thread here. Linux fork creates a new process | |||
| 662 | // which only copies the main thread. | |||
| 663 | LLDB_LOG(log, "exec received, stop tracking all but main thread")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "exec received, stop tracking all but main thread" ); } while (0); | |||
| 664 | ||||
| 665 | llvm::erase_if(m_threads, [&](std::unique_ptr<NativeThreadProtocol> &t) { | |||
| 666 | return t->GetID() != GetID(); | |||
| 667 | }); | |||
| 668 | assert(m_threads.size() == 1)(static_cast <bool> (m_threads.size() == 1) ? void (0) : __assert_fail ("m_threads.size() == 1", "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , 668, __extension__ __PRETTY_FUNCTION__)); | |||
| 669 | auto *main_thread = static_cast<NativeThreadLinux *>(m_threads[0].get()); | |||
| 670 | ||||
| 671 | SetCurrentThreadID(main_thread->GetID()); | |||
| 672 | main_thread->SetStoppedByExec(); | |||
| 673 | ||||
| 674 | // Tell coordinator about about the "new" (since exec) stopped main thread. | |||
| 675 | ThreadWasCreated(*main_thread); | |||
| 676 | ||||
| 677 | // Let our delegate know we have just exec'd. | |||
| 678 | NotifyDidExec(); | |||
| 679 | ||||
| 680 | // Let the process know we're stopped. | |||
| 681 | StopRunningThreads(main_thread->GetID()); | |||
| 682 | ||||
| 683 | break; | |||
| 684 | } | |||
| 685 | ||||
| 686 | case (SIGTRAP5 | (PTRACE_EVENT_EXIT << 8)): { | |||
| 687 | // The inferior process or one of its threads is about to exit. We don't | |||
| 688 | // want to do anything with the thread so we just resume it. In case we | |||
| 689 | // want to implement "break on thread exit" functionality, we would need to | |||
| 690 | // stop here. | |||
| 691 | ||||
| 692 | unsigned long data = 0; | |||
| 693 | if (GetEventMessage(thread.GetID(), &data).Fail()) | |||
| 694 | data = -1; | |||
| 695 | ||||
| 696 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received PTRACE_EVENT_EXIT, data = {0:x}, WIFEXITED={1}, " "WIFSIGNALED={2}, pid = {3}, main_thread = {4}", data, (((data ) & 0x7f) == 0), (((signed char) (((data) & 0x7f) + 1 ) >> 1) > 0), thread.GetID(), is_main_thread); } while (0) | |||
| 697 | "received PTRACE_EVENT_EXIT, data = {0:x}, WIFEXITED={1}, "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received PTRACE_EVENT_EXIT, data = {0:x}, WIFEXITED={1}, " "WIFSIGNALED={2}, pid = {3}, main_thread = {4}", data, (((data ) & 0x7f) == 0), (((signed char) (((data) & 0x7f) + 1 ) >> 1) > 0), thread.GetID(), is_main_thread); } while (0) | |||
| 698 | "WIFSIGNALED={2}, pid = {3}, main_thread = {4}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received PTRACE_EVENT_EXIT, data = {0:x}, WIFEXITED={1}, " "WIFSIGNALED={2}, pid = {3}, main_thread = {4}", data, (((data ) & 0x7f) == 0), (((signed char) (((data) & 0x7f) + 1 ) >> 1) > 0), thread.GetID(), is_main_thread); } while (0) | |||
| 699 | data, WIFEXITED(data), WIFSIGNALED(data), thread.GetID(),do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received PTRACE_EVENT_EXIT, data = {0:x}, WIFEXITED={1}, " "WIFSIGNALED={2}, pid = {3}, main_thread = {4}", data, (((data ) & 0x7f) == 0), (((signed char) (((data) & 0x7f) + 1 ) >> 1) > 0), thread.GetID(), is_main_thread); } while (0) | |||
| 700 | is_main_thread)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received PTRACE_EVENT_EXIT, data = {0:x}, WIFEXITED={1}, " "WIFSIGNALED={2}, pid = {3}, main_thread = {4}", data, (((data ) & 0x7f) == 0), (((signed char) (((data) & 0x7f) + 1 ) >> 1) > 0), thread.GetID(), is_main_thread); } while (0); | |||
| 701 | ||||
| 702 | ||||
| 703 | StateType state = thread.GetState(); | |||
| 704 | if (!StateIsRunningState(state)) { | |||
| 705 | // Due to a kernel bug, we may sometimes get this stop after the inferior | |||
| 706 | // gets a SIGKILL. This confuses our state tracking logic in | |||
| 707 | // ResumeThread(), since normally, we should not be receiving any ptrace | |||
| 708 | // events while the inferior is stopped. This makes sure that the | |||
| 709 | // inferior is resumed and exits normally. | |||
| 710 | state = eStateRunning; | |||
| 711 | } | |||
| 712 | ResumeThread(thread, state, LLDB_INVALID_SIGNAL_NUMBER(2147483647)); | |||
| 713 | ||||
| 714 | if (is_main_thread) { | |||
| 715 | // Main thread report the read (WIFEXITED) event only after all threads in | |||
| 716 | // the process exit, so we need to stop tracking it here instead of in | |||
| 717 | // MonitorCallback | |||
| 718 | StopTrackingThread(thread); | |||
| 719 | } | |||
| 720 | ||||
| 721 | break; | |||
| 722 | } | |||
| 723 | ||||
| 724 | case (SIGTRAP5 | (PTRACE_EVENT_VFORK_DONE << 8)): { | |||
| 725 | if (bool(m_enabled_extensions & Extension::vfork)) { | |||
| 726 | thread.SetStoppedByVForkDone(); | |||
| 727 | StopRunningThreads(thread.GetID()); | |||
| 728 | } | |||
| 729 | else | |||
| 730 | ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER(2147483647)); | |||
| 731 | break; | |||
| 732 | } | |||
| 733 | ||||
| 734 | case 0: | |||
| 735 | case TRAP_TRACETRAP_TRACE: // We receive this on single stepping. | |||
| 736 | case TRAP_HWBKPTTRAP_HWBKPT: // We receive this on watchpoint hit | |||
| 737 | { | |||
| 738 | // If a watchpoint was hit, report it | |||
| 739 | uint32_t wp_index; | |||
| 740 | Status error = thread.GetRegisterContext().GetWatchpointHitIndex( | |||
| 741 | wp_index, (uintptr_t)info.si_addr_sifields._sigfault.si_addr); | |||
| 742 | if (error.Fail()) | |||
| 743 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for watchpoint hits, pid = " "{0}, error = {1}", thread.GetID(), error); } while (0) | |||
| 744 | "received error while checking for watchpoint hits, pid = "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for watchpoint hits, pid = " "{0}, error = {1}", thread.GetID(), error); } while (0) | |||
| 745 | "{0}, error = {1}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for watchpoint hits, pid = " "{0}, error = {1}", thread.GetID(), error); } while (0) | |||
| 746 | thread.GetID(), error)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for watchpoint hits, pid = " "{0}, error = {1}", thread.GetID(), error); } while (0); | |||
| 747 | if (wp_index != LLDB_INVALID_INDEX32(4294967295U)) { | |||
| 748 | MonitorWatchpoint(thread, wp_index); | |||
| 749 | break; | |||
| 750 | } | |||
| 751 | ||||
| 752 | // If a breakpoint was hit, report it | |||
| 753 | uint32_t bp_index; | |||
| 754 | error = thread.GetRegisterContext().GetHardwareBreakHitIndex( | |||
| 755 | bp_index, (uintptr_t)info.si_addr_sifields._sigfault.si_addr); | |||
| 756 | if (error.Fail()) | |||
| 757 | LLDB_LOG(log, "received error while checking for hardware "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for hardware " "breakpoint hits, pid = {0}, error = {1}" , thread.GetID(), error); } while (0) | |||
| 758 | "breakpoint hits, pid = {0}, error = {1}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for hardware " "breakpoint hits, pid = {0}, error = {1}" , thread.GetID(), error); } while (0) | |||
| 759 | thread.GetID(), error)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for hardware " "breakpoint hits, pid = {0}, error = {1}" , thread.GetID(), error); } while (0); | |||
| 760 | if (bp_index != LLDB_INVALID_INDEX32(4294967295U)) { | |||
| 761 | MonitorBreakpoint(thread); | |||
| 762 | break; | |||
| 763 | } | |||
| 764 | ||||
| 765 | // Otherwise, report step over | |||
| 766 | MonitorTrace(thread); | |||
| 767 | break; | |||
| 768 | } | |||
| 769 | ||||
| 770 | case SI_KERNELSI_KERNEL: | |||
| 771 | #if defined __mips__ | |||
| 772 | // For mips there is no special signal for watchpoint So we check for | |||
| 773 | // watchpoint in kernel trap | |||
| 774 | { | |||
| 775 | // If a watchpoint was hit, report it | |||
| 776 | uint32_t wp_index; | |||
| 777 | Status error = thread.GetRegisterContext().GetWatchpointHitIndex( | |||
| 778 | wp_index, LLDB_INVALID_ADDRESS(18446744073709551615UL)); | |||
| 779 | if (error.Fail()) | |||
| 780 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for watchpoint hits, pid = " "{0}, error = {1}", thread.GetID(), error); } while (0) | |||
| 781 | "received error while checking for watchpoint hits, pid = "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for watchpoint hits, pid = " "{0}, error = {1}", thread.GetID(), error); } while (0) | |||
| 782 | "{0}, error = {1}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for watchpoint hits, pid = " "{0}, error = {1}", thread.GetID(), error); } while (0) | |||
| 783 | thread.GetID(), error)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received error while checking for watchpoint hits, pid = " "{0}, error = {1}", thread.GetID(), error); } while (0); | |||
| 784 | if (wp_index != LLDB_INVALID_INDEX32(4294967295U)) { | |||
| 785 | MonitorWatchpoint(thread, wp_index); | |||
| 786 | break; | |||
| 787 | } | |||
| 788 | } | |||
| 789 | // NO BREAK | |||
| 790 | #endif | |||
| 791 | case TRAP_BRKPTTRAP_BRKPT: | |||
| 792 | MonitorBreakpoint(thread); | |||
| 793 | break; | |||
| 794 | ||||
| 795 | case SIGTRAP5: | |||
| 796 | case (SIGTRAP5 | 0x80): | |||
| 797 | LLDB_LOG(do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming" , info.si_code, GetID(), thread.GetID()); } while (0) | |||
| 798 | log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming" , info.si_code, GetID(), thread.GetID()); } while (0) | |||
| 799 | "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming" , info.si_code, GetID(), thread.GetID()); } while (0) | |||
| 800 | info.si_code, GetID(), thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming" , info.si_code, GetID(), thread.GetID()); } while (0); | |||
| 801 | ||||
| 802 | // Ignore these signals until we know more about them. | |||
| 803 | ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER(2147483647)); | |||
| 804 | break; | |||
| 805 | ||||
| 806 | default: | |||
| 807 | LLDB_LOG(log, "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}" , info.si_code, GetID(), thread.GetID()); } while (0) | |||
| 808 | info.si_code, GetID(), thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}" , info.si_code, GetID(), thread.GetID()); } while (0); | |||
| 809 | MonitorSignal(info, thread); | |||
| 810 | break; | |||
| 811 | } | |||
| 812 | } | |||
| 813 | ||||
| 814 | void NativeProcessLinux::MonitorTrace(NativeThreadLinux &thread) { | |||
| 815 | Log *log = GetLog(POSIXLog::Process); | |||
| 816 | LLDB_LOG(log, "received trace event, pid = {0}", thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received trace event, pid = {0}", thread.GetID() ); } while (0); | |||
| 817 | ||||
| 818 | // This thread is currently stopped. | |||
| 819 | thread.SetStoppedByTrace(); | |||
| 820 | ||||
| 821 | StopRunningThreads(thread.GetID()); | |||
| 822 | } | |||
| 823 | ||||
| 824 | void NativeProcessLinux::MonitorBreakpoint(NativeThreadLinux &thread) { | |||
| 825 | Log *log = GetLog(LLDBLog::Process | LLDBLog::Breakpoints); | |||
| 826 | LLDB_LOG(log, "received breakpoint event, pid = {0}", thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received breakpoint event, pid = {0}", thread.GetID ()); } while (0); | |||
| 827 | ||||
| 828 | // Mark the thread as stopped at breakpoint. | |||
| 829 | thread.SetStoppedByBreakpoint(); | |||
| 830 | FixupBreakpointPCAsNeeded(thread); | |||
| 831 | ||||
| 832 | if (m_threads_stepping_with_breakpoint.find(thread.GetID()) != | |||
| 833 | m_threads_stepping_with_breakpoint.end()) | |||
| 834 | thread.SetStoppedByTrace(); | |||
| 835 | ||||
| 836 | StopRunningThreads(thread.GetID()); | |||
| 837 | } | |||
| 838 | ||||
| 839 | void NativeProcessLinux::MonitorWatchpoint(NativeThreadLinux &thread, | |||
| 840 | uint32_t wp_index) { | |||
| 841 | Log *log = GetLog(LLDBLog::Process | LLDBLog::Watchpoints); | |||
| 842 | LLDB_LOG(log, "received watchpoint event, pid = {0}, wp_index = {1}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received watchpoint event, pid = {0}, wp_index = {1}" , thread.GetID(), wp_index); } while (0) | |||
| 843 | thread.GetID(), wp_index)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received watchpoint event, pid = {0}, wp_index = {1}" , thread.GetID(), wp_index); } while (0); | |||
| 844 | ||||
| 845 | // Mark the thread as stopped at watchpoint. The address is at | |||
| 846 | // (lldb::addr_t)info->si_addr if we need it. | |||
| 847 | thread.SetStoppedByWatchpoint(wp_index); | |||
| 848 | ||||
| 849 | // We need to tell all other running threads before we notify the delegate | |||
| 850 | // about this stop. | |||
| 851 | StopRunningThreads(thread.GetID()); | |||
| 852 | } | |||
| 853 | ||||
| 854 | void NativeProcessLinux::MonitorSignal(const siginfo_t &info, | |||
| 855 | NativeThreadLinux &thread) { | |||
| 856 | const int signo = info.si_signo; | |||
| 857 | const bool is_from_llgs = info.si_pid_sifields._kill.si_pid == getpid(); | |||
| 858 | ||||
| 859 | Log *log = GetLog(POSIXLog::Process); | |||
| 860 | ||||
| 861 | // POSIX says that process behaviour is undefined after it ignores a SIGFPE, | |||
| 862 | // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a kill(2) | |||
| 863 | // or raise(3). Similarly for tgkill(2) on Linux. | |||
| 864 | // | |||
| 865 | // IOW, user generated signals never generate what we consider to be a | |||
| 866 | // "crash". | |||
| 867 | // | |||
| 868 | // Similarly, ACK signals generated by this monitor. | |||
| 869 | ||||
| 870 | // Handle the signal. | |||
| 871 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, " "waitpid pid = {4})", Host::GetSignalAsCString(signo), signo , info.si_code, thread.GetID()); } while (0) | |||
| 872 | "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, " "waitpid pid = {4})", Host::GetSignalAsCString(signo), signo , info.si_code, thread.GetID()); } while (0) | |||
| 873 | "waitpid pid = {4})",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, " "waitpid pid = {4})", Host::GetSignalAsCString(signo), signo , info.si_code, thread.GetID()); } while (0) | |||
| 874 | Host::GetSignalAsCString(signo), signo, info.si_code,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, " "waitpid pid = {4})", Host::GetSignalAsCString(signo), signo , info.si_code, thread.GetID()); } while (0) | |||
| 875 | thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, " "waitpid pid = {4})", Host::GetSignalAsCString(signo), signo , info.si_code, thread.GetID()); } while (0); | |||
| 876 | ||||
| 877 | // Check for thread stop notification. | |||
| 878 | if (is_from_llgs && (info.si_code == SI_TKILLSI_TKILL) && (signo == SIGSTOP19)) { | |||
| 879 | // This is a tgkill()-based stop. | |||
| 880 | LLDB_LOG(log, "pid {0} tid {1}, thread stopped", GetID(), thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} tid {1}, thread stopped", GetID(), thread .GetID()); } while (0); | |||
| 881 | ||||
| 882 | // Check that we're not already marked with a stop reason. Note this thread | |||
| 883 | // really shouldn't already be marked as stopped - if we were, that would | |||
| 884 | // imply that the kernel signaled us with the thread stopping which we | |||
| 885 | // handled and marked as stopped, and that, without an intervening resume, | |||
| 886 | // we received another stop. It is more likely that we are missing the | |||
| 887 | // marking of a run state somewhere if we find that the thread was marked | |||
| 888 | // as stopped. | |||
| 889 | const StateType thread_state = thread.GetState(); | |||
| 890 | if (!StateIsStoppedState(thread_state, false)) { | |||
| 891 | // An inferior thread has stopped because of a SIGSTOP we have sent it. | |||
| 892 | // Generally, these are not important stops and we don't want to report | |||
| 893 | // them as they are just used to stop other threads when one thread (the | |||
| 894 | // one with the *real* stop reason) hits a breakpoint (watchpoint, | |||
| 895 | // etc...). However, in the case of an asynchronous Interrupt(), this | |||
| 896 | // *is* the real stop reason, so we leave the signal intact if this is | |||
| 897 | // the thread that was chosen as the triggering thread. | |||
| 898 | if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID0) { | |||
| 899 | if (m_pending_notification_tid == thread.GetID()) | |||
| 900 | thread.SetStoppedBySignal(SIGSTOP19, &info); | |||
| 901 | else | |||
| 902 | thread.SetStoppedWithNoReason(); | |||
| 903 | ||||
| 904 | SetCurrentThreadID(thread.GetID()); | |||
| 905 | SignalIfAllThreadsStopped(); | |||
| 906 | } else { | |||
| 907 | // We can end up here if stop was initiated by LLGS but by this time a | |||
| 908 | // thread stop has occurred - maybe initiated by another event. | |||
| 909 | Status error = ResumeThread(thread, thread.GetState(), 0); | |||
| 910 | if (error.Fail()) | |||
| 911 | LLDB_LOG(log, "failed to resume thread {0}: {1}", thread.GetID(),do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "failed to resume thread {0}: {1}", thread.GetID( ), error); } while (0) | |||
| 912 | error)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "failed to resume thread {0}: {1}", thread.GetID( ), error); } while (0); | |||
| 913 | } | |||
| 914 | } else { | |||
| 915 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} tid {1}, thread was already marked as a stopped " "state (state={2}), leaving stop signal as is", GetID(), thread .GetID(), thread_state); } while (0) | |||
| 916 | "pid {0} tid {1}, thread was already marked as a stopped "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} tid {1}, thread was already marked as a stopped " "state (state={2}), leaving stop signal as is", GetID(), thread .GetID(), thread_state); } while (0) | |||
| 917 | "state (state={2}), leaving stop signal as is",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} tid {1}, thread was already marked as a stopped " "state (state={2}), leaving stop signal as is", GetID(), thread .GetID(), thread_state); } while (0) | |||
| 918 | GetID(), thread.GetID(), thread_state)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} tid {1}, thread was already marked as a stopped " "state (state={2}), leaving stop signal as is", GetID(), thread .GetID(), thread_state); } while (0); | |||
| 919 | SignalIfAllThreadsStopped(); | |||
| 920 | } | |||
| 921 | ||||
| 922 | // Done handling. | |||
| 923 | return; | |||
| 924 | } | |||
| 925 | ||||
| 926 | // Check if debugger should stop at this signal or just ignore it and resume | |||
| 927 | // the inferior. | |||
| 928 | if (m_signals_to_ignore.contains(signo)) { | |||
| 929 | ResumeThread(thread, thread.GetState(), signo); | |||
| 930 | return; | |||
| 931 | } | |||
| 932 | ||||
| 933 | // This thread is stopped. | |||
| 934 | LLDB_LOG(log, "received signal {0}", Host::GetSignalAsCString(signo))do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "received signal {0}", Host::GetSignalAsCString(signo )); } while (0); | |||
| 935 | thread.SetStoppedBySignal(signo, &info); | |||
| 936 | ||||
| 937 | // Send a stop to the debugger after we get all other threads to stop. | |||
| 938 | StopRunningThreads(thread.GetID()); | |||
| 939 | } | |||
| 940 | ||||
| 941 | bool NativeProcessLinux::MonitorClone(NativeThreadLinux &parent, | |||
| 942 | lldb::pid_t child_pid, int event) { | |||
| 943 | Log *log = GetLog(POSIXLog::Process); | |||
| 944 | LLDB_LOG(log, "parent_tid={0}, child_pid={1}, event={2}", parent.GetID(),do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "parent_tid={0}, child_pid={1}, event={2}", parent .GetID(), child_pid, event); } while (0) | |||
| 945 | child_pid, event)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "parent_tid={0}, child_pid={1}, event={2}", parent .GetID(), child_pid, event); } while (0); | |||
| 946 | ||||
| 947 | m_manager.CollectThread(child_pid); | |||
| 948 | ||||
| 949 | switch (event) { | |||
| 950 | case PTRACE_EVENT_CLONE: { | |||
| 951 | // PTRACE_EVENT_CLONE can either mean a new thread or a new process. | |||
| 952 | // Try to grab the new process' PGID to figure out which one it is. | |||
| 953 | // If PGID is the same as the PID, then it's a new process. Otherwise, | |||
| 954 | // it's a thread. | |||
| 955 | auto tgid_ret = getPIDForTID(child_pid); | |||
| 956 | if (tgid_ret != child_pid) { | |||
| 957 | // A new thread should have PGID matching our process' PID. | |||
| 958 | assert(!tgid_ret || *tgid_ret == GetID())(static_cast <bool> (!tgid_ret || *tgid_ret == GetID()) ? void (0) : __assert_fail ("!tgid_ret || *tgid_ret == GetID()" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 958, __extension__ __PRETTY_FUNCTION__)); | |||
| 959 | ||||
| 960 | NativeThreadLinux &child_thread = AddThread(child_pid, /*resume*/ true); | |||
| 961 | ThreadWasCreated(child_thread); | |||
| 962 | ||||
| 963 | // Resume the parent. | |||
| 964 | ResumeThread(parent, parent.GetState(), LLDB_INVALID_SIGNAL_NUMBER(2147483647)); | |||
| 965 | break; | |||
| 966 | } | |||
| 967 | } | |||
| 968 | [[fallthrough]]; | |||
| 969 | case PTRACE_EVENT_FORK: | |||
| 970 | case PTRACE_EVENT_VFORK: { | |||
| 971 | bool is_vfork = event == PTRACE_EVENT_VFORK; | |||
| 972 | std::unique_ptr<NativeProcessLinux> child_process{new NativeProcessLinux( | |||
| 973 | static_cast<::pid_t>(child_pid), m_terminal_fd, m_delegate, m_arch, | |||
| 974 | m_manager, {static_cast<::pid_t>(child_pid)})}; | |||
| 975 | if (!is_vfork) | |||
| 976 | child_process->m_software_breakpoints = m_software_breakpoints; | |||
| 977 | ||||
| 978 | Extension expected_ext = is_vfork ? Extension::vfork : Extension::fork; | |||
| 979 | if (bool(m_enabled_extensions & expected_ext)) { | |||
| 980 | m_delegate.NewSubprocess(this, std::move(child_process)); | |||
| 981 | // NB: non-vfork clone() is reported as fork | |||
| 982 | parent.SetStoppedByFork(is_vfork, child_pid); | |||
| 983 | StopRunningThreads(parent.GetID()); | |||
| 984 | } else { | |||
| 985 | child_process->Detach(); | |||
| 986 | ResumeThread(parent, parent.GetState(), LLDB_INVALID_SIGNAL_NUMBER(2147483647)); | |||
| 987 | } | |||
| 988 | break; | |||
| 989 | } | |||
| 990 | default: | |||
| 991 | llvm_unreachable("unknown clone_info.event")::llvm::llvm_unreachable_internal("unknown clone_info.event", "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 991 ); | |||
| 992 | } | |||
| 993 | ||||
| 994 | return true; | |||
| 995 | } | |||
| 996 | ||||
| 997 | bool NativeProcessLinux::SupportHardwareSingleStepping() const { | |||
| 998 | if (m_arch.IsMIPS() || m_arch.GetMachine() == llvm::Triple::arm || | |||
| 999 | m_arch.GetTriple().isRISCV() || m_arch.GetTriple().isLoongArch()) | |||
| 1000 | return false; | |||
| 1001 | return true; | |||
| 1002 | } | |||
| 1003 | ||||
| 1004 | Status NativeProcessLinux::Resume(const ResumeActionList &resume_actions) { | |||
| 1005 | Log *log = GetLog(POSIXLog::Process); | |||
| 1006 | LLDB_LOG(log, "pid {0}", GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0}", GetID()); } while (0); | |||
| 1007 | ||||
| 1008 | NotifyTracersProcessWillResume(); | |||
| 1009 | ||||
| 1010 | bool software_single_step = !SupportHardwareSingleStepping(); | |||
| 1011 | ||||
| 1012 | if (software_single_step) { | |||
| 1013 | for (const auto &thread : m_threads) { | |||
| 1014 | assert(thread && "thread list should not contain NULL threads")(static_cast <bool> (thread && "thread list should not contain NULL threads" ) ? void (0) : __assert_fail ("thread && \"thread list should not contain NULL threads\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1014, __extension__ __PRETTY_FUNCTION__)); | |||
| 1015 | ||||
| 1016 | const ResumeAction *const action = | |||
| 1017 | resume_actions.GetActionForThread(thread->GetID(), true); | |||
| 1018 | if (action == nullptr) | |||
| 1019 | continue; | |||
| 1020 | ||||
| 1021 | if (action->state == eStateStepping) { | |||
| 1022 | Status error = SetupSoftwareSingleStepping( | |||
| 1023 | static_cast<NativeThreadLinux &>(*thread)); | |||
| 1024 | if (error.Fail()) | |||
| 1025 | return error; | |||
| 1026 | } | |||
| 1027 | } | |||
| 1028 | } | |||
| 1029 | ||||
| 1030 | for (const auto &thread : m_threads) { | |||
| 1031 | assert(thread && "thread list should not contain NULL threads")(static_cast <bool> (thread && "thread list should not contain NULL threads" ) ? void (0) : __assert_fail ("thread && \"thread list should not contain NULL threads\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1031, __extension__ __PRETTY_FUNCTION__)); | |||
| 1032 | ||||
| 1033 | const ResumeAction *const action = | |||
| 1034 | resume_actions.GetActionForThread(thread->GetID(), true); | |||
| 1035 | ||||
| 1036 | if (action == nullptr) { | |||
| 1037 | LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(),do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "no action specified for pid {0} tid {1}", GetID( ), thread->GetID()); } while (0) | |||
| 1038 | thread->GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "no action specified for pid {0} tid {1}", GetID( ), thread->GetID()); } while (0); | |||
| 1039 | continue; | |||
| 1040 | } | |||
| 1041 | ||||
| 1042 | LLDB_LOG(log, "processing resume action state {0} for pid {1} tid {2}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "processing resume action state {0} for pid {1} tid {2}" , action->state, GetID(), thread->GetID()); } while (0) | |||
| 1043 | action->state, GetID(), thread->GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "processing resume action state {0} for pid {1} tid {2}" , action->state, GetID(), thread->GetID()); } while (0); | |||
| 1044 | ||||
| 1045 | switch (action->state) { | |||
| 1046 | case eStateRunning: | |||
| 1047 | case eStateStepping: { | |||
| 1048 | // Run the thread, possibly feeding it the signal. | |||
| 1049 | const int signo = action->signal; | |||
| 1050 | Status error = ResumeThread(static_cast<NativeThreadLinux &>(*thread), | |||
| 1051 | action->state, signo); | |||
| 1052 | if (error.Fail()) | |||
| 1053 | return Status("NativeProcessLinux::%s: failed to resume thread " | |||
| 1054 | "for pid %" PRIu64"l" "u" ", tid %" PRIu64"l" "u" ", error = %s", | |||
| 1055 | __FUNCTION__, GetID(), thread->GetID(), | |||
| 1056 | error.AsCString()); | |||
| 1057 | ||||
| 1058 | break; | |||
| 1059 | } | |||
| 1060 | ||||
| 1061 | case eStateSuspended: | |||
| 1062 | case eStateStopped: | |||
| 1063 | break; | |||
| 1064 | ||||
| 1065 | default: | |||
| 1066 | return Status("NativeProcessLinux::%s (): unexpected state %s specified " | |||
| 1067 | "for pid %" PRIu64"l" "u" ", tid %" PRIu64"l" "u", | |||
| 1068 | __FUNCTION__, StateAsCString(action->state), GetID(), | |||
| 1069 | thread->GetID()); | |||
| 1070 | } | |||
| 1071 | } | |||
| 1072 | ||||
| 1073 | return Status(); | |||
| 1074 | } | |||
| 1075 | ||||
| 1076 | Status NativeProcessLinux::Halt() { | |||
| 1077 | Status error; | |||
| 1078 | ||||
| 1079 | if (kill(GetID(), SIGSTOP19) != 0) | |||
| 1080 | error.SetErrorToErrno(); | |||
| 1081 | ||||
| 1082 | return error; | |||
| 1083 | } | |||
| 1084 | ||||
| 1085 | Status NativeProcessLinux::Detach() { | |||
| 1086 | Status error; | |||
| 1087 | ||||
| 1088 | // Tell ptrace to detach from the process. | |||
| 1089 | if (GetID() == LLDB_INVALID_PROCESS_ID0) | |||
| 1090 | return error; | |||
| 1091 | ||||
| 1092 | for (const auto &thread : m_threads) { | |||
| 1093 | Status e = Detach(thread->GetID()); | |||
| 1094 | if (e.Fail()) | |||
| 1095 | error = | |||
| 1096 | e; // Save the error, but still attempt to detach from other threads. | |||
| 1097 | } | |||
| 1098 | ||||
| 1099 | m_intel_pt_collector.Clear(); | |||
| 1100 | ||||
| 1101 | return error; | |||
| 1102 | } | |||
| 1103 | ||||
| 1104 | Status NativeProcessLinux::Signal(int signo) { | |||
| 1105 | Status error; | |||
| 1106 | ||||
| 1107 | Log *log = GetLog(POSIXLog::Process); | |||
| 1108 | LLDB_LOG(log, "sending signal {0} ({1}) to pid {1}", signo,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "sending signal {0} ({1}) to pid {1}", signo, Host ::GetSignalAsCString(signo), GetID()); } while (0) | |||
| 1109 | Host::GetSignalAsCString(signo), GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "sending signal {0} ({1}) to pid {1}", signo, Host ::GetSignalAsCString(signo), GetID()); } while (0); | |||
| 1110 | ||||
| 1111 | if (kill(GetID(), signo)) | |||
| 1112 | error.SetErrorToErrno(); | |||
| 1113 | ||||
| 1114 | return error; | |||
| 1115 | } | |||
| 1116 | ||||
| 1117 | Status NativeProcessLinux::Interrupt() { | |||
| 1118 | // Pick a running thread (or if none, a not-dead stopped thread) as the | |||
| 1119 | // chosen thread that will be the stop-reason thread. | |||
| 1120 | Log *log = GetLog(POSIXLog::Process); | |||
| 1121 | ||||
| 1122 | NativeThreadProtocol *running_thread = nullptr; | |||
| 1123 | NativeThreadProtocol *stopped_thread = nullptr; | |||
| 1124 | ||||
| 1125 | LLDB_LOG(log, "selecting running thread for interrupt target")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "selecting running thread for interrupt target"); } while (0); | |||
| 1126 | for (const auto &thread : m_threads) { | |||
| 1127 | // If we have a running or stepping thread, we'll call that the target of | |||
| 1128 | // the interrupt. | |||
| 1129 | const auto thread_state = thread->GetState(); | |||
| 1130 | if (thread_state == eStateRunning || thread_state == eStateStepping) { | |||
| 1131 | running_thread = thread.get(); | |||
| 1132 | break; | |||
| 1133 | } else if (!stopped_thread && StateIsStoppedState(thread_state, true)) { | |||
| 1134 | // Remember the first non-dead stopped thread. We'll use that as a | |||
| 1135 | // backup if there are no running threads. | |||
| 1136 | stopped_thread = thread.get(); | |||
| 1137 | } | |||
| 1138 | } | |||
| 1139 | ||||
| 1140 | if (!running_thread && !stopped_thread) { | |||
| 1141 | Status error("found no running/stepping or live stopped threads as target " | |||
| 1142 | "for interrupt"); | |||
| 1143 | LLDB_LOG(log, "skipping due to error: {0}", error)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "skipping due to error: {0}", error); } while (0); | |||
| 1144 | ||||
| 1145 | return error; | |||
| 1146 | } | |||
| 1147 | ||||
| 1148 | NativeThreadProtocol *deferred_signal_thread = | |||
| 1149 | running_thread ? running_thread : stopped_thread; | |||
| 1150 | ||||
| 1151 | LLDB_LOG(log, "pid {0} {1} tid {2} chosen for interrupt target", GetID(),do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} {1} tid {2} chosen for interrupt target" , GetID(), running_thread ? "running" : "stopped", deferred_signal_thread ->GetID()); } while (0) | |||
| 1152 | running_thread ? "running" : "stopped",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} {1} tid {2} chosen for interrupt target" , GetID(), running_thread ? "running" : "stopped", deferred_signal_thread ->GetID()); } while (0) | |||
| 1153 | deferred_signal_thread->GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} {1} tid {2} chosen for interrupt target" , GetID(), running_thread ? "running" : "stopped", deferred_signal_thread ->GetID()); } while (0); | |||
| 1154 | ||||
| 1155 | StopRunningThreads(deferred_signal_thread->GetID()); | |||
| 1156 | ||||
| 1157 | return Status(); | |||
| 1158 | } | |||
| 1159 | ||||
| 1160 | Status NativeProcessLinux::Kill() { | |||
| 1161 | Log *log = GetLog(POSIXLog::Process); | |||
| 1162 | LLDB_LOG(log, "pid {0}", GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0}", GetID()); } while (0); | |||
| 1163 | ||||
| 1164 | Status error; | |||
| 1165 | ||||
| 1166 | switch (m_state) { | |||
| 1167 | case StateType::eStateInvalid: | |||
| 1168 | case StateType::eStateExited: | |||
| 1169 | case StateType::eStateCrashed: | |||
| 1170 | case StateType::eStateDetached: | |||
| 1171 | case StateType::eStateUnloaded: | |||
| 1172 | // Nothing to do - the process is already dead. | |||
| 1173 | LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(),do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "ignored for PID {0} due to current state: {1}", GetID (), m_state); } while (0) | |||
| 1174 | m_state)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "ignored for PID {0} due to current state: {1}", GetID (), m_state); } while (0); | |||
| 1175 | return error; | |||
| 1176 | ||||
| 1177 | case StateType::eStateConnected: | |||
| 1178 | case StateType::eStateAttaching: | |||
| 1179 | case StateType::eStateLaunching: | |||
| 1180 | case StateType::eStateStopped: | |||
| 1181 | case StateType::eStateRunning: | |||
| 1182 | case StateType::eStateStepping: | |||
| 1183 | case StateType::eStateSuspended: | |||
| 1184 | // We can try to kill a process in these states. | |||
| 1185 | break; | |||
| 1186 | } | |||
| 1187 | ||||
| 1188 | if (kill(GetID(), SIGKILL9) != 0) { | |||
| 1189 | error.SetErrorToErrno(); | |||
| 1190 | return error; | |||
| 1191 | } | |||
| 1192 | ||||
| 1193 | return error; | |||
| 1194 | } | |||
| 1195 | ||||
| 1196 | Status NativeProcessLinux::GetMemoryRegionInfo(lldb::addr_t load_addr, | |||
| 1197 | MemoryRegionInfo &range_info) { | |||
| 1198 | // FIXME review that the final memory region returned extends to the end of | |||
| 1199 | // the virtual address space, | |||
| 1200 | // with no perms if it is not mapped. | |||
| 1201 | ||||
| 1202 | // Use an approach that reads memory regions from /proc/{pid}/maps. Assume | |||
| 1203 | // proc maps entries are in ascending order. | |||
| 1204 | // FIXME assert if we find differently. | |||
| 1205 | ||||
| 1206 | if (m_supports_mem_region == LazyBool::eLazyBoolNo) { | |||
| 1207 | // We're done. | |||
| 1208 | return Status("unsupported"); | |||
| 1209 | } | |||
| 1210 | ||||
| 1211 | Status error = PopulateMemoryRegionCache(); | |||
| 1212 | if (error.Fail()) { | |||
| 1213 | return error; | |||
| 1214 | } | |||
| 1215 | ||||
| 1216 | lldb::addr_t prev_base_address = 0; | |||
| 1217 | ||||
| 1218 | // FIXME start by finding the last region that is <= target address using | |||
| 1219 | // binary search. Data is sorted. | |||
| 1220 | // There can be a ton of regions on pthreads apps with lots of threads. | |||
| 1221 | for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end(); | |||
| 1222 | ++it) { | |||
| 1223 | MemoryRegionInfo &proc_entry_info = it->first; | |||
| 1224 | ||||
| 1225 | // Sanity check assumption that /proc/{pid}/maps entries are ascending. | |||
| 1226 | assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) &&(static_cast <bool> ((proc_entry_info.GetRange().GetRangeBase () >= prev_base_address) && "descending /proc/pid/maps entries detected, unexpected" ) ? void (0) : __assert_fail ("(proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && \"descending /proc/pid/maps entries detected, unexpected\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1227, __extension__ __PRETTY_FUNCTION__)) | |||
| 1227 | "descending /proc/pid/maps entries detected, unexpected")(static_cast <bool> ((proc_entry_info.GetRange().GetRangeBase () >= prev_base_address) && "descending /proc/pid/maps entries detected, unexpected" ) ? void (0) : __assert_fail ("(proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && \"descending /proc/pid/maps entries detected, unexpected\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1227, __extension__ __PRETTY_FUNCTION__)); | |||
| 1228 | prev_base_address = proc_entry_info.GetRange().GetRangeBase(); | |||
| 1229 | UNUSED_IF_ASSERT_DISABLED(prev_base_address)((void)(prev_base_address)); | |||
| 1230 | ||||
| 1231 | // If the target address comes before this entry, indicate distance to next | |||
| 1232 | // region. | |||
| 1233 | if (load_addr < proc_entry_info.GetRange().GetRangeBase()) { | |||
| 1234 | range_info.GetRange().SetRangeBase(load_addr); | |||
| 1235 | range_info.GetRange().SetByteSize( | |||
| 1236 | proc_entry_info.GetRange().GetRangeBase() - load_addr); | |||
| 1237 | range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); | |||
| 1238 | range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); | |||
| 1239 | range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); | |||
| 1240 | range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); | |||
| 1241 | ||||
| 1242 | return error; | |||
| 1243 | } else if (proc_entry_info.GetRange().Contains(load_addr)) { | |||
| 1244 | // The target address is within the memory region we're processing here. | |||
| 1245 | range_info = proc_entry_info; | |||
| 1246 | return error; | |||
| 1247 | } | |||
| 1248 | ||||
| 1249 | // The target memory address comes somewhere after the region we just | |||
| 1250 | // parsed. | |||
| 1251 | } | |||
| 1252 | ||||
| 1253 | // If we made it here, we didn't find an entry that contained the given | |||
| 1254 | // address. Return the load_addr as start and the amount of bytes betwwen | |||
| 1255 | // load address and the end of the memory as size. | |||
| 1256 | range_info.GetRange().SetRangeBase(load_addr); | |||
| 1257 | range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS(18446744073709551615UL)); | |||
| 1258 | range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); | |||
| 1259 | range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); | |||
| 1260 | range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); | |||
| 1261 | range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); | |||
| 1262 | return error; | |||
| 1263 | } | |||
| 1264 | ||||
| 1265 | Status NativeProcessLinux::PopulateMemoryRegionCache() { | |||
| 1266 | Log *log = GetLog(POSIXLog::Process); | |||
| 1267 | ||||
| 1268 | // If our cache is empty, pull the latest. There should always be at least | |||
| 1269 | // one memory region if memory region handling is supported. | |||
| 1270 | if (!m_mem_region_cache.empty()) { | |||
| 1271 | LLDB_LOG(log, "reusing {0} cached memory region entries",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "reusing {0} cached memory region entries", m_mem_region_cache .size()); } while (0) | |||
| 1272 | m_mem_region_cache.size())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "reusing {0} cached memory region entries", m_mem_region_cache .size()); } while (0); | |||
| 1273 | return Status(); | |||
| 1274 | } | |||
| 1275 | ||||
| 1276 | Status Result; | |||
| 1277 | LinuxMapCallback callback = [&](llvm::Expected<MemoryRegionInfo> Info) { | |||
| 1278 | if (Info) { | |||
| 1279 | FileSpec file_spec(Info->GetName().GetCString()); | |||
| 1280 | FileSystem::Instance().Resolve(file_spec); | |||
| 1281 | m_mem_region_cache.emplace_back(*Info, file_spec); | |||
| 1282 | return true; | |||
| 1283 | } | |||
| 1284 | ||||
| 1285 | Result = Info.takeError(); | |||
| 1286 | m_supports_mem_region = LazyBool::eLazyBoolNo; | |||
| 1287 | LLDB_LOG(log, "failed to parse proc maps: {0}", Result)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "failed to parse proc maps: {0}", Result); } while (0); | |||
| 1288 | return false; | |||
| 1289 | }; | |||
| 1290 | ||||
| 1291 | // Linux kernel since 2.6.14 has /proc/{pid}/smaps | |||
| 1292 | // if CONFIG_PROC_PAGE_MONITOR is enabled | |||
| 1293 | auto BufferOrError = getProcFile(GetID(), GetCurrentThreadID(), "smaps"); | |||
| 1294 | if (BufferOrError) | |||
| 1295 | ParseLinuxSMapRegions(BufferOrError.get()->getBuffer(), callback); | |||
| 1296 | else { | |||
| 1297 | BufferOrError = getProcFile(GetID(), GetCurrentThreadID(), "maps"); | |||
| 1298 | if (!BufferOrError) { | |||
| 1299 | m_supports_mem_region = LazyBool::eLazyBoolNo; | |||
| 1300 | return BufferOrError.getError(); | |||
| 1301 | } | |||
| 1302 | ||||
| 1303 | ParseLinuxMapRegions(BufferOrError.get()->getBuffer(), callback); | |||
| 1304 | } | |||
| 1305 | ||||
| 1306 | if (Result.Fail()) | |||
| 1307 | return Result; | |||
| 1308 | ||||
| 1309 | if (m_mem_region_cache.empty()) { | |||
| 1310 | // No entries after attempting to read them. This shouldn't happen if | |||
| 1311 | // /proc/{pid}/maps is supported. Assume we don't support map entries via | |||
| 1312 | // procfs. | |||
| 1313 | m_supports_mem_region = LazyBool::eLazyBoolNo; | |||
| 1314 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "failed to find any procfs maps entries, assuming no support " "for memory region metadata retrieval"); } while (0) | |||
| 1315 | "failed to find any procfs maps entries, assuming no support "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "failed to find any procfs maps entries, assuming no support " "for memory region metadata retrieval"); } while (0) | |||
| 1316 | "for memory region metadata retrieval")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "failed to find any procfs maps entries, assuming no support " "for memory region metadata retrieval"); } while (0); | |||
| 1317 | return Status("not supported"); | |||
| 1318 | } | |||
| 1319 | ||||
| 1320 | LLDB_LOG(log, "read {0} memory region entries from /proc/{1}/maps",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "read {0} memory region entries from /proc/{1}/maps" , m_mem_region_cache.size(), GetID()); } while (0) | |||
| 1321 | m_mem_region_cache.size(), GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "read {0} memory region entries from /proc/{1}/maps" , m_mem_region_cache.size(), GetID()); } while (0); | |||
| 1322 | ||||
| 1323 | // We support memory retrieval, remember that. | |||
| 1324 | m_supports_mem_region = LazyBool::eLazyBoolYes; | |||
| 1325 | return Status(); | |||
| 1326 | } | |||
| 1327 | ||||
| 1328 | void NativeProcessLinux::DoStopIDBumped(uint32_t newBumpId) { | |||
| 1329 | Log *log = GetLog(POSIXLog::Process); | |||
| 1330 | LLDB_LOG(log, "newBumpId={0}", newBumpId)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "newBumpId={0}", newBumpId); } while (0); | |||
| 1331 | LLDB_LOG(log, "clearing {0} entries from memory region cache",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "clearing {0} entries from memory region cache", m_mem_region_cache .size()); } while (0) | |||
| 1332 | m_mem_region_cache.size())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "clearing {0} entries from memory region cache", m_mem_region_cache .size()); } while (0); | |||
| 1333 | m_mem_region_cache.clear(); | |||
| 1334 | } | |||
| 1335 | ||||
| 1336 | llvm::Expected<uint64_t> | |||
| 1337 | NativeProcessLinux::Syscall(llvm::ArrayRef<uint64_t> args) { | |||
| 1338 | PopulateMemoryRegionCache(); | |||
| 1339 | auto region_it = llvm::find_if(m_mem_region_cache, [](const auto &pair) { | |||
| 1340 | return pair.first.GetExecutable() == MemoryRegionInfo::eYes && | |||
| 1341 | pair.first.GetShared() != MemoryRegionInfo::eYes; | |||
| 1342 | }); | |||
| 1343 | if (region_it == m_mem_region_cache.end()) | |||
| 1344 | return llvm::createStringError(llvm::inconvertibleErrorCode(), | |||
| 1345 | "No executable memory region found!"); | |||
| 1346 | ||||
| 1347 | addr_t exe_addr = region_it->first.GetRange().GetRangeBase(); | |||
| 1348 | ||||
| 1349 | NativeThreadLinux &thread = *GetCurrentThread(); | |||
| 1350 | assert(thread.GetState() == eStateStopped)(static_cast <bool> (thread.GetState() == eStateStopped ) ? void (0) : __assert_fail ("thread.GetState() == eStateStopped" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1350, __extension__ __PRETTY_FUNCTION__)); | |||
| 1351 | NativeRegisterContextLinux ®_ctx = thread.GetRegisterContext(); | |||
| 1352 | ||||
| 1353 | NativeRegisterContextLinux::SyscallData syscall_data = | |||
| 1354 | *reg_ctx.GetSyscallData(); | |||
| 1355 | ||||
| 1356 | WritableDataBufferSP registers_sp; | |||
| 1357 | if (llvm::Error Err = reg_ctx.ReadAllRegisterValues(registers_sp).ToError()) | |||
| 1358 | return std::move(Err); | |||
| 1359 | auto restore_regs = llvm::make_scope_exit( | |||
| 1360 | [&] { reg_ctx.WriteAllRegisterValues(registers_sp); }); | |||
| 1361 | ||||
| 1362 | llvm::SmallVector<uint8_t, 8> memory(syscall_data.Insn.size()); | |||
| 1363 | size_t bytes_read; | |||
| 1364 | if (llvm::Error Err = | |||
| 1365 | ReadMemory(exe_addr, memory.data(), memory.size(), bytes_read) | |||
| 1366 | .ToError()) { | |||
| 1367 | return std::move(Err); | |||
| 1368 | } | |||
| 1369 | ||||
| 1370 | auto restore_mem = llvm::make_scope_exit( | |||
| 1371 | [&] { WriteMemory(exe_addr, memory.data(), memory.size(), bytes_read); }); | |||
| 1372 | ||||
| 1373 | if (llvm::Error Err = reg_ctx.SetPC(exe_addr).ToError()) | |||
| 1374 | return std::move(Err); | |||
| 1375 | ||||
| 1376 | for (const auto &zip : llvm::zip_first(args, syscall_data.Args)) { | |||
| 1377 | if (llvm::Error Err = | |||
| 1378 | reg_ctx | |||
| 1379 | .WriteRegisterFromUnsigned(std::get<1>(zip), std::get<0>(zip)) | |||
| 1380 | .ToError()) { | |||
| 1381 | return std::move(Err); | |||
| 1382 | } | |||
| 1383 | } | |||
| 1384 | if (llvm::Error Err = WriteMemory(exe_addr, syscall_data.Insn.data(), | |||
| 1385 | syscall_data.Insn.size(), bytes_read) | |||
| 1386 | .ToError()) | |||
| 1387 | return std::move(Err); | |||
| 1388 | ||||
| 1389 | m_mem_region_cache.clear(); | |||
| 1390 | ||||
| 1391 | // With software single stepping the syscall insn buffer must also include a | |||
| 1392 | // trap instruction to stop the process. | |||
| 1393 | int req = SupportHardwareSingleStepping() ? PTRACE_SINGLESTEP : PTRACE_CONT; | |||
| 1394 | if (llvm::Error Err = | |||
| 1395 | PtraceWrapper(req, thread.GetID(), nullptr, nullptr).ToError()) | |||
| 1396 | return std::move(Err); | |||
| 1397 | ||||
| 1398 | int status; | |||
| 1399 | ::pid_t wait_pid = llvm::sys::RetryAfterSignal(-1, ::waitpid, thread.GetID(), | |||
| 1400 | &status, __WALL0x40000000); | |||
| 1401 | if (wait_pid == -1) { | |||
| 1402 | return llvm::errorCodeToError( | |||
| 1403 | std::error_code(errno(*__errno_location ()), std::generic_category())); | |||
| 1404 | } | |||
| 1405 | assert((unsigned)wait_pid == thread.GetID())(static_cast <bool> ((unsigned)wait_pid == thread.GetID ()) ? void (0) : __assert_fail ("(unsigned)wait_pid == thread.GetID()" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1405, __extension__ __PRETTY_FUNCTION__)); | |||
| 1406 | ||||
| 1407 | uint64_t result = reg_ctx.ReadRegisterAsUnsigned(syscall_data.Result, -ESRCH3); | |||
| 1408 | ||||
| 1409 | // Values larger than this are actually negative errno numbers. | |||
| 1410 | uint64_t errno_threshold = | |||
| 1411 | (uint64_t(-1) >> (64 - 8 * m_arch.GetAddressByteSize())) - 0x1000; | |||
| 1412 | if (result > errno_threshold) { | |||
| 1413 | return llvm::errorCodeToError( | |||
| 1414 | std::error_code(-result & 0xfff, std::generic_category())); | |||
| 1415 | } | |||
| 1416 | ||||
| 1417 | return result; | |||
| 1418 | } | |||
| 1419 | ||||
| 1420 | llvm::Expected<addr_t> | |||
| 1421 | NativeProcessLinux::AllocateMemory(size_t size, uint32_t permissions) { | |||
| 1422 | ||||
| 1423 | std::optional<NativeRegisterContextLinux::MmapData> mmap_data = | |||
| 1424 | GetCurrentThread()->GetRegisterContext().GetMmapData(); | |||
| 1425 | if (!mmap_data) | |||
| 1426 | return llvm::make_error<UnimplementedError>(); | |||
| 1427 | ||||
| 1428 | unsigned prot = PROT_NONE0x0; | |||
| 1429 | assert((permissions & (ePermissionsReadable | ePermissionsWritable |(static_cast <bool> ((permissions & (ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable)) == permissions && "Unknown permission!") ? void (0) : __assert_fail ("(permissions & (ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable)) == permissions && \"Unknown permission!\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1431, __extension__ __PRETTY_FUNCTION__)) | |||
| 1430 | ePermissionsExecutable)) == permissions &&(static_cast <bool> ((permissions & (ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable)) == permissions && "Unknown permission!") ? void (0) : __assert_fail ("(permissions & (ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable)) == permissions && \"Unknown permission!\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1431, __extension__ __PRETTY_FUNCTION__)) | |||
| 1431 | "Unknown permission!")(static_cast <bool> ((permissions & (ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable)) == permissions && "Unknown permission!") ? void (0) : __assert_fail ("(permissions & (ePermissionsReadable | ePermissionsWritable | ePermissionsExecutable)) == permissions && \"Unknown permission!\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1431, __extension__ __PRETTY_FUNCTION__)); | |||
| 1432 | if (permissions & ePermissionsReadable) | |||
| 1433 | prot |= PROT_READ0x1; | |||
| 1434 | if (permissions & ePermissionsWritable) | |||
| 1435 | prot |= PROT_WRITE0x2; | |||
| 1436 | if (permissions & ePermissionsExecutable) | |||
| 1437 | prot |= PROT_EXEC0x4; | |||
| 1438 | ||||
| 1439 | llvm::Expected<uint64_t> Result = | |||
| 1440 | Syscall({mmap_data->SysMmap, 0, size, prot, MAP_ANONYMOUS0x20 | MAP_PRIVATE0x02, | |||
| 1441 | uint64_t(-1), 0}); | |||
| 1442 | if (Result) | |||
| 1443 | m_allocated_memory.try_emplace(*Result, size); | |||
| 1444 | return Result; | |||
| 1445 | } | |||
| 1446 | ||||
| 1447 | llvm::Error NativeProcessLinux::DeallocateMemory(lldb::addr_t addr) { | |||
| 1448 | std::optional<NativeRegisterContextLinux::MmapData> mmap_data = | |||
| 1449 | GetCurrentThread()->GetRegisterContext().GetMmapData(); | |||
| 1450 | if (!mmap_data) | |||
| 1451 | return llvm::make_error<UnimplementedError>(); | |||
| 1452 | ||||
| 1453 | auto it = m_allocated_memory.find(addr); | |||
| 1454 | if (it == m_allocated_memory.end()) | |||
| 1455 | return llvm::createStringError(llvm::errc::invalid_argument, | |||
| 1456 | "Memory not allocated by the debugger."); | |||
| 1457 | ||||
| 1458 | llvm::Expected<uint64_t> Result = | |||
| 1459 | Syscall({mmap_data->SysMunmap, addr, it->second}); | |||
| 1460 | if (!Result) | |||
| 1461 | return Result.takeError(); | |||
| 1462 | ||||
| 1463 | m_allocated_memory.erase(it); | |||
| 1464 | return llvm::Error::success(); | |||
| 1465 | } | |||
| 1466 | ||||
| 1467 | Status NativeProcessLinux::ReadMemoryTags(int32_t type, lldb::addr_t addr, | |||
| 1468 | size_t len, | |||
| 1469 | std::vector<uint8_t> &tags) { | |||
| 1470 | llvm::Expected<NativeRegisterContextLinux::MemoryTaggingDetails> details = | |||
| 1471 | GetCurrentThread()->GetRegisterContext().GetMemoryTaggingDetails(type); | |||
| 1472 | if (!details) | |||
| 1473 | return Status(details.takeError()); | |||
| 1474 | ||||
| 1475 | // Ignore 0 length read | |||
| 1476 | if (!len) | |||
| 1477 | return Status(); | |||
| 1478 | ||||
| 1479 | // lldb will align the range it requests but it is not required to by | |||
| 1480 | // the protocol so we'll do it again just in case. | |||
| 1481 | // Remove tag bits too. Ptrace calls may work regardless but that | |||
| 1482 | // is not a guarantee. | |||
| 1483 | MemoryTagManager::TagRange range(details->manager->RemoveTagBits(addr), len); | |||
| 1484 | range = details->manager->ExpandToGranule(range); | |||
| 1485 | ||||
| 1486 | // Allocate enough space for all tags to be read | |||
| 1487 | size_t num_tags = range.GetByteSize() / details->manager->GetGranuleSize(); | |||
| 1488 | tags.resize(num_tags * details->manager->GetTagSizeInBytes()); | |||
| 1489 | ||||
| 1490 | struct iovec tags_iovec; | |||
| 1491 | uint8_t *dest = tags.data(); | |||
| 1492 | lldb::addr_t read_addr = range.GetRangeBase(); | |||
| 1493 | ||||
| 1494 | // This call can return partial data so loop until we error or | |||
| 1495 | // get all tags back. | |||
| 1496 | while (num_tags) { | |||
| 1497 | tags_iovec.iov_base = dest; | |||
| 1498 | tags_iovec.iov_len = num_tags; | |||
| 1499 | ||||
| 1500 | Status error = NativeProcessLinux::PtraceWrapper( | |||
| 1501 | details->ptrace_read_req, GetCurrentThreadID(), | |||
| 1502 | reinterpret_cast<void *>(read_addr), static_cast<void *>(&tags_iovec), | |||
| 1503 | 0, nullptr); | |||
| 1504 | ||||
| 1505 | if (error.Fail()) { | |||
| 1506 | // Discard partial reads | |||
| 1507 | tags.resize(0); | |||
| 1508 | return error; | |||
| 1509 | } | |||
| 1510 | ||||
| 1511 | size_t tags_read = tags_iovec.iov_len; | |||
| 1512 | assert(tags_read && (tags_read <= num_tags))(static_cast <bool> (tags_read && (tags_read <= num_tags)) ? void (0) : __assert_fail ("tags_read && (tags_read <= num_tags)" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1512, __extension__ __PRETTY_FUNCTION__)); | |||
| 1513 | ||||
| 1514 | dest += tags_read * details->manager->GetTagSizeInBytes(); | |||
| 1515 | read_addr += details->manager->GetGranuleSize() * tags_read; | |||
| 1516 | num_tags -= tags_read; | |||
| 1517 | } | |||
| 1518 | ||||
| 1519 | return Status(); | |||
| 1520 | } | |||
| 1521 | ||||
| 1522 | Status NativeProcessLinux::WriteMemoryTags(int32_t type, lldb::addr_t addr, | |||
| 1523 | size_t len, | |||
| 1524 | const std::vector<uint8_t> &tags) { | |||
| 1525 | llvm::Expected<NativeRegisterContextLinux::MemoryTaggingDetails> details = | |||
| 1526 | GetCurrentThread()->GetRegisterContext().GetMemoryTaggingDetails(type); | |||
| 1527 | if (!details) | |||
| ||||
| 1528 | return Status(details.takeError()); | |||
| 1529 | ||||
| 1530 | // Ignore 0 length write | |||
| 1531 | if (!len) | |||
| 1532 | return Status(); | |||
| 1533 | ||||
| 1534 | // lldb will align the range it requests but it is not required to by | |||
| 1535 | // the protocol so we'll do it again just in case. | |||
| 1536 | // Remove tag bits too. Ptrace calls may work regardless but that | |||
| 1537 | // is not a guarantee. | |||
| 1538 | MemoryTagManager::TagRange range(details->manager->RemoveTagBits(addr), len); | |||
| 1539 | range = details->manager->ExpandToGranule(range); | |||
| 1540 | ||||
| 1541 | // Not checking number of tags here, we may repeat them below | |||
| 1542 | llvm::Expected<std::vector<lldb::addr_t>> unpacked_tags_or_err = | |||
| 1543 | details->manager->UnpackTagsData(tags); | |||
| 1544 | if (!unpacked_tags_or_err) | |||
| 1545 | return Status(unpacked_tags_or_err.takeError()); | |||
| 1546 | ||||
| 1547 | llvm::Expected<std::vector<lldb::addr_t>> repeated_tags_or_err = | |||
| 1548 | details->manager->RepeatTagsForRange(*unpacked_tags_or_err, range); | |||
| 1549 | if (!repeated_tags_or_err) | |||
| 1550 | return Status(repeated_tags_or_err.takeError()); | |||
| 1551 | ||||
| 1552 | // Repack them for ptrace to use | |||
| 1553 | llvm::Expected<std::vector<uint8_t>> final_tag_data = | |||
| 1554 | details->manager->PackTags(*repeated_tags_or_err); | |||
| 1555 | if (!final_tag_data) | |||
| 1556 | return Status(final_tag_data.takeError()); | |||
| 1557 | ||||
| 1558 | struct iovec tags_vec; | |||
| 1559 | uint8_t *src = final_tag_data->data(); | |||
| 1560 | lldb::addr_t write_addr = range.GetRangeBase(); | |||
| 1561 | // unpacked tags size because the number of bytes per tag might not be 1 | |||
| 1562 | size_t num_tags = repeated_tags_or_err->size(); | |||
| 1563 | ||||
| 1564 | // This call can partially write tags, so we loop until we | |||
| 1565 | // error or all tags have been written. | |||
| 1566 | while (num_tags > 0) { | |||
| 1567 | tags_vec.iov_base = src; | |||
| 1568 | tags_vec.iov_len = num_tags; | |||
| 1569 | ||||
| 1570 | Status error = NativeProcessLinux::PtraceWrapper( | |||
| 1571 | details->ptrace_write_req, GetCurrentThreadID(), | |||
| 1572 | reinterpret_cast<void *>(write_addr), static_cast<void *>(&tags_vec), 0, | |||
| 1573 | nullptr); | |||
| 1574 | ||||
| 1575 | if (error.Fail()) { | |||
| 1576 | // Don't attempt to restore the original values in the case of a partial | |||
| 1577 | // write | |||
| 1578 | return error; | |||
| 1579 | } | |||
| 1580 | ||||
| 1581 | size_t tags_written = tags_vec.iov_len; | |||
| 1582 | assert(tags_written && (tags_written <= num_tags))(static_cast <bool> (tags_written && (tags_written <= num_tags)) ? void (0) : __assert_fail ("tags_written && (tags_written <= num_tags)" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1582, __extension__ __PRETTY_FUNCTION__)); | |||
| 1583 | ||||
| 1584 | src += tags_written * details->manager->GetTagSizeInBytes(); | |||
| 1585 | write_addr += details->manager->GetGranuleSize() * tags_written; | |||
| 1586 | num_tags -= tags_written; | |||
| 1587 | } | |||
| 1588 | ||||
| 1589 | return Status(); | |||
| 1590 | } | |||
| 1591 | ||||
| 1592 | size_t NativeProcessLinux::UpdateThreads() { | |||
| 1593 | // The NativeProcessLinux monitoring threads are always up to date with | |||
| 1594 | // respect to thread state and they keep the thread list populated properly. | |||
| 1595 | // All this method needs to do is return the thread count. | |||
| 1596 | return m_threads.size(); | |||
| 1597 | } | |||
| 1598 | ||||
| 1599 | Status NativeProcessLinux::SetBreakpoint(lldb::addr_t addr, uint32_t size, | |||
| 1600 | bool hardware) { | |||
| 1601 | if (hardware) | |||
| 1602 | return SetHardwareBreakpoint(addr, size); | |||
| 1603 | else | |||
| 1604 | return SetSoftwareBreakpoint(addr, size); | |||
| 1605 | } | |||
| 1606 | ||||
| 1607 | Status NativeProcessLinux::RemoveBreakpoint(lldb::addr_t addr, bool hardware) { | |||
| 1608 | if (hardware) | |||
| 1609 | return RemoveHardwareBreakpoint(addr); | |||
| 1610 | else | |||
| 1611 | return NativeProcessProtocol::RemoveBreakpoint(addr); | |||
| 1612 | } | |||
| 1613 | ||||
| 1614 | llvm::Expected<llvm::ArrayRef<uint8_t>> | |||
| 1615 | NativeProcessLinux::GetSoftwareBreakpointTrapOpcode(size_t size_hint) { | |||
| 1616 | // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the | |||
| 1617 | // linux kernel does otherwise. | |||
| 1618 | static const uint8_t g_arm_opcode[] = {0xf0, 0x01, 0xf0, 0xe7}; | |||
| 1619 | static const uint8_t g_thumb_opcode[] = {0x01, 0xde}; | |||
| 1620 | ||||
| 1621 | switch (GetArchitecture().GetMachine()) { | |||
| 1622 | case llvm::Triple::arm: | |||
| 1623 | switch (size_hint) { | |||
| 1624 | case 2: | |||
| 1625 | return llvm::ArrayRef(g_thumb_opcode); | |||
| 1626 | case 4: | |||
| 1627 | return llvm::ArrayRef(g_arm_opcode); | |||
| 1628 | default: | |||
| 1629 | return llvm::createStringError(llvm::inconvertibleErrorCode(), | |||
| 1630 | "Unrecognised trap opcode size hint!"); | |||
| 1631 | } | |||
| 1632 | default: | |||
| 1633 | return NativeProcessProtocol::GetSoftwareBreakpointTrapOpcode(size_hint); | |||
| 1634 | } | |||
| 1635 | } | |||
| 1636 | ||||
| 1637 | Status NativeProcessLinux::ReadMemory(lldb::addr_t addr, void *buf, size_t size, | |||
| 1638 | size_t &bytes_read) { | |||
| 1639 | if (ProcessVmReadvSupported()) { | |||
| 1640 | // The process_vm_readv path is about 50 times faster than ptrace api. We | |||
| 1641 | // want to use this syscall if it is supported. | |||
| 1642 | ||||
| 1643 | struct iovec local_iov, remote_iov; | |||
| 1644 | local_iov.iov_base = buf; | |||
| 1645 | local_iov.iov_len = size; | |||
| 1646 | remote_iov.iov_base = reinterpret_cast<void *>(addr); | |||
| 1647 | remote_iov.iov_len = size; | |||
| 1648 | ||||
| 1649 | bytes_read = process_vm_readv(GetCurrentThreadID(), &local_iov, 1, | |||
| 1650 | &remote_iov, 1, 0); | |||
| 1651 | const bool success = bytes_read == size; | |||
| 1652 | ||||
| 1653 | Log *log = GetLog(POSIXLog::Process); | |||
| 1654 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "using process_vm_readv to read {0} bytes from inferior " "address {1:x}: {2}", size, addr, success ? "Success" : llvm ::sys::StrError((*__errno_location ()))); } while (0) | |||
| 1655 | "using process_vm_readv to read {0} bytes from inferior "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "using process_vm_readv to read {0} bytes from inferior " "address {1:x}: {2}", size, addr, success ? "Success" : llvm ::sys::StrError((*__errno_location ()))); } while (0) | |||
| 1656 | "address {1:x}: {2}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "using process_vm_readv to read {0} bytes from inferior " "address {1:x}: {2}", size, addr, success ? "Success" : llvm ::sys::StrError((*__errno_location ()))); } while (0) | |||
| 1657 | size, addr, success ? "Success" : llvm::sys::StrError(errno))do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "using process_vm_readv to read {0} bytes from inferior " "address {1:x}: {2}", size, addr, success ? "Success" : llvm ::sys::StrError((*__errno_location ()))); } while (0); | |||
| 1658 | ||||
| 1659 | if (success) | |||
| 1660 | return Status(); | |||
| 1661 | // else the call failed for some reason, let's retry the read using ptrace | |||
| 1662 | // api. | |||
| 1663 | } | |||
| 1664 | ||||
| 1665 | unsigned char *dst = static_cast<unsigned char *>(buf); | |||
| 1666 | size_t remainder; | |||
| 1667 | long data; | |||
| 1668 | ||||
| 1669 | Log *log = GetLog(POSIXLog::Memory); | |||
| 1670 | LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "addr = {0}, buf = {1}, size = {2}", addr, buf, size ); } while (0); | |||
| 1671 | ||||
| 1672 | for (bytes_read = 0; bytes_read < size; bytes_read += remainder) { | |||
| 1673 | Status error = NativeProcessLinux::PtraceWrapper( | |||
| 1674 | PTRACE_PEEKDATA, GetCurrentThreadID(), (void *)addr, nullptr, 0, &data); | |||
| 1675 | if (error.Fail()) | |||
| 1676 | return error; | |||
| 1677 | ||||
| 1678 | remainder = size - bytes_read; | |||
| 1679 | remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; | |||
| 1680 | ||||
| 1681 | // Copy the data into our buffer | |||
| 1682 | memcpy(dst, &data, remainder); | |||
| 1683 | ||||
| 1684 | LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "[{0:x}]:{1:x}", addr, data); } while (0); | |||
| 1685 | addr += k_ptrace_word_size; | |||
| 1686 | dst += k_ptrace_word_size; | |||
| 1687 | } | |||
| 1688 | return Status(); | |||
| 1689 | } | |||
| 1690 | ||||
| 1691 | Status NativeProcessLinux::WriteMemory(lldb::addr_t addr, const void *buf, | |||
| 1692 | size_t size, size_t &bytes_written) { | |||
| 1693 | const unsigned char *src = static_cast<const unsigned char *>(buf); | |||
| 1694 | size_t remainder; | |||
| 1695 | Status error; | |||
| 1696 | ||||
| 1697 | Log *log = GetLog(POSIXLog::Memory); | |||
| 1698 | LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "addr = {0}, buf = {1}, size = {2}", addr, buf, size ); } while (0); | |||
| 1699 | ||||
| 1700 | for (bytes_written = 0; bytes_written < size; bytes_written += remainder) { | |||
| 1701 | remainder = size - bytes_written; | |||
| 1702 | remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; | |||
| 1703 | ||||
| 1704 | if (remainder == k_ptrace_word_size) { | |||
| 1705 | unsigned long data = 0; | |||
| 1706 | memcpy(&data, src, k_ptrace_word_size); | |||
| 1707 | ||||
| 1708 | LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "[{0:x}]:{1:x}", addr, data); } while (0); | |||
| 1709 | error = NativeProcessLinux::PtraceWrapper( | |||
| 1710 | PTRACE_POKEDATA, GetCurrentThreadID(), (void *)addr, (void *)data); | |||
| 1711 | if (error.Fail()) | |||
| 1712 | return error; | |||
| 1713 | } else { | |||
| 1714 | unsigned char buff[8]; | |||
| 1715 | size_t bytes_read; | |||
| 1716 | error = ReadMemory(addr, buff, k_ptrace_word_size, bytes_read); | |||
| 1717 | if (error.Fail()) | |||
| 1718 | return error; | |||
| 1719 | ||||
| 1720 | memcpy(buff, src, remainder); | |||
| 1721 | ||||
| 1722 | size_t bytes_written_rec; | |||
| 1723 | error = WriteMemory(addr, buff, k_ptrace_word_size, bytes_written_rec); | |||
| 1724 | if (error.Fail()) | |||
| 1725 | return error; | |||
| 1726 | ||||
| 1727 | LLDB_LOG(log, "[{0:x}]:{1:x} ({2:x})", addr, *(const unsigned long *)src,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "[{0:x}]:{1:x} ({2:x})", addr, *(const unsigned long *)src, *(unsigned long *)buff); } while (0) | |||
| 1728 | *(unsigned long *)buff)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "[{0:x}]:{1:x} ({2:x})", addr, *(const unsigned long *)src, *(unsigned long *)buff); } while (0); | |||
| 1729 | } | |||
| 1730 | ||||
| 1731 | addr += k_ptrace_word_size; | |||
| 1732 | src += k_ptrace_word_size; | |||
| 1733 | } | |||
| 1734 | return error; | |||
| 1735 | } | |||
| 1736 | ||||
| 1737 | Status NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) const { | |||
| 1738 | return PtraceWrapper(PTRACE_GETSIGINFO, tid, nullptr, siginfo); | |||
| 1739 | } | |||
| 1740 | ||||
| 1741 | Status NativeProcessLinux::GetEventMessage(lldb::tid_t tid, | |||
| 1742 | unsigned long *message) { | |||
| 1743 | return PtraceWrapper(PTRACE_GETEVENTMSG, tid, nullptr, message); | |||
| 1744 | } | |||
| 1745 | ||||
| 1746 | Status NativeProcessLinux::Detach(lldb::tid_t tid) { | |||
| 1747 | if (tid == LLDB_INVALID_THREAD_ID0) | |||
| 1748 | return Status(); | |||
| 1749 | ||||
| 1750 | return PtraceWrapper(PTRACE_DETACH, tid); | |||
| 1751 | } | |||
| 1752 | ||||
| 1753 | bool NativeProcessLinux::HasThreadNoLock(lldb::tid_t thread_id) { | |||
| 1754 | for (const auto &thread : m_threads) { | |||
| 1755 | assert(thread && "thread list should not contain NULL threads")(static_cast <bool> (thread && "thread list should not contain NULL threads" ) ? void (0) : __assert_fail ("thread && \"thread list should not contain NULL threads\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1755, __extension__ __PRETTY_FUNCTION__)); | |||
| 1756 | if (thread->GetID() == thread_id) { | |||
| 1757 | // We have this thread. | |||
| 1758 | return true; | |||
| 1759 | } | |||
| 1760 | } | |||
| 1761 | ||||
| 1762 | // We don't have this thread. | |||
| 1763 | return false; | |||
| 1764 | } | |||
| 1765 | ||||
| 1766 | void NativeProcessLinux::StopTrackingThread(NativeThreadLinux &thread) { | |||
| 1767 | Log *const log = GetLog(POSIXLog::Thread); | |||
| 1768 | lldb::tid_t thread_id = thread.GetID(); | |||
| 1769 | LLDB_LOG(log, "tid: {0}", thread_id)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "tid: {0}", thread_id); } while (0); | |||
| 1770 | ||||
| 1771 | auto it = llvm::find_if(m_threads, [&](const auto &thread_up) { | |||
| 1772 | return thread_up.get() == &thread; | |||
| 1773 | }); | |||
| 1774 | assert(it != m_threads.end())(static_cast <bool> (it != m_threads.end()) ? void (0) : __assert_fail ("it != m_threads.end()", "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , 1774, __extension__ __PRETTY_FUNCTION__)); | |||
| 1775 | m_threads.erase(it); | |||
| 1776 | ||||
| 1777 | NotifyTracersOfThreadDestroyed(thread_id); | |||
| 1778 | SignalIfAllThreadsStopped(); | |||
| 1779 | } | |||
| 1780 | ||||
| 1781 | void NativeProcessLinux::NotifyTracersProcessDidStop() { | |||
| 1782 | m_intel_pt_collector.ProcessDidStop(); | |||
| 1783 | } | |||
| 1784 | ||||
| 1785 | void NativeProcessLinux::NotifyTracersProcessWillResume() { | |||
| 1786 | m_intel_pt_collector.ProcessWillResume(); | |||
| 1787 | } | |||
| 1788 | ||||
| 1789 | Status NativeProcessLinux::NotifyTracersOfNewThread(lldb::tid_t tid) { | |||
| 1790 | Log *log = GetLog(POSIXLog::Thread); | |||
| 1791 | Status error(m_intel_pt_collector.OnThreadCreated(tid)); | |||
| 1792 | if (error.Fail()) | |||
| 1793 | LLDB_LOG(log, "Failed to trace a new thread with intel-pt, tid = {0}. {1}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Failed to trace a new thread with intel-pt, tid = {0}. {1}" , tid, error.AsCString()); } while (0) | |||
| 1794 | tid, error.AsCString())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Failed to trace a new thread with intel-pt, tid = {0}. {1}" , tid, error.AsCString()); } while (0); | |||
| 1795 | return error; | |||
| 1796 | } | |||
| 1797 | ||||
| 1798 | Status NativeProcessLinux::NotifyTracersOfThreadDestroyed(lldb::tid_t tid) { | |||
| 1799 | Log *log = GetLog(POSIXLog::Thread); | |||
| 1800 | Status error(m_intel_pt_collector.OnThreadDestroyed(tid)); | |||
| 1801 | if (error.Fail()) | |||
| 1802 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Failed to stop a destroyed thread with intel-pt, tid = {0}. {1}" , tid, error.AsCString()); } while (0) | |||
| 1803 | "Failed to stop a destroyed thread with intel-pt, tid = {0}. {1}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Failed to stop a destroyed thread with intel-pt, tid = {0}. {1}" , tid, error.AsCString()); } while (0) | |||
| 1804 | tid, error.AsCString())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Failed to stop a destroyed thread with intel-pt, tid = {0}. {1}" , tid, error.AsCString()); } while (0); | |||
| 1805 | return error; | |||
| 1806 | } | |||
| 1807 | ||||
| 1808 | NativeThreadLinux &NativeProcessLinux::AddThread(lldb::tid_t thread_id, | |||
| 1809 | bool resume) { | |||
| 1810 | Log *log = GetLog(POSIXLog::Thread); | |||
| 1811 | LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid {0} adding thread with tid {1}", GetID(), thread_id ); } while (0); | |||
| 1812 | ||||
| 1813 | assert(!HasThreadNoLock(thread_id) &&(static_cast <bool> (!HasThreadNoLock(thread_id) && "attempted to add a thread by id that already exists") ? void (0) : __assert_fail ("!HasThreadNoLock(thread_id) && \"attempted to add a thread by id that already exists\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1814, __extension__ __PRETTY_FUNCTION__)) | |||
| 1814 | "attempted to add a thread by id that already exists")(static_cast <bool> (!HasThreadNoLock(thread_id) && "attempted to add a thread by id that already exists") ? void (0) : __assert_fail ("!HasThreadNoLock(thread_id) && \"attempted to add a thread by id that already exists\"" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1814, __extension__ __PRETTY_FUNCTION__)); | |||
| 1815 | ||||
| 1816 | // If this is the first thread, save it as the current thread | |||
| 1817 | if (m_threads.empty()) | |||
| 1818 | SetCurrentThreadID(thread_id); | |||
| 1819 | ||||
| 1820 | m_threads.push_back(std::make_unique<NativeThreadLinux>(*this, thread_id)); | |||
| 1821 | NativeThreadLinux &thread = | |||
| 1822 | static_cast<NativeThreadLinux &>(*m_threads.back()); | |||
| 1823 | ||||
| 1824 | Status tracing_error = NotifyTracersOfNewThread(thread.GetID()); | |||
| 1825 | if (tracing_error.Fail()) { | |||
| 1826 | thread.SetStoppedByProcessorTrace(tracing_error.AsCString()); | |||
| 1827 | StopRunningThreads(thread.GetID()); | |||
| 1828 | } else if (resume) | |||
| 1829 | ResumeThread(thread, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER(2147483647)); | |||
| 1830 | else | |||
| 1831 | thread.SetStoppedBySignal(SIGSTOP19); | |||
| 1832 | ||||
| 1833 | return thread; | |||
| 1834 | } | |||
| 1835 | ||||
| 1836 | Status NativeProcessLinux::GetLoadedModuleFileSpec(const char *module_path, | |||
| 1837 | FileSpec &file_spec) { | |||
| 1838 | Status error = PopulateMemoryRegionCache(); | |||
| 1839 | if (error.Fail()) | |||
| 1840 | return error; | |||
| 1841 | ||||
| 1842 | FileSpec module_file_spec(module_path); | |||
| 1843 | FileSystem::Instance().Resolve(module_file_spec); | |||
| 1844 | ||||
| 1845 | file_spec.Clear(); | |||
| 1846 | for (const auto &it : m_mem_region_cache) { | |||
| 1847 | if (it.second.GetFilename() == module_file_spec.GetFilename()) { | |||
| 1848 | file_spec = it.second; | |||
| 1849 | return Status(); | |||
| 1850 | } | |||
| 1851 | } | |||
| 1852 | return Status("Module file (%s) not found in /proc/%" PRIu64"l" "u" "/maps file!", | |||
| 1853 | module_file_spec.GetFilename().AsCString(), GetID()); | |||
| 1854 | } | |||
| 1855 | ||||
| 1856 | Status NativeProcessLinux::GetFileLoadAddress(const llvm::StringRef &file_name, | |||
| 1857 | lldb::addr_t &load_addr) { | |||
| 1858 | load_addr = LLDB_INVALID_ADDRESS(18446744073709551615UL); | |||
| 1859 | Status error = PopulateMemoryRegionCache(); | |||
| 1860 | if (error.Fail()) | |||
| 1861 | return error; | |||
| 1862 | ||||
| 1863 | FileSpec file(file_name); | |||
| 1864 | for (const auto &it : m_mem_region_cache) { | |||
| 1865 | if (it.second == file) { | |||
| 1866 | load_addr = it.first.GetRange().GetRangeBase(); | |||
| 1867 | return Status(); | |||
| 1868 | } | |||
| 1869 | } | |||
| 1870 | return Status("No load address found for specified file."); | |||
| 1871 | } | |||
| 1872 | ||||
| 1873 | NativeThreadLinux *NativeProcessLinux::GetThreadByID(lldb::tid_t tid) { | |||
| 1874 | return static_cast<NativeThreadLinux *>( | |||
| 1875 | NativeProcessProtocol::GetThreadByID(tid)); | |||
| 1876 | } | |||
| 1877 | ||||
| 1878 | NativeThreadLinux *NativeProcessLinux::GetCurrentThread() { | |||
| 1879 | return static_cast<NativeThreadLinux *>( | |||
| 1880 | NativeProcessProtocol::GetCurrentThread()); | |||
| 1881 | } | |||
| 1882 | ||||
| 1883 | Status NativeProcessLinux::ResumeThread(NativeThreadLinux &thread, | |||
| 1884 | lldb::StateType state, int signo) { | |||
| 1885 | Log *const log = GetLog(POSIXLog::Thread); | |||
| 1886 | LLDB_LOG(log, "tid: {0}", thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "tid: {0}", thread.GetID()); } while (0); | |||
| 1887 | ||||
| 1888 | // Before we do the resume below, first check if we have a pending stop | |||
| 1889 | // notification that is currently waiting for all threads to stop. This is | |||
| 1890 | // potentially a buggy situation since we're ostensibly waiting for threads | |||
| 1891 | // to stop before we send out the pending notification, and here we are | |||
| 1892 | // resuming one before we send out the pending stop notification. | |||
| 1893 | if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID0) { | |||
| 1894 | LLDB_LOG(log,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "about to resume tid {0} per explicit request but we have a " "pending stop notification (tid {1}) that is actively " "waiting for this thread to stop. Valid sequence of events?" , thread.GetID(), m_pending_notification_tid); } while (0) | |||
| 1895 | "about to resume tid {0} per explicit request but we have a "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "about to resume tid {0} per explicit request but we have a " "pending stop notification (tid {1}) that is actively " "waiting for this thread to stop. Valid sequence of events?" , thread.GetID(), m_pending_notification_tid); } while (0) | |||
| 1896 | "pending stop notification (tid {1}) that is actively "do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "about to resume tid {0} per explicit request but we have a " "pending stop notification (tid {1}) that is actively " "waiting for this thread to stop. Valid sequence of events?" , thread.GetID(), m_pending_notification_tid); } while (0) | |||
| 1897 | "waiting for this thread to stop. Valid sequence of events?",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "about to resume tid {0} per explicit request but we have a " "pending stop notification (tid {1}) that is actively " "waiting for this thread to stop. Valid sequence of events?" , thread.GetID(), m_pending_notification_tid); } while (0) | |||
| 1898 | thread.GetID(), m_pending_notification_tid)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "about to resume tid {0} per explicit request but we have a " "pending stop notification (tid {1}) that is actively " "waiting for this thread to stop. Valid sequence of events?" , thread.GetID(), m_pending_notification_tid); } while (0); | |||
| 1899 | } | |||
| 1900 | ||||
| 1901 | // Request a resume. We expect this to be synchronous and the system to | |||
| 1902 | // reflect it is running after this completes. | |||
| 1903 | switch (state) { | |||
| 1904 | case eStateRunning: { | |||
| 1905 | const auto resume_result = thread.Resume(signo); | |||
| 1906 | if (resume_result.Success()) | |||
| 1907 | SetState(eStateRunning, true); | |||
| 1908 | return resume_result; | |||
| 1909 | } | |||
| 1910 | case eStateStepping: { | |||
| 1911 | const auto step_result = thread.SingleStep(signo); | |||
| 1912 | if (step_result.Success()) | |||
| 1913 | SetState(eStateRunning, true); | |||
| 1914 | return step_result; | |||
| 1915 | } | |||
| 1916 | default: | |||
| 1917 | LLDB_LOG(log, "Unhandled state {0}.", state)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "Unhandled state {0}.", state); } while (0); | |||
| 1918 | llvm_unreachable("Unhandled state for resume")::llvm::llvm_unreachable_internal("Unhandled state for resume" , "lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp", 1918); | |||
| 1919 | } | |||
| 1920 | } | |||
| 1921 | ||||
| 1922 | //===----------------------------------------------------------------------===// | |||
| 1923 | ||||
| 1924 | void NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid) { | |||
| 1925 | Log *const log = GetLog(POSIXLog::Thread); | |||
| 1926 | LLDB_LOG(log, "about to process event: (triggering_tid: {0})",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "about to process event: (triggering_tid: {0})", triggering_tid ); } while (0) | |||
| 1927 | triggering_tid)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "about to process event: (triggering_tid: {0})", triggering_tid ); } while (0); | |||
| 1928 | ||||
| 1929 | m_pending_notification_tid = triggering_tid; | |||
| 1930 | ||||
| 1931 | // Request a stop for all the thread stops that need to be stopped and are | |||
| 1932 | // not already known to be stopped. | |||
| 1933 | for (const auto &thread : m_threads) { | |||
| 1934 | if (StateIsRunningState(thread->GetState())) | |||
| 1935 | static_cast<NativeThreadLinux *>(thread.get())->RequestStop(); | |||
| 1936 | } | |||
| 1937 | ||||
| 1938 | SignalIfAllThreadsStopped(); | |||
| 1939 | LLDB_LOG(log, "event processing done")do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "event processing done"); } while (0); | |||
| 1940 | } | |||
| 1941 | ||||
| 1942 | void NativeProcessLinux::SignalIfAllThreadsStopped() { | |||
| 1943 | if (m_pending_notification_tid == LLDB_INVALID_THREAD_ID0) | |||
| 1944 | return; // No pending notification. Nothing to do. | |||
| 1945 | ||||
| 1946 | for (const auto &thread_sp : m_threads) { | |||
| 1947 | if (StateIsRunningState(thread_sp->GetState())) | |||
| 1948 | return; // Some threads are still running. Don't signal yet. | |||
| 1949 | } | |||
| 1950 | ||||
| 1951 | // We have a pending notification and all threads have stopped. | |||
| 1952 | Log *log = GetLog(LLDBLog::Process | LLDBLog::Breakpoints); | |||
| 1953 | ||||
| 1954 | // Clear any temporary breakpoints we used to implement software single | |||
| 1955 | // stepping. | |||
| 1956 | for (const auto &thread_info : m_threads_stepping_with_breakpoint) { | |||
| 1957 | Status error = RemoveBreakpoint(thread_info.second); | |||
| 1958 | if (error.Fail()) | |||
| 1959 | LLDB_LOG(log, "pid = {0} remove stepping breakpoint: {1}",do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid = {0} remove stepping breakpoint: {1}", thread_info .first, error); } while (0) | |||
| 1960 | thread_info.first, error)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "pid = {0} remove stepping breakpoint: {1}", thread_info .first, error); } while (0); | |||
| 1961 | } | |||
| 1962 | m_threads_stepping_with_breakpoint.clear(); | |||
| 1963 | ||||
| 1964 | // Notify the delegate about the stop | |||
| 1965 | SetCurrentThreadID(m_pending_notification_tid); | |||
| 1966 | SetState(StateType::eStateStopped, true); | |||
| 1967 | m_pending_notification_tid = LLDB_INVALID_THREAD_ID0; | |||
| 1968 | } | |||
| 1969 | ||||
| 1970 | void NativeProcessLinux::ThreadWasCreated(NativeThreadLinux &thread) { | |||
| 1971 | Log *const log = GetLog(POSIXLog::Thread); | |||
| 1972 | LLDB_LOG(log, "tid: {0}", thread.GetID())do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "tid: {0}", thread.GetID()); } while (0); | |||
| 1973 | ||||
| 1974 | if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID0 && | |||
| 1975 | StateIsRunningState(thread.GetState())) { | |||
| 1976 | // We will need to wait for this new thread to stop as well before firing | |||
| 1977 | // the notification. | |||
| 1978 | thread.RequestStop(); | |||
| 1979 | } | |||
| 1980 | } | |||
| 1981 | ||||
| 1982 | // Wrapper for ptrace to catch errors and log calls. Note that ptrace sets | |||
| 1983 | // errno on error because -1 can be a valid result (i.e. for PTRACE_PEEK*) | |||
| 1984 | Status NativeProcessLinux::PtraceWrapper(int req, lldb::pid_t pid, void *addr, | |||
| 1985 | void *data, size_t data_size, | |||
| 1986 | long *result) { | |||
| 1987 | Status error; | |||
| 1988 | long int ret; | |||
| 1989 | ||||
| 1990 | Log *log = GetLog(POSIXLog::Ptrace); | |||
| 1991 | ||||
| 1992 | PtraceDisplayBytes(req, data, data_size); | |||
| 1993 | ||||
| 1994 | errno(*__errno_location ()) = 0; | |||
| 1995 | if (req == PTRACE_GETREGSETPTRACE_GETREGSET || req == PTRACE_SETREGSETPTRACE_SETREGSET) | |||
| 1996 | ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), | |||
| 1997 | *(unsigned int *)addr, data); | |||
| 1998 | else | |||
| 1999 | ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), | |||
| 2000 | addr, data); | |||
| 2001 | ||||
| 2002 | if (ret == -1) | |||
| 2003 | error.SetErrorToErrno(); | |||
| 2004 | ||||
| 2005 | if (result) | |||
| 2006 | *result = ret; | |||
| 2007 | ||||
| 2008 | LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3}, {4})={5:x}", req, pid, addr, data,do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "ptrace({0}, {1}, {2}, {3}, {4})={5:x}", req, pid , addr, data, data_size, ret); } while (0) | |||
| 2009 | data_size, ret)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "ptrace({0}, {1}, {2}, {3}, {4})={5:x}", req, pid , addr, data, data_size, ret); } while (0); | |||
| 2010 | ||||
| 2011 | PtraceDisplayBytes(req, data, data_size); | |||
| 2012 | ||||
| 2013 | if (error.Fail()) | |||
| 2014 | LLDB_LOG(log, "ptrace() failed: {0}", error)do { ::lldb_private::Log *log_private = (log); if (log_private ) log_private->Format("lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp" , __func__, "ptrace() failed: {0}", error); } while (0); | |||
| 2015 | ||||
| 2016 | return error; | |||
| 2017 | } | |||
| 2018 | ||||
| 2019 | llvm::Expected<TraceSupportedResponse> NativeProcessLinux::TraceSupported() { | |||
| 2020 | if (IntelPTCollector::IsSupported()) | |||
| 2021 | return TraceSupportedResponse{"intel-pt", "Intel Processor Trace"}; | |||
| 2022 | return NativeProcessProtocol::TraceSupported(); | |||
| 2023 | } | |||
| 2024 | ||||
| 2025 | Error NativeProcessLinux::TraceStart(StringRef json_request, StringRef type) { | |||
| 2026 | if (type == "intel-pt") { | |||
| 2027 | if (Expected<TraceIntelPTStartRequest> request = | |||
| 2028 | json::parse<TraceIntelPTStartRequest>(json_request, | |||
| 2029 | "TraceIntelPTStartRequest")) { | |||
| 2030 | return m_intel_pt_collector.TraceStart(*request); | |||
| 2031 | } else | |||
| 2032 | return request.takeError(); | |||
| 2033 | } | |||
| 2034 | ||||
| 2035 | return NativeProcessProtocol::TraceStart(json_request, type); | |||
| 2036 | } | |||
| 2037 | ||||
| 2038 | Error NativeProcessLinux::TraceStop(const TraceStopRequest &request) { | |||
| 2039 | if (request.type == "intel-pt") | |||
| 2040 | return m_intel_pt_collector.TraceStop(request); | |||
| 2041 | return NativeProcessProtocol::TraceStop(request); | |||
| 2042 | } | |||
| 2043 | ||||
| 2044 | Expected<json::Value> NativeProcessLinux::TraceGetState(StringRef type) { | |||
| 2045 | if (type == "intel-pt") | |||
| 2046 | return m_intel_pt_collector.GetState(); | |||
| 2047 | return NativeProcessProtocol::TraceGetState(type); | |||
| 2048 | } | |||
| 2049 | ||||
| 2050 | Expected<std::vector<uint8_t>> NativeProcessLinux::TraceGetBinaryData( | |||
| 2051 | const TraceGetBinaryDataRequest &request) { | |||
| 2052 | if (request.type == "intel-pt") | |||
| 2053 | return m_intel_pt_collector.GetBinaryData(request); | |||
| 2054 | return NativeProcessProtocol::TraceGetBinaryData(request); | |||
| 2055 | } |