| Alexander Shaposhnikov | 696bd63 | 2016-11-26 05:23:44 +0000 | [diff] [blame] | 1 | //===-- ClangUtilityFunction.cpp ---------------------------------*- C++-*-===// |
| Sean Callanan | e71d553 | 2010-08-27 23:31:21 +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 |
| Sean Callanan | e71d553 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9 | #include "ClangUtilityFunction.h" |
| Sean Callanan | 4dbb271 | 2015-09-25 20:35:58 +0000 | [diff] [blame] | 10 | #include "ClangExpressionDeclMap.h" |
| 11 | #include "ClangExpressionParser.h" |
| Sean Callanan | 4dbb271 | 2015-09-25 20:35:58 +0000 | [diff] [blame] | 12 | |
| Sean Callanan | e71d553 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 13 | #include <stdio.h> |
| 14 | #if HAVE_SYS_TYPES_H |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 15 | #include <sys/types.h> |
| Sean Callanan | e71d553 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 16 | #endif |
| 17 | |
| Sean Callanan | e71d553 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 18 | |
| Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 19 | #include "lldb/Core/Module.h" |
| Greg Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 20 | #include "lldb/Core/StreamFile.h" |
| Greg Clayton | 399107a | 2013-02-13 23:57:48 +0000 | [diff] [blame] | 21 | #include "lldb/Expression/ExpressionSourceCode.h" |
| Greg Clayton | e01e07b | 2013-04-18 18:10:51 +0000 | [diff] [blame] | 22 | #include "lldb/Expression/IRExecutionUnit.h" |
| Sean Callanan | e71d553 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 23 | #include "lldb/Host/Host.h" |
| 24 | #include "lldb/Target/ExecutionContext.h" |
| 25 | #include "lldb/Target/Target.h" |
| Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 26 | #include "lldb/Utility/ConstString.h" |
| Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 27 | #include "lldb/Utility/Log.h" |
| Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 28 | #include "lldb/Utility/Stream.h" |
| Sean Callanan | e71d553 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace lldb_private; |
| 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 | ClangUtilityFunction::ClangUtilityFunction(ExecutionContextScope &exe_scope, |
| 42 | const char *text, const char *name) |
| 43 | : UtilityFunction(exe_scope, text, name) {} |
| Sean Callanan | e71d553 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 44 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 45 | ClangUtilityFunction::~ClangUtilityFunction() {} |
| Greg Clayton | 5573fde | 2010-09-24 23:07:41 +0000 | [diff] [blame] | 46 | |
| Sean Callanan | e71d553 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 47 | //------------------------------------------------------------------ |
| 48 | /// Install the utility function into a process |
| 49 | /// |
| Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 50 | /// @param[in] diagnostic_manager |
| 51 | /// A diagnostic manager to report errors and warnings to. |
| Sean Callanan | e71d553 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 52 | /// |
| 53 | /// @param[in] exe_ctx |
| 54 | /// The execution context to install the utility function to. |
| 55 | /// |
| 56 | /// @return |
| 57 | /// True on success (no errors); false otherwise. |
| 58 | //------------------------------------------------------------------ |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | bool ClangUtilityFunction::Install(DiagnosticManager &diagnostic_manager, |
| 60 | ExecutionContext &exe_ctx) { |
| 61 | if (m_jit_start_addr != LLDB_INVALID_ADDRESS) { |
| Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 62 | diagnostic_manager.PutString(eDiagnosticSeverityWarning, |
| 63 | "already installed"); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | return false; |
| 65 | } |
| 66 | |
| 67 | //////////////////////////////////// |
| 68 | // Set up the target and compiler |
| 69 | // |
| 70 | |
| 71 | Target *target = exe_ctx.GetTargetPtr(); |
| 72 | |
| 73 | if (!target) { |
| Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 74 | diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid target"); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 75 | return false; |
| 76 | } |
| 77 | |
| 78 | Process *process = exe_ctx.GetProcessPtr(); |
| 79 | |
| 80 | if (!process) { |
| Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 81 | diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid process"); |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | |
| 85 | ////////////////////////// |
| 86 | // Parse the expression |
| 87 | // |
| 88 | |
| 89 | bool keep_result_in_memory = false; |
| 90 | |
| 91 | ResetDeclMap(exe_ctx, keep_result_in_memory); |
| 92 | |
| 93 | if (!DeclMap()->WillParse(exe_ctx, NULL)) { |
| Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 94 | diagnostic_manager.PutString( |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 95 | eDiagnosticSeverityError, |
| 96 | "current process state is unsuitable for expression parsing"); |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | const bool generate_debug_info = true; |
| 101 | ClangExpressionParser parser(exe_ctx.GetBestExecutionContextScope(), *this, |
| 102 | generate_debug_info); |
| 103 | |
| 104 | unsigned num_errors = parser.Parse(diagnostic_manager); |
| 105 | |
| 106 | if (num_errors) { |
| 107 | ResetDeclMap(); |
| 108 | |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | ////////////////////////////////// |
| 113 | // JIT the output of the parser |
| 114 | // |
| 115 | |
| 116 | bool can_interpret = false; // should stay that way |
| 117 | |
| Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 118 | Status jit_error = parser.PrepareForExecution( |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx, |
| 120 | can_interpret, eExecutionPolicyAlways); |
| 121 | |
| 122 | if (m_jit_start_addr != LLDB_INVALID_ADDRESS) { |
| 123 | m_jit_process_wp = process->shared_from_this(); |
| 124 | if (parser.GetGenerateDebugInfo()) { |
| 125 | lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule()); |
| 126 | |
| 127 | if (jit_module_sp) { |
| 128 | ConstString const_func_name(FunctionName()); |
| 129 | FileSpec jit_file; |
| 130 | jit_file.GetFilename() = const_func_name; |
| 131 | jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString()); |
| 132 | m_jit_module_wp = jit_module_sp; |
| 133 | target->GetImages().Append(jit_module_sp); |
| 134 | } |
| Sean Callanan | 6961e87 | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 135 | } |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 136 | } |
| Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 137 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 138 | DeclMap()->DidParse(); |
| 139 | |
| 140 | ResetDeclMap(); |
| 141 | |
| 142 | if (jit_error.Success()) { |
| 143 | return true; |
| 144 | } else { |
| 145 | const char *error_cstr = jit_error.AsCString(); |
| 146 | if (error_cstr && error_cstr[0]) { |
| 147 | diagnostic_manager.Printf(eDiagnosticSeverityError, "%s", error_cstr); |
| 148 | } else { |
| Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 149 | diagnostic_manager.PutString(eDiagnosticSeverityError, |
| 150 | "expression can't be interpreted or run"); |
| Sean Callanan | e71d553 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 151 | } |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 152 | return false; |
| 153 | } |
| Sean Callanan | e71d553 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 156 | void ClangUtilityFunction::ClangUtilityFunctionHelper::ResetDeclMap( |
| 157 | ExecutionContext &exe_ctx, bool keep_result_in_memory) { |
| 158 | m_expr_decl_map_up.reset( |
| 159 | new ClangExpressionDeclMap(keep_result_in_memory, nullptr, exe_ctx)); |
| Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 160 | } |