brintos

brintos / llvm-project-archived public Read only

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