1696bd635SAlexander Shaposhnikov //===-- UtilityFunction.cpp -------------------------------------*- C++ -*-===//
2151c032cSJim Ingham //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler 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"
18579e70c9SSean Callanan #include "lldb/Expression/FunctionCaller.h"
19151c032cSJim Ingham #include "lldb/Expression/IRExecutionUnit.h"
20579e70c9SSean Callanan #include "lldb/Expression/UtilityFunction.h"
21151c032cSJim Ingham #include "lldb/Host/Host.h"
22151c032cSJim Ingham #include "lldb/Target/ExecutionContext.h"
23804f981fSBruce Mitchener #include "lldb/Target/Process.h"
24151c032cSJim Ingham #include "lldb/Target/Target.h"
25bf9a7730SZachary Turner #include "lldb/Utility/ConstString.h"
266f9e6901SZachary Turner #include "lldb/Utility/Log.h"
27bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
28151c032cSJim Ingham 
29151c032cSJim Ingham using namespace lldb_private;
30151c032cSJim Ingham using namespace lldb;
31151c032cSJim Ingham 
32151c032cSJim Ingham //------------------------------------------------------------------
33151c032cSJim Ingham /// Constructor
34151c032cSJim Ingham ///
35f05b42e9SAdrian Prantl /// \param[in] text
36151c032cSJim Ingham ///     The text of the function.  Must be a full translation unit.
37151c032cSJim Ingham ///
38f05b42e9SAdrian Prantl /// \param[in] name
39151c032cSJim Ingham ///     The name of the function, as used in the text.
40151c032cSJim Ingham //------------------------------------------------------------------
41151c032cSJim Ingham UtilityFunction::UtilityFunction(ExecutionContextScope &exe_scope,
42*7e34d78dSAdrian Prantl                                  const char *text, const char *name,
43*7e34d78dSAdrian Prantl                                  ExpressionKind kind)
44*7e34d78dSAdrian Prantl     : Expression(exe_scope, kind),
45*7e34d78dSAdrian Prantl       m_execution_unit_sp(), m_jit_module_wp(),
46ea401ec7SJim Ingham       m_function_text(),
47ea401ec7SJim Ingham       m_function_name(name) {}
48151c032cSJim Ingham 
49b9c1b51eSKate Stone UtilityFunction::~UtilityFunction() {
50151c032cSJim Ingham   lldb::ProcessSP process_sp(m_jit_process_wp.lock());
51b9c1b51eSKate Stone   if (process_sp) {
52151c032cSJim Ingham     lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
53151c032cSJim Ingham     if (jit_module_sp)
54151c032cSJim Ingham       process_sp->GetTarget().GetImages().Remove(jit_module_sp);
55151c032cSJim Ingham   }
56151c032cSJim Ingham }
57151c032cSJim Ingham 
58b9c1b51eSKate Stone // FIXME: We should check that every time this is called it is called with the
59b9c1b51eSKate Stone // same return type & arguments...
60151c032cSJim Ingham 
61b9c1b51eSKate Stone FunctionCaller *UtilityFunction::MakeFunctionCaller(
62b9c1b51eSKate Stone     const CompilerType &return_type, const ValueList &arg_value_list,
6397206d57SZachary Turner     lldb::ThreadSP thread_to_use_sp, Status &error) {
64151c032cSJim Ingham   if (m_caller_up)
65151c032cSJim Ingham     return m_caller_up.get();
66151c032cSJim Ingham 
67151c032cSJim Ingham   ProcessSP process_sp = m_jit_process_wp.lock();
68b9c1b51eSKate Stone   if (!process_sp) {
69dae50bafSJim Ingham     error.SetErrorString("Can't make a function caller without a process.");
70151c032cSJim Ingham     return nullptr;
71dae50bafSJim Ingham   }
72151c032cSJim Ingham 
73151c032cSJim Ingham   Address impl_code_address;
74151c032cSJim Ingham   impl_code_address.SetOffset(StartAddress());
75151c032cSJim Ingham   std::string name(m_function_name);
76151c032cSJim Ingham   name.append("-caller");
77151c032cSJim Ingham 
78b9c1b51eSKate Stone   m_caller_up.reset(process_sp->GetTarget().GetFunctionCallerForLanguage(
79b9c1b51eSKate Stone       Language(), return_type, impl_code_address, arg_value_list, name.c_str(),
80151c032cSJim Ingham       error));
81b9c1b51eSKate Stone   if (error.Fail()) {
82151c032cSJim Ingham 
83151c032cSJim Ingham     return nullptr;
84151c032cSJim Ingham   }
85b9c1b51eSKate Stone   if (m_caller_up) {
86579e70c9SSean Callanan     DiagnosticManager diagnostics;
87579e70c9SSean Callanan 
88b9c1b51eSKate Stone     unsigned num_errors =
89b9c1b51eSKate Stone         m_caller_up->CompileFunction(thread_to_use_sp, diagnostics);
90b9c1b51eSKate Stone     if (num_errors) {
91b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
92b9c1b51eSKate Stone           "Error compiling %s caller function: \"%s\".",
93b9c1b51eSKate Stone           m_function_name.c_str(), diagnostics.GetString().c_str());
94151c032cSJim Ingham       m_caller_up.reset();
95151c032cSJim Ingham       return nullptr;
96151c032cSJim Ingham     }
97151c032cSJim Ingham 
98579e70c9SSean Callanan     diagnostics.Clear();
99151c032cSJim Ingham     ExecutionContext exe_ctx(process_sp);
100151c032cSJim Ingham 
101b9c1b51eSKate Stone     if (!m_caller_up->WriteFunctionWrapper(exe_ctx, diagnostics)) {
102b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
103b9c1b51eSKate Stone           "Error inserting caller function for %s: \"%s\".",
104b9c1b51eSKate Stone           m_function_name.c_str(), diagnostics.GetString().c_str());
105151c032cSJim Ingham       m_caller_up.reset();
106151c032cSJim Ingham       return nullptr;
107151c032cSJim Ingham     }
108151c032cSJim Ingham   }
109151c032cSJim Ingham   return m_caller_up.get();
110151c032cSJim Ingham }
111