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
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
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
115 bool success = WriteRegisterFromUnsigned(reg, pc);
116 if (success) {
117 StackFrameSP frame_sp(
119 if (frame_sp)
120 frame_sp->ChangePC(pc);
121 else
123 }
124 return success;
125}
126
130 return false;
131 TargetSP target_sp = m_thread.CalculateTarget();
132 if (!target_sp.get())
133 return false;
134
135 if (!BehavesLikeZerothFrame() && pc != 0)
136 pc--;
137 address.SetLoadAddress(pc, target_sp.get());
138 return true;
139}
140
142 TargetSP target_sp = m_thread.CalculateTarget();
143 Target *target = target_sp.get();
144
145 lldb::addr_t callAddr = addr.GetCallableLoadAddress(target);
146 if (callAddr == LLDB_INVALID_ADDRESS)
147 return false;
148
149 return SetPC(callAddr);
150}
151
152uint64_t RegisterContext::GetSP(uint64_t fail_value) {
155 return ReadRegisterAsUnsigned(reg, fail_value);
156}
157
161 return WriteRegisterFromUnsigned(reg, sp);
162}
163
164uint64_t RegisterContext::GetFP(uint64_t fail_value) {
167 return ReadRegisterAsUnsigned(reg, fail_value);
168}
169
173 return WriteRegisterFromUnsigned(reg, fp);
174}
175
176uint64_t RegisterContext::GetReturnAddress(uint64_t fail_value) {
179 return ReadRegisterAsUnsigned(reg, fail_value);
180}
181
182uint64_t RegisterContext::GetFlags(uint64_t fail_value) {
185 return ReadRegisterAsUnsigned(reg, fail_value);
186}
187
189 uint64_t fail_value) {
190 if (reg != LLDB_INVALID_REGNUM)
191 return ReadRegisterAsUnsigned(GetRegisterInfoAtIndex(reg), fail_value);
192 return fail_value;
193}
194
195uint64_t RegisterContext::ReadRegisterAsUnsigned(const RegisterInfo *reg_info,
196 uint64_t fail_value) {
197 if (reg_info) {
198 RegisterValue value;
199 if (ReadRegister(reg_info, value))
200 return value.GetAsUInt64();
201 }
202 return fail_value;
203}
204
206 if (reg == LLDB_INVALID_REGNUM)
207 return false;
209}
210
211bool RegisterContext::WriteRegisterFromUnsigned(const RegisterInfo *reg_info,
212 uint64_t uval) {
213 if (reg_info) {
214 RegisterValue value;
215 if (value.SetUInt(uval, reg_info->byte_size))
216 return WriteRegister(reg_info, value);
217 }
218 return false;
219}
220
221bool RegisterContext::CopyFromRegisterContext(lldb::RegisterContextSP context) {
222 uint32_t num_register_sets = context->GetRegisterSetCount();
223 // We don't know that two threads have the same register context, so require
224 // the threads to be the same.
225 if (context->GetThreadID() != GetThreadID())
226 return false;
227
228 if (num_register_sets != GetRegisterSetCount())
229 return false;
230
231 RegisterContextSP frame_zero_context = m_thread.GetRegisterContext();
232
233 for (uint32_t set_idx = 0; set_idx < num_register_sets; ++set_idx) {
234 const RegisterSet *const reg_set = GetRegisterSet(set_idx);
235
236 const uint32_t num_registers = reg_set->num_registers;
237 for (uint32_t reg_idx = 0; reg_idx < num_registers; ++reg_idx) {
238 const uint32_t reg = reg_set->registers[reg_idx];
239 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
240 if (!reg_info || reg_info->value_regs)
241 continue;
242 RegisterValue reg_value;
243
244 // If we can reconstruct the register from the frame we are copying from,
245 // then do so, otherwise use the value from frame 0.
246 if (context->ReadRegister(reg_info, reg_value)) {
247 WriteRegister(reg_info, reg_value);
248 } else if (frame_zero_context->ReadRegister(reg_info, reg_value)) {
249 WriteRegister(reg_info, reg_value);
250 }
251 }
252 }
253 return true;
254}
255
257
259
261 size_t size) {
263}
264
265// Used when parsing DWARF and EH frame information and any other object file
266// sections that contain register numbers in them.
269 uint32_t num) {
270 const uint32_t num_regs = GetRegisterCount();
271
272 assert(kind < kNumRegisterKinds);
273 for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx) {
274 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg_idx);
275
276 if (reg_info->kinds[kind] == num)
277 return reg_idx;
278 }
279
280 return LLDB_INVALID_REGNUM;
281}
282
284
286
288 bool read, bool write) {
290}
291
293 return false;
294}
295
296bool RegisterContext::HardwareSingleStep(bool enable) { return false; }
297
299 const RegisterInfo *reg_info, lldb::addr_t src_addr, uint32_t src_len,
300 RegisterValue &reg_value) {
302 if (reg_info == nullptr) {
303 error.SetErrorString("invalid register info argument.");
304 return error;
305 }
306
307 // Moving from addr into a register
308 //
309 // Case 1: src_len == dst_len
310 //
311 // |AABBCCDD| Address contents
312 // |AABBCCDD| Register contents
313 //
314 // Case 2: src_len > dst_len
315 //
316 // Status! (The register should always be big enough to hold the data)
317 //
318 // Case 3: src_len < dst_len
319 //
320 // |AABB| Address contents
321 // |AABB0000| Register contents [on little-endian hardware]
322 // |0000AABB| Register contents [on big-endian hardware]
324 error.SetErrorString("register too small to receive memory data");
325 return error;
326 }
327
328 const uint32_t dst_len = reg_info->byte_size;
329
330 if (src_len > dst_len) {
331 error.SetErrorStringWithFormat(
332 "%u bytes is too big to store in register %s (%u bytes)", src_len,
333 reg_info->name, dst_len);
334 return error;
335 }
336
337 ProcessSP process_sp(m_thread.GetProcess());
338 if (process_sp) {
340
341 // Read the memory
342 const uint32_t bytes_read =
343 process_sp->ReadMemory(src_addr, src, src_len, error);
344
345 // Make sure the memory read succeeded...
346 if (bytes_read != src_len) {
347 if (error.Success()) {
348 // This might happen if we read _some_ bytes but not all
349 error.SetErrorStringWithFormat("read %u of %u bytes", bytes_read,
350 src_len);
351 }
352 return error;
353 }
354
355 // We now have a memory buffer that contains the part or all of the
356 // register value. Set the register value using this memory data.
357 // TODO: we might need to add a parameter to this function in case the byte
358 // order of the memory data doesn't match the process. For now we are
359 // assuming they are the same.
360 reg_value.SetFromMemoryData(*reg_info, src, src_len,
361 process_sp->GetByteOrder(), error);
362 } else
363 error.SetErrorString("invalid process");
364
365 return error;
366}
367
369 const RegisterInfo *reg_info, lldb::addr_t dst_addr, uint32_t dst_len,
370 const RegisterValue &reg_value) {
372 ProcessSP process_sp(m_thread.GetProcess());
373
374 if (!process_sp) {
375 error.SetErrorString("invalid process");
376 return error;
377 }
378
379 if (reg_info == nullptr) {
380 error.SetErrorString("Invalid register info argument.");
381 return error;
382 }
383
384 // TODO: we might need to add a parameter to this function in case the byte
385 // order of the memory data doesn't match the process. For now we are
386 // assuming they are the same.
388 const uint32_t bytes_copied = reg_value.GetAsMemoryData(
389 *reg_info, dst, dst_len, process_sp->GetByteOrder(), error);
390
391 if (error.Success()) {
392 if (bytes_copied == 0) {
393 error.SetErrorString("byte copy failed.");
394 } else {
395 const uint32_t bytes_written =
396 process_sp->WriteMemory(dst_addr, dst, bytes_copied, error);
397 if (bytes_written != bytes_copied) {
398 if (error.Success()) {
399 // This might happen if we read _some_ bytes but not all
400 error.SetErrorStringWithFormat("only wrote %u of %u bytes",
401 bytes_written, bytes_copied);
402 }
403 }
404 }
405 }
406
407 return error;
408}
409
411 // Get the target process whose privileged thread was used for the register
412 // read.
414 lldb_private::Process *process = CalculateProcess().get();
415
416 if (process)
417 byte_order = process->GetByteOrder();
418 return byte_order;
419}
420
422 lldb_private::RegisterCheckpoint &reg_checkpoint) {
423 return ReadAllRegisterValues(reg_checkpoint.GetData());
424}
425
427 const lldb_private::RegisterCheckpoint &reg_checkpoint) {
428 return WriteAllRegisterValues(reg_checkpoint.GetData());
429}
430
432 return m_thread.CalculateTarget();
433}
434
436 return m_thread.CalculateProcess();
437}
438
440 return m_thread.shared_from_this();
441}
442
444 // Register contexts might belong to many frames if we have inlined functions
445 // inside a frame since all inlined functions share the same registers, so we
446 // can't definitively say which frame we come from...
447 return StackFrameSP();
448}
449
452}
453
455 uint32_t source_regnum,
456 lldb::RegisterKind target_rk,
457 uint32_t &target_regnum) {
458 const uint32_t num_registers = GetRegisterCount();
459 for (uint32_t reg = 0; reg < num_registers; ++reg) {
460 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg);
461
462 if (reg_info->kinds[source_rk] == source_regnum) {
463 target_regnum = reg_info->kinds[target_rk];
464 return (target_regnum != LLDB_INVALID_REGNUM);
465 }
466 }
467 return false;
468}
static llvm::raw_ostream & error(Stream &strm)
A section + offset based address class.
Definition: Address.h:59
lldb::addr_t GetCallableLoadAddress(Target *target, bool is_indirect=false) const
Get the load address as a callable code load address.
Definition: Address.cpp:336
bool SetLoadAddress(lldb::addr_t load_addr, Target *target, bool allow_section_end=false)
Set the address to represent load_addr.
Definition: Address.cpp:1040
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:335
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 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
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:2709
virtual lldb::RegisterContextSP GetRegisterContext()=0
void CalculateExecutionContext(ExecutionContext &exe_ctx) override
Reconstruct the object's execution context into sc.
Definition: Thread.cpp:1396
lldb::ProcessSP CalculateProcess() override
Definition: Thread.cpp:1390
virtual void ClearStackFrames()
Definition: Thread.cpp:1410
lldb::TargetSP CalculateTarget() override
Definition: Thread.cpp:1382
lldb::ProcessSP GetProcess() const
Definition: Thread.h:153
virtual lldb::StackFrameSP GetFrameWithConcreteFrameIndex(uint32_t unwind_idx)
Definition: Thread.cpp:1428
#define LLDB_REGNUM_GENERIC_RA
Definition: lldb-defines.h:54
#define LLDB_INVALID_INDEX32
Definition: lldb-defines.h:75
#define LLDB_REGNUM_GENERIC_SP
Definition: lldb-defines.h:52
#define LLDB_REGNUM_GENERIC_FLAGS
Definition: lldb-defines.h:55
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:74
#define UINT32_MAX
Definition: lldb-defines.h:19
#define LLDB_INVALID_REGNUM
Definition: lldb-defines.h:79
#define LLDB_REGNUM_GENERIC_PC
Definition: lldb-defines.h:51
#define LLDB_REGNUM_GENERIC_FP
Definition: lldb-defines.h:53
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
ByteOrder
Byte ordering definitions.
@ eByteOrderInvalid
uint64_t addr_t
Definition: lldb-types.h:79
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
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:47