1696bd635SAlexander Shaposhnikov //===-- UtilityFunction.cpp -------------------------------------*- C++ -*-===//
2151c032cSJim Ingham //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6151c032cSJim Ingham //
7151c032cSJim Ingham //===----------------------------------------------------------------------===//
8151c032cSJim Ingham 
9151c032cSJim Ingham #include <stdio.h>
10151c032cSJim Ingham #if HAVE_SYS_TYPES_H
11151c032cSJim Ingham #include <sys/types.h>
12151c032cSJim Ingham #endif
13151c032cSJim Ingham 
14151c032cSJim Ingham 
15151c032cSJim Ingham #include "lldb/Core/Module.h"
16151c032cSJim Ingham #include "lldb/Core/StreamFile.h"
17579e70c9SSean Callanan #include "lldb/Expression/DiagnosticManager.h"
18151c032cSJim Ingham #include "lldb/Expression/ExpressionSourceCode.h"
19579e70c9SSean Callanan #include "lldb/Expression/FunctionCaller.h"
20151c032cSJim Ingham #include "lldb/Expression/IRExecutionUnit.h"
21579e70c9SSean Callanan #include "lldb/Expression/UtilityFunction.h"
22151c032cSJim Ingham #include "lldb/Host/Host.h"
23151c032cSJim Ingham #include "lldb/Target/ExecutionContext.h"
24804f981fSBruce Mitchener #include "lldb/Target/Process.h"
25151c032cSJim Ingham #include "lldb/Target/Target.h"
26bf9a7730SZachary Turner #include "lldb/Utility/ConstString.h"
276f9e6901SZachary Turner #include "lldb/Utility/Log.h"
28bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
29151c032cSJim Ingham 
30151c032cSJim Ingham using namespace lldb_private;
31151c032cSJim Ingham using namespace lldb;
32151c032cSJim Ingham 
33151c032cSJim Ingham //------------------------------------------------------------------
34151c032cSJim Ingham /// Constructor
35151c032cSJim Ingham ///
36151c032cSJim Ingham /// @param[in] text
37151c032cSJim Ingham ///     The text of the function.  Must be a full translation unit.
38151c032cSJim Ingham ///
39151c032cSJim Ingham /// @param[in] name
40151c032cSJim Ingham ///     The name of the function, as used in the text.
41151c032cSJim Ingham //------------------------------------------------------------------
42151c032cSJim Ingham UtilityFunction::UtilityFunction(ExecutionContextScope &exe_scope,
43b9c1b51eSKate Stone                                  const char *text, const char *name)
44b9c1b51eSKate Stone     : Expression(exe_scope), m_execution_unit_sp(), m_jit_module_wp(),
45151c032cSJim Ingham       m_function_text(ExpressionSourceCode::g_expression_prefix),
46b9c1b51eSKate Stone       m_function_name(name) {
47151c032cSJim Ingham   if (text && text[0])
48151c032cSJim Ingham     m_function_text.append(text);
49151c032cSJim Ingham }
50151c032cSJim Ingham 
51b9c1b51eSKate Stone UtilityFunction::~UtilityFunction() {
52151c032cSJim Ingham   lldb::ProcessSP process_sp(m_jit_process_wp.lock());
53b9c1b51eSKate Stone   if (process_sp) {
54151c032cSJim Ingham     lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
55151c032cSJim Ingham     if (jit_module_sp)
56151c032cSJim Ingham       process_sp->GetTarget().GetImages().Remove(jit_module_sp);
57151c032cSJim Ingham   }
58151c032cSJim Ingham }
59151c032cSJim Ingham 
60b9c1b51eSKate Stone // FIXME: We should check that every time this is called it is called with the
61b9c1b51eSKate Stone // same return type & arguments...
62151c032cSJim Ingham 
63b9c1b51eSKate Stone FunctionCaller *UtilityFunction::MakeFunctionCaller(
64b9c1b51eSKate Stone     const CompilerType &return_type, const ValueList &arg_value_list,
6597206d57SZachary Turner     lldb::ThreadSP thread_to_use_sp, Status &error) {
66151c032cSJim Ingham   if (m_caller_up)
67151c032cSJim Ingham     return m_caller_up.get();
68151c032cSJim Ingham 
69151c032cSJim Ingham   ProcessSP process_sp = m_jit_process_wp.lock();
70b9c1b51eSKate Stone   if (!process_sp) {
71dae50bafSJim Ingham     error.SetErrorString("Can't make a function caller without a process.");
72151c032cSJim Ingham     return nullptr;
73dae50bafSJim Ingham   }
74151c032cSJim Ingham 
75151c032cSJim Ingham   Address impl_code_address;
76151c032cSJim Ingham   impl_code_address.SetOffset(StartAddress());
77151c032cSJim Ingham   std::string name(m_function_name);
78151c032cSJim Ingham   name.append("-caller");
79151c032cSJim Ingham 
80b9c1b51eSKate Stone   m_caller_up.reset(process_sp->GetTarget().GetFunctionCallerForLanguage(
81b9c1b51eSKate Stone       Language(), return_type, impl_code_address, arg_value_list, name.c_str(),
82151c032cSJim Ingham       error));
83b9c1b51eSKate Stone   if (error.Fail()) {
84151c032cSJim Ingham 
85151c032cSJim Ingham     return nullptr;
86151c032cSJim Ingham   }
87b9c1b51eSKate Stone   if (m_caller_up) {
88579e70c9SSean Callanan     DiagnosticManager diagnostics;
89579e70c9SSean Callanan 
90b9c1b51eSKate Stone     unsigned num_errors =
91b9c1b51eSKate Stone         m_caller_up->CompileFunction(thread_to_use_sp, diagnostics);
92b9c1b51eSKate Stone     if (num_errors) {
93b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
94b9c1b51eSKate Stone           "Error compiling %s caller function: \"%s\".",
95b9c1b51eSKate Stone           m_function_name.c_str(), diagnostics.GetString().c_str());
96151c032cSJim Ingham       m_caller_up.reset();
97151c032cSJim Ingham       return nullptr;
98151c032cSJim Ingham     }
99151c032cSJim Ingham 
100579e70c9SSean Callanan     diagnostics.Clear();
101151c032cSJim Ingham     ExecutionContext exe_ctx(process_sp);
102151c032cSJim Ingham 
103b9c1b51eSKate Stone     if (!m_caller_up->WriteFunctionWrapper(exe_ctx, diagnostics)) {
104b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
105b9c1b51eSKate Stone           "Error inserting caller function for %s: \"%s\".",
106b9c1b51eSKate Stone           m_function_name.c_str(), diagnostics.GetString().c_str());
107151c032cSJim Ingham       m_caller_up.reset();
108151c032cSJim Ingham       return nullptr;
109151c032cSJim Ingham     }
110151c032cSJim Ingham   }
111151c032cSJim Ingham   return m_caller_up.get();
112151c032cSJim Ingham }
113