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