180814287SRaphael Isemann //===-- UtilityFunction.cpp -----------------------------------------------===//
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 
9*76e47d48SRaphael Isemann #include <cstdio>
10151c032cSJim Ingham #include <sys/types.h>
11151c032cSJim Ingham 
12151c032cSJim Ingham #include "lldb/Core/Module.h"
13151c032cSJim Ingham #include "lldb/Core/StreamFile.h"
14579e70c9SSean Callanan #include "lldb/Expression/DiagnosticManager.h"
15579e70c9SSean Callanan #include "lldb/Expression/FunctionCaller.h"
16151c032cSJim Ingham #include "lldb/Expression/IRExecutionUnit.h"
17579e70c9SSean Callanan #include "lldb/Expression/UtilityFunction.h"
18151c032cSJim Ingham #include "lldb/Host/Host.h"
19151c032cSJim Ingham #include "lldb/Target/ExecutionContext.h"
20804f981fSBruce Mitchener #include "lldb/Target/Process.h"
21151c032cSJim Ingham #include "lldb/Target/Target.h"
22bf9a7730SZachary Turner #include "lldb/Utility/ConstString.h"
236f9e6901SZachary Turner #include "lldb/Utility/Log.h"
24bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
25151c032cSJim Ingham 
26151c032cSJim Ingham using namespace lldb_private;
27151c032cSJim Ingham using namespace lldb;
28151c032cSJim Ingham 
2952f3a2faSRaphael Isemann char UtilityFunction::ID;
3052f3a2faSRaphael Isemann 
31151c032cSJim Ingham /// Constructor
32151c032cSJim Ingham ///
33f05b42e9SAdrian Prantl /// \param[in] text
34151c032cSJim Ingham ///     The text of the function.  Must be a full translation unit.
35151c032cSJim Ingham ///
36f05b42e9SAdrian Prantl /// \param[in] name
37151c032cSJim Ingham ///     The name of the function, as used in the text.
UtilityFunction(ExecutionContextScope & exe_scope,std::string text,std::string name,bool enable_debugging)38151c032cSJim Ingham UtilityFunction::UtilityFunction(ExecutionContextScope &exe_scope,
3938dfb235SJonas Devlieghere                                  std::string text, std::string name,
4038dfb235SJonas Devlieghere                                  bool enable_debugging)
4152f3a2faSRaphael Isemann     : Expression(exe_scope), m_execution_unit_sp(), m_jit_module_wp(),
42a00acbabSJonas Devlieghere       m_function_text(std::move(text)), m_function_name(std::move(name)) {}
43151c032cSJim Ingham 
~UtilityFunction()44b9c1b51eSKate Stone UtilityFunction::~UtilityFunction() {
45151c032cSJim Ingham   lldb::ProcessSP process_sp(m_jit_process_wp.lock());
46b9c1b51eSKate Stone   if (process_sp) {
47151c032cSJim Ingham     lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
48151c032cSJim Ingham     if (jit_module_sp)
49151c032cSJim Ingham       process_sp->GetTarget().GetImages().Remove(jit_module_sp);
50151c032cSJim Ingham   }
51151c032cSJim Ingham }
52151c032cSJim Ingham 
53b9c1b51eSKate Stone // FIXME: We should check that every time this is called it is called with the
54b9c1b51eSKate Stone // same return type & arguments...
55151c032cSJim Ingham 
MakeFunctionCaller(const CompilerType & return_type,const ValueList & arg_value_list,lldb::ThreadSP thread_to_use_sp,Status & error)56b9c1b51eSKate Stone FunctionCaller *UtilityFunction::MakeFunctionCaller(
57b9c1b51eSKate Stone     const CompilerType &return_type, const ValueList &arg_value_list,
5897206d57SZachary Turner     lldb::ThreadSP thread_to_use_sp, Status &error) {
59151c032cSJim Ingham   if (m_caller_up)
60151c032cSJim Ingham     return m_caller_up.get();
61151c032cSJim Ingham 
62151c032cSJim Ingham   ProcessSP process_sp = m_jit_process_wp.lock();
63b9c1b51eSKate Stone   if (!process_sp) {
64dae50bafSJim Ingham     error.SetErrorString("Can't make a function caller without a process.");
65151c032cSJim Ingham     return nullptr;
66dae50bafSJim Ingham   }
67151c032cSJim Ingham 
68151c032cSJim Ingham   Address impl_code_address;
69151c032cSJim Ingham   impl_code_address.SetOffset(StartAddress());
70151c032cSJim Ingham   std::string name(m_function_name);
71151c032cSJim Ingham   name.append("-caller");
72151c032cSJim Ingham 
73b9c1b51eSKate Stone   m_caller_up.reset(process_sp->GetTarget().GetFunctionCallerForLanguage(
74b9c1b51eSKate Stone       Language(), return_type, impl_code_address, arg_value_list, name.c_str(),
75151c032cSJim Ingham       error));
76b9c1b51eSKate Stone   if (error.Fail()) {
77151c032cSJim Ingham 
78151c032cSJim Ingham     return nullptr;
79151c032cSJim Ingham   }
80b9c1b51eSKate Stone   if (m_caller_up) {
81579e70c9SSean Callanan     DiagnosticManager diagnostics;
82579e70c9SSean Callanan 
83b9c1b51eSKate Stone     unsigned num_errors =
84b9c1b51eSKate Stone         m_caller_up->CompileFunction(thread_to_use_sp, diagnostics);
85b9c1b51eSKate Stone     if (num_errors) {
86b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
87b9c1b51eSKate Stone           "Error compiling %s caller function: \"%s\".",
88b9c1b51eSKate Stone           m_function_name.c_str(), diagnostics.GetString().c_str());
89151c032cSJim Ingham       m_caller_up.reset();
90151c032cSJim Ingham       return nullptr;
91151c032cSJim Ingham     }
92151c032cSJim Ingham 
93579e70c9SSean Callanan     diagnostics.Clear();
94151c032cSJim Ingham     ExecutionContext exe_ctx(process_sp);
95151c032cSJim Ingham 
96b9c1b51eSKate Stone     if (!m_caller_up->WriteFunctionWrapper(exe_ctx, diagnostics)) {
97b9c1b51eSKate Stone       error.SetErrorStringWithFormat(
98b9c1b51eSKate Stone           "Error inserting caller function for %s: \"%s\".",
99b9c1b51eSKate Stone           m_function_name.c_str(), diagnostics.GetString().c_str());
100151c032cSJim Ingham       m_caller_up.reset();
101151c032cSJim Ingham       return nullptr;
102151c032cSJim Ingham     }
103151c032cSJim Ingham   }
104151c032cSJim Ingham   return m_caller_up.get();
105151c032cSJim Ingham }
106