1af245d11STodd Fiala //===-- NativeProcessLinux.cpp -------------------------------- -*- C++ -*-===// 2af245d11STodd Fiala // 3af245d11STodd Fiala // The LLVM Compiler Infrastructure 4af245d11STodd Fiala // 5af245d11STodd Fiala // This file is distributed under the University of Illinois Open Source 6af245d11STodd Fiala // License. See LICENSE.TXT for details. 7af245d11STodd Fiala // 8af245d11STodd Fiala //===----------------------------------------------------------------------===// 9af245d11STodd Fiala 10af245d11STodd Fiala #include "NativeProcessLinux.h" 11af245d11STodd Fiala 12af245d11STodd Fiala // C Includes 13af245d11STodd Fiala #include <errno.h> 14af245d11STodd Fiala #include <stdint.h> 15b9c1b51eSKate Stone #include <string.h> 16af245d11STodd Fiala #include <unistd.h> 17af245d11STodd Fiala 18af245d11STodd Fiala // C++ Includes 19af245d11STodd Fiala #include <fstream> 20df7c6995SPavel Labath #include <mutex> 21c076559aSPavel Labath #include <sstream> 22af245d11STodd Fiala #include <string> 235b981ab9SPavel Labath #include <unordered_map> 24af245d11STodd Fiala 25af245d11STodd Fiala // Other libraries and framework includes 26d8c338d4STamas Berghammer #include "lldb/Core/EmulateInstruction.h" 276edef204SOleksiy Vyalov #include "lldb/Core/ModuleSpec.h" 28af245d11STodd Fiala #include "lldb/Core/RegisterValue.h" 29af245d11STodd Fiala #include "lldb/Core/State.h" 30af245d11STodd Fiala #include "lldb/Host/Host.h" 315ad891f7SPavel Labath #include "lldb/Host/HostProcess.h" 3224ae6294SZachary Turner #include "lldb/Host/PseudoTerminal.h" 3339de3110SZachary Turner #include "lldb/Host/ThreadLauncher.h" 342a86b555SPavel Labath #include "lldb/Host/common/NativeBreakpoint.h" 352a86b555SPavel Labath #include "lldb/Host/common/NativeRegisterContext.h" 364ee1c952SPavel Labath #include "lldb/Host/linux/Ptrace.h" 374ee1c952SPavel Labath #include "lldb/Host/linux/Uio.h" 38816ae4b0SKamil Rytarowski #include "lldb/Host/posix/ProcessLauncherPosixFork.h" 392a86b555SPavel Labath #include "lldb/Symbol/ObjectFile.h" 4090aff47cSZachary Turner #include "lldb/Target/Process.h" 41af245d11STodd Fiala #include "lldb/Target/ProcessLaunchInfo.h" 425b981ab9SPavel Labath #include "lldb/Target/Target.h" 43c16f5dcaSChaoren Lin #include "lldb/Utility/LLDBAssert.h" 4497206d57SZachary Turner #include "lldb/Utility/Status.h" 45f805e190SPavel Labath #include "lldb/Utility/StringExtractor.h" 4610c41f37SPavel Labath #include "llvm/Support/Errno.h" 4710c41f37SPavel Labath #include "llvm/Support/FileSystem.h" 4810c41f37SPavel Labath #include "llvm/Support/Threading.h" 49af245d11STodd Fiala 50af245d11STodd Fiala #include "NativeThreadLinux.h" 51b9c1b51eSKate Stone #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" 521e209fccSTamas Berghammer #include "Procfs.h" 53cacde7dfSTodd Fiala 54d858487eSTamas Berghammer #include <linux/unistd.h> 55d858487eSTamas Berghammer #include <sys/socket.h> 56df7c6995SPavel Labath #include <sys/syscall.h> 57d858487eSTamas Berghammer #include <sys/types.h> 58d858487eSTamas Berghammer #include <sys/user.h> 59d858487eSTamas Berghammer #include <sys/wait.h> 60d858487eSTamas Berghammer 61af245d11STodd Fiala // Support hardware breakpoints in case it has not been defined 62af245d11STodd Fiala #ifndef TRAP_HWBKPT 63af245d11STodd Fiala #define TRAP_HWBKPT 4 64af245d11STodd Fiala #endif 65af245d11STodd Fiala 667cb18bf5STamas Berghammer using namespace lldb; 677cb18bf5STamas Berghammer using namespace lldb_private; 68db264a6dSTamas Berghammer using namespace lldb_private::process_linux; 697cb18bf5STamas Berghammer using namespace llvm; 707cb18bf5STamas Berghammer 71af245d11STodd Fiala // Private bits we only need internally. 72df7c6995SPavel Labath 73b9c1b51eSKate Stone static bool ProcessVmReadvSupported() { 74df7c6995SPavel Labath static bool is_supported; 75c5f28e2aSKamil Rytarowski static llvm::once_flag flag; 76df7c6995SPavel Labath 77c5f28e2aSKamil Rytarowski llvm::call_once(flag, [] { 78a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 79df7c6995SPavel Labath 80df7c6995SPavel Labath uint32_t source = 0x47424742; 81df7c6995SPavel Labath uint32_t dest = 0; 82df7c6995SPavel Labath 83df7c6995SPavel Labath struct iovec local, remote; 84df7c6995SPavel Labath remote.iov_base = &source; 85df7c6995SPavel Labath local.iov_base = &dest; 86df7c6995SPavel Labath remote.iov_len = local.iov_len = sizeof source; 87df7c6995SPavel Labath 88b9c1b51eSKate Stone // We shall try if cross-process-memory reads work by attempting to read a 89b9c1b51eSKate Stone // value from our own process. 90df7c6995SPavel Labath ssize_t res = process_vm_readv(getpid(), &local, 1, &remote, 1, 0); 91df7c6995SPavel Labath is_supported = (res == sizeof(source) && source == dest); 92df7c6995SPavel Labath if (is_supported) 93a6321a8eSPavel Labath LLDB_LOG(log, 94a6321a8eSPavel Labath "Detected kernel support for process_vm_readv syscall. " 95a6321a8eSPavel Labath "Fast memory reads enabled."); 96df7c6995SPavel Labath else 97a6321a8eSPavel Labath LLDB_LOG(log, 98a6321a8eSPavel Labath "syscall process_vm_readv failed (error: {0}). Fast memory " 99a6321a8eSPavel Labath "reads disabled.", 10010c41f37SPavel Labath llvm::sys::StrError()); 101df7c6995SPavel Labath }); 102df7c6995SPavel Labath 103df7c6995SPavel Labath return is_supported; 104df7c6995SPavel Labath } 105df7c6995SPavel Labath 106b9c1b51eSKate Stone namespace { 107b9c1b51eSKate Stone void MaybeLogLaunchInfo(const ProcessLaunchInfo &info) { 108a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1094abe5d69SPavel Labath if (!log) 1104abe5d69SPavel Labath return; 1114abe5d69SPavel Labath 1124abe5d69SPavel Labath if (const FileAction *action = info.GetFileActionForFD(STDIN_FILENO)) 113a6321a8eSPavel Labath LLDB_LOG(log, "setting STDIN to '{0}'", action->GetFileSpec()); 1144abe5d69SPavel Labath else 115a6321a8eSPavel Labath LLDB_LOG(log, "leaving STDIN as is"); 1164abe5d69SPavel Labath 1174abe5d69SPavel Labath if (const FileAction *action = info.GetFileActionForFD(STDOUT_FILENO)) 118a6321a8eSPavel Labath LLDB_LOG(log, "setting STDOUT to '{0}'", action->GetFileSpec()); 1194abe5d69SPavel Labath else 120a6321a8eSPavel Labath LLDB_LOG(log, "leaving STDOUT as is"); 1214abe5d69SPavel Labath 1224abe5d69SPavel Labath if (const FileAction *action = info.GetFileActionForFD(STDERR_FILENO)) 123a6321a8eSPavel Labath LLDB_LOG(log, "setting STDERR to '{0}'", action->GetFileSpec()); 1244abe5d69SPavel Labath else 125a6321a8eSPavel Labath LLDB_LOG(log, "leaving STDERR as is"); 1264abe5d69SPavel Labath 1274abe5d69SPavel Labath int i = 0; 128b9c1b51eSKate Stone for (const char **args = info.GetArguments().GetConstArgumentVector(); *args; 129b9c1b51eSKate Stone ++args, ++i) 130a6321a8eSPavel Labath LLDB_LOG(log, "arg {0}: '{1}'", i, *args); 1314abe5d69SPavel Labath } 1324abe5d69SPavel Labath 133b9c1b51eSKate Stone void DisplayBytes(StreamString &s, void *bytes, uint32_t count) { 134af245d11STodd Fiala uint8_t *ptr = (uint8_t *)bytes; 135af245d11STodd Fiala const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count); 136b9c1b51eSKate Stone for (uint32_t i = 0; i < loop_count; i++) { 137af245d11STodd Fiala s.Printf("[%x]", *ptr); 138af245d11STodd Fiala ptr++; 139af245d11STodd Fiala } 140af245d11STodd Fiala } 141af245d11STodd Fiala 142b9c1b51eSKate Stone void PtraceDisplayBytes(int &req, void *data, size_t data_size) { 143aafe053cSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 144a6321a8eSPavel Labath if (!log) 145a6321a8eSPavel Labath return; 146af245d11STodd Fiala StreamString buf; 147af245d11STodd Fiala 148b9c1b51eSKate Stone switch (req) { 149b9c1b51eSKate Stone case PTRACE_POKETEXT: { 150af245d11STodd Fiala DisplayBytes(buf, &data, 8); 151aafe053cSPavel Labath LLDB_LOGV(log, "PTRACE_POKETEXT {0}", buf.GetData()); 152af245d11STodd Fiala break; 153af245d11STodd Fiala } 154b9c1b51eSKate Stone case PTRACE_POKEDATA: { 155af245d11STodd Fiala DisplayBytes(buf, &data, 8); 156aafe053cSPavel Labath LLDB_LOGV(log, "PTRACE_POKEDATA {0}", buf.GetData()); 157af245d11STodd Fiala break; 158af245d11STodd Fiala } 159b9c1b51eSKate Stone case PTRACE_POKEUSER: { 160af245d11STodd Fiala DisplayBytes(buf, &data, 8); 161aafe053cSPavel Labath LLDB_LOGV(log, "PTRACE_POKEUSER {0}", buf.GetData()); 162af245d11STodd Fiala break; 163af245d11STodd Fiala } 164b9c1b51eSKate Stone case PTRACE_SETREGS: { 165af245d11STodd Fiala DisplayBytes(buf, data, data_size); 166aafe053cSPavel Labath LLDB_LOGV(log, "PTRACE_SETREGS {0}", buf.GetData()); 167af245d11STodd Fiala break; 168af245d11STodd Fiala } 169b9c1b51eSKate Stone case PTRACE_SETFPREGS: { 170af245d11STodd Fiala DisplayBytes(buf, data, data_size); 171aafe053cSPavel Labath LLDB_LOGV(log, "PTRACE_SETFPREGS {0}", buf.GetData()); 172af245d11STodd Fiala break; 173af245d11STodd Fiala } 174b9c1b51eSKate Stone case PTRACE_SETSIGINFO: { 175af245d11STodd Fiala DisplayBytes(buf, data, sizeof(siginfo_t)); 176aafe053cSPavel Labath LLDB_LOGV(log, "PTRACE_SETSIGINFO {0}", buf.GetData()); 177af245d11STodd Fiala break; 178af245d11STodd Fiala } 179b9c1b51eSKate Stone case PTRACE_SETREGSET: { 180af245d11STodd Fiala // Extract iov_base from data, which is a pointer to the struct IOVEC 181af245d11STodd Fiala DisplayBytes(buf, *(void **)data, data_size); 182aafe053cSPavel Labath LLDB_LOGV(log, "PTRACE_SETREGSET {0}", buf.GetData()); 183af245d11STodd Fiala break; 184af245d11STodd Fiala } 185b9c1b51eSKate Stone default: {} 186af245d11STodd Fiala } 187af245d11STodd Fiala } 188af245d11STodd Fiala 18919cbe96aSPavel Labath static constexpr unsigned k_ptrace_word_size = sizeof(void *); 190b9c1b51eSKate Stone static_assert(sizeof(long) >= k_ptrace_word_size, 191b9c1b51eSKate Stone "Size of long must be larger than ptrace word size"); 1921107b5a5SPavel Labath } // end of anonymous namespace 1931107b5a5SPavel Labath 194bd7cbc5aSPavel Labath // Simple helper function to ensure flags are enabled on the given file 195bd7cbc5aSPavel Labath // descriptor. 19697206d57SZachary Turner static Status EnsureFDFlags(int fd, int flags) { 19797206d57SZachary Turner Status error; 198bd7cbc5aSPavel Labath 199bd7cbc5aSPavel Labath int status = fcntl(fd, F_GETFL); 200b9c1b51eSKate Stone if (status == -1) { 201bd7cbc5aSPavel Labath error.SetErrorToErrno(); 202bd7cbc5aSPavel Labath return error; 203bd7cbc5aSPavel Labath } 204bd7cbc5aSPavel Labath 205b9c1b51eSKate Stone if (fcntl(fd, F_SETFL, status | flags) == -1) { 206bd7cbc5aSPavel Labath error.SetErrorToErrno(); 207bd7cbc5aSPavel Labath return error; 208bd7cbc5aSPavel Labath } 209bd7cbc5aSPavel Labath 210bd7cbc5aSPavel Labath return error; 211bd7cbc5aSPavel Labath } 212bd7cbc5aSPavel Labath 213af245d11STodd Fiala // ----------------------------------------------------------------------------- 214af245d11STodd Fiala // Public Static Methods 215af245d11STodd Fiala // ----------------------------------------------------------------------------- 216af245d11STodd Fiala 21796e600fcSPavel Labath llvm::Expected<NativeProcessProtocolSP> 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 26296e600fcSPavel Labath std::shared_ptr<NativeProcessLinux> process_sp(new NativeProcessLinux( 26396e600fcSPavel Labath pid, launch_info.GetPTY().ReleaseMasterFileDescriptor(), native_delegate, 26496e600fcSPavel Labath arch, mainloop)); 26596e600fcSPavel Labath process_sp->InitializeThreads({pid}); 26696e600fcSPavel Labath return process_sp; 267af245d11STodd Fiala } 268af245d11STodd Fiala 26996e600fcSPavel Labath llvm::Expected<NativeProcessProtocolSP> NativeProcessLinux::Factory::Attach( 270b9c1b51eSKate Stone lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, 27196e600fcSPavel Labath MainLoop &mainloop) const { 272a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 273a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0:x}", pid); 274af245d11STodd Fiala 275af245d11STodd Fiala // Retrieve the architecture for the running process. 27696e600fcSPavel Labath ArchSpec arch; 27796e600fcSPavel Labath Status status = ResolveProcessArchitecture(pid, arch); 27896e600fcSPavel Labath if (!status.Success()) 27996e600fcSPavel Labath return status.ToError(); 280af245d11STodd Fiala 28196e600fcSPavel Labath auto tids_or = NativeProcessLinux::Attach(pid); 28296e600fcSPavel Labath if (!tids_or) 28396e600fcSPavel Labath return tids_or.takeError(); 284af245d11STodd Fiala 28596e600fcSPavel Labath std::shared_ptr<NativeProcessLinux> process_sp( 28696e600fcSPavel Labath new NativeProcessLinux(pid, -1, native_delegate, arch, mainloop)); 28796e600fcSPavel Labath process_sp->InitializeThreads(*tids_or); 28896e600fcSPavel Labath return process_sp; 289af245d11STodd Fiala } 290af245d11STodd Fiala 291af245d11STodd Fiala // ----------------------------------------------------------------------------- 292af245d11STodd Fiala // Public Instance Methods 293af245d11STodd Fiala // ----------------------------------------------------------------------------- 294af245d11STodd Fiala 29596e600fcSPavel Labath NativeProcessLinux::NativeProcessLinux(::pid_t pid, int terminal_fd, 29696e600fcSPavel Labath NativeDelegate &delegate, 29796e600fcSPavel Labath const ArchSpec &arch, MainLoop &mainloop) 29896e600fcSPavel Labath : NativeProcessProtocol(pid, terminal_fd, delegate), m_arch(arch) { 299b9c1b51eSKate Stone if (m_terminal_fd != -1) { 30096e600fcSPavel Labath Status status = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); 30196e600fcSPavel Labath assert(status.Success()); 3025ad891f7SPavel Labath } 303af245d11STodd Fiala 30496e600fcSPavel Labath Status status; 30596e600fcSPavel Labath m_sigchld_handle = mainloop.RegisterSignal( 30696e600fcSPavel Labath SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, status); 30796e600fcSPavel Labath assert(m_sigchld_handle && status.Success()); 30896e600fcSPavel Labath } 30996e600fcSPavel Labath 31096e600fcSPavel Labath void NativeProcessLinux::InitializeThreads(llvm::ArrayRef<::pid_t> tids) { 31196e600fcSPavel Labath for (const auto &tid : tids) { 31296e600fcSPavel Labath NativeThreadLinuxSP thread_sp = AddThread(tid); 313af245d11STodd Fiala assert(thread_sp && "AddThread() returned a nullptr thread"); 314f9077782SPavel Labath thread_sp->SetStoppedBySignal(SIGSTOP); 315f9077782SPavel Labath ThreadWasCreated(*thread_sp); 316af245d11STodd Fiala } 317af245d11STodd Fiala 31896e600fcSPavel Labath // Let our process instance know the thread has stopped. 31996e600fcSPavel Labath SetCurrentThreadID(tids[0]); 32096e600fcSPavel Labath SetState(StateType::eStateStopped, false); 32196e600fcSPavel Labath 32296e600fcSPavel Labath // Proccess any signals we received before installing our handler 32396e600fcSPavel Labath SigchldHandler(); 32496e600fcSPavel Labath } 32596e600fcSPavel Labath 32696e600fcSPavel Labath llvm::Expected<std::vector<::pid_t>> NativeProcessLinux::Attach(::pid_t pid) { 327a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 328af245d11STodd Fiala 32996e600fcSPavel Labath Status status; 330b9c1b51eSKate Stone // Use a map to keep track of the threads which we have attached/need to 331b9c1b51eSKate Stone // attach. 332af245d11STodd Fiala Host::TidMap tids_to_attach; 333b9c1b51eSKate Stone while (Host::FindProcessThreads(pid, tids_to_attach)) { 334af245d11STodd Fiala for (Host::TidMap::iterator it = tids_to_attach.begin(); 335b9c1b51eSKate Stone it != tids_to_attach.end();) { 336b9c1b51eSKate Stone if (it->second == false) { 337af245d11STodd Fiala lldb::tid_t tid = it->first; 338af245d11STodd Fiala 339af245d11STodd Fiala // Attach to the requested process. 340af245d11STodd Fiala // An attach will cause the thread to stop with a SIGSTOP. 34196e600fcSPavel Labath if ((status = PtraceWrapper(PTRACE_ATTACH, tid)).Fail()) { 342af245d11STodd Fiala // No such thread. The thread may have exited. 343af245d11STodd Fiala // More error handling may be needed. 34496e600fcSPavel Labath if (status.GetError() == ESRCH) { 345af245d11STodd Fiala it = tids_to_attach.erase(it); 346af245d11STodd Fiala continue; 34796e600fcSPavel Labath } 34896e600fcSPavel Labath return status.ToError(); 349af245d11STodd Fiala } 350af245d11STodd Fiala 35196e600fcSPavel Labath int wpid = 35296e600fcSPavel Labath llvm::sys::RetryAfterSignal(-1, ::waitpid, tid, nullptr, __WALL); 353af245d11STodd Fiala // Need to use __WALL otherwise we receive an error with errno=ECHLD 354af245d11STodd Fiala // At this point we should have a thread stopped if waitpid succeeds. 35596e600fcSPavel Labath if (wpid < 0) { 356af245d11STodd Fiala // No such thread. The thread may have exited. 357af245d11STodd Fiala // More error handling may be needed. 358b9c1b51eSKate Stone if (errno == ESRCH) { 359af245d11STodd Fiala it = tids_to_attach.erase(it); 360af245d11STodd Fiala continue; 361af245d11STodd Fiala } 36296e600fcSPavel Labath return llvm::errorCodeToError( 36396e600fcSPavel Labath std::error_code(errno, std::generic_category())); 364af245d11STodd Fiala } 365af245d11STodd Fiala 36696e600fcSPavel Labath if ((status = SetDefaultPtraceOpts(tid)).Fail()) 36796e600fcSPavel Labath return status.ToError(); 368af245d11STodd Fiala 369a6321a8eSPavel Labath LLDB_LOG(log, "adding tid = {0}", tid); 370af245d11STodd Fiala it->second = true; 371af245d11STodd Fiala } 372af245d11STodd Fiala 373af245d11STodd Fiala // move the loop forward 374af245d11STodd Fiala ++it; 375af245d11STodd Fiala } 376af245d11STodd Fiala } 377af245d11STodd Fiala 37896e600fcSPavel Labath size_t tid_count = tids_to_attach.size(); 37996e600fcSPavel Labath if (tid_count == 0) 38096e600fcSPavel Labath return llvm::make_error<StringError>("No such process", 38196e600fcSPavel Labath llvm::inconvertibleErrorCode()); 382af245d11STodd Fiala 38396e600fcSPavel Labath std::vector<::pid_t> tids; 38496e600fcSPavel Labath tids.reserve(tid_count); 38596e600fcSPavel Labath for (const auto &p : tids_to_attach) 38696e600fcSPavel Labath tids.push_back(p.first); 38796e600fcSPavel Labath return std::move(tids); 388af245d11STodd Fiala } 389af245d11STodd Fiala 39097206d57SZachary Turner Status NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) { 391af245d11STodd Fiala long ptrace_opts = 0; 392af245d11STodd Fiala 393af245d11STodd Fiala // Have the child raise an event on exit. This is used to keep the child in 394af245d11STodd Fiala // limbo until it is destroyed. 395af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXIT; 396af245d11STodd Fiala 397af245d11STodd Fiala // Have the tracer trace threads which spawn in the inferior process. 398af245d11STodd Fiala // TODO: if we want to support tracing the inferiors' child, add the 399af245d11STodd Fiala // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK) 400af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACECLONE; 401af245d11STodd Fiala 402af245d11STodd Fiala // Have the tracer notify us before execve returns 403af245d11STodd Fiala // (needed to disable legacy SIGTRAP generation) 404af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXEC; 405af245d11STodd Fiala 4064a9babb2SPavel Labath return PtraceWrapper(PTRACE_SETOPTIONS, pid, nullptr, (void *)ptrace_opts); 407af245d11STodd Fiala } 408af245d11STodd Fiala 4091107b5a5SPavel Labath // Handles all waitpid events from the inferior process. 410b9c1b51eSKate Stone void NativeProcessLinux::MonitorCallback(lldb::pid_t pid, bool exited, 4113508fc8cSPavel Labath WaitStatus status) { 412af245d11STodd Fiala Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 413af245d11STodd Fiala 414b9c1b51eSKate Stone // Certain activities differ based on whether the pid is the tid of the main 415b9c1b51eSKate Stone // thread. 4161107b5a5SPavel Labath const bool is_main_thread = (pid == GetID()); 417af245d11STodd Fiala 418af245d11STodd Fiala // Handle when the thread exits. 419b9c1b51eSKate Stone if (exited) { 420a6321a8eSPavel Labath LLDB_LOG(log, "got exit signal({0}) , tid = {1} ({2} main thread)", signal, 421a6321a8eSPavel Labath pid, is_main_thread ? "is" : "is not"); 422af245d11STodd Fiala 423af245d11STodd Fiala // This is a thread that exited. Ensure we're not tracking it anymore. 4241107b5a5SPavel Labath const bool thread_found = StopTrackingThread(pid); 425af245d11STodd Fiala 426b9c1b51eSKate Stone if (is_main_thread) { 427b9c1b51eSKate Stone // We only set the exit status and notify the delegate if we haven't 428b9c1b51eSKate Stone // already set the process 429b9c1b51eSKate Stone // state to an exited state. We normally should have received a SIGTRAP | 430b9c1b51eSKate Stone // (PTRACE_EVENT_EXIT << 8) 431af245d11STodd Fiala // for the main thread. 432b9c1b51eSKate Stone const bool already_notified = (GetState() == StateType::eStateExited) || 433b9c1b51eSKate Stone (GetState() == StateType::eStateCrashed); 434b9c1b51eSKate Stone if (!already_notified) { 435a6321a8eSPavel Labath LLDB_LOG( 436a6321a8eSPavel Labath log, 437a6321a8eSPavel Labath "tid = {0} handling main thread exit ({1}), expected exit state " 438a6321a8eSPavel Labath "already set but state was {2} instead, setting exit state now", 439a6321a8eSPavel Labath pid, 440b9c1b51eSKate Stone thread_found ? "stopped tracking thread metadata" 441b9c1b51eSKate Stone : "thread metadata not found", 4428198db30SPavel Labath GetState()); 443af245d11STodd Fiala // The main thread exited. We're done monitoring. Report to delegate. 4443508fc8cSPavel Labath SetExitStatus(status, true); 445af245d11STodd Fiala 446af245d11STodd Fiala // Notify delegate that our process has exited. 4471107b5a5SPavel Labath SetState(StateType::eStateExited, true); 448a6321a8eSPavel Labath } else 449a6321a8eSPavel Labath LLDB_LOG(log, "tid = {0} main thread now exited (%s)", pid, 450b9c1b51eSKate Stone thread_found ? "stopped tracking thread metadata" 451b9c1b51eSKate Stone : "thread metadata not found"); 452b9c1b51eSKate Stone } else { 453b9c1b51eSKate Stone // Do we want to report to the delegate in this case? I think not. If 454a6321a8eSPavel Labath // this was an orderly thread exit, we would already have received the 455a6321a8eSPavel Labath // SIGTRAP | (PTRACE_EVENT_EXIT << 8) signal, and we would have done an 456a6321a8eSPavel Labath // all-stop then. 457a6321a8eSPavel Labath LLDB_LOG(log, "tid = {0} handling non-main thread exit (%s)", pid, 458b9c1b51eSKate Stone thread_found ? "stopped tracking thread metadata" 459b9c1b51eSKate Stone : "thread metadata not found"); 460af245d11STodd Fiala } 4611107b5a5SPavel Labath return; 462af245d11STodd Fiala } 463af245d11STodd Fiala 464af245d11STodd Fiala siginfo_t info; 465b9cc0c75SPavel Labath const auto info_err = GetSignalInfo(pid, &info); 466b9cc0c75SPavel Labath auto thread_sp = GetThreadByID(pid); 467b9cc0c75SPavel Labath 468b9c1b51eSKate Stone if (!thread_sp) { 469b9c1b51eSKate Stone // Normally, the only situation when we cannot find the thread is if we have 470a6321a8eSPavel Labath // just received a new thread notification. This is indicated by 471a6321a8eSPavel Labath // GetSignalInfo() returning si_code == SI_USER and si_pid == 0 472a6321a8eSPavel Labath LLDB_LOG(log, "received notification about an unknown tid {0}.", pid); 473b9cc0c75SPavel Labath 474b9c1b51eSKate Stone if (info_err.Fail()) { 475a6321a8eSPavel Labath LLDB_LOG(log, 476a6321a8eSPavel Labath "(tid {0}) GetSignalInfo failed ({1}). " 477a6321a8eSPavel Labath "Ingoring this notification.", 478a6321a8eSPavel Labath pid, info_err); 479b9cc0c75SPavel Labath return; 480b9cc0c75SPavel Labath } 481b9cc0c75SPavel Labath 482a6321a8eSPavel Labath LLDB_LOG(log, "tid {0}, si_code: {1}, si_pid: {2}", pid, info.si_code, 483a6321a8eSPavel Labath info.si_pid); 484b9cc0c75SPavel Labath 485b9cc0c75SPavel Labath auto thread_sp = AddThread(pid); 48699e37695SRavitheja Addepally 487b9cc0c75SPavel Labath // Resume the newly created thread. 488b9cc0c75SPavel Labath ResumeThread(*thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); 489b9cc0c75SPavel Labath ThreadWasCreated(*thread_sp); 490b9cc0c75SPavel Labath return; 491b9cc0c75SPavel Labath } 492b9cc0c75SPavel Labath 493b9cc0c75SPavel Labath // Get details on the signal raised. 494b9c1b51eSKate Stone if (info_err.Success()) { 495fa03ad2eSChaoren Lin // We have retrieved the signal info. Dispatch appropriately. 496fa03ad2eSChaoren Lin if (info.si_signo == SIGTRAP) 497b9cc0c75SPavel Labath MonitorSIGTRAP(info, *thread_sp); 498fa03ad2eSChaoren Lin else 499b9cc0c75SPavel Labath MonitorSignal(info, *thread_sp, exited); 500b9c1b51eSKate Stone } else { 501b9c1b51eSKate Stone if (info_err.GetError() == EINVAL) { 502fa03ad2eSChaoren Lin // This is a group stop reception for this tid. 503b9c1b51eSKate Stone // We can reach here if we reinject SIGSTOP, SIGSTP, SIGTTIN or SIGTTOU 504a6321a8eSPavel Labath // into the tracee, triggering the group-stop mechanism. Normally 505a6321a8eSPavel Labath // receiving these would stop the process, pending a SIGCONT. Simulating 506a6321a8eSPavel Labath // this state in a debugger is hard and is generally not needed (one use 507a6321a8eSPavel Labath // case is debugging background task being managed by a shell). For 508a6321a8eSPavel Labath // general use, it is sufficient to stop the process in a signal-delivery 509b9c1b51eSKate Stone // stop which happens before the group stop. This done by MonitorSignal 510a6321a8eSPavel Labath // and works correctly for all signals. 511a6321a8eSPavel Labath LLDB_LOG(log, 512a6321a8eSPavel Labath "received a group stop for pid {0} tid {1}. Transparent " 513a6321a8eSPavel Labath "handling of group stops not supported, resuming the " 514a6321a8eSPavel Labath "thread.", 515a6321a8eSPavel Labath GetID(), pid); 516b9c1b51eSKate Stone ResumeThread(*thread_sp, thread_sp->GetState(), 517b9c1b51eSKate Stone LLDB_INVALID_SIGNAL_NUMBER); 518b9c1b51eSKate Stone } else { 519af245d11STodd Fiala // ptrace(GETSIGINFO) failed (but not due to group-stop). 520af245d11STodd Fiala 521b9c1b51eSKate Stone // A return value of ESRCH means the thread/process is no longer on the 522a6321a8eSPavel Labath // system, so it was killed somehow outside of our control. Either way, 523a6321a8eSPavel Labath // we can't do anything with it anymore. 524af245d11STodd Fiala 525b9c1b51eSKate Stone // Stop tracking the metadata for the thread since it's entirely off the 526b9c1b51eSKate Stone // system now. 5271107b5a5SPavel Labath const bool thread_found = StopTrackingThread(pid); 528af245d11STodd Fiala 529a6321a8eSPavel Labath LLDB_LOG(log, 530a6321a8eSPavel Labath "GetSignalInfo failed: {0}, tid = {1}, signal = {2}, " 531a6321a8eSPavel Labath "status = {3}, main_thread = {4}, thread_found: {5}", 532a6321a8eSPavel Labath info_err, pid, signal, status, is_main_thread, thread_found); 533af245d11STodd Fiala 534b9c1b51eSKate Stone if (is_main_thread) { 535b9c1b51eSKate Stone // Notify the delegate - our process is not available but appears to 536b9c1b51eSKate Stone // have been killed outside 537af245d11STodd Fiala // our control. Is eStateExited the right exit state in this case? 5383508fc8cSPavel Labath SetExitStatus(status, true); 5391107b5a5SPavel Labath SetState(StateType::eStateExited, true); 540b9c1b51eSKate Stone } else { 541b9c1b51eSKate Stone // This thread was pulled out from underneath us. Anything to do here? 542b9c1b51eSKate Stone // Do we want to do an all stop? 543a6321a8eSPavel Labath LLDB_LOG(log, 544a6321a8eSPavel Labath "pid {0} tid {1} non-main thread exit occurred, didn't " 545a6321a8eSPavel Labath "tell delegate anything since thread disappeared out " 546a6321a8eSPavel Labath "from underneath us", 547a6321a8eSPavel Labath GetID(), pid); 548af245d11STodd Fiala } 549af245d11STodd Fiala } 550af245d11STodd Fiala } 551af245d11STodd Fiala } 552af245d11STodd Fiala 553b9c1b51eSKate Stone void NativeProcessLinux::WaitForNewThread(::pid_t tid) { 554a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 555426bdf88SPavel Labath 556f9077782SPavel Labath NativeThreadLinuxSP new_thread_sp = GetThreadByID(tid); 557426bdf88SPavel Labath 558b9c1b51eSKate Stone if (new_thread_sp) { 559b9c1b51eSKate Stone // We are already tracking the thread - we got the event on the new thread 560b9c1b51eSKate Stone // (see 561426bdf88SPavel Labath // MonitorSignal) before this one. We are done. 562426bdf88SPavel Labath return; 563426bdf88SPavel Labath } 564426bdf88SPavel Labath 565426bdf88SPavel Labath // The thread is not tracked yet, let's wait for it to appear. 566426bdf88SPavel Labath int status = -1; 567a6321a8eSPavel Labath LLDB_LOG(log, 568a6321a8eSPavel Labath "received thread creation event for tid {0}. tid not tracked " 569a6321a8eSPavel Labath "yet, waiting for thread to appear...", 570a6321a8eSPavel Labath tid); 571c1a6b128SPavel Labath ::pid_t wait_pid = llvm::sys::RetryAfterSignal(-1, ::waitpid, tid, &status, __WALL); 572b9c1b51eSKate Stone // Since we are waiting on a specific tid, this must be the creation event. 573a6321a8eSPavel Labath // But let's do some checks just in case. 574426bdf88SPavel Labath if (wait_pid != tid) { 575a6321a8eSPavel Labath LLDB_LOG(log, 576a6321a8eSPavel Labath "waiting for tid {0} failed. Assuming the thread has " 577a6321a8eSPavel Labath "disappeared in the meantime", 578a6321a8eSPavel Labath tid); 579426bdf88SPavel Labath // The only way I know of this could happen is if the whole process was 580b9c1b51eSKate Stone // SIGKILLed in the mean time. In any case, we can't do anything about that 581b9c1b51eSKate Stone // now. 582426bdf88SPavel Labath return; 583426bdf88SPavel Labath } 584b9c1b51eSKate Stone if (WIFEXITED(status)) { 585a6321a8eSPavel Labath LLDB_LOG(log, 586a6321a8eSPavel Labath "waiting for tid {0} returned an 'exited' event. Not " 587a6321a8eSPavel Labath "tracking the thread.", 588a6321a8eSPavel Labath tid); 589426bdf88SPavel Labath // Also a very improbable event. 590426bdf88SPavel Labath return; 591426bdf88SPavel Labath } 592426bdf88SPavel Labath 593a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0}: tracking new thread tid {1}", GetID(), tid); 594f9077782SPavel Labath new_thread_sp = AddThread(tid); 59599e37695SRavitheja Addepally 596b9cc0c75SPavel Labath ResumeThread(*new_thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); 597f9077782SPavel Labath ThreadWasCreated(*new_thread_sp); 598426bdf88SPavel Labath } 599426bdf88SPavel Labath 600b9c1b51eSKate Stone void NativeProcessLinux::MonitorSIGTRAP(const siginfo_t &info, 601b9c1b51eSKate Stone NativeThreadLinux &thread) { 602a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 603b9cc0c75SPavel Labath const bool is_main_thread = (thread.GetID() == GetID()); 604af245d11STodd Fiala 605b9cc0c75SPavel Labath assert(info.si_signo == SIGTRAP && "Unexpected child signal!"); 606af245d11STodd Fiala 607b9c1b51eSKate Stone switch (info.si_code) { 608b9c1b51eSKate Stone // TODO: these two cases are required if we want to support tracing of the 609b9c1b51eSKate Stone // inferiors' children. We'd need this to debug a monitor. 610af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)): 611af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)): 612af245d11STodd Fiala 613b9c1b51eSKate Stone case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)): { 614b9c1b51eSKate Stone // This is the notification on the parent thread which informs us of new 615b9c1b51eSKate Stone // thread 616426bdf88SPavel Labath // creation. 617b9c1b51eSKate Stone // We don't want to do anything with the parent thread so we just resume it. 618b9c1b51eSKate Stone // In case we 619b9c1b51eSKate Stone // want to implement "break on thread creation" functionality, we would need 620b9c1b51eSKate Stone // to stop 621426bdf88SPavel Labath // here. 622af245d11STodd Fiala 623af245d11STodd Fiala unsigned long event_message = 0; 624b9c1b51eSKate Stone if (GetEventMessage(thread.GetID(), &event_message).Fail()) { 625a6321a8eSPavel Labath LLDB_LOG(log, 626a6321a8eSPavel Labath "pid {0} received thread creation event but " 627a6321a8eSPavel Labath "GetEventMessage failed so we don't know the new tid", 628a6321a8eSPavel Labath thread.GetID()); 629426bdf88SPavel Labath } else 630426bdf88SPavel Labath WaitForNewThread(event_message); 631af245d11STodd Fiala 632b9cc0c75SPavel Labath ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); 633af245d11STodd Fiala break; 634af245d11STodd Fiala } 635af245d11STodd Fiala 636b9c1b51eSKate Stone case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): { 637f9077782SPavel Labath NativeThreadLinuxSP main_thread_sp; 638a6321a8eSPavel Labath LLDB_LOG(log, "received exec event, code = {0}", info.si_code ^ SIGTRAP); 639a9882ceeSTodd Fiala 6401dbc6c9cSPavel Labath // Exec clears any pending notifications. 6410e1d729bSPavel Labath m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 642fa03ad2eSChaoren Lin 643b9c1b51eSKate Stone // Remove all but the main thread here. Linux fork creates a new process 644b9c1b51eSKate Stone // which only copies the main thread. 645a6321a8eSPavel Labath LLDB_LOG(log, "exec received, stop tracking all but main thread"); 646a9882ceeSTodd Fiala 647b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 648a9882ceeSTodd Fiala const bool is_main_thread = thread_sp && thread_sp->GetID() == GetID(); 649b9c1b51eSKate Stone if (is_main_thread) { 650f9077782SPavel Labath main_thread_sp = std::static_pointer_cast<NativeThreadLinux>(thread_sp); 651a6321a8eSPavel Labath LLDB_LOG(log, "found main thread with tid {0}, keeping", 652a6321a8eSPavel Labath main_thread_sp->GetID()); 653b9c1b51eSKate Stone } else { 654a6321a8eSPavel Labath LLDB_LOG(log, "discarding non-main-thread tid {0} due to exec", 655a6321a8eSPavel Labath thread_sp->GetID()); 656a9882ceeSTodd Fiala } 657a9882ceeSTodd Fiala } 658a9882ceeSTodd Fiala 659a9882ceeSTodd Fiala m_threads.clear(); 660a9882ceeSTodd Fiala 661b9c1b51eSKate Stone if (main_thread_sp) { 662a9882ceeSTodd Fiala m_threads.push_back(main_thread_sp); 663a9882ceeSTodd Fiala SetCurrentThreadID(main_thread_sp->GetID()); 664f9077782SPavel Labath main_thread_sp->SetStoppedByExec(); 665b9c1b51eSKate Stone } else { 666a9882ceeSTodd Fiala SetCurrentThreadID(LLDB_INVALID_THREAD_ID); 667a6321a8eSPavel Labath LLDB_LOG(log, 668a6321a8eSPavel Labath "pid {0} no main thread found, discarded all threads, " 669a6321a8eSPavel Labath "we're in a no-thread state!", 670a6321a8eSPavel Labath GetID()); 671a9882ceeSTodd Fiala } 672a9882ceeSTodd Fiala 673fa03ad2eSChaoren Lin // Tell coordinator about about the "new" (since exec) stopped main thread. 674f9077782SPavel Labath ThreadWasCreated(*main_thread_sp); 675fa03ad2eSChaoren Lin 676a9882ceeSTodd Fiala // Let our delegate know we have just exec'd. 677a9882ceeSTodd Fiala NotifyDidExec(); 678a9882ceeSTodd Fiala 679a9882ceeSTodd Fiala // If we have a main thread, indicate we are stopped. 680b9c1b51eSKate Stone assert(main_thread_sp && "exec called during ptraced process but no main " 681b9c1b51eSKate Stone "thread metadata tracked"); 682fa03ad2eSChaoren Lin 683fa03ad2eSChaoren Lin // Let the process know we're stopped. 684b9cc0c75SPavel Labath StopRunningThreads(main_thread_sp->GetID()); 685a9882ceeSTodd Fiala 686af245d11STodd Fiala break; 687a9882ceeSTodd Fiala } 688af245d11STodd Fiala 689b9c1b51eSKate Stone case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): { 690af245d11STodd Fiala // The inferior process or one of its threads is about to exit. 691b9c1b51eSKate Stone // We don't want to do anything with the thread so we just resume it. In 692b9c1b51eSKate Stone // case we 693b9c1b51eSKate Stone // want to implement "break on thread exit" functionality, we would need to 694b9c1b51eSKate Stone // stop 6956e35163cSPavel Labath // here. 696fa03ad2eSChaoren Lin 697af245d11STodd Fiala unsigned long data = 0; 698b9cc0c75SPavel Labath if (GetEventMessage(thread.GetID(), &data).Fail()) 699af245d11STodd Fiala data = -1; 700af245d11STodd Fiala 701a6321a8eSPavel Labath LLDB_LOG(log, 702a6321a8eSPavel Labath "received PTRACE_EVENT_EXIT, data = {0:x}, WIFEXITED={1}, " 703a6321a8eSPavel Labath "WIFSIGNALED={2}, pid = {3}, main_thread = {4}", 704a6321a8eSPavel Labath data, WIFEXITED(data), WIFSIGNALED(data), thread.GetID(), 705a6321a8eSPavel Labath is_main_thread); 706af245d11STodd Fiala 7073508fc8cSPavel Labath if (is_main_thread) 7083508fc8cSPavel Labath SetExitStatus(WaitStatus::Decode(data), true); 70975f47c3aSTodd Fiala 71086852d36SPavel Labath StateType state = thread.GetState(); 711b9c1b51eSKate Stone if (!StateIsRunningState(state)) { 712b9c1b51eSKate Stone // Due to a kernel bug, we may sometimes get this stop after the inferior 713b9c1b51eSKate Stone // gets a 714b9c1b51eSKate Stone // SIGKILL. This confuses our state tracking logic in ResumeThread(), 715b9c1b51eSKate Stone // since normally, 716b9c1b51eSKate Stone // we should not be receiving any ptrace events while the inferior is 717b9c1b51eSKate Stone // stopped. This 71886852d36SPavel Labath // makes sure that the inferior is resumed and exits normally. 71986852d36SPavel Labath state = eStateRunning; 72086852d36SPavel Labath } 72186852d36SPavel Labath ResumeThread(thread, state, LLDB_INVALID_SIGNAL_NUMBER); 722af245d11STodd Fiala 723af245d11STodd Fiala break; 724af245d11STodd Fiala } 725af245d11STodd Fiala 726af245d11STodd Fiala case 0: 727c16f5dcaSChaoren Lin case TRAP_TRACE: // We receive this on single stepping. 728c16f5dcaSChaoren Lin case TRAP_HWBKPT: // We receive this on watchpoint hit 72986fd8e45SChaoren Lin { 730c16f5dcaSChaoren Lin // If a watchpoint was hit, report it 731c16f5dcaSChaoren Lin uint32_t wp_index; 73297206d57SZachary Turner Status error = thread.GetRegisterContext()->GetWatchpointHitIndex( 733b9c1b51eSKate Stone wp_index, (uintptr_t)info.si_addr); 734a6321a8eSPavel Labath if (error.Fail()) 735a6321a8eSPavel Labath LLDB_LOG(log, 736a6321a8eSPavel Labath "received error while checking for watchpoint hits, pid = " 737a6321a8eSPavel Labath "{0}, error = {1}", 738a6321a8eSPavel Labath thread.GetID(), error); 739b9c1b51eSKate Stone if (wp_index != LLDB_INVALID_INDEX32) { 740b9cc0c75SPavel Labath MonitorWatchpoint(thread, wp_index); 741c16f5dcaSChaoren Lin break; 742c16f5dcaSChaoren Lin } 743b9cc0c75SPavel Labath 744d5ffbad2SOmair Javaid // If a breakpoint was hit, report it 745d5ffbad2SOmair Javaid uint32_t bp_index; 746d5ffbad2SOmair Javaid error = thread.GetRegisterContext()->GetHardwareBreakHitIndex( 747d5ffbad2SOmair Javaid bp_index, (uintptr_t)info.si_addr); 748d5ffbad2SOmair Javaid if (error.Fail()) 749d5ffbad2SOmair Javaid LLDB_LOG(log, "received error while checking for hardware " 750d5ffbad2SOmair Javaid "breakpoint hits, pid = {0}, error = {1}", 751d5ffbad2SOmair Javaid thread.GetID(), error); 752d5ffbad2SOmair Javaid if (bp_index != LLDB_INVALID_INDEX32) { 753d5ffbad2SOmair Javaid MonitorBreakpoint(thread); 754d5ffbad2SOmair Javaid break; 755d5ffbad2SOmair Javaid } 756d5ffbad2SOmair Javaid 757be379e15STamas Berghammer // Otherwise, report step over 758be379e15STamas Berghammer MonitorTrace(thread); 759af245d11STodd Fiala break; 760b9cc0c75SPavel Labath } 761af245d11STodd Fiala 762af245d11STodd Fiala case SI_KERNEL: 76335799963SMohit K. Bhakkad #if defined __mips__ 76435799963SMohit K. Bhakkad // For mips there is no special signal for watchpoint 76535799963SMohit K. Bhakkad // So we check for watchpoint in kernel trap 76635799963SMohit K. Bhakkad { 76735799963SMohit K. Bhakkad // If a watchpoint was hit, report it 76835799963SMohit K. Bhakkad uint32_t wp_index; 76997206d57SZachary Turner Status error = thread.GetRegisterContext()->GetWatchpointHitIndex( 770b9c1b51eSKate Stone wp_index, LLDB_INVALID_ADDRESS); 771a6321a8eSPavel Labath if (error.Fail()) 772a6321a8eSPavel Labath LLDB_LOG(log, 773a6321a8eSPavel Labath "received error while checking for watchpoint hits, pid = " 774a6321a8eSPavel Labath "{0}, error = {1}", 775a6321a8eSPavel Labath thread.GetID(), error); 776b9c1b51eSKate Stone if (wp_index != LLDB_INVALID_INDEX32) { 777b9cc0c75SPavel Labath MonitorWatchpoint(thread, wp_index); 77835799963SMohit K. Bhakkad break; 77935799963SMohit K. Bhakkad } 78035799963SMohit K. Bhakkad } 78135799963SMohit K. Bhakkad // NO BREAK 78235799963SMohit K. Bhakkad #endif 783af245d11STodd Fiala case TRAP_BRKPT: 784b9cc0c75SPavel Labath MonitorBreakpoint(thread); 785af245d11STodd Fiala break; 786af245d11STodd Fiala 787af245d11STodd Fiala case SIGTRAP: 788af245d11STodd Fiala case (SIGTRAP | 0x80): 789a6321a8eSPavel Labath LLDB_LOG( 790a6321a8eSPavel Labath log, 791a6321a8eSPavel Labath "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming", 792a6321a8eSPavel Labath info.si_code, GetID(), thread.GetID()); 793fa03ad2eSChaoren Lin 794af245d11STodd Fiala // Ignore these signals until we know more about them. 795b9cc0c75SPavel Labath ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); 796af245d11STodd Fiala break; 797af245d11STodd Fiala 798af245d11STodd Fiala default: 799*21a365baSPavel Labath LLDB_LOG(log, "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}", 800a6321a8eSPavel Labath info.si_code, GetID(), thread.GetID()); 801*21a365baSPavel Labath MonitorSignal(info, thread, false); 802af245d11STodd Fiala break; 803af245d11STodd Fiala } 804af245d11STodd Fiala } 805af245d11STodd Fiala 806b9c1b51eSKate Stone void NativeProcessLinux::MonitorTrace(NativeThreadLinux &thread) { 807a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 808a6321a8eSPavel Labath LLDB_LOG(log, "received trace event, pid = {0}", thread.GetID()); 809c16f5dcaSChaoren Lin 8100e1d729bSPavel Labath // This thread is currently stopped. 811b9cc0c75SPavel Labath thread.SetStoppedByTrace(); 812c16f5dcaSChaoren Lin 813b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 814c16f5dcaSChaoren Lin } 815c16f5dcaSChaoren Lin 816b9c1b51eSKate Stone void NativeProcessLinux::MonitorBreakpoint(NativeThreadLinux &thread) { 817b9c1b51eSKate Stone Log *log( 818b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 819a6321a8eSPavel Labath LLDB_LOG(log, "received breakpoint event, pid = {0}", thread.GetID()); 820c16f5dcaSChaoren Lin 821c16f5dcaSChaoren Lin // Mark the thread as stopped at breakpoint. 822b9cc0c75SPavel Labath thread.SetStoppedByBreakpoint(); 82397206d57SZachary Turner Status error = FixupBreakpointPCAsNeeded(thread); 824c16f5dcaSChaoren Lin if (error.Fail()) 825a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0} fixup: {1}", thread.GetID(), error); 826d8c338d4STamas Berghammer 827b9c1b51eSKate Stone if (m_threads_stepping_with_breakpoint.find(thread.GetID()) != 828b9c1b51eSKate Stone m_threads_stepping_with_breakpoint.end()) 829b9cc0c75SPavel Labath thread.SetStoppedByTrace(); 830c16f5dcaSChaoren Lin 831b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 832c16f5dcaSChaoren Lin } 833c16f5dcaSChaoren Lin 834b9c1b51eSKate Stone void NativeProcessLinux::MonitorWatchpoint(NativeThreadLinux &thread, 835b9c1b51eSKate Stone uint32_t wp_index) { 836b9c1b51eSKate Stone Log *log( 837b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_WATCHPOINTS)); 838a6321a8eSPavel Labath LLDB_LOG(log, "received watchpoint event, pid = {0}, wp_index = {1}", 839a6321a8eSPavel Labath thread.GetID(), wp_index); 840c16f5dcaSChaoren Lin 841c16f5dcaSChaoren Lin // Mark the thread as stopped at watchpoint. 842c16f5dcaSChaoren Lin // The address is at (lldb::addr_t)info->si_addr if we need it. 843f9077782SPavel Labath thread.SetStoppedByWatchpoint(wp_index); 844c16f5dcaSChaoren Lin 845b9c1b51eSKate Stone // We need to tell all other running threads before we notify the delegate 846b9c1b51eSKate Stone // about this stop. 847f9077782SPavel Labath StopRunningThreads(thread.GetID()); 848c16f5dcaSChaoren Lin } 849c16f5dcaSChaoren Lin 850b9c1b51eSKate Stone void NativeProcessLinux::MonitorSignal(const siginfo_t &info, 851b9c1b51eSKate Stone NativeThreadLinux &thread, bool exited) { 852b9cc0c75SPavel Labath const int signo = info.si_signo; 853b9cc0c75SPavel Labath const bool is_from_llgs = info.si_pid == getpid(); 854af245d11STodd Fiala 855a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 856af245d11STodd Fiala 857af245d11STodd Fiala // POSIX says that process behaviour is undefined after it ignores a SIGFPE, 858af245d11STodd Fiala // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a 859af245d11STodd Fiala // kill(2) or raise(3). Similarly for tgkill(2) on Linux. 860af245d11STodd Fiala // 861af245d11STodd Fiala // IOW, user generated signals never generate what we consider to be a 862af245d11STodd Fiala // "crash". 863af245d11STodd Fiala // 864af245d11STodd Fiala // Similarly, ACK signals generated by this monitor. 865af245d11STodd Fiala 866af245d11STodd Fiala // Handle the signal. 867a6321a8eSPavel Labath LLDB_LOG(log, 868a6321a8eSPavel Labath "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, " 869a6321a8eSPavel Labath "waitpid pid = {4})", 870a6321a8eSPavel Labath Host::GetSignalAsCString(signo), signo, info.si_code, 871b9cc0c75SPavel Labath thread.GetID()); 87258a2f669STodd Fiala 87358a2f669STodd Fiala // Check for thread stop notification. 874b9c1b51eSKate Stone if (is_from_llgs && (info.si_code == SI_TKILL) && (signo == SIGSTOP)) { 875af245d11STodd Fiala // This is a tgkill()-based stop. 876a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} tid {1}, thread stopped", GetID(), thread.GetID()); 877fa03ad2eSChaoren Lin 878aab58633SChaoren Lin // Check that we're not already marked with a stop reason. 879b9c1b51eSKate Stone // Note this thread really shouldn't already be marked as stopped - if we 880a6321a8eSPavel Labath // were, that would imply that the kernel signaled us with the thread 881a6321a8eSPavel Labath // stopping which we handled and marked as stopped, and that, without an 882a6321a8eSPavel Labath // intervening resume, we received another stop. It is more likely that we 883a6321a8eSPavel Labath // are missing the marking of a run state somewhere if we find that the 884a6321a8eSPavel Labath // thread was marked as stopped. 885b9cc0c75SPavel Labath const StateType thread_state = thread.GetState(); 886b9c1b51eSKate Stone if (!StateIsStoppedState(thread_state, false)) { 887ed89c7feSPavel Labath // An inferior thread has stopped because of a SIGSTOP we have sent it. 888b9c1b51eSKate Stone // Generally, these are not important stops and we don't want to report 889a6321a8eSPavel Labath // them as they are just used to stop other threads when one thread (the 890a6321a8eSPavel Labath // one with the *real* stop reason) hits a breakpoint (watchpoint, 891a6321a8eSPavel Labath // etc...). However, in the case of an asynchronous Interrupt(), this *is* 892a6321a8eSPavel Labath // the real stop reason, so we leave the signal intact if this is the 893a6321a8eSPavel Labath // thread that was chosen as the triggering thread. 894b9c1b51eSKate Stone if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) { 895b9cc0c75SPavel Labath if (m_pending_notification_tid == thread.GetID()) 896b9cc0c75SPavel Labath thread.SetStoppedBySignal(SIGSTOP, &info); 897ed89c7feSPavel Labath else 898b9cc0c75SPavel Labath thread.SetStoppedWithNoReason(); 899ed89c7feSPavel Labath 900b9cc0c75SPavel Labath SetCurrentThreadID(thread.GetID()); 9010e1d729bSPavel Labath SignalIfAllThreadsStopped(); 902b9c1b51eSKate Stone } else { 9030e1d729bSPavel Labath // We can end up here if stop was initiated by LLGS but by this time a 9040e1d729bSPavel Labath // thread stop has occurred - maybe initiated by another event. 90597206d57SZachary Turner Status error = ResumeThread(thread, thread.GetState(), 0); 906a6321a8eSPavel Labath if (error.Fail()) 907a6321a8eSPavel Labath LLDB_LOG(log, "failed to resume thread {0}: {1}", thread.GetID(), 908a6321a8eSPavel Labath error); 9090e1d729bSPavel Labath } 910b9c1b51eSKate Stone } else { 911a6321a8eSPavel Labath LLDB_LOG(log, 912a6321a8eSPavel Labath "pid {0} tid {1}, thread was already marked as a stopped " 913a6321a8eSPavel Labath "state (state={2}), leaving stop signal as is", 9148198db30SPavel Labath GetID(), thread.GetID(), thread_state); 9150e1d729bSPavel Labath SignalIfAllThreadsStopped(); 916af245d11STodd Fiala } 917af245d11STodd Fiala 91858a2f669STodd Fiala // Done handling. 919af245d11STodd Fiala return; 920af245d11STodd Fiala } 921af245d11STodd Fiala 9224a705e7eSPavel Labath // Check if debugger should stop at this signal or just ignore it 9234a705e7eSPavel Labath // and resume the inferior. 9244a705e7eSPavel Labath if (m_signals_to_ignore.find(signo) != m_signals_to_ignore.end()) { 9254a705e7eSPavel Labath ResumeThread(thread, thread.GetState(), signo); 9264a705e7eSPavel Labath return; 9274a705e7eSPavel Labath } 9284a705e7eSPavel Labath 92986fd8e45SChaoren Lin // This thread is stopped. 930a6321a8eSPavel Labath LLDB_LOG(log, "received signal {0}", Host::GetSignalAsCString(signo)); 931b9cc0c75SPavel Labath thread.SetStoppedBySignal(signo, &info); 93286fd8e45SChaoren Lin 93386fd8e45SChaoren Lin // Send a stop to the debugger after we get all other threads to stop. 934b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 935511e5cdcSTodd Fiala } 936af245d11STodd Fiala 937e7708688STamas Berghammer namespace { 938e7708688STamas Berghammer 939b9c1b51eSKate Stone struct EmulatorBaton { 940e7708688STamas Berghammer NativeProcessLinux *m_process; 941e7708688STamas Berghammer NativeRegisterContext *m_reg_context; 9426648fcc3SPavel Labath 9436648fcc3SPavel Labath // eRegisterKindDWARF -> RegsiterValue 9446648fcc3SPavel Labath std::unordered_map<uint32_t, RegisterValue> m_register_values; 945e7708688STamas Berghammer 946b9c1b51eSKate Stone EmulatorBaton(NativeProcessLinux *process, NativeRegisterContext *reg_context) 947b9c1b51eSKate Stone : m_process(process), m_reg_context(reg_context) {} 948e7708688STamas Berghammer }; 949e7708688STamas Berghammer 950e7708688STamas Berghammer } // anonymous namespace 951e7708688STamas Berghammer 952b9c1b51eSKate Stone static size_t ReadMemoryCallback(EmulateInstruction *instruction, void *baton, 953e7708688STamas Berghammer const EmulateInstruction::Context &context, 954b9c1b51eSKate Stone lldb::addr_t addr, void *dst, size_t length) { 955e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 956e7708688STamas Berghammer 9573eb4b458SChaoren Lin size_t bytes_read; 958e7708688STamas Berghammer emulator_baton->m_process->ReadMemory(addr, dst, length, bytes_read); 959e7708688STamas Berghammer return bytes_read; 960e7708688STamas Berghammer } 961e7708688STamas Berghammer 962b9c1b51eSKate Stone static bool ReadRegisterCallback(EmulateInstruction *instruction, void *baton, 963e7708688STamas Berghammer const RegisterInfo *reg_info, 964b9c1b51eSKate Stone RegisterValue ®_value) { 965e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 966e7708688STamas Berghammer 967b9c1b51eSKate Stone auto it = emulator_baton->m_register_values.find( 968b9c1b51eSKate Stone reg_info->kinds[eRegisterKindDWARF]); 969b9c1b51eSKate Stone if (it != emulator_baton->m_register_values.end()) { 9706648fcc3SPavel Labath reg_value = it->second; 9716648fcc3SPavel Labath return true; 9726648fcc3SPavel Labath } 9736648fcc3SPavel Labath 974e7708688STamas Berghammer // The emulator only fill in the dwarf regsiter numbers (and in some case 975e7708688STamas Berghammer // the generic register numbers). Get the full register info from the 976e7708688STamas Berghammer // register context based on the dwarf register numbers. 977b9c1b51eSKate Stone const RegisterInfo *full_reg_info = 978b9c1b51eSKate Stone emulator_baton->m_reg_context->GetRegisterInfo( 979e7708688STamas Berghammer eRegisterKindDWARF, reg_info->kinds[eRegisterKindDWARF]); 980e7708688STamas Berghammer 98197206d57SZachary Turner Status error = 982b9c1b51eSKate Stone emulator_baton->m_reg_context->ReadRegister(full_reg_info, reg_value); 9836648fcc3SPavel Labath if (error.Success()) 9846648fcc3SPavel Labath return true; 985cdc22a88SMohit K. Bhakkad 9866648fcc3SPavel Labath return false; 987e7708688STamas Berghammer } 988e7708688STamas Berghammer 989b9c1b51eSKate Stone static bool WriteRegisterCallback(EmulateInstruction *instruction, void *baton, 990e7708688STamas Berghammer const EmulateInstruction::Context &context, 991e7708688STamas Berghammer const RegisterInfo *reg_info, 992b9c1b51eSKate Stone const RegisterValue ®_value) { 993e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 994b9c1b51eSKate Stone emulator_baton->m_register_values[reg_info->kinds[eRegisterKindDWARF]] = 995b9c1b51eSKate Stone reg_value; 996e7708688STamas Berghammer return true; 997e7708688STamas Berghammer } 998e7708688STamas Berghammer 999b9c1b51eSKate Stone static size_t WriteMemoryCallback(EmulateInstruction *instruction, void *baton, 1000e7708688STamas Berghammer const EmulateInstruction::Context &context, 1001b9c1b51eSKate Stone lldb::addr_t addr, const void *dst, 1002b9c1b51eSKate Stone size_t length) { 1003e7708688STamas Berghammer return length; 1004e7708688STamas Berghammer } 1005e7708688STamas Berghammer 1006b9c1b51eSKate Stone static lldb::addr_t ReadFlags(NativeRegisterContext *regsiter_context) { 1007e7708688STamas Berghammer const RegisterInfo *flags_info = regsiter_context->GetRegisterInfo( 1008e7708688STamas Berghammer eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 1009b9c1b51eSKate Stone return regsiter_context->ReadRegisterAsUnsigned(flags_info, 1010b9c1b51eSKate Stone LLDB_INVALID_ADDRESS); 1011e7708688STamas Berghammer } 1012e7708688STamas Berghammer 101397206d57SZachary Turner Status 101497206d57SZachary Turner NativeProcessLinux::SetupSoftwareSingleStepping(NativeThreadLinux &thread) { 101597206d57SZachary Turner Status error; 1016b9cc0c75SPavel Labath NativeRegisterContextSP register_context_sp = thread.GetRegisterContext(); 1017e7708688STamas Berghammer 1018e7708688STamas Berghammer std::unique_ptr<EmulateInstruction> emulator_ap( 1019b9c1b51eSKate Stone EmulateInstruction::FindPlugin(m_arch, eInstructionTypePCModifying, 1020b9c1b51eSKate Stone nullptr)); 1021e7708688STamas Berghammer 1022e7708688STamas Berghammer if (emulator_ap == nullptr) 102397206d57SZachary Turner return Status("Instruction emulator not found!"); 1024e7708688STamas Berghammer 1025e7708688STamas Berghammer EmulatorBaton baton(this, register_context_sp.get()); 1026e7708688STamas Berghammer emulator_ap->SetBaton(&baton); 1027e7708688STamas Berghammer emulator_ap->SetReadMemCallback(&ReadMemoryCallback); 1028e7708688STamas Berghammer emulator_ap->SetReadRegCallback(&ReadRegisterCallback); 1029e7708688STamas Berghammer emulator_ap->SetWriteMemCallback(&WriteMemoryCallback); 1030e7708688STamas Berghammer emulator_ap->SetWriteRegCallback(&WriteRegisterCallback); 1031e7708688STamas Berghammer 1032e7708688STamas Berghammer if (!emulator_ap->ReadInstruction()) 103397206d57SZachary Turner return Status("Read instruction failed!"); 1034e7708688STamas Berghammer 1035b9c1b51eSKate Stone bool emulation_result = 1036b9c1b51eSKate Stone emulator_ap->EvaluateInstruction(eEmulateInstructionOptionAutoAdvancePC); 10376648fcc3SPavel Labath 1038b9c1b51eSKate Stone const RegisterInfo *reg_info_pc = register_context_sp->GetRegisterInfo( 1039b9c1b51eSKate Stone eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); 1040b9c1b51eSKate Stone const RegisterInfo *reg_info_flags = register_context_sp->GetRegisterInfo( 1041b9c1b51eSKate Stone eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 10426648fcc3SPavel Labath 1043b9c1b51eSKate Stone auto pc_it = 1044b9c1b51eSKate Stone baton.m_register_values.find(reg_info_pc->kinds[eRegisterKindDWARF]); 1045b9c1b51eSKate Stone auto flags_it = 1046b9c1b51eSKate Stone baton.m_register_values.find(reg_info_flags->kinds[eRegisterKindDWARF]); 10476648fcc3SPavel Labath 1048e7708688STamas Berghammer lldb::addr_t next_pc; 1049e7708688STamas Berghammer lldb::addr_t next_flags; 1050b9c1b51eSKate Stone if (emulation_result) { 1051b9c1b51eSKate Stone assert(pc_it != baton.m_register_values.end() && 1052b9c1b51eSKate Stone "Emulation was successfull but PC wasn't updated"); 10536648fcc3SPavel Labath next_pc = pc_it->second.GetAsUInt64(); 10546648fcc3SPavel Labath 10556648fcc3SPavel Labath if (flags_it != baton.m_register_values.end()) 10566648fcc3SPavel Labath next_flags = flags_it->second.GetAsUInt64(); 1057e7708688STamas Berghammer else 1058e7708688STamas Berghammer next_flags = ReadFlags(register_context_sp.get()); 1059b9c1b51eSKate Stone } else if (pc_it == baton.m_register_values.end()) { 1060e7708688STamas Berghammer // Emulate instruction failed and it haven't changed PC. Advance PC 1061e7708688STamas Berghammer // with the size of the current opcode because the emulation of all 1062e7708688STamas Berghammer // PC modifying instruction should be successful. The failure most 1063e7708688STamas Berghammer // likely caused by a not supported instruction which don't modify PC. 1064b9c1b51eSKate Stone next_pc = 1065b9c1b51eSKate Stone register_context_sp->GetPC() + emulator_ap->GetOpcode().GetByteSize(); 1066e7708688STamas Berghammer next_flags = ReadFlags(register_context_sp.get()); 1067b9c1b51eSKate Stone } else { 1068e7708688STamas Berghammer // The instruction emulation failed after it modified the PC. It is an 1069e7708688STamas Berghammer // unknown error where we can't continue because the next instruction is 1070e7708688STamas Berghammer // modifying the PC but we don't know how. 107197206d57SZachary Turner return Status("Instruction emulation failed unexpectedly."); 1072e7708688STamas Berghammer } 1073e7708688STamas Berghammer 1074b9c1b51eSKate Stone if (m_arch.GetMachine() == llvm::Triple::arm) { 1075b9c1b51eSKate Stone if (next_flags & 0x20) { 1076e7708688STamas Berghammer // Thumb mode 1077e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 2); 1078b9c1b51eSKate Stone } else { 1079e7708688STamas Berghammer // Arm mode 1080e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 4); 1081e7708688STamas Berghammer } 1082b9c1b51eSKate Stone } else if (m_arch.GetMachine() == llvm::Triple::mips64 || 1083b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64el || 1084b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips || 1085b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mipsel) 1086cdc22a88SMohit K. Bhakkad error = SetSoftwareBreakpoint(next_pc, 4); 1087b9c1b51eSKate Stone else { 1088e7708688STamas Berghammer // No size hint is given for the next breakpoint 1089e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 0); 1090e7708688STamas Berghammer } 1091e7708688STamas Berghammer 109242eb6908SPavel Labath // If setting the breakpoint fails because next_pc is out of 109342eb6908SPavel Labath // the address space, ignore it and let the debugee segfault. 109442eb6908SPavel Labath if (error.GetError() == EIO || error.GetError() == EFAULT) { 109597206d57SZachary Turner return Status(); 109642eb6908SPavel Labath } else if (error.Fail()) 1097e7708688STamas Berghammer return error; 1098e7708688STamas Berghammer 1099b9cc0c75SPavel Labath m_threads_stepping_with_breakpoint.insert({thread.GetID(), next_pc}); 1100e7708688STamas Berghammer 110197206d57SZachary Turner return Status(); 1102e7708688STamas Berghammer } 1103e7708688STamas Berghammer 1104b9c1b51eSKate Stone bool NativeProcessLinux::SupportHardwareSingleStepping() const { 1105b9c1b51eSKate Stone if (m_arch.GetMachine() == llvm::Triple::arm || 1106b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64 || 1107b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64el || 1108b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips || 1109b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mipsel) 1110cdc22a88SMohit K. Bhakkad return false; 1111cdc22a88SMohit K. Bhakkad return true; 1112e7708688STamas Berghammer } 1113e7708688STamas Berghammer 111497206d57SZachary Turner Status NativeProcessLinux::Resume(const ResumeActionList &resume_actions) { 1115a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1116a6321a8eSPavel Labath LLDB_LOG(log, "pid {0}", GetID()); 1117af245d11STodd Fiala 1118e7708688STamas Berghammer bool software_single_step = !SupportHardwareSingleStepping(); 1119af245d11STodd Fiala 1120b9c1b51eSKate Stone if (software_single_step) { 1121b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 1122e7708688STamas Berghammer assert(thread_sp && "thread list should not contain NULL threads"); 1123e7708688STamas Berghammer 1124b9c1b51eSKate Stone const ResumeAction *const action = 1125b9c1b51eSKate Stone resume_actions.GetActionForThread(thread_sp->GetID(), true); 1126e7708688STamas Berghammer if (action == nullptr) 1127e7708688STamas Berghammer continue; 1128e7708688STamas Berghammer 1129b9c1b51eSKate Stone if (action->state == eStateStepping) { 113097206d57SZachary Turner Status error = SetupSoftwareSingleStepping( 1131b9c1b51eSKate Stone static_cast<NativeThreadLinux &>(*thread_sp)); 1132e7708688STamas Berghammer if (error.Fail()) 1133e7708688STamas Berghammer return error; 1134e7708688STamas Berghammer } 1135e7708688STamas Berghammer } 1136e7708688STamas Berghammer } 1137e7708688STamas Berghammer 1138b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 1139af245d11STodd Fiala assert(thread_sp && "thread list should not contain NULL threads"); 1140af245d11STodd Fiala 1141b9c1b51eSKate Stone const ResumeAction *const action = 1142b9c1b51eSKate Stone resume_actions.GetActionForThread(thread_sp->GetID(), true); 11436a196ce6SChaoren Lin 1144b9c1b51eSKate Stone if (action == nullptr) { 1145a6321a8eSPavel Labath LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(), 1146a6321a8eSPavel Labath thread_sp->GetID()); 11476a196ce6SChaoren Lin continue; 11486a196ce6SChaoren Lin } 1149af245d11STodd Fiala 1150a6321a8eSPavel Labath LLDB_LOG(log, "processing resume action state {0} for pid {1} tid {2}", 11518198db30SPavel Labath action->state, GetID(), thread_sp->GetID()); 1152af245d11STodd Fiala 1153b9c1b51eSKate Stone switch (action->state) { 1154af245d11STodd Fiala case eStateRunning: 1155b9c1b51eSKate Stone case eStateStepping: { 1156af245d11STodd Fiala // Run the thread, possibly feeding it the signal. 1157fa03ad2eSChaoren Lin const int signo = action->signal; 1158b9c1b51eSKate Stone ResumeThread(static_cast<NativeThreadLinux &>(*thread_sp), action->state, 1159b9c1b51eSKate Stone signo); 1160af245d11STodd Fiala break; 1161ae29d395SChaoren Lin } 1162af245d11STodd Fiala 1163af245d11STodd Fiala case eStateSuspended: 1164af245d11STodd Fiala case eStateStopped: 1165a6321a8eSPavel Labath llvm_unreachable("Unexpected state"); 1166af245d11STodd Fiala 1167af245d11STodd Fiala default: 116897206d57SZachary Turner return Status("NativeProcessLinux::%s (): unexpected state %s specified " 1169b9c1b51eSKate Stone "for pid %" PRIu64 ", tid %" PRIu64, 1170b9c1b51eSKate Stone __FUNCTION__, StateAsCString(action->state), GetID(), 1171b9c1b51eSKate Stone thread_sp->GetID()); 1172af245d11STodd Fiala } 1173af245d11STodd Fiala } 1174af245d11STodd Fiala 117597206d57SZachary Turner return Status(); 1176af245d11STodd Fiala } 1177af245d11STodd Fiala 117897206d57SZachary Turner Status NativeProcessLinux::Halt() { 117997206d57SZachary Turner Status error; 1180af245d11STodd Fiala 1181af245d11STodd Fiala if (kill(GetID(), SIGSTOP) != 0) 1182af245d11STodd Fiala error.SetErrorToErrno(); 1183af245d11STodd Fiala 1184af245d11STodd Fiala return error; 1185af245d11STodd Fiala } 1186af245d11STodd Fiala 118797206d57SZachary Turner Status NativeProcessLinux::Detach() { 118897206d57SZachary Turner Status error; 1189af245d11STodd Fiala 1190af245d11STodd Fiala // Stop monitoring the inferior. 119119cbe96aSPavel Labath m_sigchld_handle.reset(); 1192af245d11STodd Fiala 11937a9495bcSPavel Labath // Tell ptrace to detach from the process. 11947a9495bcSPavel Labath if (GetID() == LLDB_INVALID_PROCESS_ID) 11957a9495bcSPavel Labath return error; 11967a9495bcSPavel Labath 1197b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 119897206d57SZachary Turner Status e = Detach(thread_sp->GetID()); 11997a9495bcSPavel Labath if (e.Fail()) 1200b9c1b51eSKate Stone error = 1201b9c1b51eSKate Stone e; // Save the error, but still attempt to detach from other threads. 12027a9495bcSPavel Labath } 12037a9495bcSPavel Labath 120499e37695SRavitheja Addepally m_processor_trace_monitor.clear(); 120599e37695SRavitheja Addepally m_pt_proces_trace_id = LLDB_INVALID_UID; 120699e37695SRavitheja Addepally 1207af245d11STodd Fiala return error; 1208af245d11STodd Fiala } 1209af245d11STodd Fiala 121097206d57SZachary Turner Status NativeProcessLinux::Signal(int signo) { 121197206d57SZachary Turner Status error; 1212af245d11STodd Fiala 1213a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1214a6321a8eSPavel Labath LLDB_LOG(log, "sending signal {0} ({1}) to pid {1}", signo, 1215a6321a8eSPavel Labath Host::GetSignalAsCString(signo), GetID()); 1216af245d11STodd Fiala 1217af245d11STodd Fiala if (kill(GetID(), signo)) 1218af245d11STodd Fiala error.SetErrorToErrno(); 1219af245d11STodd Fiala 1220af245d11STodd Fiala return error; 1221af245d11STodd Fiala } 1222af245d11STodd Fiala 122397206d57SZachary Turner Status NativeProcessLinux::Interrupt() { 1224e9547b80SChaoren Lin // Pick a running thread (or if none, a not-dead stopped thread) as 1225e9547b80SChaoren Lin // the chosen thread that will be the stop-reason thread. 1226a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1227e9547b80SChaoren Lin 1228e9547b80SChaoren Lin NativeThreadProtocolSP running_thread_sp; 1229e9547b80SChaoren Lin NativeThreadProtocolSP stopped_thread_sp; 1230e9547b80SChaoren Lin 1231a6321a8eSPavel Labath LLDB_LOG(log, "selecting running thread for interrupt target"); 1232b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 1233e9547b80SChaoren Lin // The thread shouldn't be null but lets just cover that here. 1234e9547b80SChaoren Lin if (!thread_sp) 1235e9547b80SChaoren Lin continue; 1236e9547b80SChaoren Lin 1237e9547b80SChaoren Lin // If we have a running or stepping thread, we'll call that the 1238e9547b80SChaoren Lin // target of the interrupt. 1239e9547b80SChaoren Lin const auto thread_state = thread_sp->GetState(); 1240b9c1b51eSKate Stone if (thread_state == eStateRunning || thread_state == eStateStepping) { 1241e9547b80SChaoren Lin running_thread_sp = thread_sp; 1242e9547b80SChaoren Lin break; 1243b9c1b51eSKate Stone } else if (!stopped_thread_sp && StateIsStoppedState(thread_state, true)) { 1244b9c1b51eSKate Stone // Remember the first non-dead stopped thread. We'll use that as a backup 1245b9c1b51eSKate Stone // if there are no running threads. 1246e9547b80SChaoren Lin stopped_thread_sp = thread_sp; 1247e9547b80SChaoren Lin } 1248e9547b80SChaoren Lin } 1249e9547b80SChaoren Lin 1250b9c1b51eSKate Stone if (!running_thread_sp && !stopped_thread_sp) { 125197206d57SZachary Turner Status error("found no running/stepping or live stopped threads as target " 1252b9c1b51eSKate Stone "for interrupt"); 1253a6321a8eSPavel Labath LLDB_LOG(log, "skipping due to error: {0}", error); 12545830aa75STamas Berghammer 1255e9547b80SChaoren Lin return error; 1256e9547b80SChaoren Lin } 1257e9547b80SChaoren Lin 1258b9c1b51eSKate Stone NativeThreadProtocolSP deferred_signal_thread_sp = 1259b9c1b51eSKate Stone running_thread_sp ? running_thread_sp : stopped_thread_sp; 1260e9547b80SChaoren Lin 1261a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} {1} tid {2} chosen for interrupt target", GetID(), 1262e9547b80SChaoren Lin running_thread_sp ? "running" : "stopped", 1263e9547b80SChaoren Lin deferred_signal_thread_sp->GetID()); 1264e9547b80SChaoren Lin 1265ed89c7feSPavel Labath StopRunningThreads(deferred_signal_thread_sp->GetID()); 126645f5cb31SPavel Labath 126797206d57SZachary Turner return Status(); 1268e9547b80SChaoren Lin } 1269e9547b80SChaoren Lin 127097206d57SZachary Turner Status NativeProcessLinux::Kill() { 1271a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1272a6321a8eSPavel Labath LLDB_LOG(log, "pid {0}", GetID()); 1273af245d11STodd Fiala 127497206d57SZachary Turner Status error; 1275af245d11STodd Fiala 1276b9c1b51eSKate Stone switch (m_state) { 1277af245d11STodd Fiala case StateType::eStateInvalid: 1278af245d11STodd Fiala case StateType::eStateExited: 1279af245d11STodd Fiala case StateType::eStateCrashed: 1280af245d11STodd Fiala case StateType::eStateDetached: 1281af245d11STodd Fiala case StateType::eStateUnloaded: 1282af245d11STodd Fiala // Nothing to do - the process is already dead. 1283a6321a8eSPavel Labath LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(), 12848198db30SPavel Labath m_state); 1285af245d11STodd Fiala return error; 1286af245d11STodd Fiala 1287af245d11STodd Fiala case StateType::eStateConnected: 1288af245d11STodd Fiala case StateType::eStateAttaching: 1289af245d11STodd Fiala case StateType::eStateLaunching: 1290af245d11STodd Fiala case StateType::eStateStopped: 1291af245d11STodd Fiala case StateType::eStateRunning: 1292af245d11STodd Fiala case StateType::eStateStepping: 1293af245d11STodd Fiala case StateType::eStateSuspended: 1294af245d11STodd Fiala // We can try to kill a process in these states. 1295af245d11STodd Fiala break; 1296af245d11STodd Fiala } 1297af245d11STodd Fiala 1298b9c1b51eSKate Stone if (kill(GetID(), SIGKILL) != 0) { 1299af245d11STodd Fiala error.SetErrorToErrno(); 1300af245d11STodd Fiala return error; 1301af245d11STodd Fiala } 1302af245d11STodd Fiala 1303af245d11STodd Fiala return error; 1304af245d11STodd Fiala } 1305af245d11STodd Fiala 130697206d57SZachary Turner static Status 130715930862SPavel Labath ParseMemoryRegionInfoFromProcMapsLine(llvm::StringRef &maps_line, 1308b9c1b51eSKate Stone MemoryRegionInfo &memory_region_info) { 1309af245d11STodd Fiala memory_region_info.Clear(); 1310af245d11STodd Fiala 131115930862SPavel Labath StringExtractor line_extractor(maps_line); 1312af245d11STodd Fiala 1313b9c1b51eSKate Stone // Format: {address_start_hex}-{address_end_hex} perms offset dev inode 1314b9c1b51eSKate Stone // pathname 1315b9c1b51eSKate Stone // perms: rwxp (letter is present if set, '-' if not, final character is 1316b9c1b51eSKate Stone // p=private, s=shared). 1317af245d11STodd Fiala 1318af245d11STodd Fiala // Parse out the starting address 1319af245d11STodd Fiala lldb::addr_t start_address = line_extractor.GetHexMaxU64(false, 0); 1320af245d11STodd Fiala 1321af245d11STodd Fiala // Parse out hyphen separating start and end address from range. 1322af245d11STodd Fiala if (!line_extractor.GetBytesLeft() || (line_extractor.GetChar() != '-')) 132397206d57SZachary Turner return Status( 1324b9c1b51eSKate Stone "malformed /proc/{pid}/maps entry, missing dash between address range"); 1325af245d11STodd Fiala 1326af245d11STodd Fiala // Parse out the ending address 1327af245d11STodd Fiala lldb::addr_t end_address = line_extractor.GetHexMaxU64(false, start_address); 1328af245d11STodd Fiala 1329af245d11STodd Fiala // Parse out the space after the address. 1330af245d11STodd Fiala if (!line_extractor.GetBytesLeft() || (line_extractor.GetChar() != ' ')) 133197206d57SZachary Turner return Status( 133297206d57SZachary Turner "malformed /proc/{pid}/maps entry, missing space after range"); 1333af245d11STodd Fiala 1334af245d11STodd Fiala // Save the range. 1335af245d11STodd Fiala memory_region_info.GetRange().SetRangeBase(start_address); 1336af245d11STodd Fiala memory_region_info.GetRange().SetRangeEnd(end_address); 1337af245d11STodd Fiala 1338b9c1b51eSKate Stone // Any memory region in /proc/{pid}/maps is by definition mapped into the 1339b9c1b51eSKate Stone // process. 1340ad007563SHoward Hellyer memory_region_info.SetMapped(MemoryRegionInfo::OptionalBool::eYes); 1341ad007563SHoward Hellyer 1342af245d11STodd Fiala // Parse out each permission entry. 1343af245d11STodd Fiala if (line_extractor.GetBytesLeft() < 4) 134497206d57SZachary Turner return Status("malformed /proc/{pid}/maps entry, missing some portion of " 1345b9c1b51eSKate Stone "permissions"); 1346af245d11STodd Fiala 1347af245d11STodd Fiala // Handle read permission. 1348af245d11STodd Fiala const char read_perm_char = line_extractor.GetChar(); 1349af245d11STodd Fiala if (read_perm_char == 'r') 1350af245d11STodd Fiala memory_region_info.SetReadable(MemoryRegionInfo::OptionalBool::eYes); 1351c73301bbSTamas Berghammer else if (read_perm_char == '-') 1352af245d11STodd Fiala memory_region_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 1353c73301bbSTamas Berghammer else 135497206d57SZachary Turner return Status("unexpected /proc/{pid}/maps read permission char"); 1355af245d11STodd Fiala 1356af245d11STodd Fiala // Handle write permission. 1357af245d11STodd Fiala const char write_perm_char = line_extractor.GetChar(); 1358af245d11STodd Fiala if (write_perm_char == 'w') 1359af245d11STodd Fiala memory_region_info.SetWritable(MemoryRegionInfo::OptionalBool::eYes); 1360c73301bbSTamas Berghammer else if (write_perm_char == '-') 1361af245d11STodd Fiala memory_region_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 1362c73301bbSTamas Berghammer else 136397206d57SZachary Turner return Status("unexpected /proc/{pid}/maps write permission char"); 1364af245d11STodd Fiala 1365af245d11STodd Fiala // Handle execute permission. 1366af245d11STodd Fiala const char exec_perm_char = line_extractor.GetChar(); 1367af245d11STodd Fiala if (exec_perm_char == 'x') 1368af245d11STodd Fiala memory_region_info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes); 1369c73301bbSTamas Berghammer else if (exec_perm_char == '-') 1370af245d11STodd Fiala memory_region_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1371c73301bbSTamas Berghammer else 137297206d57SZachary Turner return Status("unexpected /proc/{pid}/maps exec permission char"); 1373af245d11STodd Fiala 1374d7d69f80STamas Berghammer line_extractor.GetChar(); // Read the private bit 1375d7d69f80STamas Berghammer line_extractor.SkipSpaces(); // Skip the separator 1376d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the offset 1377d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the major device number 1378d7d69f80STamas Berghammer line_extractor.GetChar(); // Read the device id separator 1379d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the major device number 1380d7d69f80STamas Berghammer line_extractor.SkipSpaces(); // Skip the separator 1381d7d69f80STamas Berghammer line_extractor.GetU64(0, 10); // Read the inode number 1382d7d69f80STamas Berghammer 1383d7d69f80STamas Berghammer line_extractor.SkipSpaces(); 1384b9739d40SPavel Labath const char *name = line_extractor.Peek(); 1385b9739d40SPavel Labath if (name) 1386b9739d40SPavel Labath memory_region_info.SetName(name); 1387d7d69f80STamas Berghammer 138897206d57SZachary Turner return Status(); 1389af245d11STodd Fiala } 1390af245d11STodd Fiala 139197206d57SZachary Turner Status NativeProcessLinux::GetMemoryRegionInfo(lldb::addr_t load_addr, 1392b9c1b51eSKate Stone MemoryRegionInfo &range_info) { 1393b9c1b51eSKate Stone // FIXME review that the final memory region returned extends to the end of 1394b9c1b51eSKate Stone // the virtual address space, 1395af245d11STodd Fiala // with no perms if it is not mapped. 1396af245d11STodd Fiala 1397af245d11STodd Fiala // Use an approach that reads memory regions from /proc/{pid}/maps. 1398af245d11STodd Fiala // Assume proc maps entries are in ascending order. 1399af245d11STodd Fiala // FIXME assert if we find differently. 1400af245d11STodd Fiala 1401b9c1b51eSKate Stone if (m_supports_mem_region == LazyBool::eLazyBoolNo) { 1402af245d11STodd Fiala // We're done. 140397206d57SZachary Turner return Status("unsupported"); 1404af245d11STodd Fiala } 1405af245d11STodd Fiala 140697206d57SZachary Turner Status error = PopulateMemoryRegionCache(); 1407b9c1b51eSKate Stone if (error.Fail()) { 1408af245d11STodd Fiala return error; 1409af245d11STodd Fiala } 1410af245d11STodd Fiala 1411af245d11STodd Fiala lldb::addr_t prev_base_address = 0; 1412af245d11STodd Fiala 1413b9c1b51eSKate Stone // FIXME start by finding the last region that is <= target address using 1414b9c1b51eSKate Stone // binary search. Data is sorted. 1415af245d11STodd Fiala // There can be a ton of regions on pthreads apps with lots of threads. 1416b9c1b51eSKate Stone for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end(); 1417b9c1b51eSKate Stone ++it) { 1418a6f5795aSTamas Berghammer MemoryRegionInfo &proc_entry_info = it->first; 1419af245d11STodd Fiala 1420af245d11STodd Fiala // Sanity check assumption that /proc/{pid}/maps entries are ascending. 1421b9c1b51eSKate Stone assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && 1422b9c1b51eSKate Stone "descending /proc/pid/maps entries detected, unexpected"); 1423af245d11STodd Fiala prev_base_address = proc_entry_info.GetRange().GetRangeBase(); 1424b1554311SHafiz Abid Qadeer UNUSED_IF_ASSERT_DISABLED(prev_base_address); 1425af245d11STodd Fiala 1426b9c1b51eSKate Stone // If the target address comes before this entry, indicate distance to next 1427b9c1b51eSKate Stone // region. 1428b9c1b51eSKate Stone if (load_addr < proc_entry_info.GetRange().GetRangeBase()) { 1429af245d11STodd Fiala range_info.GetRange().SetRangeBase(load_addr); 1430b9c1b51eSKate Stone range_info.GetRange().SetByteSize( 1431b9c1b51eSKate Stone proc_entry_info.GetRange().GetRangeBase() - load_addr); 1432af245d11STodd Fiala range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 1433af245d11STodd Fiala range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 1434af245d11STodd Fiala range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1435ad007563SHoward Hellyer range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 1436af245d11STodd Fiala 1437af245d11STodd Fiala return error; 1438b9c1b51eSKate Stone } else if (proc_entry_info.GetRange().Contains(load_addr)) { 1439af245d11STodd Fiala // The target address is within the memory region we're processing here. 1440af245d11STodd Fiala range_info = proc_entry_info; 1441af245d11STodd Fiala return error; 1442af245d11STodd Fiala } 1443af245d11STodd Fiala 1444b9c1b51eSKate Stone // The target memory address comes somewhere after the region we just 1445b9c1b51eSKate Stone // parsed. 1446af245d11STodd Fiala } 1447af245d11STodd Fiala 1448b9c1b51eSKate Stone // If we made it here, we didn't find an entry that contained the given 1449b9c1b51eSKate Stone // address. Return the 1450b9c1b51eSKate Stone // load_addr as start and the amount of bytes betwwen load address and the end 1451b9c1b51eSKate Stone // of the memory as 145209839c33STamas Berghammer // size. 145309839c33STamas Berghammer range_info.GetRange().SetRangeBase(load_addr); 1454ad007563SHoward Hellyer range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); 145509839c33STamas Berghammer range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 145609839c33STamas Berghammer range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 145709839c33STamas Berghammer range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1458ad007563SHoward Hellyer range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 1459af245d11STodd Fiala return error; 1460af245d11STodd Fiala } 1461af245d11STodd Fiala 146297206d57SZachary Turner Status NativeProcessLinux::PopulateMemoryRegionCache() { 1463a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1464a6f5795aSTamas Berghammer 1465a6f5795aSTamas Berghammer // If our cache is empty, pull the latest. There should always be at least 1466a6f5795aSTamas Berghammer // one memory region if memory region handling is supported. 1467a6f5795aSTamas Berghammer if (!m_mem_region_cache.empty()) { 1468a6321a8eSPavel Labath LLDB_LOG(log, "reusing {0} cached memory region entries", 1469a6321a8eSPavel Labath m_mem_region_cache.size()); 147097206d57SZachary Turner return Status(); 1471a6f5795aSTamas Berghammer } 1472a6f5795aSTamas Berghammer 147315930862SPavel Labath auto BufferOrError = getProcFile(GetID(), "maps"); 147415930862SPavel Labath if (!BufferOrError) { 147515930862SPavel Labath m_supports_mem_region = LazyBool::eLazyBoolNo; 147615930862SPavel Labath return BufferOrError.getError(); 147715930862SPavel Labath } 147815930862SPavel Labath StringRef Rest = BufferOrError.get()->getBuffer(); 147915930862SPavel Labath while (! Rest.empty()) { 148015930862SPavel Labath StringRef Line; 148115930862SPavel Labath std::tie(Line, Rest) = Rest.split('\n'); 1482a6f5795aSTamas Berghammer MemoryRegionInfo info; 148397206d57SZachary Turner const Status parse_error = 148497206d57SZachary Turner ParseMemoryRegionInfoFromProcMapsLine(Line, info); 148515930862SPavel Labath if (parse_error.Fail()) { 148615930862SPavel Labath LLDB_LOG(log, "failed to parse proc maps line '{0}': {1}", Line, 148715930862SPavel Labath parse_error); 148815930862SPavel Labath m_supports_mem_region = LazyBool::eLazyBoolNo; 148915930862SPavel Labath return parse_error; 149015930862SPavel Labath } 1491a6f5795aSTamas Berghammer m_mem_region_cache.emplace_back( 1492a6f5795aSTamas Berghammer info, FileSpec(info.GetName().GetCString(), true)); 1493a6f5795aSTamas Berghammer } 1494a6f5795aSTamas Berghammer 149515930862SPavel Labath if (m_mem_region_cache.empty()) { 1496a6f5795aSTamas Berghammer // No entries after attempting to read them. This shouldn't happen if 1497a6f5795aSTamas Berghammer // /proc/{pid}/maps is supported. Assume we don't support map entries 1498a6f5795aSTamas Berghammer // via procfs. 149915930862SPavel Labath m_supports_mem_region = LazyBool::eLazyBoolNo; 1500a6321a8eSPavel Labath LLDB_LOG(log, 1501a6321a8eSPavel Labath "failed to find any procfs maps entries, assuming no support " 1502a6321a8eSPavel Labath "for memory region metadata retrieval"); 150397206d57SZachary Turner return Status("not supported"); 1504a6f5795aSTamas Berghammer } 1505a6f5795aSTamas Berghammer 1506a6321a8eSPavel Labath LLDB_LOG(log, "read {0} memory region entries from /proc/{1}/maps", 1507a6321a8eSPavel Labath m_mem_region_cache.size(), GetID()); 1508a6f5795aSTamas Berghammer 1509a6f5795aSTamas Berghammer // We support memory retrieval, remember that. 1510a6f5795aSTamas Berghammer m_supports_mem_region = LazyBool::eLazyBoolYes; 151197206d57SZachary Turner return Status(); 1512a6f5795aSTamas Berghammer } 1513a6f5795aSTamas Berghammer 1514b9c1b51eSKate Stone void NativeProcessLinux::DoStopIDBumped(uint32_t newBumpId) { 1515a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1516a6321a8eSPavel Labath LLDB_LOG(log, "newBumpId={0}", newBumpId); 1517a6321a8eSPavel Labath LLDB_LOG(log, "clearing {0} entries from memory region cache", 1518a6321a8eSPavel Labath m_mem_region_cache.size()); 1519af245d11STodd Fiala m_mem_region_cache.clear(); 1520af245d11STodd Fiala } 1521af245d11STodd Fiala 152297206d57SZachary Turner Status NativeProcessLinux::AllocateMemory(size_t size, uint32_t permissions, 1523b9c1b51eSKate Stone lldb::addr_t &addr) { 1524af245d11STodd Fiala // FIXME implementing this requires the equivalent of 1525af245d11STodd Fiala // InferiorCallPOSIX::InferiorCallMmap, which depends on 1526af245d11STodd Fiala // functional ThreadPlans working with Native*Protocol. 1527af245d11STodd Fiala #if 1 152897206d57SZachary Turner return Status("not implemented yet"); 1529af245d11STodd Fiala #else 1530af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 1531af245d11STodd Fiala 1532af245d11STodd Fiala unsigned prot = 0; 1533af245d11STodd Fiala if (permissions & lldb::ePermissionsReadable) 1534af245d11STodd Fiala prot |= eMmapProtRead; 1535af245d11STodd Fiala if (permissions & lldb::ePermissionsWritable) 1536af245d11STodd Fiala prot |= eMmapProtWrite; 1537af245d11STodd Fiala if (permissions & lldb::ePermissionsExecutable) 1538af245d11STodd Fiala prot |= eMmapProtExec; 1539af245d11STodd Fiala 1540af245d11STodd Fiala // TODO implement this directly in NativeProcessLinux 1541af245d11STodd Fiala // (and lift to NativeProcessPOSIX if/when that class is 1542af245d11STodd Fiala // refactored out). 1543af245d11STodd Fiala if (InferiorCallMmap(this, addr, 0, size, prot, 1544af245d11STodd Fiala eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { 1545af245d11STodd Fiala m_addr_to_mmap_size[addr] = size; 154697206d57SZachary Turner return Status(); 1547af245d11STodd Fiala } else { 1548af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 154997206d57SZachary Turner return Status("unable to allocate %" PRIu64 1550b9c1b51eSKate Stone " bytes of memory with permissions %s", 1551b9c1b51eSKate Stone size, GetPermissionsAsCString(permissions)); 1552af245d11STodd Fiala } 1553af245d11STodd Fiala #endif 1554af245d11STodd Fiala } 1555af245d11STodd Fiala 155697206d57SZachary Turner Status NativeProcessLinux::DeallocateMemory(lldb::addr_t addr) { 1557af245d11STodd Fiala // FIXME see comments in AllocateMemory - required lower-level 1558af245d11STodd Fiala // bits not in place yet (ThreadPlans) 155997206d57SZachary Turner return Status("not implemented"); 1560af245d11STodd Fiala } 1561af245d11STodd Fiala 1562b9c1b51eSKate Stone lldb::addr_t NativeProcessLinux::GetSharedLibraryInfoAddress() { 1563af245d11STodd Fiala // punt on this for now 1564af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 1565af245d11STodd Fiala } 1566af245d11STodd Fiala 1567b9c1b51eSKate Stone size_t NativeProcessLinux::UpdateThreads() { 1568af245d11STodd Fiala // The NativeProcessLinux monitoring threads are always up to date 1569af245d11STodd Fiala // with respect to thread state and they keep the thread list 1570af245d11STodd Fiala // populated properly. All this method needs to do is return the 1571af245d11STodd Fiala // thread count. 1572af245d11STodd Fiala return m_threads.size(); 1573af245d11STodd Fiala } 1574af245d11STodd Fiala 1575b9c1b51eSKate Stone bool NativeProcessLinux::GetArchitecture(ArchSpec &arch) const { 1576af245d11STodd Fiala arch = m_arch; 1577af245d11STodd Fiala return true; 1578af245d11STodd Fiala } 1579af245d11STodd Fiala 158097206d57SZachary Turner Status NativeProcessLinux::GetSoftwareBreakpointPCOffset( 1581b9c1b51eSKate Stone uint32_t &actual_opcode_size) { 1582af245d11STodd Fiala // FIXME put this behind a breakpoint protocol class that can be 1583af245d11STodd Fiala // set per architecture. Need ARM, MIPS support here. 1584af245d11STodd Fiala static const uint8_t g_i386_opcode[] = {0xCC}; 1585bb00d0b6SUlrich Weigand static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; 1586af245d11STodd Fiala 1587b9c1b51eSKate Stone switch (m_arch.GetMachine()) { 1588af245d11STodd Fiala case llvm::Triple::x86: 1589af245d11STodd Fiala case llvm::Triple::x86_64: 1590af245d11STodd Fiala actual_opcode_size = static_cast<uint32_t>(sizeof(g_i386_opcode)); 159197206d57SZachary Turner return Status(); 1592af245d11STodd Fiala 1593bb00d0b6SUlrich Weigand case llvm::Triple::systemz: 1594bb00d0b6SUlrich Weigand actual_opcode_size = static_cast<uint32_t>(sizeof(g_s390x_opcode)); 159597206d57SZachary Turner return Status(); 1596bb00d0b6SUlrich Weigand 1597ff7fd900STamas Berghammer case llvm::Triple::arm: 1598ff7fd900STamas Berghammer case llvm::Triple::aarch64: 1599e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64: 1600e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64el: 1601ce815e45SSagar Thakur case llvm::Triple::mips: 1602ce815e45SSagar Thakur case llvm::Triple::mipsel: 1603ff7fd900STamas Berghammer // On these architectures the PC don't get updated for breakpoint hits 1604c60c9452SJaydeep Patil actual_opcode_size = 0; 160597206d57SZachary Turner return Status(); 1606e8659b5dSMohit K. Bhakkad 1607af245d11STodd Fiala default: 1608af245d11STodd Fiala assert(false && "CPU type not supported!"); 160997206d57SZachary Turner return Status("CPU type not supported"); 1610af245d11STodd Fiala } 1611af245d11STodd Fiala } 1612af245d11STodd Fiala 161397206d57SZachary Turner Status NativeProcessLinux::SetBreakpoint(lldb::addr_t addr, uint32_t size, 1614b9c1b51eSKate Stone bool hardware) { 1615af245d11STodd Fiala if (hardware) 1616d5ffbad2SOmair Javaid return SetHardwareBreakpoint(addr, size); 1617af245d11STodd Fiala else 1618af245d11STodd Fiala return SetSoftwareBreakpoint(addr, size); 1619af245d11STodd Fiala } 1620af245d11STodd Fiala 162197206d57SZachary Turner Status NativeProcessLinux::RemoveBreakpoint(lldb::addr_t addr, bool hardware) { 1622d5ffbad2SOmair Javaid if (hardware) 1623d5ffbad2SOmair Javaid return RemoveHardwareBreakpoint(addr); 1624d5ffbad2SOmair Javaid else 1625d5ffbad2SOmair Javaid return NativeProcessProtocol::RemoveBreakpoint(addr); 1626d5ffbad2SOmair Javaid } 1627d5ffbad2SOmair Javaid 162897206d57SZachary Turner Status NativeProcessLinux::GetSoftwareBreakpointTrapOpcode( 1629b9c1b51eSKate Stone size_t trap_opcode_size_hint, size_t &actual_opcode_size, 1630b9c1b51eSKate Stone const uint8_t *&trap_opcode_bytes) { 163163c8be95STamas Berghammer // FIXME put this behind a breakpoint protocol class that can be set per 163263c8be95STamas Berghammer // architecture. Need MIPS support here. 16332afc5966STodd Fiala static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4}; 1634be379e15STamas Berghammer // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the 1635be379e15STamas Berghammer // linux kernel does otherwise. 1636be379e15STamas Berghammer static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7}; 1637af245d11STodd Fiala static const uint8_t g_i386_opcode[] = {0xCC}; 16383df471c3SMohit K. Bhakkad static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d}; 16392c2acf96SMohit K. Bhakkad static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00}; 1640bb00d0b6SUlrich Weigand static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; 1641be379e15STamas Berghammer static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde}; 1642af245d11STodd Fiala 1643b9c1b51eSKate Stone switch (m_arch.GetMachine()) { 16442afc5966STodd Fiala case llvm::Triple::aarch64: 16452afc5966STodd Fiala trap_opcode_bytes = g_aarch64_opcode; 16462afc5966STodd Fiala actual_opcode_size = sizeof(g_aarch64_opcode); 164797206d57SZachary Turner return Status(); 16482afc5966STodd Fiala 164963c8be95STamas Berghammer case llvm::Triple::arm: 1650b9c1b51eSKate Stone switch (trap_opcode_size_hint) { 165163c8be95STamas Berghammer case 2: 165263c8be95STamas Berghammer trap_opcode_bytes = g_thumb_breakpoint_opcode; 165363c8be95STamas Berghammer actual_opcode_size = sizeof(g_thumb_breakpoint_opcode); 165497206d57SZachary Turner return Status(); 165563c8be95STamas Berghammer case 4: 165663c8be95STamas Berghammer trap_opcode_bytes = g_arm_breakpoint_opcode; 165763c8be95STamas Berghammer actual_opcode_size = sizeof(g_arm_breakpoint_opcode); 165897206d57SZachary Turner return Status(); 165963c8be95STamas Berghammer default: 166063c8be95STamas Berghammer assert(false && "Unrecognised trap opcode size hint!"); 166197206d57SZachary Turner return Status("Unrecognised trap opcode size hint!"); 166263c8be95STamas Berghammer } 166363c8be95STamas Berghammer 1664af245d11STodd Fiala case llvm::Triple::x86: 1665af245d11STodd Fiala case llvm::Triple::x86_64: 1666af245d11STodd Fiala trap_opcode_bytes = g_i386_opcode; 1667af245d11STodd Fiala actual_opcode_size = sizeof(g_i386_opcode); 166897206d57SZachary Turner return Status(); 1669af245d11STodd Fiala 1670ce815e45SSagar Thakur case llvm::Triple::mips: 16713df471c3SMohit K. Bhakkad case llvm::Triple::mips64: 16723df471c3SMohit K. Bhakkad trap_opcode_bytes = g_mips64_opcode; 16733df471c3SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64_opcode); 167497206d57SZachary Turner return Status(); 16753df471c3SMohit K. Bhakkad 1676ce815e45SSagar Thakur case llvm::Triple::mipsel: 16772c2acf96SMohit K. Bhakkad case llvm::Triple::mips64el: 16782c2acf96SMohit K. Bhakkad trap_opcode_bytes = g_mips64el_opcode; 16792c2acf96SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64el_opcode); 168097206d57SZachary Turner return Status(); 16812c2acf96SMohit K. Bhakkad 1682bb00d0b6SUlrich Weigand case llvm::Triple::systemz: 1683bb00d0b6SUlrich Weigand trap_opcode_bytes = g_s390x_opcode; 1684bb00d0b6SUlrich Weigand actual_opcode_size = sizeof(g_s390x_opcode); 168597206d57SZachary Turner return Status(); 1686bb00d0b6SUlrich Weigand 1687af245d11STodd Fiala default: 1688af245d11STodd Fiala assert(false && "CPU type not supported!"); 168997206d57SZachary Turner return Status("CPU type not supported"); 1690af245d11STodd Fiala } 1691af245d11STodd Fiala } 1692af245d11STodd Fiala 1693af245d11STodd Fiala #if 0 1694af245d11STodd Fiala ProcessMessage::CrashReason 1695af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info) 1696af245d11STodd Fiala { 1697af245d11STodd Fiala ProcessMessage::CrashReason reason; 1698af245d11STodd Fiala assert(info->si_signo == SIGSEGV); 1699af245d11STodd Fiala 1700af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1701af245d11STodd Fiala 1702af245d11STodd Fiala switch (info->si_code) 1703af245d11STodd Fiala { 1704af245d11STodd Fiala default: 1705af245d11STodd Fiala assert(false && "unexpected si_code for SIGSEGV"); 1706af245d11STodd Fiala break; 1707af245d11STodd Fiala case SI_KERNEL: 1708af245d11STodd Fiala // Linux will occasionally send spurious SI_KERNEL codes. 1709af245d11STodd Fiala // (this is poorly documented in sigaction) 1710af245d11STodd Fiala // One way to get this is via unaligned SIMD loads. 1711af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; // for lack of anything better 1712af245d11STodd Fiala break; 1713af245d11STodd Fiala case SEGV_MAPERR: 1714af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; 1715af245d11STodd Fiala break; 1716af245d11STodd Fiala case SEGV_ACCERR: 1717af245d11STodd Fiala reason = ProcessMessage::ePrivilegedAddress; 1718af245d11STodd Fiala break; 1719af245d11STodd Fiala } 1720af245d11STodd Fiala 1721af245d11STodd Fiala return reason; 1722af245d11STodd Fiala } 1723af245d11STodd Fiala #endif 1724af245d11STodd Fiala 1725af245d11STodd Fiala #if 0 1726af245d11STodd Fiala ProcessMessage::CrashReason 1727af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info) 1728af245d11STodd Fiala { 1729af245d11STodd Fiala ProcessMessage::CrashReason reason; 1730af245d11STodd Fiala assert(info->si_signo == SIGILL); 1731af245d11STodd Fiala 1732af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1733af245d11STodd Fiala 1734af245d11STodd Fiala switch (info->si_code) 1735af245d11STodd Fiala { 1736af245d11STodd Fiala default: 1737af245d11STodd Fiala assert(false && "unexpected si_code for SIGILL"); 1738af245d11STodd Fiala break; 1739af245d11STodd Fiala case ILL_ILLOPC: 1740af245d11STodd Fiala reason = ProcessMessage::eIllegalOpcode; 1741af245d11STodd Fiala break; 1742af245d11STodd Fiala case ILL_ILLOPN: 1743af245d11STodd Fiala reason = ProcessMessage::eIllegalOperand; 1744af245d11STodd Fiala break; 1745af245d11STodd Fiala case ILL_ILLADR: 1746af245d11STodd Fiala reason = ProcessMessage::eIllegalAddressingMode; 1747af245d11STodd Fiala break; 1748af245d11STodd Fiala case ILL_ILLTRP: 1749af245d11STodd Fiala reason = ProcessMessage::eIllegalTrap; 1750af245d11STodd Fiala break; 1751af245d11STodd Fiala case ILL_PRVOPC: 1752af245d11STodd Fiala reason = ProcessMessage::ePrivilegedOpcode; 1753af245d11STodd Fiala break; 1754af245d11STodd Fiala case ILL_PRVREG: 1755af245d11STodd Fiala reason = ProcessMessage::ePrivilegedRegister; 1756af245d11STodd Fiala break; 1757af245d11STodd Fiala case ILL_COPROC: 1758af245d11STodd Fiala reason = ProcessMessage::eCoprocessorError; 1759af245d11STodd Fiala break; 1760af245d11STodd Fiala case ILL_BADSTK: 1761af245d11STodd Fiala reason = ProcessMessage::eInternalStackError; 1762af245d11STodd Fiala break; 1763af245d11STodd Fiala } 1764af245d11STodd Fiala 1765af245d11STodd Fiala return reason; 1766af245d11STodd Fiala } 1767af245d11STodd Fiala #endif 1768af245d11STodd Fiala 1769af245d11STodd Fiala #if 0 1770af245d11STodd Fiala ProcessMessage::CrashReason 1771af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info) 1772af245d11STodd Fiala { 1773af245d11STodd Fiala ProcessMessage::CrashReason reason; 1774af245d11STodd Fiala assert(info->si_signo == SIGFPE); 1775af245d11STodd Fiala 1776af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1777af245d11STodd Fiala 1778af245d11STodd Fiala switch (info->si_code) 1779af245d11STodd Fiala { 1780af245d11STodd Fiala default: 1781af245d11STodd Fiala assert(false && "unexpected si_code for SIGFPE"); 1782af245d11STodd Fiala break; 1783af245d11STodd Fiala case FPE_INTDIV: 1784af245d11STodd Fiala reason = ProcessMessage::eIntegerDivideByZero; 1785af245d11STodd Fiala break; 1786af245d11STodd Fiala case FPE_INTOVF: 1787af245d11STodd Fiala reason = ProcessMessage::eIntegerOverflow; 1788af245d11STodd Fiala break; 1789af245d11STodd Fiala case FPE_FLTDIV: 1790af245d11STodd Fiala reason = ProcessMessage::eFloatDivideByZero; 1791af245d11STodd Fiala break; 1792af245d11STodd Fiala case FPE_FLTOVF: 1793af245d11STodd Fiala reason = ProcessMessage::eFloatOverflow; 1794af245d11STodd Fiala break; 1795af245d11STodd Fiala case FPE_FLTUND: 1796af245d11STodd Fiala reason = ProcessMessage::eFloatUnderflow; 1797af245d11STodd Fiala break; 1798af245d11STodd Fiala case FPE_FLTRES: 1799af245d11STodd Fiala reason = ProcessMessage::eFloatInexactResult; 1800af245d11STodd Fiala break; 1801af245d11STodd Fiala case FPE_FLTINV: 1802af245d11STodd Fiala reason = ProcessMessage::eFloatInvalidOperation; 1803af245d11STodd Fiala break; 1804af245d11STodd Fiala case FPE_FLTSUB: 1805af245d11STodd Fiala reason = ProcessMessage::eFloatSubscriptRange; 1806af245d11STodd Fiala break; 1807af245d11STodd Fiala } 1808af245d11STodd Fiala 1809af245d11STodd Fiala return reason; 1810af245d11STodd Fiala } 1811af245d11STodd Fiala #endif 1812af245d11STodd Fiala 1813af245d11STodd Fiala #if 0 1814af245d11STodd Fiala ProcessMessage::CrashReason 1815af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info) 1816af245d11STodd Fiala { 1817af245d11STodd Fiala ProcessMessage::CrashReason reason; 1818af245d11STodd Fiala assert(info->si_signo == SIGBUS); 1819af245d11STodd Fiala 1820af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1821af245d11STodd Fiala 1822af245d11STodd Fiala switch (info->si_code) 1823af245d11STodd Fiala { 1824af245d11STodd Fiala default: 1825af245d11STodd Fiala assert(false && "unexpected si_code for SIGBUS"); 1826af245d11STodd Fiala break; 1827af245d11STodd Fiala case BUS_ADRALN: 1828af245d11STodd Fiala reason = ProcessMessage::eIllegalAlignment; 1829af245d11STodd Fiala break; 1830af245d11STodd Fiala case BUS_ADRERR: 1831af245d11STodd Fiala reason = ProcessMessage::eIllegalAddress; 1832af245d11STodd Fiala break; 1833af245d11STodd Fiala case BUS_OBJERR: 1834af245d11STodd Fiala reason = ProcessMessage::eHardwareError; 1835af245d11STodd Fiala break; 1836af245d11STodd Fiala } 1837af245d11STodd Fiala 1838af245d11STodd Fiala return reason; 1839af245d11STodd Fiala } 1840af245d11STodd Fiala #endif 1841af245d11STodd Fiala 184297206d57SZachary Turner Status NativeProcessLinux::ReadMemory(lldb::addr_t addr, void *buf, size_t size, 1843b9c1b51eSKate Stone size_t &bytes_read) { 1844df7c6995SPavel Labath if (ProcessVmReadvSupported()) { 1845b9c1b51eSKate Stone // The process_vm_readv path is about 50 times faster than ptrace api. We 1846b9c1b51eSKate Stone // want to use 1847df7c6995SPavel Labath // this syscall if it is supported. 1848df7c6995SPavel Labath 1849df7c6995SPavel Labath const ::pid_t pid = GetID(); 1850df7c6995SPavel Labath 1851df7c6995SPavel Labath struct iovec local_iov, remote_iov; 1852df7c6995SPavel Labath local_iov.iov_base = buf; 1853df7c6995SPavel Labath local_iov.iov_len = size; 1854df7c6995SPavel Labath remote_iov.iov_base = reinterpret_cast<void *>(addr); 1855df7c6995SPavel Labath remote_iov.iov_len = size; 1856df7c6995SPavel Labath 1857df7c6995SPavel Labath bytes_read = process_vm_readv(pid, &local_iov, 1, &remote_iov, 1, 0); 1858df7c6995SPavel Labath const bool success = bytes_read == size; 1859df7c6995SPavel Labath 1860a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1861a6321a8eSPavel Labath LLDB_LOG(log, 1862a6321a8eSPavel Labath "using process_vm_readv to read {0} bytes from inferior " 1863a6321a8eSPavel Labath "address {1:x}: {2}", 186410c41f37SPavel Labath size, addr, success ? "Success" : llvm::sys::StrError(errno)); 1865df7c6995SPavel Labath 1866df7c6995SPavel Labath if (success) 186797206d57SZachary Turner return Status(); 1868a6321a8eSPavel Labath // else the call failed for some reason, let's retry the read using ptrace 1869b9c1b51eSKate Stone // api. 1870df7c6995SPavel Labath } 1871df7c6995SPavel Labath 187219cbe96aSPavel Labath unsigned char *dst = static_cast<unsigned char *>(buf); 187319cbe96aSPavel Labath size_t remainder; 187419cbe96aSPavel Labath long data; 187519cbe96aSPavel Labath 1876a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 1877a6321a8eSPavel Labath LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 187819cbe96aSPavel Labath 1879b9c1b51eSKate Stone for (bytes_read = 0; bytes_read < size; bytes_read += remainder) { 188097206d57SZachary Turner Status error = NativeProcessLinux::PtraceWrapper( 1881b9c1b51eSKate Stone PTRACE_PEEKDATA, GetID(), (void *)addr, nullptr, 0, &data); 1882a6321a8eSPavel Labath if (error.Fail()) 188319cbe96aSPavel Labath return error; 188419cbe96aSPavel Labath 188519cbe96aSPavel Labath remainder = size - bytes_read; 188619cbe96aSPavel Labath remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; 188719cbe96aSPavel Labath 188819cbe96aSPavel Labath // Copy the data into our buffer 1889f6ef187bSMohit K. Bhakkad memcpy(dst, &data, remainder); 189019cbe96aSPavel Labath 1891a6321a8eSPavel Labath LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data); 189219cbe96aSPavel Labath addr += k_ptrace_word_size; 189319cbe96aSPavel Labath dst += k_ptrace_word_size; 189419cbe96aSPavel Labath } 189597206d57SZachary Turner return Status(); 1896af245d11STodd Fiala } 1897af245d11STodd Fiala 189897206d57SZachary Turner Status NativeProcessLinux::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, 1899b9c1b51eSKate Stone size_t size, 1900b9c1b51eSKate Stone size_t &bytes_read) { 190197206d57SZachary Turner Status error = ReadMemory(addr, buf, size, bytes_read); 1902b9c1b51eSKate Stone if (error.Fail()) 1903b9c1b51eSKate Stone return error; 19043eb4b458SChaoren Lin return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); 19053eb4b458SChaoren Lin } 19063eb4b458SChaoren Lin 190797206d57SZachary Turner Status NativeProcessLinux::WriteMemory(lldb::addr_t addr, const void *buf, 1908b9c1b51eSKate Stone size_t size, size_t &bytes_written) { 190919cbe96aSPavel Labath const unsigned char *src = static_cast<const unsigned char *>(buf); 191019cbe96aSPavel Labath size_t remainder; 191197206d57SZachary Turner Status error; 191219cbe96aSPavel Labath 1913a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 1914a6321a8eSPavel Labath LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 191519cbe96aSPavel Labath 1916b9c1b51eSKate Stone for (bytes_written = 0; bytes_written < size; bytes_written += remainder) { 191719cbe96aSPavel Labath remainder = size - bytes_written; 191819cbe96aSPavel Labath remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; 191919cbe96aSPavel Labath 1920b9c1b51eSKate Stone if (remainder == k_ptrace_word_size) { 192119cbe96aSPavel Labath unsigned long data = 0; 1922f6ef187bSMohit K. Bhakkad memcpy(&data, src, k_ptrace_word_size); 192319cbe96aSPavel Labath 1924a6321a8eSPavel Labath LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data); 1925b9c1b51eSKate Stone error = NativeProcessLinux::PtraceWrapper(PTRACE_POKEDATA, GetID(), 1926b9c1b51eSKate Stone (void *)addr, (void *)data); 1927a6321a8eSPavel Labath if (error.Fail()) 192819cbe96aSPavel Labath return error; 1929b9c1b51eSKate Stone } else { 193019cbe96aSPavel Labath unsigned char buff[8]; 193119cbe96aSPavel Labath size_t bytes_read; 193219cbe96aSPavel Labath error = ReadMemory(addr, buff, k_ptrace_word_size, bytes_read); 1933a6321a8eSPavel Labath if (error.Fail()) 193419cbe96aSPavel Labath return error; 193519cbe96aSPavel Labath 193619cbe96aSPavel Labath memcpy(buff, src, remainder); 193719cbe96aSPavel Labath 193819cbe96aSPavel Labath size_t bytes_written_rec; 193919cbe96aSPavel Labath error = WriteMemory(addr, buff, k_ptrace_word_size, bytes_written_rec); 1940a6321a8eSPavel Labath if (error.Fail()) 194119cbe96aSPavel Labath return error; 194219cbe96aSPavel Labath 1943a6321a8eSPavel Labath LLDB_LOG(log, "[{0:x}]:{1:x} ({2:x})", addr, *(const unsigned long *)src, 1944b9c1b51eSKate Stone *(unsigned long *)buff); 194519cbe96aSPavel Labath } 194619cbe96aSPavel Labath 194719cbe96aSPavel Labath addr += k_ptrace_word_size; 194819cbe96aSPavel Labath src += k_ptrace_word_size; 194919cbe96aSPavel Labath } 195019cbe96aSPavel Labath return error; 1951af245d11STodd Fiala } 1952af245d11STodd Fiala 195397206d57SZachary Turner Status NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) { 195419cbe96aSPavel Labath return PtraceWrapper(PTRACE_GETSIGINFO, tid, nullptr, siginfo); 1955af245d11STodd Fiala } 1956af245d11STodd Fiala 195797206d57SZachary Turner Status NativeProcessLinux::GetEventMessage(lldb::tid_t tid, 1958b9c1b51eSKate Stone unsigned long *message) { 195919cbe96aSPavel Labath return PtraceWrapper(PTRACE_GETEVENTMSG, tid, nullptr, message); 1960af245d11STodd Fiala } 1961af245d11STodd Fiala 196297206d57SZachary Turner Status NativeProcessLinux::Detach(lldb::tid_t tid) { 196397ccc294SChaoren Lin if (tid == LLDB_INVALID_THREAD_ID) 196497206d57SZachary Turner return Status(); 196597ccc294SChaoren Lin 196619cbe96aSPavel Labath return PtraceWrapper(PTRACE_DETACH, tid); 1967af245d11STodd Fiala } 1968af245d11STodd Fiala 1969b9c1b51eSKate Stone bool NativeProcessLinux::HasThreadNoLock(lldb::tid_t thread_id) { 1970b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 1971af245d11STodd Fiala assert(thread_sp && "thread list should not contain NULL threads"); 1972b9c1b51eSKate Stone if (thread_sp->GetID() == thread_id) { 1973af245d11STodd Fiala // We have this thread. 1974af245d11STodd Fiala return true; 1975af245d11STodd Fiala } 1976af245d11STodd Fiala } 1977af245d11STodd Fiala 1978af245d11STodd Fiala // We don't have this thread. 1979af245d11STodd Fiala return false; 1980af245d11STodd Fiala } 1981af245d11STodd Fiala 1982b9c1b51eSKate Stone bool NativeProcessLinux::StopTrackingThread(lldb::tid_t thread_id) { 1983a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 1984a6321a8eSPavel Labath LLDB_LOG(log, "tid: {0})", thread_id); 19851dbc6c9cSPavel Labath 19861dbc6c9cSPavel Labath bool found = false; 1987b9c1b51eSKate Stone for (auto it = m_threads.begin(); it != m_threads.end(); ++it) { 1988b9c1b51eSKate Stone if (*it && ((*it)->GetID() == thread_id)) { 1989af245d11STodd Fiala m_threads.erase(it); 19901dbc6c9cSPavel Labath found = true; 19911dbc6c9cSPavel Labath break; 1992af245d11STodd Fiala } 1993af245d11STodd Fiala } 1994af245d11STodd Fiala 199599e37695SRavitheja Addepally if (found) 199699e37695SRavitheja Addepally StopTracingForThread(thread_id); 19979eb1ecb9SPavel Labath SignalIfAllThreadsStopped(); 19981dbc6c9cSPavel Labath return found; 1999af245d11STodd Fiala } 2000af245d11STodd Fiala 2001b9c1b51eSKate Stone NativeThreadLinuxSP NativeProcessLinux::AddThread(lldb::tid_t thread_id) { 2002a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); 2003a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id); 2004af245d11STodd Fiala 2005b9c1b51eSKate Stone assert(!HasThreadNoLock(thread_id) && 2006b9c1b51eSKate Stone "attempted to add a thread by id that already exists"); 2007af245d11STodd Fiala 2008af245d11STodd Fiala // If this is the first thread, save it as the current thread 2009af245d11STodd Fiala if (m_threads.empty()) 2010af245d11STodd Fiala SetCurrentThreadID(thread_id); 2011af245d11STodd Fiala 2012f9077782SPavel Labath auto thread_sp = std::make_shared<NativeThreadLinux>(this, thread_id); 2013af245d11STodd Fiala m_threads.push_back(thread_sp); 201499e37695SRavitheja Addepally 201599e37695SRavitheja Addepally if (m_pt_proces_trace_id != LLDB_INVALID_UID) { 201699e37695SRavitheja Addepally auto traceMonitor = ProcessorTraceMonitor::Create( 201799e37695SRavitheja Addepally GetID(), thread_id, m_pt_process_trace_config, true); 201899e37695SRavitheja Addepally if (traceMonitor) { 201999e37695SRavitheja Addepally m_pt_traced_thread_group.insert(thread_id); 202099e37695SRavitheja Addepally m_processor_trace_monitor.insert( 202199e37695SRavitheja Addepally std::make_pair(thread_id, std::move(*traceMonitor))); 202299e37695SRavitheja Addepally } else { 202399e37695SRavitheja Addepally LLDB_LOG(log, "failed to start trace on thread {0}", thread_id); 202499e37695SRavitheja Addepally Status error(traceMonitor.takeError()); 202599e37695SRavitheja Addepally LLDB_LOG(log, "error {0}", error); 202699e37695SRavitheja Addepally } 202799e37695SRavitheja Addepally } 202899e37695SRavitheja Addepally 2029af245d11STodd Fiala return thread_sp; 2030af245d11STodd Fiala } 2031af245d11STodd Fiala 203297206d57SZachary Turner Status 203397206d57SZachary Turner NativeProcessLinux::FixupBreakpointPCAsNeeded(NativeThreadLinux &thread) { 2034a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS)); 2035af245d11STodd Fiala 203697206d57SZachary Turner Status error; 2037af245d11STodd Fiala 2038b9c1b51eSKate Stone // Find out the size of a breakpoint (might depend on where we are in the 2039b9c1b51eSKate Stone // code). 2040b9cc0c75SPavel Labath NativeRegisterContextSP context_sp = thread.GetRegisterContext(); 2041b9c1b51eSKate Stone if (!context_sp) { 2042af245d11STodd Fiala error.SetErrorString("cannot get a NativeRegisterContext for the thread"); 2043a6321a8eSPavel Labath LLDB_LOG(log, "failed: {0}", error); 2044af245d11STodd Fiala return error; 2045af245d11STodd Fiala } 2046af245d11STodd Fiala 2047af245d11STodd Fiala uint32_t breakpoint_size = 0; 2048b9cc0c75SPavel Labath error = GetSoftwareBreakpointPCOffset(breakpoint_size); 2049b9c1b51eSKate Stone if (error.Fail()) { 2050a6321a8eSPavel Labath LLDB_LOG(log, "GetBreakpointSize() failed: {0}", error); 2051af245d11STodd Fiala return error; 2052a6321a8eSPavel Labath } else 2053a6321a8eSPavel Labath LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size); 2054af245d11STodd Fiala 2055b9c1b51eSKate Stone // First try probing for a breakpoint at a software breakpoint location: PC - 2056b9c1b51eSKate Stone // breakpoint size. 2057b9c1b51eSKate Stone const lldb::addr_t initial_pc_addr = 2058b9c1b51eSKate Stone context_sp->GetPCfromBreakpointLocation(); 2059af245d11STodd Fiala lldb::addr_t breakpoint_addr = initial_pc_addr; 2060b9c1b51eSKate Stone if (breakpoint_size > 0) { 2061af245d11STodd Fiala // Do not allow breakpoint probe to wrap around. 20623eb4b458SChaoren Lin if (breakpoint_addr >= breakpoint_size) 20633eb4b458SChaoren Lin breakpoint_addr -= breakpoint_size; 2064af245d11STodd Fiala } 2065af245d11STodd Fiala 2066af245d11STodd Fiala // Check if we stopped because of a breakpoint. 2067af245d11STodd Fiala NativeBreakpointSP breakpoint_sp; 2068af245d11STodd Fiala error = m_breakpoint_list.GetBreakpoint(breakpoint_addr, breakpoint_sp); 2069b9c1b51eSKate Stone if (!error.Success() || !breakpoint_sp) { 2070af245d11STodd Fiala // We didn't find one at a software probe location. Nothing to do. 2071a6321a8eSPavel Labath LLDB_LOG(log, 2072a6321a8eSPavel Labath "pid {0} no lldb breakpoint found at current pc with " 2073a6321a8eSPavel Labath "adjustment: {1}", 2074a6321a8eSPavel Labath GetID(), breakpoint_addr); 207597206d57SZachary Turner return Status(); 2076af245d11STodd Fiala } 2077af245d11STodd Fiala 2078af245d11STodd Fiala // If the breakpoint is not a software breakpoint, nothing to do. 2079b9c1b51eSKate Stone if (!breakpoint_sp->IsSoftwareBreakpoint()) { 2080a6321a8eSPavel Labath LLDB_LOG( 2081a6321a8eSPavel Labath log, 2082a6321a8eSPavel Labath "pid {0} breakpoint found at {1:x}, not software, nothing to adjust", 2083a6321a8eSPavel Labath GetID(), breakpoint_addr); 208497206d57SZachary Turner return Status(); 2085af245d11STodd Fiala } 2086af245d11STodd Fiala 2087af245d11STodd Fiala // 2088af245d11STodd Fiala // We have a software breakpoint and need to adjust the PC. 2089af245d11STodd Fiala // 2090af245d11STodd Fiala 2091af245d11STodd Fiala // Sanity check. 2092b9c1b51eSKate Stone if (breakpoint_size == 0) { 2093af245d11STodd Fiala // Nothing to do! How did we get here? 2094a6321a8eSPavel Labath LLDB_LOG(log, 2095a6321a8eSPavel Labath "pid {0} breakpoint found at {1:x}, it is software, but the " 2096a6321a8eSPavel Labath "size is zero, nothing to do (unexpected)", 2097a6321a8eSPavel Labath GetID(), breakpoint_addr); 209897206d57SZachary Turner return Status(); 2099af245d11STodd Fiala } 2100af245d11STodd Fiala 2101af245d11STodd Fiala // Change the program counter. 2102a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(), 2103a6321a8eSPavel Labath thread.GetID(), initial_pc_addr, breakpoint_addr); 2104af245d11STodd Fiala 2105af245d11STodd Fiala error = context_sp->SetPC(breakpoint_addr); 2106b9c1b51eSKate Stone if (error.Fail()) { 2107a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(), 2108a6321a8eSPavel Labath thread.GetID(), error); 2109af245d11STodd Fiala return error; 2110af245d11STodd Fiala } 2111af245d11STodd Fiala 2112af245d11STodd Fiala return error; 2113af245d11STodd Fiala } 2114fa03ad2eSChaoren Lin 211597206d57SZachary Turner Status NativeProcessLinux::GetLoadedModuleFileSpec(const char *module_path, 2116b9c1b51eSKate Stone FileSpec &file_spec) { 211797206d57SZachary Turner Status error = PopulateMemoryRegionCache(); 2118a6f5795aSTamas Berghammer if (error.Fail()) 2119a6f5795aSTamas Berghammer return error; 2120a6f5795aSTamas Berghammer 21217cb18bf5STamas Berghammer FileSpec module_file_spec(module_path, true); 21227cb18bf5STamas Berghammer 21237cb18bf5STamas Berghammer file_spec.Clear(); 2124a6f5795aSTamas Berghammer for (const auto &it : m_mem_region_cache) { 2125a6f5795aSTamas Berghammer if (it.second.GetFilename() == module_file_spec.GetFilename()) { 2126a6f5795aSTamas Berghammer file_spec = it.second; 212797206d57SZachary Turner return Status(); 2128a6f5795aSTamas Berghammer } 2129a6f5795aSTamas Berghammer } 213097206d57SZachary Turner return Status("Module file (%s) not found in /proc/%" PRIu64 "/maps file!", 21317cb18bf5STamas Berghammer module_file_spec.GetFilename().AsCString(), GetID()); 21327cb18bf5STamas Berghammer } 2133c076559aSPavel Labath 213497206d57SZachary Turner Status NativeProcessLinux::GetFileLoadAddress(const llvm::StringRef &file_name, 2135b9c1b51eSKate Stone lldb::addr_t &load_addr) { 2136783bfc8cSTamas Berghammer load_addr = LLDB_INVALID_ADDRESS; 213797206d57SZachary Turner Status error = PopulateMemoryRegionCache(); 2138a6f5795aSTamas Berghammer if (error.Fail()) 2139783bfc8cSTamas Berghammer return error; 2140a6f5795aSTamas Berghammer 2141a6f5795aSTamas Berghammer FileSpec file(file_name, false); 2142a6f5795aSTamas Berghammer for (const auto &it : m_mem_region_cache) { 2143a6f5795aSTamas Berghammer if (it.second == file) { 2144a6f5795aSTamas Berghammer load_addr = it.first.GetRange().GetRangeBase(); 214597206d57SZachary Turner return Status(); 2146a6f5795aSTamas Berghammer } 2147a6f5795aSTamas Berghammer } 214897206d57SZachary Turner return Status("No load address found for specified file."); 2149783bfc8cSTamas Berghammer } 2150783bfc8cSTamas Berghammer 2151b9c1b51eSKate Stone NativeThreadLinuxSP NativeProcessLinux::GetThreadByID(lldb::tid_t tid) { 2152b9c1b51eSKate Stone return std::static_pointer_cast<NativeThreadLinux>( 2153b9c1b51eSKate Stone NativeProcessProtocol::GetThreadByID(tid)); 2154f9077782SPavel Labath } 2155f9077782SPavel Labath 215697206d57SZachary Turner Status NativeProcessLinux::ResumeThread(NativeThreadLinux &thread, 2157b9c1b51eSKate Stone lldb::StateType state, int signo) { 2158a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2159a6321a8eSPavel Labath LLDB_LOG(log, "tid: {0}", thread.GetID()); 2160c076559aSPavel Labath 2161c076559aSPavel Labath // Before we do the resume below, first check if we have a pending 2162108c325dSPavel Labath // stop notification that is currently waiting for 21630e1d729bSPavel Labath // all threads to stop. This is potentially a buggy situation since 2164c076559aSPavel Labath // we're ostensibly waiting for threads to stop before we send out the 2165c076559aSPavel Labath // pending notification, and here we are resuming one before we send 2166c076559aSPavel Labath // out the pending stop notification. 2167a6321a8eSPavel Labath if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) { 2168a6321a8eSPavel Labath LLDB_LOG(log, 2169a6321a8eSPavel Labath "about to resume tid {0} per explicit request but we have a " 2170a6321a8eSPavel Labath "pending stop notification (tid {1}) that is actively " 2171a6321a8eSPavel Labath "waiting for this thread to stop. Valid sequence of events?", 2172a6321a8eSPavel Labath thread.GetID(), m_pending_notification_tid); 2173c076559aSPavel Labath } 2174c076559aSPavel Labath 2175c076559aSPavel Labath // Request a resume. We expect this to be synchronous and the system 2176c076559aSPavel Labath // to reflect it is running after this completes. 2177b9c1b51eSKate Stone switch (state) { 2178b9c1b51eSKate Stone case eStateRunning: { 2179605b51b8SPavel Labath const auto resume_result = thread.Resume(signo); 21800e1d729bSPavel Labath if (resume_result.Success()) 21810e1d729bSPavel Labath SetState(eStateRunning, true); 21820e1d729bSPavel Labath return resume_result; 2183c076559aSPavel Labath } 2184b9c1b51eSKate Stone case eStateStepping: { 2185605b51b8SPavel Labath const auto step_result = thread.SingleStep(signo); 21860e1d729bSPavel Labath if (step_result.Success()) 21870e1d729bSPavel Labath SetState(eStateRunning, true); 21880e1d729bSPavel Labath return step_result; 21890e1d729bSPavel Labath } 21900e1d729bSPavel Labath default: 21918198db30SPavel Labath LLDB_LOG(log, "Unhandled state {0}.", state); 21920e1d729bSPavel Labath llvm_unreachable("Unhandled state for resume"); 21930e1d729bSPavel Labath } 2194c076559aSPavel Labath } 2195c076559aSPavel Labath 2196c076559aSPavel Labath //===----------------------------------------------------------------------===// 2197c076559aSPavel Labath 2198b9c1b51eSKate Stone void NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid) { 2199a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2200a6321a8eSPavel Labath LLDB_LOG(log, "about to process event: (triggering_tid: {0})", 2201a6321a8eSPavel Labath triggering_tid); 2202c076559aSPavel Labath 22030e1d729bSPavel Labath m_pending_notification_tid = triggering_tid; 22040e1d729bSPavel Labath 22050e1d729bSPavel Labath // Request a stop for all the thread stops that need to be stopped 22060e1d729bSPavel Labath // and are not already known to be stopped. 2207b9c1b51eSKate Stone for (const auto &thread_sp : m_threads) { 22080e1d729bSPavel Labath if (StateIsRunningState(thread_sp->GetState())) 22090e1d729bSPavel Labath static_pointer_cast<NativeThreadLinux>(thread_sp)->RequestStop(); 22100e1d729bSPavel Labath } 22110e1d729bSPavel Labath 22120e1d729bSPavel Labath SignalIfAllThreadsStopped(); 2213a6321a8eSPavel Labath LLDB_LOG(log, "event processing done"); 2214c076559aSPavel Labath } 2215c076559aSPavel Labath 2216b9c1b51eSKate Stone void NativeProcessLinux::SignalIfAllThreadsStopped() { 22170e1d729bSPavel Labath if (m_pending_notification_tid == LLDB_INVALID_THREAD_ID) 22180e1d729bSPavel Labath return; // No pending notification. Nothing to do. 22190e1d729bSPavel Labath 2220b9c1b51eSKate Stone for (const auto &thread_sp : m_threads) { 22210e1d729bSPavel Labath if (StateIsRunningState(thread_sp->GetState())) 22220e1d729bSPavel Labath return; // Some threads are still running. Don't signal yet. 22230e1d729bSPavel Labath } 22240e1d729bSPavel Labath 22250e1d729bSPavel Labath // We have a pending notification and all threads have stopped. 2226b9c1b51eSKate Stone Log *log( 2227b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 22289eb1ecb9SPavel Labath 2229b9c1b51eSKate Stone // Clear any temporary breakpoints we used to implement software single 2230b9c1b51eSKate Stone // stepping. 2231b9c1b51eSKate Stone for (const auto &thread_info : m_threads_stepping_with_breakpoint) { 223297206d57SZachary Turner Status error = RemoveBreakpoint(thread_info.second); 22339eb1ecb9SPavel Labath if (error.Fail()) 2234a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0} remove stepping breakpoint: {1}", 2235a6321a8eSPavel Labath thread_info.first, error); 22369eb1ecb9SPavel Labath } 22379eb1ecb9SPavel Labath m_threads_stepping_with_breakpoint.clear(); 22389eb1ecb9SPavel Labath 22399eb1ecb9SPavel Labath // Notify the delegate about the stop 22400e1d729bSPavel Labath SetCurrentThreadID(m_pending_notification_tid); 2241ed89c7feSPavel Labath SetState(StateType::eStateStopped, true); 22420e1d729bSPavel Labath m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 2243c076559aSPavel Labath } 2244c076559aSPavel Labath 2245b9c1b51eSKate Stone void NativeProcessLinux::ThreadWasCreated(NativeThreadLinux &thread) { 2246a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2247a6321a8eSPavel Labath LLDB_LOG(log, "tid: {0}", thread.GetID()); 22481dbc6c9cSPavel Labath 2249b9c1b51eSKate Stone if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID && 2250b9c1b51eSKate Stone StateIsRunningState(thread.GetState())) { 2251b9c1b51eSKate Stone // We will need to wait for this new thread to stop as well before firing 2252b9c1b51eSKate Stone // the 2253c076559aSPavel Labath // notification. 2254f9077782SPavel Labath thread.RequestStop(); 2255c076559aSPavel Labath } 2256c076559aSPavel Labath } 2257068f8a7eSTamas Berghammer 2258b9c1b51eSKate Stone void NativeProcessLinux::SigchldHandler() { 2259a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 226019cbe96aSPavel Labath // Process all pending waitpid notifications. 2261b9c1b51eSKate Stone while (true) { 226219cbe96aSPavel Labath int status = -1; 2263c1a6b128SPavel Labath ::pid_t wait_pid = llvm::sys::RetryAfterSignal(-1, ::waitpid, -1, &status, 2264c1a6b128SPavel Labath __WALL | __WNOTHREAD | WNOHANG); 226519cbe96aSPavel Labath 226619cbe96aSPavel Labath if (wait_pid == 0) 226719cbe96aSPavel Labath break; // We are done. 226819cbe96aSPavel Labath 2269b9c1b51eSKate Stone if (wait_pid == -1) { 227097206d57SZachary Turner Status error(errno, eErrorTypePOSIX); 2271a6321a8eSPavel Labath LLDB_LOG(log, "waitpid (-1, &status, _) failed: {0}", error); 227219cbe96aSPavel Labath break; 227319cbe96aSPavel Labath } 227419cbe96aSPavel Labath 22753508fc8cSPavel Labath WaitStatus wait_status = WaitStatus::Decode(status); 22763508fc8cSPavel Labath bool exited = wait_status.type == WaitStatus::Exit || 22773508fc8cSPavel Labath (wait_status.type == WaitStatus::Signal && 22783508fc8cSPavel Labath wait_pid == static_cast<::pid_t>(GetID())); 227919cbe96aSPavel Labath 22803508fc8cSPavel Labath LLDB_LOG( 22813508fc8cSPavel Labath log, 22823508fc8cSPavel Labath "waitpid (-1, &status, _) => pid = {0}, status = {1}, exited = {2}", 22833508fc8cSPavel Labath wait_pid, wait_status, exited); 228419cbe96aSPavel Labath 22853508fc8cSPavel Labath MonitorCallback(wait_pid, exited, wait_status); 228619cbe96aSPavel Labath } 2287068f8a7eSTamas Berghammer } 2288068f8a7eSTamas Berghammer 2289068f8a7eSTamas Berghammer // Wrapper for ptrace to catch errors and log calls. 2290b9c1b51eSKate Stone // Note that ptrace sets errno on error because -1 can be a valid result (i.e. 2291b9c1b51eSKate Stone // for PTRACE_PEEK*) 229297206d57SZachary Turner Status NativeProcessLinux::PtraceWrapper(int req, lldb::pid_t pid, void *addr, 2293b9c1b51eSKate Stone void *data, size_t data_size, 2294b9c1b51eSKate Stone long *result) { 229597206d57SZachary Turner Status error; 22964a9babb2SPavel Labath long int ret; 2297068f8a7eSTamas Berghammer 2298068f8a7eSTamas Berghammer Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 2299068f8a7eSTamas Berghammer 2300068f8a7eSTamas Berghammer PtraceDisplayBytes(req, data, data_size); 2301068f8a7eSTamas Berghammer 2302068f8a7eSTamas Berghammer errno = 0; 2303068f8a7eSTamas Berghammer if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) 2304b9c1b51eSKate Stone ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), 2305b9c1b51eSKate Stone *(unsigned int *)addr, data); 2306068f8a7eSTamas Berghammer else 2307b9c1b51eSKate Stone ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), 2308b9c1b51eSKate Stone addr, data); 2309068f8a7eSTamas Berghammer 23104a9babb2SPavel Labath if (ret == -1) 2311068f8a7eSTamas Berghammer error.SetErrorToErrno(); 2312068f8a7eSTamas Berghammer 23134a9babb2SPavel Labath if (result) 23144a9babb2SPavel Labath *result = ret; 23154a9babb2SPavel Labath 231628096200SPavel Labath LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3}, {4})={5:x}", req, pid, addr, data, 231728096200SPavel Labath data_size, ret); 2318068f8a7eSTamas Berghammer 2319068f8a7eSTamas Berghammer PtraceDisplayBytes(req, data, data_size); 2320068f8a7eSTamas Berghammer 2321a6321a8eSPavel Labath if (error.Fail()) 2322a6321a8eSPavel Labath LLDB_LOG(log, "ptrace() failed: {0}", error); 2323068f8a7eSTamas Berghammer 23244a9babb2SPavel Labath return error; 2325068f8a7eSTamas Berghammer } 232699e37695SRavitheja Addepally 232799e37695SRavitheja Addepally llvm::Expected<ProcessorTraceMonitor &> 232899e37695SRavitheja Addepally NativeProcessLinux::LookupProcessorTraceInstance(lldb::user_id_t traceid, 232999e37695SRavitheja Addepally lldb::tid_t thread) { 233099e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 233199e37695SRavitheja Addepally if (thread == LLDB_INVALID_THREAD_ID && traceid == m_pt_proces_trace_id) { 233299e37695SRavitheja Addepally LLDB_LOG(log, "thread not specified: {0}", traceid); 233399e37695SRavitheja Addepally return Status("tracing not active thread not specified").ToError(); 233499e37695SRavitheja Addepally } 233599e37695SRavitheja Addepally 233699e37695SRavitheja Addepally for (auto& iter : m_processor_trace_monitor) { 233799e37695SRavitheja Addepally if (traceid == iter.second->GetTraceID() && 233899e37695SRavitheja Addepally (thread == iter.first || thread == LLDB_INVALID_THREAD_ID)) 233999e37695SRavitheja Addepally return *(iter.second); 234099e37695SRavitheja Addepally } 234199e37695SRavitheja Addepally 234299e37695SRavitheja Addepally LLDB_LOG(log, "traceid not being traced: {0}", traceid); 234399e37695SRavitheja Addepally return Status("tracing not active for this thread").ToError(); 234499e37695SRavitheja Addepally } 234599e37695SRavitheja Addepally 234699e37695SRavitheja Addepally Status NativeProcessLinux::GetMetaData(lldb::user_id_t traceid, 234799e37695SRavitheja Addepally lldb::tid_t thread, 234899e37695SRavitheja Addepally llvm::MutableArrayRef<uint8_t> &buffer, 234999e37695SRavitheja Addepally size_t offset) { 235099e37695SRavitheja Addepally TraceOptions trace_options; 235199e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 235299e37695SRavitheja Addepally Status error; 235399e37695SRavitheja Addepally 235499e37695SRavitheja Addepally LLDB_LOG(log, "traceid {0}", traceid); 235599e37695SRavitheja Addepally 235699e37695SRavitheja Addepally auto perf_monitor = LookupProcessorTraceInstance(traceid, thread); 235799e37695SRavitheja Addepally if (!perf_monitor) { 235899e37695SRavitheja Addepally LLDB_LOG(log, "traceid not being traced: {0}", traceid); 235999e37695SRavitheja Addepally buffer = buffer.slice(buffer.size()); 236099e37695SRavitheja Addepally error = perf_monitor.takeError(); 236199e37695SRavitheja Addepally return error; 236299e37695SRavitheja Addepally } 236399e37695SRavitheja Addepally return (*perf_monitor).ReadPerfTraceData(buffer, offset); 236499e37695SRavitheja Addepally } 236599e37695SRavitheja Addepally 236699e37695SRavitheja Addepally Status NativeProcessLinux::GetData(lldb::user_id_t traceid, lldb::tid_t thread, 236799e37695SRavitheja Addepally llvm::MutableArrayRef<uint8_t> &buffer, 236899e37695SRavitheja Addepally size_t offset) { 236999e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 237099e37695SRavitheja Addepally Status error; 237199e37695SRavitheja Addepally 237299e37695SRavitheja Addepally LLDB_LOG(log, "traceid {0}", traceid); 237399e37695SRavitheja Addepally 237499e37695SRavitheja Addepally auto perf_monitor = LookupProcessorTraceInstance(traceid, thread); 237599e37695SRavitheja Addepally if (!perf_monitor) { 237699e37695SRavitheja Addepally LLDB_LOG(log, "traceid not being traced: {0}", traceid); 237799e37695SRavitheja Addepally buffer = buffer.slice(buffer.size()); 237899e37695SRavitheja Addepally error = perf_monitor.takeError(); 237999e37695SRavitheja Addepally return error; 238099e37695SRavitheja Addepally } 238199e37695SRavitheja Addepally return (*perf_monitor).ReadPerfTraceAux(buffer, offset); 238299e37695SRavitheja Addepally } 238399e37695SRavitheja Addepally 238499e37695SRavitheja Addepally Status NativeProcessLinux::GetTraceConfig(lldb::user_id_t traceid, 238599e37695SRavitheja Addepally TraceOptions &config) { 238699e37695SRavitheja Addepally Status error; 238799e37695SRavitheja Addepally if (config.getThreadID() == LLDB_INVALID_THREAD_ID && 238899e37695SRavitheja Addepally m_pt_proces_trace_id == traceid) { 238999e37695SRavitheja Addepally if (m_pt_proces_trace_id == LLDB_INVALID_UID) { 239099e37695SRavitheja Addepally error.SetErrorString("tracing not active for this process"); 239199e37695SRavitheja Addepally return error; 239299e37695SRavitheja Addepally } 239399e37695SRavitheja Addepally config = m_pt_process_trace_config; 239499e37695SRavitheja Addepally } else { 239599e37695SRavitheja Addepally auto perf_monitor = 239699e37695SRavitheja Addepally LookupProcessorTraceInstance(traceid, config.getThreadID()); 239799e37695SRavitheja Addepally if (!perf_monitor) { 239899e37695SRavitheja Addepally error = perf_monitor.takeError(); 239999e37695SRavitheja Addepally return error; 240099e37695SRavitheja Addepally } 240199e37695SRavitheja Addepally error = (*perf_monitor).GetTraceConfig(config); 240299e37695SRavitheja Addepally } 240399e37695SRavitheja Addepally return error; 240499e37695SRavitheja Addepally } 240599e37695SRavitheja Addepally 240699e37695SRavitheja Addepally lldb::user_id_t 240799e37695SRavitheja Addepally NativeProcessLinux::StartTraceGroup(const TraceOptions &config, 240899e37695SRavitheja Addepally Status &error) { 240999e37695SRavitheja Addepally 241099e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 241199e37695SRavitheja Addepally if (config.getType() != TraceType::eTraceTypeProcessorTrace) 241299e37695SRavitheja Addepally return LLDB_INVALID_UID; 241399e37695SRavitheja Addepally 241499e37695SRavitheja Addepally if (m_pt_proces_trace_id != LLDB_INVALID_UID) { 241599e37695SRavitheja Addepally error.SetErrorString("tracing already active on this process"); 241699e37695SRavitheja Addepally return m_pt_proces_trace_id; 241799e37695SRavitheja Addepally } 241899e37695SRavitheja Addepally 241999e37695SRavitheja Addepally for (const auto &thread_sp : m_threads) { 242099e37695SRavitheja Addepally if (auto traceInstance = ProcessorTraceMonitor::Create( 242199e37695SRavitheja Addepally GetID(), thread_sp->GetID(), config, true)) { 242299e37695SRavitheja Addepally m_pt_traced_thread_group.insert(thread_sp->GetID()); 242399e37695SRavitheja Addepally m_processor_trace_monitor.insert( 242499e37695SRavitheja Addepally std::make_pair(thread_sp->GetID(), std::move(*traceInstance))); 242599e37695SRavitheja Addepally } 242699e37695SRavitheja Addepally } 242799e37695SRavitheja Addepally 242899e37695SRavitheja Addepally m_pt_process_trace_config = config; 242999e37695SRavitheja Addepally error = ProcessorTraceMonitor::GetCPUType(m_pt_process_trace_config); 243099e37695SRavitheja Addepally 243199e37695SRavitheja Addepally // Trace on Complete process will have traceid of 0 243299e37695SRavitheja Addepally m_pt_proces_trace_id = 0; 243399e37695SRavitheja Addepally 243499e37695SRavitheja Addepally LLDB_LOG(log, "Process Trace ID {0}", m_pt_proces_trace_id); 243599e37695SRavitheja Addepally return m_pt_proces_trace_id; 243699e37695SRavitheja Addepally } 243799e37695SRavitheja Addepally 243899e37695SRavitheja Addepally lldb::user_id_t NativeProcessLinux::StartTrace(const TraceOptions &config, 243999e37695SRavitheja Addepally Status &error) { 244099e37695SRavitheja Addepally if (config.getType() != TraceType::eTraceTypeProcessorTrace) 244199e37695SRavitheja Addepally return NativeProcessProtocol::StartTrace(config, error); 244299e37695SRavitheja Addepally 244399e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 244499e37695SRavitheja Addepally 244599e37695SRavitheja Addepally lldb::tid_t threadid = config.getThreadID(); 244699e37695SRavitheja Addepally 244799e37695SRavitheja Addepally if (threadid == LLDB_INVALID_THREAD_ID) 244899e37695SRavitheja Addepally return StartTraceGroup(config, error); 244999e37695SRavitheja Addepally 245099e37695SRavitheja Addepally auto thread_sp = GetThreadByID(threadid); 245199e37695SRavitheja Addepally if (!thread_sp) { 245299e37695SRavitheja Addepally // Thread not tracked by lldb so don't trace. 245399e37695SRavitheja Addepally error.SetErrorString("invalid thread id"); 245499e37695SRavitheja Addepally return LLDB_INVALID_UID; 245599e37695SRavitheja Addepally } 245699e37695SRavitheja Addepally 245799e37695SRavitheja Addepally const auto &iter = m_processor_trace_monitor.find(threadid); 245899e37695SRavitheja Addepally if (iter != m_processor_trace_monitor.end()) { 245999e37695SRavitheja Addepally LLDB_LOG(log, "Thread already being traced"); 246099e37695SRavitheja Addepally error.SetErrorString("tracing already active on this thread"); 246199e37695SRavitheja Addepally return LLDB_INVALID_UID; 246299e37695SRavitheja Addepally } 246399e37695SRavitheja Addepally 246499e37695SRavitheja Addepally auto traceMonitor = 246599e37695SRavitheja Addepally ProcessorTraceMonitor::Create(GetID(), threadid, config, false); 246699e37695SRavitheja Addepally if (!traceMonitor) { 246799e37695SRavitheja Addepally error = traceMonitor.takeError(); 246899e37695SRavitheja Addepally LLDB_LOG(log, "error {0}", error); 246999e37695SRavitheja Addepally return LLDB_INVALID_UID; 247099e37695SRavitheja Addepally } 247199e37695SRavitheja Addepally lldb::user_id_t ret_trace_id = (*traceMonitor)->GetTraceID(); 247299e37695SRavitheja Addepally m_processor_trace_monitor.insert( 247399e37695SRavitheja Addepally std::make_pair(threadid, std::move(*traceMonitor))); 247499e37695SRavitheja Addepally return ret_trace_id; 247599e37695SRavitheja Addepally } 247699e37695SRavitheja Addepally 247799e37695SRavitheja Addepally Status NativeProcessLinux::StopTracingForThread(lldb::tid_t thread) { 247899e37695SRavitheja Addepally Status error; 247999e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 248099e37695SRavitheja Addepally LLDB_LOG(log, "Thread {0}", thread); 248199e37695SRavitheja Addepally 248299e37695SRavitheja Addepally const auto& iter = m_processor_trace_monitor.find(thread); 248399e37695SRavitheja Addepally if (iter == m_processor_trace_monitor.end()) { 248499e37695SRavitheja Addepally error.SetErrorString("tracing not active for this thread"); 248599e37695SRavitheja Addepally return error; 248699e37695SRavitheja Addepally } 248799e37695SRavitheja Addepally 248899e37695SRavitheja Addepally if (iter->second->GetTraceID() == m_pt_proces_trace_id) { 248999e37695SRavitheja Addepally // traceid maps to the whole process so we have to erase it from the 249099e37695SRavitheja Addepally // thread group. 249199e37695SRavitheja Addepally LLDB_LOG(log, "traceid maps to process"); 249299e37695SRavitheja Addepally m_pt_traced_thread_group.erase(thread); 249399e37695SRavitheja Addepally } 249499e37695SRavitheja Addepally m_processor_trace_monitor.erase(iter); 249599e37695SRavitheja Addepally 249699e37695SRavitheja Addepally return error; 249799e37695SRavitheja Addepally } 249899e37695SRavitheja Addepally 249999e37695SRavitheja Addepally Status NativeProcessLinux::StopTrace(lldb::user_id_t traceid, 250099e37695SRavitheja Addepally lldb::tid_t thread) { 250199e37695SRavitheja Addepally Status error; 250299e37695SRavitheja Addepally 250399e37695SRavitheja Addepally TraceOptions trace_options; 250499e37695SRavitheja Addepally trace_options.setThreadID(thread); 250599e37695SRavitheja Addepally error = NativeProcessLinux::GetTraceConfig(traceid, trace_options); 250699e37695SRavitheja Addepally 250799e37695SRavitheja Addepally if (error.Fail()) 250899e37695SRavitheja Addepally return error; 250999e37695SRavitheja Addepally 251099e37695SRavitheja Addepally switch (trace_options.getType()) { 251199e37695SRavitheja Addepally case lldb::TraceType::eTraceTypeProcessorTrace: 251299e37695SRavitheja Addepally if (traceid == m_pt_proces_trace_id && 251399e37695SRavitheja Addepally thread == LLDB_INVALID_THREAD_ID) 251499e37695SRavitheja Addepally StopProcessorTracingOnProcess(); 251599e37695SRavitheja Addepally else 251699e37695SRavitheja Addepally error = StopProcessorTracingOnThread(traceid, thread); 251799e37695SRavitheja Addepally break; 251899e37695SRavitheja Addepally default: 251999e37695SRavitheja Addepally error.SetErrorString("trace not supported"); 252099e37695SRavitheja Addepally break; 252199e37695SRavitheja Addepally } 252299e37695SRavitheja Addepally 252399e37695SRavitheja Addepally return error; 252499e37695SRavitheja Addepally } 252599e37695SRavitheja Addepally 252699e37695SRavitheja Addepally void NativeProcessLinux::StopProcessorTracingOnProcess() { 252799e37695SRavitheja Addepally for (auto thread_id_iter : m_pt_traced_thread_group) 252899e37695SRavitheja Addepally m_processor_trace_monitor.erase(thread_id_iter); 252999e37695SRavitheja Addepally m_pt_traced_thread_group.clear(); 253099e37695SRavitheja Addepally m_pt_proces_trace_id = LLDB_INVALID_UID; 253199e37695SRavitheja Addepally } 253299e37695SRavitheja Addepally 253399e37695SRavitheja Addepally Status NativeProcessLinux::StopProcessorTracingOnThread(lldb::user_id_t traceid, 253499e37695SRavitheja Addepally lldb::tid_t thread) { 253599e37695SRavitheja Addepally Status error; 253699e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 253799e37695SRavitheja Addepally 253899e37695SRavitheja Addepally if (thread == LLDB_INVALID_THREAD_ID) { 253999e37695SRavitheja Addepally for (auto& iter : m_processor_trace_monitor) { 254099e37695SRavitheja Addepally if (iter.second->GetTraceID() == traceid) { 254199e37695SRavitheja Addepally // Stopping a trace instance for an individual thread 254299e37695SRavitheja Addepally // hence there will only be one traceid that can match. 254399e37695SRavitheja Addepally m_processor_trace_monitor.erase(iter.first); 254499e37695SRavitheja Addepally return error; 254599e37695SRavitheja Addepally } 254699e37695SRavitheja Addepally LLDB_LOG(log, "Trace ID {0}", iter.second->GetTraceID()); 254799e37695SRavitheja Addepally } 254899e37695SRavitheja Addepally 254999e37695SRavitheja Addepally LLDB_LOG(log, "Invalid TraceID"); 255099e37695SRavitheja Addepally error.SetErrorString("invalid trace id"); 255199e37695SRavitheja Addepally return error; 255299e37695SRavitheja Addepally } 255399e37695SRavitheja Addepally 255499e37695SRavitheja Addepally // thread is specified so we can use find function on the map. 255599e37695SRavitheja Addepally const auto& iter = m_processor_trace_monitor.find(thread); 255699e37695SRavitheja Addepally if (iter == m_processor_trace_monitor.end()) { 255799e37695SRavitheja Addepally // thread not found in our map. 255899e37695SRavitheja Addepally LLDB_LOG(log, "thread not being traced"); 255999e37695SRavitheja Addepally error.SetErrorString("tracing not active for this thread"); 256099e37695SRavitheja Addepally return error; 256199e37695SRavitheja Addepally } 256299e37695SRavitheja Addepally if (iter->second->GetTraceID() != traceid) { 256399e37695SRavitheja Addepally // traceid did not match so it has to be invalid. 256499e37695SRavitheja Addepally LLDB_LOG(log, "Invalid TraceID"); 256599e37695SRavitheja Addepally error.SetErrorString("invalid trace id"); 256699e37695SRavitheja Addepally return error; 256799e37695SRavitheja Addepally } 256899e37695SRavitheja Addepally 256999e37695SRavitheja Addepally LLDB_LOG(log, "UID - {0} , Thread -{1}", traceid, thread); 257099e37695SRavitheja Addepally 257199e37695SRavitheja Addepally if (traceid == m_pt_proces_trace_id) { 257299e37695SRavitheja Addepally // traceid maps to the whole process so we have to erase it from the 257399e37695SRavitheja Addepally // thread group. 257499e37695SRavitheja Addepally LLDB_LOG(log, "traceid maps to process"); 257599e37695SRavitheja Addepally m_pt_traced_thread_group.erase(thread); 257699e37695SRavitheja Addepally } 257799e37695SRavitheja Addepally m_processor_trace_monitor.erase(iter); 257899e37695SRavitheja Addepally 257999e37695SRavitheja Addepally return error; 258099e37695SRavitheja Addepally } 2581