blob: 1e1a0f203f3d79865f8a5dd40fa97b9c58f1b751 [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"
Greg Clayton1f746072012-08-29 21:13:06 +000015#include "lldb/Symbol/CompileUnit.h"
16#include "lldb/Symbol/Function.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Symbol/ObjectFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Symbol/ObjectFile.h"
19#include "lldb/Symbol/Symbol.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Symbol/SymbolContext.h"
Greg Clayton1f746072012-08-29 21:13:06 +000021#include "lldb/Symbol/Symtab.h"
22#include "lldb/Symbol/TypeList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023
24using namespace lldb;
25using namespace lldb_private;
26
27void
28SymbolFileSymtab::Initialize()
29{
30 PluginManager::RegisterPlugin (GetPluginNameStatic(),
31 GetPluginDescriptionStatic(),
32 CreateInstance);
33}
34
35void
36SymbolFileSymtab::Terminate()
37{
38 PluginManager::UnregisterPlugin (CreateInstance);
39}
40
41
42const char *
43SymbolFileSymtab::GetPluginNameStatic()
44{
45 return "symbol-file.symtab";
46}
47
48const char *
49SymbolFileSymtab::GetPluginDescriptionStatic()
50{
51 return "Reads debug symbols from an object file's symbol table.";
52}
53
54
55SymbolFile*
56SymbolFileSymtab::CreateInstance (ObjectFile* obj_file)
57{
58 return new SymbolFileSymtab(obj_file);
59}
60
61SymbolFileSymtab::SymbolFileSymtab(ObjectFile* obj_file) :
62 SymbolFile(obj_file),
63 m_source_indexes(),
64 m_func_indexes(),
65 m_code_indexes(),
Greg Clayton1075aca2011-12-03 20:02:42 +000066 m_objc_class_name_to_index ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067{
68}
69
70SymbolFileSymtab::~SymbolFileSymtab()
71{
72}
73
Sean Callanan09ab4b72011-11-30 22:11:59 +000074ClangASTContext &
75SymbolFileSymtab::GetClangASTContext ()
76{
77 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext();
78
79 return ast;
80}
81
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +000083SymbolFileSymtab::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084{
85 uint32_t abilities = 0;
Greg Clayton5861d3e2011-06-19 04:02:02 +000086 if (m_obj_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087 {
Greg Clayton5861d3e2011-06-19 04:02:02 +000088 const Symtab *symtab = m_obj_file->GetSymtab();
89 if (symtab)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000090 {
Greg Clayton5861d3e2011-06-19 04:02:02 +000091 //----------------------------------------------------------------------
92 // The snippet of code below will get the indexes the module symbol
93 // table entries that are code, data, or function related (debug info),
94 // sort them by value (address) and dump the sorted symbols.
95 //----------------------------------------------------------------------
Greg Clayton1075aca2011-12-03 20:02:42 +000096 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeSourceFile, m_source_indexes))
Greg Clayton5861d3e2011-06-19 04:02:02 +000097 {
98 abilities |= CompileUnits;
99 }
Greg Clayton1075aca2011-12-03 20:02:42 +0000100
101 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny, m_func_indexes))
Greg Clayton5861d3e2011-06-19 04:02:02 +0000102 {
103 symtab->SortSymbolIndexesByValue(m_func_indexes, true);
104 abilities |= Functions;
105 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106
Greg Clayton1075aca2011-12-03 20:02:42 +0000107 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugNo, Symtab::eVisibilityAny, m_code_indexes))
Greg Clayton5861d3e2011-06-19 04:02:02 +0000108 {
109 symtab->SortSymbolIndexesByValue(m_code_indexes, true);
Greg Clayton5861d3e2011-06-19 04:02:02 +0000110 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111
Greg Clayton1075aca2011-12-03 20:02:42 +0000112 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeData, m_data_indexes))
Greg Clayton5861d3e2011-06-19 04:02:02 +0000113 {
114 symtab->SortSymbolIndexesByValue(m_data_indexes, true);
115 abilities |= GlobalVariables;
116 }
Sean Callanan09ab4b72011-11-30 22:11:59 +0000117
Greg Clayton1075aca2011-12-03 20:02:42 +0000118 lldb_private::Symtab::IndexCollection objc_class_indexes;
119 if (symtab->AppendSymbolIndexesWithType (eSymbolTypeObjCClass, objc_class_indexes))
Sean Callanan09ab4b72011-11-30 22:11:59 +0000120 {
Greg Clayton1075aca2011-12-03 20:02:42 +0000121 symtab->AppendSymbolNamesToMap (objc_class_indexes,
122 true,
123 true,
124 m_objc_class_name_to_index);
125 m_objc_class_name_to_index.Sort();
Sean Callanan09ab4b72011-11-30 22:11:59 +0000126 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127 }
128 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129 return abilities;
130}
131
132uint32_t
133SymbolFileSymtab::GetNumCompileUnits()
134{
135 // If we don't have any source file symbols we will just have one compile unit for
136 // the entire object file
137 if (m_source_indexes.empty())
Jim Ingham969795f2011-09-21 01:17:13 +0000138 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139
140 // If we have any source file symbols we will logically orgnize the object symbols
141 // using these.
142 return m_source_indexes.size();
143}
144
145CompUnitSP
146SymbolFileSymtab::ParseCompileUnitAtIndex(uint32_t idx)
147{
148 CompUnitSP cu_sp;
149
150 // If we don't have any source file symbols we will just have one compile unit for
151 // the entire object file
Greg Clayton9efa0762012-04-26 16:53:42 +0000152 if (idx < m_source_indexes.size())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153 {
154 const Symbol *cu_symbol = m_obj_file->GetSymtab()->SymbolAtIndex(m_source_indexes[idx]);
155 if (cu_symbol)
Greg Claytone72dfb32012-02-24 01:59:29 +0000156 cu_sp.reset(new CompileUnit (m_obj_file->GetModule(), NULL, cu_symbol->GetMangled().GetName().AsCString(), 0, eLanguageTypeUnknown));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157 }
158 return cu_sp;
159}
160
Greg Clayton1f746072012-08-29 21:13:06 +0000161lldb::LanguageType
162SymbolFileSymtab::ParseCompileUnitLanguage (const SymbolContext& sc)
163{
164 return eLanguageTypeUnknown;
165}
166
167
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168size_t
169SymbolFileSymtab::ParseCompileUnitFunctions (const SymbolContext &sc)
170{
171 size_t num_added = 0;
172 // We must at least have a valid compile unit
173 assert (sc.comp_unit != NULL);
174 const Symtab *symtab = m_obj_file->GetSymtab();
175 const Symbol *curr_symbol = NULL;
176 const Symbol *next_symbol = NULL;
177// const char *prefix = m_obj_file->SymbolPrefix();
178// if (prefix == NULL)
179// prefix == "";
180//
181// const uint32_t prefix_len = strlen(prefix);
182
183 // If we don't have any source file symbols we will just have one compile unit for
184 // the entire object file
185 if (m_source_indexes.empty())
186 {
187 // The only time we will have a user ID of zero is when we don't have
188 // and source file symbols and we declare one compile unit for the
189 // entire object file
190 if (!m_func_indexes.empty())
191 {
192
193 }
194
195 if (!m_code_indexes.empty())
196 {
197// StreamFile s(stdout);
198// symtab->Dump(&s, m_code_indexes);
199
200 uint32_t idx = 0; // Index into the indexes
201 const uint32_t num_indexes = m_code_indexes.size();
202 for (idx = 0; idx < num_indexes; ++idx)
203 {
204 uint32_t symbol_idx = m_code_indexes[idx];
205 curr_symbol = symtab->SymbolAtIndex(symbol_idx);
206 if (curr_symbol)
207 {
208 // Union of all ranges in the function DIE (if the function is discontiguous)
Greg Claytone7612132012-03-07 21:03:09 +0000209 AddressRange func_range(curr_symbol->GetAddress(), 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210 if (func_range.GetBaseAddress().IsSectionOffset())
211 {
212 uint32_t symbol_size = curr_symbol->GetByteSize();
213 if (symbol_size != 0 && !curr_symbol->GetSizeIsSibling())
214 func_range.SetByteSize(symbol_size);
215 else if (idx + 1 < num_indexes)
216 {
217 next_symbol = symtab->SymbolAtIndex(m_code_indexes[idx + 1]);
218 if (next_symbol)
219 {
Greg Claytone7612132012-03-07 21:03:09 +0000220 func_range.SetByteSize(next_symbol->GetAddress().GetOffset() - curr_symbol->GetAddress().GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000221 }
222 }
223
224 FunctionSP func_sp(new Function(sc.comp_unit,
225 symbol_idx, // UserID is the DIE offset
226 LLDB_INVALID_UID, // We don't have any type info for this function
227 curr_symbol->GetMangled(), // Linker/mangled name
228 NULL, // no return type for a code symbol...
229 func_range)); // first address range
230
231 if (func_sp.get() != NULL)
232 {
233 sc.comp_unit->AddFunction(func_sp);
234 ++num_added;
235 }
236 }
237 }
238 }
239
240 }
241 }
242 else
243 {
244 // We assume we
245 }
246 return num_added;
247}
248
249bool
250SymbolFileSymtab::ParseCompileUnitLineTable (const SymbolContext &sc)
251{
252 return false;
253}
254
255bool
256SymbolFileSymtab::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList &support_files)
257{
258 return false;
259}
260
261size_t
262SymbolFileSymtab::ParseFunctionBlocks (const SymbolContext &sc)
263{
264 return 0;
265}
266
267
268size_t
269SymbolFileSymtab::ParseTypes (const SymbolContext &sc)
270{
271 return 0;
272}
273
274
275size_t
276SymbolFileSymtab::ParseVariablesForContext (const SymbolContext& sc)
277{
278 return 0;
279}
280
281Type*
282SymbolFileSymtab::ResolveTypeUID(lldb::user_id_t type_uid)
283{
284 return NULL;
285}
286
Greg Clayton1be10fc2010-09-29 01:12:09 +0000287lldb::clang_type_t
288SymbolFileSymtab::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_Type)
289{
290 return NULL;
291}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000292
Greg Clayton526e5af2010-11-13 03:52:47 +0000293ClangNamespaceDecl
Sean Callanan213fdb82011-10-13 01:49:10 +0000294SymbolFileSymtab::FindNamespace (const SymbolContext& sc, const ConstString &name, const ClangNamespaceDecl *namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +0000295{
Greg Clayton526e5af2010-11-13 03:52:47 +0000296 return ClangNamespaceDecl();
Greg Clayton96d7d742010-11-10 23:42:09 +0000297}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298
299uint32_t
300SymbolFileSymtab::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
301{
302 if (m_obj_file->GetSymtab() == NULL)
303 return 0;
304
305 uint32_t resolved_flags = 0;
306 if (resolve_scope & eSymbolContextSymbol)
307 {
308 sc.symbol = m_obj_file->GetSymtab()->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
309 if (sc.symbol)
310 resolved_flags |= eSymbolContextSymbol;
311 }
312 return resolved_flags;
313}
314
315uint32_t
316SymbolFileSymtab::ResolveSymbolContext (const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
317{
318 return 0;
319}
320
321uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +0000322SymbolFileSymtab::FindGlobalVariables(const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323{
324 return 0;
325}
326
327uint32_t
328SymbolFileSymtab::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
329{
330 return 0;
331}
332
333uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +0000334SymbolFileSymtab::FindFunctions(const ConstString &name, const ClangNamespaceDecl *namespace_decl, uint32_t name_type_mask, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335{
336 Timer scoped_timer (__PRETTY_FUNCTION__,
337 "SymbolFileSymtab::FindFunctions (name = '%s')",
338 name.GetCString());
Greg Clayton931180e2011-01-27 06:44:37 +0000339 // If we ever support finding STABS or COFF debug info symbols,
340 // we will need to add support here. We are not trying to find symbols
341 // here, just "lldb_private::Function" objects that come from complete
342 // debug information. Any symbol queries should go through the symbol
343 // table itself in the module's object file.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344 return 0;
345}
346
347uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +0000348SymbolFileSymtab::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349{
350 Timer scoped_timer (__PRETTY_FUNCTION__,
351 "SymbolFileSymtab::FindFunctions (regex = '%s')",
352 regex.GetText());
Greg Clayton931180e2011-01-27 06:44:37 +0000353 // If we ever support finding STABS or COFF debug info symbols,
354 // we will need to add support here. We are not trying to find symbols
355 // here, just "lldb_private::Function" objects that come from complete
356 // debug information. Any symbol queries should go through the symbol
357 // table itself in the module's object file.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358 return 0;
359}
360
Sean Callanan596ab8e2011-12-02 03:41:39 +0000361static int CountMethodArgs(const char *method_signature)
362{
363 int num_args = 0;
364
365 for (const char *colon_pos = strchr(method_signature, ':');
366 colon_pos != NULL;
367 colon_pos = strchr(colon_pos + 1, ':'))
368 {
369 num_args++;
370 }
371
372 return num_args;
373}
374
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000375uint32_t
Greg Clayton1075aca2011-12-03 20:02:42 +0000376SymbolFileSymtab::FindTypes (const lldb_private::SymbolContext& sc,
377 const lldb_private::ConstString &name,
378 const ClangNamespaceDecl *namespace_decl,
379 bool append,
380 uint32_t max_matches,
381 lldb_private::TypeList& types)
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000382{
383 if (!append)
384 types.Clear();
Sean Callanan6c62c832011-12-08 02:08:40 +0000385
Greg Clayton1075aca2011-12-03 20:02:42 +0000386 if (!m_objc_class_name_to_index.IsEmpty())
Sean Callanan09ab4b72011-11-30 22:11:59 +0000387 {
Sean Callanan3ed3bca2011-12-02 18:06:45 +0000388 TypeMap::iterator iter = m_objc_class_types.find(name);
Sean Callanan596ab8e2011-12-02 03:41:39 +0000389
390 if (iter != m_objc_class_types.end())
391 {
392 types.Insert(iter->second);
393 return 1;
394 }
Sean Callanan09ab4b72011-11-30 22:11:59 +0000395
Greg Clayton1075aca2011-12-03 20:02:42 +0000396 const Symtab::NameToIndexMap::Entry *match = m_objc_class_name_to_index.FindFirstValueForName(name.GetCString());
397
398 if (match == NULL)
399 return 0;
400
Sean Callanan6c62c832011-12-08 02:08:40 +0000401 const bool isForwardDecl = false;
Sean Callanan09ab4b72011-11-30 22:11:59 +0000402 const bool isInternal = true;
403
Greg Clayton1075aca2011-12-03 20:02:42 +0000404 ClangASTContext &ast = GetClangASTContext();
Sean Callanan09ab4b72011-11-30 22:11:59 +0000405
Greg Clayton1075aca2011-12-03 20:02:42 +0000406 lldb::clang_type_t objc_object_type = ast.CreateObjCClass (name.AsCString(),
407 ast.GetTranslationUnitDecl(),
408 isForwardDecl,
Sean Callananad880762012-04-18 01:06:17 +0000409 isInternal,
410 0xffaaffaaffaaffaall);
Sean Callanan596ab8e2011-12-02 03:41:39 +0000411
Sean Callanan09ab4b72011-11-30 22:11:59 +0000412 Declaration decl;
413
Greg Claytone1cd1be2012-01-29 20:56:30 +0000414 lldb::TypeSP type(new Type (match->value,
Sean Callanan09ab4b72011-11-30 22:11:59 +0000415 this,
416 name,
Jim Ingham18f46292012-01-12 22:45:31 +0000417 0, // byte_size - don't change this from 0, we currently use that to identify these "synthetic" ObjC class types.
Greg Clayton1075aca2011-12-03 20:02:42 +0000418 NULL, // SymbolContextScope*
419 0, // encoding_uid
Sean Callanan09ab4b72011-11-30 22:11:59 +0000420 Type::eEncodingInvalid,
421 decl,
422 objc_object_type,
Sean Callanan596ab8e2011-12-02 03:41:39 +0000423 Type::eResolveStateFull));
424
Sean Callanan3ed3bca2011-12-02 18:06:45 +0000425 m_objc_class_types[name] = type;
Sean Callanan09ab4b72011-11-30 22:11:59 +0000426
427 types.Insert(type);
428
429 return 1;
430 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000431
432 return 0;
433}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434//
435//uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000436//SymbolFileSymtab::FindTypes(const SymbolContext& sc, const RegularExpression& regex, bool append, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437//{
438// return 0;
439//}
440
441
442//------------------------------------------------------------------
443// PluginInterface protocol
444//------------------------------------------------------------------
445const char *
446SymbolFileSymtab::GetPluginName()
447{
448 return "SymbolFileSymtab";
449}
450
451const char *
452SymbolFileSymtab::GetShortPluginName()
453{
454 return GetPluginNameStatic();
455}
456
457uint32_t
458SymbolFileSymtab::GetPluginVersion()
459{
460 return 1;
461}