1 //===-- RegisterContext.cpp -----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Target/RegisterContext.h"
10 #include "lldb/Core/Module.h"
11 #include "lldb/Core/Value.h"
12 #include "lldb/Expression/DWARFExpression.h"
13 #include "lldb/Target/ExecutionContext.h"
14 #include "lldb/Target/Process.h"
15 #include "lldb/Target/StackFrame.h"
16 #include "lldb/Target/Target.h"
17 #include "lldb/Target/Thread.h"
18 #include "lldb/Utility/DataExtractor.h"
19 #include "lldb/Utility/Endian.h"
20 #include "lldb/Utility/RegisterValue.h"
21 #include "lldb/Utility/Scalar.h"
24 using namespace lldb_private
;
26 RegisterContext::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()) {}
30 RegisterContext::~RegisterContext() = default;
32 void RegisterContext::InvalidateIfNeeded(bool force
) {
33 ProcessSP
process_sp(m_thread
.GetProcess());
34 bool invalidate
= force
;
35 uint32_t process_stop_id
= UINT32_MAX
;
38 process_stop_id
= process_sp
->GetStopID();
43 invalidate
= process_stop_id
!= GetStopID();
46 InvalidateAllRegisters();
47 SetStopID(process_stop_id
);
52 RegisterContext::GetRegisterInfoByName(llvm::StringRef reg_name
,
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
=
63 GetRegisterInfo(eRegisterKindGeneric
, generic_reg
);
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
);
72 if (reg_name
.equals_insensitive(reg_info
->name
) ||
73 reg_name
.equals_insensitive(reg_info
->alt_name
))
80 const RegisterInfo
*RegisterContext::GetRegisterInfo(lldb::RegisterKind kind
,
82 const uint32_t reg_num
= ConvertRegisterKindToRegisterNumber(kind
, num
);
83 if (reg_num
== LLDB_INVALID_REGNUM
)
85 return GetRegisterInfoAtIndex(reg_num
);
88 const char *RegisterContext::GetRegisterName(uint32_t reg
) {
89 const RegisterInfo
*reg_info
= GetRegisterInfoAtIndex(reg
);
91 return reg_info
->name
;
95 uint64_t RegisterContext::GetPC(uint64_t fail_value
) {
96 uint32_t reg
= ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric
,
97 LLDB_REGNUM_GENERIC_PC
);
98 uint64_t pc
= ReadRegisterAsUnsigned(reg
, fail_value
);
100 if (pc
!= fail_value
) {
101 TargetSP target_sp
= m_thread
.CalculateTarget();
103 Target
*target
= target_sp
.get();
105 pc
= target
->GetOpcodeLoadAddress(pc
, AddressClass::eCode
);
112 bool RegisterContext::SetPC(uint64_t pc
) {
113 uint32_t reg
= ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric
,
114 LLDB_REGNUM_GENERIC_PC
);
115 bool success
= WriteRegisterFromUnsigned(reg
, pc
);
117 StackFrameSP
frame_sp(
118 m_thread
.GetFrameWithConcreteFrameIndex(m_concrete_frame_idx
));
120 frame_sp
->ChangePC(pc
);
122 m_thread
.ClearStackFrames();
127 bool RegisterContext::GetPCForSymbolication(Address
&address
) {
128 addr_t pc
= GetPC(LLDB_INVALID_ADDRESS
);
129 if (pc
== LLDB_INVALID_ADDRESS
)
131 TargetSP target_sp
= m_thread
.CalculateTarget();
132 if (!target_sp
.get())
135 if (!BehavesLikeZerothFrame() && pc
!= 0)
137 address
.SetLoadAddress(pc
, target_sp
.get());
141 bool RegisterContext::SetPC(Address addr
) {
142 TargetSP target_sp
= m_thread
.CalculateTarget();
143 Target
*target
= target_sp
.get();
145 lldb::addr_t callAddr
= addr
.GetCallableLoadAddress(target
);
146 if (callAddr
== LLDB_INVALID_ADDRESS
)
149 return SetPC(callAddr
);
152 uint64_t RegisterContext::GetSP(uint64_t fail_value
) {
153 uint32_t reg
= ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric
,
154 LLDB_REGNUM_GENERIC_SP
);
155 return ReadRegisterAsUnsigned(reg
, fail_value
);
158 bool RegisterContext::SetSP(uint64_t sp
) {
159 uint32_t reg
= ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric
,
160 LLDB_REGNUM_GENERIC_SP
);
161 return WriteRegisterFromUnsigned(reg
, sp
);
164 uint64_t RegisterContext::GetFP(uint64_t fail_value
) {
165 uint32_t reg
= ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric
,
166 LLDB_REGNUM_GENERIC_FP
);
167 return ReadRegisterAsUnsigned(reg
, fail_value
);
170 bool RegisterContext::SetFP(uint64_t fp
) {
171 uint32_t reg
= ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric
,
172 LLDB_REGNUM_GENERIC_FP
);
173 return WriteRegisterFromUnsigned(reg
, fp
);
176 uint64_t RegisterContext::GetReturnAddress(uint64_t fail_value
) {
177 uint32_t reg
= ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric
,
178 LLDB_REGNUM_GENERIC_RA
);
179 return ReadRegisterAsUnsigned(reg
, fail_value
);
182 uint64_t RegisterContext::GetFlags(uint64_t fail_value
) {
183 uint32_t reg
= ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric
,
184 LLDB_REGNUM_GENERIC_FLAGS
);
185 return ReadRegisterAsUnsigned(reg
, fail_value
);
188 uint64_t RegisterContext::ReadRegisterAsUnsigned(uint32_t reg
,
189 uint64_t fail_value
) {
190 if (reg
!= LLDB_INVALID_REGNUM
)
191 return ReadRegisterAsUnsigned(GetRegisterInfoAtIndex(reg
), fail_value
);
195 uint64_t RegisterContext::ReadRegisterAsUnsigned(const RegisterInfo
*reg_info
,
196 uint64_t fail_value
) {
199 if (ReadRegister(reg_info
, value
))
200 return value
.GetAsUInt64();
205 bool RegisterContext::WriteRegisterFromUnsigned(uint32_t reg
, uint64_t uval
) {
206 if (reg
== LLDB_INVALID_REGNUM
)
208 return WriteRegisterFromUnsigned(GetRegisterInfoAtIndex(reg
), uval
);
211 bool RegisterContext::WriteRegisterFromUnsigned(const RegisterInfo
*reg_info
,
215 if (value
.SetUInt(uval
, reg_info
->byte_size
))
216 return WriteRegister(reg_info
, value
);
221 bool 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())
228 if (num_register_sets
!= GetRegisterSetCount())
231 RegisterContextSP frame_zero_context
= m_thread
.GetRegisterContext();
233 for (uint32_t set_idx
= 0; set_idx
< num_register_sets
; ++set_idx
) {
234 const RegisterSet
*const reg_set
= GetRegisterSet(set_idx
);
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
)
242 RegisterValue reg_value
;
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
);
256 lldb::tid_t
RegisterContext::GetThreadID() const { return m_thread
.GetID(); }
258 uint32_t RegisterContext::NumSupportedHardwareBreakpoints() { return 0; }
260 uint32_t RegisterContext::SetHardwareBreakpoint(lldb::addr_t addr
,
262 return LLDB_INVALID_INDEX32
;
265 // Used when parsing DWARF and EH frame information and any other object file
266 // sections that contain register numbers in them.
268 RegisterContext::ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind
,
270 const uint32_t num_regs
= GetRegisterCount();
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
);
276 if (reg_info
->kinds
[kind
] == num
)
280 return LLDB_INVALID_REGNUM
;
283 bool RegisterContext::ClearHardwareBreakpoint(uint32_t hw_idx
) { return false; }
285 uint32_t RegisterContext::NumSupportedHardwareWatchpoints() { return 0; }
287 uint32_t RegisterContext::SetHardwareWatchpoint(lldb::addr_t addr
, size_t size
,
288 bool read
, bool write
) {
289 return LLDB_INVALID_INDEX32
;
292 bool RegisterContext::ClearHardwareWatchpoint(uint32_t hw_index
) {
296 bool RegisterContext::HardwareSingleStep(bool enable
) { return false; }
298 Status
RegisterContext::ReadRegisterValueFromMemory(
299 const RegisterInfo
*reg_info
, lldb::addr_t src_addr
, uint32_t src_len
,
300 RegisterValue
®_value
) {
302 if (reg_info
== nullptr) {
303 error
.SetErrorString("invalid register info argument.");
307 // Moving from addr into a register
309 // Case 1: src_len == dst_len
311 // |AABBCCDD| Address contents
312 // |AABBCCDD| Register contents
314 // Case 2: src_len > dst_len
316 // Status! (The register should always be big enough to hold the data)
318 // Case 3: src_len < dst_len
320 // |AABB| Address contents
321 // |AABB0000| Register contents [on little-endian hardware]
322 // |0000AABB| Register contents [on big-endian hardware]
323 if (src_len
> RegisterValue::kMaxRegisterByteSize
) {
324 error
.SetErrorString("register too small to receive memory data");
328 const uint32_t dst_len
= reg_info
->byte_size
;
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
);
337 ProcessSP
process_sp(m_thread
.GetProcess());
339 uint8_t src
[RegisterValue::kMaxRegisterByteSize
];
342 const uint32_t bytes_read
=
343 process_sp
->ReadMemory(src_addr
, src
, src_len
, error
);
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
,
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
);
363 error
.SetErrorString("invalid process");
368 Status
RegisterContext::WriteRegisterValueToMemory(
369 const RegisterInfo
*reg_info
, lldb::addr_t dst_addr
, uint32_t dst_len
,
370 const RegisterValue
®_value
) {
371 uint8_t dst
[RegisterValue::kMaxRegisterByteSize
];
375 ProcessSP
process_sp(m_thread
.GetProcess());
378 // TODO: we might need to add a parameter to this function in case the byte
379 // order of the memory data doesn't match the process. For now we are
380 // assuming they are the same.
382 const uint32_t bytes_copied
= reg_value
.GetAsMemoryData(
383 reg_info
, dst
, dst_len
, process_sp
->GetByteOrder(), error
);
385 if (error
.Success()) {
386 if (bytes_copied
== 0) {
387 error
.SetErrorString("byte copy failed.");
389 const uint32_t bytes_written
=
390 process_sp
->WriteMemory(dst_addr
, dst
, bytes_copied
, error
);
391 if (bytes_written
!= bytes_copied
) {
392 if (error
.Success()) {
393 // This might happen if we read _some_ bytes but not all
394 error
.SetErrorStringWithFormat("only wrote %u of %u bytes",
395 bytes_written
, bytes_copied
);
401 error
.SetErrorString("invalid process");
406 lldb::ByteOrder
RegisterContext::GetByteOrder() {
407 // Get the target process whose privileged thread was used for the register
409 lldb::ByteOrder byte_order
= lldb::eByteOrderInvalid
;
410 lldb_private::Process
*process
= CalculateProcess().get();
413 byte_order
= process
->GetByteOrder();
417 bool RegisterContext::ReadAllRegisterValues(
418 lldb_private::RegisterCheckpoint
®_checkpoint
) {
419 return ReadAllRegisterValues(reg_checkpoint
.GetData());
422 bool RegisterContext::WriteAllRegisterValues(
423 const lldb_private::RegisterCheckpoint
®_checkpoint
) {
424 return WriteAllRegisterValues(reg_checkpoint
.GetData());
427 TargetSP
RegisterContext::CalculateTarget() {
428 return m_thread
.CalculateTarget();
431 ProcessSP
RegisterContext::CalculateProcess() {
432 return m_thread
.CalculateProcess();
435 ThreadSP
RegisterContext::CalculateThread() {
436 return m_thread
.shared_from_this();
439 StackFrameSP
RegisterContext::CalculateStackFrame() {
440 // Register contexts might belong to many frames if we have inlined functions
441 // inside a frame since all inlined functions share the same registers, so we
442 // can't definitively say which frame we come from...
443 return StackFrameSP();
446 void RegisterContext::CalculateExecutionContext(ExecutionContext
&exe_ctx
) {
447 m_thread
.CalculateExecutionContext(exe_ctx
);
450 bool RegisterContext::ConvertBetweenRegisterKinds(lldb::RegisterKind source_rk
,
451 uint32_t source_regnum
,
452 lldb::RegisterKind target_rk
,
453 uint32_t &target_regnum
) {
454 const uint32_t num_registers
= GetRegisterCount();
455 for (uint32_t reg
= 0; reg
< num_registers
; ++reg
) {
456 const RegisterInfo
*reg_info
= GetRegisterInfoAtIndex(reg
);
458 if (reg_info
->kinds
[source_rk
] == source_regnum
) {
459 target_regnum
= reg_info
->kinds
[target_rk
];
460 return (target_regnum
!= LLDB_INVALID_REGNUM
);