1 //===-- Function.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/Symbol/Function.h" 11 #include "lldb/Core/Disassembler.h" 12 #include "lldb/Core/Module.h" 13 #include "lldb/Core/ModuleList.h" 14 #include "lldb/Core/Section.h" 15 #include "lldb/Host/Host.h" 16 #include "lldb/Symbol/CompileUnit.h" 17 #include "lldb/Symbol/CompilerType.h" 18 #include "lldb/Symbol/LineTable.h" 19 #include "lldb/Symbol/SymbolFile.h" 20 #include "lldb/Symbol/SymbolVendor.h" 21 #include "lldb/Target/Language.h" 22 #include "lldb/Utility/Log.h" 23 #include "llvm/Support/Casting.h" 24 25 using namespace lldb; 26 using namespace lldb_private; 27 28 //---------------------------------------------------------------------- 29 // Basic function information is contained in the FunctionInfo class. It is 30 // designed to contain the name, linkage name, and declaration location. 31 //---------------------------------------------------------------------- 32 FunctionInfo::FunctionInfo(const char *name, const Declaration *decl_ptr) 33 : m_name(name), m_declaration(decl_ptr) {} 34 35 FunctionInfo::FunctionInfo(const ConstString &name, const Declaration *decl_ptr) 36 : m_name(name), m_declaration(decl_ptr) {} 37 38 FunctionInfo::~FunctionInfo() {} 39 40 void FunctionInfo::Dump(Stream *s, bool show_fullpaths) const { 41 if (m_name) 42 *s << ", name = \"" << m_name << "\""; 43 m_declaration.Dump(s, show_fullpaths); 44 } 45 46 int FunctionInfo::Compare(const FunctionInfo &a, const FunctionInfo &b) { 47 int result = ConstString::Compare(a.GetName(), b.GetName()); 48 if (result) 49 return result; 50 51 return Declaration::Compare(a.m_declaration, b.m_declaration); 52 } 53 54 Declaration &FunctionInfo::GetDeclaration() { return m_declaration; } 55 56 const Declaration &FunctionInfo::GetDeclaration() const { 57 return m_declaration; 58 } 59 60 ConstString FunctionInfo::GetName() const { return m_name; } 61 62 size_t FunctionInfo::MemorySize() const { 63 return m_name.MemorySize() + m_declaration.MemorySize(); 64 } 65 66 InlineFunctionInfo::InlineFunctionInfo(const char *name, const char *mangled, 67 const Declaration *decl_ptr, 68 const Declaration *call_decl_ptr) 69 : FunctionInfo(name, decl_ptr), m_mangled(ConstString(mangled), true), 70 m_call_decl(call_decl_ptr) {} 71 72 InlineFunctionInfo::InlineFunctionInfo(const ConstString &name, 73 const Mangled &mangled, 74 const Declaration *decl_ptr, 75 const Declaration *call_decl_ptr) 76 : FunctionInfo(name, decl_ptr), m_mangled(mangled), 77 m_call_decl(call_decl_ptr) {} 78 79 InlineFunctionInfo::~InlineFunctionInfo() {} 80 81 int InlineFunctionInfo::Compare(const InlineFunctionInfo &a, 82 const InlineFunctionInfo &b) { 83 84 int result = FunctionInfo::Compare(a, b); 85 if (result) 86 return result; 87 // only compare the mangled names if both have them 88 return Mangled::Compare(a.m_mangled, a.m_mangled); 89 } 90 91 void InlineFunctionInfo::Dump(Stream *s, bool show_fullpaths) const { 92 FunctionInfo::Dump(s, show_fullpaths); 93 if (m_mangled) 94 m_mangled.Dump(s); 95 } 96 97 void InlineFunctionInfo::DumpStopContext(Stream *s, 98 LanguageType language) const { 99 // s->Indent("[inlined] "); 100 s->Indent(); 101 if (m_mangled) 102 s->PutCString(m_mangled.GetName(language).AsCString()); 103 else 104 s->PutCString(m_name.AsCString()); 105 } 106 107 ConstString InlineFunctionInfo::GetName(LanguageType language) const { 108 if (m_mangled) 109 return m_mangled.GetName(language); 110 return m_name; 111 } 112 113 ConstString InlineFunctionInfo::GetDisplayName(LanguageType language) const { 114 if (m_mangled) 115 return m_mangled.GetDisplayDemangledName(language); 116 return m_name; 117 } 118 119 Declaration &InlineFunctionInfo::GetCallSite() { return m_call_decl; } 120 121 const Declaration &InlineFunctionInfo::GetCallSite() const { 122 return m_call_decl; 123 } 124 125 Mangled &InlineFunctionInfo::GetMangled() { return m_mangled; } 126 127 const Mangled &InlineFunctionInfo::GetMangled() const { return m_mangled; } 128 129 size_t InlineFunctionInfo::MemorySize() const { 130 return FunctionInfo::MemorySize() + m_mangled.MemorySize(); 131 } 132 133 //---------------------------------------------------------------------- 134 // 135 //---------------------------------------------------------------------- 136 CallEdge::CallEdge(const char *symbol_name, lldb::addr_t return_pc) 137 : return_pc(return_pc), resolved(false) { 138 lazy_callee.symbol_name = symbol_name; 139 } 140 141 void CallEdge::ParseSymbolFileAndResolve(ModuleList &images) { 142 if (resolved) 143 return; 144 145 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 146 LLDB_LOG(log, "CallEdge: Lazily parsing the call graph for {0}", 147 lazy_callee.symbol_name); 148 149 auto resolve_lazy_callee = [&]() -> Function * { 150 ConstString callee_name{lazy_callee.symbol_name}; 151 SymbolContextList sc_list; 152 size_t num_matches = 153 images.FindFunctionSymbols(callee_name, eFunctionNameTypeAuto, sc_list); 154 if (num_matches == 0 || !sc_list[0].symbol) { 155 LLDB_LOG(log, "CallEdge: Found no symbols for {0}, cannot resolve it", 156 callee_name); 157 return nullptr; 158 } 159 Address callee_addr = sc_list[0].symbol->GetAddress(); 160 if (!callee_addr.IsValid()) { 161 LLDB_LOG(log, "CallEdge: Invalid symbol address"); 162 return nullptr; 163 } 164 Function *f = callee_addr.CalculateSymbolContextFunction(); 165 if (!f) { 166 LLDB_LOG(log, "CallEdge: Could not find complete function"); 167 return nullptr; 168 } 169 return f; 170 }; 171 lazy_callee.def = resolve_lazy_callee(); 172 resolved = true; 173 } 174 175 Function *CallEdge::GetCallee(ModuleList &images) { 176 ParseSymbolFileAndResolve(images); 177 return lazy_callee.def; 178 } 179 180 lldb::addr_t CallEdge::GetReturnPCAddress(Function &caller, 181 Target &target) const { 182 const Address &base = caller.GetAddressRange().GetBaseAddress(); 183 return base.GetLoadAddress(&target) + return_pc; 184 } 185 186 //---------------------------------------------------------------------- 187 // 188 //---------------------------------------------------------------------- 189 Function::Function(CompileUnit *comp_unit, lldb::user_id_t func_uid, 190 lldb::user_id_t type_uid, const Mangled &mangled, Type *type, 191 const AddressRange &range) 192 : UserID(func_uid), m_comp_unit(comp_unit), m_type_uid(type_uid), 193 m_type(type), m_mangled(mangled), m_block(func_uid), m_range(range), 194 m_frame_base(nullptr), m_flags(), m_prologue_byte_size(0) { 195 m_block.SetParentScope(this); 196 assert(comp_unit != nullptr); 197 } 198 199 Function::~Function() {} 200 201 void Function::GetStartLineSourceInfo(FileSpec &source_file, 202 uint32_t &line_no) { 203 line_no = 0; 204 source_file.Clear(); 205 206 if (m_comp_unit == nullptr) 207 return; 208 209 // Initialize m_type if it hasn't been initialized already 210 GetType(); 211 212 if (m_type != nullptr && m_type->GetDeclaration().GetLine() != 0) { 213 source_file = m_type->GetDeclaration().GetFile(); 214 line_no = m_type->GetDeclaration().GetLine(); 215 } else { 216 LineTable *line_table = m_comp_unit->GetLineTable(); 217 if (line_table == nullptr) 218 return; 219 220 LineEntry line_entry; 221 if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(), 222 line_entry, nullptr)) { 223 line_no = line_entry.line; 224 source_file = line_entry.file; 225 } 226 } 227 } 228 229 void Function::GetEndLineSourceInfo(FileSpec &source_file, uint32_t &line_no) { 230 line_no = 0; 231 source_file.Clear(); 232 233 // The -1 is kind of cheesy, but I want to get the last line entry for the 234 // given function, not the first entry of the next. 235 Address scratch_addr(GetAddressRange().GetBaseAddress()); 236 scratch_addr.SetOffset(scratch_addr.GetOffset() + 237 GetAddressRange().GetByteSize() - 1); 238 239 LineTable *line_table = m_comp_unit->GetLineTable(); 240 if (line_table == nullptr) 241 return; 242 243 LineEntry line_entry; 244 if (line_table->FindLineEntryByAddress(scratch_addr, line_entry, nullptr)) { 245 line_no = line_entry.line; 246 source_file = line_entry.file; 247 } 248 } 249 250 llvm::MutableArrayRef<CallEdge> Function::GetCallEdges() { 251 if (m_call_edges_resolved) 252 return m_call_edges; 253 254 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 255 LLDB_LOG(log, "GetCallEdges: Attempting to parse call site info for {0}", 256 GetDisplayName()); 257 258 m_call_edges_resolved = true; 259 260 // Find the SymbolFile which provided this function's definition. 261 Block &block = GetBlock(/*can_create*/true); 262 SymbolFile *sym_file = block.GetSymbolFile(); 263 if (!sym_file) 264 return llvm::None; 265 266 // Lazily read call site information from the SymbolFile. 267 m_call_edges = sym_file->ParseCallEdgesInFunction(GetID()); 268 269 // Sort the call edges to speed up return_pc lookups. 270 std::sort(m_call_edges.begin(), m_call_edges.end(), 271 [](const CallEdge &LHS, const CallEdge &RHS) { 272 return LHS.GetUnresolvedReturnPCAddress() < 273 RHS.GetUnresolvedReturnPCAddress(); 274 }); 275 276 return m_call_edges; 277 } 278 279 llvm::MutableArrayRef<CallEdge> Function::GetTailCallingEdges() { 280 // Call edges are sorted by return PC, and tail calling edges have invalid 281 // return PCs. Find them at the end of the list. 282 return GetCallEdges().drop_until([](const CallEdge &edge) { 283 return edge.GetUnresolvedReturnPCAddress() == LLDB_INVALID_ADDRESS; 284 }); 285 } 286 287 Block &Function::GetBlock(bool can_create) { 288 if (!m_block.BlockInfoHasBeenParsed() && can_create) { 289 SymbolContext sc; 290 CalculateSymbolContext(&sc); 291 if (sc.module_sp) { 292 sc.module_sp->GetSymbolVendor()->ParseFunctionBlocks(sc); 293 } else { 294 Host::SystemLog(Host::eSystemLogError, "error: unable to find module " 295 "shared pointer for function '%s' " 296 "in %s\n", 297 GetName().GetCString(), m_comp_unit->GetPath().c_str()); 298 } 299 m_block.SetBlockInfoHasBeenParsed(true, true); 300 } 301 return m_block; 302 } 303 304 CompileUnit *Function::GetCompileUnit() { return m_comp_unit; } 305 306 const CompileUnit *Function::GetCompileUnit() const { return m_comp_unit; } 307 308 void Function::GetDescription(Stream *s, lldb::DescriptionLevel level, 309 Target *target) { 310 ConstString name = GetName(); 311 ConstString mangled = m_mangled.GetMangledName(); 312 313 *s << "id = " << (const UserID &)*this; 314 if (name) 315 *s << ", name = \"" << name.GetCString() << '"'; 316 if (mangled) 317 *s << ", mangled = \"" << mangled.GetCString() << '"'; 318 *s << ", range = "; 319 Address::DumpStyle fallback_style; 320 if (level == eDescriptionLevelVerbose) 321 fallback_style = Address::DumpStyleModuleWithFileAddress; 322 else 323 fallback_style = Address::DumpStyleFileAddress; 324 GetAddressRange().Dump(s, target, Address::DumpStyleLoadAddress, 325 fallback_style); 326 } 327 328 void Function::Dump(Stream *s, bool show_context) const { 329 s->Printf("%p: ", static_cast<const void *>(this)); 330 s->Indent(); 331 *s << "Function" << static_cast<const UserID &>(*this); 332 333 m_mangled.Dump(s); 334 335 if (m_type) 336 s->Printf(", type = %p", static_cast<void *>(m_type)); 337 else if (m_type_uid != LLDB_INVALID_UID) 338 s->Printf(", type_uid = 0x%8.8" PRIx64, m_type_uid); 339 340 s->EOL(); 341 // Dump the root object 342 if (m_block.BlockInfoHasBeenParsed()) 343 m_block.Dump(s, m_range.GetBaseAddress().GetFileAddress(), INT_MAX, 344 show_context); 345 } 346 347 void Function::CalculateSymbolContext(SymbolContext *sc) { 348 sc->function = this; 349 m_comp_unit->CalculateSymbolContext(sc); 350 } 351 352 ModuleSP Function::CalculateSymbolContextModule() { 353 SectionSP section_sp(m_range.GetBaseAddress().GetSection()); 354 if (section_sp) 355 return section_sp->GetModule(); 356 357 return this->GetCompileUnit()->GetModule(); 358 } 359 360 CompileUnit *Function::CalculateSymbolContextCompileUnit() { 361 return this->GetCompileUnit(); 362 } 363 364 Function *Function::CalculateSymbolContextFunction() { return this; } 365 366 lldb::DisassemblerSP Function::GetInstructions(const ExecutionContext &exe_ctx, 367 const char *flavor, 368 bool prefer_file_cache) { 369 ModuleSP module_sp(GetAddressRange().GetBaseAddress().GetModule()); 370 if (module_sp) { 371 const bool prefer_file_cache = false; 372 return Disassembler::DisassembleRange(module_sp->GetArchitecture(), nullptr, 373 flavor, exe_ctx, GetAddressRange(), 374 prefer_file_cache); 375 } 376 return lldb::DisassemblerSP(); 377 } 378 379 bool Function::GetDisassembly(const ExecutionContext &exe_ctx, 380 const char *flavor, bool prefer_file_cache, 381 Stream &strm) { 382 lldb::DisassemblerSP disassembler_sp = 383 GetInstructions(exe_ctx, flavor, prefer_file_cache); 384 if (disassembler_sp) { 385 const bool show_address = true; 386 const bool show_bytes = false; 387 disassembler_sp->GetInstructionList().Dump(&strm, show_address, show_bytes, 388 &exe_ctx); 389 return true; 390 } 391 return false; 392 } 393 394 // Symbol * 395 // Function::CalculateSymbolContextSymbol () 396 //{ 397 // return // TODO: find the symbol for the function??? 398 //} 399 400 void Function::DumpSymbolContext(Stream *s) { 401 m_comp_unit->DumpSymbolContext(s); 402 s->Printf(", Function{0x%8.8" PRIx64 "}", GetID()); 403 } 404 405 size_t Function::MemorySize() const { 406 size_t mem_size = sizeof(Function) + m_block.MemorySize(); 407 return mem_size; 408 } 409 410 bool Function::GetIsOptimized() { 411 bool result = false; 412 413 // Currently optimization is only indicted by the vendor extension 414 // DW_AT_APPLE_optimized which is set on a compile unit level. 415 if (m_comp_unit) { 416 result = m_comp_unit->GetIsOptimized(); 417 } 418 return result; 419 } 420 421 bool Function::IsTopLevelFunction() { 422 bool result = false; 423 424 if (Language *language = Language::FindPlugin(GetLanguage())) 425 result = language->IsTopLevelFunction(*this); 426 427 return result; 428 } 429 430 ConstString Function::GetDisplayName() const { 431 return m_mangled.GetDisplayDemangledName(GetLanguage()); 432 } 433 434 CompilerDeclContext Function::GetDeclContext() { 435 ModuleSP module_sp = CalculateSymbolContextModule(); 436 437 if (module_sp) { 438 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor(); 439 440 if (sym_vendor) { 441 SymbolFile *sym_file = sym_vendor->GetSymbolFile(); 442 443 if (sym_file) 444 return sym_file->GetDeclContextForUID(GetID()); 445 } 446 } 447 return CompilerDeclContext(); 448 } 449 450 Type *Function::GetType() { 451 if (m_type == nullptr) { 452 SymbolContext sc; 453 454 CalculateSymbolContext(&sc); 455 456 if (!sc.module_sp) 457 return nullptr; 458 459 SymbolVendor *sym_vendor = sc.module_sp->GetSymbolVendor(); 460 461 if (sym_vendor == nullptr) 462 return nullptr; 463 464 SymbolFile *sym_file = sym_vendor->GetSymbolFile(); 465 466 if (sym_file == nullptr) 467 return nullptr; 468 469 m_type = sym_file->ResolveTypeUID(m_type_uid); 470 } 471 return m_type; 472 } 473 474 const Type *Function::GetType() const { return m_type; } 475 476 CompilerType Function::GetCompilerType() { 477 Type *function_type = GetType(); 478 if (function_type) 479 return function_type->GetFullCompilerType(); 480 return CompilerType(); 481 } 482 483 uint32_t Function::GetPrologueByteSize() { 484 if (m_prologue_byte_size == 0 && 485 m_flags.IsClear(flagsCalculatedPrologueSize)) { 486 m_flags.Set(flagsCalculatedPrologueSize); 487 LineTable *line_table = m_comp_unit->GetLineTable(); 488 uint32_t prologue_end_line_idx = 0; 489 490 if (line_table) { 491 LineEntry first_line_entry; 492 uint32_t first_line_entry_idx = UINT32_MAX; 493 if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(), 494 first_line_entry, 495 &first_line_entry_idx)) { 496 // Make sure the first line entry isn't already the end of the prologue 497 addr_t prologue_end_file_addr = LLDB_INVALID_ADDRESS; 498 addr_t line_zero_end_file_addr = LLDB_INVALID_ADDRESS; 499 500 if (first_line_entry.is_prologue_end) { 501 prologue_end_file_addr = 502 first_line_entry.range.GetBaseAddress().GetFileAddress(); 503 prologue_end_line_idx = first_line_entry_idx; 504 } else { 505 // Check the first few instructions and look for one that has 506 // is_prologue_end set to true. 507 const uint32_t last_line_entry_idx = first_line_entry_idx + 6; 508 for (uint32_t idx = first_line_entry_idx + 1; 509 idx < last_line_entry_idx; ++idx) { 510 LineEntry line_entry; 511 if (line_table->GetLineEntryAtIndex(idx, line_entry)) { 512 if (line_entry.is_prologue_end) { 513 prologue_end_file_addr = 514 line_entry.range.GetBaseAddress().GetFileAddress(); 515 prologue_end_line_idx = idx; 516 break; 517 } 518 } 519 } 520 } 521 522 // If we didn't find the end of the prologue in the line tables, then 523 // just use the end address of the first line table entry 524 if (prologue_end_file_addr == LLDB_INVALID_ADDRESS) { 525 // Check the first few instructions and look for one that has a line 526 // number that's different than the first entry. 527 uint32_t last_line_entry_idx = first_line_entry_idx + 6; 528 for (uint32_t idx = first_line_entry_idx + 1; 529 idx < last_line_entry_idx; ++idx) { 530 LineEntry line_entry; 531 if (line_table->GetLineEntryAtIndex(idx, line_entry)) { 532 if (line_entry.line != first_line_entry.line) { 533 prologue_end_file_addr = 534 line_entry.range.GetBaseAddress().GetFileAddress(); 535 prologue_end_line_idx = idx; 536 break; 537 } 538 } 539 } 540 541 if (prologue_end_file_addr == LLDB_INVALID_ADDRESS) { 542 prologue_end_file_addr = 543 first_line_entry.range.GetBaseAddress().GetFileAddress() + 544 first_line_entry.range.GetByteSize(); 545 prologue_end_line_idx = first_line_entry_idx; 546 } 547 } 548 549 const addr_t func_start_file_addr = 550 m_range.GetBaseAddress().GetFileAddress(); 551 const addr_t func_end_file_addr = 552 func_start_file_addr + m_range.GetByteSize(); 553 554 // Now calculate the offset to pass the subsequent line 0 entries. 555 uint32_t first_non_zero_line = prologue_end_line_idx; 556 while (1) { 557 LineEntry line_entry; 558 if (line_table->GetLineEntryAtIndex(first_non_zero_line, 559 line_entry)) { 560 if (line_entry.line != 0) 561 break; 562 } 563 if (line_entry.range.GetBaseAddress().GetFileAddress() >= 564 func_end_file_addr) 565 break; 566 567 first_non_zero_line++; 568 } 569 570 if (first_non_zero_line > prologue_end_line_idx) { 571 LineEntry first_non_zero_entry; 572 if (line_table->GetLineEntryAtIndex(first_non_zero_line, 573 first_non_zero_entry)) { 574 line_zero_end_file_addr = 575 first_non_zero_entry.range.GetBaseAddress().GetFileAddress(); 576 } 577 } 578 579 // Verify that this prologue end file address in the function's address 580 // range just to be sure 581 if (func_start_file_addr < prologue_end_file_addr && 582 prologue_end_file_addr < func_end_file_addr) { 583 m_prologue_byte_size = prologue_end_file_addr - func_start_file_addr; 584 } 585 586 if (prologue_end_file_addr < line_zero_end_file_addr && 587 line_zero_end_file_addr < func_end_file_addr) { 588 m_prologue_byte_size += 589 line_zero_end_file_addr - prologue_end_file_addr; 590 } 591 } 592 } 593 } 594 595 return m_prologue_byte_size; 596 } 597 598 lldb::LanguageType Function::GetLanguage() const { 599 if (m_comp_unit) 600 return m_comp_unit->GetLanguage(); 601 else 602 return lldb::eLanguageTypeUnknown; 603 } 604 605 ConstString Function::GetName() const { 606 LanguageType language = lldb::eLanguageTypeUnknown; 607 if (m_comp_unit) 608 language = m_comp_unit->GetLanguage(); 609 return m_mangled.GetName(language); 610 } 611 612 ConstString Function::GetNameNoArguments() const { 613 LanguageType language = lldb::eLanguageTypeUnknown; 614 if (m_comp_unit) 615 language = m_comp_unit->GetLanguage(); 616 return m_mangled.GetName(language, Mangled::ePreferDemangledWithoutArguments); 617 } 618