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());
113 log->Printf ("SBModule(%p)::GetFileSpec () => SBFileSpec(%p)",
114 static_cast<void*>(module_sp.get()),
115 static_cast<const void*>(file_spec.get()));
121 SBModule::GetPlatformFileSpec () const
123 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
125 SBFileSpec file_spec;
126 ModuleSP module_sp (GetSP ());
128 file_spec.SetFileSpec(module_sp->GetPlatformFileSpec());
131 log->Printf ("SBModule(%p)::GetPlatformFileSpec () => SBFileSpec(%p)",
132 static_cast<void*>(module_sp.get()),
133 static_cast<const void*>(file_spec.get()));
139 SBModule::SetPlatformFileSpec (const lldb::SBFileSpec &platform_file)
142 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
144 ModuleSP module_sp (GetSP ());
147 module_sp->SetPlatformFileSpec(*platform_file);
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);
160 SBModule::GetRemoteInstallFileSpec ()
162 SBFileSpec sb_file_spec;
163 ModuleSP module_sp (GetSP ());
165 sb_file_spec.SetFileSpec (module_sp->GetRemoteInstallFileSpec());
170 SBModule::SetRemoteInstallFileSpec (lldb::SBFileSpec &file)
172 ModuleSP module_sp (GetSP ());
175 module_sp->SetRemoteInstallFileSpec(file.ref());
183 SBModule::GetUUIDBytes () const
185 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
187 const uint8_t *uuid_bytes = NULL;
188 ModuleSP module_sp (GetSP ());
190 uuid_bytes = (const uint8_t *)module_sp->GetUUID().GetBytes();
197 module_sp->GetUUID().Dump (&s);
198 log->Printf ("SBModule(%p)::GetUUIDBytes () => %s",
199 static_cast<void*>(module_sp.get()), s.GetData());
202 log->Printf ("SBModule(%p)::GetUUIDBytes () => NULL",
203 static_cast<void*>(module_sp.get()));
210 SBModule::GetUUIDString () const
212 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
214 static char uuid_string_buffer[80];
215 const char *uuid_c_string = NULL;
216 std::string uuid_string;
217 ModuleSP module_sp (GetSP ());
219 uuid_string = module_sp->GetUUID().GetAsString();
221 if (!uuid_string.empty())
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;
230 if (!uuid_string.empty())
233 module_sp->GetUUID().Dump (&s);
234 log->Printf ("SBModule(%p)::GetUUIDString () => %s",
235 static_cast<void*>(module_sp.get()), s.GetData());
238 log->Printf ("SBModule(%p)::GetUUIDString () => NULL",
239 static_cast<void*>(module_sp.get()));
241 return uuid_c_string;
246 SBModule::operator == (const SBModule &rhs) const
249 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
254 SBModule::operator != (const SBModule &rhs) const
257 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
262 SBModule::GetSP () const
268 SBModule::SetSP (const ModuleSP &module_sp)
270 m_opaque_sp = module_sp;
274 SBModule::ResolveFileAddress (lldb::addr_t vm_addr)
276 lldb::SBAddress sb_addr;
277 ModuleSP module_sp (GetSP ());
281 if (module_sp->ResolveFileAddress (vm_addr, addr))
282 sb_addr.ref() = addr;
288 SBModule::ResolveSymbolContextForAddress (const SBAddress& addr, uint32_t resolve_scope)
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);
298 SBModule::GetDescription (SBStream &description)
300 Stream &strm = description.ref();
302 ModuleSP module_sp (GetSP ());
305 module_sp->GetDescription (&strm);
308 strm.PutCString ("No value");
314 SBModule::GetNumCompileUnits()
316 ModuleSP module_sp (GetSP ());
319 return module_sp->GetNumCompileUnits ();
325 SBModule::GetCompileUnitAtIndex (uint32_t index)
328 ModuleSP module_sp (GetSP ());
331 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex (index);
332 sb_cu.reset(cu_sp.get());
338 GetUnifiedSymbolTable (const lldb::ModuleSP& module_sp)
342 SymbolVendor *symbols = module_sp->GetSymbolVendor();
344 return symbols->GetSymtab();
350 SBModule::GetNumSymbols ()
352 ModuleSP module_sp (GetSP ());
355 Symtab *symtab = GetUnifiedSymbolTable (module_sp);
357 return symtab->GetNumSymbols();
363 SBModule::GetSymbolAtIndex (size_t idx)
366 ModuleSP module_sp (GetSP ());
367 Symtab *symtab = GetUnifiedSymbolTable (module_sp);
369 sb_symbol.SetSymbol(symtab->SymbolAtIndex (idx));
374 SBModule::FindSymbol (const char *name,
375 lldb::SymbolType symbol_type)
380 ModuleSP module_sp (GetSP ());
381 Symtab *symtab = GetUnifiedSymbolTable (module_sp);
383 sb_symbol.SetSymbol(symtab->FindFirstSymbolWithNameAndType(ConstString(name), symbol_type, Symtab::eDebugAny, Symtab::eVisibilityAny));
389 lldb::SBSymbolContextList
390 SBModule::FindSymbols (const char *name, lldb::SymbolType symbol_type)
392 SBSymbolContextList sb_sc_list;
395 ModuleSP module_sp (GetSP ());
396 Symtab *symtab = GetUnifiedSymbolTable (module_sp);
399 std::vector<uint32_t> matching_symbol_indexes;
400 const size_t num_matches = symtab->FindAllSymbolsWithNameAndType(ConstString(name), symbol_type, matching_symbol_indexes);
404 sc.module_sp = module_sp;
405 SymbolContextList &sc_list = *sb_sc_list;
406 for (size_t i=0; i<num_matches; ++i)
408 sc.symbol = symtab->SymbolAtIndex (matching_symbol_indexes[i]);
422 SBModule::GetNumSections ()
424 ModuleSP module_sp (GetSP ());
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();
431 return section_list->GetSize();
437 SBModule::GetSectionAtIndex (size_t idx)
439 SBSection sb_section;
440 ModuleSP module_sp (GetSP ());
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 ();
448 sb_section.SetSP(section_list->GetSectionAtIndex (idx));
453 lldb::SBSymbolContextList
454 SBModule::FindFunctions (const char *name,
455 uint32_t name_type_mask)
457 lldb::SBSymbolContextList sb_sc_list;
458 ModuleSP module_sp (GetSP ());
459 if (name && module_sp)
461 const bool append = true;
462 const bool symbols_ok = true;
463 const bool inlines_ok = true;
464 module_sp->FindFunctions (ConstString(name),
477 SBModule::FindGlobalVariables (SBTarget &target, const char *name, uint32_t max_matches)
479 SBValueList sb_value_list;
480 ModuleSP module_sp (GetSP ());
481 if (name && module_sp)
483 VariableList variable_list;
484 const uint32_t match_count = module_sp->FindGlobalVariables (ConstString (name),
492 for (uint32_t i=0; i<match_count; ++i)
494 lldb::ValueObjectSP valobj_sp;
495 TargetSP target_sp (target.GetSP());
496 valobj_sp = ValueObjectVariable::Create (target_sp.get(), variable_list.GetVariableAtIndex(i));
498 sb_value_list.Append(SBValue(valobj_sp));
503 return sb_value_list;
507 SBModule::FindFirstGlobalVariable (lldb::SBTarget &target, const char *name)
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);
516 SBModule::FindFirstType (const char *name_cstr)
519 ModuleSP module_sp (GetSP ());
520 if (name_cstr && module_sp)
523 const bool exact_match = false;
524 ConstString name(name_cstr);
526 sb_type = SBType (module_sp->FindFirstType(sc, name, exact_match));
528 if (!sb_type.IsValid())
529 sb_type = SBType (ClangASTContext::GetBasicType (module_sp->GetClangASTContext().getASTContext(), name));
535 SBModule::GetBasicType(lldb::BasicType type)
537 ModuleSP module_sp (GetSP ());
539 return SBType (ClangASTContext::GetBasicType (module_sp->GetClangASTContext().getASTContext(), type));
544 SBModule::FindTypes (const char *type)
548 ModuleSP module_sp (GetSP ());
549 if (type && module_sp)
553 const bool exact_match = false;
554 ConstString name(type);
555 const uint32_t num_matches = module_sp->FindTypes (sc,
563 for (size_t idx = 0; idx < num_matches; idx++)
565 TypeSP type_sp (type_list.GetTypeAtIndex(idx));
567 retval.Append(SBType(type_sp));
572 SBType sb_type(ClangASTContext::GetBasicType (module_sp->GetClangASTContext().getASTContext(), name));
573 if (sb_type.IsValid())
574 retval.Append(sb_type);
582 SBModule::GetTypeByID (lldb::user_id_t uid)
584 ModuleSP module_sp (GetSP ());
587 SymbolVendor* vendor = module_sp->GetSymbolVendor();
590 Type *type_ptr = vendor->ResolveTypeUID(uid);
592 return SBType(type_ptr->shared_from_this());
599 SBModule::GetTypes (uint32_t type_mask)
601 SBTypeList sb_type_list;
603 ModuleSP module_sp (GetSP ());
606 SymbolVendor* vendor = module_sp->GetSymbolVendor();
610 vendor->GetTypes (NULL, type_mask, type_list);
611 sb_type_list.m_opaque_ap->Append(type_list);
618 SBModule::FindSection (const char *sect_name)
620 SBSection sb_section;
622 ModuleSP module_sp (GetSP ());
623 if (sect_name && module_sp)
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();
630 ConstString const_sect_name(sect_name);
631 SectionSP section_sp (section_list->FindSectionByName(const_sect_name));
634 sb_section.SetSP (section_sp);
642 SBModule::GetByteOrder ()
644 ModuleSP module_sp (GetSP ());
646 return module_sp->GetArchitecture().GetByteOrder();
647 return eByteOrderInvalid;
651 SBModule::GetTriple ()
653 ModuleSP module_sp (GetSP ());
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();
667 SBModule::GetAddressByteSize()
669 ModuleSP module_sp (GetSP ());
671 return module_sp->GetArchitecture().GetAddressByteSize();
672 return sizeof(void*);
677 SBModule::GetVersion (uint32_t *versions, uint32_t num_versions)
679 ModuleSP module_sp (GetSP ());
681 return module_sp->GetVersion(versions, num_versions);
684 if (versions && num_versions)
686 for (uint32_t i=0; i<num_versions; ++i)
687 versions[i] = UINT32_MAX;