xref: /freebsd/contrib/llvm-project/lldb/source/Expression/UtilityFunction.cpp (revision 7029da5c36f2d3cf6bb6c81bf551229f416399e8)
1 //===-- UtilityFunction.cpp -------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <stdio.h>
10 #if HAVE_SYS_TYPES_H
11 #include <sys/types.h>
12 #endif
13 
14 
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/StreamFile.h"
17 #include "lldb/Expression/DiagnosticManager.h"
18 #include "lldb/Expression/FunctionCaller.h"
19 #include "lldb/Expression/IRExecutionUnit.h"
20 #include "lldb/Expression/UtilityFunction.h"
21 #include "lldb/Host/Host.h"
22 #include "lldb/Target/ExecutionContext.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Utility/ConstString.h"
26 #include "lldb/Utility/Log.h"
27 #include "lldb/Utility/Stream.h"
28 
29 using namespace lldb_private;
30 using namespace lldb;
31 
32 /// Constructor
33 ///
34 /// \param[in] text
35 ///     The text of the function.  Must be a full translation unit.
36 ///
37 /// \param[in] name
38 ///     The name of the function, as used in the text.
39 UtilityFunction::UtilityFunction(ExecutionContextScope &exe_scope,
40                                  const char *text, const char *name,
41                                  ExpressionKind kind)
42     : Expression(exe_scope, kind),
43       m_execution_unit_sp(), m_jit_module_wp(),
44       m_function_text(),
45       m_function_name(name) {}
46 
47 UtilityFunction::~UtilityFunction() {
48   lldb::ProcessSP process_sp(m_jit_process_wp.lock());
49   if (process_sp) {
50     lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
51     if (jit_module_sp)
52       process_sp->GetTarget().GetImages().Remove(jit_module_sp);
53   }
54 }
55 
56 // FIXME: We should check that every time this is called it is called with the
57 // same return type & arguments...
58 
59 FunctionCaller *UtilityFunction::MakeFunctionCaller(
60     const CompilerType &return_type, const ValueList &arg_value_list,
61     lldb::ThreadSP thread_to_use_sp, Status &error) {
62   if (m_caller_up)
63     return m_caller_up.get();
64 
65   ProcessSP process_sp = m_jit_process_wp.lock();
66   if (!process_sp) {
67     error.SetErrorString("Can't make a function caller without a process.");
68     return nullptr;
69   }
70 
71   Address impl_code_address;
72   impl_code_address.SetOffset(StartAddress());
73   std::string name(m_function_name);
74   name.append("-caller");
75 
76   m_caller_up.reset(process_sp->GetTarget().GetFunctionCallerForLanguage(
77       Language(), return_type, impl_code_address, arg_value_list, name.c_str(),
78       error));
79   if (error.Fail()) {
80 
81     return nullptr;
82   }
83   if (m_caller_up) {
84     DiagnosticManager diagnostics;
85 
86     unsigned num_errors =
87         m_caller_up->CompileFunction(thread_to_use_sp, diagnostics);
88     if (num_errors) {
89       error.SetErrorStringWithFormat(
90           "Error compiling %s caller function: \"%s\".",
91           m_function_name.c_str(), diagnostics.GetString().c_str());
92       m_caller_up.reset();
93       return nullptr;
94     }
95 
96     diagnostics.Clear();
97     ExecutionContext exe_ctx(process_sp);
98 
99     if (!m_caller_up->WriteFunctionWrapper(exe_ctx, diagnostics)) {
100       error.SetErrorStringWithFormat(
101           "Error inserting caller function for %s: \"%s\".",
102           m_function_name.c_str(), diagnostics.GetString().c_str());
103       m_caller_up.reset();
104       return nullptr;
105     }
106   }
107   return m_caller_up.get();
108 }
109