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 199 // object pointer. The best way to deal with getting to the ivars at 200 // present is by pretending 201 // that this is a method of a class in whatever runtime the debug info says 202 // the object pointer 203 // belongs to. Do that here. 204 205 ClangASTMetadata *metadata = 206 ClangASTContext::DeclContextGetMetaData(decl_context, function_decl); 207 if (metadata && metadata->HasObjectPtr()) { 208 lldb::LanguageType language = metadata->GetObjectPtrLanguage(); 209 if (language == lldb::eLanguageTypeC_plus_plus) { 210 if (m_enforce_valid_object) { 211 lldb::VariableListSP variable_list_sp( 212 function_block->GetBlockVariableList(true)); 213 214 const char *thisErrorString = "Stopped in a context claiming to " 215 "capture a C++ object pointer, but " 216 "'this' isn't available; pretending we " 217 "are in a generic context"; 218 219 if (!variable_list_sp) { 220 err.SetErrorString(thisErrorString); 221 return; 222 } 223 224 lldb::VariableSP this_var_sp( 225 variable_list_sp->FindVariable(ConstString("this"))); 226 227 if (!this_var_sp || !this_var_sp->IsInScope(frame) || 228 !this_var_sp->LocationIsValidForFrame(frame)) { 229 err.SetErrorString(thisErrorString); 230 return; 231 } 232 } 233 234 m_in_cplusplus_method = true; 235 m_needs_object_ptr = true; 236 } else if (language == lldb::eLanguageTypeObjC) { 237 if (m_enforce_valid_object) { 238 lldb::VariableListSP variable_list_sp( 239 function_block->GetBlockVariableList(true)); 240 241 const char *selfErrorString = 242 "Stopped in a context claiming to capture an Objective-C object " 243 "pointer, but 'self' isn't available; pretending we are in a " 244 "generic context"; 245 246 if (!variable_list_sp) { 247 err.SetErrorString(selfErrorString); 248 return; 249 } 250 251 lldb::VariableSP self_variable_sp = 252 variable_list_sp->FindVariable(ConstString("self")); 253 254 if (!self_variable_sp || !self_variable_sp->IsInScope(frame) || 255 !self_variable_sp->LocationIsValidForFrame(frame)) { 256 err.SetErrorString(selfErrorString); 257 return; 258 } 259 260 Type *self_type = self_variable_sp->GetType(); 261 262 if (!self_type) { 263 err.SetErrorString(selfErrorString); 264 return; 265 } 266 267 CompilerType self_clang_type = self_type->GetForwardCompilerType(); 268 269 if (!self_clang_type) { 270 err.SetErrorString(selfErrorString); 271 return; 272 } 273 274 if (ClangASTContext::IsObjCClassType(self_clang_type)) { 275 return; 276 } else if (ClangASTContext::IsObjCObjectPointerType( 277 self_clang_type)) { 278 m_in_objectivec_method = true; 279 m_needs_object_ptr = true; 280 } else { 281 err.SetErrorString(selfErrorString); 282 return; 283 } 284 } else { 285 m_in_objectivec_method = true; 286 m_needs_object_ptr = true; 287 } 288 } 289 } 290 } 291 } 292 293 // This is a really nasty hack, meant to fix Objective-C expressions of the form 294 // (int)[myArray count]. Right now, because the type information for count is 295 // not available, [myArray count] returns id, which can't be directly cast to 296 // int without causing a clang error. 297 static void ApplyObjcCastHack(std::string &expr) { 298 #define OBJC_CAST_HACK_FROM "(int)[" 299 #define OBJC_CAST_HACK_TO "(int)(long long)[" 300 301 size_t from_offset; 302 303 while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos) 304 expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, 305 OBJC_CAST_HACK_TO); 306 307 #undef OBJC_CAST_HACK_TO 308 #undef OBJC_CAST_HACK_FROM 309 } 310 311 bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager, 312 ExecutionContext &exe_ctx, 313 lldb_private::ExecutionPolicy execution_policy, 314 bool keep_result_in_memory, 315 bool generate_debug_info) { 316 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 317 318 Status err; 319 320 InstallContext(exe_ctx); 321 322 if (Target *target = exe_ctx.GetTargetPtr()) { 323 if (PersistentExpressionState *persistent_state = 324 target->GetPersistentExpressionStateForLanguage( 325 lldb::eLanguageTypeC)) { 326 m_result_delegate.RegisterPersistentState(persistent_state); 327 } else { 328 diagnostic_manager.PutString( 329 eDiagnosticSeverityError, 330 "couldn't start parsing (no persistent data)"); 331 return false; 332 } 333 } else { 334 diagnostic_manager.PutString(eDiagnosticSeverityError, 335 "error: couldn't start parsing (no target)"); 336 return false; 337 } 338 339 ScanContext(exe_ctx, err); 340 341 if (!err.Success()) { 342 diagnostic_manager.PutString(eDiagnosticSeverityWarning, err.AsCString()); 343 } 344 345 //////////////////////////////////// 346 // Generate the expression 347 // 348 349 ApplyObjcCastHack(m_expr_text); 350 351 std::string prefix = m_expr_prefix; 352 353 if (ClangModulesDeclVendor *decl_vendor = 354 m_target->GetClangModulesDeclVendor()) { 355 const ClangModulesDeclVendor::ModuleVector &hand_imported_modules = 356 llvm::cast<ClangPersistentVariables>( 357 m_target->GetPersistentExpressionStateForLanguage( 358 lldb::eLanguageTypeC)) 359 ->GetHandLoadedClangModules(); 360 ClangModulesDeclVendor::ModuleVector modules_for_macros; 361 362 for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) { 363 modules_for_macros.push_back(module); 364 } 365 366 if (m_target->GetEnableAutoImportClangModules()) { 367 if (StackFrame *frame = exe_ctx.GetFramePtr()) { 368 if (Block *block = frame->GetFrameBlock()) { 369 SymbolContext sc; 370 371 block->CalculateSymbolContext(&sc); 372 373 if (sc.comp_unit) { 374 StreamString error_stream; 375 376 decl_vendor->AddModulesForCompileUnit( 377 *sc.comp_unit, modules_for_macros, error_stream); 378 } 379 } 380 } 381 } 382 } 383 384 lldb::LanguageType lang_type = lldb::eLanguageTypeUnknown; 385 386 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) { 387 m_transformed_text = m_expr_text; 388 } else { 389 std::unique_ptr<ExpressionSourceCode> source_code( 390 ExpressionSourceCode::CreateWrapped(prefix.c_str(), 391 m_expr_text.c_str())); 392 393 if (m_in_cplusplus_method) 394 lang_type = lldb::eLanguageTypeC_plus_plus; 395 else if (m_in_objectivec_method) 396 lang_type = lldb::eLanguageTypeObjC; 397 else 398 lang_type = lldb::eLanguageTypeC; 399 400 if (!source_code->GetText(m_transformed_text, lang_type, m_in_static_method, 401 exe_ctx)) { 402 diagnostic_manager.PutString(eDiagnosticSeverityError, 403 "couldn't construct expression body"); 404 return false; 405 } 406 } 407 408 if (log) 409 log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str()); 410 411 //////////////////////////////////// 412 // Set up the target and compiler 413 // 414 415 Target *target = exe_ctx.GetTargetPtr(); 416 417 if (!target) { 418 diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid target"); 419 return false; 420 } 421 422 ////////////////////////// 423 // Parse the expression 424 // 425 426 m_materializer_ap.reset(new Materializer()); 427 428 ResetDeclMap(exe_ctx, m_result_delegate, keep_result_in_memory); 429 430 class OnExit { 431 public: 432 typedef std::function<void(void)> Callback; 433 434 OnExit(Callback const &callback) : m_callback(callback) {} 435 436 ~OnExit() { m_callback(); } 437 438 private: 439 Callback m_callback; 440 }; 441 442 OnExit on_exit([this]() { ResetDeclMap(); }); 443 444 if (!DeclMap()->WillParse(exe_ctx, m_materializer_ap.get())) { 445 diagnostic_manager.PutString( 446 eDiagnosticSeverityError, 447 "current process state is unsuitable for expression parsing"); 448 449 ResetDeclMap(); // We are being careful here in the case of breakpoint 450 // conditions. 451 452 return false; 453 } 454 455 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) { 456 DeclMap()->SetLookupsEnabled(true); 457 } 458 459 Process *process = exe_ctx.GetProcessPtr(); 460 ExecutionContextScope *exe_scope = process; 461 462 if (!exe_scope) 463 exe_scope = exe_ctx.GetTargetPtr(); 464 465 // We use a shared pointer here so we can use the original parser - if it 466 // succeeds 467 // or the rewrite parser we might make if it fails. But the parser_sp will 468 // never be empty. 469 470 ClangExpressionParser parser(exe_scope, *this, generate_debug_info); 471 472 unsigned num_errors = parser.Parse(diagnostic_manager); 473 474 // Check here for FixItHints. If there are any try to apply the fixits and 475 // set the fixed text in m_fixed_text 476 // before returning an error. 477 if (num_errors) { 478 if (diagnostic_manager.HasFixIts()) { 479 if (parser.RewriteExpression(diagnostic_manager)) { 480 size_t fixed_start; 481 size_t fixed_end; 482 const std::string &fixed_expression = 483 diagnostic_manager.GetFixedExpression(); 484 if (ExpressionSourceCode::GetOriginalBodyBounds( 485 fixed_expression, lang_type, fixed_start, fixed_end)) 486 m_fixed_text = 487 fixed_expression.substr(fixed_start, fixed_end - fixed_start); 488 } 489 } 490 491 ResetDeclMap(); // We are being careful here in the case of breakpoint 492 // conditions. 493 494 return false; 495 } 496 497 ////////////////////////////////////////////////////////////////////////////////////////// 498 // Prepare the output of the parser for execution, evaluating it statically if 499 // possible 500 // 501 502 { 503 Status jit_error = parser.PrepareForExecution( 504 m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx, 505 m_can_interpret, execution_policy); 506 507 if (!jit_error.Success()) { 508 const char *error_cstr = jit_error.AsCString(); 509 if (error_cstr && error_cstr[0]) 510 diagnostic_manager.PutString(eDiagnosticSeverityError, error_cstr); 511 else 512 diagnostic_manager.PutString(eDiagnosticSeverityError, 513 "expression can't be interpreted or run"); 514 return false; 515 } 516 } 517 518 if (exe_ctx.GetProcessPtr() && execution_policy == eExecutionPolicyTopLevel) { 519 Status static_init_error = 520 parser.RunStaticInitializers(m_execution_unit_sp, exe_ctx); 521 522 if (!static_init_error.Success()) { 523 const char *error_cstr = static_init_error.AsCString(); 524 if (error_cstr && error_cstr[0]) 525 diagnostic_manager.Printf(eDiagnosticSeverityError, 526 "couldn't run static initializers: %s\n", 527 error_cstr); 528 else 529 diagnostic_manager.PutString(eDiagnosticSeverityError, 530 "couldn't run static initializers\n"); 531 return false; 532 } 533 } 534 535 if (m_execution_unit_sp) { 536 bool register_execution_unit = false; 537 538 if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) { 539 register_execution_unit = true; 540 } 541 542 // If there is more than one external function in the execution 543 // unit, it needs to keep living even if it's not top level, because 544 // the result could refer to that function. 545 546 if (m_execution_unit_sp->GetJittedFunctions().size() > 1) { 547 register_execution_unit = true; 548 } 549 550 if (register_execution_unit) { 551 llvm::cast<PersistentExpressionState>( 552 exe_ctx.GetTargetPtr()->GetPersistentExpressionStateForLanguage( 553 m_language)) 554 ->RegisterExecutionUnit(m_execution_unit_sp); 555 } 556 } 557 558 if (generate_debug_info) { 559 lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule()); 560 561 if (jit_module_sp) { 562 ConstString const_func_name(FunctionName()); 563 FileSpec jit_file; 564 jit_file.GetFilename() = const_func_name; 565 jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString()); 566 m_jit_module_wp = jit_module_sp; 567 target->GetImages().Append(jit_module_sp); 568 } 569 } 570 571 ResetDeclMap(); // Make this go away since we don't need any of its state 572 // after parsing. This also gets rid of any 573 // ClangASTImporter::Minions. 574 575 if (process && m_jit_start_addr != LLDB_INVALID_ADDRESS) 576 m_jit_process_wp = lldb::ProcessWP(process->shared_from_this()); 577 return true; 578 } 579 580 bool ClangUserExpression::AddArguments(ExecutionContext &exe_ctx, 581 std::vector<lldb::addr_t> &args, 582 lldb::addr_t struct_address, 583 DiagnosticManager &diagnostic_manager) { 584 lldb::addr_t object_ptr = LLDB_INVALID_ADDRESS; 585 lldb::addr_t cmd_ptr = LLDB_INVALID_ADDRESS; 586 587 if (m_needs_object_ptr) { 588 lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP(); 589 if (!frame_sp) 590 return true; 591 592 ConstString object_name; 593 594 if (m_in_cplusplus_method) { 595 object_name.SetCString("this"); 596 } else if (m_in_objectivec_method) { 597 object_name.SetCString("self"); 598 } else { 599 diagnostic_manager.PutString( 600 eDiagnosticSeverityError, 601 "need object pointer but don't know the language"); 602 return false; 603 } 604 605 Status object_ptr_error; 606 607 object_ptr = GetObjectPointer(frame_sp, object_name, object_ptr_error); 608 609 if (!object_ptr_error.Success()) { 610 exe_ctx.GetTargetRef().GetDebugger().GetAsyncOutputStream()->Printf( 611 "warning: `%s' is not accessible (substituting 0)\n", 612 object_name.AsCString()); 613 object_ptr = 0; 614 } 615 616 if (m_in_objectivec_method) { 617 ConstString cmd_name("_cmd"); 618 619 cmd_ptr = GetObjectPointer(frame_sp, cmd_name, object_ptr_error); 620 621 if (!object_ptr_error.Success()) { 622 diagnostic_manager.Printf( 623 eDiagnosticSeverityWarning, 624 "couldn't get cmd pointer (substituting NULL): %s", 625 object_ptr_error.AsCString()); 626 cmd_ptr = 0; 627 } 628 } 629 630 args.push_back(object_ptr); 631 632 if (m_in_objectivec_method) 633 args.push_back(cmd_ptr); 634 635 args.push_back(struct_address); 636 } else { 637 args.push_back(struct_address); 638 } 639 return true; 640 } 641 642 lldb::ExpressionVariableSP ClangUserExpression::GetResultAfterDematerialization( 643 ExecutionContextScope *exe_scope) { 644 return m_result_delegate.GetVariable(); 645 } 646 647 void ClangUserExpression::ClangUserExpressionHelper::ResetDeclMap( 648 ExecutionContext &exe_ctx, 649 Materializer::PersistentVariableDelegate &delegate, 650 bool keep_result_in_memory) { 651 m_expr_decl_map_up.reset( 652 new ClangExpressionDeclMap(keep_result_in_memory, &delegate, exe_ctx)); 653 } 654 655 clang::ASTConsumer * 656 ClangUserExpression::ClangUserExpressionHelper::ASTTransformer( 657 clang::ASTConsumer *passthrough) { 658 m_result_synthesizer_up.reset( 659 new ASTResultSynthesizer(passthrough, m_top_level, m_target)); 660 661 return m_result_synthesizer_up.get(); 662 } 663 664 void ClangUserExpression::ClangUserExpressionHelper::CommitPersistentDecls() { 665 if (m_result_synthesizer_up.get()) { 666 m_result_synthesizer_up->CommitPersistentDecls(); 667 } 668 } 669 670 ClangUserExpression::ResultDelegate::ResultDelegate() {} 671 672 ConstString ClangUserExpression::ResultDelegate::GetName() { 673 return m_persistent_state->GetNextPersistentVariableName(); 674 } 675 676 void ClangUserExpression::ResultDelegate::DidDematerialize( 677 lldb::ExpressionVariableSP &variable) { 678 m_variable = variable; 679 } 680 681 void ClangUserExpression::ResultDelegate::RegisterPersistentState( 682 PersistentExpressionState *persistent_state) { 683 m_persistent_state = persistent_state; 684 } 685 686 lldb::ExpressionVariableSP &ClangUserExpression::ResultDelegate::GetVariable() { 687 return m_variable; 688 } 689