Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Host/common/NativeRegisterContext.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- NativeRegisterContext.cpp -----------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "lldb/Host/common/NativeRegisterContext.h"
10
11
#include "lldb/Utility/Log.h"
12
#include "lldb/Utility/RegisterValue.h"
13
14
#include "lldb/Host/PosixApi.h"
15
#include "lldb/Host/common/NativeProcessProtocol.h"
16
#include "lldb/Host/common/NativeThreadProtocol.h"
17
18
using namespace lldb;
19
using namespace lldb_private;
20
21
NativeRegisterContext::NativeRegisterContext(NativeThreadProtocol &thread)
22
0
    : m_thread(thread) {}
23
24
// Destructor
25
0
NativeRegisterContext::~NativeRegisterContext() = default;
26
27
// FIXME revisit invalidation, process stop ids, etc.  Right now we don't
28
// support caching in NativeRegisterContext.  We can do this later by utilizing
29
// NativeProcessProtocol::GetStopID () and adding a stop id to
30
// NativeRegisterContext.
31
32
// void
33
// NativeRegisterContext::InvalidateIfNeeded (bool force) {
34
//     ProcessSP process_sp (m_thread.GetProcess());
35
//     bool invalidate = force;
36
//     uint32_t process_stop_id = UINT32_MAX;
37
38
//     if (process_sp)
39
//         process_stop_id = process_sp->GetStopID();
40
//     else
41
//         invalidate = true;
42
43
//     if (!invalidate)
44
//         invalidate = process_stop_id != GetStopID();
45
46
//     if (invalidate)
47
//     {
48
//         InvalidateAllRegisters ();
49
//         SetStopID (process_stop_id);
50
//     }
51
// }
52
53
const RegisterInfo *
54
NativeRegisterContext::GetRegisterInfoByName(llvm::StringRef reg_name,
55
0
                                             uint32_t start_idx) {
56
0
  if (reg_name.empty())
57
0
    return nullptr;
58
59
  // Generic register names take precedence over specific register names.
60
  // For example, on x86 we want "sp" to refer to the complete RSP/ESP register
61
  // rather than the 16-bit SP pseudo-register.
62
0
  uint32_t generic_reg = Args::StringToGenericRegister(reg_name);
63
0
  if (generic_reg != LLDB_INVALID_REGNUM) {
64
0
    const RegisterInfo *reg_info =
65
0
        GetRegisterInfo(eRegisterKindGeneric, generic_reg);
66
0
    if (reg_info)
67
0
      return reg_info;
68
0
  }
69
70
0
  const uint32_t num_registers = GetRegisterCount();
71
0
  for (uint32_t reg = start_idx; reg < num_registers; ++reg) {
72
0
    const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
73
74
0
    if (reg_name.equals_insensitive(reg_info->name) ||
75
0
        reg_name.equals_insensitive(reg_info->alt_name))
76
0
      return reg_info;
77
0
  }
78
79
0
  return nullptr;
80
0
}
81
82
const RegisterInfo *NativeRegisterContext::GetRegisterInfo(uint32_t kind,
83
0
                                                           uint32_t num) {
84
0
  const uint32_t reg_num = ConvertRegisterKindToRegisterNumber(kind, num);
85
0
  if (reg_num == LLDB_INVALID_REGNUM)
86
0
    return nullptr;
87
0
  return GetRegisterInfoAtIndex(reg_num);
88
0
}
89
90
0
const char *NativeRegisterContext::GetRegisterName(uint32_t reg) {
91
0
  const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
92
0
  if (reg_info)
93
0
    return reg_info->name;
94
0
  return nullptr;
95
0
}
96
97
const char *NativeRegisterContext::GetRegisterSetNameForRegisterAtIndex(
98
0
    uint32_t reg_index) const {
99
0
  const RegisterInfo *const reg_info = GetRegisterInfoAtIndex(reg_index);
100
0
  if (!reg_info)
101
0
    return nullptr;
102
103
0
  for (uint32_t set_index = 0; set_index < GetRegisterSetCount(); ++set_index) {
104
0
    const RegisterSet *const reg_set = GetRegisterSet(set_index);
105
0
    if (!reg_set)
106
0
      continue;
107
108
0
    for (uint32_t reg_num_index = 0; reg_num_index < reg_set->num_registers;
109
0
         ++reg_num_index) {
110
0
      const uint32_t reg_num = reg_set->registers[reg_num_index];
111
      // FIXME double check we're checking the right register kind here.
112
0
      if (reg_info->kinds[RegisterKind::eRegisterKindLLDB] == reg_num) {
113
        // The given register is a member of this register set.  Return the
114
        // register set name.
115
0
        return reg_set->name;
116
0
      }
117
0
    }
118
0
  }
119
120
  // Didn't find it.
121
0
  return nullptr;
122
0
}
123
124
0
lldb::addr_t NativeRegisterContext::GetPC(lldb::addr_t fail_value) {
125
0
  Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
126
127
0
  uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
128
0
                                                     LLDB_REGNUM_GENERIC_PC);
129
0
  LLDB_LOGF(log,
130
0
            "NativeRegisterContext::%s using reg index %" PRIu32
131
0
            " (default %" PRIu64 ")",
132
0
            __FUNCTION__, reg, fail_value);
133
134
0
  const uint64_t retval = ReadRegisterAsUnsigned(reg, fail_value);
135
136
0
  LLDB_LOGF(log, "NativeRegisterContext::%s " PRIu32 " retval %" PRIu64,
137
0
            __FUNCTION__, retval);
138
139
0
  return retval;
140
0
}
141
142
lldb::addr_t
143
0
NativeRegisterContext::GetPCfromBreakpointLocation(lldb::addr_t fail_value) {
144
0
  return GetPC(fail_value);
145
0
}
146
147
0
Status NativeRegisterContext::SetPC(lldb::addr_t pc) {
148
0
  uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
149
0
                                                     LLDB_REGNUM_GENERIC_PC);
150
0
  return WriteRegisterFromUnsigned(reg, pc);
151
0
}
152
153
0
lldb::addr_t NativeRegisterContext::GetSP(lldb::addr_t fail_value) {
154
0
  uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
155
0
                                                     LLDB_REGNUM_GENERIC_SP);
156
0
  return ReadRegisterAsUnsigned(reg, fail_value);
157
0
}
158
159
0
Status NativeRegisterContext::SetSP(lldb::addr_t sp) {
160
0
  uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
161
0
                                                     LLDB_REGNUM_GENERIC_SP);
162
0
  return WriteRegisterFromUnsigned(reg, sp);
163
0
}
164
165
0
lldb::addr_t NativeRegisterContext::GetFP(lldb::addr_t fail_value) {
166
0
  uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
167
0
                                                     LLDB_REGNUM_GENERIC_FP);
168
0
  return ReadRegisterAsUnsigned(reg, fail_value);
169
0
}
170
171
0
Status NativeRegisterContext::SetFP(lldb::addr_t fp) {
172
0
  uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
173
0
                                                     LLDB_REGNUM_GENERIC_FP);
174
0
  return WriteRegisterFromUnsigned(reg, fp);
175
0
}
176
177
0
lldb::addr_t NativeRegisterContext::GetReturnAddress(lldb::addr_t fail_value) {
178
0
  uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
179
0
                                                     LLDB_REGNUM_GENERIC_RA);
180
0
  return ReadRegisterAsUnsigned(reg, fail_value);
181
0
}
182
183
0
lldb::addr_t NativeRegisterContext::GetFlags(lldb::addr_t fail_value) {
184
0
  uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
185
0
                                                     LLDB_REGNUM_GENERIC_FLAGS);
186
0
  return ReadRegisterAsUnsigned(reg, fail_value);
187
0
}
188
189
lldb::addr_t
190
NativeRegisterContext::ReadRegisterAsUnsigned(uint32_t reg,
191
0
                                              lldb::addr_t fail_value) {
192
0
  if (reg != LLDB_INVALID_REGNUM)
193
0
    return ReadRegisterAsUnsigned(GetRegisterInfoAtIndex(reg), fail_value);
194
0
  return fail_value;
195
0
}
196
197
uint64_t
198
NativeRegisterContext::ReadRegisterAsUnsigned(const RegisterInfo *reg_info,
199
0
                                              lldb::addr_t fail_value) {
200
0
  Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
201
202
0
  if (reg_info) {
203
0
    RegisterValue value;
204
0
    Status error = ReadRegister(reg_info, value);
205
0
    if (error.Success()) {
206
0
      LLDB_LOGF(log,
207
0
                "NativeRegisterContext::%s ReadRegister() succeeded, value "
208
0
                "%" PRIu64,
209
0
                __FUNCTION__, value.GetAsUInt64());
210
0
      return value.GetAsUInt64();
211
0
    } else {
212
0
      LLDB_LOGF(log,
213
0
                "NativeRegisterContext::%s ReadRegister() failed, error %s",
214
0
                __FUNCTION__, error.AsCString());
215
0
    }
216
0
  } else {
217
0
    LLDB_LOGF(log, "NativeRegisterContext::%s ReadRegister() null reg_info",
218
0
              __FUNCTION__);
219
0
  }
220
0
  return fail_value;
221
0
}
222
223
Status NativeRegisterContext::WriteRegisterFromUnsigned(uint32_t reg,
224
0
                                                        uint64_t uval) {
225
0
  if (reg == LLDB_INVALID_REGNUM)
226
0
    return Status("NativeRegisterContext::%s (): reg is invalid", __FUNCTION__);
227
0
  return WriteRegisterFromUnsigned(GetRegisterInfoAtIndex(reg), uval);
228
0
}
229
230
Status
231
NativeRegisterContext::WriteRegisterFromUnsigned(const RegisterInfo *reg_info,
232
0
                                                 uint64_t uval) {
233
0
  assert(reg_info);
234
0
  if (!reg_info)
235
0
    return Status("reg_info is nullptr");
236
237
0
  RegisterValue value;
238
0
  if (!value.SetUInt(uval, reg_info->byte_size))
239
0
    return Status("RegisterValue::SetUInt () failed");
240
241
0
  return WriteRegister(reg_info, value);
242
0
}
243
244
0
lldb::tid_t NativeRegisterContext::GetThreadID() const {
245
0
  return m_thread.GetID();
246
0
}
247
248
0
uint32_t NativeRegisterContext::NumSupportedHardwareBreakpoints() { return 0; }
249
250
uint32_t NativeRegisterContext::SetHardwareBreakpoint(lldb::addr_t addr,
251
0
                                                      size_t size) {
252
0
  return LLDB_INVALID_INDEX32;
253
0
}
254
255
0
Status NativeRegisterContext::ClearAllHardwareBreakpoints() {
256
0
  return Status("not implemented");
257
0
}
258
259
0
bool NativeRegisterContext::ClearHardwareBreakpoint(uint32_t hw_idx) {
260
0
  return false;
261
0
}
262
263
Status NativeRegisterContext::GetHardwareBreakHitIndex(uint32_t &bp_index,
264
0
                                                       lldb::addr_t trap_addr) {
265
0
  bp_index = LLDB_INVALID_INDEX32;
266
0
  return Status("not implemented");
267
0
}
268
269
0
uint32_t NativeRegisterContext::NumSupportedHardwareWatchpoints() { return 0; }
270
271
uint32_t NativeRegisterContext::SetHardwareWatchpoint(lldb::addr_t addr,
272
                                                      size_t size,
273
0
                                                      uint32_t watch_flags) {
274
0
  return LLDB_INVALID_INDEX32;
275
0
}
276
277
0
bool NativeRegisterContext::ClearHardwareWatchpoint(uint32_t hw_index) {
278
0
  return false;
279
0
}
280
281
0
Status NativeRegisterContext::ClearWatchpointHit(uint32_t hw_index) {
282
0
  return Status("not implemented");
283
0
}
284
285
0
Status NativeRegisterContext::ClearAllHardwareWatchpoints() {
286
0
  return Status("not implemented");
287
0
}
288
289
0
Status NativeRegisterContext::IsWatchpointHit(uint32_t wp_index, bool &is_hit) {
290
0
  is_hit = false;
291
0
  return Status("not implemented");
292
0
}
293
294
Status NativeRegisterContext::GetWatchpointHitIndex(uint32_t &wp_index,
295
0
                                                    lldb::addr_t trap_addr) {
296
0
  wp_index = LLDB_INVALID_INDEX32;
297
0
  return Status("not implemented");
298
0
}
299
300
Status NativeRegisterContext::IsWatchpointVacant(uint32_t wp_index,
301
0
                                                 bool &is_vacant) {
302
0
  is_vacant = false;
303
0
  return Status("not implemented");
304
0
}
305
306
0
lldb::addr_t NativeRegisterContext::GetWatchpointAddress(uint32_t wp_index) {
307
0
  return LLDB_INVALID_ADDRESS;
308
0
}
309
310
0
lldb::addr_t NativeRegisterContext::GetWatchpointHitAddress(uint32_t wp_index) {
311
0
  return LLDB_INVALID_ADDRESS;
312
0
}
313
314
0
bool NativeRegisterContext::HardwareSingleStep(bool enable) { return false; }
315
316
Status NativeRegisterContext::ReadRegisterValueFromMemory(
317
    const RegisterInfo *reg_info, lldb::addr_t src_addr, size_t src_len,
318
0
    RegisterValue &reg_value) {
319
0
  Status error;
320
0
  if (reg_info == nullptr) {
321
0
    error.SetErrorString("invalid register info argument.");
322
0
    return error;
323
0
  }
324
325
  // Moving from addr into a register
326
  //
327
  // Case 1: src_len == dst_len
328
  //
329
  //   |AABBCCDD| Address contents
330
  //   |AABBCCDD| Register contents
331
  //
332
  // Case 2: src_len > dst_len
333
  //
334
  //   Status!  (The register should always be big enough to hold the data)
335
  //
336
  // Case 3: src_len < dst_len
337
  //
338
  //   |AABB| Address contents
339
  //   |AABB0000| Register contents [on little-endian hardware]
340
  //   |0000AABB| Register contents [on big-endian hardware]
341
0
  if (src_len > RegisterValue::kMaxRegisterByteSize) {
342
0
    error.SetErrorString("register too small to receive memory data");
343
0
    return error;
344
0
  }
345
346
0
  const size_t dst_len = reg_info->byte_size;
347
348
0
  if (src_len > dst_len) {
349
0
    error.SetErrorStringWithFormat(
350
0
        "%" PRIu64 " bytes is too big to store in register %s (%" PRIu64
351
0
        " bytes)",
352
0
        static_cast<uint64_t>(src_len), reg_info->name,
353
0
        static_cast<uint64_t>(dst_len));
354
0
    return error;
355
0
  }
356
357
0
  NativeProcessProtocol &process = m_thread.GetProcess();
358
0
  uint8_t src[RegisterValue::kMaxRegisterByteSize];
359
360
  // Read the memory
361
0
  size_t bytes_read;
362
0
  error = process.ReadMemory(src_addr, src, src_len, bytes_read);
363
0
  if (error.Fail())
364
0
    return error;
365
366
  // Make sure the memory read succeeded...
367
0
  if (bytes_read != src_len) {
368
    // This might happen if we read _some_ bytes but not all
369
0
    error.SetErrorStringWithFormat("read %" PRIu64 " of %" PRIu64 " bytes",
370
0
                                   static_cast<uint64_t>(bytes_read),
371
0
                                   static_cast<uint64_t>(src_len));
372
0
    return error;
373
0
  }
374
375
  // We now have a memory buffer that contains the part or all of the register
376
  // value. Set the register value using this memory data.
377
  // TODO: we might need to add a parameter to this function in case the byte
378
  // order of the memory data doesn't match the process. For now we are
379
  // assuming they are the same.
380
0
  reg_value.SetFromMemoryData(reg_info, src, src_len, process.GetByteOrder(),
381
0
                              error);
382
383
0
  return error;
384
0
}
385
386
Status NativeRegisterContext::WriteRegisterValueToMemory(
387
    const RegisterInfo *reg_info, lldb::addr_t dst_addr, size_t dst_len,
388
0
    const RegisterValue &reg_value) {
389
390
0
  uint8_t dst[RegisterValue::kMaxRegisterByteSize];
391
392
0
  Status error;
393
394
0
  NativeProcessProtocol &process = m_thread.GetProcess();
395
396
  // TODO: we might need to add a parameter to this function in case the byte
397
  // order of the memory data doesn't match the process. For now we are
398
  // assuming they are the same.
399
0
  const size_t bytes_copied = reg_value.GetAsMemoryData(
400
0
      reg_info, dst, dst_len, process.GetByteOrder(), error);
401
402
0
  if (error.Success()) {
403
0
    if (bytes_copied == 0) {
404
0
      error.SetErrorString("byte copy failed.");
405
0
    } else {
406
0
      size_t bytes_written;
407
0
      error = process.WriteMemory(dst_addr, dst, bytes_copied, bytes_written);
408
0
      if (error.Fail())
409
0
        return error;
410
411
0
      if (bytes_written != bytes_copied) {
412
        // This might happen if we read _some_ bytes but not all
413
0
        error.SetErrorStringWithFormat("only wrote %" PRIu64 " of %" PRIu64
414
0
                                       " bytes",
415
0
                                       static_cast<uint64_t>(bytes_written),
416
0
                                       static_cast<uint64_t>(bytes_copied));
417
0
      }
418
0
    }
419
0
  }
420
421
0
  return error;
422
0
}
423
424
uint32_t
425
NativeRegisterContext::ConvertRegisterKindToRegisterNumber(uint32_t kind,
426
0
                                                           uint32_t num) const {
427
0
  const uint32_t num_regs = GetRegisterCount();
428
429
0
  assert(kind < kNumRegisterKinds);
430
0
  for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx) {
431
0
    const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg_idx);
432
433
0
    if (reg_info->kinds[kind] == num)
434
0
      return reg_idx;
435
0
  }
436
437
0
  return LLDB_INVALID_REGNUM;
438
0
}
439
440
std::vector<uint32_t>
441
0
NativeRegisterContext::GetExpeditedRegisters(ExpeditedRegs expType) const {
442
0
  if (expType == ExpeditedRegs::Minimal) {
443
    // Expedite only a minimum set of important generic registers.
444
0
    static const uint32_t k_expedited_registers[] = {
445
0
        LLDB_REGNUM_GENERIC_PC, LLDB_REGNUM_GENERIC_SP, LLDB_REGNUM_GENERIC_FP,
446
0
        LLDB_REGNUM_GENERIC_RA};
447
448
0
    std::vector<uint32_t> expedited_reg_nums;
449
0
    for (uint32_t gen_reg : k_expedited_registers) {
450
0
      uint32_t reg_num =
451
0
          ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, gen_reg);
452
0
      if (reg_num == LLDB_INVALID_REGNUM)
453
0
        continue; // Target does not support the given register.
454
0
      else
455
0
        expedited_reg_nums.push_back(reg_num);
456
0
    }
457
458
0
    return expedited_reg_nums;
459
0
  }
460
461
0
  if (GetRegisterSetCount() > 0 && expType == ExpeditedRegs::Full)
462
0
    return std::vector<uint32_t>(GetRegisterSet(0)->registers,
463
0
                                 GetRegisterSet(0)->registers +
464
0
                                     GetRegisterSet(0)->num_registers);
465
466
0
  return std::vector<uint32_t>();
467
0
}