Sean Callanan | c049274 | 2011-05-23 21:40:23 +0000 | [diff] [blame] | 1 | //===-- ClangUserExpression.cpp ---------------------------------*- C++ -*-===// |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +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 |
| 13 | # include <sys/types.h> |
| 14 | #endif |
| 15 | |
| 16 | // C++ Includes |
| 17 | #include <cstdlib> |
| 18 | #include <string> |
| 19 | #include <map> |
| 20 | |
| 21 | #include "lldb/Core/ConstString.h" |
| 22 | #include "lldb/Core/Log.h" |
Greg Clayton | c71899e | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 23 | #include "lldb/Core/StreamFile.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 24 | #include "lldb/Core/StreamString.h" |
Greg Clayton | d171972 | 2010-10-05 03:13:51 +0000 | [diff] [blame] | 25 | #include "lldb/Core/ValueObjectConstResult.h" |
Sean Callanan | 05a5a1b | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 26 | #include "lldb/Expression/ASTResultSynthesizer.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 27 | #include "lldb/Expression/ClangExpressionDeclMap.h" |
| 28 | #include "lldb/Expression/ClangExpressionParser.h" |
| 29 | #include "lldb/Expression/ClangFunction.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 30 | #include "lldb/Expression/ClangUserExpression.h" |
Sean Callanan | de3d27e | 2011-09-26 18:45:31 +0000 | [diff] [blame] | 31 | #include "lldb/Expression/ExpressionSourceCode.h" |
Sean Callanan | a6686e3 | 2013-04-16 23:25:35 +0000 | [diff] [blame] | 32 | #include "lldb/Expression/IRExecutionUnit.h" |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 33 | #include "lldb/Expression/IRInterpreter.h" |
Sean Callanan | 3b16eb9 | 2013-04-11 00:09:05 +0000 | [diff] [blame] | 34 | #include "lldb/Expression/Materializer.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 35 | #include "lldb/Host/Host.h" |
Greg Clayton | 49ce896 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 36 | #include "lldb/Symbol/Block.h" |
Jim Ingham | c7f17c0 | 2012-10-30 23:35:54 +0000 | [diff] [blame] | 37 | #include "lldb/Symbol/ClangASTContext.h" |
| 38 | #include "lldb/Symbol/Function.h" |
| 39 | #include "lldb/Symbol/Type.h" |
| 40 | #include "lldb/Symbol/ClangExternalASTSourceCommon.h" |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 41 | #include "lldb/Symbol/VariableList.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 42 | #include "lldb/Target/ExecutionContext.h" |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 43 | #include "lldb/Target/Process.h" |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 44 | #include "lldb/Target/StackFrame.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 45 | #include "lldb/Target/Target.h" |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 46 | #include "lldb/Target/ThreadPlan.h" |
| 47 | #include "lldb/Target/ThreadPlanCallUserExpression.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 48 | |
Sean Callanan | c617a4c | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 49 | #include "clang/AST/DeclCXX.h" |
| 50 | #include "clang/AST/DeclObjC.h" |
| 51 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 52 | using namespace lldb_private; |
| 53 | |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 54 | ClangUserExpression::ClangUserExpression (const char *expr, |
Sean Callanan | 5b658cc | 2011-11-07 23:35:40 +0000 | [diff] [blame] | 55 | const char *expr_prefix, |
Sean Callanan | daa6efe | 2011-12-21 22:22:58 +0000 | [diff] [blame] | 56 | lldb::LanguageType language, |
| 57 | ResultType desired_type) : |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 58 | ClangExpression (), |
Sean Callanan | cdc3ea5 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 59 | m_stack_frame_bottom (LLDB_INVALID_ADDRESS), |
| 60 | m_stack_frame_top (LLDB_INVALID_ADDRESS), |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 61 | m_expr_text (expr), |
| 62 | m_expr_prefix (expr_prefix ? expr_prefix : ""), |
Sean Callanan | 5b658cc | 2011-11-07 23:35:40 +0000 | [diff] [blame] | 63 | m_language (language), |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 64 | m_transformed_text (), |
Sean Callanan | daa6efe | 2011-12-21 22:22:58 +0000 | [diff] [blame] | 65 | m_desired_type (desired_type), |
Sean Callanan | 77dd4a4 | 2012-12-06 01:35:38 +0000 | [diff] [blame] | 66 | m_enforce_valid_object (true), |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 67 | m_cplusplus (false), |
| 68 | m_objectivec (false), |
Bill Wendling | d787da4 | 2012-04-03 08:46:13 +0000 | [diff] [blame] | 69 | m_static_method(false), |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 70 | m_needs_object_ptr (false), |
Sean Callanan | 696cf5f | 2011-05-07 01:06:41 +0000 | [diff] [blame] | 71 | m_const_object (false), |
Daniel Dunbar | 97c8957 | 2011-10-31 22:50:49 +0000 | [diff] [blame] | 72 | m_target (NULL), |
Sean Callanan | cdc3ea5 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 73 | m_can_interpret (false), |
| 74 | m_materialized_address (LLDB_INVALID_ADDRESS) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 75 | { |
Sean Callanan | 5b658cc | 2011-11-07 23:35:40 +0000 | [diff] [blame] | 76 | switch (m_language) |
| 77 | { |
| 78 | case lldb::eLanguageTypeC_plus_plus: |
| 79 | m_allow_cxx = true; |
| 80 | break; |
| 81 | case lldb::eLanguageTypeObjC: |
| 82 | m_allow_objc = true; |
| 83 | break; |
| 84 | case lldb::eLanguageTypeObjC_plus_plus: |
| 85 | default: |
| 86 | m_allow_cxx = true; |
| 87 | m_allow_objc = true; |
| 88 | break; |
| 89 | } |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 92 | ClangUserExpression::~ClangUserExpression () |
| 93 | { |
| 94 | } |
| 95 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 96 | clang::ASTConsumer * |
| 97 | ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough) |
Sean Callanan | 036fa90 | 2011-08-24 22:18:12 +0000 | [diff] [blame] | 98 | { |
Sean Callanan | 2431244 | 2011-08-23 21:20:51 +0000 | [diff] [blame] | 99 | ClangASTContext *clang_ast_context = m_target->GetScratchClangASTContext(); |
| 100 | |
| 101 | if (!clang_ast_context) |
| 102 | return NULL; |
| 103 | |
Sean Callanan | 060d53f | 2011-10-08 00:21:35 +0000 | [diff] [blame] | 104 | if (!m_result_synthesizer.get()) |
| 105 | m_result_synthesizer.reset(new ASTResultSynthesizer(passthrough, |
Sean Callanan | e1301a6 | 2011-12-06 03:41:14 +0000 | [diff] [blame] | 106 | *m_target)); |
Sean Callanan | 060d53f | 2011-10-08 00:21:35 +0000 | [diff] [blame] | 107 | |
| 108 | return m_result_synthesizer.get(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 111 | void |
Sean Callanan | fa9e6dd | 2011-11-04 02:09:33 +0000 | [diff] [blame] | 112 | ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Error &err) |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 113 | { |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 114 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 5474125 | 2012-12-01 00:08:33 +0000 | [diff] [blame] | 115 | |
| 116 | if (log) |
| 117 | log->Printf("ClangUserExpression::ScanContext()"); |
| 118 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 119 | m_target = exe_ctx.GetTargetPtr(); |
Greg Clayton | 144188b | 2011-09-12 23:21:58 +0000 | [diff] [blame] | 120 | |
Sean Callanan | 5b658cc | 2011-11-07 23:35:40 +0000 | [diff] [blame] | 121 | if (!(m_allow_cxx || m_allow_objc)) |
Sean Callanan | 5474125 | 2012-12-01 00:08:33 +0000 | [diff] [blame] | 122 | { |
| 123 | if (log) |
| 124 | log->Printf(" [CUE::SC] Settings inhibit C++ and Objective-C"); |
Sean Callanan | 5b658cc | 2011-11-07 23:35:40 +0000 | [diff] [blame] | 125 | return; |
Sean Callanan | 5474125 | 2012-12-01 00:08:33 +0000 | [diff] [blame] | 126 | } |
Sean Callanan | 5b658cc | 2011-11-07 23:35:40 +0000 | [diff] [blame] | 127 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 128 | StackFrame *frame = exe_ctx.GetFramePtr(); |
| 129 | if (frame == NULL) |
Sean Callanan | 5474125 | 2012-12-01 00:08:33 +0000 | [diff] [blame] | 130 | { |
| 131 | if (log) |
| 132 | log->Printf(" [CUE::SC] Null stack frame"); |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 133 | return; |
Sean Callanan | 5474125 | 2012-12-01 00:08:33 +0000 | [diff] [blame] | 134 | } |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 135 | |
Sean Callanan | b573bd6 | 2012-07-13 21:20:29 +0000 | [diff] [blame] | 136 | SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | lldb::eSymbolContextBlock); |
Sean Callanan | e8e5557 | 2010-12-01 21:35:54 +0000 | [diff] [blame] | 137 | |
Sean Callanan | c617a4c | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 138 | if (!sym_ctx.function) |
Sean Callanan | 5474125 | 2012-12-01 00:08:33 +0000 | [diff] [blame] | 139 | { |
| 140 | if (log) |
| 141 | log->Printf(" [CUE::SC] Null function"); |
Sean Callanan | c617a4c | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 142 | return; |
Sean Callanan | 5474125 | 2012-12-01 00:08:33 +0000 | [diff] [blame] | 143 | } |
Sean Callanan | c617a4c | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 144 | |
Greg Clayton | b3a1a2b | 2012-07-14 00:53:55 +0000 | [diff] [blame] | 145 | // Find the block that defines the function represented by "sym_ctx" |
| 146 | Block *function_block = sym_ctx.GetFunctionBlock(); |
Sean Callanan | c617a4c | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 147 | |
Greg Clayton | b3a1a2b | 2012-07-14 00:53:55 +0000 | [diff] [blame] | 148 | if (!function_block) |
Sean Callanan | 5474125 | 2012-12-01 00:08:33 +0000 | [diff] [blame] | 149 | { |
| 150 | if (log) |
| 151 | log->Printf(" [CUE::SC] Null function block"); |
Greg Clayton | b3a1a2b | 2012-07-14 00:53:55 +0000 | [diff] [blame] | 152 | return; |
Sean Callanan | 5474125 | 2012-12-01 00:08:33 +0000 | [diff] [blame] | 153 | } |
Greg Clayton | b3a1a2b | 2012-07-14 00:53:55 +0000 | [diff] [blame] | 154 | |
| 155 | clang::DeclContext *decl_context = function_block->GetClangDeclContext(); |
| 156 | |
Sean Callanan | c617a4c | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 157 | if (!decl_context) |
Sean Callanan | 5474125 | 2012-12-01 00:08:33 +0000 | [diff] [blame] | 158 | { |
| 159 | if (log) |
| 160 | log->Printf(" [CUE::SC] Null decl context"); |
Sean Callanan | c617a4c | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 161 | return; |
Sean Callanan | 5474125 | 2012-12-01 00:08:33 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Sean Callanan | c617a4c | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 164 | if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context)) |
Sean Callanan | e8e5557 | 2010-12-01 21:35:54 +0000 | [diff] [blame] | 165 | { |
Sean Callanan | 5b658cc | 2011-11-07 23:35:40 +0000 | [diff] [blame] | 166 | if (m_allow_cxx && method_decl->isInstance()) |
Sean Callanan | e8e5557 | 2010-12-01 21:35:54 +0000 | [diff] [blame] | 167 | { |
Sean Callanan | 5ed59a7 | 2011-12-13 01:42:04 +0000 | [diff] [blame] | 168 | if (m_enforce_valid_object) |
Sean Callanan | fa9e6dd | 2011-11-04 02:09:33 +0000 | [diff] [blame] | 169 | { |
Greg Clayton | b3a1a2b | 2012-07-14 00:53:55 +0000 | [diff] [blame] | 170 | lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true)); |
Sean Callanan | 5ed59a7 | 2011-12-13 01:42:04 +0000 | [diff] [blame] | 171 | |
| 172 | const char *thisErrorString = "Stopped in a C++ method, but 'this' isn't available; pretending we are in a generic context"; |
| 173 | |
Greg Clayton | b3a1a2b | 2012-07-14 00:53:55 +0000 | [diff] [blame] | 174 | if (!variable_list_sp) |
Sean Callanan | 5ed59a7 | 2011-12-13 01:42:04 +0000 | [diff] [blame] | 175 | { |
Sean Callanan | 5ed59a7 | 2011-12-13 01:42:04 +0000 | [diff] [blame] | 176 | err.SetErrorString(thisErrorString); |
| 177 | return; |
| 178 | } |
| 179 | |
Greg Clayton | b3a1a2b | 2012-07-14 00:53:55 +0000 | [diff] [blame] | 180 | lldb::VariableSP this_var_sp (variable_list_sp->FindVariable(ConstString("this"))); |
Sean Callanan | 5ed59a7 | 2011-12-13 01:42:04 +0000 | [diff] [blame] | 181 | |
Greg Clayton | b3a1a2b | 2012-07-14 00:53:55 +0000 | [diff] [blame] | 182 | if (!this_var_sp || |
| 183 | !this_var_sp->IsInScope(frame) || |
| 184 | !this_var_sp->LocationIsValidForFrame (frame)) |
Sean Callanan | 5ed59a7 | 2011-12-13 01:42:04 +0000 | [diff] [blame] | 185 | { |
Sean Callanan | 5ed59a7 | 2011-12-13 01:42:04 +0000 | [diff] [blame] | 186 | err.SetErrorString(thisErrorString); |
| 187 | return; |
| 188 | } |
Sean Callanan | fa9e6dd | 2011-11-04 02:09:33 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Sean Callanan | c617a4c | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 191 | m_cplusplus = true; |
Sean Callanan | de3d27e | 2011-09-26 18:45:31 +0000 | [diff] [blame] | 192 | m_needs_object_ptr = true; |
Sean Callanan | e8e5557 | 2010-12-01 21:35:54 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
Sean Callanan | c617a4c | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 195 | else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context)) |
Sean Callanan | fa9e6dd | 2011-11-04 02:09:33 +0000 | [diff] [blame] | 196 | { |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 197 | if (m_allow_objc) |
Sean Callanan | de3d27e | 2011-09-26 18:45:31 +0000 | [diff] [blame] | 198 | { |
Sean Callanan | 5ed59a7 | 2011-12-13 01:42:04 +0000 | [diff] [blame] | 199 | if (m_enforce_valid_object) |
Sean Callanan | fa9e6dd | 2011-11-04 02:09:33 +0000 | [diff] [blame] | 200 | { |
Greg Clayton | b3a1a2b | 2012-07-14 00:53:55 +0000 | [diff] [blame] | 201 | lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true)); |
Sean Callanan | 5ed59a7 | 2011-12-13 01:42:04 +0000 | [diff] [blame] | 202 | |
| 203 | const char *selfErrorString = "Stopped in an Objective-C method, but 'self' isn't available; pretending we are in a generic context"; |
| 204 | |
Greg Clayton | b3a1a2b | 2012-07-14 00:53:55 +0000 | [diff] [blame] | 205 | if (!variable_list_sp) |
Sean Callanan | 5ed59a7 | 2011-12-13 01:42:04 +0000 | [diff] [blame] | 206 | { |
Sean Callanan | 5ed59a7 | 2011-12-13 01:42:04 +0000 | [diff] [blame] | 207 | err.SetErrorString(selfErrorString); |
| 208 | return; |
| 209 | } |
| 210 | |
Greg Clayton | b3a1a2b | 2012-07-14 00:53:55 +0000 | [diff] [blame] | 211 | lldb::VariableSP self_variable_sp = variable_list_sp->FindVariable(ConstString("self")); |
Sean Callanan | 5ed59a7 | 2011-12-13 01:42:04 +0000 | [diff] [blame] | 212 | |
Greg Clayton | b3a1a2b | 2012-07-14 00:53:55 +0000 | [diff] [blame] | 213 | if (!self_variable_sp || |
| 214 | !self_variable_sp->IsInScope(frame) || |
| 215 | !self_variable_sp->LocationIsValidForFrame (frame)) |
Sean Callanan | 5ed59a7 | 2011-12-13 01:42:04 +0000 | [diff] [blame] | 216 | { |
Sean Callanan | 5ed59a7 | 2011-12-13 01:42:04 +0000 | [diff] [blame] | 217 | err.SetErrorString(selfErrorString); |
| 218 | return; |
| 219 | } |
Sean Callanan | fa9e6dd | 2011-11-04 02:09:33 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Sean Callanan | c617a4c | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 222 | m_objectivec = true; |
Sean Callanan | de3d27e | 2011-09-26 18:45:31 +0000 | [diff] [blame] | 223 | m_needs_object_ptr = true; |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 224 | |
| 225 | if (!method_decl->isInstanceMethod()) |
| 226 | m_static_method = true; |
Sean Callanan | de3d27e | 2011-09-26 18:45:31 +0000 | [diff] [blame] | 227 | } |
Sean Callanan | e8e5557 | 2010-12-01 21:35:54 +0000 | [diff] [blame] | 228 | } |
Jim Ingham | c7f17c0 | 2012-10-30 23:35:54 +0000 | [diff] [blame] | 229 | else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_context)) |
| 230 | { |
| 231 | // We might also have a function that said in the debug information that it captured an |
| 232 | // object pointer. The best way to deal with getting to the ivars at present it by pretending |
| 233 | // that this is a method of a class in whatever runtime the debug info says the object pointer |
| 234 | // belongs to. Do that here. |
| 235 | |
Greg Clayton | 017c16a | 2013-03-27 01:48:02 +0000 | [diff] [blame] | 236 | ClangASTMetadata *metadata = ClangASTContext::GetMetadata (&decl_context->getParentASTContext(), function_decl); |
Jim Ingham | c7f17c0 | 2012-10-30 23:35:54 +0000 | [diff] [blame] | 237 | if (metadata && metadata->HasObjectPtr()) |
| 238 | { |
| 239 | lldb::LanguageType language = metadata->GetObjectPtrLanguage(); |
| 240 | if (language == lldb::eLanguageTypeC_plus_plus) |
| 241 | { |
Sean Callanan | 288ddfe | 2013-01-19 01:49:02 +0000 | [diff] [blame] | 242 | if (m_enforce_valid_object) |
| 243 | { |
| 244 | lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true)); |
| 245 | |
| 246 | const char *thisErrorString = "Stopped in a context claiming to capture a C++ object pointer, but 'this' isn't available; pretending we are in a generic context"; |
| 247 | |
| 248 | if (!variable_list_sp) |
| 249 | { |
| 250 | err.SetErrorString(thisErrorString); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | lldb::VariableSP this_var_sp (variable_list_sp->FindVariable(ConstString("this"))); |
| 255 | |
| 256 | if (!this_var_sp || |
| 257 | !this_var_sp->IsInScope(frame) || |
| 258 | !this_var_sp->LocationIsValidForFrame (frame)) |
| 259 | { |
| 260 | err.SetErrorString(thisErrorString); |
| 261 | return; |
| 262 | } |
| 263 | } |
| 264 | |
Jim Ingham | c7f17c0 | 2012-10-30 23:35:54 +0000 | [diff] [blame] | 265 | m_cplusplus = true; |
| 266 | m_needs_object_ptr = true; |
| 267 | } |
| 268 | else if (language == lldb::eLanguageTypeObjC) |
| 269 | { |
Sean Callanan | 288ddfe | 2013-01-19 01:49:02 +0000 | [diff] [blame] | 270 | if (m_enforce_valid_object) |
| 271 | { |
| 272 | lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true)); |
| 273 | |
| 274 | const char *selfErrorString = "Stopped in a context claiming to capture an Objective-C object pointer, but 'self' isn't available; pretending we are in a generic context"; |
| 275 | |
| 276 | if (!variable_list_sp) |
| 277 | { |
| 278 | err.SetErrorString(selfErrorString); |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | lldb::VariableSP self_variable_sp = variable_list_sp->FindVariable(ConstString("self")); |
| 283 | |
| 284 | if (!self_variable_sp || |
| 285 | !self_variable_sp->IsInScope(frame) || |
| 286 | !self_variable_sp->LocationIsValidForFrame (frame)) |
| 287 | { |
| 288 | err.SetErrorString(selfErrorString); |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | Type *self_type = self_variable_sp->GetType(); |
| 293 | |
| 294 | if (!self_type) |
| 295 | { |
| 296 | err.SetErrorString(selfErrorString); |
| 297 | return; |
| 298 | } |
| 299 | |
Greg Clayton | 52f7923 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 300 | ClangASTType self_clang_type = self_type->GetClangForwardType(); |
Sean Callanan | 288ddfe | 2013-01-19 01:49:02 +0000 | [diff] [blame] | 301 | |
Greg Clayton | 52f7923 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 302 | if (!self_clang_type) |
Sean Callanan | 288ddfe | 2013-01-19 01:49:02 +0000 | [diff] [blame] | 303 | { |
| 304 | err.SetErrorString(selfErrorString); |
| 305 | return; |
| 306 | } |
Greg Clayton | 52f7923 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 307 | |
| 308 | if (self_clang_type.IsObjCClassType()) |
Sean Callanan | 288ddfe | 2013-01-19 01:49:02 +0000 | [diff] [blame] | 309 | { |
| 310 | return; |
| 311 | } |
Greg Clayton | 52f7923 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 312 | else if (self_clang_type.IsObjCObjectPointerType()) |
Sean Callanan | 288ddfe | 2013-01-19 01:49:02 +0000 | [diff] [blame] | 313 | { |
| 314 | m_objectivec = true; |
| 315 | m_needs_object_ptr = true; |
| 316 | } |
| 317 | else |
| 318 | { |
| 319 | err.SetErrorString(selfErrorString); |
| 320 | return; |
| 321 | } |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | m_objectivec = true; |
| 326 | m_needs_object_ptr = true; |
| 327 | } |
Jim Ingham | c7f17c0 | 2012-10-30 23:35:54 +0000 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | } |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Sean Callanan | 28195f9 | 2013-04-19 07:09:15 +0000 | [diff] [blame] | 333 | void |
| 334 | ClangUserExpression::InstallContext (ExecutionContext &exe_ctx) |
| 335 | { |
| 336 | m_process_wp = exe_ctx.GetProcessSP(); |
| 337 | |
| 338 | lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP(); |
| 339 | |
| 340 | if (frame_sp) |
| 341 | m_address = frame_sp->GetFrameCodeAddress(); |
| 342 | } |
| 343 | |
| 344 | bool |
| 345 | ClangUserExpression::LockAndCheckContext (ExecutionContext &exe_ctx, |
| 346 | lldb::TargetSP &target_sp, |
| 347 | lldb::ProcessSP &process_sp, |
| 348 | lldb::StackFrameSP &frame_sp) |
| 349 | { |
| 350 | lldb::ProcessSP expected_process_sp = m_process_wp.lock(); |
| 351 | process_sp = exe_ctx.GetProcessSP(); |
| 352 | |
| 353 | if (process_sp != expected_process_sp) |
| 354 | return false; |
| 355 | |
| 356 | process_sp = exe_ctx.GetProcessSP(); |
| 357 | target_sp = exe_ctx.GetTargetSP(); |
| 358 | frame_sp = exe_ctx.GetFrameSP(); |
| 359 | |
| 360 | if (m_address.IsValid()) |
| 361 | { |
| 362 | if (!frame_sp) |
| 363 | return false; |
| 364 | else |
| 365 | return (0 == Address::CompareLoadAddress(m_address, frame_sp->GetFrameCodeAddress(), target_sp.get())); |
| 366 | } |
| 367 | |
| 368 | return true; |
| 369 | } |
| 370 | |
| 371 | bool |
| 372 | ClangUserExpression::MatchesContext (ExecutionContext &exe_ctx) |
| 373 | { |
| 374 | lldb::TargetSP target_sp; |
| 375 | lldb::ProcessSP process_sp; |
| 376 | lldb::StackFrameSP frame_sp; |
| 377 | |
| 378 | return LockAndCheckContext(exe_ctx, target_sp, process_sp, frame_sp); |
| 379 | } |
| 380 | |
Sean Callanan | 550f276 | 2010-10-22 23:25:16 +0000 | [diff] [blame] | 381 | // This is a really nasty hack, meant to fix Objective-C expressions of the form |
| 382 | // (int)[myArray count]. Right now, because the type information for count is |
| 383 | // not available, [myArray count] returns id, which can't be directly cast to |
| 384 | // int without causing a clang error. |
| 385 | static void |
| 386 | ApplyObjcCastHack(std::string &expr) |
| 387 | { |
| 388 | #define OBJC_CAST_HACK_FROM "(int)[" |
| 389 | #define OBJC_CAST_HACK_TO "(int)(long long)[" |
| 390 | |
| 391 | size_t from_offset; |
| 392 | |
| 393 | while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos) |
| 394 | expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO); |
| 395 | |
| 396 | #undef OBJC_CAST_HACK_TO |
| 397 | #undef OBJC_CAST_HACK_FROM |
| 398 | } |
| 399 | |
Sean Callanan | 3089237 | 2010-10-24 20:45:49 +0000 | [diff] [blame] | 400 | // Another hack, meant to allow use of unichar despite it not being available in |
| 401 | // the type information. Although we could special-case it in type lookup, |
| 402 | // hopefully we'll figure out a way to #include the same environment as is |
| 403 | // present in the original source file rather than try to hack specific type |
| 404 | // definitions in as needed. |
| 405 | static void |
| 406 | ApplyUnicharHack(std::string &expr) |
| 407 | { |
| 408 | #define UNICHAR_HACK_FROM "unichar" |
| 409 | #define UNICHAR_HACK_TO "unsigned short" |
| 410 | |
| 411 | size_t from_offset; |
| 412 | |
| 413 | while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos) |
| 414 | expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO); |
| 415 | |
| 416 | #undef UNICHAR_HACK_TO |
| 417 | #undef UNICHAR_HACK_FROM |
| 418 | } |
| 419 | |
Sean Callanan | 550f276 | 2010-10-22 23:25:16 +0000 | [diff] [blame] | 420 | bool |
Sean Callanan | a91dd99 | 2010-11-19 02:52:21 +0000 | [diff] [blame] | 421 | ClangUserExpression::Parse (Stream &error_stream, |
| 422 | ExecutionContext &exe_ctx, |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 423 | lldb_private::ExecutionPolicy execution_policy, |
Sean Callanan | 696cf5f | 2011-05-07 01:06:41 +0000 | [diff] [blame] | 424 | bool keep_result_in_memory) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 425 | { |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 426 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 427 | |
Sean Callanan | fa9e6dd | 2011-11-04 02:09:33 +0000 | [diff] [blame] | 428 | Error err; |
Sean Callanan | dd1dcfd | 2012-02-10 01:22:05 +0000 | [diff] [blame] | 429 | |
| 430 | InstallContext(exe_ctx); |
Sean Callanan | fa9e6dd | 2011-11-04 02:09:33 +0000 | [diff] [blame] | 431 | |
| 432 | ScanContext(exe_ctx, err); |
| 433 | |
| 434 | if (!err.Success()) |
| 435 | { |
| 436 | error_stream.Printf("warning: %s\n", err.AsCString()); |
| 437 | } |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 438 | |
| 439 | StreamString m_transformed_stream; |
| 440 | |
| 441 | //////////////////////////////////// |
| 442 | // Generate the expression |
| 443 | // |
Sean Callanan | 550f276 | 2010-10-22 23:25:16 +0000 | [diff] [blame] | 444 | |
| 445 | ApplyObjcCastHack(m_expr_text); |
Greg Clayton | f3d0b0c | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 446 | //ApplyUnicharHack(m_expr_text); |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 447 | |
Greg Clayton | 102b2c2 | 2013-04-18 22:45:39 +0000 | [diff] [blame] | 448 | std::unique_ptr<ExpressionSourceCode> source_code (ExpressionSourceCode::CreateWrapped(m_expr_prefix.c_str(), m_expr_text.c_str())); |
Sean Callanan | de3d27e | 2011-09-26 18:45:31 +0000 | [diff] [blame] | 449 | |
| 450 | lldb::LanguageType lang_type; |
| 451 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 452 | if (m_cplusplus) |
Sean Callanan | de3d27e | 2011-09-26 18:45:31 +0000 | [diff] [blame] | 453 | lang_type = lldb::eLanguageTypeC_plus_plus; |
| 454 | else if(m_objectivec) |
| 455 | lang_type = lldb::eLanguageTypeObjC; |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 456 | else |
Sean Callanan | de3d27e | 2011-09-26 18:45:31 +0000 | [diff] [blame] | 457 | lang_type = lldb::eLanguageTypeC; |
| 458 | |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 459 | if (!source_code->GetText(m_transformed_text, lang_type, m_const_object, m_static_method)) |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 460 | { |
Sean Callanan | de3d27e | 2011-09-26 18:45:31 +0000 | [diff] [blame] | 461 | error_stream.PutCString ("error: couldn't construct expression body"); |
| 462 | return false; |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 465 | if (log) |
| 466 | log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str()); |
| 467 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 468 | //////////////////////////////////// |
| 469 | // Set up the target and compiler |
| 470 | // |
| 471 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 472 | Target *target = exe_ctx.GetTargetPtr(); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 473 | |
| 474 | if (!target) |
| 475 | { |
| 476 | error_stream.PutCString ("error: invalid target\n"); |
| 477 | return false; |
| 478 | } |
| 479 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 480 | ////////////////////////// |
| 481 | // Parse the expression |
| 482 | // |
Sean Callanan | daa6efe | 2011-12-21 22:22:58 +0000 | [diff] [blame] | 483 | |
Sean Callanan | 3b16eb9 | 2013-04-11 00:09:05 +0000 | [diff] [blame] | 484 | m_materializer_ap.reset(new Materializer()); |
| 485 | |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 486 | m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory, exe_ctx)); |
Sean Callanan | aa301c4 | 2010-12-03 01:38:59 +0000 | [diff] [blame] | 487 | |
Greg Clayton | 52f7923 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 488 | class OnExit |
| 489 | { |
| 490 | public: |
| 491 | typedef std::function <void (void)> Callback; |
| 492 | |
| 493 | OnExit (Callback const &callback) : |
| 494 | m_callback(callback) |
| 495 | { |
| 496 | } |
| 497 | |
| 498 | ~OnExit () |
| 499 | { |
| 500 | m_callback(); |
| 501 | } |
| 502 | private: |
| 503 | Callback m_callback; |
| 504 | }; |
| 505 | |
| 506 | OnExit on_exit([this]() { m_expr_decl_map.reset(); }); |
| 507 | |
Sean Callanan | 3b16eb9 | 2013-04-11 00:09:05 +0000 | [diff] [blame] | 508 | if (!m_expr_decl_map->WillParse(exe_ctx, m_materializer_ap.get())) |
Sean Callanan | 166ba10 | 2011-08-01 18:18:33 +0000 | [diff] [blame] | 509 | { |
| 510 | error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n"); |
| 511 | return false; |
| 512 | } |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 513 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 514 | Process *process = exe_ctx.GetProcessPtr(); |
Sean Callanan | e7ba112 | 2012-02-08 18:43:35 +0000 | [diff] [blame] | 515 | ExecutionContextScope *exe_scope = process; |
| 516 | |
| 517 | if (!exe_scope) |
| 518 | exe_scope = exe_ctx.GetTargetPtr(); |
| 519 | |
| 520 | ClangExpressionParser parser(exe_scope, *this); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 521 | |
| 522 | unsigned num_errors = parser.Parse (error_stream); |
| 523 | |
| 524 | if (num_errors) |
| 525 | { |
| 526 | error_stream.Printf ("error: %d errors parsing expression\n", num_errors); |
Sean Callanan | aa301c4 | 2010-12-03 01:38:59 +0000 | [diff] [blame] | 527 | |
| 528 | m_expr_decl_map->DidParse(); |
| 529 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 530 | return false; |
| 531 | } |
| 532 | |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 533 | ////////////////////////////////////////////////////////////////////////////////////////// |
| 534 | // Prepare the output of the parser for execution, evaluating it statically if possible |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 535 | // |
Sean Callanan | 1cf3da8 | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 536 | |
| 537 | Error jit_error = parser.PrepareForExecution (m_jit_start_addr, |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 538 | m_jit_end_addr, |
Sean Callanan | 9e6f6a6 | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 539 | m_execution_unit_ap, |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 540 | exe_ctx, |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 541 | m_can_interpret, |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 542 | execution_policy); |
Sean Callanan | 6d284ef | 2011-10-12 22:20:02 +0000 | [diff] [blame] | 543 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 544 | if (jit_error.Success()) |
| 545 | { |
Sean Callanan | 1cf3da8 | 2013-03-19 00:10:07 +0000 | [diff] [blame] | 546 | if (process && m_jit_start_addr != LLDB_INVALID_ADDRESS) |
Enrico Granata | 7adfcfd | 2012-09-18 00:08:47 +0000 | [diff] [blame] | 547 | m_jit_process_wp = lldb::ProcessWP(process->shared_from_this()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 548 | return true; |
| 549 | } |
| 550 | else |
| 551 | { |
Greg Clayton | 3058197 | 2011-05-17 03:51:29 +0000 | [diff] [blame] | 552 | const char *error_cstr = jit_error.AsCString(); |
| 553 | if (error_cstr && error_cstr[0]) |
| 554 | error_stream.Printf ("error: %s\n", error_cstr); |
| 555 | else |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 556 | error_stream.Printf ("error: expression can't be interpreted or run\n"); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 557 | return false; |
| 558 | } |
| 559 | } |
| 560 | |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 561 | static lldb::addr_t |
| 562 | GetObjectPointer (lldb::StackFrameSP frame_sp, |
| 563 | ConstString &object_name, |
| 564 | Error &err) |
| 565 | { |
| 566 | err.Clear(); |
| 567 | |
| 568 | if (!frame_sp) |
| 569 | { |
| 570 | err.SetErrorStringWithFormat("Couldn't load '%s' because the context is incomplete", object_name.AsCString()); |
| 571 | return LLDB_INVALID_ADDRESS; |
| 572 | } |
| 573 | |
| 574 | lldb::VariableSP var_sp; |
| 575 | lldb::ValueObjectSP valobj_sp; |
| 576 | |
| 577 | valobj_sp = frame_sp->GetValueForVariableExpressionPath(object_name.AsCString(), |
| 578 | lldb::eNoDynamicValues, |
| 579 | StackFrame::eExpressionPathOptionCheckPtrVsMember || |
| 580 | StackFrame::eExpressionPathOptionsAllowDirectIVarAccess || |
| 581 | StackFrame::eExpressionPathOptionsNoFragileObjcIvar || |
| 582 | StackFrame::eExpressionPathOptionsNoSyntheticChildren || |
| 583 | StackFrame::eExpressionPathOptionsNoSyntheticArrayRange, |
| 584 | var_sp, |
| 585 | err); |
| 586 | |
| 587 | if (!err.Success()) |
| 588 | return LLDB_INVALID_ADDRESS; |
| 589 | |
| 590 | lldb::addr_t ret = valobj_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS); |
| 591 | |
| 592 | if (ret == LLDB_INVALID_ADDRESS) |
| 593 | { |
| 594 | err.SetErrorStringWithFormat("Couldn't load '%s' because its value couldn't be evaluated", object_name.AsCString()); |
| 595 | return LLDB_INVALID_ADDRESS; |
| 596 | } |
| 597 | |
| 598 | return ret; |
| 599 | } |
| 600 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 601 | bool |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 602 | ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream, |
Sean Callanan | ab06af9 | 2010-10-19 23:57:21 +0000 | [diff] [blame] | 603 | ExecutionContext &exe_ctx, |
| 604 | lldb::addr_t &struct_address, |
Sean Callanan | 047923c | 2010-12-14 00:42:36 +0000 | [diff] [blame] | 605 | lldb::addr_t &object_ptr, |
| 606 | lldb::addr_t &cmd_ptr) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 607 | { |
Sean Callanan | dd1dcfd | 2012-02-10 01:22:05 +0000 | [diff] [blame] | 608 | lldb::TargetSP target; |
| 609 | lldb::ProcessSP process; |
| 610 | lldb::StackFrameSP frame; |
| 611 | |
| 612 | if (!LockAndCheckContext(exe_ctx, |
| 613 | target, |
| 614 | process, |
| 615 | frame)) |
| 616 | { |
Sean Callanan | 28195f9 | 2013-04-19 07:09:15 +0000 | [diff] [blame] | 617 | error_stream.Printf("The context has changed before we could JIT the expression!\n"); |
Sean Callanan | dd1dcfd | 2012-02-10 01:22:05 +0000 | [diff] [blame] | 618 | return false; |
| 619 | } |
| 620 | |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 621 | if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) |
| 622 | { |
Sean Callanan | 3aa7da5 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 623 | if (m_needs_object_ptr) |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 624 | { |
Sean Callanan | 3aa7da5 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 625 | ConstString object_name; |
| 626 | |
| 627 | if (m_cplusplus) |
| 628 | { |
| 629 | object_name.SetCString("this"); |
| 630 | } |
| 631 | else if (m_objectivec) |
| 632 | { |
| 633 | object_name.SetCString("self"); |
| 634 | } |
| 635 | else |
| 636 | { |
| 637 | error_stream.Printf("Need object pointer but don't know the language\n"); |
| 638 | return false; |
| 639 | } |
| 640 | |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 641 | Error object_ptr_error; |
| 642 | |
| 643 | object_ptr = GetObjectPointer(frame, object_name, object_ptr_error); |
| 644 | |
| 645 | if (!object_ptr_error.Success()) |
Sean Callanan | 3aa7da5 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 646 | { |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 647 | error_stream.Printf("warning: couldn't get required object pointer (substituting NULL): %s\n", object_ptr_error.AsCString()); |
Sean Callanan | 5ed59a7 | 2011-12-13 01:42:04 +0000 | [diff] [blame] | 648 | object_ptr = 0; |
Sean Callanan | 3aa7da5 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 649 | } |
Sean Callanan | 047923c | 2010-12-14 00:42:36 +0000 | [diff] [blame] | 650 | |
| 651 | if (m_objectivec) |
| 652 | { |
| 653 | ConstString cmd_name("_cmd"); |
| 654 | |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 655 | cmd_ptr = GetObjectPointer(frame, cmd_name, object_ptr_error); |
| 656 | |
| 657 | if (!object_ptr_error.Success()) |
Sean Callanan | 047923c | 2010-12-14 00:42:36 +0000 | [diff] [blame] | 658 | { |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 659 | error_stream.Printf("warning: couldn't get cmd pointer (substituting NULL): %s\n", object_ptr_error.AsCString()); |
Sean Callanan | 5ed59a7 | 2011-12-13 01:42:04 +0000 | [diff] [blame] | 660 | cmd_ptr = 0; |
Sean Callanan | 047923c | 2010-12-14 00:42:36 +0000 | [diff] [blame] | 661 | } |
| 662 | } |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 663 | } |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 664 | |
Sean Callanan | cdc3ea5 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 665 | if (m_materialized_address == LLDB_INVALID_ADDRESS) |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 666 | { |
Sean Callanan | cdc3ea5 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 667 | Error alloc_error; |
| 668 | |
| 669 | IRMemoryMap::AllocationPolicy policy = m_can_interpret ? IRMemoryMap::eAllocationPolicyHostOnly : IRMemoryMap::eAllocationPolicyMirror; |
| 670 | |
| 671 | m_materialized_address = m_execution_unit_ap->Malloc(m_materializer_ap->GetStructByteSize(), |
| 672 | m_materializer_ap->GetStructAlignment(), |
| 673 | lldb::ePermissionsReadable | lldb::ePermissionsWritable, |
| 674 | policy, |
| 675 | alloc_error); |
| 676 | |
| 677 | if (!alloc_error.Success()) |
| 678 | { |
| 679 | error_stream.Printf("Couldn't allocate space for materialized struct: %s\n", alloc_error.AsCString()); |
| 680 | return false; |
| 681 | } |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Sean Callanan | cdc3ea5 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 684 | struct_address = m_materialized_address; |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 685 | |
Sean Callanan | cdc3ea5 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 686 | if (m_can_interpret && m_stack_frame_bottom == LLDB_INVALID_ADDRESS) |
| 687 | { |
| 688 | Error alloc_error; |
| 689 | |
| 690 | const size_t stack_frame_size = 512 * 1024; |
| 691 | |
| 692 | m_stack_frame_bottom = m_execution_unit_ap->Malloc(stack_frame_size, |
| 693 | 8, |
| 694 | lldb::ePermissionsReadable | lldb::ePermissionsWritable, |
| 695 | IRMemoryMap::eAllocationPolicyHostOnly, |
| 696 | alloc_error); |
| 697 | |
| 698 | m_stack_frame_top = m_stack_frame_bottom + stack_frame_size; |
| 699 | |
| 700 | if (!alloc_error.Success()) |
| 701 | { |
| 702 | error_stream.Printf("Couldn't allocate space for the stack frame: %s\n", alloc_error.AsCString()); |
| 703 | return false; |
| 704 | } |
| 705 | } |
| 706 | |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 707 | Error materialize_error; |
| 708 | |
| 709 | m_dematerializer_sp = m_materializer_ap->Materialize(frame, *m_execution_unit_ap, struct_address, materialize_error); |
| 710 | |
| 711 | if (!materialize_error.Success()) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 712 | { |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 713 | error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 714 | return false; |
| 715 | } |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 716 | } |
| 717 | return true; |
| 718 | } |
| 719 | |
| 720 | ThreadPlan * |
| 721 | ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream, |
Sean Callanan | 6a92553 | 2011-01-13 08:53:35 +0000 | [diff] [blame] | 722 | ExecutionContext &exe_ctx) |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 723 | { |
| 724 | lldb::addr_t struct_address; |
| 725 | |
Johnny Chen | 2bc9eb3 | 2011-07-19 19:48:13 +0000 | [diff] [blame] | 726 | lldb::addr_t object_ptr = 0; |
| 727 | lldb::addr_t cmd_ptr = 0; |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 728 | |
Sean Callanan | 047923c | 2010-12-14 00:42:36 +0000 | [diff] [blame] | 729 | PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr); |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 730 | |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 731 | // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the |
| 732 | // ClangUserExpression resources before the thread plan finishes execution in the target. But because we are |
Sean Callanan | 3aa7da5 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 733 | // forcing unwind_on_error to be true here, in practical terms that can't happen. |
| 734 | |
Jim Ingham | b794020 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 735 | const bool stop_others = true; |
| 736 | const bool unwind_on_error = true; |
| 737 | const bool ignore_breakpoints = false; |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 738 | return ClangFunction::GetThreadPlanToCallFunction (exe_ctx, |
Greg Clayton | d0882d0 | 2011-01-19 23:00:49 +0000 | [diff] [blame] | 739 | m_jit_start_addr, |
Sean Callanan | a65b527 | 2010-12-01 01:28:23 +0000 | [diff] [blame] | 740 | struct_address, |
| 741 | error_stream, |
Jim Ingham | b794020 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 742 | stop_others, |
| 743 | unwind_on_error, |
| 744 | ignore_breakpoints, |
Sean Callanan | 3aa7da5 | 2010-12-13 22:46:15 +0000 | [diff] [blame] | 745 | (m_needs_object_ptr ? &object_ptr : NULL), |
| 746 | (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL); |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | bool |
| 750 | ClangUserExpression::FinalizeJITExecution (Stream &error_stream, |
| 751 | ExecutionContext &exe_ctx, |
Sean Callanan | 0ddf806 | 2011-05-09 22:04:36 +0000 | [diff] [blame] | 752 | lldb::ClangExpressionVariableSP &result, |
Sean Callanan | cdc3ea5 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 753 | lldb::addr_t function_stack_bottom, |
| 754 | lldb::addr_t function_stack_top) |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 755 | { |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 756 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 3371102 | 2010-12-07 10:00:20 +0000 | [diff] [blame] | 757 | |
| 758 | if (log) |
Sean Callanan | 94d255f | 2010-12-07 22:55:01 +0000 | [diff] [blame] | 759 | log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --"); |
Sean Callanan | cdc3ea5 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 760 | |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 761 | if (!m_dematerializer_sp) |
| 762 | { |
Sean Callanan | 4a4448b | 2013-07-12 23:35:21 +0000 | [diff] [blame] | 763 | error_stream.Printf ("Couldn't apply expression side effects : no dematerializer is present"); |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 764 | return false; |
| 765 | } |
| 766 | |
| 767 | Error dematerialize_error; |
| 768 | |
Sean Callanan | cdc3ea5 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 769 | m_dematerializer_sp->Dematerialize(dematerialize_error, result, function_stack_bottom, function_stack_top); |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 770 | |
| 771 | if (!dematerialize_error.Success()) |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 772 | { |
Sean Callanan | 4a4448b | 2013-07-12 23:35:21 +0000 | [diff] [blame] | 773 | error_stream.Printf ("Couldn't apply expression side effects : %s\n", dematerialize_error.AsCString("unknown error")); |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 774 | return false; |
| 775 | } |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 776 | |
Johnny Chen | c613496 | 2012-01-06 00:35:38 +0000 | [diff] [blame] | 777 | if (result) |
| 778 | result->TransferAddress(); |
| 779 | |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 780 | m_dematerializer_sp.reset(); |
| 781 | |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 782 | return true; |
| 783 | } |
| 784 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 785 | ExecutionResults |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 786 | ClangUserExpression::Execute (Stream &error_stream, |
| 787 | ExecutionContext &exe_ctx, |
Jim Ingham | b794020 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 788 | bool unwind_on_error, |
| 789 | bool ignore_breakpoints, |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 790 | ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me, |
Enrico Granata | 6cca969 | 2012-07-16 23:10:35 +0000 | [diff] [blame] | 791 | lldb::ClangExpressionVariableSP &result, |
Jim Ingham | 47beabb | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 792 | bool run_others, |
| 793 | uint32_t timeout_usec) |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 794 | { |
Jim Ingham | 7812e01 | 2011-01-18 22:20:08 +0000 | [diff] [blame] | 795 | // The expression log is quite verbose, and if you're just tracking the execution of the |
| 796 | // expression, it's quite convenient to have these logs come out with the STEP log as well. |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 797 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP)); |
Sean Callanan | 3371102 | 2010-12-07 10:00:20 +0000 | [diff] [blame] | 798 | |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 799 | if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 800 | { |
Jim Ingham | 6f01c93 | 2012-10-12 17:34:26 +0000 | [diff] [blame] | 801 | lldb::addr_t struct_address = LLDB_INVALID_ADDRESS; |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 802 | |
Johnny Chen | 2bc9eb3 | 2011-07-19 19:48:13 +0000 | [diff] [blame] | 803 | lldb::addr_t object_ptr = 0; |
| 804 | lldb::addr_t cmd_ptr = 0; |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 805 | |
Johnny Chen | 544f2b6 | 2012-08-18 04:24:00 +0000 | [diff] [blame] | 806 | if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr)) |
| 807 | { |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 808 | error_stream.Printf("Errored out in %s, couldn't PrepareToExecuteJITExpression", __FUNCTION__); |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 809 | return eExecutionSetupError; |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 810 | } |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 811 | |
Sean Callanan | cdc3ea5 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 812 | lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS; |
| 813 | lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS; |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 814 | |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 815 | if (m_can_interpret) |
| 816 | { |
| 817 | llvm::Module *module = m_execution_unit_ap->GetModule(); |
| 818 | llvm::Function *function = m_execution_unit_ap->GetFunction(); |
Jim Ingham | 0296fe7 | 2011-11-08 03:00:11 +0000 | [diff] [blame] | 819 | |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 820 | if (!module || !function) |
Jim Ingham | 2370a97 | 2011-05-17 01:10:11 +0000 | [diff] [blame] | 821 | { |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 822 | error_stream.Printf("Supposed to interpret, but nothing is there"); |
| 823 | return eExecutionSetupError; |
Jim Ingham | 2370a97 | 2011-05-17 01:10:11 +0000 | [diff] [blame] | 824 | } |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 825 | |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 826 | Error interpreter_error; |
| 827 | |
| 828 | llvm::SmallVector <lldb::addr_t, 3> args; |
| 829 | |
| 830 | if (m_needs_object_ptr) |
| 831 | { |
| 832 | args.push_back(object_ptr); |
| 833 | |
| 834 | if (m_objectivec) |
| 835 | args.push_back(cmd_ptr); |
| 836 | } |
| 837 | |
| 838 | args.push_back(struct_address); |
| 839 | |
Sean Callanan | cdc3ea5 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 840 | function_stack_bottom = m_stack_frame_bottom; |
| 841 | function_stack_top = m_stack_frame_top; |
| 842 | |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 843 | IRInterpreter::Interpret (*module, |
| 844 | *function, |
| 845 | args, |
| 846 | *m_execution_unit_ap.get(), |
Sean Callanan | cdc3ea5 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 847 | interpreter_error, |
| 848 | function_stack_bottom, |
| 849 | function_stack_top); |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 850 | |
| 851 | if (!interpreter_error.Success()) |
| 852 | { |
| 853 | error_stream.Printf("Supposed to interpret, but failed: %s", interpreter_error.AsCString()); |
| 854 | return eExecutionDiscarded; |
| 855 | } |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 856 | } |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 857 | else |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 858 | { |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 859 | const bool stop_others = true; |
| 860 | const bool try_all_threads = run_others; |
| 861 | |
| 862 | Address wrapper_address (m_jit_start_addr); |
| 863 | lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (exe_ctx.GetThreadRef(), |
| 864 | wrapper_address, |
| 865 | struct_address, |
| 866 | stop_others, |
| 867 | unwind_on_error, |
| 868 | ignore_breakpoints, |
| 869 | (m_needs_object_ptr ? &object_ptr : NULL), |
| 870 | ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL), |
| 871 | shared_ptr_to_me)); |
| 872 | |
| 873 | if (!call_plan_sp || !call_plan_sp->ValidatePlan (&error_stream)) |
| 874 | return eExecutionSetupError; |
| 875 | |
Sean Callanan | cdc3ea5 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 876 | lldb::addr_t function_stack_pointer = static_cast<ThreadPlanCallFunction *>(call_plan_sp.get())->GetFunctionStackPointer(); |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 877 | |
Sean Callanan | cdc3ea5 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 878 | function_stack_bottom = function_stack_pointer - Host::GetPageSize(); |
| 879 | function_stack_top = function_stack_pointer; |
| 880 | |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 881 | if (log) |
| 882 | log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --"); |
| 883 | |
| 884 | if (exe_ctx.GetProcessPtr()) |
| 885 | exe_ctx.GetProcessPtr()->SetRunningUserExpression(true); |
| 886 | |
| 887 | ExecutionResults execution_result = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx, |
| 888 | call_plan_sp, |
| 889 | stop_others, |
| 890 | try_all_threads, |
| 891 | unwind_on_error, |
| 892 | ignore_breakpoints, |
| 893 | timeout_usec, |
| 894 | error_stream); |
| 895 | |
| 896 | if (exe_ctx.GetProcessPtr()) |
| 897 | exe_ctx.GetProcessPtr()->SetRunningUserExpression(false); |
| 898 | |
| 899 | if (log) |
| 900 | log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --"); |
| 901 | |
| 902 | if (execution_result == eExecutionInterrupted || execution_result == eExecutionHitBreakpoint) |
| 903 | { |
| 904 | const char *error_desc = NULL; |
| 905 | |
| 906 | if (call_plan_sp) |
| 907 | { |
| 908 | lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo(); |
| 909 | if (real_stop_info_sp) |
| 910 | error_desc = real_stop_info_sp->GetDescription(); |
| 911 | } |
| 912 | if (error_desc) |
| 913 | error_stream.Printf ("Execution was interrupted, reason: %s.", error_desc); |
| 914 | else |
| 915 | error_stream.Printf ("Execution was interrupted."); |
| 916 | |
| 917 | if ((execution_result == eExecutionInterrupted && unwind_on_error) |
| 918 | || (execution_result == eExecutionHitBreakpoint && ignore_breakpoints)) |
| 919 | error_stream.Printf ("\nThe process has been returned to the state before expression evaluation."); |
| 920 | else |
| 921 | error_stream.Printf ("\nThe process has been left at the point where it was interrupted, use \"thread return -x\" to return to the state before expression evaluation."); |
| 922 | |
| 923 | return execution_result; |
| 924 | } |
| 925 | else if (execution_result != eExecutionCompleted) |
| 926 | { |
| 927 | error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result)); |
| 928 | return execution_result; |
| 929 | } |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Sean Callanan | cdc3ea5 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 932 | if (FinalizeJITExecution (error_stream, exe_ctx, result, function_stack_bottom, function_stack_top)) |
| 933 | { |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 934 | return eExecutionCompleted; |
Sean Callanan | cdc3ea5 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 935 | } |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 936 | else |
Johnny Chen | 544f2b6 | 2012-08-18 04:24:00 +0000 | [diff] [blame] | 937 | { |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 938 | return eExecutionSetupError; |
Johnny Chen | 544f2b6 | 2012-08-18 04:24:00 +0000 | [diff] [blame] | 939 | } |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 940 | } |
| 941 | else |
| 942 | { |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 943 | error_stream.Printf("Expression can't be run, because there is no JIT compiled function"); |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 944 | return eExecutionSetupError; |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 948 | ExecutionResults |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 949 | ClangUserExpression::Evaluate (ExecutionContext &exe_ctx, |
| 950 | lldb_private::ExecutionPolicy execution_policy, |
Sean Callanan | 5b658cc | 2011-11-07 23:35:40 +0000 | [diff] [blame] | 951 | lldb::LanguageType language, |
Sean Callanan | daa6efe | 2011-12-21 22:22:58 +0000 | [diff] [blame] | 952 | ResultType desired_type, |
Jim Ingham | b794020 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 953 | bool unwind_on_error, |
| 954 | bool ignore_breakpoints, |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 955 | const char *expr_cstr, |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 956 | const char *expr_prefix, |
Enrico Granata | 6cca969 | 2012-07-16 23:10:35 +0000 | [diff] [blame] | 957 | lldb::ValueObjectSP &result_valobj_sp, |
Jim Ingham | 47beabb | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 958 | bool run_others, |
| 959 | uint32_t timeout_usec) |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 960 | { |
Jim Ingham | ec07c0d | 2011-08-09 00:00:49 +0000 | [diff] [blame] | 961 | Error error; |
Jim Ingham | 47beabb | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 962 | return EvaluateWithError (exe_ctx, |
| 963 | execution_policy, |
| 964 | language, |
| 965 | desired_type, |
Jim Ingham | b794020 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 966 | unwind_on_error, |
| 967 | ignore_breakpoints, |
Jim Ingham | 47beabb | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 968 | expr_cstr, |
| 969 | expr_prefix, |
| 970 | result_valobj_sp, |
| 971 | error, |
| 972 | run_others, |
| 973 | timeout_usec); |
Jim Ingham | ec07c0d | 2011-08-09 00:00:49 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | ExecutionResults |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 977 | ClangUserExpression::EvaluateWithError (ExecutionContext &exe_ctx, |
| 978 | lldb_private::ExecutionPolicy execution_policy, |
Sean Callanan | 5b658cc | 2011-11-07 23:35:40 +0000 | [diff] [blame] | 979 | lldb::LanguageType language, |
Sean Callanan | daa6efe | 2011-12-21 22:22:58 +0000 | [diff] [blame] | 980 | ResultType desired_type, |
Jim Ingham | b794020 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 981 | bool unwind_on_error, |
| 982 | bool ignore_breakpoints, |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 983 | const char *expr_cstr, |
| 984 | const char *expr_prefix, |
| 985 | lldb::ValueObjectSP &result_valobj_sp, |
Enrico Granata | 6cca969 | 2012-07-16 23:10:35 +0000 | [diff] [blame] | 986 | Error &error, |
Jim Ingham | 47beabb | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 987 | bool run_others, |
| 988 | uint32_t timeout_usec) |
Jim Ingham | ec07c0d | 2011-08-09 00:00:49 +0000 | [diff] [blame] | 989 | { |
Greg Clayton | 952e9dc | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 990 | Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP)); |
Sean Callanan | 94d255f | 2010-12-07 22:55:01 +0000 | [diff] [blame] | 991 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 992 | ExecutionResults execution_results = eExecutionSetupError; |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 993 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 994 | Process *process = exe_ctx.GetProcessPtr(); |
| 995 | |
| 996 | if (process == NULL || process->GetState() != lldb::eStateStopped) |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 997 | { |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 998 | if (execution_policy == eExecutionPolicyAlways) |
| 999 | { |
| 1000 | if (log) |
| 1001 | log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant =="); |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 1002 | |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 1003 | error.SetErrorString ("expression needed to run but couldn't"); |
| 1004 | |
| 1005 | return execution_results; |
| 1006 | } |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 1007 | } |
Sean Callanan | f7649bb | 2011-09-15 17:43:00 +0000 | [diff] [blame] | 1008 | |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 1009 | if (process == NULL || !process->CanJIT()) |
Sean Callanan | f7649bb | 2011-09-15 17:43:00 +0000 | [diff] [blame] | 1010 | execution_policy = eExecutionPolicyNever; |
Greg Clayton | 0baa394 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 1011 | |
Sean Callanan | daa6efe | 2011-12-21 22:22:58 +0000 | [diff] [blame] | 1012 | ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix, language, desired_type)); |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 1013 | |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 1014 | StreamString error_stream; |
Sean Callanan | 696cf5f | 2011-05-07 01:06:41 +0000 | [diff] [blame] | 1015 | |
Sean Callanan | 94d255f | 2010-12-07 22:55:01 +0000 | [diff] [blame] | 1016 | if (log) |
| 1017 | log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr); |
| 1018 | |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 1019 | const bool keep_expression_in_memory = true; |
| 1020 | |
Sean Callanan | daa6efe | 2011-12-21 22:22:58 +0000 | [diff] [blame] | 1021 | if (!user_expression_sp->Parse (error_stream, exe_ctx, execution_policy, keep_expression_in_memory)) |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 1022 | { |
| 1023 | if (error_stream.GetString().empty()) |
| 1024 | error.SetErrorString ("expression failed to parse, unknown error"); |
| 1025 | else |
| 1026 | error.SetErrorString (error_stream.GetString().c_str()); |
| 1027 | } |
| 1028 | else |
| 1029 | { |
Greg Clayton | 427f290 | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 1030 | lldb::ClangExpressionVariableSP expr_result; |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 1031 | |
Sean Callanan | 0f0551e | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 1032 | if (execution_policy == eExecutionPolicyNever && |
| 1033 | !user_expression_sp->CanInterpret()) |
Sean Callanan | 47dc457 | 2011-09-15 02:13:07 +0000 | [diff] [blame] | 1034 | { |
| 1035 | if (log) |
| 1036 | log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant =="); |
| 1037 | |
| 1038 | if (error_stream.GetString().empty()) |
| 1039 | error.SetErrorString ("expression needed to run but couldn't"); |
| 1040 | } |
Sean Callanan | 05a5a1b | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 1041 | else |
| 1042 | { |
| 1043 | error_stream.GetString().clear(); |
| 1044 | |
| 1045 | if (log) |
| 1046 | log->Printf("== [ClangUserExpression::Evaluate] Executing expression =="); |
| 1047 | |
| 1048 | execution_results = user_expression_sp->Execute (error_stream, |
| 1049 | exe_ctx, |
Jim Ingham | b794020 | 2013-01-15 02:47:48 +0000 | [diff] [blame] | 1050 | unwind_on_error, |
| 1051 | ignore_breakpoints, |
Sean Callanan | 05a5a1b | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 1052 | user_expression_sp, |
Enrico Granata | 6cca969 | 2012-07-16 23:10:35 +0000 | [diff] [blame] | 1053 | expr_result, |
Jim Ingham | 47beabb | 2012-10-16 21:41:58 +0000 | [diff] [blame] | 1054 | run_others, |
| 1055 | timeout_usec); |
Sean Callanan | 05a5a1b | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 1056 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 1057 | if (execution_results != eExecutionCompleted) |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 1058 | { |
Sean Callanan | 94d255f | 2010-12-07 22:55:01 +0000 | [diff] [blame] | 1059 | if (log) |
Sean Callanan | 05a5a1b | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 1060 | log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally =="); |
| 1061 | |
| 1062 | if (error_stream.GetString().empty()) |
| 1063 | error.SetErrorString ("expression failed to execute, unknown error"); |
| 1064 | else |
| 1065 | error.SetErrorString (error_stream.GetString().c_str()); |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 1066 | } |
Sean Callanan | 05a5a1b | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 1067 | else |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 1068 | { |
Sean Callanan | 05a5a1b | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 1069 | if (expr_result) |
| 1070 | { |
| 1071 | result_valobj_sp = expr_result->GetValueObject(); |
| 1072 | |
| 1073 | if (log) |
Jim Ingham | fa3a16a | 2011-03-31 00:19:25 +0000 | [diff] [blame] | 1074 | log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString()); |
Sean Callanan | 05a5a1b | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 1075 | } |
| 1076 | else |
| 1077 | { |
| 1078 | if (log) |
| 1079 | log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result =="); |
| 1080 | |
Sean Callanan | 2431244 | 2011-08-23 21:20:51 +0000 | [diff] [blame] | 1081 | error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric); |
Sean Callanan | 05a5a1b | 2010-12-16 03:17:46 +0000 | [diff] [blame] | 1082 | } |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 1083 | } |
| 1084 | } |
| 1085 | } |
Sean Callanan | 44820ec | 2010-10-19 20:15:00 +0000 | [diff] [blame] | 1086 | |
Greg Clayton | d171972 | 2010-10-05 03:13:51 +0000 | [diff] [blame] | 1087 | if (result_valobj_sp.get() == NULL) |
Jim Ingham | 47da810 | 2011-04-22 23:53:53 +0000 | [diff] [blame] | 1088 | result_valobj_sp = ValueObjectConstResult::Create (NULL, error); |
Greg Clayton | d171972 | 2010-10-05 03:13:51 +0000 | [diff] [blame] | 1089 | |
Jim Ingham | 360f53f | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 1090 | return execution_results; |
Johnny Chen | b4c0f02 | 2010-10-29 20:19:44 +0000 | [diff] [blame] | 1091 | } |