Alexander Shaposhnikov | 696bd63 | 2016-11-26 05:23:44 +0000 | [diff] [blame] | 1 | //===-- UtilityFunction.cpp -------------------------------------*- C++ -*-===// |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 9 | #include <stdio.h> |
| 10 | #if HAVE_SYS_TYPES_H |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 11 | #include <sys/types.h> |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 12 | #endif |
| 13 | |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 14 | |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 15 | #include "lldb/Core/Module.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 16 | #include "lldb/Core/StreamFile.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 17 | #include "lldb/Expression/DiagnosticManager.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 18 | #include "lldb/Expression/FunctionCaller.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 19 | #include "lldb/Expression/IRExecutionUnit.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 20 | #include "lldb/Expression/UtilityFunction.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 21 | #include "lldb/Host/Host.h" |
| 22 | #include "lldb/Target/ExecutionContext.h" |
Bruce Mitchener | 804f981 | 2015-10-07 17:22:54 +0000 | [diff] [blame] | 23 | #include "lldb/Target/Process.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 24 | #include "lldb/Target/Target.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 25 | #include "lldb/Utility/ConstString.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 26 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 27 | #include "lldb/Utility/Stream.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace lldb_private; |
| 30 | using 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 Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | UtilityFunction::UtilityFunction(ExecutionContextScope &exe_scope, |
| 42 | const char *text, const char *name) |
| 43 | : Expression(exe_scope), m_execution_unit_sp(), m_jit_module_wp(), |
Jim Ingham | ea401ec | 2019-03-06 22:43:25 +0000 | [diff] [blame^] | 44 | m_function_text(), |
| 45 | m_function_name(name) {} |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 46 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | UtilityFunction::~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 Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | // FIXME: We should check that every time this is called it is called with the |
| 57 | // same return type & arguments... |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 58 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | FunctionCaller *UtilityFunction::MakeFunctionCaller( |
| 60 | const CompilerType &return_type, const ValueList &arg_value_list, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 61 | lldb::ThreadSP thread_to_use_sp, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 62 | if (m_caller_up) |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 63 | return m_caller_up.get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | |
| 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 Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 108 | } |