aboutsummaryrefslogtreecommitdiff
path: root/source/Plugins/Process/Windows/Common/x64/RegisterContextWindows_x64.cpp
blob: 103cff4a2a5639c0a71a75f685ee4af5b5e49ccc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
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 &reg_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 &reg_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);
}