1 //===-- Function.cpp ------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "lldb/Symbol/Function.h"
10 #include "lldb/Core/Debugger.h"
11 #include "lldb/Core/Disassembler.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/ModuleList.h"
14 #include "lldb/Core/Section.h"
15 #include "lldb/Host/Host.h"
16 #include "lldb/Symbol/CompileUnit.h"
17 #include "lldb/Symbol/CompilerType.h"
18 #include "lldb/Symbol/LineTable.h"
19 #include "lldb/Symbol/SymbolFile.h"
20 #include "lldb/Target/Language.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Utility/LLDBLog.h"
23 #include "lldb/Utility/Log.h"
24 #include "llvm/Support/Casting.h"
25
26 using namespace lldb;
27 using namespace lldb_private;
28
29 // Basic function information is contained in the FunctionInfo class. It is
30 // designed to contain the name, linkage name, and declaration location.
FunctionInfo(const char * name,const Declaration * decl_ptr)31 FunctionInfo::FunctionInfo(const char *name, const Declaration *decl_ptr)
32 : m_name(name), m_declaration(decl_ptr) {}
33
FunctionInfo(ConstString name,const Declaration * decl_ptr)34 FunctionInfo::FunctionInfo(ConstString name, const Declaration *decl_ptr)
35 : m_name(name), m_declaration(decl_ptr) {}
36
37 FunctionInfo::~FunctionInfo() = default;
38
Dump(Stream * s,bool show_fullpaths) const39 void FunctionInfo::Dump(Stream *s, bool show_fullpaths) const {
40 if (m_name)
41 *s << ", name = \"" << m_name << "\"";
42 m_declaration.Dump(s, show_fullpaths);
43 }
44
Compare(const FunctionInfo & a,const FunctionInfo & b)45 int FunctionInfo::Compare(const FunctionInfo &a, const FunctionInfo &b) {
46 int result = ConstString::Compare(a.GetName(), b.GetName());
47 if (result)
48 return result;
49
50 return Declaration::Compare(a.m_declaration, b.m_declaration);
51 }
52
GetDeclaration()53 Declaration &FunctionInfo::GetDeclaration() { return m_declaration; }
54
GetDeclaration() const55 const Declaration &FunctionInfo::GetDeclaration() const {
56 return m_declaration;
57 }
58
GetName() const59 ConstString FunctionInfo::GetName() const { return m_name; }
60
MemorySize() const61 size_t FunctionInfo::MemorySize() const {
62 return m_name.MemorySize() + m_declaration.MemorySize();
63 }
64
InlineFunctionInfo(const char * name,llvm::StringRef mangled,const Declaration * decl_ptr,const Declaration * call_decl_ptr)65 InlineFunctionInfo::InlineFunctionInfo(const char *name,
66 llvm::StringRef mangled,
67 const Declaration *decl_ptr,
68 const Declaration *call_decl_ptr)
69 : FunctionInfo(name, decl_ptr), m_mangled(mangled),
70 m_call_decl(call_decl_ptr) {}
71
InlineFunctionInfo(ConstString name,const Mangled & mangled,const Declaration * decl_ptr,const Declaration * call_decl_ptr)72 InlineFunctionInfo::InlineFunctionInfo(ConstString name,
73 const Mangled &mangled,
74 const Declaration *decl_ptr,
75 const Declaration *call_decl_ptr)
76 : FunctionInfo(name, decl_ptr), m_mangled(mangled),
77 m_call_decl(call_decl_ptr) {}
78
79 InlineFunctionInfo::~InlineFunctionInfo() = default;
80
Dump(Stream * s,bool show_fullpaths) const81 void InlineFunctionInfo::Dump(Stream *s, bool show_fullpaths) const {
82 FunctionInfo::Dump(s, show_fullpaths);
83 if (m_mangled)
84 m_mangled.Dump(s);
85 }
86
DumpStopContext(Stream * s) const87 void InlineFunctionInfo::DumpStopContext(Stream *s) const {
88 // s->Indent("[inlined] ");
89 s->Indent();
90 if (m_mangled)
91 s->PutCString(m_mangled.GetName().AsCString());
92 else
93 s->PutCString(m_name.AsCString());
94 }
95
GetName() const96 ConstString InlineFunctionInfo::GetName() const {
97 if (m_mangled)
98 return m_mangled.GetName();
99 return m_name;
100 }
101
GetDisplayName() const102 ConstString InlineFunctionInfo::GetDisplayName() const {
103 if (m_mangled)
104 return m_mangled.GetDisplayDemangledName();
105 return m_name;
106 }
107
GetCallSite()108 Declaration &InlineFunctionInfo::GetCallSite() { return m_call_decl; }
109
GetCallSite() const110 const Declaration &InlineFunctionInfo::GetCallSite() const {
111 return m_call_decl;
112 }
113
GetMangled()114 Mangled &InlineFunctionInfo::GetMangled() { return m_mangled; }
115
GetMangled() const116 const Mangled &InlineFunctionInfo::GetMangled() const { return m_mangled; }
117
MemorySize() const118 size_t InlineFunctionInfo::MemorySize() const {
119 return FunctionInfo::MemorySize() + m_mangled.MemorySize();
120 }
121
122 /// @name Call site related structures
123 /// @{
124
125 CallEdge::~CallEdge() = default;
126
CallEdge(AddrType caller_address_type,lldb::addr_t caller_address,bool is_tail_call,CallSiteParameterArray && parameters)127 CallEdge::CallEdge(AddrType caller_address_type, lldb::addr_t caller_address,
128 bool is_tail_call, CallSiteParameterArray &¶meters)
129 : caller_address(caller_address), caller_address_type(caller_address_type),
130 is_tail_call(is_tail_call), parameters(std::move(parameters)) {}
131
GetLoadAddress(lldb::addr_t unresolved_pc,Function & caller,Target & target)132 lldb::addr_t CallEdge::GetLoadAddress(lldb::addr_t unresolved_pc,
133 Function &caller, Target &target) {
134 Log *log = GetLog(LLDBLog::Step);
135
136 const Address &caller_start_addr = caller.GetAddressRange().GetBaseAddress();
137
138 ModuleSP caller_module_sp = caller_start_addr.GetModule();
139 if (!caller_module_sp) {
140 LLDB_LOG(log, "GetLoadAddress: cannot get Module for caller");
141 return LLDB_INVALID_ADDRESS;
142 }
143
144 SectionList *section_list = caller_module_sp->GetSectionList();
145 if (!section_list) {
146 LLDB_LOG(log, "GetLoadAddress: cannot get SectionList for Module");
147 return LLDB_INVALID_ADDRESS;
148 }
149
150 Address the_addr = Address(unresolved_pc, section_list);
151 lldb::addr_t load_addr = the_addr.GetLoadAddress(&target);
152 return load_addr;
153 }
154
GetReturnPCAddress(Function & caller,Target & target) const155 lldb::addr_t CallEdge::GetReturnPCAddress(Function &caller,
156 Target &target) const {
157 return GetLoadAddress(GetUnresolvedReturnPCAddress(), caller, target);
158 }
159
ParseSymbolFileAndResolve(ModuleList & images)160 void DirectCallEdge::ParseSymbolFileAndResolve(ModuleList &images) {
161 if (resolved)
162 return;
163
164 Log *log = GetLog(LLDBLog::Step);
165 LLDB_LOG(log, "DirectCallEdge: Lazily parsing the call graph for {0}",
166 lazy_callee.symbol_name);
167
168 auto resolve_lazy_callee = [&]() -> Function * {
169 ConstString callee_name{lazy_callee.symbol_name};
170 SymbolContextList sc_list;
171 images.FindFunctionSymbols(callee_name, eFunctionNameTypeAuto, sc_list);
172 size_t num_matches = sc_list.GetSize();
173 if (num_matches == 0 || !sc_list[0].symbol) {
174 LLDB_LOG(log,
175 "DirectCallEdge: Found no symbols for {0}, cannot resolve it",
176 callee_name);
177 return nullptr;
178 }
179 Address callee_addr = sc_list[0].symbol->GetAddress();
180 if (!callee_addr.IsValid()) {
181 LLDB_LOG(log, "DirectCallEdge: Invalid symbol address");
182 return nullptr;
183 }
184 Function *f = callee_addr.CalculateSymbolContextFunction();
185 if (!f) {
186 LLDB_LOG(log, "DirectCallEdge: Could not find complete function");
187 return nullptr;
188 }
189 return f;
190 };
191 lazy_callee.def = resolve_lazy_callee();
192 resolved = true;
193 }
194
DirectCallEdge(const char * symbol_name,AddrType caller_address_type,lldb::addr_t caller_address,bool is_tail_call,CallSiteParameterArray && parameters)195 DirectCallEdge::DirectCallEdge(const char *symbol_name,
196 AddrType caller_address_type,
197 lldb::addr_t caller_address, bool is_tail_call,
198 CallSiteParameterArray &¶meters)
199 : CallEdge(caller_address_type, caller_address, is_tail_call,
200 std::move(parameters)) {
201 lazy_callee.symbol_name = symbol_name;
202 }
203
GetCallee(ModuleList & images,ExecutionContext &)204 Function *DirectCallEdge::GetCallee(ModuleList &images, ExecutionContext &) {
205 ParseSymbolFileAndResolve(images);
206 assert(resolved && "Did not resolve lazy callee");
207 return lazy_callee.def;
208 }
209
IndirectCallEdge(DWARFExpressionList call_target,AddrType caller_address_type,lldb::addr_t caller_address,bool is_tail_call,CallSiteParameterArray && parameters)210 IndirectCallEdge::IndirectCallEdge(DWARFExpressionList call_target,
211 AddrType caller_address_type,
212 lldb::addr_t caller_address,
213 bool is_tail_call,
214 CallSiteParameterArray &¶meters)
215 : CallEdge(caller_address_type, caller_address, is_tail_call,
216 std::move(parameters)),
217 call_target(std::move(call_target)) {}
218
GetCallee(ModuleList & images,ExecutionContext & exe_ctx)219 Function *IndirectCallEdge::GetCallee(ModuleList &images,
220 ExecutionContext &exe_ctx) {
221 Log *log = GetLog(LLDBLog::Step);
222 Status error;
223 llvm::Expected<Value> callee_addr_val = call_target.Evaluate(
224 &exe_ctx, exe_ctx.GetRegisterContext(), LLDB_INVALID_ADDRESS,
225 /*initial_value_ptr=*/nullptr,
226 /*object_address_ptr=*/nullptr);
227 if (!callee_addr_val) {
228 LLDB_LOG_ERROR(log, callee_addr_val.takeError(),
229 "IndirectCallEdge: Could not evaluate expression: {0}");
230 return nullptr;
231 }
232
233 addr_t raw_addr =
234 callee_addr_val->GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
235 if (raw_addr == LLDB_INVALID_ADDRESS) {
236 LLDB_LOG(log, "IndirectCallEdge: Could not extract address from scalar");
237 return nullptr;
238 }
239
240 Address callee_addr;
241 if (!exe_ctx.GetTargetPtr()->ResolveLoadAddress(raw_addr, callee_addr)) {
242 LLDB_LOG(log, "IndirectCallEdge: Could not resolve callee's load address");
243 return nullptr;
244 }
245
246 Function *f = callee_addr.CalculateSymbolContextFunction();
247 if (!f) {
248 LLDB_LOG(log, "IndirectCallEdge: Could not find complete function");
249 return nullptr;
250 }
251
252 return f;
253 }
254
255 /// @}
256
257 //
Function(CompileUnit * comp_unit,lldb::user_id_t func_uid,lldb::user_id_t type_uid,const Mangled & mangled,Type * type,const AddressRange & range)258 Function::Function(CompileUnit *comp_unit, lldb::user_id_t func_uid,
259 lldb::user_id_t type_uid, const Mangled &mangled, Type *type,
260 const AddressRange &range)
261 : UserID(func_uid), m_comp_unit(comp_unit), m_type_uid(type_uid),
262 m_type(type), m_mangled(mangled), m_block(func_uid), m_range(range),
263 m_frame_base(), m_flags(), m_prologue_byte_size(0) {
264 m_block.SetParentScope(this);
265 assert(comp_unit != nullptr);
266 }
267
268 Function::~Function() = default;
269
GetStartLineSourceInfo(FileSpec & source_file,uint32_t & line_no)270 void Function::GetStartLineSourceInfo(FileSpec &source_file,
271 uint32_t &line_no) {
272 line_no = 0;
273 source_file.Clear();
274
275 if (m_comp_unit == nullptr)
276 return;
277
278 // Initialize m_type if it hasn't been initialized already
279 GetType();
280
281 if (m_type != nullptr && m_type->GetDeclaration().GetLine() != 0) {
282 source_file = m_type->GetDeclaration().GetFile();
283 line_no = m_type->GetDeclaration().GetLine();
284 } else {
285 LineTable *line_table = m_comp_unit->GetLineTable();
286 if (line_table == nullptr)
287 return;
288
289 LineEntry line_entry;
290 if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(),
291 line_entry, nullptr)) {
292 line_no = line_entry.line;
293 source_file = line_entry.GetFile();
294 }
295 }
296 }
297
GetEndLineSourceInfo(FileSpec & source_file,uint32_t & line_no)298 void Function::GetEndLineSourceInfo(FileSpec &source_file, uint32_t &line_no) {
299 line_no = 0;
300 source_file.Clear();
301
302 // The -1 is kind of cheesy, but I want to get the last line entry for the
303 // given function, not the first entry of the next.
304 Address scratch_addr(GetAddressRange().GetBaseAddress());
305 scratch_addr.SetOffset(scratch_addr.GetOffset() +
306 GetAddressRange().GetByteSize() - 1);
307
308 LineTable *line_table = m_comp_unit->GetLineTable();
309 if (line_table == nullptr)
310 return;
311
312 LineEntry line_entry;
313 if (line_table->FindLineEntryByAddress(scratch_addr, line_entry, nullptr)) {
314 line_no = line_entry.line;
315 source_file = line_entry.GetFile();
316 }
317 }
318
GetCallEdges()319 llvm::ArrayRef<std::unique_ptr<CallEdge>> Function::GetCallEdges() {
320 std::lock_guard<std::mutex> guard(m_call_edges_lock);
321
322 if (m_call_edges_resolved)
323 return m_call_edges;
324
325 Log *log = GetLog(LLDBLog::Step);
326 LLDB_LOG(log, "GetCallEdges: Attempting to parse call site info for {0}",
327 GetDisplayName());
328
329 m_call_edges_resolved = true;
330
331 // Find the SymbolFile which provided this function's definition.
332 Block &block = GetBlock(/*can_create*/true);
333 SymbolFile *sym_file = block.GetSymbolFile();
334 if (!sym_file)
335 return std::nullopt;
336
337 // Lazily read call site information from the SymbolFile.
338 m_call_edges = sym_file->ParseCallEdgesInFunction(GetID());
339
340 // Sort the call edges to speed up return_pc lookups.
341 llvm::sort(m_call_edges, [](const std::unique_ptr<CallEdge> &LHS,
342 const std::unique_ptr<CallEdge> &RHS) {
343 return LHS->GetSortKey() < RHS->GetSortKey();
344 });
345
346 return m_call_edges;
347 }
348
GetTailCallingEdges()349 llvm::ArrayRef<std::unique_ptr<CallEdge>> Function::GetTailCallingEdges() {
350 // Tail calling edges are sorted at the end of the list. Find them by dropping
351 // all non-tail-calls.
352 return GetCallEdges().drop_until(
353 [](const std::unique_ptr<CallEdge> &edge) { return edge->IsTailCall(); });
354 }
355
GetCallEdgeForReturnAddress(addr_t return_pc,Target & target)356 CallEdge *Function::GetCallEdgeForReturnAddress(addr_t return_pc,
357 Target &target) {
358 auto edges = GetCallEdges();
359 auto edge_it =
360 llvm::partition_point(edges, [&](const std::unique_ptr<CallEdge> &edge) {
361 return std::make_pair(edge->IsTailCall(),
362 edge->GetReturnPCAddress(*this, target)) <
363 std::make_pair(false, return_pc);
364 });
365 if (edge_it == edges.end() ||
366 edge_it->get()->GetReturnPCAddress(*this, target) != return_pc)
367 return nullptr;
368 return edge_it->get();
369 }
370
GetBlock(bool can_create)371 Block &Function::GetBlock(bool can_create) {
372 if (!m_block.BlockInfoHasBeenParsed() && can_create) {
373 ModuleSP module_sp = CalculateSymbolContextModule();
374 if (module_sp) {
375 module_sp->GetSymbolFile()->ParseBlocksRecursive(*this);
376 } else {
377 Debugger::ReportError(llvm::formatv(
378 "unable to find module shared pointer for function '{0}' in {1}",
379 GetName().GetCString(), m_comp_unit->GetPrimaryFile().GetPath()));
380 }
381 m_block.SetBlockInfoHasBeenParsed(true, true);
382 }
383 return m_block;
384 }
385
GetCompileUnit()386 CompileUnit *Function::GetCompileUnit() { return m_comp_unit; }
387
GetCompileUnit() const388 const CompileUnit *Function::GetCompileUnit() const { return m_comp_unit; }
389
GetDescription(Stream * s,lldb::DescriptionLevel level,Target * target)390 void Function::GetDescription(Stream *s, lldb::DescriptionLevel level,
391 Target *target) {
392 ConstString name = GetName();
393 ConstString mangled = m_mangled.GetMangledName();
394
395 *s << "id = " << (const UserID &)*this;
396 if (name)
397 s->AsRawOstream() << ", name = \"" << name << '"';
398 if (mangled)
399 s->AsRawOstream() << ", mangled = \"" << mangled << '"';
400 if (level == eDescriptionLevelVerbose) {
401 *s << ", decl_context = {";
402 auto decl_context = GetCompilerContext();
403 // Drop the function itself from the context chain.
404 if (decl_context.size())
405 decl_context.pop_back();
406 llvm::interleaveComma(decl_context, *s, [&](auto &ctx) { ctx.Dump(*s); });
407 *s << "}";
408 }
409 *s << ", range = ";
410 Address::DumpStyle fallback_style;
411 if (level == eDescriptionLevelVerbose)
412 fallback_style = Address::DumpStyleModuleWithFileAddress;
413 else
414 fallback_style = Address::DumpStyleFileAddress;
415 GetAddressRange().Dump(s, target, Address::DumpStyleLoadAddress,
416 fallback_style);
417 }
418
Dump(Stream * s,bool show_context) const419 void Function::Dump(Stream *s, bool show_context) const {
420 s->Printf("%p: ", static_cast<const void *>(this));
421 s->Indent();
422 *s << "Function" << static_cast<const UserID &>(*this);
423
424 m_mangled.Dump(s);
425
426 if (m_type)
427 s->Printf(", type = %p", static_cast<void *>(m_type));
428 else if (m_type_uid != LLDB_INVALID_UID)
429 s->Printf(", type_uid = 0x%8.8" PRIx64, m_type_uid);
430
431 s->EOL();
432 // Dump the root object
433 if (m_block.BlockInfoHasBeenParsed())
434 m_block.Dump(s, m_range.GetBaseAddress().GetFileAddress(), INT_MAX,
435 show_context);
436 }
437
CalculateSymbolContext(SymbolContext * sc)438 void Function::CalculateSymbolContext(SymbolContext *sc) {
439 sc->function = this;
440 m_comp_unit->CalculateSymbolContext(sc);
441 }
442
CalculateSymbolContextModule()443 ModuleSP Function::CalculateSymbolContextModule() {
444 SectionSP section_sp(m_range.GetBaseAddress().GetSection());
445 if (section_sp)
446 return section_sp->GetModule();
447
448 return this->GetCompileUnit()->GetModule();
449 }
450
CalculateSymbolContextCompileUnit()451 CompileUnit *Function::CalculateSymbolContextCompileUnit() {
452 return this->GetCompileUnit();
453 }
454
CalculateSymbolContextFunction()455 Function *Function::CalculateSymbolContextFunction() { return this; }
456
GetInstructions(const ExecutionContext & exe_ctx,const char * flavor,bool prefer_file_cache)457 lldb::DisassemblerSP Function::GetInstructions(const ExecutionContext &exe_ctx,
458 const char *flavor,
459 bool prefer_file_cache) {
460 ModuleSP module_sp(GetAddressRange().GetBaseAddress().GetModule());
461 if (module_sp && exe_ctx.HasTargetScope()) {
462 return Disassembler::DisassembleRange(module_sp->GetArchitecture(), nullptr,
463 flavor, exe_ctx.GetTargetRef(),
464 GetAddressRange(), !prefer_file_cache);
465 }
466 return lldb::DisassemblerSP();
467 }
468
GetDisassembly(const ExecutionContext & exe_ctx,const char * flavor,Stream & strm,bool prefer_file_cache)469 bool Function::GetDisassembly(const ExecutionContext &exe_ctx,
470 const char *flavor, Stream &strm,
471 bool prefer_file_cache) {
472 lldb::DisassemblerSP disassembler_sp =
473 GetInstructions(exe_ctx, flavor, prefer_file_cache);
474 if (disassembler_sp) {
475 const bool show_address = true;
476 const bool show_bytes = false;
477 const bool show_control_flow_kind = false;
478 disassembler_sp->GetInstructionList().Dump(
479 &strm, show_address, show_bytes, show_control_flow_kind, &exe_ctx);
480 return true;
481 }
482 return false;
483 }
484
485 // Symbol *
486 // Function::CalculateSymbolContextSymbol ()
487 //{
488 // return // TODO: find the symbol for the function???
489 //}
490
DumpSymbolContext(Stream * s)491 void Function::DumpSymbolContext(Stream *s) {
492 m_comp_unit->DumpSymbolContext(s);
493 s->Printf(", Function{0x%8.8" PRIx64 "}", GetID());
494 }
495
MemorySize() const496 size_t Function::MemorySize() const {
497 size_t mem_size = sizeof(Function) + m_block.MemorySize();
498 return mem_size;
499 }
500
GetIsOptimized()501 bool Function::GetIsOptimized() {
502 bool result = false;
503
504 // Currently optimization is only indicted by the vendor extension
505 // DW_AT_APPLE_optimized which is set on a compile unit level.
506 if (m_comp_unit) {
507 result = m_comp_unit->GetIsOptimized();
508 }
509 return result;
510 }
511
IsTopLevelFunction()512 bool Function::IsTopLevelFunction() {
513 bool result = false;
514
515 if (Language *language = Language::FindPlugin(GetLanguage()))
516 result = language->IsTopLevelFunction(*this);
517
518 return result;
519 }
520
GetDisplayName() const521 ConstString Function::GetDisplayName() const {
522 return m_mangled.GetDisplayDemangledName();
523 }
524
GetDeclContext()525 CompilerDeclContext Function::GetDeclContext() {
526 if (ModuleSP module_sp = CalculateSymbolContextModule())
527 if (SymbolFile *sym_file = module_sp->GetSymbolFile())
528 return sym_file->GetDeclContextForUID(GetID());
529 return {};
530 }
531
GetCompilerContext()532 std::vector<CompilerContext> Function::GetCompilerContext() {
533 if (ModuleSP module_sp = CalculateSymbolContextModule())
534 if (SymbolFile *sym_file = module_sp->GetSymbolFile())
535 return sym_file->GetCompilerContextForUID(GetID());
536 return {};
537 }
538
GetType()539 Type *Function::GetType() {
540 if (m_type == nullptr) {
541 SymbolContext sc;
542
543 CalculateSymbolContext(&sc);
544
545 if (!sc.module_sp)
546 return nullptr;
547
548 SymbolFile *sym_file = sc.module_sp->GetSymbolFile();
549
550 if (sym_file == nullptr)
551 return nullptr;
552
553 m_type = sym_file->ResolveTypeUID(m_type_uid);
554 }
555 return m_type;
556 }
557
GetType() const558 const Type *Function::GetType() const { return m_type; }
559
GetCompilerType()560 CompilerType Function::GetCompilerType() {
561 Type *function_type = GetType();
562 if (function_type)
563 return function_type->GetFullCompilerType();
564 return CompilerType();
565 }
566
GetPrologueByteSize()567 uint32_t Function::GetPrologueByteSize() {
568 if (m_prologue_byte_size == 0 &&
569 m_flags.IsClear(flagsCalculatedPrologueSize)) {
570 m_flags.Set(flagsCalculatedPrologueSize);
571 LineTable *line_table = m_comp_unit->GetLineTable();
572 uint32_t prologue_end_line_idx = 0;
573
574 if (line_table) {
575 LineEntry first_line_entry;
576 uint32_t first_line_entry_idx = UINT32_MAX;
577 if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(),
578 first_line_entry,
579 &first_line_entry_idx)) {
580 // Make sure the first line entry isn't already the end of the prologue
581 addr_t prologue_end_file_addr = LLDB_INVALID_ADDRESS;
582 addr_t line_zero_end_file_addr = LLDB_INVALID_ADDRESS;
583
584 if (first_line_entry.is_prologue_end) {
585 prologue_end_file_addr =
586 first_line_entry.range.GetBaseAddress().GetFileAddress();
587 prologue_end_line_idx = first_line_entry_idx;
588 } else {
589 // Check the first few instructions and look for one that has
590 // is_prologue_end set to true.
591 const uint32_t last_line_entry_idx = first_line_entry_idx + 6;
592 for (uint32_t idx = first_line_entry_idx + 1;
593 idx < last_line_entry_idx; ++idx) {
594 LineEntry line_entry;
595 if (line_table->GetLineEntryAtIndex(idx, line_entry)) {
596 if (line_entry.is_prologue_end) {
597 prologue_end_file_addr =
598 line_entry.range.GetBaseAddress().GetFileAddress();
599 prologue_end_line_idx = idx;
600 break;
601 }
602 }
603 }
604 }
605
606 // If we didn't find the end of the prologue in the line tables, then
607 // just use the end address of the first line table entry
608 if (prologue_end_file_addr == LLDB_INVALID_ADDRESS) {
609 // Check the first few instructions and look for one that has a line
610 // number that's different than the first entry.
611 uint32_t last_line_entry_idx = first_line_entry_idx + 6;
612 for (uint32_t idx = first_line_entry_idx + 1;
613 idx < last_line_entry_idx; ++idx) {
614 LineEntry line_entry;
615 if (line_table->GetLineEntryAtIndex(idx, line_entry)) {
616 if (line_entry.line != first_line_entry.line) {
617 prologue_end_file_addr =
618 line_entry.range.GetBaseAddress().GetFileAddress();
619 prologue_end_line_idx = idx;
620 break;
621 }
622 }
623 }
624
625 if (prologue_end_file_addr == LLDB_INVALID_ADDRESS) {
626 prologue_end_file_addr =
627 first_line_entry.range.GetBaseAddress().GetFileAddress() +
628 first_line_entry.range.GetByteSize();
629 prologue_end_line_idx = first_line_entry_idx;
630 }
631 }
632
633 const addr_t func_start_file_addr =
634 m_range.GetBaseAddress().GetFileAddress();
635 const addr_t func_end_file_addr =
636 func_start_file_addr + m_range.GetByteSize();
637
638 // Now calculate the offset to pass the subsequent line 0 entries.
639 uint32_t first_non_zero_line = prologue_end_line_idx;
640 while (true) {
641 LineEntry line_entry;
642 if (line_table->GetLineEntryAtIndex(first_non_zero_line,
643 line_entry)) {
644 if (line_entry.line != 0)
645 break;
646 }
647 if (line_entry.range.GetBaseAddress().GetFileAddress() >=
648 func_end_file_addr)
649 break;
650
651 first_non_zero_line++;
652 }
653
654 if (first_non_zero_line > prologue_end_line_idx) {
655 LineEntry first_non_zero_entry;
656 if (line_table->GetLineEntryAtIndex(first_non_zero_line,
657 first_non_zero_entry)) {
658 line_zero_end_file_addr =
659 first_non_zero_entry.range.GetBaseAddress().GetFileAddress();
660 }
661 }
662
663 // Verify that this prologue end file address in the function's address
664 // range just to be sure
665 if (func_start_file_addr < prologue_end_file_addr &&
666 prologue_end_file_addr < func_end_file_addr) {
667 m_prologue_byte_size = prologue_end_file_addr - func_start_file_addr;
668 }
669
670 if (prologue_end_file_addr < line_zero_end_file_addr &&
671 line_zero_end_file_addr < func_end_file_addr) {
672 m_prologue_byte_size +=
673 line_zero_end_file_addr - prologue_end_file_addr;
674 }
675 }
676 }
677 }
678
679 return m_prologue_byte_size;
680 }
681
GetLanguage() const682 lldb::LanguageType Function::GetLanguage() const {
683 lldb::LanguageType lang = m_mangled.GuessLanguage();
684 if (lang != lldb::eLanguageTypeUnknown)
685 return lang;
686
687 if (m_comp_unit)
688 return m_comp_unit->GetLanguage();
689
690 return lldb::eLanguageTypeUnknown;
691 }
692
GetName() const693 ConstString Function::GetName() const {
694 return m_mangled.GetName();
695 }
696
GetNameNoArguments() const697 ConstString Function::GetNameNoArguments() const {
698 return m_mangled.GetName(Mangled::ePreferDemangledWithoutArguments);
699 }
700