LLDB  mainline
Value.h
Go to the documentation of this file.
1 //===-- Value.h -------------------------------------------------*- C++ -*-===//
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 
9 #ifndef liblldb_Value_h_
10 #define liblldb_Value_h_
11 
14 #include "lldb/Utility/Scalar.h"
15 #include "lldb/Utility/Status.h"
16 #include "lldb/lldb-enumerations.h"
19 
20 #include "llvm/ADT/APInt.h"
21 
22 #include <vector>
23 
24 #include <stdint.h>
25 #include <string.h>
26 
27 namespace lldb_private {
28 class DataExtractor;
29 }
30 namespace lldb_private {
31 class ExecutionContext;
32 }
33 namespace lldb_private {
34 class Module;
35 }
36 namespace lldb_private {
37 class Stream;
38 }
39 namespace lldb_private {
40 class Type;
41 }
42 namespace lldb_private {
43 class Variable;
44 }
45 
46 namespace lldb_private {
47 
48 class Value {
49 public:
50  // Values Less than zero are an error, greater than or equal to zero returns
51  // what the Scalar result is.
52  enum ValueType {
53  // m_value contains...
54  // ============================
55  eValueTypeScalar, // raw scalar value
56  eValueTypeVector, // byte array of m_vector.length with endianness of
57  // m_vector.byte_order
58  eValueTypeFileAddress, // file address value
59  eValueTypeLoadAddress, // load address value
60  eValueTypeHostAddress // host address value (for memory in the process that
61  // is using liblldb)
62  };
63 
64  enum ContextType // Type that describes Value::m_context
65  {
66  // m_context contains...
67  // ====================
68  eContextTypeInvalid, // undefined
69  eContextTypeRegisterInfo, // RegisterInfo * (can be a scalar or a vector
70  // register)
71  eContextTypeLLDBType, // lldb_private::Type *
72  eContextTypeVariable // lldb_private::Variable *
73  };
74 
75  const static size_t kMaxByteSize = 32u;
76 
77  struct Vector {
78  // The byte array must be big enough to hold vector registers for any
79  // supported target.
80  uint8_t bytes[kMaxByteSize];
81  size_t length;
83 
84  Vector() : length(0), byte_order(lldb::eByteOrderInvalid) {}
85 
86  Vector(const Vector &vector) { *this = vector; }
87  const Vector &operator=(const Vector &vector) {
88  SetBytes(vector.bytes, vector.length, vector.byte_order);
89  return *this;
90  }
91 
92  void Clear() { length = 0; }
93 
94  bool SetBytes(const void *bytes, size_t length,
95  lldb::ByteOrder byte_order) {
96  this->length = length;
97  this->byte_order = byte_order;
98  if (length)
99  ::memcpy(this->bytes, bytes,
100  length < kMaxByteSize ? length : kMaxByteSize);
101  return IsValid();
102  }
103 
104  bool IsValid() const {
105  return (length > 0 && length < kMaxByteSize &&
106  byte_order != lldb::eByteOrderInvalid);
107  }
108  // Casts a vector, if valid, to an unsigned int of matching or largest
109  // supported size. Truncates to the beginning of the vector if required.
110  // Returns a default constructed Scalar if the Vector data is internally
111  // inconsistent.
112  llvm::APInt rhs = llvm::APInt(BITWIDTH_INT128, NUM_OF_WORDS_INT128,
113  ((type128 *)bytes)->x);
114  Scalar GetAsScalar() const {
115  Scalar scalar;
116  if (IsValid()) {
117  if (length == 1)
118  scalar = *(const uint8_t *)bytes;
119  else if (length == 2)
120  scalar = *(const uint16_t *)bytes;
121  else if (length == 4)
122  scalar = *(const uint32_t *)bytes;
123  else if (length == 8)
124  scalar = *(const uint64_t *)bytes;
125  else if (length >= 16)
126  scalar = rhs;
127  }
128  return scalar;
129  }
130  };
131 
132  Value();
133  Value(const Scalar &scalar);
134  Value(const Vector &vector);
135  Value(const void *bytes, int len);
136  Value(const Value &rhs);
137 
138  void SetBytes(const void *bytes, int len);
139 
140  void AppendBytes(const void *bytes, int len);
141 
142  Value &operator=(const Value &rhs);
143 
144  const CompilerType &GetCompilerType();
145 
146  void SetCompilerType(const CompilerType &compiler_type);
147 
148  ValueType GetValueType() const;
149 
151 
153 
154  void SetValueType(ValueType value_type) { m_value_type = value_type; }
155 
156  void ClearContext() {
157  m_context = nullptr;
159  }
160 
161  void SetContext(ContextType context_type, void *p) {
162  m_context_type = context_type;
163  m_context = p;
165  RegisterInfo *reg_info = GetRegisterInfo();
166  if (reg_info->encoding == lldb::eEncodingVector &&
169  }
170  }
171 
172  RegisterInfo *GetRegisterInfo() const;
173 
174  Type *GetType();
175 
177 
178  const Scalar &GetScalar() const { return m_value; }
179 
180  const Vector &GetVector() const { return m_vector; }
181 
182  Scalar &GetScalar() { return m_value; }
183 
184  Vector &GetVector() { return m_vector; }
185 
186  bool SetVectorBytes(const Vector &vector) {
187  m_vector = vector;
188  return m_vector.IsValid();
189  }
190 
191  bool SetVectorBytes(uint8_t *bytes, size_t length,
192  lldb::ByteOrder byte_order) {
193  return m_vector.SetBytes(bytes, length, byte_order);
194  }
195 
197  if (m_vector.IsValid()) {
199  return true;
200  }
201  return false;
202  }
203 
204  size_t ResizeData(size_t len);
205 
206  size_t AppendDataToHostBuffer(const Value &rhs);
207 
209 
210  const DataBufferHeap &GetBuffer() const { return m_data_buffer; }
211 
212  bool ValueOf(ExecutionContext *exe_ctx);
213 
215 
216  void Dump(Stream *strm);
217 
219 
220  uint64_t GetValueByteSize(Status *error_ptr, ExecutionContext *exe_ctx);
221 
223  uint32_t data_offset,
224  Module *module); // Can be nullptr
225 
226  static const char *GetValueTypeAsCString(ValueType context_type);
227 
228  static const char *GetContextTypeAsCString(ContextType context_type);
229 
230  /// Convert this value's file address to a load address, if possible.
231  void ConvertToLoadAddress(Module *module, Target *target);
232 
233  bool GetData(DataExtractor &data);
234 
235  void Clear();
236 
237 protected:
241  void *m_context;
245 };
246 
247 class ValueList {
248 public:
249  ValueList() : m_values() {}
250 
251  ValueList(const ValueList &rhs);
252 
253  ~ValueList() = default;
254 
255  const ValueList &operator=(const ValueList &rhs);
256 
257  // void InsertValue (Value *value, size_t idx);
258  void PushValue(const Value &value);
259 
260  size_t GetSize();
261  Value *GetValueAtIndex(size_t idx);
262  void Clear();
263 
264 private:
265  typedef std::vector<Value> collection;
266 
267  collection m_values;
268 };
269 
270 } // namespace lldb_private
271 
272 #endif // liblldb_Value_h_
const Vector & operator=(const Vector &vector)
Definition: Value.h:87
An data extractor class.
Definition: DataExtractor.h:47
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
Scalar & GetScalar()
Definition: Value.h:182
static const char * GetValueTypeAsCString(ValueType context_type)
Definition: Value.cpp:641
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
ContextType m_context_type
Definition: Value.h:243
void Dump(Stream *strm)
Definition: Value.cpp:107
bool SetVectorBytes(const Vector &vector)
Definition: Value.h:186
RegisterInfo * GetRegisterInfo() const
Definition: Value.cpp:131
bool ValueOf(ExecutionContext *exe_ctx)
Definition: Value.cpp:197
Scalar GetAsScalar() const
Definition: Value.h:114
#define NUM_OF_WORDS_INT128
Definition: Scalar.h:26
Status GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data, uint32_t data_offset, Module *module)
Definition: Value.cpp:316
lldb::ByteOrder byte_order
Definition: Value.h:82
void ConvertToLoadAddress(Module *module, Target *target)
Convert this value&#39;s file address to a load address, if possible.
Definition: Value.cpp:671
void SetValueType(ValueType value_type)
Definition: Value.h:154
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
void AppendBytes(const void *bytes, int len)
Definition: Value.cpp:101
void ClearContext()
Definition: Value.h:156
A subclass of DataBuffer that stores a data buffer on the heap.
lldb::Format GetValueDefaultFormat()
Definition: Value.cpp:272
ValueType GetValueType() const
Definition: Value.cpp:114
Vector & GetVector()
Definition: Value.h:184
Type * GetType()
Definition: Value.cpp:137
bool SetVectorBytes(uint8_t *bytes, size_t length, lldb::ByteOrder byte_order)
Definition: Value.h:191
A class that describes an executable image and its associated object and symbol files.
Definition: Module.h:109
static const char * GetContextTypeAsCString(ContextType context_type)
Definition: Value.cpp:657
size_t AppendDataToHostBuffer(const Value &rhs)
Definition: Value.cpp:143
bool SetScalarFromVector()
Definition: Value.h:196
ContextType GetContextType() const
Definition: Value.h:152
bool GetData(DataExtractor &data)
Definition: Value.cpp:292
static const size_t kMaxByteSize
Definition: Value.h:75
const CompilerType & GetCompilerType()
Definition: Value.cpp:239
const Vector & GetVector() const
Definition: Value.h:180
DataBufferHeap m_data_buffer
Definition: Value.h:244
CompilerType m_compiler_type
Definition: Value.h:240
void SetContext(ContextType context_type, void *p)
Definition: Value.h:161
Definition: SBAddress.h:15
uint64_t GetValueByteSize(Status *error_ptr, ExecutionContext *exe_ctx)
Definition: Value.cpp:211
ValueType m_value_type
Definition: Value.h:242
size_t ResizeData(size_t len)
Definition: Value.cpp:190
bool IsValid() const
Definition: Value.h:104
#define BITWIDTH_INT128
Definition: Scalar.h:27
void SetCompilerType(const CompilerType &compiler_type)
Definition: Value.cpp:268
const Scalar & GetScalar() const
Definition: Value.h:178
DataBufferHeap & GetBuffer()
Definition: Value.h:208
Scalar & ResolveValue(ExecutionContext *exe_ctx)
Definition: Value.cpp:585
AddressType GetValueAddressType() const
Definition: Value.cpp:116
bool SetBytes(const void *bytes, size_t length, lldb::ByteOrder byte_order)
Definition: Value.h:94
Vector(const Vector &vector)
Definition: Value.h:86
Variable * GetVariable()
Definition: Value.cpp:625
Scalar m_value
Definition: Value.h:238
Vector m_vector
Definition: Value.h:239
const DataBufferHeap & GetBuffer() const
Definition: Value.h:210
uint8_t bytes[kMaxByteSize]
Definition: Value.h:80
An error handling class.
Definition: Status.h:44
void * m_context
Definition: Value.h:241