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" |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 15 | #include "lldb/Symbol/CompilerType.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include "lldb/Symbol/CompileUnit.h" |
| 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" |
Sean Callanan | cc427fa | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Casting.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | |
Greg Clayton | c980066 | 2010-09-10 01:30:46 +0000 | [diff] [blame] | 22 | using namespace lldb; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | //---------------------------------------------------------------------- |
| 30 | FunctionInfo::FunctionInfo (const char *name, const Declaration *decl_ptr) : |
| 31 | m_name(name), |
| 32 | m_declaration(decl_ptr) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | |
| 37 | FunctionInfo::FunctionInfo (const ConstString& name, const Declaration *decl_ptr) : |
| 38 | m_name(name), |
| 39 | m_declaration(decl_ptr) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | |
| 44 | FunctionInfo::~FunctionInfo() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | void |
Greg Clayton | 6dbd398 | 2010-09-15 05:51:24 +0000 | [diff] [blame] | 49 | FunctionInfo::Dump(Stream *s, bool show_fullpaths) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 50 | { |
| 51 | if (m_name) |
| 52 | *s << ", name = \"" << m_name << "\""; |
Greg Clayton | 6dbd398 | 2010-09-15 05:51:24 +0000 | [diff] [blame] | 53 | m_declaration.Dump(s, show_fullpaths); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
| 57 | int |
| 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& |
| 69 | FunctionInfo::GetDeclaration() |
| 70 | { |
| 71 | return m_declaration; |
| 72 | } |
| 73 | |
| 74 | const Declaration& |
| 75 | FunctionInfo::GetDeclaration() const |
| 76 | { |
| 77 | return m_declaration; |
| 78 | } |
| 79 | |
Greg Clayton | ddaf6a7 | 2015-07-08 22:32:23 +0000 | [diff] [blame] | 80 | ConstString |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | FunctionInfo::GetName() const |
| 82 | { |
| 83 | return m_name; |
| 84 | } |
| 85 | |
| 86 | size_t |
| 87 | FunctionInfo::MemorySize() const |
| 88 | { |
| 89 | return m_name.MemorySize() + m_declaration.MemorySize(); |
| 90 | } |
| 91 | |
| 92 | |
| 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), |
Greg Clayton | 037520e | 2012-07-18 23:18:10 +0000 | [diff] [blame] | 101 | m_mangled(ConstString(mangled), true), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 102 | m_call_decl (call_decl_ptr) |
| 103 | { |
| 104 | } |
| 105 | |
| 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 | |
| 119 | InlineFunctionInfo::~InlineFunctionInfo() |
| 120 | { |
| 121 | } |
| 122 | |
| 123 | int |
| 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 |
Greg Clayton | 6dbd398 | 2010-09-15 05:51:24 +0000 | [diff] [blame] | 135 | InlineFunctionInfo::Dump(Stream *s, bool show_fullpaths) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 136 | { |
Greg Clayton | 6dbd398 | 2010-09-15 05:51:24 +0000 | [diff] [blame] | 137 | FunctionInfo::Dump(s, show_fullpaths); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 138 | if (m_mangled) |
| 139 | m_mangled.Dump(s); |
| 140 | } |
| 141 | |
| 142 | void |
Greg Clayton | ddaf6a7 | 2015-07-08 22:32:23 +0000 | [diff] [blame] | 143 | InlineFunctionInfo::DumpStopContext (Stream *s, LanguageType language) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 144 | { |
| 145 | // s->Indent("[inlined] "); |
| 146 | s->Indent(); |
| 147 | if (m_mangled) |
Greg Clayton | ddaf6a7 | 2015-07-08 22:32:23 +0000 | [diff] [blame] | 148 | s->PutCString (m_mangled.GetName(language).AsCString()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 149 | else |
| 150 | s->PutCString (m_name.AsCString()); |
| 151 | } |
| 152 | |
Greg Clayton | 1b72fcb | 2010-08-24 00:45:41 +0000 | [diff] [blame] | 153 | |
Greg Clayton | ddaf6a7 | 2015-07-08 22:32:23 +0000 | [diff] [blame] | 154 | ConstString |
| 155 | InlineFunctionInfo::GetName (LanguageType language) const |
Greg Clayton | 1b72fcb | 2010-08-24 00:45:41 +0000 | [diff] [blame] | 156 | { |
| 157 | if (m_mangled) |
Greg Clayton | ddaf6a7 | 2015-07-08 22:32:23 +0000 | [diff] [blame] | 158 | return m_mangled.GetName(language); |
Greg Clayton | 1b72fcb | 2010-08-24 00:45:41 +0000 | [diff] [blame] | 159 | return m_name; |
| 160 | } |
| 161 | |
Enrico Granata | c1f705c | 2015-07-06 18:28:46 +0000 | [diff] [blame] | 162 | ConstString |
Greg Clayton | ddaf6a7 | 2015-07-08 22:32:23 +0000 | [diff] [blame] | 163 | InlineFunctionInfo::GetDisplayName (LanguageType language) const |
Enrico Granata | c1f705c | 2015-07-06 18:28:46 +0000 | [diff] [blame] | 164 | { |
| 165 | if (m_mangled) |
Greg Clayton | ddaf6a7 | 2015-07-08 22:32:23 +0000 | [diff] [blame] | 166 | return m_mangled.GetDisplayDemangledName(language); |
Enrico Granata | c1f705c | 2015-07-06 18:28:46 +0000 | [diff] [blame] | 167 | return m_name; |
| 168 | } |
Greg Clayton | 1b72fcb | 2010-08-24 00:45:41 +0000 | [diff] [blame] | 169 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 170 | Declaration & |
| 171 | InlineFunctionInfo::GetCallSite () |
| 172 | { |
| 173 | return m_call_decl; |
| 174 | } |
| 175 | |
| 176 | const Declaration & |
| 177 | InlineFunctionInfo::GetCallSite () const |
| 178 | { |
| 179 | return m_call_decl; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | Mangled& |
| 184 | InlineFunctionInfo::GetMangled() |
| 185 | { |
| 186 | return m_mangled; |
| 187 | } |
| 188 | |
| 189 | const Mangled& |
| 190 | InlineFunctionInfo::GetMangled() const |
| 191 | { |
| 192 | return m_mangled; |
| 193 | } |
| 194 | |
| 195 | size_t |
| 196 | InlineFunctionInfo::MemorySize() const |
| 197 | { |
| 198 | return FunctionInfo::MemorySize() + m_mangled.MemorySize(); |
| 199 | } |
| 200 | |
| 201 | //---------------------------------------------------------------------- |
| 202 | // |
| 203 | //---------------------------------------------------------------------- |
| 204 | Function::Function |
| 205 | ( |
| 206 | CompileUnit *comp_unit, |
| 207 | lldb::user_id_t func_uid, |
| 208 | lldb::user_id_t type_uid, |
| 209 | const Mangled &mangled, |
| 210 | Type * type, |
| 211 | const AddressRange& range |
| 212 | ) : |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 213 | UserID (func_uid), |
| 214 | m_comp_unit (comp_unit), |
| 215 | m_type_uid (type_uid), |
| 216 | m_type (type), |
| 217 | m_mangled (mangled), |
| 218 | m_block (func_uid), |
| 219 | m_range (range), |
Tamas Berghammer | 35d9d2d | 2015-08-25 11:46:06 +0000 | [diff] [blame] | 220 | m_frame_base (nullptr), |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 221 | m_flags (), |
| 222 | m_prologue_byte_size (0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 223 | { |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 224 | m_block.SetParentScope(this); |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 225 | assert(comp_unit != nullptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | Function::Function |
| 229 | ( |
| 230 | CompileUnit *comp_unit, |
| 231 | lldb::user_id_t func_uid, |
| 232 | lldb::user_id_t type_uid, |
| 233 | const char *mangled, |
| 234 | Type *type, |
| 235 | const AddressRange &range |
| 236 | ) : |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 237 | UserID (func_uid), |
| 238 | m_comp_unit (comp_unit), |
| 239 | m_type_uid (type_uid), |
| 240 | m_type (type), |
Greg Clayton | 037520e | 2012-07-18 23:18:10 +0000 | [diff] [blame] | 241 | m_mangled (ConstString(mangled), true), |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 242 | m_block (func_uid), |
| 243 | m_range (range), |
Tamas Berghammer | 35d9d2d | 2015-08-25 11:46:06 +0000 | [diff] [blame] | 244 | m_frame_base (nullptr), |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 245 | m_flags (), |
| 246 | m_prologue_byte_size (0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 247 | { |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 248 | m_block.SetParentScope(this); |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 249 | assert(comp_unit != nullptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | |
| 253 | Function::~Function() |
| 254 | { |
| 255 | } |
| 256 | |
Jim Ingham | 9976033 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 257 | void |
| 258 | Function::GetStartLineSourceInfo (FileSpec &source_file, uint32_t &line_no) |
| 259 | { |
| 260 | line_no = 0; |
| 261 | source_file.Clear(); |
| 262 | |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 263 | if (m_comp_unit == nullptr) |
Jim Ingham | 9976033 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 264 | return; |
| 265 | |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 266 | if (m_type != nullptr && m_type->GetDeclaration().GetLine() != 0) |
Jim Ingham | 9976033 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 267 | { |
| 268 | source_file = m_type->GetDeclaration().GetFile(); |
| 269 | line_no = m_type->GetDeclaration().GetLine(); |
| 270 | } |
| 271 | else |
| 272 | { |
| 273 | LineTable *line_table = m_comp_unit->GetLineTable(); |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 274 | if (line_table == nullptr) |
Jim Ingham | 9976033 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 275 | return; |
| 276 | |
| 277 | LineEntry line_entry; |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 278 | if (line_table->FindLineEntryByAddress (GetAddressRange().GetBaseAddress(), line_entry, nullptr)) |
Jim Ingham | 9976033 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 279 | { |
| 280 | line_no = line_entry.line; |
| 281 | source_file = line_entry.file; |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | void |
| 287 | Function::GetEndLineSourceInfo (FileSpec &source_file, uint32_t &line_no) |
| 288 | { |
| 289 | line_no = 0; |
| 290 | source_file.Clear(); |
| 291 | |
| 292 | // The -1 is kind of cheesy, but I want to get the last line entry for the given function, not the |
| 293 | // first entry of the next. |
| 294 | Address scratch_addr(GetAddressRange().GetBaseAddress()); |
| 295 | scratch_addr.SetOffset (scratch_addr.GetOffset() + GetAddressRange().GetByteSize() - 1); |
| 296 | |
| 297 | LineTable *line_table = m_comp_unit->GetLineTable(); |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 298 | if (line_table == nullptr) |
Jim Ingham | 9976033 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 299 | return; |
| 300 | |
| 301 | LineEntry line_entry; |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 302 | if (line_table->FindLineEntryByAddress (scratch_addr, line_entry, nullptr)) |
Jim Ingham | 9976033 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 303 | { |
| 304 | line_no = line_entry.line; |
| 305 | source_file = line_entry.file; |
| 306 | } |
| 307 | } |
| 308 | |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 309 | Block & |
| 310 | Function::GetBlock (bool can_create) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 311 | { |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 312 | if (!m_block.BlockInfoHasBeenParsed() && can_create) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 313 | { |
| 314 | SymbolContext sc; |
| 315 | CalculateSymbolContext(&sc); |
Greg Clayton | 17cc8b9 | 2011-06-24 03:47:23 +0000 | [diff] [blame] | 316 | if (sc.module_sp) |
| 317 | { |
| 318 | sc.module_sp->GetSymbolVendor()->ParseFunctionBlocks(sc); |
| 319 | } |
| 320 | else |
| 321 | { |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 322 | Host::SystemLog (Host::eSystemLogError, |
Greg Clayton | b5ad4ec | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 323 | "error: unable to find module shared pointer for function '%s' in %s\n", |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 324 | GetName().GetCString(), |
Greg Clayton | b5ad4ec | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 325 | m_comp_unit->GetPath().c_str()); |
Greg Clayton | 17cc8b9 | 2011-06-24 03:47:23 +0000 | [diff] [blame] | 326 | } |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 327 | m_block.SetBlockInfoHasBeenParsed (true, true); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 328 | } |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 329 | return m_block; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | CompileUnit* |
| 333 | Function::GetCompileUnit() |
| 334 | { |
| 335 | return m_comp_unit; |
| 336 | } |
| 337 | |
| 338 | const CompileUnit* |
| 339 | Function::GetCompileUnit() const |
| 340 | { |
| 341 | return m_comp_unit; |
| 342 | } |
| 343 | |
Greg Clayton | 0c5cd90 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 344 | |
| 345 | void |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 346 | Function::GetDescription(Stream *s, lldb::DescriptionLevel level, Target *target) |
Greg Clayton | 0c5cd90 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 347 | { |
| 348 | Type* func_type = GetType(); |
Jim Ingham | 28eb571 | 2012-10-12 17:34:26 +0000 | [diff] [blame] | 349 | const char *name = func_type ? func_type->GetName().AsCString() : "<unknown>"; |
| 350 | |
| 351 | *s << "id = " << (const UserID&)*this << ", name = \"" << name << "\", range = "; |
Greg Clayton | c980066 | 2010-09-10 01:30:46 +0000 | [diff] [blame] | 352 | |
| 353 | Address::DumpStyle fallback_style; |
| 354 | if (level == eDescriptionLevelVerbose) |
| 355 | fallback_style = Address::DumpStyleModuleWithFileAddress; |
| 356 | else |
| 357 | fallback_style = Address::DumpStyleFileAddress; |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 358 | GetAddressRange().Dump(s, target, Address::DumpStyleLoadAddress, fallback_style); |
Greg Clayton | 0c5cd90 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 361 | void |
| 362 | Function::Dump(Stream *s, bool show_context) const |
| 363 | { |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 364 | s->Printf("%p: ", static_cast<const void*>(this)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 365 | s->Indent(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 366 | *s << "Function" << static_cast<const UserID&>(*this); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 367 | |
| 368 | m_mangled.Dump(s); |
| 369 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 370 | if (m_type) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 371 | s->Printf(", type = %p", static_cast<void*>(m_type)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 372 | else if (m_type_uid != LLDB_INVALID_UID) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 373 | s->Printf(", type_uid = 0x%8.8" PRIx64, m_type_uid); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 374 | |
| 375 | s->EOL(); |
| 376 | // Dump the root object |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 377 | if (m_block.BlockInfoHasBeenParsed ()) |
| 378 | m_block.Dump(s, m_range.GetBaseAddress().GetFileAddress(), INT_MAX, show_context); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | |
| 382 | void |
| 383 | Function::CalculateSymbolContext(SymbolContext* sc) |
| 384 | { |
| 385 | sc->function = this; |
| 386 | m_comp_unit->CalculateSymbolContext(sc); |
| 387 | } |
| 388 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 389 | ModuleSP |
Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 390 | Function::CalculateSymbolContextModule () |
| 391 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 392 | SectionSP section_sp (m_range.GetBaseAddress().GetSection()); |
| 393 | if (section_sp) |
Greg Clayton | 9422dd6 | 2013-03-04 21:46:16 +0000 | [diff] [blame] | 394 | return section_sp->GetModule(); |
Jim Ingham | 881ec85 | 2011-10-07 22:20:35 +0000 | [diff] [blame] | 395 | |
Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 396 | return this->GetCompileUnit()->GetModule(); |
| 397 | } |
| 398 | |
| 399 | CompileUnit * |
| 400 | Function::CalculateSymbolContextCompileUnit () |
| 401 | { |
| 402 | return this->GetCompileUnit(); |
| 403 | } |
| 404 | |
| 405 | Function * |
| 406 | Function::CalculateSymbolContextFunction () |
| 407 | { |
| 408 | return this; |
| 409 | } |
| 410 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 411 | lldb::DisassemblerSP |
| 412 | Function::GetInstructions (const ExecutionContext &exe_ctx, |
| 413 | const char *flavor, |
| 414 | bool prefer_file_cache) |
| 415 | { |
| 416 | ModuleSP module_sp (GetAddressRange().GetBaseAddress().GetModule()); |
| 417 | if (module_sp) |
| 418 | { |
| 419 | const bool prefer_file_cache = false; |
| 420 | return Disassembler::DisassembleRange (module_sp->GetArchitecture(), |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 421 | nullptr, |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 422 | flavor, |
| 423 | exe_ctx, |
| 424 | GetAddressRange(), |
| 425 | prefer_file_cache); |
| 426 | } |
| 427 | return lldb::DisassemblerSP(); |
| 428 | } |
| 429 | |
| 430 | bool |
| 431 | Function::GetDisassembly (const ExecutionContext &exe_ctx, |
| 432 | const char *flavor, |
| 433 | bool prefer_file_cache, |
| 434 | Stream &strm) |
| 435 | { |
| 436 | lldb::DisassemblerSP disassembler_sp = GetInstructions (exe_ctx, flavor, prefer_file_cache); |
| 437 | if (disassembler_sp) |
| 438 | { |
| 439 | const bool show_address = true; |
| 440 | const bool show_bytes = false; |
| 441 | disassembler_sp->GetInstructionList().Dump (&strm, show_address, show_bytes, &exe_ctx); |
| 442 | return true; |
| 443 | } |
| 444 | return false; |
| 445 | } |
| 446 | |
| 447 | |
Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 448 | //Symbol * |
| 449 | //Function::CalculateSymbolContextSymbol () |
| 450 | //{ |
| 451 | // return // TODO: find the symbol for the function??? |
| 452 | //} |
| 453 | |
| 454 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 455 | void |
| 456 | Function::DumpSymbolContext(Stream *s) |
| 457 | { |
| 458 | m_comp_unit->DumpSymbolContext(s); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 459 | s->Printf(", Function{0x%8.8" PRIx64 "}", GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | size_t |
| 463 | Function::MemorySize () const |
| 464 | { |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 465 | size_t mem_size = sizeof(Function) + m_block.MemorySize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 466 | return mem_size; |
| 467 | } |
| 468 | |
Jason Molenda | 6ab659a | 2015-07-29 00:42:47 +0000 | [diff] [blame] | 469 | bool |
| 470 | Function::GetIsOptimized () |
| 471 | { |
| 472 | bool result = false; |
| 473 | |
| 474 | // Currently optimization is only indicted by the |
| 475 | // vendor extension DW_AT_APPLE_optimized which |
| 476 | // is set on a compile unit level. |
| 477 | if (m_comp_unit) |
| 478 | { |
| 479 | result = m_comp_unit->GetIsOptimized(); |
| 480 | } |
| 481 | return result; |
| 482 | } |
| 483 | |
Enrico Granata | c1f705c | 2015-07-06 18:28:46 +0000 | [diff] [blame] | 484 | ConstString |
| 485 | Function::GetDisplayName () const |
| 486 | { |
| 487 | if (!m_mangled) |
| 488 | return ConstString(); |
Greg Clayton | ddaf6a7 | 2015-07-08 22:32:23 +0000 | [diff] [blame] | 489 | return m_mangled.GetDisplayDemangledName(GetLanguage()); |
Enrico Granata | c1f705c | 2015-07-06 18:28:46 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 492 | CompilerDeclContext |
| 493 | Function::GetDeclContext() |
Sean Callanan | 72e4940 | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 494 | { |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 495 | ModuleSP module_sp = CalculateSymbolContextModule (); |
| 496 | |
| 497 | if (module_sp) |
| 498 | { |
| 499 | SymbolVendor *sym_vendor = module_sp->GetSymbolVendor(); |
| 500 | |
| 501 | if (sym_vendor) |
| 502 | { |
| 503 | SymbolFile *sym_file = sym_vendor->GetSymbolFile(); |
| 504 | |
| 505 | if (sym_file) |
| 506 | return sym_file->GetDeclContextForUID (GetID()); |
| 507 | } |
| 508 | } |
| 509 | return CompilerDeclContext(); |
Sean Callanan | 72e4940 | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 512 | Type* |
| 513 | Function::GetType() |
| 514 | { |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 515 | if (m_type == nullptr) |
Greg Clayton | 2bc22f8 | 2011-09-30 03:20:47 +0000 | [diff] [blame] | 516 | { |
| 517 | SymbolContext sc; |
| 518 | |
| 519 | CalculateSymbolContext (&sc); |
| 520 | |
| 521 | if (!sc.module_sp) |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 522 | return nullptr; |
Greg Clayton | 2bc22f8 | 2011-09-30 03:20:47 +0000 | [diff] [blame] | 523 | |
| 524 | SymbolVendor *sym_vendor = sc.module_sp->GetSymbolVendor(); |
| 525 | |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 526 | if (sym_vendor == nullptr) |
| 527 | return nullptr; |
Greg Clayton | 2bc22f8 | 2011-09-30 03:20:47 +0000 | [diff] [blame] | 528 | |
| 529 | SymbolFile *sym_file = sym_vendor->GetSymbolFile(); |
| 530 | |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 531 | if (sym_file == nullptr) |
| 532 | return nullptr; |
Greg Clayton | 2bc22f8 | 2011-09-30 03:20:47 +0000 | [diff] [blame] | 533 | |
Greg Clayton | 5cf58b9 | 2011-10-05 22:22:08 +0000 | [diff] [blame] | 534 | m_type = sym_file->ResolveTypeUID(m_type_uid); |
Greg Clayton | 2bc22f8 | 2011-09-30 03:20:47 +0000 | [diff] [blame] | 535 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 536 | return m_type; |
| 537 | } |
| 538 | |
| 539 | const Type* |
| 540 | Function::GetType() const |
| 541 | { |
| 542 | return m_type; |
| 543 | } |
| 544 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 545 | CompilerType |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 546 | Function::GetCompilerType() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 547 | { |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 548 | Type *function_type = GetType(); |
| 549 | if (function_type) |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 550 | return function_type->GetFullCompilerType (); |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 551 | return CompilerType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | uint32_t |
| 555 | Function::GetPrologueByteSize () |
| 556 | { |
| 557 | if (m_prologue_byte_size == 0 && m_flags.IsClear(flagsCalculatedPrologueSize)) |
| 558 | { |
| 559 | m_flags.Set(flagsCalculatedPrologueSize); |
| 560 | LineTable* line_table = m_comp_unit->GetLineTable (); |
| 561 | if (line_table) |
| 562 | { |
Greg Clayton | 83b6fab | 2012-04-26 01:01:34 +0000 | [diff] [blame] | 563 | LineEntry first_line_entry; |
| 564 | uint32_t first_line_entry_idx = UINT32_MAX; |
| 565 | if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(), first_line_entry, &first_line_entry_idx)) |
Greg Clayton | c3b8499 | 2010-11-11 20:13:30 +0000 | [diff] [blame] | 566 | { |
Greg Clayton | 83b6fab | 2012-04-26 01:01:34 +0000 | [diff] [blame] | 567 | // Make sure the first line entry isn't already the end of the prologue |
| 568 | addr_t prologue_end_file_addr = LLDB_INVALID_ADDRESS; |
| 569 | if (first_line_entry.is_prologue_end) |
| 570 | { |
| 571 | prologue_end_file_addr = first_line_entry.range.GetBaseAddress().GetFileAddress(); |
| 572 | } |
| 573 | else |
| 574 | { |
| 575 | // Check the first few instructions and look for one that has |
| 576 | // is_prologue_end set to true. |
| 577 | const uint32_t last_line_entry_idx = first_line_entry_idx + 6; |
Greg Clayton | 83b6fab | 2012-04-26 01:01:34 +0000 | [diff] [blame] | 578 | for (uint32_t idx = first_line_entry_idx + 1; idx < last_line_entry_idx; ++idx) |
| 579 | { |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 580 | LineEntry line_entry; |
Greg Clayton | 83b6fab | 2012-04-26 01:01:34 +0000 | [diff] [blame] | 581 | if (line_table->GetLineEntryAtIndex (idx, line_entry)) |
| 582 | { |
| 583 | if (line_entry.is_prologue_end) |
| 584 | { |
| 585 | prologue_end_file_addr = line_entry.range.GetBaseAddress().GetFileAddress(); |
| 586 | break; |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | } |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 591 | |
Greg Clayton | 83b6fab | 2012-04-26 01:01:34 +0000 | [diff] [blame] | 592 | // If we didn't find the end of the prologue in the line tables, |
| 593 | // then just use the end address of the first line table entry |
| 594 | if (prologue_end_file_addr == LLDB_INVALID_ADDRESS) |
| 595 | { |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 596 | // Check the first few instructions and look for one that has |
| 597 | // a line number that's different than the first entry. |
| 598 | const uint32_t last_line_entry_idx = first_line_entry_idx + 6; |
| 599 | for (uint32_t idx = first_line_entry_idx + 1; idx < last_line_entry_idx; ++idx) |
| 600 | { |
| 601 | LineEntry line_entry; |
| 602 | if (line_table->GetLineEntryAtIndex (idx, line_entry)) |
| 603 | { |
| 604 | if (line_entry.line != first_line_entry.line) |
| 605 | { |
| 606 | prologue_end_file_addr = line_entry.range.GetBaseAddress().GetFileAddress(); |
| 607 | break; |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | if (prologue_end_file_addr == LLDB_INVALID_ADDRESS) |
| 613 | { |
| 614 | prologue_end_file_addr = first_line_entry.range.GetBaseAddress().GetFileAddress() + first_line_entry.range.GetByteSize(); |
| 615 | } |
Greg Clayton | 83b6fab | 2012-04-26 01:01:34 +0000 | [diff] [blame] | 616 | } |
Greg Clayton | c3b8499 | 2010-11-11 20:13:30 +0000 | [diff] [blame] | 617 | const addr_t func_start_file_addr = m_range.GetBaseAddress().GetFileAddress(); |
Greg Clayton | 83b6fab | 2012-04-26 01:01:34 +0000 | [diff] [blame] | 618 | const addr_t func_end_file_addr = func_start_file_addr + m_range.GetByteSize(); |
| 619 | |
| 620 | // Verify that this prologue end file address in the function's |
| 621 | // address range just to be sure |
| 622 | if (func_start_file_addr < prologue_end_file_addr && prologue_end_file_addr < func_end_file_addr) |
| 623 | { |
| 624 | m_prologue_byte_size = prologue_end_file_addr - func_start_file_addr; |
| 625 | } |
Greg Clayton | c3b8499 | 2010-11-11 20:13:30 +0000 | [diff] [blame] | 626 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | return m_prologue_byte_size; |
| 630 | } |
| 631 | |
Greg Clayton | ddaf6a7 | 2015-07-08 22:32:23 +0000 | [diff] [blame] | 632 | lldb::LanguageType |
| 633 | Function::GetLanguage() const |
| 634 | { |
| 635 | if (m_comp_unit) |
| 636 | return m_comp_unit->GetLanguage(); |
| 637 | else |
| 638 | return lldb::eLanguageTypeUnknown; |
| 639 | } |
| 640 | |
| 641 | ConstString |
| 642 | Function::GetName() const |
| 643 | { |
| 644 | LanguageType language = lldb::eLanguageTypeUnknown; |
| 645 | if (m_comp_unit) |
| 646 | language = m_comp_unit->GetLanguage(); |
| 647 | return m_mangled.GetName(language); |
| 648 | } |
| 649 | |
| 650 | ConstString |
| 651 | Function::GetNameNoArguments() const |
| 652 | { |
| 653 | LanguageType language = lldb::eLanguageTypeUnknown; |
| 654 | if (m_comp_unit) |
| 655 | language = m_comp_unit->GetLanguage(); |
| 656 | return m_mangled.GetName(language, Mangled::ePreferDemangledWithoutArguments); |
| 657 | } |
| 658 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 659 | |
| 660 | |