1 //===-- IRInterpreter.cpp -------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Expression/IRInterpreter.h"
10 #include "lldb/Core/Debugger.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/ModuleSpec.h"
13 #include "lldb/Core/ValueObject.h"
14 #include "lldb/Expression/DiagnosticManager.h"
15 #include "lldb/Expression/IRExecutionUnit.h"
16 #include "lldb/Expression/IRMemoryMap.h"
17 #include "lldb/Utility/ConstString.h"
18 #include "lldb/Utility/DataExtractor.h"
19 #include "lldb/Utility/Endian.h"
20 #include "lldb/Utility/LLDBLog.h"
21 #include "lldb/Utility/Log.h"
22 #include "lldb/Utility/Scalar.h"
23 #include "lldb/Utility/Status.h"
24 #include "lldb/Utility/StreamString.h"
26 #include "lldb/Target/ABI.h"
27 #include "lldb/Target/ExecutionContext.h"
28 #include "lldb/Target/Target.h"
29 #include "lldb/Target/Thread.h"
30 #include "lldb/Target/ThreadPlan.h"
31 #include "lldb/Target/ThreadPlanCallFunctionUsingABI.h"
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"
46 using lldb_private::LLDBLog
;
48 static std::string
PrintValue(const Value
*value
, bool truncate
= false) {
50 raw_string_ostream
rso(s
);
54 s
.resize(s
.length() - 1);
57 while ((offset
= s
.find('\n')) != s
.npos
)
59 while (s
[0] == ' ' || s
[0] == '\t')
65 static std::string
PrintType(const Type
*type
, bool truncate
= false) {
67 raw_string_ostream
rso(s
);
71 s
.resize(s
.length() - 1);
75 static bool CanIgnoreCall(const CallInst
*call
) {
76 const llvm::Function
*called_function
= call
->getCalledFunction();
81 if (called_function
->isIntrinsic()) {
82 switch (called_function
->getIntrinsicID()) {
85 case llvm::Intrinsic::dbg_declare
:
86 case llvm::Intrinsic::dbg_value
:
94 class InterpreterStackFrame
{
96 typedef std::map
<const Value
*, lldb::addr_t
> ValueMap
;
99 DataLayout
&m_target_data
;
100 lldb_private::IRExecutionUnit
&m_execution_unit
;
101 const BasicBlock
*m_bb
= nullptr;
102 const BasicBlock
*m_prev_bb
= nullptr;
103 BasicBlock::const_iterator m_ii
;
104 BasicBlock::const_iterator m_ie
;
106 lldb::addr_t m_frame_process_address
;
108 lldb::addr_t m_stack_pointer
;
110 lldb::ByteOrder m_byte_order
;
111 size_t m_addr_byte_size
;
113 InterpreterStackFrame(DataLayout
&target_data
,
114 lldb_private::IRExecutionUnit
&execution_unit
,
115 lldb::addr_t stack_frame_bottom
,
116 lldb::addr_t stack_frame_top
)
117 : m_target_data(target_data
), m_execution_unit(execution_unit
) {
118 m_byte_order
= (target_data
.isLittleEndian() ? lldb::eByteOrderLittle
119 : lldb::eByteOrderBig
);
120 m_addr_byte_size
= (target_data
.getPointerSize(0));
122 m_frame_process_address
= stack_frame_bottom
;
123 m_frame_size
= stack_frame_top
- stack_frame_bottom
;
124 m_stack_pointer
= stack_frame_top
;
127 ~InterpreterStackFrame() = default;
129 void Jump(const BasicBlock
*bb
) {
132 m_ii
= m_bb
->begin();
136 std::string
SummarizeValue(const Value
*value
) {
137 lldb_private::StreamString ss
;
139 ss
.Printf("%s", PrintValue(value
).c_str());
141 ValueMap::iterator i
= m_values
.find(value
);
143 if (i
!= m_values
.end()) {
144 lldb::addr_t addr
= i
->second
;
146 ss
.Printf(" 0x%llx", (unsigned long long)addr
);
149 return std::string(ss
.GetString());
152 bool AssignToMatchType(lldb_private::Scalar
&scalar
, llvm::APInt value
,
154 size_t type_size
= m_target_data
.getTypeStoreSize(type
);
160 type_size
= PowerOf2Ceil(type_size
);
162 scalar
= value
.zextOrTrunc(type_size
* 8);
166 bool EvaluateValue(lldb_private::Scalar
&scalar
, const Value
*value
,
168 const Constant
*constant
= dyn_cast
<Constant
>(value
);
171 if (constant
->getValueID() == Value::ConstantFPVal
) {
172 if (auto *cfp
= dyn_cast
<ConstantFP
>(constant
)) {
173 if (cfp
->getType()->isDoubleTy())
174 scalar
= cfp
->getValueAPF().convertToDouble();
175 else if (cfp
->getType()->isFloatTy())
176 scalar
= cfp
->getValueAPF().convertToFloat();
185 if (!ResolveConstantValue(value_apint
, constant
))
188 return AssignToMatchType(scalar
, value_apint
, value
->getType());
191 lldb::addr_t process_address
= ResolveValue(value
, module
);
192 size_t value_size
= m_target_data
.getTypeStoreSize(value
->getType());
194 lldb_private::DataExtractor value_extractor
;
195 lldb_private::Status extract_error
;
197 m_execution_unit
.GetMemoryData(value_extractor
, process_address
,
198 value_size
, extract_error
);
200 if (!extract_error
.Success())
203 lldb::offset_t offset
= 0;
204 if (value_size
<= 8) {
205 Type
*ty
= value
->getType();
206 if (ty
->isDoubleTy()) {
207 scalar
= value_extractor
.GetDouble(&offset
);
209 } else if (ty
->isFloatTy()) {
210 scalar
= value_extractor
.GetFloat(&offset
);
213 uint64_t u64value
= value_extractor
.GetMaxU64(&offset
, value_size
);
214 return AssignToMatchType(scalar
, llvm::APInt(64, u64value
),
222 bool AssignValue(const Value
*value
, lldb_private::Scalar scalar
,
224 lldb::addr_t process_address
= ResolveValue(value
, module
);
226 if (process_address
== LLDB_INVALID_ADDRESS
)
229 lldb_private::Scalar cast_scalar
;
230 Type
*vty
= value
->getType();
231 if (vty
->isFloatTy() || vty
->isDoubleTy()) {
232 cast_scalar
= scalar
;
234 scalar
.MakeUnsigned();
235 if (!AssignToMatchType(cast_scalar
, scalar
.UInt128(llvm::APInt()),
240 size_t value_byte_size
= m_target_data
.getTypeStoreSize(value
->getType());
242 lldb_private::DataBufferHeap
buf(value_byte_size
, 0);
244 lldb_private::Status get_data_error
;
246 if (!cast_scalar
.GetAsMemoryData(buf
.GetBytes(), buf
.GetByteSize(),
247 m_byte_order
, get_data_error
))
250 lldb_private::Status write_error
;
252 m_execution_unit
.WriteMemory(process_address
, buf
.GetBytes(),
253 buf
.GetByteSize(), write_error
);
255 return write_error
.Success();
258 bool ResolveConstantValue(APInt
&value
, const Constant
*constant
) {
259 switch (constant
->getValueID()) {
262 case Value::FunctionVal
:
263 if (const Function
*constant_func
= dyn_cast
<Function
>(constant
)) {
264 lldb_private::ConstString
name(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
|| missing_weak
)
269 value
= APInt(m_target_data
.getPointerSizeInBits(), addr
);
273 case Value::ConstantIntVal
:
274 if (const ConstantInt
*constant_int
= dyn_cast
<ConstantInt
>(constant
)) {
275 value
= constant_int
->getValue();
279 case Value::ConstantFPVal
:
280 if (const ConstantFP
*constant_fp
= dyn_cast
<ConstantFP
>(constant
)) {
281 value
= constant_fp
->getValueAPF().bitcastToAPInt();
285 case Value::ConstantExprVal
:
286 if (const ConstantExpr
*constant_expr
=
287 dyn_cast
<ConstantExpr
>(constant
)) {
288 switch (constant_expr
->getOpcode()) {
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();
299 Constant
*base
= dyn_cast
<Constant
>(*op_cursor
);
304 if (!ResolveConstantValue(value
, base
))
309 if (op_cursor
== op_end
)
310 return true; // no offset to apply!
312 SmallVector
<Value
*, 8> indices(op_cursor
, op_end
);
314 cast
<GEPOperator
>(constant_expr
)->getSourceElementType();
316 // DataLayout::getIndexedOffsetInType assumes the indices are
317 // instances of ConstantInt.
319 m_target_data
.getIndexedOffsetInType(src_elem_ty
, indices
);
321 const bool is_signed
= true;
322 value
+= APInt(value
.getBitWidth(), offset
, is_signed
);
329 case Value::ConstantPointerNullVal
:
330 if (isa
<ConstantPointerNull
>(constant
)) {
331 value
= APInt(m_target_data
.getPointerSizeInBits(), 0);
339 bool MakeArgument(const Argument
*value
, uint64_t address
) {
340 lldb::addr_t data_address
= Malloc(value
->getType());
342 if (data_address
== LLDB_INVALID_ADDRESS
)
345 lldb_private::Status write_error
;
347 m_execution_unit
.WritePointerToMemory(data_address
, address
, write_error
);
349 if (!write_error
.Success()) {
350 lldb_private::Status free_error
;
351 m_execution_unit
.Free(data_address
, free_error
);
355 m_values
[value
] = data_address
;
357 lldb_private::Log
*log(GetLog(LLDBLog::Expressions
));
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
);
370 bool ResolveConstant(lldb::addr_t process_address
, const Constant
*constant
) {
371 APInt resolved_value
;
373 if (!ResolveConstantValue(resolved_value
, constant
))
376 size_t constant_size
= m_target_data
.getTypeStoreSize(constant
->getType());
377 lldb_private::DataBufferHeap
buf(constant_size
, 0);
379 lldb_private::Status get_data_error
;
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
))
387 lldb_private::Status write_error
;
389 m_execution_unit
.WriteMemory(process_address
, buf
.GetBytes(),
390 buf
.GetByteSize(), write_error
);
392 return write_error
.Success();
395 lldb::addr_t
Malloc(size_t size
, uint8_t byte_alignment
) {
396 lldb::addr_t ret
= m_stack_pointer
;
399 ret
-= (ret
% byte_alignment
);
401 if (ret
< m_frame_process_address
)
402 return LLDB_INVALID_ADDRESS
;
404 m_stack_pointer
= ret
;
408 lldb::addr_t
Malloc(llvm::Type
*type
) {
409 lldb_private::Status alloc_error
;
411 return Malloc(m_target_data
.getTypeAllocSize(type
),
412 m_target_data
.getPrefTypeAlign(type
).value());
415 std::string
PrintData(lldb::addr_t addr
, llvm::Type
*type
) {
416 size_t length
= m_target_data
.getTypeStoreSize(type
);
418 lldb_private::DataBufferHeap
buf(length
, 0);
420 lldb_private::Status read_error
;
422 m_execution_unit
.ReadMemory(buf
.GetBytes(), addr
, length
, read_error
);
424 if (!read_error
.Success())
425 return std::string("<couldn't read data>");
427 lldb_private::StreamString ss
;
429 for (size_t i
= 0; i
< length
; i
++) {
430 if ((!(i
& 0xf)) && i
)
431 ss
.Printf("%02hhx - ", buf
.GetBytes()[i
]);
433 ss
.Printf("%02hhx ", buf
.GetBytes()[i
]);
436 return std::string(ss
.GetString());
439 lldb::addr_t
ResolveValue(const Value
*value
, Module
&module
) {
440 ValueMap::iterator i
= m_values
.find(value
);
442 if (i
!= m_values
.end())
445 // Fall back and allocate space [allocation type Alloca]
447 lldb::addr_t data_address
= Malloc(value
->getType());
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
);
453 return LLDB_INVALID_ADDRESS
;
457 m_values
[value
] = data_address
;
462 static const char *unsupported_opcode_error
=
463 "Interpreter doesn't handle one of the expression's opcodes";
464 static const char *unsupported_operand_error
=
465 "Interpreter doesn't handle one of the expression's operands";
466 static const char *interpreter_internal_error
=
467 "Interpreter encountered an internal error";
468 static const char *interrupt_error
=
469 "Interrupted while interpreting expression";
470 static const char *bad_value_error
=
471 "Interpreter couldn't resolve a value during execution";
472 static const char *memory_allocation_error
=
473 "Interpreter couldn't allocate memory";
474 static const char *memory_write_error
= "Interpreter couldn't write to memory";
475 static const char *memory_read_error
= "Interpreter couldn't read from memory";
476 static const char *timeout_error
=
477 "Reached timeout while interpreting expression";
478 static const char *too_many_functions_error
=
479 "Interpreter doesn't handle modules with multiple function bodies.";
481 static bool CanResolveConstant(llvm::Constant
*constant
) {
482 switch (constant
->getValueID()) {
485 case Value::ConstantIntVal
:
486 case Value::ConstantFPVal
:
487 case Value::FunctionVal
:
489 case Value::ConstantExprVal
:
490 if (const ConstantExpr
*constant_expr
= dyn_cast
<ConstantExpr
>(constant
)) {
491 switch (constant_expr
->getOpcode()) {
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
))
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
);
518 case Value::ConstantPointerNullVal
:
523 bool IRInterpreter::CanInterpret(llvm::Module
&module
, llvm::Function
&function
,
524 lldb_private::Status
&error
,
525 const bool support_function_calls
) {
526 lldb_private::Log
*log(GetLog(LLDBLog::Expressions
));
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");
533 error
.SetErrorToGenericError();
534 error
.SetErrorString(too_many_functions_error
);
537 saw_function_with_body
= true;
538 LLDB_LOGF(log
, "Saw function with body: %s", f
.getName().str().c_str());
542 for (BasicBlock
&bb
: function
) {
543 for (Instruction
&ii
: bb
) {
544 switch (ii
.getOpcode()) {
546 LLDB_LOGF(log
, "Unsupported instruction: %s", PrintValue(&ii
).c_str());
547 error
.SetErrorToGenericError();
548 error
.SetErrorString(unsupported_opcode_error
);
551 case Instruction::Add
:
552 case Instruction::Alloca
:
553 case Instruction::BitCast
:
554 case Instruction::Br
:
555 case Instruction::PHI
:
557 case Instruction::Call
: {
558 CallInst
*call_inst
= dyn_cast
<CallInst
>(&ii
);
561 error
.SetErrorToGenericError();
562 error
.SetErrorString(interpreter_internal_error
);
566 if (!CanIgnoreCall(call_inst
) && !support_function_calls
) {
567 LLDB_LOGF(log
, "Unsupported instruction: %s",
568 PrintValue(&ii
).c_str());
569 error
.SetErrorToGenericError();
570 error
.SetErrorString(unsupported_opcode_error
);
574 case Instruction::GetElementPtr
:
576 case Instruction::FCmp
:
577 case Instruction::ICmp
: {
578 CmpInst
*cmp_inst
= dyn_cast
<CmpInst
>(&ii
);
581 error
.SetErrorToGenericError();
582 error
.SetErrorString(interpreter_internal_error
);
586 switch (cmp_inst
->getPredicate()) {
588 LLDB_LOGF(log
, "Unsupported ICmp predicate: %s",
589 PrintValue(&ii
).c_str());
591 error
.SetErrorToGenericError();
592 error
.SetErrorString(unsupported_opcode_error
);
595 case CmpInst::FCMP_OEQ
:
596 case CmpInst::ICMP_EQ
:
597 case CmpInst::FCMP_UNE
:
598 case CmpInst::ICMP_NE
:
599 case CmpInst::FCMP_OGT
:
600 case CmpInst::ICMP_UGT
:
601 case CmpInst::FCMP_OGE
:
602 case CmpInst::ICMP_UGE
:
603 case CmpInst::FCMP_OLT
:
604 case CmpInst::ICMP_ULT
:
605 case CmpInst::FCMP_OLE
:
606 case CmpInst::ICMP_ULE
:
607 case CmpInst::ICMP_SGT
:
608 case CmpInst::ICMP_SGE
:
609 case CmpInst::ICMP_SLT
:
610 case CmpInst::ICMP_SLE
:
614 case Instruction::And
:
615 case Instruction::AShr
:
616 case Instruction::IntToPtr
:
617 case Instruction::PtrToInt
:
618 case Instruction::Load
:
619 case Instruction::LShr
:
620 case Instruction::Mul
:
621 case Instruction::Or
:
622 case Instruction::Ret
:
623 case Instruction::SDiv
:
624 case Instruction::SExt
:
625 case Instruction::Shl
:
626 case Instruction::SRem
:
627 case Instruction::Store
:
628 case Instruction::Sub
:
629 case Instruction::Trunc
:
630 case Instruction::UDiv
:
631 case Instruction::URem
:
632 case Instruction::Xor
:
633 case Instruction::ZExt
:
635 case Instruction::FAdd
:
636 case Instruction::FSub
:
637 case Instruction::FMul
:
638 case Instruction::FDiv
:
642 for (unsigned oi
= 0, oe
= ii
.getNumOperands(); oi
!= oe
; ++oi
) {
643 Value
*operand
= ii
.getOperand(oi
);
644 Type
*operand_type
= operand
->getType();
646 switch (operand_type
->getTypeID()) {
649 case Type::FixedVectorTyID
:
650 case Type::ScalableVectorTyID
: {
651 LLDB_LOGF(log
, "Unsupported operand type: %s",
652 PrintType(operand_type
).c_str());
653 error
.SetErrorString(unsupported_operand_error
);
658 // The IR interpreter currently doesn't know about
659 // 128-bit integers. As they're not that frequent,
660 // we can just fall back to the JIT rather than
662 if (operand_type
->getPrimitiveSizeInBits() > 64) {
663 LLDB_LOGF(log
, "Unsupported operand type: %s",
664 PrintType(operand_type
).c_str());
665 error
.SetErrorString(unsupported_operand_error
);
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());
673 error
.SetErrorString(unsupported_operand_error
);
684 bool IRInterpreter::Interpret(llvm::Module
&module
, llvm::Function
&function
,
685 llvm::ArrayRef
<lldb::addr_t
> args
,
686 lldb_private::IRExecutionUnit
&execution_unit
,
687 lldb_private::Status
&error
,
688 lldb::addr_t stack_frame_bottom
,
689 lldb::addr_t stack_frame_top
,
690 lldb_private::ExecutionContext
&exe_ctx
,
691 lldb_private::Timeout
<std::micro
> timeout
) {
692 lldb_private::Log
*log(GetLog(LLDBLog::Expressions
));
696 raw_string_ostream
oss(s
);
698 module
.print(oss
, nullptr);
702 LLDB_LOGF(log
, "Module as passed in to IRInterpreter::Interpret: \n\"%s\"",
706 DataLayout
data_layout(&module
);
708 InterpreterStackFrame
frame(data_layout
, execution_unit
, stack_frame_bottom
,
711 if (frame
.m_frame_process_address
== LLDB_INVALID_ADDRESS
) {
712 error
.SetErrorString("Couldn't allocate stack frame");
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
)) {
721 error
.SetErrorString("Not enough arguments passed in to function");
725 lldb::addr_t ptr
= args
[arg_index
];
727 frame
.MakeArgument(&*ai
, ptr
);
730 frame
.Jump(&function
.front());
732 lldb_private::Process
*process
= exe_ctx
.GetProcessPtr();
733 lldb_private::Target
*target
= exe_ctx
.GetTargetPtr();
735 using clock
= std::chrono::steady_clock
;
737 // Compute the time at which the timeout has been exceeded.
738 std::optional
<clock::time_point
> end_time
;
739 if (timeout
&& timeout
->count() > 0)
740 end_time
= clock::now() + *timeout
;
742 while (frame
.m_ii
!= frame
.m_ie
) {
743 // Timeout reached: stop interpreting.
744 if (end_time
&& clock::now() >= *end_time
) {
745 error
.SetErrorToGenericError();
746 error
.SetErrorString(timeout_error
);
750 // If we have access to the debugger we can honor an interrupt request.
752 if (INTERRUPT_REQUESTED(target
->GetDebugger(),
753 "Interrupted in IR interpreting.")) {
754 error
.SetErrorToGenericError();
755 error
.SetErrorString(interrupt_error
);
760 const Instruction
*inst
= &*frame
.m_ii
;
762 LLDB_LOGF(log
, "Interpreting %s", PrintValue(inst
).c_str());
764 switch (inst
->getOpcode()) {
768 case Instruction::Add
:
769 case Instruction::Sub
:
770 case Instruction::Mul
:
771 case Instruction::SDiv
:
772 case Instruction::UDiv
:
773 case Instruction::SRem
:
774 case Instruction::URem
:
775 case Instruction::Shl
:
776 case Instruction::LShr
:
777 case Instruction::AShr
:
778 case Instruction::And
:
779 case Instruction::Or
:
780 case Instruction::Xor
:
781 case Instruction::FAdd
:
782 case Instruction::FSub
:
783 case Instruction::FMul
:
784 case Instruction::FDiv
: {
785 const BinaryOperator
*bin_op
= dyn_cast
<BinaryOperator
>(inst
);
790 "getOpcode() returns %s, but instruction is not a BinaryOperator",
791 inst
->getOpcodeName());
792 error
.SetErrorToGenericError();
793 error
.SetErrorString(interpreter_internal_error
);
797 Value
*lhs
= inst
->getOperand(0);
798 Value
*rhs
= inst
->getOperand(1);
800 lldb_private::Scalar L
;
801 lldb_private::Scalar R
;
803 if (!frame
.EvaluateValue(L
, lhs
, module
)) {
804 LLDB_LOGF(log
, "Couldn't evaluate %s", PrintValue(lhs
).c_str());
805 error
.SetErrorToGenericError();
806 error
.SetErrorString(bad_value_error
);
810 if (!frame
.EvaluateValue(R
, rhs
, module
)) {
811 LLDB_LOGF(log
, "Couldn't evaluate %s", PrintValue(rhs
).c_str());
812 error
.SetErrorToGenericError();
813 error
.SetErrorString(bad_value_error
);
817 lldb_private::Scalar result
;
819 switch (inst
->getOpcode()) {
822 case Instruction::Add
:
823 case Instruction::FAdd
:
826 case Instruction::Mul
:
827 case Instruction::FMul
:
830 case Instruction::Sub
:
831 case Instruction::FSub
:
834 case Instruction::SDiv
:
839 case Instruction::UDiv
:
844 case Instruction::FDiv
:
847 case Instruction::SRem
:
852 case Instruction::URem
:
857 case Instruction::Shl
:
860 case Instruction::AShr
:
863 case Instruction::LShr
:
865 result
.ShiftRightLogical(R
);
867 case Instruction::And
:
870 case Instruction::Or
:
873 case Instruction::Xor
:
878 frame
.AssignValue(inst
, result
, module
);
881 LLDB_LOGF(log
, "Interpreted a %s", inst
->getOpcodeName());
882 LLDB_LOGF(log
, " L : %s", frame
.SummarizeValue(lhs
).c_str());
883 LLDB_LOGF(log
, " R : %s", frame
.SummarizeValue(rhs
).c_str());
884 LLDB_LOGF(log
, " = : %s", frame
.SummarizeValue(inst
).c_str());
887 case Instruction::Alloca
: {
888 const AllocaInst
*alloca_inst
= cast
<AllocaInst
>(inst
);
890 if (alloca_inst
->isArrayAllocation()) {
892 "AllocaInsts are not handled if isArrayAllocation() is true");
893 error
.SetErrorToGenericError();
894 error
.SetErrorString(unsupported_opcode_error
);
898 // The semantics of Alloca are:
899 // Create a region R of virtual memory of type T, backed by a data
901 // Create a region P of virtual memory of type T*, backed by a data
903 // Write the virtual address of R into P
905 Type
*T
= alloca_inst
->getAllocatedType();
906 Type
*Tptr
= alloca_inst
->getType();
908 lldb::addr_t R
= frame
.Malloc(T
);
910 if (R
== LLDB_INVALID_ADDRESS
) {
911 LLDB_LOGF(log
, "Couldn't allocate memory for an AllocaInst");
912 error
.SetErrorToGenericError();
913 error
.SetErrorString(memory_allocation_error
);
917 lldb::addr_t P
= frame
.Malloc(Tptr
);
919 if (P
== LLDB_INVALID_ADDRESS
) {
921 "Couldn't allocate the result pointer for an AllocaInst");
922 error
.SetErrorToGenericError();
923 error
.SetErrorString(memory_allocation_error
);
927 lldb_private::Status write_error
;
929 execution_unit
.WritePointerToMemory(P
, R
, write_error
);
931 if (!write_error
.Success()) {
932 LLDB_LOGF(log
, "Couldn't write the result pointer for an AllocaInst");
933 error
.SetErrorToGenericError();
934 error
.SetErrorString(memory_write_error
);
935 lldb_private::Status free_error
;
936 execution_unit
.Free(P
, free_error
);
937 execution_unit
.Free(R
, free_error
);
941 frame
.m_values
[alloca_inst
] = P
;
944 LLDB_LOGF(log
, "Interpreted an AllocaInst");
945 LLDB_LOGF(log
, " R : 0x%" PRIx64
, R
);
946 LLDB_LOGF(log
, " P : 0x%" PRIx64
, P
);
949 case Instruction::BitCast
:
950 case Instruction::ZExt
: {
951 const CastInst
*cast_inst
= cast
<CastInst
>(inst
);
953 Value
*source
= cast_inst
->getOperand(0);
955 lldb_private::Scalar S
;
957 if (!frame
.EvaluateValue(S
, source
, module
)) {
958 LLDB_LOGF(log
, "Couldn't evaluate %s", PrintValue(source
).c_str());
959 error
.SetErrorToGenericError();
960 error
.SetErrorString(bad_value_error
);
964 frame
.AssignValue(inst
, S
, module
);
966 case Instruction::SExt
: {
967 const CastInst
*cast_inst
= cast
<CastInst
>(inst
);
969 Value
*source
= cast_inst
->getOperand(0);
971 lldb_private::Scalar S
;
973 if (!frame
.EvaluateValue(S
, source
, module
)) {
974 LLDB_LOGF(log
, "Couldn't evaluate %s", PrintValue(source
).c_str());
975 error
.SetErrorToGenericError();
976 error
.SetErrorString(bad_value_error
);
982 lldb_private::Scalar
S_signextend(S
.SLongLong());
984 frame
.AssignValue(inst
, S_signextend
, module
);
986 case Instruction::Br
: {
987 const BranchInst
*br_inst
= cast
<BranchInst
>(inst
);
989 if (br_inst
->isConditional()) {
990 Value
*condition
= br_inst
->getCondition();
992 lldb_private::Scalar C
;
994 if (!frame
.EvaluateValue(C
, condition
, module
)) {
995 LLDB_LOGF(log
, "Couldn't evaluate %s", PrintValue(condition
).c_str());
996 error
.SetErrorToGenericError();
997 error
.SetErrorString(bad_value_error
);
1002 frame
.Jump(br_inst
->getSuccessor(0));
1004 frame
.Jump(br_inst
->getSuccessor(1));
1007 LLDB_LOGF(log
, "Interpreted a BrInst with a condition");
1008 LLDB_LOGF(log
, " cond : %s",
1009 frame
.SummarizeValue(condition
).c_str());
1012 frame
.Jump(br_inst
->getSuccessor(0));
1015 LLDB_LOGF(log
, "Interpreted a BrInst with no condition");
1020 case Instruction::PHI
: {
1021 const PHINode
*phi_inst
= cast
<PHINode
>(inst
);
1022 if (!frame
.m_prev_bb
) {
1024 "Encountered PHI node without having jumped from another "
1026 error
.SetErrorToGenericError();
1027 error
.SetErrorString(interpreter_internal_error
);
1031 Value
*value
= phi_inst
->getIncomingValueForBlock(frame
.m_prev_bb
);
1032 lldb_private::Scalar result
;
1033 if (!frame
.EvaluateValue(result
, value
, module
)) {
1034 LLDB_LOGF(log
, "Couldn't evaluate %s", PrintValue(value
).c_str());
1035 error
.SetErrorToGenericError();
1036 error
.SetErrorString(bad_value_error
);
1039 frame
.AssignValue(inst
, result
, module
);
1042 LLDB_LOGF(log
, "Interpreted a %s", inst
->getOpcodeName());
1043 LLDB_LOGF(log
, " Incoming value : %s",
1044 frame
.SummarizeValue(value
).c_str());
1047 case Instruction::GetElementPtr
: {
1048 const GetElementPtrInst
*gep_inst
= cast
<GetElementPtrInst
>(inst
);
1050 const Value
*pointer_operand
= gep_inst
->getPointerOperand();
1051 Type
*src_elem_ty
= gep_inst
->getSourceElementType();
1053 lldb_private::Scalar P
;
1055 if (!frame
.EvaluateValue(P
, pointer_operand
, module
)) {
1056 LLDB_LOGF(log
, "Couldn't evaluate %s",
1057 PrintValue(pointer_operand
).c_str());
1058 error
.SetErrorToGenericError();
1059 error
.SetErrorString(bad_value_error
);
1063 typedef SmallVector
<Value
*, 8> IndexVector
;
1064 typedef IndexVector::iterator IndexIterator
;
1066 SmallVector
<Value
*, 8> indices(gep_inst
->idx_begin(),
1067 gep_inst
->idx_end());
1069 SmallVector
<Value
*, 8> const_indices
;
1071 for (IndexIterator ii
= indices
.begin(), ie
= indices
.end(); ii
!= ie
;
1073 ConstantInt
*constant_index
= dyn_cast
<ConstantInt
>(*ii
);
1075 if (!constant_index
) {
1076 lldb_private::Scalar I
;
1078 if (!frame
.EvaluateValue(I
, *ii
, module
)) {
1079 LLDB_LOGF(log
, "Couldn't evaluate %s", PrintValue(*ii
).c_str());
1080 error
.SetErrorToGenericError();
1081 error
.SetErrorString(bad_value_error
);
1085 LLDB_LOGF(log
, "Evaluated constant index %s as %llu",
1086 PrintValue(*ii
).c_str(), I
.ULongLong(LLDB_INVALID_ADDRESS
));
1088 constant_index
= cast
<ConstantInt
>(ConstantInt::get(
1089 (*ii
)->getType(), I
.ULongLong(LLDB_INVALID_ADDRESS
)));
1092 const_indices
.push_back(constant_index
);
1096 data_layout
.getIndexedOffsetInType(src_elem_ty
, const_indices
);
1098 lldb_private::Scalar Poffset
= P
+ offset
;
1100 frame
.AssignValue(inst
, Poffset
, module
);
1103 LLDB_LOGF(log
, "Interpreted a GetElementPtrInst");
1104 LLDB_LOGF(log
, " P : %s",
1105 frame
.SummarizeValue(pointer_operand
).c_str());
1106 LLDB_LOGF(log
, " Poffset : %s", frame
.SummarizeValue(inst
).c_str());
1109 case Instruction::FCmp
:
1110 case Instruction::ICmp
: {
1111 const CmpInst
*icmp_inst
= cast
<CmpInst
>(inst
);
1113 CmpInst::Predicate predicate
= icmp_inst
->getPredicate();
1115 Value
*lhs
= inst
->getOperand(0);
1116 Value
*rhs
= inst
->getOperand(1);
1118 lldb_private::Scalar L
;
1119 lldb_private::Scalar R
;
1121 if (!frame
.EvaluateValue(L
, lhs
, module
)) {
1122 LLDB_LOGF(log
, "Couldn't evaluate %s", PrintValue(lhs
).c_str());
1123 error
.SetErrorToGenericError();
1124 error
.SetErrorString(bad_value_error
);
1128 if (!frame
.EvaluateValue(R
, rhs
, module
)) {
1129 LLDB_LOGF(log
, "Couldn't evaluate %s", PrintValue(rhs
).c_str());
1130 error
.SetErrorToGenericError();
1131 error
.SetErrorString(bad_value_error
);
1135 lldb_private::Scalar result
;
1137 switch (predicate
) {
1140 case CmpInst::ICMP_EQ
:
1141 case CmpInst::FCMP_OEQ
:
1144 case CmpInst::ICMP_NE
:
1145 case CmpInst::FCMP_UNE
:
1148 case CmpInst::ICMP_UGT
:
1153 case CmpInst::ICMP_UGE
:
1158 case CmpInst::FCMP_OGE
:
1161 case CmpInst::FCMP_OGT
:
1164 case CmpInst::ICMP_ULT
:
1169 case CmpInst::FCMP_OLT
:
1172 case CmpInst::ICMP_ULE
:
1177 case CmpInst::FCMP_OLE
:
1180 case CmpInst::ICMP_SGT
:
1185 case CmpInst::ICMP_SGE
:
1190 case CmpInst::ICMP_SLT
:
1195 case CmpInst::ICMP_SLE
:
1202 frame
.AssignValue(inst
, result
, module
);
1205 LLDB_LOGF(log
, "Interpreted an ICmpInst");
1206 LLDB_LOGF(log
, " L : %s", frame
.SummarizeValue(lhs
).c_str());
1207 LLDB_LOGF(log
, " R : %s", frame
.SummarizeValue(rhs
).c_str());
1208 LLDB_LOGF(log
, " = : %s", frame
.SummarizeValue(inst
).c_str());
1211 case Instruction::IntToPtr
: {
1212 const IntToPtrInst
*int_to_ptr_inst
= cast
<IntToPtrInst
>(inst
);
1214 Value
*src_operand
= int_to_ptr_inst
->getOperand(0);
1216 lldb_private::Scalar I
;
1218 if (!frame
.EvaluateValue(I
, src_operand
, module
)) {
1219 LLDB_LOGF(log
, "Couldn't evaluate %s", PrintValue(src_operand
).c_str());
1220 error
.SetErrorToGenericError();
1221 error
.SetErrorString(bad_value_error
);
1225 frame
.AssignValue(inst
, I
, module
);
1228 LLDB_LOGF(log
, "Interpreted an IntToPtr");
1229 LLDB_LOGF(log
, " Src : %s", frame
.SummarizeValue(src_operand
).c_str());
1230 LLDB_LOGF(log
, " = : %s", frame
.SummarizeValue(inst
).c_str());
1233 case Instruction::PtrToInt
: {
1234 const PtrToIntInst
*ptr_to_int_inst
= cast
<PtrToIntInst
>(inst
);
1236 Value
*src_operand
= ptr_to_int_inst
->getOperand(0);
1238 lldb_private::Scalar I
;
1240 if (!frame
.EvaluateValue(I
, src_operand
, module
)) {
1241 LLDB_LOGF(log
, "Couldn't evaluate %s", PrintValue(src_operand
).c_str());
1242 error
.SetErrorToGenericError();
1243 error
.SetErrorString(bad_value_error
);
1247 frame
.AssignValue(inst
, I
, module
);
1250 LLDB_LOGF(log
, "Interpreted a PtrToInt");
1251 LLDB_LOGF(log
, " Src : %s", frame
.SummarizeValue(src_operand
).c_str());
1252 LLDB_LOGF(log
, " = : %s", frame
.SummarizeValue(inst
).c_str());
1255 case Instruction::Trunc
: {
1256 const TruncInst
*trunc_inst
= cast
<TruncInst
>(inst
);
1258 Value
*src_operand
= trunc_inst
->getOperand(0);
1260 lldb_private::Scalar I
;
1262 if (!frame
.EvaluateValue(I
, src_operand
, module
)) {
1263 LLDB_LOGF(log
, "Couldn't evaluate %s", PrintValue(src_operand
).c_str());
1264 error
.SetErrorToGenericError();
1265 error
.SetErrorString(bad_value_error
);
1269 frame
.AssignValue(inst
, I
, module
);
1272 LLDB_LOGF(log
, "Interpreted a Trunc");
1273 LLDB_LOGF(log
, " Src : %s", frame
.SummarizeValue(src_operand
).c_str());
1274 LLDB_LOGF(log
, " = : %s", frame
.SummarizeValue(inst
).c_str());
1277 case Instruction::Load
: {
1278 const LoadInst
*load_inst
= cast
<LoadInst
>(inst
);
1280 // The semantics of Load are:
1281 // Create a region D that will contain the loaded data
1282 // Resolve the region P containing a pointer
1283 // Dereference P to get the region R that the data should be loaded from
1284 // Transfer a unit of type type(D) from R to D
1286 const Value
*pointer_operand
= load_inst
->getPointerOperand();
1288 lldb::addr_t D
= frame
.ResolveValue(load_inst
, module
);
1289 lldb::addr_t P
= frame
.ResolveValue(pointer_operand
, module
);
1291 if (D
== LLDB_INVALID_ADDRESS
) {
1292 LLDB_LOGF(log
, "LoadInst's value doesn't resolve to anything");
1293 error
.SetErrorToGenericError();
1294 error
.SetErrorString(bad_value_error
);
1298 if (P
== LLDB_INVALID_ADDRESS
) {
1299 LLDB_LOGF(log
, "LoadInst's pointer doesn't resolve to anything");
1300 error
.SetErrorToGenericError();
1301 error
.SetErrorString(bad_value_error
);
1306 lldb_private::Status read_error
;
1307 execution_unit
.ReadPointerFromMemory(&R
, P
, read_error
);
1309 if (!read_error
.Success()) {
1310 LLDB_LOGF(log
, "Couldn't read the address to be loaded for a LoadInst");
1311 error
.SetErrorToGenericError();
1312 error
.SetErrorString(memory_read_error
);
1316 Type
*target_ty
= load_inst
->getType();
1317 size_t target_size
= data_layout
.getTypeStoreSize(target_ty
);
1318 lldb_private::DataBufferHeap
buffer(target_size
, 0);
1321 execution_unit
.ReadMemory(buffer
.GetBytes(), R
, buffer
.GetByteSize(),
1323 if (!read_error
.Success()) {
1324 LLDB_LOGF(log
, "Couldn't read from a region on behalf of a LoadInst");
1325 error
.SetErrorToGenericError();
1326 error
.SetErrorString(memory_read_error
);
1330 lldb_private::Status write_error
;
1331 execution_unit
.WriteMemory(D
, buffer
.GetBytes(), buffer
.GetByteSize(),
1333 if (!write_error
.Success()) {
1334 LLDB_LOGF(log
, "Couldn't write to a region on behalf of a LoadInst");
1335 error
.SetErrorToGenericError();
1336 error
.SetErrorString(memory_write_error
);
1341 LLDB_LOGF(log
, "Interpreted a LoadInst");
1342 LLDB_LOGF(log
, " P : 0x%" PRIx64
, P
);
1343 LLDB_LOGF(log
, " R : 0x%" PRIx64
, R
);
1344 LLDB_LOGF(log
, " D : 0x%" PRIx64
, D
);
1347 case Instruction::Ret
: {
1350 case Instruction::Store
: {
1351 const StoreInst
*store_inst
= cast
<StoreInst
>(inst
);
1353 // The semantics of Store are:
1354 // Resolve the region D containing the data to be stored
1355 // Resolve the region P containing a pointer
1356 // Dereference P to get the region R that the data should be stored in
1357 // Transfer a unit of type type(D) from D to R
1359 const Value
*value_operand
= store_inst
->getValueOperand();
1360 const Value
*pointer_operand
= store_inst
->getPointerOperand();
1362 lldb::addr_t D
= frame
.ResolveValue(value_operand
, module
);
1363 lldb::addr_t P
= frame
.ResolveValue(pointer_operand
, module
);
1365 if (D
== LLDB_INVALID_ADDRESS
) {
1366 LLDB_LOGF(log
, "StoreInst's value doesn't resolve to anything");
1367 error
.SetErrorToGenericError();
1368 error
.SetErrorString(bad_value_error
);
1372 if (P
== LLDB_INVALID_ADDRESS
) {
1373 LLDB_LOGF(log
, "StoreInst's pointer doesn't resolve to anything");
1374 error
.SetErrorToGenericError();
1375 error
.SetErrorString(bad_value_error
);
1380 lldb_private::Status read_error
;
1381 execution_unit
.ReadPointerFromMemory(&R
, P
, read_error
);
1383 if (!read_error
.Success()) {
1384 LLDB_LOGF(log
, "Couldn't read the address to be loaded for a LoadInst");
1385 error
.SetErrorToGenericError();
1386 error
.SetErrorString(memory_read_error
);
1390 Type
*target_ty
= value_operand
->getType();
1391 size_t target_size
= data_layout
.getTypeStoreSize(target_ty
);
1392 lldb_private::DataBufferHeap
buffer(target_size
, 0);
1395 execution_unit
.ReadMemory(buffer
.GetBytes(), D
, buffer
.GetByteSize(),
1397 if (!read_error
.Success()) {
1398 LLDB_LOGF(log
, "Couldn't read from a region on behalf of a StoreInst");
1399 error
.SetErrorToGenericError();
1400 error
.SetErrorString(memory_read_error
);
1404 lldb_private::Status write_error
;
1405 execution_unit
.WriteMemory(R
, buffer
.GetBytes(), buffer
.GetByteSize(),
1407 if (!write_error
.Success()) {
1408 LLDB_LOGF(log
, "Couldn't write to a region on behalf of a StoreInst");
1409 error
.SetErrorToGenericError();
1410 error
.SetErrorString(memory_write_error
);
1415 LLDB_LOGF(log
, "Interpreted a StoreInst");
1416 LLDB_LOGF(log
, " D : 0x%" PRIx64
, D
);
1417 LLDB_LOGF(log
, " P : 0x%" PRIx64
, P
);
1418 LLDB_LOGF(log
, " R : 0x%" PRIx64
, R
);
1421 case Instruction::Call
: {
1422 const CallInst
*call_inst
= cast
<CallInst
>(inst
);
1424 if (CanIgnoreCall(call_inst
))
1427 // Get the return type
1428 llvm::Type
*returnType
= call_inst
->getType();
1429 if (returnType
== nullptr) {
1430 error
.SetErrorToGenericError();
1431 error
.SetErrorString("unable to access return type");
1435 // Work with void, integer and pointer return types
1436 if (!returnType
->isVoidTy() && !returnType
->isIntegerTy() &&
1437 !returnType
->isPointerTy()) {
1438 error
.SetErrorToGenericError();
1439 error
.SetErrorString("return type is not supported");
1443 // Check we can actually get a thread
1444 if (exe_ctx
.GetThreadPtr() == nullptr) {
1445 error
.SetErrorToGenericError();
1446 error
.SetErrorString("unable to acquire thread");
1450 // Make sure we have a valid process
1452 error
.SetErrorToGenericError();
1453 error
.SetErrorString("unable to get the process");
1457 // Find the address of the callee function
1458 lldb_private::Scalar I
;
1459 const llvm::Value
*val
= call_inst
->getCalledOperand();
1461 if (!frame
.EvaluateValue(I
, val
, module
)) {
1462 error
.SetErrorToGenericError();
1463 error
.SetErrorString("unable to get address of function");
1466 lldb_private::Address
funcAddr(I
.ULongLong(LLDB_INVALID_ADDRESS
));
1468 lldb_private::DiagnosticManager diagnostics
;
1469 lldb_private::EvaluateExpressionOptions options
;
1471 llvm::FunctionType
*prototype
= call_inst
->getFunctionType();
1473 // Find number of arguments
1474 const int numArgs
= call_inst
->arg_size();
1476 // We work with a fixed array of 16 arguments which is our upper limit
1477 static lldb_private::ABI::CallArgument rawArgs
[16];
1478 if (numArgs
>= 16) {
1479 error
.SetErrorToGenericError();
1480 error
.SetErrorString("function takes too many arguments");
1484 // Push all function arguments to the argument list that will be passed
1485 // to the call function thread plan
1486 for (int i
= 0; i
< numArgs
; i
++) {
1487 // Get details of this argument
1488 llvm::Value
*arg_op
= call_inst
->getArgOperand(i
);
1489 llvm::Type
*arg_ty
= arg_op
->getType();
1491 // Ensure that this argument is an supported type
1492 if (!arg_ty
->isIntegerTy() && !arg_ty
->isPointerTy()) {
1493 error
.SetErrorToGenericError();
1494 error
.SetErrorStringWithFormat("argument %d must be integer type", i
);
1498 // Extract the arguments value
1499 lldb_private::Scalar tmp_op
= 0;
1500 if (!frame
.EvaluateValue(tmp_op
, arg_op
, module
)) {
1501 error
.SetErrorToGenericError();
1502 error
.SetErrorStringWithFormat("unable to evaluate argument %d", i
);
1506 // Check if this is a string literal or constant string pointer
1507 if (arg_ty
->isPointerTy()) {
1508 lldb::addr_t addr
= tmp_op
.ULongLong();
1509 size_t dataSize
= 0;
1511 bool Success
= execution_unit
.GetAllocSize(addr
, dataSize
);
1514 "unable to locate host data for transfer to device");
1515 // Create the required buffer
1516 rawArgs
[i
].size
= dataSize
;
1517 rawArgs
[i
].data_up
.reset(new uint8_t[dataSize
+ 1]);
1519 // Read string from host memory
1520 execution_unit
.ReadMemory(rawArgs
[i
].data_up
.get(), addr
, dataSize
,
1522 assert(!error
.Fail() &&
1523 "we have failed to read the string from memory");
1525 // Add null terminator
1526 rawArgs
[i
].data_up
[dataSize
] = '\0';
1527 rawArgs
[i
].type
= lldb_private::ABI::CallArgument::HostPointer
;
1528 } else /* if ( arg_ty->isPointerTy() ) */
1530 rawArgs
[i
].type
= lldb_private::ABI::CallArgument::TargetValue
;
1531 // Get argument size in bytes
1532 rawArgs
[i
].size
= arg_ty
->getIntegerBitWidth() / 8;
1533 // Push value into argument list for thread plan
1534 rawArgs
[i
].value
= tmp_op
.ULongLong();
1538 // Pack the arguments into an llvm::array
1539 llvm::ArrayRef
<lldb_private::ABI::CallArgument
> args(rawArgs
, numArgs
);
1541 // Setup a thread plan to call the target function
1542 lldb::ThreadPlanSP
call_plan_sp(
1543 new lldb_private::ThreadPlanCallFunctionUsingABI(
1544 exe_ctx
.GetThreadRef(), funcAddr
, *prototype
, *returnType
, args
,
1547 // Check if the plan is valid
1548 lldb_private::StreamString ss
;
1549 if (!call_plan_sp
|| !call_plan_sp
->ValidatePlan(&ss
)) {
1550 error
.SetErrorToGenericError();
1551 error
.SetErrorStringWithFormat(
1552 "unable to make ThreadPlanCallFunctionUsingABI for 0x%llx",
1557 process
->SetRunningUserExpression(true);
1559 // Execute the actual function call thread plan
1560 lldb::ExpressionResults res
=
1561 process
->RunThreadPlan(exe_ctx
, call_plan_sp
, options
, diagnostics
);
1563 // Check that the thread plan completed successfully
1564 if (res
!= lldb::ExpressionResults::eExpressionCompleted
) {
1565 error
.SetErrorToGenericError();
1566 error
.SetErrorString("ThreadPlanCallFunctionUsingABI failed");
1570 process
->SetRunningUserExpression(false);
1573 if (returnType
->isVoidTy()) {
1574 // Cant assign to void types, so we leave the frame untouched
1576 // Integer or pointer return type
1577 if (returnType
->isIntegerTy() || returnType
->isPointerTy()) {
1578 // Get the encapsulated return value
1579 lldb::ValueObjectSP retVal
= call_plan_sp
.get()->GetReturnValueObject();
1581 lldb_private::Scalar returnVal
= -1;
1582 lldb_private::ValueObject
*vobj
= retVal
.get();
1584 // Check if the return value is valid
1585 if (vobj
== nullptr || !retVal
) {
1586 error
.SetErrorToGenericError();
1587 error
.SetErrorString("unable to get the return value");
1591 // Extract the return value as a integer
1592 lldb_private::Value
&value
= vobj
->GetValue();
1593 returnVal
= value
.GetScalar();
1595 // Push the return value as the result
1596 frame
.AssignValue(inst
, returnVal
, module
);