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