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" 27af245d11STodd Fiala #include "lldb/Core/Error.h" 286edef204SOleksiy Vyalov #include "lldb/Core/ModuleSpec.h" 29af245d11STodd Fiala #include "lldb/Core/RegisterValue.h" 30af245d11STodd Fiala #include "lldb/Core/State.h" 31af245d11STodd Fiala #include "lldb/Host/Host.h" 325ad891f7SPavel Labath #include "lldb/Host/HostProcess.h" 3339de3110SZachary Turner #include "lldb/Host/ThreadLauncher.h" 342a86b555SPavel Labath #include "lldb/Host/common/NativeBreakpoint.h" 352a86b555SPavel Labath #include "lldb/Host/common/NativeRegisterContext.h" 365ad891f7SPavel Labath #include "lldb/Host/linux/ProcessLauncherLinux.h" 372a86b555SPavel Labath #include "lldb/Symbol/ObjectFile.h" 3890aff47cSZachary Turner #include "lldb/Target/Process.h" 39af245d11STodd Fiala #include "lldb/Target/ProcessLaunchInfo.h" 405b981ab9SPavel Labath #include "lldb/Target/Target.h" 41c16f5dcaSChaoren Lin #include "lldb/Utility/LLDBAssert.h" 42af245d11STodd Fiala #include "lldb/Utility/PseudoTerminal.h" 43f805e190SPavel Labath #include "lldb/Utility/StringExtractor.h" 44af245d11STodd Fiala 45af245d11STodd Fiala #include "NativeThreadLinux.h" 46b9c1b51eSKate Stone #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" 47af245d11STodd Fiala #include "ProcFileReader.h" 481e209fccSTamas Berghammer #include "Procfs.h" 49cacde7dfSTodd Fiala 50b9c1b51eSKate Stone // System includes - They have to be included after framework includes because 51b9c1b51eSKate Stone // they define some 52d858487eSTamas Berghammer // macros which collide with variable names in other modules 53d858487eSTamas Berghammer #include <linux/unistd.h> 54d858487eSTamas Berghammer #include <sys/socket.h> 558b335671SVince Harron 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 618b335671SVince Harron #include "lldb/Host/linux/Ptrace.h" 62df7c6995SPavel Labath #include "lldb/Host/linux/Uio.h" 63af245d11STodd Fiala 64af245d11STodd Fiala // Support hardware breakpoints in case it has not been defined 65af245d11STodd Fiala #ifndef TRAP_HWBKPT 66af245d11STodd Fiala #define TRAP_HWBKPT 4 67af245d11STodd Fiala #endif 68af245d11STodd Fiala 697cb18bf5STamas Berghammer using namespace lldb; 707cb18bf5STamas Berghammer using namespace lldb_private; 71db264a6dSTamas Berghammer using namespace lldb_private::process_linux; 727cb18bf5STamas Berghammer using namespace llvm; 737cb18bf5STamas Berghammer 74af245d11STodd Fiala // Private bits we only need internally. 75df7c6995SPavel Labath 76b9c1b51eSKate Stone static bool ProcessVmReadvSupported() { 77df7c6995SPavel Labath static bool is_supported; 78df7c6995SPavel Labath static std::once_flag flag; 79df7c6995SPavel Labath 80df7c6995SPavel Labath std::call_once(flag, [] { 81df7c6995SPavel Labath Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 82df7c6995SPavel Labath 83df7c6995SPavel Labath uint32_t source = 0x47424742; 84df7c6995SPavel Labath uint32_t dest = 0; 85df7c6995SPavel Labath 86df7c6995SPavel Labath struct iovec local, remote; 87df7c6995SPavel Labath remote.iov_base = &source; 88df7c6995SPavel Labath local.iov_base = &dest; 89df7c6995SPavel Labath remote.iov_len = local.iov_len = sizeof source; 90df7c6995SPavel Labath 91b9c1b51eSKate Stone // We shall try if cross-process-memory reads work by attempting to read a 92b9c1b51eSKate Stone // value from our own process. 93df7c6995SPavel Labath ssize_t res = process_vm_readv(getpid(), &local, 1, &remote, 1, 0); 94df7c6995SPavel Labath is_supported = (res == sizeof(source) && source == dest); 95b9c1b51eSKate Stone if (log) { 96df7c6995SPavel Labath if (is_supported) 97b9c1b51eSKate Stone log->Printf("%s: Detected kernel support for process_vm_readv syscall. " 98b9c1b51eSKate Stone "Fast memory reads enabled.", 99df7c6995SPavel Labath __FUNCTION__); 100df7c6995SPavel Labath else 101b9c1b51eSKate Stone log->Printf("%s: syscall process_vm_readv failed (error: %s). Fast " 102b9c1b51eSKate Stone "memory reads disabled.", 103df7c6995SPavel Labath __FUNCTION__, strerror(errno)); 104df7c6995SPavel Labath } 105df7c6995SPavel Labath }); 106df7c6995SPavel Labath 107df7c6995SPavel Labath return is_supported; 108df7c6995SPavel Labath } 109df7c6995SPavel Labath 110b9c1b51eSKate Stone namespace { 111b9c1b51eSKate Stone void MaybeLogLaunchInfo(const ProcessLaunchInfo &info) { 1124abe5d69SPavel Labath Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 1134abe5d69SPavel Labath if (!log) 1144abe5d69SPavel Labath return; 1154abe5d69SPavel Labath 1164abe5d69SPavel Labath if (const FileAction *action = info.GetFileActionForFD(STDIN_FILENO)) 117b9c1b51eSKate Stone log->Printf("%s: setting STDIN to '%s'", __FUNCTION__, 118b9c1b51eSKate Stone action->GetFileSpec().GetCString()); 1194abe5d69SPavel Labath else 1204abe5d69SPavel Labath log->Printf("%s leaving STDIN as is", __FUNCTION__); 1214abe5d69SPavel Labath 1224abe5d69SPavel Labath if (const FileAction *action = info.GetFileActionForFD(STDOUT_FILENO)) 123b9c1b51eSKate Stone log->Printf("%s setting STDOUT to '%s'", __FUNCTION__, 124b9c1b51eSKate Stone action->GetFileSpec().GetCString()); 1254abe5d69SPavel Labath else 1264abe5d69SPavel Labath log->Printf("%s leaving STDOUT as is", __FUNCTION__); 1274abe5d69SPavel Labath 1284abe5d69SPavel Labath if (const FileAction *action = info.GetFileActionForFD(STDERR_FILENO)) 129b9c1b51eSKate Stone log->Printf("%s setting STDERR to '%s'", __FUNCTION__, 130b9c1b51eSKate Stone action->GetFileSpec().GetCString()); 1314abe5d69SPavel Labath else 1324abe5d69SPavel Labath log->Printf("%s leaving STDERR as is", __FUNCTION__); 1334abe5d69SPavel Labath 1344abe5d69SPavel Labath int i = 0; 135b9c1b51eSKate Stone for (const char **args = info.GetArguments().GetConstArgumentVector(); *args; 136b9c1b51eSKate Stone ++args, ++i) 137b9c1b51eSKate Stone log->Printf("%s arg %d: \"%s\"", __FUNCTION__, i, 138b9c1b51eSKate Stone *args ? *args : "nullptr"); 1394abe5d69SPavel Labath } 1404abe5d69SPavel Labath 141b9c1b51eSKate Stone void DisplayBytes(StreamString &s, void *bytes, uint32_t count) { 142af245d11STodd Fiala uint8_t *ptr = (uint8_t *)bytes; 143af245d11STodd Fiala const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count); 144b9c1b51eSKate Stone for (uint32_t i = 0; i < loop_count; i++) { 145af245d11STodd Fiala s.Printf("[%x]", *ptr); 146af245d11STodd Fiala ptr++; 147af245d11STodd Fiala } 148af245d11STodd Fiala } 149af245d11STodd Fiala 150b9c1b51eSKate Stone void PtraceDisplayBytes(int &req, void *data, size_t data_size) { 151af245d11STodd Fiala StreamString buf; 152af245d11STodd Fiala Log *verbose_log(ProcessPOSIXLog::GetLogIfAllCategoriesSet( 153af245d11STodd Fiala POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE)); 154af245d11STodd Fiala 155b9c1b51eSKate Stone if (verbose_log) { 156b9c1b51eSKate Stone switch (req) { 157b9c1b51eSKate Stone case PTRACE_POKETEXT: { 158af245d11STodd Fiala DisplayBytes(buf, &data, 8); 159af245d11STodd Fiala verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData()); 160af245d11STodd Fiala break; 161af245d11STodd Fiala } 162b9c1b51eSKate Stone case PTRACE_POKEDATA: { 163af245d11STodd Fiala DisplayBytes(buf, &data, 8); 164af245d11STodd Fiala verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData()); 165af245d11STodd Fiala break; 166af245d11STodd Fiala } 167b9c1b51eSKate Stone case PTRACE_POKEUSER: { 168af245d11STodd Fiala DisplayBytes(buf, &data, 8); 169af245d11STodd Fiala verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData()); 170af245d11STodd Fiala break; 171af245d11STodd Fiala } 172b9c1b51eSKate Stone case PTRACE_SETREGS: { 173af245d11STodd Fiala DisplayBytes(buf, data, data_size); 174af245d11STodd Fiala verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData()); 175af245d11STodd Fiala break; 176af245d11STodd Fiala } 177b9c1b51eSKate Stone case PTRACE_SETFPREGS: { 178af245d11STodd Fiala DisplayBytes(buf, data, data_size); 179af245d11STodd Fiala verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData()); 180af245d11STodd Fiala break; 181af245d11STodd Fiala } 182b9c1b51eSKate Stone case PTRACE_SETSIGINFO: { 183af245d11STodd Fiala DisplayBytes(buf, data, sizeof(siginfo_t)); 184af245d11STodd Fiala verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData()); 185af245d11STodd Fiala break; 186af245d11STodd Fiala } 187b9c1b51eSKate Stone case PTRACE_SETREGSET: { 188af245d11STodd Fiala // Extract iov_base from data, which is a pointer to the struct IOVEC 189af245d11STodd Fiala DisplayBytes(buf, *(void **)data, data_size); 190af245d11STodd Fiala verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData()); 191af245d11STodd Fiala break; 192af245d11STodd Fiala } 193b9c1b51eSKate Stone default: {} 194af245d11STodd Fiala } 195af245d11STodd Fiala } 196af245d11STodd Fiala } 197af245d11STodd Fiala 19819cbe96aSPavel Labath static constexpr unsigned k_ptrace_word_size = sizeof(void *); 199b9c1b51eSKate Stone static_assert(sizeof(long) >= k_ptrace_word_size, 200b9c1b51eSKate Stone "Size of long must be larger than ptrace word size"); 2011107b5a5SPavel Labath } // end of anonymous namespace 2021107b5a5SPavel Labath 203bd7cbc5aSPavel Labath // Simple helper function to ensure flags are enabled on the given file 204bd7cbc5aSPavel Labath // descriptor. 205b9c1b51eSKate Stone static Error EnsureFDFlags(int fd, int flags) { 206bd7cbc5aSPavel Labath Error error; 207bd7cbc5aSPavel Labath 208bd7cbc5aSPavel Labath int status = fcntl(fd, F_GETFL); 209b9c1b51eSKate Stone if (status == -1) { 210bd7cbc5aSPavel Labath error.SetErrorToErrno(); 211bd7cbc5aSPavel Labath return error; 212bd7cbc5aSPavel Labath } 213bd7cbc5aSPavel Labath 214b9c1b51eSKate Stone if (fcntl(fd, F_SETFL, status | flags) == -1) { 215bd7cbc5aSPavel Labath error.SetErrorToErrno(); 216bd7cbc5aSPavel Labath return error; 217bd7cbc5aSPavel Labath } 218bd7cbc5aSPavel Labath 219bd7cbc5aSPavel Labath return error; 220bd7cbc5aSPavel Labath } 221bd7cbc5aSPavel Labath 222af245d11STodd Fiala // ----------------------------------------------------------------------------- 223af245d11STodd Fiala // Public Static Methods 224af245d11STodd Fiala // ----------------------------------------------------------------------------- 225af245d11STodd Fiala 226b9c1b51eSKate Stone Error NativeProcessProtocol::Launch( 227db264a6dSTamas Berghammer ProcessLaunchInfo &launch_info, 228b9c1b51eSKate Stone NativeProcessProtocol::NativeDelegate &native_delegate, MainLoop &mainloop, 229b9c1b51eSKate Stone NativeProcessProtocolSP &native_process_sp) { 230af245d11STodd Fiala Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 231af245d11STodd Fiala 2322a86b555SPavel Labath Error error; 233af245d11STodd Fiala 234af245d11STodd Fiala // Verify the working directory is valid if one was specified. 235d3173f34SChaoren Lin FileSpec working_dir{launch_info.GetWorkingDirectory()}; 236d3173f34SChaoren Lin if (working_dir && 237d3173f34SChaoren Lin (!working_dir.ResolvePath() || 238b9c1b51eSKate Stone working_dir.GetFileType() != FileSpec::eFileTypeDirectory)) { 239d3173f34SChaoren Lin error.SetErrorStringWithFormat("No such file or directory: %s", 240d3173f34SChaoren Lin working_dir.GetCString()); 241af245d11STodd Fiala return error; 242af245d11STodd Fiala } 243af245d11STodd Fiala 244af245d11STodd Fiala // Create the NativeProcessLinux in launch mode. 245af245d11STodd Fiala native_process_sp.reset(new NativeProcessLinux()); 246af245d11STodd Fiala 247b9c1b51eSKate Stone if (!native_process_sp->RegisterNativeDelegate(native_delegate)) { 248af245d11STodd Fiala native_process_sp.reset(); 249af245d11STodd Fiala error.SetErrorStringWithFormat("failed to register the native delegate"); 250af245d11STodd Fiala return error; 251af245d11STodd Fiala } 252af245d11STodd Fiala 253b9c1b51eSKate Stone error = std::static_pointer_cast<NativeProcessLinux>(native_process_sp) 254b9c1b51eSKate Stone ->LaunchInferior(mainloop, launch_info); 255af245d11STodd Fiala 256b9c1b51eSKate Stone if (error.Fail()) { 257af245d11STodd Fiala native_process_sp.reset(); 258af245d11STodd Fiala if (log) 259b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s failed to launch process: %s", 260b9c1b51eSKate Stone __FUNCTION__, error.AsCString()); 261af245d11STodd Fiala return error; 262af245d11STodd Fiala } 263af245d11STodd Fiala 264af245d11STodd Fiala launch_info.SetProcessID(native_process_sp->GetID()); 265af245d11STodd Fiala 266af245d11STodd Fiala return error; 267af245d11STodd Fiala } 268af245d11STodd Fiala 269b9c1b51eSKate Stone Error NativeProcessProtocol::Attach( 270b9c1b51eSKate Stone lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, 271b9c1b51eSKate Stone MainLoop &mainloop, NativeProcessProtocolSP &native_process_sp) { 272af245d11STodd Fiala Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 273af245d11STodd Fiala if (log && log->GetMask().Test(POSIX_LOG_VERBOSE)) 274af245d11STodd Fiala log->Printf("NativeProcessLinux::%s(pid = %" PRIi64 ")", __FUNCTION__, pid); 275af245d11STodd Fiala 276af245d11STodd Fiala // Retrieve the architecture for the running process. 277af245d11STodd Fiala ArchSpec process_arch; 2782a86b555SPavel Labath Error error = ResolveProcessArchitecture(pid, process_arch); 279af245d11STodd Fiala if (!error.Success()) 280af245d11STodd Fiala return error; 281af245d11STodd Fiala 282b9c1b51eSKate Stone std::shared_ptr<NativeProcessLinux> native_process_linux_sp( 283b9c1b51eSKate Stone new NativeProcessLinux()); 284af245d11STodd Fiala 285b9c1b51eSKate Stone if (!native_process_linux_sp->RegisterNativeDelegate(native_delegate)) { 286af245d11STodd Fiala error.SetErrorStringWithFormat("failed to register the native delegate"); 287af245d11STodd Fiala return error; 288af245d11STodd Fiala } 289af245d11STodd Fiala 29019cbe96aSPavel Labath native_process_linux_sp->AttachToInferior(mainloop, pid, error); 291af245d11STodd Fiala if (!error.Success()) 292af245d11STodd Fiala return error; 293af245d11STodd Fiala 2941339b5e8SOleksiy Vyalov native_process_sp = native_process_linux_sp; 295af245d11STodd Fiala return error; 296af245d11STodd Fiala } 297af245d11STodd Fiala 298af245d11STodd Fiala // ----------------------------------------------------------------------------- 299af245d11STodd Fiala // Public Instance Methods 300af245d11STodd Fiala // ----------------------------------------------------------------------------- 301af245d11STodd Fiala 302b9c1b51eSKate Stone NativeProcessLinux::NativeProcessLinux() 303b9c1b51eSKate Stone : NativeProcessProtocol(LLDB_INVALID_PROCESS_ID), m_arch(), 304b9c1b51eSKate Stone m_supports_mem_region(eLazyBoolCalculate), m_mem_region_cache(), 305b9c1b51eSKate Stone m_pending_notification_tid(LLDB_INVALID_THREAD_ID) {} 306af245d11STodd Fiala 307b9c1b51eSKate Stone void NativeProcessLinux::AttachToInferior(MainLoop &mainloop, lldb::pid_t pid, 308b9c1b51eSKate Stone Error &error) { 309af245d11STodd Fiala Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 310af245d11STodd Fiala if (log) 311b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s (pid = %" PRIi64 ")", __FUNCTION__, 312b9c1b51eSKate Stone pid); 313af245d11STodd Fiala 314b9c1b51eSKate Stone m_sigchld_handle = mainloop.RegisterSignal( 315b9c1b51eSKate Stone SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, error); 31619cbe96aSPavel Labath if (!m_sigchld_handle) 31719cbe96aSPavel Labath return; 31819cbe96aSPavel Labath 3192a86b555SPavel Labath error = ResolveProcessArchitecture(pid, m_arch); 320af245d11STodd Fiala if (!error.Success()) 321af245d11STodd Fiala return; 322af245d11STodd Fiala 323af245d11STodd Fiala // Set the architecture to the exe architecture. 324af245d11STodd Fiala if (log) 325b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s (pid = %" PRIi64 326b9c1b51eSKate Stone ") detected architecture %s", 327b9c1b51eSKate Stone __FUNCTION__, pid, m_arch.GetArchitectureName()); 328af245d11STodd Fiala 329af245d11STodd Fiala m_pid = pid; 330af245d11STodd Fiala SetState(eStateAttaching); 331af245d11STodd Fiala 33219cbe96aSPavel Labath Attach(pid, error); 333af245d11STodd Fiala } 334af245d11STodd Fiala 335b9c1b51eSKate Stone Error NativeProcessLinux::LaunchInferior(MainLoop &mainloop, 336b9c1b51eSKate Stone ProcessLaunchInfo &launch_info) { 3374abe5d69SPavel Labath Error error; 338b9c1b51eSKate Stone m_sigchld_handle = mainloop.RegisterSignal( 339b9c1b51eSKate Stone SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, error); 3404abe5d69SPavel Labath if (!m_sigchld_handle) 3414abe5d69SPavel Labath return error; 3424abe5d69SPavel Labath 3434abe5d69SPavel Labath SetState(eStateLaunching); 3440c4f01d4SPavel Labath 3454abe5d69SPavel Labath MaybeLogLaunchInfo(launch_info); 3464abe5d69SPavel Labath 347b9c1b51eSKate Stone ::pid_t pid = 348b9c1b51eSKate Stone ProcessLauncherLinux().LaunchProcess(launch_info, error).GetProcessId(); 3495ad891f7SPavel Labath if (error.Fail()) 3504abe5d69SPavel Labath return error; 3510c4f01d4SPavel Labath 35275f47c3aSTodd Fiala Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 35375f47c3aSTodd Fiala 354af245d11STodd Fiala // Wait for the child process to trap on its call to execve. 355af245d11STodd Fiala ::pid_t wpid; 356af245d11STodd Fiala int status; 357b9c1b51eSKate Stone if ((wpid = waitpid(pid, &status, 0)) < 0) { 358bd7cbc5aSPavel Labath error.SetErrorToErrno(); 359af245d11STodd Fiala if (log) 360bd7cbc5aSPavel Labath log->Printf("NativeProcessLinux::%s waitpid for inferior failed with %s", 361bd7cbc5aSPavel Labath __FUNCTION__, error.AsCString()); 362af245d11STodd Fiala 363af245d11STodd Fiala // Mark the inferior as invalid. 364b9c1b51eSKate Stone // FIXME this could really use a new state - eStateLaunchFailure. For now, 365b9c1b51eSKate Stone // using eStateInvalid. 366bd7cbc5aSPavel Labath SetState(StateType::eStateInvalid); 367af245d11STodd Fiala 3684abe5d69SPavel Labath return error; 369af245d11STodd Fiala } 370af245d11STodd Fiala assert(WIFSTOPPED(status) && (wpid == static_cast<::pid_t>(pid)) && 371af245d11STodd Fiala "Could not sync with inferior process."); 372af245d11STodd Fiala 373af245d11STodd Fiala if (log) 374b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s inferior started, now in stopped state", 375b9c1b51eSKate Stone __FUNCTION__); 376af245d11STodd Fiala 377bd7cbc5aSPavel Labath error = SetDefaultPtraceOpts(pid); 378b9c1b51eSKate Stone if (error.Fail()) { 379af245d11STodd Fiala if (log) 380b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s inferior failed to set default " 381b9c1b51eSKate Stone "ptrace options: %s", 382bd7cbc5aSPavel Labath __FUNCTION__, error.AsCString()); 383af245d11STodd Fiala 384af245d11STodd Fiala // Mark the inferior as invalid. 385b9c1b51eSKate Stone // FIXME this could really use a new state - eStateLaunchFailure. For now, 386b9c1b51eSKate Stone // using eStateInvalid. 387bd7cbc5aSPavel Labath SetState(StateType::eStateInvalid); 388af245d11STodd Fiala 3894abe5d69SPavel Labath return error; 390af245d11STodd Fiala } 391af245d11STodd Fiala 392af245d11STodd Fiala // Release the master terminal descriptor and pass it off to the 393af245d11STodd Fiala // NativeProcessLinux instance. Similarly stash the inferior pid. 3945ad891f7SPavel Labath m_terminal_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor(); 395bd7cbc5aSPavel Labath m_pid = pid; 3964abe5d69SPavel Labath launch_info.SetProcessID(pid); 397af245d11STodd Fiala 398b9c1b51eSKate Stone if (m_terminal_fd != -1) { 399bd7cbc5aSPavel Labath error = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); 400b9c1b51eSKate Stone if (error.Fail()) { 401af245d11STodd Fiala if (log) 402b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s inferior EnsureFDFlags failed for " 403b9c1b51eSKate Stone "ensuring terminal O_NONBLOCK setting: %s", 404bd7cbc5aSPavel Labath __FUNCTION__, error.AsCString()); 405af245d11STodd Fiala 406af245d11STodd Fiala // Mark the inferior as invalid. 407b9c1b51eSKate Stone // FIXME this could really use a new state - eStateLaunchFailure. For 408b9c1b51eSKate Stone // now, using eStateInvalid. 409bd7cbc5aSPavel Labath SetState(StateType::eStateInvalid); 410af245d11STodd Fiala 4114abe5d69SPavel Labath return error; 412af245d11STodd Fiala } 4135ad891f7SPavel Labath } 414af245d11STodd Fiala 415af245d11STodd Fiala if (log) 416b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() adding pid = %" PRIu64, __FUNCTION__, 417b9c1b51eSKate Stone uint64_t(pid)); 418af245d11STodd Fiala 4192a86b555SPavel Labath ResolveProcessArchitecture(m_pid, m_arch); 420f9077782SPavel Labath NativeThreadLinuxSP thread_sp = AddThread(pid); 421af245d11STodd Fiala assert(thread_sp && "AddThread() returned a nullptr thread"); 422f9077782SPavel Labath thread_sp->SetStoppedBySignal(SIGSTOP); 423f9077782SPavel Labath ThreadWasCreated(*thread_sp); 424af245d11STodd Fiala 425af245d11STodd Fiala // Let our process instance know the thread has stopped. 426bd7cbc5aSPavel Labath SetCurrentThreadID(thread_sp->GetID()); 427bd7cbc5aSPavel Labath SetState(StateType::eStateStopped); 428af245d11STodd Fiala 429b9c1b51eSKate Stone if (log) { 430bd7cbc5aSPavel Labath if (error.Success()) 431b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s inferior launching succeeded", 432b9c1b51eSKate Stone __FUNCTION__); 433af245d11STodd Fiala else 434b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s inferior launching failed: %s", 435b9c1b51eSKate Stone __FUNCTION__, error.AsCString()); 436af245d11STodd Fiala } 4374abe5d69SPavel Labath return error; 438af245d11STodd Fiala } 439af245d11STodd Fiala 440b9c1b51eSKate Stone ::pid_t NativeProcessLinux::Attach(lldb::pid_t pid, Error &error) { 441af245d11STodd Fiala Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 442af245d11STodd Fiala 443b9c1b51eSKate Stone // Use a map to keep track of the threads which we have attached/need to 444b9c1b51eSKate Stone // attach. 445af245d11STodd Fiala Host::TidMap tids_to_attach; 446b9c1b51eSKate Stone if (pid <= 1) { 447bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 448bd7cbc5aSPavel Labath error.SetErrorString("Attaching to process 1 is not allowed."); 449bd7cbc5aSPavel Labath return -1; 450af245d11STodd Fiala } 451af245d11STodd Fiala 452b9c1b51eSKate Stone while (Host::FindProcessThreads(pid, tids_to_attach)) { 453af245d11STodd Fiala for (Host::TidMap::iterator it = tids_to_attach.begin(); 454b9c1b51eSKate Stone it != tids_to_attach.end();) { 455b9c1b51eSKate Stone if (it->second == false) { 456af245d11STodd Fiala lldb::tid_t tid = it->first; 457af245d11STodd Fiala 458af245d11STodd Fiala // Attach to the requested process. 459af245d11STodd Fiala // An attach will cause the thread to stop with a SIGSTOP. 4604a9babb2SPavel Labath error = PtraceWrapper(PTRACE_ATTACH, tid); 461b9c1b51eSKate Stone if (error.Fail()) { 462af245d11STodd Fiala // No such thread. The thread may have exited. 463af245d11STodd Fiala // More error handling may be needed. 464b9c1b51eSKate Stone if (error.GetError() == ESRCH) { 465af245d11STodd Fiala it = tids_to_attach.erase(it); 466af245d11STodd Fiala continue; 467b9c1b51eSKate Stone } else 468bd7cbc5aSPavel Labath return -1; 469af245d11STodd Fiala } 470af245d11STodd Fiala 471af245d11STodd Fiala int status; 472af245d11STodd Fiala // Need to use __WALL otherwise we receive an error with errno=ECHLD 473af245d11STodd Fiala // At this point we should have a thread stopped if waitpid succeeds. 474b9c1b51eSKate Stone if ((status = waitpid(tid, NULL, __WALL)) < 0) { 475af245d11STodd Fiala // No such thread. The thread may have exited. 476af245d11STodd Fiala // More error handling may be needed. 477b9c1b51eSKate Stone if (errno == ESRCH) { 478af245d11STodd Fiala it = tids_to_attach.erase(it); 479af245d11STodd Fiala continue; 480b9c1b51eSKate Stone } else { 481bd7cbc5aSPavel Labath error.SetErrorToErrno(); 482bd7cbc5aSPavel Labath return -1; 483af245d11STodd Fiala } 484af245d11STodd Fiala } 485af245d11STodd Fiala 486bd7cbc5aSPavel Labath error = SetDefaultPtraceOpts(tid); 487bd7cbc5aSPavel Labath if (error.Fail()) 488bd7cbc5aSPavel Labath return -1; 489af245d11STodd Fiala 490af245d11STodd Fiala if (log) 491b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() adding tid = %" PRIu64, 492b9c1b51eSKate Stone __FUNCTION__, tid); 493af245d11STodd Fiala 494af245d11STodd Fiala it->second = true; 495af245d11STodd Fiala 496af245d11STodd Fiala // Create the thread, mark it as stopped. 497f9077782SPavel Labath NativeThreadLinuxSP thread_sp(AddThread(static_cast<lldb::tid_t>(tid))); 498af245d11STodd Fiala assert(thread_sp && "AddThread() returned a nullptr"); 499fa03ad2eSChaoren Lin 500b9c1b51eSKate Stone // This will notify this is a new thread and tell the system it is 501b9c1b51eSKate Stone // stopped. 502f9077782SPavel Labath thread_sp->SetStoppedBySignal(SIGSTOP); 503f9077782SPavel Labath ThreadWasCreated(*thread_sp); 504bd7cbc5aSPavel Labath SetCurrentThreadID(thread_sp->GetID()); 505af245d11STodd Fiala } 506af245d11STodd Fiala 507af245d11STodd Fiala // move the loop forward 508af245d11STodd Fiala ++it; 509af245d11STodd Fiala } 510af245d11STodd Fiala } 511af245d11STodd Fiala 512b9c1b51eSKate Stone if (tids_to_attach.size() > 0) { 513bd7cbc5aSPavel Labath m_pid = pid; 514af245d11STodd Fiala // Let our process instance know the thread has stopped. 515bd7cbc5aSPavel Labath SetState(StateType::eStateStopped); 516b9c1b51eSKate Stone } else { 517bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 518bd7cbc5aSPavel Labath error.SetErrorString("No such process."); 519bd7cbc5aSPavel Labath return -1; 520af245d11STodd Fiala } 521af245d11STodd Fiala 522bd7cbc5aSPavel Labath return pid; 523af245d11STodd Fiala } 524af245d11STodd Fiala 525b9c1b51eSKate Stone Error NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) { 526af245d11STodd Fiala long ptrace_opts = 0; 527af245d11STodd Fiala 528af245d11STodd Fiala // Have the child raise an event on exit. This is used to keep the child in 529af245d11STodd Fiala // limbo until it is destroyed. 530af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXIT; 531af245d11STodd Fiala 532af245d11STodd Fiala // Have the tracer trace threads which spawn in the inferior process. 533af245d11STodd Fiala // TODO: if we want to support tracing the inferiors' child, add the 534af245d11STodd Fiala // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK) 535af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACECLONE; 536af245d11STodd Fiala 537af245d11STodd Fiala // Have the tracer notify us before execve returns 538af245d11STodd Fiala // (needed to disable legacy SIGTRAP generation) 539af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXEC; 540af245d11STodd Fiala 5414a9babb2SPavel Labath return PtraceWrapper(PTRACE_SETOPTIONS, pid, nullptr, (void *)ptrace_opts); 542af245d11STodd Fiala } 543af245d11STodd Fiala 544b9c1b51eSKate Stone static ExitType convert_pid_status_to_exit_type(int status) { 545af245d11STodd Fiala if (WIFEXITED(status)) 546af245d11STodd Fiala return ExitType::eExitTypeExit; 547af245d11STodd Fiala else if (WIFSIGNALED(status)) 548af245d11STodd Fiala return ExitType::eExitTypeSignal; 549af245d11STodd Fiala else if (WIFSTOPPED(status)) 550af245d11STodd Fiala return ExitType::eExitTypeStop; 551b9c1b51eSKate Stone else { 552af245d11STodd Fiala // We don't know what this is. 553af245d11STodd Fiala return ExitType::eExitTypeInvalid; 554af245d11STodd Fiala } 555af245d11STodd Fiala } 556af245d11STodd Fiala 557b9c1b51eSKate Stone static int convert_pid_status_to_return_code(int status) { 558af245d11STodd Fiala if (WIFEXITED(status)) 559af245d11STodd Fiala return WEXITSTATUS(status); 560af245d11STodd Fiala else if (WIFSIGNALED(status)) 561af245d11STodd Fiala return WTERMSIG(status); 562af245d11STodd Fiala else if (WIFSTOPPED(status)) 563af245d11STodd Fiala return WSTOPSIG(status); 564b9c1b51eSKate Stone else { 565af245d11STodd Fiala // We don't know what this is. 566af245d11STodd Fiala return ExitType::eExitTypeInvalid; 567af245d11STodd Fiala } 568af245d11STodd Fiala } 569af245d11STodd Fiala 5701107b5a5SPavel Labath // Handles all waitpid events from the inferior process. 571b9c1b51eSKate Stone void NativeProcessLinux::MonitorCallback(lldb::pid_t pid, bool exited, 572b9c1b51eSKate Stone int signal, int status) { 573af245d11STodd Fiala Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 574af245d11STodd Fiala 575b9c1b51eSKate Stone // Certain activities differ based on whether the pid is the tid of the main 576b9c1b51eSKate Stone // thread. 5771107b5a5SPavel Labath const bool is_main_thread = (pid == GetID()); 578af245d11STodd Fiala 579af245d11STodd Fiala // Handle when the thread exits. 580b9c1b51eSKate Stone if (exited) { 581af245d11STodd Fiala if (log) 582b9c1b51eSKate Stone log->Printf( 583b9c1b51eSKate Stone "NativeProcessLinux::%s() got exit signal(%d) , tid = %" PRIu64 584b9c1b51eSKate Stone " (%s main thread)", 585b9c1b51eSKate Stone __FUNCTION__, signal, pid, is_main_thread ? "is" : "is not"); 586af245d11STodd Fiala 587af245d11STodd Fiala // This is a thread that exited. Ensure we're not tracking it anymore. 5881107b5a5SPavel Labath const bool thread_found = StopTrackingThread(pid); 589af245d11STodd Fiala 590b9c1b51eSKate Stone if (is_main_thread) { 591b9c1b51eSKate Stone // We only set the exit status and notify the delegate if we haven't 592b9c1b51eSKate Stone // already set the process 593b9c1b51eSKate Stone // state to an exited state. We normally should have received a SIGTRAP | 594b9c1b51eSKate Stone // (PTRACE_EVENT_EXIT << 8) 595af245d11STodd Fiala // for the main thread. 596b9c1b51eSKate Stone const bool already_notified = (GetState() == StateType::eStateExited) || 597b9c1b51eSKate Stone (GetState() == StateType::eStateCrashed); 598b9c1b51eSKate Stone if (!already_notified) { 599af245d11STodd Fiala if (log) 600b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() tid = %" PRIu64 601b9c1b51eSKate Stone " handling main thread exit (%s), expected exit state " 602b9c1b51eSKate Stone "already set but state was %s instead, setting exit " 603b9c1b51eSKate Stone "state now", 604b9c1b51eSKate Stone __FUNCTION__, pid, 605b9c1b51eSKate Stone thread_found ? "stopped tracking thread metadata" 606b9c1b51eSKate Stone : "thread metadata not found", 607b9c1b51eSKate Stone StateAsCString(GetState())); 608af245d11STodd Fiala // The main thread exited. We're done monitoring. Report to delegate. 609b9c1b51eSKate Stone SetExitStatus(convert_pid_status_to_exit_type(status), 610b9c1b51eSKate Stone convert_pid_status_to_return_code(status), nullptr, true); 611af245d11STodd Fiala 612af245d11STodd Fiala // Notify delegate that our process has exited. 6131107b5a5SPavel Labath SetState(StateType::eStateExited, true); 614b9c1b51eSKate Stone } else { 615af245d11STodd Fiala if (log) 616b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() tid = %" PRIu64 617b9c1b51eSKate Stone " main thread now exited (%s)", 618b9c1b51eSKate Stone __FUNCTION__, pid, 619b9c1b51eSKate Stone thread_found ? "stopped tracking thread metadata" 620b9c1b51eSKate Stone : "thread metadata not found"); 621af245d11STodd Fiala } 622b9c1b51eSKate Stone } else { 623b9c1b51eSKate Stone // Do we want to report to the delegate in this case? I think not. If 624b9c1b51eSKate Stone // this was an orderly 625b9c1b51eSKate Stone // thread exit, we would already have received the SIGTRAP | 626b9c1b51eSKate Stone // (PTRACE_EVENT_EXIT << 8) signal, 627af245d11STodd Fiala // and we would have done an all-stop then. 628af245d11STodd Fiala if (log) 629b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() tid = %" PRIu64 630b9c1b51eSKate Stone " handling non-main thread exit (%s)", 631b9c1b51eSKate Stone __FUNCTION__, pid, 632b9c1b51eSKate Stone thread_found ? "stopped tracking thread metadata" 633b9c1b51eSKate Stone : "thread metadata not found"); 634af245d11STodd Fiala } 6351107b5a5SPavel Labath return; 636af245d11STodd Fiala } 637af245d11STodd Fiala 638af245d11STodd Fiala siginfo_t info; 639b9cc0c75SPavel Labath const auto info_err = GetSignalInfo(pid, &info); 640b9cc0c75SPavel Labath auto thread_sp = GetThreadByID(pid); 641b9cc0c75SPavel Labath 642b9c1b51eSKate Stone if (!thread_sp) { 643b9c1b51eSKate Stone // Normally, the only situation when we cannot find the thread is if we have 644b9c1b51eSKate Stone // just 645b9c1b51eSKate Stone // received a new thread notification. This is indicated by GetSignalInfo() 646b9c1b51eSKate Stone // returning 647b9cc0c75SPavel Labath // si_code == SI_USER and si_pid == 0 648b9cc0c75SPavel Labath if (log) 649b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s received notification about an " 650b9c1b51eSKate Stone "unknown tid %" PRIu64 ".", 651b9c1b51eSKate Stone __FUNCTION__, pid); 652b9cc0c75SPavel Labath 653b9c1b51eSKate Stone if (info_err.Fail()) { 654b9cc0c75SPavel Labath if (log) 655b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s (tid %" PRIu64 656b9c1b51eSKate Stone ") GetSignalInfo failed (%s). Ingoring this notification.", 657b9c1b51eSKate Stone __FUNCTION__, pid, info_err.AsCString()); 658b9cc0c75SPavel Labath return; 659b9cc0c75SPavel Labath } 660b9cc0c75SPavel Labath 661b9cc0c75SPavel Labath if (log && (info.si_code != SI_USER || info.si_pid != 0)) 662b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s (tid %" PRIu64 663b9c1b51eSKate Stone ") unexpected signal info (si_code: %d, si_pid: %d). " 664b9c1b51eSKate Stone "Treating as a new thread notification anyway.", 665b9c1b51eSKate Stone __FUNCTION__, pid, info.si_code, info.si_pid); 666b9cc0c75SPavel Labath 667b9cc0c75SPavel Labath auto thread_sp = AddThread(pid); 668b9cc0c75SPavel Labath // Resume the newly created thread. 669b9cc0c75SPavel Labath ResumeThread(*thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); 670b9cc0c75SPavel Labath ThreadWasCreated(*thread_sp); 671b9cc0c75SPavel Labath return; 672b9cc0c75SPavel Labath } 673b9cc0c75SPavel Labath 674b9cc0c75SPavel Labath // Get details on the signal raised. 675b9c1b51eSKate Stone if (info_err.Success()) { 676fa03ad2eSChaoren Lin // We have retrieved the signal info. Dispatch appropriately. 677fa03ad2eSChaoren Lin if (info.si_signo == SIGTRAP) 678b9cc0c75SPavel Labath MonitorSIGTRAP(info, *thread_sp); 679fa03ad2eSChaoren Lin else 680b9cc0c75SPavel Labath MonitorSignal(info, *thread_sp, exited); 681b9c1b51eSKate Stone } else { 682b9c1b51eSKate Stone if (info_err.GetError() == EINVAL) { 683fa03ad2eSChaoren Lin // This is a group stop reception for this tid. 684b9c1b51eSKate Stone // We can reach here if we reinject SIGSTOP, SIGSTP, SIGTTIN or SIGTTOU 685b9c1b51eSKate Stone // into the 686b9c1b51eSKate Stone // tracee, triggering the group-stop mechanism. Normally receiving these 687b9c1b51eSKate Stone // would stop 688b9c1b51eSKate Stone // the process, pending a SIGCONT. Simulating this state in a debugger is 689b9c1b51eSKate Stone // hard and is 690b9c1b51eSKate Stone // generally not needed (one use case is debugging background task being 691b9c1b51eSKate Stone // managed by a 692b9c1b51eSKate Stone // shell). For general use, it is sufficient to stop the process in a 693b9c1b51eSKate Stone // signal-delivery 694b9c1b51eSKate Stone // stop which happens before the group stop. This done by MonitorSignal 695b9c1b51eSKate Stone // and works 69639036ac3SPavel Labath // correctly for all signals. 697fa03ad2eSChaoren Lin if (log) 698b9c1b51eSKate Stone log->Printf( 699b9c1b51eSKate Stone "NativeProcessLinux::%s received a group stop for pid %" PRIu64 700b9c1b51eSKate Stone " tid %" PRIu64 ". Transparent handling of group stops not " 701b9c1b51eSKate Stone "supported, resuming the thread.", 702b9c1b51eSKate Stone __FUNCTION__, GetID(), pid); 703b9c1b51eSKate Stone ResumeThread(*thread_sp, thread_sp->GetState(), 704b9c1b51eSKate Stone LLDB_INVALID_SIGNAL_NUMBER); 705b9c1b51eSKate Stone } else { 706af245d11STodd Fiala // ptrace(GETSIGINFO) failed (but not due to group-stop). 707af245d11STodd Fiala 708b9c1b51eSKate Stone // A return value of ESRCH means the thread/process is no longer on the 709b9c1b51eSKate Stone // system, 710b9c1b51eSKate Stone // so it was killed somehow outside of our control. Either way, we can't 711b9c1b51eSKate Stone // do anything 712af245d11STodd Fiala // with it anymore. 713af245d11STodd Fiala 714b9c1b51eSKate Stone // Stop tracking the metadata for the thread since it's entirely off the 715b9c1b51eSKate Stone // system now. 7161107b5a5SPavel Labath const bool thread_found = StopTrackingThread(pid); 717af245d11STodd Fiala 718af245d11STodd Fiala if (log) 719b9c1b51eSKate Stone log->Printf( 720b9c1b51eSKate Stone "NativeProcessLinux::%s GetSignalInfo failed: %s, tid = %" PRIu64 721b9c1b51eSKate Stone ", signal = %d, status = %d (%s, %s, %s)", 722b9c1b51eSKate Stone __FUNCTION__, info_err.AsCString(), pid, signal, status, 723b9c1b51eSKate Stone info_err.GetError() == ESRCH ? "thread/process killed" 724b9c1b51eSKate Stone : "unknown reason", 725b9c1b51eSKate Stone is_main_thread ? "is main thread" : "is not main thread", 726b9c1b51eSKate Stone thread_found ? "thread metadata removed" 727b9c1b51eSKate Stone : "thread metadata not found"); 728af245d11STodd Fiala 729b9c1b51eSKate Stone if (is_main_thread) { 730b9c1b51eSKate Stone // Notify the delegate - our process is not available but appears to 731b9c1b51eSKate Stone // have been killed outside 732af245d11STodd Fiala // our control. Is eStateExited the right exit state in this case? 733b9c1b51eSKate Stone SetExitStatus(convert_pid_status_to_exit_type(status), 734b9c1b51eSKate Stone convert_pid_status_to_return_code(status), nullptr, true); 7351107b5a5SPavel Labath SetState(StateType::eStateExited, true); 736b9c1b51eSKate Stone } else { 737b9c1b51eSKate Stone // This thread was pulled out from underneath us. Anything to do here? 738b9c1b51eSKate Stone // Do we want to do an all stop? 739af245d11STodd Fiala if (log) 740b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 741b9c1b51eSKate Stone " non-main thread exit occurred, didn't tell delegate " 742b9c1b51eSKate Stone "anything since thread disappeared out from underneath " 743b9c1b51eSKate Stone "us", 744b9c1b51eSKate Stone __FUNCTION__, GetID(), pid); 745af245d11STodd Fiala } 746af245d11STodd Fiala } 747af245d11STodd Fiala } 748af245d11STodd Fiala } 749af245d11STodd Fiala 750b9c1b51eSKate Stone void NativeProcessLinux::WaitForNewThread(::pid_t tid) { 751426bdf88SPavel Labath Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 752426bdf88SPavel Labath 753f9077782SPavel Labath NativeThreadLinuxSP new_thread_sp = GetThreadByID(tid); 754426bdf88SPavel Labath 755b9c1b51eSKate Stone if (new_thread_sp) { 756b9c1b51eSKate Stone // We are already tracking the thread - we got the event on the new thread 757b9c1b51eSKate Stone // (see 758426bdf88SPavel Labath // MonitorSignal) before this one. We are done. 759426bdf88SPavel Labath return; 760426bdf88SPavel Labath } 761426bdf88SPavel Labath 762426bdf88SPavel Labath // The thread is not tracked yet, let's wait for it to appear. 763426bdf88SPavel Labath int status = -1; 764426bdf88SPavel Labath ::pid_t wait_pid; 765b9c1b51eSKate Stone do { 766426bdf88SPavel Labath if (log) 767b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() received thread creation event for " 768b9c1b51eSKate Stone "tid %" PRIu32 769b9c1b51eSKate Stone ". tid not tracked yet, waiting for thread to appear...", 770b9c1b51eSKate Stone __FUNCTION__, tid); 771426bdf88SPavel Labath wait_pid = waitpid(tid, &status, __WALL); 772b9c1b51eSKate Stone } while (wait_pid == -1 && errno == EINTR); 773b9c1b51eSKate Stone // Since we are waiting on a specific tid, this must be the creation event. 774b9c1b51eSKate Stone // But let's do 775426bdf88SPavel Labath // some checks just in case. 776426bdf88SPavel Labath if (wait_pid != tid) { 777426bdf88SPavel Labath if (log) 778b9c1b51eSKate Stone log->Printf( 779b9c1b51eSKate Stone "NativeProcessLinux::%s() waiting for tid %" PRIu32 780b9c1b51eSKate Stone " failed. Assuming the thread has disappeared in the meantime", 781b9c1b51eSKate Stone __FUNCTION__, tid); 782426bdf88SPavel Labath // The only way I know of this could happen is if the whole process was 783b9c1b51eSKate Stone // SIGKILLed in the mean time. In any case, we can't do anything about that 784b9c1b51eSKate Stone // now. 785426bdf88SPavel Labath return; 786426bdf88SPavel Labath } 787b9c1b51eSKate Stone if (WIFEXITED(status)) { 788426bdf88SPavel Labath if (log) 789b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() waiting for tid %" PRIu32 790b9c1b51eSKate Stone " returned an 'exited' event. Not tracking the thread.", 791b9c1b51eSKate Stone __FUNCTION__, tid); 792426bdf88SPavel Labath // Also a very improbable event. 793426bdf88SPavel Labath return; 794426bdf88SPavel Labath } 795426bdf88SPavel Labath 796426bdf88SPavel Labath siginfo_t info; 797426bdf88SPavel Labath Error error = GetSignalInfo(tid, &info); 798b9c1b51eSKate Stone if (error.Fail()) { 799426bdf88SPavel Labath if (log) 800b9c1b51eSKate Stone log->Printf( 801b9c1b51eSKate Stone "NativeProcessLinux::%s() GetSignalInfo for tid %" PRIu32 802b9c1b51eSKate Stone " failed. Assuming the thread has disappeared in the meantime.", 803b9c1b51eSKate Stone __FUNCTION__, tid); 804426bdf88SPavel Labath return; 805426bdf88SPavel Labath } 806426bdf88SPavel Labath 807b9c1b51eSKate Stone if (((info.si_pid != 0) || (info.si_code != SI_USER)) && log) { 808b9c1b51eSKate Stone // We should be getting a thread creation signal here, but we received 809b9c1b51eSKate Stone // something 810b9c1b51eSKate Stone // else. There isn't much we can do about it now, so we will just log that. 811b9c1b51eSKate Stone // Since the 812b9c1b51eSKate Stone // thread is alive and we are receiving events from it, we shall pretend 813b9c1b51eSKate Stone // that it was 814426bdf88SPavel Labath // created properly. 815b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() GetSignalInfo for tid %" PRIu32 816b9c1b51eSKate Stone " received unexpected signal with code %d from pid %d.", 817b9c1b51eSKate Stone __FUNCTION__, tid, info.si_code, info.si_pid); 818426bdf88SPavel Labath } 819426bdf88SPavel Labath 820426bdf88SPavel Labath if (log) 821b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 822b9c1b51eSKate Stone ": tracking new thread tid %" PRIu32, 823426bdf88SPavel Labath __FUNCTION__, GetID(), tid); 824426bdf88SPavel Labath 825f9077782SPavel Labath new_thread_sp = AddThread(tid); 826b9cc0c75SPavel Labath ResumeThread(*new_thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); 827f9077782SPavel Labath ThreadWasCreated(*new_thread_sp); 828426bdf88SPavel Labath } 829426bdf88SPavel Labath 830b9c1b51eSKate Stone void NativeProcessLinux::MonitorSIGTRAP(const siginfo_t &info, 831b9c1b51eSKate Stone NativeThreadLinux &thread) { 832af245d11STodd Fiala Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 833b9cc0c75SPavel Labath const bool is_main_thread = (thread.GetID() == GetID()); 834af245d11STodd Fiala 835b9cc0c75SPavel Labath assert(info.si_signo == SIGTRAP && "Unexpected child signal!"); 836af245d11STodd Fiala 837b9c1b51eSKate Stone switch (info.si_code) { 838b9c1b51eSKate Stone // TODO: these two cases are required if we want to support tracing of the 839b9c1b51eSKate Stone // inferiors' children. We'd need this to debug a monitor. 840af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)): 841af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)): 842af245d11STodd Fiala 843b9c1b51eSKate Stone case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)): { 844b9c1b51eSKate Stone // This is the notification on the parent thread which informs us of new 845b9c1b51eSKate Stone // thread 846426bdf88SPavel Labath // creation. 847b9c1b51eSKate Stone // We don't want to do anything with the parent thread so we just resume it. 848b9c1b51eSKate Stone // In case we 849b9c1b51eSKate Stone // want to implement "break on thread creation" functionality, we would need 850b9c1b51eSKate Stone // to stop 851426bdf88SPavel Labath // here. 852af245d11STodd Fiala 853af245d11STodd Fiala unsigned long event_message = 0; 854b9c1b51eSKate Stone if (GetEventMessage(thread.GetID(), &event_message).Fail()) { 855426bdf88SPavel Labath if (log) 856b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() pid %" PRIu64 857b9c1b51eSKate Stone " received thread creation event but GetEventMessage " 858b9c1b51eSKate Stone "failed so we don't know the new tid", 859b9c1b51eSKate Stone __FUNCTION__, thread.GetID()); 860426bdf88SPavel Labath } else 861426bdf88SPavel Labath WaitForNewThread(event_message); 862af245d11STodd Fiala 863b9cc0c75SPavel Labath ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); 864af245d11STodd Fiala break; 865af245d11STodd Fiala } 866af245d11STodd Fiala 867b9c1b51eSKate Stone case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): { 868f9077782SPavel Labath NativeThreadLinuxSP main_thread_sp; 869af245d11STodd Fiala if (log) 870b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() received exec event, code = %d", 871b9c1b51eSKate Stone __FUNCTION__, info.si_code ^ SIGTRAP); 872a9882ceeSTodd Fiala 8731dbc6c9cSPavel Labath // Exec clears any pending notifications. 8740e1d729bSPavel Labath m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 875fa03ad2eSChaoren Lin 876b9c1b51eSKate Stone // Remove all but the main thread here. Linux fork creates a new process 877b9c1b51eSKate Stone // which only copies the main thread. 878a9882ceeSTodd Fiala if (log) 879b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s exec received, stop tracking all but " 880b9c1b51eSKate Stone "main thread", 881b9c1b51eSKate Stone __FUNCTION__); 882a9882ceeSTodd Fiala 883b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 884a9882ceeSTodd Fiala const bool is_main_thread = thread_sp && thread_sp->GetID() == GetID(); 885b9c1b51eSKate Stone if (is_main_thread) { 886f9077782SPavel Labath main_thread_sp = std::static_pointer_cast<NativeThreadLinux>(thread_sp); 887a9882ceeSTodd Fiala if (log) 888b9c1b51eSKate Stone log->Printf( 889b9c1b51eSKate Stone "NativeProcessLinux::%s found main thread with tid %" PRIu64 890b9c1b51eSKate Stone ", keeping", 891b9c1b51eSKate Stone __FUNCTION__, main_thread_sp->GetID()); 892b9c1b51eSKate Stone } else { 893a9882ceeSTodd Fiala if (log) 894b9c1b51eSKate Stone log->Printf( 895b9c1b51eSKate Stone "NativeProcessLinux::%s discarding non-main-thread tid %" PRIu64 896b9c1b51eSKate Stone " due to exec", 897b9c1b51eSKate Stone __FUNCTION__, thread_sp->GetID()); 898a9882ceeSTodd Fiala } 899a9882ceeSTodd Fiala } 900a9882ceeSTodd Fiala 901a9882ceeSTodd Fiala m_threads.clear(); 902a9882ceeSTodd Fiala 903b9c1b51eSKate Stone if (main_thread_sp) { 904a9882ceeSTodd Fiala m_threads.push_back(main_thread_sp); 905a9882ceeSTodd Fiala SetCurrentThreadID(main_thread_sp->GetID()); 906f9077782SPavel Labath main_thread_sp->SetStoppedByExec(); 907b9c1b51eSKate Stone } else { 908a9882ceeSTodd Fiala SetCurrentThreadID(LLDB_INVALID_THREAD_ID); 909a9882ceeSTodd Fiala if (log) 910b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s pid %" PRIu64 911b9c1b51eSKate Stone "no main thread found, discarded all threads, we're in a " 912b9c1b51eSKate Stone "no-thread state!", 913b9c1b51eSKate Stone __FUNCTION__, GetID()); 914a9882ceeSTodd Fiala } 915a9882ceeSTodd Fiala 916fa03ad2eSChaoren Lin // Tell coordinator about about the "new" (since exec) stopped main thread. 917f9077782SPavel Labath ThreadWasCreated(*main_thread_sp); 918fa03ad2eSChaoren Lin 919a9882ceeSTodd Fiala // Let our delegate know we have just exec'd. 920a9882ceeSTodd Fiala NotifyDidExec(); 921a9882ceeSTodd Fiala 922a9882ceeSTodd Fiala // If we have a main thread, indicate we are stopped. 923b9c1b51eSKate Stone assert(main_thread_sp && "exec called during ptraced process but no main " 924b9c1b51eSKate Stone "thread metadata tracked"); 925fa03ad2eSChaoren Lin 926fa03ad2eSChaoren Lin // Let the process know we're stopped. 927b9cc0c75SPavel Labath StopRunningThreads(main_thread_sp->GetID()); 928a9882ceeSTodd Fiala 929af245d11STodd Fiala break; 930a9882ceeSTodd Fiala } 931af245d11STodd Fiala 932b9c1b51eSKate Stone case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): { 933af245d11STodd Fiala // The inferior process or one of its threads is about to exit. 934b9c1b51eSKate Stone // We don't want to do anything with the thread so we just resume it. In 935b9c1b51eSKate Stone // case we 936b9c1b51eSKate Stone // want to implement "break on thread exit" functionality, we would need to 937b9c1b51eSKate Stone // stop 9386e35163cSPavel Labath // here. 939fa03ad2eSChaoren Lin 940af245d11STodd Fiala unsigned long data = 0; 941b9cc0c75SPavel Labath if (GetEventMessage(thread.GetID(), &data).Fail()) 942af245d11STodd Fiala data = -1; 943af245d11STodd Fiala 944b9c1b51eSKate Stone if (log) { 945b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() received PTRACE_EVENT_EXIT, data = " 946b9c1b51eSKate Stone "%lx (WIFEXITED=%s,WIFSIGNALED=%s), pid = %" PRIu64 " (%s)", 947b9c1b51eSKate Stone __FUNCTION__, data, WIFEXITED(data) ? "true" : "false", 948b9c1b51eSKate Stone WIFSIGNALED(data) ? "true" : "false", thread.GetID(), 949af245d11STodd Fiala is_main_thread ? "is main thread" : "not main thread"); 950af245d11STodd Fiala } 951af245d11STodd Fiala 952b9c1b51eSKate Stone if (is_main_thread) { 953b9c1b51eSKate Stone SetExitStatus(convert_pid_status_to_exit_type(data), 954b9c1b51eSKate Stone convert_pid_status_to_return_code(data), nullptr, true); 95575f47c3aSTodd Fiala } 95675f47c3aSTodd Fiala 95786852d36SPavel Labath StateType state = thread.GetState(); 958b9c1b51eSKate Stone if (!StateIsRunningState(state)) { 959b9c1b51eSKate Stone // Due to a kernel bug, we may sometimes get this stop after the inferior 960b9c1b51eSKate Stone // gets a 961b9c1b51eSKate Stone // SIGKILL. This confuses our state tracking logic in ResumeThread(), 962b9c1b51eSKate Stone // since normally, 963b9c1b51eSKate Stone // we should not be receiving any ptrace events while the inferior is 964b9c1b51eSKate Stone // stopped. This 96586852d36SPavel Labath // makes sure that the inferior is resumed and exits normally. 96686852d36SPavel Labath state = eStateRunning; 96786852d36SPavel Labath } 96886852d36SPavel Labath ResumeThread(thread, state, LLDB_INVALID_SIGNAL_NUMBER); 969af245d11STodd Fiala 970af245d11STodd Fiala break; 971af245d11STodd Fiala } 972af245d11STodd Fiala 973af245d11STodd Fiala case 0: 974c16f5dcaSChaoren Lin case TRAP_TRACE: // We receive this on single stepping. 975c16f5dcaSChaoren Lin case TRAP_HWBKPT: // We receive this on watchpoint hit 97686fd8e45SChaoren Lin { 977c16f5dcaSChaoren Lin // If a watchpoint was hit, report it 978c16f5dcaSChaoren Lin uint32_t wp_index; 979b9c1b51eSKate Stone Error error = thread.GetRegisterContext()->GetWatchpointHitIndex( 980b9c1b51eSKate Stone wp_index, (uintptr_t)info.si_addr); 981c16f5dcaSChaoren Lin if (error.Fail() && log) 982c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() " 983c16f5dcaSChaoren Lin "received error while checking for watchpoint hits, " 984c16f5dcaSChaoren Lin "pid = %" PRIu64 " error = %s", 985b9cc0c75SPavel Labath __FUNCTION__, thread.GetID(), error.AsCString()); 986b9c1b51eSKate Stone if (wp_index != LLDB_INVALID_INDEX32) { 987b9cc0c75SPavel Labath MonitorWatchpoint(thread, wp_index); 988c16f5dcaSChaoren Lin break; 989c16f5dcaSChaoren Lin } 990b9cc0c75SPavel Labath 991be379e15STamas Berghammer // Otherwise, report step over 992be379e15STamas Berghammer MonitorTrace(thread); 993af245d11STodd Fiala break; 994b9cc0c75SPavel Labath } 995af245d11STodd Fiala 996af245d11STodd Fiala case SI_KERNEL: 99735799963SMohit K. Bhakkad #if defined __mips__ 99835799963SMohit K. Bhakkad // For mips there is no special signal for watchpoint 99935799963SMohit K. Bhakkad // So we check for watchpoint in kernel trap 100035799963SMohit K. Bhakkad { 100135799963SMohit K. Bhakkad // If a watchpoint was hit, report it 100235799963SMohit K. Bhakkad uint32_t wp_index; 1003b9c1b51eSKate Stone Error error = thread.GetRegisterContext()->GetWatchpointHitIndex( 1004b9c1b51eSKate Stone wp_index, LLDB_INVALID_ADDRESS); 100535799963SMohit K. Bhakkad if (error.Fail() && log) 100635799963SMohit K. Bhakkad log->Printf("NativeProcessLinux::%s() " 100735799963SMohit K. Bhakkad "received error while checking for watchpoint hits, " 100835799963SMohit K. Bhakkad "pid = %" PRIu64 " error = %s", 100916ad0321SMohit K. Bhakkad __FUNCTION__, thread.GetID(), error.AsCString()); 1010b9c1b51eSKate Stone if (wp_index != LLDB_INVALID_INDEX32) { 1011b9cc0c75SPavel Labath MonitorWatchpoint(thread, wp_index); 101235799963SMohit K. Bhakkad break; 101335799963SMohit K. Bhakkad } 101435799963SMohit K. Bhakkad } 101535799963SMohit K. Bhakkad // NO BREAK 101635799963SMohit K. Bhakkad #endif 1017af245d11STodd Fiala case TRAP_BRKPT: 1018b9cc0c75SPavel Labath MonitorBreakpoint(thread); 1019af245d11STodd Fiala break; 1020af245d11STodd Fiala 1021af245d11STodd Fiala case SIGTRAP: 1022af245d11STodd Fiala case (SIGTRAP | 0x80): 1023af245d11STodd Fiala if (log) 1024b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() received unknown SIGTRAP system " 1025b9c1b51eSKate Stone "call stop event, pid %" PRIu64 "tid %" PRIu64 ", resuming", 1026b9c1b51eSKate Stone __FUNCTION__, GetID(), thread.GetID()); 1027fa03ad2eSChaoren Lin 1028af245d11STodd Fiala // Ignore these signals until we know more about them. 1029b9cc0c75SPavel Labath ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); 1030af245d11STodd Fiala break; 1031af245d11STodd Fiala 1032af245d11STodd Fiala default: 1033af245d11STodd Fiala assert(false && "Unexpected SIGTRAP code!"); 1034af245d11STodd Fiala if (log) 1035b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() pid %" PRIu64 "tid %" PRIu64 1036b9c1b51eSKate Stone " received unhandled SIGTRAP code: 0x%d", 1037b9cc0c75SPavel Labath __FUNCTION__, GetID(), thread.GetID(), info.si_code); 1038af245d11STodd Fiala break; 1039af245d11STodd Fiala } 1040af245d11STodd Fiala } 1041af245d11STodd Fiala 1042b9c1b51eSKate Stone void NativeProcessLinux::MonitorTrace(NativeThreadLinux &thread) { 1043c16f5dcaSChaoren Lin Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 1044c16f5dcaSChaoren Lin if (log) 1045b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() received trace event, pid = %" PRIu64 1046b9c1b51eSKate Stone " (single stepping)", 1047b9cc0c75SPavel Labath __FUNCTION__, thread.GetID()); 1048c16f5dcaSChaoren Lin 10490e1d729bSPavel Labath // This thread is currently stopped. 1050b9cc0c75SPavel Labath thread.SetStoppedByTrace(); 1051c16f5dcaSChaoren Lin 1052b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 1053c16f5dcaSChaoren Lin } 1054c16f5dcaSChaoren Lin 1055b9c1b51eSKate Stone void NativeProcessLinux::MonitorBreakpoint(NativeThreadLinux &thread) { 1056b9c1b51eSKate Stone Log *log( 1057b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 1058c16f5dcaSChaoren Lin if (log) 1059b9c1b51eSKate Stone log->Printf( 1060b9c1b51eSKate Stone "NativeProcessLinux::%s() received breakpoint event, pid = %" PRIu64, 1061b9cc0c75SPavel Labath __FUNCTION__, thread.GetID()); 1062c16f5dcaSChaoren Lin 1063c16f5dcaSChaoren Lin // Mark the thread as stopped at breakpoint. 1064b9cc0c75SPavel Labath thread.SetStoppedByBreakpoint(); 1065b9cc0c75SPavel Labath Error error = FixupBreakpointPCAsNeeded(thread); 1066c16f5dcaSChaoren Lin if (error.Fail()) 1067c16f5dcaSChaoren Lin if (log) 1068c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " fixup: %s", 1069b9cc0c75SPavel Labath __FUNCTION__, thread.GetID(), error.AsCString()); 1070d8c338d4STamas Berghammer 1071b9c1b51eSKate Stone if (m_threads_stepping_with_breakpoint.find(thread.GetID()) != 1072b9c1b51eSKate Stone m_threads_stepping_with_breakpoint.end()) 1073b9cc0c75SPavel Labath thread.SetStoppedByTrace(); 1074c16f5dcaSChaoren Lin 1075b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 1076c16f5dcaSChaoren Lin } 1077c16f5dcaSChaoren Lin 1078b9c1b51eSKate Stone void NativeProcessLinux::MonitorWatchpoint(NativeThreadLinux &thread, 1079b9c1b51eSKate Stone uint32_t wp_index) { 1080b9c1b51eSKate Stone Log *log( 1081b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_WATCHPOINTS)); 1082c16f5dcaSChaoren Lin if (log) 1083c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() received watchpoint event, " 1084c16f5dcaSChaoren Lin "pid = %" PRIu64 ", wp_index = %" PRIu32, 1085f9077782SPavel Labath __FUNCTION__, thread.GetID(), wp_index); 1086c16f5dcaSChaoren Lin 1087c16f5dcaSChaoren Lin // Mark the thread as stopped at watchpoint. 1088c16f5dcaSChaoren Lin // The address is at (lldb::addr_t)info->si_addr if we need it. 1089f9077782SPavel Labath thread.SetStoppedByWatchpoint(wp_index); 1090c16f5dcaSChaoren Lin 1091b9c1b51eSKate Stone // We need to tell all other running threads before we notify the delegate 1092b9c1b51eSKate Stone // about this stop. 1093f9077782SPavel Labath StopRunningThreads(thread.GetID()); 1094c16f5dcaSChaoren Lin } 1095c16f5dcaSChaoren Lin 1096b9c1b51eSKate Stone void NativeProcessLinux::MonitorSignal(const siginfo_t &info, 1097b9c1b51eSKate Stone NativeThreadLinux &thread, bool exited) { 1098b9cc0c75SPavel Labath const int signo = info.si_signo; 1099b9cc0c75SPavel Labath const bool is_from_llgs = info.si_pid == getpid(); 1100af245d11STodd Fiala 1101af245d11STodd Fiala Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 1102af245d11STodd Fiala 1103af245d11STodd Fiala // POSIX says that process behaviour is undefined after it ignores a SIGFPE, 1104af245d11STodd Fiala // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a 1105af245d11STodd Fiala // kill(2) or raise(3). Similarly for tgkill(2) on Linux. 1106af245d11STodd Fiala // 1107af245d11STodd Fiala // IOW, user generated signals never generate what we consider to be a 1108af245d11STodd Fiala // "crash". 1109af245d11STodd Fiala // 1110af245d11STodd Fiala // Similarly, ACK signals generated by this monitor. 1111af245d11STodd Fiala 1112af245d11STodd Fiala // Handle the signal. 1113b9c1b51eSKate Stone if (info.si_code == SI_TKILL || info.si_code == SI_USER) { 1114af245d11STodd Fiala if (log) 1115b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() received signal %s (%d) with code " 1116b9c1b51eSKate Stone "%s, (siginfo pid = %d (%s), waitpid pid = %" PRIu64 ")", 1117b9c1b51eSKate Stone __FUNCTION__, Host::GetSignalAsCString(signo), signo, 1118b9cc0c75SPavel Labath (info.si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"), 1119b9c1b51eSKate Stone info.si_pid, is_from_llgs ? "from llgs" : "not from llgs", 1120b9cc0c75SPavel Labath thread.GetID()); 1121af245d11STodd Fiala } 112258a2f669STodd Fiala 112358a2f669STodd Fiala // Check for thread stop notification. 1124b9c1b51eSKate Stone if (is_from_llgs && (info.si_code == SI_TKILL) && (signo == SIGSTOP)) { 1125af245d11STodd Fiala // This is a tgkill()-based stop. 1126fa03ad2eSChaoren Lin if (log) 1127b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 1128b9c1b51eSKate Stone ", thread stopped", 1129b9c1b51eSKate Stone __FUNCTION__, GetID(), thread.GetID()); 1130fa03ad2eSChaoren Lin 1131aab58633SChaoren Lin // Check that we're not already marked with a stop reason. 1132b9c1b51eSKate Stone // Note this thread really shouldn't already be marked as stopped - if we 1133b9c1b51eSKate Stone // were, that would imply that 1134b9c1b51eSKate Stone // the kernel signaled us with the thread stopping which we handled and 1135b9c1b51eSKate Stone // marked as stopped, 1136b9c1b51eSKate Stone // and that, without an intervening resume, we received another stop. It is 1137b9c1b51eSKate Stone // more likely 1138b9c1b51eSKate Stone // that we are missing the marking of a run state somewhere if we find that 1139b9c1b51eSKate Stone // the thread was 1140aab58633SChaoren Lin // marked as stopped. 1141b9cc0c75SPavel Labath const StateType thread_state = thread.GetState(); 1142b9c1b51eSKate Stone if (!StateIsStoppedState(thread_state, false)) { 1143ed89c7feSPavel Labath // An inferior thread has stopped because of a SIGSTOP we have sent it. 1144b9c1b51eSKate Stone // Generally, these are not important stops and we don't want to report 1145b9c1b51eSKate Stone // them as 1146b9c1b51eSKate Stone // they are just used to stop other threads when one thread (the one with 1147b9c1b51eSKate Stone // the 1148b9c1b51eSKate Stone // *real* stop reason) hits a breakpoint (watchpoint, etc...). However, in 1149b9c1b51eSKate Stone // the 1150b9c1b51eSKate Stone // case of an asynchronous Interrupt(), this *is* the real stop reason, so 1151b9c1b51eSKate Stone // we 1152ed89c7feSPavel Labath // leave the signal intact if this is the thread that was chosen as the 1153ed89c7feSPavel Labath // triggering thread. 1154b9c1b51eSKate Stone if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) { 1155b9cc0c75SPavel Labath if (m_pending_notification_tid == thread.GetID()) 1156b9cc0c75SPavel Labath thread.SetStoppedBySignal(SIGSTOP, &info); 1157ed89c7feSPavel Labath else 1158b9cc0c75SPavel Labath thread.SetStoppedWithNoReason(); 1159ed89c7feSPavel Labath 1160b9cc0c75SPavel Labath SetCurrentThreadID(thread.GetID()); 11610e1d729bSPavel Labath SignalIfAllThreadsStopped(); 1162b9c1b51eSKate Stone } else { 11630e1d729bSPavel Labath // We can end up here if stop was initiated by LLGS but by this time a 11640e1d729bSPavel Labath // thread stop has occurred - maybe initiated by another event. 1165b9cc0c75SPavel Labath Error error = ResumeThread(thread, thread.GetState(), 0); 1166b9c1b51eSKate Stone if (error.Fail() && log) { 1167b9c1b51eSKate Stone log->Printf( 1168b9c1b51eSKate Stone "NativeProcessLinux::%s failed to resume thread tid %" PRIu64 1169b9c1b51eSKate Stone ": %s", 1170b9cc0c75SPavel Labath __FUNCTION__, thread.GetID(), error.AsCString()); 11710e1d729bSPavel Labath } 11720e1d729bSPavel Labath } 1173b9c1b51eSKate Stone } else { 1174b9c1b51eSKate Stone if (log) { 1175aab58633SChaoren Lin // Retrieve the signal name if the thread was stopped by a signal. 1176aab58633SChaoren Lin int stop_signo = 0; 1177b9cc0c75SPavel Labath const bool stopped_by_signal = thread.IsStopped(&stop_signo); 1178b9c1b51eSKate Stone const char *signal_name = stopped_by_signal 1179b9c1b51eSKate Stone ? Host::GetSignalAsCString(stop_signo) 1180b9c1b51eSKate Stone : "<not stopped by signal>"; 1181aab58633SChaoren Lin if (!signal_name) 1182aab58633SChaoren Lin signal_name = "<no-signal-name>"; 1183aab58633SChaoren Lin 1184b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 1185b9c1b51eSKate Stone ", thread was already marked as a stopped state (state=%s, " 1186b9c1b51eSKate Stone "signal=%d (%s)), leaving stop signal as is", 1187b9c1b51eSKate Stone __FUNCTION__, GetID(), thread.GetID(), 1188b9c1b51eSKate Stone StateAsCString(thread_state), stop_signo, signal_name); 1189aab58633SChaoren Lin } 11900e1d729bSPavel Labath SignalIfAllThreadsStopped(); 1191af245d11STodd Fiala } 1192af245d11STodd Fiala 119358a2f669STodd Fiala // Done handling. 1194af245d11STodd Fiala return; 1195af245d11STodd Fiala } 1196af245d11STodd Fiala 1197af245d11STodd Fiala if (log) 1198b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() received signal %s", __FUNCTION__, 1199b9c1b51eSKate Stone Host::GetSignalAsCString(signo)); 1200af245d11STodd Fiala 120186fd8e45SChaoren Lin // This thread is stopped. 1202b9cc0c75SPavel Labath thread.SetStoppedBySignal(signo, &info); 120386fd8e45SChaoren Lin 120486fd8e45SChaoren Lin // Send a stop to the debugger after we get all other threads to stop. 1205b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 1206511e5cdcSTodd Fiala } 1207af245d11STodd Fiala 1208e7708688STamas Berghammer namespace { 1209e7708688STamas Berghammer 1210b9c1b51eSKate Stone struct EmulatorBaton { 1211e7708688STamas Berghammer NativeProcessLinux *m_process; 1212e7708688STamas Berghammer NativeRegisterContext *m_reg_context; 12136648fcc3SPavel Labath 12146648fcc3SPavel Labath // eRegisterKindDWARF -> RegsiterValue 12156648fcc3SPavel Labath std::unordered_map<uint32_t, RegisterValue> m_register_values; 1216e7708688STamas Berghammer 1217b9c1b51eSKate Stone EmulatorBaton(NativeProcessLinux *process, NativeRegisterContext *reg_context) 1218b9c1b51eSKate Stone : m_process(process), m_reg_context(reg_context) {} 1219e7708688STamas Berghammer }; 1220e7708688STamas Berghammer 1221e7708688STamas Berghammer } // anonymous namespace 1222e7708688STamas Berghammer 1223b9c1b51eSKate Stone static size_t ReadMemoryCallback(EmulateInstruction *instruction, void *baton, 1224e7708688STamas Berghammer const EmulateInstruction::Context &context, 1225b9c1b51eSKate Stone lldb::addr_t addr, void *dst, size_t length) { 1226e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 1227e7708688STamas Berghammer 12283eb4b458SChaoren Lin size_t bytes_read; 1229e7708688STamas Berghammer emulator_baton->m_process->ReadMemory(addr, dst, length, bytes_read); 1230e7708688STamas Berghammer return bytes_read; 1231e7708688STamas Berghammer } 1232e7708688STamas Berghammer 1233b9c1b51eSKate Stone static bool ReadRegisterCallback(EmulateInstruction *instruction, void *baton, 1234e7708688STamas Berghammer const RegisterInfo *reg_info, 1235b9c1b51eSKate Stone RegisterValue ®_value) { 1236e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 1237e7708688STamas Berghammer 1238b9c1b51eSKate Stone auto it = emulator_baton->m_register_values.find( 1239b9c1b51eSKate Stone reg_info->kinds[eRegisterKindDWARF]); 1240b9c1b51eSKate Stone if (it != emulator_baton->m_register_values.end()) { 12416648fcc3SPavel Labath reg_value = it->second; 12426648fcc3SPavel Labath return true; 12436648fcc3SPavel Labath } 12446648fcc3SPavel Labath 1245e7708688STamas Berghammer // The emulator only fill in the dwarf regsiter numbers (and in some case 1246e7708688STamas Berghammer // the generic register numbers). Get the full register info from the 1247e7708688STamas Berghammer // register context based on the dwarf register numbers. 1248b9c1b51eSKate Stone const RegisterInfo *full_reg_info = 1249b9c1b51eSKate Stone emulator_baton->m_reg_context->GetRegisterInfo( 1250e7708688STamas Berghammer eRegisterKindDWARF, reg_info->kinds[eRegisterKindDWARF]); 1251e7708688STamas Berghammer 1252b9c1b51eSKate Stone Error error = 1253b9c1b51eSKate Stone emulator_baton->m_reg_context->ReadRegister(full_reg_info, reg_value); 12546648fcc3SPavel Labath if (error.Success()) 12556648fcc3SPavel Labath return true; 1256cdc22a88SMohit K. Bhakkad 12576648fcc3SPavel Labath return false; 1258e7708688STamas Berghammer } 1259e7708688STamas Berghammer 1260b9c1b51eSKate Stone static bool WriteRegisterCallback(EmulateInstruction *instruction, void *baton, 1261e7708688STamas Berghammer const EmulateInstruction::Context &context, 1262e7708688STamas Berghammer const RegisterInfo *reg_info, 1263b9c1b51eSKate Stone const RegisterValue ®_value) { 1264e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 1265b9c1b51eSKate Stone emulator_baton->m_register_values[reg_info->kinds[eRegisterKindDWARF]] = 1266b9c1b51eSKate Stone reg_value; 1267e7708688STamas Berghammer return true; 1268e7708688STamas Berghammer } 1269e7708688STamas Berghammer 1270b9c1b51eSKate Stone static size_t WriteMemoryCallback(EmulateInstruction *instruction, void *baton, 1271e7708688STamas Berghammer const EmulateInstruction::Context &context, 1272b9c1b51eSKate Stone lldb::addr_t addr, const void *dst, 1273b9c1b51eSKate Stone size_t length) { 1274e7708688STamas Berghammer return length; 1275e7708688STamas Berghammer } 1276e7708688STamas Berghammer 1277b9c1b51eSKate Stone static lldb::addr_t ReadFlags(NativeRegisterContext *regsiter_context) { 1278e7708688STamas Berghammer const RegisterInfo *flags_info = regsiter_context->GetRegisterInfo( 1279e7708688STamas Berghammer eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 1280b9c1b51eSKate Stone return regsiter_context->ReadRegisterAsUnsigned(flags_info, 1281b9c1b51eSKate Stone LLDB_INVALID_ADDRESS); 1282e7708688STamas Berghammer } 1283e7708688STamas Berghammer 1284b9c1b51eSKate Stone Error NativeProcessLinux::SetupSoftwareSingleStepping( 1285b9c1b51eSKate Stone NativeThreadLinux &thread) { 1286e7708688STamas Berghammer Error error; 1287b9cc0c75SPavel Labath NativeRegisterContextSP register_context_sp = thread.GetRegisterContext(); 1288e7708688STamas Berghammer 1289e7708688STamas Berghammer std::unique_ptr<EmulateInstruction> emulator_ap( 1290b9c1b51eSKate Stone EmulateInstruction::FindPlugin(m_arch, eInstructionTypePCModifying, 1291b9c1b51eSKate Stone nullptr)); 1292e7708688STamas Berghammer 1293e7708688STamas Berghammer if (emulator_ap == nullptr) 1294e7708688STamas Berghammer return Error("Instruction emulator not found!"); 1295e7708688STamas Berghammer 1296e7708688STamas Berghammer EmulatorBaton baton(this, register_context_sp.get()); 1297e7708688STamas Berghammer emulator_ap->SetBaton(&baton); 1298e7708688STamas Berghammer emulator_ap->SetReadMemCallback(&ReadMemoryCallback); 1299e7708688STamas Berghammer emulator_ap->SetReadRegCallback(&ReadRegisterCallback); 1300e7708688STamas Berghammer emulator_ap->SetWriteMemCallback(&WriteMemoryCallback); 1301e7708688STamas Berghammer emulator_ap->SetWriteRegCallback(&WriteRegisterCallback); 1302e7708688STamas Berghammer 1303e7708688STamas Berghammer if (!emulator_ap->ReadInstruction()) 1304e7708688STamas Berghammer return Error("Read instruction failed!"); 1305e7708688STamas Berghammer 1306b9c1b51eSKate Stone bool emulation_result = 1307b9c1b51eSKate Stone emulator_ap->EvaluateInstruction(eEmulateInstructionOptionAutoAdvancePC); 13086648fcc3SPavel Labath 1309b9c1b51eSKate Stone const RegisterInfo *reg_info_pc = register_context_sp->GetRegisterInfo( 1310b9c1b51eSKate Stone eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); 1311b9c1b51eSKate Stone const RegisterInfo *reg_info_flags = register_context_sp->GetRegisterInfo( 1312b9c1b51eSKate Stone eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 13136648fcc3SPavel Labath 1314b9c1b51eSKate Stone auto pc_it = 1315b9c1b51eSKate Stone baton.m_register_values.find(reg_info_pc->kinds[eRegisterKindDWARF]); 1316b9c1b51eSKate Stone auto flags_it = 1317b9c1b51eSKate Stone baton.m_register_values.find(reg_info_flags->kinds[eRegisterKindDWARF]); 13186648fcc3SPavel Labath 1319e7708688STamas Berghammer lldb::addr_t next_pc; 1320e7708688STamas Berghammer lldb::addr_t next_flags; 1321b9c1b51eSKate Stone if (emulation_result) { 1322b9c1b51eSKate Stone assert(pc_it != baton.m_register_values.end() && 1323b9c1b51eSKate Stone "Emulation was successfull but PC wasn't updated"); 13246648fcc3SPavel Labath next_pc = pc_it->second.GetAsUInt64(); 13256648fcc3SPavel Labath 13266648fcc3SPavel Labath if (flags_it != baton.m_register_values.end()) 13276648fcc3SPavel Labath next_flags = flags_it->second.GetAsUInt64(); 1328e7708688STamas Berghammer else 1329e7708688STamas Berghammer next_flags = ReadFlags(register_context_sp.get()); 1330b9c1b51eSKate Stone } else if (pc_it == baton.m_register_values.end()) { 1331e7708688STamas Berghammer // Emulate instruction failed and it haven't changed PC. Advance PC 1332e7708688STamas Berghammer // with the size of the current opcode because the emulation of all 1333e7708688STamas Berghammer // PC modifying instruction should be successful. The failure most 1334e7708688STamas Berghammer // likely caused by a not supported instruction which don't modify PC. 1335b9c1b51eSKate Stone next_pc = 1336b9c1b51eSKate Stone register_context_sp->GetPC() + emulator_ap->GetOpcode().GetByteSize(); 1337e7708688STamas Berghammer next_flags = ReadFlags(register_context_sp.get()); 1338b9c1b51eSKate Stone } else { 1339e7708688STamas Berghammer // The instruction emulation failed after it modified the PC. It is an 1340e7708688STamas Berghammer // unknown error where we can't continue because the next instruction is 1341e7708688STamas Berghammer // modifying the PC but we don't know how. 1342e7708688STamas Berghammer return Error("Instruction emulation failed unexpectedly."); 1343e7708688STamas Berghammer } 1344e7708688STamas Berghammer 1345b9c1b51eSKate Stone if (m_arch.GetMachine() == llvm::Triple::arm) { 1346b9c1b51eSKate Stone if (next_flags & 0x20) { 1347e7708688STamas Berghammer // Thumb mode 1348e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 2); 1349b9c1b51eSKate Stone } else { 1350e7708688STamas Berghammer // Arm mode 1351e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 4); 1352e7708688STamas Berghammer } 1353b9c1b51eSKate Stone } else if (m_arch.GetMachine() == llvm::Triple::mips64 || 1354b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64el || 1355b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips || 1356b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mipsel) 1357cdc22a88SMohit K. Bhakkad error = SetSoftwareBreakpoint(next_pc, 4); 1358b9c1b51eSKate Stone else { 1359e7708688STamas Berghammer // No size hint is given for the next breakpoint 1360e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 0); 1361e7708688STamas Berghammer } 1362e7708688STamas Berghammer 136342eb6908SPavel Labath // If setting the breakpoint fails because next_pc is out of 136442eb6908SPavel Labath // the address space, ignore it and let the debugee segfault. 136542eb6908SPavel Labath if (error.GetError() == EIO || error.GetError() == EFAULT) { 1366665be50eSMehdi Amini return Error(); 136742eb6908SPavel Labath } else if (error.Fail()) 1368e7708688STamas Berghammer return error; 1369e7708688STamas Berghammer 1370b9cc0c75SPavel Labath m_threads_stepping_with_breakpoint.insert({thread.GetID(), next_pc}); 1371e7708688STamas Berghammer 1372665be50eSMehdi Amini return Error(); 1373e7708688STamas Berghammer } 1374e7708688STamas Berghammer 1375b9c1b51eSKate Stone bool NativeProcessLinux::SupportHardwareSingleStepping() const { 1376b9c1b51eSKate Stone if (m_arch.GetMachine() == llvm::Triple::arm || 1377b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64 || 1378b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64el || 1379b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips || 1380b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mipsel) 1381cdc22a88SMohit K. Bhakkad return false; 1382cdc22a88SMohit K. Bhakkad return true; 1383e7708688STamas Berghammer } 1384e7708688STamas Berghammer 1385b9c1b51eSKate Stone Error NativeProcessLinux::Resume(const ResumeActionList &resume_actions) { 1386af245d11STodd Fiala Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); 1387af245d11STodd Fiala if (log) 1388b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s called: pid %" PRIu64, __FUNCTION__, 1389b9c1b51eSKate Stone GetID()); 1390af245d11STodd Fiala 1391e7708688STamas Berghammer bool software_single_step = !SupportHardwareSingleStepping(); 1392af245d11STodd Fiala 1393b9c1b51eSKate Stone if (software_single_step) { 1394b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 1395e7708688STamas Berghammer assert(thread_sp && "thread list should not contain NULL threads"); 1396e7708688STamas Berghammer 1397b9c1b51eSKate Stone const ResumeAction *const action = 1398b9c1b51eSKate Stone resume_actions.GetActionForThread(thread_sp->GetID(), true); 1399e7708688STamas Berghammer if (action == nullptr) 1400e7708688STamas Berghammer continue; 1401e7708688STamas Berghammer 1402b9c1b51eSKate Stone if (action->state == eStateStepping) { 1403b9c1b51eSKate Stone Error error = SetupSoftwareSingleStepping( 1404b9c1b51eSKate Stone static_cast<NativeThreadLinux &>(*thread_sp)); 1405e7708688STamas Berghammer if (error.Fail()) 1406e7708688STamas Berghammer return error; 1407e7708688STamas Berghammer } 1408e7708688STamas Berghammer } 1409e7708688STamas Berghammer } 1410e7708688STamas Berghammer 1411b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 1412af245d11STodd Fiala assert(thread_sp && "thread list should not contain NULL threads"); 1413af245d11STodd Fiala 1414b9c1b51eSKate Stone const ResumeAction *const action = 1415b9c1b51eSKate Stone resume_actions.GetActionForThread(thread_sp->GetID(), true); 14166a196ce6SChaoren Lin 1417b9c1b51eSKate Stone if (action == nullptr) { 14186a196ce6SChaoren Lin if (log) 1419b9c1b51eSKate Stone log->Printf( 1420b9c1b51eSKate Stone "NativeProcessLinux::%s no action specified for pid %" PRIu64 1421b9c1b51eSKate Stone " tid %" PRIu64, 14226a196ce6SChaoren Lin __FUNCTION__, GetID(), thread_sp->GetID()); 14236a196ce6SChaoren Lin continue; 14246a196ce6SChaoren Lin } 1425af245d11STodd Fiala 1426b9c1b51eSKate Stone if (log) { 1427b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s processing resume action state %s " 1428b9c1b51eSKate Stone "for pid %" PRIu64 " tid %" PRIu64, 1429b9c1b51eSKate Stone __FUNCTION__, StateAsCString(action->state), GetID(), 1430b9c1b51eSKate Stone thread_sp->GetID()); 1431af245d11STodd Fiala } 1432af245d11STodd Fiala 1433b9c1b51eSKate Stone switch (action->state) { 1434af245d11STodd Fiala case eStateRunning: 1435b9c1b51eSKate Stone case eStateStepping: { 1436af245d11STodd Fiala // Run the thread, possibly feeding it the signal. 1437fa03ad2eSChaoren Lin const int signo = action->signal; 1438b9c1b51eSKate Stone ResumeThread(static_cast<NativeThreadLinux &>(*thread_sp), action->state, 1439b9c1b51eSKate Stone signo); 1440af245d11STodd Fiala break; 1441ae29d395SChaoren Lin } 1442af245d11STodd Fiala 1443af245d11STodd Fiala case eStateSuspended: 1444af245d11STodd Fiala case eStateStopped: 1445108c325dSPavel Labath lldbassert(0 && "Unexpected state"); 1446af245d11STodd Fiala 1447af245d11STodd Fiala default: 1448b9c1b51eSKate Stone return Error("NativeProcessLinux::%s (): unexpected state %s specified " 1449b9c1b51eSKate Stone "for pid %" PRIu64 ", tid %" PRIu64, 1450b9c1b51eSKate Stone __FUNCTION__, StateAsCString(action->state), GetID(), 1451b9c1b51eSKate Stone thread_sp->GetID()); 1452af245d11STodd Fiala } 1453af245d11STodd Fiala } 1454af245d11STodd Fiala 1455665be50eSMehdi Amini return Error(); 1456af245d11STodd Fiala } 1457af245d11STodd Fiala 1458b9c1b51eSKate Stone Error NativeProcessLinux::Halt() { 1459af245d11STodd Fiala Error error; 1460af245d11STodd Fiala 1461af245d11STodd Fiala if (kill(GetID(), SIGSTOP) != 0) 1462af245d11STodd Fiala error.SetErrorToErrno(); 1463af245d11STodd Fiala 1464af245d11STodd Fiala return error; 1465af245d11STodd Fiala } 1466af245d11STodd Fiala 1467b9c1b51eSKate Stone Error NativeProcessLinux::Detach() { 1468af245d11STodd Fiala Error error; 1469af245d11STodd Fiala 1470af245d11STodd Fiala // Stop monitoring the inferior. 147119cbe96aSPavel Labath m_sigchld_handle.reset(); 1472af245d11STodd Fiala 14737a9495bcSPavel Labath // Tell ptrace to detach from the process. 14747a9495bcSPavel Labath if (GetID() == LLDB_INVALID_PROCESS_ID) 14757a9495bcSPavel Labath return error; 14767a9495bcSPavel Labath 1477b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 14787a9495bcSPavel Labath Error e = Detach(thread_sp->GetID()); 14797a9495bcSPavel Labath if (e.Fail()) 1480b9c1b51eSKate Stone error = 1481b9c1b51eSKate Stone e; // Save the error, but still attempt to detach from other threads. 14827a9495bcSPavel Labath } 14837a9495bcSPavel Labath 1484af245d11STodd Fiala return error; 1485af245d11STodd Fiala } 1486af245d11STodd Fiala 1487b9c1b51eSKate Stone Error NativeProcessLinux::Signal(int signo) { 1488af245d11STodd Fiala Error error; 1489af245d11STodd Fiala 1490af245d11STodd Fiala Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 1491af245d11STodd Fiala if (log) 1492b9c1b51eSKate Stone log->Printf( 1493b9c1b51eSKate Stone "NativeProcessLinux::%s: sending signal %d (%s) to pid %" PRIu64, 149498d0a4b3SChaoren Lin __FUNCTION__, signo, Host::GetSignalAsCString(signo), GetID()); 1495af245d11STodd Fiala 1496af245d11STodd Fiala if (kill(GetID(), signo)) 1497af245d11STodd Fiala error.SetErrorToErrno(); 1498af245d11STodd Fiala 1499af245d11STodd Fiala return error; 1500af245d11STodd Fiala } 1501af245d11STodd Fiala 1502b9c1b51eSKate Stone Error NativeProcessLinux::Interrupt() { 1503e9547b80SChaoren Lin // Pick a running thread (or if none, a not-dead stopped thread) as 1504e9547b80SChaoren Lin // the chosen thread that will be the stop-reason thread. 1505e9547b80SChaoren Lin Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 1506e9547b80SChaoren Lin 1507e9547b80SChaoren Lin NativeThreadProtocolSP running_thread_sp; 1508e9547b80SChaoren Lin NativeThreadProtocolSP stopped_thread_sp; 1509e9547b80SChaoren Lin 1510e9547b80SChaoren Lin if (log) 1511b9c1b51eSKate Stone log->Printf( 1512b9c1b51eSKate Stone "NativeProcessLinux::%s selecting running thread for interrupt target", 1513b9c1b51eSKate Stone __FUNCTION__); 1514e9547b80SChaoren Lin 1515b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 1516e9547b80SChaoren Lin // The thread shouldn't be null but lets just cover that here. 1517e9547b80SChaoren Lin if (!thread_sp) 1518e9547b80SChaoren Lin continue; 1519e9547b80SChaoren Lin 1520e9547b80SChaoren Lin // If we have a running or stepping thread, we'll call that the 1521e9547b80SChaoren Lin // target of the interrupt. 1522e9547b80SChaoren Lin const auto thread_state = thread_sp->GetState(); 1523b9c1b51eSKate Stone if (thread_state == eStateRunning || thread_state == eStateStepping) { 1524e9547b80SChaoren Lin running_thread_sp = thread_sp; 1525e9547b80SChaoren Lin break; 1526b9c1b51eSKate Stone } else if (!stopped_thread_sp && StateIsStoppedState(thread_state, true)) { 1527b9c1b51eSKate Stone // Remember the first non-dead stopped thread. We'll use that as a backup 1528b9c1b51eSKate Stone // if there are no running threads. 1529e9547b80SChaoren Lin stopped_thread_sp = thread_sp; 1530e9547b80SChaoren Lin } 1531e9547b80SChaoren Lin } 1532e9547b80SChaoren Lin 1533b9c1b51eSKate Stone if (!running_thread_sp && !stopped_thread_sp) { 1534b9c1b51eSKate Stone Error error("found no running/stepping or live stopped threads as target " 1535b9c1b51eSKate Stone "for interrupt"); 1536e9547b80SChaoren Lin if (log) 1537b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s skipping due to error: %s", 1538b9c1b51eSKate Stone __FUNCTION__, error.AsCString()); 15395830aa75STamas Berghammer 1540e9547b80SChaoren Lin return error; 1541e9547b80SChaoren Lin } 1542e9547b80SChaoren Lin 1543b9c1b51eSKate Stone NativeThreadProtocolSP deferred_signal_thread_sp = 1544b9c1b51eSKate Stone running_thread_sp ? running_thread_sp : stopped_thread_sp; 1545e9547b80SChaoren Lin 1546e9547b80SChaoren Lin if (log) 1547b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s pid %" PRIu64 " %s tid %" PRIu64 1548b9c1b51eSKate Stone " chosen for interrupt target", 1549b9c1b51eSKate Stone __FUNCTION__, GetID(), 1550e9547b80SChaoren Lin running_thread_sp ? "running" : "stopped", 1551e9547b80SChaoren Lin deferred_signal_thread_sp->GetID()); 1552e9547b80SChaoren Lin 1553ed89c7feSPavel Labath StopRunningThreads(deferred_signal_thread_sp->GetID()); 155445f5cb31SPavel Labath 1555665be50eSMehdi Amini return Error(); 1556e9547b80SChaoren Lin } 1557e9547b80SChaoren Lin 1558b9c1b51eSKate Stone Error NativeProcessLinux::Kill() { 1559af245d11STodd Fiala Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 1560af245d11STodd Fiala if (log) 1561b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s called for PID %" PRIu64, __FUNCTION__, 1562b9c1b51eSKate Stone GetID()); 1563af245d11STodd Fiala 1564af245d11STodd Fiala Error error; 1565af245d11STodd Fiala 1566b9c1b51eSKate Stone switch (m_state) { 1567af245d11STodd Fiala case StateType::eStateInvalid: 1568af245d11STodd Fiala case StateType::eStateExited: 1569af245d11STodd Fiala case StateType::eStateCrashed: 1570af245d11STodd Fiala case StateType::eStateDetached: 1571af245d11STodd Fiala case StateType::eStateUnloaded: 1572af245d11STodd Fiala // Nothing to do - the process is already dead. 1573af245d11STodd Fiala if (log) 1574b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s ignored for PID %" PRIu64 1575b9c1b51eSKate Stone " due to current state: %s", 1576b9c1b51eSKate Stone __FUNCTION__, GetID(), StateAsCString(m_state)); 1577af245d11STodd Fiala return error; 1578af245d11STodd Fiala 1579af245d11STodd Fiala case StateType::eStateConnected: 1580af245d11STodd Fiala case StateType::eStateAttaching: 1581af245d11STodd Fiala case StateType::eStateLaunching: 1582af245d11STodd Fiala case StateType::eStateStopped: 1583af245d11STodd Fiala case StateType::eStateRunning: 1584af245d11STodd Fiala case StateType::eStateStepping: 1585af245d11STodd Fiala case StateType::eStateSuspended: 1586af245d11STodd Fiala // We can try to kill a process in these states. 1587af245d11STodd Fiala break; 1588af245d11STodd Fiala } 1589af245d11STodd Fiala 1590b9c1b51eSKate Stone if (kill(GetID(), SIGKILL) != 0) { 1591af245d11STodd Fiala error.SetErrorToErrno(); 1592af245d11STodd Fiala return error; 1593af245d11STodd Fiala } 1594af245d11STodd Fiala 1595af245d11STodd Fiala return error; 1596af245d11STodd Fiala } 1597af245d11STodd Fiala 1598af245d11STodd Fiala static Error 1599b9c1b51eSKate Stone ParseMemoryRegionInfoFromProcMapsLine(const std::string &maps_line, 1600b9c1b51eSKate Stone MemoryRegionInfo &memory_region_info) { 1601af245d11STodd Fiala memory_region_info.Clear(); 1602af245d11STodd Fiala 1603b9739d40SPavel Labath StringExtractor line_extractor(maps_line.c_str()); 1604af245d11STodd Fiala 1605b9c1b51eSKate Stone // Format: {address_start_hex}-{address_end_hex} perms offset dev inode 1606b9c1b51eSKate Stone // pathname 1607b9c1b51eSKate Stone // perms: rwxp (letter is present if set, '-' if not, final character is 1608b9c1b51eSKate Stone // p=private, s=shared). 1609af245d11STodd Fiala 1610af245d11STodd Fiala // Parse out the starting address 1611af245d11STodd Fiala lldb::addr_t start_address = line_extractor.GetHexMaxU64(false, 0); 1612af245d11STodd Fiala 1613af245d11STodd Fiala // Parse out hyphen separating start and end address from range. 1614af245d11STodd Fiala if (!line_extractor.GetBytesLeft() || (line_extractor.GetChar() != '-')) 1615b9c1b51eSKate Stone return Error( 1616b9c1b51eSKate Stone "malformed /proc/{pid}/maps entry, missing dash between address range"); 1617af245d11STodd Fiala 1618af245d11STodd Fiala // Parse out the ending address 1619af245d11STodd Fiala lldb::addr_t end_address = line_extractor.GetHexMaxU64(false, start_address); 1620af245d11STodd Fiala 1621af245d11STodd Fiala // Parse out the space after the address. 1622af245d11STodd Fiala if (!line_extractor.GetBytesLeft() || (line_extractor.GetChar() != ' ')) 1623af245d11STodd Fiala return Error("malformed /proc/{pid}/maps entry, missing space after range"); 1624af245d11STodd Fiala 1625af245d11STodd Fiala // Save the range. 1626af245d11STodd Fiala memory_region_info.GetRange().SetRangeBase(start_address); 1627af245d11STodd Fiala memory_region_info.GetRange().SetRangeEnd(end_address); 1628af245d11STodd Fiala 1629b9c1b51eSKate Stone // Any memory region in /proc/{pid}/maps is by definition mapped into the 1630b9c1b51eSKate Stone // process. 1631ad007563SHoward Hellyer memory_region_info.SetMapped(MemoryRegionInfo::OptionalBool::eYes); 1632ad007563SHoward Hellyer 1633af245d11STodd Fiala // Parse out each permission entry. 1634af245d11STodd Fiala if (line_extractor.GetBytesLeft() < 4) 1635b9c1b51eSKate Stone return Error("malformed /proc/{pid}/maps entry, missing some portion of " 1636b9c1b51eSKate Stone "permissions"); 1637af245d11STodd Fiala 1638af245d11STodd Fiala // Handle read permission. 1639af245d11STodd Fiala const char read_perm_char = line_extractor.GetChar(); 1640af245d11STodd Fiala if (read_perm_char == 'r') 1641af245d11STodd Fiala memory_region_info.SetReadable(MemoryRegionInfo::OptionalBool::eYes); 1642c73301bbSTamas Berghammer else if (read_perm_char == '-') 1643af245d11STodd Fiala memory_region_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 1644c73301bbSTamas Berghammer else 1645c73301bbSTamas Berghammer return Error("unexpected /proc/{pid}/maps read permission char"); 1646af245d11STodd Fiala 1647af245d11STodd Fiala // Handle write permission. 1648af245d11STodd Fiala const char write_perm_char = line_extractor.GetChar(); 1649af245d11STodd Fiala if (write_perm_char == 'w') 1650af245d11STodd Fiala memory_region_info.SetWritable(MemoryRegionInfo::OptionalBool::eYes); 1651c73301bbSTamas Berghammer else if (write_perm_char == '-') 1652af245d11STodd Fiala memory_region_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 1653c73301bbSTamas Berghammer else 1654c73301bbSTamas Berghammer return Error("unexpected /proc/{pid}/maps write permission char"); 1655af245d11STodd Fiala 1656af245d11STodd Fiala // Handle execute permission. 1657af245d11STodd Fiala const char exec_perm_char = line_extractor.GetChar(); 1658af245d11STodd Fiala if (exec_perm_char == 'x') 1659af245d11STodd Fiala memory_region_info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes); 1660c73301bbSTamas Berghammer else if (exec_perm_char == '-') 1661af245d11STodd Fiala memory_region_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1662c73301bbSTamas Berghammer else 1663c73301bbSTamas Berghammer return Error("unexpected /proc/{pid}/maps exec permission char"); 1664af245d11STodd Fiala 1665d7d69f80STamas Berghammer line_extractor.GetChar(); // Read the private bit 1666d7d69f80STamas Berghammer line_extractor.SkipSpaces(); // Skip the separator 1667d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the offset 1668d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the major device number 1669d7d69f80STamas Berghammer line_extractor.GetChar(); // Read the device id separator 1670d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the major device number 1671d7d69f80STamas Berghammer line_extractor.SkipSpaces(); // Skip the separator 1672d7d69f80STamas Berghammer line_extractor.GetU64(0, 10); // Read the inode number 1673d7d69f80STamas Berghammer 1674d7d69f80STamas Berghammer line_extractor.SkipSpaces(); 1675b9739d40SPavel Labath const char *name = line_extractor.Peek(); 1676b9739d40SPavel Labath if (name) 1677b9739d40SPavel Labath memory_region_info.SetName(name); 1678d7d69f80STamas Berghammer 1679665be50eSMehdi Amini return Error(); 1680af245d11STodd Fiala } 1681af245d11STodd Fiala 1682b9c1b51eSKate Stone Error NativeProcessLinux::GetMemoryRegionInfo(lldb::addr_t load_addr, 1683b9c1b51eSKate Stone MemoryRegionInfo &range_info) { 1684b9c1b51eSKate Stone // FIXME review that the final memory region returned extends to the end of 1685b9c1b51eSKate Stone // the virtual address space, 1686af245d11STodd Fiala // with no perms if it is not mapped. 1687af245d11STodd Fiala 1688af245d11STodd Fiala // Use an approach that reads memory regions from /proc/{pid}/maps. 1689af245d11STodd Fiala // Assume proc maps entries are in ascending order. 1690af245d11STodd Fiala // FIXME assert if we find differently. 1691af245d11STodd Fiala 1692b9c1b51eSKate Stone if (m_supports_mem_region == LazyBool::eLazyBoolNo) { 1693af245d11STodd Fiala // We're done. 1694*a6f5795aSTamas Berghammer return Error("unsupported"); 1695af245d11STodd Fiala } 1696af245d11STodd Fiala 1697*a6f5795aSTamas Berghammer Error error = PopulateMemoryRegionCache(); 1698b9c1b51eSKate Stone if (error.Fail()) { 1699af245d11STodd Fiala return error; 1700af245d11STodd Fiala } 1701af245d11STodd Fiala 1702af245d11STodd Fiala lldb::addr_t prev_base_address = 0; 1703af245d11STodd Fiala 1704b9c1b51eSKate Stone // FIXME start by finding the last region that is <= target address using 1705b9c1b51eSKate Stone // binary search. Data is sorted. 1706af245d11STodd Fiala // There can be a ton of regions on pthreads apps with lots of threads. 1707b9c1b51eSKate Stone for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end(); 1708b9c1b51eSKate Stone ++it) { 1709*a6f5795aSTamas Berghammer MemoryRegionInfo &proc_entry_info = it->first; 1710af245d11STodd Fiala 1711af245d11STodd Fiala // Sanity check assumption that /proc/{pid}/maps entries are ascending. 1712b9c1b51eSKate Stone assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && 1713b9c1b51eSKate Stone "descending /proc/pid/maps entries detected, unexpected"); 1714af245d11STodd Fiala prev_base_address = proc_entry_info.GetRange().GetRangeBase(); 1715af245d11STodd Fiala 1716b9c1b51eSKate Stone // If the target address comes before this entry, indicate distance to next 1717b9c1b51eSKate Stone // region. 1718b9c1b51eSKate Stone if (load_addr < proc_entry_info.GetRange().GetRangeBase()) { 1719af245d11STodd Fiala range_info.GetRange().SetRangeBase(load_addr); 1720b9c1b51eSKate Stone range_info.GetRange().SetByteSize( 1721b9c1b51eSKate Stone proc_entry_info.GetRange().GetRangeBase() - load_addr); 1722af245d11STodd Fiala range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 1723af245d11STodd Fiala range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 1724af245d11STodd Fiala range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1725ad007563SHoward Hellyer range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 1726af245d11STodd Fiala 1727af245d11STodd Fiala return error; 1728b9c1b51eSKate Stone } else if (proc_entry_info.GetRange().Contains(load_addr)) { 1729af245d11STodd Fiala // The target address is within the memory region we're processing here. 1730af245d11STodd Fiala range_info = proc_entry_info; 1731af245d11STodd Fiala return error; 1732af245d11STodd Fiala } 1733af245d11STodd Fiala 1734b9c1b51eSKate Stone // The target memory address comes somewhere after the region we just 1735b9c1b51eSKate Stone // parsed. 1736af245d11STodd Fiala } 1737af245d11STodd Fiala 1738b9c1b51eSKate Stone // If we made it here, we didn't find an entry that contained the given 1739b9c1b51eSKate Stone // address. Return the 1740b9c1b51eSKate Stone // load_addr as start and the amount of bytes betwwen load address and the end 1741b9c1b51eSKate Stone // of the memory as 174209839c33STamas Berghammer // size. 174309839c33STamas Berghammer range_info.GetRange().SetRangeBase(load_addr); 1744ad007563SHoward Hellyer range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); 174509839c33STamas Berghammer range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 174609839c33STamas Berghammer range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 174709839c33STamas Berghammer range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1748ad007563SHoward Hellyer range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 1749af245d11STodd Fiala return error; 1750af245d11STodd Fiala } 1751af245d11STodd Fiala 1752*a6f5795aSTamas Berghammer Error NativeProcessLinux::PopulateMemoryRegionCache() { 1753*a6f5795aSTamas Berghammer Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 1754*a6f5795aSTamas Berghammer 1755*a6f5795aSTamas Berghammer // If our cache is empty, pull the latest. There should always be at least 1756*a6f5795aSTamas Berghammer // one memory region if memory region handling is supported. 1757*a6f5795aSTamas Berghammer if (!m_mem_region_cache.empty()) { 1758*a6f5795aSTamas Berghammer if (log) 1759*a6f5795aSTamas Berghammer log->Printf("NativeProcessLinux::%s reusing %" PRIu64 1760*a6f5795aSTamas Berghammer " cached memory region entries", 1761*a6f5795aSTamas Berghammer __FUNCTION__, 1762*a6f5795aSTamas Berghammer static_cast<uint64_t>(m_mem_region_cache.size())); 1763*a6f5795aSTamas Berghammer return Error(); 1764*a6f5795aSTamas Berghammer } 1765*a6f5795aSTamas Berghammer 1766*a6f5795aSTamas Berghammer Error error = ProcFileReader::ProcessLineByLine( 1767*a6f5795aSTamas Berghammer GetID(), "maps", [&](const std::string &line) -> bool { 1768*a6f5795aSTamas Berghammer MemoryRegionInfo info; 1769*a6f5795aSTamas Berghammer const Error parse_error = 1770*a6f5795aSTamas Berghammer ParseMemoryRegionInfoFromProcMapsLine(line, info); 1771*a6f5795aSTamas Berghammer if (parse_error.Success()) { 1772*a6f5795aSTamas Berghammer m_mem_region_cache.emplace_back( 1773*a6f5795aSTamas Berghammer info, FileSpec(info.GetName().GetCString(), true)); 1774*a6f5795aSTamas Berghammer return true; 1775*a6f5795aSTamas Berghammer } else { 1776*a6f5795aSTamas Berghammer if (log) 1777*a6f5795aSTamas Berghammer log->Printf("NativeProcessLinux::%s failed to parse proc maps " 1778*a6f5795aSTamas Berghammer "line '%s': %s", 1779*a6f5795aSTamas Berghammer __FUNCTION__, line.c_str(), parse_error.AsCString()); 1780*a6f5795aSTamas Berghammer return false; 1781*a6f5795aSTamas Berghammer } 1782*a6f5795aSTamas Berghammer }); 1783*a6f5795aSTamas Berghammer 1784*a6f5795aSTamas Berghammer // If we had an error, we'll mark unsupported. 1785*a6f5795aSTamas Berghammer if (error.Fail()) { 1786*a6f5795aSTamas Berghammer m_supports_mem_region = LazyBool::eLazyBoolNo; 1787*a6f5795aSTamas Berghammer return error; 1788*a6f5795aSTamas Berghammer } else if (m_mem_region_cache.empty()) { 1789*a6f5795aSTamas Berghammer // No entries after attempting to read them. This shouldn't happen if 1790*a6f5795aSTamas Berghammer // /proc/{pid}/maps is supported. Assume we don't support map entries 1791*a6f5795aSTamas Berghammer // via procfs. 1792*a6f5795aSTamas Berghammer if (log) 1793*a6f5795aSTamas Berghammer log->Printf("NativeProcessLinux::%s failed to find any procfs maps " 1794*a6f5795aSTamas Berghammer "entries, assuming no support for memory region metadata " 1795*a6f5795aSTamas Berghammer "retrieval", 1796*a6f5795aSTamas Berghammer __FUNCTION__); 1797*a6f5795aSTamas Berghammer m_supports_mem_region = LazyBool::eLazyBoolNo; 1798*a6f5795aSTamas Berghammer error.SetErrorString("not supported"); 1799*a6f5795aSTamas Berghammer return error; 1800*a6f5795aSTamas Berghammer } 1801*a6f5795aSTamas Berghammer 1802*a6f5795aSTamas Berghammer if (log) 1803*a6f5795aSTamas Berghammer log->Printf("NativeProcessLinux::%s read %" PRIu64 1804*a6f5795aSTamas Berghammer " memory region entries from /proc/%" PRIu64 "/maps", 1805*a6f5795aSTamas Berghammer __FUNCTION__, static_cast<uint64_t>(m_mem_region_cache.size()), 1806*a6f5795aSTamas Berghammer GetID()); 1807*a6f5795aSTamas Berghammer 1808*a6f5795aSTamas Berghammer // We support memory retrieval, remember that. 1809*a6f5795aSTamas Berghammer m_supports_mem_region = LazyBool::eLazyBoolYes; 1810*a6f5795aSTamas Berghammer return Error(); 1811*a6f5795aSTamas Berghammer } 1812*a6f5795aSTamas Berghammer 1813b9c1b51eSKate Stone void NativeProcessLinux::DoStopIDBumped(uint32_t newBumpId) { 1814af245d11STodd Fiala Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 1815af245d11STodd Fiala if (log) 1816b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s(newBumpId=%" PRIu32 ") called", 1817b9c1b51eSKate Stone __FUNCTION__, newBumpId); 1818af245d11STodd Fiala 1819af245d11STodd Fiala if (log) 1820b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s clearing %" PRIu64 1821b9c1b51eSKate Stone " entries from the cache", 1822b9c1b51eSKate Stone __FUNCTION__, static_cast<uint64_t>(m_mem_region_cache.size())); 1823af245d11STodd Fiala m_mem_region_cache.clear(); 1824af245d11STodd Fiala } 1825af245d11STodd Fiala 1826b9c1b51eSKate Stone Error NativeProcessLinux::AllocateMemory(size_t size, uint32_t permissions, 1827b9c1b51eSKate Stone lldb::addr_t &addr) { 1828af245d11STodd Fiala // FIXME implementing this requires the equivalent of 1829af245d11STodd Fiala // InferiorCallPOSIX::InferiorCallMmap, which depends on 1830af245d11STodd Fiala // functional ThreadPlans working with Native*Protocol. 1831af245d11STodd Fiala #if 1 1832af245d11STodd Fiala return Error("not implemented yet"); 1833af245d11STodd Fiala #else 1834af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 1835af245d11STodd Fiala 1836af245d11STodd Fiala unsigned prot = 0; 1837af245d11STodd Fiala if (permissions & lldb::ePermissionsReadable) 1838af245d11STodd Fiala prot |= eMmapProtRead; 1839af245d11STodd Fiala if (permissions & lldb::ePermissionsWritable) 1840af245d11STodd Fiala prot |= eMmapProtWrite; 1841af245d11STodd Fiala if (permissions & lldb::ePermissionsExecutable) 1842af245d11STodd Fiala prot |= eMmapProtExec; 1843af245d11STodd Fiala 1844af245d11STodd Fiala // TODO implement this directly in NativeProcessLinux 1845af245d11STodd Fiala // (and lift to NativeProcessPOSIX if/when that class is 1846af245d11STodd Fiala // refactored out). 1847af245d11STodd Fiala if (InferiorCallMmap(this, addr, 0, size, prot, 1848af245d11STodd Fiala eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { 1849af245d11STodd Fiala m_addr_to_mmap_size[addr] = size; 1850665be50eSMehdi Amini return Error(); 1851af245d11STodd Fiala } else { 1852af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 1853b9c1b51eSKate Stone return Error("unable to allocate %" PRIu64 1854b9c1b51eSKate Stone " bytes of memory with permissions %s", 1855b9c1b51eSKate Stone size, GetPermissionsAsCString(permissions)); 1856af245d11STodd Fiala } 1857af245d11STodd Fiala #endif 1858af245d11STodd Fiala } 1859af245d11STodd Fiala 1860b9c1b51eSKate Stone Error NativeProcessLinux::DeallocateMemory(lldb::addr_t addr) { 1861af245d11STodd Fiala // FIXME see comments in AllocateMemory - required lower-level 1862af245d11STodd Fiala // bits not in place yet (ThreadPlans) 1863af245d11STodd Fiala return Error("not implemented"); 1864af245d11STodd Fiala } 1865af245d11STodd Fiala 1866b9c1b51eSKate Stone lldb::addr_t NativeProcessLinux::GetSharedLibraryInfoAddress() { 1867af245d11STodd Fiala // punt on this for now 1868af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 1869af245d11STodd Fiala } 1870af245d11STodd Fiala 1871b9c1b51eSKate Stone size_t NativeProcessLinux::UpdateThreads() { 1872af245d11STodd Fiala // The NativeProcessLinux monitoring threads are always up to date 1873af245d11STodd Fiala // with respect to thread state and they keep the thread list 1874af245d11STodd Fiala // populated properly. All this method needs to do is return the 1875af245d11STodd Fiala // thread count. 1876af245d11STodd Fiala return m_threads.size(); 1877af245d11STodd Fiala } 1878af245d11STodd Fiala 1879b9c1b51eSKate Stone bool NativeProcessLinux::GetArchitecture(ArchSpec &arch) const { 1880af245d11STodd Fiala arch = m_arch; 1881af245d11STodd Fiala return true; 1882af245d11STodd Fiala } 1883af245d11STodd Fiala 1884b9c1b51eSKate Stone Error NativeProcessLinux::GetSoftwareBreakpointPCOffset( 1885b9c1b51eSKate Stone uint32_t &actual_opcode_size) { 1886af245d11STodd Fiala // FIXME put this behind a breakpoint protocol class that can be 1887af245d11STodd Fiala // set per architecture. Need ARM, MIPS support here. 1888af245d11STodd Fiala static const uint8_t g_i386_opcode[] = {0xCC}; 1889bb00d0b6SUlrich Weigand static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; 1890af245d11STodd Fiala 1891b9c1b51eSKate Stone switch (m_arch.GetMachine()) { 1892af245d11STodd Fiala case llvm::Triple::x86: 1893af245d11STodd Fiala case llvm::Triple::x86_64: 1894af245d11STodd Fiala actual_opcode_size = static_cast<uint32_t>(sizeof(g_i386_opcode)); 1895665be50eSMehdi Amini return Error(); 1896af245d11STodd Fiala 1897bb00d0b6SUlrich Weigand case llvm::Triple::systemz: 1898bb00d0b6SUlrich Weigand actual_opcode_size = static_cast<uint32_t>(sizeof(g_s390x_opcode)); 1899665be50eSMehdi Amini return Error(); 1900bb00d0b6SUlrich Weigand 1901ff7fd900STamas Berghammer case llvm::Triple::arm: 1902ff7fd900STamas Berghammer case llvm::Triple::aarch64: 1903e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64: 1904e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64el: 1905ce815e45SSagar Thakur case llvm::Triple::mips: 1906ce815e45SSagar Thakur case llvm::Triple::mipsel: 1907ff7fd900STamas Berghammer // On these architectures the PC don't get updated for breakpoint hits 1908c60c9452SJaydeep Patil actual_opcode_size = 0; 1909665be50eSMehdi Amini return Error(); 1910e8659b5dSMohit K. Bhakkad 1911af245d11STodd Fiala default: 1912af245d11STodd Fiala assert(false && "CPU type not supported!"); 1913af245d11STodd Fiala return Error("CPU type not supported"); 1914af245d11STodd Fiala } 1915af245d11STodd Fiala } 1916af245d11STodd Fiala 1917b9c1b51eSKate Stone Error NativeProcessLinux::SetBreakpoint(lldb::addr_t addr, uint32_t size, 1918b9c1b51eSKate Stone bool hardware) { 1919af245d11STodd Fiala if (hardware) 1920af245d11STodd Fiala return Error("NativeProcessLinux does not support hardware breakpoints"); 1921af245d11STodd Fiala else 1922af245d11STodd Fiala return SetSoftwareBreakpoint(addr, size); 1923af245d11STodd Fiala } 1924af245d11STodd Fiala 1925b9c1b51eSKate Stone Error NativeProcessLinux::GetSoftwareBreakpointTrapOpcode( 1926b9c1b51eSKate Stone size_t trap_opcode_size_hint, size_t &actual_opcode_size, 1927b9c1b51eSKate Stone const uint8_t *&trap_opcode_bytes) { 192863c8be95STamas Berghammer // FIXME put this behind a breakpoint protocol class that can be set per 192963c8be95STamas Berghammer // architecture. Need MIPS support here. 19302afc5966STodd Fiala static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4}; 1931be379e15STamas Berghammer // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the 1932be379e15STamas Berghammer // linux kernel does otherwise. 1933be379e15STamas Berghammer static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7}; 1934af245d11STodd Fiala static const uint8_t g_i386_opcode[] = {0xCC}; 19353df471c3SMohit K. Bhakkad static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d}; 19362c2acf96SMohit K. Bhakkad static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00}; 1937bb00d0b6SUlrich Weigand static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; 1938be379e15STamas Berghammer static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde}; 1939af245d11STodd Fiala 1940b9c1b51eSKate Stone switch (m_arch.GetMachine()) { 19412afc5966STodd Fiala case llvm::Triple::aarch64: 19422afc5966STodd Fiala trap_opcode_bytes = g_aarch64_opcode; 19432afc5966STodd Fiala actual_opcode_size = sizeof(g_aarch64_opcode); 1944665be50eSMehdi Amini return Error(); 19452afc5966STodd Fiala 194663c8be95STamas Berghammer case llvm::Triple::arm: 1947b9c1b51eSKate Stone switch (trap_opcode_size_hint) { 194863c8be95STamas Berghammer case 2: 194963c8be95STamas Berghammer trap_opcode_bytes = g_thumb_breakpoint_opcode; 195063c8be95STamas Berghammer actual_opcode_size = sizeof(g_thumb_breakpoint_opcode); 1951665be50eSMehdi Amini return Error(); 195263c8be95STamas Berghammer case 4: 195363c8be95STamas Berghammer trap_opcode_bytes = g_arm_breakpoint_opcode; 195463c8be95STamas Berghammer actual_opcode_size = sizeof(g_arm_breakpoint_opcode); 1955665be50eSMehdi Amini return Error(); 195663c8be95STamas Berghammer default: 195763c8be95STamas Berghammer assert(false && "Unrecognised trap opcode size hint!"); 195863c8be95STamas Berghammer return Error("Unrecognised trap opcode size hint!"); 195963c8be95STamas Berghammer } 196063c8be95STamas Berghammer 1961af245d11STodd Fiala case llvm::Triple::x86: 1962af245d11STodd Fiala case llvm::Triple::x86_64: 1963af245d11STodd Fiala trap_opcode_bytes = g_i386_opcode; 1964af245d11STodd Fiala actual_opcode_size = sizeof(g_i386_opcode); 1965665be50eSMehdi Amini return Error(); 1966af245d11STodd Fiala 1967ce815e45SSagar Thakur case llvm::Triple::mips: 19683df471c3SMohit K. Bhakkad case llvm::Triple::mips64: 19693df471c3SMohit K. Bhakkad trap_opcode_bytes = g_mips64_opcode; 19703df471c3SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64_opcode); 1971665be50eSMehdi Amini return Error(); 19723df471c3SMohit K. Bhakkad 1973ce815e45SSagar Thakur case llvm::Triple::mipsel: 19742c2acf96SMohit K. Bhakkad case llvm::Triple::mips64el: 19752c2acf96SMohit K. Bhakkad trap_opcode_bytes = g_mips64el_opcode; 19762c2acf96SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64el_opcode); 1977665be50eSMehdi Amini return Error(); 19782c2acf96SMohit K. Bhakkad 1979bb00d0b6SUlrich Weigand case llvm::Triple::systemz: 1980bb00d0b6SUlrich Weigand trap_opcode_bytes = g_s390x_opcode; 1981bb00d0b6SUlrich Weigand actual_opcode_size = sizeof(g_s390x_opcode); 1982665be50eSMehdi Amini return Error(); 1983bb00d0b6SUlrich Weigand 1984af245d11STodd Fiala default: 1985af245d11STodd Fiala assert(false && "CPU type not supported!"); 1986af245d11STodd Fiala return Error("CPU type not supported"); 1987af245d11STodd Fiala } 1988af245d11STodd Fiala } 1989af245d11STodd Fiala 1990af245d11STodd Fiala #if 0 1991af245d11STodd Fiala ProcessMessage::CrashReason 1992af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info) 1993af245d11STodd Fiala { 1994af245d11STodd Fiala ProcessMessage::CrashReason reason; 1995af245d11STodd Fiala assert(info->si_signo == SIGSEGV); 1996af245d11STodd Fiala 1997af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1998af245d11STodd Fiala 1999af245d11STodd Fiala switch (info->si_code) 2000af245d11STodd Fiala { 2001af245d11STodd Fiala default: 2002af245d11STodd Fiala assert(false && "unexpected si_code for SIGSEGV"); 2003af245d11STodd Fiala break; 2004af245d11STodd Fiala case SI_KERNEL: 2005af245d11STodd Fiala // Linux will occasionally send spurious SI_KERNEL codes. 2006af245d11STodd Fiala // (this is poorly documented in sigaction) 2007af245d11STodd Fiala // One way to get this is via unaligned SIMD loads. 2008af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; // for lack of anything better 2009af245d11STodd Fiala break; 2010af245d11STodd Fiala case SEGV_MAPERR: 2011af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; 2012af245d11STodd Fiala break; 2013af245d11STodd Fiala case SEGV_ACCERR: 2014af245d11STodd Fiala reason = ProcessMessage::ePrivilegedAddress; 2015af245d11STodd Fiala break; 2016af245d11STodd Fiala } 2017af245d11STodd Fiala 2018af245d11STodd Fiala return reason; 2019af245d11STodd Fiala } 2020af245d11STodd Fiala #endif 2021af245d11STodd Fiala 2022af245d11STodd Fiala #if 0 2023af245d11STodd Fiala ProcessMessage::CrashReason 2024af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info) 2025af245d11STodd Fiala { 2026af245d11STodd Fiala ProcessMessage::CrashReason reason; 2027af245d11STodd Fiala assert(info->si_signo == SIGILL); 2028af245d11STodd Fiala 2029af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 2030af245d11STodd Fiala 2031af245d11STodd Fiala switch (info->si_code) 2032af245d11STodd Fiala { 2033af245d11STodd Fiala default: 2034af245d11STodd Fiala assert(false && "unexpected si_code for SIGILL"); 2035af245d11STodd Fiala break; 2036af245d11STodd Fiala case ILL_ILLOPC: 2037af245d11STodd Fiala reason = ProcessMessage::eIllegalOpcode; 2038af245d11STodd Fiala break; 2039af245d11STodd Fiala case ILL_ILLOPN: 2040af245d11STodd Fiala reason = ProcessMessage::eIllegalOperand; 2041af245d11STodd Fiala break; 2042af245d11STodd Fiala case ILL_ILLADR: 2043af245d11STodd Fiala reason = ProcessMessage::eIllegalAddressingMode; 2044af245d11STodd Fiala break; 2045af245d11STodd Fiala case ILL_ILLTRP: 2046af245d11STodd Fiala reason = ProcessMessage::eIllegalTrap; 2047af245d11STodd Fiala break; 2048af245d11STodd Fiala case ILL_PRVOPC: 2049af245d11STodd Fiala reason = ProcessMessage::ePrivilegedOpcode; 2050af245d11STodd Fiala break; 2051af245d11STodd Fiala case ILL_PRVREG: 2052af245d11STodd Fiala reason = ProcessMessage::ePrivilegedRegister; 2053af245d11STodd Fiala break; 2054af245d11STodd Fiala case ILL_COPROC: 2055af245d11STodd Fiala reason = ProcessMessage::eCoprocessorError; 2056af245d11STodd Fiala break; 2057af245d11STodd Fiala case ILL_BADSTK: 2058af245d11STodd Fiala reason = ProcessMessage::eInternalStackError; 2059af245d11STodd Fiala break; 2060af245d11STodd Fiala } 2061af245d11STodd Fiala 2062af245d11STodd Fiala return reason; 2063af245d11STodd Fiala } 2064af245d11STodd Fiala #endif 2065af245d11STodd Fiala 2066af245d11STodd Fiala #if 0 2067af245d11STodd Fiala ProcessMessage::CrashReason 2068af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info) 2069af245d11STodd Fiala { 2070af245d11STodd Fiala ProcessMessage::CrashReason reason; 2071af245d11STodd Fiala assert(info->si_signo == SIGFPE); 2072af245d11STodd Fiala 2073af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 2074af245d11STodd Fiala 2075af245d11STodd Fiala switch (info->si_code) 2076af245d11STodd Fiala { 2077af245d11STodd Fiala default: 2078af245d11STodd Fiala assert(false && "unexpected si_code for SIGFPE"); 2079af245d11STodd Fiala break; 2080af245d11STodd Fiala case FPE_INTDIV: 2081af245d11STodd Fiala reason = ProcessMessage::eIntegerDivideByZero; 2082af245d11STodd Fiala break; 2083af245d11STodd Fiala case FPE_INTOVF: 2084af245d11STodd Fiala reason = ProcessMessage::eIntegerOverflow; 2085af245d11STodd Fiala break; 2086af245d11STodd Fiala case FPE_FLTDIV: 2087af245d11STodd Fiala reason = ProcessMessage::eFloatDivideByZero; 2088af245d11STodd Fiala break; 2089af245d11STodd Fiala case FPE_FLTOVF: 2090af245d11STodd Fiala reason = ProcessMessage::eFloatOverflow; 2091af245d11STodd Fiala break; 2092af245d11STodd Fiala case FPE_FLTUND: 2093af245d11STodd Fiala reason = ProcessMessage::eFloatUnderflow; 2094af245d11STodd Fiala break; 2095af245d11STodd Fiala case FPE_FLTRES: 2096af245d11STodd Fiala reason = ProcessMessage::eFloatInexactResult; 2097af245d11STodd Fiala break; 2098af245d11STodd Fiala case FPE_FLTINV: 2099af245d11STodd Fiala reason = ProcessMessage::eFloatInvalidOperation; 2100af245d11STodd Fiala break; 2101af245d11STodd Fiala case FPE_FLTSUB: 2102af245d11STodd Fiala reason = ProcessMessage::eFloatSubscriptRange; 2103af245d11STodd Fiala break; 2104af245d11STodd Fiala } 2105af245d11STodd Fiala 2106af245d11STodd Fiala return reason; 2107af245d11STodd Fiala } 2108af245d11STodd Fiala #endif 2109af245d11STodd Fiala 2110af245d11STodd Fiala #if 0 2111af245d11STodd Fiala ProcessMessage::CrashReason 2112af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info) 2113af245d11STodd Fiala { 2114af245d11STodd Fiala ProcessMessage::CrashReason reason; 2115af245d11STodd Fiala assert(info->si_signo == SIGBUS); 2116af245d11STodd Fiala 2117af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 2118af245d11STodd Fiala 2119af245d11STodd Fiala switch (info->si_code) 2120af245d11STodd Fiala { 2121af245d11STodd Fiala default: 2122af245d11STodd Fiala assert(false && "unexpected si_code for SIGBUS"); 2123af245d11STodd Fiala break; 2124af245d11STodd Fiala case BUS_ADRALN: 2125af245d11STodd Fiala reason = ProcessMessage::eIllegalAlignment; 2126af245d11STodd Fiala break; 2127af245d11STodd Fiala case BUS_ADRERR: 2128af245d11STodd Fiala reason = ProcessMessage::eIllegalAddress; 2129af245d11STodd Fiala break; 2130af245d11STodd Fiala case BUS_OBJERR: 2131af245d11STodd Fiala reason = ProcessMessage::eHardwareError; 2132af245d11STodd Fiala break; 2133af245d11STodd Fiala } 2134af245d11STodd Fiala 2135af245d11STodd Fiala return reason; 2136af245d11STodd Fiala } 2137af245d11STodd Fiala #endif 2138af245d11STodd Fiala 2139b9c1b51eSKate Stone Error NativeProcessLinux::ReadMemory(lldb::addr_t addr, void *buf, size_t size, 2140b9c1b51eSKate Stone size_t &bytes_read) { 2141df7c6995SPavel Labath if (ProcessVmReadvSupported()) { 2142b9c1b51eSKate Stone // The process_vm_readv path is about 50 times faster than ptrace api. We 2143b9c1b51eSKate Stone // want to use 2144df7c6995SPavel Labath // this syscall if it is supported. 2145df7c6995SPavel Labath 2146df7c6995SPavel Labath const ::pid_t pid = GetID(); 2147df7c6995SPavel Labath 2148df7c6995SPavel Labath struct iovec local_iov, remote_iov; 2149df7c6995SPavel Labath local_iov.iov_base = buf; 2150df7c6995SPavel Labath local_iov.iov_len = size; 2151df7c6995SPavel Labath remote_iov.iov_base = reinterpret_cast<void *>(addr); 2152df7c6995SPavel Labath remote_iov.iov_len = size; 2153df7c6995SPavel Labath 2154df7c6995SPavel Labath bytes_read = process_vm_readv(pid, &local_iov, 1, &remote_iov, 1, 0); 2155df7c6995SPavel Labath const bool success = bytes_read == size; 2156df7c6995SPavel Labath 2157df7c6995SPavel Labath Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 2158df7c6995SPavel Labath if (log) 2159b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s using process_vm_readv to read %zd " 2160b9c1b51eSKate Stone "bytes from inferior address 0x%" PRIx64 ": %s", 2161b9c1b51eSKate Stone __FUNCTION__, size, addr, 2162b9c1b51eSKate Stone success ? "Success" : strerror(errno)); 2163df7c6995SPavel Labath 2164df7c6995SPavel Labath if (success) 2165665be50eSMehdi Amini return Error(); 2166df7c6995SPavel Labath // else 2167b9c1b51eSKate Stone // the call failed for some reason, let's retry the read using ptrace 2168b9c1b51eSKate Stone // api. 2169df7c6995SPavel Labath } 2170df7c6995SPavel Labath 217119cbe96aSPavel Labath unsigned char *dst = static_cast<unsigned char *>(buf); 217219cbe96aSPavel Labath size_t remainder; 217319cbe96aSPavel Labath long data; 217419cbe96aSPavel Labath 217519cbe96aSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_ALL)); 217619cbe96aSPavel Labath if (log) 217719cbe96aSPavel Labath ProcessPOSIXLog::IncNestLevel(); 2178b9c1b51eSKate Stone if (log && ProcessPOSIXLog::AtTopNestLevel() && 2179b9c1b51eSKate Stone log->GetMask().Test(POSIX_LOG_MEMORY)) 2180b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s(%p, %p, %zd, _)", __FUNCTION__, 2181b9c1b51eSKate Stone (void *)addr, buf, size); 218219cbe96aSPavel Labath 2183b9c1b51eSKate Stone for (bytes_read = 0; bytes_read < size; bytes_read += remainder) { 2184b9c1b51eSKate Stone Error error = NativeProcessLinux::PtraceWrapper( 2185b9c1b51eSKate Stone PTRACE_PEEKDATA, GetID(), (void *)addr, nullptr, 0, &data); 2186b9c1b51eSKate Stone if (error.Fail()) { 218719cbe96aSPavel Labath if (log) 218819cbe96aSPavel Labath ProcessPOSIXLog::DecNestLevel(); 218919cbe96aSPavel Labath return error; 219019cbe96aSPavel Labath } 219119cbe96aSPavel Labath 219219cbe96aSPavel Labath remainder = size - bytes_read; 219319cbe96aSPavel Labath remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; 219419cbe96aSPavel Labath 219519cbe96aSPavel Labath // Copy the data into our buffer 2196f6ef187bSMohit K. Bhakkad memcpy(dst, &data, remainder); 219719cbe96aSPavel Labath 219819cbe96aSPavel Labath if (log && ProcessPOSIXLog::AtTopNestLevel() && 219919cbe96aSPavel Labath (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 220019cbe96aSPavel Labath (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 2201b9c1b51eSKate Stone size <= POSIX_LOG_MEMORY_SHORT_BYTES))) { 220219cbe96aSPavel Labath uintptr_t print_dst = 0; 220319cbe96aSPavel Labath // Format bytes from data by moving into print_dst for log output 220419cbe96aSPavel Labath for (unsigned i = 0; i < remainder; ++i) 220519cbe96aSPavel Labath print_dst |= (((data >> i * 8) & 0xFF) << i * 8); 2206b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() [0x%" PRIx64 "]:0x%" PRIx64 2207b9c1b51eSKate Stone " (0x%" PRIx64 ")", 220879203995SPavel Labath __FUNCTION__, addr, uint64_t(print_dst), uint64_t(data)); 220919cbe96aSPavel Labath } 221019cbe96aSPavel Labath addr += k_ptrace_word_size; 221119cbe96aSPavel Labath dst += k_ptrace_word_size; 221219cbe96aSPavel Labath } 221319cbe96aSPavel Labath 221419cbe96aSPavel Labath if (log) 221519cbe96aSPavel Labath ProcessPOSIXLog::DecNestLevel(); 2216665be50eSMehdi Amini return Error(); 2217af245d11STodd Fiala } 2218af245d11STodd Fiala 2219b9c1b51eSKate Stone Error NativeProcessLinux::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, 2220b9c1b51eSKate Stone size_t size, 2221b9c1b51eSKate Stone size_t &bytes_read) { 22223eb4b458SChaoren Lin Error error = ReadMemory(addr, buf, size, bytes_read); 2223b9c1b51eSKate Stone if (error.Fail()) 2224b9c1b51eSKate Stone return error; 22253eb4b458SChaoren Lin return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); 22263eb4b458SChaoren Lin } 22273eb4b458SChaoren Lin 2228b9c1b51eSKate Stone Error NativeProcessLinux::WriteMemory(lldb::addr_t addr, const void *buf, 2229b9c1b51eSKate Stone size_t size, size_t &bytes_written) { 223019cbe96aSPavel Labath const unsigned char *src = static_cast<const unsigned char *>(buf); 223119cbe96aSPavel Labath size_t remainder; 223219cbe96aSPavel Labath Error error; 223319cbe96aSPavel Labath 223419cbe96aSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_ALL)); 223519cbe96aSPavel Labath if (log) 223619cbe96aSPavel Labath ProcessPOSIXLog::IncNestLevel(); 2237b9c1b51eSKate Stone if (log && ProcessPOSIXLog::AtTopNestLevel() && 2238b9c1b51eSKate Stone log->GetMask().Test(POSIX_LOG_MEMORY)) 2239b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s(0x%" PRIx64 ", %p, %zu)", __FUNCTION__, 2240b9c1b51eSKate Stone addr, buf, size); 224119cbe96aSPavel Labath 2242b9c1b51eSKate Stone for (bytes_written = 0; bytes_written < size; bytes_written += remainder) { 224319cbe96aSPavel Labath remainder = size - bytes_written; 224419cbe96aSPavel Labath remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; 224519cbe96aSPavel Labath 2246b9c1b51eSKate Stone if (remainder == k_ptrace_word_size) { 224719cbe96aSPavel Labath unsigned long data = 0; 2248f6ef187bSMohit K. Bhakkad memcpy(&data, src, k_ptrace_word_size); 224919cbe96aSPavel Labath 225019cbe96aSPavel Labath if (log && ProcessPOSIXLog::AtTopNestLevel() && 225119cbe96aSPavel Labath (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 225219cbe96aSPavel Labath (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 225319cbe96aSPavel Labath size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 225419cbe96aSPavel Labath log->Printf("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 225519cbe96aSPavel Labath (void *)addr, *(const unsigned long *)src, data); 225619cbe96aSPavel Labath 2257b9c1b51eSKate Stone error = NativeProcessLinux::PtraceWrapper(PTRACE_POKEDATA, GetID(), 2258b9c1b51eSKate Stone (void *)addr, (void *)data); 2259b9c1b51eSKate Stone if (error.Fail()) { 226019cbe96aSPavel Labath if (log) 226119cbe96aSPavel Labath ProcessPOSIXLog::DecNestLevel(); 226219cbe96aSPavel Labath return error; 226319cbe96aSPavel Labath } 2264b9c1b51eSKate Stone } else { 226519cbe96aSPavel Labath unsigned char buff[8]; 226619cbe96aSPavel Labath size_t bytes_read; 226719cbe96aSPavel Labath error = ReadMemory(addr, buff, k_ptrace_word_size, bytes_read); 2268b9c1b51eSKate Stone if (error.Fail()) { 226919cbe96aSPavel Labath if (log) 227019cbe96aSPavel Labath ProcessPOSIXLog::DecNestLevel(); 227119cbe96aSPavel Labath return error; 227219cbe96aSPavel Labath } 227319cbe96aSPavel Labath 227419cbe96aSPavel Labath memcpy(buff, src, remainder); 227519cbe96aSPavel Labath 227619cbe96aSPavel Labath size_t bytes_written_rec; 227719cbe96aSPavel Labath error = WriteMemory(addr, buff, k_ptrace_word_size, bytes_written_rec); 2278b9c1b51eSKate Stone if (error.Fail()) { 227919cbe96aSPavel Labath if (log) 228019cbe96aSPavel Labath ProcessPOSIXLog::DecNestLevel(); 228119cbe96aSPavel Labath return error; 228219cbe96aSPavel Labath } 228319cbe96aSPavel Labath 228419cbe96aSPavel Labath if (log && ProcessPOSIXLog::AtTopNestLevel() && 228519cbe96aSPavel Labath (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 228619cbe96aSPavel Labath (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 228719cbe96aSPavel Labath size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 228819cbe96aSPavel Labath log->Printf("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 2289b9c1b51eSKate Stone (void *)addr, *(const unsigned long *)src, 2290b9c1b51eSKate Stone *(unsigned long *)buff); 229119cbe96aSPavel Labath } 229219cbe96aSPavel Labath 229319cbe96aSPavel Labath addr += k_ptrace_word_size; 229419cbe96aSPavel Labath src += k_ptrace_word_size; 229519cbe96aSPavel Labath } 229619cbe96aSPavel Labath if (log) 229719cbe96aSPavel Labath ProcessPOSIXLog::DecNestLevel(); 229819cbe96aSPavel Labath return error; 2299af245d11STodd Fiala } 2300af245d11STodd Fiala 2301b9c1b51eSKate Stone Error NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) { 230219cbe96aSPavel Labath return PtraceWrapper(PTRACE_GETSIGINFO, tid, nullptr, siginfo); 2303af245d11STodd Fiala } 2304af245d11STodd Fiala 2305b9c1b51eSKate Stone Error NativeProcessLinux::GetEventMessage(lldb::tid_t tid, 2306b9c1b51eSKate Stone unsigned long *message) { 230719cbe96aSPavel Labath return PtraceWrapper(PTRACE_GETEVENTMSG, tid, nullptr, message); 2308af245d11STodd Fiala } 2309af245d11STodd Fiala 2310b9c1b51eSKate Stone Error NativeProcessLinux::Detach(lldb::tid_t tid) { 231197ccc294SChaoren Lin if (tid == LLDB_INVALID_THREAD_ID) 2312665be50eSMehdi Amini return Error(); 231397ccc294SChaoren Lin 231419cbe96aSPavel Labath return PtraceWrapper(PTRACE_DETACH, tid); 2315af245d11STodd Fiala } 2316af245d11STodd Fiala 2317b9c1b51eSKate Stone bool NativeProcessLinux::HasThreadNoLock(lldb::tid_t thread_id) { 2318b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 2319af245d11STodd Fiala assert(thread_sp && "thread list should not contain NULL threads"); 2320b9c1b51eSKate Stone if (thread_sp->GetID() == thread_id) { 2321af245d11STodd Fiala // We have this thread. 2322af245d11STodd Fiala return true; 2323af245d11STodd Fiala } 2324af245d11STodd Fiala } 2325af245d11STodd Fiala 2326af245d11STodd Fiala // We don't have this thread. 2327af245d11STodd Fiala return false; 2328af245d11STodd Fiala } 2329af245d11STodd Fiala 2330b9c1b51eSKate Stone bool NativeProcessLinux::StopTrackingThread(lldb::tid_t thread_id) { 23311dbc6c9cSPavel Labath Log *const log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD); 23321dbc6c9cSPavel Labath 23331dbc6c9cSPavel Labath if (log) 2334b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ")", __FUNCTION__, 2335b9c1b51eSKate Stone thread_id); 23361dbc6c9cSPavel Labath 23371dbc6c9cSPavel Labath bool found = false; 23381dbc6c9cSPavel Labath 2339b9c1b51eSKate Stone for (auto it = m_threads.begin(); it != m_threads.end(); ++it) { 2340b9c1b51eSKate Stone if (*it && ((*it)->GetID() == thread_id)) { 2341af245d11STodd Fiala m_threads.erase(it); 23421dbc6c9cSPavel Labath found = true; 23431dbc6c9cSPavel Labath break; 2344af245d11STodd Fiala } 2345af245d11STodd Fiala } 2346af245d11STodd Fiala 23479eb1ecb9SPavel Labath SignalIfAllThreadsStopped(); 23481dbc6c9cSPavel Labath 23491dbc6c9cSPavel Labath return found; 2350af245d11STodd Fiala } 2351af245d11STodd Fiala 2352b9c1b51eSKate Stone NativeThreadLinuxSP NativeProcessLinux::AddThread(lldb::tid_t thread_id) { 2353af245d11STodd Fiala Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 2354af245d11STodd Fiala 2355b9c1b51eSKate Stone if (log) { 2356b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s pid %" PRIu64 2357b9c1b51eSKate Stone " adding thread with tid %" PRIu64, 2358b9c1b51eSKate Stone __FUNCTION__, GetID(), thread_id); 2359af245d11STodd Fiala } 2360af245d11STodd Fiala 2361b9c1b51eSKate Stone assert(!HasThreadNoLock(thread_id) && 2362b9c1b51eSKate Stone "attempted to add a thread by id that already exists"); 2363af245d11STodd Fiala 2364af245d11STodd Fiala // If this is the first thread, save it as the current thread 2365af245d11STodd Fiala if (m_threads.empty()) 2366af245d11STodd Fiala SetCurrentThreadID(thread_id); 2367af245d11STodd Fiala 2368f9077782SPavel Labath auto thread_sp = std::make_shared<NativeThreadLinux>(this, thread_id); 2369af245d11STodd Fiala m_threads.push_back(thread_sp); 2370af245d11STodd Fiala return thread_sp; 2371af245d11STodd Fiala } 2372af245d11STodd Fiala 2373b9c1b51eSKate Stone Error NativeProcessLinux::FixupBreakpointPCAsNeeded(NativeThreadLinux &thread) { 237475f47c3aSTodd Fiala Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 2375af245d11STodd Fiala 2376af245d11STodd Fiala Error error; 2377af245d11STodd Fiala 2378b9c1b51eSKate Stone // Find out the size of a breakpoint (might depend on where we are in the 2379b9c1b51eSKate Stone // code). 2380b9cc0c75SPavel Labath NativeRegisterContextSP context_sp = thread.GetRegisterContext(); 2381b9c1b51eSKate Stone if (!context_sp) { 2382af245d11STodd Fiala error.SetErrorString("cannot get a NativeRegisterContext for the thread"); 2383af245d11STodd Fiala if (log) 2384b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s failed: %s", __FUNCTION__, 2385b9c1b51eSKate Stone error.AsCString()); 2386af245d11STodd Fiala return error; 2387af245d11STodd Fiala } 2388af245d11STodd Fiala 2389af245d11STodd Fiala uint32_t breakpoint_size = 0; 2390b9cc0c75SPavel Labath error = GetSoftwareBreakpointPCOffset(breakpoint_size); 2391b9c1b51eSKate Stone if (error.Fail()) { 2392af245d11STodd Fiala if (log) 2393b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s GetBreakpointSize() failed: %s", 2394b9c1b51eSKate Stone __FUNCTION__, error.AsCString()); 2395af245d11STodd Fiala return error; 2396b9c1b51eSKate Stone } else { 2397af245d11STodd Fiala if (log) 2398b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s breakpoint size: %" PRIu32, 2399b9c1b51eSKate Stone __FUNCTION__, breakpoint_size); 2400af245d11STodd Fiala } 2401af245d11STodd Fiala 2402b9c1b51eSKate Stone // First try probing for a breakpoint at a software breakpoint location: PC - 2403b9c1b51eSKate Stone // breakpoint size. 2404b9c1b51eSKate Stone const lldb::addr_t initial_pc_addr = 2405b9c1b51eSKate Stone context_sp->GetPCfromBreakpointLocation(); 2406af245d11STodd Fiala lldb::addr_t breakpoint_addr = initial_pc_addr; 2407b9c1b51eSKate Stone if (breakpoint_size > 0) { 2408af245d11STodd Fiala // Do not allow breakpoint probe to wrap around. 24093eb4b458SChaoren Lin if (breakpoint_addr >= breakpoint_size) 24103eb4b458SChaoren Lin breakpoint_addr -= breakpoint_size; 2411af245d11STodd Fiala } 2412af245d11STodd Fiala 2413af245d11STodd Fiala // Check if we stopped because of a breakpoint. 2414af245d11STodd Fiala NativeBreakpointSP breakpoint_sp; 2415af245d11STodd Fiala error = m_breakpoint_list.GetBreakpoint(breakpoint_addr, breakpoint_sp); 2416b9c1b51eSKate Stone if (!error.Success() || !breakpoint_sp) { 2417af245d11STodd Fiala // We didn't find one at a software probe location. Nothing to do. 2418af245d11STodd Fiala if (log) 2419b9c1b51eSKate Stone log->Printf( 2420b9c1b51eSKate Stone "NativeProcessLinux::%s pid %" PRIu64 2421b9c1b51eSKate Stone " no lldb breakpoint found at current pc with adjustment: 0x%" PRIx64, 2422b9c1b51eSKate Stone __FUNCTION__, GetID(), breakpoint_addr); 2423665be50eSMehdi Amini return Error(); 2424af245d11STodd Fiala } 2425af245d11STodd Fiala 2426af245d11STodd Fiala // If the breakpoint is not a software breakpoint, nothing to do. 2427b9c1b51eSKate Stone if (!breakpoint_sp->IsSoftwareBreakpoint()) { 2428af245d11STodd Fiala if (log) 2429b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s pid %" PRIu64 2430b9c1b51eSKate Stone " breakpoint found at 0x%" PRIx64 2431b9c1b51eSKate Stone ", not software, nothing to adjust", 2432b9c1b51eSKate Stone __FUNCTION__, GetID(), breakpoint_addr); 2433665be50eSMehdi Amini return Error(); 2434af245d11STodd Fiala } 2435af245d11STodd Fiala 2436af245d11STodd Fiala // 2437af245d11STodd Fiala // We have a software breakpoint and need to adjust the PC. 2438af245d11STodd Fiala // 2439af245d11STodd Fiala 2440af245d11STodd Fiala // Sanity check. 2441b9c1b51eSKate Stone if (breakpoint_size == 0) { 2442af245d11STodd Fiala // Nothing to do! How did we get here? 2443af245d11STodd Fiala if (log) 2444b9c1b51eSKate Stone log->Printf( 2445b9c1b51eSKate Stone "NativeProcessLinux::%s pid %" PRIu64 2446b9c1b51eSKate Stone " breakpoint found at 0x%" PRIx64 2447b9c1b51eSKate Stone ", it is software, but the size is zero, nothing to do (unexpected)", 2448b9c1b51eSKate Stone __FUNCTION__, GetID(), breakpoint_addr); 2449665be50eSMehdi Amini return Error(); 2450af245d11STodd Fiala } 2451af245d11STodd Fiala 2452af245d11STodd Fiala // Change the program counter. 2453af245d11STodd Fiala if (log) 2454b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 2455b9c1b51eSKate Stone ": changing PC from 0x%" PRIx64 " to 0x%" PRIx64, 2456b9c1b51eSKate Stone __FUNCTION__, GetID(), thread.GetID(), initial_pc_addr, 2457b9c1b51eSKate Stone breakpoint_addr); 2458af245d11STodd Fiala 2459af245d11STodd Fiala error = context_sp->SetPC(breakpoint_addr); 2460b9c1b51eSKate Stone if (error.Fail()) { 2461af245d11STodd Fiala if (log) 2462b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 2463b9c1b51eSKate Stone ": failed to set PC: %s", 2464b9c1b51eSKate Stone __FUNCTION__, GetID(), thread.GetID(), error.AsCString()); 2465af245d11STodd Fiala return error; 2466af245d11STodd Fiala } 2467af245d11STodd Fiala 2468af245d11STodd Fiala return error; 2469af245d11STodd Fiala } 2470fa03ad2eSChaoren Lin 2471b9c1b51eSKate Stone Error NativeProcessLinux::GetLoadedModuleFileSpec(const char *module_path, 2472b9c1b51eSKate Stone FileSpec &file_spec) { 2473*a6f5795aSTamas Berghammer Error error = PopulateMemoryRegionCache(); 2474*a6f5795aSTamas Berghammer if (error.Fail()) 2475*a6f5795aSTamas Berghammer return error; 2476*a6f5795aSTamas Berghammer 24777cb18bf5STamas Berghammer FileSpec module_file_spec(module_path, true); 24787cb18bf5STamas Berghammer 24797cb18bf5STamas Berghammer file_spec.Clear(); 2480*a6f5795aSTamas Berghammer for (const auto &it : m_mem_region_cache) { 2481*a6f5795aSTamas Berghammer if (it.second.GetFilename() == module_file_spec.GetFilename()) { 2482*a6f5795aSTamas Berghammer file_spec = it.second; 2483*a6f5795aSTamas Berghammer return Error(); 2484*a6f5795aSTamas Berghammer } 2485*a6f5795aSTamas Berghammer } 24867cb18bf5STamas Berghammer return Error("Module file (%s) not found in /proc/%" PRIu64 "/maps file!", 24877cb18bf5STamas Berghammer module_file_spec.GetFilename().AsCString(), GetID()); 24887cb18bf5STamas Berghammer } 2489c076559aSPavel Labath 2490b9c1b51eSKate Stone Error NativeProcessLinux::GetFileLoadAddress(const llvm::StringRef &file_name, 2491b9c1b51eSKate Stone lldb::addr_t &load_addr) { 2492783bfc8cSTamas Berghammer load_addr = LLDB_INVALID_ADDRESS; 2493*a6f5795aSTamas Berghammer Error error = PopulateMemoryRegionCache(); 2494*a6f5795aSTamas Berghammer if (error.Fail()) 2495783bfc8cSTamas Berghammer return error; 2496*a6f5795aSTamas Berghammer 2497*a6f5795aSTamas Berghammer FileSpec file(file_name, false); 2498*a6f5795aSTamas Berghammer for (const auto &it : m_mem_region_cache) { 2499*a6f5795aSTamas Berghammer if (it.second == file) { 2500*a6f5795aSTamas Berghammer load_addr = it.first.GetRange().GetRangeBase(); 2501*a6f5795aSTamas Berghammer return Error(); 2502*a6f5795aSTamas Berghammer } 2503*a6f5795aSTamas Berghammer } 2504*a6f5795aSTamas Berghammer return Error("No load address found for specified file."); 2505783bfc8cSTamas Berghammer } 2506783bfc8cSTamas Berghammer 2507b9c1b51eSKate Stone NativeThreadLinuxSP NativeProcessLinux::GetThreadByID(lldb::tid_t tid) { 2508b9c1b51eSKate Stone return std::static_pointer_cast<NativeThreadLinux>( 2509b9c1b51eSKate Stone NativeProcessProtocol::GetThreadByID(tid)); 2510f9077782SPavel Labath } 2511f9077782SPavel Labath 2512b9c1b51eSKate Stone Error NativeProcessLinux::ResumeThread(NativeThreadLinux &thread, 2513b9c1b51eSKate Stone lldb::StateType state, int signo) { 25145eb721edSPavel Labath Log *const log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD); 25155eb721edSPavel Labath 25161dbc6c9cSPavel Labath if (log) 2517b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ")", __FUNCTION__, 2518b9c1b51eSKate Stone thread.GetID()); 2519c076559aSPavel Labath 2520c076559aSPavel Labath // Before we do the resume below, first check if we have a pending 2521108c325dSPavel Labath // stop notification that is currently waiting for 25220e1d729bSPavel Labath // all threads to stop. This is potentially a buggy situation since 2523c076559aSPavel Labath // we're ostensibly waiting for threads to stop before we send out the 2524c076559aSPavel Labath // pending notification, and here we are resuming one before we send 2525c076559aSPavel Labath // out the pending stop notification. 2526b9c1b51eSKate Stone if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID && log) { 2527b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s about to resume tid %" PRIu64 2528b9c1b51eSKate Stone " per explicit request but we have a pending stop notification " 2529b9c1b51eSKate Stone "(tid %" PRIu64 ") that is actively waiting for this thread to " 2530b9c1b51eSKate Stone "stop. Valid sequence of events?", 2531b9c1b51eSKate Stone __FUNCTION__, thread.GetID(), m_pending_notification_tid); 2532c076559aSPavel Labath } 2533c076559aSPavel Labath 2534c076559aSPavel Labath // Request a resume. We expect this to be synchronous and the system 2535c076559aSPavel Labath // to reflect it is running after this completes. 2536b9c1b51eSKate Stone switch (state) { 2537b9c1b51eSKate Stone case eStateRunning: { 2538605b51b8SPavel Labath const auto resume_result = thread.Resume(signo); 25390e1d729bSPavel Labath if (resume_result.Success()) 25400e1d729bSPavel Labath SetState(eStateRunning, true); 25410e1d729bSPavel Labath return resume_result; 2542c076559aSPavel Labath } 2543b9c1b51eSKate Stone case eStateStepping: { 2544605b51b8SPavel Labath const auto step_result = thread.SingleStep(signo); 25450e1d729bSPavel Labath if (step_result.Success()) 25460e1d729bSPavel Labath SetState(eStateRunning, true); 25470e1d729bSPavel Labath return step_result; 25480e1d729bSPavel Labath } 25490e1d729bSPavel Labath default: 25500e1d729bSPavel Labath if (log) 2551b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s Unhandled state %s.", __FUNCTION__, 2552b9c1b51eSKate Stone StateAsCString(state)); 25530e1d729bSPavel Labath llvm_unreachable("Unhandled state for resume"); 25540e1d729bSPavel Labath } 2555c076559aSPavel Labath } 2556c076559aSPavel Labath 2557c076559aSPavel Labath //===----------------------------------------------------------------------===// 2558c076559aSPavel Labath 2559b9c1b51eSKate Stone void NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid) { 25605eb721edSPavel Labath Log *const log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD); 2561c076559aSPavel Labath 2562b9c1b51eSKate Stone if (log) { 2563b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s about to process event: " 2564b9c1b51eSKate Stone "(triggering_tid: %" PRIu64 ")", 2565c076559aSPavel Labath __FUNCTION__, triggering_tid); 2566c076559aSPavel Labath } 2567c076559aSPavel Labath 25680e1d729bSPavel Labath m_pending_notification_tid = triggering_tid; 25690e1d729bSPavel Labath 25700e1d729bSPavel Labath // Request a stop for all the thread stops that need to be stopped 25710e1d729bSPavel Labath // and are not already known to be stopped. 2572b9c1b51eSKate Stone for (const auto &thread_sp : m_threads) { 25730e1d729bSPavel Labath if (StateIsRunningState(thread_sp->GetState())) 25740e1d729bSPavel Labath static_pointer_cast<NativeThreadLinux>(thread_sp)->RequestStop(); 25750e1d729bSPavel Labath } 25760e1d729bSPavel Labath 25770e1d729bSPavel Labath SignalIfAllThreadsStopped(); 2578c076559aSPavel Labath 2579b9c1b51eSKate Stone if (log) { 25805eb721edSPavel Labath log->Printf("NativeProcessLinux::%s event processing done", __FUNCTION__); 2581c076559aSPavel Labath } 2582c076559aSPavel Labath } 2583c076559aSPavel Labath 2584b9c1b51eSKate Stone void NativeProcessLinux::SignalIfAllThreadsStopped() { 25850e1d729bSPavel Labath if (m_pending_notification_tid == LLDB_INVALID_THREAD_ID) 25860e1d729bSPavel Labath return; // No pending notification. Nothing to do. 25870e1d729bSPavel Labath 2588b9c1b51eSKate Stone for (const auto &thread_sp : m_threads) { 25890e1d729bSPavel Labath if (StateIsRunningState(thread_sp->GetState())) 25900e1d729bSPavel Labath return; // Some threads are still running. Don't signal yet. 25910e1d729bSPavel Labath } 25920e1d729bSPavel Labath 25930e1d729bSPavel Labath // We have a pending notification and all threads have stopped. 2594b9c1b51eSKate Stone Log *log( 2595b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 25969eb1ecb9SPavel Labath 2597b9c1b51eSKate Stone // Clear any temporary breakpoints we used to implement software single 2598b9c1b51eSKate Stone // stepping. 2599b9c1b51eSKate Stone for (const auto &thread_info : m_threads_stepping_with_breakpoint) { 26009eb1ecb9SPavel Labath Error error = RemoveBreakpoint(thread_info.second); 26019eb1ecb9SPavel Labath if (error.Fail()) 26029eb1ecb9SPavel Labath if (log) 2603b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 2604b9c1b51eSKate Stone " remove stepping breakpoint: %s", 26059eb1ecb9SPavel Labath __FUNCTION__, thread_info.first, error.AsCString()); 26069eb1ecb9SPavel Labath } 26079eb1ecb9SPavel Labath m_threads_stepping_with_breakpoint.clear(); 26089eb1ecb9SPavel Labath 26099eb1ecb9SPavel Labath // Notify the delegate about the stop 26100e1d729bSPavel Labath SetCurrentThreadID(m_pending_notification_tid); 2611ed89c7feSPavel Labath SetState(StateType::eStateStopped, true); 26120e1d729bSPavel Labath m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 2613c076559aSPavel Labath } 2614c076559aSPavel Labath 2615b9c1b51eSKate Stone void NativeProcessLinux::ThreadWasCreated(NativeThreadLinux &thread) { 26161dbc6c9cSPavel Labath Log *const log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD); 26171dbc6c9cSPavel Labath 26181dbc6c9cSPavel Labath if (log) 2619b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ")", __FUNCTION__, 2620b9c1b51eSKate Stone thread.GetID()); 26211dbc6c9cSPavel Labath 2622b9c1b51eSKate Stone if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID && 2623b9c1b51eSKate Stone StateIsRunningState(thread.GetState())) { 2624b9c1b51eSKate Stone // We will need to wait for this new thread to stop as well before firing 2625b9c1b51eSKate Stone // the 2626c076559aSPavel Labath // notification. 2627f9077782SPavel Labath thread.RequestStop(); 2628c076559aSPavel Labath } 2629c076559aSPavel Labath } 2630068f8a7eSTamas Berghammer 2631b9c1b51eSKate Stone void NativeProcessLinux::SigchldHandler() { 263219cbe96aSPavel Labath Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 263319cbe96aSPavel Labath // Process all pending waitpid notifications. 2634b9c1b51eSKate Stone while (true) { 263519cbe96aSPavel Labath int status = -1; 263619cbe96aSPavel Labath ::pid_t wait_pid = waitpid(-1, &status, __WALL | __WNOTHREAD | WNOHANG); 263719cbe96aSPavel Labath 263819cbe96aSPavel Labath if (wait_pid == 0) 263919cbe96aSPavel Labath break; // We are done. 264019cbe96aSPavel Labath 2641b9c1b51eSKate Stone if (wait_pid == -1) { 264219cbe96aSPavel Labath if (errno == EINTR) 264319cbe96aSPavel Labath continue; 264419cbe96aSPavel Labath 264519cbe96aSPavel Labath Error error(errno, eErrorTypePOSIX); 264619cbe96aSPavel Labath if (log) 2647b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s waitpid (-1, &status, __WALL | " 2648b9c1b51eSKate Stone "__WNOTHREAD | WNOHANG) failed: %s", 264919cbe96aSPavel Labath __FUNCTION__, error.AsCString()); 265019cbe96aSPavel Labath break; 265119cbe96aSPavel Labath } 265219cbe96aSPavel Labath 265319cbe96aSPavel Labath bool exited = false; 265419cbe96aSPavel Labath int signal = 0; 265519cbe96aSPavel Labath int exit_status = 0; 265619cbe96aSPavel Labath const char *status_cstr = nullptr; 2657b9c1b51eSKate Stone if (WIFSTOPPED(status)) { 265819cbe96aSPavel Labath signal = WSTOPSIG(status); 265919cbe96aSPavel Labath status_cstr = "STOPPED"; 2660b9c1b51eSKate Stone } else if (WIFEXITED(status)) { 266119cbe96aSPavel Labath exit_status = WEXITSTATUS(status); 266219cbe96aSPavel Labath status_cstr = "EXITED"; 266319cbe96aSPavel Labath exited = true; 2664b9c1b51eSKate Stone } else if (WIFSIGNALED(status)) { 266519cbe96aSPavel Labath signal = WTERMSIG(status); 266619cbe96aSPavel Labath status_cstr = "SIGNALED"; 266719cbe96aSPavel Labath if (wait_pid == static_cast<::pid_t>(GetID())) { 266819cbe96aSPavel Labath exited = true; 266919cbe96aSPavel Labath exit_status = -1; 267019cbe96aSPavel Labath } 2671b9c1b51eSKate Stone } else 267219cbe96aSPavel Labath status_cstr = "(\?\?\?)"; 267319cbe96aSPavel Labath 267419cbe96aSPavel Labath if (log) 2675b9c1b51eSKate Stone log->Printf("NativeProcessLinux::%s: waitpid (-1, &status, __WALL | " 2676b9c1b51eSKate Stone "__WNOTHREAD | WNOHANG)" 2677b9c1b51eSKate Stone "=> pid = %" PRIi32 2678b9c1b51eSKate Stone ", status = 0x%8.8x (%s), signal = %i, exit_state = %i", 2679b9c1b51eSKate Stone __FUNCTION__, wait_pid, status, status_cstr, signal, 2680b9c1b51eSKate Stone exit_status); 268119cbe96aSPavel Labath 268219cbe96aSPavel Labath MonitorCallback(wait_pid, exited, signal, exit_status); 268319cbe96aSPavel Labath } 2684068f8a7eSTamas Berghammer } 2685068f8a7eSTamas Berghammer 2686068f8a7eSTamas Berghammer // Wrapper for ptrace to catch errors and log calls. 2687b9c1b51eSKate Stone // Note that ptrace sets errno on error because -1 can be a valid result (i.e. 2688b9c1b51eSKate Stone // for PTRACE_PEEK*) 2689b9c1b51eSKate Stone Error NativeProcessLinux::PtraceWrapper(int req, lldb::pid_t pid, void *addr, 2690b9c1b51eSKate Stone void *data, size_t data_size, 2691b9c1b51eSKate Stone long *result) { 26924a9babb2SPavel Labath Error error; 26934a9babb2SPavel Labath long int ret; 2694068f8a7eSTamas Berghammer 2695068f8a7eSTamas Berghammer Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 2696068f8a7eSTamas Berghammer 2697068f8a7eSTamas Berghammer PtraceDisplayBytes(req, data, data_size); 2698068f8a7eSTamas Berghammer 2699068f8a7eSTamas Berghammer errno = 0; 2700068f8a7eSTamas Berghammer if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) 2701b9c1b51eSKate Stone ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), 2702b9c1b51eSKate Stone *(unsigned int *)addr, data); 2703068f8a7eSTamas Berghammer else 2704b9c1b51eSKate Stone ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), 2705b9c1b51eSKate Stone addr, data); 2706068f8a7eSTamas Berghammer 27074a9babb2SPavel Labath if (ret == -1) 2708068f8a7eSTamas Berghammer error.SetErrorToErrno(); 2709068f8a7eSTamas Berghammer 27104a9babb2SPavel Labath if (result) 27114a9babb2SPavel Labath *result = ret; 27124a9babb2SPavel Labath 2713068f8a7eSTamas Berghammer if (log) 2714b9c1b51eSKate Stone log->Printf("ptrace(%d, %" PRIu64 ", %p, %p, %zu)=%lX", req, pid, addr, 2715b9c1b51eSKate Stone data, data_size, ret); 2716068f8a7eSTamas Berghammer 2717068f8a7eSTamas Berghammer PtraceDisplayBytes(req, data, data_size); 2718068f8a7eSTamas Berghammer 2719b9c1b51eSKate Stone if (log && error.GetError() != 0) { 2720068f8a7eSTamas Berghammer const char *str; 2721b9c1b51eSKate Stone switch (error.GetError()) { 2722b9c1b51eSKate Stone case ESRCH: 2723b9c1b51eSKate Stone str = "ESRCH"; 2724b9c1b51eSKate Stone break; 2725b9c1b51eSKate Stone case EINVAL: 2726b9c1b51eSKate Stone str = "EINVAL"; 2727b9c1b51eSKate Stone break; 2728b9c1b51eSKate Stone case EBUSY: 2729b9c1b51eSKate Stone str = "EBUSY"; 2730b9c1b51eSKate Stone break; 2731b9c1b51eSKate Stone case EPERM: 2732b9c1b51eSKate Stone str = "EPERM"; 2733b9c1b51eSKate Stone break; 2734b9c1b51eSKate Stone default: 2735b9c1b51eSKate Stone str = error.AsCString(); 2736068f8a7eSTamas Berghammer } 2737068f8a7eSTamas Berghammer log->Printf("ptrace() failed; errno=%d (%s)", error.GetError(), str); 2738068f8a7eSTamas Berghammer } 2739068f8a7eSTamas Berghammer 27404a9babb2SPavel Labath return error; 2741068f8a7eSTamas Berghammer } 2742