1 //===-- Symbol.cpp ----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Symbol/Symbol.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/ModuleSpec.h"
14 #include "lldb/Core/Section.h"
15 #include "lldb/Core/Stream.h"
16 #include "lldb/Symbol/ObjectFile.h"
17 #include "lldb/Symbol/Symtab.h"
18 #include "lldb/Symbol/Function.h"
19 #include "lldb/Target/Process.h"
20 #include "lldb/Target/Target.h"
21 #include "lldb/Symbol/SymbolVendor.h"
24 using namespace lldb_private;
28 SymbolContextScope (),
31 m_type_data_resolved (false),
32 m_is_synthetic (false),
34 m_is_external (false),
35 m_size_is_sibling (false),
36 m_size_is_synthesized (false),
37 m_size_is_valid (false),
38 m_demangled_is_synthesized (false),
39 m_type (eSymbolTypeInvalid),
56 const lldb::SectionSP §ion_sp,
62 SymbolContextScope (),
65 m_type_data_resolved (false),
66 m_is_synthetic (is_artificial),
67 m_is_debug (is_debug),
68 m_is_external (external),
69 m_size_is_sibling (false),
70 m_size_is_synthesized (false),
71 m_size_is_valid (size_is_valid || size > 0),
72 m_demangled_is_synthesized (false),
74 m_mangled (ConstString(name), name_is_mangled),
75 m_addr_range (section_sp, offset, size),
90 const AddressRange &range,
94 SymbolContextScope (),
97 m_type_data_resolved (false),
98 m_is_synthetic (is_artificial),
99 m_is_debug (is_debug),
100 m_is_external (external),
101 m_size_is_sibling (false),
102 m_size_is_synthesized (false),
103 m_size_is_valid (size_is_valid || range.GetByteSize() > 0),
104 m_demangled_is_synthesized (false),
106 m_mangled (ConstString(name), name_is_mangled),
107 m_addr_range (range),
112 Symbol::Symbol(const Symbol& rhs):
113 SymbolContextScope (rhs),
115 m_type_data (rhs.m_type_data),
116 m_type_data_resolved (rhs.m_type_data_resolved),
117 m_is_synthetic (rhs.m_is_synthetic),
118 m_is_debug (rhs.m_is_debug),
119 m_is_external (rhs.m_is_external),
120 m_size_is_sibling (rhs.m_size_is_sibling),
121 m_size_is_synthesized (false),
122 m_size_is_valid (rhs.m_size_is_valid),
123 m_demangled_is_synthesized (rhs.m_demangled_is_synthesized),
125 m_mangled (rhs.m_mangled),
126 m_addr_range (rhs.m_addr_range),
127 m_flags (rhs.m_flags)
132 Symbol::operator= (const Symbol& rhs)
136 SymbolContextScope::operator= (rhs);
138 m_type_data = rhs.m_type_data;
139 m_type_data_resolved = rhs.m_type_data_resolved;
140 m_is_synthetic = rhs.m_is_synthetic;
141 m_is_debug = rhs.m_is_debug;
142 m_is_external = rhs.m_is_external;
143 m_size_is_sibling = rhs.m_size_is_sibling;
144 m_size_is_synthesized = rhs.m_size_is_sibling;
145 m_size_is_valid = rhs.m_size_is_valid;
146 m_demangled_is_synthesized = rhs.m_demangled_is_synthesized;
148 m_mangled = rhs.m_mangled;
149 m_addr_range = rhs.m_addr_range;
150 m_flags = rhs.m_flags;
161 m_type_data_resolved = false;
162 m_is_synthetic = false;
164 m_is_external = false;
165 m_size_is_sibling = false;
166 m_size_is_synthesized = false;
167 m_size_is_valid = false;
168 m_demangled_is_synthesized = false;
169 m_type = eSymbolTypeInvalid;
171 m_addr_range.Clear();
175 Symbol::ValueIsAddress() const
177 return m_addr_range.GetBaseAddress().GetSection().get() != NULL;
181 Symbol::GetReExportedSymbolName() const
183 if (m_type == eSymbolTypeReExported)
185 // For eSymbolTypeReExported, the "const char *" from a ConstString
186 // is used as the offset in the address range base address. We can
187 // then make this back into a string that is the re-exported name.
188 intptr_t str_ptr = m_addr_range.GetBaseAddress().GetOffset();
190 return ConstString((const char *)str_ptr);
194 return ConstString();
198 Symbol::GetReExportedSymbolSharedLibrary() const
200 if (m_type == eSymbolTypeReExported)
202 // For eSymbolTypeReExported, the "const char *" from a ConstString
203 // is used as the offset in the address range base address. We can
204 // then make this back into a string that is the re-exported name.
205 intptr_t str_ptr = m_addr_range.GetByteSize();
207 return FileSpec((const char *)str_ptr, false);
213 Symbol::SetReExportedSymbolName(const ConstString &name)
215 if (m_type == eSymbolTypeReExported)
217 // For eSymbolTypeReExported, the "const char *" from a ConstString
218 // is used as the offset in the address range base address.
219 m_addr_range.GetBaseAddress().SetOffset((intptr_t)name.GetCString());
227 Symbol::SetReExportedSymbolSharedLibrary(const FileSpec &fspec)
229 if (m_type == eSymbolTypeReExported)
231 // For eSymbolTypeReExported, the "const char *" from a ConstString
232 // is used as the offset in the address range base address.
233 m_addr_range.SetByteSize((intptr_t)ConstString(fspec.GetPath().c_str()).GetCString());
241 Symbol::GetSiblingIndex() const
243 return m_size_is_sibling ? m_addr_range.GetByteSize() : 0;
247 Symbol::IsTrampoline () const
249 return m_type == eSymbolTypeTrampoline;
253 Symbol::IsIndirect () const
255 return m_type == eSymbolTypeResolver;
259 Symbol::GetDescription (Stream *s, lldb::DescriptionLevel level, Target *target) const
261 s->Printf("id = {0x%8.8x}", m_uid);
263 if (m_addr_range.GetBaseAddress().GetSection())
265 if (ValueIsAddress())
267 const lldb::addr_t byte_size = GetByteSize();
270 s->PutCString (", range = ");
271 m_addr_range.Dump(s, target, Address::DumpStyleLoadAddress, Address::DumpStyleFileAddress);
275 s->PutCString (", address = ");
276 m_addr_range.GetBaseAddress().Dump(s, target, Address::DumpStyleLoadAddress, Address::DumpStyleFileAddress);
280 s->Printf (", value = 0x%16.16" PRIx64, m_addr_range.GetBaseAddress().GetOffset());
284 if (m_size_is_sibling)
285 s->Printf (", sibling = %5" PRIu64, m_addr_range.GetBaseAddress().GetOffset());
287 s->Printf (", value = 0x%16.16" PRIx64, m_addr_range.GetBaseAddress().GetOffset());
289 if (m_mangled.GetDemangledName())
290 s->Printf(", name=\"%s\"", m_mangled.GetDemangledName().AsCString());
291 if (m_mangled.GetMangledName())
292 s->Printf(", mangled=\"%s\"", m_mangled.GetMangledName().AsCString());
297 Symbol::Dump(Stream *s, Target *target, uint32_t index) const
299 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
301 // s->Printf("Symbol[%5u] %6u %c%c %-12s ",
302 s->Printf("[%5u] %6u %c%c%c %-12s ",
305 m_is_debug ? 'D' : ' ',
306 m_is_synthetic ? 'S' : ' ',
307 m_is_external ? 'X' : ' ',
310 // Make sure the size of the symbol is up to date before dumping
313 if (ValueIsAddress())
315 if (!m_addr_range.GetBaseAddress().Dump(s, NULL, Address::DumpStyleFileAddress))
316 s->Printf("%*s", 18, "");
320 if (!m_addr_range.GetBaseAddress().Dump(s, target, Address::DumpStyleLoadAddress))
321 s->Printf("%*s", 18, "");
323 const char *format = m_size_is_sibling ?
324 " Sibling -> [%5llu] 0x%8.8x %s\n":
325 " 0x%16.16" PRIx64 " 0x%8.8x %s\n";
329 m_mangled.GetName().AsCString(""));
331 else if (m_type == eSymbolTypeReExported)
333 s->Printf (" 0x%8.8x %s",
335 m_mangled.GetName().AsCString(""));
337 ConstString reexport_name = GetReExportedSymbolName();
338 intptr_t shlib = m_addr_range.GetByteSize();
340 s->Printf(" -> %s`%s\n", (const char *)shlib, reexport_name.GetCString());
342 s->Printf(" -> %s\n", reexport_name.GetCString());
346 const char *format = m_size_is_sibling ?
347 "0x%16.16" PRIx64 " Sibling -> [%5llu] 0x%8.8x %s\n":
348 "0x%16.16" PRIx64 " 0x%16.16" PRIx64 " 0x%8.8x %s\n";
350 m_addr_range.GetBaseAddress().GetOffset(),
353 m_mangled.GetName().AsCString(""));
358 Symbol::GetPrologueByteSize ()
360 if (m_type == eSymbolTypeCode || m_type == eSymbolTypeResolver)
362 if (!m_type_data_resolved)
364 m_type_data_resolved = true;
366 const Address &base_address = m_addr_range.GetBaseAddress();
367 Function *function = base_address.CalculateSymbolContextFunction();
370 // Functions have line entries which can also potentially have end of prologue information.
371 // So if this symbol points to a function, use the prologue information from there.
372 m_type_data = function->GetPrologueByteSize();
376 ModuleSP module_sp (base_address.GetModule());
380 uint32_t resolved_flags = module_sp->ResolveSymbolContextForAddress (base_address,
381 eSymbolContextLineEntry,
383 if (resolved_flags & eSymbolContextLineEntry)
385 // Default to the end of the first line entry.
386 m_type_data = sc.line_entry.range.GetByteSize();
388 // Set address for next line.
389 Address addr (base_address);
390 addr.Slide (m_type_data);
392 // Check the first few instructions and look for one that has a line number that is
393 // different than the first entry. This is also done in Function::GetPrologueByteSize().
394 uint16_t total_offset = m_type_data;
395 for (int idx = 0; idx < 6; ++idx)
397 SymbolContext sc_temp;
398 resolved_flags = module_sp->ResolveSymbolContextForAddress (addr, eSymbolContextLineEntry, sc_temp);
399 // Make sure we got line number information...
400 if (!(resolved_flags & eSymbolContextLineEntry))
403 // If this line number is different than our first one, use it and we're done.
404 if (sc_temp.line_entry.line != sc.line_entry.line)
406 m_type_data = total_offset;
410 // Slide addr up to the next line address.
411 addr.Slide (sc_temp.line_entry.range.GetByteSize());
412 total_offset += sc_temp.line_entry.range.GetByteSize();
413 // If we've gone too far, bail out.
414 if (total_offset >= m_addr_range.GetByteSize())
418 // Sanity check - this may be a function in the middle of code that has debug information, but
419 // not for this symbol. So the line entries surrounding us won't lie inside our function.
420 // In that case, the line entry will be bigger than we are, so we do that quick check and
421 // if that is true, we just return 0.
422 if (m_type_data >= m_addr_range.GetByteSize())
427 // TODO: expose something in Process to figure out the
428 // size of a function prologue.
440 Symbol::Compare(const ConstString& name, SymbolType type) const
442 if (type == eSymbolTypeAny || m_type == type)
443 return m_mangled.GetMangledName() == name || m_mangled.GetDemangledName() == name;
447 #define ENUM_TO_CSTRING(x) case eSymbolType##x: return #x;
450 Symbol::GetTypeAsString() const
454 ENUM_TO_CSTRING(Invalid);
455 ENUM_TO_CSTRING(Absolute);
456 ENUM_TO_CSTRING(Code);
457 ENUM_TO_CSTRING(Resolver);
458 ENUM_TO_CSTRING(Data);
459 ENUM_TO_CSTRING(Trampoline);
460 ENUM_TO_CSTRING(Runtime);
461 ENUM_TO_CSTRING(Exception);
462 ENUM_TO_CSTRING(SourceFile);
463 ENUM_TO_CSTRING(HeaderFile);
464 ENUM_TO_CSTRING(ObjectFile);
465 ENUM_TO_CSTRING(CommonBlock);
466 ENUM_TO_CSTRING(Block);
467 ENUM_TO_CSTRING(Local);
468 ENUM_TO_CSTRING(Param);
469 ENUM_TO_CSTRING(Variable);
470 ENUM_TO_CSTRING(VariableType);
471 ENUM_TO_CSTRING(LineEntry);
472 ENUM_TO_CSTRING(LineHeader);
473 ENUM_TO_CSTRING(ScopeBegin);
474 ENUM_TO_CSTRING(ScopeEnd);
475 ENUM_TO_CSTRING(Additional);
476 ENUM_TO_CSTRING(Compiler);
477 ENUM_TO_CSTRING(Instrumentation);
478 ENUM_TO_CSTRING(Undefined);
479 ENUM_TO_CSTRING(ObjCClass);
480 ENUM_TO_CSTRING(ObjCMetaClass);
481 ENUM_TO_CSTRING(ObjCIVar);
482 ENUM_TO_CSTRING(ReExported);
486 return "<unknown SymbolType>";
490 Symbol::CalculateSymbolContext (SymbolContext *sc)
492 // Symbols can reconstruct the symbol and the module in the symbol context
494 if (ValueIsAddress())
495 sc->module_sp = GetAddress().GetModule();
497 sc->module_sp.reset();
501 Symbol::CalculateSymbolContextModule ()
503 if (ValueIsAddress())
504 return GetAddress().GetModule();
509 Symbol::CalculateSymbolContextSymbol ()
515 Symbol::DumpSymbolContext (Stream *s)
517 bool dumped_module = false;
518 if (ValueIsAddress())
520 ModuleSP module_sp (GetAddress().GetModule());
523 dumped_module = true;
524 module_sp->DumpSymbolContext(s);
530 s->Printf("Symbol{0x%8.8x}", GetID());
534 Symbol::GetByteSize () const
536 return m_addr_range.GetByteSize();
540 Symbol::ResolveReExportedSymbol (Target &target)
542 ConstString reexport_name (GetReExportedSymbolName());
545 ModuleSpec module_spec;
547 module_spec.GetFileSpec() = GetReExportedSymbolSharedLibrary();
548 if (module_spec.GetFileSpec())
550 // Try searching for the module file spec first using the full path
551 module_sp = target.GetImages().FindFirstModule(module_spec);
554 // Next try and find the module by basename in case environment
555 // variables or other runtime trickery causes shared libraries
556 // to be loaded from alternate paths
557 module_spec.GetFileSpec().GetDirectory().Clear();
558 module_sp = target.GetImages().FindFirstModule(module_spec);
564 lldb_private::SymbolContextList sc_list;
565 module_sp->FindSymbolsWithNameAndType(reexport_name, eSymbolTypeAny, sc_list);
566 const size_t num_scs = sc_list.GetSize();
569 for (size_t i=0; i<num_scs; ++i)
571 lldb_private::SymbolContext sc;
572 if (sc_list.GetContextAtIndex(i, sc))
574 if (sc.symbol->IsExternal())
586 Symbol::GetInstructions (const ExecutionContext &exe_ctx,
588 bool prefer_file_cache)
590 ModuleSP module_sp (m_addr_range.GetBaseAddress().GetModule());
593 const bool prefer_file_cache = false;
594 return Disassembler::DisassembleRange (module_sp->GetArchitecture(),
601 return lldb::DisassemblerSP();
605 Symbol::GetDisassembly (const ExecutionContext &exe_ctx,
607 bool prefer_file_cache,
610 lldb::DisassemblerSP disassembler_sp = GetInstructions (exe_ctx, flavor, prefer_file_cache);
613 const bool show_address = true;
614 const bool show_bytes = false;
615 disassembler_sp->GetInstructionList().Dump (&strm, show_address, show_bytes, &exe_ctx);