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
NativeRegisterContext(NativeThreadProtocol & thread)21 NativeRegisterContext::NativeRegisterContext(NativeThreadProtocol &thread)
22 : m_thread(thread) {}
23
24 // Destructor
~NativeRegisterContext()25 NativeRegisterContext::~NativeRegisterContext() {}
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 *
GetRegisterInfoByName(llvm::StringRef reg_name,uint32_t start_idx)54 NativeRegisterContext::GetRegisterInfoByName(llvm::StringRef reg_name,
55 uint32_t start_idx) {
56 if (reg_name.empty())
57 return nullptr;
58
59 const uint32_t num_registers = GetRegisterCount();
60 for (uint32_t reg = start_idx; reg < num_registers; ++reg) {
61 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
62
63 if (reg_name.equals_lower(reg_info->name) ||
64 reg_name.equals_lower(reg_info->alt_name))
65 return reg_info;
66 }
67 return nullptr;
68 }
69
GetRegisterInfo(uint32_t kind,uint32_t num)70 const RegisterInfo *NativeRegisterContext::GetRegisterInfo(uint32_t kind,
71 uint32_t num) {
72 const uint32_t reg_num = ConvertRegisterKindToRegisterNumber(kind, num);
73 if (reg_num == LLDB_INVALID_REGNUM)
74 return nullptr;
75 return GetRegisterInfoAtIndex(reg_num);
76 }
77
GetRegisterName(uint32_t reg)78 const char *NativeRegisterContext::GetRegisterName(uint32_t reg) {
79 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
80 if (reg_info)
81 return reg_info->name;
82 return nullptr;
83 }
84
GetRegisterSetNameForRegisterAtIndex(uint32_t reg_index) const85 const char *NativeRegisterContext::GetRegisterSetNameForRegisterAtIndex(
86 uint32_t reg_index) const {
87 const RegisterInfo *const reg_info = GetRegisterInfoAtIndex(reg_index);
88 if (!reg_info)
89 return nullptr;
90
91 for (uint32_t set_index = 0; set_index < GetRegisterSetCount(); ++set_index) {
92 const RegisterSet *const reg_set = GetRegisterSet(set_index);
93 if (!reg_set)
94 continue;
95
96 for (uint32_t reg_num_index = 0; reg_num_index < reg_set->num_registers;
97 ++reg_num_index) {
98 const uint32_t reg_num = reg_set->registers[reg_num_index];
99 // FIXME double check we're checking the right register kind here.
100 if (reg_info->kinds[RegisterKind::eRegisterKindLLDB] == reg_num) {
101 // The given register is a member of this register set. Return the
102 // register set name.
103 return reg_set->name;
104 }
105 }
106 }
107
108 // Didn't find it.
109 return nullptr;
110 }
111
GetPC(lldb::addr_t fail_value)112 lldb::addr_t NativeRegisterContext::GetPC(lldb::addr_t fail_value) {
113 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
114
115 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
116 LLDB_REGNUM_GENERIC_PC);
117 LLDB_LOGF(log,
118 "NativeRegisterContext::%s using reg index %" PRIu32
119 " (default %" PRIu64 ")",
120 __FUNCTION__, reg, fail_value);
121
122 const uint64_t retval = ReadRegisterAsUnsigned(reg, fail_value);
123
124 LLDB_LOGF(log, "NativeRegisterContext::%s " PRIu32 " retval %" PRIu64,
125 __FUNCTION__, retval);
126
127 return retval;
128 }
129
130 lldb::addr_t
GetPCfromBreakpointLocation(lldb::addr_t fail_value)131 NativeRegisterContext::GetPCfromBreakpointLocation(lldb::addr_t fail_value) {
132 return GetPC(fail_value);
133 }
134
SetPC(lldb::addr_t pc)135 Status NativeRegisterContext::SetPC(lldb::addr_t pc) {
136 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
137 LLDB_REGNUM_GENERIC_PC);
138 return WriteRegisterFromUnsigned(reg, pc);
139 }
140
GetSP(lldb::addr_t fail_value)141 lldb::addr_t NativeRegisterContext::GetSP(lldb::addr_t fail_value) {
142 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
143 LLDB_REGNUM_GENERIC_SP);
144 return ReadRegisterAsUnsigned(reg, fail_value);
145 }
146
SetSP(lldb::addr_t sp)147 Status NativeRegisterContext::SetSP(lldb::addr_t sp) {
148 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
149 LLDB_REGNUM_GENERIC_SP);
150 return WriteRegisterFromUnsigned(reg, sp);
151 }
152
GetFP(lldb::addr_t fail_value)153 lldb::addr_t NativeRegisterContext::GetFP(lldb::addr_t fail_value) {
154 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
155 LLDB_REGNUM_GENERIC_FP);
156 return ReadRegisterAsUnsigned(reg, fail_value);
157 }
158
SetFP(lldb::addr_t fp)159 Status NativeRegisterContext::SetFP(lldb::addr_t fp) {
160 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
161 LLDB_REGNUM_GENERIC_FP);
162 return WriteRegisterFromUnsigned(reg, fp);
163 }
164
GetReturnAddress(lldb::addr_t fail_value)165 lldb::addr_t NativeRegisterContext::GetReturnAddress(lldb::addr_t fail_value) {
166 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
167 LLDB_REGNUM_GENERIC_RA);
168 return ReadRegisterAsUnsigned(reg, fail_value);
169 }
170
GetFlags(lldb::addr_t fail_value)171 lldb::addr_t NativeRegisterContext::GetFlags(lldb::addr_t fail_value) {
172 uint32_t reg = ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric,
173 LLDB_REGNUM_GENERIC_FLAGS);
174 return ReadRegisterAsUnsigned(reg, fail_value);
175 }
176
177 lldb::addr_t
ReadRegisterAsUnsigned(uint32_t reg,lldb::addr_t fail_value)178 NativeRegisterContext::ReadRegisterAsUnsigned(uint32_t reg,
179 lldb::addr_t fail_value) {
180 if (reg != LLDB_INVALID_REGNUM)
181 return ReadRegisterAsUnsigned(GetRegisterInfoAtIndex(reg), fail_value);
182 return fail_value;
183 }
184
185 uint64_t
ReadRegisterAsUnsigned(const RegisterInfo * reg_info,lldb::addr_t fail_value)186 NativeRegisterContext::ReadRegisterAsUnsigned(const RegisterInfo *reg_info,
187 lldb::addr_t fail_value) {
188 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
189
190 if (reg_info) {
191 RegisterValue value;
192 Status error = ReadRegister(reg_info, value);
193 if (error.Success()) {
194 LLDB_LOGF(log,
195 "NativeRegisterContext::%s ReadRegister() succeeded, value "
196 "%" PRIu64,
197 __FUNCTION__, value.GetAsUInt64());
198 return value.GetAsUInt64();
199 } else {
200 LLDB_LOGF(log,
201 "NativeRegisterContext::%s ReadRegister() failed, error %s",
202 __FUNCTION__, error.AsCString());
203 }
204 } else {
205 LLDB_LOGF(log, "NativeRegisterContext::%s ReadRegister() null reg_info",
206 __FUNCTION__);
207 }
208 return fail_value;
209 }
210
WriteRegisterFromUnsigned(uint32_t reg,uint64_t uval)211 Status NativeRegisterContext::WriteRegisterFromUnsigned(uint32_t reg,
212 uint64_t uval) {
213 if (reg == LLDB_INVALID_REGNUM)
214 return Status("NativeRegisterContext::%s (): reg is invalid", __FUNCTION__);
215 return WriteRegisterFromUnsigned(GetRegisterInfoAtIndex(reg), uval);
216 }
217
218 Status
WriteRegisterFromUnsigned(const RegisterInfo * reg_info,uint64_t uval)219 NativeRegisterContext::WriteRegisterFromUnsigned(const RegisterInfo *reg_info,
220 uint64_t uval) {
221 assert(reg_info);
222 if (!reg_info)
223 return Status("reg_info is nullptr");
224
225 RegisterValue value;
226 if (!value.SetUInt(uval, reg_info->byte_size))
227 return Status("RegisterValue::SetUInt () failed");
228
229 return WriteRegister(reg_info, value);
230 }
231
GetThreadID() const232 lldb::tid_t NativeRegisterContext::GetThreadID() const {
233 return m_thread.GetID();
234 }
235
NumSupportedHardwareBreakpoints()236 uint32_t NativeRegisterContext::NumSupportedHardwareBreakpoints() { return 0; }
237
SetHardwareBreakpoint(lldb::addr_t addr,size_t size)238 uint32_t NativeRegisterContext::SetHardwareBreakpoint(lldb::addr_t addr,
239 size_t size) {
240 return LLDB_INVALID_INDEX32;
241 }
242
ClearAllHardwareBreakpoints()243 Status NativeRegisterContext::ClearAllHardwareBreakpoints() {
244 return Status("not implemented");
245 }
246
ClearHardwareBreakpoint(uint32_t hw_idx)247 bool NativeRegisterContext::ClearHardwareBreakpoint(uint32_t hw_idx) {
248 return false;
249 }
250
GetHardwareBreakHitIndex(uint32_t & bp_index,lldb::addr_t trap_addr)251 Status NativeRegisterContext::GetHardwareBreakHitIndex(uint32_t &bp_index,
252 lldb::addr_t trap_addr) {
253 bp_index = LLDB_INVALID_INDEX32;
254 return Status("not implemented");
255 }
256
NumSupportedHardwareWatchpoints()257 uint32_t NativeRegisterContext::NumSupportedHardwareWatchpoints() { return 0; }
258
SetHardwareWatchpoint(lldb::addr_t addr,size_t size,uint32_t watch_flags)259 uint32_t NativeRegisterContext::SetHardwareWatchpoint(lldb::addr_t addr,
260 size_t size,
261 uint32_t watch_flags) {
262 return LLDB_INVALID_INDEX32;
263 }
264
ClearHardwareWatchpoint(uint32_t hw_index)265 bool NativeRegisterContext::ClearHardwareWatchpoint(uint32_t hw_index) {
266 return false;
267 }
268
ClearAllHardwareWatchpoints()269 Status NativeRegisterContext::ClearAllHardwareWatchpoints() {
270 return Status("not implemented");
271 }
272
IsWatchpointHit(uint32_t wp_index,bool & is_hit)273 Status NativeRegisterContext::IsWatchpointHit(uint32_t wp_index, bool &is_hit) {
274 is_hit = false;
275 return Status("not implemented");
276 }
277
GetWatchpointHitIndex(uint32_t & wp_index,lldb::addr_t trap_addr)278 Status NativeRegisterContext::GetWatchpointHitIndex(uint32_t &wp_index,
279 lldb::addr_t trap_addr) {
280 wp_index = LLDB_INVALID_INDEX32;
281 return Status("not implemented");
282 }
283
IsWatchpointVacant(uint32_t wp_index,bool & is_vacant)284 Status NativeRegisterContext::IsWatchpointVacant(uint32_t wp_index,
285 bool &is_vacant) {
286 is_vacant = false;
287 return Status("not implemented");
288 }
289
GetWatchpointAddress(uint32_t wp_index)290 lldb::addr_t NativeRegisterContext::GetWatchpointAddress(uint32_t wp_index) {
291 return LLDB_INVALID_ADDRESS;
292 }
293
GetWatchpointHitAddress(uint32_t wp_index)294 lldb::addr_t NativeRegisterContext::GetWatchpointHitAddress(uint32_t wp_index) {
295 return LLDB_INVALID_ADDRESS;
296 }
297
HardwareSingleStep(bool enable)298 bool NativeRegisterContext::HardwareSingleStep(bool enable) { return false; }
299
ReadRegisterValueFromMemory(const RegisterInfo * reg_info,lldb::addr_t src_addr,size_t src_len,RegisterValue & reg_value)300 Status NativeRegisterContext::ReadRegisterValueFromMemory(
301 const RegisterInfo *reg_info, lldb::addr_t src_addr, size_t src_len,
302 RegisterValue ®_value) {
303 Status error;
304 if (reg_info == nullptr) {
305 error.SetErrorString("invalid register info argument.");
306 return error;
307 }
308
309 // Moving from addr into a register
310 //
311 // Case 1: src_len == dst_len
312 //
313 // |AABBCCDD| Address contents
314 // |AABBCCDD| Register contents
315 //
316 // Case 2: src_len > dst_len
317 //
318 // Status! (The register should always be big enough to hold the data)
319 //
320 // Case 3: src_len < dst_len
321 //
322 // |AABB| Address contents
323 // |AABB0000| Register contents [on little-endian hardware]
324 // |0000AABB| Register contents [on big-endian hardware]
325 if (src_len > RegisterValue::kMaxRegisterByteSize) {
326 error.SetErrorString("register too small to receive memory data");
327 return error;
328 }
329
330 const size_t dst_len = reg_info->byte_size;
331
332 if (src_len > dst_len) {
333 error.SetErrorStringWithFormat(
334 "%" PRIu64 " bytes is too big to store in register %s (%" PRIu64
335 " bytes)",
336 static_cast<uint64_t>(src_len), reg_info->name,
337 static_cast<uint64_t>(dst_len));
338 return error;
339 }
340
341 NativeProcessProtocol &process = m_thread.GetProcess();
342 uint8_t src[RegisterValue::kMaxRegisterByteSize];
343
344 // Read the memory
345 size_t bytes_read;
346 error = process.ReadMemory(src_addr, src, src_len, bytes_read);
347 if (error.Fail())
348 return error;
349
350 // Make sure the memory read succeeded...
351 if (bytes_read != src_len) {
352 // This might happen if we read _some_ bytes but not all
353 error.SetErrorStringWithFormat("read %" PRIu64 " of %" PRIu64 " bytes",
354 static_cast<uint64_t>(bytes_read),
355 static_cast<uint64_t>(src_len));
356 return error;
357 }
358
359 // We now have a memory buffer that contains the part or all of the register
360 // value. Set the register value using this memory data.
361 // TODO: we might need to add a parameter to this function in case the byte
362 // order of the memory data doesn't match the process. For now we are
363 // assuming they are the same.
364 reg_value.SetFromMemoryData(reg_info, src, src_len, process.GetByteOrder(),
365 error);
366
367 return error;
368 }
369
WriteRegisterValueToMemory(const RegisterInfo * reg_info,lldb::addr_t dst_addr,size_t dst_len,const RegisterValue & reg_value)370 Status NativeRegisterContext::WriteRegisterValueToMemory(
371 const RegisterInfo *reg_info, lldb::addr_t dst_addr, size_t dst_len,
372 const RegisterValue ®_value) {
373
374 uint8_t dst[RegisterValue::kMaxRegisterByteSize];
375
376 Status error;
377
378 NativeProcessProtocol &process = m_thread.GetProcess();
379
380 // TODO: we might need to add a parameter to this function in case the byte
381 // order of the memory data doesn't match the process. For now we are
382 // assuming they are the same.
383 const size_t bytes_copied = reg_value.GetAsMemoryData(
384 reg_info, dst, dst_len, process.GetByteOrder(), error);
385
386 if (error.Success()) {
387 if (bytes_copied == 0) {
388 error.SetErrorString("byte copy failed.");
389 } else {
390 size_t bytes_written;
391 error = process.WriteMemory(dst_addr, dst, bytes_copied, bytes_written);
392 if (error.Fail())
393 return error;
394
395 if (bytes_written != bytes_copied) {
396 // This might happen if we read _some_ bytes but not all
397 error.SetErrorStringWithFormat("only wrote %" PRIu64 " of %" PRIu64
398 " bytes",
399 static_cast<uint64_t>(bytes_written),
400 static_cast<uint64_t>(bytes_copied));
401 }
402 }
403 }
404
405 return error;
406 }
407
408 uint32_t
ConvertRegisterKindToRegisterNumber(uint32_t kind,uint32_t num) const409 NativeRegisterContext::ConvertRegisterKindToRegisterNumber(uint32_t kind,
410 uint32_t num) const {
411 const uint32_t num_regs = GetRegisterCount();
412
413 assert(kind < kNumRegisterKinds);
414 for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx) {
415 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg_idx);
416
417 if (reg_info->kinds[kind] == num)
418 return reg_idx;
419 }
420
421 return LLDB_INVALID_REGNUM;
422 }
423