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 <cstdio>
10#include <sys/types.h>
11
12#include "lldb/Core/Module.h"
17#include "lldb/Host/Host.h"
19#include "lldb/Target/Process.h"
20#include "lldb/Target/Target.h"
22#include "lldb/Utility/Log.h"
23#include "lldb/Utility/State.h"
24#include "lldb/Utility/Stream.h"
25
26using namespace lldb_private;
27using namespace lldb;
28
30
31/// Constructor
32///
33/// \param[in] text
34/// The text of the function. Must be a full translation unit.
35///
36/// \param[in] name
37/// The name of the function, as used in the text.
39 std::string text, std::string name,
40 bool enable_debugging)
41 : Expression(exe_scope), m_execution_unit_sp(), m_jit_module_wp(),
42 m_function_text(std::move(text)), m_function_name(std::move(name)) {}
43
45 lldb::ProcessSP process_sp(m_jit_process_wp.lock());
46 if (process_sp) {
47 lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock());
48 if (jit_module_sp)
49 process_sp->GetTarget().GetImages().Remove(jit_module_sp);
50 }
51}
52
53// FIXME: We should check that every time this is called it is called with the
54// same return type & arguments...
55
57 const CompilerType &return_type, const ValueList &arg_value_list,
58 lldb::ThreadSP thread_to_use_sp, Status &error) {
59 if (m_caller_up)
60 return m_caller_up.get();
61
62 ProcessSP process_sp = m_jit_process_wp.lock();
63 if (!process_sp) {
65 "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) {
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().AsLanguageType(), return_type, impl_code_address,
85 arg_value_list, name.c_str(), error));
86 if (error.Fail())
87 return nullptr;
88
89 if (m_caller_up) {
90 DiagnosticManager diagnostics;
91
92 unsigned num_errors =
93 m_caller_up->CompileFunction(thread_to_use_sp, diagnostics);
94 if (num_errors) {
97 "Error compiling " + m_function_name + " caller function:"));
98
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 = Status::FromError(diagnostics.GetAsError(
109 "Error inserting " + m_function_name + " caller function:"));
110 m_caller_up.reset();
111 return nullptr;
112 }
113 }
114 return m_caller_up.get();
115}
static llvm::raw_ostream & error(Stream &strm)
A section + offset based address class.
Definition: Address.h:62
bool SetOffset(lldb::addr_t offset)
Set accessor for the offset.
Definition: Address.h:448
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
llvm::Error GetAsError(lldb::ExpressionResults result, llvm::Twine message={}) const
Returns an ExpressionError with arg as error code.
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
Encapsulates a single expression for use in lldb.
Definition: Expression.h:31
lldb::ProcessWP m_jit_process_wp
Expression's always have to have a target...
Definition: Expression.h:88
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:73
Encapsulates a function that can be called.
An error handling class.
Definition: Status.h:115
static Status FromErrorString(const char *str)
Definition: Status.h:138
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
Definition: Status.h:148
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
Definition: Status.cpp:137
UtilityFunction(ExecutionContextScope &exe_scope, std::string text, std::string name, bool enable_debugging)
Constructor.
FunctionCaller * MakeFunctionCaller(const CompilerType &return_type, const ValueList &arg_value_list, lldb::ThreadSP compilation_thread, Status &error)
std::string m_function_name
The name of the function.
std::unique_ptr< FunctionCaller > m_caller_up
A class that represents a running process on the host machine.
const char * StateAsCString(lldb::StateType state)
Converts a StateType to a C string.
Definition: State.cpp:14
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::Thread > ThreadSP
Definition: lldb-forward.h:450
@ eStateStopped
Process or thread is stopped and can be examined.
@ eExpressionParseError
@ eExpressionSetupError
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:389
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:373