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/Module.h"
12 #include "lldb/Core/Section.h"
13 #include "lldb/Host/Host.h"
14 #include "lldb/Symbol/ClangASTType.h"
15 #include "lldb/Symbol/CompileUnit.h"
16 #include "lldb/Symbol/LineTable.h"
17 #include "lldb/Symbol/SymbolFile.h"
18 #include "lldb/Symbol/SymbolVendor.h"
19 #include "llvm/Support/Casting.h"
22 using namespace lldb_private;
24 //----------------------------------------------------------------------
25 // Basic function information is contained in the FunctionInfo class.
26 // It is designed to contain the name, linkage name, and declaration
28 //----------------------------------------------------------------------
29 FunctionInfo::FunctionInfo (const char *name, const Declaration *decl_ptr) :
31 m_declaration(decl_ptr)
36 FunctionInfo::FunctionInfo (const ConstString& name, const Declaration *decl_ptr) :
38 m_declaration(decl_ptr)
43 FunctionInfo::~FunctionInfo()
48 FunctionInfo::Dump(Stream *s, bool show_fullpaths) const
51 *s << ", name = \"" << m_name << "\"";
52 m_declaration.Dump(s, show_fullpaths);
57 FunctionInfo::Compare(const FunctionInfo& a, const FunctionInfo& b)
59 int result = ConstString::Compare(a.GetName(), b.GetName());
63 return Declaration::Compare(a.m_declaration, b.m_declaration);
68 FunctionInfo::GetDeclaration()
74 FunctionInfo::GetDeclaration() const
80 FunctionInfo::GetName() const
86 FunctionInfo::MemorySize() const
88 return m_name.MemorySize() + m_declaration.MemorySize();
92 InlineFunctionInfo::InlineFunctionInfo
96 const Declaration *decl_ptr,
97 const Declaration *call_decl_ptr
99 FunctionInfo(name, decl_ptr),
100 m_mangled(ConstString(mangled), true),
101 m_call_decl (call_decl_ptr)
105 InlineFunctionInfo::InlineFunctionInfo
107 const ConstString& name,
108 const Mangled &mangled,
109 const Declaration *decl_ptr,
110 const Declaration *call_decl_ptr
112 FunctionInfo(name, decl_ptr),
114 m_call_decl (call_decl_ptr)
118 InlineFunctionInfo::~InlineFunctionInfo()
123 InlineFunctionInfo::Compare(const InlineFunctionInfo& a, const InlineFunctionInfo& b)
126 int result = FunctionInfo::Compare(a, b);
129 // only compare the mangled names if both have them
130 return Mangled::Compare(a.m_mangled, a.m_mangled);
134 InlineFunctionInfo::Dump(Stream *s, bool show_fullpaths) const
136 FunctionInfo::Dump(s, show_fullpaths);
142 InlineFunctionInfo::DumpStopContext (Stream *s) const
144 // s->Indent("[inlined] ");
147 s->PutCString (m_mangled.GetName().AsCString());
149 s->PutCString (m_name.AsCString());
154 InlineFunctionInfo::GetName () const
157 return m_mangled.GetName();
163 InlineFunctionInfo::GetCallSite ()
169 InlineFunctionInfo::GetCallSite () const
176 InlineFunctionInfo::GetMangled()
182 InlineFunctionInfo::GetMangled() const
188 InlineFunctionInfo::MemorySize() const
190 return FunctionInfo::MemorySize() + m_mangled.MemorySize();
193 //----------------------------------------------------------------------
195 //----------------------------------------------------------------------
198 CompileUnit *comp_unit,
199 lldb::user_id_t func_uid,
200 lldb::user_id_t type_uid,
201 const Mangled &mangled,
203 const AddressRange& range
206 m_comp_unit (comp_unit),
207 m_type_uid (type_uid),
214 m_prologue_byte_size (0)
216 m_block.SetParentScope(this);
217 assert(comp_unit != NULL);
222 CompileUnit *comp_unit,
223 lldb::user_id_t func_uid,
224 lldb::user_id_t type_uid,
227 const AddressRange &range
230 m_comp_unit (comp_unit),
231 m_type_uid (type_uid),
233 m_mangled (ConstString(mangled), true),
238 m_prologue_byte_size (0)
240 m_block.SetParentScope(this);
241 assert(comp_unit != NULL);
245 Function::~Function()
250 Function::GetStartLineSourceInfo (FileSpec &source_file, uint32_t &line_no)
255 if (m_comp_unit == NULL)
258 if (m_type != NULL && m_type->GetDeclaration().GetLine() != 0)
260 source_file = m_type->GetDeclaration().GetFile();
261 line_no = m_type->GetDeclaration().GetLine();
265 LineTable *line_table = m_comp_unit->GetLineTable();
266 if (line_table == NULL)
269 LineEntry line_entry;
270 if (line_table->FindLineEntryByAddress (GetAddressRange().GetBaseAddress(), line_entry, NULL))
272 line_no = line_entry.line;
273 source_file = line_entry.file;
279 Function::GetEndLineSourceInfo (FileSpec &source_file, uint32_t &line_no)
284 // The -1 is kind of cheesy, but I want to get the last line entry for the given function, not the
285 // first entry of the next.
286 Address scratch_addr(GetAddressRange().GetBaseAddress());
287 scratch_addr.SetOffset (scratch_addr.GetOffset() + GetAddressRange().GetByteSize() - 1);
289 LineTable *line_table = m_comp_unit->GetLineTable();
290 if (line_table == NULL)
293 LineEntry line_entry;
294 if (line_table->FindLineEntryByAddress (scratch_addr, line_entry, NULL))
296 line_no = line_entry.line;
297 source_file = line_entry.file;
302 Function::GetBlock (bool can_create)
304 if (!m_block.BlockInfoHasBeenParsed() && can_create)
307 CalculateSymbolContext(&sc);
310 sc.module_sp->GetSymbolVendor()->ParseFunctionBlocks(sc);
314 Host::SystemLog (Host::eSystemLogError,
315 "error: unable to find module shared pointer for function '%s' in %s\n",
316 GetName().GetCString(),
317 m_comp_unit->GetPath().c_str());
319 m_block.SetBlockInfoHasBeenParsed (true, true);
325 Function::GetCompileUnit()
331 Function::GetCompileUnit() const
338 Function::GetDescription(Stream *s, lldb::DescriptionLevel level, Target *target)
340 Type* func_type = GetType();
341 const char *name = func_type ? func_type->GetName().AsCString() : "<unknown>";
343 *s << "id = " << (const UserID&)*this << ", name = \"" << name << "\", range = ";
345 Address::DumpStyle fallback_style;
346 if (level == eDescriptionLevelVerbose)
347 fallback_style = Address::DumpStyleModuleWithFileAddress;
349 fallback_style = Address::DumpStyleFileAddress;
350 GetAddressRange().Dump(s, target, Address::DumpStyleLoadAddress, fallback_style);
354 Function::Dump(Stream *s, bool show_context) const
356 s->Printf("%p: ", this);
358 *s << "Function" << (const UserID&)*this;
364 s->Printf(", type = %p", m_type);
366 else if (m_type_uid != LLDB_INVALID_UID)
368 s->Printf(", type_uid = 0x%8.8" PRIx64, m_type_uid);
372 // Dump the root object
373 if (m_block.BlockInfoHasBeenParsed ())
374 m_block.Dump(s, m_range.GetBaseAddress().GetFileAddress(), INT_MAX, show_context);
379 Function::CalculateSymbolContext(SymbolContext* sc)
382 m_comp_unit->CalculateSymbolContext(sc);
386 Function::CalculateSymbolContextModule ()
388 SectionSP section_sp (m_range.GetBaseAddress().GetSection());
390 return section_sp->GetModule();
392 return this->GetCompileUnit()->GetModule();
396 Function::CalculateSymbolContextCompileUnit ()
398 return this->GetCompileUnit();
402 Function::CalculateSymbolContextFunction ()
408 //Function::CalculateSymbolContextSymbol ()
410 // return // TODO: find the symbol for the function???
415 Function::DumpSymbolContext(Stream *s)
417 m_comp_unit->DumpSymbolContext(s);
418 s->Printf(", Function{0x%8.8" PRIx64 "}", GetID());
422 Function::MemorySize () const
424 size_t mem_size = sizeof(Function) + m_block.MemorySize();
429 Function::GetClangDeclContext()
433 CalculateSymbolContext (&sc);
438 SymbolVendor *sym_vendor = sc.module_sp->GetSymbolVendor();
443 SymbolFile *sym_file = sym_vendor->GetSymbolFile();
448 return sym_file->GetClangDeclContextForTypeUID (sc, m_uid);
458 CalculateSymbolContext (&sc);
463 SymbolVendor *sym_vendor = sc.module_sp->GetSymbolVendor();
465 if (sym_vendor == NULL)
468 SymbolFile *sym_file = sym_vendor->GetSymbolFile();
470 if (sym_file == NULL)
473 m_type = sym_file->ResolveTypeUID(m_type_uid);
479 Function::GetType() const
485 Function::GetClangType()
487 Type *function_type = GetType();
489 return function_type->GetClangFullType();
490 return ClangASTType();
494 Function::GetPrologueByteSize ()
496 if (m_prologue_byte_size == 0 && m_flags.IsClear(flagsCalculatedPrologueSize))
498 m_flags.Set(flagsCalculatedPrologueSize);
499 LineTable* line_table = m_comp_unit->GetLineTable ();
502 LineEntry first_line_entry;
503 uint32_t first_line_entry_idx = UINT32_MAX;
504 if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(), first_line_entry, &first_line_entry_idx))
506 // Make sure the first line entry isn't already the end of the prologue
507 addr_t prologue_end_file_addr = LLDB_INVALID_ADDRESS;
508 if (first_line_entry.is_prologue_end)
510 prologue_end_file_addr = first_line_entry.range.GetBaseAddress().GetFileAddress();
514 // Check the first few instructions and look for one that has
515 // is_prologue_end set to true.
516 const uint32_t last_line_entry_idx = first_line_entry_idx + 6;
517 for (uint32_t idx = first_line_entry_idx + 1; idx < last_line_entry_idx; ++idx)
519 LineEntry line_entry;
520 if (line_table->GetLineEntryAtIndex (idx, line_entry))
522 if (line_entry.is_prologue_end)
524 prologue_end_file_addr = line_entry.range.GetBaseAddress().GetFileAddress();
531 // If we didn't find the end of the prologue in the line tables,
532 // then just use the end address of the first line table entry
533 if (prologue_end_file_addr == LLDB_INVALID_ADDRESS)
535 // Check the first few instructions and look for one that has
536 // a line number that's different than the first entry.
537 const uint32_t last_line_entry_idx = first_line_entry_idx + 6;
538 for (uint32_t idx = first_line_entry_idx + 1; idx < last_line_entry_idx; ++idx)
540 LineEntry line_entry;
541 if (line_table->GetLineEntryAtIndex (idx, line_entry))
543 if (line_entry.line != first_line_entry.line)
545 prologue_end_file_addr = line_entry.range.GetBaseAddress().GetFileAddress();
551 if (prologue_end_file_addr == LLDB_INVALID_ADDRESS)
553 prologue_end_file_addr = first_line_entry.range.GetBaseAddress().GetFileAddress() + first_line_entry.range.GetByteSize();
556 const addr_t func_start_file_addr = m_range.GetBaseAddress().GetFileAddress();
557 const addr_t func_end_file_addr = func_start_file_addr + m_range.GetByteSize();
559 // Verify that this prologue end file address in the function's
560 // address range just to be sure
561 if (func_start_file_addr < prologue_end_file_addr && prologue_end_file_addr < func_end_file_addr)
563 m_prologue_byte_size = prologue_end_file_addr - func_start_file_addr;
568 return m_prologue_byte_size;