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