1 //===-- ClangUserExpression.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include <sys/types.h>
19 #include "ClangUserExpression.h"
21 #include "ASTResultSynthesizer.h"
22 #include "ClangDiagnostic.h"
23 #include "ClangExpressionDeclMap.h"
24 #include "ClangExpressionParser.h"
25 #include "ClangModulesDeclVendor.h"
26 #include "ClangPersistentVariables.h"
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"
55 #include "clang/AST/DeclCXX.h"
56 #include "clang/AST/DeclObjC.h"
58 using namespace lldb_private;
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,
66 m_type_system_helper(*m_target_wp.lock().get(),
67 options.GetExecutionPolicy() ==
68 eExecutionPolicyTopLevel),
69 m_result_delegate(exe_scope.CalculateTarget()) {
71 case lldb::eLanguageTypeC_plus_plus:
74 case lldb::eLanguageTypeObjC:
77 case lldb::eLanguageTypeObjC_plus_plus:
85 ClangUserExpression::~ClangUserExpression() {}
87 void ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Status &err) {
88 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
91 log->Printf("ClangUserExpression::ScanContext()");
93 m_target = exe_ctx.GetTargetPtr();
95 if (!(m_allow_cxx || m_allow_objc)) {
97 log->Printf(" [CUE::SC] Settings inhibit C++ and Objective-C");
101 StackFrame *frame = exe_ctx.GetFramePtr();
104 log->Printf(" [CUE::SC] Null stack frame");
108 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
109 lldb::eSymbolContextBlock);
111 if (!sym_ctx.function) {
113 log->Printf(" [CUE::SC] Null function");
117 // Find the block that defines the function represented by "sym_ctx"
118 Block *function_block = sym_ctx.GetFunctionBlock();
120 if (!function_block) {
122 log->Printf(" [CUE::SC] Null function block");
126 CompilerDeclContext decl_context = function_block->GetDeclContext();
130 log->Printf(" [CUE::SC] Null decl context");
134 if (clang::CXXMethodDecl *method_decl =
135 ClangASTContext::DeclContextGetAsCXXMethodDecl(decl_context)) {
136 if (m_allow_cxx && method_decl->isInstance()) {
137 if (m_enforce_valid_object) {
138 lldb::VariableListSP variable_list_sp(
139 function_block->GetBlockVariableList(true));
141 const char *thisErrorString = "Stopped in a C++ method, but 'this' "
142 "isn't available; pretending we are in a "
145 if (!variable_list_sp) {
146 err.SetErrorString(thisErrorString);
150 lldb::VariableSP this_var_sp(
151 variable_list_sp->FindVariable(ConstString("this")));
153 if (!this_var_sp || !this_var_sp->IsInScope(frame) ||
154 !this_var_sp->LocationIsValidForFrame(frame)) {
155 err.SetErrorString(thisErrorString);
160 m_in_cplusplus_method = true;
161 m_needs_object_ptr = true;
163 } else if (clang::ObjCMethodDecl *method_decl =
164 ClangASTContext::DeclContextGetAsObjCMethodDecl(
167 if (m_enforce_valid_object) {
168 lldb::VariableListSP variable_list_sp(
169 function_block->GetBlockVariableList(true));
171 const char *selfErrorString = "Stopped in an Objective-C method, but "
172 "'self' isn't available; pretending we "
173 "are in a generic context";
175 if (!variable_list_sp) {
176 err.SetErrorString(selfErrorString);
180 lldb::VariableSP self_variable_sp =
181 variable_list_sp->FindVariable(ConstString("self"));
183 if (!self_variable_sp || !self_variable_sp->IsInScope(frame) ||
184 !self_variable_sp->LocationIsValidForFrame(frame)) {
185 err.SetErrorString(selfErrorString);
190 m_in_objectivec_method = true;
191 m_needs_object_ptr = true;
193 if (!method_decl->isInstanceMethod())
194 m_in_static_method = true;
196 } else if (clang::FunctionDecl *function_decl =
197 ClangASTContext::DeclContextGetAsFunctionDecl(decl_context)) {
198 // We might also have a function that said in the debug information that it
199 // captured an object pointer. The best way to deal with getting to the
200 // ivars at present is by pretending that this is a method of a class in
201 // whatever runtime the debug info says the object pointer belongs to. Do
204 ClangASTMetadata *metadata =
205 ClangASTContext::DeclContextGetMetaData(decl_context, function_decl);
206 if (metadata && metadata->HasObjectPtr()) {
207 lldb::LanguageType language = metadata->GetObjectPtrLanguage();
208 if (language == lldb::eLanguageTypeC_plus_plus) {
209 if (m_enforce_valid_object) {
210 lldb::VariableListSP variable_list_sp(
211 function_block->GetBlockVariableList(true));
213 const char *thisErrorString = "Stopped in a context claiming to "
214 "capture a C++ object pointer, but "
215 "'this' isn't available; pretending we "
216 "are in a generic context";
218 if (!variable_list_sp) {
219 err.SetErrorString(thisErrorString);
223 lldb::VariableSP this_var_sp(
224 variable_list_sp->FindVariable(ConstString("this")));
226 if (!this_var_sp || !this_var_sp->IsInScope(frame) ||
227 !this_var_sp->LocationIsValidForFrame(frame)) {
228 err.SetErrorString(thisErrorString);
233 m_in_cplusplus_method = true;
234 m_needs_object_ptr = true;
235 } else if (language == lldb::eLanguageTypeObjC) {
236 if (m_enforce_valid_object) {
237 lldb::VariableListSP variable_list_sp(
238 function_block->GetBlockVariableList(true));
240 const char *selfErrorString =
241 "Stopped in a context claiming to capture an Objective-C object "
242 "pointer, but 'self' isn't available; pretending we are in a "
245 if (!variable_list_sp) {
246 err.SetErrorString(selfErrorString);
250 lldb::VariableSP self_variable_sp =
251 variable_list_sp->FindVariable(ConstString("self"));
253 if (!self_variable_sp || !self_variable_sp->IsInScope(frame) ||
254 !self_variable_sp->LocationIsValidForFrame(frame)) {
255 err.SetErrorString(selfErrorString);
259 Type *self_type = self_variable_sp->GetType();
262 err.SetErrorString(selfErrorString);
266 CompilerType self_clang_type = self_type->GetForwardCompilerType();
268 if (!self_clang_type) {
269 err.SetErrorString(selfErrorString);
273 if (ClangASTContext::IsObjCClassType(self_clang_type)) {
275 } else if (ClangASTContext::IsObjCObjectPointerType(
277 m_in_objectivec_method = true;
278 m_needs_object_ptr = true;
280 err.SetErrorString(selfErrorString);
284 m_in_objectivec_method = true;
285 m_needs_object_ptr = true;
292 // This is a really nasty hack, meant to fix Objective-C expressions of the
293 // form (int)[myArray count]. Right now, because the type information for
294 // count is not available, [myArray count] returns id, which can't be directly
295 // cast to int without causing a clang error.
296 static void ApplyObjcCastHack(std::string &expr) {
297 #define OBJC_CAST_HACK_FROM "(int)["
298 #define OBJC_CAST_HACK_TO "(int)(long long)["
302 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos)
303 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1,
306 #undef OBJC_CAST_HACK_TO
307 #undef OBJC_CAST_HACK_FROM
311 // Utility guard that calls a callback when going out of scope.
314 typedef std::function<void(void)> Callback;
316 OnExit(Callback const &callback) : m_callback(callback) {}
318 ~OnExit() { m_callback(); }
325 bool ClangUserExpression::SetupPersistentState(DiagnosticManager &diagnostic_manager,
326 ExecutionContext &exe_ctx) {
327 if (Target *target = exe_ctx.GetTargetPtr()) {
328 if (PersistentExpressionState *persistent_state =
329 target->GetPersistentExpressionStateForLanguage(
330 lldb::eLanguageTypeC)) {
331 m_result_delegate.RegisterPersistentState(persistent_state);
333 diagnostic_manager.PutString(
334 eDiagnosticSeverityError,
335 "couldn't start parsing (no persistent data)");
339 diagnostic_manager.PutString(eDiagnosticSeverityError,
340 "error: couldn't start parsing (no target)");
346 static void SetupDeclVendor(ExecutionContext &exe_ctx, Target *target) {
347 if (ClangModulesDeclVendor *decl_vendor =
348 target->GetClangModulesDeclVendor()) {
349 const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
350 llvm::cast<ClangPersistentVariables>(
351 target->GetPersistentExpressionStateForLanguage(
352 lldb::eLanguageTypeC))
353 ->GetHandLoadedClangModules();
354 ClangModulesDeclVendor::ModuleVector modules_for_macros;
356 for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {
357 modules_for_macros.push_back(module);
360 if (target->GetEnableAutoImportClangModules()) {
361 if (StackFrame *frame = exe_ctx.GetFramePtr()) {
362 if (Block *block = frame->GetFrameBlock()) {
365 block->CalculateSymbolContext(&sc);
368 StreamString error_stream;
370 decl_vendor->AddModulesForCompileUnit(
371 *sc.comp_unit, modules_for_macros, error_stream);
379 llvm::Optional<lldb::LanguageType> ClangUserExpression::GetLanguageForExpr(
380 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx) {
381 lldb::LanguageType lang_type = lldb::LanguageType::eLanguageTypeUnknown;
383 std::string prefix = m_expr_prefix;
385 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
386 m_transformed_text = m_expr_text;
388 std::unique_ptr<ExpressionSourceCode> source_code(
389 ExpressionSourceCode::CreateWrapped(prefix.c_str(),
390 m_expr_text.c_str()));
392 if (m_in_cplusplus_method)
393 lang_type = lldb::eLanguageTypeC_plus_plus;
394 else if (m_in_objectivec_method)
395 lang_type = lldb::eLanguageTypeObjC;
397 lang_type = lldb::eLanguageTypeC;
399 if (!source_code->GetText(m_transformed_text, lang_type, m_in_static_method,
401 diagnostic_manager.PutString(eDiagnosticSeverityError,
402 "couldn't construct expression body");
403 return llvm::Optional<lldb::LanguageType>();
409 bool ClangUserExpression::PrepareForParsing(
410 DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx) {
411 InstallContext(exe_ctx);
413 if (!SetupPersistentState(diagnostic_manager, exe_ctx))
417 ScanContext(exe_ctx, err);
419 if (!err.Success()) {
420 diagnostic_manager.PutString(eDiagnosticSeverityWarning, err.AsCString());
423 ////////////////////////////////////
424 // Generate the expression
427 ApplyObjcCastHack(m_expr_text);
429 SetupDeclVendor(exe_ctx, m_target);
433 bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager,
434 ExecutionContext &exe_ctx,
435 lldb_private::ExecutionPolicy execution_policy,
436 bool keep_result_in_memory,
437 bool generate_debug_info) {
438 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
440 if (!PrepareForParsing(diagnostic_manager, exe_ctx))
443 lldb::LanguageType lang_type = lldb::LanguageType::eLanguageTypeUnknown;
444 if (auto new_lang = GetLanguageForExpr(diagnostic_manager, exe_ctx)) {
445 lang_type = new_lang.getValue();
449 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str());
451 ////////////////////////////////////
452 // Set up the target and compiler
455 Target *target = exe_ctx.GetTargetPtr();
458 diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid target");
462 //////////////////////////
463 // Parse the expression
466 m_materializer_ap.reset(new Materializer());
468 ResetDeclMap(exe_ctx, m_result_delegate, keep_result_in_memory);
470 OnExit on_exit([this]() { ResetDeclMap(); });
472 if (!DeclMap()->WillParse(exe_ctx, m_materializer_ap.get())) {
473 diagnostic_manager.PutString(
474 eDiagnosticSeverityError,
475 "current process state is unsuitable for expression parsing");
479 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
480 DeclMap()->SetLookupsEnabled(true);
483 Process *process = exe_ctx.GetProcessPtr();
484 ExecutionContextScope *exe_scope = process;
487 exe_scope = exe_ctx.GetTargetPtr();
489 // We use a shared pointer here so we can use the original parser - if it
490 // succeeds or the rewrite parser we might make if it fails. But the
491 // parser_sp will never be empty.
493 ClangExpressionParser parser(exe_scope, *this, generate_debug_info);
495 unsigned num_errors = parser.Parse(diagnostic_manager);
497 // Check here for FixItHints. If there are any try to apply the fixits and
498 // set the fixed text in m_fixed_text before returning an error.
500 if (diagnostic_manager.HasFixIts()) {
501 if (parser.RewriteExpression(diagnostic_manager)) {
504 const std::string &fixed_expression =
505 diagnostic_manager.GetFixedExpression();
506 if (ExpressionSourceCode::GetOriginalBodyBounds(
507 fixed_expression, lang_type, fixed_start, fixed_end))
509 fixed_expression.substr(fixed_start, fixed_end - fixed_start);
515 //////////////////////////////////////////////////////////////////////////////////////////
516 // Prepare the output of the parser for execution, evaluating it statically
521 Status jit_error = parser.PrepareForExecution(
522 m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx,
523 m_can_interpret, execution_policy);
525 if (!jit_error.Success()) {
526 const char *error_cstr = jit_error.AsCString();
527 if (error_cstr && error_cstr[0])
528 diagnostic_manager.PutString(eDiagnosticSeverityError, error_cstr);
530 diagnostic_manager.PutString(eDiagnosticSeverityError,
531 "expression can't be interpreted or run");
536 if (exe_ctx.GetProcessPtr() && execution_policy == eExecutionPolicyTopLevel) {
537 Status static_init_error =
538 parser.RunStaticInitializers(m_execution_unit_sp, exe_ctx);
540 if (!static_init_error.Success()) {
541 const char *error_cstr = static_init_error.AsCString();
542 if (error_cstr && error_cstr[0])
543 diagnostic_manager.Printf(eDiagnosticSeverityError,
544 "couldn't run static initializers: %s\n",
547 diagnostic_manager.PutString(eDiagnosticSeverityError,
548 "couldn't run static initializers\n");
553 if (m_execution_unit_sp) {
554 bool register_execution_unit = false;
556 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
557 register_execution_unit = true;
560 // If there is more than one external function in the execution unit, it
561 // needs to keep living even if it's not top level, because the result
562 // could refer to that function.
564 if (m_execution_unit_sp->GetJittedFunctions().size() > 1) {
565 register_execution_unit = true;
568 if (register_execution_unit) {
569 llvm::cast<PersistentExpressionState>(
570 exe_ctx.GetTargetPtr()->GetPersistentExpressionStateForLanguage(
572 ->RegisterExecutionUnit(m_execution_unit_sp);
576 if (generate_debug_info) {
577 lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule());
580 ConstString const_func_name(FunctionName());
582 jit_file.GetFilename() = const_func_name;
583 jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString());
584 m_jit_module_wp = jit_module_sp;
585 target->GetImages().Append(jit_module_sp);
589 if (process && m_jit_start_addr != LLDB_INVALID_ADDRESS)
590 m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
594 bool ClangUserExpression::AddArguments(ExecutionContext &exe_ctx,
595 std::vector<lldb::addr_t> &args,
596 lldb::addr_t struct_address,
597 DiagnosticManager &diagnostic_manager) {
598 lldb::addr_t object_ptr = LLDB_INVALID_ADDRESS;
599 lldb::addr_t cmd_ptr = LLDB_INVALID_ADDRESS;
601 if (m_needs_object_ptr) {
602 lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP();
606 ConstString object_name;
608 if (m_in_cplusplus_method) {
609 object_name.SetCString("this");
610 } else if (m_in_objectivec_method) {
611 object_name.SetCString("self");
613 diagnostic_manager.PutString(
614 eDiagnosticSeverityError,
615 "need object pointer but don't know the language");
619 Status object_ptr_error;
621 object_ptr = GetObjectPointer(frame_sp, object_name, object_ptr_error);
623 if (!object_ptr_error.Success()) {
624 exe_ctx.GetTargetRef().GetDebugger().GetAsyncOutputStream()->Printf(
625 "warning: `%s' is not accessible (substituting 0)\n",
626 object_name.AsCString());
630 if (m_in_objectivec_method) {
631 ConstString cmd_name("_cmd");
633 cmd_ptr = GetObjectPointer(frame_sp, cmd_name, object_ptr_error);
635 if (!object_ptr_error.Success()) {
636 diagnostic_manager.Printf(
637 eDiagnosticSeverityWarning,
638 "couldn't get cmd pointer (substituting NULL): %s",
639 object_ptr_error.AsCString());
644 args.push_back(object_ptr);
646 if (m_in_objectivec_method)
647 args.push_back(cmd_ptr);
649 args.push_back(struct_address);
651 args.push_back(struct_address);
656 lldb::ExpressionVariableSP ClangUserExpression::GetResultAfterDematerialization(
657 ExecutionContextScope *exe_scope) {
658 return m_result_delegate.GetVariable();
661 void ClangUserExpression::ClangUserExpressionHelper::ResetDeclMap(
662 ExecutionContext &exe_ctx,
663 Materializer::PersistentVariableDelegate &delegate,
664 bool keep_result_in_memory) {
665 m_expr_decl_map_up.reset(
666 new ClangExpressionDeclMap(keep_result_in_memory, &delegate, exe_ctx));
670 ClangUserExpression::ClangUserExpressionHelper::ASTTransformer(
671 clang::ASTConsumer *passthrough) {
672 m_result_synthesizer_up.reset(
673 new ASTResultSynthesizer(passthrough, m_top_level, m_target));
675 return m_result_synthesizer_up.get();
678 void ClangUserExpression::ClangUserExpressionHelper::CommitPersistentDecls() {
679 if (m_result_synthesizer_up.get()) {
680 m_result_synthesizer_up->CommitPersistentDecls();
684 ConstString ClangUserExpression::ResultDelegate::GetName() {
685 auto prefix = m_persistent_state->GetPersistentVariablePrefix();
686 return m_persistent_state->GetNextPersistentVariableName(*m_target_sp,
690 void ClangUserExpression::ResultDelegate::DidDematerialize(
691 lldb::ExpressionVariableSP &variable) {
692 m_variable = variable;
695 void ClangUserExpression::ResultDelegate::RegisterPersistentState(
696 PersistentExpressionState *persistent_state) {
697 m_persistent_state = persistent_state;
700 lldb::ExpressionVariableSP &ClangUserExpression::ResultDelegate::GetVariable() {