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 217*96e600fcSPavel Labath llvm::Expected<NativeProcessProtocolSP> 218*96e600fcSPavel Labath NativeProcessLinux::Factory::Launch(ProcessLaunchInfo &launch_info, 219*96e600fcSPavel Labath NativeDelegate &native_delegate, 220*96e600fcSPavel Labath MainLoop &mainloop) const { 221a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 222af245d11STodd Fiala 223*96e600fcSPavel Labath MaybeLogLaunchInfo(launch_info); 224af245d11STodd Fiala 225*96e600fcSPavel Labath Status status; 226*96e600fcSPavel Labath ::pid_t pid = ProcessLauncherPosixFork() 227*96e600fcSPavel Labath .LaunchProcess(launch_info, status) 228*96e600fcSPavel Labath .GetProcessId(); 229*96e600fcSPavel Labath LLDB_LOG(log, "pid = {0:x}", pid); 230*96e600fcSPavel Labath if (status.Fail()) { 231*96e600fcSPavel Labath LLDB_LOG(log, "failed to launch process: {0}", status); 232*96e600fcSPavel Labath return status.ToError(); 233af245d11STodd Fiala } 234af245d11STodd Fiala 235*96e600fcSPavel Labath // Wait for the child process to trap on its call to execve. 236*96e600fcSPavel Labath int wstatus; 237*96e600fcSPavel Labath ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, ::waitpid, pid, &wstatus, 0); 238*96e600fcSPavel Labath assert(wpid == pid); 239*96e600fcSPavel Labath (void)wpid; 240*96e600fcSPavel Labath if (!WIFSTOPPED(wstatus)) { 241*96e600fcSPavel Labath LLDB_LOG(log, "Could not sync with inferior process: wstatus={1}", 242*96e600fcSPavel Labath WaitStatus::Decode(wstatus)); 243*96e600fcSPavel Labath return llvm::make_error<StringError>("Could not sync with inferior process", 244*96e600fcSPavel Labath llvm::inconvertibleErrorCode()); 245*96e600fcSPavel Labath } 246*96e600fcSPavel Labath LLDB_LOG(log, "inferior started, now in stopped state"); 247af245d11STodd Fiala 248*96e600fcSPavel Labath ArchSpec arch; 249*96e600fcSPavel Labath if ((status = ResolveProcessArchitecture(pid, arch)).Fail()) 250*96e600fcSPavel Labath return status.ToError(); 251*96e600fcSPavel Labath 252*96e600fcSPavel Labath // Set the architecture to the exe architecture. 253*96e600fcSPavel Labath LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid, 254*96e600fcSPavel Labath arch.GetArchitectureName()); 255*96e600fcSPavel Labath 256*96e600fcSPavel Labath status = SetDefaultPtraceOpts(pid); 257*96e600fcSPavel Labath if (status.Fail()) { 258*96e600fcSPavel Labath LLDB_LOG(log, "failed to set default ptrace options: {0}", status); 259*96e600fcSPavel Labath return status.ToError(); 260af245d11STodd Fiala } 261af245d11STodd Fiala 262*96e600fcSPavel Labath std::shared_ptr<NativeProcessLinux> process_sp(new NativeProcessLinux( 263*96e600fcSPavel Labath pid, launch_info.GetPTY().ReleaseMasterFileDescriptor(), native_delegate, 264*96e600fcSPavel Labath arch, mainloop)); 265*96e600fcSPavel Labath process_sp->InitializeThreads({pid}); 266*96e600fcSPavel Labath return process_sp; 267af245d11STodd Fiala } 268af245d11STodd Fiala 269*96e600fcSPavel Labath llvm::Expected<NativeProcessProtocolSP> NativeProcessLinux::Factory::Attach( 270b9c1b51eSKate Stone lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, 271*96e600fcSPavel 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. 276*96e600fcSPavel Labath ArchSpec arch; 277*96e600fcSPavel Labath Status status = ResolveProcessArchitecture(pid, arch); 278*96e600fcSPavel Labath if (!status.Success()) 279*96e600fcSPavel Labath return status.ToError(); 280af245d11STodd Fiala 281*96e600fcSPavel Labath auto tids_or = NativeProcessLinux::Attach(pid); 282*96e600fcSPavel Labath if (!tids_or) 283*96e600fcSPavel Labath return tids_or.takeError(); 284af245d11STodd Fiala 285*96e600fcSPavel Labath std::shared_ptr<NativeProcessLinux> process_sp( 286*96e600fcSPavel Labath new NativeProcessLinux(pid, -1, native_delegate, arch, mainloop)); 287*96e600fcSPavel Labath process_sp->InitializeThreads(*tids_or); 288*96e600fcSPavel Labath return process_sp; 289af245d11STodd Fiala } 290af245d11STodd Fiala 291af245d11STodd Fiala // ----------------------------------------------------------------------------- 292af245d11STodd Fiala // Public Instance Methods 293af245d11STodd Fiala // ----------------------------------------------------------------------------- 294af245d11STodd Fiala 295*96e600fcSPavel Labath NativeProcessLinux::NativeProcessLinux(::pid_t pid, int terminal_fd, 296*96e600fcSPavel Labath NativeDelegate &delegate, 297*96e600fcSPavel Labath const ArchSpec &arch, MainLoop &mainloop) 298*96e600fcSPavel Labath : NativeProcessProtocol(pid, terminal_fd, delegate), m_arch(arch) { 299b9c1b51eSKate Stone if (m_terminal_fd != -1) { 300*96e600fcSPavel Labath Status status = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); 301*96e600fcSPavel Labath assert(status.Success()); 3025ad891f7SPavel Labath } 303af245d11STodd Fiala 304*96e600fcSPavel Labath Status status; 305*96e600fcSPavel Labath m_sigchld_handle = mainloop.RegisterSignal( 306*96e600fcSPavel Labath SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, status); 307*96e600fcSPavel Labath assert(m_sigchld_handle && status.Success()); 308*96e600fcSPavel Labath } 309*96e600fcSPavel Labath 310*96e600fcSPavel Labath void NativeProcessLinux::InitializeThreads(llvm::ArrayRef<::pid_t> tids) { 311*96e600fcSPavel Labath for (const auto &tid : tids) { 312*96e600fcSPavel 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 318*96e600fcSPavel Labath // Let our process instance know the thread has stopped. 319*96e600fcSPavel Labath SetCurrentThreadID(tids[0]); 320*96e600fcSPavel Labath SetState(StateType::eStateStopped, false); 321*96e600fcSPavel Labath 322*96e600fcSPavel Labath // Proccess any signals we received before installing our handler 323*96e600fcSPavel Labath SigchldHandler(); 324*96e600fcSPavel Labath } 325*96e600fcSPavel Labath 326*96e600fcSPavel Labath llvm::Expected<std::vector<::pid_t>> NativeProcessLinux::Attach(::pid_t pid) { 327a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 328af245d11STodd Fiala 329*96e600fcSPavel 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. 341*96e600fcSPavel 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. 344*96e600fcSPavel Labath if (status.GetError() == ESRCH) { 345af245d11STodd Fiala it = tids_to_attach.erase(it); 346af245d11STodd Fiala continue; 347*96e600fcSPavel Labath } 348*96e600fcSPavel Labath return status.ToError(); 349af245d11STodd Fiala } 350af245d11STodd Fiala 351*96e600fcSPavel Labath int wpid = 352*96e600fcSPavel 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. 355*96e600fcSPavel 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 } 362*96e600fcSPavel Labath return llvm::errorCodeToError( 363*96e600fcSPavel Labath std::error_code(errno, std::generic_category())); 364af245d11STodd Fiala } 365af245d11STodd Fiala 366*96e600fcSPavel Labath if ((status = SetDefaultPtraceOpts(tid)).Fail()) 367*96e600fcSPavel 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 378*96e600fcSPavel Labath size_t tid_count = tids_to_attach.size(); 379*96e600fcSPavel Labath if (tid_count == 0) 380*96e600fcSPavel Labath return llvm::make_error<StringError>("No such process", 381*96e600fcSPavel Labath llvm::inconvertibleErrorCode()); 382af245d11STodd Fiala 383*96e600fcSPavel Labath std::vector<::pid_t> tids; 384*96e600fcSPavel Labath tids.reserve(tid_count); 385*96e600fcSPavel Labath for (const auto &p : tids_to_attach) 386*96e600fcSPavel Labath tids.push_back(p.first); 387*96e600fcSPavel 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: 799a6321a8eSPavel Labath LLDB_LOG( 800a6321a8eSPavel Labath log, 801a6321a8eSPavel Labath "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming", 802a6321a8eSPavel Labath info.si_code, GetID(), thread.GetID()); 803a6321a8eSPavel Labath llvm_unreachable("Unexpected SIGTRAP code!"); 804af245d11STodd Fiala break; 805af245d11STodd Fiala } 806af245d11STodd Fiala } 807af245d11STodd Fiala 808b9c1b51eSKate Stone void NativeProcessLinux::MonitorTrace(NativeThreadLinux &thread) { 809a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 810a6321a8eSPavel Labath LLDB_LOG(log, "received trace event, pid = {0}", thread.GetID()); 811c16f5dcaSChaoren Lin 8120e1d729bSPavel Labath // This thread is currently stopped. 813b9cc0c75SPavel Labath thread.SetStoppedByTrace(); 814c16f5dcaSChaoren Lin 815b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 816c16f5dcaSChaoren Lin } 817c16f5dcaSChaoren Lin 818b9c1b51eSKate Stone void NativeProcessLinux::MonitorBreakpoint(NativeThreadLinux &thread) { 819b9c1b51eSKate Stone Log *log( 820b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 821a6321a8eSPavel Labath LLDB_LOG(log, "received breakpoint event, pid = {0}", thread.GetID()); 822c16f5dcaSChaoren Lin 823c16f5dcaSChaoren Lin // Mark the thread as stopped at breakpoint. 824b9cc0c75SPavel Labath thread.SetStoppedByBreakpoint(); 82597206d57SZachary Turner Status error = FixupBreakpointPCAsNeeded(thread); 826c16f5dcaSChaoren Lin if (error.Fail()) 827a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0} fixup: {1}", thread.GetID(), error); 828d8c338d4STamas Berghammer 829b9c1b51eSKate Stone if (m_threads_stepping_with_breakpoint.find(thread.GetID()) != 830b9c1b51eSKate Stone m_threads_stepping_with_breakpoint.end()) 831b9cc0c75SPavel Labath thread.SetStoppedByTrace(); 832c16f5dcaSChaoren Lin 833b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 834c16f5dcaSChaoren Lin } 835c16f5dcaSChaoren Lin 836b9c1b51eSKate Stone void NativeProcessLinux::MonitorWatchpoint(NativeThreadLinux &thread, 837b9c1b51eSKate Stone uint32_t wp_index) { 838b9c1b51eSKate Stone Log *log( 839b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_WATCHPOINTS)); 840a6321a8eSPavel Labath LLDB_LOG(log, "received watchpoint event, pid = {0}, wp_index = {1}", 841a6321a8eSPavel Labath thread.GetID(), wp_index); 842c16f5dcaSChaoren Lin 843c16f5dcaSChaoren Lin // Mark the thread as stopped at watchpoint. 844c16f5dcaSChaoren Lin // The address is at (lldb::addr_t)info->si_addr if we need it. 845f9077782SPavel Labath thread.SetStoppedByWatchpoint(wp_index); 846c16f5dcaSChaoren Lin 847b9c1b51eSKate Stone // We need to tell all other running threads before we notify the delegate 848b9c1b51eSKate Stone // about this stop. 849f9077782SPavel Labath StopRunningThreads(thread.GetID()); 850c16f5dcaSChaoren Lin } 851c16f5dcaSChaoren Lin 852b9c1b51eSKate Stone void NativeProcessLinux::MonitorSignal(const siginfo_t &info, 853b9c1b51eSKate Stone NativeThreadLinux &thread, bool exited) { 854b9cc0c75SPavel Labath const int signo = info.si_signo; 855b9cc0c75SPavel Labath const bool is_from_llgs = info.si_pid == getpid(); 856af245d11STodd Fiala 857a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 858af245d11STodd Fiala 859af245d11STodd Fiala // POSIX says that process behaviour is undefined after it ignores a SIGFPE, 860af245d11STodd Fiala // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a 861af245d11STodd Fiala // kill(2) or raise(3). Similarly for tgkill(2) on Linux. 862af245d11STodd Fiala // 863af245d11STodd Fiala // IOW, user generated signals never generate what we consider to be a 864af245d11STodd Fiala // "crash". 865af245d11STodd Fiala // 866af245d11STodd Fiala // Similarly, ACK signals generated by this monitor. 867af245d11STodd Fiala 868af245d11STodd Fiala // Handle the signal. 869a6321a8eSPavel Labath LLDB_LOG(log, 870a6321a8eSPavel Labath "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, " 871a6321a8eSPavel Labath "waitpid pid = {4})", 872a6321a8eSPavel Labath Host::GetSignalAsCString(signo), signo, info.si_code, 873b9cc0c75SPavel Labath thread.GetID()); 87458a2f669STodd Fiala 87558a2f669STodd Fiala // Check for thread stop notification. 876b9c1b51eSKate Stone if (is_from_llgs && (info.si_code == SI_TKILL) && (signo == SIGSTOP)) { 877af245d11STodd Fiala // This is a tgkill()-based stop. 878a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} tid {1}, thread stopped", GetID(), thread.GetID()); 879fa03ad2eSChaoren Lin 880aab58633SChaoren Lin // Check that we're not already marked with a stop reason. 881b9c1b51eSKate Stone // Note this thread really shouldn't already be marked as stopped - if we 882a6321a8eSPavel Labath // were, that would imply that the kernel signaled us with the thread 883a6321a8eSPavel Labath // stopping which we handled and marked as stopped, and that, without an 884a6321a8eSPavel Labath // intervening resume, we received another stop. It is more likely that we 885a6321a8eSPavel Labath // are missing the marking of a run state somewhere if we find that the 886a6321a8eSPavel Labath // thread was marked as stopped. 887b9cc0c75SPavel Labath const StateType thread_state = thread.GetState(); 888b9c1b51eSKate Stone if (!StateIsStoppedState(thread_state, false)) { 889ed89c7feSPavel Labath // An inferior thread has stopped because of a SIGSTOP we have sent it. 890b9c1b51eSKate Stone // Generally, these are not important stops and we don't want to report 891a6321a8eSPavel Labath // them as they are just used to stop other threads when one thread (the 892a6321a8eSPavel Labath // one with the *real* stop reason) hits a breakpoint (watchpoint, 893a6321a8eSPavel Labath // etc...). However, in the case of an asynchronous Interrupt(), this *is* 894a6321a8eSPavel Labath // the real stop reason, so we leave the signal intact if this is the 895a6321a8eSPavel Labath // thread that was chosen as the triggering thread. 896b9c1b51eSKate Stone if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) { 897b9cc0c75SPavel Labath if (m_pending_notification_tid == thread.GetID()) 898b9cc0c75SPavel Labath thread.SetStoppedBySignal(SIGSTOP, &info); 899ed89c7feSPavel Labath else 900b9cc0c75SPavel Labath thread.SetStoppedWithNoReason(); 901ed89c7feSPavel Labath 902b9cc0c75SPavel Labath SetCurrentThreadID(thread.GetID()); 9030e1d729bSPavel Labath SignalIfAllThreadsStopped(); 904b9c1b51eSKate Stone } else { 9050e1d729bSPavel Labath // We can end up here if stop was initiated by LLGS but by this time a 9060e1d729bSPavel Labath // thread stop has occurred - maybe initiated by another event. 90797206d57SZachary Turner Status error = ResumeThread(thread, thread.GetState(), 0); 908a6321a8eSPavel Labath if (error.Fail()) 909a6321a8eSPavel Labath LLDB_LOG(log, "failed to resume thread {0}: {1}", thread.GetID(), 910a6321a8eSPavel Labath error); 9110e1d729bSPavel Labath } 912b9c1b51eSKate Stone } else { 913a6321a8eSPavel Labath LLDB_LOG(log, 914a6321a8eSPavel Labath "pid {0} tid {1}, thread was already marked as a stopped " 915a6321a8eSPavel Labath "state (state={2}), leaving stop signal as is", 9168198db30SPavel Labath GetID(), thread.GetID(), thread_state); 9170e1d729bSPavel Labath SignalIfAllThreadsStopped(); 918af245d11STodd Fiala } 919af245d11STodd Fiala 92058a2f669STodd Fiala // Done handling. 921af245d11STodd Fiala return; 922af245d11STodd Fiala } 923af245d11STodd Fiala 9244a705e7eSPavel Labath // Check if debugger should stop at this signal or just ignore it 9254a705e7eSPavel Labath // and resume the inferior. 9264a705e7eSPavel Labath if (m_signals_to_ignore.find(signo) != m_signals_to_ignore.end()) { 9274a705e7eSPavel Labath ResumeThread(thread, thread.GetState(), signo); 9284a705e7eSPavel Labath return; 9294a705e7eSPavel Labath } 9304a705e7eSPavel Labath 93186fd8e45SChaoren Lin // This thread is stopped. 932a6321a8eSPavel Labath LLDB_LOG(log, "received signal {0}", Host::GetSignalAsCString(signo)); 933b9cc0c75SPavel Labath thread.SetStoppedBySignal(signo, &info); 93486fd8e45SChaoren Lin 93586fd8e45SChaoren Lin // Send a stop to the debugger after we get all other threads to stop. 936b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 937511e5cdcSTodd Fiala } 938af245d11STodd Fiala 939e7708688STamas Berghammer namespace { 940e7708688STamas Berghammer 941b9c1b51eSKate Stone struct EmulatorBaton { 942e7708688STamas Berghammer NativeProcessLinux *m_process; 943e7708688STamas Berghammer NativeRegisterContext *m_reg_context; 9446648fcc3SPavel Labath 9456648fcc3SPavel Labath // eRegisterKindDWARF -> RegsiterValue 9466648fcc3SPavel Labath std::unordered_map<uint32_t, RegisterValue> m_register_values; 947e7708688STamas Berghammer 948b9c1b51eSKate Stone EmulatorBaton(NativeProcessLinux *process, NativeRegisterContext *reg_context) 949b9c1b51eSKate Stone : m_process(process), m_reg_context(reg_context) {} 950e7708688STamas Berghammer }; 951e7708688STamas Berghammer 952e7708688STamas Berghammer } // anonymous namespace 953e7708688STamas Berghammer 954b9c1b51eSKate Stone static size_t ReadMemoryCallback(EmulateInstruction *instruction, void *baton, 955e7708688STamas Berghammer const EmulateInstruction::Context &context, 956b9c1b51eSKate Stone lldb::addr_t addr, void *dst, size_t length) { 957e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 958e7708688STamas Berghammer 9593eb4b458SChaoren Lin size_t bytes_read; 960e7708688STamas Berghammer emulator_baton->m_process->ReadMemory(addr, dst, length, bytes_read); 961e7708688STamas Berghammer return bytes_read; 962e7708688STamas Berghammer } 963e7708688STamas Berghammer 964b9c1b51eSKate Stone static bool ReadRegisterCallback(EmulateInstruction *instruction, void *baton, 965e7708688STamas Berghammer const RegisterInfo *reg_info, 966b9c1b51eSKate Stone RegisterValue ®_value) { 967e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 968e7708688STamas Berghammer 969b9c1b51eSKate Stone auto it = emulator_baton->m_register_values.find( 970b9c1b51eSKate Stone reg_info->kinds[eRegisterKindDWARF]); 971b9c1b51eSKate Stone if (it != emulator_baton->m_register_values.end()) { 9726648fcc3SPavel Labath reg_value = it->second; 9736648fcc3SPavel Labath return true; 9746648fcc3SPavel Labath } 9756648fcc3SPavel Labath 976e7708688STamas Berghammer // The emulator only fill in the dwarf regsiter numbers (and in some case 977e7708688STamas Berghammer // the generic register numbers). Get the full register info from the 978e7708688STamas Berghammer // register context based on the dwarf register numbers. 979b9c1b51eSKate Stone const RegisterInfo *full_reg_info = 980b9c1b51eSKate Stone emulator_baton->m_reg_context->GetRegisterInfo( 981e7708688STamas Berghammer eRegisterKindDWARF, reg_info->kinds[eRegisterKindDWARF]); 982e7708688STamas Berghammer 98397206d57SZachary Turner Status error = 984b9c1b51eSKate Stone emulator_baton->m_reg_context->ReadRegister(full_reg_info, reg_value); 9856648fcc3SPavel Labath if (error.Success()) 9866648fcc3SPavel Labath return true; 987cdc22a88SMohit K. Bhakkad 9886648fcc3SPavel Labath return false; 989e7708688STamas Berghammer } 990e7708688STamas Berghammer 991b9c1b51eSKate Stone static bool WriteRegisterCallback(EmulateInstruction *instruction, void *baton, 992e7708688STamas Berghammer const EmulateInstruction::Context &context, 993e7708688STamas Berghammer const RegisterInfo *reg_info, 994b9c1b51eSKate Stone const RegisterValue ®_value) { 995e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 996b9c1b51eSKate Stone emulator_baton->m_register_values[reg_info->kinds[eRegisterKindDWARF]] = 997b9c1b51eSKate Stone reg_value; 998e7708688STamas Berghammer return true; 999e7708688STamas Berghammer } 1000e7708688STamas Berghammer 1001b9c1b51eSKate Stone static size_t WriteMemoryCallback(EmulateInstruction *instruction, void *baton, 1002e7708688STamas Berghammer const EmulateInstruction::Context &context, 1003b9c1b51eSKate Stone lldb::addr_t addr, const void *dst, 1004b9c1b51eSKate Stone size_t length) { 1005e7708688STamas Berghammer return length; 1006e7708688STamas Berghammer } 1007e7708688STamas Berghammer 1008b9c1b51eSKate Stone static lldb::addr_t ReadFlags(NativeRegisterContext *regsiter_context) { 1009e7708688STamas Berghammer const RegisterInfo *flags_info = regsiter_context->GetRegisterInfo( 1010e7708688STamas Berghammer eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 1011b9c1b51eSKate Stone return regsiter_context->ReadRegisterAsUnsigned(flags_info, 1012b9c1b51eSKate Stone LLDB_INVALID_ADDRESS); 1013e7708688STamas Berghammer } 1014e7708688STamas Berghammer 101597206d57SZachary Turner Status 101697206d57SZachary Turner NativeProcessLinux::SetupSoftwareSingleStepping(NativeThreadLinux &thread) { 101797206d57SZachary Turner Status error; 1018b9cc0c75SPavel Labath NativeRegisterContextSP register_context_sp = thread.GetRegisterContext(); 1019e7708688STamas Berghammer 1020e7708688STamas Berghammer std::unique_ptr<EmulateInstruction> emulator_ap( 1021b9c1b51eSKate Stone EmulateInstruction::FindPlugin(m_arch, eInstructionTypePCModifying, 1022b9c1b51eSKate Stone nullptr)); 1023e7708688STamas Berghammer 1024e7708688STamas Berghammer if (emulator_ap == nullptr) 102597206d57SZachary Turner return Status("Instruction emulator not found!"); 1026e7708688STamas Berghammer 1027e7708688STamas Berghammer EmulatorBaton baton(this, register_context_sp.get()); 1028e7708688STamas Berghammer emulator_ap->SetBaton(&baton); 1029e7708688STamas Berghammer emulator_ap->SetReadMemCallback(&ReadMemoryCallback); 1030e7708688STamas Berghammer emulator_ap->SetReadRegCallback(&ReadRegisterCallback); 1031e7708688STamas Berghammer emulator_ap->SetWriteMemCallback(&WriteMemoryCallback); 1032e7708688STamas Berghammer emulator_ap->SetWriteRegCallback(&WriteRegisterCallback); 1033e7708688STamas Berghammer 1034e7708688STamas Berghammer if (!emulator_ap->ReadInstruction()) 103597206d57SZachary Turner return Status("Read instruction failed!"); 1036e7708688STamas Berghammer 1037b9c1b51eSKate Stone bool emulation_result = 1038b9c1b51eSKate Stone emulator_ap->EvaluateInstruction(eEmulateInstructionOptionAutoAdvancePC); 10396648fcc3SPavel Labath 1040b9c1b51eSKate Stone const RegisterInfo *reg_info_pc = register_context_sp->GetRegisterInfo( 1041b9c1b51eSKate Stone eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); 1042b9c1b51eSKate Stone const RegisterInfo *reg_info_flags = register_context_sp->GetRegisterInfo( 1043b9c1b51eSKate Stone eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 10446648fcc3SPavel Labath 1045b9c1b51eSKate Stone auto pc_it = 1046b9c1b51eSKate Stone baton.m_register_values.find(reg_info_pc->kinds[eRegisterKindDWARF]); 1047b9c1b51eSKate Stone auto flags_it = 1048b9c1b51eSKate Stone baton.m_register_values.find(reg_info_flags->kinds[eRegisterKindDWARF]); 10496648fcc3SPavel Labath 1050e7708688STamas Berghammer lldb::addr_t next_pc; 1051e7708688STamas Berghammer lldb::addr_t next_flags; 1052b9c1b51eSKate Stone if (emulation_result) { 1053b9c1b51eSKate Stone assert(pc_it != baton.m_register_values.end() && 1054b9c1b51eSKate Stone "Emulation was successfull but PC wasn't updated"); 10556648fcc3SPavel Labath next_pc = pc_it->second.GetAsUInt64(); 10566648fcc3SPavel Labath 10576648fcc3SPavel Labath if (flags_it != baton.m_register_values.end()) 10586648fcc3SPavel Labath next_flags = flags_it->second.GetAsUInt64(); 1059e7708688STamas Berghammer else 1060e7708688STamas Berghammer next_flags = ReadFlags(register_context_sp.get()); 1061b9c1b51eSKate Stone } else if (pc_it == baton.m_register_values.end()) { 1062e7708688STamas Berghammer // Emulate instruction failed and it haven't changed PC. Advance PC 1063e7708688STamas Berghammer // with the size of the current opcode because the emulation of all 1064e7708688STamas Berghammer // PC modifying instruction should be successful. The failure most 1065e7708688STamas Berghammer // likely caused by a not supported instruction which don't modify PC. 1066b9c1b51eSKate Stone next_pc = 1067b9c1b51eSKate Stone register_context_sp->GetPC() + emulator_ap->GetOpcode().GetByteSize(); 1068e7708688STamas Berghammer next_flags = ReadFlags(register_context_sp.get()); 1069b9c1b51eSKate Stone } else { 1070e7708688STamas Berghammer // The instruction emulation failed after it modified the PC. It is an 1071e7708688STamas Berghammer // unknown error where we can't continue because the next instruction is 1072e7708688STamas Berghammer // modifying the PC but we don't know how. 107397206d57SZachary Turner return Status("Instruction emulation failed unexpectedly."); 1074e7708688STamas Berghammer } 1075e7708688STamas Berghammer 1076b9c1b51eSKate Stone if (m_arch.GetMachine() == llvm::Triple::arm) { 1077b9c1b51eSKate Stone if (next_flags & 0x20) { 1078e7708688STamas Berghammer // Thumb mode 1079e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 2); 1080b9c1b51eSKate Stone } else { 1081e7708688STamas Berghammer // Arm mode 1082e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 4); 1083e7708688STamas Berghammer } 1084b9c1b51eSKate Stone } else if (m_arch.GetMachine() == llvm::Triple::mips64 || 1085b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64el || 1086b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips || 1087b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mipsel) 1088cdc22a88SMohit K. Bhakkad error = SetSoftwareBreakpoint(next_pc, 4); 1089b9c1b51eSKate Stone else { 1090e7708688STamas Berghammer // No size hint is given for the next breakpoint 1091e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 0); 1092e7708688STamas Berghammer } 1093e7708688STamas Berghammer 109442eb6908SPavel Labath // If setting the breakpoint fails because next_pc is out of 109542eb6908SPavel Labath // the address space, ignore it and let the debugee segfault. 109642eb6908SPavel Labath if (error.GetError() == EIO || error.GetError() == EFAULT) { 109797206d57SZachary Turner return Status(); 109842eb6908SPavel Labath } else if (error.Fail()) 1099e7708688STamas Berghammer return error; 1100e7708688STamas Berghammer 1101b9cc0c75SPavel Labath m_threads_stepping_with_breakpoint.insert({thread.GetID(), next_pc}); 1102e7708688STamas Berghammer 110397206d57SZachary Turner return Status(); 1104e7708688STamas Berghammer } 1105e7708688STamas Berghammer 1106b9c1b51eSKate Stone bool NativeProcessLinux::SupportHardwareSingleStepping() const { 1107b9c1b51eSKate Stone if (m_arch.GetMachine() == llvm::Triple::arm || 1108b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64 || 1109b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64el || 1110b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips || 1111b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mipsel) 1112cdc22a88SMohit K. Bhakkad return false; 1113cdc22a88SMohit K. Bhakkad return true; 1114e7708688STamas Berghammer } 1115e7708688STamas Berghammer 111697206d57SZachary Turner Status NativeProcessLinux::Resume(const ResumeActionList &resume_actions) { 1117a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1118a6321a8eSPavel Labath LLDB_LOG(log, "pid {0}", GetID()); 1119af245d11STodd Fiala 1120e7708688STamas Berghammer bool software_single_step = !SupportHardwareSingleStepping(); 1121af245d11STodd Fiala 1122b9c1b51eSKate Stone if (software_single_step) { 1123b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 1124e7708688STamas Berghammer assert(thread_sp && "thread list should not contain NULL threads"); 1125e7708688STamas Berghammer 1126b9c1b51eSKate Stone const ResumeAction *const action = 1127b9c1b51eSKate Stone resume_actions.GetActionForThread(thread_sp->GetID(), true); 1128e7708688STamas Berghammer if (action == nullptr) 1129e7708688STamas Berghammer continue; 1130e7708688STamas Berghammer 1131b9c1b51eSKate Stone if (action->state == eStateStepping) { 113297206d57SZachary Turner Status error = SetupSoftwareSingleStepping( 1133b9c1b51eSKate Stone static_cast<NativeThreadLinux &>(*thread_sp)); 1134e7708688STamas Berghammer if (error.Fail()) 1135e7708688STamas Berghammer return error; 1136e7708688STamas Berghammer } 1137e7708688STamas Berghammer } 1138e7708688STamas Berghammer } 1139e7708688STamas Berghammer 1140b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 1141af245d11STodd Fiala assert(thread_sp && "thread list should not contain NULL threads"); 1142af245d11STodd Fiala 1143b9c1b51eSKate Stone const ResumeAction *const action = 1144b9c1b51eSKate Stone resume_actions.GetActionForThread(thread_sp->GetID(), true); 11456a196ce6SChaoren Lin 1146b9c1b51eSKate Stone if (action == nullptr) { 1147a6321a8eSPavel Labath LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(), 1148a6321a8eSPavel Labath thread_sp->GetID()); 11496a196ce6SChaoren Lin continue; 11506a196ce6SChaoren Lin } 1151af245d11STodd Fiala 1152a6321a8eSPavel Labath LLDB_LOG(log, "processing resume action state {0} for pid {1} tid {2}", 11538198db30SPavel Labath action->state, GetID(), thread_sp->GetID()); 1154af245d11STodd Fiala 1155b9c1b51eSKate Stone switch (action->state) { 1156af245d11STodd Fiala case eStateRunning: 1157b9c1b51eSKate Stone case eStateStepping: { 1158af245d11STodd Fiala // Run the thread, possibly feeding it the signal. 1159fa03ad2eSChaoren Lin const int signo = action->signal; 1160b9c1b51eSKate Stone ResumeThread(static_cast<NativeThreadLinux &>(*thread_sp), action->state, 1161b9c1b51eSKate Stone signo); 1162af245d11STodd Fiala break; 1163ae29d395SChaoren Lin } 1164af245d11STodd Fiala 1165af245d11STodd Fiala case eStateSuspended: 1166af245d11STodd Fiala case eStateStopped: 1167a6321a8eSPavel Labath llvm_unreachable("Unexpected state"); 1168af245d11STodd Fiala 1169af245d11STodd Fiala default: 117097206d57SZachary Turner return Status("NativeProcessLinux::%s (): unexpected state %s specified " 1171b9c1b51eSKate Stone "for pid %" PRIu64 ", tid %" PRIu64, 1172b9c1b51eSKate Stone __FUNCTION__, StateAsCString(action->state), GetID(), 1173b9c1b51eSKate Stone thread_sp->GetID()); 1174af245d11STodd Fiala } 1175af245d11STodd Fiala } 1176af245d11STodd Fiala 117797206d57SZachary Turner return Status(); 1178af245d11STodd Fiala } 1179af245d11STodd Fiala 118097206d57SZachary Turner Status NativeProcessLinux::Halt() { 118197206d57SZachary Turner Status error; 1182af245d11STodd Fiala 1183af245d11STodd Fiala if (kill(GetID(), SIGSTOP) != 0) 1184af245d11STodd Fiala error.SetErrorToErrno(); 1185af245d11STodd Fiala 1186af245d11STodd Fiala return error; 1187af245d11STodd Fiala } 1188af245d11STodd Fiala 118997206d57SZachary Turner Status NativeProcessLinux::Detach() { 119097206d57SZachary Turner Status error; 1191af245d11STodd Fiala 1192af245d11STodd Fiala // Stop monitoring the inferior. 119319cbe96aSPavel Labath m_sigchld_handle.reset(); 1194af245d11STodd Fiala 11957a9495bcSPavel Labath // Tell ptrace to detach from the process. 11967a9495bcSPavel Labath if (GetID() == LLDB_INVALID_PROCESS_ID) 11977a9495bcSPavel Labath return error; 11987a9495bcSPavel Labath 1199b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 120097206d57SZachary Turner Status e = Detach(thread_sp->GetID()); 12017a9495bcSPavel Labath if (e.Fail()) 1202b9c1b51eSKate Stone error = 1203b9c1b51eSKate Stone e; // Save the error, but still attempt to detach from other threads. 12047a9495bcSPavel Labath } 12057a9495bcSPavel Labath 120699e37695SRavitheja Addepally m_processor_trace_monitor.clear(); 120799e37695SRavitheja Addepally m_pt_proces_trace_id = LLDB_INVALID_UID; 120899e37695SRavitheja Addepally 1209af245d11STodd Fiala return error; 1210af245d11STodd Fiala } 1211af245d11STodd Fiala 121297206d57SZachary Turner Status NativeProcessLinux::Signal(int signo) { 121397206d57SZachary Turner Status error; 1214af245d11STodd Fiala 1215a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1216a6321a8eSPavel Labath LLDB_LOG(log, "sending signal {0} ({1}) to pid {1}", signo, 1217a6321a8eSPavel Labath Host::GetSignalAsCString(signo), GetID()); 1218af245d11STodd Fiala 1219af245d11STodd Fiala if (kill(GetID(), signo)) 1220af245d11STodd Fiala error.SetErrorToErrno(); 1221af245d11STodd Fiala 1222af245d11STodd Fiala return error; 1223af245d11STodd Fiala } 1224af245d11STodd Fiala 122597206d57SZachary Turner Status NativeProcessLinux::Interrupt() { 1226e9547b80SChaoren Lin // Pick a running thread (or if none, a not-dead stopped thread) as 1227e9547b80SChaoren Lin // the chosen thread that will be the stop-reason thread. 1228a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1229e9547b80SChaoren Lin 1230e9547b80SChaoren Lin NativeThreadProtocolSP running_thread_sp; 1231e9547b80SChaoren Lin NativeThreadProtocolSP stopped_thread_sp; 1232e9547b80SChaoren Lin 1233a6321a8eSPavel Labath LLDB_LOG(log, "selecting running thread for interrupt target"); 1234b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 1235e9547b80SChaoren Lin // The thread shouldn't be null but lets just cover that here. 1236e9547b80SChaoren Lin if (!thread_sp) 1237e9547b80SChaoren Lin continue; 1238e9547b80SChaoren Lin 1239e9547b80SChaoren Lin // If we have a running or stepping thread, we'll call that the 1240e9547b80SChaoren Lin // target of the interrupt. 1241e9547b80SChaoren Lin const auto thread_state = thread_sp->GetState(); 1242b9c1b51eSKate Stone if (thread_state == eStateRunning || thread_state == eStateStepping) { 1243e9547b80SChaoren Lin running_thread_sp = thread_sp; 1244e9547b80SChaoren Lin break; 1245b9c1b51eSKate Stone } else if (!stopped_thread_sp && StateIsStoppedState(thread_state, true)) { 1246b9c1b51eSKate Stone // Remember the first non-dead stopped thread. We'll use that as a backup 1247b9c1b51eSKate Stone // if there are no running threads. 1248e9547b80SChaoren Lin stopped_thread_sp = thread_sp; 1249e9547b80SChaoren Lin } 1250e9547b80SChaoren Lin } 1251e9547b80SChaoren Lin 1252b9c1b51eSKate Stone if (!running_thread_sp && !stopped_thread_sp) { 125397206d57SZachary Turner Status error("found no running/stepping or live stopped threads as target " 1254b9c1b51eSKate Stone "for interrupt"); 1255a6321a8eSPavel Labath LLDB_LOG(log, "skipping due to error: {0}", error); 12565830aa75STamas Berghammer 1257e9547b80SChaoren Lin return error; 1258e9547b80SChaoren Lin } 1259e9547b80SChaoren Lin 1260b9c1b51eSKate Stone NativeThreadProtocolSP deferred_signal_thread_sp = 1261b9c1b51eSKate Stone running_thread_sp ? running_thread_sp : stopped_thread_sp; 1262e9547b80SChaoren Lin 1263a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} {1} tid {2} chosen for interrupt target", GetID(), 1264e9547b80SChaoren Lin running_thread_sp ? "running" : "stopped", 1265e9547b80SChaoren Lin deferred_signal_thread_sp->GetID()); 1266e9547b80SChaoren Lin 1267ed89c7feSPavel Labath StopRunningThreads(deferred_signal_thread_sp->GetID()); 126845f5cb31SPavel Labath 126997206d57SZachary Turner return Status(); 1270e9547b80SChaoren Lin } 1271e9547b80SChaoren Lin 127297206d57SZachary Turner Status NativeProcessLinux::Kill() { 1273a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1274a6321a8eSPavel Labath LLDB_LOG(log, "pid {0}", GetID()); 1275af245d11STodd Fiala 127697206d57SZachary Turner Status error; 1277af245d11STodd Fiala 1278b9c1b51eSKate Stone switch (m_state) { 1279af245d11STodd Fiala case StateType::eStateInvalid: 1280af245d11STodd Fiala case StateType::eStateExited: 1281af245d11STodd Fiala case StateType::eStateCrashed: 1282af245d11STodd Fiala case StateType::eStateDetached: 1283af245d11STodd Fiala case StateType::eStateUnloaded: 1284af245d11STodd Fiala // Nothing to do - the process is already dead. 1285a6321a8eSPavel Labath LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(), 12868198db30SPavel Labath m_state); 1287af245d11STodd Fiala return error; 1288af245d11STodd Fiala 1289af245d11STodd Fiala case StateType::eStateConnected: 1290af245d11STodd Fiala case StateType::eStateAttaching: 1291af245d11STodd Fiala case StateType::eStateLaunching: 1292af245d11STodd Fiala case StateType::eStateStopped: 1293af245d11STodd Fiala case StateType::eStateRunning: 1294af245d11STodd Fiala case StateType::eStateStepping: 1295af245d11STodd Fiala case StateType::eStateSuspended: 1296af245d11STodd Fiala // We can try to kill a process in these states. 1297af245d11STodd Fiala break; 1298af245d11STodd Fiala } 1299af245d11STodd Fiala 1300b9c1b51eSKate Stone if (kill(GetID(), SIGKILL) != 0) { 1301af245d11STodd Fiala error.SetErrorToErrno(); 1302af245d11STodd Fiala return error; 1303af245d11STodd Fiala } 1304af245d11STodd Fiala 1305af245d11STodd Fiala return error; 1306af245d11STodd Fiala } 1307af245d11STodd Fiala 130897206d57SZachary Turner static Status 130915930862SPavel Labath ParseMemoryRegionInfoFromProcMapsLine(llvm::StringRef &maps_line, 1310b9c1b51eSKate Stone MemoryRegionInfo &memory_region_info) { 1311af245d11STodd Fiala memory_region_info.Clear(); 1312af245d11STodd Fiala 131315930862SPavel Labath StringExtractor line_extractor(maps_line); 1314af245d11STodd Fiala 1315b9c1b51eSKate Stone // Format: {address_start_hex}-{address_end_hex} perms offset dev inode 1316b9c1b51eSKate Stone // pathname 1317b9c1b51eSKate Stone // perms: rwxp (letter is present if set, '-' if not, final character is 1318b9c1b51eSKate Stone // p=private, s=shared). 1319af245d11STodd Fiala 1320af245d11STodd Fiala // Parse out the starting address 1321af245d11STodd Fiala lldb::addr_t start_address = line_extractor.GetHexMaxU64(false, 0); 1322af245d11STodd Fiala 1323af245d11STodd Fiala // Parse out hyphen separating start and end address from range. 1324af245d11STodd Fiala if (!line_extractor.GetBytesLeft() || (line_extractor.GetChar() != '-')) 132597206d57SZachary Turner return Status( 1326b9c1b51eSKate Stone "malformed /proc/{pid}/maps entry, missing dash between address range"); 1327af245d11STodd Fiala 1328af245d11STodd Fiala // Parse out the ending address 1329af245d11STodd Fiala lldb::addr_t end_address = line_extractor.GetHexMaxU64(false, start_address); 1330af245d11STodd Fiala 1331af245d11STodd Fiala // Parse out the space after the address. 1332af245d11STodd Fiala if (!line_extractor.GetBytesLeft() || (line_extractor.GetChar() != ' ')) 133397206d57SZachary Turner return Status( 133497206d57SZachary Turner "malformed /proc/{pid}/maps entry, missing space after range"); 1335af245d11STodd Fiala 1336af245d11STodd Fiala // Save the range. 1337af245d11STodd Fiala memory_region_info.GetRange().SetRangeBase(start_address); 1338af245d11STodd Fiala memory_region_info.GetRange().SetRangeEnd(end_address); 1339af245d11STodd Fiala 1340b9c1b51eSKate Stone // Any memory region in /proc/{pid}/maps is by definition mapped into the 1341b9c1b51eSKate Stone // process. 1342ad007563SHoward Hellyer memory_region_info.SetMapped(MemoryRegionInfo::OptionalBool::eYes); 1343ad007563SHoward Hellyer 1344af245d11STodd Fiala // Parse out each permission entry. 1345af245d11STodd Fiala if (line_extractor.GetBytesLeft() < 4) 134697206d57SZachary Turner return Status("malformed /proc/{pid}/maps entry, missing some portion of " 1347b9c1b51eSKate Stone "permissions"); 1348af245d11STodd Fiala 1349af245d11STodd Fiala // Handle read permission. 1350af245d11STodd Fiala const char read_perm_char = line_extractor.GetChar(); 1351af245d11STodd Fiala if (read_perm_char == 'r') 1352af245d11STodd Fiala memory_region_info.SetReadable(MemoryRegionInfo::OptionalBool::eYes); 1353c73301bbSTamas Berghammer else if (read_perm_char == '-') 1354af245d11STodd Fiala memory_region_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 1355c73301bbSTamas Berghammer else 135697206d57SZachary Turner return Status("unexpected /proc/{pid}/maps read permission char"); 1357af245d11STodd Fiala 1358af245d11STodd Fiala // Handle write permission. 1359af245d11STodd Fiala const char write_perm_char = line_extractor.GetChar(); 1360af245d11STodd Fiala if (write_perm_char == 'w') 1361af245d11STodd Fiala memory_region_info.SetWritable(MemoryRegionInfo::OptionalBool::eYes); 1362c73301bbSTamas Berghammer else if (write_perm_char == '-') 1363af245d11STodd Fiala memory_region_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 1364c73301bbSTamas Berghammer else 136597206d57SZachary Turner return Status("unexpected /proc/{pid}/maps write permission char"); 1366af245d11STodd Fiala 1367af245d11STodd Fiala // Handle execute permission. 1368af245d11STodd Fiala const char exec_perm_char = line_extractor.GetChar(); 1369af245d11STodd Fiala if (exec_perm_char == 'x') 1370af245d11STodd Fiala memory_region_info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes); 1371c73301bbSTamas Berghammer else if (exec_perm_char == '-') 1372af245d11STodd Fiala memory_region_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1373c73301bbSTamas Berghammer else 137497206d57SZachary Turner return Status("unexpected /proc/{pid}/maps exec permission char"); 1375af245d11STodd Fiala 1376d7d69f80STamas Berghammer line_extractor.GetChar(); // Read the private bit 1377d7d69f80STamas Berghammer line_extractor.SkipSpaces(); // Skip the separator 1378d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the offset 1379d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the major device number 1380d7d69f80STamas Berghammer line_extractor.GetChar(); // Read the device id separator 1381d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the major device number 1382d7d69f80STamas Berghammer line_extractor.SkipSpaces(); // Skip the separator 1383d7d69f80STamas Berghammer line_extractor.GetU64(0, 10); // Read the inode number 1384d7d69f80STamas Berghammer 1385d7d69f80STamas Berghammer line_extractor.SkipSpaces(); 1386b9739d40SPavel Labath const char *name = line_extractor.Peek(); 1387b9739d40SPavel Labath if (name) 1388b9739d40SPavel Labath memory_region_info.SetName(name); 1389d7d69f80STamas Berghammer 139097206d57SZachary Turner return Status(); 1391af245d11STodd Fiala } 1392af245d11STodd Fiala 139397206d57SZachary Turner Status NativeProcessLinux::GetMemoryRegionInfo(lldb::addr_t load_addr, 1394b9c1b51eSKate Stone MemoryRegionInfo &range_info) { 1395b9c1b51eSKate Stone // FIXME review that the final memory region returned extends to the end of 1396b9c1b51eSKate Stone // the virtual address space, 1397af245d11STodd Fiala // with no perms if it is not mapped. 1398af245d11STodd Fiala 1399af245d11STodd Fiala // Use an approach that reads memory regions from /proc/{pid}/maps. 1400af245d11STodd Fiala // Assume proc maps entries are in ascending order. 1401af245d11STodd Fiala // FIXME assert if we find differently. 1402af245d11STodd Fiala 1403b9c1b51eSKate Stone if (m_supports_mem_region == LazyBool::eLazyBoolNo) { 1404af245d11STodd Fiala // We're done. 140597206d57SZachary Turner return Status("unsupported"); 1406af245d11STodd Fiala } 1407af245d11STodd Fiala 140897206d57SZachary Turner Status error = PopulateMemoryRegionCache(); 1409b9c1b51eSKate Stone if (error.Fail()) { 1410af245d11STodd Fiala return error; 1411af245d11STodd Fiala } 1412af245d11STodd Fiala 1413af245d11STodd Fiala lldb::addr_t prev_base_address = 0; 1414af245d11STodd Fiala 1415b9c1b51eSKate Stone // FIXME start by finding the last region that is <= target address using 1416b9c1b51eSKate Stone // binary search. Data is sorted. 1417af245d11STodd Fiala // There can be a ton of regions on pthreads apps with lots of threads. 1418b9c1b51eSKate Stone for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end(); 1419b9c1b51eSKate Stone ++it) { 1420a6f5795aSTamas Berghammer MemoryRegionInfo &proc_entry_info = it->first; 1421af245d11STodd Fiala 1422af245d11STodd Fiala // Sanity check assumption that /proc/{pid}/maps entries are ascending. 1423b9c1b51eSKate Stone assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && 1424b9c1b51eSKate Stone "descending /proc/pid/maps entries detected, unexpected"); 1425af245d11STodd Fiala prev_base_address = proc_entry_info.GetRange().GetRangeBase(); 1426b1554311SHafiz Abid Qadeer UNUSED_IF_ASSERT_DISABLED(prev_base_address); 1427af245d11STodd Fiala 1428b9c1b51eSKate Stone // If the target address comes before this entry, indicate distance to next 1429b9c1b51eSKate Stone // region. 1430b9c1b51eSKate Stone if (load_addr < proc_entry_info.GetRange().GetRangeBase()) { 1431af245d11STodd Fiala range_info.GetRange().SetRangeBase(load_addr); 1432b9c1b51eSKate Stone range_info.GetRange().SetByteSize( 1433b9c1b51eSKate Stone proc_entry_info.GetRange().GetRangeBase() - load_addr); 1434af245d11STodd Fiala range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 1435af245d11STodd Fiala range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 1436af245d11STodd Fiala range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1437ad007563SHoward Hellyer range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 1438af245d11STodd Fiala 1439af245d11STodd Fiala return error; 1440b9c1b51eSKate Stone } else if (proc_entry_info.GetRange().Contains(load_addr)) { 1441af245d11STodd Fiala // The target address is within the memory region we're processing here. 1442af245d11STodd Fiala range_info = proc_entry_info; 1443af245d11STodd Fiala return error; 1444af245d11STodd Fiala } 1445af245d11STodd Fiala 1446b9c1b51eSKate Stone // The target memory address comes somewhere after the region we just 1447b9c1b51eSKate Stone // parsed. 1448af245d11STodd Fiala } 1449af245d11STodd Fiala 1450b9c1b51eSKate Stone // If we made it here, we didn't find an entry that contained the given 1451b9c1b51eSKate Stone // address. Return the 1452b9c1b51eSKate Stone // load_addr as start and the amount of bytes betwwen load address and the end 1453b9c1b51eSKate Stone // of the memory as 145409839c33STamas Berghammer // size. 145509839c33STamas Berghammer range_info.GetRange().SetRangeBase(load_addr); 1456ad007563SHoward Hellyer range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); 145709839c33STamas Berghammer range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 145809839c33STamas Berghammer range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 145909839c33STamas Berghammer range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1460ad007563SHoward Hellyer range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 1461af245d11STodd Fiala return error; 1462af245d11STodd Fiala } 1463af245d11STodd Fiala 146497206d57SZachary Turner Status NativeProcessLinux::PopulateMemoryRegionCache() { 1465a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1466a6f5795aSTamas Berghammer 1467a6f5795aSTamas Berghammer // If our cache is empty, pull the latest. There should always be at least 1468a6f5795aSTamas Berghammer // one memory region if memory region handling is supported. 1469a6f5795aSTamas Berghammer if (!m_mem_region_cache.empty()) { 1470a6321a8eSPavel Labath LLDB_LOG(log, "reusing {0} cached memory region entries", 1471a6321a8eSPavel Labath m_mem_region_cache.size()); 147297206d57SZachary Turner return Status(); 1473a6f5795aSTamas Berghammer } 1474a6f5795aSTamas Berghammer 147515930862SPavel Labath auto BufferOrError = getProcFile(GetID(), "maps"); 147615930862SPavel Labath if (!BufferOrError) { 147715930862SPavel Labath m_supports_mem_region = LazyBool::eLazyBoolNo; 147815930862SPavel Labath return BufferOrError.getError(); 147915930862SPavel Labath } 148015930862SPavel Labath StringRef Rest = BufferOrError.get()->getBuffer(); 148115930862SPavel Labath while (! Rest.empty()) { 148215930862SPavel Labath StringRef Line; 148315930862SPavel Labath std::tie(Line, Rest) = Rest.split('\n'); 1484a6f5795aSTamas Berghammer MemoryRegionInfo info; 148597206d57SZachary Turner const Status parse_error = 148697206d57SZachary Turner ParseMemoryRegionInfoFromProcMapsLine(Line, info); 148715930862SPavel Labath if (parse_error.Fail()) { 148815930862SPavel Labath LLDB_LOG(log, "failed to parse proc maps line '{0}': {1}", Line, 148915930862SPavel Labath parse_error); 149015930862SPavel Labath m_supports_mem_region = LazyBool::eLazyBoolNo; 149115930862SPavel Labath return parse_error; 149215930862SPavel Labath } 1493a6f5795aSTamas Berghammer m_mem_region_cache.emplace_back( 1494a6f5795aSTamas Berghammer info, FileSpec(info.GetName().GetCString(), true)); 1495a6f5795aSTamas Berghammer } 1496a6f5795aSTamas Berghammer 149715930862SPavel Labath if (m_mem_region_cache.empty()) { 1498a6f5795aSTamas Berghammer // No entries after attempting to read them. This shouldn't happen if 1499a6f5795aSTamas Berghammer // /proc/{pid}/maps is supported. Assume we don't support map entries 1500a6f5795aSTamas Berghammer // via procfs. 150115930862SPavel Labath m_supports_mem_region = LazyBool::eLazyBoolNo; 1502a6321a8eSPavel Labath LLDB_LOG(log, 1503a6321a8eSPavel Labath "failed to find any procfs maps entries, assuming no support " 1504a6321a8eSPavel Labath "for memory region metadata retrieval"); 150597206d57SZachary Turner return Status("not supported"); 1506a6f5795aSTamas Berghammer } 1507a6f5795aSTamas Berghammer 1508a6321a8eSPavel Labath LLDB_LOG(log, "read {0} memory region entries from /proc/{1}/maps", 1509a6321a8eSPavel Labath m_mem_region_cache.size(), GetID()); 1510a6f5795aSTamas Berghammer 1511a6f5795aSTamas Berghammer // We support memory retrieval, remember that. 1512a6f5795aSTamas Berghammer m_supports_mem_region = LazyBool::eLazyBoolYes; 151397206d57SZachary Turner return Status(); 1514a6f5795aSTamas Berghammer } 1515a6f5795aSTamas Berghammer 1516b9c1b51eSKate Stone void NativeProcessLinux::DoStopIDBumped(uint32_t newBumpId) { 1517a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1518a6321a8eSPavel Labath LLDB_LOG(log, "newBumpId={0}", newBumpId); 1519a6321a8eSPavel Labath LLDB_LOG(log, "clearing {0} entries from memory region cache", 1520a6321a8eSPavel Labath m_mem_region_cache.size()); 1521af245d11STodd Fiala m_mem_region_cache.clear(); 1522af245d11STodd Fiala } 1523af245d11STodd Fiala 152497206d57SZachary Turner Status NativeProcessLinux::AllocateMemory(size_t size, uint32_t permissions, 1525b9c1b51eSKate Stone lldb::addr_t &addr) { 1526af245d11STodd Fiala // FIXME implementing this requires the equivalent of 1527af245d11STodd Fiala // InferiorCallPOSIX::InferiorCallMmap, which depends on 1528af245d11STodd Fiala // functional ThreadPlans working with Native*Protocol. 1529af245d11STodd Fiala #if 1 153097206d57SZachary Turner return Status("not implemented yet"); 1531af245d11STodd Fiala #else 1532af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 1533af245d11STodd Fiala 1534af245d11STodd Fiala unsigned prot = 0; 1535af245d11STodd Fiala if (permissions & lldb::ePermissionsReadable) 1536af245d11STodd Fiala prot |= eMmapProtRead; 1537af245d11STodd Fiala if (permissions & lldb::ePermissionsWritable) 1538af245d11STodd Fiala prot |= eMmapProtWrite; 1539af245d11STodd Fiala if (permissions & lldb::ePermissionsExecutable) 1540af245d11STodd Fiala prot |= eMmapProtExec; 1541af245d11STodd Fiala 1542af245d11STodd Fiala // TODO implement this directly in NativeProcessLinux 1543af245d11STodd Fiala // (and lift to NativeProcessPOSIX if/when that class is 1544af245d11STodd Fiala // refactored out). 1545af245d11STodd Fiala if (InferiorCallMmap(this, addr, 0, size, prot, 1546af245d11STodd Fiala eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { 1547af245d11STodd Fiala m_addr_to_mmap_size[addr] = size; 154897206d57SZachary Turner return Status(); 1549af245d11STodd Fiala } else { 1550af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 155197206d57SZachary Turner return Status("unable to allocate %" PRIu64 1552b9c1b51eSKate Stone " bytes of memory with permissions %s", 1553b9c1b51eSKate Stone size, GetPermissionsAsCString(permissions)); 1554af245d11STodd Fiala } 1555af245d11STodd Fiala #endif 1556af245d11STodd Fiala } 1557af245d11STodd Fiala 155897206d57SZachary Turner Status NativeProcessLinux::DeallocateMemory(lldb::addr_t addr) { 1559af245d11STodd Fiala // FIXME see comments in AllocateMemory - required lower-level 1560af245d11STodd Fiala // bits not in place yet (ThreadPlans) 156197206d57SZachary Turner return Status("not implemented"); 1562af245d11STodd Fiala } 1563af245d11STodd Fiala 1564b9c1b51eSKate Stone lldb::addr_t NativeProcessLinux::GetSharedLibraryInfoAddress() { 1565af245d11STodd Fiala // punt on this for now 1566af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 1567af245d11STodd Fiala } 1568af245d11STodd Fiala 1569b9c1b51eSKate Stone size_t NativeProcessLinux::UpdateThreads() { 1570af245d11STodd Fiala // The NativeProcessLinux monitoring threads are always up to date 1571af245d11STodd Fiala // with respect to thread state and they keep the thread list 1572af245d11STodd Fiala // populated properly. All this method needs to do is return the 1573af245d11STodd Fiala // thread count. 1574af245d11STodd Fiala return m_threads.size(); 1575af245d11STodd Fiala } 1576af245d11STodd Fiala 1577b9c1b51eSKate Stone bool NativeProcessLinux::GetArchitecture(ArchSpec &arch) const { 1578af245d11STodd Fiala arch = m_arch; 1579af245d11STodd Fiala return true; 1580af245d11STodd Fiala } 1581af245d11STodd Fiala 158297206d57SZachary Turner Status NativeProcessLinux::GetSoftwareBreakpointPCOffset( 1583b9c1b51eSKate Stone uint32_t &actual_opcode_size) { 1584af245d11STodd Fiala // FIXME put this behind a breakpoint protocol class that can be 1585af245d11STodd Fiala // set per architecture. Need ARM, MIPS support here. 1586af245d11STodd Fiala static const uint8_t g_i386_opcode[] = {0xCC}; 1587bb00d0b6SUlrich Weigand static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; 1588af245d11STodd Fiala 1589b9c1b51eSKate Stone switch (m_arch.GetMachine()) { 1590af245d11STodd Fiala case llvm::Triple::x86: 1591af245d11STodd Fiala case llvm::Triple::x86_64: 1592af245d11STodd Fiala actual_opcode_size = static_cast<uint32_t>(sizeof(g_i386_opcode)); 159397206d57SZachary Turner return Status(); 1594af245d11STodd Fiala 1595bb00d0b6SUlrich Weigand case llvm::Triple::systemz: 1596bb00d0b6SUlrich Weigand actual_opcode_size = static_cast<uint32_t>(sizeof(g_s390x_opcode)); 159797206d57SZachary Turner return Status(); 1598bb00d0b6SUlrich Weigand 1599ff7fd900STamas Berghammer case llvm::Triple::arm: 1600ff7fd900STamas Berghammer case llvm::Triple::aarch64: 1601e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64: 1602e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64el: 1603ce815e45SSagar Thakur case llvm::Triple::mips: 1604ce815e45SSagar Thakur case llvm::Triple::mipsel: 1605ff7fd900STamas Berghammer // On these architectures the PC don't get updated for breakpoint hits 1606c60c9452SJaydeep Patil actual_opcode_size = 0; 160797206d57SZachary Turner return Status(); 1608e8659b5dSMohit K. Bhakkad 1609af245d11STodd Fiala default: 1610af245d11STodd Fiala assert(false && "CPU type not supported!"); 161197206d57SZachary Turner return Status("CPU type not supported"); 1612af245d11STodd Fiala } 1613af245d11STodd Fiala } 1614af245d11STodd Fiala 161597206d57SZachary Turner Status NativeProcessLinux::SetBreakpoint(lldb::addr_t addr, uint32_t size, 1616b9c1b51eSKate Stone bool hardware) { 1617af245d11STodd Fiala if (hardware) 1618d5ffbad2SOmair Javaid return SetHardwareBreakpoint(addr, size); 1619af245d11STodd Fiala else 1620af245d11STodd Fiala return SetSoftwareBreakpoint(addr, size); 1621af245d11STodd Fiala } 1622af245d11STodd Fiala 162397206d57SZachary Turner Status NativeProcessLinux::RemoveBreakpoint(lldb::addr_t addr, bool hardware) { 1624d5ffbad2SOmair Javaid if (hardware) 1625d5ffbad2SOmair Javaid return RemoveHardwareBreakpoint(addr); 1626d5ffbad2SOmair Javaid else 1627d5ffbad2SOmair Javaid return NativeProcessProtocol::RemoveBreakpoint(addr); 1628d5ffbad2SOmair Javaid } 1629d5ffbad2SOmair Javaid 163097206d57SZachary Turner Status NativeProcessLinux::GetSoftwareBreakpointTrapOpcode( 1631b9c1b51eSKate Stone size_t trap_opcode_size_hint, size_t &actual_opcode_size, 1632b9c1b51eSKate Stone const uint8_t *&trap_opcode_bytes) { 163363c8be95STamas Berghammer // FIXME put this behind a breakpoint protocol class that can be set per 163463c8be95STamas Berghammer // architecture. Need MIPS support here. 16352afc5966STodd Fiala static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4}; 1636be379e15STamas Berghammer // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the 1637be379e15STamas Berghammer // linux kernel does otherwise. 1638be379e15STamas Berghammer static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7}; 1639af245d11STodd Fiala static const uint8_t g_i386_opcode[] = {0xCC}; 16403df471c3SMohit K. Bhakkad static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d}; 16412c2acf96SMohit K. Bhakkad static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00}; 1642bb00d0b6SUlrich Weigand static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; 1643be379e15STamas Berghammer static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde}; 1644af245d11STodd Fiala 1645b9c1b51eSKate Stone switch (m_arch.GetMachine()) { 16462afc5966STodd Fiala case llvm::Triple::aarch64: 16472afc5966STodd Fiala trap_opcode_bytes = g_aarch64_opcode; 16482afc5966STodd Fiala actual_opcode_size = sizeof(g_aarch64_opcode); 164997206d57SZachary Turner return Status(); 16502afc5966STodd Fiala 165163c8be95STamas Berghammer case llvm::Triple::arm: 1652b9c1b51eSKate Stone switch (trap_opcode_size_hint) { 165363c8be95STamas Berghammer case 2: 165463c8be95STamas Berghammer trap_opcode_bytes = g_thumb_breakpoint_opcode; 165563c8be95STamas Berghammer actual_opcode_size = sizeof(g_thumb_breakpoint_opcode); 165697206d57SZachary Turner return Status(); 165763c8be95STamas Berghammer case 4: 165863c8be95STamas Berghammer trap_opcode_bytes = g_arm_breakpoint_opcode; 165963c8be95STamas Berghammer actual_opcode_size = sizeof(g_arm_breakpoint_opcode); 166097206d57SZachary Turner return Status(); 166163c8be95STamas Berghammer default: 166263c8be95STamas Berghammer assert(false && "Unrecognised trap opcode size hint!"); 166397206d57SZachary Turner return Status("Unrecognised trap opcode size hint!"); 166463c8be95STamas Berghammer } 166563c8be95STamas Berghammer 1666af245d11STodd Fiala case llvm::Triple::x86: 1667af245d11STodd Fiala case llvm::Triple::x86_64: 1668af245d11STodd Fiala trap_opcode_bytes = g_i386_opcode; 1669af245d11STodd Fiala actual_opcode_size = sizeof(g_i386_opcode); 167097206d57SZachary Turner return Status(); 1671af245d11STodd Fiala 1672ce815e45SSagar Thakur case llvm::Triple::mips: 16733df471c3SMohit K. Bhakkad case llvm::Triple::mips64: 16743df471c3SMohit K. Bhakkad trap_opcode_bytes = g_mips64_opcode; 16753df471c3SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64_opcode); 167697206d57SZachary Turner return Status(); 16773df471c3SMohit K. Bhakkad 1678ce815e45SSagar Thakur case llvm::Triple::mipsel: 16792c2acf96SMohit K. Bhakkad case llvm::Triple::mips64el: 16802c2acf96SMohit K. Bhakkad trap_opcode_bytes = g_mips64el_opcode; 16812c2acf96SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64el_opcode); 168297206d57SZachary Turner return Status(); 16832c2acf96SMohit K. Bhakkad 1684bb00d0b6SUlrich Weigand case llvm::Triple::systemz: 1685bb00d0b6SUlrich Weigand trap_opcode_bytes = g_s390x_opcode; 1686bb00d0b6SUlrich Weigand actual_opcode_size = sizeof(g_s390x_opcode); 168797206d57SZachary Turner return Status(); 1688bb00d0b6SUlrich Weigand 1689af245d11STodd Fiala default: 1690af245d11STodd Fiala assert(false && "CPU type not supported!"); 169197206d57SZachary Turner return Status("CPU type not supported"); 1692af245d11STodd Fiala } 1693af245d11STodd Fiala } 1694af245d11STodd Fiala 1695af245d11STodd Fiala #if 0 1696af245d11STodd Fiala ProcessMessage::CrashReason 1697af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info) 1698af245d11STodd Fiala { 1699af245d11STodd Fiala ProcessMessage::CrashReason reason; 1700af245d11STodd Fiala assert(info->si_signo == SIGSEGV); 1701af245d11STodd Fiala 1702af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1703af245d11STodd Fiala 1704af245d11STodd Fiala switch (info->si_code) 1705af245d11STodd Fiala { 1706af245d11STodd Fiala default: 1707af245d11STodd Fiala assert(false && "unexpected si_code for SIGSEGV"); 1708af245d11STodd Fiala break; 1709af245d11STodd Fiala case SI_KERNEL: 1710af245d11STodd Fiala // Linux will occasionally send spurious SI_KERNEL codes. 1711af245d11STodd Fiala // (this is poorly documented in sigaction) 1712af245d11STodd Fiala // One way to get this is via unaligned SIMD loads. 1713af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; // for lack of anything better 1714af245d11STodd Fiala break; 1715af245d11STodd Fiala case SEGV_MAPERR: 1716af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; 1717af245d11STodd Fiala break; 1718af245d11STodd Fiala case SEGV_ACCERR: 1719af245d11STodd Fiala reason = ProcessMessage::ePrivilegedAddress; 1720af245d11STodd Fiala break; 1721af245d11STodd Fiala } 1722af245d11STodd Fiala 1723af245d11STodd Fiala return reason; 1724af245d11STodd Fiala } 1725af245d11STodd Fiala #endif 1726af245d11STodd Fiala 1727af245d11STodd Fiala #if 0 1728af245d11STodd Fiala ProcessMessage::CrashReason 1729af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info) 1730af245d11STodd Fiala { 1731af245d11STodd Fiala ProcessMessage::CrashReason reason; 1732af245d11STodd Fiala assert(info->si_signo == SIGILL); 1733af245d11STodd Fiala 1734af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1735af245d11STodd Fiala 1736af245d11STodd Fiala switch (info->si_code) 1737af245d11STodd Fiala { 1738af245d11STodd Fiala default: 1739af245d11STodd Fiala assert(false && "unexpected si_code for SIGILL"); 1740af245d11STodd Fiala break; 1741af245d11STodd Fiala case ILL_ILLOPC: 1742af245d11STodd Fiala reason = ProcessMessage::eIllegalOpcode; 1743af245d11STodd Fiala break; 1744af245d11STodd Fiala case ILL_ILLOPN: 1745af245d11STodd Fiala reason = ProcessMessage::eIllegalOperand; 1746af245d11STodd Fiala break; 1747af245d11STodd Fiala case ILL_ILLADR: 1748af245d11STodd Fiala reason = ProcessMessage::eIllegalAddressingMode; 1749af245d11STodd Fiala break; 1750af245d11STodd Fiala case ILL_ILLTRP: 1751af245d11STodd Fiala reason = ProcessMessage::eIllegalTrap; 1752af245d11STodd Fiala break; 1753af245d11STodd Fiala case ILL_PRVOPC: 1754af245d11STodd Fiala reason = ProcessMessage::ePrivilegedOpcode; 1755af245d11STodd Fiala break; 1756af245d11STodd Fiala case ILL_PRVREG: 1757af245d11STodd Fiala reason = ProcessMessage::ePrivilegedRegister; 1758af245d11STodd Fiala break; 1759af245d11STodd Fiala case ILL_COPROC: 1760af245d11STodd Fiala reason = ProcessMessage::eCoprocessorError; 1761af245d11STodd Fiala break; 1762af245d11STodd Fiala case ILL_BADSTK: 1763af245d11STodd Fiala reason = ProcessMessage::eInternalStackError; 1764af245d11STodd Fiala break; 1765af245d11STodd Fiala } 1766af245d11STodd Fiala 1767af245d11STodd Fiala return reason; 1768af245d11STodd Fiala } 1769af245d11STodd Fiala #endif 1770af245d11STodd Fiala 1771af245d11STodd Fiala #if 0 1772af245d11STodd Fiala ProcessMessage::CrashReason 1773af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info) 1774af245d11STodd Fiala { 1775af245d11STodd Fiala ProcessMessage::CrashReason reason; 1776af245d11STodd Fiala assert(info->si_signo == SIGFPE); 1777af245d11STodd Fiala 1778af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1779af245d11STodd Fiala 1780af245d11STodd Fiala switch (info->si_code) 1781af245d11STodd Fiala { 1782af245d11STodd Fiala default: 1783af245d11STodd Fiala assert(false && "unexpected si_code for SIGFPE"); 1784af245d11STodd Fiala break; 1785af245d11STodd Fiala case FPE_INTDIV: 1786af245d11STodd Fiala reason = ProcessMessage::eIntegerDivideByZero; 1787af245d11STodd Fiala break; 1788af245d11STodd Fiala case FPE_INTOVF: 1789af245d11STodd Fiala reason = ProcessMessage::eIntegerOverflow; 1790af245d11STodd Fiala break; 1791af245d11STodd Fiala case FPE_FLTDIV: 1792af245d11STodd Fiala reason = ProcessMessage::eFloatDivideByZero; 1793af245d11STodd Fiala break; 1794af245d11STodd Fiala case FPE_FLTOVF: 1795af245d11STodd Fiala reason = ProcessMessage::eFloatOverflow; 1796af245d11STodd Fiala break; 1797af245d11STodd Fiala case FPE_FLTUND: 1798af245d11STodd Fiala reason = ProcessMessage::eFloatUnderflow; 1799af245d11STodd Fiala break; 1800af245d11STodd Fiala case FPE_FLTRES: 1801af245d11STodd Fiala reason = ProcessMessage::eFloatInexactResult; 1802af245d11STodd Fiala break; 1803af245d11STodd Fiala case FPE_FLTINV: 1804af245d11STodd Fiala reason = ProcessMessage::eFloatInvalidOperation; 1805af245d11STodd Fiala break; 1806af245d11STodd Fiala case FPE_FLTSUB: 1807af245d11STodd Fiala reason = ProcessMessage::eFloatSubscriptRange; 1808af245d11STodd Fiala break; 1809af245d11STodd Fiala } 1810af245d11STodd Fiala 1811af245d11STodd Fiala return reason; 1812af245d11STodd Fiala } 1813af245d11STodd Fiala #endif 1814af245d11STodd Fiala 1815af245d11STodd Fiala #if 0 1816af245d11STodd Fiala ProcessMessage::CrashReason 1817af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info) 1818af245d11STodd Fiala { 1819af245d11STodd Fiala ProcessMessage::CrashReason reason; 1820af245d11STodd Fiala assert(info->si_signo == SIGBUS); 1821af245d11STodd Fiala 1822af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1823af245d11STodd Fiala 1824af245d11STodd Fiala switch (info->si_code) 1825af245d11STodd Fiala { 1826af245d11STodd Fiala default: 1827af245d11STodd Fiala assert(false && "unexpected si_code for SIGBUS"); 1828af245d11STodd Fiala break; 1829af245d11STodd Fiala case BUS_ADRALN: 1830af245d11STodd Fiala reason = ProcessMessage::eIllegalAlignment; 1831af245d11STodd Fiala break; 1832af245d11STodd Fiala case BUS_ADRERR: 1833af245d11STodd Fiala reason = ProcessMessage::eIllegalAddress; 1834af245d11STodd Fiala break; 1835af245d11STodd Fiala case BUS_OBJERR: 1836af245d11STodd Fiala reason = ProcessMessage::eHardwareError; 1837af245d11STodd Fiala break; 1838af245d11STodd Fiala } 1839af245d11STodd Fiala 1840af245d11STodd Fiala return reason; 1841af245d11STodd Fiala } 1842af245d11STodd Fiala #endif 1843af245d11STodd Fiala 184497206d57SZachary Turner Status NativeProcessLinux::ReadMemory(lldb::addr_t addr, void *buf, size_t size, 1845b9c1b51eSKate Stone size_t &bytes_read) { 1846df7c6995SPavel Labath if (ProcessVmReadvSupported()) { 1847b9c1b51eSKate Stone // The process_vm_readv path is about 50 times faster than ptrace api. We 1848b9c1b51eSKate Stone // want to use 1849df7c6995SPavel Labath // this syscall if it is supported. 1850df7c6995SPavel Labath 1851df7c6995SPavel Labath const ::pid_t pid = GetID(); 1852df7c6995SPavel Labath 1853df7c6995SPavel Labath struct iovec local_iov, remote_iov; 1854df7c6995SPavel Labath local_iov.iov_base = buf; 1855df7c6995SPavel Labath local_iov.iov_len = size; 1856df7c6995SPavel Labath remote_iov.iov_base = reinterpret_cast<void *>(addr); 1857df7c6995SPavel Labath remote_iov.iov_len = size; 1858df7c6995SPavel Labath 1859df7c6995SPavel Labath bytes_read = process_vm_readv(pid, &local_iov, 1, &remote_iov, 1, 0); 1860df7c6995SPavel Labath const bool success = bytes_read == size; 1861df7c6995SPavel Labath 1862a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1863a6321a8eSPavel Labath LLDB_LOG(log, 1864a6321a8eSPavel Labath "using process_vm_readv to read {0} bytes from inferior " 1865a6321a8eSPavel Labath "address {1:x}: {2}", 186610c41f37SPavel Labath size, addr, success ? "Success" : llvm::sys::StrError(errno)); 1867df7c6995SPavel Labath 1868df7c6995SPavel Labath if (success) 186997206d57SZachary Turner return Status(); 1870a6321a8eSPavel Labath // else the call failed for some reason, let's retry the read using ptrace 1871b9c1b51eSKate Stone // api. 1872df7c6995SPavel Labath } 1873df7c6995SPavel Labath 187419cbe96aSPavel Labath unsigned char *dst = static_cast<unsigned char *>(buf); 187519cbe96aSPavel Labath size_t remainder; 187619cbe96aSPavel Labath long data; 187719cbe96aSPavel Labath 1878a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 1879a6321a8eSPavel Labath LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 188019cbe96aSPavel Labath 1881b9c1b51eSKate Stone for (bytes_read = 0; bytes_read < size; bytes_read += remainder) { 188297206d57SZachary Turner Status error = NativeProcessLinux::PtraceWrapper( 1883b9c1b51eSKate Stone PTRACE_PEEKDATA, GetID(), (void *)addr, nullptr, 0, &data); 1884a6321a8eSPavel Labath if (error.Fail()) 188519cbe96aSPavel Labath return error; 188619cbe96aSPavel Labath 188719cbe96aSPavel Labath remainder = size - bytes_read; 188819cbe96aSPavel Labath remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; 188919cbe96aSPavel Labath 189019cbe96aSPavel Labath // Copy the data into our buffer 1891f6ef187bSMohit K. Bhakkad memcpy(dst, &data, remainder); 189219cbe96aSPavel Labath 1893a6321a8eSPavel Labath LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data); 189419cbe96aSPavel Labath addr += k_ptrace_word_size; 189519cbe96aSPavel Labath dst += k_ptrace_word_size; 189619cbe96aSPavel Labath } 189797206d57SZachary Turner return Status(); 1898af245d11STodd Fiala } 1899af245d11STodd Fiala 190097206d57SZachary Turner Status NativeProcessLinux::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, 1901b9c1b51eSKate Stone size_t size, 1902b9c1b51eSKate Stone size_t &bytes_read) { 190397206d57SZachary Turner Status error = ReadMemory(addr, buf, size, bytes_read); 1904b9c1b51eSKate Stone if (error.Fail()) 1905b9c1b51eSKate Stone return error; 19063eb4b458SChaoren Lin return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); 19073eb4b458SChaoren Lin } 19083eb4b458SChaoren Lin 190997206d57SZachary Turner Status NativeProcessLinux::WriteMemory(lldb::addr_t addr, const void *buf, 1910b9c1b51eSKate Stone size_t size, size_t &bytes_written) { 191119cbe96aSPavel Labath const unsigned char *src = static_cast<const unsigned char *>(buf); 191219cbe96aSPavel Labath size_t remainder; 191397206d57SZachary Turner Status error; 191419cbe96aSPavel Labath 1915a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 1916a6321a8eSPavel Labath LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 191719cbe96aSPavel Labath 1918b9c1b51eSKate Stone for (bytes_written = 0; bytes_written < size; bytes_written += remainder) { 191919cbe96aSPavel Labath remainder = size - bytes_written; 192019cbe96aSPavel Labath remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; 192119cbe96aSPavel Labath 1922b9c1b51eSKate Stone if (remainder == k_ptrace_word_size) { 192319cbe96aSPavel Labath unsigned long data = 0; 1924f6ef187bSMohit K. Bhakkad memcpy(&data, src, k_ptrace_word_size); 192519cbe96aSPavel Labath 1926a6321a8eSPavel Labath LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data); 1927b9c1b51eSKate Stone error = NativeProcessLinux::PtraceWrapper(PTRACE_POKEDATA, GetID(), 1928b9c1b51eSKate Stone (void *)addr, (void *)data); 1929a6321a8eSPavel Labath if (error.Fail()) 193019cbe96aSPavel Labath return error; 1931b9c1b51eSKate Stone } else { 193219cbe96aSPavel Labath unsigned char buff[8]; 193319cbe96aSPavel Labath size_t bytes_read; 193419cbe96aSPavel Labath error = ReadMemory(addr, buff, k_ptrace_word_size, bytes_read); 1935a6321a8eSPavel Labath if (error.Fail()) 193619cbe96aSPavel Labath return error; 193719cbe96aSPavel Labath 193819cbe96aSPavel Labath memcpy(buff, src, remainder); 193919cbe96aSPavel Labath 194019cbe96aSPavel Labath size_t bytes_written_rec; 194119cbe96aSPavel Labath error = WriteMemory(addr, buff, k_ptrace_word_size, bytes_written_rec); 1942a6321a8eSPavel Labath if (error.Fail()) 194319cbe96aSPavel Labath return error; 194419cbe96aSPavel Labath 1945a6321a8eSPavel Labath LLDB_LOG(log, "[{0:x}]:{1:x} ({2:x})", addr, *(const unsigned long *)src, 1946b9c1b51eSKate Stone *(unsigned long *)buff); 194719cbe96aSPavel Labath } 194819cbe96aSPavel Labath 194919cbe96aSPavel Labath addr += k_ptrace_word_size; 195019cbe96aSPavel Labath src += k_ptrace_word_size; 195119cbe96aSPavel Labath } 195219cbe96aSPavel Labath return error; 1953af245d11STodd Fiala } 1954af245d11STodd Fiala 195597206d57SZachary Turner Status NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) { 195619cbe96aSPavel Labath return PtraceWrapper(PTRACE_GETSIGINFO, tid, nullptr, siginfo); 1957af245d11STodd Fiala } 1958af245d11STodd Fiala 195997206d57SZachary Turner Status NativeProcessLinux::GetEventMessage(lldb::tid_t tid, 1960b9c1b51eSKate Stone unsigned long *message) { 196119cbe96aSPavel Labath return PtraceWrapper(PTRACE_GETEVENTMSG, tid, nullptr, message); 1962af245d11STodd Fiala } 1963af245d11STodd Fiala 196497206d57SZachary Turner Status NativeProcessLinux::Detach(lldb::tid_t tid) { 196597ccc294SChaoren Lin if (tid == LLDB_INVALID_THREAD_ID) 196697206d57SZachary Turner return Status(); 196797ccc294SChaoren Lin 196819cbe96aSPavel Labath return PtraceWrapper(PTRACE_DETACH, tid); 1969af245d11STodd Fiala } 1970af245d11STodd Fiala 1971b9c1b51eSKate Stone bool NativeProcessLinux::HasThreadNoLock(lldb::tid_t thread_id) { 1972b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 1973af245d11STodd Fiala assert(thread_sp && "thread list should not contain NULL threads"); 1974b9c1b51eSKate Stone if (thread_sp->GetID() == thread_id) { 1975af245d11STodd Fiala // We have this thread. 1976af245d11STodd Fiala return true; 1977af245d11STodd Fiala } 1978af245d11STodd Fiala } 1979af245d11STodd Fiala 1980af245d11STodd Fiala // We don't have this thread. 1981af245d11STodd Fiala return false; 1982af245d11STodd Fiala } 1983af245d11STodd Fiala 1984b9c1b51eSKate Stone bool NativeProcessLinux::StopTrackingThread(lldb::tid_t thread_id) { 1985a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 1986a6321a8eSPavel Labath LLDB_LOG(log, "tid: {0})", thread_id); 19871dbc6c9cSPavel Labath 19881dbc6c9cSPavel Labath bool found = false; 1989b9c1b51eSKate Stone for (auto it = m_threads.begin(); it != m_threads.end(); ++it) { 1990b9c1b51eSKate Stone if (*it && ((*it)->GetID() == thread_id)) { 1991af245d11STodd Fiala m_threads.erase(it); 19921dbc6c9cSPavel Labath found = true; 19931dbc6c9cSPavel Labath break; 1994af245d11STodd Fiala } 1995af245d11STodd Fiala } 1996af245d11STodd Fiala 199799e37695SRavitheja Addepally if (found) 199899e37695SRavitheja Addepally StopTracingForThread(thread_id); 19999eb1ecb9SPavel Labath SignalIfAllThreadsStopped(); 20001dbc6c9cSPavel Labath return found; 2001af245d11STodd Fiala } 2002af245d11STodd Fiala 2003b9c1b51eSKate Stone NativeThreadLinuxSP NativeProcessLinux::AddThread(lldb::tid_t thread_id) { 2004a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); 2005a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id); 2006af245d11STodd Fiala 2007b9c1b51eSKate Stone assert(!HasThreadNoLock(thread_id) && 2008b9c1b51eSKate Stone "attempted to add a thread by id that already exists"); 2009af245d11STodd Fiala 2010af245d11STodd Fiala // If this is the first thread, save it as the current thread 2011af245d11STodd Fiala if (m_threads.empty()) 2012af245d11STodd Fiala SetCurrentThreadID(thread_id); 2013af245d11STodd Fiala 2014f9077782SPavel Labath auto thread_sp = std::make_shared<NativeThreadLinux>(this, thread_id); 2015af245d11STodd Fiala m_threads.push_back(thread_sp); 201699e37695SRavitheja Addepally 201799e37695SRavitheja Addepally if (m_pt_proces_trace_id != LLDB_INVALID_UID) { 201899e37695SRavitheja Addepally auto traceMonitor = ProcessorTraceMonitor::Create( 201999e37695SRavitheja Addepally GetID(), thread_id, m_pt_process_trace_config, true); 202099e37695SRavitheja Addepally if (traceMonitor) { 202199e37695SRavitheja Addepally m_pt_traced_thread_group.insert(thread_id); 202299e37695SRavitheja Addepally m_processor_trace_monitor.insert( 202399e37695SRavitheja Addepally std::make_pair(thread_id, std::move(*traceMonitor))); 202499e37695SRavitheja Addepally } else { 202599e37695SRavitheja Addepally LLDB_LOG(log, "failed to start trace on thread {0}", thread_id); 202699e37695SRavitheja Addepally Status error(traceMonitor.takeError()); 202799e37695SRavitheja Addepally LLDB_LOG(log, "error {0}", error); 202899e37695SRavitheja Addepally } 202999e37695SRavitheja Addepally } 203099e37695SRavitheja Addepally 2031af245d11STodd Fiala return thread_sp; 2032af245d11STodd Fiala } 2033af245d11STodd Fiala 203497206d57SZachary Turner Status 203597206d57SZachary Turner NativeProcessLinux::FixupBreakpointPCAsNeeded(NativeThreadLinux &thread) { 2036a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS)); 2037af245d11STodd Fiala 203897206d57SZachary Turner Status error; 2039af245d11STodd Fiala 2040b9c1b51eSKate Stone // Find out the size of a breakpoint (might depend on where we are in the 2041b9c1b51eSKate Stone // code). 2042b9cc0c75SPavel Labath NativeRegisterContextSP context_sp = thread.GetRegisterContext(); 2043b9c1b51eSKate Stone if (!context_sp) { 2044af245d11STodd Fiala error.SetErrorString("cannot get a NativeRegisterContext for the thread"); 2045a6321a8eSPavel Labath LLDB_LOG(log, "failed: {0}", error); 2046af245d11STodd Fiala return error; 2047af245d11STodd Fiala } 2048af245d11STodd Fiala 2049af245d11STodd Fiala uint32_t breakpoint_size = 0; 2050b9cc0c75SPavel Labath error = GetSoftwareBreakpointPCOffset(breakpoint_size); 2051b9c1b51eSKate Stone if (error.Fail()) { 2052a6321a8eSPavel Labath LLDB_LOG(log, "GetBreakpointSize() failed: {0}", error); 2053af245d11STodd Fiala return error; 2054a6321a8eSPavel Labath } else 2055a6321a8eSPavel Labath LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size); 2056af245d11STodd Fiala 2057b9c1b51eSKate Stone // First try probing for a breakpoint at a software breakpoint location: PC - 2058b9c1b51eSKate Stone // breakpoint size. 2059b9c1b51eSKate Stone const lldb::addr_t initial_pc_addr = 2060b9c1b51eSKate Stone context_sp->GetPCfromBreakpointLocation(); 2061af245d11STodd Fiala lldb::addr_t breakpoint_addr = initial_pc_addr; 2062b9c1b51eSKate Stone if (breakpoint_size > 0) { 2063af245d11STodd Fiala // Do not allow breakpoint probe to wrap around. 20643eb4b458SChaoren Lin if (breakpoint_addr >= breakpoint_size) 20653eb4b458SChaoren Lin breakpoint_addr -= breakpoint_size; 2066af245d11STodd Fiala } 2067af245d11STodd Fiala 2068af245d11STodd Fiala // Check if we stopped because of a breakpoint. 2069af245d11STodd Fiala NativeBreakpointSP breakpoint_sp; 2070af245d11STodd Fiala error = m_breakpoint_list.GetBreakpoint(breakpoint_addr, breakpoint_sp); 2071b9c1b51eSKate Stone if (!error.Success() || !breakpoint_sp) { 2072af245d11STodd Fiala // We didn't find one at a software probe location. Nothing to do. 2073a6321a8eSPavel Labath LLDB_LOG(log, 2074a6321a8eSPavel Labath "pid {0} no lldb breakpoint found at current pc with " 2075a6321a8eSPavel Labath "adjustment: {1}", 2076a6321a8eSPavel Labath GetID(), breakpoint_addr); 207797206d57SZachary Turner return Status(); 2078af245d11STodd Fiala } 2079af245d11STodd Fiala 2080af245d11STodd Fiala // If the breakpoint is not a software breakpoint, nothing to do. 2081b9c1b51eSKate Stone if (!breakpoint_sp->IsSoftwareBreakpoint()) { 2082a6321a8eSPavel Labath LLDB_LOG( 2083a6321a8eSPavel Labath log, 2084a6321a8eSPavel Labath "pid {0} breakpoint found at {1:x}, not software, nothing to adjust", 2085a6321a8eSPavel Labath GetID(), breakpoint_addr); 208697206d57SZachary Turner return Status(); 2087af245d11STodd Fiala } 2088af245d11STodd Fiala 2089af245d11STodd Fiala // 2090af245d11STodd Fiala // We have a software breakpoint and need to adjust the PC. 2091af245d11STodd Fiala // 2092af245d11STodd Fiala 2093af245d11STodd Fiala // Sanity check. 2094b9c1b51eSKate Stone if (breakpoint_size == 0) { 2095af245d11STodd Fiala // Nothing to do! How did we get here? 2096a6321a8eSPavel Labath LLDB_LOG(log, 2097a6321a8eSPavel Labath "pid {0} breakpoint found at {1:x}, it is software, but the " 2098a6321a8eSPavel Labath "size is zero, nothing to do (unexpected)", 2099a6321a8eSPavel Labath GetID(), breakpoint_addr); 210097206d57SZachary Turner return Status(); 2101af245d11STodd Fiala } 2102af245d11STodd Fiala 2103af245d11STodd Fiala // Change the program counter. 2104a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(), 2105a6321a8eSPavel Labath thread.GetID(), initial_pc_addr, breakpoint_addr); 2106af245d11STodd Fiala 2107af245d11STodd Fiala error = context_sp->SetPC(breakpoint_addr); 2108b9c1b51eSKate Stone if (error.Fail()) { 2109a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(), 2110a6321a8eSPavel Labath thread.GetID(), error); 2111af245d11STodd Fiala return error; 2112af245d11STodd Fiala } 2113af245d11STodd Fiala 2114af245d11STodd Fiala return error; 2115af245d11STodd Fiala } 2116fa03ad2eSChaoren Lin 211797206d57SZachary Turner Status NativeProcessLinux::GetLoadedModuleFileSpec(const char *module_path, 2118b9c1b51eSKate Stone FileSpec &file_spec) { 211997206d57SZachary Turner Status error = PopulateMemoryRegionCache(); 2120a6f5795aSTamas Berghammer if (error.Fail()) 2121a6f5795aSTamas Berghammer return error; 2122a6f5795aSTamas Berghammer 21237cb18bf5STamas Berghammer FileSpec module_file_spec(module_path, true); 21247cb18bf5STamas Berghammer 21257cb18bf5STamas Berghammer file_spec.Clear(); 2126a6f5795aSTamas Berghammer for (const auto &it : m_mem_region_cache) { 2127a6f5795aSTamas Berghammer if (it.second.GetFilename() == module_file_spec.GetFilename()) { 2128a6f5795aSTamas Berghammer file_spec = it.second; 212997206d57SZachary Turner return Status(); 2130a6f5795aSTamas Berghammer } 2131a6f5795aSTamas Berghammer } 213297206d57SZachary Turner return Status("Module file (%s) not found in /proc/%" PRIu64 "/maps file!", 21337cb18bf5STamas Berghammer module_file_spec.GetFilename().AsCString(), GetID()); 21347cb18bf5STamas Berghammer } 2135c076559aSPavel Labath 213697206d57SZachary Turner Status NativeProcessLinux::GetFileLoadAddress(const llvm::StringRef &file_name, 2137b9c1b51eSKate Stone lldb::addr_t &load_addr) { 2138783bfc8cSTamas Berghammer load_addr = LLDB_INVALID_ADDRESS; 213997206d57SZachary Turner Status error = PopulateMemoryRegionCache(); 2140a6f5795aSTamas Berghammer if (error.Fail()) 2141783bfc8cSTamas Berghammer return error; 2142a6f5795aSTamas Berghammer 2143a6f5795aSTamas Berghammer FileSpec file(file_name, false); 2144a6f5795aSTamas Berghammer for (const auto &it : m_mem_region_cache) { 2145a6f5795aSTamas Berghammer if (it.second == file) { 2146a6f5795aSTamas Berghammer load_addr = it.first.GetRange().GetRangeBase(); 214797206d57SZachary Turner return Status(); 2148a6f5795aSTamas Berghammer } 2149a6f5795aSTamas Berghammer } 215097206d57SZachary Turner return Status("No load address found for specified file."); 2151783bfc8cSTamas Berghammer } 2152783bfc8cSTamas Berghammer 2153b9c1b51eSKate Stone NativeThreadLinuxSP NativeProcessLinux::GetThreadByID(lldb::tid_t tid) { 2154b9c1b51eSKate Stone return std::static_pointer_cast<NativeThreadLinux>( 2155b9c1b51eSKate Stone NativeProcessProtocol::GetThreadByID(tid)); 2156f9077782SPavel Labath } 2157f9077782SPavel Labath 215897206d57SZachary Turner Status NativeProcessLinux::ResumeThread(NativeThreadLinux &thread, 2159b9c1b51eSKate Stone lldb::StateType state, int signo) { 2160a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2161a6321a8eSPavel Labath LLDB_LOG(log, "tid: {0}", thread.GetID()); 2162c076559aSPavel Labath 2163c076559aSPavel Labath // Before we do the resume below, first check if we have a pending 2164108c325dSPavel Labath // stop notification that is currently waiting for 21650e1d729bSPavel Labath // all threads to stop. This is potentially a buggy situation since 2166c076559aSPavel Labath // we're ostensibly waiting for threads to stop before we send out the 2167c076559aSPavel Labath // pending notification, and here we are resuming one before we send 2168c076559aSPavel Labath // out the pending stop notification. 2169a6321a8eSPavel Labath if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) { 2170a6321a8eSPavel Labath LLDB_LOG(log, 2171a6321a8eSPavel Labath "about to resume tid {0} per explicit request but we have a " 2172a6321a8eSPavel Labath "pending stop notification (tid {1}) that is actively " 2173a6321a8eSPavel Labath "waiting for this thread to stop. Valid sequence of events?", 2174a6321a8eSPavel Labath thread.GetID(), m_pending_notification_tid); 2175c076559aSPavel Labath } 2176c076559aSPavel Labath 2177c076559aSPavel Labath // Request a resume. We expect this to be synchronous and the system 2178c076559aSPavel Labath // to reflect it is running after this completes. 2179b9c1b51eSKate Stone switch (state) { 2180b9c1b51eSKate Stone case eStateRunning: { 2181605b51b8SPavel Labath const auto resume_result = thread.Resume(signo); 21820e1d729bSPavel Labath if (resume_result.Success()) 21830e1d729bSPavel Labath SetState(eStateRunning, true); 21840e1d729bSPavel Labath return resume_result; 2185c076559aSPavel Labath } 2186b9c1b51eSKate Stone case eStateStepping: { 2187605b51b8SPavel Labath const auto step_result = thread.SingleStep(signo); 21880e1d729bSPavel Labath if (step_result.Success()) 21890e1d729bSPavel Labath SetState(eStateRunning, true); 21900e1d729bSPavel Labath return step_result; 21910e1d729bSPavel Labath } 21920e1d729bSPavel Labath default: 21938198db30SPavel Labath LLDB_LOG(log, "Unhandled state {0}.", state); 21940e1d729bSPavel Labath llvm_unreachable("Unhandled state for resume"); 21950e1d729bSPavel Labath } 2196c076559aSPavel Labath } 2197c076559aSPavel Labath 2198c076559aSPavel Labath //===----------------------------------------------------------------------===// 2199c076559aSPavel Labath 2200b9c1b51eSKate Stone void NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid) { 2201a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2202a6321a8eSPavel Labath LLDB_LOG(log, "about to process event: (triggering_tid: {0})", 2203a6321a8eSPavel Labath triggering_tid); 2204c076559aSPavel Labath 22050e1d729bSPavel Labath m_pending_notification_tid = triggering_tid; 22060e1d729bSPavel Labath 22070e1d729bSPavel Labath // Request a stop for all the thread stops that need to be stopped 22080e1d729bSPavel Labath // and are not already known to be stopped. 2209b9c1b51eSKate Stone for (const auto &thread_sp : m_threads) { 22100e1d729bSPavel Labath if (StateIsRunningState(thread_sp->GetState())) 22110e1d729bSPavel Labath static_pointer_cast<NativeThreadLinux>(thread_sp)->RequestStop(); 22120e1d729bSPavel Labath } 22130e1d729bSPavel Labath 22140e1d729bSPavel Labath SignalIfAllThreadsStopped(); 2215a6321a8eSPavel Labath LLDB_LOG(log, "event processing done"); 2216c076559aSPavel Labath } 2217c076559aSPavel Labath 2218b9c1b51eSKate Stone void NativeProcessLinux::SignalIfAllThreadsStopped() { 22190e1d729bSPavel Labath if (m_pending_notification_tid == LLDB_INVALID_THREAD_ID) 22200e1d729bSPavel Labath return; // No pending notification. Nothing to do. 22210e1d729bSPavel Labath 2222b9c1b51eSKate Stone for (const auto &thread_sp : m_threads) { 22230e1d729bSPavel Labath if (StateIsRunningState(thread_sp->GetState())) 22240e1d729bSPavel Labath return; // Some threads are still running. Don't signal yet. 22250e1d729bSPavel Labath } 22260e1d729bSPavel Labath 22270e1d729bSPavel Labath // We have a pending notification and all threads have stopped. 2228b9c1b51eSKate Stone Log *log( 2229b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 22309eb1ecb9SPavel Labath 2231b9c1b51eSKate Stone // Clear any temporary breakpoints we used to implement software single 2232b9c1b51eSKate Stone // stepping. 2233b9c1b51eSKate Stone for (const auto &thread_info : m_threads_stepping_with_breakpoint) { 223497206d57SZachary Turner Status error = RemoveBreakpoint(thread_info.second); 22359eb1ecb9SPavel Labath if (error.Fail()) 2236a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0} remove stepping breakpoint: {1}", 2237a6321a8eSPavel Labath thread_info.first, error); 22389eb1ecb9SPavel Labath } 22399eb1ecb9SPavel Labath m_threads_stepping_with_breakpoint.clear(); 22409eb1ecb9SPavel Labath 22419eb1ecb9SPavel Labath // Notify the delegate about the stop 22420e1d729bSPavel Labath SetCurrentThreadID(m_pending_notification_tid); 2243ed89c7feSPavel Labath SetState(StateType::eStateStopped, true); 22440e1d729bSPavel Labath m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 2245c076559aSPavel Labath } 2246c076559aSPavel Labath 2247b9c1b51eSKate Stone void NativeProcessLinux::ThreadWasCreated(NativeThreadLinux &thread) { 2248a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2249a6321a8eSPavel Labath LLDB_LOG(log, "tid: {0}", thread.GetID()); 22501dbc6c9cSPavel Labath 2251b9c1b51eSKate Stone if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID && 2252b9c1b51eSKate Stone StateIsRunningState(thread.GetState())) { 2253b9c1b51eSKate Stone // We will need to wait for this new thread to stop as well before firing 2254b9c1b51eSKate Stone // the 2255c076559aSPavel Labath // notification. 2256f9077782SPavel Labath thread.RequestStop(); 2257c076559aSPavel Labath } 2258c076559aSPavel Labath } 2259068f8a7eSTamas Berghammer 2260b9c1b51eSKate Stone void NativeProcessLinux::SigchldHandler() { 2261a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 226219cbe96aSPavel Labath // Process all pending waitpid notifications. 2263b9c1b51eSKate Stone while (true) { 226419cbe96aSPavel Labath int status = -1; 2265c1a6b128SPavel Labath ::pid_t wait_pid = llvm::sys::RetryAfterSignal(-1, ::waitpid, -1, &status, 2266c1a6b128SPavel Labath __WALL | __WNOTHREAD | WNOHANG); 226719cbe96aSPavel Labath 226819cbe96aSPavel Labath if (wait_pid == 0) 226919cbe96aSPavel Labath break; // We are done. 227019cbe96aSPavel Labath 2271b9c1b51eSKate Stone if (wait_pid == -1) { 227297206d57SZachary Turner Status error(errno, eErrorTypePOSIX); 2273a6321a8eSPavel Labath LLDB_LOG(log, "waitpid (-1, &status, _) failed: {0}", error); 227419cbe96aSPavel Labath break; 227519cbe96aSPavel Labath } 227619cbe96aSPavel Labath 22773508fc8cSPavel Labath WaitStatus wait_status = WaitStatus::Decode(status); 22783508fc8cSPavel Labath bool exited = wait_status.type == WaitStatus::Exit || 22793508fc8cSPavel Labath (wait_status.type == WaitStatus::Signal && 22803508fc8cSPavel Labath wait_pid == static_cast<::pid_t>(GetID())); 228119cbe96aSPavel Labath 22823508fc8cSPavel Labath LLDB_LOG( 22833508fc8cSPavel Labath log, 22843508fc8cSPavel Labath "waitpid (-1, &status, _) => pid = {0}, status = {1}, exited = {2}", 22853508fc8cSPavel Labath wait_pid, wait_status, exited); 228619cbe96aSPavel Labath 22873508fc8cSPavel Labath MonitorCallback(wait_pid, exited, wait_status); 228819cbe96aSPavel Labath } 2289068f8a7eSTamas Berghammer } 2290068f8a7eSTamas Berghammer 2291068f8a7eSTamas Berghammer // Wrapper for ptrace to catch errors and log calls. 2292b9c1b51eSKate Stone // Note that ptrace sets errno on error because -1 can be a valid result (i.e. 2293b9c1b51eSKate Stone // for PTRACE_PEEK*) 229497206d57SZachary Turner Status NativeProcessLinux::PtraceWrapper(int req, lldb::pid_t pid, void *addr, 2295b9c1b51eSKate Stone void *data, size_t data_size, 2296b9c1b51eSKate Stone long *result) { 229797206d57SZachary Turner Status error; 22984a9babb2SPavel Labath long int ret; 2299068f8a7eSTamas Berghammer 2300068f8a7eSTamas Berghammer Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 2301068f8a7eSTamas Berghammer 2302068f8a7eSTamas Berghammer PtraceDisplayBytes(req, data, data_size); 2303068f8a7eSTamas Berghammer 2304068f8a7eSTamas Berghammer errno = 0; 2305068f8a7eSTamas Berghammer if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) 2306b9c1b51eSKate Stone ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), 2307b9c1b51eSKate Stone *(unsigned int *)addr, data); 2308068f8a7eSTamas Berghammer else 2309b9c1b51eSKate Stone ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), 2310b9c1b51eSKate Stone addr, data); 2311068f8a7eSTamas Berghammer 23124a9babb2SPavel Labath if (ret == -1) 2313068f8a7eSTamas Berghammer error.SetErrorToErrno(); 2314068f8a7eSTamas Berghammer 23154a9babb2SPavel Labath if (result) 23164a9babb2SPavel Labath *result = ret; 23174a9babb2SPavel Labath 231828096200SPavel Labath LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3}, {4})={5:x}", req, pid, addr, data, 231928096200SPavel Labath data_size, ret); 2320068f8a7eSTamas Berghammer 2321068f8a7eSTamas Berghammer PtraceDisplayBytes(req, data, data_size); 2322068f8a7eSTamas Berghammer 2323a6321a8eSPavel Labath if (error.Fail()) 2324a6321a8eSPavel Labath LLDB_LOG(log, "ptrace() failed: {0}", error); 2325068f8a7eSTamas Berghammer 23264a9babb2SPavel Labath return error; 2327068f8a7eSTamas Berghammer } 232899e37695SRavitheja Addepally 232999e37695SRavitheja Addepally llvm::Expected<ProcessorTraceMonitor &> 233099e37695SRavitheja Addepally NativeProcessLinux::LookupProcessorTraceInstance(lldb::user_id_t traceid, 233199e37695SRavitheja Addepally lldb::tid_t thread) { 233299e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 233399e37695SRavitheja Addepally if (thread == LLDB_INVALID_THREAD_ID && traceid == m_pt_proces_trace_id) { 233499e37695SRavitheja Addepally LLDB_LOG(log, "thread not specified: {0}", traceid); 233599e37695SRavitheja Addepally return Status("tracing not active thread not specified").ToError(); 233699e37695SRavitheja Addepally } 233799e37695SRavitheja Addepally 233899e37695SRavitheja Addepally for (auto& iter : m_processor_trace_monitor) { 233999e37695SRavitheja Addepally if (traceid == iter.second->GetTraceID() && 234099e37695SRavitheja Addepally (thread == iter.first || thread == LLDB_INVALID_THREAD_ID)) 234199e37695SRavitheja Addepally return *(iter.second); 234299e37695SRavitheja Addepally } 234399e37695SRavitheja Addepally 234499e37695SRavitheja Addepally LLDB_LOG(log, "traceid not being traced: {0}", traceid); 234599e37695SRavitheja Addepally return Status("tracing not active for this thread").ToError(); 234699e37695SRavitheja Addepally } 234799e37695SRavitheja Addepally 234899e37695SRavitheja Addepally Status NativeProcessLinux::GetMetaData(lldb::user_id_t traceid, 234999e37695SRavitheja Addepally lldb::tid_t thread, 235099e37695SRavitheja Addepally llvm::MutableArrayRef<uint8_t> &buffer, 235199e37695SRavitheja Addepally size_t offset) { 235299e37695SRavitheja Addepally TraceOptions trace_options; 235399e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 235499e37695SRavitheja Addepally Status error; 235599e37695SRavitheja Addepally 235699e37695SRavitheja Addepally LLDB_LOG(log, "traceid {0}", traceid); 235799e37695SRavitheja Addepally 235899e37695SRavitheja Addepally auto perf_monitor = LookupProcessorTraceInstance(traceid, thread); 235999e37695SRavitheja Addepally if (!perf_monitor) { 236099e37695SRavitheja Addepally LLDB_LOG(log, "traceid not being traced: {0}", traceid); 236199e37695SRavitheja Addepally buffer = buffer.slice(buffer.size()); 236299e37695SRavitheja Addepally error = perf_monitor.takeError(); 236399e37695SRavitheja Addepally return error; 236499e37695SRavitheja Addepally } 236599e37695SRavitheja Addepally return (*perf_monitor).ReadPerfTraceData(buffer, offset); 236699e37695SRavitheja Addepally } 236799e37695SRavitheja Addepally 236899e37695SRavitheja Addepally Status NativeProcessLinux::GetData(lldb::user_id_t traceid, lldb::tid_t thread, 236999e37695SRavitheja Addepally llvm::MutableArrayRef<uint8_t> &buffer, 237099e37695SRavitheja Addepally size_t offset) { 237199e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 237299e37695SRavitheja Addepally Status error; 237399e37695SRavitheja Addepally 237499e37695SRavitheja Addepally LLDB_LOG(log, "traceid {0}", traceid); 237599e37695SRavitheja Addepally 237699e37695SRavitheja Addepally auto perf_monitor = LookupProcessorTraceInstance(traceid, thread); 237799e37695SRavitheja Addepally if (!perf_monitor) { 237899e37695SRavitheja Addepally LLDB_LOG(log, "traceid not being traced: {0}", traceid); 237999e37695SRavitheja Addepally buffer = buffer.slice(buffer.size()); 238099e37695SRavitheja Addepally error = perf_monitor.takeError(); 238199e37695SRavitheja Addepally return error; 238299e37695SRavitheja Addepally } 238399e37695SRavitheja Addepally return (*perf_monitor).ReadPerfTraceAux(buffer, offset); 238499e37695SRavitheja Addepally } 238599e37695SRavitheja Addepally 238699e37695SRavitheja Addepally Status NativeProcessLinux::GetTraceConfig(lldb::user_id_t traceid, 238799e37695SRavitheja Addepally TraceOptions &config) { 238899e37695SRavitheja Addepally Status error; 238999e37695SRavitheja Addepally if (config.getThreadID() == LLDB_INVALID_THREAD_ID && 239099e37695SRavitheja Addepally m_pt_proces_trace_id == traceid) { 239199e37695SRavitheja Addepally if (m_pt_proces_trace_id == LLDB_INVALID_UID) { 239299e37695SRavitheja Addepally error.SetErrorString("tracing not active for this process"); 239399e37695SRavitheja Addepally return error; 239499e37695SRavitheja Addepally } 239599e37695SRavitheja Addepally config = m_pt_process_trace_config; 239699e37695SRavitheja Addepally } else { 239799e37695SRavitheja Addepally auto perf_monitor = 239899e37695SRavitheja Addepally LookupProcessorTraceInstance(traceid, config.getThreadID()); 239999e37695SRavitheja Addepally if (!perf_monitor) { 240099e37695SRavitheja Addepally error = perf_monitor.takeError(); 240199e37695SRavitheja Addepally return error; 240299e37695SRavitheja Addepally } 240399e37695SRavitheja Addepally error = (*perf_monitor).GetTraceConfig(config); 240499e37695SRavitheja Addepally } 240599e37695SRavitheja Addepally return error; 240699e37695SRavitheja Addepally } 240799e37695SRavitheja Addepally 240899e37695SRavitheja Addepally lldb::user_id_t 240999e37695SRavitheja Addepally NativeProcessLinux::StartTraceGroup(const TraceOptions &config, 241099e37695SRavitheja Addepally Status &error) { 241199e37695SRavitheja Addepally 241299e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 241399e37695SRavitheja Addepally if (config.getType() != TraceType::eTraceTypeProcessorTrace) 241499e37695SRavitheja Addepally return LLDB_INVALID_UID; 241599e37695SRavitheja Addepally 241699e37695SRavitheja Addepally if (m_pt_proces_trace_id != LLDB_INVALID_UID) { 241799e37695SRavitheja Addepally error.SetErrorString("tracing already active on this process"); 241899e37695SRavitheja Addepally return m_pt_proces_trace_id; 241999e37695SRavitheja Addepally } 242099e37695SRavitheja Addepally 242199e37695SRavitheja Addepally for (const auto &thread_sp : m_threads) { 242299e37695SRavitheja Addepally if (auto traceInstance = ProcessorTraceMonitor::Create( 242399e37695SRavitheja Addepally GetID(), thread_sp->GetID(), config, true)) { 242499e37695SRavitheja Addepally m_pt_traced_thread_group.insert(thread_sp->GetID()); 242599e37695SRavitheja Addepally m_processor_trace_monitor.insert( 242699e37695SRavitheja Addepally std::make_pair(thread_sp->GetID(), std::move(*traceInstance))); 242799e37695SRavitheja Addepally } 242899e37695SRavitheja Addepally } 242999e37695SRavitheja Addepally 243099e37695SRavitheja Addepally m_pt_process_trace_config = config; 243199e37695SRavitheja Addepally error = ProcessorTraceMonitor::GetCPUType(m_pt_process_trace_config); 243299e37695SRavitheja Addepally 243399e37695SRavitheja Addepally // Trace on Complete process will have traceid of 0 243499e37695SRavitheja Addepally m_pt_proces_trace_id = 0; 243599e37695SRavitheja Addepally 243699e37695SRavitheja Addepally LLDB_LOG(log, "Process Trace ID {0}", m_pt_proces_trace_id); 243799e37695SRavitheja Addepally return m_pt_proces_trace_id; 243899e37695SRavitheja Addepally } 243999e37695SRavitheja Addepally 244099e37695SRavitheja Addepally lldb::user_id_t NativeProcessLinux::StartTrace(const TraceOptions &config, 244199e37695SRavitheja Addepally Status &error) { 244299e37695SRavitheja Addepally if (config.getType() != TraceType::eTraceTypeProcessorTrace) 244399e37695SRavitheja Addepally return NativeProcessProtocol::StartTrace(config, error); 244499e37695SRavitheja Addepally 244599e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 244699e37695SRavitheja Addepally 244799e37695SRavitheja Addepally lldb::tid_t threadid = config.getThreadID(); 244899e37695SRavitheja Addepally 244999e37695SRavitheja Addepally if (threadid == LLDB_INVALID_THREAD_ID) 245099e37695SRavitheja Addepally return StartTraceGroup(config, error); 245199e37695SRavitheja Addepally 245299e37695SRavitheja Addepally auto thread_sp = GetThreadByID(threadid); 245399e37695SRavitheja Addepally if (!thread_sp) { 245499e37695SRavitheja Addepally // Thread not tracked by lldb so don't trace. 245599e37695SRavitheja Addepally error.SetErrorString("invalid thread id"); 245699e37695SRavitheja Addepally return LLDB_INVALID_UID; 245799e37695SRavitheja Addepally } 245899e37695SRavitheja Addepally 245999e37695SRavitheja Addepally const auto &iter = m_processor_trace_monitor.find(threadid); 246099e37695SRavitheja Addepally if (iter != m_processor_trace_monitor.end()) { 246199e37695SRavitheja Addepally LLDB_LOG(log, "Thread already being traced"); 246299e37695SRavitheja Addepally error.SetErrorString("tracing already active on this thread"); 246399e37695SRavitheja Addepally return LLDB_INVALID_UID; 246499e37695SRavitheja Addepally } 246599e37695SRavitheja Addepally 246699e37695SRavitheja Addepally auto traceMonitor = 246799e37695SRavitheja Addepally ProcessorTraceMonitor::Create(GetID(), threadid, config, false); 246899e37695SRavitheja Addepally if (!traceMonitor) { 246999e37695SRavitheja Addepally error = traceMonitor.takeError(); 247099e37695SRavitheja Addepally LLDB_LOG(log, "error {0}", error); 247199e37695SRavitheja Addepally return LLDB_INVALID_UID; 247299e37695SRavitheja Addepally } 247399e37695SRavitheja Addepally lldb::user_id_t ret_trace_id = (*traceMonitor)->GetTraceID(); 247499e37695SRavitheja Addepally m_processor_trace_monitor.insert( 247599e37695SRavitheja Addepally std::make_pair(threadid, std::move(*traceMonitor))); 247699e37695SRavitheja Addepally return ret_trace_id; 247799e37695SRavitheja Addepally } 247899e37695SRavitheja Addepally 247999e37695SRavitheja Addepally Status NativeProcessLinux::StopTracingForThread(lldb::tid_t thread) { 248099e37695SRavitheja Addepally Status error; 248199e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 248299e37695SRavitheja Addepally LLDB_LOG(log, "Thread {0}", thread); 248399e37695SRavitheja Addepally 248499e37695SRavitheja Addepally const auto& iter = m_processor_trace_monitor.find(thread); 248599e37695SRavitheja Addepally if (iter == m_processor_trace_monitor.end()) { 248699e37695SRavitheja Addepally error.SetErrorString("tracing not active for this thread"); 248799e37695SRavitheja Addepally return error; 248899e37695SRavitheja Addepally } 248999e37695SRavitheja Addepally 249099e37695SRavitheja Addepally if (iter->second->GetTraceID() == m_pt_proces_trace_id) { 249199e37695SRavitheja Addepally // traceid maps to the whole process so we have to erase it from the 249299e37695SRavitheja Addepally // thread group. 249399e37695SRavitheja Addepally LLDB_LOG(log, "traceid maps to process"); 249499e37695SRavitheja Addepally m_pt_traced_thread_group.erase(thread); 249599e37695SRavitheja Addepally } 249699e37695SRavitheja Addepally m_processor_trace_monitor.erase(iter); 249799e37695SRavitheja Addepally 249899e37695SRavitheja Addepally return error; 249999e37695SRavitheja Addepally } 250099e37695SRavitheja Addepally 250199e37695SRavitheja Addepally Status NativeProcessLinux::StopTrace(lldb::user_id_t traceid, 250299e37695SRavitheja Addepally lldb::tid_t thread) { 250399e37695SRavitheja Addepally Status error; 250499e37695SRavitheja Addepally 250599e37695SRavitheja Addepally TraceOptions trace_options; 250699e37695SRavitheja Addepally trace_options.setThreadID(thread); 250799e37695SRavitheja Addepally error = NativeProcessLinux::GetTraceConfig(traceid, trace_options); 250899e37695SRavitheja Addepally 250999e37695SRavitheja Addepally if (error.Fail()) 251099e37695SRavitheja Addepally return error; 251199e37695SRavitheja Addepally 251299e37695SRavitheja Addepally switch (trace_options.getType()) { 251399e37695SRavitheja Addepally case lldb::TraceType::eTraceTypeProcessorTrace: 251499e37695SRavitheja Addepally if (traceid == m_pt_proces_trace_id && 251599e37695SRavitheja Addepally thread == LLDB_INVALID_THREAD_ID) 251699e37695SRavitheja Addepally StopProcessorTracingOnProcess(); 251799e37695SRavitheja Addepally else 251899e37695SRavitheja Addepally error = StopProcessorTracingOnThread(traceid, thread); 251999e37695SRavitheja Addepally break; 252099e37695SRavitheja Addepally default: 252199e37695SRavitheja Addepally error.SetErrorString("trace not supported"); 252299e37695SRavitheja Addepally break; 252399e37695SRavitheja Addepally } 252499e37695SRavitheja Addepally 252599e37695SRavitheja Addepally return error; 252699e37695SRavitheja Addepally } 252799e37695SRavitheja Addepally 252899e37695SRavitheja Addepally void NativeProcessLinux::StopProcessorTracingOnProcess() { 252999e37695SRavitheja Addepally for (auto thread_id_iter : m_pt_traced_thread_group) 253099e37695SRavitheja Addepally m_processor_trace_monitor.erase(thread_id_iter); 253199e37695SRavitheja Addepally m_pt_traced_thread_group.clear(); 253299e37695SRavitheja Addepally m_pt_proces_trace_id = LLDB_INVALID_UID; 253399e37695SRavitheja Addepally } 253499e37695SRavitheja Addepally 253599e37695SRavitheja Addepally Status NativeProcessLinux::StopProcessorTracingOnThread(lldb::user_id_t traceid, 253699e37695SRavitheja Addepally lldb::tid_t thread) { 253799e37695SRavitheja Addepally Status error; 253899e37695SRavitheja Addepally Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 253999e37695SRavitheja Addepally 254099e37695SRavitheja Addepally if (thread == LLDB_INVALID_THREAD_ID) { 254199e37695SRavitheja Addepally for (auto& iter : m_processor_trace_monitor) { 254299e37695SRavitheja Addepally if (iter.second->GetTraceID() == traceid) { 254399e37695SRavitheja Addepally // Stopping a trace instance for an individual thread 254499e37695SRavitheja Addepally // hence there will only be one traceid that can match. 254599e37695SRavitheja Addepally m_processor_trace_monitor.erase(iter.first); 254699e37695SRavitheja Addepally return error; 254799e37695SRavitheja Addepally } 254899e37695SRavitheja Addepally LLDB_LOG(log, "Trace ID {0}", iter.second->GetTraceID()); 254999e37695SRavitheja Addepally } 255099e37695SRavitheja Addepally 255199e37695SRavitheja Addepally LLDB_LOG(log, "Invalid TraceID"); 255299e37695SRavitheja Addepally error.SetErrorString("invalid trace id"); 255399e37695SRavitheja Addepally return error; 255499e37695SRavitheja Addepally } 255599e37695SRavitheja Addepally 255699e37695SRavitheja Addepally // thread is specified so we can use find function on the map. 255799e37695SRavitheja Addepally const auto& iter = m_processor_trace_monitor.find(thread); 255899e37695SRavitheja Addepally if (iter == m_processor_trace_monitor.end()) { 255999e37695SRavitheja Addepally // thread not found in our map. 256099e37695SRavitheja Addepally LLDB_LOG(log, "thread not being traced"); 256199e37695SRavitheja Addepally error.SetErrorString("tracing not active for this thread"); 256299e37695SRavitheja Addepally return error; 256399e37695SRavitheja Addepally } 256499e37695SRavitheja Addepally if (iter->second->GetTraceID() != traceid) { 256599e37695SRavitheja Addepally // traceid did not match so it has to be invalid. 256699e37695SRavitheja Addepally LLDB_LOG(log, "Invalid TraceID"); 256799e37695SRavitheja Addepally error.SetErrorString("invalid trace id"); 256899e37695SRavitheja Addepally return error; 256999e37695SRavitheja Addepally } 257099e37695SRavitheja Addepally 257199e37695SRavitheja Addepally LLDB_LOG(log, "UID - {0} , Thread -{1}", traceid, thread); 257299e37695SRavitheja Addepally 257399e37695SRavitheja Addepally if (traceid == m_pt_proces_trace_id) { 257499e37695SRavitheja Addepally // traceid maps to the whole process so we have to erase it from the 257599e37695SRavitheja Addepally // thread group. 257699e37695SRavitheja Addepally LLDB_LOG(log, "traceid maps to process"); 257799e37695SRavitheja Addepally m_pt_traced_thread_group.erase(thread); 257899e37695SRavitheja Addepally } 257999e37695SRavitheja Addepally m_processor_trace_monitor.erase(iter); 258099e37695SRavitheja Addepally 258199e37695SRavitheja Addepally return error; 258299e37695SRavitheja Addepally } 2583