1af245d11STodd Fiala //===-- NativeProcessLinux.cpp -------------------------------- -*- C++ -*-===// 2af245d11STodd Fiala // 3af245d11STodd Fiala // The LLVM Compiler Infrastructure 4af245d11STodd Fiala // 5af245d11STodd Fiala // This file is distributed under the University of Illinois Open Source 6af245d11STodd Fiala // License. See LICENSE.TXT for details. 7af245d11STodd Fiala // 8af245d11STodd Fiala //===----------------------------------------------------------------------===// 9af245d11STodd Fiala 10af245d11STodd Fiala #include "NativeProcessLinux.h" 11af245d11STodd Fiala 12af245d11STodd Fiala // C Includes 13af245d11STodd Fiala #include <errno.h> 14af245d11STodd Fiala #include <stdint.h> 15b9c1b51eSKate Stone #include <string.h> 16af245d11STodd Fiala #include <unistd.h> 17af245d11STodd Fiala 18af245d11STodd Fiala // C++ Includes 19af245d11STodd Fiala #include <fstream> 20df7c6995SPavel Labath #include <mutex> 21c076559aSPavel Labath #include <sstream> 22af245d11STodd Fiala #include <string> 235b981ab9SPavel Labath #include <unordered_map> 24af245d11STodd Fiala 25af245d11STodd Fiala // Other libraries and framework includes 26d8c338d4STamas Berghammer #include "lldb/Core/EmulateInstruction.h" 276edef204SOleksiy Vyalov #include "lldb/Core/ModuleSpec.h" 28af245d11STodd Fiala #include "lldb/Core/RegisterValue.h" 29af245d11STodd Fiala #include "lldb/Core/State.h" 30af245d11STodd Fiala #include "lldb/Host/Host.h" 315ad891f7SPavel Labath #include "lldb/Host/HostProcess.h" 3224ae6294SZachary Turner #include "lldb/Host/PseudoTerminal.h" 3339de3110SZachary Turner #include "lldb/Host/ThreadLauncher.h" 342a86b555SPavel Labath #include "lldb/Host/common/NativeBreakpoint.h" 352a86b555SPavel Labath #include "lldb/Host/common/NativeRegisterContext.h" 364ee1c952SPavel Labath #include "lldb/Host/linux/Ptrace.h" 374ee1c952SPavel Labath #include "lldb/Host/linux/Uio.h" 38816ae4b0SKamil Rytarowski #include "lldb/Host/posix/ProcessLauncherPosixFork.h" 392a86b555SPavel Labath #include "lldb/Symbol/ObjectFile.h" 4090aff47cSZachary Turner #include "lldb/Target/Process.h" 41af245d11STodd Fiala #include "lldb/Target/ProcessLaunchInfo.h" 425b981ab9SPavel Labath #include "lldb/Target/Target.h" 43c16f5dcaSChaoren Lin #include "lldb/Utility/LLDBAssert.h" 4497206d57SZachary Turner #include "lldb/Utility/Status.h" 45f805e190SPavel Labath #include "lldb/Utility/StringExtractor.h" 4610c41f37SPavel Labath #include "llvm/Support/Errno.h" 4710c41f37SPavel Labath #include "llvm/Support/FileSystem.h" 4810c41f37SPavel Labath #include "llvm/Support/Threading.h" 49af245d11STodd Fiala 50af245d11STodd Fiala #include "NativeThreadLinux.h" 51b9c1b51eSKate Stone #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" 521e209fccSTamas Berghammer #include "Procfs.h" 53cacde7dfSTodd Fiala 54d858487eSTamas Berghammer #include <linux/unistd.h> 55d858487eSTamas Berghammer #include <sys/socket.h> 56df7c6995SPavel Labath #include <sys/syscall.h> 57d858487eSTamas Berghammer #include <sys/types.h> 58d858487eSTamas Berghammer #include <sys/user.h> 59d858487eSTamas Berghammer #include <sys/wait.h> 60d858487eSTamas Berghammer 61af245d11STodd Fiala // Support hardware breakpoints in case it has not been defined 62af245d11STodd Fiala #ifndef TRAP_HWBKPT 63af245d11STodd Fiala #define TRAP_HWBKPT 4 64af245d11STodd Fiala #endif 65af245d11STodd Fiala 667cb18bf5STamas Berghammer using namespace lldb; 677cb18bf5STamas Berghammer using namespace lldb_private; 68db264a6dSTamas Berghammer using namespace lldb_private::process_linux; 697cb18bf5STamas Berghammer using namespace llvm; 707cb18bf5STamas Berghammer 71af245d11STodd Fiala // Private bits we only need internally. 72df7c6995SPavel Labath 73b9c1b51eSKate Stone static bool ProcessVmReadvSupported() { 74df7c6995SPavel Labath static bool is_supported; 75c5f28e2aSKamil Rytarowski static llvm::once_flag flag; 76df7c6995SPavel Labath 77c5f28e2aSKamil Rytarowski llvm::call_once(flag, [] { 78a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 79df7c6995SPavel Labath 80df7c6995SPavel Labath uint32_t source = 0x47424742; 81df7c6995SPavel Labath uint32_t dest = 0; 82df7c6995SPavel Labath 83df7c6995SPavel Labath struct iovec local, remote; 84df7c6995SPavel Labath remote.iov_base = &source; 85df7c6995SPavel Labath local.iov_base = &dest; 86df7c6995SPavel Labath remote.iov_len = local.iov_len = sizeof source; 87df7c6995SPavel Labath 88b9c1b51eSKate Stone // We shall try if cross-process-memory reads work by attempting to read a 89b9c1b51eSKate Stone // value from our own process. 90df7c6995SPavel Labath ssize_t res = process_vm_readv(getpid(), &local, 1, &remote, 1, 0); 91df7c6995SPavel Labath is_supported = (res == sizeof(source) && source == dest); 92df7c6995SPavel Labath if (is_supported) 93a6321a8eSPavel Labath LLDB_LOG(log, 94a6321a8eSPavel Labath "Detected kernel support for process_vm_readv syscall. " 95a6321a8eSPavel Labath "Fast memory reads enabled."); 96df7c6995SPavel Labath else 97a6321a8eSPavel Labath LLDB_LOG(log, 98a6321a8eSPavel Labath "syscall process_vm_readv failed (error: {0}). Fast memory " 99a6321a8eSPavel Labath "reads disabled.", 10010c41f37SPavel Labath llvm::sys::StrError()); 101df7c6995SPavel Labath }); 102df7c6995SPavel Labath 103df7c6995SPavel Labath return is_supported; 104df7c6995SPavel Labath } 105df7c6995SPavel Labath 106b9c1b51eSKate Stone namespace { 107b9c1b51eSKate Stone void MaybeLogLaunchInfo(const ProcessLaunchInfo &info) { 108a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1094abe5d69SPavel Labath if (!log) 1104abe5d69SPavel Labath return; 1114abe5d69SPavel Labath 1124abe5d69SPavel Labath if (const FileAction *action = info.GetFileActionForFD(STDIN_FILENO)) 113a6321a8eSPavel Labath LLDB_LOG(log, "setting STDIN to '{0}'", action->GetFileSpec()); 1144abe5d69SPavel Labath else 115a6321a8eSPavel Labath LLDB_LOG(log, "leaving STDIN as is"); 1164abe5d69SPavel Labath 1174abe5d69SPavel Labath if (const FileAction *action = info.GetFileActionForFD(STDOUT_FILENO)) 118a6321a8eSPavel Labath LLDB_LOG(log, "setting STDOUT to '{0}'", action->GetFileSpec()); 1194abe5d69SPavel Labath else 120a6321a8eSPavel Labath LLDB_LOG(log, "leaving STDOUT as is"); 1214abe5d69SPavel Labath 1224abe5d69SPavel Labath if (const FileAction *action = info.GetFileActionForFD(STDERR_FILENO)) 123a6321a8eSPavel Labath LLDB_LOG(log, "setting STDERR to '{0}'", action->GetFileSpec()); 1244abe5d69SPavel Labath else 125a6321a8eSPavel Labath LLDB_LOG(log, "leaving STDERR as is"); 1264abe5d69SPavel Labath 1274abe5d69SPavel Labath int i = 0; 128b9c1b51eSKate Stone for (const char **args = info.GetArguments().GetConstArgumentVector(); *args; 129b9c1b51eSKate Stone ++args, ++i) 130a6321a8eSPavel Labath LLDB_LOG(log, "arg {0}: '{1}'", i, *args); 1314abe5d69SPavel Labath } 1324abe5d69SPavel Labath 133b9c1b51eSKate Stone void DisplayBytes(StreamString &s, void *bytes, uint32_t count) { 134af245d11STodd Fiala uint8_t *ptr = (uint8_t *)bytes; 135af245d11STodd Fiala const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count); 136b9c1b51eSKate Stone for (uint32_t i = 0; i < loop_count; i++) { 137af245d11STodd Fiala s.Printf("[%x]", *ptr); 138af245d11STodd Fiala ptr++; 139af245d11STodd Fiala } 140af245d11STodd Fiala } 141af245d11STodd Fiala 142b9c1b51eSKate Stone void PtraceDisplayBytes(int &req, void *data, size_t data_size) { 143aafe053cSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 144a6321a8eSPavel Labath if (!log) 145a6321a8eSPavel Labath return; 146af245d11STodd Fiala StreamString buf; 147af245d11STodd Fiala 148b9c1b51eSKate Stone switch (req) { 149b9c1b51eSKate Stone case PTRACE_POKETEXT: { 150af245d11STodd Fiala DisplayBytes(buf, &data, 8); 151aafe053cSPavel Labath LLDB_LOGV(log, "PTRACE_POKETEXT {0}", buf.GetData()); 152af245d11STodd Fiala break; 153af245d11STodd Fiala } 154b9c1b51eSKate Stone case PTRACE_POKEDATA: { 155af245d11STodd Fiala DisplayBytes(buf, &data, 8); 156aafe053cSPavel Labath LLDB_LOGV(log, "PTRACE_POKEDATA {0}", buf.GetData()); 157af245d11STodd Fiala break; 158af245d11STodd Fiala } 159b9c1b51eSKate Stone case PTRACE_POKEUSER: { 160af245d11STodd Fiala DisplayBytes(buf, &data, 8); 161aafe053cSPavel Labath LLDB_LOGV(log, "PTRACE_POKEUSER {0}", buf.GetData()); 162af245d11STodd Fiala break; 163af245d11STodd Fiala } 164b9c1b51eSKate Stone case PTRACE_SETREGS: { 165af245d11STodd Fiala DisplayBytes(buf, data, data_size); 166aafe053cSPavel Labath LLDB_LOGV(log, "PTRACE_SETREGS {0}", buf.GetData()); 167af245d11STodd Fiala break; 168af245d11STodd Fiala } 169b9c1b51eSKate Stone case PTRACE_SETFPREGS: { 170af245d11STodd Fiala DisplayBytes(buf, data, data_size); 171aafe053cSPavel Labath LLDB_LOGV(log, "PTRACE_SETFPREGS {0}", buf.GetData()); 172af245d11STodd Fiala break; 173af245d11STodd Fiala } 174b9c1b51eSKate Stone case PTRACE_SETSIGINFO: { 175af245d11STodd Fiala DisplayBytes(buf, data, sizeof(siginfo_t)); 176aafe053cSPavel Labath LLDB_LOGV(log, "PTRACE_SETSIGINFO {0}", buf.GetData()); 177af245d11STodd Fiala break; 178af245d11STodd Fiala } 179b9c1b51eSKate Stone case PTRACE_SETREGSET: { 180af245d11STodd Fiala // Extract iov_base from data, which is a pointer to the struct IOVEC 181af245d11STodd Fiala DisplayBytes(buf, *(void **)data, data_size); 182aafe053cSPavel Labath LLDB_LOGV(log, "PTRACE_SETREGSET {0}", buf.GetData()); 183af245d11STodd Fiala break; 184af245d11STodd Fiala } 185b9c1b51eSKate Stone default: {} 186af245d11STodd Fiala } 187af245d11STodd Fiala } 188af245d11STodd Fiala 18919cbe96aSPavel Labath static constexpr unsigned k_ptrace_word_size = sizeof(void *); 190b9c1b51eSKate Stone static_assert(sizeof(long) >= k_ptrace_word_size, 191b9c1b51eSKate Stone "Size of long must be larger than ptrace word size"); 1921107b5a5SPavel Labath } // end of anonymous namespace 1931107b5a5SPavel Labath 194bd7cbc5aSPavel Labath // Simple helper function to ensure flags are enabled on the given file 195bd7cbc5aSPavel Labath // descriptor. 19697206d57SZachary Turner static Status EnsureFDFlags(int fd, int flags) { 19797206d57SZachary Turner Status error; 198bd7cbc5aSPavel Labath 199bd7cbc5aSPavel Labath int status = fcntl(fd, F_GETFL); 200b9c1b51eSKate Stone if (status == -1) { 201bd7cbc5aSPavel Labath error.SetErrorToErrno(); 202bd7cbc5aSPavel Labath return error; 203bd7cbc5aSPavel Labath } 204bd7cbc5aSPavel Labath 205b9c1b51eSKate Stone if (fcntl(fd, F_SETFL, status | flags) == -1) { 206bd7cbc5aSPavel Labath error.SetErrorToErrno(); 207bd7cbc5aSPavel Labath return error; 208bd7cbc5aSPavel Labath } 209bd7cbc5aSPavel Labath 210bd7cbc5aSPavel Labath return error; 211bd7cbc5aSPavel Labath } 212bd7cbc5aSPavel Labath 213af245d11STodd Fiala // ----------------------------------------------------------------------------- 214af245d11STodd Fiala // Public Static Methods 215af245d11STodd Fiala // ----------------------------------------------------------------------------- 216af245d11STodd Fiala 21797206d57SZachary Turner Status NativeProcessProtocol::Launch( 218db264a6dSTamas Berghammer ProcessLaunchInfo &launch_info, 219b9c1b51eSKate Stone NativeProcessProtocol::NativeDelegate &native_delegate, MainLoop &mainloop, 220b9c1b51eSKate Stone NativeProcessProtocolSP &native_process_sp) { 221a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 222af245d11STodd Fiala 22397206d57SZachary Turner Status error; 224af245d11STodd Fiala 225af245d11STodd Fiala // Verify the working directory is valid if one was specified. 226d3173f34SChaoren Lin FileSpec working_dir{launch_info.GetWorkingDirectory()}; 2277d86ee5aSZachary Turner if (working_dir && (!working_dir.ResolvePath() || 2287d86ee5aSZachary Turner !llvm::sys::fs::is_directory(working_dir.GetPath()))) { 229d3173f34SChaoren Lin error.SetErrorStringWithFormat("No such file or directory: %s", 230d3173f34SChaoren Lin working_dir.GetCString()); 231af245d11STodd Fiala return error; 232af245d11STodd Fiala } 233af245d11STodd Fiala 234af245d11STodd Fiala // Create the NativeProcessLinux in launch mode. 235af245d11STodd Fiala native_process_sp.reset(new NativeProcessLinux()); 236af245d11STodd Fiala 237b9c1b51eSKate Stone if (!native_process_sp->RegisterNativeDelegate(native_delegate)) { 238af245d11STodd Fiala native_process_sp.reset(); 239af245d11STodd Fiala error.SetErrorStringWithFormat("failed to register the native delegate"); 240af245d11STodd Fiala return error; 241af245d11STodd Fiala } 242af245d11STodd Fiala 243b9c1b51eSKate Stone error = std::static_pointer_cast<NativeProcessLinux>(native_process_sp) 244b9c1b51eSKate Stone ->LaunchInferior(mainloop, launch_info); 245af245d11STodd Fiala 246b9c1b51eSKate Stone if (error.Fail()) { 247af245d11STodd Fiala native_process_sp.reset(); 248a6321a8eSPavel Labath LLDB_LOG(log, "failed to launch process: {0}", error); 249af245d11STodd Fiala return error; 250af245d11STodd Fiala } 251af245d11STodd Fiala 252af245d11STodd Fiala launch_info.SetProcessID(native_process_sp->GetID()); 253af245d11STodd Fiala 254af245d11STodd Fiala return error; 255af245d11STodd Fiala } 256af245d11STodd Fiala 25797206d57SZachary Turner Status NativeProcessProtocol::Attach( 258b9c1b51eSKate Stone lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, 259b9c1b51eSKate Stone MainLoop &mainloop, NativeProcessProtocolSP &native_process_sp) { 260a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 261a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0:x}", pid); 262af245d11STodd Fiala 263af245d11STodd Fiala // Retrieve the architecture for the running process. 264af245d11STodd Fiala ArchSpec process_arch; 26597206d57SZachary Turner Status error = ResolveProcessArchitecture(pid, process_arch); 266af245d11STodd Fiala if (!error.Success()) 267af245d11STodd Fiala return error; 268af245d11STodd Fiala 269b9c1b51eSKate Stone std::shared_ptr<NativeProcessLinux> native_process_linux_sp( 270b9c1b51eSKate Stone new NativeProcessLinux()); 271af245d11STodd Fiala 272b9c1b51eSKate Stone if (!native_process_linux_sp->RegisterNativeDelegate(native_delegate)) { 273af245d11STodd Fiala error.SetErrorStringWithFormat("failed to register the native delegate"); 274af245d11STodd Fiala return error; 275af245d11STodd Fiala } 276af245d11STodd Fiala 27719cbe96aSPavel Labath native_process_linux_sp->AttachToInferior(mainloop, pid, error); 278af245d11STodd Fiala if (!error.Success()) 279af245d11STodd Fiala return error; 280af245d11STodd Fiala 2811339b5e8SOleksiy Vyalov native_process_sp = native_process_linux_sp; 282af245d11STodd Fiala return error; 283af245d11STodd Fiala } 284af245d11STodd Fiala 285af245d11STodd Fiala // ----------------------------------------------------------------------------- 286af245d11STodd Fiala // Public Instance Methods 287af245d11STodd Fiala // ----------------------------------------------------------------------------- 288af245d11STodd Fiala 289b9c1b51eSKate Stone NativeProcessLinux::NativeProcessLinux() 290b9c1b51eSKate Stone : NativeProcessProtocol(LLDB_INVALID_PROCESS_ID), m_arch(), 291b9c1b51eSKate Stone m_supports_mem_region(eLazyBoolCalculate), m_mem_region_cache(), 292b9c1b51eSKate Stone m_pending_notification_tid(LLDB_INVALID_THREAD_ID) {} 293af245d11STodd Fiala 294b9c1b51eSKate Stone void NativeProcessLinux::AttachToInferior(MainLoop &mainloop, lldb::pid_t pid, 29597206d57SZachary Turner Status &error) { 296a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 297a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0:x}", pid); 298af245d11STodd Fiala 299b9c1b51eSKate Stone m_sigchld_handle = mainloop.RegisterSignal( 300b9c1b51eSKate Stone SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, error); 30119cbe96aSPavel Labath if (!m_sigchld_handle) 30219cbe96aSPavel Labath return; 30319cbe96aSPavel Labath 3042a86b555SPavel Labath error = ResolveProcessArchitecture(pid, m_arch); 305af245d11STodd Fiala if (!error.Success()) 306af245d11STodd Fiala return; 307af245d11STodd Fiala 308af245d11STodd Fiala // Set the architecture to the exe architecture. 309a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid, 310a6321a8eSPavel Labath m_arch.GetArchitectureName()); 311af245d11STodd Fiala m_pid = pid; 312af245d11STodd Fiala SetState(eStateAttaching); 313af245d11STodd Fiala 31419cbe96aSPavel Labath Attach(pid, error); 315af245d11STodd Fiala } 316af245d11STodd Fiala 31797206d57SZachary Turner Status NativeProcessLinux::LaunchInferior(MainLoop &mainloop, 318b9c1b51eSKate Stone ProcessLaunchInfo &launch_info) { 31997206d57SZachary Turner Status error; 320b9c1b51eSKate Stone m_sigchld_handle = mainloop.RegisterSignal( 321b9c1b51eSKate Stone SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, error); 3224abe5d69SPavel Labath if (!m_sigchld_handle) 3234abe5d69SPavel Labath return error; 3244abe5d69SPavel Labath 3254abe5d69SPavel Labath SetState(eStateLaunching); 3260c4f01d4SPavel Labath 3274abe5d69SPavel Labath MaybeLogLaunchInfo(launch_info); 3284abe5d69SPavel Labath 329b9c1b51eSKate Stone ::pid_t pid = 330816ae4b0SKamil Rytarowski ProcessLauncherPosixFork().LaunchProcess(launch_info, error).GetProcessId(); 3315ad891f7SPavel Labath if (error.Fail()) 3324abe5d69SPavel Labath return error; 3330c4f01d4SPavel Labath 334a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 33575f47c3aSTodd Fiala 336af245d11STodd Fiala // Wait for the child process to trap on its call to execve. 337af245d11STodd Fiala ::pid_t wpid; 338af245d11STodd Fiala int status; 339b9c1b51eSKate Stone if ((wpid = waitpid(pid, &status, 0)) < 0) { 340bd7cbc5aSPavel Labath error.SetErrorToErrno(); 341a6321a8eSPavel Labath LLDB_LOG(log, "waitpid for inferior failed with %s", error); 342af245d11STodd Fiala 343af245d11STodd Fiala // Mark the inferior as invalid. 344b9c1b51eSKate Stone // FIXME this could really use a new state - eStateLaunchFailure. For now, 345b9c1b51eSKate Stone // using eStateInvalid. 346bd7cbc5aSPavel Labath SetState(StateType::eStateInvalid); 347af245d11STodd Fiala 3484abe5d69SPavel Labath return error; 349af245d11STodd Fiala } 350af245d11STodd Fiala assert(WIFSTOPPED(status) && (wpid == static_cast<::pid_t>(pid)) && 351af245d11STodd Fiala "Could not sync with inferior process."); 352af245d11STodd Fiala 353a6321a8eSPavel Labath LLDB_LOG(log, "inferior started, now in stopped state"); 354bd7cbc5aSPavel Labath error = SetDefaultPtraceOpts(pid); 355b9c1b51eSKate Stone if (error.Fail()) { 356a6321a8eSPavel Labath LLDB_LOG(log, "failed to set default ptrace options: {0}", error); 357af245d11STodd Fiala 358af245d11STodd Fiala // Mark the inferior as invalid. 359b9c1b51eSKate Stone // FIXME this could really use a new state - eStateLaunchFailure. For now, 360b9c1b51eSKate Stone // using eStateInvalid. 361bd7cbc5aSPavel Labath SetState(StateType::eStateInvalid); 362af245d11STodd Fiala 3634abe5d69SPavel Labath return error; 364af245d11STodd Fiala } 365af245d11STodd Fiala 366af245d11STodd Fiala // Release the master terminal descriptor and pass it off to the 367af245d11STodd Fiala // NativeProcessLinux instance. Similarly stash the inferior pid. 3685ad891f7SPavel Labath m_terminal_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor(); 369bd7cbc5aSPavel Labath m_pid = pid; 3704abe5d69SPavel Labath launch_info.SetProcessID(pid); 371af245d11STodd Fiala 372b9c1b51eSKate Stone if (m_terminal_fd != -1) { 373bd7cbc5aSPavel Labath error = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); 374b9c1b51eSKate Stone if (error.Fail()) { 375a6321a8eSPavel Labath LLDB_LOG(log, 376a6321a8eSPavel Labath "inferior EnsureFDFlags failed for ensuring terminal " 377a6321a8eSPavel Labath "O_NONBLOCK setting: {0}", 378a6321a8eSPavel Labath error); 379af245d11STodd Fiala 380af245d11STodd Fiala // Mark the inferior as invalid. 381b9c1b51eSKate Stone // FIXME this could really use a new state - eStateLaunchFailure. For 382b9c1b51eSKate Stone // now, using eStateInvalid. 383bd7cbc5aSPavel Labath SetState(StateType::eStateInvalid); 384af245d11STodd Fiala 3854abe5d69SPavel Labath return error; 386af245d11STodd Fiala } 3875ad891f7SPavel Labath } 388af245d11STodd Fiala 389a6321a8eSPavel Labath LLDB_LOG(log, "adding pid = {0}", pid); 3902a86b555SPavel Labath ResolveProcessArchitecture(m_pid, m_arch); 391f9077782SPavel Labath NativeThreadLinuxSP thread_sp = AddThread(pid); 392af245d11STodd Fiala assert(thread_sp && "AddThread() returned a nullptr thread"); 393f9077782SPavel Labath thread_sp->SetStoppedBySignal(SIGSTOP); 394f9077782SPavel Labath ThreadWasCreated(*thread_sp); 395af245d11STodd Fiala 396af245d11STodd Fiala // Let our process instance know the thread has stopped. 397bd7cbc5aSPavel Labath SetCurrentThreadID(thread_sp->GetID()); 398bd7cbc5aSPavel Labath SetState(StateType::eStateStopped); 399af245d11STodd Fiala 400a6321a8eSPavel Labath if (error.Fail()) 401a6321a8eSPavel Labath LLDB_LOG(log, "inferior launching failed {0}", error); 4024abe5d69SPavel Labath return error; 403af245d11STodd Fiala } 404af245d11STodd Fiala 40597206d57SZachary Turner ::pid_t NativeProcessLinux::Attach(lldb::pid_t pid, Status &error) { 406a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 407af245d11STodd Fiala 408b9c1b51eSKate Stone // Use a map to keep track of the threads which we have attached/need to 409b9c1b51eSKate Stone // attach. 410af245d11STodd Fiala Host::TidMap tids_to_attach; 411b9c1b51eSKate Stone if (pid <= 1) { 412bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 413bd7cbc5aSPavel Labath error.SetErrorString("Attaching to process 1 is not allowed."); 414bd7cbc5aSPavel Labath return -1; 415af245d11STodd Fiala } 416af245d11STodd Fiala 417b9c1b51eSKate Stone while (Host::FindProcessThreads(pid, tids_to_attach)) { 418af245d11STodd Fiala for (Host::TidMap::iterator it = tids_to_attach.begin(); 419b9c1b51eSKate Stone it != tids_to_attach.end();) { 420b9c1b51eSKate Stone if (it->second == false) { 421af245d11STodd Fiala lldb::tid_t tid = it->first; 422af245d11STodd Fiala 423af245d11STodd Fiala // Attach to the requested process. 424af245d11STodd Fiala // An attach will cause the thread to stop with a SIGSTOP. 4254a9babb2SPavel Labath error = PtraceWrapper(PTRACE_ATTACH, tid); 426b9c1b51eSKate Stone if (error.Fail()) { 427af245d11STodd Fiala // No such thread. The thread may have exited. 428af245d11STodd Fiala // More error handling may be needed. 429b9c1b51eSKate Stone if (error.GetError() == ESRCH) { 430af245d11STodd Fiala it = tids_to_attach.erase(it); 431af245d11STodd Fiala continue; 432b9c1b51eSKate Stone } else 433bd7cbc5aSPavel Labath return -1; 434af245d11STodd Fiala } 435af245d11STodd Fiala 436af245d11STodd Fiala int status; 437af245d11STodd Fiala // Need to use __WALL otherwise we receive an error with errno=ECHLD 438af245d11STodd Fiala // At this point we should have a thread stopped if waitpid succeeds. 439b9c1b51eSKate Stone if ((status = waitpid(tid, NULL, __WALL)) < 0) { 440af245d11STodd Fiala // No such thread. The thread may have exited. 441af245d11STodd Fiala // More error handling may be needed. 442b9c1b51eSKate Stone if (errno == ESRCH) { 443af245d11STodd Fiala it = tids_to_attach.erase(it); 444af245d11STodd Fiala continue; 445b9c1b51eSKate Stone } else { 446bd7cbc5aSPavel Labath error.SetErrorToErrno(); 447bd7cbc5aSPavel Labath return -1; 448af245d11STodd Fiala } 449af245d11STodd Fiala } 450af245d11STodd Fiala 451bd7cbc5aSPavel Labath error = SetDefaultPtraceOpts(tid); 452bd7cbc5aSPavel Labath if (error.Fail()) 453bd7cbc5aSPavel Labath return -1; 454af245d11STodd Fiala 455a6321a8eSPavel Labath LLDB_LOG(log, "adding tid = {0}", tid); 456af245d11STodd Fiala it->second = true; 457af245d11STodd Fiala 458af245d11STodd Fiala // Create the thread, mark it as stopped. 459f9077782SPavel Labath NativeThreadLinuxSP thread_sp(AddThread(static_cast<lldb::tid_t>(tid))); 460af245d11STodd Fiala assert(thread_sp && "AddThread() returned a nullptr"); 461fa03ad2eSChaoren Lin 462b9c1b51eSKate Stone // This will notify this is a new thread and tell the system it is 463b9c1b51eSKate Stone // stopped. 464f9077782SPavel Labath thread_sp->SetStoppedBySignal(SIGSTOP); 465f9077782SPavel Labath ThreadWasCreated(*thread_sp); 466bd7cbc5aSPavel Labath SetCurrentThreadID(thread_sp->GetID()); 467af245d11STodd Fiala } 468af245d11STodd Fiala 469af245d11STodd Fiala // move the loop forward 470af245d11STodd Fiala ++it; 471af245d11STodd Fiala } 472af245d11STodd Fiala } 473af245d11STodd Fiala 474b9c1b51eSKate Stone if (tids_to_attach.size() > 0) { 475bd7cbc5aSPavel Labath m_pid = pid; 476af245d11STodd Fiala // Let our process instance know the thread has stopped. 477bd7cbc5aSPavel Labath SetState(StateType::eStateStopped); 478b9c1b51eSKate Stone } else { 479bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 480bd7cbc5aSPavel Labath error.SetErrorString("No such process."); 481bd7cbc5aSPavel Labath return -1; 482af245d11STodd Fiala } 483af245d11STodd Fiala 484bd7cbc5aSPavel Labath return pid; 485af245d11STodd Fiala } 486af245d11STodd Fiala 48797206d57SZachary Turner Status NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) { 488af245d11STodd Fiala long ptrace_opts = 0; 489af245d11STodd Fiala 490af245d11STodd Fiala // Have the child raise an event on exit. This is used to keep the child in 491af245d11STodd Fiala // limbo until it is destroyed. 492af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXIT; 493af245d11STodd Fiala 494af245d11STodd Fiala // Have the tracer trace threads which spawn in the inferior process. 495af245d11STodd Fiala // TODO: if we want to support tracing the inferiors' child, add the 496af245d11STodd Fiala // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK) 497af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACECLONE; 498af245d11STodd Fiala 499af245d11STodd Fiala // Have the tracer notify us before execve returns 500af245d11STodd Fiala // (needed to disable legacy SIGTRAP generation) 501af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXEC; 502af245d11STodd Fiala 5034a9babb2SPavel Labath return PtraceWrapper(PTRACE_SETOPTIONS, pid, nullptr, (void *)ptrace_opts); 504af245d11STodd Fiala } 505af245d11STodd Fiala 5061107b5a5SPavel Labath // Handles all waitpid events from the inferior process. 507b9c1b51eSKate Stone void NativeProcessLinux::MonitorCallback(lldb::pid_t pid, bool exited, 508*3508fc8cSPavel Labath WaitStatus status) { 509af245d11STodd Fiala Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 510af245d11STodd Fiala 511b9c1b51eSKate Stone // Certain activities differ based on whether the pid is the tid of the main 512b9c1b51eSKate Stone // thread. 5131107b5a5SPavel Labath const bool is_main_thread = (pid == GetID()); 514af245d11STodd Fiala 515af245d11STodd Fiala // Handle when the thread exits. 516b9c1b51eSKate Stone if (exited) { 517a6321a8eSPavel Labath LLDB_LOG(log, "got exit signal({0}) , tid = {1} ({2} main thread)", signal, 518a6321a8eSPavel Labath pid, is_main_thread ? "is" : "is not"); 519af245d11STodd Fiala 520af245d11STodd Fiala // This is a thread that exited. Ensure we're not tracking it anymore. 5211107b5a5SPavel Labath const bool thread_found = StopTrackingThread(pid); 522af245d11STodd Fiala 523b9c1b51eSKate Stone if (is_main_thread) { 524b9c1b51eSKate Stone // We only set the exit status and notify the delegate if we haven't 525b9c1b51eSKate Stone // already set the process 526b9c1b51eSKate Stone // state to an exited state. We normally should have received a SIGTRAP | 527b9c1b51eSKate Stone // (PTRACE_EVENT_EXIT << 8) 528af245d11STodd Fiala // for the main thread. 529b9c1b51eSKate Stone const bool already_notified = (GetState() == StateType::eStateExited) || 530b9c1b51eSKate Stone (GetState() == StateType::eStateCrashed); 531b9c1b51eSKate Stone if (!already_notified) { 532a6321a8eSPavel Labath LLDB_LOG( 533a6321a8eSPavel Labath log, 534a6321a8eSPavel Labath "tid = {0} handling main thread exit ({1}), expected exit state " 535a6321a8eSPavel Labath "already set but state was {2} instead, setting exit state now", 536a6321a8eSPavel Labath pid, 537b9c1b51eSKate Stone thread_found ? "stopped tracking thread metadata" 538b9c1b51eSKate Stone : "thread metadata not found", 5398198db30SPavel Labath GetState()); 540af245d11STodd Fiala // The main thread exited. We're done monitoring. Report to delegate. 541*3508fc8cSPavel Labath SetExitStatus(status, true); 542af245d11STodd Fiala 543af245d11STodd Fiala // Notify delegate that our process has exited. 5441107b5a5SPavel Labath SetState(StateType::eStateExited, true); 545a6321a8eSPavel Labath } else 546a6321a8eSPavel Labath LLDB_LOG(log, "tid = {0} main thread now exited (%s)", pid, 547b9c1b51eSKate Stone thread_found ? "stopped tracking thread metadata" 548b9c1b51eSKate Stone : "thread metadata not found"); 549b9c1b51eSKate Stone } else { 550b9c1b51eSKate Stone // Do we want to report to the delegate in this case? I think not. If 551a6321a8eSPavel Labath // this was an orderly thread exit, we would already have received the 552a6321a8eSPavel Labath // SIGTRAP | (PTRACE_EVENT_EXIT << 8) signal, and we would have done an 553a6321a8eSPavel Labath // all-stop then. 554a6321a8eSPavel Labath LLDB_LOG(log, "tid = {0} handling non-main thread exit (%s)", pid, 555b9c1b51eSKate Stone thread_found ? "stopped tracking thread metadata" 556b9c1b51eSKate Stone : "thread metadata not found"); 557af245d11STodd Fiala } 5581107b5a5SPavel Labath return; 559af245d11STodd Fiala } 560af245d11STodd Fiala 561af245d11STodd Fiala siginfo_t info; 562b9cc0c75SPavel Labath const auto info_err = GetSignalInfo(pid, &info); 563b9cc0c75SPavel Labath auto thread_sp = GetThreadByID(pid); 564b9cc0c75SPavel Labath 565b9c1b51eSKate Stone if (!thread_sp) { 566b9c1b51eSKate Stone // Normally, the only situation when we cannot find the thread is if we have 567a6321a8eSPavel Labath // just received a new thread notification. This is indicated by 568a6321a8eSPavel Labath // GetSignalInfo() returning si_code == SI_USER and si_pid == 0 569a6321a8eSPavel Labath LLDB_LOG(log, "received notification about an unknown tid {0}.", pid); 570b9cc0c75SPavel Labath 571b9c1b51eSKate Stone if (info_err.Fail()) { 572a6321a8eSPavel Labath LLDB_LOG(log, 573a6321a8eSPavel Labath "(tid {0}) GetSignalInfo failed ({1}). " 574a6321a8eSPavel Labath "Ingoring this notification.", 575a6321a8eSPavel Labath pid, info_err); 576b9cc0c75SPavel Labath return; 577b9cc0c75SPavel Labath } 578b9cc0c75SPavel Labath 579a6321a8eSPavel Labath LLDB_LOG(log, "tid {0}, si_code: {1}, si_pid: {2}", pid, info.si_code, 580a6321a8eSPavel Labath info.si_pid); 581b9cc0c75SPavel Labath 582b9cc0c75SPavel Labath auto thread_sp = AddThread(pid); 583b9cc0c75SPavel Labath // Resume the newly created thread. 584b9cc0c75SPavel Labath ResumeThread(*thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); 585b9cc0c75SPavel Labath ThreadWasCreated(*thread_sp); 586b9cc0c75SPavel Labath return; 587b9cc0c75SPavel Labath } 588b9cc0c75SPavel Labath 589b9cc0c75SPavel Labath // Get details on the signal raised. 590b9c1b51eSKate Stone if (info_err.Success()) { 591fa03ad2eSChaoren Lin // We have retrieved the signal info. Dispatch appropriately. 592fa03ad2eSChaoren Lin if (info.si_signo == SIGTRAP) 593b9cc0c75SPavel Labath MonitorSIGTRAP(info, *thread_sp); 594fa03ad2eSChaoren Lin else 595b9cc0c75SPavel Labath MonitorSignal(info, *thread_sp, exited); 596b9c1b51eSKate Stone } else { 597b9c1b51eSKate Stone if (info_err.GetError() == EINVAL) { 598fa03ad2eSChaoren Lin // This is a group stop reception for this tid. 599b9c1b51eSKate Stone // We can reach here if we reinject SIGSTOP, SIGSTP, SIGTTIN or SIGTTOU 600a6321a8eSPavel Labath // into the tracee, triggering the group-stop mechanism. Normally 601a6321a8eSPavel Labath // receiving these would stop the process, pending a SIGCONT. Simulating 602a6321a8eSPavel Labath // this state in a debugger is hard and is generally not needed (one use 603a6321a8eSPavel Labath // case is debugging background task being managed by a shell). For 604a6321a8eSPavel Labath // general use, it is sufficient to stop the process in a signal-delivery 605b9c1b51eSKate Stone // stop which happens before the group stop. This done by MonitorSignal 606a6321a8eSPavel Labath // and works correctly for all signals. 607a6321a8eSPavel Labath LLDB_LOG(log, 608a6321a8eSPavel Labath "received a group stop for pid {0} tid {1}. Transparent " 609a6321a8eSPavel Labath "handling of group stops not supported, resuming the " 610a6321a8eSPavel Labath "thread.", 611a6321a8eSPavel Labath GetID(), pid); 612b9c1b51eSKate Stone ResumeThread(*thread_sp, thread_sp->GetState(), 613b9c1b51eSKate Stone LLDB_INVALID_SIGNAL_NUMBER); 614b9c1b51eSKate Stone } else { 615af245d11STodd Fiala // ptrace(GETSIGINFO) failed (but not due to group-stop). 616af245d11STodd Fiala 617b9c1b51eSKate Stone // A return value of ESRCH means the thread/process is no longer on the 618a6321a8eSPavel Labath // system, so it was killed somehow outside of our control. Either way, 619a6321a8eSPavel Labath // we can't do anything with it anymore. 620af245d11STodd Fiala 621b9c1b51eSKate Stone // Stop tracking the metadata for the thread since it's entirely off the 622b9c1b51eSKate Stone // system now. 6231107b5a5SPavel Labath const bool thread_found = StopTrackingThread(pid); 624af245d11STodd Fiala 625a6321a8eSPavel Labath LLDB_LOG(log, 626a6321a8eSPavel Labath "GetSignalInfo failed: {0}, tid = {1}, signal = {2}, " 627a6321a8eSPavel Labath "status = {3}, main_thread = {4}, thread_found: {5}", 628a6321a8eSPavel Labath info_err, pid, signal, status, is_main_thread, thread_found); 629af245d11STodd Fiala 630b9c1b51eSKate Stone if (is_main_thread) { 631b9c1b51eSKate Stone // Notify the delegate - our process is not available but appears to 632b9c1b51eSKate Stone // have been killed outside 633af245d11STodd Fiala // our control. Is eStateExited the right exit state in this case? 634*3508fc8cSPavel Labath SetExitStatus(status, true); 6351107b5a5SPavel Labath SetState(StateType::eStateExited, true); 636b9c1b51eSKate Stone } else { 637b9c1b51eSKate Stone // This thread was pulled out from underneath us. Anything to do here? 638b9c1b51eSKate Stone // Do we want to do an all stop? 639a6321a8eSPavel Labath LLDB_LOG(log, 640a6321a8eSPavel Labath "pid {0} tid {1} non-main thread exit occurred, didn't " 641a6321a8eSPavel Labath "tell delegate anything since thread disappeared out " 642a6321a8eSPavel Labath "from underneath us", 643a6321a8eSPavel Labath GetID(), pid); 644af245d11STodd Fiala } 645af245d11STodd Fiala } 646af245d11STodd Fiala } 647af245d11STodd Fiala } 648af245d11STodd Fiala 649b9c1b51eSKate Stone void NativeProcessLinux::WaitForNewThread(::pid_t tid) { 650a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 651426bdf88SPavel Labath 652f9077782SPavel Labath NativeThreadLinuxSP new_thread_sp = GetThreadByID(tid); 653426bdf88SPavel Labath 654b9c1b51eSKate Stone if (new_thread_sp) { 655b9c1b51eSKate Stone // We are already tracking the thread - we got the event on the new thread 656b9c1b51eSKate Stone // (see 657426bdf88SPavel Labath // MonitorSignal) before this one. We are done. 658426bdf88SPavel Labath return; 659426bdf88SPavel Labath } 660426bdf88SPavel Labath 661426bdf88SPavel Labath // The thread is not tracked yet, let's wait for it to appear. 662426bdf88SPavel Labath int status = -1; 663426bdf88SPavel Labath ::pid_t wait_pid; 664b9c1b51eSKate Stone do { 665a6321a8eSPavel Labath LLDB_LOG(log, 666a6321a8eSPavel Labath "received thread creation event for tid {0}. tid not tracked " 667a6321a8eSPavel Labath "yet, waiting for thread to appear...", 668a6321a8eSPavel Labath tid); 669426bdf88SPavel Labath wait_pid = waitpid(tid, &status, __WALL); 670b9c1b51eSKate Stone } while (wait_pid == -1 && errno == EINTR); 671b9c1b51eSKate Stone // Since we are waiting on a specific tid, this must be the creation event. 672a6321a8eSPavel Labath // But let's do some checks just in case. 673426bdf88SPavel Labath if (wait_pid != tid) { 674a6321a8eSPavel Labath LLDB_LOG(log, 675a6321a8eSPavel Labath "waiting for tid {0} failed. Assuming the thread has " 676a6321a8eSPavel Labath "disappeared in the meantime", 677a6321a8eSPavel Labath tid); 678426bdf88SPavel Labath // The only way I know of this could happen is if the whole process was 679b9c1b51eSKate Stone // SIGKILLed in the mean time. In any case, we can't do anything about that 680b9c1b51eSKate Stone // now. 681426bdf88SPavel Labath return; 682426bdf88SPavel Labath } 683b9c1b51eSKate Stone if (WIFEXITED(status)) { 684a6321a8eSPavel Labath LLDB_LOG(log, 685a6321a8eSPavel Labath "waiting for tid {0} returned an 'exited' event. Not " 686a6321a8eSPavel Labath "tracking the thread.", 687a6321a8eSPavel Labath tid); 688426bdf88SPavel Labath // Also a very improbable event. 689426bdf88SPavel Labath return; 690426bdf88SPavel Labath } 691426bdf88SPavel Labath 692a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0}: tracking new thread tid {1}", GetID(), tid); 693f9077782SPavel Labath new_thread_sp = AddThread(tid); 694b9cc0c75SPavel Labath ResumeThread(*new_thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); 695f9077782SPavel Labath ThreadWasCreated(*new_thread_sp); 696426bdf88SPavel Labath } 697426bdf88SPavel Labath 698b9c1b51eSKate Stone void NativeProcessLinux::MonitorSIGTRAP(const siginfo_t &info, 699b9c1b51eSKate Stone NativeThreadLinux &thread) { 700a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 701b9cc0c75SPavel Labath const bool is_main_thread = (thread.GetID() == GetID()); 702af245d11STodd Fiala 703b9cc0c75SPavel Labath assert(info.si_signo == SIGTRAP && "Unexpected child signal!"); 704af245d11STodd Fiala 705b9c1b51eSKate Stone switch (info.si_code) { 706b9c1b51eSKate Stone // TODO: these two cases are required if we want to support tracing of the 707b9c1b51eSKate Stone // inferiors' children. We'd need this to debug a monitor. 708af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)): 709af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)): 710af245d11STodd Fiala 711b9c1b51eSKate Stone case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)): { 712b9c1b51eSKate Stone // This is the notification on the parent thread which informs us of new 713b9c1b51eSKate Stone // thread 714426bdf88SPavel Labath // creation. 715b9c1b51eSKate Stone // We don't want to do anything with the parent thread so we just resume it. 716b9c1b51eSKate Stone // In case we 717b9c1b51eSKate Stone // want to implement "break on thread creation" functionality, we would need 718b9c1b51eSKate Stone // to stop 719426bdf88SPavel Labath // here. 720af245d11STodd Fiala 721af245d11STodd Fiala unsigned long event_message = 0; 722b9c1b51eSKate Stone if (GetEventMessage(thread.GetID(), &event_message).Fail()) { 723a6321a8eSPavel Labath LLDB_LOG(log, 724a6321a8eSPavel Labath "pid {0} received thread creation event but " 725a6321a8eSPavel Labath "GetEventMessage failed so we don't know the new tid", 726a6321a8eSPavel Labath thread.GetID()); 727426bdf88SPavel Labath } else 728426bdf88SPavel Labath WaitForNewThread(event_message); 729af245d11STodd Fiala 730b9cc0c75SPavel Labath ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); 731af245d11STodd Fiala break; 732af245d11STodd Fiala } 733af245d11STodd Fiala 734b9c1b51eSKate Stone case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): { 735f9077782SPavel Labath NativeThreadLinuxSP main_thread_sp; 736a6321a8eSPavel Labath LLDB_LOG(log, "received exec event, code = {0}", info.si_code ^ SIGTRAP); 737a9882ceeSTodd Fiala 7381dbc6c9cSPavel Labath // Exec clears any pending notifications. 7390e1d729bSPavel Labath m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 740fa03ad2eSChaoren Lin 741b9c1b51eSKate Stone // Remove all but the main thread here. Linux fork creates a new process 742b9c1b51eSKate Stone // which only copies the main thread. 743a6321a8eSPavel Labath LLDB_LOG(log, "exec received, stop tracking all but main thread"); 744a9882ceeSTodd Fiala 745b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 746a9882ceeSTodd Fiala const bool is_main_thread = thread_sp && thread_sp->GetID() == GetID(); 747b9c1b51eSKate Stone if (is_main_thread) { 748f9077782SPavel Labath main_thread_sp = std::static_pointer_cast<NativeThreadLinux>(thread_sp); 749a6321a8eSPavel Labath LLDB_LOG(log, "found main thread with tid {0}, keeping", 750a6321a8eSPavel Labath main_thread_sp->GetID()); 751b9c1b51eSKate Stone } else { 752a6321a8eSPavel Labath LLDB_LOG(log, "discarding non-main-thread tid {0} due to exec", 753a6321a8eSPavel Labath thread_sp->GetID()); 754a9882ceeSTodd Fiala } 755a9882ceeSTodd Fiala } 756a9882ceeSTodd Fiala 757a9882ceeSTodd Fiala m_threads.clear(); 758a9882ceeSTodd Fiala 759b9c1b51eSKate Stone if (main_thread_sp) { 760a9882ceeSTodd Fiala m_threads.push_back(main_thread_sp); 761a9882ceeSTodd Fiala SetCurrentThreadID(main_thread_sp->GetID()); 762f9077782SPavel Labath main_thread_sp->SetStoppedByExec(); 763b9c1b51eSKate Stone } else { 764a9882ceeSTodd Fiala SetCurrentThreadID(LLDB_INVALID_THREAD_ID); 765a6321a8eSPavel Labath LLDB_LOG(log, 766a6321a8eSPavel Labath "pid {0} no main thread found, discarded all threads, " 767a6321a8eSPavel Labath "we're in a no-thread state!", 768a6321a8eSPavel Labath GetID()); 769a9882ceeSTodd Fiala } 770a9882ceeSTodd Fiala 771fa03ad2eSChaoren Lin // Tell coordinator about about the "new" (since exec) stopped main thread. 772f9077782SPavel Labath ThreadWasCreated(*main_thread_sp); 773fa03ad2eSChaoren Lin 774a9882ceeSTodd Fiala // Let our delegate know we have just exec'd. 775a9882ceeSTodd Fiala NotifyDidExec(); 776a9882ceeSTodd Fiala 777a9882ceeSTodd Fiala // If we have a main thread, indicate we are stopped. 778b9c1b51eSKate Stone assert(main_thread_sp && "exec called during ptraced process but no main " 779b9c1b51eSKate Stone "thread metadata tracked"); 780fa03ad2eSChaoren Lin 781fa03ad2eSChaoren Lin // Let the process know we're stopped. 782b9cc0c75SPavel Labath StopRunningThreads(main_thread_sp->GetID()); 783a9882ceeSTodd Fiala 784af245d11STodd Fiala break; 785a9882ceeSTodd Fiala } 786af245d11STodd Fiala 787b9c1b51eSKate Stone case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): { 788af245d11STodd Fiala // The inferior process or one of its threads is about to exit. 789b9c1b51eSKate Stone // We don't want to do anything with the thread so we just resume it. In 790b9c1b51eSKate Stone // case we 791b9c1b51eSKate Stone // want to implement "break on thread exit" functionality, we would need to 792b9c1b51eSKate Stone // stop 7936e35163cSPavel Labath // here. 794fa03ad2eSChaoren Lin 795af245d11STodd Fiala unsigned long data = 0; 796b9cc0c75SPavel Labath if (GetEventMessage(thread.GetID(), &data).Fail()) 797af245d11STodd Fiala data = -1; 798af245d11STodd Fiala 799a6321a8eSPavel Labath LLDB_LOG(log, 800a6321a8eSPavel Labath "received PTRACE_EVENT_EXIT, data = {0:x}, WIFEXITED={1}, " 801a6321a8eSPavel Labath "WIFSIGNALED={2}, pid = {3}, main_thread = {4}", 802a6321a8eSPavel Labath data, WIFEXITED(data), WIFSIGNALED(data), thread.GetID(), 803a6321a8eSPavel Labath is_main_thread); 804af245d11STodd Fiala 805*3508fc8cSPavel Labath if (is_main_thread) 806*3508fc8cSPavel Labath SetExitStatus(WaitStatus::Decode(data), true); 80775f47c3aSTodd Fiala 80886852d36SPavel Labath StateType state = thread.GetState(); 809b9c1b51eSKate Stone if (!StateIsRunningState(state)) { 810b9c1b51eSKate Stone // Due to a kernel bug, we may sometimes get this stop after the inferior 811b9c1b51eSKate Stone // gets a 812b9c1b51eSKate Stone // SIGKILL. This confuses our state tracking logic in ResumeThread(), 813b9c1b51eSKate Stone // since normally, 814b9c1b51eSKate Stone // we should not be receiving any ptrace events while the inferior is 815b9c1b51eSKate Stone // stopped. This 81686852d36SPavel Labath // makes sure that the inferior is resumed and exits normally. 81786852d36SPavel Labath state = eStateRunning; 81886852d36SPavel Labath } 81986852d36SPavel Labath ResumeThread(thread, state, LLDB_INVALID_SIGNAL_NUMBER); 820af245d11STodd Fiala 821af245d11STodd Fiala break; 822af245d11STodd Fiala } 823af245d11STodd Fiala 824af245d11STodd Fiala case 0: 825c16f5dcaSChaoren Lin case TRAP_TRACE: // We receive this on single stepping. 826c16f5dcaSChaoren Lin case TRAP_HWBKPT: // We receive this on watchpoint hit 82786fd8e45SChaoren Lin { 828c16f5dcaSChaoren Lin // If a watchpoint was hit, report it 829c16f5dcaSChaoren Lin uint32_t wp_index; 83097206d57SZachary Turner Status error = thread.GetRegisterContext()->GetWatchpointHitIndex( 831b9c1b51eSKate Stone wp_index, (uintptr_t)info.si_addr); 832a6321a8eSPavel Labath if (error.Fail()) 833a6321a8eSPavel Labath LLDB_LOG(log, 834a6321a8eSPavel Labath "received error while checking for watchpoint hits, pid = " 835a6321a8eSPavel Labath "{0}, error = {1}", 836a6321a8eSPavel Labath thread.GetID(), error); 837b9c1b51eSKate Stone if (wp_index != LLDB_INVALID_INDEX32) { 838b9cc0c75SPavel Labath MonitorWatchpoint(thread, wp_index); 839c16f5dcaSChaoren Lin break; 840c16f5dcaSChaoren Lin } 841b9cc0c75SPavel Labath 842d5ffbad2SOmair Javaid // If a breakpoint was hit, report it 843d5ffbad2SOmair Javaid uint32_t bp_index; 844d5ffbad2SOmair Javaid error = thread.GetRegisterContext()->GetHardwareBreakHitIndex( 845d5ffbad2SOmair Javaid bp_index, (uintptr_t)info.si_addr); 846d5ffbad2SOmair Javaid if (error.Fail()) 847d5ffbad2SOmair Javaid LLDB_LOG(log, "received error while checking for hardware " 848d5ffbad2SOmair Javaid "breakpoint hits, pid = {0}, error = {1}", 849d5ffbad2SOmair Javaid thread.GetID(), error); 850d5ffbad2SOmair Javaid if (bp_index != LLDB_INVALID_INDEX32) { 851d5ffbad2SOmair Javaid MonitorBreakpoint(thread); 852d5ffbad2SOmair Javaid break; 853d5ffbad2SOmair Javaid } 854d5ffbad2SOmair Javaid 855be379e15STamas Berghammer // Otherwise, report step over 856be379e15STamas Berghammer MonitorTrace(thread); 857af245d11STodd Fiala break; 858b9cc0c75SPavel Labath } 859af245d11STodd Fiala 860af245d11STodd Fiala case SI_KERNEL: 86135799963SMohit K. Bhakkad #if defined __mips__ 86235799963SMohit K. Bhakkad // For mips there is no special signal for watchpoint 86335799963SMohit K. Bhakkad // So we check for watchpoint in kernel trap 86435799963SMohit K. Bhakkad { 86535799963SMohit K. Bhakkad // If a watchpoint was hit, report it 86635799963SMohit K. Bhakkad uint32_t wp_index; 86797206d57SZachary Turner Status error = thread.GetRegisterContext()->GetWatchpointHitIndex( 868b9c1b51eSKate Stone wp_index, LLDB_INVALID_ADDRESS); 869a6321a8eSPavel Labath if (error.Fail()) 870a6321a8eSPavel Labath LLDB_LOG(log, 871a6321a8eSPavel Labath "received error while checking for watchpoint hits, pid = " 872a6321a8eSPavel Labath "{0}, error = {1}", 873a6321a8eSPavel Labath thread.GetID(), error); 874b9c1b51eSKate Stone if (wp_index != LLDB_INVALID_INDEX32) { 875b9cc0c75SPavel Labath MonitorWatchpoint(thread, wp_index); 87635799963SMohit K. Bhakkad break; 87735799963SMohit K. Bhakkad } 87835799963SMohit K. Bhakkad } 87935799963SMohit K. Bhakkad // NO BREAK 88035799963SMohit K. Bhakkad #endif 881af245d11STodd Fiala case TRAP_BRKPT: 882b9cc0c75SPavel Labath MonitorBreakpoint(thread); 883af245d11STodd Fiala break; 884af245d11STodd Fiala 885af245d11STodd Fiala case SIGTRAP: 886af245d11STodd Fiala case (SIGTRAP | 0x80): 887a6321a8eSPavel Labath LLDB_LOG( 888a6321a8eSPavel Labath log, 889a6321a8eSPavel Labath "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming", 890a6321a8eSPavel Labath info.si_code, GetID(), thread.GetID()); 891fa03ad2eSChaoren Lin 892af245d11STodd Fiala // Ignore these signals until we know more about them. 893b9cc0c75SPavel Labath ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); 894af245d11STodd Fiala break; 895af245d11STodd Fiala 896af245d11STodd Fiala default: 897a6321a8eSPavel Labath LLDB_LOG( 898a6321a8eSPavel Labath log, 899a6321a8eSPavel Labath "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming", 900a6321a8eSPavel Labath info.si_code, GetID(), thread.GetID()); 901a6321a8eSPavel Labath llvm_unreachable("Unexpected SIGTRAP code!"); 902af245d11STodd Fiala break; 903af245d11STodd Fiala } 904af245d11STodd Fiala } 905af245d11STodd Fiala 906b9c1b51eSKate Stone void NativeProcessLinux::MonitorTrace(NativeThreadLinux &thread) { 907a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 908a6321a8eSPavel Labath LLDB_LOG(log, "received trace event, pid = {0}", thread.GetID()); 909c16f5dcaSChaoren Lin 9100e1d729bSPavel Labath // This thread is currently stopped. 911b9cc0c75SPavel Labath thread.SetStoppedByTrace(); 912c16f5dcaSChaoren Lin 913b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 914c16f5dcaSChaoren Lin } 915c16f5dcaSChaoren Lin 916b9c1b51eSKate Stone void NativeProcessLinux::MonitorBreakpoint(NativeThreadLinux &thread) { 917b9c1b51eSKate Stone Log *log( 918b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 919a6321a8eSPavel Labath LLDB_LOG(log, "received breakpoint event, pid = {0}", thread.GetID()); 920c16f5dcaSChaoren Lin 921c16f5dcaSChaoren Lin // Mark the thread as stopped at breakpoint. 922b9cc0c75SPavel Labath thread.SetStoppedByBreakpoint(); 92397206d57SZachary Turner Status error = FixupBreakpointPCAsNeeded(thread); 924c16f5dcaSChaoren Lin if (error.Fail()) 925a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0} fixup: {1}", thread.GetID(), error); 926d8c338d4STamas Berghammer 927b9c1b51eSKate Stone if (m_threads_stepping_with_breakpoint.find(thread.GetID()) != 928b9c1b51eSKate Stone m_threads_stepping_with_breakpoint.end()) 929b9cc0c75SPavel Labath thread.SetStoppedByTrace(); 930c16f5dcaSChaoren Lin 931b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 932c16f5dcaSChaoren Lin } 933c16f5dcaSChaoren Lin 934b9c1b51eSKate Stone void NativeProcessLinux::MonitorWatchpoint(NativeThreadLinux &thread, 935b9c1b51eSKate Stone uint32_t wp_index) { 936b9c1b51eSKate Stone Log *log( 937b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_WATCHPOINTS)); 938a6321a8eSPavel Labath LLDB_LOG(log, "received watchpoint event, pid = {0}, wp_index = {1}", 939a6321a8eSPavel Labath thread.GetID(), wp_index); 940c16f5dcaSChaoren Lin 941c16f5dcaSChaoren Lin // Mark the thread as stopped at watchpoint. 942c16f5dcaSChaoren Lin // The address is at (lldb::addr_t)info->si_addr if we need it. 943f9077782SPavel Labath thread.SetStoppedByWatchpoint(wp_index); 944c16f5dcaSChaoren Lin 945b9c1b51eSKate Stone // We need to tell all other running threads before we notify the delegate 946b9c1b51eSKate Stone // about this stop. 947f9077782SPavel Labath StopRunningThreads(thread.GetID()); 948c16f5dcaSChaoren Lin } 949c16f5dcaSChaoren Lin 950b9c1b51eSKate Stone void NativeProcessLinux::MonitorSignal(const siginfo_t &info, 951b9c1b51eSKate Stone NativeThreadLinux &thread, bool exited) { 952b9cc0c75SPavel Labath const int signo = info.si_signo; 953b9cc0c75SPavel Labath const bool is_from_llgs = info.si_pid == getpid(); 954af245d11STodd Fiala 955a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 956af245d11STodd Fiala 957af245d11STodd Fiala // POSIX says that process behaviour is undefined after it ignores a SIGFPE, 958af245d11STodd Fiala // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a 959af245d11STodd Fiala // kill(2) or raise(3). Similarly for tgkill(2) on Linux. 960af245d11STodd Fiala // 961af245d11STodd Fiala // IOW, user generated signals never generate what we consider to be a 962af245d11STodd Fiala // "crash". 963af245d11STodd Fiala // 964af245d11STodd Fiala // Similarly, ACK signals generated by this monitor. 965af245d11STodd Fiala 966af245d11STodd Fiala // Handle the signal. 967a6321a8eSPavel Labath LLDB_LOG(log, 968a6321a8eSPavel Labath "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, " 969a6321a8eSPavel Labath "waitpid pid = {4})", 970a6321a8eSPavel Labath Host::GetSignalAsCString(signo), signo, info.si_code, 971b9cc0c75SPavel Labath thread.GetID()); 97258a2f669STodd Fiala 97358a2f669STodd Fiala // Check for thread stop notification. 974b9c1b51eSKate Stone if (is_from_llgs && (info.si_code == SI_TKILL) && (signo == SIGSTOP)) { 975af245d11STodd Fiala // This is a tgkill()-based stop. 976a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} tid {1}, thread stopped", GetID(), thread.GetID()); 977fa03ad2eSChaoren Lin 978aab58633SChaoren Lin // Check that we're not already marked with a stop reason. 979b9c1b51eSKate Stone // Note this thread really shouldn't already be marked as stopped - if we 980a6321a8eSPavel Labath // were, that would imply that the kernel signaled us with the thread 981a6321a8eSPavel Labath // stopping which we handled and marked as stopped, and that, without an 982a6321a8eSPavel Labath // intervening resume, we received another stop. It is more likely that we 983a6321a8eSPavel Labath // are missing the marking of a run state somewhere if we find that the 984a6321a8eSPavel Labath // thread was marked as stopped. 985b9cc0c75SPavel Labath const StateType thread_state = thread.GetState(); 986b9c1b51eSKate Stone if (!StateIsStoppedState(thread_state, false)) { 987ed89c7feSPavel Labath // An inferior thread has stopped because of a SIGSTOP we have sent it. 988b9c1b51eSKate Stone // Generally, these are not important stops and we don't want to report 989a6321a8eSPavel Labath // them as they are just used to stop other threads when one thread (the 990a6321a8eSPavel Labath // one with the *real* stop reason) hits a breakpoint (watchpoint, 991a6321a8eSPavel Labath // etc...). However, in the case of an asynchronous Interrupt(), this *is* 992a6321a8eSPavel Labath // the real stop reason, so we leave the signal intact if this is the 993a6321a8eSPavel Labath // thread that was chosen as the triggering thread. 994b9c1b51eSKate Stone if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) { 995b9cc0c75SPavel Labath if (m_pending_notification_tid == thread.GetID()) 996b9cc0c75SPavel Labath thread.SetStoppedBySignal(SIGSTOP, &info); 997ed89c7feSPavel Labath else 998b9cc0c75SPavel Labath thread.SetStoppedWithNoReason(); 999ed89c7feSPavel Labath 1000b9cc0c75SPavel Labath SetCurrentThreadID(thread.GetID()); 10010e1d729bSPavel Labath SignalIfAllThreadsStopped(); 1002b9c1b51eSKate Stone } else { 10030e1d729bSPavel Labath // We can end up here if stop was initiated by LLGS but by this time a 10040e1d729bSPavel Labath // thread stop has occurred - maybe initiated by another event. 100597206d57SZachary Turner Status error = ResumeThread(thread, thread.GetState(), 0); 1006a6321a8eSPavel Labath if (error.Fail()) 1007a6321a8eSPavel Labath LLDB_LOG(log, "failed to resume thread {0}: {1}", thread.GetID(), 1008a6321a8eSPavel Labath error); 10090e1d729bSPavel Labath } 1010b9c1b51eSKate Stone } else { 1011a6321a8eSPavel Labath LLDB_LOG(log, 1012a6321a8eSPavel Labath "pid {0} tid {1}, thread was already marked as a stopped " 1013a6321a8eSPavel Labath "state (state={2}), leaving stop signal as is", 10148198db30SPavel Labath GetID(), thread.GetID(), thread_state); 10150e1d729bSPavel Labath SignalIfAllThreadsStopped(); 1016af245d11STodd Fiala } 1017af245d11STodd Fiala 101858a2f669STodd Fiala // Done handling. 1019af245d11STodd Fiala return; 1020af245d11STodd Fiala } 1021af245d11STodd Fiala 10224a705e7eSPavel Labath // Check if debugger should stop at this signal or just ignore it 10234a705e7eSPavel Labath // and resume the inferior. 10244a705e7eSPavel Labath if (m_signals_to_ignore.find(signo) != m_signals_to_ignore.end()) { 10254a705e7eSPavel Labath ResumeThread(thread, thread.GetState(), signo); 10264a705e7eSPavel Labath return; 10274a705e7eSPavel Labath } 10284a705e7eSPavel Labath 102986fd8e45SChaoren Lin // This thread is stopped. 1030a6321a8eSPavel Labath LLDB_LOG(log, "received signal {0}", Host::GetSignalAsCString(signo)); 1031b9cc0c75SPavel Labath thread.SetStoppedBySignal(signo, &info); 103286fd8e45SChaoren Lin 103386fd8e45SChaoren Lin // Send a stop to the debugger after we get all other threads to stop. 1034b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 1035511e5cdcSTodd Fiala } 1036af245d11STodd Fiala 1037e7708688STamas Berghammer namespace { 1038e7708688STamas Berghammer 1039b9c1b51eSKate Stone struct EmulatorBaton { 1040e7708688STamas Berghammer NativeProcessLinux *m_process; 1041e7708688STamas Berghammer NativeRegisterContext *m_reg_context; 10426648fcc3SPavel Labath 10436648fcc3SPavel Labath // eRegisterKindDWARF -> RegsiterValue 10446648fcc3SPavel Labath std::unordered_map<uint32_t, RegisterValue> m_register_values; 1045e7708688STamas Berghammer 1046b9c1b51eSKate Stone EmulatorBaton(NativeProcessLinux *process, NativeRegisterContext *reg_context) 1047b9c1b51eSKate Stone : m_process(process), m_reg_context(reg_context) {} 1048e7708688STamas Berghammer }; 1049e7708688STamas Berghammer 1050e7708688STamas Berghammer } // anonymous namespace 1051e7708688STamas Berghammer 1052b9c1b51eSKate Stone static size_t ReadMemoryCallback(EmulateInstruction *instruction, void *baton, 1053e7708688STamas Berghammer const EmulateInstruction::Context &context, 1054b9c1b51eSKate Stone lldb::addr_t addr, void *dst, size_t length) { 1055e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 1056e7708688STamas Berghammer 10573eb4b458SChaoren Lin size_t bytes_read; 1058e7708688STamas Berghammer emulator_baton->m_process->ReadMemory(addr, dst, length, bytes_read); 1059e7708688STamas Berghammer return bytes_read; 1060e7708688STamas Berghammer } 1061e7708688STamas Berghammer 1062b9c1b51eSKate Stone static bool ReadRegisterCallback(EmulateInstruction *instruction, void *baton, 1063e7708688STamas Berghammer const RegisterInfo *reg_info, 1064b9c1b51eSKate Stone RegisterValue ®_value) { 1065e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 1066e7708688STamas Berghammer 1067b9c1b51eSKate Stone auto it = emulator_baton->m_register_values.find( 1068b9c1b51eSKate Stone reg_info->kinds[eRegisterKindDWARF]); 1069b9c1b51eSKate Stone if (it != emulator_baton->m_register_values.end()) { 10706648fcc3SPavel Labath reg_value = it->second; 10716648fcc3SPavel Labath return true; 10726648fcc3SPavel Labath } 10736648fcc3SPavel Labath 1074e7708688STamas Berghammer // The emulator only fill in the dwarf regsiter numbers (and in some case 1075e7708688STamas Berghammer // the generic register numbers). Get the full register info from the 1076e7708688STamas Berghammer // register context based on the dwarf register numbers. 1077b9c1b51eSKate Stone const RegisterInfo *full_reg_info = 1078b9c1b51eSKate Stone emulator_baton->m_reg_context->GetRegisterInfo( 1079e7708688STamas Berghammer eRegisterKindDWARF, reg_info->kinds[eRegisterKindDWARF]); 1080e7708688STamas Berghammer 108197206d57SZachary Turner Status error = 1082b9c1b51eSKate Stone emulator_baton->m_reg_context->ReadRegister(full_reg_info, reg_value); 10836648fcc3SPavel Labath if (error.Success()) 10846648fcc3SPavel Labath return true; 1085cdc22a88SMohit K. Bhakkad 10866648fcc3SPavel Labath return false; 1087e7708688STamas Berghammer } 1088e7708688STamas Berghammer 1089b9c1b51eSKate Stone static bool WriteRegisterCallback(EmulateInstruction *instruction, void *baton, 1090e7708688STamas Berghammer const EmulateInstruction::Context &context, 1091e7708688STamas Berghammer const RegisterInfo *reg_info, 1092b9c1b51eSKate Stone const RegisterValue ®_value) { 1093e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 1094b9c1b51eSKate Stone emulator_baton->m_register_values[reg_info->kinds[eRegisterKindDWARF]] = 1095b9c1b51eSKate Stone reg_value; 1096e7708688STamas Berghammer return true; 1097e7708688STamas Berghammer } 1098e7708688STamas Berghammer 1099b9c1b51eSKate Stone static size_t WriteMemoryCallback(EmulateInstruction *instruction, void *baton, 1100e7708688STamas Berghammer const EmulateInstruction::Context &context, 1101b9c1b51eSKate Stone lldb::addr_t addr, const void *dst, 1102b9c1b51eSKate Stone size_t length) { 1103e7708688STamas Berghammer return length; 1104e7708688STamas Berghammer } 1105e7708688STamas Berghammer 1106b9c1b51eSKate Stone static lldb::addr_t ReadFlags(NativeRegisterContext *regsiter_context) { 1107e7708688STamas Berghammer const RegisterInfo *flags_info = regsiter_context->GetRegisterInfo( 1108e7708688STamas Berghammer eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 1109b9c1b51eSKate Stone return regsiter_context->ReadRegisterAsUnsigned(flags_info, 1110b9c1b51eSKate Stone LLDB_INVALID_ADDRESS); 1111e7708688STamas Berghammer } 1112e7708688STamas Berghammer 111397206d57SZachary Turner Status 111497206d57SZachary Turner NativeProcessLinux::SetupSoftwareSingleStepping(NativeThreadLinux &thread) { 111597206d57SZachary Turner Status error; 1116b9cc0c75SPavel Labath NativeRegisterContextSP register_context_sp = thread.GetRegisterContext(); 1117e7708688STamas Berghammer 1118e7708688STamas Berghammer std::unique_ptr<EmulateInstruction> emulator_ap( 1119b9c1b51eSKate Stone EmulateInstruction::FindPlugin(m_arch, eInstructionTypePCModifying, 1120b9c1b51eSKate Stone nullptr)); 1121e7708688STamas Berghammer 1122e7708688STamas Berghammer if (emulator_ap == nullptr) 112397206d57SZachary Turner return Status("Instruction emulator not found!"); 1124e7708688STamas Berghammer 1125e7708688STamas Berghammer EmulatorBaton baton(this, register_context_sp.get()); 1126e7708688STamas Berghammer emulator_ap->SetBaton(&baton); 1127e7708688STamas Berghammer emulator_ap->SetReadMemCallback(&ReadMemoryCallback); 1128e7708688STamas Berghammer emulator_ap->SetReadRegCallback(&ReadRegisterCallback); 1129e7708688STamas Berghammer emulator_ap->SetWriteMemCallback(&WriteMemoryCallback); 1130e7708688STamas Berghammer emulator_ap->SetWriteRegCallback(&WriteRegisterCallback); 1131e7708688STamas Berghammer 1132e7708688STamas Berghammer if (!emulator_ap->ReadInstruction()) 113397206d57SZachary Turner return Status("Read instruction failed!"); 1134e7708688STamas Berghammer 1135b9c1b51eSKate Stone bool emulation_result = 1136b9c1b51eSKate Stone emulator_ap->EvaluateInstruction(eEmulateInstructionOptionAutoAdvancePC); 11376648fcc3SPavel Labath 1138b9c1b51eSKate Stone const RegisterInfo *reg_info_pc = register_context_sp->GetRegisterInfo( 1139b9c1b51eSKate Stone eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); 1140b9c1b51eSKate Stone const RegisterInfo *reg_info_flags = register_context_sp->GetRegisterInfo( 1141b9c1b51eSKate Stone eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 11426648fcc3SPavel Labath 1143b9c1b51eSKate Stone auto pc_it = 1144b9c1b51eSKate Stone baton.m_register_values.find(reg_info_pc->kinds[eRegisterKindDWARF]); 1145b9c1b51eSKate Stone auto flags_it = 1146b9c1b51eSKate Stone baton.m_register_values.find(reg_info_flags->kinds[eRegisterKindDWARF]); 11476648fcc3SPavel Labath 1148e7708688STamas Berghammer lldb::addr_t next_pc; 1149e7708688STamas Berghammer lldb::addr_t next_flags; 1150b9c1b51eSKate Stone if (emulation_result) { 1151b9c1b51eSKate Stone assert(pc_it != baton.m_register_values.end() && 1152b9c1b51eSKate Stone "Emulation was successfull but PC wasn't updated"); 11536648fcc3SPavel Labath next_pc = pc_it->second.GetAsUInt64(); 11546648fcc3SPavel Labath 11556648fcc3SPavel Labath if (flags_it != baton.m_register_values.end()) 11566648fcc3SPavel Labath next_flags = flags_it->second.GetAsUInt64(); 1157e7708688STamas Berghammer else 1158e7708688STamas Berghammer next_flags = ReadFlags(register_context_sp.get()); 1159b9c1b51eSKate Stone } else if (pc_it == baton.m_register_values.end()) { 1160e7708688STamas Berghammer // Emulate instruction failed and it haven't changed PC. Advance PC 1161e7708688STamas Berghammer // with the size of the current opcode because the emulation of all 1162e7708688STamas Berghammer // PC modifying instruction should be successful. The failure most 1163e7708688STamas Berghammer // likely caused by a not supported instruction which don't modify PC. 1164b9c1b51eSKate Stone next_pc = 1165b9c1b51eSKate Stone register_context_sp->GetPC() + emulator_ap->GetOpcode().GetByteSize(); 1166e7708688STamas Berghammer next_flags = ReadFlags(register_context_sp.get()); 1167b9c1b51eSKate Stone } else { 1168e7708688STamas Berghammer // The instruction emulation failed after it modified the PC. It is an 1169e7708688STamas Berghammer // unknown error where we can't continue because the next instruction is 1170e7708688STamas Berghammer // modifying the PC but we don't know how. 117197206d57SZachary Turner return Status("Instruction emulation failed unexpectedly."); 1172e7708688STamas Berghammer } 1173e7708688STamas Berghammer 1174b9c1b51eSKate Stone if (m_arch.GetMachine() == llvm::Triple::arm) { 1175b9c1b51eSKate Stone if (next_flags & 0x20) { 1176e7708688STamas Berghammer // Thumb mode 1177e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 2); 1178b9c1b51eSKate Stone } else { 1179e7708688STamas Berghammer // Arm mode 1180e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 4); 1181e7708688STamas Berghammer } 1182b9c1b51eSKate Stone } else if (m_arch.GetMachine() == llvm::Triple::mips64 || 1183b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64el || 1184b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips || 1185b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mipsel) 1186cdc22a88SMohit K. Bhakkad error = SetSoftwareBreakpoint(next_pc, 4); 1187b9c1b51eSKate Stone else { 1188e7708688STamas Berghammer // No size hint is given for the next breakpoint 1189e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 0); 1190e7708688STamas Berghammer } 1191e7708688STamas Berghammer 119242eb6908SPavel Labath // If setting the breakpoint fails because next_pc is out of 119342eb6908SPavel Labath // the address space, ignore it and let the debugee segfault. 119442eb6908SPavel Labath if (error.GetError() == EIO || error.GetError() == EFAULT) { 119597206d57SZachary Turner return Status(); 119642eb6908SPavel Labath } else if (error.Fail()) 1197e7708688STamas Berghammer return error; 1198e7708688STamas Berghammer 1199b9cc0c75SPavel Labath m_threads_stepping_with_breakpoint.insert({thread.GetID(), next_pc}); 1200e7708688STamas Berghammer 120197206d57SZachary Turner return Status(); 1202e7708688STamas Berghammer } 1203e7708688STamas Berghammer 1204b9c1b51eSKate Stone bool NativeProcessLinux::SupportHardwareSingleStepping() const { 1205b9c1b51eSKate Stone if (m_arch.GetMachine() == llvm::Triple::arm || 1206b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64 || 1207b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64el || 1208b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips || 1209b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mipsel) 1210cdc22a88SMohit K. Bhakkad return false; 1211cdc22a88SMohit K. Bhakkad return true; 1212e7708688STamas Berghammer } 1213e7708688STamas Berghammer 121497206d57SZachary Turner Status NativeProcessLinux::Resume(const ResumeActionList &resume_actions) { 1215a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1216a6321a8eSPavel Labath LLDB_LOG(log, "pid {0}", GetID()); 1217af245d11STodd Fiala 1218e7708688STamas Berghammer bool software_single_step = !SupportHardwareSingleStepping(); 1219af245d11STodd Fiala 1220b9c1b51eSKate Stone if (software_single_step) { 1221b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 1222e7708688STamas Berghammer assert(thread_sp && "thread list should not contain NULL threads"); 1223e7708688STamas Berghammer 1224b9c1b51eSKate Stone const ResumeAction *const action = 1225b9c1b51eSKate Stone resume_actions.GetActionForThread(thread_sp->GetID(), true); 1226e7708688STamas Berghammer if (action == nullptr) 1227e7708688STamas Berghammer continue; 1228e7708688STamas Berghammer 1229b9c1b51eSKate Stone if (action->state == eStateStepping) { 123097206d57SZachary Turner Status error = SetupSoftwareSingleStepping( 1231b9c1b51eSKate Stone static_cast<NativeThreadLinux &>(*thread_sp)); 1232e7708688STamas Berghammer if (error.Fail()) 1233e7708688STamas Berghammer return error; 1234e7708688STamas Berghammer } 1235e7708688STamas Berghammer } 1236e7708688STamas Berghammer } 1237e7708688STamas Berghammer 1238b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 1239af245d11STodd Fiala assert(thread_sp && "thread list should not contain NULL threads"); 1240af245d11STodd Fiala 1241b9c1b51eSKate Stone const ResumeAction *const action = 1242b9c1b51eSKate Stone resume_actions.GetActionForThread(thread_sp->GetID(), true); 12436a196ce6SChaoren Lin 1244b9c1b51eSKate Stone if (action == nullptr) { 1245a6321a8eSPavel Labath LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(), 1246a6321a8eSPavel Labath thread_sp->GetID()); 12476a196ce6SChaoren Lin continue; 12486a196ce6SChaoren Lin } 1249af245d11STodd Fiala 1250a6321a8eSPavel Labath LLDB_LOG(log, "processing resume action state {0} for pid {1} tid {2}", 12518198db30SPavel Labath action->state, GetID(), thread_sp->GetID()); 1252af245d11STodd Fiala 1253b9c1b51eSKate Stone switch (action->state) { 1254af245d11STodd Fiala case eStateRunning: 1255b9c1b51eSKate Stone case eStateStepping: { 1256af245d11STodd Fiala // Run the thread, possibly feeding it the signal. 1257fa03ad2eSChaoren Lin const int signo = action->signal; 1258b9c1b51eSKate Stone ResumeThread(static_cast<NativeThreadLinux &>(*thread_sp), action->state, 1259b9c1b51eSKate Stone signo); 1260af245d11STodd Fiala break; 1261ae29d395SChaoren Lin } 1262af245d11STodd Fiala 1263af245d11STodd Fiala case eStateSuspended: 1264af245d11STodd Fiala case eStateStopped: 1265a6321a8eSPavel Labath llvm_unreachable("Unexpected state"); 1266af245d11STodd Fiala 1267af245d11STodd Fiala default: 126897206d57SZachary Turner return Status("NativeProcessLinux::%s (): unexpected state %s specified " 1269b9c1b51eSKate Stone "for pid %" PRIu64 ", tid %" PRIu64, 1270b9c1b51eSKate Stone __FUNCTION__, StateAsCString(action->state), GetID(), 1271b9c1b51eSKate Stone thread_sp->GetID()); 1272af245d11STodd Fiala } 1273af245d11STodd Fiala } 1274af245d11STodd Fiala 127597206d57SZachary Turner return Status(); 1276af245d11STodd Fiala } 1277af245d11STodd Fiala 127897206d57SZachary Turner Status NativeProcessLinux::Halt() { 127997206d57SZachary Turner Status error; 1280af245d11STodd Fiala 1281af245d11STodd Fiala if (kill(GetID(), SIGSTOP) != 0) 1282af245d11STodd Fiala error.SetErrorToErrno(); 1283af245d11STodd Fiala 1284af245d11STodd Fiala return error; 1285af245d11STodd Fiala } 1286af245d11STodd Fiala 128797206d57SZachary Turner Status NativeProcessLinux::Detach() { 128897206d57SZachary Turner Status error; 1289af245d11STodd Fiala 1290af245d11STodd Fiala // Stop monitoring the inferior. 129119cbe96aSPavel Labath m_sigchld_handle.reset(); 1292af245d11STodd Fiala 12937a9495bcSPavel Labath // Tell ptrace to detach from the process. 12947a9495bcSPavel Labath if (GetID() == LLDB_INVALID_PROCESS_ID) 12957a9495bcSPavel Labath return error; 12967a9495bcSPavel Labath 1297b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 129897206d57SZachary Turner Status e = Detach(thread_sp->GetID()); 12997a9495bcSPavel Labath if (e.Fail()) 1300b9c1b51eSKate Stone error = 1301b9c1b51eSKate Stone e; // Save the error, but still attempt to detach from other threads. 13027a9495bcSPavel Labath } 13037a9495bcSPavel Labath 1304af245d11STodd Fiala return error; 1305af245d11STodd Fiala } 1306af245d11STodd Fiala 130797206d57SZachary Turner Status NativeProcessLinux::Signal(int signo) { 130897206d57SZachary Turner Status error; 1309af245d11STodd Fiala 1310a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1311a6321a8eSPavel Labath LLDB_LOG(log, "sending signal {0} ({1}) to pid {1}", signo, 1312a6321a8eSPavel Labath Host::GetSignalAsCString(signo), GetID()); 1313af245d11STodd Fiala 1314af245d11STodd Fiala if (kill(GetID(), signo)) 1315af245d11STodd Fiala error.SetErrorToErrno(); 1316af245d11STodd Fiala 1317af245d11STodd Fiala return error; 1318af245d11STodd Fiala } 1319af245d11STodd Fiala 132097206d57SZachary Turner Status NativeProcessLinux::Interrupt() { 1321e9547b80SChaoren Lin // Pick a running thread (or if none, a not-dead stopped thread) as 1322e9547b80SChaoren Lin // the chosen thread that will be the stop-reason thread. 1323a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1324e9547b80SChaoren Lin 1325e9547b80SChaoren Lin NativeThreadProtocolSP running_thread_sp; 1326e9547b80SChaoren Lin NativeThreadProtocolSP stopped_thread_sp; 1327e9547b80SChaoren Lin 1328a6321a8eSPavel Labath LLDB_LOG(log, "selecting running thread for interrupt target"); 1329b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 1330e9547b80SChaoren Lin // The thread shouldn't be null but lets just cover that here. 1331e9547b80SChaoren Lin if (!thread_sp) 1332e9547b80SChaoren Lin continue; 1333e9547b80SChaoren Lin 1334e9547b80SChaoren Lin // If we have a running or stepping thread, we'll call that the 1335e9547b80SChaoren Lin // target of the interrupt. 1336e9547b80SChaoren Lin const auto thread_state = thread_sp->GetState(); 1337b9c1b51eSKate Stone if (thread_state == eStateRunning || thread_state == eStateStepping) { 1338e9547b80SChaoren Lin running_thread_sp = thread_sp; 1339e9547b80SChaoren Lin break; 1340b9c1b51eSKate Stone } else if (!stopped_thread_sp && StateIsStoppedState(thread_state, true)) { 1341b9c1b51eSKate Stone // Remember the first non-dead stopped thread. We'll use that as a backup 1342b9c1b51eSKate Stone // if there are no running threads. 1343e9547b80SChaoren Lin stopped_thread_sp = thread_sp; 1344e9547b80SChaoren Lin } 1345e9547b80SChaoren Lin } 1346e9547b80SChaoren Lin 1347b9c1b51eSKate Stone if (!running_thread_sp && !stopped_thread_sp) { 134897206d57SZachary Turner Status error("found no running/stepping or live stopped threads as target " 1349b9c1b51eSKate Stone "for interrupt"); 1350a6321a8eSPavel Labath LLDB_LOG(log, "skipping due to error: {0}", error); 13515830aa75STamas Berghammer 1352e9547b80SChaoren Lin return error; 1353e9547b80SChaoren Lin } 1354e9547b80SChaoren Lin 1355b9c1b51eSKate Stone NativeThreadProtocolSP deferred_signal_thread_sp = 1356b9c1b51eSKate Stone running_thread_sp ? running_thread_sp : stopped_thread_sp; 1357e9547b80SChaoren Lin 1358a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} {1} tid {2} chosen for interrupt target", GetID(), 1359e9547b80SChaoren Lin running_thread_sp ? "running" : "stopped", 1360e9547b80SChaoren Lin deferred_signal_thread_sp->GetID()); 1361e9547b80SChaoren Lin 1362ed89c7feSPavel Labath StopRunningThreads(deferred_signal_thread_sp->GetID()); 136345f5cb31SPavel Labath 136497206d57SZachary Turner return Status(); 1365e9547b80SChaoren Lin } 1366e9547b80SChaoren Lin 136797206d57SZachary Turner Status NativeProcessLinux::Kill() { 1368a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1369a6321a8eSPavel Labath LLDB_LOG(log, "pid {0}", GetID()); 1370af245d11STodd Fiala 137197206d57SZachary Turner Status error; 1372af245d11STodd Fiala 1373b9c1b51eSKate Stone switch (m_state) { 1374af245d11STodd Fiala case StateType::eStateInvalid: 1375af245d11STodd Fiala case StateType::eStateExited: 1376af245d11STodd Fiala case StateType::eStateCrashed: 1377af245d11STodd Fiala case StateType::eStateDetached: 1378af245d11STodd Fiala case StateType::eStateUnloaded: 1379af245d11STodd Fiala // Nothing to do - the process is already dead. 1380a6321a8eSPavel Labath LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(), 13818198db30SPavel Labath m_state); 1382af245d11STodd Fiala return error; 1383af245d11STodd Fiala 1384af245d11STodd Fiala case StateType::eStateConnected: 1385af245d11STodd Fiala case StateType::eStateAttaching: 1386af245d11STodd Fiala case StateType::eStateLaunching: 1387af245d11STodd Fiala case StateType::eStateStopped: 1388af245d11STodd Fiala case StateType::eStateRunning: 1389af245d11STodd Fiala case StateType::eStateStepping: 1390af245d11STodd Fiala case StateType::eStateSuspended: 1391af245d11STodd Fiala // We can try to kill a process in these states. 1392af245d11STodd Fiala break; 1393af245d11STodd Fiala } 1394af245d11STodd Fiala 1395b9c1b51eSKate Stone if (kill(GetID(), SIGKILL) != 0) { 1396af245d11STodd Fiala error.SetErrorToErrno(); 1397af245d11STodd Fiala return error; 1398af245d11STodd Fiala } 1399af245d11STodd Fiala 1400af245d11STodd Fiala return error; 1401af245d11STodd Fiala } 1402af245d11STodd Fiala 140397206d57SZachary Turner static Status 140415930862SPavel Labath ParseMemoryRegionInfoFromProcMapsLine(llvm::StringRef &maps_line, 1405b9c1b51eSKate Stone MemoryRegionInfo &memory_region_info) { 1406af245d11STodd Fiala memory_region_info.Clear(); 1407af245d11STodd Fiala 140815930862SPavel Labath StringExtractor line_extractor(maps_line); 1409af245d11STodd Fiala 1410b9c1b51eSKate Stone // Format: {address_start_hex}-{address_end_hex} perms offset dev inode 1411b9c1b51eSKate Stone // pathname 1412b9c1b51eSKate Stone // perms: rwxp (letter is present if set, '-' if not, final character is 1413b9c1b51eSKate Stone // p=private, s=shared). 1414af245d11STodd Fiala 1415af245d11STodd Fiala // Parse out the starting address 1416af245d11STodd Fiala lldb::addr_t start_address = line_extractor.GetHexMaxU64(false, 0); 1417af245d11STodd Fiala 1418af245d11STodd Fiala // Parse out hyphen separating start and end address from range. 1419af245d11STodd Fiala if (!line_extractor.GetBytesLeft() || (line_extractor.GetChar() != '-')) 142097206d57SZachary Turner return Status( 1421b9c1b51eSKate Stone "malformed /proc/{pid}/maps entry, missing dash between address range"); 1422af245d11STodd Fiala 1423af245d11STodd Fiala // Parse out the ending address 1424af245d11STodd Fiala lldb::addr_t end_address = line_extractor.GetHexMaxU64(false, start_address); 1425af245d11STodd Fiala 1426af245d11STodd Fiala // Parse out the space after the address. 1427af245d11STodd Fiala if (!line_extractor.GetBytesLeft() || (line_extractor.GetChar() != ' ')) 142897206d57SZachary Turner return Status( 142997206d57SZachary Turner "malformed /proc/{pid}/maps entry, missing space after range"); 1430af245d11STodd Fiala 1431af245d11STodd Fiala // Save the range. 1432af245d11STodd Fiala memory_region_info.GetRange().SetRangeBase(start_address); 1433af245d11STodd Fiala memory_region_info.GetRange().SetRangeEnd(end_address); 1434af245d11STodd Fiala 1435b9c1b51eSKate Stone // Any memory region in /proc/{pid}/maps is by definition mapped into the 1436b9c1b51eSKate Stone // process. 1437ad007563SHoward Hellyer memory_region_info.SetMapped(MemoryRegionInfo::OptionalBool::eYes); 1438ad007563SHoward Hellyer 1439af245d11STodd Fiala // Parse out each permission entry. 1440af245d11STodd Fiala if (line_extractor.GetBytesLeft() < 4) 144197206d57SZachary Turner return Status("malformed /proc/{pid}/maps entry, missing some portion of " 1442b9c1b51eSKate Stone "permissions"); 1443af245d11STodd Fiala 1444af245d11STodd Fiala // Handle read permission. 1445af245d11STodd Fiala const char read_perm_char = line_extractor.GetChar(); 1446af245d11STodd Fiala if (read_perm_char == 'r') 1447af245d11STodd Fiala memory_region_info.SetReadable(MemoryRegionInfo::OptionalBool::eYes); 1448c73301bbSTamas Berghammer else if (read_perm_char == '-') 1449af245d11STodd Fiala memory_region_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 1450c73301bbSTamas Berghammer else 145197206d57SZachary Turner return Status("unexpected /proc/{pid}/maps read permission char"); 1452af245d11STodd Fiala 1453af245d11STodd Fiala // Handle write permission. 1454af245d11STodd Fiala const char write_perm_char = line_extractor.GetChar(); 1455af245d11STodd Fiala if (write_perm_char == 'w') 1456af245d11STodd Fiala memory_region_info.SetWritable(MemoryRegionInfo::OptionalBool::eYes); 1457c73301bbSTamas Berghammer else if (write_perm_char == '-') 1458af245d11STodd Fiala memory_region_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 1459c73301bbSTamas Berghammer else 146097206d57SZachary Turner return Status("unexpected /proc/{pid}/maps write permission char"); 1461af245d11STodd Fiala 1462af245d11STodd Fiala // Handle execute permission. 1463af245d11STodd Fiala const char exec_perm_char = line_extractor.GetChar(); 1464af245d11STodd Fiala if (exec_perm_char == 'x') 1465af245d11STodd Fiala memory_region_info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes); 1466c73301bbSTamas Berghammer else if (exec_perm_char == '-') 1467af245d11STodd Fiala memory_region_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1468c73301bbSTamas Berghammer else 146997206d57SZachary Turner return Status("unexpected /proc/{pid}/maps exec permission char"); 1470af245d11STodd Fiala 1471d7d69f80STamas Berghammer line_extractor.GetChar(); // Read the private bit 1472d7d69f80STamas Berghammer line_extractor.SkipSpaces(); // Skip the separator 1473d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the offset 1474d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the major device number 1475d7d69f80STamas Berghammer line_extractor.GetChar(); // Read the device id separator 1476d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the major device number 1477d7d69f80STamas Berghammer line_extractor.SkipSpaces(); // Skip the separator 1478d7d69f80STamas Berghammer line_extractor.GetU64(0, 10); // Read the inode number 1479d7d69f80STamas Berghammer 1480d7d69f80STamas Berghammer line_extractor.SkipSpaces(); 1481b9739d40SPavel Labath const char *name = line_extractor.Peek(); 1482b9739d40SPavel Labath if (name) 1483b9739d40SPavel Labath memory_region_info.SetName(name); 1484d7d69f80STamas Berghammer 148597206d57SZachary Turner return Status(); 1486af245d11STodd Fiala } 1487af245d11STodd Fiala 148897206d57SZachary Turner Status NativeProcessLinux::GetMemoryRegionInfo(lldb::addr_t load_addr, 1489b9c1b51eSKate Stone MemoryRegionInfo &range_info) { 1490b9c1b51eSKate Stone // FIXME review that the final memory region returned extends to the end of 1491b9c1b51eSKate Stone // the virtual address space, 1492af245d11STodd Fiala // with no perms if it is not mapped. 1493af245d11STodd Fiala 1494af245d11STodd Fiala // Use an approach that reads memory regions from /proc/{pid}/maps. 1495af245d11STodd Fiala // Assume proc maps entries are in ascending order. 1496af245d11STodd Fiala // FIXME assert if we find differently. 1497af245d11STodd Fiala 1498b9c1b51eSKate Stone if (m_supports_mem_region == LazyBool::eLazyBoolNo) { 1499af245d11STodd Fiala // We're done. 150097206d57SZachary Turner return Status("unsupported"); 1501af245d11STodd Fiala } 1502af245d11STodd Fiala 150397206d57SZachary Turner Status error = PopulateMemoryRegionCache(); 1504b9c1b51eSKate Stone if (error.Fail()) { 1505af245d11STodd Fiala return error; 1506af245d11STodd Fiala } 1507af245d11STodd Fiala 1508af245d11STodd Fiala lldb::addr_t prev_base_address = 0; 1509af245d11STodd Fiala 1510b9c1b51eSKate Stone // FIXME start by finding the last region that is <= target address using 1511b9c1b51eSKate Stone // binary search. Data is sorted. 1512af245d11STodd Fiala // There can be a ton of regions on pthreads apps with lots of threads. 1513b9c1b51eSKate Stone for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end(); 1514b9c1b51eSKate Stone ++it) { 1515a6f5795aSTamas Berghammer MemoryRegionInfo &proc_entry_info = it->first; 1516af245d11STodd Fiala 1517af245d11STodd Fiala // Sanity check assumption that /proc/{pid}/maps entries are ascending. 1518b9c1b51eSKate Stone assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && 1519b9c1b51eSKate Stone "descending /proc/pid/maps entries detected, unexpected"); 1520af245d11STodd Fiala prev_base_address = proc_entry_info.GetRange().GetRangeBase(); 1521b1554311SHafiz Abid Qadeer UNUSED_IF_ASSERT_DISABLED(prev_base_address); 1522af245d11STodd Fiala 1523b9c1b51eSKate Stone // If the target address comes before this entry, indicate distance to next 1524b9c1b51eSKate Stone // region. 1525b9c1b51eSKate Stone if (load_addr < proc_entry_info.GetRange().GetRangeBase()) { 1526af245d11STodd Fiala range_info.GetRange().SetRangeBase(load_addr); 1527b9c1b51eSKate Stone range_info.GetRange().SetByteSize( 1528b9c1b51eSKate Stone proc_entry_info.GetRange().GetRangeBase() - load_addr); 1529af245d11STodd Fiala range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 1530af245d11STodd Fiala range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 1531af245d11STodd Fiala range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1532ad007563SHoward Hellyer range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 1533af245d11STodd Fiala 1534af245d11STodd Fiala return error; 1535b9c1b51eSKate Stone } else if (proc_entry_info.GetRange().Contains(load_addr)) { 1536af245d11STodd Fiala // The target address is within the memory region we're processing here. 1537af245d11STodd Fiala range_info = proc_entry_info; 1538af245d11STodd Fiala return error; 1539af245d11STodd Fiala } 1540af245d11STodd Fiala 1541b9c1b51eSKate Stone // The target memory address comes somewhere after the region we just 1542b9c1b51eSKate Stone // parsed. 1543af245d11STodd Fiala } 1544af245d11STodd Fiala 1545b9c1b51eSKate Stone // If we made it here, we didn't find an entry that contained the given 1546b9c1b51eSKate Stone // address. Return the 1547b9c1b51eSKate Stone // load_addr as start and the amount of bytes betwwen load address and the end 1548b9c1b51eSKate Stone // of the memory as 154909839c33STamas Berghammer // size. 155009839c33STamas Berghammer range_info.GetRange().SetRangeBase(load_addr); 1551ad007563SHoward Hellyer range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); 155209839c33STamas Berghammer range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 155309839c33STamas Berghammer range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 155409839c33STamas Berghammer range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1555ad007563SHoward Hellyer range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 1556af245d11STodd Fiala return error; 1557af245d11STodd Fiala } 1558af245d11STodd Fiala 155997206d57SZachary Turner Status NativeProcessLinux::PopulateMemoryRegionCache() { 1560a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1561a6f5795aSTamas Berghammer 1562a6f5795aSTamas Berghammer // If our cache is empty, pull the latest. There should always be at least 1563a6f5795aSTamas Berghammer // one memory region if memory region handling is supported. 1564a6f5795aSTamas Berghammer if (!m_mem_region_cache.empty()) { 1565a6321a8eSPavel Labath LLDB_LOG(log, "reusing {0} cached memory region entries", 1566a6321a8eSPavel Labath m_mem_region_cache.size()); 156797206d57SZachary Turner return Status(); 1568a6f5795aSTamas Berghammer } 1569a6f5795aSTamas Berghammer 157015930862SPavel Labath auto BufferOrError = getProcFile(GetID(), "maps"); 157115930862SPavel Labath if (!BufferOrError) { 157215930862SPavel Labath m_supports_mem_region = LazyBool::eLazyBoolNo; 157315930862SPavel Labath return BufferOrError.getError(); 157415930862SPavel Labath } 157515930862SPavel Labath StringRef Rest = BufferOrError.get()->getBuffer(); 157615930862SPavel Labath while (! Rest.empty()) { 157715930862SPavel Labath StringRef Line; 157815930862SPavel Labath std::tie(Line, Rest) = Rest.split('\n'); 1579a6f5795aSTamas Berghammer MemoryRegionInfo info; 158097206d57SZachary Turner const Status parse_error = 158197206d57SZachary Turner ParseMemoryRegionInfoFromProcMapsLine(Line, info); 158215930862SPavel Labath if (parse_error.Fail()) { 158315930862SPavel Labath LLDB_LOG(log, "failed to parse proc maps line '{0}': {1}", Line, 158415930862SPavel Labath parse_error); 158515930862SPavel Labath m_supports_mem_region = LazyBool::eLazyBoolNo; 158615930862SPavel Labath return parse_error; 158715930862SPavel Labath } 1588a6f5795aSTamas Berghammer m_mem_region_cache.emplace_back( 1589a6f5795aSTamas Berghammer info, FileSpec(info.GetName().GetCString(), true)); 1590a6f5795aSTamas Berghammer } 1591a6f5795aSTamas Berghammer 159215930862SPavel Labath if (m_mem_region_cache.empty()) { 1593a6f5795aSTamas Berghammer // No entries after attempting to read them. This shouldn't happen if 1594a6f5795aSTamas Berghammer // /proc/{pid}/maps is supported. Assume we don't support map entries 1595a6f5795aSTamas Berghammer // via procfs. 159615930862SPavel Labath m_supports_mem_region = LazyBool::eLazyBoolNo; 1597a6321a8eSPavel Labath LLDB_LOG(log, 1598a6321a8eSPavel Labath "failed to find any procfs maps entries, assuming no support " 1599a6321a8eSPavel Labath "for memory region metadata retrieval"); 160097206d57SZachary Turner return Status("not supported"); 1601a6f5795aSTamas Berghammer } 1602a6f5795aSTamas Berghammer 1603a6321a8eSPavel Labath LLDB_LOG(log, "read {0} memory region entries from /proc/{1}/maps", 1604a6321a8eSPavel Labath m_mem_region_cache.size(), GetID()); 1605a6f5795aSTamas Berghammer 1606a6f5795aSTamas Berghammer // We support memory retrieval, remember that. 1607a6f5795aSTamas Berghammer m_supports_mem_region = LazyBool::eLazyBoolYes; 160897206d57SZachary Turner return Status(); 1609a6f5795aSTamas Berghammer } 1610a6f5795aSTamas Berghammer 1611b9c1b51eSKate Stone void NativeProcessLinux::DoStopIDBumped(uint32_t newBumpId) { 1612a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1613a6321a8eSPavel Labath LLDB_LOG(log, "newBumpId={0}", newBumpId); 1614a6321a8eSPavel Labath LLDB_LOG(log, "clearing {0} entries from memory region cache", 1615a6321a8eSPavel Labath m_mem_region_cache.size()); 1616af245d11STodd Fiala m_mem_region_cache.clear(); 1617af245d11STodd Fiala } 1618af245d11STodd Fiala 161997206d57SZachary Turner Status NativeProcessLinux::AllocateMemory(size_t size, uint32_t permissions, 1620b9c1b51eSKate Stone lldb::addr_t &addr) { 1621af245d11STodd Fiala // FIXME implementing this requires the equivalent of 1622af245d11STodd Fiala // InferiorCallPOSIX::InferiorCallMmap, which depends on 1623af245d11STodd Fiala // functional ThreadPlans working with Native*Protocol. 1624af245d11STodd Fiala #if 1 162597206d57SZachary Turner return Status("not implemented yet"); 1626af245d11STodd Fiala #else 1627af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 1628af245d11STodd Fiala 1629af245d11STodd Fiala unsigned prot = 0; 1630af245d11STodd Fiala if (permissions & lldb::ePermissionsReadable) 1631af245d11STodd Fiala prot |= eMmapProtRead; 1632af245d11STodd Fiala if (permissions & lldb::ePermissionsWritable) 1633af245d11STodd Fiala prot |= eMmapProtWrite; 1634af245d11STodd Fiala if (permissions & lldb::ePermissionsExecutable) 1635af245d11STodd Fiala prot |= eMmapProtExec; 1636af245d11STodd Fiala 1637af245d11STodd Fiala // TODO implement this directly in NativeProcessLinux 1638af245d11STodd Fiala // (and lift to NativeProcessPOSIX if/when that class is 1639af245d11STodd Fiala // refactored out). 1640af245d11STodd Fiala if (InferiorCallMmap(this, addr, 0, size, prot, 1641af245d11STodd Fiala eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { 1642af245d11STodd Fiala m_addr_to_mmap_size[addr] = size; 164397206d57SZachary Turner return Status(); 1644af245d11STodd Fiala } else { 1645af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 164697206d57SZachary Turner return Status("unable to allocate %" PRIu64 1647b9c1b51eSKate Stone " bytes of memory with permissions %s", 1648b9c1b51eSKate Stone size, GetPermissionsAsCString(permissions)); 1649af245d11STodd Fiala } 1650af245d11STodd Fiala #endif 1651af245d11STodd Fiala } 1652af245d11STodd Fiala 165397206d57SZachary Turner Status NativeProcessLinux::DeallocateMemory(lldb::addr_t addr) { 1654af245d11STodd Fiala // FIXME see comments in AllocateMemory - required lower-level 1655af245d11STodd Fiala // bits not in place yet (ThreadPlans) 165697206d57SZachary Turner return Status("not implemented"); 1657af245d11STodd Fiala } 1658af245d11STodd Fiala 1659b9c1b51eSKate Stone lldb::addr_t NativeProcessLinux::GetSharedLibraryInfoAddress() { 1660af245d11STodd Fiala // punt on this for now 1661af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 1662af245d11STodd Fiala } 1663af245d11STodd Fiala 1664b9c1b51eSKate Stone size_t NativeProcessLinux::UpdateThreads() { 1665af245d11STodd Fiala // The NativeProcessLinux monitoring threads are always up to date 1666af245d11STodd Fiala // with respect to thread state and they keep the thread list 1667af245d11STodd Fiala // populated properly. All this method needs to do is return the 1668af245d11STodd Fiala // thread count. 1669af245d11STodd Fiala return m_threads.size(); 1670af245d11STodd Fiala } 1671af245d11STodd Fiala 1672b9c1b51eSKate Stone bool NativeProcessLinux::GetArchitecture(ArchSpec &arch) const { 1673af245d11STodd Fiala arch = m_arch; 1674af245d11STodd Fiala return true; 1675af245d11STodd Fiala } 1676af245d11STodd Fiala 167797206d57SZachary Turner Status NativeProcessLinux::GetSoftwareBreakpointPCOffset( 1678b9c1b51eSKate Stone uint32_t &actual_opcode_size) { 1679af245d11STodd Fiala // FIXME put this behind a breakpoint protocol class that can be 1680af245d11STodd Fiala // set per architecture. Need ARM, MIPS support here. 1681af245d11STodd Fiala static const uint8_t g_i386_opcode[] = {0xCC}; 1682bb00d0b6SUlrich Weigand static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; 1683af245d11STodd Fiala 1684b9c1b51eSKate Stone switch (m_arch.GetMachine()) { 1685af245d11STodd Fiala case llvm::Triple::x86: 1686af245d11STodd Fiala case llvm::Triple::x86_64: 1687af245d11STodd Fiala actual_opcode_size = static_cast<uint32_t>(sizeof(g_i386_opcode)); 168897206d57SZachary Turner return Status(); 1689af245d11STodd Fiala 1690bb00d0b6SUlrich Weigand case llvm::Triple::systemz: 1691bb00d0b6SUlrich Weigand actual_opcode_size = static_cast<uint32_t>(sizeof(g_s390x_opcode)); 169297206d57SZachary Turner return Status(); 1693bb00d0b6SUlrich Weigand 1694ff7fd900STamas Berghammer case llvm::Triple::arm: 1695ff7fd900STamas Berghammer case llvm::Triple::aarch64: 1696e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64: 1697e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64el: 1698ce815e45SSagar Thakur case llvm::Triple::mips: 1699ce815e45SSagar Thakur case llvm::Triple::mipsel: 1700ff7fd900STamas Berghammer // On these architectures the PC don't get updated for breakpoint hits 1701c60c9452SJaydeep Patil actual_opcode_size = 0; 170297206d57SZachary Turner return Status(); 1703e8659b5dSMohit K. Bhakkad 1704af245d11STodd Fiala default: 1705af245d11STodd Fiala assert(false && "CPU type not supported!"); 170697206d57SZachary Turner return Status("CPU type not supported"); 1707af245d11STodd Fiala } 1708af245d11STodd Fiala } 1709af245d11STodd Fiala 171097206d57SZachary Turner Status NativeProcessLinux::SetBreakpoint(lldb::addr_t addr, uint32_t size, 1711b9c1b51eSKate Stone bool hardware) { 1712af245d11STodd Fiala if (hardware) 1713d5ffbad2SOmair Javaid return SetHardwareBreakpoint(addr, size); 1714af245d11STodd Fiala else 1715af245d11STodd Fiala return SetSoftwareBreakpoint(addr, size); 1716af245d11STodd Fiala } 1717af245d11STodd Fiala 171897206d57SZachary Turner Status NativeProcessLinux::RemoveBreakpoint(lldb::addr_t addr, bool hardware) { 1719d5ffbad2SOmair Javaid if (hardware) 1720d5ffbad2SOmair Javaid return RemoveHardwareBreakpoint(addr); 1721d5ffbad2SOmair Javaid else 1722d5ffbad2SOmair Javaid return NativeProcessProtocol::RemoveBreakpoint(addr); 1723d5ffbad2SOmair Javaid } 1724d5ffbad2SOmair Javaid 172597206d57SZachary Turner Status NativeProcessLinux::GetSoftwareBreakpointTrapOpcode( 1726b9c1b51eSKate Stone size_t trap_opcode_size_hint, size_t &actual_opcode_size, 1727b9c1b51eSKate Stone const uint8_t *&trap_opcode_bytes) { 172863c8be95STamas Berghammer // FIXME put this behind a breakpoint protocol class that can be set per 172963c8be95STamas Berghammer // architecture. Need MIPS support here. 17302afc5966STodd Fiala static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4}; 1731be379e15STamas Berghammer // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the 1732be379e15STamas Berghammer // linux kernel does otherwise. 1733be379e15STamas Berghammer static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7}; 1734af245d11STodd Fiala static const uint8_t g_i386_opcode[] = {0xCC}; 17353df471c3SMohit K. Bhakkad static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d}; 17362c2acf96SMohit K. Bhakkad static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00}; 1737bb00d0b6SUlrich Weigand static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; 1738be379e15STamas Berghammer static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde}; 1739af245d11STodd Fiala 1740b9c1b51eSKate Stone switch (m_arch.GetMachine()) { 17412afc5966STodd Fiala case llvm::Triple::aarch64: 17422afc5966STodd Fiala trap_opcode_bytes = g_aarch64_opcode; 17432afc5966STodd Fiala actual_opcode_size = sizeof(g_aarch64_opcode); 174497206d57SZachary Turner return Status(); 17452afc5966STodd Fiala 174663c8be95STamas Berghammer case llvm::Triple::arm: 1747b9c1b51eSKate Stone switch (trap_opcode_size_hint) { 174863c8be95STamas Berghammer case 2: 174963c8be95STamas Berghammer trap_opcode_bytes = g_thumb_breakpoint_opcode; 175063c8be95STamas Berghammer actual_opcode_size = sizeof(g_thumb_breakpoint_opcode); 175197206d57SZachary Turner return Status(); 175263c8be95STamas Berghammer case 4: 175363c8be95STamas Berghammer trap_opcode_bytes = g_arm_breakpoint_opcode; 175463c8be95STamas Berghammer actual_opcode_size = sizeof(g_arm_breakpoint_opcode); 175597206d57SZachary Turner return Status(); 175663c8be95STamas Berghammer default: 175763c8be95STamas Berghammer assert(false && "Unrecognised trap opcode size hint!"); 175897206d57SZachary Turner return Status("Unrecognised trap opcode size hint!"); 175963c8be95STamas Berghammer } 176063c8be95STamas Berghammer 1761af245d11STodd Fiala case llvm::Triple::x86: 1762af245d11STodd Fiala case llvm::Triple::x86_64: 1763af245d11STodd Fiala trap_opcode_bytes = g_i386_opcode; 1764af245d11STodd Fiala actual_opcode_size = sizeof(g_i386_opcode); 176597206d57SZachary Turner return Status(); 1766af245d11STodd Fiala 1767ce815e45SSagar Thakur case llvm::Triple::mips: 17683df471c3SMohit K. Bhakkad case llvm::Triple::mips64: 17693df471c3SMohit K. Bhakkad trap_opcode_bytes = g_mips64_opcode; 17703df471c3SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64_opcode); 177197206d57SZachary Turner return Status(); 17723df471c3SMohit K. Bhakkad 1773ce815e45SSagar Thakur case llvm::Triple::mipsel: 17742c2acf96SMohit K. Bhakkad case llvm::Triple::mips64el: 17752c2acf96SMohit K. Bhakkad trap_opcode_bytes = g_mips64el_opcode; 17762c2acf96SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64el_opcode); 177797206d57SZachary Turner return Status(); 17782c2acf96SMohit K. Bhakkad 1779bb00d0b6SUlrich Weigand case llvm::Triple::systemz: 1780bb00d0b6SUlrich Weigand trap_opcode_bytes = g_s390x_opcode; 1781bb00d0b6SUlrich Weigand actual_opcode_size = sizeof(g_s390x_opcode); 178297206d57SZachary Turner return Status(); 1783bb00d0b6SUlrich Weigand 1784af245d11STodd Fiala default: 1785af245d11STodd Fiala assert(false && "CPU type not supported!"); 178697206d57SZachary Turner return Status("CPU type not supported"); 1787af245d11STodd Fiala } 1788af245d11STodd Fiala } 1789af245d11STodd Fiala 1790af245d11STodd Fiala #if 0 1791af245d11STodd Fiala ProcessMessage::CrashReason 1792af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info) 1793af245d11STodd Fiala { 1794af245d11STodd Fiala ProcessMessage::CrashReason reason; 1795af245d11STodd Fiala assert(info->si_signo == SIGSEGV); 1796af245d11STodd Fiala 1797af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1798af245d11STodd Fiala 1799af245d11STodd Fiala switch (info->si_code) 1800af245d11STodd Fiala { 1801af245d11STodd Fiala default: 1802af245d11STodd Fiala assert(false && "unexpected si_code for SIGSEGV"); 1803af245d11STodd Fiala break; 1804af245d11STodd Fiala case SI_KERNEL: 1805af245d11STodd Fiala // Linux will occasionally send spurious SI_KERNEL codes. 1806af245d11STodd Fiala // (this is poorly documented in sigaction) 1807af245d11STodd Fiala // One way to get this is via unaligned SIMD loads. 1808af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; // for lack of anything better 1809af245d11STodd Fiala break; 1810af245d11STodd Fiala case SEGV_MAPERR: 1811af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; 1812af245d11STodd Fiala break; 1813af245d11STodd Fiala case SEGV_ACCERR: 1814af245d11STodd Fiala reason = ProcessMessage::ePrivilegedAddress; 1815af245d11STodd Fiala break; 1816af245d11STodd Fiala } 1817af245d11STodd Fiala 1818af245d11STodd Fiala return reason; 1819af245d11STodd Fiala } 1820af245d11STodd Fiala #endif 1821af245d11STodd Fiala 1822af245d11STodd Fiala #if 0 1823af245d11STodd Fiala ProcessMessage::CrashReason 1824af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info) 1825af245d11STodd Fiala { 1826af245d11STodd Fiala ProcessMessage::CrashReason reason; 1827af245d11STodd Fiala assert(info->si_signo == SIGILL); 1828af245d11STodd Fiala 1829af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1830af245d11STodd Fiala 1831af245d11STodd Fiala switch (info->si_code) 1832af245d11STodd Fiala { 1833af245d11STodd Fiala default: 1834af245d11STodd Fiala assert(false && "unexpected si_code for SIGILL"); 1835af245d11STodd Fiala break; 1836af245d11STodd Fiala case ILL_ILLOPC: 1837af245d11STodd Fiala reason = ProcessMessage::eIllegalOpcode; 1838af245d11STodd Fiala break; 1839af245d11STodd Fiala case ILL_ILLOPN: 1840af245d11STodd Fiala reason = ProcessMessage::eIllegalOperand; 1841af245d11STodd Fiala break; 1842af245d11STodd Fiala case ILL_ILLADR: 1843af245d11STodd Fiala reason = ProcessMessage::eIllegalAddressingMode; 1844af245d11STodd Fiala break; 1845af245d11STodd Fiala case ILL_ILLTRP: 1846af245d11STodd Fiala reason = ProcessMessage::eIllegalTrap; 1847af245d11STodd Fiala break; 1848af245d11STodd Fiala case ILL_PRVOPC: 1849af245d11STodd Fiala reason = ProcessMessage::ePrivilegedOpcode; 1850af245d11STodd Fiala break; 1851af245d11STodd Fiala case ILL_PRVREG: 1852af245d11STodd Fiala reason = ProcessMessage::ePrivilegedRegister; 1853af245d11STodd Fiala break; 1854af245d11STodd Fiala case ILL_COPROC: 1855af245d11STodd Fiala reason = ProcessMessage::eCoprocessorError; 1856af245d11STodd Fiala break; 1857af245d11STodd Fiala case ILL_BADSTK: 1858af245d11STodd Fiala reason = ProcessMessage::eInternalStackError; 1859af245d11STodd Fiala break; 1860af245d11STodd Fiala } 1861af245d11STodd Fiala 1862af245d11STodd Fiala return reason; 1863af245d11STodd Fiala } 1864af245d11STodd Fiala #endif 1865af245d11STodd Fiala 1866af245d11STodd Fiala #if 0 1867af245d11STodd Fiala ProcessMessage::CrashReason 1868af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info) 1869af245d11STodd Fiala { 1870af245d11STodd Fiala ProcessMessage::CrashReason reason; 1871af245d11STodd Fiala assert(info->si_signo == SIGFPE); 1872af245d11STodd Fiala 1873af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1874af245d11STodd Fiala 1875af245d11STodd Fiala switch (info->si_code) 1876af245d11STodd Fiala { 1877af245d11STodd Fiala default: 1878af245d11STodd Fiala assert(false && "unexpected si_code for SIGFPE"); 1879af245d11STodd Fiala break; 1880af245d11STodd Fiala case FPE_INTDIV: 1881af245d11STodd Fiala reason = ProcessMessage::eIntegerDivideByZero; 1882af245d11STodd Fiala break; 1883af245d11STodd Fiala case FPE_INTOVF: 1884af245d11STodd Fiala reason = ProcessMessage::eIntegerOverflow; 1885af245d11STodd Fiala break; 1886af245d11STodd Fiala case FPE_FLTDIV: 1887af245d11STodd Fiala reason = ProcessMessage::eFloatDivideByZero; 1888af245d11STodd Fiala break; 1889af245d11STodd Fiala case FPE_FLTOVF: 1890af245d11STodd Fiala reason = ProcessMessage::eFloatOverflow; 1891af245d11STodd Fiala break; 1892af245d11STodd Fiala case FPE_FLTUND: 1893af245d11STodd Fiala reason = ProcessMessage::eFloatUnderflow; 1894af245d11STodd Fiala break; 1895af245d11STodd Fiala case FPE_FLTRES: 1896af245d11STodd Fiala reason = ProcessMessage::eFloatInexactResult; 1897af245d11STodd Fiala break; 1898af245d11STodd Fiala case FPE_FLTINV: 1899af245d11STodd Fiala reason = ProcessMessage::eFloatInvalidOperation; 1900af245d11STodd Fiala break; 1901af245d11STodd Fiala case FPE_FLTSUB: 1902af245d11STodd Fiala reason = ProcessMessage::eFloatSubscriptRange; 1903af245d11STodd Fiala break; 1904af245d11STodd Fiala } 1905af245d11STodd Fiala 1906af245d11STodd Fiala return reason; 1907af245d11STodd Fiala } 1908af245d11STodd Fiala #endif 1909af245d11STodd Fiala 1910af245d11STodd Fiala #if 0 1911af245d11STodd Fiala ProcessMessage::CrashReason 1912af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info) 1913af245d11STodd Fiala { 1914af245d11STodd Fiala ProcessMessage::CrashReason reason; 1915af245d11STodd Fiala assert(info->si_signo == SIGBUS); 1916af245d11STodd Fiala 1917af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1918af245d11STodd Fiala 1919af245d11STodd Fiala switch (info->si_code) 1920af245d11STodd Fiala { 1921af245d11STodd Fiala default: 1922af245d11STodd Fiala assert(false && "unexpected si_code for SIGBUS"); 1923af245d11STodd Fiala break; 1924af245d11STodd Fiala case BUS_ADRALN: 1925af245d11STodd Fiala reason = ProcessMessage::eIllegalAlignment; 1926af245d11STodd Fiala break; 1927af245d11STodd Fiala case BUS_ADRERR: 1928af245d11STodd Fiala reason = ProcessMessage::eIllegalAddress; 1929af245d11STodd Fiala break; 1930af245d11STodd Fiala case BUS_OBJERR: 1931af245d11STodd Fiala reason = ProcessMessage::eHardwareError; 1932af245d11STodd Fiala break; 1933af245d11STodd Fiala } 1934af245d11STodd Fiala 1935af245d11STodd Fiala return reason; 1936af245d11STodd Fiala } 1937af245d11STodd Fiala #endif 1938af245d11STodd Fiala 193997206d57SZachary Turner Status NativeProcessLinux::ReadMemory(lldb::addr_t addr, void *buf, size_t size, 1940b9c1b51eSKate Stone size_t &bytes_read) { 1941df7c6995SPavel Labath if (ProcessVmReadvSupported()) { 1942b9c1b51eSKate Stone // The process_vm_readv path is about 50 times faster than ptrace api. We 1943b9c1b51eSKate Stone // want to use 1944df7c6995SPavel Labath // this syscall if it is supported. 1945df7c6995SPavel Labath 1946df7c6995SPavel Labath const ::pid_t pid = GetID(); 1947df7c6995SPavel Labath 1948df7c6995SPavel Labath struct iovec local_iov, remote_iov; 1949df7c6995SPavel Labath local_iov.iov_base = buf; 1950df7c6995SPavel Labath local_iov.iov_len = size; 1951df7c6995SPavel Labath remote_iov.iov_base = reinterpret_cast<void *>(addr); 1952df7c6995SPavel Labath remote_iov.iov_len = size; 1953df7c6995SPavel Labath 1954df7c6995SPavel Labath bytes_read = process_vm_readv(pid, &local_iov, 1, &remote_iov, 1, 0); 1955df7c6995SPavel Labath const bool success = bytes_read == size; 1956df7c6995SPavel Labath 1957a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1958a6321a8eSPavel Labath LLDB_LOG(log, 1959a6321a8eSPavel Labath "using process_vm_readv to read {0} bytes from inferior " 1960a6321a8eSPavel Labath "address {1:x}: {2}", 196110c41f37SPavel Labath size, addr, success ? "Success" : llvm::sys::StrError(errno)); 1962df7c6995SPavel Labath 1963df7c6995SPavel Labath if (success) 196497206d57SZachary Turner return Status(); 1965a6321a8eSPavel Labath // else the call failed for some reason, let's retry the read using ptrace 1966b9c1b51eSKate Stone // api. 1967df7c6995SPavel Labath } 1968df7c6995SPavel Labath 196919cbe96aSPavel Labath unsigned char *dst = static_cast<unsigned char *>(buf); 197019cbe96aSPavel Labath size_t remainder; 197119cbe96aSPavel Labath long data; 197219cbe96aSPavel Labath 1973a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 1974a6321a8eSPavel Labath LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 197519cbe96aSPavel Labath 1976b9c1b51eSKate Stone for (bytes_read = 0; bytes_read < size; bytes_read += remainder) { 197797206d57SZachary Turner Status error = NativeProcessLinux::PtraceWrapper( 1978b9c1b51eSKate Stone PTRACE_PEEKDATA, GetID(), (void *)addr, nullptr, 0, &data); 1979a6321a8eSPavel Labath if (error.Fail()) 198019cbe96aSPavel Labath return error; 198119cbe96aSPavel Labath 198219cbe96aSPavel Labath remainder = size - bytes_read; 198319cbe96aSPavel Labath remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; 198419cbe96aSPavel Labath 198519cbe96aSPavel Labath // Copy the data into our buffer 1986f6ef187bSMohit K. Bhakkad memcpy(dst, &data, remainder); 198719cbe96aSPavel Labath 1988a6321a8eSPavel Labath LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data); 198919cbe96aSPavel Labath addr += k_ptrace_word_size; 199019cbe96aSPavel Labath dst += k_ptrace_word_size; 199119cbe96aSPavel Labath } 199297206d57SZachary Turner return Status(); 1993af245d11STodd Fiala } 1994af245d11STodd Fiala 199597206d57SZachary Turner Status NativeProcessLinux::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, 1996b9c1b51eSKate Stone size_t size, 1997b9c1b51eSKate Stone size_t &bytes_read) { 199897206d57SZachary Turner Status error = ReadMemory(addr, buf, size, bytes_read); 1999b9c1b51eSKate Stone if (error.Fail()) 2000b9c1b51eSKate Stone return error; 20013eb4b458SChaoren Lin return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); 20023eb4b458SChaoren Lin } 20033eb4b458SChaoren Lin 200497206d57SZachary Turner Status NativeProcessLinux::WriteMemory(lldb::addr_t addr, const void *buf, 2005b9c1b51eSKate Stone size_t size, size_t &bytes_written) { 200619cbe96aSPavel Labath const unsigned char *src = static_cast<const unsigned char *>(buf); 200719cbe96aSPavel Labath size_t remainder; 200897206d57SZachary Turner Status error; 200919cbe96aSPavel Labath 2010a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 2011a6321a8eSPavel Labath LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 201219cbe96aSPavel Labath 2013b9c1b51eSKate Stone for (bytes_written = 0; bytes_written < size; bytes_written += remainder) { 201419cbe96aSPavel Labath remainder = size - bytes_written; 201519cbe96aSPavel Labath remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; 201619cbe96aSPavel Labath 2017b9c1b51eSKate Stone if (remainder == k_ptrace_word_size) { 201819cbe96aSPavel Labath unsigned long data = 0; 2019f6ef187bSMohit K. Bhakkad memcpy(&data, src, k_ptrace_word_size); 202019cbe96aSPavel Labath 2021a6321a8eSPavel Labath LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data); 2022b9c1b51eSKate Stone error = NativeProcessLinux::PtraceWrapper(PTRACE_POKEDATA, GetID(), 2023b9c1b51eSKate Stone (void *)addr, (void *)data); 2024a6321a8eSPavel Labath if (error.Fail()) 202519cbe96aSPavel Labath return error; 2026b9c1b51eSKate Stone } else { 202719cbe96aSPavel Labath unsigned char buff[8]; 202819cbe96aSPavel Labath size_t bytes_read; 202919cbe96aSPavel Labath error = ReadMemory(addr, buff, k_ptrace_word_size, bytes_read); 2030a6321a8eSPavel Labath if (error.Fail()) 203119cbe96aSPavel Labath return error; 203219cbe96aSPavel Labath 203319cbe96aSPavel Labath memcpy(buff, src, remainder); 203419cbe96aSPavel Labath 203519cbe96aSPavel Labath size_t bytes_written_rec; 203619cbe96aSPavel Labath error = WriteMemory(addr, buff, k_ptrace_word_size, bytes_written_rec); 2037a6321a8eSPavel Labath if (error.Fail()) 203819cbe96aSPavel Labath return error; 203919cbe96aSPavel Labath 2040a6321a8eSPavel Labath LLDB_LOG(log, "[{0:x}]:{1:x} ({2:x})", addr, *(const unsigned long *)src, 2041b9c1b51eSKate Stone *(unsigned long *)buff); 204219cbe96aSPavel Labath } 204319cbe96aSPavel Labath 204419cbe96aSPavel Labath addr += k_ptrace_word_size; 204519cbe96aSPavel Labath src += k_ptrace_word_size; 204619cbe96aSPavel Labath } 204719cbe96aSPavel Labath return error; 2048af245d11STodd Fiala } 2049af245d11STodd Fiala 205097206d57SZachary Turner Status NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) { 205119cbe96aSPavel Labath return PtraceWrapper(PTRACE_GETSIGINFO, tid, nullptr, siginfo); 2052af245d11STodd Fiala } 2053af245d11STodd Fiala 205497206d57SZachary Turner Status NativeProcessLinux::GetEventMessage(lldb::tid_t tid, 2055b9c1b51eSKate Stone unsigned long *message) { 205619cbe96aSPavel Labath return PtraceWrapper(PTRACE_GETEVENTMSG, tid, nullptr, message); 2057af245d11STodd Fiala } 2058af245d11STodd Fiala 205997206d57SZachary Turner Status NativeProcessLinux::Detach(lldb::tid_t tid) { 206097ccc294SChaoren Lin if (tid == LLDB_INVALID_THREAD_ID) 206197206d57SZachary Turner return Status(); 206297ccc294SChaoren Lin 206319cbe96aSPavel Labath return PtraceWrapper(PTRACE_DETACH, tid); 2064af245d11STodd Fiala } 2065af245d11STodd Fiala 2066b9c1b51eSKate Stone bool NativeProcessLinux::HasThreadNoLock(lldb::tid_t thread_id) { 2067b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 2068af245d11STodd Fiala assert(thread_sp && "thread list should not contain NULL threads"); 2069b9c1b51eSKate Stone if (thread_sp->GetID() == thread_id) { 2070af245d11STodd Fiala // We have this thread. 2071af245d11STodd Fiala return true; 2072af245d11STodd Fiala } 2073af245d11STodd Fiala } 2074af245d11STodd Fiala 2075af245d11STodd Fiala // We don't have this thread. 2076af245d11STodd Fiala return false; 2077af245d11STodd Fiala } 2078af245d11STodd Fiala 2079b9c1b51eSKate Stone bool NativeProcessLinux::StopTrackingThread(lldb::tid_t thread_id) { 2080a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2081a6321a8eSPavel Labath LLDB_LOG(log, "tid: {0})", thread_id); 20821dbc6c9cSPavel Labath 20831dbc6c9cSPavel Labath bool found = false; 2084b9c1b51eSKate Stone for (auto it = m_threads.begin(); it != m_threads.end(); ++it) { 2085b9c1b51eSKate Stone if (*it && ((*it)->GetID() == thread_id)) { 2086af245d11STodd Fiala m_threads.erase(it); 20871dbc6c9cSPavel Labath found = true; 20881dbc6c9cSPavel Labath break; 2089af245d11STodd Fiala } 2090af245d11STodd Fiala } 2091af245d11STodd Fiala 20929eb1ecb9SPavel Labath SignalIfAllThreadsStopped(); 20931dbc6c9cSPavel Labath return found; 2094af245d11STodd Fiala } 2095af245d11STodd Fiala 2096b9c1b51eSKate Stone NativeThreadLinuxSP NativeProcessLinux::AddThread(lldb::tid_t thread_id) { 2097a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); 2098a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id); 2099af245d11STodd Fiala 2100b9c1b51eSKate Stone assert(!HasThreadNoLock(thread_id) && 2101b9c1b51eSKate Stone "attempted to add a thread by id that already exists"); 2102af245d11STodd Fiala 2103af245d11STodd Fiala // If this is the first thread, save it as the current thread 2104af245d11STodd Fiala if (m_threads.empty()) 2105af245d11STodd Fiala SetCurrentThreadID(thread_id); 2106af245d11STodd Fiala 2107f9077782SPavel Labath auto thread_sp = std::make_shared<NativeThreadLinux>(this, thread_id); 2108af245d11STodd Fiala m_threads.push_back(thread_sp); 2109af245d11STodd Fiala return thread_sp; 2110af245d11STodd Fiala } 2111af245d11STodd Fiala 211297206d57SZachary Turner Status 211397206d57SZachary Turner NativeProcessLinux::FixupBreakpointPCAsNeeded(NativeThreadLinux &thread) { 2114a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS)); 2115af245d11STodd Fiala 211697206d57SZachary Turner Status error; 2117af245d11STodd Fiala 2118b9c1b51eSKate Stone // Find out the size of a breakpoint (might depend on where we are in the 2119b9c1b51eSKate Stone // code). 2120b9cc0c75SPavel Labath NativeRegisterContextSP context_sp = thread.GetRegisterContext(); 2121b9c1b51eSKate Stone if (!context_sp) { 2122af245d11STodd Fiala error.SetErrorString("cannot get a NativeRegisterContext for the thread"); 2123a6321a8eSPavel Labath LLDB_LOG(log, "failed: {0}", error); 2124af245d11STodd Fiala return error; 2125af245d11STodd Fiala } 2126af245d11STodd Fiala 2127af245d11STodd Fiala uint32_t breakpoint_size = 0; 2128b9cc0c75SPavel Labath error = GetSoftwareBreakpointPCOffset(breakpoint_size); 2129b9c1b51eSKate Stone if (error.Fail()) { 2130a6321a8eSPavel Labath LLDB_LOG(log, "GetBreakpointSize() failed: {0}", error); 2131af245d11STodd Fiala return error; 2132a6321a8eSPavel Labath } else 2133a6321a8eSPavel Labath LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size); 2134af245d11STodd Fiala 2135b9c1b51eSKate Stone // First try probing for a breakpoint at a software breakpoint location: PC - 2136b9c1b51eSKate Stone // breakpoint size. 2137b9c1b51eSKate Stone const lldb::addr_t initial_pc_addr = 2138b9c1b51eSKate Stone context_sp->GetPCfromBreakpointLocation(); 2139af245d11STodd Fiala lldb::addr_t breakpoint_addr = initial_pc_addr; 2140b9c1b51eSKate Stone if (breakpoint_size > 0) { 2141af245d11STodd Fiala // Do not allow breakpoint probe to wrap around. 21423eb4b458SChaoren Lin if (breakpoint_addr >= breakpoint_size) 21433eb4b458SChaoren Lin breakpoint_addr -= breakpoint_size; 2144af245d11STodd Fiala } 2145af245d11STodd Fiala 2146af245d11STodd Fiala // Check if we stopped because of a breakpoint. 2147af245d11STodd Fiala NativeBreakpointSP breakpoint_sp; 2148af245d11STodd Fiala error = m_breakpoint_list.GetBreakpoint(breakpoint_addr, breakpoint_sp); 2149b9c1b51eSKate Stone if (!error.Success() || !breakpoint_sp) { 2150af245d11STodd Fiala // We didn't find one at a software probe location. Nothing to do. 2151a6321a8eSPavel Labath LLDB_LOG(log, 2152a6321a8eSPavel Labath "pid {0} no lldb breakpoint found at current pc with " 2153a6321a8eSPavel Labath "adjustment: {1}", 2154a6321a8eSPavel Labath GetID(), breakpoint_addr); 215597206d57SZachary Turner return Status(); 2156af245d11STodd Fiala } 2157af245d11STodd Fiala 2158af245d11STodd Fiala // If the breakpoint is not a software breakpoint, nothing to do. 2159b9c1b51eSKate Stone if (!breakpoint_sp->IsSoftwareBreakpoint()) { 2160a6321a8eSPavel Labath LLDB_LOG( 2161a6321a8eSPavel Labath log, 2162a6321a8eSPavel Labath "pid {0} breakpoint found at {1:x}, not software, nothing to adjust", 2163a6321a8eSPavel Labath GetID(), breakpoint_addr); 216497206d57SZachary Turner return Status(); 2165af245d11STodd Fiala } 2166af245d11STodd Fiala 2167af245d11STodd Fiala // 2168af245d11STodd Fiala // We have a software breakpoint and need to adjust the PC. 2169af245d11STodd Fiala // 2170af245d11STodd Fiala 2171af245d11STodd Fiala // Sanity check. 2172b9c1b51eSKate Stone if (breakpoint_size == 0) { 2173af245d11STodd Fiala // Nothing to do! How did we get here? 2174a6321a8eSPavel Labath LLDB_LOG(log, 2175a6321a8eSPavel Labath "pid {0} breakpoint found at {1:x}, it is software, but the " 2176a6321a8eSPavel Labath "size is zero, nothing to do (unexpected)", 2177a6321a8eSPavel Labath GetID(), breakpoint_addr); 217897206d57SZachary Turner return Status(); 2179af245d11STodd Fiala } 2180af245d11STodd Fiala 2181af245d11STodd Fiala // Change the program counter. 2182a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(), 2183a6321a8eSPavel Labath thread.GetID(), initial_pc_addr, breakpoint_addr); 2184af245d11STodd Fiala 2185af245d11STodd Fiala error = context_sp->SetPC(breakpoint_addr); 2186b9c1b51eSKate Stone if (error.Fail()) { 2187a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(), 2188a6321a8eSPavel Labath thread.GetID(), error); 2189af245d11STodd Fiala return error; 2190af245d11STodd Fiala } 2191af245d11STodd Fiala 2192af245d11STodd Fiala return error; 2193af245d11STodd Fiala } 2194fa03ad2eSChaoren Lin 219597206d57SZachary Turner Status NativeProcessLinux::GetLoadedModuleFileSpec(const char *module_path, 2196b9c1b51eSKate Stone FileSpec &file_spec) { 219797206d57SZachary Turner Status error = PopulateMemoryRegionCache(); 2198a6f5795aSTamas Berghammer if (error.Fail()) 2199a6f5795aSTamas Berghammer return error; 2200a6f5795aSTamas Berghammer 22017cb18bf5STamas Berghammer FileSpec module_file_spec(module_path, true); 22027cb18bf5STamas Berghammer 22037cb18bf5STamas Berghammer file_spec.Clear(); 2204a6f5795aSTamas Berghammer for (const auto &it : m_mem_region_cache) { 2205a6f5795aSTamas Berghammer if (it.second.GetFilename() == module_file_spec.GetFilename()) { 2206a6f5795aSTamas Berghammer file_spec = it.second; 220797206d57SZachary Turner return Status(); 2208a6f5795aSTamas Berghammer } 2209a6f5795aSTamas Berghammer } 221097206d57SZachary Turner return Status("Module file (%s) not found in /proc/%" PRIu64 "/maps file!", 22117cb18bf5STamas Berghammer module_file_spec.GetFilename().AsCString(), GetID()); 22127cb18bf5STamas Berghammer } 2213c076559aSPavel Labath 221497206d57SZachary Turner Status NativeProcessLinux::GetFileLoadAddress(const llvm::StringRef &file_name, 2215b9c1b51eSKate Stone lldb::addr_t &load_addr) { 2216783bfc8cSTamas Berghammer load_addr = LLDB_INVALID_ADDRESS; 221797206d57SZachary Turner Status error = PopulateMemoryRegionCache(); 2218a6f5795aSTamas Berghammer if (error.Fail()) 2219783bfc8cSTamas Berghammer return error; 2220a6f5795aSTamas Berghammer 2221a6f5795aSTamas Berghammer FileSpec file(file_name, false); 2222a6f5795aSTamas Berghammer for (const auto &it : m_mem_region_cache) { 2223a6f5795aSTamas Berghammer if (it.second == file) { 2224a6f5795aSTamas Berghammer load_addr = it.first.GetRange().GetRangeBase(); 222597206d57SZachary Turner return Status(); 2226a6f5795aSTamas Berghammer } 2227a6f5795aSTamas Berghammer } 222897206d57SZachary Turner return Status("No load address found for specified file."); 2229783bfc8cSTamas Berghammer } 2230783bfc8cSTamas Berghammer 2231b9c1b51eSKate Stone NativeThreadLinuxSP NativeProcessLinux::GetThreadByID(lldb::tid_t tid) { 2232b9c1b51eSKate Stone return std::static_pointer_cast<NativeThreadLinux>( 2233b9c1b51eSKate Stone NativeProcessProtocol::GetThreadByID(tid)); 2234f9077782SPavel Labath } 2235f9077782SPavel Labath 223697206d57SZachary Turner Status NativeProcessLinux::ResumeThread(NativeThreadLinux &thread, 2237b9c1b51eSKate Stone lldb::StateType state, int signo) { 2238a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2239a6321a8eSPavel Labath LLDB_LOG(log, "tid: {0}", thread.GetID()); 2240c076559aSPavel Labath 2241c076559aSPavel Labath // Before we do the resume below, first check if we have a pending 2242108c325dSPavel Labath // stop notification that is currently waiting for 22430e1d729bSPavel Labath // all threads to stop. This is potentially a buggy situation since 2244c076559aSPavel Labath // we're ostensibly waiting for threads to stop before we send out the 2245c076559aSPavel Labath // pending notification, and here we are resuming one before we send 2246c076559aSPavel Labath // out the pending stop notification. 2247a6321a8eSPavel Labath if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) { 2248a6321a8eSPavel Labath LLDB_LOG(log, 2249a6321a8eSPavel Labath "about to resume tid {0} per explicit request but we have a " 2250a6321a8eSPavel Labath "pending stop notification (tid {1}) that is actively " 2251a6321a8eSPavel Labath "waiting for this thread to stop. Valid sequence of events?", 2252a6321a8eSPavel Labath thread.GetID(), m_pending_notification_tid); 2253c076559aSPavel Labath } 2254c076559aSPavel Labath 2255c076559aSPavel Labath // Request a resume. We expect this to be synchronous and the system 2256c076559aSPavel Labath // to reflect it is running after this completes. 2257b9c1b51eSKate Stone switch (state) { 2258b9c1b51eSKate Stone case eStateRunning: { 2259605b51b8SPavel Labath const auto resume_result = thread.Resume(signo); 22600e1d729bSPavel Labath if (resume_result.Success()) 22610e1d729bSPavel Labath SetState(eStateRunning, true); 22620e1d729bSPavel Labath return resume_result; 2263c076559aSPavel Labath } 2264b9c1b51eSKate Stone case eStateStepping: { 2265605b51b8SPavel Labath const auto step_result = thread.SingleStep(signo); 22660e1d729bSPavel Labath if (step_result.Success()) 22670e1d729bSPavel Labath SetState(eStateRunning, true); 22680e1d729bSPavel Labath return step_result; 22690e1d729bSPavel Labath } 22700e1d729bSPavel Labath default: 22718198db30SPavel Labath LLDB_LOG(log, "Unhandled state {0}.", state); 22720e1d729bSPavel Labath llvm_unreachable("Unhandled state for resume"); 22730e1d729bSPavel Labath } 2274c076559aSPavel Labath } 2275c076559aSPavel Labath 2276c076559aSPavel Labath //===----------------------------------------------------------------------===// 2277c076559aSPavel Labath 2278b9c1b51eSKate Stone void NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid) { 2279a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2280a6321a8eSPavel Labath LLDB_LOG(log, "about to process event: (triggering_tid: {0})", 2281a6321a8eSPavel Labath triggering_tid); 2282c076559aSPavel Labath 22830e1d729bSPavel Labath m_pending_notification_tid = triggering_tid; 22840e1d729bSPavel Labath 22850e1d729bSPavel Labath // Request a stop for all the thread stops that need to be stopped 22860e1d729bSPavel Labath // and are not already known to be stopped. 2287b9c1b51eSKate Stone for (const auto &thread_sp : m_threads) { 22880e1d729bSPavel Labath if (StateIsRunningState(thread_sp->GetState())) 22890e1d729bSPavel Labath static_pointer_cast<NativeThreadLinux>(thread_sp)->RequestStop(); 22900e1d729bSPavel Labath } 22910e1d729bSPavel Labath 22920e1d729bSPavel Labath SignalIfAllThreadsStopped(); 2293a6321a8eSPavel Labath LLDB_LOG(log, "event processing done"); 2294c076559aSPavel Labath } 2295c076559aSPavel Labath 2296b9c1b51eSKate Stone void NativeProcessLinux::SignalIfAllThreadsStopped() { 22970e1d729bSPavel Labath if (m_pending_notification_tid == LLDB_INVALID_THREAD_ID) 22980e1d729bSPavel Labath return; // No pending notification. Nothing to do. 22990e1d729bSPavel Labath 2300b9c1b51eSKate Stone for (const auto &thread_sp : m_threads) { 23010e1d729bSPavel Labath if (StateIsRunningState(thread_sp->GetState())) 23020e1d729bSPavel Labath return; // Some threads are still running. Don't signal yet. 23030e1d729bSPavel Labath } 23040e1d729bSPavel Labath 23050e1d729bSPavel Labath // We have a pending notification and all threads have stopped. 2306b9c1b51eSKate Stone Log *log( 2307b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 23089eb1ecb9SPavel Labath 2309b9c1b51eSKate Stone // Clear any temporary breakpoints we used to implement software single 2310b9c1b51eSKate Stone // stepping. 2311b9c1b51eSKate Stone for (const auto &thread_info : m_threads_stepping_with_breakpoint) { 231297206d57SZachary Turner Status error = RemoveBreakpoint(thread_info.second); 23139eb1ecb9SPavel Labath if (error.Fail()) 2314a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0} remove stepping breakpoint: {1}", 2315a6321a8eSPavel Labath thread_info.first, error); 23169eb1ecb9SPavel Labath } 23179eb1ecb9SPavel Labath m_threads_stepping_with_breakpoint.clear(); 23189eb1ecb9SPavel Labath 23199eb1ecb9SPavel Labath // Notify the delegate about the stop 23200e1d729bSPavel Labath SetCurrentThreadID(m_pending_notification_tid); 2321ed89c7feSPavel Labath SetState(StateType::eStateStopped, true); 23220e1d729bSPavel Labath m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 2323c076559aSPavel Labath } 2324c076559aSPavel Labath 2325b9c1b51eSKate Stone void NativeProcessLinux::ThreadWasCreated(NativeThreadLinux &thread) { 2326a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2327a6321a8eSPavel Labath LLDB_LOG(log, "tid: {0}", thread.GetID()); 23281dbc6c9cSPavel Labath 2329b9c1b51eSKate Stone if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID && 2330b9c1b51eSKate Stone StateIsRunningState(thread.GetState())) { 2331b9c1b51eSKate Stone // We will need to wait for this new thread to stop as well before firing 2332b9c1b51eSKate Stone // the 2333c076559aSPavel Labath // notification. 2334f9077782SPavel Labath thread.RequestStop(); 2335c076559aSPavel Labath } 2336c076559aSPavel Labath } 2337068f8a7eSTamas Berghammer 2338b9c1b51eSKate Stone void NativeProcessLinux::SigchldHandler() { 2339a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 234019cbe96aSPavel Labath // Process all pending waitpid notifications. 2341b9c1b51eSKate Stone while (true) { 234219cbe96aSPavel Labath int status = -1; 234319cbe96aSPavel Labath ::pid_t wait_pid = waitpid(-1, &status, __WALL | __WNOTHREAD | WNOHANG); 234419cbe96aSPavel Labath 234519cbe96aSPavel Labath if (wait_pid == 0) 234619cbe96aSPavel Labath break; // We are done. 234719cbe96aSPavel Labath 2348b9c1b51eSKate Stone if (wait_pid == -1) { 234919cbe96aSPavel Labath if (errno == EINTR) 235019cbe96aSPavel Labath continue; 235119cbe96aSPavel Labath 235297206d57SZachary Turner Status error(errno, eErrorTypePOSIX); 2353a6321a8eSPavel Labath LLDB_LOG(log, "waitpid (-1, &status, _) failed: {0}", error); 235419cbe96aSPavel Labath break; 235519cbe96aSPavel Labath } 235619cbe96aSPavel Labath 2357*3508fc8cSPavel Labath WaitStatus wait_status = WaitStatus::Decode(status); 2358*3508fc8cSPavel Labath bool exited = wait_status.type == WaitStatus::Exit || 2359*3508fc8cSPavel Labath (wait_status.type == WaitStatus::Signal && 2360*3508fc8cSPavel Labath wait_pid == static_cast<::pid_t>(GetID())); 236119cbe96aSPavel Labath 2362*3508fc8cSPavel Labath LLDB_LOG( 2363*3508fc8cSPavel Labath log, 2364*3508fc8cSPavel Labath "waitpid (-1, &status, _) => pid = {0}, status = {1}, exited = {2}", 2365*3508fc8cSPavel Labath wait_pid, wait_status, exited); 236619cbe96aSPavel Labath 2367*3508fc8cSPavel Labath MonitorCallback(wait_pid, exited, wait_status); 236819cbe96aSPavel Labath } 2369068f8a7eSTamas Berghammer } 2370068f8a7eSTamas Berghammer 2371068f8a7eSTamas Berghammer // Wrapper for ptrace to catch errors and log calls. 2372b9c1b51eSKate Stone // Note that ptrace sets errno on error because -1 can be a valid result (i.e. 2373b9c1b51eSKate Stone // for PTRACE_PEEK*) 237497206d57SZachary Turner Status NativeProcessLinux::PtraceWrapper(int req, lldb::pid_t pid, void *addr, 2375b9c1b51eSKate Stone void *data, size_t data_size, 2376b9c1b51eSKate Stone long *result) { 237797206d57SZachary Turner Status error; 23784a9babb2SPavel Labath long int ret; 2379068f8a7eSTamas Berghammer 2380068f8a7eSTamas Berghammer Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 2381068f8a7eSTamas Berghammer 2382068f8a7eSTamas Berghammer PtraceDisplayBytes(req, data, data_size); 2383068f8a7eSTamas Berghammer 2384068f8a7eSTamas Berghammer errno = 0; 2385068f8a7eSTamas Berghammer if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) 2386b9c1b51eSKate Stone ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), 2387b9c1b51eSKate Stone *(unsigned int *)addr, data); 2388068f8a7eSTamas Berghammer else 2389b9c1b51eSKate Stone ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), 2390b9c1b51eSKate Stone addr, data); 2391068f8a7eSTamas Berghammer 23924a9babb2SPavel Labath if (ret == -1) 2393068f8a7eSTamas Berghammer error.SetErrorToErrno(); 2394068f8a7eSTamas Berghammer 23954a9babb2SPavel Labath if (result) 23964a9babb2SPavel Labath *result = ret; 23974a9babb2SPavel Labath 239828096200SPavel Labath LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3}, {4})={5:x}", req, pid, addr, data, 239928096200SPavel Labath data_size, ret); 2400068f8a7eSTamas Berghammer 2401068f8a7eSTamas Berghammer PtraceDisplayBytes(req, data, data_size); 2402068f8a7eSTamas Berghammer 2403a6321a8eSPavel Labath if (error.Fail()) 2404a6321a8eSPavel Labath LLDB_LOG(log, "ptrace() failed: {0}", error); 2405068f8a7eSTamas Berghammer 24064a9babb2SPavel Labath return error; 2407068f8a7eSTamas Berghammer } 2408