blob: 8cf9de966c41da8cfb580eb61187944d6f6d6d9f [file] [log] [blame]
Alexander Shaposhnikov696bd632016-11-26 05:23:44 +00001//===-- UtilityFunction.cpp -------------------------------------*- C++ -*-===//
Jim Ingham151c0322015-09-15 21:13:50 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Jim Ingham151c0322015-09-15 21:13:50 +00006//
7//===----------------------------------------------------------------------===//
8
Jim Ingham151c0322015-09-15 21:13:50 +00009#include <stdio.h>
10#if HAVE_SYS_TYPES_H
Kate Stoneb9c1b512016-09-06 20:57:50 +000011#include <sys/types.h>
Jim Ingham151c0322015-09-15 21:13:50 +000012#endif
13
Jim Ingham151c0322015-09-15 21:13:50 +000014
Jim Ingham151c0322015-09-15 21:13:50 +000015#include "lldb/Core/Module.h"
Jim Ingham151c0322015-09-15 21:13:50 +000016#include "lldb/Core/StreamFile.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000017#include "lldb/Expression/DiagnosticManager.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000018#include "lldb/Expression/FunctionCaller.h"
Jim Ingham151c0322015-09-15 21:13:50 +000019#include "lldb/Expression/IRExecutionUnit.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000020#include "lldb/Expression/UtilityFunction.h"
Jim Ingham151c0322015-09-15 21:13:50 +000021#include "lldb/Host/Host.h"
22#include "lldb/Target/ExecutionContext.h"
Bruce Mitchener804f9812015-10-07 17:22:54 +000023#include "lldb/Target/Process.h"
Jim Ingham151c0322015-09-15 21:13:50 +000024#include "lldb/Target/Target.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000025#include "lldb/Utility/ConstString.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000026#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000027#include "lldb/Utility/Stream.h"
Jim Ingham151c0322015-09-15 21:13:50 +000028
29using namespace lldb_private;
30using namespace lldb;
31
32//------------------------------------------------------------------
33/// Constructor
34///
35/// @param[in] text
36/// The text of the function. Must be a full translation unit.
37///
38/// @param[in] name
39/// The name of the function, as used in the text.
40//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000041UtilityFunction::UtilityFunction(ExecutionContextScope &exe_scope,
42 const char *text, const char *name)
43 : Expression(exe_scope), m_execution_unit_sp(), m_jit_module_wp(),
Jim Inghamea401ec2019-03-06 22:43:25 +000044 m_function_text(),
45 m_function_name(name) {}
Jim Ingham151c0322015-09-15 21:13:50 +000046
Kate Stoneb9c1b512016-09-06 20:57:50 +000047UtilityFunction::~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 }
Jim Ingham151c0322015-09-15 21:13:50 +000054}
55
Kate Stoneb9c1b512016-09-06 20:57:50 +000056// FIXME: We should check that every time this is called it is called with the
57// same return type & arguments...
Jim Ingham151c0322015-09-15 21:13:50 +000058
Kate Stoneb9c1b512016-09-06 20:57:50 +000059FunctionCaller *UtilityFunction::MakeFunctionCaller(
60 const CompilerType &return_type, const ValueList &arg_value_list,
Zachary Turner97206d52017-05-12 04:51:55 +000061 lldb::ThreadSP thread_to_use_sp, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 if (m_caller_up)
Jim Ingham151c0322015-09-15 21:13:50 +000063 return m_caller_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +000064
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();
Jim Ingham151c0322015-09-15 21:13:50 +0000108}