LLDB mainline
RegisterContext.cpp
Go to the documentation of this file.
1//===-- RegisterContext.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
10#include "lldb/Core/Module.h"
11#include "lldb/Core/Value.h"
14#include "lldb/Target/Process.h"
16#include "lldb/Target/Target.h"
17#include "lldb/Target/Thread.h"
19#include "lldb/Utility/Endian.h"
21#include "lldb/Utility/Scalar.h"
22
23using namespace lldb;
24using namespace lldb_private;
25
26RegisterContext::RegisterContext(Thread &thread, uint32_t concrete_frame_idx)
27 : m_thread(thread), m_concrete_frame_idx(concrete_frame_idx),
28 m_stop_id(thread.GetProcess()->GetStopID()) {}
29
31
33 ProcessSP process_sp(m_thread.GetProcess());
34 bool invalidate = force;
35 uint32_t process_stop_id = UINT32_MAX;
36
37 if (process_sp)
38 process_stop_id = process_sp->GetStopID();
39 else
40 invalidate = true;
41
42 if (!invalidate)
43 invalidate = process_stop_id != GetStopID();
44
45 if (invalidate) {
47 SetStopID(process_stop_id);
48 }
49}
50
51const RegisterInfo *
53 uint32_t start_idx) {
54 if (reg_name.empty())
55 return nullptr;
56
57 // Generic register names take precedence over specific register names.
58 // For example, on x86 we want "sp" to refer to the complete RSP/ESP register
59 // rather than the 16-bit SP pseudo-register.
60 uint32_t generic_reg = Args::StringToGenericRegister(reg_name);
61 if (generic_reg != LLDB_INVALID_REGNUM) {
62 const RegisterInfo *reg_info =
64 if (reg_info)
65 return reg_info;
66 }
67
68 const uint32_t num_registers = GetRegisterCount();
69 for (uint32_t reg = start_idx; reg < num_registers; ++reg) {
70 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
71
72 if (reg_name.equals_insensitive(reg_info->name) ||
73 reg_name.equals_insensitive(reg_info->alt_name))
74 return reg_info;
75 }
76
77 return nullptr;
78}
79
81 uint32_t num) {
82 const uint32_t reg_num = ConvertRegisterKindToRegisterNumber(kind, num);
83 if (reg_num == LLDB_INVALID_REGNUM)
84 return nullptr;
85 return GetRegisterInfoAtIndex(reg_num);
86}
87
88const char *RegisterContext::GetRegisterName(uint32_t reg) {
89 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
90 if (reg_info)
91 return reg_info->name;
92 return nullptr;
93}
94
95uint64_t RegisterContext::GetPC(uint64_t fail_value) {
98 uint64_t pc = ReadRegisterAsUnsigned(reg, fail_value);
99
100 if (pc != fail_value) {
101 TargetSP target_sp = m_thread.CalculateTarget();
102 if (target_sp) {
103 Target *target = target_sp.get();
104 if (target)
106 }
107 }
108
109 return pc;
110}
111
112uint64_t RegisterContext::GetThreadPointer(uint64_t fail_value) {
115 return ReadRegisterAsUnsigned(reg, fail_value);
116}
117
121 bool success = WriteRegisterFromUnsigned(reg, pc);
122 if (success) {
123 StackFrameSP frame_sp(
125 if (frame_sp)
126 frame_sp->ChangePC(pc);
127 else
129 }
130 return success;
131}
132
136 return false;
137 TargetSP target_sp = m_thread.CalculateTarget();
138 if (!target_sp.get())
139 return false;
140
141 if (!BehavesLikeZerothFrame() && pc != 0)
142 pc--;
143 address.SetLoadAddress(pc, target_sp.get());
144 return true;
145}
146
148 TargetSP target_sp = m_thread.CalculateTarget();
149 Target *target = target_sp.get();
150
151 lldb::addr_t callAddr = addr.GetCallableLoadAddress(target);
152 if (callAddr == LLDB_INVALID_ADDRESS)
153 return false;
154
155 return SetPC(callAddr);
156}
157
158uint64_t RegisterContext::GetSP(uint64_t fail_value) {
161 return ReadRegisterAsUnsigned(reg, fail_value);
162}
163
167 return WriteRegisterFromUnsigned(reg, sp);
168}
169
170uint64_t RegisterContext::GetFP(uint64_t fail_value) {
173 return ReadRegisterAsUnsigned(reg, fail_value);
174}
175
179 return WriteRegisterFromUnsigned(reg, fp);
180}
181
182uint64_t RegisterContext::GetReturnAddress(uint64_t fail_value) {
185 return ReadRegisterAsUnsigned(reg, fail_value);
186}
187
188uint64_t RegisterContext::GetFlags(uint64_t fail_value) {
191 return ReadRegisterAsUnsigned(reg, fail_value);
192}
193
195 uint64_t fail_value) {
196 if (reg != LLDB_INVALID_REGNUM)
197 return ReadRegisterAsUnsigned(GetRegisterInfoAtIndex(reg), fail_value);
198 return fail_value;
199}
200
202 uint64_t fail_value) {
203 if (reg_info) {
204 RegisterValue value;
205 if (ReadRegister(reg_info, value))
206 return value.GetAsUInt64();
207 }
208 return fail_value;
209}
210
211bool RegisterContext::WriteRegisterFromUnsigned(uint32_t reg, uint64_t uval) {
212 if (reg == LLDB_INVALID_REGNUM)
213 return false;
215}
216
218 uint64_t uval) {
219 if (reg_info) {
220 RegisterValue value;
221 if (value.SetUInt(uval, reg_info->byte_size))
222 return WriteRegister(reg_info, value);
223 }
224 return false;
225}
226
228 uint32_t num_register_sets = context->GetRegisterSetCount();
229 // We don't know that two threads have the same register context, so require
230 // the threads to be the same.
231 if (context->GetThreadID() != GetThreadID())
232 return false;
233
234 if (num_register_sets != GetRegisterSetCount())
235 return false;
236
237 RegisterContextSP frame_zero_context = m_thread.GetRegisterContext();
238
239 for (uint32_t set_idx = 0; set_idx < num_register_sets; ++set_idx) {
240 const RegisterSet *const reg_set = GetRegisterSet(set_idx);
241
242 const uint32_t num_registers = reg_set->num_registers;
243 for (uint32_t reg_idx = 0; reg_idx < num_registers; ++reg_idx) {
244 const uint32_t reg = reg_set->registers[reg_idx];
245 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
246 if (!reg_info || reg_info->value_regs)
247 continue;
248 RegisterValue reg_value;
249
250 // If we can reconstruct the register from the frame we are copying from,
251 // then do so, otherwise use the value from frame 0.
252 if (context->ReadRegister(reg_info, reg_value)) {
253 WriteRegister(reg_info, reg_value);
254 } else if (frame_zero_context->ReadRegister(reg_info, reg_value)) {
255 WriteRegister(reg_info, reg_value);
256 }
257 }
258 }
259 return true;
260}
261
263
265
267 size_t size) {
269}
270
271// Used when parsing DWARF and EH frame information and any other object file
272// sections that contain register numbers in them.
273uint32_t
275 uint32_t num) {
276 const uint32_t num_regs = GetRegisterCount();
277
278 assert(kind < kNumRegisterKinds);
279 for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx) {
280 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg_idx);
281
282 if (reg_info->kinds[kind] == num)
283 return reg_idx;
284 }
285
286 return LLDB_INVALID_REGNUM;
287}
288
289bool RegisterContext::ClearHardwareBreakpoint(uint32_t hw_idx) { return false; }
290
292
294 bool read, bool write) {
296}
297
299 return false;
300}
301
302bool RegisterContext::HardwareSingleStep(bool enable) { return false; }
303
305 const RegisterInfo *reg_info, lldb::addr_t src_addr, uint32_t src_len,
306 RegisterValue &reg_value) {
308 if (reg_info == nullptr) {
309 error.SetErrorString("invalid register info argument.");
310 return error;
311 }
312
313 // Moving from addr into a register
314 //
315 // Case 1: src_len == dst_len
316 //
317 // |AABBCCDD| Address contents
318 // |AABBCCDD| Register contents
319 //
320 // Case 2: src_len > dst_len
321 //
322 // Status! (The register should always be big enough to hold the data)
323 //
324 // Case 3: src_len < dst_len
325 //
326 // |AABB| Address contents
327 // |AABB0000| Register contents [on little-endian hardware]
328 // |0000AABB| Register contents [on big-endian hardware]
329 const uint32_t dst_len = reg_info->byte_size;
330
331 if (src_len > dst_len) {
332 error.SetErrorStringWithFormat(
333 "%u bytes is too big to store in register %s (%u bytes)", src_len,
334 reg_info->name, dst_len);
335 return error;
336 }
337
338 ProcessSP process_sp(m_thread.GetProcess());
339 if (process_sp) {
341
342 // Read the memory
343 const uint32_t bytes_read =
344 process_sp->ReadMemory(src_addr, src.data(), src_len, error);
345
346 // Make sure the memory read succeeded...
347 if (bytes_read != src_len) {
348 if (error.Success()) {
349 // This might happen if we read _some_ bytes but not all
350 error.SetErrorStringWithFormat("read %u of %u bytes", bytes_read,
351 src_len);
352 }
353 return error;
354 }
355
356 // We now have a memory buffer that contains the part or all of the
357 // register value. Set the register value using this memory data.
358 // TODO: we might need to add a parameter to this function in case the byte
359 // order of the memory data doesn't match the process. For now we are
360 // assuming they are the same.
361 reg_value.SetFromMemoryData(*reg_info, src.data(), src_len,
362 process_sp->GetByteOrder(), error);
363 } else
364 error.SetErrorString("invalid process");
365
366 return error;
367}
368
370 const RegisterInfo *reg_info, lldb::addr_t dst_addr, uint32_t dst_len,
371 const RegisterValue &reg_value) {
373 ProcessSP process_sp(m_thread.GetProcess());
374
375 if (!process_sp) {
376 error.SetErrorString("invalid process");
377 return error;
378 }
379
380 if (reg_info == nullptr) {
381 error.SetErrorString("Invalid register info argument.");
382 return error;
383 }
384
385 // TODO: we might need to add a parameter to this function in case the byte
386 // order of the memory data doesn't match the process. For now we are
387 // assuming they are the same.
389 const uint32_t bytes_copied = reg_value.GetAsMemoryData(
390 *reg_info, dst.data(), dst_len, process_sp->GetByteOrder(), error);
391
392 if (error.Success()) {
393 if (bytes_copied == 0) {
394 error.SetErrorString("byte copy failed.");
395 } else {
396 const uint32_t bytes_written =
397 process_sp->WriteMemory(dst_addr, dst.data(), bytes_copied, error);
398 if (bytes_written != bytes_copied) {
399 if (error.Success()) {
400 // This might happen if we read _some_ bytes but not all
401 error.SetErrorStringWithFormat("only wrote %u of %u bytes",
402 bytes_written, bytes_copied);
403 }
404 }
405 }
406 }
407
408 return error;
409}
410
412 // Get the target process whose privileged thread was used for the register
413 // read.
415 lldb_private::Process *process = CalculateProcess().get();
416
417 if (process)
418 byte_order = process->GetByteOrder();
419 return byte_order;
420}
421
423 lldb_private::RegisterCheckpoint &reg_checkpoint) {
424 return ReadAllRegisterValues(reg_checkpoint.GetData());
425}
426
428 const lldb_private::RegisterCheckpoint &reg_checkpoint) {
429 return WriteAllRegisterValues(reg_checkpoint.GetData());
430}
431
433 return m_thread.CalculateTarget();
434}
435
437 return m_thread.CalculateProcess();
438}
439
441 return m_thread.shared_from_this();
442}
443
445 // Register contexts might belong to many frames if we have inlined functions
446 // inside a frame since all inlined functions share the same registers, so we
447 // can't definitively say which frame we come from...
448 return StackFrameSP();
449}
450
453}
454
456 uint32_t source_regnum,
457 lldb::RegisterKind target_rk,
458 uint32_t &target_regnum) {
459 const uint32_t num_registers = GetRegisterCount();
460 for (uint32_t reg = 0; reg < num_registers; ++reg) {
461 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
462
463 if (reg_info->kinds[source_rk] == source_regnum) {
464 target_regnum = reg_info->kinds[target_rk];
465 return (target_regnum != LLDB_INVALID_REGNUM);
466 }
467 }
468 return false;
469}
static llvm::raw_ostream & error(Stream &strm)
A section + offset based address class.
Definition: Address.h:62
lldb::addr_t GetCallableLoadAddress(Target *target, bool is_indirect=false) const
Get the load address as a callable code load address.
Definition: Address.cpp:338
bool SetLoadAddress(lldb::addr_t load_addr, Target *target, bool allow_section_end=false)
Set the address to represent load_addr.
Definition: Address.cpp:1046
static uint32_t StringToGenericRegister(llvm::StringRef s)
Definition: Args.cpp:431
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A plug-in interface definition class for debugging a process.
Definition: Process.h:341
lldb::ByteOrder GetByteOrder() const
Definition: Process.cpp:3400
lldb::WritableDataBufferSP & GetData()
virtual uint32_t NumSupportedHardwareBreakpoints()
bool ConvertBetweenRegisterKinds(lldb::RegisterKind source_rk, uint32_t source_regnum, lldb::RegisterKind target_rk, uint32_t &target_regnum)
virtual void InvalidateAllRegisters()=0
virtual bool HardwareSingleStep(bool enable)
virtual bool WriteAllRegisterValues(const lldb::DataBufferSP &data_sp)
virtual uint32_t SetHardwareBreakpoint(lldb::addr_t addr, size_t size)
virtual const RegisterSet * GetRegisterSet(size_t reg_set)=0
virtual bool ClearHardwareBreakpoint(uint32_t hw_idx)
void CalculateExecutionContext(ExecutionContext &exe_ctx) override
Reconstruct the object's execution context into sc.
virtual uint32_t SetHardwareWatchpoint(lldb::addr_t addr, size_t size, bool read, bool write)
virtual bool ClearHardwareWatchpoint(uint32_t hw_index)
bool CopyFromRegisterContext(lldb::RegisterContextSP context)
virtual lldb::tid_t GetThreadID() const
virtual bool BehavesLikeZerothFrame() const
Indicates that this frame is currently executing code, that the PC value is not a return-pc but an ac...
virtual Status ReadRegisterValueFromMemory(const lldb_private::RegisterInfo *reg_info, lldb::addr_t src_addr, uint32_t src_len, RegisterValue &reg_value)
virtual uint32_t ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind, uint32_t num)
Convert from a given register numbering scheme to the lldb register numbering scheme.
bool GetPCForSymbolication(Address &address)
Get an address suitable for symbolication.
lldb::StackFrameSP CalculateStackFrame() override
uint64_t GetPC(uint64_t fail_value=LLDB_INVALID_ADDRESS)
uint64_t GetThreadPointer(uint64_t fail_value=LLDB_INVALID_ADDRESS)
uint64_t GetReturnAddress(uint64_t fail_value=LLDB_INVALID_ADDRESS)
uint64_t ReadRegisterAsUnsigned(uint32_t reg, uint64_t fail_value)
virtual const RegisterInfo * GetRegisterInfoAtIndex(size_t reg)=0
uint64_t GetSP(uint64_t fail_value=LLDB_INVALID_ADDRESS)
const char * GetRegisterName(uint32_t reg)
virtual bool WriteRegister(const RegisterInfo *reg_info, const RegisterValue &reg_value)=0
uint64_t GetFlags(uint64_t fail_value=0)
RegisterContext(Thread &thread, uint32_t concrete_frame_idx)
void SetStopID(uint32_t stop_id)
lldb::TargetSP CalculateTarget() override
const RegisterInfo * GetRegisterInfo(lldb::RegisterKind reg_kind, uint32_t reg_num)
virtual size_t GetRegisterCount()=0
lldb::ProcessSP CalculateProcess() override
virtual uint32_t NumSupportedHardwareWatchpoints()
virtual size_t GetRegisterSetCount()=0
virtual Status WriteRegisterValueToMemory(const lldb_private::RegisterInfo *reg_info, lldb::addr_t dst_addr, uint32_t dst_len, const RegisterValue &reg_value)
virtual bool ReadAllRegisterValues(lldb::WritableDataBufferSP &data_sp)
bool WriteRegisterFromUnsigned(uint32_t reg, uint64_t uval)
lldb::ThreadSP CalculateThread() override
uint64_t GetFP(uint64_t fail_value=LLDB_INVALID_ADDRESS)
const RegisterInfo * GetRegisterInfoByName(llvm::StringRef reg_name, uint32_t start_idx=0)
virtual lldb::ByteOrder GetByteOrder()
virtual bool ReadRegister(const RegisterInfo *reg_info, RegisterValue &reg_value)=0
uint32_t SetFromMemoryData(const RegisterInfo &reg_info, const void *src, uint32_t src_len, lldb::ByteOrder src_byte_order, Status &error)
bool SetUInt(uint64_t uint, uint32_t byte_size)
uint32_t GetAsMemoryData(const RegisterInfo &reg_info, void *dst, uint32_t dst_len, lldb::ByteOrder dst_byte_order, Status &error) const
uint64_t GetAsUInt64(uint64_t fail_value=UINT64_MAX, bool *success_ptr=nullptr) const
llvm::SmallVector< uint8_t, kTypicalRegisterByteSize > BytesContainer
Definition: RegisterValue.h:41
An error handling class.
Definition: Status.h:44
lldb::addr_t GetOpcodeLoadAddress(lldb::addr_t load_addr, AddressClass addr_class=AddressClass::eInvalid) const
Get load_addr as an opcode for this target.
Definition: Target.cpp:2811
virtual lldb::RegisterContextSP GetRegisterContext()=0
void CalculateExecutionContext(ExecutionContext &exe_ctx) override
Reconstruct the object's execution context into sc.
Definition: Thread.cpp:1404
lldb::ProcessSP CalculateProcess() override
Definition: Thread.cpp:1398
virtual void ClearStackFrames()
Definition: Thread.cpp:1422
lldb::TargetSP CalculateTarget() override
Definition: Thread.cpp:1390
lldb::ProcessSP GetProcess() const
Definition: Thread.h:154
virtual lldb::StackFrameSP GetFrameWithConcreteFrameIndex(uint32_t unwind_idx)
Definition: Thread.cpp:1442
#define LLDB_REGNUM_GENERIC_RA
Definition: lldb-defines.h:59
#define LLDB_INVALID_INDEX32
Definition: lldb-defines.h:83
#define LLDB_REGNUM_GENERIC_SP
Definition: lldb-defines.h:57
#define LLDB_REGNUM_GENERIC_FLAGS
Definition: lldb-defines.h:60
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
#define UINT32_MAX
Definition: lldb-defines.h:19
#define LLDB_INVALID_REGNUM
Definition: lldb-defines.h:87
#define LLDB_REGNUM_GENERIC_TP
Definition: lldb-defines.h:77
#define LLDB_REGNUM_GENERIC_PC
Definition: lldb-defines.h:56
#define LLDB_REGNUM_GENERIC_FP
Definition: lldb-defines.h:58
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
Definition: lldb-forward.h:412
std::shared_ptr< lldb_private::Thread > ThreadSP
Definition: lldb-forward.h:438
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:381
ByteOrder
Byte ordering definitions.
@ eByteOrderInvalid
uint64_t addr_t
Definition: lldb-types.h:79
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:436
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
Definition: lldb-forward.h:386
uint64_t tid_t
Definition: lldb-types.h:82
RegisterKind
Register numbering types.
@ eRegisterKindGeneric
insn ptr reg, stack ptr reg, etc not specific to any particular target
@ kNumRegisterKinds
Every register is described in detail including its name, alternate name (optional),...
const char * alt_name
Alternate name of this register, can be NULL.
uint32_t * value_regs
List of registers (terminated with LLDB_INVALID_REGNUM).
uint32_t byte_size
Size in bytes of the register.
uint32_t kinds[lldb::kNumRegisterKinds]
Holds all of the various register numbers for all register kinds.
llvm::ArrayRef< uint8_t > data(const uint8_t *context_base) const
const char * name
Name of this register, can't be NULL.
Registers are grouped into register sets.
size_t num_registers
The number of registers in REGISTERS array below.
const uint32_t * registers
An array of register indices in this set.
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:47