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> 23af245d11STodd Fiala #include <string> 24af245d11STodd Fiala 25af245d11STodd Fiala // Other libraries and framework includes 26af245d11STodd Fiala #include "lldb/Core/Debugger.h" 27d8c338d4STamas Berghammer #include "lldb/Core/EmulateInstruction.h" 28af245d11STodd Fiala #include "lldb/Core/Error.h" 29af245d11STodd Fiala #include "lldb/Core/Module.h" 306edef204SOleksiy Vyalov #include "lldb/Core/ModuleSpec.h" 31af245d11STodd Fiala #include "lldb/Core/RegisterValue.h" 32af245d11STodd Fiala #include "lldb/Core/Scalar.h" 33af245d11STodd Fiala #include "lldb/Core/State.h" 341e209fccSTamas Berghammer #include "lldb/Host/common/NativeBreakpoint.h" 350cbf0b13STamas Berghammer #include "lldb/Host/common/NativeRegisterContext.h" 36af245d11STodd Fiala #include "lldb/Host/Host.h" 3713b18261SZachary Turner #include "lldb/Host/HostInfo.h" 380cbf0b13STamas Berghammer #include "lldb/Host/HostNativeThread.h" 3939de3110SZachary Turner #include "lldb/Host/ThreadLauncher.h" 40af245d11STodd Fiala #include "lldb/Symbol/ObjectFile.h" 4190aff47cSZachary Turner #include "lldb/Target/Process.h" 42af245d11STodd Fiala #include "lldb/Target/ProcessLaunchInfo.h" 43c16f5dcaSChaoren Lin #include "lldb/Utility/LLDBAssert.h" 44af245d11STodd Fiala #include "lldb/Utility/PseudoTerminal.h" 45af245d11STodd Fiala 461e209fccSTamas Berghammer #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" 47af245d11STodd Fiala #include "Plugins/Process/Utility/LinuxSignals.h" 481e209fccSTamas Berghammer #include "Utility/StringExtractor.h" 49af245d11STodd Fiala #include "NativeThreadLinux.h" 50af245d11STodd Fiala #include "ProcFileReader.h" 511e209fccSTamas Berghammer #include "Procfs.h" 52fa03ad2eSChaoren Lin #include "ThreadStateCoordinator.h" 53cacde7dfSTodd Fiala 54d858487eSTamas Berghammer // System includes - They have to be included after framework includes because they define some 55d858487eSTamas Berghammer // macros which collide with variable names in other modules 56d858487eSTamas Berghammer #include <linux/unistd.h> 57d858487eSTamas Berghammer #include <sys/personality.h> 58d858487eSTamas Berghammer #include <sys/ptrace.h> 59d858487eSTamas Berghammer #include <sys/socket.h> 601107b5a5SPavel Labath #include <sys/signalfd.h> 61d858487eSTamas Berghammer #include <sys/syscall.h> 62d858487eSTamas Berghammer #include <sys/types.h> 63d858487eSTamas Berghammer #include <sys/uio.h> 64d858487eSTamas Berghammer #include <sys/user.h> 65d858487eSTamas Berghammer #include <sys/wait.h> 66d858487eSTamas Berghammer 671e209fccSTamas Berghammer #if defined (__arm64__) || defined (__aarch64__) 681e209fccSTamas Berghammer // NT_PRSTATUS and NT_FPREGSET definition 691e209fccSTamas Berghammer #include <elf.h> 701e209fccSTamas Berghammer #endif 711e209fccSTamas Berghammer 72cacde7dfSTodd Fiala #ifdef __ANDROID__ 73cacde7dfSTodd Fiala #define __ptrace_request int 74cacde7dfSTodd Fiala #define PT_DETACH PTRACE_DETACH 75cacde7dfSTodd Fiala #endif 76af245d11STodd Fiala 77af245d11STodd Fiala #define DEBUG_PTRACE_MAXBYTES 20 78af245d11STodd Fiala 79af245d11STodd Fiala // Support ptrace extensions even when compiled without required kernel support 80dda61943STodd Fiala #ifndef PT_GETREGS 81af245d11STodd Fiala #ifndef PTRACE_GETREGS 82af245d11STodd Fiala #define PTRACE_GETREGS 12 83af245d11STodd Fiala #endif 84dda61943STodd Fiala #endif 85dda61943STodd Fiala #ifndef PT_SETREGS 86af245d11STodd Fiala #ifndef PTRACE_SETREGS 87af245d11STodd Fiala #define PTRACE_SETREGS 13 88af245d11STodd Fiala #endif 89dda61943STodd Fiala #endif 90dda61943STodd Fiala #ifndef PT_GETFPREGS 91dda61943STodd Fiala #ifndef PTRACE_GETFPREGS 92dda61943STodd Fiala #define PTRACE_GETFPREGS 14 93dda61943STodd Fiala #endif 94dda61943STodd Fiala #endif 95dda61943STodd Fiala #ifndef PT_SETFPREGS 96dda61943STodd Fiala #ifndef PTRACE_SETFPREGS 97dda61943STodd Fiala #define PTRACE_SETFPREGS 15 98dda61943STodd Fiala #endif 99dda61943STodd Fiala #endif 100af245d11STodd Fiala #ifndef PTRACE_GETREGSET 101af245d11STodd Fiala #define PTRACE_GETREGSET 0x4204 102af245d11STodd Fiala #endif 103af245d11STodd Fiala #ifndef PTRACE_SETREGSET 104af245d11STodd Fiala #define PTRACE_SETREGSET 0x4205 105af245d11STodd Fiala #endif 106af245d11STodd Fiala #ifndef PTRACE_GET_THREAD_AREA 107af245d11STodd Fiala #define PTRACE_GET_THREAD_AREA 25 108af245d11STodd Fiala #endif 109af245d11STodd Fiala #ifndef PTRACE_ARCH_PRCTL 110af245d11STodd Fiala #define PTRACE_ARCH_PRCTL 30 111af245d11STodd Fiala #endif 112af245d11STodd Fiala #ifndef ARCH_GET_FS 113af245d11STodd Fiala #define ARCH_SET_GS 0x1001 114af245d11STodd Fiala #define ARCH_SET_FS 0x1002 115af245d11STodd Fiala #define ARCH_GET_FS 0x1003 116af245d11STodd Fiala #define ARCH_GET_GS 0x1004 117af245d11STodd Fiala #endif 118af245d11STodd Fiala 1190bce1b67STodd Fiala #define LLDB_PERSONALITY_GET_CURRENT_SETTINGS 0xffffffff 120af245d11STodd Fiala 121af245d11STodd Fiala // Support hardware breakpoints in case it has not been defined 122af245d11STodd Fiala #ifndef TRAP_HWBKPT 123af245d11STodd Fiala #define TRAP_HWBKPT 4 124af245d11STodd Fiala #endif 125af245d11STodd Fiala 126af245d11STodd Fiala // Try to define a macro to encapsulate the tgkill syscall 127af245d11STodd Fiala // fall back on kill() if tgkill isn't available 128c9346597SChaoren Lin #define tgkill(pid, tid, sig) \ 129c9346597SChaoren Lin syscall(SYS_tgkill, static_cast<::pid_t>(pid), static_cast<::pid_t>(tid), sig) 130af245d11STodd Fiala 131af245d11STodd Fiala // We disable the tracing of ptrace calls for integration builds to 132af245d11STodd Fiala // avoid the additional indirection and checks. 133af245d11STodd Fiala #ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION 13497ccc294SChaoren Lin #define PTRACE(req, pid, addr, data, data_size, error) \ 13597ccc294SChaoren Lin PtraceWrapper((req), (pid), (addr), (data), (data_size), (error), #req, __FILE__, __LINE__) 136af245d11STodd Fiala #else 13797ccc294SChaoren Lin #define PTRACE(req, pid, addr, data, data_size, error) \ 13897ccc294SChaoren Lin PtraceWrapper((req), (pid), (addr), (data), (data_size), (error)) 139af245d11STodd Fiala #endif 140af245d11STodd Fiala 1417cb18bf5STamas Berghammer using namespace lldb; 1427cb18bf5STamas Berghammer using namespace lldb_private; 143db264a6dSTamas Berghammer using namespace lldb_private::process_linux; 1447cb18bf5STamas Berghammer using namespace llvm; 1457cb18bf5STamas Berghammer 146af245d11STodd Fiala // Private bits we only need internally. 147af245d11STodd Fiala namespace 148af245d11STodd Fiala { 14943f2d971STamas Berghammer static void * const EXIT_OPERATION = nullptr; 15043f2d971STamas Berghammer 151af245d11STodd Fiala const UnixSignals& 152af245d11STodd Fiala GetUnixSignals () 153af245d11STodd Fiala { 154af245d11STodd Fiala static process_linux::LinuxSignals signals; 155af245d11STodd Fiala return signals; 156af245d11STodd Fiala } 157af245d11STodd Fiala 158fa03ad2eSChaoren Lin ThreadStateCoordinator::LogFunction 159fa03ad2eSChaoren Lin GetThreadLoggerFunction () 160fa03ad2eSChaoren Lin { 161fa03ad2eSChaoren Lin return [](const char *format, va_list args) 162fa03ad2eSChaoren Lin { 163fa03ad2eSChaoren Lin Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 164fa03ad2eSChaoren Lin if (log) 165fa03ad2eSChaoren Lin log->VAPrintf (format, args); 166fa03ad2eSChaoren Lin }; 167fa03ad2eSChaoren Lin } 168fa03ad2eSChaoren Lin 169fa03ad2eSChaoren Lin void 170fa03ad2eSChaoren Lin CoordinatorErrorHandler (const std::string &error_message) 171fa03ad2eSChaoren Lin { 172fa03ad2eSChaoren Lin Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 173fa03ad2eSChaoren Lin if (log) 17486fd8e45SChaoren Lin log->Printf ("NativeProcessLinux::%s %s", __FUNCTION__, error_message.c_str ()); 175fa03ad2eSChaoren Lin assert (false && "ThreadStateCoordinator error reported"); 176fa03ad2eSChaoren Lin } 177fa03ad2eSChaoren Lin 178af245d11STodd Fiala Error 179af245d11STodd Fiala ResolveProcessArchitecture (lldb::pid_t pid, Platform &platform, ArchSpec &arch) 180af245d11STodd Fiala { 181af245d11STodd Fiala // Grab process info for the running process. 182af245d11STodd Fiala ProcessInstanceInfo process_info; 183af245d11STodd Fiala if (!platform.GetProcessInfo (pid, process_info)) 184db264a6dSTamas Berghammer return Error("failed to get process info"); 185af245d11STodd Fiala 186af245d11STodd Fiala // Resolve the executable module. 187af245d11STodd Fiala ModuleSP exe_module_sp; 188e56f6dceSChaoren Lin ModuleSpec exe_module_spec(process_info.GetExecutableFile(), process_info.GetArchitecture()); 189af245d11STodd Fiala FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths ()); 190af245d11STodd Fiala Error error = platform.ResolveExecutable( 19154539338SOleksiy Vyalov exe_module_spec, 192af245d11STodd Fiala exe_module_sp, 193af245d11STodd Fiala executable_search_paths.GetSize () ? &executable_search_paths : NULL); 194af245d11STodd Fiala 195af245d11STodd Fiala if (!error.Success ()) 196af245d11STodd Fiala return error; 197af245d11STodd Fiala 198af245d11STodd Fiala // Check if we've got our architecture from the exe_module. 199af245d11STodd Fiala arch = exe_module_sp->GetArchitecture (); 200af245d11STodd Fiala if (arch.IsValid ()) 201af245d11STodd Fiala return Error(); 202af245d11STodd Fiala else 203af245d11STodd Fiala return Error("failed to retrieve a valid architecture from the exe module"); 204af245d11STodd Fiala } 205af245d11STodd Fiala 206af245d11STodd Fiala void 207db264a6dSTamas Berghammer DisplayBytes (StreamString &s, void *bytes, uint32_t count) 208af245d11STodd Fiala { 209af245d11STodd Fiala uint8_t *ptr = (uint8_t *)bytes; 210af245d11STodd Fiala const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count); 211af245d11STodd Fiala for(uint32_t i=0; i<loop_count; i++) 212af245d11STodd Fiala { 213af245d11STodd Fiala s.Printf ("[%x]", *ptr); 214af245d11STodd Fiala ptr++; 215af245d11STodd Fiala } 216af245d11STodd Fiala } 217af245d11STodd Fiala 218af245d11STodd Fiala void 219af245d11STodd Fiala PtraceDisplayBytes(int &req, void *data, size_t data_size) 220af245d11STodd Fiala { 221af245d11STodd Fiala StreamString buf; 222af245d11STodd Fiala Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet ( 223af245d11STodd Fiala POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE)); 224af245d11STodd Fiala 225af245d11STodd Fiala if (verbose_log) 226af245d11STodd Fiala { 227af245d11STodd Fiala switch(req) 228af245d11STodd Fiala { 229af245d11STodd Fiala case PTRACE_POKETEXT: 230af245d11STodd Fiala { 231af245d11STodd Fiala DisplayBytes(buf, &data, 8); 232af245d11STodd Fiala verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData()); 233af245d11STodd Fiala break; 234af245d11STodd Fiala } 235af245d11STodd Fiala case PTRACE_POKEDATA: 236af245d11STodd Fiala { 237af245d11STodd Fiala DisplayBytes(buf, &data, 8); 238af245d11STodd Fiala verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData()); 239af245d11STodd Fiala break; 240af245d11STodd Fiala } 241af245d11STodd Fiala case PTRACE_POKEUSER: 242af245d11STodd Fiala { 243af245d11STodd Fiala DisplayBytes(buf, &data, 8); 244af245d11STodd Fiala verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData()); 245af245d11STodd Fiala break; 246af245d11STodd Fiala } 247af245d11STodd Fiala case PTRACE_SETREGS: 248af245d11STodd Fiala { 249af245d11STodd Fiala DisplayBytes(buf, data, data_size); 250af245d11STodd Fiala verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData()); 251af245d11STodd Fiala break; 252af245d11STodd Fiala } 253af245d11STodd Fiala case PTRACE_SETFPREGS: 254af245d11STodd Fiala { 255af245d11STodd Fiala DisplayBytes(buf, data, data_size); 256af245d11STodd Fiala verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData()); 257af245d11STodd Fiala break; 258af245d11STodd Fiala } 259af245d11STodd Fiala case PTRACE_SETSIGINFO: 260af245d11STodd Fiala { 261af245d11STodd Fiala DisplayBytes(buf, data, sizeof(siginfo_t)); 262af245d11STodd Fiala verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData()); 263af245d11STodd Fiala break; 264af245d11STodd Fiala } 265af245d11STodd Fiala case PTRACE_SETREGSET: 266af245d11STodd Fiala { 267af245d11STodd Fiala // Extract iov_base from data, which is a pointer to the struct IOVEC 268af245d11STodd Fiala DisplayBytes(buf, *(void **)data, data_size); 269af245d11STodd Fiala verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData()); 270af245d11STodd Fiala break; 271af245d11STodd Fiala } 272af245d11STodd Fiala default: 273af245d11STodd Fiala { 274af245d11STodd Fiala } 275af245d11STodd Fiala } 276af245d11STodd Fiala } 277af245d11STodd Fiala } 278af245d11STodd Fiala 279af245d11STodd Fiala // Wrapper for ptrace to catch errors and log calls. 280af245d11STodd Fiala // Note that ptrace sets errno on error because -1 can be a valid result (i.e. for PTRACE_PEEK*) 281af245d11STodd Fiala long 28297ccc294SChaoren Lin PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, Error& error, 283af245d11STodd Fiala const char* reqName, const char* file, int line) 284af245d11STodd Fiala { 285af245d11STodd Fiala long int result; 286af245d11STodd Fiala 287af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE)); 288af245d11STodd Fiala 289af245d11STodd Fiala PtraceDisplayBytes(req, data, data_size); 290af245d11STodd Fiala 29197ccc294SChaoren Lin error.Clear(); 292af245d11STodd Fiala errno = 0; 293af245d11STodd Fiala if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) 294af245d11STodd Fiala result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), *(unsigned int *)addr, data); 295af245d11STodd Fiala else 296af245d11STodd Fiala result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), addr, data); 297af245d11STodd Fiala 29897ccc294SChaoren Lin if (result == -1) 29997ccc294SChaoren Lin error.SetErrorToErrno(); 30097ccc294SChaoren Lin 301af245d11STodd Fiala if (log) 302af245d11STodd Fiala log->Printf("ptrace(%s, %" PRIu64 ", %p, %p, %zu)=%lX called from file %s line %d", 303af245d11STodd Fiala reqName, pid, addr, data, data_size, result, file, line); 304af245d11STodd Fiala 305af245d11STodd Fiala PtraceDisplayBytes(req, data, data_size); 306af245d11STodd Fiala 30797ccc294SChaoren Lin if (log && error.GetError() != 0) 308af245d11STodd Fiala { 309af245d11STodd Fiala const char* str; 31097ccc294SChaoren Lin switch (error.GetError()) 311af245d11STodd Fiala { 312af245d11STodd Fiala case ESRCH: str = "ESRCH"; break; 313af245d11STodd Fiala case EINVAL: str = "EINVAL"; break; 314af245d11STodd Fiala case EBUSY: str = "EBUSY"; break; 315af245d11STodd Fiala case EPERM: str = "EPERM"; break; 31697ccc294SChaoren Lin default: str = error.AsCString(); 317af245d11STodd Fiala } 31897ccc294SChaoren Lin log->Printf("ptrace() failed; errno=%d (%s)", error.GetError(), str); 319af245d11STodd Fiala } 320af245d11STodd Fiala 321af245d11STodd Fiala return result; 322af245d11STodd Fiala } 323af245d11STodd Fiala 324af245d11STodd Fiala #ifdef LLDB_CONFIGURATION_BUILDANDINTEGRATION 325af245d11STodd Fiala // Wrapper for ptrace when logging is not required. 326af245d11STodd Fiala // Sets errno to 0 prior to calling ptrace. 327af245d11STodd Fiala long 32897ccc294SChaoren Lin PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, Error& error) 329af245d11STodd Fiala { 330af245d11STodd Fiala long result = 0; 33197ccc294SChaoren Lin 33297ccc294SChaoren Lin error.Clear(); 333af245d11STodd Fiala errno = 0; 334af245d11STodd Fiala if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) 335af245d11STodd Fiala result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), *(unsigned int *)addr, data); 336af245d11STodd Fiala else 337af245d11STodd Fiala result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), addr, data); 33897ccc294SChaoren Lin 33997ccc294SChaoren Lin if (result == -1) 34097ccc294SChaoren Lin error.SetErrorToErrno(); 341af245d11STodd Fiala return result; 342af245d11STodd Fiala } 343af245d11STodd Fiala #endif 344af245d11STodd Fiala 345af245d11STodd Fiala //------------------------------------------------------------------------------ 346af245d11STodd Fiala // Static implementations of NativeProcessLinux::ReadMemory and 347af245d11STodd Fiala // NativeProcessLinux::WriteMemory. This enables mutual recursion between these 348af245d11STodd Fiala // functions without needed to go thru the thread funnel. 349af245d11STodd Fiala 35097ccc294SChaoren Lin lldb::addr_t 351af245d11STodd Fiala DoReadMemory ( 352af245d11STodd Fiala lldb::pid_t pid, 353af245d11STodd Fiala lldb::addr_t vm_addr, 354af245d11STodd Fiala void *buf, 355af245d11STodd Fiala lldb::addr_t size, 356af245d11STodd Fiala Error &error) 357af245d11STodd Fiala { 358af245d11STodd Fiala // ptrace word size is determined by the host, not the child 359af245d11STodd Fiala static const unsigned word_size = sizeof(void*); 360af245d11STodd Fiala unsigned char *dst = static_cast<unsigned char*>(buf); 361af245d11STodd Fiala lldb::addr_t bytes_read; 362af245d11STodd Fiala lldb::addr_t remainder; 363af245d11STodd Fiala long data; 364af245d11STodd Fiala 365af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); 366af245d11STodd Fiala if (log) 367af245d11STodd Fiala ProcessPOSIXLog::IncNestLevel(); 368af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) 369af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__, 370af245d11STodd Fiala pid, word_size, (void*)vm_addr, buf, size); 371af245d11STodd Fiala 372af245d11STodd Fiala assert(sizeof(data) >= word_size); 373af245d11STodd Fiala for (bytes_read = 0; bytes_read < size; bytes_read += remainder) 374af245d11STodd Fiala { 37597ccc294SChaoren Lin data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, nullptr, 0, error); 37697ccc294SChaoren Lin if (error.Fail()) 377af245d11STodd Fiala { 378af245d11STodd Fiala if (log) 379af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 380af245d11STodd Fiala return bytes_read; 381af245d11STodd Fiala } 382af245d11STodd Fiala 383af245d11STodd Fiala remainder = size - bytes_read; 384af245d11STodd Fiala remainder = remainder > word_size ? word_size : remainder; 385af245d11STodd Fiala 386af245d11STodd Fiala // Copy the data into our buffer 387af245d11STodd Fiala for (unsigned i = 0; i < remainder; ++i) 388af245d11STodd Fiala dst[i] = ((data >> i*8) & 0xFF); 389af245d11STodd Fiala 390af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && 391af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 392af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 393af245d11STodd Fiala size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 394af245d11STodd Fiala { 395af245d11STodd Fiala uintptr_t print_dst = 0; 396af245d11STodd Fiala // Format bytes from data by moving into print_dst for log output 397af245d11STodd Fiala for (unsigned i = 0; i < remainder; ++i) 398af245d11STodd Fiala print_dst |= (((data >> i*8) & 0xFF) << i*8); 399af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 400af245d11STodd Fiala (void*)vm_addr, print_dst, (unsigned long)data); 401af245d11STodd Fiala } 402af245d11STodd Fiala 403af245d11STodd Fiala vm_addr += word_size; 404af245d11STodd Fiala dst += word_size; 405af245d11STodd Fiala } 406af245d11STodd Fiala 407af245d11STodd Fiala if (log) 408af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 409af245d11STodd Fiala return bytes_read; 410af245d11STodd Fiala } 411af245d11STodd Fiala 41297ccc294SChaoren Lin lldb::addr_t 413af245d11STodd Fiala DoWriteMemory( 414af245d11STodd Fiala lldb::pid_t pid, 415af245d11STodd Fiala lldb::addr_t vm_addr, 416af245d11STodd Fiala const void *buf, 417af245d11STodd Fiala lldb::addr_t size, 418af245d11STodd Fiala Error &error) 419af245d11STodd Fiala { 420af245d11STodd Fiala // ptrace word size is determined by the host, not the child 421af245d11STodd Fiala static const unsigned word_size = sizeof(void*); 422af245d11STodd Fiala const unsigned char *src = static_cast<const unsigned char*>(buf); 423af245d11STodd Fiala lldb::addr_t bytes_written = 0; 424af245d11STodd Fiala lldb::addr_t remainder; 425af245d11STodd Fiala 426af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); 427af245d11STodd Fiala if (log) 428af245d11STodd Fiala ProcessPOSIXLog::IncNestLevel(); 429af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) 430af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(%" PRIu64 ", %u, %p, %p, %" PRIu64 ")", __FUNCTION__, 431af245d11STodd Fiala pid, word_size, (void*)vm_addr, buf, size); 432af245d11STodd Fiala 433af245d11STodd Fiala for (bytes_written = 0; bytes_written < size; bytes_written += remainder) 434af245d11STodd Fiala { 435af245d11STodd Fiala remainder = size - bytes_written; 436af245d11STodd Fiala remainder = remainder > word_size ? word_size : remainder; 437af245d11STodd Fiala 438af245d11STodd Fiala if (remainder == word_size) 439af245d11STodd Fiala { 440af245d11STodd Fiala unsigned long data = 0; 441af245d11STodd Fiala assert(sizeof(data) >= word_size); 442af245d11STodd Fiala for (unsigned i = 0; i < word_size; ++i) 443af245d11STodd Fiala data |= (unsigned long)src[i] << i*8; 444af245d11STodd Fiala 445af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && 446af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 447af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 448af245d11STodd Fiala size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 449af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 4501e209fccSTamas Berghammer (void*)vm_addr, *(const unsigned long*)src, data); 451af245d11STodd Fiala 45297ccc294SChaoren Lin if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0, error)) 453af245d11STodd Fiala { 454af245d11STodd Fiala if (log) 455af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 456af245d11STodd Fiala return bytes_written; 457af245d11STodd Fiala } 458af245d11STodd Fiala } 459af245d11STodd Fiala else 460af245d11STodd Fiala { 461af245d11STodd Fiala unsigned char buff[8]; 462af245d11STodd Fiala if (DoReadMemory(pid, vm_addr, 463af245d11STodd Fiala buff, word_size, error) != word_size) 464af245d11STodd Fiala { 465af245d11STodd Fiala if (log) 466af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 467af245d11STodd Fiala return bytes_written; 468af245d11STodd Fiala } 469af245d11STodd Fiala 470af245d11STodd Fiala memcpy(buff, src, remainder); 471af245d11STodd Fiala 472af245d11STodd Fiala if (DoWriteMemory(pid, vm_addr, 473af245d11STodd Fiala buff, word_size, error) != word_size) 474af245d11STodd Fiala { 475af245d11STodd Fiala if (log) 476af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 477af245d11STodd Fiala return bytes_written; 478af245d11STodd Fiala } 479af245d11STodd Fiala 480af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && 481af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 482af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 483af245d11STodd Fiala size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 484af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 4851e209fccSTamas Berghammer (void*)vm_addr, *(const unsigned long*)src, *(unsigned long*)buff); 486af245d11STodd Fiala } 487af245d11STodd Fiala 488af245d11STodd Fiala vm_addr += word_size; 489af245d11STodd Fiala src += word_size; 490af245d11STodd Fiala } 491af245d11STodd Fiala if (log) 492af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 493af245d11STodd Fiala return bytes_written; 494af245d11STodd Fiala } 495af245d11STodd Fiala 496af245d11STodd Fiala //------------------------------------------------------------------------------ 497af245d11STodd Fiala /// @class Operation 498af245d11STodd Fiala /// @brief Represents a NativeProcessLinux operation. 499af245d11STodd Fiala /// 500af245d11STodd Fiala /// Under Linux, it is not possible to ptrace() from any other thread but the 501af245d11STodd Fiala /// one that spawned or attached to the process from the start. Therefore, when 502af245d11STodd Fiala /// a NativeProcessLinux is asked to deliver or change the state of an inferior 503af245d11STodd Fiala /// process the operation must be "funneled" to a specific thread to perform the 504af245d11STodd Fiala /// task. The Operation class provides an abstract base for all services the 505af245d11STodd Fiala /// NativeProcessLinux must perform via the single virtual function Execute, thus 506af245d11STodd Fiala /// encapsulating the code that needs to run in the privileged context. 507af245d11STodd Fiala class Operation 508af245d11STodd Fiala { 509af245d11STodd Fiala public: 510af245d11STodd Fiala Operation () : m_error() { } 511af245d11STodd Fiala 512af245d11STodd Fiala virtual 513af245d11STodd Fiala ~Operation() {} 514af245d11STodd Fiala 515af245d11STodd Fiala virtual void 516af245d11STodd Fiala Execute (NativeProcessLinux *process) = 0; 517af245d11STodd Fiala 518af245d11STodd Fiala const Error & 519af245d11STodd Fiala GetError () const { return m_error; } 520af245d11STodd Fiala 521af245d11STodd Fiala protected: 522af245d11STodd Fiala Error m_error; 523af245d11STodd Fiala }; 524af245d11STodd Fiala 525af245d11STodd Fiala //------------------------------------------------------------------------------ 526af245d11STodd Fiala /// @class ReadOperation 527af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadMemory. 528af245d11STodd Fiala class ReadOperation : public Operation 529af245d11STodd Fiala { 530af245d11STodd Fiala public: 531af245d11STodd Fiala ReadOperation ( 532af245d11STodd Fiala lldb::addr_t addr, 533af245d11STodd Fiala void *buff, 534af245d11STodd Fiala lldb::addr_t size, 535b35103ebSTodd Fiala lldb::addr_t &result) : 536af245d11STodd Fiala Operation (), 537af245d11STodd Fiala m_addr (addr), 538af245d11STodd Fiala m_buff (buff), 539af245d11STodd Fiala m_size (size), 540af245d11STodd Fiala m_result (result) 541af245d11STodd Fiala { 542af245d11STodd Fiala } 543af245d11STodd Fiala 544af245d11STodd Fiala void Execute (NativeProcessLinux *process) override; 545af245d11STodd Fiala 546af245d11STodd Fiala private: 547af245d11STodd Fiala lldb::addr_t m_addr; 548af245d11STodd Fiala void *m_buff; 549af245d11STodd Fiala lldb::addr_t m_size; 550af245d11STodd Fiala lldb::addr_t &m_result; 551af245d11STodd Fiala }; 552af245d11STodd Fiala 553af245d11STodd Fiala void 554af245d11STodd Fiala ReadOperation::Execute (NativeProcessLinux *process) 555af245d11STodd Fiala { 556af245d11STodd Fiala m_result = DoReadMemory (process->GetID (), m_addr, m_buff, m_size, m_error); 557af245d11STodd Fiala } 558af245d11STodd Fiala 559af245d11STodd Fiala //------------------------------------------------------------------------------ 560af245d11STodd Fiala /// @class WriteOperation 561af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteMemory. 562af245d11STodd Fiala class WriteOperation : public Operation 563af245d11STodd Fiala { 564af245d11STodd Fiala public: 565af245d11STodd Fiala WriteOperation ( 566af245d11STodd Fiala lldb::addr_t addr, 567af245d11STodd Fiala const void *buff, 568af245d11STodd Fiala lldb::addr_t size, 569af245d11STodd Fiala lldb::addr_t &result) : 570af245d11STodd Fiala Operation (), 571af245d11STodd Fiala m_addr (addr), 572af245d11STodd Fiala m_buff (buff), 573af245d11STodd Fiala m_size (size), 574af245d11STodd Fiala m_result (result) 575af245d11STodd Fiala { 576af245d11STodd Fiala } 577af245d11STodd Fiala 578af245d11STodd Fiala void Execute (NativeProcessLinux *process) override; 579af245d11STodd Fiala 580af245d11STodd Fiala private: 581af245d11STodd Fiala lldb::addr_t m_addr; 582af245d11STodd Fiala const void *m_buff; 583af245d11STodd Fiala lldb::addr_t m_size; 584af245d11STodd Fiala lldb::addr_t &m_result; 585af245d11STodd Fiala }; 586af245d11STodd Fiala 587af245d11STodd Fiala void 588af245d11STodd Fiala WriteOperation::Execute(NativeProcessLinux *process) 589af245d11STodd Fiala { 590af245d11STodd Fiala m_result = DoWriteMemory (process->GetID (), m_addr, m_buff, m_size, m_error); 591af245d11STodd Fiala } 592af245d11STodd Fiala 593af245d11STodd Fiala //------------------------------------------------------------------------------ 594af245d11STodd Fiala /// @class ReadRegOperation 595af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadRegisterValue. 596af245d11STodd Fiala class ReadRegOperation : public Operation 597af245d11STodd Fiala { 598af245d11STodd Fiala public: 599af245d11STodd Fiala ReadRegOperation(lldb::tid_t tid, uint32_t offset, const char *reg_name, 60097ccc294SChaoren Lin RegisterValue &value) 60197ccc294SChaoren Lin : m_tid(tid), 60297ccc294SChaoren Lin m_offset(static_cast<uintptr_t> (offset)), 60397ccc294SChaoren Lin m_reg_name(reg_name), 60497ccc294SChaoren Lin m_value(value) 605af245d11STodd Fiala { } 606af245d11STodd Fiala 607d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 608af245d11STodd Fiala 609af245d11STodd Fiala private: 610af245d11STodd Fiala lldb::tid_t m_tid; 611af245d11STodd Fiala uintptr_t m_offset; 612af245d11STodd Fiala const char *m_reg_name; 613af245d11STodd Fiala RegisterValue &m_value; 614af245d11STodd Fiala }; 615af245d11STodd Fiala 616af245d11STodd Fiala void 617af245d11STodd Fiala ReadRegOperation::Execute(NativeProcessLinux *monitor) 618af245d11STodd Fiala { 6190fceef80STodd Fiala #if defined (__arm64__) || defined (__aarch64__) 6200fceef80STodd Fiala if (m_offset > sizeof(struct user_pt_regs)) 6210fceef80STodd Fiala { 6220fceef80STodd Fiala uintptr_t offset = m_offset - sizeof(struct user_pt_regs); 6230fceef80STodd Fiala if (offset > sizeof(struct user_fpsimd_state)) 6240fceef80STodd Fiala { 62597ccc294SChaoren Lin m_error.SetErrorString("invalid offset value"); 62697ccc294SChaoren Lin return; 6270fceef80STodd Fiala } 6280fceef80STodd Fiala elf_fpregset_t regs; 6290fceef80STodd Fiala int regset = NT_FPREGSET; 6300fceef80STodd Fiala struct iovec ioVec; 6310fceef80STodd Fiala 6320fceef80STodd Fiala ioVec.iov_base = ®s; 6330fceef80STodd Fiala ioVec.iov_len = sizeof regs; 63497ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 63597ccc294SChaoren Lin if (m_error.Success()) 6360fceef80STodd Fiala { 637db264a6dSTamas Berghammer ArchSpec arch; 6380fceef80STodd Fiala if (monitor->GetArchitecture(arch)) 6390fceef80STodd Fiala m_value.SetBytes((void *)(((unsigned char *)(®s)) + offset), 16, arch.GetByteOrder()); 6400fceef80STodd Fiala else 64197ccc294SChaoren Lin m_error.SetErrorString("failed to get architecture"); 6420fceef80STodd Fiala } 6430fceef80STodd Fiala } 6440fceef80STodd Fiala else 6450fceef80STodd Fiala { 6460fceef80STodd Fiala elf_gregset_t regs; 6470fceef80STodd Fiala int regset = NT_PRSTATUS; 6480fceef80STodd Fiala struct iovec ioVec; 6490fceef80STodd Fiala 6500fceef80STodd Fiala ioVec.iov_base = ®s; 6510fceef80STodd Fiala ioVec.iov_len = sizeof regs; 65297ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 65397ccc294SChaoren Lin if (m_error.Success()) 6540fceef80STodd Fiala { 655db264a6dSTamas Berghammer ArchSpec arch; 6560fceef80STodd Fiala if (monitor->GetArchitecture(arch)) 6570fceef80STodd Fiala m_value.SetBytes((void *)(((unsigned char *)(regs)) + m_offset), 8, arch.GetByteOrder()); 65897ccc294SChaoren Lin else 65997ccc294SChaoren Lin m_error.SetErrorString("failed to get architecture"); 6600fceef80STodd Fiala } 6610fceef80STodd Fiala } 66209ba1a32SMohit K. Bhakkad #elif defined (__mips__) 66309ba1a32SMohit K. Bhakkad elf_gregset_t regs; 66409ba1a32SMohit K. Bhakkad PTRACE(PTRACE_GETREGS, m_tid, NULL, ®s, sizeof regs, m_error); 66509ba1a32SMohit K. Bhakkad if (m_error.Success()) 66609ba1a32SMohit K. Bhakkad { 66709ba1a32SMohit K. Bhakkad lldb_private::ArchSpec arch; 66809ba1a32SMohit K. Bhakkad if (monitor->GetArchitecture(arch)) 66909ba1a32SMohit K. Bhakkad m_value.SetBytes((void *)(((unsigned char *)(regs)) + m_offset), 8, arch.GetByteOrder()); 67009ba1a32SMohit K. Bhakkad else 67109ba1a32SMohit K. Bhakkad m_error.SetErrorString("failed to get architecture"); 67209ba1a32SMohit K. Bhakkad } 6730fceef80STodd Fiala #else 674af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS)); 675af245d11STodd Fiala 676adf8adbdSTamas Berghammer lldb::addr_t data = static_cast<unsigned long>(PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, nullptr, 0, m_error)); 67797ccc294SChaoren Lin if (m_error.Success()) 678af245d11STodd Fiala m_value = data; 67997ccc294SChaoren Lin 680af245d11STodd Fiala if (log) 681af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() reg %s: 0x%" PRIx64, __FUNCTION__, 682af245d11STodd Fiala m_reg_name, data); 6830fceef80STodd Fiala #endif 684af245d11STodd Fiala } 685af245d11STodd Fiala 686af245d11STodd Fiala //------------------------------------------------------------------------------ 687af245d11STodd Fiala /// @class WriteRegOperation 688af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteRegisterValue. 689af245d11STodd Fiala class WriteRegOperation : public Operation 690af245d11STodd Fiala { 691af245d11STodd Fiala public: 692af245d11STodd Fiala WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name, 69397ccc294SChaoren Lin const RegisterValue &value) 69497ccc294SChaoren Lin : m_tid(tid), 69597ccc294SChaoren Lin m_offset(offset), 69697ccc294SChaoren Lin m_reg_name(reg_name), 69797ccc294SChaoren Lin m_value(value) 698af245d11STodd Fiala { } 699af245d11STodd Fiala 700d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 701af245d11STodd Fiala 702af245d11STodd Fiala private: 703af245d11STodd Fiala lldb::tid_t m_tid; 704af245d11STodd Fiala uintptr_t m_offset; 705af245d11STodd Fiala const char *m_reg_name; 706af245d11STodd Fiala const RegisterValue &m_value; 707af245d11STodd Fiala }; 708af245d11STodd Fiala 709af245d11STodd Fiala void 710af245d11STodd Fiala WriteRegOperation::Execute(NativeProcessLinux *monitor) 711af245d11STodd Fiala { 7120fceef80STodd Fiala #if defined (__arm64__) || defined (__aarch64__) 7130fceef80STodd Fiala if (m_offset > sizeof(struct user_pt_regs)) 7140fceef80STodd Fiala { 7150fceef80STodd Fiala uintptr_t offset = m_offset - sizeof(struct user_pt_regs); 7160fceef80STodd Fiala if (offset > sizeof(struct user_fpsimd_state)) 7170fceef80STodd Fiala { 71897ccc294SChaoren Lin m_error.SetErrorString("invalid offset value"); 71997ccc294SChaoren Lin return; 7200fceef80STodd Fiala } 7210fceef80STodd Fiala elf_fpregset_t regs; 7220fceef80STodd Fiala int regset = NT_FPREGSET; 7230fceef80STodd Fiala struct iovec ioVec; 7240fceef80STodd Fiala 7250fceef80STodd Fiala ioVec.iov_base = ®s; 7260fceef80STodd Fiala ioVec.iov_len = sizeof regs; 72797ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 7289425b329SBhushan D. Attarde if (m_error.Success()) 7290fceef80STodd Fiala { 7300fceef80STodd Fiala ::memcpy((void *)(((unsigned char *)(®s)) + offset), m_value.GetBytes(), 16); 73197ccc294SChaoren Lin PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 7320fceef80STodd Fiala } 7330fceef80STodd Fiala } 7340fceef80STodd Fiala else 7350fceef80STodd Fiala { 7360fceef80STodd Fiala elf_gregset_t regs; 7370fceef80STodd Fiala int regset = NT_PRSTATUS; 7380fceef80STodd Fiala struct iovec ioVec; 7390fceef80STodd Fiala 7400fceef80STodd Fiala ioVec.iov_base = ®s; 7410fceef80STodd Fiala ioVec.iov_len = sizeof regs; 74297ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 7439425b329SBhushan D. Attarde if (m_error.Success()) 7440fceef80STodd Fiala { 7450fceef80STodd Fiala ::memcpy((void *)(((unsigned char *)(®s)) + m_offset), m_value.GetBytes(), 8); 74697ccc294SChaoren Lin PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 7470fceef80STodd Fiala } 7480fceef80STodd Fiala } 74909ba1a32SMohit K. Bhakkad #elif defined (__mips__) 75009ba1a32SMohit K. Bhakkad elf_gregset_t regs; 75109ba1a32SMohit K. Bhakkad PTRACE(PTRACE_GETREGS, m_tid, NULL, ®s, sizeof regs, m_error); 75209ba1a32SMohit K. Bhakkad if (m_error.Success()) 75309ba1a32SMohit K. Bhakkad { 75409ba1a32SMohit K. Bhakkad ::memcpy((void *)(((unsigned char *)(®s)) + m_offset), m_value.GetBytes(), 8); 75509ba1a32SMohit K. Bhakkad PTRACE(PTRACE_SETREGS, m_tid, NULL, ®s, sizeof regs, m_error); 75609ba1a32SMohit K. Bhakkad } 7570fceef80STodd Fiala #else 758af245d11STodd Fiala void* buf; 759af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS)); 760af245d11STodd Fiala 761af245d11STodd Fiala buf = (void*) m_value.GetAsUInt64(); 762af245d11STodd Fiala 763af245d11STodd Fiala if (log) 764af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf); 76597ccc294SChaoren Lin PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0, m_error); 7660fceef80STodd Fiala #endif 767af245d11STodd Fiala } 768af245d11STodd Fiala 769af245d11STodd Fiala //------------------------------------------------------------------------------ 770af245d11STodd Fiala /// @class ReadGPROperation 771af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadGPR. 772af245d11STodd Fiala class ReadGPROperation : public Operation 773af245d11STodd Fiala { 774af245d11STodd Fiala public: 77597ccc294SChaoren Lin ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size) 77697ccc294SChaoren Lin : m_tid(tid), m_buf(buf), m_buf_size(buf_size) 777af245d11STodd Fiala { } 778af245d11STodd Fiala 779d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 780af245d11STodd Fiala 781af245d11STodd Fiala private: 782af245d11STodd Fiala lldb::tid_t m_tid; 783af245d11STodd Fiala void *m_buf; 784af245d11STodd Fiala size_t m_buf_size; 785af245d11STodd Fiala }; 786af245d11STodd Fiala 787af245d11STodd Fiala void 788af245d11STodd Fiala ReadGPROperation::Execute(NativeProcessLinux *monitor) 789af245d11STodd Fiala { 7906ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 7916ac1be4bSTodd Fiala int regset = NT_PRSTATUS; 7926ac1be4bSTodd Fiala struct iovec ioVec; 7936ac1be4bSTodd Fiala 7946ac1be4bSTodd Fiala ioVec.iov_base = m_buf; 7956ac1be4bSTodd Fiala ioVec.iov_len = m_buf_size; 79697ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); 7976ac1be4bSTodd Fiala #else 79897ccc294SChaoren Lin PTRACE(PTRACE_GETREGS, m_tid, nullptr, m_buf, m_buf_size, m_error); 7996ac1be4bSTodd Fiala #endif 800af245d11STodd Fiala } 801af245d11STodd Fiala 802af245d11STodd Fiala //------------------------------------------------------------------------------ 803af245d11STodd Fiala /// @class ReadFPROperation 804af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadFPR. 805af245d11STodd Fiala class ReadFPROperation : public Operation 806af245d11STodd Fiala { 807af245d11STodd Fiala public: 80897ccc294SChaoren Lin ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size) 80997ccc294SChaoren Lin : m_tid(tid), 81097ccc294SChaoren Lin m_buf(buf), 81197ccc294SChaoren Lin m_buf_size(buf_size) 812af245d11STodd Fiala { } 813af245d11STodd Fiala 814d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 815af245d11STodd Fiala 816af245d11STodd Fiala private: 817af245d11STodd Fiala lldb::tid_t m_tid; 818af245d11STodd Fiala void *m_buf; 819af245d11STodd Fiala size_t m_buf_size; 820af245d11STodd Fiala }; 821af245d11STodd Fiala 822af245d11STodd Fiala void 823af245d11STodd Fiala ReadFPROperation::Execute(NativeProcessLinux *monitor) 824af245d11STodd Fiala { 8256ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 8266ac1be4bSTodd Fiala int regset = NT_FPREGSET; 8276ac1be4bSTodd Fiala struct iovec ioVec; 8286ac1be4bSTodd Fiala 8296ac1be4bSTodd Fiala ioVec.iov_base = m_buf; 8306ac1be4bSTodd Fiala ioVec.iov_len = m_buf_size; 8311e209fccSTamas Berghammer PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); 8326ac1be4bSTodd Fiala #else 83397ccc294SChaoren Lin PTRACE(PTRACE_GETFPREGS, m_tid, nullptr, m_buf, m_buf_size, m_error); 8346ac1be4bSTodd Fiala #endif 835af245d11STodd Fiala } 836af245d11STodd Fiala 837af245d11STodd Fiala //------------------------------------------------------------------------------ 838af245d11STodd Fiala /// @class ReadRegisterSetOperation 839af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadRegisterSet. 840af245d11STodd Fiala class ReadRegisterSetOperation : public Operation 841af245d11STodd Fiala { 842af245d11STodd Fiala public: 84397ccc294SChaoren Lin ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) 84497ccc294SChaoren Lin : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset) 845af245d11STodd Fiala { } 846af245d11STodd Fiala 847d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 848af245d11STodd Fiala 849af245d11STodd Fiala private: 850af245d11STodd Fiala lldb::tid_t m_tid; 851af245d11STodd Fiala void *m_buf; 852af245d11STodd Fiala size_t m_buf_size; 853af245d11STodd Fiala const unsigned int m_regset; 854af245d11STodd Fiala }; 855af245d11STodd Fiala 856af245d11STodd Fiala void 857af245d11STodd Fiala ReadRegisterSetOperation::Execute(NativeProcessLinux *monitor) 858af245d11STodd Fiala { 85997ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size, m_error); 860af245d11STodd Fiala } 861af245d11STodd Fiala 862af245d11STodd Fiala //------------------------------------------------------------------------------ 863af245d11STodd Fiala /// @class WriteGPROperation 864af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteGPR. 865af245d11STodd Fiala class WriteGPROperation : public Operation 866af245d11STodd Fiala { 867af245d11STodd Fiala public: 86897ccc294SChaoren Lin WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size) 86997ccc294SChaoren Lin : m_tid(tid), m_buf(buf), m_buf_size(buf_size) 870af245d11STodd Fiala { } 871af245d11STodd Fiala 872d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 873af245d11STodd Fiala 874af245d11STodd Fiala private: 875af245d11STodd Fiala lldb::tid_t m_tid; 876af245d11STodd Fiala void *m_buf; 877af245d11STodd Fiala size_t m_buf_size; 878af245d11STodd Fiala }; 879af245d11STodd Fiala 880af245d11STodd Fiala void 881af245d11STodd Fiala WriteGPROperation::Execute(NativeProcessLinux *monitor) 882af245d11STodd Fiala { 8836ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 8846ac1be4bSTodd Fiala int regset = NT_PRSTATUS; 8856ac1be4bSTodd Fiala struct iovec ioVec; 8866ac1be4bSTodd Fiala 8876ac1be4bSTodd Fiala ioVec.iov_base = m_buf; 8886ac1be4bSTodd Fiala ioVec.iov_len = m_buf_size; 88997ccc294SChaoren Lin PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); 8906ac1be4bSTodd Fiala #else 89197ccc294SChaoren Lin PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size, m_error); 8926ac1be4bSTodd Fiala #endif 893af245d11STodd Fiala } 894af245d11STodd Fiala 895af245d11STodd Fiala //------------------------------------------------------------------------------ 896af245d11STodd Fiala /// @class WriteFPROperation 897af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteFPR. 898af245d11STodd Fiala class WriteFPROperation : public Operation 899af245d11STodd Fiala { 900af245d11STodd Fiala public: 90197ccc294SChaoren Lin WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size) 90297ccc294SChaoren Lin : m_tid(tid), m_buf(buf), m_buf_size(buf_size) 903af245d11STodd Fiala { } 904af245d11STodd Fiala 905d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 906af245d11STodd Fiala 907af245d11STodd Fiala private: 908af245d11STodd Fiala lldb::tid_t m_tid; 909af245d11STodd Fiala void *m_buf; 910af245d11STodd Fiala size_t m_buf_size; 911af245d11STodd Fiala }; 912af245d11STodd Fiala 913af245d11STodd Fiala void 914af245d11STodd Fiala WriteFPROperation::Execute(NativeProcessLinux *monitor) 915af245d11STodd Fiala { 9166ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 9176ac1be4bSTodd Fiala int regset = NT_FPREGSET; 9186ac1be4bSTodd Fiala struct iovec ioVec; 9196ac1be4bSTodd Fiala 9206ac1be4bSTodd Fiala ioVec.iov_base = m_buf; 9216ac1be4bSTodd Fiala ioVec.iov_len = m_buf_size; 92297ccc294SChaoren Lin PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); 9236ac1be4bSTodd Fiala #else 92497ccc294SChaoren Lin PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size, m_error); 9256ac1be4bSTodd Fiala #endif 926af245d11STodd Fiala } 927af245d11STodd Fiala 928af245d11STodd Fiala //------------------------------------------------------------------------------ 929af245d11STodd Fiala /// @class WriteRegisterSetOperation 930af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteRegisterSet. 931af245d11STodd Fiala class WriteRegisterSetOperation : public Operation 932af245d11STodd Fiala { 933af245d11STodd Fiala public: 93497ccc294SChaoren Lin WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) 93597ccc294SChaoren Lin : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset) 936af245d11STodd Fiala { } 937af245d11STodd Fiala 938d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 939af245d11STodd Fiala 940af245d11STodd Fiala private: 941af245d11STodd Fiala lldb::tid_t m_tid; 942af245d11STodd Fiala void *m_buf; 943af245d11STodd Fiala size_t m_buf_size; 944af245d11STodd Fiala const unsigned int m_regset; 945af245d11STodd Fiala }; 946af245d11STodd Fiala 947af245d11STodd Fiala void 948af245d11STodd Fiala WriteRegisterSetOperation::Execute(NativeProcessLinux *monitor) 949af245d11STodd Fiala { 95097ccc294SChaoren Lin PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size, m_error); 951af245d11STodd Fiala } 952af245d11STodd Fiala 953af245d11STodd Fiala //------------------------------------------------------------------------------ 954af245d11STodd Fiala /// @class ResumeOperation 955af245d11STodd Fiala /// @brief Implements NativeProcessLinux::Resume. 956af245d11STodd Fiala class ResumeOperation : public Operation 957af245d11STodd Fiala { 958af245d11STodd Fiala public: 95997ccc294SChaoren Lin ResumeOperation(lldb::tid_t tid, uint32_t signo) : 96097ccc294SChaoren Lin m_tid(tid), m_signo(signo) { } 961af245d11STodd Fiala 962d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 963af245d11STodd Fiala 964af245d11STodd Fiala private: 965af245d11STodd Fiala lldb::tid_t m_tid; 966af245d11STodd Fiala uint32_t m_signo; 967af245d11STodd Fiala }; 968af245d11STodd Fiala 969af245d11STodd Fiala void 970af245d11STodd Fiala ResumeOperation::Execute(NativeProcessLinux *monitor) 971af245d11STodd Fiala { 972af245d11STodd Fiala intptr_t data = 0; 973af245d11STodd Fiala 974af245d11STodd Fiala if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) 975af245d11STodd Fiala data = m_signo; 976af245d11STodd Fiala 97797ccc294SChaoren Lin PTRACE(PTRACE_CONT, m_tid, nullptr, (void*)data, 0, m_error); 97897ccc294SChaoren Lin if (m_error.Fail()) 979af245d11STodd Fiala { 980af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 981af245d11STodd Fiala 982af245d11STodd Fiala if (log) 98397ccc294SChaoren Lin log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, m_error.AsCString()); 984af245d11STodd Fiala } 985af245d11STodd Fiala } 986af245d11STodd Fiala 987af245d11STodd Fiala //------------------------------------------------------------------------------ 988af245d11STodd Fiala /// @class SingleStepOperation 989af245d11STodd Fiala /// @brief Implements NativeProcessLinux::SingleStep. 990af245d11STodd Fiala class SingleStepOperation : public Operation 991af245d11STodd Fiala { 992af245d11STodd Fiala public: 99397ccc294SChaoren Lin SingleStepOperation(lldb::tid_t tid, uint32_t signo) 99497ccc294SChaoren Lin : m_tid(tid), m_signo(signo) { } 995af245d11STodd Fiala 996d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 997af245d11STodd Fiala 998af245d11STodd Fiala private: 999af245d11STodd Fiala lldb::tid_t m_tid; 1000af245d11STodd Fiala uint32_t m_signo; 1001af245d11STodd Fiala }; 1002af245d11STodd Fiala 1003af245d11STodd Fiala void 1004af245d11STodd Fiala SingleStepOperation::Execute(NativeProcessLinux *monitor) 1005af245d11STodd Fiala { 1006af245d11STodd Fiala intptr_t data = 0; 1007af245d11STodd Fiala 1008af245d11STodd Fiala if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) 1009af245d11STodd Fiala data = m_signo; 1010af245d11STodd Fiala 101197ccc294SChaoren Lin PTRACE(PTRACE_SINGLESTEP, m_tid, nullptr, (void*)data, 0, m_error); 1012af245d11STodd Fiala } 1013af245d11STodd Fiala 1014af245d11STodd Fiala //------------------------------------------------------------------------------ 1015af245d11STodd Fiala /// @class SiginfoOperation 1016af245d11STodd Fiala /// @brief Implements NativeProcessLinux::GetSignalInfo. 1017af245d11STodd Fiala class SiginfoOperation : public Operation 1018af245d11STodd Fiala { 1019af245d11STodd Fiala public: 102097ccc294SChaoren Lin SiginfoOperation(lldb::tid_t tid, void *info) 102197ccc294SChaoren Lin : m_tid(tid), m_info(info) { } 1022af245d11STodd Fiala 1023d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 1024af245d11STodd Fiala 1025af245d11STodd Fiala private: 1026af245d11STodd Fiala lldb::tid_t m_tid; 1027af245d11STodd Fiala void *m_info; 1028af245d11STodd Fiala }; 1029af245d11STodd Fiala 1030af245d11STodd Fiala void 1031af245d11STodd Fiala SiginfoOperation::Execute(NativeProcessLinux *monitor) 1032af245d11STodd Fiala { 103397ccc294SChaoren Lin PTRACE(PTRACE_GETSIGINFO, m_tid, nullptr, m_info, 0, m_error); 1034af245d11STodd Fiala } 1035af245d11STodd Fiala 1036af245d11STodd Fiala //------------------------------------------------------------------------------ 1037af245d11STodd Fiala /// @class EventMessageOperation 1038af245d11STodd Fiala /// @brief Implements NativeProcessLinux::GetEventMessage. 1039af245d11STodd Fiala class EventMessageOperation : public Operation 1040af245d11STodd Fiala { 1041af245d11STodd Fiala public: 104297ccc294SChaoren Lin EventMessageOperation(lldb::tid_t tid, unsigned long *message) 104397ccc294SChaoren Lin : m_tid(tid), m_message(message) { } 1044af245d11STodd Fiala 1045d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 1046af245d11STodd Fiala 1047af245d11STodd Fiala private: 1048af245d11STodd Fiala lldb::tid_t m_tid; 1049af245d11STodd Fiala unsigned long *m_message; 1050af245d11STodd Fiala }; 1051af245d11STodd Fiala 1052af245d11STodd Fiala void 1053af245d11STodd Fiala EventMessageOperation::Execute(NativeProcessLinux *monitor) 1054af245d11STodd Fiala { 105597ccc294SChaoren Lin PTRACE(PTRACE_GETEVENTMSG, m_tid, nullptr, m_message, 0, m_error); 1056af245d11STodd Fiala } 1057af245d11STodd Fiala 1058af245d11STodd Fiala class DetachOperation : public Operation 1059af245d11STodd Fiala { 1060af245d11STodd Fiala public: 106197ccc294SChaoren Lin DetachOperation(lldb::tid_t tid) : m_tid(tid) { } 1062af245d11STodd Fiala 1063d542efdeSTamas Berghammer void Execute(NativeProcessLinux *monitor) override; 1064af245d11STodd Fiala 1065af245d11STodd Fiala private: 1066af245d11STodd Fiala lldb::tid_t m_tid; 1067af245d11STodd Fiala }; 1068af245d11STodd Fiala 1069af245d11STodd Fiala void 1070af245d11STodd Fiala DetachOperation::Execute(NativeProcessLinux *monitor) 1071af245d11STodd Fiala { 107297ccc294SChaoren Lin PTRACE(PTRACE_DETACH, m_tid, nullptr, 0, 0, m_error); 1073af245d11STodd Fiala } 1074af245d11STodd Fiala 10751107b5a5SPavel Labath } // end of anonymous namespace 10761107b5a5SPavel Labath 10771107b5a5SPavel Labath class NativeProcessLinux::Monitor { 10781107b5a5SPavel Labath private: 10791107b5a5SPavel Labath ::pid_t m_child_pid; 10801107b5a5SPavel Labath NativeProcessLinux *m_native_process; 10811107b5a5SPavel Labath 10821107b5a5SPavel Labath enum { READ, WRITE }; 10831107b5a5SPavel Labath int m_pipefd[2] = {-1, -1}; 10841107b5a5SPavel Labath int m_signal_fd = -1; 10851107b5a5SPavel Labath HostThread m_thread; 10861107b5a5SPavel Labath 10871107b5a5SPavel Labath void 10881107b5a5SPavel Labath HandleSignals(); 10891107b5a5SPavel Labath 10901107b5a5SPavel Labath void 10911107b5a5SPavel Labath HandleWait(); 10921107b5a5SPavel Labath 10931107b5a5SPavel Labath // Returns true if the thread should exit. 10941107b5a5SPavel Labath bool 10951107b5a5SPavel Labath HandleCommands(); 10961107b5a5SPavel Labath 10971107b5a5SPavel Labath void 10981107b5a5SPavel Labath MainLoop(); 10991107b5a5SPavel Labath 11001107b5a5SPavel Labath static void * 11011107b5a5SPavel Labath RunMonitor(void *arg); 11021107b5a5SPavel Labath 11031107b5a5SPavel Labath public: 11041107b5a5SPavel Labath // Takes ownership of command_fd. 11051107b5a5SPavel Labath Monitor(::pid_t child_pid, NativeProcessLinux *native_process) 11061107b5a5SPavel Labath : m_child_pid(-getpgid(child_pid)), m_native_process(native_process) 11071107b5a5SPavel Labath {} 11081107b5a5SPavel Labath 11091107b5a5SPavel Labath ~Monitor(); 11101107b5a5SPavel Labath 11111107b5a5SPavel Labath Error 11121107b5a5SPavel Labath Initialize(); 11131107b5a5SPavel Labath }; 11141107b5a5SPavel Labath 11151107b5a5SPavel Labath Error 11161107b5a5SPavel Labath NativeProcessLinux::Monitor::Initialize() 11171107b5a5SPavel Labath { 11181107b5a5SPavel Labath Error error; 11191107b5a5SPavel Labath 11201107b5a5SPavel Labath // We get a SIGCHLD every time something interesting happens with the inferior. We shall be 11211107b5a5SPavel Labath // listening for these signals over a signalfd file descriptors. This allows us to wait for 11221107b5a5SPavel Labath // multiple kinds of events with select. 11231107b5a5SPavel Labath sigset_t signals; 11241107b5a5SPavel Labath sigemptyset(&signals); 11251107b5a5SPavel Labath sigaddset(&signals, SIGCHLD); 11261107b5a5SPavel Labath m_signal_fd = signalfd(-1, &signals, SFD_NONBLOCK | SFD_CLOEXEC); 11271107b5a5SPavel Labath if (m_signal_fd < 0) 11281107b5a5SPavel Labath { 11291107b5a5SPavel Labath return Error("NativeProcessLinux::Monitor::%s failed due to signalfd failure. Monitoring the inferior will be impossible: %s", 11301107b5a5SPavel Labath __FUNCTION__, strerror(errno)); 11311107b5a5SPavel Labath 1132af245d11STodd Fiala } 1133af245d11STodd Fiala 11341107b5a5SPavel Labath if (pipe2(m_pipefd, O_CLOEXEC) == -1) 11351107b5a5SPavel Labath { 11361107b5a5SPavel Labath error.SetErrorToErrno(); 11371107b5a5SPavel Labath return error; 11381107b5a5SPavel Labath } 11391107b5a5SPavel Labath 11401107b5a5SPavel Labath m_thread = ThreadLauncher::LaunchThread("monitor", Monitor::RunMonitor, this, nullptr); 11411107b5a5SPavel Labath if (!m_thread.IsJoinable()) 11421107b5a5SPavel Labath return Error("Failed to create monitor thread for NativeProcessLinux."); 11431107b5a5SPavel Labath 11441107b5a5SPavel Labath return error; 11451107b5a5SPavel Labath } 11461107b5a5SPavel Labath 11471107b5a5SPavel Labath NativeProcessLinux::Monitor::~Monitor() 11481107b5a5SPavel Labath { 11491107b5a5SPavel Labath if (m_pipefd[WRITE] >= 0) 11501107b5a5SPavel Labath close(m_pipefd[WRITE]); 11511107b5a5SPavel Labath if (m_thread.IsJoinable()) 11521107b5a5SPavel Labath m_thread.Join(nullptr); 11531107b5a5SPavel Labath if (m_pipefd[READ] >= 0) 11541107b5a5SPavel Labath close(m_pipefd[READ]); 11551107b5a5SPavel Labath if (m_signal_fd >= 0) 11561107b5a5SPavel Labath close(m_signal_fd); 11571107b5a5SPavel Labath } 11581107b5a5SPavel Labath 11591107b5a5SPavel Labath void 11601107b5a5SPavel Labath NativeProcessLinux::Monitor::HandleSignals() 11611107b5a5SPavel Labath { 11621107b5a5SPavel Labath Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 11631107b5a5SPavel Labath 11641107b5a5SPavel Labath // We don't really care about the content of the SIGCHLD siginfo structure, as we will get 11651107b5a5SPavel Labath // all the information from waitpid(). We just need to read all the signals so that we can 11661107b5a5SPavel Labath // sleep next time we reach select(). 11671107b5a5SPavel Labath while (true) 11681107b5a5SPavel Labath { 11691107b5a5SPavel Labath signalfd_siginfo info; 11701107b5a5SPavel Labath ssize_t size = read(m_signal_fd, &info, sizeof info); 11711107b5a5SPavel Labath if (size == -1) 11721107b5a5SPavel Labath { 11731107b5a5SPavel Labath if (errno == EAGAIN || errno == EWOULDBLOCK) 11741107b5a5SPavel Labath break; // We are done. 11751107b5a5SPavel Labath if (errno == EINTR) 11761107b5a5SPavel Labath continue; 11771107b5a5SPavel Labath if (log) 11781107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s reading from signalfd file descriptor failed: %s", 11791107b5a5SPavel Labath __FUNCTION__, strerror(errno)); 11801107b5a5SPavel Labath break; 11811107b5a5SPavel Labath } 11821107b5a5SPavel Labath if (size != sizeof info) 11831107b5a5SPavel Labath { 11841107b5a5SPavel Labath // We got incomplete information structure. This should not happen, let's just log 11851107b5a5SPavel Labath // that. 11861107b5a5SPavel Labath if (log) 11871107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s reading from signalfd file descriptor returned incomplete data: " 11881107b5a5SPavel Labath "structure size is %zd, read returned %zd bytes", 11891107b5a5SPavel Labath __FUNCTION__, sizeof info, size); 11901107b5a5SPavel Labath break; 11911107b5a5SPavel Labath } 11921107b5a5SPavel Labath if (log) 11931107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s received signal %s(%d).", __FUNCTION__, 11941107b5a5SPavel Labath Host::GetSignalAsCString(info.ssi_signo), info.ssi_signo); 11951107b5a5SPavel Labath } 11961107b5a5SPavel Labath } 11971107b5a5SPavel Labath 11981107b5a5SPavel Labath void 11991107b5a5SPavel Labath NativeProcessLinux::Monitor::HandleWait() 12001107b5a5SPavel Labath { 12011107b5a5SPavel Labath Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 12021107b5a5SPavel Labath // Process all pending waitpid notifications. 12031107b5a5SPavel Labath while (true) 12041107b5a5SPavel Labath { 12051107b5a5SPavel Labath int status = -1; 12061107b5a5SPavel Labath ::pid_t wait_pid = waitpid(m_child_pid, &status, __WALL | WNOHANG); 12071107b5a5SPavel Labath 12081107b5a5SPavel Labath if (wait_pid == 0) 12091107b5a5SPavel Labath break; // We are done. 12101107b5a5SPavel Labath 12111107b5a5SPavel Labath if (wait_pid == -1) 12121107b5a5SPavel Labath { 12131107b5a5SPavel Labath if (errno == EINTR) 12141107b5a5SPavel Labath continue; 12151107b5a5SPavel Labath 12161107b5a5SPavel Labath if (log) 12171107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s waitpid (pid = %" PRIi32 ", &status, __WALL | WNOHANG) failed: %s", 12181107b5a5SPavel Labath __FUNCTION__, m_child_pid, strerror(errno)); 12191107b5a5SPavel Labath break; 12201107b5a5SPavel Labath } 12211107b5a5SPavel Labath 12221107b5a5SPavel Labath bool exited = false; 12231107b5a5SPavel Labath int signal = 0; 12241107b5a5SPavel Labath int exit_status = 0; 12251107b5a5SPavel Labath const char *status_cstr = NULL; 12261107b5a5SPavel Labath if (WIFSTOPPED(status)) 12271107b5a5SPavel Labath { 12281107b5a5SPavel Labath signal = WSTOPSIG(status); 12291107b5a5SPavel Labath status_cstr = "STOPPED"; 12301107b5a5SPavel Labath } 12311107b5a5SPavel Labath else if (WIFEXITED(status)) 12321107b5a5SPavel Labath { 12331107b5a5SPavel Labath exit_status = WEXITSTATUS(status); 12341107b5a5SPavel Labath status_cstr = "EXITED"; 12351107b5a5SPavel Labath exited = true; 12361107b5a5SPavel Labath } 12371107b5a5SPavel Labath else if (WIFSIGNALED(status)) 12381107b5a5SPavel Labath { 12391107b5a5SPavel Labath signal = WTERMSIG(status); 12401107b5a5SPavel Labath status_cstr = "SIGNALED"; 12411107b5a5SPavel Labath if (wait_pid == abs(m_child_pid)) { 12421107b5a5SPavel Labath exited = true; 12431107b5a5SPavel Labath exit_status = -1; 12441107b5a5SPavel Labath } 12451107b5a5SPavel Labath } 12461107b5a5SPavel Labath else 12471107b5a5SPavel Labath status_cstr = "(\?\?\?)"; 12481107b5a5SPavel Labath 12491107b5a5SPavel Labath if (log) 12501107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s: waitpid (pid = %" PRIi32 ", &status, __WALL | WNOHANG)" 12511107b5a5SPavel Labath "=> pid = %" PRIi32 ", status = 0x%8.8x (%s), signal = %i, exit_state = %i", 12521107b5a5SPavel Labath __FUNCTION__, m_child_pid, wait_pid, status, status_cstr, signal, exit_status); 12531107b5a5SPavel Labath 12541107b5a5SPavel Labath m_native_process->MonitorCallback (wait_pid, exited, signal, exit_status); 12551107b5a5SPavel Labath } 12561107b5a5SPavel Labath } 12571107b5a5SPavel Labath 12581107b5a5SPavel Labath bool 12591107b5a5SPavel Labath NativeProcessLinux::Monitor::HandleCommands() 12601107b5a5SPavel Labath { 12611107b5a5SPavel Labath Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 12621107b5a5SPavel Labath 12631107b5a5SPavel Labath while (true) 12641107b5a5SPavel Labath { 12651107b5a5SPavel Labath char command = 0; 12661107b5a5SPavel Labath ssize_t size = read(m_pipefd[READ], &command, sizeof command); 12671107b5a5SPavel Labath if (size == -1) 12681107b5a5SPavel Labath { 12691107b5a5SPavel Labath if (errno == EAGAIN || errno == EWOULDBLOCK) 12701107b5a5SPavel Labath return false; 12711107b5a5SPavel Labath if (errno == EINTR) 12721107b5a5SPavel Labath continue; 12731107b5a5SPavel Labath if (log) 12741107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s exiting because read from command file descriptor failed: %s", __FUNCTION__, strerror(errno)); 12751107b5a5SPavel Labath return true; 12761107b5a5SPavel Labath } 12771107b5a5SPavel Labath if (size == 0) // end of file - write end closed 12781107b5a5SPavel Labath { 12791107b5a5SPavel Labath if (log) 12801107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s exit command received, exiting...", __FUNCTION__); 12811107b5a5SPavel Labath return true; // We are done. 12821107b5a5SPavel Labath } 12831107b5a5SPavel Labath if (log) 12841107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s received unknown command '%c'", 12851107b5a5SPavel Labath __FUNCTION__, command); 12861107b5a5SPavel Labath } 12871107b5a5SPavel Labath } 12881107b5a5SPavel Labath 12891107b5a5SPavel Labath void 12901107b5a5SPavel Labath NativeProcessLinux::Monitor::MainLoop() 12911107b5a5SPavel Labath { 12921107b5a5SPavel Labath while (true) 12931107b5a5SPavel Labath { 12941107b5a5SPavel Labath fd_set fds; 12951107b5a5SPavel Labath FD_ZERO(&fds); 12961107b5a5SPavel Labath FD_SET(m_signal_fd, &fds); 12971107b5a5SPavel Labath FD_SET(m_pipefd[READ], &fds); 12981107b5a5SPavel Labath 12991107b5a5SPavel Labath int max_fd = std::max(m_signal_fd, m_pipefd[READ]) + 1; 13001107b5a5SPavel Labath int r = select(max_fd, &fds, nullptr, nullptr, nullptr); 13011107b5a5SPavel Labath if (r < 0) 13021107b5a5SPavel Labath { 13031107b5a5SPavel Labath Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 13041107b5a5SPavel Labath if (log) 13051107b5a5SPavel Labath log->Printf("NativeProcessLinux::Monitor::%s exiting because select failed: %s", 13061107b5a5SPavel Labath __FUNCTION__, strerror(errno)); 13071107b5a5SPavel Labath return; 13081107b5a5SPavel Labath } 13091107b5a5SPavel Labath 13101107b5a5SPavel Labath if (FD_ISSET(m_pipefd[READ], &fds)) 13111107b5a5SPavel Labath { 13121107b5a5SPavel Labath if (HandleCommands()) 13131107b5a5SPavel Labath return; 13141107b5a5SPavel Labath } 13151107b5a5SPavel Labath 13161107b5a5SPavel Labath if (FD_ISSET(m_signal_fd, &fds)) 13171107b5a5SPavel Labath { 13181107b5a5SPavel Labath HandleSignals(); 13191107b5a5SPavel Labath HandleWait(); 13201107b5a5SPavel Labath } 13211107b5a5SPavel Labath } 13221107b5a5SPavel Labath } 13231107b5a5SPavel Labath 13241107b5a5SPavel Labath void * 13251107b5a5SPavel Labath NativeProcessLinux::Monitor::RunMonitor(void *arg) 13261107b5a5SPavel Labath { 13271107b5a5SPavel Labath static_cast<Monitor *>(arg)->MainLoop(); 13281107b5a5SPavel Labath return nullptr; 13291107b5a5SPavel Labath } 13301107b5a5SPavel Labath 13311107b5a5SPavel Labath 1332af245d11STodd Fiala // Simple helper function to ensure flags are enabled on the given file 1333af245d11STodd Fiala // descriptor. 1334af245d11STodd Fiala static bool 1335af245d11STodd Fiala EnsureFDFlags(int fd, int flags, Error &error) 1336af245d11STodd Fiala { 1337af245d11STodd Fiala int status; 1338af245d11STodd Fiala 1339af245d11STodd Fiala if ((status = fcntl(fd, F_GETFL)) == -1) 1340af245d11STodd Fiala { 1341af245d11STodd Fiala error.SetErrorToErrno(); 1342af245d11STodd Fiala return false; 1343af245d11STodd Fiala } 1344af245d11STodd Fiala 1345af245d11STodd Fiala if (fcntl(fd, F_SETFL, status | flags) == -1) 1346af245d11STodd Fiala { 1347af245d11STodd Fiala error.SetErrorToErrno(); 1348af245d11STodd Fiala return false; 1349af245d11STodd Fiala } 1350af245d11STodd Fiala 1351af245d11STodd Fiala return true; 1352af245d11STodd Fiala } 1353af245d11STodd Fiala 1354af245d11STodd Fiala NativeProcessLinux::OperationArgs::OperationArgs(NativeProcessLinux *monitor) 1355af245d11STodd Fiala : m_monitor(monitor) 1356af245d11STodd Fiala { 1357af245d11STodd Fiala sem_init(&m_semaphore, 0, 0); 1358af245d11STodd Fiala } 1359af245d11STodd Fiala 1360af245d11STodd Fiala NativeProcessLinux::OperationArgs::~OperationArgs() 1361af245d11STodd Fiala { 1362af245d11STodd Fiala sem_destroy(&m_semaphore); 1363af245d11STodd Fiala } 1364af245d11STodd Fiala 1365af245d11STodd Fiala NativeProcessLinux::LaunchArgs::LaunchArgs(NativeProcessLinux *monitor, 1366db264a6dSTamas Berghammer Module *module, 1367af245d11STodd Fiala char const **argv, 1368af245d11STodd Fiala char const **envp, 136975f47c3aSTodd Fiala const std::string &stdin_path, 137075f47c3aSTodd Fiala const std::string &stdout_path, 137175f47c3aSTodd Fiala const std::string &stderr_path, 13720bce1b67STodd Fiala const char *working_dir, 1373db264a6dSTamas Berghammer const ProcessLaunchInfo &launch_info) 1374af245d11STodd Fiala : OperationArgs(monitor), 1375af245d11STodd Fiala m_module(module), 1376af245d11STodd Fiala m_argv(argv), 1377af245d11STodd Fiala m_envp(envp), 1378af245d11STodd Fiala m_stdin_path(stdin_path), 1379af245d11STodd Fiala m_stdout_path(stdout_path), 1380af245d11STodd Fiala m_stderr_path(stderr_path), 13810bce1b67STodd Fiala m_working_dir(working_dir), 13820bce1b67STodd Fiala m_launch_info(launch_info) 13830bce1b67STodd Fiala { 13840bce1b67STodd Fiala } 1385af245d11STodd Fiala 1386af245d11STodd Fiala NativeProcessLinux::LaunchArgs::~LaunchArgs() 1387af245d11STodd Fiala { } 1388af245d11STodd Fiala 1389af245d11STodd Fiala NativeProcessLinux::AttachArgs::AttachArgs(NativeProcessLinux *monitor, 1390af245d11STodd Fiala lldb::pid_t pid) 1391af245d11STodd Fiala : OperationArgs(monitor), m_pid(pid) { } 1392af245d11STodd Fiala 1393af245d11STodd Fiala NativeProcessLinux::AttachArgs::~AttachArgs() 1394af245d11STodd Fiala { } 1395af245d11STodd Fiala 1396af245d11STodd Fiala // ----------------------------------------------------------------------------- 1397af245d11STodd Fiala // Public Static Methods 1398af245d11STodd Fiala // ----------------------------------------------------------------------------- 1399af245d11STodd Fiala 1400db264a6dSTamas Berghammer Error 1401af245d11STodd Fiala NativeProcessLinux::LaunchProcess ( 1402db264a6dSTamas Berghammer Module *exe_module, 1403db264a6dSTamas Berghammer ProcessLaunchInfo &launch_info, 1404db264a6dSTamas Berghammer NativeProcessProtocol::NativeDelegate &native_delegate, 1405af245d11STodd Fiala NativeProcessProtocolSP &native_process_sp) 1406af245d11STodd Fiala { 1407af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1408af245d11STodd Fiala 1409af245d11STodd Fiala Error error; 1410af245d11STodd Fiala 1411af245d11STodd Fiala // Verify the working directory is valid if one was specified. 1412af245d11STodd Fiala const char* working_dir = launch_info.GetWorkingDirectory (); 1413af245d11STodd Fiala if (working_dir) 1414af245d11STodd Fiala { 1415af245d11STodd Fiala FileSpec working_dir_fs (working_dir, true); 1416af245d11STodd Fiala if (!working_dir_fs || working_dir_fs.GetFileType () != FileSpec::eFileTypeDirectory) 1417af245d11STodd Fiala { 1418af245d11STodd Fiala error.SetErrorStringWithFormat ("No such file or directory: %s", working_dir); 1419af245d11STodd Fiala return error; 1420af245d11STodd Fiala } 1421af245d11STodd Fiala } 1422af245d11STodd Fiala 1423db264a6dSTamas Berghammer const FileAction *file_action; 1424af245d11STodd Fiala 1425af245d11STodd Fiala // Default of NULL will mean to use existing open file descriptors. 142675f47c3aSTodd Fiala std::string stdin_path; 142775f47c3aSTodd Fiala std::string stdout_path; 142875f47c3aSTodd Fiala std::string stderr_path; 1429af245d11STodd Fiala 1430af245d11STodd Fiala file_action = launch_info.GetFileActionForFD (STDIN_FILENO); 143175f47c3aSTodd Fiala if (file_action) 143275f47c3aSTodd Fiala stdin_path = file_action->GetPath (); 1433af245d11STodd Fiala 1434af245d11STodd Fiala file_action = launch_info.GetFileActionForFD (STDOUT_FILENO); 143575f47c3aSTodd Fiala if (file_action) 143675f47c3aSTodd Fiala stdout_path = file_action->GetPath (); 1437af245d11STodd Fiala 1438af245d11STodd Fiala file_action = launch_info.GetFileActionForFD (STDERR_FILENO); 143975f47c3aSTodd Fiala if (file_action) 144075f47c3aSTodd Fiala stderr_path = file_action->GetPath (); 144175f47c3aSTodd Fiala 144275f47c3aSTodd Fiala if (log) 144375f47c3aSTodd Fiala { 144475f47c3aSTodd Fiala if (!stdin_path.empty ()) 144575f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s setting STDIN to '%s'", __FUNCTION__, stdin_path.c_str ()); 144675f47c3aSTodd Fiala else 144775f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s leaving STDIN as is", __FUNCTION__); 144875f47c3aSTodd Fiala 144975f47c3aSTodd Fiala if (!stdout_path.empty ()) 145075f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s setting STDOUT to '%s'", __FUNCTION__, stdout_path.c_str ()); 145175f47c3aSTodd Fiala else 145275f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s leaving STDOUT as is", __FUNCTION__); 145375f47c3aSTodd Fiala 145475f47c3aSTodd Fiala if (!stderr_path.empty ()) 145575f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s setting STDERR to '%s'", __FUNCTION__, stderr_path.c_str ()); 145675f47c3aSTodd Fiala else 145775f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s leaving STDERR as is", __FUNCTION__); 145875f47c3aSTodd Fiala } 1459af245d11STodd Fiala 1460af245d11STodd Fiala // Create the NativeProcessLinux in launch mode. 1461af245d11STodd Fiala native_process_sp.reset (new NativeProcessLinux ()); 1462af245d11STodd Fiala 1463af245d11STodd Fiala if (log) 1464af245d11STodd Fiala { 1465af245d11STodd Fiala int i = 0; 1466af245d11STodd Fiala for (const char **args = launch_info.GetArguments ().GetConstArgumentVector (); *args; ++args, ++i) 1467af245d11STodd Fiala { 1468af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s arg %d: \"%s\"", __FUNCTION__, i, *args ? *args : "nullptr"); 1469af245d11STodd Fiala ++i; 1470af245d11STodd Fiala } 1471af245d11STodd Fiala } 1472af245d11STodd Fiala 1473af245d11STodd Fiala if (!native_process_sp->RegisterNativeDelegate (native_delegate)) 1474af245d11STodd Fiala { 1475af245d11STodd Fiala native_process_sp.reset (); 1476af245d11STodd Fiala error.SetErrorStringWithFormat ("failed to register the native delegate"); 1477af245d11STodd Fiala return error; 1478af245d11STodd Fiala } 1479af245d11STodd Fiala 1480cb84eebbSTamas Berghammer std::static_pointer_cast<NativeProcessLinux> (native_process_sp)->LaunchInferior ( 1481af245d11STodd Fiala exe_module, 1482af245d11STodd Fiala launch_info.GetArguments ().GetConstArgumentVector (), 1483af245d11STodd Fiala launch_info.GetEnvironmentEntries ().GetConstArgumentVector (), 1484af245d11STodd Fiala stdin_path, 1485af245d11STodd Fiala stdout_path, 1486af245d11STodd Fiala stderr_path, 1487af245d11STodd Fiala working_dir, 14880bce1b67STodd Fiala launch_info, 1489af245d11STodd Fiala error); 1490af245d11STodd Fiala 1491af245d11STodd Fiala if (error.Fail ()) 1492af245d11STodd Fiala { 1493af245d11STodd Fiala native_process_sp.reset (); 1494af245d11STodd Fiala if (log) 1495af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to launch process: %s", __FUNCTION__, error.AsCString ()); 1496af245d11STodd Fiala return error; 1497af245d11STodd Fiala } 1498af245d11STodd Fiala 1499af245d11STodd Fiala launch_info.SetProcessID (native_process_sp->GetID ()); 1500af245d11STodd Fiala 1501af245d11STodd Fiala return error; 1502af245d11STodd Fiala } 1503af245d11STodd Fiala 1504db264a6dSTamas Berghammer Error 1505af245d11STodd Fiala NativeProcessLinux::AttachToProcess ( 1506af245d11STodd Fiala lldb::pid_t pid, 1507db264a6dSTamas Berghammer NativeProcessProtocol::NativeDelegate &native_delegate, 1508af245d11STodd Fiala NativeProcessProtocolSP &native_process_sp) 1509af245d11STodd Fiala { 1510af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1511af245d11STodd Fiala if (log && log->GetMask ().Test (POSIX_LOG_VERBOSE)) 1512af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(pid = %" PRIi64 ")", __FUNCTION__, pid); 1513af245d11STodd Fiala 1514af245d11STodd Fiala // Grab the current platform architecture. This should be Linux, 1515af245d11STodd Fiala // since this code is only intended to run on a Linux host. 1516615eb7e6SGreg Clayton PlatformSP platform_sp (Platform::GetHostPlatform ()); 1517af245d11STodd Fiala if (!platform_sp) 1518af245d11STodd Fiala return Error("failed to get a valid default platform"); 1519af245d11STodd Fiala 1520af245d11STodd Fiala // Retrieve the architecture for the running process. 1521af245d11STodd Fiala ArchSpec process_arch; 1522af245d11STodd Fiala Error error = ResolveProcessArchitecture (pid, *platform_sp.get (), process_arch); 1523af245d11STodd Fiala if (!error.Success ()) 1524af245d11STodd Fiala return error; 1525af245d11STodd Fiala 15261339b5e8SOleksiy Vyalov std::shared_ptr<NativeProcessLinux> native_process_linux_sp (new NativeProcessLinux ()); 1527af245d11STodd Fiala 15281339b5e8SOleksiy Vyalov if (!native_process_linux_sp->RegisterNativeDelegate (native_delegate)) 1529af245d11STodd Fiala { 1530af245d11STodd Fiala error.SetErrorStringWithFormat ("failed to register the native delegate"); 1531af245d11STodd Fiala return error; 1532af245d11STodd Fiala } 1533af245d11STodd Fiala 15341339b5e8SOleksiy Vyalov native_process_linux_sp->AttachToInferior (pid, error); 1535af245d11STodd Fiala if (!error.Success ()) 1536af245d11STodd Fiala return error; 1537af245d11STodd Fiala 15381339b5e8SOleksiy Vyalov native_process_sp = native_process_linux_sp; 1539af245d11STodd Fiala return error; 1540af245d11STodd Fiala } 1541af245d11STodd Fiala 1542af245d11STodd Fiala // ----------------------------------------------------------------------------- 1543af245d11STodd Fiala // Public Instance Methods 1544af245d11STodd Fiala // ----------------------------------------------------------------------------- 1545af245d11STodd Fiala 1546af245d11STodd Fiala NativeProcessLinux::NativeProcessLinux () : 1547af245d11STodd Fiala NativeProcessProtocol (LLDB_INVALID_PROCESS_ID), 1548af245d11STodd Fiala m_arch (), 1549fa03ad2eSChaoren Lin m_operation_thread (), 1550af245d11STodd Fiala m_operation (nullptr), 1551af245d11STodd Fiala m_operation_mutex (), 1552af245d11STodd Fiala m_operation_pending (), 1553af245d11STodd Fiala m_operation_done (), 1554af245d11STodd Fiala m_supports_mem_region (eLazyBoolCalculate), 1555af245d11STodd Fiala m_mem_region_cache (), 1556fa03ad2eSChaoren Lin m_mem_region_cache_mutex (), 1557fa03ad2eSChaoren Lin m_coordinator_up (new ThreadStateCoordinator (GetThreadLoggerFunction ())), 1558fa03ad2eSChaoren Lin m_coordinator_thread () 1559af245d11STodd Fiala { 1560af245d11STodd Fiala } 1561af245d11STodd Fiala 1562af245d11STodd Fiala //------------------------------------------------------------------------------ 1563af245d11STodd Fiala /// The basic design of the NativeProcessLinux is built around two threads. 1564af245d11STodd Fiala /// 1565af245d11STodd Fiala /// One thread (@see SignalThread) simply blocks on a call to waitpid() looking 1566af245d11STodd Fiala /// for changes in the debugee state. When a change is detected a 1567af245d11STodd Fiala /// ProcessMessage is sent to the associated ProcessLinux instance. This thread 1568af245d11STodd Fiala /// "drives" state changes in the debugger. 1569af245d11STodd Fiala /// 1570af245d11STodd Fiala /// The second thread (@see OperationThread) is responsible for two things 1) 1571af245d11STodd Fiala /// launching or attaching to the inferior process, and then 2) servicing 1572af245d11STodd Fiala /// operations such as register reads/writes, stepping, etc. See the comments 1573af245d11STodd Fiala /// on the Operation class for more info as to why this is needed. 1574af245d11STodd Fiala void 1575af245d11STodd Fiala NativeProcessLinux::LaunchInferior ( 1576af245d11STodd Fiala Module *module, 1577af245d11STodd Fiala const char *argv[], 1578af245d11STodd Fiala const char *envp[], 157975f47c3aSTodd Fiala const std::string &stdin_path, 158075f47c3aSTodd Fiala const std::string &stdout_path, 158175f47c3aSTodd Fiala const std::string &stderr_path, 1582af245d11STodd Fiala const char *working_dir, 1583db264a6dSTamas Berghammer const ProcessLaunchInfo &launch_info, 1584db264a6dSTamas Berghammer Error &error) 1585af245d11STodd Fiala { 1586af245d11STodd Fiala if (module) 1587af245d11STodd Fiala m_arch = module->GetArchitecture (); 1588af245d11STodd Fiala 1589af245d11STodd Fiala SetState (eStateLaunching); 1590af245d11STodd Fiala 1591af245d11STodd Fiala std::unique_ptr<LaunchArgs> args( 1592af245d11STodd Fiala new LaunchArgs( 1593af245d11STodd Fiala this, module, argv, envp, 1594af245d11STodd Fiala stdin_path, stdout_path, stderr_path, 15950bce1b67STodd Fiala working_dir, launch_info)); 1596af245d11STodd Fiala 1597af245d11STodd Fiala sem_init (&m_operation_pending, 0, 0); 1598af245d11STodd Fiala sem_init (&m_operation_done, 0, 0); 1599af245d11STodd Fiala 1600af245d11STodd Fiala StartLaunchOpThread (args.get(), error); 1601af245d11STodd Fiala if (!error.Success ()) 1602af245d11STodd Fiala return; 1603af245d11STodd Fiala 1604fa03ad2eSChaoren Lin error = StartCoordinatorThread (); 1605fa03ad2eSChaoren Lin if (!error.Success ()) 1606fa03ad2eSChaoren Lin return; 1607fa03ad2eSChaoren Lin 1608af245d11STodd Fiala WAIT_AGAIN: 1609af245d11STodd Fiala // Wait for the operation thread to initialize. 1610af245d11STodd Fiala if (sem_wait(&args->m_semaphore)) 1611af245d11STodd Fiala { 1612af245d11STodd Fiala if (errno == EINTR) 1613af245d11STodd Fiala goto WAIT_AGAIN; 1614af245d11STodd Fiala else 1615af245d11STodd Fiala { 1616af245d11STodd Fiala error.SetErrorToErrno(); 1617af245d11STodd Fiala return; 1618af245d11STodd Fiala } 1619af245d11STodd Fiala } 1620af245d11STodd Fiala 1621af245d11STodd Fiala // Check that the launch was a success. 1622af245d11STodd Fiala if (!args->m_error.Success()) 1623af245d11STodd Fiala { 1624af245d11STodd Fiala StopOpThread(); 1625fa03ad2eSChaoren Lin StopCoordinatorThread (); 1626af245d11STodd Fiala error = args->m_error; 1627af245d11STodd Fiala return; 1628af245d11STodd Fiala } 1629af245d11STodd Fiala 1630af245d11STodd Fiala // Finally, start monitoring the child process for change in state. 16311107b5a5SPavel Labath StartMonitorThread(error); 1632af245d11STodd Fiala } 1633af245d11STodd Fiala 1634af245d11STodd Fiala void 1635db264a6dSTamas Berghammer NativeProcessLinux::AttachToInferior (lldb::pid_t pid, Error &error) 1636af245d11STodd Fiala { 1637af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1638af245d11STodd Fiala if (log) 1639af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ")", __FUNCTION__, pid); 1640af245d11STodd Fiala 1641af245d11STodd Fiala // We can use the Host for everything except the ResolveExecutable portion. 1642615eb7e6SGreg Clayton PlatformSP platform_sp = Platform::GetHostPlatform (); 1643af245d11STodd Fiala if (!platform_sp) 1644af245d11STodd Fiala { 1645af245d11STodd Fiala if (log) 1646af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 "): no default platform set", __FUNCTION__, pid); 1647af245d11STodd Fiala error.SetErrorString ("no default platform available"); 164850d60be3SShawn Best return; 1649af245d11STodd Fiala } 1650af245d11STodd Fiala 1651af245d11STodd Fiala // Gather info about the process. 1652af245d11STodd Fiala ProcessInstanceInfo process_info; 165350d60be3SShawn Best if (!platform_sp->GetProcessInfo (pid, process_info)) 165450d60be3SShawn Best { 165550d60be3SShawn Best if (log) 165650d60be3SShawn Best log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 "): failed to get process info", __FUNCTION__, pid); 165750d60be3SShawn Best error.SetErrorString ("failed to get process info"); 165850d60be3SShawn Best return; 165950d60be3SShawn Best } 1660af245d11STodd Fiala 1661af245d11STodd Fiala // Resolve the executable module 1662af245d11STodd Fiala ModuleSP exe_module_sp; 1663af245d11STodd Fiala FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths()); 1664e56f6dceSChaoren Lin ModuleSpec exe_module_spec(process_info.GetExecutableFile(), process_info.GetArchitecture()); 16656edef204SOleksiy Vyalov error = platform_sp->ResolveExecutable(exe_module_spec, exe_module_sp, 1666af245d11STodd Fiala executable_search_paths.GetSize() ? &executable_search_paths : NULL); 1667af245d11STodd Fiala if (!error.Success()) 1668af245d11STodd Fiala return; 1669af245d11STodd Fiala 1670af245d11STodd Fiala // Set the architecture to the exe architecture. 1671af245d11STodd Fiala m_arch = exe_module_sp->GetArchitecture(); 1672af245d11STodd Fiala if (log) 1673af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ") detected architecture %s", __FUNCTION__, pid, m_arch.GetArchitectureName ()); 1674af245d11STodd Fiala 1675af245d11STodd Fiala m_pid = pid; 1676af245d11STodd Fiala SetState(eStateAttaching); 1677af245d11STodd Fiala 1678af245d11STodd Fiala sem_init (&m_operation_pending, 0, 0); 1679af245d11STodd Fiala sem_init (&m_operation_done, 0, 0); 1680af245d11STodd Fiala 1681af245d11STodd Fiala std::unique_ptr<AttachArgs> args (new AttachArgs (this, pid)); 1682af245d11STodd Fiala 1683af245d11STodd Fiala StartAttachOpThread(args.get (), error); 1684af245d11STodd Fiala if (!error.Success ()) 1685af245d11STodd Fiala return; 1686af245d11STodd Fiala 1687fa03ad2eSChaoren Lin error = StartCoordinatorThread (); 1688fa03ad2eSChaoren Lin if (!error.Success ()) 1689fa03ad2eSChaoren Lin return; 1690fa03ad2eSChaoren Lin 1691af245d11STodd Fiala WAIT_AGAIN: 1692af245d11STodd Fiala // Wait for the operation thread to initialize. 1693af245d11STodd Fiala if (sem_wait (&args->m_semaphore)) 1694af245d11STodd Fiala { 1695af245d11STodd Fiala if (errno == EINTR) 1696af245d11STodd Fiala goto WAIT_AGAIN; 1697af245d11STodd Fiala else 1698af245d11STodd Fiala { 1699af245d11STodd Fiala error.SetErrorToErrno (); 1700af245d11STodd Fiala return; 1701af245d11STodd Fiala } 1702af245d11STodd Fiala } 1703af245d11STodd Fiala 1704af245d11STodd Fiala // Check that the attach was a success. 1705af245d11STodd Fiala if (!args->m_error.Success ()) 1706af245d11STodd Fiala { 1707af245d11STodd Fiala StopOpThread (); 1708fa03ad2eSChaoren Lin StopCoordinatorThread (); 1709af245d11STodd Fiala error = args->m_error; 1710af245d11STodd Fiala return; 1711af245d11STodd Fiala } 1712af245d11STodd Fiala 1713af245d11STodd Fiala // Finally, start monitoring the child process for change in state. 17141107b5a5SPavel Labath StartMonitorThread(error); 1715af245d11STodd Fiala } 1716af245d11STodd Fiala 17178bc34f4dSOleksiy Vyalov void 17188bc34f4dSOleksiy Vyalov NativeProcessLinux::Terminate () 1719af245d11STodd Fiala { 1720af245d11STodd Fiala StopMonitor(); 1721af245d11STodd Fiala } 1722af245d11STodd Fiala 1723af245d11STodd Fiala //------------------------------------------------------------------------------ 1724af245d11STodd Fiala // Thread setup and tear down. 1725af245d11STodd Fiala 1726af245d11STodd Fiala void 1727af245d11STodd Fiala NativeProcessLinux::StartLaunchOpThread(LaunchArgs *args, Error &error) 1728af245d11STodd Fiala { 1729af245d11STodd Fiala static const char *g_thread_name = "lldb.process.nativelinux.operation"; 1730af245d11STodd Fiala 1731acee96aeSZachary Turner if (m_operation_thread.IsJoinable()) 1732af245d11STodd Fiala return; 1733af245d11STodd Fiala 173439de3110SZachary Turner m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error); 1735af245d11STodd Fiala } 1736af245d11STodd Fiala 1737af245d11STodd Fiala void * 1738af245d11STodd Fiala NativeProcessLinux::LaunchOpThread(void *arg) 1739af245d11STodd Fiala { 1740af245d11STodd Fiala LaunchArgs *args = static_cast<LaunchArgs*>(arg); 1741af245d11STodd Fiala 1742af245d11STodd Fiala if (!Launch(args)) { 1743af245d11STodd Fiala sem_post(&args->m_semaphore); 1744af245d11STodd Fiala return NULL; 1745af245d11STodd Fiala } 1746af245d11STodd Fiala 1747af245d11STodd Fiala ServeOperation(args); 1748af245d11STodd Fiala return NULL; 1749af245d11STodd Fiala } 1750af245d11STodd Fiala 1751af245d11STodd Fiala bool 1752af245d11STodd Fiala NativeProcessLinux::Launch(LaunchArgs *args) 1753af245d11STodd Fiala { 17540bce1b67STodd Fiala assert (args && "null args"); 17550bce1b67STodd Fiala if (!args) 17560bce1b67STodd Fiala return false; 17570bce1b67STodd Fiala 1758af245d11STodd Fiala NativeProcessLinux *monitor = args->m_monitor; 1759af245d11STodd Fiala assert (monitor && "monitor is NULL"); 1760af245d11STodd Fiala 1761af245d11STodd Fiala const char **argv = args->m_argv; 1762af245d11STodd Fiala const char **envp = args->m_envp; 1763af245d11STodd Fiala const char *working_dir = args->m_working_dir; 1764af245d11STodd Fiala 1765af245d11STodd Fiala lldb_utility::PseudoTerminal terminal; 1766af245d11STodd Fiala const size_t err_len = 1024; 1767af245d11STodd Fiala char err_str[err_len]; 1768af245d11STodd Fiala lldb::pid_t pid; 1769af245d11STodd Fiala NativeThreadProtocolSP thread_sp; 1770af245d11STodd Fiala 1771af245d11STodd Fiala lldb::ThreadSP inferior; 1772af245d11STodd Fiala 1773af245d11STodd Fiala // Propagate the environment if one is not supplied. 1774af245d11STodd Fiala if (envp == NULL || envp[0] == NULL) 1775af245d11STodd Fiala envp = const_cast<const char **>(environ); 1776af245d11STodd Fiala 1777af245d11STodd Fiala if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t> (-1)) 1778af245d11STodd Fiala { 1779af245d11STodd Fiala args->m_error.SetErrorToGenericError(); 1780af245d11STodd Fiala args->m_error.SetErrorString("Process fork failed."); 178175f47c3aSTodd Fiala return false; 1782af245d11STodd Fiala } 1783af245d11STodd Fiala 1784af245d11STodd Fiala // Recognized child exit status codes. 1785af245d11STodd Fiala enum { 1786af245d11STodd Fiala ePtraceFailed = 1, 1787af245d11STodd Fiala eDupStdinFailed, 1788af245d11STodd Fiala eDupStdoutFailed, 1789af245d11STodd Fiala eDupStderrFailed, 1790af245d11STodd Fiala eChdirFailed, 1791af245d11STodd Fiala eExecFailed, 1792af245d11STodd Fiala eSetGidFailed 1793af245d11STodd Fiala }; 1794af245d11STodd Fiala 1795af245d11STodd Fiala // Child process. 1796af245d11STodd Fiala if (pid == 0) 1797af245d11STodd Fiala { 179875f47c3aSTodd Fiala // FIXME consider opening a pipe between parent/child and have this forked child 179975f47c3aSTodd Fiala // send log info to parent re: launch status, in place of the log lines removed here. 1800af245d11STodd Fiala 180175f47c3aSTodd Fiala // Start tracing this child that is about to exec. 180297ccc294SChaoren Lin PTRACE(PTRACE_TRACEME, 0, nullptr, nullptr, 0, args->m_error); 180397ccc294SChaoren Lin if (args->m_error.Fail()) 1804af245d11STodd Fiala exit(ePtraceFailed); 1805af245d11STodd Fiala 1806493c3a12SPavel Labath // terminal has already dupped the tty descriptors to stdin/out/err. 1807493c3a12SPavel Labath // This closes original fd from which they were copied (and avoids 1808493c3a12SPavel Labath // leaking descriptors to the debugged process. 1809493c3a12SPavel Labath terminal.CloseSlaveFileDescriptor(); 1810493c3a12SPavel Labath 1811af245d11STodd Fiala // Do not inherit setgid powers. 1812af245d11STodd Fiala if (setgid(getgid()) != 0) 1813af245d11STodd Fiala exit(eSetGidFailed); 1814af245d11STodd Fiala 1815af245d11STodd Fiala // Attempt to have our own process group. 1816af245d11STodd Fiala if (setpgid(0, 0) != 0) 1817af245d11STodd Fiala { 181875f47c3aSTodd Fiala // FIXME log that this failed. This is common. 1819af245d11STodd Fiala // Don't allow this to prevent an inferior exec. 1820af245d11STodd Fiala } 1821af245d11STodd Fiala 1822af245d11STodd Fiala // Dup file descriptors if needed. 182375f47c3aSTodd Fiala if (!args->m_stdin_path.empty ()) 182475f47c3aSTodd Fiala if (!DupDescriptor(args->m_stdin_path.c_str (), STDIN_FILENO, O_RDONLY)) 1825af245d11STodd Fiala exit(eDupStdinFailed); 1826af245d11STodd Fiala 182775f47c3aSTodd Fiala if (!args->m_stdout_path.empty ()) 182814f4476aSTamas Berghammer if (!DupDescriptor(args->m_stdout_path.c_str (), STDOUT_FILENO, O_WRONLY | O_CREAT | O_TRUNC)) 1829af245d11STodd Fiala exit(eDupStdoutFailed); 1830af245d11STodd Fiala 183175f47c3aSTodd Fiala if (!args->m_stderr_path.empty ()) 183214f4476aSTamas Berghammer if (!DupDescriptor(args->m_stderr_path.c_str (), STDERR_FILENO, O_WRONLY | O_CREAT | O_TRUNC)) 1833af245d11STodd Fiala exit(eDupStderrFailed); 1834af245d11STodd Fiala 1835af245d11STodd Fiala // Change working directory 1836af245d11STodd Fiala if (working_dir != NULL && working_dir[0]) 1837af245d11STodd Fiala if (0 != ::chdir(working_dir)) 1838af245d11STodd Fiala exit(eChdirFailed); 1839af245d11STodd Fiala 18400bce1b67STodd Fiala // Disable ASLR if requested. 18410bce1b67STodd Fiala if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR)) 18420bce1b67STodd Fiala { 18430bce1b67STodd Fiala const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS); 18440bce1b67STodd Fiala if (old_personality == -1) 18450bce1b67STodd Fiala { 184675f47c3aSTodd Fiala // Can't retrieve Linux personality. Cannot disable ASLR. 18470bce1b67STodd Fiala } 18480bce1b67STodd Fiala else 18490bce1b67STodd Fiala { 18500bce1b67STodd Fiala const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality); 18510bce1b67STodd Fiala if (new_personality == -1) 18520bce1b67STodd Fiala { 185375f47c3aSTodd Fiala // Disabling ASLR failed. 18540bce1b67STodd Fiala } 18550bce1b67STodd Fiala else 18560bce1b67STodd Fiala { 185775f47c3aSTodd Fiala // Disabling ASLR succeeded. 18580bce1b67STodd Fiala } 18590bce1b67STodd Fiala } 18600bce1b67STodd Fiala } 18610bce1b67STodd Fiala 186275f47c3aSTodd Fiala // Execute. We should never return... 1863af245d11STodd Fiala execve(argv[0], 1864af245d11STodd Fiala const_cast<char *const *>(argv), 1865af245d11STodd Fiala const_cast<char *const *>(envp)); 186675f47c3aSTodd Fiala 186775f47c3aSTodd Fiala // ...unless exec fails. In which case we definitely need to end the child here. 1868af245d11STodd Fiala exit(eExecFailed); 1869af245d11STodd Fiala } 1870af245d11STodd Fiala 187175f47c3aSTodd Fiala // 187275f47c3aSTodd Fiala // This is the parent code here. 187375f47c3aSTodd Fiala // 187475f47c3aSTodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 187575f47c3aSTodd Fiala 1876af245d11STodd Fiala // Wait for the child process to trap on its call to execve. 1877af245d11STodd Fiala ::pid_t wpid; 1878af245d11STodd Fiala int status; 1879af245d11STodd Fiala if ((wpid = waitpid(pid, &status, 0)) < 0) 1880af245d11STodd Fiala { 1881af245d11STodd Fiala args->m_error.SetErrorToErrno(); 1882af245d11STodd Fiala 1883af245d11STodd Fiala if (log) 1884af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s waitpid for inferior failed with %s", __FUNCTION__, args->m_error.AsCString ()); 1885af245d11STodd Fiala 1886af245d11STodd Fiala // Mark the inferior as invalid. 1887af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 1888af245d11STodd Fiala monitor->SetState (StateType::eStateInvalid); 1889af245d11STodd Fiala 189075f47c3aSTodd Fiala return false; 1891af245d11STodd Fiala } 1892af245d11STodd Fiala else if (WIFEXITED(status)) 1893af245d11STodd Fiala { 1894af245d11STodd Fiala // open, dup or execve likely failed for some reason. 1895af245d11STodd Fiala args->m_error.SetErrorToGenericError(); 1896af245d11STodd Fiala switch (WEXITSTATUS(status)) 1897af245d11STodd Fiala { 1898af245d11STodd Fiala case ePtraceFailed: 1899af245d11STodd Fiala args->m_error.SetErrorString("Child ptrace failed."); 1900af245d11STodd Fiala break; 1901af245d11STodd Fiala case eDupStdinFailed: 1902af245d11STodd Fiala args->m_error.SetErrorString("Child open stdin failed."); 1903af245d11STodd Fiala break; 1904af245d11STodd Fiala case eDupStdoutFailed: 1905af245d11STodd Fiala args->m_error.SetErrorString("Child open stdout failed."); 1906af245d11STodd Fiala break; 1907af245d11STodd Fiala case eDupStderrFailed: 1908af245d11STodd Fiala args->m_error.SetErrorString("Child open stderr failed."); 1909af245d11STodd Fiala break; 1910af245d11STodd Fiala case eChdirFailed: 1911af245d11STodd Fiala args->m_error.SetErrorString("Child failed to set working directory."); 1912af245d11STodd Fiala break; 1913af245d11STodd Fiala case eExecFailed: 1914af245d11STodd Fiala args->m_error.SetErrorString("Child exec failed."); 1915af245d11STodd Fiala break; 1916af245d11STodd Fiala case eSetGidFailed: 1917af245d11STodd Fiala args->m_error.SetErrorString("Child setgid failed."); 1918af245d11STodd Fiala break; 1919af245d11STodd Fiala default: 1920af245d11STodd Fiala args->m_error.SetErrorString("Child returned unknown exit status."); 1921af245d11STodd Fiala break; 1922af245d11STodd Fiala } 1923af245d11STodd Fiala 1924af245d11STodd Fiala if (log) 1925af245d11STodd Fiala { 1926af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior exited with status %d before issuing a STOP", 1927af245d11STodd Fiala __FUNCTION__, 1928af245d11STodd Fiala WEXITSTATUS(status)); 1929af245d11STodd Fiala } 1930af245d11STodd Fiala 1931af245d11STodd Fiala // Mark the inferior as invalid. 1932af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 1933af245d11STodd Fiala monitor->SetState (StateType::eStateInvalid); 1934af245d11STodd Fiala 193575f47c3aSTodd Fiala return false; 1936af245d11STodd Fiala } 1937af245d11STodd Fiala assert(WIFSTOPPED(status) && (wpid == static_cast< ::pid_t> (pid)) && 1938af245d11STodd Fiala "Could not sync with inferior process."); 1939af245d11STodd Fiala 1940af245d11STodd Fiala if (log) 1941af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior started, now in stopped state", __FUNCTION__); 1942af245d11STodd Fiala 194397ccc294SChaoren Lin args->m_error = SetDefaultPtraceOpts(pid); 194497ccc294SChaoren Lin if (args->m_error.Fail()) 1945af245d11STodd Fiala { 1946af245d11STodd Fiala if (log) 1947af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior failed to set default ptrace options: %s", 1948af245d11STodd Fiala __FUNCTION__, 1949af245d11STodd Fiala args->m_error.AsCString ()); 1950af245d11STodd Fiala 1951af245d11STodd Fiala // Mark the inferior as invalid. 1952af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 1953af245d11STodd Fiala monitor->SetState (StateType::eStateInvalid); 1954af245d11STodd Fiala 195575f47c3aSTodd Fiala return false; 1956af245d11STodd Fiala } 1957af245d11STodd Fiala 1958af245d11STodd Fiala // Release the master terminal descriptor and pass it off to the 1959af245d11STodd Fiala // NativeProcessLinux instance. Similarly stash the inferior pid. 1960af245d11STodd Fiala monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor(); 1961af245d11STodd Fiala monitor->m_pid = pid; 1962af245d11STodd Fiala 1963af245d11STodd Fiala // Set the terminal fd to be in non blocking mode (it simplifies the 1964af245d11STodd Fiala // implementation of ProcessLinux::GetSTDOUT to have a non-blocking 1965af245d11STodd Fiala // descriptor to read from). 1966af245d11STodd Fiala if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error)) 1967af245d11STodd Fiala { 1968af245d11STodd Fiala if (log) 1969af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior EnsureFDFlags failed for ensuring terminal O_NONBLOCK setting: %s", 1970af245d11STodd Fiala __FUNCTION__, 1971af245d11STodd Fiala args->m_error.AsCString ()); 1972af245d11STodd Fiala 1973af245d11STodd Fiala // Mark the inferior as invalid. 1974af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 1975af245d11STodd Fiala monitor->SetState (StateType::eStateInvalid); 1976af245d11STodd Fiala 197775f47c3aSTodd Fiala return false; 1978af245d11STodd Fiala } 1979af245d11STodd Fiala 1980af245d11STodd Fiala if (log) 1981af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() adding pid = %" PRIu64, __FUNCTION__, pid); 1982af245d11STodd Fiala 1983fa03ad2eSChaoren Lin thread_sp = monitor->AddThread (pid); 1984af245d11STodd Fiala assert (thread_sp && "AddThread() returned a nullptr thread"); 1985fa03ad2eSChaoren Lin monitor->NotifyThreadCreateStopped (pid); 1986cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGSTOP); 1987af245d11STodd Fiala 1988af245d11STodd Fiala // Let our process instance know the thread has stopped. 1989fa03ad2eSChaoren Lin monitor->SetCurrentThreadID (thread_sp->GetID ()); 1990af245d11STodd Fiala monitor->SetState (StateType::eStateStopped); 1991af245d11STodd Fiala 1992af245d11STodd Fiala if (log) 1993af245d11STodd Fiala { 1994af245d11STodd Fiala if (args->m_error.Success ()) 1995af245d11STodd Fiala { 1996af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior launching succeeded", __FUNCTION__); 1997af245d11STodd Fiala } 1998af245d11STodd Fiala else 1999af245d11STodd Fiala { 2000af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior launching failed: %s", 2001af245d11STodd Fiala __FUNCTION__, 2002af245d11STodd Fiala args->m_error.AsCString ()); 2003af245d11STodd Fiala } 2004af245d11STodd Fiala } 2005af245d11STodd Fiala return args->m_error.Success(); 2006af245d11STodd Fiala } 2007af245d11STodd Fiala 2008af245d11STodd Fiala void 2009db264a6dSTamas Berghammer NativeProcessLinux::StartAttachOpThread(AttachArgs *args, Error &error) 2010af245d11STodd Fiala { 2011af245d11STodd Fiala static const char *g_thread_name = "lldb.process.linux.operation"; 2012af245d11STodd Fiala 2013acee96aeSZachary Turner if (m_operation_thread.IsJoinable()) 2014af245d11STodd Fiala return; 2015af245d11STodd Fiala 201639de3110SZachary Turner m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error); 2017af245d11STodd Fiala } 2018af245d11STodd Fiala 2019af245d11STodd Fiala void * 2020af245d11STodd Fiala NativeProcessLinux::AttachOpThread(void *arg) 2021af245d11STodd Fiala { 2022af245d11STodd Fiala AttachArgs *args = static_cast<AttachArgs*>(arg); 2023af245d11STodd Fiala 2024af245d11STodd Fiala if (!Attach(args)) { 2025af245d11STodd Fiala sem_post(&args->m_semaphore); 2026fa03ad2eSChaoren Lin return nullptr; 2027af245d11STodd Fiala } 2028af245d11STodd Fiala 2029af245d11STodd Fiala ServeOperation(args); 2030fa03ad2eSChaoren Lin return nullptr; 2031af245d11STodd Fiala } 2032af245d11STodd Fiala 2033af245d11STodd Fiala bool 2034af245d11STodd Fiala NativeProcessLinux::Attach(AttachArgs *args) 2035af245d11STodd Fiala { 2036af245d11STodd Fiala lldb::pid_t pid = args->m_pid; 2037af245d11STodd Fiala 2038af245d11STodd Fiala NativeProcessLinux *monitor = args->m_monitor; 2039af245d11STodd Fiala lldb::ThreadSP inferior; 2040af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2041af245d11STodd Fiala 2042af245d11STodd Fiala // Use a map to keep track of the threads which we have attached/need to attach. 2043af245d11STodd Fiala Host::TidMap tids_to_attach; 2044af245d11STodd Fiala if (pid <= 1) 2045af245d11STodd Fiala { 2046af245d11STodd Fiala args->m_error.SetErrorToGenericError(); 2047af245d11STodd Fiala args->m_error.SetErrorString("Attaching to process 1 is not allowed."); 2048af245d11STodd Fiala goto FINISH; 2049af245d11STodd Fiala } 2050af245d11STodd Fiala 2051af245d11STodd Fiala while (Host::FindProcessThreads(pid, tids_to_attach)) 2052af245d11STodd Fiala { 2053af245d11STodd Fiala for (Host::TidMap::iterator it = tids_to_attach.begin(); 2054af245d11STodd Fiala it != tids_to_attach.end();) 2055af245d11STodd Fiala { 2056af245d11STodd Fiala if (it->second == false) 2057af245d11STodd Fiala { 2058af245d11STodd Fiala lldb::tid_t tid = it->first; 2059af245d11STodd Fiala 2060af245d11STodd Fiala // Attach to the requested process. 2061af245d11STodd Fiala // An attach will cause the thread to stop with a SIGSTOP. 206297ccc294SChaoren Lin PTRACE(PTRACE_ATTACH, tid, nullptr, nullptr, 0, args->m_error); 206397ccc294SChaoren Lin if (args->m_error.Fail()) 2064af245d11STodd Fiala { 2065af245d11STodd Fiala // No such thread. The thread may have exited. 2066af245d11STodd Fiala // More error handling may be needed. 206797ccc294SChaoren Lin if (args->m_error.GetError() == ESRCH) 2068af245d11STodd Fiala { 2069af245d11STodd Fiala it = tids_to_attach.erase(it); 2070af245d11STodd Fiala continue; 2071af245d11STodd Fiala } 2072af245d11STodd Fiala else 2073af245d11STodd Fiala goto FINISH; 2074af245d11STodd Fiala } 2075af245d11STodd Fiala 2076af245d11STodd Fiala int status; 2077af245d11STodd Fiala // Need to use __WALL otherwise we receive an error with errno=ECHLD 2078af245d11STodd Fiala // At this point we should have a thread stopped if waitpid succeeds. 2079af245d11STodd Fiala if ((status = waitpid(tid, NULL, __WALL)) < 0) 2080af245d11STodd Fiala { 2081af245d11STodd Fiala // No such thread. The thread may have exited. 2082af245d11STodd Fiala // More error handling may be needed. 2083af245d11STodd Fiala if (errno == ESRCH) 2084af245d11STodd Fiala { 2085af245d11STodd Fiala it = tids_to_attach.erase(it); 2086af245d11STodd Fiala continue; 2087af245d11STodd Fiala } 2088af245d11STodd Fiala else 2089af245d11STodd Fiala { 2090af245d11STodd Fiala args->m_error.SetErrorToErrno(); 2091af245d11STodd Fiala goto FINISH; 2092af245d11STodd Fiala } 2093af245d11STodd Fiala } 2094af245d11STodd Fiala 209597ccc294SChaoren Lin args->m_error = SetDefaultPtraceOpts(tid); 209697ccc294SChaoren Lin if (args->m_error.Fail()) 2097af245d11STodd Fiala goto FINISH; 2098af245d11STodd Fiala 2099af245d11STodd Fiala 2100af245d11STodd Fiala if (log) 2101af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() adding tid = %" PRIu64, __FUNCTION__, tid); 2102af245d11STodd Fiala 2103af245d11STodd Fiala it->second = true; 2104af245d11STodd Fiala 2105af245d11STodd Fiala // Create the thread, mark it as stopped. 2106af245d11STodd Fiala NativeThreadProtocolSP thread_sp (monitor->AddThread (static_cast<lldb::tid_t> (tid))); 2107af245d11STodd Fiala assert (thread_sp && "AddThread() returned a nullptr"); 2108fa03ad2eSChaoren Lin 2109fa03ad2eSChaoren Lin // This will notify this is a new thread and tell the system it is stopped. 2110fa03ad2eSChaoren Lin monitor->NotifyThreadCreateStopped (tid); 2111cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGSTOP); 2112af245d11STodd Fiala monitor->SetCurrentThreadID (thread_sp->GetID ()); 2113af245d11STodd Fiala } 2114af245d11STodd Fiala 2115af245d11STodd Fiala // move the loop forward 2116af245d11STodd Fiala ++it; 2117af245d11STodd Fiala } 2118af245d11STodd Fiala } 2119af245d11STodd Fiala 2120af245d11STodd Fiala if (tids_to_attach.size() > 0) 2121af245d11STodd Fiala { 2122af245d11STodd Fiala monitor->m_pid = pid; 2123af245d11STodd Fiala // Let our process instance know the thread has stopped. 2124af245d11STodd Fiala monitor->SetState (StateType::eStateStopped); 2125af245d11STodd Fiala } 2126af245d11STodd Fiala else 2127af245d11STodd Fiala { 2128af245d11STodd Fiala args->m_error.SetErrorToGenericError(); 2129af245d11STodd Fiala args->m_error.SetErrorString("No such process."); 2130af245d11STodd Fiala } 2131af245d11STodd Fiala 2132af245d11STodd Fiala FINISH: 2133af245d11STodd Fiala return args->m_error.Success(); 2134af245d11STodd Fiala } 2135af245d11STodd Fiala 213697ccc294SChaoren Lin Error 2137af245d11STodd Fiala NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) 2138af245d11STodd Fiala { 2139af245d11STodd Fiala long ptrace_opts = 0; 2140af245d11STodd Fiala 2141af245d11STodd Fiala // Have the child raise an event on exit. This is used to keep the child in 2142af245d11STodd Fiala // limbo until it is destroyed. 2143af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXIT; 2144af245d11STodd Fiala 2145af245d11STodd Fiala // Have the tracer trace threads which spawn in the inferior process. 2146af245d11STodd Fiala // TODO: if we want to support tracing the inferiors' child, add the 2147af245d11STodd Fiala // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK) 2148af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACECLONE; 2149af245d11STodd Fiala 2150af245d11STodd Fiala // Have the tracer notify us before execve returns 2151af245d11STodd Fiala // (needed to disable legacy SIGTRAP generation) 2152af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXEC; 2153af245d11STodd Fiala 215497ccc294SChaoren Lin Error error; 215597ccc294SChaoren Lin PTRACE(PTRACE_SETOPTIONS, pid, nullptr, (void*)ptrace_opts, 0, error); 215697ccc294SChaoren Lin return error; 2157af245d11STodd Fiala } 2158af245d11STodd Fiala 2159af245d11STodd Fiala static ExitType convert_pid_status_to_exit_type (int status) 2160af245d11STodd Fiala { 2161af245d11STodd Fiala if (WIFEXITED (status)) 2162af245d11STodd Fiala return ExitType::eExitTypeExit; 2163af245d11STodd Fiala else if (WIFSIGNALED (status)) 2164af245d11STodd Fiala return ExitType::eExitTypeSignal; 2165af245d11STodd Fiala else if (WIFSTOPPED (status)) 2166af245d11STodd Fiala return ExitType::eExitTypeStop; 2167af245d11STodd Fiala else 2168af245d11STodd Fiala { 2169af245d11STodd Fiala // We don't know what this is. 2170af245d11STodd Fiala return ExitType::eExitTypeInvalid; 2171af245d11STodd Fiala } 2172af245d11STodd Fiala } 2173af245d11STodd Fiala 2174af245d11STodd Fiala static int convert_pid_status_to_return_code (int status) 2175af245d11STodd Fiala { 2176af245d11STodd Fiala if (WIFEXITED (status)) 2177af245d11STodd Fiala return WEXITSTATUS (status); 2178af245d11STodd Fiala else if (WIFSIGNALED (status)) 2179af245d11STodd Fiala return WTERMSIG (status); 2180af245d11STodd Fiala else if (WIFSTOPPED (status)) 2181af245d11STodd Fiala return WSTOPSIG (status); 2182af245d11STodd Fiala else 2183af245d11STodd Fiala { 2184af245d11STodd Fiala // We don't know what this is. 2185af245d11STodd Fiala return ExitType::eExitTypeInvalid; 2186af245d11STodd Fiala } 2187af245d11STodd Fiala } 2188af245d11STodd Fiala 21891107b5a5SPavel Labath // Handles all waitpid events from the inferior process. 21901107b5a5SPavel Labath void 21911107b5a5SPavel Labath NativeProcessLinux::MonitorCallback(lldb::pid_t pid, 2192af245d11STodd Fiala bool exited, 2193af245d11STodd Fiala int signal, 2194af245d11STodd Fiala int status) 2195af245d11STodd Fiala { 2196af245d11STodd Fiala Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); 2197af245d11STodd Fiala 2198af245d11STodd Fiala // Certain activities differ based on whether the pid is the tid of the main thread. 21991107b5a5SPavel Labath const bool is_main_thread = (pid == GetID ()); 2200af245d11STodd Fiala 2201af245d11STodd Fiala // Handle when the thread exits. 2202af245d11STodd Fiala if (exited) 2203af245d11STodd Fiala { 2204af245d11STodd Fiala if (log) 220586fd8e45SChaoren Lin log->Printf ("NativeProcessLinux::%s() got exit signal(%d) , tid = %" PRIu64 " (%s main thread)", __FUNCTION__, signal, pid, is_main_thread ? "is" : "is not"); 2206af245d11STodd Fiala 2207af245d11STodd Fiala // This is a thread that exited. Ensure we're not tracking it anymore. 22081107b5a5SPavel Labath const bool thread_found = StopTrackingThread (pid); 2209af245d11STodd Fiala 2210fa03ad2eSChaoren Lin // Make sure the thread state coordinator knows about this. 22111107b5a5SPavel Labath NotifyThreadDeath (pid); 2212fa03ad2eSChaoren Lin 2213af245d11STodd Fiala if (is_main_thread) 2214af245d11STodd Fiala { 2215af245d11STodd Fiala // We only set the exit status and notify the delegate if we haven't already set the process 2216af245d11STodd Fiala // state to an exited state. We normally should have received a SIGTRAP | (PTRACE_EVENT_EXIT << 8) 2217af245d11STodd Fiala // for the main thread. 22181107b5a5SPavel Labath const bool already_notified = (GetState() == StateType::eStateExited) || (GetState () == StateType::eStateCrashed); 2219af245d11STodd Fiala if (!already_notified) 2220af245d11STodd Fiala { 2221af245d11STodd Fiala if (log) 22221107b5a5SPavel 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 ())); 2223af245d11STodd Fiala // The main thread exited. We're done monitoring. Report to delegate. 22241107b5a5SPavel Labath SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); 2225af245d11STodd Fiala 2226af245d11STodd Fiala // Notify delegate that our process has exited. 22271107b5a5SPavel Labath SetState (StateType::eStateExited, true); 2228af245d11STodd Fiala } 2229af245d11STodd Fiala else 2230af245d11STodd Fiala { 2231af245d11STodd Fiala if (log) 2232af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() tid = %" PRIu64 " main thread now exited (%s)", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found"); 2233af245d11STodd Fiala } 2234af245d11STodd Fiala } 2235af245d11STodd Fiala else 2236af245d11STodd Fiala { 2237af245d11STodd Fiala // Do we want to report to the delegate in this case? I think not. If this was an orderly 2238af245d11STodd Fiala // thread exit, we would already have received the SIGTRAP | (PTRACE_EVENT_EXIT << 8) signal, 2239af245d11STodd Fiala // and we would have done an all-stop then. 2240af245d11STodd Fiala if (log) 2241af245d11STodd 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"); 2242af245d11STodd Fiala } 22431107b5a5SPavel Labath return; 2244af245d11STodd Fiala } 2245af245d11STodd Fiala 2246af245d11STodd Fiala // Get details on the signal raised. 2247af245d11STodd Fiala siginfo_t info; 22481107b5a5SPavel Labath const auto err = GetSignalInfo(pid, &info); 224997ccc294SChaoren Lin if (err.Success()) 2250fa03ad2eSChaoren Lin { 2251fa03ad2eSChaoren Lin // We have retrieved the signal info. Dispatch appropriately. 2252fa03ad2eSChaoren Lin if (info.si_signo == SIGTRAP) 22531107b5a5SPavel Labath MonitorSIGTRAP(&info, pid); 2254fa03ad2eSChaoren Lin else 22551107b5a5SPavel Labath MonitorSignal(&info, pid, exited); 2256fa03ad2eSChaoren Lin } 2257fa03ad2eSChaoren Lin else 2258af245d11STodd Fiala { 225997ccc294SChaoren Lin if (err.GetError() == EINVAL) 2260af245d11STodd Fiala { 2261fa03ad2eSChaoren Lin // This is a group stop reception for this tid. 2262fa03ad2eSChaoren Lin if (log) 22631107b5a5SPavel Labath log->Printf ("NativeThreadLinux::%s received a group stop for pid %" PRIu64 " tid %" PRIu64, __FUNCTION__, GetID (), pid); 22641107b5a5SPavel Labath NotifyThreadStop (pid); 2265a9882ceeSTodd Fiala } 2266a9882ceeSTodd Fiala else 2267a9882ceeSTodd Fiala { 2268af245d11STodd Fiala // ptrace(GETSIGINFO) failed (but not due to group-stop). 2269af245d11STodd Fiala 2270af245d11STodd Fiala // A return value of ESRCH means the thread/process is no longer on the system, 2271af245d11STodd Fiala // so it was killed somehow outside of our control. Either way, we can't do anything 2272af245d11STodd Fiala // with it anymore. 2273af245d11STodd Fiala 2274af245d11STodd Fiala // Stop tracking the metadata for the thread since it's entirely off the system now. 22751107b5a5SPavel Labath const bool thread_found = StopTrackingThread (pid); 2276af245d11STodd Fiala 2277fa03ad2eSChaoren Lin // Make sure the thread state coordinator knows about this. 22781107b5a5SPavel Labath NotifyThreadDeath (pid); 2279fa03ad2eSChaoren Lin 2280af245d11STodd Fiala if (log) 2281af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d (%s, %s, %s)", 228297ccc294SChaoren 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"); 2283af245d11STodd Fiala 2284af245d11STodd Fiala if (is_main_thread) 2285af245d11STodd Fiala { 2286af245d11STodd Fiala // Notify the delegate - our process is not available but appears to have been killed outside 2287af245d11STodd Fiala // our control. Is eStateExited the right exit state in this case? 22881107b5a5SPavel Labath SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); 22891107b5a5SPavel Labath SetState (StateType::eStateExited, true); 2290af245d11STodd Fiala } 2291af245d11STodd Fiala else 2292af245d11STodd Fiala { 2293af245d11STodd Fiala // This thread was pulled out from underneath us. Anything to do here? Do we want to do an all stop? 2294af245d11STodd Fiala if (log) 22951107b5a5SPavel 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); 2296af245d11STodd Fiala } 2297af245d11STodd Fiala } 2298af245d11STodd Fiala } 2299af245d11STodd Fiala } 2300af245d11STodd Fiala 2301af245d11STodd Fiala void 2302af245d11STodd Fiala NativeProcessLinux::MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid) 2303af245d11STodd Fiala { 2304af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2305af245d11STodd Fiala const bool is_main_thread = (pid == GetID ()); 2306af245d11STodd Fiala 2307af245d11STodd Fiala assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!"); 2308af245d11STodd Fiala if (!info) 2309af245d11STodd Fiala return; 2310af245d11STodd Fiala 23115830aa75STamas Berghammer Mutex::Locker locker (m_threads_mutex); 23125830aa75STamas Berghammer 2313af245d11STodd Fiala // See if we can find a thread for this signal. 2314af245d11STodd Fiala NativeThreadProtocolSP thread_sp = GetThreadByID (pid); 2315af245d11STodd Fiala if (!thread_sp) 2316af245d11STodd Fiala { 2317af245d11STodd Fiala if (log) 2318af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " no thread found for tid %" PRIu64, __FUNCTION__, GetID (), pid); 2319af245d11STodd Fiala } 2320af245d11STodd Fiala 2321af245d11STodd Fiala switch (info->si_code) 2322af245d11STodd Fiala { 2323af245d11STodd 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. 2324af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)): 2325af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)): 2326af245d11STodd Fiala 2327af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)): 2328af245d11STodd Fiala { 2329af245d11STodd Fiala lldb::tid_t tid = LLDB_INVALID_THREAD_ID; 2330af245d11STodd Fiala 2331fa03ad2eSChaoren Lin // The main thread is stopped here. 2332fa03ad2eSChaoren Lin if (thread_sp) 2333cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGTRAP); 2334fa03ad2eSChaoren Lin NotifyThreadStop (pid); 2335fa03ad2eSChaoren Lin 2336af245d11STodd Fiala unsigned long event_message = 0; 233797ccc294SChaoren Lin if (GetEventMessage (pid, &event_message).Success()) 2338fa03ad2eSChaoren Lin { 2339af245d11STodd Fiala tid = static_cast<lldb::tid_t> (event_message); 2340af245d11STodd Fiala if (log) 2341af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " received thread creation event for tid %" PRIu64, __FUNCTION__, pid, tid); 2342af245d11STodd Fiala 2343af245d11STodd Fiala // If we don't track the thread yet: create it, mark as stopped. 2344af245d11STodd Fiala // If we do track it, this is the wait we needed. Now resume the new thread. 2345af245d11STodd Fiala // In all cases, resume the current (i.e. main process) thread. 2346511e5cdcSTodd Fiala bool created_now = false; 2347fa03ad2eSChaoren Lin NativeThreadProtocolSP new_thread_sp = GetOrCreateThread (tid, created_now); 2348fa03ad2eSChaoren Lin assert (new_thread_sp.get() && "failed to get or create the tracking data for newly created inferior thread"); 2349af245d11STodd Fiala 2350af245d11STodd Fiala // If the thread was already tracked, it means the created thread already received its SI_USER notification of creation. 2351511e5cdcSTodd Fiala if (!created_now) 2352af245d11STodd Fiala { 2353af245d11STodd Fiala // We can now resume the newly created thread since it is fully created. 2354fa03ad2eSChaoren Lin NotifyThreadCreateStopped (tid); 2355fa03ad2eSChaoren Lin m_coordinator_up->RequestThreadResume (tid, 235686fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 2357fa03ad2eSChaoren Lin { 2358cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (new_thread_sp)->SetRunning (); 235937c768caSChaoren Lin return Resume (tid_to_resume, LLDB_INVALID_SIGNAL_NUMBER); 2360fa03ad2eSChaoren Lin }, 2361fa03ad2eSChaoren Lin CoordinatorErrorHandler); 2362af245d11STodd Fiala } 2363af245d11STodd Fiala else 2364af245d11STodd Fiala { 2365af245d11STodd Fiala // Mark the thread as currently launching. Need to wait for SIGTRAP clone on the main thread before 2366af245d11STodd Fiala // this thread is ready to go. 2367cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (new_thread_sp)->SetLaunching (); 2368fa03ad2eSChaoren Lin } 2369fa03ad2eSChaoren Lin } 2370fa03ad2eSChaoren Lin else 2371fa03ad2eSChaoren Lin { 2372fa03ad2eSChaoren Lin if (log) 2373fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " received thread creation event but GetEventMessage failed so we don't know the new tid", __FUNCTION__, pid); 2374af245d11STodd Fiala } 2375af245d11STodd Fiala 2376af245d11STodd Fiala // In all cases, we can resume the main thread here. 2377fa03ad2eSChaoren Lin m_coordinator_up->RequestThreadResume (pid, 237886fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 2379fa03ad2eSChaoren Lin { 2380cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); 238137c768caSChaoren Lin return Resume (tid_to_resume, LLDB_INVALID_SIGNAL_NUMBER); 2382fa03ad2eSChaoren Lin }, 2383fa03ad2eSChaoren Lin CoordinatorErrorHandler); 2384fa03ad2eSChaoren Lin 2385af245d11STodd Fiala break; 2386af245d11STodd Fiala } 2387af245d11STodd Fiala 2388af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): 2389a9882ceeSTodd Fiala { 2390a9882ceeSTodd Fiala NativeThreadProtocolSP main_thread_sp; 2391af245d11STodd Fiala if (log) 2392af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP); 2393a9882ceeSTodd Fiala 2394fa03ad2eSChaoren Lin // The thread state coordinator needs to reset due to the exec. 2395fa03ad2eSChaoren Lin m_coordinator_up->ResetForExec (); 2396fa03ad2eSChaoren Lin 2397fa03ad2eSChaoren 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. 2398a9882ceeSTodd Fiala if (log) 2399a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s exec received, stop tracking all but main thread", __FUNCTION__); 2400a9882ceeSTodd Fiala 2401a9882ceeSTodd Fiala for (auto thread_sp : m_threads) 2402a9882ceeSTodd Fiala { 2403a9882ceeSTodd Fiala const bool is_main_thread = thread_sp && thread_sp->GetID () == GetID (); 2404a9882ceeSTodd Fiala if (is_main_thread) 2405a9882ceeSTodd Fiala { 2406a9882ceeSTodd Fiala main_thread_sp = thread_sp; 2407a9882ceeSTodd Fiala if (log) 2408a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s found main thread with tid %" PRIu64 ", keeping", __FUNCTION__, main_thread_sp->GetID ()); 2409a9882ceeSTodd Fiala } 2410a9882ceeSTodd Fiala else 2411a9882ceeSTodd Fiala { 2412fa03ad2eSChaoren Lin // Tell thread coordinator this thread is dead. 2413a9882ceeSTodd Fiala if (log) 2414a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s discarding non-main-thread tid %" PRIu64 " due to exec", __FUNCTION__, thread_sp->GetID ()); 2415a9882ceeSTodd Fiala } 2416a9882ceeSTodd Fiala } 2417a9882ceeSTodd Fiala 2418a9882ceeSTodd Fiala m_threads.clear (); 2419a9882ceeSTodd Fiala 2420a9882ceeSTodd Fiala if (main_thread_sp) 2421a9882ceeSTodd Fiala { 2422a9882ceeSTodd Fiala m_threads.push_back (main_thread_sp); 2423a9882ceeSTodd Fiala SetCurrentThreadID (main_thread_sp->GetID ()); 2424cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (main_thread_sp)->SetStoppedByExec (); 2425a9882ceeSTodd Fiala } 2426a9882ceeSTodd Fiala else 2427a9882ceeSTodd Fiala { 2428a9882ceeSTodd Fiala SetCurrentThreadID (LLDB_INVALID_THREAD_ID); 2429a9882ceeSTodd Fiala if (log) 2430a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 "no main thread found, discarded all threads, we're in a no-thread state!", __FUNCTION__, GetID ()); 2431a9882ceeSTodd Fiala } 2432a9882ceeSTodd Fiala 2433fa03ad2eSChaoren Lin // Tell coordinator about about the "new" (since exec) stopped main thread. 2434fa03ad2eSChaoren Lin const lldb::tid_t main_thread_tid = GetID (); 2435fa03ad2eSChaoren Lin NotifyThreadCreateStopped (main_thread_tid); 2436fa03ad2eSChaoren Lin 2437fa03ad2eSChaoren Lin // NOTE: ideally these next statements would execute at the same time as the coordinator thread create was executed. 2438fa03ad2eSChaoren Lin // Consider a handler that can execute when that happens. 2439a9882ceeSTodd Fiala // Let our delegate know we have just exec'd. 2440a9882ceeSTodd Fiala NotifyDidExec (); 2441a9882ceeSTodd Fiala 2442a9882ceeSTodd Fiala // If we have a main thread, indicate we are stopped. 2443a9882ceeSTodd Fiala assert (main_thread_sp && "exec called during ptraced process but no main thread metadata tracked"); 2444fa03ad2eSChaoren Lin 2445fa03ad2eSChaoren Lin // Let the process know we're stopped. 24465830aa75STamas Berghammer CallAfterRunningThreadsStop (pid, 24475830aa75STamas Berghammer [=] (lldb::tid_t signaling_tid) 24485830aa75STamas Berghammer { 24495830aa75STamas Berghammer SetState (StateType::eStateStopped, true); 24505830aa75STamas Berghammer }); 2451a9882ceeSTodd Fiala 2452af245d11STodd Fiala break; 2453a9882ceeSTodd Fiala } 2454af245d11STodd Fiala 2455af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): 2456af245d11STodd Fiala { 2457af245d11STodd Fiala // The inferior process or one of its threads is about to exit. 2458fa03ad2eSChaoren Lin 2459fa03ad2eSChaoren Lin // This thread is currently stopped. It's not actually dead yet, just about to be. 2460fa03ad2eSChaoren Lin NotifyThreadStop (pid); 2461fa03ad2eSChaoren Lin 2462af245d11STodd Fiala unsigned long data = 0; 246397ccc294SChaoren Lin if (GetEventMessage(pid, &data).Fail()) 2464af245d11STodd Fiala data = -1; 2465af245d11STodd Fiala 2466af245d11STodd Fiala if (log) 2467af245d11STodd Fiala { 2468af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received PTRACE_EVENT_EXIT, data = %lx (WIFEXITED=%s,WIFSIGNALED=%s), pid = %" PRIu64 " (%s)", 2469af245d11STodd Fiala __FUNCTION__, 2470af245d11STodd Fiala data, WIFEXITED (data) ? "true" : "false", WIFSIGNALED (data) ? "true" : "false", 2471af245d11STodd Fiala pid, 2472af245d11STodd Fiala is_main_thread ? "is main thread" : "not main thread"); 2473af245d11STodd Fiala } 2474af245d11STodd Fiala 2475af245d11STodd Fiala if (is_main_thread) 2476af245d11STodd Fiala { 2477af245d11STodd Fiala SetExitStatus (convert_pid_status_to_exit_type (data), convert_pid_status_to_return_code (data), nullptr, true); 247875f47c3aSTodd Fiala } 247975f47c3aSTodd Fiala 24809d617ba6SChaoren Lin const int signo = static_cast<int> (data); 2481fa03ad2eSChaoren Lin m_coordinator_up->RequestThreadResume (pid, 248286fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 2483fa03ad2eSChaoren Lin { 2484cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); 248537c768caSChaoren Lin return Resume (tid_to_resume, (supress_signal) ? LLDB_INVALID_SIGNAL_NUMBER : signo); 2486fa03ad2eSChaoren Lin }, 2487fa03ad2eSChaoren Lin CoordinatorErrorHandler); 2488af245d11STodd Fiala 2489af245d11STodd Fiala break; 2490af245d11STodd Fiala } 2491af245d11STodd Fiala 2492af245d11STodd Fiala case 0: 2493c16f5dcaSChaoren Lin case TRAP_TRACE: // We receive this on single stepping. 2494c16f5dcaSChaoren Lin case TRAP_HWBKPT: // We receive this on watchpoint hit 249586fd8e45SChaoren Lin if (thread_sp) 249686fd8e45SChaoren Lin { 2497c16f5dcaSChaoren Lin // If a watchpoint was hit, report it 2498c16f5dcaSChaoren Lin uint32_t wp_index; 2499c16f5dcaSChaoren Lin Error error = thread_sp->GetRegisterContext()->GetWatchpointHitIndex(wp_index); 2500c16f5dcaSChaoren Lin if (error.Fail() && log) 2501c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() " 2502c16f5dcaSChaoren Lin "received error while checking for watchpoint hits, " 2503c16f5dcaSChaoren Lin "pid = %" PRIu64 " error = %s", 2504c16f5dcaSChaoren Lin __FUNCTION__, pid, error.AsCString()); 2505c16f5dcaSChaoren Lin if (wp_index != LLDB_INVALID_INDEX32) 25065830aa75STamas Berghammer { 2507c16f5dcaSChaoren Lin MonitorWatchpoint(pid, thread_sp, wp_index); 2508c16f5dcaSChaoren Lin break; 2509c16f5dcaSChaoren Lin } 2510c16f5dcaSChaoren Lin } 2511c16f5dcaSChaoren Lin // Otherwise, report step over 2512c16f5dcaSChaoren Lin MonitorTrace(pid, thread_sp); 2513af245d11STodd Fiala break; 2514af245d11STodd Fiala 2515af245d11STodd Fiala case SI_KERNEL: 2516af245d11STodd Fiala case TRAP_BRKPT: 2517c16f5dcaSChaoren Lin MonitorBreakpoint(pid, thread_sp); 2518af245d11STodd Fiala break; 2519af245d11STodd Fiala 2520af245d11STodd Fiala case SIGTRAP: 2521af245d11STodd Fiala case (SIGTRAP | 0x80): 2522af245d11STodd Fiala if (log) 2523fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s() received unknown SIGTRAP system call stop event, pid %" PRIu64 "tid %" PRIu64 ", resuming", __FUNCTION__, GetID (), pid); 2524fa03ad2eSChaoren Lin 2525fa03ad2eSChaoren Lin // This thread is currently stopped. 2526fa03ad2eSChaoren Lin NotifyThreadStop (pid); 2527fa03ad2eSChaoren Lin if (thread_sp) 2528cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (SIGTRAP); 2529fa03ad2eSChaoren Lin 2530fa03ad2eSChaoren Lin 2531af245d11STodd Fiala // Ignore these signals until we know more about them. 2532fa03ad2eSChaoren Lin m_coordinator_up->RequestThreadResume (pid, 253386fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 2534fa03ad2eSChaoren Lin { 2535cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); 253637c768caSChaoren Lin return Resume (tid_to_resume, LLDB_INVALID_SIGNAL_NUMBER); 2537fa03ad2eSChaoren Lin }, 2538fa03ad2eSChaoren Lin CoordinatorErrorHandler); 2539af245d11STodd Fiala break; 2540af245d11STodd Fiala 2541af245d11STodd Fiala default: 2542af245d11STodd Fiala assert(false && "Unexpected SIGTRAP code!"); 2543af245d11STodd Fiala if (log) 2544af245d11STodd 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))); 2545af245d11STodd Fiala break; 2546af245d11STodd Fiala 2547af245d11STodd Fiala } 2548af245d11STodd Fiala } 2549af245d11STodd Fiala 2550af245d11STodd Fiala void 2551c16f5dcaSChaoren Lin NativeProcessLinux::MonitorTrace(lldb::pid_t pid, NativeThreadProtocolSP thread_sp) 2552c16f5dcaSChaoren Lin { 2553c16f5dcaSChaoren Lin Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 2554c16f5dcaSChaoren Lin if (log) 2555c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() received trace event, pid = %" PRIu64 " (single stepping)", 2556c16f5dcaSChaoren Lin __FUNCTION__, pid); 2557c16f5dcaSChaoren Lin 2558c16f5dcaSChaoren Lin if (thread_sp) 2559c16f5dcaSChaoren Lin std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByTrace(); 2560c16f5dcaSChaoren Lin 2561c16f5dcaSChaoren Lin // This thread is currently stopped. 2562c16f5dcaSChaoren Lin NotifyThreadStop(pid); 2563c16f5dcaSChaoren Lin 2564c16f5dcaSChaoren Lin // Here we don't have to request the rest of the threads to stop or request a deferred stop. 2565c16f5dcaSChaoren Lin // This would have already happened at the time the Resume() with step operation was signaled. 2566c16f5dcaSChaoren Lin // At this point, we just need to say we stopped, and the deferred notifcation will fire off 2567c16f5dcaSChaoren Lin // once all running threads have checked in as stopped. 2568c16f5dcaSChaoren Lin SetCurrentThreadID(pid); 2569c16f5dcaSChaoren Lin // Tell the process we have a stop (from software breakpoint). 2570c16f5dcaSChaoren Lin CallAfterRunningThreadsStop(pid, 2571c16f5dcaSChaoren Lin [=](lldb::tid_t signaling_tid) 2572c16f5dcaSChaoren Lin { 2573c16f5dcaSChaoren Lin SetState(StateType::eStateStopped, true); 2574c16f5dcaSChaoren Lin }); 2575c16f5dcaSChaoren Lin } 2576c16f5dcaSChaoren Lin 2577c16f5dcaSChaoren Lin void 2578c16f5dcaSChaoren Lin NativeProcessLinux::MonitorBreakpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp) 2579c16f5dcaSChaoren Lin { 2580c16f5dcaSChaoren Lin Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 2581c16f5dcaSChaoren Lin if (log) 2582c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() received breakpoint event, pid = %" PRIu64, 2583c16f5dcaSChaoren Lin __FUNCTION__, pid); 2584c16f5dcaSChaoren Lin 2585c16f5dcaSChaoren Lin // This thread is currently stopped. 2586c16f5dcaSChaoren Lin NotifyThreadStop(pid); 2587c16f5dcaSChaoren Lin 2588c16f5dcaSChaoren Lin // Mark the thread as stopped at breakpoint. 2589c16f5dcaSChaoren Lin if (thread_sp) 2590c16f5dcaSChaoren Lin { 2591c16f5dcaSChaoren Lin std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByBreakpoint(); 2592c16f5dcaSChaoren Lin Error error = FixupBreakpointPCAsNeeded(thread_sp); 2593c16f5dcaSChaoren Lin if (error.Fail()) 2594c16f5dcaSChaoren Lin if (log) 2595c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " fixup: %s", 2596c16f5dcaSChaoren Lin __FUNCTION__, pid, error.AsCString()); 2597d8c338d4STamas Berghammer 2598d8c338d4STamas Berghammer auto it = m_threads_stepping_with_breakpoint.find(pid); 2599d8c338d4STamas Berghammer if (it != m_threads_stepping_with_breakpoint.end()) 2600d8c338d4STamas Berghammer { 2601d8c338d4STamas Berghammer Error error = RemoveBreakpoint (it->second); 2602d8c338d4STamas Berghammer if (error.Fail()) 2603d8c338d4STamas Berghammer if (log) 2604d8c338d4STamas Berghammer log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " remove stepping breakpoint: %s", 2605d8c338d4STamas Berghammer __FUNCTION__, pid, error.AsCString()); 2606d8c338d4STamas Berghammer 2607d8c338d4STamas Berghammer m_threads_stepping_with_breakpoint.erase(it); 2608d8c338d4STamas Berghammer std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByTrace(); 2609d8c338d4STamas Berghammer } 2610c16f5dcaSChaoren Lin } 2611c16f5dcaSChaoren Lin else 2612c16f5dcaSChaoren Lin if (log) 2613c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 ": " 2614c16f5dcaSChaoren Lin "warning, cannot process software breakpoint since no thread metadata", 2615c16f5dcaSChaoren Lin __FUNCTION__, pid); 2616c16f5dcaSChaoren Lin 2617c16f5dcaSChaoren Lin 2618c16f5dcaSChaoren Lin // We need to tell all other running threads before we notify the delegate about this stop. 2619c16f5dcaSChaoren Lin CallAfterRunningThreadsStop(pid, 2620c16f5dcaSChaoren Lin [=](lldb::tid_t deferred_notification_tid) 2621c16f5dcaSChaoren Lin { 2622c16f5dcaSChaoren Lin SetCurrentThreadID(deferred_notification_tid); 2623c16f5dcaSChaoren Lin // Tell the process we have a stop (from software breakpoint). 2624c16f5dcaSChaoren Lin SetState(StateType::eStateStopped, true); 2625c16f5dcaSChaoren Lin }); 2626c16f5dcaSChaoren Lin } 2627c16f5dcaSChaoren Lin 2628c16f5dcaSChaoren Lin void 2629c16f5dcaSChaoren Lin NativeProcessLinux::MonitorWatchpoint(lldb::pid_t pid, NativeThreadProtocolSP thread_sp, uint32_t wp_index) 2630c16f5dcaSChaoren Lin { 2631c16f5dcaSChaoren Lin Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_WATCHPOINTS)); 2632c16f5dcaSChaoren Lin if (log) 2633c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() received watchpoint event, " 2634c16f5dcaSChaoren Lin "pid = %" PRIu64 ", wp_index = %" PRIu32, 2635c16f5dcaSChaoren Lin __FUNCTION__, pid, wp_index); 2636c16f5dcaSChaoren Lin 2637c16f5dcaSChaoren Lin // This thread is currently stopped. 2638c16f5dcaSChaoren Lin NotifyThreadStop(pid); 2639c16f5dcaSChaoren Lin 2640c16f5dcaSChaoren Lin // Mark the thread as stopped at watchpoint. 2641c16f5dcaSChaoren Lin // The address is at (lldb::addr_t)info->si_addr if we need it. 2642c16f5dcaSChaoren Lin lldbassert(thread_sp && "thread_sp cannot be NULL"); 2643c16f5dcaSChaoren Lin std::static_pointer_cast<NativeThreadLinux>(thread_sp)->SetStoppedByWatchpoint(wp_index); 2644c16f5dcaSChaoren Lin 2645c16f5dcaSChaoren Lin // We need to tell all other running threads before we notify the delegate about this stop. 2646c16f5dcaSChaoren Lin CallAfterRunningThreadsStop(pid, 2647c16f5dcaSChaoren Lin [=](lldb::tid_t deferred_notification_tid) 2648c16f5dcaSChaoren Lin { 2649c16f5dcaSChaoren Lin SetCurrentThreadID(deferred_notification_tid); 2650c16f5dcaSChaoren Lin // Tell the process we have a stop (from watchpoint). 2651c16f5dcaSChaoren Lin SetState(StateType::eStateStopped, true); 2652c16f5dcaSChaoren Lin }); 2653c16f5dcaSChaoren Lin } 2654c16f5dcaSChaoren Lin 2655c16f5dcaSChaoren Lin void 2656af245d11STodd Fiala NativeProcessLinux::MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited) 2657af245d11STodd Fiala { 2658511e5cdcSTodd Fiala assert (info && "null info"); 2659511e5cdcSTodd Fiala if (!info) 2660511e5cdcSTodd Fiala return; 2661511e5cdcSTodd Fiala 2662511e5cdcSTodd Fiala const int signo = info->si_signo; 2663511e5cdcSTodd Fiala const bool is_from_llgs = info->si_pid == getpid (); 2664af245d11STodd Fiala 2665af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2666af245d11STodd Fiala 2667af245d11STodd Fiala // POSIX says that process behaviour is undefined after it ignores a SIGFPE, 2668af245d11STodd Fiala // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a 2669af245d11STodd Fiala // kill(2) or raise(3). Similarly for tgkill(2) on Linux. 2670af245d11STodd Fiala // 2671af245d11STodd Fiala // IOW, user generated signals never generate what we consider to be a 2672af245d11STodd Fiala // "crash". 2673af245d11STodd Fiala // 2674af245d11STodd Fiala // Similarly, ACK signals generated by this monitor. 2675af245d11STodd Fiala 26765830aa75STamas Berghammer Mutex::Locker locker (m_threads_mutex); 26775830aa75STamas Berghammer 2678af245d11STodd Fiala // See if we can find a thread for this signal. 2679af245d11STodd Fiala NativeThreadProtocolSP thread_sp = GetThreadByID (pid); 2680af245d11STodd Fiala if (!thread_sp) 2681af245d11STodd Fiala { 2682af245d11STodd Fiala if (log) 2683af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " no thread found for tid %" PRIu64, __FUNCTION__, GetID (), pid); 2684af245d11STodd Fiala } 2685af245d11STodd Fiala 2686af245d11STodd Fiala // Handle the signal. 2687af245d11STodd Fiala if (info->si_code == SI_TKILL || info->si_code == SI_USER) 2688af245d11STodd Fiala { 2689af245d11STodd Fiala if (log) 2690af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received signal %s (%d) with code %s, (siginfo pid = %d (%s), waitpid pid = %" PRIu64 ")", 2691af245d11STodd Fiala __FUNCTION__, 2692af245d11STodd Fiala GetUnixSignals ().GetSignalAsCString (signo), 2693af245d11STodd Fiala signo, 2694af245d11STodd Fiala (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"), 2695af245d11STodd Fiala info->si_pid, 2696511e5cdcSTodd Fiala is_from_llgs ? "from llgs" : "not from llgs", 2697af245d11STodd Fiala pid); 269858a2f669STodd Fiala } 2699af245d11STodd Fiala 270058a2f669STodd Fiala // Check for new thread notification. 270158a2f669STodd Fiala if ((info->si_pid == 0) && (info->si_code == SI_USER)) 2702af245d11STodd Fiala { 2703af245d11STodd Fiala // A new thread creation is being signaled. This is one of two parts that come in 2704af245d11STodd Fiala // a non-deterministic order. pid is the thread id. 2705af245d11STodd Fiala if (log) 2706af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 " tid %" PRIu64 ": new thread notification", 2707af245d11STodd Fiala __FUNCTION__, GetID (), pid); 2708af245d11STodd Fiala 2709af245d11STodd Fiala // Did we already create the thread? 2710511e5cdcSTodd Fiala bool created_now = false; 2711511e5cdcSTodd Fiala thread_sp = GetOrCreateThread (pid, created_now); 2712af245d11STodd Fiala assert (thread_sp.get() && "failed to get or create the tracking data for newly created inferior thread"); 2713af245d11STodd Fiala 2714af245d11STodd Fiala // If the thread was already tracked, it means the main thread already received its SIGTRAP for the create. 2715511e5cdcSTodd Fiala if (!created_now) 2716af245d11STodd Fiala { 2717fa03ad2eSChaoren Lin // We can now resume the newly created thread since it is fully created. 2718fa03ad2eSChaoren Lin NotifyThreadCreateStopped (pid); 2719fa03ad2eSChaoren Lin m_coordinator_up->RequestThreadResume (pid, 272086fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 2721fa03ad2eSChaoren Lin { 2722cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); 272337c768caSChaoren Lin return Resume (tid_to_resume, LLDB_INVALID_SIGNAL_NUMBER); 2724fa03ad2eSChaoren Lin }, 2725fa03ad2eSChaoren Lin CoordinatorErrorHandler); 2726af245d11STodd Fiala } 2727af245d11STodd Fiala else 2728af245d11STodd Fiala { 2729af245d11STodd Fiala // Mark the thread as currently launching. Need to wait for SIGTRAP clone on the main thread before 2730af245d11STodd Fiala // this thread is ready to go. 2731cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetLaunching (); 2732af245d11STodd Fiala } 273358a2f669STodd Fiala 273458a2f669STodd Fiala // Done handling. 273558a2f669STodd Fiala return; 2736af245d11STodd Fiala } 273758a2f669STodd Fiala 273858a2f669STodd Fiala // Check for thread stop notification. 2739511e5cdcSTodd Fiala if (is_from_llgs && (info->si_code == SI_TKILL) && (signo == SIGSTOP)) 2740af245d11STodd Fiala { 2741af245d11STodd Fiala // This is a tgkill()-based stop. 2742af245d11STodd Fiala if (thread_sp) 2743af245d11STodd Fiala { 2744fa03ad2eSChaoren Lin if (log) 2745fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 ", thread stopped", 2746fa03ad2eSChaoren Lin __FUNCTION__, 2747fa03ad2eSChaoren Lin GetID (), 2748fa03ad2eSChaoren Lin pid); 2749fa03ad2eSChaoren Lin 2750aab58633SChaoren Lin // Check that we're not already marked with a stop reason. 2751aab58633SChaoren Lin // Note this thread really shouldn't already be marked as stopped - if we were, that would imply that 2752aab58633SChaoren Lin // the kernel signaled us with the thread stopping which we handled and marked as stopped, 2753aab58633SChaoren Lin // and that, without an intervening resume, we received another stop. It is more likely 2754aab58633SChaoren Lin // that we are missing the marking of a run state somewhere if we find that the thread was 2755aab58633SChaoren Lin // marked as stopped. 2756cb84eebbSTamas Berghammer std::shared_ptr<NativeThreadLinux> linux_thread_sp = std::static_pointer_cast<NativeThreadLinux> (thread_sp); 2757cb84eebbSTamas Berghammer assert (linux_thread_sp && "linux_thread_sp is null!"); 2758aab58633SChaoren Lin 2759cb84eebbSTamas Berghammer const StateType thread_state = linux_thread_sp->GetState (); 2760aab58633SChaoren Lin if (!StateIsStoppedState (thread_state, false)) 2761aab58633SChaoren Lin { 276203f12d6bSChaoren Lin // An inferior thread just stopped, but was not the primary cause of the process stop. 276303f12d6bSChaoren Lin // Instead, something else (like a breakpoint or step) caused the stop. Mark the 276403f12d6bSChaoren Lin // stop signal as 0 to let lldb know this isn't the important stop. 2765cb84eebbSTamas Berghammer linux_thread_sp->SetStoppedBySignal (0); 2766af245d11STodd Fiala SetCurrentThreadID (thread_sp->GetID ()); 276786fd8e45SChaoren Lin m_coordinator_up->NotifyThreadStop (thread_sp->GetID (), true, CoordinatorErrorHandler); 2768aab58633SChaoren Lin } 2769aab58633SChaoren Lin else 2770aab58633SChaoren Lin { 2771aab58633SChaoren Lin if (log) 2772aab58633SChaoren Lin { 2773aab58633SChaoren Lin // Retrieve the signal name if the thread was stopped by a signal. 2774aab58633SChaoren Lin int stop_signo = 0; 2775cb84eebbSTamas Berghammer const bool stopped_by_signal = linux_thread_sp->IsStopped (&stop_signo); 2776aab58633SChaoren Lin const char *signal_name = stopped_by_signal ? GetUnixSignals ().GetSignalAsCString (stop_signo) : "<not stopped by signal>"; 2777aab58633SChaoren Lin if (!signal_name) 2778aab58633SChaoren Lin signal_name = "<no-signal-name>"; 2779aab58633SChaoren Lin 2780aab58633SChaoren 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", 2781aab58633SChaoren Lin __FUNCTION__, 2782aab58633SChaoren Lin GetID (), 2783cb84eebbSTamas Berghammer linux_thread_sp->GetID (), 2784aab58633SChaoren Lin StateAsCString (thread_state), 2785aab58633SChaoren Lin stop_signo, 2786aab58633SChaoren Lin signal_name); 2787aab58633SChaoren Lin } 2788fa03ad2eSChaoren Lin // Tell the thread state coordinator about the stop. 2789fa03ad2eSChaoren Lin NotifyThreadStop (thread_sp->GetID ()); 2790af245d11STodd Fiala } 279186fd8e45SChaoren Lin } 2792af245d11STodd Fiala 279358a2f669STodd Fiala // Done handling. 2794af245d11STodd Fiala return; 2795af245d11STodd Fiala } 2796af245d11STodd Fiala 2797af245d11STodd Fiala if (log) 2798af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received signal %s", __FUNCTION__, GetUnixSignals ().GetSignalAsCString (signo)); 2799af245d11STodd Fiala 280086fd8e45SChaoren Lin // This thread is stopped. 280186fd8e45SChaoren Lin NotifyThreadStop (pid); 280286fd8e45SChaoren Lin 2803af245d11STodd Fiala switch (signo) 2804af245d11STodd Fiala { 2805511e5cdcSTodd Fiala case SIGSTOP: 2806511e5cdcSTodd Fiala { 280758a2f669STodd Fiala if (log) 2808511e5cdcSTodd Fiala { 2809511e5cdcSTodd Fiala if (is_from_llgs) 2810511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " received SIGSTOP from llgs, most likely an interrupt", __FUNCTION__, GetID (), pid); 2811511e5cdcSTodd Fiala else 2812511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " received SIGSTOP from outside of debugger", __FUNCTION__, GetID (), pid); 2813511e5cdcSTodd Fiala } 2814511e5cdcSTodd Fiala 2815fa03ad2eSChaoren Lin // Resume this thread to get the group-stop mechanism to fire off the true group stops. 2816fa03ad2eSChaoren Lin // This thread will get stopped again as part of the group-stop completion. 2817fa03ad2eSChaoren Lin m_coordinator_up->RequestThreadResume (pid, 281886fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 2819fa03ad2eSChaoren Lin { 2820cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); 2821fa03ad2eSChaoren Lin // Pass this signal number on to the inferior to handle. 282237c768caSChaoren Lin return Resume (tid_to_resume, (supress_signal) ? LLDB_INVALID_SIGNAL_NUMBER : signo); 2823fa03ad2eSChaoren Lin }, 2824fa03ad2eSChaoren Lin CoordinatorErrorHandler); 282586fd8e45SChaoren Lin } 282686fd8e45SChaoren Lin break; 282786fd8e45SChaoren Lin case SIGSEGV: 282886fd8e45SChaoren Lin case SIGILL: 282986fd8e45SChaoren Lin case SIGFPE: 283086fd8e45SChaoren Lin case SIGBUS: 283186fd8e45SChaoren Lin if (thread_sp) 2832cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetCrashedWithException (*info); 283386fd8e45SChaoren Lin break; 283486fd8e45SChaoren Lin default: 283586fd8e45SChaoren Lin // This is just a pre-signal-delivery notification of the incoming signal. 283686fd8e45SChaoren Lin if (thread_sp) 2837cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStoppedBySignal (signo); 2838fa03ad2eSChaoren Lin 283986fd8e45SChaoren Lin break; 284086fd8e45SChaoren Lin } 284186fd8e45SChaoren Lin 284286fd8e45SChaoren Lin // Send a stop to the debugger after we get all other threads to stop. 2843fa03ad2eSChaoren Lin CallAfterRunningThreadsStop (pid, 2844fa03ad2eSChaoren Lin [=] (lldb::tid_t signaling_tid) 2845fa03ad2eSChaoren Lin { 2846fa03ad2eSChaoren Lin SetCurrentThreadID (signaling_tid); 2847fa03ad2eSChaoren Lin SetState (StateType::eStateStopped, true); 2848fa03ad2eSChaoren Lin }); 2849511e5cdcSTodd Fiala } 2850af245d11STodd Fiala 2851af245d11STodd Fiala Error 2852af245d11STodd Fiala NativeProcessLinux::Resume (const ResumeActionList &resume_actions) 2853af245d11STodd Fiala { 2854af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); 2855af245d11STodd Fiala if (log) 2856af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s called: pid %" PRIu64, __FUNCTION__, GetID ()); 2857af245d11STodd Fiala 285803f12d6bSChaoren Lin lldb::tid_t deferred_signal_tid = LLDB_INVALID_THREAD_ID; 285903f12d6bSChaoren Lin lldb::tid_t deferred_signal_skip_tid = LLDB_INVALID_THREAD_ID; 2860ae29d395SChaoren Lin int deferred_signo = 0; 2861ae29d395SChaoren Lin NativeThreadProtocolSP deferred_signal_thread_sp; 286286fd8e45SChaoren Lin bool stepping = false; 2863af245d11STodd Fiala 2864af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 28655830aa75STamas Berghammer 2866af245d11STodd Fiala for (auto thread_sp : m_threads) 2867af245d11STodd Fiala { 2868af245d11STodd Fiala assert (thread_sp && "thread list should not contain NULL threads"); 2869af245d11STodd Fiala 2870af245d11STodd Fiala const ResumeAction *const action = resume_actions.GetActionForThread (thread_sp->GetID (), true); 28716a196ce6SChaoren Lin 28726a196ce6SChaoren Lin if (action == nullptr) 28736a196ce6SChaoren Lin { 28746a196ce6SChaoren Lin if (log) 28756a196ce6SChaoren Lin log->Printf ("NativeProcessLinux::%s no action specified for pid %" PRIu64 " tid %" PRIu64, 28766a196ce6SChaoren Lin __FUNCTION__, GetID (), thread_sp->GetID ()); 28776a196ce6SChaoren Lin continue; 28786a196ce6SChaoren Lin } 2879af245d11STodd Fiala 2880af245d11STodd Fiala if (log) 2881af245d11STodd Fiala { 2882af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s processing resume action state %s for pid %" PRIu64 " tid %" PRIu64, 2883af245d11STodd Fiala __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); 2884af245d11STodd Fiala } 2885af245d11STodd Fiala 2886af245d11STodd Fiala switch (action->state) 2887af245d11STodd Fiala { 2888af245d11STodd Fiala case eStateRunning: 2889fa03ad2eSChaoren Lin { 2890af245d11STodd Fiala // Run the thread, possibly feeding it the signal. 2891fa03ad2eSChaoren Lin const int signo = action->signal; 2892b8af31d4SChaoren Lin m_coordinator_up->RequestThreadResumeAsNeeded (thread_sp->GetID (), 289386fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 2894af245d11STodd Fiala { 2895cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetRunning (); 2896fa03ad2eSChaoren Lin // Pass this signal number on to the inferior to handle. 28975830aa75STamas Berghammer const auto resume_result = Resume (tid_to_resume, (signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER); 28985830aa75STamas Berghammer if (resume_result.Success()) 28995830aa75STamas Berghammer SetState(eStateRunning, true); 29005830aa75STamas Berghammer return resume_result; 2901fa03ad2eSChaoren Lin }, 2902fa03ad2eSChaoren Lin CoordinatorErrorHandler); 2903af245d11STodd Fiala break; 2904fa03ad2eSChaoren Lin } 2905af245d11STodd Fiala 2906af245d11STodd Fiala case eStateStepping: 2907af245d11STodd Fiala { 2908ae29d395SChaoren Lin // Request the step. 2909ae29d395SChaoren Lin const int signo = action->signal; 2910ae29d395SChaoren Lin m_coordinator_up->RequestThreadResume (thread_sp->GetID (), 291186fd8e45SChaoren Lin [=](lldb::tid_t tid_to_step, bool supress_signal) 2912af245d11STodd Fiala { 2913cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (thread_sp)->SetStepping (); 291437c768caSChaoren Lin const auto step_result = SingleStep (tid_to_step,(signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER); 291537c768caSChaoren Lin assert (step_result.Success() && "SingleStep() failed"); 29165830aa75STamas Berghammer if (step_result.Success()) 29175830aa75STamas Berghammer SetState(eStateStepping, true); 291837c768caSChaoren Lin return step_result; 2919ae29d395SChaoren Lin }, 2920ae29d395SChaoren Lin CoordinatorErrorHandler); 292186fd8e45SChaoren Lin stepping = true; 2922af245d11STodd Fiala break; 2923ae29d395SChaoren Lin } 2924af245d11STodd Fiala 2925af245d11STodd Fiala case eStateSuspended: 2926af245d11STodd Fiala case eStateStopped: 2927ae29d395SChaoren Lin // if we haven't chosen a deferred signal tid yet, use this one. 2928ae29d395SChaoren Lin if (deferred_signal_tid == LLDB_INVALID_THREAD_ID) 2929ae29d395SChaoren Lin { 2930ae29d395SChaoren Lin deferred_signal_tid = thread_sp->GetID (); 2931ae29d395SChaoren Lin deferred_signal_thread_sp = thread_sp; 2932ae29d395SChaoren Lin deferred_signo = SIGSTOP; 2933ae29d395SChaoren Lin } 2934af245d11STodd Fiala break; 2935af245d11STodd Fiala 2936af245d11STodd Fiala default: 2937af245d11STodd Fiala return Error ("NativeProcessLinux::%s (): unexpected state %s specified for pid %" PRIu64 ", tid %" PRIu64, 2938af245d11STodd Fiala __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); 2939af245d11STodd Fiala } 2940af245d11STodd Fiala } 2941af245d11STodd Fiala 2942fa03ad2eSChaoren Lin // If we had any thread stopping, then do a deferred notification of the chosen stop thread id and signal 2943fa03ad2eSChaoren Lin // after all other running threads have stopped. 294486fd8e45SChaoren Lin // If there is a stepping thread involved we'll be eventually stopped by SIGTRAP trace signal. 294586fd8e45SChaoren Lin if (deferred_signal_tid != LLDB_INVALID_THREAD_ID && !stepping) 2946fa03ad2eSChaoren Lin { 294703f12d6bSChaoren Lin CallAfterRunningThreadsStopWithSkipTID (deferred_signal_tid, 294803f12d6bSChaoren Lin deferred_signal_skip_tid, 2949fa03ad2eSChaoren Lin [=](lldb::tid_t deferred_notification_tid) 2950fa03ad2eSChaoren Lin { 2951ae29d395SChaoren Lin // Set the signal thread to the current thread. 2952fa03ad2eSChaoren Lin SetCurrentThreadID (deferred_notification_tid); 2953ae29d395SChaoren Lin 2954ae29d395SChaoren Lin // Set the thread state as stopped by the deferred signo. 2955cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (deferred_signal_thread_sp)->SetStoppedBySignal (deferred_signo); 2956ae29d395SChaoren Lin 2957ae29d395SChaoren Lin // Tell the process delegate that the process is in a stopped state. 2958fa03ad2eSChaoren Lin SetState (StateType::eStateStopped, true); 2959fa03ad2eSChaoren Lin }); 2960af245d11STodd Fiala } 2961af245d11STodd Fiala 29625830aa75STamas Berghammer return Error(); 2963af245d11STodd Fiala } 2964af245d11STodd Fiala 2965af245d11STodd Fiala Error 2966af245d11STodd Fiala NativeProcessLinux::Halt () 2967af245d11STodd Fiala { 2968af245d11STodd Fiala Error error; 2969af245d11STodd Fiala 2970af245d11STodd Fiala if (kill (GetID (), SIGSTOP) != 0) 2971af245d11STodd Fiala error.SetErrorToErrno (); 2972af245d11STodd Fiala 2973af245d11STodd Fiala return error; 2974af245d11STodd Fiala } 2975af245d11STodd Fiala 2976af245d11STodd Fiala Error 2977af245d11STodd Fiala NativeProcessLinux::Detach () 2978af245d11STodd Fiala { 2979af245d11STodd Fiala Error error; 2980af245d11STodd Fiala 2981af245d11STodd Fiala // Tell ptrace to detach from the process. 2982af245d11STodd Fiala if (GetID () != LLDB_INVALID_PROCESS_ID) 2983af245d11STodd Fiala error = Detach (GetID ()); 2984af245d11STodd Fiala 2985af245d11STodd Fiala // Stop monitoring the inferior. 2986af245d11STodd Fiala StopMonitor (); 2987af245d11STodd Fiala 2988af245d11STodd Fiala // No error. 2989af245d11STodd Fiala return error; 2990af245d11STodd Fiala } 2991af245d11STodd Fiala 2992af245d11STodd Fiala Error 2993af245d11STodd Fiala NativeProcessLinux::Signal (int signo) 2994af245d11STodd Fiala { 2995af245d11STodd Fiala Error error; 2996af245d11STodd Fiala 2997af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2998af245d11STodd Fiala if (log) 2999af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s: sending signal %d (%s) to pid %" PRIu64, 3000af245d11STodd Fiala __FUNCTION__, signo, GetUnixSignals ().GetSignalAsCString (signo), GetID ()); 3001af245d11STodd Fiala 3002af245d11STodd Fiala if (kill(GetID(), signo)) 3003af245d11STodd Fiala error.SetErrorToErrno(); 3004af245d11STodd Fiala 3005af245d11STodd Fiala return error; 3006af245d11STodd Fiala } 3007af245d11STodd Fiala 3008af245d11STodd Fiala Error 3009e9547b80SChaoren Lin NativeProcessLinux::Interrupt () 3010e9547b80SChaoren Lin { 3011e9547b80SChaoren Lin // Pick a running thread (or if none, a not-dead stopped thread) as 3012e9547b80SChaoren Lin // the chosen thread that will be the stop-reason thread. 3013e9547b80SChaoren Lin Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3014e9547b80SChaoren Lin 3015e9547b80SChaoren Lin NativeThreadProtocolSP running_thread_sp; 3016e9547b80SChaoren Lin NativeThreadProtocolSP stopped_thread_sp; 3017e9547b80SChaoren Lin 3018e9547b80SChaoren Lin if (log) 3019e9547b80SChaoren Lin log->Printf ("NativeProcessLinux::%s selecting running thread for interrupt target", __FUNCTION__); 3020e9547b80SChaoren Lin 30215830aa75STamas Berghammer Mutex::Locker locker (m_threads_mutex); 30225830aa75STamas Berghammer 3023e9547b80SChaoren Lin for (auto thread_sp : m_threads) 3024e9547b80SChaoren Lin { 3025e9547b80SChaoren Lin // The thread shouldn't be null but lets just cover that here. 3026e9547b80SChaoren Lin if (!thread_sp) 3027e9547b80SChaoren Lin continue; 3028e9547b80SChaoren Lin 3029e9547b80SChaoren Lin // If we have a running or stepping thread, we'll call that the 3030e9547b80SChaoren Lin // target of the interrupt. 3031e9547b80SChaoren Lin const auto thread_state = thread_sp->GetState (); 3032e9547b80SChaoren Lin if (thread_state == eStateRunning || 3033e9547b80SChaoren Lin thread_state == eStateStepping) 3034e9547b80SChaoren Lin { 3035e9547b80SChaoren Lin running_thread_sp = thread_sp; 3036e9547b80SChaoren Lin break; 3037e9547b80SChaoren Lin } 3038e9547b80SChaoren Lin else if (!stopped_thread_sp && StateIsStoppedState (thread_state, true)) 3039e9547b80SChaoren Lin { 3040e9547b80SChaoren Lin // Remember the first non-dead stopped thread. We'll use that as a backup if there are no running threads. 3041e9547b80SChaoren Lin stopped_thread_sp = thread_sp; 3042e9547b80SChaoren Lin } 3043e9547b80SChaoren Lin } 3044e9547b80SChaoren Lin 3045e9547b80SChaoren Lin if (!running_thread_sp && !stopped_thread_sp) 3046e9547b80SChaoren Lin { 30475830aa75STamas Berghammer Error error("found no running/stepping or live stopped threads as target for interrupt"); 3048e9547b80SChaoren Lin if (log) 3049e9547b80SChaoren Lin log->Printf ("NativeProcessLinux::%s skipping due to error: %s", __FUNCTION__, error.AsCString ()); 30505830aa75STamas Berghammer 3051e9547b80SChaoren Lin return error; 3052e9547b80SChaoren Lin } 3053e9547b80SChaoren Lin 3054e9547b80SChaoren Lin NativeThreadProtocolSP deferred_signal_thread_sp = running_thread_sp ? running_thread_sp : stopped_thread_sp; 3055e9547b80SChaoren Lin 3056e9547b80SChaoren Lin if (log) 3057e9547b80SChaoren Lin log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " %s tid %" PRIu64 " chosen for interrupt target", 3058e9547b80SChaoren Lin __FUNCTION__, 3059e9547b80SChaoren Lin GetID (), 3060e9547b80SChaoren Lin running_thread_sp ? "running" : "stopped", 3061e9547b80SChaoren Lin deferred_signal_thread_sp->GetID ()); 3062e9547b80SChaoren Lin 3063e9547b80SChaoren Lin CallAfterRunningThreadsStop (deferred_signal_thread_sp->GetID (), 3064e9547b80SChaoren Lin [=](lldb::tid_t deferred_notification_tid) 3065e9547b80SChaoren Lin { 3066e9547b80SChaoren Lin // Set the signal thread to the current thread. 3067e9547b80SChaoren Lin SetCurrentThreadID (deferred_notification_tid); 3068e9547b80SChaoren Lin 3069e9547b80SChaoren Lin // Set the thread state as stopped by the deferred signo. 3070cb84eebbSTamas Berghammer std::static_pointer_cast<NativeThreadLinux> (deferred_signal_thread_sp)->SetStoppedBySignal (SIGSTOP); 3071e9547b80SChaoren Lin 3072e9547b80SChaoren Lin // Tell the process delegate that the process is in a stopped state. 3073e9547b80SChaoren Lin SetState (StateType::eStateStopped, true); 3074e9547b80SChaoren Lin }); 30755830aa75STamas Berghammer return Error(); 3076e9547b80SChaoren Lin } 3077e9547b80SChaoren Lin 3078e9547b80SChaoren Lin Error 3079af245d11STodd Fiala NativeProcessLinux::Kill () 3080af245d11STodd Fiala { 3081af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3082af245d11STodd Fiala if (log) 3083af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s called for PID %" PRIu64, __FUNCTION__, GetID ()); 3084af245d11STodd Fiala 3085af245d11STodd Fiala Error error; 3086af245d11STodd Fiala 3087af245d11STodd Fiala switch (m_state) 3088af245d11STodd Fiala { 3089af245d11STodd Fiala case StateType::eStateInvalid: 3090af245d11STodd Fiala case StateType::eStateExited: 3091af245d11STodd Fiala case StateType::eStateCrashed: 3092af245d11STodd Fiala case StateType::eStateDetached: 3093af245d11STodd Fiala case StateType::eStateUnloaded: 3094af245d11STodd Fiala // Nothing to do - the process is already dead. 3095af245d11STodd Fiala if (log) 3096af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s ignored for PID %" PRIu64 " due to current state: %s", __FUNCTION__, GetID (), StateAsCString (m_state)); 3097af245d11STodd Fiala return error; 3098af245d11STodd Fiala 3099af245d11STodd Fiala case StateType::eStateConnected: 3100af245d11STodd Fiala case StateType::eStateAttaching: 3101af245d11STodd Fiala case StateType::eStateLaunching: 3102af245d11STodd Fiala case StateType::eStateStopped: 3103af245d11STodd Fiala case StateType::eStateRunning: 3104af245d11STodd Fiala case StateType::eStateStepping: 3105af245d11STodd Fiala case StateType::eStateSuspended: 3106af245d11STodd Fiala // We can try to kill a process in these states. 3107af245d11STodd Fiala break; 3108af245d11STodd Fiala } 3109af245d11STodd Fiala 3110af245d11STodd Fiala if (kill (GetID (), SIGKILL) != 0) 3111af245d11STodd Fiala { 3112af245d11STodd Fiala error.SetErrorToErrno (); 3113af245d11STodd Fiala return error; 3114af245d11STodd Fiala } 3115af245d11STodd Fiala 3116af245d11STodd Fiala return error; 3117af245d11STodd Fiala } 3118af245d11STodd Fiala 3119af245d11STodd Fiala static Error 3120af245d11STodd Fiala ParseMemoryRegionInfoFromProcMapsLine (const std::string &maps_line, MemoryRegionInfo &memory_region_info) 3121af245d11STodd Fiala { 3122af245d11STodd Fiala memory_region_info.Clear(); 3123af245d11STodd Fiala 3124af245d11STodd Fiala StringExtractor line_extractor (maps_line.c_str ()); 3125af245d11STodd Fiala 3126af245d11STodd Fiala // Format: {address_start_hex}-{address_end_hex} perms offset dev inode pathname 3127af245d11STodd Fiala // perms: rwxp (letter is present if set, '-' if not, final character is p=private, s=shared). 3128af245d11STodd Fiala 3129af245d11STodd Fiala // Parse out the starting address 3130af245d11STodd Fiala lldb::addr_t start_address = line_extractor.GetHexMaxU64 (false, 0); 3131af245d11STodd Fiala 3132af245d11STodd Fiala // Parse out hyphen separating start and end address from range. 3133af245d11STodd Fiala if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != '-')) 3134af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing dash between address range"); 3135af245d11STodd Fiala 3136af245d11STodd Fiala // Parse out the ending address 3137af245d11STodd Fiala lldb::addr_t end_address = line_extractor.GetHexMaxU64 (false, start_address); 3138af245d11STodd Fiala 3139af245d11STodd Fiala // Parse out the space after the address. 3140af245d11STodd Fiala if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != ' ')) 3141af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing space after range"); 3142af245d11STodd Fiala 3143af245d11STodd Fiala // Save the range. 3144af245d11STodd Fiala memory_region_info.GetRange ().SetRangeBase (start_address); 3145af245d11STodd Fiala memory_region_info.GetRange ().SetRangeEnd (end_address); 3146af245d11STodd Fiala 3147af245d11STodd Fiala // Parse out each permission entry. 3148af245d11STodd Fiala if (line_extractor.GetBytesLeft () < 4) 3149af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing some portion of permissions"); 3150af245d11STodd Fiala 3151af245d11STodd Fiala // Handle read permission. 3152af245d11STodd Fiala const char read_perm_char = line_extractor.GetChar (); 3153af245d11STodd Fiala if (read_perm_char == 'r') 3154af245d11STodd Fiala memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eYes); 3155af245d11STodd Fiala else 3156af245d11STodd Fiala { 3157af245d11STodd Fiala assert ( (read_perm_char == '-') && "unexpected /proc/{pid}/maps read permission char" ); 3158af245d11STodd Fiala memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); 3159af245d11STodd Fiala } 3160af245d11STodd Fiala 3161af245d11STodd Fiala // Handle write permission. 3162af245d11STodd Fiala const char write_perm_char = line_extractor.GetChar (); 3163af245d11STodd Fiala if (write_perm_char == 'w') 3164af245d11STodd Fiala memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eYes); 3165af245d11STodd Fiala else 3166af245d11STodd Fiala { 3167af245d11STodd Fiala assert ( (write_perm_char == '-') && "unexpected /proc/{pid}/maps write permission char" ); 3168af245d11STodd Fiala memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); 3169af245d11STodd Fiala } 3170af245d11STodd Fiala 3171af245d11STodd Fiala // Handle execute permission. 3172af245d11STodd Fiala const char exec_perm_char = line_extractor.GetChar (); 3173af245d11STodd Fiala if (exec_perm_char == 'x') 3174af245d11STodd Fiala memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eYes); 3175af245d11STodd Fiala else 3176af245d11STodd Fiala { 3177af245d11STodd Fiala assert ( (exec_perm_char == '-') && "unexpected /proc/{pid}/maps exec permission char" ); 3178af245d11STodd Fiala memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); 3179af245d11STodd Fiala } 3180af245d11STodd Fiala 3181af245d11STodd Fiala return Error (); 3182af245d11STodd Fiala } 3183af245d11STodd Fiala 3184af245d11STodd Fiala Error 3185af245d11STodd Fiala NativeProcessLinux::GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) 3186af245d11STodd Fiala { 3187af245d11STodd Fiala // FIXME review that the final memory region returned extends to the end of the virtual address space, 3188af245d11STodd Fiala // with no perms if it is not mapped. 3189af245d11STodd Fiala 3190af245d11STodd Fiala // Use an approach that reads memory regions from /proc/{pid}/maps. 3191af245d11STodd Fiala // Assume proc maps entries are in ascending order. 3192af245d11STodd Fiala // FIXME assert if we find differently. 3193af245d11STodd Fiala Mutex::Locker locker (m_mem_region_cache_mutex); 3194af245d11STodd Fiala 3195af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3196af245d11STodd Fiala Error error; 3197af245d11STodd Fiala 3198af245d11STodd Fiala if (m_supports_mem_region == LazyBool::eLazyBoolNo) 3199af245d11STodd Fiala { 3200af245d11STodd Fiala // We're done. 3201af245d11STodd Fiala error.SetErrorString ("unsupported"); 3202af245d11STodd Fiala return error; 3203af245d11STodd Fiala } 3204af245d11STodd Fiala 3205af245d11STodd Fiala // If our cache is empty, pull the latest. There should always be at least one memory region 3206af245d11STodd Fiala // if memory region handling is supported. 3207af245d11STodd Fiala if (m_mem_region_cache.empty ()) 3208af245d11STodd Fiala { 3209af245d11STodd Fiala error = ProcFileReader::ProcessLineByLine (GetID (), "maps", 3210af245d11STodd Fiala [&] (const std::string &line) -> bool 3211af245d11STodd Fiala { 3212af245d11STodd Fiala MemoryRegionInfo info; 3213af245d11STodd Fiala const Error parse_error = ParseMemoryRegionInfoFromProcMapsLine (line, info); 3214af245d11STodd Fiala if (parse_error.Success ()) 3215af245d11STodd Fiala { 3216af245d11STodd Fiala m_mem_region_cache.push_back (info); 3217af245d11STodd Fiala return true; 3218af245d11STodd Fiala } 3219af245d11STodd Fiala else 3220af245d11STodd Fiala { 3221af245d11STodd Fiala if (log) 3222af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to parse proc maps line '%s': %s", __FUNCTION__, line.c_str (), error.AsCString ()); 3223af245d11STodd Fiala return false; 3224af245d11STodd Fiala } 3225af245d11STodd Fiala }); 3226af245d11STodd Fiala 3227af245d11STodd Fiala // If we had an error, we'll mark unsupported. 3228af245d11STodd Fiala if (error.Fail ()) 3229af245d11STodd Fiala { 3230af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolNo; 3231af245d11STodd Fiala return error; 3232af245d11STodd Fiala } 3233af245d11STodd Fiala else if (m_mem_region_cache.empty ()) 3234af245d11STodd Fiala { 3235af245d11STodd Fiala // No entries after attempting to read them. This shouldn't happen if /proc/{pid}/maps 3236af245d11STodd Fiala // is supported. Assume we don't support map entries via procfs. 3237af245d11STodd Fiala if (log) 3238af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to find any procfs maps entries, assuming no support for memory region metadata retrieval", __FUNCTION__); 3239af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolNo; 3240af245d11STodd Fiala error.SetErrorString ("not supported"); 3241af245d11STodd Fiala return error; 3242af245d11STodd Fiala } 3243af245d11STodd Fiala 3244af245d11STodd Fiala if (log) 3245af245d11STodd 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 ()); 3246af245d11STodd Fiala 3247af245d11STodd Fiala // We support memory retrieval, remember that. 3248af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolYes; 3249af245d11STodd Fiala } 3250af245d11STodd Fiala else 3251af245d11STodd Fiala { 3252af245d11STodd Fiala if (log) 3253af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s reusing %" PRIu64 " cached memory region entries", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); 3254af245d11STodd Fiala } 3255af245d11STodd Fiala 3256af245d11STodd Fiala lldb::addr_t prev_base_address = 0; 3257af245d11STodd Fiala 3258af245d11STodd Fiala // FIXME start by finding the last region that is <= target address using binary search. Data is sorted. 3259af245d11STodd Fiala // There can be a ton of regions on pthreads apps with lots of threads. 3260af245d11STodd Fiala for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end (); ++it) 3261af245d11STodd Fiala { 3262af245d11STodd Fiala MemoryRegionInfo &proc_entry_info = *it; 3263af245d11STodd Fiala 3264af245d11STodd Fiala // Sanity check assumption that /proc/{pid}/maps entries are ascending. 3265af245d11STodd Fiala assert ((proc_entry_info.GetRange ().GetRangeBase () >= prev_base_address) && "descending /proc/pid/maps entries detected, unexpected"); 3266af245d11STodd Fiala prev_base_address = proc_entry_info.GetRange ().GetRangeBase (); 3267af245d11STodd Fiala 3268af245d11STodd Fiala // If the target address comes before this entry, indicate distance to next region. 3269af245d11STodd Fiala if (load_addr < proc_entry_info.GetRange ().GetRangeBase ()) 3270af245d11STodd Fiala { 3271af245d11STodd Fiala range_info.GetRange ().SetRangeBase (load_addr); 3272af245d11STodd Fiala range_info.GetRange ().SetByteSize (proc_entry_info.GetRange ().GetRangeBase () - load_addr); 3273af245d11STodd Fiala range_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); 3274af245d11STodd Fiala range_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); 3275af245d11STodd Fiala range_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); 3276af245d11STodd Fiala 3277af245d11STodd Fiala return error; 3278af245d11STodd Fiala } 3279af245d11STodd Fiala else if (proc_entry_info.GetRange ().Contains (load_addr)) 3280af245d11STodd Fiala { 3281af245d11STodd Fiala // The target address is within the memory region we're processing here. 3282af245d11STodd Fiala range_info = proc_entry_info; 3283af245d11STodd Fiala return error; 3284af245d11STodd Fiala } 3285af245d11STodd Fiala 3286af245d11STodd Fiala // The target memory address comes somewhere after the region we just parsed. 3287af245d11STodd Fiala } 3288af245d11STodd Fiala 3289af245d11STodd Fiala // If we made it here, we didn't find an entry that contained the given address. 3290af245d11STodd Fiala error.SetErrorString ("address comes after final region"); 3291af245d11STodd Fiala 3292af245d11STodd Fiala if (log) 3293af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to find map entry for address 0x%" PRIx64 ": %s", __FUNCTION__, load_addr, error.AsCString ()); 3294af245d11STodd Fiala 3295af245d11STodd Fiala return error; 3296af245d11STodd Fiala } 3297af245d11STodd Fiala 3298af245d11STodd Fiala void 3299af245d11STodd Fiala NativeProcessLinux::DoStopIDBumped (uint32_t newBumpId) 3300af245d11STodd Fiala { 3301af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3302af245d11STodd Fiala if (log) 3303af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(newBumpId=%" PRIu32 ") called", __FUNCTION__, newBumpId); 3304af245d11STodd Fiala 3305af245d11STodd Fiala { 3306af245d11STodd Fiala Mutex::Locker locker (m_mem_region_cache_mutex); 3307af245d11STodd Fiala if (log) 3308af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s clearing %" PRIu64 " entries from the cache", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); 3309af245d11STodd Fiala m_mem_region_cache.clear (); 3310af245d11STodd Fiala } 3311af245d11STodd Fiala } 3312af245d11STodd Fiala 3313af245d11STodd Fiala Error 3314af245d11STodd Fiala NativeProcessLinux::AllocateMemory ( 3315af245d11STodd Fiala lldb::addr_t size, 3316af245d11STodd Fiala uint32_t permissions, 3317af245d11STodd Fiala lldb::addr_t &addr) 3318af245d11STodd Fiala { 3319af245d11STodd Fiala // FIXME implementing this requires the equivalent of 3320af245d11STodd Fiala // InferiorCallPOSIX::InferiorCallMmap, which depends on 3321af245d11STodd Fiala // functional ThreadPlans working with Native*Protocol. 3322af245d11STodd Fiala #if 1 3323af245d11STodd Fiala return Error ("not implemented yet"); 3324af245d11STodd Fiala #else 3325af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 3326af245d11STodd Fiala 3327af245d11STodd Fiala unsigned prot = 0; 3328af245d11STodd Fiala if (permissions & lldb::ePermissionsReadable) 3329af245d11STodd Fiala prot |= eMmapProtRead; 3330af245d11STodd Fiala if (permissions & lldb::ePermissionsWritable) 3331af245d11STodd Fiala prot |= eMmapProtWrite; 3332af245d11STodd Fiala if (permissions & lldb::ePermissionsExecutable) 3333af245d11STodd Fiala prot |= eMmapProtExec; 3334af245d11STodd Fiala 3335af245d11STodd Fiala // TODO implement this directly in NativeProcessLinux 3336af245d11STodd Fiala // (and lift to NativeProcessPOSIX if/when that class is 3337af245d11STodd Fiala // refactored out). 3338af245d11STodd Fiala if (InferiorCallMmap(this, addr, 0, size, prot, 3339af245d11STodd Fiala eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { 3340af245d11STodd Fiala m_addr_to_mmap_size[addr] = size; 3341af245d11STodd Fiala return Error (); 3342af245d11STodd Fiala } else { 3343af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 3344af245d11STodd Fiala return Error("unable to allocate %" PRIu64 " bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions)); 3345af245d11STodd Fiala } 3346af245d11STodd Fiala #endif 3347af245d11STodd Fiala } 3348af245d11STodd Fiala 3349af245d11STodd Fiala Error 3350af245d11STodd Fiala NativeProcessLinux::DeallocateMemory (lldb::addr_t addr) 3351af245d11STodd Fiala { 3352af245d11STodd Fiala // FIXME see comments in AllocateMemory - required lower-level 3353af245d11STodd Fiala // bits not in place yet (ThreadPlans) 3354af245d11STodd Fiala return Error ("not implemented"); 3355af245d11STodd Fiala } 3356af245d11STodd Fiala 3357af245d11STodd Fiala lldb::addr_t 3358af245d11STodd Fiala NativeProcessLinux::GetSharedLibraryInfoAddress () 3359af245d11STodd Fiala { 3360af245d11STodd Fiala #if 1 3361af245d11STodd Fiala // punt on this for now 3362af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3363af245d11STodd Fiala #else 3364af245d11STodd Fiala // Return the image info address for the exe module 3365af245d11STodd Fiala #if 1 3366af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3367af245d11STodd Fiala 3368af245d11STodd Fiala ModuleSP module_sp; 3369af245d11STodd Fiala Error error = GetExeModuleSP (module_sp); 3370af245d11STodd Fiala if (error.Fail ()) 3371af245d11STodd Fiala { 3372af245d11STodd Fiala if (log) 3373af245d11STodd Fiala log->Warning ("NativeProcessLinux::%s failed to retrieve exe module: %s", __FUNCTION__, error.AsCString ()); 3374af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3375af245d11STodd Fiala } 3376af245d11STodd Fiala 3377af245d11STodd Fiala if (module_sp == nullptr) 3378af245d11STodd Fiala { 3379af245d11STodd Fiala if (log) 3380af245d11STodd Fiala log->Warning ("NativeProcessLinux::%s exe module returned was NULL", __FUNCTION__); 3381af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3382af245d11STodd Fiala } 3383af245d11STodd Fiala 3384af245d11STodd Fiala ObjectFileSP object_file_sp = module_sp->GetObjectFile (); 3385af245d11STodd Fiala if (object_file_sp == nullptr) 3386af245d11STodd Fiala { 3387af245d11STodd Fiala if (log) 3388af245d11STodd Fiala log->Warning ("NativeProcessLinux::%s exe module returned a NULL object file", __FUNCTION__); 3389af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3390af245d11STodd Fiala } 3391af245d11STodd Fiala 3392af245d11STodd Fiala return obj_file_sp->GetImageInfoAddress(); 3393af245d11STodd Fiala #else 3394af245d11STodd Fiala Target *target = &GetTarget(); 3395af245d11STodd Fiala ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile(); 3396af245d11STodd Fiala Address addr = obj_file->GetImageInfoAddress(target); 3397af245d11STodd Fiala 3398af245d11STodd Fiala if (addr.IsValid()) 3399af245d11STodd Fiala return addr.GetLoadAddress(target); 3400af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3401af245d11STodd Fiala #endif 3402af245d11STodd Fiala #endif // punt on this for now 3403af245d11STodd Fiala } 3404af245d11STodd Fiala 3405af245d11STodd Fiala size_t 3406af245d11STodd Fiala NativeProcessLinux::UpdateThreads () 3407af245d11STodd Fiala { 3408af245d11STodd Fiala // The NativeProcessLinux monitoring threads are always up to date 3409af245d11STodd Fiala // with respect to thread state and they keep the thread list 3410af245d11STodd Fiala // populated properly. All this method needs to do is return the 3411af245d11STodd Fiala // thread count. 3412af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 3413af245d11STodd Fiala return m_threads.size (); 3414af245d11STodd Fiala } 3415af245d11STodd Fiala 3416af245d11STodd Fiala bool 3417af245d11STodd Fiala NativeProcessLinux::GetArchitecture (ArchSpec &arch) const 3418af245d11STodd Fiala { 3419af245d11STodd Fiala arch = m_arch; 3420af245d11STodd Fiala return true; 3421af245d11STodd Fiala } 3422af245d11STodd Fiala 3423af245d11STodd Fiala Error 342463c8be95STamas Berghammer NativeProcessLinux::GetSoftwareBreakpointPCOffset (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size) 3425af245d11STodd Fiala { 3426af245d11STodd Fiala // FIXME put this behind a breakpoint protocol class that can be 3427af245d11STodd Fiala // set per architecture. Need ARM, MIPS support here. 34282afc5966STodd Fiala static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; 3429af245d11STodd Fiala static const uint8_t g_i386_opcode [] = { 0xCC }; 3430af245d11STodd Fiala 3431af245d11STodd Fiala switch (m_arch.GetMachine ()) 3432af245d11STodd Fiala { 34332afc5966STodd Fiala case llvm::Triple::aarch64: 34342afc5966STodd Fiala actual_opcode_size = static_cast<uint32_t> (sizeof(g_aarch64_opcode)); 34352afc5966STodd Fiala return Error (); 34362afc5966STodd Fiala 343763c8be95STamas Berghammer case llvm::Triple::arm: 343863c8be95STamas Berghammer actual_opcode_size = 0; // On arm the PC don't get updated for breakpoint hits 343963c8be95STamas Berghammer return Error (); 344063c8be95STamas Berghammer 3441af245d11STodd Fiala case llvm::Triple::x86: 3442af245d11STodd Fiala case llvm::Triple::x86_64: 3443af245d11STodd Fiala actual_opcode_size = static_cast<uint32_t> (sizeof(g_i386_opcode)); 3444af245d11STodd Fiala return Error (); 3445af245d11STodd Fiala 3446af245d11STodd Fiala default: 3447af245d11STodd Fiala assert(false && "CPU type not supported!"); 3448af245d11STodd Fiala return Error ("CPU type not supported"); 3449af245d11STodd Fiala } 3450af245d11STodd Fiala } 3451af245d11STodd Fiala 3452af245d11STodd Fiala Error 3453af245d11STodd Fiala NativeProcessLinux::SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) 3454af245d11STodd Fiala { 3455af245d11STodd Fiala if (hardware) 3456af245d11STodd Fiala return Error ("NativeProcessLinux does not support hardware breakpoints"); 3457af245d11STodd Fiala else 3458af245d11STodd Fiala return SetSoftwareBreakpoint (addr, size); 3459af245d11STodd Fiala } 3460af245d11STodd Fiala 3461af245d11STodd Fiala Error 346263c8be95STamas Berghammer NativeProcessLinux::GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, 346363c8be95STamas Berghammer size_t &actual_opcode_size, 346463c8be95STamas Berghammer const uint8_t *&trap_opcode_bytes) 3465af245d11STodd Fiala { 346663c8be95STamas Berghammer // FIXME put this behind a breakpoint protocol class that can be set per 346763c8be95STamas Berghammer // architecture. Need MIPS support here. 34682afc5966STodd Fiala static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; 346963c8be95STamas Berghammer // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the 347063c8be95STamas Berghammer // linux kernel does otherwise. 347163c8be95STamas Berghammer static const uint8_t g_arm_breakpoint_opcode[] = { 0xf0, 0x01, 0xf0, 0xe7 }; 3472af245d11STodd Fiala static const uint8_t g_i386_opcode [] = { 0xCC }; 34733df471c3SMohit K. Bhakkad static const uint8_t g_mips64_opcode[] = { 0x00, 0x00, 0x00, 0x0d }; 34742c2acf96SMohit K. Bhakkad static const uint8_t g_mips64el_opcode[] = { 0x0d, 0x00, 0x00, 0x00 }; 347563c8be95STamas Berghammer static const uint8_t g_thumb_breakpoint_opcode[] = { 0x01, 0xde }; 3476af245d11STodd Fiala 3477af245d11STodd Fiala switch (m_arch.GetMachine ()) 3478af245d11STodd Fiala { 34792afc5966STodd Fiala case llvm::Triple::aarch64: 34802afc5966STodd Fiala trap_opcode_bytes = g_aarch64_opcode; 34812afc5966STodd Fiala actual_opcode_size = sizeof(g_aarch64_opcode); 34822afc5966STodd Fiala return Error (); 34832afc5966STodd Fiala 348463c8be95STamas Berghammer case llvm::Triple::arm: 348563c8be95STamas Berghammer switch (trap_opcode_size_hint) 348663c8be95STamas Berghammer { 348763c8be95STamas Berghammer case 2: 348863c8be95STamas Berghammer trap_opcode_bytes = g_thumb_breakpoint_opcode; 348963c8be95STamas Berghammer actual_opcode_size = sizeof(g_thumb_breakpoint_opcode); 349063c8be95STamas Berghammer return Error (); 349163c8be95STamas Berghammer case 4: 349263c8be95STamas Berghammer trap_opcode_bytes = g_arm_breakpoint_opcode; 349363c8be95STamas Berghammer actual_opcode_size = sizeof(g_arm_breakpoint_opcode); 349463c8be95STamas Berghammer return Error (); 349563c8be95STamas Berghammer default: 349663c8be95STamas Berghammer assert(false && "Unrecognised trap opcode size hint!"); 349763c8be95STamas Berghammer return Error ("Unrecognised trap opcode size hint!"); 349863c8be95STamas Berghammer } 349963c8be95STamas Berghammer 3500af245d11STodd Fiala case llvm::Triple::x86: 3501af245d11STodd Fiala case llvm::Triple::x86_64: 3502af245d11STodd Fiala trap_opcode_bytes = g_i386_opcode; 3503af245d11STodd Fiala actual_opcode_size = sizeof(g_i386_opcode); 3504af245d11STodd Fiala return Error (); 3505af245d11STodd Fiala 35063df471c3SMohit K. Bhakkad case llvm::Triple::mips64: 35073df471c3SMohit K. Bhakkad trap_opcode_bytes = g_mips64_opcode; 35083df471c3SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64_opcode); 35093df471c3SMohit K. Bhakkad return Error (); 35103df471c3SMohit K. Bhakkad 35112c2acf96SMohit K. Bhakkad case llvm::Triple::mips64el: 35122c2acf96SMohit K. Bhakkad trap_opcode_bytes = g_mips64el_opcode; 35132c2acf96SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64el_opcode); 35142c2acf96SMohit K. Bhakkad return Error (); 35152c2acf96SMohit K. Bhakkad 3516af245d11STodd Fiala default: 3517af245d11STodd Fiala assert(false && "CPU type not supported!"); 3518af245d11STodd Fiala return Error ("CPU type not supported"); 3519af245d11STodd Fiala } 3520af245d11STodd Fiala } 3521af245d11STodd Fiala 3522af245d11STodd Fiala #if 0 3523af245d11STodd Fiala ProcessMessage::CrashReason 3524af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info) 3525af245d11STodd Fiala { 3526af245d11STodd Fiala ProcessMessage::CrashReason reason; 3527af245d11STodd Fiala assert(info->si_signo == SIGSEGV); 3528af245d11STodd Fiala 3529af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 3530af245d11STodd Fiala 3531af245d11STodd Fiala switch (info->si_code) 3532af245d11STodd Fiala { 3533af245d11STodd Fiala default: 3534af245d11STodd Fiala assert(false && "unexpected si_code for SIGSEGV"); 3535af245d11STodd Fiala break; 3536af245d11STodd Fiala case SI_KERNEL: 3537af245d11STodd Fiala // Linux will occasionally send spurious SI_KERNEL codes. 3538af245d11STodd Fiala // (this is poorly documented in sigaction) 3539af245d11STodd Fiala // One way to get this is via unaligned SIMD loads. 3540af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; // for lack of anything better 3541af245d11STodd Fiala break; 3542af245d11STodd Fiala case SEGV_MAPERR: 3543af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; 3544af245d11STodd Fiala break; 3545af245d11STodd Fiala case SEGV_ACCERR: 3546af245d11STodd Fiala reason = ProcessMessage::ePrivilegedAddress; 3547af245d11STodd Fiala break; 3548af245d11STodd Fiala } 3549af245d11STodd Fiala 3550af245d11STodd Fiala return reason; 3551af245d11STodd Fiala } 3552af245d11STodd Fiala #endif 3553af245d11STodd Fiala 3554af245d11STodd Fiala 3555af245d11STodd Fiala #if 0 3556af245d11STodd Fiala ProcessMessage::CrashReason 3557af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info) 3558af245d11STodd Fiala { 3559af245d11STodd Fiala ProcessMessage::CrashReason reason; 3560af245d11STodd Fiala assert(info->si_signo == SIGILL); 3561af245d11STodd Fiala 3562af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 3563af245d11STodd Fiala 3564af245d11STodd Fiala switch (info->si_code) 3565af245d11STodd Fiala { 3566af245d11STodd Fiala default: 3567af245d11STodd Fiala assert(false && "unexpected si_code for SIGILL"); 3568af245d11STodd Fiala break; 3569af245d11STodd Fiala case ILL_ILLOPC: 3570af245d11STodd Fiala reason = ProcessMessage::eIllegalOpcode; 3571af245d11STodd Fiala break; 3572af245d11STodd Fiala case ILL_ILLOPN: 3573af245d11STodd Fiala reason = ProcessMessage::eIllegalOperand; 3574af245d11STodd Fiala break; 3575af245d11STodd Fiala case ILL_ILLADR: 3576af245d11STodd Fiala reason = ProcessMessage::eIllegalAddressingMode; 3577af245d11STodd Fiala break; 3578af245d11STodd Fiala case ILL_ILLTRP: 3579af245d11STodd Fiala reason = ProcessMessage::eIllegalTrap; 3580af245d11STodd Fiala break; 3581af245d11STodd Fiala case ILL_PRVOPC: 3582af245d11STodd Fiala reason = ProcessMessage::ePrivilegedOpcode; 3583af245d11STodd Fiala break; 3584af245d11STodd Fiala case ILL_PRVREG: 3585af245d11STodd Fiala reason = ProcessMessage::ePrivilegedRegister; 3586af245d11STodd Fiala break; 3587af245d11STodd Fiala case ILL_COPROC: 3588af245d11STodd Fiala reason = ProcessMessage::eCoprocessorError; 3589af245d11STodd Fiala break; 3590af245d11STodd Fiala case ILL_BADSTK: 3591af245d11STodd Fiala reason = ProcessMessage::eInternalStackError; 3592af245d11STodd Fiala break; 3593af245d11STodd Fiala } 3594af245d11STodd Fiala 3595af245d11STodd Fiala return reason; 3596af245d11STodd Fiala } 3597af245d11STodd Fiala #endif 3598af245d11STodd Fiala 3599af245d11STodd Fiala #if 0 3600af245d11STodd Fiala ProcessMessage::CrashReason 3601af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info) 3602af245d11STodd Fiala { 3603af245d11STodd Fiala ProcessMessage::CrashReason reason; 3604af245d11STodd Fiala assert(info->si_signo == SIGFPE); 3605af245d11STodd Fiala 3606af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 3607af245d11STodd Fiala 3608af245d11STodd Fiala switch (info->si_code) 3609af245d11STodd Fiala { 3610af245d11STodd Fiala default: 3611af245d11STodd Fiala assert(false && "unexpected si_code for SIGFPE"); 3612af245d11STodd Fiala break; 3613af245d11STodd Fiala case FPE_INTDIV: 3614af245d11STodd Fiala reason = ProcessMessage::eIntegerDivideByZero; 3615af245d11STodd Fiala break; 3616af245d11STodd Fiala case FPE_INTOVF: 3617af245d11STodd Fiala reason = ProcessMessage::eIntegerOverflow; 3618af245d11STodd Fiala break; 3619af245d11STodd Fiala case FPE_FLTDIV: 3620af245d11STodd Fiala reason = ProcessMessage::eFloatDivideByZero; 3621af245d11STodd Fiala break; 3622af245d11STodd Fiala case FPE_FLTOVF: 3623af245d11STodd Fiala reason = ProcessMessage::eFloatOverflow; 3624af245d11STodd Fiala break; 3625af245d11STodd Fiala case FPE_FLTUND: 3626af245d11STodd Fiala reason = ProcessMessage::eFloatUnderflow; 3627af245d11STodd Fiala break; 3628af245d11STodd Fiala case FPE_FLTRES: 3629af245d11STodd Fiala reason = ProcessMessage::eFloatInexactResult; 3630af245d11STodd Fiala break; 3631af245d11STodd Fiala case FPE_FLTINV: 3632af245d11STodd Fiala reason = ProcessMessage::eFloatInvalidOperation; 3633af245d11STodd Fiala break; 3634af245d11STodd Fiala case FPE_FLTSUB: 3635af245d11STodd Fiala reason = ProcessMessage::eFloatSubscriptRange; 3636af245d11STodd Fiala break; 3637af245d11STodd Fiala } 3638af245d11STodd Fiala 3639af245d11STodd Fiala return reason; 3640af245d11STodd Fiala } 3641af245d11STodd Fiala #endif 3642af245d11STodd Fiala 3643af245d11STodd Fiala #if 0 3644af245d11STodd Fiala ProcessMessage::CrashReason 3645af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info) 3646af245d11STodd Fiala { 3647af245d11STodd Fiala ProcessMessage::CrashReason reason; 3648af245d11STodd Fiala assert(info->si_signo == SIGBUS); 3649af245d11STodd Fiala 3650af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 3651af245d11STodd Fiala 3652af245d11STodd Fiala switch (info->si_code) 3653af245d11STodd Fiala { 3654af245d11STodd Fiala default: 3655af245d11STodd Fiala assert(false && "unexpected si_code for SIGBUS"); 3656af245d11STodd Fiala break; 3657af245d11STodd Fiala case BUS_ADRALN: 3658af245d11STodd Fiala reason = ProcessMessage::eIllegalAlignment; 3659af245d11STodd Fiala break; 3660af245d11STodd Fiala case BUS_ADRERR: 3661af245d11STodd Fiala reason = ProcessMessage::eIllegalAddress; 3662af245d11STodd Fiala break; 3663af245d11STodd Fiala case BUS_OBJERR: 3664af245d11STodd Fiala reason = ProcessMessage::eHardwareError; 3665af245d11STodd Fiala break; 3666af245d11STodd Fiala } 3667af245d11STodd Fiala 3668af245d11STodd Fiala return reason; 3669af245d11STodd Fiala } 3670af245d11STodd Fiala #endif 3671af245d11STodd Fiala 3672af245d11STodd Fiala void 3673af245d11STodd Fiala NativeProcessLinux::ServeOperation(OperationArgs *args) 3674af245d11STodd Fiala { 3675af245d11STodd Fiala NativeProcessLinux *monitor = args->m_monitor; 3676af245d11STodd Fiala 3677af245d11STodd Fiala // We are finised with the arguments and are ready to go. Sync with the 3678af245d11STodd Fiala // parent thread and start serving operations on the inferior. 3679af245d11STodd Fiala sem_post(&args->m_semaphore); 3680af245d11STodd Fiala 3681af245d11STodd Fiala for(;;) 3682af245d11STodd Fiala { 3683af245d11STodd Fiala // wait for next pending operation 3684af245d11STodd Fiala if (sem_wait(&monitor->m_operation_pending)) 3685af245d11STodd Fiala { 3686af245d11STodd Fiala if (errno == EINTR) 3687af245d11STodd Fiala continue; 3688af245d11STodd Fiala assert(false && "Unexpected errno from sem_wait"); 3689af245d11STodd Fiala } 3690af245d11STodd Fiala 369143f2d971STamas Berghammer // EXIT_OPERATION used to stop the operation thread because Cancel() isn't supported on 369243f2d971STamas Berghammer // android. We don't have to send a post to the m_operation_done semaphore because in this 369343f2d971STamas Berghammer // case the synchronization is achieved by a Join() call 369443f2d971STamas Berghammer if (monitor->m_operation == EXIT_OPERATION) 36956806fdedSTamas Berghammer break; 36966806fdedSTamas Berghammer 3697cb84eebbSTamas Berghammer static_cast<Operation*>(monitor->m_operation)->Execute(monitor); 3698af245d11STodd Fiala 3699af245d11STodd Fiala // notify calling thread that operation is complete 3700af245d11STodd Fiala sem_post(&monitor->m_operation_done); 3701af245d11STodd Fiala } 3702af245d11STodd Fiala } 3703af245d11STodd Fiala 3704af245d11STodd Fiala void 3705af245d11STodd Fiala NativeProcessLinux::DoOperation(void *op) 3706af245d11STodd Fiala { 3707af245d11STodd Fiala Mutex::Locker lock(m_operation_mutex); 3708af245d11STodd Fiala 3709af245d11STodd Fiala m_operation = op; 3710af245d11STodd Fiala 3711af245d11STodd Fiala // notify operation thread that an operation is ready to be processed 3712af245d11STodd Fiala sem_post(&m_operation_pending); 3713af245d11STodd Fiala 371443f2d971STamas Berghammer // Don't wait for the operation to complete in case of an exit operation. The operation thread 371543f2d971STamas Berghammer // will exit without posting to the semaphore 371643f2d971STamas Berghammer if (m_operation == EXIT_OPERATION) 371743f2d971STamas Berghammer return; 371843f2d971STamas Berghammer 3719af245d11STodd Fiala // wait for operation to complete 3720af245d11STodd Fiala while (sem_wait(&m_operation_done)) 3721af245d11STodd Fiala { 3722af245d11STodd Fiala if (errno == EINTR) 3723af245d11STodd Fiala continue; 3724af245d11STodd Fiala assert(false && "Unexpected errno from sem_wait"); 3725af245d11STodd Fiala } 3726af245d11STodd Fiala } 3727af245d11STodd Fiala 3728af245d11STodd Fiala Error 3729af245d11STodd Fiala NativeProcessLinux::ReadMemory (lldb::addr_t addr, void *buf, lldb::addr_t size, lldb::addr_t &bytes_read) 3730af245d11STodd Fiala { 3731af245d11STodd Fiala ReadOperation op(addr, buf, size, bytes_read); 3732af245d11STodd Fiala DoOperation(&op); 3733af245d11STodd Fiala return op.GetError (); 3734af245d11STodd Fiala } 3735af245d11STodd Fiala 3736af245d11STodd Fiala Error 3737af245d11STodd Fiala NativeProcessLinux::WriteMemory (lldb::addr_t addr, const void *buf, lldb::addr_t size, lldb::addr_t &bytes_written) 3738af245d11STodd Fiala { 3739af245d11STodd Fiala WriteOperation op(addr, buf, size, bytes_written); 3740af245d11STodd Fiala DoOperation(&op); 3741af245d11STodd Fiala return op.GetError (); 3742af245d11STodd Fiala } 3743af245d11STodd Fiala 374497ccc294SChaoren Lin Error 3745af245d11STodd Fiala NativeProcessLinux::ReadRegisterValue(lldb::tid_t tid, uint32_t offset, const char* reg_name, 3746af245d11STodd Fiala uint32_t size, RegisterValue &value) 3747af245d11STodd Fiala { 374897ccc294SChaoren Lin ReadRegOperation op(tid, offset, reg_name, value); 3749af245d11STodd Fiala DoOperation(&op); 375097ccc294SChaoren Lin return op.GetError(); 3751af245d11STodd Fiala } 3752af245d11STodd Fiala 375397ccc294SChaoren Lin Error 3754af245d11STodd Fiala NativeProcessLinux::WriteRegisterValue(lldb::tid_t tid, unsigned offset, 3755af245d11STodd Fiala const char* reg_name, const RegisterValue &value) 3756af245d11STodd Fiala { 375797ccc294SChaoren Lin WriteRegOperation op(tid, offset, reg_name, value); 3758af245d11STodd Fiala DoOperation(&op); 375997ccc294SChaoren Lin return op.GetError(); 3760af245d11STodd Fiala } 3761af245d11STodd Fiala 376297ccc294SChaoren Lin Error 3763af245d11STodd Fiala NativeProcessLinux::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size) 3764af245d11STodd Fiala { 376597ccc294SChaoren Lin ReadGPROperation op(tid, buf, buf_size); 3766af245d11STodd Fiala DoOperation(&op); 376797ccc294SChaoren Lin return op.GetError(); 3768af245d11STodd Fiala } 3769af245d11STodd Fiala 377097ccc294SChaoren Lin Error 3771af245d11STodd Fiala NativeProcessLinux::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size) 3772af245d11STodd Fiala { 377397ccc294SChaoren Lin ReadFPROperation op(tid, buf, buf_size); 3774af245d11STodd Fiala DoOperation(&op); 377597ccc294SChaoren Lin return op.GetError(); 3776af245d11STodd Fiala } 3777af245d11STodd Fiala 377897ccc294SChaoren Lin Error 3779af245d11STodd Fiala NativeProcessLinux::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) 3780af245d11STodd Fiala { 378197ccc294SChaoren Lin ReadRegisterSetOperation op(tid, buf, buf_size, regset); 3782af245d11STodd Fiala DoOperation(&op); 378397ccc294SChaoren Lin return op.GetError(); 3784af245d11STodd Fiala } 3785af245d11STodd Fiala 378697ccc294SChaoren Lin Error 3787af245d11STodd Fiala NativeProcessLinux::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size) 3788af245d11STodd Fiala { 378997ccc294SChaoren Lin WriteGPROperation op(tid, buf, buf_size); 3790af245d11STodd Fiala DoOperation(&op); 379197ccc294SChaoren Lin return op.GetError(); 3792af245d11STodd Fiala } 3793af245d11STodd Fiala 379497ccc294SChaoren Lin Error 3795af245d11STodd Fiala NativeProcessLinux::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size) 3796af245d11STodd Fiala { 379797ccc294SChaoren Lin WriteFPROperation op(tid, buf, buf_size); 3798af245d11STodd Fiala DoOperation(&op); 379997ccc294SChaoren Lin return op.GetError(); 3800af245d11STodd Fiala } 3801af245d11STodd Fiala 380297ccc294SChaoren Lin Error 3803af245d11STodd Fiala NativeProcessLinux::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) 3804af245d11STodd Fiala { 380597ccc294SChaoren Lin WriteRegisterSetOperation op(tid, buf, buf_size, regset); 3806af245d11STodd Fiala DoOperation(&op); 380797ccc294SChaoren Lin return op.GetError(); 3808af245d11STodd Fiala } 3809af245d11STodd Fiala 381097ccc294SChaoren Lin Error 3811af245d11STodd Fiala NativeProcessLinux::Resume (lldb::tid_t tid, uint32_t signo) 3812af245d11STodd Fiala { 3813af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3814af245d11STodd Fiala 3815af245d11STodd Fiala if (log) 3816af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid, 3817af245d11STodd Fiala GetUnixSignals().GetSignalAsCString (signo)); 381897ccc294SChaoren Lin ResumeOperation op (tid, signo); 3819af245d11STodd Fiala DoOperation (&op); 3820af245d11STodd Fiala if (log) 382197ccc294SChaoren Lin log->Printf ("NativeProcessLinux::%s() resuming thread = %" PRIu64 " result = %s", __FUNCTION__, tid, op.GetError().Success() ? "true" : "false"); 382297ccc294SChaoren Lin return op.GetError(); 3823af245d11STodd Fiala } 3824af245d11STodd Fiala 3825d8c338d4STamas Berghammer #if defined(__arm__) 3826d8c338d4STamas Berghammer 3827d8c338d4STamas Berghammer namespace { 3828d8c338d4STamas Berghammer 3829d8c338d4STamas Berghammer struct EmulatorBaton 3830d8c338d4STamas Berghammer { 3831d8c338d4STamas Berghammer NativeProcessLinux* m_process; 3832d8c338d4STamas Berghammer NativeRegisterContext* m_reg_context; 3833d8c338d4STamas Berghammer RegisterValue m_pc; 3834d8c338d4STamas Berghammer RegisterValue m_cpsr; 3835d8c338d4STamas Berghammer 3836d8c338d4STamas Berghammer EmulatorBaton(NativeProcessLinux* process, NativeRegisterContext* reg_context) : 3837d8c338d4STamas Berghammer m_process(process), m_reg_context(reg_context) {} 3838d8c338d4STamas Berghammer }; 3839d8c338d4STamas Berghammer 3840d8c338d4STamas Berghammer } // anonymous namespace 3841d8c338d4STamas Berghammer 3842d8c338d4STamas Berghammer static size_t 3843d8c338d4STamas Berghammer ReadMemoryCallback (EmulateInstruction *instruction, 3844d8c338d4STamas Berghammer void *baton, 3845d8c338d4STamas Berghammer const EmulateInstruction::Context &context, 3846d8c338d4STamas Berghammer lldb::addr_t addr, 3847d8c338d4STamas Berghammer void *dst, 3848d8c338d4STamas Berghammer size_t length) 3849d8c338d4STamas Berghammer { 3850d8c338d4STamas Berghammer EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); 3851d8c338d4STamas Berghammer 3852d8c338d4STamas Berghammer lldb::addr_t bytes_read; 3853d8c338d4STamas Berghammer emulator_baton->m_process->ReadMemory(addr, dst, length, bytes_read); 3854d8c338d4STamas Berghammer return bytes_read; 3855d8c338d4STamas Berghammer } 3856d8c338d4STamas Berghammer 3857d8c338d4STamas Berghammer static bool 3858d8c338d4STamas Berghammer ReadRegisterCallback (EmulateInstruction *instruction, 3859d8c338d4STamas Berghammer void *baton, 3860d8c338d4STamas Berghammer const RegisterInfo *reg_info, 3861d8c338d4STamas Berghammer RegisterValue ®_value) 3862d8c338d4STamas Berghammer { 3863d8c338d4STamas Berghammer EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); 3864d8c338d4STamas Berghammer 3865d8c338d4STamas Berghammer // The emulator only fill in the dwarf regsiter numbers (and in some case 3866d8c338d4STamas Berghammer // the generic register numbers). Get the full register info from the 3867d8c338d4STamas Berghammer // register context based on the dwarf register numbers. 3868d8c338d4STamas Berghammer const RegisterInfo* full_reg_info = emulator_baton->m_reg_context->GetRegisterInfo( 3869d8c338d4STamas Berghammer eRegisterKindDWARF, reg_info->kinds[eRegisterKindDWARF]); 3870d8c338d4STamas Berghammer 3871d8c338d4STamas Berghammer Error error = emulator_baton->m_reg_context->ReadRegister(full_reg_info, reg_value); 3872d8c338d4STamas Berghammer return error.Success(); 3873d8c338d4STamas Berghammer } 3874d8c338d4STamas Berghammer 3875d8c338d4STamas Berghammer static bool 3876d8c338d4STamas Berghammer WriteRegisterCallback (EmulateInstruction *instruction, 3877d8c338d4STamas Berghammer void *baton, 3878d8c338d4STamas Berghammer const EmulateInstruction::Context &context, 3879d8c338d4STamas Berghammer const RegisterInfo *reg_info, 3880d8c338d4STamas Berghammer const RegisterValue ®_value) 3881d8c338d4STamas Berghammer { 3882d8c338d4STamas Berghammer EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); 3883d8c338d4STamas Berghammer 3884d8c338d4STamas Berghammer switch (reg_info->kinds[eRegisterKindGeneric]) 3885d8c338d4STamas Berghammer { 3886d8c338d4STamas Berghammer case LLDB_REGNUM_GENERIC_PC: 3887d8c338d4STamas Berghammer emulator_baton->m_pc = reg_value; 3888d8c338d4STamas Berghammer break; 3889d8c338d4STamas Berghammer case LLDB_REGNUM_GENERIC_FLAGS: 3890d8c338d4STamas Berghammer emulator_baton->m_cpsr = reg_value; 3891d8c338d4STamas Berghammer break; 3892d8c338d4STamas Berghammer } 3893d8c338d4STamas Berghammer 3894d8c338d4STamas Berghammer return true; 3895d8c338d4STamas Berghammer } 3896d8c338d4STamas Berghammer 3897d8c338d4STamas Berghammer static size_t WriteMemoryCallback (EmulateInstruction *instruction, 3898d8c338d4STamas Berghammer void *baton, 3899d8c338d4STamas Berghammer const EmulateInstruction::Context &context, 3900d8c338d4STamas Berghammer lldb::addr_t addr, 3901d8c338d4STamas Berghammer const void *dst, 3902d8c338d4STamas Berghammer size_t length) 3903d8c338d4STamas Berghammer { 3904d8c338d4STamas Berghammer return length; 3905d8c338d4STamas Berghammer } 3906d8c338d4STamas Berghammer 3907d8c338d4STamas Berghammer Error 3908d8c338d4STamas Berghammer NativeProcessLinux::SingleStep(lldb::tid_t tid, uint32_t signo) 3909d8c338d4STamas Berghammer { 3910d8c338d4STamas Berghammer Error error; 3911d8c338d4STamas Berghammer NativeRegisterContextSP register_context_sp = GetThreadByID(tid)->GetRegisterContext(); 3912d8c338d4STamas Berghammer 3913d8c338d4STamas Berghammer std::unique_ptr<EmulateInstruction> emulator_ap( 3914d8c338d4STamas Berghammer EmulateInstruction::FindPlugin(m_arch, eInstructionTypePCModifying, nullptr)); 3915d8c338d4STamas Berghammer 3916d8c338d4STamas Berghammer if (emulator_ap == nullptr) 3917d8c338d4STamas Berghammer return Error("Instrunction emulator not found!"); 3918d8c338d4STamas Berghammer 3919d8c338d4STamas Berghammer EmulatorBaton baton(this, register_context_sp.get()); 3920d8c338d4STamas Berghammer emulator_ap->SetBaton(&baton); 3921d8c338d4STamas Berghammer emulator_ap->SetReadMemCallback(&ReadMemoryCallback); 3922d8c338d4STamas Berghammer emulator_ap->SetReadRegCallback(&ReadRegisterCallback); 3923d8c338d4STamas Berghammer emulator_ap->SetWriteMemCallback(&WriteMemoryCallback); 3924d8c338d4STamas Berghammer emulator_ap->SetWriteRegCallback(&WriteRegisterCallback); 3925d8c338d4STamas Berghammer 3926d8c338d4STamas Berghammer if (!emulator_ap->ReadInstruction()) 3927d8c338d4STamas Berghammer return Error("Read instruction failed!"); 3928d8c338d4STamas Berghammer 3929d8c338d4STamas Berghammer if (!emulator_ap->EvaluateInstruction(eEmulateInstructionOptionAutoAdvancePC)) 3930d8c338d4STamas Berghammer return Error("Evaluate instrcution failed!"); 3931d8c338d4STamas Berghammer 3932d8c338d4STamas Berghammer lldb::addr_t next_pc = baton.m_pc.GetAsUInt32(); 3933d8c338d4STamas Berghammer lldb::addr_t next_cpsr = 0; 3934d8c338d4STamas Berghammer if (baton.m_cpsr.GetType() != RegisterValue::eTypeInvalid) 3935d8c338d4STamas Berghammer { 3936d8c338d4STamas Berghammer next_cpsr = baton.m_cpsr.GetAsUInt32(); 3937d8c338d4STamas Berghammer } 3938d8c338d4STamas Berghammer else 3939d8c338d4STamas Berghammer { 3940d8c338d4STamas Berghammer const RegisterInfo* cpsr_info = register_context_sp->GetRegisterInfo( 3941d8c338d4STamas Berghammer eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 3942d8c338d4STamas Berghammer next_cpsr = register_context_sp->ReadRegisterAsUnsigned(cpsr_info, LLDB_INVALID_ADDRESS); 3943d8c338d4STamas Berghammer } 3944d8c338d4STamas Berghammer 3945d8c338d4STamas Berghammer if (next_cpsr & 0x20) 3946d8c338d4STamas Berghammer { 3947d8c338d4STamas Berghammer // Thumb mode 3948d8c338d4STamas Berghammer error = SetBreakpoint(next_pc, 2, false); 3949d8c338d4STamas Berghammer } 3950d8c338d4STamas Berghammer else 3951d8c338d4STamas Berghammer { 3952d8c338d4STamas Berghammer // Arm mode 3953d8c338d4STamas Berghammer error = SetBreakpoint(next_pc, 4, false); 3954d8c338d4STamas Berghammer } 3955d8c338d4STamas Berghammer 3956d8c338d4STamas Berghammer if (error.Fail()) 3957d8c338d4STamas Berghammer return error; 3958d8c338d4STamas Berghammer 3959*652e384aSOmair Javaid m_threads_stepping_with_breakpoint.insert({tid, next_pc}); 3960d8c338d4STamas Berghammer 3961d8c338d4STamas Berghammer error = Resume(tid, signo); 3962d8c338d4STamas Berghammer if (error.Fail()) 3963d8c338d4STamas Berghammer return error; 3964d8c338d4STamas Berghammer 3965d8c338d4STamas Berghammer return Error(); 3966d8c338d4STamas Berghammer } 3967d8c338d4STamas Berghammer 3968d8c338d4STamas Berghammer #else // defined(__arm__) 3969d8c338d4STamas Berghammer 397097ccc294SChaoren Lin Error 3971af245d11STodd Fiala NativeProcessLinux::SingleStep(lldb::tid_t tid, uint32_t signo) 3972af245d11STodd Fiala { 397397ccc294SChaoren Lin SingleStepOperation op(tid, signo); 3974af245d11STodd Fiala DoOperation(&op); 397597ccc294SChaoren Lin return op.GetError(); 3976af245d11STodd Fiala } 3977af245d11STodd Fiala 3978d8c338d4STamas Berghammer #endif // defined(__arm__) 3979d8c338d4STamas Berghammer 398097ccc294SChaoren Lin Error 398197ccc294SChaoren Lin NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) 3982af245d11STodd Fiala { 398397ccc294SChaoren Lin SiginfoOperation op(tid, siginfo); 3984af245d11STodd Fiala DoOperation(&op); 398597ccc294SChaoren Lin return op.GetError(); 3986af245d11STodd Fiala } 3987af245d11STodd Fiala 398897ccc294SChaoren Lin Error 3989af245d11STodd Fiala NativeProcessLinux::GetEventMessage(lldb::tid_t tid, unsigned long *message) 3990af245d11STodd Fiala { 399197ccc294SChaoren Lin EventMessageOperation op(tid, message); 3992af245d11STodd Fiala DoOperation(&op); 399397ccc294SChaoren Lin return op.GetError(); 3994af245d11STodd Fiala } 3995af245d11STodd Fiala 3996db264a6dSTamas Berghammer Error 3997af245d11STodd Fiala NativeProcessLinux::Detach(lldb::tid_t tid) 3998af245d11STodd Fiala { 399997ccc294SChaoren Lin if (tid == LLDB_INVALID_THREAD_ID) 400097ccc294SChaoren Lin return Error(); 400197ccc294SChaoren Lin 400297ccc294SChaoren Lin DetachOperation op(tid); 4003af245d11STodd Fiala DoOperation(&op); 400497ccc294SChaoren Lin return op.GetError(); 4005af245d11STodd Fiala } 4006af245d11STodd Fiala 4007af245d11STodd Fiala bool 4008af245d11STodd Fiala NativeProcessLinux::DupDescriptor(const char *path, int fd, int flags) 4009af245d11STodd Fiala { 4010af245d11STodd Fiala int target_fd = open(path, flags, 0666); 4011af245d11STodd Fiala 4012af245d11STodd Fiala if (target_fd == -1) 4013af245d11STodd Fiala return false; 4014af245d11STodd Fiala 4015493c3a12SPavel Labath if (dup2(target_fd, fd) == -1) 4016493c3a12SPavel Labath return false; 4017493c3a12SPavel Labath 4018493c3a12SPavel Labath return (close(target_fd) == -1) ? false : true; 4019af245d11STodd Fiala } 4020af245d11STodd Fiala 4021af245d11STodd Fiala void 40221107b5a5SPavel Labath NativeProcessLinux::StartMonitorThread(Error &error) 4023af245d11STodd Fiala { 40241107b5a5SPavel Labath m_monitor_up.reset(new Monitor(GetID(), this)); 40251107b5a5SPavel Labath error = m_monitor_up->Initialize(); 40261107b5a5SPavel Labath if (error.Fail()) { 40271107b5a5SPavel Labath m_monitor_up.reset(); 4028af245d11STodd Fiala } 4029af245d11STodd Fiala } 4030af245d11STodd Fiala 4031af245d11STodd Fiala void 4032af245d11STodd Fiala NativeProcessLinux::StopMonitor() 4033af245d11STodd Fiala { 40341107b5a5SPavel Labath m_monitor_up.reset(); 4035fa03ad2eSChaoren Lin StopCoordinatorThread (); 403643f2d971STamas Berghammer StopOpThread(); 4037af245d11STodd Fiala sem_destroy(&m_operation_pending); 4038af245d11STodd Fiala sem_destroy(&m_operation_done); 4039af245d11STodd Fiala 4040af245d11STodd Fiala // TODO: validate whether this still holds, fix up comment. 4041af245d11STodd Fiala // Note: ProcessPOSIX passes the m_terminal_fd file descriptor to 4042af245d11STodd Fiala // Process::SetSTDIOFileDescriptor, which in turn transfers ownership of 4043af245d11STodd Fiala // the descriptor to a ConnectionFileDescriptor object. Consequently 4044af245d11STodd Fiala // even though still has the file descriptor, we shouldn't close it here. 4045af245d11STodd Fiala } 4046af245d11STodd Fiala 4047af245d11STodd Fiala void 4048af245d11STodd Fiala NativeProcessLinux::StopOpThread() 4049af245d11STodd Fiala { 4050acee96aeSZachary Turner if (!m_operation_thread.IsJoinable()) 4051af245d11STodd Fiala return; 4052af245d11STodd Fiala 405343f2d971STamas Berghammer DoOperation(EXIT_OPERATION); 405439de3110SZachary Turner m_operation_thread.Join(nullptr); 4055af245d11STodd Fiala } 4056af245d11STodd Fiala 4057fa03ad2eSChaoren Lin Error 4058fa03ad2eSChaoren Lin NativeProcessLinux::StartCoordinatorThread () 4059fa03ad2eSChaoren Lin { 4060fa03ad2eSChaoren Lin Error error; 4061fa03ad2eSChaoren Lin static const char *g_thread_name = "lldb.process.linux.ts_coordinator"; 4062fa03ad2eSChaoren Lin Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 4063fa03ad2eSChaoren Lin 4064fa03ad2eSChaoren Lin // Skip if thread is already running 4065fa03ad2eSChaoren Lin if (m_coordinator_thread.IsJoinable()) 4066fa03ad2eSChaoren Lin { 4067fa03ad2eSChaoren Lin error.SetErrorString ("ThreadStateCoordinator's run loop is already running"); 4068fa03ad2eSChaoren Lin if (log) 4069fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s %s", __FUNCTION__, error.AsCString ()); 4070fa03ad2eSChaoren Lin return error; 4071fa03ad2eSChaoren Lin } 4072fa03ad2eSChaoren Lin 4073fa03ad2eSChaoren Lin // Enable verbose logging if lldb thread logging is enabled. 4074fa03ad2eSChaoren Lin m_coordinator_up->LogEnableEventProcessing (log != nullptr); 4075fa03ad2eSChaoren Lin 4076fa03ad2eSChaoren Lin if (log) 4077fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s launching ThreadStateCoordinator thread for pid %" PRIu64, __FUNCTION__, GetID ()); 4078fa03ad2eSChaoren Lin m_coordinator_thread = ThreadLauncher::LaunchThread(g_thread_name, CoordinatorThread, this, &error); 4079fa03ad2eSChaoren Lin return error; 4080fa03ad2eSChaoren Lin } 4081fa03ad2eSChaoren Lin 4082fa03ad2eSChaoren Lin void * 4083fa03ad2eSChaoren Lin NativeProcessLinux::CoordinatorThread (void *arg) 4084fa03ad2eSChaoren Lin { 4085fa03ad2eSChaoren Lin Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 4086fa03ad2eSChaoren Lin 4087fa03ad2eSChaoren Lin NativeProcessLinux *const process = static_cast<NativeProcessLinux*> (arg); 4088fa03ad2eSChaoren Lin assert (process && "null process passed to CoordinatorThread"); 4089fa03ad2eSChaoren Lin if (!process) 4090fa03ad2eSChaoren Lin { 4091fa03ad2eSChaoren Lin if (log) 4092fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s null process, exiting ThreadStateCoordinator processing loop", __FUNCTION__); 4093fa03ad2eSChaoren Lin return nullptr; 4094fa03ad2eSChaoren Lin } 4095fa03ad2eSChaoren Lin 4096fa03ad2eSChaoren Lin // Run the thread state coordinator loop until it is done. This call uses 4097fa03ad2eSChaoren Lin // efficient waiting for an event to be ready. 4098fa03ad2eSChaoren Lin while (process->m_coordinator_up->ProcessNextEvent () == ThreadStateCoordinator::eventLoopResultContinue) 4099fa03ad2eSChaoren Lin { 4100fa03ad2eSChaoren Lin } 4101fa03ad2eSChaoren Lin 4102fa03ad2eSChaoren Lin if (log) 4103fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " exiting ThreadStateCoordinator processing loop due to coordinator indicating completion", __FUNCTION__, process->GetID ()); 4104fa03ad2eSChaoren Lin 4105fa03ad2eSChaoren Lin return nullptr; 4106fa03ad2eSChaoren Lin } 4107fa03ad2eSChaoren Lin 4108fa03ad2eSChaoren Lin void 4109fa03ad2eSChaoren Lin NativeProcessLinux::StopCoordinatorThread() 4110fa03ad2eSChaoren Lin { 4111fa03ad2eSChaoren Lin Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 4112fa03ad2eSChaoren Lin if (log) 4113fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s requesting ThreadStateCoordinator stop for pid %" PRIu64, __FUNCTION__, GetID ()); 4114fa03ad2eSChaoren Lin 4115fa03ad2eSChaoren Lin // Tell the coordinator we're done. This will cause the coordinator 4116fa03ad2eSChaoren Lin // run loop thread to exit when the processing queue hits this message. 4117fa03ad2eSChaoren Lin m_coordinator_up->StopCoordinator (); 41188bc34f4dSOleksiy Vyalov m_coordinator_thread.Join (nullptr); 4119fa03ad2eSChaoren Lin } 4120fa03ad2eSChaoren Lin 4121af245d11STodd Fiala bool 4122af245d11STodd Fiala NativeProcessLinux::HasThreadNoLock (lldb::tid_t thread_id) 4123af245d11STodd Fiala { 4124af245d11STodd Fiala for (auto thread_sp : m_threads) 4125af245d11STodd Fiala { 4126af245d11STodd Fiala assert (thread_sp && "thread list should not contain NULL threads"); 4127af245d11STodd Fiala if (thread_sp->GetID () == thread_id) 4128af245d11STodd Fiala { 4129af245d11STodd Fiala // We have this thread. 4130af245d11STodd Fiala return true; 4131af245d11STodd Fiala } 4132af245d11STodd Fiala } 4133af245d11STodd Fiala 4134af245d11STodd Fiala // We don't have this thread. 4135af245d11STodd Fiala return false; 4136af245d11STodd Fiala } 4137af245d11STodd Fiala 4138af245d11STodd Fiala NativeThreadProtocolSP 4139af245d11STodd Fiala NativeProcessLinux::MaybeGetThreadNoLock (lldb::tid_t thread_id) 4140af245d11STodd Fiala { 4141af245d11STodd Fiala // CONSIDER organize threads by map - we can do better than linear. 4142af245d11STodd Fiala for (auto thread_sp : m_threads) 4143af245d11STodd Fiala { 4144af245d11STodd Fiala if (thread_sp->GetID () == thread_id) 4145af245d11STodd Fiala return thread_sp; 4146af245d11STodd Fiala } 4147af245d11STodd Fiala 4148af245d11STodd Fiala // We don't have this thread. 4149af245d11STodd Fiala return NativeThreadProtocolSP (); 4150af245d11STodd Fiala } 4151af245d11STodd Fiala 4152af245d11STodd Fiala bool 4153af245d11STodd Fiala NativeProcessLinux::StopTrackingThread (lldb::tid_t thread_id) 4154af245d11STodd Fiala { 4155af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 4156af245d11STodd Fiala for (auto it = m_threads.begin (); it != m_threads.end (); ++it) 4157af245d11STodd Fiala { 4158af245d11STodd Fiala if (*it && ((*it)->GetID () == thread_id)) 4159af245d11STodd Fiala { 4160af245d11STodd Fiala m_threads.erase (it); 4161af245d11STodd Fiala return true; 4162af245d11STodd Fiala } 4163af245d11STodd Fiala } 4164af245d11STodd Fiala 4165af245d11STodd Fiala // Didn't find it. 4166af245d11STodd Fiala return false; 4167af245d11STodd Fiala } 4168af245d11STodd Fiala 4169af245d11STodd Fiala NativeThreadProtocolSP 4170af245d11STodd Fiala NativeProcessLinux::AddThread (lldb::tid_t thread_id) 4171af245d11STodd Fiala { 4172af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 4173af245d11STodd Fiala 4174af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 4175af245d11STodd Fiala 4176af245d11STodd Fiala if (log) 4177af245d11STodd Fiala { 4178af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " adding thread with tid %" PRIu64, 4179af245d11STodd Fiala __FUNCTION__, 4180af245d11STodd Fiala GetID (), 4181af245d11STodd Fiala thread_id); 4182af245d11STodd Fiala } 4183af245d11STodd Fiala 4184af245d11STodd Fiala assert (!HasThreadNoLock (thread_id) && "attempted to add a thread by id that already exists"); 4185af245d11STodd Fiala 4186af245d11STodd Fiala // If this is the first thread, save it as the current thread 4187af245d11STodd Fiala if (m_threads.empty ()) 4188af245d11STodd Fiala SetCurrentThreadID (thread_id); 4189af245d11STodd Fiala 4190af245d11STodd Fiala NativeThreadProtocolSP thread_sp (new NativeThreadLinux (this, thread_id)); 4191af245d11STodd Fiala m_threads.push_back (thread_sp); 4192af245d11STodd Fiala 4193af245d11STodd Fiala return thread_sp; 4194af245d11STodd Fiala } 4195af245d11STodd Fiala 4196af245d11STodd Fiala NativeThreadProtocolSP 4197af245d11STodd Fiala NativeProcessLinux::GetOrCreateThread (lldb::tid_t thread_id, bool &created) 4198af245d11STodd Fiala { 4199af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 4200af245d11STodd Fiala 4201af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 4202af245d11STodd Fiala if (log) 4203af245d11STodd Fiala { 4204af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " get/create thread with tid %" PRIu64, 4205af245d11STodd Fiala __FUNCTION__, 4206af245d11STodd Fiala GetID (), 4207af245d11STodd Fiala thread_id); 4208af245d11STodd Fiala } 4209af245d11STodd Fiala 4210af245d11STodd Fiala // Retrieve the thread if it is already getting tracked. 4211af245d11STodd Fiala NativeThreadProtocolSP thread_sp = MaybeGetThreadNoLock (thread_id); 4212af245d11STodd Fiala if (thread_sp) 4213af245d11STodd Fiala { 4214af245d11STodd Fiala if (log) 4215af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": thread already tracked, returning", 4216af245d11STodd Fiala __FUNCTION__, 4217af245d11STodd Fiala GetID (), 4218af245d11STodd Fiala thread_id); 4219af245d11STodd Fiala created = false; 4220af245d11STodd Fiala return thread_sp; 4221af245d11STodd Fiala 4222af245d11STodd Fiala } 4223af245d11STodd Fiala 4224af245d11STodd Fiala // Create the thread metadata since it isn't being tracked. 4225af245d11STodd Fiala if (log) 4226af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": thread didn't exist, tracking now", 4227af245d11STodd Fiala __FUNCTION__, 4228af245d11STodd Fiala GetID (), 4229af245d11STodd Fiala thread_id); 4230af245d11STodd Fiala 4231af245d11STodd Fiala thread_sp.reset (new NativeThreadLinux (this, thread_id)); 4232af245d11STodd Fiala m_threads.push_back (thread_sp); 4233af245d11STodd Fiala created = true; 4234af245d11STodd Fiala 4235af245d11STodd Fiala return thread_sp; 4236af245d11STodd Fiala } 4237af245d11STodd Fiala 4238af245d11STodd Fiala Error 4239af245d11STodd Fiala NativeProcessLinux::FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp) 4240af245d11STodd Fiala { 424175f47c3aSTodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 4242af245d11STodd Fiala 4243af245d11STodd Fiala Error error; 4244af245d11STodd Fiala 4245af245d11STodd Fiala // Get a linux thread pointer. 4246af245d11STodd Fiala if (!thread_sp) 4247af245d11STodd Fiala { 4248af245d11STodd Fiala error.SetErrorString ("null thread_sp"); 4249af245d11STodd Fiala if (log) 4250af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); 4251af245d11STodd Fiala return error; 4252af245d11STodd Fiala } 4253cb84eebbSTamas Berghammer std::shared_ptr<NativeThreadLinux> linux_thread_sp = std::static_pointer_cast<NativeThreadLinux> (thread_sp); 4254af245d11STodd Fiala 4255af245d11STodd Fiala // Find out the size of a breakpoint (might depend on where we are in the code). 4256cb84eebbSTamas Berghammer NativeRegisterContextSP context_sp = linux_thread_sp->GetRegisterContext (); 4257af245d11STodd Fiala if (!context_sp) 4258af245d11STodd Fiala { 4259af245d11STodd Fiala error.SetErrorString ("cannot get a NativeRegisterContext for the thread"); 4260af245d11STodd Fiala if (log) 4261af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); 4262af245d11STodd Fiala return error; 4263af245d11STodd Fiala } 4264af245d11STodd Fiala 4265af245d11STodd Fiala uint32_t breakpoint_size = 0; 426663c8be95STamas Berghammer error = GetSoftwareBreakpointPCOffset (context_sp, breakpoint_size); 4267af245d11STodd Fiala if (error.Fail ()) 4268af245d11STodd Fiala { 4269af245d11STodd Fiala if (log) 4270af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s GetBreakpointSize() failed: %s", __FUNCTION__, error.AsCString ()); 4271af245d11STodd Fiala return error; 4272af245d11STodd Fiala } 4273af245d11STodd Fiala else 4274af245d11STodd Fiala { 4275af245d11STodd Fiala if (log) 4276af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s breakpoint size: %" PRIu32, __FUNCTION__, breakpoint_size); 4277af245d11STodd Fiala } 4278af245d11STodd Fiala 4279af245d11STodd Fiala // First try probing for a breakpoint at a software breakpoint location: PC - breakpoint size. 4280af245d11STodd Fiala const lldb::addr_t initial_pc_addr = context_sp->GetPC (); 4281af245d11STodd Fiala lldb::addr_t breakpoint_addr = initial_pc_addr; 4282af245d11STodd Fiala if (breakpoint_size > static_cast<lldb::addr_t> (0)) 4283af245d11STodd Fiala { 4284af245d11STodd Fiala // Do not allow breakpoint probe to wrap around. 4285af245d11STodd Fiala if (breakpoint_addr >= static_cast<lldb::addr_t> (breakpoint_size)) 4286af245d11STodd Fiala breakpoint_addr -= static_cast<lldb::addr_t> (breakpoint_size); 4287af245d11STodd Fiala } 4288af245d11STodd Fiala 4289af245d11STodd Fiala // Check if we stopped because of a breakpoint. 4290af245d11STodd Fiala NativeBreakpointSP breakpoint_sp; 4291af245d11STodd Fiala error = m_breakpoint_list.GetBreakpoint (breakpoint_addr, breakpoint_sp); 4292af245d11STodd Fiala if (!error.Success () || !breakpoint_sp) 4293af245d11STodd Fiala { 4294af245d11STodd Fiala // We didn't find one at a software probe location. Nothing to do. 4295af245d11STodd Fiala if (log) 4296af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " no lldb breakpoint found at current pc with adjustment: 0x%" PRIx64, __FUNCTION__, GetID (), breakpoint_addr); 4297af245d11STodd Fiala return Error (); 4298af245d11STodd Fiala } 4299af245d11STodd Fiala 4300af245d11STodd Fiala // If the breakpoint is not a software breakpoint, nothing to do. 4301af245d11STodd Fiala if (!breakpoint_sp->IsSoftwareBreakpoint ()) 4302af245d11STodd Fiala { 4303af245d11STodd Fiala if (log) 4304af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " breakpoint found at 0x%" PRIx64 ", not software, nothing to adjust", __FUNCTION__, GetID (), breakpoint_addr); 4305af245d11STodd Fiala return Error (); 4306af245d11STodd Fiala } 4307af245d11STodd Fiala 4308af245d11STodd Fiala // 4309af245d11STodd Fiala // We have a software breakpoint and need to adjust the PC. 4310af245d11STodd Fiala // 4311af245d11STodd Fiala 4312af245d11STodd Fiala // Sanity check. 4313af245d11STodd Fiala if (breakpoint_size == 0) 4314af245d11STodd Fiala { 4315af245d11STodd Fiala // Nothing to do! How did we get here? 4316af245d11STodd Fiala if (log) 4317af245d11STodd 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); 4318af245d11STodd Fiala return Error (); 4319af245d11STodd Fiala } 4320af245d11STodd Fiala 4321af245d11STodd Fiala // Change the program counter. 4322af245d11STodd Fiala if (log) 4323cb84eebbSTamas 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); 4324af245d11STodd Fiala 4325af245d11STodd Fiala error = context_sp->SetPC (breakpoint_addr); 4326af245d11STodd Fiala if (error.Fail ()) 4327af245d11STodd Fiala { 4328af245d11STodd Fiala if (log) 4329cb84eebbSTamas Berghammer log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": failed to set PC: %s", __FUNCTION__, GetID (), linux_thread_sp->GetID (), error.AsCString ()); 4330af245d11STodd Fiala return error; 4331af245d11STodd Fiala } 4332af245d11STodd Fiala 4333af245d11STodd Fiala return error; 4334af245d11STodd Fiala } 4335fa03ad2eSChaoren Lin 4336fa03ad2eSChaoren Lin void 4337fa03ad2eSChaoren Lin NativeProcessLinux::NotifyThreadCreateStopped (lldb::tid_t tid) 4338fa03ad2eSChaoren Lin { 4339fa03ad2eSChaoren Lin const bool is_stopped = true; 4340fa03ad2eSChaoren Lin m_coordinator_up->NotifyThreadCreate (tid, is_stopped, CoordinatorErrorHandler); 4341fa03ad2eSChaoren Lin } 4342fa03ad2eSChaoren Lin 4343fa03ad2eSChaoren Lin void 4344fa03ad2eSChaoren Lin NativeProcessLinux::NotifyThreadDeath (lldb::tid_t tid) 4345fa03ad2eSChaoren Lin { 4346fa03ad2eSChaoren Lin m_coordinator_up->NotifyThreadDeath (tid, CoordinatorErrorHandler); 4347fa03ad2eSChaoren Lin } 4348fa03ad2eSChaoren Lin 4349fa03ad2eSChaoren Lin void 4350fa03ad2eSChaoren Lin NativeProcessLinux::NotifyThreadStop (lldb::tid_t tid) 4351fa03ad2eSChaoren Lin { 435286fd8e45SChaoren Lin m_coordinator_up->NotifyThreadStop (tid, false, CoordinatorErrorHandler); 4353fa03ad2eSChaoren Lin } 4354fa03ad2eSChaoren Lin 4355fa03ad2eSChaoren Lin void 4356fa03ad2eSChaoren Lin NativeProcessLinux::CallAfterRunningThreadsStop (lldb::tid_t tid, 4357fa03ad2eSChaoren Lin const std::function<void (lldb::tid_t tid)> &call_after_function) 4358fa03ad2eSChaoren Lin { 435986fd8e45SChaoren Lin Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 436086fd8e45SChaoren Lin if (log) 436186fd8e45SChaoren Lin log->Printf("NativeProcessLinux::%s tid %" PRIu64, __FUNCTION__, tid); 436286fd8e45SChaoren Lin 4363fa03ad2eSChaoren Lin const lldb::pid_t pid = GetID (); 4364938fcf63SChaoren Lin m_coordinator_up->CallAfterRunningThreadsStop (tid, 4365fa03ad2eSChaoren Lin [=](lldb::tid_t request_stop_tid) 4366fa03ad2eSChaoren Lin { 436737c768caSChaoren Lin return RequestThreadStop(pid, request_stop_tid); 4368fa03ad2eSChaoren Lin }, 4369fa03ad2eSChaoren Lin call_after_function, 4370fa03ad2eSChaoren Lin CoordinatorErrorHandler); 437103f12d6bSChaoren Lin } 4372fa03ad2eSChaoren Lin 437303f12d6bSChaoren Lin void 437403f12d6bSChaoren Lin NativeProcessLinux::CallAfterRunningThreadsStopWithSkipTID (lldb::tid_t deferred_signal_tid, 437503f12d6bSChaoren Lin lldb::tid_t skip_stop_request_tid, 437603f12d6bSChaoren Lin const std::function<void (lldb::tid_t tid)> &call_after_function) 437703f12d6bSChaoren Lin { 437886fd8e45SChaoren Lin Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 437986fd8e45SChaoren Lin if (log) 438086fd8e45SChaoren Lin log->Printf("NativeProcessLinux::%s deferred_signal_tid %" PRIu64 ", skip_stop_request_tid %" PRIu64, __FUNCTION__, deferred_signal_tid, skip_stop_request_tid); 438186fd8e45SChaoren Lin 438203f12d6bSChaoren Lin const lldb::pid_t pid = GetID (); 438303f12d6bSChaoren Lin m_coordinator_up->CallAfterRunningThreadsStopWithSkipTIDs (deferred_signal_tid, 438403f12d6bSChaoren Lin skip_stop_request_tid != LLDB_INVALID_THREAD_ID ? ThreadStateCoordinator::ThreadIDSet {skip_stop_request_tid} : ThreadStateCoordinator::ThreadIDSet (), 438503f12d6bSChaoren Lin [=](lldb::tid_t request_stop_tid) 438603f12d6bSChaoren Lin { 438737c768caSChaoren Lin return RequestThreadStop(pid, request_stop_tid); 438803f12d6bSChaoren Lin }, 438903f12d6bSChaoren Lin call_after_function, 439003f12d6bSChaoren Lin CoordinatorErrorHandler); 4391fa03ad2eSChaoren Lin } 439286fd8e45SChaoren Lin 4393db264a6dSTamas Berghammer Error 439486fd8e45SChaoren Lin NativeProcessLinux::RequestThreadStop (const lldb::pid_t pid, const lldb::tid_t tid) 439586fd8e45SChaoren Lin { 439686fd8e45SChaoren Lin Log* log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 439786fd8e45SChaoren Lin if (log) 439886fd8e45SChaoren Lin log->Printf ("NativeProcessLinux::%s requesting thread stop(pid: %" PRIu64 ", tid: %" PRIu64 ")", __FUNCTION__, pid, tid); 439986fd8e45SChaoren Lin 440086fd8e45SChaoren Lin Error err; 440186fd8e45SChaoren Lin errno = 0; 440286fd8e45SChaoren Lin if (::tgkill (pid, tid, SIGSTOP) != 0) 440386fd8e45SChaoren Lin { 440486fd8e45SChaoren Lin err.SetErrorToErrno (); 440586fd8e45SChaoren Lin if (log) 440686fd8e45SChaoren Lin log->Printf ("NativeProcessLinux::%s tgkill(%" PRIu64 ", %" PRIu64 ", SIGSTOP) failed: %s", __FUNCTION__, pid, tid, err.AsCString ()); 440786fd8e45SChaoren Lin } 440886fd8e45SChaoren Lin 440986fd8e45SChaoren Lin return err; 441086fd8e45SChaoren Lin } 44117cb18bf5STamas Berghammer 44127cb18bf5STamas Berghammer Error 44137cb18bf5STamas Berghammer NativeProcessLinux::GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) 44147cb18bf5STamas Berghammer { 44157cb18bf5STamas Berghammer char maps_file_name[32]; 44167cb18bf5STamas Berghammer snprintf(maps_file_name, sizeof(maps_file_name), "/proc/%" PRIu64 "/maps", GetID()); 44177cb18bf5STamas Berghammer 44187cb18bf5STamas Berghammer FileSpec maps_file_spec(maps_file_name, false); 44197cb18bf5STamas Berghammer if (!maps_file_spec.Exists()) { 44207cb18bf5STamas Berghammer file_spec.Clear(); 44217cb18bf5STamas Berghammer return Error("/proc/%" PRIu64 "/maps file doesn't exists!", GetID()); 44227cb18bf5STamas Berghammer } 44237cb18bf5STamas Berghammer 44247cb18bf5STamas Berghammer FileSpec module_file_spec(module_path, true); 44257cb18bf5STamas Berghammer 44267cb18bf5STamas Berghammer std::ifstream maps_file(maps_file_name); 44277cb18bf5STamas Berghammer std::string maps_data_str((std::istreambuf_iterator<char>(maps_file)), std::istreambuf_iterator<char>()); 44287cb18bf5STamas Berghammer StringRef maps_data(maps_data_str.c_str()); 44297cb18bf5STamas Berghammer 44307cb18bf5STamas Berghammer while (!maps_data.empty()) 44317cb18bf5STamas Berghammer { 44327cb18bf5STamas Berghammer StringRef maps_row; 44337cb18bf5STamas Berghammer std::tie(maps_row, maps_data) = maps_data.split('\n'); 44347cb18bf5STamas Berghammer 44357cb18bf5STamas Berghammer SmallVector<StringRef, 16> maps_columns; 44367cb18bf5STamas Berghammer maps_row.split(maps_columns, StringRef(" "), -1, false); 44377cb18bf5STamas Berghammer 44387cb18bf5STamas Berghammer if (maps_columns.size() >= 6) 44397cb18bf5STamas Berghammer { 44407cb18bf5STamas Berghammer file_spec.SetFile(maps_columns[5].str().c_str(), false); 44417cb18bf5STamas Berghammer if (file_spec.GetFilename() == module_file_spec.GetFilename()) 44427cb18bf5STamas Berghammer return Error(); 44437cb18bf5STamas Berghammer } 44447cb18bf5STamas Berghammer } 44457cb18bf5STamas Berghammer 44467cb18bf5STamas Berghammer file_spec.Clear(); 44477cb18bf5STamas Berghammer return Error("Module file (%s) not found in /proc/%" PRIu64 "/maps file!", 44487cb18bf5STamas Berghammer module_file_spec.GetFilename().AsCString(), GetID()); 44497cb18bf5STamas Berghammer } 4450