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: { 18011edb4eeSPavel Labath // 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 21782abefa4SPavel Labath llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 21896e600fcSPavel Labath NativeProcessLinux::Factory::Launch(ProcessLaunchInfo &launch_info, 21996e600fcSPavel Labath NativeDelegate &native_delegate, 22096e600fcSPavel Labath MainLoop &mainloop) const { 221a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 222af245d11STodd Fiala 22396e600fcSPavel Labath MaybeLogLaunchInfo(launch_info); 224af245d11STodd Fiala 22596e600fcSPavel Labath Status status; 22696e600fcSPavel Labath ::pid_t pid = ProcessLauncherPosixFork() 22796e600fcSPavel Labath .LaunchProcess(launch_info, status) 22896e600fcSPavel Labath .GetProcessId(); 22996e600fcSPavel Labath LLDB_LOG(log, "pid = {0:x}", pid); 23096e600fcSPavel Labath if (status.Fail()) { 23196e600fcSPavel Labath LLDB_LOG(log, "failed to launch process: {0}", status); 23296e600fcSPavel Labath return status.ToError(); 233af245d11STodd Fiala } 234af245d11STodd Fiala 23596e600fcSPavel Labath // Wait for the child process to trap on its call to execve. 23696e600fcSPavel Labath int wstatus; 23796e600fcSPavel Labath ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, ::waitpid, pid, &wstatus, 0); 23896e600fcSPavel Labath assert(wpid == pid); 23996e600fcSPavel Labath (void)wpid; 24096e600fcSPavel Labath if (!WIFSTOPPED(wstatus)) { 24196e600fcSPavel Labath LLDB_LOG(log, "Could not sync with inferior process: wstatus={1}", 24296e600fcSPavel Labath WaitStatus::Decode(wstatus)); 24396e600fcSPavel Labath return llvm::make_error<StringError>("Could not sync with inferior process", 24496e600fcSPavel Labath llvm::inconvertibleErrorCode()); 24596e600fcSPavel Labath } 24696e600fcSPavel Labath LLDB_LOG(log, "inferior started, now in stopped state"); 247af245d11STodd Fiala 24896e600fcSPavel Labath ArchSpec arch; 24996e600fcSPavel Labath if ((status = ResolveProcessArchitecture(pid, arch)).Fail()) 25096e600fcSPavel Labath return status.ToError(); 25196e600fcSPavel Labath 25296e600fcSPavel Labath // Set the architecture to the exe architecture. 25396e600fcSPavel Labath LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid, 25496e600fcSPavel Labath arch.GetArchitectureName()); 25596e600fcSPavel Labath 25696e600fcSPavel Labath status = SetDefaultPtraceOpts(pid); 25796e600fcSPavel Labath if (status.Fail()) { 25896e600fcSPavel Labath LLDB_LOG(log, "failed to set default ptrace options: {0}", status); 25996e600fcSPavel Labath return status.ToError(); 260af245d11STodd Fiala } 261af245d11STodd Fiala 26282abefa4SPavel Labath return std::unique_ptr<NativeProcessLinux>(new NativeProcessLinux( 26396e600fcSPavel Labath pid, launch_info.GetPTY().ReleaseMasterFileDescriptor(), native_delegate, 26482abefa4SPavel Labath arch, mainloop, {pid})); 265af245d11STodd Fiala } 266af245d11STodd Fiala 26782abefa4SPavel Labath llvm::Expected<std::unique_ptr<NativeProcessProtocol>> 26882abefa4SPavel Labath NativeProcessLinux::Factory::Attach( 269b9c1b51eSKate Stone lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, 27096e600fcSPavel Labath MainLoop &mainloop) const { 271a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 272a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0:x}", pid); 273af245d11STodd Fiala 274af245d11STodd Fiala // Retrieve the architecture for the running process. 27596e600fcSPavel Labath ArchSpec arch; 27696e600fcSPavel Labath Status status = ResolveProcessArchitecture(pid, arch); 27796e600fcSPavel Labath if (!status.Success()) 27896e600fcSPavel Labath return status.ToError(); 279af245d11STodd Fiala 28096e600fcSPavel Labath auto tids_or = NativeProcessLinux::Attach(pid); 28196e600fcSPavel Labath if (!tids_or) 28296e600fcSPavel Labath return tids_or.takeError(); 283af245d11STodd Fiala 28482abefa4SPavel Labath return std::unique_ptr<NativeProcessLinux>(new NativeProcessLinux( 28582abefa4SPavel Labath pid, -1, native_delegate, arch, mainloop, *tids_or)); 286af245d11STodd Fiala } 287af245d11STodd Fiala 288af245d11STodd Fiala // ----------------------------------------------------------------------------- 289af245d11STodd Fiala // Public Instance Methods 290af245d11STodd Fiala // ----------------------------------------------------------------------------- 291af245d11STodd Fiala 29296e600fcSPavel Labath NativeProcessLinux::NativeProcessLinux(::pid_t pid, int terminal_fd, 29396e600fcSPavel Labath NativeDelegate &delegate, 29482abefa4SPavel Labath const ArchSpec &arch, MainLoop &mainloop, 29582abefa4SPavel Labath llvm::ArrayRef<::pid_t> tids) 29696e600fcSPavel Labath : NativeProcessProtocol(pid, terminal_fd, delegate), m_arch(arch) { 297b9c1b51eSKate Stone if (m_terminal_fd != -1) { 29896e600fcSPavel Labath Status status = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); 29996e600fcSPavel Labath assert(status.Success()); 3005ad891f7SPavel Labath } 301af245d11STodd Fiala 30296e600fcSPavel Labath Status status; 30396e600fcSPavel Labath m_sigchld_handle = mainloop.RegisterSignal( 30496e600fcSPavel Labath SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, status); 30596e600fcSPavel Labath assert(m_sigchld_handle && status.Success()); 30696e600fcSPavel Labath 30796e600fcSPavel Labath for (const auto &tid : tids) { 308a5be48b3SPavel Labath NativeThreadLinux &thread = AddThread(tid); 309a5be48b3SPavel Labath thread.SetStoppedBySignal(SIGSTOP); 310a5be48b3SPavel Labath ThreadWasCreated(thread); 311af245d11STodd Fiala } 312af245d11STodd Fiala 31396e600fcSPavel Labath // Let our process instance know the thread has stopped. 31496e600fcSPavel Labath SetCurrentThreadID(tids[0]); 31596e600fcSPavel Labath SetState(StateType::eStateStopped, false); 31696e600fcSPavel Labath 31796e600fcSPavel Labath // Proccess any signals we received before installing our handler 31896e600fcSPavel Labath SigchldHandler(); 31996e600fcSPavel Labath } 32096e600fcSPavel Labath 32196e600fcSPavel Labath llvm::Expected<std::vector<::pid_t>> NativeProcessLinux::Attach(::pid_t pid) { 322a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 323af245d11STodd Fiala 32496e600fcSPavel Labath Status status; 325b9c1b51eSKate Stone // Use a map to keep track of the threads which we have attached/need to 326b9c1b51eSKate Stone // attach. 327af245d11STodd Fiala Host::TidMap tids_to_attach; 328b9c1b51eSKate Stone while (Host::FindProcessThreads(pid, tids_to_attach)) { 329af245d11STodd Fiala for (Host::TidMap::iterator it = tids_to_attach.begin(); 330b9c1b51eSKate Stone it != tids_to_attach.end();) { 331b9c1b51eSKate Stone if (it->second == false) { 332af245d11STodd Fiala lldb::tid_t tid = it->first; 333af245d11STodd Fiala 334af245d11STodd Fiala // Attach to the requested process. 335af245d11STodd Fiala // An attach will cause the thread to stop with a SIGSTOP. 33696e600fcSPavel Labath if ((status = PtraceWrapper(PTRACE_ATTACH, tid)).Fail()) { 337af245d11STodd Fiala // No such thread. The thread may have exited. 338af245d11STodd Fiala // More error handling may be needed. 33996e600fcSPavel Labath if (status.GetError() == ESRCH) { 340af245d11STodd Fiala it = tids_to_attach.erase(it); 341af245d11STodd Fiala continue; 34296e600fcSPavel Labath } 34396e600fcSPavel Labath return status.ToError(); 344af245d11STodd Fiala } 345af245d11STodd Fiala 34696e600fcSPavel Labath int wpid = 34796e600fcSPavel Labath llvm::sys::RetryAfterSignal(-1, ::waitpid, tid, nullptr, __WALL); 348af245d11STodd Fiala // Need to use __WALL otherwise we receive an error with errno=ECHLD 349af245d11STodd Fiala // At this point we should have a thread stopped if waitpid succeeds. 35096e600fcSPavel Labath if (wpid < 0) { 351af245d11STodd Fiala // No such thread. The thread may have exited. 352af245d11STodd Fiala // More error handling may be needed. 353b9c1b51eSKate Stone if (errno == ESRCH) { 354af245d11STodd Fiala it = tids_to_attach.erase(it); 355af245d11STodd Fiala continue; 356af245d11STodd Fiala } 35796e600fcSPavel Labath return llvm::errorCodeToError( 35896e600fcSPavel Labath std::error_code(errno, std::generic_category())); 359af245d11STodd Fiala } 360af245d11STodd Fiala 36196e600fcSPavel Labath if ((status = SetDefaultPtraceOpts(tid)).Fail()) 36296e600fcSPavel Labath return status.ToError(); 363af245d11STodd Fiala 364a6321a8eSPavel Labath LLDB_LOG(log, "adding tid = {0}", tid); 365af245d11STodd Fiala it->second = true; 366af245d11STodd Fiala } 367af245d11STodd Fiala 368af245d11STodd Fiala // move the loop forward 369af245d11STodd Fiala ++it; 370af245d11STodd Fiala } 371af245d11STodd Fiala } 372af245d11STodd Fiala 37396e600fcSPavel Labath size_t tid_count = tids_to_attach.size(); 37496e600fcSPavel Labath if (tid_count == 0) 37596e600fcSPavel Labath return llvm::make_error<StringError>("No such process", 37696e600fcSPavel Labath llvm::inconvertibleErrorCode()); 377af245d11STodd Fiala 37896e600fcSPavel Labath std::vector<::pid_t> tids; 37996e600fcSPavel Labath tids.reserve(tid_count); 38096e600fcSPavel Labath for (const auto &p : tids_to_attach) 38196e600fcSPavel Labath tids.push_back(p.first); 38296e600fcSPavel Labath return std::move(tids); 383af245d11STodd Fiala } 384af245d11STodd Fiala 38597206d57SZachary Turner Status NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) { 386af245d11STodd Fiala long ptrace_opts = 0; 387af245d11STodd Fiala 388af245d11STodd Fiala // Have the child raise an event on exit. This is used to keep the child in 389af245d11STodd Fiala // limbo until it is destroyed. 390af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXIT; 391af245d11STodd Fiala 392af245d11STodd Fiala // Have the tracer trace threads which spawn in the inferior process. 393af245d11STodd Fiala // TODO: if we want to support tracing the inferiors' child, add the 394af245d11STodd Fiala // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK) 395af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACECLONE; 396af245d11STodd Fiala 397af245d11STodd Fiala // Have the tracer notify us before execve returns 398af245d11STodd Fiala // (needed to disable legacy SIGTRAP generation) 399af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXEC; 400af245d11STodd Fiala 4014a9babb2SPavel Labath return PtraceWrapper(PTRACE_SETOPTIONS, pid, nullptr, (void *)ptrace_opts); 402af245d11STodd Fiala } 403af245d11STodd Fiala 4041107b5a5SPavel Labath // Handles all waitpid events from the inferior process. 405b9c1b51eSKate Stone void NativeProcessLinux::MonitorCallback(lldb::pid_t pid, bool exited, 4063508fc8cSPavel Labath WaitStatus status) { 407af245d11STodd Fiala Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 408af245d11STodd Fiala 409b9c1b51eSKate Stone // Certain activities differ based on whether the pid is the tid of the main 410b9c1b51eSKate Stone // thread. 4111107b5a5SPavel Labath const bool is_main_thread = (pid == GetID()); 412af245d11STodd Fiala 413af245d11STodd Fiala // Handle when the thread exits. 414b9c1b51eSKate Stone if (exited) { 415*d8b3c1a1SPavel Labath LLDB_LOG(log, 416*d8b3c1a1SPavel Labath "got exit signal({0}) , tid = {1} ({2} main thread), process " 417*d8b3c1a1SPavel Labath "state = {3}", 418*d8b3c1a1SPavel Labath signal, pid, is_main_thread ? "is" : "is not", GetState()); 419af245d11STodd Fiala 420af245d11STodd Fiala // This is a thread that exited. Ensure we're not tracking it anymore. 421*d8b3c1a1SPavel Labath StopTrackingThread(pid); 422af245d11STodd Fiala 423b9c1b51eSKate Stone if (is_main_thread) { 424af245d11STodd Fiala // The main thread exited. We're done monitoring. Report to delegate. 4253508fc8cSPavel Labath SetExitStatus(status, true); 426af245d11STodd Fiala 427af245d11STodd Fiala // Notify delegate that our process has exited. 4281107b5a5SPavel Labath SetState(StateType::eStateExited, true); 429af245d11STodd Fiala } 4301107b5a5SPavel Labath return; 431af245d11STodd Fiala } 432af245d11STodd Fiala 433af245d11STodd Fiala siginfo_t info; 434b9cc0c75SPavel Labath const auto info_err = GetSignalInfo(pid, &info); 435b9cc0c75SPavel Labath auto thread_sp = GetThreadByID(pid); 436b9cc0c75SPavel Labath 437b9c1b51eSKate Stone if (!thread_sp) { 438b9c1b51eSKate Stone // Normally, the only situation when we cannot find the thread is if we have 439a6321a8eSPavel Labath // just received a new thread notification. This is indicated by 440a6321a8eSPavel Labath // GetSignalInfo() returning si_code == SI_USER and si_pid == 0 441a6321a8eSPavel Labath LLDB_LOG(log, "received notification about an unknown tid {0}.", pid); 442b9cc0c75SPavel Labath 443b9c1b51eSKate Stone if (info_err.Fail()) { 444a6321a8eSPavel Labath LLDB_LOG(log, 445a6321a8eSPavel Labath "(tid {0}) GetSignalInfo failed ({1}). " 446a6321a8eSPavel Labath "Ingoring this notification.", 447a6321a8eSPavel Labath pid, info_err); 448b9cc0c75SPavel Labath return; 449b9cc0c75SPavel Labath } 450b9cc0c75SPavel Labath 451a6321a8eSPavel Labath LLDB_LOG(log, "tid {0}, si_code: {1}, si_pid: {2}", pid, info.si_code, 452a6321a8eSPavel Labath info.si_pid); 453b9cc0c75SPavel Labath 454a5be48b3SPavel Labath NativeThreadLinux &thread = AddThread(pid); 45599e37695SRavitheja Addepally 456b9cc0c75SPavel Labath // Resume the newly created thread. 457a5be48b3SPavel Labath ResumeThread(thread, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); 458a5be48b3SPavel Labath ThreadWasCreated(thread); 459b9cc0c75SPavel Labath return; 460b9cc0c75SPavel Labath } 461b9cc0c75SPavel Labath 462b9cc0c75SPavel Labath // Get details on the signal raised. 463b9c1b51eSKate Stone if (info_err.Success()) { 464fa03ad2eSChaoren Lin // We have retrieved the signal info. Dispatch appropriately. 465fa03ad2eSChaoren Lin if (info.si_signo == SIGTRAP) 466b9cc0c75SPavel Labath MonitorSIGTRAP(info, *thread_sp); 467fa03ad2eSChaoren Lin else 468b9cc0c75SPavel Labath MonitorSignal(info, *thread_sp, exited); 469b9c1b51eSKate Stone } else { 470b9c1b51eSKate Stone if (info_err.GetError() == EINVAL) { 471fa03ad2eSChaoren Lin // This is a group stop reception for this tid. 472b9c1b51eSKate Stone // We can reach here if we reinject SIGSTOP, SIGSTP, SIGTTIN or SIGTTOU 473a6321a8eSPavel Labath // into the tracee, triggering the group-stop mechanism. Normally 474a6321a8eSPavel Labath // receiving these would stop the process, pending a SIGCONT. Simulating 475a6321a8eSPavel Labath // this state in a debugger is hard and is generally not needed (one use 476a6321a8eSPavel Labath // case is debugging background task being managed by a shell). For 477a6321a8eSPavel Labath // general use, it is sufficient to stop the process in a signal-delivery 478b9c1b51eSKate Stone // stop which happens before the group stop. This done by MonitorSignal 479a6321a8eSPavel Labath // and works correctly for all signals. 480a6321a8eSPavel Labath LLDB_LOG(log, 481a6321a8eSPavel Labath "received a group stop for pid {0} tid {1}. Transparent " 482a6321a8eSPavel Labath "handling of group stops not supported, resuming the " 483a6321a8eSPavel Labath "thread.", 484a6321a8eSPavel Labath GetID(), pid); 485b9c1b51eSKate Stone ResumeThread(*thread_sp, thread_sp->GetState(), 486b9c1b51eSKate Stone LLDB_INVALID_SIGNAL_NUMBER); 487b9c1b51eSKate Stone } else { 488af245d11STodd Fiala // ptrace(GETSIGINFO) failed (but not due to group-stop). 489af245d11STodd Fiala 490b9c1b51eSKate Stone // A return value of ESRCH means the thread/process is no longer on the 491a6321a8eSPavel Labath // system, so it was killed somehow outside of our control. Either way, 492a6321a8eSPavel Labath // we can't do anything with it anymore. 493af245d11STodd Fiala 494b9c1b51eSKate Stone // Stop tracking the metadata for the thread since it's entirely off the 495b9c1b51eSKate Stone // system now. 4961107b5a5SPavel Labath const bool thread_found = StopTrackingThread(pid); 497af245d11STodd Fiala 498a6321a8eSPavel Labath LLDB_LOG(log, 499a6321a8eSPavel Labath "GetSignalInfo failed: {0}, tid = {1}, signal = {2}, " 500a6321a8eSPavel Labath "status = {3}, main_thread = {4}, thread_found: {5}", 501a6321a8eSPavel Labath info_err, pid, signal, status, is_main_thread, thread_found); 502af245d11STodd Fiala 503b9c1b51eSKate Stone if (is_main_thread) { 504b9c1b51eSKate Stone // Notify the delegate - our process is not available but appears to 505b9c1b51eSKate Stone // have been killed outside 506af245d11STodd Fiala // our control. Is eStateExited the right exit state in this case? 5073508fc8cSPavel Labath SetExitStatus(status, true); 5081107b5a5SPavel Labath SetState(StateType::eStateExited, true); 509b9c1b51eSKate Stone } else { 510b9c1b51eSKate Stone // This thread was pulled out from underneath us. Anything to do here? 511b9c1b51eSKate Stone // Do we want to do an all stop? 512a6321a8eSPavel Labath LLDB_LOG(log, 513a6321a8eSPavel Labath "pid {0} tid {1} non-main thread exit occurred, didn't " 514a6321a8eSPavel Labath "tell delegate anything since thread disappeared out " 515a6321a8eSPavel Labath "from underneath us", 516a6321a8eSPavel Labath GetID(), pid); 517af245d11STodd Fiala } 518af245d11STodd Fiala } 519af245d11STodd Fiala } 520af245d11STodd Fiala } 521af245d11STodd Fiala 522b9c1b51eSKate Stone void NativeProcessLinux::WaitForNewThread(::pid_t tid) { 523a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 524426bdf88SPavel Labath 525a5be48b3SPavel Labath if (GetThreadByID(tid)) { 526b9c1b51eSKate Stone // We are already tracking the thread - we got the event on the new thread 527a5be48b3SPavel Labath // (see MonitorSignal) before this one. We are done. 528426bdf88SPavel Labath return; 529426bdf88SPavel Labath } 530426bdf88SPavel Labath 531426bdf88SPavel Labath // The thread is not tracked yet, let's wait for it to appear. 532426bdf88SPavel Labath int status = -1; 533a6321a8eSPavel Labath LLDB_LOG(log, 534a6321a8eSPavel Labath "received thread creation event for tid {0}. tid not tracked " 535a6321a8eSPavel Labath "yet, waiting for thread to appear...", 536a6321a8eSPavel Labath tid); 537c1a6b128SPavel Labath ::pid_t wait_pid = llvm::sys::RetryAfterSignal(-1, ::waitpid, tid, &status, __WALL); 538b9c1b51eSKate Stone // Since we are waiting on a specific tid, this must be the creation event. 539a6321a8eSPavel Labath // But let's do some checks just in case. 540426bdf88SPavel Labath if (wait_pid != tid) { 541a6321a8eSPavel Labath LLDB_LOG(log, 542a6321a8eSPavel Labath "waiting for tid {0} failed. Assuming the thread has " 543a6321a8eSPavel Labath "disappeared in the meantime", 544a6321a8eSPavel Labath tid); 545426bdf88SPavel Labath // The only way I know of this could happen is if the whole process was 546b9c1b51eSKate Stone // SIGKILLed in the mean time. In any case, we can't do anything about that 547b9c1b51eSKate Stone // now. 548426bdf88SPavel Labath return; 549426bdf88SPavel Labath } 550b9c1b51eSKate Stone if (WIFEXITED(status)) { 551a6321a8eSPavel Labath LLDB_LOG(log, 552a6321a8eSPavel Labath "waiting for tid {0} returned an 'exited' event. Not " 553a6321a8eSPavel Labath "tracking the thread.", 554a6321a8eSPavel Labath tid); 555426bdf88SPavel Labath // Also a very improbable event. 556426bdf88SPavel Labath return; 557426bdf88SPavel Labath } 558426bdf88SPavel Labath 559a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0}: tracking new thread tid {1}", GetID(), tid); 560a5be48b3SPavel Labath NativeThreadLinux &new_thread = AddThread(tid); 56199e37695SRavitheja Addepally 562a5be48b3SPavel Labath ResumeThread(new_thread, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); 563a5be48b3SPavel Labath ThreadWasCreated(new_thread); 564426bdf88SPavel Labath } 565426bdf88SPavel Labath 566b9c1b51eSKate Stone void NativeProcessLinux::MonitorSIGTRAP(const siginfo_t &info, 567b9c1b51eSKate Stone NativeThreadLinux &thread) { 568a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 569b9cc0c75SPavel Labath const bool is_main_thread = (thread.GetID() == GetID()); 570af245d11STodd Fiala 571b9cc0c75SPavel Labath assert(info.si_signo == SIGTRAP && "Unexpected child signal!"); 572af245d11STodd Fiala 573b9c1b51eSKate Stone switch (info.si_code) { 574b9c1b51eSKate Stone // TODO: these two cases are required if we want to support tracing of the 575b9c1b51eSKate Stone // inferiors' children. We'd need this to debug a monitor. 576af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)): 577af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)): 578af245d11STodd Fiala 579b9c1b51eSKate Stone case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)): { 580b9c1b51eSKate Stone // This is the notification on the parent thread which informs us of new 581b9c1b51eSKate Stone // thread 582426bdf88SPavel Labath // creation. 583b9c1b51eSKate Stone // We don't want to do anything with the parent thread so we just resume it. 584b9c1b51eSKate Stone // In case we 585b9c1b51eSKate Stone // want to implement "break on thread creation" functionality, we would need 586b9c1b51eSKate Stone // to stop 587426bdf88SPavel Labath // here. 588af245d11STodd Fiala 589af245d11STodd Fiala unsigned long event_message = 0; 590b9c1b51eSKate Stone if (GetEventMessage(thread.GetID(), &event_message).Fail()) { 591a6321a8eSPavel Labath LLDB_LOG(log, 592a6321a8eSPavel Labath "pid {0} received thread creation event but " 593a6321a8eSPavel Labath "GetEventMessage failed so we don't know the new tid", 594a6321a8eSPavel Labath thread.GetID()); 595426bdf88SPavel Labath } else 596426bdf88SPavel Labath WaitForNewThread(event_message); 597af245d11STodd Fiala 598b9cc0c75SPavel Labath ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); 599af245d11STodd Fiala break; 600af245d11STodd Fiala } 601af245d11STodd Fiala 602b9c1b51eSKate Stone case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): { 603a6321a8eSPavel Labath LLDB_LOG(log, "received exec event, code = {0}", info.si_code ^ SIGTRAP); 604a9882ceeSTodd Fiala 6051dbc6c9cSPavel Labath // Exec clears any pending notifications. 6060e1d729bSPavel Labath m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 607fa03ad2eSChaoren Lin 608b9c1b51eSKate Stone // Remove all but the main thread here. Linux fork creates a new process 609b9c1b51eSKate Stone // which only copies the main thread. 610a6321a8eSPavel Labath LLDB_LOG(log, "exec received, stop tracking all but main thread"); 611a9882ceeSTodd Fiala 612a5be48b3SPavel Labath for (auto i = m_threads.begin(); i != m_threads.end();) { 613a5be48b3SPavel Labath if ((*i)->GetID() == GetID()) 614a5be48b3SPavel Labath i = m_threads.erase(i); 615a5be48b3SPavel Labath else 616a5be48b3SPavel Labath ++i; 617a9882ceeSTodd Fiala } 618a5be48b3SPavel Labath assert(m_threads.size() == 1); 619a5be48b3SPavel Labath auto *main_thread = static_cast<NativeThreadLinux *>(m_threads[0].get()); 620a9882ceeSTodd Fiala 621a5be48b3SPavel Labath SetCurrentThreadID(main_thread->GetID()); 622a5be48b3SPavel Labath main_thread->SetStoppedByExec(); 623a9882ceeSTodd Fiala 624fa03ad2eSChaoren Lin // Tell coordinator about about the "new" (since exec) stopped main thread. 625a5be48b3SPavel Labath ThreadWasCreated(*main_thread); 626fa03ad2eSChaoren Lin 627a9882ceeSTodd Fiala // Let our delegate know we have just exec'd. 628a9882ceeSTodd Fiala NotifyDidExec(); 629a9882ceeSTodd Fiala 630fa03ad2eSChaoren Lin // Let the process know we're stopped. 631a5be48b3SPavel Labath StopRunningThreads(main_thread->GetID()); 632a9882ceeSTodd Fiala 633af245d11STodd Fiala break; 634a9882ceeSTodd Fiala } 635af245d11STodd Fiala 636b9c1b51eSKate Stone case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): { 637af245d11STodd Fiala // The inferior process or one of its threads is about to exit. 638b9c1b51eSKate Stone // We don't want to do anything with the thread so we just resume it. In 639*d8b3c1a1SPavel Labath // case we want to implement "break on thread exit" functionality, we would 640*d8b3c1a1SPavel Labath // need to stop here. 641fa03ad2eSChaoren Lin 642af245d11STodd Fiala unsigned long data = 0; 643b9cc0c75SPavel Labath if (GetEventMessage(thread.GetID(), &data).Fail()) 644af245d11STodd Fiala data = -1; 645af245d11STodd Fiala 646a6321a8eSPavel Labath LLDB_LOG(log, 647a6321a8eSPavel Labath "received PTRACE_EVENT_EXIT, data = {0:x}, WIFEXITED={1}, " 648a6321a8eSPavel Labath "WIFSIGNALED={2}, pid = {3}, main_thread = {4}", 649a6321a8eSPavel Labath data, WIFEXITED(data), WIFSIGNALED(data), thread.GetID(), 650a6321a8eSPavel Labath is_main_thread); 651af245d11STodd Fiala 65275f47c3aSTodd Fiala 65386852d36SPavel Labath StateType state = thread.GetState(); 654b9c1b51eSKate Stone if (!StateIsRunningState(state)) { 655b9c1b51eSKate Stone // Due to a kernel bug, we may sometimes get this stop after the inferior 656*d8b3c1a1SPavel Labath // gets a SIGKILL. This confuses our state tracking logic in 657*d8b3c1a1SPavel Labath // ResumeThread(), since normally, we should not be receiving any ptrace 658*d8b3c1a1SPavel Labath // events while the inferior is stopped. This makes sure that the inferior 659*d8b3c1a1SPavel Labath // is resumed and exits normally. 66086852d36SPavel Labath state = eStateRunning; 66186852d36SPavel Labath } 66286852d36SPavel Labath ResumeThread(thread, state, LLDB_INVALID_SIGNAL_NUMBER); 663af245d11STodd Fiala 664af245d11STodd Fiala break; 665af245d11STodd Fiala } 666af245d11STodd Fiala 667af245d11STodd Fiala case 0: 668c16f5dcaSChaoren Lin case TRAP_TRACE: // We receive this on single stepping. 669c16f5dcaSChaoren Lin case TRAP_HWBKPT: // We receive this on watchpoint hit 67086fd8e45SChaoren Lin { 671c16f5dcaSChaoren Lin // If a watchpoint was hit, report it 672c16f5dcaSChaoren Lin uint32_t wp_index; 673d37349f3SPavel Labath Status error = thread.GetRegisterContext().GetWatchpointHitIndex( 674b9c1b51eSKate Stone wp_index, (uintptr_t)info.si_addr); 675a6321a8eSPavel Labath if (error.Fail()) 676a6321a8eSPavel Labath LLDB_LOG(log, 677a6321a8eSPavel Labath "received error while checking for watchpoint hits, pid = " 678a6321a8eSPavel Labath "{0}, error = {1}", 679a6321a8eSPavel Labath thread.GetID(), error); 680b9c1b51eSKate Stone if (wp_index != LLDB_INVALID_INDEX32) { 681b9cc0c75SPavel Labath MonitorWatchpoint(thread, wp_index); 682c16f5dcaSChaoren Lin break; 683c16f5dcaSChaoren Lin } 684b9cc0c75SPavel Labath 685d5ffbad2SOmair Javaid // If a breakpoint was hit, report it 686d5ffbad2SOmair Javaid uint32_t bp_index; 687d37349f3SPavel Labath error = thread.GetRegisterContext().GetHardwareBreakHitIndex( 688d5ffbad2SOmair Javaid bp_index, (uintptr_t)info.si_addr); 689d5ffbad2SOmair Javaid if (error.Fail()) 690d5ffbad2SOmair Javaid LLDB_LOG(log, "received error while checking for hardware " 691d5ffbad2SOmair Javaid "breakpoint hits, pid = {0}, error = {1}", 692d5ffbad2SOmair Javaid thread.GetID(), error); 693d5ffbad2SOmair Javaid if (bp_index != LLDB_INVALID_INDEX32) { 694d5ffbad2SOmair Javaid MonitorBreakpoint(thread); 695d5ffbad2SOmair Javaid break; 696d5ffbad2SOmair Javaid } 697d5ffbad2SOmair Javaid 698be379e15STamas Berghammer // Otherwise, report step over 699be379e15STamas Berghammer MonitorTrace(thread); 700af245d11STodd Fiala break; 701b9cc0c75SPavel Labath } 702af245d11STodd Fiala 703af245d11STodd Fiala case SI_KERNEL: 70435799963SMohit K. Bhakkad #if defined __mips__ 70535799963SMohit K. Bhakkad // For mips there is no special signal for watchpoint 70635799963SMohit K. Bhakkad // So we check for watchpoint in kernel trap 70735799963SMohit K. Bhakkad { 70835799963SMohit K. Bhakkad // If a watchpoint was hit, report it 70935799963SMohit K. Bhakkad uint32_t wp_index; 710d37349f3SPavel Labath Status error = thread.GetRegisterContext().GetWatchpointHitIndex( 711b9c1b51eSKate Stone wp_index, LLDB_INVALID_ADDRESS); 712a6321a8eSPavel Labath if (error.Fail()) 713a6321a8eSPavel Labath LLDB_LOG(log, 714a6321a8eSPavel Labath "received error while checking for watchpoint hits, pid = " 715a6321a8eSPavel Labath "{0}, error = {1}", 716a6321a8eSPavel Labath thread.GetID(), error); 717b9c1b51eSKate Stone if (wp_index != LLDB_INVALID_INDEX32) { 718b9cc0c75SPavel Labath MonitorWatchpoint(thread, wp_index); 71935799963SMohit K. Bhakkad break; 72035799963SMohit K. Bhakkad } 72135799963SMohit K. Bhakkad } 72235799963SMohit K. Bhakkad // NO BREAK 72335799963SMohit K. Bhakkad #endif 724af245d11STodd Fiala case TRAP_BRKPT: 725b9cc0c75SPavel Labath MonitorBreakpoint(thread); 726af245d11STodd Fiala break; 727af245d11STodd Fiala 728af245d11STodd Fiala case SIGTRAP: 729af245d11STodd Fiala case (SIGTRAP | 0x80): 730a6321a8eSPavel Labath LLDB_LOG( 731a6321a8eSPavel Labath log, 732a6321a8eSPavel Labath "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming", 733a6321a8eSPavel Labath info.si_code, GetID(), thread.GetID()); 734fa03ad2eSChaoren Lin 735af245d11STodd Fiala // Ignore these signals until we know more about them. 736b9cc0c75SPavel Labath ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); 737af245d11STodd Fiala break; 738af245d11STodd Fiala 739af245d11STodd Fiala default: 74021a365baSPavel Labath LLDB_LOG(log, "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}", 741a6321a8eSPavel Labath info.si_code, GetID(), thread.GetID()); 74221a365baSPavel Labath MonitorSignal(info, thread, false); 743af245d11STodd Fiala break; 744af245d11STodd Fiala } 745af245d11STodd Fiala } 746af245d11STodd Fiala 747b9c1b51eSKate Stone void NativeProcessLinux::MonitorTrace(NativeThreadLinux &thread) { 748a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 749a6321a8eSPavel Labath LLDB_LOG(log, "received trace event, pid = {0}", thread.GetID()); 750c16f5dcaSChaoren Lin 7510e1d729bSPavel Labath // This thread is currently stopped. 752b9cc0c75SPavel Labath thread.SetStoppedByTrace(); 753c16f5dcaSChaoren Lin 754b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 755c16f5dcaSChaoren Lin } 756c16f5dcaSChaoren Lin 757b9c1b51eSKate Stone void NativeProcessLinux::MonitorBreakpoint(NativeThreadLinux &thread) { 758b9c1b51eSKate Stone Log *log( 759b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 760a6321a8eSPavel Labath LLDB_LOG(log, "received breakpoint event, pid = {0}", thread.GetID()); 761c16f5dcaSChaoren Lin 762c16f5dcaSChaoren Lin // Mark the thread as stopped at breakpoint. 763b9cc0c75SPavel Labath thread.SetStoppedByBreakpoint(); 76497206d57SZachary Turner Status error = FixupBreakpointPCAsNeeded(thread); 765c16f5dcaSChaoren Lin if (error.Fail()) 766a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0} fixup: {1}", thread.GetID(), error); 767d8c338d4STamas Berghammer 768b9c1b51eSKate Stone if (m_threads_stepping_with_breakpoint.find(thread.GetID()) != 769b9c1b51eSKate Stone m_threads_stepping_with_breakpoint.end()) 770b9cc0c75SPavel Labath thread.SetStoppedByTrace(); 771c16f5dcaSChaoren Lin 772b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 773c16f5dcaSChaoren Lin } 774c16f5dcaSChaoren Lin 775b9c1b51eSKate Stone void NativeProcessLinux::MonitorWatchpoint(NativeThreadLinux &thread, 776b9c1b51eSKate Stone uint32_t wp_index) { 777b9c1b51eSKate Stone Log *log( 778b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_WATCHPOINTS)); 779a6321a8eSPavel Labath LLDB_LOG(log, "received watchpoint event, pid = {0}, wp_index = {1}", 780a6321a8eSPavel Labath thread.GetID(), wp_index); 781c16f5dcaSChaoren Lin 782c16f5dcaSChaoren Lin // Mark the thread as stopped at watchpoint. 783c16f5dcaSChaoren Lin // The address is at (lldb::addr_t)info->si_addr if we need it. 784f9077782SPavel Labath thread.SetStoppedByWatchpoint(wp_index); 785c16f5dcaSChaoren Lin 786b9c1b51eSKate Stone // We need to tell all other running threads before we notify the delegate 787b9c1b51eSKate Stone // about this stop. 788f9077782SPavel Labath StopRunningThreads(thread.GetID()); 789c16f5dcaSChaoren Lin } 790c16f5dcaSChaoren Lin 791b9c1b51eSKate Stone void NativeProcessLinux::MonitorSignal(const siginfo_t &info, 792b9c1b51eSKate Stone NativeThreadLinux &thread, bool exited) { 793b9cc0c75SPavel Labath const int signo = info.si_signo; 794b9cc0c75SPavel Labath const bool is_from_llgs = info.si_pid == getpid(); 795af245d11STodd Fiala 796a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 797af245d11STodd Fiala 798af245d11STodd Fiala // POSIX says that process behaviour is undefined after it ignores a SIGFPE, 799af245d11STodd Fiala // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a 800af245d11STodd Fiala // kill(2) or raise(3). Similarly for tgkill(2) on Linux. 801af245d11STodd Fiala // 802af245d11STodd Fiala // IOW, user generated signals never generate what we consider to be a 803af245d11STodd Fiala // "crash". 804af245d11STodd Fiala // 805af245d11STodd Fiala // Similarly, ACK signals generated by this monitor. 806af245d11STodd Fiala 807af245d11STodd Fiala // Handle the signal. 808a6321a8eSPavel Labath LLDB_LOG(log, 809a6321a8eSPavel Labath "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, " 810a6321a8eSPavel Labath "waitpid pid = {4})", 811a6321a8eSPavel Labath Host::GetSignalAsCString(signo), signo, info.si_code, 812b9cc0c75SPavel Labath thread.GetID()); 81358a2f669STodd Fiala 81458a2f669STodd Fiala // Check for thread stop notification. 815b9c1b51eSKate Stone if (is_from_llgs && (info.si_code == SI_TKILL) && (signo == SIGSTOP)) { 816af245d11STodd Fiala // This is a tgkill()-based stop. 817a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} tid {1}, thread stopped", GetID(), thread.GetID()); 818fa03ad2eSChaoren Lin 819aab58633SChaoren Lin // Check that we're not already marked with a stop reason. 820b9c1b51eSKate Stone // Note this thread really shouldn't already be marked as stopped - if we 821a6321a8eSPavel Labath // were, that would imply that the kernel signaled us with the thread 822a6321a8eSPavel Labath // stopping which we handled and marked as stopped, and that, without an 823a6321a8eSPavel Labath // intervening resume, we received another stop. It is more likely that we 824a6321a8eSPavel Labath // are missing the marking of a run state somewhere if we find that the 825a6321a8eSPavel Labath // thread was marked as stopped. 826b9cc0c75SPavel Labath const StateType thread_state = thread.GetState(); 827b9c1b51eSKate Stone if (!StateIsStoppedState(thread_state, false)) { 828ed89c7feSPavel Labath // An inferior thread has stopped because of a SIGSTOP we have sent it. 829b9c1b51eSKate Stone // Generally, these are not important stops and we don't want to report 830a6321a8eSPavel Labath // them as they are just used to stop other threads when one thread (the 831a6321a8eSPavel Labath // one with the *real* stop reason) hits a breakpoint (watchpoint, 832a6321a8eSPavel Labath // etc...). However, in the case of an asynchronous Interrupt(), this *is* 833a6321a8eSPavel Labath // the real stop reason, so we leave the signal intact if this is the 834a6321a8eSPavel Labath // thread that was chosen as the triggering thread. 835b9c1b51eSKate Stone if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) { 836b9cc0c75SPavel Labath if (m_pending_notification_tid == thread.GetID()) 837b9cc0c75SPavel Labath thread.SetStoppedBySignal(SIGSTOP, &info); 838ed89c7feSPavel Labath else 839b9cc0c75SPavel Labath thread.SetStoppedWithNoReason(); 840ed89c7feSPavel Labath 841b9cc0c75SPavel Labath SetCurrentThreadID(thread.GetID()); 8420e1d729bSPavel Labath SignalIfAllThreadsStopped(); 843b9c1b51eSKate Stone } else { 8440e1d729bSPavel Labath // We can end up here if stop was initiated by LLGS but by this time a 8450e1d729bSPavel Labath // thread stop has occurred - maybe initiated by another event. 84697206d57SZachary Turner Status error = ResumeThread(thread, thread.GetState(), 0); 847a6321a8eSPavel Labath if (error.Fail()) 848a6321a8eSPavel Labath LLDB_LOG(log, "failed to resume thread {0}: {1}", thread.GetID(), 849a6321a8eSPavel Labath error); 8500e1d729bSPavel Labath } 851b9c1b51eSKate Stone } else { 852a6321a8eSPavel Labath LLDB_LOG(log, 853a6321a8eSPavel Labath "pid {0} tid {1}, thread was already marked as a stopped " 854a6321a8eSPavel Labath "state (state={2}), leaving stop signal as is", 8558198db30SPavel Labath GetID(), thread.GetID(), thread_state); 8560e1d729bSPavel Labath SignalIfAllThreadsStopped(); 857af245d11STodd Fiala } 858af245d11STodd Fiala 85958a2f669STodd Fiala // Done handling. 860af245d11STodd Fiala return; 861af245d11STodd Fiala } 862af245d11STodd Fiala 8634a705e7eSPavel Labath // Check if debugger should stop at this signal or just ignore it 8644a705e7eSPavel Labath // and resume the inferior. 8654a705e7eSPavel Labath if (m_signals_to_ignore.find(signo) != m_signals_to_ignore.end()) { 8664a705e7eSPavel Labath ResumeThread(thread, thread.GetState(), signo); 8674a705e7eSPavel Labath return; 8684a705e7eSPavel Labath } 8694a705e7eSPavel Labath 87086fd8e45SChaoren Lin // This thread is stopped. 871a6321a8eSPavel Labath LLDB_LOG(log, "received signal {0}", Host::GetSignalAsCString(signo)); 872b9cc0c75SPavel Labath thread.SetStoppedBySignal(signo, &info); 87386fd8e45SChaoren Lin 87486fd8e45SChaoren Lin // Send a stop to the debugger after we get all other threads to stop. 875b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 876511e5cdcSTodd Fiala } 877af245d11STodd Fiala 878e7708688STamas Berghammer namespace { 879e7708688STamas Berghammer 880b9c1b51eSKate Stone struct EmulatorBaton { 881d37349f3SPavel Labath NativeProcessLinux &m_process; 882d37349f3SPavel Labath NativeRegisterContext &m_reg_context; 8836648fcc3SPavel Labath 8846648fcc3SPavel Labath // eRegisterKindDWARF -> RegsiterValue 8856648fcc3SPavel Labath std::unordered_map<uint32_t, RegisterValue> m_register_values; 886e7708688STamas Berghammer 887d37349f3SPavel Labath EmulatorBaton(NativeProcessLinux &process, NativeRegisterContext ®_context) 888b9c1b51eSKate Stone : m_process(process), m_reg_context(reg_context) {} 889e7708688STamas Berghammer }; 890e7708688STamas Berghammer 891e7708688STamas Berghammer } // anonymous namespace 892e7708688STamas Berghammer 893b9c1b51eSKate Stone static size_t ReadMemoryCallback(EmulateInstruction *instruction, void *baton, 894e7708688STamas Berghammer const EmulateInstruction::Context &context, 895b9c1b51eSKate Stone lldb::addr_t addr, void *dst, size_t length) { 896e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 897e7708688STamas Berghammer 8983eb4b458SChaoren Lin size_t bytes_read; 899d37349f3SPavel Labath emulator_baton->m_process.ReadMemory(addr, dst, length, bytes_read); 900e7708688STamas Berghammer return bytes_read; 901e7708688STamas Berghammer } 902e7708688STamas Berghammer 903b9c1b51eSKate Stone static bool ReadRegisterCallback(EmulateInstruction *instruction, void *baton, 904e7708688STamas Berghammer const RegisterInfo *reg_info, 905b9c1b51eSKate Stone RegisterValue ®_value) { 906e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 907e7708688STamas Berghammer 908b9c1b51eSKate Stone auto it = emulator_baton->m_register_values.find( 909b9c1b51eSKate Stone reg_info->kinds[eRegisterKindDWARF]); 910b9c1b51eSKate Stone if (it != emulator_baton->m_register_values.end()) { 9116648fcc3SPavel Labath reg_value = it->second; 9126648fcc3SPavel Labath return true; 9136648fcc3SPavel Labath } 9146648fcc3SPavel Labath 915e7708688STamas Berghammer // The emulator only fill in the dwarf regsiter numbers (and in some case 916e7708688STamas Berghammer // the generic register numbers). Get the full register info from the 917e7708688STamas Berghammer // register context based on the dwarf register numbers. 918b9c1b51eSKate Stone const RegisterInfo *full_reg_info = 919d37349f3SPavel Labath emulator_baton->m_reg_context.GetRegisterInfo( 920e7708688STamas Berghammer eRegisterKindDWARF, reg_info->kinds[eRegisterKindDWARF]); 921e7708688STamas Berghammer 92297206d57SZachary Turner Status error = 923d37349f3SPavel Labath emulator_baton->m_reg_context.ReadRegister(full_reg_info, reg_value); 9246648fcc3SPavel Labath if (error.Success()) 9256648fcc3SPavel Labath return true; 926cdc22a88SMohit K. Bhakkad 9276648fcc3SPavel Labath return false; 928e7708688STamas Berghammer } 929e7708688STamas Berghammer 930b9c1b51eSKate Stone static bool WriteRegisterCallback(EmulateInstruction *instruction, void *baton, 931e7708688STamas Berghammer const EmulateInstruction::Context &context, 932e7708688STamas Berghammer const RegisterInfo *reg_info, 933b9c1b51eSKate Stone const RegisterValue ®_value) { 934e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 935b9c1b51eSKate Stone emulator_baton->m_register_values[reg_info->kinds[eRegisterKindDWARF]] = 936b9c1b51eSKate Stone reg_value; 937e7708688STamas Berghammer return true; 938e7708688STamas Berghammer } 939e7708688STamas Berghammer 940b9c1b51eSKate Stone static size_t WriteMemoryCallback(EmulateInstruction *instruction, void *baton, 941e7708688STamas Berghammer const EmulateInstruction::Context &context, 942b9c1b51eSKate Stone lldb::addr_t addr, const void *dst, 943b9c1b51eSKate Stone size_t length) { 944e7708688STamas Berghammer return length; 945e7708688STamas Berghammer } 946e7708688STamas Berghammer 947d37349f3SPavel Labath static lldb::addr_t ReadFlags(NativeRegisterContext ®siter_context) { 948d37349f3SPavel Labath const RegisterInfo *flags_info = regsiter_context.GetRegisterInfo( 949e7708688STamas Berghammer eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 950d37349f3SPavel Labath return regsiter_context.ReadRegisterAsUnsigned(flags_info, 951b9c1b51eSKate Stone LLDB_INVALID_ADDRESS); 952e7708688STamas Berghammer } 953e7708688STamas Berghammer 95497206d57SZachary Turner Status 95597206d57SZachary Turner NativeProcessLinux::SetupSoftwareSingleStepping(NativeThreadLinux &thread) { 95697206d57SZachary Turner Status error; 957d37349f3SPavel Labath NativeRegisterContext& register_context = thread.GetRegisterContext(); 958e7708688STamas Berghammer 959e7708688STamas Berghammer std::unique_ptr<EmulateInstruction> emulator_ap( 960b9c1b51eSKate Stone EmulateInstruction::FindPlugin(m_arch, eInstructionTypePCModifying, 961b9c1b51eSKate Stone nullptr)); 962e7708688STamas Berghammer 963e7708688STamas Berghammer if (emulator_ap == nullptr) 96497206d57SZachary Turner return Status("Instruction emulator not found!"); 965e7708688STamas Berghammer 966d37349f3SPavel Labath EmulatorBaton baton(*this, register_context); 967e7708688STamas Berghammer emulator_ap->SetBaton(&baton); 968e7708688STamas Berghammer emulator_ap->SetReadMemCallback(&ReadMemoryCallback); 969e7708688STamas Berghammer emulator_ap->SetReadRegCallback(&ReadRegisterCallback); 970e7708688STamas Berghammer emulator_ap->SetWriteMemCallback(&WriteMemoryCallback); 971e7708688STamas Berghammer emulator_ap->SetWriteRegCallback(&WriteRegisterCallback); 972e7708688STamas Berghammer 973e7708688STamas Berghammer if (!emulator_ap->ReadInstruction()) 97497206d57SZachary Turner return Status("Read instruction failed!"); 975e7708688STamas Berghammer 976b9c1b51eSKate Stone bool emulation_result = 977b9c1b51eSKate Stone emulator_ap->EvaluateInstruction(eEmulateInstructionOptionAutoAdvancePC); 9786648fcc3SPavel Labath 979d37349f3SPavel Labath const RegisterInfo *reg_info_pc = register_context.GetRegisterInfo( 980b9c1b51eSKate Stone eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); 981d37349f3SPavel Labath const RegisterInfo *reg_info_flags = register_context.GetRegisterInfo( 982b9c1b51eSKate Stone eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 9836648fcc3SPavel Labath 984b9c1b51eSKate Stone auto pc_it = 985b9c1b51eSKate Stone baton.m_register_values.find(reg_info_pc->kinds[eRegisterKindDWARF]); 986b9c1b51eSKate Stone auto flags_it = 987b9c1b51eSKate Stone baton.m_register_values.find(reg_info_flags->kinds[eRegisterKindDWARF]); 9886648fcc3SPavel Labath 989e7708688STamas Berghammer lldb::addr_t next_pc; 990e7708688STamas Berghammer lldb::addr_t next_flags; 991b9c1b51eSKate Stone if (emulation_result) { 992b9c1b51eSKate Stone assert(pc_it != baton.m_register_values.end() && 993b9c1b51eSKate Stone "Emulation was successfull but PC wasn't updated"); 9946648fcc3SPavel Labath next_pc = pc_it->second.GetAsUInt64(); 9956648fcc3SPavel Labath 9966648fcc3SPavel Labath if (flags_it != baton.m_register_values.end()) 9976648fcc3SPavel Labath next_flags = flags_it->second.GetAsUInt64(); 998e7708688STamas Berghammer else 999d37349f3SPavel Labath next_flags = ReadFlags(register_context); 1000b9c1b51eSKate Stone } else if (pc_it == baton.m_register_values.end()) { 1001e7708688STamas Berghammer // Emulate instruction failed and it haven't changed PC. Advance PC 1002e7708688STamas Berghammer // with the size of the current opcode because the emulation of all 1003e7708688STamas Berghammer // PC modifying instruction should be successful. The failure most 1004e7708688STamas Berghammer // likely caused by a not supported instruction which don't modify PC. 1005d37349f3SPavel Labath next_pc = register_context.GetPC() + emulator_ap->GetOpcode().GetByteSize(); 1006d37349f3SPavel Labath next_flags = ReadFlags(register_context); 1007b9c1b51eSKate Stone } else { 1008e7708688STamas Berghammer // The instruction emulation failed after it modified the PC. It is an 1009e7708688STamas Berghammer // unknown error where we can't continue because the next instruction is 1010e7708688STamas Berghammer // modifying the PC but we don't know how. 101197206d57SZachary Turner return Status("Instruction emulation failed unexpectedly."); 1012e7708688STamas Berghammer } 1013e7708688STamas Berghammer 1014b9c1b51eSKate Stone if (m_arch.GetMachine() == llvm::Triple::arm) { 1015b9c1b51eSKate Stone if (next_flags & 0x20) { 1016e7708688STamas Berghammer // Thumb mode 1017e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 2); 1018b9c1b51eSKate Stone } else { 1019e7708688STamas Berghammer // Arm mode 1020e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 4); 1021e7708688STamas Berghammer } 1022b9c1b51eSKate Stone } else if (m_arch.GetMachine() == llvm::Triple::mips64 || 1023b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64el || 1024b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips || 1025aae0a752SEugene Zemtsov m_arch.GetMachine() == llvm::Triple::mipsel || 1026aae0a752SEugene Zemtsov m_arch.GetMachine() == llvm::Triple::ppc64le) 1027cdc22a88SMohit K. Bhakkad error = SetSoftwareBreakpoint(next_pc, 4); 1028b9c1b51eSKate Stone else { 1029e7708688STamas Berghammer // No size hint is given for the next breakpoint 1030e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 0); 1031e7708688STamas Berghammer } 1032e7708688STamas Berghammer 103342eb6908SPavel Labath // If setting the breakpoint fails because next_pc is out of 103442eb6908SPavel Labath // the address space, ignore it and let the debugee segfault. 103542eb6908SPavel Labath if (error.GetError() == EIO || error.GetError() == EFAULT) { 103697206d57SZachary Turner return Status(); 103742eb6908SPavel Labath } else if (error.Fail()) 1038e7708688STamas Berghammer return error; 1039e7708688STamas Berghammer 1040b9cc0c75SPavel Labath m_threads_stepping_with_breakpoint.insert({thread.GetID(), next_pc}); 1041e7708688STamas Berghammer 104297206d57SZachary Turner return Status(); 1043e7708688STamas Berghammer } 1044e7708688STamas Berghammer 1045b9c1b51eSKate Stone bool NativeProcessLinux::SupportHardwareSingleStepping() const { 1046b9c1b51eSKate Stone if (m_arch.GetMachine() == llvm::Triple::arm || 1047b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64 || 1048b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64el || 1049b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips || 1050b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mipsel) 1051cdc22a88SMohit K. Bhakkad return false; 1052cdc22a88SMohit K. Bhakkad return true; 1053e7708688STamas Berghammer } 1054e7708688STamas Berghammer 105597206d57SZachary Turner Status NativeProcessLinux::Resume(const ResumeActionList &resume_actions) { 1056a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1057a6321a8eSPavel Labath LLDB_LOG(log, "pid {0}", GetID()); 1058af245d11STodd Fiala 1059e7708688STamas Berghammer bool software_single_step = !SupportHardwareSingleStepping(); 1060af245d11STodd Fiala 1061b9c1b51eSKate Stone if (software_single_step) { 1062a5be48b3SPavel Labath for (const auto &thread : m_threads) { 1063a5be48b3SPavel Labath assert(thread && "thread list should not contain NULL threads"); 1064e7708688STamas Berghammer 1065b9c1b51eSKate Stone const ResumeAction *const action = 1066a5be48b3SPavel Labath resume_actions.GetActionForThread(thread->GetID(), true); 1067e7708688STamas Berghammer if (action == nullptr) 1068e7708688STamas Berghammer continue; 1069e7708688STamas Berghammer 1070b9c1b51eSKate Stone if (action->state == eStateStepping) { 107197206d57SZachary Turner Status error = SetupSoftwareSingleStepping( 1072a5be48b3SPavel Labath static_cast<NativeThreadLinux &>(*thread)); 1073e7708688STamas Berghammer if (error.Fail()) 1074e7708688STamas Berghammer return error; 1075e7708688STamas Berghammer } 1076e7708688STamas Berghammer } 1077e7708688STamas Berghammer } 1078e7708688STamas Berghammer 1079a5be48b3SPavel Labath for (const auto &thread : m_threads) { 1080a5be48b3SPavel Labath assert(thread && "thread list should not contain NULL threads"); 1081af245d11STodd Fiala 1082b9c1b51eSKate Stone const ResumeAction *const action = 1083a5be48b3SPavel Labath resume_actions.GetActionForThread(thread->GetID(), true); 10846a196ce6SChaoren Lin 1085b9c1b51eSKate Stone if (action == nullptr) { 1086a6321a8eSPavel Labath LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(), 1087a5be48b3SPavel Labath thread->GetID()); 10886a196ce6SChaoren Lin continue; 10896a196ce6SChaoren Lin } 1090af245d11STodd Fiala 1091a6321a8eSPavel Labath LLDB_LOG(log, "processing resume action state {0} for pid {1} tid {2}", 1092a5be48b3SPavel Labath action->state, GetID(), thread->GetID()); 1093af245d11STodd Fiala 1094b9c1b51eSKate Stone switch (action->state) { 1095af245d11STodd Fiala case eStateRunning: 1096b9c1b51eSKate Stone case eStateStepping: { 1097af245d11STodd Fiala // Run the thread, possibly feeding it the signal. 1098fa03ad2eSChaoren Lin const int signo = action->signal; 1099a5be48b3SPavel Labath ResumeThread(static_cast<NativeThreadLinux &>(*thread), action->state, 1100b9c1b51eSKate Stone signo); 1101af245d11STodd Fiala break; 1102ae29d395SChaoren Lin } 1103af245d11STodd Fiala 1104af245d11STodd Fiala case eStateSuspended: 1105af245d11STodd Fiala case eStateStopped: 1106a6321a8eSPavel Labath llvm_unreachable("Unexpected state"); 1107af245d11STodd Fiala 1108af245d11STodd Fiala default: 110997206d57SZachary Turner return Status("NativeProcessLinux::%s (): unexpected state %s specified " 1110b9c1b51eSKate Stone "for pid %" PRIu64 ", tid %" PRIu64, 1111b9c1b51eSKate Stone __FUNCTION__, StateAsCString(action->state), GetID(), 1112a5be48b3SPavel Labath thread->GetID()); 1113af245d11STodd Fiala } 1114af245d11STodd Fiala } 1115af245d11STodd Fiala 111697206d57SZachary Turner return Status(); 1117af245d11STodd Fiala } 1118af245d11STodd Fiala 111997206d57SZachary Turner Status NativeProcessLinux::Halt() { 112097206d57SZachary Turner Status error; 1121af245d11STodd Fiala 1122af245d11STodd Fiala if (kill(GetID(), SIGSTOP) != 0) 1123af245d11STodd Fiala error.SetErrorToErrno(); 1124af245d11STodd Fiala 1125af245d11STodd Fiala return error; 1126af245d11STodd Fiala } 1127af245d11STodd Fiala 112897206d57SZachary Turner Status NativeProcessLinux::Detach() { 112997206d57SZachary Turner Status error; 1130af245d11STodd Fiala 1131af245d11STodd Fiala // Stop monitoring the inferior. 113219cbe96aSPavel Labath m_sigchld_handle.reset(); 1133af245d11STodd Fiala 11347a9495bcSPavel Labath // Tell ptrace to detach from the process. 11357a9495bcSPavel Labath if (GetID() == LLDB_INVALID_PROCESS_ID) 11367a9495bcSPavel Labath return error; 11377a9495bcSPavel Labath 1138a5be48b3SPavel Labath for (const auto &thread : m_threads) { 1139a5be48b3SPavel Labath Status e = Detach(thread->GetID()); 11407a9495bcSPavel Labath if (e.Fail()) 1141b9c1b51eSKate Stone error = 1142b9c1b51eSKate Stone e; // Save the error, but still attempt to detach from other threads. 11437a9495bcSPavel Labath } 11447a9495bcSPavel Labath 114599e37695SRavitheja Addepally m_processor_trace_monitor.clear(); 114699e37695SRavitheja Addepally m_pt_proces_trace_id = LLDB_INVALID_UID; 114799e37695SRavitheja Addepally 1148af245d11STodd Fiala return error; 1149af245d11STodd Fiala } 1150af245d11STodd Fiala 115197206d57SZachary Turner Status NativeProcessLinux::Signal(int signo) { 115297206d57SZachary Turner Status error; 1153af245d11STodd Fiala 1154a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1155a6321a8eSPavel Labath LLDB_LOG(log, "sending signal {0} ({1}) to pid {1}", signo, 1156a6321a8eSPavel Labath Host::GetSignalAsCString(signo), GetID()); 1157af245d11STodd Fiala 1158af245d11STodd Fiala if (kill(GetID(), signo)) 1159af245d11STodd Fiala error.SetErrorToErrno(); 1160af245d11STodd Fiala 1161af245d11STodd Fiala return error; 1162af245d11STodd Fiala } 1163af245d11STodd Fiala 116497206d57SZachary Turner Status NativeProcessLinux::Interrupt() { 1165e9547b80SChaoren Lin // Pick a running thread (or if none, a not-dead stopped thread) as 1166e9547b80SChaoren Lin // the chosen thread that will be the stop-reason thread. 1167a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1168e9547b80SChaoren Lin 1169a5be48b3SPavel Labath NativeThreadProtocol *running_thread = nullptr; 1170a5be48b3SPavel Labath NativeThreadProtocol *stopped_thread = nullptr; 1171e9547b80SChaoren Lin 1172a6321a8eSPavel Labath LLDB_LOG(log, "selecting running thread for interrupt target"); 1173a5be48b3SPavel Labath for (const auto &thread : m_threads) { 1174e9547b80SChaoren Lin // If we have a running or stepping thread, we'll call that the 1175e9547b80SChaoren Lin // target of the interrupt. 1176a5be48b3SPavel Labath const auto thread_state = thread->GetState(); 1177b9c1b51eSKate Stone if (thread_state == eStateRunning || thread_state == eStateStepping) { 1178a5be48b3SPavel Labath running_thread = thread.get(); 1179e9547b80SChaoren Lin break; 1180a5be48b3SPavel Labath } else if (!stopped_thread && StateIsStoppedState(thread_state, true)) { 1181b9c1b51eSKate Stone // Remember the first non-dead stopped thread. We'll use that as a backup 1182b9c1b51eSKate Stone // if there are no running threads. 1183a5be48b3SPavel Labath stopped_thread = thread.get(); 1184e9547b80SChaoren Lin } 1185e9547b80SChaoren Lin } 1186e9547b80SChaoren Lin 1187a5be48b3SPavel Labath if (!running_thread && !stopped_thread) { 118897206d57SZachary Turner Status error("found no running/stepping or live stopped threads as target " 1189b9c1b51eSKate Stone "for interrupt"); 1190a6321a8eSPavel Labath LLDB_LOG(log, "skipping due to error: {0}", error); 11915830aa75STamas Berghammer 1192e9547b80SChaoren Lin return error; 1193e9547b80SChaoren Lin } 1194e9547b80SChaoren Lin 1195a5be48b3SPavel Labath NativeThreadProtocol *deferred_signal_thread = 1196a5be48b3SPavel Labath running_thread ? running_thread : stopped_thread; 1197e9547b80SChaoren Lin 1198a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} {1} tid {2} chosen for interrupt target", GetID(), 1199a5be48b3SPavel Labath running_thread ? "running" : "stopped", 1200a5be48b3SPavel Labath deferred_signal_thread->GetID()); 1201e9547b80SChaoren Lin 1202a5be48b3SPavel Labath StopRunningThreads(deferred_signal_thread->GetID()); 120345f5cb31SPavel Labath 120497206d57SZachary Turner return Status(); 1205e9547b80SChaoren Lin } 1206e9547b80SChaoren Lin 120797206d57SZachary Turner Status NativeProcessLinux::Kill() { 1208a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1209a6321a8eSPavel Labath LLDB_LOG(log, "pid {0}", GetID()); 1210af245d11STodd Fiala 121197206d57SZachary Turner Status error; 1212af245d11STodd Fiala 1213b9c1b51eSKate Stone switch (m_state) { 1214af245d11STodd Fiala case StateType::eStateInvalid: 1215af245d11STodd Fiala case StateType::eStateExited: 1216af245d11STodd Fiala case StateType::eStateCrashed: 1217af245d11STodd Fiala case StateType::eStateDetached: 1218af245d11STodd Fiala case StateType::eStateUnloaded: 1219af245d11STodd Fiala // Nothing to do - the process is already dead. 1220a6321a8eSPavel Labath LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(), 12218198db30SPavel Labath m_state); 1222af245d11STodd Fiala return error; 1223af245d11STodd Fiala 1224af245d11STodd Fiala case StateType::eStateConnected: 1225af245d11STodd Fiala case StateType::eStateAttaching: 1226af245d11STodd Fiala case StateType::eStateLaunching: 1227af245d11STodd Fiala case StateType::eStateStopped: 1228af245d11STodd Fiala case StateType::eStateRunning: 1229af245d11STodd Fiala case StateType::eStateStepping: 1230af245d11STodd Fiala case StateType::eStateSuspended: 1231af245d11STodd Fiala // We can try to kill a process in these states. 1232af245d11STodd Fiala break; 1233af245d11STodd Fiala } 1234af245d11STodd Fiala 1235b9c1b51eSKate Stone if (kill(GetID(), SIGKILL) != 0) { 1236af245d11STodd Fiala error.SetErrorToErrno(); 1237af245d11STodd Fiala return error; 1238af245d11STodd Fiala } 1239af245d11STodd Fiala 1240af245d11STodd Fiala return error; 1241af245d11STodd Fiala } 1242af245d11STodd Fiala 124397206d57SZachary Turner static Status 124415930862SPavel Labath ParseMemoryRegionInfoFromProcMapsLine(llvm::StringRef &maps_line, 1245b9c1b51eSKate Stone MemoryRegionInfo &memory_region_info) { 1246af245d11STodd Fiala memory_region_info.Clear(); 1247af245d11STodd Fiala 124815930862SPavel Labath StringExtractor line_extractor(maps_line); 1249af245d11STodd Fiala 1250b9c1b51eSKate Stone // Format: {address_start_hex}-{address_end_hex} perms offset dev inode 1251b9c1b51eSKate Stone // pathname 1252b9c1b51eSKate Stone // perms: rwxp (letter is present if set, '-' if not, final character is 1253b9c1b51eSKate Stone // p=private, s=shared). 1254af245d11STodd Fiala 1255af245d11STodd Fiala // Parse out the starting address 1256af245d11STodd Fiala lldb::addr_t start_address = line_extractor.GetHexMaxU64(false, 0); 1257af245d11STodd Fiala 1258af245d11STodd Fiala // Parse out hyphen separating start and end address from range. 1259af245d11STodd Fiala if (!line_extractor.GetBytesLeft() || (line_extractor.GetChar() != '-')) 126097206d57SZachary Turner return Status( 1261b9c1b51eSKate Stone "malformed /proc/{pid}/maps entry, missing dash between address range"); 1262af245d11STodd Fiala 1263af245d11STodd Fiala // Parse out the ending address 1264af245d11STodd Fiala lldb::addr_t end_address = line_extractor.GetHexMaxU64(false, start_address); 1265af245d11STodd Fiala 1266af245d11STodd Fiala // Parse out the space after the address. 1267af245d11STodd Fiala if (!line_extractor.GetBytesLeft() || (line_extractor.GetChar() != ' ')) 126897206d57SZachary Turner return Status( 126997206d57SZachary Turner "malformed /proc/{pid}/maps entry, missing space after range"); 1270af245d11STodd Fiala 1271af245d11STodd Fiala // Save the range. 1272af245d11STodd Fiala memory_region_info.GetRange().SetRangeBase(start_address); 1273af245d11STodd Fiala memory_region_info.GetRange().SetRangeEnd(end_address); 1274af245d11STodd Fiala 1275b9c1b51eSKate Stone // Any memory region in /proc/{pid}/maps is by definition mapped into the 1276b9c1b51eSKate Stone // process. 1277ad007563SHoward Hellyer memory_region_info.SetMapped(MemoryRegionInfo::OptionalBool::eYes); 1278ad007563SHoward Hellyer 1279af245d11STodd Fiala // Parse out each permission entry. 1280af245d11STodd Fiala if (line_extractor.GetBytesLeft() < 4) 128197206d57SZachary Turner return Status("malformed /proc/{pid}/maps entry, missing some portion of " 1282b9c1b51eSKate Stone "permissions"); 1283af245d11STodd Fiala 1284af245d11STodd Fiala // Handle read permission. 1285af245d11STodd Fiala const char read_perm_char = line_extractor.GetChar(); 1286af245d11STodd Fiala if (read_perm_char == 'r') 1287af245d11STodd Fiala memory_region_info.SetReadable(MemoryRegionInfo::OptionalBool::eYes); 1288c73301bbSTamas Berghammer else if (read_perm_char == '-') 1289af245d11STodd Fiala memory_region_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 1290c73301bbSTamas Berghammer else 129197206d57SZachary Turner return Status("unexpected /proc/{pid}/maps read permission char"); 1292af245d11STodd Fiala 1293af245d11STodd Fiala // Handle write permission. 1294af245d11STodd Fiala const char write_perm_char = line_extractor.GetChar(); 1295af245d11STodd Fiala if (write_perm_char == 'w') 1296af245d11STodd Fiala memory_region_info.SetWritable(MemoryRegionInfo::OptionalBool::eYes); 1297c73301bbSTamas Berghammer else if (write_perm_char == '-') 1298af245d11STodd Fiala memory_region_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 1299c73301bbSTamas Berghammer else 130097206d57SZachary Turner return Status("unexpected /proc/{pid}/maps write permission char"); 1301af245d11STodd Fiala 1302af245d11STodd Fiala // Handle execute permission. 1303af245d11STodd Fiala const char exec_perm_char = line_extractor.GetChar(); 1304af245d11STodd Fiala if (exec_perm_char == 'x') 1305af245d11STodd Fiala memory_region_info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes); 1306c73301bbSTamas Berghammer else if (exec_perm_char == '-') 1307af245d11STodd Fiala memory_region_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1308c73301bbSTamas Berghammer else 130997206d57SZachary Turner return Status("unexpected /proc/{pid}/maps exec permission char"); 1310af245d11STodd Fiala 1311d7d69f80STamas Berghammer line_extractor.GetChar(); // Read the private bit 1312d7d69f80STamas Berghammer line_extractor.SkipSpaces(); // Skip the separator 1313d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the offset 1314d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the major device number 1315d7d69f80STamas Berghammer line_extractor.GetChar(); // Read the device id separator 1316d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the major device number 1317d7d69f80STamas Berghammer line_extractor.SkipSpaces(); // Skip the separator 1318d7d69f80STamas Berghammer line_extractor.GetU64(0, 10); // Read the inode number 1319d7d69f80STamas Berghammer 1320d7d69f80STamas Berghammer line_extractor.SkipSpaces(); 1321b9739d40SPavel Labath const char *name = line_extractor.Peek(); 1322b9739d40SPavel Labath if (name) 1323b9739d40SPavel Labath memory_region_info.SetName(name); 1324d7d69f80STamas Berghammer 132597206d57SZachary Turner return Status(); 1326af245d11STodd Fiala } 1327af245d11STodd Fiala 132897206d57SZachary Turner Status NativeProcessLinux::GetMemoryRegionInfo(lldb::addr_t load_addr, 1329b9c1b51eSKate Stone MemoryRegionInfo &range_info) { 1330b9c1b51eSKate Stone // FIXME review that the final memory region returned extends to the end of 1331b9c1b51eSKate Stone // the virtual address space, 1332af245d11STodd Fiala // with no perms if it is not mapped. 1333af245d11STodd Fiala 1334af245d11STodd Fiala // Use an approach that reads memory regions from /proc/{pid}/maps. 1335af245d11STodd Fiala // Assume proc maps entries are in ascending order. 1336af245d11STodd Fiala // FIXME assert if we find differently. 1337af245d11STodd Fiala 1338b9c1b51eSKate Stone if (m_supports_mem_region == LazyBool::eLazyBoolNo) { 1339af245d11STodd Fiala // We're done. 134097206d57SZachary Turner return Status("unsupported"); 1341af245d11STodd Fiala } 1342af245d11STodd Fiala 134397206d57SZachary Turner Status error = PopulateMemoryRegionCache(); 1344b9c1b51eSKate Stone if (error.Fail()) { 1345af245d11STodd Fiala return error; 1346af245d11STodd Fiala } 1347af245d11STodd Fiala 1348af245d11STodd Fiala lldb::addr_t prev_base_address = 0; 1349af245d11STodd Fiala 1350b9c1b51eSKate Stone // FIXME start by finding the last region that is <= target address using 1351b9c1b51eSKate Stone // binary search. Data is sorted. 1352af245d11STodd Fiala // There can be a ton of regions on pthreads apps with lots of threads. 1353b9c1b51eSKate Stone for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end(); 1354b9c1b51eSKate Stone ++it) { 1355a6f5795aSTamas Berghammer MemoryRegionInfo &proc_entry_info = it->first; 1356af245d11STodd Fiala 1357af245d11STodd Fiala // Sanity check assumption that /proc/{pid}/maps entries are ascending. 1358b9c1b51eSKate Stone assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && 1359b9c1b51eSKate Stone "descending /proc/pid/maps entries detected, unexpected"); 1360af245d11STodd Fiala prev_base_address = proc_entry_info.GetRange().GetRangeBase(); 1361b1554311SHafiz Abid Qadeer UNUSED_IF_ASSERT_DISABLED(prev_base_address); 1362af245d11STodd Fiala 1363b9c1b51eSKate Stone // If the target address comes before this entry, indicate distance to next 1364b9c1b51eSKate Stone // region. 1365b9c1b51eSKate Stone if (load_addr < proc_entry_info.GetRange().GetRangeBase()) { 1366af245d11STodd Fiala range_info.GetRange().SetRangeBase(load_addr); 1367b9c1b51eSKate Stone range_info.GetRange().SetByteSize( 1368b9c1b51eSKate Stone proc_entry_info.GetRange().GetRangeBase() - load_addr); 1369af245d11STodd Fiala range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 1370af245d11STodd Fiala range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 1371af245d11STodd Fiala range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1372ad007563SHoward Hellyer range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 1373af245d11STodd Fiala 1374af245d11STodd Fiala return error; 1375b9c1b51eSKate Stone } else if (proc_entry_info.GetRange().Contains(load_addr)) { 1376af245d11STodd Fiala // The target address is within the memory region we're processing here. 1377af245d11STodd Fiala range_info = proc_entry_info; 1378af245d11STodd Fiala return error; 1379af245d11STodd Fiala } 1380af245d11STodd Fiala 1381b9c1b51eSKate Stone // The target memory address comes somewhere after the region we just 1382b9c1b51eSKate Stone // parsed. 1383af245d11STodd Fiala } 1384af245d11STodd Fiala 1385b9c1b51eSKate Stone // If we made it here, we didn't find an entry that contained the given 1386b9c1b51eSKate Stone // address. Return the 1387b9c1b51eSKate Stone // load_addr as start and the amount of bytes betwwen load address and the end 1388b9c1b51eSKate Stone // of the memory as 138909839c33STamas Berghammer // size. 139009839c33STamas Berghammer range_info.GetRange().SetRangeBase(load_addr); 1391ad007563SHoward Hellyer range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); 139209839c33STamas Berghammer range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 139309839c33STamas Berghammer range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 139409839c33STamas Berghammer range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1395ad007563SHoward Hellyer range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 1396af245d11STodd Fiala return error; 1397af245d11STodd Fiala } 1398af245d11STodd Fiala 139997206d57SZachary Turner Status NativeProcessLinux::PopulateMemoryRegionCache() { 1400a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1401a6f5795aSTamas Berghammer 1402a6f5795aSTamas Berghammer // If our cache is empty, pull the latest. There should always be at least 1403a6f5795aSTamas Berghammer // one memory region if memory region handling is supported. 1404a6f5795aSTamas Berghammer if (!m_mem_region_cache.empty()) { 1405a6321a8eSPavel Labath LLDB_LOG(log, "reusing {0} cached memory region entries", 1406a6321a8eSPavel Labath m_mem_region_cache.size()); 140797206d57SZachary Turner return Status(); 1408a6f5795aSTamas Berghammer } 1409a6f5795aSTamas Berghammer 141015930862SPavel Labath auto BufferOrError = getProcFile(GetID(), "maps"); 141115930862SPavel Labath if (!BufferOrError) { 141215930862SPavel Labath m_supports_mem_region = LazyBool::eLazyBoolNo; 141315930862SPavel Labath return BufferOrError.getError(); 141415930862SPavel Labath } 141515930862SPavel Labath StringRef Rest = BufferOrError.get()->getBuffer(); 141615930862SPavel Labath while (! Rest.empty()) { 141715930862SPavel Labath StringRef Line; 141815930862SPavel Labath std::tie(Line, Rest) = Rest.split('\n'); 1419a6f5795aSTamas Berghammer MemoryRegionInfo info; 142097206d57SZachary Turner const Status parse_error = 142197206d57SZachary Turner ParseMemoryRegionInfoFromProcMapsLine(Line, info); 142215930862SPavel Labath if (parse_error.Fail()) { 142315930862SPavel Labath LLDB_LOG(log, "failed to parse proc maps line '{0}': {1}", Line, 142415930862SPavel Labath parse_error); 142515930862SPavel Labath m_supports_mem_region = LazyBool::eLazyBoolNo; 142615930862SPavel Labath return parse_error; 142715930862SPavel Labath } 1428a6f5795aSTamas Berghammer m_mem_region_cache.emplace_back( 1429a6f5795aSTamas Berghammer info, FileSpec(info.GetName().GetCString(), true)); 1430a6f5795aSTamas Berghammer } 1431a6f5795aSTamas Berghammer 143215930862SPavel Labath if (m_mem_region_cache.empty()) { 1433a6f5795aSTamas Berghammer // No entries after attempting to read them. This shouldn't happen if 1434a6f5795aSTamas Berghammer // /proc/{pid}/maps is supported. Assume we don't support map entries 1435a6f5795aSTamas Berghammer // via procfs. 143615930862SPavel Labath m_supports_mem_region = LazyBool::eLazyBoolNo; 1437a6321a8eSPavel Labath LLDB_LOG(log, 1438a6321a8eSPavel Labath "failed to find any procfs maps entries, assuming no support " 1439a6321a8eSPavel Labath "for memory region metadata retrieval"); 144097206d57SZachary Turner return Status("not supported"); 1441a6f5795aSTamas Berghammer } 1442a6f5795aSTamas Berghammer 1443a6321a8eSPavel Labath LLDB_LOG(log, "read {0} memory region entries from /proc/{1}/maps", 1444a6321a8eSPavel Labath m_mem_region_cache.size(), GetID()); 1445a6f5795aSTamas Berghammer 1446a6f5795aSTamas Berghammer // We support memory retrieval, remember that. 1447a6f5795aSTamas Berghammer m_supports_mem_region = LazyBool::eLazyBoolYes; 144897206d57SZachary Turner return Status(); 1449a6f5795aSTamas Berghammer } 1450a6f5795aSTamas Berghammer 1451b9c1b51eSKate Stone void NativeProcessLinux::DoStopIDBumped(uint32_t newBumpId) { 1452a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1453a6321a8eSPavel Labath LLDB_LOG(log, "newBumpId={0}", newBumpId); 1454a6321a8eSPavel Labath LLDB_LOG(log, "clearing {0} entries from memory region cache", 1455a6321a8eSPavel Labath m_mem_region_cache.size()); 1456af245d11STodd Fiala m_mem_region_cache.clear(); 1457af245d11STodd Fiala } 1458af245d11STodd Fiala 145997206d57SZachary Turner Status NativeProcessLinux::AllocateMemory(size_t size, uint32_t permissions, 1460b9c1b51eSKate Stone lldb::addr_t &addr) { 1461af245d11STodd Fiala // FIXME implementing this requires the equivalent of 1462af245d11STodd Fiala // InferiorCallPOSIX::InferiorCallMmap, which depends on 1463af245d11STodd Fiala // functional ThreadPlans working with Native*Protocol. 1464af245d11STodd Fiala #if 1 146597206d57SZachary Turner return Status("not implemented yet"); 1466af245d11STodd Fiala #else 1467af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 1468af245d11STodd Fiala 1469af245d11STodd Fiala unsigned prot = 0; 1470af245d11STodd Fiala if (permissions & lldb::ePermissionsReadable) 1471af245d11STodd Fiala prot |= eMmapProtRead; 1472af245d11STodd Fiala if (permissions & lldb::ePermissionsWritable) 1473af245d11STodd Fiala prot |= eMmapProtWrite; 1474af245d11STodd Fiala if (permissions & lldb::ePermissionsExecutable) 1475af245d11STodd Fiala prot |= eMmapProtExec; 1476af245d11STodd Fiala 1477af245d11STodd Fiala // TODO implement this directly in NativeProcessLinux 1478af245d11STodd Fiala // (and lift to NativeProcessPOSIX if/when that class is 1479af245d11STodd Fiala // refactored out). 1480af245d11STodd Fiala if (InferiorCallMmap(this, addr, 0, size, prot, 1481af245d11STodd Fiala eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { 1482af245d11STodd Fiala m_addr_to_mmap_size[addr] = size; 148397206d57SZachary Turner return Status(); 1484af245d11STodd Fiala } else { 1485af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 148697206d57SZachary Turner return Status("unable to allocate %" PRIu64 1487b9c1b51eSKate Stone " bytes of memory with permissions %s", 1488b9c1b51eSKate Stone size, GetPermissionsAsCString(permissions)); 1489af245d11STodd Fiala } 1490af245d11STodd Fiala #endif 1491af245d11STodd Fiala } 1492af245d11STodd Fiala 149397206d57SZachary Turner Status NativeProcessLinux::DeallocateMemory(lldb::addr_t addr) { 1494af245d11STodd Fiala // FIXME see comments in AllocateMemory - required lower-level 1495af245d11STodd Fiala // bits not in place yet (ThreadPlans) 149697206d57SZachary Turner return Status("not implemented"); 1497af245d11STodd Fiala } 1498af245d11STodd Fiala 1499b9c1b51eSKate Stone lldb::addr_t NativeProcessLinux::GetSharedLibraryInfoAddress() { 1500af245d11STodd Fiala // punt on this for now 1501af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 1502af245d11STodd Fiala } 1503af245d11STodd Fiala 1504b9c1b51eSKate Stone size_t NativeProcessLinux::UpdateThreads() { 1505af245d11STodd Fiala // The NativeProcessLinux monitoring threads are always up to date 1506af245d11STodd Fiala // with respect to thread state and they keep the thread list 1507af245d11STodd Fiala // populated properly. All this method needs to do is return the 1508af245d11STodd Fiala // thread count. 1509af245d11STodd Fiala return m_threads.size(); 1510af245d11STodd Fiala } 1511af245d11STodd Fiala 151297206d57SZachary Turner Status NativeProcessLinux::GetSoftwareBreakpointPCOffset( 1513b9c1b51eSKate Stone uint32_t &actual_opcode_size) { 1514af245d11STodd Fiala // FIXME put this behind a breakpoint protocol class that can be 1515af245d11STodd Fiala // set per architecture. Need ARM, MIPS support here. 1516af245d11STodd Fiala static const uint8_t g_i386_opcode[] = {0xCC}; 1517bb00d0b6SUlrich Weigand static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; 1518aae0a752SEugene Zemtsov static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap 1519af245d11STodd Fiala 1520b9c1b51eSKate Stone switch (m_arch.GetMachine()) { 1521af245d11STodd Fiala case llvm::Triple::x86: 1522af245d11STodd Fiala case llvm::Triple::x86_64: 1523af245d11STodd Fiala actual_opcode_size = static_cast<uint32_t>(sizeof(g_i386_opcode)); 152497206d57SZachary Turner return Status(); 1525af245d11STodd Fiala 1526bb00d0b6SUlrich Weigand case llvm::Triple::systemz: 1527bb00d0b6SUlrich Weigand actual_opcode_size = static_cast<uint32_t>(sizeof(g_s390x_opcode)); 152897206d57SZachary Turner return Status(); 1529bb00d0b6SUlrich Weigand 1530aae0a752SEugene Zemtsov case llvm::Triple::ppc64le: 1531aae0a752SEugene Zemtsov actual_opcode_size = static_cast<uint32_t>(sizeof(g_ppc64le_opcode)); 1532aae0a752SEugene Zemtsov return Status(); 1533aae0a752SEugene Zemtsov 1534ff7fd900STamas Berghammer case llvm::Triple::arm: 1535ff7fd900STamas Berghammer case llvm::Triple::aarch64: 1536e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64: 1537e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64el: 1538ce815e45SSagar Thakur case llvm::Triple::mips: 1539ce815e45SSagar Thakur case llvm::Triple::mipsel: 1540ff7fd900STamas Berghammer // On these architectures the PC don't get updated for breakpoint hits 1541c60c9452SJaydeep Patil actual_opcode_size = 0; 154297206d57SZachary Turner return Status(); 1543e8659b5dSMohit K. Bhakkad 1544af245d11STodd Fiala default: 1545af245d11STodd Fiala assert(false && "CPU type not supported!"); 154697206d57SZachary Turner return Status("CPU type not supported"); 1547af245d11STodd Fiala } 1548af245d11STodd Fiala } 1549af245d11STodd Fiala 155097206d57SZachary Turner Status NativeProcessLinux::SetBreakpoint(lldb::addr_t addr, uint32_t size, 1551b9c1b51eSKate Stone bool hardware) { 1552af245d11STodd Fiala if (hardware) 1553d5ffbad2SOmair Javaid return SetHardwareBreakpoint(addr, size); 1554af245d11STodd Fiala else 1555af245d11STodd Fiala return SetSoftwareBreakpoint(addr, size); 1556af245d11STodd Fiala } 1557af245d11STodd Fiala 155897206d57SZachary Turner Status NativeProcessLinux::RemoveBreakpoint(lldb::addr_t addr, bool hardware) { 1559d5ffbad2SOmair Javaid if (hardware) 1560d5ffbad2SOmair Javaid return RemoveHardwareBreakpoint(addr); 1561d5ffbad2SOmair Javaid else 1562d5ffbad2SOmair Javaid return NativeProcessProtocol::RemoveBreakpoint(addr); 1563d5ffbad2SOmair Javaid } 1564d5ffbad2SOmair Javaid 156597206d57SZachary Turner Status NativeProcessLinux::GetSoftwareBreakpointTrapOpcode( 1566b9c1b51eSKate Stone size_t trap_opcode_size_hint, size_t &actual_opcode_size, 1567b9c1b51eSKate Stone const uint8_t *&trap_opcode_bytes) { 156863c8be95STamas Berghammer // FIXME put this behind a breakpoint protocol class that can be set per 156963c8be95STamas Berghammer // architecture. Need MIPS support here. 15702afc5966STodd Fiala static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4}; 1571be379e15STamas Berghammer // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the 1572be379e15STamas Berghammer // linux kernel does otherwise. 1573be379e15STamas Berghammer static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7}; 1574af245d11STodd Fiala static const uint8_t g_i386_opcode[] = {0xCC}; 15753df471c3SMohit K. Bhakkad static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d}; 15762c2acf96SMohit K. Bhakkad static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00}; 1577bb00d0b6SUlrich Weigand static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; 1578be379e15STamas Berghammer static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde}; 1579aae0a752SEugene Zemtsov static const uint8_t g_ppc64le_opcode[] = {0x08, 0x00, 0xe0, 0x7f}; // trap 1580af245d11STodd Fiala 1581b9c1b51eSKate Stone switch (m_arch.GetMachine()) { 15822afc5966STodd Fiala case llvm::Triple::aarch64: 15832afc5966STodd Fiala trap_opcode_bytes = g_aarch64_opcode; 15842afc5966STodd Fiala actual_opcode_size = sizeof(g_aarch64_opcode); 158597206d57SZachary Turner return Status(); 15862afc5966STodd Fiala 158763c8be95STamas Berghammer case llvm::Triple::arm: 1588b9c1b51eSKate Stone switch (trap_opcode_size_hint) { 158963c8be95STamas Berghammer case 2: 159063c8be95STamas Berghammer trap_opcode_bytes = g_thumb_breakpoint_opcode; 159163c8be95STamas Berghammer actual_opcode_size = sizeof(g_thumb_breakpoint_opcode); 159297206d57SZachary Turner return Status(); 159363c8be95STamas Berghammer case 4: 159463c8be95STamas Berghammer trap_opcode_bytes = g_arm_breakpoint_opcode; 159563c8be95STamas Berghammer actual_opcode_size = sizeof(g_arm_breakpoint_opcode); 159697206d57SZachary Turner return Status(); 159763c8be95STamas Berghammer default: 159863c8be95STamas Berghammer assert(false && "Unrecognised trap opcode size hint!"); 159997206d57SZachary Turner return Status("Unrecognised trap opcode size hint!"); 160063c8be95STamas Berghammer } 160163c8be95STamas Berghammer 1602af245d11STodd Fiala case llvm::Triple::x86: 1603af245d11STodd Fiala case llvm::Triple::x86_64: 1604af245d11STodd Fiala trap_opcode_bytes = g_i386_opcode; 1605af245d11STodd Fiala actual_opcode_size = sizeof(g_i386_opcode); 160697206d57SZachary Turner return Status(); 1607af245d11STodd Fiala 1608ce815e45SSagar Thakur case llvm::Triple::mips: 16093df471c3SMohit K. Bhakkad case llvm::Triple::mips64: 16103df471c3SMohit K. Bhakkad trap_opcode_bytes = g_mips64_opcode; 16113df471c3SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64_opcode); 161297206d57SZachary Turner return Status(); 16133df471c3SMohit K. Bhakkad 1614ce815e45SSagar Thakur case llvm::Triple::mipsel: 16152c2acf96SMohit K. Bhakkad case llvm::Triple::mips64el: 16162c2acf96SMohit K. Bhakkad trap_opcode_bytes = g_mips64el_opcode; 16172c2acf96SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64el_opcode); 161897206d57SZachary Turner return Status(); 16192c2acf96SMohit K. Bhakkad 1620bb00d0b6SUlrich Weigand case llvm::Triple::systemz: 1621bb00d0b6SUlrich Weigand trap_opcode_bytes = g_s390x_opcode; 1622bb00d0b6SUlrich Weigand actual_opcode_size = sizeof(g_s390x_opcode); 162397206d57SZachary Turner return Status(); 1624bb00d0b6SUlrich Weigand 1625aae0a752SEugene Zemtsov case llvm::Triple::ppc64le: 1626aae0a752SEugene Zemtsov trap_opcode_bytes = g_ppc64le_opcode; 1627aae0a752SEugene Zemtsov actual_opcode_size = sizeof(g_ppc64le_opcode); 1628aae0a752SEugene Zemtsov return Status(); 1629aae0a752SEugene Zemtsov 1630af245d11STodd Fiala default: 1631af245d11STodd Fiala assert(false && "CPU type not supported!"); 163297206d57SZachary Turner return Status("CPU type not supported"); 1633af245d11STodd Fiala } 1634af245d11STodd Fiala } 1635af245d11STodd Fiala 1636af245d11STodd Fiala #if 0 1637af245d11STodd Fiala ProcessMessage::CrashReason 1638af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info) 1639af245d11STodd Fiala { 1640af245d11STodd Fiala ProcessMessage::CrashReason reason; 1641af245d11STodd Fiala assert(info->si_signo == SIGSEGV); 1642af245d11STodd Fiala 1643af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1644af245d11STodd Fiala 1645af245d11STodd Fiala switch (info->si_code) 1646af245d11STodd Fiala { 1647af245d11STodd Fiala default: 1648af245d11STodd Fiala assert(false && "unexpected si_code for SIGSEGV"); 1649af245d11STodd Fiala break; 1650af245d11STodd Fiala case SI_KERNEL: 1651af245d11STodd Fiala // Linux will occasionally send spurious SI_KERNEL codes. 1652af245d11STodd Fiala // (this is poorly documented in sigaction) 1653af245d11STodd Fiala // One way to get this is via unaligned SIMD loads. 1654af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; // for lack of anything better 1655af245d11STodd Fiala break; 1656af245d11STodd Fiala case SEGV_MAPERR: 1657af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; 1658af245d11STodd Fiala break; 1659af245d11STodd Fiala case SEGV_ACCERR: 1660af245d11STodd Fiala reason = ProcessMessage::ePrivilegedAddress; 1661af245d11STodd Fiala break; 1662af245d11STodd Fiala } 1663af245d11STodd Fiala 1664af245d11STodd Fiala return reason; 1665af245d11STodd Fiala } 1666af245d11STodd Fiala #endif 1667af245d11STodd Fiala 1668af245d11STodd Fiala #if 0 1669af245d11STodd Fiala ProcessMessage::CrashReason 1670af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info) 1671af245d11STodd Fiala { 1672af245d11STodd Fiala ProcessMessage::CrashReason reason; 1673af245d11STodd Fiala assert(info->si_signo == SIGILL); 1674af245d11STodd Fiala 1675af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1676af245d11STodd Fiala 1677af245d11STodd Fiala switch (info->si_code) 1678af245d11STodd Fiala { 1679af245d11STodd Fiala default: 1680af245d11STodd Fiala assert(false && "unexpected si_code for SIGILL"); 1681af245d11STodd Fiala break; 1682af245d11STodd Fiala case ILL_ILLOPC: 1683af245d11STodd Fiala reason = ProcessMessage::eIllegalOpcode; 1684af245d11STodd Fiala break; 1685af245d11STodd Fiala case ILL_ILLOPN: 1686af245d11STodd Fiala reason = ProcessMessage::eIllegalOperand; 1687af245d11STodd Fiala break; 1688af245d11STodd Fiala case ILL_ILLADR: 1689af245d11STodd Fiala reason = ProcessMessage::eIllegalAddressingMode; 1690af245d11STodd Fiala break; 1691af245d11STodd Fiala case ILL_ILLTRP: 1692af245d11STodd Fiala reason = ProcessMessage::eIllegalTrap; 1693af245d11STodd Fiala break; 1694af245d11STodd Fiala case ILL_PRVOPC: 1695af245d11STodd Fiala reason = ProcessMessage::ePrivilegedOpcode; 1696af245d11STodd Fiala break; 1697af245d11STodd Fiala case ILL_PRVREG: 1698af245d11STodd Fiala reason = ProcessMessage::ePrivilegedRegister; 1699af245d11STodd Fiala break; 1700af245d11STodd Fiala case ILL_COPROC: 1701af245d11STodd Fiala reason = ProcessMessage::eCoprocessorError; 1702af245d11STodd Fiala break; 1703af245d11STodd Fiala case ILL_BADSTK: 1704af245d11STodd Fiala reason = ProcessMessage::eInternalStackError; 1705af245d11STodd Fiala break; 1706af245d11STodd Fiala } 1707af245d11STodd Fiala 1708af245d11STodd Fiala return reason; 1709af245d11STodd Fiala } 1710af245d11STodd Fiala #endif 1711af245d11STodd Fiala 1712af245d11STodd Fiala #if 0 1713af245d11STodd Fiala ProcessMessage::CrashReason 1714af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info) 1715af245d11STodd Fiala { 1716af245d11STodd Fiala ProcessMessage::CrashReason reason; 1717af245d11STodd Fiala assert(info->si_signo == SIGFPE); 1718af245d11STodd Fiala 1719af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1720af245d11STodd Fiala 1721af245d11STodd Fiala switch (info->si_code) 1722af245d11STodd Fiala { 1723af245d11STodd Fiala default: 1724af245d11STodd Fiala assert(false && "unexpected si_code for SIGFPE"); 1725af245d11STodd Fiala break; 1726af245d11STodd Fiala case FPE_INTDIV: 1727af245d11STodd Fiala reason = ProcessMessage::eIntegerDivideByZero; 1728af245d11STodd Fiala break; 1729af245d11STodd Fiala case FPE_INTOVF: 1730af245d11STodd Fiala reason = ProcessMessage::eIntegerOverflow; 1731af245d11STodd Fiala break; 1732af245d11STodd Fiala case FPE_FLTDIV: 1733af245d11STodd Fiala reason = ProcessMessage::eFloatDivideByZero; 1734af245d11STodd Fiala break; 1735af245d11STodd Fiala case FPE_FLTOVF: 1736af245d11STodd Fiala reason = ProcessMessage::eFloatOverflow; 1737af245d11STodd Fiala break; 1738af245d11STodd Fiala case FPE_FLTUND: 1739af245d11STodd Fiala reason = ProcessMessage::eFloatUnderflow; 1740af245d11STodd Fiala break; 1741af245d11STodd Fiala case FPE_FLTRES: 1742af245d11STodd Fiala reason = ProcessMessage::eFloatInexactResult; 1743af245d11STodd Fiala break; 1744af245d11STodd Fiala case FPE_FLTINV: 1745af245d11STodd Fiala reason = ProcessMessage::eFloatInvalidOperation; 1746af245d11STodd Fiala break; 1747af245d11STodd Fiala case FPE_FLTSUB: 1748af245d11STodd Fiala reason = ProcessMessage::eFloatSubscriptRange; 1749af245d11STodd Fiala break; 1750af245d11STodd Fiala } 1751af245d11STodd Fiala 1752af245d11STodd Fiala return reason; 1753af245d11STodd Fiala } 1754af245d11STodd Fiala #endif 1755af245d11STodd Fiala 1756af245d11STodd Fiala #if 0 1757af245d11STodd Fiala ProcessMessage::CrashReason 1758af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info) 1759af245d11STodd Fiala { 1760af245d11STodd Fiala ProcessMessage::CrashReason reason; 1761af245d11STodd Fiala assert(info->si_signo == SIGBUS); 1762af245d11STodd Fiala 1763af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1764af245d11STodd Fiala 1765af245d11STodd Fiala switch (info->si_code) 1766af245d11STodd Fiala { 1767af245d11STodd Fiala default: 1768af245d11STodd Fiala assert(false && "unexpected si_code for SIGBUS"); 1769af245d11STodd Fiala break; 1770af245d11STodd Fiala case BUS_ADRALN: 1771af245d11STodd Fiala reason = ProcessMessage::eIllegalAlignment; 1772af245d11STodd Fiala break; 1773af245d11STodd Fiala case BUS_ADRERR: 1774af245d11STodd Fiala reason = ProcessMessage::eIllegalAddress; 1775af245d11STodd Fiala break; 1776af245d11STodd Fiala case BUS_OBJERR: 1777af245d11STodd Fiala reason = ProcessMessage::eHardwareError; 1778af245d11STodd Fiala break; 1779af245d11STodd Fiala } 1780af245d11STodd Fiala 1781af245d11STodd Fiala return reason; 1782af245d11STodd Fiala } 1783af245d11STodd Fiala #endif 1784af245d11STodd Fiala 178597206d57SZachary Turner Status NativeProcessLinux::ReadMemory(lldb::addr_t addr, void *buf, size_t size, 1786b9c1b51eSKate Stone size_t &bytes_read) { 1787df7c6995SPavel Labath if (ProcessVmReadvSupported()) { 1788b9c1b51eSKate Stone // The process_vm_readv path is about 50 times faster than ptrace api. We 1789b9c1b51eSKate Stone // want to use 1790df7c6995SPavel Labath // this syscall if it is supported. 1791df7c6995SPavel Labath 1792df7c6995SPavel Labath const ::pid_t pid = GetID(); 1793df7c6995SPavel Labath 1794df7c6995SPavel Labath struct iovec local_iov, remote_iov; 1795df7c6995SPavel Labath local_iov.iov_base = buf; 1796df7c6995SPavel Labath local_iov.iov_len = size; 1797df7c6995SPavel Labath remote_iov.iov_base = reinterpret_cast<void *>(addr); 1798df7c6995SPavel Labath remote_iov.iov_len = size; 1799df7c6995SPavel Labath 1800df7c6995SPavel Labath bytes_read = process_vm_readv(pid, &local_iov, 1, &remote_iov, 1, 0); 1801df7c6995SPavel Labath const bool success = bytes_read == size; 1802df7c6995SPavel Labath 1803a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1804a6321a8eSPavel Labath LLDB_LOG(log, 1805a6321a8eSPavel Labath "using process_vm_readv to read {0} bytes from inferior " 1806a6321a8eSPavel Labath "address {1:x}: {2}", 180710c41f37SPavel Labath size, addr, success ? "Success" : llvm::sys::StrError(errno)); 1808df7c6995SPavel Labath 1809df7c6995SPavel Labath if (success) 181097206d57SZachary Turner return Status(); 1811a6321a8eSPavel Labath // else the call failed for some reason, let's retry the read using ptrace 1812b9c1b51eSKate Stone // api. 1813df7c6995SPavel Labath } 1814df7c6995SPavel Labath 181519cbe96aSPavel Labath unsigned char *dst = static_cast<unsigned char *>(buf); 181619cbe96aSPavel Labath size_t remainder; 181719cbe96aSPavel Labath long data; 181819cbe96aSPavel Labath 1819a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 1820a6321a8eSPavel Labath LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 182119cbe96aSPavel Labath 1822b9c1b51eSKate Stone for (bytes_read = 0; bytes_read < size; bytes_read += remainder) { 182397206d57SZachary Turner Status error = NativeProcessLinux::PtraceWrapper( 1824b9c1b51eSKate Stone PTRACE_PEEKDATA, GetID(), (void *)addr, nullptr, 0, &data); 1825a6321a8eSPavel Labath if (error.Fail()) 182619cbe96aSPavel Labath return error; 182719cbe96aSPavel Labath 182819cbe96aSPavel Labath remainder = size - bytes_read; 182919cbe96aSPavel Labath remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; 183019cbe96aSPavel Labath 183119cbe96aSPavel Labath // Copy the data into our buffer 1832f6ef187bSMohit K. Bhakkad memcpy(dst, &data, remainder); 183319cbe96aSPavel Labath 1834a6321a8eSPavel Labath LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data); 183519cbe96aSPavel Labath addr += k_ptrace_word_size; 183619cbe96aSPavel Labath dst += k_ptrace_word_size; 183719cbe96aSPavel Labath } 183897206d57SZachary Turner return Status(); 1839af245d11STodd Fiala } 1840af245d11STodd Fiala 184197206d57SZachary Turner Status NativeProcessLinux::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, 1842b9c1b51eSKate Stone size_t size, 1843b9c1b51eSKate Stone size_t &bytes_read) { 184497206d57SZachary Turner Status error = ReadMemory(addr, buf, size, bytes_read); 1845b9c1b51eSKate Stone if (error.Fail()) 1846b9c1b51eSKate Stone return error; 18473eb4b458SChaoren Lin return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); 18483eb4b458SChaoren Lin } 18493eb4b458SChaoren Lin 185097206d57SZachary Turner Status NativeProcessLinux::WriteMemory(lldb::addr_t addr, const void *buf, 1851b9c1b51eSKate Stone size_t size, size_t &bytes_written) { 185219cbe96aSPavel Labath const unsigned char *src = static_cast<const unsigned char *>(buf); 185319cbe96aSPavel Labath size_t remainder; 185497206d57SZachary Turner Status error; 185519cbe96aSPavel Labath 1856a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 1857a6321a8eSPavel Labath LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 185819cbe96aSPavel Labath 1859b9c1b51eSKate Stone for (bytes_written = 0; bytes_written < size; bytes_written += remainder) { 186019cbe96aSPavel Labath remainder = size - bytes_written; 186119cbe96aSPavel Labath remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; 186219cbe96aSPavel Labath 1863b9c1b51eSKate Stone if (remainder == k_ptrace_word_size) { 186419cbe96aSPavel Labath unsigned long data = 0; 1865f6ef187bSMohit K. Bhakkad memcpy(&data, src, k_ptrace_word_size); 186619cbe96aSPavel Labath 1867a6321a8eSPavel Labath LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data); 1868b9c1b51eSKate Stone error = NativeProcessLinux::PtraceWrapper(PTRACE_POKEDATA, GetID(), 1869b9c1b51eSKate Stone (void *)addr, (void *)data); 1870a6321a8eSPavel Labath if (error.Fail()) 187119cbe96aSPavel Labath return error; 1872b9c1b51eSKate Stone } else { 187319cbe96aSPavel Labath unsigned char buff[8]; 187419cbe96aSPavel Labath size_t bytes_read; 187519cbe96aSPavel Labath error = ReadMemory(addr, buff, k_ptrace_word_size, bytes_read); 1876a6321a8eSPavel Labath if (error.Fail()) 187719cbe96aSPavel Labath return error; 187819cbe96aSPavel Labath 187919cbe96aSPavel Labath memcpy(buff, src, remainder); 188019cbe96aSPavel Labath 188119cbe96aSPavel Labath size_t bytes_written_rec; 188219cbe96aSPavel Labath error = WriteMemory(addr, buff, k_ptrace_word_size, bytes_written_rec); 1883a6321a8eSPavel Labath if (error.Fail()) 188419cbe96aSPavel Labath return error; 188519cbe96aSPavel Labath 1886a6321a8eSPavel Labath LLDB_LOG(log, "[{0:x}]:{1:x} ({2:x})", addr, *(const unsigned long *)src, 1887b9c1b51eSKate Stone *(unsigned long *)buff); 188819cbe96aSPavel Labath } 188919cbe96aSPavel Labath 189019cbe96aSPavel Labath addr += k_ptrace_word_size; 189119cbe96aSPavel Labath src += k_ptrace_word_size; 189219cbe96aSPavel Labath } 189319cbe96aSPavel Labath return error; 1894af245d11STodd Fiala } 1895af245d11STodd Fiala 189697206d57SZachary Turner Status NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) { 189719cbe96aSPavel Labath return PtraceWrapper(PTRACE_GETSIGINFO, tid, nullptr, siginfo); 1898af245d11STodd Fiala } 1899af245d11STodd Fiala 190097206d57SZachary Turner Status NativeProcessLinux::GetEventMessage(lldb::tid_t tid, 1901b9c1b51eSKate Stone unsigned long *message) { 190219cbe96aSPavel Labath return PtraceWrapper(PTRACE_GETEVENTMSG, tid, nullptr, message); 1903af245d11STodd Fiala } 1904af245d11STodd Fiala 190597206d57SZachary Turner Status NativeProcessLinux::Detach(lldb::tid_t tid) { 190697ccc294SChaoren Lin if (tid == LLDB_INVALID_THREAD_ID) 190797206d57SZachary Turner return Status(); 190897ccc294SChaoren Lin 190919cbe96aSPavel Labath return PtraceWrapper(PTRACE_DETACH, tid); 1910af245d11STodd Fiala } 1911af245d11STodd Fiala 1912b9c1b51eSKate Stone bool NativeProcessLinux::HasThreadNoLock(lldb::tid_t thread_id) { 1913a5be48b3SPavel Labath for (const auto &thread : m_threads) { 1914a5be48b3SPavel Labath assert(thread && "thread list should not contain NULL threads"); 1915a5be48b3SPavel Labath if (thread->GetID() == thread_id) { 1916af245d11STodd Fiala // We have this thread. 1917af245d11STodd Fiala return true; 1918af245d11STodd Fiala } 1919af245d11STodd Fiala } 1920af245d11STodd Fiala 1921af245d11STodd Fiala // We don't have this thread. 1922af245d11STodd Fiala return false; 1923af245d11STodd Fiala } 1924af245d11STodd Fiala 1925b9c1b51eSKate Stone bool NativeProcessLinux::StopTrackingThread(lldb::tid_t thread_id) { 1926a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 1927a6321a8eSPavel Labath LLDB_LOG(log, "tid: {0})", thread_id); 19281dbc6c9cSPavel Labath 19291dbc6c9cSPavel Labath bool found = false; 1930b9c1b51eSKate Stone for (auto it = m_threads.begin(); it != m_threads.end(); ++it) { 1931b9c1b51eSKate Stone if (*it && ((*it)->GetID() == thread_id)) { 1932af245d11STodd Fiala m_threads.erase(it); 19331dbc6c9cSPavel Labath found = true; 19341dbc6c9cSPavel Labath break; 1935af245d11STodd Fiala } 1936af245d11STodd Fiala } 1937af245d11STodd Fiala 193899e37695SRavitheja Addepally if (found) 193999e37695SRavitheja Addepally StopTracingForThread(thread_id); 19409eb1ecb9SPavel Labath SignalIfAllThreadsStopped(); 19411dbc6c9cSPavel Labath return found; 1942af245d11STodd Fiala } 1943af245d11STodd Fiala 1944a5be48b3SPavel Labath NativeThreadLinux &NativeProcessLinux::AddThread(lldb::tid_t thread_id) { 1945a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); 1946a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id); 1947af245d11STodd Fiala 1948b9c1b51eSKate Stone assert(!HasThreadNoLock(thread_id) && 1949b9c1b51eSKate Stone "attempted to add a thread by id that already exists"); 1950af245d11STodd Fiala 1951af245d11STodd Fiala // If this is the first thread, save it as the current thread 1952af245d11STodd Fiala if (m_threads.empty()) 1953af245d11STodd Fiala SetCurrentThreadID(thread_id); 1954af245d11STodd Fiala 1955a5be48b3SPavel Labath m_threads.push_back(llvm::make_unique<NativeThreadLinux>(*this, thread_id)); 195699e37695SRavitheja Addepally 195799e37695SRavitheja Addepally if (m_pt_proces_trace_id != LLDB_INVALID_UID) { 195899e37695SRavitheja Addepally auto traceMonitor = ProcessorTraceMonitor::Create( 195999e37695SRavitheja Addepally GetID(), thread_id, m_pt_process_trace_config, true); 196099e37695SRavitheja Addepally if (traceMonitor) { 196199e37695SRavitheja Addepally m_pt_traced_thread_group.insert(thread_id); 196299e37695SRavitheja Addepally m_processor_trace_monitor.insert( 196399e37695SRavitheja Addepally std::make_pair(thread_id, std::move(*traceMonitor))); 196499e37695SRavitheja Addepally } else { 196599e37695SRavitheja Addepally LLDB_LOG(log, "failed to start trace on thread {0}", thread_id); 196699e37695SRavitheja Addepally Status error(traceMonitor.takeError()); 196799e37695SRavitheja Addepally LLDB_LOG(log, "error {0}", error); 196899e37695SRavitheja Addepally } 196999e37695SRavitheja Addepally } 197099e37695SRavitheja Addepally 1971a5be48b3SPavel Labath return static_cast<NativeThreadLinux &>(*m_threads.back()); 1972af245d11STodd Fiala } 1973af245d11STodd Fiala 197497206d57SZachary Turner Status 197597206d57SZachary Turner NativeProcessLinux::FixupBreakpointPCAsNeeded(NativeThreadLinux &thread) { 1976a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS)); 1977af245d11STodd Fiala 197897206d57SZachary Turner Status error; 1979af245d11STodd Fiala 1980b9c1b51eSKate Stone // Find out the size of a breakpoint (might depend on where we are in the 1981b9c1b51eSKate Stone // code). 1982d37349f3SPavel Labath NativeRegisterContext &context = thread.GetRegisterContext(); 1983af245d11STodd Fiala 1984af245d11STodd Fiala uint32_t breakpoint_size = 0; 1985b9cc0c75SPavel Labath error = GetSoftwareBreakpointPCOffset(breakpoint_size); 1986b9c1b51eSKate Stone if (error.Fail()) { 1987a6321a8eSPavel Labath LLDB_LOG(log, "GetBreakpointSize() failed: {0}", error); 1988af245d11STodd Fiala return error; 1989a6321a8eSPavel Labath } else 1990a6321a8eSPavel Labath LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size); 1991af245d11STodd Fiala 1992b9c1b51eSKate Stone // First try probing for a breakpoint at a software breakpoint location: PC - 1993b9c1b51eSKate Stone // breakpoint size. 1994d37349f3SPavel Labath const lldb::addr_t initial_pc_addr = context.GetPCfromBreakpointLocation(); 1995af245d11STodd Fiala lldb::addr_t breakpoint_addr = initial_pc_addr; 1996b9c1b51eSKate Stone if (breakpoint_size > 0) { 1997af245d11STodd Fiala // Do not allow breakpoint probe to wrap around. 19983eb4b458SChaoren Lin if (breakpoint_addr >= breakpoint_size) 19993eb4b458SChaoren Lin breakpoint_addr -= breakpoint_size; 2000af245d11STodd Fiala } 2001af245d11STodd Fiala 2002af245d11STodd Fiala // Check if we stopped because of a breakpoint. 2003af245d11STodd Fiala NativeBreakpointSP breakpoint_sp; 2004af245d11STodd Fiala error = m_breakpoint_list.GetBreakpoint(breakpoint_addr, breakpoint_sp); 2005b9c1b51eSKate Stone if (!error.Success() || !breakpoint_sp) { 2006af245d11STodd Fiala // We didn't find one at a software probe location. Nothing to do. 2007a6321a8eSPavel Labath LLDB_LOG(log, 2008a6321a8eSPavel Labath "pid {0} no lldb breakpoint found at current pc with " 2009a6321a8eSPavel Labath "adjustment: {1}", 2010a6321a8eSPavel Labath GetID(), breakpoint_addr); 201197206d57SZachary Turner return Status(); 2012af245d11STodd Fiala } 2013af245d11STodd Fiala 2014af245d11STodd Fiala // If the breakpoint is not a software breakpoint, nothing to do. 2015b9c1b51eSKate Stone if (!breakpoint_sp->IsSoftwareBreakpoint()) { 2016a6321a8eSPavel Labath LLDB_LOG( 2017a6321a8eSPavel Labath log, 2018a6321a8eSPavel Labath "pid {0} breakpoint found at {1:x}, not software, nothing to adjust", 2019a6321a8eSPavel Labath GetID(), breakpoint_addr); 202097206d57SZachary Turner return Status(); 2021af245d11STodd Fiala } 2022af245d11STodd Fiala 2023af245d11STodd Fiala // 2024af245d11STodd Fiala // We have a software breakpoint and need to adjust the PC. 2025af245d11STodd Fiala // 2026af245d11STodd Fiala 2027af245d11STodd Fiala // Sanity check. 2028b9c1b51eSKate Stone if (breakpoint_size == 0) { 2029af245d11STodd Fiala // Nothing to do! How did we get here? 2030a6321a8eSPavel Labath LLDB_LOG(log, 2031a6321a8eSPavel Labath "pid {0} breakpoint found at {1:x}, it is software, but the " 2032a6321a8eSPavel Labath "size is zero, nothing to do (unexpected)", 2033a6321a8eSPavel Labath GetID(), breakpoint_addr); 203497206d57SZachary Turner return Status(); 2035af245d11STodd Fiala } 2036af245d11STodd Fiala 2037af245d11STodd Fiala // Change the program counter. 2038a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(), 2039a6321a8eSPavel Labath thread.GetID(), initial_pc_addr, breakpoint_addr); 2040af245d11STodd Fiala 2041d37349f3SPavel Labath error = context.SetPC(breakpoint_addr); 2042b9c1b51eSKate Stone if (error.Fail()) { 2043a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(), 2044a6321a8eSPavel Labath thread.GetID(), error); 2045af245d11STodd Fiala return error; 2046af245d11STodd Fiala } 2047af245d11STodd Fiala 2048af245d11STodd Fiala return error; 2049af245d11STodd Fiala } 2050fa03ad2eSChaoren Lin 205197206d57SZachary Turner Status NativeProcessLinux::GetLoadedModuleFileSpec(const char *module_path, 2052b9c1b51eSKate Stone FileSpec &file_spec) { 205397206d57SZachary Turner Status error = PopulateMemoryRegionCache(); 2054a6f5795aSTamas Berghammer if (error.Fail()) 2055a6f5795aSTamas Berghammer return error; 2056a6f5795aSTamas Berghammer 20577cb18bf5STamas Berghammer FileSpec module_file_spec(module_path, true); 20587cb18bf5STamas Berghammer 20597cb18bf5STamas Berghammer file_spec.Clear(); 2060a6f5795aSTamas Berghammer for (const auto &it : m_mem_region_cache) { 2061a6f5795aSTamas Berghammer if (it.second.GetFilename() == module_file_spec.GetFilename()) { 2062a6f5795aSTamas Berghammer file_spec = it.second; 206397206d57SZachary Turner return Status(); 2064a6f5795aSTamas Berghammer } 2065a6f5795aSTamas Berghammer } 206697206d57SZachary Turner return Status("Module file (%s) not found in /proc/%" PRIu64 "/maps file!", 20677cb18bf5STamas Berghammer module_file_spec.GetFilename().AsCString(), GetID()); 20687cb18bf5STamas Berghammer } 2069c076559aSPavel Labath 207097206d57SZachary Turner Status NativeProcessLinux::GetFileLoadAddress(const llvm::StringRef &file_name, 2071b9c1b51eSKate Stone lldb::addr_t &load_addr) { 2072783bfc8cSTamas Berghammer load_addr = LLDB_INVALID_ADDRESS; 207397206d57SZachary Turner Status error = PopulateMemoryRegionCache(); 2074a6f5795aSTamas Berghammer if (error.Fail()) 2075783bfc8cSTamas Berghammer return error; 2076a6f5795aSTamas Berghammer 2077a6f5795aSTamas Berghammer FileSpec file(file_name, false); 2078a6f5795aSTamas Berghammer for (const auto &it : m_mem_region_cache) { 2079a6f5795aSTamas Berghammer if (it.second == file) { 2080a6f5795aSTamas Berghammer load_addr = it.first.GetRange().GetRangeBase(); 208197206d57SZachary Turner return Status(); 2082a6f5795aSTamas Berghammer } 2083a6f5795aSTamas Berghammer } 208497206d57SZachary Turner return Status("No load address found for specified file."); 2085783bfc8cSTamas Berghammer } 2086783bfc8cSTamas Berghammer 2087a5be48b3SPavel Labath NativeThreadLinux *NativeProcessLinux::GetThreadByID(lldb::tid_t tid) { 2088a5be48b3SPavel Labath return static_cast<NativeThreadLinux *>( 2089b9c1b51eSKate Stone NativeProcessProtocol::GetThreadByID(tid)); 2090f9077782SPavel Labath } 2091f9077782SPavel Labath 209297206d57SZachary Turner Status NativeProcessLinux::ResumeThread(NativeThreadLinux &thread, 2093b9c1b51eSKate Stone lldb::StateType state, int signo) { 2094a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2095a6321a8eSPavel Labath LLDB_LOG(log, "tid: {0}", thread.GetID()); 2096c076559aSPavel Labath 2097c076559aSPavel Labath // Before we do the resume below, first check if we have a pending 2098108c325dSPavel Labath // stop notification that is currently waiting for 20990e1d729bSPavel Labath // all threads to stop. This is potentially a buggy situation since 2100c076559aSPavel Labath // we're ostensibly waiting for threads to stop before we send out the 2101c076559aSPavel Labath // pending notification, and here we are resuming one before we send 2102c076559aSPavel Labath // out the pending stop notification. 2103a6321a8eSPavel Labath if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) { 2104a6321a8eSPavel Labath LLDB_LOG(log, 2105a6321a8eSPavel Labath "about to resume tid {0} per explicit request but we have a " 2106a6321a8eSPavel Labath "pending stop notification (tid {1}) that is actively " 2107a6321a8eSPavel Labath "waiting for this thread to stop. Valid sequence of events?", 2108a6321a8eSPavel Labath thread.GetID(), m_pending_notification_tid); 2109c076559aSPavel Labath } 2110c076559aSPavel Labath 2111c076559aSPavel Labath // Request a resume. We expect this to be synchronous and the system 2112c076559aSPavel Labath // to reflect it is running after this completes. 2113b9c1b51eSKate Stone switch (state) { 2114b9c1b51eSKate Stone case eStateRunning: { 2115605b51b8SPavel Labath const auto resume_result = thread.Resume(signo); 21160e1d729bSPavel Labath if (resume_result.Success()) 21170e1d729bSPavel Labath SetState(eStateRunning, true); 21180e1d729bSPavel Labath return resume_result; 2119c076559aSPavel Labath } 2120b9c1b51eSKate Stone case eStateStepping: { 2121605b51b8SPavel Labath const auto step_result = thread.SingleStep(signo); 21220e1d729bSPavel Labath if (step_result.Success()) 21230e1d729bSPavel Labath SetState(eStateRunning, true); 21240e1d729bSPavel Labath return step_result; 21250e1d729bSPavel Labath } 21260e1d729bSPavel Labath default: 21278198db30SPavel Labath LLDB_LOG(log, "Unhandled state {0}.", state); 21280e1d729bSPavel Labath llvm_unreachable("Unhandled state for resume"); 21290e1d729bSPavel Labath } 2130c076559aSPavel Labath } 2131c076559aSPavel Labath 2132c076559aSPavel Labath //===----------------------------------------------------------------------===// 2133c076559aSPavel Labath 2134b9c1b51eSKate Stone void NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid) { 2135a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2136a6321a8eSPavel Labath LLDB_LOG(log, "about to process event: (triggering_tid: {0})", 2137a6321a8eSPavel Labath triggering_tid); 2138c076559aSPavel Labath 21390e1d729bSPavel Labath m_pending_notification_tid = triggering_tid; 21400e1d729bSPavel Labath 21410e1d729bSPavel Labath // Request a stop for all the thread stops that need to be stopped 21420e1d729bSPavel Labath // and are not already known to be stopped. 2143a5be48b3SPavel Labath for (const auto &thread : m_threads) { 2144a5be48b3SPavel Labath if (StateIsRunningState(thread->GetState())) 2145a5be48b3SPavel Labath static_cast<NativeThreadLinux *>(thread.get())->RequestStop(); 21460e1d729bSPavel Labath } 21470e1d729bSPavel Labath 21480e1d729bSPavel Labath SignalIfAllThreadsStopped(); 2149a6321a8eSPavel Labath LLDB_LOG(log, "event processing done"); 2150c076559aSPavel Labath } 2151c076559aSPavel Labath 2152b9c1b51eSKate Stone void NativeProcessLinux::SignalIfAllThreadsStopped() { 21530e1d729bSPavel Labath if (m_pending_notification_tid == LLDB_INVALID_THREAD_ID) 21540e1d729bSPavel Labath return; // No pending notification. Nothing to do. 21550e1d729bSPavel Labath 2156b9c1b51eSKate Stone for (const auto &thread_sp : m_threads) { 21570e1d729bSPavel Labath if (StateIsRunningState(thread_sp->GetState())) 21580e1d729bSPavel Labath return; // Some threads are still running. Don't signal yet. 21590e1d729bSPavel Labath } 21600e1d729bSPavel Labath 21610e1d729bSPavel Labath // We have a pending notification and all threads have stopped. 2162b9c1b51eSKate Stone Log *log( 2163b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 21649eb1ecb9SPavel Labath 2165b9c1b51eSKate Stone // Clear any temporary breakpoints we used to implement software single 2166b9c1b51eSKate Stone // stepping. 2167b9c1b51eSKate Stone for (const auto &thread_info : m_threads_stepping_with_breakpoint) { 216897206d57SZachary Turner Status error = RemoveBreakpoint(thread_info.second); 21699eb1ecb9SPavel Labath if (error.Fail()) 2170a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0} remove stepping breakpoint: {1}", 2171a6321a8eSPavel Labath thread_info.first, error); 21729eb1ecb9SPavel Labath } 21739eb1ecb9SPavel Labath m_threads_stepping_with_breakpoint.clear(); 21749eb1ecb9SPavel Labath 21759eb1ecb9SPavel Labath // Notify the delegate about the stop 21760e1d729bSPavel Labath SetCurrentThreadID(m_pending_notification_tid); 2177ed89c7feSPavel Labath SetState(StateType::eStateStopped, true); 21780e1d729bSPavel Labath m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 2179c076559aSPavel Labath } 2180c076559aSPavel Labath 2181b9c1b51eSKate Stone void NativeProcessLinux::ThreadWasCreated(NativeThreadLinux &thread) { 2182a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2183a6321a8eSPavel Labath LLDB_LOG(log, "tid: {0}", thread.GetID()); 21841dbc6c9cSPavel Labath 2185b9c1b51eSKate Stone if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID && 2186b9c1b51eSKate Stone StateIsRunningState(thread.GetState())) { 2187b9c1b51eSKate Stone // We will need to wait for this new thread to stop as well before firing 2188b9c1b51eSKate Stone // the 2189c076559aSPavel Labath // notification. 2190f9077782SPavel Labath thread.RequestStop(); 2191c076559aSPavel Labath } 2192c076559aSPavel Labath } 2193068f8a7eSTamas Berghammer 2194b9c1b51eSKate Stone void NativeProcessLinux::SigchldHandler() { 2195a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 219619cbe96aSPavel Labath // Process all pending waitpid notifications. 2197b9c1b51eSKate Stone while (true) { 219819cbe96aSPavel Labath int status = -1; 2199c1a6b128SPavel Labath ::pid_t wait_pid = llvm::sys::RetryAfterSignal(-1, ::waitpid, -1, &status, 2200c1a6b128SPavel Labath __WALL | __WNOTHREAD | WNOHANG); 220119cbe96aSPavel Labath 220219cbe96aSPavel Labath if (wait_pid == 0) 220319cbe96aSPavel Labath break; // We are done. 220419cbe96aSPavel Labath 2205b9c1b51eSKate Stone if (wait_pid == -1) { 220697206d57SZachary Turner Status error(errno, eErrorTypePOSIX); 2207a6321a8eSPavel Labath LLDB_LOG(log, "waitpid (-1, &status, _) failed: {0}", error); 220819cbe96aSPavel Labath break; 220919cbe96aSPavel Labath } 221019cbe96aSPavel Labath 22113508fc8cSPavel Labath WaitStatus wait_status = WaitStatus::Decode(status); 22123508fc8cSPavel Labath bool exited = wait_status.type == WaitStatus::Exit || 22133508fc8cSPavel Labath (wait_status.type == WaitStatus::Signal && 22143508fc8cSPavel Labath wait_pid == static_cast<::pid_t>(GetID())); 221519cbe96aSPavel Labath 22163508fc8cSPavel Labath LLDB_LOG( 22173508fc8cSPavel Labath log, 22183508fc8cSPavel Labath "waitpid (-1, &status, _) => pid = {0}, status = {1}, exited = {2}", 22193508fc8cSPavel Labath wait_pid, wait_status, exited); 222019cbe96aSPavel Labath 22213508fc8cSPavel Labath MonitorCallback(wait_pid, exited, wait_status); 222219cbe96aSPavel Labath } 2223068f8a7eSTamas Berghammer } 2224068f8a7eSTamas Berghammer 2225068f8a7eSTamas Berghammer // Wrapper for ptrace to catch errors and log calls. 2226b9c1b51eSKate Stone // Note that ptrace sets errno on error because -1 can be a valid result (i.e. 2227b9c1b51eSKate Stone // for PTRACE_PEEK*) 222897206d57SZachary Turner Status NativeProcessLinux::PtraceWrapper(int req, lldb::pid_t pid, void *addr, 2229b9c1b51eSKate Stone void *data, size_t data_size, 2230b9c1b51eSKate Stone long *result) { 223197206d57SZachary Turner Status error; 22324a9babb2SPavel Labath long int ret; 2233068f8a7eSTamas Berghammer 2234068f8a7eSTamas Berghammer Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 2235068f8a7eSTamas Berghammer 2236068f8a7eSTamas Berghammer PtraceDisplayBytes(req, data, data_size); 2237068f8a7eSTamas Berghammer 2238068f8a7eSTamas Berghammer errno = 0; 2239068f8a7eSTamas Berghammer if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) 2240b9c1b51eSKate Stone ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), 2241b9c1b51eSKate Stone *(unsigned int *)addr, data); 2242068f8a7eSTamas Berghammer else 2243b9c1b51eSKate Stone ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), 2244b9c1b51eSKate Stone addr, data); 2245068f8a7eSTamas Berghammer 22464a9babb2SPavel Labath if (ret == -1) 2247068f8a7eSTamas Berghammer error.SetErrorToErrno(); 2248068f8a7eSTamas Berghammer 22494a9babb2SPavel Labath if (result) 22504a9babb2SPavel Labath *result = ret; 22514a9babb2SPavel Labath 225228096200SPavel Labath LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3}, {4})={5:x}", req, pid, addr, data, 225328096200SPavel Labath data_size, ret); 2254068f8a7eSTamas Berghammer 2255068f8a7eSTamas Berghammer PtraceDisplayBytes(req, data, data_size); 2256068f8a7eSTamas Berghammer 2257a6321a8eSPavel Labath if (error.Fail()) 2258a6321a8eSPavel Labath LLDB_LOG(log, "ptrace() failed: {0}", error); 2259068f8a7eSTamas Berghammer 22604a9babb2SPavel Labath return error; 2261068f8a7eSTamas Berghammer } 226299e37695SRavitheja Addepally 226399e37695SRavitheja Addepally llvm::Expected<ProcessorTraceMonitor &> 226499e37695SRavitheja Addepally NativeProcessLinux::LookupProcessorTraceInstance(lldb::user_id_t traceid, 226599e37695SRavitheja Addepally lldb::tid_t thread) { 226699e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 226799e37695SRavitheja Addepally if (thread == LLDB_INVALID_THREAD_ID && traceid == m_pt_proces_trace_id) { 226899e37695SRavitheja Addepally LLDB_LOG(log, "thread not specified: {0}", traceid); 226999e37695SRavitheja Addepally return Status("tracing not active thread not specified").ToError(); 227099e37695SRavitheja Addepally } 227199e37695SRavitheja Addepally 227299e37695SRavitheja Addepally for (auto& iter : m_processor_trace_monitor) { 227399e37695SRavitheja Addepally if (traceid == iter.second->GetTraceID() && 227499e37695SRavitheja Addepally (thread == iter.first || thread == LLDB_INVALID_THREAD_ID)) 227599e37695SRavitheja Addepally return *(iter.second); 227699e37695SRavitheja Addepally } 227799e37695SRavitheja Addepally 227899e37695SRavitheja Addepally LLDB_LOG(log, "traceid not being traced: {0}", traceid); 227999e37695SRavitheja Addepally return Status("tracing not active for this thread").ToError(); 228099e37695SRavitheja Addepally } 228199e37695SRavitheja Addepally 228299e37695SRavitheja Addepally Status NativeProcessLinux::GetMetaData(lldb::user_id_t traceid, 228399e37695SRavitheja Addepally lldb::tid_t thread, 228499e37695SRavitheja Addepally llvm::MutableArrayRef<uint8_t> &buffer, 228599e37695SRavitheja Addepally size_t offset) { 228699e37695SRavitheja Addepally TraceOptions trace_options; 228799e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 228899e37695SRavitheja Addepally Status error; 228999e37695SRavitheja Addepally 229099e37695SRavitheja Addepally LLDB_LOG(log, "traceid {0}", traceid); 229199e37695SRavitheja Addepally 229299e37695SRavitheja Addepally auto perf_monitor = LookupProcessorTraceInstance(traceid, thread); 229399e37695SRavitheja Addepally if (!perf_monitor) { 229499e37695SRavitheja Addepally LLDB_LOG(log, "traceid not being traced: {0}", traceid); 229599e37695SRavitheja Addepally buffer = buffer.slice(buffer.size()); 229699e37695SRavitheja Addepally error = perf_monitor.takeError(); 229799e37695SRavitheja Addepally return error; 229899e37695SRavitheja Addepally } 229999e37695SRavitheja Addepally return (*perf_monitor).ReadPerfTraceData(buffer, offset); 230099e37695SRavitheja Addepally } 230199e37695SRavitheja Addepally 230299e37695SRavitheja Addepally Status NativeProcessLinux::GetData(lldb::user_id_t traceid, lldb::tid_t thread, 230399e37695SRavitheja Addepally llvm::MutableArrayRef<uint8_t> &buffer, 230499e37695SRavitheja Addepally size_t offset) { 230599e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 230699e37695SRavitheja Addepally Status error; 230799e37695SRavitheja Addepally 230899e37695SRavitheja Addepally LLDB_LOG(log, "traceid {0}", traceid); 230999e37695SRavitheja Addepally 231099e37695SRavitheja Addepally auto perf_monitor = LookupProcessorTraceInstance(traceid, thread); 231199e37695SRavitheja Addepally if (!perf_monitor) { 231299e37695SRavitheja Addepally LLDB_LOG(log, "traceid not being traced: {0}", traceid); 231399e37695SRavitheja Addepally buffer = buffer.slice(buffer.size()); 231499e37695SRavitheja Addepally error = perf_monitor.takeError(); 231599e37695SRavitheja Addepally return error; 231699e37695SRavitheja Addepally } 231799e37695SRavitheja Addepally return (*perf_monitor).ReadPerfTraceAux(buffer, offset); 231899e37695SRavitheja Addepally } 231999e37695SRavitheja Addepally 232099e37695SRavitheja Addepally Status NativeProcessLinux::GetTraceConfig(lldb::user_id_t traceid, 232199e37695SRavitheja Addepally TraceOptions &config) { 232299e37695SRavitheja Addepally Status error; 232399e37695SRavitheja Addepally if (config.getThreadID() == LLDB_INVALID_THREAD_ID && 232499e37695SRavitheja Addepally m_pt_proces_trace_id == traceid) { 232599e37695SRavitheja Addepally if (m_pt_proces_trace_id == LLDB_INVALID_UID) { 232699e37695SRavitheja Addepally error.SetErrorString("tracing not active for this process"); 232799e37695SRavitheja Addepally return error; 232899e37695SRavitheja Addepally } 232999e37695SRavitheja Addepally config = m_pt_process_trace_config; 233099e37695SRavitheja Addepally } else { 233199e37695SRavitheja Addepally auto perf_monitor = 233299e37695SRavitheja Addepally LookupProcessorTraceInstance(traceid, config.getThreadID()); 233399e37695SRavitheja Addepally if (!perf_monitor) { 233499e37695SRavitheja Addepally error = perf_monitor.takeError(); 233599e37695SRavitheja Addepally return error; 233699e37695SRavitheja Addepally } 233799e37695SRavitheja Addepally error = (*perf_monitor).GetTraceConfig(config); 233899e37695SRavitheja Addepally } 233999e37695SRavitheja Addepally return error; 234099e37695SRavitheja Addepally } 234199e37695SRavitheja Addepally 234299e37695SRavitheja Addepally lldb::user_id_t 234399e37695SRavitheja Addepally NativeProcessLinux::StartTraceGroup(const TraceOptions &config, 234499e37695SRavitheja Addepally Status &error) { 234599e37695SRavitheja Addepally 234699e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 234799e37695SRavitheja Addepally if (config.getType() != TraceType::eTraceTypeProcessorTrace) 234899e37695SRavitheja Addepally return LLDB_INVALID_UID; 234999e37695SRavitheja Addepally 235099e37695SRavitheja Addepally if (m_pt_proces_trace_id != LLDB_INVALID_UID) { 235199e37695SRavitheja Addepally error.SetErrorString("tracing already active on this process"); 235299e37695SRavitheja Addepally return m_pt_proces_trace_id; 235399e37695SRavitheja Addepally } 235499e37695SRavitheja Addepally 235599e37695SRavitheja Addepally for (const auto &thread_sp : m_threads) { 235699e37695SRavitheja Addepally if (auto traceInstance = ProcessorTraceMonitor::Create( 235799e37695SRavitheja Addepally GetID(), thread_sp->GetID(), config, true)) { 235899e37695SRavitheja Addepally m_pt_traced_thread_group.insert(thread_sp->GetID()); 235999e37695SRavitheja Addepally m_processor_trace_monitor.insert( 236099e37695SRavitheja Addepally std::make_pair(thread_sp->GetID(), std::move(*traceInstance))); 236199e37695SRavitheja Addepally } 236299e37695SRavitheja Addepally } 236399e37695SRavitheja Addepally 236499e37695SRavitheja Addepally m_pt_process_trace_config = config; 236599e37695SRavitheja Addepally error = ProcessorTraceMonitor::GetCPUType(m_pt_process_trace_config); 236699e37695SRavitheja Addepally 236799e37695SRavitheja Addepally // Trace on Complete process will have traceid of 0 236899e37695SRavitheja Addepally m_pt_proces_trace_id = 0; 236999e37695SRavitheja Addepally 237099e37695SRavitheja Addepally LLDB_LOG(log, "Process Trace ID {0}", m_pt_proces_trace_id); 237199e37695SRavitheja Addepally return m_pt_proces_trace_id; 237299e37695SRavitheja Addepally } 237399e37695SRavitheja Addepally 237499e37695SRavitheja Addepally lldb::user_id_t NativeProcessLinux::StartTrace(const TraceOptions &config, 237599e37695SRavitheja Addepally Status &error) { 237699e37695SRavitheja Addepally if (config.getType() != TraceType::eTraceTypeProcessorTrace) 237799e37695SRavitheja Addepally return NativeProcessProtocol::StartTrace(config, error); 237899e37695SRavitheja Addepally 237999e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 238099e37695SRavitheja Addepally 238199e37695SRavitheja Addepally lldb::tid_t threadid = config.getThreadID(); 238299e37695SRavitheja Addepally 238399e37695SRavitheja Addepally if (threadid == LLDB_INVALID_THREAD_ID) 238499e37695SRavitheja Addepally return StartTraceGroup(config, error); 238599e37695SRavitheja Addepally 238699e37695SRavitheja Addepally auto thread_sp = GetThreadByID(threadid); 238799e37695SRavitheja Addepally if (!thread_sp) { 238899e37695SRavitheja Addepally // Thread not tracked by lldb so don't trace. 238999e37695SRavitheja Addepally error.SetErrorString("invalid thread id"); 239099e37695SRavitheja Addepally return LLDB_INVALID_UID; 239199e37695SRavitheja Addepally } 239299e37695SRavitheja Addepally 239399e37695SRavitheja Addepally const auto &iter = m_processor_trace_monitor.find(threadid); 239499e37695SRavitheja Addepally if (iter != m_processor_trace_monitor.end()) { 239599e37695SRavitheja Addepally LLDB_LOG(log, "Thread already being traced"); 239699e37695SRavitheja Addepally error.SetErrorString("tracing already active on this thread"); 239799e37695SRavitheja Addepally return LLDB_INVALID_UID; 239899e37695SRavitheja Addepally } 239999e37695SRavitheja Addepally 240099e37695SRavitheja Addepally auto traceMonitor = 240199e37695SRavitheja Addepally ProcessorTraceMonitor::Create(GetID(), threadid, config, false); 240299e37695SRavitheja Addepally if (!traceMonitor) { 240399e37695SRavitheja Addepally error = traceMonitor.takeError(); 240499e37695SRavitheja Addepally LLDB_LOG(log, "error {0}", error); 240599e37695SRavitheja Addepally return LLDB_INVALID_UID; 240699e37695SRavitheja Addepally } 240799e37695SRavitheja Addepally lldb::user_id_t ret_trace_id = (*traceMonitor)->GetTraceID(); 240899e37695SRavitheja Addepally m_processor_trace_monitor.insert( 240999e37695SRavitheja Addepally std::make_pair(threadid, std::move(*traceMonitor))); 241099e37695SRavitheja Addepally return ret_trace_id; 241199e37695SRavitheja Addepally } 241299e37695SRavitheja Addepally 241399e37695SRavitheja Addepally Status NativeProcessLinux::StopTracingForThread(lldb::tid_t thread) { 241499e37695SRavitheja Addepally Status error; 241599e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 241699e37695SRavitheja Addepally LLDB_LOG(log, "Thread {0}", thread); 241799e37695SRavitheja Addepally 241899e37695SRavitheja Addepally const auto& iter = m_processor_trace_monitor.find(thread); 241999e37695SRavitheja Addepally if (iter == m_processor_trace_monitor.end()) { 242099e37695SRavitheja Addepally error.SetErrorString("tracing not active for this thread"); 242199e37695SRavitheja Addepally return error; 242299e37695SRavitheja Addepally } 242399e37695SRavitheja Addepally 242499e37695SRavitheja Addepally if (iter->second->GetTraceID() == m_pt_proces_trace_id) { 242599e37695SRavitheja Addepally // traceid maps to the whole process so we have to erase it from the 242699e37695SRavitheja Addepally // thread group. 242799e37695SRavitheja Addepally LLDB_LOG(log, "traceid maps to process"); 242899e37695SRavitheja Addepally m_pt_traced_thread_group.erase(thread); 242999e37695SRavitheja Addepally } 243099e37695SRavitheja Addepally m_processor_trace_monitor.erase(iter); 243199e37695SRavitheja Addepally 243299e37695SRavitheja Addepally return error; 243399e37695SRavitheja Addepally } 243499e37695SRavitheja Addepally 243599e37695SRavitheja Addepally Status NativeProcessLinux::StopTrace(lldb::user_id_t traceid, 243699e37695SRavitheja Addepally lldb::tid_t thread) { 243799e37695SRavitheja Addepally Status error; 243899e37695SRavitheja Addepally 243999e37695SRavitheja Addepally TraceOptions trace_options; 244099e37695SRavitheja Addepally trace_options.setThreadID(thread); 244199e37695SRavitheja Addepally error = NativeProcessLinux::GetTraceConfig(traceid, trace_options); 244299e37695SRavitheja Addepally 244399e37695SRavitheja Addepally if (error.Fail()) 244499e37695SRavitheja Addepally return error; 244599e37695SRavitheja Addepally 244699e37695SRavitheja Addepally switch (trace_options.getType()) { 244799e37695SRavitheja Addepally case lldb::TraceType::eTraceTypeProcessorTrace: 244899e37695SRavitheja Addepally if (traceid == m_pt_proces_trace_id && 244999e37695SRavitheja Addepally thread == LLDB_INVALID_THREAD_ID) 245099e37695SRavitheja Addepally StopProcessorTracingOnProcess(); 245199e37695SRavitheja Addepally else 245299e37695SRavitheja Addepally error = StopProcessorTracingOnThread(traceid, thread); 245399e37695SRavitheja Addepally break; 245499e37695SRavitheja Addepally default: 245599e37695SRavitheja Addepally error.SetErrorString("trace not supported"); 245699e37695SRavitheja Addepally break; 245799e37695SRavitheja Addepally } 245899e37695SRavitheja Addepally 245999e37695SRavitheja Addepally return error; 246099e37695SRavitheja Addepally } 246199e37695SRavitheja Addepally 246299e37695SRavitheja Addepally void NativeProcessLinux::StopProcessorTracingOnProcess() { 246399e37695SRavitheja Addepally for (auto thread_id_iter : m_pt_traced_thread_group) 246499e37695SRavitheja Addepally m_processor_trace_monitor.erase(thread_id_iter); 246599e37695SRavitheja Addepally m_pt_traced_thread_group.clear(); 246699e37695SRavitheja Addepally m_pt_proces_trace_id = LLDB_INVALID_UID; 246799e37695SRavitheja Addepally } 246899e37695SRavitheja Addepally 246999e37695SRavitheja Addepally Status NativeProcessLinux::StopProcessorTracingOnThread(lldb::user_id_t traceid, 247099e37695SRavitheja Addepally lldb::tid_t thread) { 247199e37695SRavitheja Addepally Status error; 247299e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 247399e37695SRavitheja Addepally 247499e37695SRavitheja Addepally if (thread == LLDB_INVALID_THREAD_ID) { 247599e37695SRavitheja Addepally for (auto& iter : m_processor_trace_monitor) { 247699e37695SRavitheja Addepally if (iter.second->GetTraceID() == traceid) { 247799e37695SRavitheja Addepally // Stopping a trace instance for an individual thread 247899e37695SRavitheja Addepally // hence there will only be one traceid that can match. 247999e37695SRavitheja Addepally m_processor_trace_monitor.erase(iter.first); 248099e37695SRavitheja Addepally return error; 248199e37695SRavitheja Addepally } 248299e37695SRavitheja Addepally LLDB_LOG(log, "Trace ID {0}", iter.second->GetTraceID()); 248399e37695SRavitheja Addepally } 248499e37695SRavitheja Addepally 248599e37695SRavitheja Addepally LLDB_LOG(log, "Invalid TraceID"); 248699e37695SRavitheja Addepally error.SetErrorString("invalid trace id"); 248799e37695SRavitheja Addepally return error; 248899e37695SRavitheja Addepally } 248999e37695SRavitheja Addepally 249099e37695SRavitheja Addepally // thread is specified so we can use find function on the map. 249199e37695SRavitheja Addepally const auto& iter = m_processor_trace_monitor.find(thread); 249299e37695SRavitheja Addepally if (iter == m_processor_trace_monitor.end()) { 249399e37695SRavitheja Addepally // thread not found in our map. 249499e37695SRavitheja Addepally LLDB_LOG(log, "thread not being traced"); 249599e37695SRavitheja Addepally error.SetErrorString("tracing not active for this thread"); 249699e37695SRavitheja Addepally return error; 249799e37695SRavitheja Addepally } 249899e37695SRavitheja Addepally if (iter->second->GetTraceID() != traceid) { 249999e37695SRavitheja Addepally // traceid did not match so it has to be invalid. 250099e37695SRavitheja Addepally LLDB_LOG(log, "Invalid TraceID"); 250199e37695SRavitheja Addepally error.SetErrorString("invalid trace id"); 250299e37695SRavitheja Addepally return error; 250399e37695SRavitheja Addepally } 250499e37695SRavitheja Addepally 250599e37695SRavitheja Addepally LLDB_LOG(log, "UID - {0} , Thread -{1}", traceid, thread); 250699e37695SRavitheja Addepally 250799e37695SRavitheja Addepally if (traceid == m_pt_proces_trace_id) { 250899e37695SRavitheja Addepally // traceid maps to the whole process so we have to erase it from the 250999e37695SRavitheja Addepally // thread group. 251099e37695SRavitheja Addepally LLDB_LOG(log, "traceid maps to process"); 251199e37695SRavitheja Addepally m_pt_traced_thread_group.erase(thread); 251299e37695SRavitheja Addepally } 251399e37695SRavitheja Addepally m_processor_trace_monitor.erase(iter); 251499e37695SRavitheja Addepally 251599e37695SRavitheja Addepally return error; 251699e37695SRavitheja Addepally } 2517