Index: include/lldb/Expression/IRInterpreter.h =================================================================== --- include/lldb/Expression/IRInterpreter.h +++ include/lldb/Expression/IRInterpreter.h @@ -44,7 +44,8 @@ static bool CanInterpret (llvm::Module &module, llvm::Function &function, - lldb_private::Error &error); + lldb_private::Error &error, + lldb_private::ExecutionContext &exe_ctx); static bool Interpret (llvm::Module &module, @@ -53,7 +54,8 @@ lldb_private::IRMemoryMap &memory_map, lldb_private::Error &error, lldb::addr_t stack_frame_bottom, - lldb::addr_t stack_frame_top); + lldb::addr_t stack_frame_top, + lldb_private::ExecutionContext &exe_ctx); private: static bool Index: include/lldb/Expression/IRMemoryMap.h =================================================================== --- include/lldb/Expression/IRMemoryMap.h +++ include/lldb/Expression/IRMemoryMap.h @@ -60,7 +60,7 @@ void ReadMemory (uint8_t *bytes, lldb::addr_t process_address, size_t size, Error &error); void ReadScalarFromMemory (Scalar &scalar, lldb::addr_t process_address, size_t size, Error &error); void ReadPointerFromMemory (lldb::addr_t *address, lldb::addr_t process_address, Error &error); - + bool GetAllocSize(lldb::addr_t address, size_t &size); void GetMemoryData (DataExtractor &extractor, lldb::addr_t process_address, size_t size, Error &error); lldb::ByteOrder GetByteOrder(); Index: include/lldb/Target/ABI.h =================================================================== --- include/lldb/Target/ABI.h +++ include/lldb/Target/ABI.h @@ -58,7 +58,7 @@ lldb::addr_t returnAddress, llvm::ArrayRef args) const = 0; - // Prepare trivial call used from ThreadPlanFunctionCallGDB + // Prepare trivial call used from ThreadPlanFunctionCallNoJIT // AD: // . Because i don't want to change other ABI's this is not declared pure virtual. // The dummy implementation will simply fail. Only HexagonABI will currently Index: include/lldb/Target/ThreadPlanCallFunction.h =================================================================== --- include/lldb/Target/ThreadPlanCallFunction.h +++ include/lldb/Target/ThreadPlanCallFunction.h @@ -34,6 +34,10 @@ llvm::ArrayRef args, const EvaluateExpressionOptions &options); + ThreadPlanCallFunction(Thread &thread, + const Address &function, + const EvaluateExpressionOptions &options); + virtual ~ThreadPlanCallFunction (); @@ -133,9 +137,10 @@ virtual bool DoPlanExplainsStop (Event *event_ptr); + + virtual void + SetReturnValue(); -private: - bool ConstructorSetup (Thread &thread, ABI *& abi, @@ -153,7 +158,7 @@ bool BreakpointsExplainStop (); - + bool m_valid; bool m_stop_other_threads; bool m_unwind_on_error; @@ -172,7 +177,6 @@ // it's nice to know the real stop reason. // This gets set in DoTakedown. StreamString m_constructor_errors; - ClangASTType m_return_type; lldb::ValueObjectSP m_return_valobj_sp; // If this contains a valid pointer, use the ABI to extract values when complete bool m_takedown_done; // We want to ensure we only do the takedown once. This ensures that. bool m_should_clear_objc_exception_bp; @@ -179,6 +183,8 @@ bool m_should_clear_cxx_exception_bp; lldb::addr_t m_stop_address; // This is the address we stopped at. Also set in DoTakedown; +private: + ClangASTType m_return_type; DISALLOW_COPY_AND_ASSIGN (ThreadPlanCallFunction); }; Index: include/lldb/Target/ThreadPlanCallFunctionNoJIT.h =================================================================== --- include/lldb/Target/ThreadPlanCallFunctionNoJIT.h +++ include/lldb/Target/ThreadPlanCallFunctionNoJIT.h @@ -0,0 +1,58 @@ +//===-- ThreadPlanCallFunctionNoJIT.h --------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_ThreadPlanCallFunctionNoJIT_h_ +#define liblldb_ThreadPlanCallFunctionNoJIT_h_ + +// C Includes +// C++ Includes +// Other libraries and framework includes +// Project includes +#include "lldb/lldb-private.h" +#include "lldb/Target/ABI.h" +#include "lldb/Target/Thread.h" +#include "lldb/Target/ThreadPlanCallFunction.h" + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/IR/Type.h" + +namespace lldb_private { + +class ThreadPlanCallFunctionNoJIT : public ThreadPlanCallFunction +{ + // Create a thread plan to call a function at the address passed in the "function" + // argument, this function is executed using register manipulation instead of JIT. + // Class derives from ThreadPlanCallFunction and differs by calling a alternative + // ABI interface ABI::PrepareTrivialCall() which provides more detailed information. +public: + ThreadPlanCallFunctionNoJIT (Thread &thread, + const Address &function_address, + llvm::Type &function_prototype, + llvm::Type &return_type, + llvm::ArrayRef args, + const EvaluateExpressionOptions &options); + + ~ThreadPlanCallFunctionNoJIT (); + + void + GetDescription (Stream *s, lldb::DescriptionLevel level) override; + +protected: + void + SetReturnValue () override; + + +private: + llvm::Type &m_return_type; + DISALLOW_COPY_AND_ASSIGN (ThreadPlanCallFunctionNoJIT); +}; + +} // namespace lldb_private + +#endif // liblldb_ThreadPlanCallFunctionNoJIT_h_ Index: source/Expression/ClangExpressionParser.cpp =================================================================== --- source/Expression/ClangExpressionParser.cpp +++ source/Expression/ClangExpressionParser.cpp @@ -539,7 +539,7 @@ Error interpret_error; - can_interpret = IRInterpreter::CanInterpret(*execution_unit_sp->GetModule(), *execution_unit_sp->GetFunction(), interpret_error); + can_interpret = IRInterpreter::CanInterpret(*execution_unit_sp->GetModule(), *execution_unit_sp->GetFunction(), interpret_error, exe_ctx); Process *process = exe_ctx.GetProcessPtr(); Index: source/Expression/ClangUserExpression.cpp =================================================================== --- source/Expression/ClangUserExpression.cpp +++ source/Expression/ClangUserExpression.cpp @@ -897,7 +897,8 @@ *m_execution_unit_sp.get(), interpreter_error, function_stack_bottom, - function_stack_top); + function_stack_top, + exe_ctx); if (!interpreter_error.Success()) { Index: source/Expression/IRInterpreter.cpp =================================================================== --- source/Expression/IRInterpreter.cpp +++ source/Expression/IRInterpreter.cpp @@ -10,17 +10,28 @@ #include "lldb/Core/DataExtractor.h" #include "lldb/Core/Error.h" #include "lldb/Core/Log.h" +#include "lldb/Core/ModuleSpec.h" +#include "lldb/Core/Module.h" #include "lldb/Core/Scalar.h" #include "lldb/Core/StreamString.h" +#include "lldb/Core/ValueObject.h" #include "lldb/Expression/IRMemoryMap.h" #include "lldb/Expression/IRInterpreter.h" #include "lldb/Host/Endian.h" +#include "lldb/Target/ABI.h" +#include "lldb/Target/ExecutionContext.h" +#include "lldb/Target/Target.h" +#include "lldb/Target/Thread.h" +#include "lldb/Target/ThreadPlan.h" +#include "lldb/Target/ThreadPlanCallFunctionNoJIT.h" + #include "llvm/IR/Constants.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/Function.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/Intrinsics.h" +#include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/Support/raw_ostream.h" @@ -455,7 +466,8 @@ bool IRInterpreter::CanInterpret (llvm::Module &module, llvm::Function &function, - lldb_private::Error &error) + lldb_private::Error &error, + lldb_private::ExecutionContext &exe_ctx) { lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); @@ -507,6 +519,11 @@ return false; } + // For the time being, don't mess up other targets with hexagon stuff. + const lldb_private::ArchSpec &spec = exe_ctx.GetTargetRef().GetArchitecture(); + if (spec.GetCore() == lldb_private::ArchSpec::eCore_hexagon_generic) + break; + if (!CanIgnoreCall(call_inst)) { if (log) @@ -611,7 +628,8 @@ lldb_private::IRMemoryMap &memory_map, lldb_private::Error &error, lldb::addr_t stack_frame_bottom, - lldb::addr_t stack_frame_top) + lldb::addr_t stack_frame_top, + lldb_private::ExecutionContext &exe_ctx) { lldb_private::Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); @@ -668,29 +686,7 @@ { default: break; - case Instruction::Call: - { - const CallInst *call_inst = dyn_cast(inst); - if (!call_inst) - { - if (log) - log->Printf("getOpcode() returns %s, but instruction is not a CallInst", inst->getOpcodeName()); - error.SetErrorToGenericError(); - error.SetErrorString(interpreter_internal_error); - return false; - } - - if (!CanIgnoreCall(call_inst)) - { - if (log) - log->Printf("The interpreter shouldn't have accepted %s", PrintValue(call_inst).c_str()); - error.SetErrorToGenericError(); - error.SetErrorString(interpreter_internal_error); - return false; - } - } - break; case Instruction::Add: case Instruction::Sub: case Instruction::Mul: @@ -1476,6 +1472,242 @@ } } break; + case Instruction::Call: + { + const CallInst *call_inst = dyn_cast(inst); + + if (!call_inst) + { + if (log) + log->Printf("getOpcode() returns %s, but instruction is not a CallInst", inst->getOpcodeName()); + error.SetErrorToGenericError(); + error.SetErrorString(interpreter_internal_error); + return false; + } + + if (CanIgnoreCall(call_inst)) + break; + + // Get the return type + llvm::Type *returnType = call_inst->getType(); + if (returnType == nullptr) + { + error.SetErrorToGenericError(); + error.SetErrorString("unable to access return type"); + return false; + } + + // Work with void, integer and pointer return types + if (!returnType->isVoidTy() && + !returnType->isIntegerTy() && + !returnType->isPointerTy()) + { + error.SetErrorToGenericError(); + error.SetErrorString("return type is not supported"); + return false; + } + + // Find the address of the callee function + lldb_private::Scalar I; + const llvm::Value *val = call_inst->getCalledValue(); + + if (!frame.EvaluateValue(I, val, module)) + { + error.SetErrorToGenericError(); + error.SetErrorString("unable to get address of function"); + return false; + } + lldb_private::Address funcAddr(I.ULongLong(LLDB_INVALID_ADDRESS)); + + lldb_private::StreamString error_stream; + lldb_private::EvaluateExpressionOptions options; + + // We generally receive a function pointer which we must dereference + llvm::Type* prototype = val->getType(); + if (!prototype->isPointerTy()) + { + error.SetErrorToGenericError(); + error.SetErrorString("call need function pointer"); + return false; + } + + // Dereference the function pointer + prototype = prototype->getPointerElementType(); + if (!(prototype->isFunctionTy() || prototype->isFunctionVarArg())) + { + error.SetErrorToGenericError(); + error.SetErrorString("call need function pointer"); + return false; + } + + // Find number of arguments + const int numArgs = call_inst->getNumArgOperands(); + + // We work with a fixed array of 16 arguments which is our upper limit + static lldb_private::ABI::CallArgument rawArgs[16]; + if (numArgs >= 16) + { + error.SetErrorToGenericError(); + error.SetErrorStringWithFormat("function takes too many arguments"); + return false; + } + + // Push all function arguments to the argument list that will + // be passed to the call function thread plan + for (int i = 0; i < numArgs; i++) + { + // Get details of this argument + llvm::Value *arg_op = call_inst->getArgOperand(i); + llvm::Type *arg_ty = arg_op->getType(); + + // Ensure that this argument is an int32 type + if (!arg_ty->isIntegerTy() && !arg_ty->isPointerTy()) + { + error.SetErrorToGenericError(); + error.SetErrorStringWithFormat("argument %d must be integer type", i); + return false; + } + + // Extract the arguments value + lldb_private::Scalar tmp_op = 0; + if (!frame.EvaluateValue(tmp_op, arg_op, module)) + { + error.SetErrorToGenericError(); + error.SetErrorStringWithFormat("unable to evaluate argument %d", i); + return false; + } + + // Check if this is a string literal or constant string pointer + if (arg_ty->isPointerTy()) + { + // Pointer to just one type + assert(arg_ty->getNumContainedTypes() == 1); + + uint32_t addr = tmp_op.ULongLong(); + size_t dataSize = 0; + + if (memory_map.GetAllocSize(addr, dataSize)) + { +#pragma message ("This is currently a memory leak and needs a proper solution") + // Create the required buffer + rawArgs[i].size = dataSize; + rawArgs[i].data = new uint8_t[dataSize + 1]; + // Read string from host memory + memory_map.ReadMemory(rawArgs[i].data, addr, dataSize, error); + if (error.Fail()) + { + assert(!"we have failed to read the string from memory"); + return false; + } + // Add null terminator + rawArgs[i].data[dataSize] = '\0'; + rawArgs[i].type = lldb_private::ABI::CallArgument::HostPointer; + } + else + { + assert(!"unable to locate host data for transfer to device"); + return false; + } + } + else /* if ( arg_ty->isPointerTy() ) */ + { + rawArgs[i].type = lldb_private::ABI::CallArgument::TargetValue; + // 4 bytes for 32bit integer + rawArgs[i].size = 4; + // Push value into argument list for thread plan + rawArgs[i].value = tmp_op.ULongLong(); + } + + } + + // Pack the arguments into an llvm::array + llvm::ArrayRef args(rawArgs, numArgs); + + // Check we can actually get a thread + if (exe_ctx.GetThreadPtr() == nullptr) + { + error.SetErrorToGenericError(); + error.SetErrorStringWithFormat("unable to acquire thread"); + return false; + } + + // Setup a thread plan to call the target function + lldb::ThreadPlanSP call_plan_sp + ( + new lldb_private::ThreadPlanCallFunctionNoJIT + ( + exe_ctx.GetThreadRef(), + funcAddr, + *prototype, + *returnType, + args, + options + ) + ); + + // Check if the plan is valid + if (!call_plan_sp || !call_plan_sp->ValidatePlan(&error_stream)) + { + error.SetErrorToGenericError(); + error.SetErrorStringWithFormat("unable to make ThreadPlanCallFunctionNoJIT for 0x%llx", I.ULongLong()); + return false; + } + + // Make sure we have a valid process + if (!exe_ctx.GetProcessPtr()) + { + error.SetErrorToGenericError(); + error.SetErrorStringWithFormat("unable to get the process"); + return false; + } + + exe_ctx.GetProcessPtr()->SetRunningUserExpression(true); + + // Execute the actual function call thread plan + lldb::ExpressionResults res = exe_ctx.GetProcessRef().RunThreadPlan(exe_ctx, call_plan_sp, options, error_stream); + + // Check that the thread plan completed successfully + if (res != lldb::ExpressionResults::eExpressionCompleted) + { + error.SetErrorToGenericError(); + error.SetErrorStringWithFormat("threadPlanCallFunctionNoJIT failed"); + return false; + } + + exe_ctx.GetProcessPtr()->SetRunningUserExpression(false); + + // Void return type + if (returnType->isVoidTy()) + { + // Cant assign to void types, so we leave the frame untouched + } + else + // Integer or pointer return type + if (returnType->isIntegerTy() || returnType->isPointerTy()) + { + // Get the encapsulated return value + lldb::ValueObjectSP retVal = call_plan_sp.get()->GetReturnValueObject(); + + lldb_private::Scalar returnVal = -1; + lldb_private::ValueObject *vobj = retVal.get(); + + // Check if the return value is valid + if (vobj == nullptr || retVal.empty()) + { + error.SetErrorToGenericError(); + error.SetErrorStringWithFormat("unable to get the return value"); + return false; + } + + // Extract the return value as a integer + lldb_private::Value & value = vobj->GetValue(); + returnVal = value.GetScalar(); + + // Push the return value as the result + frame.AssignValue(inst, returnVal, module); + } + } + break; } ++frame.m_ii; Index: source/Expression/IRMemoryMap.cpp =================================================================== --- source/Expression/IRMemoryMap.cpp +++ source/Expression/IRMemoryMap.cpp @@ -418,6 +418,32 @@ m_allocations.erase(iter); } +bool +IRMemoryMap::GetAllocSize(lldb::addr_t address, size_t &size) +{ + AllocationMap::iterator iter = FindAllocation(address, size); + if (iter == m_allocations.end()) + return false; + + Allocation &al = iter->second; + + if (address > (al.m_process_start + al.m_size)) + { + size = 0; + return false; + } + + if (address > al.m_process_start) + { + int dif = address - al.m_process_start; + size = al.m_size - dif; + return true; + } + + size = al.m_size; + return true; +} + void IRMemoryMap::WriteMemory (lldb::addr_t process_address, const uint8_t *bytes, size_t size, Error &error) { Index: source/Target/CMakeLists.txt =================================================================== --- source/Target/CMakeLists.txt +++ source/Target/CMakeLists.txt @@ -40,6 +40,7 @@ ThreadPlan.cpp ThreadPlanBase.cpp ThreadPlanCallFunction.cpp + ThreadPlanCallFunctionNoJIT.cpp ThreadPlanCallUserExpression.cpp ThreadPlanPython.cpp ThreadPlanRunToAddress.cpp Index: source/Target/ThreadPlanCallFunction.cpp =================================================================== --- source/Target/ThreadPlanCallFunction.cpp +++ source/Target/ThreadPlanCallFunction.cpp @@ -153,9 +153,10 @@ m_should_clear_cxx_exception_bp (false), m_stop_address (LLDB_INVALID_ADDRESS) { - lldb::addr_t start_load_addr; - ABI *abi; - lldb::addr_t function_load_addr; + lldb::addr_t start_load_addr = LLDB_INVALID_ADDRESS; + lldb::addr_t function_load_addr = LLDB_INVALID_ADDRESS; + ABI *abi = nullptr; + if (!ConstructorSetup (thread, abi, start_load_addr, function_load_addr)) return; @@ -171,6 +172,27 @@ m_valid = true; } +ThreadPlanCallFunction::ThreadPlanCallFunction(Thread &thread, + const Address &function, + const EvaluateExpressionOptions &options) : + ThreadPlan(ThreadPlan::eKindCallFunction, "Call function plan", thread, eVoteNoOpinion, eVoteNoOpinion), + m_valid(false), + m_stop_other_threads(options.GetStopOthers()), + m_unwind_on_error(options.DoesUnwindOnError()), + m_ignore_breakpoints(options.DoesIgnoreBreakpoints()), + m_debug_execution(options.GetDebug()), + m_trap_exceptions(options.GetTrapExceptions()), + m_function_addr(function), + m_function_sp(0), + m_return_type(ClangASTType()), + m_takedown_done(false), + m_should_clear_objc_exception_bp(false), + m_should_clear_cxx_exception_bp(false), + m_stop_address(LLDB_INVALID_ADDRESS) +{ + +} + ThreadPlanCallFunction::~ThreadPlanCallFunction () { DoTakedown(PlanSucceeded()); @@ -222,13 +244,7 @@ { if (success) { - ProcessSP process_sp (m_thread.GetProcess()); - const ABI *abi = process_sp ? process_sp->GetABI().get() : NULL; - if (abi && m_return_type.IsValid()) - { - const bool persistent = false; - m_return_valobj_sp = abi->GetReturnValueObject (m_thread, m_return_type, persistent); - } + SetReturnValue(); } if (log) log->Printf ("ThreadPlanCallFunction(%p): DoTakedown called for thread 0x%4.4" PRIx64 ", m_valid: %d complete: %d.\n", @@ -574,3 +590,15 @@ return GetThread().RestoreThreadStateFromCheckpoint(m_stored_thread_state); } + +void +ThreadPlanCallFunction::SetReturnValue() +{ + ProcessSP process_sp(m_thread.GetProcess()); + const ABI *abi = process_sp ? process_sp->GetABI().get() : NULL; + if (abi && m_return_type.IsValid()) + { + const bool persistent = false; + m_return_valobj_sp = abi->GetReturnValueObject(m_thread, m_return_type, persistent); + } +} Index: source/Target/ThreadPlanCallFunctionNoJIT.cpp =================================================================== --- source/Target/ThreadPlanCallFunctionNoJIT.cpp +++ source/Target/ThreadPlanCallFunctionNoJIT.cpp @@ -0,0 +1,91 @@ +//===-- ThreadPlanCallFunctionNoJIT.cpp ------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "lldb/Target/ThreadPlanCallFunctionNoJIT.h" + +// C Includes +// C++ Includes +// Other libraries and framework includes + +// Project includes +#include "lldb/Core/Address.h" +#include "lldb/Core/Log.h" +#include "lldb/Core/Stream.h" +#include "lldb/Target/Process.h" +#include "lldb/Target/RegisterContext.h" +#include "lldb/Target/Target.h" +#include "lldb/Target/Thread.h" + +using namespace lldb; +using namespace lldb_private; + +//------------------------------------------------------------------------ +// ThreadPlanCallFunctionNoJIT: Plan to call a single function without JIT +//------------------------------------------------------------------------ +ThreadPlanCallFunctionNoJIT::ThreadPlanCallFunctionNoJIT (Thread &thread, + const Address &function, + llvm::Type &prototype, + llvm::Type &return_type, + llvm::ArrayRef args, + const EvaluateExpressionOptions &options) : + ThreadPlanCallFunction(thread,function,options), + m_return_type(return_type) +{ + lldb::addr_t start_load_addr = LLDB_INVALID_ADDRESS; + lldb::addr_t function_load_addr = LLDB_INVALID_ADDRESS; + ABI *abi = nullptr; + + if (!ConstructorSetup(thread, abi, start_load_addr, function_load_addr)) + return; + + if (!abi->PrepareTrivialCall(thread, + m_function_sp, + function_load_addr, + start_load_addr, + prototype, + args)) + return; + + ReportRegisterState("Function call was set up. Register state was:"); + + m_valid = true; +} + +ThreadPlanCallFunctionNoJIT::~ThreadPlanCallFunctionNoJIT() +{ + +} + +void +ThreadPlanCallFunctionNoJIT::GetDescription(Stream *s, DescriptionLevel level) +{ + if (level == eDescriptionLevelBrief) + { + s->Printf("Function call thread plan witout JIT"); + } + else + { + TargetSP target_sp(m_thread.CalculateTarget()); + s->Printf("Thread plan to call 0x%" PRIx64, m_function_addr.GetLoadAddress(target_sp.get())); + } +} + +void +ThreadPlanCallFunctionNoJIT::SetReturnValue() +{ + ProcessSP process_sp(m_thread.GetProcess()); + const ABI *abi = process_sp ? process_sp->GetABI().get() : NULL; + + // Ask the abi for the return value + if (abi) + { + const bool persistent = false; + m_return_valobj_sp = abi->GetReturnValueObject(m_thread, m_return_type, persistent); + } +}