1*696bd635SAlexander Shaposhnikov //===-- UtilityFunction.cpp -------------------------------------*- C++ -*-===// 2151c032cSJim Ingham // 3151c032cSJim Ingham // The LLVM Compiler Infrastructure 4151c032cSJim Ingham // 5151c032cSJim Ingham // This file is distributed under the University of Illinois Open Source 6151c032cSJim Ingham // License. See LICENSE.TXT for details. 7151c032cSJim Ingham // 8151c032cSJim Ingham //===----------------------------------------------------------------------===// 9151c032cSJim Ingham 10151c032cSJim Ingham // C Includes 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 // C++ Includes 17151c032cSJim Ingham 18151c032cSJim Ingham #include "lldb/Core/ConstString.h" 19151c032cSJim Ingham #include "lldb/Core/Log.h" 20151c032cSJim Ingham #include "lldb/Core/Module.h" 21151c032cSJim Ingham #include "lldb/Core/Stream.h" 22151c032cSJim Ingham #include "lldb/Core/StreamFile.h" 23579e70c9SSean Callanan #include "lldb/Expression/DiagnosticManager.h" 24151c032cSJim Ingham #include "lldb/Expression/ExpressionSourceCode.h" 25579e70c9SSean Callanan #include "lldb/Expression/FunctionCaller.h" 26151c032cSJim Ingham #include "lldb/Expression/IRExecutionUnit.h" 27579e70c9SSean Callanan #include "lldb/Expression/UtilityFunction.h" 28151c032cSJim Ingham #include "lldb/Host/Host.h" 29151c032cSJim Ingham #include "lldb/Target/ExecutionContext.h" 30804f981fSBruce Mitchener #include "lldb/Target/Process.h" 31151c032cSJim Ingham #include "lldb/Target/Target.h" 32151c032cSJim Ingham 33151c032cSJim Ingham using namespace lldb_private; 34151c032cSJim Ingham using namespace lldb; 35151c032cSJim Ingham 36151c032cSJim Ingham //------------------------------------------------------------------ 37151c032cSJim Ingham /// Constructor 38151c032cSJim Ingham /// 39151c032cSJim Ingham /// @param[in] text 40151c032cSJim Ingham /// The text of the function. Must be a full translation unit. 41151c032cSJim Ingham /// 42151c032cSJim Ingham /// @param[in] name 43151c032cSJim Ingham /// The name of the function, as used in the text. 44151c032cSJim Ingham //------------------------------------------------------------------ 45151c032cSJim Ingham UtilityFunction::UtilityFunction(ExecutionContextScope &exe_scope, 46b9c1b51eSKate Stone const char *text, const char *name) 47b9c1b51eSKate Stone : Expression(exe_scope), m_execution_unit_sp(), m_jit_module_wp(), 48151c032cSJim Ingham m_function_text(ExpressionSourceCode::g_expression_prefix), 49b9c1b51eSKate Stone m_function_name(name) { 50151c032cSJim Ingham if (text && text[0]) 51151c032cSJim Ingham m_function_text.append(text); 52151c032cSJim Ingham } 53151c032cSJim Ingham 54b9c1b51eSKate Stone UtilityFunction::~UtilityFunction() { 55151c032cSJim Ingham lldb::ProcessSP process_sp(m_jit_process_wp.lock()); 56b9c1b51eSKate Stone if (process_sp) { 57151c032cSJim Ingham lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock()); 58151c032cSJim Ingham if (jit_module_sp) 59151c032cSJim Ingham process_sp->GetTarget().GetImages().Remove(jit_module_sp); 60151c032cSJim Ingham } 61151c032cSJim Ingham } 62151c032cSJim Ingham 63b9c1b51eSKate Stone // FIXME: We should check that every time this is called it is called with the 64b9c1b51eSKate Stone // same return type & arguments... 65151c032cSJim Ingham 66b9c1b51eSKate Stone FunctionCaller *UtilityFunction::MakeFunctionCaller( 67b9c1b51eSKate Stone const CompilerType &return_type, const ValueList &arg_value_list, 68b9c1b51eSKate Stone lldb::ThreadSP thread_to_use_sp, Error &error) { 69151c032cSJim Ingham if (m_caller_up) 70151c032cSJim Ingham return m_caller_up.get(); 71151c032cSJim Ingham 72151c032cSJim Ingham ProcessSP process_sp = m_jit_process_wp.lock(); 73b9c1b51eSKate Stone if (!process_sp) { 74dae50bafSJim Ingham error.SetErrorString("Can't make a function caller without a process."); 75151c032cSJim Ingham return nullptr; 76dae50bafSJim Ingham } 77151c032cSJim Ingham 78151c032cSJim Ingham Address impl_code_address; 79151c032cSJim Ingham impl_code_address.SetOffset(StartAddress()); 80151c032cSJim Ingham std::string name(m_function_name); 81151c032cSJim Ingham name.append("-caller"); 82151c032cSJim Ingham 83b9c1b51eSKate Stone m_caller_up.reset(process_sp->GetTarget().GetFunctionCallerForLanguage( 84b9c1b51eSKate Stone Language(), return_type, impl_code_address, arg_value_list, name.c_str(), 85151c032cSJim Ingham error)); 86b9c1b51eSKate Stone if (error.Fail()) { 87151c032cSJim Ingham 88151c032cSJim Ingham return nullptr; 89151c032cSJim Ingham } 90b9c1b51eSKate Stone if (m_caller_up) { 91579e70c9SSean Callanan DiagnosticManager diagnostics; 92579e70c9SSean Callanan 93b9c1b51eSKate Stone unsigned num_errors = 94b9c1b51eSKate Stone m_caller_up->CompileFunction(thread_to_use_sp, diagnostics); 95b9c1b51eSKate Stone if (num_errors) { 96b9c1b51eSKate Stone error.SetErrorStringWithFormat( 97b9c1b51eSKate Stone "Error compiling %s caller function: \"%s\".", 98b9c1b51eSKate Stone m_function_name.c_str(), diagnostics.GetString().c_str()); 99151c032cSJim Ingham m_caller_up.reset(); 100151c032cSJim Ingham return nullptr; 101151c032cSJim Ingham } 102151c032cSJim Ingham 103579e70c9SSean Callanan diagnostics.Clear(); 104151c032cSJim Ingham ExecutionContext exe_ctx(process_sp); 105151c032cSJim Ingham 106b9c1b51eSKate Stone if (!m_caller_up->WriteFunctionWrapper(exe_ctx, diagnostics)) { 107b9c1b51eSKate Stone error.SetErrorStringWithFormat( 108b9c1b51eSKate Stone "Error inserting caller function for %s: \"%s\".", 109b9c1b51eSKate Stone m_function_name.c_str(), diagnostics.GetString().c_str()); 110151c032cSJim Ingham m_caller_up.reset(); 111151c032cSJim Ingham return nullptr; 112151c032cSJim Ingham } 113151c032cSJim Ingham } 114151c032cSJim Ingham return m_caller_up.get(); 115151c032cSJim Ingham } 116