Skip to content

Commit

Permalink
Initial support for native debugging of x86/x64 Windows processes
Browse files Browse the repository at this point in the history
Summary: Thanks to Hui Huang and the reviewers for all the help with this patch.

Reviewers: labath, Hui, jfb, clayborg, amccarth

Reviewed By: labath

Subscribers: amccarth, compnerd, dexonsmith, mgorny, jfb, teemperor, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D63165

llvm-svn: 368759
  • Loading branch information
aaronsm committed Aug 13, 2019
1 parent b809187 commit 5146a9e
Show file tree
Hide file tree
Showing 25 changed files with 2,916 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <cstdint>

struct alignas(16) xmm_t {
uint64_t a, b;
};

int main() {
#if defined(__x86_64__)
struct alignas(16) xmm_t {
uint64_t a, b;
};
uint64_t r8 = 0x0102030405060708;
uint64_t r9 = 0x1112131415161718;
uint64_t r10 = 0x2122232425262728;
Expand Down Expand Up @@ -49,6 +49,6 @@ int main() {
: "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
"%xmm8", "%xmm9", "%xmm10", "%xmm11", "%xmm12", "%xmm13",
"%xmm14", "%xmm15");

#endif
return 0;
}
2 changes: 2 additions & 0 deletions lldb/source/Plugins/Process/Utility/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ add_lldb_library(lldbPluginProcessUtility PLUGIN
RegisterContextPOSIX_s390x.cpp
RegisterContextPOSIX_x86.cpp
RegisterContextThreadMemory.cpp
RegisterContextWindows_i386.cpp
RegisterContextWindows_x86_64.cpp
RegisterInfoPOSIX_arm.cpp
RegisterInfoPOSIX_arm64.cpp
RegisterInfoPOSIX_ppc64le.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//===-- RegisterContextWindows_i386.cpp -------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "RegisterContextWindows_i386.h"
#include "RegisterContext_x86.h"
#include "lldb-x86-register-enums.h"

using namespace lldb_private;
using namespace lldb;

namespace {
// Declare our g_register_infos structure.
typedef struct _GPR {
uint32_t eax;
uint32_t ebx;
uint32_t ecx;
uint32_t edx;
uint32_t edi;
uint32_t esi;
uint32_t ebp;
uint32_t esp;
uint32_t eip;
uint32_t eflags;
uint32_t cs;
uint32_t fs;
uint32_t gs;
uint32_t ss;
uint32_t ds;
uint32_t es;
} GPR;

#define GPR_OFFSET(regname) (LLVM_EXTENSION offsetof(GPR, regname))

#define DEFINE_GPR(reg, alt, kind1, kind2, kind3, kind4) \
{ \
#reg, alt, sizeof(((GPR *)nullptr)->reg), GPR_OFFSET(reg), eEncodingUint, \
eFormatHex, \
{kind1, kind2, kind3, kind4, lldb_##reg##_i386 }, nullptr, nullptr, \
nullptr, 0 \
}

// clang-format off
static RegisterInfo g_register_infos_i386[] = {
// General purpose registers EH_Frame DWARF Generic Process Plugin
// =========================== ================== ================ ========================= ====================
DEFINE_GPR(eax, nullptr, ehframe_eax_i386, dwarf_eax_i386, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(ebx, nullptr, ehframe_ebx_i386, dwarf_ebx_i386, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(ecx, nullptr, ehframe_ecx_i386, dwarf_ecx_i386, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(edx, nullptr, ehframe_edx_i386, dwarf_edx_i386, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(edi, nullptr, ehframe_edi_i386, dwarf_edi_i386, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(esi, nullptr, ehframe_esi_i386, dwarf_esi_i386, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(ebp, "fp", ehframe_ebp_i386, dwarf_ebp_i386, LLDB_REGNUM_GENERIC_FP, LLDB_INVALID_REGNUM),
DEFINE_GPR(esp, "sp", ehframe_esp_i386, dwarf_esp_i386, LLDB_REGNUM_GENERIC_SP, LLDB_INVALID_REGNUM),
DEFINE_GPR(eip, "pc", ehframe_eip_i386, dwarf_eip_i386, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_REGNUM),
DEFINE_GPR(eflags, "flags", ehframe_eflags_i386, dwarf_eflags_i386, LLDB_REGNUM_GENERIC_FLAGS, LLDB_INVALID_REGNUM),
DEFINE_GPR(cs, nullptr, LLDB_INVALID_REGNUM, dwarf_cs_i386, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(fs, nullptr, LLDB_INVALID_REGNUM, dwarf_fs_i386, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(gs, nullptr, LLDB_INVALID_REGNUM, dwarf_gs_i386, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(ss, nullptr, LLDB_INVALID_REGNUM, dwarf_ss_i386, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(ds, nullptr, LLDB_INVALID_REGNUM, dwarf_ds_i386, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(es, nullptr, LLDB_INVALID_REGNUM, dwarf_es_i386, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
};
// clang-format on
} // namespace

RegisterContextWindows_i386::RegisterContextWindows_i386(
const ArchSpec &target_arch)
: lldb_private::RegisterInfoInterface(target_arch) {
assert(target_arch.GetMachine() == llvm::Triple::x86);
}

const RegisterInfo *RegisterContextWindows_i386::GetRegisterInfo() const {
return g_register_infos_i386;
}

uint32_t RegisterContextWindows_i386::GetRegisterCount() const {
return llvm::array_lengthof(g_register_infos_i386);
}

uint32_t RegisterContextWindows_i386::GetUserRegisterCount() const {
return llvm::array_lengthof(g_register_infos_i386);
}

size_t RegisterContextWindows_i386::GetGPRSize() const { return sizeof(GPR); }
27 changes: 27 additions & 0 deletions lldb/source/Plugins/Process/Utility/RegisterContextWindows_i386.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===-- RegisterContextWindows_i386.h ---------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_RegisterContextWindows_i386_H_
#define liblldb_RegisterContextWindows_i386_H_

#include "RegisterInfoInterface.h"

class RegisterContextWindows_i386 : public lldb_private::RegisterInfoInterface {
public:
RegisterContextWindows_i386(const lldb_private::ArchSpec &target_arch);

size_t GetGPRSize() const override;

const lldb_private::RegisterInfo *GetRegisterInfo() const override;

uint32_t GetRegisterCount() const override;

uint32_t GetUserRegisterCount() const override;
};

#endif
150 changes: 150 additions & 0 deletions lldb/source/Plugins/Process/Utility/RegisterContextWindows_x86_64.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
//===-- RegisterContextWindows_x86_64.cpp -----------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "RegisterContextWindows_x86_64.h"
#include "RegisterContext_x86.h"
#include "lldb-x86-register-enums.h"

#include <vector>

using namespace lldb_private;
using namespace lldb;

namespace {
typedef struct _GPR {
uint64_t rax;
uint64_t rcx;
uint64_t rdx;
uint64_t rbx;
uint64_t rsp;
uint64_t rbp;
uint64_t rsi;
uint64_t rdi;
uint64_t r8;
uint64_t r9;
uint64_t r10;
uint64_t r11;
uint64_t r12;
uint64_t r13;
uint64_t r14;
uint64_t r15;
uint64_t rip;
uint64_t rflags;
uint16_t cs;
uint16_t fs;
uint16_t gs;
uint16_t ss;
uint16_t ds;
uint16_t es;
} GPR;

#define GPR_OFFSET(regname) (LLVM_EXTENSION offsetof(GPR, regname))
#define DEFINE_GPR(reg, alt, kind1, kind2, kind3, kind4) \
{ \
#reg, alt, sizeof(((GPR *)nullptr)->reg), GPR_OFFSET(reg), eEncodingUint, \
eFormatHex, \
{kind1, kind2, kind3, kind4, lldb_##reg##_x86_64 }, nullptr, nullptr, \
nullptr, 0 \
}

typedef struct _FPReg {
XMMReg xmm0;
XMMReg xmm1;
XMMReg xmm2;
XMMReg xmm3;
XMMReg xmm4;
XMMReg xmm5;
XMMReg xmm6;
XMMReg xmm7;
XMMReg xmm8;
XMMReg xmm9;
XMMReg xmm10;
XMMReg xmm11;
XMMReg xmm12;
XMMReg xmm13;
XMMReg xmm14;
XMMReg xmm15;
} FPReg;

#define FPR_OFFSET(regname) \
(sizeof(GPR) + LLVM_EXTENSION offsetof(FPReg, regname))

#define DEFINE_XMM(reg) \
#reg, NULL, sizeof(((FPReg *)nullptr)->reg), FPR_OFFSET(reg), eEncodingUint, \
eFormatVectorOfUInt64, \
{dwarf_##reg##_x86_64, dwarf_##reg##_x86_64, LLDB_INVALID_REGNUM, \
LLDB_INVALID_REGNUM, lldb_##reg##_x86_64 }, \
nullptr, nullptr, nullptr, 0

// clang-format off
static RegisterInfo g_register_infos_x86_64[] = {
// General purpose registers EH_Frame DWARF Generic Process Plugin
// =========================== ================== ================ ========================= ====================
DEFINE_GPR(rax, nullptr, dwarf_rax_x86_64, dwarf_rax_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(rbx, nullptr, dwarf_rbx_x86_64, dwarf_rbx_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(rcx, "arg4", dwarf_rcx_x86_64, dwarf_rcx_x86_64, LLDB_REGNUM_GENERIC_ARG4, LLDB_INVALID_REGNUM),
DEFINE_GPR(rdx, "arg3", dwarf_rdx_x86_64, dwarf_rdx_x86_64, LLDB_REGNUM_GENERIC_ARG3, LLDB_INVALID_REGNUM),
DEFINE_GPR(rdi, "arg1", dwarf_rdi_x86_64, dwarf_rdi_x86_64, LLDB_REGNUM_GENERIC_ARG1, LLDB_INVALID_REGNUM),
DEFINE_GPR(rsi, "arg2", dwarf_rsi_x86_64, dwarf_rsi_x86_64, LLDB_REGNUM_GENERIC_ARG2, LLDB_INVALID_REGNUM),
DEFINE_GPR(rbp, "fp", dwarf_rbp_x86_64, dwarf_rbp_x86_64, LLDB_REGNUM_GENERIC_FP, LLDB_INVALID_REGNUM),
DEFINE_GPR(rsp, "sp", dwarf_rsp_x86_64, dwarf_rsp_x86_64, LLDB_REGNUM_GENERIC_SP, LLDB_INVALID_REGNUM),
DEFINE_GPR(r8, "arg5", dwarf_r8_x86_64, dwarf_r8_x86_64, LLDB_REGNUM_GENERIC_ARG5, LLDB_INVALID_REGNUM),
DEFINE_GPR(r9, "arg6", dwarf_r9_x86_64, dwarf_r9_x86_64, LLDB_REGNUM_GENERIC_ARG6, LLDB_INVALID_REGNUM),
DEFINE_GPR(r10, nullptr, dwarf_r10_x86_64, dwarf_r10_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(r11, nullptr, dwarf_r11_x86_64, dwarf_r11_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(r12, nullptr, dwarf_r12_x86_64, dwarf_r12_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(r13, nullptr, dwarf_r13_x86_64, dwarf_r13_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(r14, nullptr, dwarf_r14_x86_64, dwarf_r14_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(r15, nullptr, dwarf_r15_x86_64, dwarf_r15_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(rip, "pc", dwarf_rip_x86_64, dwarf_rip_x86_64, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_REGNUM),
DEFINE_GPR(rflags, "flags", dwarf_rflags_x86_64, dwarf_rflags_x86_64, LLDB_REGNUM_GENERIC_FLAGS, LLDB_INVALID_REGNUM),
DEFINE_GPR(cs, nullptr, dwarf_cs_x86_64, dwarf_cs_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(fs, nullptr, dwarf_fs_x86_64, dwarf_fs_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(gs, nullptr, dwarf_gs_x86_64, dwarf_gs_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(ss, nullptr, dwarf_ss_x86_64, dwarf_ss_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(ds, nullptr, dwarf_ds_x86_64, dwarf_ds_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_GPR(es, nullptr, dwarf_es_x86_64, dwarf_es_x86_64, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM),
DEFINE_XMM(xmm0),
DEFINE_XMM(xmm1),
DEFINE_XMM(xmm2),
DEFINE_XMM(xmm3),
DEFINE_XMM(xmm4),
DEFINE_XMM(xmm5),
DEFINE_XMM(xmm6),
DEFINE_XMM(xmm7),
DEFINE_XMM(xmm8),
DEFINE_XMM(xmm9),
DEFINE_XMM(xmm10),
DEFINE_XMM(xmm11),
DEFINE_XMM(xmm12),
DEFINE_XMM(xmm13),
DEFINE_XMM(xmm14),
DEFINE_XMM(xmm15)
};
// clang-format on
} // namespace

RegisterContextWindows_x86_64::RegisterContextWindows_x86_64(
const ArchSpec &target_arch)
: lldb_private::RegisterInfoInterface(target_arch) {
assert(target_arch.GetMachine() == llvm::Triple::x86_64);
}

const RegisterInfo *RegisterContextWindows_x86_64::GetRegisterInfo() const {
return g_register_infos_x86_64;
}

uint32_t RegisterContextWindows_x86_64::GetRegisterCount() const {
return llvm::array_lengthof(g_register_infos_x86_64);
}

uint32_t RegisterContextWindows_x86_64::GetUserRegisterCount() const {
return llvm::array_lengthof(g_register_infos_x86_64);
}

size_t RegisterContextWindows_x86_64::GetGPRSize() const { return sizeof(GPR); }
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===-- RegisterContextWindows_x86_64.h --- ---------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_RegisterContextWindows_x86_64_H_
#define liblldb_RegisterContextWindows_x86_64_H_

#include "RegisterInfoInterface.h"

class RegisterContextWindows_x86_64
: public lldb_private::RegisterInfoInterface {
public:
RegisterContextWindows_x86_64(const lldb_private::ArchSpec &target_arch);

size_t GetGPRSize() const override;

const lldb_private::RegisterInfo *GetRegisterInfo() const override;

uint32_t GetRegisterCount() const override;

uint32_t GetUserRegisterCount() const override;
};

#endif
6 changes: 6 additions & 0 deletions lldb/source/Plugins/Process/Windows/Common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
add_lldb_library(lldbPluginProcessWindowsCommon PLUGIN
DebuggerThread.cpp
LocalDebugDelegate.cpp
NativeProcessWindows.cpp
NativeRegisterContextWindows.cpp
NativeRegisterContextWindows_i386.cpp
NativeRegisterContextWindows_WoW64.cpp
NativeRegisterContextWindows_x86_64.cpp
NativeThreadWindows.cpp
ProcessDebugger.cpp
ProcessWindows.cpp
ProcessWindowsLog.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
#include "llvm/Support/Threading.h"
#include "llvm/Support/raw_ostream.h"

#ifndef STATUS_WX86_BREAKPOINT
#define STATUS_WX86_BREAKPOINT 0x4000001FL // For WOW64
#endif

using namespace lldb;
using namespace lldb_private;

Expand Down Expand Up @@ -350,7 +354,8 @@ DebuggerThread::HandleExceptionEvent(const EXCEPTION_DEBUG_INFO &info,
// 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) {
(info.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT ||
info.ExceptionRecord.ExceptionCode == STATUS_WX86_BREAKPOINT)) {
LLDB_LOG(log, "Breakpoint exception is cue to detach from process {0:x}",
m_pid_to_detach.load());
::DebugActiveProcessStop(m_pid_to_detach);
Expand Down
Loading

0 comments on commit 5146a9e

Please sign in to comment.