1 //===-- SBModule.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 "lldb/API/SBModule.h"
11 #include "lldb/API/SBAddress.h"
12 #include "lldb/API/SBFileSpec.h"
13 #include "lldb/API/SBModuleSpec.h"
14 #include "lldb/API/SBProcess.h"
15 #include "lldb/API/SBStream.h"
16 #include "lldb/API/SBSymbolContextList.h"
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/Log.h"
19 #include "lldb/Core/Section.h"
20 #include "lldb/Core/StreamString.h"
21 #include "lldb/Core/ValueObjectList.h"
22 #include "lldb/Core/ValueObjectVariable.h"
23 #include "lldb/Symbol/ObjectFile.h"
24 #include "lldb/Symbol/SymbolVendor.h"
25 #include "lldb/Symbol/Symtab.h"
26 #include "lldb/Symbol/TypeSystem.h"
27 #include "lldb/Symbol/VariableList.h"
28 #include "lldb/Target/Target.h"
29 
30 using namespace lldb;
31 using namespace lldb_private;
32 
33 
34 SBModule::SBModule () :
35     m_opaque_sp ()
36 {
37 }
38 
39 SBModule::SBModule (const lldb::ModuleSP& module_sp) :
40     m_opaque_sp (module_sp)
41 {
42 }
43 
44 SBModule::SBModule(const SBModuleSpec &module_spec) :
45     m_opaque_sp ()
46 {
47     ModuleSP module_sp;
48     Error error = ModuleList::GetSharedModule (*module_spec.m_opaque_ap,
49                                                module_sp,
50                                                NULL,
51                                                NULL,
52                                                NULL);
53     if (module_sp)
54         SetSP(module_sp);
55 }
56 
57 SBModule::SBModule(const SBModule &rhs) :
58     m_opaque_sp (rhs.m_opaque_sp)
59 {
60 }
61 
62 SBModule::SBModule (lldb::SBProcess &process, lldb::addr_t header_addr) :
63     m_opaque_sp ()
64 {
65     ProcessSP process_sp (process.GetSP());
66     if (process_sp)
67     {
68         m_opaque_sp = process_sp->ReadModuleFromMemory (FileSpec(), header_addr);
69         if (m_opaque_sp)
70         {
71             Target &target = process_sp->GetTarget();
72             bool changed = false;
73             m_opaque_sp->SetLoadAddress(target, 0, true, changed);
74             target.GetImages().Append(m_opaque_sp);
75         }
76     }
77 }
78 
79 const SBModule &
80 SBModule::operator = (const SBModule &rhs)
81 {
82     if (this != &rhs)
83         m_opaque_sp = rhs.m_opaque_sp;
84     return *this;
85 }
86 
87 SBModule::~SBModule ()
88 {
89 }
90 
91 bool
92 SBModule::IsValid () const
93 {
94     return m_opaque_sp.get() != NULL;
95 }
96 
97 void
98 SBModule::Clear()
99 {
100     m_opaque_sp.reset();
101 }
102 
103 SBFileSpec
104 SBModule::GetFileSpec () const
105 {
106     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
107 
108     SBFileSpec file_spec;
109     ModuleSP module_sp (GetSP ());
110     if (module_sp)
111         file_spec.SetFileSpec(module_sp->GetFileSpec());
112 
113     if (log)
114         log->Printf ("SBModule(%p)::GetFileSpec () => SBFileSpec(%p)",
115                      static_cast<void*>(module_sp.get()),
116                      static_cast<const void*>(file_spec.get()));
117 
118     return file_spec;
119 }
120 
121 lldb::SBFileSpec
122 SBModule::GetPlatformFileSpec () const
123 {
124     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
125 
126     SBFileSpec file_spec;
127     ModuleSP module_sp (GetSP ());
128     if (module_sp)
129         file_spec.SetFileSpec(module_sp->GetPlatformFileSpec());
130 
131     if (log)
132         log->Printf ("SBModule(%p)::GetPlatformFileSpec () => SBFileSpec(%p)",
133                      static_cast<void*>(module_sp.get()),
134                      static_cast<const void*>(file_spec.get()));
135 
136     return file_spec;
137 }
138 
139 bool
140 SBModule::SetPlatformFileSpec (const lldb::SBFileSpec &platform_file)
141 {
142     bool result = false;
143     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
144 
145     ModuleSP module_sp (GetSP ());
146     if (module_sp)
147     {
148         module_sp->SetPlatformFileSpec(*platform_file);
149         result = true;
150     }
151 
152     if (log)
153         log->Printf ("SBModule(%p)::SetPlatformFileSpec (SBFileSpec(%p (%s)) => %i",
154                      static_cast<void*>(module_sp.get()),
155                      static_cast<const void*>(platform_file.get()),
156                      platform_file->GetPath().c_str(), result);
157     return result;
158 }
159 
160 lldb::SBFileSpec
161 SBModule::GetRemoteInstallFileSpec ()
162 {
163     SBFileSpec sb_file_spec;
164     ModuleSP module_sp (GetSP ());
165     if (module_sp)
166         sb_file_spec.SetFileSpec (module_sp->GetRemoteInstallFileSpec());
167     return sb_file_spec;
168 }
169 
170 bool
171 SBModule::SetRemoteInstallFileSpec (lldb::SBFileSpec &file)
172 {
173     ModuleSP module_sp (GetSP ());
174     if (module_sp)
175     {
176         module_sp->SetRemoteInstallFileSpec(file.ref());
177         return true;
178     }
179     return false;
180 }
181 
182 
183 const uint8_t *
184 SBModule::GetUUIDBytes () const
185 {
186     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
187 
188     const uint8_t *uuid_bytes = NULL;
189     ModuleSP module_sp (GetSP ());
190     if (module_sp)
191         uuid_bytes = (const uint8_t *)module_sp->GetUUID().GetBytes();
192 
193     if (log)
194     {
195         if (uuid_bytes)
196         {
197             StreamString s;
198             module_sp->GetUUID().Dump (&s);
199             log->Printf ("SBModule(%p)::GetUUIDBytes () => %s",
200                          static_cast<void*>(module_sp.get()), s.GetData());
201         }
202         else
203             log->Printf ("SBModule(%p)::GetUUIDBytes () => NULL",
204                          static_cast<void*>(module_sp.get()));
205     }
206     return uuid_bytes;
207 }
208 
209 
210 const char *
211 SBModule::GetUUIDString () const
212 {
213     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
214 
215     const char *uuid_cstr = NULL;
216     ModuleSP module_sp (GetSP ());
217     if (module_sp)
218     {
219         // We are going to return a "const char *" value through the public
220         // API, so we need to constify it so it gets added permanently the
221         // string pool and then we don't need to worry about the lifetime of the
222         // string as it will never go away once it has been put into the ConstString
223         // string pool
224         uuid_cstr = ConstString(module_sp->GetUUID().GetAsString()).GetCString();
225     }
226 
227     if (uuid_cstr && uuid_cstr[0])
228     {
229         if (log)
230             log->Printf ("SBModule(%p)::GetUUIDString () => %s", static_cast<void*>(module_sp.get()), uuid_cstr);
231         return uuid_cstr;
232     }
233 
234     if (log)
235         log->Printf ("SBModule(%p)::GetUUIDString () => NULL", static_cast<void*>(module_sp.get()));
236     return NULL;
237 }
238 
239 
240 bool
241 SBModule::operator == (const SBModule &rhs) const
242 {
243     if (m_opaque_sp)
244         return m_opaque_sp.get() == rhs.m_opaque_sp.get();
245     return false;
246 }
247 
248 bool
249 SBModule::operator != (const SBModule &rhs) const
250 {
251     if (m_opaque_sp)
252         return m_opaque_sp.get() != rhs.m_opaque_sp.get();
253     return false;
254 }
255 
256 ModuleSP
257 SBModule::GetSP () const
258 {
259     return m_opaque_sp;
260 }
261 
262 void
263 SBModule::SetSP (const ModuleSP &module_sp)
264 {
265     m_opaque_sp = module_sp;
266 }
267 
268 SBAddress
269 SBModule::ResolveFileAddress (lldb::addr_t vm_addr)
270 {
271     lldb::SBAddress sb_addr;
272     ModuleSP module_sp (GetSP ());
273     if (module_sp)
274     {
275         Address addr;
276         if (module_sp->ResolveFileAddress (vm_addr, addr))
277             sb_addr.ref() = addr;
278     }
279     return sb_addr;
280 }
281 
282 SBSymbolContext
283 SBModule::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
284 {
285     SBSymbolContext sb_sc;
286     ModuleSP module_sp (GetSP ());
287     if (module_sp && addr.IsValid())
288         module_sp->ResolveSymbolContextForAddress (addr.ref(), resolve_scope, *sb_sc);
289     return sb_sc;
290 }
291 
292 bool
293 SBModule::GetDescription (SBStream &description)
294 {
295     Stream &strm = description.ref();
296 
297     ModuleSP module_sp (GetSP ());
298     if (module_sp)
299     {
300         module_sp->GetDescription (&strm);
301     }
302     else
303         strm.PutCString ("No value");
304 
305     return true;
306 }
307 
308 uint32_t
309 SBModule::GetNumCompileUnits()
310 {
311     ModuleSP module_sp (GetSP ());
312     if (module_sp)
313     {
314         return module_sp->GetNumCompileUnits ();
315     }
316     return 0;
317 }
318 
319 SBCompileUnit
320 SBModule::GetCompileUnitAtIndex (uint32_t index)
321 {
322     SBCompileUnit sb_cu;
323     ModuleSP module_sp (GetSP ());
324     if (module_sp)
325     {
326         CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex (index);
327         sb_cu.reset(cu_sp.get());
328     }
329     return sb_cu;
330 }
331 
332 static Symtab *
333 GetUnifiedSymbolTable (const lldb::ModuleSP& module_sp)
334 {
335     if (module_sp)
336     {
337         SymbolVendor *symbols = module_sp->GetSymbolVendor();
338         if (symbols)
339             return symbols->GetSymtab();
340     }
341     return NULL;
342 }
343 
344 size_t
345 SBModule::GetNumSymbols ()
346 {
347     ModuleSP module_sp (GetSP ());
348     if (module_sp)
349     {
350         Symtab *symtab = GetUnifiedSymbolTable (module_sp);
351         if (symtab)
352             return symtab->GetNumSymbols();
353     }
354     return 0;
355 }
356 
357 SBSymbol
358 SBModule::GetSymbolAtIndex (size_t idx)
359 {
360     SBSymbol sb_symbol;
361     ModuleSP module_sp (GetSP ());
362     Symtab *symtab = GetUnifiedSymbolTable (module_sp);
363     if (symtab)
364         sb_symbol.SetSymbol(symtab->SymbolAtIndex (idx));
365     return sb_symbol;
366 }
367 
368 lldb::SBSymbol
369 SBModule::FindSymbol (const char *name,
370                       lldb::SymbolType symbol_type)
371 {
372     SBSymbol sb_symbol;
373     if (name && name[0])
374     {
375         ModuleSP module_sp (GetSP ());
376         Symtab *symtab = GetUnifiedSymbolTable (module_sp);
377         if (symtab)
378             sb_symbol.SetSymbol(symtab->FindFirstSymbolWithNameAndType(ConstString(name), symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny));
379     }
380     return sb_symbol;
381 }
382 
383 
384 lldb::SBSymbolContextList
385 SBModule::FindSymbols (const char *name, lldb::SymbolType symbol_type)
386 {
387     SBSymbolContextList sb_sc_list;
388     if (name && name[0])
389     {
390         ModuleSP module_sp (GetSP ());
391         Symtab *symtab = GetUnifiedSymbolTable (module_sp);
392         if (symtab)
393         {
394             std::vector<uint32_t> matching_symbol_indexes;
395             const size_t num_matches = symtab->FindAllSymbolsWithNameAndType(ConstString(name), symbol_type, matching_symbol_indexes);
396             if (num_matches)
397             {
398                 SymbolContext sc;
399                 sc.module_sp = module_sp;
400                 SymbolContextList &sc_list = *sb_sc_list;
401                 for (size_t i=0; i<num_matches; ++i)
402                 {
403                     sc.symbol = symtab->SymbolAtIndex (matching_symbol_indexes[i]);
404                     if (sc.symbol)
405                         sc_list.Append(sc);
406                 }
407             }
408         }
409     }
410     return sb_sc_list;
411 
412 }
413 
414 
415 
416 size_t
417 SBModule::GetNumSections ()
418 {
419     ModuleSP module_sp (GetSP ());
420     if (module_sp)
421     {
422         // Give the symbol vendor a chance to add to the unified section list.
423         module_sp->GetSymbolVendor();
424         SectionList *section_list = module_sp->GetSectionList();
425         if (section_list)
426             return section_list->GetSize();
427     }
428     return 0;
429 }
430 
431 SBSection
432 SBModule::GetSectionAtIndex (size_t idx)
433 {
434     SBSection sb_section;
435     ModuleSP module_sp (GetSP ());
436     if (module_sp)
437     {
438         // Give the symbol vendor a chance to add to the unified section list.
439         module_sp->GetSymbolVendor();
440         SectionList *section_list = module_sp->GetSectionList ();
441 
442         if (section_list)
443             sb_section.SetSP(section_list->GetSectionAtIndex (idx));
444     }
445     return sb_section;
446 }
447 
448 lldb::SBSymbolContextList
449 SBModule::FindFunctions (const char *name,
450                          uint32_t name_type_mask)
451 {
452     lldb::SBSymbolContextList sb_sc_list;
453     ModuleSP module_sp (GetSP ());
454     if (name && module_sp)
455     {
456         const bool append = true;
457         const bool symbols_ok = true;
458         const bool inlines_ok = true;
459         module_sp->FindFunctions (ConstString(name),
460                                   NULL,
461                                   name_type_mask,
462                                   symbols_ok,
463                                   inlines_ok,
464                                   append,
465                                   *sb_sc_list);
466     }
467     return sb_sc_list;
468 }
469 
470 
471 SBValueList
472 SBModule::FindGlobalVariables (SBTarget &target, const char *name, uint32_t max_matches)
473 {
474     SBValueList sb_value_list;
475     ModuleSP module_sp (GetSP ());
476     if (name && module_sp)
477     {
478         VariableList variable_list;
479         const uint32_t match_count = module_sp->FindGlobalVariables (ConstString (name),
480                                                                      NULL,
481                                                                      false,
482                                                                      max_matches,
483                                                                      variable_list);
484 
485         if (match_count > 0)
486         {
487             for (uint32_t i=0; i<match_count; ++i)
488             {
489                 lldb::ValueObjectSP valobj_sp;
490                 TargetSP target_sp (target.GetSP());
491                 valobj_sp = ValueObjectVariable::Create (target_sp.get(), variable_list.GetVariableAtIndex(i));
492                 if (valobj_sp)
493                     sb_value_list.Append(SBValue(valobj_sp));
494             }
495         }
496     }
497 
498     return sb_value_list;
499 }
500 
501 lldb::SBValue
502 SBModule::FindFirstGlobalVariable (lldb::SBTarget &target, const char *name)
503 {
504     SBValueList sb_value_list(FindGlobalVariables(target, name, 1));
505     if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
506         return sb_value_list.GetValueAtIndex(0);
507     return SBValue();
508 }
509 
510 lldb::SBType
511 SBModule::FindFirstType (const char *name_cstr)
512 {
513     SBType sb_type;
514     ModuleSP module_sp (GetSP ());
515     if (name_cstr && module_sp)
516     {
517         SymbolContext sc;
518         const bool exact_match = false;
519         ConstString name(name_cstr);
520 
521         sb_type = SBType (module_sp->FindFirstType(sc, name, exact_match));
522 
523         if (!sb_type.IsValid())
524         {
525             TypeSystem *type_system = module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
526             if (type_system)
527                 sb_type = SBType (type_system->GetBuiltinTypeByName(name));
528         }
529     }
530     return sb_type;
531 }
532 
533 lldb::SBType
534 SBModule::GetBasicType(lldb::BasicType type)
535 {
536     ModuleSP module_sp (GetSP ());
537     if (module_sp)
538     {
539         TypeSystem *type_system = module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
540         if (type_system)
541             return SBType (type_system->GetBasicTypeFromAST(type));
542     }
543     return SBType();
544 }
545 
546 lldb::SBTypeList
547 SBModule::FindTypes (const char *type)
548 {
549     SBTypeList retval;
550 
551     ModuleSP module_sp (GetSP ());
552     if (type && module_sp)
553     {
554         SymbolContext sc;
555         TypeList type_list;
556         const bool exact_match = false;
557         ConstString name(type);
558         const uint32_t num_matches = module_sp->FindTypes (sc,
559                                                            name,
560                                                            exact_match,
561                                                            UINT32_MAX,
562                                                            type_list);
563 
564         if (num_matches > 0)
565         {
566             for (size_t idx = 0; idx < num_matches; idx++)
567             {
568                 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
569                 if (type_sp)
570                     retval.Append(SBType(type_sp));
571             }
572         }
573         else
574         {
575             TypeSystem *type_system = module_sp->GetTypeSystemForLanguage(eLanguageTypeC);
576             if (type_system)
577             {
578                 CompilerType compiler_type = type_system->GetBuiltinTypeByName(name);
579                 if (compiler_type)
580                     retval.Append(SBType(compiler_type));
581             }
582         }
583     }
584 
585     return retval;
586 }
587 
588 lldb::SBType
589 SBModule::GetTypeByID (lldb::user_id_t uid)
590 {
591     ModuleSP module_sp (GetSP ());
592     if (module_sp)
593     {
594         SymbolVendor* vendor = module_sp->GetSymbolVendor();
595         if (vendor)
596         {
597             Type *type_ptr = vendor->ResolveTypeUID(uid);
598             if (type_ptr)
599                 return SBType(type_ptr->shared_from_this());
600         }
601     }
602     return SBType();
603 }
604 
605 lldb::SBTypeList
606 SBModule::GetTypes (uint32_t type_mask)
607 {
608     SBTypeList sb_type_list;
609 
610     ModuleSP module_sp (GetSP ());
611     if (module_sp)
612     {
613         SymbolVendor* vendor = module_sp->GetSymbolVendor();
614         if (vendor)
615         {
616             TypeList type_list;
617             vendor->GetTypes (NULL, type_mask, type_list);
618             sb_type_list.m_opaque_ap->Append(type_list);
619         }
620     }
621     return sb_type_list;
622 }
623 
624 SBSection
625 SBModule::FindSection (const char *sect_name)
626 {
627     SBSection sb_section;
628 
629     ModuleSP module_sp (GetSP ());
630     if (sect_name && module_sp)
631     {
632         // Give the symbol vendor a chance to add to the unified section list.
633         module_sp->GetSymbolVendor();
634         SectionList *section_list = module_sp->GetSectionList();
635         if (section_list)
636         {
637             ConstString const_sect_name(sect_name);
638             SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
639             if (section_sp)
640             {
641                 sb_section.SetSP (section_sp);
642             }
643         }
644     }
645     return sb_section;
646 }
647 
648 lldb::ByteOrder
649 SBModule::GetByteOrder ()
650 {
651     ModuleSP module_sp (GetSP ());
652     if (module_sp)
653         return module_sp->GetArchitecture().GetByteOrder();
654     return eByteOrderInvalid;
655 }
656 
657 const char *
658 SBModule::GetTriple ()
659 {
660     ModuleSP module_sp (GetSP ());
661     if (module_sp)
662     {
663         std::string triple (module_sp->GetArchitecture().GetTriple().str());
664         // Unique the string so we don't run into ownership issues since
665         // the const strings put the string into the string pool once and
666         // the strings never comes out
667         ConstString const_triple (triple.c_str());
668         return const_triple.GetCString();
669     }
670     return NULL;
671 }
672 
673 uint32_t
674 SBModule::GetAddressByteSize()
675 {
676     ModuleSP module_sp (GetSP ());
677     if (module_sp)
678         return module_sp->GetArchitecture().GetAddressByteSize();
679     return sizeof(void*);
680 }
681 
682 
683 uint32_t
684 SBModule::GetVersion (uint32_t *versions, uint32_t num_versions)
685 {
686     ModuleSP module_sp (GetSP ());
687     if (module_sp)
688         return module_sp->GetVersion(versions, num_versions);
689     else
690     {
691         if (versions && num_versions)
692         {
693             for (uint32_t i=0; i<num_versions; ++i)
694                 versions[i] = UINT32_MAX;
695         }
696         return 0;
697     }
698 }
699 
700 lldb::SBFileSpec
701 SBModule::GetSymbolFileSpec() const
702 {
703     lldb::SBFileSpec sb_file_spec;
704     ModuleSP module_sp(GetSP());
705     if (module_sp)
706     {
707         SymbolVendor *symbol_vendor_ptr = module_sp->GetSymbolVendor();
708         if (symbol_vendor_ptr)
709             sb_file_spec.SetFileSpec(symbol_vendor_ptr->GetMainFileSpec());
710     }
711     return sb_file_spec;
712 }
713 
714 lldb::SBAddress
715 SBModule::GetObjectFileHeaderAddress() const
716 {
717     lldb::SBAddress sb_addr;
718     ModuleSP module_sp (GetSP ());
719     if (module_sp)
720     {
721         ObjectFile *objfile_ptr = module_sp->GetObjectFile();
722         if (objfile_ptr)
723             sb_addr.ref() = objfile_ptr->GetHeaderAddress();
724     }
725     return sb_addr;
726 }
727