15ffd83dbSDimitry Andric //===-- UtilityFunction.cpp -----------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
99dba64beSDimitry Andric #include "lldb/Host/Config.h"
109dba64beSDimitry Andric
11*5f7ddb14SDimitry Andric #include <cstdio>
120b57cec5SDimitry Andric #if HAVE_SYS_TYPES_H
130b57cec5SDimitry Andric #include <sys/types.h>
140b57cec5SDimitry Andric #endif
150b57cec5SDimitry Andric
160b57cec5SDimitry Andric
170b57cec5SDimitry Andric #include "lldb/Core/Module.h"
180b57cec5SDimitry Andric #include "lldb/Core/StreamFile.h"
190b57cec5SDimitry Andric #include "lldb/Expression/DiagnosticManager.h"
200b57cec5SDimitry Andric #include "lldb/Expression/FunctionCaller.h"
210b57cec5SDimitry Andric #include "lldb/Expression/IRExecutionUnit.h"
220b57cec5SDimitry Andric #include "lldb/Expression/UtilityFunction.h"
230b57cec5SDimitry Andric #include "lldb/Host/Host.h"
240b57cec5SDimitry Andric #include "lldb/Target/ExecutionContext.h"
250b57cec5SDimitry Andric #include "lldb/Target/Process.h"
260b57cec5SDimitry Andric #include "lldb/Target/Target.h"
270b57cec5SDimitry Andric #include "lldb/Utility/ConstString.h"
280b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
290b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
300b57cec5SDimitry Andric
310b57cec5SDimitry Andric using namespace lldb_private;
320b57cec5SDimitry Andric using namespace lldb;
330b57cec5SDimitry Andric
34480093f4SDimitry Andric char UtilityFunction::ID;
35480093f4SDimitry Andric
360b57cec5SDimitry Andric /// Constructor
370b57cec5SDimitry Andric ///
380b57cec5SDimitry Andric /// \param[in] text
390b57cec5SDimitry Andric /// The text of the function. Must be a full translation unit.
400b57cec5SDimitry Andric ///
410b57cec5SDimitry Andric /// \param[in] name
420b57cec5SDimitry Andric /// The name of the function, as used in the text.
UtilityFunction(ExecutionContextScope & exe_scope,std::string text,std::string name,bool enable_debugging)430b57cec5SDimitry Andric UtilityFunction::UtilityFunction(ExecutionContextScope &exe_scope,
44*5f7ddb14SDimitry Andric std::string text, std::string name,
45*5f7ddb14SDimitry Andric bool enable_debugging)
46480093f4SDimitry Andric : Expression(exe_scope), m_execution_unit_sp(), m_jit_module_wp(),
47af732203SDimitry Andric m_function_text(std::move(text)), m_function_name(std::move(name)) {}
480b57cec5SDimitry Andric
~UtilityFunction()490b57cec5SDimitry Andric UtilityFunction::~UtilityFunction() {
500b57cec5SDimitry Andric lldb::ProcessSP process_sp(m_jit_process_wp.lock());
510b57cec5SDimitry Andric if (process_sp) {
520b57cec5SDimitry Andric lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
530b57cec5SDimitry Andric if (jit_module_sp)
540b57cec5SDimitry Andric process_sp->GetTarget().GetImages().Remove(jit_module_sp);
550b57cec5SDimitry Andric }
560b57cec5SDimitry Andric }
570b57cec5SDimitry Andric
580b57cec5SDimitry Andric // FIXME: We should check that every time this is called it is called with the
590b57cec5SDimitry Andric // same return type & arguments...
600b57cec5SDimitry Andric
MakeFunctionCaller(const CompilerType & return_type,const ValueList & arg_value_list,lldb::ThreadSP thread_to_use_sp,Status & error)610b57cec5SDimitry Andric FunctionCaller *UtilityFunction::MakeFunctionCaller(
620b57cec5SDimitry Andric const CompilerType &return_type, const ValueList &arg_value_list,
630b57cec5SDimitry Andric lldb::ThreadSP thread_to_use_sp, Status &error) {
640b57cec5SDimitry Andric if (m_caller_up)
650b57cec5SDimitry Andric return m_caller_up.get();
660b57cec5SDimitry Andric
670b57cec5SDimitry Andric ProcessSP process_sp = m_jit_process_wp.lock();
680b57cec5SDimitry Andric if (!process_sp) {
690b57cec5SDimitry Andric error.SetErrorString("Can't make a function caller without a process.");
700b57cec5SDimitry Andric return nullptr;
710b57cec5SDimitry Andric }
720b57cec5SDimitry Andric
730b57cec5SDimitry Andric Address impl_code_address;
740b57cec5SDimitry Andric impl_code_address.SetOffset(StartAddress());
750b57cec5SDimitry Andric std::string name(m_function_name);
760b57cec5SDimitry Andric name.append("-caller");
770b57cec5SDimitry Andric
780b57cec5SDimitry Andric m_caller_up.reset(process_sp->GetTarget().GetFunctionCallerForLanguage(
790b57cec5SDimitry Andric Language(), return_type, impl_code_address, arg_value_list, name.c_str(),
800b57cec5SDimitry Andric error));
810b57cec5SDimitry Andric if (error.Fail()) {
820b57cec5SDimitry Andric
830b57cec5SDimitry Andric return nullptr;
840b57cec5SDimitry Andric }
850b57cec5SDimitry Andric if (m_caller_up) {
860b57cec5SDimitry Andric DiagnosticManager diagnostics;
870b57cec5SDimitry Andric
880b57cec5SDimitry Andric unsigned num_errors =
890b57cec5SDimitry Andric m_caller_up->CompileFunction(thread_to_use_sp, diagnostics);
900b57cec5SDimitry Andric if (num_errors) {
910b57cec5SDimitry Andric error.SetErrorStringWithFormat(
920b57cec5SDimitry Andric "Error compiling %s caller function: \"%s\".",
930b57cec5SDimitry Andric m_function_name.c_str(), diagnostics.GetString().c_str());
940b57cec5SDimitry Andric m_caller_up.reset();
950b57cec5SDimitry Andric return nullptr;
960b57cec5SDimitry Andric }
970b57cec5SDimitry Andric
980b57cec5SDimitry Andric diagnostics.Clear();
990b57cec5SDimitry Andric ExecutionContext exe_ctx(process_sp);
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andric if (!m_caller_up->WriteFunctionWrapper(exe_ctx, diagnostics)) {
1020b57cec5SDimitry Andric error.SetErrorStringWithFormat(
1030b57cec5SDimitry Andric "Error inserting caller function for %s: \"%s\".",
1040b57cec5SDimitry Andric m_function_name.c_str(), diagnostics.GetString().c_str());
1050b57cec5SDimitry Andric m_caller_up.reset();
1060b57cec5SDimitry Andric return nullptr;
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric }
1090b57cec5SDimitry Andric return m_caller_up.get();
1100b57cec5SDimitry Andric }
111