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"
14 #include "lldb/Symbol/TypeSystem.h"
16 #include "lldb/Target/Process.h"
18 #include "lldb/Target/StackFrame.h"
19 #include "lldb/Target/Target.h"
21 #include "lldb/Utility/LLDBLog.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 
32 namespace lldb_private {
34 }
35 
36 using namespace lldb;
37 using namespace lldb_private;
38 
39 #pragma mark ValueObjectRegisterSet
40 
41 ValueObjectSP
42 ValueObjectRegisterSet::Create(ExecutionContextScope *exe_scope,
43  lldb::RegisterContextSP &reg_ctx_sp,
44  uint32_t set_idx) {
45  auto manager_sp = ValueObjectManager::Create();
46  return (new ValueObjectRegisterSet(exe_scope, *manager_sp, reg_ctx_sp,
47  set_idx))
48  ->GetSP();
49 }
50 
51 ValueObjectRegisterSet::ValueObjectRegisterSet(ExecutionContextScope *exe_scope,
52  ValueObjectManager &manager,
53  lldb::RegisterContextSP &reg_ctx,
54  uint32_t reg_set_idx)
55  : ValueObject(exe_scope, manager), m_reg_ctx_sp(reg_ctx),
56  m_reg_set(nullptr), m_reg_set_idx(reg_set_idx) {
57  assert(reg_ctx);
58  m_reg_set = reg_ctx->GetRegisterSet(m_reg_set_idx);
59  if (m_reg_set) {
61  }
62 }
63 
65 
67  return CompilerType();
68 }
69 
71 
73  return ConstString();
74 }
75 
77  const RegisterSet *reg_set = m_reg_ctx_sp->GetRegisterSet(m_reg_set_idx);
78  if (reg_set) {
79  auto reg_count = reg_set->num_registers;
80  return reg_count <= max ? reg_count : max;
81  }
82  return 0;
83 }
84 
85 llvm::Optional<uint64_t> ValueObjectRegisterSet::GetByteSize() { return 0; }
86 
88  m_error.Clear();
89  SetValueDidChange(false);
91  StackFrame *frame = exe_ctx.GetFramePtr();
92  if (frame == nullptr)
93  m_reg_ctx_sp.reset();
94  else {
96  if (m_reg_ctx_sp) {
97  const RegisterSet *reg_set = m_reg_ctx_sp->GetRegisterSet(m_reg_set_idx);
98  if (reg_set == nullptr)
99  m_reg_ctx_sp.reset();
100  else if (m_reg_set != reg_set) {
101  SetValueDidChange(true);
102  m_name.SetCString(reg_set->name);
103  }
104  }
105  }
106  if (m_reg_ctx_sp) {
107  SetValueIsValid(true);
108  } else {
109  SetValueIsValid(false);
111  m_children.Clear();
112  }
113  return m_error.Success();
114 }
115 
117  size_t idx, bool synthetic_array_member, int32_t synthetic_index) {
118  ValueObject *valobj = nullptr;
119  if (m_reg_ctx_sp && m_reg_set) {
120  const size_t num_children = GetNumChildren();
121  if (idx < num_children)
122  valobj = new ValueObjectRegister(
123  *this, m_reg_ctx_sp,
124  m_reg_ctx_sp->GetRegisterInfoAtIndex(m_reg_set->registers[idx]));
125  }
126  return valobj;
127 }
128 
129 lldb::ValueObjectSP
131  bool can_create) {
132  ValueObject *valobj = nullptr;
133  if (m_reg_ctx_sp && m_reg_set) {
134  const RegisterInfo *reg_info =
135  m_reg_ctx_sp->GetRegisterInfoByName(name.GetStringRef());
136  if (reg_info != nullptr)
137  valobj = new ValueObjectRegister(*this, m_reg_ctx_sp, reg_info);
138  }
139  if (valobj)
140  return valobj->GetSP();
141  else
142  return ValueObjectSP();
143 }
144 
145 size_t
147  if (m_reg_ctx_sp && m_reg_set) {
148  const RegisterInfo *reg_info =
149  m_reg_ctx_sp->GetRegisterInfoByName(name.GetStringRef());
150  if (reg_info != nullptr)
151  return reg_info->kinds[eRegisterKindLLDB];
152  }
153  return UINT32_MAX;
154 }
155 
156 #pragma mark -
157 #pragma mark ValueObjectRegister
158 
159 void ValueObjectRegister::ConstructObject(const RegisterInfo *reg_info) {
160  if (reg_info) {
161  m_reg_info = *reg_info;
162  if (reg_info->name)
163  m_name.SetCString(reg_info->name);
164  else if (reg_info->alt_name)
165  m_name.SetCString(reg_info->alt_name);
166  }
167 }
168 
170  lldb::RegisterContextSP &reg_ctx_sp,
171  const RegisterInfo *reg_info)
172  : ValueObject(parent), m_reg_ctx_sp(reg_ctx_sp), m_reg_info(),
173  m_reg_value(), m_type_name(), m_compiler_type() {
174  assert(reg_ctx_sp.get());
175  ConstructObject(reg_info);
176 }
177 
179  lldb::RegisterContextSP &reg_ctx_sp,
180  const RegisterInfo *reg_info) {
181  auto manager_sp = ValueObjectManager::Create();
182  return (new ValueObjectRegister(exe_scope, *manager_sp, reg_ctx_sp, reg_info))
183  ->GetSP();
184 }
185 
187  ValueObjectManager &manager,
188  lldb::RegisterContextSP &reg_ctx,
189  const RegisterInfo *reg_info)
190  : ValueObject(exe_scope, manager), m_reg_ctx_sp(reg_ctx), m_reg_info(),
191  m_reg_value(), m_type_name(), m_compiler_type() {
192  assert(reg_ctx);
193  ConstructObject(reg_info);
194 }
195 
197 
199  if (!m_compiler_type.IsValid()) {
201  if (auto *target = exe_ctx.GetTargetPtr()) {
202  if (auto *exe_module = target->GetExecutableModulePointer()) {
203  auto type_system_or_err =
204  exe_module->GetTypeSystemForLanguage(eLanguageTypeC);
205  if (auto err = type_system_or_err.takeError()) {
206  LLDB_LOG_ERROR(GetLog(LLDBLog::Types), std::move(err),
207  "Unable to get CompilerType from TypeSystem");
208  } else {
209  if (auto ts = *type_system_or_err)
210  m_compiler_type = ts->GetBuiltinTypeForEncodingAndBitSize(
211  m_reg_info.encoding, m_reg_info.byte_size * 8);
212  }
213  }
214  }
215  }
216  return m_compiler_type;
217 }
218 
220  if (m_type_name.IsEmpty())
222  return m_type_name;
223 }
224 
227  auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
228  return children_count <= max ? children_count : max;
229 }
230 
231 llvm::Optional<uint64_t> ValueObjectRegister::GetByteSize() {
232  return m_reg_info.byte_size;
233 }
234 
236  m_error.Clear();
238  StackFrame *frame = exe_ctx.GetFramePtr();
239  if (frame == nullptr) {
240  m_reg_ctx_sp.reset();
241  m_reg_value.Clear();
242  }
243 
244  if (m_reg_ctx_sp) {
245  RegisterValue m_old_reg_value(m_reg_value);
246  if (m_reg_ctx_sp->ReadRegister(&m_reg_info, m_reg_value)) {
247  if (m_reg_value.GetData(m_data)) {
248  Process *process = exe_ctx.GetProcessPtr();
249  if (process)
252  (void *)&m_reg_info);
254  m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
255  SetValueIsValid(true);
256  SetValueDidChange(!(m_old_reg_value == m_reg_value));
257  return true;
258  }
259  }
260  }
261 
262  SetValueIsValid(false);
264  return false;
265 }
266 
267 bool ValueObjectRegister::SetValueFromCString(const char *value_str,
268  Status &error) {
269  // The new value will be in the m_data. Copy that into our register value.
270  error =
271  m_reg_value.SetValueFromString(&m_reg_info, llvm::StringRef(value_str));
272  if (!error.Success())
273  return false;
274 
275  if (!m_reg_ctx_sp->WriteRegister(&m_reg_info, m_reg_value)) {
276  error.SetErrorString("unable to write back to register");
277  return false;
278  }
279 
280  SetNeedsUpdate();
281  return true;
282 }
283 
285  error = m_reg_value.SetValueFromData(m_reg_info, data, 0, false);
286  if (!error.Success())
287  return false;
288 
289  if (!m_reg_ctx_sp->WriteRegister(&m_reg_info, m_reg_value)) {
290  error.SetErrorString("unable to write back to register");
291  return false;
292  }
293 
294  SetNeedsUpdate();
295  return true;
296 }
297 
300  false)) // make sure that you are up to date before returning anything
301  return m_reg_value.GetScalarValue(scalar);
302  return false;
303 }
304 
306  GetExpressionPathFormat epformat) {
307  s.Printf("$%s", m_reg_info.name);
308 }
lldb_private::LLDBLog::Types
@ Types
lldb_private::RegisterValue::Clear
void Clear()
Definition: RegisterValue.cpp:150
lldb_private::ValueObjectRegisterSet::GetTypeName
ConstString GetTypeName() override
Definition: ValueObjectRegister.cpp:70
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
Scalar.h
lldb_private::ValueObjectRegisterSet::CalculateNumChildren
size_t CalculateNumChildren(uint32_t max) override
Should only be called by ValueObject::GetNumChildren().
Definition: ValueObjectRegister.cpp:76
lldb_private::ValueObjectRegister
Definition: ValueObjectRegister.h:81
lldb_private::ValueObject::SetValueIsValid
void SetValueIsValid(bool valid)
Definition: ValueObject.h:980
lldb_private::RegisterValue
Definition: RegisterValue.h:28
lldb_private::Value::SetValueType
void SetValueType(ValueType value_type)
Definition: Value.h:89
lldb_private::ExecutionContext::GetProcessPtr
Process * GetProcessPtr() const
Returns a pointer to the process object.
Definition: ExecutionContext.cpp:206
lldb_private::ValueObjectRegister::m_type_name
ConstString m_type_name
Definition: ValueObjectRegister.h:118
lldb_private::CompilerType::GetTypeName
ConstString GetTypeName(bool BaseOnly=false) const
Definition: CompilerType.cpp:314
lldb_private::ValueObject::m_name
ConstString m_name
The name of this object.
Definition: ValueObject.h:849
lldb_private::Scalar
Definition: Scalar.h:34
lldb_private::ClusterManager
Definition: SharedCluster.h:22
lldb_private::Process
Definition: Process.h:338
lldb_private::ValueObjectRegister::Create
static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, lldb::RegisterContextSP &reg_ctx_sp, const RegisterInfo *reg_info)
Definition: ValueObjectRegister.cpp:178
Module.h
lldb_private::ValueObjectRegisterSet::GetQualifiedTypeName
ConstString GetQualifiedTypeName() override
Definition: ValueObjectRegister.cpp:72
lldb_private::ValueObject::SetValueDidChange
void SetValueDidChange(bool value_changed)
Definition: ValueObject.h:976
lldb_private::ValueObjectRegister::UpdateValue
bool UpdateValue() override
Definition: ValueObjectRegister.cpp:235
lldb_private::ValueObjectRegisterSet::m_reg_ctx_sp
lldb::RegisterContextSP m_reg_ctx_sp
Definition: ValueObjectRegister.h:64
lldb_private::ConstString::SetCString
void SetCString(const char *cstr)
Set the C string value.
Definition: ConstString.cpp:301
lldb_private::RegisterValue::SetValueFromData
Status SetValueFromData(const RegisterInfo &reg_info, DataExtractor &data, lldb::offset_t offset, bool partial_data_ok)
Definition: RegisterValue.cpp:165
lldb_private::ExecutionContext::GetFramePtr
StackFrame * GetFramePtr() const
Returns a pointer to the frame object.
Definition: ExecutionContext.h:408
lldb_private::ValueObjectRegister::SetData
bool SetData(DataExtractor &data, Status &error) override
Definition: ValueObjectRegister.cpp:284
StackFrame.h
lldb::eLanguageTypeC
@ eLanguageTypeC
Non-standardized C, such as K&R.
Definition: lldb-enumerations.h:440
lldb_private::Stream
Definition: Stream.h:28
lldb_private::ValueObject::m_children
ChildrenManager m_children
Definition: ValueObject.h:878
lldb_private::Status::SetErrorToGenericError
void SetErrorToGenericError()
Set the current error to a generic error.
Definition: Status.cpp:231
lldb_private::ValueObjectRegisterSet::~ValueObjectRegisterSet
~ValueObjectRegisterSet() override
Process.h
lldb_private::ValueObject::GetExpressionPathFormat
GetExpressionPathFormat
Definition: ValueObject.h:107
lldb_private::ValueObjectRegister::ValueObjectRegister
ValueObjectRegister(ValueObject &parent, lldb::RegisterContextSP &reg_ctx_sp, const RegisterInfo *reg_info)
Definition: ValueObjectRegister.cpp:169
lldb_private::ValueObject::m_data
DataExtractor m_data
A data extractor that can be used to extract the value.
Definition: ValueObject.h:851
Target.h
lldb_private::ValueObjectRegisterSet
Definition: ValueObjectRegister.h:31
lldb_private::Value::GetScalar
const Scalar & GetScalar() const
Definition: Value.h:112
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::Status::Success
bool Success() const
Test for success condition.
Definition: Status.cpp:287
lldb_private::ValueObjectRegister::m_reg_info
RegisterInfo m_reg_info
Definition: ValueObjectRegister.h:116
lldb_private::ValueObjectRegisterSet::GetIndexOfChildWithName
size_t GetIndexOfChildWithName(ConstString name) override
Definition: ValueObjectRegister.cpp:146
lldb::eRegisterKindLLDB
@ eRegisterKindLLDB
lldb's internal register numbers
Definition: lldb-enumerations.h:235
lldb_private::DataExtractor
Definition: DataExtractor.h:48
lldb_private::ConstString::GetStringRef
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
Definition: ConstString.h:201
Log.h
lldb_private::ExecutionContextScope
Definition: ExecutionContextScope.h:32
lldb_private::ValueObjectRegisterSet::CreateChildAtIndex
ValueObject * CreateChildAtIndex(size_t idx, bool synthetic_array_member, int32_t synthetic_index) override
Should only be called by ValueObject::GetChildAtIndex().
Definition: ValueObjectRegister.cpp:116
lldb_private::ConstString::IsEmpty
bool IsEmpty() const
Test for empty string.
Definition: ConstString.h:303
lldb_private::ValueObjectRegister::SetValueFromCString
bool SetValueFromCString(const char *value_str, Status &error) override
Definition: ValueObjectRegister.cpp:267
lldb_private::ValueObject::GetSP
lldb::ValueObjectSP GetSP()
Definition: ValueObject.h:555
lldb_private::DataExtractor::SetAddressByteSize
void SetAddressByteSize(uint32_t addr_size)
Set the address byte size.
Definition: DataExtractor.h:845
lldb_private::RegisterValue::GetData
bool GetData(DataExtractor &data) const
Definition: RegisterValue.cpp:34
lldb_private::DataExtractor::GetDataStart
const uint8_t * GetDataStart() const
Get the data start pointer.
Definition: DataExtractor.h:422
lldb_private::Process::GetAddressByteSize
uint32_t GetAddressByteSize() const
Definition: Process.cpp:3366
ValueObjectRegister.h
lldb_private::ConstString
Definition: ConstString.h:39
lldb_private::ValueObjectRegister::~ValueObjectRegister
~ValueObjectRegister() override
lldb_private::ValueObjectRegisterSet::m_reg_set
const RegisterSet * m_reg_set
Definition: ValueObjectRegister.h:65
lldb_private::ValueObjectRegister::GetTypeName
ConstString GetTypeName() override
Definition: ValueObjectRegister.cpp:219
lldb_private::ValueObject::UpdateValueIfNeeded
bool UpdateValueIfNeeded(bool update_format=true)
Definition: ValueObject.cpp:111
lldb_private::ValueObjectRegister::ResolveValue
bool ResolveValue(Scalar &scalar) override
Definition: ValueObjectRegister.cpp:298
lldb_private::ValueObject::GetExecutionContextRef
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:330
TypeSystem.h
CompilerType.h
lldb_private::ValueObjectRegisterSet::m_reg_set_idx
uint32_t m_reg_set_idx
Definition: ValueObjectRegister.h:66
lldb_private::CompilerType::IsValid
bool IsValid() const
Definition: CompilerType.h:123
lldb_private::CompilerType::GetNumChildren
uint32_t GetNumChildren(bool omit_empty_base_classes, const ExecutionContext *exe_ctx) const
Definition: CompilerType.cpp:579
lldb_private::ValueObject::GetNumChildren
size_t GetNumChildren(uint32_t max=UINT32_MAX)
Definition: ValueObject.cpp:496
lldb_private::Status
Definition: Status.h:44
lldb_private::ValueObject
ValueObject:
Definition: ValueObject.h:105
lldb_private::ValueObjectRegister::m_reg_ctx_sp
lldb::RegisterContextSP m_reg_ctx_sp
Definition: ValueObjectRegister.h:115
uint32_t
lldb_private::ValueObject::m_value
Value m_value
Definition: ValueObject.h:852
lldb_private::Value::ValueType::HostAddress
@ HostAddress
A host address value (for memory in the process that < A is using liblldb).
lldb_private::ValueObjectRegister::GetCompilerTypeImpl
CompilerType GetCompilerTypeImpl() override
Definition: ValueObjectRegister.cpp:198
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:19
lldb_private::ValueObject::GetCompilerType
CompilerType GetCompilerType()
Definition: ValueObject.h:352
lldb_private::ValueObjectRegisterSet::GetChildMemberWithName
lldb::ValueObjectSP GetChildMemberWithName(ConstString name, bool can_create) override
Definition: ValueObjectRegister.cpp:130
DataExtractor.h
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:35
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private::Value::ContextType::RegisterInfo
@ RegisterInfo
RegisterInfo * (can be a scalar or a vector register).
lldb_private::ValueObjectRegister::m_compiler_type
CompilerType m_compiler_type
Definition: ValueObjectRegister.h:119
Status.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::Value::SetContext
void SetContext(ContextType context_type, void *p)
Definition: Value.h:96
lldb_private::ValueObjectRegisterSet::UpdateValue
bool UpdateValue() override
Definition: ValueObjectRegister.cpp:87
lldb_private::StackFrame::GetRegisterContext
lldb::RegisterContextSP GetRegisterContext()
Get the RegisterContext for this frame, if possible.
Definition: StackFrame.cpp:1143
lldb_private::Status::Clear
void Clear()
Clear the object state.
Definition: Status.cpp:167
lldb_private::ValueObjectRegister::GetByteSize
llvm::Optional< uint64_t > GetByteSize() override
Definition: ValueObjectRegister.cpp:231
Stream.h
lldb_private::ValueObject::m_error
Status m_error
An error object that can describe any errors that occur when updating values.
Definition: ValueObject.h:855
lldb_private::ValueObjectRegisterSet::GetByteSize
llvm::Optional< uint64_t > GetByteSize() override
Definition: ValueObjectRegister.cpp:85
lldb_private::GetLog
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:309
lldb_private::ValueObjectRegister::GetExpressionPath
void GetExpressionPath(Stream &s, GetExpressionPathFormat epformat=eGetExpressionPathFormatDereferencePointers) override
Definition: ValueObjectRegister.cpp:305
lldb_private::StackFrame
Definition: StackFrame.h:40
lldb_private::ValueObjectRegister::CalculateNumChildren
size_t CalculateNumChildren(uint32_t max) override
Should only be called by ValueObject::GetNumChildren().
Definition: ValueObjectRegister.cpp:225
lldb_private::ValueObject::ChildrenManager::Clear
void Clear(size_t new_count=0)
Definition: ValueObject.h:822
lldb
Definition: SBAddress.h:15
RegisterContext.h
LLDB_LOG_ERROR
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:360
lldb_private::RegisterValue::SetValueFromString
Status SetValueFromString(const RegisterInfo *reg_info, llvm::StringRef value_str)
Definition: RegisterValue.cpp:328
Value.h
lldb_private::ValueObjectRegister::ConstructObject
void ConstructObject(const RegisterInfo *reg_info)
Definition: ValueObjectRegister.cpp:159
LLDBLog.h
lldb_private::RegisterValue::GetScalarValue
bool GetScalarValue(Scalar &scalar) const
Definition: RegisterValue.cpp:126
lldb_private::ExecutionContext::GetTargetPtr
Target * GetTargetPtr() const
Returns a pointer to the target object.
Definition: ExecutionContext.cpp:198
ExecutionContext.h
lldb_private::ValueObjectRegisterSet::GetCompilerTypeImpl
CompilerType GetCompilerTypeImpl() override
Definition: ValueObjectRegister.cpp:66
lldb_private::ClusterManager::Create
static std::shared_ptr< ClusterManager > Create()
Definition: SharedCluster.h:24
lldb_private::ValueObjectRegister::m_reg_value
RegisterValue m_reg_value
Definition: ValueObjectRegister.h:117
lldb_private::ValueObject::SetNeedsUpdate
void SetNeedsUpdate()
Definition: ValueObject.cpp:229