1 //===-- SymbolFileSymtab.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 "SymbolFileSymtab.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/PluginManager.h"
13 #include "lldb/Core/RegularExpression.h"
14 #include "lldb/Core/Timer.h"
15 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
16 #include "lldb/Symbol/CompileUnit.h"
17 #include "lldb/Symbol/Function.h"
18 #include "lldb/Symbol/ObjectFile.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 #include "lldb/Symbol/Symbol.h"
21 #include "lldb/Symbol/SymbolContext.h"
22 #include "lldb/Symbol/Symtab.h"
23 #include "lldb/Symbol/TypeList.h"
26 using namespace lldb_private;
29 SymbolFileSymtab::Initialize()
31 PluginManager::RegisterPlugin (GetPluginNameStatic(),
32 GetPluginDescriptionStatic(),
37 SymbolFileSymtab::Terminate()
39 PluginManager::UnregisterPlugin (CreateInstance);
43 lldb_private::ConstString
44 SymbolFileSymtab::GetPluginNameStatic()
46 static ConstString g_name("symtab");
51 SymbolFileSymtab::GetPluginDescriptionStatic()
53 return "Reads debug symbols from an object file's symbol table.";
58 SymbolFileSymtab::CreateInstance (ObjectFile* obj_file)
60 return new SymbolFileSymtab(obj_file);
64 SymbolFileSymtab::GetTypes (SymbolContextScope *sc_scope, uint32_t type_mask, lldb_private::TypeList &type_list)
69 SymbolFileSymtab::SymbolFileSymtab(ObjectFile* obj_file) :
74 m_objc_class_name_to_index ()
78 SymbolFileSymtab::~SymbolFileSymtab()
83 SymbolFileSymtab::GetClangASTContext ()
85 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext();
91 SymbolFileSymtab::CalculateAbilities ()
93 uint32_t abilities = 0;
96 const Symtab *symtab = m_obj_file->GetSymtab();
99 //----------------------------------------------------------------------
100 // The snippet of code below will get the indexes the module symbol
101 // table entries that are code, data, or function related (debug info),
102 // sort them by value (address) and dump the sorted symbols.
103 //----------------------------------------------------------------------
104 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeSourceFile, m_source_indexes))
106 abilities |= CompileUnits;
109 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny, m_func_indexes))
111 symtab->SortSymbolIndexesByValue(m_func_indexes, true);
112 abilities |= Functions;
115 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugNo, Symtab::eVisibilityAny, m_code_indexes))
117 symtab->SortSymbolIndexesByValue(m_code_indexes, true);
120 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeData, m_data_indexes))
122 symtab->SortSymbolIndexesByValue(m_data_indexes, true);
123 abilities |= GlobalVariables;
126 lldb_private::Symtab::IndexCollection objc_class_indexes;
127 if (symtab->AppendSymbolIndexesWithType (eSymbolTypeObjCClass, objc_class_indexes))
129 symtab->AppendSymbolNamesToMap (objc_class_indexes,
132 m_objc_class_name_to_index);
133 m_objc_class_name_to_index.Sort();
141 SymbolFileSymtab::GetNumCompileUnits()
143 // If we don't have any source file symbols we will just have one compile unit for
144 // the entire object file
145 if (m_source_indexes.empty())
148 // If we have any source file symbols we will logically orgnize the object symbols
150 return m_source_indexes.size();
154 SymbolFileSymtab::ParseCompileUnitAtIndex(uint32_t idx)
158 // If we don't have any source file symbols we will just have one compile unit for
159 // the entire object file
160 if (idx < m_source_indexes.size())
162 const Symbol *cu_symbol = m_obj_file->GetSymtab()->SymbolAtIndex(m_source_indexes[idx]);
164 cu_sp.reset(new CompileUnit (m_obj_file->GetModule(), NULL, cu_symbol->GetMangled().GetName().AsCString(), 0, eLanguageTypeUnknown));
170 SymbolFileSymtab::ParseCompileUnitLanguage (const SymbolContext& sc)
172 return eLanguageTypeUnknown;
177 SymbolFileSymtab::ParseCompileUnitFunctions (const SymbolContext &sc)
179 size_t num_added = 0;
180 // We must at least have a valid compile unit
181 assert (sc.comp_unit != NULL);
182 const Symtab *symtab = m_obj_file->GetSymtab();
183 const Symbol *curr_symbol = NULL;
184 const Symbol *next_symbol = NULL;
185 // const char *prefix = m_obj_file->SymbolPrefix();
186 // if (prefix == NULL)
189 // const uint32_t prefix_len = strlen(prefix);
191 // If we don't have any source file symbols we will just have one compile unit for
192 // the entire object file
193 if (m_source_indexes.empty())
195 // The only time we will have a user ID of zero is when we don't have
196 // and source file symbols and we declare one compile unit for the
197 // entire object file
198 if (!m_func_indexes.empty())
203 if (!m_code_indexes.empty())
205 // StreamFile s(stdout);
206 // symtab->Dump(&s, m_code_indexes);
208 uint32_t idx = 0; // Index into the indexes
209 const uint32_t num_indexes = m_code_indexes.size();
210 for (idx = 0; idx < num_indexes; ++idx)
212 uint32_t symbol_idx = m_code_indexes[idx];
213 curr_symbol = symtab->SymbolAtIndex(symbol_idx);
216 // Union of all ranges in the function DIE (if the function is discontiguous)
217 AddressRange func_range(curr_symbol->GetAddress(), 0);
218 if (func_range.GetBaseAddress().IsSectionOffset())
220 uint32_t symbol_size = curr_symbol->GetByteSize();
221 if (symbol_size != 0 && !curr_symbol->GetSizeIsSibling())
222 func_range.SetByteSize(symbol_size);
223 else if (idx + 1 < num_indexes)
225 next_symbol = symtab->SymbolAtIndex(m_code_indexes[idx + 1]);
228 func_range.SetByteSize(next_symbol->GetAddress().GetOffset() - curr_symbol->GetAddress().GetOffset());
232 FunctionSP func_sp(new Function(sc.comp_unit,
233 symbol_idx, // UserID is the DIE offset
234 LLDB_INVALID_UID, // We don't have any type info for this function
235 curr_symbol->GetMangled(), // Linker/mangled name
236 NULL, // no return type for a code symbol...
237 func_range)); // first address range
239 if (func_sp.get() != NULL)
241 sc.comp_unit->AddFunction(func_sp);
258 SymbolFileSymtab::ParseCompileUnitLineTable (const SymbolContext &sc)
264 SymbolFileSymtab::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList &support_files)
270 SymbolFileSymtab::ParseFunctionBlocks (const SymbolContext &sc)
277 SymbolFileSymtab::ParseTypes (const SymbolContext &sc)
284 SymbolFileSymtab::ParseVariablesForContext (const SymbolContext& sc)
290 SymbolFileSymtab::ResolveTypeUID(lldb::user_id_t type_uid)
296 SymbolFileSymtab::ResolveClangOpaqueTypeDefinition (lldb_private::ClangASTType& clang_opaque_type)
302 SymbolFileSymtab::FindNamespace (const SymbolContext& sc, const ConstString &name, const ClangNamespaceDecl *namespace_decl)
304 return ClangNamespaceDecl();
308 SymbolFileSymtab::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
310 if (m_obj_file->GetSymtab() == NULL)
313 uint32_t resolved_flags = 0;
314 if (resolve_scope & eSymbolContextSymbol)
316 sc.symbol = m_obj_file->GetSymtab()->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
318 resolved_flags |= eSymbolContextSymbol;
320 return resolved_flags;
324 SymbolFileSymtab::ResolveSymbolContext (const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
330 SymbolFileSymtab::FindGlobalVariables(const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables)
336 SymbolFileSymtab::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
342 SymbolFileSymtab::FindFunctions(const ConstString &name, const ClangNamespaceDecl *namespace_decl, uint32_t name_type_mask, bool include_inlines, bool append, SymbolContextList& sc_list)
344 Timer scoped_timer (__PRETTY_FUNCTION__,
345 "SymbolFileSymtab::FindFunctions (name = '%s')",
347 // If we ever support finding STABS or COFF debug info symbols,
348 // we will need to add support here. We are not trying to find symbols
349 // here, just "lldb_private::Function" objects that come from complete
350 // debug information. Any symbol queries should go through the symbol
351 // table itself in the module's object file.
356 SymbolFileSymtab::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
358 Timer scoped_timer (__PRETTY_FUNCTION__,
359 "SymbolFileSymtab::FindFunctions (regex = '%s')",
361 // If we ever support finding STABS or COFF debug info symbols,
362 // we will need to add support here. We are not trying to find symbols
363 // here, just "lldb_private::Function" objects that come from complete
364 // debug information. Any symbol queries should go through the symbol
365 // table itself in the module's object file.
369 static int CountMethodArgs(const char *method_signature)
373 for (const char *colon_pos = strchr(method_signature, ':');
375 colon_pos = strchr(colon_pos + 1, ':'))
384 SymbolFileSymtab::FindTypes (const lldb_private::SymbolContext& sc,
385 const lldb_private::ConstString &name,
386 const ClangNamespaceDecl *namespace_decl,
388 uint32_t max_matches,
389 lldb_private::TypeList& types)
394 //------------------------------------------------------------------
395 // PluginInterface protocol
396 //------------------------------------------------------------------
397 lldb_private::ConstString
398 SymbolFileSymtab::GetPluginName()
400 return GetPluginNameStatic();
404 SymbolFileSymtab::GetPluginVersion()