ClangUserExpression.cpp revision e1301a62783cf0d5457350b90830917841cbf6fc
1//===-- ClangUserExpression.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// 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"
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/Host/Host.h"
33#include "lldb/Symbol/VariableList.h"
34#include "lldb/Target/ExecutionContext.h"
35#include "lldb/Target/Process.h"
36#include "lldb/Target/StackFrame.h"
37#include "lldb/Target/Target.h"
38#include "lldb/Target/ThreadPlan.h"
39#include "lldb/Target/ThreadPlanCallUserExpression.h"
40
41#include "clang/AST/DeclCXX.h"
42#include "clang/AST/DeclObjC.h"
43
44using namespace lldb_private;
45
46ClangUserExpression::ClangUserExpression (const char *expr,
47                                          const char *expr_prefix,
48                                          lldb::LanguageType language) :
49    ClangExpression (),
50    m_expr_text (expr),
51    m_expr_prefix (expr_prefix ? expr_prefix : ""),
52    m_language (language),
53    m_transformed_text (),
54    m_desired_type (NULL, NULL),
55    m_cplusplus (false),
56    m_objectivec (false),
57    m_needs_object_ptr (false),
58    m_const_object (false),
59    m_static_method(false),
60    m_target (NULL),
61    m_evaluated_statically (false),
62    m_const_result ()
63{
64    switch (m_language)
65    {
66    case lldb::eLanguageTypeC_plus_plus:
67        m_allow_cxx = true;
68        break;
69    case lldb::eLanguageTypeObjC:
70        m_allow_objc = true;
71        break;
72    case lldb::eLanguageTypeObjC_plus_plus:
73    default:
74        m_allow_cxx = true;
75        m_allow_objc = true;
76        break;
77    }
78}
79
80ClangUserExpression::~ClangUserExpression ()
81{
82}
83
84clang::ASTConsumer *
85ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough)
86{
87    ClangASTContext *clang_ast_context = m_target->GetScratchClangASTContext();
88
89    if (!clang_ast_context)
90        return NULL;
91
92    if (!m_result_synthesizer.get())
93        m_result_synthesizer.reset(new ASTResultSynthesizer(passthrough,
94                                                            m_desired_type,
95                                                            *m_target));
96
97    return m_result_synthesizer.get();
98}
99
100void
101ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Error &err)
102{
103    m_target = exe_ctx.GetTargetPtr();
104
105    if (!(m_allow_cxx || m_allow_objc))
106        return;
107
108    StackFrame *frame = exe_ctx.GetFramePtr();
109    if (frame == NULL)
110        return;
111
112    SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction);
113
114    if (!sym_ctx.function)
115        return;
116
117    clang::DeclContext *decl_context;
118
119    if (sym_ctx.block && sym_ctx.block->GetInlinedFunctionInfo())
120        decl_context = sym_ctx.block->GetClangDeclContextForInlinedFunction();
121    else
122        decl_context = sym_ctx.function->GetClangDeclContext();
123
124    if (!decl_context)
125        return;
126
127    if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_context))
128    {
129        if (m_allow_cxx && method_decl->isInstance())
130        {
131            VariableList *vars = frame->GetVariableList(false);
132
133            const char *thisErrorString = "Stopped in a C++ method, but 'this' isn't available; pretending we are in a generic context";
134
135            if (!vars)
136            {
137                err.SetErrorToGenericError();
138                err.SetErrorString(thisErrorString);
139                return;
140            }
141
142            lldb::VariableSP this_var = vars->FindVariable(ConstString("this"));
143
144            if (!this_var ||
145                !this_var->IsInScope(frame) ||
146                !this_var->LocationIsValidForFrame (frame))
147            {
148                err.SetErrorToGenericError();
149                err.SetErrorString(thisErrorString);
150                return;
151            }
152
153            m_cplusplus = true;
154            m_needs_object_ptr = true;
155
156            do {
157                clang::QualType this_type = method_decl->getThisType(decl_context->getParentASTContext());
158
159                const clang::PointerType *this_pointer_type = llvm::dyn_cast<clang::PointerType>(this_type.getTypePtr());
160
161                if (!this_pointer_type)
162                    break;
163
164                clang::QualType this_pointee_type = this_pointer_type->getPointeeType();
165            } while (0);
166        }
167    }
168    else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_context))
169    {
170        if (m_allow_objc)
171        {
172            VariableList *vars = frame->GetVariableList(false);
173
174            const char *selfErrorString = "Stopped in an Objective-C method, but 'self' isn't available; pretending we are in a generic context";
175
176            if (!vars)
177            {
178                err.SetErrorToGenericError();
179                err.SetErrorString(selfErrorString);
180                return;
181            }
182
183            lldb::VariableSP self_var = vars->FindVariable(ConstString("self"));
184
185            if (!self_var ||
186                !self_var->IsInScope(frame) ||
187                !self_var->LocationIsValidForFrame (frame))
188            {
189                err.SetErrorToGenericError();
190                err.SetErrorString(selfErrorString);
191                return;
192            }
193
194            m_objectivec = true;
195            m_needs_object_ptr = true;
196
197            if (!method_decl->isInstanceMethod())
198                m_static_method = true;
199        }
200    }
201}
202
203// This is a really nasty hack, meant to fix Objective-C expressions of the form
204// (int)[myArray count].  Right now, because the type information for count is
205// not available, [myArray count] returns id, which can't be directly cast to
206// int without causing a clang error.
207static void
208ApplyObjcCastHack(std::string &expr)
209{
210#define OBJC_CAST_HACK_FROM "(int)["
211#define OBJC_CAST_HACK_TO   "(int)(long long)["
212
213    size_t from_offset;
214
215    while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
216        expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO);
217
218#undef OBJC_CAST_HACK_TO
219#undef OBJC_CAST_HACK_FROM
220}
221
222// Another hack, meant to allow use of unichar despite it not being available in
223// the type information.  Although we could special-case it in type lookup,
224// hopefully we'll figure out a way to #include the same environment as is
225// present in the original source file rather than try to hack specific type
226// definitions in as needed.
227static void
228ApplyUnicharHack(std::string &expr)
229{
230#define UNICHAR_HACK_FROM "unichar"
231#define UNICHAR_HACK_TO   "unsigned short"
232
233    size_t from_offset;
234
235    while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos)
236        expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO);
237
238#undef UNICHAR_HACK_TO
239#undef UNICHAR_HACK_FROM
240}
241
242bool
243ClangUserExpression::Parse (Stream &error_stream,
244                            ExecutionContext &exe_ctx,
245                            TypeFromUser desired_type,
246                            lldb_private::ExecutionPolicy execution_policy,
247                            bool keep_result_in_memory)
248{
249    lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
250
251    Error err;
252
253    ScanContext(exe_ctx, err);
254
255    if (!err.Success())
256    {
257        error_stream.Printf("warning: %s\n", err.AsCString());
258    }
259
260    StreamString m_transformed_stream;
261
262    ////////////////////////////////////
263    // Generate the expression
264    //
265
266    ApplyObjcCastHack(m_expr_text);
267    //ApplyUnicharHack(m_expr_text);
268
269    std::auto_ptr <ExpressionSourceCode> source_code (ExpressionSourceCode::CreateWrapped(m_expr_prefix.c_str(), m_expr_text.c_str()));
270
271    lldb::LanguageType lang_type;
272
273    if (m_cplusplus)
274        lang_type = lldb::eLanguageTypeC_plus_plus;
275    else if(m_objectivec)
276        lang_type = lldb::eLanguageTypeObjC;
277    else
278        lang_type = lldb::eLanguageTypeC;
279
280    if (!source_code->GetText(m_transformed_text, lang_type, m_const_object, m_static_method))
281    {
282        error_stream.PutCString ("error: couldn't construct expression body");
283        return false;
284    }
285
286    if (log)
287        log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
288
289    ////////////////////////////////////
290    // Set up the target and compiler
291    //
292
293    Target *target = exe_ctx.GetTargetPtr();
294
295    if (!target)
296    {
297        error_stream.PutCString ("error: invalid target\n");
298        return false;
299    }
300
301    //////////////////////////
302    // Parse the expression
303    //
304
305    m_desired_type = desired_type;
306
307    m_expr_decl_map.reset(new ClangExpressionDeclMap(keep_result_in_memory, exe_ctx));
308
309    if (!m_expr_decl_map->WillParse(exe_ctx))
310    {
311        error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
312        return false;
313    }
314
315    Process *process = exe_ctx.GetProcessPtr();
316    ClangExpressionParser parser(process, *this);
317
318    unsigned num_errors = parser.Parse (error_stream);
319
320    if (num_errors)
321    {
322        error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
323
324        m_expr_decl_map->DidParse();
325
326        return false;
327    }
328
329    //////////////////////////////////////////////////////////////////////////////////////////
330    // Prepare the output of the parser for execution, evaluating it statically if possible
331    //
332
333    if (execution_policy != eExecutionPolicyNever && process)
334        m_data_allocator.reset(new ProcessDataAllocator(*process));
335
336    Error jit_error = parser.PrepareForExecution (m_jit_alloc,
337                                                  m_jit_start_addr,
338                                                  m_jit_end_addr,
339                                                  exe_ctx,
340                                                  m_data_allocator.get(),
341                                                  m_evaluated_statically,
342                                                  m_const_result,
343                                                  execution_policy);
344
345    if (log && m_data_allocator.get())
346    {
347        StreamString dump_string;
348        m_data_allocator->Dump(dump_string);
349
350        log->Printf("Data buffer contents:\n%s", dump_string.GetString().c_str());
351    }
352
353    if (jit_error.Success())
354    {
355        if (process && m_jit_alloc != LLDB_INVALID_ADDRESS)
356            m_jit_process_sp = process->GetSP();
357        return true;
358    }
359    else
360    {
361        const char *error_cstr = jit_error.AsCString();
362        if (error_cstr && error_cstr[0])
363            error_stream.Printf ("error: %s\n", error_cstr);
364        else
365            error_stream.Printf ("error: expression can't be interpreted or run\n");
366        return false;
367    }
368}
369
370bool
371ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream,
372                                                    ExecutionContext &exe_ctx,
373                                                    lldb::addr_t &struct_address,
374                                                    lldb::addr_t &object_ptr,
375                                                    lldb::addr_t &cmd_ptr)
376{
377    lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
378
379    if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
380    {
381        Error materialize_error;
382
383        if (m_needs_object_ptr)
384        {
385            ConstString object_name;
386
387            if (m_cplusplus)
388            {
389                object_name.SetCString("this");
390            }
391            else if (m_objectivec)
392            {
393                object_name.SetCString("self");
394            }
395            else
396            {
397                error_stream.Printf("Need object pointer but don't know the language\n");
398                return false;
399            }
400
401            if (!(m_expr_decl_map->GetObjectPointer(object_ptr, object_name, exe_ctx, materialize_error)))
402            {
403                error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
404                return false;
405            }
406
407            if (m_objectivec)
408            {
409                ConstString cmd_name("_cmd");
410
411                if (!(m_expr_decl_map->GetObjectPointer(cmd_ptr, cmd_name, exe_ctx, materialize_error, true)))
412                {
413                    error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString());
414                    return false;
415                }
416            }
417        }
418
419        if (!m_expr_decl_map->Materialize(exe_ctx, struct_address, materialize_error))
420        {
421            error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString());
422            return false;
423        }
424
425#if 0
426		// jingham: look here
427        StreamFile logfile ("/tmp/exprs.txt", "a");
428        logfile.Printf("0x%16.16llx: thread = 0x%4.4x, expr = '%s'\n", m_jit_start_addr, exe_ctx.thread ? exe_ctx.thread->GetID() : -1, m_expr_text.c_str());
429#endif
430
431        if (log)
432        {
433            log->Printf("-- [ClangUserExpression::PrepareToExecuteJITExpression] Materializing for execution --");
434
435            log->Printf("  Function address  : 0x%llx", (uint64_t)m_jit_start_addr);
436
437            if (m_needs_object_ptr)
438                log->Printf("  Object pointer    : 0x%llx", (uint64_t)object_ptr);
439
440            log->Printf("  Structure address : 0x%llx", (uint64_t)struct_address);
441
442            StreamString args;
443
444            Error dump_error;
445
446            if (struct_address)
447            {
448                if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
449                {
450                    log->Printf("  Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
451                }
452                else
453                {
454                    log->Printf("  Structure contents:\n%s", args.GetData());
455                }
456            }
457        }
458    }
459    return true;
460}
461
462ThreadPlan *
463ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream,
464                                                          ExecutionContext &exe_ctx)
465{
466    lldb::addr_t struct_address;
467
468    lldb::addr_t object_ptr = 0;
469    lldb::addr_t cmd_ptr = 0;
470
471    PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr);
472
473    // FIXME: This should really return a ThreadPlanCallUserExpression, in order to make sure that we don't release the
474    // ClangUserExpression resources before the thread plan finishes execution in the target.  But because we are
475    // forcing unwind_on_error to be true here, in practical terms that can't happen.
476
477    return ClangFunction::GetThreadPlanToCallFunction (exe_ctx,
478                                                       m_jit_start_addr,
479                                                       struct_address,
480                                                       error_stream,
481                                                       true,
482                                                       true,
483                                                       (m_needs_object_ptr ? &object_ptr : NULL),
484                                                       (m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL);
485}
486
487bool
488ClangUserExpression::FinalizeJITExecution (Stream &error_stream,
489                                           ExecutionContext &exe_ctx,
490                                           lldb::ClangExpressionVariableSP &result,
491                                           lldb::addr_t function_stack_pointer)
492{
493    Error expr_error;
494
495    lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
496
497    if (log)
498    {
499        log->Printf("-- [ClangUserExpression::FinalizeJITExecution] Dematerializing after execution --");
500
501        StreamString args;
502
503        Error dump_error;
504
505        if (!m_expr_decl_map->DumpMaterializedStruct(exe_ctx, args, dump_error))
506        {
507            log->Printf("  Couldn't extract variable values : %s", dump_error.AsCString("unknown error"));
508        }
509        else
510        {
511            log->Printf("  Structure contents:\n%s", args.GetData());
512        }
513    }
514
515    lldb::addr_t function_stack_bottom = function_stack_pointer - Host::GetPageSize();
516
517
518    if (!m_expr_decl_map->Dematerialize(exe_ctx, result, function_stack_pointer, function_stack_bottom, expr_error))
519    {
520        error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error"));
521        return false;
522    }
523
524    return true;
525}
526
527ExecutionResults
528ClangUserExpression::Execute (Stream &error_stream,
529                              ExecutionContext &exe_ctx,
530                              bool discard_on_error,
531                              ClangUserExpression::ClangUserExpressionSP &shared_ptr_to_me,
532                              lldb::ClangExpressionVariableSP &result)
533{
534    // The expression log is quite verbose, and if you're just tracking the execution of the
535    // expression, it's quite convenient to have these logs come out with the STEP log as well.
536    lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
537
538    if (m_jit_start_addr != LLDB_INVALID_ADDRESS)
539    {
540        lldb::addr_t struct_address;
541
542        lldb::addr_t object_ptr = 0;
543        lldb::addr_t cmd_ptr = 0;
544
545        if (!PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr, cmd_ptr))
546            return eExecutionSetupError;
547
548        const bool stop_others = true;
549        const bool try_all_threads = true;
550
551        Address wrapper_address (NULL, m_jit_start_addr);
552        lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression (exe_ctx.GetThreadRef(),
553                                                                          wrapper_address,
554                                                                          struct_address,
555                                                                          stop_others,
556                                                                          discard_on_error,
557                                                                          (m_needs_object_ptr ? &object_ptr : NULL),
558                                                                          ((m_needs_object_ptr && m_objectivec) ? &cmd_ptr : NULL),
559                                                                          shared_ptr_to_me));
560
561        if (call_plan_sp == NULL || !call_plan_sp->ValidatePlan (NULL))
562            return eExecutionSetupError;
563
564        lldb::addr_t function_stack_pointer = static_cast<ThreadPlanCallFunction *>(call_plan_sp.get())->GetFunctionStackPointer();
565
566        call_plan_sp->SetPrivate(true);
567
568        uint32_t single_thread_timeout_usec = 500000;
569
570        if (log)
571            log->Printf("-- [ClangUserExpression::Execute] Execution of expression begins --");
572
573        if (exe_ctx.GetProcessPtr())
574            exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
575
576        ExecutionResults execution_result = exe_ctx.GetProcessRef().RunThreadPlan (exe_ctx,
577                                                                                   call_plan_sp,
578                                                                                   stop_others,
579                                                                                   try_all_threads,
580                                                                                   discard_on_error,
581                                                                                   single_thread_timeout_usec,
582                                                                                   error_stream);
583
584        if (exe_ctx.GetProcessPtr())
585            exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
586
587        if (log)
588            log->Printf("-- [ClangUserExpression::Execute] Execution of expression completed --");
589
590        if (execution_result == eExecutionInterrupted)
591        {
592            const char *error_desc = NULL;
593
594            if (call_plan_sp)
595            {
596                lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
597                if (real_stop_info_sp)
598                    error_desc = real_stop_info_sp->GetDescription();
599            }
600            if (error_desc)
601                error_stream.Printf ("Execution was interrupted, reason: %s.", error_desc);
602            else
603                error_stream.Printf ("Execution was interrupted.");
604
605            if (discard_on_error)
606                error_stream.Printf ("\nThe process has been returned to the state before execution.");
607            else
608                error_stream.Printf ("\nThe process has been left at the point where it was interrupted.");
609
610            return execution_result;
611        }
612        else if (execution_result != eExecutionCompleted)
613        {
614            error_stream.Printf ("Couldn't execute function; result was %s\n", Process::ExecutionResultAsCString (execution_result));
615            return execution_result;
616        }
617
618        if  (FinalizeJITExecution (error_stream, exe_ctx, result, function_stack_pointer))
619            return eExecutionCompleted;
620        else
621            return eExecutionSetupError;
622    }
623    else
624    {
625        error_stream.Printf("Expression can't be run, because there is no JIT compiled function");
626        return eExecutionSetupError;
627    }
628}
629
630ExecutionResults
631ClangUserExpression::Evaluate (ExecutionContext &exe_ctx,
632                               lldb_private::ExecutionPolicy execution_policy,
633                               lldb::LanguageType language,
634                               bool discard_on_error,
635                               const char *expr_cstr,
636                               const char *expr_prefix,
637                               lldb::ValueObjectSP &result_valobj_sp)
638{
639    Error error;
640    return EvaluateWithError (exe_ctx, execution_policy, language, discard_on_error, expr_cstr, expr_prefix, result_valobj_sp, error);
641}
642
643ExecutionResults
644ClangUserExpression::EvaluateWithError (ExecutionContext &exe_ctx,
645                                        lldb_private::ExecutionPolicy execution_policy,
646                                        lldb::LanguageType language,
647                                        bool discard_on_error,
648                                        const char *expr_cstr,
649                                        const char *expr_prefix,
650                                        lldb::ValueObjectSP &result_valobj_sp,
651                                        Error &error)
652{
653    lldb::LogSP log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_EXPRESSIONS | LIBLLDB_LOG_STEP));
654
655    ExecutionResults execution_results = eExecutionSetupError;
656
657    Process *process = exe_ctx.GetProcessPtr();
658
659    if (process == NULL || process->GetState() != lldb::eStateStopped)
660    {
661        if (execution_policy == eExecutionPolicyAlways)
662        {
663            if (log)
664                log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
665
666            error.SetErrorString ("expression needed to run but couldn't");
667
668            return execution_results;
669        }
670    }
671
672    if (process == NULL || !process->CanJIT())
673        execution_policy = eExecutionPolicyNever;
674
675    ClangUserExpressionSP user_expression_sp (new ClangUserExpression (expr_cstr, expr_prefix, language));
676
677    StreamString error_stream;
678
679    if (log)
680        log->Printf("== [ClangUserExpression::Evaluate] Parsing expression %s ==", expr_cstr);
681
682    const bool keep_expression_in_memory = true;
683
684    if (!user_expression_sp->Parse (error_stream, exe_ctx, TypeFromUser(NULL, NULL), execution_policy, keep_expression_in_memory))
685    {
686        if (error_stream.GetString().empty())
687            error.SetErrorString ("expression failed to parse, unknown error");
688        else
689            error.SetErrorString (error_stream.GetString().c_str());
690    }
691    else
692    {
693        lldb::ClangExpressionVariableSP expr_result;
694
695        if (user_expression_sp->EvaluatedStatically())
696        {
697            if (log)
698                log->Printf("== [ClangUserExpression::Evaluate] Expression evaluated as a constant ==");
699
700            if (user_expression_sp->m_const_result)
701                result_valobj_sp = user_expression_sp->m_const_result->GetValueObject();
702            else
703                error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
704
705            execution_results = eExecutionCompleted;
706        }
707        else if (execution_policy == eExecutionPolicyNever)
708        {
709            if (log)
710                log->Printf("== [ClangUserExpression::Evaluate] Expression may not run, but is not constant ==");
711
712            if (error_stream.GetString().empty())
713                error.SetErrorString ("expression needed to run but couldn't");
714        }
715        else
716        {
717            error_stream.GetString().clear();
718
719            if (log)
720                log->Printf("== [ClangUserExpression::Evaluate] Executing expression ==");
721
722            execution_results = user_expression_sp->Execute (error_stream,
723                                                             exe_ctx,
724                                                             discard_on_error,
725                                                             user_expression_sp,
726                                                             expr_result);
727
728            if (execution_results != eExecutionCompleted)
729            {
730                if (log)
731                    log->Printf("== [ClangUserExpression::Evaluate] Execution completed abnormally ==");
732
733                if (error_stream.GetString().empty())
734                    error.SetErrorString ("expression failed to execute, unknown error");
735                else
736                    error.SetErrorString (error_stream.GetString().c_str());
737            }
738            else
739            {
740                if (expr_result)
741                {
742                    result_valobj_sp = expr_result->GetValueObject();
743
744                    if (log)
745                        log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with result %s ==", result_valobj_sp->GetValueAsCString());
746                }
747                else
748                {
749                    if (log)
750                        log->Printf("== [ClangUserExpression::Evaluate] Execution completed normally with no result ==");
751
752                    error.SetError(ClangUserExpression::kNoResult, lldb::eErrorTypeGeneric);
753                }
754            }
755        }
756    }
757
758    if (result_valobj_sp.get() == NULL)
759        result_valobj_sp = ValueObjectConstResult::Create (NULL, error);
760
761    return execution_results;
762}
763