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 Address return_pc_addr{base.GetSection(), return_pc}; 184 return return_pc_addr.GetLoadAddress(&target); 185 } 186 187 //---------------------------------------------------------------------- 188 // 189 //---------------------------------------------------------------------- 190 Function::Function(CompileUnit *comp_unit, lldb::user_id_t func_uid, 191 lldb::user_id_t type_uid, const Mangled &mangled, Type *type, 192 const AddressRange &range) 193 : UserID(func_uid), m_comp_unit(comp_unit), m_type_uid(type_uid), 194 m_type(type), m_mangled(mangled), m_block(func_uid), m_range(range), 195 m_frame_base(nullptr), m_flags(), m_prologue_byte_size(0) { 196 m_block.SetParentScope(this); 197 assert(comp_unit != nullptr); 198 } 199 200 Function::~Function() {} 201 202 void Function::GetStartLineSourceInfo(FileSpec &source_file, 203 uint32_t &line_no) { 204 line_no = 0; 205 source_file.Clear(); 206 207 if (m_comp_unit == nullptr) 208 return; 209 210 // Initialize m_type if it hasn't been initialized already 211 GetType(); 212 213 if (m_type != nullptr && m_type->GetDeclaration().GetLine() != 0) { 214 source_file = m_type->GetDeclaration().GetFile(); 215 line_no = m_type->GetDeclaration().GetLine(); 216 } else { 217 LineTable *line_table = m_comp_unit->GetLineTable(); 218 if (line_table == nullptr) 219 return; 220 221 LineEntry line_entry; 222 if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(), 223 line_entry, nullptr)) { 224 line_no = line_entry.line; 225 source_file = line_entry.file; 226 } 227 } 228 } 229 230 void Function::GetEndLineSourceInfo(FileSpec &source_file, uint32_t &line_no) { 231 line_no = 0; 232 source_file.Clear(); 233 234 // The -1 is kind of cheesy, but I want to get the last line entry for the 235 // given function, not the first entry of the next. 236 Address scratch_addr(GetAddressRange().GetBaseAddress()); 237 scratch_addr.SetOffset(scratch_addr.GetOffset() + 238 GetAddressRange().GetByteSize() - 1); 239 240 LineTable *line_table = m_comp_unit->GetLineTable(); 241 if (line_table == nullptr) 242 return; 243 244 LineEntry line_entry; 245 if (line_table->FindLineEntryByAddress(scratch_addr, line_entry, nullptr)) { 246 line_no = line_entry.line; 247 source_file = line_entry.file; 248 } 249 } 250 251 llvm::MutableArrayRef<CallEdge> Function::GetCallEdges() { 252 if (m_call_edges_resolved) 253 return m_call_edges; 254 255 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 256 LLDB_LOG(log, "GetCallEdges: Attempting to parse call site info for {0}", 257 GetDisplayName()); 258 259 m_call_edges_resolved = true; 260 261 // Find the SymbolFile which provided this function's definition. 262 Block &block = GetBlock(/*can_create*/true); 263 SymbolFile *sym_file = block.GetSymbolFile(); 264 if (!sym_file) 265 return llvm::None; 266 267 // Lazily read call site information from the SymbolFile. 268 m_call_edges = sym_file->ParseCallEdgesInFunction(GetID()); 269 270 // Sort the call edges to speed up return_pc lookups. 271 std::sort(m_call_edges.begin(), m_call_edges.end(), 272 [](const CallEdge &LHS, const CallEdge &RHS) { 273 return LHS.GetUnresolvedReturnPCAddress() < 274 RHS.GetUnresolvedReturnPCAddress(); 275 }); 276 277 return m_call_edges; 278 } 279 280 llvm::MutableArrayRef<CallEdge> Function::GetTailCallingEdges() { 281 // Call edges are sorted by return PC, and tail calling edges have invalid 282 // return PCs. Find them at the end of the list. 283 return GetCallEdges().drop_until([](const CallEdge &edge) { 284 return edge.GetUnresolvedReturnPCAddress() == LLDB_INVALID_ADDRESS; 285 }); 286 } 287 288 Block &Function::GetBlock(bool can_create) { 289 if (!m_block.BlockInfoHasBeenParsed() && can_create) { 290 SymbolContext sc; 291 CalculateSymbolContext(&sc); 292 if (sc.module_sp) { 293 sc.module_sp->GetSymbolVendor()->ParseFunctionBlocks(sc); 294 } else { 295 Host::SystemLog(Host::eSystemLogError, "error: unable to find module " 296 "shared pointer for function '%s' " 297 "in %s\n", 298 GetName().GetCString(), m_comp_unit->GetPath().c_str()); 299 } 300 m_block.SetBlockInfoHasBeenParsed(true, true); 301 } 302 return m_block; 303 } 304 305 CompileUnit *Function::GetCompileUnit() { return m_comp_unit; } 306 307 const CompileUnit *Function::GetCompileUnit() const { return m_comp_unit; } 308 309 void Function::GetDescription(Stream *s, lldb::DescriptionLevel level, 310 Target *target) { 311 ConstString name = GetName(); 312 ConstString mangled = m_mangled.GetMangledName(); 313 314 *s << "id = " << (const UserID &)*this; 315 if (name) 316 *s << ", name = \"" << name.GetCString() << '"'; 317 if (mangled) 318 *s << ", mangled = \"" << mangled.GetCString() << '"'; 319 *s << ", range = "; 320 Address::DumpStyle fallback_style; 321 if (level == eDescriptionLevelVerbose) 322 fallback_style = Address::DumpStyleModuleWithFileAddress; 323 else 324 fallback_style = Address::DumpStyleFileAddress; 325 GetAddressRange().Dump(s, target, Address::DumpStyleLoadAddress, 326 fallback_style); 327 } 328 329 void Function::Dump(Stream *s, bool show_context) const { 330 s->Printf("%p: ", static_cast<const void *>(this)); 331 s->Indent(); 332 *s << "Function" << static_cast<const UserID &>(*this); 333 334 m_mangled.Dump(s); 335 336 if (m_type) 337 s->Printf(", type = %p", static_cast<void *>(m_type)); 338 else if (m_type_uid != LLDB_INVALID_UID) 339 s->Printf(", type_uid = 0x%8.8" PRIx64, m_type_uid); 340 341 s->EOL(); 342 // Dump the root object 343 if (m_block.BlockInfoHasBeenParsed()) 344 m_block.Dump(s, m_range.GetBaseAddress().GetFileAddress(), INT_MAX, 345 show_context); 346 } 347 348 void Function::CalculateSymbolContext(SymbolContext *sc) { 349 sc->function = this; 350 m_comp_unit->CalculateSymbolContext(sc); 351 } 352 353 ModuleSP Function::CalculateSymbolContextModule() { 354 SectionSP section_sp(m_range.GetBaseAddress().GetSection()); 355 if (section_sp) 356 return section_sp->GetModule(); 357 358 return this->GetCompileUnit()->GetModule(); 359 } 360 361 CompileUnit *Function::CalculateSymbolContextCompileUnit() { 362 return this->GetCompileUnit(); 363 } 364 365 Function *Function::CalculateSymbolContextFunction() { return this; } 366 367 lldb::DisassemblerSP Function::GetInstructions(const ExecutionContext &exe_ctx, 368 const char *flavor, 369 bool prefer_file_cache) { 370 ModuleSP module_sp(GetAddressRange().GetBaseAddress().GetModule()); 371 if (module_sp) { 372 const bool prefer_file_cache = false; 373 return Disassembler::DisassembleRange(module_sp->GetArchitecture(), nullptr, 374 flavor, exe_ctx, GetAddressRange(), 375 prefer_file_cache); 376 } 377 return lldb::DisassemblerSP(); 378 } 379 380 bool Function::GetDisassembly(const ExecutionContext &exe_ctx, 381 const char *flavor, bool prefer_file_cache, 382 Stream &strm) { 383 lldb::DisassemblerSP disassembler_sp = 384 GetInstructions(exe_ctx, flavor, prefer_file_cache); 385 if (disassembler_sp) { 386 const bool show_address = true; 387 const bool show_bytes = false; 388 disassembler_sp->GetInstructionList().Dump(&strm, show_address, show_bytes, 389 &exe_ctx); 390 return true; 391 } 392 return false; 393 } 394 395 // Symbol * 396 // Function::CalculateSymbolContextSymbol () 397 //{ 398 // return // TODO: find the symbol for the function??? 399 //} 400 401 void Function::DumpSymbolContext(Stream *s) { 402 m_comp_unit->DumpSymbolContext(s); 403 s->Printf(", Function{0x%8.8" PRIx64 "}", GetID()); 404 } 405 406 size_t Function::MemorySize() const { 407 size_t mem_size = sizeof(Function) + m_block.MemorySize(); 408 return mem_size; 409 } 410 411 bool Function::GetIsOptimized() { 412 bool result = false; 413 414 // Currently optimization is only indicted by the vendor extension 415 // DW_AT_APPLE_optimized which is set on a compile unit level. 416 if (m_comp_unit) { 417 result = m_comp_unit->GetIsOptimized(); 418 } 419 return result; 420 } 421 422 bool Function::IsTopLevelFunction() { 423 bool result = false; 424 425 if (Language *language = Language::FindPlugin(GetLanguage())) 426 result = language->IsTopLevelFunction(*this); 427 428 return result; 429 } 430 431 ConstString Function::GetDisplayName() const { 432 return m_mangled.GetDisplayDemangledName(GetLanguage()); 433 } 434 435 CompilerDeclContext Function::GetDeclContext() { 436 ModuleSP module_sp = CalculateSymbolContextModule(); 437 438 if (module_sp) { 439 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor(); 440 441 if (sym_vendor) { 442 SymbolFile *sym_file = sym_vendor->GetSymbolFile(); 443 444 if (sym_file) 445 return sym_file->GetDeclContextForUID(GetID()); 446 } 447 } 448 return CompilerDeclContext(); 449 } 450 451 Type *Function::GetType() { 452 if (m_type == nullptr) { 453 SymbolContext sc; 454 455 CalculateSymbolContext(&sc); 456 457 if (!sc.module_sp) 458 return nullptr; 459 460 SymbolVendor *sym_vendor = sc.module_sp->GetSymbolVendor(); 461 462 if (sym_vendor == nullptr) 463 return nullptr; 464 465 SymbolFile *sym_file = sym_vendor->GetSymbolFile(); 466 467 if (sym_file == nullptr) 468 return nullptr; 469 470 m_type = sym_file->ResolveTypeUID(m_type_uid); 471 } 472 return m_type; 473 } 474 475 const Type *Function::GetType() const { return m_type; } 476 477 CompilerType Function::GetCompilerType() { 478 Type *function_type = GetType(); 479 if (function_type) 480 return function_type->GetFullCompilerType(); 481 return CompilerType(); 482 } 483 484 uint32_t Function::GetPrologueByteSize() { 485 if (m_prologue_byte_size == 0 && 486 m_flags.IsClear(flagsCalculatedPrologueSize)) { 487 m_flags.Set(flagsCalculatedPrologueSize); 488 LineTable *line_table = m_comp_unit->GetLineTable(); 489 uint32_t prologue_end_line_idx = 0; 490 491 if (line_table) { 492 LineEntry first_line_entry; 493 uint32_t first_line_entry_idx = UINT32_MAX; 494 if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(), 495 first_line_entry, 496 &first_line_entry_idx)) { 497 // Make sure the first line entry isn't already the end of the prologue 498 addr_t prologue_end_file_addr = LLDB_INVALID_ADDRESS; 499 addr_t line_zero_end_file_addr = LLDB_INVALID_ADDRESS; 500 501 if (first_line_entry.is_prologue_end) { 502 prologue_end_file_addr = 503 first_line_entry.range.GetBaseAddress().GetFileAddress(); 504 prologue_end_line_idx = first_line_entry_idx; 505 } else { 506 // Check the first few instructions and look for one that has 507 // is_prologue_end set to true. 508 const uint32_t last_line_entry_idx = first_line_entry_idx + 6; 509 for (uint32_t idx = first_line_entry_idx + 1; 510 idx < last_line_entry_idx; ++idx) { 511 LineEntry line_entry; 512 if (line_table->GetLineEntryAtIndex(idx, line_entry)) { 513 if (line_entry.is_prologue_end) { 514 prologue_end_file_addr = 515 line_entry.range.GetBaseAddress().GetFileAddress(); 516 prologue_end_line_idx = idx; 517 break; 518 } 519 } 520 } 521 } 522 523 // If we didn't find the end of the prologue in the line tables, then 524 // just use the end address of the first line table entry 525 if (prologue_end_file_addr == LLDB_INVALID_ADDRESS) { 526 // Check the first few instructions and look for one that has a line 527 // number that's different than the first entry. 528 uint32_t last_line_entry_idx = first_line_entry_idx + 6; 529 for (uint32_t idx = first_line_entry_idx + 1; 530 idx < last_line_entry_idx; ++idx) { 531 LineEntry line_entry; 532 if (line_table->GetLineEntryAtIndex(idx, line_entry)) { 533 if (line_entry.line != first_line_entry.line) { 534 prologue_end_file_addr = 535 line_entry.range.GetBaseAddress().GetFileAddress(); 536 prologue_end_line_idx = idx; 537 break; 538 } 539 } 540 } 541 542 if (prologue_end_file_addr == LLDB_INVALID_ADDRESS) { 543 prologue_end_file_addr = 544 first_line_entry.range.GetBaseAddress().GetFileAddress() + 545 first_line_entry.range.GetByteSize(); 546 prologue_end_line_idx = first_line_entry_idx; 547 } 548 } 549 550 const addr_t func_start_file_addr = 551 m_range.GetBaseAddress().GetFileAddress(); 552 const addr_t func_end_file_addr = 553 func_start_file_addr + m_range.GetByteSize(); 554 555 // Now calculate the offset to pass the subsequent line 0 entries. 556 uint32_t first_non_zero_line = prologue_end_line_idx; 557 while (1) { 558 LineEntry line_entry; 559 if (line_table->GetLineEntryAtIndex(first_non_zero_line, 560 line_entry)) { 561 if (line_entry.line != 0) 562 break; 563 } 564 if (line_entry.range.GetBaseAddress().GetFileAddress() >= 565 func_end_file_addr) 566 break; 567 568 first_non_zero_line++; 569 } 570 571 if (first_non_zero_line > prologue_end_line_idx) { 572 LineEntry first_non_zero_entry; 573 if (line_table->GetLineEntryAtIndex(first_non_zero_line, 574 first_non_zero_entry)) { 575 line_zero_end_file_addr = 576 first_non_zero_entry.range.GetBaseAddress().GetFileAddress(); 577 } 578 } 579 580 // Verify that this prologue end file address in the function's address 581 // range just to be sure 582 if (func_start_file_addr < prologue_end_file_addr && 583 prologue_end_file_addr < func_end_file_addr) { 584 m_prologue_byte_size = prologue_end_file_addr - func_start_file_addr; 585 } 586 587 if (prologue_end_file_addr < line_zero_end_file_addr && 588 line_zero_end_file_addr < func_end_file_addr) { 589 m_prologue_byte_size += 590 line_zero_end_file_addr - prologue_end_file_addr; 591 } 592 } 593 } 594 } 595 596 return m_prologue_byte_size; 597 } 598 599 lldb::LanguageType Function::GetLanguage() const { 600 if (m_comp_unit) 601 return m_comp_unit->GetLanguage(); 602 else 603 return lldb::eLanguageTypeUnknown; 604 } 605 606 ConstString Function::GetName() const { 607 LanguageType language = lldb::eLanguageTypeUnknown; 608 if (m_comp_unit) 609 language = m_comp_unit->GetLanguage(); 610 return m_mangled.GetName(language); 611 } 612 613 ConstString Function::GetNameNoArguments() const { 614 LanguageType language = lldb::eLanguageTypeUnknown; 615 if (m_comp_unit) 616 language = m_comp_unit->GetLanguage(); 617 return m_mangled.GetName(language, Mangled::ePreferDemangledWithoutArguments); 618 } 619