1 //===-- UtilityFunction.cpp -------------------------------------*- C++ -*-===// 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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include <stdio.h> 10 #if HAVE_SYS_TYPES_H 11 #include <sys/types.h> 12 #endif 13 14 15 #include "lldb/Core/Module.h" 16 #include "lldb/Core/StreamFile.h" 17 #include "lldb/Expression/DiagnosticManager.h" 18 #include "lldb/Expression/ExpressionSourceCode.h" 19 #include "lldb/Expression/FunctionCaller.h" 20 #include "lldb/Expression/IRExecutionUnit.h" 21 #include "lldb/Expression/UtilityFunction.h" 22 #include "lldb/Host/Host.h" 23 #include "lldb/Target/ExecutionContext.h" 24 #include "lldb/Target/Process.h" 25 #include "lldb/Target/Target.h" 26 #include "lldb/Utility/ConstString.h" 27 #include "lldb/Utility/Log.h" 28 #include "lldb/Utility/Stream.h" 29 30 using namespace lldb_private; 31 using namespace lldb; 32 33 //------------------------------------------------------------------ 34 /// Constructor 35 /// 36 /// @param[in] text 37 /// The text of the function. Must be a full translation unit. 38 /// 39 /// @param[in] name 40 /// The name of the function, as used in the text. 41 //------------------------------------------------------------------ 42 UtilityFunction::UtilityFunction(ExecutionContextScope &exe_scope, 43 const char *text, const char *name) 44 : Expression(exe_scope), m_execution_unit_sp(), m_jit_module_wp(), 45 m_function_text(ExpressionSourceCode::g_expression_prefix), 46 m_function_name(name) { 47 if (text && text[0]) 48 m_function_text.append(text); 49 } 50 51 UtilityFunction::~UtilityFunction() { 52 lldb::ProcessSP process_sp(m_jit_process_wp.lock()); 53 if (process_sp) { 54 lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock()); 55 if (jit_module_sp) 56 process_sp->GetTarget().GetImages().Remove(jit_module_sp); 57 } 58 } 59 60 // FIXME: We should check that every time this is called it is called with the 61 // same return type & arguments... 62 63 FunctionCaller *UtilityFunction::MakeFunctionCaller( 64 const CompilerType &return_type, const ValueList &arg_value_list, 65 lldb::ThreadSP thread_to_use_sp, Status &error) { 66 if (m_caller_up) 67 return m_caller_up.get(); 68 69 ProcessSP process_sp = m_jit_process_wp.lock(); 70 if (!process_sp) { 71 error.SetErrorString("Can't make a function caller without a process."); 72 return nullptr; 73 } 74 75 Address impl_code_address; 76 impl_code_address.SetOffset(StartAddress()); 77 std::string name(m_function_name); 78 name.append("-caller"); 79 80 m_caller_up.reset(process_sp->GetTarget().GetFunctionCallerForLanguage( 81 Language(), return_type, impl_code_address, arg_value_list, name.c_str(), 82 error)); 83 if (error.Fail()) { 84 85 return nullptr; 86 } 87 if (m_caller_up) { 88 DiagnosticManager diagnostics; 89 90 unsigned num_errors = 91 m_caller_up->CompileFunction(thread_to_use_sp, diagnostics); 92 if (num_errors) { 93 error.SetErrorStringWithFormat( 94 "Error compiling %s caller function: \"%s\".", 95 m_function_name.c_str(), diagnostics.GetString().c_str()); 96 m_caller_up.reset(); 97 return nullptr; 98 } 99 100 diagnostics.Clear(); 101 ExecutionContext exe_ctx(process_sp); 102 103 if (!m_caller_up->WriteFunctionWrapper(exe_ctx, diagnostics)) { 104 error.SetErrorStringWithFormat( 105 "Error inserting caller function for %s: \"%s\".", 106 m_function_name.c_str(), diagnostics.GetString().c_str()); 107 m_caller_up.reset(); 108 return nullptr; 109 } 110 } 111 return m_caller_up.get(); 112 } 113