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