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 "lldb/lldb-python.h" 11af245d11STodd Fiala 12af245d11STodd Fiala #include "NativeProcessLinux.h" 13af245d11STodd Fiala 14af245d11STodd Fiala // C Includes 15af245d11STodd Fiala #include <errno.h> 16af245d11STodd Fiala #include <poll.h> 17af245d11STodd Fiala #include <string.h> 18af245d11STodd Fiala #include <stdint.h> 19af245d11STodd Fiala #include <unistd.h> 20af245d11STodd Fiala 21af245d11STodd Fiala // C++ Includes 22af245d11STodd Fiala #include <fstream> 23c076559aSPavel Labath #include <sstream> 24af245d11STodd Fiala #include <string> 25af245d11STodd Fiala 26af245d11STodd Fiala // Other libraries and framework includes 27af245d11STodd Fiala #include "lldb/Core/Debugger.h" 28d8c338d4STamas Berghammer #include "lldb/Core/EmulateInstruction.h" 29af245d11STodd Fiala #include "lldb/Core/Error.h" 30af245d11STodd Fiala #include "lldb/Core/Module.h" 316edef204SOleksiy Vyalov #include "lldb/Core/ModuleSpec.h" 32af245d11STodd Fiala #include "lldb/Core/RegisterValue.h" 33af245d11STodd Fiala #include "lldb/Core/Scalar.h" 34af245d11STodd Fiala #include "lldb/Core/State.h" 351e209fccSTamas Berghammer #include "lldb/Host/common/NativeBreakpoint.h" 360cbf0b13STamas Berghammer #include "lldb/Host/common/NativeRegisterContext.h" 37af245d11STodd Fiala #include "lldb/Host/Host.h" 3813b18261SZachary Turner #include "lldb/Host/HostInfo.h" 390cbf0b13STamas Berghammer #include "lldb/Host/HostNativeThread.h" 4039de3110SZachary Turner #include "lldb/Host/ThreadLauncher.h" 41af245d11STodd Fiala #include "lldb/Symbol/ObjectFile.h" 4290aff47cSZachary Turner #include "lldb/Target/Process.h" 43af245d11STodd Fiala #include "lldb/Target/ProcessLaunchInfo.h" 44c16f5dcaSChaoren Lin #include "lldb/Utility/LLDBAssert.h" 45af245d11STodd Fiala #include "lldb/Utility/PseudoTerminal.h" 46af245d11STodd Fiala 471e209fccSTamas Berghammer #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" 48af245d11STodd Fiala #include "Plugins/Process/Utility/LinuxSignals.h" 491e209fccSTamas Berghammer #include "Utility/StringExtractor.h" 50af245d11STodd Fiala #include "NativeThreadLinux.h" 51af245d11STodd Fiala #include "ProcFileReader.h" 521e209fccSTamas Berghammer #include "Procfs.h" 53cacde7dfSTodd Fiala 54d858487eSTamas Berghammer // System includes - They have to be included after framework includes because they define some 55d858487eSTamas Berghammer // macros which collide with variable names in other modules 56d858487eSTamas Berghammer #include <linux/unistd.h> 57d858487eSTamas Berghammer #include <sys/personality.h> 58d858487eSTamas Berghammer #include <sys/ptrace.h> 59d858487eSTamas Berghammer #include <sys/socket.h> 601107b5a5SPavel Labath #include <sys/signalfd.h> 61d858487eSTamas Berghammer #include <sys/syscall.h> 62d858487eSTamas Berghammer #include <sys/types.h> 63d858487eSTamas Berghammer #include <sys/uio.h> 64d858487eSTamas Berghammer #include <sys/user.h> 65d858487eSTamas Berghammer #include <sys/wait.h> 66d858487eSTamas Berghammer 671e209fccSTamas Berghammer #if defined (__arm64__) || defined (__aarch64__) 681e209fccSTamas Berghammer // NT_PRSTATUS and NT_FPREGSET definition 691e209fccSTamas Berghammer #include <elf.h> 701e209fccSTamas Berghammer #endif 711e209fccSTamas Berghammer 72cacde7dfSTodd Fiala #ifdef __ANDROID__ 73cacde7dfSTodd Fiala #define __ptrace_request int 74cacde7dfSTodd Fiala #define PT_DETACH PTRACE_DETACH 75cacde7dfSTodd Fiala #endif 76af245d11STodd Fiala 77af245d11STodd Fiala #define DEBUG_PTRACE_MAXBYTES 20 78af245d11STodd Fiala 79af245d11STodd Fiala // Support ptrace extensions even when compiled without required kernel support 80dda61943STodd Fiala #ifndef PT_GETREGS 81af245d11STodd Fiala #ifndef PTRACE_GETREGS 82af245d11STodd Fiala #define PTRACE_GETREGS 12 83af245d11STodd Fiala #endif 84dda61943STodd Fiala #endif 85dda61943STodd Fiala #ifndef PT_SETREGS 86af245d11STodd Fiala #ifndef PTRACE_SETREGS 87af245d11STodd Fiala #define PTRACE_SETREGS 13 88af245d11STodd Fiala #endif 89dda61943STodd Fiala #endif 90dda61943STodd Fiala #ifndef PT_GETFPREGS 91dda61943STodd Fiala #ifndef PTRACE_GETFPREGS 92dda61943STodd Fiala #define PTRACE_GETFPREGS 14 93dda61943STodd Fiala #endif 94dda61943STodd Fiala #endif 95dda61943STodd Fiala #ifndef PT_SETFPREGS 96dda61943STodd Fiala #ifndef PTRACE_SETFPREGS 97dda61943STodd Fiala #define PTRACE_SETFPREGS 15 98dda61943STodd Fiala #endif 99dda61943STodd Fiala #endif 100af245d11STodd Fiala #ifndef PTRACE_GETREGSET 101af245d11STodd Fiala #define PTRACE_GETREGSET 0x4204 102af245d11STodd Fiala #endif 103af245d11STodd Fiala #ifndef PTRACE_SETREGSET 104af245d11STodd Fiala #define PTRACE_SETREGSET 0x4205 105af245d11STodd Fiala #endif 106af245d11STodd Fiala #ifndef PTRACE_GET_THREAD_AREA 107af245d11STodd Fiala #define PTRACE_GET_THREAD_AREA 25 108af245d11STodd Fiala #endif 109af245d11STodd Fiala #ifndef PTRACE_ARCH_PRCTL 110af245d11STodd Fiala #define PTRACE_ARCH_PRCTL 30 111af245d11STodd Fiala #endif 112af245d11STodd Fiala #ifndef ARCH_GET_FS 113af245d11STodd Fiala #define ARCH_SET_GS 0x1001 114af245d11STodd Fiala #define ARCH_SET_FS 0x1002 115af245d11STodd Fiala #define ARCH_GET_FS 0x1003 116af245d11STodd Fiala #define ARCH_GET_GS 0x1004 117af245d11STodd Fiala #endif 118af245d11STodd Fiala 1190bce1b67STodd Fiala #define LLDB_PERSONALITY_GET_CURRENT_SETTINGS 0xffffffff 120af245d11STodd Fiala 121af245d11STodd Fiala // Support hardware breakpoints in case it has not been defined 122af245d11STodd Fiala #ifndef TRAP_HWBKPT 123af245d11STodd Fiala #define TRAP_HWBKPT 4 124af245d11STodd Fiala #endif 125af245d11STodd Fiala 126af245d11STodd Fiala // Try to define a macro to encapsulate the tgkill syscall 127af245d11STodd Fiala // fall back on kill() if tgkill isn't available 128c9346597SChaoren Lin #define tgkill(pid, tid, sig) \ 129c9346597SChaoren Lin syscall(SYS_tgkill, static_cast<::pid_t>(pid), static_cast<::pid_t>(tid), sig) 130af245d11STodd Fiala 131af245d11STodd Fiala // We disable the tracing of ptrace calls for integration builds to 132af245d11STodd Fiala // avoid the additional indirection and checks. 133af245d11STodd Fiala #ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION 13497ccc294SChaoren Lin #define PTRACE(req, pid, addr, data, data_size, error) \ 13597ccc294SChaoren Lin PtraceWrapper((req), (pid), (addr), (data), (data_size), (error), #req, __FILE__, __LINE__) 136af245d11STodd Fiala #else 13797ccc294SChaoren Lin #define PTRACE(req, pid, addr, data, data_size, error) \ 13897ccc294SChaoren Lin PtraceWrapper((req), (pid), (addr), (data), (data_size), (error)) 139af245d11STodd Fiala #endif 140af245d11STodd Fiala 1417cb18bf5STamas Berghammer using namespace lldb; 1427cb18bf5STamas Berghammer using namespace lldb_private; 143db264a6dSTamas Berghammer using namespace lldb_private::process_linux; 1447cb18bf5STamas Berghammer using namespace llvm; 1457cb18bf5STamas Berghammer 146af245d11STodd Fiala // Private bits we only need internally. 147af245d11STodd Fiala namespace 148af245d11STodd Fiala { 149af245d11STodd Fiala const UnixSignals& 150af245d11STodd Fiala GetUnixSignals () 151af245d11STodd Fiala { 152af245d11STodd Fiala static process_linux::LinuxSignals signals; 153af245d11STodd Fiala return signals; 154af245d11STodd Fiala } 155af245d11STodd Fiala 156c076559aSPavel Labath NativeProcessLinux::LogFunction 157fa03ad2eSChaoren Lin GetThreadLoggerFunction () 158fa03ad2eSChaoren Lin { 159fa03ad2eSChaoren Lin return [](const char *format, va_list args) 160fa03ad2eSChaoren Lin { 161fa03ad2eSChaoren Lin Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 162fa03ad2eSChaoren Lin if (log) 163fa03ad2eSChaoren Lin log->VAPrintf (format, args); 164fa03ad2eSChaoren Lin }; 165fa03ad2eSChaoren Lin } 166fa03ad2eSChaoren Lin 167fa03ad2eSChaoren Lin void 168fa03ad2eSChaoren Lin CoordinatorErrorHandler (const std::string &error_message) 169fa03ad2eSChaoren Lin { 170fa03ad2eSChaoren Lin Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 171fa03ad2eSChaoren Lin if (log) 17286fd8e45SChaoren Lin log->Printf ("NativeProcessLinux::%s %s", __FUNCTION__, error_message.c_str ()); 173c076559aSPavel Labath assert (false && "NativeProcessLinux error reported"); 174fa03ad2eSChaoren Lin } 175fa03ad2eSChaoren Lin 176af245d11STodd Fiala Error 177af245d11STodd Fiala ResolveProcessArchitecture (lldb::pid_t pid, Platform &platform, ArchSpec &arch) 178af245d11STodd Fiala { 179af245d11STodd Fiala // Grab process info for the running process. 180af245d11STodd Fiala ProcessInstanceInfo process_info; 181af245d11STodd Fiala if (!platform.GetProcessInfo (pid, process_info)) 182db264a6dSTamas Berghammer return Error("failed to get process info"); 183af245d11STodd Fiala 184af245d11STodd Fiala // Resolve the executable module. 185af245d11STodd Fiala ModuleSP exe_module_sp; 186e56f6dceSChaoren Lin ModuleSpec exe_module_spec(process_info.GetExecutableFile(), process_info.GetArchitecture()); 187af245d11STodd Fiala FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths ()); 188af245d11STodd Fiala Error error = platform.ResolveExecutable( 18954539338SOleksiy Vyalov exe_module_spec, 190af245d11STodd Fiala exe_module_sp, 191af245d11STodd Fiala executable_search_paths.GetSize () ? &executable_search_paths : NULL); 192af245d11STodd Fiala 193af245d11STodd Fiala if (!error.Success ()) 194af245d11STodd Fiala return error; 195af245d11STodd Fiala 196af245d11STodd Fiala // Check if we've got our architecture from the exe_module. 197af245d11STodd Fiala arch = exe_module_sp->GetArchitecture (); 198af245d11STodd Fiala if (arch.IsValid ()) 199af245d11STodd Fiala return Error(); 200af245d11STodd Fiala else 201af245d11STodd Fiala return Error("failed to retrieve a valid architecture from the exe module"); 202af245d11STodd Fiala } 203af245d11STodd Fiala 204af245d11STodd Fiala void 205db264a6dSTamas Berghammer DisplayBytes (StreamString &s, void *bytes, uint32_t count) 206af245d11STodd Fiala { 207af245d11STodd Fiala uint8_t *ptr = (uint8_t *)bytes; 208af245d11STodd Fiala const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count); 209af245d11STodd Fiala for(uint32_t i=0; i<loop_count; i++) 210af245d11STodd Fiala { 211af245d11STodd Fiala s.Printf ("[%x]", *ptr); 212af245d11STodd Fiala ptr++; 213af245d11STodd Fiala } 214af245d11STodd Fiala } 215af245d11STodd Fiala 216af245d11STodd Fiala void 217af245d11STodd Fiala PtraceDisplayBytes(int &req, void *data, size_t data_size) 218af245d11STodd Fiala { 219af245d11STodd Fiala StreamString buf; 220af245d11STodd Fiala Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet ( 221af245d11STodd Fiala POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE)); 222af245d11STodd Fiala 223af245d11STodd Fiala if (verbose_log) 224af245d11STodd Fiala { 225af245d11STodd Fiala switch(req) 226af245d11STodd Fiala { 227af245d11STodd Fiala case PTRACE_POKETEXT: 228af245d11STodd Fiala { 229af245d11STodd Fiala DisplayBytes(buf, &data, 8); 230af245d11STodd Fiala verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData()); 231af245d11STodd Fiala break; 232af245d11STodd Fiala } 233af245d11STodd Fiala case PTRACE_POKEDATA: 234af245d11STodd Fiala { 235af245d11STodd Fiala DisplayBytes(buf, &data, 8); 236af245d11STodd Fiala verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData()); 237af245d11STodd Fiala break; 238af245d11STodd Fiala } 239af245d11STodd Fiala case PTRACE_POKEUSER: 240af245d11STodd Fiala { 241af245d11STodd Fiala DisplayBytes(buf, &data, 8); 242af245d11STodd Fiala verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData()); 243af245d11STodd Fiala break; 244af245d11STodd Fiala } 245af245d11STodd Fiala case PTRACE_SETREGS: 246af245d11STodd Fiala { 247af245d11STodd Fiala DisplayBytes(buf, data, data_size); 248af245d11STodd Fiala verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData()); 249af245d11STodd Fiala break; 250af245d11STodd Fiala } 251af245d11STodd Fiala case PTRACE_SETFPREGS: 252af245d11STodd Fiala { 253af245d11STodd Fiala DisplayBytes(buf, data, data_size); 254af245d11STodd Fiala verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData()); 255af245d11STodd Fiala break; 256af245d11STodd Fiala } 257af245d11STodd Fiala case PTRACE_SETSIGINFO: 258af245d11STodd Fiala { 259af245d11STodd Fiala DisplayBytes(buf, data, sizeof(siginfo_t)); 260af245d11STodd Fiala verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData()); 261af245d11STodd Fiala break; 262af245d11STodd Fiala } 263af245d11STodd Fiala case PTRACE_SETREGSET: 264af245d11STodd Fiala { 265af245d11STodd Fiala // Extract iov_base from data, which is a pointer to the struct IOVEC 266af245d11STodd Fiala DisplayBytes(buf, *(void **)data, data_size); 267af245d11STodd Fiala verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData()); 268af245d11STodd Fiala break; 269af245d11STodd Fiala } 270af245d11STodd Fiala default: 271af245d11STodd Fiala { 272af245d11STodd Fiala } 273af245d11STodd Fiala } 274af245d11STodd Fiala } 275af245d11STodd Fiala } 276af245d11STodd Fiala 277af245d11STodd Fiala // Wrapper for ptrace to catch errors and log calls. 278af245d11STodd Fiala // Note that ptrace sets errno on error because -1 can be a valid result (i.e. for PTRACE_PEEK*) 279af245d11STodd Fiala long 28097ccc294SChaoren Lin PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, Error& error, 281af245d11STodd Fiala const char* reqName, const char* file, int line) 282af245d11STodd Fiala { 283af245d11STodd Fiala long int result; 284af245d11STodd Fiala 285af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE)); 286af245d11STodd Fiala 287af245d11STodd Fiala PtraceDisplayBytes(req, data, data_size); 288af245d11STodd Fiala 28997ccc294SChaoren Lin error.Clear(); 290af245d11STodd Fiala errno = 0; 291af245d11STodd Fiala if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) 292af245d11STodd Fiala result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), *(unsigned int *)addr, data); 293af245d11STodd Fiala else 294af245d11STodd Fiala result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), addr, data); 295af245d11STodd Fiala 29697ccc294SChaoren Lin if (result == -1) 29797ccc294SChaoren Lin error.SetErrorToErrno(); 29897ccc294SChaoren Lin 299af245d11STodd Fiala if (log) 300af245d11STodd Fiala log->Printf("ptrace(%s, %" PRIu64 ", %p, %p, %zu)=%lX called from file %s line %d", 301af245d11STodd Fiala reqName, pid, addr, data, data_size, result, file, line); 302af245d11STodd Fiala 303af245d11STodd Fiala PtraceDisplayBytes(req, data, data_size); 304af245d11STodd Fiala 30597ccc294SChaoren Lin if (log && error.GetError() != 0) 306af245d11STodd Fiala { 307af245d11STodd Fiala const char* str; 30897ccc294SChaoren Lin switch (error.GetError()) 309af245d11STodd Fiala { 310af245d11STodd Fiala case ESRCH: str = "ESRCH"; break; 311af245d11STodd Fiala case EINVAL: str = "EINVAL"; break; 312af245d11STodd Fiala case EBUSY: str = "EBUSY"; break; 313af245d11STodd Fiala case EPERM: str = "EPERM"; break; 31497ccc294SChaoren Lin default: str = error.AsCString(); 315af245d11STodd Fiala } 31697ccc294SChaoren Lin log->Printf("ptrace() failed; errno=%d (%s)", error.GetError(), str); 317af245d11STodd Fiala } 318af245d11STodd Fiala 319af245d11STodd Fiala return result; 320af245d11STodd Fiala } 321af245d11STodd Fiala 322af245d11STodd Fiala #ifdef LLDB_CONFIGURATION_BUILDANDINTEGRATION 323af245d11STodd Fiala // Wrapper for ptrace when logging is not required. 324af245d11STodd Fiala // Sets errno to 0 prior to calling ptrace. 325af245d11STodd Fiala long 32697ccc294SChaoren Lin PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, Error& error) 327af245d11STodd Fiala { 328af245d11STodd Fiala long result = 0; 32997ccc294SChaoren Lin 33097ccc294SChaoren Lin error.Clear(); 331af245d11STodd Fiala errno = 0; 332af245d11STodd Fiala if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) 333af245d11STodd Fiala result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), *(unsigned int *)addr, data); 334af245d11STodd Fiala else 335af245d11STodd Fiala result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), addr, data); 33697ccc294SChaoren Lin 33797ccc294SChaoren Lin if (result == -1) 33897ccc294SChaoren Lin error.SetErrorToErrno(); 339af245d11STodd Fiala return result; 340af245d11STodd Fiala } 341af245d11STodd Fiala #endif 342af245d11STodd Fiala 343af245d11STodd Fiala //------------------------------------------------------------------------------ 344af245d11STodd Fiala // Static implementations of NativeProcessLinux::ReadMemory and 345af245d11STodd Fiala // NativeProcessLinux::WriteMemory. This enables mutual recursion between these 346af245d11STodd Fiala // functions without needed to go thru the thread funnel. 347af245d11STodd Fiala 3483eb4b458SChaoren Lin size_t 349af245d11STodd Fiala DoReadMemory( 350af245d11STodd Fiala lldb::pid_t pid, 351af245d11STodd Fiala lldb::addr_t vm_addr, 352af245d11STodd Fiala void *buf, 3533eb4b458SChaoren Lin size_t size, 354af245d11STodd Fiala Error &error) 355af245d11STodd Fiala { 356af245d11STodd Fiala // ptrace word size is determined by the host, not the child 357af245d11STodd Fiala static const unsigned word_size = sizeof(void*); 358af245d11STodd Fiala unsigned char *dst = static_cast<unsigned char*>(buf); 3593eb4b458SChaoren Lin size_t bytes_read; 3603eb4b458SChaoren Lin size_t remainder; 361af245d11STodd Fiala long data; 362af245d11STodd Fiala 363af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); 364af245d11STodd Fiala if (log) 365af245d11STodd Fiala ProcessPOSIXLog::IncNestLevel(); 366af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) 367af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__, 368af245d11STodd Fiala pid, word_size, (void*)vm_addr, buf, size); 369af245d11STodd Fiala 370af245d11STodd Fiala assert(sizeof(data) >= word_size); 371af245d11STodd Fiala for (bytes_read = 0; bytes_read < size; bytes_read += remainder) 372af245d11STodd Fiala { 37397ccc294SChaoren Lin data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, nullptr, 0, error); 37497ccc294SChaoren Lin if (error.Fail()) 375af245d11STodd Fiala { 376af245d11STodd Fiala if (log) 377af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 378af245d11STodd Fiala return bytes_read; 379af245d11STodd Fiala } 380af245d11STodd Fiala 381af245d11STodd Fiala remainder = size - bytes_read; 382af245d11STodd Fiala remainder = remainder > word_size ? word_size : remainder; 383af245d11STodd Fiala 384af245d11STodd Fiala // Copy the data into our buffer 385af245d11STodd Fiala for (unsigned i = 0; i < remainder; ++i) 386af245d11STodd Fiala dst[i] = ((data >> i*8) & 0xFF); 387af245d11STodd Fiala 388af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && 389af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 390af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 391af245d11STodd Fiala size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 392af245d11STodd Fiala { 393af245d11STodd Fiala uintptr_t print_dst = 0; 394af245d11STodd Fiala // Format bytes from data by moving into print_dst for log output 395af245d11STodd Fiala for (unsigned i = 0; i < remainder; ++i) 396af245d11STodd Fiala print_dst |= (((data >> i*8) & 0xFF) << i*8); 397af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 398af245d11STodd Fiala (void*)vm_addr, print_dst, (unsigned long)data); 399af245d11STodd Fiala } 400af245d11STodd Fiala 401af245d11STodd Fiala vm_addr += word_size; 402af245d11STodd Fiala dst += word_size; 403af245d11STodd Fiala } 404af245d11STodd Fiala 405af245d11STodd Fiala if (log) 406af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 407af245d11STodd Fiala return bytes_read; 408af245d11STodd Fiala } 409af245d11STodd Fiala 4103eb4b458SChaoren Lin size_t 411af245d11STodd Fiala DoWriteMemory( 412af245d11STodd Fiala lldb::pid_t pid, 413af245d11STodd Fiala lldb::addr_t vm_addr, 414af245d11STodd Fiala const void *buf, 4153eb4b458SChaoren Lin size_t size, 416af245d11STodd Fiala Error &error) 417af245d11STodd Fiala { 418af245d11STodd Fiala // ptrace word size is determined by the host, not the child 419af245d11STodd Fiala static const unsigned word_size = sizeof(void*); 420af245d11STodd Fiala const unsigned char *src = static_cast<const unsigned char*>(buf); 4213eb4b458SChaoren Lin size_t bytes_written = 0; 4223eb4b458SChaoren Lin size_t remainder; 423af245d11STodd Fiala 424af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); 425af245d11STodd Fiala if (log) 426af245d11STodd Fiala ProcessPOSIXLog::IncNestLevel(); 427af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) 428af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(%" PRIu64 ", %u, %p, %p, %" PRIu64 ")", __FUNCTION__, 429af245d11STodd Fiala pid, word_size, (void*)vm_addr, buf, size); 430af245d11STodd Fiala 431af245d11STodd Fiala for (bytes_written = 0; bytes_written < size; bytes_written += remainder) 432af245d11STodd Fiala { 433af245d11STodd Fiala remainder = size - bytes_written; 434af245d11STodd Fiala remainder = remainder > word_size ? word_size : remainder; 435af245d11STodd Fiala 436af245d11STodd Fiala if (remainder == word_size) 437af245d11STodd Fiala { 438af245d11STodd Fiala unsigned long data = 0; 439af245d11STodd Fiala assert(sizeof(data) >= word_size); 440af245d11STodd Fiala for (unsigned i = 0; i < word_size; ++i) 441af245d11STodd Fiala data |= (unsigned long)src[i] << i*8; 442af245d11STodd Fiala 443af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && 444af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 445af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 446af245d11STodd Fiala size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 447af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 4481e209fccSTamas Berghammer (void*)vm_addr, *(const unsigned long*)src, data); 449af245d11STodd Fiala 45097ccc294SChaoren Lin if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0, error)) 451af245d11STodd Fiala { 452af245d11STodd Fiala if (log) 453af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 454af245d11STodd Fiala return bytes_written; 455af245d11STodd Fiala } 456af245d11STodd Fiala } 457af245d11STodd Fiala else 458af245d11STodd Fiala { 459af245d11STodd Fiala unsigned char buff[8]; 460af245d11STodd Fiala if (DoReadMemory(pid, vm_addr, 461af245d11STodd Fiala buff, word_size, error) != word_size) 462af245d11STodd Fiala { 463af245d11STodd Fiala if (log) 464af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 465af245d11STodd Fiala return bytes_written; 466af245d11STodd Fiala } 467af245d11STodd Fiala 468af245d11STodd Fiala memcpy(buff, src, remainder); 469af245d11STodd Fiala 470af245d11STodd Fiala if (DoWriteMemory(pid, vm_addr, 471af245d11STodd Fiala buff, word_size, error) != word_size) 472af245d11STodd Fiala { 473af245d11STodd Fiala if (log) 474af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 475af245d11STodd Fiala return bytes_written; 476af245d11STodd Fiala } 477af245d11STodd Fiala 478af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && 479af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 480af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 481af245d11STodd Fiala size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 482af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 4831e209fccSTamas Berghammer (void*)vm_addr, *(const unsigned long*)src, *(unsigned long*)buff); 484af245d11STodd Fiala } 485af245d11STodd Fiala 486af245d11STodd Fiala vm_addr += word_size; 487af245d11STodd Fiala src += word_size; 488af245d11STodd Fiala } 489af245d11STodd Fiala if (log) 490af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 491af245d11STodd Fiala return bytes_written; 492af245d11STodd Fiala } 493af245d11STodd Fiala 494af245d11STodd Fiala //------------------------------------------------------------------------------ 495af245d11STodd Fiala /// @class Operation 496af245d11STodd Fiala /// @brief Represents a NativeProcessLinux operation. 497af245d11STodd Fiala /// 498af245d11STodd Fiala /// Under Linux, it is not possible to ptrace() from any other thread but the 499af245d11STodd Fiala /// one that spawned or attached to the process from the start. Therefore, when 500af245d11STodd Fiala /// a NativeProcessLinux is asked to deliver or change the state of an inferior 501af245d11STodd Fiala /// process the operation must be "funneled" to a specific thread to perform the 502af245d11STodd Fiala /// task. The Operation class provides an abstract base for all services the 503af245d11STodd Fiala /// NativeProcessLinux must perform via the single virtual function Execute, thus 504af245d11STodd Fiala /// encapsulating the code that needs to run in the privileged context. 505af245d11STodd Fiala class Operation 506af245d11STodd Fiala { 507af245d11STodd Fiala public: 508af245d11STodd Fiala Operation () : m_error() { } 509af245d11STodd Fiala 510af245d11STodd Fiala virtual 511af245d11STodd Fiala ~Operation() {} 512af245d11STodd Fiala 513af245d11STodd Fiala virtual void 514af245d11STodd Fiala Execute (NativeProcessLinux *process) = 0; 515af245d11STodd Fiala 516af245d11STodd Fiala const Error & 517af245d11STodd Fiala GetError () const { return m_error; } 518af245d11STodd Fiala 519af245d11STodd Fiala protected: 520af245d11STodd Fiala Error m_error; 521af245d11STodd Fiala }; 522af245d11STodd Fiala 523af245d11STodd Fiala //------------------------------------------------------------------------------ 524af245d11STodd Fiala /// @class ReadOperation 525af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadMemory. 526af245d11STodd Fiala class ReadOperation : public Operation 527af245d11STodd Fiala { 528af245d11STodd Fiala public: 529af245d11STodd Fiala ReadOperation( 530af245d11STodd Fiala lldb::addr_t addr, 531af245d11STodd Fiala void *buff, 5323eb4b458SChaoren Lin size_t size, 5333eb4b458SChaoren Lin size_t &result) : 534af245d11STodd Fiala Operation (), 535af245d11STodd Fiala m_addr (addr), 536af245d11STodd Fiala m_buff (buff), 537af245d11STodd Fiala m_size (size), 538af245d11STodd Fiala m_result (result) 539af245d11STodd Fiala { 540af245d11STodd Fiala } 541af245d11STodd Fiala 542af245d11STodd Fiala void Execute (NativeProcessLinux *process) override; 543af245d11STodd Fiala 544af245d11STodd Fiala private: 545af245d11STodd Fiala lldb::addr_t m_addr; 546af245d11STodd Fiala void *m_buff; 5473eb4b458SChaoren Lin size_t m_size; 5483eb4b458SChaoren Lin size_t &m_result; 549af245d11STodd Fiala }; 550af245d11STodd Fiala 551af245d11STodd Fiala void 552af245d11STodd Fiala ReadOperation::Execute (NativeProcessLinux *process) 553af245d11STodd Fiala { 554af245d11STodd Fiala m_result = DoReadMemory (process->GetID (), m_addr, m_buff, m_size, m_error); 555af245d11STodd Fiala } 556af245d11STodd Fiala 557af245d11STodd Fiala //------------------------------------------------------------------------------ 558af245d11STodd Fiala /// @class WriteOperation 559af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteMemory. 560af245d11STodd Fiala class WriteOperation : public Operation 561af245d11STodd Fiala { 562af245d11STodd Fiala public: 563af245d11STodd Fiala WriteOperation( 564af245d11STodd Fiala lldb::addr_t addr, 565af245d11STodd Fiala const void *buff, 5663eb4b458SChaoren Lin size_t size, 5673eb4b458SChaoren Lin size_t &result) : 568af245d11STodd Fiala Operation (), 569af245d11STodd Fiala m_addr (addr), 570af245d11STodd Fiala m_buff (buff), 571af245d11STodd Fiala m_size (size), 572af245d11STodd Fiala m_result (result) 573af245d11STodd Fiala { 574af245d11STodd Fiala } 575af245d11STodd Fiala 576af245d11STodd Fiala void Execute (NativeProcessLinux *process) override; 577af245d11STodd Fiala 578af245d11STodd Fiala private: 579af245d11STodd Fiala lldb::addr_t m_addr; 580af245d11STodd Fiala const void *m_buff; 5813eb4b458SChaoren Lin size_t m_size; 5823eb4b458SChaoren Lin size_t &m_result; 583af245d11STodd Fiala }; 584af245d11STodd Fiala 585af245d11STodd Fiala void 586af245d11STodd Fiala WriteOperation::Execute(NativeProcessLinux *process) 587af245d11STodd Fiala { 588af245d11STodd Fiala m_result = DoWriteMemory (process->GetID (), m_addr, m_buff, m_size, m_error); 589af245d11STodd Fiala } 590af245d11STodd Fiala 591af245d11STodd Fiala //------------------------------------------------------------------------------ 592af245d11STodd Fiala /// @class ReadRegOperation 593af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadRegisterValue. 594af245d11STodd Fiala class ReadRegOperation : public Operation 595af245d11STodd Fiala { 596af245d11STodd Fiala public: 597af245d11STodd Fiala ReadRegOperation(lldb::tid_t tid, uint32_t offset, const char *reg_name, 59897ccc294SChaoren Lin RegisterValue &value) 59997ccc294SChaoren Lin : m_tid(tid), 60097ccc294SChaoren Lin m_offset(static_cast<uintptr_t> (offset)), 60197ccc294SChaoren Lin m_reg_name(reg_name), 60297ccc294SChaoren Lin m_value(value) 603af245d11STodd Fiala { } 604af245d11STodd Fiala 605d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 606af245d11STodd Fiala 607af245d11STodd Fiala private: 608af245d11STodd Fiala lldb::tid_t m_tid; 609af245d11STodd Fiala uintptr_t m_offset; 610af245d11STodd Fiala const char *m_reg_name; 611af245d11STodd Fiala RegisterValue &m_value; 612af245d11STodd Fiala }; 613af245d11STodd Fiala 614af245d11STodd Fiala void 615af245d11STodd Fiala ReadRegOperation::Execute(NativeProcessLinux *monitor) 616af245d11STodd Fiala { 6170fceef80STodd Fiala #if defined (__arm64__) || defined (__aarch64__) 6180fceef80STodd Fiala if (m_offset > sizeof(struct user_pt_regs)) 6190fceef80STodd Fiala { 6200fceef80STodd Fiala uintptr_t offset = m_offset - sizeof(struct user_pt_regs); 6210fceef80STodd Fiala if (offset > sizeof(struct user_fpsimd_state)) 6220fceef80STodd Fiala { 62397ccc294SChaoren Lin m_error.SetErrorString("invalid offset value"); 62497ccc294SChaoren Lin return; 6250fceef80STodd Fiala } 6260fceef80STodd Fiala elf_fpregset_t regs; 6270fceef80STodd Fiala int regset = NT_FPREGSET; 6280fceef80STodd Fiala struct iovec ioVec; 6290fceef80STodd Fiala 6300fceef80STodd Fiala ioVec.iov_base = ®s; 6310fceef80STodd Fiala ioVec.iov_len = sizeof regs; 63297ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 63397ccc294SChaoren Lin if (m_error.Success()) 6340fceef80STodd Fiala { 635db264a6dSTamas Berghammer ArchSpec arch; 6360fceef80STodd Fiala if (monitor->GetArchitecture(arch)) 6370fceef80STodd Fiala m_value.SetBytes((void *)(((unsigned char *)(®s)) + offset), 16, arch.GetByteOrder()); 6380fceef80STodd Fiala else 63997ccc294SChaoren Lin m_error.SetErrorString("failed to get architecture"); 6400fceef80STodd Fiala } 6410fceef80STodd Fiala } 6420fceef80STodd Fiala else 6430fceef80STodd Fiala { 6440fceef80STodd Fiala elf_gregset_t regs; 6450fceef80STodd Fiala int regset = NT_PRSTATUS; 6460fceef80STodd Fiala struct iovec ioVec; 6470fceef80STodd Fiala 6480fceef80STodd Fiala ioVec.iov_base = ®s; 6490fceef80STodd Fiala ioVec.iov_len = sizeof regs; 65097ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 65197ccc294SChaoren Lin if (m_error.Success()) 6520fceef80STodd Fiala { 653db264a6dSTamas Berghammer ArchSpec arch; 6540fceef80STodd Fiala if (monitor->GetArchitecture(arch)) 6550fceef80STodd Fiala m_value.SetBytes((void *)(((unsigned char *)(regs)) + m_offset), 8, arch.GetByteOrder()); 65697ccc294SChaoren Lin else 65797ccc294SChaoren Lin m_error.SetErrorString("failed to get architecture"); 6580fceef80STodd Fiala } 6590fceef80STodd Fiala } 66009ba1a32SMohit K. Bhakkad #elif defined (__mips__) 66109ba1a32SMohit K. Bhakkad elf_gregset_t regs; 66209ba1a32SMohit K. Bhakkad PTRACE(PTRACE_GETREGS, m_tid, NULL, ®s, sizeof regs, m_error); 66309ba1a32SMohit K. Bhakkad if (m_error.Success()) 66409ba1a32SMohit K. Bhakkad { 66509ba1a32SMohit K. Bhakkad lldb_private::ArchSpec arch; 66609ba1a32SMohit K. Bhakkad if (monitor->GetArchitecture(arch)) 66709ba1a32SMohit K. Bhakkad m_value.SetBytes((void *)(((unsigned char *)(regs)) + m_offset), 8, arch.GetByteOrder()); 66809ba1a32SMohit K. Bhakkad else 66909ba1a32SMohit K. Bhakkad m_error.SetErrorString("failed to get architecture"); 67009ba1a32SMohit K. Bhakkad } 6710fceef80STodd Fiala #else 672af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS)); 673af245d11STodd Fiala 674adf8adbdSTamas Berghammer lldb::addr_t data = static_cast<unsigned long>(PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, nullptr, 0, m_error)); 67597ccc294SChaoren Lin if (m_error.Success()) 676af245d11STodd Fiala m_value = data; 67797ccc294SChaoren Lin 678af245d11STodd Fiala if (log) 679af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() reg %s: 0x%" PRIx64, __FUNCTION__, 680af245d11STodd Fiala m_reg_name, data); 6810fceef80STodd Fiala #endif 682af245d11STodd Fiala } 683af245d11STodd Fiala 684af245d11STodd Fiala //------------------------------------------------------------------------------ 685af245d11STodd Fiala /// @class WriteRegOperation 686af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteRegisterValue. 687af245d11STodd Fiala class WriteRegOperation : public Operation 688af245d11STodd Fiala { 689af245d11STodd Fiala public: 690af245d11STodd Fiala WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name, 69197ccc294SChaoren Lin const RegisterValue &value) 69297ccc294SChaoren Lin : m_tid(tid), 69397ccc294SChaoren Lin m_offset(offset), 69497ccc294SChaoren Lin m_reg_name(reg_name), 69597ccc294SChaoren Lin m_value(value) 696af245d11STodd Fiala { } 697af245d11STodd Fiala 698d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 699af245d11STodd Fiala 700af245d11STodd Fiala private: 701af245d11STodd Fiala lldb::tid_t m_tid; 702af245d11STodd Fiala uintptr_t m_offset; 703af245d11STodd Fiala const char *m_reg_name; 704af245d11STodd Fiala const RegisterValue &m_value; 705af245d11STodd Fiala }; 706af245d11STodd Fiala 707af245d11STodd Fiala void 708af245d11STodd Fiala WriteRegOperation::Execute(NativeProcessLinux *monitor) 709af245d11STodd Fiala { 7100fceef80STodd Fiala #if defined (__arm64__) || defined (__aarch64__) 7110fceef80STodd Fiala if (m_offset > sizeof(struct user_pt_regs)) 7120fceef80STodd Fiala { 7130fceef80STodd Fiala uintptr_t offset = m_offset - sizeof(struct user_pt_regs); 7140fceef80STodd Fiala if (offset > sizeof(struct user_fpsimd_state)) 7150fceef80STodd Fiala { 71697ccc294SChaoren Lin m_error.SetErrorString("invalid offset value"); 71797ccc294SChaoren Lin return; 7180fceef80STodd Fiala } 7190fceef80STodd Fiala elf_fpregset_t regs; 7200fceef80STodd Fiala int regset = NT_FPREGSET; 7210fceef80STodd Fiala struct iovec ioVec; 7220fceef80STodd Fiala 7230fceef80STodd Fiala ioVec.iov_base = ®s; 7240fceef80STodd Fiala ioVec.iov_len = sizeof regs; 72597ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 7269425b329SBhushan D. Attarde if (m_error.Success()) 7270fceef80STodd Fiala { 7280fceef80STodd Fiala ::memcpy((void *)(((unsigned char *)(®s)) + offset), m_value.GetBytes(), 16); 72997ccc294SChaoren Lin PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 7300fceef80STodd Fiala } 7310fceef80STodd Fiala } 7320fceef80STodd Fiala else 7330fceef80STodd Fiala { 7340fceef80STodd Fiala elf_gregset_t regs; 7350fceef80STodd Fiala int regset = NT_PRSTATUS; 7360fceef80STodd Fiala struct iovec ioVec; 7370fceef80STodd Fiala 7380fceef80STodd Fiala ioVec.iov_base = ®s; 7390fceef80STodd Fiala ioVec.iov_len = sizeof regs; 74097ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 7419425b329SBhushan D. Attarde if (m_error.Success()) 7420fceef80STodd Fiala { 7430fceef80STodd Fiala ::memcpy((void *)(((unsigned char *)(®s)) + m_offset), m_value.GetBytes(), 8); 74497ccc294SChaoren Lin PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 7450fceef80STodd Fiala } 7460fceef80STodd Fiala } 74709ba1a32SMohit K. Bhakkad #elif defined (__mips__) 74809ba1a32SMohit K. Bhakkad elf_gregset_t regs; 74909ba1a32SMohit K. Bhakkad PTRACE(PTRACE_GETREGS, m_tid, NULL, ®s, sizeof regs, m_error); 75009ba1a32SMohit K. Bhakkad if (m_error.Success()) 75109ba1a32SMohit K. Bhakkad { 75209ba1a32SMohit K. Bhakkad ::memcpy((void *)(((unsigned char *)(®s)) + m_offset), m_value.GetBytes(), 8); 75309ba1a32SMohit K. Bhakkad PTRACE(PTRACE_SETREGS, m_tid, NULL, ®s, sizeof regs, m_error); 75409ba1a32SMohit K. Bhakkad } 7550fceef80STodd Fiala #else 756af245d11STodd Fiala void* buf; 757af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS)); 758af245d11STodd Fiala 759af245d11STodd Fiala buf = (void*) m_value.GetAsUInt64(); 760af245d11STodd Fiala 761af245d11STodd Fiala if (log) 762af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf); 76397ccc294SChaoren Lin PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0, m_error); 7640fceef80STodd Fiala #endif 765af245d11STodd Fiala } 766af245d11STodd Fiala 767af245d11STodd Fiala //------------------------------------------------------------------------------ 768af245d11STodd Fiala /// @class ReadGPROperation 769af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadGPR. 770af245d11STodd Fiala class ReadGPROperation : public Operation 771af245d11STodd Fiala { 772af245d11STodd Fiala public: 77397ccc294SChaoren Lin ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size) 77497ccc294SChaoren Lin : m_tid(tid), m_buf(buf), m_buf_size(buf_size) 775af245d11STodd Fiala { } 776af245d11STodd Fiala 777d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 778af245d11STodd Fiala 779af245d11STodd Fiala private: 780af245d11STodd Fiala lldb::tid_t m_tid; 781af245d11STodd Fiala void *m_buf; 782af245d11STodd Fiala size_t m_buf_size; 783af245d11STodd Fiala }; 784af245d11STodd Fiala 785af245d11STodd Fiala void 786af245d11STodd Fiala ReadGPROperation::Execute(NativeProcessLinux *monitor) 787af245d11STodd Fiala { 7886ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 7896ac1be4bSTodd Fiala int regset = NT_PRSTATUS; 7906ac1be4bSTodd Fiala struct iovec ioVec; 7916ac1be4bSTodd Fiala 7926ac1be4bSTodd Fiala ioVec.iov_base = m_buf; 7936ac1be4bSTodd Fiala ioVec.iov_len = m_buf_size; 79497ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); 7956ac1be4bSTodd Fiala #else 79697ccc294SChaoren Lin PTRACE(PTRACE_GETREGS, m_tid, nullptr, m_buf, m_buf_size, m_error); 7976ac1be4bSTodd Fiala #endif 798af245d11STodd Fiala } 799af245d11STodd Fiala 800af245d11STodd Fiala //------------------------------------------------------------------------------ 801af245d11STodd Fiala /// @class ReadFPROperation 802af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadFPR. 803af245d11STodd Fiala class ReadFPROperation : public Operation 804af245d11STodd Fiala { 805af245d11STodd Fiala public: 80697ccc294SChaoren Lin ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size) 80797ccc294SChaoren Lin : m_tid(tid), 80897ccc294SChaoren Lin m_buf(buf), 80997ccc294SChaoren Lin m_buf_size(buf_size) 810af245d11STodd Fiala { } 811af245d11STodd Fiala 812d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 813af245d11STodd Fiala 814af245d11STodd Fiala private: 815af245d11STodd Fiala lldb::tid_t m_tid; 816af245d11STodd Fiala void *m_buf; 817af245d11STodd Fiala size_t m_buf_size; 818af245d11STodd Fiala }; 819af245d11STodd Fiala 820af245d11STodd Fiala void 821af245d11STodd Fiala ReadFPROperation::Execute(NativeProcessLinux *monitor) 822af245d11STodd Fiala { 8236ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 8246ac1be4bSTodd Fiala int regset = NT_FPREGSET; 8256ac1be4bSTodd Fiala struct iovec ioVec; 8266ac1be4bSTodd Fiala 8276ac1be4bSTodd Fiala ioVec.iov_base = m_buf; 8286ac1be4bSTodd Fiala ioVec.iov_len = m_buf_size; 8291e209fccSTamas Berghammer PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); 8306ac1be4bSTodd Fiala #else 83197ccc294SChaoren Lin PTRACE(PTRACE_GETFPREGS, m_tid, nullptr, m_buf, m_buf_size, m_error); 8326ac1be4bSTodd Fiala #endif 833af245d11STodd Fiala } 834af245d11STodd Fiala 835af245d11STodd Fiala //------------------------------------------------------------------------------ 836af245d11STodd Fiala /// @class ReadRegisterSetOperation 837af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadRegisterSet. 838af245d11STodd Fiala class ReadRegisterSetOperation : public Operation 839af245d11STodd Fiala { 840af245d11STodd Fiala public: 84197ccc294SChaoren Lin ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) 84297ccc294SChaoren Lin : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset) 843af245d11STodd Fiala { } 844af245d11STodd Fiala 845d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 846af245d11STodd Fiala 847af245d11STodd Fiala private: 848af245d11STodd Fiala lldb::tid_t m_tid; 849af245d11STodd Fiala void *m_buf; 850af245d11STodd Fiala size_t m_buf_size; 851af245d11STodd Fiala const unsigned int m_regset; 852af245d11STodd Fiala }; 853af245d11STodd Fiala 854af245d11STodd Fiala void 855af245d11STodd Fiala ReadRegisterSetOperation::Execute(NativeProcessLinux *monitor) 856af245d11STodd Fiala { 85797ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size, m_error); 858af245d11STodd Fiala } 859af245d11STodd Fiala 860af245d11STodd Fiala //------------------------------------------------------------------------------ 861af245d11STodd Fiala /// @class WriteGPROperation 862af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteGPR. 863af245d11STodd Fiala class WriteGPROperation : public Operation 864af245d11STodd Fiala { 865af245d11STodd Fiala public: 86697ccc294SChaoren Lin WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size) 86797ccc294SChaoren Lin : m_tid(tid), m_buf(buf), m_buf_size(buf_size) 868af245d11STodd Fiala { } 869af245d11STodd Fiala 870d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 871af245d11STodd Fiala 872af245d11STodd Fiala private: 873af245d11STodd Fiala lldb::tid_t m_tid; 874af245d11STodd Fiala void *m_buf; 875af245d11STodd Fiala size_t m_buf_size; 876af245d11STodd Fiala }; 877af245d11STodd Fiala 878af245d11STodd Fiala void 879af245d11STodd Fiala WriteGPROperation::Execute(NativeProcessLinux *monitor) 880af245d11STodd Fiala { 8816ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 8826ac1be4bSTodd Fiala int regset = NT_PRSTATUS; 8836ac1be4bSTodd Fiala struct iovec ioVec; 8846ac1be4bSTodd Fiala 8856ac1be4bSTodd Fiala ioVec.iov_base = m_buf; 8866ac1be4bSTodd Fiala ioVec.iov_len = m_buf_size; 88797ccc294SChaoren Lin PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); 8886ac1be4bSTodd Fiala #else 88997ccc294SChaoren Lin PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size, m_error); 8906ac1be4bSTodd Fiala #endif 891af245d11STodd Fiala } 892af245d11STodd Fiala 893af245d11STodd Fiala //------------------------------------------------------------------------------ 894af245d11STodd Fiala /// @class WriteFPROperation 895af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteFPR. 896af245d11STodd Fiala class WriteFPROperation : public Operation 897af245d11STodd Fiala { 898af245d11STodd Fiala public: 89997ccc294SChaoren Lin WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size) 90097ccc294SChaoren Lin : m_tid(tid), m_buf(buf), m_buf_size(buf_size) 901af245d11STodd Fiala { } 902af245d11STodd Fiala 903d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 904af245d11STodd Fiala 905af245d11STodd Fiala private: 906af245d11STodd Fiala lldb::tid_t m_tid; 907af245d11STodd Fiala void *m_buf; 908af245d11STodd Fiala size_t m_buf_size; 909af245d11STodd Fiala }; 910af245d11STodd Fiala 911af245d11STodd Fiala void 912af245d11STodd Fiala WriteFPROperation::Execute(NativeProcessLinux *monitor) 913af245d11STodd Fiala { 9146ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 9156ac1be4bSTodd Fiala int regset = NT_FPREGSET; 9166ac1be4bSTodd Fiala struct iovec ioVec; 9176ac1be4bSTodd Fiala 9186ac1be4bSTodd Fiala ioVec.iov_base = m_buf; 9196ac1be4bSTodd Fiala ioVec.iov_len = m_buf_size; 92097ccc294SChaoren Lin PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); 9216ac1be4bSTodd Fiala #else 92297ccc294SChaoren Lin PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size, m_error); 9236ac1be4bSTodd Fiala #endif 924af245d11STodd Fiala } 925af245d11STodd Fiala 926af245d11STodd Fiala //------------------------------------------------------------------------------ 927af245d11STodd Fiala /// @class WriteRegisterSetOperation 928af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteRegisterSet. 929af245d11STodd Fiala class WriteRegisterSetOperation : public Operation 930af245d11STodd Fiala { 931af245d11STodd Fiala public: 93297ccc294SChaoren Lin WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) 93397ccc294SChaoren Lin : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset) 934af245d11STodd Fiala { } 935af245d11STodd Fiala 936d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 937af245d11STodd Fiala 938af245d11STodd Fiala private: 939af245d11STodd Fiala lldb::tid_t m_tid; 940af245d11STodd Fiala void *m_buf; 941af245d11STodd Fiala size_t m_buf_size; 942af245d11STodd Fiala const unsigned int m_regset; 943af245d11STodd Fiala }; 944af245d11STodd Fiala 945af245d11STodd Fiala void 946af245d11STodd Fiala WriteRegisterSetOperation::Execute(NativeProcessLinux *monitor) 947af245d11STodd Fiala { 94897ccc294SChaoren Lin PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size, m_error); 949af245d11STodd Fiala } 950af245d11STodd Fiala 951af245d11STodd Fiala //------------------------------------------------------------------------------ 952af245d11STodd Fiala /// @class ResumeOperation 953af245d11STodd Fiala /// @brief Implements NativeProcessLinux::Resume. 954af245d11STodd Fiala class ResumeOperation : public Operation 955af245d11STodd Fiala { 956af245d11STodd Fiala public: 95797ccc294SChaoren Lin ResumeOperation(lldb::tid_t tid, uint32_t signo) : 95897ccc294SChaoren Lin m_tid(tid), m_signo(signo) { } 959af245d11STodd Fiala 960d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 961af245d11STodd Fiala 962af245d11STodd Fiala private: 963af245d11STodd Fiala lldb::tid_t m_tid; 964af245d11STodd Fiala uint32_t m_signo; 965af245d11STodd Fiala }; 966af245d11STodd Fiala 967af245d11STodd Fiala void 968af245d11STodd Fiala ResumeOperation::Execute(NativeProcessLinux *monitor) 969af245d11STodd Fiala { 970af245d11STodd Fiala intptr_t data = 0; 971af245d11STodd Fiala 972af245d11STodd Fiala if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) 973af245d11STodd Fiala data = m_signo; 974af245d11STodd Fiala 97597ccc294SChaoren Lin PTRACE(PTRACE_CONT, m_tid, nullptr, (void*)data, 0, m_error); 97697ccc294SChaoren Lin if (m_error.Fail()) 977af245d11STodd Fiala { 978af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 979af245d11STodd Fiala 980af245d11STodd Fiala if (log) 98197ccc294SChaoren Lin log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, m_error.AsCString()); 982af245d11STodd Fiala } 983af245d11STodd Fiala } 984af245d11STodd Fiala 985af245d11STodd Fiala //------------------------------------------------------------------------------ 986af245d11STodd Fiala /// @class SingleStepOperation 987af245d11STodd Fiala /// @brief Implements NativeProcessLinux::SingleStep. 988af245d11STodd Fiala class SingleStepOperation : public Operation 989af245d11STodd Fiala { 990af245d11STodd Fiala public: 99197ccc294SChaoren Lin SingleStepOperation(lldb::tid_t tid, uint32_t signo) 99297ccc294SChaoren Lin : m_tid(tid), m_signo(signo) { } 993af245d11STodd Fiala 994d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 995af245d11STodd Fiala 996af245d11STodd Fiala private: 997af245d11STodd Fiala lldb::tid_t m_tid; 998af245d11STodd Fiala uint32_t m_signo; 999af245d11STodd Fiala }; 1000af245d11STodd Fiala 1001af245d11STodd Fiala void 1002af245d11STodd Fiala SingleStepOperation::Execute(NativeProcessLinux *monitor) 1003af245d11STodd Fiala { 1004af245d11STodd Fiala intptr_t data = 0; 1005af245d11STodd Fiala 1006af245d11STodd Fiala if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) 1007af245d11STodd Fiala data = m_signo; 1008af245d11STodd Fiala 100997ccc294SChaoren Lin PTRACE(PTRACE_SINGLESTEP, m_tid, nullptr, (void*)data, 0, m_error); 1010af245d11STodd Fiala } 1011af245d11STodd Fiala 1012af245d11STodd Fiala //------------------------------------------------------------------------------ 1013af245d11STodd Fiala /// @class SiginfoOperation 1014af245d11STodd Fiala /// @brief Implements NativeProcessLinux::GetSignalInfo. 1015af245d11STodd Fiala class SiginfoOperation : public Operation 1016af245d11STodd Fiala { 1017af245d11STodd Fiala public: 101897ccc294SChaoren Lin SiginfoOperation(lldb::tid_t tid, void *info) 101997ccc294SChaoren Lin : m_tid(tid), m_info(info) { } 1020af245d11STodd Fiala 1021d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 1022af245d11STodd Fiala 1023af245d11STodd Fiala private: 1024af245d11STodd Fiala lldb::tid_t m_tid; 1025af245d11STodd Fiala void *m_info; 1026af245d11STodd Fiala }; 1027af245d11STodd Fiala 1028af245d11STodd Fiala void 1029af245d11STodd Fiala SiginfoOperation::Execute(NativeProcessLinux *monitor) 1030af245d11STodd Fiala { 103197ccc294SChaoren Lin PTRACE(PTRACE_GETSIGINFO, m_tid, nullptr, m_info, 0, m_error); 1032af245d11STodd Fiala } 1033af245d11STodd Fiala 1034af245d11STodd Fiala //------------------------------------------------------------------------------ 1035af245d11STodd Fiala /// @class EventMessageOperation 1036af245d11STodd Fiala /// @brief Implements NativeProcessLinux::GetEventMessage. 1037af245d11STodd Fiala class EventMessageOperation : public Operation 1038af245d11STodd Fiala { 1039af245d11STodd Fiala public: 104097ccc294SChaoren Lin EventMessageOperation(lldb::tid_t tid, unsigned long *message) 104197ccc294SChaoren Lin : m_tid(tid), m_message(message) { } 1042af245d11STodd Fiala 1043d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 1044af245d11STodd Fiala 1045af245d11STodd Fiala private: 1046af245d11STodd Fiala lldb::tid_t m_tid; 1047af245d11STodd Fiala unsigned long *m_message; 1048af245d11STodd Fiala }; 1049af245d11STodd Fiala 1050af245d11STodd Fiala void 1051af245d11STodd Fiala EventMessageOperation::Execute(NativeProcessLinux *monitor) 1052af245d11STodd Fiala { 105397ccc294SChaoren Lin PTRACE(PTRACE_GETEVENTMSG, m_tid, nullptr, m_message, 0, m_error); 1054af245d11STodd Fiala } 1055af245d11STodd Fiala 1056af245d11STodd Fiala class DetachOperation : public Operation 1057af245d11STodd Fiala { 1058af245d11STodd Fiala public: 105997ccc294SChaoren Lin DetachOperation(lldb::tid_t tid) : m_tid(tid) { } 1060af245d11STodd Fiala 1061d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 1062af245d11STodd Fiala 1063af245d11STodd Fiala private: 1064af245d11STodd Fiala lldb::tid_t m_tid; 1065af245d11STodd Fiala }; 1066af245d11STodd Fiala 1067af245d11STodd Fiala void 1068af245d11STodd Fiala DetachOperation::Execute(NativeProcessLinux *monitor) 1069af245d11STodd Fiala { 107097ccc294SChaoren Lin PTRACE(PTRACE_DETACH, m_tid, nullptr, 0, 0, m_error); 1071af245d11STodd Fiala } 10721107b5a5SPavel Labath } // end of anonymous namespace 10731107b5a5SPavel Labath 1074bd7cbc5aSPavel Labath // Simple helper function to ensure flags are enabled on the given file 1075bd7cbc5aSPavel Labath // descriptor. 1076bd7cbc5aSPavel Labath static Error 1077bd7cbc5aSPavel Labath EnsureFDFlags(int fd, int flags) 1078bd7cbc5aSPavel Labath { 1079bd7cbc5aSPavel Labath Error error; 1080bd7cbc5aSPavel Labath 1081bd7cbc5aSPavel Labath int status = fcntl(fd, F_GETFL); 1082bd7cbc5aSPavel Labath if (status == -1) 1083bd7cbc5aSPavel Labath { 1084bd7cbc5aSPavel Labath error.SetErrorToErrno(); 1085bd7cbc5aSPavel Labath return error; 1086bd7cbc5aSPavel Labath } 1087bd7cbc5aSPavel Labath 1088bd7cbc5aSPavel Labath if (fcntl(fd, F_SETFL, status | flags) == -1) 1089bd7cbc5aSPavel Labath { 1090bd7cbc5aSPavel Labath error.SetErrorToErrno(); 1091bd7cbc5aSPavel Labath return error; 1092bd7cbc5aSPavel Labath } 1093bd7cbc5aSPavel Labath 1094bd7cbc5aSPavel Labath return error; 1095bd7cbc5aSPavel Labath } 1096bd7cbc5aSPavel Labath 1097bd7cbc5aSPavel Labath // This class encapsulates the privileged thread which performs all ptrace and wait operations on 1098bd7cbc5aSPavel Labath // the inferior. The thread consists of a main loop which waits for events and processes them 1099bd7cbc5aSPavel Labath // - SIGCHLD (delivered over a signalfd file descriptor): These signals notify us of events in 1100bd7cbc5aSPavel Labath // the inferior process. Upon receiving this signal we do a waitpid to get more information 1101bd7cbc5aSPavel Labath // and dispatch to NativeProcessLinux::MonitorCallback. 1102bd7cbc5aSPavel Labath // - requests for ptrace operations: These initiated via the DoOperation method, which funnels 1103bd7cbc5aSPavel Labath // them to the Monitor thread via m_operation member. The Monitor thread is signaled over a 1104bd7cbc5aSPavel Labath // pipe, and the completion of the operation is signalled over the semaphore. 1105bd7cbc5aSPavel Labath // - thread exit event: this is signaled from the Monitor destructor by closing the write end 1106bd7cbc5aSPavel Labath // of the command pipe. 110745f5cb31SPavel Labath class NativeProcessLinux::Monitor 110845f5cb31SPavel Labath { 11091107b5a5SPavel Labath private: 1110bd7cbc5aSPavel Labath // The initial monitor operation (launch or attach). It returns a inferior process id. 1111bd7cbc5aSPavel Labath std::unique_ptr<InitialOperation> m_initial_operation_up; 1112bd7cbc5aSPavel Labath 1113bd7cbc5aSPavel Labath ::pid_t m_child_pid = -1; 11141107b5a5SPavel Labath NativeProcessLinux * m_native_process; 11151107b5a5SPavel Labath 11161107b5a5SPavel Labath enum { READ, WRITE }; 11171107b5a5SPavel Labath int m_pipefd[2] = {-1, -1}; 11181107b5a5SPavel Labath int m_signal_fd = -1; 11191107b5a5SPavel Labath HostThread m_thread; 11201107b5a5SPavel Labath 1121bd7cbc5aSPavel Labath // current operation which must be executed on the priviliged thread 1122bd7cbc5aSPavel Labath Mutex m_operation_mutex; 1123bd7cbc5aSPavel Labath Operation *m_operation = nullptr; 1124bd7cbc5aSPavel Labath sem_t m_operation_sem; 1125bd7cbc5aSPavel Labath Error m_operation_error; 1126bd7cbc5aSPavel Labath 112745f5cb31SPavel Labath unsigned m_operation_nesting_level = 0; 112845f5cb31SPavel Labath 1129bd7cbc5aSPavel Labath static constexpr char operation_command = 'o'; 113045f5cb31SPavel Labath static constexpr char begin_block_command = '{'; 113145f5cb31SPavel Labath static constexpr char end_block_command = '}'; 1132bd7cbc5aSPavel Labath 11331107b5a5SPavel Labath void 11341107b5a5SPavel Labath HandleSignals(); 11351107b5a5SPavel Labath 11361107b5a5SPavel Labath void 11371107b5a5SPavel Labath HandleWait(); 11381107b5a5SPavel Labath 11391107b5a5SPavel Labath // Returns true if the thread should exit. 11401107b5a5SPavel Labath bool 11411107b5a5SPavel Labath HandleCommands(); 11421107b5a5SPavel Labath 11431107b5a5SPavel Labath void 11441107b5a5SPavel Labath MainLoop(); 11451107b5a5SPavel Labath 11461107b5a5SPavel Labath static void * 11471107b5a5SPavel Labath RunMonitor(void *arg); 11481107b5a5SPavel Labath 1149bd7cbc5aSPavel Labath Error 115045f5cb31SPavel Labath WaitForAck(); 115145f5cb31SPavel Labath 115245f5cb31SPavel Labath void 115345f5cb31SPavel Labath BeginOperationBlock() 115445f5cb31SPavel Labath { 115545f5cb31SPavel Labath write(m_pipefd[WRITE], &begin_block_command, sizeof operation_command); 115645f5cb31SPavel Labath WaitForAck(); 115745f5cb31SPavel Labath } 115845f5cb31SPavel Labath 115945f5cb31SPavel Labath void 116045f5cb31SPavel Labath EndOperationBlock() 116145f5cb31SPavel Labath { 116245f5cb31SPavel Labath write(m_pipefd[WRITE], &end_block_command, sizeof operation_command); 116345f5cb31SPavel Labath WaitForAck(); 116445f5cb31SPavel Labath } 116545f5cb31SPavel Labath 11661107b5a5SPavel Labath public: 1167bd7cbc5aSPavel Labath Monitor(const InitialOperation &initial_operation, 1168bd7cbc5aSPavel Labath NativeProcessLinux *native_process) 1169bd7cbc5aSPavel Labath : m_initial_operation_up(new InitialOperation(initial_operation)), 1170bd7cbc5aSPavel Labath m_native_process(native_process) 1171bd7cbc5aSPavel Labath { 1172bd7cbc5aSPavel Labath sem_init(&m_operation_sem, 0, 0); 1173bd7cbc5aSPavel Labath } 11741107b5a5SPavel Labath 11751107b5a5SPavel Labath ~Monitor(); 11761107b5a5SPavel Labath 11771107b5a5SPavel Labath Error 11781107b5a5SPavel Labath Initialize(); 1179bd7cbc5aSPavel Labath 1180bd7cbc5aSPavel Labath void 118145f5cb31SPavel Labath Terminate(); 118245f5cb31SPavel Labath 118345f5cb31SPavel Labath void 1184bd7cbc5aSPavel Labath DoOperation(Operation *op); 118545f5cb31SPavel Labath 118645f5cb31SPavel Labath class ScopedOperationLock { 118745f5cb31SPavel Labath Monitor &m_monitor; 118845f5cb31SPavel Labath 118945f5cb31SPavel Labath public: 119045f5cb31SPavel Labath ScopedOperationLock(Monitor &monitor) 119145f5cb31SPavel Labath : m_monitor(monitor) 119245f5cb31SPavel Labath { m_monitor.BeginOperationBlock(); } 119345f5cb31SPavel Labath 119445f5cb31SPavel Labath ~ScopedOperationLock() 119545f5cb31SPavel Labath { m_monitor.EndOperationBlock(); } 119645f5cb31SPavel Labath }; 11971107b5a5SPavel Labath }; 1198bd7cbc5aSPavel Labath constexpr char NativeProcessLinux::Monitor::operation_command; 119945f5cb31SPavel Labath constexpr char NativeProcessLinux::Monitor::begin_block_command; 120045f5cb31SPavel Labath constexpr char NativeProcessLinux::Monitor::end_block_command; 12011107b5a5SPavel Labath 12021107b5a5SPavel Labath Error 12031107b5a5SPavel Labath NativeProcessLinux::Monitor::Initialize() 12041107b5a5SPavel Labath { 12051107b5a5SPavel Labath Error error; 12061107b5a5SPavel Labath 12071107b5a5SPavel Labath // We get a SIGCHLD every time something interesting happens with the inferior. We shall be 12081107b5a5SPavel Labath // listening for these signals over a signalfd file descriptors. This allows us to wait for 12091107b5a5SPavel Labath // multiple kinds of events with select. 12101107b5a5SPavel Labath sigset_t signals; 12111107b5a5SPavel Labath sigemptyset(&signals); 12121107b5a5SPavel Labath sigaddset(&signals, SIGCHLD); 12131107b5a5SPavel Labath m_signal_fd = signalfd(-1, &signals, SFD_NONBLOCK | SFD_CLOEXEC); 12141107b5a5SPavel Labath if (m_signal_fd < 0) 12151107b5a5SPavel Labath { 12161107b5a5SPavel Labath return Error("NativeProcessLinux::Monitor::%s failed due to signalfd failure. Monitoring the inferior will be impossible: %s", 12171107b5a5SPavel Labath __FUNCTION__, strerror(errno)); 12181107b5a5SPavel Labath 1219af245d11STodd Fiala } 1220af245d11STodd Fiala 12211107b5a5SPavel Labath if (pipe2(m_pipefd, O_CLOEXEC) == -1) 12221107b5a5SPavel Labath { 12231107b5a5SPavel Labath error.SetErrorToErrno(); 12241107b5a5SPavel Labath return error; 12251107b5a5SPavel Labath } 12261107b5a5SPavel Labath 1227bd7cbc5aSPavel Labath if ((error = EnsureFDFlags(m_pipefd[READ], O_NONBLOCK)).Fail()) { 1228bd7cbc5aSPavel Labath return error; 1229bd7cbc5aSPavel Labath } 1230bd7cbc5aSPavel Labath 1231bd7cbc5aSPavel Labath static const char g_thread_name[] = "lldb.process.nativelinux.monitor"; 1232bd7cbc5aSPavel Labath m_thread = ThreadLauncher::LaunchThread(g_thread_name, Monitor::RunMonitor, this, nullptr); 12331107b5a5SPavel Labath if (!m_thread.IsJoinable()) 12341107b5a5SPavel Labath return Error("Failed to create monitor thread for NativeProcessLinux."); 12351107b5a5SPavel Labath 1236bd7cbc5aSPavel Labath // Wait for initial operation to complete. 123745f5cb31SPavel Labath return WaitForAck(); 1238bd7cbc5aSPavel Labath } 1239bd7cbc5aSPavel Labath 1240bd7cbc5aSPavel Labath void 1241bd7cbc5aSPavel Labath NativeProcessLinux::Monitor::DoOperation(Operation *op) 1242bd7cbc5aSPavel Labath { 1243bd7cbc5aSPavel Labath if (m_thread.EqualsThread(pthread_self())) { 1244bd7cbc5aSPavel Labath // If we're on the Monitor thread, we can simply execute the operation. 1245bd7cbc5aSPavel Labath op->Execute(m_native_process); 1246bd7cbc5aSPavel Labath return; 1247bd7cbc5aSPavel Labath } 1248bd7cbc5aSPavel Labath 1249bd7cbc5aSPavel Labath // Otherwise we need to pass the operation to the Monitor thread so it can handle it. 1250bd7cbc5aSPavel Labath Mutex::Locker lock(m_operation_mutex); 1251bd7cbc5aSPavel Labath 1252bd7cbc5aSPavel Labath m_operation = op; 1253bd7cbc5aSPavel Labath 1254bd7cbc5aSPavel Labath // notify the thread that an operation is ready to be processed 1255bd7cbc5aSPavel Labath write(m_pipefd[WRITE], &operation_command, sizeof operation_command); 1256bd7cbc5aSPavel Labath 125745f5cb31SPavel Labath WaitForAck(); 125845f5cb31SPavel Labath } 125945f5cb31SPavel Labath 126045f5cb31SPavel Labath void 126145f5cb31SPavel Labath NativeProcessLinux::Monitor::Terminate() 126245f5cb31SPavel Labath { 126345f5cb31SPavel Labath if (m_pipefd[WRITE] >= 0) 126445f5cb31SPavel Labath { 126545f5cb31SPavel Labath close(m_pipefd[WRITE]); 126645f5cb31SPavel Labath m_pipefd[WRITE] = -1; 126745f5cb31SPavel Labath } 126845f5cb31SPavel Labath if (m_thread.IsJoinable()) 126945f5cb31SPavel Labath m_thread.Join(nullptr); 12701107b5a5SPavel Labath } 12711107b5a5SPavel Labath 12721107b5a5SPavel Labath NativeProcessLinux::Monitor::~Monitor() 12731107b5a5SPavel Labath { 127445f5cb31SPavel Labath Terminate(); 12751107b5a5SPavel Labath if (m_pipefd[READ] >= 0) 12761107b5a5SPavel Labath close(m_pipefd[READ]); 12771107b5a5SPavel Labath if (m_signal_fd >= 0) 12781107b5a5SPavel Labath close(m_signal_fd); 1279bd7cbc5aSPavel Labath sem_destroy(&m_operation_sem); 12801107b5a5SPavel Labath } 12811107b5a5SPavel Labath 12821107b5a5SPavel Labath void 12831107b5a5SPavel Labath NativeProcessLinux::Monitor::HandleSignals() 12841107b5a5SPavel Labath { 12851107b5a5SPavel Labath Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 12861107b5a5SPavel Labath 12871107b5a5SPavel Labath // We don't really care about the content of the SIGCHLD siginfo structure, as we will get 12881107b5a5SPavel Labath // all the information from waitpid(). We just need to read all the signals so that we can 12891107b5a5SPavel Labath // sleep next time we reach select(). 12901107b5a5SPavel Labath while (true) 12911107b5a5SPavel Labath { 12921107b5a5SPavel Labath signalfd_siginfo info; 12931107b5a5SPavel Labath ssize_t size = read(m_signal_fd, &info, sizeof info); 12941107b5a5SPavel Labath if (size == -1) 12951107b5a5SPavel Labath { 12961107b5a5SPavel Labath if (errno == EAGAIN || errno == EWOULDBLOCK) 12971107b5a5SPavel Labath break; // We are done. 12981107b5a5SPavel Labath if (errno == EINTR) 12991107b5a5SPavel Labath continue; 13001107b5a5SPavel Labath if (log) 13011107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s reading from signalfd file descriptor failed: %s", 13021107b5a5SPavel Labath __FUNCTION__, strerror(errno)); 13031107b5a5SPavel Labath break; 13041107b5a5SPavel Labath } 13051107b5a5SPavel Labath if (size != sizeof info) 13061107b5a5SPavel Labath { 13071107b5a5SPavel Labath // We got incomplete information structure. This should not happen, let's just log 13081107b5a5SPavel Labath // that. 13091107b5a5SPavel Labath if (log) 13101107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s reading from signalfd file descriptor returned incomplete data: " 13111107b5a5SPavel Labath "structure size is %zd, read returned %zd bytes", 13121107b5a5SPavel Labath __FUNCTION__, sizeof info, size); 13131107b5a5SPavel Labath break; 13141107b5a5SPavel Labath } 13151107b5a5SPavel Labath if (log) 13161107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s received signal %s(%d).", __FUNCTION__, 13171107b5a5SPavel Labath Host::GetSignalAsCString(info.ssi_signo), info.ssi_signo); 13181107b5a5SPavel Labath } 13191107b5a5SPavel Labath } 13201107b5a5SPavel Labath 13211107b5a5SPavel Labath void 13221107b5a5SPavel Labath NativeProcessLinux::Monitor::HandleWait() 13231107b5a5SPavel Labath { 13241107b5a5SPavel Labath Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 13251107b5a5SPavel Labath // Process all pending waitpid notifications. 13261107b5a5SPavel Labath while (true) 13271107b5a5SPavel Labath { 13281107b5a5SPavel Labath int status = -1; 13291107b5a5SPavel Labath ::pid_t wait_pid = waitpid(m_child_pid, &status, __WALL | WNOHANG); 13301107b5a5SPavel Labath 13311107b5a5SPavel Labath if (wait_pid == 0) 13321107b5a5SPavel Labath break; // We are done. 13331107b5a5SPavel Labath 13341107b5a5SPavel Labath if (wait_pid == -1) 13351107b5a5SPavel Labath { 13361107b5a5SPavel Labath if (errno == EINTR) 13371107b5a5SPavel Labath continue; 13381107b5a5SPavel Labath 13391107b5a5SPavel Labath if (log) 13401107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s waitpid (pid = %" PRIi32 ", &status, __WALL | WNOHANG) failed: %s", 13411107b5a5SPavel Labath __FUNCTION__, m_child_pid, strerror(errno)); 13421107b5a5SPavel Labath break; 13431107b5a5SPavel Labath } 13441107b5a5SPavel Labath 13451107b5a5SPavel Labath bool exited = false; 13461107b5a5SPavel Labath int signal = 0; 13471107b5a5SPavel Labath int exit_status = 0; 13481107b5a5SPavel Labath const char *status_cstr = NULL; 13491107b5a5SPavel Labath if (WIFSTOPPED(status)) 13501107b5a5SPavel Labath { 13511107b5a5SPavel Labath signal = WSTOPSIG(status); 13521107b5a5SPavel Labath status_cstr = "STOPPED"; 13531107b5a5SPavel Labath } 13541107b5a5SPavel Labath else if (WIFEXITED(status)) 13551107b5a5SPavel Labath { 13561107b5a5SPavel Labath exit_status = WEXITSTATUS(status); 13571107b5a5SPavel Labath status_cstr = "EXITED"; 13581107b5a5SPavel Labath exited = true; 13591107b5a5SPavel Labath } 13601107b5a5SPavel Labath else if (WIFSIGNALED(status)) 13611107b5a5SPavel Labath { 13621107b5a5SPavel Labath signal = WTERMSIG(status); 13631107b5a5SPavel Labath status_cstr = "SIGNALED"; 13641107b5a5SPavel Labath if (wait_pid == abs(m_child_pid)) { 13651107b5a5SPavel Labath exited = true; 13661107b5a5SPavel Labath exit_status = -1; 13671107b5a5SPavel Labath } 13681107b5a5SPavel Labath } 13691107b5a5SPavel Labath else 13701107b5a5SPavel Labath status_cstr = "(\?\?\?)"; 13711107b5a5SPavel Labath 13721107b5a5SPavel Labath if (log) 13731107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s: waitpid (pid = %" PRIi32 ", &status, __WALL | WNOHANG)" 13741107b5a5SPavel Labath "=> pid = %" PRIi32 ", status = 0x%8.8x (%s), signal = %i, exit_state = %i", 13751107b5a5SPavel Labath __FUNCTION__, m_child_pid, wait_pid, status, status_cstr, signal, exit_status); 13761107b5a5SPavel Labath 13771107b5a5SPavel Labath m_native_process->MonitorCallback (wait_pid, exited, signal, exit_status); 13781107b5a5SPavel Labath } 13791107b5a5SPavel Labath } 13801107b5a5SPavel Labath 13811107b5a5SPavel Labath bool 13821107b5a5SPavel Labath NativeProcessLinux::Monitor::HandleCommands() 13831107b5a5SPavel Labath { 13841107b5a5SPavel Labath Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 13851107b5a5SPavel Labath 13861107b5a5SPavel Labath while (true) 13871107b5a5SPavel Labath { 13881107b5a5SPavel Labath char command = 0; 13891107b5a5SPavel Labath ssize_t size = read(m_pipefd[READ], &command, sizeof command); 13901107b5a5SPavel Labath if (size == -1) 13911107b5a5SPavel Labath { 13921107b5a5SPavel Labath if (errno == EAGAIN || errno == EWOULDBLOCK) 13931107b5a5SPavel Labath return false; 13941107b5a5SPavel Labath if (errno == EINTR) 13951107b5a5SPavel Labath continue; 13961107b5a5SPavel Labath if (log) 13971107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s exiting because read from command file descriptor failed: %s", __FUNCTION__, strerror(errno)); 13981107b5a5SPavel Labath return true; 13991107b5a5SPavel Labath } 14001107b5a5SPavel Labath if (size == 0) // end of file - write end closed 14011107b5a5SPavel Labath { 14021107b5a5SPavel Labath if (log) 14031107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s exit command received, exiting...", __FUNCTION__); 140445f5cb31SPavel Labath assert(m_operation_nesting_level == 0 && "Unbalanced begin/end block commands detected"); 14051107b5a5SPavel Labath return true; // We are done. 14061107b5a5SPavel Labath } 1407bd7cbc5aSPavel Labath 1408bd7cbc5aSPavel Labath switch (command) 1409bd7cbc5aSPavel Labath { 1410bd7cbc5aSPavel Labath case operation_command: 1411bd7cbc5aSPavel Labath m_operation->Execute(m_native_process); 141245f5cb31SPavel Labath break; 141345f5cb31SPavel Labath case begin_block_command: 141445f5cb31SPavel Labath ++m_operation_nesting_level; 141545f5cb31SPavel Labath break; 141645f5cb31SPavel Labath case end_block_command: 141745f5cb31SPavel Labath assert(m_operation_nesting_level > 0); 141845f5cb31SPavel Labath --m_operation_nesting_level; 1419bd7cbc5aSPavel Labath break; 1420bd7cbc5aSPavel Labath default: 14211107b5a5SPavel Labath if (log) 14221107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s received unknown command '%c'", 14231107b5a5SPavel Labath __FUNCTION__, command); 14241107b5a5SPavel Labath } 142545f5cb31SPavel Labath 142645f5cb31SPavel Labath // notify calling thread that the command has been processed 142745f5cb31SPavel Labath sem_post(&m_operation_sem); 14281107b5a5SPavel Labath } 1429bd7cbc5aSPavel Labath } 14301107b5a5SPavel Labath 14311107b5a5SPavel Labath void 14321107b5a5SPavel Labath NativeProcessLinux::Monitor::MainLoop() 14331107b5a5SPavel Labath { 1434bd7cbc5aSPavel Labath ::pid_t child_pid = (*m_initial_operation_up)(m_operation_error); 1435bd7cbc5aSPavel Labath m_initial_operation_up.reset(); 1436bd7cbc5aSPavel Labath m_child_pid = -getpgid(child_pid), 1437bd7cbc5aSPavel Labath sem_post(&m_operation_sem); 1438bd7cbc5aSPavel Labath 14391107b5a5SPavel Labath while (true) 14401107b5a5SPavel Labath { 14411107b5a5SPavel Labath fd_set fds; 14421107b5a5SPavel Labath FD_ZERO(&fds); 144345f5cb31SPavel Labath // Only process waitpid events if we are outside of an operation block. Any pending 144445f5cb31SPavel Labath // events will be processed after we leave the block. 144545f5cb31SPavel Labath if (m_operation_nesting_level == 0) 14461107b5a5SPavel Labath FD_SET(m_signal_fd, &fds); 14471107b5a5SPavel Labath FD_SET(m_pipefd[READ], &fds); 14481107b5a5SPavel Labath 14491107b5a5SPavel Labath int max_fd = std::max(m_signal_fd, m_pipefd[READ]) + 1; 14501107b5a5SPavel Labath int r = select(max_fd, &fds, nullptr, nullptr, nullptr); 14511107b5a5SPavel Labath if (r < 0) 14521107b5a5SPavel Labath { 14531107b5a5SPavel Labath Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 14541107b5a5SPavel Labath if (log) 14551107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s exiting because select failed: %s", 14561107b5a5SPavel Labath __FUNCTION__, strerror(errno)); 14571107b5a5SPavel Labath return; 14581107b5a5SPavel Labath } 14591107b5a5SPavel Labath 14601107b5a5SPavel Labath if (FD_ISSET(m_pipefd[READ], &fds)) 14611107b5a5SPavel Labath { 14621107b5a5SPavel Labath if (HandleCommands()) 14631107b5a5SPavel Labath return; 14641107b5a5SPavel Labath } 14651107b5a5SPavel Labath 14661107b5a5SPavel Labath if (FD_ISSET(m_signal_fd, &fds)) 14671107b5a5SPavel Labath { 14681107b5a5SPavel Labath HandleSignals(); 14691107b5a5SPavel Labath HandleWait(); 14701107b5a5SPavel Labath } 14711107b5a5SPavel Labath } 14721107b5a5SPavel Labath } 14731107b5a5SPavel Labath 1474bd7cbc5aSPavel Labath Error 147545f5cb31SPavel Labath NativeProcessLinux::Monitor::WaitForAck() 1476bd7cbc5aSPavel Labath { 1477bd7cbc5aSPavel Labath Error error; 1478bd7cbc5aSPavel Labath while (sem_wait(&m_operation_sem) != 0) 1479bd7cbc5aSPavel Labath { 1480bd7cbc5aSPavel Labath if (errno == EINTR) 1481bd7cbc5aSPavel Labath continue; 1482bd7cbc5aSPavel Labath 1483bd7cbc5aSPavel Labath error.SetErrorToErrno(); 1484bd7cbc5aSPavel Labath return error; 1485bd7cbc5aSPavel Labath } 1486bd7cbc5aSPavel Labath 1487bd7cbc5aSPavel Labath return m_operation_error; 1488bd7cbc5aSPavel Labath } 1489bd7cbc5aSPavel Labath 14901107b5a5SPavel Labath void * 14911107b5a5SPavel Labath NativeProcessLinux::Monitor::RunMonitor(void *arg) 14921107b5a5SPavel Labath { 14931107b5a5SPavel Labath static_cast<Monitor *>(arg)->MainLoop(); 14941107b5a5SPavel Labath return nullptr; 14951107b5a5SPavel Labath } 14961107b5a5SPavel Labath 14971107b5a5SPavel Labath 1498bd7cbc5aSPavel Labath NativeProcessLinux::LaunchArgs::LaunchArgs(Module *module, 1499af245d11STodd Fiala char const **argv, 1500af245d11STodd Fiala char const **envp, 150175f47c3aSTodd Fiala const std::string &stdin_path, 150275f47c3aSTodd Fiala const std::string &stdout_path, 150375f47c3aSTodd Fiala const std::string &stderr_path, 15040bce1b67STodd Fiala const char *working_dir, 1505db264a6dSTamas Berghammer const ProcessLaunchInfo &launch_info) 1506bd7cbc5aSPavel Labath : m_module(module), 1507af245d11STodd Fiala m_argv(argv), 1508af245d11STodd Fiala m_envp(envp), 1509af245d11STodd Fiala m_stdin_path(stdin_path), 1510af245d11STodd Fiala m_stdout_path(stdout_path), 1511af245d11STodd Fiala m_stderr_path(stderr_path), 15120bce1b67STodd Fiala m_working_dir(working_dir), 15130bce1b67STodd Fiala m_launch_info(launch_info) 15140bce1b67STodd Fiala { 15150bce1b67STodd Fiala } 1516af245d11STodd Fiala 1517af245d11STodd Fiala NativeProcessLinux::LaunchArgs::~LaunchArgs() 1518af245d11STodd Fiala { } 1519af245d11STodd Fiala 1520af245d11STodd Fiala // ----------------------------------------------------------------------------- 1521af245d11STodd Fiala // Public Static Methods 1522af245d11STodd Fiala // ----------------------------------------------------------------------------- 1523af245d11STodd Fiala 1524db264a6dSTamas Berghammer Error 1525af245d11STodd Fiala NativeProcessLinux::LaunchProcess ( 1526db264a6dSTamas Berghammer Module *exe_module, 1527db264a6dSTamas Berghammer ProcessLaunchInfo &launch_info, 1528db264a6dSTamas Berghammer NativeProcessProtocol::NativeDelegate &native_delegate, 1529af245d11STodd Fiala NativeProcessProtocolSP &native_process_sp) 1530af245d11STodd Fiala { 1531af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1532af245d11STodd Fiala 1533af245d11STodd Fiala Error error; 1534af245d11STodd Fiala 1535af245d11STodd Fiala // Verify the working directory is valid if one was specified. 1536af245d11STodd Fiala const char* working_dir = launch_info.GetWorkingDirectory (); 1537af245d11STodd Fiala if (working_dir) 1538af245d11STodd Fiala { 1539af245d11STodd Fiala FileSpec working_dir_fs (working_dir, true); 1540af245d11STodd Fiala if (!working_dir_fs || working_dir_fs.GetFileType () != FileSpec::eFileTypeDirectory) 1541af245d11STodd Fiala { 1542af245d11STodd Fiala error.SetErrorStringWithFormat ("No such file or directory: %s", working_dir); 1543af245d11STodd Fiala return error; 1544af245d11STodd Fiala } 1545af245d11STodd Fiala } 1546af245d11STodd Fiala 1547db264a6dSTamas Berghammer const FileAction *file_action; 1548af245d11STodd Fiala 1549af245d11STodd Fiala // Default of NULL will mean to use existing open file descriptors. 155075f47c3aSTodd Fiala std::string stdin_path; 155175f47c3aSTodd Fiala std::string stdout_path; 155275f47c3aSTodd Fiala std::string stderr_path; 1553af245d11STodd Fiala 1554af245d11STodd Fiala file_action = launch_info.GetFileActionForFD (STDIN_FILENO); 155575f47c3aSTodd Fiala if (file_action) 155675f47c3aSTodd Fiala stdin_path = file_action->GetPath (); 1557af245d11STodd Fiala 1558af245d11STodd Fiala file_action = launch_info.GetFileActionForFD (STDOUT_FILENO); 155975f47c3aSTodd Fiala if (file_action) 156075f47c3aSTodd Fiala stdout_path = file_action->GetPath (); 1561af245d11STodd Fiala 1562af245d11STodd Fiala file_action = launch_info.GetFileActionForFD (STDERR_FILENO); 156375f47c3aSTodd Fiala if (file_action) 156475f47c3aSTodd Fiala stderr_path = file_action->GetPath (); 156575f47c3aSTodd Fiala 156675f47c3aSTodd Fiala if (log) 156775f47c3aSTodd Fiala { 156875f47c3aSTodd Fiala if (!stdin_path.empty ()) 156975f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s setting STDIN to '%s'", __FUNCTION__, stdin_path.c_str ()); 157075f47c3aSTodd Fiala else 157175f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s leaving STDIN as is", __FUNCTION__); 157275f47c3aSTodd Fiala 157375f47c3aSTodd Fiala if (!stdout_path.empty ()) 157475f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s setting STDOUT to '%s'", __FUNCTION__, stdout_path.c_str ()); 157575f47c3aSTodd Fiala else 157675f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s leaving STDOUT as is", __FUNCTION__); 157775f47c3aSTodd Fiala 157875f47c3aSTodd Fiala if (!stderr_path.empty ()) 157975f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s setting STDERR to '%s'", __FUNCTION__, stderr_path.c_str ()); 158075f47c3aSTodd Fiala else 158175f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s leaving STDERR as is", __FUNCTION__); 158275f47c3aSTodd Fiala } 1583af245d11STodd Fiala 1584af245d11STodd Fiala // Create the NativeProcessLinux in launch mode. 1585af245d11STodd Fiala native_process_sp.reset (new NativeProcessLinux ()); 1586af245d11STodd Fiala 1587af245d11STodd Fiala if (log) 1588af245d11STodd Fiala { 1589af245d11STodd Fiala int i = 0; 1590af245d11STodd Fiala for (const char **args = launch_info.GetArguments ().GetConstArgumentVector (); *args; ++args, ++i) 1591af245d11STodd Fiala { 1592af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s arg %d: \"%s\"", __FUNCTION__, i, *args ? *args : "nullptr"); 1593af245d11STodd Fiala ++i; 1594af245d11STodd Fiala } 1595af245d11STodd Fiala } 1596af245d11STodd Fiala 1597af245d11STodd Fiala if (!native_process_sp->RegisterNativeDelegate (native_delegate)) 1598af245d11STodd Fiala { 1599af245d11STodd Fiala native_process_sp.reset (); 1600af245d11STodd Fiala error.SetErrorStringWithFormat ("failed to register the native delegate"); 1601af245d11STodd Fiala return error; 1602af245d11STodd Fiala } 1603af245d11STodd Fiala 1604cb84eebbSTamas Berghammer std::static_pointer_cast<NativeProcessLinux> (native_process_sp)->LaunchInferior ( 1605af245d11STodd Fiala exe_module, 1606af245d11STodd Fiala launch_info.GetArguments ().GetConstArgumentVector (), 1607af245d11STodd Fiala launch_info.GetEnvironmentEntries ().GetConstArgumentVector (), 1608af245d11STodd Fiala stdin_path, 1609af245d11STodd Fiala stdout_path, 1610af245d11STodd Fiala stderr_path, 1611af245d11STodd Fiala working_dir, 16120bce1b67STodd Fiala launch_info, 1613af245d11STodd Fiala error); 1614af245d11STodd Fiala 1615af245d11STodd Fiala if (error.Fail ()) 1616af245d11STodd Fiala { 1617af245d11STodd Fiala native_process_sp.reset (); 1618af245d11STodd Fiala if (log) 1619af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to launch process: %s", __FUNCTION__, error.AsCString ()); 1620af245d11STodd Fiala return error; 1621af245d11STodd Fiala } 1622af245d11STodd Fiala 1623af245d11STodd Fiala launch_info.SetProcessID (native_process_sp->GetID ()); 1624af245d11STodd Fiala 1625af245d11STodd Fiala return error; 1626af245d11STodd Fiala } 1627af245d11STodd Fiala 1628db264a6dSTamas Berghammer Error 1629af245d11STodd Fiala NativeProcessLinux::AttachToProcess ( 1630af245d11STodd Fiala lldb::pid_t pid, 1631db264a6dSTamas Berghammer NativeProcessProtocol::NativeDelegate &native_delegate, 1632af245d11STodd Fiala NativeProcessProtocolSP &native_process_sp) 1633af245d11STodd Fiala { 1634af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1635af245d11STodd Fiala if (log && log->GetMask ().Test (POSIX_LOG_VERBOSE)) 1636af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(pid = %" PRIi64 ")", __FUNCTION__, pid); 1637af245d11STodd Fiala 1638af245d11STodd Fiala // Grab the current platform architecture. This should be Linux, 1639af245d11STodd Fiala // since this code is only intended to run on a Linux host. 1640615eb7e6SGreg Clayton PlatformSP platform_sp (Platform::GetHostPlatform ()); 1641af245d11STodd Fiala if (!platform_sp) 1642af245d11STodd Fiala return Error("failed to get a valid default platform"); 1643af245d11STodd Fiala 1644af245d11STodd Fiala // Retrieve the architecture for the running process. 1645af245d11STodd Fiala ArchSpec process_arch; 1646af245d11STodd Fiala Error error = ResolveProcessArchitecture (pid, *platform_sp.get (), process_arch); 1647af245d11STodd Fiala if (!error.Success ()) 1648af245d11STodd Fiala return error; 1649af245d11STodd Fiala 16501339b5e8SOleksiy Vyalov std::shared_ptr<NativeProcessLinux> native_process_linux_sp (new NativeProcessLinux ()); 1651af245d11STodd Fiala 16521339b5e8SOleksiy Vyalov if (!native_process_linux_sp->RegisterNativeDelegate (native_delegate)) 1653af245d11STodd Fiala { 1654af245d11STodd Fiala error.SetErrorStringWithFormat ("failed to register the native delegate"); 1655af245d11STodd Fiala return error; 1656af245d11STodd Fiala } 1657af245d11STodd Fiala 16581339b5e8SOleksiy Vyalov native_process_linux_sp->AttachToInferior (pid, error); 1659af245d11STodd Fiala if (!error.Success ()) 1660af245d11STodd Fiala return error; 1661af245d11STodd Fiala 16621339b5e8SOleksiy Vyalov native_process_sp = native_process_linux_sp; 1663af245d11STodd Fiala return error; 1664af245d11STodd Fiala } 1665af245d11STodd Fiala 1666af245d11STodd Fiala // ----------------------------------------------------------------------------- 1667af245d11STodd Fiala // Public Instance Methods 1668af245d11STodd Fiala // ----------------------------------------------------------------------------- 1669af245d11STodd Fiala 1670af245d11STodd Fiala NativeProcessLinux::NativeProcessLinux () : 1671af245d11STodd Fiala NativeProcessProtocol (LLDB_INVALID_PROCESS_ID), 1672af245d11STodd Fiala m_arch (), 1673af245d11STodd Fiala m_supports_mem_region (eLazyBoolCalculate), 1674af245d11STodd Fiala m_mem_region_cache (), 1675fa03ad2eSChaoren Lin m_mem_region_cache_mutex (), 1676c076559aSPavel Labath m_log_function (GetThreadLoggerFunction()), 1677c076559aSPavel Labath m_tid_map (), 1678c076559aSPavel Labath m_log_event_processing (false) 1679af245d11STodd Fiala { 1680af245d11STodd Fiala } 1681af245d11STodd Fiala 1682af245d11STodd Fiala //------------------------------------------------------------------------------ 1683bd7cbc5aSPavel Labath // NativeProcessLinux spawns a new thread which performs all operations on the inferior process. 1684bd7cbc5aSPavel Labath // Refer to Monitor and Operation classes to see why this is necessary. 1685bd7cbc5aSPavel Labath //------------------------------------------------------------------------------ 1686af245d11STodd Fiala void 1687af245d11STodd Fiala NativeProcessLinux::LaunchInferior ( 1688af245d11STodd Fiala Module *module, 1689af245d11STodd Fiala const char *argv[], 1690af245d11STodd Fiala const char *envp[], 169175f47c3aSTodd Fiala const std::string &stdin_path, 169275f47c3aSTodd Fiala const std::string &stdout_path, 169375f47c3aSTodd Fiala const std::string &stderr_path, 1694af245d11STodd Fiala const char *working_dir, 1695db264a6dSTamas Berghammer const ProcessLaunchInfo &launch_info, 1696db264a6dSTamas Berghammer Error &error) 1697af245d11STodd Fiala { 1698af245d11STodd Fiala if (module) 1699af245d11STodd Fiala m_arch = module->GetArchitecture (); 1700af245d11STodd Fiala 1701af245d11STodd Fiala SetState (eStateLaunching); 1702af245d11STodd Fiala 1703af245d11STodd Fiala std::unique_ptr<LaunchArgs> args( 1704af245d11STodd Fiala new LaunchArgs( 1705bd7cbc5aSPavel Labath module, argv, envp, 1706af245d11STodd Fiala stdin_path, stdout_path, stderr_path, 17070bce1b67STodd Fiala working_dir, launch_info)); 1708af245d11STodd Fiala 1709bd7cbc5aSPavel Labath StartMonitorThread ([&] (Error &e) { return Launch(args.get(), e); }, error); 1710af245d11STodd Fiala if (!error.Success ()) 1711af245d11STodd Fiala return; 1712af245d11STodd Fiala } 1713af245d11STodd Fiala 1714af245d11STodd Fiala void 1715db264a6dSTamas Berghammer NativeProcessLinux::AttachToInferior (lldb::pid_t pid, Error &error) 1716af245d11STodd Fiala { 1717af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1718af245d11STodd Fiala if (log) 1719af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ")", __FUNCTION__, pid); 1720af245d11STodd Fiala 1721af245d11STodd Fiala // We can use the Host for everything except the ResolveExecutable portion. 1722615eb7e6SGreg Clayton PlatformSP platform_sp = Platform::GetHostPlatform (); 1723af245d11STodd Fiala if (!platform_sp) 1724af245d11STodd Fiala { 1725af245d11STodd Fiala if (log) 1726af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 "): no default platform set", __FUNCTION__, pid); 1727af245d11STodd Fiala error.SetErrorString ("no default platform available"); 172850d60be3SShawn Best return; 1729af245d11STodd Fiala } 1730af245d11STodd Fiala 1731af245d11STodd Fiala // Gather info about the process. 1732af245d11STodd Fiala ProcessInstanceInfo process_info; 173350d60be3SShawn Best if (!platform_sp->GetProcessInfo (pid, process_info)) 173450d60be3SShawn Best { 173550d60be3SShawn Best if (log) 173650d60be3SShawn Best log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 "): failed to get process info", __FUNCTION__, pid); 173750d60be3SShawn Best error.SetErrorString ("failed to get process info"); 173850d60be3SShawn Best return; 173950d60be3SShawn Best } 1740af245d11STodd Fiala 1741af245d11STodd Fiala // Resolve the executable module 1742af245d11STodd Fiala ModuleSP exe_module_sp; 1743af245d11STodd Fiala FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths()); 1744e56f6dceSChaoren Lin ModuleSpec exe_module_spec(process_info.GetExecutableFile(), process_info.GetArchitecture()); 17456edef204SOleksiy Vyalov error = platform_sp->ResolveExecutable(exe_module_spec, exe_module_sp, 1746af245d11STodd Fiala executable_search_paths.GetSize() ? &executable_search_paths : NULL); 1747af245d11STodd Fiala if (!error.Success()) 1748af245d11STodd Fiala return; 1749af245d11STodd Fiala 1750af245d11STodd Fiala // Set the architecture to the exe architecture. 1751af245d11STodd Fiala m_arch = exe_module_sp->GetArchitecture(); 1752af245d11STodd Fiala if (log) 1753af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ") detected architecture %s", __FUNCTION__, pid, m_arch.GetArchitectureName ()); 1754af245d11STodd Fiala 1755af245d11STodd Fiala m_pid = pid; 1756af245d11STodd Fiala SetState(eStateAttaching); 1757af245d11STodd Fiala 1758bd7cbc5aSPavel Labath StartMonitorThread ([=] (Error &e) { return Attach(pid, e); }, error); 1759af245d11STodd Fiala if (!error.Success ()) 1760af245d11STodd Fiala return; 1761af245d11STodd Fiala } 1762af245d11STodd Fiala 17638bc34f4dSOleksiy Vyalov void 17648bc34f4dSOleksiy Vyalov NativeProcessLinux::Terminate () 1765af245d11STodd Fiala { 176645f5cb31SPavel Labath m_monitor_up->Terminate(); 1767af245d11STodd Fiala } 1768af245d11STodd Fiala 1769bd7cbc5aSPavel Labath ::pid_t 1770bd7cbc5aSPavel Labath NativeProcessLinux::Launch(LaunchArgs *args, Error &error) 1771af245d11STodd Fiala { 17720bce1b67STodd Fiala assert (args && "null args"); 1773af245d11STodd Fiala 1774af245d11STodd Fiala const char **argv = args->m_argv; 1775af245d11STodd Fiala const char **envp = args->m_envp; 1776af245d11STodd Fiala const char *working_dir = args->m_working_dir; 1777af245d11STodd Fiala 1778af245d11STodd Fiala lldb_utility::PseudoTerminal terminal; 1779af245d11STodd Fiala const size_t err_len = 1024; 1780af245d11STodd Fiala char err_str[err_len]; 1781af245d11STodd Fiala lldb::pid_t pid; 1782af245d11STodd Fiala NativeThreadProtocolSP thread_sp; 1783af245d11STodd Fiala 1784af245d11STodd Fiala lldb::ThreadSP inferior; 1785af245d11STodd Fiala 1786af245d11STodd Fiala // Propagate the environment if one is not supplied. 1787af245d11STodd Fiala if (envp == NULL || envp[0] == NULL) 1788af245d11STodd Fiala envp = const_cast<const char **>(environ); 1789af245d11STodd Fiala 1790af245d11STodd Fiala if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t> (-1)) 1791af245d11STodd Fiala { 1792bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 1793bd7cbc5aSPavel Labath error.SetErrorStringWithFormat("Process fork failed: %s", err_str); 1794bd7cbc5aSPavel Labath return -1; 1795af245d11STodd Fiala } 1796af245d11STodd Fiala 1797af245d11STodd Fiala // Recognized child exit status codes. 1798af245d11STodd Fiala enum { 1799af245d11STodd Fiala ePtraceFailed = 1, 1800af245d11STodd Fiala eDupStdinFailed, 1801af245d11STodd Fiala eDupStdoutFailed, 1802af245d11STodd Fiala eDupStderrFailed, 1803af245d11STodd Fiala eChdirFailed, 1804af245d11STodd Fiala eExecFailed, 1805af245d11STodd Fiala eSetGidFailed 1806af245d11STodd Fiala }; 1807af245d11STodd Fiala 1808af245d11STodd Fiala // Child process. 1809af245d11STodd Fiala if (pid == 0) 1810af245d11STodd Fiala { 181175f47c3aSTodd Fiala // FIXME consider opening a pipe between parent/child and have this forked child 181275f47c3aSTodd Fiala // send log info to parent re: launch status, in place of the log lines removed here. 1813af245d11STodd Fiala 181475f47c3aSTodd Fiala // Start tracing this child that is about to exec. 1815bd7cbc5aSPavel Labath PTRACE(PTRACE_TRACEME, 0, nullptr, nullptr, 0, error); 1816bd7cbc5aSPavel Labath if (error.Fail()) 1817af245d11STodd Fiala exit(ePtraceFailed); 1818af245d11STodd Fiala 1819493c3a12SPavel Labath // terminal has already dupped the tty descriptors to stdin/out/err. 1820493c3a12SPavel Labath // This closes original fd from which they were copied (and avoids 1821493c3a12SPavel Labath // leaking descriptors to the debugged process. 1822493c3a12SPavel Labath terminal.CloseSlaveFileDescriptor(); 1823493c3a12SPavel Labath 1824af245d11STodd Fiala // Do not inherit setgid powers. 1825af245d11STodd Fiala if (setgid(getgid()) != 0) 1826af245d11STodd Fiala exit(eSetGidFailed); 1827af245d11STodd Fiala 1828af245d11STodd Fiala // Attempt to have our own process group. 1829af245d11STodd Fiala if (setpgid(0, 0) != 0) 1830af245d11STodd Fiala { 183175f47c3aSTodd Fiala // FIXME log that this failed. This is common. 1832af245d11STodd Fiala // Don't allow this to prevent an inferior exec. 1833af245d11STodd Fiala } 1834af245d11STodd Fiala 1835af245d11STodd Fiala // Dup file descriptors if needed. 183675f47c3aSTodd Fiala if (!args->m_stdin_path.empty ()) 183775f47c3aSTodd Fiala if (!DupDescriptor(args->m_stdin_path.c_str (), STDIN_FILENO, O_RDONLY)) 1838af245d11STodd Fiala exit(eDupStdinFailed); 1839af245d11STodd Fiala 184075f47c3aSTodd Fiala if (!args->m_stdout_path.empty ()) 184114f4476aSTamas Berghammer if (!DupDescriptor(args->m_stdout_path.c_str (), STDOUT_FILENO, O_WRONLY | O_CREAT | O_TRUNC)) 1842af245d11STodd Fiala exit(eDupStdoutFailed); 1843af245d11STodd Fiala 184475f47c3aSTodd Fiala if (!args->m_stderr_path.empty ()) 184514f4476aSTamas Berghammer if (!DupDescriptor(args->m_stderr_path.c_str (), STDERR_FILENO, O_WRONLY | O_CREAT | O_TRUNC)) 1846af245d11STodd Fiala exit(eDupStderrFailed); 1847af245d11STodd Fiala 18489cf4f2c2SChaoren Lin // Close everything besides stdin, stdout, and stderr that has no file 18499cf4f2c2SChaoren Lin // action to avoid leaking 18509cf4f2c2SChaoren Lin for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd) 18519cf4f2c2SChaoren Lin if (!args->m_launch_info.GetFileActionForFD(fd)) 18529cf4f2c2SChaoren Lin close(fd); 18539cf4f2c2SChaoren Lin 1854af245d11STodd Fiala // Change working directory 1855af245d11STodd Fiala if (working_dir != NULL && working_dir[0]) 1856af245d11STodd Fiala if (0 != ::chdir(working_dir)) 1857af245d11STodd Fiala exit(eChdirFailed); 1858af245d11STodd Fiala 18590bce1b67STodd Fiala // Disable ASLR if requested. 18600bce1b67STodd Fiala if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR)) 18610bce1b67STodd Fiala { 18620bce1b67STodd Fiala const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS); 18630bce1b67STodd Fiala if (old_personality == -1) 18640bce1b67STodd Fiala { 186575f47c3aSTodd Fiala // Can't retrieve Linux personality. Cannot disable ASLR. 18660bce1b67STodd Fiala } 18670bce1b67STodd Fiala else 18680bce1b67STodd Fiala { 18690bce1b67STodd Fiala const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality); 18700bce1b67STodd Fiala if (new_personality == -1) 18710bce1b67STodd Fiala { 187275f47c3aSTodd Fiala // Disabling ASLR failed. 18730bce1b67STodd Fiala } 18740bce1b67STodd Fiala else 18750bce1b67STodd Fiala { 187675f47c3aSTodd Fiala // Disabling ASLR succeeded. 18770bce1b67STodd Fiala } 18780bce1b67STodd Fiala } 18790bce1b67STodd Fiala } 18800bce1b67STodd Fiala 188175f47c3aSTodd Fiala // Execute. We should never return... 1882af245d11STodd Fiala execve(argv[0], 1883af245d11STodd Fiala const_cast<char *const *>(argv), 1884af245d11STodd Fiala const_cast<char *const *>(envp)); 188575f47c3aSTodd Fiala 188675f47c3aSTodd Fiala // ...unless exec fails. In which case we definitely need to end the child here. 1887af245d11STodd Fiala exit(eExecFailed); 1888af245d11STodd Fiala } 1889af245d11STodd Fiala 189075f47c3aSTodd Fiala // 189175f47c3aSTodd Fiala // This is the parent code here. 189275f47c3aSTodd Fiala // 189375f47c3aSTodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 189475f47c3aSTodd Fiala 1895af245d11STodd Fiala // Wait for the child process to trap on its call to execve. 1896af245d11STodd Fiala ::pid_t wpid; 1897af245d11STodd Fiala int status; 1898af245d11STodd Fiala if ((wpid = waitpid(pid, &status, 0)) < 0) 1899af245d11STodd Fiala { 1900bd7cbc5aSPavel Labath error.SetErrorToErrno(); 1901af245d11STodd Fiala if (log) 1902bd7cbc5aSPavel Labath log->Printf ("NativeProcessLinux::%s waitpid for inferior failed with %s", 1903bd7cbc5aSPavel Labath __FUNCTION__, error.AsCString ()); 1904af245d11STodd Fiala 1905af245d11STodd Fiala // Mark the inferior as invalid. 1906af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 1907bd7cbc5aSPavel Labath SetState (StateType::eStateInvalid); 1908af245d11STodd Fiala 1909bd7cbc5aSPavel Labath return -1; 1910af245d11STodd Fiala } 1911af245d11STodd Fiala else if (WIFEXITED(status)) 1912af245d11STodd Fiala { 1913af245d11STodd Fiala // open, dup or execve likely failed for some reason. 1914bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 1915af245d11STodd Fiala switch (WEXITSTATUS(status)) 1916af245d11STodd Fiala { 1917af245d11STodd Fiala case ePtraceFailed: 1918bd7cbc5aSPavel Labath error.SetErrorString("Child ptrace failed."); 1919af245d11STodd Fiala break; 1920af245d11STodd Fiala case eDupStdinFailed: 1921bd7cbc5aSPavel Labath error.SetErrorString("Child open stdin failed."); 1922af245d11STodd Fiala break; 1923af245d11STodd Fiala case eDupStdoutFailed: 1924bd7cbc5aSPavel Labath error.SetErrorString("Child open stdout failed."); 1925af245d11STodd Fiala break; 1926af245d11STodd Fiala case eDupStderrFailed: 1927bd7cbc5aSPavel Labath error.SetErrorString("Child open stderr failed."); 1928af245d11STodd Fiala break; 1929af245d11STodd Fiala case eChdirFailed: 1930bd7cbc5aSPavel Labath error.SetErrorString("Child failed to set working directory."); 1931af245d11STodd Fiala break; 1932af245d11STodd Fiala case eExecFailed: 1933bd7cbc5aSPavel Labath error.SetErrorString("Child exec failed."); 1934af245d11STodd Fiala break; 1935af245d11STodd Fiala case eSetGidFailed: 1936bd7cbc5aSPavel Labath error.SetErrorString("Child setgid failed."); 1937af245d11STodd Fiala break; 1938af245d11STodd Fiala default: 1939bd7cbc5aSPavel Labath error.SetErrorString("Child returned unknown exit status."); 1940af245d11STodd Fiala break; 1941af245d11STodd Fiala } 1942af245d11STodd Fiala 1943af245d11STodd Fiala if (log) 1944af245d11STodd Fiala { 1945af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior exited with status %d before issuing a STOP", 1946af245d11STodd Fiala __FUNCTION__, 1947af245d11STodd Fiala WEXITSTATUS(status)); 1948af245d11STodd Fiala } 1949af245d11STodd Fiala 1950af245d11STodd Fiala // Mark the inferior as invalid. 1951af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 1952bd7cbc5aSPavel Labath SetState (StateType::eStateInvalid); 1953af245d11STodd Fiala 1954bd7cbc5aSPavel Labath return -1; 1955af245d11STodd Fiala } 1956af245d11STodd Fiala assert(WIFSTOPPED(status) && (wpid == static_cast< ::pid_t> (pid)) && 1957af245d11STodd Fiala "Could not sync with inferior process."); 1958af245d11STodd Fiala 1959af245d11STodd Fiala if (log) 1960af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior started, now in stopped state", __FUNCTION__); 1961af245d11STodd Fiala 1962bd7cbc5aSPavel Labath error = SetDefaultPtraceOpts(pid); 1963bd7cbc5aSPavel Labath if (error.Fail()) 1964af245d11STodd Fiala { 1965af245d11STodd Fiala if (log) 1966af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior failed to set default ptrace options: %s", 1967bd7cbc5aSPavel Labath __FUNCTION__, error.AsCString ()); 1968af245d11STodd Fiala 1969af245d11STodd Fiala // Mark the inferior as invalid. 1970af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 1971bd7cbc5aSPavel Labath SetState (StateType::eStateInvalid); 1972af245d11STodd Fiala 1973bd7cbc5aSPavel Labath return -1; 1974af245d11STodd Fiala } 1975af245d11STodd Fiala 1976af245d11STodd Fiala // Release the master terminal descriptor and pass it off to the 1977af245d11STodd Fiala // NativeProcessLinux instance. Similarly stash the inferior pid. 1978bd7cbc5aSPavel Labath m_terminal_fd = terminal.ReleaseMasterFileDescriptor(); 1979bd7cbc5aSPavel Labath m_pid = pid; 1980af245d11STodd Fiala 1981af245d11STodd Fiala // Set the terminal fd to be in non blocking mode (it simplifies the 1982af245d11STodd Fiala // implementation of ProcessLinux::GetSTDOUT to have a non-blocking 1983af245d11STodd Fiala // descriptor to read from). 1984bd7cbc5aSPavel Labath error = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); 1985bd7cbc5aSPavel Labath if (error.Fail()) 1986af245d11STodd Fiala { 1987af245d11STodd Fiala if (log) 1988af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior EnsureFDFlags failed for ensuring terminal O_NONBLOCK setting: %s", 1989bd7cbc5aSPavel Labath __FUNCTION__, error.AsCString ()); 1990af245d11STodd Fiala 1991af245d11STodd Fiala // Mark the inferior as invalid. 1992af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 1993bd7cbc5aSPavel Labath SetState (StateType::eStateInvalid); 1994af245d11STodd Fiala 1995bd7cbc5aSPavel Labath return -1; 1996af245d11STodd Fiala } 1997af245d11STodd Fiala 1998af245d11STodd Fiala if (log) 1999af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() adding pid = %" PRIu64, __FUNCTION__, pid); 2000af245d11STodd Fiala 2001bd7cbc5aSPavel Labath thread_sp = AddThread (pid); 2002af245d11STodd Fiala assert (thread_sp && "AddThread() returned a nullptr thread"); 2003bd7cbc5aSPavel Labath NotifyThreadCreateStopped (pid); 2004cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGSTOP); 2005af245d11STodd Fiala 2006af245d11STodd Fiala // Let our process instance know the thread has stopped. 2007bd7cbc5aSPavel Labath SetCurrentThreadID (thread_sp->GetID ()); 2008bd7cbc5aSPavel Labath SetState (StateType::eStateStopped); 2009af245d11STodd Fiala 2010af245d11STodd Fiala if (log) 2011af245d11STodd Fiala { 2012bd7cbc5aSPavel Labath if (error.Success ()) 2013af245d11STodd Fiala { 2014af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior launching succeeded", __FUNCTION__); 2015af245d11STodd Fiala } 2016af245d11STodd Fiala else 2017af245d11STodd Fiala { 2018af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior launching failed: %s", 2019bd7cbc5aSPavel Labath __FUNCTION__, error.AsCString ()); 2020bd7cbc5aSPavel Labath return -1; 2021af245d11STodd Fiala } 2022af245d11STodd Fiala } 2023bd7cbc5aSPavel Labath return pid; 2024af245d11STodd Fiala } 2025af245d11STodd Fiala 2026bd7cbc5aSPavel Labath ::pid_t 2027bd7cbc5aSPavel Labath NativeProcessLinux::Attach(lldb::pid_t pid, Error &error) 2028af245d11STodd Fiala { 2029af245d11STodd Fiala lldb::ThreadSP inferior; 2030af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2031af245d11STodd Fiala 2032af245d11STodd Fiala // Use a map to keep track of the threads which we have attached/need to attach. 2033af245d11STodd Fiala Host::TidMap tids_to_attach; 2034af245d11STodd Fiala if (pid <= 1) 2035af245d11STodd Fiala { 2036bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 2037bd7cbc5aSPavel Labath error.SetErrorString("Attaching to process 1 is not allowed."); 2038bd7cbc5aSPavel Labath return -1; 2039af245d11STodd Fiala } 2040af245d11STodd Fiala 2041af245d11STodd Fiala while (Host::FindProcessThreads(pid, tids_to_attach)) 2042af245d11STodd Fiala { 2043af245d11STodd Fiala for (Host::TidMap::iterator it = tids_to_attach.begin(); 2044af245d11STodd Fiala it != tids_to_attach.end();) 2045af245d11STodd Fiala { 2046af245d11STodd Fiala if (it->second == false) 2047af245d11STodd Fiala { 2048af245d11STodd Fiala lldb::tid_t tid = it->first; 2049af245d11STodd Fiala 2050af245d11STodd Fiala // Attach to the requested process. 2051af245d11STodd Fiala // An attach will cause the thread to stop with a SIGSTOP. 2052bd7cbc5aSPavel Labath PTRACE(PTRACE_ATTACH, tid, nullptr, nullptr, 0, error); 2053bd7cbc5aSPavel Labath if (error.Fail()) 2054af245d11STodd Fiala { 2055af245d11STodd Fiala // No such thread. The thread may have exited. 2056af245d11STodd Fiala // More error handling may be needed. 2057bd7cbc5aSPavel Labath if (error.GetError() == ESRCH) 2058af245d11STodd Fiala { 2059af245d11STodd Fiala it = tids_to_attach.erase(it); 2060af245d11STodd Fiala continue; 2061af245d11STodd Fiala } 2062af245d11STodd Fiala else 2063bd7cbc5aSPavel Labath return -1; 2064af245d11STodd Fiala } 2065af245d11STodd Fiala 2066af245d11STodd Fiala int status; 2067af245d11STodd Fiala // Need to use __WALL otherwise we receive an error with errno=ECHLD 2068af245d11STodd Fiala // At this point we should have a thread stopped if waitpid succeeds. 2069af245d11STodd Fiala if ((status = waitpid(tid, NULL, __WALL)) < 0) 2070af245d11STodd Fiala { 2071af245d11STodd Fiala // No such thread. The thread may have exited. 2072af245d11STodd Fiala // More error handling may be needed. 2073af245d11STodd Fiala if (errno == ESRCH) 2074af245d11STodd Fiala { 2075af245d11STodd Fiala it = tids_to_attach.erase(it); 2076af245d11STodd Fiala continue; 2077af245d11STodd Fiala } 2078af245d11STodd Fiala else 2079af245d11STodd Fiala { 2080bd7cbc5aSPavel Labath error.SetErrorToErrno(); 2081bd7cbc5aSPavel Labath return -1; 2082af245d11STodd Fiala } 2083af245d11STodd Fiala } 2084af245d11STodd Fiala 2085bd7cbc5aSPavel Labath error = SetDefaultPtraceOpts(tid); 2086bd7cbc5aSPavel Labath if (error.Fail()) 2087bd7cbc5aSPavel Labath return -1; 2088af245d11STodd Fiala 2089af245d11STodd Fiala if (log) 2090af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() adding tid = %" PRIu64, __FUNCTION__, tid); 2091af245d11STodd Fiala 2092af245d11STodd Fiala it->second = true; 2093af245d11STodd Fiala 2094af245d11STodd Fiala // Create the thread, mark it as stopped. 2095bd7cbc5aSPavel Labath NativeThreadProtocolSP thread_sp (AddThread (static_cast<lldb::tid_t> (tid))); 2096af245d11STodd Fiala assert (thread_sp && "AddThread() returned a nullptr"); 2097fa03ad2eSChaoren Lin 2098fa03ad2eSChaoren Lin // This will notify this is a new thread and tell the system it is stopped. 2099bd7cbc5aSPavel Labath NotifyThreadCreateStopped (tid); 2100cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGSTOP); 2101bd7cbc5aSPavel Labath SetCurrentThreadID (thread_sp->GetID ()); 2102af245d11STodd Fiala } 2103af245d11STodd Fiala 2104af245d11STodd Fiala // move the loop forward 2105af245d11STodd Fiala ++it; 2106af245d11STodd Fiala } 2107af245d11STodd Fiala } 2108af245d11STodd Fiala 2109af245d11STodd Fiala if (tids_to_attach.size() > 0) 2110af245d11STodd Fiala { 2111bd7cbc5aSPavel Labath m_pid = pid; 2112af245d11STodd Fiala // Let our process instance know the thread has stopped. 2113bd7cbc5aSPavel Labath SetState (StateType::eStateStopped); 2114af245d11STodd Fiala } 2115af245d11STodd Fiala else 2116af245d11STodd Fiala { 2117bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 2118bd7cbc5aSPavel Labath error.SetErrorString("No such process."); 2119bd7cbc5aSPavel Labath return -1; 2120af245d11STodd Fiala } 2121af245d11STodd Fiala 2122bd7cbc5aSPavel Labath return pid; 2123af245d11STodd Fiala } 2124af245d11STodd Fiala 212597ccc294SChaoren Lin Error 2126af245d11STodd Fiala NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) 2127af245d11STodd Fiala { 2128af245d11STodd Fiala long ptrace_opts = 0; 2129af245d11STodd Fiala 2130af245d11STodd Fiala // Have the child raise an event on exit. This is used to keep the child in 2131af245d11STodd Fiala // limbo until it is destroyed. 2132af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXIT; 2133af245d11STodd Fiala 2134af245d11STodd Fiala // Have the tracer trace threads which spawn in the inferior process. 2135af245d11STodd Fiala // TODO: if we want to support tracing the inferiors' child, add the 2136af245d11STodd Fiala // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK) 2137af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACECLONE; 2138af245d11STodd Fiala 2139af245d11STodd Fiala // Have the tracer notify us before execve returns 2140af245d11STodd Fiala // (needed to disable legacy SIGTRAP generation) 2141af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXEC; 2142af245d11STodd Fiala 214397ccc294SChaoren Lin Error error; 214497ccc294SChaoren Lin PTRACE(PTRACE_SETOPTIONS, pid, nullptr, (void*)ptrace_opts, 0, error); 214597ccc294SChaoren Lin return error; 2146af245d11STodd Fiala } 2147af245d11STodd Fiala 2148af245d11STodd Fiala static ExitType convert_pid_status_to_exit_type (int status) 2149af245d11STodd Fiala { 2150af245d11STodd Fiala if (WIFEXITED (status)) 2151af245d11STodd Fiala return ExitType::eExitTypeExit; 2152af245d11STodd Fiala else if (WIFSIGNALED (status)) 2153af245d11STodd Fiala return ExitType::eExitTypeSignal; 2154af245d11STodd Fiala else if (WIFSTOPPED (status)) 2155af245d11STodd Fiala return ExitType::eExitTypeStop; 2156af245d11STodd Fiala else 2157af245d11STodd Fiala { 2158af245d11STodd Fiala // We don't know what this is. 2159af245d11STodd Fiala return ExitType::eExitTypeInvalid; 2160af245d11STodd Fiala } 2161af245d11STodd Fiala } 2162af245d11STodd Fiala 2163af245d11STodd Fiala static int convert_pid_status_to_return_code (int status) 2164af245d11STodd Fiala { 2165af245d11STodd Fiala if (WIFEXITED (status)) 2166af245d11STodd Fiala return WEXITSTATUS (status); 2167af245d11STodd Fiala else if (WIFSIGNALED (status)) 2168af245d11STodd Fiala return WTERMSIG (status); 2169af245d11STodd Fiala else if (WIFSTOPPED (status)) 2170af245d11STodd Fiala return WSTOPSIG (status); 2171af245d11STodd Fiala else 2172af245d11STodd Fiala { 2173af245d11STodd Fiala // We don't know what this is. 2174af245d11STodd Fiala return ExitType::eExitTypeInvalid; 2175af245d11STodd Fiala } 2176af245d11STodd Fiala } 2177af245d11STodd Fiala 21781107b5a5SPavel Labath // Handles all waitpid events from the inferior process. 21791107b5a5SPavel Labath void 21801107b5a5SPavel Labath NativeProcessLinux::MonitorCallback(lldb::pid_t pid, 2181af245d11STodd Fiala bool exited, 2182af245d11STodd Fiala int signal, 2183af245d11STodd Fiala int status) 2184af245d11STodd Fiala { 2185af245d11STodd Fiala Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); 2186af245d11STodd Fiala 2187af245d11STodd Fiala // Certain activities differ based on whether the pid is the tid of the main thread. 21881107b5a5SPavel Labath const bool is_main_thread = (pid == GetID ()); 2189af245d11STodd Fiala 2190af245d11STodd Fiala // Handle when the thread exits. 2191af245d11STodd Fiala if (exited) 2192af245d11STodd Fiala { 2193af245d11STodd Fiala if (log) 219486fd8e45SChaoren Lin log->Printf ("NativeProcessLinux::%s() got exit signal(%d) , tid = %" PRIu64 " (%s main thread)", __FUNCTION__, signal, pid, is_main_thread ? "is" : "is not"); 2195af245d11STodd Fiala 2196af245d11STodd Fiala // This is a thread that exited. Ensure we're not tracking it anymore. 21971107b5a5SPavel Labath const bool thread_found = StopTrackingThread (pid); 2198af245d11STodd Fiala 2199fa03ad2eSChaoren Lin // Make sure the thread state coordinator knows about this. 22001107b5a5SPavel Labath NotifyThreadDeath (pid); 2201fa03ad2eSChaoren Lin 2202af245d11STodd Fiala if (is_main_thread) 2203af245d11STodd Fiala { 2204af245d11STodd Fiala // We only set the exit status and notify the delegate if we haven't already set the process 2205af245d11STodd Fiala // state to an exited state. We normally should have received a SIGTRAP | (PTRACE_EVENT_EXIT << 8) 2206af245d11STodd Fiala // for the main thread. 22071107b5a5SPavel Labath const bool already_notified = (GetState() == StateType::eStateExited) || (GetState () == StateType::eStateCrashed); 2208af245d11STodd Fiala if (!already_notified) 2209af245d11STodd Fiala { 2210af245d11STodd Fiala if (log) 22111107b5a5SPavel Labath log->Printf ("NativeProcessLinux::%s() tid = %" PRIu64 " handling main thread exit (%s), expected exit state already set but state was %s instead, setting exit state now", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found", StateAsCString (GetState ())); 2212af245d11STodd Fiala // The main thread exited. We're done monitoring. Report to delegate. 22131107b5a5SPavel Labath SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); 2214af245d11STodd Fiala 2215af245d11STodd Fiala // Notify delegate that our process has exited. 22161107b5a5SPavel Labath SetState (StateType::eStateExited, true); 2217af245d11STodd Fiala } 2218af245d11STodd Fiala else 2219af245d11STodd Fiala { 2220af245d11STodd Fiala if (log) 2221af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() tid = %" PRIu64 " main thread now exited (%s)", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found"); 2222af245d11STodd Fiala } 2223af245d11STodd Fiala } 2224af245d11STodd Fiala else 2225af245d11STodd Fiala { 2226af245d11STodd Fiala // Do we want to report to the delegate in this case? I think not. If this was an orderly 2227af245d11STodd Fiala // thread exit, we would already have received the SIGTRAP | (PTRACE_EVENT_EXIT << 8) signal, 2228af245d11STodd Fiala // and we would have done an all-stop then. 2229af245d11STodd Fiala if (log) 2230af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() tid = %" PRIu64 " handling non-main thread exit (%s)", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found"); 2231af245d11STodd Fiala } 22321107b5a5SPavel Labath return; 2233af245d11STodd Fiala } 2234af245d11STodd Fiala 2235af245d11STodd Fiala // Get details on the signal raised. 2236af245d11STodd Fiala siginfo_t info; 22371107b5a5SPavel Labath const auto err = GetSignalInfo(pid, &info); 223897ccc294SChaoren Lin if (err.Success()) 2239fa03ad2eSChaoren Lin { 2240fa03ad2eSChaoren Lin // We have retrieved the signal info. Dispatch appropriately. 2241fa03ad2eSChaoren Lin if (info.si_signo == SIGTRAP) 22421107b5a5SPavel Labath MonitorSIGTRAP(&info, pid); 2243fa03ad2eSChaoren Lin else 22441107b5a5SPavel Labath MonitorSignal(&info, pid, exited); 2245fa03ad2eSChaoren Lin } 2246fa03ad2eSChaoren Lin else 2247af245d11STodd Fiala { 224897ccc294SChaoren Lin if (err.GetError() == EINVAL) 2249af245d11STodd Fiala { 2250fa03ad2eSChaoren Lin // This is a group stop reception for this tid. 2251fa03ad2eSChaoren Lin if (log) 22521107b5a5SPavel Labath log->Printf ("NativeThreadLinux::%s received a group stop for pid %" PRIu64 " tid %" PRIu64, __FUNCTION__, GetID (), pid); 22531107b5a5SPavel Labath NotifyThreadStop (pid); 2254a9882ceeSTodd Fiala } 2255a9882ceeSTodd Fiala else 2256a9882ceeSTodd Fiala { 2257af245d11STodd Fiala // ptrace(GETSIGINFO) failed (but not due to group-stop). 2258af245d11STodd Fiala 2259af245d11STodd Fiala // A return value of ESRCH means the thread/process is no longer on the system, 2260af245d11STodd Fiala // so it was killed somehow outside of our control. Either way, we can't do anything 2261af245d11STodd Fiala // with it anymore. 2262af245d11STodd Fiala 2263af245d11STodd Fiala // Stop tracking the metadata for the thread since it's entirely off the system now. 22641107b5a5SPavel Labath const bool thread_found = StopTrackingThread (pid); 2265af245d11STodd Fiala 2266fa03ad2eSChaoren Lin // Make sure the thread state coordinator knows about this. 22671107b5a5SPavel Labath NotifyThreadDeath (pid); 2268fa03ad2eSChaoren Lin 2269af245d11STodd Fiala if (log) 2270af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d (%s, %s, %s)", 227197ccc294SChaoren Lin __FUNCTION__, err.AsCString(), pid, signal, status, err.GetError() == ESRCH ? "thread/process killed" : "unknown reason", is_main_thread ? "is main thread" : "is not main thread", thread_found ? "thread metadata removed" : "thread metadata not found"); 2272af245d11STodd Fiala 2273af245d11STodd Fiala if (is_main_thread) 2274af245d11STodd Fiala { 2275af245d11STodd Fiala // Notify the delegate - our process is not available but appears to have been killed outside 2276af245d11STodd Fiala // our control. Is eStateExited the right exit state in this case? 22771107b5a5SPavel Labath SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); 22781107b5a5SPavel Labath SetState (StateType::eStateExited, true); 2279af245d11STodd Fiala } 2280af245d11STodd Fiala else 2281af245d11STodd Fiala { 2282af245d11STodd Fiala // This thread was pulled out from underneath us. Anything to do here? Do we want to do an all stop? 2283af245d11STodd Fiala if (log) 22841107b5a5SPavel Labath log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 " non-main thread exit occurred, didn't tell delegate anything since thread disappeared out from underneath us", __FUNCTION__, GetID (), pid); 2285af245d11STodd Fiala } 2286af245d11STodd Fiala } 2287af245d11STodd Fiala } 2288af245d11STodd Fiala } 2289af245d11STodd Fiala 2290af245d11STodd Fiala void 2291426bdf88SPavel Labath NativeProcessLinux::WaitForNewThread(::pid_t tid) 2292426bdf88SPavel Labath { 2293426bdf88SPavel Labath Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2294426bdf88SPavel Labath 2295426bdf88SPavel Labath NativeThreadProtocolSP new_thread_sp = GetThreadByID(tid); 2296426bdf88SPavel Labath 2297426bdf88SPavel Labath if (new_thread_sp) 2298426bdf88SPavel Labath { 2299426bdf88SPavel Labath // We are already tracking the thread - we got the event on the new thread (see 2300426bdf88SPavel Labath // MonitorSignal) before this one. We are done. 2301426bdf88SPavel Labath return; 2302426bdf88SPavel Labath } 2303426bdf88SPavel Labath 2304426bdf88SPavel Labath // The thread is not tracked yet, let's wait for it to appear. 2305426bdf88SPavel Labath int status = -1; 2306426bdf88SPavel Labath ::pid_t wait_pid; 2307426bdf88SPavel Labath do 2308426bdf88SPavel Labath { 2309426bdf88SPavel Labath if (log) 2310426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() received thread creation event for tid %" PRIu32 ". tid not tracked yet, waiting for thread to appear...", __FUNCTION__, tid); 2311426bdf88SPavel Labath wait_pid = waitpid(tid, &status, __WALL); 2312426bdf88SPavel Labath } 2313426bdf88SPavel Labath while (wait_pid == -1 && errno == EINTR); 2314426bdf88SPavel Labath // Since we are waiting on a specific tid, this must be the creation event. But let's do 2315426bdf88SPavel Labath // some checks just in case. 2316426bdf88SPavel Labath if (wait_pid != tid) { 2317426bdf88SPavel Labath if (log) 2318426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() waiting for tid %" PRIu32 " failed. Assuming the thread has disappeared in the meantime", __FUNCTION__, tid); 2319426bdf88SPavel Labath // The only way I know of this could happen is if the whole process was 2320426bdf88SPavel Labath // SIGKILLed in the mean time. In any case, we can't do anything about that now. 2321426bdf88SPavel Labath return; 2322426bdf88SPavel Labath } 2323426bdf88SPavel Labath if (WIFEXITED(status)) 2324426bdf88SPavel Labath { 2325426bdf88SPavel Labath if (log) 2326426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() waiting for tid %" PRIu32 " returned an 'exited' event. Not tracking the thread.", __FUNCTION__, tid); 2327426bdf88SPavel Labath // Also a very improbable event. 2328426bdf88SPavel Labath return; 2329426bdf88SPavel Labath } 2330426bdf88SPavel Labath 2331426bdf88SPavel Labath siginfo_t info; 2332426bdf88SPavel Labath Error error = GetSignalInfo(tid, &info); 2333426bdf88SPavel Labath if (error.Fail()) 2334426bdf88SPavel Labath { 2335426bdf88SPavel Labath if (log) 2336426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() GetSignalInfo for tid %" PRIu32 " failed. Assuming the thread has disappeared in the meantime.", __FUNCTION__, tid); 2337426bdf88SPavel Labath return; 2338426bdf88SPavel Labath } 2339426bdf88SPavel Labath 2340426bdf88SPavel Labath if (((info.si_pid != 0) || (info.si_code != SI_USER)) && log) 2341426bdf88SPavel Labath { 2342426bdf88SPavel Labath // We should be getting a thread creation signal here, but we received something 2343426bdf88SPavel Labath // else. There isn't much we can do about it now, so we will just log that. Since the 2344426bdf88SPavel Labath // thread is alive and we are receiving events from it, we shall pretend that it was 2345426bdf88SPavel Labath // created properly. 2346426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() GetSignalInfo for tid %" PRIu32 " received unexpected signal with code %d from pid %d.", __FUNCTION__, tid, info.si_code, info.si_pid); 2347426bdf88SPavel Labath } 2348426bdf88SPavel Labath 2349426bdf88SPavel Labath if (log) 2350426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 ": tracking new thread tid %" PRIu32, 2351426bdf88SPavel Labath __FUNCTION__, GetID (), tid); 2352426bdf88SPavel Labath 2353426bdf88SPavel Labath new_thread_sp = AddThread(tid); 2354426bdf88SPavel Labath std::static_pointer_cast<NativeThreadLinux> (new_thread_sp)->SetRunning (); 2355426bdf88SPavel Labath Resume (tid, LLDB_INVALID_SIGNAL_NUMBER); 2356c076559aSPavel Labath NotifyThreadCreate (tid, false, CoordinatorErrorHandler); 2357426bdf88SPavel Labath } 2358426bdf88SPavel Labath 2359426bdf88SPavel Labath void 2360af245d11STodd Fiala NativeProcessLinux::MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid) 2361af245d11STodd Fiala { 2362af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2363af245d11STodd Fiala const bool is_main_thread = (pid == GetID ()); 2364af245d11STodd Fiala 2365af245d11STodd Fiala assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!"); 2366af245d11STodd Fiala if (!info) 2367af245d11STodd Fiala return; 2368af245d11STodd Fiala 23695830aa75STamas Berghammer Mutex::Locker locker (m_threads_mutex); 23705830aa75STamas Berghammer 2371af245d11STodd Fiala // See if we can find a thread for this signal. 2372af245d11STodd Fiala NativeThreadProtocolSP thread_sp = GetThreadByID (pid); 2373af245d11STodd Fiala if (!thread_sp) 2374af245d11STodd Fiala { 2375af245d11STodd Fiala if (log) 2376af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " no thread found for tid %" PRIu64, __FUNCTION__, GetID (), pid); 2377af245d11STodd Fiala } 2378af245d11STodd Fiala 2379af245d11STodd Fiala switch (info->si_code) 2380af245d11STodd Fiala { 2381af245d11STodd Fiala // TODO: these two cases are required if we want to support tracing of the inferiors' children. We'd need this to debug a monitor. 2382af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)): 2383af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)): 2384af245d11STodd Fiala 2385af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)): 2386af245d11STodd Fiala { 23875fd24c67SPavel Labath // This is the notification on the parent thread which informs us of new thread 2388426bdf88SPavel Labath // creation. 2389426bdf88SPavel Labath // We don't want to do anything with the parent thread so we just resume it. In case we 2390426bdf88SPavel Labath // want to implement "break on thread creation" functionality, we would need to stop 2391426bdf88SPavel Labath // here. 2392af245d11STodd Fiala 2393af245d11STodd Fiala unsigned long event_message = 0; 2394426bdf88SPavel Labath if (GetEventMessage (pid, &event_message).Fail()) 2395fa03ad2eSChaoren Lin { 2396426bdf88SPavel Labath if (log) 2397fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " received thread creation event but GetEventMessage failed so we don't know the new tid", __FUNCTION__, pid); 2398426bdf88SPavel Labath } else 2399426bdf88SPavel Labath WaitForNewThread(event_message); 2400af245d11STodd Fiala 24015fd24c67SPavel Labath Resume (pid, LLDB_INVALID_SIGNAL_NUMBER); 2402af245d11STodd Fiala break; 2403af245d11STodd Fiala } 2404af245d11STodd Fiala 2405af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): 2406a9882ceeSTodd Fiala { 2407a9882ceeSTodd Fiala NativeThreadProtocolSP main_thread_sp; 2408af245d11STodd Fiala if (log) 2409af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP); 2410a9882ceeSTodd Fiala 2411fa03ad2eSChaoren Lin // The thread state coordinator needs to reset due to the exec. 2412c076559aSPavel Labath ResetForExec (); 2413fa03ad2eSChaoren Lin 2414fa03ad2eSChaoren Lin // Remove all but the main thread here. Linux fork creates a new process which only copies the main thread. Mutexes are in undefined state. 2415a9882ceeSTodd Fiala if (log) 2416a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s exec received, stop tracking all but main thread", __FUNCTION__); 2417a9882ceeSTodd Fiala 2418a9882ceeSTodd Fiala for (auto thread_sp : m_threads) 2419a9882ceeSTodd Fiala { 2420a9882ceeSTodd Fiala const bool is_main_thread = thread_sp && thread_sp->GetID () == GetID (); 2421a9882ceeSTodd Fiala if (is_main_thread) 2422a9882ceeSTodd Fiala { 2423a9882ceeSTodd Fiala main_thread_sp = thread_sp; 2424a9882ceeSTodd Fiala if (log) 2425a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s found main thread with tid %" PRIu64 ", keeping", __FUNCTION__, main_thread_sp->GetID ()); 2426a9882ceeSTodd Fiala } 2427a9882ceeSTodd Fiala else 2428a9882ceeSTodd Fiala { 2429fa03ad2eSChaoren Lin // Tell thread coordinator this thread is dead. 2430a9882ceeSTodd Fiala if (log) 2431a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s discarding non-main-thread tid %" PRIu64 " due to exec", __FUNCTION__, thread_sp->GetID ()); 2432a9882ceeSTodd Fiala } 2433a9882ceeSTodd Fiala } 2434a9882ceeSTodd Fiala 2435a9882ceeSTodd Fiala m_threads.clear (); 2436a9882ceeSTodd Fiala 2437a9882ceeSTodd Fiala if (main_thread_sp) 2438a9882ceeSTodd Fiala { 2439a9882ceeSTodd Fiala m_threads.push_back (main_thread_sp); 2440a9882ceeSTodd Fiala SetCurrentThreadID (main_thread_sp->GetID ()); 2441cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (main_thread_sp)->SetStoppedByExec (); 2442a9882ceeSTodd Fiala } 2443a9882ceeSTodd Fiala else 2444a9882ceeSTodd Fiala { 2445a9882ceeSTodd Fiala SetCurrentThreadID (LLDB_INVALID_THREAD_ID); 2446a9882ceeSTodd Fiala if (log) 2447a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 "no main thread found, discarded all threads, we're in a no-thread state!", __FUNCTION__, GetID ()); 2448a9882ceeSTodd Fiala } 2449a9882ceeSTodd Fiala 2450fa03ad2eSChaoren Lin // Tell coordinator about about the "new" (since exec) stopped main thread. 2451fa03ad2eSChaoren Lin const lldb::tid_t main_thread_tid = GetID (); 2452fa03ad2eSChaoren Lin NotifyThreadCreateStopped (main_thread_tid); 2453fa03ad2eSChaoren Lin 2454fa03ad2eSChaoren Lin // NOTE: ideally these next statements would execute at the same time as the coordinator thread create was executed. 2455fa03ad2eSChaoren Lin // Consider a handler that can execute when that happens. 2456a9882ceeSTodd Fiala // Let our delegate know we have just exec'd. 2457a9882ceeSTodd Fiala NotifyDidExec (); 2458a9882ceeSTodd Fiala 2459a9882ceeSTodd Fiala // If we have a main thread, indicate we are stopped. 2460a9882ceeSTodd Fiala assert (main_thread_sp && "exec called during ptraced process but no main thread metadata tracked"); 2461fa03ad2eSChaoren Lin 2462fa03ad2eSChaoren Lin // Let the process know we're stopped. 2463ed89c7feSPavel Labath StopRunningThreads (pid); 2464a9882ceeSTodd Fiala 2465af245d11STodd Fiala break; 2466a9882ceeSTodd Fiala } 2467af245d11STodd Fiala 2468af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): 2469af245d11STodd Fiala { 2470af245d11STodd Fiala // The inferior process or one of its threads is about to exit. 2471fa03ad2eSChaoren Lin 2472fa03ad2eSChaoren Lin // This thread is currently stopped. It's not actually dead yet, just about to be. 2473fa03ad2eSChaoren Lin NotifyThreadStop (pid); 2474fa03ad2eSChaoren Lin 2475af245d11STodd Fiala unsigned long data = 0; 247697ccc294SChaoren Lin if (GetEventMessage(pid, &data).Fail()) 2477af245d11STodd Fiala data = -1; 2478af245d11STodd Fiala 2479af245d11STodd Fiala if (log) 2480af245d11STodd Fiala { 2481af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received PTRACE_EVENT_EXIT, data = %lx (WIFEXITED=%s,WIFSIGNALED=%s), pid = %" PRIu64 " (%s)", 2482af245d11STodd Fiala __FUNCTION__, 2483af245d11STodd Fiala data, WIFEXITED (data) ? "true" : "false", WIFSIGNALED (data) ? "true" : "false", 2484af245d11STodd Fiala pid, 2485af245d11STodd Fiala is_main_thread ? "is main thread" : "not main thread"); 2486af245d11STodd Fiala } 2487af245d11STodd Fiala 2488af245d11STodd Fiala if (is_main_thread) 2489af245d11STodd Fiala { 2490af245d11STodd Fiala SetExitStatus (convert_pid_status_to_exit_type (data), convert_pid_status_to_return_code (data), nullptr, true); 249175f47c3aSTodd Fiala } 249275f47c3aSTodd Fiala 24939d617ba6SChaoren Lin const int signo = static_cast<int> (data); 2494c076559aSPavel Labath RequestThreadResume (pid, 249586fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 2496fa03ad2eSChaoren Lin { 2497cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); 249837c768caSChaoren Lin return Resume (tid_to_resume, (supress_signal) ? LLDB_INVALID_SIGNAL_NUMBER : signo); 2499fa03ad2eSChaoren Lin }, 2500fa03ad2eSChaoren Lin CoordinatorErrorHandler); 2501af245d11STodd Fiala 2502af245d11STodd Fiala break; 2503af245d11STodd Fiala } 2504af245d11STodd Fiala 2505af245d11STodd Fiala case 0: 2506c16f5dcaSChaoren Lin case TRAP_TRACE: // We receive this on single stepping. 2507c16f5dcaSChaoren Lin case TRAP_HWBKPT: // We receive this on watchpoint hit 250886fd8e45SChaoren Lin if (thread_sp) 250986fd8e45SChaoren Lin { 2510c16f5dcaSChaoren Lin // If a watchpoint was hit, report it 2511c16f5dcaSChaoren Lin uint32_t wp_index; 2512c16f5dcaSChaoren Lin Error error = thread_sp->GetRegisterContext()->GetWatchpointHitIndex(wp_index); 2513c16f5dcaSChaoren Lin if (error.Fail() && log) 2514c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() " 2515c16f5dcaSChaoren Lin "received error while checking for watchpoint hits, " 2516c16f5dcaSChaoren Lin "pid = %" PRIu64 " error = %s", 2517c16f5dcaSChaoren Lin __FUNCTION__, pid, error.AsCString()); 2518c16f5dcaSChaoren Lin if (wp_index != LLDB_INVALID_INDEX32) 25195830aa75STamas Berghammer { 2520c16f5dcaSChaoren Lin MonitorWatchpoint(pid, thread_sp, wp_index); 2521c16f5dcaSChaoren Lin break; 2522c16f5dcaSChaoren Lin } 2523c16f5dcaSChaoren Lin } 2524c16f5dcaSChaoren Lin // Otherwise, report step over 2525c16f5dcaSChaoren Lin MonitorTrace(pid, thread_sp); 2526af245d11STodd Fiala break; 2527af245d11STodd Fiala 2528af245d11STodd Fiala case SI_KERNEL: 2529af245d11STodd Fiala case TRAP_BRKPT: 2530c16f5dcaSChaoren Lin MonitorBreakpoint(pid, thread_sp); 2531af245d11STodd Fiala break; 2532af245d11STodd Fiala 2533af245d11STodd Fiala case SIGTRAP: 2534af245d11STodd Fiala case (SIGTRAP | 0x80): 2535af245d11STodd Fiala if (log) 2536fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s() received unknown SIGTRAP system call stop event, pid %" PRIu64 "tid %" PRIu64 ", resuming", __FUNCTION__, GetID (), pid); 2537fa03ad2eSChaoren Lin 2538fa03ad2eSChaoren Lin // This thread is currently stopped. 2539fa03ad2eSChaoren Lin NotifyThreadStop (pid); 2540fa03ad2eSChaoren Lin if (thread_sp) 2541cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGTRAP); 2542fa03ad2eSChaoren Lin 2543fa03ad2eSChaoren Lin 2544af245d11STodd Fiala // Ignore these signals until we know more about them. 2545c076559aSPavel Labath RequestThreadResume (pid, 254686fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 2547fa03ad2eSChaoren Lin { 2548cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); 254937c768caSChaoren Lin return Resume (tid_to_resume, LLDB_INVALID_SIGNAL_NUMBER); 2550fa03ad2eSChaoren Lin }, 2551fa03ad2eSChaoren Lin CoordinatorErrorHandler); 2552af245d11STodd Fiala break; 2553af245d11STodd Fiala 2554af245d11STodd Fiala default: 2555af245d11STodd Fiala assert(false && "Unexpected SIGTRAP code!"); 2556af245d11STodd Fiala if (log) 2557af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 "tid %" PRIu64 " received unhandled SIGTRAP code: 0x%" PRIx64, __FUNCTION__, GetID (), pid, static_cast<uint64_t> (SIGTRAP | (PTRACE_EVENT_CLONE << 8))); 2558af245d11STodd Fiala break; 2559af245d11STodd Fiala 2560af245d11STodd Fiala } 2561af245d11STodd Fiala } 2562af245d11STodd Fiala 2563af245d11STodd Fiala void 2564c16f5dcaSChaoren Lin NativeProcessLinux::MonitorTrace(lldb::pid_t pid, NativeThreadProtocolSP thread_sp) 2565c16f5dcaSChaoren Lin { 2566c16f5dcaSChaoren Lin Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 2567c16f5dcaSChaoren Lin if (log) 2568c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() received trace event, pid = %" PRIu64 " (single stepping)", 2569c16f5dcaSChaoren Lin __FUNCTION__, pid); 2570c16f5dcaSChaoren Lin 2571c16f5dcaSChaoren Lin if (thread_sp) 2572c16f5dcaSChaoren Lin std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByTrace(); 2573c16f5dcaSChaoren Lin 2574c16f5dcaSChaoren Lin // This thread is currently stopped. 2575c16f5dcaSChaoren Lin NotifyThreadStop(pid); 2576c16f5dcaSChaoren Lin 2577c16f5dcaSChaoren Lin // Here we don't have to request the rest of the threads to stop or request a deferred stop. 2578c16f5dcaSChaoren Lin // This would have already happened at the time the Resume() with step operation was signaled. 2579c16f5dcaSChaoren Lin // At this point, we just need to say we stopped, and the deferred notifcation will fire off 2580c16f5dcaSChaoren Lin // once all running threads have checked in as stopped. 2581c16f5dcaSChaoren Lin SetCurrentThreadID(pid); 2582c16f5dcaSChaoren Lin // Tell the process we have a stop (from software breakpoint). 2583ed89c7feSPavel Labath StopRunningThreads(pid); 2584c16f5dcaSChaoren Lin } 2585c16f5dcaSChaoren Lin 2586c16f5dcaSChaoren Lin void 2587c16f5dcaSChaoren Lin NativeProcessLinux::MonitorBreakpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp) 2588c16f5dcaSChaoren Lin { 2589c16f5dcaSChaoren Lin Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 2590c16f5dcaSChaoren Lin if (log) 2591c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() received breakpoint event, pid = %" PRIu64, 2592c16f5dcaSChaoren Lin __FUNCTION__, pid); 2593c16f5dcaSChaoren Lin 2594c16f5dcaSChaoren Lin // This thread is currently stopped. 2595c16f5dcaSChaoren Lin NotifyThreadStop(pid); 2596c16f5dcaSChaoren Lin 2597c16f5dcaSChaoren Lin // Mark the thread as stopped at breakpoint. 2598c16f5dcaSChaoren Lin if (thread_sp) 2599c16f5dcaSChaoren Lin { 2600c16f5dcaSChaoren Lin std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByBreakpoint(); 2601c16f5dcaSChaoren Lin Error error = FixupBreakpointPCAsNeeded(thread_sp); 2602c16f5dcaSChaoren Lin if (error.Fail()) 2603c16f5dcaSChaoren Lin if (log) 2604c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " fixup: %s", 2605c16f5dcaSChaoren Lin __FUNCTION__, pid, error.AsCString()); 2606d8c338d4STamas Berghammer 2607d8c338d4STamas Berghammer auto it = m_threads_stepping_with_breakpoint.find(pid); 2608d8c338d4STamas Berghammer if (it != m_threads_stepping_with_breakpoint.end()) 2609d8c338d4STamas Berghammer { 2610d8c338d4STamas Berghammer Error error = RemoveBreakpoint (it->second); 2611d8c338d4STamas Berghammer if (error.Fail()) 2612d8c338d4STamas Berghammer if (log) 2613d8c338d4STamas Berghammer log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " remove stepping breakpoint: %s", 2614d8c338d4STamas Berghammer __FUNCTION__, pid, error.AsCString()); 2615d8c338d4STamas Berghammer 2616d8c338d4STamas Berghammer m_threads_stepping_with_breakpoint.erase(it); 2617d8c338d4STamas Berghammer std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByTrace(); 2618d8c338d4STamas Berghammer } 2619c16f5dcaSChaoren Lin } 2620c16f5dcaSChaoren Lin else 2621c16f5dcaSChaoren Lin if (log) 2622c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 ": " 2623c16f5dcaSChaoren Lin "warning, cannot process software breakpoint since no thread metadata", 2624c16f5dcaSChaoren Lin __FUNCTION__, pid); 2625c16f5dcaSChaoren Lin 2626c16f5dcaSChaoren Lin 2627c16f5dcaSChaoren Lin // We need to tell all other running threads before we notify the delegate about this stop. 2628ed89c7feSPavel Labath StopRunningThreads(pid); 2629c16f5dcaSChaoren Lin } 2630c16f5dcaSChaoren Lin 2631c16f5dcaSChaoren Lin void 2632c16f5dcaSChaoren Lin NativeProcessLinux::MonitorWatchpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp, uint32_t wp_index) 2633c16f5dcaSChaoren Lin { 2634c16f5dcaSChaoren Lin Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_WATCHPOINTS)); 2635c16f5dcaSChaoren Lin if (log) 2636c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() received watchpoint event, " 2637c16f5dcaSChaoren Lin "pid = %" PRIu64 ", wp_index = %" PRIu32, 2638c16f5dcaSChaoren Lin __FUNCTION__, pid, wp_index); 2639c16f5dcaSChaoren Lin 2640c16f5dcaSChaoren Lin // This thread is currently stopped. 2641c16f5dcaSChaoren Lin NotifyThreadStop(pid); 2642c16f5dcaSChaoren Lin 2643c16f5dcaSChaoren Lin // Mark the thread as stopped at watchpoint. 2644c16f5dcaSChaoren Lin // The address is at (lldb::addr_t)info->si_addr if we need it. 2645c16f5dcaSChaoren Lin lldbassert(thread_sp && "thread_sp cannot be NULL"); 2646c16f5dcaSChaoren Lin std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByWatchpoint(wp_index); 2647c16f5dcaSChaoren Lin 2648c16f5dcaSChaoren Lin // We need to tell all other running threads before we notify the delegate about this stop. 2649ed89c7feSPavel Labath StopRunningThreads(pid); 2650c16f5dcaSChaoren Lin } 2651c16f5dcaSChaoren Lin 2652c16f5dcaSChaoren Lin void 2653af245d11STodd Fiala NativeProcessLinux::MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited) 2654af245d11STodd Fiala { 2655511e5cdcSTodd Fiala assert (info && "null info"); 2656511e5cdcSTodd Fiala if (!info) 2657511e5cdcSTodd Fiala return; 2658511e5cdcSTodd Fiala 2659511e5cdcSTodd Fiala const int signo = info->si_signo; 2660511e5cdcSTodd Fiala const bool is_from_llgs = info->si_pid == getpid (); 2661af245d11STodd Fiala 2662af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2663af245d11STodd Fiala 2664af245d11STodd Fiala // POSIX says that process behaviour is undefined after it ignores a SIGFPE, 2665af245d11STodd Fiala // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a 2666af245d11STodd Fiala // kill(2) or raise(3). Similarly for tgkill(2) on Linux. 2667af245d11STodd Fiala // 2668af245d11STodd Fiala // IOW, user generated signals never generate what we consider to be a 2669af245d11STodd Fiala // "crash". 2670af245d11STodd Fiala // 2671af245d11STodd Fiala // Similarly, ACK signals generated by this monitor. 2672af245d11STodd Fiala 26735830aa75STamas Berghammer Mutex::Locker locker (m_threads_mutex); 26745830aa75STamas Berghammer 2675af245d11STodd Fiala // See if we can find a thread for this signal. 2676af245d11STodd Fiala NativeThreadProtocolSP thread_sp = GetThreadByID (pid); 2677af245d11STodd Fiala if (!thread_sp) 2678af245d11STodd Fiala { 2679af245d11STodd Fiala if (log) 2680af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " no thread found for tid %" PRIu64, __FUNCTION__, GetID (), pid); 2681af245d11STodd Fiala } 2682af245d11STodd Fiala 2683af245d11STodd Fiala // Handle the signal. 2684af245d11STodd Fiala if (info->si_code == SI_TKILL || info->si_code == SI_USER) 2685af245d11STodd Fiala { 2686af245d11STodd Fiala if (log) 2687af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received signal %s (%d) with code %s, (siginfo pid = %d (%s), waitpid pid = %" PRIu64 ")", 2688af245d11STodd Fiala __FUNCTION__, 2689af245d11STodd Fiala GetUnixSignals ().GetSignalAsCString (signo), 2690af245d11STodd Fiala signo, 2691af245d11STodd Fiala (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"), 2692af245d11STodd Fiala info->si_pid, 2693511e5cdcSTodd Fiala is_from_llgs ? "from llgs" : "not from llgs", 2694af245d11STodd Fiala pid); 269558a2f669STodd Fiala } 2696af245d11STodd Fiala 269758a2f669STodd Fiala // Check for new thread notification. 269858a2f669STodd Fiala if ((info->si_pid == 0) && (info->si_code == SI_USER)) 2699af245d11STodd Fiala { 2700af245d11STodd Fiala // A new thread creation is being signaled. This is one of two parts that come in 2701426bdf88SPavel Labath // a non-deterministic order. This code handles the case where the new thread event comes 2702426bdf88SPavel Labath // before the event on the parent thread. For the opposite case see code in 2703426bdf88SPavel Labath // MonitorSIGTRAP. 2704af245d11STodd Fiala if (log) 2705af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 " tid %" PRIu64 ": new thread notification", 2706af245d11STodd Fiala __FUNCTION__, GetID (), pid); 2707af245d11STodd Fiala 27085fd24c67SPavel Labath thread_sp = AddThread(pid); 27095fd24c67SPavel Labath assert (thread_sp.get() && "failed to create the tracking data for newly created inferior thread"); 27105fd24c67SPavel Labath // We can now resume the newly created thread. 2711cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); 27125fd24c67SPavel Labath Resume (pid, LLDB_INVALID_SIGNAL_NUMBER); 2713c076559aSPavel Labath NotifyThreadCreate (pid, false, CoordinatorErrorHandler); 271458a2f669STodd Fiala // Done handling. 271558a2f669STodd Fiala return; 2716af245d11STodd Fiala } 271758a2f669STodd Fiala 271858a2f669STodd Fiala // Check for thread stop notification. 2719511e5cdcSTodd Fiala if (is_from_llgs && (info->si_code == SI_TKILL) && (signo == SIGSTOP)) 2720af245d11STodd Fiala { 2721af245d11STodd Fiala // This is a tgkill()-based stop. 2722af245d11STodd Fiala if (thread_sp) 2723af245d11STodd Fiala { 2724fa03ad2eSChaoren Lin if (log) 2725fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 ", thread stopped", 2726fa03ad2eSChaoren Lin __FUNCTION__, 2727fa03ad2eSChaoren Lin GetID (), 2728fa03ad2eSChaoren Lin pid); 2729fa03ad2eSChaoren Lin 2730aab58633SChaoren Lin // Check that we're not already marked with a stop reason. 2731aab58633SChaoren Lin // Note this thread really shouldn't already be marked as stopped - if we were, that would imply that 2732aab58633SChaoren Lin // the kernel signaled us with the thread stopping which we handled and marked as stopped, 2733aab58633SChaoren Lin // and that, without an intervening resume, we received another stop. It is more likely 2734aab58633SChaoren Lin // that we are missing the marking of a run state somewhere if we find that the thread was 2735aab58633SChaoren Lin // marked as stopped. 2736cb84eebbSTamas Berghammer std::shared_ptr<NativeThreadLinux> linux_thread_sp = std::static_pointer_cast<NativeThreadLinux> (thread_sp); 2737cb84eebbSTamas Berghammer assert (linux_thread_sp && "linux_thread_sp is null!"); 2738aab58633SChaoren Lin 2739cb84eebbSTamas Berghammer const StateType thread_state = linux_thread_sp->GetState (); 2740aab58633SChaoren Lin if (!StateIsStoppedState (thread_state, false)) 2741aab58633SChaoren Lin { 2742ed89c7feSPavel Labath // An inferior thread has stopped because of a SIGSTOP we have sent it. 2743ed89c7feSPavel Labath // Generally, these are not important stops and we don't want to report them as 2744ed89c7feSPavel Labath // they are just used to stop other threads when one thread (the one with the 2745ed89c7feSPavel Labath // *real* stop reason) hits a breakpoint (watchpoint, etc...). However, in the 2746ed89c7feSPavel Labath // case of an asynchronous Interrupt(), this *is* the real stop reason, so we 2747ed89c7feSPavel Labath // leave the signal intact if this is the thread that was chosen as the 2748ed89c7feSPavel Labath // triggering thread. 2749ed89c7feSPavel Labath if (m_pending_notification_up && m_pending_notification_up->triggering_tid == pid) 2750ed89c7feSPavel Labath linux_thread_sp->SetStoppedBySignal(SIGSTOP); 2751ed89c7feSPavel Labath else 2752cb84eebbSTamas Berghammer linux_thread_sp->SetStoppedBySignal(0); 2753ed89c7feSPavel Labath 2754af245d11STodd Fiala SetCurrentThreadID (thread_sp->GetID ()); 2755c076559aSPavel Labath NotifyThreadStop (thread_sp->GetID (), true, CoordinatorErrorHandler); 2756aab58633SChaoren Lin } 2757aab58633SChaoren Lin else 2758aab58633SChaoren Lin { 2759aab58633SChaoren Lin if (log) 2760aab58633SChaoren Lin { 2761aab58633SChaoren Lin // Retrieve the signal name if the thread was stopped by a signal. 2762aab58633SChaoren Lin int stop_signo = 0; 2763cb84eebbSTamas Berghammer const bool stopped_by_signal = linux_thread_sp->IsStopped (&stop_signo); 2764aab58633SChaoren Lin const char *signal_name = stopped_by_signal ? GetUnixSignals ().GetSignalAsCString (stop_signo) : "<not stopped by signal>"; 2765aab58633SChaoren Lin if (!signal_name) 2766aab58633SChaoren Lin signal_name = "<no-signal-name>"; 2767aab58633SChaoren Lin 2768aab58633SChaoren Lin log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 ", thread was already marked as a stopped state (state=%s, signal=%d (%s)), leaving stop signal as is", 2769aab58633SChaoren Lin __FUNCTION__, 2770aab58633SChaoren Lin GetID (), 2771cb84eebbSTamas Berghammer linux_thread_sp->GetID (), 2772aab58633SChaoren Lin StateAsCString (thread_state), 2773aab58633SChaoren Lin stop_signo, 2774aab58633SChaoren Lin signal_name); 2775aab58633SChaoren Lin } 2776fa03ad2eSChaoren Lin // Tell the thread state coordinator about the stop. 2777fa03ad2eSChaoren Lin NotifyThreadStop (thread_sp->GetID ()); 2778af245d11STodd Fiala } 277986fd8e45SChaoren Lin } 2780af245d11STodd Fiala 278158a2f669STodd Fiala // Done handling. 2782af245d11STodd Fiala return; 2783af245d11STodd Fiala } 2784af245d11STodd Fiala 2785af245d11STodd Fiala if (log) 2786af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received signal %s", __FUNCTION__, GetUnixSignals ().GetSignalAsCString (signo)); 2787af245d11STodd Fiala 278886fd8e45SChaoren Lin // This thread is stopped. 278986fd8e45SChaoren Lin NotifyThreadStop (pid); 279086fd8e45SChaoren Lin 2791af245d11STodd Fiala switch (signo) 2792af245d11STodd Fiala { 2793511e5cdcSTodd Fiala case SIGSTOP: 2794511e5cdcSTodd Fiala { 279558a2f669STodd Fiala if (log) 2796511e5cdcSTodd Fiala { 2797511e5cdcSTodd Fiala if (is_from_llgs) 2798511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " received SIGSTOP from llgs, most likely an interrupt", __FUNCTION__, GetID (), pid); 2799511e5cdcSTodd Fiala else 2800511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " received SIGSTOP from outside of debugger", __FUNCTION__, GetID (), pid); 2801511e5cdcSTodd Fiala } 2802511e5cdcSTodd Fiala 2803fa03ad2eSChaoren Lin // Resume this thread to get the group-stop mechanism to fire off the true group stops. 2804fa03ad2eSChaoren Lin // This thread will get stopped again as part of the group-stop completion. 2805c076559aSPavel Labath RequestThreadResume (pid, 280686fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 2807fa03ad2eSChaoren Lin { 2808cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); 2809fa03ad2eSChaoren Lin // Pass this signal number on to the inferior to handle. 281037c768caSChaoren Lin return Resume (tid_to_resume, (supress_signal) ? LLDB_INVALID_SIGNAL_NUMBER : signo); 2811fa03ad2eSChaoren Lin }, 2812fa03ad2eSChaoren Lin CoordinatorErrorHandler); 281386fd8e45SChaoren Lin } 281486fd8e45SChaoren Lin break; 281586fd8e45SChaoren Lin case SIGSEGV: 281686fd8e45SChaoren Lin case SIGILL: 281786fd8e45SChaoren Lin case SIGFPE: 281886fd8e45SChaoren Lin case SIGBUS: 281986fd8e45SChaoren Lin if (thread_sp) 2820cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetCrashedWithException (*info); 282186fd8e45SChaoren Lin break; 282286fd8e45SChaoren Lin default: 282386fd8e45SChaoren Lin // This is just a pre-signal-delivery notification of the incoming signal. 282486fd8e45SChaoren Lin if (thread_sp) 2825cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (signo); 2826fa03ad2eSChaoren Lin 282786fd8e45SChaoren Lin break; 282886fd8e45SChaoren Lin } 282986fd8e45SChaoren Lin 283086fd8e45SChaoren Lin // Send a stop to the debugger after we get all other threads to stop. 2831ed89c7feSPavel Labath StopRunningThreads (pid); 2832511e5cdcSTodd Fiala } 2833af245d11STodd Fiala 2834e7708688STamas Berghammer namespace { 2835e7708688STamas Berghammer 2836e7708688STamas Berghammer struct EmulatorBaton 2837e7708688STamas Berghammer { 2838e7708688STamas Berghammer NativeProcessLinux* m_process; 2839e7708688STamas Berghammer NativeRegisterContext* m_reg_context; 28406648fcc3SPavel Labath 28416648fcc3SPavel Labath // eRegisterKindDWARF -> RegsiterValue 28426648fcc3SPavel Labath std::unordered_map<uint32_t, RegisterValue> m_register_values; 2843e7708688STamas Berghammer 2844e7708688STamas Berghammer EmulatorBaton(NativeProcessLinux* process, NativeRegisterContext* reg_context) : 2845e7708688STamas Berghammer m_process(process), m_reg_context(reg_context) {} 2846e7708688STamas Berghammer }; 2847e7708688STamas Berghammer 2848e7708688STamas Berghammer } // anonymous namespace 2849e7708688STamas Berghammer 2850e7708688STamas Berghammer static size_t 2851e7708688STamas Berghammer ReadMemoryCallback (EmulateInstruction *instruction, 2852e7708688STamas Berghammer void *baton, 2853e7708688STamas Berghammer const EmulateInstruction::Context &context, 2854e7708688STamas Berghammer lldb::addr_t addr, 2855e7708688STamas Berghammer void *dst, 2856e7708688STamas Berghammer size_t length) 2857e7708688STamas Berghammer { 2858e7708688STamas Berghammer EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); 2859e7708688STamas Berghammer 28603eb4b458SChaoren Lin size_t bytes_read; 2861e7708688STamas Berghammer emulator_baton->m_process->ReadMemory(addr, dst, length, bytes_read); 2862e7708688STamas Berghammer return bytes_read; 2863e7708688STamas Berghammer } 2864e7708688STamas Berghammer 2865e7708688STamas Berghammer static bool 2866e7708688STamas Berghammer ReadRegisterCallback (EmulateInstruction *instruction, 2867e7708688STamas Berghammer void *baton, 2868e7708688STamas Berghammer const RegisterInfo *reg_info, 2869e7708688STamas Berghammer RegisterValue ®_value) 2870e7708688STamas Berghammer { 2871e7708688STamas Berghammer EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); 2872e7708688STamas Berghammer 28736648fcc3SPavel Labath auto it = emulator_baton->m_register_values.find(reg_info->kinds[eRegisterKindDWARF]); 28746648fcc3SPavel Labath if (it != emulator_baton->m_register_values.end()) 28756648fcc3SPavel Labath { 28766648fcc3SPavel Labath reg_value = it->second; 28776648fcc3SPavel Labath return true; 28786648fcc3SPavel Labath } 28796648fcc3SPavel Labath 2880e7708688STamas Berghammer // The emulator only fill in the dwarf regsiter numbers (and in some case 2881e7708688STamas Berghammer // the generic register numbers). Get the full register info from the 2882e7708688STamas Berghammer // register context based on the dwarf register numbers. 2883e7708688STamas Berghammer const RegisterInfo* full_reg_info = emulator_baton->m_reg_context->GetRegisterInfo( 2884e7708688STamas Berghammer eRegisterKindDWARF, reg_info->kinds[eRegisterKindDWARF]); 2885e7708688STamas Berghammer 2886e7708688STamas Berghammer Error error = emulator_baton->m_reg_context->ReadRegister(full_reg_info, reg_value); 28876648fcc3SPavel Labath if (error.Success()) 28886648fcc3SPavel Labath return true; 2889*cdc22a88SMohit K. Bhakkad 28906648fcc3SPavel Labath return false; 2891e7708688STamas Berghammer } 2892e7708688STamas Berghammer 2893e7708688STamas Berghammer static bool 2894e7708688STamas Berghammer WriteRegisterCallback (EmulateInstruction *instruction, 2895e7708688STamas Berghammer void *baton, 2896e7708688STamas Berghammer const EmulateInstruction::Context &context, 2897e7708688STamas Berghammer const RegisterInfo *reg_info, 2898e7708688STamas Berghammer const RegisterValue ®_value) 2899e7708688STamas Berghammer { 2900e7708688STamas Berghammer EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); 29016648fcc3SPavel Labath emulator_baton->m_register_values[reg_info->kinds[eRegisterKindDWARF]] = reg_value; 2902e7708688STamas Berghammer return true; 2903e7708688STamas Berghammer } 2904e7708688STamas Berghammer 2905e7708688STamas Berghammer static size_t 2906e7708688STamas Berghammer WriteMemoryCallback (EmulateInstruction *instruction, 2907e7708688STamas Berghammer void *baton, 2908e7708688STamas Berghammer const EmulateInstruction::Context &context, 2909e7708688STamas Berghammer lldb::addr_t addr, 2910e7708688STamas Berghammer const void *dst, 2911e7708688STamas Berghammer size_t length) 2912e7708688STamas Berghammer { 2913e7708688STamas Berghammer return length; 2914e7708688STamas Berghammer } 2915e7708688STamas Berghammer 2916e7708688STamas Berghammer static lldb::addr_t 2917e7708688STamas Berghammer ReadFlags (NativeRegisterContext* regsiter_context) 2918e7708688STamas Berghammer { 2919e7708688STamas Berghammer const RegisterInfo* flags_info = regsiter_context->GetRegisterInfo( 2920e7708688STamas Berghammer eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 2921e7708688STamas Berghammer return regsiter_context->ReadRegisterAsUnsigned(flags_info, LLDB_INVALID_ADDRESS); 2922e7708688STamas Berghammer } 2923e7708688STamas Berghammer 2924e7708688STamas Berghammer Error 2925e7708688STamas Berghammer NativeProcessLinux::SetupSoftwareSingleStepping(NativeThreadProtocolSP thread_sp) 2926e7708688STamas Berghammer { 2927e7708688STamas Berghammer Error error; 2928e7708688STamas Berghammer NativeRegisterContextSP register_context_sp = thread_sp->GetRegisterContext(); 2929e7708688STamas Berghammer 2930e7708688STamas Berghammer std::unique_ptr<EmulateInstruction> emulator_ap( 2931e7708688STamas Berghammer EmulateInstruction::FindPlugin(m_arch, eInstructionTypePCModifying, nullptr)); 2932e7708688STamas Berghammer 2933e7708688STamas Berghammer if (emulator_ap == nullptr) 2934e7708688STamas Berghammer return Error("Instruction emulator not found!"); 2935e7708688STamas Berghammer 2936e7708688STamas Berghammer EmulatorBaton baton(this, register_context_sp.get()); 2937e7708688STamas Berghammer emulator_ap->SetBaton(&baton); 2938e7708688STamas Berghammer emulator_ap->SetReadMemCallback(&ReadMemoryCallback); 2939e7708688STamas Berghammer emulator_ap->SetReadRegCallback(&ReadRegisterCallback); 2940e7708688STamas Berghammer emulator_ap->SetWriteMemCallback(&WriteMemoryCallback); 2941e7708688STamas Berghammer emulator_ap->SetWriteRegCallback(&WriteRegisterCallback); 2942e7708688STamas Berghammer 2943e7708688STamas Berghammer if (!emulator_ap->ReadInstruction()) 2944e7708688STamas Berghammer return Error("Read instruction failed!"); 2945e7708688STamas Berghammer 29466648fcc3SPavel Labath bool emulation_result = emulator_ap->EvaluateInstruction(eEmulateInstructionOptionAutoAdvancePC); 29476648fcc3SPavel Labath 29486648fcc3SPavel Labath const RegisterInfo* reg_info_pc = register_context_sp->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); 29496648fcc3SPavel Labath const RegisterInfo* reg_info_flags = register_context_sp->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 29506648fcc3SPavel Labath 29516648fcc3SPavel Labath auto pc_it = baton.m_register_values.find(reg_info_pc->kinds[eRegisterKindDWARF]); 29526648fcc3SPavel Labath auto flags_it = baton.m_register_values.find(reg_info_flags->kinds[eRegisterKindDWARF]); 29536648fcc3SPavel Labath 2954e7708688STamas Berghammer lldb::addr_t next_pc; 2955e7708688STamas Berghammer lldb::addr_t next_flags; 29566648fcc3SPavel Labath if (emulation_result) 2957e7708688STamas Berghammer { 29586648fcc3SPavel Labath assert(pc_it != baton.m_register_values.end() && "Emulation was successfull but PC wasn't updated"); 29596648fcc3SPavel Labath next_pc = pc_it->second.GetAsUInt64(); 29606648fcc3SPavel Labath 29616648fcc3SPavel Labath if (flags_it != baton.m_register_values.end()) 29626648fcc3SPavel Labath next_flags = flags_it->second.GetAsUInt64(); 2963e7708688STamas Berghammer else 2964e7708688STamas Berghammer next_flags = ReadFlags (register_context_sp.get()); 2965e7708688STamas Berghammer } 29666648fcc3SPavel Labath else if (pc_it == baton.m_register_values.end()) 2967e7708688STamas Berghammer { 2968e7708688STamas Berghammer // Emulate instruction failed and it haven't changed PC. Advance PC 2969e7708688STamas Berghammer // with the size of the current opcode because the emulation of all 2970e7708688STamas Berghammer // PC modifying instruction should be successful. The failure most 2971e7708688STamas Berghammer // likely caused by a not supported instruction which don't modify PC. 2972e7708688STamas Berghammer next_pc = register_context_sp->GetPC() + emulator_ap->GetOpcode().GetByteSize(); 2973e7708688STamas Berghammer next_flags = ReadFlags (register_context_sp.get()); 2974e7708688STamas Berghammer } 2975e7708688STamas Berghammer else 2976e7708688STamas Berghammer { 2977e7708688STamas Berghammer // The instruction emulation failed after it modified the PC. It is an 2978e7708688STamas Berghammer // unknown error where we can't continue because the next instruction is 2979e7708688STamas Berghammer // modifying the PC but we don't know how. 2980e7708688STamas Berghammer return Error ("Instruction emulation failed unexpectedly."); 2981e7708688STamas Berghammer } 2982e7708688STamas Berghammer 2983e7708688STamas Berghammer if (m_arch.GetMachine() == llvm::Triple::arm) 2984e7708688STamas Berghammer { 2985e7708688STamas Berghammer if (next_flags & 0x20) 2986e7708688STamas Berghammer { 2987e7708688STamas Berghammer // Thumb mode 2988e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 2); 2989e7708688STamas Berghammer } 2990e7708688STamas Berghammer else 2991e7708688STamas Berghammer { 2992e7708688STamas Berghammer // Arm mode 2993e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 4); 2994e7708688STamas Berghammer } 2995e7708688STamas Berghammer } 2996*cdc22a88SMohit K. Bhakkad else if (m_arch.GetMachine() == llvm::Triple::mips64 2997*cdc22a88SMohit K. Bhakkad || m_arch.GetMachine() == llvm::Triple::mips64el) 2998*cdc22a88SMohit K. Bhakkad error = SetSoftwareBreakpoint(next_pc, 4); 2999e7708688STamas Berghammer else 3000e7708688STamas Berghammer { 3001e7708688STamas Berghammer // No size hint is given for the next breakpoint 3002e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 0); 3003e7708688STamas Berghammer } 3004e7708688STamas Berghammer 3005e7708688STamas Berghammer if (error.Fail()) 3006e7708688STamas Berghammer return error; 3007e7708688STamas Berghammer 3008e7708688STamas Berghammer m_threads_stepping_with_breakpoint.insert({thread_sp->GetID(), next_pc}); 3009e7708688STamas Berghammer 3010e7708688STamas Berghammer return Error(); 3011e7708688STamas Berghammer } 3012e7708688STamas Berghammer 3013e7708688STamas Berghammer bool 3014e7708688STamas Berghammer NativeProcessLinux::SupportHardwareSingleStepping() const 3015e7708688STamas Berghammer { 3016*cdc22a88SMohit K. Bhakkad if (m_arch.GetMachine() == llvm::Triple::arm 3017*cdc22a88SMohit K. Bhakkad || m_arch.GetMachine() == llvm::Triple::mips64 || m_arch.GetMachine() == llvm::Triple::mips64el) 3018*cdc22a88SMohit K. Bhakkad return false; 3019*cdc22a88SMohit K. Bhakkad return true; 3020e7708688STamas Berghammer } 3021e7708688STamas Berghammer 3022af245d11STodd Fiala Error 3023af245d11STodd Fiala NativeProcessLinux::Resume (const ResumeActionList &resume_actions) 3024af245d11STodd Fiala { 3025af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); 3026af245d11STodd Fiala if (log) 3027af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s called: pid %" PRIu64, __FUNCTION__, GetID ()); 3028af245d11STodd Fiala 302903f12d6bSChaoren Lin lldb::tid_t deferred_signal_tid = LLDB_INVALID_THREAD_ID; 303003f12d6bSChaoren Lin lldb::tid_t deferred_signal_skip_tid = LLDB_INVALID_THREAD_ID; 3031ae29d395SChaoren Lin int deferred_signo = 0; 3032ae29d395SChaoren Lin NativeThreadProtocolSP deferred_signal_thread_sp; 303386fd8e45SChaoren Lin bool stepping = false; 3034e7708688STamas Berghammer bool software_single_step = !SupportHardwareSingleStepping(); 3035af245d11STodd Fiala 303645f5cb31SPavel Labath Monitor::ScopedOperationLock monitor_lock(*m_monitor_up); 3037af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 30385830aa75STamas Berghammer 3039e7708688STamas Berghammer if (software_single_step) 3040e7708688STamas Berghammer { 3041e7708688STamas Berghammer for (auto thread_sp : m_threads) 3042e7708688STamas Berghammer { 3043e7708688STamas Berghammer assert (thread_sp && "thread list should not contain NULL threads"); 3044e7708688STamas Berghammer 3045e7708688STamas Berghammer const ResumeAction *const action = resume_actions.GetActionForThread (thread_sp->GetID (), true); 3046e7708688STamas Berghammer if (action == nullptr) 3047e7708688STamas Berghammer continue; 3048e7708688STamas Berghammer 3049e7708688STamas Berghammer if (action->state == eStateStepping) 3050e7708688STamas Berghammer { 3051e7708688STamas Berghammer Error error = SetupSoftwareSingleStepping(thread_sp); 3052e7708688STamas Berghammer if (error.Fail()) 3053e7708688STamas Berghammer return error; 3054e7708688STamas Berghammer } 3055e7708688STamas Berghammer } 3056e7708688STamas Berghammer } 3057e7708688STamas Berghammer 3058af245d11STodd Fiala for (auto thread_sp : m_threads) 3059af245d11STodd Fiala { 3060af245d11STodd Fiala assert (thread_sp && "thread list should not contain NULL threads"); 3061af245d11STodd Fiala 3062af245d11STodd Fiala const ResumeAction *const action = resume_actions.GetActionForThread (thread_sp->GetID (), true); 30636a196ce6SChaoren Lin 30646a196ce6SChaoren Lin if (action == nullptr) 30656a196ce6SChaoren Lin { 30666a196ce6SChaoren Lin if (log) 30676a196ce6SChaoren Lin log->Printf ("NativeProcessLinux::%s no action specified for pid %" PRIu64 " tid %" PRIu64, 30686a196ce6SChaoren Lin __FUNCTION__, GetID (), thread_sp->GetID ()); 30696a196ce6SChaoren Lin continue; 30706a196ce6SChaoren Lin } 3071af245d11STodd Fiala 3072af245d11STodd Fiala if (log) 3073af245d11STodd Fiala { 3074af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s processing resume action state %s for pid %" PRIu64 " tid %" PRIu64, 3075af245d11STodd Fiala __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); 3076af245d11STodd Fiala } 3077af245d11STodd Fiala 3078af245d11STodd Fiala switch (action->state) 3079af245d11STodd Fiala { 3080af245d11STodd Fiala case eStateRunning: 3081fa03ad2eSChaoren Lin { 3082af245d11STodd Fiala // Run the thread, possibly feeding it the signal. 3083fa03ad2eSChaoren Lin const int signo = action->signal; 3084c076559aSPavel Labath RequestThreadResumeAsNeeded (thread_sp->GetID (), 308586fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 3086af245d11STodd Fiala { 3087cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); 3088fa03ad2eSChaoren Lin // Pass this signal number on to the inferior to handle. 30895830aa75STamas Berghammer const auto resume_result = Resume (tid_to_resume, (signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER); 30905830aa75STamas Berghammer if (resume_result.Success()) 30915830aa75STamas Berghammer SetState(eStateRunning, true); 30925830aa75STamas Berghammer return resume_result; 3093fa03ad2eSChaoren Lin }, 3094fa03ad2eSChaoren Lin CoordinatorErrorHandler); 3095af245d11STodd Fiala break; 3096fa03ad2eSChaoren Lin } 3097af245d11STodd Fiala 3098af245d11STodd Fiala case eStateStepping: 3099af245d11STodd Fiala { 3100ae29d395SChaoren Lin // Request the step. 3101ae29d395SChaoren Lin const int signo = action->signal; 3102c076559aSPavel Labath RequestThreadResume (thread_sp->GetID (), 310386fd8e45SChaoren Lin [=](lldb::tid_t tid_to_step, bool supress_signal) 3104af245d11STodd Fiala { 3105cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStepping (); 3106e7708688STamas Berghammer 3107e7708688STamas Berghammer Error step_result; 3108e7708688STamas Berghammer if (software_single_step) 3109e7708688STamas Berghammer step_result = Resume (tid_to_step, (signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER); 3110e7708688STamas Berghammer else 3111e7708688STamas Berghammer step_result = SingleStep (tid_to_step,(signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER); 3112e7708688STamas Berghammer 311337c768caSChaoren Lin assert (step_result.Success() && "SingleStep() failed"); 31145830aa75STamas Berghammer if (step_result.Success()) 31155830aa75STamas Berghammer SetState(eStateStepping, true); 311637c768caSChaoren Lin return step_result; 3117ae29d395SChaoren Lin }, 3118ae29d395SChaoren Lin CoordinatorErrorHandler); 311986fd8e45SChaoren Lin stepping = true; 3120af245d11STodd Fiala break; 3121ae29d395SChaoren Lin } 3122af245d11STodd Fiala 3123af245d11STodd Fiala case eStateSuspended: 3124af245d11STodd Fiala case eStateStopped: 3125ae29d395SChaoren Lin // if we haven't chosen a deferred signal tid yet, use this one. 3126ae29d395SChaoren Lin if (deferred_signal_tid == LLDB_INVALID_THREAD_ID) 3127ae29d395SChaoren Lin { 3128ae29d395SChaoren Lin deferred_signal_tid = thread_sp->GetID (); 3129ae29d395SChaoren Lin deferred_signal_thread_sp = thread_sp; 3130ae29d395SChaoren Lin deferred_signo = SIGSTOP; 3131ae29d395SChaoren Lin } 3132af245d11STodd Fiala break; 3133af245d11STodd Fiala 3134af245d11STodd Fiala default: 3135af245d11STodd Fiala return Error ("NativeProcessLinux::%s (): unexpected state %s specified for pid %" PRIu64 ", tid %" PRIu64, 3136af245d11STodd Fiala __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); 3137af245d11STodd Fiala } 3138af245d11STodd Fiala } 3139af245d11STodd Fiala 3140fa03ad2eSChaoren Lin // If we had any thread stopping, then do a deferred notification of the chosen stop thread id and signal 3141fa03ad2eSChaoren Lin // after all other running threads have stopped. 314286fd8e45SChaoren Lin // If there is a stepping thread involved we'll be eventually stopped by SIGTRAP trace signal. 314386fd8e45SChaoren Lin if (deferred_signal_tid != LLDB_INVALID_THREAD_ID && !stepping) 3144ed89c7feSPavel Labath StopRunningThreadsWithSkipTID(deferred_signal_tid, deferred_signal_skip_tid); 3145af245d11STodd Fiala 31465830aa75STamas Berghammer return Error(); 3147af245d11STodd Fiala } 3148af245d11STodd Fiala 3149af245d11STodd Fiala Error 3150af245d11STodd Fiala NativeProcessLinux::Halt () 3151af245d11STodd Fiala { 3152af245d11STodd Fiala Error error; 3153af245d11STodd Fiala 3154af245d11STodd Fiala if (kill (GetID (), SIGSTOP) != 0) 3155af245d11STodd Fiala error.SetErrorToErrno (); 3156af245d11STodd Fiala 3157af245d11STodd Fiala return error; 3158af245d11STodd Fiala } 3159af245d11STodd Fiala 3160af245d11STodd Fiala Error 3161af245d11STodd Fiala NativeProcessLinux::Detach () 3162af245d11STodd Fiala { 3163af245d11STodd Fiala Error error; 3164af245d11STodd Fiala 3165af245d11STodd Fiala // Tell ptrace to detach from the process. 3166af245d11STodd Fiala if (GetID () != LLDB_INVALID_PROCESS_ID) 3167af245d11STodd Fiala error = Detach (GetID ()); 3168af245d11STodd Fiala 3169af245d11STodd Fiala // Stop monitoring the inferior. 317045f5cb31SPavel Labath m_monitor_up->Terminate(); 3171af245d11STodd Fiala 3172af245d11STodd Fiala // No error. 3173af245d11STodd Fiala return error; 3174af245d11STodd Fiala } 3175af245d11STodd Fiala 3176af245d11STodd Fiala Error 3177af245d11STodd Fiala NativeProcessLinux::Signal (int signo) 3178af245d11STodd Fiala { 3179af245d11STodd Fiala Error error; 3180af245d11STodd Fiala 3181af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3182af245d11STodd Fiala if (log) 3183af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s: sending signal %d (%s) to pid %" PRIu64, 3184af245d11STodd Fiala __FUNCTION__, signo, GetUnixSignals ().GetSignalAsCString (signo), GetID ()); 3185af245d11STodd Fiala 3186af245d11STodd Fiala if (kill(GetID(), signo)) 3187af245d11STodd Fiala error.SetErrorToErrno(); 3188af245d11STodd Fiala 3189af245d11STodd Fiala return error; 3190af245d11STodd Fiala } 3191af245d11STodd Fiala 3192af245d11STodd Fiala Error 3193e9547b80SChaoren Lin NativeProcessLinux::Interrupt () 3194e9547b80SChaoren Lin { 3195e9547b80SChaoren Lin // Pick a running thread (or if none, a not-dead stopped thread) as 3196e9547b80SChaoren Lin // the chosen thread that will be the stop-reason thread. 3197e9547b80SChaoren Lin Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3198e9547b80SChaoren Lin 3199e9547b80SChaoren Lin NativeThreadProtocolSP running_thread_sp; 3200e9547b80SChaoren Lin NativeThreadProtocolSP stopped_thread_sp; 3201e9547b80SChaoren Lin 3202e9547b80SChaoren Lin if (log) 3203e9547b80SChaoren Lin log->Printf ("NativeProcessLinux::%s selecting running thread for interrupt target", __FUNCTION__); 3204e9547b80SChaoren Lin 320545f5cb31SPavel Labath Monitor::ScopedOperationLock monitor_lock(*m_monitor_up); 32065830aa75STamas Berghammer Mutex::Locker locker (m_threads_mutex); 32075830aa75STamas Berghammer 3208e9547b80SChaoren Lin for (auto thread_sp : m_threads) 3209e9547b80SChaoren Lin { 3210e9547b80SChaoren Lin // The thread shouldn't be null but lets just cover that here. 3211e9547b80SChaoren Lin if (!thread_sp) 3212e9547b80SChaoren Lin continue; 3213e9547b80SChaoren Lin 3214e9547b80SChaoren Lin // If we have a running or stepping thread, we'll call that the 3215e9547b80SChaoren Lin // target of the interrupt. 3216e9547b80SChaoren Lin const auto thread_state = thread_sp->GetState (); 3217e9547b80SChaoren Lin if (thread_state == eStateRunning || 3218e9547b80SChaoren Lin thread_state == eStateStepping) 3219e9547b80SChaoren Lin { 3220e9547b80SChaoren Lin running_thread_sp = thread_sp; 3221e9547b80SChaoren Lin break; 3222e9547b80SChaoren Lin } 3223e9547b80SChaoren Lin else if (!stopped_thread_sp && StateIsStoppedState (thread_state, true)) 3224e9547b80SChaoren Lin { 3225e9547b80SChaoren Lin // Remember the first non-dead stopped thread. We'll use that as a backup if there are no running threads. 3226e9547b80SChaoren Lin stopped_thread_sp = thread_sp; 3227e9547b80SChaoren Lin } 3228e9547b80SChaoren Lin } 3229e9547b80SChaoren Lin 3230e9547b80SChaoren Lin if (!running_thread_sp && !stopped_thread_sp) 3231e9547b80SChaoren Lin { 32325830aa75STamas Berghammer Error error("found no running/stepping or live stopped threads as target for interrupt"); 3233e9547b80SChaoren Lin if (log) 3234e9547b80SChaoren Lin log->Printf ("NativeProcessLinux::%s skipping due to error: %s", __FUNCTION__, error.AsCString ()); 32355830aa75STamas Berghammer 3236e9547b80SChaoren Lin return error; 3237e9547b80SChaoren Lin } 3238e9547b80SChaoren Lin 3239e9547b80SChaoren Lin NativeThreadProtocolSP deferred_signal_thread_sp = running_thread_sp ? running_thread_sp : stopped_thread_sp; 3240e9547b80SChaoren Lin 3241e9547b80SChaoren Lin if (log) 3242e9547b80SChaoren Lin log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " %s tid %" PRIu64 " chosen for interrupt target", 3243e9547b80SChaoren Lin __FUNCTION__, 3244e9547b80SChaoren Lin GetID (), 3245e9547b80SChaoren Lin running_thread_sp ? "running" : "stopped", 3246e9547b80SChaoren Lin deferred_signal_thread_sp->GetID ()); 3247e9547b80SChaoren Lin 3248ed89c7feSPavel Labath StopRunningThreads(deferred_signal_thread_sp->GetID()); 324945f5cb31SPavel Labath 32505830aa75STamas Berghammer return Error(); 3251e9547b80SChaoren Lin } 3252e9547b80SChaoren Lin 3253e9547b80SChaoren Lin Error 3254af245d11STodd Fiala NativeProcessLinux::Kill () 3255af245d11STodd Fiala { 3256af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3257af245d11STodd Fiala if (log) 3258af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s called for PID %" PRIu64, __FUNCTION__, GetID ()); 3259af245d11STodd Fiala 3260af245d11STodd Fiala Error error; 3261af245d11STodd Fiala 3262af245d11STodd Fiala switch (m_state) 3263af245d11STodd Fiala { 3264af245d11STodd Fiala case StateType::eStateInvalid: 3265af245d11STodd Fiala case StateType::eStateExited: 3266af245d11STodd Fiala case StateType::eStateCrashed: 3267af245d11STodd Fiala case StateType::eStateDetached: 3268af245d11STodd Fiala case StateType::eStateUnloaded: 3269af245d11STodd Fiala // Nothing to do - the process is already dead. 3270af245d11STodd Fiala if (log) 3271af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s ignored for PID %" PRIu64 " due to current state: %s", __FUNCTION__, GetID (), StateAsCString (m_state)); 3272af245d11STodd Fiala return error; 3273af245d11STodd Fiala 3274af245d11STodd Fiala case StateType::eStateConnected: 3275af245d11STodd Fiala case StateType::eStateAttaching: 3276af245d11STodd Fiala case StateType::eStateLaunching: 3277af245d11STodd Fiala case StateType::eStateStopped: 3278af245d11STodd Fiala case StateType::eStateRunning: 3279af245d11STodd Fiala case StateType::eStateStepping: 3280af245d11STodd Fiala case StateType::eStateSuspended: 3281af245d11STodd Fiala // We can try to kill a process in these states. 3282af245d11STodd Fiala break; 3283af245d11STodd Fiala } 3284af245d11STodd Fiala 3285af245d11STodd Fiala if (kill (GetID (), SIGKILL) != 0) 3286af245d11STodd Fiala { 3287af245d11STodd Fiala error.SetErrorToErrno (); 3288af245d11STodd Fiala return error; 3289af245d11STodd Fiala } 3290af245d11STodd Fiala 3291af245d11STodd Fiala return error; 3292af245d11STodd Fiala } 3293af245d11STodd Fiala 3294af245d11STodd Fiala static Error 3295af245d11STodd Fiala ParseMemoryRegionInfoFromProcMapsLine (const std::string &maps_line, MemoryRegionInfo &memory_region_info) 3296af245d11STodd Fiala { 3297af245d11STodd Fiala memory_region_info.Clear(); 3298af245d11STodd Fiala 3299af245d11STodd Fiala StringExtractor line_extractor (maps_line.c_str ()); 3300af245d11STodd Fiala 3301af245d11STodd Fiala // Format: {address_start_hex}-{address_end_hex} perms offset dev inode pathname 3302af245d11STodd Fiala // perms: rwxp (letter is present if set, '-' if not, final character is p=private, s=shared). 3303af245d11STodd Fiala 3304af245d11STodd Fiala // Parse out the starting address 3305af245d11STodd Fiala lldb::addr_t start_address = line_extractor.GetHexMaxU64 (false, 0); 3306af245d11STodd Fiala 3307af245d11STodd Fiala // Parse out hyphen separating start and end address from range. 3308af245d11STodd Fiala if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != '-')) 3309af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing dash between address range"); 3310af245d11STodd Fiala 3311af245d11STodd Fiala // Parse out the ending address 3312af245d11STodd Fiala lldb::addr_t end_address = line_extractor.GetHexMaxU64 (false, start_address); 3313af245d11STodd Fiala 3314af245d11STodd Fiala // Parse out the space after the address. 3315af245d11STodd Fiala if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != ' ')) 3316af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing space after range"); 3317af245d11STodd Fiala 3318af245d11STodd Fiala // Save the range. 3319af245d11STodd Fiala memory_region_info.GetRange ().SetRangeBase (start_address); 3320af245d11STodd Fiala memory_region_info.GetRange ().SetRangeEnd (end_address); 3321af245d11STodd Fiala 3322af245d11STodd Fiala // Parse out each permission entry. 3323af245d11STodd Fiala if (line_extractor.GetBytesLeft () < 4) 3324af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing some portion of permissions"); 3325af245d11STodd Fiala 3326af245d11STodd Fiala // Handle read permission. 3327af245d11STodd Fiala const char read_perm_char = line_extractor.GetChar (); 3328af245d11STodd Fiala if (read_perm_char == 'r') 3329af245d11STodd Fiala memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eYes); 3330af245d11STodd Fiala else 3331af245d11STodd Fiala { 3332af245d11STodd Fiala assert ( (read_perm_char == '-') && "unexpected /proc/{pid}/maps read permission char" ); 3333af245d11STodd Fiala memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); 3334af245d11STodd Fiala } 3335af245d11STodd Fiala 3336af245d11STodd Fiala // Handle write permission. 3337af245d11STodd Fiala const char write_perm_char = line_extractor.GetChar (); 3338af245d11STodd Fiala if (write_perm_char == 'w') 3339af245d11STodd Fiala memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eYes); 3340af245d11STodd Fiala else 3341af245d11STodd Fiala { 3342af245d11STodd Fiala assert ( (write_perm_char == '-') && "unexpected /proc/{pid}/maps write permission char" ); 3343af245d11STodd Fiala memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); 3344af245d11STodd Fiala } 3345af245d11STodd Fiala 3346af245d11STodd Fiala // Handle execute permission. 3347af245d11STodd Fiala const char exec_perm_char = line_extractor.GetChar (); 3348af245d11STodd Fiala if (exec_perm_char == 'x') 3349af245d11STodd Fiala memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eYes); 3350af245d11STodd Fiala else 3351af245d11STodd Fiala { 3352af245d11STodd Fiala assert ( (exec_perm_char == '-') && "unexpected /proc/{pid}/maps exec permission char" ); 3353af245d11STodd Fiala memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); 3354af245d11STodd Fiala } 3355af245d11STodd Fiala 3356af245d11STodd Fiala return Error (); 3357af245d11STodd Fiala } 3358af245d11STodd Fiala 3359af245d11STodd Fiala Error 3360af245d11STodd Fiala NativeProcessLinux::GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) 3361af245d11STodd Fiala { 3362af245d11STodd Fiala // FIXME review that the final memory region returned extends to the end of the virtual address space, 3363af245d11STodd Fiala // with no perms if it is not mapped. 3364af245d11STodd Fiala 3365af245d11STodd Fiala // Use an approach that reads memory regions from /proc/{pid}/maps. 3366af245d11STodd Fiala // Assume proc maps entries are in ascending order. 3367af245d11STodd Fiala // FIXME assert if we find differently. 3368af245d11STodd Fiala Mutex::Locker locker (m_mem_region_cache_mutex); 3369af245d11STodd Fiala 3370af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3371af245d11STodd Fiala Error error; 3372af245d11STodd Fiala 3373af245d11STodd Fiala if (m_supports_mem_region == LazyBool::eLazyBoolNo) 3374af245d11STodd Fiala { 3375af245d11STodd Fiala // We're done. 3376af245d11STodd Fiala error.SetErrorString ("unsupported"); 3377af245d11STodd Fiala return error; 3378af245d11STodd Fiala } 3379af245d11STodd Fiala 3380af245d11STodd Fiala // If our cache is empty, pull the latest. There should always be at least one memory region 3381af245d11STodd Fiala // if memory region handling is supported. 3382af245d11STodd Fiala if (m_mem_region_cache.empty ()) 3383af245d11STodd Fiala { 3384af245d11STodd Fiala error = ProcFileReader::ProcessLineByLine (GetID (), "maps", 3385af245d11STodd Fiala [&] (const std::string &line) -> bool 3386af245d11STodd Fiala { 3387af245d11STodd Fiala MemoryRegionInfo info; 3388af245d11STodd Fiala const Error parse_error = ParseMemoryRegionInfoFromProcMapsLine (line, info); 3389af245d11STodd Fiala if (parse_error.Success ()) 3390af245d11STodd Fiala { 3391af245d11STodd Fiala m_mem_region_cache.push_back (info); 3392af245d11STodd Fiala return true; 3393af245d11STodd Fiala } 3394af245d11STodd Fiala else 3395af245d11STodd Fiala { 3396af245d11STodd Fiala if (log) 3397af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to parse proc maps line '%s': %s", __FUNCTION__, line.c_str (), error.AsCString ()); 3398af245d11STodd Fiala return false; 3399af245d11STodd Fiala } 3400af245d11STodd Fiala }); 3401af245d11STodd Fiala 3402af245d11STodd Fiala // If we had an error, we'll mark unsupported. 3403af245d11STodd Fiala if (error.Fail ()) 3404af245d11STodd Fiala { 3405af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolNo; 3406af245d11STodd Fiala return error; 3407af245d11STodd Fiala } 3408af245d11STodd Fiala else if (m_mem_region_cache.empty ()) 3409af245d11STodd Fiala { 3410af245d11STodd Fiala // No entries after attempting to read them. This shouldn't happen if /proc/{pid}/maps 3411af245d11STodd Fiala // is supported. Assume we don't support map entries via procfs. 3412af245d11STodd Fiala if (log) 3413af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to find any procfs maps entries, assuming no support for memory region metadata retrieval", __FUNCTION__); 3414af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolNo; 3415af245d11STodd Fiala error.SetErrorString ("not supported"); 3416af245d11STodd Fiala return error; 3417af245d11STodd Fiala } 3418af245d11STodd Fiala 3419af245d11STodd Fiala if (log) 3420af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s read %" PRIu64 " memory region entries from /proc/%" PRIu64 "/maps", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ()), GetID ()); 3421af245d11STodd Fiala 3422af245d11STodd Fiala // We support memory retrieval, remember that. 3423af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolYes; 3424af245d11STodd Fiala } 3425af245d11STodd Fiala else 3426af245d11STodd Fiala { 3427af245d11STodd Fiala if (log) 3428af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s reusing %" PRIu64 " cached memory region entries", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); 3429af245d11STodd Fiala } 3430af245d11STodd Fiala 3431af245d11STodd Fiala lldb::addr_t prev_base_address = 0; 3432af245d11STodd Fiala 3433af245d11STodd Fiala // FIXME start by finding the last region that is <= target address using binary search. Data is sorted. 3434af245d11STodd Fiala // There can be a ton of regions on pthreads apps with lots of threads. 3435af245d11STodd Fiala for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end (); ++it) 3436af245d11STodd Fiala { 3437af245d11STodd Fiala MemoryRegionInfo &proc_entry_info = *it; 3438af245d11STodd Fiala 3439af245d11STodd Fiala // Sanity check assumption that /proc/{pid}/maps entries are ascending. 3440af245d11STodd Fiala assert ((proc_entry_info.GetRange ().GetRangeBase () >= prev_base_address) && "descending /proc/pid/maps entries detected, unexpected"); 3441af245d11STodd Fiala prev_base_address = proc_entry_info.GetRange ().GetRangeBase (); 3442af245d11STodd Fiala 3443af245d11STodd Fiala // If the target address comes before this entry, indicate distance to next region. 3444af245d11STodd Fiala if (load_addr < proc_entry_info.GetRange ().GetRangeBase ()) 3445af245d11STodd Fiala { 3446af245d11STodd Fiala range_info.GetRange ().SetRangeBase (load_addr); 3447af245d11STodd Fiala range_info.GetRange ().SetByteSize (proc_entry_info.GetRange ().GetRangeBase () - load_addr); 3448af245d11STodd Fiala range_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); 3449af245d11STodd Fiala range_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); 3450af245d11STodd Fiala range_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); 3451af245d11STodd Fiala 3452af245d11STodd Fiala return error; 3453af245d11STodd Fiala } 3454af245d11STodd Fiala else if (proc_entry_info.GetRange ().Contains (load_addr)) 3455af245d11STodd Fiala { 3456af245d11STodd Fiala // The target address is within the memory region we're processing here. 3457af245d11STodd Fiala range_info = proc_entry_info; 3458af245d11STodd Fiala return error; 3459af245d11STodd Fiala } 3460af245d11STodd Fiala 3461af245d11STodd Fiala // The target memory address comes somewhere after the region we just parsed. 3462af245d11STodd Fiala } 3463af245d11STodd Fiala 3464af245d11STodd Fiala // If we made it here, we didn't find an entry that contained the given address. 3465af245d11STodd Fiala error.SetErrorString ("address comes after final region"); 3466af245d11STodd Fiala 3467af245d11STodd Fiala if (log) 3468af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to find map entry for address 0x%" PRIx64 ": %s", __FUNCTION__, load_addr, error.AsCString ()); 3469af245d11STodd Fiala 3470af245d11STodd Fiala return error; 3471af245d11STodd Fiala } 3472af245d11STodd Fiala 3473af245d11STodd Fiala void 3474af245d11STodd Fiala NativeProcessLinux::DoStopIDBumped (uint32_t newBumpId) 3475af245d11STodd Fiala { 3476af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3477af245d11STodd Fiala if (log) 3478af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(newBumpId=%" PRIu32 ") called", __FUNCTION__, newBumpId); 3479af245d11STodd Fiala 3480af245d11STodd Fiala { 3481af245d11STodd Fiala Mutex::Locker locker (m_mem_region_cache_mutex); 3482af245d11STodd Fiala if (log) 3483af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s clearing %" PRIu64 " entries from the cache", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); 3484af245d11STodd Fiala m_mem_region_cache.clear (); 3485af245d11STodd Fiala } 3486af245d11STodd Fiala } 3487af245d11STodd Fiala 3488af245d11STodd Fiala Error 34893eb4b458SChaoren Lin NativeProcessLinux::AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) 3490af245d11STodd Fiala { 3491af245d11STodd Fiala // FIXME implementing this requires the equivalent of 3492af245d11STodd Fiala // InferiorCallPOSIX::InferiorCallMmap, which depends on 3493af245d11STodd Fiala // functional ThreadPlans working with Native*Protocol. 3494af245d11STodd Fiala #if 1 3495af245d11STodd Fiala return Error ("not implemented yet"); 3496af245d11STodd Fiala #else 3497af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 3498af245d11STodd Fiala 3499af245d11STodd Fiala unsigned prot = 0; 3500af245d11STodd Fiala if (permissions & lldb::ePermissionsReadable) 3501af245d11STodd Fiala prot |= eMmapProtRead; 3502af245d11STodd Fiala if (permissions & lldb::ePermissionsWritable) 3503af245d11STodd Fiala prot |= eMmapProtWrite; 3504af245d11STodd Fiala if (permissions & lldb::ePermissionsExecutable) 3505af245d11STodd Fiala prot |= eMmapProtExec; 3506af245d11STodd Fiala 3507af245d11STodd Fiala // TODO implement this directly in NativeProcessLinux 3508af245d11STodd Fiala // (and lift to NativeProcessPOSIX if/when that class is 3509af245d11STodd Fiala // refactored out). 3510af245d11STodd Fiala if (InferiorCallMmap(this, addr, 0, size, prot, 3511af245d11STodd Fiala eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { 3512af245d11STodd Fiala m_addr_to_mmap_size[addr] = size; 3513af245d11STodd Fiala return Error (); 3514af245d11STodd Fiala } else { 3515af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 3516af245d11STodd Fiala return Error("unable to allocate %" PRIu64 " bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions)); 3517af245d11STodd Fiala } 3518af245d11STodd Fiala #endif 3519af245d11STodd Fiala } 3520af245d11STodd Fiala 3521af245d11STodd Fiala Error 3522af245d11STodd Fiala NativeProcessLinux::DeallocateMemory (lldb::addr_t addr) 3523af245d11STodd Fiala { 3524af245d11STodd Fiala // FIXME see comments in AllocateMemory - required lower-level 3525af245d11STodd Fiala // bits not in place yet (ThreadPlans) 3526af245d11STodd Fiala return Error ("not implemented"); 3527af245d11STodd Fiala } 3528af245d11STodd Fiala 3529af245d11STodd Fiala lldb::addr_t 3530af245d11STodd Fiala NativeProcessLinux::GetSharedLibraryInfoAddress () 3531af245d11STodd Fiala { 3532af245d11STodd Fiala #if 1 3533af245d11STodd Fiala // punt on this for now 3534af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3535af245d11STodd Fiala #else 3536af245d11STodd Fiala // Return the image info address for the exe module 3537af245d11STodd Fiala #if 1 3538af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3539af245d11STodd Fiala 3540af245d11STodd Fiala ModuleSP module_sp; 3541af245d11STodd Fiala Error error = GetExeModuleSP (module_sp); 3542af245d11STodd Fiala if (error.Fail ()) 3543af245d11STodd Fiala { 3544af245d11STodd Fiala if (log) 3545af245d11STodd Fiala log->Warning ("NativeProcessLinux::%s failed to retrieve exe module: %s", __FUNCTION__, error.AsCString ()); 3546af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3547af245d11STodd Fiala } 3548af245d11STodd Fiala 3549af245d11STodd Fiala if (module_sp == nullptr) 3550af245d11STodd Fiala { 3551af245d11STodd Fiala if (log) 3552af245d11STodd Fiala log->Warning ("NativeProcessLinux::%s exe module returned was NULL", __FUNCTION__); 3553af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3554af245d11STodd Fiala } 3555af245d11STodd Fiala 3556af245d11STodd Fiala ObjectFileSP object_file_sp = module_sp->GetObjectFile (); 3557af245d11STodd Fiala if (object_file_sp == nullptr) 3558af245d11STodd Fiala { 3559af245d11STodd Fiala if (log) 3560af245d11STodd Fiala log->Warning ("NativeProcessLinux::%s exe module returned a NULL object file", __FUNCTION__); 3561af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3562af245d11STodd Fiala } 3563af245d11STodd Fiala 3564af245d11STodd Fiala return obj_file_sp->GetImageInfoAddress(); 3565af245d11STodd Fiala #else 3566af245d11STodd Fiala Target *target = &GetTarget(); 3567af245d11STodd Fiala ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile(); 3568af245d11STodd Fiala Address addr = obj_file->GetImageInfoAddress(target); 3569af245d11STodd Fiala 3570af245d11STodd Fiala if (addr.IsValid()) 3571af245d11STodd Fiala return addr.GetLoadAddress(target); 3572af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3573af245d11STodd Fiala #endif 3574af245d11STodd Fiala #endif // punt on this for now 3575af245d11STodd Fiala } 3576af245d11STodd Fiala 3577af245d11STodd Fiala size_t 3578af245d11STodd Fiala NativeProcessLinux::UpdateThreads () 3579af245d11STodd Fiala { 3580af245d11STodd Fiala // The NativeProcessLinux monitoring threads are always up to date 3581af245d11STodd Fiala // with respect to thread state and they keep the thread list 3582af245d11STodd Fiala // populated properly. All this method needs to do is return the 3583af245d11STodd Fiala // thread count. 3584af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 3585af245d11STodd Fiala return m_threads.size (); 3586af245d11STodd Fiala } 3587af245d11STodd Fiala 3588af245d11STodd Fiala bool 3589af245d11STodd Fiala NativeProcessLinux::GetArchitecture (ArchSpec &arch) const 3590af245d11STodd Fiala { 3591af245d11STodd Fiala arch = m_arch; 3592af245d11STodd Fiala return true; 3593af245d11STodd Fiala } 3594af245d11STodd Fiala 3595af245d11STodd Fiala Error 359663c8be95STamas Berghammer NativeProcessLinux::GetSoftwareBreakpointPCOffset (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size) 3597af245d11STodd Fiala { 3598af245d11STodd Fiala // FIXME put this behind a breakpoint protocol class that can be 3599af245d11STodd Fiala // set per architecture. Need ARM, MIPS support here. 36002afc5966STodd Fiala static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; 3601af245d11STodd Fiala static const uint8_t g_i386_opcode [] = { 0xCC }; 3602e8659b5dSMohit K. Bhakkad static const uint8_t g_mips64_opcode[] = { 0x00, 0x00, 0x00, 0x0d }; 3603af245d11STodd Fiala 3604af245d11STodd Fiala switch (m_arch.GetMachine ()) 3605af245d11STodd Fiala { 36062afc5966STodd Fiala case llvm::Triple::aarch64: 36072afc5966STodd Fiala actual_opcode_size = static_cast<uint32_t> (sizeof(g_aarch64_opcode)); 36082afc5966STodd Fiala return Error (); 36092afc5966STodd Fiala 361063c8be95STamas Berghammer case llvm::Triple::arm: 361163c8be95STamas Berghammer actual_opcode_size = 0; // On arm the PC don't get updated for breakpoint hits 361263c8be95STamas Berghammer return Error (); 361363c8be95STamas Berghammer 3614af245d11STodd Fiala case llvm::Triple::x86: 3615af245d11STodd Fiala case llvm::Triple::x86_64: 3616af245d11STodd Fiala actual_opcode_size = static_cast<uint32_t> (sizeof(g_i386_opcode)); 3617af245d11STodd Fiala return Error (); 3618af245d11STodd Fiala 3619e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64: 3620e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64el: 3621e8659b5dSMohit K. Bhakkad actual_opcode_size = static_cast<uint32_t> (sizeof(g_mips64_opcode)); 3622e8659b5dSMohit K. Bhakkad return Error (); 3623e8659b5dSMohit K. Bhakkad 3624af245d11STodd Fiala default: 3625af245d11STodd Fiala assert(false && "CPU type not supported!"); 3626af245d11STodd Fiala return Error ("CPU type not supported"); 3627af245d11STodd Fiala } 3628af245d11STodd Fiala } 3629af245d11STodd Fiala 3630af245d11STodd Fiala Error 3631af245d11STodd Fiala NativeProcessLinux::SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) 3632af245d11STodd Fiala { 3633af245d11STodd Fiala if (hardware) 3634af245d11STodd Fiala return Error ("NativeProcessLinux does not support hardware breakpoints"); 3635af245d11STodd Fiala else 3636af245d11STodd Fiala return SetSoftwareBreakpoint (addr, size); 3637af245d11STodd Fiala } 3638af245d11STodd Fiala 3639af245d11STodd Fiala Error 364063c8be95STamas Berghammer NativeProcessLinux::GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, 364163c8be95STamas Berghammer size_t &actual_opcode_size, 364263c8be95STamas Berghammer const uint8_t *&trap_opcode_bytes) 3643af245d11STodd Fiala { 364463c8be95STamas Berghammer // FIXME put this behind a breakpoint protocol class that can be set per 364563c8be95STamas Berghammer // architecture. Need MIPS support here. 36462afc5966STodd Fiala static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; 364763c8be95STamas Berghammer // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the 364863c8be95STamas Berghammer // linux kernel does otherwise. 364963c8be95STamas Berghammer static const uint8_t g_arm_breakpoint_opcode[] = { 0xf0, 0x01, 0xf0, 0xe7 }; 3650af245d11STodd Fiala static const uint8_t g_i386_opcode [] = { 0xCC }; 36513df471c3SMohit K. Bhakkad static const uint8_t g_mips64_opcode[] = { 0x00, 0x00, 0x00, 0x0d }; 36522c2acf96SMohit K. Bhakkad static const uint8_t g_mips64el_opcode[] = { 0x0d, 0x00, 0x00, 0x00 }; 365363c8be95STamas Berghammer static const uint8_t g_thumb_breakpoint_opcode[] = { 0x01, 0xde }; 3654af245d11STodd Fiala 3655af245d11STodd Fiala switch (m_arch.GetMachine ()) 3656af245d11STodd Fiala { 36572afc5966STodd Fiala case llvm::Triple::aarch64: 36582afc5966STodd Fiala trap_opcode_bytes = g_aarch64_opcode; 36592afc5966STodd Fiala actual_opcode_size = sizeof(g_aarch64_opcode); 36602afc5966STodd Fiala return Error (); 36612afc5966STodd Fiala 366263c8be95STamas Berghammer case llvm::Triple::arm: 366363c8be95STamas Berghammer switch (trap_opcode_size_hint) 366463c8be95STamas Berghammer { 366563c8be95STamas Berghammer case 2: 366663c8be95STamas Berghammer trap_opcode_bytes = g_thumb_breakpoint_opcode; 366763c8be95STamas Berghammer actual_opcode_size = sizeof(g_thumb_breakpoint_opcode); 366863c8be95STamas Berghammer return Error (); 366963c8be95STamas Berghammer case 4: 367063c8be95STamas Berghammer trap_opcode_bytes = g_arm_breakpoint_opcode; 367163c8be95STamas Berghammer actual_opcode_size = sizeof(g_arm_breakpoint_opcode); 367263c8be95STamas Berghammer return Error (); 367363c8be95STamas Berghammer default: 367463c8be95STamas Berghammer assert(false && "Unrecognised trap opcode size hint!"); 367563c8be95STamas Berghammer return Error ("Unrecognised trap opcode size hint!"); 367663c8be95STamas Berghammer } 367763c8be95STamas Berghammer 3678af245d11STodd Fiala case llvm::Triple::x86: 3679af245d11STodd Fiala case llvm::Triple::x86_64: 3680af245d11STodd Fiala trap_opcode_bytes = g_i386_opcode; 3681af245d11STodd Fiala actual_opcode_size = sizeof(g_i386_opcode); 3682af245d11STodd Fiala return Error (); 3683af245d11STodd Fiala 36843df471c3SMohit K. Bhakkad case llvm::Triple::mips64: 36853df471c3SMohit K. Bhakkad trap_opcode_bytes = g_mips64_opcode; 36863df471c3SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64_opcode); 36873df471c3SMohit K. Bhakkad return Error (); 36883df471c3SMohit K. Bhakkad 36892c2acf96SMohit K. Bhakkad case llvm::Triple::mips64el: 36902c2acf96SMohit K. Bhakkad trap_opcode_bytes = g_mips64el_opcode; 36912c2acf96SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64el_opcode); 36922c2acf96SMohit K. Bhakkad return Error (); 36932c2acf96SMohit K. Bhakkad 3694af245d11STodd Fiala default: 3695af245d11STodd Fiala assert(false && "CPU type not supported!"); 3696af245d11STodd Fiala return Error ("CPU type not supported"); 3697af245d11STodd Fiala } 3698af245d11STodd Fiala } 3699af245d11STodd Fiala 3700af245d11STodd Fiala #if 0 3701af245d11STodd Fiala ProcessMessage::CrashReason 3702af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info) 3703af245d11STodd Fiala { 3704af245d11STodd Fiala ProcessMessage::CrashReason reason; 3705af245d11STodd Fiala assert(info->si_signo == SIGSEGV); 3706af245d11STodd Fiala 3707af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 3708af245d11STodd Fiala 3709af245d11STodd Fiala switch (info->si_code) 3710af245d11STodd Fiala { 3711af245d11STodd Fiala default: 3712af245d11STodd Fiala assert(false && "unexpected si_code for SIGSEGV"); 3713af245d11STodd Fiala break; 3714af245d11STodd Fiala case SI_KERNEL: 3715af245d11STodd Fiala // Linux will occasionally send spurious SI_KERNEL codes. 3716af245d11STodd Fiala // (this is poorly documented in sigaction) 3717af245d11STodd Fiala // One way to get this is via unaligned SIMD loads. 3718af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; // for lack of anything better 3719af245d11STodd Fiala break; 3720af245d11STodd Fiala case SEGV_MAPERR: 3721af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; 3722af245d11STodd Fiala break; 3723af245d11STodd Fiala case SEGV_ACCERR: 3724af245d11STodd Fiala reason = ProcessMessage::ePrivilegedAddress; 3725af245d11STodd Fiala break; 3726af245d11STodd Fiala } 3727af245d11STodd Fiala 3728af245d11STodd Fiala return reason; 3729af245d11STodd Fiala } 3730af245d11STodd Fiala #endif 3731af245d11STodd Fiala 3732af245d11STodd Fiala 3733af245d11STodd Fiala #if 0 3734af245d11STodd Fiala ProcessMessage::CrashReason 3735af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info) 3736af245d11STodd Fiala { 3737af245d11STodd Fiala ProcessMessage::CrashReason reason; 3738af245d11STodd Fiala assert(info->si_signo == SIGILL); 3739af245d11STodd Fiala 3740af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 3741af245d11STodd Fiala 3742af245d11STodd Fiala switch (info->si_code) 3743af245d11STodd Fiala { 3744af245d11STodd Fiala default: 3745af245d11STodd Fiala assert(false && "unexpected si_code for SIGILL"); 3746af245d11STodd Fiala break; 3747af245d11STodd Fiala case ILL_ILLOPC: 3748af245d11STodd Fiala reason = ProcessMessage::eIllegalOpcode; 3749af245d11STodd Fiala break; 3750af245d11STodd Fiala case ILL_ILLOPN: 3751af245d11STodd Fiala reason = ProcessMessage::eIllegalOperand; 3752af245d11STodd Fiala break; 3753af245d11STodd Fiala case ILL_ILLADR: 3754af245d11STodd Fiala reason = ProcessMessage::eIllegalAddressingMode; 3755af245d11STodd Fiala break; 3756af245d11STodd Fiala case ILL_ILLTRP: 3757af245d11STodd Fiala reason = ProcessMessage::eIllegalTrap; 3758af245d11STodd Fiala break; 3759af245d11STodd Fiala case ILL_PRVOPC: 3760af245d11STodd Fiala reason = ProcessMessage::ePrivilegedOpcode; 3761af245d11STodd Fiala break; 3762af245d11STodd Fiala case ILL_PRVREG: 3763af245d11STodd Fiala reason = ProcessMessage::ePrivilegedRegister; 3764af245d11STodd Fiala break; 3765af245d11STodd Fiala case ILL_COPROC: 3766af245d11STodd Fiala reason = ProcessMessage::eCoprocessorError; 3767af245d11STodd Fiala break; 3768af245d11STodd Fiala case ILL_BADSTK: 3769af245d11STodd Fiala reason = ProcessMessage::eInternalStackError; 3770af245d11STodd Fiala break; 3771af245d11STodd Fiala } 3772af245d11STodd Fiala 3773af245d11STodd Fiala return reason; 3774af245d11STodd Fiala } 3775af245d11STodd Fiala #endif 3776af245d11STodd Fiala 3777af245d11STodd Fiala #if 0 3778af245d11STodd Fiala ProcessMessage::CrashReason 3779af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info) 3780af245d11STodd Fiala { 3781af245d11STodd Fiala ProcessMessage::CrashReason reason; 3782af245d11STodd Fiala assert(info->si_signo == SIGFPE); 3783af245d11STodd Fiala 3784af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 3785af245d11STodd Fiala 3786af245d11STodd Fiala switch (info->si_code) 3787af245d11STodd Fiala { 3788af245d11STodd Fiala default: 3789af245d11STodd Fiala assert(false && "unexpected si_code for SIGFPE"); 3790af245d11STodd Fiala break; 3791af245d11STodd Fiala case FPE_INTDIV: 3792af245d11STodd Fiala reason = ProcessMessage::eIntegerDivideByZero; 3793af245d11STodd Fiala break; 3794af245d11STodd Fiala case FPE_INTOVF: 3795af245d11STodd Fiala reason = ProcessMessage::eIntegerOverflow; 3796af245d11STodd Fiala break; 3797af245d11STodd Fiala case FPE_FLTDIV: 3798af245d11STodd Fiala reason = ProcessMessage::eFloatDivideByZero; 3799af245d11STodd Fiala break; 3800af245d11STodd Fiala case FPE_FLTOVF: 3801af245d11STodd Fiala reason = ProcessMessage::eFloatOverflow; 3802af245d11STodd Fiala break; 3803af245d11STodd Fiala case FPE_FLTUND: 3804af245d11STodd Fiala reason = ProcessMessage::eFloatUnderflow; 3805af245d11STodd Fiala break; 3806af245d11STodd Fiala case FPE_FLTRES: 3807af245d11STodd Fiala reason = ProcessMessage::eFloatInexactResult; 3808af245d11STodd Fiala break; 3809af245d11STodd Fiala case FPE_FLTINV: 3810af245d11STodd Fiala reason = ProcessMessage::eFloatInvalidOperation; 3811af245d11STodd Fiala break; 3812af245d11STodd Fiala case FPE_FLTSUB: 3813af245d11STodd Fiala reason = ProcessMessage::eFloatSubscriptRange; 3814af245d11STodd Fiala break; 3815af245d11STodd Fiala } 3816af245d11STodd Fiala 3817af245d11STodd Fiala return reason; 3818af245d11STodd Fiala } 3819af245d11STodd Fiala #endif 3820af245d11STodd Fiala 3821af245d11STodd Fiala #if 0 3822af245d11STodd Fiala ProcessMessage::CrashReason 3823af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info) 3824af245d11STodd Fiala { 3825af245d11STodd Fiala ProcessMessage::CrashReason reason; 3826af245d11STodd Fiala assert(info->si_signo == SIGBUS); 3827af245d11STodd Fiala 3828af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 3829af245d11STodd Fiala 3830af245d11STodd Fiala switch (info->si_code) 3831af245d11STodd Fiala { 3832af245d11STodd Fiala default: 3833af245d11STodd Fiala assert(false && "unexpected si_code for SIGBUS"); 3834af245d11STodd Fiala break; 3835af245d11STodd Fiala case BUS_ADRALN: 3836af245d11STodd Fiala reason = ProcessMessage::eIllegalAlignment; 3837af245d11STodd Fiala break; 3838af245d11STodd Fiala case BUS_ADRERR: 3839af245d11STodd Fiala reason = ProcessMessage::eIllegalAddress; 3840af245d11STodd Fiala break; 3841af245d11STodd Fiala case BUS_OBJERR: 3842af245d11STodd Fiala reason = ProcessMessage::eHardwareError; 3843af245d11STodd Fiala break; 3844af245d11STodd Fiala } 3845af245d11STodd Fiala 3846af245d11STodd Fiala return reason; 3847af245d11STodd Fiala } 3848af245d11STodd Fiala #endif 3849af245d11STodd Fiala 3850af245d11STodd Fiala Error 385145f5cb31SPavel Labath NativeProcessLinux::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) 385245f5cb31SPavel Labath { 385345f5cb31SPavel Labath // The base SetWatchpoint will end up executing monitor operations. Let's lock the monitor 385445f5cb31SPavel Labath // for it. 385545f5cb31SPavel Labath Monitor::ScopedOperationLock monitor_lock(*m_monitor_up); 385645f5cb31SPavel Labath return NativeProcessProtocol::SetWatchpoint(addr, size, watch_flags, hardware); 385745f5cb31SPavel Labath } 385845f5cb31SPavel Labath 385945f5cb31SPavel Labath Error 386045f5cb31SPavel Labath NativeProcessLinux::RemoveWatchpoint (lldb::addr_t addr) 386145f5cb31SPavel Labath { 386245f5cb31SPavel Labath // The base RemoveWatchpoint will end up executing monitor operations. Let's lock the monitor 386345f5cb31SPavel Labath // for it. 386445f5cb31SPavel Labath Monitor::ScopedOperationLock monitor_lock(*m_monitor_up); 386545f5cb31SPavel Labath return NativeProcessProtocol::RemoveWatchpoint(addr); 386645f5cb31SPavel Labath } 386745f5cb31SPavel Labath 386845f5cb31SPavel Labath Error 386926438d26SChaoren Lin NativeProcessLinux::ReadMemory (lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) 3870af245d11STodd Fiala { 3871af245d11STodd Fiala ReadOperation op(addr, buf, size, bytes_read); 3872bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 3873af245d11STodd Fiala return op.GetError (); 3874af245d11STodd Fiala } 3875af245d11STodd Fiala 3876af245d11STodd Fiala Error 38773eb4b458SChaoren Lin NativeProcessLinux::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) 38783eb4b458SChaoren Lin { 38793eb4b458SChaoren Lin Error error = ReadMemory(addr, buf, size, bytes_read); 38803eb4b458SChaoren Lin if (error.Fail()) return error; 38813eb4b458SChaoren Lin return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); 38823eb4b458SChaoren Lin } 38833eb4b458SChaoren Lin 38843eb4b458SChaoren Lin Error 38853eb4b458SChaoren Lin NativeProcessLinux::WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) 3886af245d11STodd Fiala { 3887af245d11STodd Fiala WriteOperation op(addr, buf, size, bytes_written); 3888bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 3889af245d11STodd Fiala return op.GetError (); 3890af245d11STodd Fiala } 3891af245d11STodd Fiala 389297ccc294SChaoren Lin Error 3893af245d11STodd Fiala NativeProcessLinux::ReadRegisterValue(lldb::tid_t tid, uint32_t offset, const char* reg_name, 3894af245d11STodd Fiala uint32_t size, RegisterValue &value) 3895af245d11STodd Fiala { 389697ccc294SChaoren Lin ReadRegOperation op(tid, offset, reg_name, value); 3897bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 389897ccc294SChaoren Lin return op.GetError(); 3899af245d11STodd Fiala } 3900af245d11STodd Fiala 390197ccc294SChaoren Lin Error 3902af245d11STodd Fiala NativeProcessLinux::WriteRegisterValue(lldb::tid_t tid, unsigned offset, 3903af245d11STodd Fiala const char* reg_name, const RegisterValue &value) 3904af245d11STodd Fiala { 390597ccc294SChaoren Lin WriteRegOperation op(tid, offset, reg_name, value); 3906bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 390797ccc294SChaoren Lin return op.GetError(); 3908af245d11STodd Fiala } 3909af245d11STodd Fiala 391097ccc294SChaoren Lin Error 3911af245d11STodd Fiala NativeProcessLinux::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size) 3912af245d11STodd Fiala { 391397ccc294SChaoren Lin ReadGPROperation op(tid, buf, buf_size); 3914bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 391597ccc294SChaoren Lin return op.GetError(); 3916af245d11STodd Fiala } 3917af245d11STodd Fiala 391897ccc294SChaoren Lin Error 3919af245d11STodd Fiala NativeProcessLinux::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size) 3920af245d11STodd Fiala { 392197ccc294SChaoren Lin ReadFPROperation op(tid, buf, buf_size); 3922bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 392397ccc294SChaoren Lin return op.GetError(); 3924af245d11STodd Fiala } 3925af245d11STodd Fiala 392697ccc294SChaoren Lin Error 3927af245d11STodd Fiala NativeProcessLinux::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) 3928af245d11STodd Fiala { 392997ccc294SChaoren Lin ReadRegisterSetOperation op(tid, buf, buf_size, regset); 3930bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 393197ccc294SChaoren Lin return op.GetError(); 3932af245d11STodd Fiala } 3933af245d11STodd Fiala 393497ccc294SChaoren Lin Error 3935af245d11STodd Fiala NativeProcessLinux::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size) 3936af245d11STodd Fiala { 393797ccc294SChaoren Lin WriteGPROperation op(tid, buf, buf_size); 3938bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 393997ccc294SChaoren Lin return op.GetError(); 3940af245d11STodd Fiala } 3941af245d11STodd Fiala 394297ccc294SChaoren Lin Error 3943af245d11STodd Fiala NativeProcessLinux::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size) 3944af245d11STodd Fiala { 394597ccc294SChaoren Lin WriteFPROperation op(tid, buf, buf_size); 3946bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 394797ccc294SChaoren Lin return op.GetError(); 3948af245d11STodd Fiala } 3949af245d11STodd Fiala 395097ccc294SChaoren Lin Error 3951af245d11STodd Fiala NativeProcessLinux::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) 3952af245d11STodd Fiala { 395397ccc294SChaoren Lin WriteRegisterSetOperation op(tid, buf, buf_size, regset); 3954bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 395597ccc294SChaoren Lin return op.GetError(); 3956af245d11STodd Fiala } 3957af245d11STodd Fiala 395897ccc294SChaoren Lin Error 3959af245d11STodd Fiala NativeProcessLinux::Resume (lldb::tid_t tid, uint32_t signo) 3960af245d11STodd Fiala { 3961af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3962af245d11STodd Fiala 3963af245d11STodd Fiala if (log) 3964af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid, 3965af245d11STodd Fiala GetUnixSignals().GetSignalAsCString (signo)); 396697ccc294SChaoren Lin ResumeOperation op (tid, signo); 3967bd7cbc5aSPavel Labath m_monitor_up->DoOperation (&op); 3968af245d11STodd Fiala if (log) 396997ccc294SChaoren Lin log->Printf ("NativeProcessLinux::%s() resuming thread = %" PRIu64 " result = %s", __FUNCTION__, tid, op.GetError().Success() ? "true" : "false"); 397097ccc294SChaoren Lin return op.GetError(); 3971af245d11STodd Fiala } 3972af245d11STodd Fiala 397397ccc294SChaoren Lin Error 3974af245d11STodd Fiala NativeProcessLinux::SingleStep(lldb::tid_t tid, uint32_t signo) 3975af245d11STodd Fiala { 397697ccc294SChaoren Lin SingleStepOperation op(tid, signo); 3977bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 397897ccc294SChaoren Lin return op.GetError(); 3979af245d11STodd Fiala } 3980af245d11STodd Fiala 398197ccc294SChaoren Lin Error 398297ccc294SChaoren Lin NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) 3983af245d11STodd Fiala { 398497ccc294SChaoren Lin SiginfoOperation op(tid, siginfo); 3985bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 398697ccc294SChaoren Lin return op.GetError(); 3987af245d11STodd Fiala } 3988af245d11STodd Fiala 398997ccc294SChaoren Lin Error 3990af245d11STodd Fiala NativeProcessLinux::GetEventMessage(lldb::tid_t tid, unsigned long *message) 3991af245d11STodd Fiala { 399297ccc294SChaoren Lin EventMessageOperation op(tid, message); 3993bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 399497ccc294SChaoren Lin return op.GetError(); 3995af245d11STodd Fiala } 3996af245d11STodd Fiala 3997db264a6dSTamas Berghammer Error 3998af245d11STodd Fiala NativeProcessLinux::Detach(lldb::tid_t tid) 3999af245d11STodd Fiala { 400097ccc294SChaoren Lin if (tid == LLDB_INVALID_THREAD_ID) 400197ccc294SChaoren Lin return Error(); 400297ccc294SChaoren Lin 400397ccc294SChaoren Lin DetachOperation op(tid); 4004bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 400597ccc294SChaoren Lin return op.GetError(); 4006af245d11STodd Fiala } 4007af245d11STodd Fiala 4008af245d11STodd Fiala bool 4009af245d11STodd Fiala NativeProcessLinux::DupDescriptor(const char *path, int fd, int flags) 4010af245d11STodd Fiala { 4011af245d11STodd Fiala int target_fd = open(path, flags, 0666); 4012af245d11STodd Fiala 4013af245d11STodd Fiala if (target_fd == -1) 4014af245d11STodd Fiala return false; 4015af245d11STodd Fiala 4016493c3a12SPavel Labath if (dup2(target_fd, fd) == -1) 4017493c3a12SPavel Labath return false; 4018493c3a12SPavel Labath 4019493c3a12SPavel Labath return (close(target_fd) == -1) ? false : true; 4020af245d11STodd Fiala } 4021af245d11STodd Fiala 4022af245d11STodd Fiala void 4023bd7cbc5aSPavel Labath NativeProcessLinux::StartMonitorThread(const InitialOperation &initial_operation, Error &error) 4024af245d11STodd Fiala { 4025bd7cbc5aSPavel Labath m_monitor_up.reset(new Monitor(initial_operation, this)); 40261107b5a5SPavel Labath error = m_monitor_up->Initialize(); 40271107b5a5SPavel Labath if (error.Fail()) { 40281107b5a5SPavel Labath m_monitor_up.reset(); 4029af245d11STodd Fiala } 4030af245d11STodd Fiala } 4031af245d11STodd Fiala 4032af245d11STodd Fiala bool 4033af245d11STodd Fiala NativeProcessLinux::HasThreadNoLock (lldb::tid_t thread_id) 4034af245d11STodd Fiala { 4035af245d11STodd Fiala for (auto thread_sp : m_threads) 4036af245d11STodd Fiala { 4037af245d11STodd Fiala assert (thread_sp && "thread list should not contain NULL threads"); 4038af245d11STodd Fiala if (thread_sp->GetID () == thread_id) 4039af245d11STodd Fiala { 4040af245d11STodd Fiala // We have this thread. 4041af245d11STodd Fiala return true; 4042af245d11STodd Fiala } 4043af245d11STodd Fiala } 4044af245d11STodd Fiala 4045af245d11STodd Fiala // We don't have this thread. 4046af245d11STodd Fiala return false; 4047af245d11STodd Fiala } 4048af245d11STodd Fiala 4049af245d11STodd Fiala NativeThreadProtocolSP 4050af245d11STodd Fiala NativeProcessLinux::MaybeGetThreadNoLock (lldb::tid_t thread_id) 4051af245d11STodd Fiala { 4052af245d11STodd Fiala // CONSIDER organize threads by map - we can do better than linear. 4053af245d11STodd Fiala for (auto thread_sp : m_threads) 4054af245d11STodd Fiala { 4055af245d11STodd Fiala if (thread_sp->GetID () == thread_id) 4056af245d11STodd Fiala return thread_sp; 4057af245d11STodd Fiala } 4058af245d11STodd Fiala 4059af245d11STodd Fiala // We don't have this thread. 4060af245d11STodd Fiala return NativeThreadProtocolSP (); 4061af245d11STodd Fiala } 4062af245d11STodd Fiala 4063af245d11STodd Fiala bool 4064af245d11STodd Fiala NativeProcessLinux::StopTrackingThread (lldb::tid_t thread_id) 4065af245d11STodd Fiala { 4066af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 4067af245d11STodd Fiala for (auto it = m_threads.begin (); it != m_threads.end (); ++it) 4068af245d11STodd Fiala { 4069af245d11STodd Fiala if (*it && ((*it)->GetID () == thread_id)) 4070af245d11STodd Fiala { 4071af245d11STodd Fiala m_threads.erase (it); 4072af245d11STodd Fiala return true; 4073af245d11STodd Fiala } 4074af245d11STodd Fiala } 4075af245d11STodd Fiala 4076af245d11STodd Fiala // Didn't find it. 4077af245d11STodd Fiala return false; 4078af245d11STodd Fiala } 4079af245d11STodd Fiala 4080af245d11STodd Fiala NativeThreadProtocolSP 4081af245d11STodd Fiala NativeProcessLinux::AddThread (lldb::tid_t thread_id) 4082af245d11STodd Fiala { 4083af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 4084af245d11STodd Fiala 4085af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 4086af245d11STodd Fiala 4087af245d11STodd Fiala if (log) 4088af245d11STodd Fiala { 4089af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " adding thread with tid %" PRIu64, 4090af245d11STodd Fiala __FUNCTION__, 4091af245d11STodd Fiala GetID (), 4092af245d11STodd Fiala thread_id); 4093af245d11STodd Fiala } 4094af245d11STodd Fiala 4095af245d11STodd Fiala assert (!HasThreadNoLock (thread_id) && "attempted to add a thread by id that already exists"); 4096af245d11STodd Fiala 4097af245d11STodd Fiala // If this is the first thread, save it as the current thread 4098af245d11STodd Fiala if (m_threads.empty ()) 4099af245d11STodd Fiala SetCurrentThreadID (thread_id); 4100af245d11STodd Fiala 4101af245d11STodd Fiala NativeThreadProtocolSP thread_sp (new NativeThreadLinux (this, thread_id)); 4102af245d11STodd Fiala m_threads.push_back (thread_sp); 4103af245d11STodd Fiala 4104af245d11STodd Fiala return thread_sp; 4105af245d11STodd Fiala } 4106af245d11STodd Fiala 4107af245d11STodd Fiala Error 4108af245d11STodd Fiala NativeProcessLinux::FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp) 4109af245d11STodd Fiala { 411075f47c3aSTodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 4111af245d11STodd Fiala 4112af245d11STodd Fiala Error error; 4113af245d11STodd Fiala 4114af245d11STodd Fiala // Get a linux thread pointer. 4115af245d11STodd Fiala if (!thread_sp) 4116af245d11STodd Fiala { 4117af245d11STodd Fiala error.SetErrorString ("null thread_sp"); 4118af245d11STodd Fiala if (log) 4119af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); 4120af245d11STodd Fiala return error; 4121af245d11STodd Fiala } 4122cb84eebbSTamas Berghammer std::shared_ptr<NativeThreadLinux> linux_thread_sp = std::static_pointer_cast<NativeThreadLinux> (thread_sp); 4123af245d11STodd Fiala 4124af245d11STodd Fiala // Find out the size of a breakpoint (might depend on where we are in the code). 4125cb84eebbSTamas Berghammer NativeRegisterContextSP context_sp = linux_thread_sp->GetRegisterContext (); 4126af245d11STodd Fiala if (!context_sp) 4127af245d11STodd Fiala { 4128af245d11STodd Fiala error.SetErrorString ("cannot get a NativeRegisterContext for the thread"); 4129af245d11STodd Fiala if (log) 4130af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); 4131af245d11STodd Fiala return error; 4132af245d11STodd Fiala } 4133af245d11STodd Fiala 4134af245d11STodd Fiala uint32_t breakpoint_size = 0; 413563c8be95STamas Berghammer error = GetSoftwareBreakpointPCOffset (context_sp, breakpoint_size); 4136af245d11STodd Fiala if (error.Fail ()) 4137af245d11STodd Fiala { 4138af245d11STodd Fiala if (log) 4139af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s GetBreakpointSize() failed: %s", __FUNCTION__, error.AsCString ()); 4140af245d11STodd Fiala return error; 4141af245d11STodd Fiala } 4142af245d11STodd Fiala else 4143af245d11STodd Fiala { 4144af245d11STodd Fiala if (log) 4145af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s breakpoint size: %" PRIu32, __FUNCTION__, breakpoint_size); 4146af245d11STodd Fiala } 4147af245d11STodd Fiala 4148af245d11STodd Fiala // First try probing for a breakpoint at a software breakpoint location: PC - breakpoint size. 4149af245d11STodd Fiala const lldb::addr_t initial_pc_addr = context_sp->GetPC (); 4150af245d11STodd Fiala lldb::addr_t breakpoint_addr = initial_pc_addr; 41513eb4b458SChaoren Lin if (breakpoint_size > 0) 4152af245d11STodd Fiala { 4153af245d11STodd Fiala // Do not allow breakpoint probe to wrap around. 41543eb4b458SChaoren Lin if (breakpoint_addr >= breakpoint_size) 41553eb4b458SChaoren Lin breakpoint_addr -= breakpoint_size; 4156af245d11STodd Fiala } 4157af245d11STodd Fiala 4158af245d11STodd Fiala // Check if we stopped because of a breakpoint. 4159af245d11STodd Fiala NativeBreakpointSP breakpoint_sp; 4160af245d11STodd Fiala error = m_breakpoint_list.GetBreakpoint (breakpoint_addr, breakpoint_sp); 4161af245d11STodd Fiala if (!error.Success () || !breakpoint_sp) 4162af245d11STodd Fiala { 4163af245d11STodd Fiala // We didn't find one at a software probe location. Nothing to do. 4164af245d11STodd Fiala if (log) 4165af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " no lldb breakpoint found at current pc with adjustment: 0x%" PRIx64, __FUNCTION__, GetID (), breakpoint_addr); 4166af245d11STodd Fiala return Error (); 4167af245d11STodd Fiala } 4168af245d11STodd Fiala 4169af245d11STodd Fiala // If the breakpoint is not a software breakpoint, nothing to do. 4170af245d11STodd Fiala if (!breakpoint_sp->IsSoftwareBreakpoint ()) 4171af245d11STodd Fiala { 4172af245d11STodd Fiala if (log) 4173af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " breakpoint found at 0x%" PRIx64 ", not software, nothing to adjust", __FUNCTION__, GetID (), breakpoint_addr); 4174af245d11STodd Fiala return Error (); 4175af245d11STodd Fiala } 4176af245d11STodd Fiala 4177af245d11STodd Fiala // 4178af245d11STodd Fiala // We have a software breakpoint and need to adjust the PC. 4179af245d11STodd Fiala // 4180af245d11STodd Fiala 4181af245d11STodd Fiala // Sanity check. 4182af245d11STodd Fiala if (breakpoint_size == 0) 4183af245d11STodd Fiala { 4184af245d11STodd Fiala // Nothing to do! How did we get here? 4185af245d11STodd Fiala if (log) 4186af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " breakpoint found at 0x%" PRIx64 ", it is software, but the size is zero, nothing to do (unexpected)", __FUNCTION__, GetID (), breakpoint_addr); 4187af245d11STodd Fiala return Error (); 4188af245d11STodd Fiala } 4189af245d11STodd Fiala 4190af245d11STodd Fiala // Change the program counter. 4191af245d11STodd Fiala if (log) 4192cb84eebbSTamas Berghammer log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": changing PC from 0x%" PRIx64 " to 0x%" PRIx64, __FUNCTION__, GetID (), linux_thread_sp->GetID (), initial_pc_addr, breakpoint_addr); 4193af245d11STodd Fiala 4194af245d11STodd Fiala error = context_sp->SetPC (breakpoint_addr); 4195af245d11STodd Fiala if (error.Fail ()) 4196af245d11STodd Fiala { 4197af245d11STodd Fiala if (log) 4198cb84eebbSTamas Berghammer log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": failed to set PC: %s", __FUNCTION__, GetID (), linux_thread_sp->GetID (), error.AsCString ()); 4199af245d11STodd Fiala return error; 4200af245d11STodd Fiala } 4201af245d11STodd Fiala 4202af245d11STodd Fiala return error; 4203af245d11STodd Fiala } 4204fa03ad2eSChaoren Lin 4205fa03ad2eSChaoren Lin void 4206fa03ad2eSChaoren Lin NativeProcessLinux::NotifyThreadCreateStopped (lldb::tid_t tid) 4207fa03ad2eSChaoren Lin { 4208fa03ad2eSChaoren Lin const bool is_stopped = true; 4209c076559aSPavel Labath NotifyThreadCreate (tid, is_stopped, CoordinatorErrorHandler); 4210fa03ad2eSChaoren Lin } 4211fa03ad2eSChaoren Lin 4212fa03ad2eSChaoren Lin void 4213fa03ad2eSChaoren Lin NativeProcessLinux::NotifyThreadDeath (lldb::tid_t tid) 4214fa03ad2eSChaoren Lin { 4215c076559aSPavel Labath NotifyThreadDeath (tid, CoordinatorErrorHandler); 4216fa03ad2eSChaoren Lin } 4217fa03ad2eSChaoren Lin 4218fa03ad2eSChaoren Lin void 4219fa03ad2eSChaoren Lin NativeProcessLinux::NotifyThreadStop (lldb::tid_t tid) 4220fa03ad2eSChaoren Lin { 4221c076559aSPavel Labath NotifyThreadStop (tid, false, CoordinatorErrorHandler); 4222fa03ad2eSChaoren Lin } 4223fa03ad2eSChaoren Lin 4224fa03ad2eSChaoren Lin void 4225ed89c7feSPavel Labath NativeProcessLinux::StopRunningThreads(lldb::tid_t trigerring_tid) 4226fa03ad2eSChaoren Lin { 422786fd8e45SChaoren Lin Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 422886fd8e45SChaoren Lin if (log) 4229ed89c7feSPavel Labath log->Printf("NativeProcessLinux::%s tid %" PRIu64, __FUNCTION__, trigerring_tid); 423086fd8e45SChaoren Lin 4231fa03ad2eSChaoren Lin const lldb::pid_t pid = GetID (); 4232ed89c7feSPavel Labath StopRunningThreads(trigerring_tid, 4233ed89c7feSPavel Labath [=](lldb::tid_t request_stop_tid) { return RequestThreadStop(pid, request_stop_tid); }, 4234fa03ad2eSChaoren Lin CoordinatorErrorHandler); 423503f12d6bSChaoren Lin } 4236fa03ad2eSChaoren Lin 423703f12d6bSChaoren Lin void 4238ed89c7feSPavel Labath NativeProcessLinux::StopRunningThreadsWithSkipTID(lldb::tid_t deferred_signal_tid, 4239ed89c7feSPavel Labath lldb::tid_t skip_stop_request_tid) 424003f12d6bSChaoren Lin { 424186fd8e45SChaoren Lin Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 424286fd8e45SChaoren Lin if (log) 424386fd8e45SChaoren Lin log->Printf("NativeProcessLinux::%s deferred_signal_tid %" PRIu64 ", skip_stop_request_tid %" PRIu64, __FUNCTION__, deferred_signal_tid, skip_stop_request_tid); 424486fd8e45SChaoren Lin 424503f12d6bSChaoren Lin const lldb::pid_t pid = GetID (); 4246ed89c7feSPavel Labath StopRunningThreadsWithSkipTID(deferred_signal_tid, 4247c076559aSPavel Labath skip_stop_request_tid != LLDB_INVALID_THREAD_ID ? NativeProcessLinux::ThreadIDSet {skip_stop_request_tid} : NativeProcessLinux::ThreadIDSet (), 4248c076559aSPavel Labath [=](lldb::tid_t request_stop_tid) { return RequestThreadStop(pid, request_stop_tid); }, 424903f12d6bSChaoren Lin CoordinatorErrorHandler); 4250fa03ad2eSChaoren Lin } 425186fd8e45SChaoren Lin 4252db264a6dSTamas Berghammer Error 425386fd8e45SChaoren Lin NativeProcessLinux::RequestThreadStop (const lldb::pid_t pid, const lldb::tid_t tid) 425486fd8e45SChaoren Lin { 425586fd8e45SChaoren Lin Log* log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 425686fd8e45SChaoren Lin if (log) 425786fd8e45SChaoren Lin log->Printf ("NativeProcessLinux::%s requesting thread stop(pid: %" PRIu64 ", tid: %" PRIu64 ")", __FUNCTION__, pid, tid); 425886fd8e45SChaoren Lin 425986fd8e45SChaoren Lin Error err; 426086fd8e45SChaoren Lin errno = 0; 426186fd8e45SChaoren Lin if (::tgkill (pid, tid, SIGSTOP) != 0) 426286fd8e45SChaoren Lin { 426386fd8e45SChaoren Lin err.SetErrorToErrno (); 426486fd8e45SChaoren Lin if (log) 426586fd8e45SChaoren Lin log->Printf ("NativeProcessLinux::%s tgkill(%" PRIu64 ", %" PRIu64 ", SIGSTOP) failed: %s", __FUNCTION__, pid, tid, err.AsCString ()); 426686fd8e45SChaoren Lin } 426786fd8e45SChaoren Lin 426886fd8e45SChaoren Lin return err; 426986fd8e45SChaoren Lin } 42707cb18bf5STamas Berghammer 42717cb18bf5STamas Berghammer Error 42727cb18bf5STamas Berghammer NativeProcessLinux::GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) 42737cb18bf5STamas Berghammer { 42747cb18bf5STamas Berghammer char maps_file_name[32]; 42757cb18bf5STamas Berghammer snprintf(maps_file_name, sizeof(maps_file_name), "/proc/%" PRIu64 "/maps", GetID()); 42767cb18bf5STamas Berghammer 42777cb18bf5STamas Berghammer FileSpec maps_file_spec(maps_file_name, false); 42787cb18bf5STamas Berghammer if (!maps_file_spec.Exists()) { 42797cb18bf5STamas Berghammer file_spec.Clear(); 42807cb18bf5STamas Berghammer return Error("/proc/%" PRIu64 "/maps file doesn't exists!", GetID()); 42817cb18bf5STamas Berghammer } 42827cb18bf5STamas Berghammer 42837cb18bf5STamas Berghammer FileSpec module_file_spec(module_path, true); 42847cb18bf5STamas Berghammer 42857cb18bf5STamas Berghammer std::ifstream maps_file(maps_file_name); 42867cb18bf5STamas Berghammer std::string maps_data_str((std::istreambuf_iterator<char>(maps_file)), std::istreambuf_iterator<char>()); 42877cb18bf5STamas Berghammer StringRef maps_data(maps_data_str.c_str()); 42887cb18bf5STamas Berghammer 42897cb18bf5STamas Berghammer while (!maps_data.empty()) 42907cb18bf5STamas Berghammer { 42917cb18bf5STamas Berghammer StringRef maps_row; 42927cb18bf5STamas Berghammer std::tie(maps_row, maps_data) = maps_data.split('\n'); 42937cb18bf5STamas Berghammer 42947cb18bf5STamas Berghammer SmallVector<StringRef, 16> maps_columns; 42957cb18bf5STamas Berghammer maps_row.split(maps_columns, StringRef(" "), -1, false); 42967cb18bf5STamas Berghammer 42977cb18bf5STamas Berghammer if (maps_columns.size() >= 6) 42987cb18bf5STamas Berghammer { 42997cb18bf5STamas Berghammer file_spec.SetFile(maps_columns[5].str().c_str(), false); 43007cb18bf5STamas Berghammer if (file_spec.GetFilename() == module_file_spec.GetFilename()) 43017cb18bf5STamas Berghammer return Error(); 43027cb18bf5STamas Berghammer } 43037cb18bf5STamas Berghammer } 43047cb18bf5STamas Berghammer 43057cb18bf5STamas Berghammer file_spec.Clear(); 43067cb18bf5STamas Berghammer return Error("Module file (%s) not found in /proc/%" PRIu64 "/maps file!", 43077cb18bf5STamas Berghammer module_file_spec.GetFilename().AsCString(), GetID()); 43087cb18bf5STamas Berghammer } 4309c076559aSPavel Labath 4310c076559aSPavel Labath void 4311c076559aSPavel Labath NativeProcessLinux::DoResume( 4312c076559aSPavel Labath lldb::tid_t tid, 4313c076559aSPavel Labath ResumeThreadFunction request_thread_resume_function, 4314c076559aSPavel Labath ErrorFunction error_function, 4315c076559aSPavel Labath bool error_when_already_running) 4316c076559aSPavel Labath { 4317c076559aSPavel Labath // Ensure we know about the thread. 4318c076559aSPavel Labath auto find_it = m_tid_map.find (tid); 4319c076559aSPavel Labath if (find_it == m_tid_map.end ()) 4320c076559aSPavel Labath { 4321c076559aSPavel Labath // We don't know about this thread. This is an error condition. 4322c076559aSPavel Labath std::ostringstream error_message; 4323c076559aSPavel Labath error_message << "error: tid " << tid << " asked to resume but tid is unknown"; 4324c076559aSPavel Labath error_function (error_message.str ()); 4325c076559aSPavel Labath return; 4326c076559aSPavel Labath } 4327c076559aSPavel Labath auto& context = find_it->second; 4328c076559aSPavel Labath // Tell the thread to resume if we don't already think it is running. 4329c076559aSPavel Labath const bool is_stopped = context.m_state == ThreadState::Stopped; 4330c076559aSPavel Labath if (!is_stopped) 4331c076559aSPavel Labath { 4332c076559aSPavel Labath // It's not an error, just a log, if the error_when_already_running flag is not set. 4333c076559aSPavel Labath // This covers cases where, for instance, we're just trying to resume all threads 4334c076559aSPavel Labath // from the user side. 4335c076559aSPavel Labath if (!error_when_already_running) 4336c076559aSPavel Labath { 4337c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s tid %" PRIu64 " optional resume skipped since it is already running", 4338c076559aSPavel Labath __FUNCTION__, 4339c076559aSPavel Labath tid); 4340c076559aSPavel Labath } 4341c076559aSPavel Labath else 4342c076559aSPavel Labath { 4343c076559aSPavel Labath // Skip the resume call - we have tracked it to be running. And we unconditionally 4344c076559aSPavel Labath // expected to resume this thread. Flag this as an error. 4345c076559aSPavel Labath std::ostringstream error_message; 4346c076559aSPavel Labath error_message << "error: tid " << tid << " asked to resume but we think it is already running"; 4347c076559aSPavel Labath error_function (error_message.str ()); 4348c076559aSPavel Labath } 4349c076559aSPavel Labath 4350c076559aSPavel Labath // Error or not, we're done. 4351c076559aSPavel Labath return; 4352c076559aSPavel Labath } 4353c076559aSPavel Labath 4354c076559aSPavel Labath // Before we do the resume below, first check if we have a pending 4355c076559aSPavel Labath // stop notification this is currently or was previously waiting for 4356c076559aSPavel Labath // this thread to stop. This is potentially a buggy situation since 4357c076559aSPavel Labath // we're ostensibly waiting for threads to stop before we send out the 4358c076559aSPavel Labath // pending notification, and here we are resuming one before we send 4359c076559aSPavel Labath // out the pending stop notification. 4360c076559aSPavel Labath if (m_pending_notification_up) 4361c076559aSPavel Labath { 4362c076559aSPavel Labath if (m_pending_notification_up->wait_for_stop_tids.count (tid) > 0) 4363c076559aSPavel Labath { 4364c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s about to resume tid %" PRIu64 " per explicit request but we have a pending stop notification (tid %" PRIu64 ") that is actively waiting for this thread to stop. Valid sequence of events?", __FUNCTION__, tid, m_pending_notification_up->triggering_tid); 4365c076559aSPavel Labath } 4366c076559aSPavel Labath else if (m_pending_notification_up->original_wait_for_stop_tids.count (tid) > 0) 4367c076559aSPavel Labath { 4368c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s about to resume tid %" PRIu64 " per explicit request but we have a pending stop notification (tid %" PRIu64 ") that hasn't fired yet and this is one of the threads we had been waiting on (and already marked satisfied for this tid). Valid sequence of events?", __FUNCTION__, tid, m_pending_notification_up->triggering_tid); 4369c076559aSPavel Labath for (auto tid : m_pending_notification_up->wait_for_stop_tids) 4370c076559aSPavel Labath { 4371c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s tid %" PRIu64 " deferred stop notification still waiting on tid %" PRIu64, 4372c076559aSPavel Labath __FUNCTION__, 4373c076559aSPavel Labath m_pending_notification_up->triggering_tid, 4374c076559aSPavel Labath tid); 4375c076559aSPavel Labath } 4376c076559aSPavel Labath } 4377c076559aSPavel Labath } 4378c076559aSPavel Labath 4379c076559aSPavel Labath // Request a resume. We expect this to be synchronous and the system 4380c076559aSPavel Labath // to reflect it is running after this completes. 4381c076559aSPavel Labath const auto error = request_thread_resume_function (tid, false); 4382c076559aSPavel Labath if (error.Success ()) 4383c076559aSPavel Labath { 4384c076559aSPavel Labath // Now mark it is running. 4385c076559aSPavel Labath context.m_state = ThreadState::Running; 4386c076559aSPavel Labath context.m_request_resume_function = request_thread_resume_function; 4387c076559aSPavel Labath } 4388c076559aSPavel Labath else 4389c076559aSPavel Labath { 4390c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s failed to resume thread tid %" PRIu64 ": %s", 4391c076559aSPavel Labath __FUNCTION__, tid, error.AsCString ()); 4392c076559aSPavel Labath } 4393c076559aSPavel Labath 4394c076559aSPavel Labath return; 4395c076559aSPavel Labath } 4396c076559aSPavel Labath 4397c076559aSPavel Labath //===----------------------------------------------------------------------===// 4398c076559aSPavel Labath 4399c076559aSPavel Labath void 4400ed89c7feSPavel Labath NativeProcessLinux::StopThreads(const lldb::tid_t triggering_tid, 4401c076559aSPavel Labath const ThreadIDSet &wait_for_stop_tids, 4402c076559aSPavel Labath const StopThreadFunction &request_thread_stop_function, 4403c076559aSPavel Labath const ErrorFunction &error_function) 4404c076559aSPavel Labath { 4405c076559aSPavel Labath std::lock_guard<std::mutex> lock(m_event_mutex); 4406c076559aSPavel Labath 4407c076559aSPavel Labath if (m_log_event_processing) 4408c076559aSPavel Labath { 4409c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s about to process event: (triggering_tid: %" PRIu64 ", wait_for_stop_tids.size(): %zd)", 4410c076559aSPavel Labath __FUNCTION__, triggering_tid, wait_for_stop_tids.size()); 4411c076559aSPavel Labath } 4412c076559aSPavel Labath 4413ed89c7feSPavel Labath DoStopThreads(PendingNotificationUP(new PendingNotification( 4414ed89c7feSPavel Labath triggering_tid, wait_for_stop_tids, request_thread_stop_function, error_function))); 4415c076559aSPavel Labath 4416c076559aSPavel Labath if (m_log_event_processing) 4417c076559aSPavel Labath { 4418c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s event processing done", __FUNCTION__); 4419c076559aSPavel Labath } 4420c076559aSPavel Labath } 4421c076559aSPavel Labath 4422c076559aSPavel Labath void 4423ed89c7feSPavel Labath NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid, 4424c076559aSPavel Labath const StopThreadFunction &request_thread_stop_function, 4425c076559aSPavel Labath const ErrorFunction &error_function) 4426c076559aSPavel Labath { 4427c076559aSPavel Labath std::lock_guard<std::mutex> lock(m_event_mutex); 4428c076559aSPavel Labath 4429c076559aSPavel Labath if (m_log_event_processing) 4430c076559aSPavel Labath { 4431c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s about to process event: (triggering_tid: %" PRIu64 ")", 4432c076559aSPavel Labath __FUNCTION__, triggering_tid); 4433c076559aSPavel Labath } 4434c076559aSPavel Labath 4435ed89c7feSPavel Labath DoStopThreads(PendingNotificationUP(new PendingNotification( 4436c076559aSPavel Labath triggering_tid, 4437c076559aSPavel Labath request_thread_stop_function, 4438c076559aSPavel Labath error_function))); 4439c076559aSPavel Labath 4440c076559aSPavel Labath if (m_log_event_processing) 4441c076559aSPavel Labath { 4442c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s event processing done", __FUNCTION__); 4443c076559aSPavel Labath } 4444c076559aSPavel Labath } 4445c076559aSPavel Labath 4446c076559aSPavel Labath void 4447ed89c7feSPavel Labath NativeProcessLinux::StopRunningThreadsWithSkipTID(lldb::tid_t triggering_tid, 4448c076559aSPavel Labath const ThreadIDSet &skip_stop_request_tids, 4449c076559aSPavel Labath const StopThreadFunction &request_thread_stop_function, 4450c076559aSPavel Labath const ErrorFunction &error_function) 4451c076559aSPavel Labath { 4452c076559aSPavel Labath std::lock_guard<std::mutex> lock(m_event_mutex); 4453c076559aSPavel Labath 4454c076559aSPavel Labath if (m_log_event_processing) 4455c076559aSPavel Labath { 4456c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s about to process event: (triggering_tid: %" PRIu64 ", skip_stop_request_tids.size(): %zd)", 4457c076559aSPavel Labath __FUNCTION__, triggering_tid, skip_stop_request_tids.size()); 4458c076559aSPavel Labath } 4459c076559aSPavel Labath 4460ed89c7feSPavel Labath DoStopThreads(PendingNotificationUP(new PendingNotification( 4461c076559aSPavel Labath triggering_tid, 4462c076559aSPavel Labath request_thread_stop_function, 4463c076559aSPavel Labath skip_stop_request_tids, 4464c076559aSPavel Labath error_function))); 4465c076559aSPavel Labath 4466c076559aSPavel Labath if (m_log_event_processing) 4467c076559aSPavel Labath { 4468c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s event processing done", __FUNCTION__); 4469c076559aSPavel Labath } 4470c076559aSPavel Labath } 4471c076559aSPavel Labath 4472c076559aSPavel Labath void 4473c076559aSPavel Labath NativeProcessLinux::SignalIfRequirementsSatisfied() 4474c076559aSPavel Labath { 4475c076559aSPavel Labath if (m_pending_notification_up && m_pending_notification_up->wait_for_stop_tids.empty ()) 4476c076559aSPavel Labath { 4477ed89c7feSPavel Labath SetCurrentThreadID(m_pending_notification_up->triggering_tid); 4478ed89c7feSPavel Labath SetState(StateType::eStateStopped, true); 4479c076559aSPavel Labath m_pending_notification_up.reset(); 4480c076559aSPavel Labath } 4481c076559aSPavel Labath } 4482c076559aSPavel Labath 4483c076559aSPavel Labath bool 4484c076559aSPavel Labath NativeProcessLinux::RequestStopOnAllSpecifiedThreads() 4485c076559aSPavel Labath { 4486c076559aSPavel Labath // Request a stop for all the thread stops that need to be stopped 4487c076559aSPavel Labath // and are not already known to be stopped. Keep a list of all the 4488c076559aSPavel Labath // threads from which we still need to hear a stop reply. 4489c076559aSPavel Labath 4490c076559aSPavel Labath ThreadIDSet sent_tids; 4491c076559aSPavel Labath for (auto tid : m_pending_notification_up->wait_for_stop_tids) 4492c076559aSPavel Labath { 4493c076559aSPavel Labath // Validate we know about all tids for which we must first receive a stop before 4494c076559aSPavel Labath // triggering the deferred stop notification. 4495c076559aSPavel Labath auto find_it = m_tid_map.find (tid); 4496c076559aSPavel Labath if (find_it == m_tid_map.end ()) 4497c076559aSPavel Labath { 4498c076559aSPavel Labath // This is an error. We shouldn't be asking for waiting pids that aren't known. 4499c076559aSPavel Labath // NOTE: we may be stripping out the specification of wait tids and handle this 4500c076559aSPavel Labath // automatically, in which case this state can never occur. 4501c076559aSPavel Labath std::ostringstream error_message; 4502c076559aSPavel Labath error_message << "error: deferred notification for tid " << m_pending_notification_up->triggering_tid << " specified an unknown/untracked pending stop tid " << m_pending_notification_up->triggering_tid; 4503c076559aSPavel Labath m_pending_notification_up->error_function (error_message.str ()); 4504c076559aSPavel Labath 4505c076559aSPavel Labath // Bail out here. 4506c076559aSPavel Labath return false; 4507c076559aSPavel Labath } 4508c076559aSPavel Labath 4509c076559aSPavel Labath // If the pending stop thread is currently running, we need to send it a stop request. 4510c076559aSPavel Labath auto& context = find_it->second; 4511c076559aSPavel Labath if (context.m_state == ThreadState::Running) 4512c076559aSPavel Labath { 4513c076559aSPavel Labath RequestThreadStop (tid, context); 4514c076559aSPavel Labath sent_tids.insert (tid); 4515c076559aSPavel Labath } 4516c076559aSPavel Labath } 4517c076559aSPavel Labath // We only need to wait for the sent_tids - so swap our wait set 4518c076559aSPavel Labath // to the sent tids. The rest are already stopped and we won't 4519c076559aSPavel Labath // be receiving stop notifications for them. 4520c076559aSPavel Labath m_pending_notification_up->wait_for_stop_tids.swap (sent_tids); 4521c076559aSPavel Labath 4522c076559aSPavel Labath // Succeeded, keep running. 4523c076559aSPavel Labath return true; 4524c076559aSPavel Labath } 4525c076559aSPavel Labath 4526c076559aSPavel Labath void 4527c076559aSPavel Labath NativeProcessLinux::RequestStopOnAllRunningThreads() 4528c076559aSPavel Labath { 4529c076559aSPavel Labath // Request a stop for all the thread stops that need to be stopped 4530c076559aSPavel Labath // and are not already known to be stopped. Keep a list of all the 4531c076559aSPavel Labath // threads from which we still need to hear a stop reply. 4532c076559aSPavel Labath 4533c076559aSPavel Labath ThreadIDSet sent_tids; 4534c076559aSPavel Labath for (auto it = m_tid_map.begin(); it != m_tid_map.end(); ++it) 4535c076559aSPavel Labath { 4536c076559aSPavel Labath // We only care about threads not stopped. 4537c076559aSPavel Labath const bool running = it->second.m_state == ThreadState::Running; 4538c076559aSPavel Labath if (running) 4539c076559aSPavel Labath { 4540c076559aSPavel Labath const lldb::tid_t tid = it->first; 4541c076559aSPavel Labath 4542c076559aSPavel Labath // Request this thread stop if the tid stop request is not explicitly ignored. 4543c076559aSPavel Labath const bool skip_stop_request = m_pending_notification_up->skip_stop_request_tids.count (tid) > 0; 4544c076559aSPavel Labath if (!skip_stop_request) 4545c076559aSPavel Labath RequestThreadStop (tid, it->second); 4546c076559aSPavel Labath 4547c076559aSPavel Labath // Even if we skipped sending the stop request for other reasons (like stepping), 4548c076559aSPavel Labath // we still need to wait for that stepping thread to notify completion/stop. 4549c076559aSPavel Labath sent_tids.insert (tid); 4550c076559aSPavel Labath } 4551c076559aSPavel Labath } 4552c076559aSPavel Labath 4553c076559aSPavel Labath // Set the wait list to the set of tids for which we requested stops. 4554c076559aSPavel Labath m_pending_notification_up->wait_for_stop_tids.swap (sent_tids); 4555c076559aSPavel Labath } 4556c076559aSPavel Labath 4557c076559aSPavel Labath void 4558c076559aSPavel Labath NativeProcessLinux::RequestThreadStop (lldb::tid_t tid, ThreadContext& context) 4559c076559aSPavel Labath { 4560c076559aSPavel Labath const auto error = m_pending_notification_up->request_thread_stop_function (tid); 4561c076559aSPavel Labath if (error.Success ()) 4562c076559aSPavel Labath context.m_stop_requested = true; 4563c076559aSPavel Labath else 4564c076559aSPavel Labath { 4565c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s failed to request thread stop tid %" PRIu64 ": %s", 4566c076559aSPavel Labath __FUNCTION__, tid, error.AsCString ()); 4567c076559aSPavel Labath } 4568c076559aSPavel Labath } 4569c076559aSPavel Labath 4570c076559aSPavel Labath 4571c076559aSPavel Labath void 4572c076559aSPavel Labath NativeProcessLinux::ThreadDidStop (lldb::tid_t tid, bool initiated_by_llgs, const ErrorFunction &error_function) 4573c076559aSPavel Labath { 4574c076559aSPavel Labath // Ensure we know about the thread. 4575c076559aSPavel Labath auto find_it = m_tid_map.find (tid); 4576c076559aSPavel Labath if (find_it == m_tid_map.end ()) 4577c076559aSPavel Labath { 4578c076559aSPavel Labath // We don't know about this thread. This is an error condition. 4579c076559aSPavel Labath std::ostringstream error_message; 4580c076559aSPavel Labath error_message << "error: tid " << tid << " asked to stop but tid is unknown"; 4581c076559aSPavel Labath error_function (error_message.str ()); 4582c076559aSPavel Labath return; 4583c076559aSPavel Labath } 4584c076559aSPavel Labath 4585c076559aSPavel Labath // Update the global list of known thread states. This one is definitely stopped. 4586c076559aSPavel Labath auto& context = find_it->second; 4587c076559aSPavel Labath const auto stop_was_requested = context.m_stop_requested; 4588c076559aSPavel Labath context.m_state = ThreadState::Stopped; 4589c076559aSPavel Labath context.m_stop_requested = false; 4590c076559aSPavel Labath 4591c076559aSPavel Labath // If we have a pending notification, remove this from the set. 4592c076559aSPavel Labath if (m_pending_notification_up) 4593c076559aSPavel Labath { 4594c076559aSPavel Labath m_pending_notification_up->wait_for_stop_tids.erase(tid); 4595c076559aSPavel Labath SignalIfRequirementsSatisfied(); 4596c076559aSPavel Labath } 4597c076559aSPavel Labath 4598c076559aSPavel Labath if (initiated_by_llgs && context.m_request_resume_function && !stop_was_requested) 4599c076559aSPavel Labath { 4600c076559aSPavel Labath // We can end up here if stop was initiated by LLGS but by this time a 4601c076559aSPavel Labath // thread stop has occurred - maybe initiated by another event. 4602c076559aSPavel Labath TSCLog ("Resuming thread %" PRIu64 " since stop wasn't requested", tid); 4603c076559aSPavel Labath const auto error = context.m_request_resume_function (tid, true); 4604c076559aSPavel Labath if (error.Success ()) 4605c076559aSPavel Labath { 4606c076559aSPavel Labath context.m_state = ThreadState::Running; 4607c076559aSPavel Labath } 4608c076559aSPavel Labath else 4609c076559aSPavel Labath { 4610c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s failed to resume thread tid %" PRIu64 ": %s", 4611c076559aSPavel Labath __FUNCTION__, tid, error.AsCString ()); 4612c076559aSPavel Labath } 4613c076559aSPavel Labath } 4614c076559aSPavel Labath } 4615c076559aSPavel Labath 4616c076559aSPavel Labath void 4617ed89c7feSPavel Labath NativeProcessLinux::DoStopThreads(PendingNotificationUP &¬ification_up) 4618c076559aSPavel Labath { 4619c076559aSPavel Labath // Validate we know about the deferred trigger thread. 4620c076559aSPavel Labath if (!IsKnownThread (notification_up->triggering_tid)) 4621c076559aSPavel Labath { 4622c076559aSPavel Labath // We don't know about this thread. This is an error condition. 4623c076559aSPavel Labath std::ostringstream error_message; 4624c076559aSPavel Labath error_message << "error: deferred notification tid " << notification_up->triggering_tid << " is unknown"; 4625c076559aSPavel Labath notification_up->error_function (error_message.str ()); 4626c076559aSPavel Labath 4627c076559aSPavel Labath // We bail out here. 4628c076559aSPavel Labath return; 4629c076559aSPavel Labath } 4630c076559aSPavel Labath 4631c076559aSPavel Labath if (m_pending_notification_up) 4632c076559aSPavel Labath { 4633c076559aSPavel Labath // Yikes - we've already got a pending signal notification in progress. 4634c076559aSPavel Labath // Log this info. We lose the pending notification here. 4635c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s dropping existing pending signal notification for tid %" PRIu64 ", to be replaced with signal for tid %" PRIu64, 4636c076559aSPavel Labath __FUNCTION__, 4637c076559aSPavel Labath m_pending_notification_up->triggering_tid, 4638c076559aSPavel Labath notification_up->triggering_tid); 4639c076559aSPavel Labath } 4640c076559aSPavel Labath m_pending_notification_up = std::move(notification_up); 4641c076559aSPavel Labath 4642c076559aSPavel Labath if (m_pending_notification_up->request_stop_on_all_unstopped_threads) 4643c076559aSPavel Labath RequestStopOnAllRunningThreads(); 4644c076559aSPavel Labath else 4645c076559aSPavel Labath { 4646c076559aSPavel Labath if (!RequestStopOnAllSpecifiedThreads()) 4647c076559aSPavel Labath return; 4648c076559aSPavel Labath } 4649c076559aSPavel Labath 4650ed89c7feSPavel Labath SignalIfRequirementsSatisfied(); 4651c076559aSPavel Labath } 4652c076559aSPavel Labath 4653c076559aSPavel Labath void 4654c076559aSPavel Labath NativeProcessLinux::ThreadWasCreated (lldb::tid_t tid, bool is_stopped, const ErrorFunction &error_function) 4655c076559aSPavel Labath { 4656c076559aSPavel Labath // Ensure we don't already know about the thread. 4657c076559aSPavel Labath auto find_it = m_tid_map.find (tid); 4658c076559aSPavel Labath if (find_it != m_tid_map.end ()) 4659c076559aSPavel Labath { 4660c076559aSPavel Labath // We already know about this thread. This is an error condition. 4661c076559aSPavel Labath std::ostringstream error_message; 4662c076559aSPavel Labath error_message << "error: notified tid " << tid << " created but we already know about this thread"; 4663c076559aSPavel Labath error_function (error_message.str ()); 4664c076559aSPavel Labath return; 4665c076559aSPavel Labath } 4666c076559aSPavel Labath 4667c076559aSPavel Labath // Add the new thread to the stop map. 4668c076559aSPavel Labath ThreadContext ctx; 4669c076559aSPavel Labath ctx.m_state = (is_stopped) ? ThreadState::Stopped : ThreadState::Running; 4670c076559aSPavel Labath m_tid_map[tid] = std::move(ctx); 4671c076559aSPavel Labath 4672c076559aSPavel Labath if (m_pending_notification_up && !is_stopped) 4673c076559aSPavel Labath { 4674c076559aSPavel Labath // We will need to wait for this new thread to stop as well before firing the 4675c076559aSPavel Labath // notification. 4676c076559aSPavel Labath m_pending_notification_up->wait_for_stop_tids.insert(tid); 4677c076559aSPavel Labath m_pending_notification_up->request_thread_stop_function(tid); 4678c076559aSPavel Labath } 4679c076559aSPavel Labath } 4680c076559aSPavel Labath 4681c076559aSPavel Labath void 4682c076559aSPavel Labath NativeProcessLinux::ThreadDidDie (lldb::tid_t tid, const ErrorFunction &error_function) 4683c076559aSPavel Labath { 4684c076559aSPavel Labath // Ensure we know about the thread. 4685c076559aSPavel Labath auto find_it = m_tid_map.find (tid); 4686c076559aSPavel Labath if (find_it == m_tid_map.end ()) 4687c076559aSPavel Labath { 4688c076559aSPavel Labath // We don't know about this thread. This is an error condition. 4689c076559aSPavel Labath std::ostringstream error_message; 4690c076559aSPavel Labath error_message << "error: notified tid " << tid << " died but tid is unknown"; 4691c076559aSPavel Labath error_function (error_message.str ()); 4692c076559aSPavel Labath return; 4693c076559aSPavel Labath } 4694c076559aSPavel Labath 4695c076559aSPavel Labath // Update the global list of known thread states. While this one is stopped, it is also dead. 4696c076559aSPavel Labath // So stop tracking it. We assume the user of this coordinator will not keep trying to add 4697c076559aSPavel Labath // dependencies on a thread after it is known to be dead. 4698c076559aSPavel Labath m_tid_map.erase (find_it); 4699c076559aSPavel Labath 4700c076559aSPavel Labath // If we have a pending notification, remove this from the set. 4701c076559aSPavel Labath if (m_pending_notification_up) 4702c076559aSPavel Labath { 4703c076559aSPavel Labath m_pending_notification_up->wait_for_stop_tids.erase(tid); 4704c076559aSPavel Labath SignalIfRequirementsSatisfied(); 4705c076559aSPavel Labath } 4706c076559aSPavel Labath } 4707c076559aSPavel Labath 4708c076559aSPavel Labath void 4709c076559aSPavel Labath NativeProcessLinux::TSCLog (const char *format, ...) 4710c076559aSPavel Labath { 4711c076559aSPavel Labath va_list args; 4712c076559aSPavel Labath va_start (args, format); 4713c076559aSPavel Labath 4714c076559aSPavel Labath m_log_function (format, args); 4715c076559aSPavel Labath 4716c076559aSPavel Labath va_end (args); 4717c076559aSPavel Labath } 4718c076559aSPavel Labath 4719c076559aSPavel Labath void 4720c076559aSPavel Labath NativeProcessLinux::NotifyThreadStop (lldb::tid_t tid, 4721c076559aSPavel Labath bool initiated_by_llgs, 4722c076559aSPavel Labath const ErrorFunction &error_function) 4723c076559aSPavel Labath { 4724c076559aSPavel Labath std::lock_guard<std::mutex> lock(m_event_mutex); 4725c076559aSPavel Labath 4726c076559aSPavel Labath if (m_log_event_processing) 4727c076559aSPavel Labath { 4728c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s about to process event: (tid: %" PRIu64 ", %sinitiated by llgs)", 4729c076559aSPavel Labath __FUNCTION__, tid, initiated_by_llgs?"":"not "); 4730c076559aSPavel Labath } 4731c076559aSPavel Labath 4732c076559aSPavel Labath ThreadDidStop (tid, initiated_by_llgs, error_function); 4733c076559aSPavel Labath 4734c076559aSPavel Labath if (m_log_event_processing) 4735c076559aSPavel Labath { 4736c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s event processing done", __FUNCTION__); 4737c076559aSPavel Labath } 4738c076559aSPavel Labath } 4739c076559aSPavel Labath 4740c076559aSPavel Labath void 4741c076559aSPavel Labath NativeProcessLinux::RequestThreadResume (lldb::tid_t tid, 4742c076559aSPavel Labath const ResumeThreadFunction &request_thread_resume_function, 4743c076559aSPavel Labath const ErrorFunction &error_function) 4744c076559aSPavel Labath { 4745c076559aSPavel Labath std::lock_guard<std::mutex> lock(m_event_mutex); 4746c076559aSPavel Labath 4747c076559aSPavel Labath if (m_log_event_processing) 4748c076559aSPavel Labath { 4749c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s about to process event: (tid: %" PRIu64 ")", 4750c076559aSPavel Labath __FUNCTION__, tid); 4751c076559aSPavel Labath } 4752c076559aSPavel Labath 4753c076559aSPavel Labath DoResume(tid, request_thread_resume_function, error_function, true); 4754c076559aSPavel Labath 4755c076559aSPavel Labath if (m_log_event_processing) 4756c076559aSPavel Labath { 4757c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s event processing done", __FUNCTION__); 4758c076559aSPavel Labath } 4759c076559aSPavel Labath } 4760c076559aSPavel Labath 4761c076559aSPavel Labath void 4762c076559aSPavel Labath NativeProcessLinux::RequestThreadResumeAsNeeded (lldb::tid_t tid, 4763c076559aSPavel Labath const ResumeThreadFunction &request_thread_resume_function, 4764c076559aSPavel Labath const ErrorFunction &error_function) 4765c076559aSPavel Labath { 4766c076559aSPavel Labath std::lock_guard<std::mutex> lock(m_event_mutex); 4767c076559aSPavel Labath 4768c076559aSPavel Labath if (m_log_event_processing) 4769c076559aSPavel Labath { 4770c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s about to process event: (tid: %" PRIu64 ")", 4771c076559aSPavel Labath __FUNCTION__, tid); 4772c076559aSPavel Labath } 4773c076559aSPavel Labath 4774c076559aSPavel Labath DoResume (tid, request_thread_resume_function, error_function, false); 4775c076559aSPavel Labath 4776c076559aSPavel Labath if (m_log_event_processing) 4777c076559aSPavel Labath { 4778c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s event processing done", __FUNCTION__); 4779c076559aSPavel Labath } 4780c076559aSPavel Labath } 4781c076559aSPavel Labath 4782c076559aSPavel Labath void 4783c076559aSPavel Labath NativeProcessLinux::NotifyThreadCreate (lldb::tid_t tid, 4784c076559aSPavel Labath bool is_stopped, 4785c076559aSPavel Labath const ErrorFunction &error_function) 4786c076559aSPavel Labath { 4787c076559aSPavel Labath std::lock_guard<std::mutex> lock(m_event_mutex); 4788c076559aSPavel Labath 4789c076559aSPavel Labath if (m_log_event_processing) 4790c076559aSPavel Labath { 4791c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s about to process event: (tid: %" PRIu64 ", is %sstopped)", 4792c076559aSPavel Labath __FUNCTION__, tid, is_stopped?"":"not "); 4793c076559aSPavel Labath } 4794c076559aSPavel Labath 4795c076559aSPavel Labath ThreadWasCreated (tid, is_stopped, error_function); 4796c076559aSPavel Labath 4797c076559aSPavel Labath if (m_log_event_processing) 4798c076559aSPavel Labath { 4799c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s event processing done", __FUNCTION__); 4800c076559aSPavel Labath } 4801c076559aSPavel Labath } 4802c076559aSPavel Labath 4803c076559aSPavel Labath void 4804c076559aSPavel Labath NativeProcessLinux::NotifyThreadDeath (lldb::tid_t tid, 4805c076559aSPavel Labath const ErrorFunction &error_function) 4806c076559aSPavel Labath { 4807c076559aSPavel Labath std::lock_guard<std::mutex> lock(m_event_mutex); 4808c076559aSPavel Labath 4809c076559aSPavel Labath if (m_log_event_processing) 4810c076559aSPavel Labath { 4811c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s about to process event: (tid: %" PRIu64 ")", __FUNCTION__, tid); 4812c076559aSPavel Labath } 4813c076559aSPavel Labath 4814c076559aSPavel Labath ThreadDidDie(tid, error_function); 4815c076559aSPavel Labath 4816c076559aSPavel Labath if (m_log_event_processing) 4817c076559aSPavel Labath { 4818c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s event processing done", __FUNCTION__); 4819c076559aSPavel Labath } 4820c076559aSPavel Labath } 4821c076559aSPavel Labath 4822c076559aSPavel Labath void 4823c076559aSPavel Labath NativeProcessLinux::ResetForExec () 4824c076559aSPavel Labath { 4825c076559aSPavel Labath std::lock_guard<std::mutex> lock(m_event_mutex); 4826c076559aSPavel Labath 4827c076559aSPavel Labath if (m_log_event_processing) 4828c076559aSPavel Labath { 4829c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s about to process event", __FUNCTION__); 4830c076559aSPavel Labath } 4831c076559aSPavel Labath 4832c076559aSPavel Labath // Clear the pending notification if there was one. 4833c076559aSPavel Labath m_pending_notification_up.reset (); 4834c076559aSPavel Labath 4835c076559aSPavel Labath // Clear the stop map - we no longer know anything about any thread state. 4836c076559aSPavel Labath // The caller is expected to reset thread states for all threads, and we 4837c076559aSPavel Labath // will assume anything we haven't heard about is running and requires a 4838c076559aSPavel Labath // stop. 4839c076559aSPavel Labath m_tid_map.clear (); 4840c076559aSPavel Labath 4841c076559aSPavel Labath if (m_log_event_processing) 4842c076559aSPavel Labath { 4843c076559aSPavel Labath TSCLog ("NativeProcessLinux::%s event processing done", __FUNCTION__); 4844c076559aSPavel Labath } 4845c076559aSPavel Labath } 4846c076559aSPavel Labath void 4847c076559aSPavel Labath NativeProcessLinux::LogEnableEventProcessing (bool enabled) 4848c076559aSPavel Labath { 4849c076559aSPavel Labath m_log_event_processing = enabled; 4850c076559aSPavel Labath } 4851c076559aSPavel Labath 4852c076559aSPavel Labath bool 4853c076559aSPavel Labath NativeProcessLinux::IsKnownThread (lldb::tid_t tid) const 4854c076559aSPavel Labath { 4855c076559aSPavel Labath return m_tid_map.find (tid) != m_tid_map.end (); 4856c076559aSPavel Labath } 4857