1 //===-- SBModule.cpp --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
30 using namespace lldb_private;
33 SBModule::SBModule () :
38 SBModule::SBModule (const lldb::ModuleSP& module_sp) :
39 m_opaque_sp (module_sp)
43 SBModule::SBModule(const SBModuleSpec &module_spec) :
47 Error error = ModuleList::GetSharedModule (*module_spec.m_opaque_ap,
56 SBModule::SBModule(const SBModule &rhs) :
57 m_opaque_sp (rhs.m_opaque_sp)
61 SBModule::SBModule (lldb::SBProcess &process, lldb::addr_t header_addr) :
64 ProcessSP process_sp (process.GetSP());
67 m_opaque_sp = process_sp->ReadModuleFromMemory (FileSpec(), header_addr);
70 Target &target = process_sp->GetTarget();
72 m_opaque_sp->SetLoadAddress(target, 0, true, changed);
73 target.GetImages().Append(m_opaque_sp);
79 SBModule::operator = (const SBModule &rhs)
82 m_opaque_sp = rhs.m_opaque_sp;
86 SBModule::~SBModule ()
91 SBModule::IsValid () const
93 return m_opaque_sp.get() != NULL;
103 SBModule::GetFileSpec () const
105 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
107 SBFileSpec file_spec;
108 ModuleSP module_sp (GetSP ());
110 file_spec.SetFileSpec(module_sp->GetFileSpec());
114 log->Printf ("SBModule(%p)::GetFileSpec () => SBFileSpec(%p)",
115 module_sp.get(), file_spec.get());
122 SBModule::GetPlatformFileSpec () const
124 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
126 SBFileSpec file_spec;
127 ModuleSP module_sp (GetSP ());
129 file_spec.SetFileSpec(module_sp->GetPlatformFileSpec());
133 log->Printf ("SBModule(%p)::GetPlatformFileSpec () => SBFileSpec(%p)",
134 module_sp.get(), file_spec.get());
142 SBModule::SetPlatformFileSpec (const lldb::SBFileSpec &platform_file)
145 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
147 ModuleSP module_sp (GetSP ());
150 module_sp->SetPlatformFileSpec(*platform_file);
156 log->Printf ("SBModule(%p)::SetPlatformFileSpec (SBFileSpec(%p (%s)) => %i",
159 platform_file->GetPath().c_str(),
166 SBModule::GetRemoteInstallFileSpec ()
168 SBFileSpec sb_file_spec;
169 ModuleSP module_sp (GetSP ());
171 sb_file_spec.SetFileSpec (module_sp->GetRemoteInstallFileSpec());
176 SBModule::SetRemoteInstallFileSpec (lldb::SBFileSpec &file)
178 ModuleSP module_sp (GetSP ());
181 module_sp->SetRemoteInstallFileSpec(file.ref());
189 SBModule::GetUUIDBytes () const
191 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
193 const uint8_t *uuid_bytes = NULL;
194 ModuleSP module_sp (GetSP ());
196 uuid_bytes = (const uint8_t *)module_sp->GetUUID().GetBytes();
203 module_sp->GetUUID().Dump (&s);
204 log->Printf ("SBModule(%p)::GetUUIDBytes () => %s", module_sp.get(), s.GetData());
207 log->Printf ("SBModule(%p)::GetUUIDBytes () => NULL", module_sp.get());
214 SBModule::GetUUIDString () const
216 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
218 static char uuid_string_buffer[80];
219 const char *uuid_c_string = NULL;
220 std::string uuid_string;
221 ModuleSP module_sp (GetSP ());
223 uuid_string = module_sp->GetUUID().GetAsString();
225 if (!uuid_string.empty())
227 strncpy (uuid_string_buffer, uuid_string.c_str(), sizeof (uuid_string_buffer));
228 uuid_c_string = uuid_string_buffer;
233 if (!uuid_string.empty())
236 module_sp->GetUUID().Dump (&s);
237 log->Printf ("SBModule(%p)::GetUUIDString () => %s", module_sp.get(), s.GetData());
240 log->Printf ("SBModule(%p)::GetUUIDString () => NULL", module_sp.get());
242 return uuid_c_string;
247 SBModule::operator == (const SBModule &rhs) const
250 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
255 SBModule::operator != (const SBModule &rhs) const
258 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
263 SBModule::GetSP () const
269 SBModule::SetSP (const ModuleSP &module_sp)
271 m_opaque_sp = module_sp;
275 SBModule::ResolveFileAddress (lldb::addr_t vm_addr)
277 lldb::SBAddress sb_addr;
278 ModuleSP module_sp (GetSP ());
282 if (module_sp->ResolveFileAddress (vm_addr, addr))
283 sb_addr.ref() = addr;
289 SBModule::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
291 SBSymbolContext sb_sc;
292 ModuleSP module_sp (GetSP ());
293 if (module_sp && addr.IsValid())
294 module_sp->ResolveSymbolContextForAddress (addr.ref(), resolve_scope, *sb_sc);
299 SBModule::GetDescription (SBStream &description)
301 Stream &strm = description.ref();
303 ModuleSP module_sp (GetSP ());
306 module_sp->GetDescription (&strm);
309 strm.PutCString ("No value");
315 SBModule::GetNumCompileUnits()
317 ModuleSP module_sp (GetSP ());
320 return module_sp->GetNumCompileUnits ();
326 SBModule::GetCompileUnitAtIndex (uint32_t index)
329 ModuleSP module_sp (GetSP ());
332 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex (index);
333 sb_cu.reset(cu_sp.get());
339 GetUnifiedSymbolTable (const lldb::ModuleSP& module_sp)
343 SymbolVendor *symbols = module_sp->GetSymbolVendor();
345 return symbols->GetSymtab();
351 SBModule::GetNumSymbols ()
353 ModuleSP module_sp (GetSP ());
356 Symtab *symtab = GetUnifiedSymbolTable (module_sp);
358 return symtab->GetNumSymbols();
364 SBModule::GetSymbolAtIndex (size_t idx)
367 ModuleSP module_sp (GetSP ());
368 Symtab *symtab = GetUnifiedSymbolTable (module_sp);
370 sb_symbol.SetSymbol(symtab->SymbolAtIndex (idx));
375 SBModule::FindSymbol (const char *name,
376 lldb::SymbolType symbol_type)
381 ModuleSP module_sp (GetSP ());
382 Symtab *symtab = GetUnifiedSymbolTable (module_sp);
384 sb_symbol.SetSymbol(symtab->FindFirstSymbolWithNameAndType(ConstString(name), symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny));
390 lldb::SBSymbolContextList
391 SBModule::FindSymbols (const char *name, lldb::SymbolType symbol_type)
393 SBSymbolContextList sb_sc_list;
396 ModuleSP module_sp (GetSP ());
397 Symtab *symtab = GetUnifiedSymbolTable (module_sp);
400 std::vector<uint32_t> matching_symbol_indexes;
401 const size_t num_matches = symtab->FindAllSymbolsWithNameAndType(ConstString(name), symbol_type, matching_symbol_indexes);
405 sc.module_sp = module_sp;
406 SymbolContextList &sc_list = *sb_sc_list;
407 for (size_t i=0; i<num_matches; ++i)
409 sc.symbol = symtab->SymbolAtIndex (matching_symbol_indexes[i]);
423 SBModule::GetNumSections ()
425 ModuleSP module_sp (GetSP ());
428 // Give the symbol vendor a chance to add to the unified section list.
429 module_sp->GetSymbolVendor();
430 SectionList *section_list = module_sp->GetSectionList();
432 return section_list->GetSize();
438 SBModule::GetSectionAtIndex (size_t idx)
440 SBSection sb_section;
441 ModuleSP module_sp (GetSP ());
444 // Give the symbol vendor a chance to add to the unified section list.
445 module_sp->GetSymbolVendor();
446 SectionList *section_list = module_sp->GetSectionList ();
449 sb_section.SetSP(section_list->GetSectionAtIndex (idx));
454 lldb::SBSymbolContextList
455 SBModule::FindFunctions (const char *name,
456 uint32_t name_type_mask)
458 lldb::SBSymbolContextList sb_sc_list;
459 ModuleSP module_sp (GetSP ());
460 if (name && module_sp)
462 const bool append = true;
463 const bool symbols_ok = true;
464 const bool inlines_ok = true;
465 module_sp->FindFunctions (ConstString(name),
478 SBModule::FindGlobalVariables (SBTarget &target, const char *name, uint32_t max_matches)
480 SBValueList sb_value_list;
481 ModuleSP module_sp (GetSP ());
482 if (name && module_sp)
484 VariableList variable_list;
485 const uint32_t match_count = module_sp->FindGlobalVariables (ConstString (name),
493 for (uint32_t i=0; i<match_count; ++i)
495 lldb::ValueObjectSP valobj_sp;
496 TargetSP target_sp (target.GetSP());
497 valobj_sp = ValueObjectVariable::Create (target_sp.get(), variable_list.GetVariableAtIndex(i));
499 sb_value_list.Append(SBValue(valobj_sp));
504 return sb_value_list;
508 SBModule::FindFirstGlobalVariable (lldb::SBTarget &target, const char *name)
510 SBValueList sb_value_list(FindGlobalVariables(target, name, 1));
511 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
512 return sb_value_list.GetValueAtIndex(0);
517 SBModule::FindFirstType (const char *name_cstr)
520 ModuleSP module_sp (GetSP ());
521 if (name_cstr && module_sp)
524 const bool exact_match = false;
525 ConstString name(name_cstr);
527 sb_type = SBType (module_sp->FindFirstType(sc, name, exact_match));
529 if (!sb_type.IsValid())
530 sb_type = SBType (ClangASTContext::GetBasicType (module_sp->GetClangASTContext().getASTContext(), name));
536 SBModule::GetBasicType(lldb::BasicType type)
538 ModuleSP module_sp (GetSP ());
540 return SBType (ClangASTContext::GetBasicType (module_sp->GetClangASTContext().getASTContext(), type));
545 SBModule::FindTypes (const char *type)
549 ModuleSP module_sp (GetSP ());
550 if (type && module_sp)
554 const bool exact_match = false;
555 ConstString name(type);
556 const uint32_t num_matches = module_sp->FindTypes (sc,
564 for (size_t idx = 0; idx < num_matches; idx++)
566 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
568 retval.Append(SBType(type_sp));
573 SBType sb_type(ClangASTContext::GetBasicType (module_sp->GetClangASTContext().getASTContext(), name));
574 if (sb_type.IsValid())
575 retval.Append(sb_type);
583 SBModule::GetTypeByID (lldb::user_id_t uid)
585 ModuleSP module_sp (GetSP ());
588 SymbolVendor* vendor = module_sp->GetSymbolVendor();
591 Type *type_ptr = vendor->ResolveTypeUID(uid);
593 return SBType(type_ptr->shared_from_this());
600 SBModule::GetTypes (uint32_t type_mask)
602 SBTypeList sb_type_list;
604 ModuleSP module_sp (GetSP ());
607 SymbolVendor* vendor = module_sp->GetSymbolVendor();
611 vendor->GetTypes (NULL, type_mask, type_list);
612 sb_type_list.m_opaque_ap->Append(type_list);
619 SBModule::FindSection (const char *sect_name)
621 SBSection sb_section;
623 ModuleSP module_sp (GetSP ());
624 if (sect_name && module_sp)
626 // Give the symbol vendor a chance to add to the unified section list.
627 module_sp->GetSymbolVendor();
628 SectionList *section_list = module_sp->GetSectionList();
631 ConstString const_sect_name(sect_name);
632 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
635 sb_section.SetSP (section_sp);
643 SBModule::GetByteOrder ()
645 ModuleSP module_sp (GetSP ());
647 return module_sp->GetArchitecture().GetByteOrder();
648 return eByteOrderInvalid;
652 SBModule::GetTriple ()
654 ModuleSP module_sp (GetSP ());
657 std::string triple (module_sp->GetArchitecture().GetTriple().str());
658 // Unique the string so we don't run into ownership issues since
659 // the const strings put the string into the string pool once and
660 // the strings never comes out
661 ConstString const_triple (triple.c_str());
662 return const_triple.GetCString();
668 SBModule::GetAddressByteSize()
670 ModuleSP module_sp (GetSP ());
672 return module_sp->GetArchitecture().GetAddressByteSize();
673 return sizeof(void*);
678 SBModule::GetVersion (uint32_t *versions, uint32_t num_versions)
680 ModuleSP module_sp (GetSP ());
682 return module_sp->GetVersion(versions, num_versions);
685 if (versions && num_versions)
687 for (uint32_t i=0; i<num_versions; ++i)
688 versions[i] = UINT32_MAX;