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, 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(),
168 SBModule::GetUUIDBytes () const
170 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
172 const uint8_t *uuid_bytes = NULL;
173 ModuleSP module_sp (GetSP ());
175 uuid_bytes = (const uint8_t *)module_sp->GetUUID().GetBytes();
182 module_sp->GetUUID().Dump (&s);
183 log->Printf ("SBModule(%p)::GetUUIDBytes () => %s", module_sp.get(), s.GetData());
186 log->Printf ("SBModule(%p)::GetUUIDBytes () => NULL", module_sp.get());
193 SBModule::GetUUIDString () const
195 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
197 static char uuid_string_buffer[80];
198 const char *uuid_c_string = NULL;
199 std::string uuid_string;
200 ModuleSP module_sp (GetSP ());
202 uuid_string = module_sp->GetUUID().GetAsString();
204 if (!uuid_string.empty())
206 strncpy (uuid_string_buffer, uuid_string.c_str(), sizeof (uuid_string_buffer));
207 uuid_c_string = uuid_string_buffer;
212 if (!uuid_string.empty())
215 module_sp->GetUUID().Dump (&s);
216 log->Printf ("SBModule(%p)::GetUUIDString () => %s", module_sp.get(), s.GetData());
219 log->Printf ("SBModule(%p)::GetUUIDString () => NULL", module_sp.get());
221 return uuid_c_string;
226 SBModule::operator == (const SBModule &rhs) const
229 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
234 SBModule::operator != (const SBModule &rhs) const
237 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
242 SBModule::GetSP () const
248 SBModule::SetSP (const ModuleSP &module_sp)
250 m_opaque_sp = module_sp;
254 SBModule::ResolveFileAddress (lldb::addr_t vm_addr)
256 lldb::SBAddress sb_addr;
257 ModuleSP module_sp (GetSP ());
261 if (module_sp->ResolveFileAddress (vm_addr, addr))
262 sb_addr.ref() = addr;
268 SBModule::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
270 SBSymbolContext sb_sc;
271 ModuleSP module_sp (GetSP ());
272 if (module_sp && addr.IsValid())
273 module_sp->ResolveSymbolContextForAddress (addr.ref(), resolve_scope, *sb_sc);
278 SBModule::GetDescription (SBStream &description)
280 Stream &strm = description.ref();
282 ModuleSP module_sp (GetSP ());
285 module_sp->GetDescription (&strm);
288 strm.PutCString ("No value");
294 SBModule::GetNumCompileUnits()
296 ModuleSP module_sp (GetSP ());
299 return module_sp->GetNumCompileUnits ();
305 SBModule::GetCompileUnitAtIndex (uint32_t index)
308 ModuleSP module_sp (GetSP ());
311 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex (index);
312 sb_cu.reset(cu_sp.get());
318 GetUnifiedSymbolTable (const lldb::ModuleSP& module_sp)
322 SymbolVendor *symbols = module_sp->GetSymbolVendor();
324 return symbols->GetSymtab();
330 SBModule::GetNumSymbols ()
332 ModuleSP module_sp (GetSP ());
335 Symtab *symtab = GetUnifiedSymbolTable (module_sp);
337 return symtab->GetNumSymbols();
343 SBModule::GetSymbolAtIndex (size_t idx)
346 ModuleSP module_sp (GetSP ());
347 Symtab *symtab = GetUnifiedSymbolTable (module_sp);
349 sb_symbol.SetSymbol(symtab->SymbolAtIndex (idx));
354 SBModule::FindSymbol (const char *name,
355 lldb::SymbolType symbol_type)
360 ModuleSP module_sp (GetSP ());
361 Symtab *symtab = GetUnifiedSymbolTable (module_sp);
363 sb_symbol.SetSymbol(symtab->FindFirstSymbolWithNameAndType(ConstString(name), symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny));
369 lldb::SBSymbolContextList
370 SBModule::FindSymbols (const char *name, lldb::SymbolType symbol_type)
372 SBSymbolContextList sb_sc_list;
375 ModuleSP module_sp (GetSP ());
376 Symtab *symtab = GetUnifiedSymbolTable (module_sp);
379 std::vector<uint32_t> matching_symbol_indexes;
380 const size_t num_matches = symtab->FindAllSymbolsWithNameAndType(ConstString(name), symbol_type, matching_symbol_indexes);
384 sc.module_sp = module_sp;
385 SymbolContextList &sc_list = *sb_sc_list;
386 for (size_t i=0; i<num_matches; ++i)
388 sc.symbol = symtab->SymbolAtIndex (matching_symbol_indexes[i]);
402 SBModule::GetNumSections ()
404 ModuleSP module_sp (GetSP ());
407 // Give the symbol vendor a chance to add to the unified section list.
408 module_sp->GetSymbolVendor();
409 SectionList *section_list = module_sp->GetSectionList();
411 return section_list->GetSize();
417 SBModule::GetSectionAtIndex (size_t idx)
419 SBSection sb_section;
420 ModuleSP module_sp (GetSP ());
423 // Give the symbol vendor a chance to add to the unified section list.
424 module_sp->GetSymbolVendor();
425 SectionList *section_list = module_sp->GetSectionList ();
428 sb_section.SetSP(section_list->GetSectionAtIndex (idx));
433 lldb::SBSymbolContextList
434 SBModule::FindFunctions (const char *name,
435 uint32_t name_type_mask)
437 lldb::SBSymbolContextList sb_sc_list;
438 ModuleSP module_sp (GetSP ());
439 if (name && module_sp)
441 const bool append = true;
442 const bool symbols_ok = true;
443 const bool inlines_ok = true;
444 module_sp->FindFunctions (ConstString(name),
457 SBModule::FindGlobalVariables (SBTarget &target, const char *name, uint32_t max_matches)
459 SBValueList sb_value_list;
460 ModuleSP module_sp (GetSP ());
461 if (name && module_sp)
463 VariableList variable_list;
464 const uint32_t match_count = module_sp->FindGlobalVariables (ConstString (name),
472 for (uint32_t i=0; i<match_count; ++i)
474 lldb::ValueObjectSP valobj_sp;
475 TargetSP target_sp (target.GetSP());
476 valobj_sp = ValueObjectVariable::Create (target_sp.get(), variable_list.GetVariableAtIndex(i));
478 sb_value_list.Append(SBValue(valobj_sp));
483 return sb_value_list;
487 SBModule::FindFirstGlobalVariable (lldb::SBTarget &target, const char *name)
489 SBValueList sb_value_list(FindGlobalVariables(target, name, 1));
490 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0)
491 return sb_value_list.GetValueAtIndex(0);
496 SBModule::FindFirstType (const char *name_cstr)
499 ModuleSP module_sp (GetSP ());
500 if (name_cstr && module_sp)
503 const bool exact_match = false;
504 ConstString name(name_cstr);
506 sb_type = SBType (module_sp->FindFirstType(sc, name, exact_match));
508 if (!sb_type.IsValid())
509 sb_type = SBType (ClangASTContext::GetBasicType (module_sp->GetClangASTContext().getASTContext(), name));
515 SBModule::GetBasicType(lldb::BasicType type)
517 ModuleSP module_sp (GetSP ());
519 return SBType (ClangASTContext::GetBasicType (module_sp->GetClangASTContext().getASTContext(), type));
524 SBModule::FindTypes (const char *type)
528 ModuleSP module_sp (GetSP ());
529 if (type && module_sp)
533 const bool exact_match = false;
534 ConstString name(type);
535 const uint32_t num_matches = module_sp->FindTypes (sc,
543 for (size_t idx = 0; idx < num_matches; idx++)
545 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
547 retval.Append(SBType(type_sp));
552 SBType sb_type(ClangASTContext::GetBasicType (module_sp->GetClangASTContext().getASTContext(), name));
553 if (sb_type.IsValid())
554 retval.Append(sb_type);
562 SBModule::GetTypes (uint32_t type_mask)
564 SBTypeList sb_type_list;
566 ModuleSP module_sp (GetSP ());
569 SymbolVendor* vendor = module_sp->GetSymbolVendor();
573 vendor->GetTypes (NULL, type_mask, type_list);
574 sb_type_list.m_opaque_ap->Append(type_list);
581 SBModule::FindSection (const char *sect_name)
583 SBSection sb_section;
585 ModuleSP module_sp (GetSP ());
586 if (sect_name && module_sp)
588 // Give the symbol vendor a chance to add to the unified section list.
589 module_sp->GetSymbolVendor();
590 SectionList *section_list = module_sp->GetSectionList();
593 ConstString const_sect_name(sect_name);
594 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
597 sb_section.SetSP (section_sp);
605 SBModule::GetByteOrder ()
607 ModuleSP module_sp (GetSP ());
609 return module_sp->GetArchitecture().GetByteOrder();
610 return eByteOrderInvalid;
614 SBModule::GetTriple ()
616 ModuleSP module_sp (GetSP ());
619 std::string triple (module_sp->GetArchitecture().GetTriple().str());
620 // Unique the string so we don't run into ownership issues since
621 // the const strings put the string into the string pool once and
622 // the strings never comes out
623 ConstString const_triple (triple.c_str());
624 return const_triple.GetCString();
630 SBModule::GetAddressByteSize()
632 ModuleSP module_sp (GetSP ());
634 return module_sp->GetArchitecture().GetAddressByteSize();
635 return sizeof(void*);
640 SBModule::GetVersion (uint32_t *versions, uint32_t num_versions)
642 ModuleSP module_sp (GetSP ());
644 return module_sp->GetVersion(versions, num_versions);
647 if (versions && num_versions)
649 for (uint32_t i=0; i<num_versions; ++i)
650 versions[i] = UINT32_MAX;