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