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" 32*5ad891f7SPavel Labath #include "lldb/Host/HostProcess.h" 3339de3110SZachary Turner #include "lldb/Host/ThreadLauncher.h" 342a86b555SPavel Labath #include "lldb/Host/common/NativeBreakpoint.h" 352a86b555SPavel Labath #include "lldb/Host/common/NativeRegisterContext.h" 36*5ad891f7SPavel Labath #include "lldb/Host/linux/ProcessLauncherLinux.h" 372a86b555SPavel Labath #include "lldb/Symbol/ObjectFile.h" 3890aff47cSZachary Turner #include "lldb/Target/Process.h" 39af245d11STodd Fiala #include "lldb/Target/ProcessLaunchInfo.h" 405b981ab9SPavel Labath #include "lldb/Target/Target.h" 41c16f5dcaSChaoren Lin #include "lldb/Utility/LLDBAssert.h" 42af245d11STodd Fiala #include "lldb/Utility/PseudoTerminal.h" 43f805e190SPavel Labath #include "lldb/Utility/StringExtractor.h" 44af245d11STodd Fiala 451e209fccSTamas Berghammer #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" 46af245d11STodd Fiala #include "NativeThreadLinux.h" 47af245d11STodd Fiala #include "ProcFileReader.h" 481e209fccSTamas Berghammer #include "Procfs.h" 49cacde7dfSTodd Fiala 50d858487eSTamas Berghammer // System includes - They have to be included after framework includes because they define some 51d858487eSTamas Berghammer // macros which collide with variable names in other modules 52d858487eSTamas Berghammer #include <linux/unistd.h> 53d858487eSTamas Berghammer #include <sys/socket.h> 548b335671SVince Harron 55df7c6995SPavel Labath #include <sys/syscall.h> 56d858487eSTamas Berghammer #include <sys/types.h> 57d858487eSTamas Berghammer #include <sys/user.h> 58d858487eSTamas Berghammer #include <sys/wait.h> 59d858487eSTamas Berghammer 608b335671SVince Harron #include "lldb/Host/linux/Personality.h" 618b335671SVince Harron #include "lldb/Host/linux/Ptrace.h" 62df7c6995SPavel Labath #include "lldb/Host/linux/Uio.h" 638b335671SVince Harron #include "lldb/Host/android/Android.h" 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 1132a86b555SPavel Labath ResolveProcessArchitecture(lldb::pid_t pid, ArchSpec &arch) 114af245d11STodd Fiala { 115af245d11STodd Fiala // Grab process info for the running process. 116af245d11STodd Fiala ProcessInstanceInfo process_info; 1172a86b555SPavel Labath if (!Host::GetProcessInfo(pid, process_info)) 118db264a6dSTamas Berghammer return Error("failed to get process info"); 119af245d11STodd Fiala 120af245d11STodd Fiala // Resolve the executable module. 1212a86b555SPavel Labath ModuleSpecList module_specs; 1222a86b555SPavel Labath if (!ObjectFile::GetModuleSpecifications(process_info.GetExecutableFile(), 0, 0, module_specs)) 1232a86b555SPavel Labath return Error("failed to get module specifications"); 1242a86b555SPavel Labath assert(module_specs.GetSize() == 1); 125af245d11STodd Fiala 1262a86b555SPavel 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 1344abe5d69SPavel Labath MaybeLogLaunchInfo(const ProcessLaunchInfo &info) 1354abe5d69SPavel Labath { 1364abe5d69SPavel Labath Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 1374abe5d69SPavel Labath if (!log) 1384abe5d69SPavel Labath return; 1394abe5d69SPavel Labath 1404abe5d69SPavel Labath if (const FileAction *action = info.GetFileActionForFD(STDIN_FILENO)) 1414abe5d69SPavel Labath log->Printf("%s: setting STDIN to '%s'", __FUNCTION__, action->GetFileSpec().GetCString()); 1424abe5d69SPavel Labath else 1434abe5d69SPavel Labath log->Printf("%s leaving STDIN as is", __FUNCTION__); 1444abe5d69SPavel Labath 1454abe5d69SPavel Labath if (const FileAction *action = info.GetFileActionForFD(STDOUT_FILENO)) 1464abe5d69SPavel Labath log->Printf("%s setting STDOUT to '%s'", __FUNCTION__, action->GetFileSpec().GetCString()); 1474abe5d69SPavel Labath else 1484abe5d69SPavel Labath log->Printf("%s leaving STDOUT as is", __FUNCTION__); 1494abe5d69SPavel Labath 1504abe5d69SPavel Labath if (const FileAction *action = info.GetFileActionForFD(STDERR_FILENO)) 1514abe5d69SPavel Labath log->Printf("%s setting STDERR to '%s'", __FUNCTION__, action->GetFileSpec().GetCString()); 1524abe5d69SPavel Labath else 1534abe5d69SPavel Labath log->Printf("%s leaving STDERR as is", __FUNCTION__); 1544abe5d69SPavel Labath 1554abe5d69SPavel Labath int i = 0; 1564abe5d69SPavel Labath for (const char **args = info.GetArguments().GetConstArgumentVector(); *args; ++args, ++i) 1574abe5d69SPavel Labath log->Printf("%s arg %d: \"%s\"", __FUNCTION__, i, *args ? *args : "nullptr"); 1584abe5d69SPavel Labath } 1594abe5d69SPavel Labath 1604abe5d69SPavel Labath void 161db264a6dSTamas Berghammer DisplayBytes(StreamString &s, void *bytes, uint32_t count) 162af245d11STodd Fiala { 163af245d11STodd Fiala uint8_t *ptr = (uint8_t *)bytes; 164af245d11STodd Fiala const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count); 165af245d11STodd Fiala for (uint32_t i = 0; i < loop_count; i++) 166af245d11STodd Fiala { 167af245d11STodd Fiala s.Printf("[%x]", *ptr); 168af245d11STodd Fiala ptr++; 169af245d11STodd Fiala } 170af245d11STodd Fiala } 171af245d11STodd Fiala 172af245d11STodd Fiala void 173af245d11STodd Fiala PtraceDisplayBytes(int &req, void *data, size_t data_size) 174af245d11STodd Fiala { 175af245d11STodd Fiala StreamString buf; 176af245d11STodd Fiala Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet ( 177af245d11STodd Fiala POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE)); 178af245d11STodd Fiala 179af245d11STodd Fiala if (verbose_log) 180af245d11STodd Fiala { 181af245d11STodd Fiala switch(req) 182af245d11STodd Fiala { 183af245d11STodd Fiala case PTRACE_POKETEXT: 184af245d11STodd Fiala { 185af245d11STodd Fiala DisplayBytes(buf, &data, 8); 186af245d11STodd Fiala verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData()); 187af245d11STodd Fiala break; 188af245d11STodd Fiala } 189af245d11STodd Fiala case PTRACE_POKEDATA: 190af245d11STodd Fiala { 191af245d11STodd Fiala DisplayBytes(buf, &data, 8); 192af245d11STodd Fiala verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData()); 193af245d11STodd Fiala break; 194af245d11STodd Fiala } 195af245d11STodd Fiala case PTRACE_POKEUSER: 196af245d11STodd Fiala { 197af245d11STodd Fiala DisplayBytes(buf, &data, 8); 198af245d11STodd Fiala verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData()); 199af245d11STodd Fiala break; 200af245d11STodd Fiala } 201af245d11STodd Fiala case PTRACE_SETREGS: 202af245d11STodd Fiala { 203af245d11STodd Fiala DisplayBytes(buf, data, data_size); 204af245d11STodd Fiala verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData()); 205af245d11STodd Fiala break; 206af245d11STodd Fiala } 207af245d11STodd Fiala case PTRACE_SETFPREGS: 208af245d11STodd Fiala { 209af245d11STodd Fiala DisplayBytes(buf, data, data_size); 210af245d11STodd Fiala verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData()); 211af245d11STodd Fiala break; 212af245d11STodd Fiala } 213af245d11STodd Fiala case PTRACE_SETSIGINFO: 214af245d11STodd Fiala { 215af245d11STodd Fiala DisplayBytes(buf, data, sizeof(siginfo_t)); 216af245d11STodd Fiala verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData()); 217af245d11STodd Fiala break; 218af245d11STodd Fiala } 219af245d11STodd Fiala case PTRACE_SETREGSET: 220af245d11STodd Fiala { 221af245d11STodd Fiala // Extract iov_base from data, which is a pointer to the struct IOVEC 222af245d11STodd Fiala DisplayBytes(buf, *(void **)data, data_size); 223af245d11STodd Fiala verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData()); 224af245d11STodd Fiala break; 225af245d11STodd Fiala } 226af245d11STodd Fiala default: 227af245d11STodd Fiala { 228af245d11STodd Fiala } 229af245d11STodd Fiala } 230af245d11STodd Fiala } 231af245d11STodd Fiala } 232af245d11STodd Fiala 23319cbe96aSPavel Labath static constexpr unsigned k_ptrace_word_size = sizeof(void*); 23419cbe96aSPavel Labath static_assert(sizeof(long) >= k_ptrace_word_size, "Size of long must be larger than ptrace word size"); 2351107b5a5SPavel Labath } // end of anonymous namespace 2361107b5a5SPavel Labath 237bd7cbc5aSPavel Labath // Simple helper function to ensure flags are enabled on the given file 238bd7cbc5aSPavel Labath // descriptor. 239bd7cbc5aSPavel Labath static Error 240bd7cbc5aSPavel Labath EnsureFDFlags(int fd, int flags) 241bd7cbc5aSPavel Labath { 242bd7cbc5aSPavel Labath Error error; 243bd7cbc5aSPavel Labath 244bd7cbc5aSPavel Labath int status = fcntl(fd, F_GETFL); 245bd7cbc5aSPavel Labath if (status == -1) 246bd7cbc5aSPavel Labath { 247bd7cbc5aSPavel Labath error.SetErrorToErrno(); 248bd7cbc5aSPavel Labath return error; 249bd7cbc5aSPavel Labath } 250bd7cbc5aSPavel Labath 251bd7cbc5aSPavel Labath if (fcntl(fd, F_SETFL, status | flags) == -1) 252bd7cbc5aSPavel Labath { 253bd7cbc5aSPavel Labath error.SetErrorToErrno(); 254bd7cbc5aSPavel Labath return error; 255bd7cbc5aSPavel Labath } 256bd7cbc5aSPavel Labath 257bd7cbc5aSPavel Labath return error; 258bd7cbc5aSPavel Labath } 259bd7cbc5aSPavel Labath 260af245d11STodd Fiala // ----------------------------------------------------------------------------- 261af245d11STodd Fiala // Public Static Methods 262af245d11STodd Fiala // ----------------------------------------------------------------------------- 263af245d11STodd Fiala 264db264a6dSTamas Berghammer Error 265d5b310f2SPavel Labath NativeProcessProtocol::Launch ( 266db264a6dSTamas Berghammer ProcessLaunchInfo &launch_info, 267db264a6dSTamas Berghammer NativeProcessProtocol::NativeDelegate &native_delegate, 26819cbe96aSPavel Labath MainLoop &mainloop, 269af245d11STodd Fiala NativeProcessProtocolSP &native_process_sp) 270af245d11STodd Fiala { 271af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 272af245d11STodd Fiala 2732a86b555SPavel Labath Error error; 274af245d11STodd Fiala 275af245d11STodd Fiala // Verify the working directory is valid if one was specified. 276d3173f34SChaoren Lin FileSpec working_dir{launch_info.GetWorkingDirectory()}; 277d3173f34SChaoren Lin if (working_dir && 278d3173f34SChaoren Lin (!working_dir.ResolvePath() || 279d3173f34SChaoren Lin working_dir.GetFileType() != FileSpec::eFileTypeDirectory)) 280af245d11STodd Fiala { 281d3173f34SChaoren Lin error.SetErrorStringWithFormat ("No such file or directory: %s", 282d3173f34SChaoren Lin working_dir.GetCString()); 283af245d11STodd Fiala return error; 284af245d11STodd Fiala } 285af245d11STodd Fiala 286af245d11STodd Fiala // Create the NativeProcessLinux in launch mode. 287af245d11STodd Fiala native_process_sp.reset (new NativeProcessLinux ()); 288af245d11STodd Fiala 289af245d11STodd Fiala if (!native_process_sp->RegisterNativeDelegate (native_delegate)) 290af245d11STodd Fiala { 291af245d11STodd Fiala native_process_sp.reset (); 292af245d11STodd Fiala error.SetErrorStringWithFormat ("failed to register the native delegate"); 293af245d11STodd Fiala return error; 294af245d11STodd Fiala } 295af245d11STodd Fiala 2964abe5d69SPavel Labath error = std::static_pointer_cast<NativeProcessLinux>(native_process_sp)->LaunchInferior(mainloop, launch_info); 297af245d11STodd Fiala 298af245d11STodd Fiala if (error.Fail ()) 299af245d11STodd Fiala { 300af245d11STodd Fiala native_process_sp.reset (); 301af245d11STodd Fiala if (log) 302af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to launch process: %s", __FUNCTION__, error.AsCString ()); 303af245d11STodd Fiala return error; 304af245d11STodd Fiala } 305af245d11STodd Fiala 306af245d11STodd Fiala launch_info.SetProcessID (native_process_sp->GetID ()); 307af245d11STodd Fiala 308af245d11STodd Fiala return error; 309af245d11STodd Fiala } 310af245d11STodd Fiala 311db264a6dSTamas Berghammer Error 312d5b310f2SPavel Labath NativeProcessProtocol::Attach ( 313af245d11STodd Fiala lldb::pid_t pid, 314db264a6dSTamas Berghammer NativeProcessProtocol::NativeDelegate &native_delegate, 31519cbe96aSPavel Labath MainLoop &mainloop, 316af245d11STodd Fiala NativeProcessProtocolSP &native_process_sp) 317af245d11STodd Fiala { 318af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 319af245d11STodd Fiala if (log && log->GetMask ().Test (POSIX_LOG_VERBOSE)) 320af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(pid = %" PRIi64 ")", __FUNCTION__, pid); 321af245d11STodd Fiala 322af245d11STodd Fiala // Retrieve the architecture for the running process. 323af245d11STodd Fiala ArchSpec process_arch; 3242a86b555SPavel Labath Error error = ResolveProcessArchitecture(pid, process_arch); 325af245d11STodd Fiala if (!error.Success ()) 326af245d11STodd Fiala return error; 327af245d11STodd Fiala 3281339b5e8SOleksiy Vyalov std::shared_ptr<NativeProcessLinux> native_process_linux_sp (new NativeProcessLinux ()); 329af245d11STodd Fiala 3301339b5e8SOleksiy Vyalov if (!native_process_linux_sp->RegisterNativeDelegate (native_delegate)) 331af245d11STodd Fiala { 332af245d11STodd Fiala error.SetErrorStringWithFormat ("failed to register the native delegate"); 333af245d11STodd Fiala return error; 334af245d11STodd Fiala } 335af245d11STodd Fiala 33619cbe96aSPavel Labath native_process_linux_sp->AttachToInferior (mainloop, pid, error); 337af245d11STodd Fiala if (!error.Success ()) 338af245d11STodd Fiala return error; 339af245d11STodd Fiala 3401339b5e8SOleksiy Vyalov native_process_sp = native_process_linux_sp; 341af245d11STodd Fiala return error; 342af245d11STodd Fiala } 343af245d11STodd Fiala 344af245d11STodd Fiala // ----------------------------------------------------------------------------- 345af245d11STodd Fiala // Public Instance Methods 346af245d11STodd Fiala // ----------------------------------------------------------------------------- 347af245d11STodd Fiala 348af245d11STodd Fiala NativeProcessLinux::NativeProcessLinux () : 349af245d11STodd Fiala NativeProcessProtocol (LLDB_INVALID_PROCESS_ID), 350af245d11STodd Fiala m_arch (), 351af245d11STodd Fiala m_supports_mem_region (eLazyBoolCalculate), 352af245d11STodd Fiala m_mem_region_cache (), 3530e1d729bSPavel Labath m_pending_notification_tid(LLDB_INVALID_THREAD_ID) 354af245d11STodd Fiala { 355af245d11STodd Fiala } 356af245d11STodd Fiala 357af245d11STodd Fiala void 35819cbe96aSPavel Labath NativeProcessLinux::AttachToInferior (MainLoop &mainloop, lldb::pid_t pid, Error &error) 359af245d11STodd Fiala { 360af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 361af245d11STodd Fiala if (log) 362af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ")", __FUNCTION__, pid); 363af245d11STodd Fiala 36419cbe96aSPavel Labath m_sigchld_handle = mainloop.RegisterSignal(SIGCHLD, 36519cbe96aSPavel Labath [this] (MainLoopBase &) { SigchldHandler(); }, error); 36619cbe96aSPavel Labath if (! m_sigchld_handle) 36719cbe96aSPavel Labath return; 36819cbe96aSPavel Labath 3692a86b555SPavel Labath error = ResolveProcessArchitecture(pid, m_arch); 370af245d11STodd Fiala if (!error.Success()) 371af245d11STodd Fiala return; 372af245d11STodd Fiala 373af245d11STodd Fiala // Set the architecture to the exe architecture. 374af245d11STodd Fiala if (log) 375af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ") detected architecture %s", __FUNCTION__, pid, m_arch.GetArchitectureName ()); 376af245d11STodd Fiala 377af245d11STodd Fiala m_pid = pid; 378af245d11STodd Fiala SetState(eStateAttaching); 379af245d11STodd Fiala 38019cbe96aSPavel Labath Attach(pid, error); 381af245d11STodd Fiala } 382af245d11STodd Fiala 3834abe5d69SPavel Labath Error 3844abe5d69SPavel Labath NativeProcessLinux::LaunchInferior(MainLoop &mainloop, ProcessLaunchInfo &launch_info) 3850c4f01d4SPavel Labath { 3864abe5d69SPavel Labath Error error; 3874abe5d69SPavel Labath m_sigchld_handle = mainloop.RegisterSignal(SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, error); 3884abe5d69SPavel Labath if (!m_sigchld_handle) 3894abe5d69SPavel Labath return error; 3904abe5d69SPavel Labath 3914abe5d69SPavel Labath SetState(eStateLaunching); 3920c4f01d4SPavel Labath 3934abe5d69SPavel Labath MaybeLogLaunchInfo(launch_info); 3944abe5d69SPavel Labath 395*5ad891f7SPavel Labath ::pid_t pid = ProcessLauncherLinux().LaunchProcess(launch_info, error).GetProcessId(); 396*5ad891f7SPavel Labath if (error.Fail()) 3974abe5d69SPavel Labath return error; 3980c4f01d4SPavel Labath 39975f47c3aSTodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 40075f47c3aSTodd Fiala 401af245d11STodd Fiala // Wait for the child process to trap on its call to execve. 402af245d11STodd Fiala ::pid_t wpid; 403af245d11STodd Fiala int status; 404af245d11STodd Fiala if ((wpid = waitpid(pid, &status, 0)) < 0) 405af245d11STodd Fiala { 406bd7cbc5aSPavel Labath error.SetErrorToErrno(); 407af245d11STodd Fiala if (log) 408bd7cbc5aSPavel Labath log->Printf ("NativeProcessLinux::%s waitpid for inferior failed with %s", 409bd7cbc5aSPavel Labath __FUNCTION__, error.AsCString ()); 410af245d11STodd Fiala 411af245d11STodd Fiala // Mark the inferior as invalid. 412af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 413bd7cbc5aSPavel Labath SetState (StateType::eStateInvalid); 414af245d11STodd Fiala 4154abe5d69SPavel Labath return error; 416af245d11STodd Fiala } 417af245d11STodd Fiala assert(WIFSTOPPED(status) && (wpid == static_cast< ::pid_t> (pid)) && 418af245d11STodd Fiala "Could not sync with inferior process."); 419af245d11STodd Fiala 420af245d11STodd Fiala if (log) 421af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior started, now in stopped state", __FUNCTION__); 422af245d11STodd Fiala 423bd7cbc5aSPavel Labath error = SetDefaultPtraceOpts(pid); 424bd7cbc5aSPavel Labath if (error.Fail()) 425af245d11STodd Fiala { 426af245d11STodd Fiala if (log) 427af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s inferior failed to set default ptrace options: %s", 428bd7cbc5aSPavel Labath __FUNCTION__, error.AsCString ()); 429af245d11STodd Fiala 430af245d11STodd Fiala // Mark the inferior as invalid. 431af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 432bd7cbc5aSPavel Labath SetState (StateType::eStateInvalid); 433af245d11STodd Fiala 4344abe5d69SPavel Labath return error; 435af245d11STodd Fiala } 436af245d11STodd Fiala 437af245d11STodd Fiala // Release the master terminal descriptor and pass it off to the 438af245d11STodd Fiala // NativeProcessLinux instance. Similarly stash the inferior pid. 439*5ad891f7SPavel Labath m_terminal_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor(); 440bd7cbc5aSPavel Labath m_pid = pid; 4414abe5d69SPavel Labath launch_info.SetProcessID(pid); 442af245d11STodd Fiala 443*5ad891f7SPavel Labath if (m_terminal_fd != -1) 444*5ad891f7SPavel Labath { 445bd7cbc5aSPavel Labath error = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); 446bd7cbc5aSPavel Labath if (error.Fail()) 447af245d11STodd Fiala { 448af245d11STodd Fiala if (log) 449*5ad891f7SPavel Labath log->Printf( 450*5ad891f7SPavel Labath "NativeProcessLinux::%s inferior EnsureFDFlags failed for ensuring terminal O_NONBLOCK setting: %s", 451bd7cbc5aSPavel Labath __FUNCTION__, error.AsCString()); 452af245d11STodd Fiala 453af245d11STodd Fiala // Mark the inferior as invalid. 454af245d11STodd Fiala // FIXME this could really use a new state - eStateLaunchFailure. For now, using eStateInvalid. 455bd7cbc5aSPavel Labath SetState(StateType::eStateInvalid); 456af245d11STodd Fiala 4574abe5d69SPavel Labath return error; 458af245d11STodd Fiala } 459*5ad891f7SPavel Labath } 460af245d11STodd Fiala 461af245d11STodd Fiala if (log) 462*5ad891f7SPavel Labath log->Printf("NativeProcessLinux::%s() adding pid = %" PRIu64, __FUNCTION__, uint64_t(pid)); 463af245d11STodd Fiala 4642a86b555SPavel Labath ResolveProcessArchitecture(m_pid, m_arch); 465f9077782SPavel Labath NativeThreadLinuxSP thread_sp = AddThread(pid); 466af245d11STodd Fiala assert (thread_sp && "AddThread() returned a nullptr thread"); 467f9077782SPavel Labath thread_sp->SetStoppedBySignal(SIGSTOP); 468f9077782SPavel Labath ThreadWasCreated(*thread_sp); 469af245d11STodd Fiala 470af245d11STodd Fiala // Let our process instance know the thread has stopped. 471bd7cbc5aSPavel Labath SetCurrentThreadID (thread_sp->GetID ()); 472bd7cbc5aSPavel Labath SetState (StateType::eStateStopped); 473af245d11STodd Fiala 474af245d11STodd Fiala if (log) 475af245d11STodd Fiala { 476bd7cbc5aSPavel Labath if (error.Success ()) 477af245d11STodd Fiala log->Printf("NativeProcessLinux::%s inferior launching succeeded", __FUNCTION__); 478af245d11STodd Fiala else 4794abe5d69SPavel Labath log->Printf("NativeProcessLinux::%s inferior launching failed: %s", __FUNCTION__, error.AsCString()); 480af245d11STodd Fiala } 4814abe5d69SPavel Labath return error; 482af245d11STodd Fiala } 483af245d11STodd Fiala 484bd7cbc5aSPavel Labath ::pid_t 485bd7cbc5aSPavel Labath NativeProcessLinux::Attach(lldb::pid_t pid, Error &error) 486af245d11STodd Fiala { 487af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 488af245d11STodd Fiala 489af245d11STodd Fiala // Use a map to keep track of the threads which we have attached/need to attach. 490af245d11STodd Fiala Host::TidMap tids_to_attach; 491af245d11STodd Fiala if (pid <= 1) 492af245d11STodd Fiala { 493bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 494bd7cbc5aSPavel Labath error.SetErrorString("Attaching to process 1 is not allowed."); 495bd7cbc5aSPavel Labath return -1; 496af245d11STodd Fiala } 497af245d11STodd Fiala 498af245d11STodd Fiala while (Host::FindProcessThreads(pid, tids_to_attach)) 499af245d11STodd Fiala { 500af245d11STodd Fiala for (Host::TidMap::iterator it = tids_to_attach.begin(); 501af245d11STodd Fiala it != tids_to_attach.end();) 502af245d11STodd Fiala { 503af245d11STodd Fiala if (it->second == false) 504af245d11STodd Fiala { 505af245d11STodd Fiala lldb::tid_t tid = it->first; 506af245d11STodd Fiala 507af245d11STodd Fiala // Attach to the requested process. 508af245d11STodd Fiala // An attach will cause the thread to stop with a SIGSTOP. 5094a9babb2SPavel Labath error = PtraceWrapper(PTRACE_ATTACH, tid); 510bd7cbc5aSPavel Labath if (error.Fail()) 511af245d11STodd Fiala { 512af245d11STodd Fiala // No such thread. The thread may have exited. 513af245d11STodd Fiala // More error handling may be needed. 514bd7cbc5aSPavel Labath if (error.GetError() == ESRCH) 515af245d11STodd Fiala { 516af245d11STodd Fiala it = tids_to_attach.erase(it); 517af245d11STodd Fiala continue; 518af245d11STodd Fiala } 519af245d11STodd Fiala else 520bd7cbc5aSPavel Labath return -1; 521af245d11STodd Fiala } 522af245d11STodd Fiala 523af245d11STodd Fiala int status; 524af245d11STodd Fiala // Need to use __WALL otherwise we receive an error with errno=ECHLD 525af245d11STodd Fiala // At this point we should have a thread stopped if waitpid succeeds. 526af245d11STodd Fiala if ((status = waitpid(tid, NULL, __WALL)) < 0) 527af245d11STodd Fiala { 528af245d11STodd Fiala // No such thread. The thread may have exited. 529af245d11STodd Fiala // More error handling may be needed. 530af245d11STodd Fiala if (errno == ESRCH) 531af245d11STodd Fiala { 532af245d11STodd Fiala it = tids_to_attach.erase(it); 533af245d11STodd Fiala continue; 534af245d11STodd Fiala } 535af245d11STodd Fiala else 536af245d11STodd Fiala { 537bd7cbc5aSPavel Labath error.SetErrorToErrno(); 538bd7cbc5aSPavel Labath return -1; 539af245d11STodd Fiala } 540af245d11STodd Fiala } 541af245d11STodd Fiala 542bd7cbc5aSPavel Labath error = SetDefaultPtraceOpts(tid); 543bd7cbc5aSPavel Labath if (error.Fail()) 544bd7cbc5aSPavel Labath return -1; 545af245d11STodd Fiala 546af245d11STodd Fiala if (log) 547af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() adding tid = %" PRIu64, __FUNCTION__, tid); 548af245d11STodd Fiala 549af245d11STodd Fiala it->second = true; 550af245d11STodd Fiala 551af245d11STodd Fiala // Create the thread, mark it as stopped. 552f9077782SPavel Labath NativeThreadLinuxSP thread_sp (AddThread(static_cast<lldb::tid_t>(tid))); 553af245d11STodd Fiala assert (thread_sp && "AddThread() returned a nullptr"); 554fa03ad2eSChaoren Lin 555fa03ad2eSChaoren Lin // This will notify this is a new thread and tell the system it is stopped. 556f9077782SPavel Labath thread_sp->SetStoppedBySignal(SIGSTOP); 557f9077782SPavel Labath ThreadWasCreated(*thread_sp); 558bd7cbc5aSPavel Labath SetCurrentThreadID (thread_sp->GetID ()); 559af245d11STodd Fiala } 560af245d11STodd Fiala 561af245d11STodd Fiala // move the loop forward 562af245d11STodd Fiala ++it; 563af245d11STodd Fiala } 564af245d11STodd Fiala } 565af245d11STodd Fiala 566af245d11STodd Fiala if (tids_to_attach.size() > 0) 567af245d11STodd Fiala { 568bd7cbc5aSPavel Labath m_pid = pid; 569af245d11STodd Fiala // Let our process instance know the thread has stopped. 570bd7cbc5aSPavel Labath SetState (StateType::eStateStopped); 571af245d11STodd Fiala } 572af245d11STodd Fiala else 573af245d11STodd Fiala { 574bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 575bd7cbc5aSPavel Labath error.SetErrorString("No such process."); 576bd7cbc5aSPavel Labath return -1; 577af245d11STodd Fiala } 578af245d11STodd Fiala 579bd7cbc5aSPavel Labath return pid; 580af245d11STodd Fiala } 581af245d11STodd Fiala 58297ccc294SChaoren Lin Error 583af245d11STodd Fiala NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) 584af245d11STodd Fiala { 585af245d11STodd Fiala long ptrace_opts = 0; 586af245d11STodd Fiala 587af245d11STodd Fiala // Have the child raise an event on exit. This is used to keep the child in 588af245d11STodd Fiala // limbo until it is destroyed. 589af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXIT; 590af245d11STodd Fiala 591af245d11STodd Fiala // Have the tracer trace threads which spawn in the inferior process. 592af245d11STodd Fiala // TODO: if we want to support tracing the inferiors' child, add the 593af245d11STodd Fiala // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK) 594af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACECLONE; 595af245d11STodd Fiala 596af245d11STodd Fiala // Have the tracer notify us before execve returns 597af245d11STodd Fiala // (needed to disable legacy SIGTRAP generation) 598af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXEC; 599af245d11STodd Fiala 6004a9babb2SPavel Labath return PtraceWrapper(PTRACE_SETOPTIONS, pid, nullptr, (void*)ptrace_opts); 601af245d11STodd Fiala } 602af245d11STodd Fiala 603af245d11STodd Fiala static ExitType convert_pid_status_to_exit_type (int status) 604af245d11STodd Fiala { 605af245d11STodd Fiala if (WIFEXITED (status)) 606af245d11STodd Fiala return ExitType::eExitTypeExit; 607af245d11STodd Fiala else if (WIFSIGNALED (status)) 608af245d11STodd Fiala return ExitType::eExitTypeSignal; 609af245d11STodd Fiala else if (WIFSTOPPED (status)) 610af245d11STodd Fiala return ExitType::eExitTypeStop; 611af245d11STodd Fiala else 612af245d11STodd Fiala { 613af245d11STodd Fiala // We don't know what this is. 614af245d11STodd Fiala return ExitType::eExitTypeInvalid; 615af245d11STodd Fiala } 616af245d11STodd Fiala } 617af245d11STodd Fiala 618af245d11STodd Fiala static int convert_pid_status_to_return_code (int status) 619af245d11STodd Fiala { 620af245d11STodd Fiala if (WIFEXITED (status)) 621af245d11STodd Fiala return WEXITSTATUS (status); 622af245d11STodd Fiala else if (WIFSIGNALED (status)) 623af245d11STodd Fiala return WTERMSIG (status); 624af245d11STodd Fiala else if (WIFSTOPPED (status)) 625af245d11STodd Fiala return WSTOPSIG (status); 626af245d11STodd Fiala else 627af245d11STodd Fiala { 628af245d11STodd Fiala // We don't know what this is. 629af245d11STodd Fiala return ExitType::eExitTypeInvalid; 630af245d11STodd Fiala } 631af245d11STodd Fiala } 632af245d11STodd Fiala 6331107b5a5SPavel Labath // Handles all waitpid events from the inferior process. 6341107b5a5SPavel Labath void 6351107b5a5SPavel Labath NativeProcessLinux::MonitorCallback(lldb::pid_t pid, 636af245d11STodd Fiala bool exited, 637af245d11STodd Fiala int signal, 638af245d11STodd Fiala int status) 639af245d11STodd Fiala { 640af245d11STodd Fiala Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); 641af245d11STodd Fiala 642af245d11STodd Fiala // Certain activities differ based on whether the pid is the tid of the main thread. 6431107b5a5SPavel Labath const bool is_main_thread = (pid == GetID ()); 644af245d11STodd Fiala 645af245d11STodd Fiala // Handle when the thread exits. 646af245d11STodd Fiala if (exited) 647af245d11STodd Fiala { 648af245d11STodd Fiala if (log) 64986fd8e45SChaoren Lin log->Printf ("NativeProcessLinux::%s() got exit signal(%d) , tid = %" PRIu64 " (%s main thread)", __FUNCTION__, signal, pid, is_main_thread ? "is" : "is not"); 650af245d11STodd Fiala 651af245d11STodd Fiala // This is a thread that exited. Ensure we're not tracking it anymore. 6521107b5a5SPavel Labath const bool thread_found = StopTrackingThread (pid); 653af245d11STodd Fiala 654af245d11STodd Fiala if (is_main_thread) 655af245d11STodd Fiala { 656af245d11STodd Fiala // We only set the exit status and notify the delegate if we haven't already set the process 657af245d11STodd Fiala // state to an exited state. We normally should have received a SIGTRAP | (PTRACE_EVENT_EXIT << 8) 658af245d11STodd Fiala // for the main thread. 6591107b5a5SPavel Labath const bool already_notified = (GetState() == StateType::eStateExited) || (GetState () == StateType::eStateCrashed); 660af245d11STodd Fiala if (!already_notified) 661af245d11STodd Fiala { 662af245d11STodd Fiala if (log) 6631107b5a5SPavel 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 ())); 664af245d11STodd Fiala // The main thread exited. We're done monitoring. Report to delegate. 6651107b5a5SPavel Labath SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); 666af245d11STodd Fiala 667af245d11STodd Fiala // Notify delegate that our process has exited. 6681107b5a5SPavel Labath SetState (StateType::eStateExited, true); 669af245d11STodd Fiala } 670af245d11STodd Fiala else 671af245d11STodd Fiala { 672af245d11STodd Fiala if (log) 673af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() tid = %" PRIu64 " main thread now exited (%s)", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found"); 674af245d11STodd Fiala } 675af245d11STodd Fiala } 676af245d11STodd Fiala else 677af245d11STodd Fiala { 678af245d11STodd Fiala // Do we want to report to the delegate in this case? I think not. If this was an orderly 679af245d11STodd Fiala // thread exit, we would already have received the SIGTRAP | (PTRACE_EVENT_EXIT << 8) signal, 680af245d11STodd Fiala // and we would have done an all-stop then. 681af245d11STodd Fiala if (log) 682af245d11STodd 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"); 683af245d11STodd Fiala } 6841107b5a5SPavel Labath return; 685af245d11STodd Fiala } 686af245d11STodd Fiala 687af245d11STodd Fiala siginfo_t info; 688b9cc0c75SPavel Labath const auto info_err = GetSignalInfo(pid, &info); 689b9cc0c75SPavel Labath auto thread_sp = GetThreadByID(pid); 690b9cc0c75SPavel Labath 691b9cc0c75SPavel Labath if (! thread_sp) 692b9cc0c75SPavel Labath { 693b9cc0c75SPavel Labath // Normally, the only situation when we cannot find the thread is if we have just 694b9cc0c75SPavel Labath // received a new thread notification. This is indicated by GetSignalInfo() returning 695b9cc0c75SPavel Labath // si_code == SI_USER and si_pid == 0 696b9cc0c75SPavel Labath if (log) 697b9cc0c75SPavel Labath log->Printf("NativeProcessLinux::%s received notification about an unknown tid %" PRIu64 ".", __FUNCTION__, pid); 698b9cc0c75SPavel Labath 699b9cc0c75SPavel Labath if (info_err.Fail()) 700b9cc0c75SPavel Labath { 701b9cc0c75SPavel Labath if (log) 702b9cc0c75SPavel Labath log->Printf("NativeProcessLinux::%s (tid %" PRIu64 ") GetSignalInfo failed (%s). Ingoring this notification.", __FUNCTION__, pid, info_err.AsCString()); 703b9cc0c75SPavel Labath return; 704b9cc0c75SPavel Labath } 705b9cc0c75SPavel Labath 706b9cc0c75SPavel Labath if (log && (info.si_code != SI_USER || info.si_pid != 0)) 707b9cc0c75SPavel 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); 708b9cc0c75SPavel Labath 709b9cc0c75SPavel Labath auto thread_sp = AddThread(pid); 710b9cc0c75SPavel Labath // Resume the newly created thread. 711b9cc0c75SPavel Labath ResumeThread(*thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); 712b9cc0c75SPavel Labath ThreadWasCreated(*thread_sp); 713b9cc0c75SPavel Labath return; 714b9cc0c75SPavel Labath } 715b9cc0c75SPavel Labath 716b9cc0c75SPavel Labath // Get details on the signal raised. 717b9cc0c75SPavel Labath if (info_err.Success()) 718fa03ad2eSChaoren Lin { 719fa03ad2eSChaoren Lin // We have retrieved the signal info. Dispatch appropriately. 720fa03ad2eSChaoren Lin if (info.si_signo == SIGTRAP) 721b9cc0c75SPavel Labath MonitorSIGTRAP(info, *thread_sp); 722fa03ad2eSChaoren Lin else 723b9cc0c75SPavel Labath MonitorSignal(info, *thread_sp, exited); 724fa03ad2eSChaoren Lin } 725fa03ad2eSChaoren Lin else 726af245d11STodd Fiala { 727b9cc0c75SPavel Labath if (info_err.GetError() == EINVAL) 728af245d11STodd Fiala { 729fa03ad2eSChaoren Lin // This is a group stop reception for this tid. 73039036ac3SPavel Labath // We can reach here if we reinject SIGSTOP, SIGSTP, SIGTTIN or SIGTTOU into the 73139036ac3SPavel Labath // tracee, triggering the group-stop mechanism. Normally receiving these would stop 73239036ac3SPavel Labath // the process, pending a SIGCONT. Simulating this state in a debugger is hard and is 73339036ac3SPavel Labath // generally not needed (one use case is debugging background task being managed by a 73439036ac3SPavel Labath // shell). For general use, it is sufficient to stop the process in a signal-delivery 73539036ac3SPavel Labath // stop which happens before the group stop. This done by MonitorSignal and works 73639036ac3SPavel Labath // correctly for all signals. 737fa03ad2eSChaoren Lin if (log) 73839036ac3SPavel 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); 739b9cc0c75SPavel Labath ResumeThread(*thread_sp, thread_sp->GetState(), LLDB_INVALID_SIGNAL_NUMBER); 740a9882ceeSTodd Fiala } 741a9882ceeSTodd Fiala else 742a9882ceeSTodd Fiala { 743af245d11STodd Fiala // ptrace(GETSIGINFO) failed (but not due to group-stop). 744af245d11STodd Fiala 745af245d11STodd Fiala // A return value of ESRCH means the thread/process is no longer on the system, 746af245d11STodd Fiala // so it was killed somehow outside of our control. Either way, we can't do anything 747af245d11STodd Fiala // with it anymore. 748af245d11STodd Fiala 749af245d11STodd Fiala // Stop tracking the metadata for the thread since it's entirely off the system now. 7501107b5a5SPavel Labath const bool thread_found = StopTrackingThread (pid); 751af245d11STodd Fiala 752af245d11STodd Fiala if (log) 753af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d (%s, %s, %s)", 754b9cc0c75SPavel 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"); 755af245d11STodd Fiala 756af245d11STodd Fiala if (is_main_thread) 757af245d11STodd Fiala { 758af245d11STodd Fiala // Notify the delegate - our process is not available but appears to have been killed outside 759af245d11STodd Fiala // our control. Is eStateExited the right exit state in this case? 7601107b5a5SPavel Labath SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); 7611107b5a5SPavel Labath SetState (StateType::eStateExited, true); 762af245d11STodd Fiala } 763af245d11STodd Fiala else 764af245d11STodd Fiala { 765af245d11STodd Fiala // This thread was pulled out from underneath us. Anything to do here? Do we want to do an all stop? 766af245d11STodd Fiala if (log) 7671107b5a5SPavel 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); 768af245d11STodd Fiala } 769af245d11STodd Fiala } 770af245d11STodd Fiala } 771af245d11STodd Fiala } 772af245d11STodd Fiala 773af245d11STodd Fiala void 774426bdf88SPavel Labath NativeProcessLinux::WaitForNewThread(::pid_t tid) 775426bdf88SPavel Labath { 776426bdf88SPavel Labath Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 777426bdf88SPavel Labath 778f9077782SPavel Labath NativeThreadLinuxSP new_thread_sp = GetThreadByID(tid); 779426bdf88SPavel Labath 780426bdf88SPavel Labath if (new_thread_sp) 781426bdf88SPavel Labath { 782426bdf88SPavel Labath // We are already tracking the thread - we got the event on the new thread (see 783426bdf88SPavel Labath // MonitorSignal) before this one. We are done. 784426bdf88SPavel Labath return; 785426bdf88SPavel Labath } 786426bdf88SPavel Labath 787426bdf88SPavel Labath // The thread is not tracked yet, let's wait for it to appear. 788426bdf88SPavel Labath int status = -1; 789426bdf88SPavel Labath ::pid_t wait_pid; 790426bdf88SPavel Labath do 791426bdf88SPavel Labath { 792426bdf88SPavel Labath if (log) 793426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() received thread creation event for tid %" PRIu32 ". tid not tracked yet, waiting for thread to appear...", __FUNCTION__, tid); 794426bdf88SPavel Labath wait_pid = waitpid(tid, &status, __WALL); 795426bdf88SPavel Labath } 796426bdf88SPavel Labath while (wait_pid == -1 && errno == EINTR); 797426bdf88SPavel Labath // Since we are waiting on a specific tid, this must be the creation event. But let's do 798426bdf88SPavel Labath // some checks just in case. 799426bdf88SPavel Labath if (wait_pid != tid) { 800426bdf88SPavel Labath if (log) 801426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() waiting for tid %" PRIu32 " failed. Assuming the thread has disappeared in the meantime", __FUNCTION__, tid); 802426bdf88SPavel Labath // The only way I know of this could happen is if the whole process was 803426bdf88SPavel Labath // SIGKILLed in the mean time. In any case, we can't do anything about that now. 804426bdf88SPavel Labath return; 805426bdf88SPavel Labath } 806426bdf88SPavel Labath if (WIFEXITED(status)) 807426bdf88SPavel Labath { 808426bdf88SPavel Labath if (log) 809426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() waiting for tid %" PRIu32 " returned an 'exited' event. Not tracking the thread.", __FUNCTION__, tid); 810426bdf88SPavel Labath // Also a very improbable event. 811426bdf88SPavel Labath return; 812426bdf88SPavel Labath } 813426bdf88SPavel Labath 814426bdf88SPavel Labath siginfo_t info; 815426bdf88SPavel Labath Error error = GetSignalInfo(tid, &info); 816426bdf88SPavel Labath if (error.Fail()) 817426bdf88SPavel Labath { 818426bdf88SPavel Labath if (log) 819426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() GetSignalInfo for tid %" PRIu32 " failed. Assuming the thread has disappeared in the meantime.", __FUNCTION__, tid); 820426bdf88SPavel Labath return; 821426bdf88SPavel Labath } 822426bdf88SPavel Labath 823426bdf88SPavel Labath if (((info.si_pid != 0) || (info.si_code != SI_USER)) && log) 824426bdf88SPavel Labath { 825426bdf88SPavel Labath // We should be getting a thread creation signal here, but we received something 826426bdf88SPavel Labath // else. There isn't much we can do about it now, so we will just log that. Since the 827426bdf88SPavel Labath // thread is alive and we are receiving events from it, we shall pretend that it was 828426bdf88SPavel Labath // created properly. 829426bdf88SPavel 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); 830426bdf88SPavel Labath } 831426bdf88SPavel Labath 832426bdf88SPavel Labath if (log) 833426bdf88SPavel Labath log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 ": tracking new thread tid %" PRIu32, 834426bdf88SPavel Labath __FUNCTION__, GetID (), tid); 835426bdf88SPavel Labath 836f9077782SPavel Labath new_thread_sp = AddThread(tid); 837b9cc0c75SPavel Labath ResumeThread(*new_thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); 838f9077782SPavel Labath ThreadWasCreated(*new_thread_sp); 839426bdf88SPavel Labath } 840426bdf88SPavel Labath 841426bdf88SPavel Labath void 842b9cc0c75SPavel Labath NativeProcessLinux::MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread) 843af245d11STodd Fiala { 844af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 845b9cc0c75SPavel Labath const bool is_main_thread = (thread.GetID() == GetID ()); 846af245d11STodd Fiala 847b9cc0c75SPavel Labath assert(info.si_signo == SIGTRAP && "Unexpected child signal!"); 848af245d11STodd Fiala 849b9cc0c75SPavel Labath switch (info.si_code) 850af245d11STodd Fiala { 851af245d11STodd 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. 852af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)): 853af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)): 854af245d11STodd Fiala 855af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)): 856af245d11STodd Fiala { 8575fd24c67SPavel Labath // This is the notification on the parent thread which informs us of new thread 858426bdf88SPavel Labath // creation. 859426bdf88SPavel Labath // We don't want to do anything with the parent thread so we just resume it. In case we 860426bdf88SPavel Labath // want to implement "break on thread creation" functionality, we would need to stop 861426bdf88SPavel Labath // here. 862af245d11STodd Fiala 863af245d11STodd Fiala unsigned long event_message = 0; 864b9cc0c75SPavel Labath if (GetEventMessage(thread.GetID(), &event_message).Fail()) 865fa03ad2eSChaoren Lin { 866426bdf88SPavel Labath if (log) 867b9cc0c75SPavel 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()); 868426bdf88SPavel Labath } else 869426bdf88SPavel Labath WaitForNewThread(event_message); 870af245d11STodd Fiala 871b9cc0c75SPavel Labath ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); 872af245d11STodd Fiala break; 873af245d11STodd Fiala } 874af245d11STodd Fiala 875af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): 876a9882ceeSTodd Fiala { 877f9077782SPavel Labath NativeThreadLinuxSP main_thread_sp; 878af245d11STodd Fiala if (log) 879b9cc0c75SPavel Labath log->Printf ("NativeProcessLinux::%s() received exec event, code = %d", __FUNCTION__, info.si_code ^ SIGTRAP); 880a9882ceeSTodd Fiala 8811dbc6c9cSPavel Labath // Exec clears any pending notifications. 8820e1d729bSPavel Labath m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 883fa03ad2eSChaoren Lin 88457a77118SPavel Labath // Remove all but the main thread here. Linux fork creates a new process which only copies the main thread. 885a9882ceeSTodd Fiala if (log) 886a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s exec received, stop tracking all but main thread", __FUNCTION__); 887a9882ceeSTodd Fiala 888a9882ceeSTodd Fiala for (auto thread_sp : m_threads) 889a9882ceeSTodd Fiala { 890a9882ceeSTodd Fiala const bool is_main_thread = thread_sp && thread_sp->GetID () == GetID (); 891a9882ceeSTodd Fiala if (is_main_thread) 892a9882ceeSTodd Fiala { 893f9077782SPavel Labath main_thread_sp = std::static_pointer_cast<NativeThreadLinux>(thread_sp); 894a9882ceeSTodd Fiala if (log) 895a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s found main thread with tid %" PRIu64 ", keeping", __FUNCTION__, main_thread_sp->GetID ()); 896a9882ceeSTodd Fiala } 897a9882ceeSTodd Fiala else 898a9882ceeSTodd Fiala { 899a9882ceeSTodd Fiala if (log) 900a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s discarding non-main-thread tid %" PRIu64 " due to exec", __FUNCTION__, thread_sp->GetID ()); 901a9882ceeSTodd Fiala } 902a9882ceeSTodd Fiala } 903a9882ceeSTodd Fiala 904a9882ceeSTodd Fiala m_threads.clear (); 905a9882ceeSTodd Fiala 906a9882ceeSTodd Fiala if (main_thread_sp) 907a9882ceeSTodd Fiala { 908a9882ceeSTodd Fiala m_threads.push_back (main_thread_sp); 909a9882ceeSTodd Fiala SetCurrentThreadID (main_thread_sp->GetID ()); 910f9077782SPavel Labath main_thread_sp->SetStoppedByExec(); 911a9882ceeSTodd Fiala } 912a9882ceeSTodd Fiala else 913a9882ceeSTodd Fiala { 914a9882ceeSTodd Fiala SetCurrentThreadID (LLDB_INVALID_THREAD_ID); 915a9882ceeSTodd Fiala if (log) 916a9882ceeSTodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 "no main thread found, discarded all threads, we're in a no-thread state!", __FUNCTION__, GetID ()); 917a9882ceeSTodd Fiala } 918a9882ceeSTodd Fiala 919fa03ad2eSChaoren Lin // Tell coordinator about about the "new" (since exec) stopped main thread. 920f9077782SPavel Labath ThreadWasCreated(*main_thread_sp); 921fa03ad2eSChaoren Lin 922a9882ceeSTodd Fiala // Let our delegate know we have just exec'd. 923a9882ceeSTodd Fiala NotifyDidExec (); 924a9882ceeSTodd Fiala 925a9882ceeSTodd Fiala // If we have a main thread, indicate we are stopped. 926a9882ceeSTodd Fiala assert (main_thread_sp && "exec called during ptraced process but no main thread metadata tracked"); 927fa03ad2eSChaoren Lin 928fa03ad2eSChaoren Lin // Let the process know we're stopped. 929b9cc0c75SPavel Labath StopRunningThreads(main_thread_sp->GetID()); 930a9882ceeSTodd Fiala 931af245d11STodd Fiala break; 932a9882ceeSTodd Fiala } 933af245d11STodd Fiala 934af245d11STodd Fiala case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): 935af245d11STodd Fiala { 936af245d11STodd Fiala // The inferior process or one of its threads is about to exit. 9376e35163cSPavel Labath // We don't want to do anything with the thread so we just resume it. In case we 9386e35163cSPavel Labath // want to implement "break on thread exit" functionality, we would need to stop 9396e35163cSPavel Labath // here. 940fa03ad2eSChaoren Lin 941af245d11STodd Fiala unsigned long data = 0; 942b9cc0c75SPavel Labath if (GetEventMessage(thread.GetID(), &data).Fail()) 943af245d11STodd Fiala data = -1; 944af245d11STodd Fiala 945af245d11STodd Fiala if (log) 946af245d11STodd Fiala { 947af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received PTRACE_EVENT_EXIT, data = %lx (WIFEXITED=%s,WIFSIGNALED=%s), pid = %" PRIu64 " (%s)", 948af245d11STodd Fiala __FUNCTION__, 949af245d11STodd Fiala data, WIFEXITED (data) ? "true" : "false", WIFSIGNALED (data) ? "true" : "false", 950b9cc0c75SPavel Labath thread.GetID(), 951af245d11STodd Fiala is_main_thread ? "is main thread" : "not main thread"); 952af245d11STodd Fiala } 953af245d11STodd Fiala 954af245d11STodd Fiala if (is_main_thread) 955af245d11STodd Fiala { 956af245d11STodd Fiala SetExitStatus (convert_pid_status_to_exit_type (data), convert_pid_status_to_return_code (data), nullptr, true); 95775f47c3aSTodd Fiala } 95875f47c3aSTodd Fiala 95986852d36SPavel Labath StateType state = thread.GetState(); 96086852d36SPavel Labath if (! StateIsRunningState(state)) 96186852d36SPavel Labath { 96286852d36SPavel Labath // Due to a kernel bug, we may sometimes get this stop after the inferior gets a 96386852d36SPavel Labath // SIGKILL. This confuses our state tracking logic in ResumeThread(), since normally, 96486852d36SPavel Labath // we should not be receiving any ptrace events while the inferior is stopped. This 96586852d36SPavel Labath // makes sure that the inferior is resumed and exits normally. 96686852d36SPavel Labath state = eStateRunning; 96786852d36SPavel Labath } 96886852d36SPavel Labath ResumeThread(thread, state, LLDB_INVALID_SIGNAL_NUMBER); 969af245d11STodd Fiala 970af245d11STodd Fiala break; 971af245d11STodd Fiala } 972af245d11STodd Fiala 973af245d11STodd Fiala case 0: 974c16f5dcaSChaoren Lin case TRAP_TRACE: // We receive this on single stepping. 975c16f5dcaSChaoren Lin case TRAP_HWBKPT: // We receive this on watchpoint hit 97686fd8e45SChaoren Lin { 977c16f5dcaSChaoren Lin // If a watchpoint was hit, report it 978c16f5dcaSChaoren Lin uint32_t wp_index; 9791fa5c4b9STamas Berghammer Error error = thread.GetRegisterContext()->GetWatchpointHitIndex(wp_index, (uintptr_t)info.si_addr); 980c16f5dcaSChaoren Lin if (error.Fail() && log) 981c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() " 982c16f5dcaSChaoren Lin "received error while checking for watchpoint hits, " 983c16f5dcaSChaoren Lin "pid = %" PRIu64 " error = %s", 984b9cc0c75SPavel Labath __FUNCTION__, thread.GetID(), error.AsCString()); 985c16f5dcaSChaoren Lin if (wp_index != LLDB_INVALID_INDEX32) 9865830aa75STamas Berghammer { 987b9cc0c75SPavel Labath MonitorWatchpoint(thread, wp_index); 988c16f5dcaSChaoren Lin break; 989c16f5dcaSChaoren Lin } 990b9cc0c75SPavel Labath 991be379e15STamas Berghammer // Otherwise, report step over 992be379e15STamas Berghammer MonitorTrace(thread); 993af245d11STodd Fiala break; 994b9cc0c75SPavel Labath } 995af245d11STodd Fiala 996af245d11STodd Fiala case SI_KERNEL: 99735799963SMohit K. Bhakkad #if defined __mips__ 99835799963SMohit K. Bhakkad // For mips there is no special signal for watchpoint 99935799963SMohit K. Bhakkad // So we check for watchpoint in kernel trap 100035799963SMohit K. Bhakkad { 100135799963SMohit K. Bhakkad // If a watchpoint was hit, report it 100235799963SMohit K. Bhakkad uint32_t wp_index; 1003b9cc0c75SPavel Labath Error error = thread.GetRegisterContext()->GetWatchpointHitIndex(wp_index, LLDB_INVALID_ADDRESS); 100435799963SMohit K. Bhakkad if (error.Fail() && log) 100535799963SMohit K. Bhakkad log->Printf("NativeProcessLinux::%s() " 100635799963SMohit K. Bhakkad "received error while checking for watchpoint hits, " 100735799963SMohit K. Bhakkad "pid = %" PRIu64 " error = %s", 100816ad0321SMohit K. Bhakkad __FUNCTION__, thread.GetID(), error.AsCString()); 100935799963SMohit K. Bhakkad if (wp_index != LLDB_INVALID_INDEX32) 101035799963SMohit K. Bhakkad { 1011b9cc0c75SPavel Labath MonitorWatchpoint(thread, wp_index); 101235799963SMohit K. Bhakkad break; 101335799963SMohit K. Bhakkad } 101435799963SMohit K. Bhakkad } 101535799963SMohit K. Bhakkad // NO BREAK 101635799963SMohit K. Bhakkad #endif 1017af245d11STodd Fiala case TRAP_BRKPT: 1018b9cc0c75SPavel Labath MonitorBreakpoint(thread); 1019af245d11STodd Fiala break; 1020af245d11STodd Fiala 1021af245d11STodd Fiala case SIGTRAP: 1022af245d11STodd Fiala case (SIGTRAP | 0x80): 1023af245d11STodd Fiala if (log) 1024b9cc0c75SPavel Labath log->Printf ("NativeProcessLinux::%s() received unknown SIGTRAP system call stop event, pid %" PRIu64 "tid %" PRIu64 ", resuming", __FUNCTION__, GetID (), thread.GetID()); 1025fa03ad2eSChaoren Lin 1026af245d11STodd Fiala // Ignore these signals until we know more about them. 1027b9cc0c75SPavel Labath ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); 1028af245d11STodd Fiala break; 1029af245d11STodd Fiala 1030af245d11STodd Fiala default: 1031af245d11STodd Fiala assert(false && "Unexpected SIGTRAP code!"); 1032af245d11STodd Fiala if (log) 10336e35163cSPavel Labath log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 "tid %" PRIu64 " received unhandled SIGTRAP code: 0x%d", 1034b9cc0c75SPavel Labath __FUNCTION__, GetID(), thread.GetID(), info.si_code); 1035af245d11STodd Fiala break; 1036af245d11STodd Fiala 1037af245d11STodd Fiala } 1038af245d11STodd Fiala } 1039af245d11STodd Fiala 1040af245d11STodd Fiala void 1041b9cc0c75SPavel Labath NativeProcessLinux::MonitorTrace(NativeThreadLinux &thread) 1042c16f5dcaSChaoren Lin { 1043c16f5dcaSChaoren Lin Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 1044c16f5dcaSChaoren Lin if (log) 1045c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() received trace event, pid = %" PRIu64 " (single stepping)", 1046b9cc0c75SPavel Labath __FUNCTION__, thread.GetID()); 1047c16f5dcaSChaoren Lin 10480e1d729bSPavel Labath // This thread is currently stopped. 1049b9cc0c75SPavel Labath thread.SetStoppedByTrace(); 1050c16f5dcaSChaoren Lin 1051b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 1052c16f5dcaSChaoren Lin } 1053c16f5dcaSChaoren Lin 1054c16f5dcaSChaoren Lin void 1055b9cc0c75SPavel Labath NativeProcessLinux::MonitorBreakpoint(NativeThreadLinux &thread) 1056c16f5dcaSChaoren Lin { 1057c16f5dcaSChaoren Lin Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 1058c16f5dcaSChaoren Lin if (log) 1059c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() received breakpoint event, pid = %" PRIu64, 1060b9cc0c75SPavel Labath __FUNCTION__, thread.GetID()); 1061c16f5dcaSChaoren Lin 1062c16f5dcaSChaoren Lin // Mark the thread as stopped at breakpoint. 1063b9cc0c75SPavel Labath thread.SetStoppedByBreakpoint(); 1064b9cc0c75SPavel Labath Error error = FixupBreakpointPCAsNeeded(thread); 1065c16f5dcaSChaoren Lin if (error.Fail()) 1066c16f5dcaSChaoren Lin if (log) 1067c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " fixup: %s", 1068b9cc0c75SPavel Labath __FUNCTION__, thread.GetID(), error.AsCString()); 1069d8c338d4STamas Berghammer 1070b9cc0c75SPavel Labath if (m_threads_stepping_with_breakpoint.find(thread.GetID()) != m_threads_stepping_with_breakpoint.end()) 1071b9cc0c75SPavel Labath thread.SetStoppedByTrace(); 1072c16f5dcaSChaoren Lin 1073b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 1074c16f5dcaSChaoren Lin } 1075c16f5dcaSChaoren Lin 1076c16f5dcaSChaoren Lin void 1077f9077782SPavel Labath NativeProcessLinux::MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index) 1078c16f5dcaSChaoren Lin { 1079c16f5dcaSChaoren Lin Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_WATCHPOINTS)); 1080c16f5dcaSChaoren Lin if (log) 1081c16f5dcaSChaoren Lin log->Printf("NativeProcessLinux::%s() received watchpoint event, " 1082c16f5dcaSChaoren Lin "pid = %" PRIu64 ", wp_index = %" PRIu32, 1083f9077782SPavel Labath __FUNCTION__, thread.GetID(), wp_index); 1084c16f5dcaSChaoren Lin 1085c16f5dcaSChaoren Lin // Mark the thread as stopped at watchpoint. 1086c16f5dcaSChaoren Lin // The address is at (lldb::addr_t)info->si_addr if we need it. 1087f9077782SPavel Labath thread.SetStoppedByWatchpoint(wp_index); 1088c16f5dcaSChaoren Lin 1089c16f5dcaSChaoren Lin // We need to tell all other running threads before we notify the delegate about this stop. 1090f9077782SPavel Labath StopRunningThreads(thread.GetID()); 1091c16f5dcaSChaoren Lin } 1092c16f5dcaSChaoren Lin 1093c16f5dcaSChaoren Lin void 1094b9cc0c75SPavel Labath NativeProcessLinux::MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread, bool exited) 1095af245d11STodd Fiala { 1096b9cc0c75SPavel Labath const int signo = info.si_signo; 1097b9cc0c75SPavel Labath const bool is_from_llgs = info.si_pid == getpid (); 1098af245d11STodd Fiala 1099af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1100af245d11STodd Fiala 1101af245d11STodd Fiala // POSIX says that process behaviour is undefined after it ignores a SIGFPE, 1102af245d11STodd Fiala // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a 1103af245d11STodd Fiala // kill(2) or raise(3). Similarly for tgkill(2) on Linux. 1104af245d11STodd Fiala // 1105af245d11STodd Fiala // IOW, user generated signals never generate what we consider to be a 1106af245d11STodd Fiala // "crash". 1107af245d11STodd Fiala // 1108af245d11STodd Fiala // Similarly, ACK signals generated by this monitor. 1109af245d11STodd Fiala 1110af245d11STodd Fiala // Handle the signal. 1111b9cc0c75SPavel Labath if (info.si_code == SI_TKILL || info.si_code == SI_USER) 1112af245d11STodd Fiala { 1113af245d11STodd Fiala if (log) 1114af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s() received signal %s (%d) with code %s, (siginfo pid = %d (%s), waitpid pid = %" PRIu64 ")", 1115af245d11STodd Fiala __FUNCTION__, 111698d0a4b3SChaoren Lin Host::GetSignalAsCString(signo), 1117af245d11STodd Fiala signo, 1118b9cc0c75SPavel Labath (info.si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"), 1119b9cc0c75SPavel Labath info.si_pid, 1120511e5cdcSTodd Fiala is_from_llgs ? "from llgs" : "not from llgs", 1121b9cc0c75SPavel Labath thread.GetID()); 1122af245d11STodd Fiala } 112358a2f669STodd Fiala 112458a2f669STodd Fiala // Check for thread stop notification. 1125b9cc0c75SPavel Labath if (is_from_llgs && (info.si_code == SI_TKILL) && (signo == SIGSTOP)) 1126af245d11STodd Fiala { 1127af245d11STodd Fiala // This is a tgkill()-based stop. 1128fa03ad2eSChaoren Lin if (log) 1129fa03ad2eSChaoren Lin log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 ", thread stopped", 1130fa03ad2eSChaoren Lin __FUNCTION__, 1131fa03ad2eSChaoren Lin GetID (), 1132b9cc0c75SPavel Labath thread.GetID()); 1133fa03ad2eSChaoren Lin 1134aab58633SChaoren Lin // Check that we're not already marked with a stop reason. 1135aab58633SChaoren Lin // Note this thread really shouldn't already be marked as stopped - if we were, that would imply that 1136aab58633SChaoren Lin // the kernel signaled us with the thread stopping which we handled and marked as stopped, 1137aab58633SChaoren Lin // and that, without an intervening resume, we received another stop. It is more likely 1138aab58633SChaoren Lin // that we are missing the marking of a run state somewhere if we find that the thread was 1139aab58633SChaoren Lin // marked as stopped. 1140b9cc0c75SPavel Labath const StateType thread_state = thread.GetState(); 1141aab58633SChaoren Lin if (!StateIsStoppedState (thread_state, false)) 1142aab58633SChaoren Lin { 1143ed89c7feSPavel Labath // An inferior thread has stopped because of a SIGSTOP we have sent it. 1144ed89c7feSPavel Labath // Generally, these are not important stops and we don't want to report them as 1145ed89c7feSPavel Labath // they are just used to stop other threads when one thread (the one with the 1146ed89c7feSPavel Labath // *real* stop reason) hits a breakpoint (watchpoint, etc...). However, in the 1147ed89c7feSPavel Labath // case of an asynchronous Interrupt(), this *is* the real stop reason, so we 1148ed89c7feSPavel Labath // leave the signal intact if this is the thread that was chosen as the 1149ed89c7feSPavel Labath // triggering thread. 11500e1d729bSPavel Labath if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) 11510e1d729bSPavel Labath { 1152b9cc0c75SPavel Labath if (m_pending_notification_tid == thread.GetID()) 1153b9cc0c75SPavel Labath thread.SetStoppedBySignal(SIGSTOP, &info); 1154ed89c7feSPavel Labath else 1155b9cc0c75SPavel Labath thread.SetStoppedWithNoReason(); 1156ed89c7feSPavel Labath 1157b9cc0c75SPavel Labath SetCurrentThreadID (thread.GetID ()); 11580e1d729bSPavel Labath SignalIfAllThreadsStopped(); 11590e1d729bSPavel Labath } 11600e1d729bSPavel Labath else 11610e1d729bSPavel Labath { 11620e1d729bSPavel Labath // We can end up here if stop was initiated by LLGS but by this time a 11630e1d729bSPavel Labath // thread stop has occurred - maybe initiated by another event. 1164b9cc0c75SPavel Labath Error error = ResumeThread(thread, thread.GetState(), 0); 11650e1d729bSPavel Labath if (error.Fail() && log) 11660e1d729bSPavel Labath { 11670e1d729bSPavel Labath log->Printf("NativeProcessLinux::%s failed to resume thread tid %" PRIu64 ": %s", 1168b9cc0c75SPavel Labath __FUNCTION__, thread.GetID(), error.AsCString()); 11690e1d729bSPavel Labath } 11700e1d729bSPavel Labath } 1171aab58633SChaoren Lin } 1172aab58633SChaoren Lin else 1173aab58633SChaoren Lin { 1174aab58633SChaoren Lin if (log) 1175aab58633SChaoren Lin { 1176aab58633SChaoren Lin // Retrieve the signal name if the thread was stopped by a signal. 1177aab58633SChaoren Lin int stop_signo = 0; 1178b9cc0c75SPavel Labath const bool stopped_by_signal = thread.IsStopped(&stop_signo); 117998d0a4b3SChaoren Lin const char *signal_name = stopped_by_signal ? Host::GetSignalAsCString(stop_signo) : "<not stopped by signal>"; 1180aab58633SChaoren Lin if (!signal_name) 1181aab58633SChaoren Lin signal_name = "<no-signal-name>"; 1182aab58633SChaoren Lin 1183aab58633SChaoren 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", 1184aab58633SChaoren Lin __FUNCTION__, 1185aab58633SChaoren Lin GetID (), 1186b9cc0c75SPavel Labath thread.GetID(), 1187aab58633SChaoren Lin StateAsCString (thread_state), 1188aab58633SChaoren Lin stop_signo, 1189aab58633SChaoren Lin signal_name); 1190aab58633SChaoren Lin } 11910e1d729bSPavel Labath SignalIfAllThreadsStopped(); 1192af245d11STodd Fiala } 1193af245d11STodd Fiala 119458a2f669STodd Fiala // Done handling. 1195af245d11STodd Fiala return; 1196af245d11STodd Fiala } 1197af245d11STodd Fiala 1198af245d11STodd Fiala if (log) 119998d0a4b3SChaoren Lin log->Printf ("NativeProcessLinux::%s() received signal %s", __FUNCTION__, Host::GetSignalAsCString(signo)); 1200af245d11STodd Fiala 120186fd8e45SChaoren Lin // This thread is stopped. 1202b9cc0c75SPavel Labath thread.SetStoppedBySignal(signo, &info); 120386fd8e45SChaoren Lin 120486fd8e45SChaoren Lin // Send a stop to the debugger after we get all other threads to stop. 1205b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 1206511e5cdcSTodd Fiala } 1207af245d11STodd Fiala 1208e7708688STamas Berghammer namespace { 1209e7708688STamas Berghammer 1210e7708688STamas Berghammer struct EmulatorBaton 1211e7708688STamas Berghammer { 1212e7708688STamas Berghammer NativeProcessLinux* m_process; 1213e7708688STamas Berghammer NativeRegisterContext* m_reg_context; 12146648fcc3SPavel Labath 12156648fcc3SPavel Labath // eRegisterKindDWARF -> RegsiterValue 12166648fcc3SPavel Labath std::unordered_map<uint32_t, RegisterValue> m_register_values; 1217e7708688STamas Berghammer 1218e7708688STamas Berghammer EmulatorBaton(NativeProcessLinux* process, NativeRegisterContext* reg_context) : 1219e7708688STamas Berghammer m_process(process), m_reg_context(reg_context) {} 1220e7708688STamas Berghammer }; 1221e7708688STamas Berghammer 1222e7708688STamas Berghammer } // anonymous namespace 1223e7708688STamas Berghammer 1224e7708688STamas Berghammer static size_t 1225e7708688STamas Berghammer ReadMemoryCallback (EmulateInstruction *instruction, 1226e7708688STamas Berghammer void *baton, 1227e7708688STamas Berghammer const EmulateInstruction::Context &context, 1228e7708688STamas Berghammer lldb::addr_t addr, 1229e7708688STamas Berghammer void *dst, 1230e7708688STamas Berghammer size_t length) 1231e7708688STamas Berghammer { 1232e7708688STamas Berghammer EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); 1233e7708688STamas Berghammer 12343eb4b458SChaoren Lin size_t bytes_read; 1235e7708688STamas Berghammer emulator_baton->m_process->ReadMemory(addr, dst, length, bytes_read); 1236e7708688STamas Berghammer return bytes_read; 1237e7708688STamas Berghammer } 1238e7708688STamas Berghammer 1239e7708688STamas Berghammer static bool 1240e7708688STamas Berghammer ReadRegisterCallback (EmulateInstruction *instruction, 1241e7708688STamas Berghammer void *baton, 1242e7708688STamas Berghammer const RegisterInfo *reg_info, 1243e7708688STamas Berghammer RegisterValue ®_value) 1244e7708688STamas Berghammer { 1245e7708688STamas Berghammer EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); 1246e7708688STamas Berghammer 12476648fcc3SPavel Labath auto it = emulator_baton->m_register_values.find(reg_info->kinds[eRegisterKindDWARF]); 12486648fcc3SPavel Labath if (it != emulator_baton->m_register_values.end()) 12496648fcc3SPavel Labath { 12506648fcc3SPavel Labath reg_value = it->second; 12516648fcc3SPavel Labath return true; 12526648fcc3SPavel Labath } 12536648fcc3SPavel Labath 1254e7708688STamas Berghammer // The emulator only fill in the dwarf regsiter numbers (and in some case 1255e7708688STamas Berghammer // the generic register numbers). Get the full register info from the 1256e7708688STamas Berghammer // register context based on the dwarf register numbers. 1257e7708688STamas Berghammer const RegisterInfo* full_reg_info = emulator_baton->m_reg_context->GetRegisterInfo( 1258e7708688STamas Berghammer eRegisterKindDWARF, reg_info->kinds[eRegisterKindDWARF]); 1259e7708688STamas Berghammer 1260e7708688STamas Berghammer Error error = emulator_baton->m_reg_context->ReadRegister(full_reg_info, reg_value); 12616648fcc3SPavel Labath if (error.Success()) 12626648fcc3SPavel Labath return true; 1263cdc22a88SMohit K. Bhakkad 12646648fcc3SPavel Labath return false; 1265e7708688STamas Berghammer } 1266e7708688STamas Berghammer 1267e7708688STamas Berghammer static bool 1268e7708688STamas Berghammer WriteRegisterCallback (EmulateInstruction *instruction, 1269e7708688STamas Berghammer void *baton, 1270e7708688STamas Berghammer const EmulateInstruction::Context &context, 1271e7708688STamas Berghammer const RegisterInfo *reg_info, 1272e7708688STamas Berghammer const RegisterValue ®_value) 1273e7708688STamas Berghammer { 1274e7708688STamas Berghammer EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); 12756648fcc3SPavel Labath emulator_baton->m_register_values[reg_info->kinds[eRegisterKindDWARF]] = reg_value; 1276e7708688STamas Berghammer return true; 1277e7708688STamas Berghammer } 1278e7708688STamas Berghammer 1279e7708688STamas Berghammer static size_t 1280e7708688STamas Berghammer WriteMemoryCallback (EmulateInstruction *instruction, 1281e7708688STamas Berghammer void *baton, 1282e7708688STamas Berghammer const EmulateInstruction::Context &context, 1283e7708688STamas Berghammer lldb::addr_t addr, 1284e7708688STamas Berghammer const void *dst, 1285e7708688STamas Berghammer size_t length) 1286e7708688STamas Berghammer { 1287e7708688STamas Berghammer return length; 1288e7708688STamas Berghammer } 1289e7708688STamas Berghammer 1290e7708688STamas Berghammer static lldb::addr_t 1291e7708688STamas Berghammer ReadFlags (NativeRegisterContext* regsiter_context) 1292e7708688STamas Berghammer { 1293e7708688STamas Berghammer const RegisterInfo* flags_info = regsiter_context->GetRegisterInfo( 1294e7708688STamas Berghammer eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 1295e7708688STamas Berghammer return regsiter_context->ReadRegisterAsUnsigned(flags_info, LLDB_INVALID_ADDRESS); 1296e7708688STamas Berghammer } 1297e7708688STamas Berghammer 1298e7708688STamas Berghammer Error 1299b9cc0c75SPavel Labath NativeProcessLinux::SetupSoftwareSingleStepping(NativeThreadLinux &thread) 1300e7708688STamas Berghammer { 1301e7708688STamas Berghammer Error error; 1302b9cc0c75SPavel Labath NativeRegisterContextSP register_context_sp = thread.GetRegisterContext(); 1303e7708688STamas Berghammer 1304e7708688STamas Berghammer std::unique_ptr<EmulateInstruction> emulator_ap( 1305e7708688STamas Berghammer EmulateInstruction::FindPlugin(m_arch, eInstructionTypePCModifying, nullptr)); 1306e7708688STamas Berghammer 1307e7708688STamas Berghammer if (emulator_ap == nullptr) 1308e7708688STamas Berghammer return Error("Instruction emulator not found!"); 1309e7708688STamas Berghammer 1310e7708688STamas Berghammer EmulatorBaton baton(this, register_context_sp.get()); 1311e7708688STamas Berghammer emulator_ap->SetBaton(&baton); 1312e7708688STamas Berghammer emulator_ap->SetReadMemCallback(&ReadMemoryCallback); 1313e7708688STamas Berghammer emulator_ap->SetReadRegCallback(&ReadRegisterCallback); 1314e7708688STamas Berghammer emulator_ap->SetWriteMemCallback(&WriteMemoryCallback); 1315e7708688STamas Berghammer emulator_ap->SetWriteRegCallback(&WriteRegisterCallback); 1316e7708688STamas Berghammer 1317e7708688STamas Berghammer if (!emulator_ap->ReadInstruction()) 1318e7708688STamas Berghammer return Error("Read instruction failed!"); 1319e7708688STamas Berghammer 13206648fcc3SPavel Labath bool emulation_result = emulator_ap->EvaluateInstruction(eEmulateInstructionOptionAutoAdvancePC); 13216648fcc3SPavel Labath 13226648fcc3SPavel Labath const RegisterInfo* reg_info_pc = register_context_sp->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); 13236648fcc3SPavel Labath const RegisterInfo* reg_info_flags = register_context_sp->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 13246648fcc3SPavel Labath 13256648fcc3SPavel Labath auto pc_it = baton.m_register_values.find(reg_info_pc->kinds[eRegisterKindDWARF]); 13266648fcc3SPavel Labath auto flags_it = baton.m_register_values.find(reg_info_flags->kinds[eRegisterKindDWARF]); 13276648fcc3SPavel Labath 1328e7708688STamas Berghammer lldb::addr_t next_pc; 1329e7708688STamas Berghammer lldb::addr_t next_flags; 13306648fcc3SPavel Labath if (emulation_result) 1331e7708688STamas Berghammer { 13326648fcc3SPavel Labath assert(pc_it != baton.m_register_values.end() && "Emulation was successfull but PC wasn't updated"); 13336648fcc3SPavel Labath next_pc = pc_it->second.GetAsUInt64(); 13346648fcc3SPavel Labath 13356648fcc3SPavel Labath if (flags_it != baton.m_register_values.end()) 13366648fcc3SPavel Labath next_flags = flags_it->second.GetAsUInt64(); 1337e7708688STamas Berghammer else 1338e7708688STamas Berghammer next_flags = ReadFlags (register_context_sp.get()); 1339e7708688STamas Berghammer } 13406648fcc3SPavel Labath else if (pc_it == baton.m_register_values.end()) 1341e7708688STamas Berghammer { 1342e7708688STamas Berghammer // Emulate instruction failed and it haven't changed PC. Advance PC 1343e7708688STamas Berghammer // with the size of the current opcode because the emulation of all 1344e7708688STamas Berghammer // PC modifying instruction should be successful. The failure most 1345e7708688STamas Berghammer // likely caused by a not supported instruction which don't modify PC. 1346e7708688STamas Berghammer next_pc = register_context_sp->GetPC() + emulator_ap->GetOpcode().GetByteSize(); 1347e7708688STamas Berghammer next_flags = ReadFlags (register_context_sp.get()); 1348e7708688STamas Berghammer } 1349e7708688STamas Berghammer else 1350e7708688STamas Berghammer { 1351e7708688STamas Berghammer // The instruction emulation failed after it modified the PC. It is an 1352e7708688STamas Berghammer // unknown error where we can't continue because the next instruction is 1353e7708688STamas Berghammer // modifying the PC but we don't know how. 1354e7708688STamas Berghammer return Error ("Instruction emulation failed unexpectedly."); 1355e7708688STamas Berghammer } 1356e7708688STamas Berghammer 1357e7708688STamas Berghammer if (m_arch.GetMachine() == llvm::Triple::arm) 1358e7708688STamas Berghammer { 1359e7708688STamas Berghammer if (next_flags & 0x20) 1360e7708688STamas Berghammer { 1361e7708688STamas Berghammer // Thumb mode 1362e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 2); 1363e7708688STamas Berghammer } 1364e7708688STamas Berghammer else 1365e7708688STamas Berghammer { 1366e7708688STamas Berghammer // Arm mode 1367e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 4); 1368e7708688STamas Berghammer } 1369e7708688STamas Berghammer } 1370cdc22a88SMohit K. Bhakkad else if (m_arch.GetMachine() == llvm::Triple::mips64 1371c60c9452SJaydeep Patil || m_arch.GetMachine() == llvm::Triple::mips64el 1372c60c9452SJaydeep Patil || m_arch.GetMachine() == llvm::Triple::mips 1373c60c9452SJaydeep Patil || m_arch.GetMachine() == llvm::Triple::mipsel) 1374cdc22a88SMohit K. Bhakkad error = SetSoftwareBreakpoint(next_pc, 4); 1375e7708688STamas Berghammer else 1376e7708688STamas Berghammer { 1377e7708688STamas Berghammer // No size hint is given for the next breakpoint 1378e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 0); 1379e7708688STamas Berghammer } 1380e7708688STamas Berghammer 1381e7708688STamas Berghammer if (error.Fail()) 1382e7708688STamas Berghammer return error; 1383e7708688STamas Berghammer 1384b9cc0c75SPavel Labath m_threads_stepping_with_breakpoint.insert({thread.GetID(), next_pc}); 1385e7708688STamas Berghammer 1386e7708688STamas Berghammer return Error(); 1387e7708688STamas Berghammer } 1388e7708688STamas Berghammer 1389e7708688STamas Berghammer bool 1390e7708688STamas Berghammer NativeProcessLinux::SupportHardwareSingleStepping() const 1391e7708688STamas Berghammer { 1392cdc22a88SMohit K. Bhakkad if (m_arch.GetMachine() == llvm::Triple::arm 1393c60c9452SJaydeep Patil || m_arch.GetMachine() == llvm::Triple::mips64 || m_arch.GetMachine() == llvm::Triple::mips64el 1394c60c9452SJaydeep Patil || m_arch.GetMachine() == llvm::Triple::mips || m_arch.GetMachine() == llvm::Triple::mipsel) 1395cdc22a88SMohit K. Bhakkad return false; 1396cdc22a88SMohit K. Bhakkad return true; 1397e7708688STamas Berghammer } 1398e7708688STamas Berghammer 1399af245d11STodd Fiala Error 1400af245d11STodd Fiala NativeProcessLinux::Resume (const ResumeActionList &resume_actions) 1401af245d11STodd Fiala { 1402af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); 1403af245d11STodd Fiala if (log) 1404af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s called: pid %" PRIu64, __FUNCTION__, GetID ()); 1405af245d11STodd Fiala 1406e7708688STamas Berghammer bool software_single_step = !SupportHardwareSingleStepping(); 1407af245d11STodd Fiala 1408e7708688STamas Berghammer if (software_single_step) 1409e7708688STamas Berghammer { 1410e7708688STamas Berghammer for (auto thread_sp : m_threads) 1411e7708688STamas Berghammer { 1412e7708688STamas Berghammer assert (thread_sp && "thread list should not contain NULL threads"); 1413e7708688STamas Berghammer 1414e7708688STamas Berghammer const ResumeAction *const action = resume_actions.GetActionForThread (thread_sp->GetID (), true); 1415e7708688STamas Berghammer if (action == nullptr) 1416e7708688STamas Berghammer continue; 1417e7708688STamas Berghammer 1418e7708688STamas Berghammer if (action->state == eStateStepping) 1419e7708688STamas Berghammer { 1420b9cc0c75SPavel Labath Error error = SetupSoftwareSingleStepping(static_cast<NativeThreadLinux &>(*thread_sp)); 1421e7708688STamas Berghammer if (error.Fail()) 1422e7708688STamas Berghammer return error; 1423e7708688STamas Berghammer } 1424e7708688STamas Berghammer } 1425e7708688STamas Berghammer } 1426e7708688STamas Berghammer 1427af245d11STodd Fiala for (auto thread_sp : m_threads) 1428af245d11STodd Fiala { 1429af245d11STodd Fiala assert (thread_sp && "thread list should not contain NULL threads"); 1430af245d11STodd Fiala 1431af245d11STodd Fiala const ResumeAction *const action = resume_actions.GetActionForThread (thread_sp->GetID (), true); 14326a196ce6SChaoren Lin 14336a196ce6SChaoren Lin if (action == nullptr) 14346a196ce6SChaoren Lin { 14356a196ce6SChaoren Lin if (log) 14366a196ce6SChaoren Lin log->Printf ("NativeProcessLinux::%s no action specified for pid %" PRIu64 " tid %" PRIu64, 14376a196ce6SChaoren Lin __FUNCTION__, GetID (), thread_sp->GetID ()); 14386a196ce6SChaoren Lin continue; 14396a196ce6SChaoren Lin } 1440af245d11STodd Fiala 1441af245d11STodd Fiala if (log) 1442af245d11STodd Fiala { 1443af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s processing resume action state %s for pid %" PRIu64 " tid %" PRIu64, 1444af245d11STodd Fiala __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); 1445af245d11STodd Fiala } 1446af245d11STodd Fiala 1447af245d11STodd Fiala switch (action->state) 1448af245d11STodd Fiala { 1449af245d11STodd Fiala case eStateRunning: 14500e1d729bSPavel Labath case eStateStepping: 1451fa03ad2eSChaoren Lin { 1452af245d11STodd Fiala // Run the thread, possibly feeding it the signal. 1453fa03ad2eSChaoren Lin const int signo = action->signal; 1454b9cc0c75SPavel Labath ResumeThread(static_cast<NativeThreadLinux &>(*thread_sp), action->state, signo); 1455af245d11STodd Fiala break; 1456ae29d395SChaoren Lin } 1457af245d11STodd Fiala 1458af245d11STodd Fiala case eStateSuspended: 1459af245d11STodd Fiala case eStateStopped: 1460108c325dSPavel Labath lldbassert(0 && "Unexpected state"); 1461af245d11STodd Fiala 1462af245d11STodd Fiala default: 1463af245d11STodd Fiala return Error ("NativeProcessLinux::%s (): unexpected state %s specified for pid %" PRIu64 ", tid %" PRIu64, 1464af245d11STodd Fiala __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); 1465af245d11STodd Fiala } 1466af245d11STodd Fiala } 1467af245d11STodd Fiala 14685830aa75STamas Berghammer return Error(); 1469af245d11STodd Fiala } 1470af245d11STodd Fiala 1471af245d11STodd Fiala Error 1472af245d11STodd Fiala NativeProcessLinux::Halt () 1473af245d11STodd Fiala { 1474af245d11STodd Fiala Error error; 1475af245d11STodd Fiala 1476af245d11STodd Fiala if (kill (GetID (), SIGSTOP) != 0) 1477af245d11STodd Fiala error.SetErrorToErrno (); 1478af245d11STodd Fiala 1479af245d11STodd Fiala return error; 1480af245d11STodd Fiala } 1481af245d11STodd Fiala 1482af245d11STodd Fiala Error 1483af245d11STodd Fiala NativeProcessLinux::Detach () 1484af245d11STodd Fiala { 1485af245d11STodd Fiala Error error; 1486af245d11STodd Fiala 1487af245d11STodd Fiala // Stop monitoring the inferior. 148819cbe96aSPavel Labath m_sigchld_handle.reset(); 1489af245d11STodd Fiala 14907a9495bcSPavel Labath // Tell ptrace to detach from the process. 14917a9495bcSPavel Labath if (GetID () == LLDB_INVALID_PROCESS_ID) 14927a9495bcSPavel Labath return error; 14937a9495bcSPavel Labath 14947a9495bcSPavel Labath for (auto thread_sp : m_threads) 14957a9495bcSPavel Labath { 14967a9495bcSPavel Labath Error e = Detach(thread_sp->GetID()); 14977a9495bcSPavel Labath if (e.Fail()) 14987a9495bcSPavel Labath error = e; // Save the error, but still attempt to detach from other threads. 14997a9495bcSPavel Labath } 15007a9495bcSPavel Labath 1501af245d11STodd Fiala return error; 1502af245d11STodd Fiala } 1503af245d11STodd Fiala 1504af245d11STodd Fiala Error 1505af245d11STodd Fiala NativeProcessLinux::Signal (int signo) 1506af245d11STodd Fiala { 1507af245d11STodd Fiala Error error; 1508af245d11STodd Fiala 1509af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1510af245d11STodd Fiala if (log) 1511af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s: sending signal %d (%s) to pid %" PRIu64, 151298d0a4b3SChaoren Lin __FUNCTION__, signo, Host::GetSignalAsCString(signo), GetID()); 1513af245d11STodd Fiala 1514af245d11STodd Fiala if (kill(GetID(), signo)) 1515af245d11STodd Fiala error.SetErrorToErrno(); 1516af245d11STodd Fiala 1517af245d11STodd Fiala return error; 1518af245d11STodd Fiala } 1519af245d11STodd Fiala 1520af245d11STodd Fiala Error 1521e9547b80SChaoren Lin NativeProcessLinux::Interrupt () 1522e9547b80SChaoren Lin { 1523e9547b80SChaoren Lin // Pick a running thread (or if none, a not-dead stopped thread) as 1524e9547b80SChaoren Lin // the chosen thread that will be the stop-reason thread. 1525e9547b80SChaoren Lin Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1526e9547b80SChaoren Lin 1527e9547b80SChaoren Lin NativeThreadProtocolSP running_thread_sp; 1528e9547b80SChaoren Lin NativeThreadProtocolSP stopped_thread_sp; 1529e9547b80SChaoren Lin 1530e9547b80SChaoren Lin if (log) 1531e9547b80SChaoren Lin log->Printf ("NativeProcessLinux::%s selecting running thread for interrupt target", __FUNCTION__); 1532e9547b80SChaoren Lin 1533e9547b80SChaoren Lin for (auto thread_sp : m_threads) 1534e9547b80SChaoren Lin { 1535e9547b80SChaoren Lin // The thread shouldn't be null but lets just cover that here. 1536e9547b80SChaoren Lin if (!thread_sp) 1537e9547b80SChaoren Lin continue; 1538e9547b80SChaoren Lin 1539e9547b80SChaoren Lin // If we have a running or stepping thread, we'll call that the 1540e9547b80SChaoren Lin // target of the interrupt. 1541e9547b80SChaoren Lin const auto thread_state = thread_sp->GetState (); 1542e9547b80SChaoren Lin if (thread_state == eStateRunning || 1543e9547b80SChaoren Lin thread_state == eStateStepping) 1544e9547b80SChaoren Lin { 1545e9547b80SChaoren Lin running_thread_sp = thread_sp; 1546e9547b80SChaoren Lin break; 1547e9547b80SChaoren Lin } 1548e9547b80SChaoren Lin else if (!stopped_thread_sp && StateIsStoppedState (thread_state, true)) 1549e9547b80SChaoren Lin { 1550e9547b80SChaoren Lin // Remember the first non-dead stopped thread. We'll use that as a backup if there are no running threads. 1551e9547b80SChaoren Lin stopped_thread_sp = thread_sp; 1552e9547b80SChaoren Lin } 1553e9547b80SChaoren Lin } 1554e9547b80SChaoren Lin 1555e9547b80SChaoren Lin if (!running_thread_sp && !stopped_thread_sp) 1556e9547b80SChaoren Lin { 15575830aa75STamas Berghammer Error error("found no running/stepping or live stopped threads as target for interrupt"); 1558e9547b80SChaoren Lin if (log) 1559e9547b80SChaoren Lin log->Printf ("NativeProcessLinux::%s skipping due to error: %s", __FUNCTION__, error.AsCString ()); 15605830aa75STamas Berghammer 1561e9547b80SChaoren Lin return error; 1562e9547b80SChaoren Lin } 1563e9547b80SChaoren Lin 1564e9547b80SChaoren Lin NativeThreadProtocolSP deferred_signal_thread_sp = running_thread_sp ? running_thread_sp : stopped_thread_sp; 1565e9547b80SChaoren Lin 1566e9547b80SChaoren Lin if (log) 1567e9547b80SChaoren Lin log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " %s tid %" PRIu64 " chosen for interrupt target", 1568e9547b80SChaoren Lin __FUNCTION__, 1569e9547b80SChaoren Lin GetID (), 1570e9547b80SChaoren Lin running_thread_sp ? "running" : "stopped", 1571e9547b80SChaoren Lin deferred_signal_thread_sp->GetID ()); 1572e9547b80SChaoren Lin 1573ed89c7feSPavel Labath StopRunningThreads(deferred_signal_thread_sp->GetID()); 157445f5cb31SPavel Labath 15755830aa75STamas Berghammer return Error(); 1576e9547b80SChaoren Lin } 1577e9547b80SChaoren Lin 1578e9547b80SChaoren Lin Error 1579af245d11STodd Fiala NativeProcessLinux::Kill () 1580af245d11STodd Fiala { 1581af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1582af245d11STodd Fiala if (log) 1583af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s called for PID %" PRIu64, __FUNCTION__, GetID ()); 1584af245d11STodd Fiala 1585af245d11STodd Fiala Error error; 1586af245d11STodd Fiala 1587af245d11STodd Fiala switch (m_state) 1588af245d11STodd Fiala { 1589af245d11STodd Fiala case StateType::eStateInvalid: 1590af245d11STodd Fiala case StateType::eStateExited: 1591af245d11STodd Fiala case StateType::eStateCrashed: 1592af245d11STodd Fiala case StateType::eStateDetached: 1593af245d11STodd Fiala case StateType::eStateUnloaded: 1594af245d11STodd Fiala // Nothing to do - the process is already dead. 1595af245d11STodd Fiala if (log) 1596af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s ignored for PID %" PRIu64 " due to current state: %s", __FUNCTION__, GetID (), StateAsCString (m_state)); 1597af245d11STodd Fiala return error; 1598af245d11STodd Fiala 1599af245d11STodd Fiala case StateType::eStateConnected: 1600af245d11STodd Fiala case StateType::eStateAttaching: 1601af245d11STodd Fiala case StateType::eStateLaunching: 1602af245d11STodd Fiala case StateType::eStateStopped: 1603af245d11STodd Fiala case StateType::eStateRunning: 1604af245d11STodd Fiala case StateType::eStateStepping: 1605af245d11STodd Fiala case StateType::eStateSuspended: 1606af245d11STodd Fiala // We can try to kill a process in these states. 1607af245d11STodd Fiala break; 1608af245d11STodd Fiala } 1609af245d11STodd Fiala 1610af245d11STodd Fiala if (kill (GetID (), SIGKILL) != 0) 1611af245d11STodd Fiala { 1612af245d11STodd Fiala error.SetErrorToErrno (); 1613af245d11STodd Fiala return error; 1614af245d11STodd Fiala } 1615af245d11STodd Fiala 1616af245d11STodd Fiala return error; 1617af245d11STodd Fiala } 1618af245d11STodd Fiala 1619af245d11STodd Fiala static Error 1620af245d11STodd Fiala ParseMemoryRegionInfoFromProcMapsLine (const std::string &maps_line, MemoryRegionInfo &memory_region_info) 1621af245d11STodd Fiala { 1622af245d11STodd Fiala memory_region_info.Clear(); 1623af245d11STodd Fiala 1624af245d11STodd Fiala StringExtractor line_extractor (maps_line.c_str ()); 1625af245d11STodd Fiala 1626af245d11STodd Fiala // Format: {address_start_hex}-{address_end_hex} perms offset dev inode pathname 1627af245d11STodd Fiala // perms: rwxp (letter is present if set, '-' if not, final character is p=private, s=shared). 1628af245d11STodd Fiala 1629af245d11STodd Fiala // Parse out the starting address 1630af245d11STodd Fiala lldb::addr_t start_address = line_extractor.GetHexMaxU64 (false, 0); 1631af245d11STodd Fiala 1632af245d11STodd Fiala // Parse out hyphen separating start and end address from range. 1633af245d11STodd Fiala if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != '-')) 1634af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing dash between address range"); 1635af245d11STodd Fiala 1636af245d11STodd Fiala // Parse out the ending address 1637af245d11STodd Fiala lldb::addr_t end_address = line_extractor.GetHexMaxU64 (false, start_address); 1638af245d11STodd Fiala 1639af245d11STodd Fiala // Parse out the space after the address. 1640af245d11STodd Fiala if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != ' ')) 1641af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing space after range"); 1642af245d11STodd Fiala 1643af245d11STodd Fiala // Save the range. 1644af245d11STodd Fiala memory_region_info.GetRange ().SetRangeBase (start_address); 1645af245d11STodd Fiala memory_region_info.GetRange ().SetRangeEnd (end_address); 1646af245d11STodd Fiala 1647ad007563SHoward Hellyer // Any memory region in /proc/{pid}/maps is by definition mapped into the process. 1648ad007563SHoward Hellyer memory_region_info.SetMapped(MemoryRegionInfo::OptionalBool::eYes); 1649ad007563SHoward Hellyer 1650af245d11STodd Fiala // Parse out each permission entry. 1651af245d11STodd Fiala if (line_extractor.GetBytesLeft () < 4) 1652af245d11STodd Fiala return Error ("malformed /proc/{pid}/maps entry, missing some portion of permissions"); 1653af245d11STodd Fiala 1654af245d11STodd Fiala // Handle read permission. 1655af245d11STodd Fiala const char read_perm_char = line_extractor.GetChar (); 1656af245d11STodd Fiala if (read_perm_char == 'r') 1657af245d11STodd Fiala memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eYes); 1658c73301bbSTamas Berghammer else if (read_perm_char == '-') 1659af245d11STodd Fiala memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); 1660c73301bbSTamas Berghammer else 1661c73301bbSTamas Berghammer return Error ("unexpected /proc/{pid}/maps read permission char"); 1662af245d11STodd Fiala 1663af245d11STodd Fiala // Handle write permission. 1664af245d11STodd Fiala const char write_perm_char = line_extractor.GetChar (); 1665af245d11STodd Fiala if (write_perm_char == 'w') 1666af245d11STodd Fiala memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eYes); 1667c73301bbSTamas Berghammer else if (write_perm_char == '-') 1668af245d11STodd Fiala memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); 1669c73301bbSTamas Berghammer else 1670c73301bbSTamas Berghammer return Error ("unexpected /proc/{pid}/maps write permission char"); 1671af245d11STodd Fiala 1672af245d11STodd Fiala // Handle execute permission. 1673af245d11STodd Fiala const char exec_perm_char = line_extractor.GetChar (); 1674af245d11STodd Fiala if (exec_perm_char == 'x') 1675af245d11STodd Fiala memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eYes); 1676c73301bbSTamas Berghammer else if (exec_perm_char == '-') 1677af245d11STodd Fiala memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); 1678c73301bbSTamas Berghammer else 1679c73301bbSTamas Berghammer return Error ("unexpected /proc/{pid}/maps exec permission char"); 1680af245d11STodd Fiala 1681af245d11STodd Fiala return Error (); 1682af245d11STodd Fiala } 1683af245d11STodd Fiala 1684af245d11STodd Fiala Error 1685af245d11STodd Fiala NativeProcessLinux::GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) 1686af245d11STodd Fiala { 1687af245d11STodd Fiala // FIXME review that the final memory region returned extends to the end of the virtual address space, 1688af245d11STodd Fiala // with no perms if it is not mapped. 1689af245d11STodd Fiala 1690af245d11STodd Fiala // Use an approach that reads memory regions from /proc/{pid}/maps. 1691af245d11STodd Fiala // Assume proc maps entries are in ascending order. 1692af245d11STodd Fiala // FIXME assert if we find differently. 1693af245d11STodd Fiala 1694af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1695af245d11STodd Fiala Error error; 1696af245d11STodd Fiala 1697af245d11STodd Fiala if (m_supports_mem_region == LazyBool::eLazyBoolNo) 1698af245d11STodd Fiala { 1699af245d11STodd Fiala // We're done. 1700af245d11STodd Fiala error.SetErrorString ("unsupported"); 1701af245d11STodd Fiala return error; 1702af245d11STodd Fiala } 1703af245d11STodd Fiala 1704af245d11STodd Fiala // If our cache is empty, pull the latest. There should always be at least one memory region 1705af245d11STodd Fiala // if memory region handling is supported. 1706af245d11STodd Fiala if (m_mem_region_cache.empty ()) 1707af245d11STodd Fiala { 1708af245d11STodd Fiala error = ProcFileReader::ProcessLineByLine (GetID (), "maps", 1709af245d11STodd Fiala [&] (const std::string &line) -> bool 1710af245d11STodd Fiala { 1711af245d11STodd Fiala MemoryRegionInfo info; 1712af245d11STodd Fiala const Error parse_error = ParseMemoryRegionInfoFromProcMapsLine (line, info); 1713af245d11STodd Fiala if (parse_error.Success ()) 1714af245d11STodd Fiala { 1715af245d11STodd Fiala m_mem_region_cache.push_back (info); 1716af245d11STodd Fiala return true; 1717af245d11STodd Fiala } 1718af245d11STodd Fiala else 1719af245d11STodd Fiala { 1720af245d11STodd Fiala if (log) 1721af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to parse proc maps line '%s': %s", __FUNCTION__, line.c_str (), error.AsCString ()); 1722af245d11STodd Fiala return false; 1723af245d11STodd Fiala } 1724af245d11STodd Fiala }); 1725af245d11STodd Fiala 1726af245d11STodd Fiala // If we had an error, we'll mark unsupported. 1727af245d11STodd Fiala if (error.Fail ()) 1728af245d11STodd Fiala { 1729af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolNo; 1730af245d11STodd Fiala return error; 1731af245d11STodd Fiala } 1732af245d11STodd Fiala else if (m_mem_region_cache.empty ()) 1733af245d11STodd Fiala { 1734af245d11STodd Fiala // No entries after attempting to read them. This shouldn't happen if /proc/{pid}/maps 1735af245d11STodd Fiala // is supported. Assume we don't support map entries via procfs. 1736af245d11STodd Fiala if (log) 1737af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed to find any procfs maps entries, assuming no support for memory region metadata retrieval", __FUNCTION__); 1738af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolNo; 1739af245d11STodd Fiala error.SetErrorString ("not supported"); 1740af245d11STodd Fiala return error; 1741af245d11STodd Fiala } 1742af245d11STodd Fiala 1743af245d11STodd Fiala if (log) 1744af245d11STodd 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 ()); 1745af245d11STodd Fiala 1746af245d11STodd Fiala // We support memory retrieval, remember that. 1747af245d11STodd Fiala m_supports_mem_region = LazyBool::eLazyBoolYes; 1748af245d11STodd Fiala } 1749af245d11STodd Fiala else 1750af245d11STodd Fiala { 1751af245d11STodd Fiala if (log) 1752af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s reusing %" PRIu64 " cached memory region entries", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); 1753af245d11STodd Fiala } 1754af245d11STodd Fiala 1755af245d11STodd Fiala lldb::addr_t prev_base_address = 0; 1756af245d11STodd Fiala 1757af245d11STodd Fiala // FIXME start by finding the last region that is <= target address using binary search. Data is sorted. 1758af245d11STodd Fiala // There can be a ton of regions on pthreads apps with lots of threads. 1759af245d11STodd Fiala for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end (); ++it) 1760af245d11STodd Fiala { 1761af245d11STodd Fiala MemoryRegionInfo &proc_entry_info = *it; 1762af245d11STodd Fiala 1763af245d11STodd Fiala // Sanity check assumption that /proc/{pid}/maps entries are ascending. 1764af245d11STodd Fiala assert ((proc_entry_info.GetRange ().GetRangeBase () >= prev_base_address) && "descending /proc/pid/maps entries detected, unexpected"); 1765af245d11STodd Fiala prev_base_address = proc_entry_info.GetRange ().GetRangeBase (); 1766af245d11STodd Fiala 1767af245d11STodd Fiala // If the target address comes before this entry, indicate distance to next region. 1768af245d11STodd Fiala if (load_addr < proc_entry_info.GetRange ().GetRangeBase ()) 1769af245d11STodd Fiala { 1770af245d11STodd Fiala range_info.GetRange ().SetRangeBase (load_addr); 1771af245d11STodd Fiala range_info.GetRange ().SetByteSize (proc_entry_info.GetRange ().GetRangeBase () - load_addr); 1772af245d11STodd Fiala range_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); 1773af245d11STodd Fiala range_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); 1774af245d11STodd Fiala range_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); 1775ad007563SHoward Hellyer range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 1776af245d11STodd Fiala 1777af245d11STodd Fiala return error; 1778af245d11STodd Fiala } 1779af245d11STodd Fiala else if (proc_entry_info.GetRange ().Contains (load_addr)) 1780af245d11STodd Fiala { 1781af245d11STodd Fiala // The target address is within the memory region we're processing here. 1782af245d11STodd Fiala range_info = proc_entry_info; 1783af245d11STodd Fiala return error; 1784af245d11STodd Fiala } 1785af245d11STodd Fiala 1786af245d11STodd Fiala // The target memory address comes somewhere after the region we just parsed. 1787af245d11STodd Fiala } 1788af245d11STodd Fiala 178909839c33STamas Berghammer // If we made it here, we didn't find an entry that contained the given address. Return the 179009839c33STamas Berghammer // load_addr as start and the amount of bytes betwwen load address and the end of the memory as 179109839c33STamas Berghammer // size. 179209839c33STamas Berghammer range_info.GetRange ().SetRangeBase (load_addr); 1793ad007563SHoward Hellyer range_info.GetRange ().SetRangeEnd(LLDB_INVALID_ADDRESS); 179409839c33STamas Berghammer range_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); 179509839c33STamas Berghammer range_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); 179609839c33STamas Berghammer range_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); 1797ad007563SHoward Hellyer range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 1798af245d11STodd Fiala return error; 1799af245d11STodd Fiala } 1800af245d11STodd Fiala 1801af245d11STodd Fiala void 1802af245d11STodd Fiala NativeProcessLinux::DoStopIDBumped (uint32_t newBumpId) 1803af245d11STodd Fiala { 1804af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 1805af245d11STodd Fiala if (log) 1806af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s(newBumpId=%" PRIu32 ") called", __FUNCTION__, newBumpId); 1807af245d11STodd Fiala 1808af245d11STodd Fiala if (log) 1809af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s clearing %" PRIu64 " entries from the cache", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); 1810af245d11STodd Fiala m_mem_region_cache.clear (); 1811af245d11STodd Fiala } 1812af245d11STodd Fiala 1813af245d11STodd Fiala Error 18143eb4b458SChaoren Lin NativeProcessLinux::AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) 1815af245d11STodd Fiala { 1816af245d11STodd Fiala // FIXME implementing this requires the equivalent of 1817af245d11STodd Fiala // InferiorCallPOSIX::InferiorCallMmap, which depends on 1818af245d11STodd Fiala // functional ThreadPlans working with Native*Protocol. 1819af245d11STodd Fiala #if 1 1820af245d11STodd Fiala return Error ("not implemented yet"); 1821af245d11STodd Fiala #else 1822af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 1823af245d11STodd Fiala 1824af245d11STodd Fiala unsigned prot = 0; 1825af245d11STodd Fiala if (permissions & lldb::ePermissionsReadable) 1826af245d11STodd Fiala prot |= eMmapProtRead; 1827af245d11STodd Fiala if (permissions & lldb::ePermissionsWritable) 1828af245d11STodd Fiala prot |= eMmapProtWrite; 1829af245d11STodd Fiala if (permissions & lldb::ePermissionsExecutable) 1830af245d11STodd Fiala prot |= eMmapProtExec; 1831af245d11STodd Fiala 1832af245d11STodd Fiala // TODO implement this directly in NativeProcessLinux 1833af245d11STodd Fiala // (and lift to NativeProcessPOSIX if/when that class is 1834af245d11STodd Fiala // refactored out). 1835af245d11STodd Fiala if (InferiorCallMmap(this, addr, 0, size, prot, 1836af245d11STodd Fiala eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { 1837af245d11STodd Fiala m_addr_to_mmap_size[addr] = size; 1838af245d11STodd Fiala return Error (); 1839af245d11STodd Fiala } else { 1840af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 1841af245d11STodd Fiala return Error("unable to allocate %" PRIu64 " bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions)); 1842af245d11STodd Fiala } 1843af245d11STodd Fiala #endif 1844af245d11STodd Fiala } 1845af245d11STodd Fiala 1846af245d11STodd Fiala Error 1847af245d11STodd Fiala NativeProcessLinux::DeallocateMemory (lldb::addr_t addr) 1848af245d11STodd Fiala { 1849af245d11STodd Fiala // FIXME see comments in AllocateMemory - required lower-level 1850af245d11STodd Fiala // bits not in place yet (ThreadPlans) 1851af245d11STodd Fiala return Error ("not implemented"); 1852af245d11STodd Fiala } 1853af245d11STodd Fiala 1854af245d11STodd Fiala lldb::addr_t 1855af245d11STodd Fiala NativeProcessLinux::GetSharedLibraryInfoAddress () 1856af245d11STodd Fiala { 1857af245d11STodd Fiala // punt on this for now 1858af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 1859af245d11STodd Fiala } 1860af245d11STodd Fiala 1861af245d11STodd Fiala size_t 1862af245d11STodd Fiala NativeProcessLinux::UpdateThreads () 1863af245d11STodd Fiala { 1864af245d11STodd Fiala // The NativeProcessLinux monitoring threads are always up to date 1865af245d11STodd Fiala // with respect to thread state and they keep the thread list 1866af245d11STodd Fiala // populated properly. All this method needs to do is return the 1867af245d11STodd Fiala // thread count. 1868af245d11STodd Fiala return m_threads.size (); 1869af245d11STodd Fiala } 1870af245d11STodd Fiala 1871af245d11STodd Fiala bool 1872af245d11STodd Fiala NativeProcessLinux::GetArchitecture (ArchSpec &arch) const 1873af245d11STodd Fiala { 1874af245d11STodd Fiala arch = m_arch; 1875af245d11STodd Fiala return true; 1876af245d11STodd Fiala } 1877af245d11STodd Fiala 1878af245d11STodd Fiala Error 1879b9cc0c75SPavel Labath NativeProcessLinux::GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size) 1880af245d11STodd Fiala { 1881af245d11STodd Fiala // FIXME put this behind a breakpoint protocol class that can be 1882af245d11STodd Fiala // set per architecture. Need ARM, MIPS support here. 1883af245d11STodd Fiala static const uint8_t g_i386_opcode [] = { 0xCC }; 1884bb00d0b6SUlrich Weigand static const uint8_t g_s390x_opcode[] = { 0x00, 0x01 }; 1885af245d11STodd Fiala 1886af245d11STodd Fiala switch (m_arch.GetMachine ()) 1887af245d11STodd Fiala { 1888af245d11STodd Fiala case llvm::Triple::x86: 1889af245d11STodd Fiala case llvm::Triple::x86_64: 1890af245d11STodd Fiala actual_opcode_size = static_cast<uint32_t> (sizeof(g_i386_opcode)); 1891af245d11STodd Fiala return Error (); 1892af245d11STodd Fiala 1893bb00d0b6SUlrich Weigand case llvm::Triple::systemz: 1894bb00d0b6SUlrich Weigand actual_opcode_size = static_cast<uint32_t> (sizeof(g_s390x_opcode)); 1895bb00d0b6SUlrich Weigand return Error (); 1896bb00d0b6SUlrich Weigand 1897ff7fd900STamas Berghammer case llvm::Triple::arm: 1898ff7fd900STamas Berghammer case llvm::Triple::aarch64: 1899e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64: 1900e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64el: 1901ce815e45SSagar Thakur case llvm::Triple::mips: 1902ce815e45SSagar Thakur case llvm::Triple::mipsel: 1903ff7fd900STamas Berghammer // On these architectures the PC don't get updated for breakpoint hits 1904c60c9452SJaydeep Patil actual_opcode_size = 0; 1905e8659b5dSMohit K. Bhakkad return Error (); 1906e8659b5dSMohit K. Bhakkad 1907af245d11STodd Fiala default: 1908af245d11STodd Fiala assert(false && "CPU type not supported!"); 1909af245d11STodd Fiala return Error ("CPU type not supported"); 1910af245d11STodd Fiala } 1911af245d11STodd Fiala } 1912af245d11STodd Fiala 1913af245d11STodd Fiala Error 1914af245d11STodd Fiala NativeProcessLinux::SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) 1915af245d11STodd Fiala { 1916af245d11STodd Fiala if (hardware) 1917af245d11STodd Fiala return Error ("NativeProcessLinux does not support hardware breakpoints"); 1918af245d11STodd Fiala else 1919af245d11STodd Fiala return SetSoftwareBreakpoint (addr, size); 1920af245d11STodd Fiala } 1921af245d11STodd Fiala 1922af245d11STodd Fiala Error 192363c8be95STamas Berghammer NativeProcessLinux::GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, 192463c8be95STamas Berghammer size_t &actual_opcode_size, 192563c8be95STamas Berghammer const uint8_t *&trap_opcode_bytes) 1926af245d11STodd Fiala { 192763c8be95STamas Berghammer // FIXME put this behind a breakpoint protocol class that can be set per 192863c8be95STamas Berghammer // architecture. Need MIPS support here. 19292afc5966STodd Fiala static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; 1930be379e15STamas Berghammer // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the 1931be379e15STamas Berghammer // linux kernel does otherwise. 1932be379e15STamas Berghammer static const uint8_t g_arm_breakpoint_opcode[] = { 0xf0, 0x01, 0xf0, 0xe7 }; 1933af245d11STodd Fiala static const uint8_t g_i386_opcode [] = { 0xCC }; 19343df471c3SMohit K. Bhakkad static const uint8_t g_mips64_opcode[] = { 0x00, 0x00, 0x00, 0x0d }; 19352c2acf96SMohit K. Bhakkad static const uint8_t g_mips64el_opcode[] = { 0x0d, 0x00, 0x00, 0x00 }; 1936bb00d0b6SUlrich Weigand static const uint8_t g_s390x_opcode[] = { 0x00, 0x01 }; 1937be379e15STamas Berghammer static const uint8_t g_thumb_breakpoint_opcode[] = { 0x01, 0xde }; 1938af245d11STodd Fiala 1939af245d11STodd Fiala switch (m_arch.GetMachine ()) 1940af245d11STodd Fiala { 19412afc5966STodd Fiala case llvm::Triple::aarch64: 19422afc5966STodd Fiala trap_opcode_bytes = g_aarch64_opcode; 19432afc5966STodd Fiala actual_opcode_size = sizeof(g_aarch64_opcode); 19442afc5966STodd Fiala return Error (); 19452afc5966STodd Fiala 194663c8be95STamas Berghammer case llvm::Triple::arm: 194763c8be95STamas Berghammer switch (trap_opcode_size_hint) 194863c8be95STamas Berghammer { 194963c8be95STamas Berghammer case 2: 195063c8be95STamas Berghammer trap_opcode_bytes = g_thumb_breakpoint_opcode; 195163c8be95STamas Berghammer actual_opcode_size = sizeof(g_thumb_breakpoint_opcode); 195263c8be95STamas Berghammer return Error (); 195363c8be95STamas Berghammer case 4: 195463c8be95STamas Berghammer trap_opcode_bytes = g_arm_breakpoint_opcode; 195563c8be95STamas Berghammer actual_opcode_size = sizeof(g_arm_breakpoint_opcode); 195663c8be95STamas Berghammer return Error (); 195763c8be95STamas Berghammer default: 195863c8be95STamas Berghammer assert(false && "Unrecognised trap opcode size hint!"); 195963c8be95STamas Berghammer return Error ("Unrecognised trap opcode size hint!"); 196063c8be95STamas Berghammer } 196163c8be95STamas Berghammer 1962af245d11STodd Fiala case llvm::Triple::x86: 1963af245d11STodd Fiala case llvm::Triple::x86_64: 1964af245d11STodd Fiala trap_opcode_bytes = g_i386_opcode; 1965af245d11STodd Fiala actual_opcode_size = sizeof(g_i386_opcode); 1966af245d11STodd Fiala return Error (); 1967af245d11STodd Fiala 1968ce815e45SSagar Thakur case llvm::Triple::mips: 19693df471c3SMohit K. Bhakkad case llvm::Triple::mips64: 19703df471c3SMohit K. Bhakkad trap_opcode_bytes = g_mips64_opcode; 19713df471c3SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64_opcode); 19723df471c3SMohit K. Bhakkad return Error (); 19733df471c3SMohit K. Bhakkad 1974ce815e45SSagar Thakur case llvm::Triple::mipsel: 19752c2acf96SMohit K. Bhakkad case llvm::Triple::mips64el: 19762c2acf96SMohit K. Bhakkad trap_opcode_bytes = g_mips64el_opcode; 19772c2acf96SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64el_opcode); 19782c2acf96SMohit K. Bhakkad return Error (); 19792c2acf96SMohit K. Bhakkad 1980bb00d0b6SUlrich Weigand case llvm::Triple::systemz: 1981bb00d0b6SUlrich Weigand trap_opcode_bytes = g_s390x_opcode; 1982bb00d0b6SUlrich Weigand actual_opcode_size = sizeof(g_s390x_opcode); 1983bb00d0b6SUlrich Weigand return Error (); 1984bb00d0b6SUlrich Weigand 1985af245d11STodd Fiala default: 1986af245d11STodd Fiala assert(false && "CPU type not supported!"); 1987af245d11STodd Fiala return Error ("CPU type not supported"); 1988af245d11STodd Fiala } 1989af245d11STodd Fiala } 1990af245d11STodd Fiala 1991af245d11STodd Fiala #if 0 1992af245d11STodd Fiala ProcessMessage::CrashReason 1993af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info) 1994af245d11STodd Fiala { 1995af245d11STodd Fiala ProcessMessage::CrashReason reason; 1996af245d11STodd Fiala assert(info->si_signo == SIGSEGV); 1997af245d11STodd Fiala 1998af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1999af245d11STodd Fiala 2000af245d11STodd Fiala switch (info->si_code) 2001af245d11STodd Fiala { 2002af245d11STodd Fiala default: 2003af245d11STodd Fiala assert(false && "unexpected si_code for SIGSEGV"); 2004af245d11STodd Fiala break; 2005af245d11STodd Fiala case SI_KERNEL: 2006af245d11STodd Fiala // Linux will occasionally send spurious SI_KERNEL codes. 2007af245d11STodd Fiala // (this is poorly documented in sigaction) 2008af245d11STodd Fiala // One way to get this is via unaligned SIMD loads. 2009af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; // for lack of anything better 2010af245d11STodd Fiala break; 2011af245d11STodd Fiala case SEGV_MAPERR: 2012af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; 2013af245d11STodd Fiala break; 2014af245d11STodd Fiala case SEGV_ACCERR: 2015af245d11STodd Fiala reason = ProcessMessage::ePrivilegedAddress; 2016af245d11STodd Fiala break; 2017af245d11STodd Fiala } 2018af245d11STodd Fiala 2019af245d11STodd Fiala return reason; 2020af245d11STodd Fiala } 2021af245d11STodd Fiala #endif 2022af245d11STodd Fiala 2023af245d11STodd Fiala 2024af245d11STodd Fiala #if 0 2025af245d11STodd Fiala ProcessMessage::CrashReason 2026af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info) 2027af245d11STodd Fiala { 2028af245d11STodd Fiala ProcessMessage::CrashReason reason; 2029af245d11STodd Fiala assert(info->si_signo == SIGILL); 2030af245d11STodd Fiala 2031af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 2032af245d11STodd Fiala 2033af245d11STodd Fiala switch (info->si_code) 2034af245d11STodd Fiala { 2035af245d11STodd Fiala default: 2036af245d11STodd Fiala assert(false && "unexpected si_code for SIGILL"); 2037af245d11STodd Fiala break; 2038af245d11STodd Fiala case ILL_ILLOPC: 2039af245d11STodd Fiala reason = ProcessMessage::eIllegalOpcode; 2040af245d11STodd Fiala break; 2041af245d11STodd Fiala case ILL_ILLOPN: 2042af245d11STodd Fiala reason = ProcessMessage::eIllegalOperand; 2043af245d11STodd Fiala break; 2044af245d11STodd Fiala case ILL_ILLADR: 2045af245d11STodd Fiala reason = ProcessMessage::eIllegalAddressingMode; 2046af245d11STodd Fiala break; 2047af245d11STodd Fiala case ILL_ILLTRP: 2048af245d11STodd Fiala reason = ProcessMessage::eIllegalTrap; 2049af245d11STodd Fiala break; 2050af245d11STodd Fiala case ILL_PRVOPC: 2051af245d11STodd Fiala reason = ProcessMessage::ePrivilegedOpcode; 2052af245d11STodd Fiala break; 2053af245d11STodd Fiala case ILL_PRVREG: 2054af245d11STodd Fiala reason = ProcessMessage::ePrivilegedRegister; 2055af245d11STodd Fiala break; 2056af245d11STodd Fiala case ILL_COPROC: 2057af245d11STodd Fiala reason = ProcessMessage::eCoprocessorError; 2058af245d11STodd Fiala break; 2059af245d11STodd Fiala case ILL_BADSTK: 2060af245d11STodd Fiala reason = ProcessMessage::eInternalStackError; 2061af245d11STodd Fiala break; 2062af245d11STodd Fiala } 2063af245d11STodd Fiala 2064af245d11STodd Fiala return reason; 2065af245d11STodd Fiala } 2066af245d11STodd Fiala #endif 2067af245d11STodd Fiala 2068af245d11STodd Fiala #if 0 2069af245d11STodd Fiala ProcessMessage::CrashReason 2070af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info) 2071af245d11STodd Fiala { 2072af245d11STodd Fiala ProcessMessage::CrashReason reason; 2073af245d11STodd Fiala assert(info->si_signo == SIGFPE); 2074af245d11STodd Fiala 2075af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 2076af245d11STodd Fiala 2077af245d11STodd Fiala switch (info->si_code) 2078af245d11STodd Fiala { 2079af245d11STodd Fiala default: 2080af245d11STodd Fiala assert(false && "unexpected si_code for SIGFPE"); 2081af245d11STodd Fiala break; 2082af245d11STodd Fiala case FPE_INTDIV: 2083af245d11STodd Fiala reason = ProcessMessage::eIntegerDivideByZero; 2084af245d11STodd Fiala break; 2085af245d11STodd Fiala case FPE_INTOVF: 2086af245d11STodd Fiala reason = ProcessMessage::eIntegerOverflow; 2087af245d11STodd Fiala break; 2088af245d11STodd Fiala case FPE_FLTDIV: 2089af245d11STodd Fiala reason = ProcessMessage::eFloatDivideByZero; 2090af245d11STodd Fiala break; 2091af245d11STodd Fiala case FPE_FLTOVF: 2092af245d11STodd Fiala reason = ProcessMessage::eFloatOverflow; 2093af245d11STodd Fiala break; 2094af245d11STodd Fiala case FPE_FLTUND: 2095af245d11STodd Fiala reason = ProcessMessage::eFloatUnderflow; 2096af245d11STodd Fiala break; 2097af245d11STodd Fiala case FPE_FLTRES: 2098af245d11STodd Fiala reason = ProcessMessage::eFloatInexactResult; 2099af245d11STodd Fiala break; 2100af245d11STodd Fiala case FPE_FLTINV: 2101af245d11STodd Fiala reason = ProcessMessage::eFloatInvalidOperation; 2102af245d11STodd Fiala break; 2103af245d11STodd Fiala case FPE_FLTSUB: 2104af245d11STodd Fiala reason = ProcessMessage::eFloatSubscriptRange; 2105af245d11STodd Fiala break; 2106af245d11STodd Fiala } 2107af245d11STodd Fiala 2108af245d11STodd Fiala return reason; 2109af245d11STodd Fiala } 2110af245d11STodd Fiala #endif 2111af245d11STodd Fiala 2112af245d11STodd Fiala #if 0 2113af245d11STodd Fiala ProcessMessage::CrashReason 2114af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info) 2115af245d11STodd Fiala { 2116af245d11STodd Fiala ProcessMessage::CrashReason reason; 2117af245d11STodd Fiala assert(info->si_signo == SIGBUS); 2118af245d11STodd Fiala 2119af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 2120af245d11STodd Fiala 2121af245d11STodd Fiala switch (info->si_code) 2122af245d11STodd Fiala { 2123af245d11STodd Fiala default: 2124af245d11STodd Fiala assert(false && "unexpected si_code for SIGBUS"); 2125af245d11STodd Fiala break; 2126af245d11STodd Fiala case BUS_ADRALN: 2127af245d11STodd Fiala reason = ProcessMessage::eIllegalAlignment; 2128af245d11STodd Fiala break; 2129af245d11STodd Fiala case BUS_ADRERR: 2130af245d11STodd Fiala reason = ProcessMessage::eIllegalAddress; 2131af245d11STodd Fiala break; 2132af245d11STodd Fiala case BUS_OBJERR: 2133af245d11STodd Fiala reason = ProcessMessage::eHardwareError; 2134af245d11STodd Fiala break; 2135af245d11STodd Fiala } 2136af245d11STodd Fiala 2137af245d11STodd Fiala return reason; 2138af245d11STodd Fiala } 2139af245d11STodd Fiala #endif 2140af245d11STodd Fiala 2141af245d11STodd Fiala Error 214226438d26SChaoren Lin NativeProcessLinux::ReadMemory (lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) 2143af245d11STodd Fiala { 2144df7c6995SPavel Labath if (ProcessVmReadvSupported()) { 2145df7c6995SPavel Labath // The process_vm_readv path is about 50 times faster than ptrace api. We want to use 2146df7c6995SPavel Labath // this syscall if it is supported. 2147df7c6995SPavel Labath 2148df7c6995SPavel Labath const ::pid_t pid = GetID(); 2149df7c6995SPavel Labath 2150df7c6995SPavel Labath struct iovec local_iov, remote_iov; 2151df7c6995SPavel Labath local_iov.iov_base = buf; 2152df7c6995SPavel Labath local_iov.iov_len = size; 2153df7c6995SPavel Labath remote_iov.iov_base = reinterpret_cast<void *>(addr); 2154df7c6995SPavel Labath remote_iov.iov_len = size; 2155df7c6995SPavel Labath 2156df7c6995SPavel Labath bytes_read = process_vm_readv(pid, &local_iov, 1, &remote_iov, 1, 0); 2157df7c6995SPavel Labath const bool success = bytes_read == size; 2158df7c6995SPavel Labath 2159df7c6995SPavel Labath Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); 2160df7c6995SPavel Labath if (log) 2161df7c6995SPavel Labath log->Printf ("NativeProcessLinux::%s using process_vm_readv to read %zd bytes from inferior address 0x%" PRIx64": %s", 2162df7c6995SPavel Labath __FUNCTION__, size, addr, success ? "Success" : strerror(errno)); 2163df7c6995SPavel Labath 2164df7c6995SPavel Labath if (success) 2165df7c6995SPavel Labath return Error(); 2166df7c6995SPavel Labath // else 2167df7c6995SPavel Labath // the call failed for some reason, let's retry the read using ptrace api. 2168df7c6995SPavel Labath } 2169df7c6995SPavel Labath 217019cbe96aSPavel Labath unsigned char *dst = static_cast<unsigned char*>(buf); 217119cbe96aSPavel Labath size_t remainder; 217219cbe96aSPavel Labath long data; 217319cbe96aSPavel Labath 217419cbe96aSPavel Labath Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); 217519cbe96aSPavel Labath if (log) 217619cbe96aSPavel Labath ProcessPOSIXLog::IncNestLevel(); 217719cbe96aSPavel Labath if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) 217819cbe96aSPavel Labath log->Printf ("NativeProcessLinux::%s(%p, %p, %zd, _)", __FUNCTION__, (void*)addr, buf, size); 217919cbe96aSPavel Labath 218019cbe96aSPavel Labath for (bytes_read = 0; bytes_read < size; bytes_read += remainder) 218119cbe96aSPavel Labath { 218219cbe96aSPavel Labath Error error = NativeProcessLinux::PtraceWrapper(PTRACE_PEEKDATA, GetID(), (void*)addr, nullptr, 0, &data); 218319cbe96aSPavel Labath if (error.Fail()) 218419cbe96aSPavel Labath { 218519cbe96aSPavel Labath if (log) 218619cbe96aSPavel Labath ProcessPOSIXLog::DecNestLevel(); 218719cbe96aSPavel Labath return error; 218819cbe96aSPavel Labath } 218919cbe96aSPavel Labath 219019cbe96aSPavel Labath remainder = size - bytes_read; 219119cbe96aSPavel Labath remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; 219219cbe96aSPavel Labath 219319cbe96aSPavel Labath // Copy the data into our buffer 2194f6ef187bSMohit K. Bhakkad memcpy(dst, &data, remainder); 219519cbe96aSPavel Labath 219619cbe96aSPavel Labath if (log && ProcessPOSIXLog::AtTopNestLevel() && 219719cbe96aSPavel Labath (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 219819cbe96aSPavel Labath (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 219919cbe96aSPavel Labath size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 220019cbe96aSPavel Labath { 220119cbe96aSPavel Labath uintptr_t print_dst = 0; 220219cbe96aSPavel Labath // Format bytes from data by moving into print_dst for log output 220319cbe96aSPavel Labath for (unsigned i = 0; i < remainder; ++i) 220419cbe96aSPavel Labath print_dst |= (((data >> i*8) & 0xFF) << i*8); 220579203995SPavel Labath log->Printf ("NativeProcessLinux::%s() [0x%" PRIx64 "]:0x%" PRIx64 " (0x%" PRIx64 ")", 220679203995SPavel Labath __FUNCTION__, addr, uint64_t(print_dst), uint64_t(data)); 220719cbe96aSPavel Labath } 220819cbe96aSPavel Labath addr += k_ptrace_word_size; 220919cbe96aSPavel Labath dst += k_ptrace_word_size; 221019cbe96aSPavel Labath } 221119cbe96aSPavel Labath 221219cbe96aSPavel Labath if (log) 221319cbe96aSPavel Labath ProcessPOSIXLog::DecNestLevel(); 221419cbe96aSPavel Labath return Error(); 2215af245d11STodd Fiala } 2216af245d11STodd Fiala 2217af245d11STodd Fiala Error 22183eb4b458SChaoren Lin NativeProcessLinux::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) 22193eb4b458SChaoren Lin { 22203eb4b458SChaoren Lin Error error = ReadMemory(addr, buf, size, bytes_read); 22213eb4b458SChaoren Lin if (error.Fail()) return error; 22223eb4b458SChaoren Lin return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); 22233eb4b458SChaoren Lin } 22243eb4b458SChaoren Lin 22253eb4b458SChaoren Lin Error 22263eb4b458SChaoren Lin NativeProcessLinux::WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) 2227af245d11STodd Fiala { 222819cbe96aSPavel Labath const unsigned char *src = static_cast<const unsigned char*>(buf); 222919cbe96aSPavel Labath size_t remainder; 223019cbe96aSPavel Labath Error error; 223119cbe96aSPavel Labath 223219cbe96aSPavel Labath Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); 223319cbe96aSPavel Labath if (log) 223419cbe96aSPavel Labath ProcessPOSIXLog::IncNestLevel(); 223519cbe96aSPavel Labath if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) 223679203995SPavel Labath log->Printf ("NativeProcessLinux::%s(0x%" PRIx64 ", %p, %zu)", __FUNCTION__, addr, buf, size); 223719cbe96aSPavel Labath 223819cbe96aSPavel Labath for (bytes_written = 0; bytes_written < size; bytes_written += remainder) 223919cbe96aSPavel Labath { 224019cbe96aSPavel Labath remainder = size - bytes_written; 224119cbe96aSPavel Labath remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; 224219cbe96aSPavel Labath 224319cbe96aSPavel Labath if (remainder == k_ptrace_word_size) 224419cbe96aSPavel Labath { 224519cbe96aSPavel Labath unsigned long data = 0; 2246f6ef187bSMohit K. Bhakkad memcpy(&data, src, k_ptrace_word_size); 224719cbe96aSPavel Labath 224819cbe96aSPavel Labath if (log && ProcessPOSIXLog::AtTopNestLevel() && 224919cbe96aSPavel Labath (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 225019cbe96aSPavel Labath (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 225119cbe96aSPavel Labath size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 225219cbe96aSPavel Labath log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 225319cbe96aSPavel Labath (void*)addr, *(const unsigned long*)src, data); 225419cbe96aSPavel Labath 225519cbe96aSPavel Labath error = NativeProcessLinux::PtraceWrapper(PTRACE_POKEDATA, GetID(), (void*)addr, (void*)data); 225619cbe96aSPavel Labath if (error.Fail()) 225719cbe96aSPavel Labath { 225819cbe96aSPavel Labath if (log) 225919cbe96aSPavel Labath ProcessPOSIXLog::DecNestLevel(); 226019cbe96aSPavel Labath return error; 226119cbe96aSPavel Labath } 226219cbe96aSPavel Labath } 226319cbe96aSPavel Labath else 226419cbe96aSPavel Labath { 226519cbe96aSPavel Labath unsigned char buff[8]; 226619cbe96aSPavel Labath size_t bytes_read; 226719cbe96aSPavel Labath error = ReadMemory(addr, buff, k_ptrace_word_size, bytes_read); 226819cbe96aSPavel Labath if (error.Fail()) 226919cbe96aSPavel Labath { 227019cbe96aSPavel Labath if (log) 227119cbe96aSPavel Labath ProcessPOSIXLog::DecNestLevel(); 227219cbe96aSPavel Labath return error; 227319cbe96aSPavel Labath } 227419cbe96aSPavel Labath 227519cbe96aSPavel Labath memcpy(buff, src, remainder); 227619cbe96aSPavel Labath 227719cbe96aSPavel Labath size_t bytes_written_rec; 227819cbe96aSPavel Labath error = WriteMemory(addr, buff, k_ptrace_word_size, bytes_written_rec); 227919cbe96aSPavel Labath if (error.Fail()) 228019cbe96aSPavel Labath { 228119cbe96aSPavel Labath if (log) 228219cbe96aSPavel Labath ProcessPOSIXLog::DecNestLevel(); 228319cbe96aSPavel Labath return error; 228419cbe96aSPavel Labath } 228519cbe96aSPavel Labath 228619cbe96aSPavel Labath if (log && ProcessPOSIXLog::AtTopNestLevel() && 228719cbe96aSPavel Labath (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || 228819cbe96aSPavel Labath (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && 228919cbe96aSPavel Labath size <= POSIX_LOG_MEMORY_SHORT_BYTES))) 229019cbe96aSPavel Labath log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, 229119cbe96aSPavel Labath (void*)addr, *(const unsigned long*)src, *(unsigned long*)buff); 229219cbe96aSPavel Labath } 229319cbe96aSPavel Labath 229419cbe96aSPavel Labath addr += k_ptrace_word_size; 229519cbe96aSPavel Labath src += k_ptrace_word_size; 229619cbe96aSPavel Labath } 229719cbe96aSPavel Labath if (log) 229819cbe96aSPavel Labath ProcessPOSIXLog::DecNestLevel(); 229919cbe96aSPavel Labath return error; 2300af245d11STodd Fiala } 2301af245d11STodd Fiala 230297ccc294SChaoren Lin Error 230397ccc294SChaoren Lin NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) 2304af245d11STodd Fiala { 230519cbe96aSPavel Labath return PtraceWrapper(PTRACE_GETSIGINFO, tid, nullptr, siginfo); 2306af245d11STodd Fiala } 2307af245d11STodd Fiala 230897ccc294SChaoren Lin Error 2309af245d11STodd Fiala NativeProcessLinux::GetEventMessage(lldb::tid_t tid, unsigned long *message) 2310af245d11STodd Fiala { 231119cbe96aSPavel Labath return PtraceWrapper(PTRACE_GETEVENTMSG, tid, nullptr, message); 2312af245d11STodd Fiala } 2313af245d11STodd Fiala 2314db264a6dSTamas Berghammer Error 2315af245d11STodd Fiala NativeProcessLinux::Detach(lldb::tid_t tid) 2316af245d11STodd Fiala { 231797ccc294SChaoren Lin if (tid == LLDB_INVALID_THREAD_ID) 231897ccc294SChaoren Lin return Error(); 231997ccc294SChaoren Lin 232019cbe96aSPavel Labath return PtraceWrapper(PTRACE_DETACH, tid); 2321af245d11STodd Fiala } 2322af245d11STodd Fiala 2323af245d11STodd Fiala bool 2324af245d11STodd Fiala NativeProcessLinux::HasThreadNoLock (lldb::tid_t thread_id) 2325af245d11STodd Fiala { 2326af245d11STodd Fiala for (auto thread_sp : m_threads) 2327af245d11STodd Fiala { 2328af245d11STodd Fiala assert (thread_sp && "thread list should not contain NULL threads"); 2329af245d11STodd Fiala if (thread_sp->GetID () == thread_id) 2330af245d11STodd Fiala { 2331af245d11STodd Fiala // We have this thread. 2332af245d11STodd Fiala return true; 2333af245d11STodd Fiala } 2334af245d11STodd Fiala } 2335af245d11STodd Fiala 2336af245d11STodd Fiala // We don't have this thread. 2337af245d11STodd Fiala return false; 2338af245d11STodd Fiala } 2339af245d11STodd Fiala 2340af245d11STodd Fiala bool 2341af245d11STodd Fiala NativeProcessLinux::StopTrackingThread (lldb::tid_t thread_id) 2342af245d11STodd Fiala { 23431dbc6c9cSPavel Labath Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 23441dbc6c9cSPavel Labath 23451dbc6c9cSPavel Labath if (log) 23461dbc6c9cSPavel Labath log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ")", __FUNCTION__, thread_id); 23471dbc6c9cSPavel Labath 23481dbc6c9cSPavel Labath bool found = false; 23491dbc6c9cSPavel Labath 2350af245d11STodd Fiala for (auto it = m_threads.begin (); it != m_threads.end (); ++it) 2351af245d11STodd Fiala { 2352af245d11STodd Fiala if (*it && ((*it)->GetID () == thread_id)) 2353af245d11STodd Fiala { 2354af245d11STodd Fiala m_threads.erase (it); 23551dbc6c9cSPavel Labath found = true; 23561dbc6c9cSPavel Labath break; 2357af245d11STodd Fiala } 2358af245d11STodd Fiala } 2359af245d11STodd Fiala 23609eb1ecb9SPavel Labath SignalIfAllThreadsStopped(); 23611dbc6c9cSPavel Labath 23621dbc6c9cSPavel Labath return found; 2363af245d11STodd Fiala } 2364af245d11STodd Fiala 2365f9077782SPavel Labath NativeThreadLinuxSP 2366af245d11STodd Fiala NativeProcessLinux::AddThread (lldb::tid_t thread_id) 2367af245d11STodd Fiala { 2368af245d11STodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 2369af245d11STodd Fiala 2370af245d11STodd Fiala if (log) 2371af245d11STodd Fiala { 2372af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " adding thread with tid %" PRIu64, 2373af245d11STodd Fiala __FUNCTION__, 2374af245d11STodd Fiala GetID (), 2375af245d11STodd Fiala thread_id); 2376af245d11STodd Fiala } 2377af245d11STodd Fiala 2378af245d11STodd Fiala assert (!HasThreadNoLock (thread_id) && "attempted to add a thread by id that already exists"); 2379af245d11STodd Fiala 2380af245d11STodd Fiala // If this is the first thread, save it as the current thread 2381af245d11STodd Fiala if (m_threads.empty ()) 2382af245d11STodd Fiala SetCurrentThreadID (thread_id); 2383af245d11STodd Fiala 2384f9077782SPavel Labath auto thread_sp = std::make_shared<NativeThreadLinux>(this, thread_id); 2385af245d11STodd Fiala m_threads.push_back (thread_sp); 2386af245d11STodd Fiala return thread_sp; 2387af245d11STodd Fiala } 2388af245d11STodd Fiala 2389af245d11STodd Fiala Error 2390b9cc0c75SPavel Labath NativeProcessLinux::FixupBreakpointPCAsNeeded(NativeThreadLinux &thread) 2391af245d11STodd Fiala { 239275f47c3aSTodd Fiala Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); 2393af245d11STodd Fiala 2394af245d11STodd Fiala Error error; 2395af245d11STodd Fiala 2396af245d11STodd Fiala // Find out the size of a breakpoint (might depend on where we are in the code). 2397b9cc0c75SPavel Labath NativeRegisterContextSP context_sp = thread.GetRegisterContext(); 2398af245d11STodd Fiala if (!context_sp) 2399af245d11STodd Fiala { 2400af245d11STodd Fiala error.SetErrorString ("cannot get a NativeRegisterContext for the thread"); 2401af245d11STodd Fiala if (log) 2402af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); 2403af245d11STodd Fiala return error; 2404af245d11STodd Fiala } 2405af245d11STodd Fiala 2406af245d11STodd Fiala uint32_t breakpoint_size = 0; 2407b9cc0c75SPavel Labath error = GetSoftwareBreakpointPCOffset(breakpoint_size); 2408af245d11STodd Fiala if (error.Fail ()) 2409af245d11STodd Fiala { 2410af245d11STodd Fiala if (log) 2411af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s GetBreakpointSize() failed: %s", __FUNCTION__, error.AsCString ()); 2412af245d11STodd Fiala return error; 2413af245d11STodd Fiala } 2414af245d11STodd Fiala else 2415af245d11STodd Fiala { 2416af245d11STodd Fiala if (log) 2417af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s breakpoint size: %" PRIu32, __FUNCTION__, breakpoint_size); 2418af245d11STodd Fiala } 2419af245d11STodd Fiala 2420af245d11STodd Fiala // First try probing for a breakpoint at a software breakpoint location: PC - breakpoint size. 2421c60c9452SJaydeep Patil const lldb::addr_t initial_pc_addr = context_sp->GetPCfromBreakpointLocation (); 2422af245d11STodd Fiala lldb::addr_t breakpoint_addr = initial_pc_addr; 24233eb4b458SChaoren Lin if (breakpoint_size > 0) 2424af245d11STodd Fiala { 2425af245d11STodd Fiala // Do not allow breakpoint probe to wrap around. 24263eb4b458SChaoren Lin if (breakpoint_addr >= breakpoint_size) 24273eb4b458SChaoren Lin breakpoint_addr -= breakpoint_size; 2428af245d11STodd Fiala } 2429af245d11STodd Fiala 2430af245d11STodd Fiala // Check if we stopped because of a breakpoint. 2431af245d11STodd Fiala NativeBreakpointSP breakpoint_sp; 2432af245d11STodd Fiala error = m_breakpoint_list.GetBreakpoint (breakpoint_addr, breakpoint_sp); 2433af245d11STodd Fiala if (!error.Success () || !breakpoint_sp) 2434af245d11STodd Fiala { 2435af245d11STodd Fiala // We didn't find one at a software probe location. Nothing to do. 2436af245d11STodd Fiala if (log) 2437af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " no lldb breakpoint found at current pc with adjustment: 0x%" PRIx64, __FUNCTION__, GetID (), breakpoint_addr); 2438af245d11STodd Fiala return Error (); 2439af245d11STodd Fiala } 2440af245d11STodd Fiala 2441af245d11STodd Fiala // If the breakpoint is not a software breakpoint, nothing to do. 2442af245d11STodd Fiala if (!breakpoint_sp->IsSoftwareBreakpoint ()) 2443af245d11STodd Fiala { 2444af245d11STodd Fiala if (log) 2445af245d11STodd Fiala log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " breakpoint found at 0x%" PRIx64 ", not software, nothing to adjust", __FUNCTION__, GetID (), breakpoint_addr); 2446af245d11STodd Fiala return Error (); 2447af245d11STodd Fiala } 2448af245d11STodd Fiala 2449af245d11STodd Fiala // 2450af245d11STodd Fiala // We have a software breakpoint and need to adjust the PC. 2451af245d11STodd Fiala // 2452af245d11STodd Fiala 2453af245d11STodd Fiala // Sanity check. 2454af245d11STodd Fiala if (breakpoint_size == 0) 2455af245d11STodd Fiala { 2456af245d11STodd Fiala // Nothing to do! How did we get here? 2457af245d11STodd Fiala if (log) 2458af245d11STodd 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); 2459af245d11STodd Fiala return Error (); 2460af245d11STodd Fiala } 2461af245d11STodd Fiala 2462af245d11STodd Fiala // Change the program counter. 2463af245d11STodd Fiala if (log) 2464b9cc0c75SPavel 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); 2465af245d11STodd Fiala 2466af245d11STodd Fiala error = context_sp->SetPC (breakpoint_addr); 2467af245d11STodd Fiala if (error.Fail ()) 2468af245d11STodd Fiala { 2469af245d11STodd Fiala if (log) 2470b9cc0c75SPavel Labath log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": failed to set PC: %s", __FUNCTION__, GetID(), thread.GetID(), error.AsCString ()); 2471af245d11STodd Fiala return error; 2472af245d11STodd Fiala } 2473af245d11STodd Fiala 2474af245d11STodd Fiala return error; 2475af245d11STodd Fiala } 2476fa03ad2eSChaoren Lin 24777cb18bf5STamas Berghammer Error 24787cb18bf5STamas Berghammer NativeProcessLinux::GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) 24797cb18bf5STamas Berghammer { 24807cb18bf5STamas Berghammer FileSpec module_file_spec(module_path, true); 24817cb18bf5STamas Berghammer 2482162fb8e8SPavel Labath bool found = false; 24837cb18bf5STamas Berghammer file_spec.Clear(); 2484162fb8e8SPavel Labath ProcFileReader::ProcessLineByLine(GetID(), "maps", 2485162fb8e8SPavel Labath [&] (const std::string &line) 2486162fb8e8SPavel Labath { 2487162fb8e8SPavel Labath SmallVector<StringRef, 16> columns; 2488162fb8e8SPavel Labath StringRef(line).split(columns, " ", -1, false); 2489162fb8e8SPavel Labath if (columns.size() < 6) 2490162fb8e8SPavel Labath return true; // continue searching 2491162fb8e8SPavel Labath 2492162fb8e8SPavel Labath FileSpec this_file_spec(columns[5].str().c_str(), false); 2493162fb8e8SPavel Labath if (this_file_spec.GetFilename() != module_file_spec.GetFilename()) 2494162fb8e8SPavel Labath return true; // continue searching 2495162fb8e8SPavel Labath 2496162fb8e8SPavel Labath file_spec = this_file_spec; 2497162fb8e8SPavel Labath found = true; 2498162fb8e8SPavel Labath return false; // we are done 2499162fb8e8SPavel Labath }); 2500162fb8e8SPavel Labath 2501162fb8e8SPavel Labath if (! found) 25027cb18bf5STamas Berghammer return Error("Module file (%s) not found in /proc/%" PRIu64 "/maps file!", 25037cb18bf5STamas Berghammer module_file_spec.GetFilename().AsCString(), GetID()); 2504162fb8e8SPavel Labath 2505162fb8e8SPavel Labath return Error(); 25067cb18bf5STamas Berghammer } 2507c076559aSPavel Labath 25085eb721edSPavel Labath Error 2509783bfc8cSTamas Berghammer NativeProcessLinux::GetFileLoadAddress(const llvm::StringRef& file_name, lldb::addr_t& load_addr) 2510783bfc8cSTamas Berghammer { 2511783bfc8cSTamas Berghammer load_addr = LLDB_INVALID_ADDRESS; 2512783bfc8cSTamas Berghammer Error error = ProcFileReader::ProcessLineByLine (GetID (), "maps", 2513783bfc8cSTamas Berghammer [&] (const std::string &line) -> bool 2514783bfc8cSTamas Berghammer { 2515783bfc8cSTamas Berghammer StringRef maps_row(line); 2516783bfc8cSTamas Berghammer 2517783bfc8cSTamas Berghammer SmallVector<StringRef, 16> maps_columns; 2518783bfc8cSTamas Berghammer maps_row.split(maps_columns, StringRef(" "), -1, false); 2519783bfc8cSTamas Berghammer 2520783bfc8cSTamas Berghammer if (maps_columns.size() < 6) 2521783bfc8cSTamas Berghammer { 2522783bfc8cSTamas Berghammer // Return true to continue reading the proc file 2523783bfc8cSTamas Berghammer return true; 2524783bfc8cSTamas Berghammer } 2525783bfc8cSTamas Berghammer 2526783bfc8cSTamas Berghammer if (maps_columns[5] == file_name) 2527783bfc8cSTamas Berghammer { 2528783bfc8cSTamas Berghammer StringExtractor addr_extractor(maps_columns[0].str().c_str()); 2529783bfc8cSTamas Berghammer load_addr = addr_extractor.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); 2530783bfc8cSTamas Berghammer 2531783bfc8cSTamas Berghammer // Return false to stop reading the proc file further 2532783bfc8cSTamas Berghammer return false; 2533783bfc8cSTamas Berghammer } 2534783bfc8cSTamas Berghammer 2535783bfc8cSTamas Berghammer // Return true to continue reading the proc file 2536783bfc8cSTamas Berghammer return true; 2537783bfc8cSTamas Berghammer }); 2538783bfc8cSTamas Berghammer return error; 2539783bfc8cSTamas Berghammer } 2540783bfc8cSTamas Berghammer 2541f9077782SPavel Labath NativeThreadLinuxSP 2542f9077782SPavel Labath NativeProcessLinux::GetThreadByID(lldb::tid_t tid) 2543f9077782SPavel Labath { 2544f9077782SPavel Labath return std::static_pointer_cast<NativeThreadLinux>(NativeProcessProtocol::GetThreadByID(tid)); 2545f9077782SPavel Labath } 2546f9077782SPavel Labath 2547783bfc8cSTamas Berghammer Error 2548b9cc0c75SPavel Labath NativeProcessLinux::ResumeThread(NativeThreadLinux &thread, lldb::StateType state, int signo) 2549c076559aSPavel Labath { 25505eb721edSPavel Labath Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 25515eb721edSPavel Labath 25521dbc6c9cSPavel Labath if (log) 25530e1d729bSPavel Labath log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ")", 2554b9cc0c75SPavel Labath __FUNCTION__, thread.GetID()); 2555c076559aSPavel Labath 2556c076559aSPavel Labath // Before we do the resume below, first check if we have a pending 2557108c325dSPavel Labath // stop notification that is currently waiting for 25580e1d729bSPavel Labath // all threads to stop. This is potentially a buggy situation since 2559c076559aSPavel Labath // we're ostensibly waiting for threads to stop before we send out the 2560c076559aSPavel Labath // pending notification, and here we are resuming one before we send 2561c076559aSPavel Labath // out the pending stop notification. 25620e1d729bSPavel Labath if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID && log) 2563c076559aSPavel Labath { 2564b9cc0c75SPavel 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); 2565c076559aSPavel Labath } 2566c076559aSPavel Labath 2567c076559aSPavel Labath // Request a resume. We expect this to be synchronous and the system 2568c076559aSPavel Labath // to reflect it is running after this completes. 25690e1d729bSPavel Labath switch (state) 2570c076559aSPavel Labath { 25710e1d729bSPavel Labath case eStateRunning: 25720e1d729bSPavel Labath { 2573605b51b8SPavel Labath const auto resume_result = thread.Resume(signo); 25740e1d729bSPavel Labath if (resume_result.Success()) 25750e1d729bSPavel Labath SetState(eStateRunning, true); 25760e1d729bSPavel Labath return resume_result; 2577c076559aSPavel Labath } 25780e1d729bSPavel Labath case eStateStepping: 25790e1d729bSPavel Labath { 2580605b51b8SPavel Labath const auto step_result = thread.SingleStep(signo); 25810e1d729bSPavel Labath if (step_result.Success()) 25820e1d729bSPavel Labath SetState(eStateRunning, true); 25830e1d729bSPavel Labath return step_result; 25840e1d729bSPavel Labath } 25850e1d729bSPavel Labath default: 25860e1d729bSPavel Labath if (log) 25870e1d729bSPavel Labath log->Printf("NativeProcessLinux::%s Unhandled state %s.", 25880e1d729bSPavel Labath __FUNCTION__, StateAsCString(state)); 25890e1d729bSPavel Labath llvm_unreachable("Unhandled state for resume"); 25900e1d729bSPavel Labath } 2591c076559aSPavel Labath } 2592c076559aSPavel Labath 2593c076559aSPavel Labath //===----------------------------------------------------------------------===// 2594c076559aSPavel Labath 2595c076559aSPavel Labath void 2596337f3eb9SPavel Labath NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid) 2597c076559aSPavel Labath { 25985eb721edSPavel Labath Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 2599c076559aSPavel Labath 26005eb721edSPavel Labath if (log) 2601c076559aSPavel Labath { 26025eb721edSPavel Labath log->Printf("NativeProcessLinux::%s about to process event: (triggering_tid: %" PRIu64 ")", 2603c076559aSPavel Labath __FUNCTION__, triggering_tid); 2604c076559aSPavel Labath } 2605c076559aSPavel Labath 26060e1d729bSPavel Labath m_pending_notification_tid = triggering_tid; 26070e1d729bSPavel Labath 26080e1d729bSPavel Labath // Request a stop for all the thread stops that need to be stopped 26090e1d729bSPavel Labath // and are not already known to be stopped. 26100e1d729bSPavel Labath for (const auto &thread_sp: m_threads) 26110e1d729bSPavel Labath { 26120e1d729bSPavel Labath if (StateIsRunningState(thread_sp->GetState())) 26130e1d729bSPavel Labath static_pointer_cast<NativeThreadLinux>(thread_sp)->RequestStop(); 26140e1d729bSPavel Labath } 26150e1d729bSPavel Labath 26160e1d729bSPavel Labath SignalIfAllThreadsStopped(); 2617c076559aSPavel Labath 26185eb721edSPavel Labath if (log) 2619c076559aSPavel Labath { 26205eb721edSPavel Labath log->Printf("NativeProcessLinux::%s event processing done", __FUNCTION__); 2621c076559aSPavel Labath } 2622c076559aSPavel Labath } 2623c076559aSPavel Labath 2624c076559aSPavel Labath void 26259eb1ecb9SPavel Labath NativeProcessLinux::SignalIfAllThreadsStopped() 2626c076559aSPavel Labath { 26270e1d729bSPavel Labath if (m_pending_notification_tid == LLDB_INVALID_THREAD_ID) 26280e1d729bSPavel Labath return; // No pending notification. Nothing to do. 26290e1d729bSPavel Labath 26300e1d729bSPavel Labath for (const auto &thread_sp: m_threads) 2631c076559aSPavel Labath { 26320e1d729bSPavel Labath if (StateIsRunningState(thread_sp->GetState())) 26330e1d729bSPavel Labath return; // Some threads are still running. Don't signal yet. 26340e1d729bSPavel Labath } 26350e1d729bSPavel Labath 26360e1d729bSPavel Labath // We have a pending notification and all threads have stopped. 26379eb1ecb9SPavel Labath Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 26389eb1ecb9SPavel Labath 26399eb1ecb9SPavel Labath // Clear any temporary breakpoints we used to implement software single stepping. 26409eb1ecb9SPavel Labath for (const auto &thread_info: m_threads_stepping_with_breakpoint) 26419eb1ecb9SPavel Labath { 26429eb1ecb9SPavel Labath Error error = RemoveBreakpoint (thread_info.second); 26439eb1ecb9SPavel Labath if (error.Fail()) 26449eb1ecb9SPavel Labath if (log) 26459eb1ecb9SPavel Labath log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " remove stepping breakpoint: %s", 26469eb1ecb9SPavel Labath __FUNCTION__, thread_info.first, error.AsCString()); 26479eb1ecb9SPavel Labath } 26489eb1ecb9SPavel Labath m_threads_stepping_with_breakpoint.clear(); 26499eb1ecb9SPavel Labath 26509eb1ecb9SPavel Labath // Notify the delegate about the stop 26510e1d729bSPavel Labath SetCurrentThreadID(m_pending_notification_tid); 2652ed89c7feSPavel Labath SetState(StateType::eStateStopped, true); 26530e1d729bSPavel Labath m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 2654c076559aSPavel Labath } 2655c076559aSPavel Labath 2656c076559aSPavel Labath void 2657f9077782SPavel Labath NativeProcessLinux::ThreadWasCreated(NativeThreadLinux &thread) 2658c076559aSPavel Labath { 26591dbc6c9cSPavel Labath Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); 26601dbc6c9cSPavel Labath 26611dbc6c9cSPavel Labath if (log) 2662f9077782SPavel Labath log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ")", __FUNCTION__, thread.GetID()); 26631dbc6c9cSPavel Labath 2664f9077782SPavel Labath if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID && StateIsRunningState(thread.GetState())) 2665c076559aSPavel Labath { 2666c076559aSPavel Labath // We will need to wait for this new thread to stop as well before firing the 2667c076559aSPavel Labath // notification. 2668f9077782SPavel Labath thread.RequestStop(); 2669c076559aSPavel Labath } 2670c076559aSPavel Labath } 2671068f8a7eSTamas Berghammer 267219cbe96aSPavel Labath void 267319cbe96aSPavel Labath NativeProcessLinux::SigchldHandler() 2674068f8a7eSTamas Berghammer { 267519cbe96aSPavel Labath Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); 267619cbe96aSPavel Labath // Process all pending waitpid notifications. 267719cbe96aSPavel Labath while (true) 267819cbe96aSPavel Labath { 267919cbe96aSPavel Labath int status = -1; 268019cbe96aSPavel Labath ::pid_t wait_pid = waitpid(-1, &status, __WALL | __WNOTHREAD | WNOHANG); 268119cbe96aSPavel Labath 268219cbe96aSPavel Labath if (wait_pid == 0) 268319cbe96aSPavel Labath break; // We are done. 268419cbe96aSPavel Labath 268519cbe96aSPavel Labath if (wait_pid == -1) 268619cbe96aSPavel Labath { 268719cbe96aSPavel Labath if (errno == EINTR) 268819cbe96aSPavel Labath continue; 268919cbe96aSPavel Labath 269019cbe96aSPavel Labath Error error(errno, eErrorTypePOSIX); 269119cbe96aSPavel Labath if (log) 269219cbe96aSPavel Labath log->Printf("NativeProcessLinux::%s waitpid (-1, &status, __WALL | __WNOTHREAD | WNOHANG) failed: %s", 269319cbe96aSPavel Labath __FUNCTION__, error.AsCString()); 269419cbe96aSPavel Labath break; 269519cbe96aSPavel Labath } 269619cbe96aSPavel Labath 269719cbe96aSPavel Labath bool exited = false; 269819cbe96aSPavel Labath int signal = 0; 269919cbe96aSPavel Labath int exit_status = 0; 270019cbe96aSPavel Labath const char *status_cstr = nullptr; 270119cbe96aSPavel Labath if (WIFSTOPPED(status)) 270219cbe96aSPavel Labath { 270319cbe96aSPavel Labath signal = WSTOPSIG(status); 270419cbe96aSPavel Labath status_cstr = "STOPPED"; 270519cbe96aSPavel Labath } 270619cbe96aSPavel Labath else if (WIFEXITED(status)) 270719cbe96aSPavel Labath { 270819cbe96aSPavel Labath exit_status = WEXITSTATUS(status); 270919cbe96aSPavel Labath status_cstr = "EXITED"; 271019cbe96aSPavel Labath exited = true; 271119cbe96aSPavel Labath } 271219cbe96aSPavel Labath else if (WIFSIGNALED(status)) 271319cbe96aSPavel Labath { 271419cbe96aSPavel Labath signal = WTERMSIG(status); 271519cbe96aSPavel Labath status_cstr = "SIGNALED"; 271619cbe96aSPavel Labath if (wait_pid == static_cast< ::pid_t>(GetID())) { 271719cbe96aSPavel Labath exited = true; 271819cbe96aSPavel Labath exit_status = -1; 271919cbe96aSPavel Labath } 272019cbe96aSPavel Labath } 272119cbe96aSPavel Labath else 272219cbe96aSPavel Labath status_cstr = "(\?\?\?)"; 272319cbe96aSPavel Labath 272419cbe96aSPavel Labath if (log) 272519cbe96aSPavel Labath log->Printf("NativeProcessLinux::%s: waitpid (-1, &status, __WALL | __WNOTHREAD | WNOHANG)" 272619cbe96aSPavel Labath "=> pid = %" PRIi32 ", status = 0x%8.8x (%s), signal = %i, exit_state = %i", 272719cbe96aSPavel Labath __FUNCTION__, wait_pid, status, status_cstr, signal, exit_status); 272819cbe96aSPavel Labath 272919cbe96aSPavel Labath MonitorCallback (wait_pid, exited, signal, exit_status); 273019cbe96aSPavel Labath } 2731068f8a7eSTamas Berghammer } 2732068f8a7eSTamas Berghammer 2733068f8a7eSTamas Berghammer // Wrapper for ptrace to catch errors and log calls. 2734068f8a7eSTamas Berghammer // Note that ptrace sets errno on error because -1 can be a valid result (i.e. for PTRACE_PEEK*) 27354a9babb2SPavel Labath Error 27364a9babb2SPavel Labath NativeProcessLinux::PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, long *result) 2737068f8a7eSTamas Berghammer { 27384a9babb2SPavel Labath Error error; 27394a9babb2SPavel Labath long int ret; 2740068f8a7eSTamas Berghammer 2741068f8a7eSTamas Berghammer Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE)); 2742068f8a7eSTamas Berghammer 2743068f8a7eSTamas Berghammer PtraceDisplayBytes(req, data, data_size); 2744068f8a7eSTamas Berghammer 2745068f8a7eSTamas Berghammer errno = 0; 2746068f8a7eSTamas Berghammer if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) 27474a9babb2SPavel Labath ret = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), *(unsigned int *)addr, data); 2748068f8a7eSTamas Berghammer else 27494a9babb2SPavel Labath ret = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), addr, data); 2750068f8a7eSTamas Berghammer 27514a9babb2SPavel Labath if (ret == -1) 2752068f8a7eSTamas Berghammer error.SetErrorToErrno(); 2753068f8a7eSTamas Berghammer 27544a9babb2SPavel Labath if (result) 27554a9babb2SPavel Labath *result = ret; 27564a9babb2SPavel Labath 2757068f8a7eSTamas Berghammer if (log) 27584a9babb2SPavel Labath log->Printf("ptrace(%d, %" PRIu64 ", %p, %p, %zu)=%lX", req, pid, addr, data, data_size, ret); 2759068f8a7eSTamas Berghammer 2760068f8a7eSTamas Berghammer PtraceDisplayBytes(req, data, data_size); 2761068f8a7eSTamas Berghammer 2762068f8a7eSTamas Berghammer if (log && error.GetError() != 0) 2763068f8a7eSTamas Berghammer { 2764068f8a7eSTamas Berghammer const char* str; 2765068f8a7eSTamas Berghammer switch (error.GetError()) 2766068f8a7eSTamas Berghammer { 2767068f8a7eSTamas Berghammer case ESRCH: str = "ESRCH"; break; 2768068f8a7eSTamas Berghammer case EINVAL: str = "EINVAL"; break; 2769068f8a7eSTamas Berghammer case EBUSY: str = "EBUSY"; break; 2770068f8a7eSTamas Berghammer case EPERM: str = "EPERM"; break; 2771068f8a7eSTamas Berghammer default: str = error.AsCString(); 2772068f8a7eSTamas Berghammer } 2773068f8a7eSTamas Berghammer log->Printf("ptrace() failed; errno=%d (%s)", error.GetError(), str); 2774068f8a7eSTamas Berghammer } 2775068f8a7eSTamas Berghammer 27764a9babb2SPavel Labath return error; 2777068f8a7eSTamas Berghammer } 2778