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/Log.h"
22 #include "lldb/Utility/Scalar.h"
23 #include "lldb/Utility/Status.h"
24 #include "lldb/Utility/Stream.h"
25 
26 #include "llvm/ADT/StringRef.h"
27 
28 #include <cassert>
29 #include <memory>
30 
31 namespace lldb_private {
33 }
34 
35 using namespace lldb;
36 using namespace lldb_private;
37 
38 #pragma mark ValueObjectRegisterSet
39 
40 ValueObjectSP
41 ValueObjectRegisterSet::Create(ExecutionContextScope *exe_scope,
42  lldb::RegisterContextSP &reg_ctx_sp,
43  uint32_t set_idx) {
44  auto manager_sp = ValueObjectManager::Create();
45  return (new ValueObjectRegisterSet(exe_scope, *manager_sp, reg_ctx_sp,
46  set_idx))
47  ->GetSP();
48 }
49 
50 ValueObjectRegisterSet::ValueObjectRegisterSet(ExecutionContextScope *exe_scope,
51  ValueObjectManager &manager,
52  lldb::RegisterContextSP &reg_ctx,
53  uint32_t reg_set_idx)
54  : ValueObject(exe_scope, manager), m_reg_ctx_sp(reg_ctx),
55  m_reg_set(nullptr), m_reg_set_idx(reg_set_idx) {
56  assert(reg_ctx);
57  m_reg_set = reg_ctx->GetRegisterSet(m_reg_set_idx);
58  if (m_reg_set) {
60  }
61 }
62 
64 
66  return CompilerType();
67 }
68 
70 
72  return ConstString();
73 }
74 
76  const RegisterSet *reg_set = m_reg_ctx_sp->GetRegisterSet(m_reg_set_idx);
77  if (reg_set) {
78  auto reg_count = reg_set->num_registers;
79  return reg_count <= max ? reg_count : max;
80  }
81  return 0;
82 }
83 
84 llvm::Optional<uint64_t> ValueObjectRegisterSet::GetByteSize() { return 0; }
85 
87  m_error.Clear();
88  SetValueDidChange(false);
90  StackFrame *frame = exe_ctx.GetFramePtr();
91  if (frame == nullptr)
92  m_reg_ctx_sp.reset();
93  else {
95  if (m_reg_ctx_sp) {
96  const RegisterSet *reg_set = m_reg_ctx_sp->GetRegisterSet(m_reg_set_idx);
97  if (reg_set == nullptr)
98  m_reg_ctx_sp.reset();
99  else if (m_reg_set != reg_set) {
100  SetValueDidChange(true);
101  m_name.SetCString(reg_set->name);
102  }
103  }
104  }
105  if (m_reg_ctx_sp) {
106  SetValueIsValid(true);
107  } else {
108  SetValueIsValid(false);
110  m_children.Clear();
111  }
112  return m_error.Success();
113 }
114 
116  size_t idx, bool synthetic_array_member, int32_t synthetic_index) {
117  ValueObject *valobj = nullptr;
118  if (m_reg_ctx_sp && m_reg_set) {
119  const size_t num_children = GetNumChildren();
120  if (idx < num_children)
121  valobj = new ValueObjectRegister(
122  *this, m_reg_ctx_sp,
123  m_reg_ctx_sp->GetRegisterInfoAtIndex(m_reg_set->registers[idx]));
124  }
125  return valobj;
126 }
127 
128 lldb::ValueObjectSP
130  bool can_create) {
131  ValueObject *valobj = nullptr;
132  if (m_reg_ctx_sp && m_reg_set) {
133  const RegisterInfo *reg_info =
134  m_reg_ctx_sp->GetRegisterInfoByName(name.GetStringRef());
135  if (reg_info != nullptr)
136  valobj = new ValueObjectRegister(*this, m_reg_ctx_sp, reg_info);
137  }
138  if (valobj)
139  return valobj->GetSP();
140  else
141  return ValueObjectSP();
142 }
143 
144 size_t
146  if (m_reg_ctx_sp && m_reg_set) {
147  const RegisterInfo *reg_info =
148  m_reg_ctx_sp->GetRegisterInfoByName(name.GetStringRef());
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 
158 void ValueObjectRegister::ConstructObject(const RegisterInfo *reg_info) {
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,
187  lldb::RegisterContextSP &reg_ctx,
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()) {
207  std::move(err), "Unable to get CompilerType from TypeSystem");
208  } else {
210  type_system_or_err->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  if (m_reg_ctx_sp->WriteRegister(&m_reg_info, m_reg_value)) {
274  SetNeedsUpdate();
275  return true;
276  } else
277  return false;
278  } else
279  return false;
280 }
281 
283  error = m_reg_value.SetValueFromData(&m_reg_info, data, 0, false);
284  if (error.Success()) {
285  if (m_reg_ctx_sp->WriteRegister(&m_reg_info, m_reg_value)) {
286  SetNeedsUpdate();
287  return true;
288  } else
289  return false;
290  } else
291  return false;
292 }
293 
296  false)) // make sure that you are up to date before returning anything
297  return m_reg_value.GetScalarValue(scalar);
298  return false;
299 }
300 
302  GetExpressionPathFormat epformat) {
303  s.Printf("$%s", m_reg_info.name);
304 }
lldb_private::RegisterValue::Clear
void Clear()
Definition: RegisterValue.cpp:160
lldb_private::ValueObjectRegisterSet::GetTypeName
ConstString GetTypeName() override
Definition: ValueObjectRegister.cpp:69
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:75
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:208
lldb_private::ValueObjectRegister::m_type_name
ConstString m_type_name
Definition: ValueObjectRegister.h:118
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:341
lldb_private::ValueObjectRegister::Create
static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, lldb::RegisterContextSP &reg_ctx_sp, const RegisterInfo *reg_info)
Definition: ValueObjectRegister.cpp:177
Module.h
lldb_private::ValueObjectRegisterSet::GetQualifiedTypeName
ConstString GetQualifiedTypeName() override
Definition: ValueObjectRegister.cpp:71
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:302
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:282
StackFrame.h
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:232
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:168
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:288
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:145
lldb::eRegisterKindLLDB
@ eRegisterKindLLDB
lldb's internal register numbers
Definition: lldb-enumerations.h:234
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:202
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:115
lldb_private::ConstString::IsEmpty
bool IsEmpty() const
Test for empty string.
Definition: ConstString.h:304
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:3348
ValueObjectRegister.h
lldb_private::ConstString
Definition: ConstString.h:40
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
LIBLLDB_LOG_TYPES
#define LIBLLDB_LOG_TYPES
Definition: Logging.h:33
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:294
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:72
lldb_private::CompilerType::GetNumChildren
uint32_t GetNumChildren(bool omit_empty_base_classes, const ExecutionContext *exe_ctx) const
Definition: CompilerType.cpp:515
lldb_private::ValueObject::GetNumChildren
size_t GetNumChildren(uint32_t max=UINT32_MAX)
Definition: ValueObject.cpp:496
lldb_private::RegisterValue::SetValueFromData
Status SetValueFromData(const RegisterInfo *reg_info, DataExtractor &data, lldb::offset_t offset, bool partial_data_ok)
Definition: RegisterValue.cpp:172
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:197
lldb_private::CompilerType::GetTypeName
ConstString GetTypeName() const
Definition: CompilerType.cpp:277
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:31
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:129
DataExtractor.h
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:33
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::eLanguageTypeC
@ eLanguageTypeC
Non-standardized C, such as K&R.
Definition: lldb-enumerations.h:439
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:86
lldb_private::StackFrame::GetRegisterContext
lldb::RegisterContextSP GetRegisterContext()
Get the RegisterContext for this frame, if possible.
Definition: StackFrame.cpp:1123
lldb_private::Status::Clear
void Clear()
Clear the object state.
Definition: Status.cpp:168
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::GetLogIfAnyCategoriesSet
Log * GetLogIfAnyCategoriesSet(uint32_t mask)
Definition: Logging.cpp:62
lldb_private::ValueObjectRegisterSet::GetByteSize
llvm::Optional< uint64_t > GetByteSize() override
Definition: ValueObjectRegister.cpp:84
lldb_private::ValueObjectRegister::GetExpressionPath
void GetExpressionPath(Stream &s, GetExpressionPathFormat epformat=eGetExpressionPathFormatDereferencePointers) override
Definition: ValueObjectRegister.cpp:301
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:265
lldb_private::RegisterValue::SetValueFromString
Status SetValueFromString(const RegisterInfo *reg_info, llvm::StringRef value_str)
Definition: RegisterValue.cpp:335
Value.h
lldb_private::ValueObjectRegister::ConstructObject
void ConstructObject(const RegisterInfo *reg_info)
Definition: ValueObjectRegister.cpp:158
lldb_private::RegisterValue::GetScalarValue
bool GetScalarValue(Scalar &scalar) const
Definition: RegisterValue.cpp:136
lldb_private::ExecutionContext::GetTargetPtr
Target * GetTargetPtr() const
Returns a pointer to the target object.
Definition: ExecutionContext.cpp:200
ExecutionContext.h
lldb_private::ValueObjectRegisterSet::GetCompilerTypeImpl
CompilerType GetCompilerTypeImpl() override
Definition: ValueObjectRegister.cpp:65
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