Index: vendor/lldb/dist-release_60/source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp =================================================================== --- vendor/lldb/dist-release_60/source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp (revision 329403) +++ vendor/lldb/dist-release_60/source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp (revision 329404) @@ -1,436 +1,374 @@ //===-- PlatformNetBSD.cpp -------------------------------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #include "PlatformNetBSD.h" #include "lldb/Host/Config.h" // C Includes #include #ifndef LLDB_DISABLE_POSIX #include #endif // C++ Includes // Other libraries and framework includes // Project includes #include "lldb/Core/Debugger.h" #include "lldb/Core/PluginManager.h" #include "lldb/Core/State.h" #include "lldb/Host/HostInfo.h" #include "lldb/Target/Process.h" #include "lldb/Target/Target.h" #include "lldb/Utility/FileSpec.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/Status.h" #include "lldb/Utility/StreamString.h" // Define these constants from NetBSD mman.h for use when targeting // remote netbsd systems even when host has different values. #define MAP_PRIVATE 0x0002 #define MAP_ANON 0x1000 using namespace lldb; using namespace lldb_private; using namespace lldb_private::platform_netbsd; static uint32_t g_initialize_count = 0; //------------------------------------------------------------------ PlatformSP PlatformNetBSD::CreateInstance(bool force, const ArchSpec *arch) { Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM)); - if (log) { - const char *arch_name; - if (arch && arch->GetArchitectureName()) - arch_name = arch->GetArchitectureName(); - else - arch_name = ""; + LLDB_LOG(log, "force = {0}, arch=({1}, {2})", force, + arch ? arch->GetArchitectureName() : "", + arch ? arch->GetTriple().getTriple() : ""); - const char *triple_cstr = - arch ? arch->GetTriple().getTriple().c_str() : ""; - - log->Printf("PlatformNetBSD::%s(force=%s, arch={%s,%s})", __FUNCTION__, - force ? "true" : "false", arch_name, triple_cstr); - } - bool create = force; if (create == false && arch && arch->IsValid()) { const llvm::Triple &triple = arch->GetTriple(); switch (triple.getOS()) { case llvm::Triple::NetBSD: create = true; break; default: break; } } + LLDB_LOG(log, "create = {0}", create); if (create) { - if (log) - log->Printf("PlatformNetBSD::%s() creating remote-netbsd platform", - __FUNCTION__); return PlatformSP(new PlatformNetBSD(false)); } - - if (log) - log->Printf( - "PlatformNetBSD::%s() aborting creation of remote-netbsd platform", - __FUNCTION__); - return PlatformSP(); } ConstString PlatformNetBSD::GetPluginNameStatic(bool is_host) { if (is_host) { static ConstString g_host_name(Platform::GetHostPlatformName()); return g_host_name; } else { static ConstString g_remote_name("remote-netbsd"); return g_remote_name; } } const char *PlatformNetBSD::GetPluginDescriptionStatic(bool is_host) { if (is_host) return "Local NetBSD user platform plug-in."; else return "Remote NetBSD user platform plug-in."; } ConstString PlatformNetBSD::GetPluginName() { return GetPluginNameStatic(IsHost()); } void PlatformNetBSD::Initialize() { PlatformPOSIX::Initialize(); if (g_initialize_count++ == 0) { #if defined(__NetBSD__) PlatformSP default_platform_sp(new PlatformNetBSD(true)); default_platform_sp->SetSystemArchitecture(HostInfo::GetArchitecture()); Platform::SetHostPlatform(default_platform_sp); #endif PluginManager::RegisterPlugin( PlatformNetBSD::GetPluginNameStatic(false), PlatformNetBSD::GetPluginDescriptionStatic(false), PlatformNetBSD::CreateInstance, nullptr); } } void PlatformNetBSD::Terminate() { if (g_initialize_count > 0) { if (--g_initialize_count == 0) { PluginManager::UnregisterPlugin(PlatformNetBSD::CreateInstance); } } PlatformPOSIX::Terminate(); } //------------------------------------------------------------------ /// Default Constructor //------------------------------------------------------------------ PlatformNetBSD::PlatformNetBSD(bool is_host) : PlatformPOSIX(is_host) // This is the local host platform {} PlatformNetBSD::~PlatformNetBSD() = default; bool PlatformNetBSD::GetSupportedArchitectureAtIndex(uint32_t idx, ArchSpec &arch) { if (IsHost()) { ArchSpec hostArch = HostInfo::GetArchitecture(HostInfo::eArchKindDefault); if (hostArch.GetTriple().isOSNetBSD()) { if (idx == 0) { arch = hostArch; return arch.IsValid(); } else if (idx == 1) { // If the default host architecture is 64-bit, look for a 32-bit variant if (hostArch.IsValid() && hostArch.GetTriple().isArch64Bit()) { arch = HostInfo::GetArchitecture(HostInfo::eArchKind32); return arch.IsValid(); } } } } else { if (m_remote_platform_sp) return m_remote_platform_sp->GetSupportedArchitectureAtIndex(idx, arch); llvm::Triple triple; // Set the OS to NetBSD triple.setOS(llvm::Triple::NetBSD); // Set the architecture switch (idx) { case 0: triple.setArchName("x86_64"); break; case 1: triple.setArchName("i386"); break; default: return false; } // Leave the vendor as "llvm::Triple:UnknownVendor" and don't specify the // vendor by // calling triple.SetVendorName("unknown") so that it is a "unspecified // unknown". // This means when someone calls triple.GetVendorName() it will return an // empty string // which indicates that the vendor can be set when two architectures are // merged // Now set the triple into "arch" and return true arch.SetTriple(triple); return true; } return false; } void PlatformNetBSD::GetStatus(Stream &strm) { Platform::GetStatus(strm); #ifndef LLDB_DISABLE_POSIX // Display local kernel information only when we are running in host mode. // Otherwise, we would end up printing non-NetBSD information (when running // on Mac OS for example). if (IsHost()) { struct utsname un; if (uname(&un)) return; strm.Printf(" Kernel: %s\n", un.sysname); strm.Printf(" Release: %s\n", un.release); strm.Printf(" Version: %s\n", un.version); } #endif } int32_t PlatformNetBSD::GetResumeCountForLaunchInfo(ProcessLaunchInfo &launch_info) { int32_t resume_count = 0; // Always resume past the initial stop when we use eLaunchFlagDebug if (launch_info.GetFlags().Test(eLaunchFlagDebug)) { // Resume past the stop for the final exec into the true inferior. ++resume_count; } // If we're not launching a shell, we're done. const FileSpec &shell = launch_info.GetShell(); if (!shell) return resume_count; std::string shell_string = shell.GetPath(); // We're in a shell, so for sure we have to resume past the shell exec. ++resume_count; // Figure out what shell we're planning on using. const char *shell_name = strrchr(shell_string.c_str(), '/'); if (shell_name == NULL) shell_name = shell_string.c_str(); else shell_name++; if (strcmp(shell_name, "csh") == 0 || strcmp(shell_name, "tcsh") == 0 || strcmp(shell_name, "zsh") == 0 || strcmp(shell_name, "sh") == 0) { // These shells seem to re-exec themselves. Add another resume. ++resume_count; } return resume_count; } bool PlatformNetBSD::CanDebugProcess() { if (IsHost()) { return true; } else { // If we're connected, we can debug. return IsConnected(); } } // For local debugging, NetBSD will override the debug logic to use llgs-launch -// rather than -// lldb-launch, llgs-attach. This differs from current lldb-launch, -// debugserver-attach -// approach on MacOSX. -lldb::ProcessSP PlatformNetBSD::DebugProcess( - ProcessLaunchInfo &launch_info, Debugger &debugger, - Target *target, // Can be NULL, if NULL create a new - // target, else use existing one - Status &error) { +// rather than lldb-launch, llgs-attach. This differs from current lldb-launch, +// debugserver-attach approach on MacOSX. +lldb::ProcessSP +PlatformNetBSD::DebugProcess(ProcessLaunchInfo &launch_info, Debugger &debugger, + Target *target, // Can be NULL, if NULL create a new + // target, else use existing one + Status &error) { Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM)); - if (log) - log->Printf("PlatformNetBSD::%s entered (target %p)", __FUNCTION__, - static_cast(target)); + LLDB_LOG(log, "target {0}", target); // If we're a remote host, use standard behavior from parent class. if (!IsHost()) return PlatformPOSIX::DebugProcess(launch_info, debugger, target, error); // // For local debugging, we'll insist on having ProcessGDBRemote create the // process. // ProcessSP process_sp; // Make sure we stop at the entry point launch_info.GetFlags().Set(eLaunchFlagDebug); // We always launch the process we are going to debug in a separate process // group, since then we can handle ^C interrupts ourselves w/o having to worry // about the target getting them as well. launch_info.SetLaunchInSeparateProcessGroup(true); // Ensure we have a target. if (target == nullptr) { - if (log) - log->Printf("PlatformNetBSD::%s creating new target", __FUNCTION__); - + LLDB_LOG(log, "creating new target"); TargetSP new_target_sp; error = debugger.GetTargetList().CreateTarget(debugger, "", "", false, nullptr, new_target_sp); if (error.Fail()) { - if (log) - log->Printf("PlatformNetBSD::%s failed to create new target: %s", - __FUNCTION__, error.AsCString()); + LLDB_LOG(log, "failed to create new target: {0}", error); return process_sp; } target = new_target_sp.get(); if (!target) { error.SetErrorString("CreateTarget() returned nullptr"); - if (log) - log->Printf("PlatformNetBSD::%s failed: %s", __FUNCTION__, - error.AsCString()); + LLDB_LOG(log, "error: {0}", error); return process_sp; } - } else { - if (log) - log->Printf("PlatformNetBSD::%s using provided target", __FUNCTION__); } // Mark target as currently selected target. debugger.GetTargetList().SetSelectedTarget(target); // Now create the gdb-remote process. - if (log) - log->Printf( - "PlatformNetBSD::%s having target create process with gdb-remote plugin", - __FUNCTION__); + LLDB_LOG(log, "having target create process with gdb-remote plugin"); process_sp = target->CreateProcess( launch_info.GetListenerForProcess(debugger), "gdb-remote", nullptr); if (!process_sp) { error.SetErrorString("CreateProcess() failed for gdb-remote process"); - if (log) - log->Printf("PlatformNetBSD::%s failed: %s", __FUNCTION__, - error.AsCString()); + LLDB_LOG(log, "error: {0}", error); return process_sp; - } else { - if (log) - log->Printf("PlatformNetBSD::%s successfully created process", - __FUNCTION__); } + LLDB_LOG(log, "successfully created process"); // Adjust launch for a hijacker. ListenerSP listener_sp; if (!launch_info.GetHijackListener()) { - if (log) - log->Printf("PlatformNetBSD::%s setting up hijacker", __FUNCTION__); - + LLDB_LOG(log, "setting up hijacker"); listener_sp = Listener::MakeListener("lldb.PlatformNetBSD.DebugProcess.hijack"); launch_info.SetHijackListener(listener_sp); process_sp->HijackProcessEvents(listener_sp); } // Log file actions. if (log) { - log->Printf( - "PlatformNetBSD::%s launching process with the following file actions:", - __FUNCTION__); - + LLDB_LOG(log, "launching process with the following file actions:"); StreamString stream; size_t i = 0; const FileAction *file_action; while ((file_action = launch_info.GetFileActionAtIndex(i++)) != nullptr) { file_action->Dump(stream); - log->PutCString(stream.GetData()); + LLDB_LOG(log, "{0}", stream.GetData()); stream.Clear(); } } // Do the launch. error = process_sp->Launch(launch_info); if (error.Success()) { // Handle the hijacking of process events. if (listener_sp) { const StateType state = process_sp->WaitForProcessToStop( llvm::None, NULL, false, listener_sp); - if (state == eStateStopped) { - if (log) - log->Printf("PlatformNetBSD::%s pid %" PRIu64 " state %s\n", - __FUNCTION__, process_sp->GetID(), StateAsCString(state)); - } else { - if (log) - log->Printf("PlatformNetBSD::%s pid %" PRIu64 - " state is not stopped - %s\n", - __FUNCTION__, process_sp->GetID(), StateAsCString(state)); - } + LLDB_LOG(log, "pid {0} state {0}", process_sp->GetID(), state); } // Hook up process PTY if we have one (which we should for local debugging // with llgs). int pty_fd = launch_info.GetPTY().ReleaseMasterFileDescriptor(); if (pty_fd != PseudoTerminal::invalid_fd) { process_sp->SetSTDIOFileDescriptor(pty_fd); - if (log) - log->Printf("PlatformNetBSD::%s pid %" PRIu64 - " hooked up STDIO pty to process", - __FUNCTION__, process_sp->GetID()); - } else { - if (log) - log->Printf("PlatformNetBSD::%s pid %" PRIu64 - " not using process STDIO pty", - __FUNCTION__, process_sp->GetID()); - } + LLDB_LOG(log, "hooked up STDIO pty to process"); + } else + LLDB_LOG(log, "not using process STDIO pty"); } else { - if (log) - log->Printf("PlatformNetBSD::%s process launch failed: %s", __FUNCTION__, - error.AsCString()); + LLDB_LOG(log, "process launch failed: {0}", error); // FIXME figure out appropriate cleanup here. Do we delete the target? Do // we delete the process? Does our caller do that? } return process_sp; } void PlatformNetBSD::CalculateTrapHandlerSymbolNames() { m_trap_handlers.push_back(ConstString("_sigtramp")); } MmapArgList PlatformNetBSD::GetMmapArgumentList(const ArchSpec &arch, addr_t addr, addr_t length, unsigned prot, unsigned flags, addr_t fd, addr_t offset) { uint64_t flags_platform = 0; if (flags & eMmapFlagsPrivate) flags_platform |= MAP_PRIVATE; if (flags & eMmapFlagsAnon) flags_platform |= MAP_ANON; MmapArgList args({addr, length, prot, flags_platform, fd, offset}); return args; } Index: vendor/lldb/dist-release_60/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp =================================================================== --- vendor/lldb/dist-release_60/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp (revision 329403) +++ vendor/lldb/dist-release_60/source/Plugins/Process/NetBSD/NativeProcessNetBSD.cpp (revision 329404) @@ -1,914 +1,914 @@ //===-- NativeProcessNetBSD.cpp ------------------------------- -*- C++ -*-===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #include "NativeProcessNetBSD.h" // C Includes // C++ Includes // Other libraries and framework includes #include "Plugins/Process/POSIX/ProcessPOSIXLog.h" #include "lldb/Core/State.h" #include "lldb/Host/HostProcess.h" #include "lldb/Host/common/NativeBreakpoint.h" #include "lldb/Host/common/NativeRegisterContext.h" #include "lldb/Host/posix/ProcessLauncherPosixFork.h" #include "lldb/Target/Process.h" #include "llvm/Support/Errno.h" // System includes - They have to be included after framework includes because // they define some // macros which collide with variable names in other modules // clang-format off #include #include #include #include #include #include #include // clang-format on using namespace lldb; using namespace lldb_private; using namespace lldb_private::process_netbsd; using namespace llvm; // Simple helper function to ensure flags are enabled on the given file // descriptor. static Status EnsureFDFlags(int fd, int flags) { Status error; int status = fcntl(fd, F_GETFL); if (status == -1) { error.SetErrorToErrno(); return error; } if (fcntl(fd, F_SETFL, status | flags) == -1) { error.SetErrorToErrno(); return error; } return error; } // ----------------------------------------------------------------------------- // Public Static Methods // ----------------------------------------------------------------------------- llvm::Expected> NativeProcessNetBSD::Factory::Launch(ProcessLaunchInfo &launch_info, NativeDelegate &native_delegate, MainLoop &mainloop) const { Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); Status status; ::pid_t pid = ProcessLauncherPosixFork() .LaunchProcess(launch_info, status) .GetProcessId(); LLDB_LOG(log, "pid = {0:x}", pid); if (status.Fail()) { LLDB_LOG(log, "failed to launch process: {0}", status); return status.ToError(); } // Wait for the child process to trap on its call to execve. int wstatus; ::pid_t wpid = llvm::sys::RetryAfterSignal(-1, ::waitpid, pid, &wstatus, 0); assert(wpid == pid); (void)wpid; if (!WIFSTOPPED(wstatus)) { LLDB_LOG(log, "Could not sync with inferior process: wstatus={1}", WaitStatus::Decode(wstatus)); return llvm::make_error("Could not sync with inferior process", llvm::inconvertibleErrorCode()); } LLDB_LOG(log, "inferior started, now in stopped state"); ArchSpec arch; if ((status = ResolveProcessArchitecture(pid, arch)).Fail()) return status.ToError(); // Set the architecture to the exe architecture. LLDB_LOG(log, "pid = {0:x}, detected architecture {1}", pid, arch.GetArchitectureName()); std::unique_ptr process_up(new NativeProcessNetBSD( pid, launch_info.GetPTY().ReleaseMasterFileDescriptor(), native_delegate, arch, mainloop)); status = process_up->ReinitializeThreads(); if (status.Fail()) return status.ToError(); for (const auto &thread : process_up->m_threads) static_cast(*thread).SetStoppedBySignal(SIGSTOP); - process_up->SetState(StateType::eStateStopped); + process_up->SetState(StateType::eStateStopped, false); return std::move(process_up); } llvm::Expected> NativeProcessNetBSD::Factory::Attach( lldb::pid_t pid, NativeProcessProtocol::NativeDelegate &native_delegate, MainLoop &mainloop) const { Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); LLDB_LOG(log, "pid = {0:x}", pid); // Retrieve the architecture for the running process. ArchSpec arch; Status status = ResolveProcessArchitecture(pid, arch); if (!status.Success()) return status.ToError(); std::unique_ptr process_up( new NativeProcessNetBSD(pid, -1, native_delegate, arch, mainloop)); status = process_up->Attach(); if (!status.Success()) return status.ToError(); return std::move(process_up); } // ----------------------------------------------------------------------------- // Public Instance Methods // ----------------------------------------------------------------------------- NativeProcessNetBSD::NativeProcessNetBSD(::pid_t pid, int terminal_fd, NativeDelegate &delegate, const ArchSpec &arch, MainLoop &mainloop) : NativeProcessProtocol(pid, terminal_fd, delegate), m_arch(arch) { if (m_terminal_fd != -1) { Status status = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); assert(status.Success()); } Status status; m_sigchld_handle = mainloop.RegisterSignal( SIGCHLD, [this](MainLoopBase &) { SigchldHandler(); }, status); assert(m_sigchld_handle && status.Success()); } // Handles all waitpid events from the inferior process. void NativeProcessNetBSD::MonitorCallback(lldb::pid_t pid, int signal) { switch (signal) { case SIGTRAP: return MonitorSIGTRAP(pid); case SIGSTOP: return MonitorSIGSTOP(pid); default: return MonitorSignal(pid, signal); } } void NativeProcessNetBSD::MonitorExited(lldb::pid_t pid, WaitStatus status) { Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); LLDB_LOG(log, "got exit signal({0}) , pid = {1}", status, pid); /* Stop Tracking All Threads attached to Process */ m_threads.clear(); SetExitStatus(status, true); // Notify delegate that our process has exited. SetState(StateType::eStateExited, true); } void NativeProcessNetBSD::MonitorSIGSTOP(lldb::pid_t pid) { ptrace_siginfo_t info; const auto siginfo_err = PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); // Get details on the signal raised. if (siginfo_err.Success()) { // Handle SIGSTOP from LLGS (LLDB GDB Server) if (info.psi_siginfo.si_code == SI_USER && info.psi_siginfo.si_pid == ::getpid()) { /* Stop Tracking all Threads attached to Process */ for (const auto &thread : m_threads) { static_cast(*thread).SetStoppedBySignal( SIGSTOP, &info.psi_siginfo); } } } } void NativeProcessNetBSD::MonitorSIGTRAP(lldb::pid_t pid) { Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); ptrace_siginfo_t info; const auto siginfo_err = PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); // Get details on the signal raised. if (siginfo_err.Fail()) { return; } switch (info.psi_siginfo.si_code) { case TRAP_BRKPT: for (const auto &thread : m_threads) { static_cast(*thread).SetStoppedByBreakpoint(); FixupBreakpointPCAsNeeded(static_cast(*thread)); } SetState(StateType::eStateStopped, true); break; case TRAP_TRACE: for (const auto &thread : m_threads) static_cast(*thread).SetStoppedByTrace(); SetState(StateType::eStateStopped, true); break; case TRAP_EXEC: { Status error = ReinitializeThreads(); if (error.Fail()) { SetState(StateType::eStateInvalid); return; } // Let our delegate know we have just exec'd. NotifyDidExec(); for (const auto &thread : m_threads) static_cast(*thread).SetStoppedByExec(); SetState(StateType::eStateStopped, true); } break; case TRAP_DBREG: { // If a watchpoint was hit, report it uint32_t wp_index; Status error = static_cast(*m_threads[info.psi_lwpid]) .GetRegisterContext() .GetWatchpointHitIndex( wp_index, (uintptr_t)info.psi_siginfo.si_addr); if (error.Fail()) LLDB_LOG(log, "received error while checking for watchpoint hits, pid = " "{0}, LWP = {1}, error = {2}", GetID(), info.psi_lwpid, error); if (wp_index != LLDB_INVALID_INDEX32) { for (const auto &thread : m_threads) static_cast(*thread).SetStoppedByWatchpoint( wp_index); SetState(StateType::eStateStopped, true); break; } // If a breakpoint was hit, report it uint32_t bp_index; error = static_cast(*m_threads[info.psi_lwpid]) .GetRegisterContext() .GetHardwareBreakHitIndex(bp_index, (uintptr_t)info.psi_siginfo.si_addr); if (error.Fail()) LLDB_LOG(log, "received error while checking for hardware " "breakpoint hits, pid = {0}, LWP = {1}, error = {2}", GetID(), info.psi_lwpid, error); if (bp_index != LLDB_INVALID_INDEX32) { for (const auto &thread : m_threads) static_cast(*thread).SetStoppedByBreakpoint(); SetState(StateType::eStateStopped, true); break; } } break; } } void NativeProcessNetBSD::MonitorSignal(lldb::pid_t pid, int signal) { ptrace_siginfo_t info; const auto siginfo_err = PtraceWrapper(PT_GET_SIGINFO, pid, &info, sizeof(info)); for (const auto &thread : m_threads) { static_cast(*thread).SetStoppedBySignal( info.psi_siginfo.si_signo, &info.psi_siginfo); } SetState(StateType::eStateStopped, true); } Status NativeProcessNetBSD::PtraceWrapper(int req, lldb::pid_t pid, void *addr, int data, int *result) { Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PTRACE)); Status error; int ret; errno = 0; ret = ptrace(req, static_cast<::pid_t>(pid), addr, data); if (ret == -1) error.SetErrorToErrno(); if (result) *result = ret; LLDB_LOG(log, "ptrace({0}, {1}, {2}, {3})={4:x}", req, pid, addr, data, ret); if (error.Fail()) LLDB_LOG(log, "ptrace() failed: {0}", error); return error; } Status NativeProcessNetBSD::GetSoftwareBreakpointPCOffset( uint32_t &actual_opcode_size) { // FIXME put this behind a breakpoint protocol class that can be // set per architecture. Need ARM, MIPS support here. static const uint8_t g_i386_opcode[] = {0xCC}; switch (m_arch.GetMachine()) { case llvm::Triple::x86_64: actual_opcode_size = static_cast(sizeof(g_i386_opcode)); return Status(); default: assert(false && "CPU type not supported!"); return Status("CPU type not supported"); } } Status NativeProcessNetBSD::FixupBreakpointPCAsNeeded(NativeThreadNetBSD &thread) { Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_BREAKPOINTS)); Status error; // Find out the size of a breakpoint (might depend on where we are in the // code). NativeRegisterContext& context = thread.GetRegisterContext(); uint32_t breakpoint_size = 0; error = GetSoftwareBreakpointPCOffset(breakpoint_size); if (error.Fail()) { LLDB_LOG(log, "GetBreakpointSize() failed: {0}", error); return error; } else LLDB_LOG(log, "breakpoint size: {0}", breakpoint_size); // First try probing for a breakpoint at a software breakpoint location: PC // - breakpoint size. const lldb::addr_t initial_pc_addr = context.GetPCfromBreakpointLocation(); lldb::addr_t breakpoint_addr = initial_pc_addr; if (breakpoint_size > 0) { // Do not allow breakpoint probe to wrap around. if (breakpoint_addr >= breakpoint_size) breakpoint_addr -= breakpoint_size; } // Check if we stopped because of a breakpoint. NativeBreakpointSP breakpoint_sp; error = m_breakpoint_list.GetBreakpoint(breakpoint_addr, breakpoint_sp); if (!error.Success() || !breakpoint_sp) { // We didn't find one at a software probe location. Nothing to do. LLDB_LOG(log, "pid {0} no lldb breakpoint found at current pc with " "adjustment: {1}", GetID(), breakpoint_addr); return Status(); } // If the breakpoint is not a software breakpoint, nothing to do. if (!breakpoint_sp->IsSoftwareBreakpoint()) { LLDB_LOG( log, "pid {0} breakpoint found at {1:x}, not software, nothing to adjust", GetID(), breakpoint_addr); return Status(); } // // We have a software breakpoint and need to adjust the PC. // // Sanity check. if (breakpoint_size == 0) { // Nothing to do! How did we get here? LLDB_LOG(log, "pid {0} breakpoint found at {1:x}, it is software, but the " "size is zero, nothing to do (unexpected)", GetID(), breakpoint_addr); return Status(); } // // We have a software breakpoint and need to adjust the PC. // // Sanity check. if (breakpoint_size == 0) { // Nothing to do! How did we get here? LLDB_LOG(log, "pid {0} breakpoint found at {1:x}, it is software, but the " "size is zero, nothing to do (unexpected)", GetID(), breakpoint_addr); return Status(); } // Change the program counter. LLDB_LOG(log, "pid {0} tid {1}: changing PC from {2:x} to {3:x}", GetID(), thread.GetID(), initial_pc_addr, breakpoint_addr); error = context.SetPC(breakpoint_addr); if (error.Fail()) { LLDB_LOG(log, "pid {0} tid {1}: failed to set PC: {2}", GetID(), thread.GetID(), error); return error; } return error; } Status NativeProcessNetBSD::Resume(const ResumeActionList &resume_actions) { Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); LLDB_LOG(log, "pid {0}", GetID()); const auto &thread = m_threads[0]; const ResumeAction *const action = resume_actions.GetActionForThread(thread->GetID(), true); if (action == nullptr) { LLDB_LOG(log, "no action specified for pid {0} tid {1}", GetID(), thread->GetID()); return Status(); } Status error; switch (action->state) { case eStateRunning: { // Run the thread, possibly feeding it the signal. error = NativeProcessNetBSD::PtraceWrapper(PT_CONTINUE, GetID(), (void *)1, action->signal); if (!error.Success()) return error; for (const auto &thread : m_threads) static_cast(*thread).SetRunning(); SetState(eStateRunning, true); break; } case eStateStepping: // Run the thread, possibly feeding it the signal. error = NativeProcessNetBSD::PtraceWrapper(PT_STEP, GetID(), (void *)1, action->signal); if (!error.Success()) return error; for (const auto &thread : m_threads) static_cast(*thread).SetStepping(); SetState(eStateStepping, true); break; case eStateSuspended: case eStateStopped: llvm_unreachable("Unexpected state"); default: return Status("NativeProcessNetBSD::%s (): unexpected state %s specified " "for pid %" PRIu64 ", tid %" PRIu64, __FUNCTION__, StateAsCString(action->state), GetID(), thread->GetID()); } return Status(); } Status NativeProcessNetBSD::Halt() { Status error; if (kill(GetID(), SIGSTOP) != 0) error.SetErrorToErrno(); return error; } Status NativeProcessNetBSD::Detach() { Status error; // Stop monitoring the inferior. m_sigchld_handle.reset(); // Tell ptrace to detach from the process. if (GetID() == LLDB_INVALID_PROCESS_ID) return error; return PtraceWrapper(PT_DETACH, GetID()); } Status NativeProcessNetBSD::Signal(int signo) { Status error; if (kill(GetID(), signo)) error.SetErrorToErrno(); return error; } Status NativeProcessNetBSD::Kill() { Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); LLDB_LOG(log, "pid {0}", GetID()); Status error; switch (m_state) { case StateType::eStateInvalid: case StateType::eStateExited: case StateType::eStateCrashed: case StateType::eStateDetached: case StateType::eStateUnloaded: // Nothing to do - the process is already dead. LLDB_LOG(log, "ignored for PID {0} due to current state: {1}", GetID(), StateAsCString(m_state)); return error; case StateType::eStateConnected: case StateType::eStateAttaching: case StateType::eStateLaunching: case StateType::eStateStopped: case StateType::eStateRunning: case StateType::eStateStepping: case StateType::eStateSuspended: // We can try to kill a process in these states. break; } if (kill(GetID(), SIGKILL) != 0) { error.SetErrorToErrno(); return error; } return error; } Status NativeProcessNetBSD::GetMemoryRegionInfo(lldb::addr_t load_addr, MemoryRegionInfo &range_info) { if (m_supports_mem_region == LazyBool::eLazyBoolNo) { // We're done. return Status("unsupported"); } Status error = PopulateMemoryRegionCache(); if (error.Fail()) { return error; } lldb::addr_t prev_base_address = 0; // FIXME start by finding the last region that is <= target address using // binary search. Data is sorted. // There can be a ton of regions on pthreads apps with lots of threads. for (auto it = m_mem_region_cache.begin(); it != m_mem_region_cache.end(); ++it) { MemoryRegionInfo &proc_entry_info = it->first; // Sanity check assumption that memory map entries are ascending. assert((proc_entry_info.GetRange().GetRangeBase() >= prev_base_address) && "descending memory map entries detected, unexpected"); prev_base_address = proc_entry_info.GetRange().GetRangeBase(); UNUSED_IF_ASSERT_DISABLED(prev_base_address); // If the target address comes before this entry, indicate distance to // next region. if (load_addr < proc_entry_info.GetRange().GetRangeBase()) { range_info.GetRange().SetRangeBase(load_addr); range_info.GetRange().SetByteSize( proc_entry_info.GetRange().GetRangeBase() - load_addr); range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); return error; } else if (proc_entry_info.GetRange().Contains(load_addr)) { // The target address is within the memory region we're processing here. range_info = proc_entry_info; return error; } // The target memory address comes somewhere after the region we just // parsed. } // If we made it here, we didn't find an entry that contained the given // address. Return the // load_addr as start and the amount of bytes betwwen load address and the // end of the memory as size. range_info.GetRange().SetRangeBase(load_addr); range_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); range_info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); range_info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); range_info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); range_info.SetMapped(MemoryRegionInfo::OptionalBool::eNo); return error; } Status NativeProcessNetBSD::PopulateMemoryRegionCache() { Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); // If our cache is empty, pull the latest. There should always be at least // one memory region if memory region handling is supported. if (!m_mem_region_cache.empty()) { LLDB_LOG(log, "reusing {0} cached memory region entries", m_mem_region_cache.size()); return Status(); } struct kinfo_vmentry *vm; size_t count, i; vm = kinfo_getvmmap(GetID(), &count); if (vm == NULL) { m_supports_mem_region = LazyBool::eLazyBoolNo; Status error; error.SetErrorString("not supported"); return error; } for (i = 0; i < count; i++) { MemoryRegionInfo info; info.Clear(); info.GetRange().SetRangeBase(vm[i].kve_start); info.GetRange().SetRangeEnd(vm[i].kve_end); info.SetMapped(MemoryRegionInfo::OptionalBool::eYes); if (vm[i].kve_protection & VM_PROT_READ) info.SetReadable(MemoryRegionInfo::OptionalBool::eYes); else info.SetReadable(MemoryRegionInfo::OptionalBool::eNo); if (vm[i].kve_protection & VM_PROT_WRITE) info.SetWritable(MemoryRegionInfo::OptionalBool::eYes); else info.SetWritable(MemoryRegionInfo::OptionalBool::eNo); if (vm[i].kve_protection & VM_PROT_EXECUTE) info.SetExecutable(MemoryRegionInfo::OptionalBool::eYes); else info.SetExecutable(MemoryRegionInfo::OptionalBool::eNo); if (vm[i].kve_path[0]) info.SetName(vm[i].kve_path); m_mem_region_cache.emplace_back( info, FileSpec(info.GetName().GetCString(), true)); } free(vm); if (m_mem_region_cache.empty()) { // No entries after attempting to read them. This shouldn't happen. // Assume we don't support map entries. LLDB_LOG(log, "failed to find any vmmap entries, assuming no support " "for memory region metadata retrieval"); m_supports_mem_region = LazyBool::eLazyBoolNo; Status error; error.SetErrorString("not supported"); return error; } LLDB_LOG(log, "read {0} memory region entries from process {1}", m_mem_region_cache.size(), GetID()); // We support memory retrieval, remember that. m_supports_mem_region = LazyBool::eLazyBoolYes; return Status(); } Status NativeProcessNetBSD::AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) { return Status("Unimplemented"); } Status NativeProcessNetBSD::DeallocateMemory(lldb::addr_t addr) { return Status("Unimplemented"); } lldb::addr_t NativeProcessNetBSD::GetSharedLibraryInfoAddress() { // punt on this for now return LLDB_INVALID_ADDRESS; } size_t NativeProcessNetBSD::UpdateThreads() { return m_threads.size(); } Status NativeProcessNetBSD::SetBreakpoint(lldb::addr_t addr, uint32_t size, bool hardware) { if (hardware) return Status("NativeProcessNetBSD does not support hardware breakpoints"); else return SetSoftwareBreakpoint(addr, size); } Status NativeProcessNetBSD::GetSoftwareBreakpointTrapOpcode( size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) { static const uint8_t g_i386_opcode[] = {0xCC}; switch (m_arch.GetMachine()) { case llvm::Triple::x86: case llvm::Triple::x86_64: trap_opcode_bytes = g_i386_opcode; actual_opcode_size = sizeof(g_i386_opcode); return Status(); default: assert(false && "CPU type not supported!"); return Status("CPU type not supported"); } } Status NativeProcessNetBSD::GetLoadedModuleFileSpec(const char *module_path, FileSpec &file_spec) { return Status("Unimplemented"); } Status NativeProcessNetBSD::GetFileLoadAddress(const llvm::StringRef &file_name, lldb::addr_t &load_addr) { load_addr = LLDB_INVALID_ADDRESS; return Status(); } void NativeProcessNetBSD::SigchldHandler() { Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_PROCESS)); // Process all pending waitpid notifications. int status; ::pid_t wait_pid = llvm::sys::RetryAfterSignal(-1, waitpid, GetID(), &status, WALLSIG | WNOHANG); if (wait_pid == 0) return; // We are done. if (wait_pid == -1) { Status error(errno, eErrorTypePOSIX); LLDB_LOG(log, "waitpid ({0}, &status, _) failed: {1}", GetID(), error); } WaitStatus wait_status = WaitStatus::Decode(status); bool exited = wait_status.type == WaitStatus::Exit || (wait_status.type == WaitStatus::Signal && wait_pid == static_cast<::pid_t>(GetID())); LLDB_LOG(log, "waitpid ({0}, &status, _) => pid = {1}, status = {2}, exited = {3}", GetID(), wait_pid, status, exited); if (exited) MonitorExited(wait_pid, wait_status); else { assert(wait_status.type == WaitStatus::Stop); MonitorCallback(wait_pid, wait_status.status); } } bool NativeProcessNetBSD::HasThreadNoLock(lldb::tid_t thread_id) { for (const auto &thread : m_threads) { assert(thread && "thread list should not contain NULL threads"); if (thread->GetID() == thread_id) { // We have this thread. return true; } } // We don't have this thread. return false; } NativeThreadNetBSD &NativeProcessNetBSD::AddThread(lldb::tid_t thread_id) { Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_THREAD)); LLDB_LOG(log, "pid {0} adding thread with tid {1}", GetID(), thread_id); assert(!HasThreadNoLock(thread_id) && "attempted to add a thread by id that already exists"); // If this is the first thread, save it as the current thread if (m_threads.empty()) SetCurrentThreadID(thread_id); m_threads.push_back(llvm::make_unique(*this, thread_id)); return static_cast(*m_threads.back()); } Status NativeProcessNetBSD::Attach() { // Attach to the requested process. // An attach will cause the thread to stop with a SIGSTOP. Status status = PtraceWrapper(PT_ATTACH, m_pid); if (status.Fail()) return status; int wstatus; // Need to use WALLSIG otherwise we receive an error with errno=ECHLD // At this point we should have a thread stopped if waitpid succeeds. if ((wstatus = waitpid(m_pid, NULL, WALLSIG)) < 0) return Status(errno, eErrorTypePOSIX); /* Initialize threads */ status = ReinitializeThreads(); if (status.Fail()) return status; for (const auto &thread : m_threads) static_cast(*thread).SetStoppedBySignal(SIGSTOP); // Let our process instance know the thread has stopped. SetState(StateType::eStateStopped); return Status(); } Status NativeProcessNetBSD::ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) { unsigned char *dst = static_cast(buf); struct ptrace_io_desc io; Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); bytes_read = 0; io.piod_op = PIOD_READ_D; io.piod_len = size; do { io.piod_offs = (void *)(addr + bytes_read); io.piod_addr = dst + bytes_read; Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); if (error.Fail()) return error; bytes_read = io.piod_len; io.piod_len = size - bytes_read; } while (bytes_read < size); return Status(); } Status NativeProcessNetBSD::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) { Status error = ReadMemory(addr, buf, size, bytes_read); if (error.Fail()) return error; return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); } Status NativeProcessNetBSD::WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) { const unsigned char *src = static_cast(buf); Status error; struct ptrace_io_desc io; Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_MEMORY)); LLDB_LOG(log, "addr = {0}, buf = {1}, size = {2}", addr, buf, size); bytes_written = 0; io.piod_op = PIOD_WRITE_D; io.piod_len = size; do { io.piod_addr = const_cast(static_cast(src + bytes_written)); io.piod_offs = (void *)(addr + bytes_written); Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); if (error.Fail()) return error; bytes_written = io.piod_len; io.piod_len = size - bytes_written; } while (bytes_written < size); return error; } llvm::ErrorOr> NativeProcessNetBSD::GetAuxvData() const { /* * ELF_AUX_ENTRIES is currently restricted to kernel * ( r. 1.155 specifies 15) * * ptrace(2) returns the whole AUXV including extra fiels after AT_NULL this * information isn't needed. */ size_t auxv_size = 100 * sizeof(AuxInfo); ErrorOr> buf = llvm::MemoryBuffer::getNewMemBuffer(auxv_size); struct ptrace_io_desc io; io.piod_op = PIOD_READ_AUXV; io.piod_offs = 0; io.piod_addr = const_cast(static_cast(buf.get()->getBufferStart())); io.piod_len = auxv_size; Status error = NativeProcessNetBSD::PtraceWrapper(PT_IO, GetID(), &io); if (error.Fail()) return std::error_code(error.GetError(), std::generic_category()); if (io.piod_len < 1) return std::error_code(ECANCELED, std::generic_category()); return buf; } Status NativeProcessNetBSD::ReinitializeThreads() { // Clear old threads m_threads.clear(); // Initialize new thread struct ptrace_lwpinfo info = {}; Status error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info)); if (error.Fail()) { return error; } // Reinitialize from scratch threads and register them in process while (info.pl_lwpid != 0) { AddThread(info.pl_lwpid); error = PtraceWrapper(PT_LWPINFO, GetID(), &info, sizeof(info)); if (error.Fail()) { return error; } } return error; }