Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 1 | //===-- ClangUserExpression.cpp -------------------------------------*- C++ -*-===// |
| 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 |
| 13 | # include <sys/types.h> |
| 14 | #endif |
| 15 | |
| 16 | // C++ Includes |
| 17 | |
| 18 | #include "lldb/Core/ConstString.h" |
| 19 | #include "lldb/Core/Log.h" |
| 20 | #include "lldb/Core/Module.h" |
| 21 | #include "lldb/Core/Stream.h" |
| 22 | #include "lldb/Core/StreamFile.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame^] | 23 | #include "lldb/Expression/DiagnosticManager.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 24 | #include "lldb/Expression/ExpressionSourceCode.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame^] | 25 | #include "lldb/Expression/FunctionCaller.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 26 | #include "lldb/Expression/IRExecutionUnit.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame^] | 27 | #include "lldb/Expression/UtilityFunction.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 28 | #include "lldb/Host/Host.h" |
| 29 | #include "lldb/Target/ExecutionContext.h" |
Bruce Mitchener | 804f981 | 2015-10-07 17:22:54 +0000 | [diff] [blame] | 30 | #include "lldb/Target/Process.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 31 | #include "lldb/Target/Target.h" |
| 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 | //------------------------------------------------------------------ |
| 45 | UtilityFunction::UtilityFunction (ExecutionContextScope &exe_scope, |
| 46 | const char *text, |
| 47 | const char *name) : |
| 48 | Expression (exe_scope), |
| 49 | m_execution_unit_sp (), |
| 50 | m_jit_module_wp (), |
| 51 | m_function_text (ExpressionSourceCode::g_expression_prefix), |
| 52 | m_function_name (name) |
| 53 | { |
| 54 | if (text && text[0]) |
| 55 | m_function_text.append (text); |
| 56 | } |
| 57 | |
| 58 | UtilityFunction::~UtilityFunction () |
| 59 | { |
| 60 | lldb::ProcessSP process_sp (m_jit_process_wp.lock()); |
| 61 | if (process_sp) |
| 62 | { |
| 63 | lldb::ModuleSP jit_module_sp (m_jit_module_wp.lock()); |
| 64 | if (jit_module_sp) |
| 65 | process_sp->GetTarget().GetImages().Remove(jit_module_sp); |
| 66 | } |
| 67 | |
| 68 | } |
| 69 | |
| 70 | // FIXME: We should check that every time this is called it is called with the same return type & arguments... |
| 71 | |
| 72 | FunctionCaller * |
| 73 | UtilityFunction::MakeFunctionCaller (const CompilerType &return_type, const ValueList &arg_value_list, Error &error) |
| 74 | { |
| 75 | if (m_caller_up) |
| 76 | return m_caller_up.get(); |
| 77 | |
| 78 | ProcessSP process_sp = m_jit_process_wp.lock(); |
| 79 | if (!process_sp) |
| 80 | return nullptr; |
| 81 | |
| 82 | Address impl_code_address; |
| 83 | impl_code_address.SetOffset(StartAddress()); |
| 84 | std::string name(m_function_name); |
| 85 | name.append("-caller"); |
| 86 | |
| 87 | m_caller_up.reset (process_sp->GetTarget().GetFunctionCallerForLanguage (Language(), |
| 88 | return_type, |
| 89 | impl_code_address, |
| 90 | arg_value_list, |
| 91 | name.c_str(), |
| 92 | error)); |
| 93 | if (error.Fail()) |
| 94 | { |
| 95 | |
| 96 | return nullptr; |
| 97 | } |
| 98 | if (m_caller_up) |
| 99 | { |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame^] | 100 | DiagnosticManager diagnostics; |
| 101 | |
| 102 | unsigned num_errors = m_caller_up->CompileFunction(diagnostics); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 103 | if (num_errors) |
| 104 | { |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame^] | 105 | error.SetErrorStringWithFormat("Error compiling %s caller function: \"%s\".", m_function_name.c_str(), |
| 106 | diagnostics.GetString().c_str()); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 107 | m_caller_up.reset(); |
| 108 | return nullptr; |
| 109 | } |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame^] | 110 | |
| 111 | diagnostics.Clear(); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 112 | ExecutionContext exe_ctx(process_sp); |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame^] | 113 | |
| 114 | if (!m_caller_up->WriteFunctionWrapper(exe_ctx, diagnostics)) |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 115 | { |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame^] | 116 | error.SetErrorStringWithFormat("Error inserting caller function for %s: \"%s\".", m_function_name.c_str(), |
| 117 | diagnostics.GetString().c_str()); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 118 | m_caller_up.reset(); |
| 119 | return nullptr; |
| 120 | } |
| 121 | } |
| 122 | return m_caller_up.get(); |
| 123 | } |