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 <stdint.h> 15b9c1b51eSKate Stone #include <string.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" 325ad891f7SPavel 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" 365ad891f7SPavel 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 45af245d11STodd Fiala #include "NativeThreadLinux.h" 46b9c1b51eSKate Stone #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" 47af245d11STodd Fiala #include "ProcFileReader.h" 481e209fccSTamas Berghammer #include "Procfs.h" 49cacde7dfSTodd Fiala 50b9c1b51eSKate Stone // System includes - They have to be included after framework includes because 51b9c1b51eSKate Stone // they define some 52d858487eSTamas Berghammer // macros which collide with variable names in other modules 53d858487eSTamas Berghammer #include <linux/unistd.h> 54d858487eSTamas Berghammer #include <sys/socket.h> 558b335671SVince Harron 56df7c6995SPavel Labath #include <sys/syscall.h> 57d858487eSTamas Berghammer #include <sys/types.h> 58d858487eSTamas Berghammer #include <sys/user.h> 59d858487eSTamas Berghammer #include <sys/wait.h> 60d858487eSTamas Berghammer 618b335671SVince Harron #include "lldb/Host/linux/Ptrace.h" 62df7c6995SPavel Labath #include "lldb/Host/linux/Uio.h" 63af245d11STodd Fiala 64af245d11STodd Fiala // Support hardware breakpoints in case it has not been defined 65af245d11STodd Fiala #ifndef TRAP_HWBKPT 66af245d11STodd Fiala #define TRAP_HWBKPT 4 67af245d11STodd Fiala #endif 68af245d11STodd Fiala 697cb18bf5STamas Berghammer using namespace lldb; 707cb18bf5STamas Berghammer using namespace lldb_private; 71db264a6dSTamas Berghammer using namespace lldb_private::process_linux; 727cb18bf5STamas Berghammer using namespace llvm; 737cb18bf5STamas Berghammer 74af245d11STodd Fiala // Private bits we only need internally. 75df7c6995SPavel Labath 76b9c1b51eSKate Stone static bool ProcessVmReadvSupported() { 77df7c6995SPavel Labath static bool is_supported; 78df7c6995SPavel Labath static std::once_flag flag; 79df7c6995SPavel Labath 80df7c6995SPavel Labath std::call_once(flag, [] { 81a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 82df7c6995SPavel Labath 83df7c6995SPavel Labath uint32_t source = 0x47424742; 84df7c6995SPavel Labath uint32_t dest = 0; 85df7c6995SPavel Labath 86df7c6995SPavel Labath struct iovec local, remote; 87df7c6995SPavel Labath remote.iov_base = &source; 88df7c6995SPavel Labath local.iov_base = &dest; 89df7c6995SPavel Labath remote.iov_len = local.iov_len = sizeof source; 90df7c6995SPavel Labath 91b9c1b51eSKate Stone // We shall try if cross-process-memory reads work by attempting to read a 92b9c1b51eSKate Stone // value from our own process. 93df7c6995SPavel Labath ssize_t res = process_vm_readv(getpid(), &local, 1, &remote, 1, 0); 94df7c6995SPavel Labath is_supported = (res == sizeof(source) && source == dest); 95df7c6995SPavel Labath if (is_supported) 96a6321a8eSPavel Labath LLDB_LOG(log, 97a6321a8eSPavel Labath "Detected kernel support for process_vm_readv syscall. " 98a6321a8eSPavel Labath "Fast memory reads enabled."); 99df7c6995SPavel Labath else 100a6321a8eSPavel Labath LLDB_LOG(log, 101a6321a8eSPavel Labath "syscall process_vm_readv failed (error: {0}). Fast memory " 102a6321a8eSPavel Labath "reads disabled.", 103a6321a8eSPavel Labath strerror(errno)); 104df7c6995SPavel Labath }); 105df7c6995SPavel Labath 106df7c6995SPavel Labath return is_supported; 107df7c6995SPavel Labath } 108df7c6995SPavel Labath 109b9c1b51eSKate Stone namespace { 110b9c1b51eSKate Stone void MaybeLogLaunchInfo(const ProcessLaunchInfo &info) { 111a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1124abe5d69SPavel Labath if (!log) 1134abe5d69SPavel Labath return; 1144abe5d69SPavel Labath 1154abe5d69SPavel Labath if (const FileAction *action = info.GetFileActionForFD(STDIN_FILENO)) 116a6321a8eSPavel Labath LLDB_LOG(log, "setting STDIN to '{0}'", action->GetFileSpec()); 1174abe5d69SPavel Labath else 118a6321a8eSPavel Labath LLDB_LOG(log, "leaving STDIN as is"); 1194abe5d69SPavel Labath 1204abe5d69SPavel Labath if (const FileAction *action = info.GetFileActionForFD(STDOUT_FILENO)) 121a6321a8eSPavel Labath LLDB_LOG(log, "setting STDOUT to '{0}'", action->GetFileSpec()); 1224abe5d69SPavel Labath else 123a6321a8eSPavel Labath LLDB_LOG(log, "leaving STDOUT as is"); 1244abe5d69SPavel Labath 1254abe5d69SPavel Labath if (const FileAction *action = info.GetFileActionForFD(STDERR_FILENO)) 126a6321a8eSPavel Labath LLDB_LOG(log, "setting STDERR to '{0}'", action->GetFileSpec()); 1274abe5d69SPavel Labath else 128a6321a8eSPavel Labath LLDB_LOG(log, "leaving STDERR as is"); 1294abe5d69SPavel Labath 1304abe5d69SPavel Labath int i = 0; 131b9c1b51eSKate Stone for (const char **args = info.GetArguments().GetConstArgumentVector(); *args; 132b9c1b51eSKate Stone ++args, ++i) 133a6321a8eSPavel Labath LLDB_LOG(log, "arg {0}: '{1}'", i, *args); 1344abe5d69SPavel Labath } 1354abe5d69SPavel Labath 136b9c1b51eSKate Stone void DisplayBytes(StreamString &s, void *bytes, uint32_t count) { 137af245d11STodd Fiala uint8_t *ptr = (uint8_t *)bytes; 138af245d11STodd Fiala const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count); 139b9c1b51eSKate Stone for (uint32_t i = 0; i < loop_count; i++) { 140af245d11STodd Fiala s.Printf("[%x]", *ptr); 141af245d11STodd Fiala ptr++; 142af245d11STodd Fiala } 143af245d11STodd Fiala } 144af245d11STodd Fiala 145b9c1b51eSKate Stone void PtraceDisplayBytes(int &req, void *data, size_t data_size) { 146a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE | 147a6321a8eSPavel Labath POSIX_LOG_VERBOSE)); 148a6321a8eSPavel Labath if (!log) 149a6321a8eSPavel Labath return; 150af245d11STodd Fiala StreamString buf; 151af245d11STodd Fiala 152b9c1b51eSKate Stone switch (req) { 153b9c1b51eSKate Stone case PTRACE_POKETEXT: { 154af245d11STodd Fiala DisplayBytes(buf, &data, 8); 155a6321a8eSPavel Labath LLDB_LOG(log, "PTRACE_POKETEXT {0}", buf.GetData()); 156af245d11STodd Fiala break; 157af245d11STodd Fiala } 158b9c1b51eSKate Stone case PTRACE_POKEDATA: { 159af245d11STodd Fiala DisplayBytes(buf, &data, 8); 160a6321a8eSPavel Labath LLDB_LOG(log, "PTRACE_POKEDATA {0}", buf.GetData()); 161af245d11STodd Fiala break; 162af245d11STodd Fiala } 163b9c1b51eSKate Stone case PTRACE_POKEUSER: { 164af245d11STodd Fiala DisplayBytes(buf, &data, 8); 165a6321a8eSPavel Labath LLDB_LOG(log, "PTRACE_POKEUSER {0}", buf.GetData()); 166af245d11STodd Fiala break; 167af245d11STodd Fiala } 168b9c1b51eSKate Stone case PTRACE_SETREGS: { 169af245d11STodd Fiala DisplayBytes(buf, data, data_size); 170a6321a8eSPavel Labath LLDB_LOG(log, "PTRACE_SETREGS {0}", buf.GetData()); 171af245d11STodd Fiala break; 172af245d11STodd Fiala } 173b9c1b51eSKate Stone case PTRACE_SETFPREGS: { 174af245d11STodd Fiala DisplayBytes(buf, data, data_size); 175a6321a8eSPavel Labath LLDB_LOG(log, "PTRACE_SETFPREGS {0}", buf.GetData()); 176af245d11STodd Fiala break; 177af245d11STodd Fiala } 178b9c1b51eSKate Stone case PTRACE_SETSIGINFO: { 179af245d11STodd Fiala DisplayBytes(buf, data, sizeof(siginfo_t)); 180a6321a8eSPavel Labath LLDB_LOG(log, "PTRACE_SETSIGINFO {0}", buf.GetData()); 181af245d11STodd Fiala break; 182af245d11STodd Fiala } 183b9c1b51eSKate Stone case PTRACE_SETREGSET: { 184af245d11STodd Fiala // Extract iov_base from data, which is a pointer to the struct IOVEC 185af245d11STodd Fiala DisplayBytes(buf, *(void **)data, data_size); 186a6321a8eSPavel Labath LLDB_LOG(log, "PTRACE_SETREGSET {0}", buf.GetData()); 187af245d11STodd Fiala break; 188af245d11STodd Fiala } 189b9c1b51eSKate Stone default: {} 190af245d11STodd Fiala } 191af245d11STodd Fiala } 192af245d11STodd Fiala 19319cbe96aSPavel Labath static constexpr unsigned k_ptrace_word_size = sizeof(void *); 194b9c1b51eSKate Stone static_assert(sizeof(long) >= k_ptrace_word_size, 195b9c1b51eSKate Stone "Size of long must be larger than ptrace word size"); 1961107b5a5SPavel Labath } // end of anonymous namespace 1971107b5a5SPavel Labath 198bd7cbc5aSPavel Labath // Simple helper function to ensure flags are enabled on the given file 199bd7cbc5aSPavel Labath // descriptor. 200b9c1b51eSKate Stone static Error EnsureFDFlags(int fd, int flags) { 201bd7cbc5aSPavel Labath Error error; 202bd7cbc5aSPavel Labath 203bd7cbc5aSPavel Labath int status = fcntl(fd, F_GETFL); 204b9c1b51eSKate Stone if (status == -1) { 205bd7cbc5aSPavel Labath error.SetErrorToErrno(); 206bd7cbc5aSPavel Labath return error; 207bd7cbc5aSPavel Labath } 208bd7cbc5aSPavel Labath 209b9c1b51eSKate Stone if (fcntl(fd, F_SETFL, status | flags) == -1) { 210bd7cbc5aSPavel Labath error.SetErrorToErrno(); 211bd7cbc5aSPavel Labath return error; 212bd7cbc5aSPavel Labath } 213bd7cbc5aSPavel Labath 214bd7cbc5aSPavel Labath return error; 215bd7cbc5aSPavel Labath } 216bd7cbc5aSPavel Labath 217af245d11STodd Fiala // ----------------------------------------------------------------------------- 218af245d11STodd Fiala // Public Static Methods 219af245d11STodd Fiala // ----------------------------------------------------------------------------- 220af245d11STodd Fiala 221b9c1b51eSKate Stone Error NativeProcessProtocol::Launch( 222db264a6dSTamas Berghammer ProcessLaunchInfo &launch_info, 223b9c1b51eSKate Stone NativeProcessProtocol::NativeDelegate &native_delegate, MainLoop &mainloop, 224b9c1b51eSKate Stone NativeProcessProtocolSP &native_process_sp) { 225a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 226af245d11STodd Fiala 2272a86b555SPavel Labath Error error; 228af245d11STodd Fiala 229af245d11STodd Fiala // Verify the working directory is valid if one was specified. 230d3173f34SChaoren Lin FileSpec working_dir{launch_info.GetWorkingDirectory()}; 231d3173f34SChaoren Lin if (working_dir && 232d3173f34SChaoren Lin (!working_dir.ResolvePath() || 233b9c1b51eSKate Stone working_dir.GetFileType() != FileSpec::eFileTypeDirectory)) { 234d3173f34SChaoren Lin error.SetErrorStringWithFormat("No such file or directory: %s", 235d3173f34SChaoren Lin working_dir.GetCString()); 236af245d11STodd Fiala return error; 237af245d11STodd Fiala } 238af245d11STodd Fiala 239af245d11STodd Fiala // Create the NativeProcessLinux in launch mode. 240af245d11STodd Fiala native_process_sp.reset(new NativeProcessLinux()); 241af245d11STodd Fiala 242b9c1b51eSKate Stone if (!native_process_sp->RegisterNativeDelegate(native_delegate)) { 243af245d11STodd Fiala native_process_sp.reset(); 244af245d11STodd Fiala error.SetErrorStringWithFormat("failed to register the native delegate"); 245af245d11STodd Fiala return error; 246af245d11STodd Fiala } 247af245d11STodd Fiala 248b9c1b51eSKate Stone error = std::static_pointer_cast<NativeProcessLinux>(native_process_sp) 249b9c1b51eSKate Stone ->LaunchInferior(mainloop, launch_info); 250af245d11STodd Fiala 251b9c1b51eSKate Stone if (error.Fail()) { 252af245d11STodd Fiala native_process_sp.reset(); 253a6321a8eSPavel Labath LLDB_LOG(log, "failed to launch process: {0}", error); 254af245d11STodd Fiala return error; 255af245d11STodd Fiala } 256af245d11STodd Fiala 257af245d11STodd Fiala launch_info.SetProcessID(native_process_sp->GetID()); 258af245d11STodd Fiala 259af245d11STodd Fiala return error; 260af245d11STodd Fiala } 261af245d11STodd Fiala 262b9c1b51eSKate Stone Error NativeProcessProtocol::Attach( 263b9c1b51eSKate Stone lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, 264b9c1b51eSKate Stone MainLoop &mainloop, NativeProcessProtocolSP &native_process_sp) { 265a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 266a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0:x}", pid); 267af245d11STodd Fiala 268af245d11STodd Fiala // Retrieve the architecture for the running process. 269af245d11STodd Fiala ArchSpec process_arch; 2702a86b555SPavel Labath Error error = ResolveProcessArchitecture(pid, process_arch); 271af245d11STodd Fiala if (!error.Success()) 272af245d11STodd Fiala return error; 273af245d11STodd Fiala 274b9c1b51eSKate Stone std::shared_ptr<NativeProcessLinux> native_process_linux_sp( 275b9c1b51eSKate Stone new NativeProcessLinux()); 276af245d11STodd Fiala 277b9c1b51eSKate Stone if (!native_process_linux_sp->RegisterNativeDelegate(native_delegate)) { 278af245d11STodd Fiala error.SetErrorStringWithFormat("failed to register the native delegate"); 279af245d11STodd Fiala return error; 280af245d11STodd Fiala } 281af245d11STodd Fiala 28219cbe96aSPavel Labath native_process_linux_sp->AttachToInferior(mainloop, pid, error); 283af245d11STodd Fiala if (!error.Success()) 284af245d11STodd Fiala return error; 285af245d11STodd Fiala 2861339b5e8SOleksiy Vyalov native_process_sp = native_process_linux_sp; 287af245d11STodd Fiala return error; 288af245d11STodd Fiala } 289af245d11STodd Fiala 290af245d11STodd Fiala // ----------------------------------------------------------------------------- 291af245d11STodd Fiala // Public Instance Methods 292af245d11STodd Fiala // ----------------------------------------------------------------------------- 293af245d11STodd Fiala 294b9c1b51eSKate Stone NativeProcessLinux::NativeProcessLinux() 295b9c1b51eSKate Stone : NativeProcessProtocol(LLDB_INVALID_PROCESS_ID), m_arch(), 296b9c1b51eSKate Stone m_supports_mem_region(eLazyBoolCalculate), m_mem_region_cache(), 297b9c1b51eSKate Stone m_pending_notification_tid(LLDB_INVALID_THREAD_ID) {} 298af245d11STodd Fiala 299b9c1b51eSKate Stone void NativeProcessLinux::AttachToInferior(MainLoop &mainloop, lldb::pid_t pid, 300b9c1b51eSKate Stone Error &error) { 301a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 302a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0:x}", pid); 303af245d11STodd Fiala 304b9c1b51eSKate Stone m_sigchld_handle = mainloop.RegisterSignal( 305b9c1b51eSKate Stone SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, error); 30619cbe96aSPavel Labath if (!m_sigchld_handle) 30719cbe96aSPavel Labath return; 30819cbe96aSPavel Labath 3092a86b555SPavel Labath error = ResolveProcessArchitecture(pid, m_arch); 310af245d11STodd Fiala if (!error.Success()) 311af245d11STodd Fiala return; 312af245d11STodd Fiala 313af245d11STodd Fiala // Set the architecture to the exe architecture. 314a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid, 315a6321a8eSPavel Labath m_arch.GetArchitectureName()); 316af245d11STodd Fiala m_pid = pid; 317af245d11STodd Fiala SetState(eStateAttaching); 318af245d11STodd Fiala 31919cbe96aSPavel Labath Attach(pid, error); 320af245d11STodd Fiala } 321af245d11STodd Fiala 322b9c1b51eSKate Stone Error NativeProcessLinux::LaunchInferior(MainLoop &mainloop, 323b9c1b51eSKate Stone ProcessLaunchInfo &launch_info) { 3244abe5d69SPavel Labath Error error; 325b9c1b51eSKate Stone m_sigchld_handle = mainloop.RegisterSignal( 326b9c1b51eSKate Stone SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, error); 3274abe5d69SPavel Labath if (!m_sigchld_handle) 3284abe5d69SPavel Labath return error; 3294abe5d69SPavel Labath 3304abe5d69SPavel Labath SetState(eStateLaunching); 3310c4f01d4SPavel Labath 3324abe5d69SPavel Labath MaybeLogLaunchInfo(launch_info); 3334abe5d69SPavel Labath 334b9c1b51eSKate Stone ::pid_t pid = 335b9c1b51eSKate Stone ProcessLauncherLinux().LaunchProcess(launch_info, error).GetProcessId(); 3365ad891f7SPavel Labath if (error.Fail()) 3374abe5d69SPavel Labath return error; 3380c4f01d4SPavel Labath 339a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 34075f47c3aSTodd Fiala 341af245d11STodd Fiala // Wait for the child process to trap on its call to execve. 342af245d11STodd Fiala ::pid_t wpid; 343af245d11STodd Fiala int status; 344b9c1b51eSKate Stone if ((wpid = waitpid(pid, &status, 0)) < 0) { 345bd7cbc5aSPavel Labath error.SetErrorToErrno(); 346a6321a8eSPavel Labath LLDB_LOG(log, "waitpid for inferior failed with %s", error); 347af245d11STodd Fiala 348af245d11STodd Fiala // Mark the inferior as invalid. 349b9c1b51eSKate Stone // FIXME this could really use a new state - eStateLaunchFailure. For now, 350b9c1b51eSKate Stone // using eStateInvalid. 351bd7cbc5aSPavel Labath SetState(StateType::eStateInvalid); 352af245d11STodd Fiala 3534abe5d69SPavel Labath return error; 354af245d11STodd Fiala } 355af245d11STodd Fiala assert(WIFSTOPPED(status) && (wpid == static_cast<::pid_t>(pid)) && 356af245d11STodd Fiala "Could not sync with inferior process."); 357af245d11STodd Fiala 358a6321a8eSPavel Labath LLDB_LOG(log, "inferior started, now in stopped state"); 359bd7cbc5aSPavel Labath error = SetDefaultPtraceOpts(pid); 360b9c1b51eSKate Stone if (error.Fail()) { 361a6321a8eSPavel Labath LLDB_LOG(log, "failed to set default ptrace options: {0}", error); 362af245d11STodd Fiala 363af245d11STodd Fiala // Mark the inferior as invalid. 364b9c1b51eSKate Stone // FIXME this could really use a new state - eStateLaunchFailure. For now, 365b9c1b51eSKate Stone // using eStateInvalid. 366bd7cbc5aSPavel Labath SetState(StateType::eStateInvalid); 367af245d11STodd Fiala 3684abe5d69SPavel Labath return error; 369af245d11STodd Fiala } 370af245d11STodd Fiala 371af245d11STodd Fiala // Release the master terminal descriptor and pass it off to the 372af245d11STodd Fiala // NativeProcessLinux instance. Similarly stash the inferior pid. 3735ad891f7SPavel Labath m_terminal_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor(); 374bd7cbc5aSPavel Labath m_pid = pid; 3754abe5d69SPavel Labath launch_info.SetProcessID(pid); 376af245d11STodd Fiala 377b9c1b51eSKate Stone if (m_terminal_fd != -1) { 378bd7cbc5aSPavel Labath error = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); 379b9c1b51eSKate Stone if (error.Fail()) { 380a6321a8eSPavel Labath LLDB_LOG(log, 381a6321a8eSPavel Labath "inferior EnsureFDFlags failed for ensuring terminal " 382a6321a8eSPavel Labath "O_NONBLOCK setting: {0}", 383a6321a8eSPavel Labath error); 384af245d11STodd Fiala 385af245d11STodd Fiala // Mark the inferior as invalid. 386b9c1b51eSKate Stone // FIXME this could really use a new state - eStateLaunchFailure. For 387b9c1b51eSKate Stone // now, using eStateInvalid. 388bd7cbc5aSPavel Labath SetState(StateType::eStateInvalid); 389af245d11STodd Fiala 3904abe5d69SPavel Labath return error; 391af245d11STodd Fiala } 3925ad891f7SPavel Labath } 393af245d11STodd Fiala 394a6321a8eSPavel Labath LLDB_LOG(log, "adding pid = {0}", pid); 3952a86b555SPavel Labath ResolveProcessArchitecture(m_pid, m_arch); 396f9077782SPavel Labath NativeThreadLinuxSP thread_sp = AddThread(pid); 397af245d11STodd Fiala assert(thread_sp && "AddThread() returned a nullptr thread"); 398f9077782SPavel Labath thread_sp->SetStoppedBySignal(SIGSTOP); 399f9077782SPavel Labath ThreadWasCreated(*thread_sp); 400af245d11STodd Fiala 401af245d11STodd Fiala // Let our process instance know the thread has stopped. 402bd7cbc5aSPavel Labath SetCurrentThreadID(thread_sp->GetID()); 403bd7cbc5aSPavel Labath SetState(StateType::eStateStopped); 404af245d11STodd Fiala 405a6321a8eSPavel Labath if (error.Fail()) 406a6321a8eSPavel Labath LLDB_LOG(log, "inferior launching failed {0}", error); 4074abe5d69SPavel Labath return error; 408af245d11STodd Fiala } 409af245d11STodd Fiala 410b9c1b51eSKate Stone ::pid_t NativeProcessLinux::Attach(lldb::pid_t pid, Error &error) { 411a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 412af245d11STodd Fiala 413b9c1b51eSKate Stone // Use a map to keep track of the threads which we have attached/need to 414b9c1b51eSKate Stone // attach. 415af245d11STodd Fiala Host::TidMap tids_to_attach; 416b9c1b51eSKate Stone if (pid <= 1) { 417bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 418bd7cbc5aSPavel Labath error.SetErrorString("Attaching to process 1 is not allowed."); 419bd7cbc5aSPavel Labath return -1; 420af245d11STodd Fiala } 421af245d11STodd Fiala 422b9c1b51eSKate Stone while (Host::FindProcessThreads(pid, tids_to_attach)) { 423af245d11STodd Fiala for (Host::TidMap::iterator it = tids_to_attach.begin(); 424b9c1b51eSKate Stone it != tids_to_attach.end();) { 425b9c1b51eSKate Stone if (it->second == false) { 426af245d11STodd Fiala lldb::tid_t tid = it->first; 427af245d11STodd Fiala 428af245d11STodd Fiala // Attach to the requested process. 429af245d11STodd Fiala // An attach will cause the thread to stop with a SIGSTOP. 4304a9babb2SPavel Labath error = PtraceWrapper(PTRACE_ATTACH, tid); 431b9c1b51eSKate Stone if (error.Fail()) { 432af245d11STodd Fiala // No such thread. The thread may have exited. 433af245d11STodd Fiala // More error handling may be needed. 434b9c1b51eSKate Stone if (error.GetError() == ESRCH) { 435af245d11STodd Fiala it = tids_to_attach.erase(it); 436af245d11STodd Fiala continue; 437b9c1b51eSKate Stone } else 438bd7cbc5aSPavel Labath return -1; 439af245d11STodd Fiala } 440af245d11STodd Fiala 441af245d11STodd Fiala int status; 442af245d11STodd Fiala // Need to use __WALL otherwise we receive an error with errno=ECHLD 443af245d11STodd Fiala // At this point we should have a thread stopped if waitpid succeeds. 444b9c1b51eSKate Stone if ((status = waitpid(tid, NULL, __WALL)) < 0) { 445af245d11STodd Fiala // No such thread. The thread may have exited. 446af245d11STodd Fiala // More error handling may be needed. 447b9c1b51eSKate Stone if (errno == ESRCH) { 448af245d11STodd Fiala it = tids_to_attach.erase(it); 449af245d11STodd Fiala continue; 450b9c1b51eSKate Stone } else { 451bd7cbc5aSPavel Labath error.SetErrorToErrno(); 452bd7cbc5aSPavel Labath return -1; 453af245d11STodd Fiala } 454af245d11STodd Fiala } 455af245d11STodd Fiala 456bd7cbc5aSPavel Labath error = SetDefaultPtraceOpts(tid); 457bd7cbc5aSPavel Labath if (error.Fail()) 458bd7cbc5aSPavel Labath return -1; 459af245d11STodd Fiala 460a6321a8eSPavel Labath LLDB_LOG(log, "adding tid = {0}", tid); 461af245d11STodd Fiala it->second = true; 462af245d11STodd Fiala 463af245d11STodd Fiala // Create the thread, mark it as stopped. 464f9077782SPavel Labath NativeThreadLinuxSP thread_sp(AddThread(static_cast<lldb::tid_t>(tid))); 465af245d11STodd Fiala assert(thread_sp && "AddThread() returned a nullptr"); 466fa03ad2eSChaoren Lin 467b9c1b51eSKate Stone // This will notify this is a new thread and tell the system it is 468b9c1b51eSKate Stone // stopped. 469f9077782SPavel Labath thread_sp->SetStoppedBySignal(SIGSTOP); 470f9077782SPavel Labath ThreadWasCreated(*thread_sp); 471bd7cbc5aSPavel Labath SetCurrentThreadID(thread_sp->GetID()); 472af245d11STodd Fiala } 473af245d11STodd Fiala 474af245d11STodd Fiala // move the loop forward 475af245d11STodd Fiala ++it; 476af245d11STodd Fiala } 477af245d11STodd Fiala } 478af245d11STodd Fiala 479b9c1b51eSKate Stone if (tids_to_attach.size() > 0) { 480bd7cbc5aSPavel Labath m_pid = pid; 481af245d11STodd Fiala // Let our process instance know the thread has stopped. 482bd7cbc5aSPavel Labath SetState(StateType::eStateStopped); 483b9c1b51eSKate Stone } else { 484bd7cbc5aSPavel Labath error.SetErrorToGenericError(); 485bd7cbc5aSPavel Labath error.SetErrorString("No such process."); 486bd7cbc5aSPavel Labath return -1; 487af245d11STodd Fiala } 488af245d11STodd Fiala 489bd7cbc5aSPavel Labath return pid; 490af245d11STodd Fiala } 491af245d11STodd Fiala 492b9c1b51eSKate Stone Error NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) { 493af245d11STodd Fiala long ptrace_opts = 0; 494af245d11STodd Fiala 495af245d11STodd Fiala // Have the child raise an event on exit. This is used to keep the child in 496af245d11STodd Fiala // limbo until it is destroyed. 497af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXIT; 498af245d11STodd Fiala 499af245d11STodd Fiala // Have the tracer trace threads which spawn in the inferior process. 500af245d11STodd Fiala // TODO: if we want to support tracing the inferiors' child, add the 501af245d11STodd Fiala // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK) 502af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACECLONE; 503af245d11STodd Fiala 504af245d11STodd Fiala // Have the tracer notify us before execve returns 505af245d11STodd Fiala // (needed to disable legacy SIGTRAP generation) 506af245d11STodd Fiala ptrace_opts |= PTRACE_O_TRACEEXEC; 507af245d11STodd Fiala 5084a9babb2SPavel Labath return PtraceWrapper(PTRACE_SETOPTIONS, pid, nullptr, (void *)ptrace_opts); 509af245d11STodd Fiala } 510af245d11STodd Fiala 511b9c1b51eSKate Stone static ExitType convert_pid_status_to_exit_type(int status) { 512af245d11STodd Fiala if (WIFEXITED(status)) 513af245d11STodd Fiala return ExitType::eExitTypeExit; 514af245d11STodd Fiala else if (WIFSIGNALED(status)) 515af245d11STodd Fiala return ExitType::eExitTypeSignal; 516af245d11STodd Fiala else if (WIFSTOPPED(status)) 517af245d11STodd Fiala return ExitType::eExitTypeStop; 518b9c1b51eSKate Stone else { 519af245d11STodd Fiala // We don't know what this is. 520af245d11STodd Fiala return ExitType::eExitTypeInvalid; 521af245d11STodd Fiala } 522af245d11STodd Fiala } 523af245d11STodd Fiala 524b9c1b51eSKate Stone static int convert_pid_status_to_return_code(int status) { 525af245d11STodd Fiala if (WIFEXITED(status)) 526af245d11STodd Fiala return WEXITSTATUS(status); 527af245d11STodd Fiala else if (WIFSIGNALED(status)) 528af245d11STodd Fiala return WTERMSIG(status); 529af245d11STodd Fiala else if (WIFSTOPPED(status)) 530af245d11STodd Fiala return WSTOPSIG(status); 531b9c1b51eSKate Stone else { 532af245d11STodd Fiala // We don't know what this is. 533af245d11STodd Fiala return ExitType::eExitTypeInvalid; 534af245d11STodd Fiala } 535af245d11STodd Fiala } 536af245d11STodd Fiala 5371107b5a5SPavel Labath // Handles all waitpid events from the inferior process. 538b9c1b51eSKate Stone void NativeProcessLinux::MonitorCallback(lldb::pid_t pid, bool exited, 539b9c1b51eSKate Stone int signal, int status) { 540af245d11STodd Fiala Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 541af245d11STodd Fiala 542b9c1b51eSKate Stone // Certain activities differ based on whether the pid is the tid of the main 543b9c1b51eSKate Stone // thread. 5441107b5a5SPavel Labath const bool is_main_thread = (pid == GetID()); 545af245d11STodd Fiala 546af245d11STodd Fiala // Handle when the thread exits. 547b9c1b51eSKate Stone if (exited) { 548a6321a8eSPavel Labath LLDB_LOG(log, "got exit signal({0}) , tid = {1} ({2} main thread)", signal, 549a6321a8eSPavel Labath pid, is_main_thread ? "is" : "is not"); 550af245d11STodd Fiala 551af245d11STodd Fiala // This is a thread that exited. Ensure we're not tracking it anymore. 5521107b5a5SPavel Labath const bool thread_found = StopTrackingThread(pid); 553af245d11STodd Fiala 554b9c1b51eSKate Stone if (is_main_thread) { 555b9c1b51eSKate Stone // We only set the exit status and notify the delegate if we haven't 556b9c1b51eSKate Stone // already set the process 557b9c1b51eSKate Stone // state to an exited state. We normally should have received a SIGTRAP | 558b9c1b51eSKate Stone // (PTRACE_EVENT_EXIT << 8) 559af245d11STodd Fiala // for the main thread. 560b9c1b51eSKate Stone const bool already_notified = (GetState() == StateType::eStateExited) || 561b9c1b51eSKate Stone (GetState() == StateType::eStateCrashed); 562b9c1b51eSKate Stone if (!already_notified) { 563a6321a8eSPavel Labath LLDB_LOG( 564a6321a8eSPavel Labath log, 565a6321a8eSPavel Labath "tid = {0} handling main thread exit ({1}), expected exit state " 566a6321a8eSPavel Labath "already set but state was {2} instead, setting exit state now", 567a6321a8eSPavel Labath pid, 568b9c1b51eSKate Stone thread_found ? "stopped tracking thread metadata" 569b9c1b51eSKate Stone : "thread metadata not found", 570b9c1b51eSKate Stone StateAsCString(GetState())); 571af245d11STodd Fiala // The main thread exited. We're done monitoring. Report to delegate. 572b9c1b51eSKate Stone SetExitStatus(convert_pid_status_to_exit_type(status), 573b9c1b51eSKate Stone convert_pid_status_to_return_code(status), nullptr, true); 574af245d11STodd Fiala 575af245d11STodd Fiala // Notify delegate that our process has exited. 5761107b5a5SPavel Labath SetState(StateType::eStateExited, true); 577a6321a8eSPavel Labath } else 578a6321a8eSPavel Labath LLDB_LOG(log, "tid = {0} main thread now exited (%s)", pid, 579b9c1b51eSKate Stone thread_found ? "stopped tracking thread metadata" 580b9c1b51eSKate Stone : "thread metadata not found"); 581b9c1b51eSKate Stone } else { 582b9c1b51eSKate Stone // Do we want to report to the delegate in this case? I think not. If 583a6321a8eSPavel Labath // this was an orderly thread exit, we would already have received the 584a6321a8eSPavel Labath // SIGTRAP | (PTRACE_EVENT_EXIT << 8) signal, and we would have done an 585a6321a8eSPavel Labath // all-stop then. 586a6321a8eSPavel Labath LLDB_LOG(log, "tid = {0} handling non-main thread exit (%s)", pid, 587b9c1b51eSKate Stone thread_found ? "stopped tracking thread metadata" 588b9c1b51eSKate Stone : "thread metadata not found"); 589af245d11STodd Fiala } 5901107b5a5SPavel Labath return; 591af245d11STodd Fiala } 592af245d11STodd Fiala 593af245d11STodd Fiala siginfo_t info; 594b9cc0c75SPavel Labath const auto info_err = GetSignalInfo(pid, &info); 595b9cc0c75SPavel Labath auto thread_sp = GetThreadByID(pid); 596b9cc0c75SPavel Labath 597b9c1b51eSKate Stone if (!thread_sp) { 598b9c1b51eSKate Stone // Normally, the only situation when we cannot find the thread is if we have 599a6321a8eSPavel Labath // just received a new thread notification. This is indicated by 600a6321a8eSPavel Labath // GetSignalInfo() returning si_code == SI_USER and si_pid == 0 601a6321a8eSPavel Labath LLDB_LOG(log, "received notification about an unknown tid {0}.", pid); 602b9cc0c75SPavel Labath 603b9c1b51eSKate Stone if (info_err.Fail()) { 604a6321a8eSPavel Labath LLDB_LOG(log, 605a6321a8eSPavel Labath "(tid {0}) GetSignalInfo failed ({1}). " 606a6321a8eSPavel Labath "Ingoring this notification.", 607a6321a8eSPavel Labath pid, info_err); 608b9cc0c75SPavel Labath return; 609b9cc0c75SPavel Labath } 610b9cc0c75SPavel Labath 611a6321a8eSPavel Labath LLDB_LOG(log, "tid {0}, si_code: {1}, si_pid: {2}", pid, info.si_code, 612a6321a8eSPavel Labath info.si_pid); 613b9cc0c75SPavel Labath 614b9cc0c75SPavel Labath auto thread_sp = AddThread(pid); 615b9cc0c75SPavel Labath // Resume the newly created thread. 616b9cc0c75SPavel Labath ResumeThread(*thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); 617b9cc0c75SPavel Labath ThreadWasCreated(*thread_sp); 618b9cc0c75SPavel Labath return; 619b9cc0c75SPavel Labath } 620b9cc0c75SPavel Labath 621b9cc0c75SPavel Labath // Get details on the signal raised. 622b9c1b51eSKate Stone if (info_err.Success()) { 623fa03ad2eSChaoren Lin // We have retrieved the signal info. Dispatch appropriately. 624fa03ad2eSChaoren Lin if (info.si_signo == SIGTRAP) 625b9cc0c75SPavel Labath MonitorSIGTRAP(info, *thread_sp); 626fa03ad2eSChaoren Lin else 627b9cc0c75SPavel Labath MonitorSignal(info, *thread_sp, exited); 628b9c1b51eSKate Stone } else { 629b9c1b51eSKate Stone if (info_err.GetError() == EINVAL) { 630fa03ad2eSChaoren Lin // This is a group stop reception for this tid. 631b9c1b51eSKate Stone // We can reach here if we reinject SIGSTOP, SIGSTP, SIGTTIN or SIGTTOU 632a6321a8eSPavel Labath // into the tracee, triggering the group-stop mechanism. Normally 633a6321a8eSPavel Labath // receiving these would stop the process, pending a SIGCONT. Simulating 634a6321a8eSPavel Labath // this state in a debugger is hard and is generally not needed (one use 635a6321a8eSPavel Labath // case is debugging background task being managed by a shell). For 636a6321a8eSPavel Labath // general use, it is sufficient to stop the process in a signal-delivery 637b9c1b51eSKate Stone // stop which happens before the group stop. This done by MonitorSignal 638a6321a8eSPavel Labath // and works correctly for all signals. 639a6321a8eSPavel Labath LLDB_LOG(log, 640a6321a8eSPavel Labath "received a group stop for pid {0} tid {1}. Transparent " 641a6321a8eSPavel Labath "handling of group stops not supported, resuming the " 642a6321a8eSPavel Labath "thread.", 643a6321a8eSPavel Labath GetID(), pid); 644b9c1b51eSKate Stone ResumeThread(*thread_sp, thread_sp->GetState(), 645b9c1b51eSKate Stone LLDB_INVALID_SIGNAL_NUMBER); 646b9c1b51eSKate Stone } else { 647af245d11STodd Fiala // ptrace(GETSIGINFO) failed (but not due to group-stop). 648af245d11STodd Fiala 649b9c1b51eSKate Stone // A return value of ESRCH means the thread/process is no longer on the 650a6321a8eSPavel Labath // system, so it was killed somehow outside of our control. Either way, 651a6321a8eSPavel Labath // we can't do anything with it anymore. 652af245d11STodd Fiala 653b9c1b51eSKate Stone // Stop tracking the metadata for the thread since it's entirely off the 654b9c1b51eSKate Stone // system now. 6551107b5a5SPavel Labath const bool thread_found = StopTrackingThread(pid); 656af245d11STodd Fiala 657a6321a8eSPavel Labath LLDB_LOG(log, 658a6321a8eSPavel Labath "GetSignalInfo failed: {0}, tid = {1}, signal = {2}, " 659a6321a8eSPavel Labath "status = {3}, main_thread = {4}, thread_found: {5}", 660a6321a8eSPavel Labath info_err, pid, signal, status, is_main_thread, thread_found); 661af245d11STodd Fiala 662b9c1b51eSKate Stone if (is_main_thread) { 663b9c1b51eSKate Stone // Notify the delegate - our process is not available but appears to 664b9c1b51eSKate Stone // have been killed outside 665af245d11STodd Fiala // our control. Is eStateExited the right exit state in this case? 666b9c1b51eSKate Stone SetExitStatus(convert_pid_status_to_exit_type(status), 667b9c1b51eSKate Stone convert_pid_status_to_return_code(status), nullptr, true); 6681107b5a5SPavel Labath SetState(StateType::eStateExited, true); 669b9c1b51eSKate Stone } else { 670b9c1b51eSKate Stone // This thread was pulled out from underneath us. Anything to do here? 671b9c1b51eSKate Stone // Do we want to do an all stop? 672a6321a8eSPavel Labath LLDB_LOG(log, 673a6321a8eSPavel Labath "pid {0} tid {1} non-main thread exit occurred, didn't " 674a6321a8eSPavel Labath "tell delegate anything since thread disappeared out " 675a6321a8eSPavel Labath "from underneath us", 676a6321a8eSPavel Labath GetID(), pid); 677af245d11STodd Fiala } 678af245d11STodd Fiala } 679af245d11STodd Fiala } 680af245d11STodd Fiala } 681af245d11STodd Fiala 682b9c1b51eSKate Stone void NativeProcessLinux::WaitForNewThread(::pid_t tid) { 683a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 684426bdf88SPavel Labath 685f9077782SPavel Labath NativeThreadLinuxSP new_thread_sp = GetThreadByID(tid); 686426bdf88SPavel Labath 687b9c1b51eSKate Stone if (new_thread_sp) { 688b9c1b51eSKate Stone // We are already tracking the thread - we got the event on the new thread 689b9c1b51eSKate Stone // (see 690426bdf88SPavel Labath // MonitorSignal) before this one. We are done. 691426bdf88SPavel Labath return; 692426bdf88SPavel Labath } 693426bdf88SPavel Labath 694426bdf88SPavel Labath // The thread is not tracked yet, let's wait for it to appear. 695426bdf88SPavel Labath int status = -1; 696426bdf88SPavel Labath ::pid_t wait_pid; 697b9c1b51eSKate Stone do { 698a6321a8eSPavel Labath LLDB_LOG(log, 699a6321a8eSPavel Labath "received thread creation event for tid {0}. tid not tracked " 700a6321a8eSPavel Labath "yet, waiting for thread to appear...", 701a6321a8eSPavel Labath tid); 702426bdf88SPavel Labath wait_pid = waitpid(tid, &status, __WALL); 703b9c1b51eSKate Stone } while (wait_pid == -1 && errno == EINTR); 704b9c1b51eSKate Stone // Since we are waiting on a specific tid, this must be the creation event. 705a6321a8eSPavel Labath // But let's do some checks just in case. 706426bdf88SPavel Labath if (wait_pid != tid) { 707a6321a8eSPavel Labath LLDB_LOG(log, 708a6321a8eSPavel Labath "waiting for tid {0} failed. Assuming the thread has " 709a6321a8eSPavel Labath "disappeared in the meantime", 710a6321a8eSPavel Labath tid); 711426bdf88SPavel Labath // The only way I know of this could happen is if the whole process was 712b9c1b51eSKate Stone // SIGKILLed in the mean time. In any case, we can't do anything about that 713b9c1b51eSKate Stone // now. 714426bdf88SPavel Labath return; 715426bdf88SPavel Labath } 716b9c1b51eSKate Stone if (WIFEXITED(status)) { 717a6321a8eSPavel Labath LLDB_LOG(log, 718a6321a8eSPavel Labath "waiting for tid {0} returned an 'exited' event. Not " 719a6321a8eSPavel Labath "tracking the thread.", 720a6321a8eSPavel Labath tid); 721426bdf88SPavel Labath // Also a very improbable event. 722426bdf88SPavel Labath return; 723426bdf88SPavel Labath } 724426bdf88SPavel Labath 725a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0}: tracking new thread tid {1}", GetID(), tid); 726f9077782SPavel Labath new_thread_sp = AddThread(tid); 727b9cc0c75SPavel Labath ResumeThread(*new_thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); 728f9077782SPavel Labath ThreadWasCreated(*new_thread_sp); 729426bdf88SPavel Labath } 730426bdf88SPavel Labath 731b9c1b51eSKate Stone void NativeProcessLinux::MonitorSIGTRAP(const siginfo_t &info, 732b9c1b51eSKate Stone NativeThreadLinux &thread) { 733a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 734b9cc0c75SPavel Labath const bool is_main_thread = (thread.GetID() == GetID()); 735af245d11STodd Fiala 736b9cc0c75SPavel Labath assert(info.si_signo == SIGTRAP && "Unexpected child signal!"); 737af245d11STodd Fiala 738b9c1b51eSKate Stone switch (info.si_code) { 739b9c1b51eSKate Stone // TODO: these two cases are required if we want to support tracing of the 740b9c1b51eSKate Stone // inferiors' children. We'd need this to debug a monitor. 741af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)): 742af245d11STodd Fiala // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)): 743af245d11STodd Fiala 744b9c1b51eSKate Stone case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)): { 745b9c1b51eSKate Stone // This is the notification on the parent thread which informs us of new 746b9c1b51eSKate Stone // thread 747426bdf88SPavel Labath // creation. 748b9c1b51eSKate Stone // We don't want to do anything with the parent thread so we just resume it. 749b9c1b51eSKate Stone // In case we 750b9c1b51eSKate Stone // want to implement "break on thread creation" functionality, we would need 751b9c1b51eSKate Stone // to stop 752426bdf88SPavel Labath // here. 753af245d11STodd Fiala 754af245d11STodd Fiala unsigned long event_message = 0; 755b9c1b51eSKate Stone if (GetEventMessage(thread.GetID(), &event_message).Fail()) { 756a6321a8eSPavel Labath LLDB_LOG(log, 757a6321a8eSPavel Labath "pid {0} received thread creation event but " 758a6321a8eSPavel Labath "GetEventMessage failed so we don't know the new tid", 759a6321a8eSPavel Labath thread.GetID()); 760426bdf88SPavel Labath } else 761426bdf88SPavel Labath WaitForNewThread(event_message); 762af245d11STodd Fiala 763b9cc0c75SPavel Labath ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); 764af245d11STodd Fiala break; 765af245d11STodd Fiala } 766af245d11STodd Fiala 767b9c1b51eSKate Stone case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): { 768f9077782SPavel Labath NativeThreadLinuxSP main_thread_sp; 769a6321a8eSPavel Labath LLDB_LOG(log, "received exec event, code = {0}", info.si_code ^ SIGTRAP); 770a9882ceeSTodd Fiala 7711dbc6c9cSPavel Labath // Exec clears any pending notifications. 7720e1d729bSPavel Labath m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 773fa03ad2eSChaoren Lin 774b9c1b51eSKate Stone // Remove all but the main thread here. Linux fork creates a new process 775b9c1b51eSKate Stone // which only copies the main thread. 776a6321a8eSPavel Labath LLDB_LOG(log, "exec received, stop tracking all but main thread"); 777a9882ceeSTodd Fiala 778b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 779a9882ceeSTodd Fiala const bool is_main_thread = thread_sp && thread_sp->GetID() == GetID(); 780b9c1b51eSKate Stone if (is_main_thread) { 781f9077782SPavel Labath main_thread_sp = std::static_pointer_cast<NativeThreadLinux>(thread_sp); 782a6321a8eSPavel Labath LLDB_LOG(log, "found main thread with tid {0}, keeping", 783a6321a8eSPavel Labath main_thread_sp->GetID()); 784b9c1b51eSKate Stone } else { 785a6321a8eSPavel Labath LLDB_LOG(log, "discarding non-main-thread tid {0} due to exec", 786a6321a8eSPavel Labath thread_sp->GetID()); 787a9882ceeSTodd Fiala } 788a9882ceeSTodd Fiala } 789a9882ceeSTodd Fiala 790a9882ceeSTodd Fiala m_threads.clear(); 791a9882ceeSTodd Fiala 792b9c1b51eSKate Stone if (main_thread_sp) { 793a9882ceeSTodd Fiala m_threads.push_back(main_thread_sp); 794a9882ceeSTodd Fiala SetCurrentThreadID(main_thread_sp->GetID()); 795f9077782SPavel Labath main_thread_sp->SetStoppedByExec(); 796b9c1b51eSKate Stone } else { 797a9882ceeSTodd Fiala SetCurrentThreadID(LLDB_INVALID_THREAD_ID); 798a6321a8eSPavel Labath LLDB_LOG(log, 799a6321a8eSPavel Labath "pid {0} no main thread found, discarded all threads, " 800a6321a8eSPavel Labath "we're in a no-thread state!", 801a6321a8eSPavel Labath GetID()); 802a9882ceeSTodd Fiala } 803a9882ceeSTodd Fiala 804fa03ad2eSChaoren Lin // Tell coordinator about about the "new" (since exec) stopped main thread. 805f9077782SPavel Labath ThreadWasCreated(*main_thread_sp); 806fa03ad2eSChaoren Lin 807a9882ceeSTodd Fiala // Let our delegate know we have just exec'd. 808a9882ceeSTodd Fiala NotifyDidExec(); 809a9882ceeSTodd Fiala 810a9882ceeSTodd Fiala // If we have a main thread, indicate we are stopped. 811b9c1b51eSKate Stone assert(main_thread_sp && "exec called during ptraced process but no main " 812b9c1b51eSKate Stone "thread metadata tracked"); 813fa03ad2eSChaoren Lin 814fa03ad2eSChaoren Lin // Let the process know we're stopped. 815b9cc0c75SPavel Labath StopRunningThreads(main_thread_sp->GetID()); 816a9882ceeSTodd Fiala 817af245d11STodd Fiala break; 818a9882ceeSTodd Fiala } 819af245d11STodd Fiala 820b9c1b51eSKate Stone case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): { 821af245d11STodd Fiala // The inferior process or one of its threads is about to exit. 822b9c1b51eSKate Stone // We don't want to do anything with the thread so we just resume it. In 823b9c1b51eSKate Stone // case we 824b9c1b51eSKate Stone // want to implement "break on thread exit" functionality, we would need to 825b9c1b51eSKate Stone // stop 8266e35163cSPavel Labath // here. 827fa03ad2eSChaoren Lin 828af245d11STodd Fiala unsigned long data = 0; 829b9cc0c75SPavel Labath if (GetEventMessage(thread.GetID(), &data).Fail()) 830af245d11STodd Fiala data = -1; 831af245d11STodd Fiala 832a6321a8eSPavel Labath LLDB_LOG(log, 833a6321a8eSPavel Labath "received PTRACE_EVENT_EXIT, data = {0:x}, WIFEXITED={1}, " 834a6321a8eSPavel Labath "WIFSIGNALED={2}, pid = {3}, main_thread = {4}", 835a6321a8eSPavel Labath data, WIFEXITED(data), WIFSIGNALED(data), thread.GetID(), 836a6321a8eSPavel Labath is_main_thread); 837af245d11STodd Fiala 838b9c1b51eSKate Stone if (is_main_thread) { 839b9c1b51eSKate Stone SetExitStatus(convert_pid_status_to_exit_type(data), 840b9c1b51eSKate Stone convert_pid_status_to_return_code(data), nullptr, true); 84175f47c3aSTodd Fiala } 84275f47c3aSTodd Fiala 84386852d36SPavel Labath StateType state = thread.GetState(); 844b9c1b51eSKate Stone if (!StateIsRunningState(state)) { 845b9c1b51eSKate Stone // Due to a kernel bug, we may sometimes get this stop after the inferior 846b9c1b51eSKate Stone // gets a 847b9c1b51eSKate Stone // SIGKILL. This confuses our state tracking logic in ResumeThread(), 848b9c1b51eSKate Stone // since normally, 849b9c1b51eSKate Stone // we should not be receiving any ptrace events while the inferior is 850b9c1b51eSKate Stone // stopped. This 85186852d36SPavel Labath // makes sure that the inferior is resumed and exits normally. 85286852d36SPavel Labath state = eStateRunning; 85386852d36SPavel Labath } 85486852d36SPavel Labath ResumeThread(thread, state, LLDB_INVALID_SIGNAL_NUMBER); 855af245d11STodd Fiala 856af245d11STodd Fiala break; 857af245d11STodd Fiala } 858af245d11STodd Fiala 859af245d11STodd Fiala case 0: 860c16f5dcaSChaoren Lin case TRAP_TRACE: // We receive this on single stepping. 861c16f5dcaSChaoren Lin case TRAP_HWBKPT: // We receive this on watchpoint hit 86286fd8e45SChaoren Lin { 863c16f5dcaSChaoren Lin // If a watchpoint was hit, report it 864c16f5dcaSChaoren Lin uint32_t wp_index; 865b9c1b51eSKate Stone Error error = thread.GetRegisterContext()->GetWatchpointHitIndex( 866b9c1b51eSKate Stone wp_index, (uintptr_t)info.si_addr); 867a6321a8eSPavel Labath if (error.Fail()) 868a6321a8eSPavel Labath LLDB_LOG(log, 869a6321a8eSPavel Labath "received error while checking for watchpoint hits, pid = " 870a6321a8eSPavel Labath "{0}, error = {1}", 871a6321a8eSPavel Labath thread.GetID(), error); 872b9c1b51eSKate Stone if (wp_index != LLDB_INVALID_INDEX32) { 873b9cc0c75SPavel Labath MonitorWatchpoint(thread, wp_index); 874c16f5dcaSChaoren Lin break; 875c16f5dcaSChaoren Lin } 876b9cc0c75SPavel Labath 877be379e15STamas Berghammer // Otherwise, report step over 878be379e15STamas Berghammer MonitorTrace(thread); 879af245d11STodd Fiala break; 880b9cc0c75SPavel Labath } 881af245d11STodd Fiala 882af245d11STodd Fiala case SI_KERNEL: 88335799963SMohit K. Bhakkad #if defined __mips__ 88435799963SMohit K. Bhakkad // For mips there is no special signal for watchpoint 88535799963SMohit K. Bhakkad // So we check for watchpoint in kernel trap 88635799963SMohit K. Bhakkad { 88735799963SMohit K. Bhakkad // If a watchpoint was hit, report it 88835799963SMohit K. Bhakkad uint32_t wp_index; 889b9c1b51eSKate Stone Error error = thread.GetRegisterContext()->GetWatchpointHitIndex( 890b9c1b51eSKate Stone wp_index, LLDB_INVALID_ADDRESS); 891a6321a8eSPavel Labath if (error.Fail()) 892a6321a8eSPavel Labath LLDB_LOG(log, 893a6321a8eSPavel Labath "received error while checking for watchpoint hits, pid = " 894a6321a8eSPavel Labath "{0}, error = {1}", 895a6321a8eSPavel Labath thread.GetID(), error); 896b9c1b51eSKate Stone if (wp_index != LLDB_INVALID_INDEX32) { 897b9cc0c75SPavel Labath MonitorWatchpoint(thread, wp_index); 89835799963SMohit K. Bhakkad break; 89935799963SMohit K. Bhakkad } 90035799963SMohit K. Bhakkad } 90135799963SMohit K. Bhakkad // NO BREAK 90235799963SMohit K. Bhakkad #endif 903af245d11STodd Fiala case TRAP_BRKPT: 904b9cc0c75SPavel Labath MonitorBreakpoint(thread); 905af245d11STodd Fiala break; 906af245d11STodd Fiala 907af245d11STodd Fiala case SIGTRAP: 908af245d11STodd Fiala case (SIGTRAP | 0x80): 909a6321a8eSPavel Labath LLDB_LOG( 910a6321a8eSPavel Labath log, 911a6321a8eSPavel Labath "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming", 912a6321a8eSPavel Labath info.si_code, GetID(), thread.GetID()); 913fa03ad2eSChaoren Lin 914af245d11STodd Fiala // Ignore these signals until we know more about them. 915b9cc0c75SPavel Labath ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); 916af245d11STodd Fiala break; 917af245d11STodd Fiala 918af245d11STodd Fiala default: 919a6321a8eSPavel Labath LLDB_LOG( 920a6321a8eSPavel Labath log, 921a6321a8eSPavel Labath "received unknown SIGTRAP stop event ({0}, pid {1} tid {2}, resuming", 922a6321a8eSPavel Labath info.si_code, GetID(), thread.GetID()); 923a6321a8eSPavel Labath llvm_unreachable("Unexpected SIGTRAP code!"); 924af245d11STodd Fiala break; 925af245d11STodd Fiala } 926af245d11STodd Fiala } 927af245d11STodd Fiala 928b9c1b51eSKate Stone void NativeProcessLinux::MonitorTrace(NativeThreadLinux &thread) { 929a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 930a6321a8eSPavel Labath LLDB_LOG(log, "received trace event, pid = {0}", thread.GetID()); 931c16f5dcaSChaoren Lin 9320e1d729bSPavel Labath // This thread is currently stopped. 933b9cc0c75SPavel Labath thread.SetStoppedByTrace(); 934c16f5dcaSChaoren Lin 935b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 936c16f5dcaSChaoren Lin } 937c16f5dcaSChaoren Lin 938b9c1b51eSKate Stone void NativeProcessLinux::MonitorBreakpoint(NativeThreadLinux &thread) { 939b9c1b51eSKate Stone Log *log( 940b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 941a6321a8eSPavel Labath LLDB_LOG(log, "received breakpoint event, pid = {0}", thread.GetID()); 942c16f5dcaSChaoren Lin 943c16f5dcaSChaoren Lin // Mark the thread as stopped at breakpoint. 944b9cc0c75SPavel Labath thread.SetStoppedByBreakpoint(); 945b9cc0c75SPavel Labath Error error = FixupBreakpointPCAsNeeded(thread); 946c16f5dcaSChaoren Lin if (error.Fail()) 947a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0} fixup: {1}", thread.GetID(), error); 948d8c338d4STamas Berghammer 949b9c1b51eSKate Stone if (m_threads_stepping_with_breakpoint.find(thread.GetID()) != 950b9c1b51eSKate Stone m_threads_stepping_with_breakpoint.end()) 951b9cc0c75SPavel Labath thread.SetStoppedByTrace(); 952c16f5dcaSChaoren Lin 953b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 954c16f5dcaSChaoren Lin } 955c16f5dcaSChaoren Lin 956b9c1b51eSKate Stone void NativeProcessLinux::MonitorWatchpoint(NativeThreadLinux &thread, 957b9c1b51eSKate Stone uint32_t wp_index) { 958b9c1b51eSKate Stone Log *log( 959b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_WATCHPOINTS)); 960a6321a8eSPavel Labath LLDB_LOG(log, "received watchpoint event, pid = {0}, wp_index = {1}", 961a6321a8eSPavel Labath thread.GetID(), wp_index); 962c16f5dcaSChaoren Lin 963c16f5dcaSChaoren Lin // Mark the thread as stopped at watchpoint. 964c16f5dcaSChaoren Lin // The address is at (lldb::addr_t)info->si_addr if we need it. 965f9077782SPavel Labath thread.SetStoppedByWatchpoint(wp_index); 966c16f5dcaSChaoren Lin 967b9c1b51eSKate Stone // We need to tell all other running threads before we notify the delegate 968b9c1b51eSKate Stone // about this stop. 969f9077782SPavel Labath StopRunningThreads(thread.GetID()); 970c16f5dcaSChaoren Lin } 971c16f5dcaSChaoren Lin 972b9c1b51eSKate Stone void NativeProcessLinux::MonitorSignal(const siginfo_t &info, 973b9c1b51eSKate Stone NativeThreadLinux &thread, bool exited) { 974b9cc0c75SPavel Labath const int signo = info.si_signo; 975b9cc0c75SPavel Labath const bool is_from_llgs = info.si_pid == getpid(); 976af245d11STodd Fiala 977a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 978af245d11STodd Fiala 979af245d11STodd Fiala // POSIX says that process behaviour is undefined after it ignores a SIGFPE, 980af245d11STodd Fiala // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a 981af245d11STodd Fiala // kill(2) or raise(3). Similarly for tgkill(2) on Linux. 982af245d11STodd Fiala // 983af245d11STodd Fiala // IOW, user generated signals never generate what we consider to be a 984af245d11STodd Fiala // "crash". 985af245d11STodd Fiala // 986af245d11STodd Fiala // Similarly, ACK signals generated by this monitor. 987af245d11STodd Fiala 988af245d11STodd Fiala // Handle the signal. 989a6321a8eSPavel Labath LLDB_LOG(log, 990a6321a8eSPavel Labath "received signal {0} ({1}) with code {2}, (siginfo pid = {3}, " 991a6321a8eSPavel Labath "waitpid pid = {4})", 992a6321a8eSPavel Labath Host::GetSignalAsCString(signo), signo, info.si_code, 993b9cc0c75SPavel Labath thread.GetID()); 99458a2f669STodd Fiala 99558a2f669STodd Fiala // Check for thread stop notification. 996b9c1b51eSKate Stone if (is_from_llgs && (info.si_code == SI_TKILL) && (signo == SIGSTOP)) { 997af245d11STodd Fiala // This is a tgkill()-based stop. 998a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} tid {1}, thread stopped", GetID(), thread.GetID()); 999fa03ad2eSChaoren Lin 1000aab58633SChaoren Lin // Check that we're not already marked with a stop reason. 1001b9c1b51eSKate Stone // Note this thread really shouldn't already be marked as stopped - if we 1002a6321a8eSPavel Labath // were, that would imply that the kernel signaled us with the thread 1003a6321a8eSPavel Labath // stopping which we handled and marked as stopped, and that, without an 1004a6321a8eSPavel Labath // intervening resume, we received another stop. It is more likely that we 1005a6321a8eSPavel Labath // are missing the marking of a run state somewhere if we find that the 1006a6321a8eSPavel Labath // thread was marked as stopped. 1007b9cc0c75SPavel Labath const StateType thread_state = thread.GetState(); 1008b9c1b51eSKate Stone if (!StateIsStoppedState(thread_state, false)) { 1009ed89c7feSPavel Labath // An inferior thread has stopped because of a SIGSTOP we have sent it. 1010b9c1b51eSKate Stone // Generally, these are not important stops and we don't want to report 1011a6321a8eSPavel Labath // them as they are just used to stop other threads when one thread (the 1012a6321a8eSPavel Labath // one with the *real* stop reason) hits a breakpoint (watchpoint, 1013a6321a8eSPavel Labath // etc...). However, in the case of an asynchronous Interrupt(), this *is* 1014a6321a8eSPavel Labath // the real stop reason, so we leave the signal intact if this is the 1015a6321a8eSPavel Labath // thread that was chosen as the triggering thread. 1016b9c1b51eSKate Stone if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) { 1017b9cc0c75SPavel Labath if (m_pending_notification_tid == thread.GetID()) 1018b9cc0c75SPavel Labath thread.SetStoppedBySignal(SIGSTOP, &info); 1019ed89c7feSPavel Labath else 1020b9cc0c75SPavel Labath thread.SetStoppedWithNoReason(); 1021ed89c7feSPavel Labath 1022b9cc0c75SPavel Labath SetCurrentThreadID(thread.GetID()); 10230e1d729bSPavel Labath SignalIfAllThreadsStopped(); 1024b9c1b51eSKate Stone } else { 10250e1d729bSPavel Labath // We can end up here if stop was initiated by LLGS but by this time a 10260e1d729bSPavel Labath // thread stop has occurred - maybe initiated by another event. 1027b9cc0c75SPavel Labath Error error = ResumeThread(thread, thread.GetState(), 0); 1028a6321a8eSPavel Labath if (error.Fail()) 1029a6321a8eSPavel Labath LLDB_LOG(log, "failed to resume thread {0}: {1}", thread.GetID(), 1030a6321a8eSPavel Labath error); 10310e1d729bSPavel Labath } 1032b9c1b51eSKate Stone } else { 1033a6321a8eSPavel Labath LLDB_LOG(log, 1034a6321a8eSPavel Labath "pid {0} tid {1}, thread was already marked as a stopped " 1035a6321a8eSPavel Labath "state (state={2}), leaving stop signal as is", 1036a6321a8eSPavel Labath GetID(), thread.GetID(), StateAsCString(thread_state)); 10370e1d729bSPavel Labath SignalIfAllThreadsStopped(); 1038af245d11STodd Fiala } 1039af245d11STodd Fiala 104058a2f669STodd Fiala // Done handling. 1041af245d11STodd Fiala return; 1042af245d11STodd Fiala } 1043af245d11STodd Fiala 104486fd8e45SChaoren Lin // This thread is stopped. 1045a6321a8eSPavel Labath LLDB_LOG(log, "received signal {0}", Host::GetSignalAsCString(signo)); 1046b9cc0c75SPavel Labath thread.SetStoppedBySignal(signo, &info); 104786fd8e45SChaoren Lin 104886fd8e45SChaoren Lin // Send a stop to the debugger after we get all other threads to stop. 1049b9cc0c75SPavel Labath StopRunningThreads(thread.GetID()); 1050511e5cdcSTodd Fiala } 1051af245d11STodd Fiala 1052e7708688STamas Berghammer namespace { 1053e7708688STamas Berghammer 1054b9c1b51eSKate Stone struct EmulatorBaton { 1055e7708688STamas Berghammer NativeProcessLinux *m_process; 1056e7708688STamas Berghammer NativeRegisterContext *m_reg_context; 10576648fcc3SPavel Labath 10586648fcc3SPavel Labath // eRegisterKindDWARF -> RegsiterValue 10596648fcc3SPavel Labath std::unordered_map<uint32_t, RegisterValue> m_register_values; 1060e7708688STamas Berghammer 1061b9c1b51eSKate Stone EmulatorBaton(NativeProcessLinux *process, NativeRegisterContext *reg_context) 1062b9c1b51eSKate Stone : m_process(process), m_reg_context(reg_context) {} 1063e7708688STamas Berghammer }; 1064e7708688STamas Berghammer 1065e7708688STamas Berghammer } // anonymous namespace 1066e7708688STamas Berghammer 1067b9c1b51eSKate Stone static size_t ReadMemoryCallback(EmulateInstruction *instruction, void *baton, 1068e7708688STamas Berghammer const EmulateInstruction::Context &context, 1069b9c1b51eSKate Stone lldb::addr_t addr, void *dst, size_t length) { 1070e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 1071e7708688STamas Berghammer 10723eb4b458SChaoren Lin size_t bytes_read; 1073e7708688STamas Berghammer emulator_baton->m_process->ReadMemory(addr, dst, length, bytes_read); 1074e7708688STamas Berghammer return bytes_read; 1075e7708688STamas Berghammer } 1076e7708688STamas Berghammer 1077b9c1b51eSKate Stone static bool ReadRegisterCallback(EmulateInstruction *instruction, void *baton, 1078e7708688STamas Berghammer const RegisterInfo *reg_info, 1079b9c1b51eSKate Stone RegisterValue ®_value) { 1080e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 1081e7708688STamas Berghammer 1082b9c1b51eSKate Stone auto it = emulator_baton->m_register_values.find( 1083b9c1b51eSKate Stone reg_info->kinds[eRegisterKindDWARF]); 1084b9c1b51eSKate Stone if (it != emulator_baton->m_register_values.end()) { 10856648fcc3SPavel Labath reg_value = it->second; 10866648fcc3SPavel Labath return true; 10876648fcc3SPavel Labath } 10886648fcc3SPavel Labath 1089e7708688STamas Berghammer // The emulator only fill in the dwarf regsiter numbers (and in some case 1090e7708688STamas Berghammer // the generic register numbers). Get the full register info from the 1091e7708688STamas Berghammer // register context based on the dwarf register numbers. 1092b9c1b51eSKate Stone const RegisterInfo *full_reg_info = 1093b9c1b51eSKate Stone emulator_baton->m_reg_context->GetRegisterInfo( 1094e7708688STamas Berghammer eRegisterKindDWARF, reg_info->kinds[eRegisterKindDWARF]); 1095e7708688STamas Berghammer 1096b9c1b51eSKate Stone Error error = 1097b9c1b51eSKate Stone emulator_baton->m_reg_context->ReadRegister(full_reg_info, reg_value); 10986648fcc3SPavel Labath if (error.Success()) 10996648fcc3SPavel Labath return true; 1100cdc22a88SMohit K. Bhakkad 11016648fcc3SPavel Labath return false; 1102e7708688STamas Berghammer } 1103e7708688STamas Berghammer 1104b9c1b51eSKate Stone static bool WriteRegisterCallback(EmulateInstruction *instruction, void *baton, 1105e7708688STamas Berghammer const EmulateInstruction::Context &context, 1106e7708688STamas Berghammer const RegisterInfo *reg_info, 1107b9c1b51eSKate Stone const RegisterValue ®_value) { 1108e7708688STamas Berghammer EmulatorBaton *emulator_baton = static_cast<EmulatorBaton *>(baton); 1109b9c1b51eSKate Stone emulator_baton->m_register_values[reg_info->kinds[eRegisterKindDWARF]] = 1110b9c1b51eSKate Stone reg_value; 1111e7708688STamas Berghammer return true; 1112e7708688STamas Berghammer } 1113e7708688STamas Berghammer 1114b9c1b51eSKate Stone static size_t WriteMemoryCallback(EmulateInstruction *instruction, void *baton, 1115e7708688STamas Berghammer const EmulateInstruction::Context &context, 1116b9c1b51eSKate Stone lldb::addr_t addr, const void *dst, 1117b9c1b51eSKate Stone size_t length) { 1118e7708688STamas Berghammer return length; 1119e7708688STamas Berghammer } 1120e7708688STamas Berghammer 1121b9c1b51eSKate Stone static lldb::addr_t ReadFlags(NativeRegisterContext *regsiter_context) { 1122e7708688STamas Berghammer const RegisterInfo *flags_info = regsiter_context->GetRegisterInfo( 1123e7708688STamas Berghammer eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 1124b9c1b51eSKate Stone return regsiter_context->ReadRegisterAsUnsigned(flags_info, 1125b9c1b51eSKate Stone LLDB_INVALID_ADDRESS); 1126e7708688STamas Berghammer } 1127e7708688STamas Berghammer 1128b9c1b51eSKate Stone Error NativeProcessLinux::SetupSoftwareSingleStepping( 1129b9c1b51eSKate Stone NativeThreadLinux &thread) { 1130e7708688STamas Berghammer Error error; 1131b9cc0c75SPavel Labath NativeRegisterContextSP register_context_sp = thread.GetRegisterContext(); 1132e7708688STamas Berghammer 1133e7708688STamas Berghammer std::unique_ptr<EmulateInstruction> emulator_ap( 1134b9c1b51eSKate Stone EmulateInstruction::FindPlugin(m_arch, eInstructionTypePCModifying, 1135b9c1b51eSKate Stone nullptr)); 1136e7708688STamas Berghammer 1137e7708688STamas Berghammer if (emulator_ap == nullptr) 1138e7708688STamas Berghammer return Error("Instruction emulator not found!"); 1139e7708688STamas Berghammer 1140e7708688STamas Berghammer EmulatorBaton baton(this, register_context_sp.get()); 1141e7708688STamas Berghammer emulator_ap->SetBaton(&baton); 1142e7708688STamas Berghammer emulator_ap->SetReadMemCallback(&ReadMemoryCallback); 1143e7708688STamas Berghammer emulator_ap->SetReadRegCallback(&ReadRegisterCallback); 1144e7708688STamas Berghammer emulator_ap->SetWriteMemCallback(&WriteMemoryCallback); 1145e7708688STamas Berghammer emulator_ap->SetWriteRegCallback(&WriteRegisterCallback); 1146e7708688STamas Berghammer 1147e7708688STamas Berghammer if (!emulator_ap->ReadInstruction()) 1148e7708688STamas Berghammer return Error("Read instruction failed!"); 1149e7708688STamas Berghammer 1150b9c1b51eSKate Stone bool emulation_result = 1151b9c1b51eSKate Stone emulator_ap->EvaluateInstruction(eEmulateInstructionOptionAutoAdvancePC); 11526648fcc3SPavel Labath 1153b9c1b51eSKate Stone const RegisterInfo *reg_info_pc = register_context_sp->GetRegisterInfo( 1154b9c1b51eSKate Stone eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); 1155b9c1b51eSKate Stone const RegisterInfo *reg_info_flags = register_context_sp->GetRegisterInfo( 1156b9c1b51eSKate Stone eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); 11576648fcc3SPavel Labath 1158b9c1b51eSKate Stone auto pc_it = 1159b9c1b51eSKate Stone baton.m_register_values.find(reg_info_pc->kinds[eRegisterKindDWARF]); 1160b9c1b51eSKate Stone auto flags_it = 1161b9c1b51eSKate Stone baton.m_register_values.find(reg_info_flags->kinds[eRegisterKindDWARF]); 11626648fcc3SPavel Labath 1163e7708688STamas Berghammer lldb::addr_t next_pc; 1164e7708688STamas Berghammer lldb::addr_t next_flags; 1165b9c1b51eSKate Stone if (emulation_result) { 1166b9c1b51eSKate Stone assert(pc_it != baton.m_register_values.end() && 1167b9c1b51eSKate Stone "Emulation was successfull but PC wasn't updated"); 11686648fcc3SPavel Labath next_pc = pc_it->second.GetAsUInt64(); 11696648fcc3SPavel Labath 11706648fcc3SPavel Labath if (flags_it != baton.m_register_values.end()) 11716648fcc3SPavel Labath next_flags = flags_it->second.GetAsUInt64(); 1172e7708688STamas Berghammer else 1173e7708688STamas Berghammer next_flags = ReadFlags(register_context_sp.get()); 1174b9c1b51eSKate Stone } else if (pc_it == baton.m_register_values.end()) { 1175e7708688STamas Berghammer // Emulate instruction failed and it haven't changed PC. Advance PC 1176e7708688STamas Berghammer // with the size of the current opcode because the emulation of all 1177e7708688STamas Berghammer // PC modifying instruction should be successful. The failure most 1178e7708688STamas Berghammer // likely caused by a not supported instruction which don't modify PC. 1179b9c1b51eSKate Stone next_pc = 1180b9c1b51eSKate Stone register_context_sp->GetPC() + emulator_ap->GetOpcode().GetByteSize(); 1181e7708688STamas Berghammer next_flags = ReadFlags(register_context_sp.get()); 1182b9c1b51eSKate Stone } else { 1183e7708688STamas Berghammer // The instruction emulation failed after it modified the PC. It is an 1184e7708688STamas Berghammer // unknown error where we can't continue because the next instruction is 1185e7708688STamas Berghammer // modifying the PC but we don't know how. 1186e7708688STamas Berghammer return Error("Instruction emulation failed unexpectedly."); 1187e7708688STamas Berghammer } 1188e7708688STamas Berghammer 1189b9c1b51eSKate Stone if (m_arch.GetMachine() == llvm::Triple::arm) { 1190b9c1b51eSKate Stone if (next_flags & 0x20) { 1191e7708688STamas Berghammer // Thumb mode 1192e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 2); 1193b9c1b51eSKate Stone } else { 1194e7708688STamas Berghammer // Arm mode 1195e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 4); 1196e7708688STamas Berghammer } 1197b9c1b51eSKate Stone } else if (m_arch.GetMachine() == llvm::Triple::mips64 || 1198b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64el || 1199b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips || 1200b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mipsel) 1201cdc22a88SMohit K. Bhakkad error = SetSoftwareBreakpoint(next_pc, 4); 1202b9c1b51eSKate Stone else { 1203e7708688STamas Berghammer // No size hint is given for the next breakpoint 1204e7708688STamas Berghammer error = SetSoftwareBreakpoint(next_pc, 0); 1205e7708688STamas Berghammer } 1206e7708688STamas Berghammer 120742eb6908SPavel Labath // If setting the breakpoint fails because next_pc is out of 120842eb6908SPavel Labath // the address space, ignore it and let the debugee segfault. 120942eb6908SPavel Labath if (error.GetError() == EIO || error.GetError() == EFAULT) { 1210665be50eSMehdi Amini return Error(); 121142eb6908SPavel Labath } else if (error.Fail()) 1212e7708688STamas Berghammer return error; 1213e7708688STamas Berghammer 1214b9cc0c75SPavel Labath m_threads_stepping_with_breakpoint.insert({thread.GetID(), next_pc}); 1215e7708688STamas Berghammer 1216665be50eSMehdi Amini return Error(); 1217e7708688STamas Berghammer } 1218e7708688STamas Berghammer 1219b9c1b51eSKate Stone bool NativeProcessLinux::SupportHardwareSingleStepping() const { 1220b9c1b51eSKate Stone if (m_arch.GetMachine() == llvm::Triple::arm || 1221b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64 || 1222b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips64el || 1223b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mips || 1224b9c1b51eSKate Stone m_arch.GetMachine() == llvm::Triple::mipsel) 1225cdc22a88SMohit K. Bhakkad return false; 1226cdc22a88SMohit K. Bhakkad return true; 1227e7708688STamas Berghammer } 1228e7708688STamas Berghammer 1229b9c1b51eSKate Stone Error NativeProcessLinux::Resume(const ResumeActionList &resume_actions) { 1230a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1231a6321a8eSPavel Labath LLDB_LOG(log, "pid {0}", GetID()); 1232af245d11STodd Fiala 1233e7708688STamas Berghammer bool software_single_step = !SupportHardwareSingleStepping(); 1234af245d11STodd Fiala 1235b9c1b51eSKate Stone if (software_single_step) { 1236b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 1237e7708688STamas Berghammer assert(thread_sp && "thread list should not contain NULL threads"); 1238e7708688STamas Berghammer 1239b9c1b51eSKate Stone const ResumeAction *const action = 1240b9c1b51eSKate Stone resume_actions.GetActionForThread(thread_sp->GetID(), true); 1241e7708688STamas Berghammer if (action == nullptr) 1242e7708688STamas Berghammer continue; 1243e7708688STamas Berghammer 1244b9c1b51eSKate Stone if (action->state == eStateStepping) { 1245b9c1b51eSKate Stone Error error = SetupSoftwareSingleStepping( 1246b9c1b51eSKate Stone static_cast<NativeThreadLinux &>(*thread_sp)); 1247e7708688STamas Berghammer if (error.Fail()) 1248e7708688STamas Berghammer return error; 1249e7708688STamas Berghammer } 1250e7708688STamas Berghammer } 1251e7708688STamas Berghammer } 1252e7708688STamas Berghammer 1253b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 1254af245d11STodd Fiala assert(thread_sp && "thread list should not contain NULL threads"); 1255af245d11STodd Fiala 1256b9c1b51eSKate Stone const ResumeAction *const action = 1257b9c1b51eSKate Stone resume_actions.GetActionForThread(thread_sp->GetID(), true); 12586a196ce6SChaoren Lin 1259b9c1b51eSKate Stone if (action == nullptr) { 1260a6321a8eSPavel Labath LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(), 1261a6321a8eSPavel Labath thread_sp->GetID()); 12626a196ce6SChaoren Lin continue; 12636a196ce6SChaoren Lin } 1264af245d11STodd Fiala 1265a6321a8eSPavel Labath LLDB_LOG(log, "processing resume action state {0} for pid {1} tid {2}", 1266a6321a8eSPavel Labath StateAsCString(action->state), GetID(), thread_sp->GetID()); 1267af245d11STodd Fiala 1268b9c1b51eSKate Stone switch (action->state) { 1269af245d11STodd Fiala case eStateRunning: 1270b9c1b51eSKate Stone case eStateStepping: { 1271af245d11STodd Fiala // Run the thread, possibly feeding it the signal. 1272fa03ad2eSChaoren Lin const int signo = action->signal; 1273b9c1b51eSKate Stone ResumeThread(static_cast<NativeThreadLinux &>(*thread_sp), action->state, 1274b9c1b51eSKate Stone signo); 1275af245d11STodd Fiala break; 1276ae29d395SChaoren Lin } 1277af245d11STodd Fiala 1278af245d11STodd Fiala case eStateSuspended: 1279af245d11STodd Fiala case eStateStopped: 1280a6321a8eSPavel Labath llvm_unreachable("Unexpected state"); 1281af245d11STodd Fiala 1282af245d11STodd Fiala default: 1283b9c1b51eSKate Stone return Error("NativeProcessLinux::%s (): unexpected state %s specified " 1284b9c1b51eSKate Stone "for pid %" PRIu64 ", tid %" PRIu64, 1285b9c1b51eSKate Stone __FUNCTION__, StateAsCString(action->state), GetID(), 1286b9c1b51eSKate Stone thread_sp->GetID()); 1287af245d11STodd Fiala } 1288af245d11STodd Fiala } 1289af245d11STodd Fiala 1290665be50eSMehdi Amini return Error(); 1291af245d11STodd Fiala } 1292af245d11STodd Fiala 1293b9c1b51eSKate Stone Error NativeProcessLinux::Halt() { 1294af245d11STodd Fiala Error error; 1295af245d11STodd Fiala 1296af245d11STodd Fiala if (kill(GetID(), SIGSTOP) != 0) 1297af245d11STodd Fiala error.SetErrorToErrno(); 1298af245d11STodd Fiala 1299af245d11STodd Fiala return error; 1300af245d11STodd Fiala } 1301af245d11STodd Fiala 1302b9c1b51eSKate Stone Error NativeProcessLinux::Detach() { 1303af245d11STodd Fiala Error error; 1304af245d11STodd Fiala 1305af245d11STodd Fiala // Stop monitoring the inferior. 130619cbe96aSPavel Labath m_sigchld_handle.reset(); 1307af245d11STodd Fiala 13087a9495bcSPavel Labath // Tell ptrace to detach from the process. 13097a9495bcSPavel Labath if (GetID() == LLDB_INVALID_PROCESS_ID) 13107a9495bcSPavel Labath return error; 13117a9495bcSPavel Labath 1312b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 13137a9495bcSPavel Labath Error e = Detach(thread_sp->GetID()); 13147a9495bcSPavel Labath if (e.Fail()) 1315b9c1b51eSKate Stone error = 1316b9c1b51eSKate Stone e; // Save the error, but still attempt to detach from other threads. 13177a9495bcSPavel Labath } 13187a9495bcSPavel Labath 1319af245d11STodd Fiala return error; 1320af245d11STodd Fiala } 1321af245d11STodd Fiala 1322b9c1b51eSKate Stone Error NativeProcessLinux::Signal(int signo) { 1323af245d11STodd Fiala Error error; 1324af245d11STodd Fiala 1325a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1326a6321a8eSPavel Labath LLDB_LOG(log, "sending signal {0} ({1}) to pid {1}", signo, 1327a6321a8eSPavel Labath Host::GetSignalAsCString(signo), GetID()); 1328af245d11STodd Fiala 1329af245d11STodd Fiala if (kill(GetID(), signo)) 1330af245d11STodd Fiala error.SetErrorToErrno(); 1331af245d11STodd Fiala 1332af245d11STodd Fiala return error; 1333af245d11STodd Fiala } 1334af245d11STodd Fiala 1335b9c1b51eSKate Stone Error NativeProcessLinux::Interrupt() { 1336e9547b80SChaoren Lin // Pick a running thread (or if none, a not-dead stopped thread) as 1337e9547b80SChaoren Lin // the chosen thread that will be the stop-reason thread. 1338a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1339e9547b80SChaoren Lin 1340e9547b80SChaoren Lin NativeThreadProtocolSP running_thread_sp; 1341e9547b80SChaoren Lin NativeThreadProtocolSP stopped_thread_sp; 1342e9547b80SChaoren Lin 1343a6321a8eSPavel Labath LLDB_LOG(log, "selecting running thread for interrupt target"); 1344b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 1345e9547b80SChaoren Lin // The thread shouldn't be null but lets just cover that here. 1346e9547b80SChaoren Lin if (!thread_sp) 1347e9547b80SChaoren Lin continue; 1348e9547b80SChaoren Lin 1349e9547b80SChaoren Lin // If we have a running or stepping thread, we'll call that the 1350e9547b80SChaoren Lin // target of the interrupt. 1351e9547b80SChaoren Lin const auto thread_state = thread_sp->GetState(); 1352b9c1b51eSKate Stone if (thread_state == eStateRunning || thread_state == eStateStepping) { 1353e9547b80SChaoren Lin running_thread_sp = thread_sp; 1354e9547b80SChaoren Lin break; 1355b9c1b51eSKate Stone } else if (!stopped_thread_sp && StateIsStoppedState(thread_state, true)) { 1356b9c1b51eSKate Stone // Remember the first non-dead stopped thread. We'll use that as a backup 1357b9c1b51eSKate Stone // if there are no running threads. 1358e9547b80SChaoren Lin stopped_thread_sp = thread_sp; 1359e9547b80SChaoren Lin } 1360e9547b80SChaoren Lin } 1361e9547b80SChaoren Lin 1362b9c1b51eSKate Stone if (!running_thread_sp && !stopped_thread_sp) { 1363b9c1b51eSKate Stone Error error("found no running/stepping or live stopped threads as target " 1364b9c1b51eSKate Stone "for interrupt"); 1365a6321a8eSPavel Labath LLDB_LOG(log, "skipping due to error: {0}", error); 13665830aa75STamas Berghammer 1367e9547b80SChaoren Lin return error; 1368e9547b80SChaoren Lin } 1369e9547b80SChaoren Lin 1370b9c1b51eSKate Stone NativeThreadProtocolSP deferred_signal_thread_sp = 1371b9c1b51eSKate Stone running_thread_sp ? running_thread_sp : stopped_thread_sp; 1372e9547b80SChaoren Lin 1373a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} {1} tid {2} chosen for interrupt target", GetID(), 1374e9547b80SChaoren Lin running_thread_sp ? "running" : "stopped", 1375e9547b80SChaoren Lin deferred_signal_thread_sp->GetID()); 1376e9547b80SChaoren Lin 1377ed89c7feSPavel Labath StopRunningThreads(deferred_signal_thread_sp->GetID()); 137845f5cb31SPavel Labath 1379665be50eSMehdi Amini return Error(); 1380e9547b80SChaoren Lin } 1381e9547b80SChaoren Lin 1382b9c1b51eSKate Stone Error NativeProcessLinux::Kill() { 1383a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1384a6321a8eSPavel Labath LLDB_LOG(log, "pid {0}", GetID()); 1385af245d11STodd Fiala 1386af245d11STodd Fiala Error error; 1387af245d11STodd Fiala 1388b9c1b51eSKate Stone switch (m_state) { 1389af245d11STodd Fiala case StateType::eStateInvalid: 1390af245d11STodd Fiala case StateType::eStateExited: 1391af245d11STodd Fiala case StateType::eStateCrashed: 1392af245d11STodd Fiala case StateType::eStateDetached: 1393af245d11STodd Fiala case StateType::eStateUnloaded: 1394af245d11STodd Fiala // Nothing to do - the process is already dead. 1395a6321a8eSPavel Labath LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(), 1396a6321a8eSPavel Labath StateAsCString(m_state)); 1397af245d11STodd Fiala return error; 1398af245d11STodd Fiala 1399af245d11STodd Fiala case StateType::eStateConnected: 1400af245d11STodd Fiala case StateType::eStateAttaching: 1401af245d11STodd Fiala case StateType::eStateLaunching: 1402af245d11STodd Fiala case StateType::eStateStopped: 1403af245d11STodd Fiala case StateType::eStateRunning: 1404af245d11STodd Fiala case StateType::eStateStepping: 1405af245d11STodd Fiala case StateType::eStateSuspended: 1406af245d11STodd Fiala // We can try to kill a process in these states. 1407af245d11STodd Fiala break; 1408af245d11STodd Fiala } 1409af245d11STodd Fiala 1410b9c1b51eSKate Stone if (kill(GetID(), SIGKILL) != 0) { 1411af245d11STodd Fiala error.SetErrorToErrno(); 1412af245d11STodd Fiala return error; 1413af245d11STodd Fiala } 1414af245d11STodd Fiala 1415af245d11STodd Fiala return error; 1416af245d11STodd Fiala } 1417af245d11STodd Fiala 1418af245d11STodd Fiala static Error 1419b9c1b51eSKate Stone ParseMemoryRegionInfoFromProcMapsLine(const std::string &maps_line, 1420b9c1b51eSKate Stone MemoryRegionInfo &memory_region_info) { 1421af245d11STodd Fiala memory_region_info.Clear(); 1422af245d11STodd Fiala 1423b9739d40SPavel Labath StringExtractor line_extractor(maps_line.c_str()); 1424af245d11STodd Fiala 1425b9c1b51eSKate Stone // Format: {address_start_hex}-{address_end_hex} perms offset dev inode 1426b9c1b51eSKate Stone // pathname 1427b9c1b51eSKate Stone // perms: rwxp (letter is present if set, '-' if not, final character is 1428b9c1b51eSKate Stone // p=private, s=shared). 1429af245d11STodd Fiala 1430af245d11STodd Fiala // Parse out the starting address 1431af245d11STodd Fiala lldb::addr_t start_address = line_extractor.GetHexMaxU64(false, 0); 1432af245d11STodd Fiala 1433af245d11STodd Fiala // Parse out hyphen separating start and end address from range. 1434af245d11STodd Fiala if (!line_extractor.GetBytesLeft() || (line_extractor.GetChar() != '-')) 1435b9c1b51eSKate Stone return Error( 1436b9c1b51eSKate Stone "malformed /proc/{pid}/maps entry, missing dash between address range"); 1437af245d11STodd Fiala 1438af245d11STodd Fiala // Parse out the ending address 1439af245d11STodd Fiala lldb::addr_t end_address = line_extractor.GetHexMaxU64(false, start_address); 1440af245d11STodd Fiala 1441af245d11STodd Fiala // Parse out the space after the address. 1442af245d11STodd Fiala if (!line_extractor.GetBytesLeft() || (line_extractor.GetChar() != ' ')) 1443af245d11STodd Fiala return Error("malformed /proc/{pid}/maps entry, missing space after range"); 1444af245d11STodd Fiala 1445af245d11STodd Fiala // Save the range. 1446af245d11STodd Fiala memory_region_info.GetRange().SetRangeBase(start_address); 1447af245d11STodd Fiala memory_region_info.GetRange().SetRangeEnd(end_address); 1448af245d11STodd Fiala 1449b9c1b51eSKate Stone // Any memory region in /proc/{pid}/maps is by definition mapped into the 1450b9c1b51eSKate Stone // process. 1451ad007563SHoward Hellyer memory_region_info.SetMapped(MemoryRegionInfo::OptionalBool::eYes); 1452ad007563SHoward Hellyer 1453af245d11STodd Fiala // Parse out each permission entry. 1454af245d11STodd Fiala if (line_extractor.GetBytesLeft() < 4) 1455b9c1b51eSKate Stone return Error("malformed /proc/{pid}/maps entry, missing some portion of " 1456b9c1b51eSKate Stone "permissions"); 1457af245d11STodd Fiala 1458af245d11STodd Fiala // Handle read permission. 1459af245d11STodd Fiala const char read_perm_char = line_extractor.GetChar(); 1460af245d11STodd Fiala if (read_perm_char == 'r') 1461af245d11STodd Fiala memory_region_info.SetReadable(MemoryRegionInfo::OptionalBool::eYes); 1462c73301bbSTamas Berghammer else if (read_perm_char == '-') 1463af245d11STodd Fiala memory_region_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 1464c73301bbSTamas Berghammer else 1465c73301bbSTamas Berghammer return Error("unexpected /proc/{pid}/maps read permission char"); 1466af245d11STodd Fiala 1467af245d11STodd Fiala // Handle write permission. 1468af245d11STodd Fiala const char write_perm_char = line_extractor.GetChar(); 1469af245d11STodd Fiala if (write_perm_char == 'w') 1470af245d11STodd Fiala memory_region_info.SetWritable(MemoryRegionInfo::OptionalBool::eYes); 1471c73301bbSTamas Berghammer else if (write_perm_char == '-') 1472af245d11STodd Fiala memory_region_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 1473c73301bbSTamas Berghammer else 1474c73301bbSTamas Berghammer return Error("unexpected /proc/{pid}/maps write permission char"); 1475af245d11STodd Fiala 1476af245d11STodd Fiala // Handle execute permission. 1477af245d11STodd Fiala const char exec_perm_char = line_extractor.GetChar(); 1478af245d11STodd Fiala if (exec_perm_char == 'x') 1479af245d11STodd Fiala memory_region_info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes); 1480c73301bbSTamas Berghammer else if (exec_perm_char == '-') 1481af245d11STodd Fiala memory_region_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1482c73301bbSTamas Berghammer else 1483c73301bbSTamas Berghammer return Error("unexpected /proc/{pid}/maps exec permission char"); 1484af245d11STodd Fiala 1485d7d69f80STamas Berghammer line_extractor.GetChar(); // Read the private bit 1486d7d69f80STamas Berghammer line_extractor.SkipSpaces(); // Skip the separator 1487d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the offset 1488d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the major device number 1489d7d69f80STamas Berghammer line_extractor.GetChar(); // Read the device id separator 1490d7d69f80STamas Berghammer line_extractor.GetHexMaxU64(false, 0); // Read the major device number 1491d7d69f80STamas Berghammer line_extractor.SkipSpaces(); // Skip the separator 1492d7d69f80STamas Berghammer line_extractor.GetU64(0, 10); // Read the inode number 1493d7d69f80STamas Berghammer 1494d7d69f80STamas Berghammer line_extractor.SkipSpaces(); 1495b9739d40SPavel Labath const char *name = line_extractor.Peek(); 1496b9739d40SPavel Labath if (name) 1497b9739d40SPavel Labath memory_region_info.SetName(name); 1498d7d69f80STamas Berghammer 1499665be50eSMehdi Amini return Error(); 1500af245d11STodd Fiala } 1501af245d11STodd Fiala 1502b9c1b51eSKate Stone Error NativeProcessLinux::GetMemoryRegionInfo(lldb::addr_t load_addr, 1503b9c1b51eSKate Stone MemoryRegionInfo &range_info) { 1504b9c1b51eSKate Stone // FIXME review that the final memory region returned extends to the end of 1505b9c1b51eSKate Stone // the virtual address space, 1506af245d11STodd Fiala // with no perms if it is not mapped. 1507af245d11STodd Fiala 1508af245d11STodd Fiala // Use an approach that reads memory regions from /proc/{pid}/maps. 1509af245d11STodd Fiala // Assume proc maps entries are in ascending order. 1510af245d11STodd Fiala // FIXME assert if we find differently. 1511af245d11STodd Fiala 1512b9c1b51eSKate Stone if (m_supports_mem_region == LazyBool::eLazyBoolNo) { 1513af245d11STodd Fiala // We're done. 1514a6f5795aSTamas Berghammer return Error("unsupported"); 1515af245d11STodd Fiala } 1516af245d11STodd Fiala 1517a6f5795aSTamas Berghammer Error error = PopulateMemoryRegionCache(); 1518b9c1b51eSKate Stone if (error.Fail()) { 1519af245d11STodd Fiala return error; 1520af245d11STodd Fiala } 1521af245d11STodd Fiala 1522af245d11STodd Fiala lldb::addr_t prev_base_address = 0; 1523af245d11STodd Fiala 1524b9c1b51eSKate Stone // FIXME start by finding the last region that is <= target address using 1525b9c1b51eSKate Stone // binary search. Data is sorted. 1526af245d11STodd Fiala // There can be a ton of regions on pthreads apps with lots of threads. 1527b9c1b51eSKate Stone for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end(); 1528b9c1b51eSKate Stone ++it) { 1529a6f5795aSTamas Berghammer MemoryRegionInfo &proc_entry_info = it->first; 1530af245d11STodd Fiala 1531af245d11STodd Fiala // Sanity check assumption that /proc/{pid}/maps entries are ascending. 1532b9c1b51eSKate Stone assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && 1533b9c1b51eSKate Stone "descending /proc/pid/maps entries detected, unexpected"); 1534af245d11STodd Fiala prev_base_address = proc_entry_info.GetRange().GetRangeBase(); 1535*b1554311SHafiz Abid Qadeer UNUSED_IF_ASSERT_DISABLED(prev_base_address); 1536af245d11STodd Fiala 1537b9c1b51eSKate Stone // If the target address comes before this entry, indicate distance to next 1538b9c1b51eSKate Stone // region. 1539b9c1b51eSKate Stone if (load_addr < proc_entry_info.GetRange().GetRangeBase()) { 1540af245d11STodd Fiala range_info.GetRange().SetRangeBase(load_addr); 1541b9c1b51eSKate Stone range_info.GetRange().SetByteSize( 1542b9c1b51eSKate Stone proc_entry_info.GetRange().GetRangeBase() - load_addr); 1543af245d11STodd Fiala range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 1544af245d11STodd Fiala range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 1545af245d11STodd Fiala range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1546ad007563SHoward Hellyer range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 1547af245d11STodd Fiala 1548af245d11STodd Fiala return error; 1549b9c1b51eSKate Stone } else if (proc_entry_info.GetRange().Contains(load_addr)) { 1550af245d11STodd Fiala // The target address is within the memory region we're processing here. 1551af245d11STodd Fiala range_info = proc_entry_info; 1552af245d11STodd Fiala return error; 1553af245d11STodd Fiala } 1554af245d11STodd Fiala 1555b9c1b51eSKate Stone // The target memory address comes somewhere after the region we just 1556b9c1b51eSKate Stone // parsed. 1557af245d11STodd Fiala } 1558af245d11STodd Fiala 1559b9c1b51eSKate Stone // If we made it here, we didn't find an entry that contained the given 1560b9c1b51eSKate Stone // address. Return the 1561b9c1b51eSKate Stone // load_addr as start and the amount of bytes betwwen load address and the end 1562b9c1b51eSKate Stone // of the memory as 156309839c33STamas Berghammer // size. 156409839c33STamas Berghammer range_info.GetRange().SetRangeBase(load_addr); 1565ad007563SHoward Hellyer range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); 156609839c33STamas Berghammer range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); 156709839c33STamas Berghammer range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); 156809839c33STamas Berghammer range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); 1569ad007563SHoward Hellyer range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); 1570af245d11STodd Fiala return error; 1571af245d11STodd Fiala } 1572af245d11STodd Fiala 1573a6f5795aSTamas Berghammer Error NativeProcessLinux::PopulateMemoryRegionCache() { 1574a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1575a6f5795aSTamas Berghammer 1576a6f5795aSTamas Berghammer // If our cache is empty, pull the latest. There should always be at least 1577a6f5795aSTamas Berghammer // one memory region if memory region handling is supported. 1578a6f5795aSTamas Berghammer if (!m_mem_region_cache.empty()) { 1579a6321a8eSPavel Labath LLDB_LOG(log, "reusing {0} cached memory region entries", 1580a6321a8eSPavel Labath m_mem_region_cache.size()); 1581a6f5795aSTamas Berghammer return Error(); 1582a6f5795aSTamas Berghammer } 1583a6f5795aSTamas Berghammer 1584a6f5795aSTamas Berghammer Error error = ProcFileReader::ProcessLineByLine( 1585a6f5795aSTamas Berghammer GetID(), "maps", [&](const std::string &line) -> bool { 1586a6f5795aSTamas Berghammer MemoryRegionInfo info; 1587a6f5795aSTamas Berghammer const Error parse_error = 1588a6f5795aSTamas Berghammer ParseMemoryRegionInfoFromProcMapsLine(line, info); 1589a6f5795aSTamas Berghammer if (parse_error.Success()) { 1590a6f5795aSTamas Berghammer m_mem_region_cache.emplace_back( 1591a6f5795aSTamas Berghammer info, FileSpec(info.GetName().GetCString(), true)); 1592a6f5795aSTamas Berghammer return true; 1593a6f5795aSTamas Berghammer } else { 1594a6321a8eSPavel Labath LLDB_LOG(log, "failed to parse proc maps line '{0}': {1}", line, 1595a6321a8eSPavel Labath parse_error); 1596a6f5795aSTamas Berghammer return false; 1597a6f5795aSTamas Berghammer } 1598a6f5795aSTamas Berghammer }); 1599a6f5795aSTamas Berghammer 1600a6f5795aSTamas Berghammer // If we had an error, we'll mark unsupported. 1601a6f5795aSTamas Berghammer if (error.Fail()) { 1602a6f5795aSTamas Berghammer m_supports_mem_region = LazyBool::eLazyBoolNo; 1603a6f5795aSTamas Berghammer return error; 1604a6f5795aSTamas Berghammer } else if (m_mem_region_cache.empty()) { 1605a6f5795aSTamas Berghammer // No entries after attempting to read them. This shouldn't happen if 1606a6f5795aSTamas Berghammer // /proc/{pid}/maps is supported. Assume we don't support map entries 1607a6f5795aSTamas Berghammer // via procfs. 1608a6321a8eSPavel Labath LLDB_LOG(log, 1609a6321a8eSPavel Labath "failed to find any procfs maps entries, assuming no support " 1610a6321a8eSPavel Labath "for memory region metadata retrieval"); 1611a6f5795aSTamas Berghammer m_supports_mem_region = LazyBool::eLazyBoolNo; 1612a6f5795aSTamas Berghammer error.SetErrorString("not supported"); 1613a6f5795aSTamas Berghammer return error; 1614a6f5795aSTamas Berghammer } 1615a6f5795aSTamas Berghammer 1616a6321a8eSPavel Labath LLDB_LOG(log, "read {0} memory region entries from /proc/{1}/maps", 1617a6321a8eSPavel Labath m_mem_region_cache.size(), GetID()); 1618a6f5795aSTamas Berghammer 1619a6f5795aSTamas Berghammer // We support memory retrieval, remember that. 1620a6f5795aSTamas Berghammer m_supports_mem_region = LazyBool::eLazyBoolYes; 1621a6f5795aSTamas Berghammer return Error(); 1622a6f5795aSTamas Berghammer } 1623a6f5795aSTamas Berghammer 1624b9c1b51eSKate Stone void NativeProcessLinux::DoStopIDBumped(uint32_t newBumpId) { 1625a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1626a6321a8eSPavel Labath LLDB_LOG(log, "newBumpId={0}", newBumpId); 1627a6321a8eSPavel Labath LLDB_LOG(log, "clearing {0} entries from memory region cache", 1628a6321a8eSPavel Labath m_mem_region_cache.size()); 1629af245d11STodd Fiala m_mem_region_cache.clear(); 1630af245d11STodd Fiala } 1631af245d11STodd Fiala 1632b9c1b51eSKate Stone Error NativeProcessLinux::AllocateMemory(size_t size, uint32_t permissions, 1633b9c1b51eSKate Stone lldb::addr_t &addr) { 1634af245d11STodd Fiala // FIXME implementing this requires the equivalent of 1635af245d11STodd Fiala // InferiorCallPOSIX::InferiorCallMmap, which depends on 1636af245d11STodd Fiala // functional ThreadPlans working with Native*Protocol. 1637af245d11STodd Fiala #if 1 1638af245d11STodd Fiala return Error("not implemented yet"); 1639af245d11STodd Fiala #else 1640af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 1641af245d11STodd Fiala 1642af245d11STodd Fiala unsigned prot = 0; 1643af245d11STodd Fiala if (permissions & lldb::ePermissionsReadable) 1644af245d11STodd Fiala prot |= eMmapProtRead; 1645af245d11STodd Fiala if (permissions & lldb::ePermissionsWritable) 1646af245d11STodd Fiala prot |= eMmapProtWrite; 1647af245d11STodd Fiala if (permissions & lldb::ePermissionsExecutable) 1648af245d11STodd Fiala prot |= eMmapProtExec; 1649af245d11STodd Fiala 1650af245d11STodd Fiala // TODO implement this directly in NativeProcessLinux 1651af245d11STodd Fiala // (and lift to NativeProcessPOSIX if/when that class is 1652af245d11STodd Fiala // refactored out). 1653af245d11STodd Fiala if (InferiorCallMmap(this, addr, 0, size, prot, 1654af245d11STodd Fiala eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { 1655af245d11STodd Fiala m_addr_to_mmap_size[addr] = size; 1656665be50eSMehdi Amini return Error(); 1657af245d11STodd Fiala } else { 1658af245d11STodd Fiala addr = LLDB_INVALID_ADDRESS; 1659b9c1b51eSKate Stone return Error("unable to allocate %" PRIu64 1660b9c1b51eSKate Stone " bytes of memory with permissions %s", 1661b9c1b51eSKate Stone size, GetPermissionsAsCString(permissions)); 1662af245d11STodd Fiala } 1663af245d11STodd Fiala #endif 1664af245d11STodd Fiala } 1665af245d11STodd Fiala 1666b9c1b51eSKate Stone Error NativeProcessLinux::DeallocateMemory(lldb::addr_t addr) { 1667af245d11STodd Fiala // FIXME see comments in AllocateMemory - required lower-level 1668af245d11STodd Fiala // bits not in place yet (ThreadPlans) 1669af245d11STodd Fiala return Error("not implemented"); 1670af245d11STodd Fiala } 1671af245d11STodd Fiala 1672b9c1b51eSKate Stone lldb::addr_t NativeProcessLinux::GetSharedLibraryInfoAddress() { 1673af245d11STodd Fiala // punt on this for now 1674af245d11STodd Fiala return LLDB_INVALID_ADDRESS; 1675af245d11STodd Fiala } 1676af245d11STodd Fiala 1677b9c1b51eSKate Stone size_t NativeProcessLinux::UpdateThreads() { 1678af245d11STodd Fiala // The NativeProcessLinux monitoring threads are always up to date 1679af245d11STodd Fiala // with respect to thread state and they keep the thread list 1680af245d11STodd Fiala // populated properly. All this method needs to do is return the 1681af245d11STodd Fiala // thread count. 1682af245d11STodd Fiala return m_threads.size(); 1683af245d11STodd Fiala } 1684af245d11STodd Fiala 1685b9c1b51eSKate Stone bool NativeProcessLinux::GetArchitecture(ArchSpec &arch) const { 1686af245d11STodd Fiala arch = m_arch; 1687af245d11STodd Fiala return true; 1688af245d11STodd Fiala } 1689af245d11STodd Fiala 1690b9c1b51eSKate Stone Error NativeProcessLinux::GetSoftwareBreakpointPCOffset( 1691b9c1b51eSKate Stone uint32_t &actual_opcode_size) { 1692af245d11STodd Fiala // FIXME put this behind a breakpoint protocol class that can be 1693af245d11STodd Fiala // set per architecture. Need ARM, MIPS support here. 1694af245d11STodd Fiala static const uint8_t g_i386_opcode[] = {0xCC}; 1695bb00d0b6SUlrich Weigand static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; 1696af245d11STodd Fiala 1697b9c1b51eSKate Stone switch (m_arch.GetMachine()) { 1698af245d11STodd Fiala case llvm::Triple::x86: 1699af245d11STodd Fiala case llvm::Triple::x86_64: 1700af245d11STodd Fiala actual_opcode_size = static_cast<uint32_t>(sizeof(g_i386_opcode)); 1701665be50eSMehdi Amini return Error(); 1702af245d11STodd Fiala 1703bb00d0b6SUlrich Weigand case llvm::Triple::systemz: 1704bb00d0b6SUlrich Weigand actual_opcode_size = static_cast<uint32_t>(sizeof(g_s390x_opcode)); 1705665be50eSMehdi Amini return Error(); 1706bb00d0b6SUlrich Weigand 1707ff7fd900STamas Berghammer case llvm::Triple::arm: 1708ff7fd900STamas Berghammer case llvm::Triple::aarch64: 1709e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64: 1710e8659b5dSMohit K. Bhakkad case llvm::Triple::mips64el: 1711ce815e45SSagar Thakur case llvm::Triple::mips: 1712ce815e45SSagar Thakur case llvm::Triple::mipsel: 1713ff7fd900STamas Berghammer // On these architectures the PC don't get updated for breakpoint hits 1714c60c9452SJaydeep Patil actual_opcode_size = 0; 1715665be50eSMehdi Amini return Error(); 1716e8659b5dSMohit K. Bhakkad 1717af245d11STodd Fiala default: 1718af245d11STodd Fiala assert(false && "CPU type not supported!"); 1719af245d11STodd Fiala return Error("CPU type not supported"); 1720af245d11STodd Fiala } 1721af245d11STodd Fiala } 1722af245d11STodd Fiala 1723b9c1b51eSKate Stone Error NativeProcessLinux::SetBreakpoint(lldb::addr_t addr, uint32_t size, 1724b9c1b51eSKate Stone bool hardware) { 1725af245d11STodd Fiala if (hardware) 1726af245d11STodd Fiala return Error("NativeProcessLinux does not support hardware breakpoints"); 1727af245d11STodd Fiala else 1728af245d11STodd Fiala return SetSoftwareBreakpoint(addr, size); 1729af245d11STodd Fiala } 1730af245d11STodd Fiala 1731b9c1b51eSKate Stone Error NativeProcessLinux::GetSoftwareBreakpointTrapOpcode( 1732b9c1b51eSKate Stone size_t trap_opcode_size_hint, size_t &actual_opcode_size, 1733b9c1b51eSKate Stone const uint8_t *&trap_opcode_bytes) { 173463c8be95STamas Berghammer // FIXME put this behind a breakpoint protocol class that can be set per 173563c8be95STamas Berghammer // architecture. Need MIPS support here. 17362afc5966STodd Fiala static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4}; 1737be379e15STamas Berghammer // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the 1738be379e15STamas Berghammer // linux kernel does otherwise. 1739be379e15STamas Berghammer static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7}; 1740af245d11STodd Fiala static const uint8_t g_i386_opcode[] = {0xCC}; 17413df471c3SMohit K. Bhakkad static const uint8_t g_mips64_opcode[] = {0x00, 0x00, 0x00, 0x0d}; 17422c2acf96SMohit K. Bhakkad static const uint8_t g_mips64el_opcode[] = {0x0d, 0x00, 0x00, 0x00}; 1743bb00d0b6SUlrich Weigand static const uint8_t g_s390x_opcode[] = {0x00, 0x01}; 1744be379e15STamas Berghammer static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde}; 1745af245d11STodd Fiala 1746b9c1b51eSKate Stone switch (m_arch.GetMachine()) { 17472afc5966STodd Fiala case llvm::Triple::aarch64: 17482afc5966STodd Fiala trap_opcode_bytes = g_aarch64_opcode; 17492afc5966STodd Fiala actual_opcode_size = sizeof(g_aarch64_opcode); 1750665be50eSMehdi Amini return Error(); 17512afc5966STodd Fiala 175263c8be95STamas Berghammer case llvm::Triple::arm: 1753b9c1b51eSKate Stone switch (trap_opcode_size_hint) { 175463c8be95STamas Berghammer case 2: 175563c8be95STamas Berghammer trap_opcode_bytes = g_thumb_breakpoint_opcode; 175663c8be95STamas Berghammer actual_opcode_size = sizeof(g_thumb_breakpoint_opcode); 1757665be50eSMehdi Amini return Error(); 175863c8be95STamas Berghammer case 4: 175963c8be95STamas Berghammer trap_opcode_bytes = g_arm_breakpoint_opcode; 176063c8be95STamas Berghammer actual_opcode_size = sizeof(g_arm_breakpoint_opcode); 1761665be50eSMehdi Amini return Error(); 176263c8be95STamas Berghammer default: 176363c8be95STamas Berghammer assert(false && "Unrecognised trap opcode size hint!"); 176463c8be95STamas Berghammer return Error("Unrecognised trap opcode size hint!"); 176563c8be95STamas Berghammer } 176663c8be95STamas Berghammer 1767af245d11STodd Fiala case llvm::Triple::x86: 1768af245d11STodd Fiala case llvm::Triple::x86_64: 1769af245d11STodd Fiala trap_opcode_bytes = g_i386_opcode; 1770af245d11STodd Fiala actual_opcode_size = sizeof(g_i386_opcode); 1771665be50eSMehdi Amini return Error(); 1772af245d11STodd Fiala 1773ce815e45SSagar Thakur case llvm::Triple::mips: 17743df471c3SMohit K. Bhakkad case llvm::Triple::mips64: 17753df471c3SMohit K. Bhakkad trap_opcode_bytes = g_mips64_opcode; 17763df471c3SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64_opcode); 1777665be50eSMehdi Amini return Error(); 17783df471c3SMohit K. Bhakkad 1779ce815e45SSagar Thakur case llvm::Triple::mipsel: 17802c2acf96SMohit K. Bhakkad case llvm::Triple::mips64el: 17812c2acf96SMohit K. Bhakkad trap_opcode_bytes = g_mips64el_opcode; 17822c2acf96SMohit K. Bhakkad actual_opcode_size = sizeof(g_mips64el_opcode); 1783665be50eSMehdi Amini return Error(); 17842c2acf96SMohit K. Bhakkad 1785bb00d0b6SUlrich Weigand case llvm::Triple::systemz: 1786bb00d0b6SUlrich Weigand trap_opcode_bytes = g_s390x_opcode; 1787bb00d0b6SUlrich Weigand actual_opcode_size = sizeof(g_s390x_opcode); 1788665be50eSMehdi Amini return Error(); 1789bb00d0b6SUlrich Weigand 1790af245d11STodd Fiala default: 1791af245d11STodd Fiala assert(false && "CPU type not supported!"); 1792af245d11STodd Fiala return Error("CPU type not supported"); 1793af245d11STodd Fiala } 1794af245d11STodd Fiala } 1795af245d11STodd Fiala 1796af245d11STodd Fiala #if 0 1797af245d11STodd Fiala ProcessMessage::CrashReason 1798af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info) 1799af245d11STodd Fiala { 1800af245d11STodd Fiala ProcessMessage::CrashReason reason; 1801af245d11STodd Fiala assert(info->si_signo == SIGSEGV); 1802af245d11STodd Fiala 1803af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1804af245d11STodd Fiala 1805af245d11STodd Fiala switch (info->si_code) 1806af245d11STodd Fiala { 1807af245d11STodd Fiala default: 1808af245d11STodd Fiala assert(false && "unexpected si_code for SIGSEGV"); 1809af245d11STodd Fiala break; 1810af245d11STodd Fiala case SI_KERNEL: 1811af245d11STodd Fiala // Linux will occasionally send spurious SI_KERNEL codes. 1812af245d11STodd Fiala // (this is poorly documented in sigaction) 1813af245d11STodd Fiala // One way to get this is via unaligned SIMD loads. 1814af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; // for lack of anything better 1815af245d11STodd Fiala break; 1816af245d11STodd Fiala case SEGV_MAPERR: 1817af245d11STodd Fiala reason = ProcessMessage::eInvalidAddress; 1818af245d11STodd Fiala break; 1819af245d11STodd Fiala case SEGV_ACCERR: 1820af245d11STodd Fiala reason = ProcessMessage::ePrivilegedAddress; 1821af245d11STodd Fiala break; 1822af245d11STodd Fiala } 1823af245d11STodd Fiala 1824af245d11STodd Fiala return reason; 1825af245d11STodd Fiala } 1826af245d11STodd Fiala #endif 1827af245d11STodd Fiala 1828af245d11STodd Fiala #if 0 1829af245d11STodd Fiala ProcessMessage::CrashReason 1830af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info) 1831af245d11STodd Fiala { 1832af245d11STodd Fiala ProcessMessage::CrashReason reason; 1833af245d11STodd Fiala assert(info->si_signo == SIGILL); 1834af245d11STodd Fiala 1835af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1836af245d11STodd Fiala 1837af245d11STodd Fiala switch (info->si_code) 1838af245d11STodd Fiala { 1839af245d11STodd Fiala default: 1840af245d11STodd Fiala assert(false && "unexpected si_code for SIGILL"); 1841af245d11STodd Fiala break; 1842af245d11STodd Fiala case ILL_ILLOPC: 1843af245d11STodd Fiala reason = ProcessMessage::eIllegalOpcode; 1844af245d11STodd Fiala break; 1845af245d11STodd Fiala case ILL_ILLOPN: 1846af245d11STodd Fiala reason = ProcessMessage::eIllegalOperand; 1847af245d11STodd Fiala break; 1848af245d11STodd Fiala case ILL_ILLADR: 1849af245d11STodd Fiala reason = ProcessMessage::eIllegalAddressingMode; 1850af245d11STodd Fiala break; 1851af245d11STodd Fiala case ILL_ILLTRP: 1852af245d11STodd Fiala reason = ProcessMessage::eIllegalTrap; 1853af245d11STodd Fiala break; 1854af245d11STodd Fiala case ILL_PRVOPC: 1855af245d11STodd Fiala reason = ProcessMessage::ePrivilegedOpcode; 1856af245d11STodd Fiala break; 1857af245d11STodd Fiala case ILL_PRVREG: 1858af245d11STodd Fiala reason = ProcessMessage::ePrivilegedRegister; 1859af245d11STodd Fiala break; 1860af245d11STodd Fiala case ILL_COPROC: 1861af245d11STodd Fiala reason = ProcessMessage::eCoprocessorError; 1862af245d11STodd Fiala break; 1863af245d11STodd Fiala case ILL_BADSTK: 1864af245d11STodd Fiala reason = ProcessMessage::eInternalStackError; 1865af245d11STodd Fiala break; 1866af245d11STodd Fiala } 1867af245d11STodd Fiala 1868af245d11STodd Fiala return reason; 1869af245d11STodd Fiala } 1870af245d11STodd Fiala #endif 1871af245d11STodd Fiala 1872af245d11STodd Fiala #if 0 1873af245d11STodd Fiala ProcessMessage::CrashReason 1874af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info) 1875af245d11STodd Fiala { 1876af245d11STodd Fiala ProcessMessage::CrashReason reason; 1877af245d11STodd Fiala assert(info->si_signo == SIGFPE); 1878af245d11STodd Fiala 1879af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1880af245d11STodd Fiala 1881af245d11STodd Fiala switch (info->si_code) 1882af245d11STodd Fiala { 1883af245d11STodd Fiala default: 1884af245d11STodd Fiala assert(false && "unexpected si_code for SIGFPE"); 1885af245d11STodd Fiala break; 1886af245d11STodd Fiala case FPE_INTDIV: 1887af245d11STodd Fiala reason = ProcessMessage::eIntegerDivideByZero; 1888af245d11STodd Fiala break; 1889af245d11STodd Fiala case FPE_INTOVF: 1890af245d11STodd Fiala reason = ProcessMessage::eIntegerOverflow; 1891af245d11STodd Fiala break; 1892af245d11STodd Fiala case FPE_FLTDIV: 1893af245d11STodd Fiala reason = ProcessMessage::eFloatDivideByZero; 1894af245d11STodd Fiala break; 1895af245d11STodd Fiala case FPE_FLTOVF: 1896af245d11STodd Fiala reason = ProcessMessage::eFloatOverflow; 1897af245d11STodd Fiala break; 1898af245d11STodd Fiala case FPE_FLTUND: 1899af245d11STodd Fiala reason = ProcessMessage::eFloatUnderflow; 1900af245d11STodd Fiala break; 1901af245d11STodd Fiala case FPE_FLTRES: 1902af245d11STodd Fiala reason = ProcessMessage::eFloatInexactResult; 1903af245d11STodd Fiala break; 1904af245d11STodd Fiala case FPE_FLTINV: 1905af245d11STodd Fiala reason = ProcessMessage::eFloatInvalidOperation; 1906af245d11STodd Fiala break; 1907af245d11STodd Fiala case FPE_FLTSUB: 1908af245d11STodd Fiala reason = ProcessMessage::eFloatSubscriptRange; 1909af245d11STodd Fiala break; 1910af245d11STodd Fiala } 1911af245d11STodd Fiala 1912af245d11STodd Fiala return reason; 1913af245d11STodd Fiala } 1914af245d11STodd Fiala #endif 1915af245d11STodd Fiala 1916af245d11STodd Fiala #if 0 1917af245d11STodd Fiala ProcessMessage::CrashReason 1918af245d11STodd Fiala NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info) 1919af245d11STodd Fiala { 1920af245d11STodd Fiala ProcessMessage::CrashReason reason; 1921af245d11STodd Fiala assert(info->si_signo == SIGBUS); 1922af245d11STodd Fiala 1923af245d11STodd Fiala reason = ProcessMessage::eInvalidCrashReason; 1924af245d11STodd Fiala 1925af245d11STodd Fiala switch (info->si_code) 1926af245d11STodd Fiala { 1927af245d11STodd Fiala default: 1928af245d11STodd Fiala assert(false && "unexpected si_code for SIGBUS"); 1929af245d11STodd Fiala break; 1930af245d11STodd Fiala case BUS_ADRALN: 1931af245d11STodd Fiala reason = ProcessMessage::eIllegalAlignment; 1932af245d11STodd Fiala break; 1933af245d11STodd Fiala case BUS_ADRERR: 1934af245d11STodd Fiala reason = ProcessMessage::eIllegalAddress; 1935af245d11STodd Fiala break; 1936af245d11STodd Fiala case BUS_OBJERR: 1937af245d11STodd Fiala reason = ProcessMessage::eHardwareError; 1938af245d11STodd Fiala break; 1939af245d11STodd Fiala } 1940af245d11STodd Fiala 1941af245d11STodd Fiala return reason; 1942af245d11STodd Fiala } 1943af245d11STodd Fiala #endif 1944af245d11STodd Fiala 1945b9c1b51eSKate Stone Error NativeProcessLinux::ReadMemory(lldb::addr_t addr, void *buf, size_t size, 1946b9c1b51eSKate Stone size_t &bytes_read) { 1947df7c6995SPavel Labath if (ProcessVmReadvSupported()) { 1948b9c1b51eSKate Stone // The process_vm_readv path is about 50 times faster than ptrace api. We 1949b9c1b51eSKate Stone // want to use 1950df7c6995SPavel Labath // this syscall if it is supported. 1951df7c6995SPavel Labath 1952df7c6995SPavel Labath const ::pid_t pid = GetID(); 1953df7c6995SPavel Labath 1954df7c6995SPavel Labath struct iovec local_iov, remote_iov; 1955df7c6995SPavel Labath local_iov.iov_base = buf; 1956df7c6995SPavel Labath local_iov.iov_len = size; 1957df7c6995SPavel Labath remote_iov.iov_base = reinterpret_cast<void *>(addr); 1958df7c6995SPavel Labath remote_iov.iov_len = size; 1959df7c6995SPavel Labath 1960df7c6995SPavel Labath bytes_read = process_vm_readv(pid, &local_iov, 1, &remote_iov, 1, 0); 1961df7c6995SPavel Labath const bool success = bytes_read == size; 1962df7c6995SPavel Labath 1963a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 1964a6321a8eSPavel Labath LLDB_LOG(log, 1965a6321a8eSPavel Labath "using process_vm_readv to read {0} bytes from inferior " 1966a6321a8eSPavel Labath "address {1:x}: {2}", 1967a6321a8eSPavel Labath size, addr, success ? "Success" : strerror(errno)); 1968df7c6995SPavel Labath 1969df7c6995SPavel Labath if (success) 1970665be50eSMehdi Amini return Error(); 1971a6321a8eSPavel Labath // else the call failed for some reason, let's retry the read using ptrace 1972b9c1b51eSKate Stone // api. 1973df7c6995SPavel Labath } 1974df7c6995SPavel Labath 197519cbe96aSPavel Labath unsigned char *dst = static_cast<unsigned char *>(buf); 197619cbe96aSPavel Labath size_t remainder; 197719cbe96aSPavel Labath long data; 197819cbe96aSPavel Labath 1979a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 1980a6321a8eSPavel Labath LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 198119cbe96aSPavel Labath 1982b9c1b51eSKate Stone for (bytes_read = 0; bytes_read < size; bytes_read += remainder) { 1983b9c1b51eSKate Stone Error error = NativeProcessLinux::PtraceWrapper( 1984b9c1b51eSKate Stone PTRACE_PEEKDATA, GetID(), (void *)addr, nullptr, 0, &data); 1985a6321a8eSPavel Labath if (error.Fail()) 198619cbe96aSPavel Labath return error; 198719cbe96aSPavel Labath 198819cbe96aSPavel Labath remainder = size - bytes_read; 198919cbe96aSPavel Labath remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; 199019cbe96aSPavel Labath 199119cbe96aSPavel Labath // Copy the data into our buffer 1992f6ef187bSMohit K. Bhakkad memcpy(dst, &data, remainder); 199319cbe96aSPavel Labath 1994a6321a8eSPavel Labath LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data); 199519cbe96aSPavel Labath addr += k_ptrace_word_size; 199619cbe96aSPavel Labath dst += k_ptrace_word_size; 199719cbe96aSPavel Labath } 1998665be50eSMehdi Amini return Error(); 1999af245d11STodd Fiala } 2000af245d11STodd Fiala 2001b9c1b51eSKate Stone Error NativeProcessLinux::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, 2002b9c1b51eSKate Stone size_t size, 2003b9c1b51eSKate Stone size_t &bytes_read) { 20043eb4b458SChaoren Lin Error error = ReadMemory(addr, buf, size, bytes_read); 2005b9c1b51eSKate Stone if (error.Fail()) 2006b9c1b51eSKate Stone return error; 20073eb4b458SChaoren Lin return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); 20083eb4b458SChaoren Lin } 20093eb4b458SChaoren Lin 2010b9c1b51eSKate Stone Error NativeProcessLinux::WriteMemory(lldb::addr_t addr, const void *buf, 2011b9c1b51eSKate Stone size_t size, size_t &bytes_written) { 201219cbe96aSPavel Labath const unsigned char *src = static_cast<const unsigned char *>(buf); 201319cbe96aSPavel Labath size_t remainder; 201419cbe96aSPavel Labath Error error; 201519cbe96aSPavel Labath 2016a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); 2017a6321a8eSPavel Labath LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); 201819cbe96aSPavel Labath 2019b9c1b51eSKate Stone for (bytes_written = 0; bytes_written < size; bytes_written += remainder) { 202019cbe96aSPavel Labath remainder = size - bytes_written; 202119cbe96aSPavel Labath remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; 202219cbe96aSPavel Labath 2023b9c1b51eSKate Stone if (remainder == k_ptrace_word_size) { 202419cbe96aSPavel Labath unsigned long data = 0; 2025f6ef187bSMohit K. Bhakkad memcpy(&data, src, k_ptrace_word_size); 202619cbe96aSPavel Labath 2027a6321a8eSPavel Labath LLDB_LOG(log, "[{0:x}]:{1:x}", addr, data); 2028b9c1b51eSKate Stone error = NativeProcessLinux::PtraceWrapper(PTRACE_POKEDATA, GetID(), 2029b9c1b51eSKate Stone (void *)addr, (void *)data); 2030a6321a8eSPavel Labath if (error.Fail()) 203119cbe96aSPavel Labath return error; 2032b9c1b51eSKate Stone } else { 203319cbe96aSPavel Labath unsigned char buff[8]; 203419cbe96aSPavel Labath size_t bytes_read; 203519cbe96aSPavel Labath error = ReadMemory(addr, buff, k_ptrace_word_size, bytes_read); 2036a6321a8eSPavel Labath if (error.Fail()) 203719cbe96aSPavel Labath return error; 203819cbe96aSPavel Labath 203919cbe96aSPavel Labath memcpy(buff, src, remainder); 204019cbe96aSPavel Labath 204119cbe96aSPavel Labath size_t bytes_written_rec; 204219cbe96aSPavel Labath error = WriteMemory(addr, buff, k_ptrace_word_size, bytes_written_rec); 2043a6321a8eSPavel Labath if (error.Fail()) 204419cbe96aSPavel Labath return error; 204519cbe96aSPavel Labath 2046a6321a8eSPavel Labath LLDB_LOG(log, "[{0:x}]:{1:x} ({2:x})", addr, *(const unsigned long *)src, 2047b9c1b51eSKate Stone *(unsigned long *)buff); 204819cbe96aSPavel Labath } 204919cbe96aSPavel Labath 205019cbe96aSPavel Labath addr += k_ptrace_word_size; 205119cbe96aSPavel Labath src += k_ptrace_word_size; 205219cbe96aSPavel Labath } 205319cbe96aSPavel Labath return error; 2054af245d11STodd Fiala } 2055af245d11STodd Fiala 2056b9c1b51eSKate Stone Error NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) { 205719cbe96aSPavel Labath return PtraceWrapper(PTRACE_GETSIGINFO, tid, nullptr, siginfo); 2058af245d11STodd Fiala } 2059af245d11STodd Fiala 2060b9c1b51eSKate Stone Error NativeProcessLinux::GetEventMessage(lldb::tid_t tid, 2061b9c1b51eSKate Stone unsigned long *message) { 206219cbe96aSPavel Labath return PtraceWrapper(PTRACE_GETEVENTMSG, tid, nullptr, message); 2063af245d11STodd Fiala } 2064af245d11STodd Fiala 2065b9c1b51eSKate Stone Error NativeProcessLinux::Detach(lldb::tid_t tid) { 206697ccc294SChaoren Lin if (tid == LLDB_INVALID_THREAD_ID) 2067665be50eSMehdi Amini return Error(); 206897ccc294SChaoren Lin 206919cbe96aSPavel Labath return PtraceWrapper(PTRACE_DETACH, tid); 2070af245d11STodd Fiala } 2071af245d11STodd Fiala 2072b9c1b51eSKate Stone bool NativeProcessLinux::HasThreadNoLock(lldb::tid_t thread_id) { 2073b9c1b51eSKate Stone for (auto thread_sp : m_threads) { 2074af245d11STodd Fiala assert(thread_sp && "thread list should not contain NULL threads"); 2075b9c1b51eSKate Stone if (thread_sp->GetID() == thread_id) { 2076af245d11STodd Fiala // We have this thread. 2077af245d11STodd Fiala return true; 2078af245d11STodd Fiala } 2079af245d11STodd Fiala } 2080af245d11STodd Fiala 2081af245d11STodd Fiala // We don't have this thread. 2082af245d11STodd Fiala return false; 2083af245d11STodd Fiala } 2084af245d11STodd Fiala 2085b9c1b51eSKate Stone bool NativeProcessLinux::StopTrackingThread(lldb::tid_t thread_id) { 2086a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2087a6321a8eSPavel Labath LLDB_LOG(log, "tid: {0})", thread_id); 20881dbc6c9cSPavel Labath 20891dbc6c9cSPavel Labath bool found = false; 2090b9c1b51eSKate Stone for (auto it = m_threads.begin(); it != m_threads.end(); ++it) { 2091b9c1b51eSKate Stone if (*it && ((*it)->GetID() == thread_id)) { 2092af245d11STodd Fiala m_threads.erase(it); 20931dbc6c9cSPavel Labath found = true; 20941dbc6c9cSPavel Labath break; 2095af245d11STodd Fiala } 2096af245d11STodd Fiala } 2097af245d11STodd Fiala 20989eb1ecb9SPavel Labath SignalIfAllThreadsStopped(); 20991dbc6c9cSPavel Labath return found; 2100af245d11STodd Fiala } 2101af245d11STodd Fiala 2102b9c1b51eSKate Stone NativeThreadLinuxSP NativeProcessLinux::AddThread(lldb::tid_t thread_id) { 2103a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); 2104a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id); 2105af245d11STodd Fiala 2106b9c1b51eSKate Stone assert(!HasThreadNoLock(thread_id) && 2107b9c1b51eSKate Stone "attempted to add a thread by id that already exists"); 2108af245d11STodd Fiala 2109af245d11STodd Fiala // If this is the first thread, save it as the current thread 2110af245d11STodd Fiala if (m_threads.empty()) 2111af245d11STodd Fiala SetCurrentThreadID(thread_id); 2112af245d11STodd Fiala 2113f9077782SPavel Labath auto thread_sp = std::make_shared<NativeThreadLinux>(this, thread_id); 2114af245d11STodd Fiala m_threads.push_back(thread_sp); 2115af245d11STodd Fiala return thread_sp; 2116af245d11STodd Fiala } 2117af245d11STodd Fiala 2118b9c1b51eSKate Stone Error NativeProcessLinux::FixupBreakpointPCAsNeeded(NativeThreadLinux &thread) { 2119a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS)); 2120af245d11STodd Fiala 2121af245d11STodd Fiala Error error; 2122af245d11STodd Fiala 2123b9c1b51eSKate Stone // Find out the size of a breakpoint (might depend on where we are in the 2124b9c1b51eSKate Stone // code). 2125b9cc0c75SPavel Labath NativeRegisterContextSP context_sp = thread.GetRegisterContext(); 2126b9c1b51eSKate Stone if (!context_sp) { 2127af245d11STodd Fiala error.SetErrorString("cannot get a NativeRegisterContext for the thread"); 2128a6321a8eSPavel Labath LLDB_LOG(log, "failed: {0}", error); 2129af245d11STodd Fiala return error; 2130af245d11STodd Fiala } 2131af245d11STodd Fiala 2132af245d11STodd Fiala uint32_t breakpoint_size = 0; 2133b9cc0c75SPavel Labath error = GetSoftwareBreakpointPCOffset(breakpoint_size); 2134b9c1b51eSKate Stone if (error.Fail()) { 2135a6321a8eSPavel Labath LLDB_LOG(log, "GetBreakpointSize() failed: {0}", error); 2136af245d11STodd Fiala return error; 2137a6321a8eSPavel Labath } else 2138a6321a8eSPavel Labath LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size); 2139af245d11STodd Fiala 2140b9c1b51eSKate Stone // First try probing for a breakpoint at a software breakpoint location: PC - 2141b9c1b51eSKate Stone // breakpoint size. 2142b9c1b51eSKate Stone const lldb::addr_t initial_pc_addr = 2143b9c1b51eSKate Stone context_sp->GetPCfromBreakpointLocation(); 2144af245d11STodd Fiala lldb::addr_t breakpoint_addr = initial_pc_addr; 2145b9c1b51eSKate Stone if (breakpoint_size > 0) { 2146af245d11STodd Fiala // Do not allow breakpoint probe to wrap around. 21473eb4b458SChaoren Lin if (breakpoint_addr >= breakpoint_size) 21483eb4b458SChaoren Lin breakpoint_addr -= breakpoint_size; 2149af245d11STodd Fiala } 2150af245d11STodd Fiala 2151af245d11STodd Fiala // Check if we stopped because of a breakpoint. 2152af245d11STodd Fiala NativeBreakpointSP breakpoint_sp; 2153af245d11STodd Fiala error = m_breakpoint_list.GetBreakpoint(breakpoint_addr, breakpoint_sp); 2154b9c1b51eSKate Stone if (!error.Success() || !breakpoint_sp) { 2155af245d11STodd Fiala // We didn't find one at a software probe location. Nothing to do. 2156a6321a8eSPavel Labath LLDB_LOG(log, 2157a6321a8eSPavel Labath "pid {0} no lldb breakpoint found at current pc with " 2158a6321a8eSPavel Labath "adjustment: {1}", 2159a6321a8eSPavel Labath GetID(), breakpoint_addr); 2160665be50eSMehdi Amini return Error(); 2161af245d11STodd Fiala } 2162af245d11STodd Fiala 2163af245d11STodd Fiala // If the breakpoint is not a software breakpoint, nothing to do. 2164b9c1b51eSKate Stone if (!breakpoint_sp->IsSoftwareBreakpoint()) { 2165a6321a8eSPavel Labath LLDB_LOG( 2166a6321a8eSPavel Labath log, 2167a6321a8eSPavel Labath "pid {0} breakpoint found at {1:x}, not software, nothing to adjust", 2168a6321a8eSPavel Labath GetID(), breakpoint_addr); 2169665be50eSMehdi Amini return Error(); 2170af245d11STodd Fiala } 2171af245d11STodd Fiala 2172af245d11STodd Fiala // 2173af245d11STodd Fiala // We have a software breakpoint and need to adjust the PC. 2174af245d11STodd Fiala // 2175af245d11STodd Fiala 2176af245d11STodd Fiala // Sanity check. 2177b9c1b51eSKate Stone if (breakpoint_size == 0) { 2178af245d11STodd Fiala // Nothing to do! How did we get here? 2179a6321a8eSPavel Labath LLDB_LOG(log, 2180a6321a8eSPavel Labath "pid {0} breakpoint found at {1:x}, it is software, but the " 2181a6321a8eSPavel Labath "size is zero, nothing to do (unexpected)", 2182a6321a8eSPavel Labath GetID(), breakpoint_addr); 2183665be50eSMehdi Amini return Error(); 2184af245d11STodd Fiala } 2185af245d11STodd Fiala 2186af245d11STodd Fiala // Change the program counter. 2187a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(), 2188a6321a8eSPavel Labath thread.GetID(), initial_pc_addr, breakpoint_addr); 2189af245d11STodd Fiala 2190af245d11STodd Fiala error = context_sp->SetPC(breakpoint_addr); 2191b9c1b51eSKate Stone if (error.Fail()) { 2192a6321a8eSPavel Labath LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(), 2193a6321a8eSPavel Labath thread.GetID(), error); 2194af245d11STodd Fiala return error; 2195af245d11STodd Fiala } 2196af245d11STodd Fiala 2197af245d11STodd Fiala return error; 2198af245d11STodd Fiala } 2199fa03ad2eSChaoren Lin 2200b9c1b51eSKate Stone Error NativeProcessLinux::GetLoadedModuleFileSpec(const char *module_path, 2201b9c1b51eSKate Stone FileSpec &file_spec) { 2202a6f5795aSTamas Berghammer Error error = PopulateMemoryRegionCache(); 2203a6f5795aSTamas Berghammer if (error.Fail()) 2204a6f5795aSTamas Berghammer return error; 2205a6f5795aSTamas Berghammer 22067cb18bf5STamas Berghammer FileSpec module_file_spec(module_path, true); 22077cb18bf5STamas Berghammer 22087cb18bf5STamas Berghammer file_spec.Clear(); 2209a6f5795aSTamas Berghammer for (const auto &it : m_mem_region_cache) { 2210a6f5795aSTamas Berghammer if (it.second.GetFilename() == module_file_spec.GetFilename()) { 2211a6f5795aSTamas Berghammer file_spec = it.second; 2212a6f5795aSTamas Berghammer return Error(); 2213a6f5795aSTamas Berghammer } 2214a6f5795aSTamas Berghammer } 22157cb18bf5STamas Berghammer return Error("Module file (%s) not found in /proc/%" PRIu64 "/maps file!", 22167cb18bf5STamas Berghammer module_file_spec.GetFilename().AsCString(), GetID()); 22177cb18bf5STamas Berghammer } 2218c076559aSPavel Labath 2219b9c1b51eSKate Stone Error NativeProcessLinux::GetFileLoadAddress(const llvm::StringRef &file_name, 2220b9c1b51eSKate Stone lldb::addr_t &load_addr) { 2221783bfc8cSTamas Berghammer load_addr = LLDB_INVALID_ADDRESS; 2222a6f5795aSTamas Berghammer Error error = PopulateMemoryRegionCache(); 2223a6f5795aSTamas Berghammer if (error.Fail()) 2224783bfc8cSTamas Berghammer return error; 2225a6f5795aSTamas Berghammer 2226a6f5795aSTamas Berghammer FileSpec file(file_name, false); 2227a6f5795aSTamas Berghammer for (const auto &it : m_mem_region_cache) { 2228a6f5795aSTamas Berghammer if (it.second == file) { 2229a6f5795aSTamas Berghammer load_addr = it.first.GetRange().GetRangeBase(); 2230a6f5795aSTamas Berghammer return Error(); 2231a6f5795aSTamas Berghammer } 2232a6f5795aSTamas Berghammer } 2233a6f5795aSTamas Berghammer return Error("No load address found for specified file."); 2234783bfc8cSTamas Berghammer } 2235783bfc8cSTamas Berghammer 2236b9c1b51eSKate Stone NativeThreadLinuxSP NativeProcessLinux::GetThreadByID(lldb::tid_t tid) { 2237b9c1b51eSKate Stone return std::static_pointer_cast<NativeThreadLinux>( 2238b9c1b51eSKate Stone NativeProcessProtocol::GetThreadByID(tid)); 2239f9077782SPavel Labath } 2240f9077782SPavel Labath 2241b9c1b51eSKate Stone Error NativeProcessLinux::ResumeThread(NativeThreadLinux &thread, 2242b9c1b51eSKate Stone lldb::StateType state, int signo) { 2243a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2244a6321a8eSPavel Labath LLDB_LOG(log, "tid: {0}", thread.GetID()); 2245c076559aSPavel Labath 2246c076559aSPavel Labath // Before we do the resume below, first check if we have a pending 2247108c325dSPavel Labath // stop notification that is currently waiting for 22480e1d729bSPavel Labath // all threads to stop. This is potentially a buggy situation since 2249c076559aSPavel Labath // we're ostensibly waiting for threads to stop before we send out the 2250c076559aSPavel Labath // pending notification, and here we are resuming one before we send 2251c076559aSPavel Labath // out the pending stop notification. 2252a6321a8eSPavel Labath if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) { 2253a6321a8eSPavel Labath LLDB_LOG(log, 2254a6321a8eSPavel Labath "about to resume tid {0} per explicit request but we have a " 2255a6321a8eSPavel Labath "pending stop notification (tid {1}) that is actively " 2256a6321a8eSPavel Labath "waiting for this thread to stop. Valid sequence of events?", 2257a6321a8eSPavel Labath thread.GetID(), m_pending_notification_tid); 2258c076559aSPavel Labath } 2259c076559aSPavel Labath 2260c076559aSPavel Labath // Request a resume. We expect this to be synchronous and the system 2261c076559aSPavel Labath // to reflect it is running after this completes. 2262b9c1b51eSKate Stone switch (state) { 2263b9c1b51eSKate Stone case eStateRunning: { 2264605b51b8SPavel Labath const auto resume_result = thread.Resume(signo); 22650e1d729bSPavel Labath if (resume_result.Success()) 22660e1d729bSPavel Labath SetState(eStateRunning, true); 22670e1d729bSPavel Labath return resume_result; 2268c076559aSPavel Labath } 2269b9c1b51eSKate Stone case eStateStepping: { 2270605b51b8SPavel Labath const auto step_result = thread.SingleStep(signo); 22710e1d729bSPavel Labath if (step_result.Success()) 22720e1d729bSPavel Labath SetState(eStateRunning, true); 22730e1d729bSPavel Labath return step_result; 22740e1d729bSPavel Labath } 22750e1d729bSPavel Labath default: 2276a6321a8eSPavel Labath LLDB_LOG(log, "Unhandled state {0}.", StateAsCString(state)); 22770e1d729bSPavel Labath llvm_unreachable("Unhandled state for resume"); 22780e1d729bSPavel Labath } 2279c076559aSPavel Labath } 2280c076559aSPavel Labath 2281c076559aSPavel Labath //===----------------------------------------------------------------------===// 2282c076559aSPavel Labath 2283b9c1b51eSKate Stone void NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid) { 2284a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2285a6321a8eSPavel Labath LLDB_LOG(log, "about to process event: (triggering_tid: {0})", 2286a6321a8eSPavel Labath triggering_tid); 2287c076559aSPavel Labath 22880e1d729bSPavel Labath m_pending_notification_tid = triggering_tid; 22890e1d729bSPavel Labath 22900e1d729bSPavel Labath // Request a stop for all the thread stops that need to be stopped 22910e1d729bSPavel Labath // and are not already known to be stopped. 2292b9c1b51eSKate Stone for (const auto &thread_sp : m_threads) { 22930e1d729bSPavel Labath if (StateIsRunningState(thread_sp->GetState())) 22940e1d729bSPavel Labath static_pointer_cast<NativeThreadLinux>(thread_sp)->RequestStop(); 22950e1d729bSPavel Labath } 22960e1d729bSPavel Labath 22970e1d729bSPavel Labath SignalIfAllThreadsStopped(); 2298a6321a8eSPavel Labath LLDB_LOG(log, "event processing done"); 2299c076559aSPavel Labath } 2300c076559aSPavel Labath 2301b9c1b51eSKate Stone void NativeProcessLinux::SignalIfAllThreadsStopped() { 23020e1d729bSPavel Labath if (m_pending_notification_tid == LLDB_INVALID_THREAD_ID) 23030e1d729bSPavel Labath return; // No pending notification. Nothing to do. 23040e1d729bSPavel Labath 2305b9c1b51eSKate Stone for (const auto &thread_sp : m_threads) { 23060e1d729bSPavel Labath if (StateIsRunningState(thread_sp->GetState())) 23070e1d729bSPavel Labath return; // Some threads are still running. Don't signal yet. 23080e1d729bSPavel Labath } 23090e1d729bSPavel Labath 23100e1d729bSPavel Labath // We have a pending notification and all threads have stopped. 2311b9c1b51eSKate Stone Log *log( 2312b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); 23139eb1ecb9SPavel Labath 2314b9c1b51eSKate Stone // Clear any temporary breakpoints we used to implement software single 2315b9c1b51eSKate Stone // stepping. 2316b9c1b51eSKate Stone for (const auto &thread_info : m_threads_stepping_with_breakpoint) { 23179eb1ecb9SPavel Labath Error error = RemoveBreakpoint(thread_info.second); 23189eb1ecb9SPavel Labath if (error.Fail()) 2319a6321a8eSPavel Labath LLDB_LOG(log, "pid = {0} remove stepping breakpoint: {1}", 2320a6321a8eSPavel Labath thread_info.first, error); 23219eb1ecb9SPavel Labath } 23229eb1ecb9SPavel Labath m_threads_stepping_with_breakpoint.clear(); 23239eb1ecb9SPavel Labath 23249eb1ecb9SPavel Labath // Notify the delegate about the stop 23250e1d729bSPavel Labath SetCurrentThreadID(m_pending_notification_tid); 2326ed89c7feSPavel Labath SetState(StateType::eStateStopped, true); 23270e1d729bSPavel Labath m_pending_notification_tid = LLDB_INVALID_THREAD_ID; 2328c076559aSPavel Labath } 2329c076559aSPavel Labath 2330b9c1b51eSKate Stone void NativeProcessLinux::ThreadWasCreated(NativeThreadLinux &thread) { 2331a6321a8eSPavel Labath Log *const log = ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD); 2332a6321a8eSPavel Labath LLDB_LOG(log, "tid: {0}", thread.GetID()); 23331dbc6c9cSPavel Labath 2334b9c1b51eSKate Stone if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID && 2335b9c1b51eSKate Stone StateIsRunningState(thread.GetState())) { 2336b9c1b51eSKate Stone // We will need to wait for this new thread to stop as well before firing 2337b9c1b51eSKate Stone // the 2338c076559aSPavel Labath // notification. 2339f9077782SPavel Labath thread.RequestStop(); 2340c076559aSPavel Labath } 2341c076559aSPavel Labath } 2342068f8a7eSTamas Berghammer 2343b9c1b51eSKate Stone void NativeProcessLinux::SigchldHandler() { 2344a6321a8eSPavel Labath Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); 234519cbe96aSPavel Labath // Process all pending waitpid notifications. 2346b9c1b51eSKate Stone while (true) { 234719cbe96aSPavel Labath int status = -1; 234819cbe96aSPavel Labath ::pid_t wait_pid = waitpid(-1, &status, __WALL | __WNOTHREAD | WNOHANG); 234919cbe96aSPavel Labath 235019cbe96aSPavel Labath if (wait_pid == 0) 235119cbe96aSPavel Labath break; // We are done. 235219cbe96aSPavel Labath 2353b9c1b51eSKate Stone if (wait_pid == -1) { 235419cbe96aSPavel Labath if (errno == EINTR) 235519cbe96aSPavel Labath continue; 235619cbe96aSPavel Labath 235719cbe96aSPavel Labath Error error(errno, eErrorTypePOSIX); 2358a6321a8eSPavel Labath LLDB_LOG(log, "waitpid (-1, &status, _) failed: {0}", error); 235919cbe96aSPavel Labath break; 236019cbe96aSPavel Labath } 236119cbe96aSPavel Labath 236219cbe96aSPavel Labath bool exited = false; 236319cbe96aSPavel Labath int signal = 0; 236419cbe96aSPavel Labath int exit_status = 0; 236519cbe96aSPavel Labath const char *status_cstr = nullptr; 2366b9c1b51eSKate Stone if (WIFSTOPPED(status)) { 236719cbe96aSPavel Labath signal = WSTOPSIG(status); 236819cbe96aSPavel Labath status_cstr = "STOPPED"; 2369b9c1b51eSKate Stone } else if (WIFEXITED(status)) { 237019cbe96aSPavel Labath exit_status = WEXITSTATUS(status); 237119cbe96aSPavel Labath status_cstr = "EXITED"; 237219cbe96aSPavel Labath exited = true; 2373b9c1b51eSKate Stone } else if (WIFSIGNALED(status)) { 237419cbe96aSPavel Labath signal = WTERMSIG(status); 237519cbe96aSPavel Labath status_cstr = "SIGNALED"; 237619cbe96aSPavel Labath if (wait_pid == static_cast<::pid_t>(GetID())) { 237719cbe96aSPavel Labath exited = true; 237819cbe96aSPavel Labath exit_status = -1; 237919cbe96aSPavel Labath } 2380b9c1b51eSKate Stone } else 238119cbe96aSPavel Labath status_cstr = "(\?\?\?)"; 238219cbe96aSPavel Labath 2383a6321a8eSPavel Labath LLDB_LOG(log, 2384a6321a8eSPavel Labath "waitpid (-1, &status, _) => pid = {0}, status = {1:x} " 2385a6321a8eSPavel Labath "({2}), signal = {3}, exit_state = {4}", 2386a6321a8eSPavel Labath wait_pid, status, status_cstr, signal, exit_status); 238719cbe96aSPavel Labath 238819cbe96aSPavel Labath MonitorCallback(wait_pid, exited, signal, exit_status); 238919cbe96aSPavel Labath } 2390068f8a7eSTamas Berghammer } 2391068f8a7eSTamas Berghammer 2392068f8a7eSTamas Berghammer // Wrapper for ptrace to catch errors and log calls. 2393b9c1b51eSKate Stone // Note that ptrace sets errno on error because -1 can be a valid result (i.e. 2394b9c1b51eSKate Stone // for PTRACE_PEEK*) 2395b9c1b51eSKate Stone Error NativeProcessLinux::PtraceWrapper(int req, lldb::pid_t pid, void *addr, 2396b9c1b51eSKate Stone void *data, size_t data_size, 2397b9c1b51eSKate Stone long *result) { 23984a9babb2SPavel Labath Error error; 23994a9babb2SPavel Labath long int ret; 2400068f8a7eSTamas Berghammer 2401068f8a7eSTamas Berghammer Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); 2402068f8a7eSTamas Berghammer 2403068f8a7eSTamas Berghammer PtraceDisplayBytes(req, data, data_size); 2404068f8a7eSTamas Berghammer 2405068f8a7eSTamas Berghammer errno = 0; 2406068f8a7eSTamas Berghammer if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) 2407b9c1b51eSKate Stone ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), 2408b9c1b51eSKate Stone *(unsigned int *)addr, data); 2409068f8a7eSTamas Berghammer else 2410b9c1b51eSKate Stone ret = ptrace(static_cast<__ptrace_request>(req), static_cast<::pid_t>(pid), 2411b9c1b51eSKate Stone addr, data); 2412068f8a7eSTamas Berghammer 24134a9babb2SPavel Labath if (ret == -1) 2414068f8a7eSTamas Berghammer error.SetErrorToErrno(); 2415068f8a7eSTamas Berghammer 24164a9babb2SPavel Labath if (result) 24174a9babb2SPavel Labath *result = ret; 24184a9babb2SPavel Labath 2419a6321a8eSPavel Labath LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3}, {4}, {5})={6:x}", req, pid, addr, 2420b9c1b51eSKate Stone data, data_size, ret); 2421068f8a7eSTamas Berghammer 2422068f8a7eSTamas Berghammer PtraceDisplayBytes(req, data, data_size); 2423068f8a7eSTamas Berghammer 2424a6321a8eSPavel Labath if (error.Fail()) 2425a6321a8eSPavel Labath LLDB_LOG(log, "ptrace() failed: {0}", error); 2426068f8a7eSTamas Berghammer 24274a9babb2SPavel Labath return error; 2428068f8a7eSTamas Berghammer } 2429