1 //===-- ClangExpressionDeclMap.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 "ClangExpressionDeclMap.h" 11 12 #include "ASTDumper.h" 13 #include "ClangASTSource.h" 14 #include "ClangModulesDeclVendor.h" 15 #include "ClangPersistentVariables.h" 16 17 #include "clang/AST/ASTConsumer.h" 18 #include "clang/AST/ASTContext.h" 19 #include "clang/AST/DeclarationName.h" 20 #include "clang/AST/Decl.h" 21 #include "lldb/lldb-private.h" 22 #include "lldb/Core/Address.h" 23 #include "lldb/Core/Error.h" 24 #include "lldb/Core/Log.h" 25 #include "lldb/Core/Module.h" 26 #include "lldb/Core/ModuleSpec.h" 27 #include "lldb/Core/RegisterValue.h" 28 #include "lldb/Core/ValueObjectConstResult.h" 29 #include "lldb/Core/ValueObjectVariable.h" 30 #include "lldb/Expression/Materializer.h" 31 #include "lldb/Host/Endian.h" 32 #include "lldb/Symbol/ClangASTContext.h" 33 #include "lldb/Symbol/CompilerDecl.h" 34 #include "lldb/Symbol/CompilerDeclContext.h" 35 #include "lldb/Symbol/CompileUnit.h" 36 #include "lldb/Symbol/Function.h" 37 #include "lldb/Symbol/ObjectFile.h" 38 #include "lldb/Symbol/SymbolContext.h" 39 #include "lldb/Symbol/SymbolFile.h" 40 #include "lldb/Symbol/SymbolVendor.h" 41 #include "lldb/Symbol/Type.h" 42 #include "lldb/Symbol/TypeList.h" 43 #include "lldb/Symbol/Variable.h" 44 #include "lldb/Symbol/VariableList.h" 45 #include "lldb/Target/CPPLanguageRuntime.h" 46 #include "lldb/Target/ExecutionContext.h" 47 #include "lldb/Target/ObjCLanguageRuntime.h" 48 #include "lldb/Target/Process.h" 49 #include "lldb/Target/RegisterContext.h" 50 #include "lldb/Target/StackFrame.h" 51 #include "lldb/Target/Target.h" 52 #include "lldb/Target/Thread.h" 53 54 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" 55 56 using namespace lldb; 57 using namespace lldb_private; 58 using namespace clang; 59 60 ClangExpressionDeclMap::ClangExpressionDeclMap (bool keep_result_in_memory, 61 Materializer::PersistentVariableDelegate *result_delegate, 62 ExecutionContext &exe_ctx) : 63 ClangASTSource (exe_ctx.GetTargetSP()), 64 m_found_entities (), 65 m_struct_members (), 66 m_keep_result_in_memory (keep_result_in_memory), 67 m_result_delegate (result_delegate), 68 m_parser_vars (), 69 m_struct_vars () 70 { 71 EnableStructVars(); 72 } 73 74 ClangExpressionDeclMap::~ClangExpressionDeclMap() 75 { 76 // Note: The model is now that the parser's AST context and all associated 77 // data does not vanish until the expression has been executed. This means 78 // that valuable lookup data (like namespaces) doesn't vanish, but 79 80 DidParse(); 81 DisableStructVars(); 82 } 83 84 bool 85 ClangExpressionDeclMap::WillParse(ExecutionContext &exe_ctx, 86 Materializer *materializer) 87 { 88 ClangASTMetrics::ClearLocalCounters(); 89 90 EnableParserVars(); 91 m_parser_vars->m_exe_ctx = exe_ctx; 92 93 Target *target = exe_ctx.GetTargetPtr(); 94 if (exe_ctx.GetFramePtr()) 95 m_parser_vars->m_sym_ctx = exe_ctx.GetFramePtr()->GetSymbolContext(lldb::eSymbolContextEverything); 96 else if (exe_ctx.GetThreadPtr() && exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)) 97 m_parser_vars->m_sym_ctx = exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)->GetSymbolContext(lldb::eSymbolContextEverything); 98 else if (exe_ctx.GetProcessPtr()) 99 { 100 m_parser_vars->m_sym_ctx.Clear(true); 101 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP(); 102 } 103 else if (target) 104 { 105 m_parser_vars->m_sym_ctx.Clear(true); 106 m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP(); 107 } 108 109 if (target) 110 { 111 m_parser_vars->m_persistent_vars = llvm::cast<ClangPersistentVariables>(target->GetPersistentExpressionStateForLanguage(eLanguageTypeC)); 112 113 if (!target->GetScratchClangASTContext()) 114 return false; 115 } 116 117 m_parser_vars->m_target_info = GetTargetInfo(); 118 m_parser_vars->m_materializer = materializer; 119 120 return true; 121 } 122 123 void 124 ClangExpressionDeclMap::InstallCodeGenerator (clang::ASTConsumer *code_gen) 125 { 126 assert(m_parser_vars); 127 m_parser_vars->m_code_gen = code_gen; 128 } 129 130 void 131 ClangExpressionDeclMap::DidParse() 132 { 133 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 134 135 if (log) 136 ClangASTMetrics::DumpCounters(log); 137 138 if (m_parser_vars.get()) 139 { 140 for (size_t entity_index = 0, num_entities = m_found_entities.GetSize(); 141 entity_index < num_entities; 142 ++entity_index) 143 { 144 ExpressionVariableSP var_sp(m_found_entities.GetVariableAtIndex(entity_index)); 145 if (var_sp) 146 llvm::cast<ClangExpressionVariable>(var_sp.get())->DisableParserVars(GetParserID()); 147 } 148 149 for (size_t pvar_index = 0, num_pvars = m_parser_vars->m_persistent_vars->GetSize(); 150 pvar_index < num_pvars; 151 ++pvar_index) 152 { 153 ExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariableAtIndex(pvar_index)); 154 if (ClangExpressionVariable *clang_var = llvm::dyn_cast<ClangExpressionVariable>(pvar_sp.get())) 155 clang_var->DisableParserVars(GetParserID()); 156 } 157 158 DisableParserVars(); 159 } 160 } 161 162 // Interface for IRForTarget 163 164 ClangExpressionDeclMap::TargetInfo 165 ClangExpressionDeclMap::GetTargetInfo() 166 { 167 assert (m_parser_vars.get()); 168 169 TargetInfo ret; 170 171 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx; 172 173 Process *process = exe_ctx.GetProcessPtr(); 174 if (process) 175 { 176 ret.byte_order = process->GetByteOrder(); 177 ret.address_byte_size = process->GetAddressByteSize(); 178 } 179 else 180 { 181 Target *target = exe_ctx.GetTargetPtr(); 182 if (target) 183 { 184 ret.byte_order = target->GetArchitecture().GetByteOrder(); 185 ret.address_byte_size = target->GetArchitecture().GetAddressByteSize(); 186 } 187 } 188 189 return ret; 190 } 191 192 bool 193 ClangExpressionDeclMap::AddPersistentVariable 194 ( 195 const NamedDecl *decl, 196 const ConstString &name, 197 TypeFromParser parser_type, 198 bool is_result, 199 bool is_lvalue 200 ) 201 { 202 assert (m_parser_vars.get()); 203 204 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(parser_type.GetTypeSystem()); 205 if (ast == nullptr) 206 return false; 207 208 if (m_parser_vars->m_materializer && is_result) 209 { 210 Error err; 211 212 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx; 213 Target *target = exe_ctx.GetTargetPtr(); 214 if (target == nullptr) 215 return false; 216 217 ClangASTContext *context(target->GetScratchClangASTContext()); 218 219 TypeFromUser user_type(m_ast_importer_sp->DeportType(context->getASTContext(), 220 ast->getASTContext(), 221 parser_type.GetOpaqueQualType()), 222 context); 223 224 uint32_t offset = m_parser_vars->m_materializer->AddResultVariable(user_type, 225 is_lvalue, 226 m_keep_result_in_memory, 227 m_result_delegate, 228 err); 229 230 ClangExpressionVariable *var = new ClangExpressionVariable(exe_ctx.GetBestExecutionContextScope(), 231 name, 232 user_type, 233 m_parser_vars->m_target_info.byte_order, 234 m_parser_vars->m_target_info.address_byte_size); 235 236 m_found_entities.AddNewlyConstructedVariable(var); 237 238 var->EnableParserVars(GetParserID()); 239 240 ClangExpressionVariable::ParserVars *parser_vars = var->GetParserVars(GetParserID()); 241 242 parser_vars->m_named_decl = decl; 243 parser_vars->m_parser_type = parser_type; 244 245 var->EnableJITVars(GetParserID()); 246 247 ClangExpressionVariable::JITVars *jit_vars = var->GetJITVars(GetParserID()); 248 249 jit_vars->m_offset = offset; 250 251 return true; 252 } 253 254 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 255 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx; 256 Target *target = exe_ctx.GetTargetPtr(); 257 if (target == NULL) 258 return false; 259 260 ClangASTContext *context(target->GetScratchClangASTContext()); 261 262 TypeFromUser user_type(m_ast_importer_sp->DeportType(context->getASTContext(), 263 ast->getASTContext(), 264 parser_type.GetOpaqueQualType()), 265 context); 266 267 if (!user_type.GetOpaqueQualType()) 268 { 269 if (log) 270 log->Printf("Persistent variable's type wasn't copied successfully"); 271 return false; 272 } 273 274 if (!m_parser_vars->m_target_info.IsValid()) 275 return false; 276 277 ClangExpressionVariable *var = llvm::cast<ClangExpressionVariable>(m_parser_vars->m_persistent_vars->CreatePersistentVariable (exe_ctx.GetBestExecutionContextScope (), 278 name, 279 user_type, 280 m_parser_vars->m_target_info.byte_order, 281 m_parser_vars->m_target_info.address_byte_size).get()); 282 283 if (!var) 284 return false; 285 286 var->m_frozen_sp->SetHasCompleteType(); 287 288 if (is_result) 289 var->m_flags |= ClangExpressionVariable::EVNeedsFreezeDry; 290 else 291 var->m_flags |= ClangExpressionVariable::EVKeepInTarget; // explicitly-declared persistent variables should persist 292 293 if (is_lvalue) 294 { 295 var->m_flags |= ClangExpressionVariable::EVIsProgramReference; 296 } 297 else 298 { 299 var->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated; 300 var->m_flags |= ClangExpressionVariable::EVNeedsAllocation; 301 } 302 303 if (m_keep_result_in_memory) 304 { 305 var->m_flags |= ClangExpressionVariable::EVKeepInTarget; 306 } 307 308 if (log) 309 log->Printf("Created persistent variable with flags 0x%hx", var->m_flags); 310 311 var->EnableParserVars(GetParserID()); 312 313 ClangExpressionVariable::ParserVars *parser_vars = var->GetParserVars(GetParserID()); 314 315 parser_vars->m_named_decl = decl; 316 parser_vars->m_parser_type = parser_type; 317 318 return true; 319 } 320 321 bool 322 ClangExpressionDeclMap::AddValueToStruct 323 ( 324 const NamedDecl *decl, 325 const ConstString &name, 326 llvm::Value *value, 327 size_t size, 328 lldb::offset_t alignment 329 ) 330 { 331 assert (m_struct_vars.get()); 332 assert (m_parser_vars.get()); 333 334 bool is_persistent_variable = false; 335 336 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 337 338 m_struct_vars->m_struct_laid_out = false; 339 340 if (ClangExpressionVariable::FindVariableInList(m_struct_members, decl, GetParserID())) 341 return true; 342 343 ClangExpressionVariable *var(ClangExpressionVariable::FindVariableInList(m_found_entities, decl, GetParserID())); 344 345 if (!var) 346 { 347 var = ClangExpressionVariable::FindVariableInList(*m_parser_vars->m_persistent_vars, decl, GetParserID()); 348 is_persistent_variable = true; 349 } 350 351 if (!var) 352 return false; 353 354 if (log) 355 log->Printf("Adding value for (NamedDecl*)%p [%s - %s] to the structure", 356 static_cast<const void*>(decl), name.GetCString(), 357 var->GetName().GetCString()); 358 359 // We know entity->m_parser_vars is valid because we used a parser variable 360 // to find it 361 362 ClangExpressionVariable::ParserVars *parser_vars = llvm::cast<ClangExpressionVariable>(var)->GetParserVars(GetParserID()); 363 364 parser_vars->m_llvm_value = value; 365 366 if (ClangExpressionVariable::JITVars *jit_vars = llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID())) 367 { 368 // We already laid this out; do not touch 369 370 if (log) 371 log->Printf("Already placed at 0x%llx", (unsigned long long)jit_vars->m_offset); 372 } 373 374 llvm::cast<ClangExpressionVariable>(var)->EnableJITVars(GetParserID()); 375 376 ClangExpressionVariable::JITVars *jit_vars = llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID()); 377 378 jit_vars->m_alignment = alignment; 379 jit_vars->m_size = size; 380 381 m_struct_members.AddVariable(var->shared_from_this()); 382 383 if (m_parser_vars->m_materializer) 384 { 385 uint32_t offset = 0; 386 387 Error err; 388 389 if (is_persistent_variable) 390 { 391 ExpressionVariableSP var_sp(var->shared_from_this()); 392 offset = m_parser_vars->m_materializer->AddPersistentVariable(var_sp, nullptr, err); 393 } 394 else 395 { 396 if (const lldb_private::Symbol *sym = parser_vars->m_lldb_sym) 397 offset = m_parser_vars->m_materializer->AddSymbol(*sym, err); 398 else if (const RegisterInfo *reg_info = var->GetRegisterInfo()) 399 offset = m_parser_vars->m_materializer->AddRegister(*reg_info, err); 400 else if (parser_vars->m_lldb_var) 401 offset = m_parser_vars->m_materializer->AddVariable(parser_vars->m_lldb_var, err); 402 } 403 404 if (!err.Success()) 405 return false; 406 407 if (log) 408 log->Printf("Placed at 0x%llx", (unsigned long long)offset); 409 410 jit_vars->m_offset = offset; // TODO DoStructLayout() should not change this. 411 } 412 413 return true; 414 } 415 416 bool 417 ClangExpressionDeclMap::DoStructLayout () 418 { 419 assert (m_struct_vars.get()); 420 421 if (m_struct_vars->m_struct_laid_out) 422 return true; 423 424 if (!m_parser_vars->m_materializer) 425 return false; 426 427 m_struct_vars->m_struct_alignment = m_parser_vars->m_materializer->GetStructAlignment(); 428 m_struct_vars->m_struct_size = m_parser_vars->m_materializer->GetStructByteSize(); 429 m_struct_vars->m_struct_laid_out = true; 430 return true; 431 } 432 433 bool ClangExpressionDeclMap::GetStructInfo 434 ( 435 uint32_t &num_elements, 436 size_t &size, 437 lldb::offset_t &alignment 438 ) 439 { 440 assert (m_struct_vars.get()); 441 442 if (!m_struct_vars->m_struct_laid_out) 443 return false; 444 445 num_elements = m_struct_members.GetSize(); 446 size = m_struct_vars->m_struct_size; 447 alignment = m_struct_vars->m_struct_alignment; 448 449 return true; 450 } 451 452 bool 453 ClangExpressionDeclMap::GetStructElement 454 ( 455 const NamedDecl *&decl, 456 llvm::Value *&value, 457 lldb::offset_t &offset, 458 ConstString &name, 459 uint32_t index 460 ) 461 { 462 assert (m_struct_vars.get()); 463 464 if (!m_struct_vars->m_struct_laid_out) 465 return false; 466 467 if (index >= m_struct_members.GetSize()) 468 return false; 469 470 ExpressionVariableSP member_sp(m_struct_members.GetVariableAtIndex(index)); 471 472 if (!member_sp) 473 return false; 474 475 ClangExpressionVariable::ParserVars *parser_vars = llvm::cast<ClangExpressionVariable>(member_sp.get())->GetParserVars(GetParserID()); 476 ClangExpressionVariable::JITVars *jit_vars = llvm::cast<ClangExpressionVariable>(member_sp.get())->GetJITVars(GetParserID()); 477 478 if (!parser_vars || 479 !jit_vars || 480 !member_sp->GetValueObject()) 481 return false; 482 483 decl = parser_vars->m_named_decl; 484 value = parser_vars->m_llvm_value; 485 offset = jit_vars->m_offset; 486 name = member_sp->GetName(); 487 488 return true; 489 } 490 491 bool 492 ClangExpressionDeclMap::GetFunctionInfo 493 ( 494 const NamedDecl *decl, 495 uint64_t &ptr 496 ) 497 { 498 ClangExpressionVariable *entity(ClangExpressionVariable::FindVariableInList(m_found_entities, decl, GetParserID())); 499 500 if (!entity) 501 return false; 502 503 // We know m_parser_vars is valid since we searched for the variable by 504 // its NamedDecl 505 506 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID()); 507 508 ptr = parser_vars->m_lldb_value.GetScalar().ULongLong(); 509 510 return true; 511 } 512 513 static void 514 FindCodeSymbolInContext 515 ( 516 const ConstString &name, 517 SymbolContext &sym_ctx, 518 uint32_t name_type_mask, 519 SymbolContextList &sc_list 520 ) 521 { 522 sc_list.Clear(); 523 SymbolContextList temp_sc_list; 524 if (sym_ctx.module_sp) 525 sym_ctx.module_sp->FindFunctions(name, 526 NULL, 527 name_type_mask, 528 true, // include_symbols 529 false, // include_inlines 530 true, // append 531 temp_sc_list); 532 if (temp_sc_list.GetSize() == 0) 533 { 534 if (sym_ctx.target_sp) 535 sym_ctx.target_sp->GetImages().FindFunctions(name, 536 name_type_mask, 537 true, // include_symbols 538 false, // include_inlines 539 true, // append 540 temp_sc_list); 541 } 542 543 SymbolContextList internal_symbol_sc_list; 544 unsigned temp_sc_list_size = temp_sc_list.GetSize(); 545 for (unsigned i = 0; i < temp_sc_list_size; i++) 546 { 547 SymbolContext sc; 548 temp_sc_list.GetContextAtIndex(i, sc); 549 if (sc.function) 550 { 551 sc_list.Append(sc); 552 } 553 else if (sc.symbol) 554 { 555 if (sc.symbol->IsExternal()) 556 { 557 sc_list.Append(sc); 558 } 559 else 560 { 561 internal_symbol_sc_list.Append(sc); 562 } 563 } 564 } 565 566 // If we had internal symbols and we didn't find any external symbols or 567 // functions in debug info, then fallback to the internal symbols 568 if (sc_list.GetSize() == 0 && internal_symbol_sc_list.GetSize()) 569 { 570 sc_list = internal_symbol_sc_list; 571 } 572 } 573 574 ConstString 575 FindBestAlternateMangledName 576 ( 577 const ConstString &demangled, 578 const LanguageType &lang_type, 579 SymbolContext &sym_ctx 580 ) 581 { 582 CPlusPlusLanguage::MethodName cpp_name(demangled); 583 std::string scope_qualified_name = cpp_name.GetScopeQualifiedName(); 584 585 if (!scope_qualified_name.size()) 586 return ConstString(); 587 588 if (!sym_ctx.module_sp) 589 return ConstString(); 590 591 SymbolVendor *sym_vendor = sym_ctx.module_sp->GetSymbolVendor(); 592 if (!sym_vendor) 593 return ConstString(); 594 595 lldb_private::SymbolFile *sym_file = sym_vendor->GetSymbolFile(); 596 if (!sym_file) 597 return ConstString(); 598 599 std::vector<ConstString> alternates; 600 sym_file->GetMangledNamesForFunction(scope_qualified_name, alternates); 601 602 std::vector<ConstString> param_and_qual_matches; 603 std::vector<ConstString> param_matches; 604 for (size_t i = 0; i < alternates.size(); i++) 605 { 606 ConstString alternate_mangled_name = alternates[i]; 607 Mangled mangled(alternate_mangled_name, true); 608 ConstString demangled = mangled.GetDemangledName(lang_type); 609 610 CPlusPlusLanguage::MethodName alternate_cpp_name(demangled); 611 if (!cpp_name.IsValid()) 612 continue; 613 614 if (alternate_cpp_name.GetArguments() == cpp_name.GetArguments()) 615 { 616 if (alternate_cpp_name.GetQualifiers() == cpp_name.GetQualifiers()) 617 param_and_qual_matches.push_back(alternate_mangled_name); 618 else 619 param_matches.push_back(alternate_mangled_name); 620 } 621 } 622 623 if (param_and_qual_matches.size()) 624 return param_and_qual_matches[0]; // It is assumed that there will be only one! 625 else if (param_matches.size()) 626 return param_matches[0]; // Return one of them as a best match 627 else 628 return ConstString(); 629 } 630 631 bool 632 ClangExpressionDeclMap::GetFunctionAddress 633 ( 634 const ConstString &name, 635 uint64_t &func_addr 636 ) 637 { 638 assert (m_parser_vars.get()); 639 640 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 641 ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx; 642 Target *target = exe_ctx.GetTargetPtr(); 643 // Back out in all cases where we're not fully initialized 644 if (target == NULL) 645 return false; 646 if (!m_parser_vars->m_sym_ctx.target_sp) 647 return false; 648 649 SymbolContextList sc_list; 650 651 FindCodeSymbolInContext(name, m_parser_vars->m_sym_ctx, eFunctionNameTypeAuto, sc_list); 652 653 uint32_t sc_list_size = sc_list.GetSize(); 654 655 if (sc_list_size == 0) 656 { 657 SymbolContext &sc = m_parser_vars->m_sym_ctx; 658 if (sc.comp_unit) 659 { 660 LanguageType lang_type = sc.comp_unit->GetLanguage(); 661 if (Language::LanguageIsCPlusPlus(lang_type) && 662 CPlusPlusLanguage::IsCPPMangledName(name.AsCString())) 663 { 664 Mangled mangled(name, true); 665 ConstString demangled = mangled.GetDemangledName(lang_type); 666 667 if (demangled) 668 { 669 ConstString best_alternate_mangled_name = FindBestAlternateMangledName(demangled, lang_type, sc); 670 if (best_alternate_mangled_name) 671 { 672 FindCodeSymbolInContext( 673 best_alternate_mangled_name, m_parser_vars->m_sym_ctx, eFunctionNameTypeAuto, sc_list); 674 sc_list_size = sc_list.GetSize(); 675 } 676 677 if (sc_list_size == 0) 678 { 679 FindCodeSymbolInContext( 680 demangled, m_parser_vars->m_sym_ctx, eFunctionNameTypeFull, sc_list); 681 sc_list_size = sc_list.GetSize(); 682 } 683 } 684 } 685 } 686 } 687 688 if (sc_list_size == 0) 689 { 690 // We occasionally get debug information in which a const function is reported 691 // as non-const, so the mangled name is wrong. This is a hack to compensate. 692 693 if (!strncmp(name.GetCString(), "_ZN", 3) && 694 strncmp(name.GetCString(), "_ZNK", 4)) 695 { 696 std::string fixed_scratch("_ZNK"); 697 fixed_scratch.append(name.GetCString() + 3); 698 ConstString fixed_name(fixed_scratch.c_str()); 699 700 if (log) 701 log->Printf("Failed to find symbols given non-const name %s; trying %s", name.GetCString(), fixed_name.GetCString()); 702 703 FindCodeSymbolInContext( 704 fixed_name, m_parser_vars->m_sym_ctx, eFunctionNameTypeAuto, sc_list); 705 sc_list_size = sc_list.GetSize(); 706 } 707 } 708 709 lldb::addr_t intern_callable_load_addr = LLDB_INVALID_ADDRESS; 710 711 for (uint32_t i=0; i<sc_list_size; ++i) 712 { 713 SymbolContext sym_ctx; 714 sc_list.GetContextAtIndex(i, sym_ctx); 715 716 717 lldb::addr_t callable_load_addr = LLDB_INVALID_ADDRESS; 718 719 if (sym_ctx.function) 720 { 721 const Address func_so_addr = sym_ctx.function->GetAddressRange().GetBaseAddress(); 722 if (func_so_addr.IsValid()) 723 { 724 callable_load_addr = func_so_addr.GetCallableLoadAddress(target, false); 725 } 726 } 727 else if (sym_ctx.symbol) 728 { 729 if (sym_ctx.symbol->IsExternal()) 730 callable_load_addr = sym_ctx.symbol->ResolveCallableAddress(*target); 731 else 732 { 733 if (intern_callable_load_addr == LLDB_INVALID_ADDRESS) 734 intern_callable_load_addr = sym_ctx.symbol->ResolveCallableAddress(*target); 735 } 736 } 737 738 if (callable_load_addr != LLDB_INVALID_ADDRESS) 739 { 740 func_addr = callable_load_addr; 741 return true; 742 } 743 } 744 745 // See if we found an internal symbol 746 if (intern_callable_load_addr != LLDB_INVALID_ADDRESS) 747 { 748 func_addr = intern_callable_load_addr; 749 return true; 750 } 751 752 return false; 753 } 754 755 addr_t 756 ClangExpressionDeclMap::GetSymbolAddress (Target &target, 757 Process *process, 758 const ConstString &name, 759 lldb::SymbolType symbol_type, 760 lldb_private::Module *module) 761 { 762 SymbolContextList sc_list; 763 764 if (module) 765 module->FindSymbolsWithNameAndType(name, symbol_type, sc_list); 766 else 767 target.GetImages().FindSymbolsWithNameAndType(name, symbol_type, sc_list); 768 769 const uint32_t num_matches = sc_list.GetSize(); 770 addr_t symbol_load_addr = LLDB_INVALID_ADDRESS; 771 772 for (uint32_t i=0; i<num_matches && (symbol_load_addr == 0 || symbol_load_addr == LLDB_INVALID_ADDRESS); i++) 773 { 774 SymbolContext sym_ctx; 775 sc_list.GetContextAtIndex(i, sym_ctx); 776 777 const Address sym_address = sym_ctx.symbol->GetAddress(); 778 779 if (!sym_address.IsValid()) 780 continue; 781 782 switch (sym_ctx.symbol->GetType()) 783 { 784 case eSymbolTypeCode: 785 case eSymbolTypeTrampoline: 786 symbol_load_addr = sym_address.GetCallableLoadAddress (&target); 787 break; 788 789 case eSymbolTypeResolver: 790 symbol_load_addr = sym_address.GetCallableLoadAddress (&target, true); 791 break; 792 793 case eSymbolTypeReExported: 794 { 795 ConstString reexport_name = sym_ctx.symbol->GetReExportedSymbolName(); 796 if (reexport_name) 797 { 798 ModuleSP reexport_module_sp; 799 ModuleSpec reexport_module_spec; 800 reexport_module_spec.GetPlatformFileSpec() = sym_ctx.symbol->GetReExportedSymbolSharedLibrary(); 801 if (reexport_module_spec.GetPlatformFileSpec()) 802 { 803 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec); 804 if (!reexport_module_sp) 805 { 806 reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear(); 807 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec); 808 } 809 } 810 symbol_load_addr = GetSymbolAddress(target, process, sym_ctx.symbol->GetReExportedSymbolName(), symbol_type, reexport_module_sp.get()); 811 } 812 } 813 break; 814 815 case eSymbolTypeData: 816 case eSymbolTypeRuntime: 817 case eSymbolTypeVariable: 818 case eSymbolTypeLocal: 819 case eSymbolTypeParam: 820 case eSymbolTypeInvalid: 821 case eSymbolTypeAbsolute: 822 case eSymbolTypeException: 823 case eSymbolTypeSourceFile: 824 case eSymbolTypeHeaderFile: 825 case eSymbolTypeObjectFile: 826 case eSymbolTypeCommonBlock: 827 case eSymbolTypeBlock: 828 case eSymbolTypeVariableType: 829 case eSymbolTypeLineEntry: 830 case eSymbolTypeLineHeader: 831 case eSymbolTypeScopeBegin: 832 case eSymbolTypeScopeEnd: 833 case eSymbolTypeAdditional: 834 case eSymbolTypeCompiler: 835 case eSymbolTypeInstrumentation: 836 case eSymbolTypeUndefined: 837 case eSymbolTypeObjCClass: 838 case eSymbolTypeObjCMetaClass: 839 case eSymbolTypeObjCIVar: 840 symbol_load_addr = sym_address.GetLoadAddress (&target); 841 break; 842 } 843 } 844 845 if (symbol_load_addr == LLDB_INVALID_ADDRESS && process) 846 { 847 ObjCLanguageRuntime *runtime = process->GetObjCLanguageRuntime(); 848 849 if (runtime) 850 { 851 symbol_load_addr = runtime->LookupRuntimeSymbol(name); 852 } 853 } 854 855 return symbol_load_addr; 856 } 857 858 addr_t 859 ClangExpressionDeclMap::GetSymbolAddress (const ConstString &name, lldb::SymbolType symbol_type) 860 { 861 assert (m_parser_vars.get()); 862 863 if (!m_parser_vars->m_exe_ctx.GetTargetPtr()) 864 return false; 865 866 return GetSymbolAddress(m_parser_vars->m_exe_ctx.GetTargetRef(), m_parser_vars->m_exe_ctx.GetProcessPtr(), name, symbol_type); 867 } 868 869 const Symbol * 870 ClangExpressionDeclMap::FindGlobalDataSymbol (Target &target, 871 const ConstString &name, 872 lldb_private::Module *module) 873 { 874 SymbolContextList sc_list; 875 876 if (module) 877 module->FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list); 878 else 879 target.GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeAny, sc_list); 880 881 const uint32_t matches = sc_list.GetSize(); 882 for (uint32_t i=0; i<matches; ++i) 883 { 884 SymbolContext sym_ctx; 885 sc_list.GetContextAtIndex(i, sym_ctx); 886 if (sym_ctx.symbol) 887 { 888 const Symbol *symbol = sym_ctx.symbol; 889 const Address sym_address = symbol->GetAddress(); 890 891 if (sym_address.IsValid()) 892 { 893 switch (symbol->GetType()) 894 { 895 case eSymbolTypeData: 896 case eSymbolTypeRuntime: 897 case eSymbolTypeAbsolute: 898 case eSymbolTypeObjCClass: 899 case eSymbolTypeObjCMetaClass: 900 case eSymbolTypeObjCIVar: 901 if (symbol->GetDemangledNameIsSynthesized()) 902 { 903 // If the demangled name was synthesized, then don't use it 904 // for expressions. Only let the symbol match if the mangled 905 // named matches for these symbols. 906 if (symbol->GetMangled().GetMangledName() != name) 907 break; 908 } 909 return symbol; 910 911 case eSymbolTypeReExported: 912 { 913 ConstString reexport_name = symbol->GetReExportedSymbolName(); 914 if (reexport_name) 915 { 916 ModuleSP reexport_module_sp; 917 ModuleSpec reexport_module_spec; 918 reexport_module_spec.GetPlatformFileSpec() = symbol->GetReExportedSymbolSharedLibrary(); 919 if (reexport_module_spec.GetPlatformFileSpec()) 920 { 921 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec); 922 if (!reexport_module_sp) 923 { 924 reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear(); 925 reexport_module_sp = target.GetImages().FindFirstModule(reexport_module_spec); 926 } 927 } 928 // Don't allow us to try and resolve a re-exported symbol if it is the same 929 // as the current symbol 930 if (name == symbol->GetReExportedSymbolName() && module == reexport_module_sp.get()) 931 return NULL; 932 933 return FindGlobalDataSymbol(target, symbol->GetReExportedSymbolName(), reexport_module_sp.get()); 934 } 935 } 936 break; 937 938 case eSymbolTypeCode: // We already lookup functions elsewhere 939 case eSymbolTypeVariable: 940 case eSymbolTypeLocal: 941 case eSymbolTypeParam: 942 case eSymbolTypeTrampoline: 943 case eSymbolTypeInvalid: 944 case eSymbolTypeException: 945 case eSymbolTypeSourceFile: 946 case eSymbolTypeHeaderFile: 947 case eSymbolTypeObjectFile: 948 case eSymbolTypeCommonBlock: 949 case eSymbolTypeBlock: 950 case eSymbolTypeVariableType: 951 case eSymbolTypeLineEntry: 952 case eSymbolTypeLineHeader: 953 case eSymbolTypeScopeBegin: 954 case eSymbolTypeScopeEnd: 955 case eSymbolTypeAdditional: 956 case eSymbolTypeCompiler: 957 case eSymbolTypeInstrumentation: 958 case eSymbolTypeUndefined: 959 case eSymbolTypeResolver: 960 break; 961 } 962 } 963 } 964 } 965 966 return NULL; 967 } 968 969 lldb::VariableSP 970 ClangExpressionDeclMap::FindGlobalVariable 971 ( 972 Target &target, 973 ModuleSP &module, 974 const ConstString &name, 975 CompilerDeclContext *namespace_decl, 976 TypeFromUser *type 977 ) 978 { 979 VariableList vars; 980 981 if (module && namespace_decl) 982 module->FindGlobalVariables (name, namespace_decl, true, -1, vars); 983 else 984 target.GetImages().FindGlobalVariables(name, true, -1, vars); 985 986 if (vars.GetSize()) 987 { 988 if (type) 989 { 990 for (size_t i = 0; i < vars.GetSize(); ++i) 991 { 992 VariableSP var_sp = vars.GetVariableAtIndex(i); 993 994 if (ClangASTContext::AreTypesSame(*type, var_sp->GetType()->GetFullCompilerType ())) 995 return var_sp; 996 } 997 } 998 else 999 { 1000 return vars.GetVariableAtIndex(0); 1001 } 1002 } 1003 1004 return VariableSP(); 1005 } 1006 1007 // Interface for ClangASTSource 1008 1009 void 1010 ClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context) 1011 { 1012 assert (m_ast_context); 1013 1014 ClangASTMetrics::RegisterVisibleQuery(); 1015 1016 const ConstString name(context.m_decl_name.getAsString().c_str()); 1017 1018 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 1019 1020 if (GetImportInProgress()) 1021 { 1022 if (log && log->GetVerbose()) 1023 log->Printf("Ignoring a query during an import"); 1024 return; 1025 } 1026 1027 static unsigned int invocation_id = 0; 1028 unsigned int current_id = invocation_id++; 1029 1030 if (log) 1031 { 1032 if (!context.m_decl_context) 1033 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in a NULL DeclContext", current_id, name.GetCString()); 1034 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context)) 1035 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in '%s'", current_id, name.GetCString(), context_named_decl->getNameAsString().c_str()); 1036 else 1037 log->Printf("ClangExpressionDeclMap::FindExternalVisibleDecls[%u] for '%s' in a '%s'", current_id, name.GetCString(), context.m_decl_context->getDeclKindName()); 1038 } 1039 1040 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context)) 1041 { 1042 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer_sp->GetNamespaceMap(namespace_context); 1043 1044 if (log && log->GetVerbose()) 1045 log->Printf(" CEDM::FEVD[%u] Inspecting (NamespaceMap*)%p (%d entries)", 1046 current_id, static_cast<void*>(namespace_map.get()), 1047 (int)namespace_map->size()); 1048 1049 if (!namespace_map) 1050 return; 1051 1052 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end(); 1053 i != e; 1054 ++i) 1055 { 1056 if (log) 1057 log->Printf(" CEDM::FEVD[%u] Searching namespace %s in module %s", 1058 current_id, 1059 i->second.GetName().AsCString(), 1060 i->first->GetFileSpec().GetFilename().GetCString()); 1061 1062 FindExternalVisibleDecls(context, 1063 i->first, 1064 i->second, 1065 current_id); 1066 } 1067 } 1068 else if (isa<TranslationUnitDecl>(context.m_decl_context)) 1069 { 1070 CompilerDeclContext namespace_decl; 1071 1072 if (log) 1073 log->Printf(" CEDM::FEVD[%u] Searching the root namespace", current_id); 1074 1075 FindExternalVisibleDecls(context, 1076 lldb::ModuleSP(), 1077 namespace_decl, 1078 current_id); 1079 } 1080 1081 if (!context.m_found.variable) 1082 ClangASTSource::FindExternalVisibleDecls(context); 1083 } 1084 1085 void 1086 ClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context, 1087 lldb::ModuleSP module_sp, 1088 CompilerDeclContext &namespace_decl, 1089 unsigned int current_id) 1090 { 1091 assert (m_ast_context); 1092 1093 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 1094 1095 SymbolContextList sc_list; 1096 1097 const ConstString name(context.m_decl_name.getAsString().c_str()); 1098 1099 const char *name_unique_cstr = name.GetCString(); 1100 1101 if (name_unique_cstr == NULL) 1102 return; 1103 1104 static ConstString id_name("id"); 1105 static ConstString Class_name("Class"); 1106 1107 if (name == id_name || name == Class_name) 1108 return; 1109 1110 // Only look for functions by name out in our symbols if the function 1111 // doesn't start with our phony prefix of '$' 1112 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 1113 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr(); 1114 SymbolContext sym_ctx; 1115 if (frame != nullptr) 1116 sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction|lldb::eSymbolContextBlock); 1117 if (name_unique_cstr[0] == '$' && !namespace_decl) 1118 { 1119 static ConstString g_lldb_class_name ("$__lldb_class"); 1120 1121 if (name == g_lldb_class_name) 1122 { 1123 // Clang is looking for the type of "this" 1124 1125 if (frame == NULL) 1126 return; 1127 1128 1129 // Find the block that defines the function represented by "sym_ctx" 1130 Block *function_block = sym_ctx.GetFunctionBlock(); 1131 1132 if (!function_block) 1133 return; 1134 1135 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext(); 1136 1137 if (!function_decl_ctx) 1138 return; 1139 1140 clang::CXXMethodDecl *method_decl = ClangASTContext::DeclContextGetAsCXXMethodDecl(function_decl_ctx); 1141 1142 if (method_decl) 1143 { 1144 clang::CXXRecordDecl *class_decl = method_decl->getParent(); 1145 1146 QualType class_qual_type(class_decl->getTypeForDecl(), 0); 1147 1148 TypeFromUser class_user_type (class_qual_type.getAsOpaquePtr(), 1149 ClangASTContext::GetASTContext(&class_decl->getASTContext())); 1150 1151 if (log) 1152 { 1153 ASTDumper ast_dumper(class_qual_type); 1154 log->Printf(" CEDM::FEVD[%u] Adding type for $__lldb_class: %s", current_id, ast_dumper.GetCString()); 1155 } 1156 1157 AddThisType(context, class_user_type, current_id); 1158 1159 if (method_decl->isInstance()) 1160 { 1161 // self is a pointer to the object 1162 1163 QualType class_pointer_type = method_decl->getASTContext().getPointerType(class_qual_type); 1164 1165 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(), 1166 ClangASTContext::GetASTContext(&method_decl->getASTContext())); 1167 1168 m_struct_vars->m_object_pointer_type = self_user_type; 1169 } 1170 } 1171 else 1172 { 1173 // This branch will get hit if we are executing code in the context of a function that 1174 // claims to have an object pointer (through DW_AT_object_pointer?) but is not formally a 1175 // method of the class. In that case, just look up the "this" variable in the current 1176 // scope and use its type. 1177 // FIXME: This code is formally correct, but clang doesn't currently emit DW_AT_object_pointer 1178 // for C++ so it hasn't actually been tested. 1179 1180 VariableList *vars = frame->GetVariableList(false); 1181 1182 lldb::VariableSP this_var = vars->FindVariable(ConstString("this")); 1183 1184 if (this_var && 1185 this_var->IsInScope(frame) && 1186 this_var->LocationIsValidForFrame (frame)) 1187 { 1188 Type *this_type = this_var->GetType(); 1189 1190 if (!this_type) 1191 return; 1192 1193 TypeFromUser pointee_type = this_type->GetForwardCompilerType ().GetPointeeType(); 1194 1195 if (pointee_type.IsValid()) 1196 { 1197 if (log) 1198 { 1199 ASTDumper ast_dumper(pointee_type); 1200 log->Printf(" FEVD[%u] Adding type for $__lldb_class: %s", current_id, ast_dumper.GetCString()); 1201 } 1202 1203 AddThisType(context, pointee_type, current_id); 1204 TypeFromUser this_user_type(this_type->GetFullCompilerType ()); 1205 m_struct_vars->m_object_pointer_type = this_user_type; 1206 return; 1207 } 1208 } 1209 } 1210 1211 return; 1212 } 1213 1214 static ConstString g_lldb_objc_class_name ("$__lldb_objc_class"); 1215 if (name == g_lldb_objc_class_name) 1216 { 1217 // Clang is looking for the type of "*self" 1218 1219 if (!frame) 1220 return; 1221 1222 SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction|lldb::eSymbolContextBlock); 1223 1224 // Find the block that defines the function represented by "sym_ctx" 1225 Block *function_block = sym_ctx.GetFunctionBlock(); 1226 1227 if (!function_block) 1228 return; 1229 1230 CompilerDeclContext function_decl_ctx = function_block->GetDeclContext(); 1231 1232 if (!function_decl_ctx) 1233 return; 1234 1235 clang::ObjCMethodDecl *method_decl = ClangASTContext::DeclContextGetAsObjCMethodDecl(function_decl_ctx); 1236 1237 if (method_decl) 1238 { 1239 ObjCInterfaceDecl* self_interface = method_decl->getClassInterface(); 1240 1241 if (!self_interface) 1242 return; 1243 1244 const clang::Type *interface_type = self_interface->getTypeForDecl(); 1245 1246 if (!interface_type) 1247 return; // This is unlikely, but we have seen crashes where this occurred 1248 1249 TypeFromUser class_user_type(QualType(interface_type, 0).getAsOpaquePtr(), 1250 ClangASTContext::GetASTContext(&method_decl->getASTContext())); 1251 1252 if (log) 1253 { 1254 ASTDumper ast_dumper(interface_type); 1255 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString()); 1256 } 1257 1258 AddOneType(context, class_user_type, current_id); 1259 1260 if (method_decl->isInstanceMethod()) 1261 { 1262 // self is a pointer to the object 1263 1264 QualType class_pointer_type = method_decl->getASTContext().getObjCObjectPointerType(QualType(interface_type, 0)); 1265 1266 TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(), 1267 ClangASTContext::GetASTContext(&method_decl->getASTContext())); 1268 1269 m_struct_vars->m_object_pointer_type = self_user_type; 1270 } 1271 else 1272 { 1273 // self is a Class pointer 1274 QualType class_type = method_decl->getASTContext().getObjCClassType(); 1275 1276 TypeFromUser self_user_type(class_type.getAsOpaquePtr(), 1277 ClangASTContext::GetASTContext(&method_decl->getASTContext())); 1278 1279 m_struct_vars->m_object_pointer_type = self_user_type; 1280 } 1281 1282 return; 1283 } 1284 else 1285 { 1286 // This branch will get hit if we are executing code in the context of a function that 1287 // claims to have an object pointer (through DW_AT_object_pointer?) but is not formally a 1288 // method of the class. In that case, just look up the "self" variable in the current 1289 // scope and use its type. 1290 1291 VariableList *vars = frame->GetVariableList(false); 1292 1293 lldb::VariableSP self_var = vars->FindVariable(ConstString("self")); 1294 1295 if (self_var && 1296 self_var->IsInScope(frame) && 1297 self_var->LocationIsValidForFrame (frame)) 1298 { 1299 Type *self_type = self_var->GetType(); 1300 1301 if (!self_type) 1302 return; 1303 1304 CompilerType self_clang_type = self_type->GetFullCompilerType (); 1305 1306 if (ClangASTContext::IsObjCClassType(self_clang_type)) 1307 { 1308 return; 1309 } 1310 else if (ClangASTContext::IsObjCObjectPointerType(self_clang_type)) 1311 { 1312 self_clang_type = self_clang_type.GetPointeeType(); 1313 1314 if (!self_clang_type) 1315 return; 1316 1317 if (log) 1318 { 1319 ASTDumper ast_dumper(self_type->GetFullCompilerType ()); 1320 log->Printf(" FEVD[%u] Adding type for $__lldb_objc_class: %s", current_id, ast_dumper.GetCString()); 1321 } 1322 1323 TypeFromUser class_user_type (self_clang_type); 1324 1325 AddOneType(context, class_user_type, current_id); 1326 1327 TypeFromUser self_user_type(self_type->GetFullCompilerType ()); 1328 1329 m_struct_vars->m_object_pointer_type = self_user_type; 1330 return; 1331 } 1332 } 1333 } 1334 1335 return; 1336 } 1337 1338 // any other $__lldb names should be weeded out now 1339 if (!::strncmp(name_unique_cstr, "$__lldb", sizeof("$__lldb") - 1)) 1340 return; 1341 1342 do 1343 { 1344 if (!target) 1345 break; 1346 1347 ClangASTContext *scratch_clang_ast_context = target->GetScratchClangASTContext(); 1348 1349 if (!scratch_clang_ast_context) 1350 break; 1351 1352 ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext(); 1353 1354 if (!scratch_ast_context) 1355 break; 1356 1357 TypeDecl *ptype_type_decl = m_parser_vars->m_persistent_vars->GetPersistentType(name); 1358 1359 if (!ptype_type_decl) 1360 break; 1361 1362 Decl *parser_ptype_decl = m_ast_importer_sp->CopyDecl(m_ast_context, scratch_ast_context, ptype_type_decl); 1363 1364 if (!parser_ptype_decl) 1365 break; 1366 1367 TypeDecl *parser_ptype_type_decl = dyn_cast<TypeDecl>(parser_ptype_decl); 1368 1369 if (!parser_ptype_type_decl) 1370 break; 1371 1372 if (log) 1373 log->Printf(" CEDM::FEVD[%u] Found persistent type %s", current_id, name.GetCString()); 1374 1375 context.AddNamedDecl(parser_ptype_type_decl); 1376 } while (0); 1377 1378 ExpressionVariableSP pvar_sp(m_parser_vars->m_persistent_vars->GetVariable(name)); 1379 1380 if (pvar_sp) 1381 { 1382 AddOneVariable(context, pvar_sp, current_id); 1383 return; 1384 } 1385 1386 const char *reg_name(&name.GetCString()[1]); 1387 1388 if (m_parser_vars->m_exe_ctx.GetRegisterContext()) 1389 { 1390 const RegisterInfo *reg_info(m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName(reg_name)); 1391 1392 if (reg_info) 1393 { 1394 if (log) 1395 log->Printf(" CEDM::FEVD[%u] Found register %s", current_id, reg_info->name); 1396 1397 AddOneRegister(context, reg_info, current_id); 1398 } 1399 } 1400 } 1401 else 1402 { 1403 ValueObjectSP valobj; 1404 VariableSP var; 1405 1406 if (frame && !namespace_decl) 1407 { 1408 CompilerDeclContext compiler_decl_context = sym_ctx.block != nullptr ? sym_ctx.block->GetDeclContext() : CompilerDeclContext(); 1409 1410 if (compiler_decl_context) 1411 { 1412 // Make sure that the variables are parsed so that we have the declarations 1413 VariableListSP vars = frame->GetInScopeVariableList(true); 1414 for (size_t i = 0; i < vars->GetSize(); i++) 1415 vars->GetVariableAtIndex(i)->GetDecl(); 1416 1417 // Search for declarations matching the name 1418 std::vector<CompilerDecl> found_decls = compiler_decl_context.FindDeclByName(name); 1419 1420 bool variable_found = false; 1421 for (CompilerDecl decl : found_decls) 1422 { 1423 var = decl.GetAsVariable(); 1424 if (var) 1425 { 1426 variable_found = true; 1427 valobj = ValueObjectVariable::Create(frame, var); 1428 AddOneVariable(context, var, valobj, current_id); 1429 context.m_found.variable = true; 1430 } 1431 } 1432 if (variable_found) 1433 return; 1434 } 1435 } 1436 if (target) 1437 { 1438 var = FindGlobalVariable (*target, 1439 module_sp, 1440 name, 1441 &namespace_decl, 1442 NULL); 1443 1444 if (var) 1445 { 1446 valobj = ValueObjectVariable::Create(target, var); 1447 AddOneVariable(context, var, valobj, current_id); 1448 context.m_found.variable = true; 1449 return; 1450 } 1451 } 1452 1453 std::vector<clang::NamedDecl *> decls_from_modules; 1454 1455 if (target) 1456 { 1457 if (ClangModulesDeclVendor *decl_vendor = target->GetClangModulesDeclVendor()) 1458 { 1459 decl_vendor->FindDecls(name, false, UINT32_MAX, decls_from_modules); 1460 } 1461 } 1462 1463 if (!context.m_found.variable) 1464 { 1465 const bool include_inlines = false; 1466 const bool append = false; 1467 1468 if (namespace_decl && module_sp) 1469 { 1470 const bool include_symbols = false; 1471 1472 module_sp->FindFunctions(name, 1473 &namespace_decl, 1474 eFunctionNameTypeBase, 1475 include_symbols, 1476 include_inlines, 1477 append, 1478 sc_list); 1479 } 1480 else if (target && !namespace_decl) 1481 { 1482 const bool include_symbols = true; 1483 1484 // TODO Fix FindFunctions so that it doesn't return 1485 // instance methods for eFunctionNameTypeBase. 1486 1487 target->GetImages().FindFunctions(name, 1488 eFunctionNameTypeFull, 1489 include_symbols, 1490 include_inlines, 1491 append, 1492 sc_list); 1493 } 1494 1495 // If we found more than one function, see if we can use the 1496 // frame's decl context to remove functions that are shadowed 1497 // by other functions which match in type but are nearer in scope. 1498 // 1499 // AddOneFunction will not add a function whose type has already been 1500 // added, so if there's another function in the list with a matching 1501 // type, check to see if their decl context is a parent of the current 1502 // frame's or was imported via a and using statement, and pick the 1503 // best match according to lookup rules. 1504 if (sc_list.GetSize() > 1) 1505 { 1506 // Collect some info about our frame's context. 1507 StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr(); 1508 SymbolContext frame_sym_ctx; 1509 if (frame != nullptr) 1510 frame_sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction|lldb::eSymbolContextBlock); 1511 CompilerDeclContext frame_decl_context = frame_sym_ctx.block != nullptr ? frame_sym_ctx.block->GetDeclContext() : CompilerDeclContext(); 1512 1513 // We can't do this without a compiler decl context for our frame. 1514 if (frame_decl_context) 1515 { 1516 clang::DeclContext *frame_decl_ctx = (clang::DeclContext *)frame_decl_context.GetOpaqueDeclContext(); 1517 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(frame_decl_context.GetTypeSystem()); 1518 1519 // Structure to hold the info needed when comparing function 1520 // declarations. 1521 struct FuncDeclInfo 1522 { 1523 ConstString m_name; 1524 CompilerType m_copied_type; 1525 uint32_t m_decl_lvl; 1526 SymbolContext m_sym_ctx; 1527 }; 1528 1529 // First, symplify things by looping through the symbol contexts 1530 // to remove unwanted functions and separate out the functions we 1531 // want to compare and prune into a separate list. 1532 // Cache the info needed about the function declarations in a 1533 // vector for efficiency. 1534 SymbolContextList sc_sym_list; 1535 uint32_t num_indices = sc_list.GetSize(); 1536 std::vector<FuncDeclInfo> fdi_cache; 1537 fdi_cache.reserve(num_indices); 1538 for (uint32_t index = 0; index < num_indices; ++index) 1539 { 1540 FuncDeclInfo fdi; 1541 SymbolContext sym_ctx; 1542 sc_list.GetContextAtIndex(index, sym_ctx); 1543 1544 // We don't know enough about symbols to compare them, 1545 // but we should keep them in the list. 1546 Function *function = sym_ctx.function; 1547 if (!function) 1548 { 1549 sc_sym_list.Append(sym_ctx); 1550 continue; 1551 } 1552 // Filter out functions without declaration contexts, as well as 1553 // class/instance methods, since they'll be skipped in the 1554 // code that follows anyway. 1555 CompilerDeclContext func_decl_context = function->GetDeclContext(); 1556 if (!func_decl_context || func_decl_context.IsClassMethod(nullptr, nullptr, nullptr)) 1557 continue; 1558 // We can only prune functions for which we can copy the type. 1559 CompilerType func_clang_type = function->GetType()->GetFullCompilerType(); 1560 CompilerType copied_func_type = GuardedCopyType(func_clang_type); 1561 if (!copied_func_type) 1562 { 1563 sc_sym_list.Append(sym_ctx); 1564 continue; 1565 } 1566 1567 fdi.m_sym_ctx = sym_ctx; 1568 fdi.m_name = function->GetName(); 1569 fdi.m_copied_type = copied_func_type; 1570 fdi.m_decl_lvl = LLDB_INVALID_DECL_LEVEL; 1571 if (fdi.m_copied_type && func_decl_context) 1572 { 1573 // Call CountDeclLevels to get the number of parent scopes we 1574 // have to look through before we find the function declaration. 1575 // When comparing functions of the same type, the one with a 1576 // lower count will be closer to us in the lookup scope and 1577 // shadows the other. 1578 clang::DeclContext *func_decl_ctx = (clang::DeclContext *)func_decl_context.GetOpaqueDeclContext(); 1579 fdi.m_decl_lvl = ast->CountDeclLevels(frame_decl_ctx, 1580 func_decl_ctx, 1581 &fdi.m_name, 1582 &fdi.m_copied_type); 1583 } 1584 fdi_cache.emplace_back(fdi); 1585 } 1586 1587 // Loop through the functions in our cache looking for matching types, 1588 // then compare their scope levels to see which is closer. 1589 std::multimap<CompilerType, const FuncDeclInfo*> matches; 1590 for (const FuncDeclInfo &fdi : fdi_cache) 1591 { 1592 const CompilerType t = fdi.m_copied_type; 1593 auto q = matches.find(t); 1594 if (q != matches.end()) 1595 { 1596 if (q->second->m_decl_lvl > fdi.m_decl_lvl) 1597 // This function is closer; remove the old set. 1598 matches.erase(t); 1599 else if (q->second->m_decl_lvl < fdi.m_decl_lvl) 1600 // The functions in our set are closer - skip this one. 1601 continue; 1602 } 1603 matches.insert(std::make_pair(t, &fdi)); 1604 } 1605 1606 // Loop through our matches and add their symbol contexts to our list. 1607 SymbolContextList sc_func_list; 1608 for (const auto &q : matches) 1609 sc_func_list.Append(q.second->m_sym_ctx); 1610 1611 // Rejoin the lists with the functions in front. 1612 sc_list = sc_func_list; 1613 sc_list.Append(sc_sym_list); 1614 } 1615 } 1616 1617 if (sc_list.GetSize()) 1618 { 1619 Symbol *extern_symbol = NULL; 1620 Symbol *non_extern_symbol = NULL; 1621 1622 for (uint32_t index = 0, num_indices = sc_list.GetSize(); 1623 index < num_indices; 1624 ++index) 1625 { 1626 SymbolContext sym_ctx; 1627 sc_list.GetContextAtIndex(index, sym_ctx); 1628 1629 if (sym_ctx.function) 1630 { 1631 CompilerDeclContext decl_ctx = sym_ctx.function->GetDeclContext(); 1632 1633 if (!decl_ctx) 1634 continue; 1635 1636 // Filter out class/instance methods. 1637 if (decl_ctx.IsClassMethod(nullptr, nullptr, nullptr)) 1638 continue; 1639 1640 AddOneFunction(context, sym_ctx.function, NULL, current_id); 1641 context.m_found.function_with_type_info = true; 1642 context.m_found.function = true; 1643 } 1644 else if (sym_ctx.symbol) 1645 { 1646 if (sym_ctx.symbol->GetType() == eSymbolTypeReExported && target) 1647 { 1648 sym_ctx.symbol = sym_ctx.symbol->ResolveReExportedSymbol(*target); 1649 if (sym_ctx.symbol == NULL) 1650 continue; 1651 } 1652 1653 if (sym_ctx.symbol->IsExternal()) 1654 extern_symbol = sym_ctx.symbol; 1655 else 1656 non_extern_symbol = sym_ctx.symbol; 1657 } 1658 } 1659 1660 if (!context.m_found.function_with_type_info) 1661 { 1662 for (clang::NamedDecl *decl : decls_from_modules) 1663 { 1664 if (llvm::isa<clang::FunctionDecl>(decl)) 1665 { 1666 clang::NamedDecl *copied_decl = llvm::cast<FunctionDecl>(m_ast_importer_sp->CopyDecl(m_ast_context, &decl->getASTContext(), decl)); 1667 context.AddNamedDecl(copied_decl); 1668 context.m_found.function_with_type_info = true; 1669 } 1670 } 1671 } 1672 1673 if (!context.m_found.function_with_type_info) 1674 { 1675 if (extern_symbol) 1676 { 1677 AddOneFunction (context, NULL, extern_symbol, current_id); 1678 context.m_found.function = true; 1679 } 1680 else if (non_extern_symbol) 1681 { 1682 AddOneFunction (context, NULL, non_extern_symbol, current_id); 1683 context.m_found.function = true; 1684 } 1685 } 1686 } 1687 1688 if (!context.m_found.function_with_type_info) 1689 { 1690 // Try the modules next. 1691 1692 do 1693 { 1694 if (ClangModulesDeclVendor *modules_decl_vendor = m_target->GetClangModulesDeclVendor()) 1695 { 1696 bool append = false; 1697 uint32_t max_matches = 1; 1698 std::vector <clang::NamedDecl *> decls; 1699 1700 if (!modules_decl_vendor->FindDecls(name, 1701 append, 1702 max_matches, 1703 decls)) 1704 break; 1705 1706 clang::NamedDecl *const decl_from_modules = decls[0]; 1707 1708 if (llvm::isa<clang::FunctionDecl>(decl_from_modules)) 1709 { 1710 if (log) 1711 { 1712 log->Printf(" CAS::FEVD[%u] Matching function found for \"%s\" in the modules", 1713 current_id, 1714 name.GetCString()); 1715 } 1716 1717 clang::Decl *copied_decl = m_ast_importer_sp->CopyDecl(m_ast_context, &decl_from_modules->getASTContext(), decl_from_modules); 1718 clang::FunctionDecl *copied_function_decl = copied_decl ? dyn_cast<clang::FunctionDecl>(copied_decl) : nullptr; 1719 1720 if (!copied_function_decl) 1721 { 1722 if (log) 1723 log->Printf(" CAS::FEVD[%u] - Couldn't export a function declaration from the modules", 1724 current_id); 1725 1726 break; 1727 } 1728 1729 if (copied_function_decl->getBody() && m_parser_vars->m_code_gen) 1730 { 1731 DeclGroupRef decl_group_ref(copied_function_decl); 1732 m_parser_vars->m_code_gen->HandleTopLevelDecl(decl_group_ref); 1733 } 1734 1735 context.AddNamedDecl(copied_function_decl); 1736 1737 context.m_found.function_with_type_info = true; 1738 context.m_found.function = true; 1739 } 1740 else if (llvm::isa<clang::VarDecl>(decl_from_modules)) 1741 { 1742 if (log) 1743 { 1744 log->Printf(" CAS::FEVD[%u] Matching variable found for \"%s\" in the modules", 1745 current_id, 1746 name.GetCString()); 1747 } 1748 1749 clang::Decl *copied_decl = m_ast_importer_sp->CopyDecl(m_ast_context, &decl_from_modules->getASTContext(), decl_from_modules); 1750 clang::VarDecl *copied_var_decl = copied_decl ? dyn_cast_or_null<clang::VarDecl>(copied_decl) : nullptr; 1751 1752 if (!copied_var_decl) 1753 { 1754 if (log) 1755 log->Printf(" CAS::FEVD[%u] - Couldn't export a variable declaration from the modules", 1756 current_id); 1757 1758 break; 1759 } 1760 1761 context.AddNamedDecl(copied_var_decl); 1762 1763 context.m_found.variable = true; 1764 } 1765 } 1766 } while (0); 1767 } 1768 1769 if (target && !context.m_found.variable && !namespace_decl) 1770 { 1771 // We couldn't find a non-symbol variable for this. Now we'll hunt for a generic 1772 // data symbol, and -- if it is found -- treat it as a variable. 1773 1774 const Symbol *data_symbol = FindGlobalDataSymbol(*target, name); 1775 1776 if (data_symbol) 1777 { 1778 std::string warning("got name from symbols: "); 1779 warning.append(name.AsCString()); 1780 const unsigned diag_id = m_ast_context->getDiagnostics().getCustomDiagID(clang::DiagnosticsEngine::Level::Warning, "%0"); 1781 m_ast_context->getDiagnostics().Report(diag_id) << warning.c_str(); 1782 AddOneGenericVariable(context, *data_symbol, current_id); 1783 context.m_found.variable = true; 1784 } 1785 } 1786 } 1787 } 1788 } 1789 1790 //static opaque_compiler_type_t 1791 //MaybePromoteToBlockPointerType 1792 //( 1793 // ASTContext *ast_context, 1794 // opaque_compiler_type_t candidate_type 1795 //) 1796 //{ 1797 // if (!candidate_type) 1798 // return candidate_type; 1799 // 1800 // QualType candidate_qual_type = QualType::getFromOpaquePtr(candidate_type); 1801 // 1802 // const PointerType *candidate_pointer_type = dyn_cast<PointerType>(candidate_qual_type); 1803 // 1804 // if (!candidate_pointer_type) 1805 // return candidate_type; 1806 // 1807 // QualType pointee_qual_type = candidate_pointer_type->getPointeeType(); 1808 // 1809 // const RecordType *pointee_record_type = dyn_cast<RecordType>(pointee_qual_type); 1810 // 1811 // if (!pointee_record_type) 1812 // return candidate_type; 1813 // 1814 // RecordDecl *pointee_record_decl = pointee_record_type->getDecl(); 1815 // 1816 // if (!pointee_record_decl->isRecord()) 1817 // return candidate_type; 1818 // 1819 // if (!pointee_record_decl->getName().startswith(llvm::StringRef("__block_literal_"))) 1820 // return candidate_type; 1821 // 1822 // QualType generic_function_type = ast_context->getFunctionNoProtoType(ast_context->UnknownAnyTy); 1823 // QualType block_pointer_type = ast_context->getBlockPointerType(generic_function_type); 1824 // 1825 // return block_pointer_type.getAsOpaquePtr(); 1826 //} 1827 1828 bool 1829 ClangExpressionDeclMap::GetVariableValue (VariableSP &var, 1830 lldb_private::Value &var_location, 1831 TypeFromUser *user_type, 1832 TypeFromParser *parser_type) 1833 { 1834 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 1835 1836 Type *var_type = var->GetType(); 1837 1838 if (!var_type) 1839 { 1840 if (log) 1841 log->PutCString("Skipped a definition because it has no type"); 1842 return false; 1843 } 1844 1845 CompilerType var_clang_type = var_type->GetFullCompilerType (); 1846 1847 if (!var_clang_type) 1848 { 1849 if (log) 1850 log->PutCString("Skipped a definition because it has no Clang type"); 1851 return false; 1852 } 1853 1854 ClangASTContext *clang_ast = llvm::dyn_cast_or_null<ClangASTContext>(var_type->GetForwardCompilerType().GetTypeSystem()); 1855 1856 if (!clang_ast) 1857 { 1858 if (log) 1859 log->PutCString("Skipped a definition because it has no Clang AST"); 1860 return false; 1861 } 1862 1863 1864 ASTContext *ast = clang_ast->getASTContext(); 1865 1866 if (!ast) 1867 { 1868 if (log) 1869 log->PutCString("There is no AST context for the current execution context"); 1870 return false; 1871 } 1872 //var_clang_type = MaybePromoteToBlockPointerType (ast, var_clang_type); 1873 1874 DWARFExpression &var_location_expr = var->LocationExpression(); 1875 1876 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 1877 Error err; 1878 1879 if (var->GetLocationIsConstantValueData()) 1880 { 1881 DataExtractor const_value_extractor; 1882 1883 if (var_location_expr.GetExpressionData(const_value_extractor)) 1884 { 1885 var_location = Value(const_value_extractor.GetDataStart(), const_value_extractor.GetByteSize()); 1886 var_location.SetValueType(Value::eValueTypeHostAddress); 1887 } 1888 else 1889 { 1890 if (log) 1891 log->Printf("Error evaluating constant variable: %s", err.AsCString()); 1892 return false; 1893 } 1894 } 1895 1896 CompilerType type_to_use = GuardedCopyType(var_clang_type); 1897 1898 if (!type_to_use) 1899 { 1900 if (log) 1901 log->Printf("Couldn't copy a variable's type into the parser's AST context"); 1902 1903 return false; 1904 } 1905 1906 if (parser_type) 1907 *parser_type = TypeFromParser(type_to_use); 1908 1909 if (var_location.GetContextType() == Value::eContextTypeInvalid) 1910 var_location.SetCompilerType(type_to_use); 1911 1912 if (var_location.GetValueType() == Value::eValueTypeFileAddress) 1913 { 1914 SymbolContext var_sc; 1915 var->CalculateSymbolContext(&var_sc); 1916 1917 if (!var_sc.module_sp) 1918 return false; 1919 1920 Address so_addr(var_location.GetScalar().ULongLong(), var_sc.module_sp->GetSectionList()); 1921 1922 lldb::addr_t load_addr = so_addr.GetLoadAddress(target); 1923 1924 if (load_addr != LLDB_INVALID_ADDRESS) 1925 { 1926 var_location.GetScalar() = load_addr; 1927 var_location.SetValueType(Value::eValueTypeLoadAddress); 1928 } 1929 } 1930 1931 if (user_type) 1932 *user_type = TypeFromUser(var_clang_type); 1933 1934 return true; 1935 } 1936 1937 void 1938 ClangExpressionDeclMap::AddOneVariable (NameSearchContext &context, VariableSP var, ValueObjectSP valobj, unsigned int current_id) 1939 { 1940 assert (m_parser_vars.get()); 1941 1942 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 1943 1944 TypeFromUser ut; 1945 TypeFromParser pt; 1946 Value var_location; 1947 1948 if (!GetVariableValue (var, var_location, &ut, &pt)) 1949 return; 1950 1951 clang::QualType parser_opaque_type = QualType::getFromOpaquePtr(pt.GetOpaqueQualType()); 1952 1953 if (parser_opaque_type.isNull()) 1954 return; 1955 1956 if (const clang::Type *parser_type = parser_opaque_type.getTypePtr()) 1957 { 1958 if (const TagType *tag_type = dyn_cast<TagType>(parser_type)) 1959 CompleteType(tag_type->getDecl()); 1960 if (const ObjCObjectPointerType *objc_object_ptr_type = dyn_cast<ObjCObjectPointerType>(parser_type)) 1961 CompleteType(objc_object_ptr_type->getInterfaceDecl()); 1962 } 1963 1964 1965 bool is_reference = pt.IsReferenceType(); 1966 1967 NamedDecl *var_decl = NULL; 1968 if (is_reference) 1969 var_decl = context.AddVarDecl(pt); 1970 else 1971 var_decl = context.AddVarDecl(pt.GetLValueReferenceType()); 1972 1973 std::string decl_name(context.m_decl_name.getAsString()); 1974 ConstString entity_name(decl_name.c_str()); 1975 ClangExpressionVariable *entity(new ClangExpressionVariable(valobj)); 1976 m_found_entities.AddNewlyConstructedVariable(entity); 1977 1978 assert (entity); 1979 entity->EnableParserVars(GetParserID()); 1980 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID()); 1981 parser_vars->m_parser_type = pt; 1982 parser_vars->m_named_decl = var_decl; 1983 parser_vars->m_llvm_value = NULL; 1984 parser_vars->m_lldb_value = var_location; 1985 parser_vars->m_lldb_var = var; 1986 1987 if (is_reference) 1988 entity->m_flags |= ClangExpressionVariable::EVTypeIsReference; 1989 1990 if (log) 1991 { 1992 ASTDumper orig_dumper(ut.GetOpaqueQualType()); 1993 ASTDumper ast_dumper(var_decl); 1994 log->Printf(" CEDM::FEVD[%u] Found variable %s, returned %s (original %s)", current_id, decl_name.c_str(), ast_dumper.GetCString(), orig_dumper.GetCString()); 1995 } 1996 } 1997 1998 void 1999 ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context, 2000 ExpressionVariableSP &pvar_sp, 2001 unsigned int current_id) 2002 { 2003 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 2004 2005 TypeFromUser user_type (llvm::cast<ClangExpressionVariable>(pvar_sp.get())->GetTypeFromUser()); 2006 2007 TypeFromParser parser_type (GuardedCopyType(user_type)); 2008 2009 if (!parser_type.GetOpaqueQualType()) 2010 { 2011 if (log) 2012 log->Printf(" CEDM::FEVD[%u] Couldn't import type for pvar %s", current_id, pvar_sp->GetName().GetCString()); 2013 return; 2014 } 2015 2016 NamedDecl *var_decl = context.AddVarDecl(parser_type.GetLValueReferenceType()); 2017 2018 llvm::cast<ClangExpressionVariable>(pvar_sp.get())->EnableParserVars(GetParserID()); 2019 ClangExpressionVariable::ParserVars *parser_vars = llvm::cast<ClangExpressionVariable>(pvar_sp.get())->GetParserVars(GetParserID()); 2020 parser_vars->m_parser_type = parser_type; 2021 parser_vars->m_named_decl = var_decl; 2022 parser_vars->m_llvm_value = NULL; 2023 parser_vars->m_lldb_value.Clear(); 2024 2025 if (log) 2026 { 2027 ASTDumper ast_dumper(var_decl); 2028 log->Printf(" CEDM::FEVD[%u] Added pvar %s, returned %s", current_id, pvar_sp->GetName().GetCString(), ast_dumper.GetCString()); 2029 } 2030 } 2031 2032 void 2033 ClangExpressionDeclMap::AddOneGenericVariable(NameSearchContext &context, 2034 const Symbol &symbol, 2035 unsigned int current_id) 2036 { 2037 assert(m_parser_vars.get()); 2038 2039 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 2040 2041 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 2042 2043 if (target == NULL) 2044 return; 2045 2046 ASTContext *scratch_ast_context = target->GetScratchClangASTContext()->getASTContext(); 2047 2048 TypeFromUser user_type (ClangASTContext::GetBasicType(scratch_ast_context, eBasicTypeVoid).GetPointerType().GetLValueReferenceType()); 2049 TypeFromParser parser_type (ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid).GetPointerType().GetLValueReferenceType()); 2050 NamedDecl *var_decl = context.AddVarDecl(parser_type); 2051 2052 std::string decl_name(context.m_decl_name.getAsString()); 2053 ConstString entity_name(decl_name.c_str()); 2054 ClangExpressionVariable *entity(new ClangExpressionVariable(m_parser_vars->m_exe_ctx.GetBestExecutionContextScope (), 2055 entity_name, 2056 user_type, 2057 m_parser_vars->m_target_info.byte_order, 2058 m_parser_vars->m_target_info.address_byte_size)); 2059 m_found_entities.AddNewlyConstructedVariable(entity); 2060 2061 entity->EnableParserVars(GetParserID()); 2062 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID()); 2063 2064 const Address symbol_address = symbol.GetAddress(); 2065 lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target); 2066 2067 //parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, user_type.GetOpaqueQualType()); 2068 parser_vars->m_lldb_value.SetCompilerType(user_type); 2069 parser_vars->m_lldb_value.GetScalar() = symbol_load_addr; 2070 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress); 2071 2072 parser_vars->m_parser_type = parser_type; 2073 parser_vars->m_named_decl = var_decl; 2074 parser_vars->m_llvm_value = NULL; 2075 parser_vars->m_lldb_sym = &symbol; 2076 2077 if (log) 2078 { 2079 ASTDumper ast_dumper(var_decl); 2080 2081 log->Printf(" CEDM::FEVD[%u] Found variable %s, returned %s", current_id, decl_name.c_str(), ast_dumper.GetCString()); 2082 } 2083 } 2084 2085 bool 2086 ClangExpressionDeclMap::ResolveUnknownTypes() 2087 { 2088 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 2089 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 2090 2091 ClangASTContext *scratch_ast_context = target->GetScratchClangASTContext(); 2092 2093 for (size_t index = 0, num_entities = m_found_entities.GetSize(); 2094 index < num_entities; 2095 ++index) 2096 { 2097 ExpressionVariableSP entity = m_found_entities.GetVariableAtIndex(index); 2098 2099 ClangExpressionVariable::ParserVars *parser_vars = llvm::cast<ClangExpressionVariable>(entity.get())->GetParserVars(GetParserID()); 2100 2101 if (entity->m_flags & ClangExpressionVariable::EVUnknownType) 2102 { 2103 const NamedDecl *named_decl = parser_vars->m_named_decl; 2104 const VarDecl *var_decl = dyn_cast<VarDecl>(named_decl); 2105 2106 if (!var_decl) 2107 { 2108 if (log) 2109 log->Printf("Entity of unknown type does not have a VarDecl"); 2110 return false; 2111 } 2112 2113 if (log) 2114 { 2115 ASTDumper ast_dumper(const_cast<VarDecl*>(var_decl)); 2116 log->Printf("Variable of unknown type now has Decl %s", ast_dumper.GetCString()); 2117 } 2118 2119 QualType var_type = var_decl->getType(); 2120 TypeFromParser parser_type(var_type.getAsOpaquePtr(), ClangASTContext::GetASTContext(&var_decl->getASTContext())); 2121 2122 lldb::opaque_compiler_type_t copied_type = m_ast_importer_sp->CopyType(scratch_ast_context->getASTContext(), &var_decl->getASTContext(), var_type.getAsOpaquePtr()); 2123 2124 if (!copied_type) 2125 { 2126 if (log) 2127 log->Printf("ClangExpressionDeclMap::ResolveUnknownType - Couldn't import the type for a variable"); 2128 2129 return (bool) lldb::ExpressionVariableSP(); 2130 } 2131 2132 TypeFromUser user_type(copied_type, scratch_ast_context); 2133 2134 // parser_vars->m_lldb_value.SetContext(Value::eContextTypeClangType, user_type.GetOpaqueQualType()); 2135 parser_vars->m_lldb_value.SetCompilerType(user_type); 2136 parser_vars->m_parser_type = parser_type; 2137 2138 entity->SetCompilerType(user_type); 2139 2140 entity->m_flags &= ~(ClangExpressionVariable::EVUnknownType); 2141 } 2142 } 2143 2144 return true; 2145 } 2146 2147 void 2148 ClangExpressionDeclMap::AddOneRegister (NameSearchContext &context, 2149 const RegisterInfo *reg_info, 2150 unsigned int current_id) 2151 { 2152 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 2153 2154 CompilerType clang_type = ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (m_ast_context, 2155 reg_info->encoding, 2156 reg_info->byte_size * 8); 2157 2158 if (!clang_type) 2159 { 2160 if (log) 2161 log->Printf(" Tried to add a type for %s, but couldn't get one", context.m_decl_name.getAsString().c_str()); 2162 return; 2163 } 2164 2165 TypeFromParser parser_clang_type (clang_type); 2166 2167 NamedDecl *var_decl = context.AddVarDecl(parser_clang_type); 2168 2169 ClangExpressionVariable *entity(new ClangExpressionVariable(m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), 2170 m_parser_vars->m_target_info.byte_order, 2171 m_parser_vars->m_target_info.address_byte_size)); 2172 m_found_entities.AddNewlyConstructedVariable(entity); 2173 2174 std::string decl_name(context.m_decl_name.getAsString()); 2175 entity->SetName (ConstString (decl_name.c_str())); 2176 entity->SetRegisterInfo (reg_info); 2177 entity->EnableParserVars(GetParserID()); 2178 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID()); 2179 parser_vars->m_parser_type = parser_clang_type; 2180 parser_vars->m_named_decl = var_decl; 2181 parser_vars->m_llvm_value = NULL; 2182 parser_vars->m_lldb_value.Clear(); 2183 entity->m_flags |= ClangExpressionVariable::EVBareRegister; 2184 2185 if (log) 2186 { 2187 ASTDumper ast_dumper(var_decl); 2188 log->Printf(" CEDM::FEVD[%d] Added register %s, returned %s", current_id, context.m_decl_name.getAsString().c_str(), ast_dumper.GetCString()); 2189 } 2190 } 2191 2192 void 2193 ClangExpressionDeclMap::AddOneFunction (NameSearchContext &context, 2194 Function* function, 2195 Symbol* symbol, 2196 unsigned int current_id) 2197 { 2198 assert (m_parser_vars.get()); 2199 2200 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 2201 2202 NamedDecl *function_decl = NULL; 2203 Address fun_address; 2204 CompilerType function_clang_type; 2205 2206 bool is_indirect_function = false; 2207 2208 if (function) 2209 { 2210 Type *function_type = function->GetType(); 2211 2212 if (!function_type) 2213 { 2214 if (log) 2215 log->PutCString(" Skipped a function because it has no type"); 2216 return; 2217 } 2218 2219 function_clang_type = function_type->GetFullCompilerType (); 2220 2221 if (!function_clang_type) 2222 { 2223 if (log) 2224 log->PutCString(" Skipped a function because it has no Clang type"); 2225 return; 2226 } 2227 2228 fun_address = function->GetAddressRange().GetBaseAddress(); 2229 2230 CompilerType copied_function_type = GuardedCopyType(function_clang_type); 2231 if (copied_function_type) 2232 { 2233 function_decl = context.AddFunDecl(copied_function_type); 2234 2235 if (!function_decl) 2236 { 2237 if (log) 2238 { 2239 log->Printf (" Failed to create a function decl for '%s' {0x%8.8" PRIx64 "}", 2240 function_type->GetName().GetCString(), 2241 function_type->GetID()); 2242 } 2243 2244 return; 2245 } 2246 } 2247 else 2248 { 2249 // We failed to copy the type we found 2250 if (log) 2251 { 2252 log->Printf (" Failed to import the function type '%s' {0x%8.8" PRIx64 "} into the expression parser AST contenxt", 2253 function_type->GetName().GetCString(), 2254 function_type->GetID()); 2255 } 2256 2257 return; 2258 } 2259 } 2260 else if (symbol) 2261 { 2262 fun_address = symbol->GetAddress(); 2263 function_decl = context.AddGenericFunDecl(); 2264 is_indirect_function = symbol->IsIndirect(); 2265 } 2266 else 2267 { 2268 if (log) 2269 log->PutCString(" AddOneFunction called with no function and no symbol"); 2270 return; 2271 } 2272 2273 Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr(); 2274 2275 lldb::addr_t load_addr = fun_address.GetCallableLoadAddress(target, is_indirect_function); 2276 2277 ClangExpressionVariable *entity(new ClangExpressionVariable (m_parser_vars->m_exe_ctx.GetBestExecutionContextScope (), 2278 m_parser_vars->m_target_info.byte_order, 2279 m_parser_vars->m_target_info.address_byte_size)); 2280 m_found_entities.AddNewlyConstructedVariable(entity); 2281 2282 std::string decl_name(context.m_decl_name.getAsString()); 2283 entity->SetName(ConstString(decl_name.c_str())); 2284 entity->SetCompilerType (function_clang_type); 2285 entity->EnableParserVars(GetParserID()); 2286 2287 ClangExpressionVariable::ParserVars *parser_vars = entity->GetParserVars(GetParserID()); 2288 2289 if (load_addr != LLDB_INVALID_ADDRESS) 2290 { 2291 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeLoadAddress); 2292 parser_vars->m_lldb_value.GetScalar() = load_addr; 2293 } 2294 else 2295 { 2296 // We have to try finding a file address. 2297 2298 lldb::addr_t file_addr = fun_address.GetFileAddress(); 2299 2300 parser_vars->m_lldb_value.SetValueType(Value::eValueTypeFileAddress); 2301 parser_vars->m_lldb_value.GetScalar() = file_addr; 2302 } 2303 2304 2305 parser_vars->m_named_decl = function_decl; 2306 parser_vars->m_llvm_value = NULL; 2307 2308 if (log) 2309 { 2310 ASTDumper ast_dumper(function_decl); 2311 2312 StreamString ss; 2313 2314 fun_address.Dump(&ss, m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), Address::DumpStyleResolvedDescription); 2315 2316 log->Printf(" CEDM::FEVD[%u] Found %s function %s (description %s), returned %s", 2317 current_id, 2318 (function ? "specific" : "generic"), 2319 decl_name.c_str(), 2320 ss.GetData(), 2321 ast_dumper.GetCString()); 2322 } 2323 } 2324 2325 void 2326 ClangExpressionDeclMap::AddThisType(NameSearchContext &context, 2327 TypeFromUser &ut, 2328 unsigned int current_id) 2329 { 2330 CompilerType copied_clang_type = GuardedCopyType(ut); 2331 2332 if (!copied_clang_type) 2333 { 2334 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 2335 2336 if (log) 2337 log->Printf("ClangExpressionDeclMap::AddThisType - Couldn't import the type"); 2338 2339 return; 2340 } 2341 2342 if (copied_clang_type.IsAggregateType() && copied_clang_type.GetCompleteType ()) 2343 { 2344 CompilerType void_clang_type = ClangASTContext::GetBasicType(m_ast_context, eBasicTypeVoid); 2345 CompilerType void_ptr_clang_type = void_clang_type.GetPointerType(); 2346 2347 CompilerType method_type = ClangASTContext::CreateFunctionType (m_ast_context, 2348 void_clang_type, 2349 &void_ptr_clang_type, 2350 1, 2351 false, 2352 copied_clang_type.GetTypeQualifiers()); 2353 2354 const bool is_virtual = false; 2355 const bool is_static = false; 2356 const bool is_inline = false; 2357 const bool is_explicit = false; 2358 const bool is_attr_used = true; 2359 const bool is_artificial = false; 2360 2361 ClangASTContext::GetASTContext(m_ast_context)-> 2362 AddMethodToCXXRecordType (copied_clang_type.GetOpaqueQualType(), 2363 "$__lldb_expr", 2364 method_type, 2365 lldb::eAccessPublic, 2366 is_virtual, 2367 is_static, 2368 is_inline, 2369 is_explicit, 2370 is_attr_used, 2371 is_artificial); 2372 } 2373 2374 if (!copied_clang_type.IsValid()) 2375 return; 2376 2377 TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo(QualType::getFromOpaquePtr(copied_clang_type.GetOpaqueQualType())); 2378 2379 if (!type_source_info) 2380 return; 2381 2382 // Construct a typedef type because if "*this" is a templated type we can't just return ClassTemplateSpecializationDecls in response to name queries. 2383 // Using a typedef makes this much more robust. 2384 2385 TypedefDecl *typedef_decl = TypedefDecl::Create(*m_ast_context, 2386 m_ast_context->getTranslationUnitDecl(), 2387 SourceLocation(), 2388 SourceLocation(), 2389 context.m_decl_name.getAsIdentifierInfo(), 2390 type_source_info); 2391 2392 2393 if (!typedef_decl) 2394 return; 2395 2396 context.AddNamedDecl(typedef_decl); 2397 2398 return; 2399 } 2400 2401 void 2402 ClangExpressionDeclMap::AddOneType(NameSearchContext &context, 2403 TypeFromUser &ut, 2404 unsigned int current_id) 2405 { 2406 CompilerType copied_clang_type = GuardedCopyType(ut); 2407 2408 if (!copied_clang_type) 2409 { 2410 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 2411 2412 if (log) 2413 log->Printf("ClangExpressionDeclMap::AddOneType - Couldn't import the type"); 2414 2415 return; 2416 } 2417 2418 context.AddTypeDecl(copied_clang_type); 2419 } 2420