Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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" |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 11 | #include "lldb/Core/Disassembler.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Module.h" |
| 13 | #include "lldb/Core/Section.h" |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 14 | #include "lldb/Host/Host.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | #include "lldb/Symbol/CompileUnit.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 16 | #include "lldb/Symbol/CompilerType.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Symbol/LineTable.h" |
Sean Callanan | 72e4940 | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 18 | #include "lldb/Symbol/SymbolFile.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #include "lldb/Symbol/SymbolVendor.h" |
Enrico Granata | 6754e04 | 2015-09-30 23:12:22 +0000 | [diff] [blame] | 20 | #include "lldb/Target/Language.h" |
Sean Callanan | cc427fa | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Casting.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | |
Greg Clayton | c980066 | 2010-09-10 01:30:46 +0000 | [diff] [blame] | 23 | using namespace lldb; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | using namespace lldb_private; |
| 25 | |
| 26 | //---------------------------------------------------------------------- |
| 27 | // Basic function information is contained in the FunctionInfo class. |
| 28 | // It is designed to contain the name, linkage name, and declaration |
| 29 | // location. |
| 30 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 31 | FunctionInfo::FunctionInfo(const char *name, const Declaration *decl_ptr) |
| 32 | : m_name(name), m_declaration(decl_ptr) {} |
| 33 | |
| 34 | FunctionInfo::FunctionInfo(const ConstString &name, const Declaration *decl_ptr) |
| 35 | : m_name(name), m_declaration(decl_ptr) {} |
| 36 | |
| 37 | FunctionInfo::~FunctionInfo() {} |
| 38 | |
| 39 | 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); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 50 | return Declaration::Compare(a.m_declaration, b.m_declaration); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | Declaration &FunctionInfo::GetDeclaration() { return m_declaration; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | const Declaration &FunctionInfo::GetDeclaration() const { |
| 56 | return m_declaration; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | ConstString FunctionInfo::GetName() const { return m_name; } |
| 60 | |
| 61 | size_t FunctionInfo::MemorySize() const { |
| 62 | return m_name.MemorySize() + m_declaration.MemorySize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 77 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 78 | InlineFunctionInfo::~InlineFunctionInfo() {} |
| 79 | |
| 80 | int InlineFunctionInfo::Compare(const InlineFunctionInfo &a, |
| 81 | const InlineFunctionInfo &b) { |
| 82 | |
| 83 | int result = FunctionInfo::Compare(a, b); |
| 84 | if (result) |
| 85 | return result; |
| 86 | // only compare the mangled names if both have them |
| 87 | return Mangled::Compare(a.m_mangled, a.m_mangled); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | void InlineFunctionInfo::Dump(Stream *s, bool show_fullpaths) const { |
| 91 | FunctionInfo::Dump(s, show_fullpaths); |
| 92 | if (m_mangled) |
| 93 | m_mangled.Dump(s); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | void InlineFunctionInfo::DumpStopContext(Stream *s, |
| 97 | LanguageType language) const { |
| 98 | // s->Indent("[inlined] "); |
| 99 | s->Indent(); |
| 100 | if (m_mangled) |
| 101 | s->PutCString(m_mangled.GetName(language).AsCString()); |
| 102 | else |
| 103 | s->PutCString(m_name.AsCString()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | ConstString InlineFunctionInfo::GetName(LanguageType language) const { |
| 107 | if (m_mangled) |
| 108 | return m_mangled.GetName(language); |
| 109 | return m_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | ConstString InlineFunctionInfo::GetDisplayName(LanguageType language) const { |
| 113 | if (m_mangled) |
| 114 | return m_mangled.GetDisplayDemangledName(language); |
| 115 | return m_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 118 | Declaration &InlineFunctionInfo::GetCallSite() { return m_call_decl; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | const Declaration &InlineFunctionInfo::GetCallSite() const { |
| 121 | return m_call_decl; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | Mangled &InlineFunctionInfo::GetMangled() { return m_mangled; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 125 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 126 | const Mangled &InlineFunctionInfo::GetMangled() const { return m_mangled; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 127 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 128 | size_t InlineFunctionInfo::MemorySize() const { |
| 129 | return FunctionInfo::MemorySize() + m_mangled.MemorySize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | //---------------------------------------------------------------------- |
| 133 | // |
| 134 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 156 | Function::~Function() {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 157 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 158 | void Function::GetStartLineSourceInfo(FileSpec &source_file, |
| 159 | uint32_t &line_no) { |
| 160 | line_no = 0; |
| 161 | source_file.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 162 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 163 | if (m_comp_unit == nullptr) |
| 164 | return; |
Jim Ingham | 9976033 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 165 | |
Jason Molenda | f81f15a | 2016-09-08 02:26:58 +0000 | [diff] [blame] | 166 | // Initialize m_type if it hasn't been initialized already |
| 167 | GetType(); |
| 168 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 169 | if (m_type != nullptr && m_type->GetDeclaration().GetLine() != 0) { |
| 170 | source_file = m_type->GetDeclaration().GetFile(); |
| 171 | line_no = m_type->GetDeclaration().GetLine(); |
| 172 | } else { |
Jim Ingham | 9976033 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 173 | LineTable *line_table = m_comp_unit->GetLineTable(); |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 174 | if (line_table == nullptr) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 175 | return; |
| 176 | |
Jim Ingham | 9976033 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 177 | LineEntry line_entry; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 178 | if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(), |
| 179 | line_entry, nullptr)) { |
| 180 | line_no = line_entry.line; |
| 181 | source_file = line_entry.file; |
Jim Ingham | 9976033 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 182 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 183 | } |
Jim Ingham | 9976033 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 186 | void Function::GetEndLineSourceInfo(FileSpec &source_file, uint32_t &line_no) { |
| 187 | line_no = 0; |
| 188 | source_file.Clear(); |
| 189 | |
| 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); |
| 196 | |
| 197 | LineTable *line_table = m_comp_unit->GetLineTable(); |
| 198 | if (line_table == nullptr) |
| 199 | return; |
| 200 | |
| 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; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | Block &Function::GetBlock(bool can_create) { |
| 209 | if (!m_block.BlockInfoHasBeenParsed() && can_create) { |
| 210 | SymbolContext sc; |
| 211 | CalculateSymbolContext(&sc); |
| 212 | if (sc.module_sp) { |
| 213 | sc.module_sp->GetSymbolVendor()->ParseFunctionBlocks(sc); |
| 214 | } else { |
| 215 | Host::SystemLog(Host::eSystemLogError, "error: unable to find module " |
| 216 | "shared pointer for function '%s' " |
| 217 | "in %s\n", |
| 218 | GetName().GetCString(), m_comp_unit->GetPath().c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 219 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 220 | m_block.SetBlockInfoHasBeenParsed(true, true); |
| 221 | } |
| 222 | return m_block; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 225 | CompileUnit *Function::GetCompileUnit() { return m_comp_unit; } |
| 226 | |
| 227 | const CompileUnit *Function::GetCompileUnit() const { return m_comp_unit; } |
| 228 | |
| 229 | void Function::GetDescription(Stream *s, lldb::DescriptionLevel level, |
| 230 | Target *target) { |
| 231 | Type *func_type = GetType(); |
| 232 | const char *name = func_type ? func_type->GetName().AsCString() : "<unknown>"; |
| 233 | |
| 234 | *s << "id = " << (const UserID &)*this << ", name = \"" << name |
| 235 | << "\", range = "; |
| 236 | |
| 237 | Address::DumpStyle fallback_style; |
| 238 | if (level == eDescriptionLevelVerbose) |
| 239 | fallback_style = Address::DumpStyleModuleWithFileAddress; |
| 240 | else |
| 241 | fallback_style = Address::DumpStyleFileAddress; |
| 242 | GetAddressRange().Dump(s, target, Address::DumpStyleLoadAddress, |
| 243 | fallback_style); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 246 | void Function::Dump(Stream *s, bool show_context) const { |
| 247 | s->Printf("%p: ", static_cast<const void *>(this)); |
| 248 | s->Indent(); |
| 249 | *s << "Function" << static_cast<const UserID &>(*this); |
| 250 | |
| 251 | m_mangled.Dump(s); |
| 252 | |
| 253 | if (m_type) |
| 254 | s->Printf(", type = %p", static_cast<void *>(m_type)); |
| 255 | else if (m_type_uid != LLDB_INVALID_UID) |
| 256 | s->Printf(", type_uid = 0x%8.8" PRIx64, m_type_uid); |
| 257 | |
| 258 | s->EOL(); |
| 259 | // Dump the root object |
| 260 | if (m_block.BlockInfoHasBeenParsed()) |
| 261 | m_block.Dump(s, m_range.GetBaseAddress().GetFileAddress(), INT_MAX, |
| 262 | show_context); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 265 | void Function::CalculateSymbolContext(SymbolContext *sc) { |
| 266 | sc->function = this; |
| 267 | m_comp_unit->CalculateSymbolContext(sc); |
Greg Clayton | 0c5cd90 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 270 | ModuleSP Function::CalculateSymbolContextModule() { |
| 271 | SectionSP section_sp(m_range.GetBaseAddress().GetSection()); |
| 272 | if (section_sp) |
| 273 | return section_sp->GetModule(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 274 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 275 | return this->GetCompileUnit()->GetModule(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 278 | CompileUnit *Function::CalculateSymbolContextCompileUnit() { |
| 279 | return this->GetCompileUnit(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 282 | Function *Function::CalculateSymbolContextFunction() { return this; } |
| 283 | |
| 284 | lldb::DisassemblerSP Function::GetInstructions(const ExecutionContext &exe_ctx, |
| 285 | const char *flavor, |
| 286 | bool prefer_file_cache) { |
| 287 | ModuleSP module_sp(GetAddressRange().GetBaseAddress().GetModule()); |
| 288 | if (module_sp) { |
| 289 | const bool prefer_file_cache = false; |
| 290 | return Disassembler::DisassembleRange(module_sp->GetArchitecture(), nullptr, |
| 291 | flavor, exe_ctx, GetAddressRange(), |
| 292 | prefer_file_cache); |
| 293 | } |
| 294 | return lldb::DisassemblerSP(); |
Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 297 | bool Function::GetDisassembly(const ExecutionContext &exe_ctx, |
| 298 | const char *flavor, bool prefer_file_cache, |
| 299 | Stream &strm) { |
| 300 | lldb::DisassemblerSP disassembler_sp = |
| 301 | GetInstructions(exe_ctx, flavor, prefer_file_cache); |
| 302 | if (disassembler_sp) { |
| 303 | const bool show_address = true; |
| 304 | const bool show_bytes = false; |
| 305 | disassembler_sp->GetInstructionList().Dump(&strm, show_address, show_bytes, |
| 306 | &exe_ctx); |
| 307 | return true; |
| 308 | } |
| 309 | return false; |
Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 312 | // Symbol * |
| 313 | // Function::CalculateSymbolContextSymbol () |
Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 314 | //{ |
| 315 | // return // TODO: find the symbol for the function??? |
| 316 | //} |
| 317 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 318 | void Function::DumpSymbolContext(Stream *s) { |
| 319 | m_comp_unit->DumpSymbolContext(s); |
| 320 | s->Printf(", Function{0x%8.8" PRIx64 "}", GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 323 | size_t Function::MemorySize() const { |
| 324 | size_t mem_size = sizeof(Function) + m_block.MemorySize(); |
| 325 | return mem_size; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 328 | bool Function::GetIsOptimized() { |
| 329 | bool result = false; |
Jason Molenda | 6ab659a | 2015-07-29 00:42:47 +0000 | [diff] [blame] | 330 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 331 | // Currently optimization is only indicted by the |
| 332 | // vendor extension DW_AT_APPLE_optimized which |
| 333 | // is set on a compile unit level. |
| 334 | if (m_comp_unit) { |
| 335 | result = m_comp_unit->GetIsOptimized(); |
| 336 | } |
| 337 | return result; |
| 338 | } |
| 339 | |
| 340 | bool Function::IsTopLevelFunction() { |
| 341 | bool result = false; |
| 342 | |
| 343 | if (Language *language = Language::FindPlugin(GetLanguage())) |
| 344 | result = language->IsTopLevelFunction(*this); |
| 345 | |
| 346 | return result; |
| 347 | } |
| 348 | |
| 349 | ConstString Function::GetDisplayName() const { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 350 | return m_mangled.GetDisplayDemangledName(GetLanguage()); |
| 351 | } |
| 352 | |
| 353 | CompilerDeclContext Function::GetDeclContext() { |
| 354 | ModuleSP module_sp = CalculateSymbolContextModule(); |
| 355 | |
| 356 | if (module_sp) { |
| 357 | SymbolVendor *sym_vendor = module_sp->GetSymbolVendor(); |
| 358 | |
| 359 | if (sym_vendor) { |
| 360 | SymbolFile *sym_file = sym_vendor->GetSymbolFile(); |
| 361 | |
| 362 | if (sym_file) |
| 363 | return sym_file->GetDeclContextForUID(GetID()); |
Jason Molenda | 6ab659a | 2015-07-29 00:42:47 +0000 | [diff] [blame] | 364 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 365 | } |
| 366 | return CompilerDeclContext(); |
Jason Molenda | 6ab659a | 2015-07-29 00:42:47 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 369 | Type *Function::GetType() { |
| 370 | if (m_type == nullptr) { |
| 371 | SymbolContext sc; |
| 372 | |
| 373 | CalculateSymbolContext(&sc); |
| 374 | |
| 375 | if (!sc.module_sp) |
| 376 | return nullptr; |
| 377 | |
| 378 | SymbolVendor *sym_vendor = sc.module_sp->GetSymbolVendor(); |
| 379 | |
| 380 | if (sym_vendor == nullptr) |
| 381 | return nullptr; |
| 382 | |
| 383 | SymbolFile *sym_file = sym_vendor->GetSymbolFile(); |
| 384 | |
| 385 | if (sym_file == nullptr) |
| 386 | return nullptr; |
| 387 | |
| 388 | m_type = sym_file->ResolveTypeUID(m_type_uid); |
| 389 | } |
| 390 | return m_type; |
Enrico Granata | 6754e04 | 2015-09-30 23:12:22 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 393 | const Type *Function::GetType() const { return m_type; } |
| 394 | |
| 395 | CompilerType Function::GetCompilerType() { |
| 396 | Type *function_type = GetType(); |
| 397 | if (function_type) |
| 398 | return function_type->GetFullCompilerType(); |
| 399 | return CompilerType(); |
Enrico Granata | c1f705c | 2015-07-06 18:28:46 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 402 | uint32_t Function::GetPrologueByteSize() { |
| 403 | if (m_prologue_byte_size == 0 && |
| 404 | m_flags.IsClear(flagsCalculatedPrologueSize)) { |
| 405 | m_flags.Set(flagsCalculatedPrologueSize); |
| 406 | LineTable *line_table = m_comp_unit->GetLineTable(); |
| 407 | uint32_t prologue_end_line_idx = 0; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 408 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 409 | if (line_table) { |
| 410 | LineEntry first_line_entry; |
| 411 | uint32_t first_line_entry_idx = UINT32_MAX; |
| 412 | if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(), |
| 413 | first_line_entry, |
| 414 | &first_line_entry_idx)) { |
| 415 | // Make sure the first line entry isn't already the end of the prologue |
| 416 | addr_t prologue_end_file_addr = LLDB_INVALID_ADDRESS; |
| 417 | addr_t line_zero_end_file_addr = LLDB_INVALID_ADDRESS; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 418 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 419 | if (first_line_entry.is_prologue_end) { |
| 420 | prologue_end_file_addr = |
| 421 | first_line_entry.range.GetBaseAddress().GetFileAddress(); |
| 422 | prologue_end_line_idx = first_line_entry_idx; |
| 423 | } else { |
| 424 | // Check the first few instructions and look for one that has |
| 425 | // is_prologue_end set to true. |
| 426 | const uint32_t last_line_entry_idx = first_line_entry_idx + 6; |
| 427 | for (uint32_t idx = first_line_entry_idx + 1; |
| 428 | idx < last_line_entry_idx; ++idx) { |
| 429 | LineEntry line_entry; |
| 430 | if (line_table->GetLineEntryAtIndex(idx, line_entry)) { |
| 431 | if (line_entry.is_prologue_end) { |
| 432 | prologue_end_file_addr = |
| 433 | line_entry.range.GetBaseAddress().GetFileAddress(); |
| 434 | prologue_end_line_idx = idx; |
| 435 | break; |
| 436 | } |
Greg Clayton | c3b8499 | 2010-11-11 20:13:30 +0000 | [diff] [blame] | 437 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 438 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 439 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 440 | |
| 441 | // If we didn't find the end of the prologue in the line tables, |
| 442 | // then just use the end address of the first line table entry |
| 443 | if (prologue_end_file_addr == LLDB_INVALID_ADDRESS) { |
| 444 | // Check the first few instructions and look for one that has |
| 445 | // a line number that's different than the first entry. |
| 446 | uint32_t last_line_entry_idx = first_line_entry_idx + 6; |
| 447 | for (uint32_t idx = first_line_entry_idx + 1; |
| 448 | idx < last_line_entry_idx; ++idx) { |
| 449 | LineEntry line_entry; |
| 450 | if (line_table->GetLineEntryAtIndex(idx, line_entry)) { |
| 451 | if (line_entry.line != first_line_entry.line) { |
| 452 | prologue_end_file_addr = |
| 453 | line_entry.range.GetBaseAddress().GetFileAddress(); |
| 454 | prologue_end_line_idx = idx; |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | if (prologue_end_file_addr == LLDB_INVALID_ADDRESS) { |
| 461 | prologue_end_file_addr = |
| 462 | first_line_entry.range.GetBaseAddress().GetFileAddress() + |
| 463 | first_line_entry.range.GetByteSize(); |
| 464 | prologue_end_line_idx = first_line_entry_idx; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | const addr_t func_start_file_addr = |
| 469 | m_range.GetBaseAddress().GetFileAddress(); |
| 470 | const addr_t func_end_file_addr = |
| 471 | func_start_file_addr + m_range.GetByteSize(); |
| 472 | |
| 473 | // Now calculate the offset to pass the subsequent line 0 entries. |
| 474 | uint32_t first_non_zero_line = prologue_end_line_idx; |
| 475 | while (1) { |
| 476 | LineEntry line_entry; |
| 477 | if (line_table->GetLineEntryAtIndex(first_non_zero_line, |
| 478 | line_entry)) { |
| 479 | if (line_entry.line != 0) |
| 480 | break; |
| 481 | } |
| 482 | if (line_entry.range.GetBaseAddress().GetFileAddress() >= |
| 483 | func_end_file_addr) |
| 484 | break; |
| 485 | |
| 486 | first_non_zero_line++; |
| 487 | } |
| 488 | |
| 489 | if (first_non_zero_line > prologue_end_line_idx) { |
| 490 | LineEntry first_non_zero_entry; |
| 491 | if (line_table->GetLineEntryAtIndex(first_non_zero_line, |
| 492 | first_non_zero_entry)) { |
| 493 | line_zero_end_file_addr = |
| 494 | first_non_zero_entry.range.GetBaseAddress().GetFileAddress(); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | // Verify that this prologue end file address in the function's |
| 499 | // address range just to be sure |
| 500 | if (func_start_file_addr < prologue_end_file_addr && |
| 501 | prologue_end_file_addr < func_end_file_addr) { |
| 502 | m_prologue_byte_size = prologue_end_file_addr - func_start_file_addr; |
| 503 | } |
| 504 | |
| 505 | if (prologue_end_file_addr < line_zero_end_file_addr && |
| 506 | line_zero_end_file_addr < func_end_file_addr) { |
| 507 | m_prologue_byte_size += |
| 508 | line_zero_end_file_addr - prologue_end_file_addr; |
| 509 | } |
| 510 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 511 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | return m_prologue_byte_size; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 517 | lldb::LanguageType Function::GetLanguage() const { |
| 518 | if (m_comp_unit) |
| 519 | return m_comp_unit->GetLanguage(); |
| 520 | else |
| 521 | return lldb::eLanguageTypeUnknown; |
Greg Clayton | ddaf6a7 | 2015-07-08 22:32:23 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 524 | ConstString Function::GetName() const { |
| 525 | LanguageType language = lldb::eLanguageTypeUnknown; |
| 526 | if (m_comp_unit) |
| 527 | language = m_comp_unit->GetLanguage(); |
| 528 | return m_mangled.GetName(language); |
Greg Clayton | ddaf6a7 | 2015-07-08 22:32:23 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 531 | ConstString Function::GetNameNoArguments() const { |
| 532 | LanguageType language = lldb::eLanguageTypeUnknown; |
| 533 | if (m_comp_unit) |
| 534 | language = m_comp_unit->GetLanguage(); |
| 535 | return m_mangled.GetName(language, Mangled::ePreferDemangledWithoutArguments); |
Greg Clayton | ddaf6a7 | 2015-07-08 22:32:23 +0000 | [diff] [blame] | 536 | } |