1 //===-- SymbolVendor.mm -----------------------------------------*- 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/SymbolVendor.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/PluginManager.h"
18 #include "lldb/Core/Stream.h"
19 #include "lldb/Symbol/CompileUnit.h"
20 #include "lldb/Symbol/ObjectFile.h"
21 #include "lldb/Symbol/SymbolFile.h"
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
26 
27 //----------------------------------------------------------------------
28 // FindPlugin
29 //
30 // Platforms can register a callback to use when creating symbol
31 // vendors to allow for complex debug information file setups, and to
32 // also allow for finding separate debug information files.
33 //----------------------------------------------------------------------
34 SymbolVendor*
35 SymbolVendor::FindPlugin (const lldb::ModuleSP &module_sp, lldb_private::Stream *feedback_strm)
36 {
37     std::unique_ptr<SymbolVendor> instance_ap;
38     SymbolVendorCreateInstance create_callback;
39 
40     for (size_t idx = 0; (create_callback = PluginManager::GetSymbolVendorCreateCallbackAtIndex(idx)) != nullptr; ++idx)
41     {
42         instance_ap.reset(create_callback(module_sp, feedback_strm));
43 
44         if (instance_ap.get())
45         {
46             return instance_ap.release();
47         }
48     }
49     // The default implementation just tries to create debug information using the
50     // file representation for the module.
51     instance_ap.reset(new SymbolVendor(module_sp));
52     if (instance_ap.get())
53     {
54         ObjectFile *objfile = module_sp->GetObjectFile();
55         if (objfile)
56             instance_ap->AddSymbolFileRepresentation(objfile->shared_from_this());
57     }
58     return instance_ap.release();
59 }
60 
61 //----------------------------------------------------------------------
62 // SymbolVendor constructor
63 //----------------------------------------------------------------------
64 SymbolVendor::SymbolVendor(const lldb::ModuleSP &module_sp) :
65     ModuleChild (module_sp),
66     m_type_list(),
67     m_compile_units(),
68     m_sym_file_ap()
69 {
70 }
71 
72 //----------------------------------------------------------------------
73 // Destructor
74 //----------------------------------------------------------------------
75 SymbolVendor::~SymbolVendor()
76 {
77 }
78 
79 //----------------------------------------------------------------------
80 // Add a representation given an object file.
81 //----------------------------------------------------------------------
82 void
83 SymbolVendor::AddSymbolFileRepresentation(const ObjectFileSP &objfile_sp)
84 {
85     ModuleSP module_sp(GetModule());
86     if (module_sp)
87     {
88         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
89         if (objfile_sp)
90         {
91             m_objfile_sp = objfile_sp;
92             m_sym_file_ap.reset(SymbolFile::FindPlugin(objfile_sp.get()));
93         }
94     }
95 }
96 
97 bool
98 SymbolVendor::SetCompileUnitAtIndex (size_t idx, const CompUnitSP &cu_sp)
99 {
100     ModuleSP module_sp(GetModule());
101     if (module_sp)
102     {
103         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
104         const size_t num_compile_units = GetNumCompileUnits();
105         if (idx < num_compile_units)
106         {
107             // Fire off an assertion if this compile unit already exists for now.
108             // The partial parsing should take care of only setting the compile
109             // unit once, so if this assertion fails, we need to make sure that
110             // we don't have a race condition, or have a second parse of the same
111             // compile unit.
112             assert(m_compile_units[idx].get() == nullptr);
113             m_compile_units[idx] = cu_sp;
114             return true;
115         }
116         else
117         {
118             // This should NOT happen, and if it does, we want to crash and know
119             // about it
120             assert (idx < num_compile_units);
121         }
122     }
123     return false;
124 }
125 
126 size_t
127 SymbolVendor::GetNumCompileUnits()
128 {
129     ModuleSP module_sp(GetModule());
130     if (module_sp)
131     {
132         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
133         if (m_compile_units.empty())
134         {
135             if (m_sym_file_ap.get())
136             {
137                 // Resize our array of compile unit shared pointers -- which will
138                 // each remain NULL until someone asks for the actual compile unit
139                 // information. When this happens, the symbol file will be asked
140                 // to parse this compile unit information.
141                 m_compile_units.resize(m_sym_file_ap->GetNumCompileUnits());
142             }
143         }
144     }
145     return m_compile_units.size();
146 }
147 
148 lldb::LanguageType
149 SymbolVendor::ParseCompileUnitLanguage (const SymbolContext& sc)
150 {
151     ModuleSP module_sp(GetModule());
152     if (module_sp)
153     {
154         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
155         if (m_sym_file_ap.get())
156             return m_sym_file_ap->ParseCompileUnitLanguage(sc);
157     }
158     return eLanguageTypeUnknown;
159 }
160 
161 
162 size_t
163 SymbolVendor::ParseCompileUnitFunctions (const SymbolContext &sc)
164 {
165     ModuleSP module_sp(GetModule());
166     if (module_sp)
167     {
168         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
169         if (m_sym_file_ap.get())
170             return m_sym_file_ap->ParseCompileUnitFunctions(sc);
171     }
172     return 0;
173 }
174 
175 bool
176 SymbolVendor::ParseCompileUnitLineTable (const SymbolContext &sc)
177 {
178     ModuleSP module_sp(GetModule());
179     if (module_sp)
180     {
181         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
182         if (m_sym_file_ap.get())
183             return m_sym_file_ap->ParseCompileUnitLineTable(sc);
184     }
185     return false;
186 }
187 
188 bool
189 SymbolVendor::ParseCompileUnitDebugMacros (const SymbolContext &sc)
190 {
191     ModuleSP module_sp(GetModule());
192     if (module_sp)
193     {
194         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
195         if (m_sym_file_ap.get())
196             return m_sym_file_ap->ParseCompileUnitDebugMacros(sc);
197     }
198     return false;
199 }
200 bool
201 SymbolVendor::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
202 {
203     ModuleSP module_sp(GetModule());
204     if (module_sp)
205     {
206         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
207         if (m_sym_file_ap.get())
208             return m_sym_file_ap->ParseCompileUnitSupportFiles(sc, support_files);
209     }
210     return false;
211 }
212 
213 bool
214 SymbolVendor::ParseCompileUnitIsOptimized(const SymbolContext &sc)
215 {
216     ModuleSP module_sp(GetModule());
217     if (module_sp)
218     {
219         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
220         if (m_sym_file_ap.get())
221             return m_sym_file_ap->ParseCompileUnitIsOptimized(sc);
222     }
223     return false;
224 }
225 
226 bool
227 SymbolVendor::ParseImportedModules(const SymbolContext &sc, std::vector<ConstString> &imported_modules)
228 {
229     ModuleSP module_sp(GetModule());
230     if (module_sp)
231     {
232         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
233         if (m_sym_file_ap.get())
234             return m_sym_file_ap->ParseImportedModules(sc, imported_modules);
235     }
236     return false;
237 
238 }
239 
240 size_t
241 SymbolVendor::ParseFunctionBlocks (const SymbolContext &sc)
242 {
243     ModuleSP module_sp(GetModule());
244     if (module_sp)
245     {
246         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
247         if (m_sym_file_ap.get())
248             return m_sym_file_ap->ParseFunctionBlocks(sc);
249     }
250     return 0;
251 }
252 
253 size_t
254 SymbolVendor::ParseTypes (const SymbolContext &sc)
255 {
256     ModuleSP module_sp(GetModule());
257     if (module_sp)
258     {
259         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
260         if (m_sym_file_ap.get())
261             return m_sym_file_ap->ParseTypes(sc);
262     }
263     return 0;
264 }
265 
266 size_t
267 SymbolVendor::ParseVariablesForContext (const SymbolContext& sc)
268 {
269     ModuleSP module_sp(GetModule());
270     if (module_sp)
271     {
272         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
273         if (m_sym_file_ap.get())
274             return m_sym_file_ap->ParseVariablesForContext(sc);
275     }
276     return 0;
277 }
278 
279 Type*
280 SymbolVendor::ResolveTypeUID(lldb::user_id_t type_uid)
281 {
282     ModuleSP module_sp(GetModule());
283     if (module_sp)
284     {
285         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
286         if (m_sym_file_ap.get())
287             return m_sym_file_ap->ResolveTypeUID(type_uid);
288     }
289     return nullptr;
290 }
291 
292 
293 uint32_t
294 SymbolVendor::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
295 {
296     ModuleSP module_sp(GetModule());
297     if (module_sp)
298     {
299         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
300         if (m_sym_file_ap.get())
301             return m_sym_file_ap->ResolveSymbolContext(so_addr, resolve_scope, sc);
302     }
303     return 0;
304 }
305 
306 uint32_t
307 SymbolVendor::ResolveSymbolContext (const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
308 {
309     ModuleSP module_sp(GetModule());
310     if (module_sp)
311     {
312         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
313         if (m_sym_file_ap.get())
314             return m_sym_file_ap->ResolveSymbolContext(file_spec, line, check_inlines, resolve_scope, sc_list);
315     }
316     return 0;
317 }
318 
319 size_t
320 SymbolVendor::FindGlobalVariables (const ConstString &name, const CompilerDeclContext *parent_decl_ctx, bool append, size_t max_matches, VariableList& variables)
321 {
322     ModuleSP module_sp(GetModule());
323     if (module_sp)
324     {
325         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
326         if (m_sym_file_ap.get())
327             return m_sym_file_ap->FindGlobalVariables(name, parent_decl_ctx, append, max_matches, variables);
328     }
329     return 0;
330 }
331 
332 size_t
333 SymbolVendor::FindGlobalVariables (const RegularExpression& regex, bool append, size_t max_matches, VariableList& variables)
334 {
335     ModuleSP module_sp(GetModule());
336     if (module_sp)
337     {
338         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
339         if (m_sym_file_ap.get())
340             return m_sym_file_ap->FindGlobalVariables(regex, append, max_matches, variables);
341     }
342     return 0;
343 }
344 
345 size_t
346 SymbolVendor::FindFunctions(const ConstString &name, const CompilerDeclContext *parent_decl_ctx, uint32_t name_type_mask, bool include_inlines, bool append, SymbolContextList& sc_list)
347 {
348     ModuleSP module_sp(GetModule());
349     if (module_sp)
350     {
351         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
352         if (m_sym_file_ap.get())
353             return m_sym_file_ap->FindFunctions(name, parent_decl_ctx, name_type_mask, include_inlines, append, sc_list);
354     }
355     return 0;
356 }
357 
358 size_t
359 SymbolVendor::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
360 {
361     ModuleSP module_sp(GetModule());
362     if (module_sp)
363     {
364         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
365         if (m_sym_file_ap.get())
366             return m_sym_file_ap->FindFunctions(regex, include_inlines, append, sc_list);
367     }
368     return 0;
369 }
370 
371 
372 size_t
373 SymbolVendor::FindTypes (const SymbolContext& sc, const ConstString &name, const CompilerDeclContext *parent_decl_ctx, bool append, size_t max_matches, llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, TypeMap& types)
374 {
375     ModuleSP module_sp(GetModule());
376     if (module_sp)
377     {
378         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
379         if (m_sym_file_ap.get())
380             return m_sym_file_ap->FindTypes(sc, name, parent_decl_ctx, append, max_matches, searched_symbol_files, types);
381     }
382     if (!append)
383         types.Clear();
384     return 0;
385 }
386 
387 size_t
388 SymbolVendor::FindTypes (const std::vector<CompilerContext> &context, bool append, TypeMap& types)
389 {
390     ModuleSP module_sp(GetModule());
391     if (module_sp)
392     {
393         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
394         if (m_sym_file_ap.get())
395             return m_sym_file_ap->FindTypes(context, append, types);
396     }
397     if (!append)
398         types.Clear();
399     return 0;
400 }
401 
402 size_t
403 SymbolVendor::GetTypes (SymbolContextScope *sc_scope,
404                         uint32_t type_mask,
405                         lldb_private::TypeList &type_list)
406 {
407     ModuleSP module_sp(GetModule());
408     if (module_sp)
409     {
410         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
411         if (m_sym_file_ap.get())
412             return m_sym_file_ap->GetTypes (sc_scope, type_mask, type_list);
413     }
414     return 0;
415 }
416 
417 CompilerDeclContext
418 SymbolVendor::FindNamespace(const SymbolContext& sc, const ConstString &name, const CompilerDeclContext *parent_decl_ctx)
419 {
420     CompilerDeclContext namespace_decl_ctx;
421     ModuleSP module_sp(GetModule());
422     if (module_sp)
423     {
424         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
425         if (m_sym_file_ap.get())
426             namespace_decl_ctx = m_sym_file_ap->FindNamespace (sc, name, parent_decl_ctx);
427     }
428     return namespace_decl_ctx;
429 }
430 
431 void
432 SymbolVendor::Dump(Stream *s)
433 {
434     ModuleSP module_sp(GetModule());
435     if (module_sp)
436     {
437         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
438 
439         bool show_context = false;
440 
441         s->Printf("%p: ", static_cast<void*>(this));
442         s->Indent();
443         s->PutCString("SymbolVendor");
444         if (m_sym_file_ap.get())
445         {
446             ObjectFile *objfile = m_sym_file_ap->GetObjectFile();
447             if (objfile)
448             {
449                 const FileSpec &objfile_file_spec = objfile->GetFileSpec();
450                 if (objfile_file_spec)
451                 {
452                     s->PutCString(" (");
453                     objfile_file_spec.Dump(s);
454                     s->PutChar(')');
455                 }
456             }
457         }
458         s->EOL();
459         s->IndentMore();
460         m_type_list.Dump(s, show_context);
461 
462         CompileUnitConstIter cu_pos, cu_end;
463         cu_end = m_compile_units.end();
464         for (cu_pos = m_compile_units.begin(); cu_pos != cu_end; ++cu_pos)
465         {
466             // We currently only dump the compile units that have been parsed
467             if (cu_pos->get())
468                 (*cu_pos)->Dump(s, show_context);
469         }
470 
471         s->IndentLess();
472     }
473 }
474 
475 CompUnitSP
476 SymbolVendor::GetCompileUnitAtIndex(size_t idx)
477 {
478     CompUnitSP cu_sp;
479     ModuleSP module_sp(GetModule());
480     if (module_sp)
481     {
482         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
483         const size_t num_compile_units = GetNumCompileUnits();
484         if (idx < num_compile_units)
485         {
486             cu_sp = m_compile_units[idx];
487             if (cu_sp.get() == nullptr)
488             {
489                 m_compile_units[idx] = m_sym_file_ap->ParseCompileUnitAtIndex(idx);
490                 cu_sp = m_compile_units[idx];
491             }
492         }
493     }
494     return cu_sp;
495 }
496 
497 FileSpec
498 SymbolVendor::GetMainFileSpec() const
499 {
500     if (m_sym_file_ap.get())
501     {
502         const ObjectFile *symfile_objfile = m_sym_file_ap->GetObjectFile();
503         if (symfile_objfile)
504             return symfile_objfile->GetFileSpec();
505     }
506 
507     return FileSpec();
508 }
509 
510 Symtab *
511 SymbolVendor::GetSymtab ()
512 {
513     ModuleSP module_sp(GetModule());
514     if (module_sp)
515     {
516         ObjectFile *objfile = module_sp->GetObjectFile();
517         if (objfile)
518         {
519             // Get symbol table from unified section list.
520             return objfile->GetSymtab ();
521         }
522     }
523     return nullptr;
524 }
525 
526 void
527 SymbolVendor::ClearSymtab()
528 {
529     ModuleSP module_sp(GetModule());
530     if (module_sp)
531     {
532         ObjectFile *objfile = module_sp->GetObjectFile();
533         if (objfile)
534         {
535             // Clear symbol table from unified section list.
536             objfile->ClearSymtab ();
537         }
538     }
539 }
540 
541 void
542 SymbolVendor::SectionFileAddressesChanged ()
543 {
544     ModuleSP module_sp(GetModule());
545     if (module_sp)
546     {
547         ObjectFile *module_objfile = module_sp->GetObjectFile ();
548         if (m_sym_file_ap.get())
549         {
550             ObjectFile *symfile_objfile = m_sym_file_ap->GetObjectFile ();
551             if (symfile_objfile != module_objfile)
552                 symfile_objfile->SectionFileAddressesChanged ();
553         }
554         Symtab *symtab = GetSymtab ();
555         if (symtab)
556         {
557             symtab->SectionFileAddressesChanged ();
558         }
559     }
560 }
561 
562 //------------------------------------------------------------------
563 // PluginInterface protocol
564 //------------------------------------------------------------------
565 lldb_private::ConstString
566 SymbolVendor::GetPluginName()
567 {
568     static ConstString g_name("vendor-default");
569     return g_name;
570 }
571 
572 uint32_t
573 SymbolVendor::GetPluginVersion()
574 {
575     return 1;
576 }
577 
578