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 #include <stdio.h>
11 #if HAVE_SYS_TYPES_H
12 #include <sys/types.h>
13 #endif
14 
15 #include <cstdlib>
16 #include <map>
17 #include <string>
18 
19 #include "ClangUserExpression.h"
20 
21 #include "ASTResultSynthesizer.h"
22 #include "ClangDiagnostic.h"
23 #include "ClangExpressionDeclMap.h"
24 #include "ClangExpressionParser.h"
25 #include "ClangModulesDeclVendor.h"
26 #include "ClangPersistentVariables.h"
27 
28 #include "lldb/Core/Debugger.h"
29 #include "lldb/Core/Module.h"
30 #include "lldb/Core/StreamFile.h"
31 #include "lldb/Core/ValueObjectConstResult.h"
32 #include "lldb/Expression/ExpressionSourceCode.h"
33 #include "lldb/Expression/IRExecutionUnit.h"
34 #include "lldb/Expression/IRInterpreter.h"
35 #include "lldb/Expression/Materializer.h"
36 #include "lldb/Host/HostInfo.h"
37 #include "lldb/Symbol/Block.h"
38 #include "lldb/Symbol/ClangASTContext.h"
39 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
40 #include "lldb/Symbol/Function.h"
41 #include "lldb/Symbol/ObjectFile.h"
42 #include "lldb/Symbol/SymbolVendor.h"
43 #include "lldb/Symbol/Type.h"
44 #include "lldb/Symbol/VariableList.h"
45 #include "lldb/Target/ExecutionContext.h"
46 #include "lldb/Target/Process.h"
47 #include "lldb/Target/StackFrame.h"
48 #include "lldb/Target/Target.h"
49 #include "lldb/Target/ThreadPlan.h"
50 #include "lldb/Target/ThreadPlanCallUserExpression.h"
51 #include "lldb/Utility/ConstString.h"
52 #include "lldb/Utility/Log.h"
53 #include "lldb/Utility/StreamString.h"
54 
55 #include "clang/AST/DeclCXX.h"
56 #include "clang/AST/DeclObjC.h"
57 
58 using namespace lldb_private;
59 
60 ClangUserExpression::ClangUserExpression(
61     ExecutionContextScope &exe_scope, llvm::StringRef expr,
62     llvm::StringRef prefix, lldb::LanguageType language,
63     ResultType desired_type, const EvaluateExpressionOptions &options)
64     : LLVMUserExpression(exe_scope, expr, prefix, language, desired_type,
65                          options),
66       m_type_system_helper(*m_target_wp.lock().get(),
67                            options.GetExecutionPolicy() ==
68                                eExecutionPolicyTopLevel) {
69   switch (m_language) {
70   case lldb::eLanguageTypeC_plus_plus:
71     m_allow_cxx = true;
72     break;
73   case lldb::eLanguageTypeObjC:
74     m_allow_objc = true;
75     break;
76   case lldb::eLanguageTypeObjC_plus_plus:
77   default:
78     m_allow_cxx = true;
79     m_allow_objc = true;
80     break;
81   }
82 }
83 
84 ClangUserExpression::~ClangUserExpression() {}
85 
86 void ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Status &err) {
87   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
88 
89   if (log)
90     log->Printf("ClangUserExpression::ScanContext()");
91 
92   m_target = exe_ctx.GetTargetPtr();
93 
94   if (!(m_allow_cxx || m_allow_objc)) {
95     if (log)
96       log->Printf("  [CUE::SC] Settings inhibit C++ and Objective-C");
97     return;
98   }
99 
100   StackFrame *frame = exe_ctx.GetFramePtr();
101   if (frame == NULL) {
102     if (log)
103       log->Printf("  [CUE::SC] Null stack frame");
104     return;
105   }
106 
107   SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
108                                                   lldb::eSymbolContextBlock);
109 
110   if (!sym_ctx.function) {
111     if (log)
112       log->Printf("  [CUE::SC] Null function");
113     return;
114   }
115 
116   // Find the block that defines the function represented by "sym_ctx"
117   Block *function_block = sym_ctx.GetFunctionBlock();
118 
119   if (!function_block) {
120     if (log)
121       log->Printf("  [CUE::SC] Null function block");
122     return;
123   }
124 
125   CompilerDeclContext decl_context = function_block->GetDeclContext();
126 
127   if (!decl_context) {
128     if (log)
129       log->Printf("  [CUE::SC] Null decl context");
130     return;
131   }
132 
133   if (clang::CXXMethodDecl *method_decl =
134           ClangASTContext::DeclContextGetAsCXXMethodDecl(decl_context)) {
135     if (m_allow_cxx && method_decl->isInstance()) {
136       if (m_enforce_valid_object) {
137         lldb::VariableListSP variable_list_sp(
138             function_block->GetBlockVariableList(true));
139 
140         const char *thisErrorString = "Stopped in a C++ method, but 'this' "
141                                       "isn't available; pretending we are in a "
142                                       "generic context";
143 
144         if (!variable_list_sp) {
145           err.SetErrorString(thisErrorString);
146           return;
147         }
148 
149         lldb::VariableSP this_var_sp(
150             variable_list_sp->FindVariable(ConstString("this")));
151 
152         if (!this_var_sp || !this_var_sp->IsInScope(frame) ||
153             !this_var_sp->LocationIsValidForFrame(frame)) {
154           err.SetErrorString(thisErrorString);
155           return;
156         }
157       }
158 
159       m_in_cplusplus_method = true;
160       m_needs_object_ptr = true;
161     }
162   } else if (clang::ObjCMethodDecl *method_decl =
163                  ClangASTContext::DeclContextGetAsObjCMethodDecl(
164                      decl_context)) {
165     if (m_allow_objc) {
166       if (m_enforce_valid_object) {
167         lldb::VariableListSP variable_list_sp(
168             function_block->GetBlockVariableList(true));
169 
170         const char *selfErrorString = "Stopped in an Objective-C method, but "
171                                       "'self' isn't available; pretending we "
172                                       "are in a generic context";
173 
174         if (!variable_list_sp) {
175           err.SetErrorString(selfErrorString);
176           return;
177         }
178 
179         lldb::VariableSP self_variable_sp =
180             variable_list_sp->FindVariable(ConstString("self"));
181 
182         if (!self_variable_sp || !self_variable_sp->IsInScope(frame) ||
183             !self_variable_sp->LocationIsValidForFrame(frame)) {
184           err.SetErrorString(selfErrorString);
185           return;
186         }
187       }
188 
189       m_in_objectivec_method = true;
190       m_needs_object_ptr = true;
191 
192       if (!method_decl->isInstanceMethod())
193         m_in_static_method = true;
194     }
195   } else if (clang::FunctionDecl *function_decl =
196                  ClangASTContext::DeclContextGetAsFunctionDecl(decl_context)) {
197     // We might also have a function that said in the debug information that it
198     // captured an object pointer.  The best way to deal with getting to the
199     // ivars at present is by pretending that this is a method of a class in
200     // whatever runtime the debug info says the object pointer belongs to.  Do
201     // that here.
202 
203     ClangASTMetadata *metadata =
204         ClangASTContext::DeclContextGetMetaData(decl_context, function_decl);
205     if (metadata && metadata->HasObjectPtr()) {
206       lldb::LanguageType language = metadata->GetObjectPtrLanguage();
207       if (language == lldb::eLanguageTypeC_plus_plus) {
208         if (m_enforce_valid_object) {
209           lldb::VariableListSP variable_list_sp(
210               function_block->GetBlockVariableList(true));
211 
212           const char *thisErrorString = "Stopped in a context claiming to "
213                                         "capture a C++ object pointer, but "
214                                         "'this' isn't available; pretending we "
215                                         "are in a generic context";
216 
217           if (!variable_list_sp) {
218             err.SetErrorString(thisErrorString);
219             return;
220           }
221 
222           lldb::VariableSP this_var_sp(
223               variable_list_sp->FindVariable(ConstString("this")));
224 
225           if (!this_var_sp || !this_var_sp->IsInScope(frame) ||
226               !this_var_sp->LocationIsValidForFrame(frame)) {
227             err.SetErrorString(thisErrorString);
228             return;
229           }
230         }
231 
232         m_in_cplusplus_method = true;
233         m_needs_object_ptr = true;
234       } else if (language == lldb::eLanguageTypeObjC) {
235         if (m_enforce_valid_object) {
236           lldb::VariableListSP variable_list_sp(
237               function_block->GetBlockVariableList(true));
238 
239           const char *selfErrorString =
240               "Stopped in a context claiming to capture an Objective-C object "
241               "pointer, but 'self' isn't available; pretending we are in a "
242               "generic context";
243 
244           if (!variable_list_sp) {
245             err.SetErrorString(selfErrorString);
246             return;
247           }
248 
249           lldb::VariableSP self_variable_sp =
250               variable_list_sp->FindVariable(ConstString("self"));
251 
252           if (!self_variable_sp || !self_variable_sp->IsInScope(frame) ||
253               !self_variable_sp->LocationIsValidForFrame(frame)) {
254             err.SetErrorString(selfErrorString);
255             return;
256           }
257 
258           Type *self_type = self_variable_sp->GetType();
259 
260           if (!self_type) {
261             err.SetErrorString(selfErrorString);
262             return;
263           }
264 
265           CompilerType self_clang_type = self_type->GetForwardCompilerType();
266 
267           if (!self_clang_type) {
268             err.SetErrorString(selfErrorString);
269             return;
270           }
271 
272           if (ClangASTContext::IsObjCClassType(self_clang_type)) {
273             return;
274           } else if (ClangASTContext::IsObjCObjectPointerType(
275                          self_clang_type)) {
276             m_in_objectivec_method = true;
277             m_needs_object_ptr = true;
278           } else {
279             err.SetErrorString(selfErrorString);
280             return;
281           }
282         } else {
283           m_in_objectivec_method = true;
284           m_needs_object_ptr = true;
285         }
286       }
287     }
288   }
289 }
290 
291 // This is a really nasty hack, meant to fix Objective-C expressions of the
292 // form (int)[myArray count].  Right now, because the type information for
293 // count is not available, [myArray count] returns id, which can't be directly
294 // cast to int without causing a clang error.
295 static void ApplyObjcCastHack(std::string &expr) {
296 #define OBJC_CAST_HACK_FROM "(int)["
297 #define OBJC_CAST_HACK_TO "(int)(long long)["
298 
299   size_t from_offset;
300 
301   while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
302     expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1,
303                  OBJC_CAST_HACK_TO);
304 
305 #undef OBJC_CAST_HACK_TO
306 #undef OBJC_CAST_HACK_FROM
307 }
308 
309 bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager,
310                                 ExecutionContext &exe_ctx,
311                                 lldb_private::ExecutionPolicy execution_policy,
312                                 bool keep_result_in_memory,
313                                 bool generate_debug_info) {
314   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
315 
316   Status err;
317 
318   InstallContext(exe_ctx);
319 
320   if (Target *target = exe_ctx.GetTargetPtr()) {
321     if (PersistentExpressionState *persistent_state =
322             target->GetPersistentExpressionStateForLanguage(
323                 lldb::eLanguageTypeC)) {
324       m_result_delegate.RegisterPersistentState(persistent_state);
325     } else {
326       diagnostic_manager.PutString(
327           eDiagnosticSeverityError,
328           "couldn't start parsing (no persistent data)");
329       return false;
330     }
331   } else {
332     diagnostic_manager.PutString(eDiagnosticSeverityError,
333                                  "error: couldn't start parsing (no target)");
334     return false;
335   }
336 
337   ScanContext(exe_ctx, err);
338 
339   if (!err.Success()) {
340     diagnostic_manager.PutString(eDiagnosticSeverityWarning, err.AsCString());
341   }
342 
343   ////////////////////////////////////
344   // Generate the expression
345   //
346 
347   ApplyObjcCastHack(m_expr_text);
348 
349   std::string prefix = m_expr_prefix;
350 
351   if (ClangModulesDeclVendor *decl_vendor =
352           m_target->GetClangModulesDeclVendor()) {
353     const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
354         llvm::cast<ClangPersistentVariables>(
355             m_target->GetPersistentExpressionStateForLanguage(
356                 lldb::eLanguageTypeC))
357             ->GetHandLoadedClangModules();
358     ClangModulesDeclVendor::ModuleVector modules_for_macros;
359 
360     for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {
361       modules_for_macros.push_back(module);
362     }
363 
364     if (m_target->GetEnableAutoImportClangModules()) {
365       if (StackFrame *frame = exe_ctx.GetFramePtr()) {
366         if (Block *block = frame->GetFrameBlock()) {
367           SymbolContext sc;
368 
369           block->CalculateSymbolContext(&sc);
370 
371           if (sc.comp_unit) {
372             StreamString error_stream;
373 
374             decl_vendor->AddModulesForCompileUnit(
375                 *sc.comp_unit, modules_for_macros, error_stream);
376           }
377         }
378       }
379     }
380   }
381 
382   lldb::LanguageType lang_type = lldb::eLanguageTypeUnknown;
383 
384   if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
385     m_transformed_text = m_expr_text;
386   } else {
387     std::unique_ptr<ExpressionSourceCode> source_code(
388         ExpressionSourceCode::CreateWrapped(prefix.c_str(),
389                                             m_expr_text.c_str()));
390 
391     if (m_in_cplusplus_method)
392       lang_type = lldb::eLanguageTypeC_plus_plus;
393     else if (m_in_objectivec_method)
394       lang_type = lldb::eLanguageTypeObjC;
395     else
396       lang_type = lldb::eLanguageTypeC;
397 
398     if (!source_code->GetText(m_transformed_text, lang_type, m_in_static_method,
399                               exe_ctx)) {
400       diagnostic_manager.PutString(eDiagnosticSeverityError,
401                                    "couldn't construct expression body");
402       return false;
403     }
404   }
405 
406   if (log)
407     log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
408 
409   ////////////////////////////////////
410   // Set up the target and compiler
411   //
412 
413   Target *target = exe_ctx.GetTargetPtr();
414 
415   if (!target) {
416     diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid target");
417     return false;
418   }
419 
420   //////////////////////////
421   // Parse the expression
422   //
423 
424   m_materializer_ap.reset(new Materializer());
425 
426   ResetDeclMap(exe_ctx, m_result_delegate, keep_result_in_memory);
427 
428   class OnExit {
429   public:
430     typedef std::function<void(void)> Callback;
431 
432     OnExit(Callback const &callback) : m_callback(callback) {}
433 
434     ~OnExit() { m_callback(); }
435 
436   private:
437     Callback m_callback;
438   };
439 
440   OnExit on_exit([this]() { ResetDeclMap(); });
441 
442   if (!DeclMap()->WillParse(exe_ctx, m_materializer_ap.get())) {
443     diagnostic_manager.PutString(
444         eDiagnosticSeverityError,
445         "current process state is unsuitable for expression parsing");
446 
447     ResetDeclMap(); // We are being careful here in the case of breakpoint
448                     // conditions.
449 
450     return false;
451   }
452 
453   if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
454     DeclMap()->SetLookupsEnabled(true);
455   }
456 
457   Process *process = exe_ctx.GetProcessPtr();
458   ExecutionContextScope *exe_scope = process;
459 
460   if (!exe_scope)
461     exe_scope = exe_ctx.GetTargetPtr();
462 
463   // We use a shared pointer here so we can use the original parser - if it
464   // succeeds or the rewrite parser we might make if it fails.  But the
465   // parser_sp will never be empty.
466 
467   ClangExpressionParser parser(exe_scope, *this, generate_debug_info);
468 
469   unsigned num_errors = parser.Parse(diagnostic_manager);
470 
471   // Check here for FixItHints.  If there are any try to apply the fixits and
472   // set the fixed text in m_fixed_text before returning an error.
473   if (num_errors) {
474     if (diagnostic_manager.HasFixIts()) {
475       if (parser.RewriteExpression(diagnostic_manager)) {
476         size_t fixed_start;
477         size_t fixed_end;
478         const std::string &fixed_expression =
479             diagnostic_manager.GetFixedExpression();
480         if (ExpressionSourceCode::GetOriginalBodyBounds(
481                 fixed_expression, lang_type, fixed_start, fixed_end))
482           m_fixed_text =
483               fixed_expression.substr(fixed_start, fixed_end - fixed_start);
484       }
485     }
486 
487     ResetDeclMap(); // We are being careful here in the case of breakpoint
488                     // conditions.
489 
490     return false;
491   }
492 
493   //////////////////////////////////////////////////////////////////////////////////////////
494   // Prepare the output of the parser for execution, evaluating it statically
495   // if possible
496   //
497 
498   {
499     Status jit_error = parser.PrepareForExecution(
500         m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx,
501         m_can_interpret, execution_policy);
502 
503     if (!jit_error.Success()) {
504       const char *error_cstr = jit_error.AsCString();
505       if (error_cstr && error_cstr[0])
506         diagnostic_manager.PutString(eDiagnosticSeverityError, error_cstr);
507       else
508         diagnostic_manager.PutString(eDiagnosticSeverityError,
509                                      "expression can't be interpreted or run");
510       return false;
511     }
512   }
513 
514   if (exe_ctx.GetProcessPtr() && execution_policy == eExecutionPolicyTopLevel) {
515     Status static_init_error =
516         parser.RunStaticInitializers(m_execution_unit_sp, exe_ctx);
517 
518     if (!static_init_error.Success()) {
519       const char *error_cstr = static_init_error.AsCString();
520       if (error_cstr && error_cstr[0])
521         diagnostic_manager.Printf(eDiagnosticSeverityError,
522                                   "couldn't run static initializers: %s\n",
523                                   error_cstr);
524       else
525         diagnostic_manager.PutString(eDiagnosticSeverityError,
526                                      "couldn't run static initializers\n");
527       return false;
528     }
529   }
530 
531   if (m_execution_unit_sp) {
532     bool register_execution_unit = false;
533 
534     if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
535       register_execution_unit = true;
536     }
537 
538     // If there is more than one external function in the execution unit, it
539     // needs to keep living even if it's not top level, because the result
540     // could refer to that function.
541 
542     if (m_execution_unit_sp->GetJittedFunctions().size() > 1) {
543       register_execution_unit = true;
544     }
545 
546     if (register_execution_unit) {
547       llvm::cast<PersistentExpressionState>(
548           exe_ctx.GetTargetPtr()->GetPersistentExpressionStateForLanguage(
549               m_language))
550           ->RegisterExecutionUnit(m_execution_unit_sp);
551     }
552   }
553 
554   if (generate_debug_info) {
555     lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule());
556 
557     if (jit_module_sp) {
558       ConstString const_func_name(FunctionName());
559       FileSpec jit_file;
560       jit_file.GetFilename() = const_func_name;
561       jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString());
562       m_jit_module_wp = jit_module_sp;
563       target->GetImages().Append(jit_module_sp);
564     }
565   }
566 
567   ResetDeclMap(); // Make this go away since we don't need any of its state
568                   // after parsing.  This also gets rid of any
569                   // ClangASTImporter::Minions.
570 
571   if (process && m_jit_start_addr != LLDB_INVALID_ADDRESS)
572     m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
573   return true;
574 }
575 
576 bool ClangUserExpression::AddArguments(ExecutionContext &exe_ctx,
577                                        std::vector<lldb::addr_t> &args,
578                                        lldb::addr_t struct_address,
579                                        DiagnosticManager &diagnostic_manager) {
580   lldb::addr_t object_ptr = LLDB_INVALID_ADDRESS;
581   lldb::addr_t cmd_ptr = LLDB_INVALID_ADDRESS;
582 
583   if (m_needs_object_ptr) {
584     lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP();
585     if (!frame_sp)
586       return true;
587 
588     ConstString object_name;
589 
590     if (m_in_cplusplus_method) {
591       object_name.SetCString("this");
592     } else if (m_in_objectivec_method) {
593       object_name.SetCString("self");
594     } else {
595       diagnostic_manager.PutString(
596           eDiagnosticSeverityError,
597           "need object pointer but don't know the language");
598       return false;
599     }
600 
601     Status object_ptr_error;
602 
603     object_ptr = GetObjectPointer(frame_sp, object_name, object_ptr_error);
604 
605     if (!object_ptr_error.Success()) {
606       exe_ctx.GetTargetRef().GetDebugger().GetAsyncOutputStream()->Printf(
607           "warning: `%s' is not accessible (substituting 0)\n",
608           object_name.AsCString());
609       object_ptr = 0;
610     }
611 
612     if (m_in_objectivec_method) {
613       ConstString cmd_name("_cmd");
614 
615       cmd_ptr = GetObjectPointer(frame_sp, cmd_name, object_ptr_error);
616 
617       if (!object_ptr_error.Success()) {
618         diagnostic_manager.Printf(
619             eDiagnosticSeverityWarning,
620             "couldn't get cmd pointer (substituting NULL): %s",
621             object_ptr_error.AsCString());
622         cmd_ptr = 0;
623       }
624     }
625 
626     args.push_back(object_ptr);
627 
628     if (m_in_objectivec_method)
629       args.push_back(cmd_ptr);
630 
631     args.push_back(struct_address);
632   } else {
633     args.push_back(struct_address);
634   }
635   return true;
636 }
637 
638 lldb::ExpressionVariableSP ClangUserExpression::GetResultAfterDematerialization(
639     ExecutionContextScope *exe_scope) {
640   return m_result_delegate.GetVariable();
641 }
642 
643 void ClangUserExpression::ClangUserExpressionHelper::ResetDeclMap(
644     ExecutionContext &exe_ctx,
645     Materializer::PersistentVariableDelegate &delegate,
646     bool keep_result_in_memory) {
647   m_expr_decl_map_up.reset(
648       new ClangExpressionDeclMap(keep_result_in_memory, &delegate, exe_ctx));
649 }
650 
651 clang::ASTConsumer *
652 ClangUserExpression::ClangUserExpressionHelper::ASTTransformer(
653     clang::ASTConsumer *passthrough) {
654   m_result_synthesizer_up.reset(
655       new ASTResultSynthesizer(passthrough, m_top_level, m_target));
656 
657   return m_result_synthesizer_up.get();
658 }
659 
660 void ClangUserExpression::ClangUserExpressionHelper::CommitPersistentDecls() {
661   if (m_result_synthesizer_up.get()) {
662     m_result_synthesizer_up->CommitPersistentDecls();
663   }
664 }
665 
666 ClangUserExpression::ResultDelegate::ResultDelegate() {}
667 
668 ConstString ClangUserExpression::ResultDelegate::GetName() {
669   return m_persistent_state->GetNextPersistentVariableName();
670 }
671 
672 void ClangUserExpression::ResultDelegate::DidDematerialize(
673     lldb::ExpressionVariableSP &variable) {
674   m_variable = variable;
675 }
676 
677 void ClangUserExpression::ResultDelegate::RegisterPersistentState(
678     PersistentExpressionState *persistent_state) {
679   m_persistent_state = persistent_state;
680 }
681 
682 lldb::ExpressionVariableSP &ClangUserExpression::ResultDelegate::GetVariable() {
683   return m_variable;
684 }
685