Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Value.cpp -----------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/Core/Value.h" |
| 11 | |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Address.h" // for Address |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Module.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 14 | #include "lldb/Symbol/CompilerType.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | #include "lldb/Symbol/ObjectFile.h" |
| 16 | #include "lldb/Symbol/SymbolContext.h" |
| 17 | #include "lldb/Symbol/Type.h" |
| 18 | #include "lldb/Symbol/Variable.h" |
| 19 | #include "lldb/Target/ExecutionContext.h" |
| 20 | #include "lldb/Target/Process.h" |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 21 | #include "lldb/Target/SectionLoadList.h" |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 22 | #include "lldb/Target/Target.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 23 | #include "lldb/Utility/ConstString.h" // for ConstString |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 24 | #include "lldb/Utility/DataBufferHeap.h" |
| 25 | #include "lldb/Utility/DataExtractor.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 26 | #include "lldb/Utility/Endian.h" // for InlHostByteOrder |
| 27 | #include "lldb/Utility/FileSpec.h" // for FileSpec |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 28 | #include "lldb/Utility/State.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 29 | #include "lldb/Utility/Stream.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 30 | #include "lldb/lldb-defines.h" // for LLDB_INVALID_ADDRESS |
| 31 | #include "lldb/lldb-forward.h" // for DataBufferSP, ModuleSP |
| 32 | #include "lldb/lldb-types.h" // for addr_t |
| 33 | |
| 34 | #include <memory> // for make_shared |
| 35 | #include <string> // for string |
| 36 | |
| 37 | #include <inttypes.h> // for PRIx64 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | |
| 39 | using namespace lldb; |
| 40 | using namespace lldb_private; |
| 41 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 42 | Value::Value() |
| 43 | : m_value(), m_vector(), m_compiler_type(), m_context(NULL), |
| 44 | m_value_type(eValueTypeScalar), m_context_type(eContextTypeInvalid), |
| 45 | m_data_buffer() {} |
| 46 | |
| 47 | Value::Value(const Scalar &scalar) |
| 48 | : m_value(scalar), m_vector(), m_compiler_type(), m_context(NULL), |
| 49 | m_value_type(eValueTypeScalar), m_context_type(eContextTypeInvalid), |
| 50 | m_data_buffer() {} |
| 51 | |
| 52 | Value::Value(const void *bytes, int len) |
| 53 | : m_value(), m_vector(), m_compiler_type(), m_context(NULL), |
| 54 | m_value_type(eValueTypeHostAddress), m_context_type(eContextTypeInvalid), |
| 55 | m_data_buffer() { |
| 56 | SetBytes(bytes, len); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | Value::Value(const Value &v) |
| 60 | : m_value(v.m_value), m_vector(v.m_vector), |
| 61 | m_compiler_type(v.m_compiler_type), m_context(v.m_context), |
| 62 | m_value_type(v.m_value_type), m_context_type(v.m_context_type), |
| 63 | m_data_buffer() { |
| 64 | const uintptr_t rhs_value = |
| 65 | (uintptr_t)v.m_value.ULongLong(LLDB_INVALID_ADDRESS); |
| 66 | if ((rhs_value != 0) && |
| 67 | (rhs_value == (uintptr_t)v.m_data_buffer.GetBytes())) { |
| 68 | m_data_buffer.CopyData(v.m_data_buffer.GetBytes(), |
| 69 | v.m_data_buffer.GetByteSize()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | |
Greg Clayton | 2740787 | 2014-07-12 00:24:33 +0000 | [diff] [blame] | 71 | m_value = (uintptr_t)m_data_buffer.GetBytes(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 72 | } |
Greg Clayton | 2740787 | 2014-07-12 00:24:33 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 75 | Value &Value::operator=(const Value &rhs) { |
| 76 | if (this != &rhs) { |
| 77 | m_value = rhs.m_value; |
| 78 | m_vector = rhs.m_vector; |
| 79 | m_compiler_type = rhs.m_compiler_type; |
| 80 | m_context = rhs.m_context; |
| 81 | m_value_type = rhs.m_value_type; |
| 82 | m_context_type = rhs.m_context_type; |
| 83 | const uintptr_t rhs_value = |
| 84 | (uintptr_t)rhs.m_value.ULongLong(LLDB_INVALID_ADDRESS); |
| 85 | if ((rhs_value != 0) && |
| 86 | (rhs_value == (uintptr_t)rhs.m_data_buffer.GetBytes())) { |
| 87 | m_data_buffer.CopyData(rhs.m_data_buffer.GetBytes(), |
| 88 | rhs.m_data_buffer.GetByteSize()); |
Greg Clayton | 2740787 | 2014-07-12 00:24:33 +0000 | [diff] [blame] | 89 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | m_value = (uintptr_t)m_data_buffer.GetBytes(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 91 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 92 | } |
| 93 | return *this; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | void Value::SetBytes(const void *bytes, int len) { |
| 97 | m_value_type = eValueTypeHostAddress; |
| 98 | m_data_buffer.CopyData(bytes, len); |
| 99 | m_value = (uintptr_t)m_data_buffer.GetBytes(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 102 | void Value::AppendBytes(const void *bytes, int len) { |
| 103 | m_value_type = eValueTypeHostAddress; |
| 104 | m_data_buffer.AppendData(bytes, len); |
| 105 | m_value = (uintptr_t)m_data_buffer.GetBytes(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 108 | void Value::Dump(Stream *strm) { |
| 109 | m_value.GetValue(strm, true); |
| 110 | strm->Printf(", value_type = %s, context = %p, context_type = %s", |
| 111 | Value::GetValueTypeAsCString(m_value_type), m_context, |
| 112 | Value::GetContextTypeAsCString(m_context_type)); |
| 113 | } |
| 114 | |
| 115 | Value::ValueType Value::GetValueType() const { return m_value_type; } |
| 116 | |
| 117 | AddressType Value::GetValueAddressType() const { |
| 118 | switch (m_value_type) { |
| 119 | default: |
| 120 | case eValueTypeScalar: |
| 121 | break; |
| 122 | case eValueTypeLoadAddress: |
| 123 | return eAddressTypeLoad; |
| 124 | case eValueTypeFileAddress: |
| 125 | return eAddressTypeFile; |
| 126 | case eValueTypeHostAddress: |
| 127 | return eAddressTypeHost; |
| 128 | } |
| 129 | return eAddressTypeInvalid; |
| 130 | } |
| 131 | |
| 132 | RegisterInfo *Value::GetRegisterInfo() const { |
| 133 | if (m_context_type == eContextTypeRegisterInfo) |
| 134 | return static_cast<RegisterInfo *>(m_context); |
| 135 | return NULL; |
| 136 | } |
| 137 | |
| 138 | Type *Value::GetType() { |
| 139 | if (m_context_type == eContextTypeLLDBType) |
| 140 | return static_cast<Type *>(m_context); |
| 141 | return NULL; |
| 142 | } |
| 143 | |
| 144 | size_t Value::AppendDataToHostBuffer(const Value &rhs) { |
Don Hinton | f031e48 | 2017-11-14 18:19:41 +0000 | [diff] [blame] | 145 | if (this == &rhs) |
| 146 | return 0; |
| 147 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 148 | size_t curr_size = m_data_buffer.GetByteSize(); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 149 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 150 | switch (rhs.GetValueType()) { |
| 151 | case eValueTypeScalar: { |
| 152 | const size_t scalar_size = rhs.m_value.GetByteSize(); |
| 153 | if (scalar_size > 0) { |
| 154 | const size_t new_size = curr_size + scalar_size; |
| 155 | if (ResizeData(new_size) == new_size) { |
| 156 | rhs.m_value.GetAsMemoryData(m_data_buffer.GetBytes() + curr_size, |
| 157 | scalar_size, endian::InlHostByteOrder(), |
| 158 | error); |
| 159 | return scalar_size; |
| 160 | } |
Greg Clayton | ac58361 | 2014-07-31 18:19:28 +0000 | [diff] [blame] | 161 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 162 | } break; |
| 163 | case eValueTypeVector: { |
| 164 | const size_t vector_size = rhs.m_vector.length; |
| 165 | if (vector_size > 0) { |
| 166 | const size_t new_size = curr_size + vector_size; |
| 167 | if (ResizeData(new_size) == new_size) { |
| 168 | ::memcpy(m_data_buffer.GetBytes() + curr_size, rhs.m_vector.bytes, |
| 169 | vector_size); |
| 170 | return vector_size; |
| 171 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 172 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 173 | } break; |
| 174 | case eValueTypeFileAddress: |
| 175 | case eValueTypeLoadAddress: |
| 176 | case eValueTypeHostAddress: { |
| 177 | const uint8_t *src = rhs.GetBuffer().GetBytes(); |
| 178 | const size_t src_len = rhs.GetBuffer().GetByteSize(); |
| 179 | if (src && src_len > 0) { |
| 180 | const size_t new_size = curr_size + src_len; |
| 181 | if (ResizeData(new_size) == new_size) { |
| 182 | ::memcpy(m_data_buffer.GetBytes() + curr_size, src, src_len); |
| 183 | return src_len; |
| 184 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 185 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 186 | } break; |
| 187 | } |
| 188 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 191 | size_t Value::ResizeData(size_t len) { |
| 192 | m_value_type = eValueTypeHostAddress; |
| 193 | m_data_buffer.SetByteSize(len); |
| 194 | m_value = (uintptr_t)m_data_buffer.GetBytes(); |
| 195 | return m_data_buffer.GetByteSize(); |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 198 | bool Value::ValueOf(ExecutionContext *exe_ctx) { |
| 199 | switch (m_context_type) { |
| 200 | case eContextTypeInvalid: |
| 201 | case eContextTypeRegisterInfo: // RegisterInfo * |
| 202 | case eContextTypeLLDBType: // Type * |
| 203 | break; |
| 204 | |
| 205 | case eContextTypeVariable: // Variable * |
| 206 | ResolveValue(exe_ctx); |
| 207 | return true; |
| 208 | } |
| 209 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 212 | uint64_t Value::GetValueByteSize(Status *error_ptr, ExecutionContext *exe_ctx) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 213 | uint64_t byte_size = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 214 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 215 | switch (m_context_type) { |
| 216 | case eContextTypeRegisterInfo: // RegisterInfo * |
| 217 | if (GetRegisterInfo()) |
| 218 | byte_size = GetRegisterInfo()->byte_size; |
| 219 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 220 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 221 | case eContextTypeInvalid: |
| 222 | case eContextTypeLLDBType: // Type * |
| 223 | case eContextTypeVariable: // Variable * |
| 224 | { |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 225 | const CompilerType &ast_type = GetCompilerType(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 226 | if (ast_type.IsValid()) |
| 227 | byte_size = ast_type.GetByteSize( |
| 228 | exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr); |
| 229 | } break; |
| 230 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 231 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 232 | if (error_ptr) { |
| 233 | if (byte_size == 0) { |
| 234 | if (error_ptr->Success()) |
| 235 | error_ptr->SetErrorString("Unable to determine byte size."); |
| 236 | } else { |
| 237 | error_ptr->Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 238 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 239 | } |
| 240 | return byte_size; |
| 241 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 242 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 243 | const CompilerType &Value::GetCompilerType() { |
| 244 | if (!m_compiler_type.IsValid()) { |
| 245 | switch (m_context_type) { |
| 246 | case eContextTypeInvalid: |
| 247 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 248 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 249 | case eContextTypeRegisterInfo: |
| 250 | break; // TODO: Eventually convert into a compiler type? |
| 251 | |
| 252 | case eContextTypeLLDBType: { |
| 253 | Type *lldb_type = GetType(); |
| 254 | if (lldb_type) |
| 255 | m_compiler_type = lldb_type->GetForwardCompilerType(); |
| 256 | } break; |
| 257 | |
| 258 | case eContextTypeVariable: { |
| 259 | Variable *variable = GetVariable(); |
| 260 | if (variable) { |
| 261 | Type *variable_type = variable->GetType(); |
| 262 | if (variable_type) |
| 263 | m_compiler_type = variable_type->GetForwardCompilerType(); |
| 264 | } |
| 265 | } break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 266 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 267 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 268 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 269 | return m_compiler_type; |
| 270 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 271 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 272 | void Value::SetCompilerType(const CompilerType &compiler_type) { |
| 273 | m_compiler_type = compiler_type; |
| 274 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 275 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 276 | lldb::Format Value::GetValueDefaultFormat() { |
| 277 | switch (m_context_type) { |
| 278 | case eContextTypeRegisterInfo: |
| 279 | if (GetRegisterInfo()) |
| 280 | return GetRegisterInfo()->format; |
| 281 | break; |
| 282 | |
| 283 | case eContextTypeInvalid: |
| 284 | case eContextTypeLLDBType: |
| 285 | case eContextTypeVariable: { |
| 286 | const CompilerType &ast_type = GetCompilerType(); |
| 287 | if (ast_type.IsValid()) |
| 288 | return ast_type.GetFormat(); |
| 289 | } break; |
| 290 | } |
| 291 | |
| 292 | // Return a good default in case we can't figure anything out |
| 293 | return eFormatHex; |
| 294 | } |
| 295 | |
| 296 | bool Value::GetData(DataExtractor &data) { |
| 297 | switch (m_value_type) { |
| 298 | default: |
| 299 | break; |
| 300 | |
| 301 | case eValueTypeScalar: |
| 302 | if (m_value.GetData(data)) |
| 303 | return true; |
| 304 | break; |
| 305 | |
| 306 | case eValueTypeLoadAddress: |
| 307 | case eValueTypeFileAddress: |
| 308 | case eValueTypeHostAddress: |
| 309 | if (m_data_buffer.GetByteSize()) { |
| 310 | data.SetData(m_data_buffer.GetBytes(), m_data_buffer.GetByteSize(), |
| 311 | data.GetByteOrder()); |
| 312 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 313 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 314 | break; |
| 315 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 316 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 317 | return false; |
| 318 | } |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 319 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 320 | Status Value::GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data, |
| 321 | uint32_t data_offset, Module *module) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 322 | data.Clear(); |
| 323 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 324 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 325 | lldb::addr_t address = LLDB_INVALID_ADDRESS; |
| 326 | AddressType address_type = eAddressTypeFile; |
| 327 | Address file_so_addr; |
| 328 | const CompilerType &ast_type = GetCompilerType(); |
| 329 | switch (m_value_type) { |
| 330 | case eValueTypeVector: |
| 331 | if (ast_type.IsValid()) |
| 332 | data.SetAddressByteSize(ast_type.GetPointerByteSize()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 333 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 334 | data.SetAddressByteSize(sizeof(void *)); |
| 335 | data.SetData(m_vector.bytes, m_vector.length, m_vector.byte_order); |
| 336 | break; |
| 337 | |
| 338 | case eValueTypeScalar: { |
| 339 | data.SetByteOrder(endian::InlHostByteOrder()); |
| 340 | if (ast_type.IsValid()) |
| 341 | data.SetAddressByteSize(ast_type.GetPointerByteSize()); |
| 342 | else |
| 343 | data.SetAddressByteSize(sizeof(void *)); |
| 344 | |
| 345 | uint32_t limit_byte_size = UINT32_MAX; |
| 346 | |
| 347 | if (ast_type.IsValid()) { |
| 348 | limit_byte_size = ast_type.GetByteSize( |
| 349 | exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 352 | if (limit_byte_size <= m_value.GetByteSize()) { |
| 353 | if (m_value.GetData(data, limit_byte_size)) |
| 354 | return error; // Success; |
| 355 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 356 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 357 | error.SetErrorStringWithFormat("extracting data from value failed"); |
| 358 | break; |
| 359 | } |
| 360 | case eValueTypeLoadAddress: |
| 361 | if (exe_ctx == NULL) { |
| 362 | error.SetErrorString("can't read load address (no execution context)"); |
| 363 | } else { |
| 364 | Process *process = exe_ctx->GetProcessPtr(); |
| 365 | if (process == NULL || !process->IsAlive()) { |
| 366 | Target *target = exe_ctx->GetTargetPtr(); |
| 367 | if (target) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 368 | // Allow expressions to run and evaluate things when the target has |
| 369 | // memory sections loaded. This allows you to use "target modules |
| 370 | // load" to load your executable and any shared libraries, then |
| 371 | // execute commands where you can look at types in data sections. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 372 | const SectionLoadList &target_sections = target->GetSectionLoadList(); |
| 373 | if (!target_sections.IsEmpty()) { |
| 374 | address = m_value.ULongLong(LLDB_INVALID_ADDRESS); |
| 375 | if (target_sections.ResolveLoadAddress(address, file_so_addr)) { |
| 376 | address_type = eAddressTypeLoad; |
| 377 | data.SetByteOrder(target->GetArchitecture().GetByteOrder()); |
| 378 | data.SetAddressByteSize( |
| 379 | target->GetArchitecture().GetAddressByteSize()); |
| 380 | } else |
| 381 | address = LLDB_INVALID_ADDRESS; |
| 382 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 383 | } else { |
| 384 | error.SetErrorString("can't read load address (invalid process)"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 385 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 386 | } else { |
| 387 | address = m_value.ULongLong(LLDB_INVALID_ADDRESS); |
| 388 | address_type = eAddressTypeLoad; |
| 389 | data.SetByteOrder( |
| 390 | process->GetTarget().GetArchitecture().GetByteOrder()); |
| 391 | data.SetAddressByteSize( |
| 392 | process->GetTarget().GetArchitecture().GetAddressByteSize()); |
| 393 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 394 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 395 | break; |
| 396 | |
| 397 | case eValueTypeFileAddress: |
| 398 | if (exe_ctx == NULL) { |
| 399 | error.SetErrorString("can't read file address (no execution context)"); |
| 400 | } else if (exe_ctx->GetTargetPtr() == NULL) { |
| 401 | error.SetErrorString("can't read file address (invalid target)"); |
| 402 | } else { |
| 403 | address = m_value.ULongLong(LLDB_INVALID_ADDRESS); |
| 404 | if (address == LLDB_INVALID_ADDRESS) { |
| 405 | error.SetErrorString("invalid file address"); |
| 406 | } else { |
| 407 | if (module == NULL) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 408 | // The only thing we can currently lock down to a module so that we |
| 409 | // can resolve a file address, is a variable. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 410 | Variable *variable = GetVariable(); |
| 411 | if (variable) { |
| 412 | SymbolContext var_sc; |
| 413 | variable->CalculateSymbolContext(&var_sc); |
| 414 | module = var_sc.module_sp.get(); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | if (module) { |
| 419 | bool resolved = false; |
| 420 | ObjectFile *objfile = module->GetObjectFile(); |
| 421 | if (objfile) { |
| 422 | Address so_addr(address, objfile->GetSectionList()); |
| 423 | addr_t load_address = |
| 424 | so_addr.GetLoadAddress(exe_ctx->GetTargetPtr()); |
| 425 | bool process_launched_and_stopped = |
| 426 | exe_ctx->GetProcessPtr() |
| 427 | ? StateIsStoppedState(exe_ctx->GetProcessPtr()->GetState(), |
| 428 | true /* must_exist */) |
| 429 | : false; |
| 430 | // Don't use the load address if the process has exited. |
| 431 | if (load_address != LLDB_INVALID_ADDRESS && |
| 432 | process_launched_and_stopped) { |
| 433 | resolved = true; |
| 434 | address = load_address; |
| 435 | address_type = eAddressTypeLoad; |
| 436 | data.SetByteOrder( |
| 437 | exe_ctx->GetTargetRef().GetArchitecture().GetByteOrder()); |
| 438 | data.SetAddressByteSize(exe_ctx->GetTargetRef() |
| 439 | .GetArchitecture() |
| 440 | .GetAddressByteSize()); |
| 441 | } else { |
| 442 | if (so_addr.IsSectionOffset()) { |
| 443 | resolved = true; |
| 444 | file_so_addr = so_addr; |
| 445 | data.SetByteOrder(objfile->GetByteOrder()); |
| 446 | data.SetAddressByteSize(objfile->GetAddressByteSize()); |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | if (!resolved) { |
| 451 | Variable *variable = GetVariable(); |
| 452 | |
| 453 | if (module) { |
| 454 | if (variable) |
| 455 | error.SetErrorStringWithFormat( |
| 456 | "unable to resolve the module for file address 0x%" PRIx64 |
| 457 | " for variable '%s' in %s", |
| 458 | address, variable->GetName().AsCString(""), |
| 459 | module->GetFileSpec().GetPath().c_str()); |
| 460 | else |
| 461 | error.SetErrorStringWithFormat( |
| 462 | "unable to resolve the module for file address 0x%" PRIx64 |
| 463 | " in %s", |
| 464 | address, module->GetFileSpec().GetPath().c_str()); |
| 465 | } else { |
| 466 | if (variable) |
| 467 | error.SetErrorStringWithFormat( |
| 468 | "unable to resolve the module for file address 0x%" PRIx64 |
| 469 | " for variable '%s'", |
| 470 | address, variable->GetName().AsCString("")); |
| 471 | else |
| 472 | error.SetErrorStringWithFormat( |
| 473 | "unable to resolve the module for file address 0x%" PRIx64, |
| 474 | address); |
| 475 | } |
| 476 | } |
| 477 | } else { |
| 478 | // Can't convert a file address to anything valid without more |
| 479 | // context (which Module it came from) |
| 480 | error.SetErrorString( |
| 481 | "can't read memory from file address without more context"); |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | break; |
| 486 | |
| 487 | case eValueTypeHostAddress: |
| 488 | address = m_value.ULongLong(LLDB_INVALID_ADDRESS); |
| 489 | address_type = eAddressTypeHost; |
| 490 | if (exe_ctx) { |
| 491 | Target *target = exe_ctx->GetTargetPtr(); |
| 492 | if (target) { |
| 493 | data.SetByteOrder(target->GetArchitecture().GetByteOrder()); |
| 494 | data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize()); |
| 495 | break; |
| 496 | } |
| 497 | } |
| 498 | // fallback to host settings |
| 499 | data.SetByteOrder(endian::InlHostByteOrder()); |
| 500 | data.SetAddressByteSize(sizeof(void *)); |
| 501 | break; |
| 502 | } |
| 503 | |
| 504 | // Bail if we encountered any errors |
| 505 | if (error.Fail()) |
| 506 | return error; |
| 507 | |
| 508 | if (address == LLDB_INVALID_ADDRESS) { |
| 509 | error.SetErrorStringWithFormat("invalid %s address", |
| 510 | address_type == eAddressTypeHost ? "host" |
| 511 | : "load"); |
| 512 | return error; |
| 513 | } |
| 514 | |
| 515 | // If we got here, we need to read the value from memory |
| 516 | size_t byte_size = GetValueByteSize(&error, exe_ctx); |
| 517 | |
| 518 | // Bail if we encountered any errors getting the byte size |
| 519 | if (error.Fail()) |
| 520 | return error; |
| 521 | |
| 522 | // Make sure we have enough room within "data", and if we don't make |
| 523 | // something large enough that does |
| 524 | if (!data.ValidOffsetForDataOfSize(data_offset, byte_size)) { |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 525 | auto data_sp = |
| 526 | std::make_shared<DataBufferHeap>(data_offset + byte_size, '\0'); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 527 | data.SetData(data_sp); |
| 528 | } |
| 529 | |
| 530 | uint8_t *dst = const_cast<uint8_t *>(data.PeekData(data_offset, byte_size)); |
| 531 | if (dst != NULL) { |
| 532 | if (address_type == eAddressTypeHost) { |
| 533 | // The address is an address in this process, so just copy it. |
| 534 | if (address == 0) { |
| 535 | error.SetErrorStringWithFormat( |
| 536 | "trying to read from host address of 0."); |
| 537 | return error; |
| 538 | } |
Pavel Labath | eac00c3 | 2017-11-02 21:35:26 +0000 | [diff] [blame] | 539 | memcpy(dst, reinterpret_cast<uint8_t *>(address), byte_size); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 540 | } else if ((address_type == eAddressTypeLoad) || |
| 541 | (address_type == eAddressTypeFile)) { |
| 542 | if (file_so_addr.IsValid()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 543 | // We have a file address that we were able to translate into a section |
| 544 | // offset address so we might be able to read this from the object |
| 545 | // files if we don't have a live process. Lets always try and read from |
| 546 | // the process if we have one though since we want to read the actual |
| 547 | // value by setting "prefer_file_cache" to false. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 548 | const bool prefer_file_cache = false; |
| 549 | if (exe_ctx->GetTargetRef().ReadMemory(file_so_addr, prefer_file_cache, |
| 550 | dst, byte_size, |
| 551 | error) != byte_size) { |
| 552 | error.SetErrorStringWithFormat( |
| 553 | "read memory from 0x%" PRIx64 " failed", (uint64_t)address); |
| 554 | } |
| 555 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 556 | // The execution context might have a NULL process, but it might have a |
| 557 | // valid process in the exe_ctx->target, so use the |
| 558 | // ExecutionContext::GetProcess accessor to ensure we get the process |
| 559 | // if there is one. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 560 | Process *process = exe_ctx->GetProcessPtr(); |
| 561 | |
| 562 | if (process) { |
| 563 | const size_t bytes_read = |
| 564 | process->ReadMemory(address, dst, byte_size, error); |
| 565 | if (bytes_read != byte_size) |
| 566 | error.SetErrorStringWithFormat( |
| 567 | "read memory from 0x%" PRIx64 " failed (%u of %u bytes read)", |
| 568 | (uint64_t)address, (uint32_t)bytes_read, (uint32_t)byte_size); |
| 569 | } else { |
| 570 | error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 |
| 571 | " failed (invalid process)", |
| 572 | (uint64_t)address); |
| 573 | } |
| 574 | } |
| 575 | } else { |
| 576 | error.SetErrorStringWithFormat("unsupported AddressType value (%i)", |
| 577 | address_type); |
| 578 | } |
| 579 | } else { |
| 580 | error.SetErrorStringWithFormat("out of memory"); |
| 581 | } |
| 582 | |
| 583 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 586 | Scalar &Value::ResolveValue(ExecutionContext *exe_ctx) { |
| 587 | const CompilerType &compiler_type = GetCompilerType(); |
| 588 | if (compiler_type.IsValid()) { |
| 589 | switch (m_value_type) { |
| 590 | case eValueTypeScalar: // raw scalar value |
| 591 | break; |
| 592 | |
| 593 | default: |
| 594 | case eValueTypeFileAddress: |
| 595 | case eValueTypeLoadAddress: // load address value |
| 596 | case eValueTypeHostAddress: // host address value (for memory in the process |
| 597 | // that is using liblldb) |
| 598 | { |
| 599 | DataExtractor data; |
| 600 | lldb::addr_t addr = m_value.ULongLong(LLDB_INVALID_ADDRESS); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 601 | Status error(GetValueAsData(exe_ctx, data, 0, NULL)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 602 | if (error.Success()) { |
| 603 | Scalar scalar; |
| 604 | if (compiler_type.GetValueAsScalar(data, 0, data.GetByteSize(), |
| 605 | scalar)) { |
| 606 | m_value = scalar; |
| 607 | m_value_type = eValueTypeScalar; |
| 608 | } else { |
| 609 | if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes()) { |
| 610 | m_value.Clear(); |
| 611 | m_value_type = eValueTypeScalar; |
| 612 | } |
| 613 | } |
| 614 | } else { |
| 615 | if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes()) { |
| 616 | m_value.Clear(); |
| 617 | m_value_type = eValueTypeScalar; |
| 618 | } |
| 619 | } |
| 620 | } break; |
| 621 | } |
| 622 | } |
| 623 | return m_value; |
| 624 | } |
| 625 | |
| 626 | Variable *Value::GetVariable() { |
| 627 | if (m_context_type == eContextTypeVariable) |
| 628 | return static_cast<Variable *>(m_context); |
| 629 | return NULL; |
| 630 | } |
| 631 | |
| 632 | void Value::Clear() { |
| 633 | m_value.Clear(); |
| 634 | m_vector.Clear(); |
| 635 | m_compiler_type.Clear(); |
| 636 | m_value_type = eValueTypeScalar; |
| 637 | m_context = NULL; |
| 638 | m_context_type = eContextTypeInvalid; |
| 639 | m_data_buffer.Clear(); |
| 640 | } |
| 641 | |
| 642 | const char *Value::GetValueTypeAsCString(ValueType value_type) { |
| 643 | switch (value_type) { |
| 644 | case eValueTypeScalar: |
| 645 | return "scalar"; |
| 646 | case eValueTypeVector: |
| 647 | return "vector"; |
| 648 | case eValueTypeFileAddress: |
| 649 | return "file address"; |
| 650 | case eValueTypeLoadAddress: |
| 651 | return "load address"; |
| 652 | case eValueTypeHostAddress: |
| 653 | return "host address"; |
| 654 | }; |
| 655 | return "???"; |
| 656 | } |
| 657 | |
| 658 | const char *Value::GetContextTypeAsCString(ContextType context_type) { |
| 659 | switch (context_type) { |
| 660 | case eContextTypeInvalid: |
| 661 | return "invalid"; |
| 662 | case eContextTypeRegisterInfo: |
| 663 | return "RegisterInfo *"; |
| 664 | case eContextTypeLLDBType: |
| 665 | return "Type *"; |
| 666 | case eContextTypeVariable: |
| 667 | return "Variable *"; |
| 668 | }; |
| 669 | return "???"; |
| 670 | } |
| 671 | |
Adrian Prantl | b51804e | 2018-05-03 23:32:47 +0000 | [diff] [blame] | 672 | void Value::ConvertToLoadAddress(Module *module, Target *target) { |
| 673 | if (!module || !target || (GetValueType() != eValueTypeFileAddress)) |
| 674 | return; |
| 675 | |
| 676 | lldb::addr_t file_addr = GetScalar().ULongLong(LLDB_INVALID_ADDRESS); |
| 677 | if (file_addr == LLDB_INVALID_ADDRESS) |
| 678 | return; |
| 679 | |
| 680 | Address so_addr; |
| 681 | if (!module->ResolveFileAddress(file_addr, so_addr)) |
| 682 | return; |
| 683 | lldb::addr_t load_addr = so_addr.GetLoadAddress(target); |
| 684 | if (load_addr == LLDB_INVALID_ADDRESS) |
| 685 | return; |
| 686 | |
| 687 | SetValueType(Value::eValueTypeLoadAddress); |
| 688 | GetScalar() = load_addr; |
| 689 | } |
| 690 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 691 | ValueList::ValueList(const ValueList &rhs) { m_values = rhs.m_values; } |
| 692 | |
| 693 | const ValueList &ValueList::operator=(const ValueList &rhs) { |
| 694 | m_values = rhs.m_values; |
| 695 | return *this; |
| 696 | } |
| 697 | |
| 698 | void ValueList::PushValue(const Value &value) { m_values.push_back(value); } |
| 699 | |
| 700 | size_t ValueList::GetSize() { return m_values.size(); } |
| 701 | |
| 702 | Value *ValueList::GetValueAtIndex(size_t idx) { |
| 703 | if (idx < GetSize()) { |
| 704 | return &(m_values[idx]); |
| 705 | } else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 706 | return NULL; |
| 707 | } |
| 708 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 709 | void ValueList::Clear() { m_values.clear(); } |