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 #include <linux/unistd.h> 210bce1b67STodd Fiala #include <sys/personality.h> 22af245d11STodd Fiala #include <sys/ptrace.h> 236ac1be4bSTodd Fiala #include <sys/uio.h> 24af245d11STodd Fiala #include <sys/socket.h> 25af245d11STodd Fiala #include <sys/syscall.h> 26af245d11STodd Fiala #include <sys/types.h> 27af245d11STodd Fiala #include <sys/user.h> 28af245d11STodd Fiala #include <sys/wait.h> 29af245d11STodd Fiala 306ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 316ac1be4bSTodd Fiala // NT_PRSTATUS and NT_FPREGSET definition 326ac1be4bSTodd Fiala #include <elf.h> 336ac1be4bSTodd Fiala #endif 346ac1be4bSTodd Fiala 35af245d11STodd Fiala // C++ Includes 36af245d11STodd Fiala #include <fstream> 37af245d11STodd Fiala #include <string> 38af245d11STodd Fiala 39af245d11STodd Fiala // Other libraries and framework includes 40af245d11STodd Fiala #include "lldb/Core/Debugger.h" 41af245d11STodd Fiala #include "lldb/Core/Error.h" 42af245d11STodd Fiala #include "lldb/Core/Module.h" 43af245d11STodd Fiala #include "lldb/Core/RegisterValue.h" 44af245d11STodd Fiala #include "lldb/Core/Scalar.h" 45af245d11STodd Fiala #include "lldb/Core/State.h" 46af245d11STodd Fiala #include "lldb/Host/Host.h" 4713b18261SZachary Turner #include "lldb/Host/HostInfo.h" 4839de3110SZachary Turner #include "lldb/Host/ThreadLauncher.h" 49af245d11STodd Fiala #include "lldb/Symbol/ObjectFile.h" 50af245d11STodd Fiala #include "lldb/Target/NativeRegisterContext.h" 51af245d11STodd Fiala #include "lldb/Target/ProcessLaunchInfo.h" 52af245d11STodd Fiala #include "lldb/Utility/PseudoTerminal.h" 53af245d11STodd Fiala 54af245d11STodd Fiala #include "Host/common/NativeBreakpoint.h" 55af245d11STodd Fiala #include "Utility/StringExtractor.h" 56af245d11STodd Fiala 57af245d11STodd Fiala #include "Plugins/Process/Utility/LinuxSignals.h" 58af245d11STodd Fiala #include "NativeThreadLinux.h" 59af245d11STodd Fiala #include "ProcFileReader.h" 60af245d11STodd Fiala #include "ProcessPOSIXLog.h" 61af245d11STodd Fiala 62af245d11STodd Fiala #define DEBUG_PTRACE_MAXBYTES 20 63af245d11STodd Fiala 64af245d11STodd Fiala // Support ptrace extensions even when compiled without required kernel support 65dda61943STodd Fiala #ifndef PT_GETREGS 66af245d11STodd Fiala #ifndef PTRACE_GETREGS 67af245d11STodd Fiala #define PTRACE_GETREGS 12 68af245d11STodd Fiala #endif 69dda61943STodd Fiala #endif 70dda61943STodd Fiala #ifndef PT_SETREGS 71af245d11STodd Fiala #ifndef PTRACE_SETREGS 72af245d11STodd Fiala #define PTRACE_SETREGS 13 73af245d11STodd Fiala #endif 74dda61943STodd Fiala #endif 75dda61943STodd Fiala #ifndef PT_GETFPREGS 76dda61943STodd Fiala #ifndef PTRACE_GETFPREGS 77dda61943STodd Fiala #define PTRACE_GETFPREGS 14 78dda61943STodd Fiala #endif 79dda61943STodd Fiala #endif 80dda61943STodd Fiala #ifndef PT_SETFPREGS 81dda61943STodd Fiala #ifndef PTRACE_SETFPREGS 82dda61943STodd Fiala #define PTRACE_SETFPREGS 15 83dda61943STodd Fiala #endif 84dda61943STodd Fiala #endif 85af245d11STodd Fiala #ifndef PTRACE_GETREGSET 86af245d11STodd Fiala #define PTRACE_GETREGSET 0x4204 87af245d11STodd Fiala #endif 88af245d11STodd Fiala #ifndef PTRACE_SETREGSET 89af245d11STodd Fiala #define PTRACE_SETREGSET 0x4205 90af245d11STodd Fiala #endif 91af245d11STodd Fiala #ifndef PTRACE_GET_THREAD_AREA 92af245d11STodd Fiala #define PTRACE_GET_THREAD_AREA 25 93af245d11STodd Fiala #endif 94af245d11STodd Fiala #ifndef PTRACE_ARCH_PRCTL 95af245d11STodd Fiala #define PTRACE_ARCH_PRCTL 30 96af245d11STodd Fiala #endif 97af245d11STodd Fiala #ifndef ARCH_GET_FS 98af245d11STodd Fiala #define ARCH_SET_GS 0x1001 99af245d11STodd Fiala #define ARCH_SET_FS 0x1002 100af245d11STodd Fiala #define ARCH_GET_FS 0x1003 101af245d11STodd Fiala #define ARCH_GET_GS 0x1004 102af245d11STodd Fiala #endif 103af245d11STodd Fiala 1040bce1b67STodd Fiala #define LLDB_PERSONALITY_GET_CURRENT_SETTINGS 0xffffffff 105af245d11STodd Fiala 106af245d11STodd Fiala // Support hardware breakpoints in case it has not been defined 107af245d11STodd Fiala #ifndef TRAP_HWBKPT 108af245d11STodd Fiala #define TRAP_HWBKPT 4 109af245d11STodd Fiala #endif 110af245d11STodd Fiala 111af245d11STodd Fiala // Try to define a macro to encapsulate the tgkill syscall 112af245d11STodd Fiala // fall back on kill() if tgkill isn't available 113af245d11STodd Fiala #define tgkill(pid, tid, sig) syscall(SYS_tgkill, pid, tid, sig) 114af245d11STodd Fiala 115af245d11STodd Fiala // We disable the tracing of ptrace calls for integration builds to 116af245d11STodd Fiala // avoid the additional indirection and checks. 117af245d11STodd Fiala #ifndef LLDB_CONFIGURATION_BUILDANDINTEGRATION 118af245d11STodd Fiala #define PTRACE(req, pid, addr, data, data_size) \ 119af245d11STodd Fiala PtraceWrapper((req), (pid), (addr), (data), (data_size), #req, __FILE__, __LINE__) 120af245d11STodd Fiala #else 121af245d11STodd Fiala #define PTRACE(req, pid, addr, data, data_size) \ 122af245d11STodd Fiala PtraceWrapper((req), (pid), (addr), (data), (data_size)) 123af245d11STodd Fiala #endif 124af245d11STodd Fiala 125af245d11STodd Fiala // Private bits we only need internally. 126af245d11STodd Fiala namespace 127af245d11STodd Fiala { 128af245d11STodd Fiala using namespace lldb; 129af245d11STodd Fiala using namespace lldb_private; 130af245d11STodd Fiala 131af245d11STodd Fiala const UnixSignals& 132af245d11STodd Fiala GetUnixSignals () 133af245d11STodd Fiala { 134af245d11STodd Fiala static process_linux::LinuxSignals signals; 135af245d11STodd Fiala return signals; 136af245d11STodd Fiala } 137af245d11STodd Fiala 138af245d11STodd Fiala const char * 139696b5287SZachary Turner GetFilePath(const lldb_private::FileAction *file_action, const char *default_path) 140af245d11STodd Fiala { 141af245d11STodd Fiala const char *pts_name = "/dev/pts/"; 142af245d11STodd Fiala const char *path = NULL; 143af245d11STodd Fiala 144af245d11STodd Fiala if (file_action) 145af245d11STodd Fiala { 146696b5287SZachary Turner if (file_action->GetAction() == FileAction::eFileActionOpen) 147af245d11STodd Fiala { 148af245d11STodd Fiala path = file_action->GetPath (); 149af245d11STodd Fiala // By default the stdio paths passed in will be pseudo-terminal 150af245d11STodd Fiala // (/dev/pts). If so, convert to using a different default path 151af245d11STodd Fiala // instead to redirect I/O to the debugger console. This should 152af245d11STodd Fiala // also handle user overrides to /dev/null or a different file. 153af245d11STodd Fiala if (!path || ::strncmp (path, pts_name, ::strlen (pts_name)) == 0) 154af245d11STodd Fiala path = default_path; 155af245d11STodd Fiala } 156af245d11STodd Fiala } 157af245d11STodd Fiala 158af245d11STodd Fiala return path; 159af245d11STodd Fiala } 160af245d11STodd Fiala 161af245d11STodd Fiala Error 162af245d11STodd Fiala ResolveProcessArchitecture (lldb::pid_t pid, Platform &platform, ArchSpec &arch) 163af245d11STodd Fiala { 164af245d11STodd Fiala // Grab process info for the running process. 165af245d11STodd Fiala ProcessInstanceInfo process_info; 166af245d11STodd Fiala if (!platform.GetProcessInfo (pid, process_info)) 167af245d11STodd Fiala return lldb_private::Error("failed to get process info"); 168af245d11STodd Fiala 169af245d11STodd Fiala // Resolve the executable module. 170af245d11STodd Fiala ModuleSP exe_module_sp; 171af245d11STodd Fiala FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths ()); 172af245d11STodd Fiala Error error = platform.ResolveExecutable( 173af245d11STodd Fiala process_info.GetExecutableFile (), 174af245d11STodd Fiala platform.GetSystemArchitecture (), 175af245d11STodd Fiala exe_module_sp, 176af245d11STodd Fiala executable_search_paths.GetSize () ? &executable_search_paths : NULL); 177af245d11STodd Fiala 178af245d11STodd Fiala if (!error.Success ()) 179af245d11STodd Fiala return error; 180af245d11STodd Fiala 181af245d11STodd Fiala // Check if we've got our architecture from the exe_module. 182af245d11STodd Fiala arch = exe_module_sp->GetArchitecture (); 183af245d11STodd Fiala if (arch.IsValid ()) 184af245d11STodd Fiala return Error(); 185af245d11STodd Fiala else 186af245d11STodd Fiala return Error("failed to retrieve a valid architecture from the exe module"); 187af245d11STodd Fiala } 188af245d11STodd Fiala 189af245d11STodd Fiala void 190af245d11STodd Fiala DisplayBytes (lldb_private::StreamString &s, void *bytes, uint32_t count) 191af245d11STodd Fiala { 192af245d11STodd Fiala uint8_t *ptr = (uint8_t *)bytes; 193af245d11STodd Fiala const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count); 194af245d11STodd Fiala for(uint32_t i=0; i<loop_count; i++) 195af245d11STodd Fiala { 196af245d11STodd Fiala s.Printf ("[%x]", *ptr); 197af245d11STodd Fiala ptr++; 198af245d11STodd Fiala } 199af245d11STodd Fiala } 200af245d11STodd Fiala 201af245d11STodd Fiala void 202af245d11STodd Fiala PtraceDisplayBytes(int &req, void *data, size_t data_size) 203af245d11STodd Fiala { 204af245d11STodd Fiala StreamString buf; 205af245d11STodd Fiala Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet ( 206af245d11STodd Fiala POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE)); 207af245d11STodd Fiala 208af245d11STodd Fiala if (verbose_log) 209af245d11STodd Fiala { 210af245d11STodd Fiala switch(req) 211af245d11STodd Fiala { 212af245d11STodd Fiala case PTRACE_POKETEXT: 213af245d11STodd Fiala { 214af245d11STodd Fiala DisplayBytes(buf, &data, 8); 215af245d11STodd Fiala verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData()); 216af245d11STodd Fiala break; 217af245d11STodd Fiala } 218af245d11STodd Fiala case PTRACE_POKEDATA: 219af245d11STodd Fiala { 220af245d11STodd Fiala DisplayBytes(buf, &data, 8); 221af245d11STodd Fiala verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData()); 222af245d11STodd Fiala break; 223af245d11STodd Fiala } 224af245d11STodd Fiala case PTRACE_POKEUSER: 225af245d11STodd Fiala { 226af245d11STodd Fiala DisplayBytes(buf, &data, 8); 227af245d11STodd Fiala verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData()); 228af245d11STodd Fiala break; 229af245d11STodd Fiala } 230af245d11STodd Fiala case PTRACE_SETREGS: 231af245d11STodd Fiala { 232af245d11STodd Fiala DisplayBytes(buf, data, data_size); 233af245d11STodd Fiala verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData()); 234af245d11STodd Fiala break; 235af245d11STodd Fiala } 236af245d11STodd Fiala case PTRACE_SETFPREGS: 237af245d11STodd Fiala { 238af245d11STodd Fiala DisplayBytes(buf, data, data_size); 239af245d11STodd Fiala verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData()); 240af245d11STodd Fiala break; 241af245d11STodd Fiala } 242af245d11STodd Fiala case PTRACE_SETSIGINFO: 243af245d11STodd Fiala { 244af245d11STodd Fiala DisplayBytes(buf, data, sizeof(siginfo_t)); 245af245d11STodd Fiala verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData()); 246af245d11STodd Fiala break; 247af245d11STodd Fiala } 248af245d11STodd Fiala case PTRACE_SETREGSET: 249af245d11STodd Fiala { 250af245d11STodd Fiala // Extract iov_base from data, which is a pointer to the struct IOVEC 251af245d11STodd Fiala DisplayBytes(buf, *(void **)data, data_size); 252af245d11STodd Fiala verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData()); 253af245d11STodd Fiala break; 254af245d11STodd Fiala } 255af245d11STodd Fiala default: 256af245d11STodd Fiala { 257af245d11STodd Fiala } 258af245d11STodd Fiala } 259af245d11STodd Fiala } 260af245d11STodd Fiala } 261af245d11STodd Fiala 262af245d11STodd Fiala // Wrapper for ptrace to catch errors and log calls. 263af245d11STodd Fiala // Note that ptrace sets errno on error because -1 can be a valid result (i.e. for PTRACE_PEEK*) 264af245d11STodd Fiala long 265af245d11STodd Fiala PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, 266af245d11STodd Fiala const char* reqName, const char* file, int line) 267af245d11STodd Fiala { 268af245d11STodd Fiala long int result; 269af245d11STodd Fiala 270af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE)); 271af245d11STodd Fiala 272af245d11STodd Fiala PtraceDisplayBytes(req, data, data_size); 273af245d11STodd Fiala 274af245d11STodd Fiala errno = 0; 275af245d11STodd Fiala if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) 276af245d11STodd Fiala result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), *(unsigned int *)addr, data); 277af245d11STodd Fiala else 278af245d11STodd Fiala result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), addr, data); 279af245d11STodd Fiala 280af245d11STodd Fiala if (log) 281af245d11STodd Fiala log->Printf("ptrace(%s, %" PRIu64 ", %p, %p, %zu)=%lX called from file %s line %d", 282af245d11STodd Fiala reqName, pid, addr, data, data_size, result, file, line); 283af245d11STodd Fiala 284af245d11STodd Fiala PtraceDisplayBytes(req, data, data_size); 285af245d11STodd Fiala 286af245d11STodd Fiala if (log && errno != 0) 287af245d11STodd Fiala { 288af245d11STodd Fiala const char* str; 289af245d11STodd Fiala switch (errno) 290af245d11STodd Fiala { 291af245d11STodd Fiala case ESRCH: str = "ESRCH"; break; 292af245d11STodd Fiala case EINVAL: str = "EINVAL"; break; 293af245d11STodd Fiala case EBUSY: str = "EBUSY"; break; 294af245d11STodd Fiala case EPERM: str = "EPERM"; break; 295af245d11STodd Fiala default: str = "<unknown>"; 296af245d11STodd Fiala } 297af245d11STodd Fiala log->Printf("ptrace() failed; errno=%d (%s)", errno, str); 298af245d11STodd Fiala } 299af245d11STodd Fiala 300af245d11STodd Fiala return result; 301af245d11STodd Fiala } 302af245d11STodd Fiala 303af245d11STodd Fiala #ifdef LLDB_CONFIGURATION_BUILDANDINTEGRATION 304af245d11STodd Fiala // Wrapper for ptrace when logging is not required. 305af245d11STodd Fiala // Sets errno to 0 prior to calling ptrace. 306af245d11STodd Fiala long 307af245d11STodd Fiala PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size) 308af245d11STodd Fiala { 309af245d11STodd Fiala long result = 0; 310af245d11STodd Fiala errno = 0; 311af245d11STodd Fiala if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) 312af245d11STodd Fiala result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), *(unsigned int *)addr, data); 313af245d11STodd Fiala else 314af245d11STodd Fiala result = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), addr, data); 315af245d11STodd Fiala return result; 316af245d11STodd Fiala } 317af245d11STodd Fiala #endif 318af245d11STodd Fiala 319af245d11STodd Fiala //------------------------------------------------------------------------------ 320af245d11STodd Fiala // Static implementations of NativeProcessLinux::ReadMemory and 321af245d11STodd Fiala // NativeProcessLinux::WriteMemory. This enables mutual recursion between these 322af245d11STodd Fiala // functions without needed to go thru the thread funnel. 323af245d11STodd Fiala 324af245d11STodd Fiala static lldb::addr_t 325af245d11STodd Fiala DoReadMemory ( 326af245d11STodd Fiala lldb::pid_t pid, 327af245d11STodd Fiala lldb::addr_t vm_addr, 328af245d11STodd Fiala void *buf, 329af245d11STodd Fiala lldb::addr_t size, 330af245d11STodd Fiala Error &error) 331af245d11STodd Fiala { 332af245d11STodd Fiala // ptrace word size is determined by the host, not the child 333af245d11STodd Fiala static const unsigned word_size = sizeof(void*); 334af245d11STodd Fiala unsigned char *dst = static_cast<unsigned char*>(buf); 335af245d11STodd Fiala lldb::addr_t bytes_read; 336af245d11STodd Fiala lldb::addr_t remainder; 337af245d11STodd Fiala long data; 338af245d11STodd Fiala 339af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); 340af245d11STodd Fiala if (log) 341af245d11STodd Fiala ProcessPOSIXLog::IncNestLevel(); 342af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) 343af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(%" PRIu64 ", %d, %p, %p, %zd, _)", __FUNCTION__, 344af245d11STodd Fiala pid, word_size, (void*)vm_addr, buf, size); 345af245d11STodd Fiala 346af245d11STodd Fiala assert(sizeof(data) >= word_size); 347af245d11STodd Fiala for (bytes_read = 0; bytes_read < size; bytes_read += remainder) 348af245d11STodd Fiala { 349af245d11STodd Fiala errno = 0; 350af245d11STodd Fiala data = PTRACE(PTRACE_PEEKDATA, pid, (void*)vm_addr, NULL, 0); 351af245d11STodd Fiala if (errno) 352af245d11STodd Fiala { 353af245d11STodd Fiala error.SetErrorToErrno(); 354af245d11STodd Fiala if (log) 355af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 356af245d11STodd Fiala return bytes_read; 357af245d11STodd Fiala } 358af245d11STodd Fiala 359af245d11STodd Fiala remainder = size - bytes_read; 360af245d11STodd Fiala remainder = remainder > word_size ? word_size : remainder; 361af245d11STodd Fiala 362af245d11STodd Fiala // Copy the data into our buffer 363af245d11STodd Fiala for (unsigned i = 0; i < remainder; ++i) 364af245d11STodd Fiala dst[i] = ((data >> i*8) & 0xFF); 365af245d11STodd Fiala 366af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && 367af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 368af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 369af245d11STodd Fiala size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 370af245d11STodd Fiala { 371af245d11STodd Fiala uintptr_t print_dst = 0; 372af245d11STodd Fiala // Format bytes from data by moving into print_dst for log output 373af245d11STodd Fiala for (unsigned i = 0; i < remainder; ++i) 374af245d11STodd Fiala print_dst |= (((data >> i*8) & 0xFF) << i*8); 375af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 376af245d11STodd Fiala (void*)vm_addr, print_dst, (unsigned long)data); 377af245d11STodd Fiala } 378af245d11STodd Fiala 379af245d11STodd Fiala vm_addr += word_size; 380af245d11STodd Fiala dst += word_size; 381af245d11STodd Fiala } 382af245d11STodd Fiala 383af245d11STodd Fiala if (log) 384af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 385af245d11STodd Fiala return bytes_read; 386af245d11STodd Fiala } 387af245d11STodd Fiala 388af245d11STodd Fiala static lldb::addr_t 389af245d11STodd Fiala DoWriteMemory( 390af245d11STodd Fiala lldb::pid_t pid, 391af245d11STodd Fiala lldb::addr_t vm_addr, 392af245d11STodd Fiala const void *buf, 393af245d11STodd Fiala lldb::addr_t size, 394af245d11STodd Fiala Error &error) 395af245d11STodd Fiala { 396af245d11STodd Fiala // ptrace word size is determined by the host, not the child 397af245d11STodd Fiala static const unsigned word_size = sizeof(void*); 398af245d11STodd Fiala const unsigned char *src = static_cast<const unsigned char*>(buf); 399af245d11STodd Fiala lldb::addr_t bytes_written = 0; 400af245d11STodd Fiala lldb::addr_t remainder; 401af245d11STodd Fiala 402af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); 403af245d11STodd Fiala if (log) 404af245d11STodd Fiala ProcessPOSIXLog::IncNestLevel(); 405af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) 406af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(%" PRIu64 ", %u, %p, %p, %" PRIu64 ")", __FUNCTION__, 407af245d11STodd Fiala pid, word_size, (void*)vm_addr, buf, size); 408af245d11STodd Fiala 409af245d11STodd Fiala for (bytes_written = 0; bytes_written < size; bytes_written += remainder) 410af245d11STodd Fiala { 411af245d11STodd Fiala remainder = size - bytes_written; 412af245d11STodd Fiala remainder = remainder > word_size ? word_size : remainder; 413af245d11STodd Fiala 414af245d11STodd Fiala if (remainder == word_size) 415af245d11STodd Fiala { 416af245d11STodd Fiala unsigned long data = 0; 417af245d11STodd Fiala assert(sizeof(data) >= word_size); 418af245d11STodd Fiala for (unsigned i = 0; i < word_size; ++i) 419af245d11STodd Fiala data |= (unsigned long)src[i] << i*8; 420af245d11STodd Fiala 421af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && 422af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 423af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 424af245d11STodd Fiala size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 425af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 426af245d11STodd Fiala (void*)vm_addr, *(unsigned long*)src, data); 427af245d11STodd Fiala 428af245d11STodd Fiala if (PTRACE(PTRACE_POKEDATA, pid, (void*)vm_addr, (void*)data, 0)) 429af245d11STodd Fiala { 430af245d11STodd Fiala error.SetErrorToErrno(); 431af245d11STodd Fiala if (log) 432af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 433af245d11STodd Fiala return bytes_written; 434af245d11STodd Fiala } 435af245d11STodd Fiala } 436af245d11STodd Fiala else 437af245d11STodd Fiala { 438af245d11STodd Fiala unsigned char buff[8]; 439af245d11STodd Fiala if (DoReadMemory(pid, vm_addr, 440af245d11STodd Fiala buff, word_size, error) != word_size) 441af245d11STodd Fiala { 442af245d11STodd Fiala if (log) 443af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 444af245d11STodd Fiala return bytes_written; 445af245d11STodd Fiala } 446af245d11STodd Fiala 447af245d11STodd Fiala memcpy(buff, src, remainder); 448af245d11STodd Fiala 449af245d11STodd Fiala if (DoWriteMemory(pid, vm_addr, 450af245d11STodd Fiala buff, word_size, error) != word_size) 451af245d11STodd Fiala { 452af245d11STodd Fiala if (log) 453af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 454af245d11STodd Fiala return bytes_written; 455af245d11STodd Fiala } 456af245d11STodd Fiala 457af245d11STodd Fiala if (log && ProcessPOSIXLog::AtTopNestLevel() && 458af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 459af245d11STodd Fiala (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 460af245d11STodd Fiala size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 461af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 462af245d11STodd Fiala (void*)vm_addr, *(unsigned long*)src, *(unsigned long*)buff); 463af245d11STodd Fiala } 464af245d11STodd Fiala 465af245d11STodd Fiala vm_addr += word_size; 466af245d11STodd Fiala src += word_size; 467af245d11STodd Fiala } 468af245d11STodd Fiala if (log) 469af245d11STodd Fiala ProcessPOSIXLog::DecNestLevel(); 470af245d11STodd Fiala return bytes_written; 471af245d11STodd Fiala } 472af245d11STodd Fiala 473af245d11STodd Fiala //------------------------------------------------------------------------------ 474af245d11STodd Fiala /// @class Operation 475af245d11STodd Fiala /// @brief Represents a NativeProcessLinux operation. 476af245d11STodd Fiala /// 477af245d11STodd Fiala /// Under Linux, it is not possible to ptrace() from any other thread but the 478af245d11STodd Fiala /// one that spawned or attached to the process from the start. Therefore, when 479af245d11STodd Fiala /// a NativeProcessLinux is asked to deliver or change the state of an inferior 480af245d11STodd Fiala /// process the operation must be "funneled" to a specific thread to perform the 481af245d11STodd Fiala /// task. The Operation class provides an abstract base for all services the 482af245d11STodd Fiala /// NativeProcessLinux must perform via the single virtual function Execute, thus 483af245d11STodd Fiala /// encapsulating the code that needs to run in the privileged context. 484af245d11STodd Fiala class Operation 485af245d11STodd Fiala { 486af245d11STodd Fiala public: 487af245d11STodd Fiala Operation () : m_error() { } 488af245d11STodd Fiala 489af245d11STodd Fiala virtual 490af245d11STodd Fiala ~Operation() {} 491af245d11STodd Fiala 492af245d11STodd Fiala virtual void 493af245d11STodd Fiala Execute (NativeProcessLinux *process) = 0; 494af245d11STodd Fiala 495af245d11STodd Fiala const Error & 496af245d11STodd Fiala GetError () const { return m_error; } 497af245d11STodd Fiala 498af245d11STodd Fiala protected: 499af245d11STodd Fiala Error m_error; 500af245d11STodd Fiala }; 501af245d11STodd Fiala 502af245d11STodd Fiala //------------------------------------------------------------------------------ 503af245d11STodd Fiala /// @class ReadOperation 504af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadMemory. 505af245d11STodd Fiala class ReadOperation : public Operation 506af245d11STodd Fiala { 507af245d11STodd Fiala public: 508af245d11STodd Fiala ReadOperation ( 509af245d11STodd Fiala lldb::addr_t addr, 510af245d11STodd Fiala void *buff, 511af245d11STodd Fiala lldb::addr_t size, 512b35103ebSTodd Fiala lldb::addr_t &result) : 513af245d11STodd Fiala Operation (), 514af245d11STodd Fiala m_addr (addr), 515af245d11STodd Fiala m_buff (buff), 516af245d11STodd Fiala m_size (size), 517af245d11STodd Fiala m_result (result) 518af245d11STodd Fiala { 519af245d11STodd Fiala } 520af245d11STodd Fiala 521af245d11STodd Fiala void Execute (NativeProcessLinux *process) override; 522af245d11STodd Fiala 523af245d11STodd Fiala private: 524af245d11STodd Fiala lldb::addr_t m_addr; 525af245d11STodd Fiala void *m_buff; 526af245d11STodd Fiala lldb::addr_t m_size; 527af245d11STodd Fiala lldb::addr_t &m_result; 528af245d11STodd Fiala }; 529af245d11STodd Fiala 530af245d11STodd Fiala void 531af245d11STodd Fiala ReadOperation::Execute (NativeProcessLinux *process) 532af245d11STodd Fiala { 533af245d11STodd Fiala m_result = DoReadMemory (process->GetID (), m_addr, m_buff, m_size, m_error); 534af245d11STodd Fiala } 535af245d11STodd Fiala 536af245d11STodd Fiala //------------------------------------------------------------------------------ 537af245d11STodd Fiala /// @class WriteOperation 538af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteMemory. 539af245d11STodd Fiala class WriteOperation : public Operation 540af245d11STodd Fiala { 541af245d11STodd Fiala public: 542af245d11STodd Fiala WriteOperation ( 543af245d11STodd Fiala lldb::addr_t addr, 544af245d11STodd Fiala const void *buff, 545af245d11STodd Fiala lldb::addr_t size, 546af245d11STodd Fiala lldb::addr_t &result) : 547af245d11STodd Fiala Operation (), 548af245d11STodd Fiala m_addr (addr), 549af245d11STodd Fiala m_buff (buff), 550af245d11STodd Fiala m_size (size), 551af245d11STodd Fiala m_result (result) 552af245d11STodd Fiala { 553af245d11STodd Fiala } 554af245d11STodd Fiala 555af245d11STodd Fiala void Execute (NativeProcessLinux *process) override; 556af245d11STodd Fiala 557af245d11STodd Fiala private: 558af245d11STodd Fiala lldb::addr_t m_addr; 559af245d11STodd Fiala const void *m_buff; 560af245d11STodd Fiala lldb::addr_t m_size; 561af245d11STodd Fiala lldb::addr_t &m_result; 562af245d11STodd Fiala }; 563af245d11STodd Fiala 564af245d11STodd Fiala void 565af245d11STodd Fiala WriteOperation::Execute(NativeProcessLinux *process) 566af245d11STodd Fiala { 567af245d11STodd Fiala m_result = DoWriteMemory (process->GetID (), m_addr, m_buff, m_size, m_error); 568af245d11STodd Fiala } 569af245d11STodd Fiala 570af245d11STodd Fiala //------------------------------------------------------------------------------ 571af245d11STodd Fiala /// @class ReadRegOperation 572af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadRegisterValue. 573af245d11STodd Fiala class ReadRegOperation : public Operation 574af245d11STodd Fiala { 575af245d11STodd Fiala public: 576af245d11STodd Fiala ReadRegOperation(lldb::tid_t tid, uint32_t offset, const char *reg_name, 577af245d11STodd Fiala RegisterValue &value, bool &result) 578af245d11STodd Fiala : m_tid(tid), m_offset(static_cast<uintptr_t> (offset)), m_reg_name(reg_name), 579af245d11STodd Fiala m_value(value), m_result(result) 580af245d11STodd Fiala { } 581af245d11STodd Fiala 582af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 583af245d11STodd Fiala 584af245d11STodd Fiala private: 585af245d11STodd Fiala lldb::tid_t m_tid; 586af245d11STodd Fiala uintptr_t m_offset; 587af245d11STodd Fiala const char *m_reg_name; 588af245d11STodd Fiala RegisterValue &m_value; 589af245d11STodd Fiala bool &m_result; 590af245d11STodd Fiala }; 591af245d11STodd Fiala 592af245d11STodd Fiala void 593af245d11STodd Fiala ReadRegOperation::Execute(NativeProcessLinux *monitor) 594af245d11STodd Fiala { 595af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS)); 596af245d11STodd Fiala 597af245d11STodd Fiala // Set errno to zero so that we can detect a failed peek. 598af245d11STodd Fiala errno = 0; 599af245d11STodd Fiala lldb::addr_t data = PTRACE(PTRACE_PEEKUSER, m_tid, (void*)m_offset, NULL, 0); 600af245d11STodd Fiala if (errno) 601af245d11STodd Fiala m_result = false; 602af245d11STodd Fiala else 603af245d11STodd Fiala { 604af245d11STodd Fiala m_value = data; 605af245d11STodd Fiala m_result = true; 606af245d11STodd Fiala } 607af245d11STodd Fiala if (log) 608af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() reg %s: 0x%" PRIx64, __FUNCTION__, 609af245d11STodd Fiala m_reg_name, data); 610af245d11STodd Fiala } 611af245d11STodd Fiala 612af245d11STodd Fiala //------------------------------------------------------------------------------ 613af245d11STodd Fiala /// @class WriteRegOperation 614af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteRegisterValue. 615af245d11STodd Fiala class WriteRegOperation : public Operation 616af245d11STodd Fiala { 617af245d11STodd Fiala public: 618af245d11STodd Fiala WriteRegOperation(lldb::tid_t tid, unsigned offset, const char *reg_name, 619af245d11STodd Fiala const RegisterValue &value, bool &result) 620af245d11STodd Fiala : m_tid(tid), m_offset(offset), m_reg_name(reg_name), 621af245d11STodd Fiala m_value(value), m_result(result) 622af245d11STodd Fiala { } 623af245d11STodd Fiala 624af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 625af245d11STodd Fiala 626af245d11STodd Fiala private: 627af245d11STodd Fiala lldb::tid_t m_tid; 628af245d11STodd Fiala uintptr_t m_offset; 629af245d11STodd Fiala const char *m_reg_name; 630af245d11STodd Fiala const RegisterValue &m_value; 631af245d11STodd Fiala bool &m_result; 632af245d11STodd Fiala }; 633af245d11STodd Fiala 634af245d11STodd Fiala void 635af245d11STodd Fiala WriteRegOperation::Execute(NativeProcessLinux *monitor) 636af245d11STodd Fiala { 637af245d11STodd Fiala void* buf; 638af245d11STodd Fiala Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS)); 639af245d11STodd Fiala 640af245d11STodd Fiala buf = (void*) m_value.GetAsUInt64(); 641af245d11STodd Fiala 642af245d11STodd Fiala if (log) 643af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() reg %s: %p", __FUNCTION__, m_reg_name, buf); 644af245d11STodd Fiala if (PTRACE(PTRACE_POKEUSER, m_tid, (void*)m_offset, buf, 0)) 645af245d11STodd Fiala m_result = false; 646af245d11STodd Fiala else 647af245d11STodd Fiala m_result = true; 648af245d11STodd Fiala } 649af245d11STodd Fiala 650af245d11STodd Fiala //------------------------------------------------------------------------------ 651af245d11STodd Fiala /// @class ReadGPROperation 652af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadGPR. 653af245d11STodd Fiala class ReadGPROperation : public Operation 654af245d11STodd Fiala { 655af245d11STodd Fiala public: 656af245d11STodd Fiala ReadGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result) 657af245d11STodd Fiala : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result) 658af245d11STodd Fiala { } 659af245d11STodd Fiala 660af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 661af245d11STodd Fiala 662af245d11STodd Fiala private: 663af245d11STodd Fiala lldb::tid_t m_tid; 664af245d11STodd Fiala void *m_buf; 665af245d11STodd Fiala size_t m_buf_size; 666af245d11STodd Fiala bool &m_result; 667af245d11STodd Fiala }; 668af245d11STodd Fiala 669af245d11STodd Fiala void 670af245d11STodd Fiala ReadGPROperation::Execute(NativeProcessLinux *monitor) 671af245d11STodd Fiala { 6726ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 6736ac1be4bSTodd Fiala int regset = NT_PRSTATUS; 6746ac1be4bSTodd Fiala struct iovec ioVec; 6756ac1be4bSTodd Fiala 6766ac1be4bSTodd Fiala ioVec.iov_base = m_buf; 6776ac1be4bSTodd Fiala ioVec.iov_len = m_buf_size; 6786ac1be4bSTodd Fiala if (PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, m_buf_size) < 0) 6796ac1be4bSTodd Fiala m_result = false; 6806ac1be4bSTodd Fiala else 6816ac1be4bSTodd Fiala m_result = true; 6826ac1be4bSTodd Fiala #else 683af245d11STodd Fiala if (PTRACE(PTRACE_GETREGS, m_tid, NULL, m_buf, m_buf_size) < 0) 684af245d11STodd Fiala m_result = false; 685af245d11STodd Fiala else 686af245d11STodd Fiala m_result = true; 6876ac1be4bSTodd Fiala #endif 688af245d11STodd Fiala } 689af245d11STodd Fiala 690af245d11STodd Fiala //------------------------------------------------------------------------------ 691af245d11STodd Fiala /// @class ReadFPROperation 692af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadFPR. 693af245d11STodd Fiala class ReadFPROperation : public Operation 694af245d11STodd Fiala { 695af245d11STodd Fiala public: 696af245d11STodd Fiala ReadFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result) 697af245d11STodd Fiala : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result) 698af245d11STodd Fiala { } 699af245d11STodd Fiala 700af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 701af245d11STodd Fiala 702af245d11STodd Fiala private: 703af245d11STodd Fiala lldb::tid_t m_tid; 704af245d11STodd Fiala void *m_buf; 705af245d11STodd Fiala size_t m_buf_size; 706af245d11STodd Fiala bool &m_result; 707af245d11STodd Fiala }; 708af245d11STodd Fiala 709af245d11STodd Fiala void 710af245d11STodd Fiala ReadFPROperation::Execute(NativeProcessLinux *monitor) 711af245d11STodd Fiala { 7126ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 7136ac1be4bSTodd Fiala int regset = NT_FPREGSET; 7146ac1be4bSTodd Fiala struct iovec ioVec; 7156ac1be4bSTodd Fiala 7166ac1be4bSTodd Fiala ioVec.iov_base = m_buf; 7176ac1be4bSTodd Fiala ioVec.iov_len = m_buf_size; 7186ac1be4bSTodd Fiala if (PTRACE(PTRACE_GETREGSET, m_tid, ®set, &ioVec, m_buf_size) < 0) 7196ac1be4bSTodd Fiala m_result = false; 7206ac1be4bSTodd Fiala else 7216ac1be4bSTodd Fiala m_result = true; 7226ac1be4bSTodd Fiala #else 723af245d11STodd Fiala if (PTRACE(PTRACE_GETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0) 724af245d11STodd Fiala m_result = false; 725af245d11STodd Fiala else 726af245d11STodd Fiala m_result = true; 7276ac1be4bSTodd Fiala #endif 728af245d11STodd Fiala } 729af245d11STodd Fiala 730af245d11STodd Fiala //------------------------------------------------------------------------------ 731af245d11STodd Fiala /// @class ReadRegisterSetOperation 732af245d11STodd Fiala /// @brief Implements NativeProcessLinux::ReadRegisterSet. 733af245d11STodd Fiala class ReadRegisterSetOperation : public Operation 734af245d11STodd Fiala { 735af245d11STodd Fiala public: 736af245d11STodd Fiala ReadRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result) 737af245d11STodd Fiala : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result) 738af245d11STodd Fiala { } 739af245d11STodd Fiala 740af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 741af245d11STodd Fiala 742af245d11STodd Fiala private: 743af245d11STodd Fiala lldb::tid_t m_tid; 744af245d11STodd Fiala void *m_buf; 745af245d11STodd Fiala size_t m_buf_size; 746af245d11STodd Fiala const unsigned int m_regset; 747af245d11STodd Fiala bool &m_result; 748af245d11STodd Fiala }; 749af245d11STodd Fiala 750af245d11STodd Fiala void 751af245d11STodd Fiala ReadRegisterSetOperation::Execute(NativeProcessLinux *monitor) 752af245d11STodd Fiala { 753af245d11STodd Fiala if (PTRACE(PTRACE_GETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0) 754af245d11STodd Fiala m_result = false; 755af245d11STodd Fiala else 756af245d11STodd Fiala m_result = true; 757af245d11STodd Fiala } 758af245d11STodd Fiala 759af245d11STodd Fiala //------------------------------------------------------------------------------ 760af245d11STodd Fiala /// @class WriteGPROperation 761af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteGPR. 762af245d11STodd Fiala class WriteGPROperation : public Operation 763af245d11STodd Fiala { 764af245d11STodd Fiala public: 765af245d11STodd Fiala WriteGPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result) 766af245d11STodd Fiala : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result) 767af245d11STodd Fiala { } 768af245d11STodd Fiala 769af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 770af245d11STodd Fiala 771af245d11STodd Fiala private: 772af245d11STodd Fiala lldb::tid_t m_tid; 773af245d11STodd Fiala void *m_buf; 774af245d11STodd Fiala size_t m_buf_size; 775af245d11STodd Fiala bool &m_result; 776af245d11STodd Fiala }; 777af245d11STodd Fiala 778af245d11STodd Fiala void 779af245d11STodd Fiala WriteGPROperation::Execute(NativeProcessLinux *monitor) 780af245d11STodd Fiala { 7816ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 7826ac1be4bSTodd Fiala int regset = NT_PRSTATUS; 7836ac1be4bSTodd Fiala struct iovec ioVec; 7846ac1be4bSTodd Fiala 7856ac1be4bSTodd Fiala ioVec.iov_base = m_buf; 7866ac1be4bSTodd Fiala ioVec.iov_len = m_buf_size; 7876ac1be4bSTodd Fiala if (PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, m_buf_size) < 0) 7886ac1be4bSTodd Fiala m_result = false; 7896ac1be4bSTodd Fiala else 7906ac1be4bSTodd Fiala m_result = true; 7916ac1be4bSTodd Fiala #else 792af245d11STodd Fiala if (PTRACE(PTRACE_SETREGS, m_tid, NULL, m_buf, m_buf_size) < 0) 793af245d11STodd Fiala m_result = false; 794af245d11STodd Fiala else 795af245d11STodd Fiala m_result = true; 7966ac1be4bSTodd Fiala #endif 797af245d11STodd Fiala } 798af245d11STodd Fiala 799af245d11STodd Fiala //------------------------------------------------------------------------------ 800af245d11STodd Fiala /// @class WriteFPROperation 801af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteFPR. 802af245d11STodd Fiala class WriteFPROperation : public Operation 803af245d11STodd Fiala { 804af245d11STodd Fiala public: 805af245d11STodd Fiala WriteFPROperation(lldb::tid_t tid, void *buf, size_t buf_size, bool &result) 806af245d11STodd Fiala : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_result(result) 807af245d11STodd Fiala { } 808af245d11STodd Fiala 809af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 810af245d11STodd Fiala 811af245d11STodd Fiala private: 812af245d11STodd Fiala lldb::tid_t m_tid; 813af245d11STodd Fiala void *m_buf; 814af245d11STodd Fiala size_t m_buf_size; 815af245d11STodd Fiala bool &m_result; 816af245d11STodd Fiala }; 817af245d11STodd Fiala 818af245d11STodd Fiala void 819af245d11STodd Fiala WriteFPROperation::Execute(NativeProcessLinux *monitor) 820af245d11STodd Fiala { 8216ac1be4bSTodd Fiala #if defined (__arm64__) || defined (__aarch64__) 8226ac1be4bSTodd Fiala int regset = NT_FPREGSET; 8236ac1be4bSTodd Fiala struct iovec ioVec; 8246ac1be4bSTodd Fiala 8256ac1be4bSTodd Fiala ioVec.iov_base = m_buf; 8266ac1be4bSTodd Fiala ioVec.iov_len = m_buf_size; 8276ac1be4bSTodd Fiala if (PTRACE(PTRACE_SETREGSET, m_tid, ®set, &ioVec, m_buf_size) < 0) 8286ac1be4bSTodd Fiala m_result = false; 8296ac1be4bSTodd Fiala else 8306ac1be4bSTodd Fiala m_result = true; 8316ac1be4bSTodd Fiala #else 832af245d11STodd Fiala if (PTRACE(PTRACE_SETFPREGS, m_tid, NULL, m_buf, m_buf_size) < 0) 833af245d11STodd Fiala m_result = false; 834af245d11STodd Fiala else 835af245d11STodd Fiala m_result = true; 8366ac1be4bSTodd Fiala #endif 837af245d11STodd Fiala } 838af245d11STodd Fiala 839af245d11STodd Fiala //------------------------------------------------------------------------------ 840af245d11STodd Fiala /// @class WriteRegisterSetOperation 841af245d11STodd Fiala /// @brief Implements NativeProcessLinux::WriteRegisterSet. 842af245d11STodd Fiala class WriteRegisterSetOperation : public Operation 843af245d11STodd Fiala { 844af245d11STodd Fiala public: 845af245d11STodd Fiala WriteRegisterSetOperation(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset, bool &result) 846af245d11STodd Fiala : m_tid(tid), m_buf(buf), m_buf_size(buf_size), m_regset(regset), m_result(result) 847af245d11STodd Fiala { } 848af245d11STodd Fiala 849af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 850af245d11STodd Fiala 851af245d11STodd Fiala private: 852af245d11STodd Fiala lldb::tid_t m_tid; 853af245d11STodd Fiala void *m_buf; 854af245d11STodd Fiala size_t m_buf_size; 855af245d11STodd Fiala const unsigned int m_regset; 856af245d11STodd Fiala bool &m_result; 857af245d11STodd Fiala }; 858af245d11STodd Fiala 859af245d11STodd Fiala void 860af245d11STodd Fiala WriteRegisterSetOperation::Execute(NativeProcessLinux *monitor) 861af245d11STodd Fiala { 862af245d11STodd Fiala if (PTRACE(PTRACE_SETREGSET, m_tid, (void *)&m_regset, m_buf, m_buf_size) < 0) 863af245d11STodd Fiala m_result = false; 864af245d11STodd Fiala else 865af245d11STodd Fiala m_result = true; 866af245d11STodd Fiala } 867af245d11STodd Fiala 868af245d11STodd Fiala //------------------------------------------------------------------------------ 869af245d11STodd Fiala /// @class ResumeOperation 870af245d11STodd Fiala /// @brief Implements NativeProcessLinux::Resume. 871af245d11STodd Fiala class ResumeOperation : public Operation 872af245d11STodd Fiala { 873af245d11STodd Fiala public: 874af245d11STodd Fiala ResumeOperation(lldb::tid_t tid, uint32_t signo, bool &result) : 875af245d11STodd Fiala m_tid(tid), m_signo(signo), m_result(result) { } 876af245d11STodd Fiala 877af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 878af245d11STodd Fiala 879af245d11STodd Fiala private: 880af245d11STodd Fiala lldb::tid_t m_tid; 881af245d11STodd Fiala uint32_t m_signo; 882af245d11STodd Fiala bool &m_result; 883af245d11STodd Fiala }; 884af245d11STodd Fiala 885af245d11STodd Fiala void 886af245d11STodd Fiala ResumeOperation::Execute(NativeProcessLinux *monitor) 887af245d11STodd Fiala { 888af245d11STodd Fiala intptr_t data = 0; 889af245d11STodd Fiala 890af245d11STodd Fiala if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) 891af245d11STodd Fiala data = m_signo; 892af245d11STodd Fiala 893af245d11STodd Fiala if (PTRACE(PTRACE_CONT, m_tid, NULL, (void*)data, 0)) 894af245d11STodd Fiala { 895af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 896af245d11STodd Fiala 897af245d11STodd Fiala if (log) 898af245d11STodd Fiala log->Printf ("ResumeOperation (%" PRIu64 ") failed: %s", m_tid, strerror(errno)); 899af245d11STodd Fiala m_result = false; 900af245d11STodd Fiala } 901af245d11STodd Fiala else 902af245d11STodd Fiala m_result = true; 903af245d11STodd Fiala } 904af245d11STodd Fiala 905af245d11STodd Fiala //------------------------------------------------------------------------------ 906af245d11STodd Fiala /// @class SingleStepOperation 907af245d11STodd Fiala /// @brief Implements NativeProcessLinux::SingleStep. 908af245d11STodd Fiala class SingleStepOperation : public Operation 909af245d11STodd Fiala { 910af245d11STodd Fiala public: 911af245d11STodd Fiala SingleStepOperation(lldb::tid_t tid, uint32_t signo, bool &result) 912af245d11STodd Fiala : m_tid(tid), m_signo(signo), m_result(result) { } 913af245d11STodd Fiala 914af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 915af245d11STodd Fiala 916af245d11STodd Fiala private: 917af245d11STodd Fiala lldb::tid_t m_tid; 918af245d11STodd Fiala uint32_t m_signo; 919af245d11STodd Fiala bool &m_result; 920af245d11STodd Fiala }; 921af245d11STodd Fiala 922af245d11STodd Fiala void 923af245d11STodd Fiala SingleStepOperation::Execute(NativeProcessLinux *monitor) 924af245d11STodd Fiala { 925af245d11STodd Fiala intptr_t data = 0; 926af245d11STodd Fiala 927af245d11STodd Fiala if (m_signo != LLDB_INVALID_SIGNAL_NUMBER) 928af245d11STodd Fiala data = m_signo; 929af245d11STodd Fiala 930af245d11STodd Fiala if (PTRACE(PTRACE_SINGLESTEP, m_tid, NULL, (void*)data, 0)) 931af245d11STodd Fiala m_result = false; 932af245d11STodd Fiala else 933af245d11STodd Fiala m_result = true; 934af245d11STodd Fiala } 935af245d11STodd Fiala 936af245d11STodd Fiala //------------------------------------------------------------------------------ 937af245d11STodd Fiala /// @class SiginfoOperation 938af245d11STodd Fiala /// @brief Implements NativeProcessLinux::GetSignalInfo. 939af245d11STodd Fiala class SiginfoOperation : public Operation 940af245d11STodd Fiala { 941af245d11STodd Fiala public: 942af245d11STodd Fiala SiginfoOperation(lldb::tid_t tid, void *info, bool &result, int &ptrace_err) 943af245d11STodd Fiala : m_tid(tid), m_info(info), m_result(result), m_err(ptrace_err) { } 944af245d11STodd Fiala 945af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 946af245d11STodd Fiala 947af245d11STodd Fiala private: 948af245d11STodd Fiala lldb::tid_t m_tid; 949af245d11STodd Fiala void *m_info; 950af245d11STodd Fiala bool &m_result; 951af245d11STodd Fiala int &m_err; 952af245d11STodd Fiala }; 953af245d11STodd Fiala 954af245d11STodd Fiala void 955af245d11STodd Fiala SiginfoOperation::Execute(NativeProcessLinux *monitor) 956af245d11STodd Fiala { 957af245d11STodd Fiala if (PTRACE(PTRACE_GETSIGINFO, m_tid, NULL, m_info, 0)) { 958af245d11STodd Fiala m_result = false; 959af245d11STodd Fiala m_err = errno; 960af245d11STodd Fiala } 961af245d11STodd Fiala else 962af245d11STodd Fiala m_result = true; 963af245d11STodd Fiala } 964af245d11STodd Fiala 965af245d11STodd Fiala //------------------------------------------------------------------------------ 966af245d11STodd Fiala /// @class EventMessageOperation 967af245d11STodd Fiala /// @brief Implements NativeProcessLinux::GetEventMessage. 968af245d11STodd Fiala class EventMessageOperation : public Operation 969af245d11STodd Fiala { 970af245d11STodd Fiala public: 971af245d11STodd Fiala EventMessageOperation(lldb::tid_t tid, unsigned long *message, bool &result) 972af245d11STodd Fiala : m_tid(tid), m_message(message), m_result(result) { } 973af245d11STodd Fiala 974af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 975af245d11STodd Fiala 976af245d11STodd Fiala private: 977af245d11STodd Fiala lldb::tid_t m_tid; 978af245d11STodd Fiala unsigned long *m_message; 979af245d11STodd Fiala bool &m_result; 980af245d11STodd Fiala }; 981af245d11STodd Fiala 982af245d11STodd Fiala void 983af245d11STodd Fiala EventMessageOperation::Execute(NativeProcessLinux *monitor) 984af245d11STodd Fiala { 985af245d11STodd Fiala if (PTRACE(PTRACE_GETEVENTMSG, m_tid, NULL, m_message, 0)) 986af245d11STodd Fiala m_result = false; 987af245d11STodd Fiala else 988af245d11STodd Fiala m_result = true; 989af245d11STodd Fiala } 990af245d11STodd Fiala 991af245d11STodd Fiala class DetachOperation : public Operation 992af245d11STodd Fiala { 993af245d11STodd Fiala public: 994af245d11STodd Fiala DetachOperation(lldb::tid_t tid, Error &result) : m_tid(tid), m_error(result) { } 995af245d11STodd Fiala 996af245d11STodd Fiala void Execute(NativeProcessLinux *monitor); 997af245d11STodd Fiala 998af245d11STodd Fiala private: 999af245d11STodd Fiala lldb::tid_t m_tid; 1000af245d11STodd Fiala Error &m_error; 1001af245d11STodd Fiala }; 1002af245d11STodd Fiala 1003af245d11STodd Fiala void 1004af245d11STodd Fiala DetachOperation::Execute(NativeProcessLinux *monitor) 1005af245d11STodd Fiala { 1006af245d11STodd Fiala if (ptrace(PT_DETACH, m_tid, NULL, 0) < 0) 1007af245d11STodd Fiala m_error.SetErrorToErrno(); 1008af245d11STodd Fiala } 1009af245d11STodd Fiala 1010af245d11STodd Fiala } 1011af245d11STodd Fiala 1012af245d11STodd Fiala using namespace lldb_private; 1013af245d11STodd Fiala 1014af245d11STodd Fiala // Simple helper function to ensure flags are enabled on the given file 1015af245d11STodd Fiala // descriptor. 1016af245d11STodd Fiala static bool 1017af245d11STodd Fiala EnsureFDFlags(int fd, int flags, Error &error) 1018af245d11STodd Fiala { 1019af245d11STodd Fiala int status; 1020af245d11STodd Fiala 1021af245d11STodd Fiala if ((status = fcntl(fd, F_GETFL)) == -1) 1022af245d11STodd Fiala { 1023af245d11STodd Fiala error.SetErrorToErrno(); 1024af245d11STodd Fiala return false; 1025af245d11STodd Fiala } 1026af245d11STodd Fiala 1027af245d11STodd Fiala if (fcntl(fd, F_SETFL, status | flags) == -1) 1028af245d11STodd Fiala { 1029af245d11STodd Fiala error.SetErrorToErrno(); 1030af245d11STodd Fiala return false; 1031af245d11STodd Fiala } 1032af245d11STodd Fiala 1033af245d11STodd Fiala return true; 1034af245d11STodd Fiala } 1035af245d11STodd Fiala 1036af245d11STodd Fiala NativeProcessLinux::OperationArgs::OperationArgs(NativeProcessLinux *monitor) 1037af245d11STodd Fiala : m_monitor(monitor) 1038af245d11STodd Fiala { 1039af245d11STodd Fiala sem_init(&m_semaphore, 0, 0); 1040af245d11STodd Fiala } 1041af245d11STodd Fiala 1042af245d11STodd Fiala NativeProcessLinux::OperationArgs::~OperationArgs() 1043af245d11STodd Fiala { 1044af245d11STodd Fiala sem_destroy(&m_semaphore); 1045af245d11STodd Fiala } 1046af245d11STodd Fiala 1047af245d11STodd Fiala NativeProcessLinux::LaunchArgs::LaunchArgs(NativeProcessLinux *monitor, 1048af245d11STodd Fiala lldb_private::Module *module, 1049af245d11STodd Fiala char const **argv, 1050af245d11STodd Fiala char const **envp, 1051af245d11STodd Fiala const char *stdin_path, 1052af245d11STodd Fiala const char *stdout_path, 1053af245d11STodd Fiala const char *stderr_path, 10540bce1b67STodd Fiala const char *working_dir, 10550bce1b67STodd Fiala const lldb_private::ProcessLaunchInfo &launch_info) 1056af245d11STodd Fiala : OperationArgs(monitor), 1057af245d11STodd Fiala m_module(module), 1058af245d11STodd Fiala m_argv(argv), 1059af245d11STodd Fiala m_envp(envp), 1060af245d11STodd Fiala m_stdin_path(stdin_path), 1061af245d11STodd Fiala m_stdout_path(stdout_path), 1062af245d11STodd Fiala m_stderr_path(stderr_path), 10630bce1b67STodd Fiala m_working_dir(working_dir), 10640bce1b67STodd Fiala m_launch_info(launch_info) 10650bce1b67STodd Fiala { 10660bce1b67STodd Fiala } 1067af245d11STodd Fiala 1068af245d11STodd Fiala NativeProcessLinux::LaunchArgs::~LaunchArgs() 1069af245d11STodd Fiala { } 1070af245d11STodd Fiala 1071af245d11STodd Fiala NativeProcessLinux::AttachArgs::AttachArgs(NativeProcessLinux *monitor, 1072af245d11STodd Fiala lldb::pid_t pid) 1073af245d11STodd Fiala : OperationArgs(monitor), m_pid(pid) { } 1074af245d11STodd Fiala 1075af245d11STodd Fiala NativeProcessLinux::AttachArgs::~AttachArgs() 1076af245d11STodd Fiala { } 1077af245d11STodd Fiala 1078af245d11STodd Fiala // ----------------------------------------------------------------------------- 1079af245d11STodd Fiala // Public Static Methods 1080af245d11STodd Fiala // ----------------------------------------------------------------------------- 1081af245d11STodd Fiala 1082af245d11STodd Fiala lldb_private::Error 1083af245d11STodd Fiala NativeProcessLinux::LaunchProcess ( 1084af245d11STodd Fiala lldb_private::Module *exe_module, 1085af245d11STodd Fiala lldb_private::ProcessLaunchInfo &launch_info, 1086af245d11STodd Fiala lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate, 1087af245d11STodd Fiala NativeProcessProtocolSP &native_process_sp) 1088af245d11STodd Fiala { 1089af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1090af245d11STodd Fiala 1091af245d11STodd Fiala Error error; 1092af245d11STodd Fiala 1093af245d11STodd Fiala // Verify the working directory is valid if one was specified. 1094af245d11STodd Fiala const char* working_dir = launch_info.GetWorkingDirectory (); 1095af245d11STodd Fiala if (working_dir) 1096af245d11STodd Fiala { 1097af245d11STodd Fiala FileSpec working_dir_fs (working_dir, true); 1098af245d11STodd Fiala if (!working_dir_fs || working_dir_fs.GetFileType () != FileSpec::eFileTypeDirectory) 1099af245d11STodd Fiala { 1100af245d11STodd Fiala error.SetErrorStringWithFormat ("No such file or directory: %s", working_dir); 1101af245d11STodd Fiala return error; 1102af245d11STodd Fiala } 1103af245d11STodd Fiala } 1104af245d11STodd Fiala 1105696b5287SZachary Turner const lldb_private::FileAction *file_action; 1106af245d11STodd Fiala 1107af245d11STodd Fiala // Default of NULL will mean to use existing open file descriptors. 1108af245d11STodd Fiala const char *stdin_path = NULL; 1109af245d11STodd Fiala const char *stdout_path = NULL; 1110af245d11STodd Fiala const char *stderr_path = NULL; 1111af245d11STodd Fiala 1112af245d11STodd Fiala file_action = launch_info.GetFileActionForFD (STDIN_FILENO); 1113af245d11STodd Fiala stdin_path = GetFilePath (file_action, stdin_path); 1114af245d11STodd Fiala 1115af245d11STodd Fiala file_action = launch_info.GetFileActionForFD (STDOUT_FILENO); 1116af245d11STodd Fiala stdout_path = GetFilePath (file_action, stdout_path); 1117af245d11STodd Fiala 1118af245d11STodd Fiala file_action = launch_info.GetFileActionForFD (STDERR_FILENO); 1119af245d11STodd Fiala stderr_path = GetFilePath (file_action, stderr_path); 1120af245d11STodd Fiala 1121af245d11STodd Fiala // Create the NativeProcessLinux in launch mode. 1122af245d11STodd Fiala native_process_sp.reset (new NativeProcessLinux ()); 1123af245d11STodd Fiala 1124af245d11STodd Fiala if (log) 1125af245d11STodd Fiala { 1126af245d11STodd Fiala int i = 0; 1127af245d11STodd Fiala for (const char **args = launch_info.GetArguments ().GetConstArgumentVector (); *args; ++args, ++i) 1128af245d11STodd Fiala { 1129af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s arg %d: \"%s\"", __FUNCTION__, i, *args ? *args : "nullptr"); 1130af245d11STodd Fiala ++i; 1131af245d11STodd Fiala } 1132af245d11STodd Fiala } 1133af245d11STodd Fiala 1134af245d11STodd Fiala if (!native_process_sp->RegisterNativeDelegate (native_delegate)) 1135af245d11STodd Fiala { 1136af245d11STodd Fiala native_process_sp.reset (); 1137af245d11STodd Fiala error.SetErrorStringWithFormat ("failed to register the native delegate"); 1138af245d11STodd Fiala return error; 1139af245d11STodd Fiala } 1140af245d11STodd Fiala 1141af245d11STodd Fiala reinterpret_cast<NativeProcessLinux*> (native_process_sp.get ())->LaunchInferior ( 1142af245d11STodd Fiala exe_module, 1143af245d11STodd Fiala launch_info.GetArguments ().GetConstArgumentVector (), 1144af245d11STodd Fiala launch_info.GetEnvironmentEntries ().GetConstArgumentVector (), 1145af245d11STodd Fiala stdin_path, 1146af245d11STodd Fiala stdout_path, 1147af245d11STodd Fiala stderr_path, 1148af245d11STodd Fiala working_dir, 11490bce1b67STodd Fiala launch_info, 1150af245d11STodd Fiala error); 1151af245d11STodd Fiala 1152af245d11STodd Fiala if (error.Fail ()) 1153af245d11STodd Fiala { 1154af245d11STodd Fiala native_process_sp.reset (); 1155af245d11STodd Fiala if (log) 1156af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to launch process: %s", __FUNCTION__, error.AsCString ()); 1157af245d11STodd Fiala return error; 1158af245d11STodd Fiala } 1159af245d11STodd Fiala 1160af245d11STodd Fiala launch_info.SetProcessID (native_process_sp->GetID ()); 1161af245d11STodd Fiala 1162af245d11STodd Fiala return error; 1163af245d11STodd Fiala } 1164af245d11STodd Fiala 1165af245d11STodd Fiala lldb_private::Error 1166af245d11STodd Fiala NativeProcessLinux::AttachToProcess ( 1167af245d11STodd Fiala lldb::pid_t pid, 1168af245d11STodd Fiala lldb_private::NativeProcessProtocol::NativeDelegate &native_delegate, 1169af245d11STodd Fiala NativeProcessProtocolSP &native_process_sp) 1170af245d11STodd Fiala { 1171af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1172af245d11STodd Fiala if (log && log->GetMask ().Test (POSIX_LOG_VERBOSE)) 1173af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(pid = %" PRIi64 ")", __FUNCTION__, pid); 1174af245d11STodd Fiala 1175af245d11STodd Fiala // Grab the current platform architecture. This should be Linux, 1176af245d11STodd Fiala // since this code is only intended to run on a Linux host. 1177af245d11STodd Fiala PlatformSP platform_sp (Platform::GetDefaultPlatform ()); 1178af245d11STodd Fiala if (!platform_sp) 1179af245d11STodd Fiala return Error("failed to get a valid default platform"); 1180af245d11STodd Fiala 1181af245d11STodd Fiala // Retrieve the architecture for the running process. 1182af245d11STodd Fiala ArchSpec process_arch; 1183af245d11STodd Fiala Error error = ResolveProcessArchitecture (pid, *platform_sp.get (), process_arch); 1184af245d11STodd Fiala if (!error.Success ()) 1185af245d11STodd Fiala return error; 1186af245d11STodd Fiala 1187af245d11STodd Fiala native_process_sp.reset (new NativeProcessLinux ()); 1188af245d11STodd Fiala 1189af245d11STodd Fiala if (!native_process_sp->RegisterNativeDelegate (native_delegate)) 1190af245d11STodd Fiala { 1191af245d11STodd Fiala native_process_sp.reset (new NativeProcessLinux ()); 1192af245d11STodd Fiala error.SetErrorStringWithFormat ("failed to register the native delegate"); 1193af245d11STodd Fiala return error; 1194af245d11STodd Fiala } 1195af245d11STodd Fiala 1196af245d11STodd Fiala reinterpret_cast<NativeProcessLinux*> (native_process_sp.get ())->AttachToInferior (pid, error); 1197af245d11STodd Fiala if (!error.Success ()) 1198af245d11STodd Fiala { 1199af245d11STodd Fiala native_process_sp.reset (); 1200af245d11STodd Fiala return error; 1201af245d11STodd Fiala } 1202af245d11STodd Fiala 1203af245d11STodd Fiala return error; 1204af245d11STodd Fiala } 1205af245d11STodd Fiala 1206af245d11STodd Fiala // ----------------------------------------------------------------------------- 1207af245d11STodd Fiala // Public Instance Methods 1208af245d11STodd Fiala // ----------------------------------------------------------------------------- 1209af245d11STodd Fiala 1210af245d11STodd Fiala NativeProcessLinux::NativeProcessLinux () : 1211af245d11STodd Fiala NativeProcessProtocol (LLDB_INVALID_PROCESS_ID), 1212af245d11STodd Fiala m_arch (), 1213af245d11STodd Fiala m_operation (nullptr), 1214af245d11STodd Fiala m_operation_mutex (), 1215af245d11STodd Fiala m_operation_pending (), 1216af245d11STodd Fiala m_operation_done (), 1217af245d11STodd Fiala m_wait_for_stop_tids (), 1218af245d11STodd Fiala m_wait_for_stop_tids_mutex (), 1219*511e5cdcSTodd Fiala m_wait_for_group_stop_tids (), 1220*511e5cdcSTodd Fiala m_group_stop_signal_tid (LLDB_INVALID_THREAD_ID), 1221*511e5cdcSTodd Fiala m_group_stop_signal (LLDB_INVALID_SIGNAL_NUMBER), 1222*511e5cdcSTodd Fiala m_wait_for_group_stop_tids_mutex (), 1223af245d11STodd Fiala m_supports_mem_region (eLazyBoolCalculate), 1224af245d11STodd Fiala m_mem_region_cache (), 1225af245d11STodd Fiala m_mem_region_cache_mutex () 1226af245d11STodd Fiala { 1227af245d11STodd Fiala } 1228af245d11STodd Fiala 1229af245d11STodd Fiala //------------------------------------------------------------------------------ 1230af245d11STodd Fiala /// The basic design of the NativeProcessLinux is built around two threads. 1231af245d11STodd Fiala /// 1232af245d11STodd Fiala /// One thread (@see SignalThread) simply blocks on a call to waitpid() looking 1233af245d11STodd Fiala /// for changes in the debugee state. When a change is detected a 1234af245d11STodd Fiala /// ProcessMessage is sent to the associated ProcessLinux instance. This thread 1235af245d11STodd Fiala /// "drives" state changes in the debugger. 1236af245d11STodd Fiala /// 1237af245d11STodd Fiala /// The second thread (@see OperationThread) is responsible for two things 1) 1238af245d11STodd Fiala /// launching or attaching to the inferior process, and then 2) servicing 1239af245d11STodd Fiala /// operations such as register reads/writes, stepping, etc. See the comments 1240af245d11STodd Fiala /// on the Operation class for more info as to why this is needed. 1241af245d11STodd Fiala void 1242af245d11STodd Fiala NativeProcessLinux::LaunchInferior ( 1243af245d11STodd Fiala Module *module, 1244af245d11STodd Fiala const char *argv[], 1245af245d11STodd Fiala const char *envp[], 1246af245d11STodd Fiala const char *stdin_path, 1247af245d11STodd Fiala const char *stdout_path, 1248af245d11STodd Fiala const char *stderr_path, 1249af245d11STodd Fiala const char *working_dir, 12500bce1b67STodd Fiala const lldb_private::ProcessLaunchInfo &launch_info, 1251af245d11STodd Fiala lldb_private::Error &error) 1252af245d11STodd Fiala { 1253af245d11STodd Fiala if (module) 1254af245d11STodd Fiala m_arch = module->GetArchitecture (); 1255af245d11STodd Fiala 1256af245d11STodd Fiala SetState(eStateLaunching); 1257af245d11STodd Fiala 1258af245d11STodd Fiala std::unique_ptr<LaunchArgs> args( 1259af245d11STodd Fiala new LaunchArgs( 1260af245d11STodd Fiala this, module, argv, envp, 1261af245d11STodd Fiala stdin_path, stdout_path, stderr_path, 12620bce1b67STodd Fiala working_dir, launch_info)); 1263af245d11STodd Fiala 1264af245d11STodd Fiala sem_init(&m_operation_pending, 0, 0); 1265af245d11STodd Fiala sem_init(&m_operation_done, 0, 0); 1266af245d11STodd Fiala 1267af245d11STodd Fiala StartLaunchOpThread(args.get(), error); 1268af245d11STodd Fiala if (!error.Success()) 1269af245d11STodd Fiala return; 1270af245d11STodd Fiala 1271af245d11STodd Fiala WAIT_AGAIN: 1272af245d11STodd Fiala // Wait for the operation thread to initialize. 1273af245d11STodd Fiala if (sem_wait(&args->m_semaphore)) 1274af245d11STodd Fiala { 1275af245d11STodd Fiala if (errno == EINTR) 1276af245d11STodd Fiala goto WAIT_AGAIN; 1277af245d11STodd Fiala else 1278af245d11STodd Fiala { 1279af245d11STodd Fiala error.SetErrorToErrno(); 1280af245d11STodd Fiala return; 1281af245d11STodd Fiala } 1282af245d11STodd Fiala } 1283af245d11STodd Fiala 1284af245d11STodd Fiala // Check that the launch was a success. 1285af245d11STodd Fiala if (!args->m_error.Success()) 1286af245d11STodd Fiala { 1287af245d11STodd Fiala StopOpThread(); 1288af245d11STodd Fiala error = args->m_error; 1289af245d11STodd Fiala return; 1290af245d11STodd Fiala } 1291af245d11STodd Fiala 1292af245d11STodd Fiala // Finally, start monitoring the child process for change in state. 1293af245d11STodd Fiala m_monitor_thread = Host::StartMonitoringChildProcess( 1294af245d11STodd Fiala NativeProcessLinux::MonitorCallback, this, GetID(), true); 129539de3110SZachary Turner if (m_monitor_thread.GetState() != eThreadStateRunning) 1296af245d11STodd Fiala { 1297af245d11STodd Fiala error.SetErrorToGenericError(); 1298af245d11STodd Fiala error.SetErrorString ("Process attach failed to create monitor thread for NativeProcessLinux::MonitorCallback."); 1299af245d11STodd Fiala return; 1300af245d11STodd Fiala } 1301af245d11STodd Fiala } 1302af245d11STodd Fiala 1303af245d11STodd Fiala void 1304af245d11STodd Fiala NativeProcessLinux::AttachToInferior (lldb::pid_t pid, lldb_private::Error &error) 1305af245d11STodd Fiala { 1306af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1307af245d11STodd Fiala if (log) 1308af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ")", __FUNCTION__, pid); 1309af245d11STodd Fiala 1310af245d11STodd Fiala // We can use the Host for everything except the ResolveExecutable portion. 1311af245d11STodd Fiala PlatformSP platform_sp = Platform::GetDefaultPlatform (); 1312af245d11STodd Fiala if (!platform_sp) 1313af245d11STodd Fiala { 1314af245d11STodd Fiala if (log) 1315af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 "): no default platform set", __FUNCTION__, pid); 1316af245d11STodd Fiala error.SetErrorString ("no default platform available"); 1317af245d11STodd Fiala } 1318af245d11STodd Fiala 1319af245d11STodd Fiala // Gather info about the process. 1320af245d11STodd Fiala ProcessInstanceInfo process_info; 1321af245d11STodd Fiala platform_sp->GetProcessInfo (pid, process_info); 1322af245d11STodd Fiala 1323af245d11STodd Fiala // Resolve the executable module 1324af245d11STodd Fiala ModuleSP exe_module_sp; 1325af245d11STodd Fiala FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths()); 1326af245d11STodd Fiala 132713b18261SZachary Turner error = platform_sp->ResolveExecutable(process_info.GetExecutableFile(), HostInfo::GetArchitecture(), exe_module_sp, 1328af245d11STodd Fiala executable_search_paths.GetSize() ? &executable_search_paths : NULL); 1329af245d11STodd Fiala if (!error.Success()) 1330af245d11STodd Fiala return; 1331af245d11STodd Fiala 1332af245d11STodd Fiala // Set the architecture to the exe architecture. 1333af245d11STodd Fiala m_arch = exe_module_sp->GetArchitecture(); 1334af245d11STodd Fiala if (log) 1335af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ") detected architecture %s", __FUNCTION__, pid, m_arch.GetArchitectureName ()); 1336af245d11STodd Fiala 1337af245d11STodd Fiala m_pid = pid; 1338af245d11STodd Fiala SetState(eStateAttaching); 1339af245d11STodd Fiala 1340af245d11STodd Fiala sem_init (&m_operation_pending, 0, 0); 1341af245d11STodd Fiala sem_init (&m_operation_done, 0, 0); 1342af245d11STodd Fiala 1343af245d11STodd Fiala std::unique_ptr<AttachArgs> args (new AttachArgs (this, pid)); 1344af245d11STodd Fiala 1345af245d11STodd Fiala StartAttachOpThread(args.get (), error); 1346af245d11STodd Fiala if (!error.Success ()) 1347af245d11STodd Fiala return; 1348af245d11STodd Fiala 1349af245d11STodd Fiala WAIT_AGAIN: 1350af245d11STodd Fiala // Wait for the operation thread to initialize. 1351af245d11STodd Fiala if (sem_wait (&args->m_semaphore)) 1352af245d11STodd Fiala { 1353af245d11STodd Fiala if (errno == EINTR) 1354af245d11STodd Fiala goto WAIT_AGAIN; 1355af245d11STodd Fiala else 1356af245d11STodd Fiala { 1357af245d11STodd Fiala error.SetErrorToErrno (); 1358af245d11STodd Fiala return; 1359af245d11STodd Fiala } 1360af245d11STodd Fiala } 1361af245d11STodd Fiala 1362af245d11STodd Fiala // Check that the attach was a success. 1363af245d11STodd Fiala if (!args->m_error.Success ()) 1364af245d11STodd Fiala { 1365af245d11STodd Fiala StopOpThread (); 1366af245d11STodd Fiala error = args->m_error; 1367af245d11STodd Fiala return; 1368af245d11STodd Fiala } 1369af245d11STodd Fiala 1370af245d11STodd Fiala // Finally, start monitoring the child process for change in state. 1371af245d11STodd Fiala m_monitor_thread = Host::StartMonitoringChildProcess ( 1372af245d11STodd Fiala NativeProcessLinux::MonitorCallback, this, GetID (), true); 137339de3110SZachary Turner if (m_monitor_thread.GetState() != eThreadStateRunning) 1374af245d11STodd Fiala { 1375af245d11STodd Fiala error.SetErrorToGenericError (); 1376af245d11STodd Fiala error.SetErrorString ("Process attach failed to create monitor thread for NativeProcessLinux::MonitorCallback."); 1377af245d11STodd Fiala return; 1378af245d11STodd Fiala } 1379af245d11STodd Fiala } 1380af245d11STodd Fiala 1381af245d11STodd Fiala NativeProcessLinux::~NativeProcessLinux() 1382af245d11STodd Fiala { 1383af245d11STodd Fiala StopMonitor(); 1384af245d11STodd Fiala } 1385af245d11STodd Fiala 1386af245d11STodd Fiala //------------------------------------------------------------------------------ 1387af245d11STodd Fiala // Thread setup and tear down. 1388af245d11STodd Fiala 1389af245d11STodd Fiala void 1390af245d11STodd Fiala NativeProcessLinux::StartLaunchOpThread(LaunchArgs *args, Error &error) 1391af245d11STodd Fiala { 1392af245d11STodd Fiala static const char *g_thread_name = "lldb.process.nativelinux.operation"; 1393af245d11STodd Fiala 139439de3110SZachary Turner if (m_operation_thread.GetState() == eThreadStateRunning) 1395af245d11STodd Fiala return; 1396af245d11STodd Fiala 139739de3110SZachary Turner m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, LaunchOpThread, args, &error); 1398af245d11STodd Fiala } 1399af245d11STodd Fiala 1400af245d11STodd Fiala void * 1401af245d11STodd Fiala NativeProcessLinux::LaunchOpThread(void *arg) 1402af245d11STodd Fiala { 1403af245d11STodd Fiala LaunchArgs *args = static_cast<LaunchArgs*>(arg); 1404af245d11STodd Fiala 1405af245d11STodd Fiala if (!Launch(args)) { 1406af245d11STodd Fiala sem_post(&args->m_semaphore); 1407af245d11STodd Fiala return NULL; 1408af245d11STodd Fiala } 1409af245d11STodd Fiala 1410af245d11STodd Fiala ServeOperation(args); 1411af245d11STodd Fiala return NULL; 1412af245d11STodd Fiala } 1413af245d11STodd Fiala 1414af245d11STodd Fiala bool 1415af245d11STodd Fiala NativeProcessLinux::Launch(LaunchArgs *args) 1416af245d11STodd Fiala { 14170bce1b67STodd Fiala assert (args && "null args"); 14180bce1b67STodd Fiala if (!args) 14190bce1b67STodd Fiala return false; 14200bce1b67STodd Fiala 1421af245d11STodd Fiala NativeProcessLinux *monitor = args->m_monitor; 1422af245d11STodd Fiala assert (monitor && "monitor is NULL"); 1423af245d11STodd Fiala if (!monitor) 1424af245d11STodd Fiala return false; 1425af245d11STodd Fiala 1426af245d11STodd Fiala const char **argv = args->m_argv; 1427af245d11STodd Fiala const char **envp = args->m_envp; 1428af245d11STodd Fiala const char *stdin_path = args->m_stdin_path; 1429af245d11STodd Fiala const char *stdout_path = args->m_stdout_path; 1430af245d11STodd Fiala const char *stderr_path = args->m_stderr_path; 1431af245d11STodd Fiala const char *working_dir = args->m_working_dir; 1432af245d11STodd Fiala 1433af245d11STodd Fiala lldb_utility::PseudoTerminal terminal; 1434af245d11STodd Fiala const size_t err_len = 1024; 1435af245d11STodd Fiala char err_str[err_len]; 1436af245d11STodd Fiala lldb::pid_t pid; 1437af245d11STodd Fiala NativeThreadProtocolSP thread_sp; 1438af245d11STodd Fiala 1439af245d11STodd Fiala lldb::ThreadSP inferior; 1440af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1441af245d11STodd Fiala 1442af245d11STodd Fiala // Propagate the environment if one is not supplied. 1443af245d11STodd Fiala if (envp == NULL || envp[0] == NULL) 1444af245d11STodd Fiala envp = const_cast<const char **>(environ); 1445af245d11STodd Fiala 1446af245d11STodd Fiala if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t> (-1)) 1447af245d11STodd Fiala { 1448af245d11STodd Fiala args->m_error.SetErrorToGenericError(); 1449af245d11STodd Fiala args->m_error.SetErrorString("Process fork failed."); 1450af245d11STodd Fiala goto FINISH; 1451af245d11STodd Fiala } 1452af245d11STodd Fiala 1453af245d11STodd Fiala // Recognized child exit status codes. 1454af245d11STodd Fiala enum { 1455af245d11STodd Fiala ePtraceFailed = 1, 1456af245d11STodd Fiala eDupStdinFailed, 1457af245d11STodd Fiala eDupStdoutFailed, 1458af245d11STodd Fiala eDupStderrFailed, 1459af245d11STodd Fiala eChdirFailed, 1460af245d11STodd Fiala eExecFailed, 1461af245d11STodd Fiala eSetGidFailed 1462af245d11STodd Fiala }; 1463af245d11STodd Fiala 1464af245d11STodd Fiala // Child process. 1465af245d11STodd Fiala if (pid == 0) 1466af245d11STodd Fiala { 1467af245d11STodd Fiala if (log) 1468af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior process preparing to fork", __FUNCTION__); 1469af245d11STodd Fiala 1470af245d11STodd Fiala // Trace this process. 1471af245d11STodd Fiala if (log) 1472af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior process issuing PTRACE_TRACEME", __FUNCTION__); 1473af245d11STodd Fiala 1474af245d11STodd Fiala if (PTRACE(PTRACE_TRACEME, 0, NULL, NULL, 0) < 0) 1475af245d11STodd Fiala { 1476af245d11STodd Fiala if (log) 1477af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior process PTRACE_TRACEME failed", __FUNCTION__); 1478af245d11STodd Fiala exit(ePtraceFailed); 1479af245d11STodd Fiala } 1480af245d11STodd Fiala 1481af245d11STodd Fiala // Do not inherit setgid powers. 1482af245d11STodd Fiala if (log) 1483af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior process resetting gid", __FUNCTION__); 1484af245d11STodd Fiala 1485af245d11STodd Fiala if (setgid(getgid()) != 0) 1486af245d11STodd Fiala { 1487af245d11STodd Fiala if (log) 1488af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior process setgid() failed", __FUNCTION__); 1489af245d11STodd Fiala exit(eSetGidFailed); 1490af245d11STodd Fiala } 1491af245d11STodd Fiala 1492af245d11STodd Fiala // Attempt to have our own process group. 1493af245d11STodd Fiala // TODO verify if we really want this. 1494af245d11STodd Fiala if (log) 1495af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior process resetting process group", __FUNCTION__); 1496af245d11STodd Fiala 1497af245d11STodd Fiala if (setpgid(0, 0) != 0) 1498af245d11STodd Fiala { 1499af245d11STodd Fiala if (log) 1500af245d11STodd Fiala { 1501af245d11STodd Fiala const int error_code = errno; 1502*511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s inferior setpgid() failed, errno=%d (%s), continuing with existing process group %" PRIu64, 1503af245d11STodd Fiala __FUNCTION__, 1504af245d11STodd Fiala error_code, 1505af245d11STodd Fiala strerror (error_code), 1506af245d11STodd Fiala static_cast<lldb::pid_t> (getpgid (0))); 1507af245d11STodd Fiala } 1508af245d11STodd Fiala // Don't allow this to prevent an inferior exec. 1509af245d11STodd Fiala } 1510af245d11STodd Fiala 1511af245d11STodd Fiala // Dup file descriptors if needed. 1512af245d11STodd Fiala // 1513af245d11STodd Fiala // FIXME: If two or more of the paths are the same we needlessly open 1514af245d11STodd Fiala // the same file multiple times. 1515af245d11STodd Fiala if (stdin_path != NULL && stdin_path[0]) 1516af245d11STodd Fiala if (!DupDescriptor(stdin_path, STDIN_FILENO, O_RDONLY)) 1517af245d11STodd Fiala exit(eDupStdinFailed); 1518af245d11STodd Fiala 1519af245d11STodd Fiala if (stdout_path != NULL && stdout_path[0]) 1520af245d11STodd Fiala if (!DupDescriptor(stdout_path, STDOUT_FILENO, O_WRONLY | O_CREAT)) 1521af245d11STodd Fiala exit(eDupStdoutFailed); 1522af245d11STodd Fiala 1523af245d11STodd Fiala if (stderr_path != NULL && stderr_path[0]) 1524af245d11STodd Fiala if (!DupDescriptor(stderr_path, STDERR_FILENO, O_WRONLY | O_CREAT)) 1525af245d11STodd Fiala exit(eDupStderrFailed); 1526af245d11STodd Fiala 1527af245d11STodd Fiala // Change working directory 1528af245d11STodd Fiala if (working_dir != NULL && working_dir[0]) 1529af245d11STodd Fiala if (0 != ::chdir(working_dir)) 1530af245d11STodd Fiala exit(eChdirFailed); 1531af245d11STodd Fiala 15320bce1b67STodd Fiala // Disable ASLR if requested. 15330bce1b67STodd Fiala if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR)) 15340bce1b67STodd Fiala { 15350bce1b67STodd Fiala const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS); 15360bce1b67STodd Fiala if (old_personality == -1) 15370bce1b67STodd Fiala { 15380bce1b67STodd Fiala if (log) 15390bce1b67STodd Fiala log->Printf ("NativeProcessLinux::%s retrieval of Linux personality () failed: %s. Cannot disable ASLR.", __FUNCTION__, strerror (errno)); 15400bce1b67STodd Fiala } 15410bce1b67STodd Fiala else 15420bce1b67STodd Fiala { 15430bce1b67STodd Fiala const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality); 15440bce1b67STodd Fiala if (new_personality == -1) 15450bce1b67STodd Fiala { 15460bce1b67STodd Fiala if (log) 15470bce1b67STodd Fiala log->Printf ("NativeProcessLinux::%s setting of Linux personality () to disable ASLR failed, ignoring: %s", __FUNCTION__, strerror (errno)); 15480bce1b67STodd Fiala 15490bce1b67STodd Fiala } 15500bce1b67STodd Fiala else 15510bce1b67STodd Fiala { 15520bce1b67STodd Fiala if (log) 15530bce1b67STodd Fiala log->Printf ("NativeProcessLinux::%s disbling ASLR: SUCCESS", __FUNCTION__); 15540bce1b67STodd Fiala 15550bce1b67STodd Fiala } 15560bce1b67STodd Fiala } 15570bce1b67STodd Fiala } 15580bce1b67STodd Fiala 1559af245d11STodd Fiala // Execute. We should never return. 1560af245d11STodd Fiala execve(argv[0], 1561af245d11STodd Fiala const_cast<char *const *>(argv), 1562af245d11STodd Fiala const_cast<char *const *>(envp)); 1563af245d11STodd Fiala exit(eExecFailed); 1564af245d11STodd Fiala } 1565af245d11STodd Fiala 1566af245d11STodd Fiala // Wait for the child process to trap on its call to execve. 1567af245d11STodd Fiala ::pid_t wpid; 1568af245d11STodd Fiala int status; 1569af245d11STodd Fiala if ((wpid = waitpid(pid, &status, 0)) < 0) 1570af245d11STodd Fiala { 1571af245d11STodd Fiala args->m_error.SetErrorToErrno(); 1572af245d11STodd Fiala 1573af245d11STodd Fiala if (log) 1574af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s waitpid for inferior failed with %s", __FUNCTION__, args->m_error.AsCString ()); 1575af245d11STodd Fiala 1576af245d11STodd Fiala // Mark the inferior as invalid. 1577af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 1578af245d11STodd Fiala monitor->SetState (StateType::eStateInvalid); 1579af245d11STodd Fiala 1580af245d11STodd Fiala goto FINISH; 1581af245d11STodd Fiala } 1582af245d11STodd Fiala else if (WIFEXITED(status)) 1583af245d11STodd Fiala { 1584af245d11STodd Fiala // open, dup or execve likely failed for some reason. 1585af245d11STodd Fiala args->m_error.SetErrorToGenericError(); 1586af245d11STodd Fiala switch (WEXITSTATUS(status)) 1587af245d11STodd Fiala { 1588af245d11STodd Fiala case ePtraceFailed: 1589af245d11STodd Fiala args->m_error.SetErrorString("Child ptrace failed."); 1590af245d11STodd Fiala break; 1591af245d11STodd Fiala case eDupStdinFailed: 1592af245d11STodd Fiala args->m_error.SetErrorString("Child open stdin failed."); 1593af245d11STodd Fiala break; 1594af245d11STodd Fiala case eDupStdoutFailed: 1595af245d11STodd Fiala args->m_error.SetErrorString("Child open stdout failed."); 1596af245d11STodd Fiala break; 1597af245d11STodd Fiala case eDupStderrFailed: 1598af245d11STodd Fiala args->m_error.SetErrorString("Child open stderr failed."); 1599af245d11STodd Fiala break; 1600af245d11STodd Fiala case eChdirFailed: 1601af245d11STodd Fiala args->m_error.SetErrorString("Child failed to set working directory."); 1602af245d11STodd Fiala break; 1603af245d11STodd Fiala case eExecFailed: 1604af245d11STodd Fiala args->m_error.SetErrorString("Child exec failed."); 1605af245d11STodd Fiala break; 1606af245d11STodd Fiala case eSetGidFailed: 1607af245d11STodd Fiala args->m_error.SetErrorString("Child setgid failed."); 1608af245d11STodd Fiala break; 1609af245d11STodd Fiala default: 1610af245d11STodd Fiala args->m_error.SetErrorString("Child returned unknown exit status."); 1611af245d11STodd Fiala break; 1612af245d11STodd Fiala } 1613af245d11STodd Fiala 1614af245d11STodd Fiala if (log) 1615af245d11STodd Fiala { 1616af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior exited with status %d before issuing a STOP", 1617af245d11STodd Fiala __FUNCTION__, 1618af245d11STodd Fiala WEXITSTATUS(status)); 1619af245d11STodd Fiala } 1620af245d11STodd Fiala 1621af245d11STodd Fiala // Mark the inferior as invalid. 1622af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 1623af245d11STodd Fiala monitor->SetState (StateType::eStateInvalid); 1624af245d11STodd Fiala 1625af245d11STodd Fiala goto FINISH; 1626af245d11STodd Fiala } 1627af245d11STodd Fiala assert(WIFSTOPPED(status) && (wpid == static_cast< ::pid_t> (pid)) && 1628af245d11STodd Fiala "Could not sync with inferior process."); 1629af245d11STodd Fiala 1630af245d11STodd Fiala if (log) 1631af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior started, now in stopped state", __FUNCTION__); 1632af245d11STodd Fiala 1633af245d11STodd Fiala if (!SetDefaultPtraceOpts(pid)) 1634af245d11STodd Fiala { 1635af245d11STodd Fiala args->m_error.SetErrorToErrno(); 1636af245d11STodd Fiala if (log) 1637af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior failed to set default ptrace options: %s", 1638af245d11STodd Fiala __FUNCTION__, 1639af245d11STodd Fiala args->m_error.AsCString ()); 1640af245d11STodd Fiala 1641af245d11STodd Fiala // Mark the inferior as invalid. 1642af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 1643af245d11STodd Fiala monitor->SetState (StateType::eStateInvalid); 1644af245d11STodd Fiala 1645af245d11STodd Fiala goto FINISH; 1646af245d11STodd Fiala } 1647af245d11STodd Fiala 1648af245d11STodd Fiala // Release the master terminal descriptor and pass it off to the 1649af245d11STodd Fiala // NativeProcessLinux instance. Similarly stash the inferior pid. 1650af245d11STodd Fiala monitor->m_terminal_fd = terminal.ReleaseMasterFileDescriptor(); 1651af245d11STodd Fiala monitor->m_pid = pid; 1652af245d11STodd Fiala 1653af245d11STodd Fiala // Set the terminal fd to be in non blocking mode (it simplifies the 1654af245d11STodd Fiala // implementation of ProcessLinux::GetSTDOUT to have a non-blocking 1655af245d11STodd Fiala // descriptor to read from). 1656af245d11STodd Fiala if (!EnsureFDFlags(monitor->m_terminal_fd, O_NONBLOCK, args->m_error)) 1657af245d11STodd Fiala { 1658af245d11STodd Fiala if (log) 1659af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior EnsureFDFlags failed for ensuring terminal O_NONBLOCK setting: %s", 1660af245d11STodd Fiala __FUNCTION__, 1661af245d11STodd Fiala args->m_error.AsCString ()); 1662af245d11STodd Fiala 1663af245d11STodd Fiala // Mark the inferior as invalid. 1664af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 1665af245d11STodd Fiala monitor->SetState (StateType::eStateInvalid); 1666af245d11STodd Fiala 1667af245d11STodd Fiala goto FINISH; 1668af245d11STodd Fiala } 1669af245d11STodd Fiala 1670af245d11STodd Fiala if (log) 1671af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() adding pid = %" PRIu64, __FUNCTION__, pid); 1672af245d11STodd Fiala 1673af245d11STodd Fiala thread_sp = monitor->AddThread (static_cast<lldb::tid_t> (pid)); 1674af245d11STodd Fiala assert (thread_sp && "AddThread() returned a nullptr thread"); 1675af245d11STodd Fiala reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedBySignal (SIGSTOP); 1676af245d11STodd Fiala monitor->SetCurrentThreadID (thread_sp->GetID ()); 1677af245d11STodd Fiala 1678af245d11STodd Fiala // Let our process instance know the thread has stopped. 1679af245d11STodd Fiala monitor->SetState (StateType::eStateStopped); 1680af245d11STodd Fiala 1681af245d11STodd Fiala FINISH: 1682af245d11STodd Fiala if (log) 1683af245d11STodd Fiala { 1684af245d11STodd Fiala if (args->m_error.Success ()) 1685af245d11STodd Fiala { 1686af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior launching succeeded", __FUNCTION__); 1687af245d11STodd Fiala } 1688af245d11STodd Fiala else 1689af245d11STodd Fiala { 1690af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior launching failed: %s", 1691af245d11STodd Fiala __FUNCTION__, 1692af245d11STodd Fiala args->m_error.AsCString ()); 1693af245d11STodd Fiala } 1694af245d11STodd Fiala } 1695af245d11STodd Fiala return args->m_error.Success(); 1696af245d11STodd Fiala } 1697af245d11STodd Fiala 1698af245d11STodd Fiala void 1699af245d11STodd Fiala NativeProcessLinux::StartAttachOpThread(AttachArgs *args, lldb_private::Error &error) 1700af245d11STodd Fiala { 1701af245d11STodd Fiala static const char *g_thread_name = "lldb.process.linux.operation"; 1702af245d11STodd Fiala 170339de3110SZachary Turner if (m_operation_thread.GetState() == eThreadStateRunning) 1704af245d11STodd Fiala return; 1705af245d11STodd Fiala 170639de3110SZachary Turner m_operation_thread = ThreadLauncher::LaunchThread(g_thread_name, AttachOpThread, args, &error); 1707af245d11STodd Fiala } 1708af245d11STodd Fiala 1709af245d11STodd Fiala void * 1710af245d11STodd Fiala NativeProcessLinux::AttachOpThread(void *arg) 1711af245d11STodd Fiala { 1712af245d11STodd Fiala AttachArgs *args = static_cast<AttachArgs*>(arg); 1713af245d11STodd Fiala 1714af245d11STodd Fiala if (!Attach(args)) { 1715af245d11STodd Fiala sem_post(&args->m_semaphore); 1716af245d11STodd Fiala return NULL; 1717af245d11STodd Fiala } 1718af245d11STodd Fiala 1719af245d11STodd Fiala ServeOperation(args); 1720af245d11STodd Fiala return NULL; 1721af245d11STodd Fiala } 1722af245d11STodd Fiala 1723af245d11STodd Fiala bool 1724af245d11STodd Fiala NativeProcessLinux::Attach(AttachArgs *args) 1725af245d11STodd Fiala { 1726af245d11STodd Fiala lldb::pid_t pid = args->m_pid; 1727af245d11STodd Fiala 1728af245d11STodd Fiala NativeProcessLinux *monitor = args->m_monitor; 1729af245d11STodd Fiala lldb::ThreadSP inferior; 1730af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1731af245d11STodd Fiala 1732af245d11STodd Fiala // Use a map to keep track of the threads which we have attached/need to attach. 1733af245d11STodd Fiala Host::TidMap tids_to_attach; 1734af245d11STodd Fiala if (pid <= 1) 1735af245d11STodd Fiala { 1736af245d11STodd Fiala args->m_error.SetErrorToGenericError(); 1737af245d11STodd Fiala args->m_error.SetErrorString("Attaching to process 1 is not allowed."); 1738af245d11STodd Fiala goto FINISH; 1739af245d11STodd Fiala } 1740af245d11STodd Fiala 1741af245d11STodd Fiala while (Host::FindProcessThreads(pid, tids_to_attach)) 1742af245d11STodd Fiala { 1743af245d11STodd Fiala for (Host::TidMap::iterator it = tids_to_attach.begin(); 1744af245d11STodd Fiala it != tids_to_attach.end();) 1745af245d11STodd Fiala { 1746af245d11STodd Fiala if (it->second == false) 1747af245d11STodd Fiala { 1748af245d11STodd Fiala lldb::tid_t tid = it->first; 1749af245d11STodd Fiala 1750af245d11STodd Fiala // Attach to the requested process. 1751af245d11STodd Fiala // An attach will cause the thread to stop with a SIGSTOP. 1752af245d11STodd Fiala if (PTRACE(PTRACE_ATTACH, tid, NULL, NULL, 0) < 0) 1753af245d11STodd Fiala { 1754af245d11STodd Fiala // No such thread. The thread may have exited. 1755af245d11STodd Fiala // More error handling may be needed. 1756af245d11STodd Fiala if (errno == ESRCH) 1757af245d11STodd Fiala { 1758af245d11STodd Fiala it = tids_to_attach.erase(it); 1759af245d11STodd Fiala continue; 1760af245d11STodd Fiala } 1761af245d11STodd Fiala else 1762af245d11STodd Fiala { 1763af245d11STodd Fiala args->m_error.SetErrorToErrno(); 1764af245d11STodd Fiala goto FINISH; 1765af245d11STodd Fiala } 1766af245d11STodd Fiala } 1767af245d11STodd Fiala 1768af245d11STodd Fiala int status; 1769af245d11STodd Fiala // Need to use __WALL otherwise we receive an error with errno=ECHLD 1770af245d11STodd Fiala // At this point we should have a thread stopped if waitpid succeeds. 1771af245d11STodd Fiala if ((status = waitpid(tid, NULL, __WALL)) < 0) 1772af245d11STodd Fiala { 1773af245d11STodd Fiala // No such thread. The thread may have exited. 1774af245d11STodd Fiala // More error handling may be needed. 1775af245d11STodd Fiala if (errno == ESRCH) 1776af245d11STodd Fiala { 1777af245d11STodd Fiala it = tids_to_attach.erase(it); 1778af245d11STodd Fiala continue; 1779af245d11STodd Fiala } 1780af245d11STodd Fiala else 1781af245d11STodd Fiala { 1782af245d11STodd Fiala args->m_error.SetErrorToErrno(); 1783af245d11STodd Fiala goto FINISH; 1784af245d11STodd Fiala } 1785af245d11STodd Fiala } 1786af245d11STodd Fiala 1787af245d11STodd Fiala if (!SetDefaultPtraceOpts(tid)) 1788af245d11STodd Fiala { 1789af245d11STodd Fiala args->m_error.SetErrorToErrno(); 1790af245d11STodd Fiala goto FINISH; 1791af245d11STodd Fiala } 1792af245d11STodd Fiala 1793af245d11STodd Fiala 1794af245d11STodd Fiala if (log) 1795af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() adding tid = %" PRIu64, __FUNCTION__, tid); 1796af245d11STodd Fiala 1797af245d11STodd Fiala it->second = true; 1798af245d11STodd Fiala 1799af245d11STodd Fiala // Create the thread, mark it as stopped. 1800af245d11STodd Fiala NativeThreadProtocolSP thread_sp (monitor->AddThread (static_cast<lldb::tid_t> (tid))); 1801af245d11STodd Fiala assert (thread_sp && "AddThread() returned a nullptr"); 1802af245d11STodd Fiala reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedBySignal (SIGSTOP); 1803af245d11STodd Fiala monitor->SetCurrentThreadID (thread_sp->GetID ()); 1804af245d11STodd Fiala } 1805af245d11STodd Fiala 1806af245d11STodd Fiala // move the loop forward 1807af245d11STodd Fiala ++it; 1808af245d11STodd Fiala } 1809af245d11STodd Fiala } 1810af245d11STodd Fiala 1811af245d11STodd Fiala if (tids_to_attach.size() > 0) 1812af245d11STodd Fiala { 1813af245d11STodd Fiala monitor->m_pid = pid; 1814af245d11STodd Fiala // Let our process instance know the thread has stopped. 1815af245d11STodd Fiala monitor->SetState (StateType::eStateStopped); 1816af245d11STodd Fiala } 1817af245d11STodd Fiala else 1818af245d11STodd Fiala { 1819af245d11STodd Fiala args->m_error.SetErrorToGenericError(); 1820af245d11STodd Fiala args->m_error.SetErrorString("No such process."); 1821af245d11STodd Fiala } 1822af245d11STodd Fiala 1823af245d11STodd Fiala FINISH: 1824af245d11STodd Fiala return args->m_error.Success(); 1825af245d11STodd Fiala } 1826af245d11STodd Fiala 1827af245d11STodd Fiala bool 1828af245d11STodd Fiala NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) 1829af245d11STodd Fiala { 1830af245d11STodd Fiala long ptrace_opts = 0; 1831af245d11STodd Fiala 1832af245d11STodd Fiala // Have the child raise an event on exit. This is used to keep the child in 1833af245d11STodd Fiala // limbo until it is destroyed. 1834af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXIT; 1835af245d11STodd Fiala 1836af245d11STodd Fiala // Have the tracer trace threads which spawn in the inferior process. 1837af245d11STodd Fiala // TODO: if we want to support tracing the inferiors' child, add the 1838af245d11STodd Fiala // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK) 1839af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACECLONE; 1840af245d11STodd Fiala 1841af245d11STodd Fiala // Have the tracer notify us before execve returns 1842af245d11STodd Fiala // (needed to disable legacy SIGTRAP generation) 1843af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXEC; 1844af245d11STodd Fiala 1845af245d11STodd Fiala return PTRACE(PTRACE_SETOPTIONS, pid, NULL, (void*)ptrace_opts, 0) >= 0; 1846af245d11STodd Fiala } 1847af245d11STodd Fiala 1848af245d11STodd Fiala static ExitType convert_pid_status_to_exit_type (int status) 1849af245d11STodd Fiala { 1850af245d11STodd Fiala if (WIFEXITED (status)) 1851af245d11STodd Fiala return ExitType::eExitTypeExit; 1852af245d11STodd Fiala else if (WIFSIGNALED (status)) 1853af245d11STodd Fiala return ExitType::eExitTypeSignal; 1854af245d11STodd Fiala else if (WIFSTOPPED (status)) 1855af245d11STodd Fiala return ExitType::eExitTypeStop; 1856af245d11STodd Fiala else 1857af245d11STodd Fiala { 1858af245d11STodd Fiala // We don't know what this is. 1859af245d11STodd Fiala return ExitType::eExitTypeInvalid; 1860af245d11STodd Fiala } 1861af245d11STodd Fiala } 1862af245d11STodd Fiala 1863af245d11STodd Fiala static int convert_pid_status_to_return_code (int status) 1864af245d11STodd Fiala { 1865af245d11STodd Fiala if (WIFEXITED (status)) 1866af245d11STodd Fiala return WEXITSTATUS (status); 1867af245d11STodd Fiala else if (WIFSIGNALED (status)) 1868af245d11STodd Fiala return WTERMSIG (status); 1869af245d11STodd Fiala else if (WIFSTOPPED (status)) 1870af245d11STodd Fiala return WSTOPSIG (status); 1871af245d11STodd Fiala else 1872af245d11STodd Fiala { 1873af245d11STodd Fiala // We don't know what this is. 1874af245d11STodd Fiala return ExitType::eExitTypeInvalid; 1875af245d11STodd Fiala } 1876af245d11STodd Fiala } 1877af245d11STodd Fiala 1878af245d11STodd Fiala // Main process monitoring waitpid-loop handler. 1879af245d11STodd Fiala bool 1880af245d11STodd Fiala NativeProcessLinux::MonitorCallback(void *callback_baton, 1881af245d11STodd Fiala lldb::pid_t pid, 1882af245d11STodd Fiala bool exited, 1883af245d11STodd Fiala int signal, 1884af245d11STodd Fiala int status) 1885af245d11STodd Fiala { 1886af245d11STodd Fiala Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); 1887af245d11STodd Fiala 1888af245d11STodd Fiala NativeProcessLinux *const process = static_cast<NativeProcessLinux*>(callback_baton); 1889af245d11STodd Fiala assert (process && "process is null"); 1890af245d11STodd Fiala if (!process) 1891af245d11STodd Fiala { 1892af245d11STodd Fiala if (log) 1893af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " callback_baton was null, can't determine process to use", __FUNCTION__, pid); 1894af245d11STodd Fiala return true; 1895af245d11STodd Fiala } 1896af245d11STodd Fiala 1897af245d11STodd Fiala // Certain activities differ based on whether the pid is the tid of the main thread. 1898af245d11STodd Fiala const bool is_main_thread = (pid == process->GetID ()); 1899af245d11STodd Fiala 1900af245d11STodd Fiala // Assume we keep monitoring by default. 1901af245d11STodd Fiala bool stop_monitoring = false; 1902af245d11STodd Fiala 1903af245d11STodd Fiala // Handle when the thread exits. 1904af245d11STodd Fiala if (exited) 1905af245d11STodd Fiala { 1906af245d11STodd Fiala if (log) 1907af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() got exit signal, tid = %" PRIu64 " (%s main thread)", __FUNCTION__, pid, is_main_thread ? "is" : "is not"); 1908af245d11STodd Fiala 1909af245d11STodd Fiala // This is a thread that exited. Ensure we're not tracking it anymore. 1910af245d11STodd Fiala const bool thread_found = process->StopTrackingThread (pid); 1911af245d11STodd Fiala 1912af245d11STodd Fiala if (is_main_thread) 1913af245d11STodd Fiala { 1914af245d11STodd Fiala // We only set the exit status and notify the delegate if we haven't already set the process 1915af245d11STodd Fiala // state to an exited state. We normally should have received a SIGTRAP | (PTRACE_EVENT_EXIT << 8) 1916af245d11STodd Fiala // for the main thread. 1917af245d11STodd Fiala const bool already_notified = (process->GetState() == StateType::eStateExited) | (process->GetState () == StateType::eStateCrashed); 1918af245d11STodd Fiala if (!already_notified) 1919af245d11STodd Fiala { 1920af245d11STodd Fiala if (log) 1921af245d11STodd 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 ())); 1922af245d11STodd Fiala // The main thread exited. We're done monitoring. Report to delegate. 1923af245d11STodd Fiala process->SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); 1924af245d11STodd Fiala 1925af245d11STodd Fiala // Notify delegate that our process has exited. 1926af245d11STodd Fiala process->SetState (StateType::eStateExited, true); 1927af245d11STodd Fiala } 1928af245d11STodd Fiala else 1929af245d11STodd Fiala { 1930af245d11STodd Fiala if (log) 1931af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() tid = %" PRIu64 " main thread now exited (%s)", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found"); 1932af245d11STodd Fiala } 1933af245d11STodd Fiala return true; 1934af245d11STodd Fiala } 1935af245d11STodd Fiala else 1936af245d11STodd Fiala { 1937af245d11STodd Fiala // Do we want to report to the delegate in this case? I think not. If this was an orderly 1938af245d11STodd Fiala // thread exit, we would already have received the SIGTRAP | (PTRACE_EVENT_EXIT << 8) signal, 1939af245d11STodd Fiala // and we would have done an all-stop then. 1940af245d11STodd Fiala if (log) 1941af245d11STodd 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"); 1942af245d11STodd Fiala 1943af245d11STodd Fiala // Not the main thread, we keep going. 1944af245d11STodd Fiala return false; 1945af245d11STodd Fiala } 1946af245d11STodd Fiala } 1947af245d11STodd Fiala 1948af245d11STodd Fiala // Get details on the signal raised. 1949af245d11STodd Fiala siginfo_t info; 1950af245d11STodd Fiala int ptrace_err = 0; 1951af245d11STodd Fiala 1952af245d11STodd Fiala if (!process->GetSignalInfo (pid, &info, ptrace_err)) 1953af245d11STodd Fiala { 1954af245d11STodd Fiala if (ptrace_err == EINVAL) 1955af245d11STodd Fiala { 1956*511e5cdcSTodd Fiala process->OnGroupStop (pid); 1957a9882ceeSTodd Fiala } 1958a9882ceeSTodd Fiala else 1959a9882ceeSTodd Fiala { 1960af245d11STodd Fiala // ptrace(GETSIGINFO) failed (but not due to group-stop). 1961af245d11STodd Fiala 1962af245d11STodd Fiala // A return value of ESRCH means the thread/process is no longer on the system, 1963af245d11STodd Fiala // so it was killed somehow outside of our control. Either way, we can't do anything 1964af245d11STodd Fiala // with it anymore. 1965af245d11STodd Fiala 1966af245d11STodd Fiala // We stop monitoring if it was the main thread. 1967af245d11STodd Fiala stop_monitoring = is_main_thread; 1968af245d11STodd Fiala 1969af245d11STodd Fiala // Stop tracking the metadata for the thread since it's entirely off the system now. 1970af245d11STodd Fiala const bool thread_found = process->StopTrackingThread (pid); 1971af245d11STodd Fiala 1972af245d11STodd Fiala if (log) 1973af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d (%s, %s, %s)", 1974af245d11STodd Fiala __FUNCTION__, strerror(ptrace_err), pid, signal, status, ptrace_err == 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"); 1975af245d11STodd Fiala 1976af245d11STodd Fiala if (is_main_thread) 1977af245d11STodd Fiala { 1978af245d11STodd Fiala // Notify the delegate - our process is not available but appears to have been killed outside 1979af245d11STodd Fiala // our control. Is eStateExited the right exit state in this case? 1980af245d11STodd Fiala process->SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); 1981af245d11STodd Fiala process->SetState (StateType::eStateExited, true); 1982af245d11STodd Fiala } 1983af245d11STodd Fiala else 1984af245d11STodd Fiala { 1985af245d11STodd Fiala // This thread was pulled out from underneath us. Anything to do here? Do we want to do an all stop? 1986af245d11STodd Fiala if (log) 1987af245d11STodd 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); 1988af245d11STodd Fiala } 1989af245d11STodd Fiala } 1990af245d11STodd Fiala } 1991af245d11STodd Fiala else 1992af245d11STodd Fiala { 1993af245d11STodd Fiala // We have retrieved the signal info. Dispatch appropriately. 1994af245d11STodd Fiala if (info.si_signo == SIGTRAP) 1995af245d11STodd Fiala process->MonitorSIGTRAP(&info, pid); 1996af245d11STodd Fiala else 1997af245d11STodd Fiala process->MonitorSignal(&info, pid, exited); 1998af245d11STodd Fiala 1999af245d11STodd Fiala stop_monitoring = false; 2000af245d11STodd Fiala } 2001af245d11STodd Fiala 2002af245d11STodd Fiala return stop_monitoring; 2003af245d11STodd Fiala } 2004af245d11STodd Fiala 2005af245d11STodd Fiala void 2006af245d11STodd Fiala NativeProcessLinux::MonitorSIGTRAP(const siginfo_t *info, lldb::pid_t pid) 2007af245d11STodd Fiala { 2008af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2009af245d11STodd Fiala const bool is_main_thread = (pid == GetID ()); 2010af245d11STodd Fiala 2011af245d11STodd Fiala assert(info && info->si_signo == SIGTRAP && "Unexpected child signal!"); 2012af245d11STodd Fiala if (!info) 2013af245d11STodd Fiala return; 2014af245d11STodd Fiala 2015af245d11STodd Fiala // See if we can find a thread for this signal. 2016af245d11STodd Fiala NativeThreadProtocolSP thread_sp = GetThreadByID (pid); 2017af245d11STodd Fiala if (!thread_sp) 2018af245d11STodd Fiala { 2019af245d11STodd Fiala if (log) 2020af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " no thread found for tid %" PRIu64, __FUNCTION__, GetID (), pid); 2021af245d11STodd Fiala } 2022af245d11STodd Fiala 2023af245d11STodd Fiala switch (info->si_code) 2024af245d11STodd Fiala { 2025af245d11STodd 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. 2026af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)): 2027af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)): 2028af245d11STodd Fiala 2029af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)): 2030af245d11STodd Fiala { 2031af245d11STodd Fiala lldb::tid_t tid = LLDB_INVALID_THREAD_ID; 2032af245d11STodd Fiala 2033af245d11STodd Fiala unsigned long event_message = 0; 2034af245d11STodd Fiala if (GetEventMessage(pid, &event_message)) 2035af245d11STodd Fiala tid = static_cast<lldb::tid_t> (event_message); 2036af245d11STodd Fiala 2037af245d11STodd Fiala if (log) 2038af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " received thread creation event for tid %" PRIu64, __FUNCTION__, pid, tid); 2039af245d11STodd Fiala 2040af245d11STodd Fiala // If we don't track the thread yet: create it, mark as stopped. 2041af245d11STodd Fiala // If we do track it, this is the wait we needed. Now resume the new thread. 2042af245d11STodd Fiala // In all cases, resume the current (i.e. main process) thread. 2043*511e5cdcSTodd Fiala bool created_now = false; 2044*511e5cdcSTodd Fiala thread_sp = GetOrCreateThread (tid, created_now); 2045af245d11STodd Fiala assert (thread_sp.get() && "failed to get or create the tracking data for newly created inferior thread"); 2046af245d11STodd Fiala 2047af245d11STodd Fiala // If the thread was already tracked, it means the created thread already received its SI_USER notification of creation. 2048*511e5cdcSTodd Fiala if (!created_now) 2049af245d11STodd Fiala { 2050af245d11STodd Fiala // FIXME loops like we want to stop all theads here. 2051af245d11STodd Fiala // StopAllThreads 2052af245d11STodd Fiala 2053af245d11STodd Fiala // We can now resume the newly created thread since it is fully created. 2054af245d11STodd Fiala reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetRunning (); 2055af245d11STodd Fiala Resume (tid, LLDB_INVALID_SIGNAL_NUMBER); 2056af245d11STodd Fiala } 2057af245d11STodd Fiala else 2058af245d11STodd Fiala { 2059af245d11STodd Fiala // Mark the thread as currently launching. Need to wait for SIGTRAP clone on the main thread before 2060af245d11STodd Fiala // this thread is ready to go. 2061af245d11STodd Fiala reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetLaunching (); 2062af245d11STodd Fiala } 2063af245d11STodd Fiala 2064af245d11STodd Fiala // In all cases, we can resume the main thread here. 2065af245d11STodd Fiala Resume (pid, LLDB_INVALID_SIGNAL_NUMBER); 2066af245d11STodd Fiala break; 2067af245d11STodd Fiala } 2068af245d11STodd Fiala 2069af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): 2070a9882ceeSTodd Fiala { 2071a9882ceeSTodd Fiala NativeThreadProtocolSP main_thread_sp; 2072a9882ceeSTodd Fiala 2073af245d11STodd Fiala if (log) 2074af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received exec event, code = %d", __FUNCTION__, info->si_code ^ SIGTRAP); 2075a9882ceeSTodd Fiala 2076a9882ceeSTodd Fiala // Remove all but the main thread here. 2077a9882ceeSTodd Fiala // FIXME check if we really need to do this - how does ptrace behave under exec when multiple threads were present 2078a9882ceeSTodd Fiala // before the exec? If we get all the detach signals right, we don't need to do this. However, it makes it clearer 2079a9882ceeSTodd Fiala // what we should really be tracking. 2080a9882ceeSTodd Fiala { 2081a9882ceeSTodd Fiala Mutex::Locker locker (m_threads_mutex); 2082a9882ceeSTodd Fiala 2083a9882ceeSTodd Fiala if (log) 2084a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s exec received, stop tracking all but main thread", __FUNCTION__); 2085a9882ceeSTodd Fiala 2086a9882ceeSTodd Fiala for (auto thread_sp : m_threads) 2087a9882ceeSTodd Fiala { 2088a9882ceeSTodd Fiala const bool is_main_thread = thread_sp && thread_sp->GetID () == GetID (); 2089a9882ceeSTodd Fiala if (is_main_thread) 2090a9882ceeSTodd Fiala { 2091a9882ceeSTodd Fiala main_thread_sp = thread_sp; 2092a9882ceeSTodd Fiala if (log) 2093a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s found main thread with tid %" PRIu64 ", keeping", __FUNCTION__, main_thread_sp->GetID ()); 2094a9882ceeSTodd Fiala } 2095a9882ceeSTodd Fiala else 2096a9882ceeSTodd Fiala { 2097a9882ceeSTodd Fiala if (log) 2098a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s discarding non-main-thread tid %" PRIu64 " due to exec", __FUNCTION__, thread_sp->GetID ()); 2099a9882ceeSTodd Fiala } 2100a9882ceeSTodd Fiala } 2101a9882ceeSTodd Fiala 2102a9882ceeSTodd Fiala m_threads.clear (); 2103a9882ceeSTodd Fiala 2104a9882ceeSTodd Fiala if (main_thread_sp) 2105a9882ceeSTodd Fiala { 2106a9882ceeSTodd Fiala m_threads.push_back (main_thread_sp); 2107a9882ceeSTodd Fiala SetCurrentThreadID (main_thread_sp->GetID ()); 2108a9882ceeSTodd Fiala reinterpret_cast<NativeThreadLinux*>(main_thread_sp.get())->SetStoppedByExec (); 2109a9882ceeSTodd Fiala } 2110a9882ceeSTodd Fiala else 2111a9882ceeSTodd Fiala { 2112a9882ceeSTodd Fiala SetCurrentThreadID (LLDB_INVALID_THREAD_ID); 2113a9882ceeSTodd Fiala if (log) 2114a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 "no main thread found, discarded all threads, we're in a no-thread state!", __FUNCTION__, GetID ()); 2115a9882ceeSTodd Fiala } 2116a9882ceeSTodd Fiala } 2117a9882ceeSTodd Fiala 2118a9882ceeSTodd Fiala // Let our delegate know we have just exec'd. 2119a9882ceeSTodd Fiala NotifyDidExec (); 2120a9882ceeSTodd Fiala 2121a9882ceeSTodd Fiala // If we have a main thread, indicate we are stopped. 2122a9882ceeSTodd Fiala assert (main_thread_sp && "exec called during ptraced process but no main thread metadata tracked"); 2123a9882ceeSTodd Fiala SetState (StateType::eStateStopped); 2124a9882ceeSTodd Fiala 2125af245d11STodd Fiala break; 2126a9882ceeSTodd Fiala } 2127af245d11STodd Fiala 2128af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): 2129af245d11STodd Fiala { 2130af245d11STodd Fiala // The inferior process or one of its threads is about to exit. 2131af245d11STodd Fiala // Maintain the process or thread in a state of "limbo" until we are 2132af245d11STodd Fiala // explicitly commanded to detach, destroy, resume, etc. 2133af245d11STodd Fiala unsigned long data = 0; 2134af245d11STodd Fiala if (!GetEventMessage(pid, &data)) 2135af245d11STodd Fiala data = -1; 2136af245d11STodd Fiala 2137af245d11STodd Fiala if (log) 2138af245d11STodd Fiala { 2139af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received PTRACE_EVENT_EXIT, data = %lx (WIFEXITED=%s,WIFSIGNALED=%s), pid = %" PRIu64 " (%s)", 2140af245d11STodd Fiala __FUNCTION__, 2141af245d11STodd Fiala data, WIFEXITED (data) ? "true" : "false", WIFSIGNALED (data) ? "true" : "false", 2142af245d11STodd Fiala pid, 2143af245d11STodd Fiala is_main_thread ? "is main thread" : "not main thread"); 2144af245d11STodd Fiala } 2145af245d11STodd Fiala 2146af245d11STodd Fiala // Set the thread to exited. 2147af245d11STodd Fiala if (thread_sp) 2148af245d11STodd Fiala reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetExited (); 2149af245d11STodd Fiala else 2150af245d11STodd Fiala { 2151af245d11STodd Fiala if (log) 2152af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " failed to retrieve thread for tid %" PRIu64", cannot set thread state", __FUNCTION__, GetID (), pid); 2153af245d11STodd Fiala } 2154af245d11STodd Fiala 2155af245d11STodd Fiala if (is_main_thread) 2156af245d11STodd Fiala { 2157af245d11STodd Fiala SetExitStatus (convert_pid_status_to_exit_type (data), convert_pid_status_to_return_code (data), nullptr, true); 2158af245d11STodd Fiala // Resume the thread so it completely exits. 2159af245d11STodd Fiala Resume (pid, LLDB_INVALID_SIGNAL_NUMBER); 2160af245d11STodd Fiala } 2161af245d11STodd Fiala else 2162af245d11STodd Fiala { 2163af245d11STodd Fiala // FIXME figure out the path where we plan to reap the metadata for the thread. 2164af245d11STodd Fiala } 2165af245d11STodd Fiala 2166af245d11STodd Fiala break; 2167af245d11STodd Fiala } 2168af245d11STodd Fiala 2169af245d11STodd Fiala case 0: 2170af245d11STodd Fiala case TRAP_TRACE: 2171af245d11STodd Fiala // We receive this on single stepping. 2172af245d11STodd Fiala if (log) 2173af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received trace event, pid = %" PRIu64 " (single stepping)", __FUNCTION__, pid); 2174af245d11STodd Fiala 2175af245d11STodd Fiala if (thread_sp) 2176af245d11STodd Fiala { 2177af245d11STodd Fiala reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedBySignal (SIGTRAP); 2178af245d11STodd Fiala SetCurrentThreadID (thread_sp->GetID ()); 2179af245d11STodd Fiala } 2180af245d11STodd Fiala else 2181af245d11STodd Fiala { 2182af245d11STodd Fiala if (log) 2183af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 " single stepping received trace but thread not found", __FUNCTION__, GetID (), pid); 2184af245d11STodd Fiala } 2185af245d11STodd Fiala 2186af245d11STodd Fiala // Tell the process we have a stop (from single stepping). 2187af245d11STodd Fiala SetState (StateType::eStateStopped, true); 2188af245d11STodd Fiala break; 2189af245d11STodd Fiala 2190af245d11STodd Fiala case SI_KERNEL: 2191af245d11STodd Fiala case TRAP_BRKPT: 2192af245d11STodd Fiala if (log) 2193af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received breakpoint event, pid = %" PRIu64, __FUNCTION__, pid); 2194af245d11STodd Fiala 2195af245d11STodd Fiala // Mark the thread as stopped at breakpoint. 2196af245d11STodd Fiala if (thread_sp) 2197af245d11STodd Fiala { 2198af245d11STodd Fiala reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedBySignal (SIGTRAP); 2199af245d11STodd Fiala Error error = FixupBreakpointPCAsNeeded (thread_sp); 2200af245d11STodd Fiala if (error.Fail ()) 2201af245d11STodd Fiala { 2202af245d11STodd Fiala if (log) 2203af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 " fixup: %s", __FUNCTION__, pid, error.AsCString ()); 2204af245d11STodd Fiala } 2205af245d11STodd Fiala } 2206af245d11STodd Fiala else 2207af245d11STodd Fiala { 2208af245d11STodd Fiala if (log) 2209af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 ": warning, cannot process software breakpoint since no thread metadata", __FUNCTION__, pid); 2210af245d11STodd Fiala } 2211af245d11STodd Fiala 2212af245d11STodd Fiala 2213af245d11STodd Fiala // Tell the process we have a stop from this thread. 2214af245d11STodd Fiala SetCurrentThreadID (pid); 2215af245d11STodd Fiala SetState (StateType::eStateStopped, true); 2216af245d11STodd Fiala break; 2217af245d11STodd Fiala 2218af245d11STodd Fiala case TRAP_HWBKPT: 2219af245d11STodd Fiala if (log) 2220af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received watchpoint event, pid = %" PRIu64, __FUNCTION__, pid); 2221af245d11STodd Fiala 2222af245d11STodd Fiala // Mark the thread as stopped at watchpoint. 2223af245d11STodd Fiala // The address is at (lldb::addr_t)info->si_addr if we need it. 2224af245d11STodd Fiala if (thread_sp) 2225af245d11STodd Fiala reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedBySignal (SIGTRAP); 2226af245d11STodd Fiala else 2227af245d11STodd Fiala { 2228af245d11STodd Fiala if (log) 2229af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 ": warning, cannot process hardware breakpoint since no thread metadata", __FUNCTION__, GetID (), pid); 2230af245d11STodd Fiala } 2231af245d11STodd Fiala 2232af245d11STodd Fiala // Tell the process we have a stop from this thread. 2233af245d11STodd Fiala SetCurrentThreadID (pid); 2234af245d11STodd Fiala SetState (StateType::eStateStopped, true); 2235af245d11STodd Fiala break; 2236af245d11STodd Fiala 2237af245d11STodd Fiala case SIGTRAP: 2238af245d11STodd Fiala case (SIGTRAP | 0x80): 2239af245d11STodd Fiala if (log) 2240af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received system call stop event, pid %" PRIu64 "tid %" PRIu64, __FUNCTION__, GetID (), pid); 2241af245d11STodd Fiala // Ignore these signals until we know more about them. 2242af245d11STodd Fiala Resume(pid, 0); 2243af245d11STodd Fiala break; 2244af245d11STodd Fiala 2245af245d11STodd Fiala default: 2246af245d11STodd Fiala assert(false && "Unexpected SIGTRAP code!"); 2247af245d11STodd Fiala if (log) 2248af245d11STodd 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))); 2249af245d11STodd Fiala break; 2250af245d11STodd Fiala 2251af245d11STodd Fiala } 2252af245d11STodd Fiala } 2253af245d11STodd Fiala 2254af245d11STodd Fiala void 2255af245d11STodd Fiala NativeProcessLinux::MonitorSignal(const siginfo_t *info, lldb::pid_t pid, bool exited) 2256af245d11STodd Fiala { 2257*511e5cdcSTodd Fiala assert (info && "null info"); 2258*511e5cdcSTodd Fiala if (!info) 2259*511e5cdcSTodd Fiala return; 2260*511e5cdcSTodd Fiala 2261*511e5cdcSTodd Fiala const int signo = info->si_signo; 2262*511e5cdcSTodd Fiala const bool is_from_llgs = info->si_pid == getpid (); 2263af245d11STodd Fiala 2264af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2265af245d11STodd Fiala 2266af245d11STodd Fiala // POSIX says that process behaviour is undefined after it ignores a SIGFPE, 2267af245d11STodd Fiala // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a 2268af245d11STodd Fiala // kill(2) or raise(3). Similarly for tgkill(2) on Linux. 2269af245d11STodd Fiala // 2270af245d11STodd Fiala // IOW, user generated signals never generate what we consider to be a 2271af245d11STodd Fiala // "crash". 2272af245d11STodd Fiala // 2273af245d11STodd Fiala // Similarly, ACK signals generated by this monitor. 2274af245d11STodd Fiala 2275af245d11STodd Fiala // See if we can find a thread for this signal. 2276af245d11STodd Fiala NativeThreadProtocolSP thread_sp = GetThreadByID (pid); 2277af245d11STodd Fiala if (!thread_sp) 2278af245d11STodd Fiala { 2279af245d11STodd Fiala if (log) 2280af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " no thread found for tid %" PRIu64, __FUNCTION__, GetID (), pid); 2281af245d11STodd Fiala } 2282af245d11STodd Fiala 2283af245d11STodd Fiala // Handle the signal. 2284af245d11STodd Fiala if (info->si_code == SI_TKILL || info->si_code == SI_USER) 2285af245d11STodd Fiala { 2286af245d11STodd Fiala if (log) 2287af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received signal %s (%d) with code %s, (siginfo pid = %d (%s), waitpid pid = %" PRIu64 ")", 2288af245d11STodd Fiala __FUNCTION__, 2289af245d11STodd Fiala GetUnixSignals ().GetSignalAsCString (signo), 2290af245d11STodd Fiala signo, 2291af245d11STodd Fiala (info->si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"), 2292af245d11STodd Fiala info->si_pid, 2293*511e5cdcSTodd Fiala is_from_llgs ? "from llgs" : "not from llgs", 2294af245d11STodd Fiala pid); 229558a2f669STodd Fiala } 2296af245d11STodd Fiala 229758a2f669STodd Fiala // Check for new thread notification. 229858a2f669STodd Fiala if ((info->si_pid == 0) && (info->si_code == SI_USER)) 2299af245d11STodd Fiala { 2300af245d11STodd Fiala // A new thread creation is being signaled. This is one of two parts that come in 2301af245d11STodd Fiala // a non-deterministic order. pid is the thread id. 2302af245d11STodd Fiala if (log) 2303af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 " tid %" PRIu64 ": new thread notification", 2304af245d11STodd Fiala __FUNCTION__, GetID (), pid); 2305af245d11STodd Fiala 2306af245d11STodd Fiala // Did we already create the thread? 2307*511e5cdcSTodd Fiala bool created_now = false; 2308*511e5cdcSTodd Fiala thread_sp = GetOrCreateThread (pid, created_now); 2309af245d11STodd Fiala assert (thread_sp.get() && "failed to get or create the tracking data for newly created inferior thread"); 2310af245d11STodd Fiala 2311af245d11STodd Fiala // If the thread was already tracked, it means the main thread already received its SIGTRAP for the create. 2312*511e5cdcSTodd Fiala if (!created_now) 2313af245d11STodd Fiala { 2314af245d11STodd Fiala // We can now resume this thread up since it is fully created. 2315af245d11STodd Fiala reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetRunning (); 2316af245d11STodd Fiala Resume (thread_sp->GetID (), LLDB_INVALID_SIGNAL_NUMBER); 2317af245d11STodd Fiala } 2318af245d11STodd Fiala else 2319af245d11STodd Fiala { 2320af245d11STodd Fiala // Mark the thread as currently launching. Need to wait for SIGTRAP clone on the main thread before 2321af245d11STodd Fiala // this thread is ready to go. 2322af245d11STodd Fiala reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetLaunching (); 2323af245d11STodd Fiala } 232458a2f669STodd Fiala 232558a2f669STodd Fiala // Done handling. 232658a2f669STodd Fiala return; 2327af245d11STodd Fiala } 232858a2f669STodd Fiala 232958a2f669STodd Fiala // Check for thread stop notification. 2330*511e5cdcSTodd Fiala if (is_from_llgs && (info->si_code == SI_TKILL) && (signo == SIGSTOP)) 2331af245d11STodd Fiala { 2332af245d11STodd Fiala // This is a tgkill()-based stop. 2333af245d11STodd Fiala if (thread_sp) 2334af245d11STodd Fiala { 2335af245d11STodd Fiala // An inferior thread just stopped. Mark it as such. 2336af245d11STodd Fiala reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedBySignal (signo); 2337af245d11STodd Fiala SetCurrentThreadID (thread_sp->GetID ()); 2338af245d11STodd Fiala 2339af245d11STodd Fiala // Remove this tid from the wait-for-stop set. 2340af245d11STodd Fiala Mutex::Locker locker (m_wait_for_stop_tids_mutex); 2341af245d11STodd Fiala 2342af245d11STodd Fiala auto removed_count = m_wait_for_stop_tids.erase (thread_sp->GetID ()); 2343af245d11STodd Fiala if (removed_count < 1) 2344af245d11STodd Fiala { 2345af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 " tid %" PRIu64 ": tgkill()-stopped thread not in m_wait_for_stop_tids", 2346af245d11STodd Fiala __FUNCTION__, GetID (), thread_sp->GetID ()); 2347af245d11STodd Fiala 2348af245d11STodd Fiala } 2349af245d11STodd Fiala 2350af245d11STodd Fiala // If this is the last thread in the m_wait_for_stop_tids, we need to notify 2351af245d11STodd Fiala // the delegate that a stop has occurred now that every thread that was supposed 2352af245d11STodd Fiala // to stop has stopped. 2353af245d11STodd Fiala if (m_wait_for_stop_tids.empty ()) 2354af245d11STodd Fiala { 2355af245d11STodd Fiala if (log) 2356af245d11STodd Fiala { 2357af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 ", setting process state to stopped now that all tids marked for stop have completed", 2358af245d11STodd Fiala __FUNCTION__, 2359af245d11STodd Fiala GetID (), 2360af245d11STodd Fiala pid); 2361af245d11STodd Fiala } 2362af245d11STodd Fiala SetState (StateType::eStateStopped, true); 2363af245d11STodd Fiala } 2364af245d11STodd Fiala } 2365af245d11STodd Fiala 236658a2f669STodd Fiala // Done handling. 2367af245d11STodd Fiala return; 2368af245d11STodd Fiala } 2369af245d11STodd Fiala 2370af245d11STodd Fiala if (log) 2371af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received signal %s", __FUNCTION__, GetUnixSignals ().GetSignalAsCString (signo)); 2372af245d11STodd Fiala 2373af245d11STodd Fiala switch (signo) 2374af245d11STodd Fiala { 2375af245d11STodd Fiala case SIGSEGV: 2376af245d11STodd Fiala { 2377af245d11STodd Fiala lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); 2378af245d11STodd Fiala 2379af245d11STodd Fiala // FIXME figure out how to propagate this properly. Seems like it 2380af245d11STodd Fiala // should go in ThreadStopInfo. 2381af245d11STodd Fiala // We can get more details on the exact nature of the crash here. 2382af245d11STodd Fiala // ProcessMessage::CrashReason reason = GetCrashReasonForSIGSEGV(info); 2383af245d11STodd Fiala if (!exited) 2384af245d11STodd Fiala { 2385af245d11STodd Fiala // This is just a pre-signal-delivery notification of the incoming signal. 2386af245d11STodd Fiala // Send a stop to the debugger. 2387af245d11STodd Fiala if (thread_sp) 2388af245d11STodd Fiala { 2389af245d11STodd Fiala reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedBySignal (signo); 2390af245d11STodd Fiala SetCurrentThreadID (thread_sp->GetID ()); 2391af245d11STodd Fiala } 2392af245d11STodd Fiala SetState (StateType::eStateStopped, true); 2393af245d11STodd Fiala } 2394af245d11STodd Fiala else 2395af245d11STodd Fiala { 2396af245d11STodd Fiala if (thread_sp) 2397af245d11STodd Fiala { 2398af245d11STodd Fiala // FIXME figure out what type this is. 2399af245d11STodd Fiala const uint64_t exception_type = static_cast<uint64_t> (SIGSEGV); 2400af245d11STodd Fiala reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetCrashedWithException (exception_type, fault_addr); 2401af245d11STodd Fiala } 2402af245d11STodd Fiala SetState (StateType::eStateCrashed, true); 2403af245d11STodd Fiala } 2404af245d11STodd Fiala } 2405af245d11STodd Fiala break; 2406af245d11STodd Fiala 240758a2f669STodd Fiala case SIGABRT: 2408af245d11STodd Fiala case SIGILL: 2409af245d11STodd Fiala case SIGFPE: 2410af245d11STodd Fiala case SIGBUS: 2411af245d11STodd Fiala { 241258a2f669STodd Fiala // Break these out into separate cases once I have more data for each type of signal. 241358a2f669STodd Fiala lldb::addr_t fault_addr = reinterpret_cast<lldb::addr_t>(info->si_addr); 241458a2f669STodd Fiala if (!exited) 241558a2f669STodd Fiala { 241658a2f669STodd Fiala // This is just a pre-signal-delivery notification of the incoming signal. 241758a2f669STodd Fiala // Send a stop to the debugger. 241858a2f669STodd Fiala if (thread_sp) 241958a2f669STodd Fiala { 242058a2f669STodd Fiala reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetStoppedBySignal (signo); 242158a2f669STodd Fiala SetCurrentThreadID (thread_sp->GetID ()); 242258a2f669STodd Fiala } 242358a2f669STodd Fiala SetState (StateType::eStateStopped, true); 242458a2f669STodd Fiala } 242558a2f669STodd Fiala else 242658a2f669STodd Fiala { 242758a2f669STodd Fiala if (thread_sp) 242858a2f669STodd Fiala { 242958a2f669STodd Fiala // FIXME figure out how to report exit by signal correctly. 243058a2f669STodd Fiala const uint64_t exception_type = static_cast<uint64_t> (SIGABRT); 243158a2f669STodd Fiala reinterpret_cast<NativeThreadLinux*> (thread_sp.get ())->SetCrashedWithException (exception_type, fault_addr); 243258a2f669STodd Fiala } 243358a2f669STodd Fiala SetState (StateType::eStateCrashed, true); 243458a2f669STodd Fiala } 2435af245d11STodd Fiala } 2436af245d11STodd Fiala break; 2437af245d11STodd Fiala 2438*511e5cdcSTodd Fiala case SIGSTOP: 2439*511e5cdcSTodd Fiala { 244058a2f669STodd Fiala if (log) 2441*511e5cdcSTodd Fiala { 2442*511e5cdcSTodd Fiala if (is_from_llgs) 2443*511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " received SIGSTOP from llgs, most likely an interrupt", __FUNCTION__, GetID (), pid); 2444*511e5cdcSTodd Fiala else 2445*511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " received SIGSTOP from outside of debugger", __FUNCTION__, GetID (), pid); 2446*511e5cdcSTodd Fiala } 2447*511e5cdcSTodd Fiala 2448*511e5cdcSTodd Fiala // Save group stop tids to wait for. 2449*511e5cdcSTodd Fiala SetGroupStopTids (pid, SIGSTOP); 2450*511e5cdcSTodd Fiala // Fall through to deliver signal to thread. 2451*511e5cdcSTodd Fiala // This will trigger a group stop sequence, after which we'll notify the process that everything stopped. 2452*511e5cdcSTodd Fiala } 2453*511e5cdcSTodd Fiala 2454*511e5cdcSTodd Fiala default: 2455*511e5cdcSTodd Fiala { 2456*511e5cdcSTodd Fiala if (log) 2457*511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " resuming thread with signal %s (%d)", __FUNCTION__, GetID (), pid, GetUnixSignals().GetSignalAsCString (signo), signo); 2458*511e5cdcSTodd Fiala 2459*511e5cdcSTodd Fiala // Pass the signal on to the inferior. 2460*511e5cdcSTodd Fiala const bool resume_success = Resume (pid, signo); 2461*511e5cdcSTodd Fiala 2462*511e5cdcSTodd Fiala if (log) 2463*511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " resume %s", __FUNCTION__, GetID (), pid, resume_success ? "SUCCESS" : "FAILURE"); 2464*511e5cdcSTodd Fiala 2465*511e5cdcSTodd Fiala } 2466af245d11STodd Fiala break; 2467af245d11STodd Fiala } 2468af245d11STodd Fiala } 2469af245d11STodd Fiala 2470*511e5cdcSTodd Fiala void 2471*511e5cdcSTodd Fiala NativeProcessLinux::SetGroupStopTids (lldb::tid_t signaled_thread_tid, int signo) 2472*511e5cdcSTodd Fiala { 2473*511e5cdcSTodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 2474*511e5cdcSTodd Fiala 2475*511e5cdcSTodd Fiala // Lock 1 - thread lock. 2476*511e5cdcSTodd Fiala { 2477*511e5cdcSTodd Fiala Mutex::Locker locker (m_threads_mutex); 2478*511e5cdcSTodd Fiala // Lock 2 - group stop tids 2479*511e5cdcSTodd Fiala { 2480*511e5cdcSTodd Fiala Mutex::Locker locker (m_wait_for_group_stop_tids_mutex); 2481*511e5cdcSTodd Fiala if (log) 2482*511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " loading up known threads in set%s", 2483*511e5cdcSTodd Fiala __FUNCTION__, 2484*511e5cdcSTodd Fiala GetID (), 2485*511e5cdcSTodd Fiala signaled_thread_tid, 2486*511e5cdcSTodd Fiala m_wait_for_group_stop_tids.empty () ? " (currently empty)" 2487*511e5cdcSTodd Fiala : "(group_stop_tids not empty?!?)"); 2488*511e5cdcSTodd Fiala 2489*511e5cdcSTodd Fiala // Add all known threads not already stopped into the wait for group-stop tids. 2490*511e5cdcSTodd Fiala for (auto thread_sp : m_threads) 2491*511e5cdcSTodd Fiala { 2492*511e5cdcSTodd Fiala int unused_signo = LLDB_INVALID_SIGNAL_NUMBER; 2493*511e5cdcSTodd Fiala if (thread_sp && !((NativeThreadLinux*)thread_sp.get())->IsStopped (&unused_signo)) 2494*511e5cdcSTodd Fiala { 2495*511e5cdcSTodd Fiala // Wait on this thread for a group stop before we notify the delegate about the process state change. 2496*511e5cdcSTodd Fiala m_wait_for_group_stop_tids.insert (thread_sp->GetID ()); 2497*511e5cdcSTodd Fiala } 2498*511e5cdcSTodd Fiala } 2499*511e5cdcSTodd Fiala 2500*511e5cdcSTodd Fiala m_group_stop_signal_tid = signaled_thread_tid; 2501*511e5cdcSTodd Fiala m_group_stop_signal = signo; 2502*511e5cdcSTodd Fiala } 2503*511e5cdcSTodd Fiala } 2504*511e5cdcSTodd Fiala } 2505*511e5cdcSTodd Fiala 2506*511e5cdcSTodd Fiala void 2507*511e5cdcSTodd Fiala NativeProcessLinux::OnGroupStop (lldb::tid_t tid) 2508*511e5cdcSTodd Fiala { 2509*511e5cdcSTodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 2510*511e5cdcSTodd Fiala bool should_tell_delegate = false; 2511*511e5cdcSTodd Fiala 2512*511e5cdcSTodd Fiala // Lock 1 - thread lock. 2513*511e5cdcSTodd Fiala { 2514*511e5cdcSTodd Fiala Mutex::Locker locker (m_threads_mutex); 2515*511e5cdcSTodd Fiala // Lock 2 - group stop tids 2516*511e5cdcSTodd Fiala { 2517*511e5cdcSTodd Fiala Mutex::Locker locker (m_wait_for_group_stop_tids_mutex); 2518*511e5cdcSTodd Fiala 2519*511e5cdcSTodd Fiala // Remove this thread from the set. 2520*511e5cdcSTodd Fiala auto remove_result = m_wait_for_group_stop_tids.erase (tid); 2521*511e5cdcSTodd Fiala if (log) 2522*511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " tried to remove tid from group-stop set: %s", 2523*511e5cdcSTodd Fiala __FUNCTION__, 2524*511e5cdcSTodd Fiala GetID (), 2525*511e5cdcSTodd Fiala tid, 2526*511e5cdcSTodd Fiala remove_result > 0 ? "SUCCESS" : "FAILURE"); 2527*511e5cdcSTodd Fiala 2528*511e5cdcSTodd Fiala // Grab the thread metadata for this thread. 2529*511e5cdcSTodd Fiala NativeThreadProtocolSP thread_sp = GetThreadByIDUnlocked (tid); 2530*511e5cdcSTodd Fiala if (thread_sp) 2531*511e5cdcSTodd Fiala { 2532*511e5cdcSTodd Fiala NativeThreadLinux *const linux_thread = static_cast<NativeThreadLinux*> (thread_sp.get ()); 2533*511e5cdcSTodd Fiala if (thread_sp->GetID () == m_group_stop_signal_tid) 2534*511e5cdcSTodd Fiala { 2535*511e5cdcSTodd Fiala linux_thread->SetStoppedBySignal (m_group_stop_signal); 2536*511e5cdcSTodd Fiala if (log) 2537*511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " set group stop tid to state 'stopped by signal %d'", 2538*511e5cdcSTodd Fiala __FUNCTION__, 2539*511e5cdcSTodd Fiala GetID (), 2540*511e5cdcSTodd Fiala tid, 2541*511e5cdcSTodd Fiala m_group_stop_signal); 2542*511e5cdcSTodd Fiala } 2543*511e5cdcSTodd Fiala else 2544*511e5cdcSTodd Fiala { 2545*511e5cdcSTodd Fiala int stopping_signal = LLDB_INVALID_SIGNAL_NUMBER; 2546*511e5cdcSTodd Fiala if (linux_thread->IsStopped (&stopping_signal)) 2547*511e5cdcSTodd Fiala { 2548*511e5cdcSTodd Fiala if (log) 2549*511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " thread is already stopped with signal %d, not clearing", 2550*511e5cdcSTodd Fiala __FUNCTION__, 2551*511e5cdcSTodd Fiala GetID (), 2552*511e5cdcSTodd Fiala tid, 2553*511e5cdcSTodd Fiala stopping_signal); 2554*511e5cdcSTodd Fiala 2555*511e5cdcSTodd Fiala } 2556*511e5cdcSTodd Fiala else 2557*511e5cdcSTodd Fiala { 2558*511e5cdcSTodd Fiala linux_thread->SetStoppedBySignal (0); 2559*511e5cdcSTodd Fiala if (log) 2560*511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " set stopped by signal with signal 0 (i.e. debugger-initiated stop)", 2561*511e5cdcSTodd Fiala __FUNCTION__, 2562*511e5cdcSTodd Fiala GetID (), 2563*511e5cdcSTodd Fiala tid); 2564*511e5cdcSTodd Fiala 2565*511e5cdcSTodd Fiala } 2566*511e5cdcSTodd Fiala } 2567*511e5cdcSTodd Fiala } 2568*511e5cdcSTodd Fiala else 2569*511e5cdcSTodd Fiala { 2570*511e5cdcSTodd Fiala if (log) 2571*511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " WARNING failed to find thread metadata for tid", 2572*511e5cdcSTodd Fiala __FUNCTION__, 2573*511e5cdcSTodd Fiala GetID (), 2574*511e5cdcSTodd Fiala tid); 2575*511e5cdcSTodd Fiala 2576*511e5cdcSTodd Fiala } 2577*511e5cdcSTodd Fiala 2578*511e5cdcSTodd Fiala // If there are no more threads we're waiting on for group stop, signal the process. 2579*511e5cdcSTodd Fiala if (m_wait_for_group_stop_tids.empty ()) 2580*511e5cdcSTodd Fiala { 2581*511e5cdcSTodd Fiala if (log) 2582*511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " done waiting for group stop, will notify delegate of process state change", 2583*511e5cdcSTodd Fiala __FUNCTION__, 2584*511e5cdcSTodd Fiala GetID (), 2585*511e5cdcSTodd Fiala tid); 2586*511e5cdcSTodd Fiala 2587*511e5cdcSTodd Fiala SetCurrentThreadID (m_group_stop_signal_tid); 2588*511e5cdcSTodd Fiala 2589*511e5cdcSTodd Fiala // Tell the delegate about the stop event, after we release our mutexes. 2590*511e5cdcSTodd Fiala should_tell_delegate = true; 2591*511e5cdcSTodd Fiala } 2592*511e5cdcSTodd Fiala } 2593*511e5cdcSTodd Fiala } 2594*511e5cdcSTodd Fiala 2595*511e5cdcSTodd Fiala // If we're ready to broadcast the process event change, do it now that we're no longer 2596*511e5cdcSTodd Fiala // holding any locks. Note this does introduce a potential race, we should think about 2597*511e5cdcSTodd Fiala // adding a notification queue. 2598*511e5cdcSTodd Fiala if (should_tell_delegate) 2599*511e5cdcSTodd Fiala { 2600*511e5cdcSTodd Fiala if (log) 2601*511e5cdcSTodd Fiala log->Printf ("NativeProcessLinux::%s pid = %" PRIu64 " tid %" PRIu64 " done waiting for group stop, notifying delegate of process state change", 2602*511e5cdcSTodd Fiala __FUNCTION__, 2603*511e5cdcSTodd Fiala GetID (), 2604*511e5cdcSTodd Fiala tid); 2605*511e5cdcSTodd Fiala SetState (StateType::eStateStopped, true); 2606*511e5cdcSTodd Fiala } 2607*511e5cdcSTodd Fiala } 2608*511e5cdcSTodd Fiala 2609af245d11STodd Fiala Error 2610af245d11STodd Fiala NativeProcessLinux::Resume (const ResumeActionList &resume_actions) 2611af245d11STodd Fiala { 2612af245d11STodd Fiala Error error; 2613af245d11STodd Fiala 2614af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); 2615af245d11STodd Fiala if (log) 2616af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s called: pid %" PRIu64, __FUNCTION__, GetID ()); 2617af245d11STodd Fiala 2618af245d11STodd Fiala int run_thread_count = 0; 2619af245d11STodd Fiala int stop_thread_count = 0; 2620af245d11STodd Fiala int step_thread_count = 0; 2621af245d11STodd Fiala 2622af245d11STodd Fiala std::vector<NativeThreadProtocolSP> new_stop_threads; 2623af245d11STodd Fiala 2624af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 2625af245d11STodd Fiala for (auto thread_sp : m_threads) 2626af245d11STodd Fiala { 2627af245d11STodd Fiala assert (thread_sp && "thread list should not contain NULL threads"); 2628af245d11STodd Fiala NativeThreadLinux *const linux_thread_p = reinterpret_cast<NativeThreadLinux*> (thread_sp.get ()); 2629af245d11STodd Fiala 2630af245d11STodd Fiala const ResumeAction *const action = resume_actions.GetActionForThread (thread_sp->GetID (), true); 2631af245d11STodd Fiala assert (action && "NULL ResumeAction returned for thread during Resume ()"); 2632af245d11STodd Fiala 2633af245d11STodd Fiala if (log) 2634af245d11STodd Fiala { 2635af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s processing resume action state %s for pid %" PRIu64 " tid %" PRIu64, 2636af245d11STodd Fiala __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); 2637af245d11STodd Fiala } 2638af245d11STodd Fiala 2639af245d11STodd Fiala switch (action->state) 2640af245d11STodd Fiala { 2641af245d11STodd Fiala case eStateRunning: 2642af245d11STodd Fiala // Run the thread, possibly feeding it the signal. 2643af245d11STodd Fiala linux_thread_p->SetRunning (); 2644af245d11STodd Fiala if (action->signal > 0) 2645af245d11STodd Fiala { 2646af245d11STodd Fiala // Resume the thread and deliver the given signal, 2647af245d11STodd Fiala // then mark as delivered. 2648af245d11STodd Fiala Resume (thread_sp->GetID (), action->signal); 2649af245d11STodd Fiala resume_actions.SetSignalHandledForThread (thread_sp->GetID ()); 2650af245d11STodd Fiala } 2651af245d11STodd Fiala else 2652af245d11STodd Fiala { 2653af245d11STodd Fiala // Just resume the thread with no signal. 2654af245d11STodd Fiala Resume (thread_sp->GetID (), LLDB_INVALID_SIGNAL_NUMBER); 2655af245d11STodd Fiala } 2656af245d11STodd Fiala ++run_thread_count; 2657af245d11STodd Fiala break; 2658af245d11STodd Fiala 2659af245d11STodd Fiala case eStateStepping: 2660af245d11STodd Fiala // Note: if we have multiple threads, we may need to stop 2661af245d11STodd Fiala // the other threads first, then step this one. 2662af245d11STodd Fiala linux_thread_p->SetStepping (); 2663af245d11STodd Fiala if (SingleStep (thread_sp->GetID (), 0)) 2664af245d11STodd Fiala { 2665af245d11STodd Fiala if (log) 2666af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 " single step succeeded", 2667af245d11STodd Fiala __FUNCTION__, GetID (), thread_sp->GetID ()); 2668af245d11STodd Fiala } 2669af245d11STodd Fiala else 2670af245d11STodd Fiala { 2671af245d11STodd Fiala if (log) 2672af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 " single step failed", 2673af245d11STodd Fiala __FUNCTION__, GetID (), thread_sp->GetID ()); 2674af245d11STodd Fiala } 2675af245d11STodd Fiala ++step_thread_count; 2676af245d11STodd Fiala break; 2677af245d11STodd Fiala 2678af245d11STodd Fiala case eStateSuspended: 2679af245d11STodd Fiala case eStateStopped: 2680af245d11STodd Fiala if (!StateIsStoppedState (linux_thread_p->GetState (), false)) 2681af245d11STodd Fiala new_stop_threads.push_back (thread_sp); 2682af245d11STodd Fiala else 2683af245d11STodd Fiala { 2684af245d11STodd Fiala if (log) 2685af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s no need to stop pid %" PRIu64 " tid %" PRIu64 ", thread state already %s", 2686af245d11STodd Fiala __FUNCTION__, GetID (), thread_sp->GetID (), StateAsCString (linux_thread_p->GetState ())); 2687af245d11STodd Fiala } 2688af245d11STodd Fiala 2689af245d11STodd Fiala ++stop_thread_count; 2690af245d11STodd Fiala break; 2691af245d11STodd Fiala 2692af245d11STodd Fiala default: 2693af245d11STodd Fiala return Error ("NativeProcessLinux::%s (): unexpected state %s specified for pid %" PRIu64 ", tid %" PRIu64, 2694af245d11STodd Fiala __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); 2695af245d11STodd Fiala } 2696af245d11STodd Fiala } 2697af245d11STodd Fiala 2698af245d11STodd Fiala // If any thread was set to run, notify the process state as running. 2699af245d11STodd Fiala if (run_thread_count > 0) 2700af245d11STodd Fiala SetState (StateType::eStateRunning, true); 2701af245d11STodd Fiala 2702af245d11STodd Fiala // Now do a tgkill SIGSTOP on each thread we want to stop. 2703af245d11STodd Fiala if (!new_stop_threads.empty ()) 2704af245d11STodd Fiala { 2705af245d11STodd Fiala // Lock the m_wait_for_stop_tids set so we can fill it with every thread we expect to have stopped. 2706af245d11STodd Fiala Mutex::Locker stop_thread_id_locker (m_wait_for_stop_tids_mutex); 2707af245d11STodd Fiala for (auto thread_sp : new_stop_threads) 2708af245d11STodd Fiala { 2709af245d11STodd Fiala // Send a stop signal to the thread. 2710af245d11STodd Fiala const int result = tgkill (GetID (), thread_sp->GetID (), SIGSTOP); 2711af245d11STodd Fiala if (result != 0) 2712af245d11STodd Fiala { 2713af245d11STodd Fiala // tgkill failed. 2714af245d11STodd Fiala if (log) 2715af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s error: tgkill SIGSTOP for pid %" PRIu64 " tid %" PRIu64 "failed, retval %d", 2716af245d11STodd Fiala __FUNCTION__, GetID (), thread_sp->GetID (), result); 2717af245d11STodd Fiala } 2718af245d11STodd Fiala else 2719af245d11STodd Fiala { 2720af245d11STodd Fiala // tgkill succeeded. Don't mark the thread state, though. Let the signal 2721af245d11STodd Fiala // handling mark it. 2722af245d11STodd Fiala if (log) 2723af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s tgkill SIGSTOP for pid %" PRIu64 " tid %" PRIu64 " succeeded", 2724af245d11STodd Fiala __FUNCTION__, GetID (), thread_sp->GetID ()); 2725af245d11STodd Fiala 2726af245d11STodd Fiala // Add it to the set of threads we expect to signal a stop. 2727af245d11STodd Fiala // We won't tell the delegate about it until this list drains to empty. 2728af245d11STodd Fiala m_wait_for_stop_tids.insert (thread_sp->GetID ()); 2729af245d11STodd Fiala } 2730af245d11STodd Fiala } 2731af245d11STodd Fiala } 2732af245d11STodd Fiala 2733af245d11STodd Fiala return error; 2734af245d11STodd Fiala } 2735af245d11STodd Fiala 2736af245d11STodd Fiala Error 2737af245d11STodd Fiala NativeProcessLinux::Halt () 2738af245d11STodd Fiala { 2739af245d11STodd Fiala Error error; 2740af245d11STodd Fiala 2741af245d11STodd Fiala // FIXME check if we're already stopped 2742af245d11STodd Fiala const bool is_stopped = false; 2743af245d11STodd Fiala if (is_stopped) 2744af245d11STodd Fiala return error; 2745af245d11STodd Fiala 2746af245d11STodd Fiala if (kill (GetID (), SIGSTOP) != 0) 2747af245d11STodd Fiala error.SetErrorToErrno (); 2748af245d11STodd Fiala 2749af245d11STodd Fiala return error; 2750af245d11STodd Fiala } 2751af245d11STodd Fiala 2752af245d11STodd Fiala Error 2753af245d11STodd Fiala NativeProcessLinux::Detach () 2754af245d11STodd Fiala { 2755af245d11STodd Fiala Error error; 2756af245d11STodd Fiala 2757af245d11STodd Fiala // Tell ptrace to detach from the process. 2758af245d11STodd Fiala if (GetID () != LLDB_INVALID_PROCESS_ID) 2759af245d11STodd Fiala error = Detach (GetID ()); 2760af245d11STodd Fiala 2761af245d11STodd Fiala // Stop monitoring the inferior. 2762af245d11STodd Fiala StopMonitor (); 2763af245d11STodd Fiala 2764af245d11STodd Fiala // No error. 2765af245d11STodd Fiala return error; 2766af245d11STodd Fiala } 2767af245d11STodd Fiala 2768af245d11STodd Fiala Error 2769af245d11STodd Fiala NativeProcessLinux::Signal (int signo) 2770af245d11STodd Fiala { 2771af245d11STodd Fiala Error error; 2772af245d11STodd Fiala 2773af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2774af245d11STodd Fiala if (log) 2775af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s: sending signal %d (%s) to pid %" PRIu64, 2776af245d11STodd Fiala __FUNCTION__, signo, GetUnixSignals ().GetSignalAsCString (signo), GetID ()); 2777af245d11STodd Fiala 2778af245d11STodd Fiala if (kill(GetID(), signo)) 2779af245d11STodd Fiala error.SetErrorToErrno(); 2780af245d11STodd Fiala 2781af245d11STodd Fiala return error; 2782af245d11STodd Fiala } 2783af245d11STodd Fiala 2784af245d11STodd Fiala Error 2785af245d11STodd Fiala NativeProcessLinux::Kill () 2786af245d11STodd Fiala { 2787af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2788af245d11STodd Fiala if (log) 2789af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s called for PID %" PRIu64, __FUNCTION__, GetID ()); 2790af245d11STodd Fiala 2791af245d11STodd Fiala Error error; 2792af245d11STodd Fiala 2793af245d11STodd Fiala switch (m_state) 2794af245d11STodd Fiala { 2795af245d11STodd Fiala case StateType::eStateInvalid: 2796af245d11STodd Fiala case StateType::eStateExited: 2797af245d11STodd Fiala case StateType::eStateCrashed: 2798af245d11STodd Fiala case StateType::eStateDetached: 2799af245d11STodd Fiala case StateType::eStateUnloaded: 2800af245d11STodd Fiala // Nothing to do - the process is already dead. 2801af245d11STodd Fiala if (log) 2802af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s ignored for PID %" PRIu64 " due to current state: %s", __FUNCTION__, GetID (), StateAsCString (m_state)); 2803af245d11STodd Fiala return error; 2804af245d11STodd Fiala 2805af245d11STodd Fiala case StateType::eStateConnected: 2806af245d11STodd Fiala case StateType::eStateAttaching: 2807af245d11STodd Fiala case StateType::eStateLaunching: 2808af245d11STodd Fiala case StateType::eStateStopped: 2809af245d11STodd Fiala case StateType::eStateRunning: 2810af245d11STodd Fiala case StateType::eStateStepping: 2811af245d11STodd Fiala case StateType::eStateSuspended: 2812af245d11STodd Fiala // We can try to kill a process in these states. 2813af245d11STodd Fiala break; 2814af245d11STodd Fiala } 2815af245d11STodd Fiala 2816af245d11STodd Fiala if (kill (GetID (), SIGKILL) != 0) 2817af245d11STodd Fiala { 2818af245d11STodd Fiala error.SetErrorToErrno (); 2819af245d11STodd Fiala return error; 2820af245d11STodd Fiala } 2821af245d11STodd Fiala 2822af245d11STodd Fiala return error; 2823af245d11STodd Fiala } 2824af245d11STodd Fiala 2825af245d11STodd Fiala static Error 2826af245d11STodd Fiala ParseMemoryRegionInfoFromProcMapsLine (const std::string &maps_line, MemoryRegionInfo &memory_region_info) 2827af245d11STodd Fiala { 2828af245d11STodd Fiala memory_region_info.Clear(); 2829af245d11STodd Fiala 2830af245d11STodd Fiala StringExtractor line_extractor (maps_line.c_str ()); 2831af245d11STodd Fiala 2832af245d11STodd Fiala // Format: {address_start_hex}-{address_end_hex} perms offset dev inode pathname 2833af245d11STodd Fiala // perms: rwxp (letter is present if set, '-' if not, final character is p=private, s=shared). 2834af245d11STodd Fiala 2835af245d11STodd Fiala // Parse out the starting address 2836af245d11STodd Fiala lldb::addr_t start_address = line_extractor.GetHexMaxU64 (false, 0); 2837af245d11STodd Fiala 2838af245d11STodd Fiala // Parse out hyphen separating start and end address from range. 2839af245d11STodd Fiala if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != '-')) 2840af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing dash between address range"); 2841af245d11STodd Fiala 2842af245d11STodd Fiala // Parse out the ending address 2843af245d11STodd Fiala lldb::addr_t end_address = line_extractor.GetHexMaxU64 (false, start_address); 2844af245d11STodd Fiala 2845af245d11STodd Fiala // Parse out the space after the address. 2846af245d11STodd Fiala if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != ' ')) 2847af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing space after range"); 2848af245d11STodd Fiala 2849af245d11STodd Fiala // Save the range. 2850af245d11STodd Fiala memory_region_info.GetRange ().SetRangeBase (start_address); 2851af245d11STodd Fiala memory_region_info.GetRange ().SetRangeEnd (end_address); 2852af245d11STodd Fiala 2853af245d11STodd Fiala // Parse out each permission entry. 2854af245d11STodd Fiala if (line_extractor.GetBytesLeft () < 4) 2855af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing some portion of permissions"); 2856af245d11STodd Fiala 2857af245d11STodd Fiala // Handle read permission. 2858af245d11STodd Fiala const char read_perm_char = line_extractor.GetChar (); 2859af245d11STodd Fiala if (read_perm_char == 'r') 2860af245d11STodd Fiala memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eYes); 2861af245d11STodd Fiala else 2862af245d11STodd Fiala { 2863af245d11STodd Fiala assert ( (read_perm_char == '-') && "unexpected /proc/{pid}/maps read permission char" ); 2864af245d11STodd Fiala memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); 2865af245d11STodd Fiala } 2866af245d11STodd Fiala 2867af245d11STodd Fiala // Handle write permission. 2868af245d11STodd Fiala const char write_perm_char = line_extractor.GetChar (); 2869af245d11STodd Fiala if (write_perm_char == 'w') 2870af245d11STodd Fiala memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eYes); 2871af245d11STodd Fiala else 2872af245d11STodd Fiala { 2873af245d11STodd Fiala assert ( (write_perm_char == '-') && "unexpected /proc/{pid}/maps write permission char" ); 2874af245d11STodd Fiala memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); 2875af245d11STodd Fiala } 2876af245d11STodd Fiala 2877af245d11STodd Fiala // Handle execute permission. 2878af245d11STodd Fiala const char exec_perm_char = line_extractor.GetChar (); 2879af245d11STodd Fiala if (exec_perm_char == 'x') 2880af245d11STodd Fiala memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eYes); 2881af245d11STodd Fiala else 2882af245d11STodd Fiala { 2883af245d11STodd Fiala assert ( (exec_perm_char == '-') && "unexpected /proc/{pid}/maps exec permission char" ); 2884af245d11STodd Fiala memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); 2885af245d11STodd Fiala } 2886af245d11STodd Fiala 2887af245d11STodd Fiala return Error (); 2888af245d11STodd Fiala } 2889af245d11STodd Fiala 2890af245d11STodd Fiala Error 2891af245d11STodd Fiala NativeProcessLinux::GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) 2892af245d11STodd Fiala { 2893af245d11STodd Fiala // FIXME review that the final memory region returned extends to the end of the virtual address space, 2894af245d11STodd Fiala // with no perms if it is not mapped. 2895af245d11STodd Fiala 2896af245d11STodd Fiala // Use an approach that reads memory regions from /proc/{pid}/maps. 2897af245d11STodd Fiala // Assume proc maps entries are in ascending order. 2898af245d11STodd Fiala // FIXME assert if we find differently. 2899af245d11STodd Fiala Mutex::Locker locker (m_mem_region_cache_mutex); 2900af245d11STodd Fiala 2901af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2902af245d11STodd Fiala Error error; 2903af245d11STodd Fiala 2904af245d11STodd Fiala if (m_supports_mem_region == LazyBool::eLazyBoolNo) 2905af245d11STodd Fiala { 2906af245d11STodd Fiala // We're done. 2907af245d11STodd Fiala error.SetErrorString ("unsupported"); 2908af245d11STodd Fiala return error; 2909af245d11STodd Fiala } 2910af245d11STodd Fiala 2911af245d11STodd Fiala // If our cache is empty, pull the latest. There should always be at least one memory region 2912af245d11STodd Fiala // if memory region handling is supported. 2913af245d11STodd Fiala if (m_mem_region_cache.empty ()) 2914af245d11STodd Fiala { 2915af245d11STodd Fiala error = ProcFileReader::ProcessLineByLine (GetID (), "maps", 2916af245d11STodd Fiala [&] (const std::string &line) -> bool 2917af245d11STodd Fiala { 2918af245d11STodd Fiala MemoryRegionInfo info; 2919af245d11STodd Fiala const Error parse_error = ParseMemoryRegionInfoFromProcMapsLine (line, info); 2920af245d11STodd Fiala if (parse_error.Success ()) 2921af245d11STodd Fiala { 2922af245d11STodd Fiala m_mem_region_cache.push_back (info); 2923af245d11STodd Fiala return true; 2924af245d11STodd Fiala } 2925af245d11STodd Fiala else 2926af245d11STodd Fiala { 2927af245d11STodd Fiala if (log) 2928af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to parse proc maps line '%s': %s", __FUNCTION__, line.c_str (), error.AsCString ()); 2929af245d11STodd Fiala return false; 2930af245d11STodd Fiala } 2931af245d11STodd Fiala }); 2932af245d11STodd Fiala 2933af245d11STodd Fiala // If we had an error, we'll mark unsupported. 2934af245d11STodd Fiala if (error.Fail ()) 2935af245d11STodd Fiala { 2936af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolNo; 2937af245d11STodd Fiala return error; 2938af245d11STodd Fiala } 2939af245d11STodd Fiala else if (m_mem_region_cache.empty ()) 2940af245d11STodd Fiala { 2941af245d11STodd Fiala // No entries after attempting to read them. This shouldn't happen if /proc/{pid}/maps 2942af245d11STodd Fiala // is supported. Assume we don't support map entries via procfs. 2943af245d11STodd Fiala if (log) 2944af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to find any procfs maps entries, assuming no support for memory region metadata retrieval", __FUNCTION__); 2945af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolNo; 2946af245d11STodd Fiala error.SetErrorString ("not supported"); 2947af245d11STodd Fiala return error; 2948af245d11STodd Fiala } 2949af245d11STodd Fiala 2950af245d11STodd Fiala if (log) 2951af245d11STodd 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 ()); 2952af245d11STodd Fiala 2953af245d11STodd Fiala // We support memory retrieval, remember that. 2954af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolYes; 2955af245d11STodd Fiala } 2956af245d11STodd Fiala else 2957af245d11STodd Fiala { 2958af245d11STodd Fiala if (log) 2959af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s reusing %" PRIu64 " cached memory region entries", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); 2960af245d11STodd Fiala } 2961af245d11STodd Fiala 2962af245d11STodd Fiala lldb::addr_t prev_base_address = 0; 2963af245d11STodd Fiala 2964af245d11STodd Fiala // FIXME start by finding the last region that is <= target address using binary search. Data is sorted. 2965af245d11STodd Fiala // There can be a ton of regions on pthreads apps with lots of threads. 2966af245d11STodd Fiala for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end (); ++it) 2967af245d11STodd Fiala { 2968af245d11STodd Fiala MemoryRegionInfo &proc_entry_info = *it; 2969af245d11STodd Fiala 2970af245d11STodd Fiala // Sanity check assumption that /proc/{pid}/maps entries are ascending. 2971af245d11STodd Fiala assert ((proc_entry_info.GetRange ().GetRangeBase () >= prev_base_address) && "descending /proc/pid/maps entries detected, unexpected"); 2972af245d11STodd Fiala prev_base_address = proc_entry_info.GetRange ().GetRangeBase (); 2973af245d11STodd Fiala 2974af245d11STodd Fiala // If the target address comes before this entry, indicate distance to next region. 2975af245d11STodd Fiala if (load_addr < proc_entry_info.GetRange ().GetRangeBase ()) 2976af245d11STodd Fiala { 2977af245d11STodd Fiala range_info.GetRange ().SetRangeBase (load_addr); 2978af245d11STodd Fiala range_info.GetRange ().SetByteSize (proc_entry_info.GetRange ().GetRangeBase () - load_addr); 2979af245d11STodd Fiala range_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); 2980af245d11STodd Fiala range_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); 2981af245d11STodd Fiala range_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); 2982af245d11STodd Fiala 2983af245d11STodd Fiala return error; 2984af245d11STodd Fiala } 2985af245d11STodd Fiala else if (proc_entry_info.GetRange ().Contains (load_addr)) 2986af245d11STodd Fiala { 2987af245d11STodd Fiala // The target address is within the memory region we're processing here. 2988af245d11STodd Fiala range_info = proc_entry_info; 2989af245d11STodd Fiala return error; 2990af245d11STodd Fiala } 2991af245d11STodd Fiala 2992af245d11STodd Fiala // The target memory address comes somewhere after the region we just parsed. 2993af245d11STodd Fiala } 2994af245d11STodd Fiala 2995af245d11STodd Fiala // If we made it here, we didn't find an entry that contained the given address. 2996af245d11STodd Fiala error.SetErrorString ("address comes after final region"); 2997af245d11STodd Fiala 2998af245d11STodd Fiala if (log) 2999af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to find map entry for address 0x%" PRIx64 ": %s", __FUNCTION__, load_addr, error.AsCString ()); 3000af245d11STodd Fiala 3001af245d11STodd Fiala return error; 3002af245d11STodd Fiala } 3003af245d11STodd Fiala 3004af245d11STodd Fiala void 3005af245d11STodd Fiala NativeProcessLinux::DoStopIDBumped (uint32_t newBumpId) 3006af245d11STodd Fiala { 3007af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3008af245d11STodd Fiala if (log) 3009af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(newBumpId=%" PRIu32 ") called", __FUNCTION__, newBumpId); 3010af245d11STodd Fiala 3011af245d11STodd Fiala { 3012af245d11STodd Fiala Mutex::Locker locker (m_mem_region_cache_mutex); 3013af245d11STodd Fiala if (log) 3014af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s clearing %" PRIu64 " entries from the cache", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); 3015af245d11STodd Fiala m_mem_region_cache.clear (); 3016af245d11STodd Fiala } 3017af245d11STodd Fiala } 3018af245d11STodd Fiala 3019af245d11STodd Fiala Error 3020af245d11STodd Fiala NativeProcessLinux::AllocateMemory ( 3021af245d11STodd Fiala lldb::addr_t size, 3022af245d11STodd Fiala uint32_t permissions, 3023af245d11STodd Fiala lldb::addr_t &addr) 3024af245d11STodd Fiala { 3025af245d11STodd Fiala // FIXME implementing this requires the equivalent of 3026af245d11STodd Fiala // InferiorCallPOSIX::InferiorCallMmap, which depends on 3027af245d11STodd Fiala // functional ThreadPlans working with Native*Protocol. 3028af245d11STodd Fiala #if 1 3029af245d11STodd Fiala return Error ("not implemented yet"); 3030af245d11STodd Fiala #else 3031af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 3032af245d11STodd Fiala 3033af245d11STodd Fiala unsigned prot = 0; 3034af245d11STodd Fiala if (permissions & lldb::ePermissionsReadable) 3035af245d11STodd Fiala prot |= eMmapProtRead; 3036af245d11STodd Fiala if (permissions & lldb::ePermissionsWritable) 3037af245d11STodd Fiala prot |= eMmapProtWrite; 3038af245d11STodd Fiala if (permissions & lldb::ePermissionsExecutable) 3039af245d11STodd Fiala prot |= eMmapProtExec; 3040af245d11STodd Fiala 3041af245d11STodd Fiala // TODO implement this directly in NativeProcessLinux 3042af245d11STodd Fiala // (and lift to NativeProcessPOSIX if/when that class is 3043af245d11STodd Fiala // refactored out). 3044af245d11STodd Fiala if (InferiorCallMmap(this, addr, 0, size, prot, 3045af245d11STodd Fiala eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { 3046af245d11STodd Fiala m_addr_to_mmap_size[addr] = size; 3047af245d11STodd Fiala return Error (); 3048af245d11STodd Fiala } else { 3049af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 3050af245d11STodd Fiala return Error("unable to allocate %" PRIu64 " bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions)); 3051af245d11STodd Fiala } 3052af245d11STodd Fiala #endif 3053af245d11STodd Fiala } 3054af245d11STodd Fiala 3055af245d11STodd Fiala Error 3056af245d11STodd Fiala NativeProcessLinux::DeallocateMemory (lldb::addr_t addr) 3057af245d11STodd Fiala { 3058af245d11STodd Fiala // FIXME see comments in AllocateMemory - required lower-level 3059af245d11STodd Fiala // bits not in place yet (ThreadPlans) 3060af245d11STodd Fiala return Error ("not implemented"); 3061af245d11STodd Fiala } 3062af245d11STodd Fiala 3063af245d11STodd Fiala lldb::addr_t 3064af245d11STodd Fiala NativeProcessLinux::GetSharedLibraryInfoAddress () 3065af245d11STodd Fiala { 3066af245d11STodd Fiala #if 1 3067af245d11STodd Fiala // punt on this for now 3068af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3069af245d11STodd Fiala #else 3070af245d11STodd Fiala // Return the image info address for the exe module 3071af245d11STodd Fiala #if 1 3072af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3073af245d11STodd Fiala 3074af245d11STodd Fiala ModuleSP module_sp; 3075af245d11STodd Fiala Error error = GetExeModuleSP (module_sp); 3076af245d11STodd Fiala if (error.Fail ()) 3077af245d11STodd Fiala { 3078af245d11STodd Fiala if (log) 3079af245d11STodd Fiala log->Warning ("NativeProcessLinux::%s failed to retrieve exe module: %s", __FUNCTION__, error.AsCString ()); 3080af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3081af245d11STodd Fiala } 3082af245d11STodd Fiala 3083af245d11STodd Fiala if (module_sp == nullptr) 3084af245d11STodd Fiala { 3085af245d11STodd Fiala if (log) 3086af245d11STodd Fiala log->Warning ("NativeProcessLinux::%s exe module returned was NULL", __FUNCTION__); 3087af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3088af245d11STodd Fiala } 3089af245d11STodd Fiala 3090af245d11STodd Fiala ObjectFileSP object_file_sp = module_sp->GetObjectFile (); 3091af245d11STodd Fiala if (object_file_sp == nullptr) 3092af245d11STodd Fiala { 3093af245d11STodd Fiala if (log) 3094af245d11STodd Fiala log->Warning ("NativeProcessLinux::%s exe module returned a NULL object file", __FUNCTION__); 3095af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3096af245d11STodd Fiala } 3097af245d11STodd Fiala 3098af245d11STodd Fiala return obj_file_sp->GetImageInfoAddress(); 3099af245d11STodd Fiala #else 3100af245d11STodd Fiala Target *target = &GetTarget(); 3101af245d11STodd Fiala ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile(); 3102af245d11STodd Fiala Address addr = obj_file->GetImageInfoAddress(target); 3103af245d11STodd Fiala 3104af245d11STodd Fiala if (addr.IsValid()) 3105af245d11STodd Fiala return addr.GetLoadAddress(target); 3106af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 3107af245d11STodd Fiala #endif 3108af245d11STodd Fiala #endif // punt on this for now 3109af245d11STodd Fiala } 3110af245d11STodd Fiala 3111af245d11STodd Fiala size_t 3112af245d11STodd Fiala NativeProcessLinux::UpdateThreads () 3113af245d11STodd Fiala { 3114af245d11STodd Fiala // The NativeProcessLinux monitoring threads are always up to date 3115af245d11STodd Fiala // with respect to thread state and they keep the thread list 3116af245d11STodd Fiala // populated properly. All this method needs to do is return the 3117af245d11STodd Fiala // thread count. 3118af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 3119af245d11STodd Fiala return m_threads.size (); 3120af245d11STodd Fiala } 3121af245d11STodd Fiala 3122af245d11STodd Fiala bool 3123af245d11STodd Fiala NativeProcessLinux::GetArchitecture (ArchSpec &arch) const 3124af245d11STodd Fiala { 3125af245d11STodd Fiala arch = m_arch; 3126af245d11STodd Fiala return true; 3127af245d11STodd Fiala } 3128af245d11STodd Fiala 3129af245d11STodd Fiala Error 3130af245d11STodd Fiala NativeProcessLinux::GetSoftwareBreakpointSize (NativeRegisterContextSP context_sp, uint32_t &actual_opcode_size) 3131af245d11STodd Fiala { 3132af245d11STodd Fiala // FIXME put this behind a breakpoint protocol class that can be 3133af245d11STodd Fiala // set per architecture. Need ARM, MIPS support here. 31342afc5966STodd Fiala static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; 3135af245d11STodd Fiala static const uint8_t g_i386_opcode [] = { 0xCC }; 3136af245d11STodd Fiala 3137af245d11STodd Fiala switch (m_arch.GetMachine ()) 3138af245d11STodd Fiala { 31392afc5966STodd Fiala case llvm::Triple::aarch64: 31402afc5966STodd Fiala actual_opcode_size = static_cast<uint32_t> (sizeof(g_aarch64_opcode)); 31412afc5966STodd Fiala return Error (); 31422afc5966STodd Fiala 3143af245d11STodd Fiala case llvm::Triple::x86: 3144af245d11STodd Fiala case llvm::Triple::x86_64: 3145af245d11STodd Fiala actual_opcode_size = static_cast<uint32_t> (sizeof(g_i386_opcode)); 3146af245d11STodd Fiala return Error (); 3147af245d11STodd Fiala 3148af245d11STodd Fiala default: 3149af245d11STodd Fiala assert(false && "CPU type not supported!"); 3150af245d11STodd Fiala return Error ("CPU type not supported"); 3151af245d11STodd Fiala } 3152af245d11STodd Fiala } 3153af245d11STodd Fiala 3154af245d11STodd Fiala Error 3155af245d11STodd Fiala NativeProcessLinux::SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) 3156af245d11STodd Fiala { 3157af245d11STodd Fiala if (hardware) 3158af245d11STodd Fiala return Error ("NativeProcessLinux does not support hardware breakpoints"); 3159af245d11STodd Fiala else 3160af245d11STodd Fiala return SetSoftwareBreakpoint (addr, size); 3161af245d11STodd Fiala } 3162af245d11STodd Fiala 3163af245d11STodd Fiala Error 3164af245d11STodd Fiala NativeProcessLinux::GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) 3165af245d11STodd Fiala { 3166af245d11STodd Fiala // FIXME put this behind a breakpoint protocol class that can be 3167af245d11STodd Fiala // set per architecture. Need ARM, MIPS support here. 31682afc5966STodd Fiala static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; 3169af245d11STodd Fiala static const uint8_t g_i386_opcode [] = { 0xCC }; 3170af245d11STodd Fiala 3171af245d11STodd Fiala switch (m_arch.GetMachine ()) 3172af245d11STodd Fiala { 31732afc5966STodd Fiala case llvm::Triple::aarch64: 31742afc5966STodd Fiala trap_opcode_bytes = g_aarch64_opcode; 31752afc5966STodd Fiala actual_opcode_size = sizeof(g_aarch64_opcode); 31762afc5966STodd Fiala return Error (); 31772afc5966STodd Fiala 3178af245d11STodd Fiala case llvm::Triple::x86: 3179af245d11STodd Fiala case llvm::Triple::x86_64: 3180af245d11STodd Fiala trap_opcode_bytes = g_i386_opcode; 3181af245d11STodd Fiala actual_opcode_size = sizeof(g_i386_opcode); 3182af245d11STodd Fiala return Error (); 3183af245d11STodd Fiala 3184af245d11STodd Fiala default: 3185af245d11STodd Fiala assert(false && "CPU type not supported!"); 3186af245d11STodd Fiala return Error ("CPU type not supported"); 3187af245d11STodd Fiala } 3188af245d11STodd Fiala } 3189af245d11STodd Fiala 3190af245d11STodd Fiala #if 0 3191af245d11STodd Fiala ProcessMessage::CrashReason 3192af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info) 3193af245d11STodd Fiala { 3194af245d11STodd Fiala ProcessMessage::CrashReason reason; 3195af245d11STodd Fiala assert(info->si_signo == SIGSEGV); 3196af245d11STodd Fiala 3197af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 3198af245d11STodd Fiala 3199af245d11STodd Fiala switch (info->si_code) 3200af245d11STodd Fiala { 3201af245d11STodd Fiala default: 3202af245d11STodd Fiala assert(false && "unexpected si_code for SIGSEGV"); 3203af245d11STodd Fiala break; 3204af245d11STodd Fiala case SI_KERNEL: 3205af245d11STodd Fiala // Linux will occasionally send spurious SI_KERNEL codes. 3206af245d11STodd Fiala // (this is poorly documented in sigaction) 3207af245d11STodd Fiala // One way to get this is via unaligned SIMD loads. 3208af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; // for lack of anything better 3209af245d11STodd Fiala break; 3210af245d11STodd Fiala case SEGV_MAPERR: 3211af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; 3212af245d11STodd Fiala break; 3213af245d11STodd Fiala case SEGV_ACCERR: 3214af245d11STodd Fiala reason = ProcessMessage::ePrivilegedAddress; 3215af245d11STodd Fiala break; 3216af245d11STodd Fiala } 3217af245d11STodd Fiala 3218af245d11STodd Fiala return reason; 3219af245d11STodd Fiala } 3220af245d11STodd Fiala #endif 3221af245d11STodd Fiala 3222af245d11STodd Fiala 3223af245d11STodd Fiala #if 0 3224af245d11STodd Fiala ProcessMessage::CrashReason 3225af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info) 3226af245d11STodd Fiala { 3227af245d11STodd Fiala ProcessMessage::CrashReason reason; 3228af245d11STodd Fiala assert(info->si_signo == SIGILL); 3229af245d11STodd Fiala 3230af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 3231af245d11STodd Fiala 3232af245d11STodd Fiala switch (info->si_code) 3233af245d11STodd Fiala { 3234af245d11STodd Fiala default: 3235af245d11STodd Fiala assert(false && "unexpected si_code for SIGILL"); 3236af245d11STodd Fiala break; 3237af245d11STodd Fiala case ILL_ILLOPC: 3238af245d11STodd Fiala reason = ProcessMessage::eIllegalOpcode; 3239af245d11STodd Fiala break; 3240af245d11STodd Fiala case ILL_ILLOPN: 3241af245d11STodd Fiala reason = ProcessMessage::eIllegalOperand; 3242af245d11STodd Fiala break; 3243af245d11STodd Fiala case ILL_ILLADR: 3244af245d11STodd Fiala reason = ProcessMessage::eIllegalAddressingMode; 3245af245d11STodd Fiala break; 3246af245d11STodd Fiala case ILL_ILLTRP: 3247af245d11STodd Fiala reason = ProcessMessage::eIllegalTrap; 3248af245d11STodd Fiala break; 3249af245d11STodd Fiala case ILL_PRVOPC: 3250af245d11STodd Fiala reason = ProcessMessage::ePrivilegedOpcode; 3251af245d11STodd Fiala break; 3252af245d11STodd Fiala case ILL_PRVREG: 3253af245d11STodd Fiala reason = ProcessMessage::ePrivilegedRegister; 3254af245d11STodd Fiala break; 3255af245d11STodd Fiala case ILL_COPROC: 3256af245d11STodd Fiala reason = ProcessMessage::eCoprocessorError; 3257af245d11STodd Fiala break; 3258af245d11STodd Fiala case ILL_BADSTK: 3259af245d11STodd Fiala reason = ProcessMessage::eInternalStackError; 3260af245d11STodd Fiala break; 3261af245d11STodd Fiala } 3262af245d11STodd Fiala 3263af245d11STodd Fiala return reason; 3264af245d11STodd Fiala } 3265af245d11STodd Fiala #endif 3266af245d11STodd Fiala 3267af245d11STodd Fiala #if 0 3268af245d11STodd Fiala ProcessMessage::CrashReason 3269af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info) 3270af245d11STodd Fiala { 3271af245d11STodd Fiala ProcessMessage::CrashReason reason; 3272af245d11STodd Fiala assert(info->si_signo == SIGFPE); 3273af245d11STodd Fiala 3274af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 3275af245d11STodd Fiala 3276af245d11STodd Fiala switch (info->si_code) 3277af245d11STodd Fiala { 3278af245d11STodd Fiala default: 3279af245d11STodd Fiala assert(false && "unexpected si_code for SIGFPE"); 3280af245d11STodd Fiala break; 3281af245d11STodd Fiala case FPE_INTDIV: 3282af245d11STodd Fiala reason = ProcessMessage::eIntegerDivideByZero; 3283af245d11STodd Fiala break; 3284af245d11STodd Fiala case FPE_INTOVF: 3285af245d11STodd Fiala reason = ProcessMessage::eIntegerOverflow; 3286af245d11STodd Fiala break; 3287af245d11STodd Fiala case FPE_FLTDIV: 3288af245d11STodd Fiala reason = ProcessMessage::eFloatDivideByZero; 3289af245d11STodd Fiala break; 3290af245d11STodd Fiala case FPE_FLTOVF: 3291af245d11STodd Fiala reason = ProcessMessage::eFloatOverflow; 3292af245d11STodd Fiala break; 3293af245d11STodd Fiala case FPE_FLTUND: 3294af245d11STodd Fiala reason = ProcessMessage::eFloatUnderflow; 3295af245d11STodd Fiala break; 3296af245d11STodd Fiala case FPE_FLTRES: 3297af245d11STodd Fiala reason = ProcessMessage::eFloatInexactResult; 3298af245d11STodd Fiala break; 3299af245d11STodd Fiala case FPE_FLTINV: 3300af245d11STodd Fiala reason = ProcessMessage::eFloatInvalidOperation; 3301af245d11STodd Fiala break; 3302af245d11STodd Fiala case FPE_FLTSUB: 3303af245d11STodd Fiala reason = ProcessMessage::eFloatSubscriptRange; 3304af245d11STodd Fiala break; 3305af245d11STodd Fiala } 3306af245d11STodd Fiala 3307af245d11STodd Fiala return reason; 3308af245d11STodd Fiala } 3309af245d11STodd Fiala #endif 3310af245d11STodd Fiala 3311af245d11STodd Fiala #if 0 3312af245d11STodd Fiala ProcessMessage::CrashReason 3313af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info) 3314af245d11STodd Fiala { 3315af245d11STodd Fiala ProcessMessage::CrashReason reason; 3316af245d11STodd Fiala assert(info->si_signo == SIGBUS); 3317af245d11STodd Fiala 3318af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 3319af245d11STodd Fiala 3320af245d11STodd Fiala switch (info->si_code) 3321af245d11STodd Fiala { 3322af245d11STodd Fiala default: 3323af245d11STodd Fiala assert(false && "unexpected si_code for SIGBUS"); 3324af245d11STodd Fiala break; 3325af245d11STodd Fiala case BUS_ADRALN: 3326af245d11STodd Fiala reason = ProcessMessage::eIllegalAlignment; 3327af245d11STodd Fiala break; 3328af245d11STodd Fiala case BUS_ADRERR: 3329af245d11STodd Fiala reason = ProcessMessage::eIllegalAddress; 3330af245d11STodd Fiala break; 3331af245d11STodd Fiala case BUS_OBJERR: 3332af245d11STodd Fiala reason = ProcessMessage::eHardwareError; 3333af245d11STodd Fiala break; 3334af245d11STodd Fiala } 3335af245d11STodd Fiala 3336af245d11STodd Fiala return reason; 3337af245d11STodd Fiala } 3338af245d11STodd Fiala #endif 3339af245d11STodd Fiala 3340af245d11STodd Fiala void 3341af245d11STodd Fiala NativeProcessLinux::ServeOperation(OperationArgs *args) 3342af245d11STodd Fiala { 3343af245d11STodd Fiala NativeProcessLinux *monitor = args->m_monitor; 3344af245d11STodd Fiala 3345af245d11STodd Fiala // We are finised with the arguments and are ready to go. Sync with the 3346af245d11STodd Fiala // parent thread and start serving operations on the inferior. 3347af245d11STodd Fiala sem_post(&args->m_semaphore); 3348af245d11STodd Fiala 3349af245d11STodd Fiala for(;;) 3350af245d11STodd Fiala { 3351af245d11STodd Fiala // wait for next pending operation 3352af245d11STodd Fiala if (sem_wait(&monitor->m_operation_pending)) 3353af245d11STodd Fiala { 3354af245d11STodd Fiala if (errno == EINTR) 3355af245d11STodd Fiala continue; 3356af245d11STodd Fiala assert(false && "Unexpected errno from sem_wait"); 3357af245d11STodd Fiala } 3358af245d11STodd Fiala 3359af245d11STodd Fiala reinterpret_cast<Operation*>(monitor->m_operation)->Execute(monitor); 3360af245d11STodd Fiala 3361af245d11STodd Fiala // notify calling thread that operation is complete 3362af245d11STodd Fiala sem_post(&monitor->m_operation_done); 3363af245d11STodd Fiala } 3364af245d11STodd Fiala } 3365af245d11STodd Fiala 3366af245d11STodd Fiala void 3367af245d11STodd Fiala NativeProcessLinux::DoOperation(void *op) 3368af245d11STodd Fiala { 3369af245d11STodd Fiala Mutex::Locker lock(m_operation_mutex); 3370af245d11STodd Fiala 3371af245d11STodd Fiala m_operation = op; 3372af245d11STodd Fiala 3373af245d11STodd Fiala // notify operation thread that an operation is ready to be processed 3374af245d11STodd Fiala sem_post(&m_operation_pending); 3375af245d11STodd Fiala 3376af245d11STodd Fiala // wait for operation to complete 3377af245d11STodd Fiala while (sem_wait(&m_operation_done)) 3378af245d11STodd Fiala { 3379af245d11STodd Fiala if (errno == EINTR) 3380af245d11STodd Fiala continue; 3381af245d11STodd Fiala assert(false && "Unexpected errno from sem_wait"); 3382af245d11STodd Fiala } 3383af245d11STodd Fiala } 3384af245d11STodd Fiala 3385af245d11STodd Fiala Error 3386af245d11STodd Fiala NativeProcessLinux::ReadMemory (lldb::addr_t addr, void *buf, lldb::addr_t size, lldb::addr_t &bytes_read) 3387af245d11STodd Fiala { 3388af245d11STodd Fiala ReadOperation op(addr, buf, size, bytes_read); 3389af245d11STodd Fiala DoOperation(&op); 3390af245d11STodd Fiala return op.GetError (); 3391af245d11STodd Fiala } 3392af245d11STodd Fiala 3393af245d11STodd Fiala Error 3394af245d11STodd Fiala NativeProcessLinux::WriteMemory (lldb::addr_t addr, const void *buf, lldb::addr_t size, lldb::addr_t &bytes_written) 3395af245d11STodd Fiala { 3396af245d11STodd Fiala WriteOperation op(addr, buf, size, bytes_written); 3397af245d11STodd Fiala DoOperation(&op); 3398af245d11STodd Fiala return op.GetError (); 3399af245d11STodd Fiala } 3400af245d11STodd Fiala 3401af245d11STodd Fiala bool 3402af245d11STodd Fiala NativeProcessLinux::ReadRegisterValue(lldb::tid_t tid, uint32_t offset, const char* reg_name, 3403af245d11STodd Fiala uint32_t size, RegisterValue &value) 3404af245d11STodd Fiala { 3405af245d11STodd Fiala bool result; 3406af245d11STodd Fiala ReadRegOperation op(tid, offset, reg_name, value, result); 3407af245d11STodd Fiala DoOperation(&op); 3408af245d11STodd Fiala return result; 3409af245d11STodd Fiala } 3410af245d11STodd Fiala 3411af245d11STodd Fiala bool 3412af245d11STodd Fiala NativeProcessLinux::WriteRegisterValue(lldb::tid_t tid, unsigned offset, 3413af245d11STodd Fiala const char* reg_name, const RegisterValue &value) 3414af245d11STodd Fiala { 3415af245d11STodd Fiala bool result; 3416af245d11STodd Fiala WriteRegOperation op(tid, offset, reg_name, value, result); 3417af245d11STodd Fiala DoOperation(&op); 3418af245d11STodd Fiala return result; 3419af245d11STodd Fiala } 3420af245d11STodd Fiala 3421af245d11STodd Fiala bool 3422af245d11STodd Fiala NativeProcessLinux::ReadGPR(lldb::tid_t tid, void *buf, size_t buf_size) 3423af245d11STodd Fiala { 3424af245d11STodd Fiala bool result; 3425af245d11STodd Fiala ReadGPROperation op(tid, buf, buf_size, result); 3426af245d11STodd Fiala DoOperation(&op); 3427af245d11STodd Fiala return result; 3428af245d11STodd Fiala } 3429af245d11STodd Fiala 3430af245d11STodd Fiala bool 3431af245d11STodd Fiala NativeProcessLinux::ReadFPR(lldb::tid_t tid, void *buf, size_t buf_size) 3432af245d11STodd Fiala { 3433af245d11STodd Fiala bool result; 3434af245d11STodd Fiala ReadFPROperation op(tid, buf, buf_size, result); 3435af245d11STodd Fiala DoOperation(&op); 3436af245d11STodd Fiala return result; 3437af245d11STodd Fiala } 3438af245d11STodd Fiala 3439af245d11STodd Fiala bool 3440af245d11STodd Fiala NativeProcessLinux::ReadRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) 3441af245d11STodd Fiala { 3442af245d11STodd Fiala bool result; 3443af245d11STodd Fiala ReadRegisterSetOperation op(tid, buf, buf_size, regset, result); 3444af245d11STodd Fiala DoOperation(&op); 3445af245d11STodd Fiala return result; 3446af245d11STodd Fiala } 3447af245d11STodd Fiala 3448af245d11STodd Fiala bool 3449af245d11STodd Fiala NativeProcessLinux::WriteGPR(lldb::tid_t tid, void *buf, size_t buf_size) 3450af245d11STodd Fiala { 3451af245d11STodd Fiala bool result; 3452af245d11STodd Fiala WriteGPROperation op(tid, buf, buf_size, result); 3453af245d11STodd Fiala DoOperation(&op); 3454af245d11STodd Fiala return result; 3455af245d11STodd Fiala } 3456af245d11STodd Fiala 3457af245d11STodd Fiala bool 3458af245d11STodd Fiala NativeProcessLinux::WriteFPR(lldb::tid_t tid, void *buf, size_t buf_size) 3459af245d11STodd Fiala { 3460af245d11STodd Fiala bool result; 3461af245d11STodd Fiala WriteFPROperation op(tid, buf, buf_size, result); 3462af245d11STodd Fiala DoOperation(&op); 3463af245d11STodd Fiala return result; 3464af245d11STodd Fiala } 3465af245d11STodd Fiala 3466af245d11STodd Fiala bool 3467af245d11STodd Fiala NativeProcessLinux::WriteRegisterSet(lldb::tid_t tid, void *buf, size_t buf_size, unsigned int regset) 3468af245d11STodd Fiala { 3469af245d11STodd Fiala bool result; 3470af245d11STodd Fiala WriteRegisterSetOperation op(tid, buf, buf_size, regset, result); 3471af245d11STodd Fiala DoOperation(&op); 3472af245d11STodd Fiala return result; 3473af245d11STodd Fiala } 3474af245d11STodd Fiala 3475af245d11STodd Fiala bool 3476af245d11STodd Fiala NativeProcessLinux::Resume (lldb::tid_t tid, uint32_t signo) 3477af245d11STodd Fiala { 3478af245d11STodd Fiala bool result; 3479af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 3480af245d11STodd Fiala 3481af245d11STodd Fiala if (log) 3482af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() resuming thread = %" PRIu64 " with signal %s", __FUNCTION__, tid, 3483af245d11STodd Fiala GetUnixSignals().GetSignalAsCString (signo)); 3484af245d11STodd Fiala ResumeOperation op (tid, signo, result); 3485af245d11STodd Fiala DoOperation (&op); 3486af245d11STodd Fiala if (log) 3487af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() resuming result = %s", __FUNCTION__, result ? "true" : "false"); 3488af245d11STodd Fiala return result; 3489af245d11STodd Fiala } 3490af245d11STodd Fiala 3491af245d11STodd Fiala bool 3492af245d11STodd Fiala NativeProcessLinux::SingleStep(lldb::tid_t tid, uint32_t signo) 3493af245d11STodd Fiala { 3494af245d11STodd Fiala bool result; 3495af245d11STodd Fiala SingleStepOperation op(tid, signo, result); 3496af245d11STodd Fiala DoOperation(&op); 3497af245d11STodd Fiala return result; 3498af245d11STodd Fiala } 3499af245d11STodd Fiala 3500af245d11STodd Fiala bool 3501af245d11STodd Fiala NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo, int &ptrace_err) 3502af245d11STodd Fiala { 3503af245d11STodd Fiala bool result; 3504af245d11STodd Fiala SiginfoOperation op(tid, siginfo, result, ptrace_err); 3505af245d11STodd Fiala DoOperation(&op); 3506af245d11STodd Fiala return result; 3507af245d11STodd Fiala } 3508af245d11STodd Fiala 3509af245d11STodd Fiala bool 3510af245d11STodd Fiala NativeProcessLinux::GetEventMessage(lldb::tid_t tid, unsigned long *message) 3511af245d11STodd Fiala { 3512af245d11STodd Fiala bool result; 3513af245d11STodd Fiala EventMessageOperation op(tid, message, result); 3514af245d11STodd Fiala DoOperation(&op); 3515af245d11STodd Fiala return result; 3516af245d11STodd Fiala } 3517af245d11STodd Fiala 3518af245d11STodd Fiala lldb_private::Error 3519af245d11STodd Fiala NativeProcessLinux::Detach(lldb::tid_t tid) 3520af245d11STodd Fiala { 3521af245d11STodd Fiala lldb_private::Error error; 3522af245d11STodd Fiala if (tid != LLDB_INVALID_THREAD_ID) 3523af245d11STodd Fiala { 3524af245d11STodd Fiala DetachOperation op(tid, error); 3525af245d11STodd Fiala DoOperation(&op); 3526af245d11STodd Fiala } 3527af245d11STodd Fiala return error; 3528af245d11STodd Fiala } 3529af245d11STodd Fiala 3530af245d11STodd Fiala bool 3531af245d11STodd Fiala NativeProcessLinux::DupDescriptor(const char *path, int fd, int flags) 3532af245d11STodd Fiala { 3533af245d11STodd Fiala int target_fd = open(path, flags, 0666); 3534af245d11STodd Fiala 3535af245d11STodd Fiala if (target_fd == -1) 3536af245d11STodd Fiala return false; 3537af245d11STodd Fiala 3538af245d11STodd Fiala return (dup2(target_fd, fd) == -1) ? false : true; 3539af245d11STodd Fiala } 3540af245d11STodd Fiala 3541af245d11STodd Fiala void 3542af245d11STodd Fiala NativeProcessLinux::StopMonitoringChildProcess() 3543af245d11STodd Fiala { 354439de3110SZachary Turner if (m_monitor_thread.GetState() == eThreadStateRunning) 3545af245d11STodd Fiala { 354639de3110SZachary Turner m_monitor_thread.Cancel(); 354739de3110SZachary Turner m_monitor_thread.Join(nullptr); 354839de3110SZachary Turner m_monitor_thread.Reset(); 3549af245d11STodd Fiala } 3550af245d11STodd Fiala } 3551af245d11STodd Fiala 3552af245d11STodd Fiala void 3553af245d11STodd Fiala NativeProcessLinux::StopMonitor() 3554af245d11STodd Fiala { 3555af245d11STodd Fiala StopMonitoringChildProcess(); 3556af245d11STodd Fiala StopOpThread(); 3557af245d11STodd Fiala sem_destroy(&m_operation_pending); 3558af245d11STodd Fiala sem_destroy(&m_operation_done); 3559af245d11STodd Fiala 3560af245d11STodd Fiala // TODO: validate whether this still holds, fix up comment. 3561af245d11STodd Fiala // Note: ProcessPOSIX passes the m_terminal_fd file descriptor to 3562af245d11STodd Fiala // Process::SetSTDIOFileDescriptor, which in turn transfers ownership of 3563af245d11STodd Fiala // the descriptor to a ConnectionFileDescriptor object. Consequently 3564af245d11STodd Fiala // even though still has the file descriptor, we shouldn't close it here. 3565af245d11STodd Fiala } 3566af245d11STodd Fiala 3567af245d11STodd Fiala void 3568af245d11STodd Fiala NativeProcessLinux::StopOpThread() 3569af245d11STodd Fiala { 357039de3110SZachary Turner if (m_operation_thread.GetState() != eThreadStateRunning) 3571af245d11STodd Fiala return; 3572af245d11STodd Fiala 357339de3110SZachary Turner m_operation_thread.Cancel(); 357439de3110SZachary Turner m_operation_thread.Join(nullptr); 357539de3110SZachary Turner m_operation_thread.Reset(); 3576af245d11STodd Fiala } 3577af245d11STodd Fiala 3578af245d11STodd Fiala bool 3579af245d11STodd Fiala NativeProcessLinux::HasThreadNoLock (lldb::tid_t thread_id) 3580af245d11STodd Fiala { 3581af245d11STodd Fiala for (auto thread_sp : m_threads) 3582af245d11STodd Fiala { 3583af245d11STodd Fiala assert (thread_sp && "thread list should not contain NULL threads"); 3584af245d11STodd Fiala if (thread_sp->GetID () == thread_id) 3585af245d11STodd Fiala { 3586af245d11STodd Fiala // We have this thread. 3587af245d11STodd Fiala return true; 3588af245d11STodd Fiala } 3589af245d11STodd Fiala } 3590af245d11STodd Fiala 3591af245d11STodd Fiala // We don't have this thread. 3592af245d11STodd Fiala return false; 3593af245d11STodd Fiala } 3594af245d11STodd Fiala 3595af245d11STodd Fiala NativeThreadProtocolSP 3596af245d11STodd Fiala NativeProcessLinux::MaybeGetThreadNoLock (lldb::tid_t thread_id) 3597af245d11STodd Fiala { 3598af245d11STodd Fiala // CONSIDER organize threads by map - we can do better than linear. 3599af245d11STodd Fiala for (auto thread_sp : m_threads) 3600af245d11STodd Fiala { 3601af245d11STodd Fiala if (thread_sp->GetID () == thread_id) 3602af245d11STodd Fiala return thread_sp; 3603af245d11STodd Fiala } 3604af245d11STodd Fiala 3605af245d11STodd Fiala // We don't have this thread. 3606af245d11STodd Fiala return NativeThreadProtocolSP (); 3607af245d11STodd Fiala } 3608af245d11STodd Fiala 3609af245d11STodd Fiala bool 3610af245d11STodd Fiala NativeProcessLinux::StopTrackingThread (lldb::tid_t thread_id) 3611af245d11STodd Fiala { 3612af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 3613af245d11STodd Fiala for (auto it = m_threads.begin (); it != m_threads.end (); ++it) 3614af245d11STodd Fiala { 3615af245d11STodd Fiala if (*it && ((*it)->GetID () == thread_id)) 3616af245d11STodd Fiala { 3617af245d11STodd Fiala m_threads.erase (it); 3618af245d11STodd Fiala return true; 3619af245d11STodd Fiala } 3620af245d11STodd Fiala } 3621af245d11STodd Fiala 3622af245d11STodd Fiala // Didn't find it. 3623af245d11STodd Fiala return false; 3624af245d11STodd Fiala } 3625af245d11STodd Fiala 3626af245d11STodd Fiala NativeThreadProtocolSP 3627af245d11STodd Fiala NativeProcessLinux::AddThread (lldb::tid_t thread_id) 3628af245d11STodd Fiala { 3629af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 3630af245d11STodd Fiala 3631af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 3632af245d11STodd Fiala 3633af245d11STodd Fiala if (log) 3634af245d11STodd Fiala { 3635af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " adding thread with tid %" PRIu64, 3636af245d11STodd Fiala __FUNCTION__, 3637af245d11STodd Fiala GetID (), 3638af245d11STodd Fiala thread_id); 3639af245d11STodd Fiala } 3640af245d11STodd Fiala 3641af245d11STodd Fiala assert (!HasThreadNoLock (thread_id) && "attempted to add a thread by id that already exists"); 3642af245d11STodd Fiala 3643af245d11STodd Fiala // If this is the first thread, save it as the current thread 3644af245d11STodd Fiala if (m_threads.empty ()) 3645af245d11STodd Fiala SetCurrentThreadID (thread_id); 3646af245d11STodd Fiala 3647af245d11STodd Fiala NativeThreadProtocolSP thread_sp (new NativeThreadLinux (this, thread_id)); 3648af245d11STodd Fiala m_threads.push_back (thread_sp); 3649af245d11STodd Fiala 3650af245d11STodd Fiala return thread_sp; 3651af245d11STodd Fiala } 3652af245d11STodd Fiala 3653af245d11STodd Fiala NativeThreadProtocolSP 3654af245d11STodd Fiala NativeProcessLinux::GetOrCreateThread (lldb::tid_t thread_id, bool &created) 3655af245d11STodd Fiala { 3656af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 3657af245d11STodd Fiala 3658af245d11STodd Fiala Mutex::Locker locker (m_threads_mutex); 3659af245d11STodd Fiala if (log) 3660af245d11STodd Fiala { 3661af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " get/create thread with tid %" PRIu64, 3662af245d11STodd Fiala __FUNCTION__, 3663af245d11STodd Fiala GetID (), 3664af245d11STodd Fiala thread_id); 3665af245d11STodd Fiala } 3666af245d11STodd Fiala 3667af245d11STodd Fiala // Retrieve the thread if it is already getting tracked. 3668af245d11STodd Fiala NativeThreadProtocolSP thread_sp = MaybeGetThreadNoLock (thread_id); 3669af245d11STodd Fiala if (thread_sp) 3670af245d11STodd Fiala { 3671af245d11STodd Fiala if (log) 3672af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": thread already tracked, returning", 3673af245d11STodd Fiala __FUNCTION__, 3674af245d11STodd Fiala GetID (), 3675af245d11STodd Fiala thread_id); 3676af245d11STodd Fiala created = false; 3677af245d11STodd Fiala return thread_sp; 3678af245d11STodd Fiala 3679af245d11STodd Fiala } 3680af245d11STodd Fiala 3681af245d11STodd Fiala // Create the thread metadata since it isn't being tracked. 3682af245d11STodd Fiala if (log) 3683af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": thread didn't exist, tracking now", 3684af245d11STodd Fiala __FUNCTION__, 3685af245d11STodd Fiala GetID (), 3686af245d11STodd Fiala thread_id); 3687af245d11STodd Fiala 3688af245d11STodd Fiala thread_sp.reset (new NativeThreadLinux (this, thread_id)); 3689af245d11STodd Fiala m_threads.push_back (thread_sp); 3690af245d11STodd Fiala created = true; 3691af245d11STodd Fiala 3692af245d11STodd Fiala return thread_sp; 3693af245d11STodd Fiala } 3694af245d11STodd Fiala 3695af245d11STodd Fiala Error 3696af245d11STodd Fiala NativeProcessLinux::FixupBreakpointPCAsNeeded (NativeThreadProtocolSP &thread_sp) 3697af245d11STodd Fiala { 3698af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 3699af245d11STodd Fiala 3700af245d11STodd Fiala Error error; 3701af245d11STodd Fiala 3702af245d11STodd Fiala // Get a linux thread pointer. 3703af245d11STodd Fiala if (!thread_sp) 3704af245d11STodd Fiala { 3705af245d11STodd Fiala error.SetErrorString ("null thread_sp"); 3706af245d11STodd Fiala if (log) 3707af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); 3708af245d11STodd Fiala return error; 3709af245d11STodd Fiala } 3710af245d11STodd Fiala NativeThreadLinux *const linux_thread_p = reinterpret_cast<NativeThreadLinux*> (thread_sp.get()); 3711af245d11STodd Fiala 3712af245d11STodd Fiala // Find out the size of a breakpoint (might depend on where we are in the code). 3713af245d11STodd Fiala NativeRegisterContextSP context_sp = linux_thread_p->GetRegisterContext (); 3714af245d11STodd Fiala if (!context_sp) 3715af245d11STodd Fiala { 3716af245d11STodd Fiala error.SetErrorString ("cannot get a NativeRegisterContext for the thread"); 3717af245d11STodd Fiala if (log) 3718af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); 3719af245d11STodd Fiala return error; 3720af245d11STodd Fiala } 3721af245d11STodd Fiala 3722af245d11STodd Fiala uint32_t breakpoint_size = 0; 3723af245d11STodd Fiala error = GetSoftwareBreakpointSize (context_sp, breakpoint_size); 3724af245d11STodd Fiala if (error.Fail ()) 3725af245d11STodd Fiala { 3726af245d11STodd Fiala if (log) 3727af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s GetBreakpointSize() failed: %s", __FUNCTION__, error.AsCString ()); 3728af245d11STodd Fiala return error; 3729af245d11STodd Fiala } 3730af245d11STodd Fiala else 3731af245d11STodd Fiala { 3732af245d11STodd Fiala if (log) 3733af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s breakpoint size: %" PRIu32, __FUNCTION__, breakpoint_size); 3734af245d11STodd Fiala } 3735af245d11STodd Fiala 3736af245d11STodd Fiala // First try probing for a breakpoint at a software breakpoint location: PC - breakpoint size. 3737af245d11STodd Fiala const lldb::addr_t initial_pc_addr = context_sp->GetPC (); 3738af245d11STodd Fiala lldb::addr_t breakpoint_addr = initial_pc_addr; 3739af245d11STodd Fiala if (breakpoint_size > static_cast<lldb::addr_t> (0)) 3740af245d11STodd Fiala { 3741af245d11STodd Fiala // Do not allow breakpoint probe to wrap around. 3742af245d11STodd Fiala if (breakpoint_addr >= static_cast<lldb::addr_t> (breakpoint_size)) 3743af245d11STodd Fiala breakpoint_addr -= static_cast<lldb::addr_t> (breakpoint_size); 3744af245d11STodd Fiala } 3745af245d11STodd Fiala 3746af245d11STodd Fiala // Check if we stopped because of a breakpoint. 3747af245d11STodd Fiala NativeBreakpointSP breakpoint_sp; 3748af245d11STodd Fiala error = m_breakpoint_list.GetBreakpoint (breakpoint_addr, breakpoint_sp); 3749af245d11STodd Fiala if (!error.Success () || !breakpoint_sp) 3750af245d11STodd Fiala { 3751af245d11STodd Fiala // We didn't find one at a software probe location. Nothing to do. 3752af245d11STodd Fiala if (log) 3753af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " no lldb breakpoint found at current pc with adjustment: 0x%" PRIx64, __FUNCTION__, GetID (), breakpoint_addr); 3754af245d11STodd Fiala return Error (); 3755af245d11STodd Fiala } 3756af245d11STodd Fiala 3757af245d11STodd Fiala // If the breakpoint is not a software breakpoint, nothing to do. 3758af245d11STodd Fiala if (!breakpoint_sp->IsSoftwareBreakpoint ()) 3759af245d11STodd Fiala { 3760af245d11STodd Fiala if (log) 3761af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " breakpoint found at 0x%" PRIx64 ", not software, nothing to adjust", __FUNCTION__, GetID (), breakpoint_addr); 3762af245d11STodd Fiala return Error (); 3763af245d11STodd Fiala } 3764af245d11STodd Fiala 3765af245d11STodd Fiala // 3766af245d11STodd Fiala // We have a software breakpoint and need to adjust the PC. 3767af245d11STodd Fiala // 3768af245d11STodd Fiala 3769af245d11STodd Fiala // Sanity check. 3770af245d11STodd Fiala if (breakpoint_size == 0) 3771af245d11STodd Fiala { 3772af245d11STodd Fiala // Nothing to do! How did we get here? 3773af245d11STodd Fiala if (log) 3774af245d11STodd 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); 3775af245d11STodd Fiala return Error (); 3776af245d11STodd Fiala } 3777af245d11STodd Fiala 3778af245d11STodd Fiala // Change the program counter. 3779af245d11STodd Fiala if (log) 3780af245d11STodd 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); 3781af245d11STodd Fiala 3782af245d11STodd Fiala error = context_sp->SetPC (breakpoint_addr); 3783af245d11STodd Fiala if (error.Fail ()) 3784af245d11STodd Fiala { 3785af245d11STodd Fiala if (log) 3786af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": failed to set PC: %s", __FUNCTION__, GetID (), linux_thread_p->GetID (), error.AsCString ()); 3787af245d11STodd Fiala return error; 3788af245d11STodd Fiala } 3789af245d11STodd Fiala 3790af245d11STodd Fiala return error; 3791af245d11STodd Fiala } 3792