1 //===-- Function.cpp --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
24 using namespace lldb_private;
26 //----------------------------------------------------------------------
27 // Basic function information is contained in the FunctionInfo class. It is
28 // designed to contain the name, linkage name, and declaration location.
29 //----------------------------------------------------------------------
30 FunctionInfo::FunctionInfo(const char *name, const Declaration *decl_ptr)
31 : m_name(name), m_declaration(decl_ptr) {}
33 FunctionInfo::FunctionInfo(const ConstString &name, const Declaration *decl_ptr)
34 : m_name(name), m_declaration(decl_ptr) {}
36 FunctionInfo::~FunctionInfo() {}
38 void FunctionInfo::Dump(Stream *s, bool show_fullpaths) const {
40 *s << ", name = \"" << m_name << "\"";
41 m_declaration.Dump(s, show_fullpaths);
44 int FunctionInfo::Compare(const FunctionInfo &a, const FunctionInfo &b) {
45 int result = ConstString::Compare(a.GetName(), b.GetName());
49 return Declaration::Compare(a.m_declaration, b.m_declaration);
52 Declaration &FunctionInfo::GetDeclaration() { return m_declaration; }
54 const Declaration &FunctionInfo::GetDeclaration() const {
58 ConstString FunctionInfo::GetName() const { return m_name; }
60 size_t FunctionInfo::MemorySize() const {
61 return m_name.MemorySize() + m_declaration.MemorySize();
64 InlineFunctionInfo::InlineFunctionInfo(const char *name, const char *mangled,
65 const Declaration *decl_ptr,
66 const Declaration *call_decl_ptr)
67 : FunctionInfo(name, decl_ptr), m_mangled(ConstString(mangled), true),
68 m_call_decl(call_decl_ptr) {}
70 InlineFunctionInfo::InlineFunctionInfo(const ConstString &name,
71 const Mangled &mangled,
72 const Declaration *decl_ptr,
73 const Declaration *call_decl_ptr)
74 : FunctionInfo(name, decl_ptr), m_mangled(mangled),
75 m_call_decl(call_decl_ptr) {}
77 InlineFunctionInfo::~InlineFunctionInfo() {}
79 int InlineFunctionInfo::Compare(const InlineFunctionInfo &a,
80 const InlineFunctionInfo &b) {
82 int result = FunctionInfo::Compare(a, b);
85 // only compare the mangled names if both have them
86 return Mangled::Compare(a.m_mangled, a.m_mangled);
89 void InlineFunctionInfo::Dump(Stream *s, bool show_fullpaths) const {
90 FunctionInfo::Dump(s, show_fullpaths);
95 void InlineFunctionInfo::DumpStopContext(Stream *s,
96 LanguageType language) const {
97 // s->Indent("[inlined] ");
100 s->PutCString(m_mangled.GetName(language).AsCString());
102 s->PutCString(m_name.AsCString());
105 ConstString InlineFunctionInfo::GetName(LanguageType language) const {
107 return m_mangled.GetName(language);
111 ConstString InlineFunctionInfo::GetDisplayName(LanguageType language) const {
113 return m_mangled.GetDisplayDemangledName(language);
117 Declaration &InlineFunctionInfo::GetCallSite() { return m_call_decl; }
119 const Declaration &InlineFunctionInfo::GetCallSite() const {
123 Mangled &InlineFunctionInfo::GetMangled() { return m_mangled; }
125 const Mangled &InlineFunctionInfo::GetMangled() const { return m_mangled; }
127 size_t InlineFunctionInfo::MemorySize() const {
128 return FunctionInfo::MemorySize() + m_mangled.MemorySize();
131 //----------------------------------------------------------------------
133 //----------------------------------------------------------------------
134 Function::Function(CompileUnit *comp_unit, lldb::user_id_t func_uid,
135 lldb::user_id_t type_uid, const Mangled &mangled, Type *type,
136 const AddressRange &range)
137 : UserID(func_uid), m_comp_unit(comp_unit), m_type_uid(type_uid),
138 m_type(type), m_mangled(mangled), m_block(func_uid), m_range(range),
139 m_frame_base(nullptr), m_flags(), m_prologue_byte_size(0) {
140 m_block.SetParentScope(this);
141 assert(comp_unit != nullptr);
144 Function::Function(CompileUnit *comp_unit, lldb::user_id_t func_uid,
145 lldb::user_id_t type_uid, const char *mangled, Type *type,
146 const AddressRange &range)
147 : UserID(func_uid), m_comp_unit(comp_unit), m_type_uid(type_uid),
148 m_type(type), m_mangled(ConstString(mangled), true), m_block(func_uid),
149 m_range(range), m_frame_base(nullptr), m_flags(),
150 m_prologue_byte_size(0) {
151 m_block.SetParentScope(this);
152 assert(comp_unit != nullptr);
155 Function::~Function() {}
157 void Function::GetStartLineSourceInfo(FileSpec &source_file,
162 if (m_comp_unit == nullptr)
165 // Initialize m_type if it hasn't been initialized already
168 if (m_type != nullptr && m_type->GetDeclaration().GetLine() != 0) {
169 source_file = m_type->GetDeclaration().GetFile();
170 line_no = m_type->GetDeclaration().GetLine();
172 LineTable *line_table = m_comp_unit->GetLineTable();
173 if (line_table == nullptr)
176 LineEntry line_entry;
177 if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(),
178 line_entry, nullptr)) {
179 line_no = line_entry.line;
180 source_file = line_entry.file;
185 void Function::GetEndLineSourceInfo(FileSpec &source_file, uint32_t &line_no) {
189 // The -1 is kind of cheesy, but I want to get the last line entry for the
190 // given function, not the first entry of the next.
191 Address scratch_addr(GetAddressRange().GetBaseAddress());
192 scratch_addr.SetOffset(scratch_addr.GetOffset() +
193 GetAddressRange().GetByteSize() - 1);
195 LineTable *line_table = m_comp_unit->GetLineTable();
196 if (line_table == nullptr)
199 LineEntry line_entry;
200 if (line_table->FindLineEntryByAddress(scratch_addr, line_entry, nullptr)) {
201 line_no = line_entry.line;
202 source_file = line_entry.file;
206 Block &Function::GetBlock(bool can_create) {
207 if (!m_block.BlockInfoHasBeenParsed() && can_create) {
209 CalculateSymbolContext(&sc);
211 sc.module_sp->GetSymbolVendor()->ParseFunctionBlocks(sc);
213 Host::SystemLog(Host::eSystemLogError, "error: unable to find module "
214 "shared pointer for function '%s' "
216 GetName().GetCString(), m_comp_unit->GetPath().c_str());
218 m_block.SetBlockInfoHasBeenParsed(true, true);
223 CompileUnit *Function::GetCompileUnit() { return m_comp_unit; }
225 const CompileUnit *Function::GetCompileUnit() const { return m_comp_unit; }
227 void Function::GetDescription(Stream *s, lldb::DescriptionLevel level,
229 ConstString name = GetName();
230 ConstString mangled = m_mangled.GetMangledName();
232 *s << "id = " << (const UserID &)*this;
234 *s << ", name = \"" << name.GetCString() << '"';
236 *s << ", mangled = \"" << mangled.GetCString() << '"';
238 Address::DumpStyle fallback_style;
239 if (level == eDescriptionLevelVerbose)
240 fallback_style = Address::DumpStyleModuleWithFileAddress;
242 fallback_style = Address::DumpStyleFileAddress;
243 GetAddressRange().Dump(s, target, Address::DumpStyleLoadAddress,
247 void Function::Dump(Stream *s, bool show_context) const {
248 s->Printf("%p: ", static_cast<const void *>(this));
250 *s << "Function" << static_cast<const UserID &>(*this);
255 s->Printf(", type = %p", static_cast<void *>(m_type));
256 else if (m_type_uid != LLDB_INVALID_UID)
257 s->Printf(", type_uid = 0x%8.8" PRIx64, m_type_uid);
260 // Dump the root object
261 if (m_block.BlockInfoHasBeenParsed())
262 m_block.Dump(s, m_range.GetBaseAddress().GetFileAddress(), INT_MAX,
266 void Function::CalculateSymbolContext(SymbolContext *sc) {
268 m_comp_unit->CalculateSymbolContext(sc);
271 ModuleSP Function::CalculateSymbolContextModule() {
272 SectionSP section_sp(m_range.GetBaseAddress().GetSection());
274 return section_sp->GetModule();
276 return this->GetCompileUnit()->GetModule();
279 CompileUnit *Function::CalculateSymbolContextCompileUnit() {
280 return this->GetCompileUnit();
283 Function *Function::CalculateSymbolContextFunction() { return this; }
285 lldb::DisassemblerSP Function::GetInstructions(const ExecutionContext &exe_ctx,
287 bool prefer_file_cache) {
288 ModuleSP module_sp(GetAddressRange().GetBaseAddress().GetModule());
290 const bool prefer_file_cache = false;
291 return Disassembler::DisassembleRange(module_sp->GetArchitecture(), nullptr,
292 flavor, exe_ctx, GetAddressRange(),
295 return lldb::DisassemblerSP();
298 bool Function::GetDisassembly(const ExecutionContext &exe_ctx,
299 const char *flavor, bool prefer_file_cache,
301 lldb::DisassemblerSP disassembler_sp =
302 GetInstructions(exe_ctx, flavor, prefer_file_cache);
303 if (disassembler_sp) {
304 const bool show_address = true;
305 const bool show_bytes = false;
306 disassembler_sp->GetInstructionList().Dump(&strm, show_address, show_bytes,
314 // Function::CalculateSymbolContextSymbol ()
316 // return // TODO: find the symbol for the function???
319 void Function::DumpSymbolContext(Stream *s) {
320 m_comp_unit->DumpSymbolContext(s);
321 s->Printf(", Function{0x%8.8" PRIx64 "}", GetID());
324 size_t Function::MemorySize() const {
325 size_t mem_size = sizeof(Function) + m_block.MemorySize();
329 bool Function::GetIsOptimized() {
332 // Currently optimization is only indicted by the vendor extension
333 // DW_AT_APPLE_optimized which is set on a compile unit level.
335 result = m_comp_unit->GetIsOptimized();
340 bool Function::IsTopLevelFunction() {
343 if (Language *language = Language::FindPlugin(GetLanguage()))
344 result = language->IsTopLevelFunction(*this);
349 ConstString Function::GetDisplayName() const {
350 return m_mangled.GetDisplayDemangledName(GetLanguage());
353 CompilerDeclContext Function::GetDeclContext() {
354 ModuleSP module_sp = CalculateSymbolContextModule();
357 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
360 SymbolFile *sym_file = sym_vendor->GetSymbolFile();
363 return sym_file->GetDeclContextForUID(GetID());
366 return CompilerDeclContext();
369 Type *Function::GetType() {
370 if (m_type == nullptr) {
373 CalculateSymbolContext(&sc);
378 SymbolVendor *sym_vendor = sc.module_sp->GetSymbolVendor();
380 if (sym_vendor == nullptr)
383 SymbolFile *sym_file = sym_vendor->GetSymbolFile();
385 if (sym_file == nullptr)
388 m_type = sym_file->ResolveTypeUID(m_type_uid);
393 const Type *Function::GetType() const { return m_type; }
395 CompilerType Function::GetCompilerType() {
396 Type *function_type = GetType();
398 return function_type->GetFullCompilerType();
399 return CompilerType();
402 uint32_t Function::GetPrologueByteSize() {
403 if (m_prologue_byte_size == 0 &&
404 m_flags.IsClear(flagsCalculatedPrologueSize)) {
405 m_flags.Set(flagsCalculatedPrologueSize);
406 LineTable *line_table = m_comp_unit->GetLineTable();
407 uint32_t prologue_end_line_idx = 0;
410 LineEntry first_line_entry;
411 uint32_t first_line_entry_idx = UINT32_MAX;
412 if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(),
414 &first_line_entry_idx)) {
415 // Make sure the first line entry isn't already the end of the prologue
416 addr_t prologue_end_file_addr = LLDB_INVALID_ADDRESS;
417 addr_t line_zero_end_file_addr = LLDB_INVALID_ADDRESS;
419 if (first_line_entry.is_prologue_end) {
420 prologue_end_file_addr =
421 first_line_entry.range.GetBaseAddress().GetFileAddress();
422 prologue_end_line_idx = first_line_entry_idx;
424 // Check the first few instructions and look for one that has
425 // is_prologue_end set to true.
426 const uint32_t last_line_entry_idx = first_line_entry_idx + 6;
427 for (uint32_t idx = first_line_entry_idx + 1;
428 idx < last_line_entry_idx; ++idx) {
429 LineEntry line_entry;
430 if (line_table->GetLineEntryAtIndex(idx, line_entry)) {
431 if (line_entry.is_prologue_end) {
432 prologue_end_file_addr =
433 line_entry.range.GetBaseAddress().GetFileAddress();
434 prologue_end_line_idx = idx;
441 // If we didn't find the end of the prologue in the line tables, then
442 // just use the end address of the first line table entry
443 if (prologue_end_file_addr == LLDB_INVALID_ADDRESS) {
444 // Check the first few instructions and look for one that has a line
445 // number that's different than the first entry.
446 uint32_t last_line_entry_idx = first_line_entry_idx + 6;
447 for (uint32_t idx = first_line_entry_idx + 1;
448 idx < last_line_entry_idx; ++idx) {
449 LineEntry line_entry;
450 if (line_table->GetLineEntryAtIndex(idx, line_entry)) {
451 if (line_entry.line != first_line_entry.line) {
452 prologue_end_file_addr =
453 line_entry.range.GetBaseAddress().GetFileAddress();
454 prologue_end_line_idx = idx;
460 if (prologue_end_file_addr == LLDB_INVALID_ADDRESS) {
461 prologue_end_file_addr =
462 first_line_entry.range.GetBaseAddress().GetFileAddress() +
463 first_line_entry.range.GetByteSize();
464 prologue_end_line_idx = first_line_entry_idx;
468 const addr_t func_start_file_addr =
469 m_range.GetBaseAddress().GetFileAddress();
470 const addr_t func_end_file_addr =
471 func_start_file_addr + m_range.GetByteSize();
473 // Now calculate the offset to pass the subsequent line 0 entries.
474 uint32_t first_non_zero_line = prologue_end_line_idx;
476 LineEntry line_entry;
477 if (line_table->GetLineEntryAtIndex(first_non_zero_line,
479 if (line_entry.line != 0)
482 if (line_entry.range.GetBaseAddress().GetFileAddress() >=
486 first_non_zero_line++;
489 if (first_non_zero_line > prologue_end_line_idx) {
490 LineEntry first_non_zero_entry;
491 if (line_table->GetLineEntryAtIndex(first_non_zero_line,
492 first_non_zero_entry)) {
493 line_zero_end_file_addr =
494 first_non_zero_entry.range.GetBaseAddress().GetFileAddress();
498 // Verify that this prologue end file address in the function's address
499 // range just to be sure
500 if (func_start_file_addr < prologue_end_file_addr &&
501 prologue_end_file_addr < func_end_file_addr) {
502 m_prologue_byte_size = prologue_end_file_addr - func_start_file_addr;
505 if (prologue_end_file_addr < line_zero_end_file_addr &&
506 line_zero_end_file_addr < func_end_file_addr) {
507 m_prologue_byte_size +=
508 line_zero_end_file_addr - prologue_end_file_addr;
514 return m_prologue_byte_size;
517 lldb::LanguageType Function::GetLanguage() const {
519 return m_comp_unit->GetLanguage();
521 return lldb::eLanguageTypeUnknown;
524 ConstString Function::GetName() const {
525 LanguageType language = lldb::eLanguageTypeUnknown;
527 language = m_comp_unit->GetLanguage();
528 return m_mangled.GetName(language);
531 ConstString Function::GetNameNoArguments() const {
532 LanguageType language = lldb::eLanguageTypeUnknown;
534 language = m_comp_unit->GetLanguage();
535 return m_mangled.GetName(language, Mangled::ePreferDemangledWithoutArguments);