1 //===-- UtilityFunction.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include <sys/types.h>
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/StreamFile.h"
18 #include "lldb/Expression/DiagnosticManager.h"
19 #include "lldb/Expression/ExpressionSourceCode.h"
20 #include "lldb/Expression/FunctionCaller.h"
21 #include "lldb/Expression/IRExecutionUnit.h"
22 #include "lldb/Expression/UtilityFunction.h"
23 #include "lldb/Host/Host.h"
24 #include "lldb/Target/ExecutionContext.h"
25 #include "lldb/Target/Process.h"
26 #include "lldb/Target/Target.h"
27 #include "lldb/Utility/ConstString.h"
28 #include "lldb/Utility/Log.h"
29 #include "lldb/Utility/Stream.h"
31 using namespace lldb_private;
34 //------------------------------------------------------------------
38 /// The text of the function. Must be a full translation unit.
41 /// The name of the function, as used in the text.
42 //------------------------------------------------------------------
43 UtilityFunction::UtilityFunction(ExecutionContextScope &exe_scope,
44 const char *text, const char *name)
45 : Expression(exe_scope), m_execution_unit_sp(), m_jit_module_wp(),
46 m_function_text(ExpressionSourceCode::g_expression_prefix),
47 m_function_name(name) {
49 m_function_text.append(text);
52 UtilityFunction::~UtilityFunction() {
53 lldb::ProcessSP process_sp(m_jit_process_wp.lock());
55 lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
57 process_sp->GetTarget().GetImages().Remove(jit_module_sp);
61 // FIXME: We should check that every time this is called it is called with the
62 // same return type & arguments...
64 FunctionCaller *UtilityFunction::MakeFunctionCaller(
65 const CompilerType &return_type, const ValueList &arg_value_list,
66 lldb::ThreadSP thread_to_use_sp, Status &error) {
68 return m_caller_up.get();
70 ProcessSP process_sp = m_jit_process_wp.lock();
72 error.SetErrorString("Can't make a function caller without a process.");
76 Address impl_code_address;
77 impl_code_address.SetOffset(StartAddress());
78 std::string name(m_function_name);
79 name.append("-caller");
81 m_caller_up.reset(process_sp->GetTarget().GetFunctionCallerForLanguage(
82 Language(), return_type, impl_code_address, arg_value_list, name.c_str(),
89 DiagnosticManager diagnostics;
92 m_caller_up->CompileFunction(thread_to_use_sp, diagnostics);
94 error.SetErrorStringWithFormat(
95 "Error compiling %s caller function: \"%s\".",
96 m_function_name.c_str(), diagnostics.GetString().c_str());
102 ExecutionContext exe_ctx(process_sp);
104 if (!m_caller_up->WriteFunctionWrapper(exe_ctx, diagnostics)) {
105 error.SetErrorStringWithFormat(
106 "Error inserting caller function for %s: \"%s\".",
107 m_function_name.c_str(), diagnostics.GetString().c_str());
112 return m_caller_up.get();