Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 1 | //===-- UserExpression.cpp ---------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #if HAVE_SYS_TYPES_H |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 12 | #include <sys/types.h> |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 13 | #endif |
| 14 | |
| 15 | #include <cstdlib> |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 16 | #include <map> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 17 | #include <string> |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 18 | |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 19 | #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 20 | #include "lldb/Core/ConstString.h" |
| 21 | #include "lldb/Core/Log.h" |
| 22 | #include "lldb/Core/Module.h" |
| 23 | #include "lldb/Core/StreamFile.h" |
| 24 | #include "lldb/Core/StreamString.h" |
| 25 | #include "lldb/Core/ValueObjectConstResult.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 26 | #include "lldb/Expression/DiagnosticManager.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 27 | #include "lldb/Expression/ExpressionSourceCode.h" |
| 28 | #include "lldb/Expression/IRExecutionUnit.h" |
| 29 | #include "lldb/Expression/IRInterpreter.h" |
| 30 | #include "lldb/Expression/Materializer.h" |
| 31 | #include "lldb/Expression/UserExpression.h" |
| 32 | #include "lldb/Host/HostInfo.h" |
| 33 | #include "lldb/Symbol/Block.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 34 | #include "lldb/Symbol/Function.h" |
| 35 | #include "lldb/Symbol/ObjectFile.h" |
| 36 | #include "lldb/Symbol/SymbolVendor.h" |
| 37 | #include "lldb/Symbol/Type.h" |
Sean Callanan | 8f1f9a1 | 2015-09-30 19:57:57 +0000 | [diff] [blame] | 38 | #include "lldb/Symbol/TypeSystem.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 39 | #include "lldb/Symbol/VariableList.h" |
| 40 | #include "lldb/Target/ExecutionContext.h" |
| 41 | #include "lldb/Target/Process.h" |
| 42 | #include "lldb/Target/StackFrame.h" |
| 43 | #include "lldb/Target/Target.h" |
| 44 | #include "lldb/Target/ThreadPlan.h" |
| 45 | #include "lldb/Target/ThreadPlanCallUserExpression.h" |
| 46 | |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 47 | using namespace lldb_private; |
| 48 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | UserExpression::UserExpression(ExecutionContextScope &exe_scope, |
Zachary Turner | c5d7df9 | 2016-11-08 04:52:16 +0000 | [diff] [blame] | 50 | llvm::StringRef expr, llvm::StringRef prefix, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | lldb::LanguageType language, |
| 52 | ResultType desired_type, |
| 53 | const EvaluateExpressionOptions &options) |
Zachary Turner | c5d7df9 | 2016-11-08 04:52:16 +0000 | [diff] [blame] | 54 | : Expression(exe_scope), m_expr_text(expr), m_expr_prefix(prefix), |
| 55 | m_language(language), m_desired_type(desired_type), m_options(options) {} |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | |
| 57 | UserExpression::~UserExpression() {} |
| 58 | |
| 59 | void UserExpression::InstallContext(ExecutionContext &exe_ctx) { |
| 60 | m_jit_process_wp = exe_ctx.GetProcessSP(); |
| 61 | |
| 62 | lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP(); |
| 63 | |
| 64 | if (frame_sp) |
| 65 | m_address = frame_sp->GetFrameCodeAddress(); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 68 | bool UserExpression::LockAndCheckContext(ExecutionContext &exe_ctx, |
| 69 | lldb::TargetSP &target_sp, |
| 70 | lldb::ProcessSP &process_sp, |
| 71 | lldb::StackFrameSP &frame_sp) { |
| 72 | lldb::ProcessSP expected_process_sp = m_jit_process_wp.lock(); |
| 73 | process_sp = exe_ctx.GetProcessSP(); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 74 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 75 | if (process_sp != expected_process_sp) |
| 76 | return false; |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 77 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 78 | process_sp = exe_ctx.GetProcessSP(); |
| 79 | target_sp = exe_ctx.GetTargetSP(); |
| 80 | frame_sp = exe_ctx.GetFrameSP(); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 81 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | if (m_address.IsValid()) { |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 83 | if (!frame_sp) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | return false; |
| 85 | else |
| 86 | return (0 == Address::CompareLoadAddress(m_address, |
| 87 | frame_sp->GetFrameCodeAddress(), |
| 88 | target_sp.get())); |
| 89 | } |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 90 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | return true; |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 94 | bool UserExpression::MatchesContext(ExecutionContext &exe_ctx) { |
| 95 | lldb::TargetSP target_sp; |
| 96 | lldb::ProcessSP process_sp; |
| 97 | lldb::StackFrameSP frame_sp; |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 98 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 99 | return LockAndCheckContext(exe_ctx, target_sp, process_sp, frame_sp); |
| 100 | } |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 102 | lldb::addr_t UserExpression::GetObjectPointer(lldb::StackFrameSP frame_sp, |
| 103 | ConstString &object_name, |
| 104 | Error &err) { |
| 105 | err.Clear(); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 106 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | if (!frame_sp) { |
| 108 | err.SetErrorStringWithFormat( |
| 109 | "Couldn't load '%s' because the context is incomplete", |
| 110 | object_name.AsCString()); |
| 111 | return LLDB_INVALID_ADDRESS; |
| 112 | } |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 113 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | lldb::VariableSP var_sp; |
| 115 | lldb::ValueObjectSP valobj_sp; |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 116 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 117 | valobj_sp = frame_sp->GetValueForVariableExpressionPath( |
| 118 | object_name.AsCString(), lldb::eNoDynamicValues, |
| 119 | StackFrame::eExpressionPathOptionCheckPtrVsMember | |
| 120 | StackFrame::eExpressionPathOptionsNoFragileObjcIvar | |
| 121 | StackFrame::eExpressionPathOptionsNoSyntheticChildren | |
| 122 | StackFrame::eExpressionPathOptionsNoSyntheticArrayRange, |
| 123 | var_sp, err); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 124 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 125 | if (!err.Success() || !valobj_sp.get()) |
| 126 | return LLDB_INVALID_ADDRESS; |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 127 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 128 | lldb::addr_t ret = valobj_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 129 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 130 | if (ret == LLDB_INVALID_ADDRESS) { |
| 131 | err.SetErrorStringWithFormat( |
| 132 | "Couldn't load '%s' because its value couldn't be evaluated", |
| 133 | object_name.AsCString()); |
| 134 | return LLDB_INVALID_ADDRESS; |
| 135 | } |
Dawn Perchik | 1bbaede | 2015-10-07 22:01:12 +0000 | [diff] [blame] | 136 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 137 | return ret; |
| 138 | } |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 139 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 140 | lldb::ExpressionResults UserExpression::Evaluate( |
| 141 | ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options, |
Zachary Turner | c5d7df9 | 2016-11-08 04:52:16 +0000 | [diff] [blame] | 142 | llvm::StringRef expr, llvm::StringRef prefix, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 143 | lldb::ValueObjectSP &result_valobj_sp, Error &error, uint32_t line_offset, |
| 144 | std::string *fixed_expression, lldb::ModuleSP *jit_module_sp_ptr) { |
| 145 | Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS | |
| 146 | LIBLLDB_LOG_STEP)); |
| 147 | |
| 148 | lldb_private::ExecutionPolicy execution_policy = options.GetExecutionPolicy(); |
| 149 | lldb::LanguageType language = options.GetLanguage(); |
| 150 | const ResultType desired_type = options.DoesCoerceToId() |
| 151 | ? UserExpression::eResultTypeId |
| 152 | : UserExpression::eResultTypeAny; |
| 153 | lldb::ExpressionResults execution_results = lldb::eExpressionSetupError; |
| 154 | |
| 155 | Target *target = exe_ctx.GetTargetPtr(); |
| 156 | if (!target) { |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 157 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 158 | log->Printf("== [UserExpression::Evaluate] Passed a NULL target, can't " |
| 159 | "run expressions."); |
Jim Ingham | 0298082 | 2016-11-07 22:47:01 +0000 | [diff] [blame] | 160 | error.SetErrorString("expression passed a null target"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 161 | return lldb::eExpressionSetupError; |
| 162 | } |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 163 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 164 | Process *process = exe_ctx.GetProcessPtr(); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 165 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 166 | if (process == NULL || process->GetState() != lldb::eStateStopped) { |
| 167 | if (execution_policy == eExecutionPolicyAlways) { |
| 168 | if (log) |
| 169 | log->Printf("== [UserExpression::Evaluate] Expression may not run, but " |
| 170 | "is not constant =="); |
| 171 | |
| 172 | error.SetErrorString("expression needed to run but couldn't"); |
| 173 | |
| 174 | return execution_results; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if (process == NULL || !process->CanJIT()) |
| 179 | execution_policy = eExecutionPolicyNever; |
| 180 | |
| 181 | // We need to set the expression execution thread here, turns out parse can |
| 182 | // call functions in the process of |
| 183 | // looking up symbols, which will escape the context set by exe_ctx passed to |
| 184 | // Execute. |
| 185 | lldb::ThreadSP thread_sp = exe_ctx.GetThreadSP(); |
| 186 | ThreadList::ExpressionExecutionThreadPusher execution_thread_pusher( |
| 187 | thread_sp); |
| 188 | |
Zachary Turner | c5d7df9 | 2016-11-08 04:52:16 +0000 | [diff] [blame] | 189 | llvm::StringRef full_prefix; |
| 190 | llvm::StringRef option_prefix(options.GetPrefix()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 191 | std::string full_prefix_storage; |
Zachary Turner | c5d7df9 | 2016-11-08 04:52:16 +0000 | [diff] [blame] | 192 | if (!prefix.empty() && !option_prefix.empty()) { |
| 193 | full_prefix_storage = prefix; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 194 | full_prefix_storage.append(option_prefix); |
Zachary Turner | c5d7df9 | 2016-11-08 04:52:16 +0000 | [diff] [blame] | 195 | full_prefix = full_prefix_storage; |
| 196 | } else if (!prefix.empty()) |
| 197 | full_prefix = prefix; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 198 | else |
| 199 | full_prefix = option_prefix; |
| 200 | |
| 201 | // If the language was not specified in the expression command, |
| 202 | // set it to the language in the target's properties if |
| 203 | // specified, else default to the langage for the frame. |
| 204 | if (language == lldb::eLanguageTypeUnknown) { |
| 205 | if (target->GetLanguage() != lldb::eLanguageTypeUnknown) |
| 206 | language = target->GetLanguage(); |
| 207 | else if (StackFrame *frame = exe_ctx.GetFramePtr()) |
| 208 | language = frame->GetLanguage(); |
| 209 | } |
| 210 | |
| 211 | lldb::UserExpressionSP user_expression_sp( |
Zachary Turner | c5d7df9 | 2016-11-08 04:52:16 +0000 | [diff] [blame] | 212 | target->GetUserExpressionForLanguage(expr, full_prefix, language, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 213 | desired_type, options, error)); |
| 214 | if (error.Fail()) { |
| 215 | if (log) |
| 216 | log->Printf("== [UserExpression::Evaluate] Getting expression: %s ==", |
| 217 | error.AsCString()); |
| 218 | return lldb::eExpressionSetupError; |
| 219 | } |
| 220 | |
| 221 | if (log) |
| 222 | log->Printf("== [UserExpression::Evaluate] Parsing expression %s ==", |
Zachary Turner | c5d7df9 | 2016-11-08 04:52:16 +0000 | [diff] [blame] | 223 | expr.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 224 | |
| 225 | const bool keep_expression_in_memory = true; |
| 226 | const bool generate_debug_info = options.GetGenerateDebugInfo(); |
| 227 | |
| 228 | if (options.InvokeCancelCallback(lldb::eExpressionEvaluationParse)) { |
| 229 | error.SetErrorString("expression interrupted by callback before parse"); |
| 230 | result_valobj_sp = ValueObjectConstResult::Create( |
| 231 | exe_ctx.GetBestExecutionContextScope(), error); |
| 232 | return lldb::eExpressionInterrupted; |
| 233 | } |
| 234 | |
| 235 | DiagnosticManager diagnostic_manager; |
| 236 | |
| 237 | bool parse_success = |
| 238 | user_expression_sp->Parse(diagnostic_manager, exe_ctx, execution_policy, |
| 239 | keep_expression_in_memory, generate_debug_info); |
| 240 | |
| 241 | // Calculate the fixed expression always, since we need it for errors. |
| 242 | std::string tmp_fixed_expression; |
| 243 | if (fixed_expression == nullptr) |
| 244 | fixed_expression = &tmp_fixed_expression; |
| 245 | |
| 246 | const char *fixed_text = user_expression_sp->GetFixedText(); |
| 247 | if (fixed_text != nullptr) |
| 248 | fixed_expression->append(fixed_text); |
| 249 | |
| 250 | // If there is a fixed expression, try to parse it: |
| 251 | if (!parse_success) { |
| 252 | execution_results = lldb::eExpressionParseError; |
| 253 | if (fixed_expression && !fixed_expression->empty() && |
| 254 | options.GetAutoApplyFixIts()) { |
| 255 | lldb::UserExpressionSP fixed_expression_sp( |
| 256 | target->GetUserExpressionForLanguage(fixed_expression->c_str(), |
| 257 | full_prefix, language, |
| 258 | desired_type, options, error)); |
| 259 | DiagnosticManager fixed_diagnostic_manager; |
| 260 | parse_success = fixed_expression_sp->Parse( |
| 261 | fixed_diagnostic_manager, exe_ctx, execution_policy, |
| 262 | keep_expression_in_memory, generate_debug_info); |
| 263 | if (parse_success) { |
| 264 | diagnostic_manager.Clear(); |
| 265 | user_expression_sp = fixed_expression_sp; |
| 266 | } else { |
| 267 | // If the fixed expression failed to parse, don't tell the user about, |
| 268 | // that won't help. |
| 269 | fixed_expression->clear(); |
| 270 | } |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 273 | if (!parse_success) { |
| 274 | if (!fixed_expression->empty() && target->GetEnableNotifyAboutFixIts()) { |
| 275 | error.SetExpressionErrorWithFormat( |
| 276 | execution_results, |
| 277 | "expression failed to parse, fixed expression suggested:\n %s", |
| 278 | fixed_expression->c_str()); |
| 279 | } else { |
| 280 | if (!diagnostic_manager.Diagnostics().size()) |
| 281 | error.SetExpressionError(execution_results, |
| 282 | "expression failed to parse, unknown error"); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 283 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 284 | error.SetExpressionError(execution_results, |
| 285 | diagnostic_manager.GetString().c_str()); |
| 286 | } |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 287 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 288 | } |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 289 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 290 | if (parse_success) { |
| 291 | // If a pointer to a lldb::ModuleSP was passed in, return the JIT'ed module |
| 292 | // if one was created |
| 293 | if (jit_module_sp_ptr) |
| 294 | *jit_module_sp_ptr = user_expression_sp->GetJITModule(); |
| 295 | |
| 296 | lldb::ExpressionVariableSP expr_result; |
| 297 | |
| 298 | if (execution_policy == eExecutionPolicyNever && |
| 299 | !user_expression_sp->CanInterpret()) { |
| 300 | if (log) |
| 301 | log->Printf("== [UserExpression::Evaluate] Expression may not run, but " |
| 302 | "is not constant =="); |
| 303 | |
| 304 | if (!diagnostic_manager.Diagnostics().size()) |
| 305 | error.SetExpressionError(lldb::eExpressionSetupError, |
| 306 | "expression needed to run but couldn't"); |
| 307 | } else if (execution_policy == eExecutionPolicyTopLevel) { |
| 308 | error.SetError(UserExpression::kNoResult, lldb::eErrorTypeGeneric); |
| 309 | return lldb::eExpressionCompleted; |
| 310 | } else { |
| 311 | if (options.InvokeCancelCallback(lldb::eExpressionEvaluationExecution)) { |
| 312 | error.SetExpressionError( |
| 313 | lldb::eExpressionInterrupted, |
| 314 | "expression interrupted by callback before execution"); |
| 315 | result_valobj_sp = ValueObjectConstResult::Create( |
| 316 | exe_ctx.GetBestExecutionContextScope(), error); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 317 | return lldb::eExpressionInterrupted; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 318 | } |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 319 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 320 | diagnostic_manager.Clear(); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 321 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 322 | if (log) |
| 323 | log->Printf("== [UserExpression::Evaluate] Executing expression =="); |
| 324 | |
| 325 | execution_results = |
| 326 | user_expression_sp->Execute(diagnostic_manager, exe_ctx, options, |
| 327 | user_expression_sp, expr_result); |
| 328 | |
| 329 | if (execution_results != lldb::eExpressionCompleted) { |
| 330 | if (log) |
| 331 | log->Printf("== [UserExpression::Evaluate] Execution completed " |
| 332 | "abnormally =="); |
| 333 | |
| 334 | if (!diagnostic_manager.Diagnostics().size()) |
| 335 | error.SetExpressionError( |
| 336 | execution_results, "expression failed to execute, unknown error"); |
| 337 | else |
| 338 | error.SetExpressionError(execution_results, |
| 339 | diagnostic_manager.GetString().c_str()); |
| 340 | } else { |
| 341 | if (expr_result) { |
| 342 | result_valobj_sp = expr_result->GetValueObject(); |
| 343 | |
| 344 | if (log) |
| 345 | log->Printf("== [UserExpression::Evaluate] Execution completed " |
| 346 | "normally with result %s ==", |
| 347 | result_valobj_sp->GetValueAsCString()); |
| 348 | } else { |
| 349 | if (log) |
| 350 | log->Printf("== [UserExpression::Evaluate] Execution completed " |
| 351 | "normally with no result =="); |
| 352 | |
| 353 | error.SetError(UserExpression::kNoResult, lldb::eErrorTypeGeneric); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | if (options.InvokeCancelCallback(lldb::eExpressionEvaluationComplete)) { |
| 360 | error.SetExpressionError( |
| 361 | lldb::eExpressionInterrupted, |
| 362 | "expression interrupted by callback after complete"); |
| 363 | return lldb::eExpressionInterrupted; |
| 364 | } |
| 365 | |
| 366 | if (result_valobj_sp.get() == NULL) { |
| 367 | result_valobj_sp = ValueObjectConstResult::Create( |
| 368 | exe_ctx.GetBestExecutionContextScope(), error); |
| 369 | } |
| 370 | |
| 371 | return execution_results; |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 372 | } |
Jim Ingham | ff7ac6a | 2016-04-12 17:17:35 +0000 | [diff] [blame] | 373 | |
| 374 | lldb::ExpressionResults |
| 375 | UserExpression::Execute(DiagnosticManager &diagnostic_manager, |
| 376 | ExecutionContext &exe_ctx, |
| 377 | const EvaluateExpressionOptions &options, |
| 378 | lldb::UserExpressionSP &shared_ptr_to_me, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 379 | lldb::ExpressionVariableSP &result_var) { |
| 380 | lldb::ExpressionResults expr_result = DoExecute( |
| 381 | diagnostic_manager, exe_ctx, options, shared_ptr_to_me, result_var); |
| 382 | Target *target = exe_ctx.GetTargetPtr(); |
| 383 | if (options.GetResultIsInternal() && result_var && target) { |
| 384 | target->GetPersistentExpressionStateForLanguage(m_language) |
| 385 | ->RemovePersistentVariable(result_var); |
| 386 | } |
| 387 | return expr_result; |
Jim Ingham | ff7ac6a | 2016-04-12 17:17:35 +0000 | [diff] [blame] | 388 | } |