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