LLDB mainline
ValueObjectRegister.cpp
Go to the documentation of this file.
1//===-- ValueObjectRegister.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
11#include "lldb/Core/Module.h"
12#include "lldb/Core/Value.h"
16#include "lldb/Target/Process.h"
19#include "lldb/Target/Target.h"
22#include "lldb/Utility/Log.h"
23#include "lldb/Utility/Scalar.h"
24#include "lldb/Utility/Status.h"
25#include "lldb/Utility/Stream.h"
26
27#include "llvm/ADT/StringRef.h"
28
29#include <cassert>
30#include <memory>
31#include <optional>
32
33namespace lldb_private {
35}
36
37using namespace lldb;
38using namespace lldb_private;
39
40#pragma mark ValueObjectRegisterSet
41
44 lldb::RegisterContextSP &reg_ctx_sp,
45 uint32_t set_idx) {
46 auto manager_sp = ValueObjectManager::Create();
47 return (new ValueObjectRegisterSet(exe_scope, *manager_sp, reg_ctx_sp,
48 set_idx))
49 ->GetSP();
50}
51
53 ValueObjectManager &manager,
55 uint32_t reg_set_idx)
56 : ValueObject(exe_scope, manager), m_reg_ctx_sp(reg_ctx),
57 m_reg_set(nullptr), m_reg_set_idx(reg_set_idx) {
58 assert(reg_ctx);
59 m_reg_set = reg_ctx->GetRegisterSet(m_reg_set_idx);
60 if (m_reg_set) {
62 }
63}
64
66
68 return CompilerType();
69}
70
72
74 return ConstString();
75}
76
77llvm::Expected<uint32_t>
79 const RegisterSet *reg_set = m_reg_ctx_sp->GetRegisterSet(m_reg_set_idx);
80 if (reg_set) {
81 auto reg_count = reg_set->num_registers;
82 return reg_count <= max ? reg_count : max;
83 }
84 return 0;
85}
86
87std::optional<uint64_t> ValueObjectRegisterSet::GetByteSize() { return 0; }
88
90 m_error.Clear();
91 SetValueDidChange(false);
93 StackFrame *frame = exe_ctx.GetFramePtr();
94 if (frame == nullptr)
95 m_reg_ctx_sp.reset();
96 else {
98 if (m_reg_ctx_sp) {
99 const RegisterSet *reg_set = m_reg_ctx_sp->GetRegisterSet(m_reg_set_idx);
100 if (reg_set == nullptr)
101 m_reg_ctx_sp.reset();
102 else if (m_reg_set != reg_set) {
103 SetValueDidChange(true);
104 m_name.SetCString(reg_set->name);
105 }
106 }
107 }
108 if (m_reg_ctx_sp) {
109 SetValueIsValid(true);
110 } else {
111 SetValueIsValid(false);
112 m_error = Status::FromErrorString("no register context");
114 }
115 return m_error.Success();
116}
117
119 if (m_reg_ctx_sp && m_reg_set) {
120 return new ValueObjectRegister(
121 *this, m_reg_ctx_sp,
122 m_reg_ctx_sp->GetRegisterInfoAtIndex(m_reg_set->registers[idx]));
123 }
124 return nullptr;
125}
126
129 bool can_create) {
130 ValueObject *valobj = nullptr;
131 if (m_reg_ctx_sp && m_reg_set) {
132 const RegisterInfo *reg_info = m_reg_ctx_sp->GetRegisterInfoByName(name);
133 if (reg_info != nullptr)
134 valobj = new ValueObjectRegister(*this, m_reg_ctx_sp, reg_info);
135 }
136 if (valobj)
137 return valobj->GetSP();
138 else
139 return ValueObjectSP();
140}
141
143 if (m_reg_ctx_sp && m_reg_set) {
144 const RegisterInfo *reg_info = m_reg_ctx_sp->GetRegisterInfoByName(name);
145 if (reg_info != nullptr)
146 return reg_info->kinds[eRegisterKindLLDB];
147 }
148 return UINT32_MAX;
149}
150
151#pragma mark -
152#pragma mark ValueObjectRegister
153
155 if (reg_info) {
156 m_reg_info = *reg_info;
157 if (reg_info->name)
158 m_name.SetCString(reg_info->name);
159 else if (reg_info->alt_name)
160 m_name.SetCString(reg_info->alt_name);
161 }
162}
163
165 lldb::RegisterContextSP &reg_ctx_sp,
166 const RegisterInfo *reg_info)
167 : ValueObject(parent), m_reg_ctx_sp(reg_ctx_sp), m_reg_info(),
168 m_reg_value(), m_type_name(), m_compiler_type() {
169 assert(reg_ctx_sp.get());
170 ConstructObject(reg_info);
171}
172
174 lldb::RegisterContextSP &reg_ctx_sp,
175 const RegisterInfo *reg_info) {
176 auto manager_sp = ValueObjectManager::Create();
177 return (new ValueObjectRegister(exe_scope, *manager_sp, reg_ctx_sp, reg_info))
178 ->GetSP();
179}
180
182 ValueObjectManager &manager,
184 const RegisterInfo *reg_info)
185 : ValueObject(exe_scope, manager), m_reg_ctx_sp(reg_ctx), m_reg_info(),
186 m_reg_value(), m_type_name(), m_compiler_type() {
187 assert(reg_ctx);
188 ConstructObject(reg_info);
189}
190
192
194 if (!m_compiler_type.IsValid()) {
196 if (auto *target = exe_ctx.GetTargetPtr()) {
197 if (auto *exe_module = target->GetExecutableModulePointer()) {
198 auto type_system_or_err =
199 exe_module->GetTypeSystemForLanguage(eLanguageTypeC);
200 if (auto err = type_system_or_err.takeError()) {
201 LLDB_LOG_ERROR(GetLog(LLDBLog::Types), std::move(err),
202 "Unable to get CompilerType from TypeSystem: {0}");
203 } else {
204 if (auto ts = *type_system_or_err)
205 m_compiler_type = ts->GetBuiltinTypeForEncodingAndBitSize(
207 }
208 }
209 }
210 }
211 return m_compiler_type;
212}
213
215 if (m_type_name.IsEmpty())
217 return m_type_name;
218}
219
220llvm::Expected<uint32_t>
223 auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
224 if (!children_count)
225 return children_count;
226 return *children_count <= max ? *children_count : max;
227}
228
229std::optional<uint64_t> ValueObjectRegister::GetByteSize() {
230 return m_reg_info.byte_size;
231}
232
234 m_error.Clear();
236 StackFrame *frame = exe_ctx.GetFramePtr();
237 if (frame == nullptr) {
238 m_reg_ctx_sp.reset();
240 }
241
242 if (m_reg_ctx_sp) {
243 RegisterValue m_old_reg_value(m_reg_value);
244 if (m_reg_ctx_sp->ReadRegister(&m_reg_info, m_reg_value)) {
246 Process *process = exe_ctx.GetProcessPtr();
247 if (process)
250 (void *)&m_reg_info);
252 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
253 SetValueIsValid(true);
254 SetValueDidChange(!(m_old_reg_value == m_reg_value));
255 return true;
256 }
257 }
258 }
259
260 SetValueIsValid(false);
261 m_error = Status::FromErrorString("no register context");
262 return false;
263}
264
266 Status &error) {
267 // The new value will be in the m_data. Copy that into our register value.
268 error =
269 m_reg_value.SetValueFromString(&m_reg_info, llvm::StringRef(value_str));
270 if (!error.Success())
271 return false;
272
273 if (!m_reg_ctx_sp->WriteRegister(&m_reg_info, m_reg_value)) {
274 error = Status::FromErrorString("unable to write back to register");
275 return false;
276 }
277
279 return true;
280}
281
283 error = m_reg_value.SetValueFromData(m_reg_info, data, 0, false);
284 if (!error.Success())
285 return false;
286
287 if (!m_reg_ctx_sp->WriteRegister(&m_reg_info, m_reg_value)) {
288 error = Status::FromErrorString("unable to write back to register");
289 return false;
290 }
291
293 return true;
294}
295
298 false)) // make sure that you are up to date before returning anything
299 return m_reg_value.GetScalarValue(scalar);
300 return false;
301}
302
304 GetExpressionPathFormat epformat) {
305 s.Printf("$%s", m_reg_info.name);
306}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:392
static std::shared_ptr< ClusterManager > Create()
Definition: SharedCluster.h:24
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
ConstString GetTypeName(bool BaseOnly=false) const
llvm::Expected< uint32_t > GetNumChildren(bool omit_empty_base_classes, const ExecutionContext *exe_ctx) const
A uniqued constant string class.
Definition: ConstString.h:40
void SetCString(const char *cstr)
Set the C string value.
bool IsEmpty() const
Test for empty string.
Definition: ConstString.h:304
An data extractor class.
Definition: DataExtractor.h:48
const uint8_t * GetDataStart() const
Get the data start pointer.
void SetAddressByteSize(uint32_t addr_size)
Set the address byte size.
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
StackFrame * GetFramePtr() const
Returns a pointer to the frame object.
Target * GetTargetPtr() const
Returns a pointer to the target object.
Process * GetProcessPtr() const
Returns a pointer to the process object.
A plug-in interface definition class for debugging a process.
Definition: Process.h:343
uint32_t GetAddressByteSize() const
Definition: Process.cpp:3615
bool GetData(DataExtractor &data) const
Status SetValueFromString(const RegisterInfo *reg_info, llvm::StringRef value_str)
bool GetScalarValue(Scalar &scalar) const
Status SetValueFromData(const RegisterInfo &reg_info, DataExtractor &data, lldb::offset_t offset, bool partial_data_ok)
This base class provides an interface to stack frames.
Definition: StackFrame.h:44
lldb::RegisterContextSP GetRegisterContext()
Get the RegisterContext for this frame, if possible.
An error handling class.
Definition: Status.h:115
void Clear()
Clear the object state.
Definition: Status.cpp:215
static Status FromErrorString(const char *str)
Definition: Status.h:138
bool Success() const
Test for success condition.
Definition: Status.cpp:280
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
ValueObjectRegisterSet(ExecutionContextScope *exe_scope, ValueObjectManager &manager, lldb::RegisterContextSP &reg_ctx_sp, uint32_t set_idx)
std::optional< uint64_t > GetByteSize() override
size_t GetIndexOfChildWithName(llvm::StringRef name) override
lldb::ValueObjectSP GetChildMemberWithName(llvm::StringRef name, bool can_create=true) override
static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, lldb::RegisterContextSP &reg_ctx_sp, uint32_t set_idx)
llvm::Expected< uint32_t > CalculateNumChildren(uint32_t max) override
Should only be called by ValueObject::GetNumChildren().
ValueObject * CreateChildAtIndex(size_t idx) override
Should only be called by ValueObject::GetChildAtIndex().
bool SetValueFromCString(const char *value_str, Status &error) override
bool SetData(DataExtractor &data, Status &error) override
static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, lldb::RegisterContextSP &reg_ctx_sp, const RegisterInfo *reg_info)
llvm::Expected< uint32_t > CalculateNumChildren(uint32_t max) override
Should only be called by ValueObject::GetNumChildren().
std::optional< uint64_t > GetByteSize() override
ValueObjectRegister(ValueObject &parent, lldb::RegisterContextSP &reg_ctx_sp, const RegisterInfo *reg_info)
lldb::RegisterContextSP m_reg_ctx_sp
CompilerType GetCompilerTypeImpl() override
void ConstructObject(const RegisterInfo *reg_info)
void GetExpressionPath(Stream &s, GetExpressionPathFormat epformat=eGetExpressionPathFormatDereferencePointers) override
bool ResolveValue(Scalar &scalar) override
void SetValueIsValid(bool valid)
Definition: ValueObject.h:1059
CompilerType GetCompilerType()
Definition: ValueObject.h:352
lldb::ValueObjectSP GetSP()
Definition: ValueObject.h:569
ChildrenManager m_children
Definition: ValueObject.h:953
Status m_error
An error object that can describe any errors that occur when updating values.
Definition: ValueObject.h:930
DataExtractor m_data
A data extractor that can be used to extract the value.
Definition: ValueObject.h:926
void SetValueDidChange(bool value_changed)
Definition: ValueObject.h:1055
bool UpdateValueIfNeeded(bool update_format=true)
ConstString m_name
The name of this object.
Definition: ValueObject.h:924
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:330
const Scalar & GetScalar() const
Definition: Value.h:112
@ HostAddress
A host address value (for memory in the process that < A is using liblldb).
void SetContext(ContextType context_type, void *p)
Definition: Value.h:96
void SetValueType(ValueType value_type)
Definition: Value.h:89
@ RegisterInfo
RegisterInfo * (can be a scalar or a vector register).
#define UINT32_MAX
Definition: lldb-defines.h:19
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:332
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:484
@ eLanguageTypeC
Non-standardized C, such as K&R.
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
Definition: lldb-forward.h:394
@ eRegisterKindLLDB
lldb's internal register numbers
Every register is described in detail including its name, alternate name (optional),...
lldb::Encoding encoding
Encoding of the register bits.
const char * alt_name
Alternate name of this register, can be NULL.
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.
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.
const char * name
Name of this register set.