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/ClangASTType.h"
16 #include "lldb/Symbol/CompileUnit.h"
17 #include "lldb/Symbol/LineTable.h"
18 #include "lldb/Symbol/SymbolFile.h"
19 #include "lldb/Symbol/SymbolVendor.h"
20 #include "llvm/Support/Casting.h"
23 using namespace lldb_private;
25 //----------------------------------------------------------------------
26 // Basic function information is contained in the FunctionInfo class.
27 // It is designed to contain the name, linkage name, and declaration
29 //----------------------------------------------------------------------
30 FunctionInfo::FunctionInfo (const char *name, const Declaration *decl_ptr) :
32 m_declaration(decl_ptr)
37 FunctionInfo::FunctionInfo (const ConstString& name, const Declaration *decl_ptr) :
39 m_declaration(decl_ptr)
44 FunctionInfo::~FunctionInfo()
49 FunctionInfo::Dump(Stream *s, bool show_fullpaths) const
52 *s << ", name = \"" << m_name << "\"";
53 m_declaration.Dump(s, show_fullpaths);
58 FunctionInfo::Compare(const FunctionInfo& a, const FunctionInfo& b)
60 int result = ConstString::Compare(a.GetName(), b.GetName());
64 return Declaration::Compare(a.m_declaration, b.m_declaration);
69 FunctionInfo::GetDeclaration()
75 FunctionInfo::GetDeclaration() const
81 FunctionInfo::GetName() const
87 FunctionInfo::MemorySize() const
89 return m_name.MemorySize() + m_declaration.MemorySize();
93 InlineFunctionInfo::InlineFunctionInfo
97 const Declaration *decl_ptr,
98 const Declaration *call_decl_ptr
100 FunctionInfo(name, decl_ptr),
101 m_mangled(ConstString(mangled), true),
102 m_call_decl (call_decl_ptr)
106 InlineFunctionInfo::InlineFunctionInfo
108 const ConstString& name,
109 const Mangled &mangled,
110 const Declaration *decl_ptr,
111 const Declaration *call_decl_ptr
113 FunctionInfo(name, decl_ptr),
115 m_call_decl (call_decl_ptr)
119 InlineFunctionInfo::~InlineFunctionInfo()
124 InlineFunctionInfo::Compare(const InlineFunctionInfo& a, const InlineFunctionInfo& b)
127 int result = FunctionInfo::Compare(a, b);
130 // only compare the mangled names if both have them
131 return Mangled::Compare(a.m_mangled, a.m_mangled);
135 InlineFunctionInfo::Dump(Stream *s, bool show_fullpaths) const
137 FunctionInfo::Dump(s, show_fullpaths);
143 InlineFunctionInfo::DumpStopContext (Stream *s) const
145 // s->Indent("[inlined] ");
148 s->PutCString (m_mangled.GetName().AsCString());
150 s->PutCString (m_name.AsCString());
155 InlineFunctionInfo::GetName () const
158 return m_mangled.GetName();
164 InlineFunctionInfo::GetCallSite ()
170 InlineFunctionInfo::GetCallSite () const
177 InlineFunctionInfo::GetMangled()
183 InlineFunctionInfo::GetMangled() const
189 InlineFunctionInfo::MemorySize() const
191 return FunctionInfo::MemorySize() + m_mangled.MemorySize();
194 //----------------------------------------------------------------------
196 //----------------------------------------------------------------------
199 CompileUnit *comp_unit,
200 lldb::user_id_t func_uid,
201 lldb::user_id_t type_uid,
202 const Mangled &mangled,
204 const AddressRange& range
207 m_comp_unit (comp_unit),
208 m_type_uid (type_uid),
215 m_prologue_byte_size (0)
217 m_block.SetParentScope(this);
218 assert(comp_unit != NULL);
223 CompileUnit *comp_unit,
224 lldb::user_id_t func_uid,
225 lldb::user_id_t type_uid,
228 const AddressRange &range
231 m_comp_unit (comp_unit),
232 m_type_uid (type_uid),
234 m_mangled (ConstString(mangled), true),
239 m_prologue_byte_size (0)
241 m_block.SetParentScope(this);
242 assert(comp_unit != NULL);
246 Function::~Function()
251 Function::GetStartLineSourceInfo (FileSpec &source_file, uint32_t &line_no)
256 if (m_comp_unit == NULL)
259 if (m_type != NULL && m_type->GetDeclaration().GetLine() != 0)
261 source_file = m_type->GetDeclaration().GetFile();
262 line_no = m_type->GetDeclaration().GetLine();
266 LineTable *line_table = m_comp_unit->GetLineTable();
267 if (line_table == NULL)
270 LineEntry line_entry;
271 if (line_table->FindLineEntryByAddress (GetAddressRange().GetBaseAddress(), line_entry, NULL))
273 line_no = line_entry.line;
274 source_file = line_entry.file;
280 Function::GetEndLineSourceInfo (FileSpec &source_file, uint32_t &line_no)
285 // The -1 is kind of cheesy, but I want to get the last line entry for the given function, not the
286 // first entry of the next.
287 Address scratch_addr(GetAddressRange().GetBaseAddress());
288 scratch_addr.SetOffset (scratch_addr.GetOffset() + GetAddressRange().GetByteSize() - 1);
290 LineTable *line_table = m_comp_unit->GetLineTable();
291 if (line_table == NULL)
294 LineEntry line_entry;
295 if (line_table->FindLineEntryByAddress (scratch_addr, line_entry, NULL))
297 line_no = line_entry.line;
298 source_file = line_entry.file;
303 Function::GetBlock (bool can_create)
305 if (!m_block.BlockInfoHasBeenParsed() && can_create)
308 CalculateSymbolContext(&sc);
311 sc.module_sp->GetSymbolVendor()->ParseFunctionBlocks(sc);
315 Host::SystemLog (Host::eSystemLogError,
316 "error: unable to find module shared pointer for function '%s' in %s\n",
317 GetName().GetCString(),
318 m_comp_unit->GetPath().c_str());
320 m_block.SetBlockInfoHasBeenParsed (true, true);
326 Function::GetCompileUnit()
332 Function::GetCompileUnit() const
339 Function::GetDescription(Stream *s, lldb::DescriptionLevel level, Target *target)
341 Type* func_type = GetType();
342 const char *name = func_type ? func_type->GetName().AsCString() : "<unknown>";
344 *s << "id = " << (const UserID&)*this << ", name = \"" << name << "\", range = ";
346 Address::DumpStyle fallback_style;
347 if (level == eDescriptionLevelVerbose)
348 fallback_style = Address::DumpStyleModuleWithFileAddress;
350 fallback_style = Address::DumpStyleFileAddress;
351 GetAddressRange().Dump(s, target, Address::DumpStyleLoadAddress, fallback_style);
355 Function::Dump(Stream *s, bool show_context) const
357 s->Printf("%p: ", this);
359 *s << "Function" << (const UserID&)*this;
365 s->Printf(", type = %p", m_type);
367 else if (m_type_uid != LLDB_INVALID_UID)
369 s->Printf(", type_uid = 0x%8.8" PRIx64, m_type_uid);
373 // Dump the root object
374 if (m_block.BlockInfoHasBeenParsed ())
375 m_block.Dump(s, m_range.GetBaseAddress().GetFileAddress(), INT_MAX, show_context);
380 Function::CalculateSymbolContext(SymbolContext* sc)
383 m_comp_unit->CalculateSymbolContext(sc);
387 Function::CalculateSymbolContextModule ()
389 SectionSP section_sp (m_range.GetBaseAddress().GetSection());
391 return section_sp->GetModule();
393 return this->GetCompileUnit()->GetModule();
397 Function::CalculateSymbolContextCompileUnit ()
399 return this->GetCompileUnit();
403 Function::CalculateSymbolContextFunction ()
409 Function::GetInstructions (const ExecutionContext &exe_ctx,
411 bool prefer_file_cache)
413 ModuleSP module_sp (GetAddressRange().GetBaseAddress().GetModule());
416 const bool prefer_file_cache = false;
417 return Disassembler::DisassembleRange (module_sp->GetArchitecture(),
424 return lldb::DisassemblerSP();
428 Function::GetDisassembly (const ExecutionContext &exe_ctx,
430 bool prefer_file_cache,
433 lldb::DisassemblerSP disassembler_sp = GetInstructions (exe_ctx, flavor, prefer_file_cache);
436 const bool show_address = true;
437 const bool show_bytes = false;
438 disassembler_sp->GetInstructionList().Dump (&strm, show_address, show_bytes, &exe_ctx);
446 //Function::CalculateSymbolContextSymbol ()
448 // return // TODO: find the symbol for the function???
453 Function::DumpSymbolContext(Stream *s)
455 m_comp_unit->DumpSymbolContext(s);
456 s->Printf(", Function{0x%8.8" PRIx64 "}", GetID());
460 Function::MemorySize () const
462 size_t mem_size = sizeof(Function) + m_block.MemorySize();
467 Function::GetClangDeclContext()
471 CalculateSymbolContext (&sc);
476 SymbolVendor *sym_vendor = sc.module_sp->GetSymbolVendor();
481 SymbolFile *sym_file = sym_vendor->GetSymbolFile();
486 return sym_file->GetClangDeclContextForTypeUID (sc, m_uid);
496 CalculateSymbolContext (&sc);
501 SymbolVendor *sym_vendor = sc.module_sp->GetSymbolVendor();
503 if (sym_vendor == NULL)
506 SymbolFile *sym_file = sym_vendor->GetSymbolFile();
508 if (sym_file == NULL)
511 m_type = sym_file->ResolveTypeUID(m_type_uid);
517 Function::GetType() const
523 Function::GetClangType()
525 Type *function_type = GetType();
527 return function_type->GetClangFullType();
528 return ClangASTType();
532 Function::GetPrologueByteSize ()
534 if (m_prologue_byte_size == 0 && m_flags.IsClear(flagsCalculatedPrologueSize))
536 m_flags.Set(flagsCalculatedPrologueSize);
537 LineTable* line_table = m_comp_unit->GetLineTable ();
540 LineEntry first_line_entry;
541 uint32_t first_line_entry_idx = UINT32_MAX;
542 if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(), first_line_entry, &first_line_entry_idx))
544 // Make sure the first line entry isn't already the end of the prologue
545 addr_t prologue_end_file_addr = LLDB_INVALID_ADDRESS;
546 if (first_line_entry.is_prologue_end)
548 prologue_end_file_addr = first_line_entry.range.GetBaseAddress().GetFileAddress();
552 // Check the first few instructions and look for one that has
553 // is_prologue_end set to true.
554 const uint32_t last_line_entry_idx = first_line_entry_idx + 6;
555 for (uint32_t idx = first_line_entry_idx + 1; idx < last_line_entry_idx; ++idx)
557 LineEntry line_entry;
558 if (line_table->GetLineEntryAtIndex (idx, line_entry))
560 if (line_entry.is_prologue_end)
562 prologue_end_file_addr = line_entry.range.GetBaseAddress().GetFileAddress();
569 // If we didn't find the end of the prologue in the line tables,
570 // then just use the end address of the first line table entry
571 if (prologue_end_file_addr == LLDB_INVALID_ADDRESS)
573 // Check the first few instructions and look for one that has
574 // a line number that's different than the first entry.
575 const uint32_t last_line_entry_idx = first_line_entry_idx + 6;
576 for (uint32_t idx = first_line_entry_idx + 1; idx < last_line_entry_idx; ++idx)
578 LineEntry line_entry;
579 if (line_table->GetLineEntryAtIndex (idx, line_entry))
581 if (line_entry.line != first_line_entry.line)
583 prologue_end_file_addr = line_entry.range.GetBaseAddress().GetFileAddress();
589 if (prologue_end_file_addr == LLDB_INVALID_ADDRESS)
591 prologue_end_file_addr = first_line_entry.range.GetBaseAddress().GetFileAddress() + first_line_entry.range.GetByteSize();
594 const addr_t func_start_file_addr = m_range.GetBaseAddress().GetFileAddress();
595 const addr_t func_end_file_addr = func_start_file_addr + m_range.GetByteSize();
597 // Verify that this prologue end file address in the function's
598 // address range just to be sure
599 if (func_start_file_addr < prologue_end_file_addr && prologue_end_file_addr < func_end_file_addr)
601 m_prologue_byte_size = prologue_end_file_addr - func_start_file_addr;
606 return m_prologue_byte_size;