LLDB  mainline
UtilityFunction.cpp
Go to the documentation of this file.
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 "lldb/Host/Config.h"
10 
11 #include <stdio.h>
12 #if HAVE_SYS_TYPES_H
13 #include <sys/types.h>
14 #endif
15 
16 
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/StreamFile.h"
23 #include "lldb/Host/Host.h"
25 #include "lldb/Target/Process.h"
26 #include "lldb/Target/Target.h"
28 #include "lldb/Utility/Log.h"
29 #include "lldb/Utility/Stream.h"
30 
31 using namespace lldb_private;
32 using namespace lldb;
33 
35 
36 /// Constructor
37 ///
38 /// \param[in] text
39 /// The text of the function. Must be a full translation unit.
40 ///
41 /// \param[in] name
42 /// The name of the function, as used in the text.
44  std::string text, std::string name,
45  bool enable_debugging)
46  : Expression(exe_scope), m_execution_unit_sp(), m_jit_module_wp(),
47  m_function_text(std::move(text)), m_function_name(std::move(name)) {}
48 
50  lldb::ProcessSP process_sp(m_jit_process_wp.lock());
51  if (process_sp) {
52  lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
53  if (jit_module_sp)
54  process_sp->GetTarget().GetImages().Remove(jit_module_sp);
55  }
56 }
57 
58 // FIXME: We should check that every time this is called it is called with the
59 // same return type & arguments...
60 
62  const CompilerType &return_type, const ValueList &arg_value_list,
63  lldb::ThreadSP thread_to_use_sp, Status &error) {
64  if (m_caller_up)
65  return m_caller_up.get();
66 
67  ProcessSP process_sp = m_jit_process_wp.lock();
68  if (!process_sp) {
69  error.SetErrorString("Can't make a function caller without a process.");
70  return nullptr;
71  }
72 
73  Address impl_code_address;
74  impl_code_address.SetOffset(StartAddress());
76  name.append("-caller");
77 
78  m_caller_up.reset(process_sp->GetTarget().GetFunctionCallerForLanguage(
79  Language(), return_type, impl_code_address, arg_value_list, name.c_str(),
80  error));
81  if (error.Fail()) {
82 
83  return nullptr;
84  }
85  if (m_caller_up) {
86  DiagnosticManager diagnostics;
87 
88  unsigned num_errors =
89  m_caller_up->CompileFunction(thread_to_use_sp, diagnostics);
90  if (num_errors) {
91  error.SetErrorStringWithFormat(
92  "Error compiling %s caller function: \"%s\".",
93  m_function_name.c_str(), diagnostics.GetString().c_str());
94  m_caller_up.reset();
95  return nullptr;
96  }
97 
98  diagnostics.Clear();
99  ExecutionContext exe_ctx(process_sp);
100 
101  if (!m_caller_up->WriteFunctionWrapper(exe_ctx, diagnostics)) {
102  error.SetErrorStringWithFormat(
103  "Error inserting caller function for %s: \"%s\".",
104  m_function_name.c_str(), diagnostics.GetString().c_str());
105  m_caller_up.reset();
106  return nullptr;
107  }
108  }
109  return m_caller_up.get();
110 }
lldb_private::ExecutionContext
Definition: ExecutionContext.h:291
FunctionCaller.h
lldb_private::UtilityFunction::m_jit_module_wp
lldb::ModuleWP m_jit_module_wp
Definition: UtilityFunction.h:115
lldb_private::UtilityFunction::m_function_name
std::string m_function_name
The name of the function.
Definition: UtilityFunction.h:119
Host.h
lldb_private::Expression::m_jit_process_wp
lldb::ProcessWP m_jit_process_wp
Expression's always have to have a target...
Definition: Expression.h:93
Module.h
lldb_private::ValueList
Definition: Value.h:157
lldb_private::DiagnosticManager::GetString
std::string GetString(char separator='\n')
Definition: DiagnosticManager.cpp:47
Process.h
Target.h
lldb_private::UtilityFunction::MakeFunctionCaller
FunctionCaller * MakeFunctionCaller(const CompilerType &return_type, const ValueList &arg_value_list, lldb::ThreadSP compilation_thread, Status &error)
Definition: UtilityFunction.cpp:61
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
Log.h
lldb_private::ExecutionContextScope
Definition: ExecutionContextScope.h:32
lldb_private::UtilityFunction::UtilityFunction
UtilityFunction(ExecutionContextScope &exe_scope, std::string text, std::string name, bool enable_debugging)
Constructor.
Definition: UtilityFunction.cpp:43
IRExecutionUnit.h
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
UtilityFunction.h
lldb_private::UtilityFunction::~UtilityFunction
~UtilityFunction() override
Definition: UtilityFunction.cpp:49
StreamFile.h
lldb_private::Status
Definition: Status.h:44
lldb_private::UtilityFunction::m_caller_up
std::unique_ptr< FunctionCaller > m_caller_up
Definition: UtilityFunction.h:120
lldb_private::Language
Definition: Language.h:29
lldb_private::Address
Definition: Address.h:59
lldb_private::Expression::StartAddress
lldb::addr_t StartAddress()
Return the address of the function's JIT-compiled code, or LLDB_INVALID_ADDRESS if the function is no...
Definition: Expression.h:78
lldb_private::DiagnosticManager
Definition: DiagnosticManager.h:93
lldb_private::Address::SetOffset
bool SetOffset(lldb::addr_t offset)
Set accessor for the offset.
Definition: Address.h:425
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:33
DiagnosticManager.h
lldb_private::DiagnosticManager::Clear
void Clear()
Definition: DiagnosticManager.h:95
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
ConstString.h
Stream.h
lldb
Definition: SBAddress.h:15
lldb_private::Expression
Definition: Expression.h:33
ExecutionContext.h
lldb_private::UtilityFunction::ID
static char ID
Definition: UtilityFunction.h:32
lldb_private::FunctionCaller
Definition: FunctionCaller.h:56