1 //===-- UtilityFunction.cpp -------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
11 #include <sys/types.h>
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/StreamFile.h"
17 #include "lldb/Expression/DiagnosticManager.h"
18 #include "lldb/Expression/FunctionCaller.h"
19 #include "lldb/Expression/IRExecutionUnit.h"
20 #include "lldb/Expression/UtilityFunction.h"
21 #include "lldb/Host/Host.h"
22 #include "lldb/Target/ExecutionContext.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Utility/ConstString.h"
26 #include "lldb/Utility/Log.h"
27 #include "lldb/Utility/Stream.h"
29 using namespace lldb_private;
35 /// The text of the function. Must be a full translation unit.
38 /// The name of the function, as used in the text.
39 UtilityFunction::UtilityFunction(ExecutionContextScope &exe_scope,
40 const char *text, const char *name,
42 : Expression(exe_scope, kind),
43 m_execution_unit_sp(), m_jit_module_wp(),
45 m_function_name(name) {}
47 UtilityFunction::~UtilityFunction() {
48 lldb::ProcessSP process_sp(m_jit_process_wp.lock());
50 lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
52 process_sp->GetTarget().GetImages().Remove(jit_module_sp);
56 // FIXME: We should check that every time this is called it is called with the
57 // same return type & arguments...
59 FunctionCaller *UtilityFunction::MakeFunctionCaller(
60 const CompilerType &return_type, const ValueList &arg_value_list,
61 lldb::ThreadSP thread_to_use_sp, Status &error) {
63 return m_caller_up.get();
65 ProcessSP process_sp = m_jit_process_wp.lock();
67 error.SetErrorString("Can't make a function caller without a process.");
71 Address impl_code_address;
72 impl_code_address.SetOffset(StartAddress());
73 std::string name(m_function_name);
74 name.append("-caller");
76 m_caller_up.reset(process_sp->GetTarget().GetFunctionCallerForLanguage(
77 Language(), return_type, impl_code_address, arg_value_list, name.c_str(),
84 DiagnosticManager diagnostics;
87 m_caller_up->CompileFunction(thread_to_use_sp, diagnostics);
89 error.SetErrorStringWithFormat(
90 "Error compiling %s caller function: \"%s\".",
91 m_function_name.c_str(), diagnostics.GetString().c_str());
97 ExecutionContext exe_ctx(process_sp);
99 if (!m_caller_up->WriteFunctionWrapper(exe_ctx, diagnostics)) {
100 error.SetErrorStringWithFormat(
101 "Error inserting caller function for %s: \"%s\".",
102 m_function_name.c_str(), diagnostics.GetString().c_str());
107 return m_caller_up.get();