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 ///
35*f05b42e9SAdrian Prantl /// \param[in] text
36151c032cSJim Ingham ///     The text of the function.  Must be a full translation unit.
37151c032cSJim Ingham ///
38*f05b42e9SAdrian 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,
42b9c1b51eSKate Stone                                  const char *text, const char *name)
43b9c1b51eSKate Stone     : Expression(exe_scope), m_execution_unit_sp(), m_jit_module_wp(),
44ea401ec7SJim Ingham       m_function_text(),
45ea401ec7SJim Ingham       m_function_name(name) {}
46151c032cSJim Ingham 
47b9c1b51eSKate Stone UtilityFunction::~UtilityFunction() {
48151c032cSJim Ingham   lldb::ProcessSP process_sp(m_jit_process_wp.lock());
49b9c1b51eSKate Stone   if (process_sp) {
50151c032cSJim Ingham     lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
51151c032cSJim Ingham     if (jit_module_sp)
52151c032cSJim Ingham       process_sp->GetTarget().GetImages().Remove(jit_module_sp);
53151c032cSJim Ingham   }
54151c032cSJim Ingham }
55151c032cSJim Ingham 
56b9c1b51eSKate Stone // FIXME: We should check that every time this is called it is called with the
57b9c1b51eSKate Stone // same return type & arguments...
58151c032cSJim Ingham 
59b9c1b51eSKate Stone FunctionCaller *UtilityFunction::MakeFunctionCaller(
60b9c1b51eSKate Stone     const CompilerType &return_type, const ValueList &arg_value_list,
6197206d57SZachary Turner     lldb::ThreadSP thread_to_use_sp, Status &error) {
62151c032cSJim Ingham   if (m_caller_up)
63151c032cSJim Ingham     return m_caller_up.get();
64151c032cSJim Ingham 
65151c032cSJim Ingham   ProcessSP process_sp = m_jit_process_wp.lock();
66b9c1b51eSKate Stone   if (!process_sp) {
67dae50bafSJim Ingham     error.SetErrorString("Can't make a function caller without a process.");
68151c032cSJim Ingham     return nullptr;
69dae50bafSJim Ingham   }
70151c032cSJim Ingham 
71151c032cSJim Ingham   Address impl_code_address;
72151c032cSJim Ingham   impl_code_address.SetOffset(StartAddress());
73151c032cSJim Ingham   std::string name(m_function_name);
74151c032cSJim Ingham   name.append("-caller");
75151c032cSJim Ingham 
76b9c1b51eSKate Stone   m_caller_up.reset(process_sp->GetTarget().GetFunctionCallerForLanguage(
77b9c1b51eSKate Stone       Language(), return_type, impl_code_address, arg_value_list, name.c_str(),
78151c032cSJim Ingham       error));
79b9c1b51eSKate Stone   if (error.Fail()) {
80151c032cSJim Ingham 
81151c032cSJim Ingham     return nullptr;
82151c032cSJim Ingham   }
83b9c1b51eSKate Stone   if (m_caller_up) {
84579e70c9SSean Callanan     DiagnosticManager diagnostics;
85579e70c9SSean Callanan 
86b9c1b51eSKate Stone     unsigned num_errors =
87b9c1b51eSKate Stone         m_caller_up->CompileFunction(thread_to_use_sp, diagnostics);
88b9c1b51eSKate Stone     if (num_errors) {
89b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
90b9c1b51eSKate Stone           "Error compiling %s caller function: \"%s\".",
91b9c1b51eSKate Stone           m_function_name.c_str(), diagnostics.GetString().c_str());
92151c032cSJim Ingham       m_caller_up.reset();
93151c032cSJim Ingham       return nullptr;
94151c032cSJim Ingham     }
95151c032cSJim Ingham 
96579e70c9SSean Callanan     diagnostics.Clear();
97151c032cSJim Ingham     ExecutionContext exe_ctx(process_sp);
98151c032cSJim Ingham 
99b9c1b51eSKate Stone     if (!m_caller_up->WriteFunctionWrapper(exe_ctx, diagnostics)) {
100b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
101b9c1b51eSKate Stone           "Error inserting caller function for %s: \"%s\".",
102b9c1b51eSKate Stone           m_function_name.c_str(), diagnostics.GetString().c_str());
103151c032cSJim Ingham       m_caller_up.reset();
104151c032cSJim Ingham       return nullptr;
105151c032cSJim Ingham     }
106151c032cSJim Ingham   }
107151c032cSJim Ingham   return m_caller_up.get();
108151c032cSJim Ingham }
109