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" 3239de3110SZachary Turner #include "lldb/Host/ThreadLauncher.h" 332a86b555SPavel Labath #include "lldb/Host/common/NativeBreakpoint.h" 342a86b555SPavel Labath #include "lldb/Host/common/NativeRegisterContext.h" 35*4ee1c952SPavel Labath #include "lldb/Host/linux/Ptrace.h" 36*4ee1c952SPavel Labath #include "lldb/Host/linux/Uio.h" 37816ae4b0SKamil Rytarowski #include "lldb/Host/posix/ProcessLauncherPosixFork.h" 382a86b555SPavel Labath #include "lldb/Symbol/ObjectFile.h" 3990aff47cSZachary Turner #include "lldb/Target/Process.h" 40af245d11STodd Fiala #include "lldb/Target/ProcessLaunchInfo.h" 415b981ab9SPavel Labath #include "lldb/Target/Target.h" 42bf9a7730SZachary Turner #include "lldb/Utility/Error.h" 43c16f5dcaSChaoren Lin #include "lldb/Utility/LLDBAssert.h" 44af245d11STodd Fiala #include "lldb/Utility/PseudoTerminal.h" 45f805e190SPavel Labath #include "lldb/Utility/StringExtractor.h" 46af245d11STodd Fiala 47af245d11STodd Fiala #include "NativeThreadLinux.h" 48b9c1b51eSKate Stone #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" 49af245d11STodd Fiala #include "ProcFileReader.h" 501e209fccSTamas Berghammer #include "Procfs.h" 51cacde7dfSTodd Fiala 52*4ee1c952SPavel Labath #include "llvm/Support/Threading.h" 53*4ee1c952SPavel Labath 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.", 100a6321a8eSPavel Labath strerror(errno)); 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) { 143a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE | 144a6321a8eSPavel Labath POSIX_LOG_VERBOSE)); 145a6321a8eSPavel Labath if (!log) 146a6321a8eSPavel Labath return; 147af245d11STodd Fiala StreamString buf; 148af245d11STodd Fiala 149b9c1b51eSKate Stone switch (req) { 150b9c1b51eSKate Stone case PTRACE_POKETEXT: { 151af245d11STodd Fiala DisplayBytes(buf, &data, 8); 152a6321a8eSPavel Labath LLDB_LOG(log, "PTRACE_POKETEXT {0}", buf.GetData()); 153af245d11STodd Fiala break; 154af245d11STodd Fiala } 155b9c1b51eSKate Stone case PTRACE_POKEDATA: { 156af245d11STodd Fiala DisplayBytes(buf, &data, 8); 157a6321a8eSPavel Labath LLDB_LOG(log, "PTRACE_POKEDATA {0}", buf.GetData()); 158af245d11STodd Fiala break; 159af245d11STodd Fiala } 160b9c1b51eSKate Stone case PTRACE_POKEUSER: { 161af245d11STodd Fiala DisplayBytes(buf, &data, 8); 162a6321a8eSPavel Labath LLDB_LOG(log, "PTRACE_POKEUSER {0}", buf.GetData()); 163af245d11STodd Fiala break; 164af245d11STodd Fiala } 165b9c1b51eSKate Stone case PTRACE_SETREGS: { 166af245d11STodd Fiala DisplayBytes(buf, data, data_size); 167a6321a8eSPavel Labath LLDB_LOG(log, "PTRACE_SETREGS {0}", buf.GetData()); 168af245d11STodd Fiala break; 169af245d11STodd Fiala } 170b9c1b51eSKate Stone case PTRACE_SETFPREGS: { 171af245d11STodd Fiala DisplayBytes(buf, data, data_size); 172a6321a8eSPavel Labath LLDB_LOG(log, "PTRACE_SETFPREGS {0}", buf.GetData()); 173af245d11STodd Fiala break; 174af245d11STodd Fiala } 175b9c1b51eSKate Stone case PTRACE_SETSIGINFO: { 176af245d11STodd Fiala DisplayBytes(buf, data, sizeof(siginfo_t)); 177a6321a8eSPavel Labath LLDB_LOG(log, "PTRACE_SETSIGINFO {0}", buf.GetData()); 178af245d11STodd Fiala break; 179af245d11STodd Fiala } 180b9c1b51eSKate Stone case PTRACE_SETREGSET: { 181af245d11STodd Fiala // Extract iov_base from data, which is a pointer to the struct IOVEC 182af245d11STodd Fiala DisplayBytes(buf, *(void **)data, data_size); 183a6321a8eSPavel Labath LLDB_LOG(log, "PTRACE_SETREGSET {0}", buf.GetData()); 184af245d11STodd Fiala break; 185af245d11STodd Fiala } 186b9c1b51eSKate Stone default: {} 187af245d11STodd Fiala } 188af245d11STodd Fiala } 189af245d11STodd Fiala 19019cbe96aSPavel Labath static constexpr unsigned k_ptrace_word_size = sizeof(void *); 191b9c1b51eSKate Stone static_assert(sizeof(long) >= k_ptrace_word_size, 192b9c1b51eSKate Stone "Size of long must be larger than ptrace word size"); 1931107b5a5SPavel Labath } // end of anonymous namespace 1941107b5a5SPavel Labath 195bd7cbc5aSPavel Labath // Simple helper function to ensure flags are enabled on the given file 196bd7cbc5aSPavel Labath // descriptor. 197b9c1b51eSKate Stone static Error EnsureFDFlags(int fd, int flags) { 198bd7cbc5aSPavel Labath Error error; 199bd7cbc5aSPavel Labath 200bd7cbc5aSPavel Labath int status = fcntl(fd, F_GETFL); 201b9c1b51eSKate Stone if (status == -1) { 202bd7cbc5aSPavel Labath error.SetErrorToErrno(); 203bd7cbc5aSPavel Labath return error; 204bd7cbc5aSPavel Labath } 205bd7cbc5aSPavel Labath 206b9c1b51eSKate Stone if (fcntl(fd, F_SETFL, status | flags) == -1) { 207bd7cbc5aSPavel Labath error.SetErrorToErrno(); 208bd7cbc5aSPavel Labath return error; 209bd7cbc5aSPavel Labath } 210bd7cbc5aSPavel Labath 211bd7cbc5aSPavel Labath return error; 212bd7cbc5aSPavel Labath } 213bd7cbc5aSPavel Labath 214af245d11STodd Fiala // ----------------------------------------------------------------------------- 215af245d11STodd Fiala // Public Static Methods 216af245d11STodd Fiala // ----------------------------------------------------------------------------- 217af245d11STodd Fiala 218b9c1b51eSKate Stone Error NativeProcessProtocol::Launch( 219db264a6dSTamas Berghammer ProcessLaunchInfo &launch_info, 220b9c1b51eSKate Stone NativeProcessProtocol::NativeDelegate &native_delegate, MainLoop &mainloop, 221b9c1b51eSKate Stone NativeProcessProtocolSP &native_process_sp) { 222a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 223af245d11STodd Fiala 2242a86b555SPavel Labath Error error; 225af245d11STodd Fiala 226af245d11STodd Fiala // Verify the working directory is valid if one was specified. 227d3173f34SChaoren Lin FileSpec working_dir{launch_info.GetWorkingDirectory()}; 228d3173f34SChaoren Lin if (working_dir && 229d3173f34SChaoren Lin (!working_dir.ResolvePath() || 230b9c1b51eSKate Stone working_dir.GetFileType() != FileSpec::eFileTypeDirectory)) { 231d3173f34SChaoren Lin error.SetErrorStringWithFormat("No such file or directory: %s", 232d3173f34SChaoren Lin working_dir.GetCString()); 233af245d11STodd Fiala return error; 234af245d11STodd Fiala } 235af245d11STodd Fiala 236af245d11STodd Fiala // Create the NativeProcessLinux in launch mode. 237af245d11STodd Fiala native_process_sp.reset(new NativeProcessLinux()); 238af245d11STodd Fiala 239b9c1b51eSKate Stone if (!native_process_sp->RegisterNativeDelegate(native_delegate)) { 240af245d11STodd Fiala native_process_sp.reset(); 241af245d11STodd Fiala error.SetErrorStringWithFormat("failed to register the native delegate"); 242af245d11STodd Fiala return error; 243af245d11STodd Fiala } 244af245d11STodd Fiala 245b9c1b51eSKate Stone error = std::static_pointer_cast<NativeProcessLinux>(native_process_sp) 246b9c1b51eSKate Stone ->LaunchInferior(mainloop, launch_info); 247af245d11STodd Fiala 248b9c1b51eSKate Stone if (error.Fail()) { 249af245d11STodd Fiala native_process_sp.reset(); 250a6321a8eSPavel Labath LLDB_LOG(log, "failed to launch process: {0}", error); 251af245d11STodd Fiala return error; 252af245d11STodd Fiala } 253af245d11STodd Fiala 254af245d11STodd Fiala launch_info.SetProcessID(native_process_sp->GetID()); 255af245d11STodd Fiala 256af245d11STodd Fiala return error; 257af245d11STodd Fiala } 258af245d11STodd Fiala 259b9c1b51eSKate Stone Error NativeProcessProtocol::Attach( 260b9c1b51eSKate Stone lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, 261b9c1b51eSKate Stone MainLoop &mainloop, NativeProcessProtocolSP &native_process_sp) { 262a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 263a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0:x}", pid); 264af245d11STodd Fiala 265af245d11STodd Fiala // Retrieve the architecture for the running process. 266af245d11STodd Fiala ArchSpec process_arch; 2672a86b555SPavel Labath Error error = ResolveProcessArchitecture(pid, process_arch); 268af245d11STodd Fiala if (!error.Success()) 269af245d11STodd Fiala return error; 270af245d11STodd Fiala 271b9c1b51eSKate Stone std::shared_ptr<NativeProcessLinux> native_process_linux_sp( 272b9c1b51eSKate Stone new NativeProcessLinux()); 273af245d11STodd Fiala 274b9c1b51eSKate Stone if (!native_process_linux_sp->RegisterNativeDelegate(native_delegate)) { 275af245d11STodd Fiala error.SetErrorStringWithFormat("failed to register the native delegate"); 276af245d11STodd Fiala return error; 277af245d11STodd Fiala } 278af245d11STodd Fiala 27919cbe96aSPavel Labath native_process_linux_sp->AttachToInferior(mainloop, pid, error); 280af245d11STodd Fiala if (!error.Success()) 281af245d11STodd Fiala return error; 282af245d11STodd Fiala 2831339b5e8SOleksiy Vyalov native_process_sp = native_process_linux_sp; 284af245d11STodd Fiala return error; 285af245d11STodd Fiala } 286af245d11STodd Fiala 287af245d11STodd Fiala // ----------------------------------------------------------------------------- 288af245d11STodd Fiala // Public Instance Methods 289af245d11STodd Fiala // ----------------------------------------------------------------------------- 290af245d11STodd Fiala 291b9c1b51eSKate Stone NativeProcessLinux::NativeProcessLinux() 292b9c1b51eSKate Stone : NativeProcessProtocol(LLDB_INVALID_PROCESS_ID), m_arch(), 293b9c1b51eSKate Stone m_supports_mem_region(eLazyBoolCalculate), m_mem_region_cache(), 294b9c1b51eSKate Stone m_pending_notification_tid(LLDB_INVALID_THREAD_ID) {} 295af245d11STodd Fiala 296b9c1b51eSKate Stone void NativeProcessLinux::AttachToInferior(MainLoop &mainloop, lldb::pid_t pid, 297b9c1b51eSKate Stone Error &error) { 298a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 299a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0:x}", pid); 300af245d11STodd Fiala 301b9c1b51eSKate Stone m_sigchld_handle = mainloop.RegisterSignal( 302b9c1b51eSKate Stone SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, error); 30319cbe96aSPavel Labath if (!m_sigchld_handle) 30419cbe96aSPavel Labath return; 30519cbe96aSPavel Labath 3062a86b555SPavel Labath error = ResolveProcessArchitecture(pid, m_arch); 307af245d11STodd Fiala if (!error.Success()) 308af245d11STodd Fiala return; 309af245d11STodd Fiala 310af245d11STodd Fiala // Set the architecture to the exe architecture. 311a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid, 312a6321a8eSPavel Labath m_arch.GetArchitectureName()); 313af245d11STodd Fiala m_pid = pid; 314af245d11STodd Fiala SetState(eStateAttaching); 315af245d11STodd Fiala 31619cbe96aSPavel Labath Attach(pid, error); 317af245d11STodd Fiala } 318af245d11STodd Fiala 319b9c1b51eSKate Stone Error NativeProcessLinux::LaunchInferior(MainLoop &mainloop, 320b9c1b51eSKate Stone ProcessLaunchInfo &launch_info) { 3214abe5d69SPavel Labath Error error; 322b9c1b51eSKate Stone m_sigchld_handle = mainloop.RegisterSignal( 323b9c1b51eSKate Stone SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, error); 3244abe5d69SPavel Labath if (!m_sigchld_handle) 3254abe5d69SPavel Labath return error; 3264abe5d69SPavel Labath 3274abe5d69SPavel Labath SetState(eStateLaunching); 3280c4f01d4SPavel Labath 3294abe5d69SPavel Labath MaybeLogLaunchInfo(launch_info); 3304abe5d69SPavel Labath 331b9c1b51eSKate Stone ::pid_t pid = 332816ae4b0SKamil Rytarowski ProcessLauncherPosixFork().LaunchProcess(launch_info, error).GetProcessId(); 3335ad891f7SPavel Labath if (error.Fail()) 3344abe5d69SPavel Labath return error; 3350c4f01d4SPavel Labath 336a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 33775f47c3aSTodd Fiala 338af245d11STodd Fiala // Wait for the child process to trap on its call to execve. 339af245d11STodd Fiala ::pid_t wpid; 340af245d11STodd Fiala int status; 341b9c1b51eSKate Stone if ((wpid = waitpid(pid, &status, 0)) < 0) { 342bd7cbc5aSPavel Labath error.SetErrorToErrno(); 343a6321a8eSPavel Labath LLDB_LOG(log, "waitpid for inferior failed with %s", error); 344af245d11STodd Fiala 345af245d11STodd Fiala // Mark the inferior as invalid. 346b9c1b51eSKate Stone // FIXME this could really use a new state - eStateLaunchFailure. For now, 347b9c1b51eSKate Stone // using eStateInvalid. 348bd7cbc5aSPavel Labath SetState(StateType::eStateInvalid); 349af245d11STodd Fiala 3504abe5d69SPavel Labath return error; 351af245d11STodd Fiala } 352af245d11STodd Fiala assert(WIFSTOPPED(status) && (wpid == static_cast<::pid_t>(pid)) && 353af245d11STodd Fiala "Could not sync with inferior process."); 354af245d11STodd Fiala 355a6321a8eSPavel Labath LLDB_LOG(log, "inferior started, now in stopped state"); 356bd7cbc5aSPavel Labath error = SetDefaultPtraceOpts(pid); 357b9c1b51eSKate Stone if (error.Fail()) { 358a6321a8eSPavel Labath LLDB_LOG(log, "failed to set default ptrace options: {0}", error); 359af245d11STodd Fiala 360af245d11STodd Fiala // Mark the inferior as invalid. 361b9c1b51eSKate Stone // FIXME this could really use a new state - eStateLaunchFailure. For now, 362b9c1b51eSKate Stone // using eStateInvalid. 363bd7cbc5aSPavel Labath SetState(StateType::eStateInvalid); 364af245d11STodd Fiala 3654abe5d69SPavel Labath return error; 366af245d11STodd Fiala } 367af245d11STodd Fiala 368af245d11STodd Fiala // Release the master terminal descriptor and pass it off to the 369af245d11STodd Fiala // NativeProcessLinux instance. Similarly stash the inferior pid. 3705ad891f7SPavel Labath m_terminal_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor(); 371bd7cbc5aSPavel Labath m_pid = pid; 3724abe5d69SPavel Labath launch_info.SetProcessID(pid); 373af245d11STodd Fiala 374b9c1b51eSKate Stone if (m_terminal_fd != -1) { 375bd7cbc5aSPavel Labath error = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); 376b9c1b51eSKate Stone if (error.Fail()) { 377a6321a8eSPavel Labath LLDB_LOG(log, 378a6321a8eSPavel Labath "inferior EnsureFDFlags failed for ensuring terminal " 379a6321a8eSPavel Labath "O_NONBLOCK setting: {0}", 380a6321a8eSPavel Labath error); 381af245d11STodd Fiala 382af245d11STodd Fiala // Mark the inferior as invalid. 383b9c1b51eSKate Stone // FIXME this could really use a new state - eStateLaunchFailure. For 384b9c1b51eSKate Stone // now, using eStateInvalid. 385bd7cbc5aSPavel Labath SetState(StateType::eStateInvalid); 386af245d11STodd Fiala 3874abe5d69SPavel Labath return error; 388af245d11STodd Fiala } 3895ad891f7SPavel Labath } 390af245d11STodd Fiala 391a6321a8eSPavel Labath LLDB_LOG(log, "adding pid = {0}", pid); 3922a86b555SPavel Labath ResolveProcessArchitecture(m_pid, m_arch); 393f9077782SPavel Labath NativeThreadLinuxSP thread_sp = AddThread(pid); 394af245d11STodd Fiala assert(thread_sp && "AddThread() returned a nullptr thread"); 395f9077782SPavel Labath thread_sp->SetStoppedBySignal(SIGSTOP); 396f9077782SPavel Labath ThreadWasCreated(*thread_sp); 397af245d11STodd Fiala 398af245d11STodd Fiala // Let our process instance know the thread has stopped. 399bd7cbc5aSPavel Labath SetCurrentThreadID(thread_sp->GetID()); 400bd7cbc5aSPavel Labath SetState(StateType::eStateStopped); 401af245d11STodd Fiala 402a6321a8eSPavel Labath if (error.Fail()) 403a6321a8eSPavel Labath LLDB_LOG(log, "inferior launching failed {0}", error); 4044abe5d69SPavel Labath return error; 405af245d11STodd Fiala } 406af245d11STodd Fiala 407b9c1b51eSKate Stone ::pid_t NativeProcessLinux::Attach(lldb::pid_t pid, Error &error) { 408a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 409af245d11STodd Fiala 410b9c1b51eSKate Stone // Use a map to keep track of the threads which we have attached/need to 411b9c1b51eSKate Stone // attach. 412af245d11STodd Fiala Host::TidMap tids_to_attach; 413b9c1b51eSKate Stone if (pid <= 1) { 414bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 415bd7cbc5aSPavel Labath error.SetErrorString("Attaching to process 1 is not allowed."); 416bd7cbc5aSPavel Labath return -1; 417af245d11STodd Fiala } 418af245d11STodd Fiala 419b9c1b51eSKate Stone while (Host::FindProcessThreads(pid, tids_to_attach)) { 420af245d11STodd Fiala for (Host::TidMap::iterator it = tids_to_attach.begin(); 421b9c1b51eSKate Stone it != tids_to_attach.end();) { 422b9c1b51eSKate Stone if (it->second == false) { 423af245d11STodd Fiala lldb::tid_t tid = it->first; 424af245d11STodd Fiala 425af245d11STodd Fiala // Attach to the requested process. 426af245d11STodd Fiala // An attach will cause the thread to stop with a SIGSTOP. 4274a9babb2SPavel Labath error = PtraceWrapper(PTRACE_ATTACH, tid); 428b9c1b51eSKate Stone if (error.Fail()) { 429af245d11STodd Fiala // No such thread. The thread may have exited. 430af245d11STodd Fiala // More error handling may be needed. 431b9c1b51eSKate Stone if (error.GetError() == ESRCH) { 432af245d11STodd Fiala it = tids_to_attach.erase(it); 433af245d11STodd Fiala continue; 434b9c1b51eSKate Stone } else 435bd7cbc5aSPavel Labath return -1; 436af245d11STodd Fiala } 437af245d11STodd Fiala 438af245d11STodd Fiala int status; 439af245d11STodd Fiala // Need to use __WALL otherwise we receive an error with errno=ECHLD 440af245d11STodd Fiala // At this point we should have a thread stopped if waitpid succeeds. 441b9c1b51eSKate Stone if ((status = waitpid(tid, NULL, __WALL)) < 0) { 442af245d11STodd Fiala // No such thread. The thread may have exited. 443af245d11STodd Fiala // More error handling may be needed. 444b9c1b51eSKate Stone if (errno == ESRCH) { 445af245d11STodd Fiala it = tids_to_attach.erase(it); 446af245d11STodd Fiala continue; 447b9c1b51eSKate Stone } else { 448bd7cbc5aSPavel Labath error.SetErrorToErrno(); 449bd7cbc5aSPavel Labath return -1; 450af245d11STodd Fiala } 451af245d11STodd Fiala } 452af245d11STodd Fiala 453bd7cbc5aSPavel Labath error = SetDefaultPtraceOpts(tid); 454bd7cbc5aSPavel Labath if (error.Fail()) 455bd7cbc5aSPavel Labath return -1; 456af245d11STodd Fiala 457a6321a8eSPavel Labath LLDB_LOG(log, "adding tid = {0}", tid); 458af245d11STodd Fiala it->second = true; 459af245d11STodd Fiala 460af245d11STodd Fiala // Create the thread, mark it as stopped. 461f9077782SPavel Labath NativeThreadLinuxSP thread_sp(AddThread(static_cast<lldb::tid_t>(tid))); 462af245d11STodd Fiala assert(thread_sp && "AddThread() returned a nullptr"); 463fa03ad2eSChaoren Lin 464b9c1b51eSKate Stone // This will notify this is a new thread and tell the system it is 465b9c1b51eSKate Stone // stopped. 466f9077782SPavel Labath thread_sp->SetStoppedBySignal(SIGSTOP); 467f9077782SPavel Labath ThreadWasCreated(*thread_sp); 468bd7cbc5aSPavel Labath SetCurrentThreadID(thread_sp->GetID()); 469af245d11STodd Fiala } 470af245d11STodd Fiala 471af245d11STodd Fiala // move the loop forward 472af245d11STodd Fiala ++it; 473af245d11STodd Fiala } 474af245d11STodd Fiala } 475af245d11STodd Fiala 476b9c1b51eSKate Stone if (tids_to_attach.size() > 0) { 477bd7cbc5aSPavel Labath m_pid = pid; 478af245d11STodd Fiala // Let our process instance know the thread has stopped. 479bd7cbc5aSPavel Labath SetState(StateType::eStateStopped); 480b9c1b51eSKate Stone } else { 481bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 482bd7cbc5aSPavel Labath error.SetErrorString("No such process."); 483bd7cbc5aSPavel Labath return -1; 484af245d11STodd Fiala } 485af245d11STodd Fiala 486bd7cbc5aSPavel Labath return pid; 487af245d11STodd Fiala } 488af245d11STodd Fiala 489b9c1b51eSKate Stone Error NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) { 490af245d11STodd Fiala long ptrace_opts = 0; 491af245d11STodd Fiala 492af245d11STodd Fiala // Have the child raise an event on exit. This is used to keep the child in 493af245d11STodd Fiala // limbo until it is destroyed. 494af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXIT; 495af245d11STodd Fiala 496af245d11STodd Fiala // Have the tracer trace threads which spawn in the inferior process. 497af245d11STodd Fiala // TODO: if we want to support tracing the inferiors' child, add the 498af245d11STodd Fiala // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK) 499af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACECLONE; 500af245d11STodd Fiala 501af245d11STodd Fiala // Have the tracer notify us before execve returns 502af245d11STodd Fiala // (needed to disable legacy SIGTRAP generation) 503af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXEC; 504af245d11STodd Fiala 5054a9babb2SPavel Labath return PtraceWrapper(PTRACE_SETOPTIONS, pid, nullptr, (void *)ptrace_opts); 506af245d11STodd Fiala } 507af245d11STodd Fiala 508b9c1b51eSKate Stone static ExitType convert_pid_status_to_exit_type(int status) { 509af245d11STodd Fiala if (WIFEXITED(status)) 510af245d11STodd Fiala return ExitType::eExitTypeExit; 511af245d11STodd Fiala else if (WIFSIGNALED(status)) 512af245d11STodd Fiala return ExitType::eExitTypeSignal; 513af245d11STodd Fiala else if (WIFSTOPPED(status)) 514af245d11STodd Fiala return ExitType::eExitTypeStop; 515b9c1b51eSKate Stone else { 516af245d11STodd Fiala // We don't know what this is. 517af245d11STodd Fiala return ExitType::eExitTypeInvalid; 518af245d11STodd Fiala } 519af245d11STodd Fiala } 520af245d11STodd Fiala 521b9c1b51eSKate Stone static int convert_pid_status_to_return_code(int status) { 522af245d11STodd Fiala if (WIFEXITED(status)) 523af245d11STodd Fiala return WEXITSTATUS(status); 524af245d11STodd Fiala else if (WIFSIGNALED(status)) 525af245d11STodd Fiala return WTERMSIG(status); 526af245d11STodd Fiala else if (WIFSTOPPED(status)) 527af245d11STodd Fiala return WSTOPSIG(status); 528b9c1b51eSKate Stone else { 529af245d11STodd Fiala // We don't know what this is. 530af245d11STodd Fiala return ExitType::eExitTypeInvalid; 531af245d11STodd Fiala } 532af245d11STodd Fiala } 533af245d11STodd Fiala 5341107b5a5SPavel Labath // Handles all waitpid events from the inferior process. 535b9c1b51eSKate Stone void NativeProcessLinux::MonitorCallback(lldb::pid_t pid, bool exited, 536b9c1b51eSKate Stone int signal, int status) { 537af245d11STodd Fiala Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 538af245d11STodd Fiala 539b9c1b51eSKate Stone // Certain activities differ based on whether the pid is the tid of the main 540b9c1b51eSKate Stone // thread. 5411107b5a5SPavel Labath const bool is_main_thread = (pid == GetID()); 542af245d11STodd Fiala 543af245d11STodd Fiala // Handle when the thread exits. 544b9c1b51eSKate Stone if (exited) { 545a6321a8eSPavel Labath LLDB_LOG(log, "got exit signal({0}) , tid = {1} ({2} main thread)", signal, 546a6321a8eSPavel Labath pid, is_main_thread ? "is" : "is not"); 547af245d11STodd Fiala 548af245d11STodd Fiala // This is a thread that exited. Ensure we're not tracking it anymore. 5491107b5a5SPavel Labath const bool thread_found = StopTrackingThread(pid); 550af245d11STodd Fiala 551b9c1b51eSKate Stone if (is_main_thread) { 552b9c1b51eSKate Stone // We only set the exit status and notify the delegate if we haven't 553b9c1b51eSKate Stone // already set the process 554b9c1b51eSKate Stone // state to an exited state. We normally should have received a SIGTRAP | 555b9c1b51eSKate Stone // (PTRACE_EVENT_EXIT << 8) 556af245d11STodd Fiala // for the main thread. 557b9c1b51eSKate Stone const bool already_notified = (GetState() == StateType::eStateExited) || 558b9c1b51eSKate Stone (GetState() == StateType::eStateCrashed); 559b9c1b51eSKate Stone if (!already_notified) { 560a6321a8eSPavel Labath LLDB_LOG( 561a6321a8eSPavel Labath log, 562a6321a8eSPavel Labath "tid = {0} handling main thread exit ({1}), expected exit state " 563a6321a8eSPavel Labath "already set but state was {2} instead, setting exit state now", 564a6321a8eSPavel Labath pid, 565b9c1b51eSKate Stone thread_found ? "stopped tracking thread metadata" 566b9c1b51eSKate Stone : "thread metadata not found", 5678198db30SPavel Labath GetState()); 568af245d11STodd Fiala // The main thread exited. We're done monitoring. Report to delegate. 569b9c1b51eSKate Stone SetExitStatus(convert_pid_status_to_exit_type(status), 570b9c1b51eSKate Stone convert_pid_status_to_return_code(status), nullptr, true); 571af245d11STodd Fiala 572af245d11STodd Fiala // Notify delegate that our process has exited. 5731107b5a5SPavel Labath SetState(StateType::eStateExited, true); 574a6321a8eSPavel Labath } else 575a6321a8eSPavel Labath LLDB_LOG(log, "tid = {0} main thread now exited (%s)", pid, 576b9c1b51eSKate Stone thread_found ? "stopped tracking thread metadata" 577b9c1b51eSKate Stone : "thread metadata not found"); 578b9c1b51eSKate Stone } else { 579b9c1b51eSKate Stone // Do we want to report to the delegate in this case? I think not. If 580a6321a8eSPavel Labath // this was an orderly thread exit, we would already have received the 581a6321a8eSPavel Labath // SIGTRAP | (PTRACE_EVENT_EXIT << 8) signal, and we would have done an 582a6321a8eSPavel Labath // all-stop then. 583a6321a8eSPavel Labath LLDB_LOG(log, "tid = {0} handling non-main thread exit (%s)", pid, 584b9c1b51eSKate Stone thread_found ? "stopped tracking thread metadata" 585b9c1b51eSKate Stone : "thread metadata not found"); 586af245d11STodd Fiala } 5871107b5a5SPavel Labath return; 588af245d11STodd Fiala } 589af245d11STodd Fiala 590af245d11STodd Fiala siginfo_t info; 591b9cc0c75SPavel Labath const auto info_err = GetSignalInfo(pid, &info); 592b9cc0c75SPavel Labath auto thread_sp = GetThreadByID(pid); 593b9cc0c75SPavel Labath 594b9c1b51eSKate Stone if (!thread_sp) { 595b9c1b51eSKate Stone // Normally, the only situation when we cannot find the thread is if we have 596a6321a8eSPavel Labath // just received a new thread notification. This is indicated by 597a6321a8eSPavel Labath // GetSignalInfo() returning si_code == SI_USER and si_pid == 0 598a6321a8eSPavel Labath LLDB_LOG(log, "received notification about an unknown tid {0}.", pid); 599b9cc0c75SPavel Labath 600b9c1b51eSKate Stone if (info_err.Fail()) { 601a6321a8eSPavel Labath LLDB_LOG(log, 602a6321a8eSPavel Labath "(tid {0}) GetSignalInfo failed ({1}). " 603a6321a8eSPavel Labath "Ingoring this notification.", 604a6321a8eSPavel Labath pid, info_err); 605b9cc0c75SPavel Labath return; 606b9cc0c75SPavel Labath } 607b9cc0c75SPavel Labath 608a6321a8eSPavel Labath LLDB_LOG(log, "tid {0}, si_code: {1}, si_pid: {2}", pid, info.si_code, 609a6321a8eSPavel Labath info.si_pid); 610b9cc0c75SPavel Labath 611b9cc0c75SPavel Labath auto thread_sp = AddThread(pid); 612b9cc0c75SPavel Labath // Resume the newly created thread. 613b9cc0c75SPavel Labath ResumeThread(*thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); 614b9cc0c75SPavel Labath ThreadWasCreated(*thread_sp); 615b9cc0c75SPavel Labath return; 616b9cc0c75SPavel Labath } 617b9cc0c75SPavel Labath 618b9cc0c75SPavel Labath // Get details on the signal raised. 619b9c1b51eSKate Stone if (info_err.Success()) { 620fa03ad2eSChaoren Lin // We have retrieved the signal info. Dispatch appropriately. 621fa03ad2eSChaoren Lin if (info.si_signo == SIGTRAP) 622b9cc0c75SPavel Labath MonitorSIGTRAP(info, *thread_sp); 623fa03ad2eSChaoren Lin else 624b9cc0c75SPavel Labath MonitorSignal(info, *thread_sp, exited); 625b9c1b51eSKate Stone } else { 626b9c1b51eSKate Stone if (info_err.GetError() == EINVAL) { 627fa03ad2eSChaoren Lin // This is a group stop reception for this tid. 628b9c1b51eSKate Stone // We can reach here if we reinject SIGSTOP, SIGSTP, SIGTTIN or SIGTTOU 629a6321a8eSPavel Labath // into the tracee, triggering the group-stop mechanism. Normally 630a6321a8eSPavel Labath // receiving these would stop the process, pending a SIGCONT. Simulating 631a6321a8eSPavel Labath // this state in a debugger is hard and is generally not needed (one use 632a6321a8eSPavel Labath // case is debugging background task being managed by a shell). For 633a6321a8eSPavel Labath // general use, it is sufficient to stop the process in a signal-delivery 634b9c1b51eSKate Stone // stop which happens before the group stop. This done by MonitorSignal 635a6321a8eSPavel Labath // and works correctly for all signals. 636a6321a8eSPavel Labath LLDB_LOG(log, 637a6321a8eSPavel Labath "received a group stop for pid {0} tid {1}. Transparent " 638a6321a8eSPavel Labath "handling of group stops not supported, resuming the " 639a6321a8eSPavel Labath "thread.", 640a6321a8eSPavel Labath GetID(), pid); 641b9c1b51eSKate Stone ResumeThread(*thread_sp, thread_sp->GetState(), 642b9c1b51eSKate Stone LLDB_INVALID_SIGNAL_NUMBER); 643b9c1b51eSKate Stone } else { 644af245d11STodd Fiala // ptrace(GETSIGINFO) failed (but not due to group-stop). 645af245d11STodd Fiala 646b9c1b51eSKate Stone // A return value of ESRCH means the thread/process is no longer on the 647a6321a8eSPavel Labath // system, so it was killed somehow outside of our control. Either way, 648a6321a8eSPavel Labath // we can't do anything with it anymore. 649af245d11STodd Fiala 650b9c1b51eSKate Stone // Stop tracking the metadata for the thread since it's entirely off the 651b9c1b51eSKate Stone // system now. 6521107b5a5SPavel Labath const bool thread_found = StopTrackingThread(pid); 653af245d11STodd Fiala 654a6321a8eSPavel Labath LLDB_LOG(log, 655a6321a8eSPavel Labath "GetSignalInfo failed: {0}, tid = {1}, signal = {2}, " 656a6321a8eSPavel Labath "status = {3}, main_thread = {4}, thread_found: {5}", 657a6321a8eSPavel Labath info_err, pid, signal, status, is_main_thread, thread_found); 658af245d11STodd Fiala 659b9c1b51eSKate Stone if (is_main_thread) { 660b9c1b51eSKate Stone // Notify the delegate - our process is not available but appears to 661b9c1b51eSKate Stone // have been killed outside 662af245d11STodd Fiala // our control. Is eStateExited the right exit state in this case? 663b9c1b51eSKate Stone SetExitStatus(convert_pid_status_to_exit_type(status), 664b9c1b51eSKate Stone convert_pid_status_to_return_code(status), nullptr, true); 6651107b5a5SPavel Labath SetState(StateType::eStateExited, true); 666b9c1b51eSKate Stone } else { 667b9c1b51eSKate Stone // This thread was pulled out from underneath us. Anything to do here? 668b9c1b51eSKate Stone // Do we want to do an all stop? 669a6321a8eSPavel Labath LLDB_LOG(log, 670a6321a8eSPavel Labath "pid {0} tid {1} non-main thread exit occurred, didn't " 671a6321a8eSPavel Labath "tell delegate anything since thread disappeared out " 672a6321a8eSPavel Labath "from underneath us", 673a6321a8eSPavel Labath GetID(), pid); 674af245d11STodd Fiala } 675af245d11STodd Fiala } 676af245d11STodd Fiala } 677af245d11STodd Fiala } 678af245d11STodd Fiala 679b9c1b51eSKate Stone void NativeProcessLinux::WaitForNewThread(::pid_t tid) { 680a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 681426bdf88SPavel Labath 682f9077782SPavel Labath NativeThreadLinuxSP new_thread_sp = GetThreadByID(tid); 683426bdf88SPavel Labath 684b9c1b51eSKate Stone if (new_thread_sp) { 685b9c1b51eSKate Stone // We are already tracking the thread - we got the event on the new thread 686b9c1b51eSKate Stone // (see 687426bdf88SPavel Labath // MonitorSignal) before this one. We are done. 688426bdf88SPavel Labath return; 689426bdf88SPavel Labath } 690426bdf88SPavel Labath 691426bdf88SPavel Labath // The thread is not tracked yet, let's wait for it to appear. 692426bdf88SPavel Labath int status = -1; 693426bdf88SPavel Labath ::pid_t wait_pid; 694b9c1b51eSKate Stone do { 695a6321a8eSPavel Labath LLDB_LOG(log, 696a6321a8eSPavel Labath "received thread creation event for tid {0}. tid not tracked " 697a6321a8eSPavel Labath "yet, waiting for thread to appear...", 698a6321a8eSPavel Labath tid); 699426bdf88SPavel Labath wait_pid = waitpid(tid, &status, __WALL); 700b9c1b51eSKate Stone } while (wait_pid == -1 && errno == EINTR); 701b9c1b51eSKate Stone // Since we are waiting on a specific tid, this must be the creation event. 702a6321a8eSPavel Labath // But let's do some checks just in case. 703426bdf88SPavel Labath if (wait_pid != tid) { 704a6321a8eSPavel Labath LLDB_LOG(log, 705a6321a8eSPavel Labath "waiting for tid {0} failed. Assuming the thread has " 706a6321a8eSPavel Labath "disappeared in the meantime", 707a6321a8eSPavel Labath tid); 708426bdf88SPavel Labath // The only way I know of this could happen is if the whole process was 709b9c1b51eSKate Stone // SIGKILLed in the mean time. In any case, we can't do anything about that 710b9c1b51eSKate Stone // now. 711426bdf88SPavel Labath return; 712426bdf88SPavel Labath } 713b9c1b51eSKate Stone if (WIFEXITED(status)) { 714a6321a8eSPavel Labath LLDB_LOG(log, 715a6321a8eSPavel Labath "waiting for tid {0} returned an 'exited' event. Not " 716a6321a8eSPavel Labath "tracking the thread.", 717a6321a8eSPavel Labath tid); 718426bdf88SPavel Labath // Also a very improbable event. 719426bdf88SPavel Labath return; 720426bdf88SPavel Labath } 721426bdf88SPavel Labath 722a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0}: tracking new thread tid {1}", GetID(), tid); 723f9077782SPavel Labath new_thread_sp = AddThread(tid); 724b9cc0c75SPavel Labath ResumeThread(*new_thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); 725f9077782SPavel Labath ThreadWasCreated(*new_thread_sp); 726426bdf88SPavel Labath } 727426bdf88SPavel Labath 728b9c1b51eSKate Stone void NativeProcessLinux::MonitorSIGTRAP(const siginfo_t &info, 729b9c1b51eSKate Stone NativeThreadLinux &thread) { 730a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 731b9cc0c75SPavel Labath const bool is_main_thread = (thread.GetID() == GetID()); 732af245d11STodd Fiala 733b9cc0c75SPavel Labath assert(info.si_signo == SIGTRAP && "Unexpected child signal!"); 734af245d11STodd Fiala 735b9c1b51eSKate Stone switch (info.si_code) { 736b9c1b51eSKate Stone // TODO: these two cases are required if we want to support tracing of the 737b9c1b51eSKate Stone // inferiors' children. We'd need this to debug a monitor. 738af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)): 739af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)): 740af245d11STodd Fiala 741b9c1b51eSKate Stone case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)): { 742b9c1b51eSKate Stone // This is the notification on the parent thread which informs us of new 743b9c1b51eSKate Stone // thread 744426bdf88SPavel Labath // creation. 745b9c1b51eSKate Stone // We don't want to do anything with the parent thread so we just resume it. 746b9c1b51eSKate Stone // In case we 747b9c1b51eSKate Stone // want to implement "break on thread creation" functionality, we would need 748b9c1b51eSKate Stone // to stop 749426bdf88SPavel Labath // here. 750af245d11STodd Fiala 751af245d11STodd Fiala unsigned long event_message = 0; 752b9c1b51eSKate Stone if (GetEventMessage(thread.GetID(), &event_message).Fail()) { 753a6321a8eSPavel Labath LLDB_LOG(log, 754a6321a8eSPavel Labath "pid {0} received thread creation event but " 755a6321a8eSPavel Labath "GetEventMessage failed so we don't know the new tid", 756a6321a8eSPavel Labath thread.GetID()); 757426bdf88SPavel Labath } else 758426bdf88SPavel Labath WaitForNewThread(event_message); 759af245d11STodd Fiala 760b9cc0c75SPavel Labath ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); 761af245d11STodd Fiala break; 762af245d11STodd Fiala } 763af245d11STodd Fiala 764b9c1b51eSKate Stone case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): { 765f9077782SPavel Labath NativeThreadLinuxSP main_thread_sp; 766a6321a8eSPavel Labath LLDB_LOG(log, "received exec event, code = {0}", info.si_code ^ SIGTRAP); 767a9882ceeSTodd Fiala 7681dbc6c9cSPavel Labath // Exec clears any pending notifications. 7690e1d729bSPavel Labath m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 770fa03ad2eSChaoren Lin 771b9c1b51eSKate Stone // Remove all but the main thread here. Linux fork creates a new process 772b9c1b51eSKate Stone // which only copies the main thread. 773a6321a8eSPavel Labath LLDB_LOG(log, "exec received, stop tracking all but main thread"); 774a9882ceeSTodd Fiala 775b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 776a9882ceeSTodd Fiala const bool is_main_thread = thread_sp && thread_sp->GetID() == GetID(); 777b9c1b51eSKate Stone if (is_main_thread) { 778f9077782SPavel Labath main_thread_sp = std::static_pointer_cast<NativeThreadLinux>(thread_sp); 779a6321a8eSPavel Labath LLDB_LOG(log, "found main thread with tid {0}, keeping", 780a6321a8eSPavel Labath main_thread_sp->GetID()); 781b9c1b51eSKate Stone } else { 782a6321a8eSPavel Labath LLDB_LOG(log, "discarding non-main-thread tid {0} due to exec", 783a6321a8eSPavel Labath thread_sp->GetID()); 784a9882ceeSTodd Fiala } 785a9882ceeSTodd Fiala } 786a9882ceeSTodd Fiala 787a9882ceeSTodd Fiala m_threads.clear(); 788a9882ceeSTodd Fiala 789b9c1b51eSKate Stone if (main_thread_sp) { 790a9882ceeSTodd Fiala m_threads.push_back(main_thread_sp); 791a9882ceeSTodd Fiala SetCurrentThreadID(main_thread_sp->GetID()); 792f9077782SPavel Labath main_thread_sp->SetStoppedByExec(); 793b9c1b51eSKate Stone } else { 794a9882ceeSTodd Fiala SetCurrentThreadID(LLDB_INVALID_THREAD_ID); 795a6321a8eSPavel Labath LLDB_LOG(log, 796a6321a8eSPavel Labath "pid {0} no main thread found, discarded all threads, " 797a6321a8eSPavel Labath "we're in a no-thread state!", 798a6321a8eSPavel Labath GetID()); 799a9882ceeSTodd Fiala } 800a9882ceeSTodd Fiala 801fa03ad2eSChaoren Lin // Tell coordinator about about the "new" (since exec) stopped main thread. 802f9077782SPavel Labath ThreadWasCreated(*main_thread_sp); 803fa03ad2eSChaoren Lin 804a9882ceeSTodd Fiala // Let our delegate know we have just exec'd. 805a9882ceeSTodd Fiala NotifyDidExec(); 806a9882ceeSTodd Fiala 807a9882ceeSTodd Fiala // If we have a main thread, indicate we are stopped. 808b9c1b51eSKate Stone assert(main_thread_sp && "exec called during ptraced process but no main " 809b9c1b51eSKate Stone "thread metadata tracked"); 810fa03ad2eSChaoren Lin 811fa03ad2eSChaoren Lin // Let the process know we're stopped. 812b9cc0c75SPavel Labath StopRunningThreads(main_thread_sp->GetID()); 813a9882ceeSTodd Fiala 814af245d11STodd Fiala break; 815a9882ceeSTodd Fiala } 816af245d11STodd Fiala 817b9c1b51eSKate Stone case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): { 818af245d11STodd Fiala // The inferior process or one of its threads is about to exit. 819b9c1b51eSKate Stone // We don't want to do anything with the thread so we just resume it. In 820b9c1b51eSKate Stone // case we 821b9c1b51eSKate Stone // want to implement "break on thread exit" functionality, we would need to 822b9c1b51eSKate Stone // stop 8236e35163cSPavel Labath // here. 824fa03ad2eSChaoren Lin 825af245d11STodd Fiala unsigned long data = 0; 826b9cc0c75SPavel Labath if (GetEventMessage(thread.GetID(), &data).Fail()) 827af245d11STodd Fiala data = -1; 828af245d11STodd Fiala 829a6321a8eSPavel Labath LLDB_LOG(log, 830a6321a8eSPavel Labath "received PTRACE_EVENT_EXIT, data = {0:x}, WIFEXITED={1}, " 831a6321a8eSPavel Labath "WIFSIGNALED={2}, pid = {3}, main_thread = {4}", 832a6321a8eSPavel Labath data, WIFEXITED(data), WIFSIGNALED(data), thread.GetID(), 833a6321a8eSPavel Labath is_main_thread); 834af245d11STodd Fiala 835b9c1b51eSKate Stone if (is_main_thread) { 836b9c1b51eSKate Stone SetExitStatus(convert_pid_status_to_exit_type(data), 837b9c1b51eSKate Stone convert_pid_status_to_return_code(data), nullptr, true); 83875f47c3aSTodd Fiala } 83975f47c3aSTodd Fiala 84086852d36SPavel Labath StateType state = thread.GetState(); 841b9c1b51eSKate Stone if (!StateIsRunningState(state)) { 842b9c1b51eSKate Stone // Due to a kernel bug, we may sometimes get this stop after the inferior 843b9c1b51eSKate Stone // gets a 844b9c1b51eSKate Stone // SIGKILL. This confuses our state tracking logic in ResumeThread(), 845b9c1b51eSKate Stone // since normally, 846b9c1b51eSKate Stone // we should not be receiving any ptrace events while the inferior is 847b9c1b51eSKate Stone // stopped. This 84886852d36SPavel Labath // makes sure that the inferior is resumed and exits normally. 84986852d36SPavel Labath state = eStateRunning; 85086852d36SPavel Labath } 85186852d36SPavel Labath ResumeThread(thread, state, LLDB_INVALID_SIGNAL_NUMBER); 852af245d11STodd Fiala 853af245d11STodd Fiala break; 854af245d11STodd Fiala } 855af245d11STodd Fiala 856af245d11STodd Fiala case 0: 857c16f5dcaSChaoren Lin case TRAP_TRACE: // We receive this on single stepping. 858c16f5dcaSChaoren Lin case TRAP_HWBKPT: // We receive this on watchpoint hit 85986fd8e45SChaoren Lin { 860c16f5dcaSChaoren Lin // If a watchpoint was hit, report it 861c16f5dcaSChaoren Lin uint32_t wp_index; 862b9c1b51eSKate Stone Error error = thread.GetRegisterContext()->GetWatchpointHitIndex( 863b9c1b51eSKate Stone wp_index, (uintptr_t)info.si_addr); 864a6321a8eSPavel Labath if (error.Fail()) 865a6321a8eSPavel Labath LLDB_LOG(log, 866a6321a8eSPavel Labath "received error while checking for watchpoint hits, pid = " 867a6321a8eSPavel Labath "{0}, error = {1}", 868a6321a8eSPavel Labath thread.GetID(), error); 869b9c1b51eSKate Stone if (wp_index != LLDB_INVALID_INDEX32) { 870b9cc0c75SPavel Labath MonitorWatchpoint(thread, wp_index); 871c16f5dcaSChaoren Lin break; 872c16f5dcaSChaoren Lin } 873b9cc0c75SPavel Labath 874be379e15STamas Berghammer // Otherwise, report step over 875be379e15STamas Berghammer MonitorTrace(thread); 876af245d11STodd Fiala break; 877b9cc0c75SPavel Labath } 878af245d11STodd Fiala 879af245d11STodd Fiala case SI_KERNEL: 88035799963SMohit K. Bhakkad #if defined __mips__ 88135799963SMohit K. Bhakkad // For mips there is no special signal for watchpoint 88235799963SMohit K. Bhakkad // So we check for watchpoint in kernel trap 88335799963SMohit K. Bhakkad { 88435799963SMohit K. Bhakkad // If a watchpoint was hit, report it 88535799963SMohit K. Bhakkad uint32_t wp_index; 886b9c1b51eSKate Stone Error error = thread.GetRegisterContext()->GetWatchpointHitIndex( 887b9c1b51eSKate Stone wp_index, LLDB_INVALID_ADDRESS); 888a6321a8eSPavel Labath if (error.Fail()) 889a6321a8eSPavel Labath LLDB_LOG(log, 890a6321a8eSPavel Labath "received error while checking for watchpoint hits, pid = " 891a6321a8eSPavel Labath "{0}, error = {1}", 892a6321a8eSPavel Labath thread.GetID(), error); 893b9c1b51eSKate Stone if (wp_index != LLDB_INVALID_INDEX32) { 894b9cc0c75SPavel Labath MonitorWatchpoint(thread, wp_index); 89535799963SMohit K. Bhakkad break; 89635799963SMohit K. Bhakkad } 89735799963SMohit K. Bhakkad } 89835799963SMohit K. Bhakkad // NO BREAK 89935799963SMohit K. Bhakkad #endif 900af245d11STodd Fiala case TRAP_BRKPT: 901b9cc0c75SPavel Labath MonitorBreakpoint(thread); 902af245d11STodd Fiala break; 903af245d11STodd Fiala 904af245d11STodd Fiala case SIGTRAP: 905af245d11STodd Fiala case (SIGTRAP | 0x80): 906a6321a8eSPavel Labath LLDB_LOG( 907a6321a8eSPavel Labath log, 908a6321a8eSPavel Labath "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming", 909a6321a8eSPavel Labath info.si_code, GetID(), thread.GetID()); 910fa03ad2eSChaoren Lin 911af245d11STodd Fiala // Ignore these signals until we know more about them. 912b9cc0c75SPavel Labath ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); 913af245d11STodd Fiala break; 914af245d11STodd Fiala 915af245d11STodd Fiala default: 916a6321a8eSPavel Labath LLDB_LOG( 917a6321a8eSPavel Labath log, 918a6321a8eSPavel Labath "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming", 919a6321a8eSPavel Labath info.si_code, GetID(), thread.GetID()); 920a6321a8eSPavel Labath llvm_unreachable("Unexpected SIGTRAP code!"); 921af245d11STodd Fiala break; 922af245d11STodd Fiala } 923af245d11STodd Fiala } 924af245d11STodd Fiala 925b9c1b51eSKate Stone void NativeProcessLinux::MonitorTrace(NativeThreadLinux &thread) { 926a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 927a6321a8eSPavel Labath LLDB_LOG(log, "received trace event, pid = {0}", thread.GetID()); 928c16f5dcaSChaoren Lin 9290e1d729bSPavel Labath // This thread is currently stopped. 930b9cc0c75SPavel Labath thread.SetStoppedByTrace(); 931c16f5dcaSChaoren Lin 932b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 933c16f5dcaSChaoren Lin } 934c16f5dcaSChaoren Lin 935b9c1b51eSKate Stone void NativeProcessLinux::MonitorBreakpoint(NativeThreadLinux &thread) { 936b9c1b51eSKate Stone Log *log( 937b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 938a6321a8eSPavel Labath LLDB_LOG(log, "received breakpoint event, pid = {0}", thread.GetID()); 939c16f5dcaSChaoren Lin 940c16f5dcaSChaoren Lin // Mark the thread as stopped at breakpoint. 941b9cc0c75SPavel Labath thread.SetStoppedByBreakpoint(); 942b9cc0c75SPavel Labath Error error = FixupBreakpointPCAsNeeded(thread); 943c16f5dcaSChaoren Lin if (error.Fail()) 944a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0} fixup: {1}", thread.GetID(), error); 945d8c338d4STamas Berghammer 946b9c1b51eSKate Stone if (m_threads_stepping_with_breakpoint.find(thread.GetID()) != 947b9c1b51eSKate Stone m_threads_stepping_with_breakpoint.end()) 948b9cc0c75SPavel Labath thread.SetStoppedByTrace(); 949c16f5dcaSChaoren Lin 950b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 951c16f5dcaSChaoren Lin } 952c16f5dcaSChaoren Lin 953b9c1b51eSKate Stone void NativeProcessLinux::MonitorWatchpoint(NativeThreadLinux &thread, 954b9c1b51eSKate Stone uint32_t wp_index) { 955b9c1b51eSKate Stone Log *log( 956b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_WATCHPOINTS)); 957a6321a8eSPavel Labath LLDB_LOG(log, "received watchpoint event, pid = {0}, wp_index = {1}", 958a6321a8eSPavel Labath thread.GetID(), wp_index); 959c16f5dcaSChaoren Lin 960c16f5dcaSChaoren Lin // Mark the thread as stopped at watchpoint. 961c16f5dcaSChaoren Lin // The address is at (lldb::addr_t)info->si_addr if we need it. 962f9077782SPavel Labath thread.SetStoppedByWatchpoint(wp_index); 963c16f5dcaSChaoren Lin 964b9c1b51eSKate Stone // We need to tell all other running threads before we notify the delegate 965b9c1b51eSKate Stone // about this stop. 966f9077782SPavel Labath StopRunningThreads(thread.GetID()); 967c16f5dcaSChaoren Lin } 968c16f5dcaSChaoren Lin 969b9c1b51eSKate Stone void NativeProcessLinux::MonitorSignal(const siginfo_t &info, 970b9c1b51eSKate Stone NativeThreadLinux &thread, bool exited) { 971b9cc0c75SPavel Labath const int signo = info.si_signo; 972b9cc0c75SPavel Labath const bool is_from_llgs = info.si_pid == getpid(); 973af245d11STodd Fiala 974a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 975af245d11STodd Fiala 976af245d11STodd Fiala // POSIX says that process behaviour is undefined after it ignores a SIGFPE, 977af245d11STodd Fiala // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a 978af245d11STodd Fiala // kill(2) or raise(3). Similarly for tgkill(2) on Linux. 979af245d11STodd Fiala // 980af245d11STodd Fiala // IOW, user generated signals never generate what we consider to be a 981af245d11STodd Fiala // "crash". 982af245d11STodd Fiala // 983af245d11STodd Fiala // Similarly, ACK signals generated by this monitor. 984af245d11STodd Fiala 985af245d11STodd Fiala // Handle the signal. 986a6321a8eSPavel Labath LLDB_LOG(log, 987a6321a8eSPavel Labath "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, " 988a6321a8eSPavel Labath "waitpid pid = {4})", 989a6321a8eSPavel Labath Host::GetSignalAsCString(signo), signo, info.si_code, 990b9cc0c75SPavel Labath thread.GetID()); 99158a2f669STodd Fiala 99258a2f669STodd Fiala // Check for thread stop notification. 993b9c1b51eSKate Stone if (is_from_llgs && (info.si_code == SI_TKILL) && (signo == SIGSTOP)) { 994af245d11STodd Fiala // This is a tgkill()-based stop. 995a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} tid {1}, thread stopped", GetID(), thread.GetID()); 996fa03ad2eSChaoren Lin 997aab58633SChaoren Lin // Check that we're not already marked with a stop reason. 998b9c1b51eSKate Stone // Note this thread really shouldn't already be marked as stopped - if we 999a6321a8eSPavel Labath // were, that would imply that the kernel signaled us with the thread 1000a6321a8eSPavel Labath // stopping which we handled and marked as stopped, and that, without an 1001a6321a8eSPavel Labath // intervening resume, we received another stop. It is more likely that we 1002a6321a8eSPavel Labath // are missing the marking of a run state somewhere if we find that the 1003a6321a8eSPavel Labath // thread was marked as stopped. 1004b9cc0c75SPavel Labath const StateType thread_state = thread.GetState(); 1005b9c1b51eSKate Stone if (!StateIsStoppedState(thread_state, false)) { 1006ed89c7feSPavel Labath // An inferior thread has stopped because of a SIGSTOP we have sent it. 1007b9c1b51eSKate Stone // Generally, these are not important stops and we don't want to report 1008a6321a8eSPavel Labath // them as they are just used to stop other threads when one thread (the 1009a6321a8eSPavel Labath // one with the *real* stop reason) hits a breakpoint (watchpoint, 1010a6321a8eSPavel Labath // etc...). However, in the case of an asynchronous Interrupt(), this *is* 1011a6321a8eSPavel Labath // the real stop reason, so we leave the signal intact if this is the 1012a6321a8eSPavel Labath // thread that was chosen as the triggering thread. 1013b9c1b51eSKate Stone if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) { 1014b9cc0c75SPavel Labath if (m_pending_notification_tid == thread.GetID()) 1015b9cc0c75SPavel Labath thread.SetStoppedBySignal(SIGSTOP, &info); 1016ed89c7feSPavel Labath else 1017b9cc0c75SPavel Labath thread.SetStoppedWithNoReason(); 1018ed89c7feSPavel Labath 1019b9cc0c75SPavel Labath SetCurrentThreadID(thread.GetID()); 10200e1d729bSPavel Labath SignalIfAllThreadsStopped(); 1021b9c1b51eSKate Stone } else { 10220e1d729bSPavel Labath // We can end up here if stop was initiated by LLGS but by this time a 10230e1d729bSPavel Labath // thread stop has occurred - maybe initiated by another event. 1024b9cc0c75SPavel Labath Error error = ResumeThread(thread, thread.GetState(), 0); 1025a6321a8eSPavel Labath if (error.Fail()) 1026a6321a8eSPavel Labath LLDB_LOG(log, "failed to resume thread {0}: {1}", thread.GetID(), 1027a6321a8eSPavel Labath error); 10280e1d729bSPavel Labath } 1029b9c1b51eSKate Stone } else { 1030a6321a8eSPavel Labath LLDB_LOG(log, 1031a6321a8eSPavel Labath "pid {0} tid {1}, thread was already marked as a stopped " 1032a6321a8eSPavel Labath "state (state={2}), leaving stop signal as is", 10338198db30SPavel Labath GetID(), thread.GetID(), thread_state); 10340e1d729bSPavel Labath SignalIfAllThreadsStopped(); 1035af245d11STodd Fiala } 1036af245d11STodd Fiala 103758a2f669STodd Fiala // Done handling. 1038af245d11STodd Fiala return; 1039af245d11STodd Fiala } 1040af245d11STodd Fiala 104186fd8e45SChaoren Lin // This thread is stopped. 1042a6321a8eSPavel Labath LLDB_LOG(log, "received signal {0}", Host::GetSignalAsCString(signo)); 1043b9cc0c75SPavel Labath thread.SetStoppedBySignal(signo, &info); 104486fd8e45SChaoren Lin 104586fd8e45SChaoren Lin // Send a stop to the debugger after we get all other threads to stop. 1046b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 1047511e5cdcSTodd Fiala } 1048af245d11STodd Fiala 1049e7708688STamas Berghammer namespace { 1050e7708688STamas Berghammer 1051b9c1b51eSKate Stone struct EmulatorBaton { 1052e7708688STamas Berghammer NativeProcessLinux *m_process; 1053e7708688STamas Berghammer NativeRegisterContext *m_reg_context; 10546648fcc3SPavel Labath 10556648fcc3SPavel Labath // eRegisterKindDWARF -> RegsiterValue 10566648fcc3SPavel Labath std::unordered_map<uint32_t, RegisterValue> m_register_values; 1057e7708688STamas Berghammer 1058b9c1b51eSKate Stone EmulatorBaton(NativeProcessLinux *process, NativeRegisterContext *reg_context) 1059b9c1b51eSKate Stone : m_process(process), m_reg_context(reg_context) {} 1060e7708688STamas Berghammer }; 1061e7708688STamas Berghammer 1062e7708688STamas Berghammer } // anonymous namespace 1063e7708688STamas Berghammer 1064b9c1b51eSKate Stone static size_t ReadMemoryCallback(EmulateInstruction *instruction, void *baton, 1065e7708688STamas Berghammer const EmulateInstruction::Context &context, 1066b9c1b51eSKate Stone lldb::addr_t addr, void *dst, size_t length) { 1067e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 1068e7708688STamas Berghammer 10693eb4b458SChaoren Lin size_t bytes_read; 1070e7708688STamas Berghammer emulator_baton->m_process->ReadMemory(addr, dst, length, bytes_read); 1071e7708688STamas Berghammer return bytes_read; 1072e7708688STamas Berghammer } 1073e7708688STamas Berghammer 1074b9c1b51eSKate Stone static bool ReadRegisterCallback(EmulateInstruction *instruction, void *baton, 1075e7708688STamas Berghammer const RegisterInfo *reg_info, 1076b9c1b51eSKate Stone RegisterValue ®_value) { 1077e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 1078e7708688STamas Berghammer 1079b9c1b51eSKate Stone auto it = emulator_baton->m_register_values.find( 1080b9c1b51eSKate Stone reg_info->kinds[eRegisterKindDWARF]); 1081b9c1b51eSKate Stone if (it != emulator_baton->m_register_values.end()) { 10826648fcc3SPavel Labath reg_value = it->second; 10836648fcc3SPavel Labath return true; 10846648fcc3SPavel Labath } 10856648fcc3SPavel Labath 1086e7708688STamas Berghammer // The emulator only fill in the dwarf regsiter numbers (and in some case 1087e7708688STamas Berghammer // the generic register numbers). Get the full register info from the 1088e7708688STamas Berghammer // register context based on the dwarf register numbers. 1089b9c1b51eSKate Stone const RegisterInfo *full_reg_info = 1090b9c1b51eSKate Stone emulator_baton->m_reg_context->GetRegisterInfo( 1091e7708688STamas Berghammer eRegisterKindDWARF, reg_info->kinds[eRegisterKindDWARF]); 1092e7708688STamas Berghammer 1093b9c1b51eSKate Stone Error error = 1094b9c1b51eSKate Stone emulator_baton->m_reg_context->ReadRegister(full_reg_info, reg_value); 10956648fcc3SPavel Labath if (error.Success()) 10966648fcc3SPavel Labath return true; 1097cdc22a88SMohit K. Bhakkad 10986648fcc3SPavel Labath return false; 1099e7708688STamas Berghammer } 1100e7708688STamas Berghammer 1101b9c1b51eSKate Stone static bool WriteRegisterCallback(EmulateInstruction *instruction, void *baton, 1102e7708688STamas Berghammer const EmulateInstruction::Context &context, 1103e7708688STamas Berghammer const RegisterInfo *reg_info, 1104b9c1b51eSKate Stone const RegisterValue ®_value) { 1105e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 1106b9c1b51eSKate Stone emulator_baton->m_register_values[reg_info->kinds[eRegisterKindDWARF]] = 1107b9c1b51eSKate Stone reg_value; 1108e7708688STamas Berghammer return true; 1109e7708688STamas Berghammer } 1110e7708688STamas Berghammer 1111b9c1b51eSKate Stone static size_t WriteMemoryCallback(EmulateInstruction *instruction, void *baton, 1112e7708688STamas Berghammer const EmulateInstruction::Context &context, 1113b9c1b51eSKate Stone lldb::addr_t addr, const void *dst, 1114b9c1b51eSKate Stone size_t length) { 1115e7708688STamas Berghammer return length; 1116e7708688STamas Berghammer } 1117e7708688STamas Berghammer 1118b9c1b51eSKate Stone static lldb::addr_t ReadFlags(NativeRegisterContext *regsiter_context) { 1119e7708688STamas Berghammer const RegisterInfo *flags_info = regsiter_context->GetRegisterInfo( 1120e7708688STamas Berghammer eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 1121b9c1b51eSKate Stone return regsiter_context->ReadRegisterAsUnsigned(flags_info, 1122b9c1b51eSKate Stone LLDB_INVALID_ADDRESS); 1123e7708688STamas Berghammer } 1124e7708688STamas Berghammer 1125b9c1b51eSKate Stone Error NativeProcessLinux::SetupSoftwareSingleStepping( 1126b9c1b51eSKate Stone NativeThreadLinux &thread) { 1127e7708688STamas Berghammer Error error; 1128b9cc0c75SPavel Labath NativeRegisterContextSP register_context_sp = thread.GetRegisterContext(); 1129e7708688STamas Berghammer 1130e7708688STamas Berghammer std::unique_ptr<EmulateInstruction> emulator_ap( 1131b9c1b51eSKate Stone EmulateInstruction::FindPlugin(m_arch, eInstructionTypePCModifying, 1132b9c1b51eSKate Stone nullptr)); 1133e7708688STamas Berghammer 1134e7708688STamas Berghammer if (emulator_ap == nullptr) 1135e7708688STamas Berghammer return Error("Instruction emulator not found!"); 1136e7708688STamas Berghammer 1137e7708688STamas Berghammer EmulatorBaton baton(this, register_context_sp.get()); 1138e7708688STamas Berghammer emulator_ap->SetBaton(&baton); 1139e7708688STamas Berghammer emulator_ap->SetReadMemCallback(&ReadMemoryCallback); 1140e7708688STamas Berghammer emulator_ap->SetReadRegCallback(&ReadRegisterCallback); 1141e7708688STamas Berghammer emulator_ap->SetWriteMemCallback(&WriteMemoryCallback); 1142e7708688STamas Berghammer emulator_ap->SetWriteRegCallback(&WriteRegisterCallback); 1143e7708688STamas Berghammer 1144e7708688STamas Berghammer if (!emulator_ap->ReadInstruction()) 1145e7708688STamas Berghammer return Error("Read instruction failed!"); 1146e7708688STamas Berghammer 1147b9c1b51eSKate Stone bool emulation_result = 1148b9c1b51eSKate Stone emulator_ap->EvaluateInstruction(eEmulateInstructionOptionAutoAdvancePC); 11496648fcc3SPavel Labath 1150b9c1b51eSKate Stone const RegisterInfo *reg_info_pc = register_context_sp->GetRegisterInfo( 1151b9c1b51eSKate Stone eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); 1152b9c1b51eSKate Stone const RegisterInfo *reg_info_flags = register_context_sp->GetRegisterInfo( 1153b9c1b51eSKate Stone eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 11546648fcc3SPavel Labath 1155b9c1b51eSKate Stone auto pc_it = 1156b9c1b51eSKate Stone baton.m_register_values.find(reg_info_pc->kinds[eRegisterKindDWARF]); 1157b9c1b51eSKate Stone auto flags_it = 1158b9c1b51eSKate Stone baton.m_register_values.find(reg_info_flags->kinds[eRegisterKindDWARF]); 11596648fcc3SPavel Labath 1160e7708688STamas Berghammer lldb::addr_t next_pc; 1161e7708688STamas Berghammer lldb::addr_t next_flags; 1162b9c1b51eSKate Stone if (emulation_result) { 1163b9c1b51eSKate Stone assert(pc_it != baton.m_register_values.end() && 1164b9c1b51eSKate Stone "Emulation was successfull but PC wasn't updated"); 11656648fcc3SPavel Labath next_pc = pc_it->second.GetAsUInt64(); 11666648fcc3SPavel Labath 11676648fcc3SPavel Labath if (flags_it != baton.m_register_values.end()) 11686648fcc3SPavel Labath next_flags = flags_it->second.GetAsUInt64(); 1169e7708688STamas Berghammer else 1170e7708688STamas Berghammer next_flags = ReadFlags(register_context_sp.get()); 1171b9c1b51eSKate Stone } else if (pc_it == baton.m_register_values.end()) { 1172e7708688STamas Berghammer // Emulate instruction failed and it haven't changed PC. Advance PC 1173e7708688STamas Berghammer // with the size of the current opcode because the emulation of all 1174e7708688STamas Berghammer // PC modifying instruction should be successful. The failure most 1175e7708688STamas Berghammer // likely caused by a not supported instruction which don't modify PC. 1176b9c1b51eSKate Stone next_pc = 1177b9c1b51eSKate Stone register_context_sp->GetPC() + emulator_ap->GetOpcode().GetByteSize(); 1178e7708688STamas Berghammer next_flags = ReadFlags(register_context_sp.get()); 1179b9c1b51eSKate Stone } else { 1180e7708688STamas Berghammer // The instruction emulation failed after it modified the PC. It is an 1181e7708688STamas Berghammer // unknown error where we can't continue because the next instruction is 1182e7708688STamas Berghammer // modifying the PC but we don't know how. 1183e7708688STamas Berghammer return Error("Instruction emulation failed unexpectedly."); 1184e7708688STamas Berghammer } 1185e7708688STamas Berghammer 1186b9c1b51eSKate Stone if (m_arch.GetMachine() == llvm::Triple::arm) { 1187b9c1b51eSKate Stone if (next_flags & 0x20) { 1188e7708688STamas Berghammer // Thumb mode 1189e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 2); 1190b9c1b51eSKate Stone } else { 1191e7708688STamas Berghammer // Arm mode 1192e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 4); 1193e7708688STamas Berghammer } 1194b9c1b51eSKate Stone } else if (m_arch.GetMachine() == llvm::Triple::mips64 || 1195b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64el || 1196b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips || 1197b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mipsel) 1198cdc22a88SMohit K. Bhakkad error = SetSoftwareBreakpoint(next_pc, 4); 1199b9c1b51eSKate Stone else { 1200e7708688STamas Berghammer // No size hint is given for the next breakpoint 1201e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 0); 1202e7708688STamas Berghammer } 1203e7708688STamas Berghammer 120442eb6908SPavel Labath // If setting the breakpoint fails because next_pc is out of 120542eb6908SPavel Labath // the address space, ignore it and let the debugee segfault. 120642eb6908SPavel Labath if (error.GetError() == EIO || error.GetError() == EFAULT) { 1207665be50eSMehdi Amini return Error(); 120842eb6908SPavel Labath } else if (error.Fail()) 1209e7708688STamas Berghammer return error; 1210e7708688STamas Berghammer 1211b9cc0c75SPavel Labath m_threads_stepping_with_breakpoint.insert({thread.GetID(), next_pc}); 1212e7708688STamas Berghammer 1213665be50eSMehdi Amini return Error(); 1214e7708688STamas Berghammer } 1215e7708688STamas Berghammer 1216b9c1b51eSKate Stone bool NativeProcessLinux::SupportHardwareSingleStepping() const { 1217b9c1b51eSKate Stone if (m_arch.GetMachine() == llvm::Triple::arm || 1218b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64 || 1219b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64el || 1220b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips || 1221b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mipsel) 1222cdc22a88SMohit K. Bhakkad return false; 1223cdc22a88SMohit K. Bhakkad return true; 1224e7708688STamas Berghammer } 1225e7708688STamas Berghammer 1226b9c1b51eSKate Stone Error NativeProcessLinux::Resume(const ResumeActionList &resume_actions) { 1227a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1228a6321a8eSPavel Labath LLDB_LOG(log, "pid {0}", GetID()); 1229af245d11STodd Fiala 1230e7708688STamas Berghammer bool software_single_step = !SupportHardwareSingleStepping(); 1231af245d11STodd Fiala 1232b9c1b51eSKate Stone if (software_single_step) { 1233b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 1234e7708688STamas Berghammer assert(thread_sp && "thread list should not contain NULL threads"); 1235e7708688STamas Berghammer 1236b9c1b51eSKate Stone const ResumeAction *const action = 1237b9c1b51eSKate Stone resume_actions.GetActionForThread(thread_sp->GetID(), true); 1238e7708688STamas Berghammer if (action == nullptr) 1239e7708688STamas Berghammer continue; 1240e7708688STamas Berghammer 1241b9c1b51eSKate Stone if (action->state == eStateStepping) { 1242b9c1b51eSKate Stone Error error = SetupSoftwareSingleStepping( 1243b9c1b51eSKate Stone static_cast<NativeThreadLinux &>(*thread_sp)); 1244e7708688STamas Berghammer if (error.Fail()) 1245e7708688STamas Berghammer return error; 1246e7708688STamas Berghammer } 1247e7708688STamas Berghammer } 1248e7708688STamas Berghammer } 1249e7708688STamas Berghammer 1250b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 1251af245d11STodd Fiala assert(thread_sp && "thread list should not contain NULL threads"); 1252af245d11STodd Fiala 1253b9c1b51eSKate Stone const ResumeAction *const action = 1254b9c1b51eSKate Stone resume_actions.GetActionForThread(thread_sp->GetID(), true); 12556a196ce6SChaoren Lin 1256b9c1b51eSKate Stone if (action == nullptr) { 1257a6321a8eSPavel Labath LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(), 1258a6321a8eSPavel Labath thread_sp->GetID()); 12596a196ce6SChaoren Lin continue; 12606a196ce6SChaoren Lin } 1261af245d11STodd Fiala 1262a6321a8eSPavel Labath LLDB_LOG(log, "processing resume action state {0} for pid {1} tid {2}", 12638198db30SPavel Labath action->state, GetID(), thread_sp->GetID()); 1264af245d11STodd Fiala 1265b9c1b51eSKate Stone switch (action->state) { 1266af245d11STodd Fiala case eStateRunning: 1267b9c1b51eSKate Stone case eStateStepping: { 1268af245d11STodd Fiala // Run the thread, possibly feeding it the signal. 1269fa03ad2eSChaoren Lin const int signo = action->signal; 1270b9c1b51eSKate Stone ResumeThread(static_cast<NativeThreadLinux &>(*thread_sp), action->state, 1271b9c1b51eSKate Stone signo); 1272af245d11STodd Fiala break; 1273ae29d395SChaoren Lin } 1274af245d11STodd Fiala 1275af245d11STodd Fiala case eStateSuspended: 1276af245d11STodd Fiala case eStateStopped: 1277a6321a8eSPavel Labath llvm_unreachable("Unexpected state"); 1278af245d11STodd Fiala 1279af245d11STodd Fiala default: 1280b9c1b51eSKate Stone return Error("NativeProcessLinux::%s (): unexpected state %s specified " 1281b9c1b51eSKate Stone "for pid %" PRIu64 ", tid %" PRIu64, 1282b9c1b51eSKate Stone __FUNCTION__, StateAsCString(action->state), GetID(), 1283b9c1b51eSKate Stone thread_sp->GetID()); 1284af245d11STodd Fiala } 1285af245d11STodd Fiala } 1286af245d11STodd Fiala 1287665be50eSMehdi Amini return Error(); 1288af245d11STodd Fiala } 1289af245d11STodd Fiala 1290b9c1b51eSKate Stone Error NativeProcessLinux::Halt() { 1291af245d11STodd Fiala Error error; 1292af245d11STodd Fiala 1293af245d11STodd Fiala if (kill(GetID(), SIGSTOP) != 0) 1294af245d11STodd Fiala error.SetErrorToErrno(); 1295af245d11STodd Fiala 1296af245d11STodd Fiala return error; 1297af245d11STodd Fiala } 1298af245d11STodd Fiala 1299b9c1b51eSKate Stone Error NativeProcessLinux::Detach() { 1300af245d11STodd Fiala Error error; 1301af245d11STodd Fiala 1302af245d11STodd Fiala // Stop monitoring the inferior. 130319cbe96aSPavel Labath m_sigchld_handle.reset(); 1304af245d11STodd Fiala 13057a9495bcSPavel Labath // Tell ptrace to detach from the process. 13067a9495bcSPavel Labath if (GetID() == LLDB_INVALID_PROCESS_ID) 13077a9495bcSPavel Labath return error; 13087a9495bcSPavel Labath 1309b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 13107a9495bcSPavel Labath Error e = Detach(thread_sp->GetID()); 13117a9495bcSPavel Labath if (e.Fail()) 1312b9c1b51eSKate Stone error = 1313b9c1b51eSKate Stone e; // Save the error, but still attempt to detach from other threads. 13147a9495bcSPavel Labath } 13157a9495bcSPavel Labath 1316af245d11STodd Fiala return error; 1317af245d11STodd Fiala } 1318af245d11STodd Fiala 1319b9c1b51eSKate Stone Error NativeProcessLinux::Signal(int signo) { 1320af245d11STodd Fiala Error error; 1321af245d11STodd Fiala 1322a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1323a6321a8eSPavel Labath LLDB_LOG(log, "sending signal {0} ({1}) to pid {1}", signo, 1324a6321a8eSPavel Labath Host::GetSignalAsCString(signo), GetID()); 1325af245d11STodd Fiala 1326af245d11STodd Fiala if (kill(GetID(), signo)) 1327af245d11STodd Fiala error.SetErrorToErrno(); 1328af245d11STodd Fiala 1329af245d11STodd Fiala return error; 1330af245d11STodd Fiala } 1331af245d11STodd Fiala 1332b9c1b51eSKate Stone Error NativeProcessLinux::Interrupt() { 1333e9547b80SChaoren Lin // Pick a running thread (or if none, a not-dead stopped thread) as 1334e9547b80SChaoren Lin // the chosen thread that will be the stop-reason thread. 1335a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1336e9547b80SChaoren Lin 1337e9547b80SChaoren Lin NativeThreadProtocolSP running_thread_sp; 1338e9547b80SChaoren Lin NativeThreadProtocolSP stopped_thread_sp; 1339e9547b80SChaoren Lin 1340a6321a8eSPavel Labath LLDB_LOG(log, "selecting running thread for interrupt target"); 1341b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 1342e9547b80SChaoren Lin // The thread shouldn't be null but lets just cover that here. 1343e9547b80SChaoren Lin if (!thread_sp) 1344e9547b80SChaoren Lin continue; 1345e9547b80SChaoren Lin 1346e9547b80SChaoren Lin // If we have a running or stepping thread, we'll call that the 1347e9547b80SChaoren Lin // target of the interrupt. 1348e9547b80SChaoren Lin const auto thread_state = thread_sp->GetState(); 1349b9c1b51eSKate Stone if (thread_state == eStateRunning || thread_state == eStateStepping) { 1350e9547b80SChaoren Lin running_thread_sp = thread_sp; 1351e9547b80SChaoren Lin break; 1352b9c1b51eSKate Stone } else if (!stopped_thread_sp && StateIsStoppedState(thread_state, true)) { 1353b9c1b51eSKate Stone // Remember the first non-dead stopped thread. We'll use that as a backup 1354b9c1b51eSKate Stone // if there are no running threads. 1355e9547b80SChaoren Lin stopped_thread_sp = thread_sp; 1356e9547b80SChaoren Lin } 1357e9547b80SChaoren Lin } 1358e9547b80SChaoren Lin 1359b9c1b51eSKate Stone if (!running_thread_sp && !stopped_thread_sp) { 1360b9c1b51eSKate Stone Error error("found no running/stepping or live stopped threads as target " 1361b9c1b51eSKate Stone "for interrupt"); 1362a6321a8eSPavel Labath LLDB_LOG(log, "skipping due to error: {0}", error); 13635830aa75STamas Berghammer 1364e9547b80SChaoren Lin return error; 1365e9547b80SChaoren Lin } 1366e9547b80SChaoren Lin 1367b9c1b51eSKate Stone NativeThreadProtocolSP deferred_signal_thread_sp = 1368b9c1b51eSKate Stone running_thread_sp ? running_thread_sp : stopped_thread_sp; 1369e9547b80SChaoren Lin 1370a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} {1} tid {2} chosen for interrupt target", GetID(), 1371e9547b80SChaoren Lin running_thread_sp ? "running" : "stopped", 1372e9547b80SChaoren Lin deferred_signal_thread_sp->GetID()); 1373e9547b80SChaoren Lin 1374ed89c7feSPavel Labath StopRunningThreads(deferred_signal_thread_sp->GetID()); 137545f5cb31SPavel Labath 1376665be50eSMehdi Amini return Error(); 1377e9547b80SChaoren Lin } 1378e9547b80SChaoren Lin 1379b9c1b51eSKate Stone Error NativeProcessLinux::Kill() { 1380a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1381a6321a8eSPavel Labath LLDB_LOG(log, "pid {0}", GetID()); 1382af245d11STodd Fiala 1383af245d11STodd Fiala Error error; 1384af245d11STodd Fiala 1385b9c1b51eSKate Stone switch (m_state) { 1386af245d11STodd Fiala case StateType::eStateInvalid: 1387af245d11STodd Fiala case StateType::eStateExited: 1388af245d11STodd Fiala case StateType::eStateCrashed: 1389af245d11STodd Fiala case StateType::eStateDetached: 1390af245d11STodd Fiala case StateType::eStateUnloaded: 1391af245d11STodd Fiala // Nothing to do - the process is already dead. 1392a6321a8eSPavel Labath LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(), 13938198db30SPavel Labath m_state); 1394af245d11STodd Fiala return error; 1395af245d11STodd Fiala 1396af245d11STodd Fiala case StateType::eStateConnected: 1397af245d11STodd Fiala case StateType::eStateAttaching: 1398af245d11STodd Fiala case StateType::eStateLaunching: 1399af245d11STodd Fiala case StateType::eStateStopped: 1400af245d11STodd Fiala case StateType::eStateRunning: 1401af245d11STodd Fiala case StateType::eStateStepping: 1402af245d11STodd Fiala case StateType::eStateSuspended: 1403af245d11STodd Fiala // We can try to kill a process in these states. 1404af245d11STodd Fiala break; 1405af245d11STodd Fiala } 1406af245d11STodd Fiala 1407b9c1b51eSKate Stone if (kill(GetID(), SIGKILL) != 0) { 1408af245d11STodd Fiala error.SetErrorToErrno(); 1409af245d11STodd Fiala return error; 1410af245d11STodd Fiala } 1411af245d11STodd Fiala 1412af245d11STodd Fiala return error; 1413af245d11STodd Fiala } 1414af245d11STodd Fiala 1415af245d11STodd Fiala static Error 1416b9c1b51eSKate Stone ParseMemoryRegionInfoFromProcMapsLine(const std::string &maps_line, 1417b9c1b51eSKate Stone MemoryRegionInfo &memory_region_info) { 1418af245d11STodd Fiala memory_region_info.Clear(); 1419af245d11STodd Fiala 1420b9739d40SPavel Labath StringExtractor line_extractor(maps_line.c_str()); 1421af245d11STodd Fiala 1422b9c1b51eSKate Stone // Format: {address_start_hex}-{address_end_hex} perms offset dev inode 1423b9c1b51eSKate Stone // pathname 1424b9c1b51eSKate Stone // perms: rwxp (letter is present if set, '-' if not, final character is 1425b9c1b51eSKate Stone // p=private, s=shared). 1426af245d11STodd Fiala 1427af245d11STodd Fiala // Parse out the starting address 1428af245d11STodd Fiala lldb::addr_t start_address = line_extractor.GetHexMaxU64(false, 0); 1429af245d11STodd Fiala 1430af245d11STodd Fiala // Parse out hyphen separating start and end address from range. 1431af245d11STodd Fiala if (!line_extractor.GetBytesLeft() || (line_extractor.GetChar() != '-')) 1432b9c1b51eSKate Stone return Error( 1433b9c1b51eSKate Stone "malformed /proc/{pid}/maps entry, missing dash between address range"); 1434af245d11STodd Fiala 1435af245d11STodd Fiala // Parse out the ending address 1436af245d11STodd Fiala lldb::addr_t end_address = line_extractor.GetHexMaxU64(false, start_address); 1437af245d11STodd Fiala 1438af245d11STodd Fiala // Parse out the space after the address. 1439af245d11STodd Fiala if (!line_extractor.GetBytesLeft() || (line_extractor.GetChar() != ' ')) 1440af245d11STodd Fiala return Error("malformed /proc/{pid}/maps entry, missing space after range"); 1441af245d11STodd Fiala 1442af245d11STodd Fiala // Save the range. 1443af245d11STodd Fiala memory_region_info.GetRange().SetRangeBase(start_address); 1444af245d11STodd Fiala memory_region_info.GetRange().SetRangeEnd(end_address); 1445af245d11STodd Fiala 1446b9c1b51eSKate Stone // Any memory region in /proc/{pid}/maps is by definition mapped into the 1447b9c1b51eSKate Stone // process. 1448ad007563SHoward Hellyer memory_region_info.SetMapped(MemoryRegionInfo::OptionalBool::eYes); 1449ad007563SHoward Hellyer 1450af245d11STodd Fiala // Parse out each permission entry. 1451af245d11STodd Fiala if (line_extractor.GetBytesLeft() < 4) 1452b9c1b51eSKate Stone return Error("malformed /proc/{pid}/maps entry, missing some portion of " 1453b9c1b51eSKate Stone "permissions"); 1454af245d11STodd Fiala 1455af245d11STodd Fiala // Handle read permission. 1456af245d11STodd Fiala const char read_perm_char = line_extractor.GetChar(); 1457af245d11STodd Fiala if (read_perm_char == 'r') 1458af245d11STodd Fiala memory_region_info.SetReadable(MemoryRegionInfo::OptionalBool::eYes); 1459c73301bbSTamas Berghammer else if (read_perm_char == '-') 1460af245d11STodd Fiala memory_region_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 1461c73301bbSTamas Berghammer else 1462c73301bbSTamas Berghammer return Error("unexpected /proc/{pid}/maps read permission char"); 1463af245d11STodd Fiala 1464af245d11STodd Fiala // Handle write permission. 1465af245d11STodd Fiala const char write_perm_char = line_extractor.GetChar(); 1466af245d11STodd Fiala if (write_perm_char == 'w') 1467af245d11STodd Fiala memory_region_info.SetWritable(MemoryRegionInfo::OptionalBool::eYes); 1468c73301bbSTamas Berghammer else if (write_perm_char == '-') 1469af245d11STodd Fiala memory_region_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 1470c73301bbSTamas Berghammer else 1471c73301bbSTamas Berghammer return Error("unexpected /proc/{pid}/maps write permission char"); 1472af245d11STodd Fiala 1473af245d11STodd Fiala // Handle execute permission. 1474af245d11STodd Fiala const char exec_perm_char = line_extractor.GetChar(); 1475af245d11STodd Fiala if (exec_perm_char == 'x') 1476af245d11STodd Fiala memory_region_info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes); 1477c73301bbSTamas Berghammer else if (exec_perm_char == '-') 1478af245d11STodd Fiala memory_region_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1479c73301bbSTamas Berghammer else 1480c73301bbSTamas Berghammer return Error("unexpected /proc/{pid}/maps exec permission char"); 1481af245d11STodd Fiala 1482d7d69f80STamas Berghammer line_extractor.GetChar(); // Read the private bit 1483d7d69f80STamas Berghammer line_extractor.SkipSpaces(); // Skip the separator 1484d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the offset 1485d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the major device number 1486d7d69f80STamas Berghammer line_extractor.GetChar(); // Read the device id separator 1487d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the major device number 1488d7d69f80STamas Berghammer line_extractor.SkipSpaces(); // Skip the separator 1489d7d69f80STamas Berghammer line_extractor.GetU64(0, 10); // Read the inode number 1490d7d69f80STamas Berghammer 1491d7d69f80STamas Berghammer line_extractor.SkipSpaces(); 1492b9739d40SPavel Labath const char *name = line_extractor.Peek(); 1493b9739d40SPavel Labath if (name) 1494b9739d40SPavel Labath memory_region_info.SetName(name); 1495d7d69f80STamas Berghammer 1496665be50eSMehdi Amini return Error(); 1497af245d11STodd Fiala } 1498af245d11STodd Fiala 1499b9c1b51eSKate Stone Error NativeProcessLinux::GetMemoryRegionInfo(lldb::addr_t load_addr, 1500b9c1b51eSKate Stone MemoryRegionInfo &range_info) { 1501b9c1b51eSKate Stone // FIXME review that the final memory region returned extends to the end of 1502b9c1b51eSKate Stone // the virtual address space, 1503af245d11STodd Fiala // with no perms if it is not mapped. 1504af245d11STodd Fiala 1505af245d11STodd Fiala // Use an approach that reads memory regions from /proc/{pid}/maps. 1506af245d11STodd Fiala // Assume proc maps entries are in ascending order. 1507af245d11STodd Fiala // FIXME assert if we find differently. 1508af245d11STodd Fiala 1509b9c1b51eSKate Stone if (m_supports_mem_region == LazyBool::eLazyBoolNo) { 1510af245d11STodd Fiala // We're done. 1511a6f5795aSTamas Berghammer return Error("unsupported"); 1512af245d11STodd Fiala } 1513af245d11STodd Fiala 1514a6f5795aSTamas Berghammer Error error = PopulateMemoryRegionCache(); 1515b9c1b51eSKate Stone if (error.Fail()) { 1516af245d11STodd Fiala return error; 1517af245d11STodd Fiala } 1518af245d11STodd Fiala 1519af245d11STodd Fiala lldb::addr_t prev_base_address = 0; 1520af245d11STodd Fiala 1521b9c1b51eSKate Stone // FIXME start by finding the last region that is <= target address using 1522b9c1b51eSKate Stone // binary search. Data is sorted. 1523af245d11STodd Fiala // There can be a ton of regions on pthreads apps with lots of threads. 1524b9c1b51eSKate Stone for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end(); 1525b9c1b51eSKate Stone ++it) { 1526a6f5795aSTamas Berghammer MemoryRegionInfo &proc_entry_info = it->first; 1527af245d11STodd Fiala 1528af245d11STodd Fiala // Sanity check assumption that /proc/{pid}/maps entries are ascending. 1529b9c1b51eSKate Stone assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && 1530b9c1b51eSKate Stone "descending /proc/pid/maps entries detected, unexpected"); 1531af245d11STodd Fiala prev_base_address = proc_entry_info.GetRange().GetRangeBase(); 1532b1554311SHafiz Abid Qadeer UNUSED_IF_ASSERT_DISABLED(prev_base_address); 1533af245d11STodd Fiala 1534b9c1b51eSKate Stone // If the target address comes before this entry, indicate distance to next 1535b9c1b51eSKate Stone // region. 1536b9c1b51eSKate Stone if (load_addr < proc_entry_info.GetRange().GetRangeBase()) { 1537af245d11STodd Fiala range_info.GetRange().SetRangeBase(load_addr); 1538b9c1b51eSKate Stone range_info.GetRange().SetByteSize( 1539b9c1b51eSKate Stone proc_entry_info.GetRange().GetRangeBase() - load_addr); 1540af245d11STodd Fiala range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 1541af245d11STodd Fiala range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 1542af245d11STodd Fiala range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1543ad007563SHoward Hellyer range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 1544af245d11STodd Fiala 1545af245d11STodd Fiala return error; 1546b9c1b51eSKate Stone } else if (proc_entry_info.GetRange().Contains(load_addr)) { 1547af245d11STodd Fiala // The target address is within the memory region we're processing here. 1548af245d11STodd Fiala range_info = proc_entry_info; 1549af245d11STodd Fiala return error; 1550af245d11STodd Fiala } 1551af245d11STodd Fiala 1552b9c1b51eSKate Stone // The target memory address comes somewhere after the region we just 1553b9c1b51eSKate Stone // parsed. 1554af245d11STodd Fiala } 1555af245d11STodd Fiala 1556b9c1b51eSKate Stone // If we made it here, we didn't find an entry that contained the given 1557b9c1b51eSKate Stone // address. Return the 1558b9c1b51eSKate Stone // load_addr as start and the amount of bytes betwwen load address and the end 1559b9c1b51eSKate Stone // of the memory as 156009839c33STamas Berghammer // size. 156109839c33STamas Berghammer range_info.GetRange().SetRangeBase(load_addr); 1562ad007563SHoward Hellyer range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); 156309839c33STamas Berghammer range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 156409839c33STamas Berghammer range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 156509839c33STamas Berghammer range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1566ad007563SHoward Hellyer range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 1567af245d11STodd Fiala return error; 1568af245d11STodd Fiala } 1569af245d11STodd Fiala 1570a6f5795aSTamas Berghammer Error NativeProcessLinux::PopulateMemoryRegionCache() { 1571a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1572a6f5795aSTamas Berghammer 1573a6f5795aSTamas Berghammer // If our cache is empty, pull the latest. There should always be at least 1574a6f5795aSTamas Berghammer // one memory region if memory region handling is supported. 1575a6f5795aSTamas Berghammer if (!m_mem_region_cache.empty()) { 1576a6321a8eSPavel Labath LLDB_LOG(log, "reusing {0} cached memory region entries", 1577a6321a8eSPavel Labath m_mem_region_cache.size()); 1578a6f5795aSTamas Berghammer return Error(); 1579a6f5795aSTamas Berghammer } 1580a6f5795aSTamas Berghammer 1581a6f5795aSTamas Berghammer Error error = ProcFileReader::ProcessLineByLine( 1582a6f5795aSTamas Berghammer GetID(), "maps", [&](const std::string &line) -> bool { 1583a6f5795aSTamas Berghammer MemoryRegionInfo info; 1584a6f5795aSTamas Berghammer const Error parse_error = 1585a6f5795aSTamas Berghammer ParseMemoryRegionInfoFromProcMapsLine(line, info); 1586a6f5795aSTamas Berghammer if (parse_error.Success()) { 1587a6f5795aSTamas Berghammer m_mem_region_cache.emplace_back( 1588a6f5795aSTamas Berghammer info, FileSpec(info.GetName().GetCString(), true)); 1589a6f5795aSTamas Berghammer return true; 1590a6f5795aSTamas Berghammer } else { 1591a6321a8eSPavel Labath LLDB_LOG(log, "failed to parse proc maps line '{0}': {1}", line, 1592a6321a8eSPavel Labath parse_error); 1593a6f5795aSTamas Berghammer return false; 1594a6f5795aSTamas Berghammer } 1595a6f5795aSTamas Berghammer }); 1596a6f5795aSTamas Berghammer 1597a6f5795aSTamas Berghammer // If we had an error, we'll mark unsupported. 1598a6f5795aSTamas Berghammer if (error.Fail()) { 1599a6f5795aSTamas Berghammer m_supports_mem_region = LazyBool::eLazyBoolNo; 1600a6f5795aSTamas Berghammer return error; 1601a6f5795aSTamas Berghammer } else if (m_mem_region_cache.empty()) { 1602a6f5795aSTamas Berghammer // No entries after attempting to read them. This shouldn't happen if 1603a6f5795aSTamas Berghammer // /proc/{pid}/maps is supported. Assume we don't support map entries 1604a6f5795aSTamas Berghammer // via procfs. 1605a6321a8eSPavel Labath LLDB_LOG(log, 1606a6321a8eSPavel Labath "failed to find any procfs maps entries, assuming no support " 1607a6321a8eSPavel Labath "for memory region metadata retrieval"); 1608a6f5795aSTamas Berghammer m_supports_mem_region = LazyBool::eLazyBoolNo; 1609a6f5795aSTamas Berghammer error.SetErrorString("not supported"); 1610a6f5795aSTamas Berghammer return error; 1611a6f5795aSTamas Berghammer } 1612a6f5795aSTamas Berghammer 1613a6321a8eSPavel Labath LLDB_LOG(log, "read {0} memory region entries from /proc/{1}/maps", 1614a6321a8eSPavel Labath m_mem_region_cache.size(), GetID()); 1615a6f5795aSTamas Berghammer 1616a6f5795aSTamas Berghammer // We support memory retrieval, remember that. 1617a6f5795aSTamas Berghammer m_supports_mem_region = LazyBool::eLazyBoolYes; 1618a6f5795aSTamas Berghammer return Error(); 1619a6f5795aSTamas Berghammer } 1620a6f5795aSTamas Berghammer 1621b9c1b51eSKate Stone void NativeProcessLinux::DoStopIDBumped(uint32_t newBumpId) { 1622a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1623a6321a8eSPavel Labath LLDB_LOG(log, "newBumpId={0}", newBumpId); 1624a6321a8eSPavel Labath LLDB_LOG(log, "clearing {0} entries from memory region cache", 1625a6321a8eSPavel Labath m_mem_region_cache.size()); 1626af245d11STodd Fiala m_mem_region_cache.clear(); 1627af245d11STodd Fiala } 1628af245d11STodd Fiala 1629b9c1b51eSKate Stone Error NativeProcessLinux::AllocateMemory(size_t size, uint32_t permissions, 1630b9c1b51eSKate Stone lldb::addr_t &addr) { 1631af245d11STodd Fiala // FIXME implementing this requires the equivalent of 1632af245d11STodd Fiala // InferiorCallPOSIX::InferiorCallMmap, which depends on 1633af245d11STodd Fiala // functional ThreadPlans working with Native*Protocol. 1634af245d11STodd Fiala #if 1 1635af245d11STodd Fiala return Error("not implemented yet"); 1636af245d11STodd Fiala #else 1637af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 1638af245d11STodd Fiala 1639af245d11STodd Fiala unsigned prot = 0; 1640af245d11STodd Fiala if (permissions & lldb::ePermissionsReadable) 1641af245d11STodd Fiala prot |= eMmapProtRead; 1642af245d11STodd Fiala if (permissions & lldb::ePermissionsWritable) 1643af245d11STodd Fiala prot |= eMmapProtWrite; 1644af245d11STodd Fiala if (permissions & lldb::ePermissionsExecutable) 1645af245d11STodd Fiala prot |= eMmapProtExec; 1646af245d11STodd Fiala 1647af245d11STodd Fiala // TODO implement this directly in NativeProcessLinux 1648af245d11STodd Fiala // (and lift to NativeProcessPOSIX if/when that class is 1649af245d11STodd Fiala // refactored out). 1650af245d11STodd Fiala if (InferiorCallMmap(this, addr, 0, size, prot, 1651af245d11STodd Fiala eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { 1652af245d11STodd Fiala m_addr_to_mmap_size[addr] = size; 1653665be50eSMehdi Amini return Error(); 1654af245d11STodd Fiala } else { 1655af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 1656b9c1b51eSKate Stone return Error("unable to allocate %" PRIu64 1657b9c1b51eSKate Stone " bytes of memory with permissions %s", 1658b9c1b51eSKate Stone size, GetPermissionsAsCString(permissions)); 1659af245d11STodd Fiala } 1660af245d11STodd Fiala #endif 1661af245d11STodd Fiala } 1662af245d11STodd Fiala 1663b9c1b51eSKate Stone Error NativeProcessLinux::DeallocateMemory(lldb::addr_t addr) { 1664af245d11STodd Fiala // FIXME see comments in AllocateMemory - required lower-level 1665af245d11STodd Fiala // bits not in place yet (ThreadPlans) 1666af245d11STodd Fiala return Error("not implemented"); 1667af245d11STodd Fiala } 1668af245d11STodd Fiala 1669b9c1b51eSKate Stone lldb::addr_t NativeProcessLinux::GetSharedLibraryInfoAddress() { 1670af245d11STodd Fiala // punt on this for now 1671af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 1672af245d11STodd Fiala } 1673af245d11STodd Fiala 1674b9c1b51eSKate Stone size_t NativeProcessLinux::UpdateThreads() { 1675af245d11STodd Fiala // The NativeProcessLinux monitoring threads are always up to date 1676af245d11STodd Fiala // with respect to thread state and they keep the thread list 1677af245d11STodd Fiala // populated properly. All this method needs to do is return the 1678af245d11STodd Fiala // thread count. 1679af245d11STodd Fiala return m_threads.size(); 1680af245d11STodd Fiala } 1681af245d11STodd Fiala 1682b9c1b51eSKate Stone bool NativeProcessLinux::GetArchitecture(ArchSpec &arch) const { 1683af245d11STodd Fiala arch = m_arch; 1684af245d11STodd Fiala return true; 1685af245d11STodd Fiala } 1686af245d11STodd Fiala 1687b9c1b51eSKate Stone Error NativeProcessLinux::GetSoftwareBreakpointPCOffset( 1688b9c1b51eSKate Stone uint32_t &actual_opcode_size) { 1689af245d11STodd Fiala // FIXME put this behind a breakpoint protocol class that can be 1690af245d11STodd Fiala // set per architecture. Need ARM, MIPS support here. 1691af245d11STodd Fiala static const uint8_t g_i386_opcode[] = {0xCC}; 1692bb00d0b6SUlrich Weigand static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; 1693af245d11STodd Fiala 1694b9c1b51eSKate Stone switch (m_arch.GetMachine()) { 1695af245d11STodd Fiala case llvm::Triple::x86: 1696af245d11STodd Fiala case llvm::Triple::x86_64: 1697af245d11STodd Fiala actual_opcode_size = static_cast<uint32_t>(sizeof(g_i386_opcode)); 1698665be50eSMehdi Amini return Error(); 1699af245d11STodd Fiala 1700bb00d0b6SUlrich Weigand case llvm::Triple::systemz: 1701bb00d0b6SUlrich Weigand actual_opcode_size = static_cast<uint32_t>(sizeof(g_s390x_opcode)); 1702665be50eSMehdi Amini return Error(); 1703bb00d0b6SUlrich Weigand 1704ff7fd900STamas Berghammer case llvm::Triple::arm: 1705ff7fd900STamas Berghammer case llvm::Triple::aarch64: 1706e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64: 1707e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64el: 1708ce815e45SSagar Thakur case llvm::Triple::mips: 1709ce815e45SSagar Thakur case llvm::Triple::mipsel: 1710ff7fd900STamas Berghammer // On these architectures the PC don't get updated for breakpoint hits 1711c60c9452SJaydeep Patil actual_opcode_size = 0; 1712665be50eSMehdi Amini return Error(); 1713e8659b5dSMohit K. Bhakkad 1714af245d11STodd Fiala default: 1715af245d11STodd Fiala assert(false && "CPU type not supported!"); 1716af245d11STodd Fiala return Error("CPU type not supported"); 1717af245d11STodd Fiala } 1718af245d11STodd Fiala } 1719af245d11STodd Fiala 1720b9c1b51eSKate Stone Error NativeProcessLinux::SetBreakpoint(lldb::addr_t addr, uint32_t size, 1721b9c1b51eSKate Stone bool hardware) { 1722af245d11STodd Fiala if (hardware) 1723af245d11STodd Fiala return Error("NativeProcessLinux does not support hardware breakpoints"); 1724af245d11STodd Fiala else 1725af245d11STodd Fiala return SetSoftwareBreakpoint(addr, size); 1726af245d11STodd Fiala } 1727af245d11STodd Fiala 1728b9c1b51eSKate Stone Error NativeProcessLinux::GetSoftwareBreakpointTrapOpcode( 1729b9c1b51eSKate Stone size_t trap_opcode_size_hint, size_t &actual_opcode_size, 1730b9c1b51eSKate Stone const uint8_t *&trap_opcode_bytes) { 173163c8be95STamas Berghammer // FIXME put this behind a breakpoint protocol class that can be set per 173263c8be95STamas Berghammer // architecture. Need MIPS support here. 17332afc5966STodd Fiala static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4}; 1734be379e15STamas Berghammer // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the 1735be379e15STamas Berghammer // linux kernel does otherwise. 1736be379e15STamas Berghammer static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7}; 1737af245d11STodd Fiala static const uint8_t g_i386_opcode[] = {0xCC}; 17383df471c3SMohit K. Bhakkad static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d}; 17392c2acf96SMohit K. Bhakkad static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00}; 1740bb00d0b6SUlrich Weigand static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; 1741be379e15STamas Berghammer static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde}; 1742af245d11STodd Fiala 1743b9c1b51eSKate Stone switch (m_arch.GetMachine()) { 17442afc5966STodd Fiala case llvm::Triple::aarch64: 17452afc5966STodd Fiala trap_opcode_bytes = g_aarch64_opcode; 17462afc5966STodd Fiala actual_opcode_size = sizeof(g_aarch64_opcode); 1747665be50eSMehdi Amini return Error(); 17482afc5966STodd Fiala 174963c8be95STamas Berghammer case llvm::Triple::arm: 1750b9c1b51eSKate Stone switch (trap_opcode_size_hint) { 175163c8be95STamas Berghammer case 2: 175263c8be95STamas Berghammer trap_opcode_bytes = g_thumb_breakpoint_opcode; 175363c8be95STamas Berghammer actual_opcode_size = sizeof(g_thumb_breakpoint_opcode); 1754665be50eSMehdi Amini return Error(); 175563c8be95STamas Berghammer case 4: 175663c8be95STamas Berghammer trap_opcode_bytes = g_arm_breakpoint_opcode; 175763c8be95STamas Berghammer actual_opcode_size = sizeof(g_arm_breakpoint_opcode); 1758665be50eSMehdi Amini return Error(); 175963c8be95STamas Berghammer default: 176063c8be95STamas Berghammer assert(false && "Unrecognised trap opcode size hint!"); 176163c8be95STamas Berghammer return Error("Unrecognised trap opcode size hint!"); 176263c8be95STamas Berghammer } 176363c8be95STamas Berghammer 1764af245d11STodd Fiala case llvm::Triple::x86: 1765af245d11STodd Fiala case llvm::Triple::x86_64: 1766af245d11STodd Fiala trap_opcode_bytes = g_i386_opcode; 1767af245d11STodd Fiala actual_opcode_size = sizeof(g_i386_opcode); 1768665be50eSMehdi Amini return Error(); 1769af245d11STodd Fiala 1770ce815e45SSagar Thakur case llvm::Triple::mips: 17713df471c3SMohit K. Bhakkad case llvm::Triple::mips64: 17723df471c3SMohit K. Bhakkad trap_opcode_bytes = g_mips64_opcode; 17733df471c3SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64_opcode); 1774665be50eSMehdi Amini return Error(); 17753df471c3SMohit K. Bhakkad 1776ce815e45SSagar Thakur case llvm::Triple::mipsel: 17772c2acf96SMohit K. Bhakkad case llvm::Triple::mips64el: 17782c2acf96SMohit K. Bhakkad trap_opcode_bytes = g_mips64el_opcode; 17792c2acf96SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64el_opcode); 1780665be50eSMehdi Amini return Error(); 17812c2acf96SMohit K. Bhakkad 1782bb00d0b6SUlrich Weigand case llvm::Triple::systemz: 1783bb00d0b6SUlrich Weigand trap_opcode_bytes = g_s390x_opcode; 1784bb00d0b6SUlrich Weigand actual_opcode_size = sizeof(g_s390x_opcode); 1785665be50eSMehdi Amini return Error(); 1786bb00d0b6SUlrich Weigand 1787af245d11STodd Fiala default: 1788af245d11STodd Fiala assert(false && "CPU type not supported!"); 1789af245d11STodd Fiala return Error("CPU type not supported"); 1790af245d11STodd Fiala } 1791af245d11STodd Fiala } 1792af245d11STodd Fiala 1793af245d11STodd Fiala #if 0 1794af245d11STodd Fiala ProcessMessage::CrashReason 1795af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info) 1796af245d11STodd Fiala { 1797af245d11STodd Fiala ProcessMessage::CrashReason reason; 1798af245d11STodd Fiala assert(info->si_signo == SIGSEGV); 1799af245d11STodd Fiala 1800af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1801af245d11STodd Fiala 1802af245d11STodd Fiala switch (info->si_code) 1803af245d11STodd Fiala { 1804af245d11STodd Fiala default: 1805af245d11STodd Fiala assert(false && "unexpected si_code for SIGSEGV"); 1806af245d11STodd Fiala break; 1807af245d11STodd Fiala case SI_KERNEL: 1808af245d11STodd Fiala // Linux will occasionally send spurious SI_KERNEL codes. 1809af245d11STodd Fiala // (this is poorly documented in sigaction) 1810af245d11STodd Fiala // One way to get this is via unaligned SIMD loads. 1811af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; // for lack of anything better 1812af245d11STodd Fiala break; 1813af245d11STodd Fiala case SEGV_MAPERR: 1814af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; 1815af245d11STodd Fiala break; 1816af245d11STodd Fiala case SEGV_ACCERR: 1817af245d11STodd Fiala reason = ProcessMessage::ePrivilegedAddress; 1818af245d11STodd Fiala break; 1819af245d11STodd Fiala } 1820af245d11STodd Fiala 1821af245d11STodd Fiala return reason; 1822af245d11STodd Fiala } 1823af245d11STodd Fiala #endif 1824af245d11STodd Fiala 1825af245d11STodd Fiala #if 0 1826af245d11STodd Fiala ProcessMessage::CrashReason 1827af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info) 1828af245d11STodd Fiala { 1829af245d11STodd Fiala ProcessMessage::CrashReason reason; 1830af245d11STodd Fiala assert(info->si_signo == SIGILL); 1831af245d11STodd Fiala 1832af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1833af245d11STodd Fiala 1834af245d11STodd Fiala switch (info->si_code) 1835af245d11STodd Fiala { 1836af245d11STodd Fiala default: 1837af245d11STodd Fiala assert(false && "unexpected si_code for SIGILL"); 1838af245d11STodd Fiala break; 1839af245d11STodd Fiala case ILL_ILLOPC: 1840af245d11STodd Fiala reason = ProcessMessage::eIllegalOpcode; 1841af245d11STodd Fiala break; 1842af245d11STodd Fiala case ILL_ILLOPN: 1843af245d11STodd Fiala reason = ProcessMessage::eIllegalOperand; 1844af245d11STodd Fiala break; 1845af245d11STodd Fiala case ILL_ILLADR: 1846af245d11STodd Fiala reason = ProcessMessage::eIllegalAddressingMode; 1847af245d11STodd Fiala break; 1848af245d11STodd Fiala case ILL_ILLTRP: 1849af245d11STodd Fiala reason = ProcessMessage::eIllegalTrap; 1850af245d11STodd Fiala break; 1851af245d11STodd Fiala case ILL_PRVOPC: 1852af245d11STodd Fiala reason = ProcessMessage::ePrivilegedOpcode; 1853af245d11STodd Fiala break; 1854af245d11STodd Fiala case ILL_PRVREG: 1855af245d11STodd Fiala reason = ProcessMessage::ePrivilegedRegister; 1856af245d11STodd Fiala break; 1857af245d11STodd Fiala case ILL_COPROC: 1858af245d11STodd Fiala reason = ProcessMessage::eCoprocessorError; 1859af245d11STodd Fiala break; 1860af245d11STodd Fiala case ILL_BADSTK: 1861af245d11STodd Fiala reason = ProcessMessage::eInternalStackError; 1862af245d11STodd Fiala break; 1863af245d11STodd Fiala } 1864af245d11STodd Fiala 1865af245d11STodd Fiala return reason; 1866af245d11STodd Fiala } 1867af245d11STodd Fiala #endif 1868af245d11STodd Fiala 1869af245d11STodd Fiala #if 0 1870af245d11STodd Fiala ProcessMessage::CrashReason 1871af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info) 1872af245d11STodd Fiala { 1873af245d11STodd Fiala ProcessMessage::CrashReason reason; 1874af245d11STodd Fiala assert(info->si_signo == SIGFPE); 1875af245d11STodd Fiala 1876af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1877af245d11STodd Fiala 1878af245d11STodd Fiala switch (info->si_code) 1879af245d11STodd Fiala { 1880af245d11STodd Fiala default: 1881af245d11STodd Fiala assert(false && "unexpected si_code for SIGFPE"); 1882af245d11STodd Fiala break; 1883af245d11STodd Fiala case FPE_INTDIV: 1884af245d11STodd Fiala reason = ProcessMessage::eIntegerDivideByZero; 1885af245d11STodd Fiala break; 1886af245d11STodd Fiala case FPE_INTOVF: 1887af245d11STodd Fiala reason = ProcessMessage::eIntegerOverflow; 1888af245d11STodd Fiala break; 1889af245d11STodd Fiala case FPE_FLTDIV: 1890af245d11STodd Fiala reason = ProcessMessage::eFloatDivideByZero; 1891af245d11STodd Fiala break; 1892af245d11STodd Fiala case FPE_FLTOVF: 1893af245d11STodd Fiala reason = ProcessMessage::eFloatOverflow; 1894af245d11STodd Fiala break; 1895af245d11STodd Fiala case FPE_FLTUND: 1896af245d11STodd Fiala reason = ProcessMessage::eFloatUnderflow; 1897af245d11STodd Fiala break; 1898af245d11STodd Fiala case FPE_FLTRES: 1899af245d11STodd Fiala reason = ProcessMessage::eFloatInexactResult; 1900af245d11STodd Fiala break; 1901af245d11STodd Fiala case FPE_FLTINV: 1902af245d11STodd Fiala reason = ProcessMessage::eFloatInvalidOperation; 1903af245d11STodd Fiala break; 1904af245d11STodd Fiala case FPE_FLTSUB: 1905af245d11STodd Fiala reason = ProcessMessage::eFloatSubscriptRange; 1906af245d11STodd Fiala break; 1907af245d11STodd Fiala } 1908af245d11STodd Fiala 1909af245d11STodd Fiala return reason; 1910af245d11STodd Fiala } 1911af245d11STodd Fiala #endif 1912af245d11STodd Fiala 1913af245d11STodd Fiala #if 0 1914af245d11STodd Fiala ProcessMessage::CrashReason 1915af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info) 1916af245d11STodd Fiala { 1917af245d11STodd Fiala ProcessMessage::CrashReason reason; 1918af245d11STodd Fiala assert(info->si_signo == SIGBUS); 1919af245d11STodd Fiala 1920af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1921af245d11STodd Fiala 1922af245d11STodd Fiala switch (info->si_code) 1923af245d11STodd Fiala { 1924af245d11STodd Fiala default: 1925af245d11STodd Fiala assert(false && "unexpected si_code for SIGBUS"); 1926af245d11STodd Fiala break; 1927af245d11STodd Fiala case BUS_ADRALN: 1928af245d11STodd Fiala reason = ProcessMessage::eIllegalAlignment; 1929af245d11STodd Fiala break; 1930af245d11STodd Fiala case BUS_ADRERR: 1931af245d11STodd Fiala reason = ProcessMessage::eIllegalAddress; 1932af245d11STodd Fiala break; 1933af245d11STodd Fiala case BUS_OBJERR: 1934af245d11STodd Fiala reason = ProcessMessage::eHardwareError; 1935af245d11STodd Fiala break; 1936af245d11STodd Fiala } 1937af245d11STodd Fiala 1938af245d11STodd Fiala return reason; 1939af245d11STodd Fiala } 1940af245d11STodd Fiala #endif 1941af245d11STodd Fiala 1942b9c1b51eSKate Stone Error NativeProcessLinux::ReadMemory(lldb::addr_t addr, void *buf, size_t size, 1943b9c1b51eSKate Stone size_t &bytes_read) { 1944df7c6995SPavel Labath if (ProcessVmReadvSupported()) { 1945b9c1b51eSKate Stone // The process_vm_readv path is about 50 times faster than ptrace api. We 1946b9c1b51eSKate Stone // want to use 1947df7c6995SPavel Labath // this syscall if it is supported. 1948df7c6995SPavel Labath 1949df7c6995SPavel Labath const ::pid_t pid = GetID(); 1950df7c6995SPavel Labath 1951df7c6995SPavel Labath struct iovec local_iov, remote_iov; 1952df7c6995SPavel Labath local_iov.iov_base = buf; 1953df7c6995SPavel Labath local_iov.iov_len = size; 1954df7c6995SPavel Labath remote_iov.iov_base = reinterpret_cast<void *>(addr); 1955df7c6995SPavel Labath remote_iov.iov_len = size; 1956df7c6995SPavel Labath 1957df7c6995SPavel Labath bytes_read = process_vm_readv(pid, &local_iov, 1, &remote_iov, 1, 0); 1958df7c6995SPavel Labath const bool success = bytes_read == size; 1959df7c6995SPavel Labath 1960a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1961a6321a8eSPavel Labath LLDB_LOG(log, 1962a6321a8eSPavel Labath "using process_vm_readv to read {0} bytes from inferior " 1963a6321a8eSPavel Labath "address {1:x}: {2}", 1964a6321a8eSPavel Labath size, addr, success ? "Success" : strerror(errno)); 1965df7c6995SPavel Labath 1966df7c6995SPavel Labath if (success) 1967665be50eSMehdi Amini return Error(); 1968a6321a8eSPavel Labath // else the call failed for some reason, let's retry the read using ptrace 1969b9c1b51eSKate Stone // api. 1970df7c6995SPavel Labath } 1971df7c6995SPavel Labath 197219cbe96aSPavel Labath unsigned char *dst = static_cast<unsigned char *>(buf); 197319cbe96aSPavel Labath size_t remainder; 197419cbe96aSPavel Labath long data; 197519cbe96aSPavel Labath 1976a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 1977a6321a8eSPavel Labath LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 197819cbe96aSPavel Labath 1979b9c1b51eSKate Stone for (bytes_read = 0; bytes_read < size; bytes_read += remainder) { 1980b9c1b51eSKate Stone Error error = NativeProcessLinux::PtraceWrapper( 1981b9c1b51eSKate Stone PTRACE_PEEKDATA, GetID(), (void *)addr, nullptr, 0, &data); 1982a6321a8eSPavel Labath if (error.Fail()) 198319cbe96aSPavel Labath return error; 198419cbe96aSPavel Labath 198519cbe96aSPavel Labath remainder = size - bytes_read; 198619cbe96aSPavel Labath remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; 198719cbe96aSPavel Labath 198819cbe96aSPavel Labath // Copy the data into our buffer 1989f6ef187bSMohit K. Bhakkad memcpy(dst, &data, remainder); 199019cbe96aSPavel Labath 1991a6321a8eSPavel Labath LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data); 199219cbe96aSPavel Labath addr += k_ptrace_word_size; 199319cbe96aSPavel Labath dst += k_ptrace_word_size; 199419cbe96aSPavel Labath } 1995665be50eSMehdi Amini return Error(); 1996af245d11STodd Fiala } 1997af245d11STodd Fiala 1998b9c1b51eSKate Stone Error NativeProcessLinux::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, 1999b9c1b51eSKate Stone size_t size, 2000b9c1b51eSKate Stone size_t &bytes_read) { 20013eb4b458SChaoren Lin Error error = ReadMemory(addr, buf, size, bytes_read); 2002b9c1b51eSKate Stone if (error.Fail()) 2003b9c1b51eSKate Stone return error; 20043eb4b458SChaoren Lin return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); 20053eb4b458SChaoren Lin } 20063eb4b458SChaoren Lin 2007b9c1b51eSKate Stone Error NativeProcessLinux::WriteMemory(lldb::addr_t addr, const void *buf, 2008b9c1b51eSKate Stone size_t size, size_t &bytes_written) { 200919cbe96aSPavel Labath const unsigned char *src = static_cast<const unsigned char *>(buf); 201019cbe96aSPavel Labath size_t remainder; 201119cbe96aSPavel Labath Error error; 201219cbe96aSPavel Labath 2013a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 2014a6321a8eSPavel Labath LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 201519cbe96aSPavel Labath 2016b9c1b51eSKate Stone for (bytes_written = 0; bytes_written < size; bytes_written += remainder) { 201719cbe96aSPavel Labath remainder = size - bytes_written; 201819cbe96aSPavel Labath remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; 201919cbe96aSPavel Labath 2020b9c1b51eSKate Stone if (remainder == k_ptrace_word_size) { 202119cbe96aSPavel Labath unsigned long data = 0; 2022f6ef187bSMohit K. Bhakkad memcpy(&data, src, k_ptrace_word_size); 202319cbe96aSPavel Labath 2024a6321a8eSPavel Labath LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data); 2025b9c1b51eSKate Stone error = NativeProcessLinux::PtraceWrapper(PTRACE_POKEDATA, GetID(), 2026b9c1b51eSKate Stone (void *)addr, (void *)data); 2027a6321a8eSPavel Labath if (error.Fail()) 202819cbe96aSPavel Labath return error; 2029b9c1b51eSKate Stone } else { 203019cbe96aSPavel Labath unsigned char buff[8]; 203119cbe96aSPavel Labath size_t bytes_read; 203219cbe96aSPavel Labath error = ReadMemory(addr, buff, k_ptrace_word_size, bytes_read); 2033a6321a8eSPavel Labath if (error.Fail()) 203419cbe96aSPavel Labath return error; 203519cbe96aSPavel Labath 203619cbe96aSPavel Labath memcpy(buff, src, remainder); 203719cbe96aSPavel Labath 203819cbe96aSPavel Labath size_t bytes_written_rec; 203919cbe96aSPavel Labath error = WriteMemory(addr, buff, k_ptrace_word_size, bytes_written_rec); 2040a6321a8eSPavel Labath if (error.Fail()) 204119cbe96aSPavel Labath return error; 204219cbe96aSPavel Labath 2043a6321a8eSPavel Labath LLDB_LOG(log, "[{0:x}]:{1:x} ({2:x})", addr, *(const unsigned long *)src, 2044b9c1b51eSKate Stone *(unsigned long *)buff); 204519cbe96aSPavel Labath } 204619cbe96aSPavel Labath 204719cbe96aSPavel Labath addr += k_ptrace_word_size; 204819cbe96aSPavel Labath src += k_ptrace_word_size; 204919cbe96aSPavel Labath } 205019cbe96aSPavel Labath return error; 2051af245d11STodd Fiala } 2052af245d11STodd Fiala 2053b9c1b51eSKate Stone Error NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) { 205419cbe96aSPavel Labath return PtraceWrapper(PTRACE_GETSIGINFO, tid, nullptr, siginfo); 2055af245d11STodd Fiala } 2056af245d11STodd Fiala 2057b9c1b51eSKate Stone Error NativeProcessLinux::GetEventMessage(lldb::tid_t tid, 2058b9c1b51eSKate Stone unsigned long *message) { 205919cbe96aSPavel Labath return PtraceWrapper(PTRACE_GETEVENTMSG, tid, nullptr, message); 2060af245d11STodd Fiala } 2061af245d11STodd Fiala 2062b9c1b51eSKate Stone Error NativeProcessLinux::Detach(lldb::tid_t tid) { 206397ccc294SChaoren Lin if (tid == LLDB_INVALID_THREAD_ID) 2064665be50eSMehdi Amini return Error(); 206597ccc294SChaoren Lin 206619cbe96aSPavel Labath return PtraceWrapper(PTRACE_DETACH, tid); 2067af245d11STodd Fiala } 2068af245d11STodd Fiala 2069b9c1b51eSKate Stone bool NativeProcessLinux::HasThreadNoLock(lldb::tid_t thread_id) { 2070b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 2071af245d11STodd Fiala assert(thread_sp && "thread list should not contain NULL threads"); 2072b9c1b51eSKate Stone if (thread_sp->GetID() == thread_id) { 2073af245d11STodd Fiala // We have this thread. 2074af245d11STodd Fiala return true; 2075af245d11STodd Fiala } 2076af245d11STodd Fiala } 2077af245d11STodd Fiala 2078af245d11STodd Fiala // We don't have this thread. 2079af245d11STodd Fiala return false; 2080af245d11STodd Fiala } 2081af245d11STodd Fiala 2082b9c1b51eSKate Stone bool NativeProcessLinux::StopTrackingThread(lldb::tid_t thread_id) { 2083a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2084a6321a8eSPavel Labath LLDB_LOG(log, "tid: {0})", thread_id); 20851dbc6c9cSPavel Labath 20861dbc6c9cSPavel Labath bool found = false; 2087b9c1b51eSKate Stone for (auto it = m_threads.begin(); it != m_threads.end(); ++it) { 2088b9c1b51eSKate Stone if (*it && ((*it)->GetID() == thread_id)) { 2089af245d11STodd Fiala m_threads.erase(it); 20901dbc6c9cSPavel Labath found = true; 20911dbc6c9cSPavel Labath break; 2092af245d11STodd Fiala } 2093af245d11STodd Fiala } 2094af245d11STodd Fiala 20959eb1ecb9SPavel Labath SignalIfAllThreadsStopped(); 20961dbc6c9cSPavel Labath return found; 2097af245d11STodd Fiala } 2098af245d11STodd Fiala 2099b9c1b51eSKate Stone NativeThreadLinuxSP NativeProcessLinux::AddThread(lldb::tid_t thread_id) { 2100a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); 2101a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id); 2102af245d11STodd Fiala 2103b9c1b51eSKate Stone assert(!HasThreadNoLock(thread_id) && 2104b9c1b51eSKate Stone "attempted to add a thread by id that already exists"); 2105af245d11STodd Fiala 2106af245d11STodd Fiala // If this is the first thread, save it as the current thread 2107af245d11STodd Fiala if (m_threads.empty()) 2108af245d11STodd Fiala SetCurrentThreadID(thread_id); 2109af245d11STodd Fiala 2110f9077782SPavel Labath auto thread_sp = std::make_shared<NativeThreadLinux>(this, thread_id); 2111af245d11STodd Fiala m_threads.push_back(thread_sp); 2112af245d11STodd Fiala return thread_sp; 2113af245d11STodd Fiala } 2114af245d11STodd Fiala 2115b9c1b51eSKate Stone Error NativeProcessLinux::FixupBreakpointPCAsNeeded(NativeThreadLinux &thread) { 2116a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS)); 2117af245d11STodd Fiala 2118af245d11STodd Fiala Error error; 2119af245d11STodd Fiala 2120b9c1b51eSKate Stone // Find out the size of a breakpoint (might depend on where we are in the 2121b9c1b51eSKate Stone // code). 2122b9cc0c75SPavel Labath NativeRegisterContextSP context_sp = thread.GetRegisterContext(); 2123b9c1b51eSKate Stone if (!context_sp) { 2124af245d11STodd Fiala error.SetErrorString("cannot get a NativeRegisterContext for the thread"); 2125a6321a8eSPavel Labath LLDB_LOG(log, "failed: {0}", error); 2126af245d11STodd Fiala return error; 2127af245d11STodd Fiala } 2128af245d11STodd Fiala 2129af245d11STodd Fiala uint32_t breakpoint_size = 0; 2130b9cc0c75SPavel Labath error = GetSoftwareBreakpointPCOffset(breakpoint_size); 2131b9c1b51eSKate Stone if (error.Fail()) { 2132a6321a8eSPavel Labath LLDB_LOG(log, "GetBreakpointSize() failed: {0}", error); 2133af245d11STodd Fiala return error; 2134a6321a8eSPavel Labath } else 2135a6321a8eSPavel Labath LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size); 2136af245d11STodd Fiala 2137b9c1b51eSKate Stone // First try probing for a breakpoint at a software breakpoint location: PC - 2138b9c1b51eSKate Stone // breakpoint size. 2139b9c1b51eSKate Stone const lldb::addr_t initial_pc_addr = 2140b9c1b51eSKate Stone context_sp->GetPCfromBreakpointLocation(); 2141af245d11STodd Fiala lldb::addr_t breakpoint_addr = initial_pc_addr; 2142b9c1b51eSKate Stone if (breakpoint_size > 0) { 2143af245d11STodd Fiala // Do not allow breakpoint probe to wrap around. 21443eb4b458SChaoren Lin if (breakpoint_addr >= breakpoint_size) 21453eb4b458SChaoren Lin breakpoint_addr -= breakpoint_size; 2146af245d11STodd Fiala } 2147af245d11STodd Fiala 2148af245d11STodd Fiala // Check if we stopped because of a breakpoint. 2149af245d11STodd Fiala NativeBreakpointSP breakpoint_sp; 2150af245d11STodd Fiala error = m_breakpoint_list.GetBreakpoint(breakpoint_addr, breakpoint_sp); 2151b9c1b51eSKate Stone if (!error.Success() || !breakpoint_sp) { 2152af245d11STodd Fiala // We didn't find one at a software probe location. Nothing to do. 2153a6321a8eSPavel Labath LLDB_LOG(log, 2154a6321a8eSPavel Labath "pid {0} no lldb breakpoint found at current pc with " 2155a6321a8eSPavel Labath "adjustment: {1}", 2156a6321a8eSPavel Labath GetID(), breakpoint_addr); 2157665be50eSMehdi Amini return Error(); 2158af245d11STodd Fiala } 2159af245d11STodd Fiala 2160af245d11STodd Fiala // If the breakpoint is not a software breakpoint, nothing to do. 2161b9c1b51eSKate Stone if (!breakpoint_sp->IsSoftwareBreakpoint()) { 2162a6321a8eSPavel Labath LLDB_LOG( 2163a6321a8eSPavel Labath log, 2164a6321a8eSPavel Labath "pid {0} breakpoint found at {1:x}, not software, nothing to adjust", 2165a6321a8eSPavel Labath GetID(), breakpoint_addr); 2166665be50eSMehdi Amini return Error(); 2167af245d11STodd Fiala } 2168af245d11STodd Fiala 2169af245d11STodd Fiala // 2170af245d11STodd Fiala // We have a software breakpoint and need to adjust the PC. 2171af245d11STodd Fiala // 2172af245d11STodd Fiala 2173af245d11STodd Fiala // Sanity check. 2174b9c1b51eSKate Stone if (breakpoint_size == 0) { 2175af245d11STodd Fiala // Nothing to do! How did we get here? 2176a6321a8eSPavel Labath LLDB_LOG(log, 2177a6321a8eSPavel Labath "pid {0} breakpoint found at {1:x}, it is software, but the " 2178a6321a8eSPavel Labath "size is zero, nothing to do (unexpected)", 2179a6321a8eSPavel Labath GetID(), breakpoint_addr); 2180665be50eSMehdi Amini return Error(); 2181af245d11STodd Fiala } 2182af245d11STodd Fiala 2183af245d11STodd Fiala // Change the program counter. 2184a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(), 2185a6321a8eSPavel Labath thread.GetID(), initial_pc_addr, breakpoint_addr); 2186af245d11STodd Fiala 2187af245d11STodd Fiala error = context_sp->SetPC(breakpoint_addr); 2188b9c1b51eSKate Stone if (error.Fail()) { 2189a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(), 2190a6321a8eSPavel Labath thread.GetID(), error); 2191af245d11STodd Fiala return error; 2192af245d11STodd Fiala } 2193af245d11STodd Fiala 2194af245d11STodd Fiala return error; 2195af245d11STodd Fiala } 2196fa03ad2eSChaoren Lin 2197b9c1b51eSKate Stone Error NativeProcessLinux::GetLoadedModuleFileSpec(const char *module_path, 2198b9c1b51eSKate Stone FileSpec &file_spec) { 2199a6f5795aSTamas Berghammer Error error = PopulateMemoryRegionCache(); 2200a6f5795aSTamas Berghammer if (error.Fail()) 2201a6f5795aSTamas Berghammer return error; 2202a6f5795aSTamas Berghammer 22037cb18bf5STamas Berghammer FileSpec module_file_spec(module_path, true); 22047cb18bf5STamas Berghammer 22057cb18bf5STamas Berghammer file_spec.Clear(); 2206a6f5795aSTamas Berghammer for (const auto &it : m_mem_region_cache) { 2207a6f5795aSTamas Berghammer if (it.second.GetFilename() == module_file_spec.GetFilename()) { 2208a6f5795aSTamas Berghammer file_spec = it.second; 2209a6f5795aSTamas Berghammer return Error(); 2210a6f5795aSTamas Berghammer } 2211a6f5795aSTamas Berghammer } 22127cb18bf5STamas Berghammer return Error("Module file (%s) not found in /proc/%" PRIu64 "/maps file!", 22137cb18bf5STamas Berghammer module_file_spec.GetFilename().AsCString(), GetID()); 22147cb18bf5STamas Berghammer } 2215c076559aSPavel Labath 2216b9c1b51eSKate Stone Error NativeProcessLinux::GetFileLoadAddress(const llvm::StringRef &file_name, 2217b9c1b51eSKate Stone lldb::addr_t &load_addr) { 2218783bfc8cSTamas Berghammer load_addr = LLDB_INVALID_ADDRESS; 2219a6f5795aSTamas Berghammer Error error = PopulateMemoryRegionCache(); 2220a6f5795aSTamas Berghammer if (error.Fail()) 2221783bfc8cSTamas Berghammer return error; 2222a6f5795aSTamas Berghammer 2223a6f5795aSTamas Berghammer FileSpec file(file_name, false); 2224a6f5795aSTamas Berghammer for (const auto &it : m_mem_region_cache) { 2225a6f5795aSTamas Berghammer if (it.second == file) { 2226a6f5795aSTamas Berghammer load_addr = it.first.GetRange().GetRangeBase(); 2227a6f5795aSTamas Berghammer return Error(); 2228a6f5795aSTamas Berghammer } 2229a6f5795aSTamas Berghammer } 2230a6f5795aSTamas Berghammer return Error("No load address found for specified file."); 2231783bfc8cSTamas Berghammer } 2232783bfc8cSTamas Berghammer 2233b9c1b51eSKate Stone NativeThreadLinuxSP NativeProcessLinux::GetThreadByID(lldb::tid_t tid) { 2234b9c1b51eSKate Stone return std::static_pointer_cast<NativeThreadLinux>( 2235b9c1b51eSKate Stone NativeProcessProtocol::GetThreadByID(tid)); 2236f9077782SPavel Labath } 2237f9077782SPavel Labath 2238b9c1b51eSKate Stone Error NativeProcessLinux::ResumeThread(NativeThreadLinux &thread, 2239b9c1b51eSKate Stone lldb::StateType state, int signo) { 2240a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2241a6321a8eSPavel Labath LLDB_LOG(log, "tid: {0}", thread.GetID()); 2242c076559aSPavel Labath 2243c076559aSPavel Labath // Before we do the resume below, first check if we have a pending 2244108c325dSPavel Labath // stop notification that is currently waiting for 22450e1d729bSPavel Labath // all threads to stop. This is potentially a buggy situation since 2246c076559aSPavel Labath // we're ostensibly waiting for threads to stop before we send out the 2247c076559aSPavel Labath // pending notification, and here we are resuming one before we send 2248c076559aSPavel Labath // out the pending stop notification. 2249a6321a8eSPavel Labath if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) { 2250a6321a8eSPavel Labath LLDB_LOG(log, 2251a6321a8eSPavel Labath "about to resume tid {0} per explicit request but we have a " 2252a6321a8eSPavel Labath "pending stop notification (tid {1}) that is actively " 2253a6321a8eSPavel Labath "waiting for this thread to stop. Valid sequence of events?", 2254a6321a8eSPavel Labath thread.GetID(), m_pending_notification_tid); 2255c076559aSPavel Labath } 2256c076559aSPavel Labath 2257c076559aSPavel Labath // Request a resume. We expect this to be synchronous and the system 2258c076559aSPavel Labath // to reflect it is running after this completes. 2259b9c1b51eSKate Stone switch (state) { 2260b9c1b51eSKate Stone case eStateRunning: { 2261605b51b8SPavel Labath const auto resume_result = thread.Resume(signo); 22620e1d729bSPavel Labath if (resume_result.Success()) 22630e1d729bSPavel Labath SetState(eStateRunning, true); 22640e1d729bSPavel Labath return resume_result; 2265c076559aSPavel Labath } 2266b9c1b51eSKate Stone case eStateStepping: { 2267605b51b8SPavel Labath const auto step_result = thread.SingleStep(signo); 22680e1d729bSPavel Labath if (step_result.Success()) 22690e1d729bSPavel Labath SetState(eStateRunning, true); 22700e1d729bSPavel Labath return step_result; 22710e1d729bSPavel Labath } 22720e1d729bSPavel Labath default: 22738198db30SPavel Labath LLDB_LOG(log, "Unhandled state {0}.", state); 22740e1d729bSPavel Labath llvm_unreachable("Unhandled state for resume"); 22750e1d729bSPavel Labath } 2276c076559aSPavel Labath } 2277c076559aSPavel Labath 2278c076559aSPavel Labath //===----------------------------------------------------------------------===// 2279c076559aSPavel Labath 2280b9c1b51eSKate Stone void NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid) { 2281a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2282a6321a8eSPavel Labath LLDB_LOG(log, "about to process event: (triggering_tid: {0})", 2283a6321a8eSPavel Labath triggering_tid); 2284c076559aSPavel Labath 22850e1d729bSPavel Labath m_pending_notification_tid = triggering_tid; 22860e1d729bSPavel Labath 22870e1d729bSPavel Labath // Request a stop for all the thread stops that need to be stopped 22880e1d729bSPavel Labath // and are not already known to be stopped. 2289b9c1b51eSKate Stone for (const auto &thread_sp : m_threads) { 22900e1d729bSPavel Labath if (StateIsRunningState(thread_sp->GetState())) 22910e1d729bSPavel Labath static_pointer_cast<NativeThreadLinux>(thread_sp)->RequestStop(); 22920e1d729bSPavel Labath } 22930e1d729bSPavel Labath 22940e1d729bSPavel Labath SignalIfAllThreadsStopped(); 2295a6321a8eSPavel Labath LLDB_LOG(log, "event processing done"); 2296c076559aSPavel Labath } 2297c076559aSPavel Labath 2298b9c1b51eSKate Stone void NativeProcessLinux::SignalIfAllThreadsStopped() { 22990e1d729bSPavel Labath if (m_pending_notification_tid == LLDB_INVALID_THREAD_ID) 23000e1d729bSPavel Labath return; // No pending notification. Nothing to do. 23010e1d729bSPavel Labath 2302b9c1b51eSKate Stone for (const auto &thread_sp : m_threads) { 23030e1d729bSPavel Labath if (StateIsRunningState(thread_sp->GetState())) 23040e1d729bSPavel Labath return; // Some threads are still running. Don't signal yet. 23050e1d729bSPavel Labath } 23060e1d729bSPavel Labath 23070e1d729bSPavel Labath // We have a pending notification and all threads have stopped. 2308b9c1b51eSKate Stone Log *log( 2309b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 23109eb1ecb9SPavel Labath 2311b9c1b51eSKate Stone // Clear any temporary breakpoints we used to implement software single 2312b9c1b51eSKate Stone // stepping. 2313b9c1b51eSKate Stone for (const auto &thread_info : m_threads_stepping_with_breakpoint) { 23149eb1ecb9SPavel Labath Error error = RemoveBreakpoint(thread_info.second); 23159eb1ecb9SPavel Labath if (error.Fail()) 2316a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0} remove stepping breakpoint: {1}", 2317a6321a8eSPavel Labath thread_info.first, error); 23189eb1ecb9SPavel Labath } 23199eb1ecb9SPavel Labath m_threads_stepping_with_breakpoint.clear(); 23209eb1ecb9SPavel Labath 23219eb1ecb9SPavel Labath // Notify the delegate about the stop 23220e1d729bSPavel Labath SetCurrentThreadID(m_pending_notification_tid); 2323ed89c7feSPavel Labath SetState(StateType::eStateStopped, true); 23240e1d729bSPavel Labath m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 2325c076559aSPavel Labath } 2326c076559aSPavel Labath 2327b9c1b51eSKate Stone void NativeProcessLinux::ThreadWasCreated(NativeThreadLinux &thread) { 2328a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2329a6321a8eSPavel Labath LLDB_LOG(log, "tid: {0}", thread.GetID()); 23301dbc6c9cSPavel Labath 2331b9c1b51eSKate Stone if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID && 2332b9c1b51eSKate Stone StateIsRunningState(thread.GetState())) { 2333b9c1b51eSKate Stone // We will need to wait for this new thread to stop as well before firing 2334b9c1b51eSKate Stone // the 2335c076559aSPavel Labath // notification. 2336f9077782SPavel Labath thread.RequestStop(); 2337c076559aSPavel Labath } 2338c076559aSPavel Labath } 2339068f8a7eSTamas Berghammer 2340b9c1b51eSKate Stone void NativeProcessLinux::SigchldHandler() { 2341a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 234219cbe96aSPavel Labath // Process all pending waitpid notifications. 2343b9c1b51eSKate Stone while (true) { 234419cbe96aSPavel Labath int status = -1; 234519cbe96aSPavel Labath ::pid_t wait_pid = waitpid(-1, &status, __WALL | __WNOTHREAD | WNOHANG); 234619cbe96aSPavel Labath 234719cbe96aSPavel Labath if (wait_pid == 0) 234819cbe96aSPavel Labath break; // We are done. 234919cbe96aSPavel Labath 2350b9c1b51eSKate Stone if (wait_pid == -1) { 235119cbe96aSPavel Labath if (errno == EINTR) 235219cbe96aSPavel Labath continue; 235319cbe96aSPavel Labath 235419cbe96aSPavel Labath Error error(errno, eErrorTypePOSIX); 2355a6321a8eSPavel Labath LLDB_LOG(log, "waitpid (-1, &status, _) failed: {0}", error); 235619cbe96aSPavel Labath break; 235719cbe96aSPavel Labath } 235819cbe96aSPavel Labath 235919cbe96aSPavel Labath bool exited = false; 236019cbe96aSPavel Labath int signal = 0; 236119cbe96aSPavel Labath int exit_status = 0; 236219cbe96aSPavel Labath const char *status_cstr = nullptr; 2363b9c1b51eSKate Stone if (WIFSTOPPED(status)) { 236419cbe96aSPavel Labath signal = WSTOPSIG(status); 236519cbe96aSPavel Labath status_cstr = "STOPPED"; 2366b9c1b51eSKate Stone } else if (WIFEXITED(status)) { 236719cbe96aSPavel Labath exit_status = WEXITSTATUS(status); 236819cbe96aSPavel Labath status_cstr = "EXITED"; 236919cbe96aSPavel Labath exited = true; 2370b9c1b51eSKate Stone } else if (WIFSIGNALED(status)) { 237119cbe96aSPavel Labath signal = WTERMSIG(status); 237219cbe96aSPavel Labath status_cstr = "SIGNALED"; 237319cbe96aSPavel Labath if (wait_pid == static_cast<::pid_t>(GetID())) { 237419cbe96aSPavel Labath exited = true; 237519cbe96aSPavel Labath exit_status = -1; 237619cbe96aSPavel Labath } 2377b9c1b51eSKate Stone } else 237819cbe96aSPavel Labath status_cstr = "(\?\?\?)"; 237919cbe96aSPavel Labath 2380a6321a8eSPavel Labath LLDB_LOG(log, 2381a6321a8eSPavel Labath "waitpid (-1, &status, _) => pid = {0}, status = {1:x} " 2382a6321a8eSPavel Labath "({2}), signal = {3}, exit_state = {4}", 2383a6321a8eSPavel Labath wait_pid, status, status_cstr, signal, exit_status); 238419cbe96aSPavel Labath 238519cbe96aSPavel Labath MonitorCallback(wait_pid, exited, signal, exit_status); 238619cbe96aSPavel Labath } 2387068f8a7eSTamas Berghammer } 2388068f8a7eSTamas Berghammer 2389068f8a7eSTamas Berghammer // Wrapper for ptrace to catch errors and log calls. 2390b9c1b51eSKate Stone // Note that ptrace sets errno on error because -1 can be a valid result (i.e. 2391b9c1b51eSKate Stone // for PTRACE_PEEK*) 2392b9c1b51eSKate Stone Error NativeProcessLinux::PtraceWrapper(int req, lldb::pid_t pid, void *addr, 2393b9c1b51eSKate Stone void *data, size_t data_size, 2394b9c1b51eSKate Stone long *result) { 23954a9babb2SPavel Labath Error error; 23964a9babb2SPavel Labath long int ret; 2397068f8a7eSTamas Berghammer 2398068f8a7eSTamas Berghammer Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 2399068f8a7eSTamas Berghammer 2400068f8a7eSTamas Berghammer PtraceDisplayBytes(req, data, data_size); 2401068f8a7eSTamas Berghammer 2402068f8a7eSTamas Berghammer errno = 0; 2403068f8a7eSTamas Berghammer if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) 2404b9c1b51eSKate Stone ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), 2405b9c1b51eSKate Stone *(unsigned int *)addr, data); 2406068f8a7eSTamas Berghammer else 2407b9c1b51eSKate Stone ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), 2408b9c1b51eSKate Stone addr, data); 2409068f8a7eSTamas Berghammer 24104a9babb2SPavel Labath if (ret == -1) 2411068f8a7eSTamas Berghammer error.SetErrorToErrno(); 2412068f8a7eSTamas Berghammer 24134a9babb2SPavel Labath if (result) 24144a9babb2SPavel Labath *result = ret; 24154a9babb2SPavel Labath 2416a6321a8eSPavel Labath LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3}, {4}, {5})={6:x}", req, pid, addr, 2417b9c1b51eSKate Stone data, data_size, ret); 2418068f8a7eSTamas Berghammer 2419068f8a7eSTamas Berghammer PtraceDisplayBytes(req, data, data_size); 2420068f8a7eSTamas Berghammer 2421a6321a8eSPavel Labath if (error.Fail()) 2422a6321a8eSPavel Labath LLDB_LOG(log, "ptrace() failed: {0}", error); 2423068f8a7eSTamas Berghammer 24244a9babb2SPavel Labath return error; 2425068f8a7eSTamas Berghammer } 2426