blob: 2d8ac9147cec7dfd0ad47d5a42012371a358f201 [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"
19#include "lldb/Core/Stream.h"
Greg Claytone1a916a2010-07-21 22:12:05 +000020#include "lldb/Symbol/ClangASTType.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Symbol/ClangASTContext.h"
22#include "lldb/Symbol/ObjectFile.h"
23#include "lldb/Symbol/SymbolContext.h"
24#include "lldb/Symbol/Type.h"
25#include "lldb/Symbol/Variable.h"
26#include "lldb/Target/ExecutionContext.h"
27#include "lldb/Target/Process.h"
Greg Clayton514487e2011-02-15 21:59:32 +000028#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
30using namespace lldb;
31using namespace lldb_private;
32
33Value::Value() :
34 m_value(),
35 m_value_type(eValueTypeScalar),
36 m_context(NULL),
37 m_context_type(eContextTypeInvalid)
38{
39}
40
41Value::Value(const Scalar& scalar) :
42 m_value(scalar),
43 m_value_type(eValueTypeScalar),
44 m_context(NULL),
45 m_context_type(eContextTypeInvalid)
46{
47}
48
49Value::Value(int v) :
50 m_value(v),
51 m_value_type(eValueTypeScalar),
52 m_context(NULL),
53 m_context_type(eContextTypeInvalid)
54{
55}
56
57Value::Value(unsigned int v) :
58 m_value(v),
59 m_value_type(eValueTypeScalar),
60 m_context(NULL),
61 m_context_type(eContextTypeInvalid)
62{
63}
64
65Value::Value(long v) :
66 m_value(v),
67 m_value_type(eValueTypeScalar),
68 m_context(NULL),
69 m_context_type(eContextTypeInvalid)
70{
71}
72
73Value::Value(unsigned long v) :
74 m_value(v),
75 m_value_type(eValueTypeScalar),
76 m_context(NULL),
77 m_context_type(eContextTypeInvalid)
78{
79}
80
81Value::Value(long long v) :
82 m_value(v),
83 m_value_type(eValueTypeScalar),
84 m_context(NULL),
85 m_context_type(eContextTypeInvalid)
86{
87}
88
89Value::Value(unsigned long long v) :
90 m_value(v),
91 m_value_type(eValueTypeScalar),
92 m_context(NULL),
93 m_context_type(eContextTypeInvalid)
94{
95}
96
97Value::Value(float v) :
98 m_value(v),
99 m_value_type(eValueTypeScalar),
100 m_context(NULL),
101 m_context_type(eContextTypeInvalid)
102{
103}
104
105Value::Value(double v) :
106 m_value(v),
107 m_value_type(eValueTypeScalar),
108 m_context(NULL),
109 m_context_type(eContextTypeInvalid)
110{
111}
112
113Value::Value(long double v) :
114 m_value(v),
115 m_value_type(eValueTypeScalar),
116 m_context(NULL),
117 m_context_type(eContextTypeInvalid)
118{
119}
120
121Value::Value(const uint8_t *bytes, int len) :
122 m_value(),
123 m_value_type(eValueTypeHostAddress),
124 m_context(NULL),
125 m_context_type(eContextTypeInvalid)
126{
127 m_data_buffer.CopyData(bytes, len);
128 m_value = (uintptr_t)m_data_buffer.GetBytes();
129}
130
131Value::Value(const Value &v) :
132 m_value(v.m_value),
133 m_value_type(v.m_value_type),
134 m_context(v.m_context),
135 m_context_type(v.m_context_type)
136{
137 if ((uintptr_t)v.m_value.ULongLong(LLDB_INVALID_ADDRESS) == (uintptr_t)v.m_data_buffer.GetBytes())
138 {
139 m_data_buffer.CopyData(v.m_data_buffer.GetBytes(),
140 v.m_data_buffer.GetByteSize());
141
142 m_value = (uintptr_t)m_data_buffer.GetBytes();
143 }
144}
145
Greg Clayton1a65ae12011-01-25 23:55:37 +0000146Value &
147Value::operator=(const Value &rhs)
148{
149 if (this != &rhs)
150 {
151 m_value = rhs.m_value;
152 m_value_type = rhs.m_value_type;
153 m_context = rhs.m_context;
154 m_context_type = rhs.m_context_type;
155 if ((uintptr_t)rhs.m_value.ULongLong(LLDB_INVALID_ADDRESS) == (uintptr_t)rhs.m_data_buffer.GetBytes())
156 {
157 m_data_buffer.CopyData(rhs.m_data_buffer.GetBytes(),
158 rhs.m_data_buffer.GetByteSize());
159
160 m_value = (uintptr_t)m_data_buffer.GetBytes();
161 }
162 }
163 return *this;
164}
165
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166Value *
167Value::CreateProxy()
168{
169 if (m_context_type == eContextTypeValue)
170 return ((Value*)m_context)->CreateProxy ();
171
172 Value *ret = new Value;
173 ret->SetContext(eContextTypeValue, this);
174 return ret;
175}
176
177Value *
178Value::GetProxyTarget()
179{
180 if (m_context_type == eContextTypeValue)
181 return (Value*)m_context;
182 else
183 return NULL;
184}
185
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186void
187Value::Dump (Stream* strm)
188{
189 if (m_context_type == eContextTypeValue)
190 {
191 ((Value*)m_context)->Dump (strm);
192 return;
193 }
194
195 m_value.GetValue (strm, true);
196 strm->Printf(", value_type = %s, context = %p, context_type = %s",
197 Value::GetValueTypeAsCString(m_value_type),
198 m_context,
199 Value::GetContextTypeAsCString(m_context_type));
200}
201
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202Value::ValueType
203Value::GetValueType() const
204{
205 if (m_context_type == eContextTypeValue)
206 return ((Value*)m_context)->GetValueType ();
207
208 return m_value_type;
209}
210
Greg Claytone0d378b2011-03-24 21:19:54 +0000211AddressType
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212Value::GetValueAddressType () const
213{
214 if (m_context_type == eContextTypeValue)
215 return ((Value*)m_context)->GetValueAddressType ();
216
217 switch (m_value_type)
218 {
219 default:
220 case eValueTypeScalar:
221 break;
222 case eValueTypeLoadAddress: return eAddressTypeLoad;
223 case eValueTypeFileAddress: return eAddressTypeFile;
224 case eValueTypeHostAddress: return eAddressTypeHost;
225 }
226 return eAddressTypeInvalid;
227}
228
229
230Value::ContextType
231Value::GetContextType() const
232{
233 if (m_context_type == eContextTypeValue)
234 return ((Value*)m_context)->GetContextType ();
235
236 return m_context_type;
237}
238
239void
240Value::SetValueType (Value::ValueType value_type)
241{
242 if (m_context_type == eContextTypeValue)
243 {
244 ((Value*)m_context)->SetValueType(value_type);
245 return;
246 }
247
248 m_value_type = value_type;
249}
250
251void
252Value::ClearContext ()
253{
254 if (m_context_type == eContextTypeValue)
255 {
256 ((Value*)m_context)->ClearContext();
257 return;
258 }
259
260 m_context = NULL;
261 m_context_type = eContextTypeInvalid;
262}
263
264void
265Value::SetContext (Value::ContextType context_type, void *p)
266{
267 if (m_context_type == eContextTypeValue)
268 {
269 ((Value*)m_context)->SetContext(context_type, p);
270 return;
271 }
272
273 m_context_type = context_type;
274 m_context = p;
275}
276
277RegisterInfo *
278Value::GetRegisterInfo()
279{
280 if (m_context_type == eContextTypeValue)
281 return ((Value*)m_context)->GetRegisterInfo();
282
Greg Clayton526e5af2010-11-13 03:52:47 +0000283 if (m_context_type == eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284 return static_cast<RegisterInfo *> (m_context);
285 return NULL;
286}
287
288Type *
289Value::GetType()
290{
291 if (m_context_type == eContextTypeValue)
292 return ((Value*)m_context)->GetType();
293
Greg Clayton526e5af2010-11-13 03:52:47 +0000294 if (m_context_type == eContextTypeLLDBType)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295 return static_cast<Type *> (m_context);
296 return NULL;
297}
298
299Scalar &
300Value::GetScalar()
301{
302 if (m_context_type == eContextTypeValue)
303 return ((Value*)m_context)->GetScalar();
304
305 return m_value;
306}
307
308void
309Value::ResizeData(int len)
310{
311 if (m_context_type == eContextTypeValue)
312 {
313 ((Value*)m_context)->ResizeData(len);
314 return;
315 }
316
317 m_value_type = eValueTypeHostAddress;
318 m_data_buffer.SetByteSize(len);
319 m_value = (uintptr_t)m_data_buffer.GetBytes();
320}
321
322bool
323Value::ValueOf(ExecutionContext *exe_ctx, clang::ASTContext *ast_context)
324{
325 if (m_context_type == eContextTypeValue)
326 return ((Value*)m_context)->ValueOf(exe_ctx, ast_context);
327
328 switch (m_context_type)
329 {
330 default:
331 case eContextTypeInvalid:
Greg Clayton526e5af2010-11-13 03:52:47 +0000332 case eContextTypeClangType: // clang::Type *
333 case eContextTypeRegisterInfo: // RegisterInfo *
334 case eContextTypeLLDBType: // Type *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335 break;
336
Greg Clayton526e5af2010-11-13 03:52:47 +0000337 case eContextTypeVariable: // Variable *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000338 ResolveValue(exe_ctx, ast_context);
339 return true;
340 }
341 return false;
342}
343
344size_t
345Value::GetValueByteSize (clang::ASTContext *ast_context, Error *error_ptr)
346{
347 if (m_context_type == eContextTypeValue)
348 return ((Value*)m_context)->GetValueByteSize(ast_context, error_ptr);
349
350 size_t byte_size = 0;
351
352 switch (m_context_type)
353 {
354 default:
355 case eContextTypeInvalid:
356 // If we have no context, there is no way to know how much memory to read
357 if (error_ptr)
358 error_ptr->SetErrorString ("Invalid context type, there is no way to know how much memory to read.");
359 break;
360
Greg Clayton526e5af2010-11-13 03:52:47 +0000361 case eContextTypeClangType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362 if (ast_context == NULL)
363 {
364 if (error_ptr)
365 error_ptr->SetErrorString ("Can't determine size of opaque clang type with NULL ASTContext *.");
366 }
367 else
368 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000369 uint64_t bit_width = ClangASTType::GetClangTypeBitWidth (ast_context, m_context);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370 byte_size = (bit_width + 7 ) / 8;
371 }
372 break;
373
Greg Clayton526e5af2010-11-13 03:52:47 +0000374 case eContextTypeRegisterInfo: // RegisterInfo *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375 if (GetRegisterInfo())
376 byte_size = GetRegisterInfo()->byte_size;
377 else if (error_ptr)
378 error_ptr->SetErrorString ("Can't determine byte size with NULL RegisterInfo *.");
379
380 break;
381
Greg Clayton526e5af2010-11-13 03:52:47 +0000382 case eContextTypeLLDBType: // Type *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383 if (GetType())
384 byte_size = GetType()->GetByteSize();
385 else if (error_ptr)
386 error_ptr->SetErrorString ("Can't determine byte size with NULL Type *.");
387 break;
388
Greg Clayton526e5af2010-11-13 03:52:47 +0000389 case eContextTypeVariable: // Variable *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390 if (GetVariable())
391 byte_size = GetVariable()->GetType()->GetByteSize();
392 else if (error_ptr)
393 error_ptr->SetErrorString ("Can't determine byte size with NULL Variable *.");
394 break;
395 }
396
397 if (error_ptr)
398 {
399 if (byte_size == 0)
400 {
401 if (error_ptr->Success())
402 error_ptr->SetErrorString("Unable to determine byte size.");
403 }
404 else
405 {
406 error_ptr->Clear();
407 }
408 }
409 return byte_size;
410}
411
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000412clang_type_t
Greg Clayton1be10fc2010-09-29 01:12:09 +0000413Value::GetClangType ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414{
415 if (m_context_type == eContextTypeValue)
Greg Clayton1be10fc2010-09-29 01:12:09 +0000416 return ((Value*)m_context)->GetClangType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417
418 switch (m_context_type)
419 {
420 default:
421 case eContextTypeInvalid:
422 break;
423
Greg Clayton526e5af2010-11-13 03:52:47 +0000424 case eContextTypeClangType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425 return m_context;
426
Greg Clayton526e5af2010-11-13 03:52:47 +0000427 case eContextTypeRegisterInfo:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000428 break; // TODO: Eventually convert into a clang type?
429
Greg Clayton526e5af2010-11-13 03:52:47 +0000430 case eContextTypeLLDBType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431 if (GetType())
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000432 return GetType()->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433 break;
434
Greg Clayton526e5af2010-11-13 03:52:47 +0000435 case eContextTypeVariable:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436 if (GetVariable())
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000437 return GetVariable()->GetType()->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438 break;
439 }
440
441 return NULL;
442}
443
444lldb::Format
445Value::GetValueDefaultFormat ()
446{
447 if (m_context_type == eContextTypeValue)
448 return ((Value*)m_context)->GetValueDefaultFormat();
449
450 switch (m_context_type)
451 {
452 default:
453 case eContextTypeInvalid:
454 break;
455
Greg Clayton526e5af2010-11-13 03:52:47 +0000456 case eContextTypeClangType:
Greg Claytone1a916a2010-07-21 22:12:05 +0000457 return ClangASTType::GetFormat (m_context);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458
Greg Clayton526e5af2010-11-13 03:52:47 +0000459 case eContextTypeRegisterInfo:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460 if (GetRegisterInfo())
461 return GetRegisterInfo()->format;
462 break;
463
Greg Clayton526e5af2010-11-13 03:52:47 +0000464 case eContextTypeLLDBType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465 if (GetType())
466 return GetType()->GetFormat();
467 break;
468
Greg Clayton526e5af2010-11-13 03:52:47 +0000469 case eContextTypeVariable:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470 if (GetVariable())
471 return GetVariable()->GetType()->GetFormat();
472 break;
473
474 }
475
476 // Return a good default in case we can't figure anything out
477 return eFormatHex;
478}
479
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000480bool
481Value::GetData (DataExtractor &data)
482{
483 switch (m_value_type)
484 {
485 default:
486 break;
487
488 case eValueTypeScalar:
489 if (m_value.GetData (data))
490 return true;
491 break;
492
493 case eValueTypeLoadAddress:
494 case eValueTypeFileAddress:
495 case eValueTypeHostAddress:
496 if (m_data_buffer.GetByteSize())
497 {
498 data.SetData(m_data_buffer.GetBytes(), m_data_buffer.GetByteSize(), data.GetByteOrder());
499 return true;
500 }
501 break;
502 }
503
504 return false;
505
506}
507
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508Error
509Value::GetValueAsData (ExecutionContext *exe_ctx, clang::ASTContext *ast_context, DataExtractor &data, uint32_t data_offset)
510{
511 if (m_context_type == eContextTypeValue)
512 return ((Value*)m_context)->GetValueAsData(exe_ctx, ast_context, data, data_offset);
513
514 data.Clear();
515
516 Error error;
517 lldb::addr_t address = LLDB_INVALID_ADDRESS;
Greg Claytone0d378b2011-03-24 21:19:54 +0000518 AddressType address_type = eAddressTypeFile;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519 switch (m_value_type)
520 {
521 default:
Greg Clayton7c8a9662010-11-02 01:50:16 +0000522 error.SetErrorStringWithFormat("invalid value type %i", m_value_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523 break;
524
525 case eValueTypeScalar:
Greg Clayton7fb56d02011-02-01 01:31:41 +0000526 data.SetByteOrder (lldb::endian::InlHostByteOrder());
Jim Inghamf6ea93f2011-02-24 21:23:14 +0000527 if (m_context_type == eContextTypeClangType && ast_context)
528 {
529 uint32_t ptr_bit_width = ClangASTType::GetClangTypeBitWidth (ast_context,
530 ClangASTContext::GetVoidPtrType(ast_context, false));
531 uint32_t ptr_byte_size = (ptr_bit_width + 7) / 8;
532 data.SetAddressByteSize (ptr_byte_size);
533 }
534 else
535 data.SetAddressByteSize(sizeof(void *));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536 if (m_value.GetData (data))
537 return error; // Success;
Greg Clayton7c8a9662010-11-02 01:50:16 +0000538 error.SetErrorStringWithFormat("extracting data from value failed");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000539 break;
540
541 case eValueTypeLoadAddress:
542 if (exe_ctx == NULL)
543 {
Greg Clayton7c8a9662010-11-02 01:50:16 +0000544 error.SetErrorString ("can't read memory (no execution context)");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000545 }
546 else if (exe_ctx->process == NULL)
547 {
Greg Clayton7c8a9662010-11-02 01:50:16 +0000548 error.SetErrorString ("can't read memory (invalid process)");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000549 }
550 else
551 {
552 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
553 address_type = eAddressTypeLoad;
Greg Clayton514487e2011-02-15 21:59:32 +0000554 data.SetByteOrder(exe_ctx->process->GetTarget().GetArchitecture().GetByteOrder());
555 data.SetAddressByteSize(exe_ctx->process->GetTarget().GetArchitecture().GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000556 }
557 break;
558
559 case eValueTypeFileAddress:
560 {
561 // The only thing we can currently lock down to a module so that
562 // we can resolve a file address, is a variable.
563 Variable *variable = GetVariable();
564
565 if (GetVariable())
566 {
567 lldb::addr_t file_addr = m_value.ULongLong(LLDB_INVALID_ADDRESS);
568 if (file_addr != LLDB_INVALID_ADDRESS)
569 {
570 SymbolContext var_sc;
571 variable->CalculateSymbolContext(&var_sc);
572 if (var_sc.module_sp)
573 {
574 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
575 if (objfile)
576 {
577 Address so_addr(file_addr, objfile->GetSectionList());
Greg Claytonf5e56de2010-09-14 23:36:40 +0000578 address = so_addr.GetLoadAddress (exe_ctx->target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579 if (address != LLDB_INVALID_ADDRESS)
Greg Claytonad3843c2010-08-18 18:28:52 +0000580 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581 address_type = eAddressTypeLoad;
Greg Clayton514487e2011-02-15 21:59:32 +0000582 data.SetByteOrder(exe_ctx->target->GetArchitecture().GetByteOrder());
583 data.SetAddressByteSize(exe_ctx->target->GetArchitecture().GetAddressByteSize());
Greg Claytonad3843c2010-08-18 18:28:52 +0000584 }
585 else
586 {
587 data.SetByteOrder(objfile->GetByteOrder());
588 data.SetAddressByteSize(objfile->GetAddressByteSize());
589 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590 }
591 if (address_type == eAddressTypeFile)
592 error.SetErrorStringWithFormat ("%s is not loaded.\n", var_sc.module_sp->GetFileSpec().GetFilename().AsCString());
593 }
594 else
595 {
Greg Clayton7c8a9662010-11-02 01:50:16 +0000596 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%llx for variable '%s'", file_addr, variable->GetName().AsCString(""));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000597 }
598 }
599 else
600 {
601 error.SetErrorString ("Invalid file address.");
602 }
603 }
604 else
605 {
606 // Can't convert a file address to anything valid without more
607 // context (which Module it came from)
Greg Clayton7c8a9662010-11-02 01:50:16 +0000608 error.SetErrorString ("can't read memory from file address without more context");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609 }
610 }
611 break;
612
613 case eValueTypeHostAddress:
614 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
Greg Clayton7fb56d02011-02-01 01:31:41 +0000615 data.SetByteOrder(lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616 data.SetAddressByteSize(sizeof(void *));
617 address_type = eAddressTypeHost;
618 break;
619 }
620
621 // Bail if we encountered any errors
622 if (error.Fail())
623 return error;
624
625 if (address == LLDB_INVALID_ADDRESS)
626 {
Greg Clayton7c8a9662010-11-02 01:50:16 +0000627 error.SetErrorStringWithFormat ("invalid %s address", address_type == eAddressTypeHost ? "host" : "load");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628 return error;
629 }
630
631 // If we got here, we need to read the value from memory
632 uint32_t byte_size = GetValueByteSize (ast_context, &error);
633
634 // Bail if we encountered any errors getting the byte size
635 if (error.Fail())
636 return error;
637
638 // Make sure we have enough room within "data", and if we don't make
639 // something large enough that does
640 if (!data.ValidOffsetForDataOfSize (data_offset, byte_size))
641 {
642 DataBufferSP data_sp(new DataBufferHeap (data_offset + byte_size, '\0'));
643 data.SetData(data_sp);
644 }
645
Greg Claytonc982c762010-07-09 20:39:50 +0000646 uint8_t* dst = const_cast<uint8_t*>(data.PeekData (data_offset, byte_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647 if (dst != NULL)
648 {
649 if (address_type == eAddressTypeHost)
650 {
651 // The address is an address in this process, so just copy it
652 memcpy (dst, (uint8_t*)NULL + address, byte_size);
653 }
654 else if (address_type == eAddressTypeLoad)
655 {
656 if (exe_ctx->process->ReadMemory(address, dst, byte_size, error) != byte_size)
657 {
658 if (error.Success())
659 error.SetErrorStringWithFormat("read %u bytes of memory from 0x%llx failed", (uint64_t)address, byte_size);
Greg Clayton7c8a9662010-11-02 01:50:16 +0000660 else
661 error.SetErrorStringWithFormat("read memory from 0x%llx failed", (uint64_t)address);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000662 }
663 }
664 else
665 {
Greg Claytone0d378b2011-03-24 21:19:54 +0000666 error.SetErrorStringWithFormat ("unsupported AddressType value (%i)", address_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000667 }
668 }
669 else
670 {
Greg Clayton7c8a9662010-11-02 01:50:16 +0000671 error.SetErrorStringWithFormat ("out of memory");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000672 }
673
674 return error;
675}
676
677Scalar &
678Value::ResolveValue(ExecutionContext *exe_ctx, clang::ASTContext *ast_context)
679{
680 Scalar scalar;
681 if (m_context_type == eContextTypeValue)
682 {
683 // Resolve the proxy
684
Greg Clayton1a65ae12011-01-25 23:55:37 +0000685 Value * rhs = (Value*)m_context;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000686
Greg Clayton1a65ae12011-01-25 23:55:37 +0000687 m_value = rhs->m_value;
688 m_value_type = rhs->m_value_type;
689 m_context = rhs->m_context;
690 m_context_type = rhs->m_context_type;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000691
Greg Clayton1a65ae12011-01-25 23:55:37 +0000692 if ((uintptr_t)rhs->m_value.ULongLong(LLDB_INVALID_ADDRESS) == (uintptr_t)rhs->m_data_buffer.GetBytes())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000693 {
Greg Clayton1a65ae12011-01-25 23:55:37 +0000694 m_data_buffer.CopyData(rhs->m_data_buffer.GetBytes(),
695 rhs->m_data_buffer.GetByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696
697 m_value = (uintptr_t)m_data_buffer.GetBytes();
698 }
699 }
700
Greg Clayton526e5af2010-11-13 03:52:47 +0000701 if (m_context_type == eContextTypeClangType)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702 {
Greg Clayton1be10fc2010-09-29 01:12:09 +0000703 void *opaque_clang_qual_type = GetClangType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704 switch (m_value_type)
705 {
706 case eValueTypeScalar: // raw scalar value
707 break;
708
709 default:
710 case eValueTypeFileAddress:
711 m_value.Clear();
712 break;
713
714 case eValueTypeLoadAddress: // load address value
715 case eValueTypeHostAddress: // host address value (for memory in the process that is using liblldb)
716 {
Greg Claytone0d378b2011-03-24 21:19:54 +0000717 AddressType address_type = m_value_type == eValueTypeLoadAddress ? eAddressTypeLoad : eAddressTypeHost;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718 lldb::addr_t addr = m_value.ULongLong(LLDB_INVALID_ADDRESS);
719 DataExtractor data;
Greg Claytone1a916a2010-07-21 22:12:05 +0000720 if (ClangASTType::ReadFromMemory (ast_context, opaque_clang_qual_type, exe_ctx, addr, address_type, data))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000721 {
Greg Claytone1a916a2010-07-21 22:12:05 +0000722 if (ClangASTType::GetValueAsScalar (ast_context, opaque_clang_qual_type, data, 0, data.GetByteSize(), scalar))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000723 {
724 m_value = scalar;
725 m_value_type = eValueTypeScalar;
726 }
727 else
728 {
729 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes())
730 {
731 m_value.Clear();
732 m_value_type = eValueTypeScalar;
733 }
734 }
735 }
736 else
737 {
738 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes())
739 {
740 m_value.Clear();
741 m_value_type = eValueTypeScalar;
742 }
743 }
744 }
745 break;
746 }
747
748
749 }
750 return m_value;
751}
752
753Variable *
754Value::GetVariable()
755{
756 if (m_context_type == eContextTypeValue)
757 return ((Value*)m_context)->GetVariable();
758
Greg Clayton526e5af2010-11-13 03:52:47 +0000759 if (m_context_type == eContextTypeVariable)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000760 return static_cast<Variable *> (m_context);
761 return NULL;
762}
763
764
765
766const char *
767Value::GetValueTypeAsCString (ValueType value_type)
768{
769 switch (value_type)
770 {
771 case eValueTypeScalar: return "scalar";
772 case eValueTypeFileAddress: return "file address";
773 case eValueTypeLoadAddress: return "load address";
774 case eValueTypeHostAddress: return "host address";
775 };
776 return "???";
777}
778
779const char *
780Value::GetContextTypeAsCString (ContextType context_type)
781{
782 switch (context_type)
783 {
784 case eContextTypeInvalid: return "invalid";
Greg Clayton526e5af2010-11-13 03:52:47 +0000785 case eContextTypeClangType: return "clang::Type *";
786 case eContextTypeRegisterInfo: return "RegisterInfo *";
787 case eContextTypeLLDBType: return "Type *";
788 case eContextTypeVariable: return "Variable *";
Greg Claytonc982c762010-07-09 20:39:50 +0000789 case eContextTypeValue: return "Value"; // TODO: Sean, more description here?
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000790 };
791 return "???";
792}
793
794ValueList::ValueList (const ValueList &rhs)
795{
796 m_values = rhs.m_values;
797}
798
799const ValueList &
800ValueList::operator= (const ValueList &rhs)
801{
802 m_values = rhs.m_values;
803 return *this;
804}
805
806void
807ValueList::PushValue (const Value &value)
808{
809 m_values.push_back (value);
810}
811
812size_t
813ValueList::GetSize()
814{
815 return m_values.size();
816}
817
818Value *
819ValueList::GetValueAtIndex (size_t idx)
820{
821 if (idx < GetSize())
822 {
823 return &(m_values[idx]);
824 }
825 else
826 return NULL;
827}
828
829void
830ValueList::Clear ()
831{
832 m_values.clear();
833}