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);
114 }
115 return m_error.Success();
116}
117
119 size_t idx, bool synthetic_array_member, int32_t synthetic_index) {
120 ValueObject *valobj = nullptr;
121 if (m_reg_ctx_sp && m_reg_set) {
122 uint32_t num_children = GetNumChildrenIgnoringErrors();
123 if (idx < num_children)
124 valobj = new ValueObjectRegister(
125 *this, m_reg_ctx_sp,
126 m_reg_ctx_sp->GetRegisterInfoAtIndex(m_reg_set->registers[idx]));
127 }
128 return valobj;
129}
130
133 bool can_create) {
134 ValueObject *valobj = nullptr;
135 if (m_reg_ctx_sp && m_reg_set) {
136 const RegisterInfo *reg_info = m_reg_ctx_sp->GetRegisterInfoByName(name);
137 if (reg_info != nullptr)
138 valobj = new ValueObjectRegister(*this, m_reg_ctx_sp, reg_info);
139 }
140 if (valobj)
141 return valobj->GetSP();
142 else
143 return ValueObjectSP();
144}
145
147 if (m_reg_ctx_sp && m_reg_set) {
148 const RegisterInfo *reg_info = m_reg_ctx_sp->GetRegisterInfoByName(name);
149 if (reg_info != nullptr)
150 return reg_info->kinds[eRegisterKindLLDB];
151 }
152 return UINT32_MAX;
153}
154
155#pragma mark -
156#pragma mark ValueObjectRegister
157
159 if (reg_info) {
160 m_reg_info = *reg_info;
161 if (reg_info->name)
162 m_name.SetCString(reg_info->name);
163 else if (reg_info->alt_name)
164 m_name.SetCString(reg_info->alt_name);
165 }
166}
167
169 lldb::RegisterContextSP &reg_ctx_sp,
170 const RegisterInfo *reg_info)
171 : ValueObject(parent), m_reg_ctx_sp(reg_ctx_sp), m_reg_info(),
172 m_reg_value(), m_type_name(), m_compiler_type() {
173 assert(reg_ctx_sp.get());
174 ConstructObject(reg_info);
175}
176
178 lldb::RegisterContextSP &reg_ctx_sp,
179 const RegisterInfo *reg_info) {
180 auto manager_sp = ValueObjectManager::Create();
181 return (new ValueObjectRegister(exe_scope, *manager_sp, reg_ctx_sp, reg_info))
182 ->GetSP();
183}
184
186 ValueObjectManager &manager,
188 const RegisterInfo *reg_info)
189 : ValueObject(exe_scope, manager), m_reg_ctx_sp(reg_ctx), m_reg_info(),
190 m_reg_value(), m_type_name(), m_compiler_type() {
191 assert(reg_ctx);
192 ConstructObject(reg_info);
193}
194
196
198 if (!m_compiler_type.IsValid()) {
200 if (auto *target = exe_ctx.GetTargetPtr()) {
201 if (auto *exe_module = target->GetExecutableModulePointer()) {
202 auto type_system_or_err =
203 exe_module->GetTypeSystemForLanguage(eLanguageTypeC);
204 if (auto err = type_system_or_err.takeError()) {
205 LLDB_LOG_ERROR(GetLog(LLDBLog::Types), std::move(err),
206 "Unable to get CompilerType from TypeSystem: {0}");
207 } else {
208 if (auto ts = *type_system_or_err)
209 m_compiler_type = ts->GetBuiltinTypeForEncodingAndBitSize(
211 }
212 }
213 }
214 }
215 return m_compiler_type;
216}
217
219 if (m_type_name.IsEmpty())
221 return m_type_name;
222}
223
224llvm::Expected<uint32_t>
227 auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
228 if (!children_count)
229 return children_count;
230 return *children_count <= max ? *children_count : max;
231}
232
233std::optional<uint64_t> ValueObjectRegister::GetByteSize() {
234 return m_reg_info.byte_size;
235}
236
238 m_error.Clear();
240 StackFrame *frame = exe_ctx.GetFramePtr();
241 if (frame == nullptr) {
242 m_reg_ctx_sp.reset();
244 }
245
246 if (m_reg_ctx_sp) {
247 RegisterValue m_old_reg_value(m_reg_value);
248 if (m_reg_ctx_sp->ReadRegister(&m_reg_info, m_reg_value)) {
250 Process *process = exe_ctx.GetProcessPtr();
251 if (process)
254 (void *)&m_reg_info);
256 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
257 SetValueIsValid(true);
258 SetValueDidChange(!(m_old_reg_value == m_reg_value));
259 return true;
260 }
261 }
262 }
263
264 SetValueIsValid(false);
266 return false;
267}
268
270 Status &error) {
271 // The new value will be in the m_data. Copy that into our register value.
272 error =
273 m_reg_value.SetValueFromString(&m_reg_info, llvm::StringRef(value_str));
274 if (!error.Success())
275 return false;
276
277 if (!m_reg_ctx_sp->WriteRegister(&m_reg_info, m_reg_value)) {
278 error.SetErrorString("unable to write back to register");
279 return false;
280 }
281
283 return true;
284}
285
287 error = m_reg_value.SetValueFromData(m_reg_info, data, 0, false);
288 if (!error.Success())
289 return false;
290
291 if (!m_reg_ctx_sp->WriteRegister(&m_reg_info, m_reg_value)) {
292 error.SetErrorString("unable to write back to register");
293 return false;
294 }
295
297 return true;
298}
299
302 false)) // make sure that you are up to date before returning anything
303 return m_reg_value.GetScalarValue(scalar);
304 return false;
305}
306
308 GetExpressionPathFormat epformat) {
309 s.Printf("$%s", m_reg_info.name);
310}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:365
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:302
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:340
uint32_t GetAddressByteSize() const
Definition: Process.cpp:3403
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:42
lldb::RegisterContextSP GetRegisterContext()
Get the RegisterContext for this frame, if possible.
An error handling class.
Definition: Status.h:44
void Clear()
Clear the object state.
Definition: Status.cpp:167
void SetErrorToGenericError()
Set the current error to a generic error.
Definition: Status.cpp:223
bool Success() const
Test for success condition.
Definition: Status.cpp:279
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
ValueObject * CreateChildAtIndex(size_t idx, bool synthetic_array_member, int32_t synthetic_index) override
Should only be called by ValueObject::GetChildAtIndex().
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().
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:976
CompilerType GetCompilerType()
Definition: ValueObject.h:352
lldb::ValueObjectSP GetSP()
Definition: ValueObject.h:545
ChildrenManager m_children
Definition: ValueObject.h:873
Status m_error
An error object that can describe any errors that occur when updating values.
Definition: ValueObject.h:850
DataExtractor m_data
A data extractor that can be used to extract the value.
Definition: ValueObject.h:846
void SetValueDidChange(bool value_changed)
Definition: ValueObject.h:972
bool UpdateValueIfNeeded(bool update_format=true)
ConstString m_name
The name of this object.
Definition: ValueObject.h:844
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:330
uint32_t GetNumChildrenIgnoringErrors(uint32_t max=UINT32_MAX)
Like GetNumChildren but returns 0 on error.
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.
Definition: SBAttachInfo.h:14
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:314
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:472
@ eLanguageTypeC
Non-standardized C, such as K&R.
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
Definition: lldb-forward.h:386
@ 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.