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 "NativeProcessLinux.h" 11af245d11STodd Fiala 12af245d11STodd Fiala // C Includes 13af245d11STodd Fiala #include <errno.h> 14af245d11STodd Fiala #include <string.h> 15af245d11STodd Fiala #include <stdint.h> 16af245d11STodd Fiala #include <unistd.h> 17af245d11STodd Fiala 18af245d11STodd Fiala // C++ Includes 19af245d11STodd Fiala #include <fstream> 20df7c6995SPavel Labath #include <mutex> 21c076559aSPavel Labath #include <sstream> 22af245d11STodd Fiala #include <string> 235b981ab9SPavel Labath #include <unordered_map> 24af245d11STodd Fiala 25af245d11STodd Fiala // Other libraries and framework includes 26d8c338d4STamas Berghammer #include "lldb/Core/EmulateInstruction.h" 27af245d11STodd Fiala #include "lldb/Core/Error.h" 286edef204SOleksiy Vyalov #include "lldb/Core/ModuleSpec.h" 29af245d11STodd Fiala #include "lldb/Core/RegisterValue.h" 30af245d11STodd Fiala #include "lldb/Core/State.h" 31af245d11STodd Fiala #include "lldb/Host/Host.h" 3239de3110SZachary Turner #include "lldb/Host/ThreadLauncher.h" 33*2a86b555SPavel Labath #include "lldb/Host/common/NativeBreakpoint.h" 34*2a86b555SPavel Labath #include "lldb/Host/common/NativeRegisterContext.h" 35*2a86b555SPavel Labath #include "lldb/Symbol/ObjectFile.h" 3690aff47cSZachary Turner #include "lldb/Target/Process.h" 37af245d11STodd Fiala #include "lldb/Target/ProcessLaunchInfo.h" 385b981ab9SPavel Labath #include "lldb/Target/Target.h" 39c16f5dcaSChaoren Lin #include "lldb/Utility/LLDBAssert.h" 40af245d11STodd Fiala #include "lldb/Utility/PseudoTerminal.h" 41f805e190SPavel Labath #include "lldb/Utility/StringExtractor.h" 42af245d11STodd Fiala 431e209fccSTamas Berghammer #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" 44af245d11STodd Fiala #include "NativeThreadLinux.h" 45af245d11STodd Fiala #include "ProcFileReader.h" 461e209fccSTamas Berghammer #include "Procfs.h" 47cacde7dfSTodd Fiala 48d858487eSTamas Berghammer // System includes - They have to be included after framework includes because they define some 49d858487eSTamas Berghammer // macros which collide with variable names in other modules 50d858487eSTamas Berghammer #include <linux/unistd.h> 51d858487eSTamas Berghammer #include <sys/socket.h> 528b335671SVince Harron 53df7c6995SPavel Labath #include <sys/syscall.h> 54d858487eSTamas Berghammer #include <sys/types.h> 55d858487eSTamas Berghammer #include <sys/user.h> 56d858487eSTamas Berghammer #include <sys/wait.h> 57d858487eSTamas Berghammer 588b335671SVince Harron #include "lldb/Host/linux/Personality.h" 598b335671SVince Harron #include "lldb/Host/linux/Ptrace.h" 60df7c6995SPavel Labath #include "lldb/Host/linux/Uio.h" 618b335671SVince Harron #include "lldb/Host/android/Android.h" 62af245d11STodd Fiala 630bce1b67STodd Fiala #define LLDB_PERSONALITY_GET_CURRENT_SETTINGS 0xffffffff 64af245d11STodd Fiala 65af245d11STodd Fiala // Support hardware breakpoints in case it has not been defined 66af245d11STodd Fiala #ifndef TRAP_HWBKPT 67af245d11STodd Fiala #define TRAP_HWBKPT 4 68af245d11STodd Fiala #endif 69af245d11STodd Fiala 707cb18bf5STamas Berghammer using namespace lldb; 717cb18bf5STamas Berghammer using namespace lldb_private; 72db264a6dSTamas Berghammer using namespace lldb_private::process_linux; 737cb18bf5STamas Berghammer using namespace llvm; 747cb18bf5STamas Berghammer 75af245d11STodd Fiala // Private bits we only need internally. 76df7c6995SPavel Labath 77df7c6995SPavel Labath static bool ProcessVmReadvSupported() 78df7c6995SPavel Labath { 79df7c6995SPavel Labath static bool is_supported; 80df7c6995SPavel Labath static std::once_flag flag; 81df7c6995SPavel Labath 82df7c6995SPavel Labath std::call_once(flag, [] { 83df7c6995SPavel Labath Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 84df7c6995SPavel Labath 85df7c6995SPavel Labath uint32_t source = 0x47424742; 86df7c6995SPavel Labath uint32_t dest = 0; 87df7c6995SPavel Labath 88df7c6995SPavel Labath struct iovec local, remote; 89df7c6995SPavel Labath remote.iov_base = &source; 90df7c6995SPavel Labath local.iov_base = &dest; 91df7c6995SPavel Labath remote.iov_len = local.iov_len = sizeof source; 92df7c6995SPavel Labath 93df7c6995SPavel Labath // We shall try if cross-process-memory reads work by attempting to read a value from our own process. 94df7c6995SPavel Labath ssize_t res = process_vm_readv(getpid(), &local, 1, &remote, 1, 0); 95df7c6995SPavel Labath is_supported = (res == sizeof(source) && source == dest); 96df7c6995SPavel Labath if (log) 97df7c6995SPavel Labath { 98df7c6995SPavel Labath if (is_supported) 99df7c6995SPavel Labath log->Printf("%s: Detected kernel support for process_vm_readv syscall. Fast memory reads enabled.", 100df7c6995SPavel Labath __FUNCTION__); 101df7c6995SPavel Labath else 102df7c6995SPavel Labath log->Printf("%s: syscall process_vm_readv failed (error: %s). Fast memory reads disabled.", 103df7c6995SPavel Labath __FUNCTION__, strerror(errno)); 104df7c6995SPavel Labath } 105df7c6995SPavel Labath }); 106df7c6995SPavel Labath 107df7c6995SPavel Labath return is_supported; 108df7c6995SPavel Labath } 109df7c6995SPavel Labath 110af245d11STodd Fiala namespace 111af245d11STodd Fiala { 112af245d11STodd Fiala Error 113*2a86b555SPavel Labath ResolveProcessArchitecture(lldb::pid_t pid, ArchSpec &arch) 114af245d11STodd Fiala { 115af245d11STodd Fiala // Grab process info for the running process. 116af245d11STodd Fiala ProcessInstanceInfo process_info; 117*2a86b555SPavel Labath if (!Host::GetProcessInfo(pid, process_info)) 118db264a6dSTamas Berghammer return Error("failed to get process info"); 119af245d11STodd Fiala 120af245d11STodd Fiala // Resolve the executable module. 121*2a86b555SPavel Labath ModuleSpecList module_specs; 122*2a86b555SPavel Labath if (!ObjectFile::GetModuleSpecifications(process_info.GetExecutableFile(), 0, 0, module_specs)) 123*2a86b555SPavel Labath return Error("failed to get module specifications"); 124*2a86b555SPavel Labath assert(module_specs.GetSize() == 1); 125af245d11STodd Fiala 126*2a86b555SPavel Labath arch = module_specs.GetModuleSpecRefAtIndex(0).GetArchitecture(); 127af245d11STodd Fiala if (arch.IsValid()) 128af245d11STodd Fiala return Error(); 129af245d11STodd Fiala else 130af245d11STodd Fiala return Error("failed to retrieve a valid architecture from the exe module"); 131af245d11STodd Fiala } 132af245d11STodd Fiala 133af245d11STodd Fiala void 134db264a6dSTamas Berghammer DisplayBytes (StreamString &s, void *bytes, uint32_t count) 135af245d11STodd Fiala { 136af245d11STodd Fiala uint8_t *ptr = (uint8_t *)bytes; 137af245d11STodd Fiala const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count); 138af245d11STodd Fiala for(uint32_t i=0; i<loop_count; i++) 139af245d11STodd Fiala { 140af245d11STodd Fiala s.Printf ("[%x]", *ptr); 141af245d11STodd Fiala ptr++; 142af245d11STodd Fiala } 143af245d11STodd Fiala } 144af245d11STodd Fiala 145af245d11STodd Fiala void 146af245d11STodd Fiala PtraceDisplayBytes(int &req, void *data, size_t data_size) 147af245d11STodd Fiala { 148af245d11STodd Fiala StreamString buf; 149af245d11STodd Fiala Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet ( 150af245d11STodd Fiala POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE)); 151af245d11STodd Fiala 152af245d11STodd Fiala if (verbose_log) 153af245d11STodd Fiala { 154af245d11STodd Fiala switch(req) 155af245d11STodd Fiala { 156af245d11STodd Fiala case PTRACE_POKETEXT: 157af245d11STodd Fiala { 158af245d11STodd Fiala DisplayBytes(buf, &data, 8); 159af245d11STodd Fiala verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData()); 160af245d11STodd Fiala break; 161af245d11STodd Fiala } 162af245d11STodd Fiala case PTRACE_POKEDATA: 163af245d11STodd Fiala { 164af245d11STodd Fiala DisplayBytes(buf, &data, 8); 165af245d11STodd Fiala verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData()); 166af245d11STodd Fiala break; 167af245d11STodd Fiala } 168af245d11STodd Fiala case PTRACE_POKEUSER: 169af245d11STodd Fiala { 170af245d11STodd Fiala DisplayBytes(buf, &data, 8); 171af245d11STodd Fiala verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData()); 172af245d11STodd Fiala break; 173af245d11STodd Fiala } 174af245d11STodd Fiala case PTRACE_SETREGS: 175af245d11STodd Fiala { 176af245d11STodd Fiala DisplayBytes(buf, data, data_size); 177af245d11STodd Fiala verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData()); 178af245d11STodd Fiala break; 179af245d11STodd Fiala } 180af245d11STodd Fiala case PTRACE_SETFPREGS: 181af245d11STodd Fiala { 182af245d11STodd Fiala DisplayBytes(buf, data, data_size); 183af245d11STodd Fiala verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData()); 184af245d11STodd Fiala break; 185af245d11STodd Fiala } 186af245d11STodd Fiala case PTRACE_SETSIGINFO: 187af245d11STodd Fiala { 188af245d11STodd Fiala DisplayBytes(buf, data, sizeof(siginfo_t)); 189af245d11STodd Fiala verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData()); 190af245d11STodd Fiala break; 191af245d11STodd Fiala } 192af245d11STodd Fiala case PTRACE_SETREGSET: 193af245d11STodd Fiala { 194af245d11STodd Fiala // Extract iov_base from data, which is a pointer to the struct IOVEC 195af245d11STodd Fiala DisplayBytes(buf, *(void **)data, data_size); 196af245d11STodd Fiala verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData()); 197af245d11STodd Fiala break; 198af245d11STodd Fiala } 199af245d11STodd Fiala default: 200af245d11STodd Fiala { 201af245d11STodd Fiala } 202af245d11STodd Fiala } 203af245d11STodd Fiala } 204af245d11STodd Fiala } 205af245d11STodd Fiala 20619cbe96aSPavel Labath static constexpr unsigned k_ptrace_word_size = sizeof(void*); 20719cbe96aSPavel Labath static_assert(sizeof(long) >= k_ptrace_word_size, "Size of long must be larger than ptrace word size"); 2081107b5a5SPavel Labath } // end of anonymous namespace 2091107b5a5SPavel Labath 210bd7cbc5aSPavel Labath // Simple helper function to ensure flags are enabled on the given file 211bd7cbc5aSPavel Labath // descriptor. 212bd7cbc5aSPavel Labath static Error 213bd7cbc5aSPavel Labath EnsureFDFlags(int fd, int flags) 214bd7cbc5aSPavel Labath { 215bd7cbc5aSPavel Labath Error error; 216bd7cbc5aSPavel Labath 217bd7cbc5aSPavel Labath int status = fcntl(fd, F_GETFL); 218bd7cbc5aSPavel Labath if (status == -1) 219bd7cbc5aSPavel Labath { 220bd7cbc5aSPavel Labath error.SetErrorToErrno(); 221bd7cbc5aSPavel Labath return error; 222bd7cbc5aSPavel Labath } 223bd7cbc5aSPavel Labath 224bd7cbc5aSPavel Labath if (fcntl(fd, F_SETFL, status | flags) == -1) 225bd7cbc5aSPavel Labath { 226bd7cbc5aSPavel Labath error.SetErrorToErrno(); 227bd7cbc5aSPavel Labath return error; 228bd7cbc5aSPavel Labath } 229bd7cbc5aSPavel Labath 230bd7cbc5aSPavel Labath return error; 231bd7cbc5aSPavel Labath } 232bd7cbc5aSPavel Labath 233*2a86b555SPavel Labath NativeProcessLinux::LaunchArgs::LaunchArgs(char const **argv, char const **envp, const FileSpec &stdin_file_spec, 234*2a86b555SPavel Labath const FileSpec &stdout_file_spec, const FileSpec &stderr_file_spec, 235*2a86b555SPavel Labath const FileSpec &working_dir, const ProcessLaunchInfo &launch_info) 236*2a86b555SPavel Labath : m_argv(argv), 237af245d11STodd Fiala m_envp(envp), 238d3173f34SChaoren Lin m_stdin_file_spec(stdin_file_spec), 239d3173f34SChaoren Lin m_stdout_file_spec(stdout_file_spec), 240d3173f34SChaoren Lin m_stderr_file_spec(stderr_file_spec), 2410bce1b67STodd Fiala m_working_dir(working_dir), 2420bce1b67STodd Fiala m_launch_info(launch_info) 2430bce1b67STodd Fiala { 2440bce1b67STodd Fiala } 245af245d11STodd Fiala 246af245d11STodd Fiala NativeProcessLinux::LaunchArgs::~LaunchArgs() 247af245d11STodd Fiala { } 248af245d11STodd Fiala 249af245d11STodd Fiala // ----------------------------------------------------------------------------- 250af245d11STodd Fiala // Public Static Methods 251af245d11STodd Fiala // ----------------------------------------------------------------------------- 252af245d11STodd Fiala 253db264a6dSTamas Berghammer Error 254d5b310f2SPavel Labath NativeProcessProtocol::Launch ( 255db264a6dSTamas Berghammer ProcessLaunchInfo &launch_info, 256db264a6dSTamas Berghammer NativeProcessProtocol::NativeDelegate &native_delegate, 25719cbe96aSPavel Labath MainLoop &mainloop, 258af245d11STodd Fiala NativeProcessProtocolSP &native_process_sp) 259af245d11STodd Fiala { 260af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 261af245d11STodd Fiala 262*2a86b555SPavel Labath Error error; 263af245d11STodd Fiala 264af245d11STodd Fiala // Verify the working directory is valid if one was specified. 265d3173f34SChaoren Lin FileSpec working_dir{launch_info.GetWorkingDirectory()}; 266d3173f34SChaoren Lin if (working_dir && 267d3173f34SChaoren Lin (!working_dir.ResolvePath() || 268d3173f34SChaoren Lin working_dir.GetFileType() != FileSpec::eFileTypeDirectory)) 269af245d11STodd Fiala { 270d3173f34SChaoren Lin error.SetErrorStringWithFormat ("No such file or directory: %s", 271d3173f34SChaoren Lin working_dir.GetCString()); 272af245d11STodd Fiala return error; 273af245d11STodd Fiala } 274af245d11STodd Fiala 275db264a6dSTamas Berghammer const FileAction *file_action; 276af245d11STodd Fiala 277d3173f34SChaoren Lin // Default of empty will mean to use existing open file descriptors. 278d3173f34SChaoren Lin FileSpec stdin_file_spec{}; 279d3173f34SChaoren Lin FileSpec stdout_file_spec{}; 280d3173f34SChaoren Lin FileSpec stderr_file_spec{}; 281af245d11STodd Fiala 282af245d11STodd Fiala file_action = launch_info.GetFileActionForFD (STDIN_FILENO); 28375f47c3aSTodd Fiala if (file_action) 284d3173f34SChaoren Lin stdin_file_spec = file_action->GetFileSpec(); 285af245d11STodd Fiala 286af245d11STodd Fiala file_action = launch_info.GetFileActionForFD (STDOUT_FILENO); 28775f47c3aSTodd Fiala if (file_action) 288d3173f34SChaoren Lin stdout_file_spec = file_action->GetFileSpec(); 289af245d11STodd Fiala 290af245d11STodd Fiala file_action = launch_info.GetFileActionForFD (STDERR_FILENO); 29175f47c3aSTodd Fiala if (file_action) 292d3173f34SChaoren Lin stderr_file_spec = file_action->GetFileSpec(); 29375f47c3aSTodd Fiala 29475f47c3aSTodd Fiala if (log) 29575f47c3aSTodd Fiala { 296d3173f34SChaoren Lin if (stdin_file_spec) 297d3173f34SChaoren Lin log->Printf ("NativeProcessLinux::%s setting STDIN to '%s'", 298d3173f34SChaoren Lin __FUNCTION__, stdin_file_spec.GetCString()); 29975f47c3aSTodd Fiala else 30075f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s leaving STDIN as is", __FUNCTION__); 30175f47c3aSTodd Fiala 302d3173f34SChaoren Lin if (stdout_file_spec) 303d3173f34SChaoren Lin log->Printf ("NativeProcessLinux::%s setting STDOUT to '%s'", 304d3173f34SChaoren Lin __FUNCTION__, stdout_file_spec.GetCString()); 30575f47c3aSTodd Fiala else 30675f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s leaving STDOUT as is", __FUNCTION__); 30775f47c3aSTodd Fiala 308d3173f34SChaoren Lin if (stderr_file_spec) 309d3173f34SChaoren Lin log->Printf ("NativeProcessLinux::%s setting STDERR to '%s'", 310d3173f34SChaoren Lin __FUNCTION__, stderr_file_spec.GetCString()); 31175f47c3aSTodd Fiala else 31275f47c3aSTodd Fiala log->Printf ("NativeProcessLinux::%s leaving STDERR as is", __FUNCTION__); 31375f47c3aSTodd Fiala } 314af245d11STodd Fiala 315af245d11STodd Fiala // Create the NativeProcessLinux in launch mode. 316af245d11STodd Fiala native_process_sp.reset (new NativeProcessLinux ()); 317af245d11STodd Fiala 318af245d11STodd Fiala if (log) 319af245d11STodd Fiala { 320af245d11STodd Fiala int i = 0; 321af245d11STodd Fiala for (const char **args = launch_info.GetArguments ().GetConstArgumentVector (); *args; ++args, ++i) 322af245d11STodd Fiala { 323af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s arg %d: \"%s\"", __FUNCTION__, i, *args ? *args : "nullptr"); 324af245d11STodd Fiala ++i; 325af245d11STodd Fiala } 326af245d11STodd Fiala } 327af245d11STodd Fiala 328af245d11STodd Fiala if (!native_process_sp->RegisterNativeDelegate (native_delegate)) 329af245d11STodd Fiala { 330af245d11STodd Fiala native_process_sp.reset (); 331af245d11STodd Fiala error.SetErrorStringWithFormat ("failed to register the native delegate"); 332af245d11STodd Fiala return error; 333af245d11STodd Fiala } 334af245d11STodd Fiala 335cb84eebbSTamas Berghammer std::static_pointer_cast<NativeProcessLinux> (native_process_sp)->LaunchInferior ( 33619cbe96aSPavel Labath mainloop, 337af245d11STodd Fiala launch_info.GetArguments ().GetConstArgumentVector (), 338af245d11STodd Fiala launch_info.GetEnvironmentEntries ().GetConstArgumentVector (), 339d3173f34SChaoren Lin stdin_file_spec, 340d3173f34SChaoren Lin stdout_file_spec, 341d3173f34SChaoren Lin stderr_file_spec, 342af245d11STodd Fiala working_dir, 3430bce1b67STodd Fiala launch_info, 344af245d11STodd Fiala error); 345af245d11STodd Fiala 346af245d11STodd Fiala if (error.Fail ()) 347af245d11STodd Fiala { 348af245d11STodd Fiala native_process_sp.reset (); 349af245d11STodd Fiala if (log) 350af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to launch process: %s", __FUNCTION__, error.AsCString ()); 351af245d11STodd Fiala return error; 352af245d11STodd Fiala } 353af245d11STodd Fiala 354af245d11STodd Fiala launch_info.SetProcessID (native_process_sp->GetID ()); 355af245d11STodd Fiala 356af245d11STodd Fiala return error; 357af245d11STodd Fiala } 358af245d11STodd Fiala 359db264a6dSTamas Berghammer Error 360d5b310f2SPavel Labath NativeProcessProtocol::Attach ( 361af245d11STodd Fiala lldb::pid_t pid, 362db264a6dSTamas Berghammer NativeProcessProtocol::NativeDelegate &native_delegate, 36319cbe96aSPavel Labath MainLoop &mainloop, 364af245d11STodd Fiala NativeProcessProtocolSP &native_process_sp) 365af245d11STodd Fiala { 366af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 367af245d11STodd Fiala if (log && log->GetMask ().Test (POSIX_LOG_VERBOSE)) 368af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(pid = %" PRIi64 ")", __FUNCTION__, pid); 369af245d11STodd Fiala 370af245d11STodd Fiala // Retrieve the architecture for the running process. 371af245d11STodd Fiala ArchSpec process_arch; 372*2a86b555SPavel Labath Error error = ResolveProcessArchitecture(pid, process_arch); 373af245d11STodd Fiala if (!error.Success ()) 374af245d11STodd Fiala return error; 375af245d11STodd Fiala 3761339b5e8SOleksiy Vyalov std::shared_ptr<NativeProcessLinux> native_process_linux_sp (new NativeProcessLinux ()); 377af245d11STodd Fiala 3781339b5e8SOleksiy Vyalov if (!native_process_linux_sp->RegisterNativeDelegate (native_delegate)) 379af245d11STodd Fiala { 380af245d11STodd Fiala error.SetErrorStringWithFormat ("failed to register the native delegate"); 381af245d11STodd Fiala return error; 382af245d11STodd Fiala } 383af245d11STodd Fiala 38419cbe96aSPavel Labath native_process_linux_sp->AttachToInferior (mainloop, pid, error); 385af245d11STodd Fiala if (!error.Success ()) 386af245d11STodd Fiala return error; 387af245d11STodd Fiala 3881339b5e8SOleksiy Vyalov native_process_sp = native_process_linux_sp; 389af245d11STodd Fiala return error; 390af245d11STodd Fiala } 391af245d11STodd Fiala 392af245d11STodd Fiala // ----------------------------------------------------------------------------- 393af245d11STodd Fiala // Public Instance Methods 394af245d11STodd Fiala // ----------------------------------------------------------------------------- 395af245d11STodd Fiala 396af245d11STodd Fiala NativeProcessLinux::NativeProcessLinux () : 397af245d11STodd Fiala NativeProcessProtocol (LLDB_INVALID_PROCESS_ID), 398af245d11STodd Fiala m_arch (), 399af245d11STodd Fiala m_supports_mem_region (eLazyBoolCalculate), 400af245d11STodd Fiala m_mem_region_cache (), 4010e1d729bSPavel Labath m_pending_notification_tid(LLDB_INVALID_THREAD_ID) 402af245d11STodd Fiala { 403af245d11STodd Fiala } 404af245d11STodd Fiala 405af245d11STodd Fiala void 406af245d11STodd Fiala NativeProcessLinux::LaunchInferior ( 40719cbe96aSPavel Labath MainLoop &mainloop, 408af245d11STodd Fiala const char *argv[], 409af245d11STodd Fiala const char *envp[], 410d3173f34SChaoren Lin const FileSpec &stdin_file_spec, 411d3173f34SChaoren Lin const FileSpec &stdout_file_spec, 412d3173f34SChaoren Lin const FileSpec &stderr_file_spec, 413d3173f34SChaoren Lin const FileSpec &working_dir, 414db264a6dSTamas Berghammer const ProcessLaunchInfo &launch_info, 415db264a6dSTamas Berghammer Error &error) 416af245d11STodd Fiala { 41719cbe96aSPavel Labath m_sigchld_handle = mainloop.RegisterSignal(SIGCHLD, 41819cbe96aSPavel Labath [this] (MainLoopBase &) { SigchldHandler(); }, error); 41919cbe96aSPavel Labath if (! m_sigchld_handle) 42019cbe96aSPavel Labath return; 42119cbe96aSPavel Labath 422af245d11STodd Fiala SetState (eStateLaunching); 423af245d11STodd Fiala 424af245d11STodd Fiala std::unique_ptr<LaunchArgs> args( 425*2a86b555SPavel Labath new LaunchArgs(argv, envp, stdin_file_spec, stdout_file_spec, stderr_file_spec, working_dir, launch_info)); 426af245d11STodd Fiala 42719cbe96aSPavel Labath Launch(args.get(), error); 428af245d11STodd Fiala } 429af245d11STodd Fiala 430af245d11STodd Fiala void 43119cbe96aSPavel Labath NativeProcessLinux::AttachToInferior (MainLoop &mainloop, lldb::pid_t pid, Error &error) 432af245d11STodd Fiala { 433af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 434af245d11STodd Fiala if (log) 435af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ")", __FUNCTION__, pid); 436af245d11STodd Fiala 43719cbe96aSPavel Labath m_sigchld_handle = mainloop.RegisterSignal(SIGCHLD, 43819cbe96aSPavel Labath [this] (MainLoopBase &) { SigchldHandler(); }, error); 43919cbe96aSPavel Labath if (! m_sigchld_handle) 44019cbe96aSPavel Labath return; 44119cbe96aSPavel Labath 442*2a86b555SPavel Labath error = ResolveProcessArchitecture(pid, m_arch); 443af245d11STodd Fiala if (!error.Success()) 444af245d11STodd Fiala return; 445af245d11STodd Fiala 446af245d11STodd Fiala // Set the architecture to the exe architecture. 447af245d11STodd Fiala if (log) 448af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ") detected architecture %s", __FUNCTION__, pid, m_arch.GetArchitectureName ()); 449af245d11STodd Fiala 450af245d11STodd Fiala m_pid = pid; 451af245d11STodd Fiala SetState(eStateAttaching); 452af245d11STodd Fiala 45319cbe96aSPavel Labath Attach(pid, error); 454af245d11STodd Fiala } 455af245d11STodd Fiala 456bd7cbc5aSPavel Labath ::pid_t 457bd7cbc5aSPavel Labath NativeProcessLinux::Launch(LaunchArgs *args, Error &error) 458af245d11STodd Fiala { 4590bce1b67STodd Fiala assert (args && "null args"); 460af245d11STodd Fiala 461af245d11STodd Fiala const char **argv = args->m_argv; 462af245d11STodd Fiala const char **envp = args->m_envp; 463d3173f34SChaoren Lin const FileSpec working_dir = args->m_working_dir; 464af245d11STodd Fiala 465af245d11STodd Fiala lldb_utility::PseudoTerminal terminal; 466af245d11STodd Fiala const size_t err_len = 1024; 467af245d11STodd Fiala char err_str[err_len]; 468af245d11STodd Fiala lldb::pid_t pid; 469af245d11STodd Fiala 470af245d11STodd Fiala // Propagate the environment if one is not supplied. 471af245d11STodd Fiala if (envp == NULL || envp[0] == NULL) 472af245d11STodd Fiala envp = const_cast<const char **>(environ); 473af245d11STodd Fiala 474af245d11STodd Fiala if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t> (-1)) 475af245d11STodd Fiala { 476bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 477bd7cbc5aSPavel Labath error.SetErrorStringWithFormat("Process fork failed: %s", err_str); 478bd7cbc5aSPavel Labath return -1; 479af245d11STodd Fiala } 480af245d11STodd Fiala 481af245d11STodd Fiala // Recognized child exit status codes. 482af245d11STodd Fiala enum { 483af245d11STodd Fiala ePtraceFailed = 1, 484af245d11STodd Fiala eDupStdinFailed, 485af245d11STodd Fiala eDupStdoutFailed, 486af245d11STodd Fiala eDupStderrFailed, 487af245d11STodd Fiala eChdirFailed, 488af245d11STodd Fiala eExecFailed, 48978856474SPavel Labath eSetGidFailed, 49078856474SPavel Labath eSetSigMaskFailed 491af245d11STodd Fiala }; 492af245d11STodd Fiala 493af245d11STodd Fiala // Child process. 494af245d11STodd Fiala if (pid == 0) 495af245d11STodd Fiala { 496d2c4c9b1SPavel Labath // First, make sure we disable all logging. If we are logging to stdout, our logs can be 497d2c4c9b1SPavel Labath // mistaken for inferior output. 498d2c4c9b1SPavel Labath Log::DisableAllLogChannels(nullptr); 49975f47c3aSTodd Fiala // FIXME consider opening a pipe between parent/child and have this forked child 500d2c4c9b1SPavel Labath // send log info to parent re: launch status. 501af245d11STodd Fiala 50275f47c3aSTodd Fiala // Start tracing this child that is about to exec. 5034a9babb2SPavel Labath error = PtraceWrapper(PTRACE_TRACEME, 0); 504bd7cbc5aSPavel Labath if (error.Fail()) 505af245d11STodd Fiala exit(ePtraceFailed); 506af245d11STodd Fiala 507493c3a12SPavel Labath // terminal has already dupped the tty descriptors to stdin/out/err. 508493c3a12SPavel Labath // This closes original fd from which they were copied (and avoids 509493c3a12SPavel Labath // leaking descriptors to the debugged process. 510493c3a12SPavel Labath terminal.CloseSlaveFileDescriptor(); 511493c3a12SPavel Labath 512af245d11STodd Fiala // Do not inherit setgid powers. 513af245d11STodd Fiala if (setgid(getgid()) != 0) 514af245d11STodd Fiala exit(eSetGidFailed); 515af245d11STodd Fiala 516af245d11STodd Fiala // Attempt to have our own process group. 517af245d11STodd Fiala if (setpgid(0, 0) != 0) 518af245d11STodd Fiala { 51975f47c3aSTodd Fiala // FIXME log that this failed. This is common. 520af245d11STodd Fiala // Don't allow this to prevent an inferior exec. 521af245d11STodd Fiala } 522af245d11STodd Fiala 523af245d11STodd Fiala // Dup file descriptors if needed. 524d3173f34SChaoren Lin if (args->m_stdin_file_spec) 525d3173f34SChaoren Lin if (!DupDescriptor(args->m_stdin_file_spec, STDIN_FILENO, O_RDONLY)) 526af245d11STodd Fiala exit(eDupStdinFailed); 527af245d11STodd Fiala 528d3173f34SChaoren Lin if (args->m_stdout_file_spec) 529d3173f34SChaoren Lin if (!DupDescriptor(args->m_stdout_file_spec, STDOUT_FILENO, O_WRONLY | O_CREAT | O_TRUNC)) 530af245d11STodd Fiala exit(eDupStdoutFailed); 531af245d11STodd Fiala 532d3173f34SChaoren Lin if (args->m_stderr_file_spec) 533d3173f34SChaoren Lin if (!DupDescriptor(args->m_stderr_file_spec, STDERR_FILENO, O_WRONLY | O_CREAT | O_TRUNC)) 534af245d11STodd Fiala exit(eDupStderrFailed); 535af245d11STodd Fiala 5369cf4f2c2SChaoren Lin // Close everything besides stdin, stdout, and stderr that has no file 5379cf4f2c2SChaoren Lin // action to avoid leaking 5389cf4f2c2SChaoren Lin for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd) 5399cf4f2c2SChaoren Lin if (!args->m_launch_info.GetFileActionForFD(fd)) 5409cf4f2c2SChaoren Lin close(fd); 5419cf4f2c2SChaoren Lin 542af245d11STodd Fiala // Change working directory 543d3173f34SChaoren Lin if (working_dir && 0 != ::chdir(working_dir.GetCString())) 544af245d11STodd Fiala exit(eChdirFailed); 545af245d11STodd Fiala 5460bce1b67STodd Fiala // Disable ASLR if requested. 5470bce1b67STodd Fiala if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR)) 5480bce1b67STodd Fiala { 5490bce1b67STodd Fiala const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS); 5500bce1b67STodd Fiala if (old_personality == -1) 5510bce1b67STodd Fiala { 55275f47c3aSTodd Fiala // Can't retrieve Linux personality. Cannot disable ASLR. 5530bce1b67STodd Fiala } 5540bce1b67STodd Fiala else 5550bce1b67STodd Fiala { 5560bce1b67STodd Fiala const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality); 5570bce1b67STodd Fiala if (new_personality == -1) 5580bce1b67STodd Fiala { 55975f47c3aSTodd Fiala // Disabling ASLR failed. 5600bce1b67STodd Fiala } 5610bce1b67STodd Fiala else 5620bce1b67STodd Fiala { 56375f47c3aSTodd Fiala // Disabling ASLR succeeded. 5640bce1b67STodd Fiala } 5650bce1b67STodd Fiala } 5660bce1b67STodd Fiala } 5670bce1b67STodd Fiala 56878856474SPavel Labath // Clear the signal mask to prevent the child from being affected by 56978856474SPavel Labath // any masking done by the parent. 57078856474SPavel Labath sigset_t set; 57178856474SPavel Labath if (sigemptyset(&set) != 0 || pthread_sigmask(SIG_SETMASK, &set, nullptr) != 0) 57278856474SPavel Labath exit(eSetSigMaskFailed); 57378856474SPavel Labath 57475f47c3aSTodd Fiala // Execute. We should never return... 575af245d11STodd Fiala execve(argv[0], 576af245d11STodd Fiala const_cast<char *const *>(argv), 577af245d11STodd Fiala const_cast<char *const *>(envp)); 57875f47c3aSTodd Fiala 57975f47c3aSTodd Fiala // ...unless exec fails. In which case we definitely need to end the child here. 580af245d11STodd Fiala exit(eExecFailed); 581af245d11STodd Fiala } 582af245d11STodd Fiala 58375f47c3aSTodd Fiala // 58475f47c3aSTodd Fiala // This is the parent code here. 58575f47c3aSTodd Fiala // 58675f47c3aSTodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 58775f47c3aSTodd Fiala 588af245d11STodd Fiala // Wait for the child process to trap on its call to execve. 589af245d11STodd Fiala ::pid_t wpid; 590af245d11STodd Fiala int status; 591af245d11STodd Fiala if ((wpid = waitpid(pid, &status, 0)) < 0) 592af245d11STodd Fiala { 593bd7cbc5aSPavel Labath error.SetErrorToErrno(); 594af245d11STodd Fiala if (log) 595bd7cbc5aSPavel Labath log->Printf ("NativeProcessLinux::%s waitpid for inferior failed with %s", 596bd7cbc5aSPavel Labath __FUNCTION__, error.AsCString ()); 597af245d11STodd Fiala 598af245d11STodd Fiala // Mark the inferior as invalid. 599af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 600bd7cbc5aSPavel Labath SetState (StateType::eStateInvalid); 601af245d11STodd Fiala 602bd7cbc5aSPavel Labath return -1; 603af245d11STodd Fiala } 604af245d11STodd Fiala else if (WIFEXITED(status)) 605af245d11STodd Fiala { 606af245d11STodd Fiala // open, dup or execve likely failed for some reason. 607bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 608af245d11STodd Fiala switch (WEXITSTATUS(status)) 609af245d11STodd Fiala { 610af245d11STodd Fiala case ePtraceFailed: 611bd7cbc5aSPavel Labath error.SetErrorString("Child ptrace failed."); 612af245d11STodd Fiala break; 613af245d11STodd Fiala case eDupStdinFailed: 614bd7cbc5aSPavel Labath error.SetErrorString("Child open stdin failed."); 615af245d11STodd Fiala break; 616af245d11STodd Fiala case eDupStdoutFailed: 617bd7cbc5aSPavel Labath error.SetErrorString("Child open stdout failed."); 618af245d11STodd Fiala break; 619af245d11STodd Fiala case eDupStderrFailed: 620bd7cbc5aSPavel Labath error.SetErrorString("Child open stderr failed."); 621af245d11STodd Fiala break; 622af245d11STodd Fiala case eChdirFailed: 623bd7cbc5aSPavel Labath error.SetErrorString("Child failed to set working directory."); 624af245d11STodd Fiala break; 625af245d11STodd Fiala case eExecFailed: 626bd7cbc5aSPavel Labath error.SetErrorString("Child exec failed."); 627af245d11STodd Fiala break; 628af245d11STodd Fiala case eSetGidFailed: 629bd7cbc5aSPavel Labath error.SetErrorString("Child setgid failed."); 630af245d11STodd Fiala break; 63178856474SPavel Labath case eSetSigMaskFailed: 63278856474SPavel Labath error.SetErrorString("Child failed to set signal mask."); 63378856474SPavel Labath break; 634af245d11STodd Fiala default: 635bd7cbc5aSPavel Labath error.SetErrorString("Child returned unknown exit status."); 636af245d11STodd Fiala break; 637af245d11STodd Fiala } 638af245d11STodd Fiala 639af245d11STodd Fiala if (log) 640af245d11STodd Fiala { 641af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior exited with status %d before issuing a STOP", 642af245d11STodd Fiala __FUNCTION__, 643af245d11STodd Fiala WEXITSTATUS(status)); 644af245d11STodd Fiala } 645af245d11STodd Fiala 646af245d11STodd Fiala // Mark the inferior as invalid. 647af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 648bd7cbc5aSPavel Labath SetState (StateType::eStateInvalid); 649af245d11STodd Fiala 650bd7cbc5aSPavel Labath return -1; 651af245d11STodd Fiala } 652af245d11STodd Fiala assert(WIFSTOPPED(status) && (wpid == static_cast< ::pid_t> (pid)) && 653af245d11STodd Fiala "Could not sync with inferior process."); 654af245d11STodd Fiala 655af245d11STodd Fiala if (log) 656af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior started, now in stopped state", __FUNCTION__); 657af245d11STodd Fiala 658bd7cbc5aSPavel Labath error = SetDefaultPtraceOpts(pid); 659bd7cbc5aSPavel Labath if (error.Fail()) 660af245d11STodd Fiala { 661af245d11STodd Fiala if (log) 662af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior failed to set default ptrace options: %s", 663bd7cbc5aSPavel Labath __FUNCTION__, error.AsCString ()); 664af245d11STodd Fiala 665af245d11STodd Fiala // Mark the inferior as invalid. 666af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 667bd7cbc5aSPavel Labath SetState (StateType::eStateInvalid); 668af245d11STodd Fiala 669bd7cbc5aSPavel Labath return -1; 670af245d11STodd Fiala } 671af245d11STodd Fiala 672af245d11STodd Fiala // Release the master terminal descriptor and pass it off to the 673af245d11STodd Fiala // NativeProcessLinux instance. Similarly stash the inferior pid. 674bd7cbc5aSPavel Labath m_terminal_fd = terminal.ReleaseMasterFileDescriptor(); 675bd7cbc5aSPavel Labath m_pid = pid; 676af245d11STodd Fiala 677af245d11STodd Fiala // Set the terminal fd to be in non blocking mode (it simplifies the 678af245d11STodd Fiala // implementation of ProcessLinux::GetSTDOUT to have a non-blocking 679af245d11STodd Fiala // descriptor to read from). 680bd7cbc5aSPavel Labath error = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); 681bd7cbc5aSPavel Labath if (error.Fail()) 682af245d11STodd Fiala { 683af245d11STodd Fiala if (log) 684af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior EnsureFDFlags failed for ensuring terminal O_NONBLOCK setting: %s", 685bd7cbc5aSPavel Labath __FUNCTION__, error.AsCString ()); 686af245d11STodd Fiala 687af245d11STodd Fiala // Mark the inferior as invalid. 688af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 689bd7cbc5aSPavel Labath SetState (StateType::eStateInvalid); 690af245d11STodd Fiala 691bd7cbc5aSPavel Labath return -1; 692af245d11STodd Fiala } 693af245d11STodd Fiala 694af245d11STodd Fiala if (log) 695af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() adding pid = %" PRIu64, __FUNCTION__, pid); 696af245d11STodd Fiala 697*2a86b555SPavel Labath ResolveProcessArchitecture(m_pid, m_arch); 698f9077782SPavel Labath NativeThreadLinuxSP thread_sp = AddThread(pid); 699af245d11STodd Fiala assert (thread_sp && "AddThread() returned a nullptr thread"); 700f9077782SPavel Labath thread_sp->SetStoppedBySignal(SIGSTOP); 701f9077782SPavel Labath ThreadWasCreated(*thread_sp); 702af245d11STodd Fiala 703af245d11STodd Fiala // Let our process instance know the thread has stopped. 704bd7cbc5aSPavel Labath SetCurrentThreadID (thread_sp->GetID ()); 705bd7cbc5aSPavel Labath SetState (StateType::eStateStopped); 706af245d11STodd Fiala 707af245d11STodd Fiala if (log) 708af245d11STodd Fiala { 709bd7cbc5aSPavel Labath if (error.Success ()) 710af245d11STodd Fiala { 711af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior launching succeeded", __FUNCTION__); 712af245d11STodd Fiala } 713af245d11STodd Fiala else 714af245d11STodd Fiala { 715af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior launching failed: %s", 716bd7cbc5aSPavel Labath __FUNCTION__, error.AsCString ()); 717bd7cbc5aSPavel Labath return -1; 718af245d11STodd Fiala } 719af245d11STodd Fiala } 720bd7cbc5aSPavel Labath return pid; 721af245d11STodd Fiala } 722af245d11STodd Fiala 723bd7cbc5aSPavel Labath ::pid_t 724bd7cbc5aSPavel Labath NativeProcessLinux::Attach(lldb::pid_t pid, Error &error) 725af245d11STodd Fiala { 726af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 727af245d11STodd Fiala 728af245d11STodd Fiala // Use a map to keep track of the threads which we have attached/need to attach. 729af245d11STodd Fiala Host::TidMap tids_to_attach; 730af245d11STodd Fiala if (pid <= 1) 731af245d11STodd Fiala { 732bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 733bd7cbc5aSPavel Labath error.SetErrorString("Attaching to process 1 is not allowed."); 734bd7cbc5aSPavel Labath return -1; 735af245d11STodd Fiala } 736af245d11STodd Fiala 737af245d11STodd Fiala while (Host::FindProcessThreads(pid, tids_to_attach)) 738af245d11STodd Fiala { 739af245d11STodd Fiala for (Host::TidMap::iterator it = tids_to_attach.begin(); 740af245d11STodd Fiala it != tids_to_attach.end();) 741af245d11STodd Fiala { 742af245d11STodd Fiala if (it->second == false) 743af245d11STodd Fiala { 744af245d11STodd Fiala lldb::tid_t tid = it->first; 745af245d11STodd Fiala 746af245d11STodd Fiala // Attach to the requested process. 747af245d11STodd Fiala // An attach will cause the thread to stop with a SIGSTOP. 7484a9babb2SPavel Labath error = PtraceWrapper(PTRACE_ATTACH, tid); 749bd7cbc5aSPavel Labath if (error.Fail()) 750af245d11STodd Fiala { 751af245d11STodd Fiala // No such thread. The thread may have exited. 752af245d11STodd Fiala // More error handling may be needed. 753bd7cbc5aSPavel Labath if (error.GetError() == ESRCH) 754af245d11STodd Fiala { 755af245d11STodd Fiala it = tids_to_attach.erase(it); 756af245d11STodd Fiala continue; 757af245d11STodd Fiala } 758af245d11STodd Fiala else 759bd7cbc5aSPavel Labath return -1; 760af245d11STodd Fiala } 761af245d11STodd Fiala 762af245d11STodd Fiala int status; 763af245d11STodd Fiala // Need to use __WALL otherwise we receive an error with errno=ECHLD 764af245d11STodd Fiala // At this point we should have a thread stopped if waitpid succeeds. 765af245d11STodd Fiala if ((status = waitpid(tid, NULL, __WALL)) < 0) 766af245d11STodd Fiala { 767af245d11STodd Fiala // No such thread. The thread may have exited. 768af245d11STodd Fiala // More error handling may be needed. 769af245d11STodd Fiala if (errno == ESRCH) 770af245d11STodd Fiala { 771af245d11STodd Fiala it = tids_to_attach.erase(it); 772af245d11STodd Fiala continue; 773af245d11STodd Fiala } 774af245d11STodd Fiala else 775af245d11STodd Fiala { 776bd7cbc5aSPavel Labath error.SetErrorToErrno(); 777bd7cbc5aSPavel Labath return -1; 778af245d11STodd Fiala } 779af245d11STodd Fiala } 780af245d11STodd Fiala 781bd7cbc5aSPavel Labath error = SetDefaultPtraceOpts(tid); 782bd7cbc5aSPavel Labath if (error.Fail()) 783bd7cbc5aSPavel Labath return -1; 784af245d11STodd Fiala 785af245d11STodd Fiala if (log) 786af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() adding tid = %" PRIu64, __FUNCTION__, tid); 787af245d11STodd Fiala 788af245d11STodd Fiala it->second = true; 789af245d11STodd Fiala 790af245d11STodd Fiala // Create the thread, mark it as stopped. 791f9077782SPavel Labath NativeThreadLinuxSP thread_sp (AddThread(static_cast<lldb::tid_t>(tid))); 792af245d11STodd Fiala assert (thread_sp && "AddThread() returned a nullptr"); 793fa03ad2eSChaoren Lin 794fa03ad2eSChaoren Lin // This will notify this is a new thread and tell the system it is stopped. 795f9077782SPavel Labath thread_sp->SetStoppedBySignal(SIGSTOP); 796f9077782SPavel Labath ThreadWasCreated(*thread_sp); 797bd7cbc5aSPavel Labath SetCurrentThreadID (thread_sp->GetID ()); 798af245d11STodd Fiala } 799af245d11STodd Fiala 800af245d11STodd Fiala // move the loop forward 801af245d11STodd Fiala ++it; 802af245d11STodd Fiala } 803af245d11STodd Fiala } 804af245d11STodd Fiala 805af245d11STodd Fiala if (tids_to_attach.size() > 0) 806af245d11STodd Fiala { 807bd7cbc5aSPavel Labath m_pid = pid; 808af245d11STodd Fiala // Let our process instance know the thread has stopped. 809bd7cbc5aSPavel Labath SetState (StateType::eStateStopped); 810af245d11STodd Fiala } 811af245d11STodd Fiala else 812af245d11STodd Fiala { 813bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 814bd7cbc5aSPavel Labath error.SetErrorString("No such process."); 815bd7cbc5aSPavel Labath return -1; 816af245d11STodd Fiala } 817af245d11STodd Fiala 818bd7cbc5aSPavel Labath return pid; 819af245d11STodd Fiala } 820af245d11STodd Fiala 82197ccc294SChaoren Lin Error 822af245d11STodd Fiala NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) 823af245d11STodd Fiala { 824af245d11STodd Fiala long ptrace_opts = 0; 825af245d11STodd Fiala 826af245d11STodd Fiala // Have the child raise an event on exit. This is used to keep the child in 827af245d11STodd Fiala // limbo until it is destroyed. 828af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXIT; 829af245d11STodd Fiala 830af245d11STodd Fiala // Have the tracer trace threads which spawn in the inferior process. 831af245d11STodd Fiala // TODO: if we want to support tracing the inferiors' child, add the 832af245d11STodd Fiala // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK) 833af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACECLONE; 834af245d11STodd Fiala 835af245d11STodd Fiala // Have the tracer notify us before execve returns 836af245d11STodd Fiala // (needed to disable legacy SIGTRAP generation) 837af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXEC; 838af245d11STodd Fiala 8394a9babb2SPavel Labath return PtraceWrapper(PTRACE_SETOPTIONS, pid, nullptr, (void*)ptrace_opts); 840af245d11STodd Fiala } 841af245d11STodd Fiala 842af245d11STodd Fiala static ExitType convert_pid_status_to_exit_type (int status) 843af245d11STodd Fiala { 844af245d11STodd Fiala if (WIFEXITED (status)) 845af245d11STodd Fiala return ExitType::eExitTypeExit; 846af245d11STodd Fiala else if (WIFSIGNALED (status)) 847af245d11STodd Fiala return ExitType::eExitTypeSignal; 848af245d11STodd Fiala else if (WIFSTOPPED (status)) 849af245d11STodd Fiala return ExitType::eExitTypeStop; 850af245d11STodd Fiala else 851af245d11STodd Fiala { 852af245d11STodd Fiala // We don't know what this is. 853af245d11STodd Fiala return ExitType::eExitTypeInvalid; 854af245d11STodd Fiala } 855af245d11STodd Fiala } 856af245d11STodd Fiala 857af245d11STodd Fiala static int convert_pid_status_to_return_code (int status) 858af245d11STodd Fiala { 859af245d11STodd Fiala if (WIFEXITED (status)) 860af245d11STodd Fiala return WEXITSTATUS (status); 861af245d11STodd Fiala else if (WIFSIGNALED (status)) 862af245d11STodd Fiala return WTERMSIG (status); 863af245d11STodd Fiala else if (WIFSTOPPED (status)) 864af245d11STodd Fiala return WSTOPSIG (status); 865af245d11STodd Fiala else 866af245d11STodd Fiala { 867af245d11STodd Fiala // We don't know what this is. 868af245d11STodd Fiala return ExitType::eExitTypeInvalid; 869af245d11STodd Fiala } 870af245d11STodd Fiala } 871af245d11STodd Fiala 8721107b5a5SPavel Labath // Handles all waitpid events from the inferior process. 8731107b5a5SPavel Labath void 8741107b5a5SPavel Labath NativeProcessLinux::MonitorCallback(lldb::pid_t pid, 875af245d11STodd Fiala bool exited, 876af245d11STodd Fiala int signal, 877af245d11STodd Fiala int status) 878af245d11STodd Fiala { 879af245d11STodd Fiala Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); 880af245d11STodd Fiala 881af245d11STodd Fiala // Certain activities differ based on whether the pid is the tid of the main thread. 8821107b5a5SPavel Labath const bool is_main_thread = (pid == GetID ()); 883af245d11STodd Fiala 884af245d11STodd Fiala // Handle when the thread exits. 885af245d11STodd Fiala if (exited) 886af245d11STodd Fiala { 887af245d11STodd Fiala if (log) 88886fd8e45SChaoren Lin log->Printf ("NativeProcessLinux::%s() got exit signal(%d) , tid = %" PRIu64 " (%s main thread)", __FUNCTION__, signal, pid, is_main_thread ? "is" : "is not"); 889af245d11STodd Fiala 890af245d11STodd Fiala // This is a thread that exited. Ensure we're not tracking it anymore. 8911107b5a5SPavel Labath const bool thread_found = StopTrackingThread (pid); 892af245d11STodd Fiala 893af245d11STodd Fiala if (is_main_thread) 894af245d11STodd Fiala { 895af245d11STodd Fiala // We only set the exit status and notify the delegate if we haven't already set the process 896af245d11STodd Fiala // state to an exited state. We normally should have received a SIGTRAP | (PTRACE_EVENT_EXIT << 8) 897af245d11STodd Fiala // for the main thread. 8981107b5a5SPavel Labath const bool already_notified = (GetState() == StateType::eStateExited) || (GetState () == StateType::eStateCrashed); 899af245d11STodd Fiala if (!already_notified) 900af245d11STodd Fiala { 901af245d11STodd Fiala if (log) 9021107b5a5SPavel Labath log->Printf ("NativeProcessLinux::%s() tid = %" PRIu64 " handling main thread exit (%s), expected exit state already set but state was %s instead, setting exit state now", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found", StateAsCString (GetState ())); 903af245d11STodd Fiala // The main thread exited. We're done monitoring. Report to delegate. 9041107b5a5SPavel Labath SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); 905af245d11STodd Fiala 906af245d11STodd Fiala // Notify delegate that our process has exited. 9071107b5a5SPavel Labath SetState (StateType::eStateExited, true); 908af245d11STodd Fiala } 909af245d11STodd Fiala else 910af245d11STodd Fiala { 911af245d11STodd Fiala if (log) 912af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() tid = %" PRIu64 " main thread now exited (%s)", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found"); 913af245d11STodd Fiala } 914af245d11STodd Fiala } 915af245d11STodd Fiala else 916af245d11STodd Fiala { 917af245d11STodd Fiala // Do we want to report to the delegate in this case? I think not. If this was an orderly 918af245d11STodd Fiala // thread exit, we would already have received the SIGTRAP | (PTRACE_EVENT_EXIT << 8) signal, 919af245d11STodd Fiala // and we would have done an all-stop then. 920af245d11STodd Fiala if (log) 921af245d11STodd 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"); 922af245d11STodd Fiala } 9231107b5a5SPavel Labath return; 924af245d11STodd Fiala } 925af245d11STodd Fiala 926af245d11STodd Fiala siginfo_t info; 927b9cc0c75SPavel Labath const auto info_err = GetSignalInfo(pid, &info); 928b9cc0c75SPavel Labath auto thread_sp = GetThreadByID(pid); 929b9cc0c75SPavel Labath 930b9cc0c75SPavel Labath if (! thread_sp) 931b9cc0c75SPavel Labath { 932b9cc0c75SPavel Labath // Normally, the only situation when we cannot find the thread is if we have just 933b9cc0c75SPavel Labath // received a new thread notification. This is indicated by GetSignalInfo() returning 934b9cc0c75SPavel Labath // si_code == SI_USER and si_pid == 0 935b9cc0c75SPavel Labath if (log) 936b9cc0c75SPavel Labath log->Printf("NativeProcessLinux::%s received notification about an unknown tid %" PRIu64 ".", __FUNCTION__, pid); 937b9cc0c75SPavel Labath 938b9cc0c75SPavel Labath if (info_err.Fail()) 939b9cc0c75SPavel Labath { 940b9cc0c75SPavel Labath if (log) 941b9cc0c75SPavel Labath log->Printf("NativeProcessLinux::%s (tid %" PRIu64 ") GetSignalInfo failed (%s). Ingoring this notification.", __FUNCTION__, pid, info_err.AsCString()); 942b9cc0c75SPavel Labath return; 943b9cc0c75SPavel Labath } 944b9cc0c75SPavel Labath 945b9cc0c75SPavel Labath if (log && (info.si_code != SI_USER || info.si_pid != 0)) 946b9cc0c75SPavel Labath log->Printf("NativeProcessLinux::%s (tid %" PRIu64 ") unexpected signal info (si_code: %d, si_pid: %d). Treating as a new thread notification anyway.", __FUNCTION__, pid, info.si_code, info.si_pid); 947b9cc0c75SPavel Labath 948b9cc0c75SPavel Labath auto thread_sp = AddThread(pid); 949b9cc0c75SPavel Labath // Resume the newly created thread. 950b9cc0c75SPavel Labath ResumeThread(*thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); 951b9cc0c75SPavel Labath ThreadWasCreated(*thread_sp); 952b9cc0c75SPavel Labath return; 953b9cc0c75SPavel Labath } 954b9cc0c75SPavel Labath 955b9cc0c75SPavel Labath // Get details on the signal raised. 956b9cc0c75SPavel Labath if (info_err.Success()) 957fa03ad2eSChaoren Lin { 958fa03ad2eSChaoren Lin // We have retrieved the signal info. Dispatch appropriately. 959fa03ad2eSChaoren Lin if (info.si_signo == SIGTRAP) 960b9cc0c75SPavel Labath MonitorSIGTRAP(info, *thread_sp); 961fa03ad2eSChaoren Lin else 962b9cc0c75SPavel Labath MonitorSignal(info, *thread_sp, exited); 963fa03ad2eSChaoren Lin } 964fa03ad2eSChaoren Lin else 965af245d11STodd Fiala { 966b9cc0c75SPavel Labath if (info_err.GetError() == EINVAL) 967af245d11STodd Fiala { 968fa03ad2eSChaoren Lin // This is a group stop reception for this tid. 96939036ac3SPavel Labath // We can reach here if we reinject SIGSTOP, SIGSTP, SIGTTIN or SIGTTOU into the 97039036ac3SPavel Labath // tracee, triggering the group-stop mechanism. Normally receiving these would stop 97139036ac3SPavel Labath // the process, pending a SIGCONT. Simulating this state in a debugger is hard and is 97239036ac3SPavel Labath // generally not needed (one use case is debugging background task being managed by a 97339036ac3SPavel Labath // shell). For general use, it is sufficient to stop the process in a signal-delivery 97439036ac3SPavel Labath // stop which happens before the group stop. This done by MonitorSignal and works 97539036ac3SPavel Labath // correctly for all signals. 976fa03ad2eSChaoren Lin if (log) 97739036ac3SPavel Labath log->Printf("NativeProcessLinux::%s received a group stop for pid %" PRIu64 " tid %" PRIu64 ". Transparent handling of group stops not supported, resuming the thread.", __FUNCTION__, GetID (), pid); 978b9cc0c75SPavel Labath ResumeThread(*thread_sp, thread_sp->GetState(), LLDB_INVALID_SIGNAL_NUMBER); 979a9882ceeSTodd Fiala } 980a9882ceeSTodd Fiala else 981a9882ceeSTodd Fiala { 982af245d11STodd Fiala // ptrace(GETSIGINFO) failed (but not due to group-stop). 983af245d11STodd Fiala 984af245d11STodd Fiala // A return value of ESRCH means the thread/process is no longer on the system, 985af245d11STodd Fiala // so it was killed somehow outside of our control. Either way, we can't do anything 986af245d11STodd Fiala // with it anymore. 987af245d11STodd Fiala 988af245d11STodd Fiala // Stop tracking the metadata for the thread since it's entirely off the system now. 9891107b5a5SPavel Labath const bool thread_found = StopTrackingThread (pid); 990af245d11STodd Fiala 991af245d11STodd Fiala if (log) 992af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d (%s, %s, %s)", 993b9cc0c75SPavel Labath __FUNCTION__, info_err.AsCString(), pid, signal, status, info_err.GetError() == ESRCH ? "thread/process killed" : "unknown reason", is_main_thread ? "is main thread" : "is not main thread", thread_found ? "thread metadata removed" : "thread metadata not found"); 994af245d11STodd Fiala 995af245d11STodd Fiala if (is_main_thread) 996af245d11STodd Fiala { 997af245d11STodd Fiala // Notify the delegate - our process is not available but appears to have been killed outside 998af245d11STodd Fiala // our control. Is eStateExited the right exit state in this case? 9991107b5a5SPavel Labath SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); 10001107b5a5SPavel Labath SetState (StateType::eStateExited, true); 1001af245d11STodd Fiala } 1002af245d11STodd Fiala else 1003af245d11STodd Fiala { 1004af245d11STodd Fiala // This thread was pulled out from underneath us. Anything to do here? Do we want to do an all stop? 1005af245d11STodd Fiala if (log) 10061107b5a5SPavel Labath log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 " non-main thread exit occurred, didn't tell delegate anything since thread disappeared out from underneath us", __FUNCTION__, GetID (), pid); 1007af245d11STodd Fiala } 1008af245d11STodd Fiala } 1009af245d11STodd Fiala } 1010af245d11STodd Fiala } 1011af245d11STodd Fiala 1012af245d11STodd Fiala void 1013426bdf88SPavel Labath NativeProcessLinux::WaitForNewThread(::pid_t tid) 1014426bdf88SPavel Labath { 1015426bdf88SPavel Labath Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1016426bdf88SPavel Labath 1017f9077782SPavel Labath NativeThreadLinuxSP new_thread_sp = GetThreadByID(tid); 1018426bdf88SPavel Labath 1019426bdf88SPavel Labath if (new_thread_sp) 1020426bdf88SPavel Labath { 1021426bdf88SPavel Labath // We are already tracking the thread - we got the event on the new thread (see 1022426bdf88SPavel Labath // MonitorSignal) before this one. We are done. 1023426bdf88SPavel Labath return; 1024426bdf88SPavel Labath } 1025426bdf88SPavel Labath 1026426bdf88SPavel Labath // The thread is not tracked yet, let's wait for it to appear. 1027426bdf88SPavel Labath int status = -1; 1028426bdf88SPavel Labath ::pid_t wait_pid; 1029426bdf88SPavel Labath do 1030426bdf88SPavel Labath { 1031426bdf88SPavel Labath if (log) 1032426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() received thread creation event for tid %" PRIu32 ". tid not tracked yet, waiting for thread to appear...", __FUNCTION__, tid); 1033426bdf88SPavel Labath wait_pid = waitpid(tid, &status, __WALL); 1034426bdf88SPavel Labath } 1035426bdf88SPavel Labath while (wait_pid == -1 && errno == EINTR); 1036426bdf88SPavel Labath // Since we are waiting on a specific tid, this must be the creation event. But let's do 1037426bdf88SPavel Labath // some checks just in case. 1038426bdf88SPavel Labath if (wait_pid != tid) { 1039426bdf88SPavel Labath if (log) 1040426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() waiting for tid %" PRIu32 " failed. Assuming the thread has disappeared in the meantime", __FUNCTION__, tid); 1041426bdf88SPavel Labath // The only way I know of this could happen is if the whole process was 1042426bdf88SPavel Labath // SIGKILLed in the mean time. In any case, we can't do anything about that now. 1043426bdf88SPavel Labath return; 1044426bdf88SPavel Labath } 1045426bdf88SPavel Labath if (WIFEXITED(status)) 1046426bdf88SPavel Labath { 1047426bdf88SPavel Labath if (log) 1048426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() waiting for tid %" PRIu32 " returned an 'exited' event. Not tracking the thread.", __FUNCTION__, tid); 1049426bdf88SPavel Labath // Also a very improbable event. 1050426bdf88SPavel Labath return; 1051426bdf88SPavel Labath } 1052426bdf88SPavel Labath 1053426bdf88SPavel Labath siginfo_t info; 1054426bdf88SPavel Labath Error error = GetSignalInfo(tid, &info); 1055426bdf88SPavel Labath if (error.Fail()) 1056426bdf88SPavel Labath { 1057426bdf88SPavel Labath if (log) 1058426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() GetSignalInfo for tid %" PRIu32 " failed. Assuming the thread has disappeared in the meantime.", __FUNCTION__, tid); 1059426bdf88SPavel Labath return; 1060426bdf88SPavel Labath } 1061426bdf88SPavel Labath 1062426bdf88SPavel Labath if (((info.si_pid != 0) || (info.si_code != SI_USER)) && log) 1063426bdf88SPavel Labath { 1064426bdf88SPavel Labath // We should be getting a thread creation signal here, but we received something 1065426bdf88SPavel Labath // else. There isn't much we can do about it now, so we will just log that. Since the 1066426bdf88SPavel Labath // thread is alive and we are receiving events from it, we shall pretend that it was 1067426bdf88SPavel Labath // created properly. 1068426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() GetSignalInfo for tid %" PRIu32 " received unexpected signal with code %d from pid %d.", __FUNCTION__, tid, info.si_code, info.si_pid); 1069426bdf88SPavel Labath } 1070426bdf88SPavel Labath 1071426bdf88SPavel Labath if (log) 1072426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 ": tracking new thread tid %" PRIu32, 1073426bdf88SPavel Labath __FUNCTION__, GetID (), tid); 1074426bdf88SPavel Labath 1075f9077782SPavel Labath new_thread_sp = AddThread(tid); 1076b9cc0c75SPavel Labath ResumeThread(*new_thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); 1077f9077782SPavel Labath ThreadWasCreated(*new_thread_sp); 1078426bdf88SPavel Labath } 1079426bdf88SPavel Labath 1080426bdf88SPavel Labath void 1081b9cc0c75SPavel Labath NativeProcessLinux::MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread) 1082af245d11STodd Fiala { 1083af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1084b9cc0c75SPavel Labath const bool is_main_thread = (thread.GetID() == GetID ()); 1085af245d11STodd Fiala 1086b9cc0c75SPavel Labath assert(info.si_signo == SIGTRAP && "Unexpected child signal!"); 1087af245d11STodd Fiala 1088b9cc0c75SPavel Labath switch (info.si_code) 1089af245d11STodd Fiala { 1090af245d11STodd 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. 1091af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)): 1092af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)): 1093af245d11STodd Fiala 1094af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)): 1095af245d11STodd Fiala { 10965fd24c67SPavel Labath // This is the notification on the parent thread which informs us of new thread 1097426bdf88SPavel Labath // creation. 1098426bdf88SPavel Labath // We don't want to do anything with the parent thread so we just resume it. In case we 1099426bdf88SPavel Labath // want to implement "break on thread creation" functionality, we would need to stop 1100426bdf88SPavel Labath // here. 1101af245d11STodd Fiala 1102af245d11STodd Fiala unsigned long event_message = 0; 1103b9cc0c75SPavel Labath if (GetEventMessage(thread.GetID(), &event_message).Fail()) 1104fa03ad2eSChaoren Lin { 1105426bdf88SPavel Labath if (log) 1106b9cc0c75SPavel Labath log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " received thread creation event but GetEventMessage failed so we don't know the new tid", __FUNCTION__, thread.GetID()); 1107426bdf88SPavel Labath } else 1108426bdf88SPavel Labath WaitForNewThread(event_message); 1109af245d11STodd Fiala 1110b9cc0c75SPavel Labath ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); 1111af245d11STodd Fiala break; 1112af245d11STodd Fiala } 1113af245d11STodd Fiala 1114af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): 1115a9882ceeSTodd Fiala { 1116f9077782SPavel Labath NativeThreadLinuxSP main_thread_sp; 1117af245d11STodd Fiala if (log) 1118b9cc0c75SPavel Labath log->Printf ("NativeProcessLinux::%s() received exec event, code = %d", __FUNCTION__, info.si_code ^ SIGTRAP); 1119a9882ceeSTodd Fiala 11201dbc6c9cSPavel Labath // Exec clears any pending notifications. 11210e1d729bSPavel Labath m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 1122fa03ad2eSChaoren Lin 112357a77118SPavel Labath // Remove all but the main thread here. Linux fork creates a new process which only copies the main thread. 1124a9882ceeSTodd Fiala if (log) 1125a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s exec received, stop tracking all but main thread", __FUNCTION__); 1126a9882ceeSTodd Fiala 1127a9882ceeSTodd Fiala for (auto thread_sp : m_threads) 1128a9882ceeSTodd Fiala { 1129a9882ceeSTodd Fiala const bool is_main_thread = thread_sp && thread_sp->GetID () == GetID (); 1130a9882ceeSTodd Fiala if (is_main_thread) 1131a9882ceeSTodd Fiala { 1132f9077782SPavel Labath main_thread_sp = std::static_pointer_cast<NativeThreadLinux>(thread_sp); 1133a9882ceeSTodd Fiala if (log) 1134a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s found main thread with tid %" PRIu64 ", keeping", __FUNCTION__, main_thread_sp->GetID ()); 1135a9882ceeSTodd Fiala } 1136a9882ceeSTodd Fiala else 1137a9882ceeSTodd Fiala { 1138a9882ceeSTodd Fiala if (log) 1139a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s discarding non-main-thread tid %" PRIu64 " due to exec", __FUNCTION__, thread_sp->GetID ()); 1140a9882ceeSTodd Fiala } 1141a9882ceeSTodd Fiala } 1142a9882ceeSTodd Fiala 1143a9882ceeSTodd Fiala m_threads.clear (); 1144a9882ceeSTodd Fiala 1145a9882ceeSTodd Fiala if (main_thread_sp) 1146a9882ceeSTodd Fiala { 1147a9882ceeSTodd Fiala m_threads.push_back (main_thread_sp); 1148a9882ceeSTodd Fiala SetCurrentThreadID (main_thread_sp->GetID ()); 1149f9077782SPavel Labath main_thread_sp->SetStoppedByExec(); 1150a9882ceeSTodd Fiala } 1151a9882ceeSTodd Fiala else 1152a9882ceeSTodd Fiala { 1153a9882ceeSTodd Fiala SetCurrentThreadID (LLDB_INVALID_THREAD_ID); 1154a9882ceeSTodd Fiala if (log) 1155a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 "no main thread found, discarded all threads, we're in a no-thread state!", __FUNCTION__, GetID ()); 1156a9882ceeSTodd Fiala } 1157a9882ceeSTodd Fiala 1158fa03ad2eSChaoren Lin // Tell coordinator about about the "new" (since exec) stopped main thread. 1159f9077782SPavel Labath ThreadWasCreated(*main_thread_sp); 1160fa03ad2eSChaoren Lin 1161a9882ceeSTodd Fiala // Let our delegate know we have just exec'd. 1162a9882ceeSTodd Fiala NotifyDidExec (); 1163a9882ceeSTodd Fiala 1164a9882ceeSTodd Fiala // If we have a main thread, indicate we are stopped. 1165a9882ceeSTodd Fiala assert (main_thread_sp && "exec called during ptraced process but no main thread metadata tracked"); 1166fa03ad2eSChaoren Lin 1167fa03ad2eSChaoren Lin // Let the process know we're stopped. 1168b9cc0c75SPavel Labath StopRunningThreads(main_thread_sp->GetID()); 1169a9882ceeSTodd Fiala 1170af245d11STodd Fiala break; 1171a9882ceeSTodd Fiala } 1172af245d11STodd Fiala 1173af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): 1174af245d11STodd Fiala { 1175af245d11STodd Fiala // The inferior process or one of its threads is about to exit. 11766e35163cSPavel Labath // We don't want to do anything with the thread so we just resume it. In case we 11776e35163cSPavel Labath // want to implement "break on thread exit" functionality, we would need to stop 11786e35163cSPavel Labath // here. 1179fa03ad2eSChaoren Lin 1180af245d11STodd Fiala unsigned long data = 0; 1181b9cc0c75SPavel Labath if (GetEventMessage(thread.GetID(), &data).Fail()) 1182af245d11STodd Fiala data = -1; 1183af245d11STodd Fiala 1184af245d11STodd Fiala if (log) 1185af245d11STodd Fiala { 1186af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received PTRACE_EVENT_EXIT, data = %lx (WIFEXITED=%s,WIFSIGNALED=%s), pid = %" PRIu64 " (%s)", 1187af245d11STodd Fiala __FUNCTION__, 1188af245d11STodd Fiala data, WIFEXITED (data) ? "true" : "false", WIFSIGNALED (data) ? "true" : "false", 1189b9cc0c75SPavel Labath thread.GetID(), 1190af245d11STodd Fiala is_main_thread ? "is main thread" : "not main thread"); 1191af245d11STodd Fiala } 1192af245d11STodd Fiala 1193af245d11STodd Fiala if (is_main_thread) 1194af245d11STodd Fiala { 1195af245d11STodd Fiala SetExitStatus (convert_pid_status_to_exit_type (data), convert_pid_status_to_return_code (data), nullptr, true); 119675f47c3aSTodd Fiala } 119775f47c3aSTodd Fiala 119886852d36SPavel Labath StateType state = thread.GetState(); 119986852d36SPavel Labath if (! StateIsRunningState(state)) 120086852d36SPavel Labath { 120186852d36SPavel Labath // Due to a kernel bug, we may sometimes get this stop after the inferior gets a 120286852d36SPavel Labath // SIGKILL. This confuses our state tracking logic in ResumeThread(), since normally, 120386852d36SPavel Labath // we should not be receiving any ptrace events while the inferior is stopped. This 120486852d36SPavel Labath // makes sure that the inferior is resumed and exits normally. 120586852d36SPavel Labath state = eStateRunning; 120686852d36SPavel Labath } 120786852d36SPavel Labath ResumeThread(thread, state, LLDB_INVALID_SIGNAL_NUMBER); 1208af245d11STodd Fiala 1209af245d11STodd Fiala break; 1210af245d11STodd Fiala } 1211af245d11STodd Fiala 1212af245d11STodd Fiala case 0: 1213c16f5dcaSChaoren Lin case TRAP_TRACE: // We receive this on single stepping. 1214c16f5dcaSChaoren Lin case TRAP_HWBKPT: // We receive this on watchpoint hit 121586fd8e45SChaoren Lin { 1216c16f5dcaSChaoren Lin // If a watchpoint was hit, report it 1217c16f5dcaSChaoren Lin uint32_t wp_index; 12181fa5c4b9STamas Berghammer Error error = thread.GetRegisterContext()->GetWatchpointHitIndex(wp_index, (uintptr_t)info.si_addr); 1219c16f5dcaSChaoren Lin if (error.Fail() && log) 1220c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() " 1221c16f5dcaSChaoren Lin "received error while checking for watchpoint hits, " 1222c16f5dcaSChaoren Lin "pid = %" PRIu64 " error = %s", 1223b9cc0c75SPavel Labath __FUNCTION__, thread.GetID(), error.AsCString()); 1224c16f5dcaSChaoren Lin if (wp_index != LLDB_INVALID_INDEX32) 12255830aa75STamas Berghammer { 1226b9cc0c75SPavel Labath MonitorWatchpoint(thread, wp_index); 1227c16f5dcaSChaoren Lin break; 1228c16f5dcaSChaoren Lin } 1229b9cc0c75SPavel Labath 1230be379e15STamas Berghammer // Otherwise, report step over 1231be379e15STamas Berghammer MonitorTrace(thread); 1232af245d11STodd Fiala break; 1233b9cc0c75SPavel Labath } 1234af245d11STodd Fiala 1235af245d11STodd Fiala case SI_KERNEL: 123635799963SMohit K. Bhakkad #if defined __mips__ 123735799963SMohit K. Bhakkad // For mips there is no special signal for watchpoint 123835799963SMohit K. Bhakkad // So we check for watchpoint in kernel trap 123935799963SMohit K. Bhakkad { 124035799963SMohit K. Bhakkad // If a watchpoint was hit, report it 124135799963SMohit K. Bhakkad uint32_t wp_index; 1242b9cc0c75SPavel Labath Error error = thread.GetRegisterContext()->GetWatchpointHitIndex(wp_index, LLDB_INVALID_ADDRESS); 124335799963SMohit K. Bhakkad if (error.Fail() && log) 124435799963SMohit K. Bhakkad log->Printf("NativeProcessLinux::%s() " 124535799963SMohit K. Bhakkad "received error while checking for watchpoint hits, " 124635799963SMohit K. Bhakkad "pid = %" PRIu64 " error = %s", 124716ad0321SMohit K. Bhakkad __FUNCTION__, thread.GetID(), error.AsCString()); 124835799963SMohit K. Bhakkad if (wp_index != LLDB_INVALID_INDEX32) 124935799963SMohit K. Bhakkad { 1250b9cc0c75SPavel Labath MonitorWatchpoint(thread, wp_index); 125135799963SMohit K. Bhakkad break; 125235799963SMohit K. Bhakkad } 125335799963SMohit K. Bhakkad } 125435799963SMohit K. Bhakkad // NO BREAK 125535799963SMohit K. Bhakkad #endif 1256af245d11STodd Fiala case TRAP_BRKPT: 1257b9cc0c75SPavel Labath MonitorBreakpoint(thread); 1258af245d11STodd Fiala break; 1259af245d11STodd Fiala 1260af245d11STodd Fiala case SIGTRAP: 1261af245d11STodd Fiala case (SIGTRAP | 0x80): 1262af245d11STodd Fiala if (log) 1263b9cc0c75SPavel Labath log->Printf ("NativeProcessLinux::%s() received unknown SIGTRAP system call stop event, pid %" PRIu64 "tid %" PRIu64 ", resuming", __FUNCTION__, GetID (), thread.GetID()); 1264fa03ad2eSChaoren Lin 1265af245d11STodd Fiala // Ignore these signals until we know more about them. 1266b9cc0c75SPavel Labath ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); 1267af245d11STodd Fiala break; 1268af245d11STodd Fiala 1269af245d11STodd Fiala default: 1270af245d11STodd Fiala assert(false && "Unexpected SIGTRAP code!"); 1271af245d11STodd Fiala if (log) 12726e35163cSPavel Labath log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 "tid %" PRIu64 " received unhandled SIGTRAP code: 0x%d", 1273b9cc0c75SPavel Labath __FUNCTION__, GetID(), thread.GetID(), info.si_code); 1274af245d11STodd Fiala break; 1275af245d11STodd Fiala 1276af245d11STodd Fiala } 1277af245d11STodd Fiala } 1278af245d11STodd Fiala 1279af245d11STodd Fiala void 1280b9cc0c75SPavel Labath NativeProcessLinux::MonitorTrace(NativeThreadLinux &thread) 1281c16f5dcaSChaoren Lin { 1282c16f5dcaSChaoren Lin Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 1283c16f5dcaSChaoren Lin if (log) 1284c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() received trace event, pid = %" PRIu64 " (single stepping)", 1285b9cc0c75SPavel Labath __FUNCTION__, thread.GetID()); 1286c16f5dcaSChaoren Lin 12870e1d729bSPavel Labath // This thread is currently stopped. 1288b9cc0c75SPavel Labath thread.SetStoppedByTrace(); 1289c16f5dcaSChaoren Lin 1290b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 1291c16f5dcaSChaoren Lin } 1292c16f5dcaSChaoren Lin 1293c16f5dcaSChaoren Lin void 1294b9cc0c75SPavel Labath NativeProcessLinux::MonitorBreakpoint(NativeThreadLinux &thread) 1295c16f5dcaSChaoren Lin { 1296c16f5dcaSChaoren Lin Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 1297c16f5dcaSChaoren Lin if (log) 1298c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() received breakpoint event, pid = %" PRIu64, 1299b9cc0c75SPavel Labath __FUNCTION__, thread.GetID()); 1300c16f5dcaSChaoren Lin 1301c16f5dcaSChaoren Lin // Mark the thread as stopped at breakpoint. 1302b9cc0c75SPavel Labath thread.SetStoppedByBreakpoint(); 1303b9cc0c75SPavel Labath Error error = FixupBreakpointPCAsNeeded(thread); 1304c16f5dcaSChaoren Lin if (error.Fail()) 1305c16f5dcaSChaoren Lin if (log) 1306c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " fixup: %s", 1307b9cc0c75SPavel Labath __FUNCTION__, thread.GetID(), error.AsCString()); 1308d8c338d4STamas Berghammer 1309b9cc0c75SPavel Labath if (m_threads_stepping_with_breakpoint.find(thread.GetID()) != m_threads_stepping_with_breakpoint.end()) 1310b9cc0c75SPavel Labath thread.SetStoppedByTrace(); 1311c16f5dcaSChaoren Lin 1312b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 1313c16f5dcaSChaoren Lin } 1314c16f5dcaSChaoren Lin 1315c16f5dcaSChaoren Lin void 1316f9077782SPavel Labath NativeProcessLinux::MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index) 1317c16f5dcaSChaoren Lin { 1318c16f5dcaSChaoren Lin Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_WATCHPOINTS)); 1319c16f5dcaSChaoren Lin if (log) 1320c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() received watchpoint event, " 1321c16f5dcaSChaoren Lin "pid = %" PRIu64 ", wp_index = %" PRIu32, 1322f9077782SPavel Labath __FUNCTION__, thread.GetID(), wp_index); 1323c16f5dcaSChaoren Lin 1324c16f5dcaSChaoren Lin // Mark the thread as stopped at watchpoint. 1325c16f5dcaSChaoren Lin // The address is at (lldb::addr_t)info->si_addr if we need it. 1326f9077782SPavel Labath thread.SetStoppedByWatchpoint(wp_index); 1327c16f5dcaSChaoren Lin 1328c16f5dcaSChaoren Lin // We need to tell all other running threads before we notify the delegate about this stop. 1329f9077782SPavel Labath StopRunningThreads(thread.GetID()); 1330c16f5dcaSChaoren Lin } 1331c16f5dcaSChaoren Lin 1332c16f5dcaSChaoren Lin void 1333b9cc0c75SPavel Labath NativeProcessLinux::MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread, bool exited) 1334af245d11STodd Fiala { 1335b9cc0c75SPavel Labath const int signo = info.si_signo; 1336b9cc0c75SPavel Labath const bool is_from_llgs = info.si_pid == getpid (); 1337af245d11STodd Fiala 1338af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1339af245d11STodd Fiala 1340af245d11STodd Fiala // POSIX says that process behaviour is undefined after it ignores a SIGFPE, 1341af245d11STodd Fiala // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a 1342af245d11STodd Fiala // kill(2) or raise(3). Similarly for tgkill(2) on Linux. 1343af245d11STodd Fiala // 1344af245d11STodd Fiala // IOW, user generated signals never generate what we consider to be a 1345af245d11STodd Fiala // "crash". 1346af245d11STodd Fiala // 1347af245d11STodd Fiala // Similarly, ACK signals generated by this monitor. 1348af245d11STodd Fiala 1349af245d11STodd Fiala // Handle the signal. 1350b9cc0c75SPavel Labath if (info.si_code == SI_TKILL || info.si_code == SI_USER) 1351af245d11STodd Fiala { 1352af245d11STodd Fiala if (log) 1353af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received signal %s (%d) with code %s, (siginfo pid = %d (%s), waitpid pid = %" PRIu64 ")", 1354af245d11STodd Fiala __FUNCTION__, 135598d0a4b3SChaoren Lin Host::GetSignalAsCString(signo), 1356af245d11STodd Fiala signo, 1357b9cc0c75SPavel Labath (info.si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"), 1358b9cc0c75SPavel Labath info.si_pid, 1359511e5cdcSTodd Fiala is_from_llgs ? "from llgs" : "not from llgs", 1360b9cc0c75SPavel Labath thread.GetID()); 1361af245d11STodd Fiala } 136258a2f669STodd Fiala 136358a2f669STodd Fiala // Check for thread stop notification. 1364b9cc0c75SPavel Labath if (is_from_llgs && (info.si_code == SI_TKILL) && (signo == SIGSTOP)) 1365af245d11STodd Fiala { 1366af245d11STodd Fiala // This is a tgkill()-based stop. 1367fa03ad2eSChaoren Lin if (log) 1368fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 ", thread stopped", 1369fa03ad2eSChaoren Lin __FUNCTION__, 1370fa03ad2eSChaoren Lin GetID (), 1371b9cc0c75SPavel Labath thread.GetID()); 1372fa03ad2eSChaoren Lin 1373aab58633SChaoren Lin // Check that we're not already marked with a stop reason. 1374aab58633SChaoren Lin // Note this thread really shouldn't already be marked as stopped - if we were, that would imply that 1375aab58633SChaoren Lin // the kernel signaled us with the thread stopping which we handled and marked as stopped, 1376aab58633SChaoren Lin // and that, without an intervening resume, we received another stop. It is more likely 1377aab58633SChaoren Lin // that we are missing the marking of a run state somewhere if we find that the thread was 1378aab58633SChaoren Lin // marked as stopped. 1379b9cc0c75SPavel Labath const StateType thread_state = thread.GetState(); 1380aab58633SChaoren Lin if (!StateIsStoppedState (thread_state, false)) 1381aab58633SChaoren Lin { 1382ed89c7feSPavel Labath // An inferior thread has stopped because of a SIGSTOP we have sent it. 1383ed89c7feSPavel Labath // Generally, these are not important stops and we don't want to report them as 1384ed89c7feSPavel Labath // they are just used to stop other threads when one thread (the one with the 1385ed89c7feSPavel Labath // *real* stop reason) hits a breakpoint (watchpoint, etc...). However, in the 1386ed89c7feSPavel Labath // case of an asynchronous Interrupt(), this *is* the real stop reason, so we 1387ed89c7feSPavel Labath // leave the signal intact if this is the thread that was chosen as the 1388ed89c7feSPavel Labath // triggering thread. 13890e1d729bSPavel Labath if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) 13900e1d729bSPavel Labath { 1391b9cc0c75SPavel Labath if (m_pending_notification_tid == thread.GetID()) 1392b9cc0c75SPavel Labath thread.SetStoppedBySignal(SIGSTOP, &info); 1393ed89c7feSPavel Labath else 1394b9cc0c75SPavel Labath thread.SetStoppedWithNoReason(); 1395ed89c7feSPavel Labath 1396b9cc0c75SPavel Labath SetCurrentThreadID (thread.GetID ()); 13970e1d729bSPavel Labath SignalIfAllThreadsStopped(); 13980e1d729bSPavel Labath } 13990e1d729bSPavel Labath else 14000e1d729bSPavel Labath { 14010e1d729bSPavel Labath // We can end up here if stop was initiated by LLGS but by this time a 14020e1d729bSPavel Labath // thread stop has occurred - maybe initiated by another event. 1403b9cc0c75SPavel Labath Error error = ResumeThread(thread, thread.GetState(), 0); 14040e1d729bSPavel Labath if (error.Fail() && log) 14050e1d729bSPavel Labath { 14060e1d729bSPavel Labath log->Printf("NativeProcessLinux::%s failed to resume thread tid %" PRIu64 ": %s", 1407b9cc0c75SPavel Labath __FUNCTION__, thread.GetID(), error.AsCString()); 14080e1d729bSPavel Labath } 14090e1d729bSPavel Labath } 1410aab58633SChaoren Lin } 1411aab58633SChaoren Lin else 1412aab58633SChaoren Lin { 1413aab58633SChaoren Lin if (log) 1414aab58633SChaoren Lin { 1415aab58633SChaoren Lin // Retrieve the signal name if the thread was stopped by a signal. 1416aab58633SChaoren Lin int stop_signo = 0; 1417b9cc0c75SPavel Labath const bool stopped_by_signal = thread.IsStopped(&stop_signo); 141898d0a4b3SChaoren Lin const char *signal_name = stopped_by_signal ? Host::GetSignalAsCString(stop_signo) : "<not stopped by signal>"; 1419aab58633SChaoren Lin if (!signal_name) 1420aab58633SChaoren Lin signal_name = "<no-signal-name>"; 1421aab58633SChaoren Lin 1422aab58633SChaoren Lin log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 ", thread was already marked as a stopped state (state=%s, signal=%d (%s)), leaving stop signal as is", 1423aab58633SChaoren Lin __FUNCTION__, 1424aab58633SChaoren Lin GetID (), 1425b9cc0c75SPavel Labath thread.GetID(), 1426aab58633SChaoren Lin StateAsCString (thread_state), 1427aab58633SChaoren Lin stop_signo, 1428aab58633SChaoren Lin signal_name); 1429aab58633SChaoren Lin } 14300e1d729bSPavel Labath SignalIfAllThreadsStopped(); 1431af245d11STodd Fiala } 1432af245d11STodd Fiala 143358a2f669STodd Fiala // Done handling. 1434af245d11STodd Fiala return; 1435af245d11STodd Fiala } 1436af245d11STodd Fiala 1437af245d11STodd Fiala if (log) 143898d0a4b3SChaoren Lin log->Printf ("NativeProcessLinux::%s() received signal %s", __FUNCTION__, Host::GetSignalAsCString(signo)); 1439af245d11STodd Fiala 144086fd8e45SChaoren Lin // This thread is stopped. 1441b9cc0c75SPavel Labath thread.SetStoppedBySignal(signo, &info); 144286fd8e45SChaoren Lin 144386fd8e45SChaoren Lin // Send a stop to the debugger after we get all other threads to stop. 1444b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 1445511e5cdcSTodd Fiala } 1446af245d11STodd Fiala 1447e7708688STamas Berghammer namespace { 1448e7708688STamas Berghammer 1449e7708688STamas Berghammer struct EmulatorBaton 1450e7708688STamas Berghammer { 1451e7708688STamas Berghammer NativeProcessLinux* m_process; 1452e7708688STamas Berghammer NativeRegisterContext* m_reg_context; 14536648fcc3SPavel Labath 14546648fcc3SPavel Labath // eRegisterKindDWARF -> RegsiterValue 14556648fcc3SPavel Labath std::unordered_map<uint32_t, RegisterValue> m_register_values; 1456e7708688STamas Berghammer 1457e7708688STamas Berghammer EmulatorBaton(NativeProcessLinux* process, NativeRegisterContext* reg_context) : 1458e7708688STamas Berghammer m_process(process), m_reg_context(reg_context) {} 1459e7708688STamas Berghammer }; 1460e7708688STamas Berghammer 1461e7708688STamas Berghammer } // anonymous namespace 1462e7708688STamas Berghammer 1463e7708688STamas Berghammer static size_t 1464e7708688STamas Berghammer ReadMemoryCallback (EmulateInstruction *instruction, 1465e7708688STamas Berghammer void *baton, 1466e7708688STamas Berghammer const EmulateInstruction::Context &context, 1467e7708688STamas Berghammer lldb::addr_t addr, 1468e7708688STamas Berghammer void *dst, 1469e7708688STamas Berghammer size_t length) 1470e7708688STamas Berghammer { 1471e7708688STamas Berghammer EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); 1472e7708688STamas Berghammer 14733eb4b458SChaoren Lin size_t bytes_read; 1474e7708688STamas Berghammer emulator_baton->m_process->ReadMemory(addr, dst, length, bytes_read); 1475e7708688STamas Berghammer return bytes_read; 1476e7708688STamas Berghammer } 1477e7708688STamas Berghammer 1478e7708688STamas Berghammer static bool 1479e7708688STamas Berghammer ReadRegisterCallback (EmulateInstruction *instruction, 1480e7708688STamas Berghammer void *baton, 1481e7708688STamas Berghammer const RegisterInfo *reg_info, 1482e7708688STamas Berghammer RegisterValue ®_value) 1483e7708688STamas Berghammer { 1484e7708688STamas Berghammer EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); 1485e7708688STamas Berghammer 14866648fcc3SPavel Labath auto it = emulator_baton->m_register_values.find(reg_info->kinds[eRegisterKindDWARF]); 14876648fcc3SPavel Labath if (it != emulator_baton->m_register_values.end()) 14886648fcc3SPavel Labath { 14896648fcc3SPavel Labath reg_value = it->second; 14906648fcc3SPavel Labath return true; 14916648fcc3SPavel Labath } 14926648fcc3SPavel Labath 1493e7708688STamas Berghammer // The emulator only fill in the dwarf regsiter numbers (and in some case 1494e7708688STamas Berghammer // the generic register numbers). Get the full register info from the 1495e7708688STamas Berghammer // register context based on the dwarf register numbers. 1496e7708688STamas Berghammer const RegisterInfo* full_reg_info = emulator_baton->m_reg_context->GetRegisterInfo( 1497e7708688STamas Berghammer eRegisterKindDWARF, reg_info->kinds[eRegisterKindDWARF]); 1498e7708688STamas Berghammer 1499e7708688STamas Berghammer Error error = emulator_baton->m_reg_context->ReadRegister(full_reg_info, reg_value); 15006648fcc3SPavel Labath if (error.Success()) 15016648fcc3SPavel Labath return true; 1502cdc22a88SMohit K. Bhakkad 15036648fcc3SPavel Labath return false; 1504e7708688STamas Berghammer } 1505e7708688STamas Berghammer 1506e7708688STamas Berghammer static bool 1507e7708688STamas Berghammer WriteRegisterCallback (EmulateInstruction *instruction, 1508e7708688STamas Berghammer void *baton, 1509e7708688STamas Berghammer const EmulateInstruction::Context &context, 1510e7708688STamas Berghammer const RegisterInfo *reg_info, 1511e7708688STamas Berghammer const RegisterValue ®_value) 1512e7708688STamas Berghammer { 1513e7708688STamas Berghammer EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); 15146648fcc3SPavel Labath emulator_baton->m_register_values[reg_info->kinds[eRegisterKindDWARF]] = reg_value; 1515e7708688STamas Berghammer return true; 1516e7708688STamas Berghammer } 1517e7708688STamas Berghammer 1518e7708688STamas Berghammer static size_t 1519e7708688STamas Berghammer WriteMemoryCallback (EmulateInstruction *instruction, 1520e7708688STamas Berghammer void *baton, 1521e7708688STamas Berghammer const EmulateInstruction::Context &context, 1522e7708688STamas Berghammer lldb::addr_t addr, 1523e7708688STamas Berghammer const void *dst, 1524e7708688STamas Berghammer size_t length) 1525e7708688STamas Berghammer { 1526e7708688STamas Berghammer return length; 1527e7708688STamas Berghammer } 1528e7708688STamas Berghammer 1529e7708688STamas Berghammer static lldb::addr_t 1530e7708688STamas Berghammer ReadFlags (NativeRegisterContext* regsiter_context) 1531e7708688STamas Berghammer { 1532e7708688STamas Berghammer const RegisterInfo* flags_info = regsiter_context->GetRegisterInfo( 1533e7708688STamas Berghammer eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 1534e7708688STamas Berghammer return regsiter_context->ReadRegisterAsUnsigned(flags_info, LLDB_INVALID_ADDRESS); 1535e7708688STamas Berghammer } 1536e7708688STamas Berghammer 1537e7708688STamas Berghammer Error 1538b9cc0c75SPavel Labath NativeProcessLinux::SetupSoftwareSingleStepping(NativeThreadLinux &thread) 1539e7708688STamas Berghammer { 1540e7708688STamas Berghammer Error error; 1541b9cc0c75SPavel Labath NativeRegisterContextSP register_context_sp = thread.GetRegisterContext(); 1542e7708688STamas Berghammer 1543e7708688STamas Berghammer std::unique_ptr<EmulateInstruction> emulator_ap( 1544e7708688STamas Berghammer EmulateInstruction::FindPlugin(m_arch, eInstructionTypePCModifying, nullptr)); 1545e7708688STamas Berghammer 1546e7708688STamas Berghammer if (emulator_ap == nullptr) 1547e7708688STamas Berghammer return Error("Instruction emulator not found!"); 1548e7708688STamas Berghammer 1549e7708688STamas Berghammer EmulatorBaton baton(this, register_context_sp.get()); 1550e7708688STamas Berghammer emulator_ap->SetBaton(&baton); 1551e7708688STamas Berghammer emulator_ap->SetReadMemCallback(&ReadMemoryCallback); 1552e7708688STamas Berghammer emulator_ap->SetReadRegCallback(&ReadRegisterCallback); 1553e7708688STamas Berghammer emulator_ap->SetWriteMemCallback(&WriteMemoryCallback); 1554e7708688STamas Berghammer emulator_ap->SetWriteRegCallback(&WriteRegisterCallback); 1555e7708688STamas Berghammer 1556e7708688STamas Berghammer if (!emulator_ap->ReadInstruction()) 1557e7708688STamas Berghammer return Error("Read instruction failed!"); 1558e7708688STamas Berghammer 15596648fcc3SPavel Labath bool emulation_result = emulator_ap->EvaluateInstruction(eEmulateInstructionOptionAutoAdvancePC); 15606648fcc3SPavel Labath 15616648fcc3SPavel Labath const RegisterInfo* reg_info_pc = register_context_sp->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); 15626648fcc3SPavel Labath const RegisterInfo* reg_info_flags = register_context_sp->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 15636648fcc3SPavel Labath 15646648fcc3SPavel Labath auto pc_it = baton.m_register_values.find(reg_info_pc->kinds[eRegisterKindDWARF]); 15656648fcc3SPavel Labath auto flags_it = baton.m_register_values.find(reg_info_flags->kinds[eRegisterKindDWARF]); 15666648fcc3SPavel Labath 1567e7708688STamas Berghammer lldb::addr_t next_pc; 1568e7708688STamas Berghammer lldb::addr_t next_flags; 15696648fcc3SPavel Labath if (emulation_result) 1570e7708688STamas Berghammer { 15716648fcc3SPavel Labath assert(pc_it != baton.m_register_values.end() && "Emulation was successfull but PC wasn't updated"); 15726648fcc3SPavel Labath next_pc = pc_it->second.GetAsUInt64(); 15736648fcc3SPavel Labath 15746648fcc3SPavel Labath if (flags_it != baton.m_register_values.end()) 15756648fcc3SPavel Labath next_flags = flags_it->second.GetAsUInt64(); 1576e7708688STamas Berghammer else 1577e7708688STamas Berghammer next_flags = ReadFlags (register_context_sp.get()); 1578e7708688STamas Berghammer } 15796648fcc3SPavel Labath else if (pc_it == baton.m_register_values.end()) 1580e7708688STamas Berghammer { 1581e7708688STamas Berghammer // Emulate instruction failed and it haven't changed PC. Advance PC 1582e7708688STamas Berghammer // with the size of the current opcode because the emulation of all 1583e7708688STamas Berghammer // PC modifying instruction should be successful. The failure most 1584e7708688STamas Berghammer // likely caused by a not supported instruction which don't modify PC. 1585e7708688STamas Berghammer next_pc = register_context_sp->GetPC() + emulator_ap->GetOpcode().GetByteSize(); 1586e7708688STamas Berghammer next_flags = ReadFlags (register_context_sp.get()); 1587e7708688STamas Berghammer } 1588e7708688STamas Berghammer else 1589e7708688STamas Berghammer { 1590e7708688STamas Berghammer // The instruction emulation failed after it modified the PC. It is an 1591e7708688STamas Berghammer // unknown error where we can't continue because the next instruction is 1592e7708688STamas Berghammer // modifying the PC but we don't know how. 1593e7708688STamas Berghammer return Error ("Instruction emulation failed unexpectedly."); 1594e7708688STamas Berghammer } 1595e7708688STamas Berghammer 1596e7708688STamas Berghammer if (m_arch.GetMachine() == llvm::Triple::arm) 1597e7708688STamas Berghammer { 1598e7708688STamas Berghammer if (next_flags & 0x20) 1599e7708688STamas Berghammer { 1600e7708688STamas Berghammer // Thumb mode 1601e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 2); 1602e7708688STamas Berghammer } 1603e7708688STamas Berghammer else 1604e7708688STamas Berghammer { 1605e7708688STamas Berghammer // Arm mode 1606e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 4); 1607e7708688STamas Berghammer } 1608e7708688STamas Berghammer } 1609cdc22a88SMohit K. Bhakkad else if (m_arch.GetMachine() == llvm::Triple::mips64 1610c60c9452SJaydeep Patil || m_arch.GetMachine() == llvm::Triple::mips64el 1611c60c9452SJaydeep Patil || m_arch.GetMachine() == llvm::Triple::mips 1612c60c9452SJaydeep Patil || m_arch.GetMachine() == llvm::Triple::mipsel) 1613cdc22a88SMohit K. Bhakkad error = SetSoftwareBreakpoint(next_pc, 4); 1614e7708688STamas Berghammer else 1615e7708688STamas Berghammer { 1616e7708688STamas Berghammer // No size hint is given for the next breakpoint 1617e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 0); 1618e7708688STamas Berghammer } 1619e7708688STamas Berghammer 1620e7708688STamas Berghammer if (error.Fail()) 1621e7708688STamas Berghammer return error; 1622e7708688STamas Berghammer 1623b9cc0c75SPavel Labath m_threads_stepping_with_breakpoint.insert({thread.GetID(), next_pc}); 1624e7708688STamas Berghammer 1625e7708688STamas Berghammer return Error(); 1626e7708688STamas Berghammer } 1627e7708688STamas Berghammer 1628e7708688STamas Berghammer bool 1629e7708688STamas Berghammer NativeProcessLinux::SupportHardwareSingleStepping() const 1630e7708688STamas Berghammer { 1631cdc22a88SMohit K. Bhakkad if (m_arch.GetMachine() == llvm::Triple::arm 1632c60c9452SJaydeep Patil || m_arch.GetMachine() == llvm::Triple::mips64 || m_arch.GetMachine() == llvm::Triple::mips64el 1633c60c9452SJaydeep Patil || m_arch.GetMachine() == llvm::Triple::mips || m_arch.GetMachine() == llvm::Triple::mipsel) 1634cdc22a88SMohit K. Bhakkad return false; 1635cdc22a88SMohit K. Bhakkad return true; 1636e7708688STamas Berghammer } 1637e7708688STamas Berghammer 1638af245d11STodd Fiala Error 1639af245d11STodd Fiala NativeProcessLinux::Resume (const ResumeActionList &resume_actions) 1640af245d11STodd Fiala { 1641af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); 1642af245d11STodd Fiala if (log) 1643af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s called: pid %" PRIu64, __FUNCTION__, GetID ()); 1644af245d11STodd Fiala 1645e7708688STamas Berghammer bool software_single_step = !SupportHardwareSingleStepping(); 1646af245d11STodd Fiala 1647e7708688STamas Berghammer if (software_single_step) 1648e7708688STamas Berghammer { 1649e7708688STamas Berghammer for (auto thread_sp : m_threads) 1650e7708688STamas Berghammer { 1651e7708688STamas Berghammer assert (thread_sp && "thread list should not contain NULL threads"); 1652e7708688STamas Berghammer 1653e7708688STamas Berghammer const ResumeAction *const action = resume_actions.GetActionForThread (thread_sp->GetID (), true); 1654e7708688STamas Berghammer if (action == nullptr) 1655e7708688STamas Berghammer continue; 1656e7708688STamas Berghammer 1657e7708688STamas Berghammer if (action->state == eStateStepping) 1658e7708688STamas Berghammer { 1659b9cc0c75SPavel Labath Error error = SetupSoftwareSingleStepping(static_cast<NativeThreadLinux &>(*thread_sp)); 1660e7708688STamas Berghammer if (error.Fail()) 1661e7708688STamas Berghammer return error; 1662e7708688STamas Berghammer } 1663e7708688STamas Berghammer } 1664e7708688STamas Berghammer } 1665e7708688STamas Berghammer 1666af245d11STodd Fiala for (auto thread_sp : m_threads) 1667af245d11STodd Fiala { 1668af245d11STodd Fiala assert (thread_sp && "thread list should not contain NULL threads"); 1669af245d11STodd Fiala 1670af245d11STodd Fiala const ResumeAction *const action = resume_actions.GetActionForThread (thread_sp->GetID (), true); 16716a196ce6SChaoren Lin 16726a196ce6SChaoren Lin if (action == nullptr) 16736a196ce6SChaoren Lin { 16746a196ce6SChaoren Lin if (log) 16756a196ce6SChaoren Lin log->Printf ("NativeProcessLinux::%s no action specified for pid %" PRIu64 " tid %" PRIu64, 16766a196ce6SChaoren Lin __FUNCTION__, GetID (), thread_sp->GetID ()); 16776a196ce6SChaoren Lin continue; 16786a196ce6SChaoren Lin } 1679af245d11STodd Fiala 1680af245d11STodd Fiala if (log) 1681af245d11STodd Fiala { 1682af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s processing resume action state %s for pid %" PRIu64 " tid %" PRIu64, 1683af245d11STodd Fiala __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); 1684af245d11STodd Fiala } 1685af245d11STodd Fiala 1686af245d11STodd Fiala switch (action->state) 1687af245d11STodd Fiala { 1688af245d11STodd Fiala case eStateRunning: 16890e1d729bSPavel Labath case eStateStepping: 1690fa03ad2eSChaoren Lin { 1691af245d11STodd Fiala // Run the thread, possibly feeding it the signal. 1692fa03ad2eSChaoren Lin const int signo = action->signal; 1693b9cc0c75SPavel Labath ResumeThread(static_cast<NativeThreadLinux &>(*thread_sp), action->state, signo); 1694af245d11STodd Fiala break; 1695ae29d395SChaoren Lin } 1696af245d11STodd Fiala 1697af245d11STodd Fiala case eStateSuspended: 1698af245d11STodd Fiala case eStateStopped: 1699108c325dSPavel Labath lldbassert(0 && "Unexpected state"); 1700af245d11STodd Fiala 1701af245d11STodd Fiala default: 1702af245d11STodd Fiala return Error ("NativeProcessLinux::%s (): unexpected state %s specified for pid %" PRIu64 ", tid %" PRIu64, 1703af245d11STodd Fiala __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); 1704af245d11STodd Fiala } 1705af245d11STodd Fiala } 1706af245d11STodd Fiala 17075830aa75STamas Berghammer return Error(); 1708af245d11STodd Fiala } 1709af245d11STodd Fiala 1710af245d11STodd Fiala Error 1711af245d11STodd Fiala NativeProcessLinux::Halt () 1712af245d11STodd Fiala { 1713af245d11STodd Fiala Error error; 1714af245d11STodd Fiala 1715af245d11STodd Fiala if (kill (GetID (), SIGSTOP) != 0) 1716af245d11STodd Fiala error.SetErrorToErrno (); 1717af245d11STodd Fiala 1718af245d11STodd Fiala return error; 1719af245d11STodd Fiala } 1720af245d11STodd Fiala 1721af245d11STodd Fiala Error 1722af245d11STodd Fiala NativeProcessLinux::Detach () 1723af245d11STodd Fiala { 1724af245d11STodd Fiala Error error; 1725af245d11STodd Fiala 1726af245d11STodd Fiala // Stop monitoring the inferior. 172719cbe96aSPavel Labath m_sigchld_handle.reset(); 1728af245d11STodd Fiala 17297a9495bcSPavel Labath // Tell ptrace to detach from the process. 17307a9495bcSPavel Labath if (GetID () == LLDB_INVALID_PROCESS_ID) 17317a9495bcSPavel Labath return error; 17327a9495bcSPavel Labath 17337a9495bcSPavel Labath for (auto thread_sp : m_threads) 17347a9495bcSPavel Labath { 17357a9495bcSPavel Labath Error e = Detach(thread_sp->GetID()); 17367a9495bcSPavel Labath if (e.Fail()) 17377a9495bcSPavel Labath error = e; // Save the error, but still attempt to detach from other threads. 17387a9495bcSPavel Labath } 17397a9495bcSPavel Labath 1740af245d11STodd Fiala return error; 1741af245d11STodd Fiala } 1742af245d11STodd Fiala 1743af245d11STodd Fiala Error 1744af245d11STodd Fiala NativeProcessLinux::Signal (int signo) 1745af245d11STodd Fiala { 1746af245d11STodd Fiala Error error; 1747af245d11STodd Fiala 1748af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1749af245d11STodd Fiala if (log) 1750af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s: sending signal %d (%s) to pid %" PRIu64, 175198d0a4b3SChaoren Lin __FUNCTION__, signo, Host::GetSignalAsCString(signo), GetID()); 1752af245d11STodd Fiala 1753af245d11STodd Fiala if (kill(GetID(), signo)) 1754af245d11STodd Fiala error.SetErrorToErrno(); 1755af245d11STodd Fiala 1756af245d11STodd Fiala return error; 1757af245d11STodd Fiala } 1758af245d11STodd Fiala 1759af245d11STodd Fiala Error 1760e9547b80SChaoren Lin NativeProcessLinux::Interrupt () 1761e9547b80SChaoren Lin { 1762e9547b80SChaoren Lin // Pick a running thread (or if none, a not-dead stopped thread) as 1763e9547b80SChaoren Lin // the chosen thread that will be the stop-reason thread. 1764e9547b80SChaoren Lin Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1765e9547b80SChaoren Lin 1766e9547b80SChaoren Lin NativeThreadProtocolSP running_thread_sp; 1767e9547b80SChaoren Lin NativeThreadProtocolSP stopped_thread_sp; 1768e9547b80SChaoren Lin 1769e9547b80SChaoren Lin if (log) 1770e9547b80SChaoren Lin log->Printf ("NativeProcessLinux::%s selecting running thread for interrupt target", __FUNCTION__); 1771e9547b80SChaoren Lin 1772e9547b80SChaoren Lin for (auto thread_sp : m_threads) 1773e9547b80SChaoren Lin { 1774e9547b80SChaoren Lin // The thread shouldn't be null but lets just cover that here. 1775e9547b80SChaoren Lin if (!thread_sp) 1776e9547b80SChaoren Lin continue; 1777e9547b80SChaoren Lin 1778e9547b80SChaoren Lin // If we have a running or stepping thread, we'll call that the 1779e9547b80SChaoren Lin // target of the interrupt. 1780e9547b80SChaoren Lin const auto thread_state = thread_sp->GetState (); 1781e9547b80SChaoren Lin if (thread_state == eStateRunning || 1782e9547b80SChaoren Lin thread_state == eStateStepping) 1783e9547b80SChaoren Lin { 1784e9547b80SChaoren Lin running_thread_sp = thread_sp; 1785e9547b80SChaoren Lin break; 1786e9547b80SChaoren Lin } 1787e9547b80SChaoren Lin else if (!stopped_thread_sp && StateIsStoppedState (thread_state, true)) 1788e9547b80SChaoren Lin { 1789e9547b80SChaoren Lin // Remember the first non-dead stopped thread. We'll use that as a backup if there are no running threads. 1790e9547b80SChaoren Lin stopped_thread_sp = thread_sp; 1791e9547b80SChaoren Lin } 1792e9547b80SChaoren Lin } 1793e9547b80SChaoren Lin 1794e9547b80SChaoren Lin if (!running_thread_sp && !stopped_thread_sp) 1795e9547b80SChaoren Lin { 17965830aa75STamas Berghammer Error error("found no running/stepping or live stopped threads as target for interrupt"); 1797e9547b80SChaoren Lin if (log) 1798e9547b80SChaoren Lin log->Printf ("NativeProcessLinux::%s skipping due to error: %s", __FUNCTION__, error.AsCString ()); 17995830aa75STamas Berghammer 1800e9547b80SChaoren Lin return error; 1801e9547b80SChaoren Lin } 1802e9547b80SChaoren Lin 1803e9547b80SChaoren Lin NativeThreadProtocolSP deferred_signal_thread_sp = running_thread_sp ? running_thread_sp : stopped_thread_sp; 1804e9547b80SChaoren Lin 1805e9547b80SChaoren Lin if (log) 1806e9547b80SChaoren Lin log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " %s tid %" PRIu64 " chosen for interrupt target", 1807e9547b80SChaoren Lin __FUNCTION__, 1808e9547b80SChaoren Lin GetID (), 1809e9547b80SChaoren Lin running_thread_sp ? "running" : "stopped", 1810e9547b80SChaoren Lin deferred_signal_thread_sp->GetID ()); 1811e9547b80SChaoren Lin 1812ed89c7feSPavel Labath StopRunningThreads(deferred_signal_thread_sp->GetID()); 181345f5cb31SPavel Labath 18145830aa75STamas Berghammer return Error(); 1815e9547b80SChaoren Lin } 1816e9547b80SChaoren Lin 1817e9547b80SChaoren Lin Error 1818af245d11STodd Fiala NativeProcessLinux::Kill () 1819af245d11STodd Fiala { 1820af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1821af245d11STodd Fiala if (log) 1822af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s called for PID %" PRIu64, __FUNCTION__, GetID ()); 1823af245d11STodd Fiala 1824af245d11STodd Fiala Error error; 1825af245d11STodd Fiala 1826af245d11STodd Fiala switch (m_state) 1827af245d11STodd Fiala { 1828af245d11STodd Fiala case StateType::eStateInvalid: 1829af245d11STodd Fiala case StateType::eStateExited: 1830af245d11STodd Fiala case StateType::eStateCrashed: 1831af245d11STodd Fiala case StateType::eStateDetached: 1832af245d11STodd Fiala case StateType::eStateUnloaded: 1833af245d11STodd Fiala // Nothing to do - the process is already dead. 1834af245d11STodd Fiala if (log) 1835af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s ignored for PID %" PRIu64 " due to current state: %s", __FUNCTION__, GetID (), StateAsCString (m_state)); 1836af245d11STodd Fiala return error; 1837af245d11STodd Fiala 1838af245d11STodd Fiala case StateType::eStateConnected: 1839af245d11STodd Fiala case StateType::eStateAttaching: 1840af245d11STodd Fiala case StateType::eStateLaunching: 1841af245d11STodd Fiala case StateType::eStateStopped: 1842af245d11STodd Fiala case StateType::eStateRunning: 1843af245d11STodd Fiala case StateType::eStateStepping: 1844af245d11STodd Fiala case StateType::eStateSuspended: 1845af245d11STodd Fiala // We can try to kill a process in these states. 1846af245d11STodd Fiala break; 1847af245d11STodd Fiala } 1848af245d11STodd Fiala 1849af245d11STodd Fiala if (kill (GetID (), SIGKILL) != 0) 1850af245d11STodd Fiala { 1851af245d11STodd Fiala error.SetErrorToErrno (); 1852af245d11STodd Fiala return error; 1853af245d11STodd Fiala } 1854af245d11STodd Fiala 1855af245d11STodd Fiala return error; 1856af245d11STodd Fiala } 1857af245d11STodd Fiala 1858af245d11STodd Fiala static Error 1859af245d11STodd Fiala ParseMemoryRegionInfoFromProcMapsLine (const std::string &maps_line, MemoryRegionInfo &memory_region_info) 1860af245d11STodd Fiala { 1861af245d11STodd Fiala memory_region_info.Clear(); 1862af245d11STodd Fiala 1863af245d11STodd Fiala StringExtractor line_extractor (maps_line.c_str ()); 1864af245d11STodd Fiala 1865af245d11STodd Fiala // Format: {address_start_hex}-{address_end_hex} perms offset dev inode pathname 1866af245d11STodd Fiala // perms: rwxp (letter is present if set, '-' if not, final character is p=private, s=shared). 1867af245d11STodd Fiala 1868af245d11STodd Fiala // Parse out the starting address 1869af245d11STodd Fiala lldb::addr_t start_address = line_extractor.GetHexMaxU64 (false, 0); 1870af245d11STodd Fiala 1871af245d11STodd Fiala // Parse out hyphen separating start and end address from range. 1872af245d11STodd Fiala if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != '-')) 1873af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing dash between address range"); 1874af245d11STodd Fiala 1875af245d11STodd Fiala // Parse out the ending address 1876af245d11STodd Fiala lldb::addr_t end_address = line_extractor.GetHexMaxU64 (false, start_address); 1877af245d11STodd Fiala 1878af245d11STodd Fiala // Parse out the space after the address. 1879af245d11STodd Fiala if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != ' ')) 1880af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing space after range"); 1881af245d11STodd Fiala 1882af245d11STodd Fiala // Save the range. 1883af245d11STodd Fiala memory_region_info.GetRange ().SetRangeBase (start_address); 1884af245d11STodd Fiala memory_region_info.GetRange ().SetRangeEnd (end_address); 1885af245d11STodd Fiala 1886af245d11STodd Fiala // Parse out each permission entry. 1887af245d11STodd Fiala if (line_extractor.GetBytesLeft () < 4) 1888af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing some portion of permissions"); 1889af245d11STodd Fiala 1890af245d11STodd Fiala // Handle read permission. 1891af245d11STodd Fiala const char read_perm_char = line_extractor.GetChar (); 1892af245d11STodd Fiala if (read_perm_char == 'r') 1893af245d11STodd Fiala memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eYes); 1894af245d11STodd Fiala else 1895af245d11STodd Fiala { 1896af245d11STodd Fiala assert ( (read_perm_char == '-') && "unexpected /proc/{pid}/maps read permission char" ); 1897af245d11STodd Fiala memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); 1898af245d11STodd Fiala } 1899af245d11STodd Fiala 1900af245d11STodd Fiala // Handle write permission. 1901af245d11STodd Fiala const char write_perm_char = line_extractor.GetChar (); 1902af245d11STodd Fiala if (write_perm_char == 'w') 1903af245d11STodd Fiala memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eYes); 1904af245d11STodd Fiala else 1905af245d11STodd Fiala { 1906af245d11STodd Fiala assert ( (write_perm_char == '-') && "unexpected /proc/{pid}/maps write permission char" ); 1907af245d11STodd Fiala memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); 1908af245d11STodd Fiala } 1909af245d11STodd Fiala 1910af245d11STodd Fiala // Handle execute permission. 1911af245d11STodd Fiala const char exec_perm_char = line_extractor.GetChar (); 1912af245d11STodd Fiala if (exec_perm_char == 'x') 1913af245d11STodd Fiala memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eYes); 1914af245d11STodd Fiala else 1915af245d11STodd Fiala { 1916af245d11STodd Fiala assert ( (exec_perm_char == '-') && "unexpected /proc/{pid}/maps exec permission char" ); 1917af245d11STodd Fiala memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); 1918af245d11STodd Fiala } 1919af245d11STodd Fiala 1920af245d11STodd Fiala return Error (); 1921af245d11STodd Fiala } 1922af245d11STodd Fiala 1923af245d11STodd Fiala Error 1924af245d11STodd Fiala NativeProcessLinux::GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) 1925af245d11STodd Fiala { 1926af245d11STodd Fiala // FIXME review that the final memory region returned extends to the end of the virtual address space, 1927af245d11STodd Fiala // with no perms if it is not mapped. 1928af245d11STodd Fiala 1929af245d11STodd Fiala // Use an approach that reads memory regions from /proc/{pid}/maps. 1930af245d11STodd Fiala // Assume proc maps entries are in ascending order. 1931af245d11STodd Fiala // FIXME assert if we find differently. 1932af245d11STodd Fiala 1933af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1934af245d11STodd Fiala Error error; 1935af245d11STodd Fiala 1936af245d11STodd Fiala if (m_supports_mem_region == LazyBool::eLazyBoolNo) 1937af245d11STodd Fiala { 1938af245d11STodd Fiala // We're done. 1939af245d11STodd Fiala error.SetErrorString ("unsupported"); 1940af245d11STodd Fiala return error; 1941af245d11STodd Fiala } 1942af245d11STodd Fiala 1943af245d11STodd Fiala // If our cache is empty, pull the latest. There should always be at least one memory region 1944af245d11STodd Fiala // if memory region handling is supported. 1945af245d11STodd Fiala if (m_mem_region_cache.empty ()) 1946af245d11STodd Fiala { 1947af245d11STodd Fiala error = ProcFileReader::ProcessLineByLine (GetID (), "maps", 1948af245d11STodd Fiala [&] (const std::string &line) -> bool 1949af245d11STodd Fiala { 1950af245d11STodd Fiala MemoryRegionInfo info; 1951af245d11STodd Fiala const Error parse_error = ParseMemoryRegionInfoFromProcMapsLine (line, info); 1952af245d11STodd Fiala if (parse_error.Success ()) 1953af245d11STodd Fiala { 1954af245d11STodd Fiala m_mem_region_cache.push_back (info); 1955af245d11STodd Fiala return true; 1956af245d11STodd Fiala } 1957af245d11STodd Fiala else 1958af245d11STodd Fiala { 1959af245d11STodd Fiala if (log) 1960af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to parse proc maps line '%s': %s", __FUNCTION__, line.c_str (), error.AsCString ()); 1961af245d11STodd Fiala return false; 1962af245d11STodd Fiala } 1963af245d11STodd Fiala }); 1964af245d11STodd Fiala 1965af245d11STodd Fiala // If we had an error, we'll mark unsupported. 1966af245d11STodd Fiala if (error.Fail ()) 1967af245d11STodd Fiala { 1968af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolNo; 1969af245d11STodd Fiala return error; 1970af245d11STodd Fiala } 1971af245d11STodd Fiala else if (m_mem_region_cache.empty ()) 1972af245d11STodd Fiala { 1973af245d11STodd Fiala // No entries after attempting to read them. This shouldn't happen if /proc/{pid}/maps 1974af245d11STodd Fiala // is supported. Assume we don't support map entries via procfs. 1975af245d11STodd Fiala if (log) 1976af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to find any procfs maps entries, assuming no support for memory region metadata retrieval", __FUNCTION__); 1977af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolNo; 1978af245d11STodd Fiala error.SetErrorString ("not supported"); 1979af245d11STodd Fiala return error; 1980af245d11STodd Fiala } 1981af245d11STodd Fiala 1982af245d11STodd Fiala if (log) 1983af245d11STodd 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 ()); 1984af245d11STodd Fiala 1985af245d11STodd Fiala // We support memory retrieval, remember that. 1986af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolYes; 1987af245d11STodd Fiala } 1988af245d11STodd Fiala else 1989af245d11STodd Fiala { 1990af245d11STodd Fiala if (log) 1991af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s reusing %" PRIu64 " cached memory region entries", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); 1992af245d11STodd Fiala } 1993af245d11STodd Fiala 1994af245d11STodd Fiala lldb::addr_t prev_base_address = 0; 1995af245d11STodd Fiala 1996af245d11STodd Fiala // FIXME start by finding the last region that is <= target address using binary search. Data is sorted. 1997af245d11STodd Fiala // There can be a ton of regions on pthreads apps with lots of threads. 1998af245d11STodd Fiala for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end (); ++it) 1999af245d11STodd Fiala { 2000af245d11STodd Fiala MemoryRegionInfo &proc_entry_info = *it; 2001af245d11STodd Fiala 2002af245d11STodd Fiala // Sanity check assumption that /proc/{pid}/maps entries are ascending. 2003af245d11STodd Fiala assert ((proc_entry_info.GetRange ().GetRangeBase () >= prev_base_address) && "descending /proc/pid/maps entries detected, unexpected"); 2004af245d11STodd Fiala prev_base_address = proc_entry_info.GetRange ().GetRangeBase (); 2005af245d11STodd Fiala 2006af245d11STodd Fiala // If the target address comes before this entry, indicate distance to next region. 2007af245d11STodd Fiala if (load_addr < proc_entry_info.GetRange ().GetRangeBase ()) 2008af245d11STodd Fiala { 2009af245d11STodd Fiala range_info.GetRange ().SetRangeBase (load_addr); 2010af245d11STodd Fiala range_info.GetRange ().SetByteSize (proc_entry_info.GetRange ().GetRangeBase () - load_addr); 2011af245d11STodd Fiala range_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); 2012af245d11STodd Fiala range_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); 2013af245d11STodd Fiala range_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); 2014af245d11STodd Fiala 2015af245d11STodd Fiala return error; 2016af245d11STodd Fiala } 2017af245d11STodd Fiala else if (proc_entry_info.GetRange ().Contains (load_addr)) 2018af245d11STodd Fiala { 2019af245d11STodd Fiala // The target address is within the memory region we're processing here. 2020af245d11STodd Fiala range_info = proc_entry_info; 2021af245d11STodd Fiala return error; 2022af245d11STodd Fiala } 2023af245d11STodd Fiala 2024af245d11STodd Fiala // The target memory address comes somewhere after the region we just parsed. 2025af245d11STodd Fiala } 2026af245d11STodd Fiala 202709839c33STamas Berghammer // If we made it here, we didn't find an entry that contained the given address. Return the 202809839c33STamas Berghammer // load_addr as start and the amount of bytes betwwen load address and the end of the memory as 202909839c33STamas Berghammer // size. 203009839c33STamas Berghammer range_info.GetRange ().SetRangeBase (load_addr); 203109839c33STamas Berghammer switch (m_arch.GetAddressByteSize()) 203209839c33STamas Berghammer { 203309839c33STamas Berghammer case 4: 203409839c33STamas Berghammer range_info.GetRange ().SetByteSize (0x100000000ull - load_addr); 203509839c33STamas Berghammer break; 203609839c33STamas Berghammer case 8: 203709839c33STamas Berghammer range_info.GetRange ().SetByteSize (0ull - load_addr); 203809839c33STamas Berghammer break; 203909839c33STamas Berghammer default: 204009839c33STamas Berghammer assert(false && "Unrecognized data byte size"); 204109839c33STamas Berghammer break; 204209839c33STamas Berghammer } 204309839c33STamas Berghammer range_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); 204409839c33STamas Berghammer range_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); 204509839c33STamas Berghammer range_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); 2046af245d11STodd Fiala return error; 2047af245d11STodd Fiala } 2048af245d11STodd Fiala 2049af245d11STodd Fiala void 2050af245d11STodd Fiala NativeProcessLinux::DoStopIDBumped (uint32_t newBumpId) 2051af245d11STodd Fiala { 2052af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2053af245d11STodd Fiala if (log) 2054af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(newBumpId=%" PRIu32 ") called", __FUNCTION__, newBumpId); 2055af245d11STodd Fiala 2056af245d11STodd Fiala if (log) 2057af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s clearing %" PRIu64 " entries from the cache", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); 2058af245d11STodd Fiala m_mem_region_cache.clear (); 2059af245d11STodd Fiala } 2060af245d11STodd Fiala 2061af245d11STodd Fiala Error 20623eb4b458SChaoren Lin NativeProcessLinux::AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) 2063af245d11STodd Fiala { 2064af245d11STodd Fiala // FIXME implementing this requires the equivalent of 2065af245d11STodd Fiala // InferiorCallPOSIX::InferiorCallMmap, which depends on 2066af245d11STodd Fiala // functional ThreadPlans working with Native*Protocol. 2067af245d11STodd Fiala #if 1 2068af245d11STodd Fiala return Error ("not implemented yet"); 2069af245d11STodd Fiala #else 2070af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 2071af245d11STodd Fiala 2072af245d11STodd Fiala unsigned prot = 0; 2073af245d11STodd Fiala if (permissions & lldb::ePermissionsReadable) 2074af245d11STodd Fiala prot |= eMmapProtRead; 2075af245d11STodd Fiala if (permissions & lldb::ePermissionsWritable) 2076af245d11STodd Fiala prot |= eMmapProtWrite; 2077af245d11STodd Fiala if (permissions & lldb::ePermissionsExecutable) 2078af245d11STodd Fiala prot |= eMmapProtExec; 2079af245d11STodd Fiala 2080af245d11STodd Fiala // TODO implement this directly in NativeProcessLinux 2081af245d11STodd Fiala // (and lift to NativeProcessPOSIX if/when that class is 2082af245d11STodd Fiala // refactored out). 2083af245d11STodd Fiala if (InferiorCallMmap(this, addr, 0, size, prot, 2084af245d11STodd Fiala eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { 2085af245d11STodd Fiala m_addr_to_mmap_size[addr] = size; 2086af245d11STodd Fiala return Error (); 2087af245d11STodd Fiala } else { 2088af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 2089af245d11STodd Fiala return Error("unable to allocate %" PRIu64 " bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions)); 2090af245d11STodd Fiala } 2091af245d11STodd Fiala #endif 2092af245d11STodd Fiala } 2093af245d11STodd Fiala 2094af245d11STodd Fiala Error 2095af245d11STodd Fiala NativeProcessLinux::DeallocateMemory (lldb::addr_t addr) 2096af245d11STodd Fiala { 2097af245d11STodd Fiala // FIXME see comments in AllocateMemory - required lower-level 2098af245d11STodd Fiala // bits not in place yet (ThreadPlans) 2099af245d11STodd Fiala return Error ("not implemented"); 2100af245d11STodd Fiala } 2101af245d11STodd Fiala 2102af245d11STodd Fiala lldb::addr_t 2103af245d11STodd Fiala NativeProcessLinux::GetSharedLibraryInfoAddress () 2104af245d11STodd Fiala { 2105af245d11STodd Fiala // punt on this for now 2106af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 2107af245d11STodd Fiala } 2108af245d11STodd Fiala 2109af245d11STodd Fiala size_t 2110af245d11STodd Fiala NativeProcessLinux::UpdateThreads () 2111af245d11STodd Fiala { 2112af245d11STodd Fiala // The NativeProcessLinux monitoring threads are always up to date 2113af245d11STodd Fiala // with respect to thread state and they keep the thread list 2114af245d11STodd Fiala // populated properly. All this method needs to do is return the 2115af245d11STodd Fiala // thread count. 2116af245d11STodd Fiala return m_threads.size (); 2117af245d11STodd Fiala } 2118af245d11STodd Fiala 2119af245d11STodd Fiala bool 2120af245d11STodd Fiala NativeProcessLinux::GetArchitecture (ArchSpec &arch) const 2121af245d11STodd Fiala { 2122af245d11STodd Fiala arch = m_arch; 2123af245d11STodd Fiala return true; 2124af245d11STodd Fiala } 2125af245d11STodd Fiala 2126af245d11STodd Fiala Error 2127b9cc0c75SPavel Labath NativeProcessLinux::GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size) 2128af245d11STodd Fiala { 2129af245d11STodd Fiala // FIXME put this behind a breakpoint protocol class that can be 2130af245d11STodd Fiala // set per architecture. Need ARM, MIPS support here. 2131af245d11STodd Fiala static const uint8_t g_i386_opcode [] = { 0xCC }; 2132bb00d0b6SUlrich Weigand static const uint8_t g_s390x_opcode[] = { 0x00, 0x01 }; 2133af245d11STodd Fiala 2134af245d11STodd Fiala switch (m_arch.GetMachine ()) 2135af245d11STodd Fiala { 2136af245d11STodd Fiala case llvm::Triple::x86: 2137af245d11STodd Fiala case llvm::Triple::x86_64: 2138af245d11STodd Fiala actual_opcode_size = static_cast<uint32_t> (sizeof(g_i386_opcode)); 2139af245d11STodd Fiala return Error (); 2140af245d11STodd Fiala 2141bb00d0b6SUlrich Weigand case llvm::Triple::systemz: 2142bb00d0b6SUlrich Weigand actual_opcode_size = static_cast<uint32_t> (sizeof(g_s390x_opcode)); 2143bb00d0b6SUlrich Weigand return Error (); 2144bb00d0b6SUlrich Weigand 2145ff7fd900STamas Berghammer case llvm::Triple::arm: 2146ff7fd900STamas Berghammer case llvm::Triple::aarch64: 2147e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64: 2148e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64el: 2149ce815e45SSagar Thakur case llvm::Triple::mips: 2150ce815e45SSagar Thakur case llvm::Triple::mipsel: 2151ff7fd900STamas Berghammer // On these architectures the PC don't get updated for breakpoint hits 2152c60c9452SJaydeep Patil actual_opcode_size = 0; 2153e8659b5dSMohit K. Bhakkad return Error (); 2154e8659b5dSMohit K. Bhakkad 2155af245d11STodd Fiala default: 2156af245d11STodd Fiala assert(false && "CPU type not supported!"); 2157af245d11STodd Fiala return Error ("CPU type not supported"); 2158af245d11STodd Fiala } 2159af245d11STodd Fiala } 2160af245d11STodd Fiala 2161af245d11STodd Fiala Error 2162af245d11STodd Fiala NativeProcessLinux::SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) 2163af245d11STodd Fiala { 2164af245d11STodd Fiala if (hardware) 2165af245d11STodd Fiala return Error ("NativeProcessLinux does not support hardware breakpoints"); 2166af245d11STodd Fiala else 2167af245d11STodd Fiala return SetSoftwareBreakpoint (addr, size); 2168af245d11STodd Fiala } 2169af245d11STodd Fiala 2170af245d11STodd Fiala Error 217163c8be95STamas Berghammer NativeProcessLinux::GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, 217263c8be95STamas Berghammer size_t &actual_opcode_size, 217363c8be95STamas Berghammer const uint8_t *&trap_opcode_bytes) 2174af245d11STodd Fiala { 217563c8be95STamas Berghammer // FIXME put this behind a breakpoint protocol class that can be set per 217663c8be95STamas Berghammer // architecture. Need MIPS support here. 21772afc5966STodd Fiala static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; 2178be379e15STamas Berghammer // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the 2179be379e15STamas Berghammer // linux kernel does otherwise. 2180be379e15STamas Berghammer static const uint8_t g_arm_breakpoint_opcode[] = { 0xf0, 0x01, 0xf0, 0xe7 }; 2181af245d11STodd Fiala static const uint8_t g_i386_opcode [] = { 0xCC }; 21823df471c3SMohit K. Bhakkad static const uint8_t g_mips64_opcode[] = { 0x00, 0x00, 0x00, 0x0d }; 21832c2acf96SMohit K. Bhakkad static const uint8_t g_mips64el_opcode[] = { 0x0d, 0x00, 0x00, 0x00 }; 2184bb00d0b6SUlrich Weigand static const uint8_t g_s390x_opcode[] = { 0x00, 0x01 }; 2185be379e15STamas Berghammer static const uint8_t g_thumb_breakpoint_opcode[] = { 0x01, 0xde }; 2186af245d11STodd Fiala 2187af245d11STodd Fiala switch (m_arch.GetMachine ()) 2188af245d11STodd Fiala { 21892afc5966STodd Fiala case llvm::Triple::aarch64: 21902afc5966STodd Fiala trap_opcode_bytes = g_aarch64_opcode; 21912afc5966STodd Fiala actual_opcode_size = sizeof(g_aarch64_opcode); 21922afc5966STodd Fiala return Error (); 21932afc5966STodd Fiala 219463c8be95STamas Berghammer case llvm::Triple::arm: 219563c8be95STamas Berghammer switch (trap_opcode_size_hint) 219663c8be95STamas Berghammer { 219763c8be95STamas Berghammer case 2: 219863c8be95STamas Berghammer trap_opcode_bytes = g_thumb_breakpoint_opcode; 219963c8be95STamas Berghammer actual_opcode_size = sizeof(g_thumb_breakpoint_opcode); 220063c8be95STamas Berghammer return Error (); 220163c8be95STamas Berghammer case 4: 220263c8be95STamas Berghammer trap_opcode_bytes = g_arm_breakpoint_opcode; 220363c8be95STamas Berghammer actual_opcode_size = sizeof(g_arm_breakpoint_opcode); 220463c8be95STamas Berghammer return Error (); 220563c8be95STamas Berghammer default: 220663c8be95STamas Berghammer assert(false && "Unrecognised trap opcode size hint!"); 220763c8be95STamas Berghammer return Error ("Unrecognised trap opcode size hint!"); 220863c8be95STamas Berghammer } 220963c8be95STamas Berghammer 2210af245d11STodd Fiala case llvm::Triple::x86: 2211af245d11STodd Fiala case llvm::Triple::x86_64: 2212af245d11STodd Fiala trap_opcode_bytes = g_i386_opcode; 2213af245d11STodd Fiala actual_opcode_size = sizeof(g_i386_opcode); 2214af245d11STodd Fiala return Error (); 2215af245d11STodd Fiala 2216ce815e45SSagar Thakur case llvm::Triple::mips: 22173df471c3SMohit K. Bhakkad case llvm::Triple::mips64: 22183df471c3SMohit K. Bhakkad trap_opcode_bytes = g_mips64_opcode; 22193df471c3SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64_opcode); 22203df471c3SMohit K. Bhakkad return Error (); 22213df471c3SMohit K. Bhakkad 2222ce815e45SSagar Thakur case llvm::Triple::mipsel: 22232c2acf96SMohit K. Bhakkad case llvm::Triple::mips64el: 22242c2acf96SMohit K. Bhakkad trap_opcode_bytes = g_mips64el_opcode; 22252c2acf96SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64el_opcode); 22262c2acf96SMohit K. Bhakkad return Error (); 22272c2acf96SMohit K. Bhakkad 2228bb00d0b6SUlrich Weigand case llvm::Triple::systemz: 2229bb00d0b6SUlrich Weigand trap_opcode_bytes = g_s390x_opcode; 2230bb00d0b6SUlrich Weigand actual_opcode_size = sizeof(g_s390x_opcode); 2231bb00d0b6SUlrich Weigand return Error (); 2232bb00d0b6SUlrich Weigand 2233af245d11STodd Fiala default: 2234af245d11STodd Fiala assert(false && "CPU type not supported!"); 2235af245d11STodd Fiala return Error ("CPU type not supported"); 2236af245d11STodd Fiala } 2237af245d11STodd Fiala } 2238af245d11STodd Fiala 2239af245d11STodd Fiala #if 0 2240af245d11STodd Fiala ProcessMessage::CrashReason 2241af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info) 2242af245d11STodd Fiala { 2243af245d11STodd Fiala ProcessMessage::CrashReason reason; 2244af245d11STodd Fiala assert(info->si_signo == SIGSEGV); 2245af245d11STodd Fiala 2246af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 2247af245d11STodd Fiala 2248af245d11STodd Fiala switch (info->si_code) 2249af245d11STodd Fiala { 2250af245d11STodd Fiala default: 2251af245d11STodd Fiala assert(false && "unexpected si_code for SIGSEGV"); 2252af245d11STodd Fiala break; 2253af245d11STodd Fiala case SI_KERNEL: 2254af245d11STodd Fiala // Linux will occasionally send spurious SI_KERNEL codes. 2255af245d11STodd Fiala // (this is poorly documented in sigaction) 2256af245d11STodd Fiala // One way to get this is via unaligned SIMD loads. 2257af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; // for lack of anything better 2258af245d11STodd Fiala break; 2259af245d11STodd Fiala case SEGV_MAPERR: 2260af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; 2261af245d11STodd Fiala break; 2262af245d11STodd Fiala case SEGV_ACCERR: 2263af245d11STodd Fiala reason = ProcessMessage::ePrivilegedAddress; 2264af245d11STodd Fiala break; 2265af245d11STodd Fiala } 2266af245d11STodd Fiala 2267af245d11STodd Fiala return reason; 2268af245d11STodd Fiala } 2269af245d11STodd Fiala #endif 2270af245d11STodd Fiala 2271af245d11STodd Fiala 2272af245d11STodd Fiala #if 0 2273af245d11STodd Fiala ProcessMessage::CrashReason 2274af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info) 2275af245d11STodd Fiala { 2276af245d11STodd Fiala ProcessMessage::CrashReason reason; 2277af245d11STodd Fiala assert(info->si_signo == SIGILL); 2278af245d11STodd Fiala 2279af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 2280af245d11STodd Fiala 2281af245d11STodd Fiala switch (info->si_code) 2282af245d11STodd Fiala { 2283af245d11STodd Fiala default: 2284af245d11STodd Fiala assert(false && "unexpected si_code for SIGILL"); 2285af245d11STodd Fiala break; 2286af245d11STodd Fiala case ILL_ILLOPC: 2287af245d11STodd Fiala reason = ProcessMessage::eIllegalOpcode; 2288af245d11STodd Fiala break; 2289af245d11STodd Fiala case ILL_ILLOPN: 2290af245d11STodd Fiala reason = ProcessMessage::eIllegalOperand; 2291af245d11STodd Fiala break; 2292af245d11STodd Fiala case ILL_ILLADR: 2293af245d11STodd Fiala reason = ProcessMessage::eIllegalAddressingMode; 2294af245d11STodd Fiala break; 2295af245d11STodd Fiala case ILL_ILLTRP: 2296af245d11STodd Fiala reason = ProcessMessage::eIllegalTrap; 2297af245d11STodd Fiala break; 2298af245d11STodd Fiala case ILL_PRVOPC: 2299af245d11STodd Fiala reason = ProcessMessage::ePrivilegedOpcode; 2300af245d11STodd Fiala break; 2301af245d11STodd Fiala case ILL_PRVREG: 2302af245d11STodd Fiala reason = ProcessMessage::ePrivilegedRegister; 2303af245d11STodd Fiala break; 2304af245d11STodd Fiala case ILL_COPROC: 2305af245d11STodd Fiala reason = ProcessMessage::eCoprocessorError; 2306af245d11STodd Fiala break; 2307af245d11STodd Fiala case ILL_BADSTK: 2308af245d11STodd Fiala reason = ProcessMessage::eInternalStackError; 2309af245d11STodd Fiala break; 2310af245d11STodd Fiala } 2311af245d11STodd Fiala 2312af245d11STodd Fiala return reason; 2313af245d11STodd Fiala } 2314af245d11STodd Fiala #endif 2315af245d11STodd Fiala 2316af245d11STodd Fiala #if 0 2317af245d11STodd Fiala ProcessMessage::CrashReason 2318af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info) 2319af245d11STodd Fiala { 2320af245d11STodd Fiala ProcessMessage::CrashReason reason; 2321af245d11STodd Fiala assert(info->si_signo == SIGFPE); 2322af245d11STodd Fiala 2323af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 2324af245d11STodd Fiala 2325af245d11STodd Fiala switch (info->si_code) 2326af245d11STodd Fiala { 2327af245d11STodd Fiala default: 2328af245d11STodd Fiala assert(false && "unexpected si_code for SIGFPE"); 2329af245d11STodd Fiala break; 2330af245d11STodd Fiala case FPE_INTDIV: 2331af245d11STodd Fiala reason = ProcessMessage::eIntegerDivideByZero; 2332af245d11STodd Fiala break; 2333af245d11STodd Fiala case FPE_INTOVF: 2334af245d11STodd Fiala reason = ProcessMessage::eIntegerOverflow; 2335af245d11STodd Fiala break; 2336af245d11STodd Fiala case FPE_FLTDIV: 2337af245d11STodd Fiala reason = ProcessMessage::eFloatDivideByZero; 2338af245d11STodd Fiala break; 2339af245d11STodd Fiala case FPE_FLTOVF: 2340af245d11STodd Fiala reason = ProcessMessage::eFloatOverflow; 2341af245d11STodd Fiala break; 2342af245d11STodd Fiala case FPE_FLTUND: 2343af245d11STodd Fiala reason = ProcessMessage::eFloatUnderflow; 2344af245d11STodd Fiala break; 2345af245d11STodd Fiala case FPE_FLTRES: 2346af245d11STodd Fiala reason = ProcessMessage::eFloatInexactResult; 2347af245d11STodd Fiala break; 2348af245d11STodd Fiala case FPE_FLTINV: 2349af245d11STodd Fiala reason = ProcessMessage::eFloatInvalidOperation; 2350af245d11STodd Fiala break; 2351af245d11STodd Fiala case FPE_FLTSUB: 2352af245d11STodd Fiala reason = ProcessMessage::eFloatSubscriptRange; 2353af245d11STodd Fiala break; 2354af245d11STodd Fiala } 2355af245d11STodd Fiala 2356af245d11STodd Fiala return reason; 2357af245d11STodd Fiala } 2358af245d11STodd Fiala #endif 2359af245d11STodd Fiala 2360af245d11STodd Fiala #if 0 2361af245d11STodd Fiala ProcessMessage::CrashReason 2362af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info) 2363af245d11STodd Fiala { 2364af245d11STodd Fiala ProcessMessage::CrashReason reason; 2365af245d11STodd Fiala assert(info->si_signo == SIGBUS); 2366af245d11STodd Fiala 2367af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 2368af245d11STodd Fiala 2369af245d11STodd Fiala switch (info->si_code) 2370af245d11STodd Fiala { 2371af245d11STodd Fiala default: 2372af245d11STodd Fiala assert(false && "unexpected si_code for SIGBUS"); 2373af245d11STodd Fiala break; 2374af245d11STodd Fiala case BUS_ADRALN: 2375af245d11STodd Fiala reason = ProcessMessage::eIllegalAlignment; 2376af245d11STodd Fiala break; 2377af245d11STodd Fiala case BUS_ADRERR: 2378af245d11STodd Fiala reason = ProcessMessage::eIllegalAddress; 2379af245d11STodd Fiala break; 2380af245d11STodd Fiala case BUS_OBJERR: 2381af245d11STodd Fiala reason = ProcessMessage::eHardwareError; 2382af245d11STodd Fiala break; 2383af245d11STodd Fiala } 2384af245d11STodd Fiala 2385af245d11STodd Fiala return reason; 2386af245d11STodd Fiala } 2387af245d11STodd Fiala #endif 2388af245d11STodd Fiala 2389af245d11STodd Fiala Error 239026438d26SChaoren Lin NativeProcessLinux::ReadMemory (lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) 2391af245d11STodd Fiala { 2392df7c6995SPavel Labath if (ProcessVmReadvSupported()) { 2393df7c6995SPavel Labath // The process_vm_readv path is about 50 times faster than ptrace api. We want to use 2394df7c6995SPavel Labath // this syscall if it is supported. 2395df7c6995SPavel Labath 2396df7c6995SPavel Labath const ::pid_t pid = GetID(); 2397df7c6995SPavel Labath 2398df7c6995SPavel Labath struct iovec local_iov, remote_iov; 2399df7c6995SPavel Labath local_iov.iov_base = buf; 2400df7c6995SPavel Labath local_iov.iov_len = size; 2401df7c6995SPavel Labath remote_iov.iov_base = reinterpret_cast<void *>(addr); 2402df7c6995SPavel Labath remote_iov.iov_len = size; 2403df7c6995SPavel Labath 2404df7c6995SPavel Labath bytes_read = process_vm_readv(pid, &local_iov, 1, &remote_iov, 1, 0); 2405df7c6995SPavel Labath const bool success = bytes_read == size; 2406df7c6995SPavel Labath 2407df7c6995SPavel Labath Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2408df7c6995SPavel Labath if (log) 2409df7c6995SPavel Labath log->Printf ("NativeProcessLinux::%s using process_vm_readv to read %zd bytes from inferior address 0x%" PRIx64": %s", 2410df7c6995SPavel Labath __FUNCTION__, size, addr, success ? "Success" : strerror(errno)); 2411df7c6995SPavel Labath 2412df7c6995SPavel Labath if (success) 2413df7c6995SPavel Labath return Error(); 2414df7c6995SPavel Labath // else 2415df7c6995SPavel Labath // the call failed for some reason, let's retry the read using ptrace api. 2416df7c6995SPavel Labath } 2417df7c6995SPavel Labath 241819cbe96aSPavel Labath unsigned char *dst = static_cast<unsigned char*>(buf); 241919cbe96aSPavel Labath size_t remainder; 242019cbe96aSPavel Labath long data; 242119cbe96aSPavel Labath 242219cbe96aSPavel Labath Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); 242319cbe96aSPavel Labath if (log) 242419cbe96aSPavel Labath ProcessPOSIXLog::IncNestLevel(); 242519cbe96aSPavel Labath if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) 242619cbe96aSPavel Labath log->Printf ("NativeProcessLinux::%s(%p, %p, %zd, _)", __FUNCTION__, (void*)addr, buf, size); 242719cbe96aSPavel Labath 242819cbe96aSPavel Labath for (bytes_read = 0; bytes_read < size; bytes_read += remainder) 242919cbe96aSPavel Labath { 243019cbe96aSPavel Labath Error error = NativeProcessLinux::PtraceWrapper(PTRACE_PEEKDATA, GetID(), (void*)addr, nullptr, 0, &data); 243119cbe96aSPavel Labath if (error.Fail()) 243219cbe96aSPavel Labath { 243319cbe96aSPavel Labath if (log) 243419cbe96aSPavel Labath ProcessPOSIXLog::DecNestLevel(); 243519cbe96aSPavel Labath return error; 243619cbe96aSPavel Labath } 243719cbe96aSPavel Labath 243819cbe96aSPavel Labath remainder = size - bytes_read; 243919cbe96aSPavel Labath remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; 244019cbe96aSPavel Labath 244119cbe96aSPavel Labath // Copy the data into our buffer 2442f6ef187bSMohit K. Bhakkad memcpy(dst, &data, remainder); 244319cbe96aSPavel Labath 244419cbe96aSPavel Labath if (log && ProcessPOSIXLog::AtTopNestLevel() && 244519cbe96aSPavel Labath (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 244619cbe96aSPavel Labath (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 244719cbe96aSPavel Labath size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 244819cbe96aSPavel Labath { 244919cbe96aSPavel Labath uintptr_t print_dst = 0; 245019cbe96aSPavel Labath // Format bytes from data by moving into print_dst for log output 245119cbe96aSPavel Labath for (unsigned i = 0; i < remainder; ++i) 245219cbe96aSPavel Labath print_dst |= (((data >> i*8) & 0xFF) << i*8); 245379203995SPavel Labath log->Printf ("NativeProcessLinux::%s() [0x%" PRIx64 "]:0x%" PRIx64 " (0x%" PRIx64 ")", 245479203995SPavel Labath __FUNCTION__, addr, uint64_t(print_dst), uint64_t(data)); 245519cbe96aSPavel Labath } 245619cbe96aSPavel Labath addr += k_ptrace_word_size; 245719cbe96aSPavel Labath dst += k_ptrace_word_size; 245819cbe96aSPavel Labath } 245919cbe96aSPavel Labath 246019cbe96aSPavel Labath if (log) 246119cbe96aSPavel Labath ProcessPOSIXLog::DecNestLevel(); 246219cbe96aSPavel Labath return Error(); 2463af245d11STodd Fiala } 2464af245d11STodd Fiala 2465af245d11STodd Fiala Error 24663eb4b458SChaoren Lin NativeProcessLinux::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) 24673eb4b458SChaoren Lin { 24683eb4b458SChaoren Lin Error error = ReadMemory(addr, buf, size, bytes_read); 24693eb4b458SChaoren Lin if (error.Fail()) return error; 24703eb4b458SChaoren Lin return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); 24713eb4b458SChaoren Lin } 24723eb4b458SChaoren Lin 24733eb4b458SChaoren Lin Error 24743eb4b458SChaoren Lin NativeProcessLinux::WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) 2475af245d11STodd Fiala { 247619cbe96aSPavel Labath const unsigned char *src = static_cast<const unsigned char*>(buf); 247719cbe96aSPavel Labath size_t remainder; 247819cbe96aSPavel Labath Error error; 247919cbe96aSPavel Labath 248019cbe96aSPavel Labath Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); 248119cbe96aSPavel Labath if (log) 248219cbe96aSPavel Labath ProcessPOSIXLog::IncNestLevel(); 248319cbe96aSPavel Labath if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) 248479203995SPavel Labath log->Printf ("NativeProcessLinux::%s(0x%" PRIx64 ", %p, %zu)", __FUNCTION__, addr, buf, size); 248519cbe96aSPavel Labath 248619cbe96aSPavel Labath for (bytes_written = 0; bytes_written < size; bytes_written += remainder) 248719cbe96aSPavel Labath { 248819cbe96aSPavel Labath remainder = size - bytes_written; 248919cbe96aSPavel Labath remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; 249019cbe96aSPavel Labath 249119cbe96aSPavel Labath if (remainder == k_ptrace_word_size) 249219cbe96aSPavel Labath { 249319cbe96aSPavel Labath unsigned long data = 0; 2494f6ef187bSMohit K. Bhakkad memcpy(&data, src, k_ptrace_word_size); 249519cbe96aSPavel Labath 249619cbe96aSPavel Labath if (log && ProcessPOSIXLog::AtTopNestLevel() && 249719cbe96aSPavel Labath (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 249819cbe96aSPavel Labath (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 249919cbe96aSPavel Labath size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 250019cbe96aSPavel Labath log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 250119cbe96aSPavel Labath (void*)addr, *(const unsigned long*)src, data); 250219cbe96aSPavel Labath 250319cbe96aSPavel Labath error = NativeProcessLinux::PtraceWrapper(PTRACE_POKEDATA, GetID(), (void*)addr, (void*)data); 250419cbe96aSPavel Labath if (error.Fail()) 250519cbe96aSPavel Labath { 250619cbe96aSPavel Labath if (log) 250719cbe96aSPavel Labath ProcessPOSIXLog::DecNestLevel(); 250819cbe96aSPavel Labath return error; 250919cbe96aSPavel Labath } 251019cbe96aSPavel Labath } 251119cbe96aSPavel Labath else 251219cbe96aSPavel Labath { 251319cbe96aSPavel Labath unsigned char buff[8]; 251419cbe96aSPavel Labath size_t bytes_read; 251519cbe96aSPavel Labath error = ReadMemory(addr, buff, k_ptrace_word_size, bytes_read); 251619cbe96aSPavel Labath if (error.Fail()) 251719cbe96aSPavel Labath { 251819cbe96aSPavel Labath if (log) 251919cbe96aSPavel Labath ProcessPOSIXLog::DecNestLevel(); 252019cbe96aSPavel Labath return error; 252119cbe96aSPavel Labath } 252219cbe96aSPavel Labath 252319cbe96aSPavel Labath memcpy(buff, src, remainder); 252419cbe96aSPavel Labath 252519cbe96aSPavel Labath size_t bytes_written_rec; 252619cbe96aSPavel Labath error = WriteMemory(addr, buff, k_ptrace_word_size, bytes_written_rec); 252719cbe96aSPavel Labath if (error.Fail()) 252819cbe96aSPavel Labath { 252919cbe96aSPavel Labath if (log) 253019cbe96aSPavel Labath ProcessPOSIXLog::DecNestLevel(); 253119cbe96aSPavel Labath return error; 253219cbe96aSPavel Labath } 253319cbe96aSPavel Labath 253419cbe96aSPavel Labath if (log && ProcessPOSIXLog::AtTopNestLevel() && 253519cbe96aSPavel Labath (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 253619cbe96aSPavel Labath (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 253719cbe96aSPavel Labath size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 253819cbe96aSPavel Labath log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 253919cbe96aSPavel Labath (void*)addr, *(const unsigned long*)src, *(unsigned long*)buff); 254019cbe96aSPavel Labath } 254119cbe96aSPavel Labath 254219cbe96aSPavel Labath addr += k_ptrace_word_size; 254319cbe96aSPavel Labath src += k_ptrace_word_size; 254419cbe96aSPavel Labath } 254519cbe96aSPavel Labath if (log) 254619cbe96aSPavel Labath ProcessPOSIXLog::DecNestLevel(); 254719cbe96aSPavel Labath return error; 2548af245d11STodd Fiala } 2549af245d11STodd Fiala 255097ccc294SChaoren Lin Error 255197ccc294SChaoren Lin NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) 2552af245d11STodd Fiala { 255319cbe96aSPavel Labath return PtraceWrapper(PTRACE_GETSIGINFO, tid, nullptr, siginfo); 2554af245d11STodd Fiala } 2555af245d11STodd Fiala 255697ccc294SChaoren Lin Error 2557af245d11STodd Fiala NativeProcessLinux::GetEventMessage(lldb::tid_t tid, unsigned long *message) 2558af245d11STodd Fiala { 255919cbe96aSPavel Labath return PtraceWrapper(PTRACE_GETEVENTMSG, tid, nullptr, message); 2560af245d11STodd Fiala } 2561af245d11STodd Fiala 2562db264a6dSTamas Berghammer Error 2563af245d11STodd Fiala NativeProcessLinux::Detach(lldb::tid_t tid) 2564af245d11STodd Fiala { 256597ccc294SChaoren Lin if (tid == LLDB_INVALID_THREAD_ID) 256697ccc294SChaoren Lin return Error(); 256797ccc294SChaoren Lin 256819cbe96aSPavel Labath return PtraceWrapper(PTRACE_DETACH, tid); 2569af245d11STodd Fiala } 2570af245d11STodd Fiala 2571af245d11STodd Fiala bool 2572d3173f34SChaoren Lin NativeProcessLinux::DupDescriptor(const FileSpec &file_spec, int fd, int flags) 2573af245d11STodd Fiala { 2574d3173f34SChaoren Lin int target_fd = open(file_spec.GetCString(), flags, 0666); 2575af245d11STodd Fiala 2576af245d11STodd Fiala if (target_fd == -1) 2577af245d11STodd Fiala return false; 2578af245d11STodd Fiala 2579493c3a12SPavel Labath if (dup2(target_fd, fd) == -1) 2580493c3a12SPavel Labath return false; 2581493c3a12SPavel Labath 2582493c3a12SPavel Labath return (close(target_fd) == -1) ? false : true; 2583af245d11STodd Fiala } 2584af245d11STodd Fiala 2585af245d11STodd Fiala bool 2586af245d11STodd Fiala NativeProcessLinux::HasThreadNoLock (lldb::tid_t thread_id) 2587af245d11STodd Fiala { 2588af245d11STodd Fiala for (auto thread_sp : m_threads) 2589af245d11STodd Fiala { 2590af245d11STodd Fiala assert (thread_sp && "thread list should not contain NULL threads"); 2591af245d11STodd Fiala if (thread_sp->GetID () == thread_id) 2592af245d11STodd Fiala { 2593af245d11STodd Fiala // We have this thread. 2594af245d11STodd Fiala return true; 2595af245d11STodd Fiala } 2596af245d11STodd Fiala } 2597af245d11STodd Fiala 2598af245d11STodd Fiala // We don't have this thread. 2599af245d11STodd Fiala return false; 2600af245d11STodd Fiala } 2601af245d11STodd Fiala 2602af245d11STodd Fiala bool 2603af245d11STodd Fiala NativeProcessLinux::StopTrackingThread (lldb::tid_t thread_id) 2604af245d11STodd Fiala { 26051dbc6c9cSPavel Labath Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 26061dbc6c9cSPavel Labath 26071dbc6c9cSPavel Labath if (log) 26081dbc6c9cSPavel Labath log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ")", __FUNCTION__, thread_id); 26091dbc6c9cSPavel Labath 26101dbc6c9cSPavel Labath bool found = false; 26111dbc6c9cSPavel Labath 2612af245d11STodd Fiala for (auto it = m_threads.begin (); it != m_threads.end (); ++it) 2613af245d11STodd Fiala { 2614af245d11STodd Fiala if (*it && ((*it)->GetID () == thread_id)) 2615af245d11STodd Fiala { 2616af245d11STodd Fiala m_threads.erase (it); 26171dbc6c9cSPavel Labath found = true; 26181dbc6c9cSPavel Labath break; 2619af245d11STodd Fiala } 2620af245d11STodd Fiala } 2621af245d11STodd Fiala 26229eb1ecb9SPavel Labath SignalIfAllThreadsStopped(); 26231dbc6c9cSPavel Labath 26241dbc6c9cSPavel Labath return found; 2625af245d11STodd Fiala } 2626af245d11STodd Fiala 2627f9077782SPavel Labath NativeThreadLinuxSP 2628af245d11STodd Fiala NativeProcessLinux::AddThread (lldb::tid_t thread_id) 2629af245d11STodd Fiala { 2630af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 2631af245d11STodd Fiala 2632af245d11STodd Fiala if (log) 2633af245d11STodd Fiala { 2634af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " adding thread with tid %" PRIu64, 2635af245d11STodd Fiala __FUNCTION__, 2636af245d11STodd Fiala GetID (), 2637af245d11STodd Fiala thread_id); 2638af245d11STodd Fiala } 2639af245d11STodd Fiala 2640af245d11STodd Fiala assert (!HasThreadNoLock (thread_id) && "attempted to add a thread by id that already exists"); 2641af245d11STodd Fiala 2642af245d11STodd Fiala // If this is the first thread, save it as the current thread 2643af245d11STodd Fiala if (m_threads.empty ()) 2644af245d11STodd Fiala SetCurrentThreadID (thread_id); 2645af245d11STodd Fiala 2646f9077782SPavel Labath auto thread_sp = std::make_shared<NativeThreadLinux>(this, thread_id); 2647af245d11STodd Fiala m_threads.push_back (thread_sp); 2648af245d11STodd Fiala return thread_sp; 2649af245d11STodd Fiala } 2650af245d11STodd Fiala 2651af245d11STodd Fiala Error 2652b9cc0c75SPavel Labath NativeProcessLinux::FixupBreakpointPCAsNeeded(NativeThreadLinux &thread) 2653af245d11STodd Fiala { 265475f47c3aSTodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 2655af245d11STodd Fiala 2656af245d11STodd Fiala Error error; 2657af245d11STodd Fiala 2658af245d11STodd Fiala // Find out the size of a breakpoint (might depend on where we are in the code). 2659b9cc0c75SPavel Labath NativeRegisterContextSP context_sp = thread.GetRegisterContext(); 2660af245d11STodd Fiala if (!context_sp) 2661af245d11STodd Fiala { 2662af245d11STodd Fiala error.SetErrorString ("cannot get a NativeRegisterContext for the thread"); 2663af245d11STodd Fiala if (log) 2664af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); 2665af245d11STodd Fiala return error; 2666af245d11STodd Fiala } 2667af245d11STodd Fiala 2668af245d11STodd Fiala uint32_t breakpoint_size = 0; 2669b9cc0c75SPavel Labath error = GetSoftwareBreakpointPCOffset(breakpoint_size); 2670af245d11STodd Fiala if (error.Fail ()) 2671af245d11STodd Fiala { 2672af245d11STodd Fiala if (log) 2673af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s GetBreakpointSize() failed: %s", __FUNCTION__, error.AsCString ()); 2674af245d11STodd Fiala return error; 2675af245d11STodd Fiala } 2676af245d11STodd Fiala else 2677af245d11STodd Fiala { 2678af245d11STodd Fiala if (log) 2679af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s breakpoint size: %" PRIu32, __FUNCTION__, breakpoint_size); 2680af245d11STodd Fiala } 2681af245d11STodd Fiala 2682af245d11STodd Fiala // First try probing for a breakpoint at a software breakpoint location: PC - breakpoint size. 2683c60c9452SJaydeep Patil const lldb::addr_t initial_pc_addr = context_sp->GetPCfromBreakpointLocation (); 2684af245d11STodd Fiala lldb::addr_t breakpoint_addr = initial_pc_addr; 26853eb4b458SChaoren Lin if (breakpoint_size > 0) 2686af245d11STodd Fiala { 2687af245d11STodd Fiala // Do not allow breakpoint probe to wrap around. 26883eb4b458SChaoren Lin if (breakpoint_addr >= breakpoint_size) 26893eb4b458SChaoren Lin breakpoint_addr -= breakpoint_size; 2690af245d11STodd Fiala } 2691af245d11STodd Fiala 2692af245d11STodd Fiala // Check if we stopped because of a breakpoint. 2693af245d11STodd Fiala NativeBreakpointSP breakpoint_sp; 2694af245d11STodd Fiala error = m_breakpoint_list.GetBreakpoint (breakpoint_addr, breakpoint_sp); 2695af245d11STodd Fiala if (!error.Success () || !breakpoint_sp) 2696af245d11STodd Fiala { 2697af245d11STodd Fiala // We didn't find one at a software probe location. Nothing to do. 2698af245d11STodd Fiala if (log) 2699af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " no lldb breakpoint found at current pc with adjustment: 0x%" PRIx64, __FUNCTION__, GetID (), breakpoint_addr); 2700af245d11STodd Fiala return Error (); 2701af245d11STodd Fiala } 2702af245d11STodd Fiala 2703af245d11STodd Fiala // If the breakpoint is not a software breakpoint, nothing to do. 2704af245d11STodd Fiala if (!breakpoint_sp->IsSoftwareBreakpoint ()) 2705af245d11STodd Fiala { 2706af245d11STodd Fiala if (log) 2707af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " breakpoint found at 0x%" PRIx64 ", not software, nothing to adjust", __FUNCTION__, GetID (), breakpoint_addr); 2708af245d11STodd Fiala return Error (); 2709af245d11STodd Fiala } 2710af245d11STodd Fiala 2711af245d11STodd Fiala // 2712af245d11STodd Fiala // We have a software breakpoint and need to adjust the PC. 2713af245d11STodd Fiala // 2714af245d11STodd Fiala 2715af245d11STodd Fiala // Sanity check. 2716af245d11STodd Fiala if (breakpoint_size == 0) 2717af245d11STodd Fiala { 2718af245d11STodd Fiala // Nothing to do! How did we get here? 2719af245d11STodd Fiala if (log) 2720af245d11STodd 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); 2721af245d11STodd Fiala return Error (); 2722af245d11STodd Fiala } 2723af245d11STodd Fiala 2724af245d11STodd Fiala // Change the program counter. 2725af245d11STodd Fiala if (log) 2726b9cc0c75SPavel Labath log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": changing PC from 0x%" PRIx64 " to 0x%" PRIx64, __FUNCTION__, GetID(), thread.GetID(), initial_pc_addr, breakpoint_addr); 2727af245d11STodd Fiala 2728af245d11STodd Fiala error = context_sp->SetPC (breakpoint_addr); 2729af245d11STodd Fiala if (error.Fail ()) 2730af245d11STodd Fiala { 2731af245d11STodd Fiala if (log) 2732b9cc0c75SPavel Labath log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": failed to set PC: %s", __FUNCTION__, GetID(), thread.GetID(), error.AsCString ()); 2733af245d11STodd Fiala return error; 2734af245d11STodd Fiala } 2735af245d11STodd Fiala 2736af245d11STodd Fiala return error; 2737af245d11STodd Fiala } 2738fa03ad2eSChaoren Lin 27397cb18bf5STamas Berghammer Error 27407cb18bf5STamas Berghammer NativeProcessLinux::GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) 27417cb18bf5STamas Berghammer { 27427cb18bf5STamas Berghammer FileSpec module_file_spec(module_path, true); 27437cb18bf5STamas Berghammer 2744162fb8e8SPavel Labath bool found = false; 27457cb18bf5STamas Berghammer file_spec.Clear(); 2746162fb8e8SPavel Labath ProcFileReader::ProcessLineByLine(GetID(), "maps", 2747162fb8e8SPavel Labath [&] (const std::string &line) 2748162fb8e8SPavel Labath { 2749162fb8e8SPavel Labath SmallVector<StringRef, 16> columns; 2750162fb8e8SPavel Labath StringRef(line).split(columns, " ", -1, false); 2751162fb8e8SPavel Labath if (columns.size() < 6) 2752162fb8e8SPavel Labath return true; // continue searching 2753162fb8e8SPavel Labath 2754162fb8e8SPavel Labath FileSpec this_file_spec(columns[5].str().c_str(), false); 2755162fb8e8SPavel Labath if (this_file_spec.GetFilename() != module_file_spec.GetFilename()) 2756162fb8e8SPavel Labath return true; // continue searching 2757162fb8e8SPavel Labath 2758162fb8e8SPavel Labath file_spec = this_file_spec; 2759162fb8e8SPavel Labath found = true; 2760162fb8e8SPavel Labath return false; // we are done 2761162fb8e8SPavel Labath }); 2762162fb8e8SPavel Labath 2763162fb8e8SPavel Labath if (! found) 27647cb18bf5STamas Berghammer return Error("Module file (%s) not found in /proc/%" PRIu64 "/maps file!", 27657cb18bf5STamas Berghammer module_file_spec.GetFilename().AsCString(), GetID()); 2766162fb8e8SPavel Labath 2767162fb8e8SPavel Labath return Error(); 27687cb18bf5STamas Berghammer } 2769c076559aSPavel Labath 27705eb721edSPavel Labath Error 2771783bfc8cSTamas Berghammer NativeProcessLinux::GetFileLoadAddress(const llvm::StringRef& file_name, lldb::addr_t& load_addr) 2772783bfc8cSTamas Berghammer { 2773783bfc8cSTamas Berghammer load_addr = LLDB_INVALID_ADDRESS; 2774783bfc8cSTamas Berghammer Error error = ProcFileReader::ProcessLineByLine (GetID (), "maps", 2775783bfc8cSTamas Berghammer [&] (const std::string &line) -> bool 2776783bfc8cSTamas Berghammer { 2777783bfc8cSTamas Berghammer StringRef maps_row(line); 2778783bfc8cSTamas Berghammer 2779783bfc8cSTamas Berghammer SmallVector<StringRef, 16> maps_columns; 2780783bfc8cSTamas Berghammer maps_row.split(maps_columns, StringRef(" "), -1, false); 2781783bfc8cSTamas Berghammer 2782783bfc8cSTamas Berghammer if (maps_columns.size() < 6) 2783783bfc8cSTamas Berghammer { 2784783bfc8cSTamas Berghammer // Return true to continue reading the proc file 2785783bfc8cSTamas Berghammer return true; 2786783bfc8cSTamas Berghammer } 2787783bfc8cSTamas Berghammer 2788783bfc8cSTamas Berghammer if (maps_columns[5] == file_name) 2789783bfc8cSTamas Berghammer { 2790783bfc8cSTamas Berghammer StringExtractor addr_extractor(maps_columns[0].str().c_str()); 2791783bfc8cSTamas Berghammer load_addr = addr_extractor.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); 2792783bfc8cSTamas Berghammer 2793783bfc8cSTamas Berghammer // Return false to stop reading the proc file further 2794783bfc8cSTamas Berghammer return false; 2795783bfc8cSTamas Berghammer } 2796783bfc8cSTamas Berghammer 2797783bfc8cSTamas Berghammer // Return true to continue reading the proc file 2798783bfc8cSTamas Berghammer return true; 2799783bfc8cSTamas Berghammer }); 2800783bfc8cSTamas Berghammer return error; 2801783bfc8cSTamas Berghammer } 2802783bfc8cSTamas Berghammer 2803f9077782SPavel Labath NativeThreadLinuxSP 2804f9077782SPavel Labath NativeProcessLinux::GetThreadByID(lldb::tid_t tid) 2805f9077782SPavel Labath { 2806f9077782SPavel Labath return std::static_pointer_cast<NativeThreadLinux>(NativeProcessProtocol::GetThreadByID(tid)); 2807f9077782SPavel Labath } 2808f9077782SPavel Labath 2809783bfc8cSTamas Berghammer Error 2810b9cc0c75SPavel Labath NativeProcessLinux::ResumeThread(NativeThreadLinux &thread, lldb::StateType state, int signo) 2811c076559aSPavel Labath { 28125eb721edSPavel Labath Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 28135eb721edSPavel Labath 28141dbc6c9cSPavel Labath if (log) 28150e1d729bSPavel Labath log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ")", 2816b9cc0c75SPavel Labath __FUNCTION__, thread.GetID()); 2817c076559aSPavel Labath 2818c076559aSPavel Labath // Before we do the resume below, first check if we have a pending 2819108c325dSPavel Labath // stop notification that is currently waiting for 28200e1d729bSPavel Labath // all threads to stop. This is potentially a buggy situation since 2821c076559aSPavel Labath // we're ostensibly waiting for threads to stop before we send out the 2822c076559aSPavel Labath // pending notification, and here we are resuming one before we send 2823c076559aSPavel Labath // out the pending stop notification. 28240e1d729bSPavel Labath if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID && log) 2825c076559aSPavel Labath { 2826b9cc0c75SPavel Labath log->Printf("NativeProcessLinux::%s about to resume tid %" PRIu64 " per explicit request but we have a pending stop notification (tid %" PRIu64 ") that is actively waiting for this thread to stop. Valid sequence of events?", __FUNCTION__, thread.GetID(), m_pending_notification_tid); 2827c076559aSPavel Labath } 2828c076559aSPavel Labath 2829c076559aSPavel Labath // Request a resume. We expect this to be synchronous and the system 2830c076559aSPavel Labath // to reflect it is running after this completes. 28310e1d729bSPavel Labath switch (state) 2832c076559aSPavel Labath { 28330e1d729bSPavel Labath case eStateRunning: 28340e1d729bSPavel Labath { 2835605b51b8SPavel Labath const auto resume_result = thread.Resume(signo); 28360e1d729bSPavel Labath if (resume_result.Success()) 28370e1d729bSPavel Labath SetState(eStateRunning, true); 28380e1d729bSPavel Labath return resume_result; 2839c076559aSPavel Labath } 28400e1d729bSPavel Labath case eStateStepping: 28410e1d729bSPavel Labath { 2842605b51b8SPavel Labath const auto step_result = thread.SingleStep(signo); 28430e1d729bSPavel Labath if (step_result.Success()) 28440e1d729bSPavel Labath SetState(eStateRunning, true); 28450e1d729bSPavel Labath return step_result; 28460e1d729bSPavel Labath } 28470e1d729bSPavel Labath default: 28480e1d729bSPavel Labath if (log) 28490e1d729bSPavel Labath log->Printf("NativeProcessLinux::%s Unhandled state %s.", 28500e1d729bSPavel Labath __FUNCTION__, StateAsCString(state)); 28510e1d729bSPavel Labath llvm_unreachable("Unhandled state for resume"); 28520e1d729bSPavel Labath } 2853c076559aSPavel Labath } 2854c076559aSPavel Labath 2855c076559aSPavel Labath //===----------------------------------------------------------------------===// 2856c076559aSPavel Labath 2857c076559aSPavel Labath void 2858337f3eb9SPavel Labath NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid) 2859c076559aSPavel Labath { 28605eb721edSPavel Labath Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 2861c076559aSPavel Labath 28625eb721edSPavel Labath if (log) 2863c076559aSPavel Labath { 28645eb721edSPavel Labath log->Printf("NativeProcessLinux::%s about to process event: (triggering_tid: %" PRIu64 ")", 2865c076559aSPavel Labath __FUNCTION__, triggering_tid); 2866c076559aSPavel Labath } 2867c076559aSPavel Labath 28680e1d729bSPavel Labath m_pending_notification_tid = triggering_tid; 28690e1d729bSPavel Labath 28700e1d729bSPavel Labath // Request a stop for all the thread stops that need to be stopped 28710e1d729bSPavel Labath // and are not already known to be stopped. 28720e1d729bSPavel Labath for (const auto &thread_sp: m_threads) 28730e1d729bSPavel Labath { 28740e1d729bSPavel Labath if (StateIsRunningState(thread_sp->GetState())) 28750e1d729bSPavel Labath static_pointer_cast<NativeThreadLinux>(thread_sp)->RequestStop(); 28760e1d729bSPavel Labath } 28770e1d729bSPavel Labath 28780e1d729bSPavel Labath SignalIfAllThreadsStopped(); 2879c076559aSPavel Labath 28805eb721edSPavel Labath if (log) 2881c076559aSPavel Labath { 28825eb721edSPavel Labath log->Printf("NativeProcessLinux::%s event processing done", __FUNCTION__); 2883c076559aSPavel Labath } 2884c076559aSPavel Labath } 2885c076559aSPavel Labath 2886c076559aSPavel Labath void 28879eb1ecb9SPavel Labath NativeProcessLinux::SignalIfAllThreadsStopped() 2888c076559aSPavel Labath { 28890e1d729bSPavel Labath if (m_pending_notification_tid == LLDB_INVALID_THREAD_ID) 28900e1d729bSPavel Labath return; // No pending notification. Nothing to do. 28910e1d729bSPavel Labath 28920e1d729bSPavel Labath for (const auto &thread_sp: m_threads) 2893c076559aSPavel Labath { 28940e1d729bSPavel Labath if (StateIsRunningState(thread_sp->GetState())) 28950e1d729bSPavel Labath return; // Some threads are still running. Don't signal yet. 28960e1d729bSPavel Labath } 28970e1d729bSPavel Labath 28980e1d729bSPavel Labath // We have a pending notification and all threads have stopped. 28999eb1ecb9SPavel Labath Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 29009eb1ecb9SPavel Labath 29019eb1ecb9SPavel Labath // Clear any temporary breakpoints we used to implement software single stepping. 29029eb1ecb9SPavel Labath for (const auto &thread_info: m_threads_stepping_with_breakpoint) 29039eb1ecb9SPavel Labath { 29049eb1ecb9SPavel Labath Error error = RemoveBreakpoint (thread_info.second); 29059eb1ecb9SPavel Labath if (error.Fail()) 29069eb1ecb9SPavel Labath if (log) 29079eb1ecb9SPavel Labath log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " remove stepping breakpoint: %s", 29089eb1ecb9SPavel Labath __FUNCTION__, thread_info.first, error.AsCString()); 29099eb1ecb9SPavel Labath } 29109eb1ecb9SPavel Labath m_threads_stepping_with_breakpoint.clear(); 29119eb1ecb9SPavel Labath 29129eb1ecb9SPavel Labath // Notify the delegate about the stop 29130e1d729bSPavel Labath SetCurrentThreadID(m_pending_notification_tid); 2914ed89c7feSPavel Labath SetState(StateType::eStateStopped, true); 29150e1d729bSPavel Labath m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 2916c076559aSPavel Labath } 2917c076559aSPavel Labath 2918c076559aSPavel Labath void 2919f9077782SPavel Labath NativeProcessLinux::ThreadWasCreated(NativeThreadLinux &thread) 2920c076559aSPavel Labath { 29211dbc6c9cSPavel Labath Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 29221dbc6c9cSPavel Labath 29231dbc6c9cSPavel Labath if (log) 2924f9077782SPavel Labath log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ")", __FUNCTION__, thread.GetID()); 29251dbc6c9cSPavel Labath 2926f9077782SPavel Labath if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID && StateIsRunningState(thread.GetState())) 2927c076559aSPavel Labath { 2928c076559aSPavel Labath // We will need to wait for this new thread to stop as well before firing the 2929c076559aSPavel Labath // notification. 2930f9077782SPavel Labath thread.RequestStop(); 2931c076559aSPavel Labath } 2932c076559aSPavel Labath } 2933068f8a7eSTamas Berghammer 293419cbe96aSPavel Labath void 293519cbe96aSPavel Labath NativeProcessLinux::SigchldHandler() 2936068f8a7eSTamas Berghammer { 293719cbe96aSPavel Labath Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 293819cbe96aSPavel Labath // Process all pending waitpid notifications. 293919cbe96aSPavel Labath while (true) 294019cbe96aSPavel Labath { 294119cbe96aSPavel Labath int status = -1; 294219cbe96aSPavel Labath ::pid_t wait_pid = waitpid(-1, &status, __WALL | __WNOTHREAD | WNOHANG); 294319cbe96aSPavel Labath 294419cbe96aSPavel Labath if (wait_pid == 0) 294519cbe96aSPavel Labath break; // We are done. 294619cbe96aSPavel Labath 294719cbe96aSPavel Labath if (wait_pid == -1) 294819cbe96aSPavel Labath { 294919cbe96aSPavel Labath if (errno == EINTR) 295019cbe96aSPavel Labath continue; 295119cbe96aSPavel Labath 295219cbe96aSPavel Labath Error error(errno, eErrorTypePOSIX); 295319cbe96aSPavel Labath if (log) 295419cbe96aSPavel Labath log->Printf("NativeProcessLinux::%s waitpid (-1, &status, __WALL | __WNOTHREAD | WNOHANG) failed: %s", 295519cbe96aSPavel Labath __FUNCTION__, error.AsCString()); 295619cbe96aSPavel Labath break; 295719cbe96aSPavel Labath } 295819cbe96aSPavel Labath 295919cbe96aSPavel Labath bool exited = false; 296019cbe96aSPavel Labath int signal = 0; 296119cbe96aSPavel Labath int exit_status = 0; 296219cbe96aSPavel Labath const char *status_cstr = nullptr; 296319cbe96aSPavel Labath if (WIFSTOPPED(status)) 296419cbe96aSPavel Labath { 296519cbe96aSPavel Labath signal = WSTOPSIG(status); 296619cbe96aSPavel Labath status_cstr = "STOPPED"; 296719cbe96aSPavel Labath } 296819cbe96aSPavel Labath else if (WIFEXITED(status)) 296919cbe96aSPavel Labath { 297019cbe96aSPavel Labath exit_status = WEXITSTATUS(status); 297119cbe96aSPavel Labath status_cstr = "EXITED"; 297219cbe96aSPavel Labath exited = true; 297319cbe96aSPavel Labath } 297419cbe96aSPavel Labath else if (WIFSIGNALED(status)) 297519cbe96aSPavel Labath { 297619cbe96aSPavel Labath signal = WTERMSIG(status); 297719cbe96aSPavel Labath status_cstr = "SIGNALED"; 297819cbe96aSPavel Labath if (wait_pid == static_cast< ::pid_t>(GetID())) { 297919cbe96aSPavel Labath exited = true; 298019cbe96aSPavel Labath exit_status = -1; 298119cbe96aSPavel Labath } 298219cbe96aSPavel Labath } 298319cbe96aSPavel Labath else 298419cbe96aSPavel Labath status_cstr = "(\?\?\?)"; 298519cbe96aSPavel Labath 298619cbe96aSPavel Labath if (log) 298719cbe96aSPavel Labath log->Printf("NativeProcessLinux::%s: waitpid (-1, &status, __WALL | __WNOTHREAD | WNOHANG)" 298819cbe96aSPavel Labath "=> pid = %" PRIi32 ", status = 0x%8.8x (%s), signal = %i, exit_state = %i", 298919cbe96aSPavel Labath __FUNCTION__, wait_pid, status, status_cstr, signal, exit_status); 299019cbe96aSPavel Labath 299119cbe96aSPavel Labath MonitorCallback (wait_pid, exited, signal, exit_status); 299219cbe96aSPavel Labath } 2993068f8a7eSTamas Berghammer } 2994068f8a7eSTamas Berghammer 2995068f8a7eSTamas Berghammer // Wrapper for ptrace to catch errors and log calls. 2996068f8a7eSTamas Berghammer // Note that ptrace sets errno on error because -1 can be a valid result (i.e. for PTRACE_PEEK*) 29974a9babb2SPavel Labath Error 29984a9babb2SPavel Labath NativeProcessLinux::PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, long *result) 2999068f8a7eSTamas Berghammer { 30004a9babb2SPavel Labath Error error; 30014a9babb2SPavel Labath long int ret; 3002068f8a7eSTamas Berghammer 3003068f8a7eSTamas Berghammer Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE)); 3004068f8a7eSTamas Berghammer 3005068f8a7eSTamas Berghammer PtraceDisplayBytes(req, data, data_size); 3006068f8a7eSTamas Berghammer 3007068f8a7eSTamas Berghammer errno = 0; 3008068f8a7eSTamas Berghammer if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) 30094a9babb2SPavel Labath ret = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), *(unsigned int *)addr, data); 3010068f8a7eSTamas Berghammer else 30114a9babb2SPavel Labath ret = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), addr, data); 3012068f8a7eSTamas Berghammer 30134a9babb2SPavel Labath if (ret == -1) 3014068f8a7eSTamas Berghammer error.SetErrorToErrno(); 3015068f8a7eSTamas Berghammer 30164a9babb2SPavel Labath if (result) 30174a9babb2SPavel Labath *result = ret; 30184a9babb2SPavel Labath 3019068f8a7eSTamas Berghammer if (log) 30204a9babb2SPavel Labath log->Printf("ptrace(%d, %" PRIu64 ", %p, %p, %zu)=%lX", req, pid, addr, data, data_size, ret); 3021068f8a7eSTamas Berghammer 3022068f8a7eSTamas Berghammer PtraceDisplayBytes(req, data, data_size); 3023068f8a7eSTamas Berghammer 3024068f8a7eSTamas Berghammer if (log && error.GetError() != 0) 3025068f8a7eSTamas Berghammer { 3026068f8a7eSTamas Berghammer const char* str; 3027068f8a7eSTamas Berghammer switch (error.GetError()) 3028068f8a7eSTamas Berghammer { 3029068f8a7eSTamas Berghammer case ESRCH: str = "ESRCH"; break; 3030068f8a7eSTamas Berghammer case EINVAL: str = "EINVAL"; break; 3031068f8a7eSTamas Berghammer case EBUSY: str = "EBUSY"; break; 3032068f8a7eSTamas Berghammer case EPERM: str = "EPERM"; break; 3033068f8a7eSTamas Berghammer default: str = error.AsCString(); 3034068f8a7eSTamas Berghammer } 3035068f8a7eSTamas Berghammer log->Printf("ptrace() failed; errno=%d (%s)", error.GetError(), str); 3036068f8a7eSTamas Berghammer } 3037068f8a7eSTamas Berghammer 30384a9babb2SPavel Labath return error; 3039068f8a7eSTamas Berghammer } 3040