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