xref: /freebsd/contrib/llvm-project/lldb/source/Expression/UtilityFunction.cpp (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
1 //===-- UtilityFunction.cpp -----------------------------------------------===//
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 <cstdio>
10 #include <sys/types.h>
11 
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/StreamFile.h"
14 #include "lldb/Expression/DiagnosticManager.h"
15 #include "lldb/Expression/FunctionCaller.h"
16 #include "lldb/Expression/IRExecutionUnit.h"
17 #include "lldb/Expression/UtilityFunction.h"
18 #include "lldb/Host/Host.h"
19 #include "lldb/Target/ExecutionContext.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Utility/ConstString.h"
23 #include "lldb/Utility/Log.h"
24 #include "lldb/Utility/State.h"
25 #include "lldb/Utility/Stream.h"
26 
27 using namespace lldb_private;
28 using namespace lldb;
29 
30 char UtilityFunction::ID;
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                                  std::string text, std::string name,
41                                  bool enable_debugging)
42     : Expression(exe_scope), m_execution_unit_sp(), m_jit_module_wp(),
43       m_function_text(std::move(text)), m_function_name(std::move(name)) {}
44 
45 UtilityFunction::~UtilityFunction() {
46   lldb::ProcessSP process_sp(m_jit_process_wp.lock());
47   if (process_sp) {
48     lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
49     if (jit_module_sp)
50       process_sp->GetTarget().GetImages().Remove(jit_module_sp);
51   }
52 }
53 
54 // FIXME: We should check that every time this is called it is called with the
55 // same return type & arguments...
56 
57 FunctionCaller *UtilityFunction::MakeFunctionCaller(
58     const CompilerType &return_type, const ValueList &arg_value_list,
59     lldb::ThreadSP thread_to_use_sp, Status &error) {
60   if (m_caller_up)
61     return m_caller_up.get();
62 
63   ProcessSP process_sp = m_jit_process_wp.lock();
64   if (!process_sp) {
65     error.SetErrorString("Can't make a function caller without a process.");
66     return nullptr;
67   }
68   // Since we might need to allocate memory and maybe call code to make
69   // the caller, we need to be stopped.
70   if (process_sp->GetState() != lldb::eStateStopped) {
71     error.SetErrorStringWithFormatv(
72         "Can't make a function caller while the process is {0}: the process "
73         "must be stopped to allocate memory.",
74         StateAsCString(process_sp->GetState()));
75     return nullptr;
76   }
77 
78   Address impl_code_address;
79   impl_code_address.SetOffset(StartAddress());
80   std::string name(m_function_name);
81   name.append("-caller");
82 
83   m_caller_up.reset(process_sp->GetTarget().GetFunctionCallerForLanguage(
84       Language(), return_type, impl_code_address, arg_value_list, name.c_str(),
85       error));
86   if (error.Fail()) {
87 
88     return nullptr;
89   }
90   if (m_caller_up) {
91     DiagnosticManager diagnostics;
92 
93     unsigned num_errors =
94         m_caller_up->CompileFunction(thread_to_use_sp, diagnostics);
95     if (num_errors) {
96       error.SetErrorStringWithFormat(
97           "Error compiling %s caller function: \"%s\".",
98           m_function_name.c_str(), diagnostics.GetString().c_str());
99       m_caller_up.reset();
100       return nullptr;
101     }
102 
103     diagnostics.Clear();
104     ExecutionContext exe_ctx(process_sp);
105 
106     if (!m_caller_up->WriteFunctionWrapper(exe_ctx, diagnostics)) {
107       error.SetErrorStringWithFormat(
108           "Error inserting caller function for %s: \"%s\".",
109           m_function_name.c_str(), diagnostics.GetString().c_str());
110       m_caller_up.reset();
111       return nullptr;
112     }
113   }
114   return m_caller_up.get();
115 }
116