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 216ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 226ac1be4bSTodd Fiala // NT_PRSTATUS and NT_FPREGSET definition 236ac1be4bSTodd Fiala #include <elf.h> 246ac1be4bSTodd Fiala #endif 256ac1be4bSTodd Fiala 26af245d11STodd Fiala // C++ Includes 27af245d11STodd Fiala #include <fstream> 28af245d11STodd Fiala #include <string> 29af245d11STodd Fiala 30af245d11STodd Fiala // Other libraries and framework includes 31af245d11STodd Fiala #include "lldb/Core/Debugger.h" 32af245d11STodd Fiala #include "lldb/Core/Error.h" 33af245d11STodd Fiala #include "lldb/Core/Module.h" 346edef204SOleksiy Vyalov #include "lldb/Core/ModuleSpec.h" 35af245d11STodd Fiala #include "lldb/Core/RegisterValue.h" 36af245d11STodd Fiala #include "lldb/Core/Scalar.h" 37af245d11STodd Fiala #include "lldb/Core/State.h" 38*0cbf0b13STamas Berghammer #include "lldb/Host/common/NativeRegisterContext.h" 39af245d11STodd Fiala #include "lldb/Host/Host.h" 4013b18261SZachary Turner #include "lldb/Host/HostInfo.h" 41*0cbf0b13STamas Berghammer #include "lldb/Host/HostNativeThread.h" 4239de3110SZachary Turner #include "lldb/Host/ThreadLauncher.h" 43af245d11STodd Fiala #include "lldb/Symbol/ObjectFile.h" 4490aff47cSZachary Turner #include "lldb/Target/Process.h" 45af245d11STodd Fiala #include "lldb/Target/ProcessLaunchInfo.h" 46af245d11STodd Fiala #include "lldb/Utility/PseudoTerminal.h" 47af245d11STodd Fiala 482fe1d0abSChaoren Lin #include "lldb/Host/common/NativeBreakpoint.h" 49af245d11STodd Fiala #include "Utility/StringExtractor.h" 50af245d11STodd Fiala 51af245d11STodd Fiala #include "Plugins/Process/Utility/LinuxSignals.h" 52af245d11STodd Fiala #include "NativeThreadLinux.h" 53af245d11STodd Fiala #include "ProcFileReader.h" 54fa03ad2eSChaoren Lin #include "ThreadStateCoordinator.h" 55cacde7dfSTodd Fiala #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" 56cacde7dfSTodd Fiala 57d858487eSTamas Berghammer // System includes - They have to be included after framework includes because they define some 58d858487eSTamas Berghammer // macros which collide with variable names in other modules 59d858487eSTamas Berghammer #include <linux/unistd.h> 60d858487eSTamas Berghammer #ifndef __ANDROID__ 61d858487eSTamas Berghammer #include <sys/procfs.h> 62d858487eSTamas Berghammer #endif 63d858487eSTamas Berghammer #include <sys/personality.h> 64d858487eSTamas Berghammer #include <sys/ptrace.h> 65d858487eSTamas Berghammer #include <sys/socket.h> 66d858487eSTamas Berghammer #include <sys/syscall.h> 67d858487eSTamas Berghammer #include <sys/types.h> 68d858487eSTamas Berghammer #include <sys/uio.h> 69d858487eSTamas Berghammer #include <sys/user.h> 70d858487eSTamas Berghammer #include <sys/wait.h> 71d858487eSTamas 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 141af245d11STodd Fiala // Private bits we only need internally. 142af245d11STodd Fiala namespace 143af245d11STodd Fiala { 144af245d11STodd Fiala using namespace lldb; 145af245d11STodd Fiala using namespace lldb_private; 146af245d11STodd Fiala 14743f2d971STamas Berghammer static void * const EXIT_OPERATION = nullptr; 14843f2d971STamas Berghammer 149af245d11STodd Fiala const UnixSignals& 150af245d11STodd Fiala GetUnixSignals () 151af245d11STodd Fiala { 152af245d11STodd Fiala static process_linux::LinuxSignals signals; 153af245d11STodd Fiala return signals; 154af245d11STodd Fiala } 155af245d11STodd Fiala 156fa03ad2eSChaoren Lin ThreadStateCoordinator::LogFunction 157fa03ad2eSChaoren Lin GetThreadLoggerFunction () 158fa03ad2eSChaoren Lin { 159fa03ad2eSChaoren Lin return [](const char *format, va_list args) 160fa03ad2eSChaoren Lin { 161fa03ad2eSChaoren Lin Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 162fa03ad2eSChaoren Lin if (log) 163fa03ad2eSChaoren Lin log->VAPrintf (format, args); 164fa03ad2eSChaoren Lin }; 165fa03ad2eSChaoren Lin } 166fa03ad2eSChaoren Lin 167fa03ad2eSChaoren Lin void 168fa03ad2eSChaoren Lin CoordinatorErrorHandler (const std::string &error_message) 169fa03ad2eSChaoren Lin { 170fa03ad2eSChaoren Lin Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 171fa03ad2eSChaoren Lin if (log) 17286fd8e45SChaoren Lin log->Printf ("NativeProcessLinux::%s %s", __FUNCTION__, error_message.c_str ()); 173fa03ad2eSChaoren Lin assert (false && "ThreadStateCoordinator error reported"); 174fa03ad2eSChaoren Lin } 175fa03ad2eSChaoren Lin 176af245d11STodd Fiala Error 177af245d11STodd Fiala ResolveProcessArchitecture (lldb::pid_t pid, Platform &platform, ArchSpec &arch) 178af245d11STodd Fiala { 179af245d11STodd Fiala // Grab process info for the running process. 180af245d11STodd Fiala ProcessInstanceInfo process_info; 181af245d11STodd Fiala if (!platform.GetProcessInfo (pid, process_info)) 182af245d11STodd Fiala return lldb_private::Error("failed to get process info"); 183af245d11STodd Fiala 184af245d11STodd Fiala // Resolve the executable module. 185af245d11STodd Fiala ModuleSP exe_module_sp; 186e56f6dceSChaoren Lin ModuleSpec exe_module_spec(process_info.GetExecutableFile(), process_info.GetArchitecture()); 187af245d11STodd Fiala FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths ()); 188af245d11STodd Fiala Error error = platform.ResolveExecutable( 18954539338SOleksiy Vyalov exe_module_spec, 190af245d11STodd Fiala exe_module_sp, 191af245d11STodd Fiala executable_search_paths.GetSize () ? &executable_search_paths : NULL); 192af245d11STodd Fiala 193af245d11STodd Fiala if (!error.Success ()) 194af245d11STodd Fiala return error; 195af245d11STodd Fiala 196af245d11STodd Fiala // Check if we've got our architecture from the exe_module. 197af245d11STodd Fiala arch = exe_module_sp->GetArchitecture (); 198af245d11STodd Fiala if (arch.IsValid ()) 199af245d11STodd Fiala return Error(); 200af245d11STodd Fiala else 201af245d11STodd Fiala return Error("failed to retrieve a valid architecture from the exe module"); 202af245d11STodd Fiala } 203af245d11STodd Fiala 204af245d11STodd Fiala void 205af245d11STodd Fiala DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count) 206af245d11STodd Fiala { 207af245d11STodd Fiala uint8_t *ptr = (uint8_t *)bytes; 208af245d11STodd Fiala const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count); 209af245d11STodd Fiala for(uint32_t i=0; i<loop_count; i++) 210af245d11STodd Fiala { 211af245d11STodd Fiala s.Printf ("[%x]", *ptr); 212af245d11STodd Fiala ptr++; 213af245d11STodd Fiala } 214af245d11STodd Fiala } 215af245d11STodd Fiala 216af245d11STodd Fiala void 217af245d11STodd Fiala PtraceDisplayBytes(int &req, void *data, size_t data_size) 218af245d11STodd Fiala { 219af245d11STodd Fiala StreamString buf; 220af245d11STodd Fiala Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet ( 221af245d11STodd Fiala POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE)); 222af245d11STodd Fiala 223af245d11STodd Fiala if (verbose_log) 224af245d11STodd Fiala { 225af245d11STodd Fiala switch(req) 226af245d11STodd Fiala { 227af245d11STodd Fiala case PTRACE_POKETEXT: 228af245d11STodd Fiala { 229af245d11STodd Fiala DisplayBytes(buf, &data, 8); 230af245d11STodd Fiala verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData()); 231af245d11STodd Fiala break; 232af245d11STodd Fiala } 233af245d11STodd Fiala case PTRACE_POKEDATA: 234af245d11STodd Fiala { 235af245d11STodd Fiala DisplayBytes(buf, &data, 8); 236af245d11STodd Fiala verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData()); 237af245d11STodd Fiala break; 238af245d11STodd Fiala } 239af245d11STodd Fiala case PTRACE_POKEUSER: 240af245d11STodd Fiala { 241af245d11STodd Fiala DisplayBytes(buf, &data, 8); 242af245d11STodd Fiala verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData()); 243af245d11STodd Fiala break; 244af245d11STodd Fiala } 245af245d11STodd Fiala case PTRACE_SETREGS: 246af245d11STodd Fiala { 247af245d11STodd Fiala DisplayBytes(buf, data, data_size); 248af245d11STodd Fiala verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData()); 249af245d11STodd Fiala break; 250af245d11STodd Fiala } 251af245d11STodd Fiala case PTRACE_SETFPREGS: 252af245d11STodd Fiala { 253af245d11STodd Fiala DisplayBytes(buf, data, data_size); 254af245d11STodd Fiala verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData()); 255af245d11STodd Fiala break; 256af245d11STodd Fiala } 257af245d11STodd Fiala case PTRACE_SETSIGINFO: 258af245d11STodd Fiala { 259af245d11STodd Fiala DisplayBytes(buf, data, sizeof(siginfo_t)); 260af245d11STodd Fiala verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData()); 261af245d11STodd Fiala break; 262af245d11STodd Fiala } 263af245d11STodd Fiala case PTRACE_SETREGSET: 264af245d11STodd Fiala { 265af245d11STodd Fiala // Extract iov_base from data, which is a pointer to the struct IOVEC 266af245d11STodd Fiala DisplayBytes(buf, *(void **)data, data_size); 267af245d11STodd Fiala verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData()); 268af245d11STodd Fiala break; 269af245d11STodd Fiala } 270af245d11STodd Fiala default: 271af245d11STodd Fiala { 272af245d11STodd Fiala } 273af245d11STodd Fiala } 274af245d11STodd Fiala } 275af245d11STodd Fiala } 276af245d11STodd Fiala 277af245d11STodd Fiala // Wrapper for ptrace to catch errors and log calls. 278af245d11STodd Fiala // Note that ptrace sets errno on error because -1 can be a valid result (i.e. for PTRACE_PEEK*) 279af245d11STodd Fiala long 28097ccc294SChaoren Lin PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, Error& error, 281af245d11STodd Fiala const char* reqName, const char* file, int line) 282af245d11STodd Fiala { 283af245d11STodd Fiala long int result; 284af245d11STodd Fiala 285af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE)); 286af245d11STodd Fiala 287af245d11STodd Fiala PtraceDisplayBytes(req, data, data_size); 288af245d11STodd Fiala 28997ccc294SChaoren Lin error.Clear(); 290af245d11STodd Fiala errno = 0; 291af245d11STodd Fiala if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) 292af245d11STodd Fiala result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), *(unsigned int *)addr, data); 293af245d11STodd Fiala else 294af245d11STodd Fiala result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), addr, data); 295af245d11STodd Fiala 29697ccc294SChaoren Lin if (result == -1) 29797ccc294SChaoren Lin error.SetErrorToErrno(); 29897ccc294SChaoren Lin 299af245d11STodd Fiala if (log) 300af245d11STodd Fiala log->Printf("ptrace(%s, %" PRIu64 ", %p, %p, %zu)=%lX called from file %s line %d", 301af245d11STodd Fiala reqName, pid, addr, data, data_size, result, file, line); 302af245d11STodd Fiala 303af245d11STodd Fiala PtraceDisplayBytes(req, data, data_size); 304af245d11STodd Fiala 30597ccc294SChaoren Lin if (log && error.GetError() != 0) 306af245d11STodd Fiala { 307af245d11STodd Fiala const char* str; 30897ccc294SChaoren Lin switch (error.GetError()) 309af245d11STodd Fiala { 310af245d11STodd Fiala case ESRCH: str = "ESRCH"; break; 311af245d11STodd Fiala case EINVAL: str = "EINVAL"; break; 312af245d11STodd Fiala case EBUSY: str = "EBUSY"; break; 313af245d11STodd Fiala case EPERM: str = "EPERM"; break; 31497ccc294SChaoren Lin default: str = error.AsCString(); 315af245d11STodd Fiala } 31697ccc294SChaoren Lin log->Printf("ptrace() failed; errno=%d (%s)", error.GetError(), str); 317af245d11STodd Fiala } 318af245d11STodd Fiala 319af245d11STodd Fiala return result; 320af245d11STodd Fiala } 321af245d11STodd Fiala 322af245d11STodd Fiala #ifdef LLDB_CONFIGURATION_BUILDANDINTEGRATION 323af245d11STodd Fiala // Wrapper for ptrace when logging is not required. 324af245d11STodd Fiala // Sets errno to 0 prior to calling ptrace. 325af245d11STodd Fiala long 32697ccc294SChaoren Lin PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, Error& error) 327af245d11STodd Fiala { 328af245d11STodd Fiala long result = 0; 32997ccc294SChaoren Lin 33097ccc294SChaoren Lin error.Clear(); 331af245d11STodd Fiala errno = 0; 332af245d11STodd Fiala if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) 333af245d11STodd Fiala result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), *(unsigned int *)addr, data); 334af245d11STodd Fiala else 335af245d11STodd Fiala result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), addr, data); 33697ccc294SChaoren Lin 33797ccc294SChaoren Lin if (result == -1) 33897ccc294SChaoren Lin error.SetErrorToErrno(); 339af245d11STodd Fiala return result; 340af245d11STodd Fiala } 341af245d11STodd Fiala #endif 342af245d11STodd Fiala 343af245d11STodd Fiala //------------------------------------------------------------------------------ 344af245d11STodd Fiala // Static implementations of NativeProcessLinux::ReadMemory and 345af245d11STodd Fiala // NativeProcessLinux::WriteMemory. This enables mutual recursion between these 346af245d11STodd Fiala // functions without needed to go thru the thread funnel. 347af245d11STodd Fiala 34897ccc294SChaoren Lin lldb::addr_t 349af245d11STodd Fiala DoReadMemory ( 350af245d11STodd Fiala lldb::pid_t pid, 351af245d11STodd Fiala lldb::addr_t vm_addr, 352af245d11STodd Fiala void *buf, 353af245d11STodd Fiala lldb::addr_t size, 354af245d11STodd Fiala Error &error) 355af245d11STodd Fiala { 356af245d11STodd Fiala // ptrace word size is determined by the host, not the child 357af245d11STodd Fiala static const unsigned word_size = sizeof(void*); 358af245d11STodd Fiala unsigned char *dst = static_cast<unsigned char*>(buf); 359af245d11STodd Fiala lldb::addr_t bytes_read; 360af245d11STodd Fiala lldb::addr_t remainder; 361af245d11STodd Fiala long data; 362af245d11STodd Fiala 363af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); 364af245d11STodd Fiala if (log) 365af245d11STodd Fiala ProcessPOSIXLog::IncNestLevel(); 366af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) 367af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__, 368af245d11STodd Fiala pid, word_size, (void*)vm_addr, buf, size); 369af245d11STodd Fiala 370af245d11STodd Fiala assert(sizeof(data) >= word_size); 371af245d11STodd Fiala for (bytes_read = 0; bytes_read < size; bytes_read += remainder) 372af245d11STodd Fiala { 37397ccc294SChaoren Lin data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, nullptr, 0, error); 37497ccc294SChaoren Lin if (error.Fail()) 375af245d11STodd Fiala { 376af245d11STodd Fiala if (log) 377af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 378af245d11STodd Fiala return bytes_read; 379af245d11STodd Fiala } 380af245d11STodd Fiala 381af245d11STodd Fiala remainder = size - bytes_read; 382af245d11STodd Fiala remainder = remainder > word_size ? word_size : remainder; 383af245d11STodd Fiala 384af245d11STodd Fiala // Copy the data into our buffer 385af245d11STodd Fiala for (unsigned i = 0; i < remainder; ++i) 386af245d11STodd Fiala dst[i] = ((data >> i*8) & 0xFF); 387af245d11STodd Fiala 388af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && 389af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 390af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 391af245d11STodd Fiala size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 392af245d11STodd Fiala { 393af245d11STodd Fiala uintptr_t print_dst = 0; 394af245d11STodd Fiala // Format bytes from data by moving into print_dst for log output 395af245d11STodd Fiala for (unsigned i = 0; i < remainder; ++i) 396af245d11STodd Fiala print_dst |= (((data >> i*8) & 0xFF) << i*8); 397af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 398af245d11STodd Fiala (void*)vm_addr, print_dst, (unsigned long)data); 399af245d11STodd Fiala } 400af245d11STodd Fiala 401af245d11STodd Fiala vm_addr += word_size; 402af245d11STodd Fiala dst += word_size; 403af245d11STodd Fiala } 404af245d11STodd Fiala 405af245d11STodd Fiala if (log) 406af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 407af245d11STodd Fiala return bytes_read; 408af245d11STodd Fiala } 409af245d11STodd Fiala 41097ccc294SChaoren Lin lldb::addr_t 411af245d11STodd Fiala DoWriteMemory( 412af245d11STodd Fiala lldb::pid_t pid, 413af245d11STodd Fiala lldb::addr_t vm_addr, 414af245d11STodd Fiala const void *buf, 415af245d11STodd Fiala lldb::addr_t size, 416af245d11STodd Fiala Error &error) 417af245d11STodd Fiala { 418af245d11STodd Fiala // ptrace word size is determined by the host, not the child 419af245d11STodd Fiala static const unsigned word_size = sizeof(void*); 420af245d11STodd Fiala const unsigned char *src = static_cast<const unsigned char*>(buf); 421af245d11STodd Fiala lldb::addr_t bytes_written = 0; 422af245d11STodd Fiala lldb::addr_t remainder; 423af245d11STodd Fiala 424af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); 425af245d11STodd Fiala if (log) 426af245d11STodd Fiala ProcessPOSIXLog::IncNestLevel(); 427af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) 428af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(%" PRIu64 ", %u, %p, %p, %" PRIu64 ")", __FUNCTION__, 429af245d11STodd Fiala pid, word_size, (void*)vm_addr, buf, size); 430af245d11STodd Fiala 431af245d11STodd Fiala for (bytes_written = 0; bytes_written < size; bytes_written += remainder) 432af245d11STodd Fiala { 433af245d11STodd Fiala remainder = size - bytes_written; 434af245d11STodd Fiala remainder = remainder > word_size ? word_size : remainder; 435af245d11STodd Fiala 436af245d11STodd Fiala if (remainder == word_size) 437af245d11STodd Fiala { 438af245d11STodd Fiala unsigned long data = 0; 439af245d11STodd Fiala assert(sizeof(data) >= word_size); 440af245d11STodd Fiala for (unsigned i = 0; i < word_size; ++i) 441af245d11STodd Fiala data |= (unsigned long)src[i] << i*8; 442af245d11STodd Fiala 443af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && 444af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 445af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 446af245d11STodd Fiala size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 447af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 448af245d11STodd Fiala (void*)vm_addr, *(unsigned long*)src, data); 449af245d11STodd Fiala 45097ccc294SChaoren Lin if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0, error)) 451af245d11STodd Fiala { 452af245d11STodd Fiala if (log) 453af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 454af245d11STodd Fiala return bytes_written; 455af245d11STodd Fiala } 456af245d11STodd Fiala } 457af245d11STodd Fiala else 458af245d11STodd Fiala { 459af245d11STodd Fiala unsigned char buff[8]; 460af245d11STodd Fiala if (DoReadMemory(pid, vm_addr, 461af245d11STodd Fiala buff, word_size, error) != word_size) 462af245d11STodd Fiala { 463af245d11STodd Fiala if (log) 464af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 465af245d11STodd Fiala return bytes_written; 466af245d11STodd Fiala } 467af245d11STodd Fiala 468af245d11STodd Fiala memcpy(buff, src, remainder); 469af245d11STodd Fiala 470af245d11STodd Fiala if (DoWriteMemory(pid, vm_addr, 471af245d11STodd Fiala buff, word_size, error) != word_size) 472af245d11STodd Fiala { 473af245d11STodd Fiala if (log) 474af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 475af245d11STodd Fiala return bytes_written; 476af245d11STodd Fiala } 477af245d11STodd Fiala 478af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && 479af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 480af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 481af245d11STodd Fiala size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 482af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 483af245d11STodd Fiala (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff); 484af245d11STodd Fiala } 485af245d11STodd Fiala 486af245d11STodd Fiala vm_addr += word_size; 487af245d11STodd Fiala src += word_size; 488af245d11STodd Fiala } 489af245d11STodd Fiala if (log) 490af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 491af245d11STodd Fiala return bytes_written; 492af245d11STodd Fiala } 493af245d11STodd Fiala 494af245d11STodd Fiala //------------------------------------------------------------------------------ 495af245d11STodd Fiala /// @class Operation 496af245d11STodd Fiala /// @brief Represents a NativeProcessLinux operation. 497af245d11STodd Fiala /// 498af245d11STodd Fiala /// Under Linux, it is not possible to ptrace() from any other thread but the 499af245d11STodd Fiala /// one that spawned or attached to the process from the start. Therefore, when 500af245d11STodd Fiala /// a NativeProcessLinux is asked to deliver or change the state of an inferior 501af245d11STodd Fiala /// process the operation must be "funneled" to a specific thread to perform the 502af245d11STodd Fiala /// task. The Operation class provides an abstract base for all services the 503af245d11STodd Fiala /// NativeProcessLinux must perform via the single virtual function Execute, thus 504af245d11STodd Fiala /// encapsulating the code that needs to run in the privileged context. 505af245d11STodd Fiala class Operation 506af245d11STodd Fiala { 507af245d11STodd Fiala public: 508af245d11STodd Fiala Operation () : m_error() { } 509af245d11STodd Fiala 510af245d11STodd Fiala virtual 511af245d11STodd Fiala ~Operation() {} 512af245d11STodd Fiala 513af245d11STodd Fiala virtual void 514af245d11STodd Fiala Execute (NativeProcessLinux *process) = 0; 515af245d11STodd Fiala 516af245d11STodd Fiala const Error & 517af245d11STodd Fiala GetError () const { return m_error; } 518af245d11STodd Fiala 519af245d11STodd Fiala protected: 520af245d11STodd Fiala Error m_error; 521af245d11STodd Fiala }; 522af245d11STodd Fiala 523af245d11STodd Fiala //------------------------------------------------------------------------------ 524af245d11STodd Fiala /// @class ReadOperation 525af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadMemory. 526af245d11STodd Fiala class ReadOperation : public Operation 527af245d11STodd Fiala { 528af245d11STodd Fiala public: 529af245d11STodd Fiala ReadOperation ( 530af245d11STodd Fiala lldb::addr_t addr, 531af245d11STodd Fiala void *buff, 532af245d11STodd Fiala lldb::addr_t size, 533b35103ebSTodd Fiala lldb::addr_t &result) : 534af245d11STodd Fiala Operation (), 535af245d11STodd Fiala m_addr (addr), 536af245d11STodd Fiala m_buff (buff), 537af245d11STodd Fiala m_size (size), 538af245d11STodd Fiala m_result (result) 539af245d11STodd Fiala { 540af245d11STodd Fiala } 541af245d11STodd Fiala 542af245d11STodd Fiala void Execute (NativeProcessLinux *process) override; 543af245d11STodd Fiala 544af245d11STodd Fiala private: 545af245d11STodd Fiala lldb::addr_t m_addr; 546af245d11STodd Fiala void *m_buff; 547af245d11STodd Fiala lldb::addr_t m_size; 548af245d11STodd Fiala lldb::addr_t &m_result; 549af245d11STodd Fiala }; 550af245d11STodd Fiala 551af245d11STodd Fiala void 552af245d11STodd Fiala ReadOperation::Execute (NativeProcessLinux *process) 553af245d11STodd Fiala { 554af245d11STodd Fiala m_result = DoReadMemory (process->GetID (), m_addr, m_buff, m_size, m_error); 555af245d11STodd Fiala } 556af245d11STodd Fiala 557af245d11STodd Fiala //------------------------------------------------------------------------------ 558af245d11STodd Fiala /// @class WriteOperation 559af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteMemory. 560af245d11STodd Fiala class WriteOperation : public Operation 561af245d11STodd Fiala { 562af245d11STodd Fiala public: 563af245d11STodd Fiala WriteOperation ( 564af245d11STodd Fiala lldb::addr_t addr, 565af245d11STodd Fiala const void *buff, 566af245d11STodd Fiala lldb::addr_t size, 567af245d11STodd Fiala lldb::addr_t &result) : 568af245d11STodd Fiala Operation (), 569af245d11STodd Fiala m_addr (addr), 570af245d11STodd Fiala m_buff (buff), 571af245d11STodd Fiala m_size (size), 572af245d11STodd Fiala m_result (result) 573af245d11STodd Fiala { 574af245d11STodd Fiala } 575af245d11STodd Fiala 576af245d11STodd Fiala void Execute (NativeProcessLinux *process) override; 577af245d11STodd Fiala 578af245d11STodd Fiala private: 579af245d11STodd Fiala lldb::addr_t m_addr; 580af245d11STodd Fiala const void *m_buff; 581af245d11STodd Fiala lldb::addr_t m_size; 582af245d11STodd Fiala lldb::addr_t &m_result; 583af245d11STodd Fiala }; 584af245d11STodd Fiala 585af245d11STodd Fiala void 586af245d11STodd Fiala WriteOperation::Execute(NativeProcessLinux *process) 587af245d11STodd Fiala { 588af245d11STodd Fiala m_result = DoWriteMemory (process->GetID (), m_addr, m_buff, m_size, m_error); 589af245d11STodd Fiala } 590af245d11STodd Fiala 591af245d11STodd Fiala //------------------------------------------------------------------------------ 592af245d11STodd Fiala /// @class ReadRegOperation 593af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadRegisterValue. 594af245d11STodd Fiala class ReadRegOperation : public Operation 595af245d11STodd Fiala { 596af245d11STodd Fiala public: 597af245d11STodd Fiala ReadRegOperation(lldb::tid_t tid, uint32_t offset, const char *reg_name, 59897ccc294SChaoren Lin RegisterValue &value) 59997ccc294SChaoren Lin : m_tid(tid), 60097ccc294SChaoren Lin m_offset(static_cast<uintptr_t> (offset)), 60197ccc294SChaoren Lin m_reg_name(reg_name), 60297ccc294SChaoren Lin m_value(value) 603af245d11STodd Fiala { } 604af245d11STodd Fiala 605af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 606af245d11STodd Fiala 607af245d11STodd Fiala private: 608af245d11STodd Fiala lldb::tid_t m_tid; 609af245d11STodd Fiala uintptr_t m_offset; 610af245d11STodd Fiala const char *m_reg_name; 611af245d11STodd Fiala RegisterValue &m_value; 612af245d11STodd Fiala }; 613af245d11STodd Fiala 614af245d11STodd Fiala void 615af245d11STodd Fiala ReadRegOperation::Execute(NativeProcessLinux *monitor) 616af245d11STodd Fiala { 6170fceef80STodd Fiala #if defined (__arm64__) || defined (__aarch64__) 6180fceef80STodd Fiala if (m_offset > sizeof(struct user_pt_regs)) 6190fceef80STodd Fiala { 6200fceef80STodd Fiala uintptr_t offset = m_offset - sizeof(struct user_pt_regs); 6210fceef80STodd Fiala if (offset > sizeof(struct user_fpsimd_state)) 6220fceef80STodd Fiala { 62397ccc294SChaoren Lin m_error.SetErrorString("invalid offset value"); 62497ccc294SChaoren Lin return; 6250fceef80STodd Fiala } 6260fceef80STodd Fiala elf_fpregset_t regs; 6270fceef80STodd Fiala int regset = NT_FPREGSET; 6280fceef80STodd Fiala struct iovec ioVec; 6290fceef80STodd Fiala 6300fceef80STodd Fiala ioVec.iov_base = ®s; 6310fceef80STodd Fiala ioVec.iov_len = sizeof regs; 63297ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 63397ccc294SChaoren Lin if (m_error.Success()) 6340fceef80STodd Fiala { 6350fceef80STodd Fiala lldb_private::ArchSpec arch; 6360fceef80STodd Fiala if (monitor->GetArchitecture(arch)) 6370fceef80STodd Fiala m_value.SetBytes((void *)(((unsigned char *)(®s)) + offset), 16, arch.GetByteOrder()); 6380fceef80STodd Fiala else 63997ccc294SChaoren Lin m_error.SetErrorString("failed to get architecture"); 6400fceef80STodd Fiala } 6410fceef80STodd Fiala } 6420fceef80STodd Fiala else 6430fceef80STodd Fiala { 6440fceef80STodd Fiala elf_gregset_t regs; 6450fceef80STodd Fiala int regset = NT_PRSTATUS; 6460fceef80STodd Fiala struct iovec ioVec; 6470fceef80STodd Fiala 6480fceef80STodd Fiala ioVec.iov_base = ®s; 6490fceef80STodd Fiala ioVec.iov_len = sizeof regs; 65097ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 65197ccc294SChaoren Lin if (m_error.Success()) 6520fceef80STodd Fiala { 6530fceef80STodd Fiala lldb_private::ArchSpec arch; 6540fceef80STodd Fiala if (monitor->GetArchitecture(arch)) 6550fceef80STodd Fiala m_value.SetBytes((void *)(((unsigned char *)(regs)) + m_offset), 8, arch.GetByteOrder()); 65697ccc294SChaoren Lin else 65797ccc294SChaoren Lin m_error.SetErrorString("failed to get architecture"); 6580fceef80STodd Fiala } 6590fceef80STodd Fiala } 6600fceef80STodd Fiala #else 661af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS)); 662af245d11STodd Fiala 66397ccc294SChaoren Lin lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, nullptr, 0, m_error); 66497ccc294SChaoren Lin if (m_error.Success()) 665af245d11STodd Fiala m_value = data; 66697ccc294SChaoren Lin 667af245d11STodd Fiala if (log) 668af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() reg %s: 0x%" PRIx64, __FUNCTION__, 669af245d11STodd Fiala m_reg_name, data); 6700fceef80STodd Fiala #endif 671af245d11STodd Fiala } 672af245d11STodd Fiala 673af245d11STodd Fiala //------------------------------------------------------------------------------ 674af245d11STodd Fiala /// @class WriteRegOperation 675af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteRegisterValue. 676af245d11STodd Fiala class WriteRegOperation : public Operation 677af245d11STodd Fiala { 678af245d11STodd Fiala public: 679af245d11STodd Fiala WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name, 68097ccc294SChaoren Lin const RegisterValue &value) 68197ccc294SChaoren Lin : m_tid(tid), 68297ccc294SChaoren Lin m_offset(offset), 68397ccc294SChaoren Lin m_reg_name(reg_name), 68497ccc294SChaoren Lin m_value(value) 685af245d11STodd Fiala { } 686af245d11STodd Fiala 687af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 688af245d11STodd Fiala 689af245d11STodd Fiala private: 690af245d11STodd Fiala lldb::tid_t m_tid; 691af245d11STodd Fiala uintptr_t m_offset; 692af245d11STodd Fiala const char *m_reg_name; 693af245d11STodd Fiala const RegisterValue &m_value; 694af245d11STodd Fiala }; 695af245d11STodd Fiala 696af245d11STodd Fiala void 697af245d11STodd Fiala WriteRegOperation::Execute(NativeProcessLinux *monitor) 698af245d11STodd Fiala { 6990fceef80STodd Fiala #if defined (__arm64__) || defined (__aarch64__) 7000fceef80STodd Fiala if (m_offset > sizeof(struct user_pt_regs)) 7010fceef80STodd Fiala { 7020fceef80STodd Fiala uintptr_t offset = m_offset - sizeof(struct user_pt_regs); 7030fceef80STodd Fiala if (offset > sizeof(struct user_fpsimd_state)) 7040fceef80STodd Fiala { 70597ccc294SChaoren Lin m_error.SetErrorString("invalid offset value"); 70697ccc294SChaoren Lin return; 7070fceef80STodd Fiala } 7080fceef80STodd Fiala elf_fpregset_t regs; 7090fceef80STodd Fiala int regset = NT_FPREGSET; 7100fceef80STodd Fiala struct iovec ioVec; 7110fceef80STodd Fiala 7120fceef80STodd Fiala ioVec.iov_base = ®s; 7130fceef80STodd Fiala ioVec.iov_len = sizeof regs; 71497ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 7159425b329SBhushan D. Attarde if (m_error.Success()) 7160fceef80STodd Fiala { 7170fceef80STodd Fiala ::memcpy((void *)(((unsigned char *)(®s)) + offset), m_value.GetBytes(), 16); 71897ccc294SChaoren Lin PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 7190fceef80STodd Fiala } 7200fceef80STodd Fiala } 7210fceef80STodd Fiala else 7220fceef80STodd Fiala { 7230fceef80STodd Fiala elf_gregset_t regs; 7240fceef80STodd Fiala int regset = NT_PRSTATUS; 7250fceef80STodd Fiala struct iovec ioVec; 7260fceef80STodd Fiala 7270fceef80STodd Fiala ioVec.iov_base = ®s; 7280fceef80STodd Fiala ioVec.iov_len = sizeof regs; 72997ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 7309425b329SBhushan D. Attarde if (m_error.Success()) 7310fceef80STodd Fiala { 7320fceef80STodd Fiala ::memcpy((void *)(((unsigned char *)(®s)) + m_offset), m_value.GetBytes(), 8); 73397ccc294SChaoren Lin PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, sizeof regs, m_error); 7340fceef80STodd Fiala } 7350fceef80STodd Fiala } 7360fceef80STodd Fiala #else 737af245d11STodd Fiala void* buf; 738af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS)); 739af245d11STodd Fiala 740af245d11STodd Fiala buf = (void*) m_value.GetAsUInt64(); 741af245d11STodd Fiala 742af245d11STodd Fiala if (log) 743af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf); 74497ccc294SChaoren Lin PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0, m_error); 7450fceef80STodd Fiala #endif 746af245d11STodd Fiala } 747af245d11STodd Fiala 748af245d11STodd Fiala //------------------------------------------------------------------------------ 749af245d11STodd Fiala /// @class ReadGPROperation 750af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadGPR. 751af245d11STodd Fiala class ReadGPROperation : public Operation 752af245d11STodd Fiala { 753af245d11STodd Fiala public: 75497ccc294SChaoren Lin ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size) 75597ccc294SChaoren Lin : m_tid(tid), m_buf(buf), m_buf_size(buf_size) 756af245d11STodd Fiala { } 757af245d11STodd Fiala 758af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 759af245d11STodd Fiala 760af245d11STodd Fiala private: 761af245d11STodd Fiala lldb::tid_t m_tid; 762af245d11STodd Fiala void *m_buf; 763af245d11STodd Fiala size_t m_buf_size; 764af245d11STodd Fiala }; 765af245d11STodd Fiala 766af245d11STodd Fiala void 767af245d11STodd Fiala ReadGPROperation::Execute(NativeProcessLinux *monitor) 768af245d11STodd Fiala { 7696ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 7706ac1be4bSTodd Fiala int regset = NT_PRSTATUS; 7716ac1be4bSTodd Fiala struct iovec ioVec; 7726ac1be4bSTodd Fiala 7736ac1be4bSTodd Fiala ioVec.iov_base = m_buf; 7746ac1be4bSTodd Fiala ioVec.iov_len = m_buf_size; 77597ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); 7766ac1be4bSTodd Fiala #else 77797ccc294SChaoren Lin PTRACE(PTRACE_GETREGS, m_tid, nullptr, m_buf, m_buf_size, m_error); 7786ac1be4bSTodd Fiala #endif 779af245d11STodd Fiala } 780af245d11STodd Fiala 781af245d11STodd Fiala //------------------------------------------------------------------------------ 782af245d11STodd Fiala /// @class ReadFPROperation 783af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadFPR. 784af245d11STodd Fiala class ReadFPROperation : public Operation 785af245d11STodd Fiala { 786af245d11STodd Fiala public: 78797ccc294SChaoren Lin ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size) 78897ccc294SChaoren Lin : m_tid(tid), 78997ccc294SChaoren Lin m_buf(buf), 79097ccc294SChaoren Lin m_buf_size(buf_size) 791af245d11STodd Fiala { } 792af245d11STodd Fiala 793af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 794af245d11STodd Fiala 795af245d11STodd Fiala private: 796af245d11STodd Fiala lldb::tid_t m_tid; 797af245d11STodd Fiala void *m_buf; 798af245d11STodd Fiala size_t m_buf_size; 799af245d11STodd Fiala }; 800af245d11STodd Fiala 801af245d11STodd Fiala void 802af245d11STodd Fiala ReadFPROperation::Execute(NativeProcessLinux *monitor) 803af245d11STodd Fiala { 8046ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 8056ac1be4bSTodd Fiala int regset = NT_FPREGSET; 8066ac1be4bSTodd Fiala struct iovec ioVec; 8076ac1be4bSTodd Fiala 8086ac1be4bSTodd Fiala ioVec.iov_base = m_buf; 8096ac1be4bSTodd Fiala ioVec.iov_len = m_buf_size; 8106ac1be4bSTodd Fiala if (PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, m_buf_size) < 0) 8116ac1be4bSTodd Fiala m_result = false; 8126ac1be4bSTodd Fiala else 8136ac1be4bSTodd Fiala m_result = true; 8146ac1be4bSTodd Fiala #else 81597ccc294SChaoren Lin PTRACE(PTRACE_GETFPREGS, m_tid, nullptr, m_buf, m_buf_size, m_error); 8166ac1be4bSTodd Fiala #endif 817af245d11STodd Fiala } 818af245d11STodd Fiala 819af245d11STodd Fiala //------------------------------------------------------------------------------ 820af245d11STodd Fiala /// @class ReadRegisterSetOperation 821af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadRegisterSet. 822af245d11STodd Fiala class ReadRegisterSetOperation : public Operation 823af245d11STodd Fiala { 824af245d11STodd Fiala public: 82597ccc294SChaoren Lin ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) 82697ccc294SChaoren Lin : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset) 827af245d11STodd Fiala { } 828af245d11STodd Fiala 829af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 830af245d11STodd Fiala 831af245d11STodd Fiala private: 832af245d11STodd Fiala lldb::tid_t m_tid; 833af245d11STodd Fiala void *m_buf; 834af245d11STodd Fiala size_t m_buf_size; 835af245d11STodd Fiala const unsigned int m_regset; 836af245d11STodd Fiala }; 837af245d11STodd Fiala 838af245d11STodd Fiala void 839af245d11STodd Fiala ReadRegisterSetOperation::Execute(NativeProcessLinux *monitor) 840af245d11STodd Fiala { 84197ccc294SChaoren Lin PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size, m_error); 842af245d11STodd Fiala } 843af245d11STodd Fiala 844af245d11STodd Fiala //------------------------------------------------------------------------------ 845af245d11STodd Fiala /// @class WriteGPROperation 846af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteGPR. 847af245d11STodd Fiala class WriteGPROperation : public Operation 848af245d11STodd Fiala { 849af245d11STodd Fiala public: 85097ccc294SChaoren Lin WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size) 85197ccc294SChaoren Lin : m_tid(tid), m_buf(buf), m_buf_size(buf_size) 852af245d11STodd Fiala { } 853af245d11STodd Fiala 854af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 855af245d11STodd Fiala 856af245d11STodd Fiala private: 857af245d11STodd Fiala lldb::tid_t m_tid; 858af245d11STodd Fiala void *m_buf; 859af245d11STodd Fiala size_t m_buf_size; 860af245d11STodd Fiala }; 861af245d11STodd Fiala 862af245d11STodd Fiala void 863af245d11STodd Fiala WriteGPROperation::Execute(NativeProcessLinux *monitor) 864af245d11STodd Fiala { 8656ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 8666ac1be4bSTodd Fiala int regset = NT_PRSTATUS; 8676ac1be4bSTodd Fiala struct iovec ioVec; 8686ac1be4bSTodd Fiala 8696ac1be4bSTodd Fiala ioVec.iov_base = m_buf; 8706ac1be4bSTodd Fiala ioVec.iov_len = m_buf_size; 87197ccc294SChaoren Lin PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); 8726ac1be4bSTodd Fiala #else 87397ccc294SChaoren Lin PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size, m_error); 8746ac1be4bSTodd Fiala #endif 875af245d11STodd Fiala } 876af245d11STodd Fiala 877af245d11STodd Fiala //------------------------------------------------------------------------------ 878af245d11STodd Fiala /// @class WriteFPROperation 879af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteFPR. 880af245d11STodd Fiala class WriteFPROperation : public Operation 881af245d11STodd Fiala { 882af245d11STodd Fiala public: 88397ccc294SChaoren Lin WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size) 88497ccc294SChaoren Lin : m_tid(tid), m_buf(buf), m_buf_size(buf_size) 885af245d11STodd Fiala { } 886af245d11STodd Fiala 887af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 888af245d11STodd Fiala 889af245d11STodd Fiala private: 890af245d11STodd Fiala lldb::tid_t m_tid; 891af245d11STodd Fiala void *m_buf; 892af245d11STodd Fiala size_t m_buf_size; 893af245d11STodd Fiala }; 894af245d11STodd Fiala 895af245d11STodd Fiala void 896af245d11STodd Fiala WriteFPROperation::Execute(NativeProcessLinux *monitor) 897af245d11STodd Fiala { 8986ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 8996ac1be4bSTodd Fiala int regset = NT_FPREGSET; 9006ac1be4bSTodd Fiala struct iovec ioVec; 9016ac1be4bSTodd Fiala 9026ac1be4bSTodd Fiala ioVec.iov_base = m_buf; 9036ac1be4bSTodd Fiala ioVec.iov_len = m_buf_size; 90497ccc294SChaoren Lin PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, m_buf_size, m_error); 9056ac1be4bSTodd Fiala #else 90697ccc294SChaoren Lin PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size, m_error); 9076ac1be4bSTodd Fiala #endif 908af245d11STodd Fiala } 909af245d11STodd Fiala 910af245d11STodd Fiala //------------------------------------------------------------------------------ 911af245d11STodd Fiala /// @class WriteRegisterSetOperation 912af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteRegisterSet. 913af245d11STodd Fiala class WriteRegisterSetOperation : public Operation 914af245d11STodd Fiala { 915af245d11STodd Fiala public: 91697ccc294SChaoren Lin WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) 91797ccc294SChaoren Lin : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset) 918af245d11STodd Fiala { } 919af245d11STodd Fiala 920af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 921af245d11STodd Fiala 922af245d11STodd Fiala private: 923af245d11STodd Fiala lldb::tid_t m_tid; 924af245d11STodd Fiala void *m_buf; 925af245d11STodd Fiala size_t m_buf_size; 926af245d11STodd Fiala const unsigned int m_regset; 927af245d11STodd Fiala }; 928af245d11STodd Fiala 929af245d11STodd Fiala void 930af245d11STodd Fiala WriteRegisterSetOperation::Execute(NativeProcessLinux *monitor) 931af245d11STodd Fiala { 93297ccc294SChaoren Lin PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size, m_error); 933af245d11STodd Fiala } 934af245d11STodd Fiala 935af245d11STodd Fiala //------------------------------------------------------------------------------ 936af245d11STodd Fiala /// @class ResumeOperation 937af245d11STodd Fiala /// @brief Implements NativeProcessLinux::Resume. 938af245d11STodd Fiala class ResumeOperation : public Operation 939af245d11STodd Fiala { 940af245d11STodd Fiala public: 94197ccc294SChaoren Lin ResumeOperation(lldb::tid_t tid, uint32_t signo) : 94297ccc294SChaoren Lin m_tid(tid), m_signo(signo) { } 943af245d11STodd Fiala 944af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 945af245d11STodd Fiala 946af245d11STodd Fiala private: 947af245d11STodd Fiala lldb::tid_t m_tid; 948af245d11STodd Fiala uint32_t m_signo; 949af245d11STodd Fiala }; 950af245d11STodd Fiala 951af245d11STodd Fiala void 952af245d11STodd Fiala ResumeOperation::Execute(NativeProcessLinux *monitor) 953af245d11STodd Fiala { 954af245d11STodd Fiala intptr_t data = 0; 955af245d11STodd Fiala 956af245d11STodd Fiala if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) 957af245d11STodd Fiala data = m_signo; 958af245d11STodd Fiala 95997ccc294SChaoren Lin PTRACE(PTRACE_CONT, m_tid, nullptr, (void*)data, 0, m_error); 96097ccc294SChaoren Lin if (m_error.Fail()) 961af245d11STodd Fiala { 962af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 963af245d11STodd Fiala 964af245d11STodd Fiala if (log) 96597ccc294SChaoren Lin log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, m_error.AsCString()); 966af245d11STodd Fiala } 967af245d11STodd Fiala } 968af245d11STodd Fiala 969af245d11STodd Fiala //------------------------------------------------------------------------------ 970af245d11STodd Fiala /// @class SingleStepOperation 971af245d11STodd Fiala /// @brief Implements NativeProcessLinux::SingleStep. 972af245d11STodd Fiala class SingleStepOperation : public Operation 973af245d11STodd Fiala { 974af245d11STodd Fiala public: 97597ccc294SChaoren Lin SingleStepOperation(lldb::tid_t tid, uint32_t signo) 97697ccc294SChaoren Lin : m_tid(tid), m_signo(signo) { } 977af245d11STodd Fiala 978af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 979af245d11STodd Fiala 980af245d11STodd Fiala private: 981af245d11STodd Fiala lldb::tid_t m_tid; 982af245d11STodd Fiala uint32_t m_signo; 983af245d11STodd Fiala }; 984af245d11STodd Fiala 985af245d11STodd Fiala void 986af245d11STodd Fiala SingleStepOperation::Execute(NativeProcessLinux *monitor) 987af245d11STodd Fiala { 988af245d11STodd Fiala intptr_t data = 0; 989af245d11STodd Fiala 990af245d11STodd Fiala if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) 991af245d11STodd Fiala data = m_signo; 992af245d11STodd Fiala 99397ccc294SChaoren Lin PTRACE(PTRACE_SINGLESTEP, m_tid, nullptr, (void*)data, 0, m_error); 994af245d11STodd Fiala } 995af245d11STodd Fiala 996af245d11STodd Fiala //------------------------------------------------------------------------------ 997af245d11STodd Fiala /// @class SiginfoOperation 998af245d11STodd Fiala /// @brief Implements NativeProcessLinux::GetSignalInfo. 999af245d11STodd Fiala class SiginfoOperation : public Operation 1000af245d11STodd Fiala { 1001af245d11STodd Fiala public: 100297ccc294SChaoren Lin SiginfoOperation(lldb::tid_t tid, void *info) 100397ccc294SChaoren Lin : m_tid(tid), m_info(info) { } 1004af245d11STodd Fiala 1005af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 1006af245d11STodd Fiala 1007af245d11STodd Fiala private: 1008af245d11STodd Fiala lldb::tid_t m_tid; 1009af245d11STodd Fiala void *m_info; 1010af245d11STodd Fiala }; 1011af245d11STodd Fiala 1012af245d11STodd Fiala void 1013af245d11STodd Fiala SiginfoOperation::Execute(NativeProcessLinux *monitor) 1014af245d11STodd Fiala { 101597ccc294SChaoren Lin PTRACE(PTRACE_GETSIGINFO, m_tid, nullptr, m_info, 0, m_error); 1016af245d11STodd Fiala } 1017af245d11STodd Fiala 1018af245d11STodd Fiala //------------------------------------------------------------------------------ 1019af245d11STodd Fiala /// @class EventMessageOperation 1020af245d11STodd Fiala /// @brief Implements NativeProcessLinux::GetEventMessage. 1021af245d11STodd Fiala class EventMessageOperation : public Operation 1022af245d11STodd Fiala { 1023af245d11STodd Fiala public: 102497ccc294SChaoren Lin EventMessageOperation(lldb::tid_t tid, unsigned long *message) 102597ccc294SChaoren Lin : m_tid(tid), m_message(message) { } 1026af245d11STodd Fiala 1027af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 1028af245d11STodd Fiala 1029af245d11STodd Fiala private: 1030af245d11STodd Fiala lldb::tid_t m_tid; 1031af245d11STodd Fiala unsigned long *m_message; 1032af245d11STodd Fiala }; 1033af245d11STodd Fiala 1034af245d11STodd Fiala void 1035af245d11STodd Fiala EventMessageOperation::Execute(NativeProcessLinux *monitor) 1036af245d11STodd Fiala { 103797ccc294SChaoren Lin PTRACE(PTRACE_GETEVENTMSG, m_tid, nullptr, m_message, 0, m_error); 1038af245d11STodd Fiala } 1039af245d11STodd Fiala 1040af245d11STodd Fiala class DetachOperation : public Operation 1041af245d11STodd Fiala { 1042af245d11STodd Fiala public: 104397ccc294SChaoren Lin DetachOperation(lldb::tid_t tid) : m_tid(tid) { } 1044af245d11STodd Fiala 1045af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 1046af245d11STodd Fiala 1047af245d11STodd Fiala private: 1048af245d11STodd Fiala lldb::tid_t m_tid; 1049af245d11STodd Fiala }; 1050af245d11STodd Fiala 1051af245d11STodd Fiala void 1052af245d11STodd Fiala DetachOperation::Execute(NativeProcessLinux *monitor) 1053af245d11STodd Fiala { 105497ccc294SChaoren Lin PTRACE(PTRACE_DETACH, m_tid, nullptr, 0, 0, m_error); 1055af245d11STodd Fiala } 1056af245d11STodd Fiala 1057af245d11STodd Fiala } 1058af245d11STodd Fiala 1059af245d11STodd Fiala using namespace lldb_private; 1060af245d11STodd Fiala 1061af245d11STodd Fiala // Simple helper function to ensure flags are enabled on the given file 1062af245d11STodd Fiala // descriptor. 1063af245d11STodd Fiala static bool 1064af245d11STodd Fiala EnsureFDFlags(int fd, int flags, Error &error) 1065af245d11STodd Fiala { 1066af245d11STodd Fiala int status; 1067af245d11STodd Fiala 1068af245d11STodd Fiala if ((status = fcntl(fd, F_GETFL)) == -1) 1069af245d11STodd Fiala { 1070af245d11STodd Fiala error.SetErrorToErrno(); 1071af245d11STodd Fiala return false; 1072af245d11STodd Fiala } 1073af245d11STodd Fiala 1074af245d11STodd Fiala if (fcntl(fd, F_SETFL, status | flags) == -1) 1075af245d11STodd Fiala { 1076af245d11STodd Fiala error.SetErrorToErrno(); 1077af245d11STodd Fiala return false; 1078af245d11STodd Fiala } 1079af245d11STodd Fiala 1080af245d11STodd Fiala return true; 1081af245d11STodd Fiala } 1082af245d11STodd Fiala 1083af245d11STodd Fiala NativeProcessLinux::OperationArgs::OperationArgs(NativeProcessLinux *monitor) 1084af245d11STodd Fiala : m_monitor(monitor) 1085af245d11STodd Fiala { 1086af245d11STodd Fiala sem_init(&m_semaphore, 0, 0); 1087af245d11STodd Fiala } 1088af245d11STodd Fiala 1089af245d11STodd Fiala NativeProcessLinux::OperationArgs::~OperationArgs() 1090af245d11STodd Fiala { 1091af245d11STodd Fiala sem_destroy(&m_semaphore); 1092af245d11STodd Fiala } 1093af245d11STodd Fiala 1094af245d11STodd Fiala NativeProcessLinux::LaunchArgs::LaunchArgs(NativeProcessLinux *monitor, 1095af245d11STodd Fiala lldb_private::Module *module, 1096af245d11STodd Fiala char const **argv, 1097af245d11STodd Fiala char const **envp, 109875f47c3aSTodd Fiala const std::string &stdin_path, 109975f47c3aSTodd Fiala const std::string &stdout_path, 110075f47c3aSTodd Fiala const std::string &stderr_path, 11010bce1b67STodd Fiala const char *working_dir, 11020bce1b67STodd Fiala const lldb_private::ProcessLaunchInfo &launch_info) 1103af245d11STodd Fiala : OperationArgs(monitor), 1104af245d11STodd Fiala m_module(module), 1105af245d11STodd Fiala m_argv(argv), 1106af245d11STodd Fiala m_envp(envp), 1107af245d11STodd Fiala m_stdin_path(stdin_path), 1108af245d11STodd Fiala m_stdout_path(stdout_path), 1109af245d11STodd Fiala m_stderr_path(stderr_path), 11100bce1b67STodd Fiala m_working_dir(working_dir), 11110bce1b67STodd Fiala m_launch_info(launch_info) 11120bce1b67STodd Fiala { 11130bce1b67STodd Fiala } 1114af245d11STodd Fiala 1115af245d11STodd Fiala NativeProcessLinux::LaunchArgs::~LaunchArgs() 1116af245d11STodd Fiala { } 1117af245d11STodd Fiala 1118af245d11STodd Fiala NativeProcessLinux::AttachArgs::AttachArgs(NativeProcessLinux *monitor, 1119af245d11STodd Fiala lldb::pid_t pid) 1120af245d11STodd Fiala : OperationArgs(monitor), m_pid(pid) { } 1121af245d11STodd Fiala 1122af245d11STodd Fiala NativeProcessLinux::AttachArgs::~AttachArgs() 1123af245d11STodd Fiala { } 1124af245d11STodd Fiala 1125af245d11STodd Fiala // ----------------------------------------------------------------------------- 1126af245d11STodd Fiala // Public Static Methods 1127af245d11STodd Fiala // ----------------------------------------------------------------------------- 1128af245d11STodd Fiala 1129af245d11STodd Fiala lldb_private::Error 1130af245d11STodd Fiala NativeProcessLinux::LaunchProcess ( 1131af245d11STodd Fiala lldb_private::Module *exe_module, 1132af245d11STodd Fiala lldb_private::ProcessLaunchInfo &launch_info, 1133af245d11STodd Fiala lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate, 1134af245d11STodd Fiala NativeProcessProtocolSP &native_process_sp) 1135af245d11STodd Fiala { 1136af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1137af245d11STodd Fiala 1138af245d11STodd Fiala Error error; 1139af245d11STodd Fiala 1140af245d11STodd Fiala // Verify the working directory is valid if one was specified. 1141af245d11STodd Fiala const char* working_dir = launch_info.GetWorkingDirectory (); 1142af245d11STodd Fiala if (working_dir) 1143af245d11STodd Fiala { 1144af245d11STodd Fiala FileSpec working_dir_fs (working_dir, true); 1145af245d11STodd Fiala if (!working_dir_fs || working_dir_fs.GetFileType () != FileSpec::eFileTypeDirectory) 1146af245d11STodd Fiala { 1147af245d11STodd Fiala error.SetErrorStringWithFormat ("No such file or directory: %s", working_dir); 1148af245d11STodd Fiala return error; 1149af245d11STodd Fiala } 1150af245d11STodd Fiala } 1151af245d11STodd Fiala 1152696b5287SZachary Turner const lldb_private::FileAction *file_action; 1153af245d11STodd Fiala 1154af245d11STodd Fiala // Default of NULL will mean to use existing open file descriptors. 115575f47c3aSTodd Fiala std::string stdin_path; 115675f47c3aSTodd Fiala std::string stdout_path; 115775f47c3aSTodd Fiala std::string stderr_path; 1158af245d11STodd Fiala 1159af245d11STodd Fiala file_action = launch_info.GetFileActionForFD (STDIN_FILENO); 116075f47c3aSTodd Fiala if (file_action) 116175f47c3aSTodd Fiala stdin_path = file_action->GetPath (); 1162af245d11STodd Fiala 1163af245d11STodd Fiala file_action = launch_info.GetFileActionForFD (STDOUT_FILENO); 116475f47c3aSTodd Fiala if (file_action) 116575f47c3aSTodd Fiala stdout_path = file_action->GetPath (); 1166af245d11STodd Fiala 1167af245d11STodd Fiala file_action = launch_info.GetFileActionForFD (STDERR_FILENO); 116875f47c3aSTodd Fiala if (file_action) 116975f47c3aSTodd Fiala stderr_path = file_action->GetPath (); 117075f47c3aSTodd Fiala 117175f47c3aSTodd Fiala if (log) 117275f47c3aSTodd Fiala { 117375f47c3aSTodd Fiala if (!stdin_path.empty ()) 117475f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s setting STDIN to '%s'", __FUNCTION__, stdin_path.c_str ()); 117575f47c3aSTodd Fiala else 117675f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s leaving STDIN as is", __FUNCTION__); 117775f47c3aSTodd Fiala 117875f47c3aSTodd Fiala if (!stdout_path.empty ()) 117975f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s setting STDOUT to '%s'", __FUNCTION__, stdout_path.c_str ()); 118075f47c3aSTodd Fiala else 118175f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s leaving STDOUT as is", __FUNCTION__); 118275f47c3aSTodd Fiala 118375f47c3aSTodd Fiala if (!stderr_path.empty ()) 118475f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s setting STDERR to '%s'", __FUNCTION__, stderr_path.c_str ()); 118575f47c3aSTodd Fiala else 118675f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s leaving STDERR as is", __FUNCTION__); 118775f47c3aSTodd Fiala } 1188af245d11STodd Fiala 1189af245d11STodd Fiala // Create the NativeProcessLinux in launch mode. 1190af245d11STodd Fiala native_process_sp.reset (new NativeProcessLinux ()); 1191af245d11STodd Fiala 1192af245d11STodd Fiala if (log) 1193af245d11STodd Fiala { 1194af245d11STodd Fiala int i = 0; 1195af245d11STodd Fiala for (const char **args = launch_info.GetArguments ().GetConstArgumentVector (); *args; ++args, ++i) 1196af245d11STodd Fiala { 1197af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s arg %d: \"%s\"", __FUNCTION__, i, *args ? *args : "nullptr"); 1198af245d11STodd Fiala ++i; 1199af245d11STodd Fiala } 1200af245d11STodd Fiala } 1201af245d11STodd Fiala 1202af245d11STodd Fiala if (!native_process_sp->RegisterNativeDelegate (native_delegate)) 1203af245d11STodd Fiala { 1204af245d11STodd Fiala native_process_sp.reset (); 1205af245d11STodd Fiala error.SetErrorStringWithFormat ("failed to register the native delegate"); 1206af245d11STodd Fiala return error; 1207af245d11STodd Fiala } 1208af245d11STodd Fiala 1209af245d11STodd Fiala reinterpret_cast<NativeProcessLinux*> (native_process_sp.get ())->LaunchInferior ( 1210af245d11STodd Fiala exe_module, 1211af245d11STodd Fiala launch_info.GetArguments ().GetConstArgumentVector (), 1212af245d11STodd Fiala launch_info.GetEnvironmentEntries ().GetConstArgumentVector (), 1213af245d11STodd Fiala stdin_path, 1214af245d11STodd Fiala stdout_path, 1215af245d11STodd Fiala stderr_path, 1216af245d11STodd Fiala working_dir, 12170bce1b67STodd Fiala launch_info, 1218af245d11STodd Fiala error); 1219af245d11STodd Fiala 1220af245d11STodd Fiala if (error.Fail ()) 1221af245d11STodd Fiala { 1222af245d11STodd Fiala native_process_sp.reset (); 1223af245d11STodd Fiala if (log) 1224af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to launch process: %s", __FUNCTION__, error.AsCString ()); 1225af245d11STodd Fiala return error; 1226af245d11STodd Fiala } 1227af245d11STodd Fiala 1228af245d11STodd Fiala launch_info.SetProcessID (native_process_sp->GetID ()); 1229af245d11STodd Fiala 1230af245d11STodd Fiala return error; 1231af245d11STodd Fiala } 1232af245d11STodd Fiala 1233af245d11STodd Fiala lldb_private::Error 1234af245d11STodd Fiala NativeProcessLinux::AttachToProcess ( 1235af245d11STodd Fiala lldb::pid_t pid, 1236af245d11STodd Fiala lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate, 1237af245d11STodd Fiala NativeProcessProtocolSP &native_process_sp) 1238af245d11STodd Fiala { 1239af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1240af245d11STodd Fiala if (log && log->GetMask ().Test (POSIX_LOG_VERBOSE)) 1241af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(pid = %" PRIi64 ")", __FUNCTION__, pid); 1242af245d11STodd Fiala 1243af245d11STodd Fiala // Grab the current platform architecture. This should be Linux, 1244af245d11STodd Fiala // since this code is only intended to run on a Linux host. 1245615eb7e6SGreg Clayton PlatformSP platform_sp (Platform::GetHostPlatform ()); 1246af245d11STodd Fiala if (!platform_sp) 1247af245d11STodd Fiala return Error("failed to get a valid default platform"); 1248af245d11STodd Fiala 1249af245d11STodd Fiala // Retrieve the architecture for the running process. 1250af245d11STodd Fiala ArchSpec process_arch; 1251af245d11STodd Fiala Error error = ResolveProcessArchitecture (pid, *platform_sp.get (), process_arch); 1252af245d11STodd Fiala if (!error.Success ()) 1253af245d11STodd Fiala return error; 1254af245d11STodd Fiala 12551339b5e8SOleksiy Vyalov std::shared_ptr<NativeProcessLinux> native_process_linux_sp (new NativeProcessLinux ()); 1256af245d11STodd Fiala 12571339b5e8SOleksiy Vyalov if (!native_process_linux_sp->RegisterNativeDelegate (native_delegate)) 1258af245d11STodd Fiala { 1259af245d11STodd Fiala error.SetErrorStringWithFormat ("failed to register the native delegate"); 1260af245d11STodd Fiala return error; 1261af245d11STodd Fiala } 1262af245d11STodd Fiala 12631339b5e8SOleksiy Vyalov native_process_linux_sp->AttachToInferior (pid, error); 1264af245d11STodd Fiala if (!error.Success ()) 1265af245d11STodd Fiala return error; 1266af245d11STodd Fiala 12671339b5e8SOleksiy Vyalov native_process_sp = native_process_linux_sp; 1268af245d11STodd Fiala return error; 1269af245d11STodd Fiala } 1270af245d11STodd Fiala 1271af245d11STodd Fiala // ----------------------------------------------------------------------------- 1272af245d11STodd Fiala // Public Instance Methods 1273af245d11STodd Fiala // ----------------------------------------------------------------------------- 1274af245d11STodd Fiala 1275af245d11STodd Fiala NativeProcessLinux::NativeProcessLinux () : 1276af245d11STodd Fiala NativeProcessProtocol (LLDB_INVALID_PROCESS_ID), 1277af245d11STodd Fiala m_arch (), 1278fa03ad2eSChaoren Lin m_operation_thread (), 1279fa03ad2eSChaoren Lin m_monitor_thread (), 1280af245d11STodd Fiala m_operation (nullptr), 1281af245d11STodd Fiala m_operation_mutex (), 1282af245d11STodd Fiala m_operation_pending (), 1283af245d11STodd Fiala m_operation_done (), 1284af245d11STodd Fiala m_supports_mem_region (eLazyBoolCalculate), 1285af245d11STodd Fiala m_mem_region_cache (), 1286fa03ad2eSChaoren Lin m_mem_region_cache_mutex (), 1287fa03ad2eSChaoren Lin m_coordinator_up (new ThreadStateCoordinator (GetThreadLoggerFunction ())), 1288fa03ad2eSChaoren Lin m_coordinator_thread () 1289af245d11STodd Fiala { 1290af245d11STodd Fiala } 1291af245d11STodd Fiala 1292af245d11STodd Fiala //------------------------------------------------------------------------------ 1293af245d11STodd Fiala /// The basic design of the NativeProcessLinux is built around two threads. 1294af245d11STodd Fiala /// 1295af245d11STodd Fiala /// One thread (@see SignalThread) simply blocks on a call to waitpid() looking 1296af245d11STodd Fiala /// for changes in the debugee state. When a change is detected a 1297af245d11STodd Fiala /// ProcessMessage is sent to the associated ProcessLinux instance. This thread 1298af245d11STodd Fiala /// "drives" state changes in the debugger. 1299af245d11STodd Fiala /// 1300af245d11STodd Fiala /// The second thread (@see OperationThread) is responsible for two things 1) 1301af245d11STodd Fiala /// launching or attaching to the inferior process, and then 2) servicing 1302af245d11STodd Fiala /// operations such as register reads/writes, stepping, etc. See the comments 1303af245d11STodd Fiala /// on the Operation class for more info as to why this is needed. 1304af245d11STodd Fiala void 1305af245d11STodd Fiala NativeProcessLinux::LaunchInferior ( 1306af245d11STodd Fiala Module *module, 1307af245d11STodd Fiala const char *argv[], 1308af245d11STodd Fiala const char *envp[], 130975f47c3aSTodd Fiala const std::string &stdin_path, 131075f47c3aSTodd Fiala const std::string &stdout_path, 131175f47c3aSTodd Fiala const std::string &stderr_path, 1312af245d11STodd Fiala const char *working_dir, 13130bce1b67STodd Fiala const lldb_private::ProcessLaunchInfo &launch_info, 1314af245d11STodd Fiala lldb_private::Error &error) 1315af245d11STodd Fiala { 1316af245d11STodd Fiala if (module) 1317af245d11STodd Fiala m_arch = module->GetArchitecture (); 1318af245d11STodd Fiala 1319af245d11STodd Fiala SetState (eStateLaunching); 1320af245d11STodd Fiala 1321af245d11STodd Fiala std::unique_ptr<LaunchArgs> args( 1322af245d11STodd Fiala new LaunchArgs( 1323af245d11STodd Fiala this, module, argv, envp, 1324af245d11STodd Fiala stdin_path, stdout_path, stderr_path, 13250bce1b67STodd Fiala working_dir, launch_info)); 1326af245d11STodd Fiala 1327af245d11STodd Fiala sem_init (&m_operation_pending, 0, 0); 1328af245d11STodd Fiala sem_init (&m_operation_done, 0, 0); 1329af245d11STodd Fiala 1330af245d11STodd Fiala StartLaunchOpThread (args.get(), error); 1331af245d11STodd Fiala if (!error.Success ()) 1332af245d11STodd Fiala return; 1333af245d11STodd Fiala 1334fa03ad2eSChaoren Lin error = StartCoordinatorThread (); 1335fa03ad2eSChaoren Lin if (!error.Success ()) 1336fa03ad2eSChaoren Lin return; 1337fa03ad2eSChaoren Lin 1338af245d11STodd Fiala WAIT_AGAIN: 1339af245d11STodd Fiala // Wait for the operation thread to initialize. 1340af245d11STodd Fiala if (sem_wait(&args->m_semaphore)) 1341af245d11STodd Fiala { 1342af245d11STodd Fiala if (errno == EINTR) 1343af245d11STodd Fiala goto WAIT_AGAIN; 1344af245d11STodd Fiala else 1345af245d11STodd Fiala { 1346af245d11STodd Fiala error.SetErrorToErrno(); 1347af245d11STodd Fiala return; 1348af245d11STodd Fiala } 1349af245d11STodd Fiala } 1350af245d11STodd Fiala 1351af245d11STodd Fiala // Check that the launch was a success. 1352af245d11STodd Fiala if (!args->m_error.Success()) 1353af245d11STodd Fiala { 1354af245d11STodd Fiala StopOpThread(); 1355fa03ad2eSChaoren Lin StopCoordinatorThread (); 1356af245d11STodd Fiala error = args->m_error; 1357af245d11STodd Fiala return; 1358af245d11STodd Fiala } 1359af245d11STodd Fiala 1360af245d11STodd Fiala // Finally, start monitoring the child process for change in state. 1361af245d11STodd Fiala m_monitor_thread = Host::StartMonitoringChildProcess( 1362af245d11STodd Fiala NativeProcessLinux::MonitorCallback, this, GetID(), true); 1363acee96aeSZachary Turner if (!m_monitor_thread.IsJoinable()) 1364af245d11STodd Fiala { 1365af245d11STodd Fiala error.SetErrorToGenericError(); 1366af245d11STodd Fiala error.SetErrorString ("Process attach failed to create monitor thread for NativeProcessLinux::MonitorCallback."); 1367af245d11STodd Fiala return; 1368af245d11STodd Fiala } 1369af245d11STodd Fiala } 1370af245d11STodd Fiala 1371af245d11STodd Fiala void 1372af245d11STodd Fiala NativeProcessLinux::AttachToInferior (lldb::pid_t pid, lldb_private::Error &error) 1373af245d11STodd Fiala { 1374af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1375af245d11STodd Fiala if (log) 1376af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ")", __FUNCTION__, pid); 1377af245d11STodd Fiala 1378af245d11STodd Fiala // We can use the Host for everything except the ResolveExecutable portion. 1379615eb7e6SGreg Clayton PlatformSP platform_sp = Platform::GetHostPlatform (); 1380af245d11STodd Fiala if (!platform_sp) 1381af245d11STodd Fiala { 1382af245d11STodd Fiala if (log) 1383af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 "): no default platform set", __FUNCTION__, pid); 1384af245d11STodd Fiala error.SetErrorString ("no default platform available"); 138550d60be3SShawn Best return; 1386af245d11STodd Fiala } 1387af245d11STodd Fiala 1388af245d11STodd Fiala // Gather info about the process. 1389af245d11STodd Fiala ProcessInstanceInfo process_info; 139050d60be3SShawn Best if (!platform_sp->GetProcessInfo (pid, process_info)) 139150d60be3SShawn Best { 139250d60be3SShawn Best if (log) 139350d60be3SShawn Best log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 "): failed to get process info", __FUNCTION__, pid); 139450d60be3SShawn Best error.SetErrorString ("failed to get process info"); 139550d60be3SShawn Best return; 139650d60be3SShawn Best } 1397af245d11STodd Fiala 1398af245d11STodd Fiala // Resolve the executable module 1399af245d11STodd Fiala ModuleSP exe_module_sp; 1400af245d11STodd Fiala FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths()); 1401e56f6dceSChaoren Lin ModuleSpec exe_module_spec(process_info.GetExecutableFile(), process_info.GetArchitecture()); 14026edef204SOleksiy Vyalov error = platform_sp->ResolveExecutable(exe_module_spec, exe_module_sp, 1403af245d11STodd Fiala executable_search_paths.GetSize() ? &executable_search_paths : NULL); 1404af245d11STodd Fiala if (!error.Success()) 1405af245d11STodd Fiala return; 1406af245d11STodd Fiala 1407af245d11STodd Fiala // Set the architecture to the exe architecture. 1408af245d11STodd Fiala m_arch = exe_module_sp->GetArchitecture(); 1409af245d11STodd Fiala if (log) 1410af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ") detected architecture %s", __FUNCTION__, pid, m_arch.GetArchitectureName ()); 1411af245d11STodd Fiala 1412af245d11STodd Fiala m_pid = pid; 1413af245d11STodd Fiala SetState(eStateAttaching); 1414af245d11STodd Fiala 1415af245d11STodd Fiala sem_init (&m_operation_pending, 0, 0); 1416af245d11STodd Fiala sem_init (&m_operation_done, 0, 0); 1417af245d11STodd Fiala 1418af245d11STodd Fiala std::unique_ptr<AttachArgs> args (new AttachArgs (this, pid)); 1419af245d11STodd Fiala 1420af245d11STodd Fiala StartAttachOpThread(args.get (), error); 1421af245d11STodd Fiala if (!error.Success ()) 1422af245d11STodd Fiala return; 1423af245d11STodd Fiala 1424fa03ad2eSChaoren Lin error = StartCoordinatorThread (); 1425fa03ad2eSChaoren Lin if (!error.Success ()) 1426fa03ad2eSChaoren Lin return; 1427fa03ad2eSChaoren Lin 1428af245d11STodd Fiala WAIT_AGAIN: 1429af245d11STodd Fiala // Wait for the operation thread to initialize. 1430af245d11STodd Fiala if (sem_wait (&args->m_semaphore)) 1431af245d11STodd Fiala { 1432af245d11STodd Fiala if (errno == EINTR) 1433af245d11STodd Fiala goto WAIT_AGAIN; 1434af245d11STodd Fiala else 1435af245d11STodd Fiala { 1436af245d11STodd Fiala error.SetErrorToErrno (); 1437af245d11STodd Fiala return; 1438af245d11STodd Fiala } 1439af245d11STodd Fiala } 1440af245d11STodd Fiala 1441af245d11STodd Fiala // Check that the attach was a success. 1442af245d11STodd Fiala if (!args->m_error.Success ()) 1443af245d11STodd Fiala { 1444af245d11STodd Fiala StopOpThread (); 1445fa03ad2eSChaoren Lin StopCoordinatorThread (); 1446af245d11STodd Fiala error = args->m_error; 1447af245d11STodd Fiala return; 1448af245d11STodd Fiala } 1449af245d11STodd Fiala 1450af245d11STodd Fiala // Finally, start monitoring the child process for change in state. 1451af245d11STodd Fiala m_monitor_thread = Host::StartMonitoringChildProcess ( 1452af245d11STodd Fiala NativeProcessLinux::MonitorCallback, this, GetID (), true); 1453acee96aeSZachary Turner if (!m_monitor_thread.IsJoinable()) 1454af245d11STodd Fiala { 1455af245d11STodd Fiala error.SetErrorToGenericError (); 1456af245d11STodd Fiala error.SetErrorString ("Process attach failed to create monitor thread for NativeProcessLinux::MonitorCallback."); 1457af245d11STodd Fiala return; 1458af245d11STodd Fiala } 1459af245d11STodd Fiala } 1460af245d11STodd Fiala 14618bc34f4dSOleksiy Vyalov void 14628bc34f4dSOleksiy Vyalov NativeProcessLinux::Terminate () 1463af245d11STodd Fiala { 1464af245d11STodd Fiala StopMonitor(); 1465af245d11STodd Fiala } 1466af245d11STodd Fiala 1467af245d11STodd Fiala //------------------------------------------------------------------------------ 1468af245d11STodd Fiala // Thread setup and tear down. 1469af245d11STodd Fiala 1470af245d11STodd Fiala void 1471af245d11STodd Fiala NativeProcessLinux::StartLaunchOpThread(LaunchArgs *args, Error &error) 1472af245d11STodd Fiala { 1473af245d11STodd Fiala static const char *g_thread_name = "lldb.process.nativelinux.operation"; 1474af245d11STodd Fiala 1475acee96aeSZachary Turner if (m_operation_thread.IsJoinable()) 1476af245d11STodd Fiala return; 1477af245d11STodd Fiala 147839de3110SZachary Turner m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error); 1479af245d11STodd Fiala } 1480af245d11STodd Fiala 1481af245d11STodd Fiala void * 1482af245d11STodd Fiala NativeProcessLinux::LaunchOpThread(void *arg) 1483af245d11STodd Fiala { 1484af245d11STodd Fiala LaunchArgs *args = static_cast<LaunchArgs*>(arg); 1485af245d11STodd Fiala 1486af245d11STodd Fiala if (!Launch(args)) { 1487af245d11STodd Fiala sem_post(&args->m_semaphore); 1488af245d11STodd Fiala return NULL; 1489af245d11STodd Fiala } 1490af245d11STodd Fiala 1491af245d11STodd Fiala ServeOperation(args); 1492af245d11STodd Fiala return NULL; 1493af245d11STodd Fiala } 1494af245d11STodd Fiala 1495af245d11STodd Fiala bool 1496af245d11STodd Fiala NativeProcessLinux::Launch(LaunchArgs *args) 1497af245d11STodd Fiala { 14980bce1b67STodd Fiala assert (args && "null args"); 14990bce1b67STodd Fiala if (!args) 15000bce1b67STodd Fiala return false; 15010bce1b67STodd Fiala 1502af245d11STodd Fiala NativeProcessLinux *monitor = args->m_monitor; 1503af245d11STodd Fiala assert (monitor && "monitor is NULL"); 1504af245d11STodd Fiala 1505af245d11STodd Fiala const char **argv = args->m_argv; 1506af245d11STodd Fiala const char **envp = args->m_envp; 1507af245d11STodd Fiala const char *working_dir = args->m_working_dir; 1508af245d11STodd Fiala 1509af245d11STodd Fiala lldb_utility::PseudoTerminal terminal; 1510af245d11STodd Fiala const size_t err_len = 1024; 1511af245d11STodd Fiala char err_str[err_len]; 1512af245d11STodd Fiala lldb::pid_t pid; 1513af245d11STodd Fiala NativeThreadProtocolSP thread_sp; 1514af245d11STodd Fiala 1515af245d11STodd Fiala lldb::ThreadSP inferior; 1516af245d11STodd Fiala 1517af245d11STodd Fiala // Propagate the environment if one is not supplied. 1518af245d11STodd Fiala if (envp == NULL || envp[0] == NULL) 1519af245d11STodd Fiala envp = const_cast<const char **>(environ); 1520af245d11STodd Fiala 1521af245d11STodd Fiala if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t> (-1)) 1522af245d11STodd Fiala { 1523af245d11STodd Fiala args->m_error.SetErrorToGenericError(); 1524af245d11STodd Fiala args->m_error.SetErrorString("Process fork failed."); 152575f47c3aSTodd Fiala return false; 1526af245d11STodd Fiala } 1527af245d11STodd Fiala 1528af245d11STodd Fiala // Recognized child exit status codes. 1529af245d11STodd Fiala enum { 1530af245d11STodd Fiala ePtraceFailed = 1, 1531af245d11STodd Fiala eDupStdinFailed, 1532af245d11STodd Fiala eDupStdoutFailed, 1533af245d11STodd Fiala eDupStderrFailed, 1534af245d11STodd Fiala eChdirFailed, 1535af245d11STodd Fiala eExecFailed, 1536af245d11STodd Fiala eSetGidFailed 1537af245d11STodd Fiala }; 1538af245d11STodd Fiala 1539af245d11STodd Fiala // Child process. 1540af245d11STodd Fiala if (pid == 0) 1541af245d11STodd Fiala { 154275f47c3aSTodd Fiala // FIXME consider opening a pipe between parent/child and have this forked child 154375f47c3aSTodd Fiala // send log info to parent re: launch status, in place of the log lines removed here. 1544af245d11STodd Fiala 154575f47c3aSTodd Fiala // Start tracing this child that is about to exec. 154697ccc294SChaoren Lin PTRACE(PTRACE_TRACEME, 0, nullptr, nullptr, 0, args->m_error); 154797ccc294SChaoren Lin if (args->m_error.Fail()) 1548af245d11STodd Fiala exit(ePtraceFailed); 1549af245d11STodd Fiala 1550493c3a12SPavel Labath // terminal has already dupped the tty descriptors to stdin/out/err. 1551493c3a12SPavel Labath // This closes original fd from which they were copied (and avoids 1552493c3a12SPavel Labath // leaking descriptors to the debugged process. 1553493c3a12SPavel Labath terminal.CloseSlaveFileDescriptor(); 1554493c3a12SPavel Labath 1555af245d11STodd Fiala // Do not inherit setgid powers. 1556af245d11STodd Fiala if (setgid(getgid()) != 0) 1557af245d11STodd Fiala exit(eSetGidFailed); 1558af245d11STodd Fiala 1559af245d11STodd Fiala // Attempt to have our own process group. 1560af245d11STodd Fiala if (setpgid(0, 0) != 0) 1561af245d11STodd Fiala { 156275f47c3aSTodd Fiala // FIXME log that this failed. This is common. 1563af245d11STodd Fiala // Don't allow this to prevent an inferior exec. 1564af245d11STodd Fiala } 1565af245d11STodd Fiala 1566af245d11STodd Fiala // Dup file descriptors if needed. 156775f47c3aSTodd Fiala if (!args->m_stdin_path.empty ()) 156875f47c3aSTodd Fiala if (!DupDescriptor(args->m_stdin_path.c_str (), STDIN_FILENO, O_RDONLY)) 1569af245d11STodd Fiala exit(eDupStdinFailed); 1570af245d11STodd Fiala 157175f47c3aSTodd Fiala if (!args->m_stdout_path.empty ()) 157214f4476aSTamas Berghammer if (!DupDescriptor(args->m_stdout_path.c_str (), STDOUT_FILENO, O_WRONLY | O_CREAT | O_TRUNC)) 1573af245d11STodd Fiala exit(eDupStdoutFailed); 1574af245d11STodd Fiala 157575f47c3aSTodd Fiala if (!args->m_stderr_path.empty ()) 157614f4476aSTamas Berghammer if (!DupDescriptor(args->m_stderr_path.c_str (), STDERR_FILENO, O_WRONLY | O_CREAT | O_TRUNC)) 1577af245d11STodd Fiala exit(eDupStderrFailed); 1578af245d11STodd Fiala 1579af245d11STodd Fiala // Change working directory 1580af245d11STodd Fiala if (working_dir != NULL && working_dir[0]) 1581af245d11STodd Fiala if (0 != ::chdir(working_dir)) 1582af245d11STodd Fiala exit(eChdirFailed); 1583af245d11STodd Fiala 15840bce1b67STodd Fiala // Disable ASLR if requested. 15850bce1b67STodd Fiala if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR)) 15860bce1b67STodd Fiala { 15870bce1b67STodd Fiala const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS); 15880bce1b67STodd Fiala if (old_personality == -1) 15890bce1b67STodd Fiala { 159075f47c3aSTodd Fiala // Can't retrieve Linux personality. Cannot disable ASLR. 15910bce1b67STodd Fiala } 15920bce1b67STodd Fiala else 15930bce1b67STodd Fiala { 15940bce1b67STodd Fiala const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality); 15950bce1b67STodd Fiala if (new_personality == -1) 15960bce1b67STodd Fiala { 159775f47c3aSTodd Fiala // Disabling ASLR failed. 15980bce1b67STodd Fiala } 15990bce1b67STodd Fiala else 16000bce1b67STodd Fiala { 160175f47c3aSTodd Fiala // Disabling ASLR succeeded. 16020bce1b67STodd Fiala } 16030bce1b67STodd Fiala } 16040bce1b67STodd Fiala } 16050bce1b67STodd Fiala 160675f47c3aSTodd Fiala // Execute. We should never return... 1607af245d11STodd Fiala execve(argv[0], 1608af245d11STodd Fiala const_cast<char *const *>(argv), 1609af245d11STodd Fiala const_cast<char *const *>(envp)); 161075f47c3aSTodd Fiala 161175f47c3aSTodd Fiala // ...unless exec fails. In which case we definitely need to end the child here. 1612af245d11STodd Fiala exit(eExecFailed); 1613af245d11STodd Fiala } 1614af245d11STodd Fiala 161575f47c3aSTodd Fiala // 161675f47c3aSTodd Fiala // This is the parent code here. 161775f47c3aSTodd Fiala // 161875f47c3aSTodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 161975f47c3aSTodd Fiala 1620af245d11STodd Fiala // Wait for the child process to trap on its call to execve. 1621af245d11STodd Fiala ::pid_t wpid; 1622af245d11STodd Fiala int status; 1623af245d11STodd Fiala if ((wpid = waitpid(pid, &status, 0)) < 0) 1624af245d11STodd Fiala { 1625af245d11STodd Fiala args->m_error.SetErrorToErrno(); 1626af245d11STodd Fiala 1627af245d11STodd Fiala if (log) 1628af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s waitpid for inferior failed with %s", __FUNCTION__, args->m_error.AsCString ()); 1629af245d11STodd Fiala 1630af245d11STodd Fiala // Mark the inferior as invalid. 1631af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 1632af245d11STodd Fiala monitor->SetState (StateType::eStateInvalid); 1633af245d11STodd Fiala 163475f47c3aSTodd Fiala return false; 1635af245d11STodd Fiala } 1636af245d11STodd Fiala else if (WIFEXITED(status)) 1637af245d11STodd Fiala { 1638af245d11STodd Fiala // open, dup or execve likely failed for some reason. 1639af245d11STodd Fiala args->m_error.SetErrorToGenericError(); 1640af245d11STodd Fiala switch (WEXITSTATUS(status)) 1641af245d11STodd Fiala { 1642af245d11STodd Fiala case ePtraceFailed: 1643af245d11STodd Fiala args->m_error.SetErrorString("Child ptrace failed."); 1644af245d11STodd Fiala break; 1645af245d11STodd Fiala case eDupStdinFailed: 1646af245d11STodd Fiala args->m_error.SetErrorString("Child open stdin failed."); 1647af245d11STodd Fiala break; 1648af245d11STodd Fiala case eDupStdoutFailed: 1649af245d11STodd Fiala args->m_error.SetErrorString("Child open stdout failed."); 1650af245d11STodd Fiala break; 1651af245d11STodd Fiala case eDupStderrFailed: 1652af245d11STodd Fiala args->m_error.SetErrorString("Child open stderr failed."); 1653af245d11STodd Fiala break; 1654af245d11STodd Fiala case eChdirFailed: 1655af245d11STodd Fiala args->m_error.SetErrorString("Child failed to set working directory."); 1656af245d11STodd Fiala break; 1657af245d11STodd Fiala case eExecFailed: 1658af245d11STodd Fiala args->m_error.SetErrorString("Child exec failed."); 1659af245d11STodd Fiala break; 1660af245d11STodd Fiala case eSetGidFailed: 1661af245d11STodd Fiala args->m_error.SetErrorString("Child setgid failed."); 1662af245d11STodd Fiala break; 1663af245d11STodd Fiala default: 1664af245d11STodd Fiala args->m_error.SetErrorString("Child returned unknown exit status."); 1665af245d11STodd Fiala break; 1666af245d11STodd Fiala } 1667af245d11STodd Fiala 1668af245d11STodd Fiala if (log) 1669af245d11STodd Fiala { 1670af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior exited with status %d before issuing a STOP", 1671af245d11STodd Fiala __FUNCTION__, 1672af245d11STodd Fiala WEXITSTATUS(status)); 1673af245d11STodd Fiala } 1674af245d11STodd Fiala 1675af245d11STodd Fiala // Mark the inferior as invalid. 1676af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 1677af245d11STodd Fiala monitor->SetState (StateType::eStateInvalid); 1678af245d11STodd Fiala 167975f47c3aSTodd Fiala return false; 1680af245d11STodd Fiala } 1681af245d11STodd Fiala assert(WIFSTOPPED(status) && (wpid == static_cast< ::pid_t> (pid)) && 1682af245d11STodd Fiala "Could not sync with inferior process."); 1683af245d11STodd Fiala 1684af245d11STodd Fiala if (log) 1685af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior started, now in stopped state", __FUNCTION__); 1686af245d11STodd Fiala 168797ccc294SChaoren Lin args->m_error = SetDefaultPtraceOpts(pid); 168897ccc294SChaoren Lin if (args->m_error.Fail()) 1689af245d11STodd Fiala { 1690af245d11STodd Fiala if (log) 1691af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior failed to set default ptrace options: %s", 1692af245d11STodd Fiala __FUNCTION__, 1693af245d11STodd Fiala args->m_error.AsCString ()); 1694af245d11STodd Fiala 1695af245d11STodd Fiala // Mark the inferior as invalid. 1696af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 1697af245d11STodd Fiala monitor->SetState (StateType::eStateInvalid); 1698af245d11STodd Fiala 169975f47c3aSTodd Fiala return false; 1700af245d11STodd Fiala } 1701af245d11STodd Fiala 1702af245d11STodd Fiala // Release the master terminal descriptor and pass it off to the 1703af245d11STodd Fiala // NativeProcessLinux instance. Similarly stash the inferior pid. 1704af245d11STodd Fiala monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor(); 1705af245d11STodd Fiala monitor->m_pid = pid; 1706af245d11STodd Fiala 1707af245d11STodd Fiala // Set the terminal fd to be in non blocking mode (it simplifies the 1708af245d11STodd Fiala // implementation of ProcessLinux::GetSTDOUT to have a non-blocking 1709af245d11STodd Fiala // descriptor to read from). 1710af245d11STodd Fiala if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error)) 1711af245d11STodd Fiala { 1712af245d11STodd Fiala if (log) 1713af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior EnsureFDFlags failed for ensuring terminal O_NONBLOCK setting: %s", 1714af245d11STodd Fiala __FUNCTION__, 1715af245d11STodd Fiala args->m_error.AsCString ()); 1716af245d11STodd Fiala 1717af245d11STodd Fiala // Mark the inferior as invalid. 1718af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 1719af245d11STodd Fiala monitor->SetState (StateType::eStateInvalid); 1720af245d11STodd Fiala 172175f47c3aSTodd Fiala return false; 1722af245d11STodd Fiala } 1723af245d11STodd Fiala 1724af245d11STodd Fiala if (log) 1725af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() adding pid = %" PRIu64, __FUNCTION__, pid); 1726af245d11STodd Fiala 1727fa03ad2eSChaoren Lin thread_sp = monitor->AddThread (pid); 1728af245d11STodd Fiala assert (thread_sp && "AddThread() returned a nullptr thread"); 1729fa03ad2eSChaoren Lin monitor->NotifyThreadCreateStopped (pid); 1730af245d11STodd Fiala reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedBySignal (SIGSTOP); 1731af245d11STodd Fiala 1732af245d11STodd Fiala // Let our process instance know the thread has stopped. 1733fa03ad2eSChaoren Lin monitor->SetCurrentThreadID (thread_sp->GetID ()); 1734af245d11STodd Fiala monitor->SetState (StateType::eStateStopped); 1735af245d11STodd Fiala 1736af245d11STodd Fiala if (log) 1737af245d11STodd Fiala { 1738af245d11STodd Fiala if (args->m_error.Success ()) 1739af245d11STodd Fiala { 1740af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior launching succeeded", __FUNCTION__); 1741af245d11STodd Fiala } 1742af245d11STodd Fiala else 1743af245d11STodd Fiala { 1744af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior launching failed: %s", 1745af245d11STodd Fiala __FUNCTION__, 1746af245d11STodd Fiala args->m_error.AsCString ()); 1747af245d11STodd Fiala } 1748af245d11STodd Fiala } 1749af245d11STodd Fiala return args->m_error.Success(); 1750af245d11STodd Fiala } 1751af245d11STodd Fiala 1752af245d11STodd Fiala void 1753af245d11STodd Fiala NativeProcessLinux::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error) 1754af245d11STodd Fiala { 1755af245d11STodd Fiala static const char *g_thread_name = "lldb.process.linux.operation"; 1756af245d11STodd Fiala 1757acee96aeSZachary Turner if (m_operation_thread.IsJoinable()) 1758af245d11STodd Fiala return; 1759af245d11STodd Fiala 176039de3110SZachary Turner m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error); 1761af245d11STodd Fiala } 1762af245d11STodd Fiala 1763af245d11STodd Fiala void * 1764af245d11STodd Fiala NativeProcessLinux::AttachOpThread(void *arg) 1765af245d11STodd Fiala { 1766af245d11STodd Fiala AttachArgs *args = static_cast<AttachArgs*>(arg); 1767af245d11STodd Fiala 1768af245d11STodd Fiala if (!Attach(args)) { 1769af245d11STodd Fiala sem_post(&args->m_semaphore); 1770fa03ad2eSChaoren Lin return nullptr; 1771af245d11STodd Fiala } 1772af245d11STodd Fiala 1773af245d11STodd Fiala ServeOperation(args); 1774fa03ad2eSChaoren Lin return nullptr; 1775af245d11STodd Fiala } 1776af245d11STodd Fiala 1777af245d11STodd Fiala bool 1778af245d11STodd Fiala NativeProcessLinux::Attach(AttachArgs *args) 1779af245d11STodd Fiala { 1780af245d11STodd Fiala lldb::pid_t pid = args->m_pid; 1781af245d11STodd Fiala 1782af245d11STodd Fiala NativeProcessLinux *monitor = args->m_monitor; 1783af245d11STodd Fiala lldb::ThreadSP inferior; 1784af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1785af245d11STodd Fiala 1786af245d11STodd Fiala // Use a map to keep track of the threads which we have attached/need to attach. 1787af245d11STodd Fiala Host::TidMap tids_to_attach; 1788af245d11STodd Fiala if (pid <= 1) 1789af245d11STodd Fiala { 1790af245d11STodd Fiala args->m_error.SetErrorToGenericError(); 1791af245d11STodd Fiala args->m_error.SetErrorString("Attaching to process 1 is not allowed."); 1792af245d11STodd Fiala goto FINISH; 1793af245d11STodd Fiala } 1794af245d11STodd Fiala 1795af245d11STodd Fiala while (Host::FindProcessThreads(pid, tids_to_attach)) 1796af245d11STodd Fiala { 1797af245d11STodd Fiala for (Host::TidMap::iterator it = tids_to_attach.begin(); 1798af245d11STodd Fiala it != tids_to_attach.end();) 1799af245d11STodd Fiala { 1800af245d11STodd Fiala if (it->second == false) 1801af245d11STodd Fiala { 1802af245d11STodd Fiala lldb::tid_t tid = it->first; 1803af245d11STodd Fiala 1804af245d11STodd Fiala // Attach to the requested process. 1805af245d11STodd Fiala // An attach will cause the thread to stop with a SIGSTOP. 180697ccc294SChaoren Lin PTRACE(PTRACE_ATTACH, tid, nullptr, nullptr, 0, args->m_error); 180797ccc294SChaoren Lin if (args->m_error.Fail()) 1808af245d11STodd Fiala { 1809af245d11STodd Fiala // No such thread. The thread may have exited. 1810af245d11STodd Fiala // More error handling may be needed. 181197ccc294SChaoren Lin if (args->m_error.GetError() == ESRCH) 1812af245d11STodd Fiala { 1813af245d11STodd Fiala it = tids_to_attach.erase(it); 1814af245d11STodd Fiala continue; 1815af245d11STodd Fiala } 1816af245d11STodd Fiala else 1817af245d11STodd Fiala goto FINISH; 1818af245d11STodd Fiala } 1819af245d11STodd Fiala 1820af245d11STodd Fiala int status; 1821af245d11STodd Fiala // Need to use __WALL otherwise we receive an error with errno=ECHLD 1822af245d11STodd Fiala // At this point we should have a thread stopped if waitpid succeeds. 1823af245d11STodd Fiala if ((status = waitpid(tid, NULL, __WALL)) < 0) 1824af245d11STodd Fiala { 1825af245d11STodd Fiala // No such thread. The thread may have exited. 1826af245d11STodd Fiala // More error handling may be needed. 1827af245d11STodd Fiala if (errno == ESRCH) 1828af245d11STodd Fiala { 1829af245d11STodd Fiala it = tids_to_attach.erase(it); 1830af245d11STodd Fiala continue; 1831af245d11STodd Fiala } 1832af245d11STodd Fiala else 1833af245d11STodd Fiala { 1834af245d11STodd Fiala args->m_error.SetErrorToErrno(); 1835af245d11STodd Fiala goto FINISH; 1836af245d11STodd Fiala } 1837af245d11STodd Fiala } 1838af245d11STodd Fiala 183997ccc294SChaoren Lin args->m_error = SetDefaultPtraceOpts(tid); 184097ccc294SChaoren Lin if (args->m_error.Fail()) 1841af245d11STodd Fiala goto FINISH; 1842af245d11STodd Fiala 1843af245d11STodd Fiala 1844af245d11STodd Fiala if (log) 1845af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() adding tid = %" PRIu64, __FUNCTION__, tid); 1846af245d11STodd Fiala 1847af245d11STodd Fiala it->second = true; 1848af245d11STodd Fiala 1849af245d11STodd Fiala // Create the thread, mark it as stopped. 1850af245d11STodd Fiala NativeThreadProtocolSP thread_sp (monitor->AddThread (static_cast<lldb::tid_t> (tid))); 1851af245d11STodd Fiala assert (thread_sp && "AddThread() returned a nullptr"); 1852fa03ad2eSChaoren Lin 1853fa03ad2eSChaoren Lin // This will notify this is a new thread and tell the system it is stopped. 1854fa03ad2eSChaoren Lin monitor->NotifyThreadCreateStopped (tid); 1855af245d11STodd Fiala reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedBySignal (SIGSTOP); 1856af245d11STodd Fiala monitor->SetCurrentThreadID (thread_sp->GetID ()); 1857af245d11STodd Fiala } 1858af245d11STodd Fiala 1859af245d11STodd Fiala // move the loop forward 1860af245d11STodd Fiala ++it; 1861af245d11STodd Fiala } 1862af245d11STodd Fiala } 1863af245d11STodd Fiala 1864af245d11STodd Fiala if (tids_to_attach.size() > 0) 1865af245d11STodd Fiala { 1866af245d11STodd Fiala monitor->m_pid = pid; 1867af245d11STodd Fiala // Let our process instance know the thread has stopped. 1868af245d11STodd Fiala monitor->SetState (StateType::eStateStopped); 1869af245d11STodd Fiala } 1870af245d11STodd Fiala else 1871af245d11STodd Fiala { 1872af245d11STodd Fiala args->m_error.SetErrorToGenericError(); 1873af245d11STodd Fiala args->m_error.SetErrorString("No such process."); 1874af245d11STodd Fiala } 1875af245d11STodd Fiala 1876af245d11STodd Fiala FINISH: 1877af245d11STodd Fiala return args->m_error.Success(); 1878af245d11STodd Fiala } 1879af245d11STodd Fiala 188097ccc294SChaoren Lin Error 1881af245d11STodd Fiala NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) 1882af245d11STodd Fiala { 1883af245d11STodd Fiala long ptrace_opts = 0; 1884af245d11STodd Fiala 1885af245d11STodd Fiala // Have the child raise an event on exit. This is used to keep the child in 1886af245d11STodd Fiala // limbo until it is destroyed. 1887af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXIT; 1888af245d11STodd Fiala 1889af245d11STodd Fiala // Have the tracer trace threads which spawn in the inferior process. 1890af245d11STodd Fiala // TODO: if we want to support tracing the inferiors' child, add the 1891af245d11STodd Fiala // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK) 1892af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACECLONE; 1893af245d11STodd Fiala 1894af245d11STodd Fiala // Have the tracer notify us before execve returns 1895af245d11STodd Fiala // (needed to disable legacy SIGTRAP generation) 1896af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXEC; 1897af245d11STodd Fiala 189897ccc294SChaoren Lin Error error; 189997ccc294SChaoren Lin PTRACE(PTRACE_SETOPTIONS, pid, nullptr, (void*)ptrace_opts, 0, error); 190097ccc294SChaoren Lin return error; 1901af245d11STodd Fiala } 1902af245d11STodd Fiala 1903af245d11STodd Fiala static ExitType convert_pid_status_to_exit_type (int status) 1904af245d11STodd Fiala { 1905af245d11STodd Fiala if (WIFEXITED (status)) 1906af245d11STodd Fiala return ExitType::eExitTypeExit; 1907af245d11STodd Fiala else if (WIFSIGNALED (status)) 1908af245d11STodd Fiala return ExitType::eExitTypeSignal; 1909af245d11STodd Fiala else if (WIFSTOPPED (status)) 1910af245d11STodd Fiala return ExitType::eExitTypeStop; 1911af245d11STodd Fiala else 1912af245d11STodd Fiala { 1913af245d11STodd Fiala // We don't know what this is. 1914af245d11STodd Fiala return ExitType::eExitTypeInvalid; 1915af245d11STodd Fiala } 1916af245d11STodd Fiala } 1917af245d11STodd Fiala 1918af245d11STodd Fiala static int convert_pid_status_to_return_code (int status) 1919af245d11STodd Fiala { 1920af245d11STodd Fiala if (WIFEXITED (status)) 1921af245d11STodd Fiala return WEXITSTATUS (status); 1922af245d11STodd Fiala else if (WIFSIGNALED (status)) 1923af245d11STodd Fiala return WTERMSIG (status); 1924af245d11STodd Fiala else if (WIFSTOPPED (status)) 1925af245d11STodd Fiala return WSTOPSIG (status); 1926af245d11STodd Fiala else 1927af245d11STodd Fiala { 1928af245d11STodd Fiala // We don't know what this is. 1929af245d11STodd Fiala return ExitType::eExitTypeInvalid; 1930af245d11STodd Fiala } 1931af245d11STodd Fiala } 1932af245d11STodd Fiala 1933af245d11STodd Fiala // Main process monitoring waitpid-loop handler. 1934af245d11STodd Fiala bool 1935af245d11STodd Fiala NativeProcessLinux::MonitorCallback(void *callback_baton, 1936af245d11STodd Fiala lldb::pid_t pid, 1937af245d11STodd Fiala bool exited, 1938af245d11STodd Fiala int signal, 1939af245d11STodd Fiala int status) 1940af245d11STodd Fiala { 1941af245d11STodd Fiala Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); 1942af245d11STodd Fiala 1943af245d11STodd Fiala NativeProcessLinux *const process = static_cast<NativeProcessLinux*>(callback_baton); 1944af245d11STodd Fiala assert (process && "process is null"); 1945af245d11STodd Fiala if (!process) 1946af245d11STodd Fiala { 1947af245d11STodd Fiala if (log) 1948af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " callback_baton was null, can't determine process to use", __FUNCTION__, pid); 1949af245d11STodd Fiala return true; 1950af245d11STodd Fiala } 1951af245d11STodd Fiala 1952af245d11STodd Fiala // Certain activities differ based on whether the pid is the tid of the main thread. 1953af245d11STodd Fiala const bool is_main_thread = (pid == process->GetID ()); 1954af245d11STodd Fiala 1955af245d11STodd Fiala // Assume we keep monitoring by default. 1956af245d11STodd Fiala bool stop_monitoring = false; 1957af245d11STodd Fiala 1958af245d11STodd Fiala // Handle when the thread exits. 1959af245d11STodd Fiala if (exited) 1960af245d11STodd Fiala { 1961af245d11STodd Fiala if (log) 196286fd8e45SChaoren Lin log->Printf ("NativeProcessLinux::%s() got exit signal(%d) , tid = %" PRIu64 " (%s main thread)", __FUNCTION__, signal, pid, is_main_thread ? "is" : "is not"); 1963af245d11STodd Fiala 1964af245d11STodd Fiala // This is a thread that exited. Ensure we're not tracking it anymore. 1965af245d11STodd Fiala const bool thread_found = process->StopTrackingThread (pid); 1966af245d11STodd Fiala 1967fa03ad2eSChaoren Lin // Make sure the thread state coordinator knows about this. 1968fa03ad2eSChaoren Lin process->NotifyThreadDeath (pid); 1969fa03ad2eSChaoren Lin 1970af245d11STodd Fiala if (is_main_thread) 1971af245d11STodd Fiala { 1972af245d11STodd Fiala // We only set the exit status and notify the delegate if we haven't already set the process 1973af245d11STodd Fiala // state to an exited state. We normally should have received a SIGTRAP | (PTRACE_EVENT_EXIT << 8) 1974af245d11STodd Fiala // for the main thread. 1975fa03ad2eSChaoren Lin const bool already_notified = (process->GetState() == StateType::eStateExited) || (process->GetState () == StateType::eStateCrashed); 1976af245d11STodd Fiala if (!already_notified) 1977af245d11STodd Fiala { 1978af245d11STodd Fiala if (log) 1979af245d11STodd Fiala 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 (process->GetState ())); 1980af245d11STodd Fiala // The main thread exited. We're done monitoring. Report to delegate. 1981af245d11STodd Fiala process->SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); 1982af245d11STodd Fiala 1983af245d11STodd Fiala // Notify delegate that our process has exited. 1984af245d11STodd Fiala process->SetState (StateType::eStateExited, true); 1985af245d11STodd Fiala } 1986af245d11STodd Fiala else 1987af245d11STodd Fiala { 1988af245d11STodd Fiala if (log) 1989af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() tid = %" PRIu64 " main thread now exited (%s)", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found"); 1990af245d11STodd Fiala } 1991af245d11STodd Fiala return true; 1992af245d11STodd Fiala } 1993af245d11STodd Fiala else 1994af245d11STodd Fiala { 1995af245d11STodd Fiala // Do we want to report to the delegate in this case? I think not. If this was an orderly 1996af245d11STodd Fiala // thread exit, we would already have received the SIGTRAP | (PTRACE_EVENT_EXIT << 8) signal, 1997af245d11STodd Fiala // and we would have done an all-stop then. 1998af245d11STodd Fiala if (log) 1999af245d11STodd 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"); 2000af245d11STodd Fiala 2001af245d11STodd Fiala // Not the main thread, we keep going. 2002af245d11STodd Fiala return false; 2003af245d11STodd Fiala } 2004af245d11STodd Fiala } 2005af245d11STodd Fiala 2006af245d11STodd Fiala // Get details on the signal raised. 2007af245d11STodd Fiala siginfo_t info; 200897ccc294SChaoren Lin const auto err = process->GetSignalInfo(pid, &info); 200997ccc294SChaoren Lin if (err.Success()) 2010fa03ad2eSChaoren Lin { 2011fa03ad2eSChaoren Lin // We have retrieved the signal info. Dispatch appropriately. 2012fa03ad2eSChaoren Lin if (info.si_signo == SIGTRAP) 2013fa03ad2eSChaoren Lin process->MonitorSIGTRAP(&info, pid); 2014fa03ad2eSChaoren Lin else 2015fa03ad2eSChaoren Lin process->MonitorSignal(&info, pid, exited); 2016fa03ad2eSChaoren Lin 2017fa03ad2eSChaoren Lin stop_monitoring = false; 2018fa03ad2eSChaoren Lin } 2019fa03ad2eSChaoren Lin else 2020af245d11STodd Fiala { 202197ccc294SChaoren Lin if (err.GetError() == EINVAL) 2022af245d11STodd Fiala { 2023fa03ad2eSChaoren Lin // This is a group stop reception for this tid. 2024fa03ad2eSChaoren Lin if (log) 2025fa03ad2eSChaoren Lin log->Printf ("NativeThreadLinux::%s received a group stop for pid %" PRIu64 " tid %" PRIu64, __FUNCTION__, process->GetID (), pid); 2026fa03ad2eSChaoren Lin process->NotifyThreadStop (pid); 2027a9882ceeSTodd Fiala } 2028a9882ceeSTodd Fiala else 2029a9882ceeSTodd Fiala { 2030af245d11STodd Fiala // ptrace(GETSIGINFO) failed (but not due to group-stop). 2031af245d11STodd Fiala 2032af245d11STodd Fiala // A return value of ESRCH means the thread/process is no longer on the system, 2033af245d11STodd Fiala // so it was killed somehow outside of our control. Either way, we can't do anything 2034af245d11STodd Fiala // with it anymore. 2035af245d11STodd Fiala 2036af245d11STodd Fiala // We stop monitoring if it was the main thread. 2037af245d11STodd Fiala stop_monitoring = is_main_thread; 2038af245d11STodd Fiala 2039af245d11STodd Fiala // Stop tracking the metadata for the thread since it's entirely off the system now. 2040af245d11STodd Fiala const bool thread_found = process->StopTrackingThread (pid); 2041af245d11STodd Fiala 2042fa03ad2eSChaoren Lin // Make sure the thread state coordinator knows about this. 2043fa03ad2eSChaoren Lin process->NotifyThreadDeath (pid); 2044fa03ad2eSChaoren Lin 2045af245d11STodd Fiala if (log) 2046af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d (%s, %s, %s)", 204797ccc294SChaoren 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"); 2048af245d11STodd Fiala 2049af245d11STodd Fiala if (is_main_thread) 2050af245d11STodd Fiala { 2051af245d11STodd Fiala // Notify the delegate - our process is not available but appears to have been killed outside 2052af245d11STodd Fiala // our control. Is eStateExited the right exit state in this case? 2053af245d11STodd Fiala process->SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); 2054af245d11STodd Fiala process->SetState (StateType::eStateExited, true); 2055af245d11STodd Fiala } 2056af245d11STodd Fiala else 2057af245d11STodd Fiala { 2058af245d11STodd Fiala // This thread was pulled out from underneath us. Anything to do here? Do we want to do an all stop? 2059af245d11STodd Fiala if (log) 2060af245d11STodd Fiala 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__, process->GetID (), pid); 2061af245d11STodd Fiala } 2062af245d11STodd Fiala } 2063af245d11STodd Fiala } 2064af245d11STodd Fiala 2065af245d11STodd Fiala return stop_monitoring; 2066af245d11STodd Fiala } 2067af245d11STodd Fiala 2068af245d11STodd Fiala void 2069af245d11STodd Fiala NativeProcessLinux::MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid) 2070af245d11STodd Fiala { 2071af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2072af245d11STodd Fiala const bool is_main_thread = (pid == GetID ()); 2073af245d11STodd Fiala 2074af245d11STodd Fiala assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!"); 2075af245d11STodd Fiala if (!info) 2076af245d11STodd Fiala return; 2077af245d11STodd Fiala 20785830aa75STamas Berghammer Mutex::Locker locker (m_threads_mutex); 20795830aa75STamas Berghammer 2080af245d11STodd Fiala // See if we can find a thread for this signal. 2081af245d11STodd Fiala NativeThreadProtocolSP thread_sp = GetThreadByID (pid); 2082af245d11STodd Fiala if (!thread_sp) 2083af245d11STodd Fiala { 2084af245d11STodd Fiala if (log) 2085af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " no thread found for tid %" PRIu64, __FUNCTION__, GetID (), pid); 2086af245d11STodd Fiala } 2087af245d11STodd Fiala 2088af245d11STodd Fiala switch (info->si_code) 2089af245d11STodd Fiala { 2090af245d11STodd 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. 2091af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)): 2092af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)): 2093af245d11STodd Fiala 2094af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)): 2095af245d11STodd Fiala { 2096af245d11STodd Fiala lldb::tid_t tid = LLDB_INVALID_THREAD_ID; 2097af245d11STodd Fiala 2098fa03ad2eSChaoren Lin // The main thread is stopped here. 2099fa03ad2eSChaoren Lin if (thread_sp) 2100fa03ad2eSChaoren Lin reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedBySignal (SIGTRAP); 2101fa03ad2eSChaoren Lin NotifyThreadStop (pid); 2102fa03ad2eSChaoren Lin 2103af245d11STodd Fiala unsigned long event_message = 0; 210497ccc294SChaoren Lin if (GetEventMessage (pid, &event_message).Success()) 2105fa03ad2eSChaoren Lin { 2106af245d11STodd Fiala tid = static_cast<lldb::tid_t> (event_message); 2107af245d11STodd Fiala if (log) 2108af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " received thread creation event for tid %" PRIu64, __FUNCTION__, pid, tid); 2109af245d11STodd Fiala 2110af245d11STodd Fiala // If we don't track the thread yet: create it, mark as stopped. 2111af245d11STodd Fiala // If we do track it, this is the wait we needed. Now resume the new thread. 2112af245d11STodd Fiala // In all cases, resume the current (i.e. main process) thread. 2113511e5cdcSTodd Fiala bool created_now = false; 2114fa03ad2eSChaoren Lin NativeThreadProtocolSP new_thread_sp = GetOrCreateThread (tid, created_now); 2115fa03ad2eSChaoren Lin assert (new_thread_sp.get() && "failed to get or create the tracking data for newly created inferior thread"); 2116af245d11STodd Fiala 2117af245d11STodd Fiala // If the thread was already tracked, it means the created thread already received its SI_USER notification of creation. 2118511e5cdcSTodd Fiala if (!created_now) 2119af245d11STodd Fiala { 2120af245d11STodd Fiala // We can now resume the newly created thread since it is fully created. 2121fa03ad2eSChaoren Lin NotifyThreadCreateStopped (tid); 2122fa03ad2eSChaoren Lin m_coordinator_up->RequestThreadResume (tid, 212386fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 2124fa03ad2eSChaoren Lin { 2125fa03ad2eSChaoren Lin reinterpret_cast<NativeThreadLinux*> (new_thread_sp.get ())->SetRunning (); 212637c768caSChaoren Lin return Resume (tid_to_resume, LLDB_INVALID_SIGNAL_NUMBER); 2127fa03ad2eSChaoren Lin }, 2128fa03ad2eSChaoren Lin CoordinatorErrorHandler); 2129af245d11STodd Fiala } 2130af245d11STodd Fiala else 2131af245d11STodd Fiala { 2132af245d11STodd Fiala // Mark the thread as currently launching. Need to wait for SIGTRAP clone on the main thread before 2133af245d11STodd Fiala // this thread is ready to go. 2134fa03ad2eSChaoren Lin reinterpret_cast<NativeThreadLinux*> (new_thread_sp.get ())->SetLaunching (); 2135fa03ad2eSChaoren Lin } 2136fa03ad2eSChaoren Lin } 2137fa03ad2eSChaoren Lin else 2138fa03ad2eSChaoren Lin { 2139fa03ad2eSChaoren Lin if (log) 2140fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " received thread creation event but GetEventMessage failed so we don't know the new tid", __FUNCTION__, pid); 2141af245d11STodd Fiala } 2142af245d11STodd Fiala 2143af245d11STodd Fiala // In all cases, we can resume the main thread here. 2144fa03ad2eSChaoren Lin m_coordinator_up->RequestThreadResume (pid, 214586fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 2146fa03ad2eSChaoren Lin { 2147fa03ad2eSChaoren Lin reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetRunning (); 214837c768caSChaoren Lin return Resume (tid_to_resume, LLDB_INVALID_SIGNAL_NUMBER); 2149fa03ad2eSChaoren Lin }, 2150fa03ad2eSChaoren Lin CoordinatorErrorHandler); 2151fa03ad2eSChaoren Lin 2152af245d11STodd Fiala break; 2153af245d11STodd Fiala } 2154af245d11STodd Fiala 2155af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): 2156a9882ceeSTodd Fiala { 2157a9882ceeSTodd Fiala NativeThreadProtocolSP main_thread_sp; 2158af245d11STodd Fiala if (log) 2159af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP); 2160a9882ceeSTodd Fiala 2161fa03ad2eSChaoren Lin // The thread state coordinator needs to reset due to the exec. 2162fa03ad2eSChaoren Lin m_coordinator_up->ResetForExec (); 2163fa03ad2eSChaoren Lin 2164fa03ad2eSChaoren 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. 2165a9882ceeSTodd Fiala if (log) 2166a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s exec received, stop tracking all but main thread", __FUNCTION__); 2167a9882ceeSTodd Fiala 2168a9882ceeSTodd Fiala for (auto thread_sp : m_threads) 2169a9882ceeSTodd Fiala { 2170a9882ceeSTodd Fiala const bool is_main_thread = thread_sp && thread_sp->GetID () == GetID (); 2171a9882ceeSTodd Fiala if (is_main_thread) 2172a9882ceeSTodd Fiala { 2173a9882ceeSTodd Fiala main_thread_sp = thread_sp; 2174a9882ceeSTodd Fiala if (log) 2175a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s found main thread with tid %" PRIu64 ", keeping", __FUNCTION__, main_thread_sp->GetID ()); 2176a9882ceeSTodd Fiala } 2177a9882ceeSTodd Fiala else 2178a9882ceeSTodd Fiala { 2179fa03ad2eSChaoren Lin // Tell thread coordinator this thread is dead. 2180a9882ceeSTodd Fiala if (log) 2181a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s discarding non-main-thread tid %" PRIu64 " due to exec", __FUNCTION__, thread_sp->GetID ()); 2182a9882ceeSTodd Fiala } 2183a9882ceeSTodd Fiala } 2184a9882ceeSTodd Fiala 2185a9882ceeSTodd Fiala m_threads.clear (); 2186a9882ceeSTodd Fiala 2187a9882ceeSTodd Fiala if (main_thread_sp) 2188a9882ceeSTodd Fiala { 2189a9882ceeSTodd Fiala m_threads.push_back (main_thread_sp); 2190a9882ceeSTodd Fiala SetCurrentThreadID (main_thread_sp->GetID ()); 2191a9882ceeSTodd Fiala reinterpret_cast<NativeThreadLinux*>(main_thread_sp.get())->SetStoppedByExec (); 2192a9882ceeSTodd Fiala } 2193a9882ceeSTodd Fiala else 2194a9882ceeSTodd Fiala { 2195a9882ceeSTodd Fiala SetCurrentThreadID (LLDB_INVALID_THREAD_ID); 2196a9882ceeSTodd Fiala if (log) 2197a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 "no main thread found, discarded all threads, we're in a no-thread state!", __FUNCTION__, GetID ()); 2198a9882ceeSTodd Fiala } 2199a9882ceeSTodd Fiala 2200fa03ad2eSChaoren Lin // Tell coordinator about about the "new" (since exec) stopped main thread. 2201fa03ad2eSChaoren Lin const lldb::tid_t main_thread_tid = GetID (); 2202fa03ad2eSChaoren Lin NotifyThreadCreateStopped (main_thread_tid); 2203fa03ad2eSChaoren Lin 2204fa03ad2eSChaoren Lin // NOTE: ideally these next statements would execute at the same time as the coordinator thread create was executed. 2205fa03ad2eSChaoren Lin // Consider a handler that can execute when that happens. 2206a9882ceeSTodd Fiala // Let our delegate know we have just exec'd. 2207a9882ceeSTodd Fiala NotifyDidExec (); 2208a9882ceeSTodd Fiala 2209a9882ceeSTodd Fiala // If we have a main thread, indicate we are stopped. 2210a9882ceeSTodd Fiala assert (main_thread_sp && "exec called during ptraced process but no main thread metadata tracked"); 2211fa03ad2eSChaoren Lin 2212fa03ad2eSChaoren Lin // Let the process know we're stopped. 22135830aa75STamas Berghammer CallAfterRunningThreadsStop (pid, 22145830aa75STamas Berghammer [=] (lldb::tid_t signaling_tid) 22155830aa75STamas Berghammer { 22165830aa75STamas Berghammer SetState (StateType::eStateStopped, true); 22175830aa75STamas Berghammer }); 2218a9882ceeSTodd Fiala 2219af245d11STodd Fiala break; 2220a9882ceeSTodd Fiala } 2221af245d11STodd Fiala 2222af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): 2223af245d11STodd Fiala { 2224af245d11STodd Fiala // The inferior process or one of its threads is about to exit. 2225fa03ad2eSChaoren Lin 2226fa03ad2eSChaoren Lin // This thread is currently stopped. It's not actually dead yet, just about to be. 2227fa03ad2eSChaoren Lin NotifyThreadStop (pid); 2228fa03ad2eSChaoren Lin 2229af245d11STodd Fiala unsigned long data = 0; 223097ccc294SChaoren Lin if (GetEventMessage(pid, &data).Fail()) 2231af245d11STodd Fiala data = -1; 2232af245d11STodd Fiala 2233af245d11STodd Fiala if (log) 2234af245d11STodd Fiala { 2235af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received PTRACE_EVENT_EXIT, data = %lx (WIFEXITED=%s,WIFSIGNALED=%s), pid = %" PRIu64 " (%s)", 2236af245d11STodd Fiala __FUNCTION__, 2237af245d11STodd Fiala data, WIFEXITED (data) ? "true" : "false", WIFSIGNALED (data) ? "true" : "false", 2238af245d11STodd Fiala pid, 2239af245d11STodd Fiala is_main_thread ? "is main thread" : "not main thread"); 2240af245d11STodd Fiala } 2241af245d11STodd Fiala 2242af245d11STodd Fiala if (is_main_thread) 2243af245d11STodd Fiala { 2244af245d11STodd Fiala SetExitStatus (convert_pid_status_to_exit_type (data), convert_pid_status_to_return_code (data), nullptr, true); 224575f47c3aSTodd Fiala } 224675f47c3aSTodd Fiala 22479d617ba6SChaoren Lin const int signo = static_cast<int> (data); 2248fa03ad2eSChaoren Lin m_coordinator_up->RequestThreadResume (pid, 224986fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 2250fa03ad2eSChaoren Lin { 2251fa03ad2eSChaoren Lin reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetRunning (); 225237c768caSChaoren Lin return Resume (tid_to_resume, (supress_signal) ? LLDB_INVALID_SIGNAL_NUMBER : signo); 2253fa03ad2eSChaoren Lin }, 2254fa03ad2eSChaoren Lin CoordinatorErrorHandler); 2255af245d11STodd Fiala 2256af245d11STodd Fiala break; 2257af245d11STodd Fiala } 2258af245d11STodd Fiala 2259af245d11STodd Fiala case 0: 2260af245d11STodd Fiala case TRAP_TRACE: 2261af245d11STodd Fiala // We receive this on single stepping. 2262af245d11STodd Fiala if (log) 2263af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received trace event, pid = %" PRIu64 " (single stepping)", __FUNCTION__, pid); 2264af245d11STodd Fiala 226586fd8e45SChaoren Lin if (thread_sp) 226686fd8e45SChaoren Lin { 226728e57429SChaoren Lin reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedByTrace (); 226886fd8e45SChaoren Lin } 226986fd8e45SChaoren Lin 2270fa03ad2eSChaoren Lin // This thread is currently stopped. 2271fa03ad2eSChaoren Lin NotifyThreadStop (pid); 2272fa03ad2eSChaoren Lin 2273ae29d395SChaoren Lin // Here we don't have to request the rest of the threads to stop or request a deferred stop. 2274ae29d395SChaoren Lin // This would have already happened at the time the Resume() with step operation was signaled. 2275ae29d395SChaoren Lin // At this point, we just need to say we stopped, and the deferred notifcation will fire off 2276ae29d395SChaoren Lin // once all running threads have checked in as stopped. 227786fd8e45SChaoren Lin SetCurrentThreadID (pid); 227886fd8e45SChaoren Lin // Tell the process we have a stop (from software breakpoint). 22795830aa75STamas Berghammer CallAfterRunningThreadsStop (pid, 22805830aa75STamas Berghammer [=] (lldb::tid_t signaling_tid) 22815830aa75STamas Berghammer { 228286fd8e45SChaoren Lin SetState (StateType::eStateStopped, true); 22835830aa75STamas Berghammer }); 2284af245d11STodd Fiala break; 2285af245d11STodd Fiala 2286af245d11STodd Fiala case SI_KERNEL: 2287af245d11STodd Fiala case TRAP_BRKPT: 2288af245d11STodd Fiala if (log) 2289af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received breakpoint event, pid = %" PRIu64, __FUNCTION__, pid); 2290af245d11STodd Fiala 2291fa03ad2eSChaoren Lin // This thread is currently stopped. 2292fa03ad2eSChaoren Lin NotifyThreadStop (pid); 2293fa03ad2eSChaoren Lin 2294af245d11STodd Fiala // Mark the thread as stopped at breakpoint. 2295af245d11STodd Fiala if (thread_sp) 2296af245d11STodd Fiala { 229728e57429SChaoren Lin reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedByBreakpoint (); 2298af245d11STodd Fiala Error error = FixupBreakpointPCAsNeeded (thread_sp); 2299af245d11STodd Fiala if (error.Fail ()) 2300af245d11STodd Fiala { 2301af245d11STodd Fiala if (log) 2302af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 " fixup: %s", __FUNCTION__, pid, error.AsCString ()); 2303af245d11STodd Fiala } 2304af245d11STodd Fiala } 2305af245d11STodd Fiala else 2306af245d11STodd Fiala { 2307af245d11STodd Fiala if (log) 2308af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 ": warning, cannot process software breakpoint since no thread metadata", __FUNCTION__, pid); 2309af245d11STodd Fiala } 2310af245d11STodd Fiala 2311af245d11STodd Fiala 2312fa03ad2eSChaoren Lin // We need to tell all other running threads before we notify the delegate about this stop. 2313fa03ad2eSChaoren Lin CallAfterRunningThreadsStop (pid, 2314fa03ad2eSChaoren Lin [=](lldb::tid_t deferred_notification_tid) 2315fa03ad2eSChaoren Lin { 2316fa03ad2eSChaoren Lin SetCurrentThreadID (deferred_notification_tid); 2317fa03ad2eSChaoren Lin // Tell the process we have a stop (from software breakpoint). 2318af245d11STodd Fiala SetState (StateType::eStateStopped, true); 2319fa03ad2eSChaoren Lin }); 2320af245d11STodd Fiala break; 2321af245d11STodd Fiala 2322af245d11STodd Fiala case TRAP_HWBKPT: 2323af245d11STodd Fiala if (log) 2324af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received watchpoint event, pid = %" PRIu64, __FUNCTION__, pid); 2325af245d11STodd Fiala 2326fa03ad2eSChaoren Lin // This thread is currently stopped. 2327fa03ad2eSChaoren Lin NotifyThreadStop (pid); 2328fa03ad2eSChaoren Lin 2329af245d11STodd Fiala // Mark the thread as stopped at watchpoint. 2330af245d11STodd Fiala // The address is at (lldb::addr_t)info->si_addr if we need it. 2331af245d11STodd Fiala if (thread_sp) 233218fe6404SChaoren Lin reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedByWatchpoint (); 2333af245d11STodd Fiala else 2334af245d11STodd Fiala { 2335af245d11STodd Fiala if (log) 2336af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 ": warning, cannot process hardware breakpoint since no thread metadata", __FUNCTION__, GetID (), pid); 2337af245d11STodd Fiala } 2338af245d11STodd Fiala 2339fa03ad2eSChaoren Lin // We need to tell all other running threads before we notify the delegate about this stop. 2340fa03ad2eSChaoren Lin CallAfterRunningThreadsStop (pid, 2341fa03ad2eSChaoren Lin [=](lldb::tid_t deferred_notification_tid) 2342fa03ad2eSChaoren Lin { 2343fa03ad2eSChaoren Lin SetCurrentThreadID (deferred_notification_tid); 2344fa03ad2eSChaoren Lin // Tell the process we have a stop (from hardware breakpoint). 2345af245d11STodd Fiala SetState (StateType::eStateStopped, true); 2346fa03ad2eSChaoren Lin }); 2347af245d11STodd Fiala break; 2348af245d11STodd Fiala 2349af245d11STodd Fiala case SIGTRAP: 2350af245d11STodd Fiala case (SIGTRAP | 0x80): 2351af245d11STodd Fiala if (log) 2352fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s() received unknown SIGTRAP system call stop event, pid %" PRIu64 "tid %" PRIu64 ", resuming", __FUNCTION__, GetID (), pid); 2353fa03ad2eSChaoren Lin 2354fa03ad2eSChaoren Lin // This thread is currently stopped. 2355fa03ad2eSChaoren Lin NotifyThreadStop (pid); 2356fa03ad2eSChaoren Lin if (thread_sp) 2357fa03ad2eSChaoren Lin reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedBySignal (SIGTRAP); 2358fa03ad2eSChaoren Lin 2359fa03ad2eSChaoren Lin 2360af245d11STodd Fiala // Ignore these signals until we know more about them. 2361fa03ad2eSChaoren Lin m_coordinator_up->RequestThreadResume (pid, 236286fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 2363fa03ad2eSChaoren Lin { 2364fa03ad2eSChaoren Lin reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetRunning (); 236537c768caSChaoren Lin return Resume (tid_to_resume, LLDB_INVALID_SIGNAL_NUMBER); 2366fa03ad2eSChaoren Lin }, 2367fa03ad2eSChaoren Lin CoordinatorErrorHandler); 2368af245d11STodd Fiala break; 2369af245d11STodd Fiala 2370af245d11STodd Fiala default: 2371af245d11STodd Fiala assert(false && "Unexpected SIGTRAP code!"); 2372af245d11STodd Fiala if (log) 2373af245d11STodd 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))); 2374af245d11STodd Fiala break; 2375af245d11STodd Fiala 2376af245d11STodd Fiala } 2377af245d11STodd Fiala } 2378af245d11STodd Fiala 2379af245d11STodd Fiala void 2380af245d11STodd Fiala NativeProcessLinux::MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited) 2381af245d11STodd Fiala { 2382511e5cdcSTodd Fiala assert (info && "null info"); 2383511e5cdcSTodd Fiala if (!info) 2384511e5cdcSTodd Fiala return; 2385511e5cdcSTodd Fiala 2386511e5cdcSTodd Fiala const int signo = info->si_signo; 2387511e5cdcSTodd Fiala const bool is_from_llgs = info->si_pid == getpid (); 2388af245d11STodd Fiala 2389af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2390af245d11STodd Fiala 2391af245d11STodd Fiala // POSIX says that process behaviour is undefined after it ignores a SIGFPE, 2392af245d11STodd Fiala // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a 2393af245d11STodd Fiala // kill(2) or raise(3). Similarly for tgkill(2) on Linux. 2394af245d11STodd Fiala // 2395af245d11STodd Fiala // IOW, user generated signals never generate what we consider to be a 2396af245d11STodd Fiala // "crash". 2397af245d11STodd Fiala // 2398af245d11STodd Fiala // Similarly, ACK signals generated by this monitor. 2399af245d11STodd Fiala 24005830aa75STamas Berghammer Mutex::Locker locker (m_threads_mutex); 24015830aa75STamas Berghammer 2402af245d11STodd Fiala // See if we can find a thread for this signal. 2403af245d11STodd Fiala NativeThreadProtocolSP thread_sp = GetThreadByID (pid); 2404af245d11STodd Fiala if (!thread_sp) 2405af245d11STodd Fiala { 2406af245d11STodd Fiala if (log) 2407af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " no thread found for tid %" PRIu64, __FUNCTION__, GetID (), pid); 2408af245d11STodd Fiala } 2409af245d11STodd Fiala 2410af245d11STodd Fiala // Handle the signal. 2411af245d11STodd Fiala if (info->si_code == SI_TKILL || info->si_code == SI_USER) 2412af245d11STodd Fiala { 2413af245d11STodd Fiala if (log) 2414af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received signal %s (%d) with code %s, (siginfo pid = %d (%s), waitpid pid = %" PRIu64 ")", 2415af245d11STodd Fiala __FUNCTION__, 2416af245d11STodd Fiala GetUnixSignals ().GetSignalAsCString (signo), 2417af245d11STodd Fiala signo, 2418af245d11STodd Fiala (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"), 2419af245d11STodd Fiala info->si_pid, 2420511e5cdcSTodd Fiala is_from_llgs ? "from llgs" : "not from llgs", 2421af245d11STodd Fiala pid); 242258a2f669STodd Fiala } 2423af245d11STodd Fiala 242458a2f669STodd Fiala // Check for new thread notification. 242558a2f669STodd Fiala if ((info->si_pid == 0) && (info->si_code == SI_USER)) 2426af245d11STodd Fiala { 2427af245d11STodd Fiala // A new thread creation is being signaled. This is one of two parts that come in 2428af245d11STodd Fiala // a non-deterministic order. pid is the thread id. 2429af245d11STodd Fiala if (log) 2430af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 " tid %" PRIu64 ": new thread notification", 2431af245d11STodd Fiala __FUNCTION__, GetID (), pid); 2432af245d11STodd Fiala 2433af245d11STodd Fiala // Did we already create the thread? 2434511e5cdcSTodd Fiala bool created_now = false; 2435511e5cdcSTodd Fiala thread_sp = GetOrCreateThread (pid, created_now); 2436af245d11STodd Fiala assert (thread_sp.get() && "failed to get or create the tracking data for newly created inferior thread"); 2437af245d11STodd Fiala 2438af245d11STodd Fiala // If the thread was already tracked, it means the main thread already received its SIGTRAP for the create. 2439511e5cdcSTodd Fiala if (!created_now) 2440af245d11STodd Fiala { 2441fa03ad2eSChaoren Lin // We can now resume the newly created thread since it is fully created. 2442fa03ad2eSChaoren Lin NotifyThreadCreateStopped (pid); 2443fa03ad2eSChaoren Lin m_coordinator_up->RequestThreadResume (pid, 244486fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 2445fa03ad2eSChaoren Lin { 2446af245d11STodd Fiala reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetRunning (); 244737c768caSChaoren Lin return Resume (tid_to_resume, LLDB_INVALID_SIGNAL_NUMBER); 2448fa03ad2eSChaoren Lin }, 2449fa03ad2eSChaoren Lin CoordinatorErrorHandler); 2450af245d11STodd Fiala } 2451af245d11STodd Fiala else 2452af245d11STodd Fiala { 2453af245d11STodd Fiala // Mark the thread as currently launching. Need to wait for SIGTRAP clone on the main thread before 2454af245d11STodd Fiala // this thread is ready to go. 2455af245d11STodd Fiala reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetLaunching (); 2456af245d11STodd Fiala } 245758a2f669STodd Fiala 245858a2f669STodd Fiala // Done handling. 245958a2f669STodd Fiala return; 2460af245d11STodd Fiala } 246158a2f669STodd Fiala 246258a2f669STodd Fiala // Check for thread stop notification. 2463511e5cdcSTodd Fiala if (is_from_llgs && (info->si_code == SI_TKILL) && (signo == SIGSTOP)) 2464af245d11STodd Fiala { 2465af245d11STodd Fiala // This is a tgkill()-based stop. 2466af245d11STodd Fiala if (thread_sp) 2467af245d11STodd Fiala { 2468fa03ad2eSChaoren Lin if (log) 2469fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 ", thread stopped", 2470fa03ad2eSChaoren Lin __FUNCTION__, 2471fa03ad2eSChaoren Lin GetID (), 2472fa03ad2eSChaoren Lin pid); 2473fa03ad2eSChaoren Lin 2474aab58633SChaoren Lin // Check that we're not already marked with a stop reason. 2475aab58633SChaoren Lin // Note this thread really shouldn't already be marked as stopped - if we were, that would imply that 2476aab58633SChaoren Lin // the kernel signaled us with the thread stopping which we handled and marked as stopped, 2477aab58633SChaoren Lin // and that, without an intervening resume, we received another stop. It is more likely 2478aab58633SChaoren Lin // that we are missing the marking of a run state somewhere if we find that the thread was 2479aab58633SChaoren Lin // marked as stopped. 2480aab58633SChaoren Lin NativeThreadLinux *const linux_thread_p = reinterpret_cast<NativeThreadLinux*> (thread_sp.get ()); 2481aab58633SChaoren Lin assert (linux_thread_p && "linux_thread_p is null!"); 2482aab58633SChaoren Lin 2483aab58633SChaoren Lin const StateType thread_state = linux_thread_p->GetState (); 2484aab58633SChaoren Lin if (!StateIsStoppedState (thread_state, false)) 2485aab58633SChaoren Lin { 248603f12d6bSChaoren Lin // An inferior thread just stopped, but was not the primary cause of the process stop. 248703f12d6bSChaoren Lin // Instead, something else (like a breakpoint or step) caused the stop. Mark the 248803f12d6bSChaoren Lin // stop signal as 0 to let lldb know this isn't the important stop. 248986fd8e45SChaoren Lin linux_thread_p->SetStoppedBySignal (0); 2490af245d11STodd Fiala SetCurrentThreadID (thread_sp->GetID ()); 249186fd8e45SChaoren Lin m_coordinator_up->NotifyThreadStop (thread_sp->GetID (), true, CoordinatorErrorHandler); 2492aab58633SChaoren Lin } 2493aab58633SChaoren Lin else 2494aab58633SChaoren Lin { 2495aab58633SChaoren Lin if (log) 2496aab58633SChaoren Lin { 2497aab58633SChaoren Lin // Retrieve the signal name if the thread was stopped by a signal. 2498aab58633SChaoren Lin int stop_signo = 0; 2499aab58633SChaoren Lin const bool stopped_by_signal = linux_thread_p->IsStopped (&stop_signo); 2500aab58633SChaoren Lin const char *signal_name = stopped_by_signal ? GetUnixSignals ().GetSignalAsCString (stop_signo) : "<not stopped by signal>"; 2501aab58633SChaoren Lin if (!signal_name) 2502aab58633SChaoren Lin signal_name = "<no-signal-name>"; 2503aab58633SChaoren Lin 2504aab58633SChaoren 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", 2505aab58633SChaoren Lin __FUNCTION__, 2506aab58633SChaoren Lin GetID (), 2507aab58633SChaoren Lin linux_thread_p->GetID (), 2508aab58633SChaoren Lin StateAsCString (thread_state), 2509aab58633SChaoren Lin stop_signo, 2510aab58633SChaoren Lin signal_name); 2511aab58633SChaoren Lin } 2512fa03ad2eSChaoren Lin // Tell the thread state coordinator about the stop. 2513fa03ad2eSChaoren Lin NotifyThreadStop (thread_sp->GetID ()); 2514af245d11STodd Fiala } 251586fd8e45SChaoren Lin } 2516af245d11STodd Fiala 251758a2f669STodd Fiala // Done handling. 2518af245d11STodd Fiala return; 2519af245d11STodd Fiala } 2520af245d11STodd Fiala 2521af245d11STodd Fiala if (log) 2522af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received signal %s", __FUNCTION__, GetUnixSignals ().GetSignalAsCString (signo)); 2523af245d11STodd Fiala 252486fd8e45SChaoren Lin // This thread is stopped. 252586fd8e45SChaoren Lin NotifyThreadStop (pid); 252686fd8e45SChaoren Lin 2527af245d11STodd Fiala switch (signo) 2528af245d11STodd Fiala { 2529511e5cdcSTodd Fiala case SIGSTOP: 2530511e5cdcSTodd Fiala { 253158a2f669STodd Fiala if (log) 2532511e5cdcSTodd Fiala { 2533511e5cdcSTodd Fiala if (is_from_llgs) 2534511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " received SIGSTOP from llgs, most likely an interrupt", __FUNCTION__, GetID (), pid); 2535511e5cdcSTodd Fiala else 2536511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " received SIGSTOP from outside of debugger", __FUNCTION__, GetID (), pid); 2537511e5cdcSTodd Fiala } 2538511e5cdcSTodd Fiala 2539fa03ad2eSChaoren Lin // Resume this thread to get the group-stop mechanism to fire off the true group stops. 2540fa03ad2eSChaoren Lin // This thread will get stopped again as part of the group-stop completion. 2541fa03ad2eSChaoren Lin m_coordinator_up->RequestThreadResume (pid, 254286fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 2543fa03ad2eSChaoren Lin { 2544fa03ad2eSChaoren Lin reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetRunning (); 2545fa03ad2eSChaoren Lin // Pass this signal number on to the inferior to handle. 254637c768caSChaoren Lin return Resume (tid_to_resume, (supress_signal) ? LLDB_INVALID_SIGNAL_NUMBER : signo); 2547fa03ad2eSChaoren Lin }, 2548fa03ad2eSChaoren Lin CoordinatorErrorHandler); 254986fd8e45SChaoren Lin } 255086fd8e45SChaoren Lin break; 255186fd8e45SChaoren Lin case SIGSEGV: 255286fd8e45SChaoren Lin case SIGILL: 255386fd8e45SChaoren Lin case SIGFPE: 255486fd8e45SChaoren Lin case SIGBUS: 255586fd8e45SChaoren Lin if (thread_sp) 255628e57429SChaoren Lin reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetCrashedWithException (*info); 255786fd8e45SChaoren Lin break; 255886fd8e45SChaoren Lin default: 255986fd8e45SChaoren Lin // This is just a pre-signal-delivery notification of the incoming signal. 256086fd8e45SChaoren Lin if (thread_sp) 256186fd8e45SChaoren Lin reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedBySignal (signo); 2562fa03ad2eSChaoren Lin 256386fd8e45SChaoren Lin break; 256486fd8e45SChaoren Lin } 256586fd8e45SChaoren Lin 256686fd8e45SChaoren Lin // Send a stop to the debugger after we get all other threads to stop. 2567fa03ad2eSChaoren Lin CallAfterRunningThreadsStop (pid, 2568fa03ad2eSChaoren Lin [=] (lldb::tid_t signaling_tid) 2569fa03ad2eSChaoren Lin { 2570fa03ad2eSChaoren Lin SetCurrentThreadID (signaling_tid); 2571fa03ad2eSChaoren Lin SetState (StateType::eStateStopped, true); 2572fa03ad2eSChaoren Lin }); 2573511e5cdcSTodd Fiala } 2574af245d11STodd Fiala 2575af245d11STodd Fiala Error 2576af245d11STodd Fiala NativeProcessLinux::Resume (const ResumeActionList &resume_actions) 2577af245d11STodd Fiala { 2578af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); 2579af245d11STodd Fiala if (log) 2580af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s called: pid %" PRIu64, __FUNCTION__, GetID ()); 2581af245d11STodd Fiala 258203f12d6bSChaoren Lin lldb::tid_t deferred_signal_tid = LLDB_INVALID_THREAD_ID; 258303f12d6bSChaoren Lin lldb::tid_t deferred_signal_skip_tid = LLDB_INVALID_THREAD_ID; 2584ae29d395SChaoren Lin int deferred_signo = 0; 2585ae29d395SChaoren Lin NativeThreadProtocolSP deferred_signal_thread_sp; 258686fd8e45SChaoren Lin bool stepping = false; 2587af245d11STodd Fiala 2588af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 25895830aa75STamas Berghammer 2590af245d11STodd Fiala for (auto thread_sp : m_threads) 2591af245d11STodd Fiala { 2592af245d11STodd Fiala assert (thread_sp && "thread list should not contain NULL threads"); 2593af245d11STodd Fiala 2594af245d11STodd Fiala const ResumeAction *const action = resume_actions.GetActionForThread (thread_sp->GetID (), true); 25956a196ce6SChaoren Lin 25966a196ce6SChaoren Lin if (action == nullptr) 25976a196ce6SChaoren Lin { 25986a196ce6SChaoren Lin if (log) 25996a196ce6SChaoren Lin log->Printf ("NativeProcessLinux::%s no action specified for pid %" PRIu64 " tid %" PRIu64, 26006a196ce6SChaoren Lin __FUNCTION__, GetID (), thread_sp->GetID ()); 26016a196ce6SChaoren Lin continue; 26026a196ce6SChaoren Lin } 2603af245d11STodd Fiala 2604af245d11STodd Fiala if (log) 2605af245d11STodd Fiala { 2606af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s processing resume action state %s for pid %" PRIu64 " tid %" PRIu64, 2607af245d11STodd Fiala __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); 2608af245d11STodd Fiala } 2609af245d11STodd Fiala 2610af245d11STodd Fiala switch (action->state) 2611af245d11STodd Fiala { 2612af245d11STodd Fiala case eStateRunning: 2613fa03ad2eSChaoren Lin { 2614af245d11STodd Fiala // Run the thread, possibly feeding it the signal. 2615fa03ad2eSChaoren Lin const int signo = action->signal; 2616b8af31d4SChaoren Lin m_coordinator_up->RequestThreadResumeAsNeeded (thread_sp->GetID (), 261786fd8e45SChaoren Lin [=](lldb::tid_t tid_to_resume, bool supress_signal) 2618af245d11STodd Fiala { 2619fa03ad2eSChaoren Lin reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetRunning (); 2620fa03ad2eSChaoren Lin // Pass this signal number on to the inferior to handle. 26215830aa75STamas Berghammer const auto resume_result = Resume (tid_to_resume, (signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER); 26225830aa75STamas Berghammer if (resume_result.Success()) 26235830aa75STamas Berghammer SetState(eStateRunning, true); 26245830aa75STamas Berghammer return resume_result; 2625fa03ad2eSChaoren Lin }, 2626fa03ad2eSChaoren Lin CoordinatorErrorHandler); 2627af245d11STodd Fiala break; 2628fa03ad2eSChaoren Lin } 2629af245d11STodd Fiala 2630af245d11STodd Fiala case eStateStepping: 2631af245d11STodd Fiala { 2632ae29d395SChaoren Lin // Request the step. 2633ae29d395SChaoren Lin const int signo = action->signal; 2634ae29d395SChaoren Lin m_coordinator_up->RequestThreadResume (thread_sp->GetID (), 263586fd8e45SChaoren Lin [=](lldb::tid_t tid_to_step, bool supress_signal) 2636af245d11STodd Fiala { 2637ae29d395SChaoren Lin reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStepping (); 263837c768caSChaoren Lin const auto step_result = SingleStep (tid_to_step,(signo > 0 && !supress_signal) ? signo : LLDB_INVALID_SIGNAL_NUMBER); 263937c768caSChaoren Lin assert (step_result.Success() && "SingleStep() failed"); 26405830aa75STamas Berghammer if (step_result.Success()) 26415830aa75STamas Berghammer SetState(eStateStepping, true); 264237c768caSChaoren Lin return step_result; 2643ae29d395SChaoren Lin }, 2644ae29d395SChaoren Lin CoordinatorErrorHandler); 264586fd8e45SChaoren Lin stepping = true; 2646af245d11STodd Fiala break; 2647ae29d395SChaoren Lin } 2648af245d11STodd Fiala 2649af245d11STodd Fiala case eStateSuspended: 2650af245d11STodd Fiala case eStateStopped: 2651ae29d395SChaoren Lin // if we haven't chosen a deferred signal tid yet, use this one. 2652ae29d395SChaoren Lin if (deferred_signal_tid == LLDB_INVALID_THREAD_ID) 2653ae29d395SChaoren Lin { 2654ae29d395SChaoren Lin deferred_signal_tid = thread_sp->GetID (); 2655ae29d395SChaoren Lin deferred_signal_thread_sp = thread_sp; 2656ae29d395SChaoren Lin deferred_signo = SIGSTOP; 2657ae29d395SChaoren Lin } 2658af245d11STodd Fiala break; 2659af245d11STodd Fiala 2660af245d11STodd Fiala default: 2661af245d11STodd Fiala return Error ("NativeProcessLinux::%s (): unexpected state %s specified for pid %" PRIu64 ", tid %" PRIu64, 2662af245d11STodd Fiala __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); 2663af245d11STodd Fiala } 2664af245d11STodd Fiala } 2665af245d11STodd Fiala 2666fa03ad2eSChaoren Lin // If we had any thread stopping, then do a deferred notification of the chosen stop thread id and signal 2667fa03ad2eSChaoren Lin // after all other running threads have stopped. 266886fd8e45SChaoren Lin // If there is a stepping thread involved we'll be eventually stopped by SIGTRAP trace signal. 266986fd8e45SChaoren Lin if (deferred_signal_tid != LLDB_INVALID_THREAD_ID && !stepping) 2670fa03ad2eSChaoren Lin { 267103f12d6bSChaoren Lin CallAfterRunningThreadsStopWithSkipTID (deferred_signal_tid, 267203f12d6bSChaoren Lin deferred_signal_skip_tid, 2673fa03ad2eSChaoren Lin [=](lldb::tid_t deferred_notification_tid) 2674fa03ad2eSChaoren Lin { 2675ae29d395SChaoren Lin // Set the signal thread to the current thread. 2676fa03ad2eSChaoren Lin SetCurrentThreadID (deferred_notification_tid); 2677ae29d395SChaoren Lin 2678ae29d395SChaoren Lin // Set the thread state as stopped by the deferred signo. 2679ae29d395SChaoren Lin reinterpret_cast<NativeThreadLinux*> (deferred_signal_thread_sp.get ())->SetStoppedBySignal (deferred_signo); 2680ae29d395SChaoren Lin 2681ae29d395SChaoren Lin // Tell the process delegate that the process is in a stopped state. 2682fa03ad2eSChaoren Lin SetState (StateType::eStateStopped, true); 2683fa03ad2eSChaoren Lin }); 2684af245d11STodd Fiala } 2685af245d11STodd Fiala 26865830aa75STamas Berghammer return Error(); 2687af245d11STodd Fiala } 2688af245d11STodd Fiala 2689af245d11STodd Fiala Error 2690af245d11STodd Fiala NativeProcessLinux::Halt () 2691af245d11STodd Fiala { 2692af245d11STodd Fiala Error error; 2693af245d11STodd Fiala 2694af245d11STodd Fiala if (kill (GetID (), SIGSTOP) != 0) 2695af245d11STodd Fiala error.SetErrorToErrno (); 2696af245d11STodd Fiala 2697af245d11STodd Fiala return error; 2698af245d11STodd Fiala } 2699af245d11STodd Fiala 2700af245d11STodd Fiala Error 2701af245d11STodd Fiala NativeProcessLinux::Detach () 2702af245d11STodd Fiala { 2703af245d11STodd Fiala Error error; 2704af245d11STodd Fiala 2705af245d11STodd Fiala // Tell ptrace to detach from the process. 2706af245d11STodd Fiala if (GetID () != LLDB_INVALID_PROCESS_ID) 2707af245d11STodd Fiala error = Detach (GetID ()); 2708af245d11STodd Fiala 2709af245d11STodd Fiala // Stop monitoring the inferior. 2710af245d11STodd Fiala StopMonitor (); 2711af245d11STodd Fiala 2712af245d11STodd Fiala // No error. 2713af245d11STodd Fiala return error; 2714af245d11STodd Fiala } 2715af245d11STodd Fiala 2716af245d11STodd Fiala Error 2717af245d11STodd Fiala NativeProcessLinux::Signal (int signo) 2718af245d11STodd Fiala { 2719af245d11STodd Fiala Error error; 2720af245d11STodd Fiala 2721af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2722af245d11STodd Fiala if (log) 2723af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s: sending signal %d (%s) to pid %" PRIu64, 2724af245d11STodd Fiala __FUNCTION__, signo, GetUnixSignals ().GetSignalAsCString (signo), GetID ()); 2725af245d11STodd Fiala 2726af245d11STodd Fiala if (kill(GetID(), signo)) 2727af245d11STodd Fiala error.SetErrorToErrno(); 2728af245d11STodd Fiala 2729af245d11STodd Fiala return error; 2730af245d11STodd Fiala } 2731af245d11STodd Fiala 2732af245d11STodd Fiala Error 2733e9547b80SChaoren Lin NativeProcessLinux::Interrupt () 2734e9547b80SChaoren Lin { 2735e9547b80SChaoren Lin // Pick a running thread (or if none, a not-dead stopped thread) as 2736e9547b80SChaoren Lin // the chosen thread that will be the stop-reason thread. 2737e9547b80SChaoren Lin Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2738e9547b80SChaoren Lin 2739e9547b80SChaoren Lin NativeThreadProtocolSP running_thread_sp; 2740e9547b80SChaoren Lin NativeThreadProtocolSP stopped_thread_sp; 2741e9547b80SChaoren Lin 2742e9547b80SChaoren Lin if (log) 2743e9547b80SChaoren Lin log->Printf ("NativeProcessLinux::%s selecting running thread for interrupt target", __FUNCTION__); 2744e9547b80SChaoren Lin 27455830aa75STamas Berghammer Mutex::Locker locker (m_threads_mutex); 27465830aa75STamas Berghammer 2747e9547b80SChaoren Lin for (auto thread_sp : m_threads) 2748e9547b80SChaoren Lin { 2749e9547b80SChaoren Lin // The thread shouldn't be null but lets just cover that here. 2750e9547b80SChaoren Lin if (!thread_sp) 2751e9547b80SChaoren Lin continue; 2752e9547b80SChaoren Lin 2753e9547b80SChaoren Lin // If we have a running or stepping thread, we'll call that the 2754e9547b80SChaoren Lin // target of the interrupt. 2755e9547b80SChaoren Lin const auto thread_state = thread_sp->GetState (); 2756e9547b80SChaoren Lin if (thread_state == eStateRunning || 2757e9547b80SChaoren Lin thread_state == eStateStepping) 2758e9547b80SChaoren Lin { 2759e9547b80SChaoren Lin running_thread_sp = thread_sp; 2760e9547b80SChaoren Lin break; 2761e9547b80SChaoren Lin } 2762e9547b80SChaoren Lin else if (!stopped_thread_sp && StateIsStoppedState (thread_state, true)) 2763e9547b80SChaoren Lin { 2764e9547b80SChaoren Lin // Remember the first non-dead stopped thread. We'll use that as a backup if there are no running threads. 2765e9547b80SChaoren Lin stopped_thread_sp = thread_sp; 2766e9547b80SChaoren Lin } 2767e9547b80SChaoren Lin } 2768e9547b80SChaoren Lin 2769e9547b80SChaoren Lin if (!running_thread_sp && !stopped_thread_sp) 2770e9547b80SChaoren Lin { 27715830aa75STamas Berghammer Error error("found no running/stepping or live stopped threads as target for interrupt"); 2772e9547b80SChaoren Lin if (log) 2773e9547b80SChaoren Lin log->Printf ("NativeProcessLinux::%s skipping due to error: %s", __FUNCTION__, error.AsCString ()); 27745830aa75STamas Berghammer 2775e9547b80SChaoren Lin return error; 2776e9547b80SChaoren Lin } 2777e9547b80SChaoren Lin 2778e9547b80SChaoren Lin NativeThreadProtocolSP deferred_signal_thread_sp = running_thread_sp ? running_thread_sp : stopped_thread_sp; 2779e9547b80SChaoren Lin 2780e9547b80SChaoren Lin if (log) 2781e9547b80SChaoren Lin log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " %s tid %" PRIu64 " chosen for interrupt target", 2782e9547b80SChaoren Lin __FUNCTION__, 2783e9547b80SChaoren Lin GetID (), 2784e9547b80SChaoren Lin running_thread_sp ? "running" : "stopped", 2785e9547b80SChaoren Lin deferred_signal_thread_sp->GetID ()); 2786e9547b80SChaoren Lin 2787e9547b80SChaoren Lin CallAfterRunningThreadsStop (deferred_signal_thread_sp->GetID (), 2788e9547b80SChaoren Lin [=](lldb::tid_t deferred_notification_tid) 2789e9547b80SChaoren Lin { 2790e9547b80SChaoren Lin // Set the signal thread to the current thread. 2791e9547b80SChaoren Lin SetCurrentThreadID (deferred_notification_tid); 2792e9547b80SChaoren Lin 2793e9547b80SChaoren Lin // Set the thread state as stopped by the deferred signo. 2794e9547b80SChaoren Lin reinterpret_cast<NativeThreadLinux*> (deferred_signal_thread_sp.get ())->SetStoppedBySignal (SIGSTOP); 2795e9547b80SChaoren Lin 2796e9547b80SChaoren Lin // Tell the process delegate that the process is in a stopped state. 2797e9547b80SChaoren Lin SetState (StateType::eStateStopped, true); 2798e9547b80SChaoren Lin }); 27995830aa75STamas Berghammer return Error(); 2800e9547b80SChaoren Lin } 2801e9547b80SChaoren Lin 2802e9547b80SChaoren Lin Error 2803af245d11STodd Fiala NativeProcessLinux::Kill () 2804af245d11STodd Fiala { 2805af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2806af245d11STodd Fiala if (log) 2807af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s called for PID %" PRIu64, __FUNCTION__, GetID ()); 2808af245d11STodd Fiala 2809af245d11STodd Fiala Error error; 2810af245d11STodd Fiala 2811af245d11STodd Fiala switch (m_state) 2812af245d11STodd Fiala { 2813af245d11STodd Fiala case StateType::eStateInvalid: 2814af245d11STodd Fiala case StateType::eStateExited: 2815af245d11STodd Fiala case StateType::eStateCrashed: 2816af245d11STodd Fiala case StateType::eStateDetached: 2817af245d11STodd Fiala case StateType::eStateUnloaded: 2818af245d11STodd Fiala // Nothing to do - the process is already dead. 2819af245d11STodd Fiala if (log) 2820af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s ignored for PID %" PRIu64 " due to current state: %s", __FUNCTION__, GetID (), StateAsCString (m_state)); 2821af245d11STodd Fiala return error; 2822af245d11STodd Fiala 2823af245d11STodd Fiala case StateType::eStateConnected: 2824af245d11STodd Fiala case StateType::eStateAttaching: 2825af245d11STodd Fiala case StateType::eStateLaunching: 2826af245d11STodd Fiala case StateType::eStateStopped: 2827af245d11STodd Fiala case StateType::eStateRunning: 2828af245d11STodd Fiala case StateType::eStateStepping: 2829af245d11STodd Fiala case StateType::eStateSuspended: 2830af245d11STodd Fiala // We can try to kill a process in these states. 2831af245d11STodd Fiala break; 2832af245d11STodd Fiala } 2833af245d11STodd Fiala 2834af245d11STodd Fiala if (kill (GetID (), SIGKILL) != 0) 2835af245d11STodd Fiala { 2836af245d11STodd Fiala error.SetErrorToErrno (); 2837af245d11STodd Fiala return error; 2838af245d11STodd Fiala } 2839af245d11STodd Fiala 2840af245d11STodd Fiala return error; 2841af245d11STodd Fiala } 2842af245d11STodd Fiala 2843af245d11STodd Fiala static Error 2844af245d11STodd Fiala ParseMemoryRegionInfoFromProcMapsLine (const std::string &maps_line, MemoryRegionInfo &memory_region_info) 2845af245d11STodd Fiala { 2846af245d11STodd Fiala memory_region_info.Clear(); 2847af245d11STodd Fiala 2848af245d11STodd Fiala StringExtractor line_extractor (maps_line.c_str ()); 2849af245d11STodd Fiala 2850af245d11STodd Fiala // Format: {address_start_hex}-{address_end_hex} perms offset dev inode pathname 2851af245d11STodd Fiala // perms: rwxp (letter is present if set, '-' if not, final character is p=private, s=shared). 2852af245d11STodd Fiala 2853af245d11STodd Fiala // Parse out the starting address 2854af245d11STodd Fiala lldb::addr_t start_address = line_extractor.GetHexMaxU64 (false, 0); 2855af245d11STodd Fiala 2856af245d11STodd Fiala // Parse out hyphen separating start and end address from range. 2857af245d11STodd Fiala if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != '-')) 2858af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing dash between address range"); 2859af245d11STodd Fiala 2860af245d11STodd Fiala // Parse out the ending address 2861af245d11STodd Fiala lldb::addr_t end_address = line_extractor.GetHexMaxU64 (false, start_address); 2862af245d11STodd Fiala 2863af245d11STodd Fiala // Parse out the space after the address. 2864af245d11STodd Fiala if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != ' ')) 2865af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing space after range"); 2866af245d11STodd Fiala 2867af245d11STodd Fiala // Save the range. 2868af245d11STodd Fiala memory_region_info.GetRange ().SetRangeBase (start_address); 2869af245d11STodd Fiala memory_region_info.GetRange ().SetRangeEnd (end_address); 2870af245d11STodd Fiala 2871af245d11STodd Fiala // Parse out each permission entry. 2872af245d11STodd Fiala if (line_extractor.GetBytesLeft () < 4) 2873af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing some portion of permissions"); 2874af245d11STodd Fiala 2875af245d11STodd Fiala // Handle read permission. 2876af245d11STodd Fiala const char read_perm_char = line_extractor.GetChar (); 2877af245d11STodd Fiala if (read_perm_char == 'r') 2878af245d11STodd Fiala memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eYes); 2879af245d11STodd Fiala else 2880af245d11STodd Fiala { 2881af245d11STodd Fiala assert ( (read_perm_char == '-') && "unexpected /proc/{pid}/maps read permission char" ); 2882af245d11STodd Fiala memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); 2883af245d11STodd Fiala } 2884af245d11STodd Fiala 2885af245d11STodd Fiala // Handle write permission. 2886af245d11STodd Fiala const char write_perm_char = line_extractor.GetChar (); 2887af245d11STodd Fiala if (write_perm_char == 'w') 2888af245d11STodd Fiala memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eYes); 2889af245d11STodd Fiala else 2890af245d11STodd Fiala { 2891af245d11STodd Fiala assert ( (write_perm_char == '-') && "unexpected /proc/{pid}/maps write permission char" ); 2892af245d11STodd Fiala memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); 2893af245d11STodd Fiala } 2894af245d11STodd Fiala 2895af245d11STodd Fiala // Handle execute permission. 2896af245d11STodd Fiala const char exec_perm_char = line_extractor.GetChar (); 2897af245d11STodd Fiala if (exec_perm_char == 'x') 2898af245d11STodd Fiala memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eYes); 2899af245d11STodd Fiala else 2900af245d11STodd Fiala { 2901af245d11STodd Fiala assert ( (exec_perm_char == '-') && "unexpected /proc/{pid}/maps exec permission char" ); 2902af245d11STodd Fiala memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); 2903af245d11STodd Fiala } 2904af245d11STodd Fiala 2905af245d11STodd Fiala return Error (); 2906af245d11STodd Fiala } 2907af245d11STodd Fiala 2908af245d11STodd Fiala Error 2909af245d11STodd Fiala NativeProcessLinux::GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) 2910af245d11STodd Fiala { 2911af245d11STodd Fiala // FIXME review that the final memory region returned extends to the end of the virtual address space, 2912af245d11STodd Fiala // with no perms if it is not mapped. 2913af245d11STodd Fiala 2914af245d11STodd Fiala // Use an approach that reads memory regions from /proc/{pid}/maps. 2915af245d11STodd Fiala // Assume proc maps entries are in ascending order. 2916af245d11STodd Fiala // FIXME assert if we find differently. 2917af245d11STodd Fiala Mutex::Locker locker (m_mem_region_cache_mutex); 2918af245d11STodd Fiala 2919af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2920af245d11STodd Fiala Error error; 2921af245d11STodd Fiala 2922af245d11STodd Fiala if (m_supports_mem_region == LazyBool::eLazyBoolNo) 2923af245d11STodd Fiala { 2924af245d11STodd Fiala // We're done. 2925af245d11STodd Fiala error.SetErrorString ("unsupported"); 2926af245d11STodd Fiala return error; 2927af245d11STodd Fiala } 2928af245d11STodd Fiala 2929af245d11STodd Fiala // If our cache is empty, pull the latest. There should always be at least one memory region 2930af245d11STodd Fiala // if memory region handling is supported. 2931af245d11STodd Fiala if (m_mem_region_cache.empty ()) 2932af245d11STodd Fiala { 2933af245d11STodd Fiala error = ProcFileReader::ProcessLineByLine (GetID (), "maps", 2934af245d11STodd Fiala [&] (const std::string &line) -> bool 2935af245d11STodd Fiala { 2936af245d11STodd Fiala MemoryRegionInfo info; 2937af245d11STodd Fiala const Error parse_error = ParseMemoryRegionInfoFromProcMapsLine (line, info); 2938af245d11STodd Fiala if (parse_error.Success ()) 2939af245d11STodd Fiala { 2940af245d11STodd Fiala m_mem_region_cache.push_back (info); 2941af245d11STodd Fiala return true; 2942af245d11STodd Fiala } 2943af245d11STodd Fiala else 2944af245d11STodd Fiala { 2945af245d11STodd Fiala if (log) 2946af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to parse proc maps line '%s': %s", __FUNCTION__, line.c_str (), error.AsCString ()); 2947af245d11STodd Fiala return false; 2948af245d11STodd Fiala } 2949af245d11STodd Fiala }); 2950af245d11STodd Fiala 2951af245d11STodd Fiala // If we had an error, we'll mark unsupported. 2952af245d11STodd Fiala if (error.Fail ()) 2953af245d11STodd Fiala { 2954af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolNo; 2955af245d11STodd Fiala return error; 2956af245d11STodd Fiala } 2957af245d11STodd Fiala else if (m_mem_region_cache.empty ()) 2958af245d11STodd Fiala { 2959af245d11STodd Fiala // No entries after attempting to read them. This shouldn't happen if /proc/{pid}/maps 2960af245d11STodd Fiala // is supported. Assume we don't support map entries via procfs. 2961af245d11STodd Fiala if (log) 2962af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to find any procfs maps entries, assuming no support for memory region metadata retrieval", __FUNCTION__); 2963af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolNo; 2964af245d11STodd Fiala error.SetErrorString ("not supported"); 2965af245d11STodd Fiala return error; 2966af245d11STodd Fiala } 2967af245d11STodd Fiala 2968af245d11STodd Fiala if (log) 2969af245d11STodd 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 ()); 2970af245d11STodd Fiala 2971af245d11STodd Fiala // We support memory retrieval, remember that. 2972af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolYes; 2973af245d11STodd Fiala } 2974af245d11STodd Fiala else 2975af245d11STodd Fiala { 2976af245d11STodd Fiala if (log) 2977af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s reusing %" PRIu64 " cached memory region entries", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); 2978af245d11STodd Fiala } 2979af245d11STodd Fiala 2980af245d11STodd Fiala lldb::addr_t prev_base_address = 0; 2981af245d11STodd Fiala 2982af245d11STodd Fiala // FIXME start by finding the last region that is <= target address using binary search. Data is sorted. 2983af245d11STodd Fiala // There can be a ton of regions on pthreads apps with lots of threads. 2984af245d11STodd Fiala for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end (); ++it) 2985af245d11STodd Fiala { 2986af245d11STodd Fiala MemoryRegionInfo &proc_entry_info = *it; 2987af245d11STodd Fiala 2988af245d11STodd Fiala // Sanity check assumption that /proc/{pid}/maps entries are ascending. 2989af245d11STodd Fiala assert ((proc_entry_info.GetRange ().GetRangeBase () >= prev_base_address) && "descending /proc/pid/maps entries detected, unexpected"); 2990af245d11STodd Fiala prev_base_address = proc_entry_info.GetRange ().GetRangeBase (); 2991af245d11STodd Fiala 2992af245d11STodd Fiala // If the target address comes before this entry, indicate distance to next region. 2993af245d11STodd Fiala if (load_addr < proc_entry_info.GetRange ().GetRangeBase ()) 2994af245d11STodd Fiala { 2995af245d11STodd Fiala range_info.GetRange ().SetRangeBase (load_addr); 2996af245d11STodd Fiala range_info.GetRange ().SetByteSize (proc_entry_info.GetRange ().GetRangeBase () - load_addr); 2997af245d11STodd Fiala range_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); 2998af245d11STodd Fiala range_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); 2999af245d11STodd Fiala range_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); 3000af245d11STodd Fiala 3001af245d11STodd Fiala return error; 3002af245d11STodd Fiala } 3003af245d11STodd Fiala else if (proc_entry_info.GetRange ().Contains (load_addr)) 3004af245d11STodd Fiala { 3005af245d11STodd Fiala // The target address is within the memory region we're processing here. 3006af245d11STodd Fiala range_info = proc_entry_info; 3007af245d11STodd Fiala return error; 3008af245d11STodd Fiala } 3009af245d11STodd Fiala 3010af245d11STodd Fiala // The target memory address comes somewhere after the region we just parsed. 3011af245d11STodd Fiala } 3012af245d11STodd Fiala 3013af245d11STodd Fiala // If we made it here, we didn't find an entry that contained the given address. 3014af245d11STodd Fiala error.SetErrorString ("address comes after final region"); 3015af245d11STodd Fiala 3016af245d11STodd Fiala if (log) 3017af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to find map entry for address 0x%" PRIx64 ": %s", __FUNCTION__, load_addr, error.AsCString ()); 3018af245d11STodd Fiala 3019af245d11STodd Fiala return error; 3020af245d11STodd Fiala } 3021af245d11STodd Fiala 3022af245d11STodd Fiala void 3023af245d11STodd Fiala NativeProcessLinux::DoStopIDBumped (uint32_t newBumpId) 3024af245d11STodd Fiala { 3025af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3026af245d11STodd Fiala if (log) 3027af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(newBumpId=%" PRIu32 ") called", __FUNCTION__, newBumpId); 3028af245d11STodd Fiala 3029af245d11STodd Fiala { 3030af245d11STodd Fiala Mutex::Locker locker (m_mem_region_cache_mutex); 3031af245d11STodd Fiala if (log) 3032af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s clearing %" PRIu64 " entries from the cache", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); 3033af245d11STodd Fiala m_mem_region_cache.clear (); 3034af245d11STodd Fiala } 3035af245d11STodd Fiala } 3036af245d11STodd Fiala 3037af245d11STodd Fiala Error 3038af245d11STodd Fiala NativeProcessLinux::AllocateMemory ( 3039af245d11STodd Fiala lldb::addr_t size, 3040af245d11STodd Fiala uint32_t permissions, 3041af245d11STodd Fiala lldb::addr_t &addr) 3042af245d11STodd Fiala { 3043af245d11STodd Fiala // FIXME implementing this requires the equivalent of 3044af245d11STodd Fiala // InferiorCallPOSIX::InferiorCallMmap, which depends on 3045af245d11STodd Fiala // functional ThreadPlans working with Native*Protocol. 3046af245d11STodd Fiala #if 1 3047af245d11STodd Fiala return Error ("not implemented yet"); 3048af245d11STodd Fiala #else 3049af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 3050af245d11STodd Fiala 3051af245d11STodd Fiala unsigned prot = 0; 3052af245d11STodd Fiala if (permissions & lldb::ePermissionsReadable) 3053af245d11STodd Fiala prot |= eMmapProtRead; 3054af245d11STodd Fiala if (permissions & lldb::ePermissionsWritable) 3055af245d11STodd Fiala prot |= eMmapProtWrite; 3056af245d11STodd Fiala if (permissions & lldb::ePermissionsExecutable) 3057af245d11STodd Fiala prot |= eMmapProtExec; 3058af245d11STodd Fiala 3059af245d11STodd Fiala // TODO implement this directly in NativeProcessLinux 3060af245d11STodd Fiala // (and lift to NativeProcessPOSIX if/when that class is 3061af245d11STodd Fiala // refactored out). 3062af245d11STodd Fiala if (InferiorCallMmap(this, addr, 0, size, prot, 3063af245d11STodd Fiala eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { 3064af245d11STodd Fiala m_addr_to_mmap_size[addr] = size; 3065af245d11STodd Fiala return Error (); 3066af245d11STodd Fiala } else { 3067af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 3068af245d11STodd Fiala return Error("unable to allocate %" PRIu64 " bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions)); 3069af245d11STodd Fiala } 3070af245d11STodd Fiala #endif 3071af245d11STodd Fiala } 3072af245d11STodd Fiala 3073af245d11STodd Fiala Error 3074af245d11STodd Fiala NativeProcessLinux::DeallocateMemory (lldb::addr_t addr) 3075af245d11STodd Fiala { 3076af245d11STodd Fiala // FIXME see comments in AllocateMemory - required lower-level 3077af245d11STodd Fiala // bits not in place yet (ThreadPlans) 3078af245d11STodd Fiala return Error ("not implemented"); 3079af245d11STodd Fiala } 3080af245d11STodd Fiala 3081af245d11STodd Fiala lldb::addr_t 3082af245d11STodd Fiala NativeProcessLinux::GetSharedLibraryInfoAddress () 3083af245d11STodd Fiala { 3084af245d11STodd Fiala #if 1 3085af245d11STodd Fiala // punt on this for now 3086af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3087af245d11STodd Fiala #else 3088af245d11STodd Fiala // Return the image info address for the exe module 3089af245d11STodd Fiala #if 1 3090af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3091af245d11STodd Fiala 3092af245d11STodd Fiala ModuleSP module_sp; 3093af245d11STodd Fiala Error error = GetExeModuleSP (module_sp); 3094af245d11STodd Fiala if (error.Fail ()) 3095af245d11STodd Fiala { 3096af245d11STodd Fiala if (log) 3097af245d11STodd Fiala log->Warning ("NativeProcessLinux::%s failed to retrieve exe module: %s", __FUNCTION__, error.AsCString ()); 3098af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3099af245d11STodd Fiala } 3100af245d11STodd Fiala 3101af245d11STodd Fiala if (module_sp == nullptr) 3102af245d11STodd Fiala { 3103af245d11STodd Fiala if (log) 3104af245d11STodd Fiala log->Warning ("NativeProcessLinux::%s exe module returned was NULL", __FUNCTION__); 3105af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3106af245d11STodd Fiala } 3107af245d11STodd Fiala 3108af245d11STodd Fiala ObjectFileSP object_file_sp = module_sp->GetObjectFile (); 3109af245d11STodd Fiala if (object_file_sp == nullptr) 3110af245d11STodd Fiala { 3111af245d11STodd Fiala if (log) 3112af245d11STodd Fiala log->Warning ("NativeProcessLinux::%s exe module returned a NULL object file", __FUNCTION__); 3113af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3114af245d11STodd Fiala } 3115af245d11STodd Fiala 3116af245d11STodd Fiala return obj_file_sp->GetImageInfoAddress(); 3117af245d11STodd Fiala #else 3118af245d11STodd Fiala Target *target = &GetTarget(); 3119af245d11STodd Fiala ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile(); 3120af245d11STodd Fiala Address addr = obj_file->GetImageInfoAddress(target); 3121af245d11STodd Fiala 3122af245d11STodd Fiala if (addr.IsValid()) 3123af245d11STodd Fiala return addr.GetLoadAddress(target); 3124af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3125af245d11STodd Fiala #endif 3126af245d11STodd Fiala #endif // punt on this for now 3127af245d11STodd Fiala } 3128af245d11STodd Fiala 3129af245d11STodd Fiala size_t 3130af245d11STodd Fiala NativeProcessLinux::UpdateThreads () 3131af245d11STodd Fiala { 3132af245d11STodd Fiala // The NativeProcessLinux monitoring threads are always up to date 3133af245d11STodd Fiala // with respect to thread state and they keep the thread list 3134af245d11STodd Fiala // populated properly. All this method needs to do is return the 3135af245d11STodd Fiala // thread count. 3136af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 3137af245d11STodd Fiala return m_threads.size (); 3138af245d11STodd Fiala } 3139af245d11STodd Fiala 3140af245d11STodd Fiala bool 3141af245d11STodd Fiala NativeProcessLinux::GetArchitecture (ArchSpec &arch) const 3142af245d11STodd Fiala { 3143af245d11STodd Fiala arch = m_arch; 3144af245d11STodd Fiala return true; 3145af245d11STodd Fiala } 3146af245d11STodd Fiala 3147af245d11STodd Fiala Error 3148af245d11STodd Fiala NativeProcessLinux::GetSoftwareBreakpointSize (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size) 3149af245d11STodd Fiala { 3150af245d11STodd Fiala // FIXME put this behind a breakpoint protocol class that can be 3151af245d11STodd Fiala // set per architecture. Need ARM, MIPS support here. 31522afc5966STodd Fiala static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; 3153af245d11STodd Fiala static const uint8_t g_i386_opcode [] = { 0xCC }; 3154af245d11STodd Fiala 3155af245d11STodd Fiala switch (m_arch.GetMachine ()) 3156af245d11STodd Fiala { 31572afc5966STodd Fiala case llvm::Triple::aarch64: 31582afc5966STodd Fiala actual_opcode_size = static_cast<uint32_t> (sizeof(g_aarch64_opcode)); 31592afc5966STodd Fiala return Error (); 31602afc5966STodd Fiala 3161af245d11STodd Fiala case llvm::Triple::x86: 3162af245d11STodd Fiala case llvm::Triple::x86_64: 3163af245d11STodd Fiala actual_opcode_size = static_cast<uint32_t> (sizeof(g_i386_opcode)); 3164af245d11STodd Fiala return Error (); 3165af245d11STodd Fiala 3166af245d11STodd Fiala default: 3167af245d11STodd Fiala assert(false && "CPU type not supported!"); 3168af245d11STodd Fiala return Error ("CPU type not supported"); 3169af245d11STodd Fiala } 3170af245d11STodd Fiala } 3171af245d11STodd Fiala 3172af245d11STodd Fiala Error 3173af245d11STodd Fiala NativeProcessLinux::SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) 3174af245d11STodd Fiala { 3175af245d11STodd Fiala if (hardware) 3176af245d11STodd Fiala return Error ("NativeProcessLinux does not support hardware breakpoints"); 3177af245d11STodd Fiala else 3178af245d11STodd Fiala return SetSoftwareBreakpoint (addr, size); 3179af245d11STodd Fiala } 3180af245d11STodd Fiala 3181af245d11STodd Fiala Error 3182af245d11STodd Fiala NativeProcessLinux::GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) 3183af245d11STodd Fiala { 3184af245d11STodd Fiala // FIXME put this behind a breakpoint protocol class that can be 3185af245d11STodd Fiala // set per architecture. Need ARM, MIPS support here. 31862afc5966STodd Fiala static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; 3187af245d11STodd Fiala static const uint8_t g_i386_opcode [] = { 0xCC }; 3188af245d11STodd Fiala 3189af245d11STodd Fiala switch (m_arch.GetMachine ()) 3190af245d11STodd Fiala { 31912afc5966STodd Fiala case llvm::Triple::aarch64: 31922afc5966STodd Fiala trap_opcode_bytes = g_aarch64_opcode; 31932afc5966STodd Fiala actual_opcode_size = sizeof(g_aarch64_opcode); 31942afc5966STodd Fiala return Error (); 31952afc5966STodd Fiala 3196af245d11STodd Fiala case llvm::Triple::x86: 3197af245d11STodd Fiala case llvm::Triple::x86_64: 3198af245d11STodd Fiala trap_opcode_bytes = g_i386_opcode; 3199af245d11STodd Fiala actual_opcode_size = sizeof(g_i386_opcode); 3200af245d11STodd Fiala return Error (); 3201af245d11STodd Fiala 3202af245d11STodd Fiala default: 3203af245d11STodd Fiala assert(false && "CPU type not supported!"); 3204af245d11STodd Fiala return Error ("CPU type not supported"); 3205af245d11STodd Fiala } 3206af245d11STodd Fiala } 3207af245d11STodd Fiala 3208af245d11STodd Fiala #if 0 3209af245d11STodd Fiala ProcessMessage::CrashReason 3210af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info) 3211af245d11STodd Fiala { 3212af245d11STodd Fiala ProcessMessage::CrashReason reason; 3213af245d11STodd Fiala assert(info->si_signo == SIGSEGV); 3214af245d11STodd Fiala 3215af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 3216af245d11STodd Fiala 3217af245d11STodd Fiala switch (info->si_code) 3218af245d11STodd Fiala { 3219af245d11STodd Fiala default: 3220af245d11STodd Fiala assert(false && "unexpected si_code for SIGSEGV"); 3221af245d11STodd Fiala break; 3222af245d11STodd Fiala case SI_KERNEL: 3223af245d11STodd Fiala // Linux will occasionally send spurious SI_KERNEL codes. 3224af245d11STodd Fiala // (this is poorly documented in sigaction) 3225af245d11STodd Fiala // One way to get this is via unaligned SIMD loads. 3226af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; // for lack of anything better 3227af245d11STodd Fiala break; 3228af245d11STodd Fiala case SEGV_MAPERR: 3229af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; 3230af245d11STodd Fiala break; 3231af245d11STodd Fiala case SEGV_ACCERR: 3232af245d11STodd Fiala reason = ProcessMessage::ePrivilegedAddress; 3233af245d11STodd Fiala break; 3234af245d11STodd Fiala } 3235af245d11STodd Fiala 3236af245d11STodd Fiala return reason; 3237af245d11STodd Fiala } 3238af245d11STodd Fiala #endif 3239af245d11STodd Fiala 3240af245d11STodd Fiala 3241af245d11STodd Fiala #if 0 3242af245d11STodd Fiala ProcessMessage::CrashReason 3243af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info) 3244af245d11STodd Fiala { 3245af245d11STodd Fiala ProcessMessage::CrashReason reason; 3246af245d11STodd Fiala assert(info->si_signo == SIGILL); 3247af245d11STodd Fiala 3248af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 3249af245d11STodd Fiala 3250af245d11STodd Fiala switch (info->si_code) 3251af245d11STodd Fiala { 3252af245d11STodd Fiala default: 3253af245d11STodd Fiala assert(false && "unexpected si_code for SIGILL"); 3254af245d11STodd Fiala break; 3255af245d11STodd Fiala case ILL_ILLOPC: 3256af245d11STodd Fiala reason = ProcessMessage::eIllegalOpcode; 3257af245d11STodd Fiala break; 3258af245d11STodd Fiala case ILL_ILLOPN: 3259af245d11STodd Fiala reason = ProcessMessage::eIllegalOperand; 3260af245d11STodd Fiala break; 3261af245d11STodd Fiala case ILL_ILLADR: 3262af245d11STodd Fiala reason = ProcessMessage::eIllegalAddressingMode; 3263af245d11STodd Fiala break; 3264af245d11STodd Fiala case ILL_ILLTRP: 3265af245d11STodd Fiala reason = ProcessMessage::eIllegalTrap; 3266af245d11STodd Fiala break; 3267af245d11STodd Fiala case ILL_PRVOPC: 3268af245d11STodd Fiala reason = ProcessMessage::ePrivilegedOpcode; 3269af245d11STodd Fiala break; 3270af245d11STodd Fiala case ILL_PRVREG: 3271af245d11STodd Fiala reason = ProcessMessage::ePrivilegedRegister; 3272af245d11STodd Fiala break; 3273af245d11STodd Fiala case ILL_COPROC: 3274af245d11STodd Fiala reason = ProcessMessage::eCoprocessorError; 3275af245d11STodd Fiala break; 3276af245d11STodd Fiala case ILL_BADSTK: 3277af245d11STodd Fiala reason = ProcessMessage::eInternalStackError; 3278af245d11STodd Fiala break; 3279af245d11STodd Fiala } 3280af245d11STodd Fiala 3281af245d11STodd Fiala return reason; 3282af245d11STodd Fiala } 3283af245d11STodd Fiala #endif 3284af245d11STodd Fiala 3285af245d11STodd Fiala #if 0 3286af245d11STodd Fiala ProcessMessage::CrashReason 3287af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info) 3288af245d11STodd Fiala { 3289af245d11STodd Fiala ProcessMessage::CrashReason reason; 3290af245d11STodd Fiala assert(info->si_signo == SIGFPE); 3291af245d11STodd Fiala 3292af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 3293af245d11STodd Fiala 3294af245d11STodd Fiala switch (info->si_code) 3295af245d11STodd Fiala { 3296af245d11STodd Fiala default: 3297af245d11STodd Fiala assert(false && "unexpected si_code for SIGFPE"); 3298af245d11STodd Fiala break; 3299af245d11STodd Fiala case FPE_INTDIV: 3300af245d11STodd Fiala reason = ProcessMessage::eIntegerDivideByZero; 3301af245d11STodd Fiala break; 3302af245d11STodd Fiala case FPE_INTOVF: 3303af245d11STodd Fiala reason = ProcessMessage::eIntegerOverflow; 3304af245d11STodd Fiala break; 3305af245d11STodd Fiala case FPE_FLTDIV: 3306af245d11STodd Fiala reason = ProcessMessage::eFloatDivideByZero; 3307af245d11STodd Fiala break; 3308af245d11STodd Fiala case FPE_FLTOVF: 3309af245d11STodd Fiala reason = ProcessMessage::eFloatOverflow; 3310af245d11STodd Fiala break; 3311af245d11STodd Fiala case FPE_FLTUND: 3312af245d11STodd Fiala reason = ProcessMessage::eFloatUnderflow; 3313af245d11STodd Fiala break; 3314af245d11STodd Fiala case FPE_FLTRES: 3315af245d11STodd Fiala reason = ProcessMessage::eFloatInexactResult; 3316af245d11STodd Fiala break; 3317af245d11STodd Fiala case FPE_FLTINV: 3318af245d11STodd Fiala reason = ProcessMessage::eFloatInvalidOperation; 3319af245d11STodd Fiala break; 3320af245d11STodd Fiala case FPE_FLTSUB: 3321af245d11STodd Fiala reason = ProcessMessage::eFloatSubscriptRange; 3322af245d11STodd Fiala break; 3323af245d11STodd Fiala } 3324af245d11STodd Fiala 3325af245d11STodd Fiala return reason; 3326af245d11STodd Fiala } 3327af245d11STodd Fiala #endif 3328af245d11STodd Fiala 3329af245d11STodd Fiala #if 0 3330af245d11STodd Fiala ProcessMessage::CrashReason 3331af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info) 3332af245d11STodd Fiala { 3333af245d11STodd Fiala ProcessMessage::CrashReason reason; 3334af245d11STodd Fiala assert(info->si_signo == SIGBUS); 3335af245d11STodd Fiala 3336af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 3337af245d11STodd Fiala 3338af245d11STodd Fiala switch (info->si_code) 3339af245d11STodd Fiala { 3340af245d11STodd Fiala default: 3341af245d11STodd Fiala assert(false && "unexpected si_code for SIGBUS"); 3342af245d11STodd Fiala break; 3343af245d11STodd Fiala case BUS_ADRALN: 3344af245d11STodd Fiala reason = ProcessMessage::eIllegalAlignment; 3345af245d11STodd Fiala break; 3346af245d11STodd Fiala case BUS_ADRERR: 3347af245d11STodd Fiala reason = ProcessMessage::eIllegalAddress; 3348af245d11STodd Fiala break; 3349af245d11STodd Fiala case BUS_OBJERR: 3350af245d11STodd Fiala reason = ProcessMessage::eHardwareError; 3351af245d11STodd Fiala break; 3352af245d11STodd Fiala } 3353af245d11STodd Fiala 3354af245d11STodd Fiala return reason; 3355af245d11STodd Fiala } 3356af245d11STodd Fiala #endif 3357af245d11STodd Fiala 3358af245d11STodd Fiala void 3359af245d11STodd Fiala NativeProcessLinux::ServeOperation(OperationArgs *args) 3360af245d11STodd Fiala { 3361af245d11STodd Fiala NativeProcessLinux *monitor = args->m_monitor; 3362af245d11STodd Fiala 3363af245d11STodd Fiala // We are finised with the arguments and are ready to go. Sync with the 3364af245d11STodd Fiala // parent thread and start serving operations on the inferior. 3365af245d11STodd Fiala sem_post(&args->m_semaphore); 3366af245d11STodd Fiala 3367af245d11STodd Fiala for(;;) 3368af245d11STodd Fiala { 3369af245d11STodd Fiala // wait for next pending operation 3370af245d11STodd Fiala if (sem_wait(&monitor->m_operation_pending)) 3371af245d11STodd Fiala { 3372af245d11STodd Fiala if (errno == EINTR) 3373af245d11STodd Fiala continue; 3374af245d11STodd Fiala assert(false && "Unexpected errno from sem_wait"); 3375af245d11STodd Fiala } 3376af245d11STodd Fiala 337743f2d971STamas Berghammer // EXIT_OPERATION used to stop the operation thread because Cancel() isn't supported on 337843f2d971STamas Berghammer // android. We don't have to send a post to the m_operation_done semaphore because in this 337943f2d971STamas Berghammer // case the synchronization is achieved by a Join() call 338043f2d971STamas Berghammer if (monitor->m_operation == EXIT_OPERATION) 33816806fdedSTamas Berghammer break; 33826806fdedSTamas Berghammer 3383af245d11STodd Fiala reinterpret_cast<Operation*>(monitor->m_operation)->Execute(monitor); 3384af245d11STodd Fiala 3385af245d11STodd Fiala // notify calling thread that operation is complete 3386af245d11STodd Fiala sem_post(&monitor->m_operation_done); 3387af245d11STodd Fiala } 3388af245d11STodd Fiala } 3389af245d11STodd Fiala 3390af245d11STodd Fiala void 3391af245d11STodd Fiala NativeProcessLinux::DoOperation(void *op) 3392af245d11STodd Fiala { 3393af245d11STodd Fiala Mutex::Locker lock(m_operation_mutex); 3394af245d11STodd Fiala 3395af245d11STodd Fiala m_operation = op; 3396af245d11STodd Fiala 3397af245d11STodd Fiala // notify operation thread that an operation is ready to be processed 3398af245d11STodd Fiala sem_post(&m_operation_pending); 3399af245d11STodd Fiala 340043f2d971STamas Berghammer // Don't wait for the operation to complete in case of an exit operation. The operation thread 340143f2d971STamas Berghammer // will exit without posting to the semaphore 340243f2d971STamas Berghammer if (m_operation == EXIT_OPERATION) 340343f2d971STamas Berghammer return; 340443f2d971STamas Berghammer 3405af245d11STodd Fiala // wait for operation to complete 3406af245d11STodd Fiala while (sem_wait(&m_operation_done)) 3407af245d11STodd Fiala { 3408af245d11STodd Fiala if (errno == EINTR) 3409af245d11STodd Fiala continue; 3410af245d11STodd Fiala assert(false && "Unexpected errno from sem_wait"); 3411af245d11STodd Fiala } 3412af245d11STodd Fiala } 3413af245d11STodd Fiala 3414af245d11STodd Fiala Error 3415af245d11STodd Fiala NativeProcessLinux::ReadMemory (lldb::addr_t addr, void *buf, lldb::addr_t size, lldb::addr_t &bytes_read) 3416af245d11STodd Fiala { 3417af245d11STodd Fiala ReadOperation op(addr, buf, size, bytes_read); 3418af245d11STodd Fiala DoOperation(&op); 3419af245d11STodd Fiala return op.GetError (); 3420af245d11STodd Fiala } 3421af245d11STodd Fiala 3422af245d11STodd Fiala Error 3423af245d11STodd Fiala NativeProcessLinux::WriteMemory (lldb::addr_t addr, const void *buf, lldb::addr_t size, lldb::addr_t &bytes_written) 3424af245d11STodd Fiala { 3425af245d11STodd Fiala WriteOperation op(addr, buf, size, bytes_written); 3426af245d11STodd Fiala DoOperation(&op); 3427af245d11STodd Fiala return op.GetError (); 3428af245d11STodd Fiala } 3429af245d11STodd Fiala 343097ccc294SChaoren Lin Error 3431af245d11STodd Fiala NativeProcessLinux::ReadRegisterValue(lldb::tid_t tid, uint32_t offset, const char* reg_name, 3432af245d11STodd Fiala uint32_t size, RegisterValue &value) 3433af245d11STodd Fiala { 343497ccc294SChaoren Lin ReadRegOperation op(tid, offset, reg_name, value); 3435af245d11STodd Fiala DoOperation(&op); 343697ccc294SChaoren Lin return op.GetError(); 3437af245d11STodd Fiala } 3438af245d11STodd Fiala 343997ccc294SChaoren Lin Error 3440af245d11STodd Fiala NativeProcessLinux::WriteRegisterValue(lldb::tid_t tid, unsigned offset, 3441af245d11STodd Fiala const char* reg_name, const RegisterValue &value) 3442af245d11STodd Fiala { 344397ccc294SChaoren Lin WriteRegOperation op(tid, offset, reg_name, value); 3444af245d11STodd Fiala DoOperation(&op); 344597ccc294SChaoren Lin return op.GetError(); 3446af245d11STodd Fiala } 3447af245d11STodd Fiala 344897ccc294SChaoren Lin Error 3449af245d11STodd Fiala NativeProcessLinux::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size) 3450af245d11STodd Fiala { 345197ccc294SChaoren Lin ReadGPROperation op(tid, buf, buf_size); 3452af245d11STodd Fiala DoOperation(&op); 345397ccc294SChaoren Lin return op.GetError(); 3454af245d11STodd Fiala } 3455af245d11STodd Fiala 345697ccc294SChaoren Lin Error 3457af245d11STodd Fiala NativeProcessLinux::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size) 3458af245d11STodd Fiala { 345997ccc294SChaoren Lin ReadFPROperation op(tid, buf, buf_size); 3460af245d11STodd Fiala DoOperation(&op); 346197ccc294SChaoren Lin return op.GetError(); 3462af245d11STodd Fiala } 3463af245d11STodd Fiala 346497ccc294SChaoren Lin Error 3465af245d11STodd Fiala NativeProcessLinux::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) 3466af245d11STodd Fiala { 346797ccc294SChaoren Lin ReadRegisterSetOperation op(tid, buf, buf_size, regset); 3468af245d11STodd Fiala DoOperation(&op); 346997ccc294SChaoren Lin return op.GetError(); 3470af245d11STodd Fiala } 3471af245d11STodd Fiala 347297ccc294SChaoren Lin Error 3473af245d11STodd Fiala NativeProcessLinux::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size) 3474af245d11STodd Fiala { 347597ccc294SChaoren Lin WriteGPROperation op(tid, buf, buf_size); 3476af245d11STodd Fiala DoOperation(&op); 347797ccc294SChaoren Lin return op.GetError(); 3478af245d11STodd Fiala } 3479af245d11STodd Fiala 348097ccc294SChaoren Lin Error 3481af245d11STodd Fiala NativeProcessLinux::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size) 3482af245d11STodd Fiala { 348397ccc294SChaoren Lin WriteFPROperation op(tid, buf, buf_size); 3484af245d11STodd Fiala DoOperation(&op); 348597ccc294SChaoren Lin return op.GetError(); 3486af245d11STodd Fiala } 3487af245d11STodd Fiala 348897ccc294SChaoren Lin Error 3489af245d11STodd Fiala NativeProcessLinux::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) 3490af245d11STodd Fiala { 349197ccc294SChaoren Lin WriteRegisterSetOperation op(tid, buf, buf_size, regset); 3492af245d11STodd Fiala DoOperation(&op); 349397ccc294SChaoren Lin return op.GetError(); 3494af245d11STodd Fiala } 3495af245d11STodd Fiala 349697ccc294SChaoren Lin Error 3497af245d11STodd Fiala NativeProcessLinux::Resume (lldb::tid_t tid, uint32_t signo) 3498af245d11STodd Fiala { 3499af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3500af245d11STodd Fiala 3501af245d11STodd Fiala if (log) 3502af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid, 3503af245d11STodd Fiala GetUnixSignals().GetSignalAsCString (signo)); 350497ccc294SChaoren Lin ResumeOperation op (tid, signo); 3505af245d11STodd Fiala DoOperation (&op); 3506af245d11STodd Fiala if (log) 350797ccc294SChaoren Lin log->Printf ("NativeProcessLinux::%s() resuming thread = %" PRIu64 " result = %s", __FUNCTION__, tid, op.GetError().Success() ? "true" : "false"); 350897ccc294SChaoren Lin return op.GetError(); 3509af245d11STodd Fiala } 3510af245d11STodd Fiala 351197ccc294SChaoren Lin Error 3512af245d11STodd Fiala NativeProcessLinux::SingleStep(lldb::tid_t tid, uint32_t signo) 3513af245d11STodd Fiala { 351497ccc294SChaoren Lin SingleStepOperation op(tid, signo); 3515af245d11STodd Fiala DoOperation(&op); 351697ccc294SChaoren Lin return op.GetError(); 3517af245d11STodd Fiala } 3518af245d11STodd Fiala 351997ccc294SChaoren Lin Error 352097ccc294SChaoren Lin NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) 3521af245d11STodd Fiala { 352297ccc294SChaoren Lin SiginfoOperation op(tid, siginfo); 3523af245d11STodd Fiala DoOperation(&op); 352497ccc294SChaoren Lin return op.GetError(); 3525af245d11STodd Fiala } 3526af245d11STodd Fiala 352797ccc294SChaoren Lin Error 3528af245d11STodd Fiala NativeProcessLinux::GetEventMessage(lldb::tid_t tid, unsigned long *message) 3529af245d11STodd Fiala { 353097ccc294SChaoren Lin EventMessageOperation op(tid, message); 3531af245d11STodd Fiala DoOperation(&op); 353297ccc294SChaoren Lin return op.GetError(); 3533af245d11STodd Fiala } 3534af245d11STodd Fiala 3535af245d11STodd Fiala lldb_private::Error 3536af245d11STodd Fiala NativeProcessLinux::Detach(lldb::tid_t tid) 3537af245d11STodd Fiala { 353897ccc294SChaoren Lin if (tid == LLDB_INVALID_THREAD_ID) 353997ccc294SChaoren Lin return Error(); 354097ccc294SChaoren Lin 354197ccc294SChaoren Lin DetachOperation op(tid); 3542af245d11STodd Fiala DoOperation(&op); 354397ccc294SChaoren Lin return op.GetError(); 3544af245d11STodd Fiala } 3545af245d11STodd Fiala 3546af245d11STodd Fiala bool 3547af245d11STodd Fiala NativeProcessLinux::DupDescriptor(const char *path, int fd, int flags) 3548af245d11STodd Fiala { 3549af245d11STodd Fiala int target_fd = open(path, flags, 0666); 3550af245d11STodd Fiala 3551af245d11STodd Fiala if (target_fd == -1) 3552af245d11STodd Fiala return false; 3553af245d11STodd Fiala 3554493c3a12SPavel Labath if (dup2(target_fd, fd) == -1) 3555493c3a12SPavel Labath return false; 3556493c3a12SPavel Labath 3557493c3a12SPavel Labath return (close(target_fd) == -1) ? false : true; 3558af245d11STodd Fiala } 3559af245d11STodd Fiala 3560af245d11STodd Fiala void 3561*0cbf0b13STamas Berghammer NativeProcessLinux::StopMonitorThread() 3562af245d11STodd Fiala { 3563acee96aeSZachary Turner if (m_monitor_thread.IsJoinable()) 3564af245d11STodd Fiala { 3565*0cbf0b13STamas Berghammer ::pthread_kill(m_monitor_thread.GetNativeThread().GetSystemHandle(), SIGUSR1); 356639de3110SZachary Turner m_monitor_thread.Join(nullptr); 3567af245d11STodd Fiala } 3568af245d11STodd Fiala } 3569af245d11STodd Fiala 3570af245d11STodd Fiala void 3571af245d11STodd Fiala NativeProcessLinux::StopMonitor() 3572af245d11STodd Fiala { 3573*0cbf0b13STamas Berghammer StopMonitorThread(); 3574fa03ad2eSChaoren Lin StopCoordinatorThread (); 357543f2d971STamas Berghammer StopOpThread(); 3576af245d11STodd Fiala sem_destroy(&m_operation_pending); 3577af245d11STodd Fiala sem_destroy(&m_operation_done); 3578af245d11STodd Fiala 3579af245d11STodd Fiala // TODO: validate whether this still holds, fix up comment. 3580af245d11STodd Fiala // Note: ProcessPOSIX passes the m_terminal_fd file descriptor to 3581af245d11STodd Fiala // Process::SetSTDIOFileDescriptor, which in turn transfers ownership of 3582af245d11STodd Fiala // the descriptor to a ConnectionFileDescriptor object. Consequently 3583af245d11STodd Fiala // even though still has the file descriptor, we shouldn't close it here. 3584af245d11STodd Fiala } 3585af245d11STodd Fiala 3586af245d11STodd Fiala void 3587af245d11STodd Fiala NativeProcessLinux::StopOpThread() 3588af245d11STodd Fiala { 3589acee96aeSZachary Turner if (!m_operation_thread.IsJoinable()) 3590af245d11STodd Fiala return; 3591af245d11STodd Fiala 359243f2d971STamas Berghammer DoOperation(EXIT_OPERATION); 359339de3110SZachary Turner m_operation_thread.Join(nullptr); 3594af245d11STodd Fiala } 3595af245d11STodd Fiala 3596fa03ad2eSChaoren Lin Error 3597fa03ad2eSChaoren Lin NativeProcessLinux::StartCoordinatorThread () 3598fa03ad2eSChaoren Lin { 3599fa03ad2eSChaoren Lin Error error; 3600fa03ad2eSChaoren Lin static const char *g_thread_name = "lldb.process.linux.ts_coordinator"; 3601fa03ad2eSChaoren Lin Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 3602fa03ad2eSChaoren Lin 3603fa03ad2eSChaoren Lin // Skip if thread is already running 3604fa03ad2eSChaoren Lin if (m_coordinator_thread.IsJoinable()) 3605fa03ad2eSChaoren Lin { 3606fa03ad2eSChaoren Lin error.SetErrorString ("ThreadStateCoordinator's run loop is already running"); 3607fa03ad2eSChaoren Lin if (log) 3608fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s %s", __FUNCTION__, error.AsCString ()); 3609fa03ad2eSChaoren Lin return error; 3610fa03ad2eSChaoren Lin } 3611fa03ad2eSChaoren Lin 3612fa03ad2eSChaoren Lin // Enable verbose logging if lldb thread logging is enabled. 3613fa03ad2eSChaoren Lin m_coordinator_up->LogEnableEventProcessing (log != nullptr); 3614fa03ad2eSChaoren Lin 3615fa03ad2eSChaoren Lin if (log) 3616fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s launching ThreadStateCoordinator thread for pid %" PRIu64, __FUNCTION__, GetID ()); 3617fa03ad2eSChaoren Lin m_coordinator_thread = ThreadLauncher::LaunchThread(g_thread_name, CoordinatorThread, this, &error); 3618fa03ad2eSChaoren Lin return error; 3619fa03ad2eSChaoren Lin } 3620fa03ad2eSChaoren Lin 3621fa03ad2eSChaoren Lin void * 3622fa03ad2eSChaoren Lin NativeProcessLinux::CoordinatorThread (void *arg) 3623fa03ad2eSChaoren Lin { 3624fa03ad2eSChaoren Lin Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 3625fa03ad2eSChaoren Lin 3626fa03ad2eSChaoren Lin NativeProcessLinux *const process = static_cast<NativeProcessLinux*> (arg); 3627fa03ad2eSChaoren Lin assert (process && "null process passed to CoordinatorThread"); 3628fa03ad2eSChaoren Lin if (!process) 3629fa03ad2eSChaoren Lin { 3630fa03ad2eSChaoren Lin if (log) 3631fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s null process, exiting ThreadStateCoordinator processing loop", __FUNCTION__); 3632fa03ad2eSChaoren Lin return nullptr; 3633fa03ad2eSChaoren Lin } 3634fa03ad2eSChaoren Lin 3635fa03ad2eSChaoren Lin // Run the thread state coordinator loop until it is done. This call uses 3636fa03ad2eSChaoren Lin // efficient waiting for an event to be ready. 3637fa03ad2eSChaoren Lin while (process->m_coordinator_up->ProcessNextEvent () == ThreadStateCoordinator::eventLoopResultContinue) 3638fa03ad2eSChaoren Lin { 3639fa03ad2eSChaoren Lin } 3640fa03ad2eSChaoren Lin 3641fa03ad2eSChaoren Lin if (log) 3642fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " exiting ThreadStateCoordinator processing loop due to coordinator indicating completion", __FUNCTION__, process->GetID ()); 3643fa03ad2eSChaoren Lin 3644fa03ad2eSChaoren Lin return nullptr; 3645fa03ad2eSChaoren Lin } 3646fa03ad2eSChaoren Lin 3647fa03ad2eSChaoren Lin void 3648fa03ad2eSChaoren Lin NativeProcessLinux::StopCoordinatorThread() 3649fa03ad2eSChaoren Lin { 3650fa03ad2eSChaoren Lin Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 3651fa03ad2eSChaoren Lin if (log) 3652fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s requesting ThreadStateCoordinator stop for pid %" PRIu64, __FUNCTION__, GetID ()); 3653fa03ad2eSChaoren Lin 3654fa03ad2eSChaoren Lin // Tell the coordinator we're done. This will cause the coordinator 3655fa03ad2eSChaoren Lin // run loop thread to exit when the processing queue hits this message. 3656fa03ad2eSChaoren Lin m_coordinator_up->StopCoordinator (); 36578bc34f4dSOleksiy Vyalov m_coordinator_thread.Join (nullptr); 3658fa03ad2eSChaoren Lin } 3659fa03ad2eSChaoren Lin 3660af245d11STodd Fiala bool 3661af245d11STodd Fiala NativeProcessLinux::HasThreadNoLock (lldb::tid_t thread_id) 3662af245d11STodd Fiala { 3663af245d11STodd Fiala for (auto thread_sp : m_threads) 3664af245d11STodd Fiala { 3665af245d11STodd Fiala assert (thread_sp && "thread list should not contain NULL threads"); 3666af245d11STodd Fiala if (thread_sp->GetID () == thread_id) 3667af245d11STodd Fiala { 3668af245d11STodd Fiala // We have this thread. 3669af245d11STodd Fiala return true; 3670af245d11STodd Fiala } 3671af245d11STodd Fiala } 3672af245d11STodd Fiala 3673af245d11STodd Fiala // We don't have this thread. 3674af245d11STodd Fiala return false; 3675af245d11STodd Fiala } 3676af245d11STodd Fiala 3677af245d11STodd Fiala NativeThreadProtocolSP 3678af245d11STodd Fiala NativeProcessLinux::MaybeGetThreadNoLock (lldb::tid_t thread_id) 3679af245d11STodd Fiala { 3680af245d11STodd Fiala // CONSIDER organize threads by map - we can do better than linear. 3681af245d11STodd Fiala for (auto thread_sp : m_threads) 3682af245d11STodd Fiala { 3683af245d11STodd Fiala if (thread_sp->GetID () == thread_id) 3684af245d11STodd Fiala return thread_sp; 3685af245d11STodd Fiala } 3686af245d11STodd Fiala 3687af245d11STodd Fiala // We don't have this thread. 3688af245d11STodd Fiala return NativeThreadProtocolSP (); 3689af245d11STodd Fiala } 3690af245d11STodd Fiala 3691af245d11STodd Fiala bool 3692af245d11STodd Fiala NativeProcessLinux::StopTrackingThread (lldb::tid_t thread_id) 3693af245d11STodd Fiala { 3694af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 3695af245d11STodd Fiala for (auto it = m_threads.begin (); it != m_threads.end (); ++it) 3696af245d11STodd Fiala { 3697af245d11STodd Fiala if (*it && ((*it)->GetID () == thread_id)) 3698af245d11STodd Fiala { 3699af245d11STodd Fiala m_threads.erase (it); 3700af245d11STodd Fiala return true; 3701af245d11STodd Fiala } 3702af245d11STodd Fiala } 3703af245d11STodd Fiala 3704af245d11STodd Fiala // Didn't find it. 3705af245d11STodd Fiala return false; 3706af245d11STodd Fiala } 3707af245d11STodd Fiala 3708af245d11STodd Fiala NativeThreadProtocolSP 3709af245d11STodd Fiala NativeProcessLinux::AddThread (lldb::tid_t thread_id) 3710af245d11STodd Fiala { 3711af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 3712af245d11STodd Fiala 3713af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 3714af245d11STodd Fiala 3715af245d11STodd Fiala if (log) 3716af245d11STodd Fiala { 3717af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " adding thread with tid %" PRIu64, 3718af245d11STodd Fiala __FUNCTION__, 3719af245d11STodd Fiala GetID (), 3720af245d11STodd Fiala thread_id); 3721af245d11STodd Fiala } 3722af245d11STodd Fiala 3723af245d11STodd Fiala assert (!HasThreadNoLock (thread_id) && "attempted to add a thread by id that already exists"); 3724af245d11STodd Fiala 3725af245d11STodd Fiala // If this is the first thread, save it as the current thread 3726af245d11STodd Fiala if (m_threads.empty ()) 3727af245d11STodd Fiala SetCurrentThreadID (thread_id); 3728af245d11STodd Fiala 3729af245d11STodd Fiala NativeThreadProtocolSP thread_sp (new NativeThreadLinux (this, thread_id)); 3730af245d11STodd Fiala m_threads.push_back (thread_sp); 3731af245d11STodd Fiala 3732af245d11STodd Fiala return thread_sp; 3733af245d11STodd Fiala } 3734af245d11STodd Fiala 3735af245d11STodd Fiala NativeThreadProtocolSP 3736af245d11STodd Fiala NativeProcessLinux::GetOrCreateThread (lldb::tid_t thread_id, bool &created) 3737af245d11STodd Fiala { 3738af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 3739af245d11STodd Fiala 3740af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 3741af245d11STodd Fiala if (log) 3742af245d11STodd Fiala { 3743af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " get/create thread with tid %" PRIu64, 3744af245d11STodd Fiala __FUNCTION__, 3745af245d11STodd Fiala GetID (), 3746af245d11STodd Fiala thread_id); 3747af245d11STodd Fiala } 3748af245d11STodd Fiala 3749af245d11STodd Fiala // Retrieve the thread if it is already getting tracked. 3750af245d11STodd Fiala NativeThreadProtocolSP thread_sp = MaybeGetThreadNoLock (thread_id); 3751af245d11STodd Fiala if (thread_sp) 3752af245d11STodd Fiala { 3753af245d11STodd Fiala if (log) 3754af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": thread already tracked, returning", 3755af245d11STodd Fiala __FUNCTION__, 3756af245d11STodd Fiala GetID (), 3757af245d11STodd Fiala thread_id); 3758af245d11STodd Fiala created = false; 3759af245d11STodd Fiala return thread_sp; 3760af245d11STodd Fiala 3761af245d11STodd Fiala } 3762af245d11STodd Fiala 3763af245d11STodd Fiala // Create the thread metadata since it isn't being tracked. 3764af245d11STodd Fiala if (log) 3765af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": thread didn't exist, tracking now", 3766af245d11STodd Fiala __FUNCTION__, 3767af245d11STodd Fiala GetID (), 3768af245d11STodd Fiala thread_id); 3769af245d11STodd Fiala 3770af245d11STodd Fiala thread_sp.reset (new NativeThreadLinux (this, thread_id)); 3771af245d11STodd Fiala m_threads.push_back (thread_sp); 3772af245d11STodd Fiala created = true; 3773af245d11STodd Fiala 3774af245d11STodd Fiala return thread_sp; 3775af245d11STodd Fiala } 3776af245d11STodd Fiala 3777af245d11STodd Fiala Error 3778af245d11STodd Fiala NativeProcessLinux::FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp) 3779af245d11STodd Fiala { 378075f47c3aSTodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 3781af245d11STodd Fiala 3782af245d11STodd Fiala Error error; 3783af245d11STodd Fiala 3784af245d11STodd Fiala // Get a linux thread pointer. 3785af245d11STodd Fiala if (!thread_sp) 3786af245d11STodd Fiala { 3787af245d11STodd Fiala error.SetErrorString ("null thread_sp"); 3788af245d11STodd Fiala if (log) 3789af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); 3790af245d11STodd Fiala return error; 3791af245d11STodd Fiala } 3792af245d11STodd Fiala NativeThreadLinux *const linux_thread_p = reinterpret_cast<NativeThreadLinux*> (thread_sp.get()); 3793af245d11STodd Fiala 3794af245d11STodd Fiala // Find out the size of a breakpoint (might depend on where we are in the code). 3795af245d11STodd Fiala NativeRegisterContextSP context_sp = linux_thread_p->GetRegisterContext (); 3796af245d11STodd Fiala if (!context_sp) 3797af245d11STodd Fiala { 3798af245d11STodd Fiala error.SetErrorString ("cannot get a NativeRegisterContext for the thread"); 3799af245d11STodd Fiala if (log) 3800af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); 3801af245d11STodd Fiala return error; 3802af245d11STodd Fiala } 3803af245d11STodd Fiala 3804af245d11STodd Fiala uint32_t breakpoint_size = 0; 3805af245d11STodd Fiala error = GetSoftwareBreakpointSize (context_sp, breakpoint_size); 3806af245d11STodd Fiala if (error.Fail ()) 3807af245d11STodd Fiala { 3808af245d11STodd Fiala if (log) 3809af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s GetBreakpointSize() failed: %s", __FUNCTION__, error.AsCString ()); 3810af245d11STodd Fiala return error; 3811af245d11STodd Fiala } 3812af245d11STodd Fiala else 3813af245d11STodd Fiala { 3814af245d11STodd Fiala if (log) 3815af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s breakpoint size: %" PRIu32, __FUNCTION__, breakpoint_size); 3816af245d11STodd Fiala } 3817af245d11STodd Fiala 3818af245d11STodd Fiala // First try probing for a breakpoint at a software breakpoint location: PC - breakpoint size. 3819af245d11STodd Fiala const lldb::addr_t initial_pc_addr = context_sp->GetPC (); 3820af245d11STodd Fiala lldb::addr_t breakpoint_addr = initial_pc_addr; 3821af245d11STodd Fiala if (breakpoint_size > static_cast<lldb::addr_t> (0)) 3822af245d11STodd Fiala { 3823af245d11STodd Fiala // Do not allow breakpoint probe to wrap around. 3824af245d11STodd Fiala if (breakpoint_addr >= static_cast<lldb::addr_t> (breakpoint_size)) 3825af245d11STodd Fiala breakpoint_addr -= static_cast<lldb::addr_t> (breakpoint_size); 3826af245d11STodd Fiala } 3827af245d11STodd Fiala 3828af245d11STodd Fiala // Check if we stopped because of a breakpoint. 3829af245d11STodd Fiala NativeBreakpointSP breakpoint_sp; 3830af245d11STodd Fiala error = m_breakpoint_list.GetBreakpoint (breakpoint_addr, breakpoint_sp); 3831af245d11STodd Fiala if (!error.Success () || !breakpoint_sp) 3832af245d11STodd Fiala { 3833af245d11STodd Fiala // We didn't find one at a software probe location. Nothing to do. 3834af245d11STodd Fiala if (log) 3835af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " no lldb breakpoint found at current pc with adjustment: 0x%" PRIx64, __FUNCTION__, GetID (), breakpoint_addr); 3836af245d11STodd Fiala return Error (); 3837af245d11STodd Fiala } 3838af245d11STodd Fiala 3839af245d11STodd Fiala // If the breakpoint is not a software breakpoint, nothing to do. 3840af245d11STodd Fiala if (!breakpoint_sp->IsSoftwareBreakpoint ()) 3841af245d11STodd Fiala { 3842af245d11STodd Fiala if (log) 3843af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " breakpoint found at 0x%" PRIx64 ", not software, nothing to adjust", __FUNCTION__, GetID (), breakpoint_addr); 3844af245d11STodd Fiala return Error (); 3845af245d11STodd Fiala } 3846af245d11STodd Fiala 3847af245d11STodd Fiala // 3848af245d11STodd Fiala // We have a software breakpoint and need to adjust the PC. 3849af245d11STodd Fiala // 3850af245d11STodd Fiala 3851af245d11STodd Fiala // Sanity check. 3852af245d11STodd Fiala if (breakpoint_size == 0) 3853af245d11STodd Fiala { 3854af245d11STodd Fiala // Nothing to do! How did we get here? 3855af245d11STodd Fiala if (log) 3856af245d11STodd 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); 3857af245d11STodd Fiala return Error (); 3858af245d11STodd Fiala } 3859af245d11STodd Fiala 3860af245d11STodd Fiala // Change the program counter. 3861af245d11STodd Fiala if (log) 3862af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": changing PC from 0x%" PRIx64 " to 0x%" PRIx64, __FUNCTION__, GetID (), linux_thread_p->GetID (), initial_pc_addr, breakpoint_addr); 3863af245d11STodd Fiala 3864af245d11STodd Fiala error = context_sp->SetPC (breakpoint_addr); 3865af245d11STodd Fiala if (error.Fail ()) 3866af245d11STodd Fiala { 3867af245d11STodd Fiala if (log) 3868af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": failed to set PC: %s", __FUNCTION__, GetID (), linux_thread_p->GetID (), error.AsCString ()); 3869af245d11STodd Fiala return error; 3870af245d11STodd Fiala } 3871af245d11STodd Fiala 3872af245d11STodd Fiala return error; 3873af245d11STodd Fiala } 3874fa03ad2eSChaoren Lin 3875fa03ad2eSChaoren Lin void 3876fa03ad2eSChaoren Lin NativeProcessLinux::NotifyThreadCreateStopped (lldb::tid_t tid) 3877fa03ad2eSChaoren Lin { 3878fa03ad2eSChaoren Lin const bool is_stopped = true; 3879fa03ad2eSChaoren Lin m_coordinator_up->NotifyThreadCreate (tid, is_stopped, CoordinatorErrorHandler); 3880fa03ad2eSChaoren Lin } 3881fa03ad2eSChaoren Lin 3882fa03ad2eSChaoren Lin void 3883fa03ad2eSChaoren Lin NativeProcessLinux::NotifyThreadDeath (lldb::tid_t tid) 3884fa03ad2eSChaoren Lin { 3885fa03ad2eSChaoren Lin m_coordinator_up->NotifyThreadDeath (tid, CoordinatorErrorHandler); 3886fa03ad2eSChaoren Lin } 3887fa03ad2eSChaoren Lin 3888fa03ad2eSChaoren Lin void 3889fa03ad2eSChaoren Lin NativeProcessLinux::NotifyThreadStop (lldb::tid_t tid) 3890fa03ad2eSChaoren Lin { 389186fd8e45SChaoren Lin m_coordinator_up->NotifyThreadStop (tid, false, CoordinatorErrorHandler); 3892fa03ad2eSChaoren Lin } 3893fa03ad2eSChaoren Lin 3894fa03ad2eSChaoren Lin void 3895fa03ad2eSChaoren Lin NativeProcessLinux::CallAfterRunningThreadsStop (lldb::tid_t tid, 3896fa03ad2eSChaoren Lin const std::function<void (lldb::tid_t tid)> &call_after_function) 3897fa03ad2eSChaoren Lin { 389886fd8e45SChaoren Lin Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 389986fd8e45SChaoren Lin if (log) 390086fd8e45SChaoren Lin log->Printf("NativeProcessLinux::%s tid %" PRIu64, __FUNCTION__, tid); 390186fd8e45SChaoren Lin 3902fa03ad2eSChaoren Lin const lldb::pid_t pid = GetID (); 3903938fcf63SChaoren Lin m_coordinator_up->CallAfterRunningThreadsStop (tid, 3904fa03ad2eSChaoren Lin [=](lldb::tid_t request_stop_tid) 3905fa03ad2eSChaoren Lin { 390637c768caSChaoren Lin return RequestThreadStop(pid, request_stop_tid); 3907fa03ad2eSChaoren Lin }, 3908fa03ad2eSChaoren Lin call_after_function, 3909fa03ad2eSChaoren Lin CoordinatorErrorHandler); 391003f12d6bSChaoren Lin } 3911fa03ad2eSChaoren Lin 391203f12d6bSChaoren Lin void 391303f12d6bSChaoren Lin NativeProcessLinux::CallAfterRunningThreadsStopWithSkipTID (lldb::tid_t deferred_signal_tid, 391403f12d6bSChaoren Lin lldb::tid_t skip_stop_request_tid, 391503f12d6bSChaoren Lin const std::function<void (lldb::tid_t tid)> &call_after_function) 391603f12d6bSChaoren Lin { 391786fd8e45SChaoren Lin Log *const log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 391886fd8e45SChaoren Lin if (log) 391986fd8e45SChaoren Lin log->Printf("NativeProcessLinux::%s deferred_signal_tid %" PRIu64 ", skip_stop_request_tid %" PRIu64, __FUNCTION__, deferred_signal_tid, skip_stop_request_tid); 392086fd8e45SChaoren Lin 392103f12d6bSChaoren Lin const lldb::pid_t pid = GetID (); 392203f12d6bSChaoren Lin m_coordinator_up->CallAfterRunningThreadsStopWithSkipTIDs (deferred_signal_tid, 392303f12d6bSChaoren Lin skip_stop_request_tid != LLDB_INVALID_THREAD_ID ? ThreadStateCoordinator::ThreadIDSet {skip_stop_request_tid} : ThreadStateCoordinator::ThreadIDSet (), 392403f12d6bSChaoren Lin [=](lldb::tid_t request_stop_tid) 392503f12d6bSChaoren Lin { 392637c768caSChaoren Lin return RequestThreadStop(pid, request_stop_tid); 392703f12d6bSChaoren Lin }, 392803f12d6bSChaoren Lin call_after_function, 392903f12d6bSChaoren Lin CoordinatorErrorHandler); 3930fa03ad2eSChaoren Lin } 393186fd8e45SChaoren Lin 393286fd8e45SChaoren Lin lldb_private::Error 393386fd8e45SChaoren Lin NativeProcessLinux::RequestThreadStop (const lldb::pid_t pid, const lldb::tid_t tid) 393486fd8e45SChaoren Lin { 393586fd8e45SChaoren Lin Log* log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 393686fd8e45SChaoren Lin if (log) 393786fd8e45SChaoren Lin log->Printf ("NativeProcessLinux::%s requesting thread stop(pid: %" PRIu64 ", tid: %" PRIu64 ")", __FUNCTION__, pid, tid); 393886fd8e45SChaoren Lin 393986fd8e45SChaoren Lin Error err; 394086fd8e45SChaoren Lin errno = 0; 394186fd8e45SChaoren Lin if (::tgkill (pid, tid, SIGSTOP) != 0) 394286fd8e45SChaoren Lin { 394386fd8e45SChaoren Lin err.SetErrorToErrno (); 394486fd8e45SChaoren Lin if (log) 394586fd8e45SChaoren Lin log->Printf ("NativeProcessLinux::%s tgkill(%" PRIu64 ", %" PRIu64 ", SIGSTOP) failed: %s", __FUNCTION__, pid, tid, err.AsCString ()); 394686fd8e45SChaoren Lin } 394786fd8e45SChaoren Lin 394886fd8e45SChaoren Lin return err; 394986fd8e45SChaoren Lin } 3950