blob: 8fc7f7c5a105a9c68d96b08b94010591de4d6593 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Core/DataExtractor.h"
17#include "lldb/Core/DataBufferHeap.h"
18#include "lldb/Core/Module.h"
Johnny Chen04ef9492012-02-02 22:11:13 +000019#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/Stream.h"
Greg Claytone1a916a2010-07-21 22:12:05 +000021#include "lldb/Symbol/ClangASTType.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Symbol/ClangASTContext.h"
23#include "lldb/Symbol/ObjectFile.h"
24#include "lldb/Symbol/SymbolContext.h"
25#include "lldb/Symbol/Type.h"
26#include "lldb/Symbol/Variable.h"
27#include "lldb/Target/ExecutionContext.h"
28#include "lldb/Target/Process.h"
Greg Clayton514487e2011-02-15 21:59:32 +000029#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030
31using namespace lldb;
32using namespace lldb_private;
33
34Value::Value() :
Greg Clayton644247c2011-07-07 01:59:51 +000035 m_value (),
36 m_value_type (eValueTypeScalar),
37 m_context (NULL),
38 m_context_type (eContextTypeInvalid),
39 m_data_buffer ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040{
41}
42
43Value::Value(const Scalar& scalar) :
Greg Clayton644247c2011-07-07 01:59:51 +000044 m_value (scalar),
45 m_value_type (eValueTypeScalar),
46 m_context (NULL),
47 m_context_type (eContextTypeInvalid),
48 m_data_buffer ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049{
50}
51
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052
53Value::Value(const uint8_t *bytes, int len) :
Greg Clayton644247c2011-07-07 01:59:51 +000054 m_value (),
55 m_value_type (eValueTypeHostAddress),
56 m_context (NULL),
57 m_context_type (eContextTypeInvalid),
58 m_data_buffer ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059{
60 m_data_buffer.CopyData(bytes, len);
61 m_value = (uintptr_t)m_data_buffer.GetBytes();
62}
63
64Value::Value(const Value &v) :
65 m_value(v.m_value),
66 m_value_type(v.m_value_type),
67 m_context(v.m_context),
68 m_context_type(v.m_context_type)
69{
70 if ((uintptr_t)v.m_value.ULongLong(LLDB_INVALID_ADDRESS) == (uintptr_t)v.m_data_buffer.GetBytes())
71 {
72 m_data_buffer.CopyData(v.m_data_buffer.GetBytes(),
73 v.m_data_buffer.GetByteSize());
74
75 m_value = (uintptr_t)m_data_buffer.GetBytes();
76 }
77}
78
Greg Clayton1a65ae12011-01-25 23:55:37 +000079Value &
80Value::operator=(const Value &rhs)
81{
82 if (this != &rhs)
83 {
84 m_value = rhs.m_value;
85 m_value_type = rhs.m_value_type;
86 m_context = rhs.m_context;
87 m_context_type = rhs.m_context_type;
88 if ((uintptr_t)rhs.m_value.ULongLong(LLDB_INVALID_ADDRESS) == (uintptr_t)rhs.m_data_buffer.GetBytes())
89 {
90 m_data_buffer.CopyData(rhs.m_data_buffer.GetBytes(),
91 rhs.m_data_buffer.GetByteSize());
92
93 m_value = (uintptr_t)m_data_buffer.GetBytes();
94 }
95 }
96 return *this;
97}
98
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099void
100Value::Dump (Stream* strm)
101{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102 m_value.GetValue (strm, true);
103 strm->Printf(", value_type = %s, context = %p, context_type = %s",
104 Value::GetValueTypeAsCString(m_value_type),
105 m_context,
106 Value::GetContextTypeAsCString(m_context_type));
107}
108
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109Value::ValueType
110Value::GetValueType() const
111{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112 return m_value_type;
113}
114
Greg Claytone0d378b2011-03-24 21:19:54 +0000115AddressType
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116Value::GetValueAddressType () const
117{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118 switch (m_value_type)
119 {
120 default:
121 case eValueTypeScalar:
122 break;
123 case eValueTypeLoadAddress: return eAddressTypeLoad;
124 case eValueTypeFileAddress: return eAddressTypeFile;
125 case eValueTypeHostAddress: return eAddressTypeHost;
126 }
127 return eAddressTypeInvalid;
128}
129
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130RegisterInfo *
131Value::GetRegisterInfo()
132{
Greg Clayton526e5af2010-11-13 03:52:47 +0000133 if (m_context_type == eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134 return static_cast<RegisterInfo *> (m_context);
135 return NULL;
136}
137
138Type *
139Value::GetType()
140{
Greg Clayton526e5af2010-11-13 03:52:47 +0000141 if (m_context_type == eContextTypeLLDBType)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142 return static_cast<Type *> (m_context);
143 return NULL;
144}
145
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146void
147Value::ResizeData(int len)
148{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149 m_value_type = eValueTypeHostAddress;
150 m_data_buffer.SetByteSize(len);
151 m_value = (uintptr_t)m_data_buffer.GetBytes();
152}
153
154bool
155Value::ValueOf(ExecutionContext *exe_ctx, clang::ASTContext *ast_context)
156{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157 switch (m_context_type)
158 {
159 default:
160 case eContextTypeInvalid:
Greg Clayton644247c2011-07-07 01:59:51 +0000161 case eContextTypeClangType: // clang::Type *
162 case eContextTypeRegisterInfo: // RegisterInfo *
163 case eContextTypeLLDBType: // Type *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164 break;
165
Greg Clayton644247c2011-07-07 01:59:51 +0000166 case eContextTypeVariable: // Variable *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167 ResolveValue(exe_ctx, ast_context);
168 return true;
169 }
170 return false;
171}
172
173size_t
174Value::GetValueByteSize (clang::ASTContext *ast_context, Error *error_ptr)
175{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176 size_t byte_size = 0;
177
178 switch (m_context_type)
179 {
180 default:
181 case eContextTypeInvalid:
182 // If we have no context, there is no way to know how much memory to read
183 if (error_ptr)
184 error_ptr->SetErrorString ("Invalid context type, there is no way to know how much memory to read.");
185 break;
186
Greg Clayton526e5af2010-11-13 03:52:47 +0000187 case eContextTypeClangType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188 if (ast_context == NULL)
189 {
190 if (error_ptr)
191 error_ptr->SetErrorString ("Can't determine size of opaque clang type with NULL ASTContext *.");
192 }
193 else
194 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000195 uint64_t bit_width = ClangASTType::GetClangTypeBitWidth (ast_context, m_context);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196 byte_size = (bit_width + 7 ) / 8;
197 }
198 break;
199
Greg Clayton526e5af2010-11-13 03:52:47 +0000200 case eContextTypeRegisterInfo: // RegisterInfo *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201 if (GetRegisterInfo())
202 byte_size = GetRegisterInfo()->byte_size;
203 else if (error_ptr)
204 error_ptr->SetErrorString ("Can't determine byte size with NULL RegisterInfo *.");
205
206 break;
207
Greg Clayton526e5af2010-11-13 03:52:47 +0000208 case eContextTypeLLDBType: // Type *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209 if (GetType())
210 byte_size = GetType()->GetByteSize();
211 else if (error_ptr)
212 error_ptr->SetErrorString ("Can't determine byte size with NULL Type *.");
213 break;
214
Greg Clayton526e5af2010-11-13 03:52:47 +0000215 case eContextTypeVariable: // Variable *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000216 if (GetVariable())
Jim Inghamb7c91a22011-12-22 17:03:37 +0000217 {
218 if (GetVariable()->GetType())
219 byte_size = GetVariable()->GetType()->GetByteSize();
220 else if (error_ptr)
221 error_ptr->SetErrorString ("Can't determine byte size with NULL Type *.");
222 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223 else if (error_ptr)
224 error_ptr->SetErrorString ("Can't determine byte size with NULL Variable *.");
225 break;
226 }
227
228 if (error_ptr)
229 {
230 if (byte_size == 0)
231 {
232 if (error_ptr->Success())
233 error_ptr->SetErrorString("Unable to determine byte size.");
234 }
235 else
236 {
237 error_ptr->Clear();
238 }
239 }
240 return byte_size;
241}
242
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000243clang_type_t
Greg Clayton1be10fc2010-09-29 01:12:09 +0000244Value::GetClangType ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246 switch (m_context_type)
247 {
248 default:
249 case eContextTypeInvalid:
250 break;
251
Greg Clayton526e5af2010-11-13 03:52:47 +0000252 case eContextTypeClangType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253 return m_context;
254
Greg Clayton526e5af2010-11-13 03:52:47 +0000255 case eContextTypeRegisterInfo:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256 break; // TODO: Eventually convert into a clang type?
257
Greg Clayton526e5af2010-11-13 03:52:47 +0000258 case eContextTypeLLDBType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259 if (GetType())
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000260 return GetType()->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261 break;
262
Greg Clayton526e5af2010-11-13 03:52:47 +0000263 case eContextTypeVariable:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264 if (GetVariable())
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000265 return GetVariable()->GetType()->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000266 break;
267 }
268
269 return NULL;
270}
271
272lldb::Format
273Value::GetValueDefaultFormat ()
274{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275 switch (m_context_type)
276 {
277 default:
278 case eContextTypeInvalid:
279 break;
280
Greg Clayton526e5af2010-11-13 03:52:47 +0000281 case eContextTypeClangType:
Greg Claytone1a916a2010-07-21 22:12:05 +0000282 return ClangASTType::GetFormat (m_context);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283
Greg Clayton526e5af2010-11-13 03:52:47 +0000284 case eContextTypeRegisterInfo:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285 if (GetRegisterInfo())
286 return GetRegisterInfo()->format;
287 break;
288
Greg Clayton526e5af2010-11-13 03:52:47 +0000289 case eContextTypeLLDBType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290 if (GetType())
291 return GetType()->GetFormat();
292 break;
293
Greg Clayton526e5af2010-11-13 03:52:47 +0000294 case eContextTypeVariable:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295 if (GetVariable())
296 return GetVariable()->GetType()->GetFormat();
297 break;
298
299 }
300
301 // Return a good default in case we can't figure anything out
302 return eFormatHex;
303}
304
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000305bool
306Value::GetData (DataExtractor &data)
307{
308 switch (m_value_type)
309 {
310 default:
311 break;
312
313 case eValueTypeScalar:
314 if (m_value.GetData (data))
315 return true;
316 break;
317
318 case eValueTypeLoadAddress:
319 case eValueTypeFileAddress:
320 case eValueTypeHostAddress:
321 if (m_data_buffer.GetByteSize())
322 {
323 data.SetData(m_data_buffer.GetBytes(), m_data_buffer.GetByteSize(), data.GetByteOrder());
324 return true;
325 }
326 break;
327 }
328
329 return false;
330
331}
332
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333Error
Greg Clayton644247c2011-07-07 01:59:51 +0000334Value::GetValueAsData (ExecutionContext *exe_ctx,
335 clang::ASTContext *ast_context,
336 DataExtractor &data,
337 uint32_t data_offset,
338 Module *module)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340 data.Clear();
341
342 Error error;
343 lldb::addr_t address = LLDB_INVALID_ADDRESS;
Greg Claytone0d378b2011-03-24 21:19:54 +0000344 AddressType address_type = eAddressTypeFile;
Greg Claytondea8cb42011-06-29 22:09:02 +0000345 Address file_so_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346 switch (m_value_type)
347 {
348 default:
Greg Clayton7c8a9662010-11-02 01:50:16 +0000349 error.SetErrorStringWithFormat("invalid value type %i", m_value_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350 break;
351
352 case eValueTypeScalar:
Greg Clayton7fb56d02011-02-01 01:31:41 +0000353 data.SetByteOrder (lldb::endian::InlHostByteOrder());
Jim Inghamf6ea93f2011-02-24 21:23:14 +0000354 if (m_context_type == eContextTypeClangType && ast_context)
355 {
356 uint32_t ptr_bit_width = ClangASTType::GetClangTypeBitWidth (ast_context,
357 ClangASTContext::GetVoidPtrType(ast_context, false));
358 uint32_t ptr_byte_size = (ptr_bit_width + 7) / 8;
359 data.SetAddressByteSize (ptr_byte_size);
360 }
361 else
362 data.SetAddressByteSize(sizeof(void *));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363 if (m_value.GetData (data))
364 return error; // Success;
Greg Clayton7c8a9662010-11-02 01:50:16 +0000365 error.SetErrorStringWithFormat("extracting data from value failed");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000366 break;
367
368 case eValueTypeLoadAddress:
369 if (exe_ctx == NULL)
370 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000371 error.SetErrorString ("can't read load address (no execution context)");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372 }
Greg Clayton644247c2011-07-07 01:59:51 +0000373 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000374 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000375 Process *process = exe_ctx->GetProcessPtr();
Greg Clayton644247c2011-07-07 01:59:51 +0000376 if (process == NULL)
377 {
378 error.SetErrorString ("can't read load address (invalid process)");
379 }
380 else
381 {
382 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
383 address_type = eAddressTypeLoad;
384 data.SetByteOrder(process->GetTarget().GetArchitecture().GetByteOrder());
385 data.SetAddressByteSize(process->GetTarget().GetArchitecture().GetAddressByteSize());
386 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387 }
388 break;
389
390 case eValueTypeFileAddress:
Greg Claytondea8cb42011-06-29 22:09:02 +0000391 if (exe_ctx == NULL)
392 {
393 error.SetErrorString ("can't read file address (no execution context)");
394 }
Greg Claytonc14ee322011-09-22 04:58:26 +0000395 else if (exe_ctx->GetTargetPtr() == NULL)
Greg Claytondea8cb42011-06-29 22:09:02 +0000396 {
397 error.SetErrorString ("can't read file address (invalid target)");
398 }
399 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400 {
Greg Clayton644247c2011-07-07 01:59:51 +0000401 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
402 if (address == LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403 {
Greg Clayton644247c2011-07-07 01:59:51 +0000404 error.SetErrorString ("invalid file address");
405 }
406 else
407 {
408 if (module == NULL)
409 {
410 // The only thing we can currently lock down to a module so that
411 // we can resolve a file address, is a variable.
412 Variable *variable = GetVariable();
413 if (variable)
414 {
415 SymbolContext var_sc;
416 variable->CalculateSymbolContext(&var_sc);
417 module = var_sc.module_sp.get();
418 }
419 }
420
421 if (module)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000423 bool resolved = false;
Greg Clayton644247c2011-07-07 01:59:51 +0000424 ObjectFile *objfile = module->GetObjectFile();
425 if (objfile)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426 {
Greg Clayton644247c2011-07-07 01:59:51 +0000427 Address so_addr(address, objfile->GetSectionList());
Greg Claytonc14ee322011-09-22 04:58:26 +0000428 addr_t load_address = so_addr.GetLoadAddress (exe_ctx->GetTargetPtr());
Johnny Chen04ef9492012-02-02 22:11:13 +0000429 bool process_launched_and_stopped = exe_ctx->GetProcessPtr()
430 ? StateIsStoppedState(exe_ctx->GetProcessPtr()->GetState(), true /* must_exist */)
431 : false;
Johnny Chenfd72fbe2012-02-02 19:55:18 +0000432 // Don't use the load address if the process has exited.
433 if (load_address != LLDB_INVALID_ADDRESS && process_launched_and_stopped)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434 {
Greg Clayton644247c2011-07-07 01:59:51 +0000435 resolved = true;
436 address = load_address;
437 address_type = eAddressTypeLoad;
Greg Claytonc14ee322011-09-22 04:58:26 +0000438 data.SetByteOrder(exe_ctx->GetTargetRef().GetArchitecture().GetByteOrder());
439 data.SetAddressByteSize(exe_ctx->GetTargetRef().GetArchitecture().GetAddressByteSize());
Greg Clayton644247c2011-07-07 01:59:51 +0000440 }
441 else
442 {
443 if (so_addr.IsSectionOffset())
Greg Claytonad3843c2010-08-18 18:28:52 +0000444 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000445 resolved = true;
Greg Clayton644247c2011-07-07 01:59:51 +0000446 file_so_addr = so_addr;
447 data.SetByteOrder(objfile->GetByteOrder());
448 data.SetAddressByteSize(objfile->GetAddressByteSize());
Greg Claytonad3843c2010-08-18 18:28:52 +0000449 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451 }
Greg Claytondea8cb42011-06-29 22:09:02 +0000452 if (!resolved)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453 {
Greg Clayton644247c2011-07-07 01:59:51 +0000454 Variable *variable = GetVariable();
455
456 if (module)
457 {
458 if (variable)
459 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%llx for variable '%s' in %s%s%s",
460 address,
461 variable->GetName().AsCString(""),
462 module->GetFileSpec().GetDirectory().GetCString(),
463 module->GetFileSpec().GetDirectory() ? "/" : "",
464 module->GetFileSpec().GetFilename().GetCString());
465 else
466 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%llx in %s%s%s",
467 address,
468 module->GetFileSpec().GetDirectory().GetCString(),
469 module->GetFileSpec().GetDirectory() ? "/" : "",
470 module->GetFileSpec().GetFilename().GetCString());
471 }
Greg Claytondea8cb42011-06-29 22:09:02 +0000472 else
Greg Clayton644247c2011-07-07 01:59:51 +0000473 {
474 if (variable)
475 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%llx for variable '%s'",
476 address,
477 variable->GetName().AsCString(""));
478 else
479 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%llx", address);
480 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481 }
482 }
483 else
484 {
Greg Clayton644247c2011-07-07 01:59:51 +0000485 // Can't convert a file address to anything valid without more
486 // context (which Module it came from)
487 error.SetErrorString ("can't read memory from file address without more context");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488 }
489 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490 }
491 break;
492
493 case eValueTypeHostAddress:
494 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
Enrico Granatab046e032012-04-24 01:23:23 +0000495 address_type = eAddressTypeHost;
496 if (exe_ctx)
497 {
498 Target *target = exe_ctx->GetTargetPtr();
499 if (target)
500 {
501 data.SetByteOrder(target->GetArchitecture().GetByteOrder());
502 data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
503 break;
504 }
505 }
506 // fallback to host settings
Greg Clayton7fb56d02011-02-01 01:31:41 +0000507 data.SetByteOrder(lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508 data.SetAddressByteSize(sizeof(void *));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509 break;
510 }
511
512 // Bail if we encountered any errors
513 if (error.Fail())
514 return error;
515
516 if (address == LLDB_INVALID_ADDRESS)
517 {
Greg Clayton7c8a9662010-11-02 01:50:16 +0000518 error.SetErrorStringWithFormat ("invalid %s address", address_type == eAddressTypeHost ? "host" : "load");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519 return error;
520 }
521
522 // If we got here, we need to read the value from memory
523 uint32_t byte_size = GetValueByteSize (ast_context, &error);
524
525 // Bail if we encountered any errors getting the byte size
526 if (error.Fail())
527 return error;
528
529 // Make sure we have enough room within "data", and if we don't make
530 // something large enough that does
531 if (!data.ValidOffsetForDataOfSize (data_offset, byte_size))
532 {
533 DataBufferSP data_sp(new DataBufferHeap (data_offset + byte_size, '\0'));
534 data.SetData(data_sp);
535 }
536
Greg Claytonc982c762010-07-09 20:39:50 +0000537 uint8_t* dst = const_cast<uint8_t*>(data.PeekData (data_offset, byte_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000538 if (dst != NULL)
539 {
540 if (address_type == eAddressTypeHost)
541 {
542 // The address is an address in this process, so just copy it
543 memcpy (dst, (uint8_t*)NULL + address, byte_size);
544 }
Greg Claytondea8cb42011-06-29 22:09:02 +0000545 else if ((address_type == eAddressTypeLoad) || (address_type == eAddressTypeFile))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000547 if (file_so_addr.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000548 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000549 // We have a file address that we were able to translate into a
550 // section offset address so we might be able to read this from
551 // the object files if we don't have a live process. Lets always
552 // try and read from the process if we have one though since we
553 // want to read the actual value by setting "prefer_file_cache"
554 // to false.
555 const bool prefer_file_cache = false;
Greg Claytonc14ee322011-09-22 04:58:26 +0000556 if (exe_ctx->GetTargetRef().ReadMemory(file_so_addr, prefer_file_cache, dst, byte_size, error) != byte_size)
Greg Claytondea8cb42011-06-29 22:09:02 +0000557 {
Greg Clayton7c8a9662010-11-02 01:50:16 +0000558 error.SetErrorStringWithFormat("read memory from 0x%llx failed", (uint64_t)address);
Greg Claytondea8cb42011-06-29 22:09:02 +0000559 }
560 }
561 else
562 {
Greg Clayton644247c2011-07-07 01:59:51 +0000563 // The execution context might have a NULL process, but it
564 // might have a valid process in the exe_ctx->target, so use
565 // the ExecutionContext::GetProcess accessor to ensure we
566 // get the process if there is one.
Greg Claytonc14ee322011-09-22 04:58:26 +0000567 Process *process = exe_ctx->GetProcessPtr();
Greg Clayton644247c2011-07-07 01:59:51 +0000568
569 if (process)
Greg Claytondea8cb42011-06-29 22:09:02 +0000570 {
Greg Clayton644247c2011-07-07 01:59:51 +0000571 const size_t bytes_read = process->ReadMemory(address, dst, byte_size, error);
572 if (bytes_read != byte_size)
573 error.SetErrorStringWithFormat("read memory from 0x%llx failed (%u of %u bytes read)",
574 (uint64_t)address,
575 (uint32_t)bytes_read,
576 (uint32_t)byte_size);
577 }
578 else
579 {
580 error.SetErrorStringWithFormat("read memory from 0x%llx failed (invalid process)", (uint64_t)address);
Greg Claytondea8cb42011-06-29 22:09:02 +0000581 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582 }
583 }
584 else
585 {
Greg Claytone0d378b2011-03-24 21:19:54 +0000586 error.SetErrorStringWithFormat ("unsupported AddressType value (%i)", address_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587 }
588 }
589 else
590 {
Greg Clayton7c8a9662010-11-02 01:50:16 +0000591 error.SetErrorStringWithFormat ("out of memory");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592 }
593
594 return error;
595}
596
597Scalar &
598Value::ResolveValue(ExecutionContext *exe_ctx, clang::ASTContext *ast_context)
599{
Greg Clayton644247c2011-07-07 01:59:51 +0000600 void *opaque_clang_qual_type = GetClangType();
601 if (opaque_clang_qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000603 switch (m_value_type)
604 {
605 case eValueTypeScalar: // raw scalar value
606 break;
607
608 default:
609 case eValueTypeFileAddress:
610 m_value.Clear();
611 break;
612
613 case eValueTypeLoadAddress: // load address value
614 case eValueTypeHostAddress: // host address value (for memory in the process that is using liblldb)
615 {
Greg Claytone0d378b2011-03-24 21:19:54 +0000616 AddressType address_type = m_value_type == eValueTypeLoadAddress ? eAddressTypeLoad : eAddressTypeHost;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617 lldb::addr_t addr = m_value.ULongLong(LLDB_INVALID_ADDRESS);
618 DataExtractor data;
Greg Claytone1a916a2010-07-21 22:12:05 +0000619 if (ClangASTType::ReadFromMemory (ast_context, opaque_clang_qual_type, exe_ctx, addr, address_type, data))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620 {
Greg Clayton644247c2011-07-07 01:59:51 +0000621 Scalar scalar;
Greg Claytone1a916a2010-07-21 22:12:05 +0000622 if (ClangASTType::GetValueAsScalar (ast_context, opaque_clang_qual_type, data, 0, data.GetByteSize(), scalar))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623 {
624 m_value = scalar;
625 m_value_type = eValueTypeScalar;
626 }
627 else
628 {
629 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes())
630 {
631 m_value.Clear();
632 m_value_type = eValueTypeScalar;
633 }
634 }
635 }
636 else
637 {
638 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes())
639 {
640 m_value.Clear();
641 m_value_type = eValueTypeScalar;
642 }
643 }
644 }
645 break;
646 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647 }
648 return m_value;
649}
650
651Variable *
652Value::GetVariable()
653{
Greg Clayton526e5af2010-11-13 03:52:47 +0000654 if (m_context_type == eContextTypeVariable)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000655 return static_cast<Variable *> (m_context);
656 return NULL;
657}
658
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659const char *
660Value::GetValueTypeAsCString (ValueType value_type)
661{
662 switch (value_type)
663 {
664 case eValueTypeScalar: return "scalar";
665 case eValueTypeFileAddress: return "file address";
666 case eValueTypeLoadAddress: return "load address";
667 case eValueTypeHostAddress: return "host address";
668 };
669 return "???";
670}
671
672const char *
673Value::GetContextTypeAsCString (ContextType context_type)
674{
675 switch (context_type)
676 {
Greg Clayton644247c2011-07-07 01:59:51 +0000677 case eContextTypeInvalid: return "invalid";
678 case eContextTypeClangType: return "clang::Type *";
679 case eContextTypeRegisterInfo: return "RegisterInfo *";
680 case eContextTypeLLDBType: return "Type *";
681 case eContextTypeVariable: return "Variable *";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000682 };
683 return "???";
684}
685
686ValueList::ValueList (const ValueList &rhs)
687{
688 m_values = rhs.m_values;
689}
690
691const ValueList &
692ValueList::operator= (const ValueList &rhs)
693{
694 m_values = rhs.m_values;
695 return *this;
696}
697
698void
699ValueList::PushValue (const Value &value)
700{
701 m_values.push_back (value);
702}
703
704size_t
705ValueList::GetSize()
706{
707 return m_values.size();
708}
709
710Value *
711ValueList::GetValueAtIndex (size_t idx)
712{
713 if (idx < GetSize())
714 {
715 return &(m_values[idx]);
716 }
717 else
718 return NULL;
719}
720
721void
722ValueList::Clear ()
723{
724 m_values.clear();
725}