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