blob: c3e02d7573ffff68383a7762718bc560a082c6f2 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SymbolFileSymtab.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 "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/ObjectFile.h"
16#include "lldb/Symbol/Symtab.h"
17#include "lldb/Symbol/ObjectFile.h"
18#include "lldb/Symbol/Symbol.h"
19#include "lldb/Symbol/CompileUnit.h"
20#include "lldb/Symbol/SymbolContext.h"
21#include "lldb/Symbol/Function.h"
22
23using namespace lldb;
24using namespace lldb_private;
25
26void
27SymbolFileSymtab::Initialize()
28{
29 PluginManager::RegisterPlugin (GetPluginNameStatic(),
30 GetPluginDescriptionStatic(),
31 CreateInstance);
32}
33
34void
35SymbolFileSymtab::Terminate()
36{
37 PluginManager::UnregisterPlugin (CreateInstance);
38}
39
40
41const char *
42SymbolFileSymtab::GetPluginNameStatic()
43{
44 return "symbol-file.symtab";
45}
46
47const char *
48SymbolFileSymtab::GetPluginDescriptionStatic()
49{
50 return "Reads debug symbols from an object file's symbol table.";
51}
52
53
54SymbolFile*
55SymbolFileSymtab::CreateInstance (ObjectFile* obj_file)
56{
57 return new SymbolFileSymtab(obj_file);
58}
59
60SymbolFileSymtab::SymbolFileSymtab(ObjectFile* obj_file) :
61 SymbolFile(obj_file),
62 m_source_indexes(),
63 m_func_indexes(),
64 m_code_indexes(),
65 m_data_indexes(),
66 m_addr_indexes()
67{
68}
69
70SymbolFileSymtab::~SymbolFileSymtab()
71{
72}
73
74
75uint32_t
76SymbolFileSymtab::GetAbilities ()
77{
78 uint32_t abilities = 0;
Greg Clayton5861d3e2011-06-19 04:02:02 +000079 if (m_obj_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080 {
Greg Clayton5861d3e2011-06-19 04:02:02 +000081 const Symtab *symtab = m_obj_file->GetSymtab();
82 if (symtab)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084
Greg Clayton5861d3e2011-06-19 04:02:02 +000085 //----------------------------------------------------------------------
86 // The snippet of code below will get the indexes the module symbol
87 // table entries that are code, data, or function related (debug info),
88 // sort them by value (address) and dump the sorted symbols.
89 //----------------------------------------------------------------------
90 symtab->AppendSymbolIndexesWithType(eSymbolTypeSourceFile, m_source_indexes);
91 if (!m_source_indexes.empty())
92 {
93 abilities |= CompileUnits;
94 }
95 symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny, m_func_indexes);
96 if (!m_func_indexes.empty())
97 {
98 symtab->SortSymbolIndexesByValue(m_func_indexes, true);
99 abilities |= Functions;
100 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101
Greg Clayton5861d3e2011-06-19 04:02:02 +0000102 symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugNo, Symtab::eVisibilityAny, m_code_indexes);
103 if (!m_code_indexes.empty())
104 {
105 symtab->SortSymbolIndexesByValue(m_code_indexes, true);
106 abilities |= Labels;
107 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108
Greg Clayton5861d3e2011-06-19 04:02:02 +0000109 symtab->AppendSymbolIndexesWithType(eSymbolTypeData, m_data_indexes);
110
111 if (!m_data_indexes.empty())
112 {
113 symtab->SortSymbolIndexesByValue(m_data_indexes, true);
114 abilities |= GlobalVariables;
115 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116 }
117 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118 return abilities;
119}
120
121uint32_t
122SymbolFileSymtab::GetNumCompileUnits()
123{
124 // If we don't have any source file symbols we will just have one compile unit for
125 // the entire object file
126 if (m_source_indexes.empty())
Jim Ingham969795f2011-09-21 01:17:13 +0000127 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128
129 // If we have any source file symbols we will logically orgnize the object symbols
130 // using these.
131 return m_source_indexes.size();
132}
133
134CompUnitSP
135SymbolFileSymtab::ParseCompileUnitAtIndex(uint32_t idx)
136{
137 CompUnitSP cu_sp;
138
139 // If we don't have any source file symbols we will just have one compile unit for
140 // the entire object file
Jim Ingham969795f2011-09-21 01:17:13 +0000141// if (m_source_indexes.empty())
142// {
143// const FileSpec &obj_file_spec = m_obj_file->GetFileSpec();
144// if (obj_file_spec)
145// cu_sp.reset(new CompileUnit(m_obj_file->GetModule(), NULL, obj_file_spec, 0, eLanguageTypeUnknown));
146//
147// }
148 /* else */ if (idx < m_source_indexes.size())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149 {
150 const Symbol *cu_symbol = m_obj_file->GetSymtab()->SymbolAtIndex(m_source_indexes[idx]);
151 if (cu_symbol)
Greg Clayton9e409562010-07-28 02:04:09 +0000152 cu_sp.reset(new CompileUnit(m_obj_file->GetModule(), NULL, cu_symbol->GetMangled().GetName().AsCString(), 0, eLanguageTypeUnknown));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153 }
154 return cu_sp;
155}
156
157size_t
158SymbolFileSymtab::ParseCompileUnitFunctions (const SymbolContext &sc)
159{
160 size_t num_added = 0;
161 // We must at least have a valid compile unit
162 assert (sc.comp_unit != NULL);
163 const Symtab *symtab = m_obj_file->GetSymtab();
164 const Symbol *curr_symbol = NULL;
165 const Symbol *next_symbol = NULL;
166// const char *prefix = m_obj_file->SymbolPrefix();
167// if (prefix == NULL)
168// prefix == "";
169//
170// const uint32_t prefix_len = strlen(prefix);
171
172 // If we don't have any source file symbols we will just have one compile unit for
173 // the entire object file
174 if (m_source_indexes.empty())
175 {
176 // The only time we will have a user ID of zero is when we don't have
177 // and source file symbols and we declare one compile unit for the
178 // entire object file
179 if (!m_func_indexes.empty())
180 {
181
182 }
183
184 if (!m_code_indexes.empty())
185 {
186// StreamFile s(stdout);
187// symtab->Dump(&s, m_code_indexes);
188
189 uint32_t idx = 0; // Index into the indexes
190 const uint32_t num_indexes = m_code_indexes.size();
191 for (idx = 0; idx < num_indexes; ++idx)
192 {
193 uint32_t symbol_idx = m_code_indexes[idx];
194 curr_symbol = symtab->SymbolAtIndex(symbol_idx);
195 if (curr_symbol)
196 {
197 // Union of all ranges in the function DIE (if the function is discontiguous)
198 AddressRange func_range(curr_symbol->GetValue(), 0);
199 if (func_range.GetBaseAddress().IsSectionOffset())
200 {
201 uint32_t symbol_size = curr_symbol->GetByteSize();
202 if (symbol_size != 0 && !curr_symbol->GetSizeIsSibling())
203 func_range.SetByteSize(symbol_size);
204 else if (idx + 1 < num_indexes)
205 {
206 next_symbol = symtab->SymbolAtIndex(m_code_indexes[idx + 1]);
207 if (next_symbol)
208 {
209 func_range.SetByteSize(next_symbol->GetValue().GetOffset() - curr_symbol->GetValue().GetOffset());
210 }
211 }
212
213 FunctionSP func_sp(new Function(sc.comp_unit,
214 symbol_idx, // UserID is the DIE offset
215 LLDB_INVALID_UID, // We don't have any type info for this function
216 curr_symbol->GetMangled(), // Linker/mangled name
217 NULL, // no return type for a code symbol...
218 func_range)); // first address range
219
220 if (func_sp.get() != NULL)
221 {
222 sc.comp_unit->AddFunction(func_sp);
223 ++num_added;
224 }
225 }
226 }
227 }
228
229 }
230 }
231 else
232 {
233 // We assume we
234 }
235 return num_added;
236}
237
238bool
239SymbolFileSymtab::ParseCompileUnitLineTable (const SymbolContext &sc)
240{
241 return false;
242}
243
244bool
245SymbolFileSymtab::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList &support_files)
246{
247 return false;
248}
249
250size_t
251SymbolFileSymtab::ParseFunctionBlocks (const SymbolContext &sc)
252{
253 return 0;
254}
255
256
257size_t
258SymbolFileSymtab::ParseTypes (const SymbolContext &sc)
259{
260 return 0;
261}
262
263
264size_t
265SymbolFileSymtab::ParseVariablesForContext (const SymbolContext& sc)
266{
267 return 0;
268}
269
270Type*
271SymbolFileSymtab::ResolveTypeUID(lldb::user_id_t type_uid)
272{
273 return NULL;
274}
275
Greg Clayton1be10fc2010-09-29 01:12:09 +0000276lldb::clang_type_t
277SymbolFileSymtab::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_Type)
278{
279 return NULL;
280}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281
Greg Clayton526e5af2010-11-13 03:52:47 +0000282ClangNamespaceDecl
Sean Callanan213fdb82011-10-13 01:49:10 +0000283SymbolFileSymtab::FindNamespace (const SymbolContext& sc, const ConstString &name, const ClangNamespaceDecl *namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +0000284{
Greg Clayton526e5af2010-11-13 03:52:47 +0000285 return ClangNamespaceDecl();
Greg Clayton96d7d742010-11-10 23:42:09 +0000286}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287
288uint32_t
289SymbolFileSymtab::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
290{
291 if (m_obj_file->GetSymtab() == NULL)
292 return 0;
293
294 uint32_t resolved_flags = 0;
295 if (resolve_scope & eSymbolContextSymbol)
296 {
297 sc.symbol = m_obj_file->GetSymtab()->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
298 if (sc.symbol)
299 resolved_flags |= eSymbolContextSymbol;
300 }
301 return resolved_flags;
302}
303
304uint32_t
305SymbolFileSymtab::ResolveSymbolContext (const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
306{
307 return 0;
308}
309
310uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +0000311SymbolFileSymtab::FindGlobalVariables(const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312{
313 return 0;
314}
315
316uint32_t
317SymbolFileSymtab::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
318{
319 return 0;
320}
321
322uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +0000323SymbolFileSymtab::FindFunctions(const ConstString &name, const ClangNamespaceDecl *namespace_decl, uint32_t name_type_mask, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324{
325 Timer scoped_timer (__PRETTY_FUNCTION__,
326 "SymbolFileSymtab::FindFunctions (name = '%s')",
327 name.GetCString());
Greg Clayton931180e2011-01-27 06:44:37 +0000328 // If we ever support finding STABS or COFF debug info symbols,
329 // we will need to add support here. We are not trying to find symbols
330 // here, just "lldb_private::Function" objects that come from complete
331 // debug information. Any symbol queries should go through the symbol
332 // table itself in the module's object file.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333 return 0;
334}
335
336uint32_t
337SymbolFileSymtab::FindFunctions(const RegularExpression& regex, bool append, SymbolContextList& sc_list)
338{
339 Timer scoped_timer (__PRETTY_FUNCTION__,
340 "SymbolFileSymtab::FindFunctions (regex = '%s')",
341 regex.GetText());
Greg Clayton931180e2011-01-27 06:44:37 +0000342 // If we ever support finding STABS or COFF debug info symbols,
343 // we will need to add support here. We are not trying to find symbols
344 // here, just "lldb_private::Function" objects that come from complete
345 // debug information. Any symbol queries should go through the symbol
346 // table itself in the module's object file.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000347 return 0;
348}
349
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000350uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +0000351SymbolFileSymtab::FindTypes (const lldb_private::SymbolContext& sc, const lldb_private::ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, lldb_private::TypeList& types)
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000352{
353 if (!append)
354 types.Clear();
355
356 return 0;
357}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358//
359//uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000360//SymbolFileSymtab::FindTypes(const SymbolContext& sc, const RegularExpression& regex, bool append, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000361//{
362// return 0;
363//}
364
365
366//------------------------------------------------------------------
367// PluginInterface protocol
368//------------------------------------------------------------------
369const char *
370SymbolFileSymtab::GetPluginName()
371{
372 return "SymbolFileSymtab";
373}
374
375const char *
376SymbolFileSymtab::GetShortPluginName()
377{
378 return GetPluginNameStatic();
379}
380
381uint32_t
382SymbolFileSymtab::GetPluginVersion()
383{
384 return 1;
385}