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