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.
28 // It is designed to contain the name, linkage name, and declaration
30 //----------------------------------------------------------------------
31 FunctionInfo::FunctionInfo(const char *name, const Declaration *decl_ptr)
32 : m_name(name), m_declaration(decl_ptr) {}
34 FunctionInfo::FunctionInfo(const ConstString &name, const Declaration *decl_ptr)
35 : m_name(name), m_declaration(decl_ptr) {}
37 FunctionInfo::~FunctionInfo() {}
39 void FunctionInfo::Dump(Stream *s, bool show_fullpaths) const {
41 *s << ", name = \"" << m_name << "\"";
42 m_declaration.Dump(s, show_fullpaths);
45 int FunctionInfo::Compare(const FunctionInfo &a, const FunctionInfo &b) {
46 int result = ConstString::Compare(a.GetName(), b.GetName());
50 return Declaration::Compare(a.m_declaration, b.m_declaration);
53 Declaration &FunctionInfo::GetDeclaration() { return m_declaration; }
55 const Declaration &FunctionInfo::GetDeclaration() const {
59 ConstString FunctionInfo::GetName() const { return m_name; }
61 size_t FunctionInfo::MemorySize() const {
62 return m_name.MemorySize() + m_declaration.MemorySize();
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) {}
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) {}
78 InlineFunctionInfo::~InlineFunctionInfo() {}
80 int InlineFunctionInfo::Compare(const InlineFunctionInfo &a,
81 const InlineFunctionInfo &b) {
83 int result = FunctionInfo::Compare(a, b);
86 // only compare the mangled names if both have them
87 return Mangled::Compare(a.m_mangled, a.m_mangled);
90 void InlineFunctionInfo::Dump(Stream *s, bool show_fullpaths) const {
91 FunctionInfo::Dump(s, show_fullpaths);
96 void InlineFunctionInfo::DumpStopContext(Stream *s,
97 LanguageType language) const {
98 // s->Indent("[inlined] ");
101 s->PutCString(m_mangled.GetName(language).AsCString());
103 s->PutCString(m_name.AsCString());
106 ConstString InlineFunctionInfo::GetName(LanguageType language) const {
108 return m_mangled.GetName(language);
112 ConstString InlineFunctionInfo::GetDisplayName(LanguageType language) const {
114 return m_mangled.GetDisplayDemangledName(language);
118 Declaration &InlineFunctionInfo::GetCallSite() { return m_call_decl; }
120 const Declaration &InlineFunctionInfo::GetCallSite() const {
124 Mangled &InlineFunctionInfo::GetMangled() { return m_mangled; }
126 const Mangled &InlineFunctionInfo::GetMangled() const { return m_mangled; }
128 size_t InlineFunctionInfo::MemorySize() const {
129 return FunctionInfo::MemorySize() + m_mangled.MemorySize();
132 //----------------------------------------------------------------------
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);
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);
156 Function::~Function() {}
158 void Function::GetStartLineSourceInfo(FileSpec &source_file,
163 if (m_comp_unit == nullptr)
166 // Initialize m_type if it hasn't been initialized already
169 if (m_type != nullptr && m_type->GetDeclaration().GetLine() != 0) {
170 source_file = m_type->GetDeclaration().GetFile();
171 line_no = m_type->GetDeclaration().GetLine();
173 LineTable *line_table = m_comp_unit->GetLineTable();
174 if (line_table == nullptr)
177 LineEntry line_entry;
178 if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(),
179 line_entry, nullptr)) {
180 line_no = line_entry.line;
181 source_file = line_entry.file;
186 void Function::GetEndLineSourceInfo(FileSpec &source_file, uint32_t &line_no) {
190 // The -1 is kind of cheesy, but I want to get the last line entry for the
191 // given function, not the
192 // first entry of the next.
193 Address scratch_addr(GetAddressRange().GetBaseAddress());
194 scratch_addr.SetOffset(scratch_addr.GetOffset() +
195 GetAddressRange().GetByteSize() - 1);
197 LineTable *line_table = m_comp_unit->GetLineTable();
198 if (line_table == nullptr)
201 LineEntry line_entry;
202 if (line_table->FindLineEntryByAddress(scratch_addr, line_entry, nullptr)) {
203 line_no = line_entry.line;
204 source_file = line_entry.file;
208 Block &Function::GetBlock(bool can_create) {
209 if (!m_block.BlockInfoHasBeenParsed() && can_create) {
211 CalculateSymbolContext(&sc);
213 sc.module_sp->GetSymbolVendor()->ParseFunctionBlocks(sc);
215 Host::SystemLog(Host::eSystemLogError, "error: unable to find module "
216 "shared pointer for function '%s' "
218 GetName().GetCString(), m_comp_unit->GetPath().c_str());
220 m_block.SetBlockInfoHasBeenParsed(true, true);
225 CompileUnit *Function::GetCompileUnit() { return m_comp_unit; }
227 const CompileUnit *Function::GetCompileUnit() const { return m_comp_unit; }
229 void Function::GetDescription(Stream *s, lldb::DescriptionLevel level,
231 ConstString name = GetName();
232 ConstString mangled = m_mangled.GetMangledName();
234 *s << "id = " << (const UserID &)*this;
236 *s << ", name = \"" << name.GetCString() << '"';
238 *s << ", mangled = \"" << mangled.GetCString() << '"';
240 Address::DumpStyle fallback_style;
241 if (level == eDescriptionLevelVerbose)
242 fallback_style = Address::DumpStyleModuleWithFileAddress;
244 fallback_style = Address::DumpStyleFileAddress;
245 GetAddressRange().Dump(s, target, Address::DumpStyleLoadAddress,
249 void Function::Dump(Stream *s, bool show_context) const {
250 s->Printf("%p: ", static_cast<const void *>(this));
252 *s << "Function" << static_cast<const UserID &>(*this);
257 s->Printf(", type = %p", static_cast<void *>(m_type));
258 else if (m_type_uid != LLDB_INVALID_UID)
259 s->Printf(", type_uid = 0x%8.8" PRIx64, m_type_uid);
262 // Dump the root object
263 if (m_block.BlockInfoHasBeenParsed())
264 m_block.Dump(s, m_range.GetBaseAddress().GetFileAddress(), INT_MAX,
268 void Function::CalculateSymbolContext(SymbolContext *sc) {
270 m_comp_unit->CalculateSymbolContext(sc);
273 ModuleSP Function::CalculateSymbolContextModule() {
274 SectionSP section_sp(m_range.GetBaseAddress().GetSection());
276 return section_sp->GetModule();
278 return this->GetCompileUnit()->GetModule();
281 CompileUnit *Function::CalculateSymbolContextCompileUnit() {
282 return this->GetCompileUnit();
285 Function *Function::CalculateSymbolContextFunction() { return this; }
287 lldb::DisassemblerSP Function::GetInstructions(const ExecutionContext &exe_ctx,
289 bool prefer_file_cache) {
290 ModuleSP module_sp(GetAddressRange().GetBaseAddress().GetModule());
292 const bool prefer_file_cache = false;
293 return Disassembler::DisassembleRange(module_sp->GetArchitecture(), nullptr,
294 flavor, exe_ctx, GetAddressRange(),
297 return lldb::DisassemblerSP();
300 bool Function::GetDisassembly(const ExecutionContext &exe_ctx,
301 const char *flavor, bool prefer_file_cache,
303 lldb::DisassemblerSP disassembler_sp =
304 GetInstructions(exe_ctx, flavor, prefer_file_cache);
305 if (disassembler_sp) {
306 const bool show_address = true;
307 const bool show_bytes = false;
308 disassembler_sp->GetInstructionList().Dump(&strm, show_address, show_bytes,
316 // Function::CalculateSymbolContextSymbol ()
318 // return // TODO: find the symbol for the function???
321 void Function::DumpSymbolContext(Stream *s) {
322 m_comp_unit->DumpSymbolContext(s);
323 s->Printf(", Function{0x%8.8" PRIx64 "}", GetID());
326 size_t Function::MemorySize() const {
327 size_t mem_size = sizeof(Function) + m_block.MemorySize();
331 bool Function::GetIsOptimized() {
334 // Currently optimization is only indicted by the
335 // vendor extension DW_AT_APPLE_optimized which
336 // is set on a compile unit level.
338 result = m_comp_unit->GetIsOptimized();
343 bool Function::IsTopLevelFunction() {
346 if (Language *language = Language::FindPlugin(GetLanguage()))
347 result = language->IsTopLevelFunction(*this);
352 ConstString Function::GetDisplayName() const {
353 return m_mangled.GetDisplayDemangledName(GetLanguage());
356 CompilerDeclContext Function::GetDeclContext() {
357 ModuleSP module_sp = CalculateSymbolContextModule();
360 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
363 SymbolFile *sym_file = sym_vendor->GetSymbolFile();
366 return sym_file->GetDeclContextForUID(GetID());
369 return CompilerDeclContext();
372 Type *Function::GetType() {
373 if (m_type == nullptr) {
376 CalculateSymbolContext(&sc);
381 SymbolVendor *sym_vendor = sc.module_sp->GetSymbolVendor();
383 if (sym_vendor == nullptr)
386 SymbolFile *sym_file = sym_vendor->GetSymbolFile();
388 if (sym_file == nullptr)
391 m_type = sym_file->ResolveTypeUID(m_type_uid);
396 const Type *Function::GetType() const { return m_type; }
398 CompilerType Function::GetCompilerType() {
399 Type *function_type = GetType();
401 return function_type->GetFullCompilerType();
402 return CompilerType();
405 uint32_t Function::GetPrologueByteSize() {
406 if (m_prologue_byte_size == 0 &&
407 m_flags.IsClear(flagsCalculatedPrologueSize)) {
408 m_flags.Set(flagsCalculatedPrologueSize);
409 LineTable *line_table = m_comp_unit->GetLineTable();
410 uint32_t prologue_end_line_idx = 0;
413 LineEntry first_line_entry;
414 uint32_t first_line_entry_idx = UINT32_MAX;
415 if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(),
417 &first_line_entry_idx)) {
418 // Make sure the first line entry isn't already the end of the prologue
419 addr_t prologue_end_file_addr = LLDB_INVALID_ADDRESS;
420 addr_t line_zero_end_file_addr = LLDB_INVALID_ADDRESS;
422 if (first_line_entry.is_prologue_end) {
423 prologue_end_file_addr =
424 first_line_entry.range.GetBaseAddress().GetFileAddress();
425 prologue_end_line_idx = first_line_entry_idx;
427 // Check the first few instructions and look for one that has
428 // is_prologue_end set to true.
429 const uint32_t last_line_entry_idx = first_line_entry_idx + 6;
430 for (uint32_t idx = first_line_entry_idx + 1;
431 idx < last_line_entry_idx; ++idx) {
432 LineEntry line_entry;
433 if (line_table->GetLineEntryAtIndex(idx, line_entry)) {
434 if (line_entry.is_prologue_end) {
435 prologue_end_file_addr =
436 line_entry.range.GetBaseAddress().GetFileAddress();
437 prologue_end_line_idx = idx;
444 // If we didn't find the end of the prologue in the line tables,
445 // then just use the end address of the first line table entry
446 if (prologue_end_file_addr == LLDB_INVALID_ADDRESS) {
447 // Check the first few instructions and look for one that has
448 // a line number that's different than the first entry.
449 uint32_t last_line_entry_idx = first_line_entry_idx + 6;
450 for (uint32_t idx = first_line_entry_idx + 1;
451 idx < last_line_entry_idx; ++idx) {
452 LineEntry line_entry;
453 if (line_table->GetLineEntryAtIndex(idx, line_entry)) {
454 if (line_entry.line != first_line_entry.line) {
455 prologue_end_file_addr =
456 line_entry.range.GetBaseAddress().GetFileAddress();
457 prologue_end_line_idx = idx;
463 if (prologue_end_file_addr == LLDB_INVALID_ADDRESS) {
464 prologue_end_file_addr =
465 first_line_entry.range.GetBaseAddress().GetFileAddress() +
466 first_line_entry.range.GetByteSize();
467 prologue_end_line_idx = first_line_entry_idx;
471 const addr_t func_start_file_addr =
472 m_range.GetBaseAddress().GetFileAddress();
473 const addr_t func_end_file_addr =
474 func_start_file_addr + m_range.GetByteSize();
476 // Now calculate the offset to pass the subsequent line 0 entries.
477 uint32_t first_non_zero_line = prologue_end_line_idx;
479 LineEntry line_entry;
480 if (line_table->GetLineEntryAtIndex(first_non_zero_line,
482 if (line_entry.line != 0)
485 if (line_entry.range.GetBaseAddress().GetFileAddress() >=
489 first_non_zero_line++;
492 if (first_non_zero_line > prologue_end_line_idx) {
493 LineEntry first_non_zero_entry;
494 if (line_table->GetLineEntryAtIndex(first_non_zero_line,
495 first_non_zero_entry)) {
496 line_zero_end_file_addr =
497 first_non_zero_entry.range.GetBaseAddress().GetFileAddress();
501 // Verify that this prologue end file address in the function's
502 // address range just to be sure
503 if (func_start_file_addr < prologue_end_file_addr &&
504 prologue_end_file_addr < func_end_file_addr) {
505 m_prologue_byte_size = prologue_end_file_addr - func_start_file_addr;
508 if (prologue_end_file_addr < line_zero_end_file_addr &&
509 line_zero_end_file_addr < func_end_file_addr) {
510 m_prologue_byte_size +=
511 line_zero_end_file_addr - prologue_end_file_addr;
517 return m_prologue_byte_size;
520 lldb::LanguageType Function::GetLanguage() const {
522 return m_comp_unit->GetLanguage();
524 return lldb::eLanguageTypeUnknown;
527 ConstString Function::GetName() const {
528 LanguageType language = lldb::eLanguageTypeUnknown;
530 language = m_comp_unit->GetLanguage();
531 return m_mangled.GetName(language);
534 ConstString Function::GetNameNoArguments() const {
535 LanguageType language = lldb::eLanguageTypeUnknown;
537 language = m_comp_unit->GetLanguage();
538 return m_mangled.GetName(language, Mangled::ePreferDemangledWithoutArguments);