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 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #if HAVE_SYS_TYPES_H |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 12 | #include <sys/types.h> |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 13 | #endif |
| 14 | |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 15 | |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 16 | #include "lldb/Core/Module.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 17 | #include "lldb/Core/StreamFile.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 18 | #include "lldb/Expression/DiagnosticManager.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 19 | #include "lldb/Expression/ExpressionSourceCode.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 20 | #include "lldb/Expression/FunctionCaller.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 21 | #include "lldb/Expression/IRExecutionUnit.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 22 | #include "lldb/Expression/UtilityFunction.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 23 | #include "lldb/Host/Host.h" |
| 24 | #include "lldb/Target/ExecutionContext.h" |
Bruce Mitchener | 804f981 | 2015-10-07 17:22:54 +0000 | [diff] [blame] | 25 | #include "lldb/Target/Process.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 26 | #include "lldb/Target/Target.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 27 | #include "lldb/Utility/ConstString.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 28 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 29 | #include "lldb/Utility/Stream.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace lldb_private; |
| 32 | using namespace lldb; |
| 33 | |
| 34 | //------------------------------------------------------------------ |
| 35 | /// Constructor |
| 36 | /// |
| 37 | /// @param[in] text |
| 38 | /// The text of the function. Must be a full translation unit. |
| 39 | /// |
| 40 | /// @param[in] name |
| 41 | /// The name of the function, as used in the text. |
| 42 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 43 | UtilityFunction::UtilityFunction(ExecutionContextScope &exe_scope, |
| 44 | const char *text, const char *name) |
| 45 | : Expression(exe_scope), m_execution_unit_sp(), m_jit_module_wp(), |
| 46 | m_function_text(ExpressionSourceCode::g_expression_prefix), |
| 47 | m_function_name(name) { |
| 48 | if (text && text[0]) |
| 49 | m_function_text.append(text); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 52 | UtilityFunction::~UtilityFunction() { |
| 53 | lldb::ProcessSP process_sp(m_jit_process_wp.lock()); |
| 54 | if (process_sp) { |
| 55 | lldb::ModuleSP jit_module_sp(m_jit_module_wp.lock()); |
| 56 | if (jit_module_sp) |
| 57 | process_sp->GetTarget().GetImages().Remove(jit_module_sp); |
| 58 | } |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | // FIXME: We should check that every time this is called it is called with the |
| 62 | // same return type & arguments... |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 63 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | FunctionCaller *UtilityFunction::MakeFunctionCaller( |
| 65 | const CompilerType &return_type, const ValueList &arg_value_list, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 66 | lldb::ThreadSP thread_to_use_sp, Status &error) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | if (m_caller_up) |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 68 | return m_caller_up.get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | |
| 70 | ProcessSP process_sp = m_jit_process_wp.lock(); |
| 71 | if (!process_sp) { |
| 72 | error.SetErrorString("Can't make a function caller without a process."); |
| 73 | return nullptr; |
| 74 | } |
| 75 | |
| 76 | Address impl_code_address; |
| 77 | impl_code_address.SetOffset(StartAddress()); |
| 78 | std::string name(m_function_name); |
| 79 | name.append("-caller"); |
| 80 | |
| 81 | m_caller_up.reset(process_sp->GetTarget().GetFunctionCallerForLanguage( |
| 82 | Language(), return_type, impl_code_address, arg_value_list, name.c_str(), |
| 83 | error)); |
| 84 | if (error.Fail()) { |
| 85 | |
| 86 | return nullptr; |
| 87 | } |
| 88 | if (m_caller_up) { |
| 89 | DiagnosticManager diagnostics; |
| 90 | |
| 91 | unsigned num_errors = |
| 92 | m_caller_up->CompileFunction(thread_to_use_sp, diagnostics); |
| 93 | if (num_errors) { |
| 94 | error.SetErrorStringWithFormat( |
| 95 | "Error compiling %s caller function: \"%s\".", |
| 96 | m_function_name.c_str(), diagnostics.GetString().c_str()); |
| 97 | m_caller_up.reset(); |
| 98 | return nullptr; |
| 99 | } |
| 100 | |
| 101 | diagnostics.Clear(); |
| 102 | ExecutionContext exe_ctx(process_sp); |
| 103 | |
| 104 | if (!m_caller_up->WriteFunctionWrapper(exe_ctx, diagnostics)) { |
| 105 | error.SetErrorStringWithFormat( |
| 106 | "Error inserting caller function for %s: \"%s\".", |
| 107 | m_function_name.c_str(), diagnostics.GetString().c_str()); |
| 108 | m_caller_up.reset(); |
| 109 | return nullptr; |
| 110 | } |
| 111 | } |
| 112 | return m_caller_up.get(); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 113 | } |