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 | e1a916a | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 15 | #include "lldb/Symbol/ClangASTType.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 | |
| 80 | const ConstString& |
| 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 |
| 143 | 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 | |
Greg Clayton | 1b72fcb | 2010-08-24 00:45:41 +0000 | [diff] [blame] | 153 | |
| 154 | const ConstString & |
| 155 | InlineFunctionInfo::GetName () const |
| 156 | { |
| 157 | if (m_mangled) |
| 158 | return m_mangled.GetName(); |
| 159 | return m_name; |
| 160 | } |
| 161 | |
| 162 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 163 | Declaration & |
| 164 | InlineFunctionInfo::GetCallSite () |
| 165 | { |
| 166 | return m_call_decl; |
| 167 | } |
| 168 | |
| 169 | const Declaration & |
| 170 | InlineFunctionInfo::GetCallSite () const |
| 171 | { |
| 172 | return m_call_decl; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | Mangled& |
| 177 | InlineFunctionInfo::GetMangled() |
| 178 | { |
| 179 | return m_mangled; |
| 180 | } |
| 181 | |
| 182 | const Mangled& |
| 183 | InlineFunctionInfo::GetMangled() const |
| 184 | { |
| 185 | return m_mangled; |
| 186 | } |
| 187 | |
| 188 | size_t |
| 189 | InlineFunctionInfo::MemorySize() const |
| 190 | { |
| 191 | return FunctionInfo::MemorySize() + m_mangled.MemorySize(); |
| 192 | } |
| 193 | |
| 194 | //---------------------------------------------------------------------- |
| 195 | // |
| 196 | //---------------------------------------------------------------------- |
| 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 | ) : |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 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) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 216 | { |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 217 | m_block.SetParentScope(this); |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame^] | 218 | assert(comp_unit != nullptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 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 | ) : |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 230 | UserID (func_uid), |
| 231 | m_comp_unit (comp_unit), |
| 232 | m_type_uid (type_uid), |
| 233 | m_type (type), |
Greg Clayton | 037520e | 2012-07-18 23:18:10 +0000 | [diff] [blame] | 234 | m_mangled (ConstString(mangled), true), |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 235 | m_block (func_uid), |
| 236 | m_range (range), |
| 237 | m_frame_base (), |
| 238 | m_flags (), |
| 239 | m_prologue_byte_size (0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 240 | { |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 241 | m_block.SetParentScope(this); |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame^] | 242 | assert(comp_unit != nullptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | |
| 246 | Function::~Function() |
| 247 | { |
| 248 | } |
| 249 | |
Jim Ingham | 9976033 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 250 | void |
| 251 | Function::GetStartLineSourceInfo (FileSpec &source_file, uint32_t &line_no) |
| 252 | { |
| 253 | line_no = 0; |
| 254 | source_file.Clear(); |
| 255 | |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame^] | 256 | if (m_comp_unit == nullptr) |
Jim Ingham | 9976033 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 257 | return; |
| 258 | |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame^] | 259 | if (m_type != nullptr && m_type->GetDeclaration().GetLine() != 0) |
Jim Ingham | 9976033 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 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(); |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame^] | 267 | if (line_table == nullptr) |
Jim Ingham | 9976033 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 268 | return; |
| 269 | |
| 270 | LineEntry line_entry; |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame^] | 271 | if (line_table->FindLineEntryByAddress (GetAddressRange().GetBaseAddress(), line_entry, nullptr)) |
Jim Ingham | 9976033 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 272 | { |
| 273 | line_no = line_entry.line; |
| 274 | source_file = line_entry.file; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | void |
| 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(); |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame^] | 291 | if (line_table == nullptr) |
Jim Ingham | 9976033 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 292 | return; |
| 293 | |
| 294 | LineEntry line_entry; |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame^] | 295 | if (line_table->FindLineEntryByAddress (scratch_addr, line_entry, nullptr)) |
Jim Ingham | 9976033 | 2010-08-20 01:15:01 +0000 | [diff] [blame] | 296 | { |
| 297 | line_no = line_entry.line; |
| 298 | source_file = line_entry.file; |
| 299 | } |
| 300 | } |
| 301 | |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 302 | Block & |
| 303 | Function::GetBlock (bool can_create) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 304 | { |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 305 | if (!m_block.BlockInfoHasBeenParsed() && can_create) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 306 | { |
| 307 | SymbolContext sc; |
| 308 | CalculateSymbolContext(&sc); |
Greg Clayton | 17cc8b9 | 2011-06-24 03:47:23 +0000 | [diff] [blame] | 309 | if (sc.module_sp) |
| 310 | { |
| 311 | sc.module_sp->GetSymbolVendor()->ParseFunctionBlocks(sc); |
| 312 | } |
| 313 | else |
| 314 | { |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 315 | Host::SystemLog (Host::eSystemLogError, |
Greg Clayton | b5ad4ec | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 316 | "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] | 317 | GetName().GetCString(), |
Greg Clayton | b5ad4ec | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 318 | m_comp_unit->GetPath().c_str()); |
Greg Clayton | 17cc8b9 | 2011-06-24 03:47:23 +0000 | [diff] [blame] | 319 | } |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 320 | m_block.SetBlockInfoHasBeenParsed (true, true); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 321 | } |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 322 | return m_block; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | CompileUnit* |
| 326 | Function::GetCompileUnit() |
| 327 | { |
| 328 | return m_comp_unit; |
| 329 | } |
| 330 | |
| 331 | const CompileUnit* |
| 332 | Function::GetCompileUnit() const |
| 333 | { |
| 334 | return m_comp_unit; |
| 335 | } |
| 336 | |
Greg Clayton | 0c5cd90 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 337 | |
| 338 | void |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 339 | Function::GetDescription(Stream *s, lldb::DescriptionLevel level, Target *target) |
Greg Clayton | 0c5cd90 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 340 | { |
| 341 | Type* func_type = GetType(); |
Jim Ingham | 28eb571 | 2012-10-12 17:34:26 +0000 | [diff] [blame] | 342 | const char *name = func_type ? func_type->GetName().AsCString() : "<unknown>"; |
| 343 | |
| 344 | *s << "id = " << (const UserID&)*this << ", name = \"" << name << "\", range = "; |
Greg Clayton | c980066 | 2010-09-10 01:30:46 +0000 | [diff] [blame] | 345 | |
| 346 | Address::DumpStyle fallback_style; |
| 347 | if (level == eDescriptionLevelVerbose) |
| 348 | fallback_style = Address::DumpStyleModuleWithFileAddress; |
| 349 | else |
| 350 | fallback_style = Address::DumpStyleFileAddress; |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 351 | GetAddressRange().Dump(s, target, Address::DumpStyleLoadAddress, fallback_style); |
Greg Clayton | 0c5cd90 | 2010-06-28 21:30:43 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 354 | void |
| 355 | Function::Dump(Stream *s, bool show_context) const |
| 356 | { |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 357 | s->Printf("%p: ", static_cast<const void*>(this)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 358 | s->Indent(); |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 359 | *s << "Function" << static_cast<const UserID&>(*this); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 360 | |
| 361 | m_mangled.Dump(s); |
| 362 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 363 | if (m_type) |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 364 | s->Printf(", type = %p", static_cast<void*>(m_type)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 365 | else if (m_type_uid != LLDB_INVALID_UID) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 366 | s->Printf(", type_uid = 0x%8.8" PRIx64, m_type_uid); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 367 | |
| 368 | s->EOL(); |
| 369 | // Dump the root object |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 370 | if (m_block.BlockInfoHasBeenParsed ()) |
| 371 | m_block.Dump(s, m_range.GetBaseAddress().GetFileAddress(), INT_MAX, show_context); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | |
| 375 | void |
| 376 | Function::CalculateSymbolContext(SymbolContext* sc) |
| 377 | { |
| 378 | sc->function = this; |
| 379 | m_comp_unit->CalculateSymbolContext(sc); |
| 380 | } |
| 381 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 382 | ModuleSP |
Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 383 | Function::CalculateSymbolContextModule () |
| 384 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 385 | SectionSP section_sp (m_range.GetBaseAddress().GetSection()); |
| 386 | if (section_sp) |
Greg Clayton | 9422dd6 | 2013-03-04 21:46:16 +0000 | [diff] [blame] | 387 | return section_sp->GetModule(); |
Jim Ingham | 881ec85 | 2011-10-07 22:20:35 +0000 | [diff] [blame] | 388 | |
Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 389 | return this->GetCompileUnit()->GetModule(); |
| 390 | } |
| 391 | |
| 392 | CompileUnit * |
| 393 | Function::CalculateSymbolContextCompileUnit () |
| 394 | { |
| 395 | return this->GetCompileUnit(); |
| 396 | } |
| 397 | |
| 398 | Function * |
| 399 | Function::CalculateSymbolContextFunction () |
| 400 | { |
| 401 | return this; |
| 402 | } |
| 403 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 404 | lldb::DisassemblerSP |
| 405 | Function::GetInstructions (const ExecutionContext &exe_ctx, |
| 406 | const char *flavor, |
| 407 | bool prefer_file_cache) |
| 408 | { |
| 409 | ModuleSP module_sp (GetAddressRange().GetBaseAddress().GetModule()); |
| 410 | if (module_sp) |
| 411 | { |
| 412 | const bool prefer_file_cache = false; |
| 413 | return Disassembler::DisassembleRange (module_sp->GetArchitecture(), |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame^] | 414 | nullptr, |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 415 | flavor, |
| 416 | exe_ctx, |
| 417 | GetAddressRange(), |
| 418 | prefer_file_cache); |
| 419 | } |
| 420 | return lldb::DisassemblerSP(); |
| 421 | } |
| 422 | |
| 423 | bool |
| 424 | Function::GetDisassembly (const ExecutionContext &exe_ctx, |
| 425 | const char *flavor, |
| 426 | bool prefer_file_cache, |
| 427 | Stream &strm) |
| 428 | { |
| 429 | lldb::DisassemblerSP disassembler_sp = GetInstructions (exe_ctx, flavor, prefer_file_cache); |
| 430 | if (disassembler_sp) |
| 431 | { |
| 432 | const bool show_address = true; |
| 433 | const bool show_bytes = false; |
| 434 | disassembler_sp->GetInstructionList().Dump (&strm, show_address, show_bytes, &exe_ctx); |
| 435 | return true; |
| 436 | } |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | |
Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 441 | //Symbol * |
| 442 | //Function::CalculateSymbolContextSymbol () |
| 443 | //{ |
| 444 | // return // TODO: find the symbol for the function??? |
| 445 | //} |
| 446 | |
| 447 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 448 | void |
| 449 | Function::DumpSymbolContext(Stream *s) |
| 450 | { |
| 451 | m_comp_unit->DumpSymbolContext(s); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 452 | s->Printf(", Function{0x%8.8" PRIx64 "}", GetID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | size_t |
| 456 | Function::MemorySize () const |
| 457 | { |
Greg Clayton | 0b76a2c | 2010-08-21 02:22:51 +0000 | [diff] [blame] | 458 | size_t mem_size = sizeof(Function) + m_block.MemorySize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 459 | return mem_size; |
| 460 | } |
| 461 | |
Sean Callanan | 72e4940 | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 462 | clang::DeclContext * |
| 463 | Function::GetClangDeclContext() |
| 464 | { |
| 465 | SymbolContext sc; |
| 466 | |
| 467 | CalculateSymbolContext (&sc); |
| 468 | |
| 469 | if (!sc.module_sp) |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame^] | 470 | return nullptr; |
Sean Callanan | 72e4940 | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 471 | |
| 472 | SymbolVendor *sym_vendor = sc.module_sp->GetSymbolVendor(); |
| 473 | |
| 474 | if (!sym_vendor) |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame^] | 475 | return nullptr; |
Sean Callanan | 72e4940 | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 476 | |
| 477 | SymbolFile *sym_file = sym_vendor->GetSymbolFile(); |
| 478 | |
| 479 | if (!sym_file) |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame^] | 480 | return nullptr; |
Sean Callanan | 72e4940 | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 481 | |
| 482 | return sym_file->GetClangDeclContextForTypeUID (sc, m_uid); |
| 483 | } |
| 484 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 485 | Type* |
| 486 | Function::GetType() |
| 487 | { |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame^] | 488 | if (m_type == nullptr) |
Greg Clayton | 2bc22f8 | 2011-09-30 03:20:47 +0000 | [diff] [blame] | 489 | { |
| 490 | SymbolContext sc; |
| 491 | |
| 492 | CalculateSymbolContext (&sc); |
| 493 | |
| 494 | if (!sc.module_sp) |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame^] | 495 | return nullptr; |
Greg Clayton | 2bc22f8 | 2011-09-30 03:20:47 +0000 | [diff] [blame] | 496 | |
| 497 | SymbolVendor *sym_vendor = sc.module_sp->GetSymbolVendor(); |
| 498 | |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame^] | 499 | if (sym_vendor == nullptr) |
| 500 | return nullptr; |
Greg Clayton | 2bc22f8 | 2011-09-30 03:20:47 +0000 | [diff] [blame] | 501 | |
| 502 | SymbolFile *sym_file = sym_vendor->GetSymbolFile(); |
| 503 | |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame^] | 504 | if (sym_file == nullptr) |
| 505 | return nullptr; |
Greg Clayton | 2bc22f8 | 2011-09-30 03:20:47 +0000 | [diff] [blame] | 506 | |
Greg Clayton | 5cf58b9 | 2011-10-05 22:22:08 +0000 | [diff] [blame] | 507 | m_type = sym_file->ResolveTypeUID(m_type_uid); |
Greg Clayton | 2bc22f8 | 2011-09-30 03:20:47 +0000 | [diff] [blame] | 508 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 509 | return m_type; |
| 510 | } |
| 511 | |
| 512 | const Type* |
| 513 | Function::GetType() const |
| 514 | { |
| 515 | return m_type; |
| 516 | } |
| 517 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 518 | ClangASTType |
| 519 | Function::GetClangType() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 520 | { |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 521 | Type *function_type = GetType(); |
| 522 | if (function_type) |
| 523 | return function_type->GetClangFullType(); |
| 524 | return ClangASTType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | uint32_t |
| 528 | Function::GetPrologueByteSize () |
| 529 | { |
| 530 | if (m_prologue_byte_size == 0 && m_flags.IsClear(flagsCalculatedPrologueSize)) |
| 531 | { |
| 532 | m_flags.Set(flagsCalculatedPrologueSize); |
| 533 | LineTable* line_table = m_comp_unit->GetLineTable (); |
| 534 | if (line_table) |
| 535 | { |
Greg Clayton | 83b6fab | 2012-04-26 01:01:34 +0000 | [diff] [blame] | 536 | LineEntry first_line_entry; |
| 537 | uint32_t first_line_entry_idx = UINT32_MAX; |
| 538 | 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] | 539 | { |
Greg Clayton | 83b6fab | 2012-04-26 01:01:34 +0000 | [diff] [blame] | 540 | // Make sure the first line entry isn't already the end of the prologue |
| 541 | addr_t prologue_end_file_addr = LLDB_INVALID_ADDRESS; |
| 542 | if (first_line_entry.is_prologue_end) |
| 543 | { |
| 544 | prologue_end_file_addr = first_line_entry.range.GetBaseAddress().GetFileAddress(); |
| 545 | } |
| 546 | else |
| 547 | { |
| 548 | // Check the first few instructions and look for one that has |
| 549 | // is_prologue_end set to true. |
| 550 | const uint32_t last_line_entry_idx = first_line_entry_idx + 6; |
Greg Clayton | 83b6fab | 2012-04-26 01:01:34 +0000 | [diff] [blame] | 551 | for (uint32_t idx = first_line_entry_idx + 1; idx < last_line_entry_idx; ++idx) |
| 552 | { |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 553 | LineEntry line_entry; |
Greg Clayton | 83b6fab | 2012-04-26 01:01:34 +0000 | [diff] [blame] | 554 | if (line_table->GetLineEntryAtIndex (idx, line_entry)) |
| 555 | { |
| 556 | if (line_entry.is_prologue_end) |
| 557 | { |
| 558 | prologue_end_file_addr = line_entry.range.GetBaseAddress().GetFileAddress(); |
| 559 | break; |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | } |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 564 | |
Greg Clayton | 83b6fab | 2012-04-26 01:01:34 +0000 | [diff] [blame] | 565 | // If we didn't find the end of the prologue in the line tables, |
| 566 | // then just use the end address of the first line table entry |
| 567 | if (prologue_end_file_addr == LLDB_INVALID_ADDRESS) |
| 568 | { |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 569 | // Check the first few instructions and look for one that has |
| 570 | // a line number that's different than the first entry. |
| 571 | const uint32_t last_line_entry_idx = first_line_entry_idx + 6; |
| 572 | for (uint32_t idx = first_line_entry_idx + 1; idx < last_line_entry_idx; ++idx) |
| 573 | { |
| 574 | LineEntry line_entry; |
| 575 | if (line_table->GetLineEntryAtIndex (idx, line_entry)) |
| 576 | { |
| 577 | if (line_entry.line != first_line_entry.line) |
| 578 | { |
| 579 | prologue_end_file_addr = line_entry.range.GetBaseAddress().GetFileAddress(); |
| 580 | break; |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | if (prologue_end_file_addr == LLDB_INVALID_ADDRESS) |
| 586 | { |
| 587 | prologue_end_file_addr = first_line_entry.range.GetBaseAddress().GetFileAddress() + first_line_entry.range.GetByteSize(); |
| 588 | } |
Greg Clayton | 83b6fab | 2012-04-26 01:01:34 +0000 | [diff] [blame] | 589 | } |
Greg Clayton | c3b8499 | 2010-11-11 20:13:30 +0000 | [diff] [blame] | 590 | const addr_t func_start_file_addr = m_range.GetBaseAddress().GetFileAddress(); |
Greg Clayton | 83b6fab | 2012-04-26 01:01:34 +0000 | [diff] [blame] | 591 | const addr_t func_end_file_addr = func_start_file_addr + m_range.GetByteSize(); |
| 592 | |
| 593 | // Verify that this prologue end file address in the function's |
| 594 | // address range just to be sure |
| 595 | if (func_start_file_addr < prologue_end_file_addr && prologue_end_file_addr < func_end_file_addr) |
| 596 | { |
| 597 | m_prologue_byte_size = prologue_end_file_addr - func_start_file_addr; |
| 598 | } |
Greg Clayton | c3b8499 | 2010-11-11 20:13:30 +0000 | [diff] [blame] | 599 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | return m_prologue_byte_size; |
| 603 | } |
| 604 | |
| 605 | |
| 606 | |