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/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"
21
22 using namespace lldb;
23 using namespace lldb_private;
24
25 //----------------------------------------------------------------------
26 // Basic function information is contained in the FunctionInfo class.
27 // It is designed to contain the name, linkage name, and declaration
28 // location.
29 //----------------------------------------------------------------------
FunctionInfo(const char * name,const Declaration * decl_ptr)30 FunctionInfo::FunctionInfo (const char *name, const Declaration *decl_ptr) :
31 m_name(name),
32 m_declaration(decl_ptr)
33 {
34 }
35
36
FunctionInfo(const ConstString & name,const Declaration * decl_ptr)37 FunctionInfo::FunctionInfo (const ConstString& name, const Declaration *decl_ptr) :
38 m_name(name),
39 m_declaration(decl_ptr)
40 {
41 }
42
43
~FunctionInfo()44 FunctionInfo::~FunctionInfo()
45 {
46 }
47
48 void
Dump(Stream * s,bool show_fullpaths) const49 FunctionInfo::Dump(Stream *s, bool show_fullpaths) const
50 {
51 if (m_name)
52 *s << ", name = \"" << m_name << "\"";
53 m_declaration.Dump(s, show_fullpaths);
54 }
55
56
57 int
Compare(const FunctionInfo & a,const FunctionInfo & b)58 FunctionInfo::Compare(const FunctionInfo& a, const FunctionInfo& b)
59 {
60 int result = ConstString::Compare(a.GetName(), b.GetName());
61 if (result)
62 return result;
63
64 return Declaration::Compare(a.m_declaration, b.m_declaration);
65 }
66
67
68 Declaration&
GetDeclaration()69 FunctionInfo::GetDeclaration()
70 {
71 return m_declaration;
72 }
73
74 const Declaration&
GetDeclaration() const75 FunctionInfo::GetDeclaration() const
76 {
77 return m_declaration;
78 }
79
80 const ConstString&
GetName() const81 FunctionInfo::GetName() const
82 {
83 return m_name;
84 }
85
86 size_t
MemorySize() const87 FunctionInfo::MemorySize() const
88 {
89 return m_name.MemorySize() + m_declaration.MemorySize();
90 }
91
92
InlineFunctionInfo(const char * name,const char * mangled,const Declaration * decl_ptr,const Declaration * call_decl_ptr)93 InlineFunctionInfo::InlineFunctionInfo
94 (
95 const char *name,
96 const char *mangled,
97 const Declaration *decl_ptr,
98 const Declaration *call_decl_ptr
99 ) :
100 FunctionInfo(name, decl_ptr),
101 m_mangled(ConstString(mangled), true),
102 m_call_decl (call_decl_ptr)
103 {
104 }
105
InlineFunctionInfo(const ConstString & name,const Mangled & mangled,const Declaration * decl_ptr,const Declaration * call_decl_ptr)106 InlineFunctionInfo::InlineFunctionInfo
107 (
108 const ConstString& name,
109 const Mangled &mangled,
110 const Declaration *decl_ptr,
111 const Declaration *call_decl_ptr
112 ) :
113 FunctionInfo(name, decl_ptr),
114 m_mangled(mangled),
115 m_call_decl (call_decl_ptr)
116 {
117 }
118
~InlineFunctionInfo()119 InlineFunctionInfo::~InlineFunctionInfo()
120 {
121 }
122
123 int
Compare(const InlineFunctionInfo & a,const InlineFunctionInfo & b)124 InlineFunctionInfo::Compare(const InlineFunctionInfo& a, const InlineFunctionInfo& b)
125 {
126
127 int result = FunctionInfo::Compare(a, b);
128 if (result)
129 return result;
130 // only compare the mangled names if both have them
131 return Mangled::Compare(a.m_mangled, a.m_mangled);
132 }
133
134 void
Dump(Stream * s,bool show_fullpaths) const135 InlineFunctionInfo::Dump(Stream *s, bool show_fullpaths) const
136 {
137 FunctionInfo::Dump(s, show_fullpaths);
138 if (m_mangled)
139 m_mangled.Dump(s);
140 }
141
142 void
DumpStopContext(Stream * s) const143 InlineFunctionInfo::DumpStopContext (Stream *s) const
144 {
145 // s->Indent("[inlined] ");
146 s->Indent();
147 if (m_mangled)
148 s->PutCString (m_mangled.GetName().AsCString());
149 else
150 s->PutCString (m_name.AsCString());
151 }
152
153
154 const ConstString &
GetName() const155 InlineFunctionInfo::GetName () const
156 {
157 if (m_mangled)
158 return m_mangled.GetName();
159 return m_name;
160 }
161
162
163 Declaration &
GetCallSite()164 InlineFunctionInfo::GetCallSite ()
165 {
166 return m_call_decl;
167 }
168
169 const Declaration &
GetCallSite() const170 InlineFunctionInfo::GetCallSite () const
171 {
172 return m_call_decl;
173 }
174
175
176 Mangled&
GetMangled()177 InlineFunctionInfo::GetMangled()
178 {
179 return m_mangled;
180 }
181
182 const Mangled&
GetMangled() const183 InlineFunctionInfo::GetMangled() const
184 {
185 return m_mangled;
186 }
187
188 size_t
MemorySize() const189 InlineFunctionInfo::MemorySize() const
190 {
191 return FunctionInfo::MemorySize() + m_mangled.MemorySize();
192 }
193
194 //----------------------------------------------------------------------
195 //
196 //----------------------------------------------------------------------
Function(CompileUnit * comp_unit,lldb::user_id_t func_uid,lldb::user_id_t type_uid,const Mangled & mangled,Type * type,const AddressRange & range)197 Function::Function
198 (
199 CompileUnit *comp_unit,
200 lldb::user_id_t func_uid,
201 lldb::user_id_t type_uid,
202 const Mangled &mangled,
203 Type * type,
204 const AddressRange& range
205 ) :
206 UserID (func_uid),
207 m_comp_unit (comp_unit),
208 m_type_uid (type_uid),
209 m_type (type),
210 m_mangled (mangled),
211 m_block (func_uid),
212 m_range (range),
213 m_frame_base (),
214 m_flags (),
215 m_prologue_byte_size (0)
216 {
217 m_block.SetParentScope(this);
218 assert(comp_unit != NULL);
219 }
220
Function(CompileUnit * comp_unit,lldb::user_id_t func_uid,lldb::user_id_t type_uid,const char * mangled,Type * type,const AddressRange & range)221 Function::Function
222 (
223 CompileUnit *comp_unit,
224 lldb::user_id_t func_uid,
225 lldb::user_id_t type_uid,
226 const char *mangled,
227 Type *type,
228 const AddressRange &range
229 ) :
230 UserID (func_uid),
231 m_comp_unit (comp_unit),
232 m_type_uid (type_uid),
233 m_type (type),
234 m_mangled (ConstString(mangled), true),
235 m_block (func_uid),
236 m_range (range),
237 m_frame_base (),
238 m_flags (),
239 m_prologue_byte_size (0)
240 {
241 m_block.SetParentScope(this);
242 assert(comp_unit != NULL);
243 }
244
245
~Function()246 Function::~Function()
247 {
248 }
249
250 void
GetStartLineSourceInfo(FileSpec & source_file,uint32_t & line_no)251 Function::GetStartLineSourceInfo (FileSpec &source_file, uint32_t &line_no)
252 {
253 line_no = 0;
254 source_file.Clear();
255
256 if (m_comp_unit == NULL)
257 return;
258
259 if (m_type != NULL && m_type->GetDeclaration().GetLine() != 0)
260 {
261 source_file = m_type->GetDeclaration().GetFile();
262 line_no = m_type->GetDeclaration().GetLine();
263 }
264 else
265 {
266 LineTable *line_table = m_comp_unit->GetLineTable();
267 if (line_table == NULL)
268 return;
269
270 LineEntry line_entry;
271 if (line_table->FindLineEntryByAddress (GetAddressRange().GetBaseAddress(), line_entry, NULL))
272 {
273 line_no = line_entry.line;
274 source_file = line_entry.file;
275 }
276 }
277 }
278
279 void
GetEndLineSourceInfo(FileSpec & source_file,uint32_t & line_no)280 Function::GetEndLineSourceInfo (FileSpec &source_file, uint32_t &line_no)
281 {
282 line_no = 0;
283 source_file.Clear();
284
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);
289
290 LineTable *line_table = m_comp_unit->GetLineTable();
291 if (line_table == NULL)
292 return;
293
294 LineEntry line_entry;
295 if (line_table->FindLineEntryByAddress (scratch_addr, line_entry, NULL))
296 {
297 line_no = line_entry.line;
298 source_file = line_entry.file;
299 }
300 }
301
302 Block &
GetBlock(bool can_create)303 Function::GetBlock (bool can_create)
304 {
305 if (!m_block.BlockInfoHasBeenParsed() && can_create)
306 {
307 SymbolContext sc;
308 CalculateSymbolContext(&sc);
309 if (sc.module_sp)
310 {
311 sc.module_sp->GetSymbolVendor()->ParseFunctionBlocks(sc);
312 }
313 else
314 {
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());
319 }
320 m_block.SetBlockInfoHasBeenParsed (true, true);
321 }
322 return m_block;
323 }
324
325 CompileUnit*
GetCompileUnit()326 Function::GetCompileUnit()
327 {
328 return m_comp_unit;
329 }
330
331 const CompileUnit*
GetCompileUnit() const332 Function::GetCompileUnit() const
333 {
334 return m_comp_unit;
335 }
336
337
338 void
GetDescription(Stream * s,lldb::DescriptionLevel level,Target * target)339 Function::GetDescription(Stream *s, lldb::DescriptionLevel level, Target *target)
340 {
341 Type* func_type = GetType();
342 const char *name = func_type ? func_type->GetName().AsCString() : "<unknown>";
343
344 *s << "id = " << (const UserID&)*this << ", name = \"" << name << "\", range = ";
345
346 Address::DumpStyle fallback_style;
347 if (level == eDescriptionLevelVerbose)
348 fallback_style = Address::DumpStyleModuleWithFileAddress;
349 else
350 fallback_style = Address::DumpStyleFileAddress;
351 GetAddressRange().Dump(s, target, Address::DumpStyleLoadAddress, fallback_style);
352 }
353
354 void
Dump(Stream * s,bool show_context) const355 Function::Dump(Stream *s, bool show_context) const
356 {
357 s->Printf("%p: ", this);
358 s->Indent();
359 *s << "Function" << (const UserID&)*this;
360
361 m_mangled.Dump(s);
362
363 if (m_type)
364 {
365 s->Printf(", type = %p", m_type);
366 }
367 else if (m_type_uid != LLDB_INVALID_UID)
368 {
369 s->Printf(", type_uid = 0x%8.8" PRIx64, m_type_uid);
370 }
371
372 s->EOL();
373 // Dump the root object
374 if (m_block.BlockInfoHasBeenParsed ())
375 m_block.Dump(s, m_range.GetBaseAddress().GetFileAddress(), INT_MAX, show_context);
376 }
377
378
379 void
CalculateSymbolContext(SymbolContext * sc)380 Function::CalculateSymbolContext(SymbolContext* sc)
381 {
382 sc->function = this;
383 m_comp_unit->CalculateSymbolContext(sc);
384 }
385
386 ModuleSP
CalculateSymbolContextModule()387 Function::CalculateSymbolContextModule ()
388 {
389 SectionSP section_sp (m_range.GetBaseAddress().GetSection());
390 if (section_sp)
391 return section_sp->GetModule();
392
393 return this->GetCompileUnit()->GetModule();
394 }
395
396 CompileUnit *
CalculateSymbolContextCompileUnit()397 Function::CalculateSymbolContextCompileUnit ()
398 {
399 return this->GetCompileUnit();
400 }
401
402 Function *
CalculateSymbolContextFunction()403 Function::CalculateSymbolContextFunction ()
404 {
405 return this;
406 }
407
408 lldb::DisassemblerSP
GetInstructions(const ExecutionContext & exe_ctx,const char * flavor,bool prefer_file_cache)409 Function::GetInstructions (const ExecutionContext &exe_ctx,
410 const char *flavor,
411 bool prefer_file_cache)
412 {
413 ModuleSP module_sp (GetAddressRange().GetBaseAddress().GetModule());
414 if (module_sp)
415 {
416 const bool prefer_file_cache = false;
417 return Disassembler::DisassembleRange (module_sp->GetArchitecture(),
418 NULL,
419 flavor,
420 exe_ctx,
421 GetAddressRange(),
422 prefer_file_cache);
423 }
424 return lldb::DisassemblerSP();
425 }
426
427 bool
GetDisassembly(const ExecutionContext & exe_ctx,const char * flavor,bool prefer_file_cache,Stream & strm)428 Function::GetDisassembly (const ExecutionContext &exe_ctx,
429 const char *flavor,
430 bool prefer_file_cache,
431 Stream &strm)
432 {
433 lldb::DisassemblerSP disassembler_sp = GetInstructions (exe_ctx, flavor, prefer_file_cache);
434 if (disassembler_sp)
435 {
436 const bool show_address = true;
437 const bool show_bytes = false;
438 disassembler_sp->GetInstructionList().Dump (&strm, show_address, show_bytes, &exe_ctx);
439 return true;
440 }
441 return false;
442 }
443
444
445 //Symbol *
446 //Function::CalculateSymbolContextSymbol ()
447 //{
448 // return // TODO: find the symbol for the function???
449 //}
450
451
452 void
DumpSymbolContext(Stream * s)453 Function::DumpSymbolContext(Stream *s)
454 {
455 m_comp_unit->DumpSymbolContext(s);
456 s->Printf(", Function{0x%8.8" PRIx64 "}", GetID());
457 }
458
459 size_t
MemorySize() const460 Function::MemorySize () const
461 {
462 size_t mem_size = sizeof(Function) + m_block.MemorySize();
463 return mem_size;
464 }
465
466 clang::DeclContext *
GetClangDeclContext()467 Function::GetClangDeclContext()
468 {
469 SymbolContext sc;
470
471 CalculateSymbolContext (&sc);
472
473 if (!sc.module_sp)
474 return NULL;
475
476 SymbolVendor *sym_vendor = sc.module_sp->GetSymbolVendor();
477
478 if (!sym_vendor)
479 return NULL;
480
481 SymbolFile *sym_file = sym_vendor->GetSymbolFile();
482
483 if (!sym_file)
484 return NULL;
485
486 return sym_file->GetClangDeclContextForTypeUID (sc, m_uid);
487 }
488
489 Type*
GetType()490 Function::GetType()
491 {
492 if (m_type == NULL)
493 {
494 SymbolContext sc;
495
496 CalculateSymbolContext (&sc);
497
498 if (!sc.module_sp)
499 return NULL;
500
501 SymbolVendor *sym_vendor = sc.module_sp->GetSymbolVendor();
502
503 if (sym_vendor == NULL)
504 return NULL;
505
506 SymbolFile *sym_file = sym_vendor->GetSymbolFile();
507
508 if (sym_file == NULL)
509 return NULL;
510
511 m_type = sym_file->ResolveTypeUID(m_type_uid);
512 }
513 return m_type;
514 }
515
516 const Type*
GetType() const517 Function::GetType() const
518 {
519 return m_type;
520 }
521
522 ClangASTType
GetClangType()523 Function::GetClangType()
524 {
525 Type *function_type = GetType();
526 if (function_type)
527 return function_type->GetClangFullType();
528 return ClangASTType();
529 }
530
531 uint32_t
GetPrologueByteSize()532 Function::GetPrologueByteSize ()
533 {
534 if (m_prologue_byte_size == 0 && m_flags.IsClear(flagsCalculatedPrologueSize))
535 {
536 m_flags.Set(flagsCalculatedPrologueSize);
537 LineTable* line_table = m_comp_unit->GetLineTable ();
538 if (line_table)
539 {
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))
543 {
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)
547 {
548 prologue_end_file_addr = first_line_entry.range.GetBaseAddress().GetFileAddress();
549 }
550 else
551 {
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)
556 {
557 LineEntry line_entry;
558 if (line_table->GetLineEntryAtIndex (idx, line_entry))
559 {
560 if (line_entry.is_prologue_end)
561 {
562 prologue_end_file_addr = line_entry.range.GetBaseAddress().GetFileAddress();
563 break;
564 }
565 }
566 }
567 }
568
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)
572 {
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)
577 {
578 LineEntry line_entry;
579 if (line_table->GetLineEntryAtIndex (idx, line_entry))
580 {
581 if (line_entry.line != first_line_entry.line)
582 {
583 prologue_end_file_addr = line_entry.range.GetBaseAddress().GetFileAddress();
584 break;
585 }
586 }
587 }
588
589 if (prologue_end_file_addr == LLDB_INVALID_ADDRESS)
590 {
591 prologue_end_file_addr = first_line_entry.range.GetBaseAddress().GetFileAddress() + first_line_entry.range.GetByteSize();
592 }
593 }
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();
596
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)
600 {
601 m_prologue_byte_size = prologue_end_file_addr - func_start_file_addr;
602 }
603 }
604 }
605 }
606 return m_prologue_byte_size;
607 }
608
609
610
611