LLDB mainline
IRInterpreter.cpp
Go to the documentation of this file.
1//===-- IRInterpreter.cpp -------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "lldb/Core/Debugger.h"
11#include "lldb/Core/Module.h"
18#include "lldb/Utility/Endian.h"
20#include "lldb/Utility/Log.h"
21#include "lldb/Utility/Scalar.h"
22#include "lldb/Utility/Status.h"
25
26#include "lldb/Target/ABI.h"
28#include "lldb/Target/Target.h"
29#include "lldb/Target/Thread.h"
32
33#include "llvm/IR/Constants.h"
34#include "llvm/IR/DataLayout.h"
35#include "llvm/IR/Function.h"
36#include "llvm/IR/Instructions.h"
37#include "llvm/IR/Intrinsics.h"
38#include "llvm/IR/LLVMContext.h"
39#include "llvm/IR/Module.h"
40#include "llvm/IR/Operator.h"
41#include "llvm/Support/raw_ostream.h"
42
43#include <map>
44
45using namespace llvm;
47
48static std::string PrintValue(const Value *value, bool truncate = false) {
49 std::string s;
50 raw_string_ostream rso(s);
51 value->print(rso);
52 if (truncate)
53 s.resize(s.length() - 1);
54
55 size_t offset;
56 while ((offset = s.find('\n')) != s.npos)
57 s.erase(offset, 1);
58 while (s[0] == ' ' || s[0] == '\t')
59 s.erase(0, 1);
60
61 return s;
62}
63
64static std::string PrintType(const Type *type, bool truncate = false) {
65 std::string s;
66 raw_string_ostream rso(s);
67 type->print(rso);
68 if (truncate)
69 s.resize(s.length() - 1);
70 return s;
71}
72
73static bool CanIgnoreCall(const CallInst *call) {
74 const llvm::Function *called_function = call->getCalledFunction();
75
76 if (!called_function)
77 return false;
78
79 if (called_function->isIntrinsic()) {
80 switch (called_function->getIntrinsicID()) {
81 default:
82 break;
83 case llvm::Intrinsic::dbg_declare:
84 case llvm::Intrinsic::dbg_value:
85 return true;
86 }
87 }
88
89 return false;
90}
91
93public:
94 typedef std::map<const Value *, lldb::addr_t> ValueMap;
95
97 const DataLayout &m_target_data;
99 const BasicBlock *m_bb = nullptr;
100 const BasicBlock *m_prev_bb = nullptr;
101 BasicBlock::const_iterator m_ii;
102 BasicBlock::const_iterator m_ie;
103
107
110
111 InterpreterStackFrame(const DataLayout &target_data,
112 lldb_private::IRExecutionUnit &execution_unit,
113 lldb::addr_t stack_frame_bottom,
114 lldb::addr_t stack_frame_top)
115 : m_target_data(target_data), m_execution_unit(execution_unit) {
116 m_byte_order = (target_data.isLittleEndian() ? lldb::eByteOrderLittle
118 m_addr_byte_size = (target_data.getPointerSize(0));
119
120 m_frame_process_address = stack_frame_bottom;
121 m_frame_size = stack_frame_top - stack_frame_bottom;
122 m_stack_pointer = stack_frame_top;
123 }
124
126
127 void Jump(const BasicBlock *bb) {
128 m_prev_bb = m_bb;
129 m_bb = bb;
130 m_ii = m_bb->begin();
131 m_ie = m_bb->end();
132 }
133
134 std::string SummarizeValue(const Value *value) {
136
137 ss.Printf("%s", PrintValue(value).c_str());
138
139 ValueMap::iterator i = m_values.find(value);
140
141 if (i != m_values.end()) {
142 lldb::addr_t addr = i->second;
143
144 ss.Printf(" 0x%llx", (unsigned long long)addr);
145 }
146
147 return std::string(ss.GetString());
148 }
149
150 bool AssignToMatchType(lldb_private::Scalar &scalar, llvm::APInt value,
151 Type *type) {
152 size_t type_size = m_target_data.getTypeStoreSize(type);
153
154 if (type_size > 8)
155 return false;
156
157 if (type_size != 1)
158 type_size = PowerOf2Ceil(type_size);
159
160 scalar = value.zextOrTrunc(type_size * 8);
161 return true;
162 }
163
164 bool EvaluateValue(lldb_private::Scalar &scalar, const Value *value,
165 Module &module) {
166 const Constant *constant = dyn_cast<Constant>(value);
167
168 if (constant) {
169 if (constant->getValueID() == Value::ConstantFPVal) {
170 if (auto *cfp = dyn_cast<ConstantFP>(constant)) {
171 if (cfp->getType()->isDoubleTy())
172 scalar = cfp->getValueAPF().convertToDouble();
173 else if (cfp->getType()->isFloatTy())
174 scalar = cfp->getValueAPF().convertToFloat();
175 else
176 return false;
177 return true;
178 }
179 return false;
180 }
181 APInt value_apint;
182
183 if (!ResolveConstantValue(value_apint, constant))
184 return false;
185
186 return AssignToMatchType(scalar, value_apint, value->getType());
187 }
188
189 lldb::addr_t process_address = ResolveValue(value, module);
190 size_t value_size = m_target_data.getTypeStoreSize(value->getType());
191
192 lldb_private::DataExtractor value_extractor;
193 lldb_private::Status extract_error;
194
195 m_execution_unit.GetMemoryData(value_extractor, process_address,
196 value_size, extract_error);
197
198 if (!extract_error.Success())
199 return false;
200
201 lldb::offset_t offset = 0;
202 if (value_size <= 8) {
203 Type *ty = value->getType();
204 if (ty->isDoubleTy()) {
205 scalar = value_extractor.GetDouble(&offset);
206 return true;
207 } else if (ty->isFloatTy()) {
208 scalar = value_extractor.GetFloat(&offset);
209 return true;
210 } else {
211 uint64_t u64value = value_extractor.GetMaxU64(&offset, value_size);
212 return AssignToMatchType(scalar, llvm::APInt(64, u64value),
213 value->getType());
214 }
215 }
216
217 return false;
218 }
219
220 bool AssignValue(const Value *value, lldb_private::Scalar scalar,
221 Module &module) {
222 lldb::addr_t process_address = ResolveValue(value, module);
223
224 if (process_address == LLDB_INVALID_ADDRESS)
225 return false;
226
227 lldb_private::Scalar cast_scalar;
228 Type *vty = value->getType();
229 if (vty->isFloatTy() || vty->isDoubleTy()) {
230 cast_scalar = scalar;
231 } else {
232 scalar.MakeUnsigned();
233 if (!AssignToMatchType(cast_scalar, scalar.UInt128(llvm::APInt()),
234 value->getType()))
235 return false;
236 }
237
238 size_t value_byte_size = m_target_data.getTypeStoreSize(value->getType());
239
240 lldb_private::DataBufferHeap buf(value_byte_size, 0);
241
242 lldb_private::Status get_data_error;
243
244 if (!cast_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(),
245 m_byte_order, get_data_error))
246 return false;
247
248 lldb_private::Status write_error;
249
250 m_execution_unit.WriteMemory(process_address, buf.GetBytes(),
251 buf.GetByteSize(), write_error);
252
253 return write_error.Success();
254 }
255
256 bool ResolveConstantValue(APInt &value, const Constant *constant) {
257 switch (constant->getValueID()) {
258 default:
259 break;
260 case Value::FunctionVal:
261 if (const Function *constant_func = dyn_cast<Function>(constant)) {
263 llvm::GlobalValue::dropLLVMManglingEscape(
264 constant_func->getName()));
265 bool missing_weak = false;
266 lldb::addr_t addr = m_execution_unit.FindSymbol(name, missing_weak);
267 if (addr == LLDB_INVALID_ADDRESS)
268 return false;
269 value = APInt(m_target_data.getPointerSizeInBits(), addr);
270 return true;
271 }
272 break;
273 case Value::ConstantIntVal:
274 if (const ConstantInt *constant_int = dyn_cast<ConstantInt>(constant)) {
275 value = constant_int->getValue();
276 return true;
277 }
278 break;
279 case Value::ConstantFPVal:
280 if (const ConstantFP *constant_fp = dyn_cast<ConstantFP>(constant)) {
281 value = constant_fp->getValueAPF().bitcastToAPInt();
282 return true;
283 }
284 break;
285 case Value::ConstantExprVal:
286 if (const ConstantExpr *constant_expr =
287 dyn_cast<ConstantExpr>(constant)) {
288 switch (constant_expr->getOpcode()) {
289 default:
290 return false;
291 case Instruction::IntToPtr:
292 case Instruction::PtrToInt:
293 case Instruction::BitCast:
294 return ResolveConstantValue(value, constant_expr->getOperand(0));
295 case Instruction::GetElementPtr: {
296 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
297 ConstantExpr::const_op_iterator op_end = constant_expr->op_end();
298
299 Constant *base = dyn_cast<Constant>(*op_cursor);
300
301 if (!base)
302 return false;
303
304 if (!ResolveConstantValue(value, base))
305 return false;
306
307 op_cursor++;
308
309 if (op_cursor == op_end)
310 return true; // no offset to apply!
311
312 SmallVector<Value *, 8> indices(op_cursor, op_end);
313 Type *src_elem_ty =
314 cast<GEPOperator>(constant_expr)->getSourceElementType();
315
316 // DataLayout::getIndexedOffsetInType assumes the indices are
317 // instances of ConstantInt.
318 uint64_t offset =
319 m_target_data.getIndexedOffsetInType(src_elem_ty, indices);
320
321 const bool is_signed = true;
322 value += APInt(value.getBitWidth(), offset, is_signed);
323
324 return true;
325 }
326 }
327 }
328 break;
329 case Value::ConstantPointerNullVal:
330 if (isa<ConstantPointerNull>(constant)) {
331 value = APInt(m_target_data.getPointerSizeInBits(), 0);
332 return true;
333 }
334 break;
335 }
336 return false;
337 }
338
339 bool MakeArgument(const Argument *value, uint64_t address) {
340 lldb::addr_t data_address = Malloc(value->getType());
341
342 if (data_address == LLDB_INVALID_ADDRESS)
343 return false;
344
345 lldb_private::Status write_error;
346
347 m_execution_unit.WritePointerToMemory(data_address, address, write_error);
348
349 if (!write_error.Success()) {
350 lldb_private::Status free_error;
351 m_execution_unit.Free(data_address, free_error);
352 return false;
353 }
354
355 m_values[value] = data_address;
356
357 lldb_private::Log *log(GetLog(LLDBLog::Expressions));
358
359 if (log) {
360 LLDB_LOGF(log, "Made an allocation for argument %s",
361 PrintValue(value).c_str());
362 LLDB_LOGF(log, " Data region : %llx", (unsigned long long)address);
363 LLDB_LOGF(log, " Ref region : %llx",
364 (unsigned long long)data_address);
365 }
366
367 return true;
368 }
369
370 bool ResolveConstant(lldb::addr_t process_address, const Constant *constant) {
371 APInt resolved_value;
372
373 if (!ResolveConstantValue(resolved_value, constant))
374 return false;
375
376 size_t constant_size = m_target_data.getTypeStoreSize(constant->getType());
377 lldb_private::DataBufferHeap buf(constant_size, 0);
378
379 lldb_private::Status get_data_error;
380
381 lldb_private::Scalar resolved_scalar(
382 resolved_value.zextOrTrunc(llvm::NextPowerOf2(constant_size) * 8));
383 if (!resolved_scalar.GetAsMemoryData(buf.GetBytes(), buf.GetByteSize(),
384 m_byte_order, get_data_error))
385 return false;
386
387 lldb_private::Status write_error;
388
389 m_execution_unit.WriteMemory(process_address, buf.GetBytes(),
390 buf.GetByteSize(), write_error);
391
392 return write_error.Success();
393 }
394
395 lldb::addr_t Malloc(size_t size, uint8_t byte_alignment) {
397
398 ret -= size;
399 ret -= (ret % byte_alignment);
400
401 if (ret < m_frame_process_address)
403
404 m_stack_pointer = ret;
405 return ret;
406 }
407
408 lldb::addr_t Malloc(llvm::Type *type) {
409 lldb_private::Status alloc_error;
410
411 return Malloc(m_target_data.getTypeAllocSize(type),
412 m_target_data.getPrefTypeAlign(type).value());
413 }
414
415 std::string PrintData(lldb::addr_t addr, llvm::Type *type) {
416 size_t length = m_target_data.getTypeStoreSize(type);
417
418 lldb_private::DataBufferHeap buf(length, 0);
419
420 lldb_private::Status read_error;
421
422 m_execution_unit.ReadMemory(buf.GetBytes(), addr, length, read_error);
423
424 if (!read_error.Success())
425 return std::string("<couldn't read data>");
426
428
429 for (size_t i = 0; i < length; i++) {
430 if ((!(i & 0xf)) && i)
431 ss.Printf("%02hhx - ", buf.GetBytes()[i]);
432 else
433 ss.Printf("%02hhx ", buf.GetBytes()[i]);
434 }
435
436 return std::string(ss.GetString());
437 }
438
439 lldb::addr_t ResolveValue(const Value *value, Module &module) {
440 ValueMap::iterator i = m_values.find(value);
441
442 if (i != m_values.end())
443 return i->second;
444
445 // Fall back and allocate space [allocation type Alloca]
446
447 lldb::addr_t data_address = Malloc(value->getType());
448
449 if (const Constant *constant = dyn_cast<Constant>(value)) {
450 if (!ResolveConstant(data_address, constant)) {
451 lldb_private::Status free_error;
452 m_execution_unit.Free(data_address, free_error);
454 }
455 }
456
457 m_values[value] = data_address;
458 return data_address;
459 }
460};
461
462static const char *unsupported_opcode_error =
463 "Interpreter doesn't handle one of the expression's opcodes";
464static const char *unsupported_operand_error =
465 "Interpreter doesn't handle one of the expression's operands";
466static const char *interpreter_internal_error =
467 "Interpreter encountered an internal error";
468static const char *interrupt_error =
469 "Interrupted while interpreting expression";
470static const char *bad_value_error =
471 "Interpreter couldn't resolve a value during execution";
472static const char *memory_allocation_error =
473 "Interpreter couldn't allocate memory";
474static const char *memory_write_error = "Interpreter couldn't write to memory";
475static const char *memory_read_error = "Interpreter couldn't read from memory";
476static const char *timeout_error =
477 "Reached timeout while interpreting expression";
478static const char *too_many_functions_error =
479 "Interpreter doesn't handle modules with multiple function bodies.";
480
481static bool CanResolveConstant(llvm::Constant *constant) {
482 switch (constant->getValueID()) {
483 default:
484 return false;
485 case Value::ConstantIntVal:
486 case Value::ConstantFPVal:
487 case Value::FunctionVal:
488 return true;
489 case Value::ConstantExprVal:
490 if (const ConstantExpr *constant_expr = dyn_cast<ConstantExpr>(constant)) {
491 switch (constant_expr->getOpcode()) {
492 default:
493 return false;
494 case Instruction::IntToPtr:
495 case Instruction::PtrToInt:
496 case Instruction::BitCast:
497 return CanResolveConstant(constant_expr->getOperand(0));
498 case Instruction::GetElementPtr: {
499 // Check that the base can be constant-resolved.
500 ConstantExpr::const_op_iterator op_cursor = constant_expr->op_begin();
501 Constant *base = dyn_cast<Constant>(*op_cursor);
502 if (!base || !CanResolveConstant(base))
503 return false;
504
505 // Check that all other operands are just ConstantInt.
506 for (Value *op : make_range(constant_expr->op_begin() + 1,
507 constant_expr->op_end())) {
508 ConstantInt *constant_int = dyn_cast<ConstantInt>(op);
509 if (!constant_int)
510 return false;
511 }
512 return true;
513 }
514 }
515 } else {
516 return false;
517 }
518 case Value::ConstantPointerNullVal:
519 return true;
520 }
521}
522
523bool IRInterpreter::CanInterpret(llvm::Module &module, llvm::Function &function,
525 const bool support_function_calls) {
526 lldb_private::Log *log(GetLog(LLDBLog::Expressions));
527
528 bool saw_function_with_body = false;
529 for (Function &f : module) {
530 if (f.begin() != f.end()) {
531 if (saw_function_with_body) {
532 LLDB_LOGF(log, "More than one function in the module has a body");
534 return false;
535 }
536 saw_function_with_body = true;
537 LLDB_LOGF(log, "Saw function with body: %s", f.getName().str().c_str());
538 }
539 }
540
541 for (BasicBlock &bb : function) {
542 for (Instruction &ii : bb) {
543 switch (ii.getOpcode()) {
544 default: {
545 LLDB_LOGF(log, "Unsupported instruction: %s", PrintValue(&ii).c_str());
547 return false;
548 }
549 case Instruction::Add:
550 case Instruction::Alloca:
551 case Instruction::BitCast:
552 case Instruction::Br:
553 case Instruction::PHI:
554 break;
555 case Instruction::Call: {
556 CallInst *call_inst = dyn_cast<CallInst>(&ii);
557
558 if (!call_inst) {
559 error =
561 return false;
562 }
563
564 if (!CanIgnoreCall(call_inst) && !support_function_calls) {
565 LLDB_LOGF(log, "Unsupported instruction: %s",
566 PrintValue(&ii).c_str());
567 error =
569 return false;
570 }
571 } break;
572 case Instruction::GetElementPtr:
573 break;
574 case Instruction::FCmp:
575 case Instruction::ICmp: {
576 CmpInst *cmp_inst = dyn_cast<CmpInst>(&ii);
577
578 if (!cmp_inst) {
579 error =
581 return false;
582 }
583
584 switch (cmp_inst->getPredicate()) {
585 default: {
586 LLDB_LOGF(log, "Unsupported ICmp predicate: %s",
587 PrintValue(&ii).c_str());
588
589 error =
591 return false;
592 }
593 case CmpInst::FCMP_OEQ:
594 case CmpInst::ICMP_EQ:
595 case CmpInst::FCMP_UNE:
596 case CmpInst::ICMP_NE:
597 case CmpInst::FCMP_OGT:
598 case CmpInst::ICMP_UGT:
599 case CmpInst::FCMP_OGE:
600 case CmpInst::ICMP_UGE:
601 case CmpInst::FCMP_OLT:
602 case CmpInst::ICMP_ULT:
603 case CmpInst::FCMP_OLE:
604 case CmpInst::ICMP_ULE:
605 case CmpInst::ICMP_SGT:
606 case CmpInst::ICMP_SGE:
607 case CmpInst::ICMP_SLT:
608 case CmpInst::ICMP_SLE:
609 break;
610 }
611 } break;
612 case Instruction::And:
613 case Instruction::AShr:
614 case Instruction::IntToPtr:
615 case Instruction::PtrToInt:
616 case Instruction::Load:
617 case Instruction::LShr:
618 case Instruction::Mul:
619 case Instruction::Or:
620 case Instruction::Ret:
621 case Instruction::SDiv:
622 case Instruction::SExt:
623 case Instruction::Shl:
624 case Instruction::SRem:
625 case Instruction::Store:
626 case Instruction::Sub:
627 case Instruction::Trunc:
628 case Instruction::UDiv:
629 case Instruction::URem:
630 case Instruction::Xor:
631 case Instruction::ZExt:
632 break;
633 case Instruction::FAdd:
634 case Instruction::FSub:
635 case Instruction::FMul:
636 case Instruction::FDiv:
637 break;
638 }
639
640 for (unsigned oi = 0, oe = ii.getNumOperands(); oi != oe; ++oi) {
641 Value *operand = ii.getOperand(oi);
642 Type *operand_type = operand->getType();
643
644 switch (operand_type->getTypeID()) {
645 default:
646 break;
647 case Type::FixedVectorTyID:
648 case Type::ScalableVectorTyID: {
649 LLDB_LOGF(log, "Unsupported operand type: %s",
650 PrintType(operand_type).c_str());
651 error =
653 return false;
654 }
655 }
656
657 // The IR interpreter currently doesn't know about
658 // 128-bit integers. As they're not that frequent,
659 // we can just fall back to the JIT rather than
660 // choking.
661 if (operand_type->getPrimitiveSizeInBits() > 64) {
662 LLDB_LOGF(log, "Unsupported operand type: %s",
663 PrintType(operand_type).c_str());
664 error =
666 return false;
667 }
668
669 if (Constant *constant = llvm::dyn_cast<Constant>(operand)) {
670 if (!CanResolveConstant(constant)) {
671 LLDB_LOGF(log, "Unsupported constant: %s",
672 PrintValue(constant).c_str());
675 return false;
676 }
677 }
678 }
679 }
680 }
681
682 return true;
683}
684
685bool IRInterpreter::Interpret(llvm::Module &module, llvm::Function &function,
686 llvm::ArrayRef<lldb::addr_t> args,
687 lldb_private::IRExecutionUnit &execution_unit,
689 lldb::addr_t stack_frame_bottom,
690 lldb::addr_t stack_frame_top,
693 lldb_private::Log *log(GetLog(LLDBLog::Expressions));
694
695 if (log) {
696 std::string s;
697 raw_string_ostream oss(s);
698
699 module.print(oss, nullptr);
700
701 LLDB_LOGF(log, "Module as passed in to IRInterpreter::Interpret: \n\"%s\"",
702 s.c_str());
703 }
704
705 const DataLayout &data_layout = module.getDataLayout();
706
707 InterpreterStackFrame frame(data_layout, execution_unit, stack_frame_bottom,
708 stack_frame_top);
709
711 error =
712 lldb_private::Status::FromErrorString("Couldn't allocate stack frame");
713 }
714
715 int arg_index = 0;
716
717 for (llvm::Function::arg_iterator ai = function.arg_begin(),
718 ae = function.arg_end();
719 ai != ae; ++ai, ++arg_index) {
720 if (args.size() <= static_cast<size_t>(arg_index)) {
722 "Not enough arguments passed in to function");
723 return false;
724 }
725
726 lldb::addr_t ptr = args[arg_index];
727
728 frame.MakeArgument(&*ai, ptr);
729 }
730
731 frame.Jump(&function.front());
732
733 lldb_private::Process *process = exe_ctx.GetProcessPtr();
734 lldb_private::Target *target = exe_ctx.GetTargetPtr();
735
736 using clock = std::chrono::steady_clock;
737
738 // Compute the time at which the timeout has been exceeded.
739 std::optional<clock::time_point> end_time;
740 if (timeout && timeout->count() > 0)
741 end_time = clock::now() + *timeout;
742
743 while (frame.m_ii != frame.m_ie) {
744 // Timeout reached: stop interpreting.
745 if (end_time && clock::now() >= *end_time) {
747 return false;
748 }
749
750 // If we have access to the debugger we can honor an interrupt request.
751 if (target) {
752 if (INTERRUPT_REQUESTED(target->GetDebugger(),
753 "Interrupted in IR interpreting.")) {
755 return false;
756 }
757 }
758
759 const Instruction *inst = &*frame.m_ii;
760
761 LLDB_LOGF(log, "Interpreting %s", PrintValue(inst).c_str());
762
763 switch (inst->getOpcode()) {
764 default:
765 break;
766
767 case Instruction::Add:
768 case Instruction::Sub:
769 case Instruction::Mul:
770 case Instruction::SDiv:
771 case Instruction::UDiv:
772 case Instruction::SRem:
773 case Instruction::URem:
774 case Instruction::Shl:
775 case Instruction::LShr:
776 case Instruction::AShr:
777 case Instruction::And:
778 case Instruction::Or:
779 case Instruction::Xor:
780 case Instruction::FAdd:
781 case Instruction::FSub:
782 case Instruction::FMul:
783 case Instruction::FDiv: {
784 const BinaryOperator *bin_op = dyn_cast<BinaryOperator>(inst);
785
786 if (!bin_op) {
787 LLDB_LOGF(
788 log,
789 "getOpcode() returns %s, but instruction is not a BinaryOperator",
790 inst->getOpcodeName());
791 error =
793 return false;
794 }
795
796 Value *lhs = inst->getOperand(0);
797 Value *rhs = inst->getOperand(1);
798
801
802 if (!frame.EvaluateValue(L, lhs, module)) {
803 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(lhs).c_str());
805 return false;
806 }
807
808 if (!frame.EvaluateValue(R, rhs, module)) {
809 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(rhs).c_str());
811 return false;
812 }
813
815
816 switch (inst->getOpcode()) {
817 default:
818 break;
819 case Instruction::Add:
820 case Instruction::FAdd:
821 result = L + R;
822 break;
823 case Instruction::Mul:
824 case Instruction::FMul:
825 result = L * R;
826 break;
827 case Instruction::Sub:
828 case Instruction::FSub:
829 result = L - R;
830 break;
831 case Instruction::SDiv:
832 L.MakeSigned();
833 R.MakeSigned();
834 result = L / R;
835 break;
836 case Instruction::UDiv:
837 L.MakeUnsigned();
838 R.MakeUnsigned();
839 result = L / R;
840 break;
841 case Instruction::FDiv:
842 result = L / R;
843 break;
844 case Instruction::SRem:
845 L.MakeSigned();
846 R.MakeSigned();
847 result = L % R;
848 break;
849 case Instruction::URem:
850 L.MakeUnsigned();
851 R.MakeUnsigned();
852 result = L % R;
853 break;
854 case Instruction::Shl:
855 result = L << R;
856 break;
857 case Instruction::AShr:
858 result = L >> R;
859 break;
860 case Instruction::LShr:
861 result = L;
862 result.ShiftRightLogical(R);
863 break;
864 case Instruction::And:
865 result = L & R;
866 break;
867 case Instruction::Or:
868 result = L | R;
869 break;
870 case Instruction::Xor:
871 result = L ^ R;
872 break;
873 }
874
875 frame.AssignValue(inst, result, module);
876
877 if (log) {
878 LLDB_LOGF(log, "Interpreted a %s", inst->getOpcodeName());
879 LLDB_LOGF(log, " L : %s", frame.SummarizeValue(lhs).c_str());
880 LLDB_LOGF(log, " R : %s", frame.SummarizeValue(rhs).c_str());
881 LLDB_LOGF(log, " = : %s", frame.SummarizeValue(inst).c_str());
882 }
883 } break;
884 case Instruction::Alloca: {
885 const AllocaInst *alloca_inst = cast<AllocaInst>(inst);
886
887 std::optional<TypeSize> alloca_size =
888 alloca_inst->getAllocationSize(frame.m_target_data);
889 if (!alloca_size || alloca_size->isScalable()) {
890 LLDB_LOGF(log, "AllocaInsts are not handled if size is not computable");
892 return false;
893 }
894
895 // The semantics of Alloca are:
896 // Create a region R of virtual memory of type T, backed by a data
897 // buffer
898 // Create a region P of virtual memory of type T*, backed by a data
899 // buffer
900 // Write the virtual address of R into P
901
902 Type *Tptr = alloca_inst->getType();
903
904 lldb::addr_t R = frame.Malloc(alloca_size->getFixedValue(),
905 alloca_inst->getAlign().value());
906
907 if (R == LLDB_INVALID_ADDRESS) {
908 LLDB_LOGF(log, "Couldn't allocate memory for an AllocaInst");
910 return false;
911 }
912
913 lldb::addr_t P = frame.Malloc(Tptr);
914
915 if (P == LLDB_INVALID_ADDRESS) {
916 LLDB_LOGF(log,
917 "Couldn't allocate the result pointer for an AllocaInst");
919 return false;
920 }
921
922 lldb_private::Status write_error;
923
924 execution_unit.WritePointerToMemory(P, R, write_error);
925
926 if (!write_error.Success()) {
927 LLDB_LOGF(log, "Couldn't write the result pointer for an AllocaInst");
929 lldb_private::Status free_error;
930 execution_unit.Free(P, free_error);
931 execution_unit.Free(R, free_error);
932 return false;
933 }
934
935 frame.m_values[alloca_inst] = P;
936
937 if (log) {
938 LLDB_LOGF(log, "Interpreted an AllocaInst");
939 LLDB_LOGF(log, " R : 0x%" PRIx64, R);
940 LLDB_LOGF(log, " P : 0x%" PRIx64, P);
941 }
942 } break;
943 case Instruction::BitCast:
944 case Instruction::ZExt: {
945 const CastInst *cast_inst = cast<CastInst>(inst);
946
947 Value *source = cast_inst->getOperand(0);
948
950
951 if (!frame.EvaluateValue(S, source, module)) {
952 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(source).c_str());
954 return false;
955 }
956
957 frame.AssignValue(inst, S, module);
958 } break;
959 case Instruction::SExt: {
960 const CastInst *cast_inst = cast<CastInst>(inst);
961
962 Value *source = cast_inst->getOperand(0);
963
965
966 if (!frame.EvaluateValue(S, source, module)) {
967 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(source).c_str());
969 return false;
970 }
971
972 S.MakeSigned();
973
974 lldb_private::Scalar S_signextend(S.SLongLong());
975
976 frame.AssignValue(inst, S_signextend, module);
977 } break;
978 case Instruction::Br: {
979 const BranchInst *br_inst = cast<BranchInst>(inst);
980
981 if (br_inst->isConditional()) {
982 Value *condition = br_inst->getCondition();
983
985
986 if (!frame.EvaluateValue(C, condition, module)) {
987 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(condition).c_str());
989 return false;
990 }
991
992 if (!C.IsZero())
993 frame.Jump(br_inst->getSuccessor(0));
994 else
995 frame.Jump(br_inst->getSuccessor(1));
996
997 if (log) {
998 LLDB_LOGF(log, "Interpreted a BrInst with a condition");
999 LLDB_LOGF(log, " cond : %s",
1000 frame.SummarizeValue(condition).c_str());
1001 }
1002 } else {
1003 frame.Jump(br_inst->getSuccessor(0));
1004
1005 if (log) {
1006 LLDB_LOGF(log, "Interpreted a BrInst with no condition");
1007 }
1008 }
1009 }
1010 continue;
1011 case Instruction::PHI: {
1012 const PHINode *phi_inst = cast<PHINode>(inst);
1013 if (!frame.m_prev_bb) {
1014 LLDB_LOGF(log,
1015 "Encountered PHI node without having jumped from another "
1016 "basic block");
1017 error =
1019 return false;
1020 }
1021
1022 Value *value = phi_inst->getIncomingValueForBlock(frame.m_prev_bb);
1023 lldb_private::Scalar result;
1024 if (!frame.EvaluateValue(result, value, module)) {
1025 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(value).c_str());
1027 return false;
1028 }
1029 frame.AssignValue(inst, result, module);
1030
1031 if (log) {
1032 LLDB_LOGF(log, "Interpreted a %s", inst->getOpcodeName());
1033 LLDB_LOGF(log, " Incoming value : %s",
1034 frame.SummarizeValue(value).c_str());
1035 }
1036 } break;
1037 case Instruction::GetElementPtr: {
1038 const GetElementPtrInst *gep_inst = cast<GetElementPtrInst>(inst);
1039
1040 const Value *pointer_operand = gep_inst->getPointerOperand();
1041 Type *src_elem_ty = gep_inst->getSourceElementType();
1042
1044
1045 if (!frame.EvaluateValue(P, pointer_operand, module)) {
1046 LLDB_LOGF(log, "Couldn't evaluate %s",
1047 PrintValue(pointer_operand).c_str());
1049 return false;
1050 }
1051
1052 typedef SmallVector<Value *, 8> IndexVector;
1053 typedef IndexVector::iterator IndexIterator;
1054
1055 SmallVector<Value *, 8> indices(gep_inst->idx_begin(),
1056 gep_inst->idx_end());
1057
1058 SmallVector<Value *, 8> const_indices;
1059
1060 for (IndexIterator ii = indices.begin(), ie = indices.end(); ii != ie;
1061 ++ii) {
1062 ConstantInt *constant_index = dyn_cast<ConstantInt>(*ii);
1063
1064 if (!constant_index) {
1066
1067 if (!frame.EvaluateValue(I, *ii, module)) {
1068 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(*ii).c_str());
1070 return false;
1071 }
1072
1073 LLDB_LOGF(log, "Evaluated constant index %s as %llu",
1074 PrintValue(*ii).c_str(), I.ULongLong(LLDB_INVALID_ADDRESS));
1075
1076 constant_index = cast<ConstantInt>(ConstantInt::get(
1077 (*ii)->getType(), I.ULongLong(LLDB_INVALID_ADDRESS)));
1078 }
1079
1080 const_indices.push_back(constant_index);
1081 }
1082
1083 uint64_t offset =
1084 data_layout.getIndexedOffsetInType(src_elem_ty, const_indices);
1085
1086 lldb_private::Scalar Poffset = P + offset;
1087
1088 frame.AssignValue(inst, Poffset, module);
1089
1090 if (log) {
1091 LLDB_LOGF(log, "Interpreted a GetElementPtrInst");
1092 LLDB_LOGF(log, " P : %s",
1093 frame.SummarizeValue(pointer_operand).c_str());
1094 LLDB_LOGF(log, " Poffset : %s", frame.SummarizeValue(inst).c_str());
1095 }
1096 } break;
1097 case Instruction::FCmp:
1098 case Instruction::ICmp: {
1099 const CmpInst *icmp_inst = cast<CmpInst>(inst);
1100
1101 CmpInst::Predicate predicate = icmp_inst->getPredicate();
1102
1103 Value *lhs = inst->getOperand(0);
1104 Value *rhs = inst->getOperand(1);
1105
1108
1109 if (!frame.EvaluateValue(L, lhs, module)) {
1110 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(lhs).c_str());
1112 return false;
1113 }
1114
1115 if (!frame.EvaluateValue(R, rhs, module)) {
1116 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(rhs).c_str());
1118 return false;
1119 }
1120
1121 lldb_private::Scalar result;
1122
1123 switch (predicate) {
1124 default:
1125 return false;
1126 case CmpInst::ICMP_EQ:
1127 case CmpInst::FCMP_OEQ:
1128 result = (L == R);
1129 break;
1130 case CmpInst::ICMP_NE:
1131 case CmpInst::FCMP_UNE:
1132 result = (L != R);
1133 break;
1134 case CmpInst::ICMP_UGT:
1135 L.MakeUnsigned();
1136 R.MakeUnsigned();
1137 result = (L > R);
1138 break;
1139 case CmpInst::ICMP_UGE:
1140 L.MakeUnsigned();
1141 R.MakeUnsigned();
1142 result = (L >= R);
1143 break;
1144 case CmpInst::FCMP_OGE:
1145 result = (L >= R);
1146 break;
1147 case CmpInst::FCMP_OGT:
1148 result = (L > R);
1149 break;
1150 case CmpInst::ICMP_ULT:
1151 L.MakeUnsigned();
1152 R.MakeUnsigned();
1153 result = (L < R);
1154 break;
1155 case CmpInst::FCMP_OLT:
1156 result = (L < R);
1157 break;
1158 case CmpInst::ICMP_ULE:
1159 L.MakeUnsigned();
1160 R.MakeUnsigned();
1161 result = (L <= R);
1162 break;
1163 case CmpInst::FCMP_OLE:
1164 result = (L <= R);
1165 break;
1166 case CmpInst::ICMP_SGT:
1167 L.MakeSigned();
1168 R.MakeSigned();
1169 result = (L > R);
1170 break;
1171 case CmpInst::ICMP_SGE:
1172 L.MakeSigned();
1173 R.MakeSigned();
1174 result = (L >= R);
1175 break;
1176 case CmpInst::ICMP_SLT:
1177 L.MakeSigned();
1178 R.MakeSigned();
1179 result = (L < R);
1180 break;
1181 case CmpInst::ICMP_SLE:
1182 L.MakeSigned();
1183 R.MakeSigned();
1184 result = (L <= R);
1185 break;
1186 }
1187
1188 frame.AssignValue(inst, result, module);
1189
1190 if (log) {
1191 LLDB_LOGF(log, "Interpreted an ICmpInst");
1192 LLDB_LOGF(log, " L : %s", frame.SummarizeValue(lhs).c_str());
1193 LLDB_LOGF(log, " R : %s", frame.SummarizeValue(rhs).c_str());
1194 LLDB_LOGF(log, " = : %s", frame.SummarizeValue(inst).c_str());
1195 }
1196 } break;
1197 case Instruction::IntToPtr: {
1198 const IntToPtrInst *int_to_ptr_inst = cast<IntToPtrInst>(inst);
1199
1200 Value *src_operand = int_to_ptr_inst->getOperand(0);
1201
1203
1204 if (!frame.EvaluateValue(I, src_operand, module)) {
1205 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(src_operand).c_str());
1207 return false;
1208 }
1209
1210 frame.AssignValue(inst, I, module);
1211
1212 if (log) {
1213 LLDB_LOGF(log, "Interpreted an IntToPtr");
1214 LLDB_LOGF(log, " Src : %s", frame.SummarizeValue(src_operand).c_str());
1215 LLDB_LOGF(log, " = : %s", frame.SummarizeValue(inst).c_str());
1216 }
1217 } break;
1218 case Instruction::PtrToInt: {
1219 const PtrToIntInst *ptr_to_int_inst = cast<PtrToIntInst>(inst);
1220
1221 Value *src_operand = ptr_to_int_inst->getOperand(0);
1222
1224
1225 if (!frame.EvaluateValue(I, src_operand, module)) {
1226 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(src_operand).c_str());
1228 return false;
1229 }
1230
1231 frame.AssignValue(inst, I, module);
1232
1233 if (log) {
1234 LLDB_LOGF(log, "Interpreted a PtrToInt");
1235 LLDB_LOGF(log, " Src : %s", frame.SummarizeValue(src_operand).c_str());
1236 LLDB_LOGF(log, " = : %s", frame.SummarizeValue(inst).c_str());
1237 }
1238 } break;
1239 case Instruction::Trunc: {
1240 const TruncInst *trunc_inst = cast<TruncInst>(inst);
1241
1242 Value *src_operand = trunc_inst->getOperand(0);
1243
1245
1246 if (!frame.EvaluateValue(I, src_operand, module)) {
1247 LLDB_LOGF(log, "Couldn't evaluate %s", PrintValue(src_operand).c_str());
1249 return false;
1250 }
1251
1252 frame.AssignValue(inst, I, module);
1253
1254 if (log) {
1255 LLDB_LOGF(log, "Interpreted a Trunc");
1256 LLDB_LOGF(log, " Src : %s", frame.SummarizeValue(src_operand).c_str());
1257 LLDB_LOGF(log, " = : %s", frame.SummarizeValue(inst).c_str());
1258 }
1259 } break;
1260 case Instruction::Load: {
1261 const LoadInst *load_inst = cast<LoadInst>(inst);
1262
1263 // The semantics of Load are:
1264 // Create a region D that will contain the loaded data
1265 // Resolve the region P containing a pointer
1266 // Dereference P to get the region R that the data should be loaded from
1267 // Transfer a unit of type type(D) from R to D
1268
1269 const Value *pointer_operand = load_inst->getPointerOperand();
1270
1271 lldb::addr_t D = frame.ResolveValue(load_inst, module);
1272 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
1273
1274 if (D == LLDB_INVALID_ADDRESS) {
1275 LLDB_LOGF(log, "LoadInst's value doesn't resolve to anything");
1277 return false;
1278 }
1279
1280 if (P == LLDB_INVALID_ADDRESS) {
1281 LLDB_LOGF(log, "LoadInst's pointer doesn't resolve to anything");
1283 return false;
1284 }
1285
1286 lldb::addr_t R;
1287 lldb_private::Status read_error;
1288 execution_unit.ReadPointerFromMemory(&R, P, read_error);
1289
1290 if (!read_error.Success()) {
1291 LLDB_LOGF(log, "Couldn't read the address to be loaded for a LoadInst");
1293 return false;
1294 }
1295
1296 Type *target_ty = load_inst->getType();
1297 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1298 lldb_private::DataBufferHeap buffer(target_size, 0);
1299
1300 read_error.Clear();
1301 execution_unit.ReadMemory(buffer.GetBytes(), R, buffer.GetByteSize(),
1302 read_error);
1303 if (!read_error.Success()) {
1304 LLDB_LOGF(log, "Couldn't read from a region on behalf of a LoadInst");
1306 return false;
1307 }
1308
1309 lldb_private::Status write_error;
1310 execution_unit.WriteMemory(D, buffer.GetBytes(), buffer.GetByteSize(),
1311 write_error);
1312 if (!write_error.Success()) {
1313 LLDB_LOGF(log, "Couldn't write to a region on behalf of a LoadInst");
1315 return false;
1316 }
1317
1318 if (log) {
1319 LLDB_LOGF(log, "Interpreted a LoadInst");
1320 LLDB_LOGF(log, " P : 0x%" PRIx64, P);
1321 LLDB_LOGF(log, " R : 0x%" PRIx64, R);
1322 LLDB_LOGF(log, " D : 0x%" PRIx64, D);
1323 }
1324 } break;
1325 case Instruction::Ret: {
1326 return true;
1327 }
1328 case Instruction::Store: {
1329 const StoreInst *store_inst = cast<StoreInst>(inst);
1330
1331 // The semantics of Store are:
1332 // Resolve the region D containing the data to be stored
1333 // Resolve the region P containing a pointer
1334 // Dereference P to get the region R that the data should be stored in
1335 // Transfer a unit of type type(D) from D to R
1336
1337 const Value *value_operand = store_inst->getValueOperand();
1338 const Value *pointer_operand = store_inst->getPointerOperand();
1339
1340 lldb::addr_t D = frame.ResolveValue(value_operand, module);
1341 lldb::addr_t P = frame.ResolveValue(pointer_operand, module);
1342
1343 if (D == LLDB_INVALID_ADDRESS) {
1344 LLDB_LOGF(log, "StoreInst's value doesn't resolve to anything");
1346 return false;
1347 }
1348
1349 if (P == LLDB_INVALID_ADDRESS) {
1350 LLDB_LOGF(log, "StoreInst's pointer doesn't resolve to anything");
1352 return false;
1353 }
1354
1355 lldb::addr_t R;
1356 lldb_private::Status read_error;
1357 execution_unit.ReadPointerFromMemory(&R, P, read_error);
1358
1359 if (!read_error.Success()) {
1360 LLDB_LOGF(log, "Couldn't read the address to be loaded for a LoadInst");
1362 return false;
1363 }
1364
1365 Type *target_ty = value_operand->getType();
1366 size_t target_size = data_layout.getTypeStoreSize(target_ty);
1367 lldb_private::DataBufferHeap buffer(target_size, 0);
1368
1369 read_error.Clear();
1370 execution_unit.ReadMemory(buffer.GetBytes(), D, buffer.GetByteSize(),
1371 read_error);
1372 if (!read_error.Success()) {
1373 LLDB_LOGF(log, "Couldn't read from a region on behalf of a StoreInst");
1375 return false;
1376 }
1377
1378 lldb_private::Status write_error;
1379 execution_unit.WriteMemory(R, buffer.GetBytes(), buffer.GetByteSize(),
1380 write_error);
1381 if (!write_error.Success()) {
1382 LLDB_LOGF(log, "Couldn't write to a region on behalf of a StoreInst");
1384 return false;
1385 }
1386
1387 if (log) {
1388 LLDB_LOGF(log, "Interpreted a StoreInst");
1389 LLDB_LOGF(log, " D : 0x%" PRIx64, D);
1390 LLDB_LOGF(log, " P : 0x%" PRIx64, P);
1391 LLDB_LOGF(log, " R : 0x%" PRIx64, R);
1392 }
1393 } break;
1394 case Instruction::Call: {
1395 const CallInst *call_inst = cast<CallInst>(inst);
1396
1397 if (CanIgnoreCall(call_inst))
1398 break;
1399
1400 // Get the return type
1401 llvm::Type *returnType = call_inst->getType();
1402 if (returnType == nullptr) {
1404 "unable to access return type");
1405 return false;
1406 }
1407
1408 // Work with void, integer and pointer return types
1409 if (!returnType->isVoidTy() && !returnType->isIntegerTy() &&
1410 !returnType->isPointerTy()) {
1412 "return type is not supported");
1413 return false;
1414 }
1415
1416 // Check we can actually get a thread
1417 if (exe_ctx.GetThreadPtr() == nullptr) {
1418 error =
1419 lldb_private::Status::FromErrorString("unable to acquire thread");
1420 return false;
1421 }
1422
1423 // Make sure we have a valid process
1424 if (!process) {
1425 error =
1426 lldb_private::Status::FromErrorString("unable to get the process");
1427 return false;
1428 }
1429
1430 // Find the address of the callee function
1432 const llvm::Value *val = call_inst->getCalledOperand();
1433
1434 if (!frame.EvaluateValue(I, val, module)) {
1436 "unable to get address of function");
1437 return false;
1438 }
1440
1443
1444 llvm::FunctionType *prototype = call_inst->getFunctionType();
1445
1446 // Find number of arguments
1447 const int numArgs = call_inst->arg_size();
1448
1449 // We work with a fixed array of 16 arguments which is our upper limit
1450 static lldb_private::ABI::CallArgument rawArgs[16];
1451 if (numArgs >= 16) {
1453 "function takes too many arguments");
1454 return false;
1455 }
1456
1457 // Push all function arguments to the argument list that will be passed
1458 // to the call function thread plan
1459 for (int i = 0; i < numArgs; i++) {
1460 // Get details of this argument
1461 llvm::Value *arg_op = call_inst->getArgOperand(i);
1462 llvm::Type *arg_ty = arg_op->getType();
1463
1464 // Ensure that this argument is an supported type
1465 if (!arg_ty->isIntegerTy() && !arg_ty->isPointerTy()) {
1467 "argument %d must be integer type", i);
1468 return false;
1469 }
1470
1471 // Extract the arguments value
1472 lldb_private::Scalar tmp_op = 0;
1473 if (!frame.EvaluateValue(tmp_op, arg_op, module)) {
1475 "unable to evaluate argument %d", i);
1476 return false;
1477 }
1478
1479 // Check if this is a string literal or constant string pointer
1480 if (arg_ty->isPointerTy()) {
1481 lldb::addr_t addr = tmp_op.ULongLong();
1482 size_t dataSize = 0;
1483
1484 bool Success = execution_unit.GetAllocSize(addr, dataSize);
1486 assert(Success &&
1487 "unable to locate host data for transfer to device");
1488 // Create the required buffer
1489 rawArgs[i].size = dataSize;
1490 rawArgs[i].data_up.reset(new uint8_t[dataSize + 1]);
1491
1492 // Read string from host memory
1493 execution_unit.ReadMemory(rawArgs[i].data_up.get(), addr, dataSize,
1494 error);
1495 assert(!error.Fail() &&
1496 "we have failed to read the string from memory");
1497
1498 // Add null terminator
1499 rawArgs[i].data_up[dataSize] = '\0';
1501 } else /* if ( arg_ty->isPointerTy() ) */
1502 {
1504 // Get argument size in bytes
1505 rawArgs[i].size = arg_ty->getIntegerBitWidth() / 8;
1506 // Push value into argument list for thread plan
1507 rawArgs[i].value = tmp_op.ULongLong();
1508 }
1509 }
1510
1511 // Pack the arguments into an llvm::array
1512 llvm::ArrayRef<lldb_private::ABI::CallArgument> args(rawArgs, numArgs);
1513
1514 // Setup a thread plan to call the target function
1515 lldb::ThreadPlanSP call_plan_sp(
1517 exe_ctx.GetThreadRef(), funcAddr, *prototype, *returnType, args,
1518 options));
1519
1520 // Check if the plan is valid
1522 if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
1524 "unable to make ThreadPlanCallFunctionUsingABI for 0x%llx",
1525 I.ULongLong());
1526 return false;
1527 }
1528
1529 process->SetRunningUserExpression(true);
1530
1531 // Execute the actual function call thread plan
1533 process->RunThreadPlan(exe_ctx, call_plan_sp, options, diagnostics);
1534
1535 // Check that the thread plan completed successfully
1538 "ThreadPlanCallFunctionUsingABI failed");
1539 return false;
1540 }
1541
1542 process->SetRunningUserExpression(false);
1543
1544 // Void return type
1545 if (returnType->isVoidTy()) {
1546 // Cant assign to void types, so we leave the frame untouched
1547 } else
1548 // Integer or pointer return type
1549 if (returnType->isIntegerTy() || returnType->isPointerTy()) {
1550 // Get the encapsulated return value
1551 lldb::ValueObjectSP retVal = call_plan_sp.get()->GetReturnValueObject();
1552
1553 lldb_private::Scalar returnVal = -1;
1554 lldb_private::ValueObject *vobj = retVal.get();
1555
1556 // Check if the return value is valid
1557 if (vobj == nullptr || !retVal) {
1559 "unable to get the return value");
1560 return false;
1561 }
1562
1563 // Extract the return value as a integer
1564 lldb_private::Value &value = vobj->GetValue();
1565 returnVal = value.GetScalar();
1566
1567 // Push the return value as the result
1568 frame.AssignValue(inst, returnVal, module);
1569 }
1570 } break;
1571 }
1572
1573 ++frame.m_ii;
1574 }
1575
1576 return false;
1577}
static llvm::raw_ostream & error(Stream &strm)
#define INTERRUPT_REQUESTED(debugger,...)
This handy define will keep you from having to generate a report for the interruption by hand.
Definition Debugger.h:476
static bool CanResolveConstant(llvm::Constant *constant)
static const char * memory_allocation_error
static const char * memory_read_error
static std::string PrintValue(const Value *value, bool truncate=false)
static const char * interpreter_internal_error
static std::string PrintType(const Type *type, bool truncate=false)
static const char * interrupt_error
static const char * unsupported_operand_error
static const char * timeout_error
static bool CanIgnoreCall(const CallInst *call)
static const char * memory_write_error
static const char * unsupported_opcode_error
static const char * bad_value_error
static const char * too_many_functions_error
#define LLDB_LOGF(log,...)
Definition Log.h:376
static bool CanInterpret(llvm::Module &module, llvm::Function &function, lldb_private::Status &error, const bool support_function_calls)
static bool Interpret(llvm::Module &module, llvm::Function &function, llvm::ArrayRef< lldb::addr_t > args, lldb_private::IRExecutionUnit &execution_unit, lldb_private::Status &error, lldb::addr_t stack_frame_bottom, lldb::addr_t stack_frame_top, lldb_private::ExecutionContext &exe_ctx, lldb_private::Timeout< std::micro > timeout)
lldb::addr_t ResolveValue(const Value *value, Module &module)
const BasicBlock * m_bb
std::string SummarizeValue(const Value *value)
bool ResolveConstantValue(APInt &value, const Constant *constant)
bool ResolveConstant(lldb::addr_t process_address, const Constant *constant)
const DataLayout & m_target_data
lldb_private::IRExecutionUnit & m_execution_unit
bool MakeArgument(const Argument *value, uint64_t address)
lldb::addr_t Malloc(size_t size, uint8_t byte_alignment)
const BasicBlock * m_prev_bb
BasicBlock::const_iterator m_ie
lldb::addr_t Malloc(llvm::Type *type)
bool AssignToMatchType(lldb_private::Scalar &scalar, llvm::APInt value, Type *type)
~InterpreterStackFrame()=default
InterpreterStackFrame(const DataLayout &target_data, lldb_private::IRExecutionUnit &execution_unit, lldb::addr_t stack_frame_bottom, lldb::addr_t stack_frame_top)
void Jump(const BasicBlock *bb)
std::string PrintData(lldb::addr_t addr, llvm::Type *type)
std::map< const Value *, lldb::addr_t > ValueMap
BasicBlock::const_iterator m_ii
lldb::ByteOrder m_byte_order
bool EvaluateValue(lldb_private::Scalar &scalar, const Value *value, Module &module)
bool AssignValue(const Value *value, lldb_private::Scalar scalar, Module &module)
lldb::addr_t m_frame_process_address
A section + offset based address class.
Definition Address.h:62
A uniqued constant string class.
Definition ConstString.h:40
A subclass of DataBuffer that stores a data buffer on the heap.
lldb::offset_t GetByteSize() const override
Get the number of bytes in the data buffer.
An data extractor class.
float GetFloat(lldb::offset_t *offset_ptr) const
Extract a float from *offset_ptr.
uint64_t GetMaxU64(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an unsigned integer of size byte_size from *offset_ptr.
double GetDouble(lldb::offset_t *offset_ptr) const
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
Target * GetTargetPtr() const
Returns a pointer to the target object.
Process * GetProcessPtr() const
Returns a pointer to the process object.
Thread & GetThreadRef() const
Returns a reference to the thread object.
Thread * GetThreadPtr() const
Returns a pointer to the thread object.
"lldb/Expression/IRExecutionUnit.h" Contains the IR and, optionally, JIT- compiled code for a module.
void Free(lldb::addr_t process_address, Status &error)
void ReadPointerFromMemory(lldb::addr_t *address, lldb::addr_t process_address, Status &error)
void WritePointerToMemory(lldb::addr_t process_address, lldb::addr_t pointer, Status &error)
bool GetAllocSize(lldb::addr_t address, size_t &size)
void WriteMemory(lldb::addr_t process_address, const uint8_t *bytes, size_t size, Status &error)
void ReadMemory(uint8_t *bytes, lldb::addr_t process_address, size_t size, Status &error)
A plug-in interface definition class for debugging a process.
Definition Process.h:354
lldb::ExpressionResults RunThreadPlan(ExecutionContext &exe_ctx, lldb::ThreadPlanSP &thread_plan_sp, const EvaluateExpressionOptions &options, DiagnosticManager &diagnostic_manager)
Definition Process.cpp:5071
void SetRunningUserExpression(bool on)
Definition Process.cpp:1471
bool IsZero() const
Definition Scalar.cpp:174
unsigned long long ULongLong(unsigned long long fail_value=0) const
Definition Scalar.cpp:365
size_t GetAsMemoryData(void *dst, size_t dst_len, lldb::ByteOrder dst_byte_order, Status &error) const
Definition Scalar.cpp:788
bool ShiftRightLogical(const Scalar &rhs)
Definition Scalar.cpp:464
llvm::APInt UInt128(const llvm::APInt &fail_value) const
Definition Scalar.cpp:381
An error handling class.
Definition Status.h:118
void Clear()
Clear the object state.
Definition Status.cpp:215
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
static Status FromErrorString(const char *str)
Definition Status.h:141
bool Success() const
Test for success condition.
Definition Status.cpp:304
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
Debugger & GetDebugger() const
Definition Target.h:1194
const Value & GetValue() const
const Scalar & GetScalar() const
See comment on m_scalar to understand what GetScalar returns.
Definition Value.h:113
uint8_t * GetBytes()
Get a pointer to the data.
Definition DataBuffer.h:108
#define UNUSED_IF_ASSERT_DISABLED(x)
#define LLDB_INVALID_ADDRESS
std::shared_ptr< lldb_private::ThreadPlan > ThreadPlanSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
uint64_t offset_t
Definition lldb-types.h:85
ExpressionResults
The results of expression evaluation.
@ eExpressionCompleted
ByteOrder
Byte ordering definitions.
uint64_t addr_t
Definition lldb-types.h:80
std::unique_ptr< uint8_t[]> data_up
Definition ABI.h:39