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     //----------------------------------------------------------------------
39     // We currently only have one debug symbol parser...
40     //----------------------------------------------------------------------
41     SymbolVendorCreateInstance create_callback;
42     for (size_t idx = 0; (create_callback = PluginManager::GetSymbolVendorCreateCallbackAtIndex(idx)) != NULL; ++idx)
43     {
44         instance_ap.reset(create_callback(module_sp, feedback_strm));
45 
46         if (instance_ap.get())
47         {
48             // TODO: make sure this symbol vendor is what we want. We
49             // currently are just returning the first one we find, but
50             // we may want to call this function only when we have our
51             // main executable module and then give all symbol vendor
52             // plug-ins a chance to compete for who wins.
53             return instance_ap.release();
54         }
55     }
56     // The default implementation just tries to create debug information using the
57     // file representation for the module.
58     instance_ap.reset(new SymbolVendor(module_sp));
59     if (instance_ap.get())
60     {
61         ObjectFile *objfile = module_sp->GetObjectFile();
62         if (objfile)
63             instance_ap->AddSymbolFileRepresentation(objfile->shared_from_this());
64     }
65     return instance_ap.release();
66 }
67 
68 //----------------------------------------------------------------------
69 // SymbolVendor constructor
70 //----------------------------------------------------------------------
71 SymbolVendor::SymbolVendor(const lldb::ModuleSP &module_sp) :
72     ModuleChild (module_sp),
73     m_type_list(),
74     m_compile_units(),
75     m_sym_file_ap()
76 {
77 }
78 
79 //----------------------------------------------------------------------
80 // Destructor
81 //----------------------------------------------------------------------
82 SymbolVendor::~SymbolVendor()
83 {
84 }
85 
86 //----------------------------------------------------------------------
87 // Add a represantion given an object file.
88 //----------------------------------------------------------------------
89 void
90 SymbolVendor::AddSymbolFileRepresentation(const ObjectFileSP &objfile_sp)
91 {
92     ModuleSP module_sp(GetModule());
93     if (module_sp)
94     {
95         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
96         if (objfile_sp)
97         {
98             m_objfile_sp = objfile_sp;
99             m_sym_file_ap.reset(SymbolFile::FindPlugin(objfile_sp.get()));
100         }
101     }
102 }
103 
104 bool
105 SymbolVendor::SetCompileUnitAtIndex (size_t idx, const CompUnitSP &cu_sp)
106 {
107     ModuleSP module_sp(GetModule());
108     if (module_sp)
109     {
110         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
111         const size_t num_compile_units = GetNumCompileUnits();
112         if (idx < num_compile_units)
113         {
114             // Fire off an assertion if this compile unit already exists for now.
115             // The partial parsing should take care of only setting the compile
116             // unit once, so if this assertion fails, we need to make sure that
117             // we don't have a race condition, or have a second parse of the same
118             // compile unit.
119             assert(m_compile_units[idx].get() == NULL);
120             m_compile_units[idx] = cu_sp;
121             return true;
122         }
123         else
124         {
125             // This should NOT happen, and if it does, we want to crash and know
126             // about it
127             assert (idx < num_compile_units);
128         }
129     }
130     return false;
131 }
132 
133 size_t
134 SymbolVendor::GetNumCompileUnits()
135 {
136     ModuleSP module_sp(GetModule());
137     if (module_sp)
138     {
139         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
140         if (m_compile_units.empty())
141         {
142             if (m_sym_file_ap.get())
143             {
144                 // Resize our array of compile unit shared pointers -- which will
145                 // each remain NULL until someone asks for the actual compile unit
146                 // information. When this happens, the symbol file will be asked
147                 // to parse this compile unit information.
148                 m_compile_units.resize(m_sym_file_ap->GetNumCompileUnits());
149             }
150         }
151     }
152     return m_compile_units.size();
153 }
154 
155 lldb::LanguageType
156 SymbolVendor::ParseCompileUnitLanguage (const SymbolContext& sc)
157 {
158     ModuleSP module_sp(GetModule());
159     if (module_sp)
160     {
161         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
162         if (m_sym_file_ap.get())
163             return m_sym_file_ap->ParseCompileUnitLanguage(sc);
164     }
165     return eLanguageTypeUnknown;
166 }
167 
168 
169 size_t
170 SymbolVendor::ParseCompileUnitFunctions (const SymbolContext &sc)
171 {
172     ModuleSP module_sp(GetModule());
173     if (module_sp)
174     {
175         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
176         if (m_sym_file_ap.get())
177             return m_sym_file_ap->ParseCompileUnitFunctions(sc);
178     }
179     return 0;
180 }
181 
182 bool
183 SymbolVendor::ParseCompileUnitLineTable (const SymbolContext &sc)
184 {
185     ModuleSP module_sp(GetModule());
186     if (module_sp)
187     {
188         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
189         if (m_sym_file_ap.get())
190             return m_sym_file_ap->ParseCompileUnitLineTable(sc);
191     }
192     return false;
193 }
194 
195 bool
196 SymbolVendor::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
197 {
198     ModuleSP module_sp(GetModule());
199     if (module_sp)
200     {
201         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
202         if (m_sym_file_ap.get())
203             return m_sym_file_ap->ParseCompileUnitSupportFiles(sc, support_files);
204     }
205     return false;
206 }
207 
208 size_t
209 SymbolVendor::ParseFunctionBlocks (const SymbolContext &sc)
210 {
211     ModuleSP module_sp(GetModule());
212     if (module_sp)
213     {
214         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
215         if (m_sym_file_ap.get())
216             return m_sym_file_ap->ParseFunctionBlocks(sc);
217     }
218     return 0;
219 }
220 
221 size_t
222 SymbolVendor::ParseTypes (const SymbolContext &sc)
223 {
224     ModuleSP module_sp(GetModule());
225     if (module_sp)
226     {
227         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
228         if (m_sym_file_ap.get())
229             return m_sym_file_ap->ParseTypes(sc);
230     }
231     return 0;
232 }
233 
234 size_t
235 SymbolVendor::ParseVariablesForContext (const SymbolContext& sc)
236 {
237     ModuleSP module_sp(GetModule());
238     if (module_sp)
239     {
240         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
241         if (m_sym_file_ap.get())
242             return m_sym_file_ap->ParseVariablesForContext(sc);
243     }
244     return 0;
245 }
246 
247 Type*
248 SymbolVendor::ResolveTypeUID(lldb::user_id_t type_uid)
249 {
250     ModuleSP module_sp(GetModule());
251     if (module_sp)
252     {
253         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
254         if (m_sym_file_ap.get())
255             return m_sym_file_ap->ResolveTypeUID(type_uid);
256     }
257     return NULL;
258 }
259 
260 
261 uint32_t
262 SymbolVendor::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
263 {
264     ModuleSP module_sp(GetModule());
265     if (module_sp)
266     {
267         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
268         if (m_sym_file_ap.get())
269             return m_sym_file_ap->ResolveSymbolContext(so_addr, resolve_scope, sc);
270     }
271     return 0;
272 }
273 
274 uint32_t
275 SymbolVendor::ResolveSymbolContext (const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
276 {
277     ModuleSP module_sp(GetModule());
278     if (module_sp)
279     {
280         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
281         if (m_sym_file_ap.get())
282             return m_sym_file_ap->ResolveSymbolContext(file_spec, line, check_inlines, resolve_scope, sc_list);
283     }
284     return 0;
285 }
286 
287 size_t
288 SymbolVendor::FindGlobalVariables (const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, size_t max_matches, VariableList& variables)
289 {
290     ModuleSP module_sp(GetModule());
291     if (module_sp)
292     {
293         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
294         if (m_sym_file_ap.get())
295             return m_sym_file_ap->FindGlobalVariables(name, namespace_decl, append, max_matches, variables);
296     }
297     return 0;
298 }
299 
300 size_t
301 SymbolVendor::FindGlobalVariables (const RegularExpression& regex, bool append, size_t max_matches, VariableList& variables)
302 {
303     ModuleSP module_sp(GetModule());
304     if (module_sp)
305     {
306         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
307         if (m_sym_file_ap.get())
308             return m_sym_file_ap->FindGlobalVariables(regex, append, max_matches, variables);
309     }
310     return 0;
311 }
312 
313 size_t
314 SymbolVendor::FindFunctions(const ConstString &name, const ClangNamespaceDecl *namespace_decl, uint32_t name_type_mask, bool include_inlines, bool append, SymbolContextList& sc_list)
315 {
316     ModuleSP module_sp(GetModule());
317     if (module_sp)
318     {
319         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
320         if (m_sym_file_ap.get())
321             return m_sym_file_ap->FindFunctions(name, namespace_decl, name_type_mask, include_inlines, append, sc_list);
322     }
323     return 0;
324 }
325 
326 size_t
327 SymbolVendor::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
328 {
329     ModuleSP module_sp(GetModule());
330     if (module_sp)
331     {
332         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
333         if (m_sym_file_ap.get())
334             return m_sym_file_ap->FindFunctions(regex, include_inlines, append, sc_list);
335     }
336     return 0;
337 }
338 
339 
340 size_t
341 SymbolVendor::FindTypes (const SymbolContext& sc, const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, size_t max_matches, TypeList& types)
342 {
343     ModuleSP module_sp(GetModule());
344     if (module_sp)
345     {
346         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
347         if (m_sym_file_ap.get())
348             return m_sym_file_ap->FindTypes(sc, name, namespace_decl, append, max_matches, types);
349     }
350     if (!append)
351         types.Clear();
352     return 0;
353 }
354 
355 size_t
356 SymbolVendor::GetTypes (SymbolContextScope *sc_scope,
357                         uint32_t type_mask,
358                         lldb_private::TypeList &type_list)
359 {
360     ModuleSP module_sp(GetModule());
361     if (module_sp)
362     {
363         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
364         if (m_sym_file_ap.get())
365             return m_sym_file_ap->GetTypes (sc_scope, type_mask, type_list);
366     }
367     return 0;
368 }
369 
370 ClangNamespaceDecl
371 SymbolVendor::FindNamespace(const SymbolContext& sc, const ConstString &name, const ClangNamespaceDecl *parent_namespace_decl)
372 {
373     ClangNamespaceDecl namespace_decl;
374     ModuleSP module_sp(GetModule());
375     if (module_sp)
376     {
377         lldb_private::Mutex::Locker locker(module_sp->GetMutex());
378         if (m_sym_file_ap.get())
379             namespace_decl = m_sym_file_ap->FindNamespace (sc, name, parent_namespace_decl);
380     }
381     return namespace_decl;
382 }
383 
384 void
385 SymbolVendor::Dump(Stream *s)
386 {
387     ModuleSP module_sp(GetModule());
388     if (module_sp)
389     {
390         bool show_context = false;
391 
392         s->Printf("%p: ", this);
393         s->Indent();
394         s->PutCString("SymbolVendor");
395         if (m_sym_file_ap.get())
396         {
397             ObjectFile *objfile = m_sym_file_ap->GetObjectFile();
398             if (objfile)
399             {
400                 const FileSpec &objfile_file_spec = objfile->GetFileSpec();
401                 if (objfile_file_spec)
402                 {
403                     s->PutCString(" (");
404                     objfile_file_spec.Dump(s);
405                     s->PutChar(')');
406                 }
407             }
408         }
409         s->EOL();
410         s->IndentMore();
411         m_type_list.Dump(s, show_context);
412 
413         CompileUnitConstIter cu_pos, cu_end;
414         cu_end = m_compile_units.end();
415         for (cu_pos = m_compile_units.begin(); cu_pos != cu_end; ++cu_pos)
416         {
417             // We currently only dump the compile units that have been parsed
418             if (cu_pos->get())
419                 (*cu_pos)->Dump(s, show_context);
420         }
421 
422         s->IndentLess();
423     }
424 }
425 
426 CompUnitSP
427 SymbolVendor::GetCompileUnitAtIndex(size_t idx)
428 {
429     CompUnitSP cu_sp;
430     ModuleSP module_sp(GetModule());
431     if (module_sp)
432     {
433         const size_t num_compile_units = GetNumCompileUnits();
434         if (idx < num_compile_units)
435         {
436             cu_sp = m_compile_units[idx];
437             if (cu_sp.get() == NULL)
438             {
439                 m_compile_units[idx] = m_sym_file_ap->ParseCompileUnitAtIndex(idx);
440                 cu_sp = m_compile_units[idx];
441             }
442         }
443     }
444     return cu_sp;
445 }
446 
447 
448 //------------------------------------------------------------------
449 // PluginInterface protocol
450 //------------------------------------------------------------------
451 lldb_private::ConstString
452 SymbolVendor::GetPluginName()
453 {
454     static ConstString g_name("vendor-default");
455     return g_name;
456 }
457 
458 uint32_t
459 SymbolVendor::GetPluginVersion()
460 {
461     return 1;
462 }
463 
464