]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/API/SBModule.cpp
Merge latest (commit c8c1b3a77934768c7f7a4a9c10140c8bec529059) files
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / API / SBModule.cpp
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_string_buffer[sizeof (uuid_string_buffer) - 1] = '\0';
225         uuid_c_string = uuid_string_buffer;
226     }
227
228     if (log)
229     {
230         if (!uuid_string.empty())
231         {
232             StreamString s;
233             module_sp->GetUUID().Dump (&s);
234             log->Printf ("SBModule(%p)::GetUUIDString () => %s",
235                          static_cast<void*>(module_sp.get()), s.GetData());
236         }
237         else
238             log->Printf ("SBModule(%p)::GetUUIDString () => NULL",
239                          static_cast<void*>(module_sp.get()));
240     }
241     return uuid_c_string;
242 }
243
244
245 bool
246 SBModule::operator == (const SBModule &rhs) const
247 {
248     if (m_opaque_sp)
249         return m_opaque_sp.get() == rhs.m_opaque_sp.get();
250     return false;
251 }
252
253 bool
254 SBModule::operator != (const SBModule &rhs) const
255 {
256     if (m_opaque_sp)
257         return m_opaque_sp.get() != rhs.m_opaque_sp.get();
258     return false;
259 }
260
261 ModuleSP
262 SBModule::GetSP () const
263 {
264     return m_opaque_sp;
265 }
266
267 void
268 SBModule::SetSP (const ModuleSP &module_sp)
269 {
270     m_opaque_sp = module_sp;
271 }
272
273 SBAddress
274 SBModule::ResolveFileAddress (lldb::addr_t vm_addr)
275 {
276     lldb::SBAddress sb_addr;
277     ModuleSP module_sp (GetSP ());
278     if (module_sp)
279     {
280         Address addr;
281         if (module_sp->ResolveFileAddress (vm_addr, addr))
282             sb_addr.ref() = addr;
283     }
284     return sb_addr;
285 }
286
287 SBSymbolContext
288 SBModule::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
289 {
290     SBSymbolContext sb_sc;
291     ModuleSP module_sp (GetSP ());
292     if (module_sp && addr.IsValid())
293         module_sp->ResolveSymbolContextForAddress (addr.ref(), resolve_scope, *sb_sc);
294     return sb_sc;
295 }
296
297 bool
298 SBModule::GetDescription (SBStream &description)
299 {
300     Stream &strm = description.ref();
301
302     ModuleSP module_sp (GetSP ());
303     if (module_sp)
304     {
305         module_sp->GetDescription (&strm);
306     }
307     else
308         strm.PutCString ("No value");
309
310     return true;
311 }
312
313 uint32_t
314 SBModule::GetNumCompileUnits()
315 {
316     ModuleSP module_sp (GetSP ());
317     if (module_sp)
318     {
319         return module_sp->GetNumCompileUnits ();
320     }
321     return 0;
322 }
323
324 SBCompileUnit
325 SBModule::GetCompileUnitAtIndex (uint32_t index)
326 {
327     SBCompileUnit sb_cu;
328     ModuleSP module_sp (GetSP ());
329     if (module_sp)
330     {
331         CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex (index);
332         sb_cu.reset(cu_sp.get());
333     }
334     return sb_cu;
335 }
336
337 static Symtab *
338 GetUnifiedSymbolTable (const lldb::ModuleSP& module_sp)
339 {
340     if (module_sp)
341     {
342         SymbolVendor *symbols = module_sp->GetSymbolVendor();
343         if (symbols)
344             return symbols->GetSymtab();
345     }
346     return NULL;
347 }
348
349 size_t
350 SBModule::GetNumSymbols ()
351 {
352     ModuleSP module_sp (GetSP ());
353     if (module_sp)
354     {
355         Symtab *symtab = GetUnifiedSymbolTable (module_sp);
356         if (symtab)
357             return symtab->GetNumSymbols();
358     }
359     return 0;
360 }
361
362 SBSymbol
363 SBModule::GetSymbolAtIndex (size_t idx)
364 {
365     SBSymbol sb_symbol;
366     ModuleSP module_sp (GetSP ());
367     Symtab *symtab = GetUnifiedSymbolTable (module_sp);
368     if (symtab)
369         sb_symbol.SetSymbol(symtab->SymbolAtIndex (idx));
370     return sb_symbol;
371 }
372
373 lldb::SBSymbol
374 SBModule::FindSymbol (const char *name,
375                       lldb::SymbolType symbol_type)
376 {
377     SBSymbol sb_symbol;
378     if (name && name[0])
379     {
380         ModuleSP module_sp (GetSP ());
381         Symtab *symtab = GetUnifiedSymbolTable (module_sp);
382         if (symtab)
383             sb_symbol.SetSymbol(symtab->FindFirstSymbolWithNameAndType(ConstString(name), symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny));
384     }
385     return sb_symbol;
386 }
387
388
389 lldb::SBSymbolContextList
390 SBModule::FindSymbols (const char *name, lldb::SymbolType symbol_type)
391 {
392     SBSymbolContextList sb_sc_list;
393     if (name && name[0])
394     {
395         ModuleSP module_sp (GetSP ());
396         Symtab *symtab = GetUnifiedSymbolTable (module_sp);
397         if (symtab)
398         {
399             std::vector<uint32_t> matching_symbol_indexes;
400             const size_t num_matches = symtab->FindAllSymbolsWithNameAndType(ConstString(name), symbol_type, matching_symbol_indexes);
401             if (num_matches)
402             {
403                 SymbolContext sc;
404                 sc.module_sp = module_sp;
405                 SymbolContextList &sc_list = *sb_sc_list;
406                 for (size_t i=0; i<num_matches; ++i)
407                 {
408                     sc.symbol = symtab->SymbolAtIndex (matching_symbol_indexes[i]);
409                     if (sc.symbol)
410                         sc_list.Append(sc);
411                 }
412             }
413         }
414     }
415     return sb_sc_list;
416     
417 }
418
419
420
421 size_t
422 SBModule::GetNumSections ()
423 {
424     ModuleSP module_sp (GetSP ());
425     if (module_sp)
426     {
427         // Give the symbol vendor a chance to add to the unified section list.
428         module_sp->GetSymbolVendor();
429         SectionList *section_list = module_sp->GetSectionList();
430         if (section_list)
431             return section_list->GetSize();
432     }
433     return 0;
434 }
435
436 SBSection
437 SBModule::GetSectionAtIndex (size_t idx)
438 {
439     SBSection sb_section;
440     ModuleSP module_sp (GetSP ());
441     if (module_sp)
442     {
443         // Give the symbol vendor a chance to add to the unified section list.
444         module_sp->GetSymbolVendor();
445         SectionList *section_list = module_sp->GetSectionList ();
446
447         if (section_list)
448             sb_section.SetSP(section_list->GetSectionAtIndex (idx));
449     }
450     return sb_section;
451 }
452
453 lldb::SBSymbolContextList
454 SBModule::FindFunctions (const char *name, 
455                          uint32_t name_type_mask)
456 {
457     lldb::SBSymbolContextList sb_sc_list;
458     ModuleSP module_sp (GetSP ());
459     if (name && module_sp)
460     {
461         const bool append = true;
462         const bool symbols_ok = true;
463         const bool inlines_ok = true;
464         module_sp->FindFunctions (ConstString(name),
465                                   NULL,
466                                   name_type_mask, 
467                                   symbols_ok,
468                                   inlines_ok,
469                                   append, 
470                                   *sb_sc_list);
471     }
472     return sb_sc_list;
473 }
474
475
476 SBValueList
477 SBModule::FindGlobalVariables (SBTarget &target, const char *name, uint32_t max_matches)
478 {
479     SBValueList sb_value_list;
480     ModuleSP module_sp (GetSP ());
481     if (name && module_sp)
482     {
483         VariableList variable_list;
484         const uint32_t match_count = module_sp->FindGlobalVariables (ConstString (name),
485                                                                      NULL,
486                                                                      false, 
487                                                                      max_matches,
488                                                                      variable_list);
489
490         if (match_count > 0)
491         {
492             for (uint32_t i=0; i<match_count; ++i)
493             {
494                 lldb::ValueObjectSP valobj_sp;
495                 TargetSP target_sp (target.GetSP());
496                 valobj_sp = ValueObjectVariable::Create (target_sp.get(), variable_list.GetVariableAtIndex(i));
497                 if (valobj_sp)
498                     sb_value_list.Append(SBValue(valobj_sp));
499             }
500         }
501     }
502     
503     return sb_value_list;
504 }
505
506 lldb::SBValue
507 SBModule::FindFirstGlobalVariable (lldb::SBTarget &target, const char *name)
508 {
509     SBValueList sb_value_list(FindGlobalVariables(target, name, 1));
510     if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
511         return sb_value_list.GetValueAtIndex(0);
512     return SBValue();
513 }
514
515 lldb::SBType
516 SBModule::FindFirstType (const char *name_cstr)
517 {
518     SBType sb_type;
519     ModuleSP module_sp (GetSP ());
520     if (name_cstr && module_sp)
521     {
522         SymbolContext sc;
523         const bool exact_match = false;
524         ConstString name(name_cstr);
525
526         sb_type = SBType (module_sp->FindFirstType(sc, name, exact_match));
527         
528         if (!sb_type.IsValid())
529             sb_type = SBType (ClangASTContext::GetBasicType (module_sp->GetClangASTContext().getASTContext(), name));
530     }
531     return sb_type;
532 }
533
534 lldb::SBType
535 SBModule::GetBasicType(lldb::BasicType type)
536 {
537     ModuleSP module_sp (GetSP ());
538     if (module_sp)
539         return SBType (ClangASTContext::GetBasicType (module_sp->GetClangASTContext().getASTContext(), type));
540     return SBType();
541 }
542
543 lldb::SBTypeList
544 SBModule::FindTypes (const char *type)
545 {
546     SBTypeList retval;
547     
548     ModuleSP module_sp (GetSP ());
549     if (type && module_sp)
550     {
551         SymbolContext sc;
552         TypeList type_list;
553         const bool exact_match = false;
554         ConstString name(type);
555         const uint32_t num_matches = module_sp->FindTypes (sc,
556                                                            name,
557                                                            exact_match,
558                                                            UINT32_MAX,
559                                                            type_list);
560         
561         if (num_matches > 0)
562         {
563             for (size_t idx = 0; idx < num_matches; idx++)
564             {
565                 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
566                 if (type_sp)
567                     retval.Append(SBType(type_sp));
568             }
569         }
570         else
571         {
572             SBType sb_type(ClangASTContext::GetBasicType (module_sp->GetClangASTContext().getASTContext(), name));
573             if (sb_type.IsValid())
574                 retval.Append(sb_type);
575         }
576     }
577
578     return retval;
579 }
580
581 lldb::SBType
582 SBModule::GetTypeByID (lldb::user_id_t uid)
583 {
584     ModuleSP module_sp (GetSP ());
585     if (module_sp)
586     {
587         SymbolVendor* vendor = module_sp->GetSymbolVendor();
588         if (vendor)
589         {
590             Type *type_ptr = vendor->ResolveTypeUID(uid);
591             if (type_ptr)
592                 return SBType(type_ptr->shared_from_this());
593         }
594     }
595     return SBType();
596 }
597
598 lldb::SBTypeList
599 SBModule::GetTypes (uint32_t type_mask)
600 {
601     SBTypeList sb_type_list;
602     
603     ModuleSP module_sp (GetSP ());
604     if (module_sp)
605     {
606         SymbolVendor* vendor = module_sp->GetSymbolVendor();
607         if (vendor)
608         {
609             TypeList type_list;
610             vendor->GetTypes (NULL, type_mask, type_list);
611             sb_type_list.m_opaque_ap->Append(type_list);
612         }
613     }
614     return sb_type_list;
615 }
616
617 SBSection
618 SBModule::FindSection (const char *sect_name)
619 {
620     SBSection sb_section;
621     
622     ModuleSP module_sp (GetSP ());
623     if (sect_name && module_sp)
624     {
625         // Give the symbol vendor a chance to add to the unified section list.
626         module_sp->GetSymbolVendor();
627         SectionList *section_list = module_sp->GetSectionList();
628         if (section_list)
629         {
630             ConstString const_sect_name(sect_name);
631             SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
632             if (section_sp)
633             {
634                 sb_section.SetSP (section_sp);
635             }
636         }
637     }
638     return sb_section;
639 }
640
641 lldb::ByteOrder
642 SBModule::GetByteOrder ()
643 {
644     ModuleSP module_sp (GetSP ());
645     if (module_sp)
646         return module_sp->GetArchitecture().GetByteOrder();
647     return eByteOrderInvalid;
648 }
649
650 const char *
651 SBModule::GetTriple ()
652 {
653     ModuleSP module_sp (GetSP ());
654     if (module_sp)
655     {
656         std::string triple (module_sp->GetArchitecture().GetTriple().str());
657         // Unique the string so we don't run into ownership issues since
658         // the const strings put the string into the string pool once and
659         // the strings never comes out
660         ConstString const_triple (triple.c_str());
661         return const_triple.GetCString();
662     }
663     return NULL;
664 }
665
666 uint32_t
667 SBModule::GetAddressByteSize()
668 {
669     ModuleSP module_sp (GetSP ());
670     if (module_sp)
671         return module_sp->GetArchitecture().GetAddressByteSize();
672     return sizeof(void*);
673 }
674
675
676 uint32_t
677 SBModule::GetVersion (uint32_t *versions, uint32_t num_versions)
678 {
679     ModuleSP module_sp (GetSP ());
680     if (module_sp)
681         return module_sp->GetVersion(versions, num_versions);
682     else
683     {
684         if (versions && num_versions)
685         {
686             for (uint32_t i=0; i<num_versions; ++i)
687                 versions[i] = UINT32_MAX;
688         }
689         return 0;
690     }
691 }
692