1696bd635SAlexander Shaposhnikov //===-- UtilityFunction.cpp -------------------------------------*- C++ -*-===// 2151c032cSJim Ingham // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6151c032cSJim Ingham // 7151c032cSJim Ingham //===----------------------------------------------------------------------===// 8151c032cSJim Ingham 9c6551bf0SHaibo Huang #include "lldb/Host/Config.h" 10c6551bf0SHaibo Huang 11151c032cSJim Ingham #include <stdio.h> 12151c032cSJim Ingham #if HAVE_SYS_TYPES_H 13151c032cSJim Ingham #include <sys/types.h> 14151c032cSJim Ingham #endif 15151c032cSJim Ingham 16151c032cSJim Ingham 17151c032cSJim Ingham #include "lldb/Core/Module.h" 18151c032cSJim Ingham #include "lldb/Core/StreamFile.h" 19579e70c9SSean Callanan #include "lldb/Expression/DiagnosticManager.h" 20579e70c9SSean Callanan #include "lldb/Expression/FunctionCaller.h" 21151c032cSJim Ingham #include "lldb/Expression/IRExecutionUnit.h" 22579e70c9SSean Callanan #include "lldb/Expression/UtilityFunction.h" 23151c032cSJim Ingham #include "lldb/Host/Host.h" 24151c032cSJim Ingham #include "lldb/Target/ExecutionContext.h" 25804f981fSBruce Mitchener #include "lldb/Target/Process.h" 26151c032cSJim Ingham #include "lldb/Target/Target.h" 27bf9a7730SZachary Turner #include "lldb/Utility/ConstString.h" 286f9e6901SZachary Turner #include "lldb/Utility/Log.h" 29bf9a7730SZachary Turner #include "lldb/Utility/Stream.h" 30151c032cSJim Ingham 31151c032cSJim Ingham using namespace lldb_private; 32151c032cSJim Ingham using namespace lldb; 33151c032cSJim Ingham 34*52f3a2faSRaphael Isemann char UtilityFunction::ID; 35*52f3a2faSRaphael Isemann 36151c032cSJim Ingham /// Constructor 37151c032cSJim Ingham /// 38f05b42e9SAdrian Prantl /// \param[in] text 39151c032cSJim Ingham /// The text of the function. Must be a full translation unit. 40151c032cSJim Ingham /// 41f05b42e9SAdrian Prantl /// \param[in] name 42151c032cSJim Ingham /// The name of the function, as used in the text. 43151c032cSJim Ingham UtilityFunction::UtilityFunction(ExecutionContextScope &exe_scope, 44*52f3a2faSRaphael Isemann const char *text, const char *name) 45*52f3a2faSRaphael Isemann : Expression(exe_scope), m_execution_unit_sp(), m_jit_module_wp(), 46*52f3a2faSRaphael Isemann m_function_text(), m_function_name(name) {} 47151c032cSJim Ingham 48b9c1b51eSKate Stone UtilityFunction::~UtilityFunction() { 49151c032cSJim Ingham lldb::ProcessSP process_sp(m_jit_process_wp.lock()); 50b9c1b51eSKate Stone if (process_sp) { 51151c032cSJim Ingham lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock()); 52151c032cSJim Ingham if (jit_module_sp) 53151c032cSJim Ingham process_sp->GetTarget().GetImages().Remove(jit_module_sp); 54151c032cSJim Ingham } 55151c032cSJim Ingham } 56151c032cSJim Ingham 57b9c1b51eSKate Stone // FIXME: We should check that every time this is called it is called with the 58b9c1b51eSKate Stone // same return type & arguments... 59151c032cSJim Ingham 60b9c1b51eSKate Stone FunctionCaller *UtilityFunction::MakeFunctionCaller( 61b9c1b51eSKate Stone const CompilerType &return_type, const ValueList &arg_value_list, 6297206d57SZachary Turner lldb::ThreadSP thread_to_use_sp, Status &error) { 63151c032cSJim Ingham if (m_caller_up) 64151c032cSJim Ingham return m_caller_up.get(); 65151c032cSJim Ingham 66151c032cSJim Ingham ProcessSP process_sp = m_jit_process_wp.lock(); 67b9c1b51eSKate Stone if (!process_sp) { 68dae50bafSJim Ingham error.SetErrorString("Can't make a function caller without a process."); 69151c032cSJim Ingham return nullptr; 70dae50bafSJim Ingham } 71151c032cSJim Ingham 72151c032cSJim Ingham Address impl_code_address; 73151c032cSJim Ingham impl_code_address.SetOffset(StartAddress()); 74151c032cSJim Ingham std::string name(m_function_name); 75151c032cSJim Ingham name.append("-caller"); 76151c032cSJim Ingham 77b9c1b51eSKate Stone m_caller_up.reset(process_sp->GetTarget().GetFunctionCallerForLanguage( 78b9c1b51eSKate Stone Language(), return_type, impl_code_address, arg_value_list, name.c_str(), 79151c032cSJim Ingham error)); 80b9c1b51eSKate Stone if (error.Fail()) { 81151c032cSJim Ingham 82151c032cSJim Ingham return nullptr; 83151c032cSJim Ingham } 84b9c1b51eSKate Stone if (m_caller_up) { 85579e70c9SSean Callanan DiagnosticManager diagnostics; 86579e70c9SSean Callanan 87b9c1b51eSKate Stone unsigned num_errors = 88b9c1b51eSKate Stone m_caller_up->CompileFunction(thread_to_use_sp, diagnostics); 89b9c1b51eSKate Stone if (num_errors) { 90b9c1b51eSKate Stone error.SetErrorStringWithFormat( 91b9c1b51eSKate Stone "Error compiling %s caller function: \"%s\".", 92b9c1b51eSKate Stone m_function_name.c_str(), diagnostics.GetString().c_str()); 93151c032cSJim Ingham m_caller_up.reset(); 94151c032cSJim Ingham return nullptr; 95151c032cSJim Ingham } 96151c032cSJim Ingham 97579e70c9SSean Callanan diagnostics.Clear(); 98151c032cSJim Ingham ExecutionContext exe_ctx(process_sp); 99151c032cSJim Ingham 100b9c1b51eSKate Stone if (!m_caller_up->WriteFunctionWrapper(exe_ctx, diagnostics)) { 101b9c1b51eSKate Stone error.SetErrorStringWithFormat( 102b9c1b51eSKate Stone "Error inserting caller function for %s: \"%s\".", 103b9c1b51eSKate Stone m_function_name.c_str(), diagnostics.GetString().c_str()); 104151c032cSJim Ingham m_caller_up.reset(); 105151c032cSJim Ingham return nullptr; 106151c032cSJim Ingham } 107151c032cSJim Ingham } 108151c032cSJim Ingham return m_caller_up.get(); 109151c032cSJim Ingham } 110