1 //===-- ClangExpressionParser.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 // C++ Includes
12 // Other libraries and framework includes
13 #include "clang/AST/ASTContext.h"
14 #include "clang/AST/ExternalASTSource.h"
15 #include "clang/Basic/FileManager.h"
16 #include "clang/Basic/SourceLocation.h"
17 #include "clang/Basic/TargetInfo.h"
18 #include "clang/Basic/Version.h"
19 #include "clang/CodeGen/CodeGenAction.h"
20 #include "clang/CodeGen/ModuleBuilder.h"
21 #include "clang/Frontend/CompilerInstance.h"
22 #include "clang/Frontend/CompilerInvocation.h"
23 #include "clang/Frontend/FrontendActions.h"
24 #include "clang/Frontend/FrontendDiagnostic.h"
25 #include "clang/Frontend/FrontendPluginRegistry.h"
26 #include "clang/Frontend/TextDiagnosticBuffer.h"
27 #include "clang/Frontend/TextDiagnosticPrinter.h"
28 #include "clang/Lex/Preprocessor.h"
29 #include "clang/Parse/ParseAST.h"
30 #include "clang/Rewrite/Frontend/FrontendActions.h"
31 #include "clang/Sema/SemaConsumer.h"
32 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
33 
34 #include "llvm/ADT/StringRef.h"
35 #include "llvm/ExecutionEngine/ExecutionEngine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/FileSystem.h"
38 #include "llvm/Support/TargetSelect.h"
39 
40 #include "llvm/ExecutionEngine/MCJIT.h"
41 #include "llvm/IR/LLVMContext.h"
42 #include "llvm/IR/Module.h"
43 #include "llvm/Support/ErrorHandling.h"
44 #include "llvm/Support/MemoryBuffer.h"
45 #include "llvm/Support/DynamicLibrary.h"
46 #include "llvm/Support/Host.h"
47 #include "llvm/Support/Signals.h"
48 
49 // Project includes
50 #include "ClangExpressionParser.h"
51 
52 #include "ClangASTSource.h"
53 #include "ClangExpressionHelper.h"
54 #include "ClangExpressionDeclMap.h"
55 #include "ClangModulesDeclVendor.h"
56 #include "ClangPersistentVariables.h"
57 #include "IRForTarget.h"
58 
59 #include "lldb/Core/ArchSpec.h"
60 #include "lldb/Core/DataBufferHeap.h"
61 #include "lldb/Core/Debugger.h"
62 #include "lldb/Core/Disassembler.h"
63 #include "lldb/Core/Log.h"
64 #include "lldb/Core/Module.h"
65 #include "lldb/Core/Stream.h"
66 #include "lldb/Core/StreamFile.h"
67 #include "lldb/Core/StreamString.h"
68 #include "lldb/Expression/IRExecutionUnit.h"
69 #include "lldb/Expression/IRDynamicChecks.h"
70 #include "lldb/Expression/IRInterpreter.h"
71 #include "lldb/Host/File.h"
72 #include "lldb/Host/HostInfo.h"
73 #include "lldb/Symbol/ClangASTContext.h"
74 #include "lldb/Symbol/SymbolVendor.h"
75 #include "lldb/Target/ExecutionContext.h"
76 #include "lldb/Target/ObjCLanguageRuntime.h"
77 #include "lldb/Target/Process.h"
78 #include "lldb/Target/Target.h"
79 
80 using namespace clang;
81 using namespace llvm;
82 using namespace lldb_private;
83 
84 //===----------------------------------------------------------------------===//
85 // Utility Methods for Clang
86 //===----------------------------------------------------------------------===//
87 
88 std::string GetBuiltinIncludePath(const char *Argv0) {
89     SmallString<128> P(llvm::sys::fs::getMainExecutable(
90         Argv0, (void *)(intptr_t) GetBuiltinIncludePath));
91 
92     if (!P.empty()) {
93         llvm::sys::path::remove_filename(P); // Remove /clang from foo/bin/clang
94         llvm::sys::path::remove_filename(P); // Remove /bin   from foo/bin
95 
96         // Get foo/lib/clang/<version>/include
97         llvm::sys::path::append(P, "lib", "clang", CLANG_VERSION_STRING,
98                                 "include");
99     }
100 
101     return P.str();
102 }
103 
104 class ClangExpressionParser::LLDBPreprocessorCallbacks : public PPCallbacks
105 {
106     ClangModulesDeclVendor     &m_decl_vendor;
107     ClangPersistentVariables   &m_persistent_vars;
108     StreamString                m_error_stream;
109     bool                        m_has_errors = false;
110 
111 public:
112     LLDBPreprocessorCallbacks(ClangModulesDeclVendor &decl_vendor,
113                               ClangPersistentVariables &persistent_vars) :
114         m_decl_vendor(decl_vendor),
115         m_persistent_vars(persistent_vars)
116     {
117     }
118 
119     void
120     moduleImport(SourceLocation import_location,
121                  clang::ModuleIdPath path,
122                  const clang::Module * /*null*/) override
123     {
124         std::vector<ConstString> string_path;
125 
126         for (const std::pair<IdentifierInfo *, SourceLocation> &component : path)
127         {
128             string_path.push_back(ConstString(component.first->getName()));
129         }
130 
131         StreamString error_stream;
132 
133         ClangModulesDeclVendor::ModuleVector exported_modules;
134 
135         if (!m_decl_vendor.AddModule(string_path, &exported_modules, m_error_stream))
136         {
137             m_has_errors = true;
138         }
139 
140         for (ClangModulesDeclVendor::ModuleID module : exported_modules)
141         {
142             m_persistent_vars.AddHandLoadedClangModule(module);
143         }
144     }
145 
146     bool hasErrors()
147     {
148         return m_has_errors;
149     }
150 
151     const std::string &getErrorString()
152     {
153         return m_error_stream.GetString();
154     }
155 };
156 
157 //===----------------------------------------------------------------------===//
158 // Implementation of ClangExpressionParser
159 //===----------------------------------------------------------------------===//
160 
161 ClangExpressionParser::ClangExpressionParser (ExecutionContextScope *exe_scope,
162                                               Expression &expr,
163                                               bool generate_debug_info) :
164     ExpressionParser (exe_scope, expr, generate_debug_info),
165     m_compiler (),
166     m_code_generator (),
167     m_pp_callbacks(nullptr)
168 {
169     // 1. Create a new compiler instance.
170     m_compiler.reset(new CompilerInstance());
171 
172     // 2. Install the target.
173 
174     lldb::TargetSP target_sp;
175     if (exe_scope)
176         target_sp = exe_scope->CalculateTarget();
177 
178     // TODO: figure out what to really do when we don't have a valid target.
179     // Sometimes this will be ok to just use the host target triple (when we
180     // evaluate say "2+3", but other expressions like breakpoint conditions
181     // and other things that _are_ target specific really shouldn't just be
182     // using the host triple. This needs to be fixed in a better way.
183     if (target_sp && target_sp->GetArchitecture().IsValid())
184     {
185         std::string triple = target_sp->GetArchitecture().GetTriple().str();
186         m_compiler->getTargetOpts().Triple = triple;
187     }
188     else
189     {
190         m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
191     }
192 
193     if (target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86 ||
194         target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86_64)
195     {
196         m_compiler->getTargetOpts().Features.push_back("+sse");
197         m_compiler->getTargetOpts().Features.push_back("+sse2");
198     }
199 
200     // Any arm32 iOS environment, but not on arm64
201     if (m_compiler->getTargetOpts().Triple.find("arm64") == std::string::npos &&
202         m_compiler->getTargetOpts().Triple.find("arm") != std::string::npos &&
203         m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos)
204     {
205         m_compiler->getTargetOpts().ABI = "apcs-gnu";
206     }
207 
208     m_compiler->createDiagnostics();
209 
210     // Create the target instance.
211     m_compiler->setTarget(TargetInfo::CreateTargetInfo(
212         m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts));
213 
214     assert (m_compiler->hasTarget());
215 
216     // 3. Set options.
217 
218     lldb::LanguageType language = expr.Language();
219 
220     switch (language)
221     {
222     case lldb::eLanguageTypeC:
223     case lldb::eLanguageTypeC89:
224     case lldb::eLanguageTypeC99:
225     case lldb::eLanguageTypeC11:
226         // FIXME: the following language option is a temporary workaround,
227         // to "ask for C, get C++."
228         // For now, the expression parser must use C++ anytime the
229         // language is a C family language, because the expression parser
230         // uses features of C++ to capture values.
231         m_compiler->getLangOpts().CPlusPlus = true;
232         break;
233     case lldb::eLanguageTypeObjC:
234         m_compiler->getLangOpts().ObjC1 = true;
235         m_compiler->getLangOpts().ObjC2 = true;
236         // FIXME: the following language option is a temporary workaround,
237         // to "ask for ObjC, get ObjC++" (see comment above).
238         m_compiler->getLangOpts().CPlusPlus = true;
239         break;
240     case lldb::eLanguageTypeC_plus_plus:
241     case lldb::eLanguageTypeC_plus_plus_11:
242     case lldb::eLanguageTypeC_plus_plus_14:
243         m_compiler->getLangOpts().CPlusPlus11 = true;
244         m_compiler->getHeaderSearchOpts().UseLibcxx = true;
245         // fall thru ...
246     case lldb::eLanguageTypeC_plus_plus_03:
247         m_compiler->getLangOpts().CPlusPlus = true;
248         // FIXME: the following language option is a temporary workaround,
249         // to "ask for C++, get ObjC++".  Apple hopes to remove this requirement
250         // on non-Apple platforms, but for now it is needed.
251         m_compiler->getLangOpts().ObjC1 = true;
252         break;
253     case lldb::eLanguageTypeObjC_plus_plus:
254     case lldb::eLanguageTypeUnknown:
255     default:
256         m_compiler->getLangOpts().ObjC1 = true;
257         m_compiler->getLangOpts().ObjC2 = true;
258         m_compiler->getLangOpts().CPlusPlus = true;
259         m_compiler->getLangOpts().CPlusPlus11 = true;
260         m_compiler->getHeaderSearchOpts().UseLibcxx = true;
261         break;
262     }
263 
264     m_compiler->getLangOpts().Bool = true;
265     m_compiler->getLangOpts().WChar = true;
266     m_compiler->getLangOpts().Blocks = true;
267     m_compiler->getLangOpts().DebuggerSupport = true; // Features specifically for debugger clients
268     if (expr.DesiredResultType() == Expression::eResultTypeId)
269         m_compiler->getLangOpts().DebuggerCastResultToId = true;
270 
271     m_compiler->getLangOpts().CharIsSigned =
272             ArchSpec(m_compiler->getTargetOpts().Triple.c_str()).CharIsSignedByDefault();
273 
274     // Spell checking is a nice feature, but it ends up completing a
275     // lot of types that we didn't strictly speaking need to complete.
276     // As a result, we spend a long time parsing and importing debug
277     // information.
278     m_compiler->getLangOpts().SpellChecking = false;
279 
280     lldb::ProcessSP process_sp;
281     if (exe_scope)
282         process_sp = exe_scope->CalculateProcess();
283 
284     if (process_sp && m_compiler->getLangOpts().ObjC1)
285     {
286         if (process_sp->GetObjCLanguageRuntime())
287         {
288             if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == ObjCLanguageRuntime::ObjCRuntimeVersions::eAppleObjC_V2)
289                 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7));
290             else
291                 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, VersionTuple(10, 7));
292 
293             if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
294                 m_compiler->getLangOpts().DebuggerObjCLiteral = true;
295         }
296     }
297 
298     m_compiler->getLangOpts().ThreadsafeStatics = false;
299     m_compiler->getLangOpts().AccessControl = false; // Debuggers get universal access
300     m_compiler->getLangOpts().DollarIdents = true; // $ indicates a persistent variable name
301 
302     // Set CodeGen options
303     m_compiler->getCodeGenOpts().EmitDeclMetadata = true;
304     m_compiler->getCodeGenOpts().InstrumentFunctions = false;
305     m_compiler->getCodeGenOpts().DisableFPElim = true;
306     m_compiler->getCodeGenOpts().OmitLeafFramePointer = false;
307     if (generate_debug_info)
308         m_compiler->getCodeGenOpts().setDebugInfo(CodeGenOptions::FullDebugInfo);
309     else
310         m_compiler->getCodeGenOpts().setDebugInfo(CodeGenOptions::NoDebugInfo);
311 
312     // Disable some warnings.
313     m_compiler->getDiagnostics().setSeverityForGroup(clang::diag::Flavor::WarningOrError,
314         "unused-value", clang::diag::Severity::Ignored, SourceLocation());
315     m_compiler->getDiagnostics().setSeverityForGroup(clang::diag::Flavor::WarningOrError,
316         "odr", clang::diag::Severity::Ignored, SourceLocation());
317 
318     // Inform the target of the language options
319     //
320     // FIXME: We shouldn't need to do this, the target should be immutable once
321     // created. This complexity should be lifted elsewhere.
322     m_compiler->getTarget().adjust(m_compiler->getLangOpts());
323 
324     // 4. Set up the diagnostic buffer for reporting errors
325 
326     m_compiler->getDiagnostics().setClient(new clang::TextDiagnosticBuffer);
327 
328     // 5. Set up the source management objects inside the compiler
329 
330     clang::FileSystemOptions file_system_options;
331     m_file_manager.reset(new clang::FileManager(file_system_options));
332 
333     if (!m_compiler->hasSourceManager())
334         m_compiler->createSourceManager(*m_file_manager.get());
335 
336     m_compiler->createFileManager();
337     m_compiler->createPreprocessor(TU_Complete);
338 
339     if (ClangModulesDeclVendor *decl_vendor = target_sp->GetClangModulesDeclVendor())
340     {
341         ClangPersistentVariables *clang_persistent_vars = llvm::cast<ClangPersistentVariables>(target_sp->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC));
342         std::unique_ptr<PPCallbacks> pp_callbacks(new LLDBPreprocessorCallbacks(*decl_vendor, *clang_persistent_vars));
343         m_pp_callbacks = static_cast<LLDBPreprocessorCallbacks*>(pp_callbacks.get());
344         m_compiler->getPreprocessor().addPPCallbacks(std::move(pp_callbacks));
345     }
346 
347     // 6. Most of this we get from the CompilerInstance, but we
348     // also want to give the context an ExternalASTSource.
349     m_selector_table.reset(new SelectorTable());
350     m_builtin_context.reset(new Builtin::Context());
351 
352     std::unique_ptr<clang::ASTContext> ast_context(new ASTContext(m_compiler->getLangOpts(),
353                                                                   m_compiler->getSourceManager(),
354                                                                   m_compiler->getPreprocessor().getIdentifierTable(),
355                                                                   *m_selector_table.get(),
356                                                                   *m_builtin_context.get()));
357 
358     ast_context->InitBuiltinTypes(m_compiler->getTarget());
359 
360     ClangExpressionHelper *type_system_helper = dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
361     ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap();
362 
363     if (decl_map)
364     {
365         llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source(decl_map->CreateProxy());
366         decl_map->InstallASTContext(ast_context.get());
367         ast_context->setExternalSource(ast_source);
368     }
369 
370     m_ast_context.reset(new ClangASTContext(m_compiler->getTargetOpts().Triple.c_str()));
371     m_ast_context->setASTContext(ast_context.get());
372     m_compiler->setASTContext(ast_context.release());
373 
374     std::string module_name("$__lldb_module");
375 
376     m_llvm_context.reset(new LLVMContext());
377     m_code_generator.reset(CreateLLVMCodeGen(m_compiler->getDiagnostics(),
378                                              module_name,
379                                              m_compiler->getHeaderSearchOpts(),
380                                              m_compiler->getPreprocessorOpts(),
381                                              m_compiler->getCodeGenOpts(),
382                                              *m_llvm_context));
383 }
384 
385 ClangExpressionParser::~ClangExpressionParser()
386 {
387 }
388 
389 unsigned
390 ClangExpressionParser::Parse (Stream &stream)
391 {
392     TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer*>(m_compiler->getDiagnostics().getClient());
393 
394     diag_buf->FlushDiagnostics (m_compiler->getDiagnostics());
395 
396     const char *expr_text = m_expr.Text();
397 
398     clang::SourceManager &SourceMgr = m_compiler->getSourceManager();
399     bool created_main_file = false;
400     if (m_compiler->getCodeGenOpts().getDebugInfo() == CodeGenOptions::FullDebugInfo)
401     {
402         std::string temp_source_path;
403 
404         int temp_fd = -1;
405         llvm::SmallString<PATH_MAX> result_path;
406         FileSpec tmpdir_file_spec;
407         if (HostInfo::GetLLDBPath(lldb::ePathTypeLLDBTempSystemDir, tmpdir_file_spec))
408         {
409             tmpdir_file_spec.AppendPathComponent("lldb-%%%%%%.expr");
410             temp_source_path = tmpdir_file_spec.GetPath();
411             llvm::sys::fs::createUniqueFile(temp_source_path, temp_fd, result_path);
412         }
413         else
414         {
415             llvm::sys::fs::createTemporaryFile("lldb", "expr", temp_fd, result_path);
416         }
417 
418         if (temp_fd != -1)
419         {
420             lldb_private::File file (temp_fd, true);
421             const size_t expr_text_len = strlen(expr_text);
422             size_t bytes_written = expr_text_len;
423             if (file.Write(expr_text, bytes_written).Success())
424             {
425                 if (bytes_written == expr_text_len)
426                 {
427                     file.Close();
428                     SourceMgr.setMainFileID(SourceMgr.createFileID(
429                         m_file_manager->getFile(result_path),
430                         SourceLocation(), SrcMgr::C_User));
431                     created_main_file = true;
432                 }
433             }
434         }
435     }
436 
437     if (!created_main_file)
438     {
439         std::unique_ptr<MemoryBuffer> memory_buffer = MemoryBuffer::getMemBufferCopy(expr_text, __FUNCTION__);
440         SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(memory_buffer)));
441     }
442 
443     diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor());
444 
445     ClangExpressionHelper *type_system_helper = dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
446 
447     ASTConsumer *ast_transformer = type_system_helper->ASTTransformer(m_code_generator.get());
448 
449     if (ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap())
450         decl_map->InstallCodeGenerator(m_code_generator.get());
451 
452     if (ast_transformer)
453     {
454         ast_transformer->Initialize(m_compiler->getASTContext());
455         ParseAST(m_compiler->getPreprocessor(), ast_transformer, m_compiler->getASTContext());
456     }
457     else
458     {
459         m_code_generator->Initialize(m_compiler->getASTContext());
460         ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), m_compiler->getASTContext());
461     }
462 
463     diag_buf->EndSourceFile();
464 
465     TextDiagnosticBuffer::const_iterator diag_iterator;
466 
467     int num_errors = 0;
468 
469     if (m_pp_callbacks && m_pp_callbacks->hasErrors())
470     {
471         num_errors++;
472 
473         stream.PutCString(m_pp_callbacks->getErrorString().c_str());
474     }
475 
476     for (diag_iterator = diag_buf->warn_begin();
477          diag_iterator != diag_buf->warn_end();
478          ++diag_iterator)
479         stream.Printf("warning: %s\n", (*diag_iterator).second.c_str());
480 
481     for (diag_iterator = diag_buf->err_begin();
482          diag_iterator != diag_buf->err_end();
483          ++diag_iterator)
484     {
485         num_errors++;
486         stream.Printf("error: %s\n", (*diag_iterator).second.c_str());
487     }
488 
489     for (diag_iterator = diag_buf->note_begin();
490          diag_iterator != diag_buf->note_end();
491          ++diag_iterator)
492         stream.Printf("note: %s\n", (*diag_iterator).second.c_str());
493 
494     if (!num_errors)
495     {
496         if (type_system_helper->DeclMap() && !type_system_helper->DeclMap()->ResolveUnknownTypes())
497         {
498             stream.Printf("error: Couldn't infer the type of a variable\n");
499             num_errors++;
500         }
501     }
502 
503     return num_errors;
504 }
505 
506 static bool FindFunctionInModule (ConstString &mangled_name,
507                                   llvm::Module *module,
508                                   const char *orig_name)
509 {
510     for (llvm::Module::iterator fi = module->getFunctionList().begin(), fe = module->getFunctionList().end();
511          fi != fe;
512          ++fi)
513     {
514         if (fi->getName().str().find(orig_name) != std::string::npos)
515         {
516             mangled_name.SetCString(fi->getName().str().c_str());
517             return true;
518         }
519     }
520 
521     return false;
522 }
523 
524 Error
525 ClangExpressionParser::PrepareForExecution (lldb::addr_t &func_addr,
526                                             lldb::addr_t &func_end,
527                                             lldb::IRExecutionUnitSP &execution_unit_sp,
528                                             ExecutionContext &exe_ctx,
529                                             bool &can_interpret,
530                                             ExecutionPolicy execution_policy)
531 {
532 	func_addr = LLDB_INVALID_ADDRESS;
533 	func_end = LLDB_INVALID_ADDRESS;
534     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
535 
536     Error err;
537 
538     std::unique_ptr<llvm::Module> llvm_module_ap (m_code_generator->ReleaseModule());
539 
540     if (!llvm_module_ap.get())
541     {
542         err.SetErrorToGenericError();
543         err.SetErrorString("IR doesn't contain a module");
544         return err;
545     }
546 
547     // Find the actual name of the function (it's often mangled somehow)
548 
549     ConstString function_name;
550 
551     if (!FindFunctionInModule(function_name, llvm_module_ap.get(), m_expr.FunctionName()))
552     {
553         err.SetErrorToGenericError();
554         err.SetErrorStringWithFormat("Couldn't find %s() in the module", m_expr.FunctionName());
555         return err;
556     }
557     else
558     {
559         if (log)
560             log->Printf("Found function %s for %s", function_name.AsCString(), m_expr.FunctionName());
561     }
562 
563     execution_unit_sp.reset(new IRExecutionUnit (m_llvm_context, // handed off here
564                                                  llvm_module_ap, // handed off here
565                                                  function_name,
566                                                  exe_ctx.GetTargetSP(),
567                                                  m_compiler->getTargetOpts().Features));
568 
569     ClangExpressionHelper *type_system_helper = dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper());
570     ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap(); // result can be NULL
571 
572     if (decl_map)
573     {
574         Stream *error_stream = NULL;
575         Target *target = exe_ctx.GetTargetPtr();
576         if (target)
577             error_stream = target->GetDebugger().GetErrorFile().get();
578 
579         IRForTarget ir_for_target(decl_map,
580                                   m_expr.NeedsVariableResolution(),
581                                   *execution_unit_sp,
582                                   error_stream,
583                                   function_name.AsCString());
584 
585         bool ir_can_run = ir_for_target.runOnModule(*execution_unit_sp->GetModule());
586 
587         Error interpret_error;
588         Process *process = exe_ctx.GetProcessPtr();
589 
590         bool interpret_function_calls = !process ? false : process->CanInterpretFunctionCalls();
591         can_interpret = IRInterpreter::CanInterpret(*execution_unit_sp->GetModule(), *execution_unit_sp->GetFunction(), interpret_error, interpret_function_calls);
592 
593 
594         if (!ir_can_run)
595         {
596             err.SetErrorString("The expression could not be prepared to run in the target");
597             return err;
598         }
599 
600         if (!can_interpret && execution_policy == eExecutionPolicyNever)
601         {
602             err.SetErrorStringWithFormat("Can't run the expression locally: %s", interpret_error.AsCString());
603             return err;
604         }
605 
606         if (!process && execution_policy == eExecutionPolicyAlways)
607         {
608             err.SetErrorString("Expression needed to run in the target, but the target can't be run");
609             return err;
610         }
611 
612         if (execution_policy == eExecutionPolicyAlways || !can_interpret)
613         {
614             if (m_expr.NeedsValidation() && process)
615             {
616                 if (!process->GetDynamicCheckers())
617                 {
618                     DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
619 
620                     StreamString install_errors;
621 
622                     if (!dynamic_checkers->Install(install_errors, exe_ctx))
623                     {
624                         if (install_errors.GetString().empty())
625                             err.SetErrorString ("couldn't install checkers, unknown error");
626                         else
627                             err.SetErrorString (install_errors.GetString().c_str());
628 
629                         return err;
630                     }
631 
632                     process->SetDynamicCheckers(dynamic_checkers);
633 
634                     if (log)
635                         log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers ==");
636                 }
637 
638                 IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.AsCString());
639 
640                 if (!ir_dynamic_checks.runOnModule(*execution_unit_sp->GetModule()))
641                 {
642                     err.SetErrorToGenericError();
643                     err.SetErrorString("Couldn't add dynamic checks to the expression");
644                     return err;
645                 }
646             }
647 
648             execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
649         }
650     }
651     else
652     {
653         execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
654     }
655 
656     return err;
657 }
658