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