Newer
Older
//===-- IRInterpreter.cpp ---------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lldb/Core/DataEncoder.h"
#include "lldb/Core/Log.h"
#include "lldb/Core/ValueObjectConstResult.h"
#include "lldb/Expression/ClangExpressionDeclMap.h"
#include "lldb/Expression/ClangExpressionVariable.h"
#include "lldb/Expression/IRForTarget.h"
#include "lldb/Expression/IRInterpreter.h"
Chandler Carruth
committed
#include "llvm/IR/Constants.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/raw_ostream.h"
Chandler Carruth
committed
#include "llvm/IR/DataLayout.h"
#include <map>
using namespace llvm;
IRInterpreter::IRInterpreter(lldb_private::ClangExpressionDeclMap &decl_map,
lldb_private::Stream *error_stream) :
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
{
}
IRInterpreter::~IRInterpreter()
{
}
static std::string
PrintValue(const Value *value, bool truncate = false)
{
std::string s;
raw_string_ostream rso(s);
value->print(rso);
rso.flush();
if (truncate)
s.resize(s.length() - 1);
size_t offset;
while ((offset = s.find('\n')) != s.npos)
s.erase(offset, 1);
while (s[0] == ' ' || s[0] == '\t')
s.erase(0, 1);
return s;
}
static std::string
PrintType(const Type *type, bool truncate = false)
{
std::string s;
raw_string_ostream rso(s);
type->print(rso);
rso.flush();
if (truncate)
s.resize(s.length() - 1);
return s;
}
typedef STD_SHARED_PTR(lldb_private::DataEncoder) DataEncoderSP;
typedef STD_SHARED_PTR(lldb_private::DataExtractor) DataExtractorSP;
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
class Memory
{
public:
typedef uint32_t index_t;
struct Allocation
{
// m_virtual_address is always the address of the variable in the virtual memory
// space provided by Memory.
//
// m_origin is always non-NULL and describes the source of the data (possibly
// m_data if this allocation is the authoritative source).
//
// Possible value configurations:
//
// Allocation type getValueType() getContextType() m_origin->GetScalar() m_data
// =========================================================================================================================
// FileAddress eValueTypeFileAddress eContextTypeInvalid A location in a binary NULL
// image
//
// LoadAddress eValueTypeLoadAddress eContextTypeInvalid A location in the target's NULL
// virtual memory
//
// Alloca eValueTypeHostAddress eContextTypeInvalid == m_data->GetBytes() Deleted at end of
// execution
//
// PersistentVar eValueTypeHostAddress eContextTypeClangType A persistent variable's NULL
// location in LLDB's memory
//
// Register [ignored] eContextTypeRegister [ignored] Flushed to the register
// at the end of execution
lldb::addr_t m_virtual_address;
size_t m_extent;
lldb_private::Value m_origin;
lldb::DataBufferSP m_data;
Allocation (lldb::addr_t virtual_address,
size_t extent,
lldb::DataBufferSP data) :
m_virtual_address(virtual_address),
m_extent(extent),
m_data(data)
{
}
Allocation (const Allocation &allocation) :
m_virtual_address(allocation.m_virtual_address),
m_extent(allocation.m_extent),
m_origin(allocation.m_origin),
m_data(allocation.m_data)
{
}
};
typedef STD_SHARED_PTR(Allocation) AllocationSP;
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
struct Region
{
AllocationSP m_allocation;
uint64_t m_base;
uint64_t m_extent;
Region () :
m_allocation(),
m_base(0),
m_extent(0)
{
}
Region (AllocationSP allocation, uint64_t base, uint64_t extent) :
m_allocation(allocation),
m_base(base),
m_extent(extent)
{
}
Region (const Region ®ion) :
m_allocation(region.m_allocation),
m_base(region.m_base),
m_extent(region.m_extent)
{
}
bool IsValid ()
{
Jim Ingham
committed
return (bool) m_allocation;
}
bool IsInvalid ()
{
}
};
typedef std::vector <AllocationSP> MemoryMap;
private:
lldb::addr_t m_addr_base;
lldb::addr_t m_addr_max;
MemoryMap m_memory;
lldb::ByteOrder m_byte_order;
lldb::addr_t m_addr_byte_size;
lldb_private::ClangExpressionDeclMap &m_decl_map;
MemoryMap::iterator LookupInternal (lldb::addr_t addr)
{
for (MemoryMap::iterator i = m_memory.begin(), e = m_memory.end();
i != e;
++i)
{
if ((*i)->m_virtual_address <= addr &&
(*i)->m_virtual_address + (*i)->m_extent > addr)
return i;
}
return m_memory.end();
}
public:
lldb_private::ClangExpressionDeclMap &decl_map,
lldb::addr_t alloc_start,
lldb::addr_t alloc_max) :
m_addr_base(alloc_start),
m_addr_max(alloc_max),
m_target_data(target_data),
m_decl_map(decl_map)
{
m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig);
Micah Villmow
committed
m_addr_byte_size = (target_data.getPointerSize(0));
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
}
Region Malloc (size_t size, size_t align)
{
lldb::DataBufferSP data(new lldb_private::DataBufferHeap(size, 0));
if (data)
{
index_t index = m_memory.size();
const size_t mask = (align - 1);
m_addr_base += mask;
m_addr_base &= ~mask;
if (m_addr_base + size < m_addr_base ||
m_addr_base + size > m_addr_max)
return Region();
uint64_t base = m_addr_base;
m_memory.push_back(AllocationSP(new Allocation(base, size, data)));
m_addr_base += size;
AllocationSP alloc = m_memory[index];
alloc->m_origin.GetScalar() = (unsigned long long)data->GetBytes();
alloc->m_origin.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
alloc->m_origin.SetValueType(lldb_private::Value::eValueTypeHostAddress);
return Region(alloc, base, size);
}
return Region();
}
Region Malloc (Type *type)
{
return Malloc (m_target_data.getTypeAllocSize(type),
m_target_data.getPrefTypeAlignment(type));
}
Region Place (Type *type, lldb::addr_t base, lldb_private::Value &value)
{
index_t index = m_memory.size();
size_t size = m_target_data.getTypeAllocSize(type);
m_memory.push_back(AllocationSP(new Allocation(base, size, lldb::DataBufferSP())));
AllocationSP alloc = m_memory[index];
alloc->m_origin = value;
return Region(alloc, base, size);
}
void Free (lldb::addr_t addr)
{
MemoryMap::iterator i = LookupInternal (addr);
if (i != m_memory.end())
m_memory.erase(i);
}
Region Lookup (lldb::addr_t addr, Type *type)
{
MemoryMap::iterator i = LookupInternal(addr);
if (i == m_memory.end() || !type->isSized())
size_t size = m_target_data.getTypeStoreSize(type);
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
return Region(*i, addr, size);
}
DataEncoderSP GetEncoder (Region region)
{
if (region.m_allocation->m_origin.GetValueType() != lldb_private::Value::eValueTypeHostAddress)
return DataEncoderSP();
lldb::DataBufferSP buffer = region.m_allocation->m_data;
if (!buffer)
return DataEncoderSP();
size_t base_offset = (size_t)(region.m_base - region.m_allocation->m_virtual_address);
return DataEncoderSP(new lldb_private::DataEncoder(buffer->GetBytes() + base_offset, region.m_extent, m_byte_order, m_addr_byte_size));
}
DataExtractorSP GetExtractor (Region region)
{
if (region.m_allocation->m_origin.GetValueType() != lldb_private::Value::eValueTypeHostAddress)
return DataExtractorSP();
lldb::DataBufferSP buffer = region.m_allocation->m_data;
size_t base_offset = (size_t)(region.m_base - region.m_allocation->m_virtual_address);
if (buffer)
return DataExtractorSP(new lldb_private::DataExtractor(buffer->GetBytes() + base_offset, region.m_extent, m_byte_order, m_addr_byte_size));
else
return DataExtractorSP(new lldb_private::DataExtractor((uint8_t*)region.m_allocation->m_origin.GetScalar().ULongLong() + base_offset, region.m_extent, m_byte_order, m_addr_byte_size));
}
lldb_private::Value GetAccessTarget(lldb::addr_t addr)
{
MemoryMap::iterator i = LookupInternal(addr);
if (i == m_memory.end())
return lldb_private::Value();
lldb_private::Value target = (*i)->m_origin;
if (target.GetContextType() == lldb_private::Value::eContextTypeRegisterInfo)
{
target.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
target.SetValueType(lldb_private::Value::eValueTypeHostAddress);
target.GetScalar() = (unsigned long long)(*i)->m_data->GetBytes();
}
target.GetScalar() += (addr - (*i)->m_virtual_address);
return target;
}
bool Write (lldb::addr_t addr, const uint8_t *data, size_t length)
{
lldb_private::Value target = GetAccessTarget(addr);
return m_decl_map.WriteTarget(target, data, length);
}
bool Read (uint8_t *data, lldb::addr_t addr, size_t length)
{
lldb_private::Value source = GetAccessTarget(addr);
return m_decl_map.ReadTarget(data, source, length);
}
bool WriteToRawPtr (lldb::addr_t addr, const uint8_t *data, size_t length)
{
lldb_private::Value target = m_decl_map.WrapBareAddress(addr);
return m_decl_map.WriteTarget(target, data, length);
}
bool ReadFromRawPtr (uint8_t *data, lldb::addr_t addr, size_t length)
{
lldb_private::Value source = m_decl_map.WrapBareAddress(addr);
return m_decl_map.ReadTarget(data, source, length);
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
}
std::string PrintData (lldb::addr_t addr, size_t length)
{
lldb_private::Value target = GetAccessTarget(addr);
lldb_private::DataBufferHeap buf(length, 0);
if (!m_decl_map.ReadTarget(buf.GetBytes(), target, length))
return std::string("<couldn't read data>");
lldb_private::StreamString ss;
for (size_t i = 0; i < length; i++)
{
if ((!(i & 0xf)) && i)
ss.Printf("%02hhx - ", buf.GetBytes()[i]);
else
ss.Printf("%02hhx ", buf.GetBytes()[i]);
}
return ss.GetString();
}
std::string SummarizeRegion (Region ®ion)
{
lldb_private::StreamString ss;
lldb_private::Value base = GetAccessTarget(region.m_base);
ss.Printf("%" PRIx64 " [%s - %s %llx]",
region.m_base,
lldb_private::Value::GetValueTypeAsCString(base.GetValueType()),
lldb_private::Value::GetContextTypeAsCString(base.GetContextType()),
base.GetScalar().ULongLong());
ss.Printf(" %s", PrintData(region.m_base, region.m_extent).c_str());
return ss.GetString();
}
};
class InterpreterStackFrame
{
public:
typedef std::map <const Value*, Memory::Region> ValueMap;
ValueMap m_values;
Memory &m_memory;
lldb_private::ClangExpressionDeclMap &m_decl_map;
const BasicBlock *m_bb;
BasicBlock::const_iterator m_ii;
BasicBlock::const_iterator m_ie;
lldb::ByteOrder m_byte_order;
size_t m_addr_byte_size;
InterpreterStackFrame (DataLayout &target_data,
Memory &memory,
lldb_private::ClangExpressionDeclMap &decl_map) :
m_memory (memory),
m_target_data (target_data),
m_decl_map (decl_map)
{
m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle : lldb::eByteOrderBig);
m_addr_byte_size = (target_data.getPointerSize(0));
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
}
void Jump (const BasicBlock *bb)
{
m_bb = bb;
m_ii = m_bb->begin();
m_ie = m_bb->end();
}
bool Cache (Memory::AllocationSP allocation, Type *type)
{
if (allocation->m_origin.GetContextType() != lldb_private::Value::eContextTypeRegisterInfo)
return false;
return m_decl_map.ReadTarget(allocation->m_data->GetBytes(), allocation->m_origin, allocation->m_data->GetByteSize());
}
std::string SummarizeValue (const Value *value)
{
lldb_private::StreamString ss;
ss.Printf("%s", PrintValue(value).c_str());
ValueMap::iterator i = m_values.find(value);
if (i != m_values.end())
{
Memory::Region region = i->second;
ss.Printf(" %s", m_memory.SummarizeRegion(region).c_str());
}
return ss.GetString();
}
bool AssignToMatchType (lldb_private::Scalar &scalar, uint64_t u64value, Type *type)
{
size_t type_size = m_target_data.getTypeStoreSize(type);
switch (type_size)
{
case 1:
scalar = (uint8_t)u64value;
break;
case 2:
scalar = (uint16_t)u64value;
break;
case 4:
scalar = (uint32_t)u64value;
break;
case 8:
scalar = (uint64_t)u64value;
break;
default:
return false;
}
return true;
}
bool EvaluateValue (lldb_private::Scalar &scalar, const Value *value, Module &module)
{
const Constant *constant = dyn_cast<Constant>(value);
if (constant)
{
if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
{
return AssignToMatchType(scalar, constant_int->getLimitedValue(), value->getType());
}
}
else
{
Memory::Region region = ResolveValue(value, module);
DataExtractorSP value_extractor = m_memory.GetExtractor(region);
if (!value_extractor)
return false;
size_t value_size = m_target_data.getTypeStoreSize(value->getType());
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
uint64_t u64value = value_extractor->GetMaxU64(&offset, value_size);
return AssignToMatchType(scalar, u64value, value->getType());
}
return false;
}
bool AssignValue (const Value *value, lldb_private::Scalar &scalar, Module &module)
{
Memory::Region region = ResolveValue (value, module);
lldb_private::Scalar cast_scalar;
if (!AssignToMatchType(cast_scalar, scalar.GetRawBits64(0), value->getType()))
return false;
lldb_private::DataBufferHeap buf(cast_scalar.GetByteSize(), 0);
lldb_private::Error err;
if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(), m_byte_order, err))
return false;
DataEncoderSP region_encoder = m_memory.GetEncoder(region);
Greg Clayton
committed
if (buf.GetByteSize() > region_encoder->GetByteSize())
return false; // TODO figure out why this happens; try "expr int i = 12; i"
memcpy(region_encoder->GetDataStart(), buf.GetBytes(), buf.GetByteSize());
return true;
}
bool ResolveConstantValue (APInt &value, const Constant *constant)
{
if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant))
{
value = constant_int->getValue();
return true;
else if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant))
value = constant_fp->getValueAPF().bitcastToAPInt();
return true;
else if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant))
{
switch (constant_expr->getOpcode())
{
default:
return false;
case Instruction::IntToPtr:
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
case Instruction::BitCast:
return ResolveConstantValue(value, constant_expr->getOperand(0));
case Instruction::GetElementPtr:
{
ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
ConstantExpr::const_op_iterator op_end = constant_expr->op_end();
Constant *base = dyn_cast<Constant>(*op_cursor);
if (!base)
return false;
if (!ResolveConstantValue(value, base))
return false;
op_cursor++;
if (op_cursor == op_end)
return true; // no offset to apply!
SmallVector <Value *, 8> indices (op_cursor, op_end);
uint64_t offset = m_target_data.getIndexedOffset(base->getType(), indices);
const bool is_signed = true;
value += APInt(value.getBitWidth(), offset, is_signed);
return true;
}
return false;
}
bool ResolveConstant (Memory::Region ®ion, const Constant *constant)
{
APInt resolved_value;
if (!ResolveConstantValue(resolved_value, constant))
return false;
const uint64_t *raw_data = resolved_value.getRawData();
size_t constant_size = m_target_data.getTypeStoreSize(constant->getType());
return m_memory.Write(region.m_base, (const uint8_t*)raw_data, constant_size);
}
Memory::Region ResolveValue (const Value *value, Module &module)
{
ValueMap::iterator i = m_values.find(value);
if (i != m_values.end())
return i->second;
const GlobalValue *global_value = dyn_cast<GlobalValue>(value);
// If the variable is indirected through the argument
// array then we need to build an extra level of indirection
// for it. This is the default; only magic arguments like
// "this", "self", and "_cmd" are direct.
bool variable_is_this = false;
// Attempt to resolve the value using the program's data.
// If it is, the values to be created are:
//
// data_region - a region of memory in which the variable's data resides.
// ref_region - a region of memory in which its address (i.e., &var) resides.
// In the JIT case, this region would be a member of the struct passed in.
// pointer_region - a region of memory in which the address of the pointer
// resides. This is an IR-level variable.
do
{
lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
lldb_private::Value resolved_value;
lldb_private::ClangExpressionVariable::FlagType flags = 0;
if (global_value)
{
clang::NamedDecl *decl = IRForTarget::DeclForGlobal(global_value, &module);
if (!decl)
break;
if (isa<clang::FunctionDecl>(decl))
{
if (log)
log->Printf("The interpreter does not handle function pointers at the moment");
return Memory::Region();
}
resolved_value = m_decl_map.LookupDecl(decl, flags);
}
else
{
// Special-case "this", "self", and "_cmd"
std::string name_str = value->getName().str();
if (name_str == "this" ||
name_str == "self" ||
name_str == "_cmd")
resolved_value = m_decl_map.GetSpecialValue(lldb_private::ConstString(name_str.c_str()));
variable_is_this = true;
if (resolved_value.GetScalar().GetType() != lldb_private::Scalar::e_void)
{
if (resolved_value.GetContextType() == lldb_private::Value::eContextTypeRegisterInfo)
{
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
if (variable_is_this)
{
Memory::Region data_region = m_memory.Place(value->getType(), resolved_value.GetScalar().ULongLong(), resolved_value);
lldb_private::Value origin;
origin.SetValueType(lldb_private::Value::eValueTypeLoadAddress);
origin.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
origin.GetScalar() = resolved_value.GetScalar();
data_region.m_allocation->m_origin = origin;
Memory::Region ref_region = m_memory.Malloc(value->getType());
if (ref_region.IsInvalid())
return Memory::Region();
DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region);
if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX)
return Memory::Region();
if (log)
{
log->Printf("Made an allocation for \"this\" register variable %s", PrintValue(value).c_str());
log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base);
log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base);
}
m_values[value] = ref_region;
return ref_region;
}
else if (flags & lldb_private::ClangExpressionVariable::EVBareRegister)
{
lldb_private::RegisterInfo *reg_info = resolved_value.GetRegisterInfo();
Memory::Region data_region = (reg_info->encoding == lldb::eEncodingVector) ?
m_memory.Malloc(reg_info->byte_size, m_target_data.getPrefTypeAlignment(value->getType())) :
m_memory.Malloc(value->getType());
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
data_region.m_allocation->m_origin = resolved_value;
Memory::Region ref_region = m_memory.Malloc(value->getType());
if (!Cache(data_region.m_allocation, value->getType()))
return Memory::Region();
if (ref_region.IsInvalid())
return Memory::Region();
DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region);
if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX)
return Memory::Region();
if (log)
{
log->Printf("Made an allocation for bare register variable %s", PrintValue(value).c_str());
log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str());
log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base);
log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base);
}
m_values[value] = ref_region;
return ref_region;
}
else
{
lldb_private::RegisterInfo *reg_info = resolved_value.GetRegisterInfo();
Memory::Region data_region = (reg_info->encoding == lldb::eEncodingVector) ?
m_memory.Malloc(reg_info->byte_size, m_target_data.getPrefTypeAlignment(value->getType())) :
m_memory.Malloc(value->getType());
data_region.m_allocation->m_origin = resolved_value;
Memory::Region ref_region = m_memory.Malloc(value->getType());
Memory::Region pointer_region;
pointer_region = m_memory.Malloc(value->getType());
if (!Cache(data_region.m_allocation, value->getType()))
return Memory::Region();
if (ref_region.IsInvalid())
return Memory::Region();
if (pointer_region.IsInvalid())
return Memory::Region();
DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region);
if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX)
return Memory::Region();
if (log)
{
log->Printf("Made an allocation for ordinary register variable %s", PrintValue(value).c_str());
log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str());
log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base);
log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base);
log->Printf(" Pointer region : %llx", (unsigned long long)pointer_region.m_base);
DataEncoderSP pointer_encoder = m_memory.GetEncoder(pointer_region);
if (pointer_encoder->PutAddress(0, ref_region.m_base) == UINT32_MAX)
return Memory::Region();
m_values[value] = pointer_region;
return pointer_region;
}
}
else
{
Memory::Region data_region = m_memory.Place(value->getType(), resolved_value.GetScalar().ULongLong(), resolved_value);
Memory::Region ref_region = m_memory.Malloc(value->getType());
Memory::Region pointer_region;
pointer_region = m_memory.Malloc(value->getType());
if (ref_region.IsInvalid())
return Memory::Region();
if (pointer_region.IsInvalid() && !variable_is_this)
return Memory::Region();
DataEncoderSP ref_encoder = m_memory.GetEncoder(ref_region);
if (ref_encoder->PutAddress(0, data_region.m_base) == UINT32_MAX)
return Memory::Region();
{
DataEncoderSP pointer_encoder = m_memory.GetEncoder(pointer_region);
if (pointer_encoder->PutAddress(0, ref_region.m_base) == UINT32_MAX)
return Memory::Region();
m_values[value] = pointer_region;
}
if (log)
{
log->Printf("Made an allocation for %s", PrintValue(value).c_str());
log->Printf(" Data contents : %s", m_memory.PrintData(data_region.m_base, data_region.m_extent).c_str());
log->Printf(" Data region : %llx", (unsigned long long)data_region.m_base);
log->Printf(" Ref region : %llx", (unsigned long long)ref_region.m_base);
log->Printf(" Pointer region : %llx", (unsigned long long)pointer_region.m_base);
else
return pointer_region;
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
}
}
}
while(0);
// Fall back and allocate space [allocation type Alloca]
Type *type = value->getType();
lldb::ValueSP backing_value(new lldb_private::Value);
Memory::Region data_region = m_memory.Malloc(type);
data_region.m_allocation->m_origin.GetScalar() = (unsigned long long)data_region.m_allocation->m_data->GetBytes();
data_region.m_allocation->m_origin.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
data_region.m_allocation->m_origin.SetValueType(lldb_private::Value::eValueTypeHostAddress);
const Constant *constant = dyn_cast<Constant>(value);
do
{
if (!constant)
break;
if (!ResolveConstant (data_region, constant))
return Memory::Region();
}
while(0);
m_values[value] = data_region;
return data_region;
}
bool ConstructResult (lldb::ClangExpressionVariableSP &result,
const GlobalValue *result_value,
const lldb_private::ConstString &result_name,
lldb_private::TypeFromParser result_type,
Module &module)
{
// The result_value resolves to P, a pointer to a region R containing the result data.
// If the result variable is a reference, the region R contains a pointer to the result R_final in the original process.
if (!result_value)
return true; // There was no slot for a result – the expression doesn't return one.
ValueMap::iterator i = m_values.find(result_value);
if (i == m_values.end())
return false; // There was a slot for the result, but we didn't write into it.
Memory::Region P = i->second;
DataExtractorSP P_extractor = m_memory.GetExtractor(P);
if (!P_extractor)
return false;
Type *pointer_ty = result_value->getType();
PointerType *pointer_ptr_ty = dyn_cast<PointerType>(pointer_ty);
if (!pointer_ptr_ty)
return false;
Type *R_ty = pointer_ptr_ty->getElementType();
lldb::addr_t pointer = P_extractor->GetAddress(&offset);
Memory::Region R = m_memory.Lookup(pointer, R_ty);
if (R.m_allocation->m_origin.GetValueType() != lldb_private::Value::eValueTypeHostAddress ||
!R.m_allocation->m_data)
return false;
lldb_private::Value base;
bool transient = false;
bool maybe_make_load = false;
if (m_decl_map.ResultIsReference(result_name))
{
PointerType *R_ptr_ty = dyn_cast<PointerType>(R_ty);
if (!R_ptr_ty)
return false;
Type *R_final_ty = R_ptr_ty->getElementType();
DataExtractorSP R_extractor = m_memory.GetExtractor(R);
if (!R_extractor)
return false;
offset = 0;
lldb::addr_t R_pointer = R_extractor->GetAddress(&offset);
Memory::Region R_final = m_memory.Lookup(R_pointer, R_final_ty);
if (R_final.m_allocation)
{
if (R_final.m_allocation->m_data)
transient = true; // this is a stack allocation
base = R_final.m_allocation->m_origin;
base.GetScalar() += (R_final.m_base - R_final.m_allocation->m_virtual_address);
}
else
{
// We got a bare pointer. We are going to treat it as a load address
// or a file address, letting decl_map make the choice based on whether
// or not a process exists.
base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
base.SetValueType(lldb_private::Value::eValueTypeFileAddress);
base.GetScalar() = (unsigned long long)R_pointer;
maybe_make_load = true;
}
}
else
{
base.SetContext(lldb_private::Value::eContextTypeInvalid, NULL);
base.SetValueType(lldb_private::Value::eValueTypeHostAddress);
base.GetScalar() = (unsigned long long)R.m_allocation->m_data->GetBytes() + (R.m_base - R.m_allocation->m_virtual_address);
}
return m_decl_map.CompleteResultVariable (result, base, result_name, result_type, transient, maybe_make_load);
}
};
bool
IRInterpreter::maybeRunOnFunction (lldb::ClangExpressionVariableSP &result,
const lldb_private::ConstString &result_name,
lldb_private::TypeFromParser result_type,
Function &llvm_function,
Module &llvm_module,
lldb_private::Error &err)
if (supportsFunction (llvm_function, err))
return runOnFunction(result,
result_name,
result_type,
llvm_function,
llvm_module,
err);
else
return false;
}
static const char *unsupported_opcode_error = "Interpreter doesn't handle one of the expression's opcodes";
static const char *interpreter_initialization_error = "Interpreter couldn't be initialized";
static const char *interpreter_internal_error = "Interpreter encountered an internal error";
static const char *bad_value_error = "Interpreter couldn't resolve a value during execution";
static const char *memory_allocation_error = "Interpreter couldn't allocate memory";
static const char *memory_write_error = "Interpreter couldn't write to memory";
static const char *memory_read_error = "Interpreter couldn't read from memory";
static const char *infinite_loop_error = "Interpreter ran for too many cycles";
static const char *bad_result_error = "Result of expression is in bad memory";
IRInterpreter::supportsFunction (Function &llvm_function,
lldb_private::Error &err)
{
lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
for (Function::iterator bbi = llvm_function.begin(), bbe = llvm_function.end();
bbi != bbe;
++bbi)
{
for (BasicBlock::iterator ii = bbi->begin(), ie = bbi->end();
ii != ie;
++ii)
{
switch (ii->getOpcode())
{
default:
{
if (log)