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/types.h> 62d858487eSTamas Berghammer #include <sys/uio.h> 63d858487eSTamas Berghammer #include <sys/user.h> 64d858487eSTamas Berghammer #include <sys/wait.h> 65d858487eSTamas Berghammer 661e209fccSTamas Berghammer #if defined (__arm64__) || defined (__aarch64__) 671e209fccSTamas Berghammer // NT_PRSTATUS and NT_FPREGSET definition 681e209fccSTamas Berghammer #include <elf.h> 691e209fccSTamas Berghammer #endif 701e209fccSTamas Berghammer 71cacde7dfSTodd Fiala #ifdef __ANDROID__ 72cacde7dfSTodd Fiala #define __ptrace_request int 73cacde7dfSTodd Fiala #define PT_DETACH PTRACE_DETACH 74cacde7dfSTodd Fiala #endif 75af245d11STodd Fiala 76af245d11STodd Fiala #define DEBUG_PTRACE_MAXBYTES 20 77af245d11STodd Fiala 78af245d11STodd Fiala // Support ptrace extensions even when compiled without required kernel support 79dda61943STodd Fiala #ifndef PT_GETREGS 80af245d11STodd Fiala #ifndef PTRACE_GETREGS 81af245d11STodd Fiala #define PTRACE_GETREGS 12 82af245d11STodd Fiala #endif 83dda61943STodd Fiala #endif 84dda61943STodd Fiala #ifndef PT_SETREGS 85af245d11STodd Fiala #ifndef PTRACE_SETREGS 86af245d11STodd Fiala #define PTRACE_SETREGS 13 87af245d11STodd Fiala #endif 88dda61943STodd Fiala #endif 89dda61943STodd Fiala #ifndef PT_GETFPREGS 90dda61943STodd Fiala #ifndef PTRACE_GETFPREGS 91dda61943STodd Fiala #define PTRACE_GETFPREGS 14 92dda61943STodd Fiala #endif 93dda61943STodd Fiala #endif 94dda61943STodd Fiala #ifndef PT_SETFPREGS 95dda61943STodd Fiala #ifndef PTRACE_SETFPREGS 96dda61943STodd Fiala #define PTRACE_SETFPREGS 15 97dda61943STodd Fiala #endif 98dda61943STodd Fiala #endif 99af245d11STodd Fiala #ifndef PTRACE_GETREGSET 100af245d11STodd Fiala #define PTRACE_GETREGSET 0x4204 101af245d11STodd Fiala #endif 102af245d11STodd Fiala #ifndef PTRACE_SETREGSET 103af245d11STodd Fiala #define PTRACE_SETREGSET 0x4205 104af245d11STodd Fiala #endif 105af245d11STodd Fiala #ifndef PTRACE_GET_THREAD_AREA 106af245d11STodd Fiala #define PTRACE_GET_THREAD_AREA 25 107af245d11STodd Fiala #endif 108af245d11STodd Fiala #ifndef PTRACE_ARCH_PRCTL 109af245d11STodd Fiala #define PTRACE_ARCH_PRCTL 30 110af245d11STodd Fiala #endif 111af245d11STodd Fiala #ifndef ARCH_GET_FS 112af245d11STodd Fiala #define ARCH_SET_GS 0x1001 113af245d11STodd Fiala #define ARCH_SET_FS 0x1002 114af245d11STodd Fiala #define ARCH_GET_FS 0x1003 115af245d11STodd Fiala #define ARCH_GET_GS 0x1004 116af245d11STodd Fiala #endif 117af245d11STodd Fiala 1180bce1b67STodd Fiala #define LLDB_PERSONALITY_GET_CURRENT_SETTINGS 0xffffffff 119af245d11STodd Fiala 120af245d11STodd Fiala // Support hardware breakpoints in case it has not been defined 121af245d11STodd Fiala #ifndef TRAP_HWBKPT 122af245d11STodd Fiala #define TRAP_HWBKPT 4 123af245d11STodd Fiala #endif 124af245d11STodd Fiala 125af245d11STodd Fiala // We disable the tracing of ptrace calls for integration builds to 126af245d11STodd Fiala // avoid the additional indirection and checks. 127af245d11STodd Fiala #ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION 12897ccc294SChaoren Lin #define PTRACE(req, pid, addr, data, data_size, error) \ 12997ccc294SChaoren Lin PtraceWrapper((req), (pid), (addr), (data), (data_size), (error), #req, __FILE__, __LINE__) 130af245d11STodd Fiala #else 13197ccc294SChaoren Lin #define PTRACE(req, pid, addr, data, data_size, error) \ 13297ccc294SChaoren Lin PtraceWrapper((req), (pid), (addr), (data), (data_size), (error)) 133af245d11STodd Fiala #endif 134af245d11STodd Fiala 1357cb18bf5STamas Berghammer using namespace lldb; 1367cb18bf5STamas Berghammer using namespace lldb_private; 137db264a6dSTamas Berghammer using namespace lldb_private::process_linux; 1387cb18bf5STamas Berghammer using namespace llvm; 1397cb18bf5STamas Berghammer 140af245d11STodd Fiala // Private bits we only need internally. 141af245d11STodd Fiala namespace 142af245d11STodd Fiala { 143af245d11STodd Fiala const UnixSignals& 144af245d11STodd Fiala GetUnixSignals () 145af245d11STodd Fiala { 146af245d11STodd Fiala static process_linux::LinuxSignals signals; 147af245d11STodd Fiala return signals; 148af245d11STodd Fiala } 149af245d11STodd Fiala 150af245d11STodd Fiala Error 151af245d11STodd Fiala ResolveProcessArchitecture (lldb::pid_t pid, Platform &platform, ArchSpec &arch) 152af245d11STodd Fiala { 153af245d11STodd Fiala // Grab process info for the running process. 154af245d11STodd Fiala ProcessInstanceInfo process_info; 155af245d11STodd Fiala if (!platform.GetProcessInfo (pid, process_info)) 156db264a6dSTamas Berghammer return Error("failed to get process info"); 157af245d11STodd Fiala 158af245d11STodd Fiala // Resolve the executable module. 159af245d11STodd Fiala ModuleSP exe_module_sp; 160e56f6dceSChaoren Lin ModuleSpec exe_module_spec(process_info.GetExecutableFile(), process_info.GetArchitecture()); 161af245d11STodd Fiala FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths ()); 162af245d11STodd Fiala Error error = platform.ResolveExecutable( 16354539338SOleksiy Vyalov exe_module_spec, 164af245d11STodd Fiala exe_module_sp, 165af245d11STodd Fiala executable_search_paths.GetSize () ? &executable_search_paths : NULL); 166af245d11STodd Fiala 167af245d11STodd Fiala if (!error.Success ()) 168af245d11STodd Fiala return error; 169af245d11STodd Fiala 170af245d11STodd Fiala // Check if we've got our architecture from the exe_module. 171af245d11STodd Fiala arch = exe_module_sp->GetArchitecture (); 172af245d11STodd Fiala if (arch.IsValid ()) 173af245d11STodd Fiala return Error(); 174af245d11STodd Fiala else 175af245d11STodd Fiala return Error("failed to retrieve a valid architecture from the exe module"); 176af245d11STodd Fiala } 177af245d11STodd Fiala 178af245d11STodd Fiala void 179db264a6dSTamas Berghammer DisplayBytes (StreamString &s, void *bytes, uint32_t count) 180af245d11STodd Fiala { 181af245d11STodd Fiala uint8_t *ptr = (uint8_t *)bytes; 182af245d11STodd Fiala const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count); 183af245d11STodd Fiala for(uint32_t i=0; i<loop_count; i++) 184af245d11STodd Fiala { 185af245d11STodd Fiala s.Printf ("[%x]", *ptr); 186af245d11STodd Fiala ptr++; 187af245d11STodd Fiala } 188af245d11STodd Fiala } 189af245d11STodd Fiala 190af245d11STodd Fiala void 191af245d11STodd Fiala PtraceDisplayBytes(int &req, void *data, size_t data_size) 192af245d11STodd Fiala { 193af245d11STodd Fiala StreamString buf; 194af245d11STodd Fiala Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet ( 195af245d11STodd Fiala POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE)); 196af245d11STodd Fiala 197af245d11STodd Fiala if (verbose_log) 198af245d11STodd Fiala { 199af245d11STodd Fiala switch(req) 200af245d11STodd Fiala { 201af245d11STodd Fiala case PTRACE_POKETEXT: 202af245d11STodd Fiala { 203af245d11STodd Fiala DisplayBytes(buf, &data, 8); 204af245d11STodd Fiala verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData()); 205af245d11STodd Fiala break; 206af245d11STodd Fiala } 207af245d11STodd Fiala case PTRACE_POKEDATA: 208af245d11STodd Fiala { 209af245d11STodd Fiala DisplayBytes(buf, &data, 8); 210af245d11STodd Fiala verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData()); 211af245d11STodd Fiala break; 212af245d11STodd Fiala } 213af245d11STodd Fiala case PTRACE_POKEUSER: 214af245d11STodd Fiala { 215af245d11STodd Fiala DisplayBytes(buf, &data, 8); 216af245d11STodd Fiala verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData()); 217af245d11STodd Fiala break; 218af245d11STodd Fiala } 219af245d11STodd Fiala case PTRACE_SETREGS: 220af245d11STodd Fiala { 221af245d11STodd Fiala DisplayBytes(buf, data, data_size); 222af245d11STodd Fiala verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData()); 223af245d11STodd Fiala break; 224af245d11STodd Fiala } 225af245d11STodd Fiala case PTRACE_SETFPREGS: 226af245d11STodd Fiala { 227af245d11STodd Fiala DisplayBytes(buf, data, data_size); 228af245d11STodd Fiala verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData()); 229af245d11STodd Fiala break; 230af245d11STodd Fiala } 231af245d11STodd Fiala case PTRACE_SETSIGINFO: 232af245d11STodd Fiala { 233af245d11STodd Fiala DisplayBytes(buf, data, sizeof(siginfo_t)); 234af245d11STodd Fiala verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData()); 235af245d11STodd Fiala break; 236af245d11STodd Fiala } 237af245d11STodd Fiala case PTRACE_SETREGSET: 238af245d11STodd Fiala { 239af245d11STodd Fiala // Extract iov_base from data, which is a pointer to the struct IOVEC 240af245d11STodd Fiala DisplayBytes(buf, *(void **)data, data_size); 241af245d11STodd Fiala verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData()); 242af245d11STodd Fiala break; 243af245d11STodd Fiala } 244af245d11STodd Fiala default: 245af245d11STodd Fiala { 246af245d11STodd Fiala } 247af245d11STodd Fiala } 248af245d11STodd Fiala } 249af245d11STodd Fiala } 250af245d11STodd Fiala 251af245d11STodd Fiala // Wrapper for ptrace to catch errors and log calls. 252af245d11STodd Fiala // Note that ptrace sets errno on error because -1 can be a valid result (i.e. for PTRACE_PEEK*) 253af245d11STodd Fiala long 25497ccc294SChaoren Lin PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, Error& error, 255af245d11STodd Fiala const char* reqName, const char* file, int line) 256af245d11STodd Fiala { 257af245d11STodd Fiala long int result; 258af245d11STodd Fiala 259af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE)); 260af245d11STodd Fiala 261af245d11STodd Fiala PtraceDisplayBytes(req, data, data_size); 262af245d11STodd Fiala 26397ccc294SChaoren Lin error.Clear(); 264af245d11STodd Fiala errno = 0; 265af245d11STodd Fiala if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) 266af245d11STodd Fiala result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), *(unsigned int *)addr, data); 267af245d11STodd Fiala else 268af245d11STodd Fiala result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), addr, data); 269af245d11STodd Fiala 27097ccc294SChaoren Lin if (result == -1) 27197ccc294SChaoren Lin error.SetErrorToErrno(); 27297ccc294SChaoren Lin 273af245d11STodd Fiala if (log) 274af245d11STodd Fiala log->Printf("ptrace(%s, %" PRIu64 ", %p, %p, %zu)=%lX called from file %s line %d", 275af245d11STodd Fiala reqName, pid, addr, data, data_size, result, file, line); 276af245d11STodd Fiala 277af245d11STodd Fiala PtraceDisplayBytes(req, data, data_size); 278af245d11STodd Fiala 27997ccc294SChaoren Lin if (log && error.GetError() != 0) 280af245d11STodd Fiala { 281af245d11STodd Fiala const char* str; 28297ccc294SChaoren Lin switch (error.GetError()) 283af245d11STodd Fiala { 284af245d11STodd Fiala case ESRCH: str = "ESRCH"; break; 285af245d11STodd Fiala case EINVAL: str = "EINVAL"; break; 286af245d11STodd Fiala case EBUSY: str = "EBUSY"; break; 287af245d11STodd Fiala case EPERM: str = "EPERM"; break; 28897ccc294SChaoren Lin default: str = error.AsCString(); 289af245d11STodd Fiala } 29097ccc294SChaoren Lin log->Printf("ptrace() failed; errno=%d (%s)", error.GetError(), str); 291af245d11STodd Fiala } 292af245d11STodd Fiala 293af245d11STodd Fiala return result; 294af245d11STodd Fiala } 295af245d11STodd Fiala 296af245d11STodd Fiala #ifdef LLDB_CONFIGURATION_BUILDANDINTEGRATION 297af245d11STodd Fiala // Wrapper for ptrace when logging is not required. 298af245d11STodd Fiala // Sets errno to 0 prior to calling ptrace. 299af245d11STodd Fiala long 30097ccc294SChaoren Lin PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, Error& error) 301af245d11STodd Fiala { 302af245d11STodd Fiala long result = 0; 30397ccc294SChaoren Lin 30497ccc294SChaoren Lin error.Clear(); 305af245d11STodd Fiala errno = 0; 306af245d11STodd Fiala if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) 307af245d11STodd Fiala result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), *(unsigned int *)addr, data); 308af245d11STodd Fiala else 309af245d11STodd Fiala result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), addr, data); 31097ccc294SChaoren Lin 31197ccc294SChaoren Lin if (result == -1) 31297ccc294SChaoren Lin error.SetErrorToErrno(); 313af245d11STodd Fiala return result; 314af245d11STodd Fiala } 315af245d11STodd Fiala #endif 316af245d11STodd Fiala 317af245d11STodd Fiala //------------------------------------------------------------------------------ 318af245d11STodd Fiala // Static implementations of NativeProcessLinux::ReadMemory and 319af245d11STodd Fiala // NativeProcessLinux::WriteMemory. This enables mutual recursion between these 320af245d11STodd Fiala // functions without needed to go thru the thread funnel. 321af245d11STodd Fiala 3223eb4b458SChaoren Lin size_t 323af245d11STodd Fiala DoReadMemory( 324af245d11STodd Fiala lldb::pid_t pid, 325af245d11STodd Fiala lldb::addr_t vm_addr, 326af245d11STodd Fiala void *buf, 3273eb4b458SChaoren Lin size_t size, 328af245d11STodd Fiala Error &error) 329af245d11STodd Fiala { 330af245d11STodd Fiala // ptrace word size is determined by the host, not the child 331af245d11STodd Fiala static const unsigned word_size = sizeof(void*); 332af245d11STodd Fiala unsigned char *dst = static_cast<unsigned char*>(buf); 3333eb4b458SChaoren Lin size_t bytes_read; 3343eb4b458SChaoren Lin size_t remainder; 335af245d11STodd Fiala long data; 336af245d11STodd Fiala 337af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); 338af245d11STodd Fiala if (log) 339af245d11STodd Fiala ProcessPOSIXLog::IncNestLevel(); 340af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) 341af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__, 342af245d11STodd Fiala pid, word_size, (void*)vm_addr, buf, size); 343af245d11STodd Fiala 344af245d11STodd Fiala assert(sizeof(data) >= word_size); 345af245d11STodd Fiala for (bytes_read = 0; bytes_read < size; bytes_read += remainder) 346af245d11STodd Fiala { 34797ccc294SChaoren Lin data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, nullptr, 0, error); 34897ccc294SChaoren Lin if (error.Fail()) 349af245d11STodd Fiala { 350af245d11STodd Fiala if (log) 351af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 352af245d11STodd Fiala return bytes_read; 353af245d11STodd Fiala } 354af245d11STodd Fiala 355af245d11STodd Fiala remainder = size - bytes_read; 356af245d11STodd Fiala remainder = remainder > word_size ? word_size : remainder; 357af245d11STodd Fiala 358af245d11STodd Fiala // Copy the data into our buffer 359af245d11STodd Fiala for (unsigned i = 0; i < remainder; ++i) 360af245d11STodd Fiala dst[i] = ((data >> i*8) & 0xFF); 361af245d11STodd Fiala 362af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && 363af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 364af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 365af245d11STodd Fiala size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 366af245d11STodd Fiala { 367af245d11STodd Fiala uintptr_t print_dst = 0; 368af245d11STodd Fiala // Format bytes from data by moving into print_dst for log output 369af245d11STodd Fiala for (unsigned i = 0; i < remainder; ++i) 370af245d11STodd Fiala print_dst |= (((data >> i*8) & 0xFF) << i*8); 371af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 372af245d11STodd Fiala (void*)vm_addr, print_dst, (unsigned long)data); 373af245d11STodd Fiala } 374af245d11STodd Fiala 375af245d11STodd Fiala vm_addr += word_size; 376af245d11STodd Fiala dst += word_size; 377af245d11STodd Fiala } 378af245d11STodd Fiala 379af245d11STodd Fiala if (log) 380af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 381af245d11STodd Fiala return bytes_read; 382af245d11STodd Fiala } 383af245d11STodd Fiala 3843eb4b458SChaoren Lin size_t 385af245d11STodd Fiala DoWriteMemory( 386af245d11STodd Fiala lldb::pid_t pid, 387af245d11STodd Fiala lldb::addr_t vm_addr, 388af245d11STodd Fiala const void *buf, 3893eb4b458SChaoren Lin size_t size, 390af245d11STodd Fiala Error &error) 391af245d11STodd Fiala { 392af245d11STodd Fiala // ptrace word size is determined by the host, not the child 393af245d11STodd Fiala static const unsigned word_size = sizeof(void*); 394af245d11STodd Fiala const unsigned char *src = static_cast<const unsigned char*>(buf); 3953eb4b458SChaoren Lin size_t bytes_written = 0; 3963eb4b458SChaoren Lin size_t remainder; 397af245d11STodd Fiala 398af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); 399af245d11STodd Fiala if (log) 400af245d11STodd Fiala ProcessPOSIXLog::IncNestLevel(); 401af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) 402af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(%" PRIu64 ", %u, %p, %p, %" PRIu64 ")", __FUNCTION__, 403af245d11STodd Fiala pid, word_size, (void*)vm_addr, buf, size); 404af245d11STodd Fiala 405af245d11STodd Fiala for (bytes_written = 0; bytes_written < size; bytes_written += remainder) 406af245d11STodd Fiala { 407af245d11STodd Fiala remainder = size - bytes_written; 408af245d11STodd Fiala remainder = remainder > word_size ? word_size : remainder; 409af245d11STodd Fiala 410af245d11STodd Fiala if (remainder == word_size) 411af245d11STodd Fiala { 412af245d11STodd Fiala unsigned long data = 0; 413af245d11STodd Fiala assert(sizeof(data) >= word_size); 414af245d11STodd Fiala for (unsigned i = 0; i < word_size; ++i) 415af245d11STodd Fiala data |= (unsigned long)src[i] << i*8; 416af245d11STodd Fiala 417af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && 418af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 419af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 420af245d11STodd Fiala size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 421af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 4221e209fccSTamas Berghammer (void*)vm_addr, *(const unsigned long*)src, data); 423af245d11STodd Fiala 42497ccc294SChaoren Lin if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0, error)) 425af245d11STodd Fiala { 426af245d11STodd Fiala if (log) 427af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 428af245d11STodd Fiala return bytes_written; 429af245d11STodd Fiala } 430af245d11STodd Fiala } 431af245d11STodd Fiala else 432af245d11STodd Fiala { 433af245d11STodd Fiala unsigned char buff[8]; 434af245d11STodd Fiala if (DoReadMemory(pid, vm_addr, 435af245d11STodd Fiala buff, word_size, error) != word_size) 436af245d11STodd Fiala { 437af245d11STodd Fiala if (log) 438af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 439af245d11STodd Fiala return bytes_written; 440af245d11STodd Fiala } 441af245d11STodd Fiala 442af245d11STodd Fiala memcpy(buff, src, remainder); 443af245d11STodd Fiala 444af245d11STodd Fiala if (DoWriteMemory(pid, vm_addr, 445af245d11STodd Fiala buff, word_size, error) != word_size) 446af245d11STodd Fiala { 447af245d11STodd Fiala if (log) 448af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 449af245d11STodd Fiala return bytes_written; 450af245d11STodd Fiala } 451af245d11STodd Fiala 452af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && 453af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 454af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 455af245d11STodd Fiala size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 456af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 4571e209fccSTamas Berghammer (void*)vm_addr, *(const unsigned long*)src, *(unsigned long*)buff); 458af245d11STodd Fiala } 459af245d11STodd Fiala 460af245d11STodd Fiala vm_addr += word_size; 461af245d11STodd Fiala src += word_size; 462af245d11STodd Fiala } 463af245d11STodd Fiala if (log) 464af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 465af245d11STodd Fiala return bytes_written; 466af245d11STodd Fiala } 467af245d11STodd Fiala 468af245d11STodd Fiala //------------------------------------------------------------------------------ 469af245d11STodd Fiala /// @class Operation 470af245d11STodd Fiala /// @brief Represents a NativeProcessLinux operation. 471af245d11STodd Fiala /// 472af245d11STodd Fiala /// Under Linux, it is not possible to ptrace() from any other thread but the 473af245d11STodd Fiala /// one that spawned or attached to the process from the start. Therefore, when 474af245d11STodd Fiala /// a NativeProcessLinux is asked to deliver or change the state of an inferior 475af245d11STodd Fiala /// process the operation must be "funneled" to a specific thread to perform the 476af245d11STodd Fiala /// task. The Operation class provides an abstract base for all services the 477af245d11STodd Fiala /// NativeProcessLinux must perform via the single virtual function Execute, thus 478af245d11STodd Fiala /// encapsulating the code that needs to run in the privileged context. 479af245d11STodd Fiala class Operation 480af245d11STodd Fiala { 481af245d11STodd Fiala public: 482af245d11STodd Fiala Operation () : m_error() { } 483af245d11STodd Fiala 484af245d11STodd Fiala virtual 485af245d11STodd Fiala ~Operation() {} 486af245d11STodd Fiala 487af245d11STodd Fiala virtual void 488af245d11STodd Fiala Execute (NativeProcessLinux *process) = 0; 489af245d11STodd Fiala 490af245d11STodd Fiala const Error & 491af245d11STodd Fiala GetError () const { return m_error; } 492af245d11STodd Fiala 493af245d11STodd Fiala protected: 494af245d11STodd Fiala Error m_error; 495af245d11STodd Fiala }; 496af245d11STodd Fiala 497af245d11STodd Fiala //------------------------------------------------------------------------------ 498af245d11STodd Fiala /// @class ReadOperation 499af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadMemory. 500af245d11STodd Fiala class ReadOperation : public Operation 501af245d11STodd Fiala { 502af245d11STodd Fiala public: 503af245d11STodd Fiala ReadOperation( 504af245d11STodd Fiala lldb::addr_t addr, 505af245d11STodd Fiala void *buff, 5063eb4b458SChaoren Lin size_t size, 5073eb4b458SChaoren Lin size_t &result) : 508af245d11STodd Fiala Operation (), 509af245d11STodd Fiala m_addr (addr), 510af245d11STodd Fiala m_buff (buff), 511af245d11STodd Fiala m_size (size), 512af245d11STodd Fiala m_result (result) 513af245d11STodd Fiala { 514af245d11STodd Fiala } 515af245d11STodd Fiala 516af245d11STodd Fiala void Execute (NativeProcessLinux *process) override; 517af245d11STodd Fiala 518af245d11STodd Fiala private: 519af245d11STodd Fiala lldb::addr_t m_addr; 520af245d11STodd Fiala void *m_buff; 5213eb4b458SChaoren Lin size_t m_size; 5223eb4b458SChaoren Lin size_t &m_result; 523af245d11STodd Fiala }; 524af245d11STodd Fiala 525af245d11STodd Fiala void 526af245d11STodd Fiala ReadOperation::Execute (NativeProcessLinux *process) 527af245d11STodd Fiala { 528af245d11STodd Fiala m_result = DoReadMemory (process->GetID (), m_addr, m_buff, m_size, m_error); 529af245d11STodd Fiala } 530af245d11STodd Fiala 531af245d11STodd Fiala //------------------------------------------------------------------------------ 532af245d11STodd Fiala /// @class WriteOperation 533af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteMemory. 534af245d11STodd Fiala class WriteOperation : public Operation 535af245d11STodd Fiala { 536af245d11STodd Fiala public: 537af245d11STodd Fiala WriteOperation( 538af245d11STodd Fiala lldb::addr_t addr, 539af245d11STodd Fiala const void *buff, 5403eb4b458SChaoren Lin size_t size, 5413eb4b458SChaoren Lin size_t &result) : 542af245d11STodd Fiala Operation (), 543af245d11STodd Fiala m_addr (addr), 544af245d11STodd Fiala m_buff (buff), 545af245d11STodd Fiala m_size (size), 546af245d11STodd Fiala m_result (result) 547af245d11STodd Fiala { 548af245d11STodd Fiala } 549af245d11STodd Fiala 550af245d11STodd Fiala void Execute (NativeProcessLinux *process) override; 551af245d11STodd Fiala 552af245d11STodd Fiala private: 553af245d11STodd Fiala lldb::addr_t m_addr; 554af245d11STodd Fiala const void *m_buff; 5553eb4b458SChaoren Lin size_t m_size; 5563eb4b458SChaoren Lin size_t &m_result; 557af245d11STodd Fiala }; 558af245d11STodd Fiala 559af245d11STodd Fiala void 560af245d11STodd Fiala WriteOperation::Execute(NativeProcessLinux *process) 561af245d11STodd Fiala { 562af245d11STodd Fiala m_result = DoWriteMemory (process->GetID (), m_addr, m_buff, m_size, m_error); 563af245d11STodd Fiala } 564af245d11STodd Fiala 565af245d11STodd Fiala //------------------------------------------------------------------------------ 566af245d11STodd Fiala /// @class ReadRegOperation 567af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadRegisterValue. 568af245d11STodd Fiala class ReadRegOperation : public Operation 569af245d11STodd Fiala { 570af245d11STodd Fiala public: 571af245d11STodd Fiala ReadRegOperation(lldb::tid_t tid, uint32_t offset, const char *reg_name, 57297ccc294SChaoren Lin RegisterValue &value) 57397ccc294SChaoren Lin : m_tid(tid), 57497ccc294SChaoren Lin m_offset(static_cast<uintptr_t> (offset)), 57597ccc294SChaoren Lin m_reg_name(reg_name), 57697ccc294SChaoren Lin m_value(value) 577af245d11STodd Fiala { } 578af245d11STodd Fiala 579d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 580af245d11STodd Fiala 581af245d11STodd Fiala private: 582af245d11STodd Fiala lldb::tid_t m_tid; 583af245d11STodd Fiala uintptr_t m_offset; 584af245d11STodd Fiala const char *m_reg_name; 585af245d11STodd Fiala RegisterValue &m_value; 586af245d11STodd Fiala }; 587af245d11STodd Fiala 588af245d11STodd Fiala void 589af245d11STodd Fiala ReadRegOperation::Execute(NativeProcessLinux *monitor) 590af245d11STodd Fiala { 5910fceef80STodd Fiala #if defined (__arm64__) || defined (__aarch64__) 5920fceef80STodd Fiala if (m_offset > sizeof(struct user_pt_regs)) 5930fceef80STodd Fiala { 5940fceef80STodd Fiala uintptr_t offset = m_offset - sizeof(struct user_pt_regs); 5950fceef80STodd Fiala if (offset > sizeof(struct user_fpsimd_state)) 5960fceef80STodd Fiala { 59797ccc294SChaoren Lin m_error.SetErrorString("invalid offset value"); 59897ccc294SChaoren Lin return; 5990fceef80STodd Fiala } 6000fceef80STodd Fiala elf_fpregset_t regs; 6010fceef80STodd Fiala int regset = NT_FPREGSET; 6020fceef80STodd Fiala struct iovec ioVec; 6030fceef80STodd Fiala 6040fceef80STodd Fiala ioVec.iov_base = ®s; 6050fceef80STodd Fiala ioVec.iov_len = sizeof regs; 60697ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 60797ccc294SChaoren Lin if (m_error.Success()) 6080fceef80STodd Fiala { 609db264a6dSTamas Berghammer ArchSpec arch; 6100fceef80STodd Fiala if (monitor->GetArchitecture(arch)) 6110fceef80STodd Fiala m_value.SetBytes((void *)(((unsigned char *)(®s)) + offset), 16, arch.GetByteOrder()); 6120fceef80STodd Fiala else 61397ccc294SChaoren Lin m_error.SetErrorString("failed to get architecture"); 6140fceef80STodd Fiala } 6150fceef80STodd Fiala } 6160fceef80STodd Fiala else 6170fceef80STodd Fiala { 6180fceef80STodd Fiala elf_gregset_t regs; 6190fceef80STodd Fiala int regset = NT_PRSTATUS; 6200fceef80STodd Fiala struct iovec ioVec; 6210fceef80STodd Fiala 6220fceef80STodd Fiala ioVec.iov_base = ®s; 6230fceef80STodd Fiala ioVec.iov_len = sizeof regs; 62497ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 62597ccc294SChaoren Lin if (m_error.Success()) 6260fceef80STodd Fiala { 627db264a6dSTamas Berghammer ArchSpec arch; 6280fceef80STodd Fiala if (monitor->GetArchitecture(arch)) 6290fceef80STodd Fiala m_value.SetBytes((void *)(((unsigned char *)(regs)) + m_offset), 8, arch.GetByteOrder()); 63097ccc294SChaoren Lin else 63197ccc294SChaoren Lin m_error.SetErrorString("failed to get architecture"); 6320fceef80STodd Fiala } 6330fceef80STodd Fiala } 63409ba1a32SMohit K. Bhakkad #elif defined (__mips__) 63509ba1a32SMohit K. Bhakkad elf_gregset_t regs; 63609ba1a32SMohit K. Bhakkad PTRACE(PTRACE_GETREGS, m_tid, NULL, ®s, sizeof regs, m_error); 63709ba1a32SMohit K. Bhakkad if (m_error.Success()) 63809ba1a32SMohit K. Bhakkad { 63909ba1a32SMohit K. Bhakkad lldb_private::ArchSpec arch; 64009ba1a32SMohit K. Bhakkad if (monitor->GetArchitecture(arch)) 64109ba1a32SMohit K. Bhakkad m_value.SetBytes((void *)(((unsigned char *)(regs)) + m_offset), 8, arch.GetByteOrder()); 64209ba1a32SMohit K. Bhakkad else 64309ba1a32SMohit K. Bhakkad m_error.SetErrorString("failed to get architecture"); 64409ba1a32SMohit K. Bhakkad } 6450fceef80STodd Fiala #else 646af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS)); 647af245d11STodd Fiala 648adf8adbdSTamas Berghammer lldb::addr_t data = static_cast<unsigned long>(PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, nullptr, 0, m_error)); 64997ccc294SChaoren Lin if (m_error.Success()) 650af245d11STodd Fiala m_value = data; 65197ccc294SChaoren Lin 652af245d11STodd Fiala if (log) 653af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() reg %s: 0x%" PRIx64, __FUNCTION__, 654af245d11STodd Fiala m_reg_name, data); 6550fceef80STodd Fiala #endif 656af245d11STodd Fiala } 657af245d11STodd Fiala 658af245d11STodd Fiala //------------------------------------------------------------------------------ 659af245d11STodd Fiala /// @class WriteRegOperation 660af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteRegisterValue. 661af245d11STodd Fiala class WriteRegOperation : public Operation 662af245d11STodd Fiala { 663af245d11STodd Fiala public: 664af245d11STodd Fiala WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name, 66597ccc294SChaoren Lin const RegisterValue &value) 66697ccc294SChaoren Lin : m_tid(tid), 66797ccc294SChaoren Lin m_offset(offset), 66897ccc294SChaoren Lin m_reg_name(reg_name), 66997ccc294SChaoren Lin m_value(value) 670af245d11STodd Fiala { } 671af245d11STodd Fiala 672d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 673af245d11STodd Fiala 674af245d11STodd Fiala private: 675af245d11STodd Fiala lldb::tid_t m_tid; 676af245d11STodd Fiala uintptr_t m_offset; 677af245d11STodd Fiala const char *m_reg_name; 678af245d11STodd Fiala const RegisterValue &m_value; 679af245d11STodd Fiala }; 680af245d11STodd Fiala 681af245d11STodd Fiala void 682af245d11STodd Fiala WriteRegOperation::Execute(NativeProcessLinux *monitor) 683af245d11STodd Fiala { 6840fceef80STodd Fiala #if defined (__arm64__) || defined (__aarch64__) 6850fceef80STodd Fiala if (m_offset > sizeof(struct user_pt_regs)) 6860fceef80STodd Fiala { 6870fceef80STodd Fiala uintptr_t offset = m_offset - sizeof(struct user_pt_regs); 6880fceef80STodd Fiala if (offset > sizeof(struct user_fpsimd_state)) 6890fceef80STodd Fiala { 69097ccc294SChaoren Lin m_error.SetErrorString("invalid offset value"); 69197ccc294SChaoren Lin return; 6920fceef80STodd Fiala } 6930fceef80STodd Fiala elf_fpregset_t regs; 6940fceef80STodd Fiala int regset = NT_FPREGSET; 6950fceef80STodd Fiala struct iovec ioVec; 6960fceef80STodd Fiala 6970fceef80STodd Fiala ioVec.iov_base = ®s; 6980fceef80STodd Fiala ioVec.iov_len = sizeof regs; 69997ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 7009425b329SBhushan D. Attarde if (m_error.Success()) 7010fceef80STodd Fiala { 7020fceef80STodd Fiala ::memcpy((void *)(((unsigned char *)(®s)) + offset), m_value.GetBytes(), 16); 70397ccc294SChaoren Lin PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 7040fceef80STodd Fiala } 7050fceef80STodd Fiala } 7060fceef80STodd Fiala else 7070fceef80STodd Fiala { 7080fceef80STodd Fiala elf_gregset_t regs; 7090fceef80STodd Fiala int regset = NT_PRSTATUS; 7100fceef80STodd Fiala struct iovec ioVec; 7110fceef80STodd Fiala 7120fceef80STodd Fiala ioVec.iov_base = ®s; 7130fceef80STodd Fiala ioVec.iov_len = sizeof regs; 71497ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 7159425b329SBhushan D. Attarde if (m_error.Success()) 7160fceef80STodd Fiala { 7170fceef80STodd Fiala ::memcpy((void *)(((unsigned char *)(®s)) + m_offset), m_value.GetBytes(), 8); 71897ccc294SChaoren Lin PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 7190fceef80STodd Fiala } 7200fceef80STodd Fiala } 72109ba1a32SMohit K. Bhakkad #elif defined (__mips__) 72209ba1a32SMohit K. Bhakkad elf_gregset_t regs; 72309ba1a32SMohit K. Bhakkad PTRACE(PTRACE_GETREGS, m_tid, NULL, ®s, sizeof regs, m_error); 72409ba1a32SMohit K. Bhakkad if (m_error.Success()) 72509ba1a32SMohit K. Bhakkad { 72609ba1a32SMohit K. Bhakkad ::memcpy((void *)(((unsigned char *)(®s)) + m_offset), m_value.GetBytes(), 8); 72709ba1a32SMohit K. Bhakkad PTRACE(PTRACE_SETREGS, m_tid, NULL, ®s, sizeof regs, m_error); 72809ba1a32SMohit K. Bhakkad } 7290fceef80STodd Fiala #else 730af245d11STodd Fiala void* buf; 731af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS)); 732af245d11STodd Fiala 733af245d11STodd Fiala buf = (void*) m_value.GetAsUInt64(); 734af245d11STodd Fiala 735af245d11STodd Fiala if (log) 736af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf); 73797ccc294SChaoren Lin PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0, m_error); 7380fceef80STodd Fiala #endif 739af245d11STodd Fiala } 740af245d11STodd Fiala 741af245d11STodd Fiala //------------------------------------------------------------------------------ 742af245d11STodd Fiala /// @class ReadGPROperation 743af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadGPR. 744af245d11STodd Fiala class ReadGPROperation : public Operation 745af245d11STodd Fiala { 746af245d11STodd Fiala public: 74797ccc294SChaoren Lin ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size) 74897ccc294SChaoren Lin : m_tid(tid), m_buf(buf), m_buf_size(buf_size) 749af245d11STodd Fiala { } 750af245d11STodd Fiala 751d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 752af245d11STodd Fiala 753af245d11STodd Fiala private: 754af245d11STodd Fiala lldb::tid_t m_tid; 755af245d11STodd Fiala void *m_buf; 756af245d11STodd Fiala size_t m_buf_size; 757af245d11STodd Fiala }; 758af245d11STodd Fiala 759af245d11STodd Fiala void 760af245d11STodd Fiala ReadGPROperation::Execute(NativeProcessLinux *monitor) 761af245d11STodd Fiala { 7626ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 7636ac1be4bSTodd Fiala int regset = NT_PRSTATUS; 7646ac1be4bSTodd Fiala struct iovec ioVec; 7656ac1be4bSTodd Fiala 7666ac1be4bSTodd Fiala ioVec.iov_base = m_buf; 7676ac1be4bSTodd Fiala ioVec.iov_len = m_buf_size; 76897ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); 7696ac1be4bSTodd Fiala #else 77097ccc294SChaoren Lin PTRACE(PTRACE_GETREGS, m_tid, nullptr, m_buf, m_buf_size, m_error); 7716ac1be4bSTodd Fiala #endif 772af245d11STodd Fiala } 773af245d11STodd Fiala 774af245d11STodd Fiala //------------------------------------------------------------------------------ 775af245d11STodd Fiala /// @class ReadFPROperation 776af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadFPR. 777af245d11STodd Fiala class ReadFPROperation : public Operation 778af245d11STodd Fiala { 779af245d11STodd Fiala public: 78097ccc294SChaoren Lin ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size) 78197ccc294SChaoren Lin : m_tid(tid), 78297ccc294SChaoren Lin m_buf(buf), 78397ccc294SChaoren Lin m_buf_size(buf_size) 784af245d11STodd Fiala { } 785af245d11STodd Fiala 786d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 787af245d11STodd Fiala 788af245d11STodd Fiala private: 789af245d11STodd Fiala lldb::tid_t m_tid; 790af245d11STodd Fiala void *m_buf; 791af245d11STodd Fiala size_t m_buf_size; 792af245d11STodd Fiala }; 793af245d11STodd Fiala 794af245d11STodd Fiala void 795af245d11STodd Fiala ReadFPROperation::Execute(NativeProcessLinux *monitor) 796af245d11STodd Fiala { 7976ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 7986ac1be4bSTodd Fiala int regset = NT_FPREGSET; 7996ac1be4bSTodd Fiala struct iovec ioVec; 8006ac1be4bSTodd Fiala 8016ac1be4bSTodd Fiala ioVec.iov_base = m_buf; 8026ac1be4bSTodd Fiala ioVec.iov_len = m_buf_size; 8031e209fccSTamas Berghammer PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); 8046ac1be4bSTodd Fiala #else 80597ccc294SChaoren Lin PTRACE(PTRACE_GETFPREGS, m_tid, nullptr, m_buf, m_buf_size, m_error); 8066ac1be4bSTodd Fiala #endif 807af245d11STodd Fiala } 808af245d11STodd Fiala 809af245d11STodd Fiala //------------------------------------------------------------------------------ 810af245d11STodd Fiala /// @class ReadRegisterSetOperation 811af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadRegisterSet. 812af245d11STodd Fiala class ReadRegisterSetOperation : public Operation 813af245d11STodd Fiala { 814af245d11STodd Fiala public: 81597ccc294SChaoren Lin ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) 81697ccc294SChaoren Lin : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset) 817af245d11STodd Fiala { } 818af245d11STodd Fiala 819d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 820af245d11STodd Fiala 821af245d11STodd Fiala private: 822af245d11STodd Fiala lldb::tid_t m_tid; 823af245d11STodd Fiala void *m_buf; 824af245d11STodd Fiala size_t m_buf_size; 825af245d11STodd Fiala const unsigned int m_regset; 826af245d11STodd Fiala }; 827af245d11STodd Fiala 828af245d11STodd Fiala void 829af245d11STodd Fiala ReadRegisterSetOperation::Execute(NativeProcessLinux *monitor) 830af245d11STodd Fiala { 83197ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size, m_error); 832af245d11STodd Fiala } 833af245d11STodd Fiala 834af245d11STodd Fiala //------------------------------------------------------------------------------ 835af245d11STodd Fiala /// @class WriteGPROperation 836af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteGPR. 837af245d11STodd Fiala class WriteGPROperation : public Operation 838af245d11STodd Fiala { 839af245d11STodd Fiala public: 84097ccc294SChaoren Lin WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size) 84197ccc294SChaoren Lin : m_tid(tid), m_buf(buf), m_buf_size(buf_size) 842af245d11STodd Fiala { } 843af245d11STodd Fiala 844d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 845af245d11STodd Fiala 846af245d11STodd Fiala private: 847af245d11STodd Fiala lldb::tid_t m_tid; 848af245d11STodd Fiala void *m_buf; 849af245d11STodd Fiala size_t m_buf_size; 850af245d11STodd Fiala }; 851af245d11STodd Fiala 852af245d11STodd Fiala void 853af245d11STodd Fiala WriteGPROperation::Execute(NativeProcessLinux *monitor) 854af245d11STodd Fiala { 8556ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 8566ac1be4bSTodd Fiala int regset = NT_PRSTATUS; 8576ac1be4bSTodd Fiala struct iovec ioVec; 8586ac1be4bSTodd Fiala 8596ac1be4bSTodd Fiala ioVec.iov_base = m_buf; 8606ac1be4bSTodd Fiala ioVec.iov_len = m_buf_size; 86197ccc294SChaoren Lin PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); 8626ac1be4bSTodd Fiala #else 86397ccc294SChaoren Lin PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size, m_error); 8646ac1be4bSTodd Fiala #endif 865af245d11STodd Fiala } 866af245d11STodd Fiala 867af245d11STodd Fiala //------------------------------------------------------------------------------ 868af245d11STodd Fiala /// @class WriteFPROperation 869af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteFPR. 870af245d11STodd Fiala class WriteFPROperation : public Operation 871af245d11STodd Fiala { 872af245d11STodd Fiala public: 87397ccc294SChaoren Lin WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size) 87497ccc294SChaoren Lin : m_tid(tid), m_buf(buf), m_buf_size(buf_size) 875af245d11STodd Fiala { } 876af245d11STodd Fiala 877d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 878af245d11STodd Fiala 879af245d11STodd Fiala private: 880af245d11STodd Fiala lldb::tid_t m_tid; 881af245d11STodd Fiala void *m_buf; 882af245d11STodd Fiala size_t m_buf_size; 883af245d11STodd Fiala }; 884af245d11STodd Fiala 885af245d11STodd Fiala void 886af245d11STodd Fiala WriteFPROperation::Execute(NativeProcessLinux *monitor) 887af245d11STodd Fiala { 8886ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 8896ac1be4bSTodd Fiala int regset = NT_FPREGSET; 8906ac1be4bSTodd Fiala struct iovec ioVec; 8916ac1be4bSTodd Fiala 8926ac1be4bSTodd Fiala ioVec.iov_base = m_buf; 8936ac1be4bSTodd Fiala ioVec.iov_len = m_buf_size; 89497ccc294SChaoren Lin PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); 8956ac1be4bSTodd Fiala #else 89697ccc294SChaoren Lin PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size, m_error); 8976ac1be4bSTodd Fiala #endif 898af245d11STodd Fiala } 899af245d11STodd Fiala 900af245d11STodd Fiala //------------------------------------------------------------------------------ 901af245d11STodd Fiala /// @class WriteRegisterSetOperation 902af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteRegisterSet. 903af245d11STodd Fiala class WriteRegisterSetOperation : public Operation 904af245d11STodd Fiala { 905af245d11STodd Fiala public: 90697ccc294SChaoren Lin WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) 90797ccc294SChaoren Lin : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset) 908af245d11STodd Fiala { } 909af245d11STodd Fiala 910d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 911af245d11STodd Fiala 912af245d11STodd Fiala private: 913af245d11STodd Fiala lldb::tid_t m_tid; 914af245d11STodd Fiala void *m_buf; 915af245d11STodd Fiala size_t m_buf_size; 916af245d11STodd Fiala const unsigned int m_regset; 917af245d11STodd Fiala }; 918af245d11STodd Fiala 919af245d11STodd Fiala void 920af245d11STodd Fiala WriteRegisterSetOperation::Execute(NativeProcessLinux *monitor) 921af245d11STodd Fiala { 92297ccc294SChaoren Lin PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size, m_error); 923af245d11STodd Fiala } 924af245d11STodd Fiala 925af245d11STodd Fiala //------------------------------------------------------------------------------ 926af245d11STodd Fiala /// @class ResumeOperation 927af245d11STodd Fiala /// @brief Implements NativeProcessLinux::Resume. 928af245d11STodd Fiala class ResumeOperation : public Operation 929af245d11STodd Fiala { 930af245d11STodd Fiala public: 93197ccc294SChaoren Lin ResumeOperation(lldb::tid_t tid, uint32_t signo) : 93297ccc294SChaoren Lin m_tid(tid), m_signo(signo) { } 933af245d11STodd Fiala 934d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 935af245d11STodd Fiala 936af245d11STodd Fiala private: 937af245d11STodd Fiala lldb::tid_t m_tid; 938af245d11STodd Fiala uint32_t m_signo; 939af245d11STodd Fiala }; 940af245d11STodd Fiala 941af245d11STodd Fiala void 942af245d11STodd Fiala ResumeOperation::Execute(NativeProcessLinux *monitor) 943af245d11STodd Fiala { 944af245d11STodd Fiala intptr_t data = 0; 945af245d11STodd Fiala 946af245d11STodd Fiala if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) 947af245d11STodd Fiala data = m_signo; 948af245d11STodd Fiala 94997ccc294SChaoren Lin PTRACE(PTRACE_CONT, m_tid, nullptr, (void*)data, 0, m_error); 95097ccc294SChaoren Lin if (m_error.Fail()) 951af245d11STodd Fiala { 952af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 953af245d11STodd Fiala 954af245d11STodd Fiala if (log) 95597ccc294SChaoren Lin log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, m_error.AsCString()); 956af245d11STodd Fiala } 957af245d11STodd Fiala } 958af245d11STodd Fiala 959af245d11STodd Fiala //------------------------------------------------------------------------------ 960af245d11STodd Fiala /// @class SingleStepOperation 961af245d11STodd Fiala /// @brief Implements NativeProcessLinux::SingleStep. 962af245d11STodd Fiala class SingleStepOperation : public Operation 963af245d11STodd Fiala { 964af245d11STodd Fiala public: 96597ccc294SChaoren Lin SingleStepOperation(lldb::tid_t tid, uint32_t signo) 96697ccc294SChaoren Lin : m_tid(tid), m_signo(signo) { } 967af245d11STodd Fiala 968d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 969af245d11STodd Fiala 970af245d11STodd Fiala private: 971af245d11STodd Fiala lldb::tid_t m_tid; 972af245d11STodd Fiala uint32_t m_signo; 973af245d11STodd Fiala }; 974af245d11STodd Fiala 975af245d11STodd Fiala void 976af245d11STodd Fiala SingleStepOperation::Execute(NativeProcessLinux *monitor) 977af245d11STodd Fiala { 978af245d11STodd Fiala intptr_t data = 0; 979af245d11STodd Fiala 980af245d11STodd Fiala if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) 981af245d11STodd Fiala data = m_signo; 982af245d11STodd Fiala 98397ccc294SChaoren Lin PTRACE(PTRACE_SINGLESTEP, m_tid, nullptr, (void*)data, 0, m_error); 984af245d11STodd Fiala } 985af245d11STodd Fiala 986af245d11STodd Fiala //------------------------------------------------------------------------------ 987af245d11STodd Fiala /// @class SiginfoOperation 988af245d11STodd Fiala /// @brief Implements NativeProcessLinux::GetSignalInfo. 989af245d11STodd Fiala class SiginfoOperation : public Operation 990af245d11STodd Fiala { 991af245d11STodd Fiala public: 99297ccc294SChaoren Lin SiginfoOperation(lldb::tid_t tid, void *info) 99397ccc294SChaoren Lin : m_tid(tid), m_info(info) { } 994af245d11STodd Fiala 995d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 996af245d11STodd Fiala 997af245d11STodd Fiala private: 998af245d11STodd Fiala lldb::tid_t m_tid; 999af245d11STodd Fiala void *m_info; 1000af245d11STodd Fiala }; 1001af245d11STodd Fiala 1002af245d11STodd Fiala void 1003af245d11STodd Fiala SiginfoOperation::Execute(NativeProcessLinux *monitor) 1004af245d11STodd Fiala { 100597ccc294SChaoren Lin PTRACE(PTRACE_GETSIGINFO, m_tid, nullptr, m_info, 0, m_error); 1006af245d11STodd Fiala } 1007af245d11STodd Fiala 1008af245d11STodd Fiala //------------------------------------------------------------------------------ 1009af245d11STodd Fiala /// @class EventMessageOperation 1010af245d11STodd Fiala /// @brief Implements NativeProcessLinux::GetEventMessage. 1011af245d11STodd Fiala class EventMessageOperation : public Operation 1012af245d11STodd Fiala { 1013af245d11STodd Fiala public: 101497ccc294SChaoren Lin EventMessageOperation(lldb::tid_t tid, unsigned long *message) 101597ccc294SChaoren Lin : m_tid(tid), m_message(message) { } 1016af245d11STodd Fiala 1017d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 1018af245d11STodd Fiala 1019af245d11STodd Fiala private: 1020af245d11STodd Fiala lldb::tid_t m_tid; 1021af245d11STodd Fiala unsigned long *m_message; 1022af245d11STodd Fiala }; 1023af245d11STodd Fiala 1024af245d11STodd Fiala void 1025af245d11STodd Fiala EventMessageOperation::Execute(NativeProcessLinux *monitor) 1026af245d11STodd Fiala { 102797ccc294SChaoren Lin PTRACE(PTRACE_GETEVENTMSG, m_tid, nullptr, m_message, 0, m_error); 1028af245d11STodd Fiala } 1029af245d11STodd Fiala 1030af245d11STodd Fiala class DetachOperation : public Operation 1031af245d11STodd Fiala { 1032af245d11STodd Fiala public: 103397ccc294SChaoren Lin DetachOperation(lldb::tid_t tid) : m_tid(tid) { } 1034af245d11STodd Fiala 1035d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 1036af245d11STodd Fiala 1037af245d11STodd Fiala private: 1038af245d11STodd Fiala lldb::tid_t m_tid; 1039af245d11STodd Fiala }; 1040af245d11STodd Fiala 1041af245d11STodd Fiala void 1042af245d11STodd Fiala DetachOperation::Execute(NativeProcessLinux *monitor) 1043af245d11STodd Fiala { 104497ccc294SChaoren Lin PTRACE(PTRACE_DETACH, m_tid, nullptr, 0, 0, m_error); 1045af245d11STodd Fiala } 10461107b5a5SPavel Labath } // end of anonymous namespace 10471107b5a5SPavel Labath 1048bd7cbc5aSPavel Labath // Simple helper function to ensure flags are enabled on the given file 1049bd7cbc5aSPavel Labath // descriptor. 1050bd7cbc5aSPavel Labath static Error 1051bd7cbc5aSPavel Labath EnsureFDFlags(int fd, int flags) 1052bd7cbc5aSPavel Labath { 1053bd7cbc5aSPavel Labath Error error; 1054bd7cbc5aSPavel Labath 1055bd7cbc5aSPavel Labath int status = fcntl(fd, F_GETFL); 1056bd7cbc5aSPavel Labath if (status == -1) 1057bd7cbc5aSPavel Labath { 1058bd7cbc5aSPavel Labath error.SetErrorToErrno(); 1059bd7cbc5aSPavel Labath return error; 1060bd7cbc5aSPavel Labath } 1061bd7cbc5aSPavel Labath 1062bd7cbc5aSPavel Labath if (fcntl(fd, F_SETFL, status | flags) == -1) 1063bd7cbc5aSPavel Labath { 1064bd7cbc5aSPavel Labath error.SetErrorToErrno(); 1065bd7cbc5aSPavel Labath return error; 1066bd7cbc5aSPavel Labath } 1067bd7cbc5aSPavel Labath 1068bd7cbc5aSPavel Labath return error; 1069bd7cbc5aSPavel Labath } 1070bd7cbc5aSPavel Labath 1071bd7cbc5aSPavel Labath // This class encapsulates the privileged thread which performs all ptrace and wait operations on 1072bd7cbc5aSPavel Labath // the inferior. The thread consists of a main loop which waits for events and processes them 1073bd7cbc5aSPavel Labath // - SIGCHLD (delivered over a signalfd file descriptor): These signals notify us of events in 1074bd7cbc5aSPavel Labath // the inferior process. Upon receiving this signal we do a waitpid to get more information 1075bd7cbc5aSPavel Labath // and dispatch to NativeProcessLinux::MonitorCallback. 1076bd7cbc5aSPavel Labath // - requests for ptrace operations: These initiated via the DoOperation method, which funnels 1077bd7cbc5aSPavel Labath // them to the Monitor thread via m_operation member. The Monitor thread is signaled over a 1078bd7cbc5aSPavel Labath // pipe, and the completion of the operation is signalled over the semaphore. 1079bd7cbc5aSPavel Labath // - thread exit event: this is signaled from the Monitor destructor by closing the write end 1080bd7cbc5aSPavel Labath // of the command pipe. 108145f5cb31SPavel Labath class NativeProcessLinux::Monitor 108245f5cb31SPavel Labath { 10831107b5a5SPavel Labath private: 1084bd7cbc5aSPavel Labath // The initial monitor operation (launch or attach). It returns a inferior process id. 1085bd7cbc5aSPavel Labath std::unique_ptr<InitialOperation> m_initial_operation_up; 1086bd7cbc5aSPavel Labath 1087bd7cbc5aSPavel Labath ::pid_t m_child_pid = -1; 10881107b5a5SPavel Labath NativeProcessLinux * m_native_process; 10891107b5a5SPavel Labath 10901107b5a5SPavel Labath enum { READ, WRITE }; 10911107b5a5SPavel Labath int m_pipefd[2] = {-1, -1}; 10921107b5a5SPavel Labath int m_signal_fd = -1; 10931107b5a5SPavel Labath HostThread m_thread; 10941107b5a5SPavel Labath 1095bd7cbc5aSPavel Labath // current operation which must be executed on the priviliged thread 1096bd7cbc5aSPavel Labath Mutex m_operation_mutex; 1097bd7cbc5aSPavel Labath Operation *m_operation = nullptr; 1098bd7cbc5aSPavel Labath sem_t m_operation_sem; 1099bd7cbc5aSPavel Labath Error m_operation_error; 1100bd7cbc5aSPavel Labath 110145f5cb31SPavel Labath unsigned m_operation_nesting_level = 0; 110245f5cb31SPavel Labath 1103bd7cbc5aSPavel Labath static constexpr char operation_command = 'o'; 110445f5cb31SPavel Labath static constexpr char begin_block_command = '{'; 110545f5cb31SPavel Labath static constexpr char end_block_command = '}'; 1106bd7cbc5aSPavel Labath 11071107b5a5SPavel Labath void 11081107b5a5SPavel Labath HandleSignals(); 11091107b5a5SPavel Labath 11101107b5a5SPavel Labath void 11111107b5a5SPavel Labath HandleWait(); 11121107b5a5SPavel Labath 11131107b5a5SPavel Labath // Returns true if the thread should exit. 11141107b5a5SPavel Labath bool 11151107b5a5SPavel Labath HandleCommands(); 11161107b5a5SPavel Labath 11171107b5a5SPavel Labath void 11181107b5a5SPavel Labath MainLoop(); 11191107b5a5SPavel Labath 11201107b5a5SPavel Labath static void * 11211107b5a5SPavel Labath RunMonitor(void *arg); 11221107b5a5SPavel Labath 1123bd7cbc5aSPavel Labath Error 112445f5cb31SPavel Labath WaitForAck(); 112545f5cb31SPavel Labath 112645f5cb31SPavel Labath void 112745f5cb31SPavel Labath BeginOperationBlock() 112845f5cb31SPavel Labath { 112945f5cb31SPavel Labath write(m_pipefd[WRITE], &begin_block_command, sizeof operation_command); 113045f5cb31SPavel Labath WaitForAck(); 113145f5cb31SPavel Labath } 113245f5cb31SPavel Labath 113345f5cb31SPavel Labath void 113445f5cb31SPavel Labath EndOperationBlock() 113545f5cb31SPavel Labath { 113645f5cb31SPavel Labath write(m_pipefd[WRITE], &end_block_command, sizeof operation_command); 113745f5cb31SPavel Labath WaitForAck(); 113845f5cb31SPavel Labath } 113945f5cb31SPavel Labath 11401107b5a5SPavel Labath public: 1141bd7cbc5aSPavel Labath Monitor(const InitialOperation &initial_operation, 1142bd7cbc5aSPavel Labath NativeProcessLinux *native_process) 1143bd7cbc5aSPavel Labath : m_initial_operation_up(new InitialOperation(initial_operation)), 1144bd7cbc5aSPavel Labath m_native_process(native_process) 1145bd7cbc5aSPavel Labath { 1146bd7cbc5aSPavel Labath sem_init(&m_operation_sem, 0, 0); 1147bd7cbc5aSPavel Labath } 11481107b5a5SPavel Labath 11491107b5a5SPavel Labath ~Monitor(); 11501107b5a5SPavel Labath 11511107b5a5SPavel Labath Error 11521107b5a5SPavel Labath Initialize(); 1153bd7cbc5aSPavel Labath 1154bd7cbc5aSPavel Labath void 115545f5cb31SPavel Labath Terminate(); 115645f5cb31SPavel Labath 115745f5cb31SPavel Labath void 1158bd7cbc5aSPavel Labath DoOperation(Operation *op); 115945f5cb31SPavel Labath 116045f5cb31SPavel Labath class ScopedOperationLock { 116145f5cb31SPavel Labath Monitor &m_monitor; 116245f5cb31SPavel Labath 116345f5cb31SPavel Labath public: 116445f5cb31SPavel Labath ScopedOperationLock(Monitor &monitor) 116545f5cb31SPavel Labath : m_monitor(monitor) 116645f5cb31SPavel Labath { m_monitor.BeginOperationBlock(); } 116745f5cb31SPavel Labath 116845f5cb31SPavel Labath ~ScopedOperationLock() 116945f5cb31SPavel Labath { m_monitor.EndOperationBlock(); } 117045f5cb31SPavel Labath }; 11711107b5a5SPavel Labath }; 1172bd7cbc5aSPavel Labath constexpr char NativeProcessLinux::Monitor::operation_command; 117345f5cb31SPavel Labath constexpr char NativeProcessLinux::Monitor::begin_block_command; 117445f5cb31SPavel Labath constexpr char NativeProcessLinux::Monitor::end_block_command; 11751107b5a5SPavel Labath 11761107b5a5SPavel Labath Error 11771107b5a5SPavel Labath NativeProcessLinux::Monitor::Initialize() 11781107b5a5SPavel Labath { 11791107b5a5SPavel Labath Error error; 11801107b5a5SPavel Labath 11811107b5a5SPavel Labath // We get a SIGCHLD every time something interesting happens with the inferior. We shall be 11821107b5a5SPavel Labath // listening for these signals over a signalfd file descriptors. This allows us to wait for 11831107b5a5SPavel Labath // multiple kinds of events with select. 11841107b5a5SPavel Labath sigset_t signals; 11851107b5a5SPavel Labath sigemptyset(&signals); 11861107b5a5SPavel Labath sigaddset(&signals, SIGCHLD); 11871107b5a5SPavel Labath m_signal_fd = signalfd(-1, &signals, SFD_NONBLOCK | SFD_CLOEXEC); 11881107b5a5SPavel Labath if (m_signal_fd < 0) 11891107b5a5SPavel Labath { 11901107b5a5SPavel Labath return Error("NativeProcessLinux::Monitor::%s failed due to signalfd failure. Monitoring the inferior will be impossible: %s", 11911107b5a5SPavel Labath __FUNCTION__, strerror(errno)); 11921107b5a5SPavel Labath 1193af245d11STodd Fiala } 1194af245d11STodd Fiala 11951107b5a5SPavel Labath if (pipe2(m_pipefd, O_CLOEXEC) == -1) 11961107b5a5SPavel Labath { 11971107b5a5SPavel Labath error.SetErrorToErrno(); 11981107b5a5SPavel Labath return error; 11991107b5a5SPavel Labath } 12001107b5a5SPavel Labath 1201bd7cbc5aSPavel Labath if ((error = EnsureFDFlags(m_pipefd[READ], O_NONBLOCK)).Fail()) { 1202bd7cbc5aSPavel Labath return error; 1203bd7cbc5aSPavel Labath } 1204bd7cbc5aSPavel Labath 1205bd7cbc5aSPavel Labath static const char g_thread_name[] = "lldb.process.nativelinux.monitor"; 1206bd7cbc5aSPavel Labath m_thread = ThreadLauncher::LaunchThread(g_thread_name, Monitor::RunMonitor, this, nullptr); 12071107b5a5SPavel Labath if (!m_thread.IsJoinable()) 12081107b5a5SPavel Labath return Error("Failed to create monitor thread for NativeProcessLinux."); 12091107b5a5SPavel Labath 1210bd7cbc5aSPavel Labath // Wait for initial operation to complete. 121145f5cb31SPavel Labath return WaitForAck(); 1212bd7cbc5aSPavel Labath } 1213bd7cbc5aSPavel Labath 1214bd7cbc5aSPavel Labath void 1215bd7cbc5aSPavel Labath NativeProcessLinux::Monitor::DoOperation(Operation *op) 1216bd7cbc5aSPavel Labath { 1217bd7cbc5aSPavel Labath if (m_thread.EqualsThread(pthread_self())) { 1218bd7cbc5aSPavel Labath // If we're on the Monitor thread, we can simply execute the operation. 1219bd7cbc5aSPavel Labath op->Execute(m_native_process); 1220bd7cbc5aSPavel Labath return; 1221bd7cbc5aSPavel Labath } 1222bd7cbc5aSPavel Labath 1223bd7cbc5aSPavel Labath // Otherwise we need to pass the operation to the Monitor thread so it can handle it. 1224bd7cbc5aSPavel Labath Mutex::Locker lock(m_operation_mutex); 1225bd7cbc5aSPavel Labath 1226bd7cbc5aSPavel Labath m_operation = op; 1227bd7cbc5aSPavel Labath 1228bd7cbc5aSPavel Labath // notify the thread that an operation is ready to be processed 1229bd7cbc5aSPavel Labath write(m_pipefd[WRITE], &operation_command, sizeof operation_command); 1230bd7cbc5aSPavel Labath 123145f5cb31SPavel Labath WaitForAck(); 123245f5cb31SPavel Labath } 123345f5cb31SPavel Labath 123445f5cb31SPavel Labath void 123545f5cb31SPavel Labath NativeProcessLinux::Monitor::Terminate() 123645f5cb31SPavel Labath { 123745f5cb31SPavel Labath if (m_pipefd[WRITE] >= 0) 123845f5cb31SPavel Labath { 123945f5cb31SPavel Labath close(m_pipefd[WRITE]); 124045f5cb31SPavel Labath m_pipefd[WRITE] = -1; 124145f5cb31SPavel Labath } 124245f5cb31SPavel Labath if (m_thread.IsJoinable()) 124345f5cb31SPavel Labath m_thread.Join(nullptr); 12441107b5a5SPavel Labath } 12451107b5a5SPavel Labath 12461107b5a5SPavel Labath NativeProcessLinux::Monitor::~Monitor() 12471107b5a5SPavel Labath { 124845f5cb31SPavel Labath Terminate(); 12491107b5a5SPavel Labath if (m_pipefd[READ] >= 0) 12501107b5a5SPavel Labath close(m_pipefd[READ]); 12511107b5a5SPavel Labath if (m_signal_fd >= 0) 12521107b5a5SPavel Labath close(m_signal_fd); 1253bd7cbc5aSPavel Labath sem_destroy(&m_operation_sem); 12541107b5a5SPavel Labath } 12551107b5a5SPavel Labath 12561107b5a5SPavel Labath void 12571107b5a5SPavel Labath NativeProcessLinux::Monitor::HandleSignals() 12581107b5a5SPavel Labath { 12591107b5a5SPavel Labath Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 12601107b5a5SPavel Labath 12611107b5a5SPavel Labath // We don't really care about the content of the SIGCHLD siginfo structure, as we will get 12621107b5a5SPavel Labath // all the information from waitpid(). We just need to read all the signals so that we can 12631107b5a5SPavel Labath // sleep next time we reach select(). 12641107b5a5SPavel Labath while (true) 12651107b5a5SPavel Labath { 12661107b5a5SPavel Labath signalfd_siginfo info; 12671107b5a5SPavel Labath ssize_t size = read(m_signal_fd, &info, sizeof info); 12681107b5a5SPavel Labath if (size == -1) 12691107b5a5SPavel Labath { 12701107b5a5SPavel Labath if (errno == EAGAIN || errno == EWOULDBLOCK) 12711107b5a5SPavel Labath break; // We are done. 12721107b5a5SPavel Labath if (errno == EINTR) 12731107b5a5SPavel Labath continue; 12741107b5a5SPavel Labath if (log) 12751107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s reading from signalfd file descriptor failed: %s", 12761107b5a5SPavel Labath __FUNCTION__, strerror(errno)); 12771107b5a5SPavel Labath break; 12781107b5a5SPavel Labath } 12791107b5a5SPavel Labath if (size != sizeof info) 12801107b5a5SPavel Labath { 12811107b5a5SPavel Labath // We got incomplete information structure. This should not happen, let's just log 12821107b5a5SPavel Labath // that. 12831107b5a5SPavel Labath if (log) 12841107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s reading from signalfd file descriptor returned incomplete data: " 12851107b5a5SPavel Labath "structure size is %zd, read returned %zd bytes", 12861107b5a5SPavel Labath __FUNCTION__, sizeof info, size); 12871107b5a5SPavel Labath break; 12881107b5a5SPavel Labath } 12891107b5a5SPavel Labath if (log) 12901107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s received signal %s(%d).", __FUNCTION__, 12911107b5a5SPavel Labath Host::GetSignalAsCString(info.ssi_signo), info.ssi_signo); 12921107b5a5SPavel Labath } 12931107b5a5SPavel Labath } 12941107b5a5SPavel Labath 12951107b5a5SPavel Labath void 12961107b5a5SPavel Labath NativeProcessLinux::Monitor::HandleWait() 12971107b5a5SPavel Labath { 12981107b5a5SPavel Labath Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 12991107b5a5SPavel Labath // Process all pending waitpid notifications. 13001107b5a5SPavel Labath while (true) 13011107b5a5SPavel Labath { 13021107b5a5SPavel Labath int status = -1; 13031107b5a5SPavel Labath ::pid_t wait_pid = waitpid(m_child_pid, &status, __WALL | WNOHANG); 13041107b5a5SPavel Labath 13051107b5a5SPavel Labath if (wait_pid == 0) 13061107b5a5SPavel Labath break; // We are done. 13071107b5a5SPavel Labath 13081107b5a5SPavel Labath if (wait_pid == -1) 13091107b5a5SPavel Labath { 13101107b5a5SPavel Labath if (errno == EINTR) 13111107b5a5SPavel Labath continue; 13121107b5a5SPavel Labath 13131107b5a5SPavel Labath if (log) 13141107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s waitpid (pid = %" PRIi32 ", &status, __WALL | WNOHANG) failed: %s", 13151107b5a5SPavel Labath __FUNCTION__, m_child_pid, strerror(errno)); 13161107b5a5SPavel Labath break; 13171107b5a5SPavel Labath } 13181107b5a5SPavel Labath 13191107b5a5SPavel Labath bool exited = false; 13201107b5a5SPavel Labath int signal = 0; 13211107b5a5SPavel Labath int exit_status = 0; 13221107b5a5SPavel Labath const char *status_cstr = NULL; 13231107b5a5SPavel Labath if (WIFSTOPPED(status)) 13241107b5a5SPavel Labath { 13251107b5a5SPavel Labath signal = WSTOPSIG(status); 13261107b5a5SPavel Labath status_cstr = "STOPPED"; 13271107b5a5SPavel Labath } 13281107b5a5SPavel Labath else if (WIFEXITED(status)) 13291107b5a5SPavel Labath { 13301107b5a5SPavel Labath exit_status = WEXITSTATUS(status); 13311107b5a5SPavel Labath status_cstr = "EXITED"; 13321107b5a5SPavel Labath exited = true; 13331107b5a5SPavel Labath } 13341107b5a5SPavel Labath else if (WIFSIGNALED(status)) 13351107b5a5SPavel Labath { 13361107b5a5SPavel Labath signal = WTERMSIG(status); 13371107b5a5SPavel Labath status_cstr = "SIGNALED"; 13381107b5a5SPavel Labath if (wait_pid == abs(m_child_pid)) { 13391107b5a5SPavel Labath exited = true; 13401107b5a5SPavel Labath exit_status = -1; 13411107b5a5SPavel Labath } 13421107b5a5SPavel Labath } 13431107b5a5SPavel Labath else 13441107b5a5SPavel Labath status_cstr = "(\?\?\?)"; 13451107b5a5SPavel Labath 13461107b5a5SPavel Labath if (log) 13471107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s: waitpid (pid = %" PRIi32 ", &status, __WALL | WNOHANG)" 13481107b5a5SPavel Labath "=> pid = %" PRIi32 ", status = 0x%8.8x (%s), signal = %i, exit_state = %i", 13491107b5a5SPavel Labath __FUNCTION__, m_child_pid, wait_pid, status, status_cstr, signal, exit_status); 13501107b5a5SPavel Labath 13511107b5a5SPavel Labath m_native_process->MonitorCallback (wait_pid, exited, signal, exit_status); 13521107b5a5SPavel Labath } 13531107b5a5SPavel Labath } 13541107b5a5SPavel Labath 13551107b5a5SPavel Labath bool 13561107b5a5SPavel Labath NativeProcessLinux::Monitor::HandleCommands() 13571107b5a5SPavel Labath { 13581107b5a5SPavel Labath Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 13591107b5a5SPavel Labath 13601107b5a5SPavel Labath while (true) 13611107b5a5SPavel Labath { 13621107b5a5SPavel Labath char command = 0; 13631107b5a5SPavel Labath ssize_t size = read(m_pipefd[READ], &command, sizeof command); 13641107b5a5SPavel Labath if (size == -1) 13651107b5a5SPavel Labath { 13661107b5a5SPavel Labath if (errno == EAGAIN || errno == EWOULDBLOCK) 13671107b5a5SPavel Labath return false; 13681107b5a5SPavel Labath if (errno == EINTR) 13691107b5a5SPavel Labath continue; 13701107b5a5SPavel Labath if (log) 13711107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s exiting because read from command file descriptor failed: %s", __FUNCTION__, strerror(errno)); 13721107b5a5SPavel Labath return true; 13731107b5a5SPavel Labath } 13741107b5a5SPavel Labath if (size == 0) // end of file - write end closed 13751107b5a5SPavel Labath { 13761107b5a5SPavel Labath if (log) 13771107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s exit command received, exiting...", __FUNCTION__); 137845f5cb31SPavel Labath assert(m_operation_nesting_level == 0 && "Unbalanced begin/end block commands detected"); 13791107b5a5SPavel Labath return true; // We are done. 13801107b5a5SPavel Labath } 1381bd7cbc5aSPavel Labath 1382bd7cbc5aSPavel Labath switch (command) 1383bd7cbc5aSPavel Labath { 1384bd7cbc5aSPavel Labath case operation_command: 1385bd7cbc5aSPavel Labath m_operation->Execute(m_native_process); 138645f5cb31SPavel Labath break; 138745f5cb31SPavel Labath case begin_block_command: 138845f5cb31SPavel Labath ++m_operation_nesting_level; 138945f5cb31SPavel Labath break; 139045f5cb31SPavel Labath case end_block_command: 139145f5cb31SPavel Labath assert(m_operation_nesting_level > 0); 139245f5cb31SPavel Labath --m_operation_nesting_level; 1393bd7cbc5aSPavel Labath break; 1394bd7cbc5aSPavel Labath default: 13951107b5a5SPavel Labath if (log) 13961107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s received unknown command '%c'", 13971107b5a5SPavel Labath __FUNCTION__, command); 13981107b5a5SPavel Labath } 139945f5cb31SPavel Labath 140045f5cb31SPavel Labath // notify calling thread that the command has been processed 140145f5cb31SPavel Labath sem_post(&m_operation_sem); 14021107b5a5SPavel Labath } 1403bd7cbc5aSPavel Labath } 14041107b5a5SPavel Labath 14051107b5a5SPavel Labath void 14061107b5a5SPavel Labath NativeProcessLinux::Monitor::MainLoop() 14071107b5a5SPavel Labath { 1408bd7cbc5aSPavel Labath ::pid_t child_pid = (*m_initial_operation_up)(m_operation_error); 1409bd7cbc5aSPavel Labath m_initial_operation_up.reset(); 1410bd7cbc5aSPavel Labath m_child_pid = -getpgid(child_pid), 1411bd7cbc5aSPavel Labath sem_post(&m_operation_sem); 1412bd7cbc5aSPavel Labath 14131107b5a5SPavel Labath while (true) 14141107b5a5SPavel Labath { 14151107b5a5SPavel Labath fd_set fds; 14161107b5a5SPavel Labath FD_ZERO(&fds); 141745f5cb31SPavel Labath // Only process waitpid events if we are outside of an operation block. Any pending 141845f5cb31SPavel Labath // events will be processed after we leave the block. 141945f5cb31SPavel Labath if (m_operation_nesting_level == 0) 14201107b5a5SPavel Labath FD_SET(m_signal_fd, &fds); 14211107b5a5SPavel Labath FD_SET(m_pipefd[READ], &fds); 14221107b5a5SPavel Labath 14231107b5a5SPavel Labath int max_fd = std::max(m_signal_fd, m_pipefd[READ]) + 1; 14241107b5a5SPavel Labath int r = select(max_fd, &fds, nullptr, nullptr, nullptr); 14251107b5a5SPavel Labath if (r < 0) 14261107b5a5SPavel Labath { 14271107b5a5SPavel Labath Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 14281107b5a5SPavel Labath if (log) 14291107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s exiting because select failed: %s", 14301107b5a5SPavel Labath __FUNCTION__, strerror(errno)); 14311107b5a5SPavel Labath return; 14321107b5a5SPavel Labath } 14331107b5a5SPavel Labath 14341107b5a5SPavel Labath if (FD_ISSET(m_pipefd[READ], &fds)) 14351107b5a5SPavel Labath { 14361107b5a5SPavel Labath if (HandleCommands()) 14371107b5a5SPavel Labath return; 14381107b5a5SPavel Labath } 14391107b5a5SPavel Labath 14401107b5a5SPavel Labath if (FD_ISSET(m_signal_fd, &fds)) 14411107b5a5SPavel Labath { 14421107b5a5SPavel Labath HandleSignals(); 14431107b5a5SPavel Labath HandleWait(); 14441107b5a5SPavel Labath } 14451107b5a5SPavel Labath } 14461107b5a5SPavel Labath } 14471107b5a5SPavel Labath 1448bd7cbc5aSPavel Labath Error 144945f5cb31SPavel Labath NativeProcessLinux::Monitor::WaitForAck() 1450bd7cbc5aSPavel Labath { 1451bd7cbc5aSPavel Labath Error error; 1452bd7cbc5aSPavel Labath while (sem_wait(&m_operation_sem) != 0) 1453bd7cbc5aSPavel Labath { 1454bd7cbc5aSPavel Labath if (errno == EINTR) 1455bd7cbc5aSPavel Labath continue; 1456bd7cbc5aSPavel Labath 1457bd7cbc5aSPavel Labath error.SetErrorToErrno(); 1458bd7cbc5aSPavel Labath return error; 1459bd7cbc5aSPavel Labath } 1460bd7cbc5aSPavel Labath 1461bd7cbc5aSPavel Labath return m_operation_error; 1462bd7cbc5aSPavel Labath } 1463bd7cbc5aSPavel Labath 14641107b5a5SPavel Labath void * 14651107b5a5SPavel Labath NativeProcessLinux::Monitor::RunMonitor(void *arg) 14661107b5a5SPavel Labath { 14671107b5a5SPavel Labath static_cast<Monitor *>(arg)->MainLoop(); 14681107b5a5SPavel Labath return nullptr; 14691107b5a5SPavel Labath } 14701107b5a5SPavel Labath 14711107b5a5SPavel Labath 1472bd7cbc5aSPavel Labath NativeProcessLinux::LaunchArgs::LaunchArgs(Module *module, 1473af245d11STodd Fiala char const **argv, 1474af245d11STodd Fiala char const **envp, 147575f47c3aSTodd Fiala const std::string &stdin_path, 147675f47c3aSTodd Fiala const std::string &stdout_path, 147775f47c3aSTodd Fiala const std::string &stderr_path, 14780bce1b67STodd Fiala const char *working_dir, 1479db264a6dSTamas Berghammer const ProcessLaunchInfo &launch_info) 1480bd7cbc5aSPavel Labath : m_module(module), 1481af245d11STodd Fiala m_argv(argv), 1482af245d11STodd Fiala m_envp(envp), 1483af245d11STodd Fiala m_stdin_path(stdin_path), 1484af245d11STodd Fiala m_stdout_path(stdout_path), 1485af245d11STodd Fiala m_stderr_path(stderr_path), 14860bce1b67STodd Fiala m_working_dir(working_dir), 14870bce1b67STodd Fiala m_launch_info(launch_info) 14880bce1b67STodd Fiala { 14890bce1b67STodd Fiala } 1490af245d11STodd Fiala 1491af245d11STodd Fiala NativeProcessLinux::LaunchArgs::~LaunchArgs() 1492af245d11STodd Fiala { } 1493af245d11STodd Fiala 1494af245d11STodd Fiala // ----------------------------------------------------------------------------- 1495af245d11STodd Fiala // Public Static Methods 1496af245d11STodd Fiala // ----------------------------------------------------------------------------- 1497af245d11STodd Fiala 1498db264a6dSTamas Berghammer Error 1499af245d11STodd Fiala NativeProcessLinux::LaunchProcess ( 1500db264a6dSTamas Berghammer Module *exe_module, 1501db264a6dSTamas Berghammer ProcessLaunchInfo &launch_info, 1502db264a6dSTamas Berghammer NativeProcessProtocol::NativeDelegate &native_delegate, 1503af245d11STodd Fiala NativeProcessProtocolSP &native_process_sp) 1504af245d11STodd Fiala { 1505af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1506af245d11STodd Fiala 1507af245d11STodd Fiala Error error; 1508af245d11STodd Fiala 1509af245d11STodd Fiala // Verify the working directory is valid if one was specified. 1510af245d11STodd Fiala const char* working_dir = launch_info.GetWorkingDirectory (); 1511af245d11STodd Fiala if (working_dir) 1512af245d11STodd Fiala { 1513af245d11STodd Fiala FileSpec working_dir_fs (working_dir, true); 1514af245d11STodd Fiala if (!working_dir_fs || working_dir_fs.GetFileType () != FileSpec::eFileTypeDirectory) 1515af245d11STodd Fiala { 1516af245d11STodd Fiala error.SetErrorStringWithFormat ("No such file or directory: %s", working_dir); 1517af245d11STodd Fiala return error; 1518af245d11STodd Fiala } 1519af245d11STodd Fiala } 1520af245d11STodd Fiala 1521db264a6dSTamas Berghammer const FileAction *file_action; 1522af245d11STodd Fiala 1523af245d11STodd Fiala // Default of NULL will mean to use existing open file descriptors. 152475f47c3aSTodd Fiala std::string stdin_path; 152575f47c3aSTodd Fiala std::string stdout_path; 152675f47c3aSTodd Fiala std::string stderr_path; 1527af245d11STodd Fiala 1528af245d11STodd Fiala file_action = launch_info.GetFileActionForFD (STDIN_FILENO); 152975f47c3aSTodd Fiala if (file_action) 153075f47c3aSTodd Fiala stdin_path = file_action->GetPath (); 1531af245d11STodd Fiala 1532af245d11STodd Fiala file_action = launch_info.GetFileActionForFD (STDOUT_FILENO); 153375f47c3aSTodd Fiala if (file_action) 153475f47c3aSTodd Fiala stdout_path = file_action->GetPath (); 1535af245d11STodd Fiala 1536af245d11STodd Fiala file_action = launch_info.GetFileActionForFD (STDERR_FILENO); 153775f47c3aSTodd Fiala if (file_action) 153875f47c3aSTodd Fiala stderr_path = file_action->GetPath (); 153975f47c3aSTodd Fiala 154075f47c3aSTodd Fiala if (log) 154175f47c3aSTodd Fiala { 154275f47c3aSTodd Fiala if (!stdin_path.empty ()) 154375f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s setting STDIN to '%s'", __FUNCTION__, stdin_path.c_str ()); 154475f47c3aSTodd Fiala else 154575f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s leaving STDIN as is", __FUNCTION__); 154675f47c3aSTodd Fiala 154775f47c3aSTodd Fiala if (!stdout_path.empty ()) 154875f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s setting STDOUT to '%s'", __FUNCTION__, stdout_path.c_str ()); 154975f47c3aSTodd Fiala else 155075f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s leaving STDOUT as is", __FUNCTION__); 155175f47c3aSTodd Fiala 155275f47c3aSTodd Fiala if (!stderr_path.empty ()) 155375f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s setting STDERR to '%s'", __FUNCTION__, stderr_path.c_str ()); 155475f47c3aSTodd Fiala else 155575f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s leaving STDERR as is", __FUNCTION__); 155675f47c3aSTodd Fiala } 1557af245d11STodd Fiala 1558af245d11STodd Fiala // Create the NativeProcessLinux in launch mode. 1559af245d11STodd Fiala native_process_sp.reset (new NativeProcessLinux ()); 1560af245d11STodd Fiala 1561af245d11STodd Fiala if (log) 1562af245d11STodd Fiala { 1563af245d11STodd Fiala int i = 0; 1564af245d11STodd Fiala for (const char **args = launch_info.GetArguments ().GetConstArgumentVector (); *args; ++args, ++i) 1565af245d11STodd Fiala { 1566af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s arg %d: \"%s\"", __FUNCTION__, i, *args ? *args : "nullptr"); 1567af245d11STodd Fiala ++i; 1568af245d11STodd Fiala } 1569af245d11STodd Fiala } 1570af245d11STodd Fiala 1571af245d11STodd Fiala if (!native_process_sp->RegisterNativeDelegate (native_delegate)) 1572af245d11STodd Fiala { 1573af245d11STodd Fiala native_process_sp.reset (); 1574af245d11STodd Fiala error.SetErrorStringWithFormat ("failed to register the native delegate"); 1575af245d11STodd Fiala return error; 1576af245d11STodd Fiala } 1577af245d11STodd Fiala 1578cb84eebbSTamas Berghammer std::static_pointer_cast<NativeProcessLinux> (native_process_sp)->LaunchInferior ( 1579af245d11STodd Fiala exe_module, 1580af245d11STodd Fiala launch_info.GetArguments ().GetConstArgumentVector (), 1581af245d11STodd Fiala launch_info.GetEnvironmentEntries ().GetConstArgumentVector (), 1582af245d11STodd Fiala stdin_path, 1583af245d11STodd Fiala stdout_path, 1584af245d11STodd Fiala stderr_path, 1585af245d11STodd Fiala working_dir, 15860bce1b67STodd Fiala launch_info, 1587af245d11STodd Fiala error); 1588af245d11STodd Fiala 1589af245d11STodd Fiala if (error.Fail ()) 1590af245d11STodd Fiala { 1591af245d11STodd Fiala native_process_sp.reset (); 1592af245d11STodd Fiala if (log) 1593af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to launch process: %s", __FUNCTION__, error.AsCString ()); 1594af245d11STodd Fiala return error; 1595af245d11STodd Fiala } 1596af245d11STodd Fiala 1597af245d11STodd Fiala launch_info.SetProcessID (native_process_sp->GetID ()); 1598af245d11STodd Fiala 1599af245d11STodd Fiala return error; 1600af245d11STodd Fiala } 1601af245d11STodd Fiala 1602db264a6dSTamas Berghammer Error 1603af245d11STodd Fiala NativeProcessLinux::AttachToProcess ( 1604af245d11STodd Fiala lldb::pid_t pid, 1605db264a6dSTamas Berghammer NativeProcessProtocol::NativeDelegate &native_delegate, 1606af245d11STodd Fiala NativeProcessProtocolSP &native_process_sp) 1607af245d11STodd Fiala { 1608af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1609af245d11STodd Fiala if (log && log->GetMask ().Test (POSIX_LOG_VERBOSE)) 1610af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(pid = %" PRIi64 ")", __FUNCTION__, pid); 1611af245d11STodd Fiala 1612af245d11STodd Fiala // Grab the current platform architecture. This should be Linux, 1613af245d11STodd Fiala // since this code is only intended to run on a Linux host. 1614615eb7e6SGreg Clayton PlatformSP platform_sp (Platform::GetHostPlatform ()); 1615af245d11STodd Fiala if (!platform_sp) 1616af245d11STodd Fiala return Error("failed to get a valid default platform"); 1617af245d11STodd Fiala 1618af245d11STodd Fiala // Retrieve the architecture for the running process. 1619af245d11STodd Fiala ArchSpec process_arch; 1620af245d11STodd Fiala Error error = ResolveProcessArchitecture (pid, *platform_sp.get (), process_arch); 1621af245d11STodd Fiala if (!error.Success ()) 1622af245d11STodd Fiala return error; 1623af245d11STodd Fiala 16241339b5e8SOleksiy Vyalov std::shared_ptr<NativeProcessLinux> native_process_linux_sp (new NativeProcessLinux ()); 1625af245d11STodd Fiala 16261339b5e8SOleksiy Vyalov if (!native_process_linux_sp->RegisterNativeDelegate (native_delegate)) 1627af245d11STodd Fiala { 1628af245d11STodd Fiala error.SetErrorStringWithFormat ("failed to register the native delegate"); 1629af245d11STodd Fiala return error; 1630af245d11STodd Fiala } 1631af245d11STodd Fiala 16321339b5e8SOleksiy Vyalov native_process_linux_sp->AttachToInferior (pid, error); 1633af245d11STodd Fiala if (!error.Success ()) 1634af245d11STodd Fiala return error; 1635af245d11STodd Fiala 16361339b5e8SOleksiy Vyalov native_process_sp = native_process_linux_sp; 1637af245d11STodd Fiala return error; 1638af245d11STodd Fiala } 1639af245d11STodd Fiala 1640af245d11STodd Fiala // ----------------------------------------------------------------------------- 1641af245d11STodd Fiala // Public Instance Methods 1642af245d11STodd Fiala // ----------------------------------------------------------------------------- 1643af245d11STodd Fiala 1644af245d11STodd Fiala NativeProcessLinux::NativeProcessLinux () : 1645af245d11STodd Fiala NativeProcessProtocol (LLDB_INVALID_PROCESS_ID), 1646af245d11STodd Fiala m_arch (), 1647af245d11STodd Fiala m_supports_mem_region (eLazyBoolCalculate), 1648af245d11STodd Fiala m_mem_region_cache (), 1649*8c8ff7afSPavel Labath m_mem_region_cache_mutex () 1650af245d11STodd Fiala { 1651af245d11STodd Fiala } 1652af245d11STodd Fiala 1653af245d11STodd Fiala //------------------------------------------------------------------------------ 1654bd7cbc5aSPavel Labath // NativeProcessLinux spawns a new thread which performs all operations on the inferior process. 1655bd7cbc5aSPavel Labath // Refer to Monitor and Operation classes to see why this is necessary. 1656bd7cbc5aSPavel Labath //------------------------------------------------------------------------------ 1657af245d11STodd Fiala void 1658af245d11STodd Fiala NativeProcessLinux::LaunchInferior ( 1659af245d11STodd Fiala Module *module, 1660af245d11STodd Fiala const char *argv[], 1661af245d11STodd Fiala const char *envp[], 166275f47c3aSTodd Fiala const std::string &stdin_path, 166375f47c3aSTodd Fiala const std::string &stdout_path, 166475f47c3aSTodd Fiala const std::string &stderr_path, 1665af245d11STodd Fiala const char *working_dir, 1666db264a6dSTamas Berghammer const ProcessLaunchInfo &launch_info, 1667db264a6dSTamas Berghammer Error &error) 1668af245d11STodd Fiala { 1669af245d11STodd Fiala if (module) 1670af245d11STodd Fiala m_arch = module->GetArchitecture (); 1671af245d11STodd Fiala 1672af245d11STodd Fiala SetState (eStateLaunching); 1673af245d11STodd Fiala 1674af245d11STodd Fiala std::unique_ptr<LaunchArgs> args( 1675af245d11STodd Fiala new LaunchArgs( 1676bd7cbc5aSPavel Labath module, argv, envp, 1677af245d11STodd Fiala stdin_path, stdout_path, stderr_path, 16780bce1b67STodd Fiala working_dir, launch_info)); 1679af245d11STodd Fiala 1680bd7cbc5aSPavel Labath StartMonitorThread ([&] (Error &e) { return Launch(args.get(), e); }, error); 1681af245d11STodd Fiala if (!error.Success ()) 1682af245d11STodd Fiala return; 1683af245d11STodd Fiala } 1684af245d11STodd Fiala 1685af245d11STodd Fiala void 1686db264a6dSTamas Berghammer NativeProcessLinux::AttachToInferior (lldb::pid_t pid, Error &error) 1687af245d11STodd Fiala { 1688af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1689af245d11STodd Fiala if (log) 1690af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ")", __FUNCTION__, pid); 1691af245d11STodd Fiala 1692af245d11STodd Fiala // We can use the Host for everything except the ResolveExecutable portion. 1693615eb7e6SGreg Clayton PlatformSP platform_sp = Platform::GetHostPlatform (); 1694af245d11STodd Fiala if (!platform_sp) 1695af245d11STodd Fiala { 1696af245d11STodd Fiala if (log) 1697af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 "): no default platform set", __FUNCTION__, pid); 1698af245d11STodd Fiala error.SetErrorString ("no default platform available"); 169950d60be3SShawn Best return; 1700af245d11STodd Fiala } 1701af245d11STodd Fiala 1702af245d11STodd Fiala // Gather info about the process. 1703af245d11STodd Fiala ProcessInstanceInfo process_info; 170450d60be3SShawn Best if (!platform_sp->GetProcessInfo (pid, process_info)) 170550d60be3SShawn Best { 170650d60be3SShawn Best if (log) 170750d60be3SShawn Best log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 "): failed to get process info", __FUNCTION__, pid); 170850d60be3SShawn Best error.SetErrorString ("failed to get process info"); 170950d60be3SShawn Best return; 171050d60be3SShawn Best } 1711af245d11STodd Fiala 1712af245d11STodd Fiala // Resolve the executable module 1713af245d11STodd Fiala ModuleSP exe_module_sp; 1714af245d11STodd Fiala FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths()); 1715e56f6dceSChaoren Lin ModuleSpec exe_module_spec(process_info.GetExecutableFile(), process_info.GetArchitecture()); 17166edef204SOleksiy Vyalov error = platform_sp->ResolveExecutable(exe_module_spec, exe_module_sp, 1717af245d11STodd Fiala executable_search_paths.GetSize() ? &executable_search_paths : NULL); 1718af245d11STodd Fiala if (!error.Success()) 1719af245d11STodd Fiala return; 1720af245d11STodd Fiala 1721af245d11STodd Fiala // Set the architecture to the exe architecture. 1722af245d11STodd Fiala m_arch = exe_module_sp->GetArchitecture(); 1723af245d11STodd Fiala if (log) 1724af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ") detected architecture %s", __FUNCTION__, pid, m_arch.GetArchitectureName ()); 1725af245d11STodd Fiala 1726af245d11STodd Fiala m_pid = pid; 1727af245d11STodd Fiala SetState(eStateAttaching); 1728af245d11STodd Fiala 1729bd7cbc5aSPavel Labath StartMonitorThread ([=] (Error &e) { return Attach(pid, e); }, error); 1730af245d11STodd Fiala if (!error.Success ()) 1731af245d11STodd Fiala return; 1732af245d11STodd Fiala } 1733af245d11STodd Fiala 17348bc34f4dSOleksiy Vyalov void 17358bc34f4dSOleksiy Vyalov NativeProcessLinux::Terminate () 1736af245d11STodd Fiala { 173745f5cb31SPavel Labath m_monitor_up->Terminate(); 1738af245d11STodd Fiala } 1739af245d11STodd Fiala 1740bd7cbc5aSPavel Labath ::pid_t 1741bd7cbc5aSPavel Labath NativeProcessLinux::Launch(LaunchArgs *args, Error &error) 1742af245d11STodd Fiala { 17430bce1b67STodd Fiala assert (args && "null args"); 1744af245d11STodd Fiala 1745af245d11STodd Fiala const char **argv = args->m_argv; 1746af245d11STodd Fiala const char **envp = args->m_envp; 1747af245d11STodd Fiala const char *working_dir = args->m_working_dir; 1748af245d11STodd Fiala 1749af245d11STodd Fiala lldb_utility::PseudoTerminal terminal; 1750af245d11STodd Fiala const size_t err_len = 1024; 1751af245d11STodd Fiala char err_str[err_len]; 1752af245d11STodd Fiala lldb::pid_t pid; 1753af245d11STodd Fiala NativeThreadProtocolSP thread_sp; 1754af245d11STodd Fiala 1755af245d11STodd Fiala lldb::ThreadSP inferior; 1756af245d11STodd Fiala 1757af245d11STodd Fiala // Propagate the environment if one is not supplied. 1758af245d11STodd Fiala if (envp == NULL || envp[0] == NULL) 1759af245d11STodd Fiala envp = const_cast<const char **>(environ); 1760af245d11STodd Fiala 1761af245d11STodd Fiala if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t> (-1)) 1762af245d11STodd Fiala { 1763bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 1764bd7cbc5aSPavel Labath error.SetErrorStringWithFormat("Process fork failed: %s", err_str); 1765bd7cbc5aSPavel Labath return -1; 1766af245d11STodd Fiala } 1767af245d11STodd Fiala 1768af245d11STodd Fiala // Recognized child exit status codes. 1769af245d11STodd Fiala enum { 1770af245d11STodd Fiala ePtraceFailed = 1, 1771af245d11STodd Fiala eDupStdinFailed, 1772af245d11STodd Fiala eDupStdoutFailed, 1773af245d11STodd Fiala eDupStderrFailed, 1774af245d11STodd Fiala eChdirFailed, 1775af245d11STodd Fiala eExecFailed, 1776af245d11STodd Fiala eSetGidFailed 1777af245d11STodd Fiala }; 1778af245d11STodd Fiala 1779af245d11STodd Fiala // Child process. 1780af245d11STodd Fiala if (pid == 0) 1781af245d11STodd Fiala { 178275f47c3aSTodd Fiala // FIXME consider opening a pipe between parent/child and have this forked child 178375f47c3aSTodd Fiala // send log info to parent re: launch status, in place of the log lines removed here. 1784af245d11STodd Fiala 178575f47c3aSTodd Fiala // Start tracing this child that is about to exec. 1786bd7cbc5aSPavel Labath PTRACE(PTRACE_TRACEME, 0, nullptr, nullptr, 0, error); 1787bd7cbc5aSPavel Labath if (error.Fail()) 1788af245d11STodd Fiala exit(ePtraceFailed); 1789af245d11STodd Fiala 1790493c3a12SPavel Labath // terminal has already dupped the tty descriptors to stdin/out/err. 1791493c3a12SPavel Labath // This closes original fd from which they were copied (and avoids 1792493c3a12SPavel Labath // leaking descriptors to the debugged process. 1793493c3a12SPavel Labath terminal.CloseSlaveFileDescriptor(); 1794493c3a12SPavel Labath 1795af245d11STodd Fiala // Do not inherit setgid powers. 1796af245d11STodd Fiala if (setgid(getgid()) != 0) 1797af245d11STodd Fiala exit(eSetGidFailed); 1798af245d11STodd Fiala 1799af245d11STodd Fiala // Attempt to have our own process group. 1800af245d11STodd Fiala if (setpgid(0, 0) != 0) 1801af245d11STodd Fiala { 180275f47c3aSTodd Fiala // FIXME log that this failed. This is common. 1803af245d11STodd Fiala // Don't allow this to prevent an inferior exec. 1804af245d11STodd Fiala } 1805af245d11STodd Fiala 1806af245d11STodd Fiala // Dup file descriptors if needed. 180775f47c3aSTodd Fiala if (!args->m_stdin_path.empty ()) 180875f47c3aSTodd Fiala if (!DupDescriptor(args->m_stdin_path.c_str (), STDIN_FILENO, O_RDONLY)) 1809af245d11STodd Fiala exit(eDupStdinFailed); 1810af245d11STodd Fiala 181175f47c3aSTodd Fiala if (!args->m_stdout_path.empty ()) 181214f4476aSTamas Berghammer if (!DupDescriptor(args->m_stdout_path.c_str (), STDOUT_FILENO, O_WRONLY | O_CREAT | O_TRUNC)) 1813af245d11STodd Fiala exit(eDupStdoutFailed); 1814af245d11STodd Fiala 181575f47c3aSTodd Fiala if (!args->m_stderr_path.empty ()) 181614f4476aSTamas Berghammer if (!DupDescriptor(args->m_stderr_path.c_str (), STDERR_FILENO, O_WRONLY | O_CREAT | O_TRUNC)) 1817af245d11STodd Fiala exit(eDupStderrFailed); 1818af245d11STodd Fiala 18199cf4f2c2SChaoren Lin // Close everything besides stdin, stdout, and stderr that has no file 18209cf4f2c2SChaoren Lin // action to avoid leaking 18219cf4f2c2SChaoren Lin for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd) 18229cf4f2c2SChaoren Lin if (!args->m_launch_info.GetFileActionForFD(fd)) 18239cf4f2c2SChaoren Lin close(fd); 18249cf4f2c2SChaoren Lin 1825af245d11STodd Fiala // Change working directory 1826af245d11STodd Fiala if (working_dir != NULL && working_dir[0]) 1827af245d11STodd Fiala if (0 != ::chdir(working_dir)) 1828af245d11STodd Fiala exit(eChdirFailed); 1829af245d11STodd Fiala 18300bce1b67STodd Fiala // Disable ASLR if requested. 18310bce1b67STodd Fiala if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR)) 18320bce1b67STodd Fiala { 18330bce1b67STodd Fiala const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS); 18340bce1b67STodd Fiala if (old_personality == -1) 18350bce1b67STodd Fiala { 183675f47c3aSTodd Fiala // Can't retrieve Linux personality. Cannot disable ASLR. 18370bce1b67STodd Fiala } 18380bce1b67STodd Fiala else 18390bce1b67STodd Fiala { 18400bce1b67STodd Fiala const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality); 18410bce1b67STodd Fiala if (new_personality == -1) 18420bce1b67STodd Fiala { 184375f47c3aSTodd Fiala // Disabling ASLR failed. 18440bce1b67STodd Fiala } 18450bce1b67STodd Fiala else 18460bce1b67STodd Fiala { 184775f47c3aSTodd Fiala // Disabling ASLR succeeded. 18480bce1b67STodd Fiala } 18490bce1b67STodd Fiala } 18500bce1b67STodd Fiala } 18510bce1b67STodd Fiala 185275f47c3aSTodd Fiala // Execute. We should never return... 1853af245d11STodd Fiala execve(argv[0], 1854af245d11STodd Fiala const_cast<char *const *>(argv), 1855af245d11STodd Fiala const_cast<char *const *>(envp)); 185675f47c3aSTodd Fiala 185775f47c3aSTodd Fiala // ...unless exec fails. In which case we definitely need to end the child here. 1858af245d11STodd Fiala exit(eExecFailed); 1859af245d11STodd Fiala } 1860af245d11STodd Fiala 186175f47c3aSTodd Fiala // 186275f47c3aSTodd Fiala // This is the parent code here. 186375f47c3aSTodd Fiala // 186475f47c3aSTodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 186575f47c3aSTodd Fiala 1866af245d11STodd Fiala // Wait for the child process to trap on its call to execve. 1867af245d11STodd Fiala ::pid_t wpid; 1868af245d11STodd Fiala int status; 1869af245d11STodd Fiala if ((wpid = waitpid(pid, &status, 0)) < 0) 1870af245d11STodd Fiala { 1871bd7cbc5aSPavel Labath error.SetErrorToErrno(); 1872af245d11STodd Fiala if (log) 1873bd7cbc5aSPavel Labath log->Printf ("NativeProcessLinux::%s waitpid for inferior failed with %s", 1874bd7cbc5aSPavel Labath __FUNCTION__, error.AsCString ()); 1875af245d11STodd Fiala 1876af245d11STodd Fiala // Mark the inferior as invalid. 1877af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 1878bd7cbc5aSPavel Labath SetState (StateType::eStateInvalid); 1879af245d11STodd Fiala 1880bd7cbc5aSPavel Labath return -1; 1881af245d11STodd Fiala } 1882af245d11STodd Fiala else if (WIFEXITED(status)) 1883af245d11STodd Fiala { 1884af245d11STodd Fiala // open, dup or execve likely failed for some reason. 1885bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 1886af245d11STodd Fiala switch (WEXITSTATUS(status)) 1887af245d11STodd Fiala { 1888af245d11STodd Fiala case ePtraceFailed: 1889bd7cbc5aSPavel Labath error.SetErrorString("Child ptrace failed."); 1890af245d11STodd Fiala break; 1891af245d11STodd Fiala case eDupStdinFailed: 1892bd7cbc5aSPavel Labath error.SetErrorString("Child open stdin failed."); 1893af245d11STodd Fiala break; 1894af245d11STodd Fiala case eDupStdoutFailed: 1895bd7cbc5aSPavel Labath error.SetErrorString("Child open stdout failed."); 1896af245d11STodd Fiala break; 1897af245d11STodd Fiala case eDupStderrFailed: 1898bd7cbc5aSPavel Labath error.SetErrorString("Child open stderr failed."); 1899af245d11STodd Fiala break; 1900af245d11STodd Fiala case eChdirFailed: 1901bd7cbc5aSPavel Labath error.SetErrorString("Child failed to set working directory."); 1902af245d11STodd Fiala break; 1903af245d11STodd Fiala case eExecFailed: 1904bd7cbc5aSPavel Labath error.SetErrorString("Child exec failed."); 1905af245d11STodd Fiala break; 1906af245d11STodd Fiala case eSetGidFailed: 1907bd7cbc5aSPavel Labath error.SetErrorString("Child setgid failed."); 1908af245d11STodd Fiala break; 1909af245d11STodd Fiala default: 1910bd7cbc5aSPavel Labath error.SetErrorString("Child returned unknown exit status."); 1911af245d11STodd Fiala break; 1912af245d11STodd Fiala } 1913af245d11STodd Fiala 1914af245d11STodd Fiala if (log) 1915af245d11STodd Fiala { 1916af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior exited with status %d before issuing a STOP", 1917af245d11STodd Fiala __FUNCTION__, 1918af245d11STodd Fiala WEXITSTATUS(status)); 1919af245d11STodd Fiala } 1920af245d11STodd Fiala 1921af245d11STodd Fiala // Mark the inferior as invalid. 1922af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 1923bd7cbc5aSPavel Labath SetState (StateType::eStateInvalid); 1924af245d11STodd Fiala 1925bd7cbc5aSPavel Labath return -1; 1926af245d11STodd Fiala } 1927af245d11STodd Fiala assert(WIFSTOPPED(status) && (wpid == static_cast< ::pid_t> (pid)) && 1928af245d11STodd Fiala "Could not sync with inferior process."); 1929af245d11STodd Fiala 1930af245d11STodd Fiala if (log) 1931af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior started, now in stopped state", __FUNCTION__); 1932af245d11STodd Fiala 1933bd7cbc5aSPavel Labath error = SetDefaultPtraceOpts(pid); 1934bd7cbc5aSPavel Labath if (error.Fail()) 1935af245d11STodd Fiala { 1936af245d11STodd Fiala if (log) 1937af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior failed to set default ptrace options: %s", 1938bd7cbc5aSPavel Labath __FUNCTION__, error.AsCString ()); 1939af245d11STodd Fiala 1940af245d11STodd Fiala // Mark the inferior as invalid. 1941af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 1942bd7cbc5aSPavel Labath SetState (StateType::eStateInvalid); 1943af245d11STodd Fiala 1944bd7cbc5aSPavel Labath return -1; 1945af245d11STodd Fiala } 1946af245d11STodd Fiala 1947af245d11STodd Fiala // Release the master terminal descriptor and pass it off to the 1948af245d11STodd Fiala // NativeProcessLinux instance. Similarly stash the inferior pid. 1949bd7cbc5aSPavel Labath m_terminal_fd = terminal.ReleaseMasterFileDescriptor(); 1950bd7cbc5aSPavel Labath m_pid = pid; 1951af245d11STodd Fiala 1952af245d11STodd Fiala // Set the terminal fd to be in non blocking mode (it simplifies the 1953af245d11STodd Fiala // implementation of ProcessLinux::GetSTDOUT to have a non-blocking 1954af245d11STodd Fiala // descriptor to read from). 1955bd7cbc5aSPavel Labath error = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); 1956bd7cbc5aSPavel Labath if (error.Fail()) 1957af245d11STodd Fiala { 1958af245d11STodd Fiala if (log) 1959af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior EnsureFDFlags failed for ensuring terminal O_NONBLOCK setting: %s", 1960bd7cbc5aSPavel Labath __FUNCTION__, error.AsCString ()); 1961af245d11STodd Fiala 1962af245d11STodd Fiala // Mark the inferior as invalid. 1963af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 1964bd7cbc5aSPavel Labath SetState (StateType::eStateInvalid); 1965af245d11STodd Fiala 1966bd7cbc5aSPavel Labath return -1; 1967af245d11STodd Fiala } 1968af245d11STodd Fiala 1969af245d11STodd Fiala if (log) 1970af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() adding pid = %" PRIu64, __FUNCTION__, pid); 1971af245d11STodd Fiala 1972bd7cbc5aSPavel Labath thread_sp = AddThread (pid); 1973af245d11STodd Fiala assert (thread_sp && "AddThread() returned a nullptr thread"); 1974cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGSTOP); 1975*8c8ff7afSPavel Labath NotifyThreadCreate(pid); 1976af245d11STodd Fiala 1977af245d11STodd Fiala // Let our process instance know the thread has stopped. 1978bd7cbc5aSPavel Labath SetCurrentThreadID (thread_sp->GetID ()); 1979bd7cbc5aSPavel Labath SetState (StateType::eStateStopped); 1980af245d11STodd Fiala 1981af245d11STodd Fiala if (log) 1982af245d11STodd Fiala { 1983bd7cbc5aSPavel Labath if (error.Success ()) 1984af245d11STodd Fiala { 1985af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior launching succeeded", __FUNCTION__); 1986af245d11STodd Fiala } 1987af245d11STodd Fiala else 1988af245d11STodd Fiala { 1989af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior launching failed: %s", 1990bd7cbc5aSPavel Labath __FUNCTION__, error.AsCString ()); 1991bd7cbc5aSPavel Labath return -1; 1992af245d11STodd Fiala } 1993af245d11STodd Fiala } 1994bd7cbc5aSPavel Labath return pid; 1995af245d11STodd Fiala } 1996af245d11STodd Fiala 1997bd7cbc5aSPavel Labath ::pid_t 1998bd7cbc5aSPavel Labath NativeProcessLinux::Attach(lldb::pid_t pid, Error &error) 1999af245d11STodd Fiala { 2000af245d11STodd Fiala lldb::ThreadSP inferior; 2001af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2002af245d11STodd Fiala 2003af245d11STodd Fiala // Use a map to keep track of the threads which we have attached/need to attach. 2004af245d11STodd Fiala Host::TidMap tids_to_attach; 2005af245d11STodd Fiala if (pid <= 1) 2006af245d11STodd Fiala { 2007bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 2008bd7cbc5aSPavel Labath error.SetErrorString("Attaching to process 1 is not allowed."); 2009bd7cbc5aSPavel Labath return -1; 2010af245d11STodd Fiala } 2011af245d11STodd Fiala 2012af245d11STodd Fiala while (Host::FindProcessThreads(pid, tids_to_attach)) 2013af245d11STodd Fiala { 2014af245d11STodd Fiala for (Host::TidMap::iterator it = tids_to_attach.begin(); 2015af245d11STodd Fiala it != tids_to_attach.end();) 2016af245d11STodd Fiala { 2017af245d11STodd Fiala if (it->second == false) 2018af245d11STodd Fiala { 2019af245d11STodd Fiala lldb::tid_t tid = it->first; 2020af245d11STodd Fiala 2021af245d11STodd Fiala // Attach to the requested process. 2022af245d11STodd Fiala // An attach will cause the thread to stop with a SIGSTOP. 2023bd7cbc5aSPavel Labath PTRACE(PTRACE_ATTACH, tid, nullptr, nullptr, 0, error); 2024bd7cbc5aSPavel Labath if (error.Fail()) 2025af245d11STodd Fiala { 2026af245d11STodd Fiala // No such thread. The thread may have exited. 2027af245d11STodd Fiala // More error handling may be needed. 2028bd7cbc5aSPavel Labath if (error.GetError() == ESRCH) 2029af245d11STodd Fiala { 2030af245d11STodd Fiala it = tids_to_attach.erase(it); 2031af245d11STodd Fiala continue; 2032af245d11STodd Fiala } 2033af245d11STodd Fiala else 2034bd7cbc5aSPavel Labath return -1; 2035af245d11STodd Fiala } 2036af245d11STodd Fiala 2037af245d11STodd Fiala int status; 2038af245d11STodd Fiala // Need to use __WALL otherwise we receive an error with errno=ECHLD 2039af245d11STodd Fiala // At this point we should have a thread stopped if waitpid succeeds. 2040af245d11STodd Fiala if ((status = waitpid(tid, NULL, __WALL)) < 0) 2041af245d11STodd Fiala { 2042af245d11STodd Fiala // No such thread. The thread may have exited. 2043af245d11STodd Fiala // More error handling may be needed. 2044af245d11STodd Fiala if (errno == ESRCH) 2045af245d11STodd Fiala { 2046af245d11STodd Fiala it = tids_to_attach.erase(it); 2047af245d11STodd Fiala continue; 2048af245d11STodd Fiala } 2049af245d11STodd Fiala else 2050af245d11STodd Fiala { 2051bd7cbc5aSPavel Labath error.SetErrorToErrno(); 2052bd7cbc5aSPavel Labath return -1; 2053af245d11STodd Fiala } 2054af245d11STodd Fiala } 2055af245d11STodd Fiala 2056bd7cbc5aSPavel Labath error = SetDefaultPtraceOpts(tid); 2057bd7cbc5aSPavel Labath if (error.Fail()) 2058bd7cbc5aSPavel Labath return -1; 2059af245d11STodd Fiala 2060af245d11STodd Fiala if (log) 2061af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() adding tid = %" PRIu64, __FUNCTION__, tid); 2062af245d11STodd Fiala 2063af245d11STodd Fiala it->second = true; 2064af245d11STodd Fiala 2065af245d11STodd Fiala // Create the thread, mark it as stopped. 2066bd7cbc5aSPavel Labath NativeThreadProtocolSP thread_sp (AddThread (static_cast<lldb::tid_t> (tid))); 2067af245d11STodd Fiala assert (thread_sp && "AddThread() returned a nullptr"); 2068fa03ad2eSChaoren Lin 2069fa03ad2eSChaoren Lin // This will notify this is a new thread and tell the system it is stopped. 2070cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGSTOP); 2071*8c8ff7afSPavel Labath NotifyThreadCreate(tid); 2072bd7cbc5aSPavel Labath SetCurrentThreadID (thread_sp->GetID ()); 2073af245d11STodd Fiala } 2074af245d11STodd Fiala 2075af245d11STodd Fiala // move the loop forward 2076af245d11STodd Fiala ++it; 2077af245d11STodd Fiala } 2078af245d11STodd Fiala } 2079af245d11STodd Fiala 2080af245d11STodd Fiala if (tids_to_attach.size() > 0) 2081af245d11STodd Fiala { 2082bd7cbc5aSPavel Labath m_pid = pid; 2083af245d11STodd Fiala // Let our process instance know the thread has stopped. 2084bd7cbc5aSPavel Labath SetState (StateType::eStateStopped); 2085af245d11STodd Fiala } 2086af245d11STodd Fiala else 2087af245d11STodd Fiala { 2088bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 2089bd7cbc5aSPavel Labath error.SetErrorString("No such process."); 2090bd7cbc5aSPavel Labath return -1; 2091af245d11STodd Fiala } 2092af245d11STodd Fiala 2093bd7cbc5aSPavel Labath return pid; 2094af245d11STodd Fiala } 2095af245d11STodd Fiala 209697ccc294SChaoren Lin Error 2097af245d11STodd Fiala NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) 2098af245d11STodd Fiala { 2099af245d11STodd Fiala long ptrace_opts = 0; 2100af245d11STodd Fiala 2101af245d11STodd Fiala // Have the child raise an event on exit. This is used to keep the child in 2102af245d11STodd Fiala // limbo until it is destroyed. 2103af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXIT; 2104af245d11STodd Fiala 2105af245d11STodd Fiala // Have the tracer trace threads which spawn in the inferior process. 2106af245d11STodd Fiala // TODO: if we want to support tracing the inferiors' child, add the 2107af245d11STodd Fiala // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK) 2108af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACECLONE; 2109af245d11STodd Fiala 2110af245d11STodd Fiala // Have the tracer notify us before execve returns 2111af245d11STodd Fiala // (needed to disable legacy SIGTRAP generation) 2112af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXEC; 2113af245d11STodd Fiala 211497ccc294SChaoren Lin Error error; 211597ccc294SChaoren Lin PTRACE(PTRACE_SETOPTIONS, pid, nullptr, (void*)ptrace_opts, 0, error); 211697ccc294SChaoren Lin return error; 2117af245d11STodd Fiala } 2118af245d11STodd Fiala 2119af245d11STodd Fiala static ExitType convert_pid_status_to_exit_type (int status) 2120af245d11STodd Fiala { 2121af245d11STodd Fiala if (WIFEXITED (status)) 2122af245d11STodd Fiala return ExitType::eExitTypeExit; 2123af245d11STodd Fiala else if (WIFSIGNALED (status)) 2124af245d11STodd Fiala return ExitType::eExitTypeSignal; 2125af245d11STodd Fiala else if (WIFSTOPPED (status)) 2126af245d11STodd Fiala return ExitType::eExitTypeStop; 2127af245d11STodd Fiala else 2128af245d11STodd Fiala { 2129af245d11STodd Fiala // We don't know what this is. 2130af245d11STodd Fiala return ExitType::eExitTypeInvalid; 2131af245d11STodd Fiala } 2132af245d11STodd Fiala } 2133af245d11STodd Fiala 2134af245d11STodd Fiala static int convert_pid_status_to_return_code (int status) 2135af245d11STodd Fiala { 2136af245d11STodd Fiala if (WIFEXITED (status)) 2137af245d11STodd Fiala return WEXITSTATUS (status); 2138af245d11STodd Fiala else if (WIFSIGNALED (status)) 2139af245d11STodd Fiala return WTERMSIG (status); 2140af245d11STodd Fiala else if (WIFSTOPPED (status)) 2141af245d11STodd Fiala return WSTOPSIG (status); 2142af245d11STodd Fiala else 2143af245d11STodd Fiala { 2144af245d11STodd Fiala // We don't know what this is. 2145af245d11STodd Fiala return ExitType::eExitTypeInvalid; 2146af245d11STodd Fiala } 2147af245d11STodd Fiala } 2148af245d11STodd Fiala 21491107b5a5SPavel Labath // Handles all waitpid events from the inferior process. 21501107b5a5SPavel Labath void 21511107b5a5SPavel Labath NativeProcessLinux::MonitorCallback(lldb::pid_t pid, 2152af245d11STodd Fiala bool exited, 2153af245d11STodd Fiala int signal, 2154af245d11STodd Fiala int status) 2155af245d11STodd Fiala { 2156af245d11STodd Fiala Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); 2157af245d11STodd Fiala 2158af245d11STodd Fiala // Certain activities differ based on whether the pid is the tid of the main thread. 21591107b5a5SPavel Labath const bool is_main_thread = (pid == GetID ()); 2160af245d11STodd Fiala 2161af245d11STodd Fiala // Handle when the thread exits. 2162af245d11STodd Fiala if (exited) 2163af245d11STodd Fiala { 2164af245d11STodd Fiala if (log) 216586fd8e45SChaoren Lin log->Printf ("NativeProcessLinux::%s() got exit signal(%d) , tid = %" PRIu64 " (%s main thread)", __FUNCTION__, signal, pid, is_main_thread ? "is" : "is not"); 2166af245d11STodd Fiala 2167af245d11STodd Fiala // This is a thread that exited. Ensure we're not tracking it anymore. 21681107b5a5SPavel Labath const bool thread_found = StopTrackingThread (pid); 2169af245d11STodd Fiala 2170fa03ad2eSChaoren Lin // Make sure the thread state coordinator knows about this. 21711107b5a5SPavel Labath NotifyThreadDeath (pid); 2172fa03ad2eSChaoren Lin 2173af245d11STodd Fiala if (is_main_thread) 2174af245d11STodd Fiala { 2175af245d11STodd Fiala // We only set the exit status and notify the delegate if we haven't already set the process 2176af245d11STodd Fiala // state to an exited state. We normally should have received a SIGTRAP | (PTRACE_EVENT_EXIT << 8) 2177af245d11STodd Fiala // for the main thread. 21781107b5a5SPavel Labath const bool already_notified = (GetState() == StateType::eStateExited) || (GetState () == StateType::eStateCrashed); 2179af245d11STodd Fiala if (!already_notified) 2180af245d11STodd Fiala { 2181af245d11STodd Fiala if (log) 21821107b5a5SPavel 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 ())); 2183af245d11STodd Fiala // The main thread exited. We're done monitoring. Report to delegate. 21841107b5a5SPavel Labath SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); 2185af245d11STodd Fiala 2186af245d11STodd Fiala // Notify delegate that our process has exited. 21871107b5a5SPavel Labath SetState (StateType::eStateExited, true); 2188af245d11STodd Fiala } 2189af245d11STodd Fiala else 2190af245d11STodd Fiala { 2191af245d11STodd Fiala if (log) 2192af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() tid = %" PRIu64 " main thread now exited (%s)", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found"); 2193af245d11STodd Fiala } 2194af245d11STodd Fiala } 2195af245d11STodd Fiala else 2196af245d11STodd Fiala { 2197af245d11STodd Fiala // Do we want to report to the delegate in this case? I think not. If this was an orderly 2198af245d11STodd Fiala // thread exit, we would already have received the SIGTRAP | (PTRACE_EVENT_EXIT << 8) signal, 2199af245d11STodd Fiala // and we would have done an all-stop then. 2200af245d11STodd Fiala if (log) 2201af245d11STodd 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"); 2202af245d11STodd Fiala } 22031107b5a5SPavel Labath return; 2204af245d11STodd Fiala } 2205af245d11STodd Fiala 2206af245d11STodd Fiala // Get details on the signal raised. 2207af245d11STodd Fiala siginfo_t info; 22081107b5a5SPavel Labath const auto err = GetSignalInfo(pid, &info); 220997ccc294SChaoren Lin if (err.Success()) 2210fa03ad2eSChaoren Lin { 2211fa03ad2eSChaoren Lin // We have retrieved the signal info. Dispatch appropriately. 2212fa03ad2eSChaoren Lin if (info.si_signo == SIGTRAP) 22131107b5a5SPavel Labath MonitorSIGTRAP(&info, pid); 2214fa03ad2eSChaoren Lin else 22151107b5a5SPavel Labath MonitorSignal(&info, pid, exited); 2216fa03ad2eSChaoren Lin } 2217fa03ad2eSChaoren Lin else 2218af245d11STodd Fiala { 221997ccc294SChaoren Lin if (err.GetError() == EINVAL) 2220af245d11STodd Fiala { 2221fa03ad2eSChaoren Lin // This is a group stop reception for this tid. 2222fa03ad2eSChaoren Lin if (log) 22231107b5a5SPavel Labath log->Printf ("NativeThreadLinux::%s received a group stop for pid %" PRIu64 " tid %" PRIu64, __FUNCTION__, GetID (), pid); 22245eb721edSPavel Labath NotifyThreadStop (pid, false); 2225a9882ceeSTodd Fiala } 2226a9882ceeSTodd Fiala else 2227a9882ceeSTodd Fiala { 2228af245d11STodd Fiala // ptrace(GETSIGINFO) failed (but not due to group-stop). 2229af245d11STodd Fiala 2230af245d11STodd Fiala // A return value of ESRCH means the thread/process is no longer on the system, 2231af245d11STodd Fiala // so it was killed somehow outside of our control. Either way, we can't do anything 2232af245d11STodd Fiala // with it anymore. 2233af245d11STodd Fiala 2234af245d11STodd Fiala // Stop tracking the metadata for the thread since it's entirely off the system now. 22351107b5a5SPavel Labath const bool thread_found = StopTrackingThread (pid); 2236af245d11STodd Fiala 2237fa03ad2eSChaoren Lin // Make sure the thread state coordinator knows about this. 22381107b5a5SPavel Labath NotifyThreadDeath (pid); 2239fa03ad2eSChaoren Lin 2240af245d11STodd Fiala if (log) 2241af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d (%s, %s, %s)", 224297ccc294SChaoren 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"); 2243af245d11STodd Fiala 2244af245d11STodd Fiala if (is_main_thread) 2245af245d11STodd Fiala { 2246af245d11STodd Fiala // Notify the delegate - our process is not available but appears to have been killed outside 2247af245d11STodd Fiala // our control. Is eStateExited the right exit state in this case? 22481107b5a5SPavel Labath SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); 22491107b5a5SPavel Labath SetState (StateType::eStateExited, true); 2250af245d11STodd Fiala } 2251af245d11STodd Fiala else 2252af245d11STodd Fiala { 2253af245d11STodd Fiala // This thread was pulled out from underneath us. Anything to do here? Do we want to do an all stop? 2254af245d11STodd Fiala if (log) 22551107b5a5SPavel 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); 2256af245d11STodd Fiala } 2257af245d11STodd Fiala } 2258af245d11STodd Fiala } 2259af245d11STodd Fiala } 2260af245d11STodd Fiala 2261af245d11STodd Fiala void 2262426bdf88SPavel Labath NativeProcessLinux::WaitForNewThread(::pid_t tid) 2263426bdf88SPavel Labath { 2264426bdf88SPavel Labath Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2265426bdf88SPavel Labath 2266426bdf88SPavel Labath NativeThreadProtocolSP new_thread_sp = GetThreadByID(tid); 2267426bdf88SPavel Labath 2268426bdf88SPavel Labath if (new_thread_sp) 2269426bdf88SPavel Labath { 2270426bdf88SPavel Labath // We are already tracking the thread - we got the event on the new thread (see 2271426bdf88SPavel Labath // MonitorSignal) before this one. We are done. 2272426bdf88SPavel Labath return; 2273426bdf88SPavel Labath } 2274426bdf88SPavel Labath 2275426bdf88SPavel Labath // The thread is not tracked yet, let's wait for it to appear. 2276426bdf88SPavel Labath int status = -1; 2277426bdf88SPavel Labath ::pid_t wait_pid; 2278426bdf88SPavel Labath do 2279426bdf88SPavel Labath { 2280426bdf88SPavel Labath if (log) 2281426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() received thread creation event for tid %" PRIu32 ". tid not tracked yet, waiting for thread to appear...", __FUNCTION__, tid); 2282426bdf88SPavel Labath wait_pid = waitpid(tid, &status, __WALL); 2283426bdf88SPavel Labath } 2284426bdf88SPavel Labath while (wait_pid == -1 && errno == EINTR); 2285426bdf88SPavel Labath // Since we are waiting on a specific tid, this must be the creation event. But let's do 2286426bdf88SPavel Labath // some checks just in case. 2287426bdf88SPavel Labath if (wait_pid != tid) { 2288426bdf88SPavel Labath if (log) 2289426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() waiting for tid %" PRIu32 " failed. Assuming the thread has disappeared in the meantime", __FUNCTION__, tid); 2290426bdf88SPavel Labath // The only way I know of this could happen is if the whole process was 2291426bdf88SPavel Labath // SIGKILLed in the mean time. In any case, we can't do anything about that now. 2292426bdf88SPavel Labath return; 2293426bdf88SPavel Labath } 2294426bdf88SPavel Labath if (WIFEXITED(status)) 2295426bdf88SPavel Labath { 2296426bdf88SPavel Labath if (log) 2297426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() waiting for tid %" PRIu32 " returned an 'exited' event. Not tracking the thread.", __FUNCTION__, tid); 2298426bdf88SPavel Labath // Also a very improbable event. 2299426bdf88SPavel Labath return; 2300426bdf88SPavel Labath } 2301426bdf88SPavel Labath 2302426bdf88SPavel Labath siginfo_t info; 2303426bdf88SPavel Labath Error error = GetSignalInfo(tid, &info); 2304426bdf88SPavel Labath if (error.Fail()) 2305426bdf88SPavel Labath { 2306426bdf88SPavel Labath if (log) 2307426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() GetSignalInfo for tid %" PRIu32 " failed. Assuming the thread has disappeared in the meantime.", __FUNCTION__, tid); 2308426bdf88SPavel Labath return; 2309426bdf88SPavel Labath } 2310426bdf88SPavel Labath 2311426bdf88SPavel Labath if (((info.si_pid != 0) || (info.si_code != SI_USER)) && log) 2312426bdf88SPavel Labath { 2313426bdf88SPavel Labath // We should be getting a thread creation signal here, but we received something 2314426bdf88SPavel Labath // else. There isn't much we can do about it now, so we will just log that. Since the 2315426bdf88SPavel Labath // thread is alive and we are receiving events from it, we shall pretend that it was 2316426bdf88SPavel Labath // created properly. 2317426bdf88SPavel 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); 2318426bdf88SPavel Labath } 2319426bdf88SPavel Labath 2320426bdf88SPavel Labath if (log) 2321426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 ": tracking new thread tid %" PRIu32, 2322426bdf88SPavel Labath __FUNCTION__, GetID (), tid); 2323426bdf88SPavel Labath 2324426bdf88SPavel Labath new_thread_sp = AddThread(tid); 2325426bdf88SPavel Labath std::static_pointer_cast<NativeThreadLinux> (new_thread_sp)->SetRunning (); 2326426bdf88SPavel Labath Resume (tid, LLDB_INVALID_SIGNAL_NUMBER); 2327*8c8ff7afSPavel Labath NotifyThreadCreate(tid); 2328426bdf88SPavel Labath } 2329426bdf88SPavel Labath 2330426bdf88SPavel Labath void 2331af245d11STodd Fiala NativeProcessLinux::MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid) 2332af245d11STodd Fiala { 2333af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2334af245d11STodd Fiala const bool is_main_thread = (pid == GetID ()); 2335af245d11STodd Fiala 2336af245d11STodd Fiala assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!"); 2337af245d11STodd Fiala if (!info) 2338af245d11STodd Fiala return; 2339af245d11STodd Fiala 23405830aa75STamas Berghammer Mutex::Locker locker (m_threads_mutex); 23415830aa75STamas Berghammer 2342af245d11STodd Fiala // See if we can find a thread for this signal. 2343af245d11STodd Fiala NativeThreadProtocolSP thread_sp = GetThreadByID (pid); 2344af245d11STodd Fiala if (!thread_sp) 2345af245d11STodd Fiala { 2346af245d11STodd Fiala if (log) 2347af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " no thread found for tid %" PRIu64, __FUNCTION__, GetID (), pid); 2348af245d11STodd Fiala } 2349af245d11STodd Fiala 2350af245d11STodd Fiala switch (info->si_code) 2351af245d11STodd Fiala { 2352af245d11STodd 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. 2353af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)): 2354af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)): 2355af245d11STodd Fiala 2356af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)): 2357af245d11STodd Fiala { 23585fd24c67SPavel Labath // This is the notification on the parent thread which informs us of new thread 2359426bdf88SPavel Labath // creation. 2360426bdf88SPavel Labath // We don't want to do anything with the parent thread so we just resume it. In case we 2361426bdf88SPavel Labath // want to implement "break on thread creation" functionality, we would need to stop 2362426bdf88SPavel Labath // here. 2363af245d11STodd Fiala 2364af245d11STodd Fiala unsigned long event_message = 0; 2365426bdf88SPavel Labath if (GetEventMessage (pid, &event_message).Fail()) 2366fa03ad2eSChaoren Lin { 2367426bdf88SPavel Labath if (log) 2368fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " received thread creation event but GetEventMessage failed so we don't know the new tid", __FUNCTION__, pid); 2369426bdf88SPavel Labath } else 2370426bdf88SPavel Labath WaitForNewThread(event_message); 2371af245d11STodd Fiala 23725fd24c67SPavel Labath Resume (pid, LLDB_INVALID_SIGNAL_NUMBER); 2373af245d11STodd Fiala break; 2374af245d11STodd Fiala } 2375af245d11STodd Fiala 2376af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): 2377a9882ceeSTodd Fiala { 2378a9882ceeSTodd Fiala NativeThreadProtocolSP main_thread_sp; 2379af245d11STodd Fiala if (log) 2380af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP); 2381a9882ceeSTodd Fiala 2382fa03ad2eSChaoren Lin // The thread state coordinator needs to reset due to the exec. 2383c076559aSPavel Labath ResetForExec (); 2384fa03ad2eSChaoren Lin 2385fa03ad2eSChaoren 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. 2386a9882ceeSTodd Fiala if (log) 2387a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s exec received, stop tracking all but main thread", __FUNCTION__); 2388a9882ceeSTodd Fiala 2389a9882ceeSTodd Fiala for (auto thread_sp : m_threads) 2390a9882ceeSTodd Fiala { 2391a9882ceeSTodd Fiala const bool is_main_thread = thread_sp && thread_sp->GetID () == GetID (); 2392a9882ceeSTodd Fiala if (is_main_thread) 2393a9882ceeSTodd Fiala { 2394a9882ceeSTodd Fiala main_thread_sp = thread_sp; 2395a9882ceeSTodd Fiala if (log) 2396a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s found main thread with tid %" PRIu64 ", keeping", __FUNCTION__, main_thread_sp->GetID ()); 2397a9882ceeSTodd Fiala } 2398a9882ceeSTodd Fiala else 2399a9882ceeSTodd Fiala { 2400fa03ad2eSChaoren Lin // Tell thread coordinator this thread is dead. 2401a9882ceeSTodd Fiala if (log) 2402a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s discarding non-main-thread tid %" PRIu64 " due to exec", __FUNCTION__, thread_sp->GetID ()); 2403a9882ceeSTodd Fiala } 2404a9882ceeSTodd Fiala } 2405a9882ceeSTodd Fiala 2406a9882ceeSTodd Fiala m_threads.clear (); 2407a9882ceeSTodd Fiala 2408a9882ceeSTodd Fiala if (main_thread_sp) 2409a9882ceeSTodd Fiala { 2410a9882ceeSTodd Fiala m_threads.push_back (main_thread_sp); 2411a9882ceeSTodd Fiala SetCurrentThreadID (main_thread_sp->GetID ()); 2412cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (main_thread_sp)->SetStoppedByExec (); 2413a9882ceeSTodd Fiala } 2414a9882ceeSTodd Fiala else 2415a9882ceeSTodd Fiala { 2416a9882ceeSTodd Fiala SetCurrentThreadID (LLDB_INVALID_THREAD_ID); 2417a9882ceeSTodd Fiala if (log) 2418a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 "no main thread found, discarded all threads, we're in a no-thread state!", __FUNCTION__, GetID ()); 2419a9882ceeSTodd Fiala } 2420a9882ceeSTodd Fiala 2421fa03ad2eSChaoren Lin // Tell coordinator about about the "new" (since exec) stopped main thread. 2422fa03ad2eSChaoren Lin const lldb::tid_t main_thread_tid = GetID (); 2423*8c8ff7afSPavel Labath NotifyThreadCreate(main_thread_tid); 2424fa03ad2eSChaoren Lin 2425fa03ad2eSChaoren Lin // NOTE: ideally these next statements would execute at the same time as the coordinator thread create was executed. 2426fa03ad2eSChaoren Lin // Consider a handler that can execute when that happens. 2427a9882ceeSTodd Fiala // Let our delegate know we have just exec'd. 2428a9882ceeSTodd Fiala NotifyDidExec (); 2429a9882ceeSTodd Fiala 2430a9882ceeSTodd Fiala // If we have a main thread, indicate we are stopped. 2431a9882ceeSTodd Fiala assert (main_thread_sp && "exec called during ptraced process but no main thread metadata tracked"); 2432fa03ad2eSChaoren Lin 2433fa03ad2eSChaoren Lin // Let the process know we're stopped. 2434ed89c7feSPavel Labath StopRunningThreads (pid); 2435a9882ceeSTodd Fiala 2436af245d11STodd Fiala break; 2437a9882ceeSTodd Fiala } 2438af245d11STodd Fiala 2439af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): 2440af245d11STodd Fiala { 2441af245d11STodd Fiala // The inferior process or one of its threads is about to exit. 2442*8c8ff7afSPavel Labath if (! thread_sp) 2443*8c8ff7afSPavel Labath break; 2444fa03ad2eSChaoren Lin 2445fa03ad2eSChaoren Lin // This thread is currently stopped. It's not actually dead yet, just about to be. 24465eb721edSPavel Labath NotifyThreadStop (pid, false); 2447*8c8ff7afSPavel Labath // The actual stop reason does not matter much, as we are going to resume the thread a 2448*8c8ff7afSPavel Labath // few lines down. If we ever want to report this state to the debugger, then we should 2449*8c8ff7afSPavel Labath // invent a new stop reason. 2450*8c8ff7afSPavel Labath std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedBySignal(LLDB_INVALID_SIGNAL_NUMBER); 2451fa03ad2eSChaoren Lin 2452af245d11STodd Fiala unsigned long data = 0; 245397ccc294SChaoren Lin if (GetEventMessage(pid, &data).Fail()) 2454af245d11STodd Fiala data = -1; 2455af245d11STodd Fiala 2456af245d11STodd Fiala if (log) 2457af245d11STodd Fiala { 2458af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received PTRACE_EVENT_EXIT, data = %lx (WIFEXITED=%s,WIFSIGNALED=%s), pid = %" PRIu64 " (%s)", 2459af245d11STodd Fiala __FUNCTION__, 2460af245d11STodd Fiala data, WIFEXITED (data) ? "true" : "false", WIFSIGNALED (data) ? "true" : "false", 2461af245d11STodd Fiala pid, 2462af245d11STodd Fiala is_main_thread ? "is main thread" : "not main thread"); 2463af245d11STodd Fiala } 2464af245d11STodd Fiala 2465af245d11STodd Fiala if (is_main_thread) 2466af245d11STodd Fiala { 2467af245d11STodd Fiala SetExitStatus (convert_pid_status_to_exit_type (data), convert_pid_status_to_return_code (data), nullptr, true); 246875f47c3aSTodd Fiala } 246975f47c3aSTodd Fiala 24709d617ba6SChaoren Lin const int signo = static_cast<int> (data); 2471c076559aSPavel Labath RequestThreadResume (pid, 247286fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 2473fa03ad2eSChaoren Lin { 2474cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); 247537c768caSChaoren Lin return Resume (tid_to_resume, (supress_signal) ? LLDB_INVALID_SIGNAL_NUMBER : signo); 24765eb721edSPavel Labath }); 2477af245d11STodd Fiala 2478af245d11STodd Fiala break; 2479af245d11STodd Fiala } 2480af245d11STodd Fiala 2481af245d11STodd Fiala case 0: 2482c16f5dcaSChaoren Lin case TRAP_TRACE: // We receive this on single stepping. 2483c16f5dcaSChaoren Lin case TRAP_HWBKPT: // We receive this on watchpoint hit 248486fd8e45SChaoren Lin if (thread_sp) 248586fd8e45SChaoren Lin { 2486c16f5dcaSChaoren Lin // If a watchpoint was hit, report it 2487c16f5dcaSChaoren Lin uint32_t wp_index; 2488c16f5dcaSChaoren Lin Error error = thread_sp->GetRegisterContext()->GetWatchpointHitIndex(wp_index); 2489c16f5dcaSChaoren Lin if (error.Fail() && log) 2490c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() " 2491c16f5dcaSChaoren Lin "received error while checking for watchpoint hits, " 2492c16f5dcaSChaoren Lin "pid = %" PRIu64 " error = %s", 2493c16f5dcaSChaoren Lin __FUNCTION__, pid, error.AsCString()); 2494c16f5dcaSChaoren Lin if (wp_index != LLDB_INVALID_INDEX32) 24955830aa75STamas Berghammer { 2496c16f5dcaSChaoren Lin MonitorWatchpoint(pid, thread_sp, wp_index); 2497c16f5dcaSChaoren Lin break; 2498c16f5dcaSChaoren Lin } 2499c16f5dcaSChaoren Lin } 2500c16f5dcaSChaoren Lin // Otherwise, report step over 2501c16f5dcaSChaoren Lin MonitorTrace(pid, thread_sp); 2502af245d11STodd Fiala break; 2503af245d11STodd Fiala 2504af245d11STodd Fiala case SI_KERNEL: 2505af245d11STodd Fiala case TRAP_BRKPT: 2506c16f5dcaSChaoren Lin MonitorBreakpoint(pid, thread_sp); 2507af245d11STodd Fiala break; 2508af245d11STodd Fiala 2509af245d11STodd Fiala case SIGTRAP: 2510af245d11STodd Fiala case (SIGTRAP | 0x80): 2511af245d11STodd Fiala if (log) 2512fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s() received unknown SIGTRAP system call stop event, pid %" PRIu64 "tid %" PRIu64 ", resuming", __FUNCTION__, GetID (), pid); 2513fa03ad2eSChaoren Lin 2514fa03ad2eSChaoren Lin // This thread is currently stopped. 25155eb721edSPavel Labath NotifyThreadStop (pid, false); 2516fa03ad2eSChaoren Lin if (thread_sp) 2517cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGTRAP); 2518fa03ad2eSChaoren Lin 2519fa03ad2eSChaoren Lin 2520af245d11STodd Fiala // Ignore these signals until we know more about them. 2521c076559aSPavel Labath RequestThreadResume (pid, 252286fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 2523fa03ad2eSChaoren Lin { 2524cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); 252537c768caSChaoren Lin return Resume (tid_to_resume, LLDB_INVALID_SIGNAL_NUMBER); 25265eb721edSPavel Labath }); 2527af245d11STodd Fiala break; 2528af245d11STodd Fiala 2529af245d11STodd Fiala default: 2530af245d11STodd Fiala assert(false && "Unexpected SIGTRAP code!"); 2531af245d11STodd Fiala if (log) 2532af245d11STodd 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))); 2533af245d11STodd Fiala break; 2534af245d11STodd Fiala 2535af245d11STodd Fiala } 2536af245d11STodd Fiala } 2537af245d11STodd Fiala 2538af245d11STodd Fiala void 2539c16f5dcaSChaoren Lin NativeProcessLinux::MonitorTrace(lldb::pid_t pid, NativeThreadProtocolSP thread_sp) 2540c16f5dcaSChaoren Lin { 2541c16f5dcaSChaoren Lin Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 2542c16f5dcaSChaoren Lin if (log) 2543c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() received trace event, pid = %" PRIu64 " (single stepping)", 2544c16f5dcaSChaoren Lin __FUNCTION__, pid); 2545c16f5dcaSChaoren Lin 2546c16f5dcaSChaoren Lin if (thread_sp) 2547c16f5dcaSChaoren Lin std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByTrace(); 2548c16f5dcaSChaoren Lin 2549c16f5dcaSChaoren Lin // This thread is currently stopped. 25505eb721edSPavel Labath NotifyThreadStop(pid, false); 2551c16f5dcaSChaoren Lin 2552c16f5dcaSChaoren Lin // Here we don't have to request the rest of the threads to stop or request a deferred stop. 2553c16f5dcaSChaoren Lin // This would have already happened at the time the Resume() with step operation was signaled. 2554c16f5dcaSChaoren Lin // At this point, we just need to say we stopped, and the deferred notifcation will fire off 2555c16f5dcaSChaoren Lin // once all running threads have checked in as stopped. 2556c16f5dcaSChaoren Lin SetCurrentThreadID(pid); 2557c16f5dcaSChaoren Lin // Tell the process we have a stop (from software breakpoint). 2558ed89c7feSPavel Labath StopRunningThreads(pid); 2559c16f5dcaSChaoren Lin } 2560c16f5dcaSChaoren Lin 2561c16f5dcaSChaoren Lin void 2562c16f5dcaSChaoren Lin NativeProcessLinux::MonitorBreakpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp) 2563c16f5dcaSChaoren Lin { 2564c16f5dcaSChaoren Lin Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 2565c16f5dcaSChaoren Lin if (log) 2566c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() received breakpoint event, pid = %" PRIu64, 2567c16f5dcaSChaoren Lin __FUNCTION__, pid); 2568c16f5dcaSChaoren Lin 2569c16f5dcaSChaoren Lin // This thread is currently stopped. 25705eb721edSPavel Labath NotifyThreadStop(pid, false); 2571c16f5dcaSChaoren Lin 2572c16f5dcaSChaoren Lin // Mark the thread as stopped at breakpoint. 2573c16f5dcaSChaoren Lin if (thread_sp) 2574c16f5dcaSChaoren Lin { 2575c16f5dcaSChaoren Lin std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByBreakpoint(); 2576c16f5dcaSChaoren Lin Error error = FixupBreakpointPCAsNeeded(thread_sp); 2577c16f5dcaSChaoren Lin if (error.Fail()) 2578c16f5dcaSChaoren Lin if (log) 2579c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " fixup: %s", 2580c16f5dcaSChaoren Lin __FUNCTION__, pid, error.AsCString()); 2581d8c338d4STamas Berghammer 2582d8c338d4STamas Berghammer auto it = m_threads_stepping_with_breakpoint.find(pid); 2583d8c338d4STamas Berghammer if (it != m_threads_stepping_with_breakpoint.end()) 2584d8c338d4STamas Berghammer { 2585d8c338d4STamas Berghammer Error error = RemoveBreakpoint (it->second); 2586d8c338d4STamas Berghammer if (error.Fail()) 2587d8c338d4STamas Berghammer if (log) 2588d8c338d4STamas Berghammer log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " remove stepping breakpoint: %s", 2589d8c338d4STamas Berghammer __FUNCTION__, pid, error.AsCString()); 2590d8c338d4STamas Berghammer 2591d8c338d4STamas Berghammer m_threads_stepping_with_breakpoint.erase(it); 2592d8c338d4STamas Berghammer std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByTrace(); 2593d8c338d4STamas Berghammer } 2594c16f5dcaSChaoren Lin } 2595c16f5dcaSChaoren Lin else 2596c16f5dcaSChaoren Lin if (log) 2597c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 ": " 2598c16f5dcaSChaoren Lin "warning, cannot process software breakpoint since no thread metadata", 2599c16f5dcaSChaoren Lin __FUNCTION__, pid); 2600c16f5dcaSChaoren Lin 2601c16f5dcaSChaoren Lin 2602c16f5dcaSChaoren Lin // We need to tell all other running threads before we notify the delegate about this stop. 2603ed89c7feSPavel Labath StopRunningThreads(pid); 2604c16f5dcaSChaoren Lin } 2605c16f5dcaSChaoren Lin 2606c16f5dcaSChaoren Lin void 2607c16f5dcaSChaoren Lin NativeProcessLinux::MonitorWatchpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp, uint32_t wp_index) 2608c16f5dcaSChaoren Lin { 2609c16f5dcaSChaoren Lin Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_WATCHPOINTS)); 2610c16f5dcaSChaoren Lin if (log) 2611c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() received watchpoint event, " 2612c16f5dcaSChaoren Lin "pid = %" PRIu64 ", wp_index = %" PRIu32, 2613c16f5dcaSChaoren Lin __FUNCTION__, pid, wp_index); 2614c16f5dcaSChaoren Lin 2615c16f5dcaSChaoren Lin // This thread is currently stopped. 26165eb721edSPavel Labath NotifyThreadStop(pid, false); 2617c16f5dcaSChaoren Lin 2618c16f5dcaSChaoren Lin // Mark the thread as stopped at watchpoint. 2619c16f5dcaSChaoren Lin // The address is at (lldb::addr_t)info->si_addr if we need it. 2620c16f5dcaSChaoren Lin lldbassert(thread_sp && "thread_sp cannot be NULL"); 2621c16f5dcaSChaoren Lin std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByWatchpoint(wp_index); 2622c16f5dcaSChaoren Lin 2623c16f5dcaSChaoren Lin // We need to tell all other running threads before we notify the delegate about this stop. 2624ed89c7feSPavel Labath StopRunningThreads(pid); 2625c16f5dcaSChaoren Lin } 2626c16f5dcaSChaoren Lin 2627c16f5dcaSChaoren Lin void 2628af245d11STodd Fiala NativeProcessLinux::MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited) 2629af245d11STodd Fiala { 2630511e5cdcSTodd Fiala assert (info && "null info"); 2631511e5cdcSTodd Fiala if (!info) 2632511e5cdcSTodd Fiala return; 2633511e5cdcSTodd Fiala 2634511e5cdcSTodd Fiala const int signo = info->si_signo; 2635511e5cdcSTodd Fiala const bool is_from_llgs = info->si_pid == getpid (); 2636af245d11STodd Fiala 2637af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2638af245d11STodd Fiala 2639af245d11STodd Fiala // POSIX says that process behaviour is undefined after it ignores a SIGFPE, 2640af245d11STodd Fiala // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a 2641af245d11STodd Fiala // kill(2) or raise(3). Similarly for tgkill(2) on Linux. 2642af245d11STodd Fiala // 2643af245d11STodd Fiala // IOW, user generated signals never generate what we consider to be a 2644af245d11STodd Fiala // "crash". 2645af245d11STodd Fiala // 2646af245d11STodd Fiala // Similarly, ACK signals generated by this monitor. 2647af245d11STodd Fiala 26485830aa75STamas Berghammer Mutex::Locker locker (m_threads_mutex); 26495830aa75STamas Berghammer 2650af245d11STodd Fiala // See if we can find a thread for this signal. 2651af245d11STodd Fiala NativeThreadProtocolSP thread_sp = GetThreadByID (pid); 2652af245d11STodd Fiala if (!thread_sp) 2653af245d11STodd Fiala { 2654af245d11STodd Fiala if (log) 2655af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " no thread found for tid %" PRIu64, __FUNCTION__, GetID (), pid); 2656af245d11STodd Fiala } 2657af245d11STodd Fiala 2658af245d11STodd Fiala // Handle the signal. 2659af245d11STodd Fiala if (info->si_code == SI_TKILL || info->si_code == SI_USER) 2660af245d11STodd Fiala { 2661af245d11STodd Fiala if (log) 2662af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received signal %s (%d) with code %s, (siginfo pid = %d (%s), waitpid pid = %" PRIu64 ")", 2663af245d11STodd Fiala __FUNCTION__, 2664af245d11STodd Fiala GetUnixSignals ().GetSignalAsCString (signo), 2665af245d11STodd Fiala signo, 2666af245d11STodd Fiala (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"), 2667af245d11STodd Fiala info->si_pid, 2668511e5cdcSTodd Fiala is_from_llgs ? "from llgs" : "not from llgs", 2669af245d11STodd Fiala pid); 267058a2f669STodd Fiala } 2671af245d11STodd Fiala 267258a2f669STodd Fiala // Check for new thread notification. 267358a2f669STodd Fiala if ((info->si_pid == 0) && (info->si_code == SI_USER)) 2674af245d11STodd Fiala { 2675af245d11STodd Fiala // A new thread creation is being signaled. This is one of two parts that come in 2676426bdf88SPavel Labath // a non-deterministic order. This code handles the case where the new thread event comes 2677426bdf88SPavel Labath // before the event on the parent thread. For the opposite case see code in 2678426bdf88SPavel Labath // MonitorSIGTRAP. 2679af245d11STodd Fiala if (log) 2680af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 " tid %" PRIu64 ": new thread notification", 2681af245d11STodd Fiala __FUNCTION__, GetID (), pid); 2682af245d11STodd Fiala 26835fd24c67SPavel Labath thread_sp = AddThread(pid); 26845fd24c67SPavel Labath assert (thread_sp.get() && "failed to create the tracking data for newly created inferior thread"); 26855fd24c67SPavel Labath // We can now resume the newly created thread. 2686cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); 26875fd24c67SPavel Labath Resume (pid, LLDB_INVALID_SIGNAL_NUMBER); 2688*8c8ff7afSPavel Labath NotifyThreadCreate(pid); 268958a2f669STodd Fiala // Done handling. 269058a2f669STodd Fiala return; 2691af245d11STodd Fiala } 269258a2f669STodd Fiala 269358a2f669STodd Fiala // Check for thread stop notification. 2694511e5cdcSTodd Fiala if (is_from_llgs && (info->si_code == SI_TKILL) && (signo == SIGSTOP)) 2695af245d11STodd Fiala { 2696af245d11STodd Fiala // This is a tgkill()-based stop. 2697af245d11STodd Fiala if (thread_sp) 2698af245d11STodd Fiala { 2699fa03ad2eSChaoren Lin if (log) 2700fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 ", thread stopped", 2701fa03ad2eSChaoren Lin __FUNCTION__, 2702fa03ad2eSChaoren Lin GetID (), 2703fa03ad2eSChaoren Lin pid); 2704fa03ad2eSChaoren Lin 2705aab58633SChaoren Lin // Check that we're not already marked with a stop reason. 2706aab58633SChaoren Lin // Note this thread really shouldn't already be marked as stopped - if we were, that would imply that 2707aab58633SChaoren Lin // the kernel signaled us with the thread stopping which we handled and marked as stopped, 2708aab58633SChaoren Lin // and that, without an intervening resume, we received another stop. It is more likely 2709aab58633SChaoren Lin // that we are missing the marking of a run state somewhere if we find that the thread was 2710aab58633SChaoren Lin // marked as stopped. 2711cb84eebbSTamas Berghammer std::shared_ptr<NativeThreadLinux> linux_thread_sp = std::static_pointer_cast<NativeThreadLinux> (thread_sp); 2712cb84eebbSTamas Berghammer assert (linux_thread_sp && "linux_thread_sp is null!"); 2713aab58633SChaoren Lin 2714cb84eebbSTamas Berghammer const StateType thread_state = linux_thread_sp->GetState (); 2715aab58633SChaoren Lin if (!StateIsStoppedState (thread_state, false)) 2716aab58633SChaoren Lin { 2717ed89c7feSPavel Labath // An inferior thread has stopped because of a SIGSTOP we have sent it. 2718ed89c7feSPavel Labath // Generally, these are not important stops and we don't want to report them as 2719ed89c7feSPavel Labath // they are just used to stop other threads when one thread (the one with the 2720ed89c7feSPavel Labath // *real* stop reason) hits a breakpoint (watchpoint, etc...). However, in the 2721ed89c7feSPavel Labath // case of an asynchronous Interrupt(), this *is* the real stop reason, so we 2722ed89c7feSPavel Labath // leave the signal intact if this is the thread that was chosen as the 2723ed89c7feSPavel Labath // triggering thread. 2724ed89c7feSPavel Labath if (m_pending_notification_up && m_pending_notification_up->triggering_tid == pid) 2725ed89c7feSPavel Labath linux_thread_sp->SetStoppedBySignal(SIGSTOP); 2726ed89c7feSPavel Labath else 2727cb84eebbSTamas Berghammer linux_thread_sp->SetStoppedBySignal(0); 2728ed89c7feSPavel Labath 2729af245d11STodd Fiala SetCurrentThreadID (thread_sp->GetID ()); 27305eb721edSPavel Labath NotifyThreadStop (thread_sp->GetID (), true); 2731aab58633SChaoren Lin } 2732aab58633SChaoren Lin else 2733aab58633SChaoren Lin { 2734aab58633SChaoren Lin if (log) 2735aab58633SChaoren Lin { 2736aab58633SChaoren Lin // Retrieve the signal name if the thread was stopped by a signal. 2737aab58633SChaoren Lin int stop_signo = 0; 2738cb84eebbSTamas Berghammer const bool stopped_by_signal = linux_thread_sp->IsStopped (&stop_signo); 2739aab58633SChaoren Lin const char *signal_name = stopped_by_signal ? GetUnixSignals ().GetSignalAsCString (stop_signo) : "<not stopped by signal>"; 2740aab58633SChaoren Lin if (!signal_name) 2741aab58633SChaoren Lin signal_name = "<no-signal-name>"; 2742aab58633SChaoren Lin 2743aab58633SChaoren 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", 2744aab58633SChaoren Lin __FUNCTION__, 2745aab58633SChaoren Lin GetID (), 2746cb84eebbSTamas Berghammer linux_thread_sp->GetID (), 2747aab58633SChaoren Lin StateAsCString (thread_state), 2748aab58633SChaoren Lin stop_signo, 2749aab58633SChaoren Lin signal_name); 2750aab58633SChaoren Lin } 2751fa03ad2eSChaoren Lin // Tell the thread state coordinator about the stop. 27525eb721edSPavel Labath NotifyThreadStop (thread_sp->GetID (), false); 2753af245d11STodd Fiala } 275486fd8e45SChaoren Lin } 2755af245d11STodd Fiala 275658a2f669STodd Fiala // Done handling. 2757af245d11STodd Fiala return; 2758af245d11STodd Fiala } 2759af245d11STodd Fiala 2760af245d11STodd Fiala if (log) 2761af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received signal %s", __FUNCTION__, GetUnixSignals ().GetSignalAsCString (signo)); 2762af245d11STodd Fiala 276386fd8e45SChaoren Lin // This thread is stopped. 27645eb721edSPavel Labath NotifyThreadStop (pid, false); 276586fd8e45SChaoren Lin 2766af245d11STodd Fiala switch (signo) 2767af245d11STodd Fiala { 2768511e5cdcSTodd Fiala case SIGSTOP: 2769511e5cdcSTodd Fiala { 2770*8c8ff7afSPavel Labath std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (signo); 277158a2f669STodd Fiala if (log) 2772511e5cdcSTodd Fiala { 2773511e5cdcSTodd Fiala if (is_from_llgs) 2774511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " received SIGSTOP from llgs, most likely an interrupt", __FUNCTION__, GetID (), pid); 2775511e5cdcSTodd Fiala else 2776511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " received SIGSTOP from outside of debugger", __FUNCTION__, GetID (), pid); 2777511e5cdcSTodd Fiala } 2778511e5cdcSTodd Fiala 2779fa03ad2eSChaoren Lin // Resume this thread to get the group-stop mechanism to fire off the true group stops. 2780fa03ad2eSChaoren Lin // This thread will get stopped again as part of the group-stop completion. 2781c076559aSPavel Labath RequestThreadResume (pid, 278286fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 2783fa03ad2eSChaoren Lin { 2784cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); 2785fa03ad2eSChaoren Lin // Pass this signal number on to the inferior to handle. 278637c768caSChaoren Lin return Resume (tid_to_resume, (supress_signal) ? LLDB_INVALID_SIGNAL_NUMBER : signo); 27875eb721edSPavel Labath }); 278886fd8e45SChaoren Lin } 278986fd8e45SChaoren Lin break; 279086fd8e45SChaoren Lin case SIGSEGV: 279186fd8e45SChaoren Lin case SIGILL: 279286fd8e45SChaoren Lin case SIGFPE: 279386fd8e45SChaoren Lin case SIGBUS: 279486fd8e45SChaoren Lin if (thread_sp) 2795cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetCrashedWithException (*info); 279686fd8e45SChaoren Lin break; 279786fd8e45SChaoren Lin default: 279886fd8e45SChaoren Lin // This is just a pre-signal-delivery notification of the incoming signal. 279986fd8e45SChaoren Lin if (thread_sp) 2800cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (signo); 2801fa03ad2eSChaoren Lin 280286fd8e45SChaoren Lin break; 280386fd8e45SChaoren Lin } 280486fd8e45SChaoren Lin 280586fd8e45SChaoren Lin // Send a stop to the debugger after we get all other threads to stop. 2806ed89c7feSPavel Labath StopRunningThreads (pid); 2807511e5cdcSTodd Fiala } 2808af245d11STodd Fiala 2809e7708688STamas Berghammer namespace { 2810e7708688STamas Berghammer 2811e7708688STamas Berghammer struct EmulatorBaton 2812e7708688STamas Berghammer { 2813e7708688STamas Berghammer NativeProcessLinux* m_process; 2814e7708688STamas Berghammer NativeRegisterContext* m_reg_context; 28156648fcc3SPavel Labath 28166648fcc3SPavel Labath // eRegisterKindDWARF -> RegsiterValue 28176648fcc3SPavel Labath std::unordered_map<uint32_t, RegisterValue> m_register_values; 2818e7708688STamas Berghammer 2819e7708688STamas Berghammer EmulatorBaton(NativeProcessLinux* process, NativeRegisterContext* reg_context) : 2820e7708688STamas Berghammer m_process(process), m_reg_context(reg_context) {} 2821e7708688STamas Berghammer }; 2822e7708688STamas Berghammer 2823e7708688STamas Berghammer } // anonymous namespace 2824e7708688STamas Berghammer 2825e7708688STamas Berghammer static size_t 2826e7708688STamas Berghammer ReadMemoryCallback (EmulateInstruction *instruction, 2827e7708688STamas Berghammer void *baton, 2828e7708688STamas Berghammer const EmulateInstruction::Context &context, 2829e7708688STamas Berghammer lldb::addr_t addr, 2830e7708688STamas Berghammer void *dst, 2831e7708688STamas Berghammer size_t length) 2832e7708688STamas Berghammer { 2833e7708688STamas Berghammer EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); 2834e7708688STamas Berghammer 28353eb4b458SChaoren Lin size_t bytes_read; 2836e7708688STamas Berghammer emulator_baton->m_process->ReadMemory(addr, dst, length, bytes_read); 2837e7708688STamas Berghammer return bytes_read; 2838e7708688STamas Berghammer } 2839e7708688STamas Berghammer 2840e7708688STamas Berghammer static bool 2841e7708688STamas Berghammer ReadRegisterCallback (EmulateInstruction *instruction, 2842e7708688STamas Berghammer void *baton, 2843e7708688STamas Berghammer const RegisterInfo *reg_info, 2844e7708688STamas Berghammer RegisterValue ®_value) 2845e7708688STamas Berghammer { 2846e7708688STamas Berghammer EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); 2847e7708688STamas Berghammer 28486648fcc3SPavel Labath auto it = emulator_baton->m_register_values.find(reg_info->kinds[eRegisterKindDWARF]); 28496648fcc3SPavel Labath if (it != emulator_baton->m_register_values.end()) 28506648fcc3SPavel Labath { 28516648fcc3SPavel Labath reg_value = it->second; 28526648fcc3SPavel Labath return true; 28536648fcc3SPavel Labath } 28546648fcc3SPavel Labath 2855e7708688STamas Berghammer // The emulator only fill in the dwarf regsiter numbers (and in some case 2856e7708688STamas Berghammer // the generic register numbers). Get the full register info from the 2857e7708688STamas Berghammer // register context based on the dwarf register numbers. 2858e7708688STamas Berghammer const RegisterInfo* full_reg_info = emulator_baton->m_reg_context->GetRegisterInfo( 2859e7708688STamas Berghammer eRegisterKindDWARF, reg_info->kinds[eRegisterKindDWARF]); 2860e7708688STamas Berghammer 2861e7708688STamas Berghammer Error error = emulator_baton->m_reg_context->ReadRegister(full_reg_info, reg_value); 28626648fcc3SPavel Labath if (error.Success()) 28636648fcc3SPavel Labath return true; 2864cdc22a88SMohit K. Bhakkad 28656648fcc3SPavel Labath return false; 2866e7708688STamas Berghammer } 2867e7708688STamas Berghammer 2868e7708688STamas Berghammer static bool 2869e7708688STamas Berghammer WriteRegisterCallback (EmulateInstruction *instruction, 2870e7708688STamas Berghammer void *baton, 2871e7708688STamas Berghammer const EmulateInstruction::Context &context, 2872e7708688STamas Berghammer const RegisterInfo *reg_info, 2873e7708688STamas Berghammer const RegisterValue ®_value) 2874e7708688STamas Berghammer { 2875e7708688STamas Berghammer EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); 28766648fcc3SPavel Labath emulator_baton->m_register_values[reg_info->kinds[eRegisterKindDWARF]] = reg_value; 2877e7708688STamas Berghammer return true; 2878e7708688STamas Berghammer } 2879e7708688STamas Berghammer 2880e7708688STamas Berghammer static size_t 2881e7708688STamas Berghammer WriteMemoryCallback (EmulateInstruction *instruction, 2882e7708688STamas Berghammer void *baton, 2883e7708688STamas Berghammer const EmulateInstruction::Context &context, 2884e7708688STamas Berghammer lldb::addr_t addr, 2885e7708688STamas Berghammer const void *dst, 2886e7708688STamas Berghammer size_t length) 2887e7708688STamas Berghammer { 2888e7708688STamas Berghammer return length; 2889e7708688STamas Berghammer } 2890e7708688STamas Berghammer 2891e7708688STamas Berghammer static lldb::addr_t 2892e7708688STamas Berghammer ReadFlags (NativeRegisterContext* regsiter_context) 2893e7708688STamas Berghammer { 2894e7708688STamas Berghammer const RegisterInfo* flags_info = regsiter_context->GetRegisterInfo( 2895e7708688STamas Berghammer eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 2896e7708688STamas Berghammer return regsiter_context->ReadRegisterAsUnsigned(flags_info, LLDB_INVALID_ADDRESS); 2897e7708688STamas Berghammer } 2898e7708688STamas Berghammer 2899e7708688STamas Berghammer Error 2900e7708688STamas Berghammer NativeProcessLinux::SetupSoftwareSingleStepping(NativeThreadProtocolSP thread_sp) 2901e7708688STamas Berghammer { 2902e7708688STamas Berghammer Error error; 2903e7708688STamas Berghammer NativeRegisterContextSP register_context_sp = thread_sp->GetRegisterContext(); 2904e7708688STamas Berghammer 2905e7708688STamas Berghammer std::unique_ptr<EmulateInstruction> emulator_ap( 2906e7708688STamas Berghammer EmulateInstruction::FindPlugin(m_arch, eInstructionTypePCModifying, nullptr)); 2907e7708688STamas Berghammer 2908e7708688STamas Berghammer if (emulator_ap == nullptr) 2909e7708688STamas Berghammer return Error("Instruction emulator not found!"); 2910e7708688STamas Berghammer 2911e7708688STamas Berghammer EmulatorBaton baton(this, register_context_sp.get()); 2912e7708688STamas Berghammer emulator_ap->SetBaton(&baton); 2913e7708688STamas Berghammer emulator_ap->SetReadMemCallback(&ReadMemoryCallback); 2914e7708688STamas Berghammer emulator_ap->SetReadRegCallback(&ReadRegisterCallback); 2915e7708688STamas Berghammer emulator_ap->SetWriteMemCallback(&WriteMemoryCallback); 2916e7708688STamas Berghammer emulator_ap->SetWriteRegCallback(&WriteRegisterCallback); 2917e7708688STamas Berghammer 2918e7708688STamas Berghammer if (!emulator_ap->ReadInstruction()) 2919e7708688STamas Berghammer return Error("Read instruction failed!"); 2920e7708688STamas Berghammer 29216648fcc3SPavel Labath bool emulation_result = emulator_ap->EvaluateInstruction(eEmulateInstructionOptionAutoAdvancePC); 29226648fcc3SPavel Labath 29236648fcc3SPavel Labath const RegisterInfo* reg_info_pc = register_context_sp->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); 29246648fcc3SPavel Labath const RegisterInfo* reg_info_flags = register_context_sp->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 29256648fcc3SPavel Labath 29266648fcc3SPavel Labath auto pc_it = baton.m_register_values.find(reg_info_pc->kinds[eRegisterKindDWARF]); 29276648fcc3SPavel Labath auto flags_it = baton.m_register_values.find(reg_info_flags->kinds[eRegisterKindDWARF]); 29286648fcc3SPavel Labath 2929e7708688STamas Berghammer lldb::addr_t next_pc; 2930e7708688STamas Berghammer lldb::addr_t next_flags; 29316648fcc3SPavel Labath if (emulation_result) 2932e7708688STamas Berghammer { 29336648fcc3SPavel Labath assert(pc_it != baton.m_register_values.end() && "Emulation was successfull but PC wasn't updated"); 29346648fcc3SPavel Labath next_pc = pc_it->second.GetAsUInt64(); 29356648fcc3SPavel Labath 29366648fcc3SPavel Labath if (flags_it != baton.m_register_values.end()) 29376648fcc3SPavel Labath next_flags = flags_it->second.GetAsUInt64(); 2938e7708688STamas Berghammer else 2939e7708688STamas Berghammer next_flags = ReadFlags (register_context_sp.get()); 2940e7708688STamas Berghammer } 29416648fcc3SPavel Labath else if (pc_it == baton.m_register_values.end()) 2942e7708688STamas Berghammer { 2943e7708688STamas Berghammer // Emulate instruction failed and it haven't changed PC. Advance PC 2944e7708688STamas Berghammer // with the size of the current opcode because the emulation of all 2945e7708688STamas Berghammer // PC modifying instruction should be successful. The failure most 2946e7708688STamas Berghammer // likely caused by a not supported instruction which don't modify PC. 2947e7708688STamas Berghammer next_pc = register_context_sp->GetPC() + emulator_ap->GetOpcode().GetByteSize(); 2948e7708688STamas Berghammer next_flags = ReadFlags (register_context_sp.get()); 2949e7708688STamas Berghammer } 2950e7708688STamas Berghammer else 2951e7708688STamas Berghammer { 2952e7708688STamas Berghammer // The instruction emulation failed after it modified the PC. It is an 2953e7708688STamas Berghammer // unknown error where we can't continue because the next instruction is 2954e7708688STamas Berghammer // modifying the PC but we don't know how. 2955e7708688STamas Berghammer return Error ("Instruction emulation failed unexpectedly."); 2956e7708688STamas Berghammer } 2957e7708688STamas Berghammer 2958e7708688STamas Berghammer if (m_arch.GetMachine() == llvm::Triple::arm) 2959e7708688STamas Berghammer { 2960e7708688STamas Berghammer if (next_flags & 0x20) 2961e7708688STamas Berghammer { 2962e7708688STamas Berghammer // Thumb mode 2963e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 2); 2964e7708688STamas Berghammer } 2965e7708688STamas Berghammer else 2966e7708688STamas Berghammer { 2967e7708688STamas Berghammer // Arm mode 2968e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 4); 2969e7708688STamas Berghammer } 2970e7708688STamas Berghammer } 2971cdc22a88SMohit K. Bhakkad else if (m_arch.GetMachine() == llvm::Triple::mips64 2972cdc22a88SMohit K. Bhakkad || m_arch.GetMachine() == llvm::Triple::mips64el) 2973cdc22a88SMohit K. Bhakkad error = SetSoftwareBreakpoint(next_pc, 4); 2974e7708688STamas Berghammer else 2975e7708688STamas Berghammer { 2976e7708688STamas Berghammer // No size hint is given for the next breakpoint 2977e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 0); 2978e7708688STamas Berghammer } 2979e7708688STamas Berghammer 2980e7708688STamas Berghammer if (error.Fail()) 2981e7708688STamas Berghammer return error; 2982e7708688STamas Berghammer 2983e7708688STamas Berghammer m_threads_stepping_with_breakpoint.insert({thread_sp->GetID(), next_pc}); 2984e7708688STamas Berghammer 2985e7708688STamas Berghammer return Error(); 2986e7708688STamas Berghammer } 2987e7708688STamas Berghammer 2988e7708688STamas Berghammer bool 2989e7708688STamas Berghammer NativeProcessLinux::SupportHardwareSingleStepping() const 2990e7708688STamas Berghammer { 2991cdc22a88SMohit K. Bhakkad if (m_arch.GetMachine() == llvm::Triple::arm 2992cdc22a88SMohit K. Bhakkad || m_arch.GetMachine() == llvm::Triple::mips64 || m_arch.GetMachine() == llvm::Triple::mips64el) 2993cdc22a88SMohit K. Bhakkad return false; 2994cdc22a88SMohit K. Bhakkad return true; 2995e7708688STamas Berghammer } 2996e7708688STamas Berghammer 2997af245d11STodd Fiala Error 2998af245d11STodd Fiala NativeProcessLinux::Resume (const ResumeActionList &resume_actions) 2999af245d11STodd Fiala { 3000af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); 3001af245d11STodd Fiala if (log) 3002af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s called: pid %" PRIu64, __FUNCTION__, GetID ()); 3003af245d11STodd Fiala 300403f12d6bSChaoren Lin lldb::tid_t deferred_signal_tid = LLDB_INVALID_THREAD_ID; 300503f12d6bSChaoren Lin lldb::tid_t deferred_signal_skip_tid = LLDB_INVALID_THREAD_ID; 3006ae29d395SChaoren Lin int deferred_signo = 0; 3007ae29d395SChaoren Lin NativeThreadProtocolSP deferred_signal_thread_sp; 300886fd8e45SChaoren Lin bool stepping = false; 3009e7708688STamas Berghammer bool software_single_step = !SupportHardwareSingleStepping(); 3010af245d11STodd Fiala 301145f5cb31SPavel Labath Monitor::ScopedOperationLock monitor_lock(*m_monitor_up); 3012af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 30135830aa75STamas Berghammer 3014e7708688STamas Berghammer if (software_single_step) 3015e7708688STamas Berghammer { 3016e7708688STamas Berghammer for (auto thread_sp : m_threads) 3017e7708688STamas Berghammer { 3018e7708688STamas Berghammer assert (thread_sp && "thread list should not contain NULL threads"); 3019e7708688STamas Berghammer 3020e7708688STamas Berghammer const ResumeAction *const action = resume_actions.GetActionForThread (thread_sp->GetID (), true); 3021e7708688STamas Berghammer if (action == nullptr) 3022e7708688STamas Berghammer continue; 3023e7708688STamas Berghammer 3024e7708688STamas Berghammer if (action->state == eStateStepping) 3025e7708688STamas Berghammer { 3026e7708688STamas Berghammer Error error = SetupSoftwareSingleStepping(thread_sp); 3027e7708688STamas Berghammer if (error.Fail()) 3028e7708688STamas Berghammer return error; 3029e7708688STamas Berghammer } 3030e7708688STamas Berghammer } 3031e7708688STamas Berghammer } 3032e7708688STamas Berghammer 3033af245d11STodd Fiala for (auto thread_sp : m_threads) 3034af245d11STodd Fiala { 3035af245d11STodd Fiala assert (thread_sp && "thread list should not contain NULL threads"); 3036af245d11STodd Fiala 3037af245d11STodd Fiala const ResumeAction *const action = resume_actions.GetActionForThread (thread_sp->GetID (), true); 30386a196ce6SChaoren Lin 30396a196ce6SChaoren Lin if (action == nullptr) 30406a196ce6SChaoren Lin { 30416a196ce6SChaoren Lin if (log) 30426a196ce6SChaoren Lin log->Printf ("NativeProcessLinux::%s no action specified for pid %" PRIu64 " tid %" PRIu64, 30436a196ce6SChaoren Lin __FUNCTION__, GetID (), thread_sp->GetID ()); 30446a196ce6SChaoren Lin continue; 30456a196ce6SChaoren Lin } 3046af245d11STodd Fiala 3047af245d11STodd Fiala if (log) 3048af245d11STodd Fiala { 3049af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s processing resume action state %s for pid %" PRIu64 " tid %" PRIu64, 3050af245d11STodd Fiala __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); 3051af245d11STodd Fiala } 3052af245d11STodd Fiala 3053af245d11STodd Fiala switch (action->state) 3054af245d11STodd Fiala { 3055af245d11STodd Fiala case eStateRunning: 3056fa03ad2eSChaoren Lin { 3057af245d11STodd Fiala // Run the thread, possibly feeding it the signal. 3058fa03ad2eSChaoren Lin const int signo = action->signal; 3059c076559aSPavel Labath RequestThreadResumeAsNeeded (thread_sp->GetID (), 306086fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 3061af245d11STodd Fiala { 3062cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); 3063fa03ad2eSChaoren Lin // Pass this signal number on to the inferior to handle. 30645830aa75STamas Berghammer const auto resume_result = Resume (tid_to_resume, (signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER); 30655830aa75STamas Berghammer if (resume_result.Success()) 30665830aa75STamas Berghammer SetState(eStateRunning, true); 30675830aa75STamas Berghammer return resume_result; 30685eb721edSPavel Labath }); 3069af245d11STodd Fiala break; 3070fa03ad2eSChaoren Lin } 3071af245d11STodd Fiala 3072af245d11STodd Fiala case eStateStepping: 3073af245d11STodd Fiala { 3074ae29d395SChaoren Lin // Request the step. 3075ae29d395SChaoren Lin const int signo = action->signal; 3076c076559aSPavel Labath RequestThreadResume (thread_sp->GetID (), 307786fd8e45SChaoren Lin [=](lldb::tid_t tid_to_step, bool supress_signal) 3078af245d11STodd Fiala { 3079cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStepping (); 3080e7708688STamas Berghammer 3081e7708688STamas Berghammer Error step_result; 3082e7708688STamas Berghammer if (software_single_step) 3083e7708688STamas Berghammer step_result = Resume (tid_to_step, (signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER); 3084e7708688STamas Berghammer else 3085e7708688STamas Berghammer step_result = SingleStep (tid_to_step,(signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER); 3086e7708688STamas Berghammer 308737c768caSChaoren Lin assert (step_result.Success() && "SingleStep() failed"); 30885830aa75STamas Berghammer if (step_result.Success()) 30895830aa75STamas Berghammer SetState(eStateStepping, true); 309037c768caSChaoren Lin return step_result; 30915eb721edSPavel Labath }); 309286fd8e45SChaoren Lin stepping = true; 3093af245d11STodd Fiala break; 3094ae29d395SChaoren Lin } 3095af245d11STodd Fiala 3096af245d11STodd Fiala case eStateSuspended: 3097af245d11STodd Fiala case eStateStopped: 3098ae29d395SChaoren Lin // if we haven't chosen a deferred signal tid yet, use this one. 3099ae29d395SChaoren Lin if (deferred_signal_tid == LLDB_INVALID_THREAD_ID) 3100ae29d395SChaoren Lin { 3101ae29d395SChaoren Lin deferred_signal_tid = thread_sp->GetID (); 3102ae29d395SChaoren Lin deferred_signal_thread_sp = thread_sp; 3103ae29d395SChaoren Lin deferred_signo = SIGSTOP; 3104ae29d395SChaoren Lin } 3105af245d11STodd Fiala break; 3106af245d11STodd Fiala 3107af245d11STodd Fiala default: 3108af245d11STodd Fiala return Error ("NativeProcessLinux::%s (): unexpected state %s specified for pid %" PRIu64 ", tid %" PRIu64, 3109af245d11STodd Fiala __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); 3110af245d11STodd Fiala } 3111af245d11STodd Fiala } 3112af245d11STodd Fiala 3113fa03ad2eSChaoren Lin // If we had any thread stopping, then do a deferred notification of the chosen stop thread id and signal 3114fa03ad2eSChaoren Lin // after all other running threads have stopped. 311586fd8e45SChaoren Lin // If there is a stepping thread involved we'll be eventually stopped by SIGTRAP trace signal. 311686fd8e45SChaoren Lin if (deferred_signal_tid != LLDB_INVALID_THREAD_ID && !stepping) 3117ed89c7feSPavel Labath StopRunningThreadsWithSkipTID(deferred_signal_tid, deferred_signal_skip_tid); 3118af245d11STodd Fiala 31195830aa75STamas Berghammer return Error(); 3120af245d11STodd Fiala } 3121af245d11STodd Fiala 3122af245d11STodd Fiala Error 3123af245d11STodd Fiala NativeProcessLinux::Halt () 3124af245d11STodd Fiala { 3125af245d11STodd Fiala Error error; 3126af245d11STodd Fiala 3127af245d11STodd Fiala if (kill (GetID (), SIGSTOP) != 0) 3128af245d11STodd Fiala error.SetErrorToErrno (); 3129af245d11STodd Fiala 3130af245d11STodd Fiala return error; 3131af245d11STodd Fiala } 3132af245d11STodd Fiala 3133af245d11STodd Fiala Error 3134af245d11STodd Fiala NativeProcessLinux::Detach () 3135af245d11STodd Fiala { 3136af245d11STodd Fiala Error error; 3137af245d11STodd Fiala 3138af245d11STodd Fiala // Tell ptrace to detach from the process. 3139af245d11STodd Fiala if (GetID () != LLDB_INVALID_PROCESS_ID) 3140af245d11STodd Fiala error = Detach (GetID ()); 3141af245d11STodd Fiala 3142af245d11STodd Fiala // Stop monitoring the inferior. 314345f5cb31SPavel Labath m_monitor_up->Terminate(); 3144af245d11STodd Fiala 3145af245d11STodd Fiala // No error. 3146af245d11STodd Fiala return error; 3147af245d11STodd Fiala } 3148af245d11STodd Fiala 3149af245d11STodd Fiala Error 3150af245d11STodd Fiala NativeProcessLinux::Signal (int signo) 3151af245d11STodd Fiala { 3152af245d11STodd Fiala Error error; 3153af245d11STodd Fiala 3154af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3155af245d11STodd Fiala if (log) 3156af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s: sending signal %d (%s) to pid %" PRIu64, 3157af245d11STodd Fiala __FUNCTION__, signo, GetUnixSignals ().GetSignalAsCString (signo), GetID ()); 3158af245d11STodd Fiala 3159af245d11STodd Fiala if (kill(GetID(), signo)) 3160af245d11STodd Fiala error.SetErrorToErrno(); 3161af245d11STodd Fiala 3162af245d11STodd Fiala return error; 3163af245d11STodd Fiala } 3164af245d11STodd Fiala 3165af245d11STodd Fiala Error 3166e9547b80SChaoren Lin NativeProcessLinux::Interrupt () 3167e9547b80SChaoren Lin { 3168e9547b80SChaoren Lin // Pick a running thread (or if none, a not-dead stopped thread) as 3169e9547b80SChaoren Lin // the chosen thread that will be the stop-reason thread. 3170e9547b80SChaoren Lin Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3171e9547b80SChaoren Lin 3172e9547b80SChaoren Lin NativeThreadProtocolSP running_thread_sp; 3173e9547b80SChaoren Lin NativeThreadProtocolSP stopped_thread_sp; 3174e9547b80SChaoren Lin 3175e9547b80SChaoren Lin if (log) 3176e9547b80SChaoren Lin log->Printf ("NativeProcessLinux::%s selecting running thread for interrupt target", __FUNCTION__); 3177e9547b80SChaoren Lin 317845f5cb31SPavel Labath Monitor::ScopedOperationLock monitor_lock(*m_monitor_up); 31795830aa75STamas Berghammer Mutex::Locker locker (m_threads_mutex); 31805830aa75STamas Berghammer 3181e9547b80SChaoren Lin for (auto thread_sp : m_threads) 3182e9547b80SChaoren Lin { 3183e9547b80SChaoren Lin // The thread shouldn't be null but lets just cover that here. 3184e9547b80SChaoren Lin if (!thread_sp) 3185e9547b80SChaoren Lin continue; 3186e9547b80SChaoren Lin 3187e9547b80SChaoren Lin // If we have a running or stepping thread, we'll call that the 3188e9547b80SChaoren Lin // target of the interrupt. 3189e9547b80SChaoren Lin const auto thread_state = thread_sp->GetState (); 3190e9547b80SChaoren Lin if (thread_state == eStateRunning || 3191e9547b80SChaoren Lin thread_state == eStateStepping) 3192e9547b80SChaoren Lin { 3193e9547b80SChaoren Lin running_thread_sp = thread_sp; 3194e9547b80SChaoren Lin break; 3195e9547b80SChaoren Lin } 3196e9547b80SChaoren Lin else if (!stopped_thread_sp && StateIsStoppedState (thread_state, true)) 3197e9547b80SChaoren Lin { 3198e9547b80SChaoren Lin // Remember the first non-dead stopped thread. We'll use that as a backup if there are no running threads. 3199e9547b80SChaoren Lin stopped_thread_sp = thread_sp; 3200e9547b80SChaoren Lin } 3201e9547b80SChaoren Lin } 3202e9547b80SChaoren Lin 3203e9547b80SChaoren Lin if (!running_thread_sp && !stopped_thread_sp) 3204e9547b80SChaoren Lin { 32055830aa75STamas Berghammer Error error("found no running/stepping or live stopped threads as target for interrupt"); 3206e9547b80SChaoren Lin if (log) 3207e9547b80SChaoren Lin log->Printf ("NativeProcessLinux::%s skipping due to error: %s", __FUNCTION__, error.AsCString ()); 32085830aa75STamas Berghammer 3209e9547b80SChaoren Lin return error; 3210e9547b80SChaoren Lin } 3211e9547b80SChaoren Lin 3212e9547b80SChaoren Lin NativeThreadProtocolSP deferred_signal_thread_sp = running_thread_sp ? running_thread_sp : stopped_thread_sp; 3213e9547b80SChaoren Lin 3214e9547b80SChaoren Lin if (log) 3215e9547b80SChaoren Lin log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " %s tid %" PRIu64 " chosen for interrupt target", 3216e9547b80SChaoren Lin __FUNCTION__, 3217e9547b80SChaoren Lin GetID (), 3218e9547b80SChaoren Lin running_thread_sp ? "running" : "stopped", 3219e9547b80SChaoren Lin deferred_signal_thread_sp->GetID ()); 3220e9547b80SChaoren Lin 3221ed89c7feSPavel Labath StopRunningThreads(deferred_signal_thread_sp->GetID()); 322245f5cb31SPavel Labath 32235830aa75STamas Berghammer return Error(); 3224e9547b80SChaoren Lin } 3225e9547b80SChaoren Lin 3226e9547b80SChaoren Lin Error 3227af245d11STodd Fiala NativeProcessLinux::Kill () 3228af245d11STodd Fiala { 3229af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3230af245d11STodd Fiala if (log) 3231af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s called for PID %" PRIu64, __FUNCTION__, GetID ()); 3232af245d11STodd Fiala 3233af245d11STodd Fiala Error error; 3234af245d11STodd Fiala 3235af245d11STodd Fiala switch (m_state) 3236af245d11STodd Fiala { 3237af245d11STodd Fiala case StateType::eStateInvalid: 3238af245d11STodd Fiala case StateType::eStateExited: 3239af245d11STodd Fiala case StateType::eStateCrashed: 3240af245d11STodd Fiala case StateType::eStateDetached: 3241af245d11STodd Fiala case StateType::eStateUnloaded: 3242af245d11STodd Fiala // Nothing to do - the process is already dead. 3243af245d11STodd Fiala if (log) 3244af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s ignored for PID %" PRIu64 " due to current state: %s", __FUNCTION__, GetID (), StateAsCString (m_state)); 3245af245d11STodd Fiala return error; 3246af245d11STodd Fiala 3247af245d11STodd Fiala case StateType::eStateConnected: 3248af245d11STodd Fiala case StateType::eStateAttaching: 3249af245d11STodd Fiala case StateType::eStateLaunching: 3250af245d11STodd Fiala case StateType::eStateStopped: 3251af245d11STodd Fiala case StateType::eStateRunning: 3252af245d11STodd Fiala case StateType::eStateStepping: 3253af245d11STodd Fiala case StateType::eStateSuspended: 3254af245d11STodd Fiala // We can try to kill a process in these states. 3255af245d11STodd Fiala break; 3256af245d11STodd Fiala } 3257af245d11STodd Fiala 3258af245d11STodd Fiala if (kill (GetID (), SIGKILL) != 0) 3259af245d11STodd Fiala { 3260af245d11STodd Fiala error.SetErrorToErrno (); 3261af245d11STodd Fiala return error; 3262af245d11STodd Fiala } 3263af245d11STodd Fiala 3264af245d11STodd Fiala return error; 3265af245d11STodd Fiala } 3266af245d11STodd Fiala 3267af245d11STodd Fiala static Error 3268af245d11STodd Fiala ParseMemoryRegionInfoFromProcMapsLine (const std::string &maps_line, MemoryRegionInfo &memory_region_info) 3269af245d11STodd Fiala { 3270af245d11STodd Fiala memory_region_info.Clear(); 3271af245d11STodd Fiala 3272af245d11STodd Fiala StringExtractor line_extractor (maps_line.c_str ()); 3273af245d11STodd Fiala 3274af245d11STodd Fiala // Format: {address_start_hex}-{address_end_hex} perms offset dev inode pathname 3275af245d11STodd Fiala // perms: rwxp (letter is present if set, '-' if not, final character is p=private, s=shared). 3276af245d11STodd Fiala 3277af245d11STodd Fiala // Parse out the starting address 3278af245d11STodd Fiala lldb::addr_t start_address = line_extractor.GetHexMaxU64 (false, 0); 3279af245d11STodd Fiala 3280af245d11STodd Fiala // Parse out hyphen separating start and end address from range. 3281af245d11STodd Fiala if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != '-')) 3282af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing dash between address range"); 3283af245d11STodd Fiala 3284af245d11STodd Fiala // Parse out the ending address 3285af245d11STodd Fiala lldb::addr_t end_address = line_extractor.GetHexMaxU64 (false, start_address); 3286af245d11STodd Fiala 3287af245d11STodd Fiala // Parse out the space after the address. 3288af245d11STodd Fiala if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != ' ')) 3289af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing space after range"); 3290af245d11STodd Fiala 3291af245d11STodd Fiala // Save the range. 3292af245d11STodd Fiala memory_region_info.GetRange ().SetRangeBase (start_address); 3293af245d11STodd Fiala memory_region_info.GetRange ().SetRangeEnd (end_address); 3294af245d11STodd Fiala 3295af245d11STodd Fiala // Parse out each permission entry. 3296af245d11STodd Fiala if (line_extractor.GetBytesLeft () < 4) 3297af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing some portion of permissions"); 3298af245d11STodd Fiala 3299af245d11STodd Fiala // Handle read permission. 3300af245d11STodd Fiala const char read_perm_char = line_extractor.GetChar (); 3301af245d11STodd Fiala if (read_perm_char == 'r') 3302af245d11STodd Fiala memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eYes); 3303af245d11STodd Fiala else 3304af245d11STodd Fiala { 3305af245d11STodd Fiala assert ( (read_perm_char == '-') && "unexpected /proc/{pid}/maps read permission char" ); 3306af245d11STodd Fiala memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); 3307af245d11STodd Fiala } 3308af245d11STodd Fiala 3309af245d11STodd Fiala // Handle write permission. 3310af245d11STodd Fiala const char write_perm_char = line_extractor.GetChar (); 3311af245d11STodd Fiala if (write_perm_char == 'w') 3312af245d11STodd Fiala memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eYes); 3313af245d11STodd Fiala else 3314af245d11STodd Fiala { 3315af245d11STodd Fiala assert ( (write_perm_char == '-') && "unexpected /proc/{pid}/maps write permission char" ); 3316af245d11STodd Fiala memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); 3317af245d11STodd Fiala } 3318af245d11STodd Fiala 3319af245d11STodd Fiala // Handle execute permission. 3320af245d11STodd Fiala const char exec_perm_char = line_extractor.GetChar (); 3321af245d11STodd Fiala if (exec_perm_char == 'x') 3322af245d11STodd Fiala memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eYes); 3323af245d11STodd Fiala else 3324af245d11STodd Fiala { 3325af245d11STodd Fiala assert ( (exec_perm_char == '-') && "unexpected /proc/{pid}/maps exec permission char" ); 3326af245d11STodd Fiala memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); 3327af245d11STodd Fiala } 3328af245d11STodd Fiala 3329af245d11STodd Fiala return Error (); 3330af245d11STodd Fiala } 3331af245d11STodd Fiala 3332af245d11STodd Fiala Error 3333af245d11STodd Fiala NativeProcessLinux::GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) 3334af245d11STodd Fiala { 3335af245d11STodd Fiala // FIXME review that the final memory region returned extends to the end of the virtual address space, 3336af245d11STodd Fiala // with no perms if it is not mapped. 3337af245d11STodd Fiala 3338af245d11STodd Fiala // Use an approach that reads memory regions from /proc/{pid}/maps. 3339af245d11STodd Fiala // Assume proc maps entries are in ascending order. 3340af245d11STodd Fiala // FIXME assert if we find differently. 3341af245d11STodd Fiala Mutex::Locker locker (m_mem_region_cache_mutex); 3342af245d11STodd Fiala 3343af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3344af245d11STodd Fiala Error error; 3345af245d11STodd Fiala 3346af245d11STodd Fiala if (m_supports_mem_region == LazyBool::eLazyBoolNo) 3347af245d11STodd Fiala { 3348af245d11STodd Fiala // We're done. 3349af245d11STodd Fiala error.SetErrorString ("unsupported"); 3350af245d11STodd Fiala return error; 3351af245d11STodd Fiala } 3352af245d11STodd Fiala 3353af245d11STodd Fiala // If our cache is empty, pull the latest. There should always be at least one memory region 3354af245d11STodd Fiala // if memory region handling is supported. 3355af245d11STodd Fiala if (m_mem_region_cache.empty ()) 3356af245d11STodd Fiala { 3357af245d11STodd Fiala error = ProcFileReader::ProcessLineByLine (GetID (), "maps", 3358af245d11STodd Fiala [&] (const std::string &line) -> bool 3359af245d11STodd Fiala { 3360af245d11STodd Fiala MemoryRegionInfo info; 3361af245d11STodd Fiala const Error parse_error = ParseMemoryRegionInfoFromProcMapsLine (line, info); 3362af245d11STodd Fiala if (parse_error.Success ()) 3363af245d11STodd Fiala { 3364af245d11STodd Fiala m_mem_region_cache.push_back (info); 3365af245d11STodd Fiala return true; 3366af245d11STodd Fiala } 3367af245d11STodd Fiala else 3368af245d11STodd Fiala { 3369af245d11STodd Fiala if (log) 3370af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to parse proc maps line '%s': %s", __FUNCTION__, line.c_str (), error.AsCString ()); 3371af245d11STodd Fiala return false; 3372af245d11STodd Fiala } 3373af245d11STodd Fiala }); 3374af245d11STodd Fiala 3375af245d11STodd Fiala // If we had an error, we'll mark unsupported. 3376af245d11STodd Fiala if (error.Fail ()) 3377af245d11STodd Fiala { 3378af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolNo; 3379af245d11STodd Fiala return error; 3380af245d11STodd Fiala } 3381af245d11STodd Fiala else if (m_mem_region_cache.empty ()) 3382af245d11STodd Fiala { 3383af245d11STodd Fiala // No entries after attempting to read them. This shouldn't happen if /proc/{pid}/maps 3384af245d11STodd Fiala // is supported. Assume we don't support map entries via procfs. 3385af245d11STodd Fiala if (log) 3386af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to find any procfs maps entries, assuming no support for memory region metadata retrieval", __FUNCTION__); 3387af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolNo; 3388af245d11STodd Fiala error.SetErrorString ("not supported"); 3389af245d11STodd Fiala return error; 3390af245d11STodd Fiala } 3391af245d11STodd Fiala 3392af245d11STodd Fiala if (log) 3393af245d11STodd 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 ()); 3394af245d11STodd Fiala 3395af245d11STodd Fiala // We support memory retrieval, remember that. 3396af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolYes; 3397af245d11STodd Fiala } 3398af245d11STodd Fiala else 3399af245d11STodd Fiala { 3400af245d11STodd Fiala if (log) 3401af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s reusing %" PRIu64 " cached memory region entries", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); 3402af245d11STodd Fiala } 3403af245d11STodd Fiala 3404af245d11STodd Fiala lldb::addr_t prev_base_address = 0; 3405af245d11STodd Fiala 3406af245d11STodd Fiala // FIXME start by finding the last region that is <= target address using binary search. Data is sorted. 3407af245d11STodd Fiala // There can be a ton of regions on pthreads apps with lots of threads. 3408af245d11STodd Fiala for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end (); ++it) 3409af245d11STodd Fiala { 3410af245d11STodd Fiala MemoryRegionInfo &proc_entry_info = *it; 3411af245d11STodd Fiala 3412af245d11STodd Fiala // Sanity check assumption that /proc/{pid}/maps entries are ascending. 3413af245d11STodd Fiala assert ((proc_entry_info.GetRange ().GetRangeBase () >= prev_base_address) && "descending /proc/pid/maps entries detected, unexpected"); 3414af245d11STodd Fiala prev_base_address = proc_entry_info.GetRange ().GetRangeBase (); 3415af245d11STodd Fiala 3416af245d11STodd Fiala // If the target address comes before this entry, indicate distance to next region. 3417af245d11STodd Fiala if (load_addr < proc_entry_info.GetRange ().GetRangeBase ()) 3418af245d11STodd Fiala { 3419af245d11STodd Fiala range_info.GetRange ().SetRangeBase (load_addr); 3420af245d11STodd Fiala range_info.GetRange ().SetByteSize (proc_entry_info.GetRange ().GetRangeBase () - load_addr); 3421af245d11STodd Fiala range_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); 3422af245d11STodd Fiala range_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); 3423af245d11STodd Fiala range_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); 3424af245d11STodd Fiala 3425af245d11STodd Fiala return error; 3426af245d11STodd Fiala } 3427af245d11STodd Fiala else if (proc_entry_info.GetRange ().Contains (load_addr)) 3428af245d11STodd Fiala { 3429af245d11STodd Fiala // The target address is within the memory region we're processing here. 3430af245d11STodd Fiala range_info = proc_entry_info; 3431af245d11STodd Fiala return error; 3432af245d11STodd Fiala } 3433af245d11STodd Fiala 3434af245d11STodd Fiala // The target memory address comes somewhere after the region we just parsed. 3435af245d11STodd Fiala } 3436af245d11STodd Fiala 3437af245d11STodd Fiala // If we made it here, we didn't find an entry that contained the given address. 3438af245d11STodd Fiala error.SetErrorString ("address comes after final region"); 3439af245d11STodd Fiala 3440af245d11STodd Fiala if (log) 3441af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to find map entry for address 0x%" PRIx64 ": %s", __FUNCTION__, load_addr, error.AsCString ()); 3442af245d11STodd Fiala 3443af245d11STodd Fiala return error; 3444af245d11STodd Fiala } 3445af245d11STodd Fiala 3446af245d11STodd Fiala void 3447af245d11STodd Fiala NativeProcessLinux::DoStopIDBumped (uint32_t newBumpId) 3448af245d11STodd Fiala { 3449af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3450af245d11STodd Fiala if (log) 3451af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(newBumpId=%" PRIu32 ") called", __FUNCTION__, newBumpId); 3452af245d11STodd Fiala 3453af245d11STodd Fiala { 3454af245d11STodd Fiala Mutex::Locker locker (m_mem_region_cache_mutex); 3455af245d11STodd Fiala if (log) 3456af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s clearing %" PRIu64 " entries from the cache", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); 3457af245d11STodd Fiala m_mem_region_cache.clear (); 3458af245d11STodd Fiala } 3459af245d11STodd Fiala } 3460af245d11STodd Fiala 3461af245d11STodd Fiala Error 34623eb4b458SChaoren Lin NativeProcessLinux::AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) 3463af245d11STodd Fiala { 3464af245d11STodd Fiala // FIXME implementing this requires the equivalent of 3465af245d11STodd Fiala // InferiorCallPOSIX::InferiorCallMmap, which depends on 3466af245d11STodd Fiala // functional ThreadPlans working with Native*Protocol. 3467af245d11STodd Fiala #if 1 3468af245d11STodd Fiala return Error ("not implemented yet"); 3469af245d11STodd Fiala #else 3470af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 3471af245d11STodd Fiala 3472af245d11STodd Fiala unsigned prot = 0; 3473af245d11STodd Fiala if (permissions & lldb::ePermissionsReadable) 3474af245d11STodd Fiala prot |= eMmapProtRead; 3475af245d11STodd Fiala if (permissions & lldb::ePermissionsWritable) 3476af245d11STodd Fiala prot |= eMmapProtWrite; 3477af245d11STodd Fiala if (permissions & lldb::ePermissionsExecutable) 3478af245d11STodd Fiala prot |= eMmapProtExec; 3479af245d11STodd Fiala 3480af245d11STodd Fiala // TODO implement this directly in NativeProcessLinux 3481af245d11STodd Fiala // (and lift to NativeProcessPOSIX if/when that class is 3482af245d11STodd Fiala // refactored out). 3483af245d11STodd Fiala if (InferiorCallMmap(this, addr, 0, size, prot, 3484af245d11STodd Fiala eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { 3485af245d11STodd Fiala m_addr_to_mmap_size[addr] = size; 3486af245d11STodd Fiala return Error (); 3487af245d11STodd Fiala } else { 3488af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 3489af245d11STodd Fiala return Error("unable to allocate %" PRIu64 " bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions)); 3490af245d11STodd Fiala } 3491af245d11STodd Fiala #endif 3492af245d11STodd Fiala } 3493af245d11STodd Fiala 3494af245d11STodd Fiala Error 3495af245d11STodd Fiala NativeProcessLinux::DeallocateMemory (lldb::addr_t addr) 3496af245d11STodd Fiala { 3497af245d11STodd Fiala // FIXME see comments in AllocateMemory - required lower-level 3498af245d11STodd Fiala // bits not in place yet (ThreadPlans) 3499af245d11STodd Fiala return Error ("not implemented"); 3500af245d11STodd Fiala } 3501af245d11STodd Fiala 3502af245d11STodd Fiala lldb::addr_t 3503af245d11STodd Fiala NativeProcessLinux::GetSharedLibraryInfoAddress () 3504af245d11STodd Fiala { 3505af245d11STodd Fiala #if 1 3506af245d11STodd Fiala // punt on this for now 3507af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3508af245d11STodd Fiala #else 3509af245d11STodd Fiala // Return the image info address for the exe module 3510af245d11STodd Fiala #if 1 3511af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3512af245d11STodd Fiala 3513af245d11STodd Fiala ModuleSP module_sp; 3514af245d11STodd Fiala Error error = GetExeModuleSP (module_sp); 3515af245d11STodd Fiala if (error.Fail ()) 3516af245d11STodd Fiala { 3517af245d11STodd Fiala if (log) 3518af245d11STodd Fiala log->Warning ("NativeProcessLinux::%s failed to retrieve exe module: %s", __FUNCTION__, error.AsCString ()); 3519af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3520af245d11STodd Fiala } 3521af245d11STodd Fiala 3522af245d11STodd Fiala if (module_sp == nullptr) 3523af245d11STodd Fiala { 3524af245d11STodd Fiala if (log) 3525af245d11STodd Fiala log->Warning ("NativeProcessLinux::%s exe module returned was NULL", __FUNCTION__); 3526af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3527af245d11STodd Fiala } 3528af245d11STodd Fiala 3529af245d11STodd Fiala ObjectFileSP object_file_sp = module_sp->GetObjectFile (); 3530af245d11STodd Fiala if (object_file_sp == nullptr) 3531af245d11STodd Fiala { 3532af245d11STodd Fiala if (log) 3533af245d11STodd Fiala log->Warning ("NativeProcessLinux::%s exe module returned a NULL object file", __FUNCTION__); 3534af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3535af245d11STodd Fiala } 3536af245d11STodd Fiala 3537af245d11STodd Fiala return obj_file_sp->GetImageInfoAddress(); 3538af245d11STodd Fiala #else 3539af245d11STodd Fiala Target *target = &GetTarget(); 3540af245d11STodd Fiala ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile(); 3541af245d11STodd Fiala Address addr = obj_file->GetImageInfoAddress(target); 3542af245d11STodd Fiala 3543af245d11STodd Fiala if (addr.IsValid()) 3544af245d11STodd Fiala return addr.GetLoadAddress(target); 3545af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3546af245d11STodd Fiala #endif 3547af245d11STodd Fiala #endif // punt on this for now 3548af245d11STodd Fiala } 3549af245d11STodd Fiala 3550af245d11STodd Fiala size_t 3551af245d11STodd Fiala NativeProcessLinux::UpdateThreads () 3552af245d11STodd Fiala { 3553af245d11STodd Fiala // The NativeProcessLinux monitoring threads are always up to date 3554af245d11STodd Fiala // with respect to thread state and they keep the thread list 3555af245d11STodd Fiala // populated properly. All this method needs to do is return the 3556af245d11STodd Fiala // thread count. 3557af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 3558af245d11STodd Fiala return m_threads.size (); 3559af245d11STodd Fiala } 3560af245d11STodd Fiala 3561af245d11STodd Fiala bool 3562af245d11STodd Fiala NativeProcessLinux::GetArchitecture (ArchSpec &arch) const 3563af245d11STodd Fiala { 3564af245d11STodd Fiala arch = m_arch; 3565af245d11STodd Fiala return true; 3566af245d11STodd Fiala } 3567af245d11STodd Fiala 3568af245d11STodd Fiala Error 356963c8be95STamas Berghammer NativeProcessLinux::GetSoftwareBreakpointPCOffset (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size) 3570af245d11STodd Fiala { 3571af245d11STodd Fiala // FIXME put this behind a breakpoint protocol class that can be 3572af245d11STodd Fiala // set per architecture. Need ARM, MIPS support here. 35732afc5966STodd Fiala static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; 3574af245d11STodd Fiala static const uint8_t g_i386_opcode [] = { 0xCC }; 3575e8659b5dSMohit K. Bhakkad static const uint8_t g_mips64_opcode[] = { 0x00, 0x00, 0x00, 0x0d }; 3576af245d11STodd Fiala 3577af245d11STodd Fiala switch (m_arch.GetMachine ()) 3578af245d11STodd Fiala { 35792afc5966STodd Fiala case llvm::Triple::aarch64: 35802afc5966STodd Fiala actual_opcode_size = static_cast<uint32_t> (sizeof(g_aarch64_opcode)); 35812afc5966STodd Fiala return Error (); 35822afc5966STodd Fiala 358363c8be95STamas Berghammer case llvm::Triple::arm: 358463c8be95STamas Berghammer actual_opcode_size = 0; // On arm the PC don't get updated for breakpoint hits 358563c8be95STamas Berghammer return Error (); 358663c8be95STamas Berghammer 3587af245d11STodd Fiala case llvm::Triple::x86: 3588af245d11STodd Fiala case llvm::Triple::x86_64: 3589af245d11STodd Fiala actual_opcode_size = static_cast<uint32_t> (sizeof(g_i386_opcode)); 3590af245d11STodd Fiala return Error (); 3591af245d11STodd Fiala 3592e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64: 3593e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64el: 3594e8659b5dSMohit K. Bhakkad actual_opcode_size = static_cast<uint32_t> (sizeof(g_mips64_opcode)); 3595e8659b5dSMohit K. Bhakkad return Error (); 3596e8659b5dSMohit K. Bhakkad 3597af245d11STodd Fiala default: 3598af245d11STodd Fiala assert(false && "CPU type not supported!"); 3599af245d11STodd Fiala return Error ("CPU type not supported"); 3600af245d11STodd Fiala } 3601af245d11STodd Fiala } 3602af245d11STodd Fiala 3603af245d11STodd Fiala Error 3604af245d11STodd Fiala NativeProcessLinux::SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) 3605af245d11STodd Fiala { 3606af245d11STodd Fiala if (hardware) 3607af245d11STodd Fiala return Error ("NativeProcessLinux does not support hardware breakpoints"); 3608af245d11STodd Fiala else 3609af245d11STodd Fiala return SetSoftwareBreakpoint (addr, size); 3610af245d11STodd Fiala } 3611af245d11STodd Fiala 3612af245d11STodd Fiala Error 361363c8be95STamas Berghammer NativeProcessLinux::GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, 361463c8be95STamas Berghammer size_t &actual_opcode_size, 361563c8be95STamas Berghammer const uint8_t *&trap_opcode_bytes) 3616af245d11STodd Fiala { 361763c8be95STamas Berghammer // FIXME put this behind a breakpoint protocol class that can be set per 361863c8be95STamas Berghammer // architecture. Need MIPS support here. 36192afc5966STodd Fiala static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; 362063c8be95STamas Berghammer // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the 362163c8be95STamas Berghammer // linux kernel does otherwise. 362263c8be95STamas Berghammer static const uint8_t g_arm_breakpoint_opcode[] = { 0xf0, 0x01, 0xf0, 0xe7 }; 3623af245d11STodd Fiala static const uint8_t g_i386_opcode [] = { 0xCC }; 36243df471c3SMohit K. Bhakkad static const uint8_t g_mips64_opcode[] = { 0x00, 0x00, 0x00, 0x0d }; 36252c2acf96SMohit K. Bhakkad static const uint8_t g_mips64el_opcode[] = { 0x0d, 0x00, 0x00, 0x00 }; 362663c8be95STamas Berghammer static const uint8_t g_thumb_breakpoint_opcode[] = { 0x01, 0xde }; 3627af245d11STodd Fiala 3628af245d11STodd Fiala switch (m_arch.GetMachine ()) 3629af245d11STodd Fiala { 36302afc5966STodd Fiala case llvm::Triple::aarch64: 36312afc5966STodd Fiala trap_opcode_bytes = g_aarch64_opcode; 36322afc5966STodd Fiala actual_opcode_size = sizeof(g_aarch64_opcode); 36332afc5966STodd Fiala return Error (); 36342afc5966STodd Fiala 363563c8be95STamas Berghammer case llvm::Triple::arm: 363663c8be95STamas Berghammer switch (trap_opcode_size_hint) 363763c8be95STamas Berghammer { 363863c8be95STamas Berghammer case 2: 363963c8be95STamas Berghammer trap_opcode_bytes = g_thumb_breakpoint_opcode; 364063c8be95STamas Berghammer actual_opcode_size = sizeof(g_thumb_breakpoint_opcode); 364163c8be95STamas Berghammer return Error (); 364263c8be95STamas Berghammer case 4: 364363c8be95STamas Berghammer trap_opcode_bytes = g_arm_breakpoint_opcode; 364463c8be95STamas Berghammer actual_opcode_size = sizeof(g_arm_breakpoint_opcode); 364563c8be95STamas Berghammer return Error (); 364663c8be95STamas Berghammer default: 364763c8be95STamas Berghammer assert(false && "Unrecognised trap opcode size hint!"); 364863c8be95STamas Berghammer return Error ("Unrecognised trap opcode size hint!"); 364963c8be95STamas Berghammer } 365063c8be95STamas Berghammer 3651af245d11STodd Fiala case llvm::Triple::x86: 3652af245d11STodd Fiala case llvm::Triple::x86_64: 3653af245d11STodd Fiala trap_opcode_bytes = g_i386_opcode; 3654af245d11STodd Fiala actual_opcode_size = sizeof(g_i386_opcode); 3655af245d11STodd Fiala return Error (); 3656af245d11STodd Fiala 36573df471c3SMohit K. Bhakkad case llvm::Triple::mips64: 36583df471c3SMohit K. Bhakkad trap_opcode_bytes = g_mips64_opcode; 36593df471c3SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64_opcode); 36603df471c3SMohit K. Bhakkad return Error (); 36613df471c3SMohit K. Bhakkad 36622c2acf96SMohit K. Bhakkad case llvm::Triple::mips64el: 36632c2acf96SMohit K. Bhakkad trap_opcode_bytes = g_mips64el_opcode; 36642c2acf96SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64el_opcode); 36652c2acf96SMohit K. Bhakkad return Error (); 36662c2acf96SMohit K. Bhakkad 3667af245d11STodd Fiala default: 3668af245d11STodd Fiala assert(false && "CPU type not supported!"); 3669af245d11STodd Fiala return Error ("CPU type not supported"); 3670af245d11STodd Fiala } 3671af245d11STodd Fiala } 3672af245d11STodd Fiala 3673af245d11STodd Fiala #if 0 3674af245d11STodd Fiala ProcessMessage::CrashReason 3675af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info) 3676af245d11STodd Fiala { 3677af245d11STodd Fiala ProcessMessage::CrashReason reason; 3678af245d11STodd Fiala assert(info->si_signo == SIGSEGV); 3679af245d11STodd Fiala 3680af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 3681af245d11STodd Fiala 3682af245d11STodd Fiala switch (info->si_code) 3683af245d11STodd Fiala { 3684af245d11STodd Fiala default: 3685af245d11STodd Fiala assert(false && "unexpected si_code for SIGSEGV"); 3686af245d11STodd Fiala break; 3687af245d11STodd Fiala case SI_KERNEL: 3688af245d11STodd Fiala // Linux will occasionally send spurious SI_KERNEL codes. 3689af245d11STodd Fiala // (this is poorly documented in sigaction) 3690af245d11STodd Fiala // One way to get this is via unaligned SIMD loads. 3691af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; // for lack of anything better 3692af245d11STodd Fiala break; 3693af245d11STodd Fiala case SEGV_MAPERR: 3694af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; 3695af245d11STodd Fiala break; 3696af245d11STodd Fiala case SEGV_ACCERR: 3697af245d11STodd Fiala reason = ProcessMessage::ePrivilegedAddress; 3698af245d11STodd Fiala break; 3699af245d11STodd Fiala } 3700af245d11STodd Fiala 3701af245d11STodd Fiala return reason; 3702af245d11STodd Fiala } 3703af245d11STodd Fiala #endif 3704af245d11STodd Fiala 3705af245d11STodd Fiala 3706af245d11STodd Fiala #if 0 3707af245d11STodd Fiala ProcessMessage::CrashReason 3708af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info) 3709af245d11STodd Fiala { 3710af245d11STodd Fiala ProcessMessage::CrashReason reason; 3711af245d11STodd Fiala assert(info->si_signo == SIGILL); 3712af245d11STodd Fiala 3713af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 3714af245d11STodd Fiala 3715af245d11STodd Fiala switch (info->si_code) 3716af245d11STodd Fiala { 3717af245d11STodd Fiala default: 3718af245d11STodd Fiala assert(false && "unexpected si_code for SIGILL"); 3719af245d11STodd Fiala break; 3720af245d11STodd Fiala case ILL_ILLOPC: 3721af245d11STodd Fiala reason = ProcessMessage::eIllegalOpcode; 3722af245d11STodd Fiala break; 3723af245d11STodd Fiala case ILL_ILLOPN: 3724af245d11STodd Fiala reason = ProcessMessage::eIllegalOperand; 3725af245d11STodd Fiala break; 3726af245d11STodd Fiala case ILL_ILLADR: 3727af245d11STodd Fiala reason = ProcessMessage::eIllegalAddressingMode; 3728af245d11STodd Fiala break; 3729af245d11STodd Fiala case ILL_ILLTRP: 3730af245d11STodd Fiala reason = ProcessMessage::eIllegalTrap; 3731af245d11STodd Fiala break; 3732af245d11STodd Fiala case ILL_PRVOPC: 3733af245d11STodd Fiala reason = ProcessMessage::ePrivilegedOpcode; 3734af245d11STodd Fiala break; 3735af245d11STodd Fiala case ILL_PRVREG: 3736af245d11STodd Fiala reason = ProcessMessage::ePrivilegedRegister; 3737af245d11STodd Fiala break; 3738af245d11STodd Fiala case ILL_COPROC: 3739af245d11STodd Fiala reason = ProcessMessage::eCoprocessorError; 3740af245d11STodd Fiala break; 3741af245d11STodd Fiala case ILL_BADSTK: 3742af245d11STodd Fiala reason = ProcessMessage::eInternalStackError; 3743af245d11STodd Fiala break; 3744af245d11STodd Fiala } 3745af245d11STodd Fiala 3746af245d11STodd Fiala return reason; 3747af245d11STodd Fiala } 3748af245d11STodd Fiala #endif 3749af245d11STodd Fiala 3750af245d11STodd Fiala #if 0 3751af245d11STodd Fiala ProcessMessage::CrashReason 3752af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info) 3753af245d11STodd Fiala { 3754af245d11STodd Fiala ProcessMessage::CrashReason reason; 3755af245d11STodd Fiala assert(info->si_signo == SIGFPE); 3756af245d11STodd Fiala 3757af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 3758af245d11STodd Fiala 3759af245d11STodd Fiala switch (info->si_code) 3760af245d11STodd Fiala { 3761af245d11STodd Fiala default: 3762af245d11STodd Fiala assert(false && "unexpected si_code for SIGFPE"); 3763af245d11STodd Fiala break; 3764af245d11STodd Fiala case FPE_INTDIV: 3765af245d11STodd Fiala reason = ProcessMessage::eIntegerDivideByZero; 3766af245d11STodd Fiala break; 3767af245d11STodd Fiala case FPE_INTOVF: 3768af245d11STodd Fiala reason = ProcessMessage::eIntegerOverflow; 3769af245d11STodd Fiala break; 3770af245d11STodd Fiala case FPE_FLTDIV: 3771af245d11STodd Fiala reason = ProcessMessage::eFloatDivideByZero; 3772af245d11STodd Fiala break; 3773af245d11STodd Fiala case FPE_FLTOVF: 3774af245d11STodd Fiala reason = ProcessMessage::eFloatOverflow; 3775af245d11STodd Fiala break; 3776af245d11STodd Fiala case FPE_FLTUND: 3777af245d11STodd Fiala reason = ProcessMessage::eFloatUnderflow; 3778af245d11STodd Fiala break; 3779af245d11STodd Fiala case FPE_FLTRES: 3780af245d11STodd Fiala reason = ProcessMessage::eFloatInexactResult; 3781af245d11STodd Fiala break; 3782af245d11STodd Fiala case FPE_FLTINV: 3783af245d11STodd Fiala reason = ProcessMessage::eFloatInvalidOperation; 3784af245d11STodd Fiala break; 3785af245d11STodd Fiala case FPE_FLTSUB: 3786af245d11STodd Fiala reason = ProcessMessage::eFloatSubscriptRange; 3787af245d11STodd Fiala break; 3788af245d11STodd Fiala } 3789af245d11STodd Fiala 3790af245d11STodd Fiala return reason; 3791af245d11STodd Fiala } 3792af245d11STodd Fiala #endif 3793af245d11STodd Fiala 3794af245d11STodd Fiala #if 0 3795af245d11STodd Fiala ProcessMessage::CrashReason 3796af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info) 3797af245d11STodd Fiala { 3798af245d11STodd Fiala ProcessMessage::CrashReason reason; 3799af245d11STodd Fiala assert(info->si_signo == SIGBUS); 3800af245d11STodd Fiala 3801af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 3802af245d11STodd Fiala 3803af245d11STodd Fiala switch (info->si_code) 3804af245d11STodd Fiala { 3805af245d11STodd Fiala default: 3806af245d11STodd Fiala assert(false && "unexpected si_code for SIGBUS"); 3807af245d11STodd Fiala break; 3808af245d11STodd Fiala case BUS_ADRALN: 3809af245d11STodd Fiala reason = ProcessMessage::eIllegalAlignment; 3810af245d11STodd Fiala break; 3811af245d11STodd Fiala case BUS_ADRERR: 3812af245d11STodd Fiala reason = ProcessMessage::eIllegalAddress; 3813af245d11STodd Fiala break; 3814af245d11STodd Fiala case BUS_OBJERR: 3815af245d11STodd Fiala reason = ProcessMessage::eHardwareError; 3816af245d11STodd Fiala break; 3817af245d11STodd Fiala } 3818af245d11STodd Fiala 3819af245d11STodd Fiala return reason; 3820af245d11STodd Fiala } 3821af245d11STodd Fiala #endif 3822af245d11STodd Fiala 3823af245d11STodd Fiala Error 382445f5cb31SPavel Labath NativeProcessLinux::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) 382545f5cb31SPavel Labath { 382645f5cb31SPavel Labath // The base SetWatchpoint will end up executing monitor operations. Let's lock the monitor 382745f5cb31SPavel Labath // for it. 382845f5cb31SPavel Labath Monitor::ScopedOperationLock monitor_lock(*m_monitor_up); 382945f5cb31SPavel Labath return NativeProcessProtocol::SetWatchpoint(addr, size, watch_flags, hardware); 383045f5cb31SPavel Labath } 383145f5cb31SPavel Labath 383245f5cb31SPavel Labath Error 383345f5cb31SPavel Labath NativeProcessLinux::RemoveWatchpoint (lldb::addr_t addr) 383445f5cb31SPavel Labath { 383545f5cb31SPavel Labath // The base RemoveWatchpoint will end up executing monitor operations. Let's lock the monitor 383645f5cb31SPavel Labath // for it. 383745f5cb31SPavel Labath Monitor::ScopedOperationLock monitor_lock(*m_monitor_up); 383845f5cb31SPavel Labath return NativeProcessProtocol::RemoveWatchpoint(addr); 383945f5cb31SPavel Labath } 384045f5cb31SPavel Labath 384145f5cb31SPavel Labath Error 384226438d26SChaoren Lin NativeProcessLinux::ReadMemory (lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) 3843af245d11STodd Fiala { 3844af245d11STodd Fiala ReadOperation op(addr, buf, size, bytes_read); 3845bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 3846af245d11STodd Fiala return op.GetError (); 3847af245d11STodd Fiala } 3848af245d11STodd Fiala 3849af245d11STodd Fiala Error 38503eb4b458SChaoren Lin NativeProcessLinux::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) 38513eb4b458SChaoren Lin { 38523eb4b458SChaoren Lin Error error = ReadMemory(addr, buf, size, bytes_read); 38533eb4b458SChaoren Lin if (error.Fail()) return error; 38543eb4b458SChaoren Lin return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); 38553eb4b458SChaoren Lin } 38563eb4b458SChaoren Lin 38573eb4b458SChaoren Lin Error 38583eb4b458SChaoren Lin NativeProcessLinux::WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) 3859af245d11STodd Fiala { 3860af245d11STodd Fiala WriteOperation op(addr, buf, size, bytes_written); 3861bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 3862af245d11STodd Fiala return op.GetError (); 3863af245d11STodd Fiala } 3864af245d11STodd Fiala 386597ccc294SChaoren Lin Error 3866af245d11STodd Fiala NativeProcessLinux::ReadRegisterValue(lldb::tid_t tid, uint32_t offset, const char* reg_name, 3867af245d11STodd Fiala uint32_t size, RegisterValue &value) 3868af245d11STodd Fiala { 386997ccc294SChaoren Lin ReadRegOperation op(tid, offset, reg_name, value); 3870bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 387197ccc294SChaoren Lin return op.GetError(); 3872af245d11STodd Fiala } 3873af245d11STodd Fiala 387497ccc294SChaoren Lin Error 3875af245d11STodd Fiala NativeProcessLinux::WriteRegisterValue(lldb::tid_t tid, unsigned offset, 3876af245d11STodd Fiala const char* reg_name, const RegisterValue &value) 3877af245d11STodd Fiala { 387897ccc294SChaoren Lin WriteRegOperation op(tid, offset, reg_name, value); 3879bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 388097ccc294SChaoren Lin return op.GetError(); 3881af245d11STodd Fiala } 3882af245d11STodd Fiala 388397ccc294SChaoren Lin Error 3884af245d11STodd Fiala NativeProcessLinux::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size) 3885af245d11STodd Fiala { 388697ccc294SChaoren Lin ReadGPROperation op(tid, buf, buf_size); 3887bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 388897ccc294SChaoren Lin return op.GetError(); 3889af245d11STodd Fiala } 3890af245d11STodd Fiala 389197ccc294SChaoren Lin Error 3892af245d11STodd Fiala NativeProcessLinux::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size) 3893af245d11STodd Fiala { 389497ccc294SChaoren Lin ReadFPROperation op(tid, buf, buf_size); 3895bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 389697ccc294SChaoren Lin return op.GetError(); 3897af245d11STodd Fiala } 3898af245d11STodd Fiala 389997ccc294SChaoren Lin Error 3900af245d11STodd Fiala NativeProcessLinux::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) 3901af245d11STodd Fiala { 390297ccc294SChaoren Lin ReadRegisterSetOperation op(tid, buf, buf_size, regset); 3903bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 390497ccc294SChaoren Lin return op.GetError(); 3905af245d11STodd Fiala } 3906af245d11STodd Fiala 390797ccc294SChaoren Lin Error 3908af245d11STodd Fiala NativeProcessLinux::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size) 3909af245d11STodd Fiala { 391097ccc294SChaoren Lin WriteGPROperation op(tid, buf, buf_size); 3911bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 391297ccc294SChaoren Lin return op.GetError(); 3913af245d11STodd Fiala } 3914af245d11STodd Fiala 391597ccc294SChaoren Lin Error 3916af245d11STodd Fiala NativeProcessLinux::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size) 3917af245d11STodd Fiala { 391897ccc294SChaoren Lin WriteFPROperation op(tid, buf, buf_size); 3919bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 392097ccc294SChaoren Lin return op.GetError(); 3921af245d11STodd Fiala } 3922af245d11STodd Fiala 392397ccc294SChaoren Lin Error 3924af245d11STodd Fiala NativeProcessLinux::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) 3925af245d11STodd Fiala { 392697ccc294SChaoren Lin WriteRegisterSetOperation op(tid, buf, buf_size, regset); 3927bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 392897ccc294SChaoren Lin return op.GetError(); 3929af245d11STodd Fiala } 3930af245d11STodd Fiala 393197ccc294SChaoren Lin Error 3932af245d11STodd Fiala NativeProcessLinux::Resume (lldb::tid_t tid, uint32_t signo) 3933af245d11STodd Fiala { 3934af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3935af245d11STodd Fiala 3936af245d11STodd Fiala if (log) 3937af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid, 3938af245d11STodd Fiala GetUnixSignals().GetSignalAsCString (signo)); 393997ccc294SChaoren Lin ResumeOperation op (tid, signo); 3940bd7cbc5aSPavel Labath m_monitor_up->DoOperation (&op); 3941af245d11STodd Fiala if (log) 394297ccc294SChaoren Lin log->Printf ("NativeProcessLinux::%s() resuming thread = %" PRIu64 " result = %s", __FUNCTION__, tid, op.GetError().Success() ? "true" : "false"); 394397ccc294SChaoren Lin return op.GetError(); 3944af245d11STodd Fiala } 3945af245d11STodd Fiala 394697ccc294SChaoren Lin Error 3947af245d11STodd Fiala NativeProcessLinux::SingleStep(lldb::tid_t tid, uint32_t signo) 3948af245d11STodd Fiala { 394997ccc294SChaoren Lin SingleStepOperation op(tid, signo); 3950bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 395197ccc294SChaoren Lin return op.GetError(); 3952af245d11STodd Fiala } 3953af245d11STodd Fiala 395497ccc294SChaoren Lin Error 395597ccc294SChaoren Lin NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) 3956af245d11STodd Fiala { 395797ccc294SChaoren Lin SiginfoOperation op(tid, siginfo); 3958bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 395997ccc294SChaoren Lin return op.GetError(); 3960af245d11STodd Fiala } 3961af245d11STodd Fiala 396297ccc294SChaoren Lin Error 3963af245d11STodd Fiala NativeProcessLinux::GetEventMessage(lldb::tid_t tid, unsigned long *message) 3964af245d11STodd Fiala { 396597ccc294SChaoren Lin EventMessageOperation op(tid, message); 3966bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 396797ccc294SChaoren Lin return op.GetError(); 3968af245d11STodd Fiala } 3969af245d11STodd Fiala 3970db264a6dSTamas Berghammer Error 3971af245d11STodd Fiala NativeProcessLinux::Detach(lldb::tid_t tid) 3972af245d11STodd Fiala { 397397ccc294SChaoren Lin if (tid == LLDB_INVALID_THREAD_ID) 397497ccc294SChaoren Lin return Error(); 397597ccc294SChaoren Lin 397697ccc294SChaoren Lin DetachOperation op(tid); 3977bd7cbc5aSPavel Labath m_monitor_up->DoOperation(&op); 397897ccc294SChaoren Lin return op.GetError(); 3979af245d11STodd Fiala } 3980af245d11STodd Fiala 3981af245d11STodd Fiala bool 3982af245d11STodd Fiala NativeProcessLinux::DupDescriptor(const char *path, int fd, int flags) 3983af245d11STodd Fiala { 3984af245d11STodd Fiala int target_fd = open(path, flags, 0666); 3985af245d11STodd Fiala 3986af245d11STodd Fiala if (target_fd == -1) 3987af245d11STodd Fiala return false; 3988af245d11STodd Fiala 3989493c3a12SPavel Labath if (dup2(target_fd, fd) == -1) 3990493c3a12SPavel Labath return false; 3991493c3a12SPavel Labath 3992493c3a12SPavel Labath return (close(target_fd) == -1) ? false : true; 3993af245d11STodd Fiala } 3994af245d11STodd Fiala 3995af245d11STodd Fiala void 3996bd7cbc5aSPavel Labath NativeProcessLinux::StartMonitorThread(const InitialOperation &initial_operation, Error &error) 3997af245d11STodd Fiala { 3998bd7cbc5aSPavel Labath m_monitor_up.reset(new Monitor(initial_operation, this)); 39991107b5a5SPavel Labath error = m_monitor_up->Initialize(); 40001107b5a5SPavel Labath if (error.Fail()) { 40011107b5a5SPavel Labath m_monitor_up.reset(); 4002af245d11STodd Fiala } 4003af245d11STodd Fiala } 4004af245d11STodd Fiala 4005af245d11STodd Fiala bool 4006af245d11STodd Fiala NativeProcessLinux::HasThreadNoLock (lldb::tid_t thread_id) 4007af245d11STodd Fiala { 4008af245d11STodd Fiala for (auto thread_sp : m_threads) 4009af245d11STodd Fiala { 4010af245d11STodd Fiala assert (thread_sp && "thread list should not contain NULL threads"); 4011af245d11STodd Fiala if (thread_sp->GetID () == thread_id) 4012af245d11STodd Fiala { 4013af245d11STodd Fiala // We have this thread. 4014af245d11STodd Fiala return true; 4015af245d11STodd Fiala } 4016af245d11STodd Fiala } 4017af245d11STodd Fiala 4018af245d11STodd Fiala // We don't have this thread. 4019af245d11STodd Fiala return false; 4020af245d11STodd Fiala } 4021af245d11STodd Fiala 4022af245d11STodd Fiala NativeThreadProtocolSP 4023af245d11STodd Fiala NativeProcessLinux::MaybeGetThreadNoLock (lldb::tid_t thread_id) 4024af245d11STodd Fiala { 4025af245d11STodd Fiala // CONSIDER organize threads by map - we can do better than linear. 4026af245d11STodd Fiala for (auto thread_sp : m_threads) 4027af245d11STodd Fiala { 4028af245d11STodd Fiala if (thread_sp->GetID () == thread_id) 4029af245d11STodd Fiala return thread_sp; 4030af245d11STodd Fiala } 4031af245d11STodd Fiala 4032af245d11STodd Fiala // We don't have this thread. 4033af245d11STodd Fiala return NativeThreadProtocolSP (); 4034af245d11STodd Fiala } 4035af245d11STodd Fiala 4036af245d11STodd Fiala bool 4037af245d11STodd Fiala NativeProcessLinux::StopTrackingThread (lldb::tid_t thread_id) 4038af245d11STodd Fiala { 4039af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 4040af245d11STodd Fiala for (auto it = m_threads.begin (); it != m_threads.end (); ++it) 4041af245d11STodd Fiala { 4042af245d11STodd Fiala if (*it && ((*it)->GetID () == thread_id)) 4043af245d11STodd Fiala { 4044af245d11STodd Fiala m_threads.erase (it); 4045af245d11STodd Fiala return true; 4046af245d11STodd Fiala } 4047af245d11STodd Fiala } 4048af245d11STodd Fiala 4049af245d11STodd Fiala // Didn't find it. 4050af245d11STodd Fiala return false; 4051af245d11STodd Fiala } 4052af245d11STodd Fiala 4053af245d11STodd Fiala NativeThreadProtocolSP 4054af245d11STodd Fiala NativeProcessLinux::AddThread (lldb::tid_t thread_id) 4055af245d11STodd Fiala { 4056af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 4057af245d11STodd Fiala 4058af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 4059af245d11STodd Fiala 4060af245d11STodd Fiala if (log) 4061af245d11STodd Fiala { 4062af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " adding thread with tid %" PRIu64, 4063af245d11STodd Fiala __FUNCTION__, 4064af245d11STodd Fiala GetID (), 4065af245d11STodd Fiala thread_id); 4066af245d11STodd Fiala } 4067af245d11STodd Fiala 4068af245d11STodd Fiala assert (!HasThreadNoLock (thread_id) && "attempted to add a thread by id that already exists"); 4069af245d11STodd Fiala 4070af245d11STodd Fiala // If this is the first thread, save it as the current thread 4071af245d11STodd Fiala if (m_threads.empty ()) 4072af245d11STodd Fiala SetCurrentThreadID (thread_id); 4073af245d11STodd Fiala 4074af245d11STodd Fiala NativeThreadProtocolSP thread_sp (new NativeThreadLinux (this, thread_id)); 4075af245d11STodd Fiala m_threads.push_back (thread_sp); 4076af245d11STodd Fiala 4077af245d11STodd Fiala return thread_sp; 4078af245d11STodd Fiala } 4079af245d11STodd Fiala 4080af245d11STodd Fiala Error 4081af245d11STodd Fiala NativeProcessLinux::FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp) 4082af245d11STodd Fiala { 408375f47c3aSTodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 4084af245d11STodd Fiala 4085af245d11STodd Fiala Error error; 4086af245d11STodd Fiala 4087af245d11STodd Fiala // Get a linux thread pointer. 4088af245d11STodd Fiala if (!thread_sp) 4089af245d11STodd Fiala { 4090af245d11STodd Fiala error.SetErrorString ("null thread_sp"); 4091af245d11STodd Fiala if (log) 4092af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); 4093af245d11STodd Fiala return error; 4094af245d11STodd Fiala } 4095cb84eebbSTamas Berghammer std::shared_ptr<NativeThreadLinux> linux_thread_sp = std::static_pointer_cast<NativeThreadLinux> (thread_sp); 4096af245d11STodd Fiala 4097af245d11STodd Fiala // Find out the size of a breakpoint (might depend on where we are in the code). 4098cb84eebbSTamas Berghammer NativeRegisterContextSP context_sp = linux_thread_sp->GetRegisterContext (); 4099af245d11STodd Fiala if (!context_sp) 4100af245d11STodd Fiala { 4101af245d11STodd Fiala error.SetErrorString ("cannot get a NativeRegisterContext for the thread"); 4102af245d11STodd Fiala if (log) 4103af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); 4104af245d11STodd Fiala return error; 4105af245d11STodd Fiala } 4106af245d11STodd Fiala 4107af245d11STodd Fiala uint32_t breakpoint_size = 0; 410863c8be95STamas Berghammer error = GetSoftwareBreakpointPCOffset (context_sp, breakpoint_size); 4109af245d11STodd Fiala if (error.Fail ()) 4110af245d11STodd Fiala { 4111af245d11STodd Fiala if (log) 4112af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s GetBreakpointSize() failed: %s", __FUNCTION__, error.AsCString ()); 4113af245d11STodd Fiala return error; 4114af245d11STodd Fiala } 4115af245d11STodd Fiala else 4116af245d11STodd Fiala { 4117af245d11STodd Fiala if (log) 4118af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s breakpoint size: %" PRIu32, __FUNCTION__, breakpoint_size); 4119af245d11STodd Fiala } 4120af245d11STodd Fiala 4121af245d11STodd Fiala // First try probing for a breakpoint at a software breakpoint location: PC - breakpoint size. 4122af245d11STodd Fiala const lldb::addr_t initial_pc_addr = context_sp->GetPC (); 4123af245d11STodd Fiala lldb::addr_t breakpoint_addr = initial_pc_addr; 41243eb4b458SChaoren Lin if (breakpoint_size > 0) 4125af245d11STodd Fiala { 4126af245d11STodd Fiala // Do not allow breakpoint probe to wrap around. 41273eb4b458SChaoren Lin if (breakpoint_addr >= breakpoint_size) 41283eb4b458SChaoren Lin breakpoint_addr -= breakpoint_size; 4129af245d11STodd Fiala } 4130af245d11STodd Fiala 4131af245d11STodd Fiala // Check if we stopped because of a breakpoint. 4132af245d11STodd Fiala NativeBreakpointSP breakpoint_sp; 4133af245d11STodd Fiala error = m_breakpoint_list.GetBreakpoint (breakpoint_addr, breakpoint_sp); 4134af245d11STodd Fiala if (!error.Success () || !breakpoint_sp) 4135af245d11STodd Fiala { 4136af245d11STodd Fiala // We didn't find one at a software probe location. Nothing to do. 4137af245d11STodd Fiala if (log) 4138af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " no lldb breakpoint found at current pc with adjustment: 0x%" PRIx64, __FUNCTION__, GetID (), breakpoint_addr); 4139af245d11STodd Fiala return Error (); 4140af245d11STodd Fiala } 4141af245d11STodd Fiala 4142af245d11STodd Fiala // If the breakpoint is not a software breakpoint, nothing to do. 4143af245d11STodd Fiala if (!breakpoint_sp->IsSoftwareBreakpoint ()) 4144af245d11STodd Fiala { 4145af245d11STodd Fiala if (log) 4146af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " breakpoint found at 0x%" PRIx64 ", not software, nothing to adjust", __FUNCTION__, GetID (), breakpoint_addr); 4147af245d11STodd Fiala return Error (); 4148af245d11STodd Fiala } 4149af245d11STodd Fiala 4150af245d11STodd Fiala // 4151af245d11STodd Fiala // We have a software breakpoint and need to adjust the PC. 4152af245d11STodd Fiala // 4153af245d11STodd Fiala 4154af245d11STodd Fiala // Sanity check. 4155af245d11STodd Fiala if (breakpoint_size == 0) 4156af245d11STodd Fiala { 4157af245d11STodd Fiala // Nothing to do! How did we get here? 4158af245d11STodd Fiala if (log) 4159af245d11STodd 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); 4160af245d11STodd Fiala return Error (); 4161af245d11STodd Fiala } 4162af245d11STodd Fiala 4163af245d11STodd Fiala // Change the program counter. 4164af245d11STodd Fiala if (log) 4165cb84eebbSTamas 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); 4166af245d11STodd Fiala 4167af245d11STodd Fiala error = context_sp->SetPC (breakpoint_addr); 4168af245d11STodd Fiala if (error.Fail ()) 4169af245d11STodd Fiala { 4170af245d11STodd Fiala if (log) 4171cb84eebbSTamas Berghammer log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": failed to set PC: %s", __FUNCTION__, GetID (), linux_thread_sp->GetID (), error.AsCString ()); 4172af245d11STodd Fiala return error; 4173af245d11STodd Fiala } 4174af245d11STodd Fiala 4175af245d11STodd Fiala return error; 4176af245d11STodd Fiala } 4177fa03ad2eSChaoren Lin 41787cb18bf5STamas Berghammer Error 41797cb18bf5STamas Berghammer NativeProcessLinux::GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) 41807cb18bf5STamas Berghammer { 41817cb18bf5STamas Berghammer char maps_file_name[32]; 41827cb18bf5STamas Berghammer snprintf(maps_file_name, sizeof(maps_file_name), "/proc/%" PRIu64 "/maps", GetID()); 41837cb18bf5STamas Berghammer 41847cb18bf5STamas Berghammer FileSpec maps_file_spec(maps_file_name, false); 41857cb18bf5STamas Berghammer if (!maps_file_spec.Exists()) { 41867cb18bf5STamas Berghammer file_spec.Clear(); 41877cb18bf5STamas Berghammer return Error("/proc/%" PRIu64 "/maps file doesn't exists!", GetID()); 41887cb18bf5STamas Berghammer } 41897cb18bf5STamas Berghammer 41907cb18bf5STamas Berghammer FileSpec module_file_spec(module_path, true); 41917cb18bf5STamas Berghammer 41927cb18bf5STamas Berghammer std::ifstream maps_file(maps_file_name); 41937cb18bf5STamas Berghammer std::string maps_data_str((std::istreambuf_iterator<char>(maps_file)), std::istreambuf_iterator<char>()); 41947cb18bf5STamas Berghammer StringRef maps_data(maps_data_str.c_str()); 41957cb18bf5STamas Berghammer 41967cb18bf5STamas Berghammer while (!maps_data.empty()) 41977cb18bf5STamas Berghammer { 41987cb18bf5STamas Berghammer StringRef maps_row; 41997cb18bf5STamas Berghammer std::tie(maps_row, maps_data) = maps_data.split('\n'); 42007cb18bf5STamas Berghammer 42017cb18bf5STamas Berghammer SmallVector<StringRef, 16> maps_columns; 42027cb18bf5STamas Berghammer maps_row.split(maps_columns, StringRef(" "), -1, false); 42037cb18bf5STamas Berghammer 42047cb18bf5STamas Berghammer if (maps_columns.size() >= 6) 42057cb18bf5STamas Berghammer { 42067cb18bf5STamas Berghammer file_spec.SetFile(maps_columns[5].str().c_str(), false); 42077cb18bf5STamas Berghammer if (file_spec.GetFilename() == module_file_spec.GetFilename()) 42087cb18bf5STamas Berghammer return Error(); 42097cb18bf5STamas Berghammer } 42107cb18bf5STamas Berghammer } 42117cb18bf5STamas Berghammer 42127cb18bf5STamas Berghammer file_spec.Clear(); 42137cb18bf5STamas Berghammer return Error("Module file (%s) not found in /proc/%" PRIu64 "/maps file!", 42147cb18bf5STamas Berghammer module_file_spec.GetFilename().AsCString(), GetID()); 42157cb18bf5STamas Berghammer } 4216c076559aSPavel Labath 42175eb721edSPavel Labath Error 4218c076559aSPavel Labath NativeProcessLinux::DoResume( 4219c076559aSPavel Labath lldb::tid_t tid, 4220*8c8ff7afSPavel Labath NativeThreadLinux::ResumeThreadFunction request_thread_resume_function, 4221c076559aSPavel Labath bool error_when_already_running) 4222c076559aSPavel Labath { 42235eb721edSPavel Labath Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 42245eb721edSPavel Labath 4225*8c8ff7afSPavel Labath auto thread_sp = std::static_pointer_cast<NativeThreadLinux>(GetThreadByID(tid)); 4226*8c8ff7afSPavel Labath lldbassert(thread_sp != nullptr); 42275eb721edSPavel Labath 4228*8c8ff7afSPavel Labath auto& context = thread_sp->GetThreadContext(); 4229c076559aSPavel Labath // Tell the thread to resume if we don't already think it is running. 4230*8c8ff7afSPavel Labath const bool is_stopped = StateIsStoppedState(thread_sp->GetState(), true); 42315eb721edSPavel Labath 42325eb721edSPavel Labath lldbassert(!(error_when_already_running && !is_stopped)); 42335eb721edSPavel Labath 4234c076559aSPavel Labath if (!is_stopped) 4235c076559aSPavel Labath { 4236c076559aSPavel Labath // It's not an error, just a log, if the error_when_already_running flag is not set. 4237c076559aSPavel Labath // This covers cases where, for instance, we're just trying to resume all threads 4238c076559aSPavel Labath // from the user side. 42395eb721edSPavel Labath if (log) 42405eb721edSPavel Labath log->Printf("NativeProcessLinux::%s tid %" PRIu64 " optional resume skipped since it is already running", 4241c076559aSPavel Labath __FUNCTION__, 4242c076559aSPavel Labath tid); 42435eb721edSPavel Labath return Error(); 4244c076559aSPavel Labath } 4245c076559aSPavel Labath 4246c076559aSPavel Labath // Before we do the resume below, first check if we have a pending 4247c076559aSPavel Labath // stop notification this is currently or was previously waiting for 4248c076559aSPavel Labath // this thread to stop. This is potentially a buggy situation since 4249c076559aSPavel Labath // we're ostensibly waiting for threads to stop before we send out the 4250c076559aSPavel Labath // pending notification, and here we are resuming one before we send 4251c076559aSPavel Labath // out the pending stop notification. 42525eb721edSPavel Labath if (m_pending_notification_up && log) 4253c076559aSPavel Labath { 4254c076559aSPavel Labath if (m_pending_notification_up->wait_for_stop_tids.count (tid) > 0) 4255c076559aSPavel Labath { 42565eb721edSPavel Labath log->Printf("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); 4257c076559aSPavel Labath } 4258c076559aSPavel Labath else if (m_pending_notification_up->original_wait_for_stop_tids.count (tid) > 0) 4259c076559aSPavel Labath { 42605eb721edSPavel Labath log->Printf("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); 4261c076559aSPavel Labath for (auto tid : m_pending_notification_up->wait_for_stop_tids) 4262c076559aSPavel Labath { 42635eb721edSPavel Labath log->Printf("NativeProcessLinux::%s tid %" PRIu64 " deferred stop notification still waiting on tid %" PRIu64, 4264c076559aSPavel Labath __FUNCTION__, 4265c076559aSPavel Labath m_pending_notification_up->triggering_tid, 4266c076559aSPavel Labath tid); 4267c076559aSPavel Labath } 4268c076559aSPavel Labath } 4269c076559aSPavel Labath } 4270c076559aSPavel Labath 4271c076559aSPavel Labath // Request a resume. We expect this to be synchronous and the system 4272c076559aSPavel Labath // to reflect it is running after this completes. 4273c076559aSPavel Labath const auto error = request_thread_resume_function (tid, false); 4274c076559aSPavel Labath if (error.Success()) 4275*8c8ff7afSPavel Labath context.request_resume_function = request_thread_resume_function; 42765eb721edSPavel Labath else if (log) 4277c076559aSPavel Labath { 42785eb721edSPavel Labath log->Printf("NativeProcessLinux::%s failed to resume thread tid %" PRIu64 ": %s", 4279c076559aSPavel Labath __FUNCTION__, tid, error.AsCString ()); 4280c076559aSPavel Labath } 4281c076559aSPavel Labath 42825eb721edSPavel Labath return error; 4283c076559aSPavel Labath } 4284c076559aSPavel Labath 4285c076559aSPavel Labath //===----------------------------------------------------------------------===// 4286c076559aSPavel Labath 4287c076559aSPavel Labath void 4288ed89c7feSPavel Labath NativeProcessLinux::StopThreads(const lldb::tid_t triggering_tid, 4289337f3eb9SPavel Labath const ThreadIDSet &wait_for_stop_tids) 4290c076559aSPavel Labath { 42915eb721edSPavel Labath Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 4292c076559aSPavel Labath std::lock_guard<std::mutex> lock(m_event_mutex); 4293c076559aSPavel Labath 42945eb721edSPavel Labath if (log) 4295c076559aSPavel Labath { 42965eb721edSPavel Labath log->Printf("NativeProcessLinux::%s about to process event: (triggering_tid: %" PRIu64 ", wait_for_stop_tids.size(): %zd)", 4297c076559aSPavel Labath __FUNCTION__, triggering_tid, wait_for_stop_tids.size()); 4298c076559aSPavel Labath } 4299c076559aSPavel Labath 4300ed89c7feSPavel Labath DoStopThreads(PendingNotificationUP(new PendingNotification( 4301337f3eb9SPavel Labath triggering_tid, wait_for_stop_tids, ThreadIDSet()))); 4302c076559aSPavel Labath 43035eb721edSPavel Labath if (log) 4304c076559aSPavel Labath { 43055eb721edSPavel Labath log->Printf("NativeProcessLinux::%s event processing done", __FUNCTION__); 4306c076559aSPavel Labath } 4307c076559aSPavel Labath } 4308c076559aSPavel Labath 4309c076559aSPavel Labath void 4310337f3eb9SPavel Labath NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid) 4311c076559aSPavel Labath { 43125eb721edSPavel Labath Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 4313c076559aSPavel Labath std::lock_guard<std::mutex> lock(m_event_mutex); 4314c076559aSPavel Labath 43155eb721edSPavel Labath if (log) 4316c076559aSPavel Labath { 43175eb721edSPavel Labath log->Printf("NativeProcessLinux::%s about to process event: (triggering_tid: %" PRIu64 ")", 4318c076559aSPavel Labath __FUNCTION__, triggering_tid); 4319c076559aSPavel Labath } 4320c076559aSPavel Labath 4321337f3eb9SPavel Labath DoStopThreads(PendingNotificationUP(new PendingNotification(triggering_tid))); 4322c076559aSPavel Labath 43235eb721edSPavel Labath if (log) 4324c076559aSPavel Labath { 43255eb721edSPavel Labath log->Printf("NativeProcessLinux::%s event processing done", __FUNCTION__); 4326c076559aSPavel Labath } 4327c076559aSPavel Labath } 4328c076559aSPavel Labath 4329c076559aSPavel Labath void 4330ed89c7feSPavel Labath NativeProcessLinux::StopRunningThreadsWithSkipTID(lldb::tid_t triggering_tid, 4331337f3eb9SPavel Labath lldb::tid_t skip_stop_request_tid) 4332c076559aSPavel Labath { 43335eb721edSPavel Labath Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 4334c076559aSPavel Labath std::lock_guard<std::mutex> lock(m_event_mutex); 4335c076559aSPavel Labath 43365eb721edSPavel Labath if (log) 4337c076559aSPavel Labath { 4338337f3eb9SPavel Labath log->Printf("NativeProcessLinux::%s about to process event: (triggering_tid: %" PRIu64 ", skip_stop_request_tid: %" PRIu64 ")", 4339337f3eb9SPavel Labath __FUNCTION__, triggering_tid, skip_stop_request_tid); 4340c076559aSPavel Labath } 4341c076559aSPavel Labath 4342ed89c7feSPavel Labath DoStopThreads(PendingNotificationUP(new PendingNotification( 4343c076559aSPavel Labath triggering_tid, 4344337f3eb9SPavel Labath ThreadIDSet(), 4345337f3eb9SPavel Labath skip_stop_request_tid != LLDB_INVALID_THREAD_ID ? NativeProcessLinux::ThreadIDSet {skip_stop_request_tid} : ThreadIDSet ()))); 4346c076559aSPavel Labath 43475eb721edSPavel Labath if (log) 4348c076559aSPavel Labath { 43495eb721edSPavel Labath log->Printf("NativeProcessLinux::%s event processing done", __FUNCTION__); 4350c076559aSPavel Labath } 4351c076559aSPavel Labath } 4352c076559aSPavel Labath 4353c076559aSPavel Labath void 4354c076559aSPavel Labath NativeProcessLinux::SignalIfRequirementsSatisfied() 4355c076559aSPavel Labath { 4356c076559aSPavel Labath if (m_pending_notification_up && m_pending_notification_up->wait_for_stop_tids.empty ()) 4357c076559aSPavel Labath { 4358ed89c7feSPavel Labath SetCurrentThreadID(m_pending_notification_up->triggering_tid); 4359ed89c7feSPavel Labath SetState(StateType::eStateStopped, true); 4360c076559aSPavel Labath m_pending_notification_up.reset(); 4361c076559aSPavel Labath } 4362c076559aSPavel Labath } 4363c076559aSPavel Labath 4364c076559aSPavel Labath bool 4365c076559aSPavel Labath NativeProcessLinux::RequestStopOnAllSpecifiedThreads() 4366c076559aSPavel Labath { 4367c076559aSPavel Labath // Request a stop for all the thread stops that need to be stopped 4368c076559aSPavel Labath // and are not already known to be stopped. Keep a list of all the 4369c076559aSPavel Labath // threads from which we still need to hear a stop reply. 4370c076559aSPavel Labath 4371c076559aSPavel Labath ThreadIDSet sent_tids; 4372c076559aSPavel Labath for (auto tid : m_pending_notification_up->wait_for_stop_tids) 4373c076559aSPavel Labath { 4374c076559aSPavel Labath // Validate we know about all tids for which we must first receive a stop before 4375c076559aSPavel Labath // triggering the deferred stop notification. 4376*8c8ff7afSPavel Labath auto thread_sp = std::static_pointer_cast<NativeThreadLinux>(GetThreadByID(tid)); 4377*8c8ff7afSPavel Labath lldbassert(thread_sp != nullptr); 4378c076559aSPavel Labath 4379c076559aSPavel Labath // If the pending stop thread is currently running, we need to send it a stop request. 4380*8c8ff7afSPavel Labath if (StateIsRunningState(thread_sp->GetState())) 4381c076559aSPavel Labath { 4382*8c8ff7afSPavel Labath thread_sp->RequestStop(); 4383c076559aSPavel Labath sent_tids.insert (tid); 4384c076559aSPavel Labath } 4385c076559aSPavel Labath } 4386c076559aSPavel Labath // We only need to wait for the sent_tids - so swap our wait set 4387c076559aSPavel Labath // to the sent tids. The rest are already stopped and we won't 4388c076559aSPavel Labath // be receiving stop notifications for them. 4389c076559aSPavel Labath m_pending_notification_up->wait_for_stop_tids.swap (sent_tids); 4390c076559aSPavel Labath 4391c076559aSPavel Labath // Succeeded, keep running. 4392c076559aSPavel Labath return true; 4393c076559aSPavel Labath } 4394c076559aSPavel Labath 4395c076559aSPavel Labath void 4396c076559aSPavel Labath NativeProcessLinux::RequestStopOnAllRunningThreads() 4397c076559aSPavel Labath { 4398c076559aSPavel Labath // Request a stop for all the thread stops that need to be stopped 4399c076559aSPavel Labath // and are not already known to be stopped. Keep a list of all the 4400c076559aSPavel Labath // threads from which we still need to hear a stop reply. 4401c076559aSPavel Labath 4402c076559aSPavel Labath ThreadIDSet sent_tids; 4403*8c8ff7afSPavel Labath for (const auto &thread_sp: m_threads) 4404c076559aSPavel Labath { 4405*8c8ff7afSPavel Labath // We only care about running threads 4406*8c8ff7afSPavel Labath if (StateIsStoppedState(thread_sp->GetState(), true)) 4407*8c8ff7afSPavel Labath continue; 4408*8c8ff7afSPavel Labath 4409*8c8ff7afSPavel Labath const lldb::tid_t tid = thread_sp->GetID(); 4410c076559aSPavel Labath 4411c076559aSPavel Labath // Request this thread stop if the tid stop request is not explicitly ignored. 4412c076559aSPavel Labath const bool skip_stop_request = m_pending_notification_up->skip_stop_request_tids.count (tid) > 0; 4413c076559aSPavel Labath if (!skip_stop_request) 4414*8c8ff7afSPavel Labath static_pointer_cast<NativeThreadLinux>(thread_sp)->RequestStop(); 4415c076559aSPavel Labath 4416c076559aSPavel Labath // Even if we skipped sending the stop request for other reasons (like stepping), 4417c076559aSPavel Labath // we still need to wait for that stepping thread to notify completion/stop. 4418c076559aSPavel Labath sent_tids.insert (tid); 4419c076559aSPavel Labath } 4420c076559aSPavel Labath 4421c076559aSPavel Labath // Set the wait list to the set of tids for which we requested stops. 4422c076559aSPavel Labath m_pending_notification_up->wait_for_stop_tids.swap (sent_tids); 4423c076559aSPavel Labath } 4424c076559aSPavel Labath 4425c076559aSPavel Labath 44265eb721edSPavel Labath Error 44275eb721edSPavel Labath NativeProcessLinux::ThreadDidStop (lldb::tid_t tid, bool initiated_by_llgs) 4428c076559aSPavel Labath { 4429c076559aSPavel Labath // Ensure we know about the thread. 4430*8c8ff7afSPavel Labath auto thread_sp = std::static_pointer_cast<NativeThreadLinux>(GetThreadByID(tid)); 4431*8c8ff7afSPavel Labath lldbassert(thread_sp != nullptr); 4432c076559aSPavel Labath 4433c076559aSPavel Labath // Update the global list of known thread states. This one is definitely stopped. 4434*8c8ff7afSPavel Labath auto& context = thread_sp->GetThreadContext(); 4435*8c8ff7afSPavel Labath const auto stop_was_requested = context.stop_requested; 4436*8c8ff7afSPavel Labath context.stop_requested = false; 4437c076559aSPavel Labath 4438c076559aSPavel Labath // If we have a pending notification, remove this from the set. 4439c076559aSPavel Labath if (m_pending_notification_up) 4440c076559aSPavel Labath { 4441c076559aSPavel Labath m_pending_notification_up->wait_for_stop_tids.erase(tid); 4442c076559aSPavel Labath SignalIfRequirementsSatisfied(); 4443c076559aSPavel Labath } 4444c076559aSPavel Labath 4445*8c8ff7afSPavel Labath Error error; 4446*8c8ff7afSPavel Labath if (initiated_by_llgs && context.request_resume_function && !stop_was_requested) 4447c076559aSPavel Labath { 44485eb721edSPavel Labath Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 4449c076559aSPavel Labath // We can end up here if stop was initiated by LLGS but by this time a 4450c076559aSPavel Labath // thread stop has occurred - maybe initiated by another event. 44515eb721edSPavel Labath if (log) 44525eb721edSPavel Labath log->Printf("Resuming thread %" PRIu64 " since stop wasn't requested", tid); 4453*8c8ff7afSPavel Labath error = context.request_resume_function (tid, true); 4454*8c8ff7afSPavel Labath if (error.Fail() && log) 44555eb721edSPavel Labath { 44565eb721edSPavel Labath log->Printf("NativeProcessLinux::%s failed to resume thread tid %" PRIu64 ": %s", 4457c076559aSPavel Labath __FUNCTION__, tid, error.AsCString ()); 4458c076559aSPavel Labath } 4459*8c8ff7afSPavel Labath } 44605eb721edSPavel Labath return error; 4461c076559aSPavel Labath } 4462c076559aSPavel Labath 4463c076559aSPavel Labath void 4464ed89c7feSPavel Labath NativeProcessLinux::DoStopThreads(PendingNotificationUP &¬ification_up) 4465c076559aSPavel Labath { 44665eb721edSPavel Labath Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 44675eb721edSPavel Labath if (m_pending_notification_up && log) 4468c076559aSPavel Labath { 4469c076559aSPavel Labath // Yikes - we've already got a pending signal notification in progress. 4470c076559aSPavel Labath // Log this info. We lose the pending notification here. 44715eb721edSPavel Labath log->Printf("NativeProcessLinux::%s dropping existing pending signal notification for tid %" PRIu64 ", to be replaced with signal for tid %" PRIu64, 4472c076559aSPavel Labath __FUNCTION__, 4473c076559aSPavel Labath m_pending_notification_up->triggering_tid, 4474c076559aSPavel Labath notification_up->triggering_tid); 4475c076559aSPavel Labath } 4476c076559aSPavel Labath m_pending_notification_up = std::move(notification_up); 4477c076559aSPavel Labath 4478c076559aSPavel Labath if (m_pending_notification_up->request_stop_on_all_unstopped_threads) 4479c076559aSPavel Labath RequestStopOnAllRunningThreads(); 4480c076559aSPavel Labath else 4481c076559aSPavel Labath { 4482c076559aSPavel Labath if (!RequestStopOnAllSpecifiedThreads()) 4483c076559aSPavel Labath return; 4484c076559aSPavel Labath } 4485c076559aSPavel Labath 4486ed89c7feSPavel Labath SignalIfRequirementsSatisfied(); 4487c076559aSPavel Labath } 4488c076559aSPavel Labath 4489c076559aSPavel Labath void 4490*8c8ff7afSPavel Labath NativeProcessLinux::ThreadWasCreated (lldb::tid_t tid) 4491c076559aSPavel Labath { 4492*8c8ff7afSPavel Labath auto thread_sp = std::static_pointer_cast<NativeThreadLinux>(GetThreadByID(tid)); 4493*8c8ff7afSPavel Labath lldbassert(thread_sp != nullptr); 4494c076559aSPavel Labath 4495*8c8ff7afSPavel Labath if (m_pending_notification_up && StateIsRunningState(thread_sp->GetState())) 4496c076559aSPavel Labath { 4497c076559aSPavel Labath // We will need to wait for this new thread to stop as well before firing the 4498c076559aSPavel Labath // notification. 4499c076559aSPavel Labath m_pending_notification_up->wait_for_stop_tids.insert(tid); 4500*8c8ff7afSPavel Labath thread_sp->RequestStop(); 4501c076559aSPavel Labath } 4502c076559aSPavel Labath } 4503c076559aSPavel Labath 4504c076559aSPavel Labath void 45055eb721edSPavel Labath NativeProcessLinux::ThreadDidDie (lldb::tid_t tid) 4506c076559aSPavel Labath { 4507c076559aSPavel Labath // If we have a pending notification, remove this from the set. 4508c076559aSPavel Labath if (m_pending_notification_up) 4509c076559aSPavel Labath { 4510c076559aSPavel Labath m_pending_notification_up->wait_for_stop_tids.erase(tid); 4511c076559aSPavel Labath SignalIfRequirementsSatisfied(); 4512c076559aSPavel Labath } 4513c076559aSPavel Labath } 4514c076559aSPavel Labath 45155eb721edSPavel Labath Error 45165eb721edSPavel Labath NativeProcessLinux::NotifyThreadStop (lldb::tid_t tid, bool initiated_by_llgs) 4517c076559aSPavel Labath { 45185eb721edSPavel Labath Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 4519c076559aSPavel Labath std::lock_guard<std::mutex> lock(m_event_mutex); 4520c076559aSPavel Labath 45215eb721edSPavel Labath if (log) 4522c076559aSPavel Labath { 45235eb721edSPavel Labath log->Printf("NativeProcessLinux::%s about to process event: (tid: %" PRIu64 ", %sinitiated by llgs)", 4524c076559aSPavel Labath __FUNCTION__, tid, initiated_by_llgs?"":"not "); 4525c076559aSPavel Labath } 4526c076559aSPavel Labath 45275eb721edSPavel Labath Error error = ThreadDidStop (tid, initiated_by_llgs); 4528c076559aSPavel Labath 45295eb721edSPavel Labath if (log) 4530c076559aSPavel Labath { 45315eb721edSPavel Labath log->Printf("NativeProcessLinux::%s event processing done", __FUNCTION__); 4532c076559aSPavel Labath } 4533c076559aSPavel Labath 45345eb721edSPavel Labath return error; 45355eb721edSPavel Labath } 45365eb721edSPavel Labath 45375eb721edSPavel Labath Error 4538c076559aSPavel Labath NativeProcessLinux::RequestThreadResume (lldb::tid_t tid, 4539*8c8ff7afSPavel Labath const NativeThreadLinux::ResumeThreadFunction &request_thread_resume_function) 4540c076559aSPavel Labath { 45415eb721edSPavel Labath Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 4542c076559aSPavel Labath std::lock_guard<std::mutex> lock(m_event_mutex); 4543c076559aSPavel Labath 45445eb721edSPavel Labath if (log) 4545c076559aSPavel Labath { 45465eb721edSPavel Labath log->Printf("NativeProcessLinux::%s about to process event: (tid: %" PRIu64 ")", 4547c076559aSPavel Labath __FUNCTION__, tid); 4548c076559aSPavel Labath } 4549c076559aSPavel Labath 45505eb721edSPavel Labath Error error = DoResume(tid, request_thread_resume_function, true); 4551c076559aSPavel Labath 45525eb721edSPavel Labath if (log) 4553c076559aSPavel Labath { 45545eb721edSPavel Labath log->Printf("NativeProcessLinux::%s event processing done", __FUNCTION__); 4555c076559aSPavel Labath } 4556c076559aSPavel Labath 45575eb721edSPavel Labath return error; 45585eb721edSPavel Labath } 45595eb721edSPavel Labath 45605eb721edSPavel Labath Error 4561c076559aSPavel Labath NativeProcessLinux::RequestThreadResumeAsNeeded (lldb::tid_t tid, 4562*8c8ff7afSPavel Labath const NativeThreadLinux::ResumeThreadFunction &request_thread_resume_function) 4563c076559aSPavel Labath { 45645eb721edSPavel Labath Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 4565c076559aSPavel Labath std::lock_guard<std::mutex> lock(m_event_mutex); 4566c076559aSPavel Labath 45675eb721edSPavel Labath if (log) 4568c076559aSPavel Labath { 45695eb721edSPavel Labath log->Printf("NativeProcessLinux::%s about to process event: (tid: %" PRIu64 ")", 4570c076559aSPavel Labath __FUNCTION__, tid); 4571c076559aSPavel Labath } 4572c076559aSPavel Labath 45735eb721edSPavel Labath Error error = DoResume (tid, request_thread_resume_function, false); 4574c076559aSPavel Labath 45755eb721edSPavel Labath if (log) 4576c076559aSPavel Labath { 45775eb721edSPavel Labath log->Printf("NativeProcessLinux::%s event processing done", __FUNCTION__); 4578c076559aSPavel Labath } 45795eb721edSPavel Labath 45805eb721edSPavel Labath return error; 4581c076559aSPavel Labath } 4582c076559aSPavel Labath 4583c076559aSPavel Labath void 4584*8c8ff7afSPavel Labath NativeProcessLinux::NotifyThreadCreate(lldb::tid_t tid) 4585c076559aSPavel Labath { 45865eb721edSPavel Labath Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 4587c076559aSPavel Labath std::lock_guard<std::mutex> lock(m_event_mutex); 4588c076559aSPavel Labath 45895eb721edSPavel Labath if (log) 4590*8c8ff7afSPavel Labath log->Printf("NativeProcessLinux::%s about to process event: (tid: %" PRIu64 ")", __FUNCTION__, tid); 4591c076559aSPavel Labath 4592*8c8ff7afSPavel Labath ThreadWasCreated(tid); 4593c076559aSPavel Labath 45945eb721edSPavel Labath if (log) 4595c076559aSPavel Labath { 45965eb721edSPavel Labath log->Printf("NativeProcessLinux::%s event processing done", __FUNCTION__); 4597c076559aSPavel Labath } 4598c076559aSPavel Labath } 4599c076559aSPavel Labath 4600c076559aSPavel Labath void 46015eb721edSPavel Labath NativeProcessLinux::NotifyThreadDeath (lldb::tid_t tid) 4602c076559aSPavel Labath { 46035eb721edSPavel Labath Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 4604c076559aSPavel Labath std::lock_guard<std::mutex> lock(m_event_mutex); 4605c076559aSPavel Labath 46065eb721edSPavel Labath if (log) 4607c076559aSPavel Labath { 46085eb721edSPavel Labath log->Printf("NativeProcessLinux::%s about to process event: (tid: %" PRIu64 ")", __FUNCTION__, tid); 4609c076559aSPavel Labath } 4610c076559aSPavel Labath 46115eb721edSPavel Labath ThreadDidDie(tid); 4612c076559aSPavel Labath 46135eb721edSPavel Labath if (log) 4614c076559aSPavel Labath { 46155eb721edSPavel Labath log->Printf("NativeProcessLinux::%s event processing done", __FUNCTION__); 4616c076559aSPavel Labath } 4617c076559aSPavel Labath } 4618c076559aSPavel Labath 4619c076559aSPavel Labath void 4620c076559aSPavel Labath NativeProcessLinux::ResetForExec () 4621c076559aSPavel Labath { 46225eb721edSPavel Labath Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 4623c076559aSPavel Labath std::lock_guard<std::mutex> lock(m_event_mutex); 4624c076559aSPavel Labath 46255eb721edSPavel Labath if (log) 4626c076559aSPavel Labath { 46275eb721edSPavel Labath log->Printf("NativeProcessLinux::%s about to process event", __FUNCTION__); 4628c076559aSPavel Labath } 4629c076559aSPavel Labath 4630c076559aSPavel Labath // Clear the pending notification if there was one. 4631c076559aSPavel Labath m_pending_notification_up.reset (); 4632c076559aSPavel Labath 46335eb721edSPavel Labath if (log) 4634c076559aSPavel Labath { 46355eb721edSPavel Labath log->Printf("NativeProcessLinux::%s event processing done", __FUNCTION__); 4636c076559aSPavel Labath } 4637c076559aSPavel Labath } 4638