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