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  const char *text, const char *name)
45  : Expression(exe_scope), m_execution_unit_sp(), m_jit_module_wp(),
46  m_function_text(), m_function_name(name) {}
47 
49  lldb::ProcessSP process_sp(m_jit_process_wp.lock());
50  if (process_sp) {
51  lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
52  if (jit_module_sp)
53  process_sp->GetTarget().GetImages().Remove(jit_module_sp);
54  }
55 }
56 
57 // FIXME: We should check that every time this is called it is called with the
58 // same return type & arguments...
59 
61  const CompilerType &return_type, const ValueList &arg_value_list,
62  lldb::ThreadSP thread_to_use_sp, Status &error) {
63  if (m_caller_up)
64  return m_caller_up.get();
65 
66  ProcessSP process_sp = m_jit_process_wp.lock();
67  if (!process_sp) {
68  error.SetErrorString("Can't make a function caller without a process.");
69  return nullptr;
70  }
71 
72  Address impl_code_address;
73  impl_code_address.SetOffset(StartAddress());
74  std::string name(m_function_name);
75  name.append("-caller");
76 
77  m_caller_up.reset(process_sp->GetTarget().GetFunctionCallerForLanguage(
78  Language(), return_type, impl_code_address, arg_value_list, name.c_str(),
79  error));
80  if (error.Fail()) {
81 
82  return nullptr;
83  }
84  if (m_caller_up) {
85  DiagnosticManager diagnostics;
86 
87  unsigned num_errors =
88  m_caller_up->CompileFunction(thread_to_use_sp, diagnostics);
89  if (num_errors) {
91  "Error compiling %s caller function: \"%s\".",
92  m_function_name.c_str(), diagnostics.GetString().c_str());
93  m_caller_up.reset();
94  return nullptr;
95  }
96 
97  diagnostics.Clear();
98  ExecutionContext exe_ctx(process_sp);
99 
100  if (!m_caller_up->WriteFunctionWrapper(exe_ctx, diagnostics)) {
102  "Error inserting caller function for %s: \"%s\".",
103  m_function_name.c_str(), diagnostics.GetString().c_str());
104  m_caller_up.reset();
105  return nullptr;
106  }
107  }
108  return m_caller_up.get();
109 }
A class that represents a running process on the host machine.
virtual lldb::LanguageType Language() const
Return the language that should be used when parsing.
Definition: Expression.h:54
Encapsulates a single expression for use in lldb.
Definition: Expression.h:33
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
bool SetOffset(lldb::addr_t offset)
Set accessor for the offset.
Definition: Address.h:425
UtilityFunction(ExecutionContextScope &exe_scope, const char *text, const char *name)
Constructor.
static llvm::raw_ostream & error(Stream &strm)
std::string m_function_name
The name of the function.
FunctionCaller * MakeFunctionCaller(const CompilerType &return_type, const ValueList &arg_value_list, lldb::ThreadSP compilation_thread, Status &error)
Encapsulates a function that can be called.
lldb::addr_t StartAddress()
Return the address of the function&#39;s JIT-compiled code, or LLDB_INVALID_ADDRESS if the function is no...
Definition: Expression.h:78
std::string GetString(char separator='\n')
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:242
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
lldb::ProcessWP m_jit_process_wp
Expression&#39;s always have to have a target...
Definition: Expression.h:93
A section + offset based address class.
Definition: Address.h:59
bool Fail() const
Test for error condition.
Definition: Status.cpp:182
Definition: SBAddress.h:15
Represents a generic type in a programming language.
Definition: CompilerType.h:33
int SetErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Set the current error string to a formatted error string.
Definition: Status.cpp:256
std::unique_ptr< FunctionCaller > m_caller_up
An error handling class.
Definition: Status.h:44