1 //===-- ClangUserExpression.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
13 # include <sys/types.h>
21 #include "lldb/Core/ConstString.h"
22 #include "lldb/Core/Log.h"
23 #include "lldb/Core/StreamFile.h"
24 #include "lldb/Core/StreamString.h"
25 #include "lldb/Core/ValueObjectConstResult.h"
26 #include "lldb/Expression/ASTResultSynthesizer.h"
27 #include "lldb/Expression/ClangExpressionDeclMap.h"
28 #include "lldb/Expression/ClangExpressionParser.h"
29 #include "lldb/Expression/ClangFunction.h"
30 #include "lldb/Expression/ClangUserExpression.h"
31 #include "lldb/Expression/ExpressionSourceCode.h"
32 #include "lldb/Expression/IRExecutionUnit.h"
33 #include "lldb/Expression/IRInterpreter.h"
34 #include "lldb/Expression/Materializer.h"
35 #include "lldb/Host/Host.h"
36 #include "lldb/Symbol/Block.h"
37 #include "lldb/Symbol/ClangASTContext.h"
38 #include "lldb/Symbol/Function.h"
39 #include "lldb/Symbol/Type.h"
40 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
41 #include "lldb/Symbol/VariableList.h"
42 #include "lldb/Target/ExecutionContext.h"
43 #include "lldb/Target/Process.h"
44 #include "lldb/Target/StackFrame.h"
45 #include "lldb/Target/Target.h"
46 #include "lldb/Target/ThreadPlan.h"
47 #include "lldb/Target/ThreadPlanCallUserExpression.h"
49 #include "clang/AST/DeclCXX.h"
50 #include "clang/AST/DeclObjC.h"
52 using namespace lldb_private;
54 ClangUserExpression::ClangUserExpression (const char *expr,
55 const char *expr_prefix,
56 lldb::LanguageType language,
57 ResultType desired_type) :
59 m_stack_frame_bottom (LLDB_INVALID_ADDRESS),
60 m_stack_frame_top (LLDB_INVALID_ADDRESS),
62 m_expr_prefix (expr_prefix ? expr_prefix : ""),
63 m_language (language),
64 m_transformed_text (),
65 m_desired_type (desired_type),
66 m_enforce_valid_object (true),
69 m_static_method(false),
70 m_needs_object_ptr (false),
71 m_const_object (false),
73 m_can_interpret (false),
74 m_materialized_address (LLDB_INVALID_ADDRESS)
78 case lldb::eLanguageTypeC_plus_plus:
81 case lldb::eLanguageTypeObjC:
84 case lldb::eLanguageTypeObjC_plus_plus:
92 ClangUserExpression::~ClangUserExpression ()
97 ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
99 ClangASTContext *clang_ast_context = m_target->GetScratchClangASTContext();
101 if (!clang_ast_context)
104 if (!m_result_synthesizer.get())
105 m_result_synthesizer.reset(new ASTResultSynthesizer(passthrough,
108 return m_result_synthesizer.get();
112 ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Error &err)
114 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
117 log->Printf("ClangUserExpression::ScanContext()");
119 m_target = exe_ctx.GetTargetPtr();
121 if (!(m_allow_cxx || m_allow_objc))
124 log->Printf(" [CUE::SC] Settings inhibit C++ and Objective-C");
128 StackFrame *frame = exe_ctx.GetFramePtr();
132 log->Printf(" [CUE::SC] Null stack frame");
136 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | lldb::eSymbolContextBlock);
138 if (!sym_ctx.function)
141 log->Printf(" [CUE::SC] Null function");
145 // Find the block that defines the function represented by "sym_ctx"
146 Block *function_block = sym_ctx.GetFunctionBlock();
151 log->Printf(" [CUE::SC] Null function block");
155 clang::DeclContext *decl_context = function_block->GetClangDeclContext();
160 log->Printf(" [CUE::SC] Null decl context");
164 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context))
166 if (m_allow_cxx && method_decl->isInstance())
168 if (m_enforce_valid_object)
170 lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true));
172 const char *thisErrorString = "Stopped in a C++ method, but 'this' isn't available; pretending we are in a generic context";
174 if (!variable_list_sp)
176 err.SetErrorString(thisErrorString);
180 lldb::VariableSP this_var_sp (variable_list_sp->FindVariable(ConstString("this")));
183 !this_var_sp->IsInScope(frame) ||
184 !this_var_sp->LocationIsValidForFrame (frame))
186 err.SetErrorString(thisErrorString);
192 m_needs_object_ptr = true;
195 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context))
199 if (m_enforce_valid_object)
201 lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true));
203 const char *selfErrorString = "Stopped in an Objective-C method, but 'self' isn't available; pretending we are in a generic context";
205 if (!variable_list_sp)
207 err.SetErrorString(selfErrorString);
211 lldb::VariableSP self_variable_sp = variable_list_sp->FindVariable(ConstString("self"));
213 if (!self_variable_sp ||
214 !self_variable_sp->IsInScope(frame) ||
215 !self_variable_sp->LocationIsValidForFrame (frame))
217 err.SetErrorString(selfErrorString);
223 m_needs_object_ptr = true;
225 if (!method_decl->isInstanceMethod())
226 m_static_method = true;
229 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_context))
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.
236 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (&decl_context->getParentASTContext(), function_decl);
237 if (metadata && metadata->HasObjectPtr())
239 lldb::LanguageType language = metadata->GetObjectPtrLanguage();
240 if (language == lldb::eLanguageTypeC_plus_plus)
242 if (m_enforce_valid_object)
244 lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true));
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";
248 if (!variable_list_sp)
250 err.SetErrorString(thisErrorString);
254 lldb::VariableSP this_var_sp (variable_list_sp->FindVariable(ConstString("this")));
257 !this_var_sp->IsInScope(frame) ||
258 !this_var_sp->LocationIsValidForFrame (frame))
260 err.SetErrorString(thisErrorString);
266 m_needs_object_ptr = true;
268 else if (language == lldb::eLanguageTypeObjC)
270 if (m_enforce_valid_object)
272 lldb::VariableListSP variable_list_sp (function_block->GetBlockVariableList (true));
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";
276 if (!variable_list_sp)
278 err.SetErrorString(selfErrorString);
282 lldb::VariableSP self_variable_sp = variable_list_sp->FindVariable(ConstString("self"));
284 if (!self_variable_sp ||
285 !self_variable_sp->IsInScope(frame) ||
286 !self_variable_sp->LocationIsValidForFrame (frame))
288 err.SetErrorString(selfErrorString);
292 Type *self_type = self_variable_sp->GetType();
296 err.SetErrorString(selfErrorString);
300 ClangASTType self_clang_type = self_type->GetClangForwardType();
302 if (!self_clang_type)
304 err.SetErrorString(selfErrorString);
308 if (self_clang_type.IsObjCClassType())
312 else if (self_clang_type.IsObjCObjectPointerType())
315 m_needs_object_ptr = true;
319 err.SetErrorString(selfErrorString);
326 m_needs_object_ptr = true;
334 ClangUserExpression::InstallContext (ExecutionContext &exe_ctx)
336 m_process_wp = exe_ctx.GetProcessSP();
338 lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP();
341 m_address = frame_sp->GetFrameCodeAddress();
345 ClangUserExpression::LockAndCheckContext (ExecutionContext &exe_ctx,
346 lldb::TargetSP &target_sp,
347 lldb::ProcessSP &process_sp,
348 lldb::StackFrameSP &frame_sp)
350 lldb::ProcessSP expected_process_sp = m_process_wp.lock();
351 process_sp = exe_ctx.GetProcessSP();
353 if (process_sp != expected_process_sp)
356 process_sp = exe_ctx.GetProcessSP();
357 target_sp = exe_ctx.GetTargetSP();
358 frame_sp = exe_ctx.GetFrameSP();
360 if (m_address.IsValid())
365 return (0 == Address::CompareLoadAddress(m_address, frame_sp->GetFrameCodeAddress(), target_sp.get()));
372 ClangUserExpression::MatchesContext (ExecutionContext &exe_ctx)
374 lldb::TargetSP target_sp;
375 lldb::ProcessSP process_sp;
376 lldb::StackFrameSP frame_sp;
378 return LockAndCheckContext(exe_ctx, target_sp, process_sp, frame_sp);
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.
386 ApplyObjcCastHack(std::string &expr)
388 #define OBJC_CAST_HACK_FROM "(int)["
389 #define OBJC_CAST_HACK_TO "(int)(long long)["
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);
396 #undef OBJC_CAST_HACK_TO
397 #undef OBJC_CAST_HACK_FROM
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.
406 ApplyUnicharHack(std::string &expr)
408 #define UNICHAR_HACK_FROM "unichar"
409 #define UNICHAR_HACK_TO "unsigned short"
413 while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
414 expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
416 #undef UNICHAR_HACK_TO
417 #undef UNICHAR_HACK_FROM
421 ClangUserExpression::Parse (Stream &error_stream,
422 ExecutionContext &exe_ctx,
423 lldb_private::ExecutionPolicy execution_policy,
424 bool keep_result_in_memory)
426 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
430 InstallContext(exe_ctx);
432 ScanContext(exe_ctx, err);
436 error_stream.Printf("warning: %s\n", err.AsCString());
439 StreamString m_transformed_stream;
441 ////////////////////////////////////
442 // Generate the expression
445 ApplyObjcCastHack(m_expr_text);
446 //ApplyUnicharHack(m_expr_text);
448 std::unique_ptr<ExpressionSourceCode> source_code (ExpressionSourceCode::CreateWrapped(m_expr_prefix.c_str(), m_expr_text.c_str()));
450 lldb::LanguageType lang_type;
453 lang_type = lldb::eLanguageTypeC_plus_plus;
454 else if(m_objectivec)
455 lang_type = lldb::eLanguageTypeObjC;
457 lang_type = lldb::eLanguageTypeC;
459 if (!source_code->GetText(m_transformed_text, lang_type, m_const_object, m_static_method))
461 error_stream.PutCString ("error: couldn't construct expression body");
466 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
468 ////////////////////////////////////
469 // Set up the target and compiler
472 Target *target = exe_ctx.GetTargetPtr();
476 error_stream.PutCString ("error: invalid target\n");
480 //////////////////////////
481 // Parse the expression
484 m_materializer_ap.reset(new Materializer());
486 m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory, exe_ctx));
491 typedef std::function <void (void)> Callback;
493 OnExit (Callback const &callback) :
506 OnExit on_exit([this]() { m_expr_decl_map.reset(); });
508 if (!m_expr_decl_map->WillParse(exe_ctx, m_materializer_ap.get()))
510 error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
514 Process *process = exe_ctx.GetProcessPtr();
515 ExecutionContextScope *exe_scope = process;
518 exe_scope = exe_ctx.GetTargetPtr();
520 ClangExpressionParser parser(exe_scope, *this);
522 unsigned num_errors = parser.Parse (error_stream);
526 error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
528 m_expr_decl_map->DidParse();
533 //////////////////////////////////////////////////////////////////////////////////////////
534 // Prepare the output of the parser for execution, evaluating it statically if possible
537 Error jit_error = parser.PrepareForExecution (m_jit_start_addr,
544 if (jit_error.Success())
546 if (process && m_jit_start_addr != LLDB_INVALID_ADDRESS)
547 m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
552 const char *error_cstr = jit_error.AsCString();
553 if (error_cstr && error_cstr[0])
554 error_stream.Printf ("error: %s\n", error_cstr);
556 error_stream.Printf ("error: expression can't be interpreted or run\n");
562 GetObjectPointer (lldb::StackFrameSP frame_sp,
563 ConstString &object_name,
570 err.SetErrorStringWithFormat("Couldn't load '%s' because the context is incomplete", object_name.AsCString());
571 return LLDB_INVALID_ADDRESS;
574 lldb::VariableSP var_sp;
575 lldb::ValueObjectSP valobj_sp;
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,
588 return LLDB_INVALID_ADDRESS;
590 lldb::addr_t ret = valobj_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
592 if (ret == LLDB_INVALID_ADDRESS)
594 err.SetErrorStringWithFormat("Couldn't load '%s' because its value couldn't be evaluated", object_name.AsCString());
595 return LLDB_INVALID_ADDRESS;
602 ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
603 ExecutionContext &exe_ctx,
604 lldb::addr_t &struct_address,
605 lldb::addr_t &object_ptr,
606 lldb::addr_t &cmd_ptr)
608 lldb::TargetSP target;
609 lldb::ProcessSP process;
610 lldb::StackFrameSP frame;
612 if (!LockAndCheckContext(exe_ctx,
617 error_stream.Printf("The context has changed before we could JIT the expression!\n");
621 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret)
623 if (m_needs_object_ptr)
625 ConstString object_name;
629 object_name.SetCString("this");
631 else if (m_objectivec)
633 object_name.SetCString("self");
637 error_stream.Printf("Need object pointer but don't know the language\n");
641 Error object_ptr_error;
643 object_ptr = GetObjectPointer(frame, object_name, object_ptr_error);
645 if (!object_ptr_error.Success())
647 error_stream.Printf("warning: couldn't get required object pointer (substituting NULL): %s\n", object_ptr_error.AsCString());
653 ConstString cmd_name("_cmd");
655 cmd_ptr = GetObjectPointer(frame, cmd_name, object_ptr_error);
657 if (!object_ptr_error.Success())
659 error_stream.Printf("warning: couldn't get cmd pointer (substituting NULL): %s\n", object_ptr_error.AsCString());
665 if (m_materialized_address == LLDB_INVALID_ADDRESS)
669 IRMemoryMap::AllocationPolicy policy = m_can_interpret ? IRMemoryMap::eAllocationPolicyHostOnly : IRMemoryMap::eAllocationPolicyMirror;
671 m_materialized_address = m_execution_unit_ap->Malloc(m_materializer_ap->GetStructByteSize(),
672 m_materializer_ap->GetStructAlignment(),
673 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
677 if (!alloc_error.Success())
679 error_stream.Printf("Couldn't allocate space for materialized struct: %s\n", alloc_error.AsCString());
684 struct_address = m_materialized_address;
686 if (m_can_interpret && m_stack_frame_bottom == LLDB_INVALID_ADDRESS)
690 const size_t stack_frame_size = 512 * 1024;
692 m_stack_frame_bottom = m_execution_unit_ap->Malloc(stack_frame_size,
694 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
695 IRMemoryMap::eAllocationPolicyHostOnly,
698 m_stack_frame_top = m_stack_frame_bottom + stack_frame_size;
700 if (!alloc_error.Success())
702 error_stream.Printf("Couldn't allocate space for the stack frame: %s\n", alloc_error.AsCString());
707 Error materialize_error;
709 m_dematerializer_sp = m_materializer_ap->Materialize(frame, *m_execution_unit_ap, struct_address, materialize_error);
711 if (!materialize_error.Success())
713 error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
721 ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
722 ExecutionContext &exe_ctx)
724 lldb::addr_t struct_address;
726 lldb::addr_t object_ptr = 0;
727 lldb::addr_t cmd_ptr = 0;
729 PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr);
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
733 // forcing unwind_on_error to be true here, in practical terms that can't happen.
735 const bool stop_others = true;
736 const bool unwind_on_error = true;
737 const bool ignore_breakpoints = false;
738 return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
745 (m_needs_object_ptr ? &object_ptr : NULL),
746 (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL);
750 ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
751 ExecutionContext &exe_ctx,
752 lldb::ClangExpressionVariableSP &result,
753 lldb::addr_t function_stack_bottom,
754 lldb::addr_t function_stack_top)
756 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
759 log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --");
761 if (!m_dematerializer_sp)
763 error_stream.Printf ("Couldn't apply expression side effects : no dematerializer is present");
767 Error dematerialize_error;
769 m_dematerializer_sp->Dematerialize(dematerialize_error, result, function_stack_bottom, function_stack_top);
771 if (!dematerialize_error.Success())
773 error_stream.Printf ("Couldn't apply expression side effects : %s\n", dematerialize_error.AsCString("unknown error"));
778 result->TransferAddress();
780 m_dematerializer_sp.reset();
786 ClangUserExpression::Execute (Stream &error_stream,
787 ExecutionContext &exe_ctx,
788 bool unwind_on_error,
789 bool ignore_breakpoints,
790 ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
791 lldb::ClangExpressionVariableSP &result,
793 uint32_t timeout_usec)
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.
797 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
799 if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret)
801 lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
803 lldb::addr_t object_ptr = 0;
804 lldb::addr_t cmd_ptr = 0;
806 if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr))
808 error_stream.Printf("Errored out in %s, couldn't PrepareToExecuteJITExpression", __FUNCTION__);
809 return eExecutionSetupError;
812 lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS;
813 lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS;
817 llvm::Module *module = m_execution_unit_ap->GetModule();
818 llvm::Function *function = m_execution_unit_ap->GetFunction();
820 if (!module || !function)
822 error_stream.Printf("Supposed to interpret, but nothing is there");
823 return eExecutionSetupError;
826 Error interpreter_error;
828 llvm::SmallVector <lldb::addr_t, 3> args;
830 if (m_needs_object_ptr)
832 args.push_back(object_ptr);
835 args.push_back(cmd_ptr);
838 args.push_back(struct_address);
840 function_stack_bottom = m_stack_frame_bottom;
841 function_stack_top = m_stack_frame_top;
843 IRInterpreter::Interpret (*module,
846 *m_execution_unit_ap.get(),
848 function_stack_bottom,
851 if (!interpreter_error.Success())
853 error_stream.Printf("Supposed to interpret, but failed: %s", interpreter_error.AsCString());
854 return eExecutionDiscarded;
859 const bool stop_others = true;
860 const bool try_all_threads = run_others;
862 Address wrapper_address (m_jit_start_addr);
863 lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (exe_ctx.GetThreadRef(),
869 (m_needs_object_ptr ? &object_ptr : NULL),
870 ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL),
873 if (!call_plan_sp || !call_plan_sp->ValidatePlan (&error_stream))
874 return eExecutionSetupError;
876 lldb::addr_t function_stack_pointer = static_cast<ThreadPlanCallFunction *>(call_plan_sp.get())->GetFunctionStackPointer();
878 function_stack_bottom = function_stack_pointer - Host::GetPageSize();
879 function_stack_top = function_stack_pointer;
882 log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --");
884 if (exe_ctx.GetProcessPtr())
885 exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
887 ExecutionResults execution_result = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx,
896 if (exe_ctx.GetProcessPtr())
897 exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
900 log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --");
902 if (execution_result == eExecutionInterrupted || execution_result == eExecutionHitBreakpoint)
904 const char *error_desc = NULL;
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();
913 error_stream.Printf ("Execution was interrupted, reason: %s.", error_desc);
915 error_stream.Printf ("Execution was interrupted.");
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.");
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.");
923 return execution_result;
925 else if (execution_result != eExecutionCompleted)
927 error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
928 return execution_result;
932 if (FinalizeJITExecution (error_stream, exe_ctx, result, function_stack_bottom, function_stack_top))
934 return eExecutionCompleted;
938 return eExecutionSetupError;
943 error_stream.Printf("Expression can't be run, because there is no JIT compiled function");
944 return eExecutionSetupError;
949 ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
950 lldb_private::ExecutionPolicy execution_policy,
951 lldb::LanguageType language,
952 ResultType desired_type,
953 bool unwind_on_error,
954 bool ignore_breakpoints,
955 const char *expr_cstr,
956 const char *expr_prefix,
957 lldb::ValueObjectSP &result_valobj_sp,
959 uint32_t timeout_usec)
962 return EvaluateWithError (exe_ctx,
977 ClangUserExpression::EvaluateWithError (ExecutionContext &exe_ctx,
978 lldb_private::ExecutionPolicy execution_policy,
979 lldb::LanguageType language,
980 ResultType desired_type,
981 bool unwind_on_error,
982 bool ignore_breakpoints,
983 const char *expr_cstr,
984 const char *expr_prefix,
985 lldb::ValueObjectSP &result_valobj_sp,
988 uint32_t timeout_usec)
990 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
992 ExecutionResults execution_results = eExecutionSetupError;
994 Process *process = exe_ctx.GetProcessPtr();
996 if (process == NULL || process->GetState() != lldb::eStateStopped)
998 if (execution_policy == eExecutionPolicyAlways)
1001 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
1003 error.SetErrorString ("expression needed to run but couldn't");
1005 return execution_results;
1009 if (process == NULL || !process->CanJIT())
1010 execution_policy = eExecutionPolicyNever;
1012 ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix, language, desired_type));
1014 StreamString error_stream;
1017 log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr);
1019 const bool keep_expression_in_memory = true;
1021 if (!user_expression_sp->Parse (error_stream, exe_ctx, execution_policy, keep_expression_in_memory))
1023 if (error_stream.GetString().empty())
1024 error.SetErrorString ("expression failed to parse, unknown error");
1026 error.SetErrorString (error_stream.GetString().c_str());
1030 lldb::ClangExpressionVariableSP expr_result;
1032 if (execution_policy == eExecutionPolicyNever &&
1033 !user_expression_sp->CanInterpret())
1036 log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
1038 if (error_stream.GetString().empty())
1039 error.SetErrorString ("expression needed to run but couldn't");
1043 error_stream.GetString().clear();
1046 log->Printf("== [ClangUserExpression::Evaluate] Executing expression ==");
1048 execution_results = user_expression_sp->Execute (error_stream,
1057 if (execution_results != eExecutionCompleted)
1060 log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally ==");
1062 if (error_stream.GetString().empty())
1063 error.SetErrorString ("expression failed to execute, unknown error");
1065 error.SetErrorString (error_stream.GetString().c_str());
1071 result_valobj_sp = expr_result->GetValueObject();
1074 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString());
1079 log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result ==");
1081 error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
1087 if (result_valobj_sp.get() == NULL)
1088 result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
1090 return execution_results;