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 34 SBModule::SBModule() : m_opaque_sp() {} 35 36 SBModule::SBModule(const lldb::ModuleSP &module_sp) : m_opaque_sp(module_sp) {} 37 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 46 SBModule::SBModule(const SBModule &rhs) : m_opaque_sp(rhs.m_opaque_sp) {} 47 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 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 68 SBModule::~SBModule() {} 69 70 bool SBModule::IsValid() const { return m_opaque_sp.get() != NULL; } 71 72 void SBModule::Clear() { m_opaque_sp.reset(); } 73 74 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 90 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 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 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 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 141 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 = (const uint8_t *)module_sp->GetUUID().GetBytes(); 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 162 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 188 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 194 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 200 ModuleSP SBModule::GetSP() const { return m_opaque_sp; } 201 202 void SBModule::SetSP(const ModuleSP &module_sp) { m_opaque_sp = module_sp; } 203 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 216 SBModule::ResolveSymbolContextForAddress(const SBAddress &addr, 217 uint32_t resolve_scope) { 218 SBSymbolContext sb_sc; 219 ModuleSP module_sp(GetSP()); 220 if (module_sp && addr.IsValid()) 221 module_sp->ResolveSymbolContextForAddress(addr.ref(), resolve_scope, 222 *sb_sc); 223 return sb_sc; 224 } 225 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 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 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 static Symtab *GetUnifiedSymbolTable(const lldb::ModuleSP &module_sp) { 257 if (module_sp) { 258 SymbolVendor *symbols = module_sp->GetSymbolVendor(); 259 if (symbols) 260 return symbols->GetSymtab(); 261 } 262 return NULL; 263 } 264 265 size_t SBModule::GetNumSymbols() { 266 ModuleSP module_sp(GetSP()); 267 if (module_sp) { 268 Symtab *symtab = GetUnifiedSymbolTable(module_sp); 269 if (symtab) 270 return symtab->GetNumSymbols(); 271 } 272 return 0; 273 } 274 275 SBSymbol SBModule::GetSymbolAtIndex(size_t idx) { 276 SBSymbol sb_symbol; 277 ModuleSP module_sp(GetSP()); 278 Symtab *symtab = GetUnifiedSymbolTable(module_sp); 279 if (symtab) 280 sb_symbol.SetSymbol(symtab->SymbolAtIndex(idx)); 281 return sb_symbol; 282 } 283 284 lldb::SBSymbol SBModule::FindSymbol(const char *name, 285 lldb::SymbolType symbol_type) { 286 SBSymbol sb_symbol; 287 if (name && name[0]) { 288 ModuleSP module_sp(GetSP()); 289 Symtab *symtab = GetUnifiedSymbolTable(module_sp); 290 if (symtab) 291 sb_symbol.SetSymbol(symtab->FindFirstSymbolWithNameAndType( 292 ConstString(name), symbol_type, Symtab::eDebugAny, 293 Symtab::eVisibilityAny)); 294 } 295 return sb_symbol; 296 } 297 298 lldb::SBSymbolContextList SBModule::FindSymbols(const char *name, 299 lldb::SymbolType symbol_type) { 300 SBSymbolContextList sb_sc_list; 301 if (name && name[0]) { 302 ModuleSP module_sp(GetSP()); 303 Symtab *symtab = GetUnifiedSymbolTable(module_sp); 304 if (symtab) { 305 std::vector<uint32_t> matching_symbol_indexes; 306 const size_t num_matches = symtab->FindAllSymbolsWithNameAndType( 307 ConstString(name), symbol_type, matching_symbol_indexes); 308 if (num_matches) { 309 SymbolContext sc; 310 sc.module_sp = module_sp; 311 SymbolContextList &sc_list = *sb_sc_list; 312 for (size_t i = 0; i < num_matches; ++i) { 313 sc.symbol = symtab->SymbolAtIndex(matching_symbol_indexes[i]); 314 if (sc.symbol) 315 sc_list.Append(sc); 316 } 317 } 318 } 319 } 320 return sb_sc_list; 321 } 322 323 size_t SBModule::GetNumSections() { 324 ModuleSP module_sp(GetSP()); 325 if (module_sp) { 326 // Give the symbol vendor a chance to add to the unified section list. 327 module_sp->GetSymbolVendor(); 328 SectionList *section_list = module_sp->GetSectionList(); 329 if (section_list) 330 return section_list->GetSize(); 331 } 332 return 0; 333 } 334 335 SBSection SBModule::GetSectionAtIndex(size_t idx) { 336 SBSection sb_section; 337 ModuleSP module_sp(GetSP()); 338 if (module_sp) { 339 // Give the symbol vendor a chance to add to the unified section list. 340 module_sp->GetSymbolVendor(); 341 SectionList *section_list = module_sp->GetSectionList(); 342 343 if (section_list) 344 sb_section.SetSP(section_list->GetSectionAtIndex(idx)); 345 } 346 return sb_section; 347 } 348 349 lldb::SBSymbolContextList SBModule::FindFunctions(const char *name, 350 uint32_t name_type_mask) { 351 lldb::SBSymbolContextList sb_sc_list; 352 ModuleSP module_sp(GetSP()); 353 if (name && module_sp) { 354 const bool append = true; 355 const bool symbols_ok = true; 356 const bool inlines_ok = true; 357 module_sp->FindFunctions(ConstString(name), NULL, name_type_mask, 358 symbols_ok, inlines_ok, append, *sb_sc_list); 359 } 360 return sb_sc_list; 361 } 362 363 SBValueList SBModule::FindGlobalVariables(SBTarget &target, const char *name, 364 uint32_t max_matches) { 365 SBValueList sb_value_list; 366 ModuleSP module_sp(GetSP()); 367 if (name && module_sp) { 368 VariableList variable_list; 369 const uint32_t match_count = module_sp->FindGlobalVariables( 370 ConstString(name), NULL, false, max_matches, variable_list); 371 372 if (match_count > 0) { 373 for (uint32_t i = 0; i < match_count; ++i) { 374 lldb::ValueObjectSP valobj_sp; 375 TargetSP target_sp(target.GetSP()); 376 valobj_sp = ValueObjectVariable::Create( 377 target_sp.get(), variable_list.GetVariableAtIndex(i)); 378 if (valobj_sp) 379 sb_value_list.Append(SBValue(valobj_sp)); 380 } 381 } 382 } 383 384 return sb_value_list; 385 } 386 387 lldb::SBValue SBModule::FindFirstGlobalVariable(lldb::SBTarget &target, 388 const char *name) { 389 SBValueList sb_value_list(FindGlobalVariables(target, name, 1)); 390 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0) 391 return sb_value_list.GetValueAtIndex(0); 392 return SBValue(); 393 } 394 395 lldb::SBType SBModule::FindFirstType(const char *name_cstr) { 396 SBType sb_type; 397 ModuleSP module_sp(GetSP()); 398 if (name_cstr && module_sp) { 399 SymbolContext sc; 400 const bool exact_match = false; 401 ConstString name(name_cstr); 402 403 sb_type = SBType(module_sp->FindFirstType(sc, name, exact_match)); 404 405 if (!sb_type.IsValid()) { 406 TypeSystem *type_system = 407 module_sp->GetTypeSystemForLanguage(eLanguageTypeC); 408 if (type_system) 409 sb_type = SBType(type_system->GetBuiltinTypeByName(name)); 410 } 411 } 412 return sb_type; 413 } 414 415 lldb::SBType SBModule::GetBasicType(lldb::BasicType type) { 416 ModuleSP module_sp(GetSP()); 417 if (module_sp) { 418 TypeSystem *type_system = 419 module_sp->GetTypeSystemForLanguage(eLanguageTypeC); 420 if (type_system) 421 return SBType(type_system->GetBasicTypeFromAST(type)); 422 } 423 return SBType(); 424 } 425 426 lldb::SBTypeList SBModule::FindTypes(const char *type) { 427 SBTypeList retval; 428 429 ModuleSP module_sp(GetSP()); 430 if (type && module_sp) { 431 SymbolContext sc; 432 TypeList type_list; 433 const bool exact_match = false; 434 ConstString name(type); 435 llvm::DenseSet<SymbolFile *> searched_symbol_files; 436 const uint32_t num_matches = module_sp->FindTypes( 437 sc, name, exact_match, UINT32_MAX, searched_symbol_files, type_list); 438 439 if (num_matches > 0) { 440 for (size_t idx = 0; idx < num_matches; idx++) { 441 TypeSP type_sp(type_list.GetTypeAtIndex(idx)); 442 if (type_sp) 443 retval.Append(SBType(type_sp)); 444 } 445 } else { 446 TypeSystem *type_system = 447 module_sp->GetTypeSystemForLanguage(eLanguageTypeC); 448 if (type_system) { 449 CompilerType compiler_type = type_system->GetBuiltinTypeByName(name); 450 if (compiler_type) 451 retval.Append(SBType(compiler_type)); 452 } 453 } 454 } 455 456 return retval; 457 } 458 459 lldb::SBType SBModule::GetTypeByID(lldb::user_id_t uid) { 460 ModuleSP module_sp(GetSP()); 461 if (module_sp) { 462 SymbolVendor *vendor = module_sp->GetSymbolVendor(); 463 if (vendor) { 464 Type *type_ptr = vendor->ResolveTypeUID(uid); 465 if (type_ptr) 466 return SBType(type_ptr->shared_from_this()); 467 } 468 } 469 return SBType(); 470 } 471 472 lldb::SBTypeList SBModule::GetTypes(uint32_t type_mask) { 473 SBTypeList sb_type_list; 474 475 ModuleSP module_sp(GetSP()); 476 if (module_sp) { 477 SymbolVendor *vendor = module_sp->GetSymbolVendor(); 478 if (vendor) { 479 TypeList type_list; 480 vendor->GetTypes(NULL, type_mask, type_list); 481 sb_type_list.m_opaque_ap->Append(type_list); 482 } 483 } 484 return sb_type_list; 485 } 486 487 SBSection SBModule::FindSection(const char *sect_name) { 488 SBSection sb_section; 489 490 ModuleSP module_sp(GetSP()); 491 if (sect_name && module_sp) { 492 // Give the symbol vendor a chance to add to the unified section list. 493 module_sp->GetSymbolVendor(); 494 SectionList *section_list = module_sp->GetSectionList(); 495 if (section_list) { 496 ConstString const_sect_name(sect_name); 497 SectionSP section_sp(section_list->FindSectionByName(const_sect_name)); 498 if (section_sp) { 499 sb_section.SetSP(section_sp); 500 } 501 } 502 } 503 return sb_section; 504 } 505 506 lldb::ByteOrder SBModule::GetByteOrder() { 507 ModuleSP module_sp(GetSP()); 508 if (module_sp) 509 return module_sp->GetArchitecture().GetByteOrder(); 510 return eByteOrderInvalid; 511 } 512 513 const char *SBModule::GetTriple() { 514 ModuleSP module_sp(GetSP()); 515 if (module_sp) { 516 std::string triple(module_sp->GetArchitecture().GetTriple().str()); 517 // Unique the string so we don't run into ownership issues since the const 518 // strings put the string into the string pool once and the strings never 519 // comes out 520 ConstString const_triple(triple.c_str()); 521 return const_triple.GetCString(); 522 } 523 return NULL; 524 } 525 526 uint32_t SBModule::GetAddressByteSize() { 527 ModuleSP module_sp(GetSP()); 528 if (module_sp) 529 return module_sp->GetArchitecture().GetAddressByteSize(); 530 return sizeof(void *); 531 } 532 533 uint32_t SBModule::GetVersion(uint32_t *versions, uint32_t num_versions) { 534 ModuleSP module_sp(GetSP()); 535 if (module_sp) 536 return module_sp->GetVersion(versions, num_versions); 537 else { 538 if (versions && num_versions) { 539 for (uint32_t i = 0; i < num_versions; ++i) 540 versions[i] = UINT32_MAX; 541 } 542 return 0; 543 } 544 } 545 546 lldb::SBFileSpec SBModule::GetSymbolFileSpec() const { 547 lldb::SBFileSpec sb_file_spec; 548 ModuleSP module_sp(GetSP()); 549 if (module_sp) { 550 SymbolVendor *symbol_vendor_ptr = module_sp->GetSymbolVendor(); 551 if (symbol_vendor_ptr) 552 sb_file_spec.SetFileSpec(symbol_vendor_ptr->GetMainFileSpec()); 553 } 554 return sb_file_spec; 555 } 556 557 lldb::SBAddress SBModule::GetObjectFileHeaderAddress() const { 558 lldb::SBAddress sb_addr; 559 ModuleSP module_sp(GetSP()); 560 if (module_sp) { 561 ObjectFile *objfile_ptr = module_sp->GetObjectFile(); 562 if (objfile_ptr) 563 sb_addr.ref() = objfile_ptr->GetHeaderAddress(); 564 } 565 return sb_addr; 566 } 567