diff options
Diffstat (limited to 'source/Plugins/Process')
96 files changed, 21151 insertions, 151 deletions
diff --git a/source/Plugins/Process/CMakeLists.txt b/source/Plugins/Process/CMakeLists.txt new file mode 100644 index 000000000000..d15df94414b3 --- /dev/null +++ b/source/Plugins/Process/CMakeLists.txt @@ -0,0 +1,19 @@ +if (CMAKE_SYSTEM_NAME MATCHES "Linux") +  add_subdirectory(Linux) +  add_subdirectory(POSIX) +elseif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD") +  add_subdirectory(FreeBSD) +  add_subdirectory(POSIX) +elseif (CMAKE_SYSTEM_NAME MATCHES "NetBSD") +  add_subdirectory(POSIX) +elseif (CMAKE_SYSTEM_NAME MATCHES "Windows") +  add_subdirectory(Windows/Live) +  add_subdirectory(Windows/MiniDump) +  add_subdirectory(Windows/Common) +elseif (CMAKE_SYSTEM_NAME MATCHES "Darwin") +  add_subdirectory(MacOSX-Kernel) +endif() +add_subdirectory(gdb-remote) +add_subdirectory(Utility) +add_subdirectory(mach-core) +add_subdirectory(elf-core) diff --git a/source/Plugins/Process/FreeBSD/CMakeLists.txt b/source/Plugins/Process/FreeBSD/CMakeLists.txt new file mode 100644 index 000000000000..c0e3374fef89 --- /dev/null +++ b/source/Plugins/Process/FreeBSD/CMakeLists.txt @@ -0,0 +1,16 @@ +include_directories(.) +include_directories(../POSIX) +include_directories(../Utility) + +add_lldb_library(lldbPluginProcessFreeBSD +  ProcessFreeBSD.cpp +  FreeBSDThread.cpp +  ProcessMonitor.cpp + +  POSIXStopInfo.cpp +  RegisterContextPOSIXProcessMonitor_arm.cpp +  RegisterContextPOSIXProcessMonitor_arm64.cpp +  RegisterContextPOSIXProcessMonitor_powerpc.cpp +  RegisterContextPOSIXProcessMonitor_x86.cpp +  RegisterContextPOSIXProcessMonitor_mips64.cpp +  ) diff --git a/source/Plugins/Process/FreeBSD/Makefile b/source/Plugins/Process/FreeBSD/Makefile new file mode 100644 index 000000000000..7f546540e556 --- /dev/null +++ b/source/Plugins/Process/FreeBSD/Makefile @@ -0,0 +1,17 @@ +##===- source/Plugins/Process/FreeBSD/Makefile ---------------*- Makefile -*-===## +#  +#                     The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +#  +##===----------------------------------------------------------------------===## + +LLDB_LEVEL := ../../../.. +LIBRARYNAME := lldbPluginProcessFreeBSD +BUILD_ARCHIVE = 1 + +# Extend the include path so we may locate UnwindLLDB.h +CPP.Flags += -I$(PROJ_SRC_DIR)/$(LLDB_LEVEL)/source/Plugins/Utility + +include $(LLDB_LEVEL)/Makefile diff --git a/source/Plugins/Process/FreeBSD/ProcessFreeBSD.h b/source/Plugins/Process/FreeBSD/ProcessFreeBSD.h index 3cc46f489875..5f9365418d7a 100644 --- a/source/Plugins/Process/FreeBSD/ProcessFreeBSD.h +++ b/source/Plugins/Process/FreeBSD/ProcessFreeBSD.h @@ -88,7 +88,7 @@ public:      DoAttachToProcessWithID (lldb::pid_t pid, const lldb_private::ProcessAttachInfo &attach_info) override;      lldb_private::Error -    DoLaunch (lldb_private::Module *exe_module,  +    DoLaunch (lldb_private::Module *exe_module,                lldb_private::ProcessLaunchInfo &launch_info) override;      void @@ -160,7 +160,7 @@ public:      UpdateThreadListIfNeeded();      bool -    UpdateThreadList(lldb_private::ThreadList &old_thread_list,  +    UpdateThreadList(lldb_private::ThreadList &old_thread_list,                       lldb_private::ThreadList &new_thread_list) override;      virtual lldb::ByteOrder diff --git a/source/Plugins/Process/FreeBSD/ProcessMonitor.cpp b/source/Plugins/Process/FreeBSD/ProcessMonitor.cpp index ceb527b61d80..cd016fbd4b8c 100644 --- a/source/Plugins/Process/FreeBSD/ProcessMonitor.cpp +++ b/source/Plugins/Process/FreeBSD/ProcessMonitor.cpp @@ -317,7 +317,7 @@ ReadRegOperation::Execute(ProcessMonitor *monitor)          else if (m_size == sizeof(uint64_t))              m_value = *(uint64_t *)(((caddr_t)®s) + m_offset);          else -            memcpy(&m_value, (((caddr_t)®s) + m_offset), m_size); +            memcpy((void *)&m_value, (((caddr_t)®s) + m_offset), m_size);          m_result = true;      }  } @@ -393,7 +393,7 @@ ReadDebugRegOperation::Execute(ProcessMonitor *monitor)          if (m_size == sizeof(uintptr_t))              m_value = *(uintptr_t *)(((caddr_t)®s) + m_offset);          else -            memcpy(&m_value, (((caddr_t)®s) + m_offset), m_size); +            memcpy((void *)&m_value, (((caddr_t)®s) + m_offset), m_size);          m_result = true;      }  } diff --git a/source/Plugins/Process/FreeBSD/RegisterContextPOSIXProcessMonitor_arm64.cpp b/source/Plugins/Process/FreeBSD/RegisterContextPOSIXProcessMonitor_arm64.cpp index 012f4b6e1555..a1a0cab82a15 100644 --- a/source/Plugins/Process/FreeBSD/RegisterContextPOSIXProcessMonitor_arm64.cpp +++ b/source/Plugins/Process/FreeBSD/RegisterContextPOSIXProcessMonitor_arm64.cpp @@ -260,9 +260,7 @@ RegisterContextPOSIXProcessMonitor_arm64::HardwareSingleStep(bool enable)  bool  RegisterContextPOSIXProcessMonitor_arm64::UpdateAfterBreakpoint()  { -    lldb::addr_t pc; - -    if ((pc = GetPC()) == LLDB_INVALID_ADDRESS) +    if (GetPC() == LLDB_INVALID_ADDRESS)          return false;      return true; diff --git a/source/Plugins/Process/Linux/CMakeLists.txt b/source/Plugins/Process/Linux/CMakeLists.txt new file mode 100644 index 000000000000..80de8413d209 --- /dev/null +++ b/source/Plugins/Process/Linux/CMakeLists.txt @@ -0,0 +1,14 @@ +include_directories(.) +include_directories(../POSIX) +include_directories(../Utility) + +add_lldb_library(lldbPluginProcessLinux +  NativeProcessLinux.cpp +  NativeRegisterContextLinux.cpp +  NativeRegisterContextLinux_arm.cpp +  NativeRegisterContextLinux_arm64.cpp +  NativeRegisterContextLinux_x86_64.cpp +  NativeRegisterContextLinux_mips64.cpp +  NativeThreadLinux.cpp +  ProcFileReader.cpp +  ) diff --git a/source/Plugins/Process/Linux/Makefile b/source/Plugins/Process/Linux/Makefile new file mode 100644 index 000000000000..239e94d608e9 --- /dev/null +++ b/source/Plugins/Process/Linux/Makefile @@ -0,0 +1,17 @@ +##===- source/Plugins/Process/Linux/Makefile ---------------*- Makefile -*-===## +#  +#                     The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +#  +##===----------------------------------------------------------------------===## + +LLDB_LEVEL := ../../../.. +LIBRARYNAME := lldbPluginProcessLinux +BUILD_ARCHIVE = 1 + +# Extend the include path so we may locate UnwindLLDB.h +CPP.Flags += -I$(PROJ_SRC_DIR)/$(LLDB_LEVEL)/source/Plugins/Utility + +include $(LLDB_LEVEL)/Makefile diff --git a/source/Plugins/Process/Linux/NativeProcessLinux.cpp b/source/Plugins/Process/Linux/NativeProcessLinux.cpp new file mode 100644 index 000000000000..87c76f57830c --- /dev/null +++ b/source/Plugins/Process/Linux/NativeProcessLinux.cpp @@ -0,0 +1,3189 @@ +//===-- NativeProcessLinux.cpp -------------------------------- -*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "NativeProcessLinux.h" + +// C Includes +#include <errno.h> +#include <string.h> +#include <stdint.h> +#include <unistd.h> + +// C++ Includes +#include <fstream> +#include <mutex> +#include <sstream> +#include <string> +#include <unordered_map> + +// Other libraries and framework includes +#include "lldb/Core/EmulateInstruction.h" +#include "lldb/Core/Error.h" +#include "lldb/Core/Module.h" +#include "lldb/Core/ModuleSpec.h" +#include "lldb/Core/RegisterValue.h" +#include "lldb/Core/State.h" +#include "lldb/Host/common/NativeBreakpoint.h" +#include "lldb/Host/common/NativeRegisterContext.h" +#include "lldb/Host/Host.h" +#include "lldb/Host/ThreadLauncher.h" +#include "lldb/Target/Platform.h" +#include "lldb/Target/Process.h" +#include "lldb/Target/ProcessLaunchInfo.h" +#include "lldb/Target/Target.h" +#include "lldb/Utility/LLDBAssert.h" +#include "lldb/Utility/PseudoTerminal.h" +#include "lldb/Utility/StringExtractor.h" + +#include "Plugins/Process/POSIX/ProcessPOSIXLog.h" +#include "NativeThreadLinux.h" +#include "ProcFileReader.h" +#include "Procfs.h" + +// System includes - They have to be included after framework includes because they define some +// macros which collide with variable names in other modules +#include <linux/unistd.h> +#include <sys/socket.h> + +#include <sys/syscall.h> +#include <sys/types.h> +#include <sys/user.h> +#include <sys/wait.h> + +#include "lldb/Host/linux/Personality.h" +#include "lldb/Host/linux/Ptrace.h" +#include "lldb/Host/linux/Signalfd.h" +#include "lldb/Host/linux/Uio.h" +#include "lldb/Host/android/Android.h" + +#define LLDB_PERSONALITY_GET_CURRENT_SETTINGS  0xffffffff + +// Support hardware breakpoints in case it has not been defined +#ifndef TRAP_HWBKPT +  #define TRAP_HWBKPT 4 +#endif + +using namespace lldb; +using namespace lldb_private; +using namespace lldb_private::process_linux; +using namespace llvm; + +// Private bits we only need internally. + +static bool ProcessVmReadvSupported() +{ +    static bool is_supported; +    static std::once_flag flag; + +    std::call_once(flag, [] { +        Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); + +        uint32_t source = 0x47424742; +        uint32_t dest = 0; + +        struct iovec local, remote; +        remote.iov_base = &source; +        local.iov_base = &dest; +        remote.iov_len = local.iov_len = sizeof source; + +        // We shall try if cross-process-memory reads work by attempting to read a value from our own process. +        ssize_t res = process_vm_readv(getpid(), &local, 1, &remote, 1, 0); +        is_supported = (res == sizeof(source) && source == dest); +        if (log) +        { +            if (is_supported) +                log->Printf("%s: Detected kernel support for process_vm_readv syscall. Fast memory reads enabled.", +                        __FUNCTION__); +            else +                log->Printf("%s: syscall process_vm_readv failed (error: %s). Fast memory reads disabled.", +                        __FUNCTION__, strerror(errno)); +        } +    }); + +    return is_supported; +} + +namespace +{ +    Error +    ResolveProcessArchitecture (lldb::pid_t pid, Platform &platform, ArchSpec &arch) +    { +        // Grab process info for the running process. +        ProcessInstanceInfo process_info; +        if (!platform.GetProcessInfo (pid, process_info)) +            return Error("failed to get process info"); + +        // Resolve the executable module. +        ModuleSP exe_module_sp; +        ModuleSpec exe_module_spec(process_info.GetExecutableFile(), process_info.GetArchitecture()); +        FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths ()); +        Error error = platform.ResolveExecutable( +            exe_module_spec, +            exe_module_sp, +            executable_search_paths.GetSize () ? &executable_search_paths : NULL); + +        if (!error.Success ()) +            return error; + +        // Check if we've got our architecture from the exe_module. +        arch = exe_module_sp->GetArchitecture (); +        if (arch.IsValid ()) +            return Error(); +        else +            return Error("failed to retrieve a valid architecture from the exe module"); +    } + +    void +    DisplayBytes (StreamString &s, void *bytes, uint32_t count) +    { +        uint8_t *ptr = (uint8_t *)bytes; +        const uint32_t loop_count = std::min<uint32_t>(DEBUG_PTRACE_MAXBYTES, count); +        for(uint32_t i=0; i<loop_count; i++) +        { +            s.Printf ("[%x]", *ptr); +            ptr++; +        } +    } + +    void +    PtraceDisplayBytes(int &req, void *data, size_t data_size) +    { +        StreamString buf; +        Log *verbose_log (ProcessPOSIXLog::GetLogIfAllCategoriesSet ( +                    POSIX_LOG_PTRACE | POSIX_LOG_VERBOSE)); + +        if (verbose_log) +        { +            switch(req) +            { +            case PTRACE_POKETEXT: +            { +                DisplayBytes(buf, &data, 8); +                verbose_log->Printf("PTRACE_POKETEXT %s", buf.GetData()); +                break; +            } +            case PTRACE_POKEDATA: +            { +                DisplayBytes(buf, &data, 8); +                verbose_log->Printf("PTRACE_POKEDATA %s", buf.GetData()); +                break; +            } +            case PTRACE_POKEUSER: +            { +                DisplayBytes(buf, &data, 8); +                verbose_log->Printf("PTRACE_POKEUSER %s", buf.GetData()); +                break; +            } +            case PTRACE_SETREGS: +            { +                DisplayBytes(buf, data, data_size); +                verbose_log->Printf("PTRACE_SETREGS %s", buf.GetData()); +                break; +            } +            case PTRACE_SETFPREGS: +            { +                DisplayBytes(buf, data, data_size); +                verbose_log->Printf("PTRACE_SETFPREGS %s", buf.GetData()); +                break; +            } +            case PTRACE_SETSIGINFO: +            { +                DisplayBytes(buf, data, sizeof(siginfo_t)); +                verbose_log->Printf("PTRACE_SETSIGINFO %s", buf.GetData()); +                break; +            } +            case PTRACE_SETREGSET: +            { +                // Extract iov_base from data, which is a pointer to the struct IOVEC +                DisplayBytes(buf, *(void **)data, data_size); +                verbose_log->Printf("PTRACE_SETREGSET %s", buf.GetData()); +                break; +            } +            default: +            { +            } +            } +        } +    } + +    static constexpr unsigned k_ptrace_word_size = sizeof(void*); +    static_assert(sizeof(long) >= k_ptrace_word_size, "Size of long must be larger than ptrace word size"); +} // end of anonymous namespace + +// Simple helper function to ensure flags are enabled on the given file +// descriptor. +static Error +EnsureFDFlags(int fd, int flags) +{ +    Error 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; +} + +NativeProcessLinux::LaunchArgs::LaunchArgs(Module *module, +                                       char const **argv, +                                       char const **envp, +                                       const FileSpec &stdin_file_spec, +                                       const FileSpec &stdout_file_spec, +                                       const FileSpec &stderr_file_spec, +                                       const FileSpec &working_dir, +                                       const ProcessLaunchInfo &launch_info) +    : m_module(module), +      m_argv(argv), +      m_envp(envp), +      m_stdin_file_spec(stdin_file_spec), +      m_stdout_file_spec(stdout_file_spec), +      m_stderr_file_spec(stderr_file_spec), +      m_working_dir(working_dir), +      m_launch_info(launch_info) +{ +} + +NativeProcessLinux::LaunchArgs::~LaunchArgs() +{ } + +// ----------------------------------------------------------------------------- +// Public Static Methods +// ----------------------------------------------------------------------------- + +Error +NativeProcessProtocol::Launch ( +    ProcessLaunchInfo &launch_info, +    NativeProcessProtocol::NativeDelegate &native_delegate, +    MainLoop &mainloop, +    NativeProcessProtocolSP &native_process_sp) +{ +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); + +    lldb::ModuleSP exe_module_sp; +    PlatformSP platform_sp (Platform::GetHostPlatform ()); +    Error error = platform_sp->ResolveExecutable( +            ModuleSpec(launch_info.GetExecutableFile(), launch_info.GetArchitecture()), +            exe_module_sp, +            nullptr); + +    if (! error.Success()) +        return error; + +    // Verify the working directory is valid if one was specified. +    FileSpec working_dir{launch_info.GetWorkingDirectory()}; +    if (working_dir && +            (!working_dir.ResolvePath() || +             working_dir.GetFileType() != FileSpec::eFileTypeDirectory)) +    { +        error.SetErrorStringWithFormat ("No such file or directory: %s", +                working_dir.GetCString()); +        return error; +    } + +    const FileAction *file_action; + +    // Default of empty will mean to use existing open file descriptors. +    FileSpec stdin_file_spec{}; +    FileSpec stdout_file_spec{}; +    FileSpec stderr_file_spec{}; + +    file_action = launch_info.GetFileActionForFD (STDIN_FILENO); +    if (file_action) +        stdin_file_spec = file_action->GetFileSpec(); + +    file_action = launch_info.GetFileActionForFD (STDOUT_FILENO); +    if (file_action) +        stdout_file_spec = file_action->GetFileSpec(); + +    file_action = launch_info.GetFileActionForFD (STDERR_FILENO); +    if (file_action) +        stderr_file_spec = file_action->GetFileSpec(); + +    if (log) +    { +        if (stdin_file_spec) +            log->Printf ("NativeProcessLinux::%s setting STDIN to '%s'", +                    __FUNCTION__, stdin_file_spec.GetCString()); +        else +            log->Printf ("NativeProcessLinux::%s leaving STDIN as is", __FUNCTION__); + +        if (stdout_file_spec) +            log->Printf ("NativeProcessLinux::%s setting STDOUT to '%s'", +                    __FUNCTION__, stdout_file_spec.GetCString()); +        else +            log->Printf ("NativeProcessLinux::%s leaving STDOUT as is", __FUNCTION__); + +        if (stderr_file_spec) +            log->Printf ("NativeProcessLinux::%s setting STDERR to '%s'", +                    __FUNCTION__, stderr_file_spec.GetCString()); +        else +            log->Printf ("NativeProcessLinux::%s leaving STDERR as is", __FUNCTION__); +    } + +    // Create the NativeProcessLinux in launch mode. +    native_process_sp.reset (new NativeProcessLinux ()); + +    if (log) +    { +        int i = 0; +        for (const char **args = launch_info.GetArguments ().GetConstArgumentVector (); *args; ++args, ++i) +        { +            log->Printf ("NativeProcessLinux::%s arg %d: \"%s\"", __FUNCTION__, i, *args ? *args : "nullptr"); +            ++i; +        } +    } + +    if (!native_process_sp->RegisterNativeDelegate (native_delegate)) +    { +        native_process_sp.reset (); +        error.SetErrorStringWithFormat ("failed to register the native delegate"); +        return error; +    } + +    std::static_pointer_cast<NativeProcessLinux> (native_process_sp)->LaunchInferior ( +            mainloop, +            exe_module_sp.get(), +            launch_info.GetArguments ().GetConstArgumentVector (), +            launch_info.GetEnvironmentEntries ().GetConstArgumentVector (), +            stdin_file_spec, +            stdout_file_spec, +            stderr_file_spec, +            working_dir, +            launch_info, +            error); + +    if (error.Fail ()) +    { +        native_process_sp.reset (); +        if (log) +            log->Printf ("NativeProcessLinux::%s failed to launch process: %s", __FUNCTION__, error.AsCString ()); +        return error; +    } + +    launch_info.SetProcessID (native_process_sp->GetID ()); + +    return error; +} + +Error +NativeProcessProtocol::Attach ( +    lldb::pid_t pid, +    NativeProcessProtocol::NativeDelegate &native_delegate, +    MainLoop &mainloop, +    NativeProcessProtocolSP &native_process_sp) +{ +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); +    if (log && log->GetMask ().Test (POSIX_LOG_VERBOSE)) +        log->Printf ("NativeProcessLinux::%s(pid = %" PRIi64 ")", __FUNCTION__, pid); + +    // Grab the current platform architecture.  This should be Linux, +    // since this code is only intended to run on a Linux host. +    PlatformSP platform_sp (Platform::GetHostPlatform ()); +    if (!platform_sp) +        return Error("failed to get a valid default platform"); + +    // Retrieve the architecture for the running process. +    ArchSpec process_arch; +    Error error = ResolveProcessArchitecture (pid, *platform_sp.get (), process_arch); +    if (!error.Success ()) +        return error; + +    std::shared_ptr<NativeProcessLinux> native_process_linux_sp (new NativeProcessLinux ()); + +    if (!native_process_linux_sp->RegisterNativeDelegate (native_delegate)) +    { +        error.SetErrorStringWithFormat ("failed to register the native delegate"); +        return error; +    } + +    native_process_linux_sp->AttachToInferior (mainloop, pid, error); +    if (!error.Success ()) +        return error; + +    native_process_sp = native_process_linux_sp; +    return error; +} + +// ----------------------------------------------------------------------------- +// Public Instance Methods +// ----------------------------------------------------------------------------- + +NativeProcessLinux::NativeProcessLinux () : +    NativeProcessProtocol (LLDB_INVALID_PROCESS_ID), +    m_arch (), +    m_supports_mem_region (eLazyBoolCalculate), +    m_mem_region_cache (), +    m_mem_region_cache_mutex(), +    m_pending_notification_tid(LLDB_INVALID_THREAD_ID) +{ +} + +void +NativeProcessLinux::LaunchInferior ( +    MainLoop &mainloop, +    Module *module, +    const char *argv[], +    const char *envp[], +    const FileSpec &stdin_file_spec, +    const FileSpec &stdout_file_spec, +    const FileSpec &stderr_file_spec, +    const FileSpec &working_dir, +    const ProcessLaunchInfo &launch_info, +    Error &error) +{ +    m_sigchld_handle = mainloop.RegisterSignal(SIGCHLD, +            [this] (MainLoopBase &) { SigchldHandler(); }, error); +    if (! m_sigchld_handle) +        return; + +    if (module) +        m_arch = module->GetArchitecture (); + +    SetState (eStateLaunching); + +    std::unique_ptr<LaunchArgs> args( +        new LaunchArgs(module, argv, envp, +                       stdin_file_spec, +                       stdout_file_spec, +                       stderr_file_spec, +                       working_dir, +                       launch_info)); + +    Launch(args.get(), error); +} + +void +NativeProcessLinux::AttachToInferior (MainLoop &mainloop, lldb::pid_t pid, Error &error) +{ +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); +    if (log) +        log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ")", __FUNCTION__, pid); + +    m_sigchld_handle = mainloop.RegisterSignal(SIGCHLD, +            [this] (MainLoopBase &) { SigchldHandler(); }, error); +    if (! m_sigchld_handle) +        return; + +    // We can use the Host for everything except the ResolveExecutable portion. +    PlatformSP platform_sp = Platform::GetHostPlatform (); +    if (!platform_sp) +    { +        if (log) +            log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 "): no default platform set", __FUNCTION__, pid); +        error.SetErrorString ("no default platform available"); +        return; +    } + +    // Gather info about the process. +    ProcessInstanceInfo process_info; +    if (!platform_sp->GetProcessInfo (pid, process_info)) +    { +        if (log) +            log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 "): failed to get process info", __FUNCTION__, pid); +        error.SetErrorString ("failed to get process info"); +        return; +    } + +    // Resolve the executable module +    ModuleSP exe_module_sp; +    FileSpecList executable_search_paths (Target::GetDefaultExecutableSearchPaths()); +    ModuleSpec exe_module_spec(process_info.GetExecutableFile(), process_info.GetArchitecture()); +    error = platform_sp->ResolveExecutable(exe_module_spec, exe_module_sp, +                                           executable_search_paths.GetSize() ? &executable_search_paths : NULL); +    if (!error.Success()) +        return; + +    // Set the architecture to the exe architecture. +    m_arch = exe_module_sp->GetArchitecture(); +    if (log) +        log->Printf ("NativeProcessLinux::%s (pid = %" PRIi64 ") detected architecture %s", __FUNCTION__, pid, m_arch.GetArchitectureName ()); + +    m_pid = pid; +    SetState(eStateAttaching); + +    Attach(pid, error); +} + +::pid_t +NativeProcessLinux::Launch(LaunchArgs *args, Error &error) +{ +    assert (args && "null args"); + +    const char **argv = args->m_argv; +    const char **envp = args->m_envp; +    const FileSpec working_dir = args->m_working_dir; + +    lldb_utility::PseudoTerminal terminal; +    const size_t err_len = 1024; +    char err_str[err_len]; +    lldb::pid_t pid; + +    // Propagate the environment if one is not supplied. +    if (envp == NULL || envp[0] == NULL) +        envp = const_cast<const char **>(environ); + +    if ((pid = terminal.Fork(err_str, err_len)) == static_cast<lldb::pid_t> (-1)) +    { +        error.SetErrorToGenericError(); +        error.SetErrorStringWithFormat("Process fork failed: %s", err_str); +        return -1; +    } + +    // Recognized child exit status codes. +    enum { +        ePtraceFailed = 1, +        eDupStdinFailed, +        eDupStdoutFailed, +        eDupStderrFailed, +        eChdirFailed, +        eExecFailed, +        eSetGidFailed, +        eSetSigMaskFailed +    }; + +    // Child process. +    if (pid == 0) +    { +        // First, make sure we disable all logging. If we are logging to stdout, our logs can be +        // mistaken for inferior output. +        Log::DisableAllLogChannels(nullptr); +        // FIXME consider opening a pipe between parent/child and have this forked child +        // send log info to parent re: launch status. + +        // Start tracing this child that is about to exec. +        error = PtraceWrapper(PTRACE_TRACEME, 0); +        if (error.Fail()) +            exit(ePtraceFailed); + +        // terminal has already dupped the tty descriptors to stdin/out/err. +        // This closes original fd from which they were copied (and avoids +        // leaking descriptors to the debugged process. +        terminal.CloseSlaveFileDescriptor(); + +        // Do not inherit setgid powers. +        if (setgid(getgid()) != 0) +            exit(eSetGidFailed); + +        // Attempt to have our own process group. +        if (setpgid(0, 0) != 0) +        { +            // FIXME log that this failed. This is common. +            // Don't allow this to prevent an inferior exec. +        } + +        // Dup file descriptors if needed. +        if (args->m_stdin_file_spec) +            if (!DupDescriptor(args->m_stdin_file_spec, STDIN_FILENO, O_RDONLY)) +                exit(eDupStdinFailed); + +        if (args->m_stdout_file_spec) +            if (!DupDescriptor(args->m_stdout_file_spec, STDOUT_FILENO, O_WRONLY | O_CREAT | O_TRUNC)) +                exit(eDupStdoutFailed); + +        if (args->m_stderr_file_spec) +            if (!DupDescriptor(args->m_stderr_file_spec, STDERR_FILENO, O_WRONLY | O_CREAT | O_TRUNC)) +                exit(eDupStderrFailed); + +        // Close everything besides stdin, stdout, and stderr that has no file +        // action to avoid leaking +        for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd) +            if (!args->m_launch_info.GetFileActionForFD(fd)) +                close(fd); + +        // Change working directory +        if (working_dir && 0 != ::chdir(working_dir.GetCString())) +              exit(eChdirFailed); + +        // Disable ASLR if requested. +        if (args->m_launch_info.GetFlags ().Test (lldb::eLaunchFlagDisableASLR)) +        { +            const int old_personality = personality (LLDB_PERSONALITY_GET_CURRENT_SETTINGS); +            if (old_personality == -1) +            { +                // Can't retrieve Linux personality.  Cannot disable ASLR. +            } +            else +            { +                const int new_personality = personality (ADDR_NO_RANDOMIZE | old_personality); +                if (new_personality == -1) +                { +                    // Disabling ASLR failed. +                } +                else +                { +                    // Disabling ASLR succeeded. +                } +            } +        } + +        // Clear the signal mask to prevent the child from being affected by +        // any masking done by the parent. +        sigset_t set; +        if (sigemptyset(&set) != 0 || pthread_sigmask(SIG_SETMASK, &set, nullptr) != 0) +            exit(eSetSigMaskFailed); + +        // Execute.  We should never return... +        execve(argv[0], +               const_cast<char *const *>(argv), +               const_cast<char *const *>(envp)); + +        // ...unless exec fails.  In which case we definitely need to end the child here. +        exit(eExecFailed); +    } + +    // +    // This is the parent code here. +    // +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); + +    // Wait for the child process to trap on its call to execve. +    ::pid_t wpid; +    int status; +    if ((wpid = waitpid(pid, &status, 0)) < 0) +    { +        error.SetErrorToErrno(); +        if (log) +            log->Printf ("NativeProcessLinux::%s waitpid for inferior failed with %s", +                    __FUNCTION__, error.AsCString ()); + +        // Mark the inferior as invalid. +        // FIXME this could really use a new state - eStateLaunchFailure.  For now, using eStateInvalid. +        SetState (StateType::eStateInvalid); + +        return -1; +    } +    else if (WIFEXITED(status)) +    { +        // open, dup or execve likely failed for some reason. +        error.SetErrorToGenericError(); +        switch (WEXITSTATUS(status)) +        { +            case ePtraceFailed: +                error.SetErrorString("Child ptrace failed."); +                break; +            case eDupStdinFailed: +                error.SetErrorString("Child open stdin failed."); +                break; +            case eDupStdoutFailed: +                error.SetErrorString("Child open stdout failed."); +                break; +            case eDupStderrFailed: +                error.SetErrorString("Child open stderr failed."); +                break; +            case eChdirFailed: +                error.SetErrorString("Child failed to set working directory."); +                break; +            case eExecFailed: +                error.SetErrorString("Child exec failed."); +                break; +            case eSetGidFailed: +                error.SetErrorString("Child setgid failed."); +                break; +            case eSetSigMaskFailed: +                error.SetErrorString("Child failed to set signal mask."); +                break; +            default: +                error.SetErrorString("Child returned unknown exit status."); +                break; +        } + +        if (log) +        { +            log->Printf ("NativeProcessLinux::%s inferior exited with status %d before issuing a STOP", +                    __FUNCTION__, +                    WEXITSTATUS(status)); +        } + +        // Mark the inferior as invalid. +        // FIXME this could really use a new state - eStateLaunchFailure.  For now, using eStateInvalid. +        SetState (StateType::eStateInvalid); + +        return -1; +    } +    assert(WIFSTOPPED(status) && (wpid == static_cast< ::pid_t> (pid)) && +           "Could not sync with inferior process."); + +    if (log) +        log->Printf ("NativeProcessLinux::%s inferior started, now in stopped state", __FUNCTION__); + +    error = SetDefaultPtraceOpts(pid); +    if (error.Fail()) +    { +        if (log) +            log->Printf ("NativeProcessLinux::%s inferior failed to set default ptrace options: %s", +                    __FUNCTION__, error.AsCString ()); + +        // Mark the inferior as invalid. +        // FIXME this could really use a new state - eStateLaunchFailure.  For now, using eStateInvalid. +        SetState (StateType::eStateInvalid); + +        return -1; +    } + +    // Release the master terminal descriptor and pass it off to the +    // NativeProcessLinux instance.  Similarly stash the inferior pid. +    m_terminal_fd = terminal.ReleaseMasterFileDescriptor(); +    m_pid = pid; + +    // Set the terminal fd to be in non blocking mode (it simplifies the +    // implementation of ProcessLinux::GetSTDOUT to have a non-blocking +    // descriptor to read from). +    error = EnsureFDFlags(m_terminal_fd, O_NONBLOCK); +    if (error.Fail()) +    { +        if (log) +            log->Printf ("NativeProcessLinux::%s inferior EnsureFDFlags failed for ensuring terminal O_NONBLOCK setting: %s", +                    __FUNCTION__, error.AsCString ()); + +        // Mark the inferior as invalid. +        // FIXME this could really use a new state - eStateLaunchFailure.  For now, using eStateInvalid. +        SetState (StateType::eStateInvalid); + +        return -1; +    } + +    if (log) +        log->Printf ("NativeProcessLinux::%s() adding pid = %" PRIu64, __FUNCTION__, pid); + +    NativeThreadLinuxSP thread_sp = AddThread(pid); +    assert (thread_sp && "AddThread() returned a nullptr thread"); +    thread_sp->SetStoppedBySignal(SIGSTOP); +    ThreadWasCreated(*thread_sp); + +    // Let our process instance know the thread has stopped. +    SetCurrentThreadID (thread_sp->GetID ()); +    SetState (StateType::eStateStopped); + +    if (log) +    { +        if (error.Success ()) +        { +            log->Printf ("NativeProcessLinux::%s inferior launching succeeded", __FUNCTION__); +        } +        else +        { +            log->Printf ("NativeProcessLinux::%s inferior launching failed: %s", +                __FUNCTION__, error.AsCString ()); +            return -1; +        } +    } +    return pid; +} + +::pid_t +NativeProcessLinux::Attach(lldb::pid_t pid, Error &error) +{ +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); + +    // Use a map to keep track of the threads which we have attached/need to attach. +    Host::TidMap tids_to_attach; +    if (pid <= 1) +    { +        error.SetErrorToGenericError(); +        error.SetErrorString("Attaching to process 1 is not allowed."); +        return -1; +    } + +    while (Host::FindProcessThreads(pid, tids_to_attach)) +    { +        for (Host::TidMap::iterator it = tids_to_attach.begin(); +             it != tids_to_attach.end();) +        { +            if (it->second == false) +            { +                lldb::tid_t tid = it->first; + +                // Attach to the requested process. +                // An attach will cause the thread to stop with a SIGSTOP. +                error = PtraceWrapper(PTRACE_ATTACH, tid); +                if (error.Fail()) +                { +                    // No such thread. The thread may have exited. +                    // More error handling may be needed. +                    if (error.GetError() == ESRCH) +                    { +                        it = tids_to_attach.erase(it); +                        continue; +                    } +                    else +                        return -1; +                } + +                int status; +                // Need to use __WALL otherwise we receive an error with errno=ECHLD +                // At this point we should have a thread stopped if waitpid succeeds. +                if ((status = waitpid(tid, NULL, __WALL)) < 0) +                { +                    // No such thread. The thread may have exited. +                    // More error handling may be needed. +                    if (errno == ESRCH) +                    { +                        it = tids_to_attach.erase(it); +                        continue; +                    } +                    else +                    { +                        error.SetErrorToErrno(); +                        return -1; +                    } +                } + +                error = SetDefaultPtraceOpts(tid); +                if (error.Fail()) +                    return -1; + +                if (log) +                    log->Printf ("NativeProcessLinux::%s() adding tid = %" PRIu64, __FUNCTION__, tid); + +                it->second = true; + +                // Create the thread, mark it as stopped. +                NativeThreadLinuxSP thread_sp (AddThread(static_cast<lldb::tid_t>(tid))); +                assert (thread_sp && "AddThread() returned a nullptr"); + +                // This will notify this is a new thread and tell the system it is stopped. +                thread_sp->SetStoppedBySignal(SIGSTOP); +                ThreadWasCreated(*thread_sp); +                SetCurrentThreadID (thread_sp->GetID ()); +            } + +            // move the loop forward +            ++it; +        } +    } + +    if (tids_to_attach.size() > 0) +    { +        m_pid = pid; +        // Let our process instance know the thread has stopped. +        SetState (StateType::eStateStopped); +    } +    else +    { +        error.SetErrorToGenericError(); +        error.SetErrorString("No such process."); +        return -1; +    } + +    return pid; +} + +Error +NativeProcessLinux::SetDefaultPtraceOpts(lldb::pid_t pid) +{ +    long ptrace_opts = 0; + +    // Have the child raise an event on exit.  This is used to keep the child in +    // limbo until it is destroyed. +    ptrace_opts |= PTRACE_O_TRACEEXIT; + +    // Have the tracer trace threads which spawn in the inferior process. +    // TODO: if we want to support tracing the inferiors' child, add the +    // appropriate ptrace flags here (PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK) +    ptrace_opts |= PTRACE_O_TRACECLONE; + +    // Have the tracer notify us before execve returns +    // (needed to disable legacy SIGTRAP generation) +    ptrace_opts |= PTRACE_O_TRACEEXEC; + +    return PtraceWrapper(PTRACE_SETOPTIONS, pid, nullptr, (void*)ptrace_opts); +} + +static ExitType convert_pid_status_to_exit_type (int status) +{ +    if (WIFEXITED (status)) +        return ExitType::eExitTypeExit; +    else if (WIFSIGNALED (status)) +        return ExitType::eExitTypeSignal; +    else if (WIFSTOPPED (status)) +        return ExitType::eExitTypeStop; +    else +    { +        // We don't know what this is. +        return ExitType::eExitTypeInvalid; +    } +} + +static int convert_pid_status_to_return_code (int status) +{ +    if (WIFEXITED (status)) +        return WEXITSTATUS (status); +    else if (WIFSIGNALED (status)) +        return WTERMSIG (status); +    else if (WIFSTOPPED (status)) +        return WSTOPSIG (status); +    else +    { +        // We don't know what this is. +        return ExitType::eExitTypeInvalid; +    } +} + +// Handles all waitpid events from the inferior process. +void +NativeProcessLinux::MonitorCallback(lldb::pid_t pid, +                                    bool exited, +                                    int signal, +                                    int status) +{ +    Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); + +    // Certain activities differ based on whether the pid is the tid of the main thread. +    const bool is_main_thread = (pid == GetID ()); + +    // Handle when the thread exits. +    if (exited) +    { +        if (log) +            log->Printf ("NativeProcessLinux::%s() got exit signal(%d) , tid = %"  PRIu64 " (%s main thread)", __FUNCTION__, signal, pid, is_main_thread ? "is" : "is not"); + +        // This is a thread that exited.  Ensure we're not tracking it anymore. +        const bool thread_found = StopTrackingThread (pid); + +        if (is_main_thread) +        { +            // We only set the exit status and notify the delegate if we haven't already set the process +            // state to an exited state.  We normally should have received a SIGTRAP | (PTRACE_EVENT_EXIT << 8) +            // for the main thread. +            const bool already_notified = (GetState() == StateType::eStateExited) || (GetState () == StateType::eStateCrashed); +            if (!already_notified) +            { +                if (log) +                    log->Printf ("NativeProcessLinux::%s() tid = %"  PRIu64 " handling main thread exit (%s), expected exit state already set but state was %s instead, setting exit state now", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found", StateAsCString (GetState ())); +                // The main thread exited.  We're done monitoring.  Report to delegate. +                SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); + +                // Notify delegate that our process has exited. +                SetState (StateType::eStateExited, true); +            } +            else +            { +                if (log) +                    log->Printf ("NativeProcessLinux::%s() tid = %"  PRIu64 " main thread now exited (%s)", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found"); +            } +        } +        else +        { +            // Do we want to report to the delegate in this case?  I think not.  If this was an orderly +            // thread exit, we would already have received the SIGTRAP | (PTRACE_EVENT_EXIT << 8) signal, +            // and we would have done an all-stop then. +            if (log) +                log->Printf ("NativeProcessLinux::%s() tid = %"  PRIu64 " handling non-main thread exit (%s)", __FUNCTION__, pid, thread_found ? "stopped tracking thread metadata" : "thread metadata not found"); +        } +        return; +    } + +    siginfo_t info; +    const auto info_err = GetSignalInfo(pid, &info); +    auto thread_sp = GetThreadByID(pid); + +    if (! thread_sp) +    { +        // Normally, the only situation when we cannot find the thread is if we have just +        // received a new thread notification. This is indicated by GetSignalInfo() returning +        // si_code == SI_USER and si_pid == 0 +        if (log) +            log->Printf("NativeProcessLinux::%s received notification about an unknown tid %" PRIu64 ".", __FUNCTION__, pid); + +        if (info_err.Fail()) +        { +            if (log) +                log->Printf("NativeProcessLinux::%s (tid %" PRIu64 ") GetSignalInfo failed (%s). Ingoring this notification.", __FUNCTION__, pid, info_err.AsCString()); +            return; +        } + +        if (log && (info.si_code != SI_USER || info.si_pid != 0)) +            log->Printf("NativeProcessLinux::%s (tid %" PRIu64 ") unexpected signal info (si_code: %d, si_pid: %d). Treating as a new thread notification anyway.", __FUNCTION__, pid, info.si_code, info.si_pid); + +        auto thread_sp = AddThread(pid); +        // Resume the newly created thread. +        ResumeThread(*thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); +        ThreadWasCreated(*thread_sp); +        return; +    } + +    // Get details on the signal raised. +    if (info_err.Success()) +    { +        // We have retrieved the signal info.  Dispatch appropriately. +        if (info.si_signo == SIGTRAP) +            MonitorSIGTRAP(info, *thread_sp); +        else +            MonitorSignal(info, *thread_sp, exited); +    } +    else +    { +        if (info_err.GetError() == EINVAL) +        { +            // This is a group stop reception for this tid. +            // We can reach here if we reinject SIGSTOP, SIGSTP, SIGTTIN or SIGTTOU into the +            // tracee, triggering the group-stop mechanism. Normally receiving these would stop +            // the process, pending a SIGCONT. Simulating this state in a debugger is hard and is +            // generally not needed (one use case is debugging background task being managed by a +            // shell). For general use, it is sufficient to stop the process in a signal-delivery +            // stop which happens before the group stop. This done by MonitorSignal and works +            // correctly for all signals. +            if (log) +                log->Printf("NativeProcessLinux::%s received a group stop for pid %" PRIu64 " tid %" PRIu64 ". Transparent handling of group stops not supported, resuming the thread.", __FUNCTION__, GetID (), pid); +            ResumeThread(*thread_sp, thread_sp->GetState(), LLDB_INVALID_SIGNAL_NUMBER); +        } +        else +        { +            // ptrace(GETSIGINFO) failed (but not due to group-stop). + +            // A return value of ESRCH means the thread/process is no longer on the system, +            // so it was killed somehow outside of our control.  Either way, we can't do anything +            // with it anymore. + +            // Stop tracking the metadata for the thread since it's entirely off the system now. +            const bool thread_found = StopTrackingThread (pid); + +            if (log) +                log->Printf ("NativeProcessLinux::%s GetSignalInfo failed: %s, tid = %" PRIu64 ", signal = %d, status = %d (%s, %s, %s)", +                             __FUNCTION__, info_err.AsCString(), pid, signal, status, info_err.GetError() == ESRCH ? "thread/process killed" : "unknown reason", is_main_thread ? "is main thread" : "is not main thread", thread_found ? "thread metadata removed" : "thread metadata not found"); + +            if (is_main_thread) +            { +                // Notify the delegate - our process is not available but appears to have been killed outside +                // our control.  Is eStateExited the right exit state in this case? +                SetExitStatus (convert_pid_status_to_exit_type (status), convert_pid_status_to_return_code (status), nullptr, true); +                SetState (StateType::eStateExited, true); +            } +            else +            { +                // This thread was pulled out from underneath us.  Anything to do here? Do we want to do an all stop? +                if (log) +                    log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 " non-main thread exit occurred, didn't tell delegate anything since thread disappeared out from underneath us", __FUNCTION__, GetID (), pid); +            } +        } +    } +} + +void +NativeProcessLinux::WaitForNewThread(::pid_t tid) +{ +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); + +    NativeThreadLinuxSP new_thread_sp = GetThreadByID(tid); + +    if (new_thread_sp) +    { +        // We are already tracking the thread - we got the event on the new thread (see +        // MonitorSignal) before this one. We are done. +        return; +    } + +    // The thread is not tracked yet, let's wait for it to appear. +    int status = -1; +    ::pid_t wait_pid; +    do +    { +        if (log) +            log->Printf ("NativeProcessLinux::%s() received thread creation event for tid %" PRIu32 ". tid not tracked yet, waiting for thread to appear...", __FUNCTION__, tid); +        wait_pid = waitpid(tid, &status, __WALL); +    } +    while (wait_pid == -1 && errno == EINTR); +    // Since we are waiting on a specific tid, this must be the creation event. But let's do +    // some checks just in case. +    if (wait_pid != tid) { +        if (log) +            log->Printf ("NativeProcessLinux::%s() waiting for tid %" PRIu32 " failed. Assuming the thread has disappeared in the meantime", __FUNCTION__, tid); +        // The only way I know of this could happen is if the whole process was +        // SIGKILLed in the mean time. In any case, we can't do anything about that now. +        return; +    } +    if (WIFEXITED(status)) +    { +        if (log) +            log->Printf ("NativeProcessLinux::%s() waiting for tid %" PRIu32 " returned an 'exited' event. Not tracking the thread.", __FUNCTION__, tid); +        // Also a very improbable event. +        return; +    } + +    siginfo_t info; +    Error error = GetSignalInfo(tid, &info); +    if (error.Fail()) +    { +        if (log) +            log->Printf ("NativeProcessLinux::%s() GetSignalInfo for tid %" PRIu32 " failed. Assuming the thread has disappeared in the meantime.", __FUNCTION__, tid); +        return; +    } + +    if (((info.si_pid != 0) || (info.si_code != SI_USER)) && log) +    { +        // We should be getting a thread creation signal here, but we received something +        // else. There isn't much we can do about it now, so we will just log that. Since the +        // thread is alive and we are receiving events from it, we shall pretend that it was +        // created properly. +        log->Printf ("NativeProcessLinux::%s() GetSignalInfo for tid %" PRIu32 " received unexpected signal with code %d from pid %d.", __FUNCTION__, tid, info.si_code, info.si_pid); +    } + +    if (log) +        log->Printf ("NativeProcessLinux::%s() pid = %" PRIu64 ": tracking new thread tid %" PRIu32, +                 __FUNCTION__, GetID (), tid); + +    new_thread_sp = AddThread(tid); +    ResumeThread(*new_thread_sp, eStateRunning, LLDB_INVALID_SIGNAL_NUMBER); +    ThreadWasCreated(*new_thread_sp); +} + +void +NativeProcessLinux::MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread) +{ +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); +    const bool is_main_thread = (thread.GetID() == GetID ()); + +    assert(info.si_signo == SIGTRAP && "Unexpected child signal!"); + +    Mutex::Locker locker (m_threads_mutex); + +    switch (info.si_code) +    { +    // TODO: these two cases are required if we want to support tracing of the inferiors' children.  We'd need this to debug a monitor. +    // case (SIGTRAP | (PTRACE_EVENT_FORK << 8)): +    // case (SIGTRAP | (PTRACE_EVENT_VFORK << 8)): + +    case (SIGTRAP | (PTRACE_EVENT_CLONE << 8)): +    { +        // This is the notification on the parent thread which informs us of new thread +        // creation. +        // We don't want to do anything with the parent thread so we just resume it. In case we +        // want to implement "break on thread creation" functionality, we would need to stop +        // here. + +        unsigned long event_message = 0; +        if (GetEventMessage(thread.GetID(), &event_message).Fail()) +        { +            if (log) +                log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " received thread creation event but GetEventMessage failed so we don't know the new tid", __FUNCTION__, thread.GetID()); +        } else  +            WaitForNewThread(event_message); + +        ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); +        break; +    } + +    case (SIGTRAP | (PTRACE_EVENT_EXEC << 8)): +    { +        NativeThreadLinuxSP main_thread_sp; +        if (log) +            log->Printf ("NativeProcessLinux::%s() received exec event, code = %d", __FUNCTION__, info.si_code ^ SIGTRAP); + +        // Exec clears any pending notifications. +        m_pending_notification_tid = LLDB_INVALID_THREAD_ID; + +        // Remove all but the main thread here.  Linux fork creates a new process which only copies the main thread.  Mutexes are in undefined state. +        if (log) +            log->Printf ("NativeProcessLinux::%s exec received, stop tracking all but main thread", __FUNCTION__); + +        for (auto thread_sp : m_threads) +        { +            const bool is_main_thread = thread_sp && thread_sp->GetID () == GetID (); +            if (is_main_thread) +            { +                main_thread_sp = std::static_pointer_cast<NativeThreadLinux>(thread_sp); +                if (log) +                    log->Printf ("NativeProcessLinux::%s found main thread with tid %" PRIu64 ", keeping", __FUNCTION__, main_thread_sp->GetID ()); +            } +            else +            { +                if (log) +                    log->Printf ("NativeProcessLinux::%s discarding non-main-thread tid %" PRIu64 " due to exec", __FUNCTION__, thread_sp->GetID ()); +            } +        } + +        m_threads.clear (); + +        if (main_thread_sp) +        { +            m_threads.push_back (main_thread_sp); +            SetCurrentThreadID (main_thread_sp->GetID ()); +            main_thread_sp->SetStoppedByExec(); +        } +        else +        { +            SetCurrentThreadID (LLDB_INVALID_THREAD_ID); +            if (log) +                log->Printf ("NativeProcessLinux::%s pid %" PRIu64 "no main thread found, discarded all threads, we're in a no-thread state!", __FUNCTION__, GetID ()); +        } + +        // Tell coordinator about about the "new" (since exec) stopped main thread. +        ThreadWasCreated(*main_thread_sp); + +        // Let our delegate know we have just exec'd. +        NotifyDidExec (); + +        // If we have a main thread, indicate we are stopped. +        assert (main_thread_sp && "exec called during ptraced process but no main thread metadata tracked"); + +        // Let the process know we're stopped. +        StopRunningThreads(main_thread_sp->GetID()); + +        break; +    } + +    case (SIGTRAP | (PTRACE_EVENT_EXIT << 8)): +    { +        // The inferior process or one of its threads is about to exit. +        // We don't want to do anything with the thread so we just resume it. In case we +        // want to implement "break on thread exit" functionality, we would need to stop +        // here. + +        unsigned long data = 0; +        if (GetEventMessage(thread.GetID(), &data).Fail()) +            data = -1; + +        if (log) +        { +            log->Printf ("NativeProcessLinux::%s() received PTRACE_EVENT_EXIT, data = %lx (WIFEXITED=%s,WIFSIGNALED=%s), pid = %" PRIu64 " (%s)", +                         __FUNCTION__, +                         data, WIFEXITED (data) ? "true" : "false", WIFSIGNALED (data) ? "true" : "false", +                         thread.GetID(), +                    is_main_thread ? "is main thread" : "not main thread"); +        } + +        if (is_main_thread) +        { +            SetExitStatus (convert_pid_status_to_exit_type (data), convert_pid_status_to_return_code (data), nullptr, true); +        } + +        StateType state = thread.GetState(); +        if (! StateIsRunningState(state)) +        { +            // Due to a kernel bug, we may sometimes get this stop after the inferior gets a +            // SIGKILL. This confuses our state tracking logic in ResumeThread(), since normally, +            // we should not be receiving any ptrace events while the inferior is stopped. This +            // makes sure that the inferior is resumed and exits normally. +            state = eStateRunning; +        } +        ResumeThread(thread, state, LLDB_INVALID_SIGNAL_NUMBER); + +        break; +    } + +    case 0: +    case TRAP_TRACE:  // We receive this on single stepping. +    case TRAP_HWBKPT: // We receive this on watchpoint hit +    { +        // If a watchpoint was hit, report it +        uint32_t wp_index; +        Error error = thread.GetRegisterContext()->GetWatchpointHitIndex(wp_index, (uintptr_t)info.si_addr); +        if (error.Fail() && log) +            log->Printf("NativeProcessLinux::%s() " +                        "received error while checking for watchpoint hits, " +                        "pid = %" PRIu64 " error = %s", +                        __FUNCTION__, thread.GetID(), error.AsCString()); +        if (wp_index != LLDB_INVALID_INDEX32) +        { +            MonitorWatchpoint(thread, wp_index); +            break; +        } + +        // Otherwise, report step over +        MonitorTrace(thread); +        break; +    } + +    case SI_KERNEL: +#if defined __mips__ +        // For mips there is no special signal for watchpoint +        // So we check for watchpoint in kernel trap +    { +        // If a watchpoint was hit, report it +        uint32_t wp_index; +        Error error = thread.GetRegisterContext()->GetWatchpointHitIndex(wp_index, LLDB_INVALID_ADDRESS); +        if (error.Fail() && log) +            log->Printf("NativeProcessLinux::%s() " +                        "received error while checking for watchpoint hits, " +                        "pid = %" PRIu64 " error = %s", +                        __FUNCTION__, thread.GetID(), error.AsCString()); +        if (wp_index != LLDB_INVALID_INDEX32) +        { +            MonitorWatchpoint(thread, wp_index); +            break; +        } +    } +        // NO BREAK +#endif +    case TRAP_BRKPT: +        MonitorBreakpoint(thread); +        break; + +    case SIGTRAP: +    case (SIGTRAP | 0x80): +        if (log) +            log->Printf ("NativeProcessLinux::%s() received unknown SIGTRAP system call stop event, pid %" PRIu64 "tid %" PRIu64 ", resuming", __FUNCTION__, GetID (), thread.GetID()); + +        // Ignore these signals until we know more about them. +        ResumeThread(thread, thread.GetState(), LLDB_INVALID_SIGNAL_NUMBER); +        break; + +    default: +        assert(false && "Unexpected SIGTRAP code!"); +        if (log) +            log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 "tid %" PRIu64 " received unhandled SIGTRAP code: 0x%d", +                    __FUNCTION__, GetID(), thread.GetID(), info.si_code); +        break; +         +    } +} + +void +NativeProcessLinux::MonitorTrace(NativeThreadLinux &thread) +{ +    Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); +    if (log) +        log->Printf("NativeProcessLinux::%s() received trace event, pid = %" PRIu64 " (single stepping)", +                __FUNCTION__, thread.GetID()); + +    // This thread is currently stopped. +    thread.SetStoppedByTrace(); + +    StopRunningThreads(thread.GetID()); +} + +void +NativeProcessLinux::MonitorBreakpoint(NativeThreadLinux &thread) +{ +    Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); +    if (log) +        log->Printf("NativeProcessLinux::%s() received breakpoint event, pid = %" PRIu64, +                __FUNCTION__, thread.GetID()); + +    // Mark the thread as stopped at breakpoint. +    thread.SetStoppedByBreakpoint(); +    Error error = FixupBreakpointPCAsNeeded(thread); +    if (error.Fail()) +        if (log) +            log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " fixup: %s", +                    __FUNCTION__, thread.GetID(), error.AsCString()); + +    if (m_threads_stepping_with_breakpoint.find(thread.GetID()) != m_threads_stepping_with_breakpoint.end()) +        thread.SetStoppedByTrace(); + +    StopRunningThreads(thread.GetID()); +} + +void +NativeProcessLinux::MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index) +{ +    Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_WATCHPOINTS)); +    if (log) +        log->Printf("NativeProcessLinux::%s() received watchpoint event, " +                    "pid = %" PRIu64 ", wp_index = %" PRIu32, +                    __FUNCTION__, thread.GetID(), wp_index); + +    // Mark the thread as stopped at watchpoint. +    // The address is at (lldb::addr_t)info->si_addr if we need it. +    thread.SetStoppedByWatchpoint(wp_index); + +    // We need to tell all other running threads before we notify the delegate about this stop. +    StopRunningThreads(thread.GetID()); +} + +void +NativeProcessLinux::MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread, bool exited) +{ +    const int signo = info.si_signo; +    const bool is_from_llgs = info.si_pid == getpid (); + +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); + +    // POSIX says that process behaviour is undefined after it ignores a SIGFPE, +    // SIGILL, SIGSEGV, or SIGBUS *unless* that signal was generated by a +    // kill(2) or raise(3).  Similarly for tgkill(2) on Linux. +    // +    // IOW, user generated signals never generate what we consider to be a +    // "crash". +    // +    // Similarly, ACK signals generated by this monitor. + +    Mutex::Locker locker (m_threads_mutex); + +    // Handle the signal. +    if (info.si_code == SI_TKILL || info.si_code == SI_USER) +    { +        if (log) +            log->Printf ("NativeProcessLinux::%s() received signal %s (%d) with code %s, (siginfo pid = %d (%s), waitpid pid = %" PRIu64 ")", +                            __FUNCTION__, +                            Host::GetSignalAsCString(signo), +                            signo, +                            (info.si_code == SI_TKILL ? "SI_TKILL" : "SI_USER"), +                            info.si_pid, +                            is_from_llgs ? "from llgs" : "not from llgs", +                            thread.GetID()); +    } + +    // Check for thread stop notification. +    if (is_from_llgs && (info.si_code == SI_TKILL) && (signo == SIGSTOP)) +    { +        // This is a tgkill()-based stop. +        if (log) +            log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 ", thread stopped", +                         __FUNCTION__, +                         GetID (), +                         thread.GetID()); + +        // Check that we're not already marked with a stop reason. +        // Note this thread really shouldn't already be marked as stopped - if we were, that would imply that +        // the kernel signaled us with the thread stopping which we handled and marked as stopped, +        // and that, without an intervening resume, we received another stop.  It is more likely +        // that we are missing the marking of a run state somewhere if we find that the thread was +        // marked as stopped. +        const StateType thread_state = thread.GetState(); +        if (!StateIsStoppedState (thread_state, false)) +        { +            // An inferior thread has stopped because of a SIGSTOP we have sent it. +            // Generally, these are not important stops and we don't want to report them as +            // they are just used to stop other threads when one thread (the one with the +            // *real* stop reason) hits a breakpoint (watchpoint, etc...). However, in the +            // case of an asynchronous Interrupt(), this *is* the real stop reason, so we +            // leave the signal intact if this is the thread that was chosen as the +            // triggering thread. +            if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID) +            { +                if (m_pending_notification_tid == thread.GetID()) +                    thread.SetStoppedBySignal(SIGSTOP, &info); +                else +                    thread.SetStoppedWithNoReason(); + +                SetCurrentThreadID (thread.GetID ()); +                SignalIfAllThreadsStopped(); +            } +            else +            { +                // We can end up here if stop was initiated by LLGS but by this time a +                // thread stop has occurred - maybe initiated by another event. +                Error error = ResumeThread(thread, thread.GetState(), 0); +                if (error.Fail() && log) +                { +                    log->Printf("NativeProcessLinux::%s failed to resume thread tid  %" PRIu64 ": %s", +                            __FUNCTION__, thread.GetID(), error.AsCString()); +                } +            } +        } +        else +        { +            if (log) +            { +                // Retrieve the signal name if the thread was stopped by a signal. +                int stop_signo = 0; +                const bool stopped_by_signal = thread.IsStopped(&stop_signo); +                const char *signal_name = stopped_by_signal ? Host::GetSignalAsCString(stop_signo) : "<not stopped by signal>"; +                if (!signal_name) +                    signal_name = "<no-signal-name>"; + +                log->Printf ("NativeProcessLinux::%s() pid %" PRIu64 " tid %" PRIu64 ", thread was already marked as a stopped state (state=%s, signal=%d (%s)), leaving stop signal as is", +                             __FUNCTION__, +                             GetID (), +                             thread.GetID(), +                             StateAsCString (thread_state), +                             stop_signo, +                             signal_name); +            } +            SignalIfAllThreadsStopped(); +        } + +        // Done handling. +        return; +    } + +    if (log) +        log->Printf ("NativeProcessLinux::%s() received signal %s", __FUNCTION__, Host::GetSignalAsCString(signo)); + +    // This thread is stopped. +    thread.SetStoppedBySignal(signo, &info); + +    // Send a stop to the debugger after we get all other threads to stop. +    StopRunningThreads(thread.GetID()); +} + +namespace { + +struct EmulatorBaton +{ +    NativeProcessLinux* m_process; +    NativeRegisterContext* m_reg_context; + +    // eRegisterKindDWARF -> RegsiterValue +    std::unordered_map<uint32_t, RegisterValue> m_register_values; + +    EmulatorBaton(NativeProcessLinux* process, NativeRegisterContext* reg_context) : +            m_process(process), m_reg_context(reg_context) {} +}; + +} // anonymous namespace + +static size_t +ReadMemoryCallback (EmulateInstruction *instruction, +                    void *baton, +                    const EmulateInstruction::Context &context,  +                    lldb::addr_t addr,  +                    void *dst, +                    size_t length) +{ +    EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); + +    size_t bytes_read; +    emulator_baton->m_process->ReadMemory(addr, dst, length, bytes_read); +    return bytes_read; +} + +static bool +ReadRegisterCallback (EmulateInstruction *instruction, +                      void *baton, +                      const RegisterInfo *reg_info, +                      RegisterValue ®_value) +{ +    EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); + +    auto it = emulator_baton->m_register_values.find(reg_info->kinds[eRegisterKindDWARF]); +    if (it != emulator_baton->m_register_values.end()) +    { +        reg_value = it->second; +        return true; +    } + +    // The emulator only fill in the dwarf regsiter numbers (and in some case +    // the generic register numbers). Get the full register info from the +    // register context based on the dwarf register numbers. +    const RegisterInfo* full_reg_info = emulator_baton->m_reg_context->GetRegisterInfo( +            eRegisterKindDWARF, reg_info->kinds[eRegisterKindDWARF]); + +    Error error = emulator_baton->m_reg_context->ReadRegister(full_reg_info, reg_value); +    if (error.Success()) +        return true; + +    return false; +} + +static bool +WriteRegisterCallback (EmulateInstruction *instruction, +                       void *baton, +                       const EmulateInstruction::Context &context, +                       const RegisterInfo *reg_info, +                       const RegisterValue ®_value) +{ +    EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); +    emulator_baton->m_register_values[reg_info->kinds[eRegisterKindDWARF]] = reg_value; +    return true; +} + +static size_t +WriteMemoryCallback (EmulateInstruction *instruction, +                     void *baton, +                     const EmulateInstruction::Context &context,  +                     lldb::addr_t addr,  +                     const void *dst, +                     size_t length) +{ +    return length; +} + +static lldb::addr_t +ReadFlags (NativeRegisterContext* regsiter_context) +{ +    const RegisterInfo* flags_info = regsiter_context->GetRegisterInfo( +            eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); +    return regsiter_context->ReadRegisterAsUnsigned(flags_info, LLDB_INVALID_ADDRESS); +} + +Error +NativeProcessLinux::SetupSoftwareSingleStepping(NativeThreadLinux &thread) +{ +    Error error; +    NativeRegisterContextSP register_context_sp = thread.GetRegisterContext(); + +    std::unique_ptr<EmulateInstruction> emulator_ap( +        EmulateInstruction::FindPlugin(m_arch, eInstructionTypePCModifying, nullptr)); + +    if (emulator_ap == nullptr) +        return Error("Instruction emulator not found!"); + +    EmulatorBaton baton(this, register_context_sp.get()); +    emulator_ap->SetBaton(&baton); +    emulator_ap->SetReadMemCallback(&ReadMemoryCallback); +    emulator_ap->SetReadRegCallback(&ReadRegisterCallback); +    emulator_ap->SetWriteMemCallback(&WriteMemoryCallback); +    emulator_ap->SetWriteRegCallback(&WriteRegisterCallback); + +    if (!emulator_ap->ReadInstruction()) +        return Error("Read instruction failed!"); + +    bool emulation_result = emulator_ap->EvaluateInstruction(eEmulateInstructionOptionAutoAdvancePC); + +    const RegisterInfo* reg_info_pc = register_context_sp->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); +    const RegisterInfo* reg_info_flags = register_context_sp->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); + +    auto pc_it = baton.m_register_values.find(reg_info_pc->kinds[eRegisterKindDWARF]); +    auto flags_it = baton.m_register_values.find(reg_info_flags->kinds[eRegisterKindDWARF]); + +    lldb::addr_t next_pc; +    lldb::addr_t next_flags; +    if (emulation_result) +    { +        assert(pc_it != baton.m_register_values.end() && "Emulation was successfull but PC wasn't updated"); +        next_pc = pc_it->second.GetAsUInt64(); + +        if (flags_it != baton.m_register_values.end()) +            next_flags = flags_it->second.GetAsUInt64(); +        else +            next_flags = ReadFlags (register_context_sp.get()); +    } +    else if (pc_it == baton.m_register_values.end()) +    { +        // Emulate instruction failed and it haven't changed PC. Advance PC +        // with the size of the current opcode because the emulation of all +        // PC modifying instruction should be successful. The failure most +        // likely caused by a not supported instruction which don't modify PC. +        next_pc = register_context_sp->GetPC() + emulator_ap->GetOpcode().GetByteSize(); +        next_flags = ReadFlags (register_context_sp.get()); +    } +    else +    { +        // The instruction emulation failed after it modified the PC. It is an +        // unknown error where we can't continue because the next instruction is +        // modifying the PC but we don't  know how. +        return Error ("Instruction emulation failed unexpectedly."); +    } + +    if (m_arch.GetMachine() == llvm::Triple::arm) +    { +        if (next_flags & 0x20) +        { +            // Thumb mode +            error = SetSoftwareBreakpoint(next_pc, 2); +        } +        else +        { +            // Arm mode +            error = SetSoftwareBreakpoint(next_pc, 4); +        } +    } +    else if (m_arch.GetMachine() == llvm::Triple::mips64 +            || m_arch.GetMachine() == llvm::Triple::mips64el +            || m_arch.GetMachine() == llvm::Triple::mips +            || m_arch.GetMachine() == llvm::Triple::mipsel) +        error = SetSoftwareBreakpoint(next_pc, 4); +    else +    { +        // No size hint is given for the next breakpoint +        error = SetSoftwareBreakpoint(next_pc, 0); +    } + +    if (error.Fail()) +        return error; + +    m_threads_stepping_with_breakpoint.insert({thread.GetID(), next_pc}); + +    return Error(); +} + +bool +NativeProcessLinux::SupportHardwareSingleStepping() const +{ +    if (m_arch.GetMachine() == llvm::Triple::arm +        || m_arch.GetMachine() == llvm::Triple::mips64 || m_arch.GetMachine() == llvm::Triple::mips64el +        || m_arch.GetMachine() == llvm::Triple::mips || m_arch.GetMachine() == llvm::Triple::mipsel) +        return false; +    return true; +} + +Error +NativeProcessLinux::Resume (const ResumeActionList &resume_actions) +{ +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); +    if (log) +        log->Printf ("NativeProcessLinux::%s called: pid %" PRIu64, __FUNCTION__, GetID ()); + +    bool software_single_step = !SupportHardwareSingleStepping(); + +    Mutex::Locker locker (m_threads_mutex); + +    if (software_single_step) +    { +        for (auto thread_sp : m_threads) +        { +            assert (thread_sp && "thread list should not contain NULL threads"); + +            const ResumeAction *const action = resume_actions.GetActionForThread (thread_sp->GetID (), true); +            if (action == nullptr) +                continue; + +            if (action->state == eStateStepping) +            { +                Error error = SetupSoftwareSingleStepping(static_cast<NativeThreadLinux &>(*thread_sp)); +                if (error.Fail()) +                    return error; +            } +        } +    } + +    for (auto thread_sp : m_threads) +    { +        assert (thread_sp && "thread list should not contain NULL threads"); + +        const ResumeAction *const action = resume_actions.GetActionForThread (thread_sp->GetID (), true); + +        if (action == nullptr) +        { +            if (log) +                log->Printf ("NativeProcessLinux::%s no action specified for pid %" PRIu64 " tid %" PRIu64, +                    __FUNCTION__, GetID (), thread_sp->GetID ()); +            continue; +        } + +        if (log) +        { +            log->Printf ("NativeProcessLinux::%s processing resume action state %s for pid %" PRIu64 " tid %" PRIu64,  +                    __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); +        } + +        switch (action->state) +        { +        case eStateRunning: +        case eStateStepping: +        { +            // Run the thread, possibly feeding it the signal. +            const int signo = action->signal; +            ResumeThread(static_cast<NativeThreadLinux &>(*thread_sp), action->state, signo); +            break; +        } + +        case eStateSuspended: +        case eStateStopped: +            lldbassert(0 && "Unexpected state"); + +        default: +            return Error ("NativeProcessLinux::%s (): unexpected state %s specified for pid %" PRIu64 ", tid %" PRIu64, +                    __FUNCTION__, StateAsCString (action->state), GetID (), thread_sp->GetID ()); +        } +    } + +    return Error(); +} + +Error +NativeProcessLinux::Halt () +{ +    Error error; + +    if (kill (GetID (), SIGSTOP) != 0) +        error.SetErrorToErrno (); + +    return error; +} + +Error +NativeProcessLinux::Detach () +{ +    Error error; + +    // Stop monitoring the inferior. +    m_sigchld_handle.reset(); + +    // Tell ptrace to detach from the process. +    if (GetID () == LLDB_INVALID_PROCESS_ID) +        return error; + +    for (auto thread_sp : m_threads) +    { +        Error e = Detach(thread_sp->GetID()); +        if (e.Fail()) +            error = e; // Save the error, but still attempt to detach from other threads. +    } + +    return error; +} + +Error +NativeProcessLinux::Signal (int signo) +{ +    Error error; + +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); +    if (log) +        log->Printf ("NativeProcessLinux::%s: sending signal %d (%s) to pid %" PRIu64, +                __FUNCTION__, signo, Host::GetSignalAsCString(signo), GetID()); + +    if (kill(GetID(), signo)) +        error.SetErrorToErrno(); + +    return error; +} + +Error +NativeProcessLinux::Interrupt () +{ +    // Pick a running thread (or if none, a not-dead stopped thread) as +    // the chosen thread that will be the stop-reason thread. +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); + +    NativeThreadProtocolSP running_thread_sp; +    NativeThreadProtocolSP stopped_thread_sp; +         +    if (log) +        log->Printf ("NativeProcessLinux::%s selecting running thread for interrupt target", __FUNCTION__); + +    Mutex::Locker locker (m_threads_mutex); + +    for (auto thread_sp : m_threads) +    { +        // The thread shouldn't be null but lets just cover that here. +        if (!thread_sp) +            continue; + +        // If we have a running or stepping thread, we'll call that the +        // target of the interrupt. +        const auto thread_state = thread_sp->GetState (); +        if (thread_state == eStateRunning || +            thread_state == eStateStepping) +        { +            running_thread_sp = thread_sp; +            break; +        } +        else if (!stopped_thread_sp && StateIsStoppedState (thread_state, true)) +        { +            // Remember the first non-dead stopped thread.  We'll use that as a backup if there are no running threads. +            stopped_thread_sp = thread_sp; +        } +    } + +    if (!running_thread_sp && !stopped_thread_sp) +    { +        Error error("found no running/stepping or live stopped threads as target for interrupt"); +        if (log) +            log->Printf ("NativeProcessLinux::%s skipping due to error: %s", __FUNCTION__, error.AsCString ()); + +        return error; +    } + +    NativeThreadProtocolSP deferred_signal_thread_sp = running_thread_sp ? running_thread_sp : stopped_thread_sp; + +    if (log) +        log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " %s tid %" PRIu64 " chosen for interrupt target", +                     __FUNCTION__, +                     GetID (), +                     running_thread_sp ? "running" : "stopped", +                     deferred_signal_thread_sp->GetID ()); + +    StopRunningThreads(deferred_signal_thread_sp->GetID()); + +    return Error(); +} + +Error +NativeProcessLinux::Kill () +{ +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); +    if (log) +        log->Printf ("NativeProcessLinux::%s called for PID %" PRIu64, __FUNCTION__, GetID ()); + +    Error 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. +            if (log) +                log->Printf ("NativeProcessLinux::%s ignored for PID %" PRIu64 " due to current state: %s", __FUNCTION__, 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; +} + +static Error +ParseMemoryRegionInfoFromProcMapsLine (const std::string &maps_line, MemoryRegionInfo &memory_region_info) +{ +    memory_region_info.Clear(); + +    StringExtractor line_extractor (maps_line.c_str ()); + +    // Format: {address_start_hex}-{address_end_hex} perms offset  dev   inode   pathname +    // perms: rwxp   (letter is present if set, '-' if not, final character is p=private, s=shared). + +    // Parse out the starting address +    lldb::addr_t start_address = line_extractor.GetHexMaxU64 (false, 0); + +    // Parse out hyphen separating start and end address from range. +    if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != '-')) +        return Error ("malformed /proc/{pid}/maps entry, missing dash between address range"); + +    // Parse out the ending address +    lldb::addr_t end_address = line_extractor.GetHexMaxU64 (false, start_address); + +    // Parse out the space after the address. +    if (!line_extractor.GetBytesLeft () || (line_extractor.GetChar () != ' ')) +        return Error ("malformed /proc/{pid}/maps entry, missing space after range"); + +    // Save the range. +    memory_region_info.GetRange ().SetRangeBase (start_address); +    memory_region_info.GetRange ().SetRangeEnd (end_address); + +    // Parse out each permission entry. +    if (line_extractor.GetBytesLeft () < 4) +        return Error ("malformed /proc/{pid}/maps entry, missing some portion of permissions"); + +    // Handle read permission. +    const char read_perm_char = line_extractor.GetChar (); +    if (read_perm_char == 'r') +        memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eYes); +    else +    { +        assert ( (read_perm_char == '-') && "unexpected /proc/{pid}/maps read permission char" ); +        memory_region_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); +    } + +    // Handle write permission. +    const char write_perm_char = line_extractor.GetChar (); +    if (write_perm_char == 'w') +        memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eYes); +    else +    { +        assert ( (write_perm_char == '-') && "unexpected /proc/{pid}/maps write permission char" ); +        memory_region_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); +    } + +    // Handle execute permission. +    const char exec_perm_char = line_extractor.GetChar (); +    if (exec_perm_char == 'x') +        memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eYes); +    else +    { +        assert ( (exec_perm_char == '-') && "unexpected /proc/{pid}/maps exec permission char" ); +        memory_region_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); +    } + +    return Error (); +} + +Error +NativeProcessLinux::GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) +{ +    // FIXME review that the final memory region returned extends to the end of the virtual address space, +    // with no perms if it is not mapped. + +    // Use an approach that reads memory regions from /proc/{pid}/maps. +    // Assume proc maps entries are in ascending order. +    // FIXME assert if we find differently. +    Mutex::Locker locker (m_mem_region_cache_mutex); + +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); +    Error error; + +    if (m_supports_mem_region == LazyBool::eLazyBoolNo) +    { +        // We're done. +        error.SetErrorString ("unsupported"); +        return error; +    } + +    // 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 ()) +    { +        error = ProcFileReader::ProcessLineByLine (GetID (), "maps", +             [&] (const std::string &line) -> bool +             { +                 MemoryRegionInfo info; +                 const Error parse_error = ParseMemoryRegionInfoFromProcMapsLine (line, info); +                 if (parse_error.Success ()) +                 { +                     m_mem_region_cache.push_back (info); +                     return true; +                 } +                 else +                 { +                     if (log) +                         log->Printf ("NativeProcessLinux::%s failed to parse proc maps line '%s': %s", __FUNCTION__, line.c_str (), error.AsCString ()); +                     return false; +                 } +             }); + +        // If we had an error, we'll mark unsupported. +        if (error.Fail ()) +        { +            m_supports_mem_region = LazyBool::eLazyBoolNo; +            return error; +        } +        else if (m_mem_region_cache.empty ()) +        { +            // No entries after attempting to read them.  This shouldn't happen if /proc/{pid}/maps +            // is supported.  Assume we don't support map entries via procfs. +            if (log) +                log->Printf ("NativeProcessLinux::%s failed to find any procfs maps entries, assuming no support for memory region metadata retrieval", __FUNCTION__); +            m_supports_mem_region = LazyBool::eLazyBoolNo; +            error.SetErrorString ("not supported"); +            return error; +        } + +        if (log) +            log->Printf ("NativeProcessLinux::%s read %" PRIu64 " memory region entries from /proc/%" PRIu64 "/maps", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ()), GetID ()); + +        // We support memory retrieval, remember that. +        m_supports_mem_region = LazyBool::eLazyBoolYes; +    } +    else +    { +        if (log) +            log->Printf ("NativeProcessLinux::%s reusing %" PRIu64 " cached memory region entries", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); +    } + +    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; + +        // Sanity check assumption that /proc/{pid}/maps entries are ascending. +        assert ((proc_entry_info.GetRange ().GetRangeBase () >= prev_base_address) && "descending /proc/pid/maps entries detected, unexpected"); +        prev_base_address = proc_entry_info.GetRange ().GetRangeBase (); + +        // 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); + +            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); +    switch (m_arch.GetAddressByteSize()) +    { +        case 4: +            range_info.GetRange ().SetByteSize (0x100000000ull - load_addr); +            break; +        case 8: +            range_info.GetRange ().SetByteSize (0ull - load_addr); +            break; +        default: +            assert(false && "Unrecognized data byte size"); +            break; +    } +    range_info.SetReadable (MemoryRegionInfo::OptionalBool::eNo); +    range_info.SetWritable (MemoryRegionInfo::OptionalBool::eNo); +    range_info.SetExecutable (MemoryRegionInfo::OptionalBool::eNo); +    return error; +} + +void +NativeProcessLinux::DoStopIDBumped (uint32_t newBumpId) +{ +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); +    if (log) +        log->Printf ("NativeProcessLinux::%s(newBumpId=%" PRIu32 ") called", __FUNCTION__, newBumpId); + +    { +        Mutex::Locker locker (m_mem_region_cache_mutex); +        if (log) +            log->Printf ("NativeProcessLinux::%s clearing %" PRIu64 " entries from the cache", __FUNCTION__, static_cast<uint64_t> (m_mem_region_cache.size ())); +        m_mem_region_cache.clear (); +    } +} + +Error +NativeProcessLinux::AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) +{ +    // FIXME implementing this requires the equivalent of +    // InferiorCallPOSIX::InferiorCallMmap, which depends on +    // functional ThreadPlans working with Native*Protocol. +#if 1 +    return Error ("not implemented yet"); +#else +    addr = LLDB_INVALID_ADDRESS; + +    unsigned prot = 0; +    if (permissions & lldb::ePermissionsReadable) +        prot |= eMmapProtRead; +    if (permissions & lldb::ePermissionsWritable) +        prot |= eMmapProtWrite; +    if (permissions & lldb::ePermissionsExecutable) +        prot |= eMmapProtExec; + +    // TODO implement this directly in NativeProcessLinux +    // (and lift to NativeProcessPOSIX if/when that class is +    // refactored out). +    if (InferiorCallMmap(this, addr, 0, size, prot, +                         eMmapFlagsAnon | eMmapFlagsPrivate, -1, 0)) { +        m_addr_to_mmap_size[addr] = size; +        return Error (); +    } else { +        addr = LLDB_INVALID_ADDRESS; +        return Error("unable to allocate %" PRIu64 " bytes of memory with permissions %s", size, GetPermissionsAsCString (permissions)); +    } +#endif +} + +Error +NativeProcessLinux::DeallocateMemory (lldb::addr_t addr) +{ +    // FIXME see comments in AllocateMemory - required lower-level +    // bits not in place yet (ThreadPlans) +    return Error ("not implemented"); +} + +lldb::addr_t +NativeProcessLinux::GetSharedLibraryInfoAddress () +{ +#if 1 +    // punt on this for now +    return LLDB_INVALID_ADDRESS; +#else +    // Return the image info address for the exe module +#if 1 +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); + +    ModuleSP module_sp; +    Error error = GetExeModuleSP (module_sp); +    if (error.Fail ()) +    { +         if (log) +            log->Warning ("NativeProcessLinux::%s failed to retrieve exe module: %s", __FUNCTION__, error.AsCString ()); +        return LLDB_INVALID_ADDRESS; +    } + +    if (module_sp == nullptr) +    { +         if (log) +            log->Warning ("NativeProcessLinux::%s exe module returned was NULL", __FUNCTION__); +         return LLDB_INVALID_ADDRESS; +    } + +    ObjectFileSP object_file_sp = module_sp->GetObjectFile (); +    if (object_file_sp == nullptr) +    { +         if (log) +            log->Warning ("NativeProcessLinux::%s exe module returned a NULL object file", __FUNCTION__); +         return LLDB_INVALID_ADDRESS; +    } + +    return obj_file_sp->GetImageInfoAddress(); +#else +    Target *target = &GetTarget(); +    ObjectFile *obj_file = target->GetExecutableModule()->GetObjectFile(); +    Address addr = obj_file->GetImageInfoAddress(target); + +    if (addr.IsValid()) +        return addr.GetLoadAddress(target); +    return LLDB_INVALID_ADDRESS; +#endif +#endif // punt on this for now +} + +size_t +NativeProcessLinux::UpdateThreads () +{ +    // The NativeProcessLinux monitoring threads are always up to date +    // with respect to thread state and they keep the thread list +    // populated properly. All this method needs to do is return the +    // thread count. +    Mutex::Locker locker (m_threads_mutex); +    return m_threads.size (); +} + +bool +NativeProcessLinux::GetArchitecture (ArchSpec &arch) const +{ +    arch = m_arch; +    return true; +} + +Error +NativeProcessLinux::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: +        case llvm::Triple::x86_64: +            actual_opcode_size = static_cast<uint32_t> (sizeof(g_i386_opcode)); +            return Error (); + +        case llvm::Triple::arm: +        case llvm::Triple::aarch64: +        case llvm::Triple::mips64: +        case llvm::Triple::mips64el: +        case llvm::Triple::mips: +        case llvm::Triple::mipsel: +            // On these architectures the PC don't get updated for breakpoint hits +            actual_opcode_size = 0; +            return Error (); +         +        default: +            assert(false && "CPU type not supported!"); +            return Error ("CPU type not supported"); +    } +} + +Error +NativeProcessLinux::SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) +{ +    if (hardware) +        return Error ("NativeProcessLinux does not support hardware breakpoints"); +    else +        return SetSoftwareBreakpoint (addr, size); +} + +Error +NativeProcessLinux::GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, +                                                     size_t &actual_opcode_size, +                                                     const uint8_t *&trap_opcode_bytes) +{ +    // FIXME put this behind a breakpoint protocol class that can be set per +    // architecture.  Need MIPS support here. +    static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 }; +    // The ARM reference recommends the use of 0xe7fddefe and 0xdefe but the +    // linux kernel does otherwise. +    static const uint8_t g_arm_breakpoint_opcode[] = { 0xf0, 0x01, 0xf0, 0xe7 }; +    static const uint8_t g_i386_opcode [] = { 0xCC }; +    static const uint8_t g_mips64_opcode[] = { 0x00, 0x00, 0x00, 0x0d }; +    static const uint8_t g_mips64el_opcode[] = { 0x0d, 0x00, 0x00, 0x00 }; +    static const uint8_t g_thumb_breakpoint_opcode[] = { 0x01, 0xde }; + +    switch (m_arch.GetMachine ()) +    { +    case llvm::Triple::aarch64: +        trap_opcode_bytes = g_aarch64_opcode; +        actual_opcode_size = sizeof(g_aarch64_opcode); +        return Error (); + +    case llvm::Triple::arm: +        switch (trap_opcode_size_hint) +        { +        case 2: +            trap_opcode_bytes = g_thumb_breakpoint_opcode; +            actual_opcode_size = sizeof(g_thumb_breakpoint_opcode); +            return Error (); +        case 4: +            trap_opcode_bytes = g_arm_breakpoint_opcode; +            actual_opcode_size = sizeof(g_arm_breakpoint_opcode); +            return Error (); +        default: +            assert(false && "Unrecognised trap opcode size hint!"); +            return Error ("Unrecognised trap opcode size hint!"); +        } + +    case llvm::Triple::x86: +    case llvm::Triple::x86_64: +        trap_opcode_bytes = g_i386_opcode; +        actual_opcode_size = sizeof(g_i386_opcode); +        return Error (); + +    case llvm::Triple::mips: +    case llvm::Triple::mips64: +        trap_opcode_bytes = g_mips64_opcode; +        actual_opcode_size = sizeof(g_mips64_opcode); +        return Error (); + +    case llvm::Triple::mipsel: +    case llvm::Triple::mips64el: +        trap_opcode_bytes = g_mips64el_opcode; +        actual_opcode_size = sizeof(g_mips64el_opcode); +        return Error (); + +    default: +        assert(false && "CPU type not supported!"); +        return Error ("CPU type not supported"); +    } +} + +#if 0 +ProcessMessage::CrashReason +NativeProcessLinux::GetCrashReasonForSIGSEGV(const siginfo_t *info) +{ +    ProcessMessage::CrashReason reason; +    assert(info->si_signo == SIGSEGV); + +    reason = ProcessMessage::eInvalidCrashReason; + +    switch (info->si_code) +    { +    default: +        assert(false && "unexpected si_code for SIGSEGV"); +        break; +    case SI_KERNEL: +        // Linux will occasionally send spurious SI_KERNEL codes. +        // (this is poorly documented in sigaction) +        // One way to get this is via unaligned SIMD loads. +        reason = ProcessMessage::eInvalidAddress; // for lack of anything better +        break; +    case SEGV_MAPERR: +        reason = ProcessMessage::eInvalidAddress; +        break; +    case SEGV_ACCERR: +        reason = ProcessMessage::ePrivilegedAddress; +        break; +    } + +    return reason; +} +#endif + + +#if 0 +ProcessMessage::CrashReason +NativeProcessLinux::GetCrashReasonForSIGILL(const siginfo_t *info) +{ +    ProcessMessage::CrashReason reason; +    assert(info->si_signo == SIGILL); + +    reason = ProcessMessage::eInvalidCrashReason; + +    switch (info->si_code) +    { +    default: +        assert(false && "unexpected si_code for SIGILL"); +        break; +    case ILL_ILLOPC: +        reason = ProcessMessage::eIllegalOpcode; +        break; +    case ILL_ILLOPN: +        reason = ProcessMessage::eIllegalOperand; +        break; +    case ILL_ILLADR: +        reason = ProcessMessage::eIllegalAddressingMode; +        break; +    case ILL_ILLTRP: +        reason = ProcessMessage::eIllegalTrap; +        break; +    case ILL_PRVOPC: +        reason = ProcessMessage::ePrivilegedOpcode; +        break; +    case ILL_PRVREG: +        reason = ProcessMessage::ePrivilegedRegister; +        break; +    case ILL_COPROC: +        reason = ProcessMessage::eCoprocessorError; +        break; +    case ILL_BADSTK: +        reason = ProcessMessage::eInternalStackError; +        break; +    } + +    return reason; +} +#endif + +#if 0 +ProcessMessage::CrashReason +NativeProcessLinux::GetCrashReasonForSIGFPE(const siginfo_t *info) +{ +    ProcessMessage::CrashReason reason; +    assert(info->si_signo == SIGFPE); + +    reason = ProcessMessage::eInvalidCrashReason; + +    switch (info->si_code) +    { +    default: +        assert(false && "unexpected si_code for SIGFPE"); +        break; +    case FPE_INTDIV: +        reason = ProcessMessage::eIntegerDivideByZero; +        break; +    case FPE_INTOVF: +        reason = ProcessMessage::eIntegerOverflow; +        break; +    case FPE_FLTDIV: +        reason = ProcessMessage::eFloatDivideByZero; +        break; +    case FPE_FLTOVF: +        reason = ProcessMessage::eFloatOverflow; +        break; +    case FPE_FLTUND: +        reason = ProcessMessage::eFloatUnderflow; +        break; +    case FPE_FLTRES: +        reason = ProcessMessage::eFloatInexactResult; +        break; +    case FPE_FLTINV: +        reason = ProcessMessage::eFloatInvalidOperation; +        break; +    case FPE_FLTSUB: +        reason = ProcessMessage::eFloatSubscriptRange; +        break; +    } + +    return reason; +} +#endif + +#if 0 +ProcessMessage::CrashReason +NativeProcessLinux::GetCrashReasonForSIGBUS(const siginfo_t *info) +{ +    ProcessMessage::CrashReason reason; +    assert(info->si_signo == SIGBUS); + +    reason = ProcessMessage::eInvalidCrashReason; + +    switch (info->si_code) +    { +    default: +        assert(false && "unexpected si_code for SIGBUS"); +        break; +    case BUS_ADRALN: +        reason = ProcessMessage::eIllegalAlignment; +        break; +    case BUS_ADRERR: +        reason = ProcessMessage::eIllegalAddress; +        break; +    case BUS_OBJERR: +        reason = ProcessMessage::eHardwareError; +        break; +    } + +    return reason; +} +#endif + +Error +NativeProcessLinux::ReadMemory (lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) +{ +    if (ProcessVmReadvSupported()) { +        // The process_vm_readv path is about 50 times faster than ptrace api. We want to use +        // this syscall if it is supported. + +        const ::pid_t pid = GetID(); + +        struct iovec local_iov, remote_iov; +        local_iov.iov_base = buf; +        local_iov.iov_len = size; +        remote_iov.iov_base = reinterpret_cast<void *>(addr); +        remote_iov.iov_len = size; + +        bytes_read = process_vm_readv(pid, &local_iov, 1, &remote_iov, 1, 0); +        const bool success = bytes_read == size; + +        Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); +        if (log) +            log->Printf ("NativeProcessLinux::%s using process_vm_readv to read %zd bytes from inferior address 0x%" PRIx64": %s", +                    __FUNCTION__, size, addr, success ? "Success" : strerror(errno)); + +        if (success) +            return Error(); +        // else +        //     the call failed for some reason, let's retry the read using ptrace api. +    } + +    unsigned char *dst = static_cast<unsigned char*>(buf); +    size_t remainder; +    long data; + +    Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); +    if (log) +        ProcessPOSIXLog::IncNestLevel(); +    if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) +        log->Printf ("NativeProcessLinux::%s(%p, %p, %zd, _)", __FUNCTION__, (void*)addr, buf, size); + +    for (bytes_read = 0; bytes_read < size; bytes_read += remainder) +    { +        Error error = NativeProcessLinux::PtraceWrapper(PTRACE_PEEKDATA, GetID(), (void*)addr, nullptr, 0, &data); +        if (error.Fail()) +        { +            if (log) +                ProcessPOSIXLog::DecNestLevel(); +            return error; +        } + +        remainder = size - bytes_read; +        remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; + +        // Copy the data into our buffer +        memcpy(dst, &data, remainder); + +        if (log && ProcessPOSIXLog::AtTopNestLevel() && +                (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || +                        (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && +                                size <= POSIX_LOG_MEMORY_SHORT_BYTES))) +        { +            uintptr_t print_dst = 0; +            // Format bytes from data by moving into print_dst for log output +            for (unsigned i = 0; i < remainder; ++i) +                print_dst |= (((data >> i*8) & 0xFF) << i*8); +            log->Printf ("NativeProcessLinux::%s() [0x%" PRIx64 "]:0x%" PRIx64 " (0x%" PRIx64 ")", +                    __FUNCTION__, addr, uint64_t(print_dst), uint64_t(data)); +        } +        addr += k_ptrace_word_size; +        dst += k_ptrace_word_size; +    } + +    if (log) +        ProcessPOSIXLog::DecNestLevel(); +    return Error(); +} + +Error +NativeProcessLinux::ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) +{ +    Error error = ReadMemory(addr, buf, size, bytes_read); +    if (error.Fail()) return error; +    return m_breakpoint_list.RemoveTrapsFromBuffer(addr, buf, size); +} + +Error +NativeProcessLinux::WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) +{ +    const unsigned char *src = static_cast<const unsigned char*>(buf); +    size_t remainder; +    Error error; + +    Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_ALL)); +    if (log) +        ProcessPOSIXLog::IncNestLevel(); +    if (log && ProcessPOSIXLog::AtTopNestLevel() && log->GetMask().Test(POSIX_LOG_MEMORY)) +        log->Printf ("NativeProcessLinux::%s(0x%" PRIx64 ", %p, %zu)", __FUNCTION__, addr, buf, size); + +    for (bytes_written = 0; bytes_written < size; bytes_written += remainder) +    { +        remainder = size - bytes_written; +        remainder = remainder > k_ptrace_word_size ? k_ptrace_word_size : remainder; + +        if (remainder == k_ptrace_word_size) +        { +            unsigned long data = 0; +            memcpy(&data, src, k_ptrace_word_size); + +            if (log && ProcessPOSIXLog::AtTopNestLevel() && +                    (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || +                            (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && +                                    size <= POSIX_LOG_MEMORY_SHORT_BYTES))) +                log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, +                        (void*)addr, *(const unsigned long*)src, data); + +            error = NativeProcessLinux::PtraceWrapper(PTRACE_POKEDATA, GetID(), (void*)addr, (void*)data); +            if (error.Fail()) +            { +                if (log) +                    ProcessPOSIXLog::DecNestLevel(); +                return error; +            } +        } +        else +        { +            unsigned char buff[8]; +            size_t bytes_read; +            error = ReadMemory(addr, buff, k_ptrace_word_size, bytes_read); +            if (error.Fail()) +            { +                if (log) +                    ProcessPOSIXLog::DecNestLevel(); +                return error; +            } + +            memcpy(buff, src, remainder); + +            size_t bytes_written_rec; +            error = WriteMemory(addr, buff, k_ptrace_word_size, bytes_written_rec); +            if (error.Fail()) +            { +                if (log) +                    ProcessPOSIXLog::DecNestLevel(); +                return error; +            } + +            if (log && ProcessPOSIXLog::AtTopNestLevel() && +                    (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_LONG) || +                            (log->GetMask().Test(POSIX_LOG_MEMORY_DATA_SHORT) && +                                    size <= POSIX_LOG_MEMORY_SHORT_BYTES))) +                log->Printf ("NativeProcessLinux::%s() [%p]:0x%lx (0x%lx)", __FUNCTION__, +                        (void*)addr, *(const unsigned long*)src, *(unsigned long*)buff); +        } + +        addr += k_ptrace_word_size; +        src += k_ptrace_word_size; +    } +    if (log) +        ProcessPOSIXLog::DecNestLevel(); +    return error; +} + +Error +NativeProcessLinux::Resume (lldb::tid_t tid, uint32_t signo) +{ +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS)); + +    if (log) +        log->Printf ("NativeProcessLinux::%s() resuming thread = %"  PRIu64 " with signal %s", __FUNCTION__, tid, +                                 Host::GetSignalAsCString(signo)); + + + +    intptr_t data = 0; + +    if (signo != LLDB_INVALID_SIGNAL_NUMBER) +        data = signo; + +    Error error = PtraceWrapper(PTRACE_CONT, tid, nullptr, (void*)data); + +    if (log) +        log->Printf ("NativeProcessLinux::%s() resuming thread = %"  PRIu64 " result = %s", __FUNCTION__, tid, error.Success() ? "true" : "false"); +    return error; +} + +Error +NativeProcessLinux::SingleStep(lldb::tid_t tid, uint32_t signo) +{ +    intptr_t data = 0; + +    if (signo != LLDB_INVALID_SIGNAL_NUMBER) +        data = signo; + +    // If hardware single-stepping is not supported, we just do a continue. The breakpoint on the +    // next instruction has been setup in NativeProcessLinux::Resume. +    return PtraceWrapper(SupportHardwareSingleStepping() ? PTRACE_SINGLESTEP : PTRACE_CONT, +            tid, nullptr, (void*)data); +} + +Error +NativeProcessLinux::GetSignalInfo(lldb::tid_t tid, void *siginfo) +{ +    return PtraceWrapper(PTRACE_GETSIGINFO, tid, nullptr, siginfo); +} + +Error +NativeProcessLinux::GetEventMessage(lldb::tid_t tid, unsigned long *message) +{ +    return PtraceWrapper(PTRACE_GETEVENTMSG, tid, nullptr, message); +} + +Error +NativeProcessLinux::Detach(lldb::tid_t tid) +{ +    if (tid == LLDB_INVALID_THREAD_ID) +        return Error(); + +    return PtraceWrapper(PTRACE_DETACH, tid); +} + +bool +NativeProcessLinux::DupDescriptor(const FileSpec &file_spec, int fd, int flags) +{ +    int target_fd = open(file_spec.GetCString(), flags, 0666); + +    if (target_fd == -1) +        return false; + +    if (dup2(target_fd, fd) == -1) +        return false; + +    return (close(target_fd) == -1) ? false : true; +} + +bool +NativeProcessLinux::HasThreadNoLock (lldb::tid_t thread_id) +{ +    for (auto thread_sp : m_threads) +    { +        assert (thread_sp && "thread list should not contain NULL threads"); +        if (thread_sp->GetID () == thread_id) +        { +            // We have this thread. +            return true; +        } +    } + +    // We don't have this thread. +    return false; +} + +bool +NativeProcessLinux::StopTrackingThread (lldb::tid_t thread_id) +{ +    Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); + +    if (log) +        log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ")", __FUNCTION__, thread_id); + +    bool found = false; + +    Mutex::Locker locker (m_threads_mutex); +    for (auto it = m_threads.begin (); it != m_threads.end (); ++it) +    { +        if (*it && ((*it)->GetID () == thread_id)) +        { +            m_threads.erase (it); +            found = true; +            break; +        } +    } + +    SignalIfAllThreadsStopped(); + +    return found; +} + +NativeThreadLinuxSP +NativeProcessLinux::AddThread (lldb::tid_t thread_id) +{ +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); + +    Mutex::Locker locker (m_threads_mutex); + +    if (log) +    { +        log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " adding thread with tid %" PRIu64, +                __FUNCTION__, +                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); + +    auto thread_sp = std::make_shared<NativeThreadLinux>(this, thread_id); +    m_threads.push_back (thread_sp); +    return thread_sp; +} + +Error +NativeProcessLinux::FixupBreakpointPCAsNeeded(NativeThreadLinux &thread) +{ +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); + +    Error error; + +    // Find out the size of a breakpoint (might depend on where we are in the code). +    NativeRegisterContextSP context_sp = thread.GetRegisterContext(); +    if (!context_sp) +    { +        error.SetErrorString ("cannot get a NativeRegisterContext for the thread"); +        if (log) +            log->Printf ("NativeProcessLinux::%s failed: %s", __FUNCTION__, error.AsCString ()); +        return error; +    } + +    uint32_t breakpoint_size = 0; +    error = GetSoftwareBreakpointPCOffset(breakpoint_size); +    if (error.Fail ()) +    { +        if (log) +            log->Printf ("NativeProcessLinux::%s GetBreakpointSize() failed: %s", __FUNCTION__, error.AsCString ()); +        return error; +    } +    else +    { +        if (log) +            log->Printf ("NativeProcessLinux::%s breakpoint size: %" PRIu32, __FUNCTION__, breakpoint_size); +    } + +    // First try probing for a breakpoint at a software breakpoint location: PC - breakpoint size. +    const lldb::addr_t initial_pc_addr = context_sp->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. +        if (log) +            log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " no lldb breakpoint found at current pc with adjustment: 0x%" PRIx64, __FUNCTION__, GetID (), breakpoint_addr); +        return Error (); +    } + +    // If the breakpoint is not a software breakpoint, nothing to do. +    if (!breakpoint_sp->IsSoftwareBreakpoint ()) +    { +        if (log) +            log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " breakpoint found at 0x%" PRIx64 ", not software, nothing to adjust", __FUNCTION__, GetID (), breakpoint_addr); +        return Error (); +    } + +    // +    // 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? +        if (log) +            log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " breakpoint found at 0x%" PRIx64 ", it is software, but the size is zero, nothing to do (unexpected)", __FUNCTION__, GetID (), breakpoint_addr); +        return Error (); +    } + +    // Change the program counter. +    if (log) +        log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": changing PC from 0x%" PRIx64 " to 0x%" PRIx64, __FUNCTION__, GetID(), thread.GetID(), initial_pc_addr, breakpoint_addr); + +    error = context_sp->SetPC (breakpoint_addr); +    if (error.Fail ()) +    { +        if (log) +            log->Printf ("NativeProcessLinux::%s pid %" PRIu64 " tid %" PRIu64 ": failed to set PC: %s", __FUNCTION__, GetID(), thread.GetID(), error.AsCString ()); +        return error; +    } + +    return error; +} + +Error +NativeProcessLinux::GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) +{ +    FileSpec module_file_spec(module_path, true); + +    bool found = false; +    file_spec.Clear(); +    ProcFileReader::ProcessLineByLine(GetID(), "maps", +        [&] (const std::string &line) +        { +            SmallVector<StringRef, 16> columns; +            StringRef(line).split(columns, " ", -1, false); +            if (columns.size() < 6) +                return true; // continue searching + +            FileSpec this_file_spec(columns[5].str().c_str(), false); +            if (this_file_spec.GetFilename() != module_file_spec.GetFilename()) +                return true; // continue searching + +            file_spec = this_file_spec; +            found = true; +            return false; // we are done +        }); + +    if (! found) +        return Error("Module file (%s) not found in /proc/%" PRIu64 "/maps file!", +                module_file_spec.GetFilename().AsCString(), GetID()); + +    return Error(); +} + +Error +NativeProcessLinux::GetFileLoadAddress(const llvm::StringRef& file_name, lldb::addr_t& load_addr) +{ +    load_addr = LLDB_INVALID_ADDRESS; +    Error error = ProcFileReader::ProcessLineByLine (GetID (), "maps", +        [&] (const std::string &line) -> bool +        { +            StringRef maps_row(line); +  +            SmallVector<StringRef, 16> maps_columns; +            maps_row.split(maps_columns, StringRef(" "), -1, false); +  +            if (maps_columns.size() < 6) +            { +                // Return true to continue reading the proc file +                return true; +            } + +            if (maps_columns[5] == file_name) +            { +                StringExtractor addr_extractor(maps_columns[0].str().c_str()); +                load_addr = addr_extractor.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);  + +                // Return false to stop reading the proc file further +                return false; +            } +  +            // Return true to continue reading the proc file +            return true; +        }); +    return error;  +} + +NativeThreadLinuxSP +NativeProcessLinux::GetThreadByID(lldb::tid_t tid) +{ +    return std::static_pointer_cast<NativeThreadLinux>(NativeProcessProtocol::GetThreadByID(tid)); +} + +Error +NativeProcessLinux::ResumeThread(NativeThreadLinux &thread, lldb::StateType state, int signo) +{ +    Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); + +    if (log) +        log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ")", +                __FUNCTION__, thread.GetID()); +     +    // Before we do the resume below, first check if we have a pending +    // stop notification that is currently waiting for +    // all threads to stop.  This is potentially a buggy situation since +    // we're ostensibly waiting for threads to stop before we send out the +    // pending notification, and here we are resuming one before we send +    // out the pending stop notification. +    if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID && log) +    { +        log->Printf("NativeProcessLinux::%s about to resume tid %" PRIu64 " per explicit request but we have a pending stop notification (tid %" PRIu64 ") that is actively waiting for this thread to stop. Valid sequence of events?", __FUNCTION__, thread.GetID(), m_pending_notification_tid); +    } + +    // Request a resume.  We expect this to be synchronous and the system +    // to reflect it is running after this completes. +    switch (state) +    { +    case eStateRunning: +    { +        thread.SetRunning(); +        const auto resume_result = Resume(thread.GetID(), signo); +        if (resume_result.Success()) +            SetState(eStateRunning, true); +        return resume_result; +    } +    case eStateStepping: +    { +        thread.SetStepping(); +        const auto step_result = SingleStep(thread.GetID(), signo); +        if (step_result.Success()) +            SetState(eStateRunning, true); +        return step_result; +    } +    default: +        if (log) +            log->Printf("NativeProcessLinux::%s Unhandled state %s.", +                    __FUNCTION__, StateAsCString(state)); +        llvm_unreachable("Unhandled state for resume"); +    } +} + +//===----------------------------------------------------------------------===// + +void +NativeProcessLinux::StopRunningThreads(const lldb::tid_t triggering_tid) +{ +    Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); + +    if (log) +    { +        log->Printf("NativeProcessLinux::%s about to process event: (triggering_tid: %" PRIu64 ")", +                __FUNCTION__, triggering_tid); +    } + +    m_pending_notification_tid = triggering_tid; + +    // Request a stop for all the thread stops that need to be stopped +    // and are not already known to be stopped. +    for (const auto &thread_sp: m_threads) +    { +        if (StateIsRunningState(thread_sp->GetState())) +            static_pointer_cast<NativeThreadLinux>(thread_sp)->RequestStop(); +    } + +    SignalIfAllThreadsStopped(); + +    if (log) +    { +        log->Printf("NativeProcessLinux::%s event processing done", __FUNCTION__); +    } +} + +void +NativeProcessLinux::SignalIfAllThreadsStopped() +{ +    if (m_pending_notification_tid == LLDB_INVALID_THREAD_ID) +        return; // No pending notification. Nothing to do. + +    for (const auto &thread_sp: m_threads) +    { +        if (StateIsRunningState(thread_sp->GetState())) +            return; // Some threads are still running. Don't signal yet. +    } + +    // We have a pending notification and all threads have stopped. +    Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_BREAKPOINTS)); + +    // Clear any temporary breakpoints we used to implement software single stepping. +    for (const auto &thread_info: m_threads_stepping_with_breakpoint) +    { +        Error error = RemoveBreakpoint (thread_info.second); +        if (error.Fail()) +            if (log) +                log->Printf("NativeProcessLinux::%s() pid = %" PRIu64 " remove stepping breakpoint: %s", +                        __FUNCTION__, thread_info.first, error.AsCString()); +    } +    m_threads_stepping_with_breakpoint.clear(); + +    // Notify the delegate about the stop +    SetCurrentThreadID(m_pending_notification_tid); +    SetState(StateType::eStateStopped, true); +    m_pending_notification_tid = LLDB_INVALID_THREAD_ID; +} + +void +NativeProcessLinux::ThreadWasCreated(NativeThreadLinux &thread) +{ +    Log *const log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD); + +    if (log) +        log->Printf("NativeProcessLinux::%s (tid: %" PRIu64 ")", __FUNCTION__, thread.GetID()); + +    if (m_pending_notification_tid != LLDB_INVALID_THREAD_ID && StateIsRunningState(thread.GetState())) +    { +        // We will need to wait for this new thread to stop as well before firing the +        // notification. +        thread.RequestStop(); +    } +} + +void +NativeProcessLinux::SigchldHandler() +{ +    Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); +    // Process all pending waitpid notifications. +    while (true) +    { +        int status = -1; +        ::pid_t wait_pid = waitpid(-1, &status, __WALL | __WNOTHREAD | WNOHANG); + +        if (wait_pid == 0) +            break; // We are done. + +        if (wait_pid == -1) +        { +            if (errno == EINTR) +                continue; + +            Error error(errno, eErrorTypePOSIX); +            if (log) +                log->Printf("NativeProcessLinux::%s waitpid (-1, &status, __WALL | __WNOTHREAD | WNOHANG) failed: %s", +                        __FUNCTION__, error.AsCString()); +            break; +        } + +        bool exited = false; +        int signal = 0; +        int exit_status = 0; +        const char *status_cstr = nullptr; +        if (WIFSTOPPED(status)) +        { +            signal = WSTOPSIG(status); +            status_cstr = "STOPPED"; +        } +        else if (WIFEXITED(status)) +        { +            exit_status = WEXITSTATUS(status); +            status_cstr = "EXITED"; +            exited = true; +        } +        else if (WIFSIGNALED(status)) +        { +            signal = WTERMSIG(status); +            status_cstr = "SIGNALED"; +            if (wait_pid == static_cast< ::pid_t>(GetID())) { +                exited = true; +                exit_status = -1; +            } +        } +        else +            status_cstr = "(\?\?\?)"; + +        if (log) +            log->Printf("NativeProcessLinux::%s: waitpid (-1, &status, __WALL | __WNOTHREAD | WNOHANG)" +                "=> pid = %" PRIi32 ", status = 0x%8.8x (%s), signal = %i, exit_state = %i", +                __FUNCTION__, wait_pid, status, status_cstr, signal, exit_status); + +        MonitorCallback (wait_pid, exited, signal, exit_status); +    } +} + +// Wrapper for ptrace to catch errors and log calls. +// Note that ptrace sets errno on error because -1 can be a valid result (i.e. for PTRACE_PEEK*) +Error +NativeProcessLinux::PtraceWrapper(int req, lldb::pid_t pid, void *addr, void *data, size_t data_size, long *result) +{ +    Error error; +    long int ret; + +    Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_PTRACE)); + +    PtraceDisplayBytes(req, data, data_size); + +    errno = 0; +    if (req == PTRACE_GETREGSET || req == PTRACE_SETREGSET) +        ret = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), *(unsigned int *)addr, data); +    else +        ret = ptrace(static_cast<__ptrace_request>(req), static_cast< ::pid_t>(pid), addr, data); + +    if (ret == -1) +        error.SetErrorToErrno(); + +    if (result) +        *result = ret; + +    if (log) +        log->Printf("ptrace(%d, %" PRIu64 ", %p, %p, %zu)=%lX", req, pid, addr, data, data_size, ret); + +    PtraceDisplayBytes(req, data, data_size); + +    if (log && error.GetError() != 0) +    { +        const char* str; +        switch (error.GetError()) +        { +        case ESRCH:  str = "ESRCH"; break; +        case EINVAL: str = "EINVAL"; break; +        case EBUSY:  str = "EBUSY"; break; +        case EPERM:  str = "EPERM"; break; +        default:     str = error.AsCString(); +        } +        log->Printf("ptrace() failed; errno=%d (%s)", error.GetError(), str); +    } + +    return error; +} diff --git a/source/Plugins/Process/Linux/NativeProcessLinux.h b/source/Plugins/Process/Linux/NativeProcessLinux.h new file mode 100644 index 000000000000..7bac1dc8dbb7 --- /dev/null +++ b/source/Plugins/Process/Linux/NativeProcessLinux.h @@ -0,0 +1,329 @@ +//===-- NativeProcessLinux.h ---------------------------------- -*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_NativeProcessLinux_H_ +#define liblldb_NativeProcessLinux_H_ + +// C++ Includes +#include <unordered_set> + +// Other libraries and framework includes +#include "lldb/Core/ArchSpec.h" +#include "lldb/lldb-types.h" +#include "lldb/Host/Debug.h" +#include "lldb/Host/FileSpec.h" +#include "lldb/Host/HostThread.h" +#include "lldb/Host/Mutex.h" +#include "lldb/Target/MemoryRegionInfo.h" + +#include "lldb/Host/common/NativeProcessProtocol.h" +#include "NativeThreadLinux.h" + +namespace lldb_private { +    class Error; +    class Module; +    class Scalar; + +namespace process_linux { +    /// @class NativeProcessLinux +    /// @brief Manages communication with the inferior (debugee) process. +    /// +    /// Upon construction, this class prepares and launches an inferior process for +    /// debugging. +    /// +    /// Changes in the inferior process state are broadcasted. +    class NativeProcessLinux: public NativeProcessProtocol +    { +        friend Error +        NativeProcessProtocol::Launch (ProcessLaunchInfo &launch_info, +                NativeDelegate &native_delegate, +                MainLoop &mainloop, +                NativeProcessProtocolSP &process_sp); + +        friend Error +        NativeProcessProtocol::Attach (lldb::pid_t pid, +                NativeProcessProtocol::NativeDelegate &native_delegate, +                MainLoop &mainloop, +                NativeProcessProtocolSP &process_sp); + +    public: +        // --------------------------------------------------------------------- +        // NativeProcessProtocol Interface +        // --------------------------------------------------------------------- +        Error +        Resume (const ResumeActionList &resume_actions) override; + +        Error +        Halt () override; + +        Error +        Detach () override; + +        Error +        Signal (int signo) override; + +        Error +        Interrupt () override; + +        Error +        Kill () override; + +        Error +        GetMemoryRegionInfo (lldb::addr_t load_addr, MemoryRegionInfo &range_info) override; + +        Error +        ReadMemory(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override; + +        Error +        ReadMemoryWithoutTrap(lldb::addr_t addr, void *buf, size_t size, size_t &bytes_read) override; + +        Error +        WriteMemory(lldb::addr_t addr, const void *buf, size_t size, size_t &bytes_written) override; + +        Error +        AllocateMemory(size_t size, uint32_t permissions, lldb::addr_t &addr) override; + +        Error +        DeallocateMemory (lldb::addr_t addr) override; + +        lldb::addr_t +        GetSharedLibraryInfoAddress () override; + +        size_t +        UpdateThreads () override; + +        bool +        GetArchitecture (ArchSpec &arch) const override; + +        Error +        SetBreakpoint (lldb::addr_t addr, uint32_t size, bool hardware) override; + +        void +        DoStopIDBumped (uint32_t newBumpId) override; + +        Error +        GetLoadedModuleFileSpec(const char* module_path, FileSpec& file_spec) override; + +        Error +        GetFileLoadAddress(const llvm::StringRef& file_name, lldb::addr_t& load_addr) override; + +        NativeThreadLinuxSP +        GetThreadByID(lldb::tid_t id); + +        // --------------------------------------------------------------------- +        // Interface used by NativeRegisterContext-derived classes. +        // --------------------------------------------------------------------- +        static Error +        PtraceWrapper(int req, +                      lldb::pid_t pid, +                      void *addr = nullptr, +                      void *data = nullptr, +                      size_t data_size = 0, +                      long *result = nullptr); + +    protected: +        // --------------------------------------------------------------------- +        // NativeProcessProtocol protected interface +        // --------------------------------------------------------------------- +        Error +        GetSoftwareBreakpointTrapOpcode (size_t trap_opcode_size_hint, size_t &actual_opcode_size, const uint8_t *&trap_opcode_bytes) override; + +    private: + +        MainLoop::SignalHandleUP m_sigchld_handle; +        ArchSpec m_arch; + +        LazyBool m_supports_mem_region; +        std::vector<MemoryRegionInfo> m_mem_region_cache; +        Mutex m_mem_region_cache_mutex; + +        lldb::tid_t m_pending_notification_tid; + +        // List of thread ids stepping with a breakpoint with the address of +        // the relevan breakpoint +        std::map<lldb::tid_t, lldb::addr_t> m_threads_stepping_with_breakpoint; + +        /// @class LauchArgs +        /// +        /// @brief Simple structure to pass data to the thread responsible for +        /// launching a child process. +        struct LaunchArgs +        { +            LaunchArgs(Module *module, +                    char const **argv, +                    char const **envp, +                    const FileSpec &stdin_file_spec, +                    const FileSpec &stdout_file_spec, +                    const FileSpec &stderr_file_spec, +                    const FileSpec &working_dir, +                    const ProcessLaunchInfo &launch_info); + +            ~LaunchArgs(); + +            Module *m_module;                  // The executable image to launch. +            char const **m_argv;               // Process arguments. +            char const **m_envp;               // Process environment. +            const FileSpec m_stdin_file_spec;  // Redirect stdin if not empty. +            const FileSpec m_stdout_file_spec; // Redirect stdout if not empty. +            const FileSpec m_stderr_file_spec; // Redirect stderr if not empty. +            const FileSpec m_working_dir;      // Working directory or empty. +            const ProcessLaunchInfo &m_launch_info; +        }; + +        typedef std::function< ::pid_t(Error &)> InitialOperation; + +        // --------------------------------------------------------------------- +        // Private Instance Methods +        // --------------------------------------------------------------------- +        NativeProcessLinux (); + +        /// Launches an inferior process ready for debugging.  Forms the +        /// implementation of Process::DoLaunch. +        void +        LaunchInferior ( +            MainLoop &mainloop, +            Module *module, +            char const *argv[], +            char const *envp[], +            const FileSpec &stdin_file_spec, +            const FileSpec &stdout_file_spec, +            const FileSpec &stderr_file_spec, +            const FileSpec &working_dir, +            const ProcessLaunchInfo &launch_info, +            Error &error); + +        /// Attaches to an existing process.  Forms the +        /// implementation of Process::DoAttach +        void +        AttachToInferior (MainLoop &mainloop, lldb::pid_t pid, Error &error); + +        ::pid_t +        Launch(LaunchArgs *args, Error &error); + +        ::pid_t +        Attach(lldb::pid_t pid, Error &error); + +        static Error +        SetDefaultPtraceOpts(const lldb::pid_t); + +        static bool +        DupDescriptor(const FileSpec &file_spec, int fd, int flags); + +        static void * +        MonitorThread(void *baton); + +        void +        MonitorCallback(lldb::pid_t pid, bool exited, int signal, int status); + +        void +        WaitForNewThread(::pid_t tid); + +        void +        MonitorSIGTRAP(const siginfo_t &info, NativeThreadLinux &thread); + +        void +        MonitorTrace(NativeThreadLinux &thread); + +        void +        MonitorBreakpoint(NativeThreadLinux &thread); + +        void +        MonitorWatchpoint(NativeThreadLinux &thread, uint32_t wp_index); + +        void +        MonitorSignal(const siginfo_t &info, NativeThreadLinux &thread, bool exited); + +        bool +        SupportHardwareSingleStepping() const; + +        Error +        SetupSoftwareSingleStepping(NativeThreadLinux &thread); + +#if 0 +        static ::ProcessMessage::CrashReason +        GetCrashReasonForSIGSEGV(const siginfo_t *info); + +        static ::ProcessMessage::CrashReason +        GetCrashReasonForSIGILL(const siginfo_t *info); + +        static ::ProcessMessage::CrashReason +        GetCrashReasonForSIGFPE(const siginfo_t *info); + +        static ::ProcessMessage::CrashReason +        GetCrashReasonForSIGBUS(const siginfo_t *info); +#endif + +        bool +        HasThreadNoLock (lldb::tid_t thread_id); + +        bool +        StopTrackingThread (lldb::tid_t thread_id); + +        NativeThreadLinuxSP +        AddThread (lldb::tid_t thread_id); + +        Error +        GetSoftwareBreakpointPCOffset(uint32_t &actual_opcode_size); + +        Error +        FixupBreakpointPCAsNeeded(NativeThreadLinux &thread); + +        /// Writes a siginfo_t structure corresponding to the given thread ID to the +        /// memory region pointed to by @p siginfo. +        Error +        GetSignalInfo(lldb::tid_t tid, void *siginfo); + +        /// Writes the raw event message code (vis-a-vis PTRACE_GETEVENTMSG) +        /// corresponding to the given thread ID to the memory pointed to by @p +        /// message. +        Error +        GetEventMessage(lldb::tid_t tid, unsigned long *message); + +        /// Resumes the given thread.  If @p signo is anything but +        /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. +        Error +        Resume(lldb::tid_t tid, uint32_t signo); + +        /// Single steps the given thread.  If @p signo is anything but +        /// LLDB_INVALID_SIGNAL_NUMBER, deliver that signal to the thread. +        Error +        SingleStep(lldb::tid_t tid, uint32_t signo); + +        void +        NotifyThreadDeath (lldb::tid_t tid); + +        Error +        Detach(lldb::tid_t tid); + + +        // This method is requests a stop on all threads which are still running. It sets up a +        // deferred delegate notification, which will fire once threads report as stopped. The +        // triggerring_tid will be set as the current thread (main stop reason). +        void +        StopRunningThreads(lldb::tid_t triggering_tid); + +        // Notify the delegate if all threads have stopped. +        void SignalIfAllThreadsStopped(); + +        // Resume the given thread, optionally passing it the given signal. The type of resume +        // operation (continue, single-step) depends on the state parameter. +        Error +        ResumeThread(NativeThreadLinux &thread, lldb::StateType state, int signo); + +        void +        ThreadWasCreated(NativeThreadLinux &thread); + +        void +        SigchldHandler(); +    }; + +} // namespace process_linux +} // namespace lldb_private + +#endif // #ifndef liblldb_NativeProcessLinux_H_ diff --git a/source/Plugins/Process/Linux/NativeRegisterContextLinux.cpp b/source/Plugins/Process/Linux/NativeRegisterContextLinux.cpp new file mode 100644 index 000000000000..df0a008ff5f7 --- /dev/null +++ b/source/Plugins/Process/Linux/NativeRegisterContextLinux.cpp @@ -0,0 +1,230 @@ +//===-- NativeRegisterContextLinux.cpp --------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "NativeRegisterContextLinux.h" + +#include "lldb/Core/RegisterValue.h" +#include "lldb/Host/common/NativeProcessProtocol.h" +#include "lldb/Host/common/NativeThreadProtocol.h" +#include "lldb/Host/linux/Ptrace.h" + +#include "Plugins/Process/POSIX/ProcessPOSIXLog.h" + +using namespace lldb_private; +using namespace lldb_private::process_linux; + +NativeRegisterContextLinux::NativeRegisterContextLinux(NativeThreadProtocol &native_thread, +                                                       uint32_t concrete_frame_idx, +                                                       RegisterInfoInterface *reg_info_interface_p) : +    NativeRegisterContextRegisterInfo(native_thread, concrete_frame_idx, reg_info_interface_p) +{} + +lldb::ByteOrder +NativeRegisterContextLinux::GetByteOrder() const +{ +    // Get the target process whose privileged thread was used for the register read. +    lldb::ByteOrder byte_order = lldb::eByteOrderInvalid; + +    NativeProcessProtocolSP process_sp (m_thread.GetProcess ()); +    if (!process_sp) +        return byte_order; + +    if (!process_sp->GetByteOrder (byte_order)) +    { +        // FIXME log here +    } + +    return byte_order; +} + +Error +NativeRegisterContextLinux::ReadRegisterRaw(uint32_t reg_index, RegisterValue ®_value) +{ +    const RegisterInfo *const reg_info = GetRegisterInfoAtIndex(reg_index); +    if (!reg_info) +        return Error("register %" PRIu32 " not found", reg_index); + +    return DoReadRegisterValue(reg_info->byte_offset, reg_info->name, reg_info->byte_size, reg_value); +} + +Error +NativeRegisterContextLinux::WriteRegisterRaw(uint32_t reg_index, const RegisterValue ®_value) +{ +    uint32_t reg_to_write = reg_index; +    RegisterValue value_to_write = reg_value; + +    // Check if this is a subregister of a full register. +    const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg_index); +    if (reg_info->invalidate_regs && (reg_info->invalidate_regs[0] != LLDB_INVALID_REGNUM)) +    { +        Error error; + +        RegisterValue full_value; +        uint32_t full_reg = reg_info->invalidate_regs[0]; +        const RegisterInfo *full_reg_info = GetRegisterInfoAtIndex(full_reg); + +        // Read the full register. +        error = ReadRegister(full_reg_info, full_value); +        if (error.Fail ()) +            return error; + +        lldb::ByteOrder byte_order = GetByteOrder(); +        uint8_t dst[RegisterValue::kMaxRegisterByteSize]; + +        // Get the bytes for the full register. +        const uint32_t dest_size = full_value.GetAsMemoryData (full_reg_info, +                                                               dst, +                                                               sizeof(dst), +                                                               byte_order, +                                                               error); +        if (error.Success() && dest_size) +        { +            uint8_t src[RegisterValue::kMaxRegisterByteSize]; + +            // Get the bytes for the source data. +            const uint32_t src_size = reg_value.GetAsMemoryData (reg_info, src, sizeof(src), byte_order, error); +            if (error.Success() && src_size && (src_size < dest_size)) +            { +                // Copy the src bytes to the destination. +                memcpy (dst + (reg_info->byte_offset & 0x1), src, src_size); +                // Set this full register as the value to write. +                value_to_write.SetBytes(dst, full_value.GetByteSize(), byte_order); +                value_to_write.SetType(full_reg_info); +                reg_to_write = full_reg; +            } +        } +    } + +    const RegisterInfo *const register_to_write_info_p = GetRegisterInfoAtIndex (reg_to_write); +    assert (register_to_write_info_p && "register to write does not have valid RegisterInfo"); +    if (!register_to_write_info_p) +        return Error("NativeRegisterContextLinux::%s failed to get RegisterInfo for write register index %" PRIu32, __FUNCTION__, reg_to_write); + +    return DoWriteRegisterValue(reg_info->byte_offset, reg_info->name, reg_value); +} + +Error +NativeRegisterContextLinux::ReadGPR() +{ +    void* buf = GetGPRBuffer(); +    if (!buf) +        return Error("GPR buffer is NULL"); +    size_t buf_size = GetGPRSize(); + +    return DoReadGPR(buf, buf_size); +} + +Error +NativeRegisterContextLinux::WriteGPR() +{ +    void* buf = GetGPRBuffer(); +    if (!buf) +        return Error("GPR buffer is NULL"); +    size_t buf_size = GetGPRSize(); + +    return DoWriteGPR(buf, buf_size); +} + +Error +NativeRegisterContextLinux::ReadFPR() +{ +    void* buf = GetFPRBuffer(); +    if (!buf) +        return Error("FPR buffer is NULL"); +    size_t buf_size = GetFPRSize(); + +    return DoReadFPR(buf, buf_size); +} + +Error +NativeRegisterContextLinux::WriteFPR() +{ +    void* buf = GetFPRBuffer(); +    if (!buf) +        return Error("FPR buffer is NULL"); +    size_t buf_size = GetFPRSize(); + +    return DoWriteFPR(buf, buf_size); +} + +Error +NativeRegisterContextLinux::ReadRegisterSet(void *buf, size_t buf_size, unsigned int regset) +{ +    return NativeProcessLinux::PtraceWrapper(PTRACE_GETREGSET, m_thread.GetID(), +            static_cast<void *>(®set), buf, buf_size); +} + +Error +NativeRegisterContextLinux::WriteRegisterSet(void *buf, size_t buf_size, unsigned int regset) +{ +    return NativeProcessLinux::PtraceWrapper(PTRACE_SETREGSET, m_thread.GetID(), +            static_cast<void *>(®set), buf, buf_size); +} + +Error +NativeRegisterContextLinux::DoReadRegisterValue(uint32_t offset, +                                                const char* reg_name, +                                                uint32_t size, +                                                RegisterValue &value) +{ +    Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS)); + +    long data; +    Error error = NativeProcessLinux::PtraceWrapper( +            PTRACE_PEEKUSER, m_thread.GetID(), reinterpret_cast<void *>(offset), nullptr, 0, &data); + +    if (error.Success()) +        // First cast to an unsigned of the same size to avoid sign extension. +        value.SetUInt64(static_cast<unsigned long>(data)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux::%s() reg %s: 0x%lx", __FUNCTION__, reg_name, data); + +    return error; +} + +Error +NativeRegisterContextLinux::DoWriteRegisterValue(uint32_t offset, +                                                 const char* reg_name, +                                                 const RegisterValue &value) +{ +    Log *log (ProcessPOSIXLog::GetLogIfAllCategoriesSet (POSIX_LOG_REGISTERS)); + +    void* buf = reinterpret_cast<void *>(value.GetAsUInt64()); + +    if (log) +        log->Printf ("NativeRegisterContextLinux::%s() reg %s: %p", __FUNCTION__, reg_name, buf); + +    return NativeProcessLinux::PtraceWrapper( +            PTRACE_POKEUSER, m_thread.GetID(), reinterpret_cast<void *>(offset), buf); +} + +Error +NativeRegisterContextLinux::DoReadGPR(void *buf, size_t buf_size) +{ +    return NativeProcessLinux::PtraceWrapper(PTRACE_GETREGS, m_thread.GetID(), nullptr, buf, buf_size); +} + +Error +NativeRegisterContextLinux::DoWriteGPR(void *buf, size_t buf_size) +{ +    return NativeProcessLinux::PtraceWrapper(PTRACE_SETREGS, m_thread.GetID(), nullptr, buf, buf_size); +} + +Error +NativeRegisterContextLinux::DoReadFPR(void *buf, size_t buf_size) +{ +    return NativeProcessLinux::PtraceWrapper(PTRACE_GETFPREGS, m_thread.GetID(), nullptr, buf, buf_size); +} + +Error +NativeRegisterContextLinux::DoWriteFPR(void *buf, size_t buf_size) +{ +    return NativeProcessLinux::PtraceWrapper(PTRACE_SETFPREGS, m_thread.GetID(), nullptr, buf, buf_size); +} diff --git a/source/Plugins/Process/Linux/NativeRegisterContextLinux.h b/source/Plugins/Process/Linux/NativeRegisterContextLinux.h new file mode 100644 index 000000000000..0b0b747984b4 --- /dev/null +++ b/source/Plugins/Process/Linux/NativeRegisterContextLinux.h @@ -0,0 +1,107 @@ +//===-- NativeRegisterContextLinux.h ----------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef lldb_NativeRegisterContextLinux_h +#define lldb_NativeRegisterContextLinux_h + +#include "lldb/Host/common/NativeRegisterContextRegisterInfo.h" +#include "lldb/Host/common/NativeThreadProtocol.h" + +#include "Plugins/Process/Linux/NativeProcessLinux.h" + +namespace lldb_private { +namespace process_linux { + +class NativeRegisterContextLinux : public NativeRegisterContextRegisterInfo +{ +public: +    NativeRegisterContextLinux(NativeThreadProtocol &native_thread, +                               uint32_t concrete_frame_idx, +                               RegisterInfoInterface *reg_info_interface_p); + +    // This function is implemented in the NativeRegisterContextLinux_* subclasses to create a new +    // instance of the host specific NativeRegisterContextLinux. The implementations can't collide +    // as only one NativeRegisterContextLinux_* variant should be compiled into the final +    // executable. +    static NativeRegisterContextLinux* +    CreateHostNativeRegisterContextLinux(const ArchSpec& target_arch, +                                         NativeThreadProtocol &native_thread, +                                         uint32_t concrete_frame_idx); + +protected: +    lldb::ByteOrder +    GetByteOrder() const; + +    virtual Error +    ReadRegisterRaw(uint32_t reg_index, RegisterValue& reg_value); + +    virtual Error +    WriteRegisterRaw(uint32_t reg_index, const RegisterValue& reg_value); + +    virtual Error +    ReadRegisterSet(void *buf, size_t buf_size, unsigned int regset); + +    virtual Error +    WriteRegisterSet(void *buf, size_t buf_size, unsigned int regset); + +    virtual Error +    ReadGPR(); + +    virtual Error +    WriteGPR(); + +    virtual Error +    ReadFPR(); + +    virtual Error +    WriteFPR(); + +    virtual void* +    GetGPRBuffer() { return nullptr; } + +    virtual size_t +    GetGPRSize() { return GetRegisterInfoInterface().GetGPRSize(); } + +    virtual void* +    GetFPRBuffer() { return nullptr; } + +    virtual size_t +    GetFPRSize() { return 0; } + + +    // The Do*** functions are executed on the privileged thread and can perform ptrace +    // operations directly. +    virtual Error +    DoReadRegisterValue(uint32_t offset, +                        const char* reg_name, +                        uint32_t size, +                        RegisterValue &value); + +    virtual Error +    DoWriteRegisterValue(uint32_t offset, +                       const char* reg_name, +                       const RegisterValue &value); + +    virtual Error +    DoReadGPR(void *buf, size_t buf_size); + +    virtual Error +    DoWriteGPR(void *buf, size_t buf_size); + +    virtual Error +    DoReadFPR(void *buf, size_t buf_size); + +    virtual Error +    DoWriteFPR(void *buf, size_t buf_size); +}; + +} // namespace process_linux +} // namespace lldb_private + +#endif // #ifndef lldb_NativeRegisterContextLinux_h diff --git a/source/Plugins/Process/Linux/NativeRegisterContextLinux_arm.cpp b/source/Plugins/Process/Linux/NativeRegisterContextLinux_arm.cpp new file mode 100644 index 000000000000..31752fed5b42 --- /dev/null +++ b/source/Plugins/Process/Linux/NativeRegisterContextLinux_arm.cpp @@ -0,0 +1,1017 @@ +//===-- NativeRegisterContextLinux_arm.cpp --------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#if defined(__arm__) + +#include "NativeRegisterContextLinux_arm.h" + +#include "lldb/Core/DataBufferHeap.h" +#include "lldb/Core/Error.h" +#include "lldb/Core/Log.h" +#include "lldb/Core/RegisterValue.h" + +#include "Plugins/Process/Utility/RegisterContextLinux_arm.h" + +#define REG_CONTEXT_SIZE (GetGPRSize() + sizeof (m_fpr)) + +#ifndef PTRACE_GETVFPREGS +  #define PTRACE_GETVFPREGS 27 +  #define PTRACE_SETVFPREGS 28 +#endif +#ifndef PTRACE_GETHBPREGS +  #define PTRACE_GETHBPREGS 29 +  #define PTRACE_SETHBPREGS 30 +#endif +#if !defined(PTRACE_TYPE_ARG3) +  #define PTRACE_TYPE_ARG3 void * +#endif +#if !defined(PTRACE_TYPE_ARG4) +  #define PTRACE_TYPE_ARG4 void * +#endif + +using namespace lldb; +using namespace lldb_private; +using namespace lldb_private::process_linux; + +// arm general purpose registers. +static const uint32_t g_gpr_regnums_arm[] = +{ +    gpr_r0_arm, +    gpr_r1_arm, +    gpr_r2_arm, +    gpr_r3_arm, +    gpr_r4_arm, +    gpr_r5_arm, +    gpr_r6_arm, +    gpr_r7_arm, +    gpr_r8_arm, +    gpr_r9_arm, +    gpr_r10_arm, +    gpr_r11_arm, +    gpr_r12_arm, +    gpr_sp_arm, +    gpr_lr_arm, +    gpr_pc_arm, +    gpr_cpsr_arm, +    LLDB_INVALID_REGNUM // register sets need to end with this flag +}; +static_assert(((sizeof g_gpr_regnums_arm / sizeof g_gpr_regnums_arm[0]) - 1) == k_num_gpr_registers_arm, \ +              "g_gpr_regnums_arm has wrong number of register infos"); + +// arm floating point registers. +static const uint32_t g_fpu_regnums_arm[] = +{ +    fpu_s0_arm, +    fpu_s1_arm, +    fpu_s2_arm, +    fpu_s3_arm, +    fpu_s4_arm, +    fpu_s5_arm, +    fpu_s6_arm, +    fpu_s7_arm, +    fpu_s8_arm, +    fpu_s9_arm, +    fpu_s10_arm, +    fpu_s11_arm, +    fpu_s12_arm, +    fpu_s13_arm, +    fpu_s14_arm, +    fpu_s15_arm, +    fpu_s16_arm, +    fpu_s17_arm, +    fpu_s18_arm, +    fpu_s19_arm, +    fpu_s20_arm, +    fpu_s21_arm, +    fpu_s22_arm, +    fpu_s23_arm, +    fpu_s24_arm, +    fpu_s25_arm, +    fpu_s26_arm, +    fpu_s27_arm, +    fpu_s28_arm, +    fpu_s29_arm, +    fpu_s30_arm, +    fpu_s31_arm, +    fpu_fpscr_arm, +    fpu_d0_arm, +    fpu_d1_arm, +    fpu_d2_arm, +    fpu_d3_arm, +    fpu_d4_arm, +    fpu_d5_arm, +    fpu_d6_arm, +    fpu_d7_arm, +    fpu_d8_arm, +    fpu_d9_arm, +    fpu_d10_arm, +    fpu_d11_arm, +    fpu_d12_arm, +    fpu_d13_arm, +    fpu_d14_arm, +    fpu_d15_arm, +    fpu_d16_arm, +    fpu_d17_arm, +    fpu_d18_arm, +    fpu_d19_arm, +    fpu_d20_arm, +    fpu_d21_arm, +    fpu_d22_arm, +    fpu_d23_arm, +    fpu_d24_arm, +    fpu_d25_arm, +    fpu_d26_arm, +    fpu_d27_arm, +    fpu_d28_arm, +    fpu_d29_arm, +    fpu_d30_arm, +    fpu_d31_arm, +    fpu_q0_arm, +    fpu_q1_arm, +    fpu_q2_arm, +    fpu_q3_arm, +    fpu_q4_arm, +    fpu_q5_arm, +    fpu_q6_arm, +    fpu_q7_arm, +    fpu_q8_arm, +    fpu_q9_arm, +    fpu_q10_arm, +    fpu_q11_arm, +    fpu_q12_arm, +    fpu_q13_arm, +    fpu_q14_arm, +    fpu_q15_arm, +    LLDB_INVALID_REGNUM // register sets need to end with this flag +}; +static_assert(((sizeof g_fpu_regnums_arm / sizeof g_fpu_regnums_arm[0]) - 1) == k_num_fpr_registers_arm, \ +              "g_fpu_regnums_arm has wrong number of register infos"); + +namespace { +    // Number of register sets provided by this context. +    enum +    { +        k_num_register_sets = 2 +    }; +} + +// Register sets for arm. +static const RegisterSet +g_reg_sets_arm[k_num_register_sets] = +{ +    { "General Purpose Registers",  "gpr", k_num_gpr_registers_arm, g_gpr_regnums_arm }, +    { "Floating Point Registers",   "fpu", k_num_fpr_registers_arm, g_fpu_regnums_arm } +}; + +NativeRegisterContextLinux* +NativeRegisterContextLinux::CreateHostNativeRegisterContextLinux(const ArchSpec& target_arch, +                                                                 NativeThreadProtocol &native_thread, +                                                                 uint32_t concrete_frame_idx) +{ +    return new NativeRegisterContextLinux_arm(target_arch, native_thread, concrete_frame_idx); +} + +NativeRegisterContextLinux_arm::NativeRegisterContextLinux_arm (const ArchSpec& target_arch, +                                                                NativeThreadProtocol &native_thread, +                                                                uint32_t concrete_frame_idx) : +    NativeRegisterContextLinux (native_thread, concrete_frame_idx, new RegisterContextLinux_arm(target_arch)) +{ +    switch (target_arch.GetMachine()) +    { +        case llvm::Triple::arm: +            m_reg_info.num_registers     = k_num_registers_arm; +            m_reg_info.num_gpr_registers = k_num_gpr_registers_arm; +            m_reg_info.num_fpr_registers = k_num_fpr_registers_arm; +            m_reg_info.last_gpr          = k_last_gpr_arm; +            m_reg_info.first_fpr         = k_first_fpr_arm; +            m_reg_info.last_fpr          = k_last_fpr_arm; +            m_reg_info.first_fpr_v       = fpu_s0_arm; +            m_reg_info.last_fpr_v        = fpu_s31_arm; +            m_reg_info.gpr_flags         = gpr_cpsr_arm; +            break; +        default: +            assert(false && "Unhandled target architecture."); +            break; +    } + +    ::memset(&m_fpr, 0, sizeof (m_fpr)); +    ::memset(&m_gpr_arm, 0, sizeof (m_gpr_arm)); +    ::memset(&m_hwp_regs, 0, sizeof (m_hwp_regs)); + +    // 16 is just a maximum value, query hardware for actual watchpoint count +    m_max_hwp_supported = 16; +    m_max_hbp_supported = 16; +    m_refresh_hwdebug_info = true; +} + +uint32_t +NativeRegisterContextLinux_arm::GetRegisterSetCount () const +{ +    return k_num_register_sets; +} + +uint32_t +NativeRegisterContextLinux_arm::GetUserRegisterCount() const +{ +    uint32_t count = 0; +    for (uint32_t set_index = 0; set_index < k_num_register_sets; ++set_index) +        count += g_reg_sets_arm[set_index].num_registers; +    return count; +} + +const RegisterSet * +NativeRegisterContextLinux_arm::GetRegisterSet (uint32_t set_index) const +{ +    if (set_index < k_num_register_sets) +        return &g_reg_sets_arm[set_index]; + +    return nullptr; +} + +Error +NativeRegisterContextLinux_arm::ReadRegister (const RegisterInfo *reg_info, RegisterValue ®_value) +{ +    Error error; + +    if (!reg_info) +    { +        error.SetErrorString ("reg_info NULL"); +        return error; +    } + +    const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB]; + +    if (IsFPR(reg)) +    { +        error = ReadFPR(); +        if (error.Fail()) +            return error; +    } +    else +    { +        uint32_t full_reg = reg; +        bool is_subreg = reg_info->invalidate_regs && (reg_info->invalidate_regs[0] != LLDB_INVALID_REGNUM); + +        if (is_subreg) +        { +            // Read the full aligned 64-bit register. +            full_reg = reg_info->invalidate_regs[0]; +        } + +        error = ReadRegisterRaw(full_reg, reg_value); + +        if (error.Success ()) +        { +            // If our read was not aligned (for ah,bh,ch,dh), shift our returned value one byte to the right. +            if (is_subreg && (reg_info->byte_offset & 0x1)) +                reg_value.SetUInt64(reg_value.GetAsUInt64() >> 8); + +            // If our return byte size was greater than the return value reg size, then +            // use the type specified by reg_info rather than the uint64_t default +            if (reg_value.GetByteSize() > reg_info->byte_size) +                reg_value.SetType(reg_info); +        } +        return error; +    } + +    // Get pointer to m_fpr variable and set the data from it. +    uint32_t fpr_offset = CalculateFprOffset(reg_info); +    assert (fpr_offset < sizeof m_fpr); +    uint8_t *src = (uint8_t *)&m_fpr + fpr_offset; +    switch (reg_info->byte_size) +    { +        case 2: +            reg_value.SetUInt16(*(uint16_t *)src); +            break; +        case 4: +            reg_value.SetUInt32(*(uint32_t *)src); +            break; +        case 8: +            reg_value.SetUInt64(*(uint64_t *)src); +            break; +        case 16: +            reg_value.SetBytes(src, 16, GetByteOrder()); +            break; +        default: +            assert(false && "Unhandled data size."); +            error.SetErrorStringWithFormat ("unhandled byte size: %" PRIu32, reg_info->byte_size); +            break; +    } + +    return error; +} + +Error +NativeRegisterContextLinux_arm::WriteRegister (const RegisterInfo *reg_info, const RegisterValue ®_value) +{ +    if (!reg_info) +        return Error ("reg_info NULL"); + +    const uint32_t reg_index = reg_info->kinds[lldb::eRegisterKindLLDB]; +    if (reg_index == LLDB_INVALID_REGNUM) +        return Error ("no lldb regnum for %s", reg_info && reg_info->name ? reg_info->name : "<unknown register>"); + +    if (IsGPR(reg_index)) +        return WriteRegisterRaw(reg_index, reg_value); + +    if (IsFPR(reg_index)) +    { +        // Get pointer to m_fpr variable and set the data to it. +        uint32_t fpr_offset = CalculateFprOffset(reg_info); +        assert (fpr_offset < sizeof m_fpr); +        uint8_t *dst = (uint8_t *)&m_fpr + fpr_offset; +        switch (reg_info->byte_size) +        { +            case 2: +                *(uint16_t *)dst = reg_value.GetAsUInt16(); +                break; +            case 4: +                *(uint32_t *)dst = reg_value.GetAsUInt32(); +                break; +            case 8: +                *(uint64_t *)dst = reg_value.GetAsUInt64(); +                break; +            default: +                assert(false && "Unhandled data size."); +                return Error ("unhandled register data size %" PRIu32, reg_info->byte_size); +        } + +        Error error = WriteFPR(); +        if (error.Fail()) +            return error; + +        return Error (); +    } + +    return Error ("failed - register wasn't recognized to be a GPR or an FPR, write strategy unknown"); +} + +Error +NativeRegisterContextLinux_arm::ReadAllRegisterValues (lldb::DataBufferSP &data_sp) +{ +    Error error; + +    data_sp.reset (new DataBufferHeap (REG_CONTEXT_SIZE, 0)); +    if (!data_sp) +        return Error ("failed to allocate DataBufferHeap instance of size %" PRIu64, (uint64_t)REG_CONTEXT_SIZE); + +    error = ReadGPR(); +    if (error.Fail()) +        return error; + +    error = ReadFPR(); +    if (error.Fail()) +        return error; + +    uint8_t *dst = data_sp->GetBytes (); +    if (dst == nullptr) +    { +        error.SetErrorStringWithFormat ("DataBufferHeap instance of size %" PRIu64 " returned a null pointer", (uint64_t)REG_CONTEXT_SIZE); +        return error; +    } + +    ::memcpy (dst, &m_gpr_arm, GetGPRSize()); +    dst += GetGPRSize(); +    ::memcpy (dst, &m_fpr, sizeof(m_fpr)); + +    return error; +} + +Error +NativeRegisterContextLinux_arm::WriteAllRegisterValues (const lldb::DataBufferSP &data_sp) +{ +    Error error; + +    if (!data_sp) +    { +        error.SetErrorStringWithFormat ("NativeRegisterContextLinux_x86_64::%s invalid data_sp provided", __FUNCTION__); +        return error; +    } + +    if (data_sp->GetByteSize () != REG_CONTEXT_SIZE) +    { +        error.SetErrorStringWithFormat ("NativeRegisterContextLinux_x86_64::%s data_sp contained mismatched data size, expected %" PRIu64 ", actual %" PRIu64, __FUNCTION__, (uint64_t)REG_CONTEXT_SIZE, data_sp->GetByteSize ()); +        return error; +    } + + +    uint8_t *src = data_sp->GetBytes (); +    if (src == nullptr) +    { +        error.SetErrorStringWithFormat ("NativeRegisterContextLinux_x86_64::%s DataBuffer::GetBytes() returned a null pointer", __FUNCTION__); +        return error; +    } +    ::memcpy (&m_gpr_arm, src, GetRegisterInfoInterface ().GetGPRSize ()); + +    error = WriteGPR(); +    if (error.Fail()) +        return error; + +    src += GetRegisterInfoInterface ().GetGPRSize (); +    ::memcpy (&m_fpr, src, sizeof(m_fpr)); + +    error = WriteFPR(); +    if (error.Fail()) +        return error; + +    return error; +} + +bool +NativeRegisterContextLinux_arm::IsGPR(unsigned reg) const +{ +    return reg <= m_reg_info.last_gpr;   // GPR's come first. +} + +bool +NativeRegisterContextLinux_arm::IsFPR(unsigned reg) const +{ +    return (m_reg_info.first_fpr <= reg && reg <= m_reg_info.last_fpr); +} + +uint32_t +NativeRegisterContextLinux_arm::SetHardwareBreakpoint (lldb::addr_t addr, size_t size) +{ +    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); + +    Error error; + +    // Read hardware breakpoint and watchpoint information. +    error = ReadHardwareDebugInfo (); + +    if (error.Fail()) +        return LLDB_INVALID_INDEX32; + +    uint32_t control_value = 0, bp_index = 0; + +    // Check if size has a valid hardware breakpoint length. +    // Thumb instructions are 2-bytes but we have no way here to determine +    // if target address is a thumb or arm instruction. +    // TODO: Add support for setting thumb mode hardware breakpoints +    if (size != 4 && size != 2) +        return LLDB_INVALID_INDEX32; + +    // Setup control value +    // Make the byte_mask into a valid Byte Address Select mask +    control_value = 0xfu << 5; + +    // Enable this breakpoint and make it stop in privileged or user mode; +    control_value |= 7; + +    // Make sure bits 1:0 are clear in our address +    // This should be different once we support thumb here. +    addr &= ~((lldb::addr_t)3); + +    // Iterate over stored hardware breakpoints +    // Find a free bp_index or update reference count if duplicate. +    bp_index = LLDB_INVALID_INDEX32; + +    for (uint32_t i = 0; i < m_max_hbp_supported; i++) +    { +        if ((m_hbr_regs[i].control & 1) == 0) +        { +            bp_index = i;  // Mark last free slot +        } +        else if (m_hbr_regs[i].address == addr && m_hbr_regs[i].control == control_value) +        { +            bp_index = i;  // Mark duplicate index +            break;  // Stop searching here +        } +    } + +     if (bp_index == LLDB_INVALID_INDEX32) +         return LLDB_INVALID_INDEX32; + +    // Add new or update existing breakpoint +    if ((m_hbr_regs[bp_index].control & 1) == 0) +    { +        m_hbr_regs[bp_index].address = addr; +        m_hbr_regs[bp_index].control = control_value; +        m_hbr_regs[bp_index].refcount = 1; + +        // PTRACE call to set corresponding hardware breakpoint register. +        error = WriteHardwareDebugRegs(eDREGTypeBREAK, bp_index); + +        if (error.Fail()) +        { +            m_hbr_regs[bp_index].address = 0; +            m_hbr_regs[bp_index].control &= ~1; +            m_hbr_regs[bp_index].refcount = 0; + +            return LLDB_INVALID_INDEX32; +        } +    } +    else +        m_hbr_regs[bp_index].refcount++; + +    return bp_index; +} + +bool +NativeRegisterContextLinux_arm::ClearHardwareBreakpoint (uint32_t hw_idx) +{ +    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); + +    Error error; + +    // Read hardware breakpoint and watchpoint information. +    error = ReadHardwareDebugInfo (); + +    if (error.Fail()) +        return false; + +    if (hw_idx >= m_max_hbp_supported) +        return false; + +    // Update reference count if multiple references. +    if (m_hbr_regs[hw_idx].refcount > 1) +    { +        m_hbr_regs[hw_idx].refcount--; +        return true; +    } +    else if (m_hbr_regs[hw_idx].refcount == 1) +    { +        // Create a backup we can revert to in case of failure. +        lldb::addr_t tempAddr = m_hbr_regs[hw_idx].address; +        uint32_t tempControl = m_hbr_regs[hw_idx].control; +        uint32_t tempRefCount = m_hbr_regs[hw_idx].refcount; + +        m_hbr_regs[hw_idx].control &= ~1; +        m_hbr_regs[hw_idx].address = 0; +        m_hbr_regs[hw_idx].refcount = 0; + +        // PTRACE call to clear corresponding hardware breakpoint register. +        WriteHardwareDebugRegs(eDREGTypeBREAK, hw_idx); + +        if (error.Fail()) +        { +            m_hbr_regs[hw_idx].control = tempControl; +            m_hbr_regs[hw_idx].address = tempAddr; +            m_hbr_regs[hw_idx].refcount = tempRefCount; + +            return false; +        } + +        return true; +    } + +    return false; +} + +uint32_t +NativeRegisterContextLinux_arm::NumSupportedHardwareWatchpoints () +{ +    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); + +    Error error; + +    // Read hardware breakpoint and watchpoint information. +    error = ReadHardwareDebugInfo (); + +    if (error.Fail()) +        return LLDB_INVALID_INDEX32; + +    return m_max_hwp_supported; +} + +uint32_t +NativeRegisterContextLinux_arm::SetHardwareWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags) +{ +    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); +     +    Error error; + +    // Read hardware breakpoint and watchpoint information. +    error = ReadHardwareDebugInfo (); + +    if (error.Fail()) +        return LLDB_INVALID_INDEX32; +		 +    uint32_t control_value = 0, wp_index = 0, addr_word_offset = 0, byte_mask = 0; + +    // Check if we are setting watchpoint other than read/write/access +    // Also update watchpoint flag to match Arm write-read bit configuration. +    switch (watch_flags) +    { +        case 1: +            watch_flags = 2; +            break; +        case 2: +            watch_flags = 1; +            break; +        case 3: +            break; +        default: +            return LLDB_INVALID_INDEX32; +    } + +    // Can't watch zero bytes +    // Can't watch more than 4 bytes per WVR/WCR pair + +    if (size == 0 || size > 4) +        return LLDB_INVALID_INDEX32; + +    // We can only watch up to four bytes that follow a 4 byte aligned address +    // per watchpoint register pair, so make sure we can properly encode this. +    addr_word_offset = addr % 4; +    byte_mask = ((1u << size) - 1u) << addr_word_offset; + +    // Check if we need multiple watchpoint register +    if (byte_mask > 0xfu) +        return LLDB_INVALID_INDEX32; + +    // Setup control value +    // Make the byte_mask into a valid Byte Address Select mask +    control_value = byte_mask << 5; + +    //Turn on appropriate watchpoint flags read or write +    control_value |= (watch_flags << 3); + +    // Enable this watchpoint and make it stop in privileged or user mode; +    control_value |= 7; + +    // Make sure bits 1:0 are clear in our address +    addr &= ~((lldb::addr_t)3); + +    // Iterate over stored watchpoints +    // Find a free wp_index or update reference count if duplicate. +    wp_index = LLDB_INVALID_INDEX32; +    for (uint32_t i = 0; i < m_max_hwp_supported; i++) +    { +        if ((m_hwp_regs[i].control & 1) == 0) +        { +            wp_index = i; // Mark last free slot +        } +        else if (m_hwp_regs[i].address == addr && m_hwp_regs[i].control == control_value) +        { +            wp_index = i; // Mark duplicate index +            break; // Stop searching here +        } +    } + +     if (wp_index == LLDB_INVALID_INDEX32) +        return LLDB_INVALID_INDEX32; + +    // Add new or update existing watchpoint +    if ((m_hwp_regs[wp_index].control & 1) == 0) +    { +        // Update watchpoint in local cache +        m_hwp_regs[wp_index].address = addr; +        m_hwp_regs[wp_index].control = control_value; +        m_hwp_regs[wp_index].refcount = 1; + +        // PTRACE call to set corresponding watchpoint register. +        error = WriteHardwareDebugRegs(eDREGTypeWATCH, wp_index); + +        if (error.Fail()) +        { +            m_hwp_regs[wp_index].address = 0; +            m_hwp_regs[wp_index].control &= ~1; +            m_hwp_regs[wp_index].refcount = 0; + +            return LLDB_INVALID_INDEX32; +        } +    } +    else +        m_hwp_regs[wp_index].refcount++; + +    return wp_index; +} + +bool +NativeRegisterContextLinux_arm::ClearHardwareWatchpoint (uint32_t wp_index) +{ +    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); + +    Error error; + +    // Read hardware breakpoint and watchpoint information. +    error = ReadHardwareDebugInfo (); + +    if (error.Fail()) +        return false; + +    if (wp_index >= m_max_hwp_supported) +        return false; + +    // Update reference count if multiple references. +    if (m_hwp_regs[wp_index].refcount > 1) +    { +        m_hwp_regs[wp_index].refcount--; +        return true; +    } +    else if (m_hwp_regs[wp_index].refcount == 1) +    { +        // Create a backup we can revert to in case of failure. +        lldb::addr_t tempAddr = m_hwp_regs[wp_index].address; +        uint32_t tempControl = m_hwp_regs[wp_index].control; +        uint32_t tempRefCount = m_hwp_regs[wp_index].refcount; + +        // Update watchpoint in local cache +        m_hwp_regs[wp_index].control &= ~1; +        m_hwp_regs[wp_index].address = 0; +        m_hwp_regs[wp_index].refcount = 0; + +        // Ptrace call to update hardware debug registers +        error = WriteHardwareDebugRegs(eDREGTypeWATCH, wp_index); + +        if (error.Fail()) +        { +            m_hwp_regs[wp_index].control = tempControl; +            m_hwp_regs[wp_index].address = tempAddr; +            m_hwp_regs[wp_index].refcount = tempRefCount; + +            return false; +        } + +        return true; +    } + +    return false; +} + +Error +NativeRegisterContextLinux_arm::ClearAllHardwareWatchpoints () +{ +    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); + +    Error error; + +    // Read hardware breakpoint and watchpoint information. +    error = ReadHardwareDebugInfo (); + +    if (error.Fail()) +        return error; + +    lldb::addr_t tempAddr = 0; +    uint32_t tempControl = 0, tempRefCount = 0; + +    for (uint32_t i = 0; i < m_max_hwp_supported; i++) +    { +        if (m_hwp_regs[i].control & 0x01) +        { +            // Create a backup we can revert to in case of failure. +            tempAddr = m_hwp_regs[i].address; +            tempControl = m_hwp_regs[i].control; +            tempRefCount = m_hwp_regs[i].refcount; + +            // Clear watchpoints in local cache +            m_hwp_regs[i].control &= ~1; +            m_hwp_regs[i].address = 0; +            m_hwp_regs[i].refcount = 0; + +            // Ptrace call to update hardware debug registers +            error = WriteHardwareDebugRegs(eDREGTypeWATCH, i); + +            if (error.Fail()) +            { +                m_hwp_regs[i].control = tempControl; +                m_hwp_regs[i].address = tempAddr; +                m_hwp_regs[i].refcount = tempRefCount; + +                return error; +            } +        } +    } + +    return Error(); +} + +uint32_t +NativeRegisterContextLinux_arm::GetWatchpointSize(uint32_t wp_index) +{ +    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); + +    switch ((m_hwp_regs[wp_index].control >> 5) & 0x0f) +    { +        case 0x01: +            return 1; +        case 0x03: +            return 2; +        case 0x07: +            return 3; +        case 0x0f: +            return 4; +        default: +            return 0; +    } +} +bool +NativeRegisterContextLinux_arm::WatchpointIsEnabled(uint32_t wp_index) +{ +    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); + +    if ((m_hwp_regs[wp_index].control & 0x1) == 0x1) +        return true; +    else +        return false; +} + +Error +NativeRegisterContextLinux_arm::GetWatchpointHitIndex(uint32_t &wp_index, lldb::addr_t trap_addr) +{ +    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); + +    uint32_t watch_size; +    lldb::addr_t watch_addr; + +    for (wp_index = 0; wp_index < m_max_hwp_supported; ++wp_index) +    { +        watch_size = GetWatchpointSize (wp_index); +        watch_addr = m_hwp_regs[wp_index].address; + +        if (m_hwp_regs[wp_index].refcount >= 1 && WatchpointIsEnabled(wp_index) +            && trap_addr >= watch_addr && trap_addr < watch_addr + watch_size) +        { +            return Error(); +        } +    } + +    wp_index = LLDB_INVALID_INDEX32; +    return Error(); +} + +lldb::addr_t +NativeRegisterContextLinux_arm::GetWatchpointAddress (uint32_t wp_index) +{ +    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux_arm::%s()", __FUNCTION__); + +    if (wp_index >= m_max_hwp_supported) +        return LLDB_INVALID_ADDRESS; + +    if (WatchpointIsEnabled(wp_index)) +        return m_hwp_regs[wp_index].address; +    else +        return LLDB_INVALID_ADDRESS; +} + +Error +NativeRegisterContextLinux_arm::ReadHardwareDebugInfo() +{ +    Error error; + +    if (!m_refresh_hwdebug_info) +    { +        return Error(); +    } + +    unsigned int cap_val; + +    error = NativeProcessLinux::PtraceWrapper(PTRACE_GETHBPREGS, m_thread.GetID(), nullptr, &cap_val, sizeof(unsigned int)); + +    if (error.Fail()) +        return error; + +    m_max_hwp_supported = (cap_val >> 8) & 0xff; +    m_max_hbp_supported = cap_val & 0xff; +    m_refresh_hwdebug_info = false; + +    return error; +} + +Error +NativeRegisterContextLinux_arm::WriteHardwareDebugRegs(int hwbType, int hwb_index) +{ +    Error error; + +    lldb::addr_t *addr_buf; +    uint32_t *ctrl_buf; + +    if (hwbType == eDREGTypeWATCH) +    { +        addr_buf = &m_hwp_regs[hwb_index].address; +        ctrl_buf = &m_hwp_regs[hwb_index].control; + +        error = NativeProcessLinux::PtraceWrapper(PTRACE_SETHBPREGS, +                m_thread.GetID(), (PTRACE_TYPE_ARG3) -((hwb_index << 1) + 1), +                addr_buf, sizeof(unsigned int)); + +        if (error.Fail()) +            return error; + +        error = NativeProcessLinux::PtraceWrapper(PTRACE_SETHBPREGS, +                m_thread.GetID(), (PTRACE_TYPE_ARG3) -((hwb_index << 1) + 2), +                ctrl_buf, sizeof(unsigned int)); +    } +    else +    { +        addr_buf = &m_hwp_regs[hwb_index].address; +        ctrl_buf = &m_hwp_regs[hwb_index].control; + +        error = NativeProcessLinux::PtraceWrapper(PTRACE_SETHBPREGS, +                m_thread.GetID(), (PTRACE_TYPE_ARG3) ((hwb_index << 1) + 1), +                addr_buf, sizeof(unsigned int)); + +        if (error.Fail()) +            return error; + +        error = NativeProcessLinux::PtraceWrapper(PTRACE_SETHBPREGS, +                m_thread.GetID(), (PTRACE_TYPE_ARG3) ((hwb_index << 1) + 2), +                ctrl_buf, sizeof(unsigned int)); + +    } + +    return error; +} + +uint32_t +NativeRegisterContextLinux_arm::CalculateFprOffset(const RegisterInfo* reg_info) const +{ +    return reg_info->byte_offset - GetRegisterInfoAtIndex(m_reg_info.first_fpr)->byte_offset; +} + +Error +NativeRegisterContextLinux_arm::DoWriteRegisterValue(uint32_t offset, +                                                     const char* reg_name, +                                                     const RegisterValue &value) +{ +    // PTRACE_POKEUSER don't work in the aarch64 liux kernel used on android devices (always return +    // "Bad address"). To avoid using PTRACE_POKEUSER we read out the full GPR register set, modify +    // the requested register and write it back. This approach is about 4 times slower but the +    // performance overhead is negligible in comparision to processing time in lldb-server. +    assert(offset % 4 == 0 && "Try to write a register with unaligned offset"); +    if (offset + sizeof(uint32_t) > sizeof(m_gpr_arm)) +        return Error("Register isn't fit into the size of the GPR area"); + +    Error error = DoReadGPR(m_gpr_arm, sizeof(m_gpr_arm)); +    if (error.Fail()) +        return error; + +    uint32_t reg_value = value.GetAsUInt32(); +    // As precaution for an undefined behavior encountered while setting PC we +    // will clear thumb bit of new PC if we are already in thumb mode; that is +    // CPSR thumb mode bit is set. +    if (offset / sizeof(uint32_t) == gpr_pc_arm) +    { +        // Check if we are already in thumb mode and +        // thumb bit of current PC is read out to be zero and +        // thumb bit of next PC is read out to be one. +        if ((m_gpr_arm[gpr_cpsr_arm] &  0x20) && +            !(m_gpr_arm[gpr_pc_arm] &  0x01) && +            (value.GetAsUInt32() & 0x01)) +        { +            reg_value &= (~1ull); +        } +    } + +    m_gpr_arm[offset / sizeof(uint32_t)] = reg_value; +    return DoWriteGPR(m_gpr_arm, sizeof(m_gpr_arm)); +} + +Error +NativeRegisterContextLinux_arm::DoReadFPR(void *buf, size_t buf_size) +{ +    return NativeProcessLinux::PtraceWrapper(PTRACE_GETVFPREGS, +                                             m_thread.GetID(), +                                             nullptr, +                                             buf, +                                             buf_size); +} + +Error +NativeRegisterContextLinux_arm::DoWriteFPR(void *buf, size_t buf_size) +{ +    return NativeProcessLinux::PtraceWrapper(PTRACE_SETVFPREGS, +                                             m_thread.GetID(), +                                             nullptr, +                                             buf, +                                             buf_size); +} + +#endif // defined(__arm__) diff --git a/source/Plugins/Process/Linux/NativeRegisterContextLinux_arm.h b/source/Plugins/Process/Linux/NativeRegisterContextLinux_arm.h new file mode 100644 index 000000000000..611b36ad4db1 --- /dev/null +++ b/source/Plugins/Process/Linux/NativeRegisterContextLinux_arm.h @@ -0,0 +1,185 @@ +//===-- NativeRegisterContextLinux_arm.h ---------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#if defined(__arm__) // arm register context only needed on arm devices + +#ifndef lldb_NativeRegisterContextLinux_arm_h +#define lldb_NativeRegisterContextLinux_arm_h + +#include "Plugins/Process/Linux/NativeRegisterContextLinux.h" +#include "Plugins/Process/Utility/lldb-arm-register-enums.h" + +namespace lldb_private { +namespace process_linux { + +    class NativeProcessLinux; + +    class NativeRegisterContextLinux_arm : public NativeRegisterContextLinux +    { +    public: +        NativeRegisterContextLinux_arm (const ArchSpec& target_arch, +                                        NativeThreadProtocol &native_thread, +                                        uint32_t concrete_frame_idx); + +        uint32_t +        GetRegisterSetCount () const override; + +        const RegisterSet * +        GetRegisterSet (uint32_t set_index) const override; + +        uint32_t +        GetUserRegisterCount() const override; + +        Error +        ReadRegister (const RegisterInfo *reg_info, RegisterValue ®_value) override; + +        Error +        WriteRegister (const RegisterInfo *reg_info, const RegisterValue ®_value) override; + +        Error +        ReadAllRegisterValues (lldb::DataBufferSP &data_sp) override; + +        Error +        WriteAllRegisterValues (const lldb::DataBufferSP &data_sp) override; + +        //------------------------------------------------------------------ +        // Hardware breakpoints/watchpoint mangement functions +        //------------------------------------------------------------------ + +        uint32_t +        SetHardwareBreakpoint (lldb::addr_t addr, size_t size) override; + +        bool +        ClearHardwareBreakpoint (uint32_t hw_idx) override; + +        uint32_t +        NumSupportedHardwareWatchpoints () override; + +        uint32_t +        SetHardwareWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags) override; + +        bool +        ClearHardwareWatchpoint (uint32_t hw_index) override; + +        Error +        ClearAllHardwareWatchpoints () override; + +        Error +        GetWatchpointHitIndex(uint32_t &wp_index, lldb::addr_t trap_addr) override; + +        lldb::addr_t +        GetWatchpointAddress (uint32_t wp_index) override; + +        uint32_t +        GetWatchpointSize(uint32_t wp_index); + +        bool +        WatchpointIsEnabled(uint32_t wp_index); + +        // Debug register type select +        enum DREGType +        { +            eDREGTypeWATCH = 0, +            eDREGTypeBREAK +        }; + +    protected: +        Error +        DoWriteRegisterValue(uint32_t offset, +                             const char* reg_name, +                             const RegisterValue &value) override; + +        Error +        DoReadFPR(void *buf, size_t buf_size) override; + +        Error +        DoWriteFPR(void *buf, size_t buf_size) override; + +        void* +        GetGPRBuffer() override { return &m_gpr_arm; } + +        void* +        GetFPRBuffer() override { return &m_fpr; } + +        size_t +        GetFPRSize() override { return sizeof(m_fpr); } + +    private: +        struct RegInfo +        { +            uint32_t num_registers; +            uint32_t num_gpr_registers; +            uint32_t num_fpr_registers; + +            uint32_t last_gpr; +            uint32_t first_fpr; +            uint32_t last_fpr; + +            uint32_t first_fpr_v; +            uint32_t last_fpr_v; + +            uint32_t gpr_flags; +        }; + +        struct QReg +        { +            uint8_t bytes[16]; +        }; + +        struct FPU +        { +            union { +                uint32_t s[32]; +                uint64_t d[32]; +                QReg     q[16];  // the 128-bit NEON registers +                } floats; +            uint32_t fpscr; +        }; + +        uint32_t m_gpr_arm[k_num_gpr_registers_arm]; +        RegInfo  m_reg_info; +        FPU m_fpr;  + +        // Debug register info for hardware breakpoints and watchpoints management. +        struct DREG +        { +            lldb::addr_t address;  // Breakpoint/watchpoint address value. +            uint32_t control;  // Breakpoint/watchpoint control value. +            uint32_t refcount;  // Serves as enable/disable and refernce counter. +        }; + +        struct DREG m_hbr_regs[16];  // Arm native linux hardware breakpoints +        struct DREG m_hwp_regs[16];  // Arm native linux hardware watchpoints + +        uint32_t m_max_hwp_supported; +        uint32_t m_max_hbp_supported; +        bool m_refresh_hwdebug_info; + +        bool +        IsGPR(unsigned reg) const; + +        bool +        IsFPR(unsigned reg) const; + +        Error +        ReadHardwareDebugInfo(); + +        Error +        WriteHardwareDebugRegs(int hwbType, int hwb_index); + +        uint32_t +        CalculateFprOffset(const RegisterInfo* reg_info) const; +    }; + +} // namespace process_linux +} // namespace lldb_private + +#endif // #ifndef lldb_NativeRegisterContextLinux_arm_h + +#endif // defined(__arm__) diff --git a/source/Plugins/Process/Linux/NativeRegisterContextLinux_arm64.cpp b/source/Plugins/Process/Linux/NativeRegisterContextLinux_arm64.cpp new file mode 100644 index 000000000000..e4d26fc640f3 --- /dev/null +++ b/source/Plugins/Process/Linux/NativeRegisterContextLinux_arm64.cpp @@ -0,0 +1,1042 @@ +//===-- NativeRegisterContextLinux_arm64.cpp --------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#if defined (__arm64__) || defined (__aarch64__) + +#include "NativeRegisterContextLinux_arm64.h" + +// C Includes +// C++ Includes + +// Other libraries and framework includes +#include "lldb/Core/DataBufferHeap.h" +#include "lldb/Core/Error.h" +#include "lldb/Core/Log.h" +#include "lldb/Core/RegisterValue.h" +#include "lldb/Host/common/NativeProcessProtocol.h" + +#include "Plugins/Process/Linux/NativeProcessLinux.h" +#include "Plugins/Process/Linux/Procfs.h" +#include "Plugins/Process/Utility/RegisterContextLinux_arm64.h" + +// System includes - They have to be included after framework includes because they define some +// macros which collide with variable names in other modules +#include <sys/socket.h> +// NT_PRSTATUS and NT_FPREGSET definition +#include <elf.h> + +#define REG_CONTEXT_SIZE (GetGPRSize() + GetFPRSize()) + +using namespace lldb; +using namespace lldb_private; +using namespace lldb_private::process_linux; + +// ARM64 general purpose registers. +static const uint32_t g_gpr_regnums_arm64[] = +{ +    gpr_x0_arm64, +    gpr_x1_arm64, +    gpr_x2_arm64, +    gpr_x3_arm64, +    gpr_x4_arm64, +    gpr_x5_arm64, +    gpr_x6_arm64, +    gpr_x7_arm64, +    gpr_x8_arm64, +    gpr_x9_arm64, +    gpr_x10_arm64, +    gpr_x11_arm64, +    gpr_x12_arm64, +    gpr_x13_arm64, +    gpr_x14_arm64, +    gpr_x15_arm64, +    gpr_x16_arm64, +    gpr_x17_arm64, +    gpr_x18_arm64, +    gpr_x19_arm64, +    gpr_x20_arm64, +    gpr_x21_arm64, +    gpr_x22_arm64, +    gpr_x23_arm64, +    gpr_x24_arm64, +    gpr_x25_arm64, +    gpr_x26_arm64, +    gpr_x27_arm64, +    gpr_x28_arm64, +    gpr_fp_arm64, +    gpr_lr_arm64, +    gpr_sp_arm64, +    gpr_pc_arm64, +    gpr_cpsr_arm64, +    LLDB_INVALID_REGNUM // register sets need to end with this flag +}; +static_assert(((sizeof g_gpr_regnums_arm64 / sizeof g_gpr_regnums_arm64[0]) - 1) == k_num_gpr_registers_arm64, \ +              "g_gpr_regnums_arm64 has wrong number of register infos"); + +// ARM64 floating point registers. +static const uint32_t g_fpu_regnums_arm64[] = +{ +    fpu_v0_arm64, +    fpu_v1_arm64, +    fpu_v2_arm64, +    fpu_v3_arm64, +    fpu_v4_arm64, +    fpu_v5_arm64, +    fpu_v6_arm64, +    fpu_v7_arm64, +    fpu_v8_arm64, +    fpu_v9_arm64, +    fpu_v10_arm64, +    fpu_v11_arm64, +    fpu_v12_arm64, +    fpu_v13_arm64, +    fpu_v14_arm64, +    fpu_v15_arm64, +    fpu_v16_arm64, +    fpu_v17_arm64, +    fpu_v18_arm64, +    fpu_v19_arm64, +    fpu_v20_arm64, +    fpu_v21_arm64, +    fpu_v22_arm64, +    fpu_v23_arm64, +    fpu_v24_arm64, +    fpu_v25_arm64, +    fpu_v26_arm64, +    fpu_v27_arm64, +    fpu_v28_arm64, +    fpu_v29_arm64, +    fpu_v30_arm64, +    fpu_v31_arm64, +    fpu_fpsr_arm64, +    fpu_fpcr_arm64, +    LLDB_INVALID_REGNUM // register sets need to end with this flag +}; +static_assert(((sizeof g_fpu_regnums_arm64 / sizeof g_fpu_regnums_arm64[0]) - 1) == k_num_fpr_registers_arm64, \ +              "g_fpu_regnums_arm64 has wrong number of register infos"); + +namespace { +    // Number of register sets provided by this context. +    enum +    { +        k_num_register_sets = 2 +    }; +} + +// Register sets for ARM64. +static const RegisterSet +g_reg_sets_arm64[k_num_register_sets] = +{ +    { "General Purpose Registers",  "gpr", k_num_gpr_registers_arm64, g_gpr_regnums_arm64 }, +    { "Floating Point Registers",   "fpu", k_num_fpr_registers_arm64, g_fpu_regnums_arm64 } +}; + +NativeRegisterContextLinux* +NativeRegisterContextLinux::CreateHostNativeRegisterContextLinux(const ArchSpec& target_arch, +                                                                 NativeThreadProtocol &native_thread, +                                                                 uint32_t concrete_frame_idx) +{ +    return new NativeRegisterContextLinux_arm64(target_arch, native_thread, concrete_frame_idx); +} + +NativeRegisterContextLinux_arm64::NativeRegisterContextLinux_arm64 (const ArchSpec& target_arch, +                                                                    NativeThreadProtocol &native_thread, +                                                                    uint32_t concrete_frame_idx) : +    NativeRegisterContextLinux (native_thread, concrete_frame_idx, new RegisterContextLinux_arm64(target_arch)) +{ +    switch (target_arch.GetMachine()) +    { +        case llvm::Triple::aarch64: +            m_reg_info.num_registers     = k_num_registers_arm64; +            m_reg_info.num_gpr_registers = k_num_gpr_registers_arm64; +            m_reg_info.num_fpr_registers = k_num_fpr_registers_arm64; +            m_reg_info.last_gpr          = k_last_gpr_arm64; +            m_reg_info.first_fpr         = k_first_fpr_arm64; +            m_reg_info.last_fpr          = k_last_fpr_arm64; +            m_reg_info.first_fpr_v       = fpu_v0_arm64; +            m_reg_info.last_fpr_v        = fpu_v31_arm64; +            m_reg_info.gpr_flags         = gpr_cpsr_arm64; +            break; +        default: +            assert(false && "Unhandled target architecture."); +            break; +    } + +    ::memset(&m_fpr, 0, sizeof (m_fpr)); +    ::memset(&m_gpr_arm64, 0, sizeof (m_gpr_arm64)); +    ::memset(&m_hwp_regs, 0, sizeof (m_hwp_regs)); + +    // 16 is just a maximum value, query hardware for actual watchpoint count +    m_max_hwp_supported = 16; +    m_max_hbp_supported = 16; +    m_refresh_hwdebug_info = true; +} + +uint32_t +NativeRegisterContextLinux_arm64::GetRegisterSetCount () const +{ +    return k_num_register_sets; +} + +const RegisterSet * +NativeRegisterContextLinux_arm64::GetRegisterSet (uint32_t set_index) const +{ +    if (set_index < k_num_register_sets) +        return &g_reg_sets_arm64[set_index]; + +    return nullptr; +} + +uint32_t +NativeRegisterContextLinux_arm64::GetUserRegisterCount() const +{ +    uint32_t count = 0; +    for (uint32_t set_index = 0; set_index < k_num_register_sets; ++set_index) +        count += g_reg_sets_arm64[set_index].num_registers; +    return count; +} + +Error +NativeRegisterContextLinux_arm64::ReadRegister (const RegisterInfo *reg_info, RegisterValue ®_value) +{ +    Error error; + +    if (!reg_info) +    { +        error.SetErrorString ("reg_info NULL"); +        return error; +    } + +    const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB]; + +    if (IsFPR(reg)) +    { +        error = ReadFPR(); +        if (error.Fail()) +            return error; +    } +    else +    { +        uint32_t full_reg = reg; +        bool is_subreg = reg_info->invalidate_regs && (reg_info->invalidate_regs[0] != LLDB_INVALID_REGNUM); + +        if (is_subreg) +        { +            // Read the full aligned 64-bit register. +            full_reg = reg_info->invalidate_regs[0]; +        } + +        error = ReadRegisterRaw(full_reg, reg_value); + +        if (error.Success ()) +        { +            // If our read was not aligned (for ah,bh,ch,dh), shift our returned value one byte to the right. +            if (is_subreg && (reg_info->byte_offset & 0x1)) +                reg_value.SetUInt64(reg_value.GetAsUInt64() >> 8); + +            // If our return byte size was greater than the return value reg size, then +            // use the type specified by reg_info rather than the uint64_t default +            if (reg_value.GetByteSize() > reg_info->byte_size) +                reg_value.SetType(reg_info); +        } +        return error; +    } + +    // Get pointer to m_fpr variable and set the data from it. +    uint32_t fpr_offset = CalculateFprOffset(reg_info); +    assert (fpr_offset < sizeof m_fpr); +    uint8_t *src = (uint8_t *)&m_fpr + fpr_offset; +    reg_value.SetFromMemoryData(reg_info, src, reg_info->byte_size, eByteOrderLittle, error); + +    return error; +} + +Error +NativeRegisterContextLinux_arm64::WriteRegister (const RegisterInfo *reg_info, const RegisterValue ®_value) +{ +    if (!reg_info) +        return Error ("reg_info NULL"); + +    const uint32_t reg_index = reg_info->kinds[lldb::eRegisterKindLLDB]; +    if (reg_index == LLDB_INVALID_REGNUM) +        return Error ("no lldb regnum for %s", reg_info && reg_info->name ? reg_info->name : "<unknown register>"); + +    if (IsGPR(reg_index)) +        return WriteRegisterRaw(reg_index, reg_value); + +    if (IsFPR(reg_index)) +    { +        // Get pointer to m_fpr variable and set the data to it. +        uint32_t fpr_offset = CalculateFprOffset(reg_info); +        assert (fpr_offset < sizeof m_fpr); +        uint8_t *dst = (uint8_t *)&m_fpr + fpr_offset; +        switch (reg_info->byte_size) +        { +            case 2: +                *(uint16_t *)dst = reg_value.GetAsUInt16(); +                break; +            case 4: +                *(uint32_t *)dst = reg_value.GetAsUInt32(); +                break; +            case 8: +                *(uint64_t *)dst = reg_value.GetAsUInt64(); +                break; +            default: +                assert(false && "Unhandled data size."); +                return Error ("unhandled register data size %" PRIu32, reg_info->byte_size); +        } + +        Error error = WriteFPR(); +        if (error.Fail()) +            return error; + +        return Error (); +    } + +    return Error ("failed - register wasn't recognized to be a GPR or an FPR, write strategy unknown"); +} + +Error +NativeRegisterContextLinux_arm64::ReadAllRegisterValues (lldb::DataBufferSP &data_sp) +{ +    Error error; + +    data_sp.reset (new DataBufferHeap (REG_CONTEXT_SIZE, 0)); +    if (!data_sp) +        return Error ("failed to allocate DataBufferHeap instance of size %" PRIu64, REG_CONTEXT_SIZE); + +    error = ReadGPR(); +    if (error.Fail()) +        return error; + +    error = ReadFPR(); +    if (error.Fail()) +        return error; + +    uint8_t *dst = data_sp->GetBytes (); +    if (dst == nullptr) +    { +        error.SetErrorStringWithFormat ("DataBufferHeap instance of size %" PRIu64 " returned a null pointer", REG_CONTEXT_SIZE); +        return error; +    } + +    ::memcpy (dst, &m_gpr_arm64, GetGPRSize()); +    dst += GetGPRSize(); +    ::memcpy (dst, &m_fpr, sizeof(m_fpr)); + +    return error; +} + +Error +NativeRegisterContextLinux_arm64::WriteAllRegisterValues (const lldb::DataBufferSP &data_sp) +{ +    Error error; + +    if (!data_sp) +    { +        error.SetErrorStringWithFormat ("NativeRegisterContextLinux_x86_64::%s invalid data_sp provided", __FUNCTION__); +        return error; +    } + +    if (data_sp->GetByteSize () != REG_CONTEXT_SIZE) +    { +        error.SetErrorStringWithFormat ("NativeRegisterContextLinux_x86_64::%s data_sp contained mismatched data size, expected %" PRIu64 ", actual %" PRIu64, __FUNCTION__, REG_CONTEXT_SIZE, data_sp->GetByteSize ()); +        return error; +    } + + +    uint8_t *src = data_sp->GetBytes (); +    if (src == nullptr) +    { +        error.SetErrorStringWithFormat ("NativeRegisterContextLinux_x86_64::%s DataBuffer::GetBytes() returned a null pointer", __FUNCTION__); +        return error; +    } +    ::memcpy (&m_gpr_arm64, src, GetRegisterInfoInterface ().GetGPRSize ()); + +    error = WriteGPR(); +    if (error.Fail()) +        return error; + +    src += GetRegisterInfoInterface ().GetGPRSize (); +    ::memcpy (&m_fpr, src, sizeof(m_fpr)); + +    error = WriteFPR(); +    if (error.Fail()) +        return error; + +    return error; +} + +bool +NativeRegisterContextLinux_arm64::IsGPR(unsigned reg) const +{ +    return reg <= m_reg_info.last_gpr;   // GPR's come first. +} + +bool +NativeRegisterContextLinux_arm64::IsFPR(unsigned reg) const +{ +    return (m_reg_info.first_fpr <= reg && reg <= m_reg_info.last_fpr); +} + +uint32_t +NativeRegisterContextLinux_arm64::SetHardwareBreakpoint (lldb::addr_t addr, size_t size) +{ +    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux_arm64::%s()", __FUNCTION__); + +    Error error; + +    // Read hardware breakpoint and watchpoint information. +    error = ReadHardwareDebugInfo (); + +    if (error.Fail()) +        return LLDB_INVALID_INDEX32; + +    uint32_t control_value = 0, bp_index = 0; + +    // Check if size has a valid hardware breakpoint length. +    if (size != 4) +        return LLDB_INVALID_INDEX32;  // Invalid size for a AArch64 hardware breakpoint + +    // Check 4-byte alignment for hardware breakpoint target address. +    if (addr & 0x03) +        return LLDB_INVALID_INDEX32; // Invalid address, should be 4-byte aligned. + +    // Setup control value +    control_value = 0; +    control_value |= ((1 << size) - 1) << 5; +    control_value |= (2 << 1) | 1; + +    // Iterate over stored hardware breakpoints +    // Find a free bp_index or update reference count if duplicate. +    bp_index = LLDB_INVALID_INDEX32; +    for (uint32_t i = 0; i < m_max_hbp_supported; i++) +    { +        if ((m_hbr_regs[i].control & 1) == 0) +        { +            bp_index = i;  // Mark last free slot +        } +        else if (m_hbr_regs[i].address == addr && m_hbr_regs[i].control == control_value) +        { +            bp_index = i;  // Mark duplicate index +            break;  // Stop searching here +        } +    } + +     if (bp_index == LLDB_INVALID_INDEX32) +        return LLDB_INVALID_INDEX32; + +    // Add new or update existing breakpoint +    if ((m_hbr_regs[bp_index].control & 1) == 0) +    { +        m_hbr_regs[bp_index].address = addr; +        m_hbr_regs[bp_index].control = control_value; +        m_hbr_regs[bp_index].refcount = 1; + +        // PTRACE call to set corresponding hardware breakpoint register. +        error = WriteHardwareDebugRegs(eDREGTypeBREAK); + +        if (error.Fail()) +        { +            m_hbr_regs[bp_index].address = 0; +            m_hbr_regs[bp_index].control &= ~1; +            m_hbr_regs[bp_index].refcount = 0; + +            return LLDB_INVALID_INDEX32; +        } +    } +    else +        m_hbr_regs[bp_index].refcount++; + +    return bp_index; +} + +bool +NativeRegisterContextLinux_arm64::ClearHardwareBreakpoint (uint32_t hw_idx) +{ +    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux_arm64::%s()", __FUNCTION__); + +    Error error; + +    // Read hardware breakpoint and watchpoint information. +    error = ReadHardwareDebugInfo (); + +    if (error.Fail()) +        return false; + +    if (hw_idx >= m_max_hbp_supported) +        return false; + +    // Update reference count if multiple references. +    if (m_hbr_regs[hw_idx].refcount > 1) +    { +        m_hbr_regs[hw_idx].refcount--; +        return true; +    } +    else if (m_hbr_regs[hw_idx].refcount == 1) +    { +        // Create a backup we can revert to in case of failure. +        lldb::addr_t tempAddr = m_hbr_regs[hw_idx].address; +        uint32_t tempControl = m_hbr_regs[hw_idx].control; +        uint32_t tempRefCount = m_hbr_regs[hw_idx].refcount; + +        m_hbr_regs[hw_idx].control &= ~1; +        m_hbr_regs[hw_idx].address = 0; +        m_hbr_regs[hw_idx].refcount = 0; + +        // PTRACE call to clear corresponding hardware breakpoint register. +        WriteHardwareDebugRegs(eDREGTypeBREAK); + +        if (error.Fail()) +        { +            m_hbr_regs[hw_idx].control = tempControl; +            m_hbr_regs[hw_idx].address = tempAddr; +            m_hbr_regs[hw_idx].refcount = tempRefCount; + +            return false; +        } + +        return true; +    } + +    return false; +} + +uint32_t +NativeRegisterContextLinux_arm64::NumSupportedHardwareWatchpoints () +{ +    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux_arm64::%s()", __FUNCTION__); + +    Error error; + +    // Read hardware breakpoint and watchpoint information. +    error = ReadHardwareDebugInfo (); + +    if (error.Fail()) +        return LLDB_INVALID_INDEX32; + +    return m_max_hwp_supported; +} + +uint32_t +NativeRegisterContextLinux_arm64::SetHardwareWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags) +{ +    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux_arm64::%s()", __FUNCTION__); +     +    Error error; + +    // Read hardware breakpoint and watchpoint information. +    error = ReadHardwareDebugInfo (); + +    if (error.Fail()) +        return LLDB_INVALID_INDEX32; +		 +    uint32_t control_value = 0, wp_index = 0; + +    // Check if we are setting watchpoint other than read/write/access +    // Also update watchpoint flag to match AArch64 write-read bit configuration. +    switch (watch_flags) +    { +        case 1: +            watch_flags = 2; +            break; +        case 2: +            watch_flags = 1; +            break; +        case 3: +            break; +        default: +            return LLDB_INVALID_INDEX32; +    } + +    // Check if size has a valid hardware watchpoint length. +    if (size != 1 && size != 2 && size != 4 && size != 8) +        return LLDB_INVALID_INDEX32; + +    // Check 8-byte alignment for hardware watchpoint target address. +    // TODO: Add support for watching un-aligned addresses +    if (addr & 0x07) +        return LLDB_INVALID_INDEX32; + +    // Setup control value +    control_value = watch_flags << 3; +    control_value |= ((1 << size) - 1) << 5; +    control_value |= (2 << 1) | 1; + +    // Iterate over stored watchpoints +    // Find a free wp_index or update reference count if duplicate. +    wp_index = LLDB_INVALID_INDEX32; +    for (uint32_t i = 0; i < m_max_hwp_supported; i++) +    { +        if ((m_hwp_regs[i].control & 1) == 0) +        { +            wp_index = i; // Mark last free slot +        } +        else if (m_hwp_regs[i].address == addr && m_hwp_regs[i].control == control_value) +        { +            wp_index = i; // Mark duplicate index +            break; // Stop searching here +        } +    } + +     if (wp_index == LLDB_INVALID_INDEX32) +        return LLDB_INVALID_INDEX32; + +    // Add new or update existing watchpoint +    if ((m_hwp_regs[wp_index].control & 1) == 0) +    { +        // Update watchpoint in local cache +        m_hwp_regs[wp_index].address = addr; +        m_hwp_regs[wp_index].control = control_value; +        m_hwp_regs[wp_index].refcount = 1; + +        // PTRACE call to set corresponding watchpoint register. +        error = WriteHardwareDebugRegs(eDREGTypeWATCH); + +        if (error.Fail()) +        { +            m_hwp_regs[wp_index].address = 0; +            m_hwp_regs[wp_index].control &= ~1; +            m_hwp_regs[wp_index].refcount = 0; + +            return LLDB_INVALID_INDEX32; +        } +    } +    else +        m_hwp_regs[wp_index].refcount++; + +    return wp_index; +} + +bool +NativeRegisterContextLinux_arm64::ClearHardwareWatchpoint (uint32_t wp_index) +{ +    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux_arm64::%s()", __FUNCTION__); + +    Error error; + +    // Read hardware breakpoint and watchpoint information. +    error = ReadHardwareDebugInfo (); + +    if (error.Fail()) +        return false; + +    if (wp_index >= m_max_hwp_supported) +        return false; + +    // Update reference count if multiple references. +    if (m_hwp_regs[wp_index].refcount > 1) +    { +        m_hwp_regs[wp_index].refcount--; +        return true; +    } +    else if (m_hwp_regs[wp_index].refcount == 1) +    { +        // Create a backup we can revert to in case of failure. +        lldb::addr_t tempAddr = m_hwp_regs[wp_index].address; +        uint32_t tempControl = m_hwp_regs[wp_index].control; +        uint32_t tempRefCount = m_hwp_regs[wp_index].refcount; + +        // Update watchpoint in local cache +        m_hwp_regs[wp_index].control &= ~1; +        m_hwp_regs[wp_index].address = 0; +        m_hwp_regs[wp_index].refcount = 0; + +        // Ptrace call to update hardware debug registers +        error = WriteHardwareDebugRegs(eDREGTypeWATCH); + +        if (error.Fail()) +        { +            m_hwp_regs[wp_index].control = tempControl; +            m_hwp_regs[wp_index].address = tempAddr; +            m_hwp_regs[wp_index].refcount = tempRefCount; + +            return false; +        } + +        return true; +    } + +    return false; +} + +Error +NativeRegisterContextLinux_arm64::ClearAllHardwareWatchpoints () +{ +    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux_arm64::%s()", __FUNCTION__); + +    Error error; + +    // Read hardware breakpoint and watchpoint information. +    error = ReadHardwareDebugInfo (); + +    if (error.Fail()) +        return error; + +    lldb::addr_t tempAddr = 0; +    uint32_t tempControl = 0, tempRefCount = 0; + +    for (uint32_t i = 0; i < m_max_hwp_supported; i++) +    { +        if (m_hwp_regs[i].control & 0x01) +        { +            // Create a backup we can revert to in case of failure. +            tempAddr = m_hwp_regs[i].address; +            tempControl = m_hwp_regs[i].control; +            tempRefCount = m_hwp_regs[i].refcount; + +            // Clear watchpoints in local cache +            m_hwp_regs[i].control &= ~1; +            m_hwp_regs[i].address = 0; +            m_hwp_regs[i].refcount = 0; + +            // Ptrace call to update hardware debug registers +            error = WriteHardwareDebugRegs(eDREGTypeWATCH); + +            if (error.Fail()) +            { +                m_hwp_regs[i].control = tempControl; +                m_hwp_regs[i].address = tempAddr; +                m_hwp_regs[i].refcount = tempRefCount; + +                return error; +            } +        } +    } + +    return Error(); +} + +uint32_t +NativeRegisterContextLinux_arm64::GetWatchpointSize(uint32_t wp_index) +{ +    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux_arm64::%s()", __FUNCTION__); +    switch ((m_hwp_regs[wp_index].control >> 5) & 0xff) +    { +        case 0x01: +            return 1; +        case 0x03: +            return 2; +        case 0x0f: +            return 4; +        case 0xff: +            return 8; +        default: +            return 0; +    } +} +bool +NativeRegisterContextLinux_arm64::WatchpointIsEnabled(uint32_t wp_index) +{ +    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux_arm64::%s()", __FUNCTION__); + +    if ((m_hwp_regs[wp_index].control & 0x1) == 0x1) +        return true; +    else +        return false; +} + +Error +NativeRegisterContextLinux_arm64::GetWatchpointHitIndex(uint32_t &wp_index, lldb::addr_t trap_addr) +{ +    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux_arm64::%s()", __FUNCTION__); + +    uint32_t watch_size; +    lldb::addr_t watch_addr; + +    for (wp_index = 0; wp_index < m_max_hwp_supported; ++wp_index) +    { +        watch_size = GetWatchpointSize (wp_index); +        watch_addr = m_hwp_regs[wp_index].address; + +        if (m_hwp_regs[wp_index].refcount >= 1 && WatchpointIsEnabled(wp_index) +            && trap_addr >= watch_addr && trap_addr < watch_addr + watch_size) +        { +            return Error(); +        } +    } + +    wp_index = LLDB_INVALID_INDEX32; +    return Error(); +} + +lldb::addr_t +NativeRegisterContextLinux_arm64::GetWatchpointAddress (uint32_t wp_index) +{ +    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS)); + +    if (log) +        log->Printf ("NativeRegisterContextLinux_arm64::%s()", __FUNCTION__); + +    if (wp_index >= m_max_hwp_supported) +        return LLDB_INVALID_ADDRESS; + +    if (WatchpointIsEnabled(wp_index)) +        return m_hwp_regs[wp_index].address; +    else +        return LLDB_INVALID_ADDRESS; +} + +Error +NativeRegisterContextLinux_arm64::ReadHardwareDebugInfo() +{ +    if (!m_refresh_hwdebug_info) +    { +        return Error(); +    } + +    ::pid_t tid = m_thread.GetID(); + +    int regset = NT_ARM_HW_WATCH; +    struct iovec ioVec; +    struct user_hwdebug_state dreg_state; +    Error error; + +    ioVec.iov_base = &dreg_state; +    ioVec.iov_len = sizeof (dreg_state); +    error = NativeProcessLinux::PtraceWrapper(PTRACE_GETREGSET, tid, ®set, &ioVec, ioVec.iov_len); + +    if (error.Fail()) +        return error; + +    m_max_hwp_supported = dreg_state.dbg_info & 0xff; + +    regset = NT_ARM_HW_BREAK; +    error = NativeProcessLinux::PtraceWrapper(PTRACE_GETREGSET, tid, ®set, &ioVec, ioVec.iov_len); + +    if (error.Fail()) +        return error; +	 +    m_max_hbp_supported = dreg_state.dbg_info & 0xff; +    m_refresh_hwdebug_info = false; + +    return error; +} + +Error +NativeRegisterContextLinux_arm64::WriteHardwareDebugRegs(int hwbType) +{ +    struct iovec ioVec; +    struct user_hwdebug_state dreg_state; +    Error error; + +    memset (&dreg_state, 0, sizeof (dreg_state)); +    ioVec.iov_base = &dreg_state; + +    if (hwbType == eDREGTypeWATCH) +    { +        hwbType = NT_ARM_HW_WATCH; +        ioVec.iov_len = sizeof (dreg_state.dbg_info) + sizeof (dreg_state.pad) +                + (sizeof (dreg_state.dbg_regs [0]) * m_max_hwp_supported); + +        for (uint32_t i = 0; i < m_max_hwp_supported; i++) +        { +            dreg_state.dbg_regs[i].addr = m_hwp_regs[i].address; +            dreg_state.dbg_regs[i].ctrl = m_hwp_regs[i].control; +        } +    } +    else +    { +        hwbType = NT_ARM_HW_BREAK; +        ioVec.iov_len = sizeof (dreg_state.dbg_info) + sizeof (dreg_state.pad) +                + (sizeof (dreg_state.dbg_regs [0]) * m_max_hbp_supported); + +        for (uint32_t i = 0; i < m_max_hbp_supported; i++) +        { +            dreg_state.dbg_regs[i].addr = m_hbr_regs[i].address; +            dreg_state.dbg_regs[i].ctrl = m_hbr_regs[i].control; +        } +    } + +    return NativeProcessLinux::PtraceWrapper(PTRACE_SETREGSET, m_thread.GetID(), &hwbType, &ioVec, ioVec.iov_len); +} + +Error +NativeRegisterContextLinux_arm64::DoReadRegisterValue(uint32_t offset, +                                                      const char* reg_name, +                                                      uint32_t size, +                                                      RegisterValue &value) +{ +    Error error; +    if (offset > sizeof(struct user_pt_regs)) +    { +        uintptr_t offset = offset - sizeof(struct user_pt_regs); +        if (offset > sizeof(struct user_fpsimd_state)) +        { +            error.SetErrorString("invalid offset value"); +            return error; +        } +        elf_fpregset_t regs; +        int regset = NT_FPREGSET; +        struct iovec ioVec; + +        ioVec.iov_base = ®s; +        ioVec.iov_len = sizeof regs; +        error = NativeProcessLinux::PtraceWrapper( +                PTRACE_GETREGSET, m_thread.GetID(), ®set, &ioVec, sizeof regs); +        if (error.Success()) +        { +            ArchSpec arch; +            if (m_thread.GetProcess()->GetArchitecture(arch)) +                value.SetBytes((void *)(((unsigned char *)(®s)) + offset), 16, arch.GetByteOrder()); +            else +                error.SetErrorString("failed to get architecture"); +        } +    } +    else +    { +        elf_gregset_t regs; +        int regset = NT_PRSTATUS; +        struct iovec ioVec; + +        ioVec.iov_base = ®s; +        ioVec.iov_len = sizeof regs; +        error = NativeProcessLinux::PtraceWrapper( +                PTRACE_GETREGSET, m_thread.GetID(), ®set, &ioVec, sizeof regs); +        if (error.Success()) +        { +            ArchSpec arch; +            if (m_thread.GetProcess()->GetArchitecture(arch)) +                value.SetBytes((void *)(((unsigned char *)(regs)) + offset), 8, arch.GetByteOrder()); +            else +                error.SetErrorString("failed to get architecture"); +        } +    } +    return error; +} + +Error +NativeRegisterContextLinux_arm64::DoWriteRegisterValue(uint32_t offset, +                                                       const char* reg_name, +                                                       const RegisterValue &value) +{ +    Error error; +    ::pid_t tid = m_thread.GetID(); +    if (offset > sizeof(struct user_pt_regs)) +    { +        uintptr_t offset = offset - sizeof(struct user_pt_regs); +        if (offset > sizeof(struct user_fpsimd_state)) +        { +            error.SetErrorString("invalid offset value"); +            return error; +        } +        elf_fpregset_t regs; +        int regset = NT_FPREGSET; +        struct iovec ioVec; + +        ioVec.iov_base = ®s; +        ioVec.iov_len = sizeof regs; +        error = NativeProcessLinux::PtraceWrapper( PTRACE_GETREGSET, tid, ®set, &ioVec, sizeof regs); + +        if (error.Success()) +        { +            ::memcpy((void *)(((unsigned char *)(®s)) + offset), value.GetBytes(), 16); +            error = NativeProcessLinux::PtraceWrapper(PTRACE_SETREGSET, tid, ®set, &ioVec, sizeof regs); +        } +    } +    else +    { +        elf_gregset_t regs; +        int regset = NT_PRSTATUS; +        struct iovec ioVec; + +        ioVec.iov_base = ®s; +        ioVec.iov_len = sizeof regs; +        error = NativeProcessLinux::PtraceWrapper(PTRACE_GETREGSET, tid, ®set, &ioVec, sizeof regs); +        if (error.Success()) +        { +            ::memcpy((void *)(((unsigned char *)(®s)) + offset), value.GetBytes(), 8); +            error = NativeProcessLinux::PtraceWrapper(PTRACE_SETREGSET, tid, ®set, &ioVec, sizeof regs); +        } +    } +    return error; +} + +Error +NativeRegisterContextLinux_arm64::DoReadGPR(void *buf, size_t buf_size) +{ +    int regset = NT_PRSTATUS; +    struct iovec ioVec; +    Error error; + +    ioVec.iov_base = buf; +    ioVec.iov_len = buf_size; +    return NativeProcessLinux::PtraceWrapper(PTRACE_GETREGSET, m_thread.GetID(), ®set, &ioVec, buf_size); +} + +Error +NativeRegisterContextLinux_arm64::DoWriteGPR(void *buf, size_t buf_size) +{ +    int regset = NT_PRSTATUS; +    struct iovec ioVec; +    Error error; + +    ioVec.iov_base = buf; +    ioVec.iov_len = buf_size; +    return NativeProcessLinux::PtraceWrapper(PTRACE_SETREGSET, m_thread.GetID(), ®set, &ioVec, buf_size); +} + +Error +NativeRegisterContextLinux_arm64::DoReadFPR(void *buf, size_t buf_size) +{ +    int regset = NT_FPREGSET; +    struct iovec ioVec; +    Error error; + +    ioVec.iov_base = buf; +    ioVec.iov_len = buf_size; +    return NativeProcessLinux::PtraceWrapper(PTRACE_GETREGSET, m_thread.GetID(), ®set, &ioVec, buf_size); +} + +Error +NativeRegisterContextLinux_arm64::DoWriteFPR(void *buf, size_t buf_size) +{ +    int regset = NT_FPREGSET; +    struct iovec ioVec; +    Error error; + +    ioVec.iov_base = buf; +    ioVec.iov_len = buf_size; +    return NativeProcessLinux::PtraceWrapper(PTRACE_SETREGSET, m_thread.GetID(), ®set, &ioVec, buf_size); +} + +uint32_t +NativeRegisterContextLinux_arm64::CalculateFprOffset(const RegisterInfo* reg_info) const +{ +    return reg_info->byte_offset - GetRegisterInfoAtIndex(m_reg_info.first_fpr)->byte_offset; +} + +#endif // defined (__arm64__) || defined (__aarch64__) diff --git a/source/Plugins/Process/Linux/NativeRegisterContextLinux_arm64.h b/source/Plugins/Process/Linux/NativeRegisterContextLinux_arm64.h new file mode 100644 index 000000000000..c60baa637b8a --- /dev/null +++ b/source/Plugins/Process/Linux/NativeRegisterContextLinux_arm64.h @@ -0,0 +1,196 @@ +//===-- NativeRegisterContextLinux_arm64.h ---------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#if defined (__arm64__) || defined (__aarch64__) + +#ifndef lldb_NativeRegisterContextLinux_arm64_h +#define lldb_NativeRegisterContextLinux_arm64_h + +#include "Plugins/Process/Linux/NativeRegisterContextLinux.h" +#include "Plugins/Process/Utility/lldb-arm64-register-enums.h" + +namespace lldb_private { +namespace process_linux { + +    class NativeProcessLinux; + +    class NativeRegisterContextLinux_arm64 : public NativeRegisterContextLinux +    { +    public: +        NativeRegisterContextLinux_arm64 (const ArchSpec& target_arch, +                                          NativeThreadProtocol &native_thread, +                                          uint32_t concrete_frame_idx); + +        uint32_t +        GetRegisterSetCount () const override; + +        uint32_t +        GetUserRegisterCount() const override; + +        const RegisterSet * +        GetRegisterSet (uint32_t set_index) const override; + +        Error +        ReadRegister (const RegisterInfo *reg_info, RegisterValue ®_value) override; + +        Error +        WriteRegister (const RegisterInfo *reg_info, const RegisterValue ®_value) override; + +        Error +        ReadAllRegisterValues (lldb::DataBufferSP &data_sp) override; + +        Error +        WriteAllRegisterValues (const lldb::DataBufferSP &data_sp) override; + +        //------------------------------------------------------------------ +        // Hardware breakpoints/watchpoint mangement functions +        //------------------------------------------------------------------ + +        uint32_t +        SetHardwareBreakpoint (lldb::addr_t addr, size_t size) override; + +        bool +        ClearHardwareBreakpoint (uint32_t hw_idx) override; + +        uint32_t +        NumSupportedHardwareWatchpoints () override; + +        uint32_t +        SetHardwareWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags) override; + +        bool +        ClearHardwareWatchpoint (uint32_t hw_index) override; + +        Error +        ClearAllHardwareWatchpoints () override; + +        Error +        GetWatchpointHitIndex(uint32_t &wp_index, lldb::addr_t trap_addr) override; + +        lldb::addr_t +        GetWatchpointAddress (uint32_t wp_index) override; + +        uint32_t +        GetWatchpointSize(uint32_t wp_index); + +        bool +        WatchpointIsEnabled(uint32_t wp_index); + +        // Debug register type select +        enum DREGType +        { +            eDREGTypeWATCH = 0, +            eDREGTypeBREAK +        }; + +    protected: +        Error +        DoReadRegisterValue(uint32_t offset, +                            const char* reg_name, +                            uint32_t size, +                            RegisterValue &value) override; + +        Error +        DoWriteRegisterValue(uint32_t offset, +                             const char* reg_name, +                             const RegisterValue &value) override; + +        Error +        DoReadGPR(void *buf, size_t buf_size) override; + +        Error +        DoWriteGPR(void *buf, size_t buf_size) override; + +        Error +        DoReadFPR(void *buf, size_t buf_size) override; + +        Error +        DoWriteFPR(void *buf, size_t buf_size) override; + +        void* +        GetGPRBuffer() override { return &m_gpr_arm64; } + +        void* +        GetFPRBuffer() override { return &m_fpr; } + +        size_t +        GetFPRSize() override { return sizeof(m_fpr); } + +    private: +        struct RegInfo +        { +            uint32_t num_registers; +            uint32_t num_gpr_registers; +            uint32_t num_fpr_registers; + +            uint32_t last_gpr; +            uint32_t first_fpr; +            uint32_t last_fpr; + +            uint32_t first_fpr_v; +            uint32_t last_fpr_v; + +            uint32_t gpr_flags; +        }; + +        // based on RegisterContextDarwin_arm64.h +        struct VReg +        { +            uint8_t bytes[16]; +        }; + +        // based on RegisterContextDarwin_arm64.h +        struct FPU +        { +            VReg        v[32]; +            uint32_t    fpsr; +            uint32_t    fpcr; +        }; + +        uint64_t m_gpr_arm64[k_num_gpr_registers_arm64]; // 64-bit general purpose registers. +        RegInfo  m_reg_info; +        FPU m_fpr; // floating-point registers including extended register sets. + +        // Debug register info for hardware breakpoints and watchpoints management. +        struct DREG +        { +            lldb::addr_t address;  // Breakpoint/watchpoint address value. +            uint32_t control;  // Breakpoint/watchpoint control value. +            uint32_t refcount;  // Serves as enable/disable and refernce counter. +        }; + +        struct DREG m_hbr_regs[16];  // Arm native linux hardware breakpoints +        struct DREG m_hwp_regs[16];  // Arm native linux hardware watchpoints + +        uint32_t m_max_hwp_supported; +        uint32_t m_max_hbp_supported; +        bool m_refresh_hwdebug_info; + +        bool +        IsGPR(unsigned reg) const; + +        bool +        IsFPR(unsigned reg) const; + +        Error +        ReadHardwareDebugInfo(); + +        Error +        WriteHardwareDebugRegs(int hwbType); + +        uint32_t +        CalculateFprOffset(const RegisterInfo* reg_info) const; +    }; + +} // namespace process_linux +} // namespace lldb_private + +#endif // #ifndef lldb_NativeRegisterContextLinux_arm64_h + +#endif // defined (__arm64__) || defined (__aarch64__) diff --git a/source/Plugins/Process/Linux/NativeRegisterContextLinux_mips64.cpp b/source/Plugins/Process/Linux/NativeRegisterContextLinux_mips64.cpp new file mode 100644 index 000000000000..3cfeaf5546bc --- /dev/null +++ b/source/Plugins/Process/Linux/NativeRegisterContextLinux_mips64.cpp @@ -0,0 +1,1431 @@ +//===-- NativeRegisterContextLinux_mips64.cpp ---------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#if defined (__mips__) + +#include "NativeRegisterContextLinux_mips64.h" + +// C Includes +// C++ Includes + +// Other libraries and framework includes +#include "lldb/Core/Error.h" +#include "lldb/Core/RegisterValue.h" +#include "lldb/Core/Log.h" +#include "lldb/Core/DataBufferHeap.h" +#include "lldb/Host/HostInfo.h" +#include "lldb/Host/Host.h" +#include "lldb/Core/EmulateInstruction.h" +#include "lldb/lldb-enumerations.h" +#include "lldb/lldb-private-enumerations.h" +#include "Plugins/Process/Linux/NativeProcessLinux.h" +#include "Plugins/Process/Linux/Procfs.h" +#include "Plugins/Process/Utility/RegisterContextLinux_mips64.h" +#include "Plugins/Process/Utility/RegisterContextLinux_mips.h" +#define NT_MIPS_MSA 0x600 +#define CONFIG5_FRE (1 << 8) +#define SR_FR (1 << 26) +#define NUM_REGISTERS 32 + +#include <sys/ptrace.h> +#include <asm/ptrace.h> + +#ifndef PTRACE_GET_WATCH_REGS +enum pt_watch_style +{ +    pt_watch_style_mips32, +    pt_watch_style_mips64 +}; +struct mips32_watch_regs +{ +    uint32_t watchlo[8]; +    uint16_t watchhi[8]; +    uint16_t watch_masks[8]; +    uint32_t num_valid; +} __attribute__((aligned(8))); + +struct mips64_watch_regs +{ +    uint64_t watchlo[8]; +    uint16_t watchhi[8]; +    uint16_t watch_masks[8]; +    uint32_t num_valid; +} __attribute__((aligned(8))); + +struct pt_watch_regs +{ +    enum pt_watch_style style; +    union +    { +        struct mips32_watch_regs mips32; +        struct mips64_watch_regs mips64; +    }; +}; + +#define PTRACE_GET_WATCH_REGS 0xd0 +#define PTRACE_SET_WATCH_REGS 0xd1 +#endif + +#define W (1 << 0) +#define R (1 << 1) +#define I (1 << 2) + +#define IRW  (I | R | W) + +struct pt_watch_regs default_watch_regs; + +using namespace lldb_private; +using namespace lldb_private::process_linux; + +// ---------------------------------------------------------------------------- +// Private namespace. +// ---------------------------------------------------------------------------- + +namespace +{ +    // mips general purpose registers. +    const uint32_t +    g_gp_regnums_mips[] = +    { +        gpr_zero_mips, +        gpr_r1_mips, +        gpr_r2_mips, +        gpr_r3_mips, +        gpr_r4_mips, +        gpr_r5_mips, +        gpr_r6_mips, +        gpr_r7_mips, +        gpr_r8_mips, +        gpr_r9_mips, +        gpr_r10_mips, +        gpr_r11_mips, +        gpr_r12_mips, +        gpr_r13_mips, +        gpr_r14_mips, +        gpr_r15_mips, +        gpr_r16_mips, +        gpr_r17_mips, +        gpr_r18_mips, +        gpr_r19_mips, +        gpr_r20_mips, +        gpr_r21_mips, +        gpr_r22_mips, +        gpr_r23_mips, +        gpr_r24_mips, +        gpr_r25_mips, +        gpr_r26_mips, +        gpr_r27_mips, +        gpr_gp_mips, +        gpr_sp_mips, +        gpr_r30_mips, +        gpr_ra_mips, +        gpr_sr_mips, +        gpr_mullo_mips, +        gpr_mulhi_mips, +        gpr_badvaddr_mips, +        gpr_cause_mips, +        gpr_pc_mips, +        gpr_config5_mips, +        LLDB_INVALID_REGNUM     // register sets need to end with this flag +    }; + +    static_assert((sizeof(g_gp_regnums_mips) / sizeof(g_gp_regnums_mips[0])) - 1 == k_num_gpr_registers_mips, +                  "g_gp_regnums_mips has wrong number of register infos"); + +    // mips floating point registers. +    const uint32_t +    g_fp_regnums_mips[] = +    { +        fpr_f0_mips, +        fpr_f1_mips, +        fpr_f2_mips, +        fpr_f3_mips, +        fpr_f4_mips, +        fpr_f5_mips, +        fpr_f6_mips, +        fpr_f7_mips, +        fpr_f8_mips, +        fpr_f9_mips, +        fpr_f10_mips, +        fpr_f11_mips, +        fpr_f12_mips, +        fpr_f13_mips, +        fpr_f14_mips, +        fpr_f15_mips, +        fpr_f16_mips, +        fpr_f17_mips, +        fpr_f18_mips, +        fpr_f19_mips, +        fpr_f20_mips, +        fpr_f21_mips, +        fpr_f22_mips, +        fpr_f23_mips, +        fpr_f24_mips, +        fpr_f25_mips, +        fpr_f26_mips, +        fpr_f27_mips, +        fpr_f28_mips, +        fpr_f29_mips, +        fpr_f30_mips, +        fpr_f31_mips, +        fpr_fcsr_mips, +        fpr_fir_mips, +        fpr_config5_mips, +        LLDB_INVALID_REGNUM     // register sets need to end with this flag +    }; + +    static_assert((sizeof(g_fp_regnums_mips) / sizeof(g_fp_regnums_mips[0])) - 1 == k_num_fpr_registers_mips, +                  "g_fp_regnums_mips has wrong number of register infos"); + +    // mips MSA registers. +    const uint32_t +    g_msa_regnums_mips[] = +    { +        msa_w0_mips, +        msa_w1_mips, +        msa_w2_mips, +        msa_w3_mips, +        msa_w4_mips, +        msa_w5_mips, +        msa_w6_mips, +        msa_w7_mips, +        msa_w8_mips, +        msa_w9_mips, +        msa_w10_mips, +        msa_w11_mips, +        msa_w12_mips, +        msa_w13_mips, +        msa_w14_mips, +        msa_w15_mips, +        msa_w16_mips, +        msa_w17_mips, +        msa_w18_mips, +        msa_w19_mips, +        msa_w20_mips, +        msa_w21_mips, +        msa_w22_mips, +        msa_w23_mips, +        msa_w24_mips, +        msa_w25_mips, +        msa_w26_mips, +        msa_w27_mips, +        msa_w28_mips, +        msa_w29_mips, +        msa_w30_mips, +        msa_w31_mips, +        msa_fcsr_mips, +        msa_fir_mips, +        msa_mcsr_mips, +        msa_mir_mips, +        msa_config5_mips, +        LLDB_INVALID_REGNUM     // register sets need to end with this flag +    }; + +    static_assert((sizeof(g_msa_regnums_mips) / sizeof(g_msa_regnums_mips[0])) - 1 == k_num_msa_registers_mips, +                  "g_msa_regnums_mips has wrong number of register infos"); + +    // mips64 general purpose registers. +    const uint32_t +    g_gp_regnums_mips64[] = +    { +        gpr_zero_mips64, +        gpr_r1_mips64, +        gpr_r2_mips64, +        gpr_r3_mips64, +        gpr_r4_mips64, +        gpr_r5_mips64, +        gpr_r6_mips64, +        gpr_r7_mips64, +        gpr_r8_mips64, +        gpr_r9_mips64, +        gpr_r10_mips64, +        gpr_r11_mips64, +        gpr_r12_mips64, +        gpr_r13_mips64, +        gpr_r14_mips64, +        gpr_r15_mips64, +        gpr_r16_mips64, +        gpr_r17_mips64, +        gpr_r18_mips64, +        gpr_r19_mips64, +        gpr_r20_mips64, +        gpr_r21_mips64, +        gpr_r22_mips64, +        gpr_r23_mips64, +        gpr_r24_mips64, +        gpr_r25_mips64, +        gpr_r26_mips64, +        gpr_r27_mips64, +        gpr_gp_mips64, +        gpr_sp_mips64, +        gpr_r30_mips64, +        gpr_ra_mips64, +        gpr_sr_mips64, +        gpr_mullo_mips64, +        gpr_mulhi_mips64, +        gpr_badvaddr_mips64, +        gpr_cause_mips64, +        gpr_pc_mips64, +        gpr_config5_mips64, +        LLDB_INVALID_REGNUM     // register sets need to end with this flag +    }; + +    static_assert((sizeof(g_gp_regnums_mips64) / sizeof(g_gp_regnums_mips64[0])) - 1 == k_num_gpr_registers_mips64, +                  "g_gp_regnums_mips64 has wrong number of register infos"); + +    // mips64 floating point registers. +    const uint32_t +    g_fp_regnums_mips64[] = +    { +        fpr_f0_mips64, +        fpr_f1_mips64, +        fpr_f2_mips64, +        fpr_f3_mips64, +        fpr_f4_mips64, +        fpr_f5_mips64, +        fpr_f6_mips64, +        fpr_f7_mips64, +        fpr_f8_mips64, +        fpr_f9_mips64, +        fpr_f10_mips64, +        fpr_f11_mips64, +        fpr_f12_mips64, +        fpr_f13_mips64, +        fpr_f14_mips64, +        fpr_f15_mips64, +        fpr_f16_mips64, +        fpr_f17_mips64, +        fpr_f18_mips64, +        fpr_f19_mips64, +        fpr_f20_mips64, +        fpr_f21_mips64, +        fpr_f22_mips64, +        fpr_f23_mips64, +        fpr_f24_mips64, +        fpr_f25_mips64, +        fpr_f26_mips64, +        fpr_f27_mips64, +        fpr_f28_mips64, +        fpr_f29_mips64, +        fpr_f30_mips64, +        fpr_f31_mips64, +        fpr_fcsr_mips64, +        fpr_fir_mips64, +        fpr_config5_mips64, +        LLDB_INVALID_REGNUM     // register sets need to end with this flag +    }; + +    static_assert((sizeof(g_fp_regnums_mips64) / sizeof(g_fp_regnums_mips64[0])) - 1 == k_num_fpr_registers_mips64, +                  "g_fp_regnums_mips64 has wrong number of register infos"); + +    // mips64 MSA registers. +    const uint32_t +    g_msa_regnums_mips64[] = +    { +        msa_w0_mips64, +        msa_w1_mips64, +        msa_w2_mips64, +        msa_w3_mips64, +        msa_w4_mips64, +        msa_w5_mips64, +        msa_w6_mips64, +        msa_w7_mips64, +        msa_w8_mips64, +        msa_w9_mips64, +        msa_w10_mips64, +        msa_w11_mips64, +        msa_w12_mips64, +        msa_w13_mips64, +        msa_w14_mips64, +        msa_w15_mips64, +        msa_w16_mips64, +        msa_w17_mips64, +        msa_w18_mips64, +        msa_w19_mips64, +        msa_w20_mips64, +        msa_w21_mips64, +        msa_w22_mips64, +        msa_w23_mips64, +        msa_w24_mips64, +        msa_w25_mips64, +        msa_w26_mips64, +        msa_w27_mips64, +        msa_w28_mips64, +        msa_w29_mips64, +        msa_w30_mips64, +        msa_w31_mips64, +        msa_fcsr_mips64, +        msa_fir_mips64, +        msa_mcsr_mips64, +        msa_mir_mips64, +        msa_config5_mips64, +        LLDB_INVALID_REGNUM     // register sets need to end with this flag +    }; + +    static_assert((sizeof(g_msa_regnums_mips64) / sizeof(g_msa_regnums_mips64[0])) - 1 == k_num_msa_registers_mips64, +                  "g_msa_regnums_mips64 has wrong number of register infos"); + +    // Number of register sets provided by this context. +    enum +    { +        k_num_register_sets = 3 +    }; + +    // Register sets for mips. +    static const RegisterSet +    g_reg_sets_mips[k_num_register_sets] = +    { +        { "General Purpose Registers",  "gpr", k_num_gpr_registers_mips, g_gp_regnums_mips }, +        { "Floating Point Registers",   "fpu", k_num_fpr_registers_mips, g_fp_regnums_mips }, +        { "MSA Registers",              "msa", k_num_msa_registers_mips, g_msa_regnums_mips } +    }; + +    // Register sets for mips64. +    static const RegisterSet +    g_reg_sets_mips64[k_num_register_sets] = +    { +        { "General Purpose Registers",  "gpr", k_num_gpr_registers_mips64, g_gp_regnums_mips64 }, +        { "Floating Point Registers",   "fpu", k_num_fpr_registers_mips64, g_fp_regnums_mips64 }, +        { "MSA Registers",              "msa", k_num_msa_registers_mips64, g_msa_regnums_mips64 }, +    }; + +} // end of anonymous namespace + +NativeRegisterContextLinux* +NativeRegisterContextLinux::CreateHostNativeRegisterContextLinux(const ArchSpec& target_arch, +                                                                 NativeThreadProtocol &native_thread, +                                                                 uint32_t concrete_frame_idx) +{ +    return new NativeRegisterContextLinux_mips64(target_arch, native_thread, concrete_frame_idx); +} + +#define REG_CONTEXT_SIZE (GetRegisterInfoInterface ().GetGPRSize () + sizeof(FPR_linux_mips) + sizeof(MSA_linux_mips)) + +// ---------------------------------------------------------------------------- +// NativeRegisterContextLinux_mips64 members. +// ---------------------------------------------------------------------------- + +static RegisterInfoInterface* +CreateRegisterInfoInterface(const ArchSpec& target_arch) +{ +    if (HostInfo::GetArchitecture().GetAddressByteSize() == 4) +    { +        // 32-bit hosts run with a RegisterContextLinux_mips context. +        return new RegisterContextLinux_mips(target_arch, NativeRegisterContextLinux_mips64::IsMSAAvailable()); +    } +    else +    { +        assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) && +               "Register setting path assumes this is a 64-bit host"); +        // mips64 hosts know how to work with 64-bit and 32-bit EXEs using the mips64 register context. +        return new RegisterContextLinux_mips64 (target_arch, NativeRegisterContextLinux_mips64::IsMSAAvailable()); +    } +} + +NativeRegisterContextLinux_mips64::NativeRegisterContextLinux_mips64 (const ArchSpec& target_arch, +                                                                      NativeThreadProtocol &native_thread,  +                                                                      uint32_t concrete_frame_idx) : +    NativeRegisterContextLinux (native_thread, concrete_frame_idx, CreateRegisterInfoInterface(target_arch)) +{ +    switch (target_arch.GetMachine ()) +    { +        case llvm::Triple::mips: +        case llvm::Triple::mipsel: +            m_reg_info.num_registers        = k_num_registers_mips; +            m_reg_info.num_gpr_registers    = k_num_gpr_registers_mips; +            m_reg_info.num_fpr_registers    = k_num_fpr_registers_mips; +            m_reg_info.last_gpr             = k_last_gpr_mips; +            m_reg_info.first_fpr            = k_first_fpr_mips; +            m_reg_info.last_fpr             = k_last_fpr_mips; +            m_reg_info.first_msa            = k_first_msa_mips; +            m_reg_info.last_msa             = k_last_msa_mips; +            break; +        case llvm::Triple::mips64: +        case llvm::Triple::mips64el: +            m_reg_info.num_registers        = k_num_registers_mips64; +            m_reg_info.num_gpr_registers    = k_num_gpr_registers_mips64; +            m_reg_info.num_fpr_registers    = k_num_fpr_registers_mips64; +            m_reg_info.last_gpr             = k_last_gpr_mips64; +            m_reg_info.first_fpr            = k_first_fpr_mips64; +            m_reg_info.last_fpr             = k_last_fpr_mips64; +            m_reg_info.first_msa            = k_first_msa_mips64; +            m_reg_info.last_msa             = k_last_msa_mips64; +            break; +        default: +            assert(false && "Unhandled target architecture."); +            break; +    } + +    // Initialize m_iovec to point to the buffer and buffer size +    // using the conventions of Berkeley style UIO structures, as required +    // by PTRACE extensions. +    m_iovec.iov_base = &m_msa; +    m_iovec.iov_len = sizeof(MSA_linux_mips); + +    // init h/w watchpoint addr map +    for (int index = 0;index <= MAX_NUM_WP; index++) +        hw_addr_map[index] = LLDB_INVALID_ADDRESS; + +    ::memset(&m_gpr, 0, sizeof(GPR_linux_mips)); +    ::memset(&m_fpr, 0, sizeof(FPR_linux_mips)); +    ::memset(&m_msa, 0, sizeof(MSA_linux_mips)); +} + +uint32_t +NativeRegisterContextLinux_mips64::GetRegisterSetCount () const +{ +    return k_num_register_sets; +} + +lldb::addr_t +NativeRegisterContextLinux_mips64::GetPCfromBreakpointLocation (lldb::addr_t fail_value) +{ +    Error error; +    RegisterValue pc_value; +    lldb::addr_t pc = fail_value; +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS)); +     +    if (log) +        log->Printf ("NativeRegisterContextLinux_mips64::%s Reading PC from breakpoint location", __FUNCTION__); + +    // PC register is at index 34 of the register array +    const RegisterInfo *const pc_info_p = GetRegisterInfoAtIndex (gpr_pc_mips64); +         +    error = ReadRegister (pc_info_p, pc_value); +    if (error.Success ()) +    { +        pc = pc_value.GetAsUInt64 (); +         +        // CAUSE register is at index 37 of the register array +        const RegisterInfo *const cause_info_p = GetRegisterInfoAtIndex (gpr_cause_mips64); +        RegisterValue cause_value; + +        ReadRegister (cause_info_p, cause_value); + +        uint64_t cause = cause_value.GetAsUInt64 (); +         +        if (log) +            log->Printf ("NativeRegisterContextLinux_mips64::%s PC 0x%" PRIx64 " Cause 0x%" PRIx64, __FUNCTION__, pc, cause); + +        /* +         * The breakpoint might be in a delay slot. In this case PC points +         * to the delayed branch instruction rather then the instruction +         * in the delay slot. If the CAUSE.BD flag is set then adjust the  +         * PC based on the size of the branch instruction. +        */ +        if ((cause & (1 << 31)) != 0) +        { +            lldb::addr_t branch_delay = 0; +            branch_delay = 4;   // FIXME - Adjust according to size of branch instruction at PC +            pc = pc + branch_delay; +            pc_value.SetUInt64 (pc); +            WriteRegister (pc_info_p, pc_value); +             +            if (log) +                log->Printf ("NativeRegisterContextLinux_mips64::%s New PC 0x%" PRIx64, __FUNCTION__, pc); +        } +    } + +    return pc; +} + +const RegisterSet * +NativeRegisterContextLinux_mips64::GetRegisterSet (uint32_t set_index) const +{ +    if (set_index >= k_num_register_sets) +        return nullptr; + +    switch (GetRegisterInfoInterface ().GetTargetArchitecture ().GetMachine ()) +    { +        case llvm::Triple::mips64: +        case llvm::Triple::mips64el: +            return &g_reg_sets_mips64[set_index]; +        case llvm::Triple::mips: +        case llvm::Triple::mipsel: +            return &g_reg_sets_mips[set_index]; +        default: +            assert (false && "Unhandled target architecture."); +            return nullptr; +    } + +    return nullptr; +} + +lldb_private::Error +NativeRegisterContextLinux_mips64::ReadRegister (const RegisterInfo *reg_info, RegisterValue ®_value) +{ +    Error error; + +    if (!reg_info) +    { +        error.SetErrorString ("reg_info NULL"); +        return error; +    } + +    const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB]; +    if (reg == LLDB_INVALID_REGNUM) +    { +        // This is likely an internal register for lldb use only and should not be directly queried. +        error.SetErrorStringWithFormat ("register \"%s\" is an internal-only lldb register, cannot read directly", reg_info->name); +        return error; +    } + +    if (IsMSA(reg) && !IsMSAAvailable()) +    { +        error.SetErrorString ("MSA not available on this processor"); +        return error; +    } + +    if (IsMSA(reg) || IsFPR(reg)) +    { +        uint8_t *src; + +        error = ReadCP1(); + +        if (!error.Success()) +        { +            error.SetErrorString ("failed to read co-processor 1 register"); +            return error; +        } + +        if (IsFPR(reg)) +        { +            assert (reg_info->byte_offset < sizeof(UserArea)); +            src = (uint8_t *)&m_fpr + reg_info->byte_offset - (sizeof(m_gpr)); +        } +        else +        { +            assert (reg_info->byte_offset < sizeof(UserArea)); +            src = (uint8_t *)&m_msa + reg_info->byte_offset - (sizeof(m_gpr) + sizeof(m_fpr)); +        } +        switch (reg_info->byte_size) +        { +            case 4: +                reg_value.SetUInt32(*(uint32_t *)src); +                break; +            case 8: +                reg_value.SetUInt64(*(uint64_t *)src); +                break; +            case 16: +                reg_value.SetBytes((const void *)src, 16, GetByteOrder()); +                break; +            default: +                assert(false && "Unhandled data size."); +                error.SetErrorStringWithFormat ("unhandled byte size: %" PRIu32, reg_info->byte_size); +                break; +        } +    } +    else +    { +        error = ReadRegisterRaw(reg, reg_value); +    } + +    return error; +} + +lldb_private::Error +NativeRegisterContextLinux_mips64::WriteRegister (const RegisterInfo *reg_info, const RegisterValue ®_value) +{ +    Error error; + +    assert (reg_info && "reg_info is null"); + +    const uint32_t reg_index = reg_info->kinds[lldb::eRegisterKindLLDB]; + +    if (reg_index == LLDB_INVALID_REGNUM) +        return Error ("no lldb regnum for %s", reg_info && reg_info->name ? reg_info->name : "<unknown register>"); + +    if (IsMSA(reg_index) && !IsMSAAvailable()) +    { +        error.SetErrorString ("MSA not available on this processor"); +        return error; +    } + +    if (IsFPR(reg_index) || IsMSA(reg_index)) +    { +        uint8_t *dst; +        uint64_t *src; + +        // Initialise the FP and MSA buffers by reading all co-processor 1 registers +        ReadCP1(); + +        if (IsFPR(reg_index)) +        { +            assert (reg_info->byte_offset < sizeof(UserArea)); +            dst = (uint8_t *)&m_fpr + reg_info->byte_offset - (sizeof(m_gpr)); +        } +        else +        { +            assert (reg_info->byte_offset < sizeof(UserArea)); +            dst = (uint8_t *)&m_msa + reg_info->byte_offset - (sizeof(m_gpr) + sizeof(m_fpr)); +        } +        switch (reg_info->byte_size) +        { +            case 4: +                *(uint32_t *)dst = reg_value.GetAsUInt32(); +                break; +            case 8: +                *(uint64_t *)dst = reg_value.GetAsUInt64(); +                break; +            case 16: +                src = (uint64_t *)reg_value.GetBytes(); +                *(uint64_t *)dst = *src; +                *(uint64_t *)(dst + 8) = *(src + 1); +                break; +            default: +                assert(false && "Unhandled data size."); +                error.SetErrorStringWithFormat ("unhandled byte size: %" PRIu32, reg_info->byte_size); +                break; +        } +        error = WriteCP1(); +        if (!error.Success()) +        { +            error.SetErrorString ("failed to write co-processor 1 register"); +            return error; +        } +    } +    else +    { +        error = WriteRegisterRaw(reg_index, reg_value); +    } + +    return error; +} + +Error +NativeRegisterContextLinux_mips64::ReadAllRegisterValues (lldb::DataBufferSP &data_sp) +{ +    Error error; + +    data_sp.reset (new DataBufferHeap (REG_CONTEXT_SIZE, 0)); +    if (!data_sp) +    { +        error.SetErrorStringWithFormat ("failed to allocate DataBufferHeap instance of size %" PRIu64, REG_CONTEXT_SIZE); +        return error; +    } + +    error = ReadGPR(); +    if (!error.Success()) +    { +        error.SetErrorString ("ReadGPR() failed"); +        return error; +    } + +    error = ReadCP1(); +    if (!error.Success()) +    { +        error.SetErrorString ("ReadCP1() failed"); +        return error; +    } + +    uint8_t *dst = data_sp->GetBytes (); +    if (dst == nullptr) +    { +        error.SetErrorStringWithFormat ("DataBufferHeap instance of size %" PRIu64 " returned a null pointer", REG_CONTEXT_SIZE); +        return error; +    } + +    ::memcpy (dst, &m_gpr, GetRegisterInfoInterface ().GetGPRSize ()); +    dst += GetRegisterInfoInterface ().GetGPRSize (); + +    ::memcpy (dst, &m_fpr, GetFPRSize ()); +    dst += GetFPRSize (); + +    ::memcpy (dst, &m_msa, sizeof(MSA_linux_mips)); + +    return error; +} + +Error +NativeRegisterContextLinux_mips64::WriteAllRegisterValues (const lldb::DataBufferSP &data_sp) +{ +    Error error; + +    if (!data_sp) +    { +        error.SetErrorStringWithFormat ("NativeRegisterContextLinux_mips64::%s invalid data_sp provided", __FUNCTION__); +        return error; +    } + +    if (data_sp->GetByteSize () != REG_CONTEXT_SIZE) +    { +        error.SetErrorStringWithFormat ("NativeRegisterContextLinux_mips64::%s data_sp contained mismatched data size, expected %" PRIu64 ", actual %" PRIu64, __FUNCTION__, REG_CONTEXT_SIZE, data_sp->GetByteSize ()); +        return error; +    } + + +    uint8_t *src = data_sp->GetBytes (); +    if (src == nullptr) +    { +        error.SetErrorStringWithFormat ("NativeRegisterContextLinux_mips64::%s DataBuffer::GetBytes() returned a null pointer", __FUNCTION__); +        return error; +    } + +    ::memcpy (&m_gpr, src, GetRegisterInfoInterface ().GetGPRSize ()); +    src += GetRegisterInfoInterface ().GetGPRSize (); + +    ::memcpy (&m_fpr, src, GetFPRSize ()); +    src += GetFPRSize (); + +    ::memcpy (&m_msa, src, sizeof(MSA_linux_mips)); + +    error = WriteGPR(); +    if (!error.Success()) +    { +        error.SetErrorStringWithFormat ("NativeRegisterContextLinux_mips64::%s WriteGPR() failed", __FUNCTION__); +        return error; +    } + +    error = WriteCP1(); +    if (!error.Success()) +    { +        error.SetErrorStringWithFormat ("NativeRegisterContextLinux_mips64::%s WriteCP1() failed", __FUNCTION__); +        return error; +    } + +    return error; +} + +Error +NativeRegisterContextLinux_mips64::ReadCP1() +{ +    Error error; + +    uint8_t *src, *dst; + +    lldb::ByteOrder byte_order = GetByteOrder(); + +    uint32_t IsBigEndian = (byte_order == lldb::eByteOrderBig); + +    if (IsMSAAvailable()) +    { +        error = NativeRegisterContextLinux::ReadRegisterSet(&m_iovec, sizeof(MSA_linux_mips), NT_MIPS_MSA); +        src = (uint8_t *)&m_msa + (IsBigEndian * 8); +        dst = (uint8_t *)&m_fpr; +        for ( int i = 0; i < NUM_REGISTERS; i++) +        { +            // Copy fp values from msa buffer fetched via ptrace +            *(uint64_t *) dst = *(uint64_t *) src; +            src = src + 16; +            dst = dst + 8; +        } +        m_fpr.fir = m_msa.fir; +        m_fpr.fcsr = m_msa.fcsr; +        m_fpr.config5 = m_msa.config5; +    } +    else +    { +        error = NativeRegisterContextLinux::ReadFPR(); +    } + +    if (IsFR0() || IsFRE()) +    { +         src = (uint8_t *)&m_fpr + 4 + (IsBigEndian * 4); +         dst = (uint8_t *)&m_fpr + 8 + (IsBigEndian * 4); +         for (int i = 0; i < (NUM_REGISTERS / 2); i++) +         { +              // copy odd single from top of neighbouring even double +              *(uint32_t *) dst = *(uint32_t *) src; +              src = src + 16; +              dst = dst + 16; +         } +    } + +    return error; +} + +Error +NativeRegisterContextLinux_mips64::WriteCP1() +{ +    Error error; + +    uint8_t *src, *dst; + +    lldb::ByteOrder byte_order = GetByteOrder(); + +    uint32_t IsBigEndian = (byte_order == lldb::eByteOrderBig); + +    if (IsFR0() || IsFRE()) +    { +        src = (uint8_t *)&m_fpr + 8 + (IsBigEndian * 4); +        dst = (uint8_t *)&m_fpr + 4 + (IsBigEndian * 4); +        for (int i = 0; i < (NUM_REGISTERS / 2); i++) +        { +             // copy odd single to top of neighbouring even double +             *(uint32_t *) dst = *(uint32_t *) src; +             src = src + 16; +             dst = dst + 16; +        } +    } + +    if (IsMSAAvailable()) +    { +        dst = (uint8_t *)&m_msa + (IsBigEndian * 8); +        src = (uint8_t *)&m_fpr; +        for (int i = 0; i < NUM_REGISTERS; i++) +        { +            // Copy fp values to msa buffer for ptrace +            *(uint64_t *) dst = *(uint64_t *) src; +            dst = dst + 16; +            src = src + 8; +        } +        m_msa.fir = m_fpr.fir; +        m_msa.fcsr = m_fpr.fcsr; +        m_msa.config5 = m_fpr.config5; +        error = NativeRegisterContextLinux::WriteRegisterSet(&m_iovec, sizeof(MSA_linux_mips), NT_MIPS_MSA); +    } +    else +    { +        error = NativeRegisterContextLinux::WriteFPR(); +    } + +    return error; +} + +bool +NativeRegisterContextLinux_mips64::IsFR0() +{ +    const RegisterInfo *const reg_info_p = GetRegisterInfoAtIndex (gpr_sr_mips64); + +    RegisterValue reg_value; +    ReadRegister (reg_info_p, reg_value); + +    uint64_t value = reg_value.GetAsUInt64(); + +    return (!(value & SR_FR)); +} + +bool +NativeRegisterContextLinux_mips64::IsFRE() +{ +    const RegisterInfo *const reg_info_p = GetRegisterInfoAtIndex (gpr_config5_mips64); + +    RegisterValue reg_value; +    ReadRegister (reg_info_p, reg_value); + +    uint64_t config5 = reg_value.GetAsUInt64(); + +    return (config5 & CONFIG5_FRE); +} + +bool +NativeRegisterContextLinux_mips64::IsFPR(uint32_t reg_index) const +{ +    return (m_reg_info.first_fpr <= reg_index && reg_index <= m_reg_info.last_fpr); +} + +static uint32_t +GetWatchHi (struct pt_watch_regs *regs, uint32_t index) +{ +    Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); +    if (regs->style == pt_watch_style_mips32) +        return regs->mips32.watchhi[index]; +    else if (regs->style == pt_watch_style_mips64) +        return regs->mips64.watchhi[index]; +    if(log) +        log->Printf("Invalid watch register style"); +    return 0; +} + +static void +SetWatchHi (struct pt_watch_regs *regs, uint32_t index, uint16_t value) +{ +    Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); +    if (regs->style == pt_watch_style_mips32) +        regs->mips32.watchhi[index] = value; +    else if (regs->style == pt_watch_style_mips64) +        regs->mips64.watchhi[index] = value; +    if(log) +        log->Printf("Invalid watch register style"); +    return; +} + +static lldb::addr_t +GetWatchLo (struct pt_watch_regs *regs, uint32_t index) +{ +    Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); +    if (regs->style == pt_watch_style_mips32) +        return regs->mips32.watchlo[index]; +    else if (regs->style == pt_watch_style_mips64) +        return regs->mips64.watchlo[index]; +    if(log) +        log->Printf("Invalid watch register style"); +    return LLDB_INVALID_ADDRESS; +} + +static void +SetWatchLo (struct pt_watch_regs *regs, uint32_t index, uint64_t value) +{ +    Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); +    if (regs->style == pt_watch_style_mips32) +        regs->mips32.watchlo[index] = (uint32_t) value; +    else if (regs->style == pt_watch_style_mips64) +        regs->mips64.watchlo[index] = value; +    if(log) +        log->Printf("Invalid watch register style"); +    return; +} + +static uint32_t +GetIRWMask (struct pt_watch_regs *regs, uint32_t index) +{ +    Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); +    if (regs->style == pt_watch_style_mips32) +        return regs->mips32.watch_masks[index] & IRW; +    else if (regs->style == pt_watch_style_mips64) +        return regs->mips64.watch_masks[index] & IRW; +    if(log) +        log->Printf("Invalid watch register style"); +    return 0; +} + +static uint32_t +GetRegMask (struct pt_watch_regs *regs, uint32_t index) +{ +    Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); +    if (regs->style == pt_watch_style_mips32) +        return regs->mips32.watch_masks[index] & ~IRW; +    else if (regs->style == pt_watch_style_mips64) +        return regs->mips64.watch_masks[index] & ~IRW; +    if(log) +        log->Printf("Invalid watch register style"); +    return 0; +} + +static lldb::addr_t +GetRangeMask (lldb::addr_t mask) +{ +    lldb::addr_t mask_bit = 1; +    while (mask_bit < mask) +    { +        mask = mask | mask_bit; +        mask_bit <<= 1; +    } +    return mask; +} + +static int +GetVacantWatchIndex (struct pt_watch_regs *regs, lldb::addr_t addr, uint32_t size, uint32_t irw, uint32_t num_valid) +{ +    lldb::addr_t last_byte = addr + size - 1; +    lldb::addr_t mask = GetRangeMask (addr ^ last_byte) | IRW; +    lldb::addr_t base_addr = addr & ~mask; + +    // Check if this address is already watched by previous watch points. +    lldb::addr_t lo; +    uint16_t hi; +    uint32_t vacant_watches = 0; +    for (uint32_t index = 0; index < num_valid; index++) +    { +        lo = GetWatchLo (regs, index); +        if (lo != 0 && irw == ((uint32_t) lo & irw)) +        { +            hi = GetWatchHi (regs, index) | IRW; +            lo &= ~(lldb::addr_t) hi; +            if (addr >= lo && last_byte <= (lo + hi)) +                return index; +        } +        else +            vacant_watches++; +    } + +    // Now try to find a vacant index +    if(vacant_watches > 0) +    { +        vacant_watches = 0; +        for (uint32_t index = 0; index < num_valid; index++) +        { +            lo = GetWatchLo (regs, index); +            if (lo == 0 +              && irw == (GetIRWMask (regs, index) & irw)) +            { +                if (mask <= (GetRegMask (regs, index) | IRW)) +                { +                    // It fits, we can use it.  +                    SetWatchLo (regs, index, base_addr | irw); +                    SetWatchHi (regs, index, mask & ~IRW); +                    return index; +                } +                else +                { +                    // It doesn't fit, but has the proper IRW capabilities +                    vacant_watches++; +                } +            } +        } + +        if (vacant_watches > 1) +        { +            // Split this watchpoint accross several registers +            struct pt_watch_regs regs_copy; +            regs_copy = *regs; +            lldb::addr_t break_addr; +            uint32_t segment_size; +            for (uint32_t index = 0; index < num_valid; index++) +            { +                lo = GetWatchLo (®s_copy, index); +                hi = GetRegMask (®s_copy, index) | IRW; +                if (lo == 0 && irw == (hi & irw)) +                { +                    lo = addr & ~(lldb::addr_t) hi; +                    break_addr = lo + hi + 1; +                    if (break_addr >= addr + size) +                        segment_size = size; +                    else +                        segment_size = break_addr - addr; +                    mask = GetRangeMask (addr ^ (addr + segment_size - 1)); +                    SetWatchLo (®s_copy, index, (addr & ~mask) | irw); +                    SetWatchHi (®s_copy, index, mask & ~IRW); +                    if (break_addr >= addr + size) +                    { +                        *regs = regs_copy; +                        return index; +                    } +                    size = addr + size - break_addr; +                    addr = break_addr; +                } +            } +        } +    } +    return LLDB_INVALID_INDEX32; +} + +bool +NativeRegisterContextLinux_mips64::IsMSA(uint32_t reg_index) const +{ +    return (m_reg_info.first_msa <= reg_index && reg_index <= m_reg_info.last_msa); +} + +bool +NativeRegisterContextLinux_mips64::IsMSAAvailable() +{ +    MSA_linux_mips msa_buf; +    unsigned int regset = NT_MIPS_MSA; + +    Error error = NativeProcessLinux::PtraceWrapper(PTRACE_GETREGSET, Host::GetCurrentProcessID(), static_cast<void *>(®set), &msa_buf, sizeof(MSA_linux_mips)); + +    if (error.Success() && msa_buf.mir) +    { +        return true; +    } + +    return false; +} + +Error +NativeRegisterContextLinux_mips64::IsWatchpointHit (uint32_t wp_index, bool &is_hit) +{ +    if (wp_index >= NumSupportedHardwareWatchpoints()) +        return Error("Watchpoint index out of range"); + +    // reading the current state of watch regs +    struct pt_watch_regs watch_readback; +    Error error =  DoReadWatchPointRegisterValue(m_thread.GetID(), static_cast<void *>(&watch_readback)); + +    if (GetWatchHi (&watch_readback, wp_index) & (IRW)) +    { +        // clear hit flag in watchhi  +        SetWatchHi (&watch_readback, wp_index, (GetWatchHi (&watch_readback, wp_index) & ~(IRW))); +        DoWriteWatchPointRegisterValue(m_thread.GetID(), static_cast<void *>(&watch_readback)); +      +        is_hit = true; +        return error; +    } +    is_hit = false; +    return error; +} + +Error +NativeRegisterContextLinux_mips64::GetWatchpointHitIndex(uint32_t &wp_index, lldb::addr_t trap_addr) { +    uint32_t num_hw_wps = NumSupportedHardwareWatchpoints(); +    for (wp_index = 0; wp_index < num_hw_wps; ++wp_index) +    { +        bool is_hit; +        Error error = IsWatchpointHit(wp_index, is_hit); +        if (error.Fail()) { +            wp_index = LLDB_INVALID_INDEX32; +        } else if (is_hit) { +            return error; +        } +    } +    wp_index = LLDB_INVALID_INDEX32; +    return Error(); +} + +Error +NativeRegisterContextLinux_mips64::IsWatchpointVacant (uint32_t wp_index, bool &is_vacant) +{ +    is_vacant = false; +    return Error("MIPS TODO: NativeRegisterContextLinux_mips64::IsWatchpointVacant not implemented"); +} + +bool +NativeRegisterContextLinux_mips64::ClearHardwareWatchpoint(uint32_t wp_index) +{ +    if (wp_index >= NumSupportedHardwareWatchpoints()) +        return false; + +    struct pt_watch_regs regs; +    // First reading the current state of watch regs +    DoReadWatchPointRegisterValue(m_thread.GetID(), static_cast<void*>(®s)); + +    if (regs.style == pt_watch_style_mips32) +    { +        regs.mips32.watchlo[wp_index] = default_watch_regs.mips32.watchlo[wp_index]; +        regs.mips32.watchhi[wp_index] = default_watch_regs.mips32.watchhi[wp_index]; +        regs.mips32.watch_masks[wp_index] = default_watch_regs.mips32.watch_masks[wp_index]; +    } +    else // pt_watch_style_mips64 +    { +        regs.mips64.watchlo[wp_index] = default_watch_regs.mips64.watchlo[wp_index]; +        regs.mips64.watchhi[wp_index] = default_watch_regs.mips64.watchhi[wp_index]; +        regs.mips64.watch_masks[wp_index] = default_watch_regs.mips64.watch_masks[wp_index]; +    } + +    Error error = DoWriteWatchPointRegisterValue(m_thread.GetID(), static_cast<void *>(®s)); +    if(!error.Fail()) +    { +        hw_addr_map[wp_index] = LLDB_INVALID_ADDRESS; +        return true; +    } +    return false; +} + +Error +NativeRegisterContextLinux_mips64::ClearAllHardwareWatchpoints() +{ +    return DoWriteWatchPointRegisterValue(m_thread.GetID(), static_cast<void *>(&default_watch_regs)); +} + +Error +NativeRegisterContextLinux_mips64::SetHardwareWatchpointWithIndex ( +        lldb::addr_t addr, size_t size, uint32_t watch_flags, uint32_t wp_index)  +{ +    Error error; +    error.SetErrorString ("MIPS TODO: NativeRegisterContextLinux_mips64::SetHardwareWatchpointWithIndex not implemented"); +    return error; +} + +uint32_t +NativeRegisterContextLinux_mips64::SetHardwareWatchpoint ( +        lldb::addr_t addr, size_t size, uint32_t watch_flags) +{ +    struct pt_watch_regs regs; + +    // First reading the current state of watch regs +    DoReadWatchPointRegisterValue(m_thread.GetID(), static_cast<void *>(®s)); + +    // Try if a new watch point fits in this state +    int index = GetVacantWatchIndex (®s, addr, size, watch_flags, NumSupportedHardwareWatchpoints()); + +    // New watchpoint doesn't fit +    if (index == LLDB_INVALID_INDEX32) +    return LLDB_INVALID_INDEX32; + + +    // It fits, so we go ahead with updating the state of watch regs  +    DoWriteWatchPointRegisterValue(m_thread.GetID(), static_cast<void *>(®s)); + +    // Storing exact address   +    hw_addr_map[index] = addr;  +    return index; +} + +lldb::addr_t +NativeRegisterContextLinux_mips64::GetWatchpointAddress (uint32_t wp_index) +{ +    if (wp_index >= NumSupportedHardwareWatchpoints()) +        return LLDB_INVALID_ADDRESS; + +    return hw_addr_map[wp_index]; +} + +struct EmulatorBaton +{ +    lldb::addr_t m_watch_hit_addr; +    NativeProcessLinux* m_process; +    NativeRegisterContext* m_reg_context; + +    EmulatorBaton(NativeProcessLinux* process, NativeRegisterContext* reg_context) : +            m_watch_hit_addr(LLDB_INVALID_ADDRESS),  +            m_process(process), +            m_reg_context(reg_context)  +            {} +}; + +static size_t +ReadMemoryCallback (EmulateInstruction *instruction, void *baton, +                    const EmulateInstruction::Context &context, lldb::addr_t addr,  +                    void *dst, size_t length) +{ +    size_t bytes_read; +    EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); +    emulator_baton->m_process->ReadMemory(addr, dst, length, bytes_read); +    return bytes_read; +} + +static size_t +WriteMemoryCallback (EmulateInstruction *instruction, void *baton, +                     const EmulateInstruction::Context &context,  +                     lldb::addr_t addr, const void *dst, size_t length) +{ +    return length; +} + +static bool +ReadRegisterCallback (EmulateInstruction *instruction, void *baton, +                      const RegisterInfo *reg_info, RegisterValue ®_value) +{ +    EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); + +    const RegisterInfo* full_reg_info = emulator_baton->m_reg_context->GetRegisterInfo( +            lldb::eRegisterKindDWARF, reg_info->kinds[lldb::eRegisterKindDWARF]); + +    Error error = emulator_baton->m_reg_context->ReadRegister(full_reg_info, reg_value); +    if (error.Success()) +        return true; + +    return false; +} + +static bool +WriteRegisterCallback (EmulateInstruction *instruction, void *baton, +                       const EmulateInstruction::Context &context, +                       const RegisterInfo *reg_info, const RegisterValue ®_value) +{ +    if (reg_info->kinds[lldb::eRegisterKindDWARF] == dwarf_bad_mips64) +    { +        EmulatorBaton* emulator_baton = static_cast<EmulatorBaton*>(baton); +        emulator_baton->m_watch_hit_addr = reg_value.GetAsUInt64 (); +    } + +    return true; +} + +/* + * MIPS Linux kernel returns a masked address (last 3bits are masked) + * when a HW watchpoint is hit. However user may not have set a watchpoint + * on this address. Emulate instruction at PC and find the base address of + * the load/store instruction. This will give the exact address used to + * read/write the variable. Send this exact address to client so that + * it can decide to stop or continue the thread. +*/ +lldb::addr_t +NativeRegisterContextLinux_mips64::GetWatchpointHitAddress (uint32_t wp_index) +{ +    if (wp_index >= NumSupportedHardwareWatchpoints()) +        return LLDB_INVALID_ADDRESS; + +    lldb_private::ArchSpec arch; +    arch = GetRegisterInfoInterface().GetTargetArchitecture(); +    std::unique_ptr<EmulateInstruction> emulator_ap( +        EmulateInstruction::FindPlugin(arch, lldb_private::eInstructionTypeAny, nullptr)); + +    if (emulator_ap == nullptr) +        return LLDB_INVALID_ADDRESS; +     +    EmulatorBaton baton(static_cast<NativeProcessLinux*>(m_thread.GetProcess().get()), this); +    emulator_ap->SetBaton (&baton); +    emulator_ap->SetReadMemCallback (&ReadMemoryCallback); +    emulator_ap->SetReadRegCallback (&ReadRegisterCallback); +    emulator_ap->SetWriteMemCallback (&WriteMemoryCallback); +    emulator_ap->SetWriteRegCallback (&WriteRegisterCallback); + +    if (!emulator_ap->ReadInstruction()) +        return LLDB_INVALID_ADDRESS; + +    if (emulator_ap->EvaluateInstruction(lldb::eEmulateInstructionOptionNone)) +        return baton.m_watch_hit_addr; + +    return LLDB_INVALID_ADDRESS; +} + +uint32_t +NativeRegisterContextLinux_mips64::NumSupportedHardwareWatchpoints () +{ +    Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); +    struct pt_watch_regs regs; +    static int num_valid = 0; +    if (!num_valid) +    { +        DoReadWatchPointRegisterValue(m_thread.GetID(), static_cast<void *>(®s)); +        default_watch_regs = regs; // Keeping default watch regs values for future use +        switch (regs.style) +        { +            case pt_watch_style_mips32: +                num_valid = regs.mips32.num_valid; // Using num_valid as cache +                return num_valid; +            case pt_watch_style_mips64: +                num_valid = regs.mips64.num_valid; +                return num_valid; +            default: +                if(log) +                    log->Printf("NativeRegisterContextLinux_mips64::%s Error: Unrecognized watch register style", __FUNCTION__); +        } +        return 0; +    } +    return num_valid; +} +Error +NativeRegisterContextLinux_mips64::DoReadRegisterValue(uint32_t offset, +                                                       const char* reg_name, +                                                       uint32_t size, +                                                       RegisterValue &value) +{ +    GPR_linux_mips regs; +    ::memset(®s, 0, sizeof(GPR_linux_mips)); + +    // Clear all bits in RegisterValue before writing actual value read from ptrace to avoid garbage value in 32-bit MSB  +    value.SetBytes((void *)(((unsigned char *)®s) + offset), 8, GetByteOrder()); +    Error error = NativeProcessLinux::PtraceWrapper(PTRACE_GETREGS, m_thread.GetID(), NULL, ®s, sizeof regs); +    if (error.Success()) +    { +        lldb_private::ArchSpec arch; +        if (m_thread.GetProcess()->GetArchitecture(arch)) +            value.SetBytes((void *)(((unsigned char *)®s) + offset + 4 * (arch.GetMachine() == llvm::Triple::mips)), arch.GetFlags() & lldb_private::ArchSpec::eMIPSABI_O32 ? 4 : 8, arch.GetByteOrder()); +        else +            error.SetErrorString("failed to get architecture"); +    } +    return error; +} + +Error +NativeRegisterContextLinux_mips64::DoWriteRegisterValue(uint32_t offset, +                                                        const char* reg_name, +                                                        const RegisterValue &value) +{ +    GPR_linux_mips regs; +    Error error = NativeProcessLinux::PtraceWrapper(PTRACE_GETREGS, m_thread.GetID(), NULL, ®s, sizeof regs); +    if (error.Success()) +    { +        lldb_private::ArchSpec arch; +        if (m_thread.GetProcess()->GetArchitecture(arch)) +        { +            ::memcpy((void *)(((unsigned char *)(®s)) + offset), value.GetBytes(), arch.GetFlags() & lldb_private::ArchSpec::eMIPSABI_O32 ? 4 : 8); +            error = NativeProcessLinux::PtraceWrapper(PTRACE_SETREGS, m_thread.GetID(), NULL, ®s, sizeof regs); +        } +        else +            error.SetErrorString("failed to get architecture"); +    } +    return error; +} + +Error +NativeRegisterContextLinux_mips64::DoReadWatchPointRegisterValue(lldb::tid_t tid, void* watch_readback) +{ +    return NativeProcessLinux::PtraceWrapper( PTRACE_GET_WATCH_REGS, m_thread.GetID(), watch_readback); +} + +Error +NativeRegisterContextLinux_mips64::DoWriteWatchPointRegisterValue(lldb::tid_t tid, void* watch_reg_value) +{ +    return NativeProcessLinux::PtraceWrapper(PTRACE_SET_WATCH_REGS, m_thread.GetID(), watch_reg_value); +} + +#endif // defined (__mips__) diff --git a/source/Plugins/Process/Linux/NativeRegisterContextLinux_mips64.h b/source/Plugins/Process/Linux/NativeRegisterContextLinux_mips64.h new file mode 100644 index 000000000000..9368645116e9 --- /dev/null +++ b/source/Plugins/Process/Linux/NativeRegisterContextLinux_mips64.h @@ -0,0 +1,167 @@ +//===-- NativeRegisterContextLinux_mips64.h ---------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#if defined (__mips__) + +#ifndef lldb_NativeRegisterContextLinux_mips64_h +#define lldb_NativeRegisterContextLinux_mips64_h + +#include "Plugins/Process/Linux/NativeRegisterContextLinux.h" +#include "Plugins/Process/Utility/RegisterContext_mips.h" +#include "Plugins/Process/Utility/lldb-mips-linux-register-enums.h" + +#define MAX_NUM_WP 8 + +namespace lldb_private { +namespace process_linux { + +    class NativeProcessLinux; + +    class NativeRegisterContextLinux_mips64 : public NativeRegisterContextLinux +    { +    public: +        NativeRegisterContextLinux_mips64 (const ArchSpec& target_arch, +                                           NativeThreadProtocol &native_thread,  +                                           uint32_t concrete_frame_idx); + +        uint32_t +        GetRegisterSetCount () const override; + +        lldb::addr_t +        GetPCfromBreakpointLocation (lldb::addr_t fail_value = LLDB_INVALID_ADDRESS) override; + +        lldb::addr_t +        GetWatchpointHitAddress (uint32_t wp_index) override; + +        const RegisterSet * +        GetRegisterSet (uint32_t set_index) const override; + +        Error +        ReadRegister (const RegisterInfo *reg_info, RegisterValue ®_value) override; + +        Error +        WriteRegister (const RegisterInfo *reg_info, const RegisterValue ®_value) override; + +        Error +        ReadAllRegisterValues (lldb::DataBufferSP &data_sp) override; + +        Error +        WriteAllRegisterValues (const lldb::DataBufferSP &data_sp) override; + +        Error +        ReadCP1(); + +        Error +        WriteCP1(); + +        Error +        IsWatchpointHit (uint32_t wp_index, bool &is_hit) override; + +        Error +        GetWatchpointHitIndex(uint32_t &wp_index, lldb::addr_t trap_addr) override; + +        Error +        IsWatchpointVacant (uint32_t wp_index, bool &is_vacant) override; + +        bool +        ClearHardwareWatchpoint (uint32_t wp_index) override; + +        Error +        ClearAllHardwareWatchpoints () override; + +        Error +        SetHardwareWatchpointWithIndex (lldb::addr_t addr, size_t size, +                uint32_t watch_flags, uint32_t wp_index); + +        uint32_t +        SetHardwareWatchpoint (lldb::addr_t addr, size_t size, +                uint32_t watch_flags) override; + +        lldb::addr_t +        GetWatchpointAddress (uint32_t wp_index) override; + +        uint32_t +        NumSupportedHardwareWatchpoints () override; + +        static bool +        IsMSAAvailable(); + +    protected: +        Error +        DoReadRegisterValue(uint32_t offset, +                            const char* reg_name, +                            uint32_t size, +                            RegisterValue &value) override; + +        Error +        DoWriteRegisterValue(uint32_t offset, +                             const char* reg_name, +                             const RegisterValue &value) override; + +        Error +        DoReadWatchPointRegisterValue(lldb::tid_t tid, void* watch_readback); + +        Error +        DoWriteWatchPointRegisterValue(lldb::tid_t tid, void* watch_readback); + +        bool +        IsFR0(); + +        bool +        IsFRE(); + +        bool +        IsFPR(uint32_t reg_index) const; + +        bool +        IsMSA(uint32_t reg_index) const; + +        void* +        GetGPRBuffer() override { return &m_gpr; } + +        void* +        GetFPRBuffer() override { return &m_fpr; } + +        size_t +        GetFPRSize() override { return sizeof(FPR_linux_mips); } + +    private: +        // Info about register ranges. +        struct RegInfo +        { +            uint32_t num_registers; +            uint32_t num_gpr_registers; +            uint32_t num_fpr_registers; + +            uint32_t last_gpr; +            uint32_t first_fpr; +            uint32_t last_fpr; +            uint32_t first_msa; +            uint32_t last_msa; +        }; + +        RegInfo m_reg_info; + +        GPR_linux_mips m_gpr; + +        FPR_linux_mips m_fpr; + +        MSA_linux_mips m_msa; + +        lldb::addr_t hw_addr_map[MAX_NUM_WP]; + +        IOVEC_mips m_iovec; +    }; + +} // namespace process_linux +} // namespace lldb_private + +#endif // #ifndef lldb_NativeRegisterContextLinux_mips64_h + +#endif // defined (__mips__) diff --git a/source/Plugins/Process/Linux/NativeRegisterContextLinux_x86_64.cpp b/source/Plugins/Process/Linux/NativeRegisterContextLinux_x86_64.cpp new file mode 100755 index 000000000000..2080d2ede372 --- /dev/null +++ b/source/Plugins/Process/Linux/NativeRegisterContextLinux_x86_64.cpp @@ -0,0 +1,1239 @@ +//===-- NativeRegisterContextLinux_x86_64.cpp ---------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#if defined(__i386__) || defined(__x86_64__) + +#include "NativeRegisterContextLinux_x86_64.h" + +#include "lldb/Core/Log.h" +#include "lldb/Core/DataBufferHeap.h" +#include "lldb/Core/Error.h" +#include "lldb/Core/RegisterValue.h" +#include "lldb/Host/HostInfo.h" + +#include "Plugins/Process/Utility/RegisterContextLinux_i386.h" +#include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h" + +using namespace lldb_private; +using namespace lldb_private::process_linux; + +// ---------------------------------------------------------------------------- +// Private namespace. +// ---------------------------------------------------------------------------- + +namespace +{ +    // x86 32-bit general purpose registers. +    const uint32_t +    g_gpr_regnums_i386[] = +    { +        lldb_eax_i386, +        lldb_ebx_i386, +        lldb_ecx_i386, +        lldb_edx_i386, +        lldb_edi_i386, +        lldb_esi_i386, +        lldb_ebp_i386, +        lldb_esp_i386, +        lldb_eip_i386, +        lldb_eflags_i386, +        lldb_cs_i386, +        lldb_fs_i386, +        lldb_gs_i386, +        lldb_ss_i386, +        lldb_ds_i386, +        lldb_es_i386, +        lldb_ax_i386, +        lldb_bx_i386, +        lldb_cx_i386, +        lldb_dx_i386, +        lldb_di_i386, +        lldb_si_i386, +        lldb_bp_i386, +        lldb_sp_i386, +        lldb_ah_i386, +        lldb_bh_i386, +        lldb_ch_i386, +        lldb_dh_i386, +        lldb_al_i386, +        lldb_bl_i386, +        lldb_cl_i386, +        lldb_dl_i386, +        LLDB_INVALID_REGNUM // register sets need to end with this flag +    }; +    static_assert((sizeof(g_gpr_regnums_i386) / sizeof(g_gpr_regnums_i386[0])) - 1 == k_num_gpr_registers_i386, +                  "g_gpr_regnums_i386 has wrong number of register infos"); + +    // x86 32-bit floating point registers. +    const uint32_t +    g_fpu_regnums_i386[] = +    { +        lldb_fctrl_i386, +        lldb_fstat_i386, +        lldb_ftag_i386, +        lldb_fop_i386, +        lldb_fiseg_i386, +        lldb_fioff_i386, +        lldb_foseg_i386, +        lldb_fooff_i386, +        lldb_mxcsr_i386, +        lldb_mxcsrmask_i386, +        lldb_st0_i386, +        lldb_st1_i386, +        lldb_st2_i386, +        lldb_st3_i386, +        lldb_st4_i386, +        lldb_st5_i386, +        lldb_st6_i386, +        lldb_st7_i386, +        lldb_mm0_i386, +        lldb_mm1_i386, +        lldb_mm2_i386, +        lldb_mm3_i386, +        lldb_mm4_i386, +        lldb_mm5_i386, +        lldb_mm6_i386, +        lldb_mm7_i386, +        lldb_xmm0_i386, +        lldb_xmm1_i386, +        lldb_xmm2_i386, +        lldb_xmm3_i386, +        lldb_xmm4_i386, +        lldb_xmm5_i386, +        lldb_xmm6_i386, +        lldb_xmm7_i386, +        LLDB_INVALID_REGNUM // register sets need to end with this flag +    }; +    static_assert((sizeof(g_fpu_regnums_i386) / sizeof(g_fpu_regnums_i386[0])) - 1 == k_num_fpr_registers_i386, +                  "g_fpu_regnums_i386 has wrong number of register infos"); + +    // x86 32-bit AVX registers. +    const uint32_t +    g_avx_regnums_i386[] = +    { +        lldb_ymm0_i386, +        lldb_ymm1_i386, +        lldb_ymm2_i386, +        lldb_ymm3_i386, +        lldb_ymm4_i386, +        lldb_ymm5_i386, +        lldb_ymm6_i386, +        lldb_ymm7_i386, +        LLDB_INVALID_REGNUM // register sets need to end with this flag +    }; +    static_assert((sizeof(g_avx_regnums_i386) / sizeof(g_avx_regnums_i386[0])) - 1 == k_num_avx_registers_i386, +                  " g_avx_regnums_i386 has wrong number of register infos"); + +    // x86 64-bit general purpose registers. +    static const +    uint32_t g_gpr_regnums_x86_64[] = +    { +        lldb_rax_x86_64, +        lldb_rbx_x86_64, +        lldb_rcx_x86_64, +        lldb_rdx_x86_64, +        lldb_rdi_x86_64, +        lldb_rsi_x86_64, +        lldb_rbp_x86_64, +        lldb_rsp_x86_64, +        lldb_r8_x86_64, +        lldb_r9_x86_64, +        lldb_r10_x86_64, +        lldb_r11_x86_64, +        lldb_r12_x86_64, +        lldb_r13_x86_64, +        lldb_r14_x86_64, +        lldb_r15_x86_64, +        lldb_rip_x86_64, +        lldb_rflags_x86_64, +        lldb_cs_x86_64, +        lldb_fs_x86_64, +        lldb_gs_x86_64, +        lldb_ss_x86_64, +        lldb_ds_x86_64, +        lldb_es_x86_64, +        lldb_eax_x86_64, +        lldb_ebx_x86_64, +        lldb_ecx_x86_64, +        lldb_edx_x86_64, +        lldb_edi_x86_64, +        lldb_esi_x86_64, +        lldb_ebp_x86_64, +        lldb_esp_x86_64, +        lldb_r8d_x86_64,    // Low 32 bits or r8 +        lldb_r9d_x86_64,    // Low 32 bits or r9 +        lldb_r10d_x86_64,   // Low 32 bits or r10 +        lldb_r11d_x86_64,   // Low 32 bits or r11 +        lldb_r12d_x86_64,   // Low 32 bits or r12 +        lldb_r13d_x86_64,   // Low 32 bits or r13 +        lldb_r14d_x86_64,   // Low 32 bits or r14 +        lldb_r15d_x86_64,   // Low 32 bits or r15 +        lldb_ax_x86_64, +        lldb_bx_x86_64, +        lldb_cx_x86_64, +        lldb_dx_x86_64, +        lldb_di_x86_64, +        lldb_si_x86_64, +        lldb_bp_x86_64, +        lldb_sp_x86_64, +        lldb_r8w_x86_64,    // Low 16 bits or r8 +        lldb_r9w_x86_64,    // Low 16 bits or r9 +        lldb_r10w_x86_64,   // Low 16 bits or r10 +        lldb_r11w_x86_64,   // Low 16 bits or r11 +        lldb_r12w_x86_64,   // Low 16 bits or r12 +        lldb_r13w_x86_64,   // Low 16 bits or r13 +        lldb_r14w_x86_64,   // Low 16 bits or r14 +        lldb_r15w_x86_64,   // Low 16 bits or r15 +        lldb_ah_x86_64, +        lldb_bh_x86_64, +        lldb_ch_x86_64, +        lldb_dh_x86_64, +        lldb_al_x86_64, +        lldb_bl_x86_64, +        lldb_cl_x86_64, +        lldb_dl_x86_64, +        lldb_dil_x86_64, +        lldb_sil_x86_64, +        lldb_bpl_x86_64, +        lldb_spl_x86_64, +        lldb_r8l_x86_64,    // Low 8 bits or r8 +        lldb_r9l_x86_64,    // Low 8 bits or r9 +        lldb_r10l_x86_64,   // Low 8 bits or r10 +        lldb_r11l_x86_64,   // Low 8 bits or r11 +        lldb_r12l_x86_64,   // Low 8 bits or r12 +        lldb_r13l_x86_64,   // Low 8 bits or r13 +        lldb_r14l_x86_64,   // Low 8 bits or r14 +        lldb_r15l_x86_64,   // Low 8 bits or r15 +        LLDB_INVALID_REGNUM // register sets need to end with this flag +    }; +    static_assert((sizeof(g_gpr_regnums_x86_64) / sizeof(g_gpr_regnums_x86_64[0])) - 1 == k_num_gpr_registers_x86_64, +                  "g_gpr_regnums_x86_64 has wrong number of register infos"); + +    // x86 64-bit floating point registers. +    static const uint32_t +    g_fpu_regnums_x86_64[] = +    { +        lldb_fctrl_x86_64, +        lldb_fstat_x86_64, +        lldb_ftag_x86_64, +        lldb_fop_x86_64, +        lldb_fiseg_x86_64, +        lldb_fioff_x86_64, +        lldb_foseg_x86_64, +        lldb_fooff_x86_64, +        lldb_mxcsr_x86_64, +        lldb_mxcsrmask_x86_64, +        lldb_st0_x86_64, +        lldb_st1_x86_64, +        lldb_st2_x86_64, +        lldb_st3_x86_64, +        lldb_st4_x86_64, +        lldb_st5_x86_64, +        lldb_st6_x86_64, +        lldb_st7_x86_64, +        lldb_mm0_x86_64, +        lldb_mm1_x86_64, +        lldb_mm2_x86_64, +        lldb_mm3_x86_64, +        lldb_mm4_x86_64, +        lldb_mm5_x86_64, +        lldb_mm6_x86_64, +        lldb_mm7_x86_64, +        lldb_xmm0_x86_64, +        lldb_xmm1_x86_64, +        lldb_xmm2_x86_64, +        lldb_xmm3_x86_64, +        lldb_xmm4_x86_64, +        lldb_xmm5_x86_64, +        lldb_xmm6_x86_64, +        lldb_xmm7_x86_64, +        lldb_xmm8_x86_64, +        lldb_xmm9_x86_64, +        lldb_xmm10_x86_64, +        lldb_xmm11_x86_64, +        lldb_xmm12_x86_64, +        lldb_xmm13_x86_64, +        lldb_xmm14_x86_64, +        lldb_xmm15_x86_64, +        LLDB_INVALID_REGNUM // register sets need to end with this flag +    }; +    static_assert((sizeof(g_fpu_regnums_x86_64) / sizeof(g_fpu_regnums_x86_64[0])) - 1 == k_num_fpr_registers_x86_64, +                  "g_fpu_regnums_x86_64 has wrong number of register infos"); + +    // x86 64-bit AVX registers. +    static const uint32_t +    g_avx_regnums_x86_64[] = +    { +        lldb_ymm0_x86_64, +        lldb_ymm1_x86_64, +        lldb_ymm2_x86_64, +        lldb_ymm3_x86_64, +        lldb_ymm4_x86_64, +        lldb_ymm5_x86_64, +        lldb_ymm6_x86_64, +        lldb_ymm7_x86_64, +        lldb_ymm8_x86_64, +        lldb_ymm9_x86_64, +        lldb_ymm10_x86_64, +        lldb_ymm11_x86_64, +        lldb_ymm12_x86_64, +        lldb_ymm13_x86_64, +        lldb_ymm14_x86_64, +        lldb_ymm15_x86_64, +        LLDB_INVALID_REGNUM // register sets need to end with this flag +    }; +    static_assert((sizeof(g_avx_regnums_x86_64) / sizeof(g_avx_regnums_x86_64[0])) - 1 == k_num_avx_registers_x86_64, +                  "g_avx_regnums_x86_64 has wrong number of register infos"); + +    // Number of register sets provided by this context. +    enum +    { +        k_num_extended_register_sets = 1, +        k_num_register_sets = 3 +    }; + +    // Register sets for x86 32-bit. +    static const RegisterSet +    g_reg_sets_i386[k_num_register_sets] = +    { +        { "General Purpose Registers",  "gpr", k_num_gpr_registers_i386, g_gpr_regnums_i386 }, +        { "Floating Point Registers",   "fpu", k_num_fpr_registers_i386, g_fpu_regnums_i386 }, +        { "Advanced Vector Extensions", "avx", k_num_avx_registers_i386, g_avx_regnums_i386 } +    }; + +    // Register sets for x86 64-bit. +    static const RegisterSet +    g_reg_sets_x86_64[k_num_register_sets] = +    { +        { "General Purpose Registers",  "gpr", k_num_gpr_registers_x86_64, g_gpr_regnums_x86_64 }, +        { "Floating Point Registers",   "fpu", k_num_fpr_registers_x86_64, g_fpu_regnums_x86_64 }, +        { "Advanced Vector Extensions", "avx", k_num_avx_registers_x86_64, g_avx_regnums_x86_64 } +    }; +} + +#define REG_CONTEXT_SIZE (GetRegisterInfoInterface ().GetGPRSize () + sizeof(FPR)) + +// ---------------------------------------------------------------------------- +// Required ptrace defines. +// ---------------------------------------------------------------------------- + +// Support ptrace extensions even when compiled without required kernel support +#ifndef NT_X86_XSTATE +#define NT_X86_XSTATE 0x202 +#endif +#ifndef NT_PRXFPREG +#define NT_PRXFPREG 0x46e62b7f +#endif + +NativeRegisterContextLinux* +NativeRegisterContextLinux::CreateHostNativeRegisterContextLinux(const ArchSpec& target_arch, +                                                                 NativeThreadProtocol &native_thread, +                                                                 uint32_t concrete_frame_idx) +{ +    return new NativeRegisterContextLinux_x86_64(target_arch, native_thread, concrete_frame_idx); +} + +// ---------------------------------------------------------------------------- +// NativeRegisterContextLinux_x86_64 members. +// ---------------------------------------------------------------------------- + +static RegisterInfoInterface* +CreateRegisterInfoInterface(const ArchSpec& target_arch) +{ +    if (HostInfo::GetArchitecture().GetAddressByteSize() == 4) +    { +        // 32-bit hosts run with a RegisterContextLinux_i386 context. +        return new RegisterContextLinux_i386(target_arch); +    } +    else +    { +        assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) && +               "Register setting path assumes this is a 64-bit host"); +        // X86_64 hosts know how to work with 64-bit and 32-bit EXEs using the x86_64 register context. +        return new RegisterContextLinux_x86_64 (target_arch); +    } +} + +NativeRegisterContextLinux_x86_64::NativeRegisterContextLinux_x86_64 (const ArchSpec& target_arch, +                                                                      NativeThreadProtocol &native_thread, +                                                                      uint32_t concrete_frame_idx) : +    NativeRegisterContextLinux (native_thread, concrete_frame_idx, CreateRegisterInfoInterface(target_arch)), +    m_fpr_type (eFPRTypeNotValid), +    m_fpr (), +    m_iovec (), +    m_ymm_set (), +    m_reg_info (), +    m_gpr_x86_64 () +{ +    // Set up data about ranges of valid registers. +    switch (target_arch.GetMachine ()) +    { +        case llvm::Triple::x86: +            m_reg_info.num_registers        = k_num_registers_i386; +            m_reg_info.num_gpr_registers    = k_num_gpr_registers_i386; +            m_reg_info.num_fpr_registers    = k_num_fpr_registers_i386; +            m_reg_info.num_avx_registers    = k_num_avx_registers_i386; +            m_reg_info.last_gpr             = k_last_gpr_i386; +            m_reg_info.first_fpr            = k_first_fpr_i386; +            m_reg_info.last_fpr             = k_last_fpr_i386; +            m_reg_info.first_st             = lldb_st0_i386; +            m_reg_info.last_st              = lldb_st7_i386; +            m_reg_info.first_mm             = lldb_mm0_i386; +            m_reg_info.last_mm              = lldb_mm7_i386; +            m_reg_info.first_xmm            = lldb_xmm0_i386; +            m_reg_info.last_xmm             = lldb_xmm7_i386; +            m_reg_info.first_ymm            = lldb_ymm0_i386; +            m_reg_info.last_ymm             = lldb_ymm7_i386; +            m_reg_info.first_dr             = lldb_dr0_i386; +            m_reg_info.gpr_flags            = lldb_eflags_i386; +            break; +        case llvm::Triple::x86_64: +            m_reg_info.num_registers        = k_num_registers_x86_64; +            m_reg_info.num_gpr_registers    = k_num_gpr_registers_x86_64; +            m_reg_info.num_fpr_registers    = k_num_fpr_registers_x86_64; +            m_reg_info.num_avx_registers    = k_num_avx_registers_x86_64; +            m_reg_info.last_gpr             = k_last_gpr_x86_64; +            m_reg_info.first_fpr            = k_first_fpr_x86_64; +            m_reg_info.last_fpr             = k_last_fpr_x86_64; +            m_reg_info.first_st             = lldb_st0_x86_64; +            m_reg_info.last_st              = lldb_st7_x86_64; +            m_reg_info.first_mm             = lldb_mm0_x86_64; +            m_reg_info.last_mm              = lldb_mm7_x86_64; +            m_reg_info.first_xmm            = lldb_xmm0_x86_64; +            m_reg_info.last_xmm             = lldb_xmm15_x86_64; +            m_reg_info.first_ymm            = lldb_ymm0_x86_64; +            m_reg_info.last_ymm             = lldb_ymm15_x86_64; +            m_reg_info.first_dr             = lldb_dr0_x86_64; +            m_reg_info.gpr_flags            = lldb_rflags_x86_64; +            break; +        default: +            assert(false && "Unhandled target architecture."); +            break; +    } + +    // Initialize m_iovec to point to the buffer and buffer size +    // using the conventions of Berkeley style UIO structures, as required +    // by PTRACE extensions. +    m_iovec.iov_base = &m_fpr.xstate.xsave; +    m_iovec.iov_len = sizeof(m_fpr.xstate.xsave); + +    // Clear out the FPR state. +    ::memset(&m_fpr, 0, sizeof(FPR)); + +    // Store byte offset of fctrl (i.e. first register of FPR) +    const RegisterInfo *reg_info_fctrl = GetRegisterInfoByName("fctrl"); +    m_fctrl_offset_in_userarea = reg_info_fctrl->byte_offset; +} + +// CONSIDER after local and llgs debugging are merged, register set support can +// be moved into a base x86-64 class with IsRegisterSetAvailable made virtual. +uint32_t +NativeRegisterContextLinux_x86_64::GetRegisterSetCount () const +{ +    uint32_t sets = 0; +    for (uint32_t set_index = 0; set_index < k_num_register_sets; ++set_index) +    { +        if (IsRegisterSetAvailable (set_index)) +            ++sets; +    } + +    return sets; +} + +uint32_t +NativeRegisterContextLinux_x86_64::GetUserRegisterCount() const +{ +    uint32_t count = 0; +    for (uint32_t set_index = 0; set_index < k_num_register_sets; ++set_index) +    { +        const RegisterSet* set = GetRegisterSet(set_index); +        if (set) +            count += set->num_registers; +    } +    return count; +} + +const RegisterSet * +NativeRegisterContextLinux_x86_64::GetRegisterSet (uint32_t set_index) const +{ +    if (!IsRegisterSetAvailable (set_index)) +        return nullptr; + +    switch (GetRegisterInfoInterface ().GetTargetArchitecture ().GetMachine ()) +    { +        case llvm::Triple::x86: +            return &g_reg_sets_i386[set_index]; +        case llvm::Triple::x86_64: +            return &g_reg_sets_x86_64[set_index]; +        default: +            assert (false && "Unhandled target architecture."); +            return nullptr; +    } + +    return nullptr; +} + +Error +NativeRegisterContextLinux_x86_64::ReadRegister (const RegisterInfo *reg_info, RegisterValue ®_value) +{ +    Error error; + +    if (!reg_info) +    { +        error.SetErrorString ("reg_info NULL"); +        return error; +    } + +    const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB]; +    if (reg == LLDB_INVALID_REGNUM) +    { +        // This is likely an internal register for lldb use only and should not be directly queried. +        error.SetErrorStringWithFormat ("register \"%s\" is an internal-only lldb register, cannot read directly", reg_info->name); +        return error; +    } + +    if (IsFPR(reg, GetFPRType())) +    { +        error = ReadFPR(); +        if (error.Fail()) +            return error; +    } +    else +    { +        uint32_t full_reg = reg; +        bool is_subreg = reg_info->invalidate_regs && (reg_info->invalidate_regs[0] != LLDB_INVALID_REGNUM); + +        if (is_subreg) +        { +            // Read the full aligned 64-bit register. +            full_reg = reg_info->invalidate_regs[0]; +        } + +        error = ReadRegisterRaw(full_reg, reg_value); + +        if (error.Success ()) +        { +            // If our read was not aligned (for ah,bh,ch,dh), shift our returned value one byte to the right. +            if (is_subreg && (reg_info->byte_offset & 0x1)) +                reg_value.SetUInt64(reg_value.GetAsUInt64() >> 8); + +            // If our return byte size was greater than the return value reg size, then +            // use the type specified by reg_info rather than the uint64_t default +            if (reg_value.GetByteSize() > reg_info->byte_size) +                reg_value.SetType(reg_info); +        } +        return error; +    } + +    if (reg_info->encoding == lldb::eEncodingVector) +    { +        lldb::ByteOrder byte_order = GetByteOrder(); + +        if (byte_order != lldb::eByteOrderInvalid) +        { +            if (reg >= m_reg_info.first_st && reg <= m_reg_info.last_st) +                reg_value.SetBytes(m_fpr.xstate.fxsave.stmm[reg - m_reg_info.first_st].bytes, reg_info->byte_size, byte_order); +            if (reg >= m_reg_info.first_mm && reg <= m_reg_info.last_mm) +                reg_value.SetBytes(m_fpr.xstate.fxsave.stmm[reg - m_reg_info.first_mm].bytes, reg_info->byte_size, byte_order); +            if (reg >= m_reg_info.first_xmm && reg <= m_reg_info.last_xmm) +                reg_value.SetBytes(m_fpr.xstate.fxsave.xmm[reg - m_reg_info.first_xmm].bytes, reg_info->byte_size, byte_order); +            if (reg >= m_reg_info.first_ymm && reg <= m_reg_info.last_ymm) +            { +                // Concatenate ymm using the register halves in xmm.bytes and ymmh.bytes +                if (GetFPRType() == eFPRTypeXSAVE && CopyXSTATEtoYMM(reg, byte_order)) +                    reg_value.SetBytes(m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes, reg_info->byte_size, byte_order); +                else +                { +                    error.SetErrorString ("failed to copy ymm register value"); +                    return error; +                } +            } + +            if (reg_value.GetType() != RegisterValue::eTypeBytes) +                error.SetErrorString ("write failed - type was expected to be RegisterValue::eTypeBytes"); + +            return error; +        } + +        error.SetErrorString ("byte order is invalid"); +        return error; +    } + +    // Get pointer to m_fpr.xstate.fxsave variable and set the data from it. + +    // Byte offsets of all registers are calculated wrt 'UserArea' structure. +    // However, ReadFPR() reads fpu registers {using ptrace(PTRACE_GETFPREGS,..)} +    // and stores them in 'm_fpr' (of type FPR structure). To extract values of fpu +    // registers, m_fpr should be read at byte offsets calculated wrt to FPR structure. + +    // Since, FPR structure is also one of the member of UserArea structure. +    // byte_offset(fpu wrt FPR) = byte_offset(fpu wrt UserArea) - byte_offset(fctrl wrt UserArea) +    assert ( (reg_info->byte_offset - m_fctrl_offset_in_userarea) < sizeof(m_fpr)); +    uint8_t *src = (uint8_t *)&m_fpr + reg_info->byte_offset - m_fctrl_offset_in_userarea; +    switch (reg_info->byte_size) +    { +        case 1: +            reg_value.SetUInt8(*(uint8_t *)src); +            break; +        case 2: +            reg_value.SetUInt16(*(uint16_t *)src); +            break; +        case 4: +            reg_value.SetUInt32(*(uint32_t *)src); +            break; +        case 8: +            reg_value.SetUInt64(*(uint64_t *)src); +            break; +        default: +            assert(false && "Unhandled data size."); +            error.SetErrorStringWithFormat ("unhandled byte size: %" PRIu32, reg_info->byte_size); +            break; +    } + +    return error; +} + +Error +NativeRegisterContextLinux_x86_64::WriteRegister (const RegisterInfo *reg_info, const RegisterValue ®_value) +{ +    assert (reg_info && "reg_info is null"); + +    const uint32_t reg_index = reg_info->kinds[lldb::eRegisterKindLLDB]; +    if (reg_index == LLDB_INVALID_REGNUM) +        return Error ("no lldb regnum for %s", reg_info && reg_info->name ? reg_info->name : "<unknown register>"); + +    if (IsGPR(reg_index)) +        return WriteRegisterRaw(reg_index, reg_value); + +    if (IsFPR(reg_index, GetFPRType())) +    { +        if (reg_info->encoding == lldb::eEncodingVector) +        { +            if (reg_index >= m_reg_info.first_st && reg_index <= m_reg_info.last_st) +                ::memcpy (m_fpr.xstate.fxsave.stmm[reg_index - m_reg_info.first_st].bytes, reg_value.GetBytes(), reg_value.GetByteSize()); + +            if (reg_index >= m_reg_info.first_mm && reg_index <= m_reg_info.last_mm) +                ::memcpy (m_fpr.xstate.fxsave.stmm[reg_index - m_reg_info.first_mm].bytes, reg_value.GetBytes(), reg_value.GetByteSize()); + +            if (reg_index >= m_reg_info.first_xmm && reg_index <= m_reg_info.last_xmm) +                ::memcpy (m_fpr.xstate.fxsave.xmm[reg_index - m_reg_info.first_xmm].bytes, reg_value.GetBytes(), reg_value.GetByteSize()); + +            if (reg_index >= m_reg_info.first_ymm && reg_index <= m_reg_info.last_ymm) +            { +                if (GetFPRType() != eFPRTypeXSAVE) +                    return Error ("target processor does not support AVX"); + +                // Store ymm register content, and split into the register halves in xmm.bytes and ymmh.bytes +                ::memcpy (m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes, reg_value.GetBytes(), reg_value.GetByteSize()); +                if (!CopyYMMtoXSTATE(reg_index, GetByteOrder())) +                    return Error ("CopyYMMtoXSTATE() failed"); +            } +        } +        else +        { +            // Get pointer to m_fpr.xstate.fxsave variable and set the data to it. + +            // Byte offsets of all registers are calculated wrt 'UserArea' structure. +            // However, WriteFPR() takes m_fpr (of type FPR structure) and writes only fpu +            // registers using ptrace(PTRACE_SETFPREGS,..) API. Hence fpu registers should +            // be written in m_fpr at byte offsets calculated wrt FPR structure. + +            // Since, FPR structure is also one of the member of UserArea structure. +            // byte_offset(fpu wrt FPR) = byte_offset(fpu wrt UserArea) - byte_offset(fctrl wrt UserArea) +            assert ( (reg_info->byte_offset - m_fctrl_offset_in_userarea) < sizeof(m_fpr)); +            uint8_t *dst = (uint8_t *)&m_fpr + reg_info->byte_offset - m_fctrl_offset_in_userarea; +            switch (reg_info->byte_size) +            { +                case 1: +                    *(uint8_t *)dst = reg_value.GetAsUInt8(); +                    break; +                case 2: +                    *(uint16_t *)dst = reg_value.GetAsUInt16(); +                    break; +                case 4: +                    *(uint32_t *)dst = reg_value.GetAsUInt32(); +                    break; +                case 8: +                    *(uint64_t *)dst = reg_value.GetAsUInt64(); +                    break; +                default: +                    assert(false && "Unhandled data size."); +                    return Error ("unhandled register data size %" PRIu32, reg_info->byte_size); +            } +        } + +        Error error = WriteFPR(); +        if (error.Fail()) +            return error; + +        if (IsAVX(reg_index)) +        { +            if (!CopyYMMtoXSTATE(reg_index, GetByteOrder())) +                return Error ("CopyYMMtoXSTATE() failed"); +        } +        return Error (); +    } +    return Error ("failed - register wasn't recognized to be a GPR or an FPR, write strategy unknown"); +} + +Error +NativeRegisterContextLinux_x86_64::ReadAllRegisterValues (lldb::DataBufferSP &data_sp) +{ +    Error error; + +    data_sp.reset (new DataBufferHeap (REG_CONTEXT_SIZE, 0)); +    if (!data_sp) +    { +        error.SetErrorStringWithFormat ("failed to allocate DataBufferHeap instance of size %" PRIu64, REG_CONTEXT_SIZE); +        return error; +    } + +    error = ReadGPR(); +    if (error.Fail()) +        return error; + +    error = ReadFPR(); +    if (error.Fail()) +        return error; + +    uint8_t *dst = data_sp->GetBytes (); +    if (dst == nullptr) +    { +        error.SetErrorStringWithFormat ("DataBufferHeap instance of size %" PRIu64 " returned a null pointer", REG_CONTEXT_SIZE); +        return error; +    } + +    ::memcpy (dst, &m_gpr_x86_64, GetRegisterInfoInterface ().GetGPRSize ()); +    dst += GetRegisterInfoInterface ().GetGPRSize (); +    if (GetFPRType () == eFPRTypeFXSAVE) +        ::memcpy (dst, &m_fpr.xstate.fxsave, sizeof(m_fpr.xstate.fxsave)); +    else if (GetFPRType () == eFPRTypeXSAVE) +    { +        lldb::ByteOrder byte_order = GetByteOrder (); + +        // Assemble the YMM register content from the register halves. +        for (uint32_t reg = m_reg_info.first_ymm; reg <= m_reg_info.last_ymm; ++reg) +        { +            if (!CopyXSTATEtoYMM (reg, byte_order)) +            { +                error.SetErrorStringWithFormat ("NativeRegisterContextLinux_x86_64::%s CopyXSTATEtoYMM() failed for reg num %" PRIu32, __FUNCTION__, reg); +                return error; +            } +        } + +        // Copy the extended register state including the assembled ymm registers. +        ::memcpy (dst, &m_fpr, sizeof (m_fpr)); +    } +    else +    { +        assert (false && "how do we save the floating point registers?"); +        error.SetErrorString ("unsure how to save the floating point registers"); +    } +    /** The following code is specific to Linux x86 based architectures, +     *  where the register orig_eax (32 bit)/orig_rax (64 bit) is set to +     *  -1 to solve the bug 23659, such a setting prevents the automatic +     *  decrement of the instruction pointer which was causing the SIGILL +     *  exception. +     * **/ + +    RegisterValue value((uint64_t) -1); +    const RegisterInfo *reg_info = GetRegisterInfoInterface().GetDynamicRegisterInfo("orig_eax"); +    if (reg_info == nullptr) +        reg_info = GetRegisterInfoInterface().GetDynamicRegisterInfo("orig_rax"); + +    if (reg_info != nullptr) +        return DoWriteRegisterValue(reg_info->byte_offset,reg_info->name,value); + +    return error; +} + +Error +NativeRegisterContextLinux_x86_64::WriteAllRegisterValues (const lldb::DataBufferSP &data_sp) +{ +    Error error; + +    if (!data_sp) +    { +        error.SetErrorStringWithFormat ("NativeRegisterContextLinux_x86_64::%s invalid data_sp provided", __FUNCTION__); +        return error; +    } + +    if (data_sp->GetByteSize () != REG_CONTEXT_SIZE) +    { +        error.SetErrorStringWithFormat ("NativeRegisterContextLinux_x86_64::%s data_sp contained mismatched data size, expected %" PRIu64 ", actual %" PRIu64, __FUNCTION__, REG_CONTEXT_SIZE, data_sp->GetByteSize ()); +        return error; +    } + + +    uint8_t *src = data_sp->GetBytes (); +    if (src == nullptr) +    { +        error.SetErrorStringWithFormat ("NativeRegisterContextLinux_x86_64::%s DataBuffer::GetBytes() returned a null pointer", __FUNCTION__); +        return error; +    } +    ::memcpy (&m_gpr_x86_64, src, GetRegisterInfoInterface ().GetGPRSize ()); + +    error = WriteGPR(); +    if (error.Fail()) +        return error; + +    src += GetRegisterInfoInterface ().GetGPRSize (); +    if (GetFPRType () == eFPRTypeFXSAVE) +        ::memcpy (&m_fpr.xstate.fxsave, src, sizeof(m_fpr.xstate.fxsave)); +    else if (GetFPRType () == eFPRTypeXSAVE) +        ::memcpy (&m_fpr.xstate.xsave, src, sizeof(m_fpr.xstate.xsave)); + +    error = WriteFPR(); +    if (error.Fail()) +        return error; + +    if (GetFPRType() == eFPRTypeXSAVE) +    { +        lldb::ByteOrder byte_order = GetByteOrder(); + +        // Parse the YMM register content from the register halves. +        for (uint32_t reg = m_reg_info.first_ymm; reg <= m_reg_info.last_ymm; ++reg) +        { +            if (!CopyYMMtoXSTATE (reg, byte_order)) +            { +                error.SetErrorStringWithFormat ("NativeRegisterContextLinux_x86_64::%s CopyYMMtoXSTATE() failed for reg num %" PRIu32, __FUNCTION__, reg); +                return error; +            } +        } +    } + +    return error; +} + +bool +NativeRegisterContextLinux_x86_64::IsRegisterSetAvailable (uint32_t set_index) const +{ +    // Note: Extended register sets are assumed to be at the end of g_reg_sets. +    uint32_t num_sets = k_num_register_sets - k_num_extended_register_sets; + +    if (GetFPRType () == eFPRTypeXSAVE) +    { +        // AVX is the first extended register set. +        ++num_sets; +    } +    return (set_index < num_sets); +} + +bool +NativeRegisterContextLinux_x86_64::IsGPR(uint32_t reg_index) const +{ +    // GPRs come first. +    return reg_index <= m_reg_info.last_gpr; +} + +NativeRegisterContextLinux_x86_64::FPRType +NativeRegisterContextLinux_x86_64::GetFPRType () const +{ +    Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); +    if (m_fpr_type == eFPRTypeNotValid) +    { +        // TODO: Use assembly to call cpuid on the inferior and query ebx or ecx. + +        // Try and see if AVX register retrieval works. +        m_fpr_type = eFPRTypeXSAVE; +        if (const_cast<NativeRegisterContextLinux_x86_64*>(this)->ReadFPR().Fail()) +        { +            // Fall back to general floating point with no AVX support. +            m_fpr_type = eFPRTypeFXSAVE; + +            // Check if FXSAVE area can be read. +            if (const_cast<NativeRegisterContextLinux_x86_64*>(this)->ReadFPR().Fail()) +            { +                if (log) +                    log->Printf("NativeRegisterContextLinux_x86_64::%s ptrace APIs failed to read XSAVE/FXSAVE area", __FUNCTION__); +            } +        } +    } +    return m_fpr_type; +} + +bool +NativeRegisterContextLinux_x86_64::IsFPR(uint32_t reg_index) const +{ +    return (m_reg_info.first_fpr <= reg_index && reg_index <= m_reg_info.last_fpr); +} + +bool +NativeRegisterContextLinux_x86_64::IsFPR(uint32_t reg_index, FPRType fpr_type) const +{ +    bool generic_fpr = IsFPR(reg_index); + +    if (fpr_type == eFPRTypeXSAVE) +        return generic_fpr || IsAVX(reg_index); +    return generic_fpr; +} + +Error +NativeRegisterContextLinux_x86_64::WriteFPR() +{ +    const FPRType fpr_type = GetFPRType (); +    const lldb_private::ArchSpec& target_arch = GetRegisterInfoInterface().GetTargetArchitecture(); +    switch (fpr_type) +    { +    case FPRType::eFPRTypeFXSAVE: +        // For 32-bit inferiors on x86_32/x86_64 architectures, +        // FXSAVE area can be written using PTRACE_SETREGSET ptrace api +        // For 64-bit inferiors on x86_64 architectures, +        // FXSAVE area can be written using PTRACE_SETFPREGS ptrace api +        switch (target_arch.GetMachine ()) +        { +            case llvm::Triple::x86: +                return WriteRegisterSet(&m_iovec, sizeof(m_fpr.xstate.xsave), NT_PRXFPREG); +            case llvm::Triple::x86_64: +                return NativeRegisterContextLinux::WriteFPR(); +            default: +                assert(false && "Unhandled target architecture."); +                break; +        } +    case FPRType::eFPRTypeXSAVE: +        return WriteRegisterSet(&m_iovec, sizeof(m_fpr.xstate.xsave), NT_X86_XSTATE); +    default: +        return Error("Unrecognized FPR type"); +    } +} + +bool +NativeRegisterContextLinux_x86_64::IsAVX(uint32_t reg_index) const +{ +    return (m_reg_info.first_ymm <= reg_index && reg_index <= m_reg_info.last_ymm); +} + +bool +NativeRegisterContextLinux_x86_64::CopyXSTATEtoYMM (uint32_t reg_index, lldb::ByteOrder byte_order) +{ +    if (!IsAVX (reg_index)) +        return false; + +    if (byte_order == lldb::eByteOrderLittle) +    { +        ::memcpy (m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes, +                 m_fpr.xstate.fxsave.xmm[reg_index - m_reg_info.first_ymm].bytes, +                 sizeof (XMMReg)); +        ::memcpy (m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes + sizeof (XMMReg), +                 m_fpr.xstate.xsave.ymmh[reg_index - m_reg_info.first_ymm].bytes, +                 sizeof (YMMHReg)); +        return true; +    } + +    if (byte_order == lldb::eByteOrderBig) +    { +        ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes + sizeof (XMMReg), +                 m_fpr.xstate.fxsave.xmm[reg_index - m_reg_info.first_ymm].bytes, +                 sizeof (XMMReg)); +        ::memcpy(m_ymm_set.ymm[reg_index - m_reg_info.first_ymm].bytes, +                 m_fpr.xstate.xsave.ymmh[reg_index - m_reg_info.first_ymm].bytes, +                 sizeof (YMMHReg)); +        return true; +    } +    return false; // unsupported or invalid byte order + +} + +bool +NativeRegisterContextLinux_x86_64::CopyYMMtoXSTATE(uint32_t reg, lldb::ByteOrder byte_order) +{ +    if (!IsAVX(reg)) +        return false; + +    if (byte_order == lldb::eByteOrderLittle) +    { +        ::memcpy(m_fpr.xstate.fxsave.xmm[reg - m_reg_info.first_ymm].bytes, +                 m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes, +                 sizeof(XMMReg)); +        ::memcpy(m_fpr.xstate.xsave.ymmh[reg - m_reg_info.first_ymm].bytes, +                 m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes + sizeof(XMMReg), +                 sizeof(YMMHReg)); +        return true; +    } + +    if (byte_order == lldb::eByteOrderBig) +    { +        ::memcpy(m_fpr.xstate.fxsave.xmm[reg - m_reg_info.first_ymm].bytes, +                 m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes + sizeof(XMMReg), +                 sizeof(XMMReg)); +        ::memcpy(m_fpr.xstate.xsave.ymmh[reg - m_reg_info.first_ymm].bytes, +                 m_ymm_set.ymm[reg - m_reg_info.first_ymm].bytes, +                 sizeof(YMMHReg)); +        return true; +    } +    return false; // unsupported or invalid byte order +} + +void* +NativeRegisterContextLinux_x86_64::GetFPRBuffer() +{ +    const FPRType fpr_type = GetFPRType (); +    switch (fpr_type) +    { +    case FPRType::eFPRTypeFXSAVE: +        return &m_fpr.xstate.fxsave; +    case FPRType::eFPRTypeXSAVE: +        return &m_iovec; +    default: +        return nullptr; +    } +} + +size_t +NativeRegisterContextLinux_x86_64::GetFPRSize() +{ +    const FPRType fpr_type = GetFPRType (); +    switch (fpr_type) +    { +    case FPRType::eFPRTypeFXSAVE: +        return sizeof(m_fpr.xstate.fxsave); +    case FPRType::eFPRTypeXSAVE: +        return sizeof(m_iovec); +    default: +        return 0; +    } +} + +Error +NativeRegisterContextLinux_x86_64::ReadFPR () +{ +    const FPRType fpr_type = GetFPRType (); +    const lldb_private::ArchSpec& target_arch = GetRegisterInfoInterface().GetTargetArchitecture(); +    switch (fpr_type) +    { +    case FPRType::eFPRTypeFXSAVE: +        // For 32-bit inferiors on x86_32/x86_64 architectures, +        // FXSAVE area can be read using PTRACE_GETREGSET ptrace api +        // For 64-bit inferiors on x86_64 architectures, +        // FXSAVE area can be read using PTRACE_GETFPREGS ptrace api +        switch (target_arch.GetMachine ()) +        { +            case llvm::Triple::x86: +                return ReadRegisterSet(&m_iovec, sizeof(m_fpr.xstate.xsave), NT_PRXFPREG); +            case llvm::Triple::x86_64: +                return NativeRegisterContextLinux::ReadFPR(); +            default: +                assert(false && "Unhandled target architecture."); +                break; +        } +    case FPRType::eFPRTypeXSAVE: +        return ReadRegisterSet(&m_iovec, sizeof(m_fpr.xstate.xsave), NT_X86_XSTATE); +    default: +        return Error("Unrecognized FPR type"); +    } +} + +Error +NativeRegisterContextLinux_x86_64::IsWatchpointHit(uint32_t wp_index, bool &is_hit) +{ +    if (wp_index >= NumSupportedHardwareWatchpoints()) +        return Error("Watchpoint index out of range"); + +    RegisterValue reg_value; +    Error error = ReadRegisterRaw(m_reg_info.first_dr + 6, reg_value); +    if (error.Fail()) +    { +        is_hit = false; +        return error; +    } + +    uint64_t status_bits = reg_value.GetAsUInt64(); + +    is_hit = status_bits & (1 << wp_index); + +    return error; +} + +Error +NativeRegisterContextLinux_x86_64::GetWatchpointHitIndex(uint32_t &wp_index, lldb::addr_t trap_addr) { +    uint32_t num_hw_wps = NumSupportedHardwareWatchpoints(); +    for (wp_index = 0; wp_index < num_hw_wps; ++wp_index) +    { +        bool is_hit; +        Error error = IsWatchpointHit(wp_index, is_hit); +        if (error.Fail()) { +            wp_index = LLDB_INVALID_INDEX32; +            return error; +        } else if (is_hit) { +            return error; +        } +    } +    wp_index = LLDB_INVALID_INDEX32; +    return Error(); +} + +Error +NativeRegisterContextLinux_x86_64::IsWatchpointVacant(uint32_t wp_index, bool &is_vacant) +{ +    if (wp_index >= NumSupportedHardwareWatchpoints()) +        return Error ("Watchpoint index out of range"); + +    RegisterValue reg_value; +    Error error = ReadRegisterRaw(m_reg_info.first_dr + 7, reg_value); +    if (error.Fail()) +    { +        is_vacant = false; +        return error; +    } + +    uint64_t control_bits = reg_value.GetAsUInt64(); + +    is_vacant = !(control_bits & (1 << (2 * wp_index))); + +    return error; +} + +Error +NativeRegisterContextLinux_x86_64::SetHardwareWatchpointWithIndex( +        lldb::addr_t addr, size_t size, uint32_t watch_flags, uint32_t wp_index) { + +    if (wp_index >= NumSupportedHardwareWatchpoints()) +        return Error ("Watchpoint index out of range"); + +    // Read only watchpoints aren't supported on x86_64. Fall back to read/write waitchpoints instead. +    // TODO: Add logic to detect when a write happens and ignore that watchpoint hit. +    if (watch_flags == 0x2) +        watch_flags = 0x3; + +    if (watch_flags != 0x1 && watch_flags != 0x3) +        return Error ("Invalid read/write bits for watchpoint"); + +    if (size != 1 && size != 2 && size != 4 && size != 8) +        return Error ("Invalid size for watchpoint"); + +    bool is_vacant; +    Error error = IsWatchpointVacant (wp_index, is_vacant); +    if (error.Fail()) return error; +    if (!is_vacant) return Error("Watchpoint index not vacant"); + +    RegisterValue reg_value; +    error = ReadRegisterRaw(m_reg_info.first_dr + 7, reg_value); +    if (error.Fail()) return error; + +    // for watchpoints 0, 1, 2, or 3, respectively, +    // set bits 1, 3, 5, or 7 +    uint64_t enable_bit = 1 << (2 * wp_index); + +    // set bits 16-17, 20-21, 24-25, or 28-29 +    // with 0b01 for write, and 0b11 for read/write +    uint64_t rw_bits = watch_flags << (16 + 4 * wp_index); + +    // set bits 18-19, 22-23, 26-27, or 30-31 +    // with 0b00, 0b01, 0b10, or 0b11 +    // for 1, 2, 8 (if supported), or 4 bytes, respectively +    uint64_t size_bits = (size == 8 ? 0x2 : size - 1) << (18 + 4 * wp_index); + +    uint64_t bit_mask = (0x3 << (2 * wp_index)) | (0xF << (16 + 4 * wp_index)); + +    uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask; + +    control_bits |= enable_bit | rw_bits | size_bits; + +    error = WriteRegisterRaw(m_reg_info.first_dr + wp_index, RegisterValue(addr)); +    if (error.Fail()) return error; + +    error = WriteRegisterRaw(m_reg_info.first_dr + 7, RegisterValue(control_bits)); +    if (error.Fail()) return error; + +    error.Clear(); +    return error; +} + +bool +NativeRegisterContextLinux_x86_64::ClearHardwareWatchpoint(uint32_t wp_index) +{ +    if (wp_index >= NumSupportedHardwareWatchpoints()) +        return false; + +    RegisterValue reg_value; + +    // for watchpoints 0, 1, 2, or 3, respectively, +    // clear bits 0, 1, 2, or 3 of the debug status register (DR6) +    Error error = ReadRegisterRaw(m_reg_info.first_dr + 6, reg_value); +    if (error.Fail()) return false; +    uint64_t bit_mask = 1 << wp_index; +    uint64_t status_bits = reg_value.GetAsUInt64() & ~bit_mask; +    error = WriteRegisterRaw(m_reg_info.first_dr + 6, RegisterValue(status_bits)); +    if (error.Fail()) return false; + +    // for watchpoints 0, 1, 2, or 3, respectively, +    // clear bits {0-1,16-19}, {2-3,20-23}, {4-5,24-27}, or {6-7,28-31} +    // of the debug control register (DR7) +    error = ReadRegisterRaw(m_reg_info.first_dr + 7, reg_value); +    if (error.Fail()) return false; +    bit_mask = (0x3 << (2 * wp_index)) | (0xF << (16 + 4 * wp_index)); +    uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask; +    return WriteRegisterRaw(m_reg_info.first_dr + 7, RegisterValue(control_bits)).Success(); +} + +Error +NativeRegisterContextLinux_x86_64::ClearAllHardwareWatchpoints() +{ +    RegisterValue reg_value; + +    // clear bits {0-4} of the debug status register (DR6) +    Error error = ReadRegisterRaw(m_reg_info.first_dr + 6, reg_value); +    if (error.Fail()) return error; +    uint64_t bit_mask = 0xF; +    uint64_t status_bits = reg_value.GetAsUInt64() & ~bit_mask; +    error = WriteRegisterRaw(m_reg_info.first_dr + 6, RegisterValue(status_bits)); +    if (error.Fail()) return error; + +    // clear bits {0-7,16-31} of the debug control register (DR7) +    error = ReadRegisterRaw(m_reg_info.first_dr + 7, reg_value); +    if (error.Fail()) return error; +    bit_mask = 0xFF | (0xFFFF << 16); +    uint64_t control_bits = reg_value.GetAsUInt64() & ~bit_mask; +    return WriteRegisterRaw(m_reg_info.first_dr + 7, RegisterValue(control_bits)); +} + +uint32_t +NativeRegisterContextLinux_x86_64::SetHardwareWatchpoint( +        lldb::addr_t addr, size_t size, uint32_t watch_flags) +{ +    Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); +    const uint32_t num_hw_watchpoints = NumSupportedHardwareWatchpoints(); +    for (uint32_t wp_index = 0; wp_index < num_hw_watchpoints; ++wp_index) +    { +        bool is_vacant; +        Error error = IsWatchpointVacant(wp_index, is_vacant); +        if (is_vacant) +        { +            error = SetHardwareWatchpointWithIndex(addr, size, watch_flags, wp_index); +            if (error.Success()) +                return wp_index; +        } +        if (error.Fail() && log) +        { +            log->Printf("NativeRegisterContextLinux_x86_64::%s Error: %s", +                    __FUNCTION__, error.AsCString()); +        } +    } +    return LLDB_INVALID_INDEX32; +} + +lldb::addr_t +NativeRegisterContextLinux_x86_64::GetWatchpointAddress(uint32_t wp_index) +{ +    if (wp_index >= NumSupportedHardwareWatchpoints()) +        return LLDB_INVALID_ADDRESS; +    RegisterValue reg_value; +    if (ReadRegisterRaw(m_reg_info.first_dr + wp_index, reg_value).Fail()) +        return LLDB_INVALID_ADDRESS; +    return reg_value.GetAsUInt64(); +} + +uint32_t +NativeRegisterContextLinux_x86_64::NumSupportedHardwareWatchpoints () +{ +    // Available debug address registers: dr0, dr1, dr2, dr3 +    return 4; +} + +#endif // defined(__i386__) || defined(__x86_64__) diff --git a/source/Plugins/Process/Linux/NativeRegisterContextLinux_x86_64.h b/source/Plugins/Process/Linux/NativeRegisterContextLinux_x86_64.h new file mode 100644 index 000000000000..b04be4bb7688 --- /dev/null +++ b/source/Plugins/Process/Linux/NativeRegisterContextLinux_x86_64.h @@ -0,0 +1,171 @@ +//===-- NativeRegisterContextLinux_x86_64.h ---------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#if defined(__i386__) || defined(__x86_64__) + +#ifndef lldb_NativeRegisterContextLinux_x86_64_h +#define lldb_NativeRegisterContextLinux_x86_64_h + +#include "Plugins/Process/Linux/NativeRegisterContextLinux.h" +#include "Plugins/Process/Utility/RegisterContext_x86.h" +#include "Plugins/Process/Utility/lldb-x86-register-enums.h" + +namespace lldb_private { +namespace process_linux { + +    class NativeProcessLinux; + +    class NativeRegisterContextLinux_x86_64 : public NativeRegisterContextLinux +    { +    public: +        NativeRegisterContextLinux_x86_64 (const ArchSpec& target_arch, +                                           NativeThreadProtocol &native_thread, +                                           uint32_t concrete_frame_idx); + +        uint32_t +        GetRegisterSetCount () const override; + +        const RegisterSet * +        GetRegisterSet (uint32_t set_index) const override; + +        uint32_t +        GetUserRegisterCount() const override; + +        Error +        ReadRegister (const RegisterInfo *reg_info, RegisterValue ®_value) override; + +        Error +        WriteRegister (const RegisterInfo *reg_info, const RegisterValue ®_value) override; + +        Error +        ReadAllRegisterValues (lldb::DataBufferSP &data_sp) override; + +        Error +        WriteAllRegisterValues (const lldb::DataBufferSP &data_sp) override; + +        Error +        IsWatchpointHit(uint32_t wp_index, bool &is_hit) override; + +        Error +        GetWatchpointHitIndex(uint32_t &wp_index, lldb::addr_t trap_addr) override; + +        Error +        IsWatchpointVacant(uint32_t wp_index, bool &is_vacant) override; + +        bool +        ClearHardwareWatchpoint(uint32_t wp_index) override; + +        Error +        ClearAllHardwareWatchpoints () override; + +        Error +        SetHardwareWatchpointWithIndex(lldb::addr_t addr, size_t size, +                uint32_t watch_flags, uint32_t wp_index); + +        uint32_t +        SetHardwareWatchpoint(lldb::addr_t addr, size_t size, +                uint32_t watch_flags) override; + +        lldb::addr_t +        GetWatchpointAddress(uint32_t wp_index) override; + +        uint32_t +        NumSupportedHardwareWatchpoints() override; + +    protected: +        void* +        GetGPRBuffer() override { return &m_gpr_x86_64; } + +        void* +        GetFPRBuffer() override; + +        size_t +        GetFPRSize() override; + +        Error +        ReadFPR() override; + +        Error +        WriteFPR() override; + +    private: + +        // Private member types. +        enum FPRType +        { +            eFPRTypeNotValid = 0, +            eFPRTypeFXSAVE, +            eFPRTypeXSAVE +        }; + +        // Info about register ranges. +        struct RegInfo +        { +            uint32_t num_registers; +            uint32_t num_gpr_registers; +            uint32_t num_fpr_registers; +            uint32_t num_avx_registers; + +            uint32_t last_gpr; +            uint32_t first_fpr; +            uint32_t last_fpr; + +            uint32_t first_st; +            uint32_t last_st; +            uint32_t first_mm; +            uint32_t last_mm; +            uint32_t first_xmm; +            uint32_t last_xmm; +            uint32_t first_ymm; +            uint32_t last_ymm; + +            uint32_t first_dr; +            uint32_t gpr_flags; +        }; + +        // Private member variables. +        mutable FPRType m_fpr_type; +        FPR m_fpr; +        IOVEC m_iovec; +        YMM m_ymm_set; +        RegInfo m_reg_info; +        uint64_t m_gpr_x86_64[k_num_gpr_registers_x86_64]; +        uint32_t m_fctrl_offset_in_userarea; + +        // Private member methods. +        bool IsRegisterSetAvailable (uint32_t set_index) const; + +        bool +        IsGPR(uint32_t reg_index) const; + +        FPRType +        GetFPRType () const; + +        bool +        IsFPR(uint32_t reg_index) const; + +        bool +        IsFPR(uint32_t reg_index, FPRType fpr_type) const; + +        bool +        CopyXSTATEtoYMM (uint32_t reg_index, lldb::ByteOrder byte_order); + +        bool +        CopyYMMtoXSTATE(uint32_t reg, lldb::ByteOrder byte_order); + +        bool +        IsAVX (uint32_t reg_index) const; +    }; + +} // namespace process_linux +} // namespace lldb_private + +#endif // #ifndef lldb_NativeRegisterContextLinux_x86_64_h + +#endif // defined(__i386__) || defined(__x86_64__) diff --git a/source/Plugins/Process/Linux/NativeThreadLinux.cpp b/source/Plugins/Process/Linux/NativeThreadLinux.cpp new file mode 100644 index 000000000000..cbf82885e23a --- /dev/null +++ b/source/Plugins/Process/Linux/NativeThreadLinux.cpp @@ -0,0 +1,440 @@ +//===-- NativeThreadLinux.cpp --------------------------------- -*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "NativeThreadLinux.h" + +#include <signal.h> +#include <sstream> + +#include "NativeProcessLinux.h" +#include "NativeRegisterContextLinux.h" + +#include "lldb/Core/Log.h" +#include "lldb/Core/State.h" +#include "lldb/Host/HostNativeThread.h" +#include "lldb/Utility/LLDBAssert.h" +#include "lldb/lldb-enumerations.h" + +#include "llvm/ADT/SmallString.h" + +#include "Plugins/Process/POSIX/CrashReason.h" + +#include <sys/syscall.h> +// Try to define a macro to encapsulate the tgkill syscall +#define tgkill(pid, tid, sig) \ +    syscall(SYS_tgkill, static_cast< ::pid_t>(pid), static_cast< ::pid_t>(tid), sig) + +using namespace lldb; +using namespace lldb_private; +using namespace lldb_private::process_linux; + +namespace +{ +    void LogThreadStopInfo (Log &log, const ThreadStopInfo &stop_info, const char *const header) +    { +        switch (stop_info.reason) +        { +            case eStopReasonNone: +                log.Printf ("%s: %s no stop reason", __FUNCTION__, header); +                return; +            case eStopReasonTrace: +                log.Printf ("%s: %s trace, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); +                return; +            case eStopReasonBreakpoint: +                log.Printf ("%s: %s breakpoint, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); +                return; +            case eStopReasonWatchpoint: +                log.Printf ("%s: %s watchpoint, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); +                return; +            case eStopReasonSignal: +                log.Printf ("%s: %s signal 0x%02" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); +                return; +            case eStopReasonException: +                log.Printf ("%s: %s exception type 0x%02" PRIx64, __FUNCTION__, header, stop_info.details.exception.type); +                return; +            case eStopReasonExec: +                log.Printf ("%s: %s exec, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); +                return; +            case eStopReasonPlanComplete: +                log.Printf ("%s: %s plan complete", __FUNCTION__, header); +                return; +            case eStopReasonThreadExiting: +                log.Printf ("%s: %s thread exiting", __FUNCTION__, header); +                return; +            case eStopReasonInstrumentation: +                log.Printf ("%s: %s instrumentation", __FUNCTION__, header); +                return; +            default: +                log.Printf ("%s: %s invalid stop reason %" PRIu32, __FUNCTION__, header, static_cast<uint32_t> (stop_info.reason)); +        } +    } +} + +NativeThreadLinux::NativeThreadLinux (NativeProcessLinux *process, lldb::tid_t tid) : +    NativeThreadProtocol (process, tid), +    m_state (StateType::eStateInvalid), +    m_stop_info (), +    m_reg_context_sp (), +    m_stop_description () +{ +} + +std::string +NativeThreadLinux::GetName() +{ +    NativeProcessProtocolSP process_sp = m_process_wp.lock (); +    if (!process_sp) +        return "<unknown: no process>"; + +    // const NativeProcessLinux *const process = reinterpret_cast<NativeProcessLinux*> (process_sp->get ()); +    llvm::SmallString<32> thread_name; +    HostNativeThread::GetName(GetID(), thread_name); +    return thread_name.c_str(); +} + +lldb::StateType +NativeThreadLinux::GetState () +{ +    return m_state; +} + + +bool +NativeThreadLinux::GetStopReason (ThreadStopInfo &stop_info, std::string& description) +{ +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); + +    description.clear(); + +    switch (m_state) +    { +    case eStateStopped: +    case eStateCrashed: +    case eStateExited: +    case eStateSuspended: +    case eStateUnloaded: +        if (log) +            LogThreadStopInfo (*log, m_stop_info, "m_stop_info in thread:"); +        stop_info = m_stop_info; +        description = m_stop_description; +        if (log) +            LogThreadStopInfo (*log, stop_info, "returned stop_info:"); + +        return true; + +    case eStateInvalid: +    case eStateConnected: +    case eStateAttaching: +    case eStateLaunching: +    case eStateRunning: +    case eStateStepping: +    case eStateDetached: +        if (log) +        { +            log->Printf ("NativeThreadLinux::%s tid %" PRIu64 " in state %s cannot answer stop reason", +                    __FUNCTION__, GetID (), StateAsCString (m_state)); +        } +        return false; +    } +    llvm_unreachable("unhandled StateType!"); +} + +NativeRegisterContextSP +NativeThreadLinux::GetRegisterContext () +{ +    // Return the register context if we already created it. +    if (m_reg_context_sp) +        return m_reg_context_sp; + +    NativeProcessProtocolSP m_process_sp = m_process_wp.lock (); +    if (!m_process_sp) +        return NativeRegisterContextSP (); + +    ArchSpec target_arch; +    if (!m_process_sp->GetArchitecture (target_arch)) +        return NativeRegisterContextSP (); + +    const uint32_t concrete_frame_idx = 0; +    m_reg_context_sp.reset (NativeRegisterContextLinux::CreateHostNativeRegisterContextLinux(target_arch, +                                                                                             *this, +                                                                                             concrete_frame_idx)); + +    return m_reg_context_sp; +} + +Error +NativeThreadLinux::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) +{ +    if (!hardware) +        return Error ("not implemented"); +    if (m_state == eStateLaunching) +        return Error (); +    Error error = RemoveWatchpoint(addr); +    if (error.Fail()) return error; +    NativeRegisterContextSP reg_ctx = GetRegisterContext (); +    uint32_t wp_index = +        reg_ctx->SetHardwareWatchpoint (addr, size, watch_flags); +    if (wp_index == LLDB_INVALID_INDEX32) +        return Error ("Setting hardware watchpoint failed."); +    m_watchpoint_index_map.insert({addr, wp_index}); +    return Error (); +} + +Error +NativeThreadLinux::RemoveWatchpoint (lldb::addr_t addr) +{ +    auto wp = m_watchpoint_index_map.find(addr); +    if (wp == m_watchpoint_index_map.end()) +        return Error (); +    uint32_t wp_index = wp->second; +    m_watchpoint_index_map.erase(wp); +    if (GetRegisterContext()->ClearHardwareWatchpoint(wp_index)) +        return Error (); +    return Error ("Clearing hardware watchpoint failed."); +} + +void +NativeThreadLinux::SetRunning () +{ +    const StateType new_state = StateType::eStateRunning; +    MaybeLogStateChange (new_state); +    m_state = new_state; + +    m_stop_info.reason = StopReason::eStopReasonNone; +    m_stop_description.clear(); + +    // If watchpoints have been set, but none on this thread, +    // then this is a new thread. So set all existing watchpoints. +    if (m_watchpoint_index_map.empty()) +    { +        const auto process_sp = GetProcess(); +        if (process_sp) +        { +            const auto &watchpoint_map = process_sp->GetWatchpointMap(); +            if (watchpoint_map.empty()) return; +            GetRegisterContext()->ClearAllHardwareWatchpoints(); +            for (const auto &pair : watchpoint_map) +            { +                const auto& wp = pair.second; +                SetWatchpoint(wp.m_addr, wp.m_size, wp.m_watch_flags, wp.m_hardware); +            } +        } +    } +} + +void +NativeThreadLinux::SetStepping () +{ +    const StateType new_state = StateType::eStateStepping; +    MaybeLogStateChange (new_state); +    m_state = new_state; + +    m_stop_info.reason = StopReason::eStopReasonNone; +} + +void +NativeThreadLinux::SetStoppedBySignal(uint32_t signo, const siginfo_t *info) +{ +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); +    if (log) +        log->Printf ("NativeThreadLinux::%s called with signal 0x%02" PRIx32, __FUNCTION__, signo); + +    const StateType new_state = StateType::eStateStopped; +    MaybeLogStateChange (new_state); +    m_state = new_state; + +    m_stop_info.reason = StopReason::eStopReasonSignal; +    m_stop_info.details.signal.signo = signo; + +    m_stop_description.clear(); +    if (info) +    { +        switch (signo) +        { +        case SIGSEGV: +        case SIGBUS: +        case SIGFPE: +        case SIGILL: +             //In case of MIPS64 target, SI_KERNEL is generated for invalid 64bit address. +             const auto reason = (info->si_signo == SIGBUS && info->si_code == SI_KERNEL) ?  +                                  CrashReason::eInvalidAddress : GetCrashReason(*info); +             m_stop_description = GetCrashReasonString(reason, reinterpret_cast<uintptr_t>(info->si_addr)); +             break; +        } +    } +} + +bool +NativeThreadLinux::IsStopped (int *signo) +{ +    if (!StateIsStoppedState (m_state, false)) +        return false; + +    // If we are stopped by a signal, return the signo. +    if (signo && +        m_state == StateType::eStateStopped && +        m_stop_info.reason == StopReason::eStopReasonSignal) +    { +        *signo = m_stop_info.details.signal.signo; +    } + +    // Regardless, we are stopped. +    return true; +} + + +void +NativeThreadLinux::SetStoppedByExec () +{ +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); +    if (log) +        log->Printf ("NativeThreadLinux::%s()", __FUNCTION__); + +    const StateType new_state = StateType::eStateStopped; +    MaybeLogStateChange (new_state); +    m_state = new_state; + +    m_stop_info.reason = StopReason::eStopReasonExec; +    m_stop_info.details.signal.signo = SIGSTOP; +} + +void +NativeThreadLinux::SetStoppedByBreakpoint () +{ +    const StateType new_state = StateType::eStateStopped; +    MaybeLogStateChange (new_state); +    m_state = new_state; + +    m_stop_info.reason = StopReason::eStopReasonBreakpoint; +    m_stop_info.details.signal.signo = SIGTRAP; +    m_stop_description.clear(); +} + +void +NativeThreadLinux::SetStoppedByWatchpoint (uint32_t wp_index) +{ +    const StateType new_state = StateType::eStateStopped; +    MaybeLogStateChange (new_state); +    m_state = new_state; +    m_stop_description.clear (); + +    lldbassert(wp_index != LLDB_INVALID_INDEX32 && +               "wp_index cannot be invalid"); + +    std::ostringstream ostr; +    ostr << GetRegisterContext()->GetWatchpointAddress(wp_index) << " "; +    ostr << wp_index; + +    /* +     * MIPS: Last 3bits of the watchpoint address are masked by the kernel. For example: +     * 'n' is at 0x120010d00 and 'm' is 0x120010d04. When a watchpoint is set at 'm', then +     * watch exception is generated even when 'n' is read/written. To handle this case, +     * find the base address of the load/store instruction and append it in the stop-info  +     * packet. +    */ +    ostr << " " << GetRegisterContext()->GetWatchpointHitAddress(wp_index); + +    m_stop_description = ostr.str(); + +    m_stop_info.reason = StopReason::eStopReasonWatchpoint; +    m_stop_info.details.signal.signo = SIGTRAP; +} + +bool +NativeThreadLinux::IsStoppedAtBreakpoint () +{ +    return GetState () == StateType::eStateStopped && +        m_stop_info.reason == StopReason::eStopReasonBreakpoint; +} + +bool +NativeThreadLinux::IsStoppedAtWatchpoint () +{ +    return GetState () == StateType::eStateStopped && +        m_stop_info.reason == StopReason::eStopReasonWatchpoint; +} + +void +NativeThreadLinux::SetStoppedByTrace () +{ +    const StateType new_state = StateType::eStateStopped; +    MaybeLogStateChange (new_state); +    m_state = new_state; + +    m_stop_info.reason = StopReason::eStopReasonTrace; +    m_stop_info.details.signal.signo = SIGTRAP; +} + +void +NativeThreadLinux::SetStoppedWithNoReason () +{ +    const StateType new_state = StateType::eStateStopped; +    MaybeLogStateChange (new_state); +    m_state = new_state; + +    m_stop_info.reason = StopReason::eStopReasonNone; +    m_stop_info.details.signal.signo = 0; +} + +void +NativeThreadLinux::SetExited () +{ +    const StateType new_state = StateType::eStateExited; +    MaybeLogStateChange (new_state); +    m_state = new_state; + +    m_stop_info.reason = StopReason::eStopReasonThreadExiting; +} + +Error +NativeThreadLinux::RequestStop () +{ +    Log* log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); + +    const auto process_sp = GetProcess(); +    if (! process_sp) +        return Error("Process is null."); + +    lldb::pid_t pid = process_sp->GetID(); +    lldb::tid_t tid = GetID(); + +    if (log) +        log->Printf ("NativeThreadLinux::%s requesting thread stop(pid: %" PRIu64 ", tid: %" PRIu64 ")", __FUNCTION__, pid, tid); + +    Error err; +    errno = 0; +    if (::tgkill (pid, tid, SIGSTOP) != 0) +    { +        err.SetErrorToErrno (); +        if (log) +            log->Printf ("NativeThreadLinux::%s tgkill(%" PRIu64 ", %" PRIu64 ", SIGSTOP) failed: %s", __FUNCTION__, pid, tid, err.AsCString ()); +    } + +    return err; +} + +void +NativeThreadLinux::MaybeLogStateChange (lldb::StateType new_state) +{ +    Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); +    // If we're not logging, we're done. +    if (!log) +        return; + +    // If this is a state change to the same state, we're done. +    lldb::StateType old_state = m_state; +    if (new_state == old_state) +        return; + +    NativeProcessProtocolSP m_process_sp = m_process_wp.lock (); +    lldb::pid_t pid = m_process_sp ? m_process_sp->GetID () : LLDB_INVALID_PROCESS_ID; + +    // Log it. +    log->Printf ("NativeThreadLinux: thread (pid=%" PRIu64 ", tid=%" PRIu64 ") changing from state %s to %s", pid, GetID (), StateAsCString (old_state), StateAsCString (new_state)); +} diff --git a/source/Plugins/Process/Linux/NativeThreadLinux.h b/source/Plugins/Process/Linux/NativeThreadLinux.h new file mode 100644 index 000000000000..bf6b00a78cfd --- /dev/null +++ b/source/Plugins/Process/Linux/NativeThreadLinux.h @@ -0,0 +1,120 @@ +//===-- NativeThreadLinux.h ----------------------------------- -*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_NativeThreadLinux_H_ +#define liblldb_NativeThreadLinux_H_ + +#include "lldb/lldb-private-forward.h" +#include "lldb/Host/common/NativeThreadProtocol.h" + +#include <map> +#include <memory> +#include <string> + +namespace lldb_private { +namespace process_linux { + +    class NativeProcessLinux; + +    class NativeThreadLinux : public NativeThreadProtocol +    { +        friend class NativeProcessLinux; + +    public: +        NativeThreadLinux (NativeProcessLinux *process, lldb::tid_t tid); + +        // --------------------------------------------------------------------- +        // NativeThreadProtocol Interface +        // --------------------------------------------------------------------- +        std::string +        GetName() override; + +        lldb::StateType +        GetState () override; + +        bool +        GetStopReason (ThreadStopInfo &stop_info, std::string& description) override; + +        NativeRegisterContextSP +        GetRegisterContext () override; + +        Error +        SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) override; + +        Error +        RemoveWatchpoint (lldb::addr_t addr) override; + +    private: +        // --------------------------------------------------------------------- +        // Interface for friend classes +        // --------------------------------------------------------------------- +        void +        SetRunning (); + +        void +        SetStepping (); + +        void +        SetStoppedBySignal(uint32_t signo, const siginfo_t *info = nullptr); + +        /// Return true if the thread is stopped. +        /// If stopped by a signal, indicate the signo in the signo argument. +        /// Otherwise, return LLDB_INVALID_SIGNAL_NUMBER. +        bool +        IsStopped (int *signo); + +        void +        SetStoppedByExec (); + +        void +        SetStoppedByBreakpoint (); + +        void +        SetStoppedByWatchpoint (uint32_t wp_index); + +        bool +        IsStoppedAtBreakpoint (); + +        bool +        IsStoppedAtWatchpoint (); + +        void +        SetStoppedByTrace (); + +        void +        SetStoppedWithNoReason (); + +        void +        SetExited (); + +        Error +        RequestStop (); + +        // --------------------------------------------------------------------- +        // Private interface +        // --------------------------------------------------------------------- +        void +        MaybeLogStateChange (lldb::StateType new_state); + +        // --------------------------------------------------------------------- +        // Member Variables +        // --------------------------------------------------------------------- +        lldb::StateType m_state; +        ThreadStopInfo m_stop_info; +        NativeRegisterContextSP m_reg_context_sp; +        std::string m_stop_description; +        using WatchpointIndexMap = std::map<lldb::addr_t, uint32_t>; +        WatchpointIndexMap m_watchpoint_index_map; +    }; + +    typedef std::shared_ptr<NativeThreadLinux> NativeThreadLinuxSP; +} // namespace process_linux +} // namespace lldb_private + +#endif // #ifndef liblldb_NativeThreadLinux_H_ diff --git a/source/Plugins/Process/Linux/ProcFileReader.cpp b/source/Plugins/Process/Linux/ProcFileReader.cpp new file mode 100644 index 000000000000..4d1f231f4f9b --- /dev/null +++ b/source/Plugins/Process/Linux/ProcFileReader.cpp @@ -0,0 +1,108 @@ +//===-- ProcFileReader.cpp --------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "Plugins/Process/Linux/ProcFileReader.h" + +// C Headers +#include <fcntl.h> +#include <inttypes.h> +#include <limits.h> +#include <stdio.h> +#include <sys/stat.h> + +// C++ Headers +#include <fstream> + +// LLDB Headers +#include "lldb/Core/DataBufferHeap.h" +#include "lldb/Core/Error.h" + +using namespace lldb_private; +using namespace lldb_private::process_linux; + +lldb::DataBufferSP +ProcFileReader::ReadIntoDataBuffer (lldb::pid_t pid, const char *name) +{ +    int fd; +    char path[PATH_MAX]; + +    // Make sure we've got a nil terminated buffer for all the folks calling +    // GetBytes() directly off our returned DataBufferSP if we hit an error. +    lldb::DataBufferSP buf_sp (new DataBufferHeap(1, 0)); + +    // Ideally, we would simply create a FileSpec and call ReadFileContents. +    // However, files in procfs have zero size (since they are, in general, +    // dynamically generated by the kernel) which is incompatible with the +    // current ReadFileContents implementation. Therefore we simply stream the +    // data into a DataBuffer ourselves. +    if (snprintf (path, PATH_MAX, "/proc/%" PRIu64 "/%s", pid, name) > 0) +    { +        if ((fd = open (path, O_RDONLY, 0)) >= 0) +        { +            size_t bytes_read = 0; +            std::unique_ptr<DataBufferHeap> buf_ap(new DataBufferHeap(1024, 0)); + +            for (;;) +            { +                size_t avail = buf_ap->GetByteSize() - bytes_read; +                ssize_t status = read (fd, buf_ap->GetBytes() + bytes_read, avail); + +                if (status < 0) +                    break; + +                if (status == 0) +                { +                    buf_ap->SetByteSize (bytes_read); +                    buf_sp.reset (buf_ap.release()); +                    break; +                } + +                bytes_read += status; + +                if (avail - status == 0) +                    buf_ap->SetByteSize (2 * buf_ap->GetByteSize()); +            } +             +            close (fd); +        } +    } +     +    return buf_sp; +} + +Error +ProcFileReader::ProcessLineByLine (lldb::pid_t pid, const char *name, std::function<bool (const std::string &line)> line_parser) +{ +    Error error; + +    // Try to open the /proc/{pid}/maps entry. +    char filename [PATH_MAX]; +    snprintf (filename, sizeof(filename), "/proc/%" PRIu64 "/%s", pid, name); +    filename[sizeof (filename) - 1] = '\0'; + +    std::ifstream proc_file (filename); +    if (proc_file.fail ()) +    { +        error.SetErrorStringWithFormat ("failed to open file '%s'", filename); +        return error; +    } + +    // Read the file line by line, processing until either end of file or when the line_parser returns false. +    std::string line; +    bool should_continue = true; + +    while (should_continue && std::getline (proc_file, line)) +    { +        // Pass the line over to the line_parser for processing.  If the line_parser returns false, we +        // stop processing. +        should_continue = line_parser (line); +    } + +    return error; +} diff --git a/source/Plugins/Process/Linux/ProcFileReader.h b/source/Plugins/Process/Linux/ProcFileReader.h new file mode 100644 index 000000000000..7b3812433068 --- /dev/null +++ b/source/Plugins/Process/Linux/ProcFileReader.h @@ -0,0 +1,37 @@ +//===-- ProcFileReader.h ----------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_ProcFileReader_h_ +#define liblldb_ProcFileReader_h_ + +#include <functional> + +#include "lldb/lldb-forward.h" +#include "lldb/lldb-types.h" + +namespace lldb_private { +namespace process_linux { + +    class ProcFileReader +    { +    public: + +        static lldb::DataBufferSP +        ReadIntoDataBuffer (lldb::pid_t pid, const char *name); + +        /// Parse the /proc/{@a pid}/{@a name} file line by line, passing each line to line_parser, until +        /// either end of file or until line_parser returns false. +        static Error +        ProcessLineByLine (lldb::pid_t pid, const char *name, std::function<bool (const std::string &line)> line_parser); +    }; + +} // namespace process_linux +} // namespace lldb_private + +#endif // #ifndef liblldb_ProcFileReader_h_ diff --git a/source/Plugins/Process/Linux/Procfs.h b/source/Plugins/Process/Linux/Procfs.h new file mode 100644 index 000000000000..cad433fb095d --- /dev/null +++ b/source/Plugins/Process/Linux/Procfs.h @@ -0,0 +1,31 @@ +//===-- Procfs.h ---------------------------------------------- -*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// source/Plugins/Process/Linux/Procfs.h defines the symbols we need from +// sys/procfs.h on Android/Linux for all supported architectures. + +#include <sys/ptrace.h> + +#ifdef __ANDROID__ +#if defined (__arm64__) || defined (__aarch64__) +typedef unsigned long elf_greg_t; +typedef elf_greg_t elf_gregset_t[(sizeof (struct user_pt_regs) / sizeof(elf_greg_t))]; +typedef struct user_fpsimd_state elf_fpregset_t; +#ifndef NT_FPREGSET +    #define NT_FPREGSET NT_PRFPREG +#endif // NT_FPREGSET +#elif defined (__mips__) +#ifndef NT_FPREGSET +    #define NT_FPREGSET NT_PRFPREG +#endif // NT_FPREGSET +#endif +#else  // __ANDROID__ +#include <sys/procfs.h> +#endif // __ANDROID__ + diff --git a/source/Plugins/Process/MacOSX-Kernel/CMakeLists.txt b/source/Plugins/Process/MacOSX-Kernel/CMakeLists.txt new file mode 100644 index 000000000000..681b7405e2b8 --- /dev/null +++ b/source/Plugins/Process/MacOSX-Kernel/CMakeLists.txt @@ -0,0 +1,10 @@ +add_lldb_library(lldbPluginProcessMacOSXKernel +  CommunicationKDP.cpp +  ProcessKDP.cpp +  ProcessKDPLog.cpp +  RegisterContextKDP_arm.cpp +  RegisterContextKDP_arm64.cpp +  RegisterContextKDP_i386.cpp +  RegisterContextKDP_x86_64.cpp +  ThreadKDP.cpp +  ) diff --git a/source/Plugins/Process/MacOSX-Kernel/CommunicationKDP.cpp b/source/Plugins/Process/MacOSX-Kernel/CommunicationKDP.cpp new file mode 100644 index 000000000000..5c1c3284a35c --- /dev/null +++ b/source/Plugins/Process/MacOSX-Kernel/CommunicationKDP.cpp @@ -0,0 +1,1445 @@ +//===-- CommunicationKDP.cpp ------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + + +#include "CommunicationKDP.h" + +// C Includes +#include <errno.h> +#include <limits.h> +#include <string.h> + +// C++ Includes + +// Other libraries and framework includes +#include "lldb/Core/DataBufferHeap.h" +#include "lldb/Core/DataExtractor.h" +#include "lldb/Core/Log.h" +#include "lldb/Core/State.h" +#include "lldb/Core/UUID.h" +#include "lldb/Host/FileSpec.h" +#include "lldb/Host/Host.h" +#include "lldb/Host/TimeValue.h" +#include "lldb/Target/Process.h" + +// Project includes +#include "ProcessKDPLog.h" + +using namespace lldb; +using namespace lldb_private; + +//---------------------------------------------------------------------- +// CommunicationKDP constructor +//---------------------------------------------------------------------- +CommunicationKDP::CommunicationKDP (const char *comm_name) : +    Communication(comm_name), +    m_addr_byte_size (4), +    m_byte_order (eByteOrderLittle), +    m_packet_timeout (5), +    m_sequence_mutex (Mutex::eMutexTypeRecursive), +    m_is_running (false), +    m_session_key (0u), +    m_request_sequence_id (0u), +    m_exception_sequence_id (0u), +    m_kdp_version_version (0u), +    m_kdp_version_feature (0u), +    m_kdp_hostinfo_cpu_mask (0u), +    m_kdp_hostinfo_cpu_type (0u), +    m_kdp_hostinfo_cpu_subtype (0u) +{ +} + +//---------------------------------------------------------------------- +// Destructor +//---------------------------------------------------------------------- +CommunicationKDP::~CommunicationKDP() +{ +    if (IsConnected()) +    { +        Disconnect(); +    } +} + +bool +CommunicationKDP::SendRequestPacket (const PacketStreamType &request_packet) +{ +    Mutex::Locker locker(m_sequence_mutex); +    return SendRequestPacketNoLock (request_packet); +} + +#if 0 +typedef struct { +	uint8_t     request;	// Either: CommandType | ePacketTypeRequest, or CommandType | ePacketTypeReply +	uint8_t     sequence; +	uint16_t    length;		// Length of entire packet including this header +	uint32_t	key;		// Session key +} kdp_hdr_t; +#endif + +void +CommunicationKDP::MakeRequestPacketHeader (CommandType request_type, +                                           PacketStreamType &request_packet, +                                           uint16_t request_length) +{ +    request_packet.Clear(); +    request_packet.PutHex8 (request_type | ePacketTypeRequest); // Set the request type +    request_packet.PutHex8 (m_request_sequence_id++);           // Sequence number +    request_packet.PutHex16 (request_length);                   // Length of the packet including this header +    request_packet.PutHex32 (m_session_key);                    // Session key +} + +bool +CommunicationKDP::SendRequestAndGetReply (const CommandType command, +                                          const PacketStreamType &request_packet, +                                          DataExtractor &reply_packet) +{ +    if (IsRunning()) +    { +        Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS)); +        if (log) +        { +            PacketStreamType log_strm; +            DumpPacket (log_strm, request_packet.GetData(), request_packet.GetSize()); +            log->Printf("error: kdp running, not sending packet: %.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData()); +        } +        return false; +    } + +    Mutex::Locker locker(m_sequence_mutex); +#ifdef LLDB_CONFIGURATION_DEBUG +    // NOTE: this only works for packets that are in native endian byte order +    assert (request_packet.GetSize() == *((uint16_t *)(request_packet.GetData() + 2))); +#endif +    lldb::offset_t offset = 1; +    const uint32_t num_retries = 3; +    for (uint32_t i=0; i<num_retries; ++i) +    { +        if (SendRequestPacketNoLock(request_packet)) +        { +            const uint8_t request_sequence_id = (uint8_t)request_packet.GetData()[1]; +            while (1) +            { +                if (WaitForPacketWithTimeoutMicroSecondsNoLock (reply_packet, GetPacketTimeoutInMicroSeconds ())) +                { +                    offset = 0; +                    const uint8_t reply_command = reply_packet.GetU8 (&offset); +                    const uint8_t reply_sequence_id = reply_packet.GetU8 (&offset); +                    if (request_sequence_id == reply_sequence_id) +                    { +                        // The sequent ID was correct, now verify we got the response we were looking for +                        if ((reply_command & eCommandTypeMask) == command) +                        { +                            // Success +                            if (command == KDP_RESUMECPUS) +                                m_is_running.SetValue(true, eBroadcastAlways); +                            return true; +                        } +                        else +                        { +                            // Failed to get the correct response, bail +                            reply_packet.Clear(); +                            return false; +                        } +                    } +                    else if (reply_sequence_id > request_sequence_id) +                    { +                        // Sequence ID was greater than the sequence ID of the packet we sent, something +                        // is really wrong... +                        reply_packet.Clear(); +                        return false; +                    } +                    else +                    { +                        // The reply sequence ID was less than our current packet's sequence ID +                        // so we should keep trying to get a response because this was a response +                        // for a previous packet that we must have retried. +                    } +                } +                else +                { +                    // Break and retry sending the packet as we didn't get a response due to timeout +                    break; +                } +            } +        } +    } +    reply_packet.Clear(); +    return false; +} + +bool +CommunicationKDP::SendRequestPacketNoLock (const PacketStreamType &request_packet) +{ +    if (IsConnected()) +    { +        const char *packet_data = request_packet.GetData(); +        const size_t packet_size = request_packet.GetSize(); + +        Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS)); +        if (log) +        { +            PacketStreamType log_strm;             +            DumpPacket (log_strm, packet_data, packet_size); +            log->Printf("%.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData()); +        } +        ConnectionStatus status = eConnectionStatusSuccess; + +        size_t bytes_written = Write (packet_data,  +                                      packet_size,  +                                      status,  +                                      NULL); + +        if (bytes_written == packet_size) +            return true; +         +        if (log) +            log->Printf ("error: failed to send packet entire packet %" PRIu64 " of %" PRIu64 " bytes sent", (uint64_t)bytes_written, (uint64_t)packet_size); +    } +    return false; +} + +bool +CommunicationKDP::GetSequenceMutex (Mutex::Locker& locker) +{ +    return locker.TryLock (m_sequence_mutex); +} + + +bool +CommunicationKDP::WaitForNotRunningPrivate (const TimeValue *timeout_ptr) +{ +    return m_is_running.WaitForValueEqualTo (false, timeout_ptr, NULL); +} + +size_t +CommunicationKDP::WaitForPacketWithTimeoutMicroSeconds (DataExtractor &packet, uint32_t timeout_usec) +{ +    Mutex::Locker locker(m_sequence_mutex); +    return WaitForPacketWithTimeoutMicroSecondsNoLock (packet, timeout_usec); +} + +size_t +CommunicationKDP::WaitForPacketWithTimeoutMicroSecondsNoLock (DataExtractor &packet, uint32_t timeout_usec) +{ +    uint8_t buffer[8192]; +    Error error; + +    Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS | KDP_LOG_VERBOSE)); + +    // Check for a packet from our cache first without trying any reading... +    if (CheckForPacket (NULL, 0, packet)) +        return packet.GetByteSize(); + +    bool timed_out = false; +    while (IsConnected() && !timed_out) +    { +        lldb::ConnectionStatus status = eConnectionStatusNoConnection; +        size_t bytes_read = Read (buffer, sizeof(buffer), timeout_usec, status, &error); +         +        if (log) +            log->Printf ("%s: Read (buffer, (sizeof(buffer), timeout_usec = 0x%x, status = %s, error = %s) => bytes_read = %" PRIu64, +                         __PRETTY_FUNCTION__, +                         timeout_usec,  +                         Communication::ConnectionStatusAsCString (status), +                         error.AsCString(),  +                         (uint64_t)bytes_read); + +        if (bytes_read > 0) +        { +            if (CheckForPacket (buffer, bytes_read, packet)) +                return packet.GetByteSize(); +        } +        else +        { +            switch (status) +            { +            case eConnectionStatusInterrupted: +            case eConnectionStatusTimedOut: +                timed_out = true; +                break; +            case eConnectionStatusSuccess: +                //printf ("status = success but error = %s\n", error.AsCString("<invalid>")); +                break; +                 +            case eConnectionStatusEndOfFile: +            case eConnectionStatusNoConnection: +            case eConnectionStatusLostConnection: +            case eConnectionStatusError: +                Disconnect(); +                break; +            } +        } +    } +    packet.Clear ();     +    return 0; +} + +bool +CommunicationKDP::CheckForPacket (const uint8_t *src, size_t src_len, DataExtractor &packet) +{ +    // Put the packet data into the buffer in a thread safe fashion +    Mutex::Locker locker(m_bytes_mutex); +     +    Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS)); + +    if (src && src_len > 0) +    { +        if (log && log->GetVerbose()) +        { +            PacketStreamType log_strm; +            DataExtractor::DumpHexBytes (&log_strm, src, src_len, UINT32_MAX, LLDB_INVALID_ADDRESS); +            log->Printf ("CommunicationKDP::%s adding %u bytes: %s", +                         __FUNCTION__,  +                         (uint32_t)src_len,  +                         log_strm.GetData()); +        } +        m_bytes.append ((const char *)src, src_len); +    } + +    // Make sure we at least have enough bytes for a packet header +    const size_t bytes_available = m_bytes.size(); +    if (bytes_available >= 8) +    { +        packet.SetData (&m_bytes[0], bytes_available, m_byte_order); +        lldb::offset_t offset = 0; +        uint8_t reply_command = packet.GetU8(&offset); +        switch (reply_command) +        { +        case ePacketTypeRequest | KDP_EXCEPTION: +        case ePacketTypeRequest | KDP_TERMINATION: +            // We got an exception request, so be sure to send an ACK +            { +                PacketStreamType request_ack_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); +                // Set the reply but and make the ACK packet +                request_ack_packet.PutHex8 (reply_command | ePacketTypeReply); +                request_ack_packet.PutHex8 (packet.GetU8(&offset)); +                request_ack_packet.PutHex16 (packet.GetU16(&offset)); +                request_ack_packet.PutHex32 (packet.GetU32(&offset)); +                m_is_running.SetValue(false, eBroadcastAlways); +                // Ack to the exception or termination +                SendRequestPacketNoLock (request_ack_packet); +            } +            // Fall through to case below to get packet contents +        case ePacketTypeReply | KDP_CONNECT: +        case ePacketTypeReply | KDP_DISCONNECT: +        case ePacketTypeReply | KDP_HOSTINFO: +        case ePacketTypeReply | KDP_VERSION: +        case ePacketTypeReply | KDP_MAXBYTES: +        case ePacketTypeReply | KDP_READMEM: +        case ePacketTypeReply | KDP_WRITEMEM: +        case ePacketTypeReply | KDP_READREGS: +        case ePacketTypeReply | KDP_WRITEREGS: +        case ePacketTypeReply | KDP_LOAD: +        case ePacketTypeReply | KDP_IMAGEPATH: +        case ePacketTypeReply | KDP_SUSPEND: +        case ePacketTypeReply | KDP_RESUMECPUS: +        case ePacketTypeReply | KDP_BREAKPOINT_SET: +        case ePacketTypeReply | KDP_BREAKPOINT_REMOVE: +        case ePacketTypeReply | KDP_REGIONS: +        case ePacketTypeReply | KDP_REATTACH: +        case ePacketTypeReply | KDP_HOSTREBOOT: +        case ePacketTypeReply | KDP_READMEM64: +        case ePacketTypeReply | KDP_WRITEMEM64: +        case ePacketTypeReply | KDP_BREAKPOINT_SET64: +        case ePacketTypeReply | KDP_BREAKPOINT_REMOVE64: +        case ePacketTypeReply | KDP_KERNELVERSION: +        case ePacketTypeReply | KDP_READPHYSMEM64: +        case ePacketTypeReply | KDP_WRITEPHYSMEM64: +        case ePacketTypeReply | KDP_READIOPORT: +        case ePacketTypeReply | KDP_WRITEIOPORT: +        case ePacketTypeReply | KDP_READMSR64: +        case ePacketTypeReply | KDP_WRITEMSR64: +        case ePacketTypeReply | KDP_DUMPINFO: +            { +                offset = 2; +                const uint16_t length = packet.GetU16 (&offset); +                if (length <= bytes_available) +                { +                    // We have an entire packet ready, we need to copy the data +                    // bytes into a buffer that will be owned by the packet and +                    // erase the bytes from our communcation buffer "m_bytes" +                    packet.SetData (DataBufferSP (new DataBufferHeap (&m_bytes[0], length))); +                    m_bytes.erase (0, length); +                     +                    if (log) +                    { +                        PacketStreamType log_strm; +                        DumpPacket (log_strm, packet); +                         +                        log->Printf("%.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData()); +                    } +                    return true; +                } +            } +            break; + +        default: +            // Unrecognized reply command byte, erase this byte and try to get back on track +            if (log) +                log->Printf ("CommunicationKDP::%s: tossing junk byte: 0x%2.2x",  +                             __FUNCTION__,  +                             (uint8_t)m_bytes[0]); +            m_bytes.erase(0, 1); +            break; +        } +    } +    packet.Clear(); +    return false; +} + + +bool +CommunicationKDP::SendRequestConnect (uint16_t reply_port,  +                                      uint16_t exc_port,  +                                      const char *greeting) +{ +    PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); +    if (greeting == NULL) +        greeting = ""; + +    const CommandType command = KDP_CONNECT; +    // Length is 82 uint16_t and the length of the greeting C string with the terminating NULL +    const uint32_t command_length = 8 + 2 + 2 + ::strlen(greeting) + 1; +    MakeRequestPacketHeader (command, request_packet, command_length); +    // Always send connect ports as little endian +    request_packet.SetByteOrder (eByteOrderLittle); +    request_packet.PutHex16 (htons(reply_port)); +    request_packet.PutHex16 (htons(exc_port)); +    request_packet.SetByteOrder (m_byte_order); +    request_packet.PutCString (greeting); +    DataExtractor reply_packet; +    return SendRequestAndGetReply (command, request_packet, reply_packet); +} + +void +CommunicationKDP::ClearKDPSettings () +{ +    m_request_sequence_id = 0; +    m_kdp_version_version = 0; +    m_kdp_version_feature = 0; +    m_kdp_hostinfo_cpu_mask = 0; +    m_kdp_hostinfo_cpu_type = 0; +    m_kdp_hostinfo_cpu_subtype = 0; +} + +bool +CommunicationKDP::SendRequestReattach (uint16_t reply_port) +{ +    PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); +    const CommandType command = KDP_REATTACH; +    // Length is 8 bytes for the header plus 2 bytes for the reply UDP port +    const uint32_t command_length = 8 + 2; +    MakeRequestPacketHeader (command, request_packet, command_length); +    // Always send connect ports as little endian +    request_packet.SetByteOrder (eByteOrderLittle); +    request_packet.PutHex16(htons(reply_port)); +    request_packet.SetByteOrder (m_byte_order); +    DataExtractor reply_packet; +    if (SendRequestAndGetReply (command, request_packet, reply_packet)) +    { +        // Reset the sequence ID to zero for reattach +        ClearKDPSettings (); +        lldb::offset_t offset = 4; +        m_session_key = reply_packet.GetU32 (&offset); +        return true; +    } +    return false; +} + +uint32_t +CommunicationKDP::GetVersion () +{ +    if (!VersionIsValid()) +        SendRequestVersion(); +    return m_kdp_version_version; +} + +uint32_t +CommunicationKDP::GetFeatureFlags () +{ +    if (!VersionIsValid()) +        SendRequestVersion(); +    return m_kdp_version_feature; +} + +bool +CommunicationKDP::SendRequestVersion () +{ +    PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); +    const CommandType command = KDP_VERSION; +    const uint32_t command_length = 8; +    MakeRequestPacketHeader (command, request_packet, command_length); +    DataExtractor reply_packet; +    if (SendRequestAndGetReply (command, request_packet, reply_packet)) +    { +        lldb::offset_t offset = 8; +        m_kdp_version_version = reply_packet.GetU32 (&offset); +        m_kdp_version_feature = reply_packet.GetU32 (&offset); +        return true; +    } +    return false; +} + +#if 0 // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection... +const char * +CommunicationKDP::GetImagePath () +{ +    if (m_image_path.empty()) +        SendRequestImagePath(); +    return m_image_path.c_str(); +} + +bool +CommunicationKDP::SendRequestImagePath () +{ +    PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); +    const CommandType command = KDP_IMAGEPATH; +    const uint32_t command_length = 8; +    MakeRequestPacketHeader (command, request_packet, command_length); +    DataExtractor reply_packet; +    if (SendRequestAndGetReply (command, request_packet, reply_packet)) +    { +        const char *path = reply_packet.PeekCStr(8); +        if (path && path[0]) +            m_kernel_version.assign (path); +        return true; +    } +    return false; +} +#endif + +uint32_t +CommunicationKDP::GetCPUMask () +{ +    if (!HostInfoIsValid()) +        SendRequestHostInfo(); +    return m_kdp_hostinfo_cpu_mask; +} + +uint32_t +CommunicationKDP::GetCPUType () +{ +    if (!HostInfoIsValid()) +        SendRequestHostInfo(); +    return m_kdp_hostinfo_cpu_type; +} + +uint32_t +CommunicationKDP::GetCPUSubtype () +{ +    if (!HostInfoIsValid()) +        SendRequestHostInfo(); +    return m_kdp_hostinfo_cpu_subtype; +} + +lldb_private::UUID +CommunicationKDP::GetUUID () +{ +    UUID uuid; +    if (GetKernelVersion() == NULL) +        return uuid; + +    if (m_kernel_version.find("UUID=") == std::string::npos) +        return uuid; + +    size_t p = m_kernel_version.find("UUID=") + strlen ("UUID="); +    std::string uuid_str = m_kernel_version.substr(p, 36); +    if (uuid_str.size() < 32) +        return uuid; + +    if (uuid.SetFromCString (uuid_str.c_str()) == 0) +    { +        UUID invalid_uuid; +        return invalid_uuid; +    } + +    return uuid; +} + +bool +CommunicationKDP::RemoteIsEFI () +{ +    if (GetKernelVersion() == NULL) +        return false; +    if (strncmp (m_kernel_version.c_str(), "EFI", 3) == 0) +        return true; +    else +        return false; +} + +bool +CommunicationKDP::RemoteIsDarwinKernel () +{ +    if (GetKernelVersion() == NULL) +        return false; +    if (m_kernel_version.find("Darwin Kernel") != std::string::npos) +        return true; +    else +        return false; +} + +lldb::addr_t +CommunicationKDP::GetLoadAddress () +{ +    if (GetKernelVersion() == NULL) +        return LLDB_INVALID_ADDRESS; + +    if (m_kernel_version.find("stext=") == std::string::npos) +        return LLDB_INVALID_ADDRESS; +    size_t p = m_kernel_version.find("stext=") + strlen ("stext="); +    if (m_kernel_version[p] != '0' || m_kernel_version[p + 1] != 'x') +        return LLDB_INVALID_ADDRESS; + +    addr_t kernel_load_address; +    errno = 0; +    kernel_load_address = ::strtoul (m_kernel_version.c_str() + p, NULL, 16); +    if (errno != 0 || kernel_load_address == 0) +        return LLDB_INVALID_ADDRESS; + +    return kernel_load_address; +} + +bool +CommunicationKDP::SendRequestHostInfo () +{ +    PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); +    const CommandType command = KDP_HOSTINFO; +    const uint32_t command_length = 8; +    MakeRequestPacketHeader (command, request_packet, command_length); +    DataExtractor reply_packet; +    if (SendRequestAndGetReply (command, request_packet, reply_packet)) +    { +        lldb::offset_t offset = 8; +        m_kdp_hostinfo_cpu_mask     = reply_packet.GetU32 (&offset); +        m_kdp_hostinfo_cpu_type     = reply_packet.GetU32 (&offset); +        m_kdp_hostinfo_cpu_subtype  = reply_packet.GetU32 (&offset); +         +        ArchSpec kernel_arch; +        kernel_arch.SetArchitecture (eArchTypeMachO,  +                                     m_kdp_hostinfo_cpu_type,  +                                     m_kdp_hostinfo_cpu_subtype); +     +        m_addr_byte_size = kernel_arch.GetAddressByteSize(); +        m_byte_order = kernel_arch.GetByteOrder(); +        return true; +    } +    return false; +} + +const char * +CommunicationKDP::GetKernelVersion () +{ +    if (m_kernel_version.empty()) +        SendRequestKernelVersion (); +    return m_kernel_version.c_str(); +} + +bool +CommunicationKDP::SendRequestKernelVersion () +{ +    PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); +    const CommandType command = KDP_KERNELVERSION; +    const uint32_t command_length = 8; +    MakeRequestPacketHeader (command, request_packet, command_length); +    DataExtractor reply_packet; +    if (SendRequestAndGetReply (command, request_packet, reply_packet)) +    { +        const char *kernel_version_cstr = reply_packet.PeekCStr(8); +        if (kernel_version_cstr && kernel_version_cstr[0]) +            m_kernel_version.assign (kernel_version_cstr); +        return true; +    } +    return false; +} + +bool +CommunicationKDP::SendRequestDisconnect () +{ +    PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); +    const CommandType command = KDP_DISCONNECT; +    const uint32_t command_length = 8; +    MakeRequestPacketHeader (command, request_packet, command_length); +    DataExtractor reply_packet; +    if (SendRequestAndGetReply (command, request_packet, reply_packet)) +    { +        // Are we supposed to get a reply for disconnect? +    } +    ClearKDPSettings (); +    return true; +} + +uint32_t +CommunicationKDP::SendRequestReadMemory (lldb::addr_t addr,  +                                         void *dst,  +                                         uint32_t dst_len, +                                         Error &error) +{ +    PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); +    bool use_64 = (GetVersion() >= 11); +    uint32_t command_addr_byte_size = use_64 ? 8 : 4; +    const CommandType command = use_64 ? KDP_READMEM64 : KDP_READMEM; +    // Size is header + address size + uint32_t length +    const uint32_t command_length = 8 + command_addr_byte_size + 4; +    MakeRequestPacketHeader (command, request_packet, command_length); +    request_packet.PutMaxHex64 (addr, command_addr_byte_size); +    request_packet.PutHex32 (dst_len); +    DataExtractor reply_packet; +    if (SendRequestAndGetReply (command, request_packet, reply_packet)) +    { +        lldb::offset_t offset = 8; +        uint32_t kdp_error = reply_packet.GetU32 (&offset); +        uint32_t src_len = reply_packet.GetByteSize() - 12; +         +        if (src_len > 0) +        { +            const void *src = reply_packet.GetData(&offset, src_len); +            if (src) +            { +                ::memcpy (dst, src, src_len); +                error.Clear(); +                return src_len; +            } +        } +        if (kdp_error) +            error.SetErrorStringWithFormat ("kdp read memory failed (error %u)", kdp_error); +        else +            error.SetErrorString ("kdp read memory failed"); +    } +    else +    { +        error.SetErrorString ("failed to send packet"); +    } +    return 0; +} + + +uint32_t +CommunicationKDP::SendRequestWriteMemory (lldb::addr_t addr,  +                                          const void *src,  +                                          uint32_t src_len, +                                          Error &error) +{ +    PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); +    bool use_64 = (GetVersion() >= 11); +    uint32_t command_addr_byte_size = use_64 ? 8 : 4; +    const CommandType command = use_64 ? KDP_WRITEMEM64 : KDP_WRITEMEM; +    // Size is header + address size + uint32_t length +    const uint32_t command_length = 8 + command_addr_byte_size + 4 + src_len; +    MakeRequestPacketHeader (command, request_packet, command_length); +    request_packet.PutMaxHex64 (addr, command_addr_byte_size); +    request_packet.PutHex32 (src_len); +    request_packet.PutRawBytes(src, src_len); + +    DataExtractor reply_packet; +    if (SendRequestAndGetReply (command, request_packet, reply_packet)) +    { +        lldb::offset_t offset = 8; +        uint32_t kdp_error = reply_packet.GetU32 (&offset); +        if (kdp_error) +            error.SetErrorStringWithFormat ("kdp write memory failed (error %u)", kdp_error); +        else +        { +            error.Clear(); +            return src_len; +        } +    } +    else +    { +        error.SetErrorString ("failed to send packet"); +    } +    return 0; +} + +bool +CommunicationKDP::SendRawRequest (uint8_t command_byte, +                                  const void *src,  // Raw packet payload bytes +                                  uint32_t src_len, // Raw packet payload length +                                  DataExtractor &reply_packet, +                                  Error &error) +{ +    PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); +    // Size is header + address size + uint32_t length +    const uint32_t command_length = 8 + src_len; +    const CommandType command = (CommandType)command_byte; +    MakeRequestPacketHeader (command, request_packet, command_length); +    request_packet.PutRawBytes(src, src_len); +     +    if (SendRequestAndGetReply (command, request_packet, reply_packet)) +    { +        lldb::offset_t offset = 8; +        uint32_t kdp_error = reply_packet.GetU32 (&offset); +        if (kdp_error  && (command_byte != KDP_DUMPINFO)) +            error.SetErrorStringWithFormat ("request packet 0x%8.8x failed (error %u)", command_byte, kdp_error); +        else +        { +            error.Clear(); +            return true; +        } +    } +    else +    { +        error.SetErrorString ("failed to send packet"); +    } +    return false; +} + + +const char * +CommunicationKDP::GetCommandAsCString (uint8_t command) +{ +    switch (command) +    { +    case KDP_CONNECT:               return "KDP_CONNECT"; +    case KDP_DISCONNECT:            return "KDP_DISCONNECT"; +    case KDP_HOSTINFO:              return "KDP_HOSTINFO"; +    case KDP_VERSION:               return "KDP_VERSION"; +    case KDP_MAXBYTES:              return "KDP_MAXBYTES"; +    case KDP_READMEM:               return "KDP_READMEM"; +    case KDP_WRITEMEM:              return "KDP_WRITEMEM"; +    case KDP_READREGS:              return "KDP_READREGS"; +    case KDP_WRITEREGS:             return "KDP_WRITEREGS"; +    case KDP_LOAD:                  return "KDP_LOAD"; +    case KDP_IMAGEPATH:             return "KDP_IMAGEPATH"; +    case KDP_SUSPEND:               return "KDP_SUSPEND"; +    case KDP_RESUMECPUS:            return "KDP_RESUMECPUS"; +    case KDP_EXCEPTION:             return "KDP_EXCEPTION"; +    case KDP_TERMINATION:           return "KDP_TERMINATION"; +    case KDP_BREAKPOINT_SET:        return "KDP_BREAKPOINT_SET"; +    case KDP_BREAKPOINT_REMOVE:     return "KDP_BREAKPOINT_REMOVE"; +    case KDP_REGIONS:               return "KDP_REGIONS"; +    case KDP_REATTACH:              return "KDP_REATTACH"; +    case KDP_HOSTREBOOT:            return "KDP_HOSTREBOOT"; +    case KDP_READMEM64:             return "KDP_READMEM64"; +    case KDP_WRITEMEM64:            return "KDP_WRITEMEM64"; +    case KDP_BREAKPOINT_SET64:      return "KDP_BREAKPOINT64_SET"; +    case KDP_BREAKPOINT_REMOVE64:   return "KDP_BREAKPOINT64_REMOVE"; +    case KDP_KERNELVERSION:         return "KDP_KERNELVERSION"; +    case KDP_READPHYSMEM64:         return "KDP_READPHYSMEM64"; +    case KDP_WRITEPHYSMEM64:        return "KDP_WRITEPHYSMEM64"; +    case KDP_READIOPORT:            return "KDP_READIOPORT"; +    case KDP_WRITEIOPORT:           return "KDP_WRITEIOPORT"; +    case KDP_READMSR64:             return "KDP_READMSR64"; +    case KDP_WRITEMSR64:            return "KDP_WRITEMSR64"; +    case KDP_DUMPINFO:              return "KDP_DUMPINFO"; +    } +    return NULL; +} + +void +CommunicationKDP::DumpPacket (Stream &s, const void *data, uint32_t data_len) +{ +    DataExtractor extractor (data, data_len, m_byte_order, m_addr_byte_size); +    DumpPacket (s, extractor); +} + +void +CommunicationKDP::DumpPacket (Stream &s, const DataExtractor& packet) +{ +    const char *error_desc = NULL; +    if (packet.GetByteSize() < 8) +    { +        error_desc = "error: invalid packet (too short): "; +    } +    else +    { +        lldb::offset_t offset = 0; +        const uint8_t first_packet_byte = packet.GetU8 (&offset); +        const uint8_t sequence_id = packet.GetU8 (&offset); +        const uint16_t length = packet.GetU16 (&offset); +        const uint32_t key = packet.GetU32 (&offset); +        const CommandType command = ExtractCommand (first_packet_byte); +        const char *command_name = GetCommandAsCString (command); +        if (command_name) +        { +            const bool is_reply = ExtractIsReply(first_packet_byte); +            s.Printf ("(running=%i) %s %24s: 0x%2.2x 0x%2.2x 0x%4.4x 0x%8.8x ", +                      IsRunning(), +                      is_reply ? "<--" : "-->", +                      command_name, +                      first_packet_byte, +                      sequence_id, +                      length, +                      key); +             +            if (is_reply) +            { +                // Dump request reply packets +                switch (command) +                { +                    // Commands that return a single 32 bit error +                    case KDP_CONNECT: +                    case KDP_WRITEMEM: +                    case KDP_WRITEMEM64: +                    case KDP_BREAKPOINT_SET: +                    case KDP_BREAKPOINT_REMOVE: +                    case KDP_BREAKPOINT_SET64: +                    case KDP_BREAKPOINT_REMOVE64: +                    case KDP_WRITEREGS: +                    case KDP_LOAD: +                    case KDP_WRITEIOPORT: +                    case KDP_WRITEMSR64: +                        { +                            const uint32_t error = packet.GetU32 (&offset); +                            s.Printf(" (error=0x%8.8x)", error); +                        } +                        break; +                     +                    case KDP_DISCONNECT: +                    case KDP_REATTACH: +                    case KDP_HOSTREBOOT: +                    case KDP_SUSPEND: +                    case KDP_RESUMECPUS: +                    case KDP_EXCEPTION: +                    case KDP_TERMINATION: +                        // No return value for the reply, just the header to ack +                        s.PutCString(" ()"); +                        break; + +                    case KDP_HOSTINFO: +                        { +                            const uint32_t cpu_mask = packet.GetU32 (&offset); +                            const uint32_t cpu_type = packet.GetU32 (&offset); +                            const uint32_t cpu_subtype = packet.GetU32 (&offset); +                            s.Printf(" (cpu_mask=0x%8.8x, cpu_type=0x%8.8x, cpu_subtype=0x%8.8x)", cpu_mask, cpu_type, cpu_subtype); +                        } +                        break; +                         +                    case KDP_VERSION: +                        { +                            const uint32_t version = packet.GetU32 (&offset); +                            const uint32_t feature = packet.GetU32 (&offset); +                            s.Printf(" (version=0x%8.8x, feature=0x%8.8x)", version, feature); +                        } +                        break; +                         +                    case KDP_REGIONS: +                        { +                            const uint32_t region_count = packet.GetU32 (&offset); +                            s.Printf(" (count = %u", region_count);  +                            for (uint32_t i=0; i<region_count; ++i) +                            { +                                const addr_t region_addr = packet.GetPointer (&offset); +                                const uint32_t region_size = packet.GetU32 (&offset); +                                const uint32_t region_prot = packet.GetU32 (&offset); +                                s.Printf("\n\tregion[%" PRIu64 "] = { range = [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), size = 0x%8.8x, prot = %s }", region_addr, region_addr, region_addr + region_size, region_size, GetPermissionsAsCString (region_prot)); +                            } +                        } +                        break; + +                    case KDP_READMEM: +                    case KDP_READMEM64: +                    case KDP_READPHYSMEM64: +                        { +                            const uint32_t error = packet.GetU32 (&offset); +                            const uint32_t count = packet.GetByteSize() - offset; +                            s.Printf(" (error = 0x%8.8x:\n", error);  +                            if (count > 0) +                                packet.Dump (&s,                        // Stream to dump to +                                             offset,                    // Offset within "packet" +                                             eFormatBytesWithASCII,     // Format to use +                                             1,                         // Size of each item in bytes +                                             count,                     // Number of items +                                             16,                        // Number per line +                                             m_last_read_memory_addr,   // Don't show addresses before each line +                                             0, 0);                     // No bitfields +                        } +                        break; + +                    case KDP_READREGS: +                        { +                            const uint32_t error = packet.GetU32 (&offset); +                            const uint32_t count = packet.GetByteSize() - offset; +                            s.Printf(" (error = 0x%8.8x regs:\n", error);  +                            if (count > 0) +                                packet.Dump (&s,                        // Stream to dump to +                                             offset,                    // Offset within "packet" +                                             eFormatHex,                // Format to use +                                             m_addr_byte_size,          // Size of each item in bytes +                                             count / m_addr_byte_size,  // Number of items +                                             16 / m_addr_byte_size,     // Number per line +                                             LLDB_INVALID_ADDRESS,      // Don't show addresses before each line +                                             0, 0);                     // No bitfields +                        } +                        break; + +                    case KDP_KERNELVERSION: +                        { +                            const char *kernel_version = packet.PeekCStr(8); +                            s.Printf(" (version = \"%s\")", kernel_version); +                        } +                        break; +                         +                    case KDP_MAXBYTES: +                        { +                            const uint32_t max_bytes = packet.GetU32 (&offset); +                            s.Printf(" (max_bytes = 0x%8.8x (%u))", max_bytes, max_bytes); +                        } +                        break; +                    case KDP_IMAGEPATH: +                        { +                            const char *path = packet.GetCStr(&offset); +                            s.Printf(" (path = \"%s\")", path); +                        } +                        break; + +                    case KDP_READIOPORT: +                    case KDP_READMSR64: +                        { +                            const uint32_t error = packet.GetU32 (&offset); +                            const uint32_t count = packet.GetByteSize() - offset; +                            s.Printf(" (error = 0x%8.8x io:\n", error);  +                            if (count > 0) +                                packet.Dump (&s,                        // Stream to dump to +                                             offset,                    // Offset within "packet" +                                             eFormatHex,                // Format to use +                                             1,                         // Size of each item in bytes +                                             count,                     // Number of items +                                             16,                        // Number per line +                                             LLDB_INVALID_ADDRESS,      // Don't show addresses before each line +                                             0, 0);                     // No bitfields +                        } +                        break; +                    case KDP_DUMPINFO: +                        { +                            const uint32_t count = packet.GetByteSize() - offset; +                            s.Printf(" (count = %u, bytes = \n", count); +                            if (count > 0) +                                packet.Dump (&s,                        // Stream to dump to +                                             offset,                    // Offset within "packet" +                                             eFormatHex,                // Format to use +                                             1,                         // Size of each item in bytes +                                             count,                     // Number of items +                                             16,                        // Number per line +                                             LLDB_INVALID_ADDRESS,      // Don't show addresses before each line +                                             0, 0);                     // No bitfields +                             +                        } +                        break; +                         +                    default: +                        s.Printf(" (add support for dumping this packet reply!!!");  +                        break; +                     +                }  +            } +            else +            { +                // Dump request packets +                switch (command) +                { +                    case KDP_CONNECT:                +                        { +                            const uint16_t reply_port = ntohs(packet.GetU16 (&offset)); +                            const uint16_t exc_port = ntohs(packet.GetU16 (&offset)); +                            s.Printf(" (reply_port = %u, exc_port = %u, greeting = \"%s\")", reply_port, exc_port, packet.GetCStr(&offset)); +                        } +                        break; +                                  +                    case KDP_DISCONNECT: +                    case KDP_HOSTREBOOT: +                    case KDP_HOSTINFO: +                    case KDP_VERSION: +                    case KDP_REGIONS: +                    case KDP_KERNELVERSION: +                    case KDP_MAXBYTES: +                    case KDP_IMAGEPATH: +                    case KDP_SUSPEND: +                        // No args, just the header in the request... +                        s.PutCString(" ()"); +                        break; + +                    case KDP_RESUMECPUS: +                        { +                            const uint32_t cpu_mask = packet.GetU32 (&offset); +                            s.Printf(" (cpu_mask = 0x%8.8x)", cpu_mask); +                        } +                        break; + +                    case KDP_READMEM: +                        { +                            const uint32_t addr = packet.GetU32 (&offset); +                            const uint32_t size = packet.GetU32 (&offset); +                            s.Printf(" (addr = 0x%8.8x, size = %u)", addr, size); +                            m_last_read_memory_addr = addr; +                        } +                        break; + +                    case KDP_WRITEMEM: +                        { +                            const uint32_t addr = packet.GetU32 (&offset); +                            const uint32_t size = packet.GetU32 (&offset); +                            s.Printf(" (addr = 0x%8.8x, size = %u, bytes = \n", addr, size); +                            if (size > 0) +                                DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr); +                        } +                        break; + +                    case KDP_READMEM64: +                        { +                            const uint64_t addr = packet.GetU64 (&offset); +                            const uint32_t size = packet.GetU32 (&offset); +                            s.Printf(" (addr = 0x%16.16" PRIx64 ", size = %u)", addr, size); +                            m_last_read_memory_addr = addr; +                        } +                        break; + +                    case KDP_READPHYSMEM64: +                        { +                            const uint64_t addr = packet.GetU64 (&offset); +                            const uint32_t size = packet.GetU32 (&offset); +                            const uint32_t lcpu = packet.GetU16 (&offset); +                            s.Printf(" (addr = 0x%16.16llx, size = %u, lcpu = %u)", addr, size, lcpu); +                            m_last_read_memory_addr = addr; +                        } +                        break; + +                    case KDP_WRITEMEM64: +                        { +                            const uint64_t addr = packet.GetU64 (&offset); +                            const uint32_t size = packet.GetU32 (&offset); +                            s.Printf(" (addr = 0x%16.16" PRIx64 ", size = %u, bytes = \n", addr, size); +                            if (size > 0) +                                DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr); +                        } +                        break; + +                    case KDP_WRITEPHYSMEM64: +                        { +                            const uint64_t addr = packet.GetU64 (&offset); +                            const uint32_t size = packet.GetU32 (&offset); +                            const uint32_t lcpu = packet.GetU16 (&offset); +                            s.Printf(" (addr = 0x%16.16llx, size = %u, lcpu = %u, bytes = \n", addr, size, lcpu); +                            if (size > 0) +                                DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr); +                        } +                        break; + +                    case KDP_READREGS: +                        { +                            const uint32_t cpu = packet.GetU32 (&offset); +                            const uint32_t flavor = packet.GetU32 (&offset); +                            s.Printf(" (cpu = %u, flavor = %u)", cpu, flavor); +                        } +                        break; + +                    case KDP_WRITEREGS: +                        { +                            const uint32_t cpu = packet.GetU32 (&offset); +                            const uint32_t flavor = packet.GetU32 (&offset); +                            const uint32_t nbytes = packet.GetByteSize() - offset; +                            s.Printf(" (cpu = %u, flavor = %u, regs = \n", cpu, flavor); +                            if (nbytes > 0) +                                packet.Dump (&s,                        // Stream to dump to +                                             offset,                    // Offset within "packet" +                                             eFormatHex,                // Format to use +                                             m_addr_byte_size,          // Size of each item in bytes +                                             nbytes / m_addr_byte_size, // Number of items +                                             16 / m_addr_byte_size,     // Number per line +                                             LLDB_INVALID_ADDRESS,      // Don't show addresses before each line +                                             0, 0);                     // No bitfields +                        } +                        break; + + +                    case KDP_BREAKPOINT_SET: +                    case KDP_BREAKPOINT_REMOVE: +                        { +                            const uint32_t addr = packet.GetU32 (&offset); +                            s.Printf(" (addr = 0x%8.8x)", addr); +                        } +                        break; + +                    case KDP_BREAKPOINT_SET64: +                    case KDP_BREAKPOINT_REMOVE64: +                        { +                            const uint64_t addr = packet.GetU64 (&offset); +                            s.Printf(" (addr = 0x%16.16" PRIx64 ")", addr); +                        } +                        break; + + +                    case KDP_LOAD: +                        { +                            const char *path = packet.GetCStr(&offset); +                            s.Printf(" (path = \"%s\")", path); +                        } +                        break; + +                    case KDP_EXCEPTION: +                        { +                            const uint32_t count = packet.GetU32 (&offset); +                             +                            for (uint32_t i=0; i<count; ++i) +                            { +                                const uint32_t cpu = packet.GetU32 (&offset); +                                const uint32_t exc = packet.GetU32 (&offset); +                                const uint32_t code = packet.GetU32 (&offset); +                                const uint32_t subcode = packet.GetU32 (&offset); +                                const char *exc_cstr = NULL; +                                switch (exc) +                                { +                                    case 1:  exc_cstr = "EXC_BAD_ACCESS"; break; +                                    case 2:  exc_cstr = "EXC_BAD_INSTRUCTION"; break; +                                    case 3:  exc_cstr = "EXC_ARITHMETIC"; break; +                                    case 4:  exc_cstr = "EXC_EMULATION"; break; +                                    case 5:  exc_cstr = "EXC_SOFTWARE"; break; +                                    case 6:  exc_cstr = "EXC_BREAKPOINT"; break; +                                    case 7:  exc_cstr = "EXC_SYSCALL"; break; +                                    case 8:  exc_cstr = "EXC_MACH_SYSCALL"; break; +                                    case 9:  exc_cstr = "EXC_RPC_ALERT"; break; +                                    case 10: exc_cstr = "EXC_CRASH"; break; +                                    default: +                                        break; +                                } + +                                s.Printf ("{ cpu = 0x%8.8x, exc = %s (%u), code = %u (0x%8.8x), subcode = %u (0x%8.8x)} ",  +                                          cpu, exc_cstr, exc, code, code, subcode, subcode); +                            } +                        } +                        break; + +                    case KDP_TERMINATION: +                        { +                            const uint32_t term_code = packet.GetU32 (&offset); +                            const uint32_t exit_code = packet.GetU32 (&offset); +                            s.Printf(" (term_code = 0x%8.8x (%u), exit_code = 0x%8.8x (%u))", term_code, term_code, exit_code, exit_code); +                        } +                        break; + +                    case KDP_REATTACH: +                        { +                            const uint16_t reply_port = ntohs(packet.GetU16 (&offset)); +                            s.Printf(" (reply_port = %u)", reply_port); +                        } +                        break; + +                    case KDP_READMSR64: +                        { +                            const uint32_t address = packet.GetU32 (&offset); +                            const uint16_t lcpu = packet.GetU16 (&offset); +                            s.Printf(" (address=0x%8.8x, lcpu=0x%4.4x)", address, lcpu); +                        } +                        break; + +                    case KDP_WRITEMSR64: +                        { +                            const uint32_t address = packet.GetU32 (&offset); +                            const uint16_t lcpu = packet.GetU16 (&offset); +                            const uint32_t nbytes = packet.GetByteSize() - offset; +                            s.Printf(" (address=0x%8.8x, lcpu=0x%4.4x, nbytes=0x%8.8x)", lcpu, address, nbytes); +                            if (nbytes > 0) +                                packet.Dump (&s,                        // Stream to dump to +                                             offset,                    // Offset within "packet" +                                             eFormatHex,                // Format to use +                                             1,                         // Size of each item in bytes +                                             nbytes,                    // Number of items +                                             16,                        // Number per line +                                             LLDB_INVALID_ADDRESS,      // Don't show addresses before each line +                                             0, 0);                     // No bitfields +                        } +                        break; + +                    case KDP_READIOPORT: +                        { +                            const uint16_t lcpu = packet.GetU16 (&offset); +                            const uint16_t address = packet.GetU16 (&offset); +                            const uint16_t nbytes = packet.GetU16 (&offset); +                            s.Printf(" (lcpu=0x%4.4x, address=0x%4.4x, nbytes=%u)", lcpu, address, nbytes); +                        } +                        break; + +                    case KDP_WRITEIOPORT: +                         { +                            const uint16_t lcpu = packet.GetU16 (&offset); +                            const uint16_t address = packet.GetU16 (&offset); +                            const uint16_t nbytes = packet.GetU16 (&offset); +                            s.Printf(" (lcpu = %u, addr = 0x%4.4x, nbytes = %u, bytes = \n", lcpu, address, nbytes); +                            if (nbytes > 0) +                                packet.Dump (&s,                        // Stream to dump to +                                             offset,                    // Offset within "packet" +                                             eFormatHex,                // Format to use +                                             1,                         // Size of each item in bytes +                                             nbytes,                    // Number of items +                                             16,                        // Number per line +                                             LLDB_INVALID_ADDRESS,      // Don't show addresses before each line +                                             0, 0);                     // No bitfields +                        } +                        break; +                         +                    case KDP_DUMPINFO: +                        { +                            const uint32_t count = packet.GetByteSize() - offset; +                            s.Printf(" (count = %u, bytes = \n", count); +                            if (count > 0) +                                packet.Dump (&s,                        // Stream to dump to +                                             offset,                    // Offset within "packet" +                                             eFormatHex,                // Format to use +                                             1,                         // Size of each item in bytes +                                             count,                     // Number of items +                                             16,                        // Number per line +                                             LLDB_INVALID_ADDRESS,      // Don't show addresses before each line +                                             0, 0);                     // No bitfields +                             +                        } +                        break; +                         +               } +            } +        } +        else +        { +            error_desc = "error: invalid packet command: "; +        } +    } + +    if (error_desc) +    { +        s.PutCString (error_desc); + +        packet.Dump (&s,                    // Stream to dump to +                     0,                     // Offset into "packet" +                     eFormatBytes,          // Dump as hex bytes +                     1,                     // Size of each item is 1 for single bytes +                     packet.GetByteSize(),  // Number of bytes +                     UINT32_MAX,            // Num bytes per line +                     LLDB_INVALID_ADDRESS,  // Base address +                     0, 0);                 // Bitfield info set to not do anything bitfield related +    } +} + +uint32_t +CommunicationKDP::SendRequestReadRegisters (uint32_t cpu, +                                            uint32_t flavor, +                                            void *dst, +                                            uint32_t dst_len, +                                            Error &error) +{ +    PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); +    const CommandType command = KDP_READREGS; +    // Size is header + 4 byte cpu and 4 byte flavor +    const uint32_t command_length = 8 + 4 + 4; +    MakeRequestPacketHeader (command, request_packet, command_length); +    request_packet.PutHex32 (cpu); +    request_packet.PutHex32 (flavor); +    DataExtractor reply_packet; +    if (SendRequestAndGetReply (command, request_packet, reply_packet)) +    { +        lldb::offset_t offset = 8; +        uint32_t kdp_error = reply_packet.GetU32 (&offset); +        uint32_t src_len = reply_packet.GetByteSize() - 12; +         +        if (src_len > 0) +        { +            const uint32_t bytes_to_copy = std::min<uint32_t>(src_len, dst_len); +            const void *src = reply_packet.GetData(&offset, bytes_to_copy); +            if (src) +            { +                ::memcpy (dst, src, bytes_to_copy); +                error.Clear(); +                // Return the number of bytes we could have returned regardless if +                // we copied them or not, just so we know when things don't match up +                return src_len; +            } +        } +        if (kdp_error) +            error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error); +        else +            error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u", cpu, flavor); +    } +    else +    { +        error.SetErrorString ("failed to send packet"); +    } +    return 0; +} + +uint32_t +CommunicationKDP::SendRequestWriteRegisters (uint32_t cpu, +                                             uint32_t flavor, +                                             const void *src, +                                             uint32_t src_len, +                                             Error &error) +{ +    PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); +    const CommandType command = KDP_WRITEREGS; +    // Size is header + 4 byte cpu and 4 byte flavor +    const uint32_t command_length = 8 + 4 + 4 + src_len; +    MakeRequestPacketHeader (command, request_packet, command_length); +    request_packet.PutHex32 (cpu); +    request_packet.PutHex32 (flavor); +    request_packet.Write(src, src_len); +    DataExtractor reply_packet; +    if (SendRequestAndGetReply (command, request_packet, reply_packet)) +    { +        lldb::offset_t offset = 8; +        uint32_t kdp_error = reply_packet.GetU32 (&offset); +        if (kdp_error == 0) +            return src_len; +        error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error); +    } +    else +    { +        error.SetErrorString ("failed to send packet"); +    } +    return 0; +} + + +bool +CommunicationKDP::SendRequestResume () +{ +    PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); +    const CommandType command = KDP_RESUMECPUS; +    const uint32_t command_length = 12; +    MakeRequestPacketHeader (command, request_packet, command_length); +    request_packet.PutHex32(GetCPUMask()); + +    DataExtractor reply_packet; +    if (SendRequestAndGetReply (command, request_packet, reply_packet)) +        return true; +    return false; +} + +bool +CommunicationKDP::SendRequestBreakpoint (bool set, addr_t addr) +{ +    PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); +    bool use_64 = (GetVersion() >= 11); +    uint32_t command_addr_byte_size = use_64 ? 8 : 4; +    const CommandType command = set ? (use_64 ? KDP_BREAKPOINT_SET64    : KDP_BREAKPOINT_SET   ): +                                      (use_64 ? KDP_BREAKPOINT_REMOVE64 : KDP_BREAKPOINT_REMOVE); + +    const uint32_t command_length = 8 + command_addr_byte_size; +    MakeRequestPacketHeader (command, request_packet, command_length); +    request_packet.PutMaxHex64 (addr, command_addr_byte_size); +     +    DataExtractor reply_packet; +    if (SendRequestAndGetReply (command, request_packet, reply_packet)) +    { +        lldb::offset_t offset = 8; +        uint32_t kdp_error = reply_packet.GetU32 (&offset);         +        if (kdp_error == 0) +            return true; +    } +    return false; +} + +bool +CommunicationKDP::SendRequestSuspend () +{ +    PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); +    const CommandType command = KDP_SUSPEND; +    const uint32_t command_length = 8; +    MakeRequestPacketHeader (command, request_packet, command_length); +    DataExtractor reply_packet; +    if (SendRequestAndGetReply (command, request_packet, reply_packet)) +        return true; +    return false; +} + diff --git a/source/Plugins/Process/MacOSX-Kernel/CommunicationKDP.h b/source/Plugins/Process/MacOSX-Kernel/CommunicationKDP.h new file mode 100644 index 000000000000..98a146d5a06d --- /dev/null +++ b/source/Plugins/Process/MacOSX-Kernel/CommunicationKDP.h @@ -0,0 +1,347 @@ +//===-- CommunicationKDP.h --------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_CommunicationKDP_h_ +#define liblldb_CommunicationKDP_h_ + +// C Includes +// C++ Includes +#include <list> +#include <string> + +// Other libraries and framework includes +// Project includes +#include "lldb/lldb-private.h" +#include "lldb/Core/Communication.h" +#include "lldb/Core/Listener.h" +#include "lldb/Core/StreamBuffer.h" +#include "lldb/Host/Mutex.h" +#include "lldb/Host/Predicate.h" +#include "lldb/Host/TimeValue.h" + +class CommunicationKDP : public lldb_private::Communication +{ +public: +    enum +    { +        eBroadcastBitRunPacketSent = kLoUserBroadcastBit +    }; +     +    const static uint32_t kMaxPacketSize = 1200; +    const static uint32_t kMaxDataSize = 1024; +    typedef lldb_private::StreamBuffer<1024> PacketStreamType; +    typedef enum  +    { +        KDP_CONNECT = 0u,                +        KDP_DISCONNECT, +        KDP_HOSTINFO, +        KDP_VERSION, +        KDP_MAXBYTES, +        KDP_READMEM, +        KDP_WRITEMEM, +        KDP_READREGS, +        KDP_WRITEREGS, +        KDP_LOAD, +        KDP_IMAGEPATH, +        KDP_SUSPEND, +        KDP_RESUMECPUS, +        KDP_EXCEPTION, +        KDP_TERMINATION, +        KDP_BREAKPOINT_SET, +        KDP_BREAKPOINT_REMOVE, +        KDP_REGIONS, +        KDP_REATTACH, +        KDP_HOSTREBOOT, +        KDP_READMEM64, +        KDP_WRITEMEM64, +        KDP_BREAKPOINT_SET64, +        KDP_BREAKPOINT_REMOVE64, +        KDP_KERNELVERSION, +        KDP_READPHYSMEM64, +        KDP_WRITEPHYSMEM64, +        KDP_READIOPORT, +        KDP_WRITEIOPORT, +        KDP_READMSR64, +        KDP_WRITEMSR64, +        KDP_DUMPINFO +    } CommandType; + +    enum  +    { +        KDP_FEATURE_BP = (1u << 0) +    }; + +    typedef enum +    { +        KDP_PROTERR_SUCCESS = 0, +        KDP_PROTERR_ALREADY_CONNECTED, +        KDP_PROTERR_BAD_NBYTES, +        KDP_PROTERR_BADFLAVOR +    } KDPError; +     +    typedef enum +    { +        ePacketTypeRequest  = 0x00u, +        ePacketTypeReply    = 0x80u, +        ePacketTypeMask     = 0x80u, +        eCommandTypeMask    = 0x7fu +    } PacketType; +    //------------------------------------------------------------------ +    // Constructors and Destructors +    //------------------------------------------------------------------ +    CommunicationKDP (const char *comm_name); + +    virtual +    ~CommunicationKDP(); + +    bool +    SendRequestPacket (const PacketStreamType &request_packet); + +    // Wait for a packet within 'nsec' seconds +    size_t +    WaitForPacketWithTimeoutMicroSeconds (lldb_private::DataExtractor &response, +                                          uint32_t usec); + +    bool +    GetSequenceMutex(lldb_private::Mutex::Locker& locker); + +    bool +    CheckForPacket (const uint8_t *src,  +                    size_t src_len,  +                    lldb_private::DataExtractor &packet); +    bool +    IsRunning() const +    { +        return m_is_running.GetValue(); +    } + +    //------------------------------------------------------------------ +    // Set the global packet timeout. +    // +    // For clients, this is the timeout that gets used when sending +    // packets and waiting for responses. For servers, this might not +    // get used, and if it doesn't this should be moved to the +    // CommunicationKDPClient. +    //------------------------------------------------------------------ +    uint32_t  +    SetPacketTimeout (uint32_t packet_timeout) +    { +        const uint32_t old_packet_timeout = m_packet_timeout; +        m_packet_timeout = packet_timeout; +        return old_packet_timeout; +    } + +    uint32_t +    GetPacketTimeoutInMicroSeconds () const +    { +        return m_packet_timeout * lldb_private::TimeValue::MicroSecPerSec; +    } +     +    //------------------------------------------------------------------ +    // Public Request Packets +    //------------------------------------------------------------------ +    bool +    SendRequestConnect (uint16_t reply_port,  +                        uint16_t exc_port,  +                        const char *greeting); + +    bool +    SendRequestReattach (uint16_t reply_port); + +    bool +    SendRequestDisconnect (); +     +    uint32_t +    SendRequestReadMemory (lldb::addr_t addr,  +                           void *dst,  +                           uint32_t dst_size, +                           lldb_private::Error &error); + +    uint32_t +    SendRequestWriteMemory (lldb::addr_t addr,  +                            const void *src,  +                            uint32_t src_len, +                            lldb_private::Error &error); + +    bool +    SendRawRequest (uint8_t command_byte, +                    const void *src, +                    uint32_t src_len, +                    lldb_private::DataExtractor &reply, +                    lldb_private::Error &error); + +    uint32_t +    SendRequestReadRegisters (uint32_t cpu, +                              uint32_t flavor, +                              void *dst,  +                              uint32_t dst_size, +                              lldb_private::Error &error); + +    uint32_t +    SendRequestWriteRegisters (uint32_t cpu, +                               uint32_t flavor, +                               const void *src, +                               uint32_t src_size, +                               lldb_private::Error &error); + +    const char * +    GetKernelVersion (); +     +    // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection... +    // const char * +    // GetImagePath (); + +    uint32_t +    GetVersion (); + +    uint32_t +    GetFeatureFlags (); + +    bool +    LocalBreakpointsAreSupported () +    { +        return (GetFeatureFlags() & KDP_FEATURE_BP) != 0; +    } + +    uint32_t +    GetCPUMask (); +     +    uint32_t +    GetCPUType (); +     +    uint32_t +    GetCPUSubtype (); + +    lldb_private::UUID  +    GetUUID (); + +    bool +    RemoteIsEFI (); + +    bool +    RemoteIsDarwinKernel (); + +    lldb::addr_t +    GetLoadAddress (); + +    bool +    SendRequestResume (); + +    bool +    SendRequestSuspend (); + +    bool +    SendRequestBreakpoint (bool set, lldb::addr_t addr); + +protected: + +    bool +    SendRequestPacketNoLock (const PacketStreamType &request_packet); + +    size_t +    WaitForPacketWithTimeoutMicroSecondsNoLock (lldb_private::DataExtractor &response,  +                                                uint32_t timeout_usec); + +    bool +    WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr); + +    void +    MakeRequestPacketHeader (CommandType request_type,  +                             PacketStreamType &request_packet, +                             uint16_t request_length); + +    //------------------------------------------------------------------ +    // Protected Request Packets (use public accessors which will cache +    // results. +    //------------------------------------------------------------------ +    bool +    SendRequestVersion (); +     +    bool +    SendRequestHostInfo (); + +    bool +    SendRequestKernelVersion (); +     +    // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection... +    //bool +    //SendRequestImagePath (); + +    void +    DumpPacket (lldb_private::Stream &s,  +                const void *data,  +                uint32_t data_len); + +    void +    DumpPacket (lldb_private::Stream &s,  +                const lldb_private::DataExtractor& extractor); + +    bool +    VersionIsValid() const +    { +        return m_kdp_version_version != 0; +    } + +    bool +    HostInfoIsValid() const +    { +        return m_kdp_hostinfo_cpu_type != 0; +    } + +    bool +    ExtractIsReply (uint8_t first_packet_byte) const +    { +        // TODO: handle big endian... +        return (first_packet_byte & ePacketTypeMask) != 0; +    } + +    CommandType +    ExtractCommand (uint8_t first_packet_byte) const +    { +        // TODO: handle big endian... +        return (CommandType)(first_packet_byte & eCommandTypeMask); +    } +     +    static const char * +    GetCommandAsCString (uint8_t command); + +    void +    ClearKDPSettings (); +     +    bool +    SendRequestAndGetReply (const CommandType command, +                            const PacketStreamType &request_packet,  +                            lldb_private::DataExtractor &reply_packet); +    //------------------------------------------------------------------ +    // Classes that inherit from CommunicationKDP can see and modify these +    //------------------------------------------------------------------ +    uint32_t m_addr_byte_size; +    lldb::ByteOrder m_byte_order; +    uint32_t m_packet_timeout; +    lldb_private::Mutex m_sequence_mutex;    // Restrict access to sending/receiving packets to a single thread at a time +    lldb_private::Predicate<bool> m_is_running; +    uint32_t m_session_key; +    uint8_t m_request_sequence_id; +    uint8_t m_exception_sequence_id; +    uint32_t m_kdp_version_version; +    uint32_t m_kdp_version_feature; +    uint32_t m_kdp_hostinfo_cpu_mask; +    uint32_t m_kdp_hostinfo_cpu_type; +    uint32_t m_kdp_hostinfo_cpu_subtype; +    std::string m_kernel_version; +    //std::string m_image_path; // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection... +    lldb::addr_t m_last_read_memory_addr; // Last memory read address for logging +private: +    //------------------------------------------------------------------ +    // For CommunicationKDP only +    //------------------------------------------------------------------ +    DISALLOW_COPY_AND_ASSIGN (CommunicationKDP); +}; + +#endif  // liblldb_CommunicationKDP_h_ diff --git a/source/Plugins/Process/MacOSX-Kernel/Makefile b/source/Plugins/Process/MacOSX-Kernel/Makefile new file mode 100644 index 000000000000..e42f390ffe0d --- /dev/null +++ b/source/Plugins/Process/MacOSX-Kernel/Makefile @@ -0,0 +1,14 @@ +##===- source/Plugins/Process/MacOSX-Darwin/Makefile -------*- Makefile -*-===## +#  +#                     The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +#  +##===----------------------------------------------------------------------===## + +LLDB_LEVEL := ../../../.. +LIBRARYNAME := lldbPluginProcessDarwin +BUILD_ARCHIVE = 1 + +include $(LLDB_LEVEL)/Makefile diff --git a/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp b/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp new file mode 100644 index 000000000000..628f76d104fe --- /dev/null +++ b/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp @@ -0,0 +1,1211 @@ +//===-- ProcessKDP.cpp ------------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// C Includes +#include <errno.h> +#include <stdlib.h> + +// C++ Includes +#include <mutex> + +// Other libraries and framework includes +#include "lldb/Core/Debugger.h" +#include "lldb/Core/PluginManager.h" +#include "lldb/Core/Module.h" +#include "lldb/Core/ModuleSpec.h" +#include "lldb/Core/State.h" +#include "lldb/Core/UUID.h" +#include "lldb/Host/ConnectionFileDescriptor.h" +#include "lldb/Host/Host.h" +#include "lldb/Host/Symbols.h" +#include "lldb/Host/ThreadLauncher.h" +#include "lldb/Host/common/TCPSocket.h" +#include "lldb/Interpreter/CommandInterpreter.h" +#include "lldb/Interpreter/CommandObject.h" +#include "lldb/Interpreter/CommandObjectMultiword.h" +#include "lldb/Interpreter/CommandReturnObject.h" +#include "lldb/Interpreter/OptionGroupString.h" +#include "lldb/Interpreter/OptionGroupUInt64.h" +#include "lldb/Interpreter/OptionValueProperties.h" +#include "lldb/Symbol/ObjectFile.h" +#include "lldb/Target/RegisterContext.h" +#include "lldb/Target/Target.h" +#include "lldb/Target/Thread.h" +#include "lldb/Utility/StringExtractor.h" + +#define USEC_PER_SEC 1000000 + +// Project includes +#include "ProcessKDP.h" +#include "ProcessKDPLog.h" +#include "ThreadKDP.h" +#include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h" +#include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h" + +using namespace lldb; +using namespace lldb_private; + +namespace { + +    static PropertyDefinition +    g_properties[] = +    { +        { "packet-timeout" , OptionValue::eTypeUInt64 , true , 5, NULL, NULL, "Specify the default packet timeout in seconds." }, +        {  NULL            , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL  } +    }; +     +    enum +    { +        ePropertyPacketTimeout +    }; + +    class PluginProperties : public Properties +    { +    public: +         +        static ConstString +        GetSettingName () +        { +            return ProcessKDP::GetPluginNameStatic(); +        } + +        PluginProperties() : +            Properties () +        { +            m_collection_sp.reset (new OptionValueProperties(GetSettingName())); +            m_collection_sp->Initialize(g_properties); +        } +         +        virtual +        ~PluginProperties() +        { +        } +         +        uint64_t +        GetPacketTimeout() +        { +            const uint32_t idx = ePropertyPacketTimeout; +            return m_collection_sp->GetPropertyAtIndexAsUInt64(NULL, idx, g_properties[idx].default_uint_value); +        } +    }; + +    typedef std::shared_ptr<PluginProperties> ProcessKDPPropertiesSP; + +    static const ProcessKDPPropertiesSP & +    GetGlobalPluginProperties() +    { +        static ProcessKDPPropertiesSP g_settings_sp; +        if (!g_settings_sp) +            g_settings_sp.reset (new PluginProperties ()); +        return g_settings_sp; +    } +     +} // anonymous namespace end + +static const lldb::tid_t g_kernel_tid = 1; + +ConstString +ProcessKDP::GetPluginNameStatic() +{ +    static ConstString g_name("kdp-remote"); +    return g_name; +} + +const char * +ProcessKDP::GetPluginDescriptionStatic() +{ +    return "KDP Remote protocol based debugging plug-in for darwin kernel debugging."; +} + +void +ProcessKDP::Terminate() +{ +    PluginManager::UnregisterPlugin (ProcessKDP::CreateInstance); +} + + +lldb::ProcessSP +ProcessKDP::CreateInstance (TargetSP target_sp, +                            Listener &listener, +                            const FileSpec *crash_file_path) +{ +    lldb::ProcessSP process_sp; +    if (crash_file_path == NULL) +        process_sp.reset(new ProcessKDP (target_sp, listener)); +    return process_sp; +} + +bool +ProcessKDP::CanDebug(TargetSP target_sp, bool plugin_specified_by_name) +{ +    if (plugin_specified_by_name) +        return true; + +    // For now we are just making sure the file exists for a given module +    Module *exe_module = target_sp->GetExecutableModulePointer(); +    if (exe_module) +    { +        const llvm::Triple &triple_ref = target_sp->GetArchitecture().GetTriple(); +        switch (triple_ref.getOS()) +        { +            case llvm::Triple::Darwin:  // Should use "macosx" for desktop and "ios" for iOS, but accept darwin just in case +            case llvm::Triple::MacOSX:  // For desktop targets +            case llvm::Triple::IOS:     // For arm targets +            case llvm::Triple::TvOS: +            case llvm::Triple::WatchOS: +                if (triple_ref.getVendor() == llvm::Triple::Apple) +                { +                    ObjectFile *exe_objfile = exe_module->GetObjectFile(); +                    if (exe_objfile->GetType() == ObjectFile::eTypeExecutable &&  +                        exe_objfile->GetStrata() == ObjectFile::eStrataKernel) +                        return true; +                } +                break; + +            default: +                break; +        } +    } +    return false; +} + +//---------------------------------------------------------------------- +// ProcessKDP constructor +//---------------------------------------------------------------------- +ProcessKDP::ProcessKDP(TargetSP target_sp, Listener &listener) : +    Process (target_sp, listener), +    m_comm("lldb.process.kdp-remote.communication"), +    m_async_broadcaster (NULL, "lldb.process.kdp-remote.async-broadcaster"), +    m_dyld_plugin_name (), +    m_kernel_load_addr (LLDB_INVALID_ADDRESS), +    m_command_sp(), +    m_kernel_thread_wp() +{ +    m_async_broadcaster.SetEventName (eBroadcastBitAsyncThreadShouldExit,   "async thread should exit"); +    m_async_broadcaster.SetEventName (eBroadcastBitAsyncContinue,           "async thread continue"); +    const uint64_t timeout_seconds = GetGlobalPluginProperties()->GetPacketTimeout(); +    if (timeout_seconds > 0) +        m_comm.SetPacketTimeout(timeout_seconds); +} + +//---------------------------------------------------------------------- +// Destructor +//---------------------------------------------------------------------- +ProcessKDP::~ProcessKDP() +{ +    Clear(); +    // We need to call finalize on the process before destroying ourselves +    // to make sure all of the broadcaster cleanup goes as planned. If we +    // destruct this class, then Process::~Process() might have problems +    // trying to fully destroy the broadcaster. +    Finalize(); +} + +//---------------------------------------------------------------------- +// PluginInterface +//---------------------------------------------------------------------- +lldb_private::ConstString +ProcessKDP::GetPluginName() +{ +    return GetPluginNameStatic(); +} + +uint32_t +ProcessKDP::GetPluginVersion() +{ +    return 1; +} + +Error +ProcessKDP::WillLaunch (Module* module) +{ +    Error error; +    error.SetErrorString ("launching not supported in kdp-remote plug-in"); +    return error; +} + +Error +ProcessKDP::WillAttachToProcessWithID (lldb::pid_t pid) +{ +    Error error; +    error.SetErrorString ("attaching to a by process ID not supported in kdp-remote plug-in"); +    return error; +} + +Error +ProcessKDP::WillAttachToProcessWithName (const char *process_name, bool wait_for_launch) +{ +    Error error; +    error.SetErrorString ("attaching to a by process name not supported in kdp-remote plug-in"); +    return error; +} + +bool +ProcessKDP::GetHostArchitecture(ArchSpec &arch) +{ +    uint32_t cpu = m_comm.GetCPUType(); +    if (cpu) +    { +        uint32_t sub = m_comm.GetCPUSubtype(); +        arch.SetArchitecture(eArchTypeMachO, cpu, sub); +        // Leave architecture vendor as unspecified unknown +        arch.GetTriple().setVendor(llvm::Triple::UnknownVendor); +        arch.GetTriple().setVendorName(llvm::StringRef()); +        return true; +    } +    arch.Clear(); +    return false; +} + +Error +ProcessKDP::DoConnectRemote (Stream *strm, const char *remote_url) +{ +    Error error; + +    // Don't let any JIT happen when doing KDP as we can't allocate +    // memory and we don't want to be mucking with threads that might +    // already be handling exceptions +    SetCanJIT(false); + +    if (remote_url == NULL || remote_url[0] == '\0') +    { +        error.SetErrorStringWithFormat ("invalid connection URL '%s'", remote_url); +        return error; +    } + +    std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor()); +    if (conn_ap.get()) +    { +        // Only try once for now. +        // TODO: check if we should be retrying? +        const uint32_t max_retry_count = 1; +        for (uint32_t retry_count = 0; retry_count < max_retry_count; ++retry_count) +        { +            if (conn_ap->Connect(remote_url, &error) == eConnectionStatusSuccess) +                break; +            usleep (100000); +        } +    } + +    if (conn_ap->IsConnected()) +    { +        const TCPSocket& socket = static_cast<const TCPSocket&>(*conn_ap->GetReadObject()); +        const uint16_t reply_port = socket.GetLocalPortNumber(); + +        if (reply_port != 0) +        { +            m_comm.SetConnection(conn_ap.release()); + +            if (m_comm.SendRequestReattach(reply_port)) +            { +                if (m_comm.SendRequestConnect(reply_port, reply_port, "Greetings from LLDB...")) +                { +                    m_comm.GetVersion(); + +                    Target &target = GetTarget(); +                    ArchSpec kernel_arch; +                    // The host architecture +                    GetHostArchitecture(kernel_arch); +                    ArchSpec target_arch = target.GetArchitecture(); +                    // Merge in any unspecified stuff into the target architecture in +                    // case the target arch isn't set at all or incompletely. +                    target_arch.MergeFrom(kernel_arch); +                    target.SetArchitecture(target_arch); + +                    /* Get the kernel's UUID and load address via KDP_KERNELVERSION packet.  */ +                    /* An EFI kdp session has neither UUID nor load address. */ + +                    UUID kernel_uuid = m_comm.GetUUID (); +                    addr_t kernel_load_addr = m_comm.GetLoadAddress (); + +                    if (m_comm.RemoteIsEFI ()) +                    { +                        // Select an invalid plugin name for the dynamic loader so one doesn't get used +                        // since EFI does its own manual loading via python scripting +                        static ConstString g_none_dynamic_loader("none"); +                        m_dyld_plugin_name = g_none_dynamic_loader; + +                        if (kernel_uuid.IsValid()) { +                            // If EFI passed in a UUID= try to lookup UUID +                            // The slide will not be provided. But the UUID +                            // lookup will be used to launch EFI debug scripts +                            // from the dSYM, that can load all of the symbols. +                            ModuleSpec module_spec; +                            module_spec.GetUUID() = kernel_uuid; +                            module_spec.GetArchitecture() = target.GetArchitecture(); + +                            // Lookup UUID locally, before attempting dsymForUUID like action +                            module_spec.GetSymbolFileSpec() = Symbols::LocateExecutableSymbolFile(module_spec); +                            if (module_spec.GetSymbolFileSpec()) +                            { +                                ModuleSpec executable_module_spec = Symbols::LocateExecutableObjectFile (module_spec); +                                if (executable_module_spec.GetFileSpec().Exists()) +                                { +                                    module_spec.GetFileSpec() = executable_module_spec.GetFileSpec(); +                                } +                            } +                            if (!module_spec.GetSymbolFileSpec() || !module_spec.GetSymbolFileSpec()) +                                 Symbols::DownloadObjectAndSymbolFile (module_spec, true); + +                            if (module_spec.GetFileSpec().Exists()) +                            { +                                ModuleSP module_sp(new Module (module_spec)); +                                if (module_sp.get() && module_sp->GetObjectFile()) +                                { +                                    // Get the current target executable +                                    ModuleSP exe_module_sp (target.GetExecutableModule ()); + +                                    // Make sure you don't already have the right module loaded and they will be uniqued +                                    if (exe_module_sp.get() != module_sp.get()) +                                        target.SetExecutableModule (module_sp, false); +                                } +                            } +                        } +                    } +                    else if (m_comm.RemoteIsDarwinKernel ()) +                    { +                        m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic(); +                        if (kernel_load_addr != LLDB_INVALID_ADDRESS) +                        { +                            m_kernel_load_addr = kernel_load_addr; +                        } +                    } + +                    // Set the thread ID +                    UpdateThreadListIfNeeded (); +                    SetID (1); +                    GetThreadList (); +                    SetPrivateState (eStateStopped); +                    StreamSP async_strm_sp(target.GetDebugger().GetAsyncOutputStream()); +                    if (async_strm_sp) +                    { +                        const char *cstr; +                        if ((cstr = m_comm.GetKernelVersion ()) != NULL) +                        { +                            async_strm_sp->Printf ("Version: %s\n", cstr); +                            async_strm_sp->Flush(); +                        } +//                      if ((cstr = m_comm.GetImagePath ()) != NULL) +//                      { +//                          async_strm_sp->Printf ("Image Path: %s\n", cstr); +//                          async_strm_sp->Flush(); +//                      }             +                    } +                } +                else +                { +                    error.SetErrorString("KDP_REATTACH failed"); +                } +            } +            else +            { +                error.SetErrorString("KDP_REATTACH failed"); +            } +        } +        else +        { +            error.SetErrorString("invalid reply port from UDP connection"); +        } +    } +    else +    { +        if (error.Success()) +            error.SetErrorStringWithFormat ("failed to connect to '%s'", remote_url); +    } +    if (error.Fail()) +        m_comm.Disconnect(); + +    return error; +} + +//---------------------------------------------------------------------- +// Process Control +//---------------------------------------------------------------------- +Error +ProcessKDP::DoLaunch (Module *exe_module,  +                      ProcessLaunchInfo &launch_info) +{ +    Error error; +    error.SetErrorString ("launching not supported in kdp-remote plug-in"); +    return error; +} + +Error +ProcessKDP::DoAttachToProcessWithID (lldb::pid_t attach_pid, const ProcessAttachInfo &attach_info) +{ +    Error error; +    error.SetErrorString ("attach to process by ID is not suppported in kdp remote debugging"); +    return error; +} + +Error +ProcessKDP::DoAttachToProcessWithName (const char *process_name, const ProcessAttachInfo &attach_info) +{ +    Error error; +    error.SetErrorString ("attach to process by name is not suppported in kdp remote debugging"); +    return error; +} + + +void +ProcessKDP::DidAttach (ArchSpec &process_arch) +{ +    Process::DidAttach(process_arch); +     +    Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS)); +    if (log) +        log->Printf ("ProcessKDP::DidAttach()"); +    if (GetID() != LLDB_INVALID_PROCESS_ID) +    { +        GetHostArchitecture(process_arch); +    } +} + +addr_t +ProcessKDP::GetImageInfoAddress() +{ +    return m_kernel_load_addr; +} + +lldb_private::DynamicLoader * +ProcessKDP::GetDynamicLoader () +{ +    if (m_dyld_ap.get() == NULL) +        m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.IsEmpty() ? NULL : m_dyld_plugin_name.GetCString())); +    return m_dyld_ap.get(); +} + +Error +ProcessKDP::WillResume () +{ +    return Error(); +} + +Error +ProcessKDP::DoResume () +{ +    Error error; +    Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS)); +    // Only start the async thread if we try to do any process control +    if (!m_async_thread.IsJoinable()) +        StartAsyncThread(); + +    bool resume = false; +     +    // With KDP there is only one thread we can tell what to do +    ThreadSP kernel_thread_sp (m_thread_list.FindThreadByProtocolID(g_kernel_tid)); +                             +    if (kernel_thread_sp) +    { +        const StateType thread_resume_state = kernel_thread_sp->GetTemporaryResumeState(); +         +        if (log) +            log->Printf ("ProcessKDP::DoResume() thread_resume_state = %s", StateAsCString(thread_resume_state)); +        switch (thread_resume_state) +        { +            case eStateSuspended: +                // Nothing to do here when a thread will stay suspended +                // we just leave the CPU mask bit set to zero for the thread +                if (log) +                    log->Printf ("ProcessKDP::DoResume() = suspended???"); +                break; +                 +            case eStateStepping: +                { +                    lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext()); + +                    if (reg_ctx_sp) +                    { +                        if (log) +                            log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (true);"); +                        reg_ctx_sp->HardwareSingleStep (true); +                        resume = true; +                    } +                    else +                    { +                        error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID()); +                    } +                } +                break; +     +            case eStateRunning: +                { +                    lldb::RegisterContextSP reg_ctx_sp (kernel_thread_sp->GetRegisterContext()); +                     +                    if (reg_ctx_sp) +                    { +                        if (log) +                            log->Printf ("ProcessKDP::DoResume () reg_ctx_sp->HardwareSingleStep (false);"); +                        reg_ctx_sp->HardwareSingleStep (false); +                        resume = true; +                    } +                    else +                    { +                        error.SetErrorStringWithFormat("KDP thread 0x%llx has no register context", kernel_thread_sp->GetID()); +                    } +                } +                break; + +            default: +                // The only valid thread resume states are listed above +                assert (!"invalid thread resume state"); +                break; +        } +    } + +    if (resume) +    { +        if (log) +            log->Printf ("ProcessKDP::DoResume () sending resume"); +         +        if (m_comm.SendRequestResume ()) +        { +            m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue); +            SetPrivateState(eStateRunning); +        } +        else +            error.SetErrorString ("KDP resume failed"); +    } +    else +    { +        error.SetErrorString ("kernel thread is suspended");         +    } +     +    return error; +} + +lldb::ThreadSP +ProcessKDP::GetKernelThread() +{ +    // KDP only tells us about one thread/core. Any other threads will usually +    // be the ones that are read from memory by the OS plug-ins. +     +    ThreadSP thread_sp (m_kernel_thread_wp.lock()); +    if (!thread_sp) +    { +        thread_sp.reset(new ThreadKDP (*this, g_kernel_tid)); +        m_kernel_thread_wp = thread_sp; +    } +    return thread_sp; +} + + + + +bool +ProcessKDP::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) +{ +    // locker will keep a mutex locked until it goes out of scope +    Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_THREAD)); +    if (log && log->GetMask().Test(KDP_LOG_VERBOSE)) +        log->Printf ("ProcessKDP::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID()); +     +    // Even though there is a CPU mask, it doesn't mean we can see each CPU +    // individually, there is really only one. Lets call this thread 1. +    ThreadSP thread_sp (old_thread_list.FindThreadByProtocolID(g_kernel_tid, false)); +    if (!thread_sp) +        thread_sp = GetKernelThread (); +    new_thread_list.AddThread(thread_sp); + +    return new_thread_list.GetSize(false) > 0; +} + +void +ProcessKDP::RefreshStateAfterStop () +{ +    // Let all threads recover from stopping and do any clean up based +    // on the previous thread state (if any). +    m_thread_list.RefreshStateAfterStop(); +} + +Error +ProcessKDP::DoHalt (bool &caused_stop) +{ +    Error error; +     +    if (m_comm.IsRunning()) +    { +        if (m_destroy_in_process) +        { +            // If we are attemping to destroy, we need to not return an error to +            // Halt or DoDestroy won't get called. +            // We are also currently running, so send a process stopped event +            SetPrivateState (eStateStopped); +        } +        else +        { +            error.SetErrorString ("KDP cannot interrupt a running kernel"); +        } +    } +    return error; +} + +Error +ProcessKDP::DoDetach(bool keep_stopped) +{ +    Error error; +    Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); +    if (log) +        log->Printf ("ProcessKDP::DoDetach(keep_stopped = %i)", keep_stopped); +     +    if (m_comm.IsRunning()) +    { +        // We are running and we can't interrupt a running kernel, so we need +        // to just close the connection to the kernel and hope for the best +    } +    else +    { +        // If we are going to keep the target stopped, then don't send the disconnect message. +        if (!keep_stopped && m_comm.IsConnected()) +        { +            const bool success = m_comm.SendRequestDisconnect(); +            if (log) +            { +                if (success) +                    log->PutCString ("ProcessKDP::DoDetach() detach packet sent successfully"); +                else +                    log->PutCString ("ProcessKDP::DoDetach() connection channel shutdown failed"); +            } +            m_comm.Disconnect (); +        } +    } +    StopAsyncThread ();     +    m_comm.Clear(); +     +    SetPrivateState (eStateDetached); +    ResumePrivateStateThread(); +     +    //KillDebugserverProcess (); +    return error; +} + +Error +ProcessKDP::DoDestroy () +{ +    // For KDP there really is no difference between destroy and detach +    bool keep_stopped = false; +    return DoDetach(keep_stopped); +} + +//------------------------------------------------------------------ +// Process Queries +//------------------------------------------------------------------ + +bool +ProcessKDP::IsAlive () +{ +    return m_comm.IsConnected() && Process::IsAlive(); +} + +//------------------------------------------------------------------ +// Process Memory +//------------------------------------------------------------------ +size_t +ProcessKDP::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error) +{ +    uint8_t *data_buffer = (uint8_t *) buf; +    if (m_comm.IsConnected()) +    { +        const size_t max_read_size = 512; +        size_t total_bytes_read = 0; + +        // Read the requested amount of memory in 512 byte chunks +        while (total_bytes_read < size) +        { +            size_t bytes_to_read_this_request = size - total_bytes_read; +            if (bytes_to_read_this_request > max_read_size) +            { +                bytes_to_read_this_request = max_read_size; +            } +            size_t bytes_read = m_comm.SendRequestReadMemory (addr + total_bytes_read,  +                                                              data_buffer + total_bytes_read,  +                                                              bytes_to_read_this_request, error); +            total_bytes_read += bytes_read; +            if (error.Fail() || bytes_read == 0) +            { +                return total_bytes_read; +            } +        } + +        return total_bytes_read; +    } +    error.SetErrorString ("not connected"); +    return 0; +} + +size_t +ProcessKDP::DoWriteMemory (addr_t addr, const void *buf, size_t size, Error &error) +{ +    if (m_comm.IsConnected()) +        return m_comm.SendRequestWriteMemory (addr, buf, size, error); +    error.SetErrorString ("not connected"); +    return 0; +} + +lldb::addr_t +ProcessKDP::DoAllocateMemory (size_t size, uint32_t permissions, Error &error) +{ +    error.SetErrorString ("memory allocation not suppported in kdp remote debugging"); +    return LLDB_INVALID_ADDRESS; +} + +Error +ProcessKDP::DoDeallocateMemory (lldb::addr_t addr) +{ +    Error error; +    error.SetErrorString ("memory deallocation not suppported in kdp remote debugging"); +    return error; +} + +Error +ProcessKDP::EnableBreakpointSite (BreakpointSite *bp_site) +{ +    if (m_comm.LocalBreakpointsAreSupported ()) +    { +        Error error; +        if (!bp_site->IsEnabled()) +        { +            if (m_comm.SendRequestBreakpoint(true, bp_site->GetLoadAddress())) +            { +                bp_site->SetEnabled(true); +                bp_site->SetType (BreakpointSite::eExternal); +            } +            else +            { +                error.SetErrorString ("KDP set breakpoint failed"); +            } +        } +        return error; +    } +    return EnableSoftwareBreakpoint (bp_site); +} + +Error +ProcessKDP::DisableBreakpointSite (BreakpointSite *bp_site) +{ +    if (m_comm.LocalBreakpointsAreSupported ()) +    { +        Error error; +        if (bp_site->IsEnabled()) +        { +            BreakpointSite::Type bp_type = bp_site->GetType(); +            if (bp_type == BreakpointSite::eExternal) +            { +                if (m_destroy_in_process && m_comm.IsRunning()) +                { +                    // We are trying to destroy our connection and we are running +                    bp_site->SetEnabled(false); +                } +                else +                { +                    if (m_comm.SendRequestBreakpoint(false, bp_site->GetLoadAddress())) +                        bp_site->SetEnabled(false); +                    else +                        error.SetErrorString ("KDP remove breakpoint failed"); +                } +            } +            else +            { +                error = DisableSoftwareBreakpoint (bp_site); +            } +        } +        return error; +    } +    return DisableSoftwareBreakpoint (bp_site); +} + +Error +ProcessKDP::EnableWatchpoint (Watchpoint *wp, bool notify) +{ +    Error error; +    error.SetErrorString ("watchpoints are not suppported in kdp remote debugging"); +    return error; +} + +Error +ProcessKDP::DisableWatchpoint (Watchpoint *wp, bool notify) +{ +    Error error; +    error.SetErrorString ("watchpoints are not suppported in kdp remote debugging"); +    return error; +} + +void +ProcessKDP::Clear() +{ +    m_thread_list.Clear(); +} + +Error +ProcessKDP::DoSignal (int signo) +{ +    Error error; +    error.SetErrorString ("sending signals is not suppported in kdp remote debugging"); +    return error; +} + +void +ProcessKDP::Initialize() +{ +    static std::once_flag g_once_flag; + +    std::call_once(g_once_flag, []() +    { +        PluginManager::RegisterPlugin (GetPluginNameStatic(), +                                       GetPluginDescriptionStatic(), +                                       CreateInstance, +                                       DebuggerInitialize); + +        Log::Callbacks log_callbacks = { +            ProcessKDPLog::DisableLog, +            ProcessKDPLog::EnableLog, +            ProcessKDPLog::ListLogCategories +        }; + +        Log::RegisterLogChannel (ProcessKDP::GetPluginNameStatic(), log_callbacks); +    }); +} + +void +ProcessKDP::DebuggerInitialize (lldb_private::Debugger &debugger) +{ +    if (!PluginManager::GetSettingForProcessPlugin(debugger, PluginProperties::GetSettingName())) +    { +        const bool is_global_setting = true; +        PluginManager::CreateSettingForProcessPlugin (debugger, +                                                      GetGlobalPluginProperties()->GetValueProperties(), +                                                      ConstString ("Properties for the kdp-remote process plug-in."), +                                                      is_global_setting); +    } +} + +bool +ProcessKDP::StartAsyncThread () +{ +    Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); +     +    if (log) +        log->Printf ("ProcessKDP::StartAsyncThread ()"); + +    if (m_async_thread.IsJoinable()) +        return true; + +    m_async_thread = ThreadLauncher::LaunchThread("<lldb.process.kdp-remote.async>", ProcessKDP::AsyncThread, this, NULL); +    return m_async_thread.IsJoinable(); +} + +void +ProcessKDP::StopAsyncThread () +{ +    Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PROCESS)); +     +    if (log) +        log->Printf ("ProcessKDP::StopAsyncThread ()"); +     +    m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit); +     +    // Stop the stdio thread +    if (m_async_thread.IsJoinable()) +        m_async_thread.Join(nullptr); +} + + +void * +ProcessKDP::AsyncThread (void *arg) +{ +    ProcessKDP *process = (ProcessKDP*) arg; +     +    const lldb::pid_t pid = process->GetID(); + +    Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PROCESS)); +    if (log) +        log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread starting...", arg, pid); +     +    Listener listener ("ProcessKDP::AsyncThread"); +    EventSP event_sp; +    const uint32_t desired_event_mask = eBroadcastBitAsyncContinue | +                                        eBroadcastBitAsyncThreadShouldExit; +     +     +    if (listener.StartListeningForEvents (&process->m_async_broadcaster, desired_event_mask) == desired_event_mask) +    { +        bool done = false; +        while (!done) +        { +            if (log) +                log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp)...", +                             pid); +            if (listener.WaitForEvent (NULL, event_sp)) +            { +                uint32_t event_type = event_sp->GetType(); +                if (log) +                    log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") Got an event of type: %d...", +                                 pid, +                                 event_type); +                 +                // When we are running, poll for 1 second to try and get an exception +                // to indicate the process has stopped. If we don't get one, check to +                // make sure no one asked us to exit +                bool is_running = false; +                DataExtractor exc_reply_packet; +                do +                { +                    switch (event_type) +                    { +                    case eBroadcastBitAsyncContinue: +                        { +                            is_running = true; +                            if (process->m_comm.WaitForPacketWithTimeoutMicroSeconds (exc_reply_packet, 1 * USEC_PER_SEC)) +                            { +                                ThreadSP thread_sp (process->GetKernelThread()); +                                if (thread_sp) +                                { +                                    lldb::RegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext()); +                                    if (reg_ctx_sp) +                                        reg_ctx_sp->InvalidateAllRegisters(); +                                    static_cast<ThreadKDP *>(thread_sp.get())->SetStopInfoFrom_KDP_EXCEPTION (exc_reply_packet); +                                } + +                                // TODO: parse the stop reply packet +                                is_running = false;                                 +                                process->SetPrivateState(eStateStopped); +                            } +                            else +                            { +                                // Check to see if we are supposed to exit. There is no way to +                                // interrupt a running kernel, so all we can do is wait for an +                                // exception or detach... +                                if (listener.GetNextEvent(event_sp)) +                                { +                                    // We got an event, go through the loop again +                                    event_type = event_sp->GetType(); +                                } +                            } +                        } +                        break; +                             +                    case eBroadcastBitAsyncThreadShouldExit: +                        if (log) +                            log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got eBroadcastBitAsyncThreadShouldExit...", +                                         pid); +                        done = true; +                        is_running = false; +                        break; +                             +                    default: +                        if (log) +                            log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") got unknown event 0x%8.8x", +                                         pid, +                                         event_type); +                        done = true; +                        is_running = false; +                        break; +                    } +                } while (is_running); +            } +            else +            { +                if (log) +                    log->Printf ("ProcessKDP::AsyncThread (pid = %" PRIu64 ") listener.WaitForEvent (NULL, event_sp) => false", +                                 pid); +                done = true; +            } +        } +    } +     +    if (log) +        log->Printf ("ProcessKDP::AsyncThread (arg = %p, pid = %" PRIu64 ") thread exiting...", +                     arg, +                     pid); + +    process->m_async_thread.Reset(); +    return NULL; +} + + +class CommandObjectProcessKDPPacketSend : public CommandObjectParsed +{ +private: +     +    OptionGroupOptions m_option_group; +    OptionGroupUInt64 m_command_byte; +    OptionGroupString m_packet_data; +     +    virtual Options * +    GetOptions () +    { +        return &m_option_group; +    } +     + +public: +    CommandObjectProcessKDPPacketSend(CommandInterpreter &interpreter) : +        CommandObjectParsed (interpreter, +                             "process plugin packet send", +                             "Send a custom packet through the KDP protocol by specifying the command byte and the packet payload data. A packet will be sent with a correct header and payload, and the raw result bytes will be displayed as a string value. ", +                             NULL), +        m_option_group (interpreter), +        m_command_byte(LLDB_OPT_SET_1, true , "command", 'c', 0, eArgTypeNone, "Specify the command byte to use when sending the KDP request packet.", 0), +        m_packet_data (LLDB_OPT_SET_1, false, "payload", 'p', 0, eArgTypeNone, "Specify packet payload bytes as a hex ASCII string with no spaces or hex prefixes.", NULL) +    { +        m_option_group.Append (&m_command_byte, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); +        m_option_group.Append (&m_packet_data , LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); +        m_option_group.Finalize(); +    } +     +    ~CommandObjectProcessKDPPacketSend () +    { +    } +     +    bool +    DoExecute (Args& command, CommandReturnObject &result) +    { +        const size_t argc = command.GetArgumentCount(); +        if (argc == 0) +        { +            if (!m_command_byte.GetOptionValue().OptionWasSet()) +            { +                result.AppendError ("the --command option must be set to a valid command byte"); +                result.SetStatus (eReturnStatusFailed); +            } +            else +            { +                const uint64_t command_byte = m_command_byte.GetOptionValue().GetUInt64Value(0); +                if (command_byte > 0 && command_byte <= UINT8_MAX) +                { +                    ProcessKDP *process = (ProcessKDP *)m_interpreter.GetExecutionContext().GetProcessPtr(); +                    if (process) +                    { +                        const StateType state = process->GetState(); +                         +                        if (StateIsStoppedState (state, true)) +                        { +                            std::vector<uint8_t> payload_bytes; +                            const char *ascii_hex_bytes_cstr = m_packet_data.GetOptionValue().GetCurrentValue(); +                            if (ascii_hex_bytes_cstr && ascii_hex_bytes_cstr[0]) +                            { +                                StringExtractor extractor(ascii_hex_bytes_cstr); +                                const size_t ascii_hex_bytes_cstr_len = extractor.GetStringRef().size(); +                                if (ascii_hex_bytes_cstr_len & 1) +                                { +                                    result.AppendErrorWithFormat ("payload data must contain an even number of ASCII hex characters: '%s'", ascii_hex_bytes_cstr); +                                    result.SetStatus (eReturnStatusFailed); +                                    return false; +                                } +                                payload_bytes.resize(ascii_hex_bytes_cstr_len/2); +                                if (extractor.GetHexBytes(&payload_bytes[0], payload_bytes.size(), '\xdd') != payload_bytes.size()) +                                { +                                    result.AppendErrorWithFormat ("payload data must only contain ASCII hex characters (no spaces or hex prefixes): '%s'", ascii_hex_bytes_cstr); +                                    result.SetStatus (eReturnStatusFailed); +                                    return false; +                                } +                            } +                            Error error; +                            DataExtractor reply; +                            process->GetCommunication().SendRawRequest (command_byte, +                                                                        payload_bytes.empty() ? NULL : payload_bytes.data(), +                                                                        payload_bytes.size(), +                                                                        reply, +                                                                        error); +                             +                            if (error.Success()) +                            { +                                // Copy the binary bytes into a hex ASCII string for the result +                                StreamString packet; +                                packet.PutBytesAsRawHex8(reply.GetDataStart(), +                                                         reply.GetByteSize(), +                                                         endian::InlHostByteOrder(), +                                                         endian::InlHostByteOrder()); +                                result.AppendMessage(packet.GetString().c_str()); +                                result.SetStatus (eReturnStatusSuccessFinishResult); +                                return true; +                            } +                            else +                            { +                                const char *error_cstr = error.AsCString(); +                                if (error_cstr && error_cstr[0]) +                                    result.AppendError (error_cstr); +                                else +                                    result.AppendErrorWithFormat ("unknown error 0x%8.8x", error.GetError()); +                                result.SetStatus (eReturnStatusFailed); +                                return false; +                            } +                        } +                        else +                        { +                            result.AppendErrorWithFormat ("process must be stopped in order to send KDP packets, state is %s", StateAsCString (state)); +                            result.SetStatus (eReturnStatusFailed); +                        } +                    } +                    else +                    { +                        result.AppendError ("invalid process"); +                        result.SetStatus (eReturnStatusFailed); +                    } +                } +                else +                { +                    result.AppendErrorWithFormat ("invalid command byte 0x%" PRIx64 ", valid values are 1 - 255", command_byte); +                    result.SetStatus (eReturnStatusFailed); +                } +            } +        } +        else +        { +            result.AppendErrorWithFormat ("'%s' takes no arguments, only options.", m_cmd_name.c_str()); +            result.SetStatus (eReturnStatusFailed); +        } +        return false; +    } +}; + +class CommandObjectProcessKDPPacket : public CommandObjectMultiword +{ +private: + +public: +    CommandObjectProcessKDPPacket(CommandInterpreter &interpreter) : +    CommandObjectMultiword (interpreter, +                            "process plugin packet", +                            "Commands that deal with KDP remote packets.", +                            NULL) +    { +        LoadSubCommand ("send", CommandObjectSP (new CommandObjectProcessKDPPacketSend (interpreter))); +    } +     +    ~CommandObjectProcessKDPPacket () +    { +    } +}; + +class CommandObjectMultiwordProcessKDP : public CommandObjectMultiword +{ +public: +    CommandObjectMultiwordProcessKDP (CommandInterpreter &interpreter) : +    CommandObjectMultiword (interpreter, +                            "process plugin", +                            "A set of commands for operating on a ProcessKDP process.", +                            "process plugin <subcommand> [<subcommand-options>]") +    { +        LoadSubCommand ("packet", CommandObjectSP (new CommandObjectProcessKDPPacket    (interpreter))); +    } +     +    ~CommandObjectMultiwordProcessKDP () +    { +    } +}; + +CommandObject * +ProcessKDP::GetPluginCommandObject() +{ +    if (!m_command_sp) +        m_command_sp.reset (new CommandObjectMultiwordProcessKDP (GetTarget().GetDebugger().GetCommandInterpreter())); +    return m_command_sp.get(); +} + diff --git a/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.h b/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.h new file mode 100644 index 000000000000..fe9a4e2844bf --- /dev/null +++ b/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.h @@ -0,0 +1,274 @@ +//===-- ProcessKDP.h --------------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_ProcessKDP_h_ +#define liblldb_ProcessKDP_h_ + +// C Includes + +// C++ Includes +#include <list> +#include <vector> + +// Other libraries and framework includes +#include "lldb/Core/ArchSpec.h" +#include "lldb/Core/Broadcaster.h" +#include "lldb/Core/ConstString.h" +#include "lldb/Core/Error.h" +#include "lldb/Core/StreamString.h" +#include "lldb/Core/StringList.h" +#include "lldb/Core/ThreadSafeValue.h" +#include "lldb/Host/HostThread.h" +#include "lldb/Target/Process.h" +#include "lldb/Target/Thread.h" + +#include "CommunicationKDP.h" + +class ThreadKDP; + +class ProcessKDP : public lldb_private::Process +{ +public: +    //------------------------------------------------------------------ +    // Constructors and Destructors +    //------------------------------------------------------------------ +    static lldb::ProcessSP +    CreateInstance (lldb::TargetSP target_sp, +                    lldb_private::Listener &listener, +                    const lldb_private::FileSpec *crash_file_path); +     +    static void +    Initialize(); +     +    static void +    DebuggerInitialize (lldb_private::Debugger &debugger); + +    static void +    Terminate(); +     +    static lldb_private::ConstString +    GetPluginNameStatic(); +     +    static const char * +    GetPluginDescriptionStatic(); +     +    //------------------------------------------------------------------ +    // Constructors and Destructors +    //------------------------------------------------------------------ +    ProcessKDP(lldb::TargetSP target_sp, lldb_private::Listener &listener); +     +    virtual +    ~ProcessKDP(); +     +    //------------------------------------------------------------------ +    // Check if a given Process +    //------------------------------------------------------------------ +    virtual bool +    CanDebug (lldb::TargetSP target_sp, +              bool plugin_specified_by_name); +     +    virtual lldb_private::CommandObject * +    GetPluginCommandObject(); +     +    //------------------------------------------------------------------ +    // Creating a new process, or attaching to an existing one +    //------------------------------------------------------------------ +    virtual lldb_private::Error +    WillLaunch (lldb_private::Module* module); +     +    virtual lldb_private::Error +    DoLaunch (lldb_private::Module *exe_module,  +              lldb_private::ProcessLaunchInfo &launch_info); +     +    virtual lldb_private::Error +    WillAttachToProcessWithID (lldb::pid_t pid); +     +    virtual lldb_private::Error +    WillAttachToProcessWithName (const char *process_name, bool wait_for_launch); +     +    virtual lldb_private::Error +    DoConnectRemote (lldb_private::Stream *strm, const char *remote_url); +     +    virtual lldb_private::Error +    DoAttachToProcessWithID (lldb::pid_t pid, const lldb_private::ProcessAttachInfo &attach_info); +     +    virtual lldb_private::Error +    DoAttachToProcessWithName (const char *process_name, const lldb_private::ProcessAttachInfo &attach_info); +     +    virtual void +    DidAttach (lldb_private::ArchSpec &process_arch); +     +    lldb::addr_t +    GetImageInfoAddress(); + +    lldb_private::DynamicLoader * +    GetDynamicLoader (); + +    //------------------------------------------------------------------ +    // PluginInterface protocol +    //------------------------------------------------------------------ +    virtual lldb_private::ConstString +    GetPluginName(); +     +    virtual uint32_t +    GetPluginVersion(); +     +    //------------------------------------------------------------------ +    // Process Control +    //------------------------------------------------------------------ +    virtual lldb_private::Error +    WillResume (); +     +    virtual lldb_private::Error +    DoResume (); +     +    virtual lldb_private::Error +    DoHalt (bool &caused_stop); +     +    virtual lldb_private::Error +    DoDetach (bool keep_stopped); +     +    virtual lldb_private::Error +    DoSignal (int signal); +     +    virtual lldb_private::Error +    DoDestroy (); +     +    virtual void +    RefreshStateAfterStop(); +     +    //------------------------------------------------------------------ +    // Process Queries +    //------------------------------------------------------------------ +    virtual bool +    IsAlive (); +     +    //------------------------------------------------------------------ +    // Process Memory +    //------------------------------------------------------------------ +    virtual size_t +    DoReadMemory (lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error); +     +    virtual size_t +    DoWriteMemory (lldb::addr_t addr, const void *buf, size_t size, lldb_private::Error &error); +     +    virtual lldb::addr_t +    DoAllocateMemory (size_t size, uint32_t permissions, lldb_private::Error &error); +     +    virtual lldb_private::Error +    DoDeallocateMemory (lldb::addr_t ptr); + +    //---------------------------------------------------------------------- +    // Process Breakpoints +    //---------------------------------------------------------------------- +    virtual lldb_private::Error +    EnableBreakpointSite (lldb_private::BreakpointSite *bp_site); +     +    virtual lldb_private::Error +    DisableBreakpointSite (lldb_private::BreakpointSite *bp_site); +     +    //---------------------------------------------------------------------- +    // Process Watchpoints +    //---------------------------------------------------------------------- +    virtual lldb_private::Error +    EnableWatchpoint (lldb_private::Watchpoint *wp, bool notify = true); +     +    virtual lldb_private::Error +    DisableWatchpoint (lldb_private::Watchpoint *wp, bool notify = true); +     +    CommunicationKDP & +    GetCommunication() +    { +        return m_comm; +    } + +protected: +    friend class ThreadKDP; +    friend class CommunicationKDP; +     +    //---------------------------------------------------------------------- +    // Accessors +    //---------------------------------------------------------------------- +    bool +    IsRunning ( lldb::StateType state ) +    { +        return    state == lldb::eStateRunning || IsStepping(state); +    } +     +    bool +    IsStepping ( lldb::StateType state) +    { +        return    state == lldb::eStateStepping; +    } + +    bool +    CanResume ( lldb::StateType state) +    { +        return state == lldb::eStateStopped; +    } +     +    bool +    HasExited (lldb::StateType state) +    { +        return state == lldb::eStateExited; +    } + +    bool +    GetHostArchitecture (lldb_private::ArchSpec &arch); + +    bool +    ProcessIDIsValid ( ) const; +     +    void +    Clear ( ); +     +    virtual bool +    UpdateThreadList (lldb_private::ThreadList &old_thread_list,  +                      lldb_private::ThreadList &new_thread_list); +     +    enum +    { +        eBroadcastBitAsyncContinue                  = (1 << 0), +        eBroadcastBitAsyncThreadShouldExit          = (1 << 1) +    }; +     +    lldb::ThreadSP +    GetKernelThread (); + +    //------------------------------------------------------------------ +    /// Broadcaster event bits definitions. +    //------------------------------------------------------------------ +    CommunicationKDP m_comm; +    lldb_private::Broadcaster m_async_broadcaster; +    lldb_private::HostThread m_async_thread; +    lldb_private::ConstString m_dyld_plugin_name; +    lldb::addr_t m_kernel_load_addr; +    lldb::CommandObjectSP m_command_sp; +    lldb::ThreadWP m_kernel_thread_wp; + + +    bool +    StartAsyncThread (); +     +    void +    StopAsyncThread (); +     +    static void * +    AsyncThread (void *arg); +     +private: +    //------------------------------------------------------------------ +    // For ProcessKDP only +    //------------------------------------------------------------------ +     +    DISALLOW_COPY_AND_ASSIGN (ProcessKDP); +     +}; + +#endif  // liblldb_ProcessKDP_h_ diff --git a/source/Plugins/Process/MacOSX-Kernel/ProcessKDPLog.cpp b/source/Plugins/Process/MacOSX-Kernel/ProcessKDPLog.cpp new file mode 100644 index 000000000000..79cb62aa0066 --- /dev/null +++ b/source/Plugins/Process/MacOSX-Kernel/ProcessKDPLog.cpp @@ -0,0 +1,186 @@ +//===-- ProcessKDPLog.cpp ---------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "ProcessKDPLog.h" + +#include "lldb/Interpreter/Args.h" +#include "lldb/Core/StreamFile.h" + +#include "ProcessKDP.h" + +using namespace lldb; +using namespace lldb_private; + + +// We want to avoid global constructors where code needs to be run so here we +// control access to our static g_log_sp by hiding it in a singleton function +// that will construct the static g_log_sp the first time this function is  +// called. +static bool g_log_enabled = false; +static Log * g_log = NULL; +static Log * +GetLog () +{ +    if (!g_log_enabled) +        return NULL; +    return g_log; +} + +Log * +ProcessKDPLog::GetLogIfAllCategoriesSet (uint32_t mask) +{ +    Log *log(GetLog ()); +    if (log && mask) +    { +        uint32_t log_mask = log->GetMask().Get(); +        if ((log_mask & mask) != mask) +            return NULL; +    } +    return log; +} + +void +ProcessKDPLog::DisableLog (const char **categories, Stream *feedback_strm) +{ +    Log *log (GetLog ()); +    if (log) +    { +        uint32_t flag_bits = 0; +         +        if (categories[0] != NULL) +        { +            flag_bits = log->GetMask().Get(); +            for (size_t i = 0; categories[i] != NULL; ++i) +            { +                const char *arg = categories[i]; +                 + +                if      (::strcasecmp (arg, "all")        == 0 ) flag_bits &= ~KDP_LOG_ALL; +                else if (::strcasecmp (arg, "async")      == 0 ) flag_bits &= ~KDP_LOG_ASYNC; +                else if (::strncasecmp (arg, "break", 5)  == 0 ) flag_bits &= ~KDP_LOG_BREAKPOINTS; +                else if (::strncasecmp (arg, "comm", 4)   == 0 ) flag_bits &= ~KDP_LOG_COMM; +                else if (::strcasecmp (arg, "default")    == 0 ) flag_bits &= ~KDP_LOG_DEFAULT; +                else if (::strcasecmp (arg, "packets")    == 0 ) flag_bits &= ~KDP_LOG_PACKETS; +                else if (::strcasecmp (arg, "memory")     == 0 ) flag_bits &= ~KDP_LOG_MEMORY; +                else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits &= ~KDP_LOG_MEMORY_DATA_SHORT; +                else if (::strcasecmp (arg, "data-long")  == 0 ) flag_bits &= ~KDP_LOG_MEMORY_DATA_LONG; +                else if (::strcasecmp (arg, "process")    == 0 ) flag_bits &= ~KDP_LOG_PROCESS; +                else if (::strcasecmp (arg, "step")       == 0 ) flag_bits &= ~KDP_LOG_STEP; +                else if (::strcasecmp (arg, "thread")     == 0 ) flag_bits &= ~KDP_LOG_THREAD; +                else if (::strcasecmp (arg, "verbose")    == 0 ) flag_bits &= ~KDP_LOG_VERBOSE; +                else if (::strncasecmp (arg, "watch", 5)  == 0 ) flag_bits &= ~KDP_LOG_WATCHPOINTS; +                else +                { +                    feedback_strm->Printf("error: unrecognized log category '%s'\n", arg); +                    ListLogCategories (feedback_strm); +                } +                 +            } +        } +         +        log->GetMask().Reset (flag_bits); +        if (flag_bits == 0) +            g_log_enabled = false; +    } +     +    return; +} + +Log * +ProcessKDPLog::EnableLog (StreamSP &log_stream_sp, uint32_t log_options, const char **categories, Stream *feedback_strm) +{ +    // Try see if there already is a log - that way we can reuse its settings. +    // We could reuse the log in toto, but we don't know that the stream is the same. +    uint32_t flag_bits = 0; +    if (g_log) +        flag_bits = g_log->GetMask().Get(); + +    // Now make a new log with this stream if one was provided +    if (log_stream_sp) +    { +        if (g_log) +            g_log->SetStream(log_stream_sp); +        else +            g_log = new Log(log_stream_sp); +    } + +    if (g_log) +    { +        bool got_unknown_category = false; +        for (size_t i=0; categories[i] != NULL; ++i) +        { +            const char *arg = categories[i]; + +            if      (::strcasecmp (arg, "all")        == 0 ) flag_bits |= KDP_LOG_ALL; +            else if (::strcasecmp (arg, "async")      == 0 ) flag_bits |= KDP_LOG_ASYNC; +            else if (::strncasecmp (arg, "break", 5)  == 0 ) flag_bits |= KDP_LOG_BREAKPOINTS; +            else if (::strncasecmp (arg, "comm", 4)   == 0 ) flag_bits |= KDP_LOG_COMM; +            else if (::strcasecmp (arg, "default")    == 0 ) flag_bits |= KDP_LOG_DEFAULT; +            else if (::strcasecmp (arg, "packets")    == 0 ) flag_bits |= KDP_LOG_PACKETS; +            else if (::strcasecmp (arg, "memory")     == 0 ) flag_bits |= KDP_LOG_MEMORY; +            else if (::strcasecmp (arg, "data-short") == 0 ) flag_bits |= KDP_LOG_MEMORY_DATA_SHORT; +            else if (::strcasecmp (arg, "data-long")  == 0 ) flag_bits |= KDP_LOG_MEMORY_DATA_LONG; +            else if (::strcasecmp (arg, "process")    == 0 ) flag_bits |= KDP_LOG_PROCESS; +            else if (::strcasecmp (arg, "step")       == 0 ) flag_bits |= KDP_LOG_STEP; +            else if (::strcasecmp (arg, "thread")     == 0 ) flag_bits |= KDP_LOG_THREAD; +            else if (::strcasecmp (arg, "verbose")    == 0 ) flag_bits |= KDP_LOG_VERBOSE; +            else if (::strncasecmp (arg, "watch", 5)  == 0 ) flag_bits |= KDP_LOG_WATCHPOINTS; +            else +            { +                feedback_strm->Printf("error: unrecognized log category '%s'\n", arg); +                if (got_unknown_category == false) +                { +                    got_unknown_category = true; +                    ListLogCategories (feedback_strm); +                } +            } +        } +        if (flag_bits == 0) +            flag_bits = KDP_LOG_DEFAULT; +        g_log->GetMask().Reset(flag_bits); +        g_log->GetOptions().Reset(log_options); +    } +    g_log_enabled = true; +    return g_log; +} + +void +ProcessKDPLog::ListLogCategories (Stream *strm) +{ +    strm->Printf ("Logging categories for '%s':\n" +                  "  all - turn on all available logging categories\n" +                  "  async - log asynchronous activity\n" +                  "  break - log breakpoints\n" +                  "  communication - log communication activity\n" +                  "  default - enable the default set of logging categories for liblldb\n" +                  "  packets - log gdb remote packets\n" +                  "  memory - log memory reads and writes\n" +                  "  data-short - log memory bytes for memory reads and writes for short transactions only\n" +                  "  data-long - log memory bytes for memory reads and writes for all transactions\n" +                  "  process - log process events and activities\n" +                  "  thread - log thread events and activities\n" +                  "  step - log step related activities\n" +                  "  verbose - enable verbose logging\n" +                  "  watch - log watchpoint related activities\n", +                  ProcessKDP::GetPluginNameStatic().GetCString()); +} + + +void +ProcessKDPLog::LogIf (uint32_t mask, const char *format, ...) +{ +    Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (mask)); +    if (log) +    { +        va_list args; +        va_start (args, format); +        log->VAPrintf (format, args); +        va_end (args); +    } +} diff --git a/source/Plugins/Process/MacOSX-Kernel/ProcessKDPLog.h b/source/Plugins/Process/MacOSX-Kernel/ProcessKDPLog.h new file mode 100644 index 000000000000..0cb32d9b2dcf --- /dev/null +++ b/source/Plugins/Process/MacOSX-Kernel/ProcessKDPLog.h @@ -0,0 +1,54 @@ +//===-- ProcessKDPLog.h -----------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_ProcessKDPLog_h_ +#define liblldb_ProcessKDPLog_h_ + +// C Includes +// C++ Includes +// Other libraries and framework includes + +// Project includes +#include "lldb/Core/Log.h" + +#define KDP_LOG_VERBOSE                  (1u << 0) +#define KDP_LOG_PROCESS                  (1u << 1) +#define KDP_LOG_THREAD                   (1u << 2) +#define KDP_LOG_PACKETS                  (1u << 3) +#define KDP_LOG_MEMORY                   (1u << 4)    // Log memory reads/writes calls +#define KDP_LOG_MEMORY_DATA_SHORT        (1u << 5)    // Log short memory reads/writes bytes +#define KDP_LOG_MEMORY_DATA_LONG         (1u << 6)    // Log all memory reads/writes bytes +#define KDP_LOG_BREAKPOINTS              (1u << 7) +#define KDP_LOG_WATCHPOINTS              (1u << 8) +#define KDP_LOG_STEP                     (1u << 9) +#define KDP_LOG_COMM                     (1u << 10) +#define KDP_LOG_ASYNC                    (1u << 11) +#define KDP_LOG_ALL                      (UINT32_MAX) +#define KDP_LOG_DEFAULT                  KDP_LOG_PACKETS + +class ProcessKDPLog +{ +public: +    static lldb_private::Log * +    GetLogIfAllCategoriesSet(uint32_t mask = 0); + +    static void +    DisableLog (const char **categories, lldb_private::Stream *feedback_strm); + +    static lldb_private::Log * +    EnableLog (lldb::StreamSP &log_stream_sp, uint32_t log_options, const char **categories, lldb_private::Stream *feedback_strm); + +    static void +    ListLogCategories (lldb_private::Stream *strm); + +    static void +    LogIf (uint32_t mask, const char *format, ...); +}; + +#endif  // liblldb_ProcessKDPLog_h_ diff --git a/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_arm.cpp b/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_arm.cpp new file mode 100644 index 000000000000..449ac646ab3c --- /dev/null +++ b/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_arm.cpp @@ -0,0 +1,161 @@ +//===-- RegisterContextKDP_arm.cpp ------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "RegisterContextKDP_arm.h" + +// C Includes +// C++ Includes +// Other libraries and framework includes +// Project includes +#include "ProcessKDP.h" +#include "ThreadKDP.h" + +using namespace lldb; +using namespace lldb_private; + + +RegisterContextKDP_arm::RegisterContextKDP_arm (ThreadKDP &thread, uint32_t concrete_frame_idx) : +    RegisterContextDarwin_arm (thread, concrete_frame_idx), +    m_kdp_thread (thread) +{ +} + +RegisterContextKDP_arm::~RegisterContextKDP_arm() +{ +} + +int +RegisterContextKDP_arm::DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestReadRegisters (tid, GPRRegSet, &gpr, sizeof(gpr), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_arm::DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestReadRegisters (tid, FPURegSet, &fpu, sizeof(fpu), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_arm::DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestReadRegisters (tid, EXCRegSet, &exc, sizeof(exc), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_arm::DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestReadRegisters (tid, DBGRegSet, &dbg, sizeof(dbg), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_arm::DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestWriteRegisters (tid, GPRRegSet, &gpr, sizeof(gpr), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_arm::DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestWriteRegisters (tid, FPURegSet, &fpu, sizeof(fpu), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_arm::DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestWriteRegisters (tid, EXCRegSet, &exc, sizeof(exc), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_arm::DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestWriteRegisters (tid, DBGRegSet, &dbg, sizeof(dbg), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + + diff --git a/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_arm.h b/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_arm.h new file mode 100644 index 000000000000..1e547289d9ce --- /dev/null +++ b/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_arm.h @@ -0,0 +1,61 @@ +//===-- RegisterContextKDP_arm.h --------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_RegisterContextKDP_arm_h_ +#define liblldb_RegisterContextKDP_arm_h_ + +// C Includes + +// C++ Includes +// Other libraries and framework includes +// Project includes +#include "Plugins/Process/Utility/RegisterContextDarwin_arm.h" + +class ThreadKDP; + +class RegisterContextKDP_arm : public RegisterContextDarwin_arm +{ +public: + +    RegisterContextKDP_arm (ThreadKDP &thread,  +                            uint32_t concrete_frame_idx); + +    virtual +    ~RegisterContextKDP_arm(); + +protected: + +    virtual int +    DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr); +     +    int +    DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu); +     +    int +    DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc); +     +    int +    DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg); +     +    int +    DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr); +     +    int +    DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu); +     +    int +    DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc); +     +    int +    DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg); +     +    ThreadKDP &m_kdp_thread; +}; + +#endif  // liblldb_RegisterContextKDP_arm_h_ diff --git a/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_arm64.cpp b/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_arm64.cpp new file mode 100644 index 000000000000..ed62f1982d3c --- /dev/null +++ b/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_arm64.cpp @@ -0,0 +1,161 @@ +//===-- RegisterContextKDP_arm64.cpp ------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "RegisterContextKDP_arm64.h" + +// C Includes +// C++ Includes +// Other libraries and framework includes +// Project includes +#include "ProcessKDP.h" +#include "ThreadKDP.h" + +using namespace lldb; +using namespace lldb_private; + + +RegisterContextKDP_arm64::RegisterContextKDP_arm64 (ThreadKDP &thread, uint32_t concrete_frame_idx) : +    RegisterContextDarwin_arm64 (thread, concrete_frame_idx), +    m_kdp_thread (thread) +{ +} + +RegisterContextKDP_arm64::~RegisterContextKDP_arm64() +{ +} + +int +RegisterContextKDP_arm64::DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestReadRegisters (tid, GPRRegSet, &gpr, sizeof(gpr), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_arm64::DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestReadRegisters (tid, FPURegSet, &fpu, sizeof(fpu), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_arm64::DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestReadRegisters (tid, EXCRegSet, &exc, sizeof(exc), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_arm64::DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestReadRegisters (tid, DBGRegSet, &dbg, sizeof(dbg), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_arm64::DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestWriteRegisters (tid, GPRRegSet, &gpr, sizeof(gpr), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_arm64::DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestWriteRegisters (tid, FPURegSet, &fpu, sizeof(fpu), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_arm64::DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestWriteRegisters (tid, EXCRegSet, &exc, sizeof(exc), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_arm64::DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestWriteRegisters (tid, DBGRegSet, &dbg, sizeof(dbg), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + + diff --git a/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_arm64.h b/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_arm64.h new file mode 100644 index 000000000000..8780b7be4a9a --- /dev/null +++ b/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_arm64.h @@ -0,0 +1,61 @@ +//===-- RegisterContextKDP_arm64.h --------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_RegisterContextKDP_arm64_h_ +#define liblldb_RegisterContextKDP_arm64_h_ + +// C Includes + +// C++ Includes +// Other libraries and framework includes +// Project includes +#include "Plugins/Process/Utility/RegisterContextDarwin_arm64.h" + +class ThreadKDP; + +class RegisterContextKDP_arm64 : public RegisterContextDarwin_arm64 +{ +public: + +    RegisterContextKDP_arm64 (ThreadKDP &thread,  +                            uint32_t concrete_frame_idx); + +    virtual +    ~RegisterContextKDP_arm64(); + +protected: + +    virtual int +    DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr); +     +    int +    DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu); +     +    int +    DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc); +     +    int +    DoReadDBG (lldb::tid_t tid, int flavor, DBG &dbg); +     +    int +    DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr); +     +    int +    DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu); +     +    int +    DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc); +     +    int +    DoWriteDBG (lldb::tid_t tid, int flavor, const DBG &dbg); +     +    ThreadKDP &m_kdp_thread; +}; + +#endif  // liblldb_RegisterContextKDP_arm64_h_ diff --git a/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_i386.cpp b/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_i386.cpp new file mode 100644 index 000000000000..882b0c2e931d --- /dev/null +++ b/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_i386.cpp @@ -0,0 +1,129 @@ +//===-- RegisterContextKDP_i386.cpp -----------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + + +// C Includes +// C++ Includes +// Other libraries and framework includes +// Project includes +#include "RegisterContextKDP_i386.h" +#include "ProcessKDP.h" +#include "ThreadKDP.h" + +using namespace lldb; +using namespace lldb_private; + + +RegisterContextKDP_i386::RegisterContextKDP_i386 (ThreadKDP &thread, uint32_t concrete_frame_idx) : +    RegisterContextDarwin_i386 (thread, concrete_frame_idx), +    m_kdp_thread (thread) +{ +} + +RegisterContextKDP_i386::~RegisterContextKDP_i386() +{ +} + +int +RegisterContextKDP_i386::DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestReadRegisters (tid, GPRRegSet, &gpr, sizeof(gpr), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_i386::DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestReadRegisters (tid, FPURegSet, &fpu, sizeof(fpu), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_i386::DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestReadRegisters (tid, EXCRegSet, &exc, sizeof(exc), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_i386::DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestWriteRegisters (tid, GPRRegSet, &gpr, sizeof(gpr), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_i386::DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestWriteRegisters (tid, FPURegSet, &fpu, sizeof(fpu), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_i386::DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestWriteRegisters (tid, EXCRegSet, &exc, sizeof(exc), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + + diff --git a/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_i386.h b/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_i386.h new file mode 100644 index 000000000000..4b6bc5b262f7 --- /dev/null +++ b/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_i386.h @@ -0,0 +1,53 @@ +//===-- RegisterContextKDP_i386.h -------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_RegisterContextKDP_i386_h_ +#define liblldb_RegisterContextKDP_i386_h_ + +// C Includes +// C++ Includes +// Other libraries and framework includes +// Project includes +#include "Plugins/Process/Utility/RegisterContextDarwin_i386.h" + +class ThreadKDP; + +class RegisterContextKDP_i386 : public RegisterContextDarwin_i386 +{ +public: +    RegisterContextKDP_i386 (ThreadKDP &thread,  +                             uint32_t concrete_frame_idx); +     +    virtual +    ~RegisterContextKDP_i386(); +     +protected: +     +    virtual int +    DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr); +     +    int +    DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu); +     +    int +    DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc); +     +    int +    DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr); +     +    int +    DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu); +     +    int +    DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc); +     +    ThreadKDP &m_kdp_thread; +}; + +#endif  // liblldb_RegisterContextKDP_i386_h_ diff --git a/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_x86_64.cpp b/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_x86_64.cpp new file mode 100644 index 000000000000..f4247a5da272 --- /dev/null +++ b/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_x86_64.cpp @@ -0,0 +1,127 @@ +//===-- RegisterContextKDP_x86_64.cpp ---------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + + +// C Includes +// C++ Includes +// Other libraries and framework includes +// Project includes +#include "RegisterContextKDP_x86_64.h" +#include "ProcessKDP.h" +#include "ThreadKDP.h" + +using namespace lldb; +using namespace lldb_private; + + +RegisterContextKDP_x86_64::RegisterContextKDP_x86_64 (ThreadKDP &thread, uint32_t concrete_frame_idx) : +    RegisterContextDarwin_x86_64 (thread, concrete_frame_idx), +    m_kdp_thread (thread) +{ +} + +RegisterContextKDP_x86_64::~RegisterContextKDP_x86_64() +{ +} + +int +RegisterContextKDP_x86_64::DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestReadRegisters (tid, GPRRegSet, &gpr, sizeof(gpr), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_x86_64::DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestReadRegisters (tid, FPURegSet, &fpu, sizeof(fpu), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_x86_64::DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestReadRegisters (tid, EXCRegSet, &exc, sizeof(exc), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_x86_64::DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestWriteRegisters (tid, GPRRegSet, &gpr, sizeof(gpr), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_x86_64::DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestWriteRegisters (tid, FPURegSet, &fpu, sizeof(fpu), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} + +int +RegisterContextKDP_x86_64::DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc) +{ +    ProcessSP process_sp (CalculateProcess()); +    if (process_sp) +    { +        Error error; +        if (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().SendRequestWriteRegisters (tid, EXCRegSet, &exc, sizeof(exc), error)) +        { +            if (error.Success()) +                return 0; +        } +    } +    return -1; +} diff --git a/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_x86_64.h b/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_x86_64.h new file mode 100644 index 000000000000..a426349198ff --- /dev/null +++ b/source/Plugins/Process/MacOSX-Kernel/RegisterContextKDP_x86_64.h @@ -0,0 +1,54 @@ +//===-- RegisterContextKDP_x86_64.h -----------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_RegisterContextKDP_x86_64_h_ +#define liblldb_RegisterContextKDP_x86_64_h_ + +// C Includes +// C++ Includes +// Other libraries and framework includes +// Project includes +#include "Plugins/Process/Utility/RegisterContextDarwin_x86_64.h" + +class ThreadKDP; + +class RegisterContextKDP_x86_64 : public RegisterContextDarwin_x86_64 +{ +public: +     +    RegisterContextKDP_x86_64 (ThreadKDP &thread,  +                               uint32_t concrete_frame_idx); +     +    virtual +    ~RegisterContextKDP_x86_64(); +     +protected: +     +    virtual int +    DoReadGPR (lldb::tid_t tid, int flavor, GPR &gpr); +     +    int +    DoReadFPU (lldb::tid_t tid, int flavor, FPU &fpu); +     +    int +    DoReadEXC (lldb::tid_t tid, int flavor, EXC &exc); +     +    int +    DoWriteGPR (lldb::tid_t tid, int flavor, const GPR &gpr); +     +    int +    DoWriteFPU (lldb::tid_t tid, int flavor, const FPU &fpu); +     +    int +    DoWriteEXC (lldb::tid_t tid, int flavor, const EXC &exc); +     +    ThreadKDP &m_kdp_thread; +}; + +#endif  // liblldb_RegisterContextKDP_x86_64_h_ diff --git a/source/Plugins/Process/MacOSX-Kernel/ThreadKDP.cpp b/source/Plugins/Process/MacOSX-Kernel/ThreadKDP.cpp new file mode 100644 index 000000000000..3b8bed101a8a --- /dev/null +++ b/source/Plugins/Process/MacOSX-Kernel/ThreadKDP.cpp @@ -0,0 +1,211 @@ +//===-- ThreadKDP.cpp -------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + + +#include "ThreadKDP.h" + +#include "lldb/Utility/SafeMachO.h" + +#include "lldb/Core/ArchSpec.h" +#include "lldb/Core/DataExtractor.h" +#include "lldb/Core/StreamString.h" +#include "lldb/Core/State.h" +#include "lldb/Target/Process.h" +#include "lldb/Target/RegisterContext.h" +#include "lldb/Target/StopInfo.h" +#include "lldb/Target/Target.h" +#include "lldb/Target/Unwind.h" +#include "lldb/Breakpoint/Watchpoint.h" + +#include "ProcessKDP.h" +#include "ProcessKDPLog.h" +#include "RegisterContextKDP_arm.h" +#include "RegisterContextKDP_arm64.h" +#include "RegisterContextKDP_i386.h" +#include "RegisterContextKDP_x86_64.h" +#include "Plugins/Process/Utility/StopInfoMachException.h" + +using namespace lldb; +using namespace lldb_private; + +//---------------------------------------------------------------------- +// Thread Registers +//---------------------------------------------------------------------- + +ThreadKDP::ThreadKDP (Process &process, lldb::tid_t tid) : +    Thread(process, tid), +    m_thread_name (), +    m_dispatch_queue_name (), +    m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS) +{ +    ProcessKDPLog::LogIf(KDP_LOG_THREAD, "%p: ThreadKDP::ThreadKDP (tid = 0x%4.4x)", this, GetID()); +} + +ThreadKDP::~ThreadKDP () +{ +    ProcessKDPLog::LogIf(KDP_LOG_THREAD, "%p: ThreadKDP::~ThreadKDP (tid = 0x%4.4x)", this, GetID()); +    DestroyThread(); +} + +const char * +ThreadKDP::GetName () +{ +    if (m_thread_name.empty()) +        return NULL; +    return m_thread_name.c_str(); +} + +const char * +ThreadKDP::GetQueueName () +{ +    return NULL; +} + +void +ThreadKDP::RefreshStateAfterStop() +{ +    // Invalidate all registers in our register context. We don't set "force" to +    // true because the stop reply packet might have had some register values +    // that were expedited and these will already be copied into the register +    // context by the time this function gets called. The KDPRegisterContext +    // class has been made smart enough to detect when it needs to invalidate +    // which registers are valid by putting hooks in the register read and  +    // register supply functions where they check the process stop ID and do +    // the right thing. +    const bool force = false; +    lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext()); +    if (reg_ctx_sp) +        reg_ctx_sp->InvalidateIfNeeded (force); +} + +bool +ThreadKDP::ThreadIDIsValid (lldb::tid_t thread) +{ +    return thread != 0; +} + +void +ThreadKDP::Dump(Log *log, uint32_t index) +{ +} + + +bool +ThreadKDP::ShouldStop (bool &step_more) +{ +    return true; +} +lldb::RegisterContextSP +ThreadKDP::GetRegisterContext () +{ +    if (m_reg_context_sp.get() == NULL) +        m_reg_context_sp = CreateRegisterContextForFrame (NULL); +    return m_reg_context_sp; +} + +lldb::RegisterContextSP +ThreadKDP::CreateRegisterContextForFrame (StackFrame *frame) +{ +    lldb::RegisterContextSP reg_ctx_sp; +    uint32_t concrete_frame_idx = 0; +     +    if (frame) +        concrete_frame_idx = frame->GetConcreteFrameIndex (); + +    if (concrete_frame_idx == 0) +    { +        ProcessSP process_sp (CalculateProcess()); +        if (process_sp) +        { +            switch (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().GetCPUType()) +            { +                case llvm::MachO::CPU_TYPE_ARM: +                    reg_ctx_sp.reset (new RegisterContextKDP_arm (*this, concrete_frame_idx)); +                    break; +                case llvm::MachO::CPU_TYPE_ARM64: +                    reg_ctx_sp.reset (new RegisterContextKDP_arm64 (*this, concrete_frame_idx)); +                    break; +                case llvm::MachO::CPU_TYPE_I386: +                    reg_ctx_sp.reset (new RegisterContextKDP_i386 (*this, concrete_frame_idx)); +                    break; +                case llvm::MachO::CPU_TYPE_X86_64: +                    reg_ctx_sp.reset (new RegisterContextKDP_x86_64 (*this, concrete_frame_idx)); +                    break; +                default: +                    assert (!"Add CPU type support in KDP"); +                    break; +            } +        } +    } +    else +    { +        Unwind *unwinder = GetUnwinder (); +        if (unwinder) +            reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame); +    } +    return reg_ctx_sp; +} + +bool +ThreadKDP::CalculateStopInfo () +{ +    ProcessSP process_sp (GetProcess()); +    if (process_sp) +    { +        if (m_cached_stop_info_sp) +        { +            SetStopInfo (m_cached_stop_info_sp); +        } +        else +        { +            SetStopInfo(StopInfo::CreateStopReasonWithSignal (*this, SIGSTOP)); +        } +        return true; +    } +    return false; +} + +void +ThreadKDP::SetStopInfoFrom_KDP_EXCEPTION (const DataExtractor &exc_reply_packet) +{ +    lldb::offset_t offset = 0; +    uint8_t reply_command = exc_reply_packet.GetU8(&offset); +    if (reply_command == CommunicationKDP::KDP_EXCEPTION) +    { +        offset = 8; +        const uint32_t count = exc_reply_packet.GetU32 (&offset); +        if (count >= 1) +        { +            //const uint32_t cpu = exc_reply_packet.GetU32 (&offset); +            offset += 4; // Skip the useless CPU field +            const uint32_t exc_type = exc_reply_packet.GetU32 (&offset); +            const uint32_t exc_code = exc_reply_packet.GetU32 (&offset); +            const uint32_t exc_subcode = exc_reply_packet.GetU32 (&offset); +            // We have to make a copy of the stop info because the thread list +            // will iterate through the threads and clear all stop infos.. +             +            // Let the StopInfoMachException::CreateStopReasonWithMachException() +            // function update the PC if needed as we might hit a software breakpoint +            // and need to decrement the PC (i386 and x86_64 need this) and KDP +            // doesn't do this for us. +            const bool pc_already_adjusted = false; +            const bool adjust_pc_if_needed = true; + +            m_cached_stop_info_sp = StopInfoMachException::CreateStopReasonWithMachException (*this, +                                                                                              exc_type, +                                                                                              2, +                                                                                              exc_code, +                                                                                              exc_subcode, +                                                                                              0, +                                                                                              pc_already_adjusted, +                                                                                              adjust_pc_if_needed);             +        } +    } +} + diff --git a/source/Plugins/Process/MacOSX-Kernel/ThreadKDP.h b/source/Plugins/Process/MacOSX-Kernel/ThreadKDP.h new file mode 100644 index 000000000000..7dc373f03550 --- /dev/null +++ b/source/Plugins/Process/MacOSX-Kernel/ThreadKDP.h @@ -0,0 +1,98 @@ +//===-- ThreadKDP.h ---------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_ThreadKDP_h_ +#define liblldb_ThreadKDP_h_ + +#include <string> + +#include "lldb/Target/Process.h" +#include "lldb/Target/Thread.h" + +class ProcessKDP; + +class ThreadKDP : public lldb_private::Thread +{ +public: +    ThreadKDP (lldb_private::Process &process, +               lldb::tid_t tid); + +    virtual +    ~ThreadKDP (); + +    virtual void +    RefreshStateAfterStop(); + +    virtual const char * +    GetName (); + +    virtual const char * +    GetQueueName (); + +    virtual lldb::RegisterContextSP +    GetRegisterContext (); + +    virtual lldb::RegisterContextSP +    CreateRegisterContextForFrame (lldb_private::StackFrame *frame); + +    void +    Dump (lldb_private::Log *log, uint32_t index); + +    static bool +    ThreadIDIsValid (lldb::tid_t thread); + +    bool +    ShouldStop (bool &step_more); + +    const char * +    GetBasicInfoAsString (); + +    void +    SetName (const char *name) +    { +        if (name && name[0]) +            m_thread_name.assign (name); +        else +            m_thread_name.clear(); +    } + +    lldb::addr_t +    GetThreadDispatchQAddr () +    { +        return m_thread_dispatch_qaddr; +    } + +    void +    SetThreadDispatchQAddr (lldb::addr_t thread_dispatch_qaddr) +    { +        m_thread_dispatch_qaddr = thread_dispatch_qaddr; +    } +     +    void +    SetStopInfoFrom_KDP_EXCEPTION (const lldb_private::DataExtractor &exc_reply_packet); + +protected: +     +    friend class ProcessKDP; + +    //------------------------------------------------------------------ +    // Member variables. +    //------------------------------------------------------------------ +    std::string m_thread_name; +    std::string m_dispatch_queue_name; +    lldb::addr_t m_thread_dispatch_qaddr; +    lldb::StopInfoSP m_cached_stop_info_sp; +    //------------------------------------------------------------------ +    // Protected member functions. +    //------------------------------------------------------------------ +    virtual bool +    CalculateStopInfo (); +}; + +#endif  // liblldb_ThreadKDP_h_ diff --git a/source/Plugins/Process/POSIX/CMakeLists.txt b/source/Plugins/Process/POSIX/CMakeLists.txt new file mode 100644 index 000000000000..2ed7326dbb71 --- /dev/null +++ b/source/Plugins/Process/POSIX/CMakeLists.txt @@ -0,0 +1,8 @@ +include_directories(.) +include_directories(../Utility) + +add_lldb_library(lldbPluginProcessPOSIX +  CrashReason.cpp +  ProcessMessage.cpp +  ProcessPOSIXLog.cpp +  ) diff --git a/source/Plugins/Process/POSIX/Makefile b/source/Plugins/Process/POSIX/Makefile new file mode 100644 index 000000000000..e8ac3a8ae0ed --- /dev/null +++ b/source/Plugins/Process/POSIX/Makefile @@ -0,0 +1,32 @@ +##===- source/Plugins/Process/POSIX/Makefile ---------------*- Makefile -*-===## +#  +#                     The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +#  +##===----------------------------------------------------------------------===## + +LLDB_LEVEL := ../../../.. +LIBRARYNAME := lldbPluginProcessPOSIX +BUILD_ARCHIVE = 1 + +include $(LLDB_LEVEL)/../../Makefile.config + +# Extend the include path so we may locate UnwindLLDB.h +CPP.Flags += -I$(PROJ_SRC_DIR)/$(LLDB_LEVEL)/source/Plugins/Utility + +ifeq ($(HOST_OS),Linux) +CPP.Flags += -I$(PROJ_SRC_DIR)/$(LLDB_LEVEL)/source/Plugins/Process/Linux + +# Disable warning for now as offsetof is used with an index into a structure member array +# in defining register info tables. +CPP.Flags += -Wno-extended-offsetof +endif + +ifneq (,$(filter $(HOST_OS), FreeBSD GNU/kFreeBSD)) +# Extend the include path so we may locate ProcessMonitor +CPP.Flags += -I$(PROJ_SRC_DIR)/$(LLDB_LEVEL)/source/Plugins/Process/FreeBSD +endif + +include $(LLDB_LEVEL)/Makefile diff --git a/source/Plugins/Process/Utility/CMakeLists.txt b/source/Plugins/Process/Utility/CMakeLists.txt new file mode 100644 index 000000000000..4a847b4f966a --- /dev/null +++ b/source/Plugins/Process/Utility/CMakeLists.txt @@ -0,0 +1,47 @@ +include_directories(../../../Utility/) + +add_lldb_library(lldbPluginProcessUtility +  DynamicRegisterInfo.cpp +  FreeBSDSignals.cpp +  GDBRemoteSignals.cpp +  HistoryThread.cpp +  HistoryUnwind.cpp +  InferiorCallPOSIX.cpp +  LinuxSignals.cpp +  MipsLinuxSignals.cpp +  NetBSDSignals.cpp +  RegisterContextDarwin_arm.cpp +  RegisterContextDarwin_arm64.cpp +  RegisterContextDarwin_i386.cpp +  RegisterContextDarwin_x86_64.cpp +  RegisterContextDummy.cpp +  RegisterContextFreeBSD_arm.cpp +  RegisterContextFreeBSD_arm64.cpp +  RegisterContextFreeBSD_i386.cpp +  RegisterContextFreeBSD_mips64.cpp +  RegisterContextFreeBSD_powerpc.cpp +  RegisterContextFreeBSD_x86_64.cpp +  RegisterContextHistory.cpp +  RegisterContextLinux_arm.cpp +  RegisterContextLinux_arm64.cpp +  RegisterContextLinux_i386.cpp +  RegisterContextLinux_x86_64.cpp +  RegisterContextLinux_mips64.cpp +  RegisterContextLinux_mips.cpp +  RegisterContextLLDB.cpp +  RegisterContextMacOSXFrameBackchain.cpp +  RegisterContextMach_arm.cpp +  RegisterContextMach_i386.cpp +  RegisterContextMach_x86_64.cpp +  RegisterContextMemory.cpp +  RegisterContextPOSIX_arm.cpp +  RegisterContextPOSIX_arm64.cpp +  RegisterContextPOSIX_mips64.cpp +  RegisterContextPOSIX_powerpc.cpp +  RegisterContextPOSIX_x86.cpp +  RegisterContextThreadMemory.cpp +  StopInfoMachException.cpp +  ThreadMemory.cpp +  UnwindLLDB.cpp +  UnwindMacOSXFrameBackchain.cpp +  ) diff --git a/source/Plugins/Process/Utility/Makefile b/source/Plugins/Process/Utility/Makefile new file mode 100644 index 000000000000..eb0caf3f92fe --- /dev/null +++ b/source/Plugins/Process/Utility/Makefile @@ -0,0 +1,14 @@ +##===- source/Plugins/Utility/Makefile ---------------------*- Makefile -*-===## +# +#                     The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LLDB_LEVEL := ../../../.. +LIBRARYNAME := lldbPluginProcessUtility +BUILD_ARCHIVE = 1 + +include $(LLDB_LEVEL)/Makefile diff --git a/source/Plugins/Process/Windows/Common/CMakeLists.txt b/source/Plugins/Process/Windows/Common/CMakeLists.txt new file mode 100644 index 000000000000..5a53c3c3300c --- /dev/null +++ b/source/Plugins/Process/Windows/Common/CMakeLists.txt @@ -0,0 +1,23 @@ +include_directories(.) +include_directories(../../Utility) + +set(PROC_WINDOWS_COMMON_SOURCES +  RegisterContextWindows.cpp +  ProcessWindows.cpp +  ProcessWindowsLog.cpp +  TargetThreadWindows.cpp +  ) + +if (CMAKE_SIZEOF_VOID_P EQUAL 4) +  set(PROC_WINDOWS_COMMON_SOURCES ${PROC_WINDOWS_COMMON_SOURCES} +    x86/RegisterContextWindows_x86.cpp +    ) +elseif (CMAKE_SIZEOF_VOID_P EQUAL 8) +  set(PROC_WINDOWS_COMMON_SOURCES ${PROC_WINDOWS_COMMON_SOURCES} +    x64/RegisterContextWindows_x64.cpp +    ) +endif() + +add_lldb_library(lldbPluginProcessWindowsCommon +  ${PROC_WINDOWS_COMMON_SOURCES} +  ) diff --git a/source/Plugins/Process/Windows/Common/ExceptionRecord.h b/source/Plugins/Process/Windows/Common/ExceptionRecord.h new file mode 100644 index 000000000000..79dae3fca4e8 --- /dev/null +++ b/source/Plugins/Process/Windows/Common/ExceptionRecord.h @@ -0,0 +1,99 @@ +//===-- ExceptionRecord.h ---------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_Plugins_Process_Windows_ExceptionRecord_H_ +#define liblldb_Plugins_Process_Windows_ExceptionRecord_H_ + +#include "lldb/lldb-forward.h" +#include "lldb/Host/windows/windows.h" +#include <DbgHelp.h> + +#include <memory> +#include <vector> + +namespace lldb_private +{ + +//---------------------------------------------------------------------- +// ExceptionRecord +// +// ExceptionRecord defines an interface which allows implementors to receive +// notification of events that happen in a debugged process. +//---------------------------------------------------------------------- +class ExceptionRecord +{ +  public: +    ExceptionRecord(const EXCEPTION_RECORD &record, lldb::tid_t thread_id) +    { +        m_code = record.ExceptionCode; +        m_continuable = (record.ExceptionFlags == 0); +        if (record.ExceptionRecord) +            m_next_exception.reset(new ExceptionRecord(*record.ExceptionRecord, thread_id)); +        m_exception_addr = reinterpret_cast<lldb::addr_t>(record.ExceptionAddress); +        m_thread_id = thread_id; +        m_arguments.assign(record.ExceptionInformation, record.ExceptionInformation + record.NumberParameters); +    } + +    // MINIDUMP_EXCEPTIONs are almost identical to EXCEPTION_RECORDs. +    ExceptionRecord(const MINIDUMP_EXCEPTION &record, lldb::tid_t thread_id) : +        m_code(record.ExceptionCode), +        m_continuable(record.ExceptionFlags == 0), +        m_next_exception(nullptr), +        m_exception_addr(static_cast<lldb::addr_t>(record.ExceptionAddress)), +        m_thread_id(thread_id), +        m_arguments(record.ExceptionInformation, record.ExceptionInformation + record.NumberParameters) +    { +        // Set up link to nested exception. +        if (record.ExceptionRecord) +        { +            m_next_exception.reset(new ExceptionRecord(*reinterpret_cast<const MINIDUMP_EXCEPTION *>(record.ExceptionRecord), +                                                       thread_id)); +        } +    } + +    virtual ~ExceptionRecord() {} + +    DWORD +    GetExceptionCode() const +    { +        return m_code; +    } +    bool +    IsContinuable() const +    { +        return m_continuable; +    } +    const ExceptionRecord * +    GetNextException() const +    { +        return m_next_exception.get(); +    } +    lldb::addr_t +    GetExceptionAddress() const +    { +        return m_exception_addr; +    } + +    lldb::tid_t +    GetThreadID() const +    { +        return m_thread_id; +    } + +  private: +    DWORD m_code; +    bool m_continuable; +    std::shared_ptr<ExceptionRecord> m_next_exception; +    lldb::addr_t m_exception_addr; +    lldb::tid_t m_thread_id; +    std::vector<ULONG_PTR> m_arguments; +}; +} + +#endif diff --git a/source/Plugins/Process/Windows/Common/ProcessWindows.cpp b/source/Plugins/Process/Windows/Common/ProcessWindows.cpp new file mode 100644 index 000000000000..0e6900d8fb7f --- /dev/null +++ b/source/Plugins/Process/Windows/Common/ProcessWindows.cpp @@ -0,0 +1,100 @@ +//===-- ProcessWindows.cpp --------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "ProcessWindows.h" + +// Other libraries and framework includes +#include "lldb/Core/Module.h" +#include "lldb/Core/ModuleSpec.h" +#include "lldb/Core/PluginManager.h" +#include "lldb/Core/Section.h" +#include "lldb/Core/State.h" +#include "lldb/Host/windows/windows.h" +#include "lldb/Target/DynamicLoader.h" +#include "lldb/Target/MemoryRegionInfo.h" +#include "lldb/Target/Target.h" + +using namespace lldb; +using namespace lldb_private; + +namespace lldb_private +{ + +//------------------------------------------------------------------------------ +// Constructors and destructors. + +ProcessWindows::ProcessWindows(lldb::TargetSP target_sp, Listener &listener) +    : lldb_private::Process(target_sp, listener) +{ +} + +ProcessWindows::~ProcessWindows() +{ +} + +size_t +ProcessWindows::GetSTDOUT(char *buf, size_t buf_size, Error &error) +{ +    error.SetErrorString("GetSTDOUT unsupported on Windows"); +    return 0; +} + +size_t +ProcessWindows::GetSTDERR(char *buf, size_t buf_size, Error &error) +{ +    error.SetErrorString("GetSTDERR unsupported on Windows"); +    return 0; +} + +size_t +ProcessWindows::PutSTDIN(const char *buf, size_t buf_size, Error &error) +{ +    error.SetErrorString("PutSTDIN unsupported on Windows"); +    return 0; +} + +//------------------------------------------------------------------------------ +// ProcessInterface protocol. + + +lldb::addr_t +ProcessWindows::GetImageInfoAddress() +{ +    Target &target = GetTarget(); +    ObjectFile *obj_file = target.GetExecutableModule()->GetObjectFile(); +    Address addr = obj_file->GetImageInfoAddress(&target); +    if (addr.IsValid()) +        return addr.GetLoadAddress(&target); +    else +        return LLDB_INVALID_ADDRESS; +} + +// The Windows page protection bits are NOT independent masks that can be bitwise-ORed +// together.  For example, PAGE_EXECUTE_READ is not (PAGE_EXECUTE | PAGE_READ). +// To test for an access type, it's necessary to test for any of the bits that provide +// that access type. +bool +ProcessWindows::IsPageReadable(uint32_t protect) +{ +    return (protect & PAGE_NOACCESS) == 0; +} + +bool +ProcessWindows::IsPageWritable(uint32_t protect) +{ +    return (protect & (PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY | PAGE_READWRITE | PAGE_WRITECOPY)) != 0; +} + +bool +ProcessWindows::IsPageExecutable(uint32_t protect) +{ +    return (protect & (PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY)) != 0; +} + +} diff --git a/source/Plugins/Process/Windows/Common/ProcessWindows.h b/source/Plugins/Process/Windows/Common/ProcessWindows.h new file mode 100644 index 000000000000..2a437c0ca909 --- /dev/null +++ b/source/Plugins/Process/Windows/Common/ProcessWindows.h @@ -0,0 +1,52 @@ +//===-- ProcessWindows.h ----------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_Plugins_Process_Windows_Common_ProcessWindows_H_ +#define liblldb_Plugins_Process_Windows_Common_ProcessWindows_H_ + +// Other libraries and framework includes +#include "lldb/lldb-forward.h" +#include "lldb/Core/Error.h" +#include "lldb/Target/Process.h" + +namespace lldb_private +{ + +class ProcessWindows : public lldb_private::Process +{ +public: +    //------------------------------------------------------------------ +    // Constructors and destructors +    //------------------------------------------------------------------ +    ProcessWindows(lldb::TargetSP target_sp, +                   lldb_private::Listener &listener); + +    ~ProcessWindows(); + +    size_t GetSTDOUT(char *buf, size_t buf_size, lldb_private::Error &error) override; +    size_t GetSTDERR(char *buf, size_t buf_size, lldb_private::Error &error) override; +    size_t PutSTDIN(const char *buf, size_t buf_size, lldb_private::Error &error) override; + +    lldb::addr_t GetImageInfoAddress() override; + +protected: +    // These decode the page protection bits. +    static bool +    IsPageReadable(uint32_t protect); + +    static bool +    IsPageWritable(uint32_t protect); + +    static bool +    IsPageExecutable(uint32_t protect); +}; + +} + +#endif  // liblldb_Plugins_Process_Windows_Common_ProcessWindows_H_ diff --git a/source/Plugins/Process/Windows/Common/ProcessWindowsLog.cpp b/source/Plugins/Process/Windows/Common/ProcessWindowsLog.cpp new file mode 100644 index 000000000000..47722c5146b2 --- /dev/null +++ b/source/Plugins/Process/Windows/Common/ProcessWindowsLog.cpp @@ -0,0 +1,194 @@ +//===-- ProcessWindowsLog.cpp -----------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "ProcessWindowsLog.h" + +#include <mutex> + +#include "lldb/Core/StreamFile.h" +#include "lldb/Interpreter/Args.h" +#include "llvm/Support/ManagedStatic.h" + +using namespace lldb; +using namespace lldb_private; + + +// We want to avoid global constructors where code needs to be run so here we +// control access to our static g_log_sp by hiding it in a singleton function +// that will construct the static g_log_sp the first time this function is +// called. +static bool g_log_enabled = false; +static Log * g_log = nullptr; + +static llvm::ManagedStatic<std::once_flag> g_once_flag; + +void +ProcessWindowsLog::Initialize() +{ +    static ConstString g_name("windows"); + +    std::call_once(*g_once_flag, [](){ +        Log::Callbacks log_callbacks = { +            DisableLog, +            EnableLog, +            ListLogCategories +        }; + +        Log::RegisterLogChannel(g_name, log_callbacks); +        RegisterPluginName(g_name); +    }); +} + +void +ProcessWindowsLog::Terminate() +{ +} + +Log * +ProcessWindowsLog::GetLog() +{ +    return (g_log_enabled) ? g_log : nullptr; +} + +bool +ProcessWindowsLog::TestLogFlags(uint32_t mask, LogMaskReq req) +{ +    Log *log = GetLog(); +    if (!log) +        return false; + +    uint32_t log_mask = log->GetMask().Get(); +    if (req == LogMaskReq::All) +        return ((log_mask & mask) == mask); +    else +        return (log_mask & mask); +} + +static uint32_t +GetFlagBits(const char *arg) +{ +    if      (::strcasecmp(arg, "all") == 0 ) return WINDOWS_LOG_ALL; +    else if (::strcasecmp(arg, "break") == 0 ) return WINDOWS_LOG_BREAKPOINTS; +    else if (::strcasecmp(arg, "event") == 0 ) return WINDOWS_LOG_EVENT; +    else if (::strcasecmp(arg, "exception") == 0 ) return WINDOWS_LOG_EXCEPTION; +    else if (::strcasecmp(arg, "memory") == 0 ) return WINDOWS_LOG_MEMORY; +    else if (::strcasecmp(arg, "process") == 0 ) return WINDOWS_LOG_PROCESS; +    else if (::strcasecmp(arg, "registers") == 0 ) return WINDOWS_LOG_REGISTERS; +    else if (::strcasecmp(arg, "step") == 0 ) return WINDOWS_LOG_STEP; +    else if (::strcasecmp(arg, "thread") == 0 ) return WINDOWS_LOG_THREAD; +    else if (::strcasecmp(arg, "verbose") == 0 ) return WINDOWS_LOG_VERBOSE; +    return 0; +} + +void +ProcessWindowsLog::DisableLog(const char **args, Stream *feedback_strm) +{ +    Log *log (GetLog()); +    if (log) +    { +        uint32_t flag_bits = 0; + +        if (args[0] != nullptr) +        { +            flag_bits = log->GetMask().Get(); +            for (; args[0]; args++) +            { +                const char *arg = args[0]; +                uint32_t bits = GetFlagBits(arg); + +                if (bits) +                { +                    flag_bits &= ~bits; +                } +                else +                { +                    feedback_strm->Printf("error: unrecognized log category '%s'\n", arg); +                    ListLogCategories(feedback_strm); +                } +            } +        } + +        log->GetMask().Reset(flag_bits); +        if (flag_bits == 0) +        { +            g_log_enabled = false; +            log->SetStream(lldb::StreamSP()); +        } +    } + +    return; +} + +Log * +ProcessWindowsLog::EnableLog(StreamSP &log_stream_sp, uint32_t log_options, const char **args, Stream *feedback_strm) +{ +    // Try see if there already is a log - that way we can reuse its settings. +    // We could reuse the log in toto, but we don't know that the stream is the same. +    uint32_t flag_bits = 0; +    if (g_log) +        flag_bits = g_log->GetMask().Get(); + +    // Now make a new log with this stream if one was provided +    if (log_stream_sp) +    { +        if (g_log) +            g_log->SetStream(log_stream_sp); +        else +            g_log = new Log(log_stream_sp); +    } + +    if (g_log) +    { +        bool got_unknown_category = false; +        for (; args[0]; args++) +        { +            const char *arg = args[0]; +            uint32_t bits = GetFlagBits(arg); + +            if (bits) +            { +                flag_bits |= bits; +            } +            else +            { +                feedback_strm->Printf("error: unrecognized log category '%s'\n", arg); +                if (got_unknown_category == false) +                { +                    got_unknown_category = true; +                    ListLogCategories (feedback_strm); +                } +            } +        } +        if (flag_bits == 0) +            flag_bits = WINDOWS_LOG_ALL; +        g_log->GetMask().Reset(flag_bits); +        g_log->GetOptions().Reset(log_options); +        g_log_enabled = true; +    } +    return g_log; +} + +void +ProcessWindowsLog::ListLogCategories(Stream *strm) +{ +    strm->Printf("Logging categories for '%s':\n" +                 "  all - turn on all available logging categories\n" +                 "  break - log breakpoints\n" +                 "  event - log low level debugger events\n" +                 "  exception - log exception information\n" +                 "  memory - log memory reads and writes\n" +                 "  process - log process events and activities\n" +                 "  registers - log register read/writes\n" +                 "  thread - log thread events and activities\n" +                 "  step - log step related activities\n" +                 "  verbose - enable verbose logging\n", +                 ProcessWindowsLog::m_pluginname); +} + +const char *ProcessWindowsLog::m_pluginname = ""; diff --git a/source/Plugins/Process/Windows/Common/ProcessWindowsLog.h b/source/Plugins/Process/Windows/Common/ProcessWindowsLog.h new file mode 100644 index 000000000000..d798d131faeb --- /dev/null +++ b/source/Plugins/Process/Windows/Common/ProcessWindowsLog.h @@ -0,0 +1,96 @@ +//===-- ProcessWindowsLog.h -------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_ProcessWindowsLog_h_ +#define liblldb_ProcessWindowsLog_h_ + +#include "lldb/Core/Log.h" + +#define WINDOWS_LOG_VERBOSE     (1u << 0) +#define WINDOWS_LOG_PROCESS     (1u << 1)    // Log process operations +#define WINDOWS_LOG_EXCEPTION   (1u << 1)    // Log exceptions +#define WINDOWS_LOG_THREAD      (1u << 2)    // Log thread operations +#define WINDOWS_LOG_MEMORY      (1u << 3)    // Log memory reads/writes calls +#define WINDOWS_LOG_BREAKPOINTS (1u << 4)    // Log breakpoint operations +#define WINDOWS_LOG_STEP        (1u << 5)    // Log step operations +#define WINDOWS_LOG_REGISTERS   (1u << 6)    // Log register operations +#define WINDOWS_LOG_EVENT       (1u << 7)    // Low level debug events +#define WINDOWS_LOG_ALL         (UINT32_MAX) + +enum class LogMaskReq +{ +    All, +    Any +}; + +class ProcessWindowsLog +{ +    static const char *m_pluginname; + +public: +    // --------------------------------------------------------------------- +    // Public Static Methods +    // --------------------------------------------------------------------- +    static void +    Initialize(); + +    static void +    Terminate(); + +    static void +    RegisterPluginName(const char *pluginName) +    { +        m_pluginname = pluginName; +    } + +    static void +    RegisterPluginName(lldb_private::ConstString pluginName) +    { +        m_pluginname = pluginName.GetCString(); +    } + +    static bool +    TestLogFlags(uint32_t mask, LogMaskReq req); + +    static lldb_private::Log * +    GetLog(); + +    static void +    DisableLog(const char **args, lldb_private::Stream *feedback_strm); + +    static lldb_private::Log * +    EnableLog(lldb::StreamSP &log_stream_sp, uint32_t log_options, +               const char **args, lldb_private::Stream *feedback_strm); + +    static void +    ListLogCategories(lldb_private::Stream *strm); +}; + +#define WINLOGF_IF(Flags, Req, Method, ...)              \ +    {                                                    \ +        if (ProcessWindowsLog::TestLogFlags(Flags, Req)) \ +        {                                                \ +            Log *log = ProcessWindowsLog::GetLog();      \ +            if (log)                                     \ +                log->Method(__VA_ARGS__);                \ +        }                                                \ +    } + +#define WINLOG_IFANY(Flags, ...) WINLOGF_IF(Flags, LogMaskReq::Any, Printf, __VA_ARGS__) +#define WINLOG_IFALL(Flags, ...) WINLOGF_IF(Flags, LogMaskReq::All, Printf, __VA_ARGS__) +#define WINLOGV_IFANY(Flags, ...) WINLOGF_IF(Flags, LogMaskReq::Any, Verbose, __VA_ARGS__) +#define WINLOGV_IFALL(Flags, ...) WINLOGF_IF(Flags, LogMaskReq::All, Verbose, __VA_ARGS__) +#define WINLOGD_IFANY(Flags, ...) WINLOGF_IF(Flags, LogMaskReq::Any, Debug, __VA_ARGS__) +#define WINLOGD_IFALL(Flags, ...) WINLOGF_IF(Flags, LogMaskReq::All, Debug, __VA_ARGS__) +#define WINERR_IFANY(Flags, ...) WINLOGF_IF(Flags, LogMaskReq::Any, Error, __VA_ARGS__) +#define WINERR_IFALL(Flags, ...) WINLOGF_IF(Flags, LogMaskReq::All, Error, __VA_ARGS__) +#define WINWARN_IFANY(Flags, ...) WINLOGF_IF(Flags, LogMaskReq::Any, Warning, __VA_ARGS__) +#define WINWARN_IFALL(Flags, ...) WINLOGF_IF(Flags, LogMaskReq::All, Warning, __VA_ARGS__) + +#endif  // liblldb_ProcessWindowsLog_h_ diff --git a/source/Plugins/Process/Windows/Common/RegisterContextWindows.cpp b/source/Plugins/Process/Windows/Common/RegisterContextWindows.cpp new file mode 100644 index 000000000000..d61675f09b1b --- /dev/null +++ b/source/Plugins/Process/Windows/Common/RegisterContextWindows.cpp @@ -0,0 +1,155 @@ +//===-- RegisterContextWindows.cpp ------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/lldb-private-types.h" +#include "lldb/Core/DataBufferHeap.h" +#include "lldb/Core/Error.h" +#include "lldb/Host/windows/HostThreadWindows.h" +#include "lldb/Host/windows/windows.h" + +#include "ProcessWindowsLog.h" +#include "RegisterContextWindows.h" +#include "TargetThreadWindows.h" + +#include "llvm/ADT/STLExtras.h" + +using namespace lldb; +using namespace lldb_private; + +const DWORD kWinContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER; + +//------------------------------------------------------------------ +// Constructors and Destructors +//------------------------------------------------------------------ +RegisterContextWindows::RegisterContextWindows(Thread &thread, uint32_t concrete_frame_idx) +    : RegisterContext(thread, concrete_frame_idx) +    , m_context() +    , m_context_stale(true) +{ +} + +RegisterContextWindows::~RegisterContextWindows() +{ +} + +void +RegisterContextWindows::InvalidateAllRegisters() +{ +    m_context_stale = true; +} + +bool +RegisterContextWindows::ReadAllRegisterValues(lldb::DataBufferSP &data_sp) +{ +    if (!CacheAllRegisterValues()) +        return false; +    if (data_sp->GetByteSize() < sizeof(m_context)) +    { +        data_sp.reset(new DataBufferHeap(sizeof(CONTEXT), 0)); +    } +    memcpy(data_sp->GetBytes(), &m_context, sizeof(m_context)); +    return true; +} + +bool +RegisterContextWindows::WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) +{ +    assert(data_sp->GetByteSize() >= sizeof(m_context)); +    memcpy(&m_context, data_sp->GetBytes(), sizeof(m_context)); + +    TargetThreadWindows &wthread = static_cast<TargetThreadWindows &>(m_thread); +    if (!::SetThreadContext(wthread.GetHostThread().GetNativeThread().GetSystemHandle(), &m_context)) +        return false; + +    return true; +} + +uint32_t +RegisterContextWindows::ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind, uint32_t num) +{ +    const uint32_t num_regs = GetRegisterCount(); + +    assert(kind < kNumRegisterKinds); +    for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx) +    { +        const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg_idx); + +        if (reg_info->kinds[kind] == num) +            return reg_idx; +    } + +    return LLDB_INVALID_REGNUM; +} + +//------------------------------------------------------------------ +// Subclasses can these functions if desired +//------------------------------------------------------------------ +uint32_t +RegisterContextWindows::NumSupportedHardwareBreakpoints() +{ +    // Support for hardware breakpoints not yet implemented. +    return 0; +} + +uint32_t +RegisterContextWindows::SetHardwareBreakpoint(lldb::addr_t addr, size_t size) +{ +    return 0; +} + +bool +RegisterContextWindows::ClearHardwareBreakpoint(uint32_t hw_idx) +{ +    return false; +} + +uint32_t +RegisterContextWindows::NumSupportedHardwareWatchpoints() +{ +    // Support for hardware watchpoints not yet implemented. +    return 0; +} + +uint32_t +RegisterContextWindows::SetHardwareWatchpoint(lldb::addr_t addr, size_t size, bool read, bool write) +{ +    return 0; +} + +bool +RegisterContextWindows::ClearHardwareWatchpoint(uint32_t hw_index) +{ +    return false; +} + +bool +RegisterContextWindows::HardwareSingleStep(bool enable) +{ +    return false; +} + +bool +RegisterContextWindows::CacheAllRegisterValues() +{ +    if (!m_context_stale) +        return true; + +    TargetThreadWindows &wthread = static_cast<TargetThreadWindows &>(m_thread); +    memset(&m_context, 0, sizeof(m_context)); +    m_context.ContextFlags = kWinContextFlags; +    if (!::GetThreadContext(wthread.GetHostThread().GetNativeThread().GetSystemHandle(), &m_context)) +    { +        WINERR_IFALL(WINDOWS_LOG_REGISTERS, "GetThreadContext failed with error %u while caching register values.", +                     ::GetLastError()); +        return false; +    } +    WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "GetThreadContext successfully updated the register values.", ::GetLastError()); +    m_context_stale = false; +    return true; +} diff --git a/source/Plugins/Process/Windows/Common/RegisterContextWindows.h b/source/Plugins/Process/Windows/Common/RegisterContextWindows.h new file mode 100644 index 000000000000..66b7a9004ead --- /dev/null +++ b/source/Plugins/Process/Windows/Common/RegisterContextWindows.h @@ -0,0 +1,67 @@ +//===-- RegisterContextWindows.h --------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_RegisterContextWindows_H_ +#define liblldb_RegisterContextWindows_H_ + +#include "lldb/lldb-forward.h" +#include "lldb/Target/RegisterContext.h" + +namespace lldb_private +{ + +class Thread; + +class RegisterContextWindows : public lldb_private::RegisterContext +{ +  public: +    //------------------------------------------------------------------ +    // Constructors and Destructors +    //------------------------------------------------------------------ +    RegisterContextWindows(Thread &thread, uint32_t concrete_frame_idx); + +    virtual ~RegisterContextWindows(); + +    //------------------------------------------------------------------ +    // Subclasses must override these functions +    //------------------------------------------------------------------ +    void InvalidateAllRegisters() override; + +    bool ReadAllRegisterValues(lldb::DataBufferSP &data_sp) override; + +    bool WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) override; + +    uint32_t ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind, uint32_t num) override; + +    //------------------------------------------------------------------ +    // Subclasses can override these functions if desired +    //------------------------------------------------------------------ +    uint32_t NumSupportedHardwareBreakpoints() override; + +    uint32_t SetHardwareBreakpoint(lldb::addr_t addr, size_t size) override; + +    bool ClearHardwareBreakpoint(uint32_t hw_idx) override; + +    uint32_t NumSupportedHardwareWatchpoints() override; + +    uint32_t SetHardwareWatchpoint(lldb::addr_t addr, size_t size, bool read, bool write) override; + +    bool ClearHardwareWatchpoint(uint32_t hw_index) override; + +    bool HardwareSingleStep(bool enable) override; + +  protected: +    virtual bool CacheAllRegisterValues(); + +    CONTEXT m_context; +    bool m_context_stale; +}; +} + +#endif // #ifndef liblldb_RegisterContextWindows_H_ diff --git a/source/Plugins/Process/Windows/Common/TargetThreadWindows.cpp b/source/Plugins/Process/Windows/Common/TargetThreadWindows.cpp new file mode 100644 index 000000000000..dcb6f0c72435 --- /dev/null +++ b/source/Plugins/Process/Windows/Common/TargetThreadWindows.cpp @@ -0,0 +1,98 @@ +//===-- TargetThreadWindows.cpp----------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/Core/Log.h" +#include "lldb/Core/Logging.h" +#include "lldb/Core/State.h" +#include "lldb/Host/HostInfo.h" +#include "lldb/Host/HostNativeThreadBase.h" +#include "lldb/Host/windows/HostThreadWindows.h" +#include "lldb/Host/windows/windows.h" +#include "lldb/Target/RegisterContext.h" + +#include "TargetThreadWindows.h" +#include "ProcessWindows.h" +#include "ProcessWindowsLog.h" +#include "UnwindLLDB.h" + +using namespace lldb; +using namespace lldb_private; + +TargetThreadWindows::TargetThreadWindows(ProcessWindows &process, const HostThread &thread) +    : Thread(process, thread.GetNativeThread().GetThreadId()) +    , m_host_thread(thread) +{ +} + +TargetThreadWindows::~TargetThreadWindows() +{ +    DestroyThread(); +} + +void +TargetThreadWindows::RefreshStateAfterStop() +{ +    ::SuspendThread(m_host_thread.GetNativeThread().GetSystemHandle()); +    SetState(eStateStopped); +    GetRegisterContext()->InvalidateIfNeeded(false); +} + +void +TargetThreadWindows::WillResume(lldb::StateType resume_state) +{ +} + +void +TargetThreadWindows::DidStop() +{ +} + +bool +TargetThreadWindows::CalculateStopInfo() +{ +    SetStopInfo(m_stop_info_sp); +    return true; +} + +Unwind * +TargetThreadWindows::GetUnwinder() +{ +    // FIXME: Implement an unwinder based on the Windows unwinder exposed through DIA SDK. +    if (m_unwinder_ap.get() == NULL) +        m_unwinder_ap.reset(new UnwindLLDB(*this)); +    return m_unwinder_ap.get(); +} + +bool +TargetThreadWindows::DoResume() +{ +    StateType resume_state = GetTemporaryResumeState(); +    StateType current_state = GetState(); +    if (resume_state == current_state) +        return true; + +    if (resume_state == eStateStepping) +    { +        uint32_t flags_index = GetRegisterContext()->ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); +        uint64_t flags_value = GetRegisterContext()->ReadRegisterAsUnsigned(flags_index, 0); +        flags_value |= 0x100; // Set the trap flag on the CPU +        GetRegisterContext()->WriteRegisterFromUnsigned(flags_index, flags_value); +    } + +    if (resume_state == eStateStepping || resume_state == eStateRunning) +    { +        DWORD previous_suspend_count = 0; +        HANDLE thread_handle = m_host_thread.GetNativeThread().GetSystemHandle(); +        do +        { +            previous_suspend_count = ::ResumeThread(thread_handle); +        } while (previous_suspend_count > 0); +    } +    return true; +} diff --git a/source/Plugins/Process/Windows/Common/TargetThreadWindows.h b/source/Plugins/Process/Windows/Common/TargetThreadWindows.h new file mode 100644 index 000000000000..701b56b6d26a --- /dev/null +++ b/source/Plugins/Process/Windows/Common/TargetThreadWindows.h @@ -0,0 +1,50 @@ +//===-- TargetThreadWindows.h -----------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_Plugins_Process_Windows_TargetThreadWindows_H_ +#define liblldb_Plugins_Process_Windows_TargetThreadWindows_H_ + +//#include "ForwardDecl.h" +#include "lldb/lldb-forward.h" +#include "lldb/Host/HostThread.h" +#include "lldb/Target/Thread.h" + +namespace lldb_private +{ +class ProcessWindows; +class HostThread; +class StackFrame; + +class TargetThreadWindows : public lldb_private::Thread +{ +  public: +    TargetThreadWindows(ProcessWindows &process, const HostThread &thread); +    virtual ~TargetThreadWindows(); + +    // lldb_private::Thread overrides +    void RefreshStateAfterStop() override; +    void WillResume(lldb::StateType resume_state) override; +    void DidStop() override; +    bool CalculateStopInfo() override; +    Unwind *GetUnwinder() override; + +    bool DoResume(); + +    HostThread +    GetHostThread() const +    { +        return m_host_thread; +    } + +  private: +    HostThread m_host_thread; +}; +} + +#endif diff --git a/source/Plugins/Process/Windows/Common/x64/RegisterContextWindows_x64.cpp b/source/Plugins/Process/Windows/Common/x64/RegisterContextWindows_x64.cpp new file mode 100644 index 000000000000..103cff4a2a56 --- /dev/null +++ b/source/Plugins/Process/Windows/Common/x64/RegisterContextWindows_x64.cpp @@ -0,0 +1,323 @@ +//===-- RegisterContextWindows_x64.cpp --------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/lldb-private-types.h" +#include "lldb/Core/Error.h" +#include "lldb/Core/RegisterValue.h" +#include "lldb/Host/windows/HostThreadWindows.h" +#include "lldb/Host/windows/windows.h" + +#include "lldb-x86-register-enums.h" +#include "RegisterContext_x86.h" +#include "RegisterContextWindows_x64.h" +#include "TargetThreadWindows.h" + +#include "llvm/ADT/STLExtras.h" + +using namespace lldb; +using namespace lldb_private; + +#define DEFINE_GPR(reg, alt) #reg, alt, 8, 0, eEncodingUint, eFormatHexUppercase +#define DEFINE_GPR_BIN(reg, alt) #reg, alt, 8, 0, eEncodingUint, eFormatBinary + +namespace +{ + +// This enum defines the layout of the global RegisterInfo array.  This is necessary because +// lldb register sets are defined in terms of indices into the register array.  As such, the +// order of RegisterInfos defined in global registers array must match the order defined here. +// When defining the register set layouts, these values can appear in an arbitrary order, and that +// determines the order that register values are displayed in a dump. +enum RegisterIndex +{ +    eRegisterIndexRax, +    eRegisterIndexRbx, +    eRegisterIndexRcx, +    eRegisterIndexRdx, +    eRegisterIndexRdi, +    eRegisterIndexRsi, +    eRegisterIndexR8, +    eRegisterIndexR9, +    eRegisterIndexR10, +    eRegisterIndexR11, +    eRegisterIndexR12, +    eRegisterIndexR13, +    eRegisterIndexR14, +    eRegisterIndexR15, +    eRegisterIndexRbp, +    eRegisterIndexRsp, +    eRegisterIndexRip, +    eRegisterIndexRflags +}; + +// Array of all register information supported by Windows x86 +RegisterInfo g_register_infos[] = { +    //  Macro auto defines most stuff     eh_frame                  DWARF                   GENERIC +    //  GDB                  LLDB                  VALUE REGS    INVALIDATE REGS +    //  ================================  ========================= ======================  ========================= +    //  ===================  =================     ==========    =============== +    {DEFINE_GPR(rax, nullptr), +     {dwarf_rax_x86_64, dwarf_rax_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, lldb_rax_x86_64}, +     nullptr, +     nullptr}, +    {DEFINE_GPR(rbx, nullptr), +     {dwarf_rbx_x86_64, dwarf_rbx_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, lldb_rbx_x86_64}, +     nullptr, +     nullptr}, +    {DEFINE_GPR(rcx, nullptr), +     {dwarf_rcx_x86_64, dwarf_rcx_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, lldb_rcx_x86_64}, +     nullptr, +     nullptr}, +    {DEFINE_GPR(rdx, nullptr), +     {dwarf_rdx_x86_64, dwarf_rdx_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, lldb_rdx_x86_64}, +     nullptr, +     nullptr}, +    {DEFINE_GPR(rdi, nullptr), +     {dwarf_rdi_x86_64, dwarf_rdi_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, lldb_rdi_x86_64}, +     nullptr, +     nullptr}, +    {DEFINE_GPR(rsi, nullptr), +     {dwarf_rsi_x86_64, dwarf_rsi_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, lldb_rsi_x86_64}, +     nullptr, +     nullptr}, +    {DEFINE_GPR(r8, nullptr), +     {dwarf_r8_x86_64, dwarf_r8_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, lldb_r8_x86_64}, +     nullptr, +     nullptr}, +    {DEFINE_GPR(r9, nullptr), +     {dwarf_r9_x86_64, dwarf_r9_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, lldb_r9_x86_64}, +     nullptr, +     nullptr}, +    {DEFINE_GPR(r10, nullptr), +     {dwarf_r10_x86_64, dwarf_r10_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, lldb_r10_x86_64}, +     nullptr, +     nullptr}, +    {DEFINE_GPR(r11, nullptr), +     {dwarf_r11_x86_64, dwarf_r11_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, lldb_r11_x86_64}, +     nullptr, +     nullptr}, +    {DEFINE_GPR(r12, nullptr), +     {dwarf_r12_x86_64, dwarf_r12_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, lldb_r12_x86_64}, +     nullptr, +     nullptr}, +    {DEFINE_GPR(r13, nullptr), +     {dwarf_r13_x86_64, dwarf_r13_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, lldb_r13_x86_64}, +     nullptr, +     nullptr}, +    {DEFINE_GPR(r14, nullptr), +     {dwarf_r14_x86_64, dwarf_r14_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, lldb_r14_x86_64}, +     nullptr, +     nullptr}, +    {DEFINE_GPR(r15, nullptr), +     {dwarf_r15_x86_64, dwarf_r15_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, lldb_r15_x86_64}, +     nullptr, +     nullptr}, +    {DEFINE_GPR(rbp, "fp"), +     {dwarf_rbp_x86_64, dwarf_rbp_x86_64, LLDB_REGNUM_GENERIC_FP, LLDB_INVALID_REGNUM, lldb_rbp_x86_64}, +     nullptr, +     nullptr}, +    {DEFINE_GPR(rsp, "sp"), +     {dwarf_rsp_x86_64, dwarf_rsp_x86_64, LLDB_REGNUM_GENERIC_SP, LLDB_INVALID_REGNUM, lldb_rsp_x86_64}, +     nullptr, +     nullptr}, +    {DEFINE_GPR(rip, "pc"), +     {dwarf_rip_x86_64, dwarf_rip_x86_64, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_REGNUM, lldb_rip_x86_64}, +     nullptr, +     nullptr}, +    {DEFINE_GPR_BIN(eflags, "flags"), +     {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_REGNUM_GENERIC_FLAGS, LLDB_INVALID_REGNUM, lldb_rflags_x86_64}, +     nullptr, +     nullptr}, +}; + +// Array of lldb register numbers used to define the set of all General Purpose Registers +uint32_t g_gpr_reg_indices[] = {eRegisterIndexRax, eRegisterIndexRbx,   eRegisterIndexRcx, eRegisterIndexRdx, +                                eRegisterIndexRdi, eRegisterIndexRsi,   eRegisterIndexR8,  eRegisterIndexR9, +                                eRegisterIndexR10, eRegisterIndexR11,   eRegisterIndexR12, eRegisterIndexR13, +                                eRegisterIndexR14, eRegisterIndexR15,   eRegisterIndexRbp, eRegisterIndexRsp, +                                eRegisterIndexRip, eRegisterIndexRflags}; + +RegisterSet g_register_sets[] = { +    {"General Purpose Registers", "gpr", llvm::array_lengthof(g_gpr_reg_indices), g_gpr_reg_indices}, +}; +} + +//------------------------------------------------------------------ +// Constructors and Destructors +//------------------------------------------------------------------ +RegisterContextWindows_x64::RegisterContextWindows_x64(Thread &thread, uint32_t concrete_frame_idx) +    : RegisterContextWindows(thread, concrete_frame_idx) +{ +} + +RegisterContextWindows_x64::~RegisterContextWindows_x64() +{ +} + +size_t +RegisterContextWindows_x64::GetRegisterCount() +{ +    return llvm::array_lengthof(g_register_infos); +} + +const RegisterInfo * +RegisterContextWindows_x64::GetRegisterInfoAtIndex(size_t reg) +{ +    return &g_register_infos[reg]; +} + +size_t +RegisterContextWindows_x64::GetRegisterSetCount() +{ +    return llvm::array_lengthof(g_register_sets); +} + +const RegisterSet * +RegisterContextWindows_x64::GetRegisterSet(size_t reg_set) +{ +    return &g_register_sets[reg_set]; +} + +bool +RegisterContextWindows_x64::ReadRegister(const RegisterInfo *reg_info, RegisterValue ®_value) +{ +    if (!CacheAllRegisterValues()) +        return false; + +    switch (reg_info->kinds[eRegisterKindLLDB]) +    { +        case lldb_rax_x86_64: +            reg_value.SetUInt64(m_context.Rax); +            break; +        case lldb_rbx_x86_64: +            reg_value.SetUInt64(m_context.Rbx); +            break; +        case lldb_rcx_x86_64: +            reg_value.SetUInt64(m_context.Rcx); +            break; +        case lldb_rdx_x86_64: +            reg_value.SetUInt64(m_context.Rdx); +            break; +        case lldb_rdi_x86_64: +            reg_value.SetUInt64(m_context.Rdi); +            break; +        case lldb_rsi_x86_64: +            reg_value.SetUInt64(m_context.Rsi); +            break; +        case lldb_r8_x86_64: +            reg_value.SetUInt64(m_context.R8); +            break; +        case lldb_r9_x86_64: +            reg_value.SetUInt64(m_context.R9); +            break; +        case lldb_r10_x86_64: +            reg_value.SetUInt64(m_context.R10); +            break; +        case lldb_r11_x86_64: +            reg_value.SetUInt64(m_context.R11); +            break; +        case lldb_r12_x86_64: +            reg_value.SetUInt64(m_context.R12); +            break; +        case lldb_r13_x86_64: +            reg_value.SetUInt64(m_context.R13); +            break; +        case lldb_r14_x86_64: +            reg_value.SetUInt64(m_context.R14); +            break; +        case lldb_r15_x86_64: +            reg_value.SetUInt64(m_context.R15); +            break; +        case lldb_rbp_x86_64: +            reg_value.SetUInt64(m_context.Rbp); +            break; +        case lldb_rsp_x86_64: +            reg_value.SetUInt64(m_context.Rsp); +            break; +        case lldb_rip_x86_64: +            reg_value.SetUInt64(m_context.Rip); +            break; +        case lldb_rflags_x86_64: +            reg_value.SetUInt64(m_context.EFlags); +            break; +    } +    return true; +} + +bool +RegisterContextWindows_x64::WriteRegister(const RegisterInfo *reg_info, const RegisterValue ®_value) +{ +    // Since we cannot only write a single register value to the inferior, we need to make sure +    // our cached copy of the register values are fresh.  Otherwise when writing EAX, for example, +    // we may also overwrite some other register with a stale value. +    if (!CacheAllRegisterValues()) +        return false; + +    switch (reg_info->kinds[eRegisterKindLLDB]) +    { +        case lldb_rax_x86_64: +            m_context.Rax = reg_value.GetAsUInt64(); +            break; +        case lldb_rbx_x86_64: +            m_context.Rbx = reg_value.GetAsUInt64(); +            break; +        case lldb_rcx_x86_64: +            m_context.Rcx = reg_value.GetAsUInt64(); +            break; +        case lldb_rdx_x86_64: +            m_context.Rdx = reg_value.GetAsUInt64(); +            break; +        case lldb_rdi_x86_64: +            m_context.Rdi = reg_value.GetAsUInt64(); +            break; +        case lldb_rsi_x86_64: +            m_context.Rsi = reg_value.GetAsUInt64(); +            break; +        case lldb_r8_x86_64: +            m_context.R8 = reg_value.GetAsUInt64(); +            break; +        case lldb_r9_x86_64: +            m_context.R9 = reg_value.GetAsUInt64(); +            break; +        case lldb_r10_x86_64: +            m_context.R10 = reg_value.GetAsUInt64(); +            break; +        case lldb_r11_x86_64: +            m_context.R11 = reg_value.GetAsUInt64(); +            break; +        case lldb_r12_x86_64: +            m_context.R12 = reg_value.GetAsUInt64(); +            break; +        case lldb_r13_x86_64: +            m_context.R13 = reg_value.GetAsUInt64(); +            break; +        case lldb_r14_x86_64: +            m_context.R14 = reg_value.GetAsUInt64(); +            break; +        case lldb_r15_x86_64: +            m_context.R15 = reg_value.GetAsUInt64(); +            break; +        case lldb_rbp_x86_64: +            m_context.Rbp = reg_value.GetAsUInt64(); +            break; +        case lldb_rsp_x86_64: +            m_context.Rsp = reg_value.GetAsUInt64(); +            break; +        case lldb_rip_x86_64: +            m_context.Rip = reg_value.GetAsUInt64(); +            break; +        case lldb_rflags_x86_64: +            m_context.EFlags = reg_value.GetAsUInt64(); +            break; +    } + +    // Physically update the registers in the target process. +    TargetThreadWindows &wthread = static_cast<TargetThreadWindows &>(m_thread); +    return ::SetThreadContext(wthread.GetHostThread().GetNativeThread().GetSystemHandle(), &m_context); +} diff --git a/source/Plugins/Process/Windows/Common/x64/RegisterContextWindows_x64.h b/source/Plugins/Process/Windows/Common/x64/RegisterContextWindows_x64.h new file mode 100644 index 000000000000..e69179d99c6c --- /dev/null +++ b/source/Plugins/Process/Windows/Common/x64/RegisterContextWindows_x64.h @@ -0,0 +1,48 @@ +//===-- RegisterContextWindows_x64.h ----------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_RegisterContextWindows_x64_H_ +#define liblldb_RegisterContextWindows_x64_H_ + +#include "lldb/lldb-forward.h" +#include "RegisterContextWindows.h" + +namespace lldb_private +{ + +class Thread; + +class RegisterContextWindows_x64 : public RegisterContextWindows +{ +  public: +    //------------------------------------------------------------------ +    // Constructors and Destructors +    //------------------------------------------------------------------ +    RegisterContextWindows_x64(Thread &thread, uint32_t concrete_frame_idx); + +    virtual ~RegisterContextWindows_x64(); + +    //------------------------------------------------------------------ +    // Subclasses must override these functions +    //------------------------------------------------------------------ +    size_t GetRegisterCount() override; + +    const RegisterInfo *GetRegisterInfoAtIndex(size_t reg) override; + +    size_t GetRegisterSetCount() override; + +    const RegisterSet *GetRegisterSet(size_t reg_set) override; + +    bool ReadRegister(const RegisterInfo *reg_info, RegisterValue ®_value) override; + +    bool WriteRegister(const RegisterInfo *reg_info, const RegisterValue ®_value) override; +}; +} + +#endif // #ifndef liblldb_RegisterContextWindows_x64_H_ diff --git a/source/Plugins/Process/Windows/Common/x86/RegisterContextWindows_x86.cpp b/source/Plugins/Process/Windows/Common/x86/RegisterContextWindows_x86.cpp new file mode 100644 index 000000000000..e57e1effec9c --- /dev/null +++ b/source/Plugins/Process/Windows/Common/x86/RegisterContextWindows_x86.cpp @@ -0,0 +1,178 @@ +//===-- RegisterContextWindows_x86.cpp --------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/lldb-private-types.h" +#include "lldb/Core/Error.h" +#include "lldb/Core/RegisterValue.h" +#include "lldb/Host/windows/HostThreadWindows.h" +#include "lldb/Host/windows/windows.h" + +#include "lldb-x86-register-enums.h" +#include "ProcessWindowsLog.h" +#include "RegisterContext_x86.h" +#include "RegisterContextWindows_x86.h" + +#include "llvm/ADT/STLExtras.h" + +using namespace lldb; +using namespace lldb_private; + +#define DEFINE_GPR(reg, alt) #reg, alt, 4, 0, eEncodingUint, eFormatHexUppercase +#define DEFINE_GPR_BIN(reg, alt) #reg, alt, 4, 0, eEncodingUint, eFormatBinary + +namespace +{ + +// This enum defines the layout of the global RegisterInfo array.  This is necessary because +// lldb register sets are defined in terms of indices into the register array.  As such, the +// order of RegisterInfos defined in global registers array must match the order defined here. +// When defining the register set layouts, these values can appear in an arbitrary order, and that +// determines the order that register values are displayed in a dump. +enum RegisterIndex +{ +    eRegisterIndexEax, +    eRegisterIndexEbx, +    eRegisterIndexEcx, +    eRegisterIndexEdx, +    eRegisterIndexEdi, +    eRegisterIndexEsi, +    eRegisterIndexEbp, +    eRegisterIndexEsp, +    eRegisterIndexEip, +    eRegisterIndexEflags +}; + +// Array of all register information supported by Windows x86 +RegisterInfo g_register_infos[] = +{ +//  Macro auto defines most stuff   eh_frame                DWARF                GENERIC                    GDB                   LLDB               VALUE REGS    INVALIDATE REGS +//  ==============================  ======================= ===================  =========================  ===================   =================  ==========    =============== +    { DEFINE_GPR(eax,    nullptr),  { ehframe_eax_i386,     dwarf_eax_i386,      LLDB_INVALID_REGNUM,       LLDB_INVALID_REGNUM,  lldb_eax_i386   },  nullptr,      nullptr}, +    { DEFINE_GPR(ebx,    nullptr),  { ehframe_ebx_i386,     dwarf_ebx_i386,      LLDB_INVALID_REGNUM,       LLDB_INVALID_REGNUM,  lldb_ebx_i386   },  nullptr,      nullptr}, +    { DEFINE_GPR(ecx,    nullptr),  { ehframe_ecx_i386,     dwarf_ecx_i386,      LLDB_INVALID_REGNUM,       LLDB_INVALID_REGNUM,  lldb_ecx_i386   },  nullptr,      nullptr}, +    { DEFINE_GPR(edx,    nullptr),  { ehframe_edx_i386,     dwarf_edx_i386,      LLDB_INVALID_REGNUM,       LLDB_INVALID_REGNUM,  lldb_edx_i386   },  nullptr,      nullptr}, +    { DEFINE_GPR(edi,    nullptr),  { ehframe_edi_i386,     dwarf_edi_i386,      LLDB_INVALID_REGNUM,       LLDB_INVALID_REGNUM,  lldb_edi_i386   },  nullptr,      nullptr}, +    { DEFINE_GPR(esi,    nullptr),  { ehframe_esi_i386,     dwarf_esi_i386,      LLDB_INVALID_REGNUM,       LLDB_INVALID_REGNUM,  lldb_esi_i386   },  nullptr,      nullptr}, +    { DEFINE_GPR(ebp,    "fp"),     { ehframe_ebp_i386,     dwarf_ebp_i386,      LLDB_REGNUM_GENERIC_FP,    LLDB_INVALID_REGNUM,  lldb_ebp_i386   },  nullptr,      nullptr}, +    { DEFINE_GPR(esp,    "sp"),     { ehframe_esp_i386,     dwarf_esp_i386,      LLDB_REGNUM_GENERIC_SP,    LLDB_INVALID_REGNUM,  lldb_esp_i386   },  nullptr,      nullptr}, +    { DEFINE_GPR(eip,    "pc"),     { ehframe_eip_i386,     dwarf_eip_i386,      LLDB_REGNUM_GENERIC_PC,    LLDB_INVALID_REGNUM,  lldb_eip_i386   },  nullptr,      nullptr}, +    { DEFINE_GPR_BIN(eflags, "flags"), { ehframe_eflags_i386, dwarf_eflags_i386,   LLDB_REGNUM_GENERIC_FLAGS, LLDB_INVALID_REGNUM,  lldb_eflags_i386},  nullptr,      nullptr}, +}; + +// Array of lldb register numbers used to define the set of all General Purpose Registers +uint32_t g_gpr_reg_indices[] = +{ +    eRegisterIndexEax, +    eRegisterIndexEbx, +    eRegisterIndexEcx, +    eRegisterIndexEdx, +    eRegisterIndexEdi, +    eRegisterIndexEsi, +    eRegisterIndexEbp, +    eRegisterIndexEsp, +    eRegisterIndexEip, +    eRegisterIndexEflags +}; + +RegisterSet g_register_sets[] = { +    {"General Purpose Registers", "gpr", llvm::array_lengthof(g_gpr_reg_indices), g_gpr_reg_indices}, +}; +} + +//------------------------------------------------------------------ +// Constructors and Destructors +//------------------------------------------------------------------ +RegisterContextWindows_x86::RegisterContextWindows_x86(Thread &thread, uint32_t concrete_frame_idx) +    : RegisterContextWindows(thread, concrete_frame_idx) +{ +} + +RegisterContextWindows_x86::~RegisterContextWindows_x86() +{ +} + +size_t +RegisterContextWindows_x86::GetRegisterCount() +{ +    return llvm::array_lengthof(g_register_infos); +} + +const RegisterInfo * +RegisterContextWindows_x86::GetRegisterInfoAtIndex(size_t reg) +{ +    return &g_register_infos[reg]; +} + +size_t +RegisterContextWindows_x86::GetRegisterSetCount() +{ +    return llvm::array_lengthof(g_register_sets); +} + +const RegisterSet * +RegisterContextWindows_x86::GetRegisterSet(size_t reg_set) +{ +    return &g_register_sets[reg_set]; +} + +bool +RegisterContextWindows_x86::ReadRegister(const RegisterInfo *reg_info, RegisterValue ®_value) +{ +    if (!CacheAllRegisterValues()) +        return false; + +    uint32_t reg = reg_info->kinds[eRegisterKindLLDB]; +    switch (reg) +    { +        case lldb_eax_i386: +            WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from EAX", m_context.Eax); +            reg_value.SetUInt32(m_context.Eax); +            break; +        case lldb_ebx_i386: +            WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from EBX", m_context.Ebx); +            reg_value.SetUInt32(m_context.Ebx); +            break; +        case lldb_ecx_i386: +            WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from ECX", m_context.Ecx); +            reg_value.SetUInt32(m_context.Ecx); +            break; +        case lldb_edx_i386: +            WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from EDX", m_context.Edx); +            reg_value.SetUInt32(m_context.Edx); +            break; +        case lldb_edi_i386: +            WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from EDI", m_context.Edi); +            reg_value.SetUInt32(m_context.Edi); +            break; +        case lldb_esi_i386: +            WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from ESI", m_context.Esi); +            reg_value.SetUInt32(m_context.Esi); +            break; +        case lldb_ebp_i386: +            WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from EBP", m_context.Ebp); +            reg_value.SetUInt32(m_context.Ebp); +            break; +        case lldb_esp_i386: +            WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from ESP", m_context.Esp); +            reg_value.SetUInt32(m_context.Esp); +            break; +        case lldb_eip_i386: +            WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from EIP", m_context.Eip); +            reg_value.SetUInt32(m_context.Eip); +            break; +        case lldb_eflags_i386: +            WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Read value 0x%x from EFLAGS", m_context.EFlags); +            reg_value.SetUInt32(m_context.EFlags); +            break; +        default: +            WINWARN_IFALL(WINDOWS_LOG_REGISTERS, "Requested unknown register %u", reg); +            break; +    } +    return true; +} diff --git a/source/Plugins/Process/Windows/Common/x86/RegisterContextWindows_x86.h b/source/Plugins/Process/Windows/Common/x86/RegisterContextWindows_x86.h new file mode 100644 index 000000000000..7d854ef64a5c --- /dev/null +++ b/source/Plugins/Process/Windows/Common/x86/RegisterContextWindows_x86.h @@ -0,0 +1,48 @@ +//===-- RegisterContextWindows_x86.h ----------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_RegisterContextWindows_x86_H_ +#define liblldb_RegisterContextWindows_x86_H_ + +#include "lldb/lldb-forward.h" +#include "RegisterContextWindows.h" + +namespace lldb_private +{ + +class Thread; + +class RegisterContextWindows_x86 : public RegisterContextWindows +{ +  public: +    //------------------------------------------------------------------ +    // Constructors and Destructors +    //------------------------------------------------------------------ +    RegisterContextWindows_x86(Thread &thread, uint32_t concrete_frame_idx); + +    virtual ~RegisterContextWindows_x86(); + +    //------------------------------------------------------------------ +    // Subclasses must override these functions +    //------------------------------------------------------------------ +    size_t GetRegisterCount() override; + +    const RegisterInfo *GetRegisterInfoAtIndex(size_t reg) override; + +    size_t GetRegisterSetCount() override; + +    const RegisterSet *GetRegisterSet(size_t reg_set) override; + +    bool ReadRegister(const RegisterInfo *reg_info, RegisterValue ®_value) override; + +}; + +} + +#endif // #ifndef liblldb_RegisterContextWindows_x86_H_ diff --git a/source/Plugins/Process/Windows/Live/CMakeLists.txt b/source/Plugins/Process/Windows/Live/CMakeLists.txt new file mode 100644 index 000000000000..bdb680ad0bee --- /dev/null +++ b/source/Plugins/Process/Windows/Live/CMakeLists.txt @@ -0,0 +1,24 @@ +include_directories(.) +include_directories(../../Utility) +include_directories(../Common) + +set(PROC_WINDOWS_SOURCES +  DebuggerThread.cpp +  LocalDebugDelegate.cpp +  ProcessWindowsLive.cpp +  TargetThreadWindowsLive.cpp +  ) + +if (CMAKE_SIZEOF_VOID_P EQUAL 4) +  set(PROC_WINDOWS_SOURCES ${PROC_WINDOWS_SOURCES} +    x86/RegisterContextWindowsLive_x86.cpp +    ) +elseif (CMAKE_SIZEOF_VOID_P EQUAL 8) +  set(PROC_WINDOWS_SOURCES ${PROC_WINDOWS_SOURCES} +    x64/RegisterContextWindowsLive_x64.cpp +    ) +endif() + +add_lldb_library(lldbPluginProcessWindows +  ${PROC_WINDOWS_SOURCES} +  ) diff --git a/source/Plugins/Process/Windows/Live/DebuggerThread.cpp b/source/Plugins/Process/Windows/Live/DebuggerThread.cpp new file mode 100644 index 000000000000..d058a412c896 --- /dev/null +++ b/source/Plugins/Process/Windows/Live/DebuggerThread.cpp @@ -0,0 +1,546 @@ +//===-- DebuggerThread.DebuggerThread --------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "DebuggerThread.h" +#include "ExceptionRecord.h" +#include "IDebugDelegate.h" + +#include "lldb/Core/Error.h" +#include "lldb/Core/Log.h" +#include "lldb/Core/ModuleSpec.h" +#include "lldb/Host/FileSpec.h" +#include "lldb/Host/Predicate.h" +#include "lldb/Host/ThisThread.h" +#include "lldb/Host/ThreadLauncher.h" +#include "lldb/Host/windows/HostProcessWindows.h" +#include "lldb/Host/windows/HostThreadWindows.h" +#include "lldb/Host/windows/ProcessLauncherWindows.h" +#include "lldb/Target/ProcessLaunchInfo.h" +#include "lldb/Target/Process.h" + +#include "Plugins/Process/Windows/Common/ProcessWindowsLog.h" + +#include "llvm/ADT/STLExtras.h" +#include "llvm/Support/raw_ostream.h" + +using namespace lldb; +using namespace lldb_private; + +namespace +{ +struct DebugLaunchContext +{ +    DebugLaunchContext(DebuggerThread *thread, const ProcessLaunchInfo &launch_info) +        : m_thread(thread) +        , m_launch_info(launch_info) +    { +    } +    DebuggerThread *m_thread; +    ProcessLaunchInfo m_launch_info; +}; + +struct DebugAttachContext +{ +    DebugAttachContext(DebuggerThread *thread, lldb::pid_t pid, const ProcessAttachInfo &attach_info) +        : m_thread(thread) +        , m_pid(pid) +        , m_attach_info(attach_info) +    { +    } +    DebuggerThread *m_thread; +    lldb::pid_t m_pid; +    ProcessAttachInfo m_attach_info; +}; +} + +DebuggerThread::DebuggerThread(DebugDelegateSP debug_delegate) +    : m_debug_delegate(debug_delegate) +    , m_image_file(nullptr) +    , m_debugging_ended_event(nullptr) +    , m_is_shutting_down(false) +    , m_pid_to_detach(0) +    , m_detached(false) +{ +    m_debugging_ended_event = ::CreateEvent(nullptr, TRUE, FALSE, nullptr); +} + +DebuggerThread::~DebuggerThread() +{ +    ::CloseHandle(m_debugging_ended_event); +} + +Error +DebuggerThread::DebugLaunch(const ProcessLaunchInfo &launch_info) +{ +    WINLOG_IFALL(WINDOWS_LOG_PROCESS, +        "DebuggerThread::DebugLaunch launching '%s'", launch_info.GetExecutableFile().GetPath().c_str()); + +    Error error; +    DebugLaunchContext *context = new DebugLaunchContext(this, launch_info); +    HostThread slave_thread(ThreadLauncher::LaunchThread("lldb.plugin.process-windows.slave[?]", +                                                         DebuggerThreadLaunchRoutine, context, &error)); + +    if (!error.Success()) +    { +        WINERR_IFALL(WINDOWS_LOG_PROCESS, +            "DebugLaunch couldn't launch debugger thread.  %s", error.AsCString()); +    } + +    return error; +} + +Error +DebuggerThread::DebugAttach(lldb::pid_t pid, const ProcessAttachInfo &attach_info) +{ +    WINLOG_IFALL(WINDOWS_LOG_PROCESS, "DebuggerThread::DebugAttach attaching to '%u'", (DWORD)pid); + +    Error error; +    DebugAttachContext *context = new DebugAttachContext(this, pid, attach_info); +    HostThread slave_thread(ThreadLauncher::LaunchThread("lldb.plugin.process-windows.slave[?]", +                                                         DebuggerThreadAttachRoutine, context, &error)); + +    if (!error.Success()) +    { +        WINERR_IFALL(WINDOWS_LOG_PROCESS, "DebugAttach couldn't attach to process '%u'.  %s", (DWORD)pid, +                     error.AsCString()); +    } + +    return error; +} + +lldb::thread_result_t +DebuggerThread::DebuggerThreadLaunchRoutine(void *data) +{ +    DebugLaunchContext *context = static_cast<DebugLaunchContext *>(data); +    lldb::thread_result_t result = context->m_thread->DebuggerThreadLaunchRoutine(context->m_launch_info); +    delete context; +    return result; +} + +lldb::thread_result_t +DebuggerThread::DebuggerThreadAttachRoutine(void *data) +{ +    DebugAttachContext *context = static_cast<DebugAttachContext *>(data); +    lldb::thread_result_t result = +        context->m_thread->DebuggerThreadAttachRoutine(context->m_pid, context->m_attach_info); +    delete context; +    return result; +} + +lldb::thread_result_t +DebuggerThread::DebuggerThreadLaunchRoutine(const ProcessLaunchInfo &launch_info) +{ +    // Grab a shared_ptr reference to this so that we know it won't get deleted until after the +    // thread routine has exited. +    std::shared_ptr<DebuggerThread> this_ref(shared_from_this()); + +    WINLOG_IFALL(WINDOWS_LOG_PROCESS, "DebuggerThread preparing to launch '%s' on background thread.", +                 launch_info.GetExecutableFile().GetPath().c_str()); + +    Error error; +    ProcessLauncherWindows launcher; +    HostProcess process(launcher.LaunchProcess(launch_info, error)); +    // If we couldn't create the process, notify waiters immediately.  Otherwise enter the debug +    // loop and wait until we get the create process debug notification.  Note that if the process +    // was created successfully, we can throw away the process handle we got from CreateProcess +    // because Windows will give us another (potentially more useful?) handle when it sends us the +    // CREATE_PROCESS_DEBUG_EVENT. +    if (error.Success()) +        DebugLoop(); +    else +        m_debug_delegate->OnDebuggerError(error, 0); + +    return 0; +} + +lldb::thread_result_t +DebuggerThread::DebuggerThreadAttachRoutine(lldb::pid_t pid, const ProcessAttachInfo &attach_info) +{ +    // Grab a shared_ptr reference to this so that we know it won't get deleted until after the +    // thread routine has exited. +    std::shared_ptr<DebuggerThread> this_ref(shared_from_this()); + +    WINLOG_IFALL(WINDOWS_LOG_PROCESS, "DebuggerThread preparing to attach to process '%u' on background thread.", +                 (DWORD)pid); + +    if (!DebugActiveProcess((DWORD)pid)) +    { +        Error error(::GetLastError(), eErrorTypeWin32); +        m_debug_delegate->OnDebuggerError(error, 0); +        return 0; +    } + +    // The attach was successful, enter the debug loop.  From here on out, this is no different than +    // a create process operation, so all the same comments in DebugLaunch should apply from this +    // point out. +    DebugLoop(); + +    return 0; +} + +Error +DebuggerThread::StopDebugging(bool terminate) +{ +    Error error; + +    lldb::pid_t pid = m_process.GetProcessId(); + +    WINLOG_IFALL(WINDOWS_LOG_PROCESS, +        "StopDebugging('%s') called (inferior=%I64u).", +        (terminate ? "true" : "false"), pid); + +    // Set m_is_shutting_down to true if it was false.  Return if it was already true. +    bool expected = false; +    if (!m_is_shutting_down.compare_exchange_strong(expected, true)) +        return error; + +    // Make a copy of the process, since the termination sequence will reset +    // DebuggerThread's internal copy and it needs to remain open for the Wait operation. +    HostProcess process_copy = m_process; +    lldb::process_t handle = m_process.GetNativeProcess().GetSystemHandle(); + +    if (terminate) +    { +        // Initiate the termination before continuing the exception, so that the next debug +        // event we get is the exit process event, and not some other event. +        BOOL terminate_suceeded = TerminateProcess(handle, 0); +        WINLOG_IFALL(WINDOWS_LOG_PROCESS, +            "StopDebugging called TerminateProcess(0x%p, 0) (inferior=%I64u), success='%s'", +            handle, pid, (terminate_suceeded ? "true" : "false")); +    } + +    // If we're stuck waiting for an exception to continue (e.g. the user is at a breakpoint +    // messing around in the debugger), continue it now.  But only AFTER calling TerminateProcess +    // to make sure that the very next call to WaitForDebugEvent is an exit process event. +    if (m_active_exception.get()) +    { +        WINLOG_IFANY(WINDOWS_LOG_PROCESS|WINDOWS_LOG_EXCEPTION, +            "StopDebugging masking active exception"); + +        ContinueAsyncException(ExceptionResult::MaskException); +    } + +    if (!terminate) +    { +        // Indicate that we want to detach. +        m_pid_to_detach = GetProcess().GetProcessId(); + +        // Force a fresh break so that the detach can happen from the debugger thread. +        if (!::DebugBreakProcess(GetProcess().GetNativeProcess().GetSystemHandle())) +        { +            error.SetError(::GetLastError(), eErrorTypeWin32); +        } +    } + +    WINLOG_IFALL(WINDOWS_LOG_PROCESS, "StopDebugging waiting for detach from process %u to complete.", pid); + +    DWORD wait_result = WaitForSingleObject(m_debugging_ended_event, 5000); +    if (wait_result != WAIT_OBJECT_0) +    { +        error.SetError(GetLastError(), eErrorTypeWin32); +        WINERR_IFALL(WINDOWS_LOG_PROCESS, "StopDebugging WaitForSingleObject(0x%p, 5000) returned %u", +                        m_debugging_ended_event, wait_result); +    } +    else +    { +        WINLOG_IFALL(WINDOWS_LOG_PROCESS, "StopDebugging detach from process %u completed successfully.", pid); +    } + +    if (!error.Success()) +    { +        WINERR_IFALL(WINDOWS_LOG_PROCESS, +            "StopDebugging encountered an error while trying to stop process %u.  %s", +            pid, error.AsCString()); +    } +    return error; +} + +void +DebuggerThread::ContinueAsyncException(ExceptionResult result) +{ +    if (!m_active_exception.get()) +        return; + +    WINLOG_IFANY(WINDOWS_LOG_PROCESS|WINDOWS_LOG_EXCEPTION, +        "ContinueAsyncException called for inferior process %I64u, broadcasting.", +        m_process.GetProcessId()); + +    m_active_exception.reset(); +    m_exception_pred.SetValue(result, eBroadcastAlways); +} + +void +DebuggerThread::FreeProcessHandles() +{ +    m_process = HostProcess(); +    m_main_thread = HostThread(); +    if (m_image_file) +    { +        ::CloseHandle(m_image_file); +        m_image_file = nullptr; +    } +} + +void +DebuggerThread::DebugLoop() +{ +    DEBUG_EVENT dbe = {0}; +    bool should_debug = true; +    WINLOG_IFALL(WINDOWS_LOG_EVENT, "Entering WaitForDebugEvent loop"); +    while (should_debug) +    { +        WINLOGD_IFALL(WINDOWS_LOG_EVENT, "Calling WaitForDebugEvent"); +        BOOL wait_result = WaitForDebugEvent(&dbe, INFINITE); +        if (wait_result) +        { +            DWORD continue_status = DBG_CONTINUE; +            switch (dbe.dwDebugEventCode) +            { +                case EXCEPTION_DEBUG_EVENT: +                { +                    ExceptionResult status = HandleExceptionEvent(dbe.u.Exception, dbe.dwThreadId); + +                    if (status == ExceptionResult::MaskException) +                        continue_status = DBG_CONTINUE; +                    else if (status == ExceptionResult::SendToApplication) +                        continue_status = DBG_EXCEPTION_NOT_HANDLED; + +                    break; +                } +                case CREATE_THREAD_DEBUG_EVENT: +                    continue_status = HandleCreateThreadEvent(dbe.u.CreateThread, dbe.dwThreadId); +                    break; +                case CREATE_PROCESS_DEBUG_EVENT: +                    continue_status = HandleCreateProcessEvent(dbe.u.CreateProcessInfo, dbe.dwThreadId); +                    break; +                case EXIT_THREAD_DEBUG_EVENT: +                    continue_status = HandleExitThreadEvent(dbe.u.ExitThread, dbe.dwThreadId); +                    break; +                case EXIT_PROCESS_DEBUG_EVENT: +                    continue_status = HandleExitProcessEvent(dbe.u.ExitProcess, dbe.dwThreadId); +                    should_debug = false; +                    break; +                case LOAD_DLL_DEBUG_EVENT: +                    continue_status = HandleLoadDllEvent(dbe.u.LoadDll, dbe.dwThreadId); +                    break; +                case UNLOAD_DLL_DEBUG_EVENT: +                    continue_status = HandleUnloadDllEvent(dbe.u.UnloadDll, dbe.dwThreadId); +                    break; +                case OUTPUT_DEBUG_STRING_EVENT: +                    continue_status = HandleODSEvent(dbe.u.DebugString, dbe.dwThreadId); +                    break; +                case RIP_EVENT: +                    continue_status = HandleRipEvent(dbe.u.RipInfo, dbe.dwThreadId); +                    if (dbe.u.RipInfo.dwType == SLE_ERROR) +                        should_debug = false; +                    break; +            } + +            WINLOGD_IFALL(WINDOWS_LOG_EVENT, "DebugLoop calling ContinueDebugEvent(%u, %u, %u) on thread %u.", +                          dbe.dwProcessId, dbe.dwThreadId, continue_status, ::GetCurrentThreadId()); + +            ::ContinueDebugEvent(dbe.dwProcessId, dbe.dwThreadId, continue_status); + +            if (m_detached) +            { +                should_debug = false; +            } +        } +        else +        { +            WINERR_IFALL(WINDOWS_LOG_EVENT, +                "DebugLoop returned FALSE from WaitForDebugEvent.  Error = %u", +                ::GetCurrentThreadId(), ::GetLastError()); + +            should_debug = false; +        } +    } +    FreeProcessHandles(); + +    WINLOG_IFALL(WINDOWS_LOG_EVENT, "WaitForDebugEvent loop completed, exiting."); +    SetEvent(m_debugging_ended_event); +} + +ExceptionResult +DebuggerThread::HandleExceptionEvent(const EXCEPTION_DEBUG_INFO &info, DWORD thread_id) +{ +    if (m_is_shutting_down) +    { +        // A breakpoint that occurs while `m_pid_to_detach` is non-zero is a magic exception that +        // we use simply to wake up the DebuggerThread so that we can close out the debug loop. +        if (m_pid_to_detach != 0 && info.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT) +        { +            WINLOG_IFANY(WINDOWS_LOG_EVENT | WINDOWS_LOG_EXCEPTION | WINDOWS_LOG_PROCESS, +                            "Breakpoint exception is cue to detach from process 0x%x", +                            m_pid_to_detach); +            ::DebugActiveProcessStop(m_pid_to_detach); +            m_detached = true; +        } + +        // Don't perform any blocking operations while we're shutting down.  That will +        // cause TerminateProcess -> WaitForSingleObject to time out. +        return ExceptionResult::SendToApplication; +    } + +    bool first_chance = (info.dwFirstChance != 0); + +    m_active_exception.reset(new ExceptionRecord(info.ExceptionRecord, thread_id)); +    WINLOG_IFANY(WINDOWS_LOG_EVENT | WINDOWS_LOG_EXCEPTION, +                 "HandleExceptionEvent encountered %s chance exception 0x%x on thread 0x%x", +                 first_chance ? "first" : "second", info.ExceptionRecord.ExceptionCode, thread_id); + +    ExceptionResult result = m_debug_delegate->OnDebugException(first_chance, +                                                                *m_active_exception); +    m_exception_pred.SetValue(result, eBroadcastNever); + +    WINLOG_IFANY(WINDOWS_LOG_EVENT|WINDOWS_LOG_EXCEPTION, +        "DebuggerThread::HandleExceptionEvent waiting for ExceptionPred != BreakInDebugger"); + +    m_exception_pred.WaitForValueNotEqualTo(ExceptionResult::BreakInDebugger, result); + +    WINLOG_IFANY(WINDOWS_LOG_EVENT|WINDOWS_LOG_EXCEPTION, +        "DebuggerThread::HandleExceptionEvent got ExceptionPred = %u", +         m_exception_pred.GetValue()); + +    return result; +} + +DWORD +DebuggerThread::HandleCreateThreadEvent(const CREATE_THREAD_DEBUG_INFO &info, DWORD thread_id) +{ +    WINLOG_IFANY(WINDOWS_LOG_EVENT|WINDOWS_LOG_THREAD, +        "HandleCreateThreadEvent Thread 0x%x spawned in process %I64u", +        thread_id, m_process.GetProcessId()); +    HostThread thread(info.hThread); +    thread.GetNativeThread().SetOwnsHandle(false); +    m_debug_delegate->OnCreateThread(thread); +    return DBG_CONTINUE; +} + +DWORD +DebuggerThread::HandleCreateProcessEvent(const CREATE_PROCESS_DEBUG_INFO &info, DWORD thread_id) +{ +    uint32_t process_id = ::GetProcessId(info.hProcess); + +    WINLOG_IFANY(WINDOWS_LOG_EVENT | WINDOWS_LOG_PROCESS, "HandleCreateProcessEvent process %u spawned", process_id); + +    std::string thread_name; +    llvm::raw_string_ostream name_stream(thread_name); +    name_stream << "lldb.plugin.process-windows.slave[" << process_id << "]"; +    name_stream.flush(); +    ThisThread::SetName(thread_name.c_str()); + +    // info.hProcess and info.hThread are closed automatically by Windows when +    // EXIT_PROCESS_DEBUG_EVENT is received. +    m_process = HostProcess(info.hProcess); +    ((HostProcessWindows &)m_process.GetNativeProcess()).SetOwnsHandle(false); +    m_main_thread = HostThread(info.hThread); +    m_main_thread.GetNativeThread().SetOwnsHandle(false); +    m_image_file = info.hFile; + +    lldb::addr_t load_addr = reinterpret_cast<lldb::addr_t>(info.lpBaseOfImage); +    m_debug_delegate->OnDebuggerConnected(load_addr); + +    return DBG_CONTINUE; +} + +DWORD +DebuggerThread::HandleExitThreadEvent(const EXIT_THREAD_DEBUG_INFO &info, DWORD thread_id) +{ +    WINLOG_IFANY(WINDOWS_LOG_EVENT|WINDOWS_LOG_THREAD, +        "HandleExitThreadEvent Thread %u exited with code %u in process %I64u", +        thread_id, info.dwExitCode, m_process.GetProcessId()); +    m_debug_delegate->OnExitThread(thread_id, info.dwExitCode); +    return DBG_CONTINUE; +} + +DWORD +DebuggerThread::HandleExitProcessEvent(const EXIT_PROCESS_DEBUG_INFO &info, DWORD thread_id) +{ +    WINLOG_IFANY(WINDOWS_LOG_EVENT|WINDOWS_LOG_THREAD, +        "HandleExitProcessEvent process %I64u exited with code %u", +        m_process.GetProcessId(), info.dwExitCode); + +    m_debug_delegate->OnExitProcess(info.dwExitCode); + +    FreeProcessHandles(); +    return DBG_CONTINUE; +} + +DWORD +DebuggerThread::HandleLoadDllEvent(const LOAD_DLL_DEBUG_INFO &info, DWORD thread_id) +{ +    if (info.hFile == nullptr) +    { +        // Not sure what this is, so just ignore it. +        WINWARN_IFALL(WINDOWS_LOG_EVENT, "Inferior %I64u - HandleLoadDllEvent has a NULL file handle, returning...", +                      m_process.GetProcessId()); +        return DBG_CONTINUE; +    } + +    std::vector<char> buffer(1); +    DWORD required_size = GetFinalPathNameByHandle(info.hFile, &buffer[0], 0, VOLUME_NAME_DOS); +    if (required_size > 0) +    { +        buffer.resize(required_size + 1); +        required_size = GetFinalPathNameByHandle(info.hFile, &buffer[0], required_size + 1, VOLUME_NAME_DOS); +        llvm::StringRef path_str(&buffer[0]); +        const char *path = path_str.data(); +        if (path_str.startswith("\\\\?\\")) +            path += 4; + +        FileSpec file_spec(path, false); +        ModuleSpec module_spec(file_spec); +        lldb::addr_t load_addr = reinterpret_cast<lldb::addr_t>(info.lpBaseOfDll); + +        WINLOG_IFALL(WINDOWS_LOG_EVENT, "Inferior %I64u - HandleLoadDllEvent DLL '%s' loaded at address 0x%p...", +                     m_process.GetProcessId(), path, info.lpBaseOfDll); + +        m_debug_delegate->OnLoadDll(module_spec, load_addr); +    } +    else +    { +        WINERR_IFALL(WINDOWS_LOG_EVENT, +                     "Inferior %I64u - HandleLoadDllEvent Error %u occurred calling GetFinalPathNameByHandle", +                     m_process.GetProcessId(), ::GetLastError()); +    } +    // Windows does not automatically close info.hFile, so we need to do it. +    ::CloseHandle(info.hFile); +    return DBG_CONTINUE; +} + +DWORD +DebuggerThread::HandleUnloadDllEvent(const UNLOAD_DLL_DEBUG_INFO &info, DWORD thread_id) +{ +    WINLOG_IFALL(WINDOWS_LOG_EVENT, +        "HandleUnloadDllEvent process %I64u unloading DLL at addr 0x%p.", +        m_process.GetProcessId(), info.lpBaseOfDll); + +    m_debug_delegate->OnUnloadDll(reinterpret_cast<lldb::addr_t>(info.lpBaseOfDll)); +    return DBG_CONTINUE; +} + +DWORD +DebuggerThread::HandleODSEvent(const OUTPUT_DEBUG_STRING_INFO &info, DWORD thread_id) +{ +    return DBG_CONTINUE; +} + +DWORD +DebuggerThread::HandleRipEvent(const RIP_INFO &info, DWORD thread_id) +{ +    WINERR_IFALL(WINDOWS_LOG_EVENT, +        "HandleRipEvent encountered error %u (type=%u) in process %I64u thread %u", +        info.dwError, info.dwType, m_process.GetProcessId(), thread_id); + +    Error error(info.dwError, eErrorTypeWin32); +    m_debug_delegate->OnDebuggerError(error, info.dwType); + +    return DBG_CONTINUE; +} diff --git a/source/Plugins/Process/Windows/Live/DebuggerThread.h b/source/Plugins/Process/Windows/Live/DebuggerThread.h new file mode 100644 index 000000000000..6a2619413950 --- /dev/null +++ b/source/Plugins/Process/Windows/Live/DebuggerThread.h @@ -0,0 +1,100 @@ +//===-- DebuggerThread.h ----------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_Plugins_Process_Windows_DebuggerThread_H_ +#define liblldb_Plugins_Process_Windows_DebuggerThread_H_ + +#include <atomic> +#include <memory> + +#include "ForwardDecl.h" +#include "lldb/Host/HostProcess.h" +#include "lldb/Host/HostThread.h" +#include "lldb/Host/Predicate.h" +#include "lldb/Host/windows/windows.h" + +namespace lldb_private +{ + +//---------------------------------------------------------------------- +// DebuggerThread +// +// Debugs a single process, notifying listeners as appropriate when interesting +// things occur. +//---------------------------------------------------------------------- +class DebuggerThread : public std::enable_shared_from_this<DebuggerThread> +{ +  public: +    DebuggerThread(DebugDelegateSP debug_delegate); +    virtual ~DebuggerThread(); + +    Error DebugLaunch(const ProcessLaunchInfo &launch_info); +    Error DebugAttach(lldb::pid_t pid, const ProcessAttachInfo &attach_info); + +    HostProcess +    GetProcess() const +    { +        return m_process; +    } +    HostThread +    GetMainThread() const +    { +        return m_main_thread; +    } +    std::weak_ptr<ExceptionRecord> +    GetActiveException() +    { +        return m_active_exception; +    } + +    Error StopDebugging(bool terminate); + +    void ContinueAsyncException(ExceptionResult result); + +  private: +    void FreeProcessHandles(); +    void DebugLoop(); +    ExceptionResult HandleExceptionEvent(const EXCEPTION_DEBUG_INFO &info, DWORD thread_id); +    DWORD HandleCreateThreadEvent(const CREATE_THREAD_DEBUG_INFO &info, DWORD thread_id); +    DWORD HandleCreateProcessEvent(const CREATE_PROCESS_DEBUG_INFO &info, DWORD thread_id); +    DWORD HandleExitThreadEvent(const EXIT_THREAD_DEBUG_INFO &info, DWORD thread_id); +    DWORD HandleExitProcessEvent(const EXIT_PROCESS_DEBUG_INFO &info, DWORD thread_id); +    DWORD HandleLoadDllEvent(const LOAD_DLL_DEBUG_INFO &info, DWORD thread_id); +    DWORD HandleUnloadDllEvent(const UNLOAD_DLL_DEBUG_INFO &info, DWORD thread_id); +    DWORD HandleODSEvent(const OUTPUT_DEBUG_STRING_INFO &info, DWORD thread_id); +    DWORD HandleRipEvent(const RIP_INFO &info, DWORD thread_id); + +    DebugDelegateSP m_debug_delegate; + +    HostProcess m_process;    // The process being debugged. +    HostThread m_main_thread; // The main thread of the inferior. +    HANDLE m_image_file;      // The image file of the process being debugged. + +    ExceptionRecordSP m_active_exception; // The current exception waiting to be handled + +    Predicate<ExceptionResult> m_exception_pred; // A predicate which gets signalled when an exception +                                                 // is finished processing and the debug loop can be +                                                 // continued. + +    HANDLE m_debugging_ended_event; // An event which gets signalled by the debugger thread when it +                                    // exits the debugger loop and is detached from the inferior. + +    std::atomic<DWORD> m_pid_to_detach;  // Signals the loop to detach from the process (specified by pid). +    std::atomic<bool> m_is_shutting_down;   // Signals the debug loop to stop processing certain types of +                                            // events that block shutdown. +    bool m_detached;  // Indicates we've detached from the inferior process and the debug loop can exit. + +    static lldb::thread_result_t DebuggerThreadLaunchRoutine(void *data); +    lldb::thread_result_t DebuggerThreadLaunchRoutine(const ProcessLaunchInfo &launch_info); +    static lldb::thread_result_t DebuggerThreadAttachRoutine(void *data); +    lldb::thread_result_t DebuggerThreadAttachRoutine(lldb::pid_t pid, const ProcessAttachInfo &launch_info); +}; +} + +#endif diff --git a/source/Plugins/Process/Windows/Live/ForwardDecl.h b/source/Plugins/Process/Windows/Live/ForwardDecl.h new file mode 100644 index 000000000000..a782597555e1 --- /dev/null +++ b/source/Plugins/Process/Windows/Live/ForwardDecl.h @@ -0,0 +1,41 @@ +//===-- ForwardDecl.h -------------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_Plugins_Process_Windows_ForwardDecl_H_ +#define liblldb_Plugins_Process_Windows_ForwardDecl_H_ + +#include <memory> + +// ExceptionResult is returned by the debug delegate to specify how it processed +// the exception. +enum class ExceptionResult +{ +    BreakInDebugger,  // Break in the debugger and give the user a chance to interact with +                      // the program before continuing. +    MaskException,    // Eat the exception and don't let the application know it occurred. +    SendToApplication // Send the exception to the application to be handled as if there were +                      // no debugger attached. +}; + +namespace lldb_private +{ + +class ProcessWindows; + +class IDebugDelegate; +class DebuggerThread; +class ExceptionRecord; + +typedef std::shared_ptr<IDebugDelegate> DebugDelegateSP; +typedef std::shared_ptr<DebuggerThread> DebuggerThreadSP; +typedef std::shared_ptr<ExceptionRecord> ExceptionRecordSP; +typedef std::unique_ptr<ExceptionRecord> ExceptionRecordUP; +} + +#endif
\ No newline at end of file diff --git a/source/Plugins/Process/Windows/Live/IDebugDelegate.h b/source/Plugins/Process/Windows/Live/IDebugDelegate.h new file mode 100644 index 000000000000..0e7849bb8ffe --- /dev/null +++ b/source/Plugins/Process/Windows/Live/IDebugDelegate.h @@ -0,0 +1,46 @@ +//===-- IDebugDelegate.h ----------------------------------------*- C++ -*-===//
 +//
 +//                     The LLVM Compiler Infrastructure
 +//
 +// This file is distributed under the University of Illinois Open Source
 +// License. See LICENSE.TXT for details.
 +//
 +//===----------------------------------------------------------------------===//
 +
 +#ifndef liblldb_Plugins_Process_Windows_IDebugDelegate_H_
 +#define liblldb_Plugins_Process_Windows_IDebugDelegate_H_
 +
 +#include "ForwardDecl.h"
 +#include "lldb/lldb-forward.h"
 +#include "lldb/lldb-types.h"
 +#include <string>
 +
 +namespace lldb_private
 +{
 +class Error;
 +class HostThread;
 +
 +//----------------------------------------------------------------------
 +// IDebugDelegate
 +//
 +// IDebugDelegate defines an interface which allows implementors to receive
 +// notification of events that happen in a debugged process.
 +//----------------------------------------------------------------------
 +class IDebugDelegate
 +{
 +  public:
 +    virtual ~IDebugDelegate() {}
 +
 +    virtual void OnExitProcess(uint32_t exit_code) = 0;
 +    virtual void OnDebuggerConnected(lldb::addr_t image_base) = 0;
 +    virtual ExceptionResult OnDebugException(bool first_chance, const ExceptionRecord &record) = 0;
 +    virtual void OnCreateThread(const HostThread &thread) = 0;
 +    virtual void OnExitThread(lldb::tid_t thread_id, uint32_t exit_code) = 0;
 +    virtual void OnLoadDll(const ModuleSpec &module_spec, lldb::addr_t module_addr) = 0;
 +    virtual void OnUnloadDll(lldb::addr_t module_addr) = 0;
 +    virtual void OnDebugString(const std::string &string) = 0;
 +    virtual void OnDebuggerError(const Error &error, uint32_t type) = 0;
 +};
 +}
 +
 +#endif
 diff --git a/source/Plugins/Process/Windows/Live/LocalDebugDelegate.cpp b/source/Plugins/Process/Windows/Live/LocalDebugDelegate.cpp new file mode 100644 index 000000000000..a0ac9725c756 --- /dev/null +++ b/source/Plugins/Process/Windows/Live/LocalDebugDelegate.cpp @@ -0,0 +1,91 @@ +//===-- LocalDebugDelegate.cpp ----------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "LocalDebugDelegate.h" +#include "ProcessWindowsLive.h" + +using namespace lldb; +using namespace lldb_private; + +LocalDebugDelegate::LocalDebugDelegate(ProcessWP process) +    : m_process(process) +{ +} + +void +LocalDebugDelegate::OnExitProcess(uint32_t exit_code) +{ +    if (ProcessWindowsLiveSP process = GetProcessPointer()) +        process->OnExitProcess(exit_code); +} + +void +LocalDebugDelegate::OnDebuggerConnected(lldb::addr_t image_base) +{ +    if (ProcessWindowsLiveSP process = GetProcessPointer()) +        process->OnDebuggerConnected(image_base); +} + +ExceptionResult +LocalDebugDelegate::OnDebugException(bool first_chance, const ExceptionRecord &record) +{ +    if (ProcessWindowsLiveSP process = GetProcessPointer()) +        return process->OnDebugException(first_chance, record); +    else +        return ExceptionResult::MaskException; +} + +void +LocalDebugDelegate::OnCreateThread(const HostThread &thread) +{ +    if (ProcessWindowsLiveSP process = GetProcessPointer()) +        process->OnCreateThread(thread); +} + +void +LocalDebugDelegate::OnExitThread(lldb::tid_t thread_id, uint32_t exit_code) +{ +    if (ProcessWindowsLiveSP process = GetProcessPointer()) +        process->OnExitThread(thread_id, exit_code); +} + +void +LocalDebugDelegate::OnLoadDll(const lldb_private::ModuleSpec &module_spec, lldb::addr_t module_addr) +{ +    if (ProcessWindowsLiveSP process = GetProcessPointer()) +        process->OnLoadDll(module_spec, module_addr); +} + +void +LocalDebugDelegate::OnUnloadDll(lldb::addr_t module_addr) +{ +    if (ProcessWindowsLiveSP process = GetProcessPointer()) +        process->OnUnloadDll(module_addr); +} + +void +LocalDebugDelegate::OnDebugString(const std::string &string) +{ +    if (ProcessWindowsLiveSP process = GetProcessPointer()) +        process->OnDebugString(string); +} + +void +LocalDebugDelegate::OnDebuggerError(const Error &error, uint32_t type) +{ +    if (ProcessWindowsLiveSP process = GetProcessPointer()) +        process->OnDebuggerError(error, type); +} + +ProcessWindowsLiveSP +LocalDebugDelegate::GetProcessPointer() +{ +    ProcessSP process = m_process.lock(); +    return std::static_pointer_cast<ProcessWindowsLive>(process); +} diff --git a/source/Plugins/Process/Windows/Live/LocalDebugDelegate.h b/source/Plugins/Process/Windows/Live/LocalDebugDelegate.h new file mode 100644 index 000000000000..ec2f9418142f --- /dev/null +++ b/source/Plugins/Process/Windows/Live/LocalDebugDelegate.h @@ -0,0 +1,68 @@ +//===-- LocalDebugDelegate.h ------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_Plugins_Process_Windows_LocalDebugDelegate_H_ +#define liblldb_Plugins_Process_Windows_LocalDebugDelegate_H_ + +#include <memory> + +#include "IDebugDelegate.h" + +#include "lldb/lldb-forward.h" + +namespace lldb_private +{ + +class ProcessWindowsLive; +typedef std::shared_ptr<ProcessWindowsLive> ProcessWindowsLiveSP; + +//---------------------------------------------------------------------- +// LocalDebugDelegate +// +// LocalDebugDelegate creates a connection between a ProcessWindowsLive and the +// debug driver.  This serves to decouple ProcessWindowsLive from the debug driver. +// It would be possible to get a similar decoupling by just having +// ProcessWindowsLive implement this interface directly.  There are two reasons why +// we don't do this: +// +// 1) In the future when we add support for local debugging through LLGS, and we +//    go through the Native*Protocol interface, it is likely we will need the +//    additional flexibility provided by this sort of adapter pattern. +// 2) LLDB holds a shared_ptr to the ProcessWindows, and our driver thread +//    needs access to it as well.  To avoid a race condition, we want to make +//    sure that we're also holding onto a shared_ptr. +//    lldb_private::Process supports enable_shared_from_this, but that gives us +//    a ProcessSP (which is exactly what we are trying to decouple from the +//    driver), so this adapter serves as a way to transparently hold the +//    ProcessSP while still keeping it decoupled from the driver. +//---------------------------------------------------------------------- +class LocalDebugDelegate : public IDebugDelegate +{ +  public: +    explicit LocalDebugDelegate(lldb::ProcessWP process); + +    void OnExitProcess(uint32_t exit_code) override; +    void OnDebuggerConnected(lldb::addr_t image_base) override; +    ExceptionResult OnDebugException(bool first_chance, const ExceptionRecord &record) override; +    void OnCreateThread(const HostThread &thread) override; +    void OnExitThread(lldb::tid_t thread_id, uint32_t exit_code) override; +    void OnLoadDll(const lldb_private::ModuleSpec &module_spec, lldb::addr_t module_addr) override; +    void OnUnloadDll(lldb::addr_t module_addr) override; +    void OnDebugString(const std::string &message) override; +    void OnDebuggerError(const Error &error, uint32_t type) override; + +  private: +    ProcessWindowsLiveSP +    GetProcessPointer(); + +    lldb::ProcessWP m_process; +}; +} + +#endif diff --git a/source/Plugins/Process/Windows/Live/ProcessWindowsLive.cpp b/source/Plugins/Process/Windows/Live/ProcessWindowsLive.cpp new file mode 100644 index 000000000000..55102cc12ebb --- /dev/null +++ b/source/Plugins/Process/Windows/Live/ProcessWindowsLive.cpp @@ -0,0 +1,989 @@ +//===-- ProcessWindowsLive.cpp ----------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// Windows includes +#include "lldb/Host/windows/windows.h" +#include <psapi.h> + +// C++ Includes +#include <list> +#include <mutex> +#include <set> +#include <vector> + +// Other libraries and framework includes +#include "lldb/Core/Module.h" +#include "lldb/Core/ModuleSpec.h" +#include "lldb/Core/PluginManager.h" +#include "lldb/Core/Section.h" +#include "lldb/Core/State.h" +#include "lldb/Host/Host.h" +#include "lldb/Host/HostProcess.h" +#include "lldb/Host/HostNativeProcessBase.h" +#include "lldb/Host/HostNativeThreadBase.h" +#include "lldb/Host/MonitoringProcessLauncher.h" +#include "lldb/Host/ThreadLauncher.h" +#include "lldb/Host/windows/HostThreadWindows.h" +#include "lldb/Host/windows/ProcessLauncherWindows.h" +#include "lldb/Symbol/ObjectFile.h" +#include "lldb/Target/DynamicLoader.h" +#include "lldb/Target/FileAction.h" +#include "lldb/Target/MemoryRegionInfo.h" +#include "lldb/Target/RegisterContext.h" +#include "lldb/Target/StopInfo.h" +#include "lldb/Target/Target.h" + +#include "Plugins/Process/Windows/Common/ProcessWindowsLog.h" + +#include "DebuggerThread.h" +#include "ExceptionRecord.h" +#include "LocalDebugDelegate.h" +#include "ProcessWindowsLive.h" +#include "TargetThreadWindowsLive.h" + +#include "llvm/Support/Format.h" +#include "llvm/Support/raw_ostream.h" + +using namespace lldb; +using namespace lldb_private; + +#define BOOL_STR(b) ((b) ? "true" : "false") + +namespace +{ + +std::string +GetProcessExecutableName(HANDLE process_handle) +{ +    std::vector<char> file_name; +    DWORD file_name_size = MAX_PATH;  // first guess, not an absolute limit +    DWORD copied = 0; +    do +    { +        file_name_size *= 2; +        file_name.resize(file_name_size); +        copied = ::GetModuleFileNameEx(process_handle, NULL, file_name.data(), file_name_size); +    } while (copied >= file_name_size); +    file_name.resize(copied); +    return std::string(file_name.begin(), file_name.end()); +} + +std::string +GetProcessExecutableName(DWORD pid) +{ +    std::string file_name; +    HANDLE process_handle = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid); +    if (process_handle != NULL) +    { +        file_name = GetProcessExecutableName(process_handle); +        ::CloseHandle(process_handle); +    } +    return file_name; +} + +}  // anonymous namespace + +namespace lldb_private +{ + +// We store a pointer to this class in the ProcessWindows, so that we don't expose Windows +// OS specific types and implementation details from a public header file. +class ProcessWindowsData +{ +  public: +    ProcessWindowsData(bool stop_at_entry) +        : m_stop_at_entry(stop_at_entry) +        , m_initial_stop_event(nullptr) +        , m_initial_stop_received(false) +    { +        m_initial_stop_event = ::CreateEvent(nullptr, TRUE, FALSE, nullptr); +    } + +    ~ProcessWindowsData() { ::CloseHandle(m_initial_stop_event); } + +    lldb_private::Error m_launch_error; +    lldb_private::DebuggerThreadSP m_debugger; +    StopInfoSP m_pending_stop_info; +    HANDLE m_initial_stop_event; +    bool m_stop_at_entry; +    bool m_initial_stop_received; +    std::map<lldb::tid_t, HostThread> m_new_threads; +    std::set<lldb::tid_t> m_exited_threads; +}; +} +//------------------------------------------------------------------------------ +// Static functions. + +ProcessSP +ProcessWindowsLive::CreateInstance(lldb::TargetSP target_sp, Listener &listener, const FileSpec *) +{ +    return ProcessSP(new ProcessWindowsLive(target_sp, listener)); +} + +void +ProcessWindowsLive::Initialize() +{ +    static std::once_flag g_once_flag; + +    std::call_once(g_once_flag, []() +    { +        PluginManager::RegisterPlugin(GetPluginNameStatic(), +                                      GetPluginDescriptionStatic(), +                                      CreateInstance); +    }); +} + +//------------------------------------------------------------------------------ +// Constructors and destructors. + +ProcessWindowsLive::ProcessWindowsLive(lldb::TargetSP target_sp, Listener &listener) +    : lldb_private::ProcessWindows(target_sp, listener) +{ +} + +ProcessWindowsLive::~ProcessWindowsLive() +{ +} + +void +ProcessWindowsLive::Terminate() +{ +} + +lldb_private::ConstString +ProcessWindowsLive::GetPluginNameStatic() +{ +    static ConstString g_name("windows"); +    return g_name; +} + +const char * +ProcessWindowsLive::GetPluginDescriptionStatic() +{ +    return "Process plugin for Windows"; +} + +Error +ProcessWindowsLive::EnableBreakpointSite(BreakpointSite *bp_site) +{ +    WINLOG_IFALL(WINDOWS_LOG_BREAKPOINTS, "EnableBreakpointSite called with bp_site 0x%p " +                                          "(id=%d, addr=0x%x)", +                 bp_site->GetID(), bp_site->GetLoadAddress()); + +    Error error = EnableSoftwareBreakpoint(bp_site); +    if (!error.Success()) +    { +        WINERR_IFALL(WINDOWS_LOG_BREAKPOINTS, "EnableBreakpointSite failed.  %s", error.AsCString()); +    } +    return error; +} + +Error +ProcessWindowsLive::DisableBreakpointSite(BreakpointSite *bp_site) +{ +    WINLOG_IFALL(WINDOWS_LOG_BREAKPOINTS, "DisableBreakpointSite called with bp_site 0x%p " +                                          "(id=%d, addr=0x%x)", +                 bp_site->GetID(), bp_site->GetLoadAddress()); + +    Error error = DisableSoftwareBreakpoint(bp_site); + +    if (!error.Success()) +    { +        WINERR_IFALL(WINDOWS_LOG_BREAKPOINTS, "DisableBreakpointSite failed.  %s", error.AsCString()); +    } +    return error; +} + +bool +ProcessWindowsLive::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) +{ +    // Add all the threads that were previously running and for which we did not detect a thread +    // exited event. +    int new_size = 0; +    int continued_threads = 0; +    int exited_threads = 0; +    int new_threads = 0; + +    for (ThreadSP old_thread : old_thread_list.Threads()) +    { +        lldb::tid_t old_thread_id = old_thread->GetID(); +        auto exited_thread_iter = m_session_data->m_exited_threads.find(old_thread_id); +        if (exited_thread_iter == m_session_data->m_exited_threads.end()) +        { +            new_thread_list.AddThread(old_thread); +            ++new_size; +            ++continued_threads; +            WINLOGV_IFALL(WINDOWS_LOG_THREAD, "UpdateThreadList - Thread %u was running and is still running.", +                          old_thread_id); +        } +        else +        { +            WINLOGV_IFALL(WINDOWS_LOG_THREAD, "UpdateThreadList - Thread %u was running and has exited.", +                          old_thread_id); +            ++exited_threads; +        } +    } + +    // Also add all the threads that are new since the last time we broke into the debugger. +    for (const auto &thread_info : m_session_data->m_new_threads) +    { +        ThreadSP thread(new TargetThreadWindowsLive(*this, thread_info.second)); +        thread->SetID(thread_info.first); +        new_thread_list.AddThread(thread); +        ++new_size; +        ++new_threads; +        WINLOGV_IFALL(WINDOWS_LOG_THREAD, "UpdateThreadList - Thread %u is new since last update.", thread_info.first); +    } + +    WINLOG_IFALL(WINDOWS_LOG_THREAD, "UpdateThreadList - %d new threads, %d old threads, %d exited threads.", +                 new_threads, continued_threads, exited_threads); + +    m_session_data->m_new_threads.clear(); +    m_session_data->m_exited_threads.clear(); + +    return new_size > 0; +} + +Error +ProcessWindowsLive::DoLaunch(Module *exe_module, +                             ProcessLaunchInfo &launch_info) +{ +    // Even though m_session_data is accessed here, it is before a debugger thread has been +    // kicked off.  So there's no race conditions, and it shouldn't be necessary to acquire +    // the mutex. + +    Error result; +    if (!launch_info.GetFlags().Test(eLaunchFlagDebug)) +    { +        StreamString stream; +        stream.Printf("ProcessWindows unable to launch '%s'.  ProcessWindows can only be used for debug launches.", +                      launch_info.GetExecutableFile().GetPath().c_str()); +        std::string message = stream.GetString(); +        result.SetErrorString(message.c_str()); + +        WINERR_IFALL(WINDOWS_LOG_PROCESS, message.c_str()); +        return result; +    } + +    bool stop_at_entry = launch_info.GetFlags().Test(eLaunchFlagStopAtEntry); +    m_session_data.reset(new ProcessWindowsData(stop_at_entry)); + +    SetPrivateState(eStateLaunching); +    DebugDelegateSP delegate(new LocalDebugDelegate(shared_from_this())); +    m_session_data->m_debugger.reset(new DebuggerThread(delegate)); +    DebuggerThreadSP debugger = m_session_data->m_debugger; + +    // Kick off the DebugLaunch asynchronously and wait for it to complete. +    result = debugger->DebugLaunch(launch_info); +    if (result.Fail()) +    { +        WINERR_IFALL(WINDOWS_LOG_PROCESS, "DoLaunch failed launching '%s'.  %s", +                     launch_info.GetExecutableFile().GetPath().c_str(), result.AsCString()); +        return result; +    } + +    HostProcess process; +    Error error = WaitForDebuggerConnection(debugger, process); +    if (error.Fail()) +    { +        WINERR_IFALL(WINDOWS_LOG_PROCESS, "DoLaunch failed launching '%s'.  %s", +                     launch_info.GetExecutableFile().GetPath().c_str(), error.AsCString()); +        return error; +    } + +    WINLOG_IFALL(WINDOWS_LOG_PROCESS, "DoLaunch successfully launched '%s'", +                 launch_info.GetExecutableFile().GetPath().c_str()); + +    // We've hit the initial stop.  If eLaunchFlagsStopAtEntry was specified, the private state +    // should already be set to eStateStopped as a result of hitting the initial breakpoint.  If +    // it was not set, the breakpoint should have already been resumed from and the private state +    // should already be eStateRunning. +    launch_info.SetProcessID(process.GetProcessId()); +    SetID(process.GetProcessId()); + +    return result; +} + +Error +ProcessWindowsLive::DoAttachToProcessWithID(lldb::pid_t pid, const ProcessAttachInfo &attach_info) +{ +    m_session_data.reset(new ProcessWindowsData(!attach_info.GetContinueOnceAttached())); + +    DebugDelegateSP delegate(new LocalDebugDelegate(shared_from_this())); +    DebuggerThreadSP debugger(new DebuggerThread(delegate)); + +    m_session_data->m_debugger = debugger; + +    DWORD process_id = static_cast<DWORD>(pid); +    Error error = debugger->DebugAttach(process_id, attach_info); +    if (error.Fail()) +    { +        WINLOG_IFALL(WINDOWS_LOG_PROCESS, +                     "DoAttachToProcessWithID encountered an error occurred initiating the asynchronous attach.  %s", +                     error.AsCString()); +        return error; +    } + +    HostProcess process; +    error = WaitForDebuggerConnection(debugger, process); +    if (error.Fail()) +    { +        WINLOG_IFALL(WINDOWS_LOG_PROCESS, +                     "DoAttachToProcessWithID encountered an error waiting for the debugger to connect.  %s", +                     error.AsCString()); +        return error; +    } + +    WINLOG_IFALL(WINDOWS_LOG_PROCESS, "DoAttachToProcessWithID successfully attached to process with pid=%u", +                 process_id); + +    // We've hit the initial stop.  If eLaunchFlagsStopAtEntry was specified, the private state +    // should already be set to eStateStopped as a result of hitting the initial breakpoint.  If +    // it was not set, the breakpoint should have already been resumed from and the private state +    // should already be eStateRunning. +    SetID(process.GetProcessId()); +    return error; +} + +Error +ProcessWindowsLive::WaitForDebuggerConnection(DebuggerThreadSP debugger, HostProcess &process) +{ +    Error result; +    WINLOG_IFANY(WINDOWS_LOG_PROCESS|WINDOWS_LOG_BREAKPOINTS, "WaitForDebuggerConnection Waiting for loader breakpoint."); + +    // Block this function until we receive the initial stop from the process. +    if (::WaitForSingleObject(m_session_data->m_initial_stop_event, INFINITE) == WAIT_OBJECT_0) +    { +        WINLOG_IFANY(WINDOWS_LOG_PROCESS|WINDOWS_LOG_BREAKPOINTS, "WaitForDebuggerConnection hit loader breakpoint, returning."); + +        process = debugger->GetProcess(); +        return m_session_data->m_launch_error; +    } +    else +        return Error(::GetLastError(), eErrorTypeWin32); +} + +Error +ProcessWindowsLive::DoResume() +{ +    llvm::sys::ScopedLock lock(m_mutex); +    Error error; + +    StateType private_state = GetPrivateState(); +    if (private_state == eStateStopped || private_state == eStateCrashed) +    { +        WINLOG_IFALL(WINDOWS_LOG_PROCESS, "DoResume called for process %I64u while state is %u.  Resuming...", +                     m_session_data->m_debugger->GetProcess().GetProcessId(), GetPrivateState()); + +        ExceptionRecordSP active_exception = +            m_session_data->m_debugger->GetActiveException().lock(); +        if (active_exception) +        { +            // Resume the process and continue processing debug events.  Mask +            // the exception so that from the process's view, there is no +            // indication that anything happened. +            m_session_data->m_debugger->ContinueAsyncException( +                ExceptionResult::MaskException); +        } + +        WINLOG_IFANY(WINDOWS_LOG_PROCESS | WINDOWS_LOG_THREAD, "DoResume resuming %u threads.", +                     m_thread_list.GetSize()); + +        for (int i = 0; i < m_thread_list.GetSize(); ++i) +        { +            auto thread = std::static_pointer_cast<TargetThreadWindowsLive>( +                m_thread_list.GetThreadAtIndex(i)); +            thread->DoResume(); +        } + +        SetPrivateState(eStateRunning); +    } +    else +    { +        WINERR_IFALL(WINDOWS_LOG_PROCESS, "DoResume called for process %I64u but state is %u.  Returning...", +                     m_session_data->m_debugger->GetProcess().GetProcessId(), GetPrivateState()); +    } +    return error; +} + + +//------------------------------------------------------------------------------ +// ProcessInterface protocol. + +lldb_private::ConstString +ProcessWindowsLive::GetPluginName() +{ +    return GetPluginNameStatic(); +} + +uint32_t +ProcessWindowsLive::GetPluginVersion() +{ +    return 1; +} + +Error +ProcessWindowsLive::DoDetach(bool keep_stopped) +{ +    DebuggerThreadSP debugger_thread; +    StateType private_state; +    { +        // Acquire the lock only long enough to get the DebuggerThread. +        // StopDebugging() will trigger a call back into ProcessWindows which +        // will also acquire the lock.  Thus we have to release the lock before +        // calling StopDebugging(). +        llvm::sys::ScopedLock lock(m_mutex); + +        private_state = GetPrivateState(); + +        if (!m_session_data) +        { +            WINWARN_IFALL(WINDOWS_LOG_PROCESS, "DoDetach called while state = %u, but there is no active session.", +                          private_state); +            return Error(); +        } + +        debugger_thread = m_session_data->m_debugger; +    } + +    Error error; +    if (private_state != eStateExited && private_state != eStateDetached) +    { +        WINLOG_IFALL(WINDOWS_LOG_PROCESS, "DoDetach called for process %I64u while state = %u.  Detaching...", +                     debugger_thread->GetProcess().GetNativeProcess().GetSystemHandle(), private_state); +        error = debugger_thread->StopDebugging(false); +        if (error.Success()) +        { +            SetPrivateState(eStateDetached); +        } + +        // By the time StopDebugging returns, there is no more debugger thread, so +        // we can be assured that no other thread will race for the session data. +        m_session_data.reset(); +    } +    else +    { +        WINERR_IFALL(WINDOWS_LOG_PROCESS, +                     "DoDetach called for process %I64u while state = %u, but cannot destroy in this state.", +                     debugger_thread->GetProcess().GetNativeProcess().GetSystemHandle(), private_state); +    } + +    return error; +} + +Error +ProcessWindowsLive::DoDestroy() +{ +    DebuggerThreadSP debugger_thread; +    StateType private_state; +    { +        // Acquire this lock inside an inner scope, only long enough to get the DebuggerThread. +        // StopDebugging() will trigger a call back into ProcessWindows which will acquire the lock +        // again, so we need to not deadlock. +        llvm::sys::ScopedLock lock(m_mutex); + +        private_state = GetPrivateState(); + +        if (!m_session_data) +        { +            WINWARN_IFALL(WINDOWS_LOG_PROCESS, "DoDestroy called while state = %u, but there is no active session.", +                          private_state); +            return Error(); +        } + +        debugger_thread = m_session_data->m_debugger; +    } + +    Error error; +    if (private_state != eStateExited && private_state != eStateDetached) +    { +        WINLOG_IFALL(WINDOWS_LOG_PROCESS, "DoDestroy called for process %I64u while state = %u.  Shutting down...", +                     debugger_thread->GetProcess().GetNativeProcess().GetSystemHandle(), private_state); +        error = debugger_thread->StopDebugging(true); + +        // By the time StopDebugging returns, there is no more debugger thread, so +        // we can be assured that no other thread will race for the session data. +        m_session_data.reset(); +    } +    else +    { +        WINERR_IFALL(WINDOWS_LOG_PROCESS, +                     "DoDestroy called for process %I64u while state = %u, but cannot destroy in this state.", +                     debugger_thread->GetProcess().GetNativeProcess().GetSystemHandle(), private_state); +    } + +    return error; +} + +void +ProcessWindowsLive::RefreshStateAfterStop() +{ +    llvm::sys::ScopedLock lock(m_mutex); + +    if (!m_session_data) +    { +        WINWARN_IFALL(WINDOWS_LOG_PROCESS, "RefreshStateAfterStop called with no active session.  Returning..."); +        return; +    } + +    m_thread_list.RefreshStateAfterStop(); + +    std::weak_ptr<ExceptionRecord> exception_record = m_session_data->m_debugger->GetActiveException(); +    ExceptionRecordSP active_exception = exception_record.lock(); +    if (!active_exception) +    { +        WINERR_IFALL(WINDOWS_LOG_PROCESS, "RefreshStateAfterStop called for process %I64u but there is no " +                                          "active exception.  Why is the process stopped?", +                     m_session_data->m_debugger->GetProcess().GetProcessId()); +        return; +    } + +    StopInfoSP stop_info; +    m_thread_list.SetSelectedThreadByID(active_exception->GetThreadID()); +    ThreadSP stop_thread = m_thread_list.GetSelectedThread(); +    if (!stop_thread) +        return; + +    RegisterContextSP register_context = stop_thread->GetRegisterContext(); + +    // The current EIP is AFTER the BP opcode, which is one byte. +    uint64_t pc = register_context->GetPC() - 1; +    if (active_exception->GetExceptionCode() == EXCEPTION_BREAKPOINT) +    { +        BreakpointSiteSP site(GetBreakpointSiteList().FindByAddress(pc)); + +        if (site) +        { +            WINLOG_IFANY(WINDOWS_LOG_BREAKPOINTS | WINDOWS_LOG_EXCEPTION, +                         "RefreshStateAfterStop detected breakpoint in process %I64u at " +                         "address 0x%I64x with breakpoint site %d", +                         m_session_data->m_debugger->GetProcess().GetProcessId(), pc, site->GetID()); + +            if (site->ValidForThisThread(stop_thread.get())) +            { +                WINLOG_IFALL(WINDOWS_LOG_BREAKPOINTS | WINDOWS_LOG_EXCEPTION, +                             "Breakpoint site %d is valid for this thread (0x%I64x), creating stop info.", +                             site->GetID(), stop_thread->GetID()); + +                stop_info = StopInfo::CreateStopReasonWithBreakpointSiteID( +                    *stop_thread, site->GetID()); +                register_context->SetPC(pc); +            } +            else +            { +                WINLOG_IFALL(WINDOWS_LOG_BREAKPOINTS | WINDOWS_LOG_EXCEPTION, +                             "Breakpoint site %d is not valid for this thread, creating empty stop info.", +                             site->GetID()); +            } +        } +        stop_thread->SetStopInfo(stop_info); +    } +    else if (active_exception->GetExceptionCode() == EXCEPTION_SINGLE_STEP) +    { +        stop_info = StopInfo::CreateStopReasonToTrace(*stop_thread); +        stop_thread->SetStopInfo(stop_info); +        WINLOG_IFANY(WINDOWS_LOG_EXCEPTION | WINDOWS_LOG_STEP, "RefreshStateAfterStop single stepping thread %u", +                     stop_thread->GetID()); +    } +    else +    { +        std::string desc; +        llvm::raw_string_ostream desc_stream(desc); +        desc_stream << "Exception " << llvm::format_hex(active_exception->GetExceptionCode(), 8) +                    << " encountered at address " << llvm::format_hex(pc, 8); +        stop_info = StopInfo::CreateStopReasonWithException(*stop_thread, desc_stream.str().c_str()); +        stop_thread->SetStopInfo(stop_info); +        WINLOG_IFALL(WINDOWS_LOG_EXCEPTION, desc_stream.str().c_str()); +    } +} + +bool +ProcessWindowsLive::IsAlive() +{ +    StateType state = GetPrivateState(); +    switch (state) +    { +        case eStateCrashed: +        case eStateDetached: +        case eStateUnloaded: +        case eStateExited: +        case eStateInvalid: +            return false; +        default: +            return true; +    } +} + +Error +ProcessWindowsLive::DoHalt(bool &caused_stop) +{ +    Error error; +    StateType state = GetPrivateState(); +    if (state == eStateStopped) +        caused_stop = false; +    else +    { +        llvm::sys::ScopedLock lock(m_mutex); +        caused_stop = ::DebugBreakProcess(m_session_data->m_debugger->GetProcess().GetNativeProcess().GetSystemHandle()); +        if (!caused_stop) +        { +            error.SetError(::GetLastError(), eErrorTypeWin32); +            WINERR_IFALL(WINDOWS_LOG_PROCESS, "DoHalt called DebugBreakProcess, but it failed with error %u", +                         error.GetError()); +        } +    } +    return error; +} + +void +ProcessWindowsLive::DidLaunch() +{ +    ArchSpec arch_spec; +    DidAttach(arch_spec); +} + +void +ProcessWindowsLive::DidAttach(ArchSpec &arch_spec) +{ +    llvm::sys::ScopedLock lock(m_mutex); + +    // The initial stop won't broadcast the state change event, so account for that here. +    if (m_session_data && GetPrivateState() == eStateStopped && m_session_data->m_stop_at_entry) +        RefreshStateAfterStop(); +} + +size_t +ProcessWindowsLive::DoReadMemory(lldb::addr_t vm_addr, +                                 void *buf, +                                 size_t size, +                                 Error &error) +{ +    llvm::sys::ScopedLock lock(m_mutex); + +    if (!m_session_data) +        return 0; + +    WINLOG_IFALL(WINDOWS_LOG_MEMORY, "DoReadMemory attempting to read %u bytes from address 0x%I64x", size, vm_addr); + +    HostProcess process = m_session_data->m_debugger->GetProcess(); +    void *addr = reinterpret_cast<void *>(vm_addr); +    SIZE_T bytes_read = 0; +    if (!ReadProcessMemory(process.GetNativeProcess().GetSystemHandle(), addr, buf, size, &bytes_read)) +    { +        error.SetError(GetLastError(), eErrorTypeWin32); +        WINERR_IFALL(WINDOWS_LOG_MEMORY, "DoReadMemory failed with error code %u", error.GetError()); +    } +    return bytes_read; +} + +size_t +ProcessWindowsLive::DoWriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size, Error &error) +{ +    llvm::sys::ScopedLock lock(m_mutex); +    WINLOG_IFALL(WINDOWS_LOG_MEMORY, "DoWriteMemory attempting to write %u bytes into address 0x%I64x", size, vm_addr); + +    if (!m_session_data) +    { +        WINERR_IFANY(WINDOWS_LOG_MEMORY, "DoWriteMemory cannot write, there is no active debugger connection."); +        return 0; +    } + +    HostProcess process = m_session_data->m_debugger->GetProcess(); +    void *addr = reinterpret_cast<void *>(vm_addr); +    SIZE_T bytes_written = 0; +    lldb::process_t handle = process.GetNativeProcess().GetSystemHandle(); +    if (WriteProcessMemory(handle, addr, buf, size, &bytes_written)) +        FlushInstructionCache(handle, addr, bytes_written); +    else +    { +        error.SetError(GetLastError(), eErrorTypeWin32); +        WINLOG_IFALL(WINDOWS_LOG_MEMORY, "DoWriteMemory failed with error code %u", error.GetError()); +    } +    return bytes_written; +} + +Error +ProcessWindowsLive::GetMemoryRegionInfo(lldb::addr_t vm_addr, MemoryRegionInfo &info) +{ +    Error error; +    llvm::sys::ScopedLock lock(m_mutex); + +    if (!m_session_data) +    { +        error.SetErrorString("GetMemoryRegionInfo called with no debugging session."); +        WINERR_IFALL(WINDOWS_LOG_MEMORY, error.AsCString()); +        return error; +    } + +    HostProcess process = m_session_data->m_debugger->GetProcess(); +    lldb::process_t handle = process.GetNativeProcess().GetSystemHandle(); +    if (handle == nullptr || handle == LLDB_INVALID_PROCESS) +    { +        error.SetErrorString("GetMemoryRegionInfo called with an invalid target process."); +        WINERR_IFALL(WINDOWS_LOG_MEMORY, error.AsCString()); +        return error; +    } + +    WINLOG_IFALL(WINDOWS_LOG_MEMORY, "GetMemoryRegionInfo getting info for address 0x%I64x", vm_addr); + +    void *addr = reinterpret_cast<void *>(vm_addr); +    MEMORY_BASIC_INFORMATION mem_info = {0}; +    SIZE_T result = ::VirtualQueryEx(handle, addr, &mem_info, sizeof(mem_info)); +    if (result == 0) +    { +        error.SetError(::GetLastError(), eErrorTypeWin32); +        WINERR_IFALL(WINDOWS_LOG_MEMORY, +                     "VirtualQueryEx returned error %u while getting memory region info for address 0x%I64x", +                     error.GetError(), vm_addr); +        return error; +    } +    const bool readable = IsPageReadable(mem_info.Protect); +    const bool executable = IsPageExecutable(mem_info.Protect); +    const bool writable = IsPageWritable(mem_info.Protect); +    info.SetReadable(readable ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); +    info.SetExecutable(executable ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); +    info.SetWritable(writable ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); + +    error.SetError(::GetLastError(), eErrorTypeWin32); +    WINLOGV_IFALL(WINDOWS_LOG_MEMORY, "Memory region info for address 0x%I64u: readable=%s, executable=%s, writable=%s", +                  BOOL_STR(readable), BOOL_STR(executable), BOOL_STR(writable)); +    return error; +} + +bool +ProcessWindowsLive::CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_name) +{ +    if (plugin_specified_by_name) +        return true; + +    // For now we are just making sure the file exists for a given module +    ModuleSP exe_module_sp(target_sp->GetExecutableModule()); +    if (exe_module_sp.get()) +        return exe_module_sp->GetFileSpec().Exists(); +    // However, if there is no executable module, we return true since we might be preparing to attach. +    return true; +} + +void +ProcessWindowsLive::OnExitProcess(uint32_t exit_code) +{ +    // No need to acquire the lock since m_session_data isn't accessed. +    WINLOG_IFALL(WINDOWS_LOG_PROCESS, "Process %u exited with code %u", GetID(), exit_code); + +    TargetSP target = m_target_sp.lock(); +    if (target) +    { +        ModuleSP executable_module = target->GetExecutableModule(); +        ModuleList unloaded_modules; +        unloaded_modules.Append(executable_module); +        target->ModulesDidUnload(unloaded_modules, true); +    } + +    SetProcessExitStatus(nullptr, GetID(), true, 0, exit_code); +    SetPrivateState(eStateExited); +} + +void +ProcessWindowsLive::OnDebuggerConnected(lldb::addr_t image_base) +{ +    DebuggerThreadSP debugger = m_session_data->m_debugger; + +    WINLOG_IFALL(WINDOWS_LOG_PROCESS, "Debugger connected to process %I64u.  Image base = 0x%I64x", +                 debugger->GetProcess().GetProcessId(), image_base); + +    ModuleSP module = GetTarget().GetExecutableModule(); +    if (!module) +    { +        // During attach, we won't have the executable module, so find it now. +        const DWORD pid = debugger->GetProcess().GetProcessId(); +        const std::string file_name = GetProcessExecutableName(pid); +        if (file_name.empty()) +        { +            return; +        } + +        FileSpec executable_file(file_name, true); +        ModuleSpec module_spec(executable_file); +        Error error; +        module = GetTarget().GetSharedModule(module_spec, &error); +        if (!module) +        { +            return; +        } + +        GetTarget().SetExecutableModule(module, false); +    } + +    bool load_addr_changed; +    module->SetLoadAddress(GetTarget(), image_base, false, load_addr_changed); + +    ModuleList loaded_modules; +    loaded_modules.Append(module); +    GetTarget().ModulesDidLoad(loaded_modules); + +    // Add the main executable module to the list of pending module loads.  We can't call +    // GetTarget().ModulesDidLoad() here because we still haven't returned from DoLaunch() / DoAttach() yet +    // so the target may not have set the process instance to `this` yet. +    llvm::sys::ScopedLock lock(m_mutex); +    const HostThreadWindows &wmain_thread = debugger->GetMainThread().GetNativeThread(); +    m_session_data->m_new_threads[wmain_thread.GetThreadId()] = debugger->GetMainThread(); +} + +ExceptionResult +ProcessWindowsLive::OnDebugException(bool first_chance, const ExceptionRecord &record) +{ +    llvm::sys::ScopedLock lock(m_mutex); + +    // FIXME: Without this check, occasionally when running the test suite there is +    // an issue where m_session_data can be null.  It's not clear how this could happen +    // but it only surfaces while running the test suite.  In order to properly diagnose +    // this, we probably need to first figure allow the test suite to print out full +    // lldb logs, and then add logging to the process plugin. +    if (!m_session_data) +    { +        WINERR_IFANY(WINDOWS_LOG_EXCEPTION, +                     "Debugger thread reported exception 0x%x at address 0x%I64x, but there is no session.", +                     record.GetExceptionCode(), record.GetExceptionAddress()); +        return ExceptionResult::SendToApplication; +    } + +    if (!first_chance) +    { +        // Any second chance exception is an application crash by definition. +        SetPrivateState(eStateCrashed); +    } + +    ExceptionResult result = ExceptionResult::SendToApplication; +    switch (record.GetExceptionCode()) +    { +        case EXCEPTION_BREAKPOINT: +            // Handle breakpoints at the first chance. +            result = ExceptionResult::BreakInDebugger; + +            if (!m_session_data->m_initial_stop_received) +            { +                WINLOG_IFANY(WINDOWS_LOG_BREAKPOINTS, +                             "Hit loader breakpoint at address 0x%I64x, setting initial stop event.", +                             record.GetExceptionAddress()); +                m_session_data->m_initial_stop_received = true; +                ::SetEvent(m_session_data->m_initial_stop_event); +            } +            else +            { +                WINLOG_IFANY(WINDOWS_LOG_BREAKPOINTS, +                             "Hit non-loader breakpoint at address 0x%I64x.", +                             record.GetExceptionAddress()); +            } +            SetPrivateState(eStateStopped); +            break; +        case EXCEPTION_SINGLE_STEP: +            result = ExceptionResult::BreakInDebugger; +            SetPrivateState(eStateStopped); +            break; +        default: +            WINLOG_IFANY(WINDOWS_LOG_EXCEPTION, +                         "Debugger thread reported exception 0x%x at address 0x%I64x (first_chance=%s)", +                         record.GetExceptionCode(), record.GetExceptionAddress(), BOOL_STR(first_chance)); +            // For non-breakpoints, give the application a chance to handle the exception first. +            if (first_chance) +                result = ExceptionResult::SendToApplication; +            else +                result = ExceptionResult::BreakInDebugger; +    } + +    return result; +} + +void +ProcessWindowsLive::OnCreateThread(const HostThread &new_thread) +{ +    llvm::sys::ScopedLock lock(m_mutex); +    const HostThreadWindows &wnew_thread = new_thread.GetNativeThread(); +    m_session_data->m_new_threads[wnew_thread.GetThreadId()] = new_thread; +} + +void +ProcessWindowsLive::OnExitThread(lldb::tid_t thread_id, uint32_t exit_code) +{ +    llvm::sys::ScopedLock lock(m_mutex); + +    // On a forced termination, we may get exit thread events after the session +    // data has been cleaned up. +    if (!m_session_data) +        return; + +    // A thread may have started and exited before the debugger stopped allowing a refresh. +    // Just remove it from the new threads list in that case. +    auto iter = m_session_data->m_new_threads.find(thread_id); +    if (iter != m_session_data->m_new_threads.end()) +        m_session_data->m_new_threads.erase(iter); +    else +        m_session_data->m_exited_threads.insert(thread_id); +} + +void +ProcessWindowsLive::OnLoadDll(const ModuleSpec &module_spec, lldb::addr_t module_addr) +{ +    // Confusingly, there is no Target::AddSharedModule.  Instead, calling GetSharedModule() with +    // a new module will add it to the module list and return a corresponding ModuleSP. +    Error error; +    ModuleSP module = GetTarget().GetSharedModule(module_spec, &error); +    bool load_addr_changed = false; +    module->SetLoadAddress(GetTarget(), module_addr, false, load_addr_changed); + +    ModuleList loaded_modules; +    loaded_modules.Append(module); +    GetTarget().ModulesDidLoad(loaded_modules); +} + +void +ProcessWindowsLive::OnUnloadDll(lldb::addr_t module_addr) +{ +    Address resolved_addr; +    if (GetTarget().ResolveLoadAddress(module_addr, resolved_addr)) +    { +        ModuleSP module = resolved_addr.GetModule(); +        if (module) +        { +            ModuleList unloaded_modules; +            unloaded_modules.Append(module); +            GetTarget().ModulesDidUnload(unloaded_modules, false); +        } +    } +} + +void +ProcessWindowsLive::OnDebugString(const std::string &string) +{ +} + +void +ProcessWindowsLive::OnDebuggerError(const Error &error, uint32_t type) +{ +    llvm::sys::ScopedLock lock(m_mutex); + +    if (m_session_data->m_initial_stop_received) +    { +        // This happened while debugging.  Do we shutdown the debugging session, try to continue, +        // or do something else? +        WINERR_IFALL(WINDOWS_LOG_PROCESS, "Error %u occurred during debugging.  Unexpected behavior may result.  %s", +                     error.GetError(), error.AsCString()); +    } +    else +    { +        // If we haven't actually launched the process yet, this was an error launching the +        // process.  Set the internal error and signal the initial stop event so that the DoLaunch +        // method wakes up and returns a failure. +        m_session_data->m_launch_error = error; +        ::SetEvent(m_session_data->m_initial_stop_event); +        WINERR_IFALL(WINDOWS_LOG_PROCESS, "Error %u occurred launching the process before the initial stop.  %s", +                     error.GetError(), error.AsCString()); +        return; +    } +} diff --git a/source/Plugins/Process/Windows/Live/ProcessWindowsLive.h b/source/Plugins/Process/Windows/Live/ProcessWindowsLive.h new file mode 100644 index 000000000000..2429f873c823 --- /dev/null +++ b/source/Plugins/Process/Windows/Live/ProcessWindowsLive.h @@ -0,0 +1,125 @@ +//===-- ProcessWindowsLive.h ------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_Plugins_Process_Windows_Live_ProcessWindowsLive_H_ +#define liblldb_Plugins_Process_Windows_Live_ProcessWindowsLive_H_ + +// C Includes + +// C++ Includes +#include <memory> +#include <queue> + +// Other libraries and framework includes +#include "ForwardDecl.h" +#include "IDebugDelegate.h" +#include "lldb/lldb-forward.h" +#include "lldb/Core/Error.h" +#include "lldb/Host/HostThread.h" +#include "lldb/Target/Process.h" + +#include "llvm/Support/Mutex.h" + +#include "plugins/Process/Windows/Common/ProcessWindows.h" + +class ProcessMonitor; + +namespace lldb_private +{ +class HostProcess; +class ProcessWindowsData; + +class ProcessWindowsLive : public lldb_private::ProcessWindows, public lldb_private::IDebugDelegate +{ +public: +    //------------------------------------------------------------------ +    // Static functions. +    //------------------------------------------------------------------ +    static lldb::ProcessSP +    CreateInstance(lldb::TargetSP target_sp, +                   lldb_private::Listener &listener, +                   const lldb_private::FileSpec *); + +    static void +    Initialize(); + +    static void +    Terminate(); + +    static lldb_private::ConstString +    GetPluginNameStatic(); + +    static const char * +    GetPluginDescriptionStatic(); + +    //------------------------------------------------------------------ +    // Constructors and destructors +    //------------------------------------------------------------------ +    ProcessWindowsLive(lldb::TargetSP target_sp, +                       lldb_private::Listener &listener); + +    ~ProcessWindowsLive(); + +    // lldb_private::Process overrides +    lldb_private::ConstString GetPluginName() override; +    uint32_t GetPluginVersion() override; + +    lldb_private::Error EnableBreakpointSite(lldb_private::BreakpointSite *bp_site) override; +    lldb_private::Error DisableBreakpointSite(lldb_private::BreakpointSite *bp_site) override; + +    lldb_private::Error DoDetach(bool keep_stopped) override; +    lldb_private::Error DoLaunch(lldb_private::Module *exe_module, lldb_private::ProcessLaunchInfo &launch_info) override; +    lldb_private::Error DoAttachToProcessWithID(lldb::pid_t pid, +                                                const lldb_private::ProcessAttachInfo &attach_info) override; +    lldb_private::Error DoResume() override; +    lldb_private::Error DoDestroy() override; +    lldb_private::Error DoHalt(bool &caused_stop) override; + +    void DidLaunch() override; +    void DidAttach(lldb_private::ArchSpec &arch_spec) override; + +    void RefreshStateAfterStop() override; + +    bool CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_name) override; +    bool +    DestroyRequiresHalt() override +    { +        return false; +    } +    bool UpdateThreadList(lldb_private::ThreadList &old_thread_list, lldb_private::ThreadList &new_thread_list) override; +    bool IsAlive() override; + +    size_t DoReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, lldb_private::Error &error) override; +    size_t DoWriteMemory(lldb::addr_t vm_addr, const void *buf, size_t size, lldb_private::Error &error) override; +    lldb_private::Error GetMemoryRegionInfo(lldb::addr_t vm_addr, lldb_private::MemoryRegionInfo &info) override; + +    // IDebugDelegate overrides. +    void OnExitProcess(uint32_t exit_code) override; +    void OnDebuggerConnected(lldb::addr_t image_base) override; +    ExceptionResult OnDebugException(bool first_chance, const lldb_private::ExceptionRecord &record) override; +    void OnCreateThread(const lldb_private::HostThread &thread) override; +    void OnExitThread(lldb::tid_t thread_id, uint32_t exit_code) override; +    void OnLoadDll(const lldb_private::ModuleSpec &module_spec, lldb::addr_t module_addr) override; +    void OnUnloadDll(lldb::addr_t module_addr) override; +    void OnDebugString(const std::string &string) override; +    void OnDebuggerError(const lldb_private::Error &error, uint32_t type) override; + +  private: +    lldb_private::Error WaitForDebuggerConnection(lldb_private::DebuggerThreadSP debugger, +                                                  lldb_private::HostProcess &process); + +    llvm::sys::Mutex m_mutex; + +    // Data for the active debugging session. +    std::unique_ptr<lldb_private::ProcessWindowsData> m_session_data; +}; + +} + +#endif  // liblldb_Plugins_Process_Windows_Live_ProcessWindowsLive_H_ diff --git a/source/Plugins/Process/Windows/Live/TargetThreadWindowsLive.cpp b/source/Plugins/Process/Windows/Live/TargetThreadWindowsLive.cpp new file mode 100644 index 000000000000..52b32716e674 --- /dev/null +++ b/source/Plugins/Process/Windows/Live/TargetThreadWindowsLive.cpp @@ -0,0 +1,147 @@ +//===-- TargetThreadWindowsLive.cpp------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/Core/Log.h" +#include "lldb/Core/Logging.h" +#include "lldb/Core/State.h" +#include "lldb/Host/HostInfo.h" +#include "lldb/Host/HostNativeThreadBase.h" +#include "lldb/Host/windows/HostThreadWindows.h" +#include "lldb/Host/windows/windows.h" +#include "lldb/Target/RegisterContext.h" + +#include "TargetThreadWindowsLive.h" +#include "ProcessWindows.h" +#include "ProcessWindowsLog.h" +#include "UnwindLLDB.h" + +#if defined(_WIN64) +#include "x64/RegisterContextWindowsLive_x64.h" +#else +#include "x86/RegisterContextWindowsLive_x86.h" +#endif + +using namespace lldb; +using namespace lldb_private; + +TargetThreadWindowsLive::TargetThreadWindowsLive(ProcessWindows &process, const HostThread &thread) +    : TargetThreadWindows(process, thread) +    , m_host_thread(thread) +{ +} + +TargetThreadWindowsLive::~TargetThreadWindowsLive() +{ +    DestroyThread(); +} + +void +TargetThreadWindowsLive::RefreshStateAfterStop() +{ +    ::SuspendThread(m_host_thread.GetNativeThread().GetSystemHandle()); +    SetState(eStateStopped); +    GetRegisterContext()->InvalidateIfNeeded(false); +} + +void +TargetThreadWindowsLive::WillResume(lldb::StateType resume_state) +{ +} + +void +TargetThreadWindowsLive::DidStop() +{ +} + +RegisterContextSP +TargetThreadWindowsLive::GetRegisterContext() +{ +    if (!m_reg_context_sp) +        m_reg_context_sp = CreateRegisterContextForFrameIndex(0); + +    return m_reg_context_sp; +} + +RegisterContextSP +TargetThreadWindowsLive::CreateRegisterContextForFrame(StackFrame *frame) +{ +    return CreateRegisterContextForFrameIndex(frame->GetConcreteFrameIndex()); +} + +RegisterContextSP +TargetThreadWindowsLive::CreateRegisterContextForFrameIndex(uint32_t idx) +{ +    if (!m_reg_context_sp) +    { +        ArchSpec arch = HostInfo::GetArchitecture(); +        switch (arch.GetMachine()) +        { +            case llvm::Triple::x86: +#if defined(_WIN64) +                // FIXME: This is a Wow64 process, create a RegisterContextWindows_Wow64 +#else +                m_reg_context_sp.reset(new RegisterContextWindowsLive_x86(*this, idx)); +#endif +                break; +            case llvm::Triple::x86_64: +#if defined(_WIN64) +                m_reg_context_sp.reset(new RegisterContextWindowsLive_x64(*this, idx)); +#else +                // LLDB is 32-bit, but the target process is 64-bit.  We probably can't debug this. +#endif +            default: +                break; +        } +    } +    return m_reg_context_sp; +} + +bool +TargetThreadWindowsLive::CalculateStopInfo() +{ +    SetStopInfo(m_stop_info_sp); +    return true; +} + +Unwind * +TargetThreadWindowsLive::GetUnwinder() +{ +    // FIXME: Implement an unwinder based on the Windows unwinder exposed through DIA SDK. +    if (m_unwinder_ap.get() == NULL) +        m_unwinder_ap.reset(new UnwindLLDB(*this)); +    return m_unwinder_ap.get(); +} + +bool +TargetThreadWindowsLive::DoResume() +{ +    StateType resume_state = GetTemporaryResumeState(); +    StateType current_state = GetState(); +    if (resume_state == current_state) +        return true; + +    if (resume_state == eStateStepping) +    { +        uint32_t flags_index = GetRegisterContext()->ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FLAGS); +        uint64_t flags_value = GetRegisterContext()->ReadRegisterAsUnsigned(flags_index, 0); +        flags_value |= 0x100; // Set the trap flag on the CPU +        GetRegisterContext()->WriteRegisterFromUnsigned(flags_index, flags_value); +    } + +    if (resume_state == eStateStepping || resume_state == eStateRunning) +    { +        DWORD previous_suspend_count = 0; +        HANDLE thread_handle = m_host_thread.GetNativeThread().GetSystemHandle(); +        do +        { +            previous_suspend_count = ::ResumeThread(thread_handle); +        } while (previous_suspend_count > 0); +    } +    return true; +} diff --git a/source/Plugins/Process/Windows/Live/TargetThreadWindowsLive.h b/source/Plugins/Process/Windows/Live/TargetThreadWindowsLive.h new file mode 100644 index 000000000000..15262b9e9423 --- /dev/null +++ b/source/Plugins/Process/Windows/Live/TargetThreadWindowsLive.h @@ -0,0 +1,55 @@ +//===-- TargetThreadWindowsLive.h -------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_Plugins_Process_Windows_TargetThreadWindowsLive_H_ +#define liblldb_Plugins_Process_Windows_TargetThreadWindowsLive_H_ + +#include "lldb/lldb-forward.h" +#include "lldb/Host/HostThread.h" +#include "lldb/Target/Thread.h" + +#include "Plugins/Process/Windows/Common/TargetThreadWindows.h" + +namespace lldb_private +{ +class ProcessWindows; +class HostThread; +class StackFrame; + +class TargetThreadWindowsLive : public lldb_private::TargetThreadWindows +{ +  public: +    TargetThreadWindowsLive(ProcessWindows &process, const HostThread &thread); +    virtual ~TargetThreadWindowsLive(); + +    // lldb_private::Thread overrides +    void RefreshStateAfterStop() override; +    void WillResume(lldb::StateType resume_state) override; +    void DidStop() override; +    lldb::RegisterContextSP GetRegisterContext() override; +    lldb::RegisterContextSP CreateRegisterContextForFrame(StackFrame *frame) override; +    bool CalculateStopInfo() override; +    Unwind *GetUnwinder() override; + +    bool DoResume(); + +    HostThread +    GetHostThread() const +    { +        return m_host_thread; +    } + +  private: +    lldb::RegisterContextSP CreateRegisterContextForFrameIndex(uint32_t idx); + +    HostThread m_host_thread; +}; +} + +#endif diff --git a/source/Plugins/Process/Windows/Live/x64/RegisterContextWindowsLive_x64.cpp b/source/Plugins/Process/Windows/Live/x64/RegisterContextWindowsLive_x64.cpp new file mode 100644 index 000000000000..e74647ab68fb --- /dev/null +++ b/source/Plugins/Process/Windows/Live/x64/RegisterContextWindowsLive_x64.cpp @@ -0,0 +1,171 @@ +//===-- RegisterContextWindowsLive_x64.cpp ----------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/lldb-private-types.h" +#include "lldb/Core/Error.h" +#include "lldb/Core/RegisterValue.h" +#include "lldb/Host/windows/HostThreadWindows.h" +#include "lldb/Host/windows/windows.h" + +#include "lldb-x86-register-enums.h" +#include "RegisterContextWindowsLive_x64.h" +#include "TargetThreadWindows.h" + +#include "llvm/ADT/STLExtras.h" + +using namespace lldb; +using namespace lldb_private; + +RegisterContextWindowsLive_x64::RegisterContextWindowsLive_x64(Thread &thread, uint32_t concrete_frame_idx) +    : RegisterContextWindows_x64(thread, concrete_frame_idx) +{ +} + +RegisterContextWindowsLive_x64::~RegisterContextWindowsLive_x64() +{ +} + + +bool +RegisterContextWindowsLive_x64::ReadRegister(const RegisterInfo *reg_info, RegisterValue ®_value) +{ +    if (!CacheAllRegisterValues()) +        return false; + +    switch (reg_info->kinds[eRegisterKindLLDB]) +    { +        case lldb_rax_x86_64: +            reg_value.SetUInt64(m_context.Rax); +            break; +        case lldb_rbx_x86_64: +            reg_value.SetUInt64(m_context.Rbx); +            break; +        case lldb_rcx_x86_64: +            reg_value.SetUInt64(m_context.Rcx); +            break; +        case lldb_rdx_x86_64: +            reg_value.SetUInt64(m_context.Rdx); +            break; +        case lldb_rdi_x86_64: +            reg_value.SetUInt64(m_context.Rdi); +            break; +        case lldb_rsi_x86_64: +            reg_value.SetUInt64(m_context.Rsi); +            break; +        case lldb_r8_x86_64: +            reg_value.SetUInt64(m_context.R8); +            break; +        case lldb_r9_x86_64: +            reg_value.SetUInt64(m_context.R9); +            break; +        case lldb_r10_x86_64: +            reg_value.SetUInt64(m_context.R10); +            break; +        case lldb_r11_x86_64: +            reg_value.SetUInt64(m_context.R11); +            break; +        case lldb_r12_x86_64: +            reg_value.SetUInt64(m_context.R12); +            break; +        case lldb_r13_x86_64: +            reg_value.SetUInt64(m_context.R13); +            break; +        case lldb_r14_x86_64: +            reg_value.SetUInt64(m_context.R14); +            break; +        case lldb_r15_x86_64: +            reg_value.SetUInt64(m_context.R15); +            break; +        case lldb_rbp_x86_64: +            reg_value.SetUInt64(m_context.Rbp); +            break; +        case lldb_rsp_x86_64: +            reg_value.SetUInt64(m_context.Rsp); +            break; +        case lldb_rip_x86_64: +            reg_value.SetUInt64(m_context.Rip); +            break; +        case lldb_rflags_x86_64: +            reg_value.SetUInt64(m_context.EFlags); +            break; +    } +    return true; +} + +bool +RegisterContextWindowsLive_x64::WriteRegister(const RegisterInfo *reg_info, const RegisterValue ®_value) +{ +    // Since we cannot only write a single register value to the inferior, we need to make sure +    // our cached copy of the register values are fresh.  Otherwise when writing EAX, for example, +    // we may also overwrite some other register with a stale value. +    if (!CacheAllRegisterValues()) +        return false; + +    switch (reg_info->kinds[eRegisterKindLLDB]) +    { +        case lldb_rax_x86_64: +            m_context.Rax = reg_value.GetAsUInt64(); +            break; +        case lldb_rbx_x86_64: +            m_context.Rbx = reg_value.GetAsUInt64(); +            break; +        case lldb_rcx_x86_64: +            m_context.Rcx = reg_value.GetAsUInt64(); +            break; +        case lldb_rdx_x86_64: +            m_context.Rdx = reg_value.GetAsUInt64(); +            break; +        case lldb_rdi_x86_64: +            m_context.Rdi = reg_value.GetAsUInt64(); +            break; +        case lldb_rsi_x86_64: +            m_context.Rsi = reg_value.GetAsUInt64(); +            break; +        case lldb_r8_x86_64: +            m_context.R8 = reg_value.GetAsUInt64(); +            break; +        case lldb_r9_x86_64: +            m_context.R9 = reg_value.GetAsUInt64(); +            break; +        case lldb_r10_x86_64: +            m_context.R10 = reg_value.GetAsUInt64(); +            break; +        case lldb_r11_x86_64: +            m_context.R11 = reg_value.GetAsUInt64(); +            break; +        case lldb_r12_x86_64: +            m_context.R12 = reg_value.GetAsUInt64(); +            break; +        case lldb_r13_x86_64: +            m_context.R13 = reg_value.GetAsUInt64(); +            break; +        case lldb_r14_x86_64: +            m_context.R14 = reg_value.GetAsUInt64(); +            break; +        case lldb_r15_x86_64: +            m_context.R15 = reg_value.GetAsUInt64(); +            break; +        case lldb_rbp_x86_64: +            m_context.Rbp = reg_value.GetAsUInt64(); +            break; +        case lldb_rsp_x86_64: +            m_context.Rsp = reg_value.GetAsUInt64(); +            break; +        case lldb_rip_x86_64: +            m_context.Rip = reg_value.GetAsUInt64(); +            break; +        case lldb_rflags_x86_64: +            m_context.EFlags = reg_value.GetAsUInt64(); +            break; +    } + +    // Physically update the registers in the target process. +    TargetThreadWindows &wthread = static_cast<TargetThreadWindows &>(m_thread); +    return ::SetThreadContext(wthread.GetHostThread().GetNativeThread().GetSystemHandle(), &m_context); +} diff --git a/source/Plugins/Process/Windows/Live/x64/RegisterContextWindowsLive_x64.h b/source/Plugins/Process/Windows/Live/x64/RegisterContextWindowsLive_x64.h new file mode 100644 index 000000000000..bd250a994d3a --- /dev/null +++ b/source/Plugins/Process/Windows/Live/x64/RegisterContextWindowsLive_x64.h @@ -0,0 +1,40 @@ +//===-- RegisterContextWindowsLive_x64.h ------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_RegisterContextWindowsLive_x64_H_ +#define liblldb_RegisterContextWindowsLive_x64_H_ + +#include "lldb/lldb-forward.h" +#include "../../Common/x64/RegisterContextWindows_x64.h" + +namespace lldb_private +{ + +class Thread; + +class RegisterContextWindowsLive_x64 : public RegisterContextWindows_x64 +{ +  public: +    //------------------------------------------------------------------ +    // Constructors and Destructors +    //------------------------------------------------------------------ +    RegisterContextWindowsLive_x64(Thread &thread, uint32_t concrete_frame_idx); + +    virtual ~RegisterContextWindowsLive_x64(); + +    //------------------------------------------------------------------ +    // Subclasses must override these functions +    //------------------------------------------------------------------ +    bool ReadRegister(const RegisterInfo *reg_info, RegisterValue ®_value) override; + +    bool WriteRegister(const RegisterInfo *reg_info, const RegisterValue ®_value) override; +}; +} + +#endif // #ifndef liblldb_RegisterContextWindowsLive_x64_H_ diff --git a/source/Plugins/Process/Windows/Live/x86/RegisterContextWindowsLive_x86.cpp b/source/Plugins/Process/Windows/Live/x86/RegisterContextWindowsLive_x86.cpp new file mode 100644 index 000000000000..f2decc72d16d --- /dev/null +++ b/source/Plugins/Process/Windows/Live/x86/RegisterContextWindowsLive_x86.cpp @@ -0,0 +1,100 @@ +//===-- RegisterContextWindowsLive_x86.cpp ------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/lldb-private-types.h" +#include "lldb/Core/Error.h" +#include "lldb/Core/RegisterValue.h" +#include "lldb/Host/windows/HostThreadWindows.h" +#include "lldb/Host/windows/windows.h" + +#include "lldb-x86-register-enums.h" +#include "ProcessWindowsLog.h" +#include "RegisterContextWindowsLive_x86.h" +#include "TargetThreadWindows.h" + +#include "llvm/ADT/STLExtras.h" + +using namespace lldb; + +namespace lldb_private +{ + +RegisterContextWindowsLive_x86::RegisterContextWindowsLive_x86(Thread &thread, uint32_t concrete_frame_idx) +    : RegisterContextWindows_x86(thread, concrete_frame_idx) +{ +} + +RegisterContextWindowsLive_x86::~RegisterContextWindowsLive_x86() +{ +} + + +bool +RegisterContextWindowsLive_x86::WriteRegister(const RegisterInfo *reg_info, const RegisterValue ®_value) +{ +    // Since we cannot only write a single register value to the inferior, we need to make sure +    // our cached copy of the register values are fresh.  Otherwise when writing EAX, for example, +    // we may also overwrite some other register with a stale value. +    if (!CacheAllRegisterValues()) +        return false; + +    uint32_t reg = reg_info->kinds[eRegisterKindLLDB]; +    switch (reg) +    { +        case lldb_eax_i386: +            WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Write value 0x%x to EAX", reg_value.GetAsUInt32()); +            m_context.Eax = reg_value.GetAsUInt32(); +            break; +        case lldb_ebx_i386: +            WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Write value 0x%x to EBX", reg_value.GetAsUInt32()); +            m_context.Ebx = reg_value.GetAsUInt32(); +            break; +        case lldb_ecx_i386: +            WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Write value 0x%x to ECX", reg_value.GetAsUInt32()); +            m_context.Ecx = reg_value.GetAsUInt32(); +            break; +        case lldb_edx_i386: +            WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Write value 0x%x to EDX", reg_value.GetAsUInt32()); +            m_context.Edx = reg_value.GetAsUInt32(); +            break; +        case lldb_edi_i386: +            WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Write value 0x%x to EDI", reg_value.GetAsUInt32()); +            m_context.Edi = reg_value.GetAsUInt32(); +            break; +        case lldb_esi_i386: +            WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Write value 0x%x to ESI", reg_value.GetAsUInt32()); +            m_context.Esi = reg_value.GetAsUInt32(); +            break; +        case lldb_ebp_i386: +            WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Write value 0x%x to EBP", reg_value.GetAsUInt32()); +            m_context.Ebp = reg_value.GetAsUInt32(); +            break; +        case lldb_esp_i386: +            WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Write value 0x%x to ESP", reg_value.GetAsUInt32()); +            m_context.Esp = reg_value.GetAsUInt32(); +            break; +        case lldb_eip_i386: +            WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Write value 0x%x to EIP", reg_value.GetAsUInt32()); +            m_context.Eip = reg_value.GetAsUInt32(); +            break; +        case lldb_eflags_i386: +            WINLOG_IFALL(WINDOWS_LOG_REGISTERS, "Write value 0x%x to EFLAGS", reg_value.GetAsUInt32()); +            m_context.EFlags = reg_value.GetAsUInt32(); +            break; +        default: +            WINWARN_IFALL(WINDOWS_LOG_REGISTERS, "Write value 0x%x to unknown register %u", reg_value.GetAsUInt32(), +                          reg); +    } + +    // Physically update the registers in the target process. +    TargetThreadWindows &wthread = static_cast<TargetThreadWindows &>(m_thread); +    return ::SetThreadContext(wthread.GetHostThread().GetNativeThread().GetSystemHandle(), &m_context); +} + +}  // namespace lldb_private diff --git a/source/Plugins/Process/Windows/Live/x86/RegisterContextWindowsLive_x86.h b/source/Plugins/Process/Windows/Live/x86/RegisterContextWindowsLive_x86.h new file mode 100644 index 000000000000..9554f017408a --- /dev/null +++ b/source/Plugins/Process/Windows/Live/x86/RegisterContextWindowsLive_x86.h @@ -0,0 +1,36 @@ +//===-- RegisterContextWindowsLive_x86.h ------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_RegisterContextWindowsLive_x86_H_ +#define liblldb_RegisterContextWindowsLive_x86_H_ + +#include "lldb/lldb-forward.h" +#include "../../Common/x86/RegisterContextWindows_x86.h" + +namespace lldb_private +{ + +class Thread; + +class RegisterContextWindowsLive_x86 : public RegisterContextWindows_x86 +{ +  public: +    //------------------------------------------------------------------ +    // Constructors and Destructors +    //------------------------------------------------------------------ +    RegisterContextWindowsLive_x86(Thread &thread, uint32_t concrete_frame_idx); + +    virtual ~RegisterContextWindowsLive_x86(); + +    bool WriteRegister(const RegisterInfo *reg_info, const RegisterValue ®_value) override; +}; + +} + +#endif // #ifndef liblldb_RegisterContextWindowsLive_x86_H_ diff --git a/source/Plugins/Process/Windows/MiniDump/CMakeLists.txt b/source/Plugins/Process/Windows/MiniDump/CMakeLists.txt new file mode 100644 index 000000000000..b43246b00351 --- /dev/null +++ b/source/Plugins/Process/Windows/MiniDump/CMakeLists.txt @@ -0,0 +1,21 @@ +include_directories(../../Utility) +include_directories(../Common) + +set(PROC_WINDOWS_MINIDUMP_SOURCES +  ProcessWinMiniDump.cpp +  ThreadWinMiniDump.cpp +  ) + +if (CMAKE_SIZEOF_VOID_P EQUAL 4) +  set(PROC_WINDOWS_MINIDUMP_SOURCES ${PROC_WINDOWS_MINIDUMP_SOURCES} +    x86/RegisterContextWindowsMiniDump_x86.cpp +    ) +elseif (CMAKE_SIZEOF_VOID_P EQUAL 8) +  set(PROC_WINDOWS_MINIDUMP_SOURCES ${PROC_WINDOWS_MINIDUMP_SOURCES} +    x64/RegisterContextWindowsMiniDump_x64.cpp +    ) +endif() + +add_lldb_library(lldbPluginProcessWinMiniDump +  ${PROC_WINDOWS_MINIDUMP_SOURCES} +  ) diff --git a/source/Plugins/Process/Windows/MiniDump/ProcessWinMiniDump.cpp b/source/Plugins/Process/Windows/MiniDump/ProcessWinMiniDump.cpp new file mode 100644 index 000000000000..fbc96f085ed4 --- /dev/null +++ b/source/Plugins/Process/Windows/MiniDump/ProcessWinMiniDump.cpp @@ -0,0 +1,550 @@ +//===-- ProcessWinMiniDump.cpp ----------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "ProcessWinMiniDump.h" + +#include "lldb/Host/windows/windows.h" +#include <DbgHelp.h> + +#include <assert.h> +#include <stdlib.h> +#include <memory> +#include <mutex> + +#include "Plugins/DynamicLoader/Windows-DYLD/DynamicLoaderWindowsDYLD.h" +#include "lldb/Core/DataBufferHeap.h" +#include "lldb/Core/Log.h" +#include "lldb/Core/Module.h" +#include "lldb/Core/ModuleSpec.h" +#include "lldb/Core/PluginManager.h" +#include "lldb/Core/Section.h" +#include "lldb/Core/State.h" +#include "lldb/Target/DynamicLoader.h" +#include "lldb/Target/MemoryRegionInfo.h" +#include "lldb/Target/StopInfo.h" +#include "lldb/Target/Target.h" +#include "lldb/Target/UnixSignals.h" +#include "lldb/Utility/LLDBAssert.h" +#include "llvm/Support/ConvertUTF.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/raw_ostream.h" + +#include "ExceptionRecord.h" +#include "ThreadWinMiniDump.h" + +using namespace lldb_private; + +namespace +{ + +// Getting a string out of a mini dump is a chore.  You're usually given a +// relative virtual address (RVA), which points to a counted string that's in +// Windows Unicode (UTF-16).  This wrapper handles all the redirection and +// returns a UTF-8 copy of the string. +std::string +GetMiniDumpString(const void *base_addr, const RVA rva) +{ +    std::string result; +    if (!base_addr) +    { +        return result; +    } +    auto md_string = reinterpret_cast<const MINIDUMP_STRING *>(static_cast<const char *>(base_addr) + rva); +    auto source_start = reinterpret_cast<const UTF16 *>(md_string->Buffer); +    const auto source_length = ::wcslen(md_string->Buffer); +    const auto source_end = source_start + source_length; +    result.resize(4*source_length);  // worst case length +    auto result_start = reinterpret_cast<UTF8 *>(&result[0]); +    const auto result_end = result_start + result.size(); +    ConvertUTF16toUTF8(&source_start, source_end, &result_start, result_end, strictConversion); +    const auto result_size = std::distance(reinterpret_cast<UTF8 *>(&result[0]), result_start); +    result.resize(result_size);  // shrink to actual length +    return result; +} + +}  // anonymous namespace + +// Encapsulates the private data for ProcessWinMiniDump. +// TODO(amccarth):  Determine if we need a mutex for access. +class ProcessWinMiniDump::Data +{ +public: +    Data(); +    ~Data(); + +    FileSpec m_core_file; +    HANDLE m_dump_file;  // handle to the open minidump file +    HANDLE m_mapping;  // handle to the file mapping for the minidump file +    void * m_base_addr;  // base memory address of the minidump +    std::shared_ptr<ExceptionRecord> m_exception_sp; +}; + +ConstString +ProcessWinMiniDump::GetPluginNameStatic() +{ +    static ConstString g_name("win-minidump"); +    return g_name; +} + +const char * +ProcessWinMiniDump::GetPluginDescriptionStatic() +{ +    return "Windows minidump plug-in."; +} + +void +ProcessWinMiniDump::Terminate() +{ +    PluginManager::UnregisterPlugin(ProcessWinMiniDump::CreateInstance); +} + + +lldb::ProcessSP +ProcessWinMiniDump::CreateInstance(lldb::TargetSP target_sp, Listener &listener, const FileSpec *crash_file) +{ +    lldb::ProcessSP process_sp; +    if (crash_file) +    { +       process_sp.reset(new ProcessWinMiniDump(target_sp, listener, *crash_file)); +    } +    return process_sp; +} + +bool +ProcessWinMiniDump::CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_name) +{ +    // TODO(amccarth):  Eventually, this needs some actual logic. +    return true; +} + +ProcessWinMiniDump::ProcessWinMiniDump(lldb::TargetSP target_sp, Listener &listener, +                                       const FileSpec &core_file) : +    ProcessWindows(target_sp, listener), +    m_data_up(new Data) +{ +    m_data_up->m_core_file = core_file; +} + +ProcessWinMiniDump::~ProcessWinMiniDump() +{ +    Clear(); +    // We need to call finalize on the process before destroying ourselves +    // to make sure all of the broadcaster cleanup goes as planned. If we +    // destruct this class, then Process::~Process() might have problems +    // trying to fully destroy the broadcaster. +    Finalize(); +} + +ConstString +ProcessWinMiniDump::GetPluginName() +{ +    return GetPluginNameStatic(); +} + +uint32_t +ProcessWinMiniDump::GetPluginVersion() +{ +    return 1; +} + + +Error +ProcessWinMiniDump::DoLoadCore() +{ +    Error error; + +    error = MapMiniDumpIntoMemory(m_data_up->m_core_file.GetCString()); +    if (error.Fail()) +    { +        return error; +    } + +    GetTarget().SetArchitecture(DetermineArchitecture()); +    ReadMiscInfo();  // notably for process ID +    ReadModuleList(); +    ReadExceptionRecord(); + +    return error; + +} + +DynamicLoader * +ProcessWinMiniDump::GetDynamicLoader() +{ +    if (m_dyld_ap.get() == NULL) +        m_dyld_ap.reset (DynamicLoader::FindPlugin(this, DynamicLoaderWindowsDYLD::GetPluginNameStatic().GetCString())); +    return m_dyld_ap.get(); +} + +bool +ProcessWinMiniDump::UpdateThreadList(ThreadList &old_thread_list, ThreadList &new_thread_list) +{ +    size_t size = 0; +    auto thread_list_ptr = static_cast<const MINIDUMP_THREAD_LIST *>(FindDumpStream(ThreadListStream, &size)); +    if (thread_list_ptr) +    { +        const ULONG32 thread_count = thread_list_ptr->NumberOfThreads; +        for (ULONG32 i = 0; i < thread_count; ++i) { +            const auto &mini_dump_thread = thread_list_ptr->Threads[i]; +            auto thread_sp = std::make_shared<ThreadWinMiniDump>(*this, mini_dump_thread.ThreadId); +            if (mini_dump_thread.ThreadContext.DataSize >= sizeof(CONTEXT)) +            { +                const CONTEXT *context = reinterpret_cast<const CONTEXT *>(static_cast<const char *>(m_data_up->m_base_addr) + mini_dump_thread.ThreadContext.Rva); +                thread_sp->SetContext(context); +            } +            new_thread_list.AddThread(thread_sp); +        } +    } + +    return new_thread_list.GetSize(false) > 0; +} + +void +ProcessWinMiniDump::RefreshStateAfterStop() +{ +    if (!m_data_up) return; +    if (!m_data_up->m_exception_sp) return; + +    auto active_exception = m_data_up->m_exception_sp; +    std::string desc; +    llvm::raw_string_ostream desc_stream(desc); +    desc_stream << "Exception " +                << llvm::format_hex(active_exception->GetExceptionCode(), 8) +                << " encountered at address " +                << llvm::format_hex(active_exception->GetExceptionAddress(), 8); +    m_thread_list.SetSelectedThreadByID(active_exception->GetThreadID()); +    auto stop_thread = m_thread_list.GetSelectedThread(); +    auto stop_info = StopInfo::CreateStopReasonWithException(*stop_thread, desc_stream.str().c_str()); +    stop_thread->SetStopInfo(stop_info); +} + +Error +ProcessWinMiniDump::DoDestroy() +{ +    return Error(); +} + +bool +ProcessWinMiniDump::IsAlive() +{ +    return true; +} + +bool +ProcessWinMiniDump::WarnBeforeDetach () const +{ +    // Since this is post-mortem debugging, there's no need to warn the user +    // that quitting the debugger will terminate the process. +    return false; +} + +size_t +ProcessWinMiniDump::ReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error) +{ +    // Don't allow the caching that lldb_private::Process::ReadMemory does +    // since we have it all cached our our dump file anyway. +    return DoReadMemory(addr, buf, size, error); +} + +size_t +ProcessWinMiniDump::DoReadMemory(lldb::addr_t addr, void *buf, size_t size, Error &error) +{ +    // I don't have a sense of how frequently this is called or how many memory +    // ranges a mini dump typically has, so I'm not sure if searching for the +    // appropriate range linearly each time is stupid.  Perhaps we should build +    // an index for faster lookups. +    Range range = {0}; +    if (!FindMemoryRange(addr, &range)) +    { +        return 0; +    } + +    // There's at least some overlap between the beginning of the desired range +    // (addr) and the current range.  Figure out where the overlap begins and +    // how much overlap there is, then copy it to the destination buffer. +    lldbassert(range.start <= addr); +    const size_t offset = addr - range.start; +    lldbassert(offset < range.size); +    const size_t overlap = std::min(size, range.size - offset); +    std::memcpy(buf, range.ptr + offset, overlap); +    return overlap; +} + +Error +ProcessWinMiniDump::GetMemoryRegionInfo(lldb::addr_t load_addr, lldb_private::MemoryRegionInfo &info) +{ +    Error error; +    size_t size; +    const auto list = reinterpret_cast<const MINIDUMP_MEMORY_INFO_LIST *>(FindDumpStream(MemoryInfoListStream, &size)); +    if (list == nullptr || size < sizeof(MINIDUMP_MEMORY_INFO_LIST)) +    { +        error.SetErrorString("the mini dump contains no memory range information"); +        return error; +    } + +    if (list->SizeOfEntry < sizeof(MINIDUMP_MEMORY_INFO)) +    { +        error.SetErrorString("the entries in the mini dump memory info list are smaller than expected"); +        return error; +    } + +    if (size < list->SizeOfHeader + list->SizeOfEntry * list->NumberOfEntries) +    { +        error.SetErrorString("the mini dump memory info list is incomplete"); +        return error; +    } + +    for (int i = 0; i < list->NumberOfEntries; ++i) +    { +        const auto entry = reinterpret_cast<const MINIDUMP_MEMORY_INFO *>(reinterpret_cast<const char *>(list) + +                                                                          list->SizeOfHeader + i * list->SizeOfEntry); +        const auto head = entry->BaseAddress; +        const auto tail = head + entry->RegionSize; +        if (head <= load_addr && load_addr < tail) +        { +            info.SetReadable(IsPageReadable(entry->Protect) ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); +            info.SetWritable(IsPageWritable(entry->Protect) ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); +            info.SetExecutable(IsPageExecutable(entry->Protect) ? MemoryRegionInfo::eYes : MemoryRegionInfo::eNo); +            return error; +        } +    } +    // Note that the memory info list doesn't seem to contain ranges in kernel space, +    // so if you're walking a stack that has kernel frames, the stack may appear +    // truncated. +    error.SetErrorString("address is not in a known range"); +    return error; +} + +void +ProcessWinMiniDump::Clear() +{ +    m_thread_list.Clear(); +} + +void +ProcessWinMiniDump::Initialize() +{ +    static std::once_flag g_once_flag; + +    std::call_once(g_once_flag, []() +    { +        PluginManager::RegisterPlugin(GetPluginNameStatic(), +                                      GetPluginDescriptionStatic(), +                                      CreateInstance); +    }); +} + +ArchSpec +ProcessWinMiniDump::GetArchitecture() +{ +    // TODO +    return ArchSpec(); +} + + +ProcessWinMiniDump::Data::Data() : +    m_dump_file(INVALID_HANDLE_VALUE), +    m_mapping(NULL), +    m_base_addr(nullptr) +{ +} + +ProcessWinMiniDump::Data::~Data() +{ +    if (m_base_addr) +    { +        ::UnmapViewOfFile(m_base_addr); +        m_base_addr = nullptr; +    } +    if (m_mapping) +    { +        ::CloseHandle(m_mapping); +        m_mapping = NULL; +    } +    if (m_dump_file != INVALID_HANDLE_VALUE) +    { +        ::CloseHandle(m_dump_file); +        m_dump_file = INVALID_HANDLE_VALUE; +    } +} + +bool +ProcessWinMiniDump::FindMemoryRange(lldb::addr_t addr, Range *range_out) const +{ +    size_t stream_size = 0; +    auto mem_list_stream = static_cast<const MINIDUMP_MEMORY_LIST *>(FindDumpStream(MemoryListStream, &stream_size)); +    if (mem_list_stream) +    { +        for (ULONG32 i = 0; i < mem_list_stream->NumberOfMemoryRanges; ++i) { +            const MINIDUMP_MEMORY_DESCRIPTOR &mem_desc = mem_list_stream->MemoryRanges[i]; +            const MINIDUMP_LOCATION_DESCRIPTOR &loc_desc = mem_desc.Memory; +            const lldb::addr_t range_start = mem_desc.StartOfMemoryRange; +            const size_t range_size = loc_desc.DataSize; +            if (range_start <= addr && addr < range_start + range_size) +            { +                range_out->start = range_start; +                range_out->size = range_size; +                range_out->ptr = reinterpret_cast<const uint8_t *>(m_data_up->m_base_addr) + loc_desc.Rva; +                return true; +            } +        } +    } + +    // Some mini dumps have a Memory64ListStream that captures all the heap +    // memory.  We can't exactly use the same loop as above, because the mini +    // dump uses slightly different data structures to describe those. +    auto mem_list64_stream = static_cast<const MINIDUMP_MEMORY64_LIST *>(FindDumpStream(Memory64ListStream, &stream_size)); +    if (mem_list64_stream) +    { +        size_t base_rva = mem_list64_stream->BaseRva; +        for (ULONG32 i = 0; i < mem_list64_stream->NumberOfMemoryRanges; ++i) { +            const MINIDUMP_MEMORY_DESCRIPTOR64 &mem_desc = mem_list64_stream->MemoryRanges[i]; +            const lldb::addr_t range_start = mem_desc.StartOfMemoryRange; +            const size_t range_size = mem_desc.DataSize; +            if (range_start <= addr && addr < range_start + range_size) +            { +                range_out->start = range_start; +                range_out->size = range_size; +                range_out->ptr = reinterpret_cast<const uint8_t *>(m_data_up->m_base_addr) + base_rva; +                return true; +            } +            base_rva += range_size; +        } +    } + +    return false; +} + + +Error +ProcessWinMiniDump::MapMiniDumpIntoMemory(const char *file) +{ +    Error error; + +    m_data_up->m_dump_file = ::CreateFile(file, GENERIC_READ, FILE_SHARE_READ, +                                          NULL, OPEN_EXISTING, +                                          FILE_ATTRIBUTE_NORMAL, NULL); +    if (m_data_up->m_dump_file == INVALID_HANDLE_VALUE) +    { +        error.SetError(::GetLastError(), lldb::eErrorTypeWin32); +        return error; +    } + +    m_data_up->m_mapping = ::CreateFileMapping(m_data_up->m_dump_file, NULL, +                                               PAGE_READONLY, 0, 0, NULL); +    if (m_data_up->m_mapping == NULL) +    { +        error.SetError(::GetLastError(), lldb::eErrorTypeWin32); +        return error; +    } + +    m_data_up->m_base_addr = ::MapViewOfFile(m_data_up->m_mapping, FILE_MAP_READ, 0, 0, 0); +    if (m_data_up->m_base_addr == NULL) +    { +        error.SetError(::GetLastError(), lldb::eErrorTypeWin32); +        return error; +    } + +    return error; +} + + +ArchSpec +ProcessWinMiniDump::DetermineArchitecture() +{ +    size_t size = 0; +    auto system_info_ptr = static_cast<const MINIDUMP_SYSTEM_INFO *>(FindDumpStream(SystemInfoStream, &size)); +    if (system_info_ptr) +    { +        switch (system_info_ptr->ProcessorArchitecture) +        { +        case PROCESSOR_ARCHITECTURE_INTEL: +            return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_I386, LLDB_INVALID_CPUTYPE); +        case PROCESSOR_ARCHITECTURE_AMD64: +            return ArchSpec(eArchTypeCOFF, IMAGE_FILE_MACHINE_AMD64, LLDB_INVALID_CPUTYPE); +        default: +            break; +        } +    } + +    return ArchSpec();  // invalid or unknown +} + +void +ProcessWinMiniDump::ReadExceptionRecord() +{ +    size_t size = 0; +    auto exception_stream_ptr = static_cast<MINIDUMP_EXCEPTION_STREAM*>(FindDumpStream(ExceptionStream, &size)); +    if (exception_stream_ptr) +    { +        m_data_up->m_exception_sp.reset(new ExceptionRecord(exception_stream_ptr->ExceptionRecord, exception_stream_ptr->ThreadId)); +    } +} + +void +ProcessWinMiniDump::ReadMiscInfo() +{ +    size_t size = 0; +    const auto misc_info_ptr = static_cast<MINIDUMP_MISC_INFO*>(FindDumpStream(MiscInfoStream, &size)); +    if (!misc_info_ptr || size < sizeof(MINIDUMP_MISC_INFO)) { +        return; +    } + +    if ((misc_info_ptr->Flags1 & MINIDUMP_MISC1_PROCESS_ID) != 0) { +        // This misc info record has the process ID. +        SetID(misc_info_ptr->ProcessId); +    } +} + +void +ProcessWinMiniDump::ReadModuleList() +{ +    size_t size = 0; +    auto module_list_ptr = static_cast<MINIDUMP_MODULE_LIST*>(FindDumpStream(ModuleListStream, &size)); +    if (!module_list_ptr || module_list_ptr->NumberOfModules == 0) +    { +        return; +    } + +    for (ULONG32 i = 0; i < module_list_ptr->NumberOfModules; ++i) +    { +        const auto &module = module_list_ptr->Modules[i]; +        const auto file_name = GetMiniDumpString(m_data_up->m_base_addr, module.ModuleNameRva); +        ModuleSpec module_spec = FileSpec(file_name, true); + +        lldb::ModuleSP module_sp = GetTarget().GetSharedModule(module_spec); +        if (!module_sp) +        { +            continue; +        } +        bool load_addr_changed = false; +        module_sp->SetLoadAddress(GetTarget(), module.BaseOfImage, false, load_addr_changed); +    } +} + +void * +ProcessWinMiniDump::FindDumpStream(unsigned stream_number, size_t *size_out) const +{ +    void *stream = nullptr; +    *size_out = 0; + +    assert(m_data_up != nullptr); +    assert(m_data_up->m_base_addr != 0); + +    MINIDUMP_DIRECTORY *dir = nullptr; +    if (::MiniDumpReadDumpStream(m_data_up->m_base_addr, stream_number, &dir, nullptr, nullptr) && +        dir != nullptr && dir->Location.DataSize > 0) +    { +        assert(dir->StreamType == stream_number); +        *size_out = dir->Location.DataSize; +        stream = static_cast<void*>(static_cast<char*>(m_data_up->m_base_addr) + dir->Location.Rva); +    } + +    return stream; +} diff --git a/source/Plugins/Process/Windows/MiniDump/ProcessWinMiniDump.h b/source/Plugins/Process/Windows/MiniDump/ProcessWinMiniDump.h new file mode 100644 index 000000000000..12864be37127 --- /dev/null +++ b/source/Plugins/Process/Windows/MiniDump/ProcessWinMiniDump.h @@ -0,0 +1,140 @@ +//===-- ProcessWinMiniDump.h ------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_ProcessWinMiniDump_h_ +#define liblldb_ProcessWinMiniDump_h_ + +#include <list> +#include <vector> + +#include "lldb/Core/ConstString.h" +#include "lldb/Core/Error.h" +#include "lldb/Target/Process.h" + +#include "Plugins/Process/Windows/Common/ProcessWindows.h" + +struct ThreadData; + +class ProcessWinMiniDump : public lldb_private::ProcessWindows +{ +  public: +    static lldb::ProcessSP +    CreateInstance (lldb::TargetSP target_sp, +                    lldb_private::Listener &listener, +                    const lldb_private::FileSpec *crash_file_path); + +    static void +    Initialize(); + +    static void +    Terminate(); + +    static lldb_private::ConstString +    GetPluginNameStatic(); + +    static const char * +    GetPluginDescriptionStatic(); + +    ProcessWinMiniDump(lldb::TargetSP target_sp, +                       lldb_private::Listener &listener, +                       const lldb_private::FileSpec &core_file); + +    virtual +    ~ProcessWinMiniDump(); + +    bool +    CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_name) override; + +    lldb_private::Error +    DoLoadCore() override; + +    lldb_private::DynamicLoader * +    GetDynamicLoader() override; + +    lldb_private::ConstString +    GetPluginName() override; + +    uint32_t +    GetPluginVersion() override; + +    lldb_private::Error +    DoDestroy() override; + +    void +    RefreshStateAfterStop() override; + +    bool +    IsAlive() override; + +    bool +    WarnBeforeDetach () const override; + +    size_t +    ReadMemory(lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error) override; + +    size_t +    DoReadMemory(lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error) override; + +    lldb_private::ArchSpec +    GetArchitecture(); + +    lldb_private::Error +    GetMemoryRegionInfo(lldb::addr_t load_addr, lldb_private::MemoryRegionInfo &range_info) override; + +  protected: +    void +    Clear(); + +    bool +    UpdateThreadList(lldb_private::ThreadList &old_thread_list, +                     lldb_private::ThreadList &new_thread_list) override; + +  private: +    // Describes a range of memory captured in the mini dump. +    struct Range { +      lldb::addr_t start;  // virtual address of the beginning of the range +      size_t size;         // size of the range in bytes +      const uint8_t *ptr;  // absolute pointer to the first byte of the range +    }; + +    // If the mini dump has a memory range that contains the desired address, it +    // returns true with the details of the range in *range_out.  Otherwise, it +    // returns false. +    bool +    FindMemoryRange(lldb::addr_t addr, Range *range_out) const; + +    lldb_private::Error +    MapMiniDumpIntoMemory(const char *file); + +    lldb_private::ArchSpec +    DetermineArchitecture(); + +    void +    ReadExceptionRecord(); + +    void +    ReadMiscInfo(); + +    void +    ReadModuleList(); + +    // A thin wrapper around WinAPI's MiniDumpReadDumpStream to avoid redundant +    // checks.  If there's a failure (e.g., if the requested stream doesn't exist), +    // the function returns nullptr and sets *size_out to 0. +    void * +    FindDumpStream(unsigned stream_number, size_t *size_out) const; + +    // Isolate the data to keep Windows-specific types out of this header.  Can't +    // use the typical pimpl idiom because the implementation of this class also +    // needs access to public and protected members of the base class. +    class Data; +    std::unique_ptr<Data> m_data_up; +}; + +#endif  // liblldb_ProcessWinMiniDump_h_ diff --git a/source/Plugins/Process/Windows/MiniDump/ThreadWinMiniDump.cpp b/source/Plugins/Process/Windows/MiniDump/ThreadWinMiniDump.cpp new file mode 100644 index 000000000000..ddcd15b1ae12 --- /dev/null +++ b/source/Plugins/Process/Windows/MiniDump/ThreadWinMiniDump.cpp @@ -0,0 +1,104 @@ +//===-- ThreadWinMiniDump.cpp -----------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "ThreadWinMiniDump.h" + +#include "lldb/Host/HostInfo.h" +#include "lldb/Host/windows/windows.h" +#include <DbgHelp.h> + +#include "ProcessWinMiniDump.h" +#if defined(_WIN64) +#include "x64/RegisterContextWindowsMiniDump_x64.h" +#else +#include "x86/RegisterContextWindowsMiniDump_x86.h" +#endif + +using namespace lldb; +using namespace lldb_private; + +// This is a minimal implementation in order to get something running.  It will +// be fleshed out as more mini-dump functionality is added. + +class ThreadWinMiniDump::Data { +  public: +    Data() : m_context(nullptr) {} +    const CONTEXT *m_context; +}; + +ThreadWinMiniDump::ThreadWinMiniDump(lldb_private::Process &process, lldb::tid_t tid) : +    Thread(process, tid), +    m_data(new Data) +{ +} + +ThreadWinMiniDump::~ThreadWinMiniDump() +{ +} + +void +ThreadWinMiniDump::RefreshStateAfterStop() +{ +} + +lldb::RegisterContextSP +ThreadWinMiniDump::GetRegisterContext() +{ +    if (m_reg_context_sp.get() == NULL) { +        m_reg_context_sp = CreateRegisterContextForFrame (NULL); +    } +    return m_reg_context_sp; +} + +lldb::RegisterContextSP +ThreadWinMiniDump::CreateRegisterContextForFrame(lldb_private::StackFrame *frame) +{ +    const uint32_t concrete_frame_idx = (frame) ? frame->GetConcreteFrameIndex() : 0; +    RegisterContextSP reg_ctx_sp; +    ArchSpec arch = HostInfo::GetArchitecture(); +    switch (arch.GetMachine()) +    { +        case llvm::Triple::x86: +#if defined(_WIN64) +            // FIXME: This is a Wow64 process, create a RegisterContextWindows_Wow64 +#else +            reg_ctx_sp.reset(new RegisterContextWindowsMiniDump_x86(*this, concrete_frame_idx, m_data->m_context)); +#endif +            break; +        case llvm::Triple::x86_64: +#if defined(_WIN64) +            reg_ctx_sp.reset(new RegisterContextWindowsMiniDump_x64(*this, concrete_frame_idx, m_data->m_context)); +#else +            // LLDB is 32-bit, but the target process is 64-bit.  We probably can't debug this. +#endif +        default: +            break; +    } +    return reg_ctx_sp; +} + +void +ThreadWinMiniDump::ClearStackFrames() +{ +} + +void +ThreadWinMiniDump::SetContext(const void *context) +{ +    if (m_data) +    { +        m_data->m_context = static_cast<const CONTEXT *>(context); +    } +} + +bool +ThreadWinMiniDump::CalculateStopInfo() +{ +    return false; +} diff --git a/source/Plugins/Process/Windows/MiniDump/ThreadWinMiniDump.h b/source/Plugins/Process/Windows/MiniDump/ThreadWinMiniDump.h new file mode 100644 index 000000000000..c78925422102 --- /dev/null +++ b/source/Plugins/Process/Windows/MiniDump/ThreadWinMiniDump.h @@ -0,0 +1,49 @@ +//===-- ThreadWinMiniDump.h -------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_ThreadWinMiniDump_h_ +#define liblldb_ThreadWinMiniDump_h_ + +#include <string> + +#include "lldb/Core/DataExtractor.h" +#include "lldb/Target/Thread.h" + +class ThreadWinMiniDump : public lldb_private::Thread +{ +public: +    ThreadWinMiniDump(lldb_private::Process &process, lldb::tid_t tid); + +    virtual +    ~ThreadWinMiniDump(); + +    void +    RefreshStateAfterStop() override; + +    lldb::RegisterContextSP +    GetRegisterContext() override; + +    lldb::RegisterContextSP +    CreateRegisterContextForFrame(lldb_private::StackFrame *frame) override; + +    void +    ClearStackFrames() override; + +    void +    SetContext(const void *context); + +protected: +    lldb::RegisterContextSP m_reg_context_sp; +    class Data; +    std::unique_ptr<Data> m_data;  // for WinAPI-specific data + +    bool CalculateStopInfo() override; +}; + +#endif diff --git a/source/Plugins/Process/Windows/MiniDump/x64/RegisterContextWindowsMiniDump_x64.cpp b/source/Plugins/Process/Windows/MiniDump/x64/RegisterContextWindowsMiniDump_x64.cpp new file mode 100644 index 000000000000..41d9195f1eed --- /dev/null +++ b/source/Plugins/Process/Windows/MiniDump/x64/RegisterContextWindowsMiniDump_x64.cpp @@ -0,0 +1,47 @@ +//===-- RegisterContextWindowsMiniDump_x64.cpp ------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/lldb-private-types.h" +#include "lldb/Host/windows/windows.h" + +#include "RegisterContextWindowsMiniDump_x64.h" + +using namespace lldb; + +namespace lldb_private +{ + +RegisterContextWindowsMiniDump_x64::RegisterContextWindowsMiniDump_x64(Thread &thread, uint32_t concrete_frame_idx, const CONTEXT *context) +    : RegisterContextWindows_x64(thread, concrete_frame_idx) +{ +    if (context) +    { +        m_context = *context; +        m_context_stale = false; +    } +} + +RegisterContextWindowsMiniDump_x64::~RegisterContextWindowsMiniDump_x64() +{ +} + +bool +RegisterContextWindowsMiniDump_x64::WriteRegister(const RegisterInfo * /* reg_info */, const RegisterValue & /* reg_value */) +{ +    return false; +} + +bool +RegisterContextWindowsMiniDump_x64::CacheAllRegisterValues() +{ +    // Since this is post-mortem debugging, we either have the context or we don't. +    return !m_context_stale; +} + +}  // namespace lldb_private diff --git a/source/Plugins/Process/Windows/MiniDump/x64/RegisterContextWindowsMiniDump_x64.h b/source/Plugins/Process/Windows/MiniDump/x64/RegisterContextWindowsMiniDump_x64.h new file mode 100644 index 000000000000..86d58046113f --- /dev/null +++ b/source/Plugins/Process/Windows/MiniDump/x64/RegisterContextWindowsMiniDump_x64.h @@ -0,0 +1,36 @@ +//===-- RegisterContextWindowsMiniDump_x64.h --------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_RegisterContextWindowsMiniDump_x64_H_ +#define liblldb_RegisterContextWindowsMiniDump_x64_H_ + +#include "lldb/lldb-forward.h" +#include "Plugins/Process/Windows/Common/x64/RegisterContextWindows_x64.h" + +namespace lldb_private +{ + +class Thread; + +class RegisterContextWindowsMiniDump_x64 : public RegisterContextWindows_x64 +{ +  public: +    RegisterContextWindowsMiniDump_x64(Thread &thread, uint32_t concrete_frame_idx, const CONTEXT *context); + +    virtual ~RegisterContextWindowsMiniDump_x64(); + +    bool WriteRegister(const RegisterInfo *reg_info, const RegisterValue ®_value) override; + +  protected: +    bool CacheAllRegisterValues() override; +}; + +} + +#endif // #ifndef liblldb_RegisterContextWindowsMiniDump_x64_H_ diff --git a/source/Plugins/Process/Windows/MiniDump/x86/RegisterContextWindowsMiniDump_x86.cpp b/source/Plugins/Process/Windows/MiniDump/x86/RegisterContextWindowsMiniDump_x86.cpp new file mode 100644 index 000000000000..2c8a069c5376 --- /dev/null +++ b/source/Plugins/Process/Windows/MiniDump/x86/RegisterContextWindowsMiniDump_x86.cpp @@ -0,0 +1,47 @@ +//===-- RegisterContextWindowsMiniDump_x86.cpp ------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/lldb-private-types.h" +#include "lldb/Host/windows/windows.h" + +#include "RegisterContextWindowsMiniDump_x86.h" + +using namespace lldb; + +namespace lldb_private +{ + +RegisterContextWindowsMiniDump_x86::RegisterContextWindowsMiniDump_x86(Thread &thread, uint32_t concrete_frame_idx, const CONTEXT *context) +    : RegisterContextWindows_x86(thread, concrete_frame_idx) +{ +    if (context) +    { +        m_context = *context; +        m_context_stale = false; +    } +} + +RegisterContextWindowsMiniDump_x86::~RegisterContextWindowsMiniDump_x86() +{ +} + +bool +RegisterContextWindowsMiniDump_x86::WriteRegister(const RegisterInfo * /* reg_info */, const RegisterValue & /* reg_value */) +{ +    return false; +} + +bool +RegisterContextWindowsMiniDump_x86::CacheAllRegisterValues() +{ +    // Since this is post-mortem debugging, we either have the context or we don't. +    return !m_context_stale; +} + +}  // namespace lldb_private diff --git a/source/Plugins/Process/Windows/MiniDump/x86/RegisterContextWindowsMiniDump_x86.h b/source/Plugins/Process/Windows/MiniDump/x86/RegisterContextWindowsMiniDump_x86.h new file mode 100644 index 000000000000..d36e0cfd9e7b --- /dev/null +++ b/source/Plugins/Process/Windows/MiniDump/x86/RegisterContextWindowsMiniDump_x86.h @@ -0,0 +1,36 @@ +//===-- RegisterContextWindowsMiniDump_x86.h ------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_RegisterContextWindowsMiniDump_x86_H_ +#define liblldb_RegisterContextWindowsMiniDump_x86_H_ + +#include "lldb/lldb-forward.h" +#include "Plugins/Process/Windows/Common/x86/RegisterContextWindows_x86.h" + +namespace lldb_private +{ + +class Thread; + +class RegisterContextWindowsMiniDump_x86 : public RegisterContextWindows_x86 +{ +  public: +    RegisterContextWindowsMiniDump_x86(Thread &thread, uint32_t concrete_frame_idx, const CONTEXT *context); + +    virtual ~RegisterContextWindowsMiniDump_x86(); + +    bool WriteRegister(const RegisterInfo *reg_info, const RegisterValue ®_value) override; + +  protected: +    bool CacheAllRegisterValues() override; +}; + +} + +#endif // #ifndef liblldb_RegisterContextWindowsMiniDump_x86_H_ diff --git a/source/Plugins/Process/elf-core/CMakeLists.txt b/source/Plugins/Process/elf-core/CMakeLists.txt new file mode 100644 index 000000000000..1a4dd7e9d333 --- /dev/null +++ b/source/Plugins/Process/elf-core/CMakeLists.txt @@ -0,0 +1,11 @@ +include_directories(../Utility) + +add_lldb_library(lldbPluginProcessElfCore +  ProcessElfCore.cpp +  ThreadElfCore.cpp +  RegisterContextPOSIXCore_arm.cpp +  RegisterContextPOSIXCore_arm64.cpp +  RegisterContextPOSIXCore_mips64.cpp +  RegisterContextPOSIXCore_powerpc.cpp +  RegisterContextPOSIXCore_x86_64.cpp +  ) diff --git a/source/Plugins/Process/elf-core/Makefile b/source/Plugins/Process/elf-core/Makefile new file mode 100644 index 000000000000..8c5b3b800f5a --- /dev/null +++ b/source/Plugins/Process/elf-core/Makefile @@ -0,0 +1,14 @@ +##===- source/Plugins/Process/elf-core/Makefile -----------*- Makefile -*-===## +# +#                     The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LLDB_LEVEL := ../../../.. +LIBRARYNAME := lldbPluginProcessElfCore +BUILD_ARCHIVE = 1 + +include $(LLDB_LEVEL)/Makefile diff --git a/source/Plugins/Process/gdb-remote/CMakeLists.txt b/source/Plugins/Process/gdb-remote/CMakeLists.txt new file mode 100644 index 000000000000..8dbfa453f2c2 --- /dev/null +++ b/source/Plugins/Process/gdb-remote/CMakeLists.txt @@ -0,0 +1,16 @@ +if (CMAKE_SYSTEM_NAME MATCHES "Darwin") +  include_directories(${LIBXML2_INCLUDE_DIR}) +endif() + +add_lldb_library(lldbPluginProcessGDBRemote +  GDBRemoteCommunication.cpp +  GDBRemoteCommunicationClient.cpp +  GDBRemoteCommunicationServer.cpp +  GDBRemoteCommunicationServerCommon.cpp +  GDBRemoteCommunicationServerLLGS.cpp +  GDBRemoteCommunicationServerPlatform.cpp +  GDBRemoteRegisterContext.cpp +  ProcessGDBRemote.cpp +  ProcessGDBRemoteLog.cpp +  ThreadGDBRemote.cpp +  ) diff --git a/source/Plugins/Process/gdb-remote/Makefile b/source/Plugins/Process/gdb-remote/Makefile new file mode 100644 index 000000000000..8a9b61077875 --- /dev/null +++ b/source/Plugins/Process/gdb-remote/Makefile @@ -0,0 +1,14 @@ +##===- source/Plugins/Process/gdb-remote/Makefile -------------*- Makefile -*-===## +#  +#                     The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +#  +##===----------------------------------------------------------------------===## + +LLDB_LEVEL := ../../../.. +LIBRARYNAME := lldbPluginProcessGDBRemote +BUILD_ARCHIVE = 1 + +include $(LLDB_LEVEL)/Makefile diff --git a/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp b/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp index 2e7a5b5384f4..be380a442e3c 100644 --- a/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp +++ b/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp @@ -109,35 +109,35 @@ namespace {          { "target-definition-file" , OptionValue::eTypeFileSpec , true, 0 , NULL, NULL, "The file that provides the description for remote target registers." },          {  NULL            , OptionValue::eTypeInvalid, false, 0, NULL, NULL, NULL  }      }; -     +      enum      {          ePropertyPacketTimeout,          ePropertyTargetDefinitionFile      }; -     +      class PluginProperties : public Properties      {      public: -         +          static ConstString          GetSettingName ()          {              return ProcessGDBRemote::GetPluginNameStatic();          } -         +          PluginProperties() :          Properties ()          {              m_collection_sp.reset (new OptionValueProperties(GetSettingName()));              m_collection_sp->Initialize(g_properties);          } -         +          virtual          ~PluginProperties()          {          } -         +          uint64_t          GetPacketTimeout()          { @@ -159,9 +159,9 @@ namespace {              return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);          }      }; -     +      typedef std::shared_ptr<PluginProperties> ProcessKDPPropertiesSP; -     +      static const ProcessKDPPropertiesSP &      GetGlobalPluginProperties()      { @@ -170,7 +170,7 @@ namespace {              g_settings_sp.reset (new PluginProperties ());          return g_settings_sp;      } -     +  } // anonymous namespace end  class ProcessGDBRemote::GDBLoadedModuleInfoList @@ -446,7 +446,7 @@ ProcessGDBRemote::~ProcessGDBRemote()      // destruct this class, then Process::~Process() might have problems      // trying to fully destroy the broadcaster.      Finalize(); -     +      // The general Finalize is going to try to destroy the process and that SHOULD      // shut down the async thread.  However, if we don't kill it it will get stranded and      // its connection will go away so when it wakes up it will crash.  So kill it for sure here. @@ -587,7 +587,7 @@ ProcessGDBRemote::BuildDynamicRegisterInfo (bool force)          GetGlobalPluginProperties()->SetPacketTimeout(host_packet_timeout);      } -    // Register info search order:  +    // Register info search order:      //     1 - Use the target definition python file if one is specified.      //     2 - If the target definition doesn't have any of the info from the target.xml (registers) then proceed to read the target.xml.      //     3 - Fall back on the qRegisterInfo packets. @@ -614,12 +614,12 @@ ProcessGDBRemote::BuildDynamicRegisterInfo (bool force)      if (GetGDBServerRegisterInfo ())          return; -     +      char packet[128];      uint32_t reg_offset = 0;      uint32_t reg_num = 0;      for (StringExtractorGDBRemote::ResponseType response_type = StringExtractorGDBRemote::eResponse; -         response_type == StringExtractorGDBRemote::eResponse;  +         response_type == StringExtractorGDBRemote::eResponse;           ++reg_num)      {          const int packet_len = ::snprintf (packet, sizeof(packet), "qRegisterInfo%x", reg_num); @@ -831,7 +831,7 @@ ProcessGDBRemote::DoConnectRemote (Stream *strm, const char *remote_url)  {      Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));      Error error (WillLaunchOrAttach ()); -     +      if (error.Fail())          return error; @@ -845,7 +845,7 @@ ProcessGDBRemote::DoConnectRemote (Stream *strm, const char *remote_url)      if (pid == LLDB_INVALID_PROCESS_ID)      {          // We don't have a valid process ID, so note that we are connected -        // and could now request to launch or attach, or get remote process  +        // and could now request to launch or attach, or get remote process          // listings...          SetPrivateState (eStateConnected);      } @@ -864,7 +864,7 @@ ProcessGDBRemote::DoConnectRemote (Stream *strm, const char *remote_url)                  HandleStopReplySequence();              Target &target = GetTarget(); -            if (!target.GetArchitecture().IsValid())  +            if (!target.GetArchitecture().IsValid())              {                  if (m_gdb_comm.GetProcessArchitecture().IsValid())                  { @@ -1058,11 +1058,11 @@ ProcessGDBRemote::DoLaunch (Module *exe_module, ProcessLaunchInfo &launch_info)              m_gdb_comm.SetDetachOnError (launch_flags & eLaunchFlagDetachOnError);              m_gdb_comm.SendLaunchArchPacket (GetTarget().GetArchitecture().GetArchitectureName()); -             +              const char * launch_event_data = launch_info.GetLaunchEventData();              if (launch_event_data != NULL && *launch_event_data != '\0')                  m_gdb_comm.SendLaunchEventDataPacket (launch_event_data); -             +              if (working_dir)              {                  m_gdb_comm.SetWorkingDir (working_dir); @@ -1134,7 +1134,7 @@ ProcessGDBRemote::DoLaunch (Module *exe_module, ProcessLaunchInfo &launch_info)                  }                  SetPrivateState (SetThreadStopInfo (response)); -                 +                  if (!disable_stdio)                  {                      if (pty.GetMasterFileDescriptor() != lldb_utility::PseudoTerminal::invalid_fd) @@ -1152,8 +1152,8 @@ ProcessGDBRemote::DoLaunch (Module *exe_module, ProcessLaunchInfo &launch_info)      {          // Set our user ID to an invalid process ID.          SetID(LLDB_INVALID_PROCESS_ID); -        error.SetErrorStringWithFormat ("failed to get object file from '%s' for arch %s",  -                                        exe_module->GetFileSpec().GetFilename().AsCString(),  +        error.SetErrorStringWithFormat ("failed to get object file from '%s' for arch %s", +                                        exe_module->GetFileSpec().GetFilename().AsCString(),                                          exe_module->GetArchitecture().GetArchitectureName());      }      return error; @@ -1167,7 +1167,7 @@ ProcessGDBRemote::ConnectToDebugserver (const char *connect_url)      Error error;      // Only connect if we have a valid connect URL      Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS)); -     +      if (connect_url && connect_url[0])      {          if (log) @@ -1189,9 +1189,9 @@ ProcessGDBRemote::ConnectToDebugserver (const char *connect_url)                      // If we were interrupted, don't keep retrying.                      break;                  } -                 +                  retry_count++; -                 +                  if (retry_count >= max_retry_count)                      break; @@ -1216,7 +1216,7 @@ ProcessGDBRemote::ConnectToDebugserver (const char *connect_url)      // We always seem to be able to open a connection to a local port      // so we need to make sure we can then send data to it. If we can't      // then we aren't actually connected to anything, so try and do the -    // handshake with the remote GDB server and make sure that goes  +    // handshake with the remote GDB server and make sure that goes      // alright.      if (!m_gdb_comm.HandshakeWithServer (&error))      { @@ -1382,7 +1382,7 @@ ProcessGDBRemote::DoAttachToProcessWithID (lldb::pid_t attach_pid, const Process              char packet[64];              const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, attach_pid); -            SetID (attach_pid);             +            SetID (attach_pid);              m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet, packet_len));          }          else @@ -1405,9 +1405,9 @@ ProcessGDBRemote::DoAttachToProcessWithName (const char *process_name, const Pro          if (error.Success())          {              StreamString packet; -             +              m_gdb_comm.SetDetachOnError(attach_info.GetDetachOnError()); -             +              if (attach_info.GetWaitForLaunch())              {                  if (!m_gdb_comm.GetVAttachOrWaitSupported()) @@ -1426,7 +1426,7 @@ ProcessGDBRemote::DoAttachToProcessWithName (const char *process_name, const Pro                  packet.PutCString("vAttachName");              packet.PutChar(';');              packet.PutBytesAsRawHex8(process_name, strlen(process_name), endian::InlHostByteOrder(), endian::InlHostByteOrder()); -             +              m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (packet.GetData(), packet.GetSize()));          } @@ -1471,12 +1471,12 @@ ProcessGDBRemote::DoResume ()      Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));      if (log)          log->Printf ("ProcessGDBRemote::Resume()"); -     +      Listener listener ("gdb-remote.resume-packet-sent");      if (listener.StartListeningForEvents (&m_gdb_comm, GDBRemoteCommunication::eBroadcastBitRunPacketSent))      {          listener.StartListeningForEvents (&m_async_broadcaster, ProcessGDBRemote::eBroadcastBitAsyncThreadDidExit); -         +          const size_t num_threads = GetThreadList().GetSize();          StreamString continue_packet; @@ -1496,7 +1496,7 @@ ProcessGDBRemote::DoResume ()              else              {                  continue_packet.PutCString ("vCont"); -             +                  if (!m_continue_c_tids.empty())                  {                      if (m_gdb_comm.GetVContSupported ('c')) @@ -1504,10 +1504,10 @@ ProcessGDBRemote::DoResume ()                          for (tid_collection::const_iterator t_pos = m_continue_c_tids.begin(), t_end = m_continue_c_tids.end(); t_pos != t_end; ++t_pos)                              continue_packet.Printf(";c:%4.4" PRIx64, *t_pos);                      } -                    else  +                    else                          continue_packet_error = true;                  } -                 +                  if (!continue_packet_error && !m_continue_C_tids.empty())                  {                      if (m_gdb_comm.GetVContSupported ('C')) @@ -1515,7 +1515,7 @@ ProcessGDBRemote::DoResume ()                          for (tid_sig_collection::const_iterator s_pos = m_continue_C_tids.begin(), s_end = m_continue_C_tids.end(); s_pos != s_end; ++s_pos)                              continue_packet.Printf(";C%2.2x:%4.4" PRIx64, s_pos->second, s_pos->first);                      } -                    else  +                    else                          continue_packet_error = true;                  } @@ -1526,10 +1526,10 @@ ProcessGDBRemote::DoResume ()                          for (tid_collection::const_iterator t_pos = m_continue_s_tids.begin(), t_end = m_continue_s_tids.end(); t_pos != t_end; ++t_pos)                              continue_packet.Printf(";s:%4.4" PRIx64, *t_pos);                      } -                    else  +                    else                          continue_packet_error = true;                  } -                 +                  if (!continue_packet_error && !m_continue_S_tids.empty())                  {                      if (m_gdb_comm.GetVContSupported ('S')) @@ -1540,14 +1540,14 @@ ProcessGDBRemote::DoResume ()                      else                          continue_packet_error = true;                  } -                 +                  if (continue_packet_error)                      continue_packet.GetString().clear();              }          }          else              continue_packet_error = true; -         +          if (continue_packet_error)          {              // Either no vCont support, or we tried to use part of the vCont @@ -1563,33 +1563,33 @@ ProcessGDBRemote::DoResume ()                  {                      // All threads are resuming...                      m_gdb_comm.SetCurrentThreadForRun (-1); -                    continue_packet.PutChar ('c');  +                    continue_packet.PutChar ('c');                      continue_packet_error = false;                  }                  else if (num_continue_c_tids == 1 && -                         num_continue_C_tids == 0 &&  -                         num_continue_s_tids == 0 &&  +                         num_continue_C_tids == 0 && +                         num_continue_s_tids == 0 &&                           num_continue_S_tids == 0 )                  {                      // Only one thread is continuing                      m_gdb_comm.SetCurrentThreadForRun (m_continue_c_tids.front()); -                    continue_packet.PutChar ('c');                 +                    continue_packet.PutChar ('c');                      continue_packet_error = false;                  }              }              if (continue_packet_error && num_continue_C_tids > 0)              { -                if ((num_continue_C_tids + num_continue_c_tids) == num_threads &&  -                    num_continue_C_tids > 0 &&  -                    num_continue_s_tids == 0 &&  +                if ((num_continue_C_tids + num_continue_c_tids) == num_threads && +                    num_continue_C_tids > 0 && +                    num_continue_s_tids == 0 &&                      num_continue_S_tids == 0 )                  {                      const int continue_signo = m_continue_C_tids.front().second;                      // Only one thread is continuing                      if (num_continue_C_tids > 1)                      { -                        // More that one thread with a signal, yet we don't have  +                        // More that one thread with a signal, yet we don't have                          // vCont support and we are being asked to resume each                          // thread with a signal, we need to make sure they are                          // all the same signal, or we can't issue the continue @@ -1641,13 +1641,13 @@ ProcessGDBRemote::DoResume ()                      continue_packet_error = false;                  }                  else if (num_continue_c_tids == 0 && -                         num_continue_C_tids == 0 &&  -                         num_continue_s_tids == 1 &&  +                         num_continue_C_tids == 0 && +                         num_continue_s_tids == 1 &&                           num_continue_S_tids == 0 )                  {                      // Only one thread is stepping                      m_gdb_comm.SetCurrentThreadForRun (m_continue_s_tids.front()); -                    continue_packet.PutChar ('s');                 +                    continue_packet.PutChar ('s');                      continue_packet_error = false;                  }              } @@ -1675,8 +1675,8 @@ ProcessGDBRemote::DoResume ()                      }                  }                  else if (num_continue_c_tids == 0 && -                         num_continue_C_tids == 0 &&  -                         num_continue_s_tids == 0 &&  +                         num_continue_C_tids == 0 && +                         num_continue_s_tids == 0 &&                           num_continue_S_tids == 1 )                  {                      // Only one thread is stepping with signal @@ -1704,7 +1704,7 @@ ProcessGDBRemote::DoResume ()                      log->Printf ("ProcessGDBRemote::DoResume: Trying to resume but the async thread is dead.");                  return error;              } -             +              m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncContinue, new EventDataBytes (continue_packet.GetData(), continue_packet.GetSize()));              if (listener.WaitForEvent (&timeout, event_sp) == false) @@ -1890,7 +1890,7 @@ ProcessGDBRemote::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new      Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_THREAD));      if (log && log->GetMask().Test(GDBR_LOG_VERBOSE))          log->Printf ("ProcessGDBRemote::%s (pid = %" PRIu64 ")", __FUNCTION__, GetID()); -     +      size_t num_thread_ids = m_thread_ids.size();      // The "m_thread_ids" thread ID list should always be updated after each stop      // reply packet, but in case it isn't, update it here. @@ -1926,7 +1926,7 @@ ProcessGDBRemote::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new                             thread_sp->GetID());              }              // The m_thread_pcs vector has pc values in big-endian order, not target-endian, unlike most -            // of the register read/write packets in gdb-remote protocol.   +            // of the register read/write packets in gdb-remote protocol.              // Early in the process startup, we may not yet have set the process ByteOrder so we ignore these;              // they are a performance improvement over fetching thread register values individually, the              // method we will fall back to if needed. @@ -1936,7 +1936,7 @@ ProcessGDBRemote::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new                  RegisterContextSP reg_ctx_sp (thread_sp->GetRegisterContext());                  if (reg_ctx_sp)                  { -                    uint32_t pc_regnum = reg_ctx_sp->ConvertRegisterKindToRegisterNumber  +                    uint32_t pc_regnum = reg_ctx_sp->ConvertRegisterKindToRegisterNumber                                                                     (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);                      if (pc_regnum != LLDB_INVALID_REGNUM)                      { @@ -1947,7 +1947,7 @@ ProcessGDBRemote::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new              new_thread_list.AddThread(thread_sp);          }      } -     +      // Whatever that is left in old_thread_list_copy are not      // present in new_thread_list. Remove non-existent threads from internal id table.      size_t old_num_thread_ids = old_thread_list_copy.GetSize(false); @@ -1960,7 +1960,7 @@ ProcessGDBRemote::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new              m_thread_id_to_index_id_map.erase(old_thread_id);          }      } -     +      return true;  } @@ -2658,10 +2658,10 @@ ProcessGDBRemote::SetThreadStopInfo (StringExtractor& stop_packet)              if (tid == LLDB_INVALID_THREAD_ID)              { -                // A thread id may be invalid if the response is old style 'S' packet which does not provide the  +                // A thread id may be invalid if the response is old style 'S' packet which does not provide the                  // thread information. So update the thread list and choose the first one.                  UpdateThreadIDList (); -                 +                  if (!m_thread_ids.empty ())                  {                      tid = m_thread_ids.front (); @@ -2742,7 +2742,7 @@ ProcessGDBRemote::RefreshStateAfterStop ()      // Let all threads recover from stopping and do any clean up based      // on the previous thread state (if any).      m_thread_list_real.RefreshStateAfterStop(); -     +  }  Error @@ -2752,7 +2752,7 @@ ProcessGDBRemote::DoHalt (bool &caused_stop)      bool timed_out = false;      Mutex::Locker locker; -     +      if (m_public_state.GetValue() == eStateAttaching)      {          // We are being asked to halt during an attach. We need to just close @@ -2768,7 +2768,7 @@ ProcessGDBRemote::DoHalt (bool &caused_stop)              else                  error.SetErrorString("unknown error sending interrupt packet");          } -         +          caused_stop = m_gdb_comm.GetInterruptWasSent ();      }      return error; @@ -2781,7 +2781,7 @@ ProcessGDBRemote::DoDetach(bool keep_stopped)      Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));      if (log)          log->Printf ("ProcessGDBRemote::DoDetach(keep_stopped: %i)", keep_stopped); -  +      error = m_gdb_comm.Detach (keep_stopped);      if (log)      { @@ -2790,7 +2790,7 @@ ProcessGDBRemote::DoDetach(bool keep_stopped)          else              log->Printf ("ProcessGDBRemote::DoDetach() detach packet send failed: %s", error.AsCString() ? error.AsCString() : "<unknown error>");      } -     +      if (!error.Success())          return error; @@ -2833,7 +2833,7 @@ ProcessGDBRemote::DoDestroy ()      if (!m_gdb_comm.GetThreadSuffixSupported() && m_public_state.GetValue() != eStateRunning)      {          PlatformSP platform_sp = GetTarget().GetPlatform(); -         +          // FIXME: These should be ConstStrings so we aren't doing strcmp'ing.          if (platform_sp              && platform_sp->GetName() @@ -2845,18 +2845,18 @@ ProcessGDBRemote::DoDestroy ()                      log->PutCString ("ProcessGDBRemote::DoDestroy() - Tried resuming to destroy once already, not doing it again.");              }              else -            {             +            {                  // At present, the plans are discarded and the breakpoints disabled Process::Destroy,                  // but we really need it to happen here and it doesn't matter if we do it twice.                  m_thread_list.DiscardThreadPlans();                  DisableAllBreakpointSites(); -                 +                  bool stop_looks_like_crash = false;                  ThreadList &threads = GetThreadList(); -                 +                  {                      Mutex::Locker locker(threads.GetMutex()); -                     +                      size_t num_threads = threads.GetSize();                      for (size_t i = 0; i < num_threads; i++)                      { @@ -2877,21 +2877,21 @@ ProcessGDBRemote::DoDestroy ()                          }                      }                  } -                 +                  if (stop_looks_like_crash)                  {                      if (log)                          log->PutCString ("ProcessGDBRemote::DoDestroy() - Stopped at a breakpoint, continue and then kill.");                      m_destroy_tried_resuming = true; -                     -                    // If we are going to run again before killing, it would be good to suspend all the threads  + +                    // If we are going to run again before killing, it would be good to suspend all the threads                      // before resuming so they won't get into more trouble.  Sadly, for the threads stopped with                      // the breakpoint or exception, the exception doesn't get cleared if it is suspended, so we do                      // have to run the risk of letting those threads proceed a bit. -     +                      {                          Mutex::Locker locker(threads.GetMutex()); -                         +                          size_t num_threads = threads.GetSize();                          for (size_t i = 0; i < num_threads; i++)                          { @@ -2916,7 +2916,7 @@ ProcessGDBRemote::DoDestroy ()              }          }      } -     +      // Interrupt if our inferior is running...      int exit_status = SIGABRT;      std::string exit_string; @@ -3098,7 +3098,7 @@ ProcessGDBRemote::DoReadMemory (addr_t addr, void *buf, size_t size, Error &erro      if (size > m_max_memory_size)      {          // Keep memory read sizes down to a sane limit. This function will be -        // called multiple times in order to complete the task by  +        // called multiple times in order to complete the task by          // lldb_private::Process so it is ok to do this.          size = m_max_memory_size;      } @@ -3156,7 +3156,7 @@ ProcessGDBRemote::DoWriteMemory (addr_t addr, const void *buf, size_t size, Erro      if (size > m_max_memory_size)      {          // Keep memory read sizes down to a sane limit. This function will be -        // called multiple times in order to complete the task by  +        // called multiple times in order to complete the task by          // lldb_private::Process so it is ok to do this.          size = m_max_memory_size;      } @@ -3191,7 +3191,7 @@ ProcessGDBRemote::DoAllocateMemory (size_t size, uint32_t permissions, Error &er  {      Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS|LIBLLDB_LOG_EXPRESSIONS));      addr_t allocated_addr = LLDB_INVALID_ADDRESS; -     +      LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory();      switch (supported)      { @@ -3222,7 +3222,7 @@ ProcessGDBRemote::DoAllocateMemory (size_t size, uint32_t permissions, Error &er              }              break;      } -     +      if (allocated_addr == LLDB_INVALID_ADDRESS)          error.SetErrorStringWithFormat("unable to allocate %" PRIu64 " bytes of memory with permissions %s", (uint64_t)size, GetPermissionsAsCString (permissions));      else @@ -3231,10 +3231,10 @@ ProcessGDBRemote::DoAllocateMemory (size_t size, uint32_t permissions, Error &er  }  Error -ProcessGDBRemote::GetMemoryRegionInfo (addr_t load_addr,  +ProcessGDBRemote::GetMemoryRegionInfo (addr_t load_addr,                                         MemoryRegionInfo ®ion_info)  { -     +      Error error (m_gdb_comm.GetMemoryRegionInfo (load_addr, region_info));      return error;  } @@ -3242,7 +3242,7 @@ ProcessGDBRemote::GetMemoryRegionInfo (addr_t load_addr,  Error  ProcessGDBRemote::GetWatchpointSupportInfo (uint32_t &num)  { -     +      Error error (m_gdb_comm.GetWatchpointSupportInfo (num));      return error;  } @@ -3257,13 +3257,13 @@ ProcessGDBRemote::GetWatchpointSupportInfo (uint32_t &num, bool& after)  Error  ProcessGDBRemote::DoDeallocateMemory (lldb::addr_t addr)  { -    Error error;  +    Error error;      LazyBool supported = m_gdb_comm.SupportsAllocDeallocMemory();      switch (supported)      {          case eLazyBoolCalculate: -            // We should never be deallocating memory without allocating memory  +            // We should never be deallocating memory without allocating memory              // first so we should never get eLazyBoolCalculate              error.SetErrorString ("tried to deallocate memory without ever allocating memory");              break; @@ -3272,7 +3272,7 @@ ProcessGDBRemote::DoDeallocateMemory (lldb::addr_t addr)              if (!m_gdb_comm.DeallocateMemory (addr))                  error.SetErrorStringWithFormat("unable to deallocate memory at 0x%" PRIx64, addr);              break; -             +          case eLazyBoolNo:              // Call munmap() to deallocate memory in the inferior..              { @@ -3448,7 +3448,7 @@ ProcessGDBRemote::DisableBreakpointSite (BreakpointSite *bp_site)                      stoppoint_type = eBreakpointHardware;                  else                      stoppoint_type = eBreakpointSoftware; -                 +                  if (m_gdb_comm.SendGDBStoppointTypePacket(stoppoint_type, false, addr, bp_op_size))                  error.SetErrorToGenericError();              } @@ -3554,7 +3554,7 @@ ProcessGDBRemote::DisableWatchpoint (Watchpoint *wp, bool notify)              wp->SetEnabled(false, notify);              return error;          } -         +          if (wp->IsHardware())          {              GDBStoppointType type = GetGDBStoppointType(wp); @@ -3565,7 +3565,7 @@ ProcessGDBRemote::DisableWatchpoint (Watchpoint *wp, bool notify)                  return error;              }              else -                error.SetErrorString("sending gdb watchpoint packet failed");  +                error.SetErrorString("sending gdb watchpoint packet failed");          }          // TODO: clear software watchpoints if we implement them      } @@ -3669,7 +3669,7 @@ ProcessGDBRemote::LaunchAndConnectToDebugserver (const ProcessInfo &process_info          if (m_debugserver_pid != LLDB_INVALID_PROCESS_ID)              StartAsyncThread (); -         +          if (error.Fail())          {              Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); @@ -3678,7 +3678,7 @@ ProcessGDBRemote::LaunchAndConnectToDebugserver (const ProcessInfo &process_info                  log->Printf("failed to start debugserver process: %s", error.AsCString());              return error;          } -         +          if (m_gdb_comm.IsConnected())          {              // Finish the connection process by doing the handshake without connecting (send NULL URL) @@ -3708,7 +3708,7 @@ ProcessGDBRemote::MonitorDebugserverProcess      // The baton is a "ProcessGDBRemote *". Now this class might be gone      // and might not exist anymore, so we need to carefully try to get the      // target for this process first since we have a race condition when -    // we are done running between getting the notice that the inferior  +    // we are done running between getting the notice that the inferior      // process has died and the debugserver that was debugging this process.      // In our test suite, we are also continually running process after      // process, so we must be very careful to make sure: @@ -3738,7 +3738,7 @@ ProcessGDBRemote::MonitorDebugserverProcess          ProcessSP process_sp (target_sp->GetProcessSP());          // Now we have a shared pointer to the process that can't go away on us          // so we now make sure it was the same as the one passed in, and also make -        // sure that our previous "process *" didn't get deleted and have a new  +        // sure that our previous "process *" didn't get deleted and have a new          // "process *" created in its place with the same pointer. To verify this          // we make sure the process has our debugserver process ID. If we pass all          // of these tests, then we are sure that this process is the one we were @@ -3752,7 +3752,7 @@ ProcessGDBRemote::MonitorDebugserverProcess              // If our process hasn't yet exited, debugserver might have died.              // If the process did exit, the we are reaping it.              const StateType state = process->GetState(); -             +              if (process->m_debugserver_pid != LLDB_INVALID_PROCESS_ID &&                  state != eStateInvalid &&                  state != eStateUnloaded && @@ -3828,7 +3828,7 @@ ProcessGDBRemote::StartAsyncThread ()      if (log)          log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__); -     +      Mutex::Locker start_locker(m_async_thread_state_mutex);      if (!m_async_thread.IsJoinable())      { @@ -3855,7 +3855,7 @@ ProcessGDBRemote::StopAsyncThread ()      if (m_async_thread.IsJoinable())      {          m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit); -         +          //  This will shut down the async thread.          m_gdb_comm.Disconnect();    // Disconnect from the debug server. @@ -3972,7 +3972,7 @@ ProcessGDBRemote::AsyncThread (void *arg)                                          process->SetLastStopPacket (response);                                          process->ClearThreadIDList();                                          response.SetFilePos(1); -                                         +                                          int exit_status = response.GetHexU8();                                          const char *desc_cstr = NULL;                                          StringExtractor extractor; @@ -4091,7 +4091,7 @@ ProcessGDBRemote::AsyncThread (void *arg)  //    {  //        return Host::ListProcessesMatchingName (name, matches, pids);  //    } -//    else  +//    else  //    {  //        // FIXME: Implement talking to the remote debugserver.  //        return 0; @@ -4105,7 +4105,7 @@ ProcessGDBRemote::NewThreadNotifyBreakpointHit (void *baton,                               lldb::user_id_t break_id,                               lldb::user_id_t break_loc_id)  { -    // I don't think I have to do anything here, just make sure I notice the new thread when it starts to  +    // I don't think I have to do anything here, just make sure I notice the new thread when it starts to      // run so I can stop it if that's what I want to do.      Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));      if (log) @@ -4148,7 +4148,7 @@ ProcessGDBRemote::StartNoticingNewThreads()  bool  ProcessGDBRemote::StopNoticingNewThreads() -{    +{      Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));      if (log && log->GetVerbose())          log->Printf ("Disabling new thread notification breakpoint."); @@ -4158,7 +4158,7 @@ ProcessGDBRemote::StopNoticingNewThreads()      return true;  } -     +  DynamicLoader *  ProcessGDBRemote::GetDynamicLoader ()  { @@ -4172,9 +4172,9 @@ ProcessGDBRemote::SendEventData(const char *data)  {      int return_value;      bool was_supported; -     +      Error error; -     +      return_value = m_gdb_comm.SendLaunchEventDataPacket (data, &was_supported);      if (return_value != 0)      { @@ -4284,8 +4284,8 @@ ProcessGDBRemote::GetLoadedDynamicLibrariesInfos (lldb::addr_t image_list_addres  // Establish the largest memory read/write payloads we should use.  // If the remote stub has a max packet size, stay under that size. -//  -// If the remote stub's max packet size is crazy large, use a  +// +// If the remote stub's max packet size is crazy large, use a  // reasonable largeish default.  //  // If the remote stub doesn't advertise a max packet size, use a @@ -4398,7 +4398,7 @@ struct RegisterSetInfo  };  typedef std::map<uint32_t, RegisterSetInfo> RegisterSetMap; -  +  struct GdbServerTargetInfo  {      std::string arch; @@ -4407,13 +4407,13 @@ struct GdbServerTargetInfo      RegisterSetMap reg_set_map;      XMLNode feature_node;  }; -     +  bool  ParseRegisters (XMLNode feature_node, GdbServerTargetInfo &target_info, GDBRemoteDynamicRegisterInfo &dyn_reg_info, ABISP abi_sp)  {      if (!feature_node)          return false; -     +      uint32_t cur_reg_num = 0;      uint32_t reg_offset = 0; @@ -4443,7 +4443,7 @@ ParseRegisters (XMLNode feature_node, GdbServerTargetInfo &target_info, GDBRemot              NULL,              NULL          }; -         +          reg_node.ForEachAttribute([&target_info, &gdb_group, &gdb_type, ®_name, &alt_name, &set_name, &value_regs, &invalidate_regs, &encoding_set, &format_set, ®_info, &cur_reg_num, ®_offset](const llvm::StringRef &name, const llvm::StringRef &value) -> bool {              if (name == "name")              { @@ -4538,7 +4538,7 @@ ParseRegisters (XMLNode feature_node, GdbServerTargetInfo &target_info, GDBRemot              }              return true; // Keep iterating through all attributes          }); -         +          if (!gdb_type.empty() && !(encoding_set || format_set))          {              if (gdb_type.find("int") == 0) @@ -4557,12 +4557,12 @@ ParseRegisters (XMLNode feature_node, GdbServerTargetInfo &target_info, GDBRemot                  reg_info.encoding = eEncodingIEEE754;              }          } -         +          // Only update the register set name if we didn't get a "reg_set" attribute.          // "set_name" will be empty if we didn't have a "reg_set" attribute.          if (!set_name && !gdb_group.empty())              set_name.SetCString(gdb_group.c_str()); -         +          reg_info.byte_offset = reg_offset;          assert (reg_info.byte_size != 0);          reg_offset += reg_info.byte_size; @@ -4576,16 +4576,16 @@ ParseRegisters (XMLNode feature_node, GdbServerTargetInfo &target_info, GDBRemot              invalidate_regs.push_back(LLDB_INVALID_REGNUM);              reg_info.invalidate_regs = invalidate_regs.data();          } -         +          ++cur_reg_num;          AugmentRegisterInfoViaABI (reg_info, reg_name, abi_sp);          dyn_reg_info.AddRegister(reg_info, reg_name, alt_name, set_name); -         +          return true; // Keep iterating through all "reg" elements      });      return true;  } -     +  } // namespace {} @@ -4617,14 +4617,14 @@ ProcessGDBRemote::GetGDBServerRegisterInfo ()      {          return false;      } -     +      XMLDocument xml_document;      if (xml_document.ParseMemory(raw.c_str(), raw.size(), "target.xml"))      {          GdbServerTargetInfo target_info; -         +          XMLNode target_node = xml_document.GetRootElement("target");          if (target_node)          { @@ -4655,7 +4655,7 @@ ProcessGDBRemote::GetGDBServerRegisterInfo ()                      node.ForEachChildElementWithName("group", [&target_info](const XMLNode &node) -> bool {                          uint32_t set_id = UINT32_MAX;                          RegisterSetInfo set_info; -                         +                          node.ForEachAttribute([&set_id, &set_info](const llvm::StringRef &name, const llvm::StringRef &value) -> bool {                              if (name == "id")                                  set_id = StringConvert::ToUInt32(value.data(), UINT32_MAX, 0); @@ -4663,7 +4663,7 @@ ProcessGDBRemote::GetGDBServerRegisterInfo ()                                  set_info.name = ConstString(value);                              return true; // Keep iterating through all attributes                          }); -                         +                          if (set_id != UINT32_MAX)                              target_info.reg_set_map[set_id] = set_info;                          return true; // Keep iterating through all "group" elements @@ -4671,12 +4671,12 @@ ProcessGDBRemote::GetGDBServerRegisterInfo ()                  }                  return true; // Keep iterating through all children of the target_node              }); -             +              if (feature_node)              {                  ParseRegisters(feature_node, target_info, this->m_register_info, GetABI());              } -             +              for (const auto &include : target_info.includes)              {                  // request register file @@ -4730,7 +4730,7 @@ ProcessGDBRemote::GetLoadedModuleList (GDBLoadedModuleInfoList & list)          if (log)              log->Printf ("parsing: %s", raw.c_str());          XMLDocument doc; -         +          if (!doc.ParseMemory(raw.c_str(), raw.size(), "noname.xml"))              return Error (0, ErrorType::eErrorTypeGeneric); @@ -4750,7 +4750,7 @@ ProcessGDBRemote::GetLoadedModuleList (GDBLoadedModuleInfoList & list)              GDBLoadedModuleInfoList::LoadedModuleInfo module;              library.ForEachAttribute([log, &module](const llvm::StringRef &name, const llvm::StringRef &value) -> bool { -                 +                  if (name == "name")                      module.set_name (value.str());                  else if (name == "lm") @@ -4770,7 +4770,7 @@ ProcessGDBRemote::GetLoadedModuleList (GDBLoadedModuleInfoList & list)                      // the memory address of the libraries PT_DYAMIC section.                      module.set_dynamic(StringConvert::ToUInt64(value.data(), LLDB_INVALID_ADDRESS, 0));                  } -                 +                  return true; // Keep iterating over all properties of "library"              }); @@ -5075,7 +5075,7 @@ protected:  class CommandObjectProcessGDBRemotePacketHistory : public CommandObjectParsed  {  private: -     +  public:      CommandObjectProcessGDBRemotePacketHistory(CommandInterpreter &interpreter) :      CommandObjectParsed (interpreter, @@ -5084,11 +5084,11 @@ public:                           NULL)      {      } -     +      ~CommandObjectProcessGDBRemotePacketHistory ()      {      } -     +      bool      DoExecute (Args& command, CommandReturnObject &result) override      { @@ -5115,7 +5115,7 @@ public:  class CommandObjectProcessGDBRemotePacketXferSize : public CommandObjectParsed  {  private: -     +  public:      CommandObjectProcessGDBRemotePacketXferSize(CommandInterpreter &interpreter) :      CommandObjectParsed (interpreter, @@ -5124,11 +5124,11 @@ public:                           NULL)      {      } -     +      ~CommandObjectProcessGDBRemotePacketXferSize ()      {      } -     +      bool      DoExecute (Args& command, CommandReturnObject &result) override      { @@ -5162,7 +5162,7 @@ public:  class CommandObjectProcessGDBRemotePacketSend : public CommandObjectParsed  {  private: -     +  public:      CommandObjectProcessGDBRemotePacketSend(CommandInterpreter &interpreter) :          CommandObjectParsed (interpreter, @@ -5172,11 +5172,11 @@ public:                               NULL)      {      } -     +      ~CommandObjectProcessGDBRemotePacketSend ()      {      } -     +      bool      DoExecute (Args& command, CommandReturnObject &result) override      { @@ -5187,7 +5187,7 @@ public:              result.SetStatus (eReturnStatusFailed);              return false;          } -         +          ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr();          if (process)          { @@ -5201,7 +5201,7 @@ public:                  Stream &output_strm = result.GetOutputStream();                  output_strm.Printf ("  packet: %s\n", packet_cstr);                  std::string &response_str = response.GetStringRef(); -                 +                  if (strstr(packet_cstr, "qGetProfileData") != NULL)                  {                      response_str = process->GetGDBRemote().HarmonizeThreadIdsForProfileData(process, response); @@ -5220,7 +5220,7 @@ public:  class CommandObjectProcessGDBRemotePacketMonitor : public CommandObjectRaw  {  private: -     +  public:      CommandObjectProcessGDBRemotePacketMonitor(CommandInterpreter &interpreter) :          CommandObjectRaw (interpreter, @@ -5230,11 +5230,11 @@ public:                           NULL)      {      } -     +      ~CommandObjectProcessGDBRemotePacketMonitor ()      {      } -     +      bool      DoExecute (const char *command, CommandReturnObject &result) override      { @@ -5244,7 +5244,7 @@ public:              result.SetStatus (eReturnStatusFailed);              return false;          } -         +          ProcessGDBRemote *process = (ProcessGDBRemote *)m_interpreter.GetExecutionContext().GetProcessPtr();          if (process)          { @@ -5252,7 +5252,7 @@ public:              packet.PutCString("qRcmd,");              packet.PutBytesAsRawHex8(command, strlen(command));              const char *packet_cstr = packet.GetString().c_str(); -             +              bool send_async = true;              StringExtractorGDBRemote response;              process->GetGDBRemote().SendPacketAndWaitForResponse(packet_cstr, response, send_async); @@ -5260,7 +5260,7 @@ public:              Stream &output_strm = result.GetOutputStream();              output_strm.Printf ("  packet: %s\n", packet_cstr);              const std::string &response_str = response.GetStringRef(); -             +              if (response_str.empty())                  output_strm.PutCString ("response: \nerror: UNIMPLEMENTED\n");              else @@ -5273,7 +5273,7 @@ public:  class CommandObjectProcessGDBRemotePacket : public CommandObjectMultiword  {  private: -     +  public:      CommandObjectProcessGDBRemotePacket(CommandInterpreter &interpreter) :          CommandObjectMultiword (interpreter, @@ -5287,10 +5287,10 @@ public:          LoadSubCommand ("xfer-size", CommandObjectSP (new CommandObjectProcessGDBRemotePacketXferSize (interpreter)));          LoadSubCommand ("speed-test", CommandObjectSP (new CommandObjectProcessGDBRemoteSpeedTest (interpreter)));      } -     +      ~CommandObjectProcessGDBRemotePacket ()      { -    }     +    }  };  class CommandObjectMultiwordProcessGDBRemote : public CommandObjectMultiword diff --git a/source/Plugins/Process/mach-core/CMakeLists.txt b/source/Plugins/Process/mach-core/CMakeLists.txt new file mode 100644 index 000000000000..ac54658cf4dc --- /dev/null +++ b/source/Plugins/Process/mach-core/CMakeLists.txt @@ -0,0 +1,6 @@ +include_directories(../Utility) + +add_lldb_library(lldbPluginProcessMachCore +  ProcessMachCore.cpp +  ThreadMachCore.cpp +  ) diff --git a/source/Plugins/Process/mach-core/Makefile b/source/Plugins/Process/mach-core/Makefile new file mode 100644 index 000000000000..6db849872267 --- /dev/null +++ b/source/Plugins/Process/mach-core/Makefile @@ -0,0 +1,14 @@ +##===- source/Plugins/Process/mach-core/Makefile -----------*- Makefile -*-===## +#  +#                     The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +#  +##===----------------------------------------------------------------------===## + +LLDB_LEVEL := ../../../.. +LIBRARYNAME := lldbPluginProcessMachCore +BUILD_ARCHIVE = 1 + +include $(LLDB_LEVEL)/Makefile diff --git a/source/Plugins/Process/mach-core/ProcessMachCore.cpp b/source/Plugins/Process/mach-core/ProcessMachCore.cpp new file mode 100644 index 000000000000..b199ec606367 --- /dev/null +++ b/source/Plugins/Process/mach-core/ProcessMachCore.cpp @@ -0,0 +1,520 @@ +//===-- ProcessMachCore.cpp ------------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// C Includes +#include <errno.h> +#include <stdlib.h> + +// C++ Includes +#include "llvm/Support/MathExtras.h" +#include <mutex> + +// Other libraries and framework includes +#include "lldb/Core/DataBuffer.h" +#include "lldb/Core/Debugger.h" +#include "lldb/Core/PluginManager.h" +#include "lldb/Core/Module.h" +#include "lldb/Core/ModuleSpec.h" +#include "lldb/Core/Section.h" +#include "lldb/Core/State.h" +#include "lldb/Host/Host.h" +#include "lldb/Symbol/ObjectFile.h" +#include "lldb/Target/Target.h" +#include "lldb/Target/Thread.h" + +// Project includes +#include "ProcessMachCore.h" +#include "ThreadMachCore.h" +#include "StopInfoMachException.h" + +// Needed for the plug-in names for the dynamic loaders. +#include "lldb/Utility/SafeMachO.h" + +#include "Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h" +#include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h" +#include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h" + +using namespace lldb; +using namespace lldb_private; + +ConstString +ProcessMachCore::GetPluginNameStatic() +{ +    static ConstString g_name("mach-o-core"); +    return g_name; +} + +const char * +ProcessMachCore::GetPluginDescriptionStatic() +{ +    return "Mach-O core file debugging plug-in."; +} + +void +ProcessMachCore::Terminate() +{ +    PluginManager::UnregisterPlugin (ProcessMachCore::CreateInstance); +} + + +lldb::ProcessSP +ProcessMachCore::CreateInstance (lldb::TargetSP target_sp, Listener &listener, const FileSpec *crash_file) +{ +    lldb::ProcessSP process_sp; +    if (crash_file) +    { +        const size_t header_size = sizeof(llvm::MachO::mach_header); +        lldb::DataBufferSP data_sp (crash_file->ReadFileContents(0, header_size)); +        if (data_sp && data_sp->GetByteSize() == header_size) +        { +            DataExtractor data(data_sp, lldb::eByteOrderLittle, 4); +             +            lldb::offset_t data_offset = 0; +            llvm::MachO::mach_header mach_header; +            if (ObjectFileMachO::ParseHeader(data, &data_offset, mach_header)) +            { +                if (mach_header.filetype == llvm::MachO::MH_CORE) +                    process_sp.reset(new ProcessMachCore (target_sp, listener, *crash_file)); +            } +        } +         +    } +    return process_sp; +} + +bool +ProcessMachCore::CanDebug(lldb::TargetSP target_sp, bool plugin_specified_by_name) +{ +    if (plugin_specified_by_name) +        return true; + +    // For now we are just making sure the file exists for a given module +    if (!m_core_module_sp && m_core_file.Exists()) +    { +        // Don't add the Target's architecture to the ModuleSpec - we may be working +        // with a core file that doesn't have the correct cpusubtype in the header +        // but we should still try to use it - ModuleSpecList::FindMatchingModuleSpec +        // enforces a strict arch mach. +        ModuleSpec core_module_spec(m_core_file); +        Error error (ModuleList::GetSharedModule (core_module_spec,  +                                                  m_core_module_sp,  +                                                  NULL, +                                                  NULL,  +                                                  NULL)); + +        if (m_core_module_sp) +        { +            ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); +            if (core_objfile && core_objfile->GetType() == ObjectFile::eTypeCoreFile) +                return true; +        } +    } +    return false; +} + +//---------------------------------------------------------------------- +// ProcessMachCore constructor +//---------------------------------------------------------------------- +ProcessMachCore::ProcessMachCore(lldb::TargetSP target_sp, Listener &listener, const FileSpec &core_file) : +    Process (target_sp, listener), +    m_core_aranges (), +    m_core_module_sp (), +    m_core_file (core_file), +    m_dyld_addr (LLDB_INVALID_ADDRESS), +    m_mach_kernel_addr (LLDB_INVALID_ADDRESS), +    m_dyld_plugin_name () +{ +} + +//---------------------------------------------------------------------- +// Destructor +//---------------------------------------------------------------------- +ProcessMachCore::~ProcessMachCore() +{ +    Clear(); +    // We need to call finalize on the process before destroying ourselves +    // to make sure all of the broadcaster cleanup goes as planned. If we +    // destruct this class, then Process::~Process() might have problems +    // trying to fully destroy the broadcaster. +    Finalize(); +} + +//---------------------------------------------------------------------- +// PluginInterface +//---------------------------------------------------------------------- +ConstString +ProcessMachCore::GetPluginName() +{ +    return GetPluginNameStatic(); +} + +uint32_t +ProcessMachCore::GetPluginVersion() +{ +    return 1; +} + +bool +ProcessMachCore::GetDynamicLoaderAddress (lldb::addr_t addr) +{ +    llvm::MachO::mach_header header; +    Error error; +    if (DoReadMemory (addr, &header, sizeof(header), error) != sizeof(header)) +        return false; +    if (header.magic == llvm::MachO::MH_CIGAM || +        header.magic == llvm::MachO::MH_CIGAM_64) +    { +        header.magic        = llvm::ByteSwap_32(header.magic); +        header.cputype      = llvm::ByteSwap_32(header.cputype); +        header.cpusubtype   = llvm::ByteSwap_32(header.cpusubtype); +        header.filetype     = llvm::ByteSwap_32(header.filetype); +        header.ncmds        = llvm::ByteSwap_32(header.ncmds); +        header.sizeofcmds   = llvm::ByteSwap_32(header.sizeofcmds); +        header.flags        = llvm::ByteSwap_32(header.flags); +    } + +    // TODO: swap header if needed... +    //printf("0x%16.16" PRIx64 ": magic = 0x%8.8x, file_type= %u\n", vaddr, header.magic, header.filetype); +    if (header.magic == llvm::MachO::MH_MAGIC || +        header.magic == llvm::MachO::MH_MAGIC_64) +    { +        // Check MH_EXECUTABLE to see if we can find the mach image +        // that contains the shared library list. The dynamic loader  +        // (dyld) is what contains the list for user applications, +        // and the mach kernel contains a global that has the list  +        // of kexts to load +        switch (header.filetype) +        { +        case llvm::MachO::MH_DYLINKER: +            //printf("0x%16.16" PRIx64 ": file_type = MH_DYLINKER\n", vaddr); +            // Address of dyld "struct mach_header" in the core file +            m_dyld_addr = addr; +            return true; + +        case llvm::MachO::MH_EXECUTE: +            //printf("0x%16.16" PRIx64 ": file_type = MH_EXECUTE\n", vaddr); +            // Check MH_EXECUTABLE file types to see if the dynamic link object flag +            // is NOT set. If it isn't, then we have a mach_kernel. +            if ((header.flags & llvm::MachO::MH_DYLDLINK) == 0) +            { +                // Address of the mach kernel "struct mach_header" in the core file. +                m_mach_kernel_addr = addr; +                return true; +            } +            break; +        } +    } +    return false; +} + +//---------------------------------------------------------------------- +// Process Control +//---------------------------------------------------------------------- +Error +ProcessMachCore::DoLoadCore () +{ +    Error error; +    if (!m_core_module_sp) +    { +        error.SetErrorString ("invalid core module");    +        return error; +    } + +    ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); +    if (core_objfile == NULL) +    { +        error.SetErrorString ("invalid core object file");    +        return error; +    } +     +    if (core_objfile->GetNumThreadContexts() == 0) +    { +        error.SetErrorString ("core file doesn't contain any LC_THREAD load commands, or the LC_THREAD architecture is not supported in this lldb"); +        return error; +    } + +    SectionList *section_list = core_objfile->GetSectionList(); +    if (section_list == NULL) +    { +        error.SetErrorString ("core file has no sections");    +        return error; +    } +         +    const uint32_t num_sections = section_list->GetNumSections(0); +    if (num_sections == 0) +    { +        error.SetErrorString ("core file has no sections");    +        return error; +    } +     +    SetCanJIT(false); + +    llvm::MachO::mach_header header; +    DataExtractor data (&header,  +                        sizeof(header),  +                        m_core_module_sp->GetArchitecture().GetByteOrder(), +                        m_core_module_sp->GetArchitecture().GetAddressByteSize()); + +    bool ranges_are_sorted = true; +    addr_t vm_addr = 0; +    for (uint32_t i=0; i<num_sections; ++i) +    { +        Section *section = section_list->GetSectionAtIndex (i).get(); +        if (section) +        { +            lldb::addr_t section_vm_addr = section->GetFileAddress(); +            FileRange file_range (section->GetFileOffset(), section->GetFileSize()); +            VMRangeToFileOffset::Entry range_entry (section_vm_addr, +                                                    section->GetByteSize(), +                                                    file_range); +             +            if (vm_addr > section_vm_addr) +                ranges_are_sorted = false; +            vm_addr = section->GetFileAddress(); +            VMRangeToFileOffset::Entry *last_entry = m_core_aranges.Back(); +//            printf ("LC_SEGMENT[%u] arange=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), frange=[0x%8.8x - 0x%8.8x)\n", +//                    i,  +//                    range_entry.GetRangeBase(), +//                    range_entry.GetRangeEnd(), +//                    range_entry.data.GetRangeBase(), +//                    range_entry.data.GetRangeEnd()); + +            if (last_entry && +                last_entry->GetRangeEnd() == range_entry.GetRangeBase() && +                last_entry->data.GetRangeEnd() == range_entry.data.GetRangeBase()) +            { +                last_entry->SetRangeEnd (range_entry.GetRangeEnd()); +                last_entry->data.SetRangeEnd (range_entry.data.GetRangeEnd()); +                //puts("combine"); +            } +            else +            { +                m_core_aranges.Append(range_entry); +            } +        } +    } +    if (!ranges_are_sorted) +    { +        m_core_aranges.Sort(); +    } + +    if (m_dyld_addr == LLDB_INVALID_ADDRESS || m_mach_kernel_addr == LLDB_INVALID_ADDRESS) +    { +        // We need to locate the main executable in the memory ranges +        // we have in the core file.  We need to search for both a user-process dyld binary +        // and a kernel binary in memory; we must look at all the pages in the binary so +        // we don't miss one or the other.  Step through all memory segments searching for +        // a kernel binary and for a user process dyld -- we'll decide which to prefer  +        // later if both are present. + +        const size_t num_core_aranges = m_core_aranges.GetSize(); +        for (size_t i = 0;  +             i < num_core_aranges && (m_dyld_addr == LLDB_INVALID_ADDRESS || m_mach_kernel_addr == LLDB_INVALID_ADDRESS);  +             ++i) +        { +            const VMRangeToFileOffset::Entry *entry = m_core_aranges.GetEntryAtIndex(i); +            lldb::addr_t section_vm_addr_start = entry->GetRangeBase(); +            lldb::addr_t section_vm_addr_end = entry->GetRangeEnd(); +            for (lldb::addr_t section_vm_addr = section_vm_addr_start; +                 section_vm_addr < section_vm_addr_end; +                 section_vm_addr += 0x1000) +            { +                GetDynamicLoaderAddress (section_vm_addr); +            } +        } +    } + +    // If we found both a user-process dyld and a kernel binary, we need to decide +    // which to prefer. +    if (GetCorefilePreference() == eKernelCorefile) +    { +        if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) +        { +            m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic(); +        } +        else if (m_dyld_addr != LLDB_INVALID_ADDRESS) +        { +            m_dyld_plugin_name = DynamicLoaderMacOSXDYLD::GetPluginNameStatic(); +        } +    } +    else +    { +        if (m_dyld_addr != LLDB_INVALID_ADDRESS) +        { +            m_dyld_plugin_name = DynamicLoaderMacOSXDYLD::GetPluginNameStatic(); +        } +        else if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) +        { +            m_dyld_plugin_name = DynamicLoaderDarwinKernel::GetPluginNameStatic(); +        } +    } + +    // Even if the architecture is set in the target, we need to override +    // it to match the core file which is always single arch. +    ArchSpec arch (m_core_module_sp->GetArchitecture()); +    if (arch.GetCore() == ArchSpec::eCore_x86_32_i486) +    { +        arch.SetTriple ("i386", GetTarget().GetPlatform().get()); +    } +    if (arch.IsValid()) +        GetTarget().SetArchitecture(arch); + +    return error; +} + +lldb_private::DynamicLoader * +ProcessMachCore::GetDynamicLoader () +{ +    if (m_dyld_ap.get() == NULL) +        m_dyld_ap.reset (DynamicLoader::FindPlugin(this, m_dyld_plugin_name.IsEmpty() ? NULL : m_dyld_plugin_name.GetCString())); +    return m_dyld_ap.get(); +} + +bool +ProcessMachCore::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list) +{ +    if (old_thread_list.GetSize(false) == 0) +    { +        // Make up the thread the first time this is called so we can setup our one and only +        // core thread state. +        ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); + +        if (core_objfile) +        { +            const uint32_t num_threads = core_objfile->GetNumThreadContexts (); +            for (lldb::tid_t tid = 0; tid < num_threads; ++tid) +            { +                ThreadSP thread_sp(new ThreadMachCore (*this, tid)); +                new_thread_list.AddThread (thread_sp); +            } +        } +    } +    else +    { +        const uint32_t num_threads = old_thread_list.GetSize(false); +        for (uint32_t i=0; i<num_threads; ++i) +            new_thread_list.AddThread (old_thread_list.GetThreadAtIndex (i, false)); +    } +    return new_thread_list.GetSize(false) > 0; +} + +void +ProcessMachCore::RefreshStateAfterStop () +{ +    // Let all threads recover from stopping and do any clean up based +    // on the previous thread state (if any). +    m_thread_list.RefreshStateAfterStop(); +    //SetThreadStopInfo (m_last_stop_packet); +} + +Error +ProcessMachCore::DoDestroy () +{ +    return Error(); +} + +//------------------------------------------------------------------ +// Process Queries +//------------------------------------------------------------------ + +bool +ProcessMachCore::IsAlive () +{ +    return true; +} + +bool +ProcessMachCore::WarnBeforeDetach () const +{ +    return false; +} + +//------------------------------------------------------------------ +// Process Memory +//------------------------------------------------------------------ +size_t +ProcessMachCore::ReadMemory (addr_t addr, void *buf, size_t size, Error &error) +{ +    // Don't allow the caching that lldb_private::Process::ReadMemory does +    // since in core files we have it all cached our our core file anyway. +    return DoReadMemory (addr, buf, size, error); +} + +size_t +ProcessMachCore::DoReadMemory (addr_t addr, void *buf, size_t size, Error &error) +{ +    ObjectFile *core_objfile = m_core_module_sp->GetObjectFile(); + +    if (core_objfile) +    { +        const VMRangeToFileOffset::Entry *core_memory_entry = m_core_aranges.FindEntryThatContains (addr); +        if (core_memory_entry) +        { +            const addr_t offset = addr - core_memory_entry->GetRangeBase(); +            const addr_t bytes_left = core_memory_entry->GetRangeEnd() - addr; +            size_t bytes_to_read = size; +            if (bytes_to_read > bytes_left) +                bytes_to_read = bytes_left; +            return core_objfile->CopyData (core_memory_entry->data.GetRangeBase() + offset, bytes_to_read, buf); +        } +        else +        { +            error.SetErrorStringWithFormat ("core file does not contain 0x%" PRIx64, addr); +        } +    } +    return 0; +} + +void +ProcessMachCore::Clear() +{ +    m_thread_list.Clear(); +} + +void +ProcessMachCore::Initialize() +{ +    static std::once_flag g_once_flag; + +    std::call_once(g_once_flag, []() { +        PluginManager::RegisterPlugin (GetPluginNameStatic(), +                                       GetPluginDescriptionStatic(), +                                       CreateInstance); +    }); +} + +addr_t +ProcessMachCore::GetImageInfoAddress() +{ +    // If we found both a user-process dyld and a kernel binary, we need to decide +    // which to prefer. +    if (GetCorefilePreference() == eKernelCorefile) +    { +        if (m_mach_kernel_addr != LLDB_INVALID_ADDRESS) +        { +            return m_mach_kernel_addr; +        } +        return m_dyld_addr; +    } +    else +    { +        if (m_dyld_addr != LLDB_INVALID_ADDRESS) +        { +            return m_dyld_addr; +        } +        return m_mach_kernel_addr; +    } +} + + +lldb_private::ObjectFile * +ProcessMachCore::GetCoreObjectFile () +{ +    return m_core_module_sp->GetObjectFile(); +} diff --git a/source/Plugins/Process/mach-core/ProcessMachCore.h b/source/Plugins/Process/mach-core/ProcessMachCore.h new file mode 100644 index 000000000000..2de0b772370c --- /dev/null +++ b/source/Plugins/Process/mach-core/ProcessMachCore.h @@ -0,0 +1,164 @@ +//===-- ProcessMachCore.h ---------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_ProcessMachCore_h_ +#define liblldb_ProcessMachCore_h_ + +// C Includes +// C++ Includes +#include <list> +#include <vector> + +// Other libraries and framework includes +// Project includes +#include "lldb/Core/ConstString.h" +#include "lldb/Core/Error.h" +#include "lldb/Target/Process.h" + +class ThreadKDP; + +class ProcessMachCore : public lldb_private::Process +{ +public: +    //------------------------------------------------------------------ +    // Constructors and Destructors +    //------------------------------------------------------------------ +    ProcessMachCore(lldb::TargetSP target_sp,  +                    lldb_private::Listener &listener, +                    const lldb_private::FileSpec &core_file); +     +    ~ProcessMachCore() override; +     +    static lldb::ProcessSP +    CreateInstance (lldb::TargetSP target_sp,  +                    lldb_private::Listener &listener,  +                    const lldb_private::FileSpec *crash_file_path); +     +    static void +    Initialize(); +     +    static void +    Terminate(); +     +    static lldb_private::ConstString +    GetPluginNameStatic(); +     +    static const char * +    GetPluginDescriptionStatic(); +     +    //------------------------------------------------------------------ +    // Check if a given Process +    //------------------------------------------------------------------ +    bool +    CanDebug (lldb::TargetSP target_sp, +              bool plugin_specified_by_name) override; +     +    //------------------------------------------------------------------ +    // Creating a new process, or attaching to an existing one +    //------------------------------------------------------------------ +    lldb_private::Error +    DoLoadCore () override; +     +    lldb_private::DynamicLoader * +    GetDynamicLoader () override; + +    //------------------------------------------------------------------ +    // PluginInterface protocol +    //------------------------------------------------------------------ +    lldb_private::ConstString +    GetPluginName() override; +     +    uint32_t +    GetPluginVersion() override; +     +    //------------------------------------------------------------------ +    // Process Control +    //------------------------------------------------------------------     +    lldb_private::Error +    DoDestroy () override; +     +    void +    RefreshStateAfterStop() override; +     +    //------------------------------------------------------------------ +    // Process Queries +    //------------------------------------------------------------------ +    bool +    IsAlive () override; + +    bool +    WarnBeforeDetach () const override; + +    //------------------------------------------------------------------ +    // Process Memory +    //------------------------------------------------------------------ +    size_t +    ReadMemory (lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error) override; +     +    size_t +    DoReadMemory (lldb::addr_t addr, void *buf, size_t size, lldb_private::Error &error) override; +     +    lldb::addr_t +    GetImageInfoAddress () override; + +protected: +    friend class ThreadMachCore; +     +    void +    Clear ( ); +     +    bool +    UpdateThreadList (lldb_private::ThreadList &old_thread_list,  +                      lldb_private::ThreadList &new_thread_list) override; +     +    lldb_private::ObjectFile * +    GetCoreObjectFile (); +private: +    bool  +    GetDynamicLoaderAddress (lldb::addr_t addr); + +    typedef enum CorefilePreference { eUserProcessCorefile, eKernelCorefile } CorefilePreferences; + +    //------------------------------------------------------------------ +    /// If a core file can be interpreted multiple ways, this establishes +    /// which style wins. +    /// +    /// If a core file contains both a kernel binary and a user-process +    /// dynamic loader, lldb needs to pick one over the other.  This could +    /// be a kernel corefile that happens to have a coyp of dyld in its +    /// memory.  Or it could be a user process coredump of lldb while doing +    /// kernel debugging - so a copy of the kernel is in its heap.  This +    /// should become a setting so it can be over-ridden when necessary. +    //------------------------------------------------------------------ +    CorefilePreference +    GetCorefilePreference () +    { +        // For now, if both user process and kernel binaries a present, +        // assume this is a kernel coredump which has a copy of a user +        // process dyld in one of its pages. +        return eKernelCorefile; +    } + +    //------------------------------------------------------------------ +    // For ProcessMachCore only +    //------------------------------------------------------------------ +    typedef lldb_private::Range<lldb::addr_t, lldb::addr_t> FileRange; +    typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, FileRange> VMRangeToFileOffset; + +    VMRangeToFileOffset m_core_aranges; +    lldb::ModuleSP m_core_module_sp; +    lldb_private::FileSpec m_core_file; +    lldb::addr_t m_dyld_addr; +    lldb::addr_t m_mach_kernel_addr; +    lldb_private::ConstString m_dyld_plugin_name; + +    DISALLOW_COPY_AND_ASSIGN (ProcessMachCore); +}; + +#endif // liblldb_ProcessMachCore_h_ diff --git a/source/Plugins/Process/mach-core/ThreadMachCore.cpp b/source/Plugins/Process/mach-core/ThreadMachCore.cpp new file mode 100644 index 000000000000..2720c910e4d1 --- /dev/null +++ b/source/Plugins/Process/mach-core/ThreadMachCore.cpp @@ -0,0 +1,132 @@ +//===-- ThreadMachCore.cpp --------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + + +#include "ThreadMachCore.h" + +#include "lldb/Utility/SafeMachO.h" + +#include "lldb/Core/ArchSpec.h" +#include "lldb/Core/DataExtractor.h" +#include "lldb/Core/StreamString.h" +#include "lldb/Core/State.h" +#include "lldb/Symbol/ObjectFile.h" +#include "lldb/Target/Process.h" +#include "lldb/Target/RegisterContext.h" +#include "lldb/Target/StopInfo.h" +#include "lldb/Target/Target.h" +#include "lldb/Target/Unwind.h" +#include "lldb/Breakpoint/Watchpoint.h" + +#include "ProcessMachCore.h" +//#include "RegisterContextKDP_arm.h" +//#include "RegisterContextKDP_i386.h" +//#include "RegisterContextKDP_x86_64.h" + +using namespace lldb; +using namespace lldb_private; + +//---------------------------------------------------------------------- +// Thread Registers +//---------------------------------------------------------------------- + +ThreadMachCore::ThreadMachCore (Process &process, lldb::tid_t tid) : +    Thread(process, tid), +    m_thread_name (), +    m_dispatch_queue_name (), +    m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS), +    m_thread_reg_ctx_sp () +{ +} + +ThreadMachCore::~ThreadMachCore () +{ +    DestroyThread(); +} + +const char * +ThreadMachCore::GetName () +{ +    if (m_thread_name.empty()) +        return NULL; +    return m_thread_name.c_str(); +} + +void +ThreadMachCore::RefreshStateAfterStop() +{ +    // Invalidate all registers in our register context. We don't set "force" to +    // true because the stop reply packet might have had some register values +    // that were expedited and these will already be copied into the register +    // context by the time this function gets called. The KDPRegisterContext +    // class has been made smart enough to detect when it needs to invalidate +    // which registers are valid by putting hooks in the register read and  +    // register supply functions where they check the process stop ID and do +    // the right thing. +    const bool force = false; +    GetRegisterContext()->InvalidateIfNeeded (force); +} + +bool +ThreadMachCore::ThreadIDIsValid (lldb::tid_t thread) +{ +    return thread != 0; +} + +lldb::RegisterContextSP +ThreadMachCore::GetRegisterContext () +{ +    if (m_reg_context_sp.get() == NULL) +        m_reg_context_sp = CreateRegisterContextForFrame (NULL); +    return m_reg_context_sp; +} + +lldb::RegisterContextSP +ThreadMachCore::CreateRegisterContextForFrame (StackFrame *frame) +{ +    lldb::RegisterContextSP reg_ctx_sp; +    uint32_t concrete_frame_idx = 0; +     +    if (frame) +        concrete_frame_idx = frame->GetConcreteFrameIndex (); + +    if (concrete_frame_idx == 0) +    { +        if (!m_thread_reg_ctx_sp) +        { +            ProcessSP process_sp (GetProcess()); +             +            ObjectFile *core_objfile = static_cast<ProcessMachCore *>(process_sp.get())->GetCoreObjectFile (); +            if (core_objfile) +                m_thread_reg_ctx_sp = core_objfile->GetThreadContextAtIndex (GetID(), *this); +        } +        reg_ctx_sp = m_thread_reg_ctx_sp; +    } +    else +    { +        Unwind *unwinder = GetUnwinder (); +        if (unwinder) +            reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame); +    } +    return reg_ctx_sp; +} + +bool +ThreadMachCore::CalculateStopInfo () +{ +    ProcessSP process_sp (GetProcess()); +    if (process_sp) +    { +        SetStopInfo(StopInfo::CreateStopReasonWithSignal (*this, SIGSTOP)); +        return true; +    } +    return false; +} + + diff --git a/source/Plugins/Process/mach-core/ThreadMachCore.h b/source/Plugins/Process/mach-core/ThreadMachCore.h new file mode 100644 index 000000000000..25973540db16 --- /dev/null +++ b/source/Plugins/Process/mach-core/ThreadMachCore.h @@ -0,0 +1,91 @@ +//===-- ThreadMachCore.h ----------------------------------------*- C++ -*-===// +// +//                     The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_ThreadMachCore_h_ +#define liblldb_ThreadMachCore_h_ + +// C Includes +// C++ Includes +#include <string> + +// Other libraries and framework includes +// Project includes +#include "lldb/Target/Thread.h" + +class ProcessMachCore; + +class ThreadMachCore : public lldb_private::Thread +{ +public: +    ThreadMachCore (lldb_private::Process &process, +                    lldb::tid_t tid); + +    ~ThreadMachCore() override; + +    void +    RefreshStateAfterStop() override; + +    const char * +    GetName() override; + +    lldb::RegisterContextSP +    GetRegisterContext() override; + +    lldb::RegisterContextSP +    CreateRegisterContextForFrame(lldb_private::StackFrame *frame) override; + +    static bool +    ThreadIDIsValid (lldb::tid_t thread); + +    bool +    ShouldStop (bool &step_more); + +    const char * +    GetBasicInfoAsString (); + +    void +    SetName(const char *name) override +    { +        if (name && name[0]) +            m_thread_name.assign (name); +        else +            m_thread_name.clear(); +    } + +    lldb::addr_t +    GetThreadDispatchQAddr () +    { +        return m_thread_dispatch_qaddr; +    } + +    void +    SetThreadDispatchQAddr (lldb::addr_t thread_dispatch_qaddr) +    { +        m_thread_dispatch_qaddr = thread_dispatch_qaddr; +    } + +protected: +    friend class ProcessMachCore; + +    //------------------------------------------------------------------ +    // Member variables. +    //------------------------------------------------------------------ +    std::string m_thread_name; +    std::string m_dispatch_queue_name; +    lldb::addr_t m_thread_dispatch_qaddr; +    lldb::RegisterContextSP m_thread_reg_ctx_sp; + +    //------------------------------------------------------------------ +    // Protected member functions. +    //------------------------------------------------------------------ +    bool +    CalculateStopInfo() override; +}; + +#endif // liblldb_ThreadMachCore_h_  | 
