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 169 // API, so we need to constify it so it gets added permanently the 170 // string pool and then we don't need to worry about the lifetime of the 171 // string as it will never go away once it has been put into the ConstString 172 // string pool 173 uuid_cstr = ConstString(module_sp->GetUUID().GetAsString()).GetCString(); 174 } 175 176 if (uuid_cstr && uuid_cstr[0]) { 177 if (log) 178 log->Printf("SBModule(%p)::GetUUIDString () => %s", 179 static_cast<void *>(module_sp.get()), uuid_cstr); 180 return uuid_cstr; 181 } 182 183 if (log) 184 log->Printf("SBModule(%p)::GetUUIDString () => NULL", 185 static_cast<void *>(module_sp.get())); 186 return NULL; 187 } 188 189 bool SBModule::operator==(const SBModule &rhs) const { 190 if (m_opaque_sp) 191 return m_opaque_sp.get() == rhs.m_opaque_sp.get(); 192 return false; 193 } 194 195 bool SBModule::operator!=(const SBModule &rhs) const { 196 if (m_opaque_sp) 197 return m_opaque_sp.get() != rhs.m_opaque_sp.get(); 198 return false; 199 } 200 201 ModuleSP SBModule::GetSP() const { return m_opaque_sp; } 202 203 void SBModule::SetSP(const ModuleSP &module_sp) { m_opaque_sp = module_sp; } 204 205 SBAddress SBModule::ResolveFileAddress(lldb::addr_t vm_addr) { 206 lldb::SBAddress sb_addr; 207 ModuleSP module_sp(GetSP()); 208 if (module_sp) { 209 Address addr; 210 if (module_sp->ResolveFileAddress(vm_addr, addr)) 211 sb_addr.ref() = addr; 212 } 213 return sb_addr; 214 } 215 216 SBSymbolContext 217 SBModule::ResolveSymbolContextForAddress(const SBAddress &addr, 218 uint32_t resolve_scope) { 219 SBSymbolContext sb_sc; 220 ModuleSP module_sp(GetSP()); 221 if (module_sp && addr.IsValid()) 222 module_sp->ResolveSymbolContextForAddress(addr.ref(), resolve_scope, 223 *sb_sc); 224 return sb_sc; 225 } 226 227 bool SBModule::GetDescription(SBStream &description) { 228 Stream &strm = description.ref(); 229 230 ModuleSP module_sp(GetSP()); 231 if (module_sp) { 232 module_sp->GetDescription(&strm); 233 } else 234 strm.PutCString("No value"); 235 236 return true; 237 } 238 239 uint32_t SBModule::GetNumCompileUnits() { 240 ModuleSP module_sp(GetSP()); 241 if (module_sp) { 242 return module_sp->GetNumCompileUnits(); 243 } 244 return 0; 245 } 246 247 SBCompileUnit SBModule::GetCompileUnitAtIndex(uint32_t index) { 248 SBCompileUnit sb_cu; 249 ModuleSP module_sp(GetSP()); 250 if (module_sp) { 251 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(index); 252 sb_cu.reset(cu_sp.get()); 253 } 254 return sb_cu; 255 } 256 257 static Symtab *GetUnifiedSymbolTable(const lldb::ModuleSP &module_sp) { 258 if (module_sp) { 259 SymbolVendor *symbols = module_sp->GetSymbolVendor(); 260 if (symbols) 261 return symbols->GetSymtab(); 262 } 263 return NULL; 264 } 265 266 size_t SBModule::GetNumSymbols() { 267 ModuleSP module_sp(GetSP()); 268 if (module_sp) { 269 Symtab *symtab = GetUnifiedSymbolTable(module_sp); 270 if (symtab) 271 return symtab->GetNumSymbols(); 272 } 273 return 0; 274 } 275 276 SBSymbol SBModule::GetSymbolAtIndex(size_t idx) { 277 SBSymbol sb_symbol; 278 ModuleSP module_sp(GetSP()); 279 Symtab *symtab = GetUnifiedSymbolTable(module_sp); 280 if (symtab) 281 sb_symbol.SetSymbol(symtab->SymbolAtIndex(idx)); 282 return sb_symbol; 283 } 284 285 lldb::SBSymbol SBModule::FindSymbol(const char *name, 286 lldb::SymbolType symbol_type) { 287 SBSymbol sb_symbol; 288 if (name && name[0]) { 289 ModuleSP module_sp(GetSP()); 290 Symtab *symtab = GetUnifiedSymbolTable(module_sp); 291 if (symtab) 292 sb_symbol.SetSymbol(symtab->FindFirstSymbolWithNameAndType( 293 ConstString(name), symbol_type, Symtab::eDebugAny, 294 Symtab::eVisibilityAny)); 295 } 296 return sb_symbol; 297 } 298 299 lldb::SBSymbolContextList SBModule::FindSymbols(const char *name, 300 lldb::SymbolType symbol_type) { 301 SBSymbolContextList sb_sc_list; 302 if (name && name[0]) { 303 ModuleSP module_sp(GetSP()); 304 Symtab *symtab = GetUnifiedSymbolTable(module_sp); 305 if (symtab) { 306 std::vector<uint32_t> matching_symbol_indexes; 307 const size_t num_matches = symtab->FindAllSymbolsWithNameAndType( 308 ConstString(name), symbol_type, matching_symbol_indexes); 309 if (num_matches) { 310 SymbolContext sc; 311 sc.module_sp = module_sp; 312 SymbolContextList &sc_list = *sb_sc_list; 313 for (size_t i = 0; i < num_matches; ++i) { 314 sc.symbol = symtab->SymbolAtIndex(matching_symbol_indexes[i]); 315 if (sc.symbol) 316 sc_list.Append(sc); 317 } 318 } 319 } 320 } 321 return sb_sc_list; 322 } 323 324 size_t SBModule::GetNumSections() { 325 ModuleSP module_sp(GetSP()); 326 if (module_sp) { 327 // Give the symbol vendor a chance to add to the unified section list. 328 module_sp->GetSymbolVendor(); 329 SectionList *section_list = module_sp->GetSectionList(); 330 if (section_list) 331 return section_list->GetSize(); 332 } 333 return 0; 334 } 335 336 SBSection SBModule::GetSectionAtIndex(size_t idx) { 337 SBSection sb_section; 338 ModuleSP module_sp(GetSP()); 339 if (module_sp) { 340 // Give the symbol vendor a chance to add to the unified section list. 341 module_sp->GetSymbolVendor(); 342 SectionList *section_list = module_sp->GetSectionList(); 343 344 if (section_list) 345 sb_section.SetSP(section_list->GetSectionAtIndex(idx)); 346 } 347 return sb_section; 348 } 349 350 lldb::SBSymbolContextList SBModule::FindFunctions(const char *name, 351 uint32_t name_type_mask) { 352 lldb::SBSymbolContextList sb_sc_list; 353 ModuleSP module_sp(GetSP()); 354 if (name && module_sp) { 355 const bool append = true; 356 const bool symbols_ok = true; 357 const bool inlines_ok = true; 358 module_sp->FindFunctions(ConstString(name), NULL, name_type_mask, 359 symbols_ok, inlines_ok, append, *sb_sc_list); 360 } 361 return sb_sc_list; 362 } 363 364 SBValueList SBModule::FindGlobalVariables(SBTarget &target, const char *name, 365 uint32_t max_matches) { 366 SBValueList sb_value_list; 367 ModuleSP module_sp(GetSP()); 368 if (name && module_sp) { 369 VariableList variable_list; 370 const uint32_t match_count = module_sp->FindGlobalVariables( 371 ConstString(name), NULL, false, max_matches, variable_list); 372 373 if (match_count > 0) { 374 for (uint32_t i = 0; i < match_count; ++i) { 375 lldb::ValueObjectSP valobj_sp; 376 TargetSP target_sp(target.GetSP()); 377 valobj_sp = ValueObjectVariable::Create( 378 target_sp.get(), variable_list.GetVariableAtIndex(i)); 379 if (valobj_sp) 380 sb_value_list.Append(SBValue(valobj_sp)); 381 } 382 } 383 } 384 385 return sb_value_list; 386 } 387 388 lldb::SBValue SBModule::FindFirstGlobalVariable(lldb::SBTarget &target, 389 const char *name) { 390 SBValueList sb_value_list(FindGlobalVariables(target, name, 1)); 391 if (sb_value_list.IsValid() && sb_value_list.GetSize() > 0) 392 return sb_value_list.GetValueAtIndex(0); 393 return SBValue(); 394 } 395 396 lldb::SBType SBModule::FindFirstType(const char *name_cstr) { 397 SBType sb_type; 398 ModuleSP module_sp(GetSP()); 399 if (name_cstr && module_sp) { 400 SymbolContext sc; 401 const bool exact_match = false; 402 ConstString name(name_cstr); 403 404 sb_type = SBType(module_sp->FindFirstType(sc, name, exact_match)); 405 406 if (!sb_type.IsValid()) { 407 TypeSystem *type_system = 408 module_sp->GetTypeSystemForLanguage(eLanguageTypeC); 409 if (type_system) 410 sb_type = SBType(type_system->GetBuiltinTypeByName(name)); 411 } 412 } 413 return sb_type; 414 } 415 416 lldb::SBType SBModule::GetBasicType(lldb::BasicType type) { 417 ModuleSP module_sp(GetSP()); 418 if (module_sp) { 419 TypeSystem *type_system = 420 module_sp->GetTypeSystemForLanguage(eLanguageTypeC); 421 if (type_system) 422 return SBType(type_system->GetBasicTypeFromAST(type)); 423 } 424 return SBType(); 425 } 426 427 lldb::SBTypeList SBModule::FindTypes(const char *type) { 428 SBTypeList retval; 429 430 ModuleSP module_sp(GetSP()); 431 if (type && module_sp) { 432 SymbolContext sc; 433 TypeList type_list; 434 const bool exact_match = false; 435 ConstString name(type); 436 llvm::DenseSet<SymbolFile *> searched_symbol_files; 437 const uint32_t num_matches = module_sp->FindTypes( 438 sc, name, exact_match, UINT32_MAX, searched_symbol_files, type_list); 439 440 if (num_matches > 0) { 441 for (size_t idx = 0; idx < num_matches; idx++) { 442 TypeSP type_sp(type_list.GetTypeAtIndex(idx)); 443 if (type_sp) 444 retval.Append(SBType(type_sp)); 445 } 446 } else { 447 TypeSystem *type_system = 448 module_sp->GetTypeSystemForLanguage(eLanguageTypeC); 449 if (type_system) { 450 CompilerType compiler_type = type_system->GetBuiltinTypeByName(name); 451 if (compiler_type) 452 retval.Append(SBType(compiler_type)); 453 } 454 } 455 } 456 457 return retval; 458 } 459 460 lldb::SBType SBModule::GetTypeByID(lldb::user_id_t uid) { 461 ModuleSP module_sp(GetSP()); 462 if (module_sp) { 463 SymbolVendor *vendor = module_sp->GetSymbolVendor(); 464 if (vendor) { 465 Type *type_ptr = vendor->ResolveTypeUID(uid); 466 if (type_ptr) 467 return SBType(type_ptr->shared_from_this()); 468 } 469 } 470 return SBType(); 471 } 472 473 lldb::SBTypeList SBModule::GetTypes(uint32_t type_mask) { 474 SBTypeList sb_type_list; 475 476 ModuleSP module_sp(GetSP()); 477 if (module_sp) { 478 SymbolVendor *vendor = module_sp->GetSymbolVendor(); 479 if (vendor) { 480 TypeList type_list; 481 vendor->GetTypes(NULL, type_mask, type_list); 482 sb_type_list.m_opaque_ap->Append(type_list); 483 } 484 } 485 return sb_type_list; 486 } 487 488 SBSection SBModule::FindSection(const char *sect_name) { 489 SBSection sb_section; 490 491 ModuleSP module_sp(GetSP()); 492 if (sect_name && module_sp) { 493 // Give the symbol vendor a chance to add to the unified section list. 494 module_sp->GetSymbolVendor(); 495 SectionList *section_list = module_sp->GetSectionList(); 496 if (section_list) { 497 ConstString const_sect_name(sect_name); 498 SectionSP section_sp(section_list->FindSectionByName(const_sect_name)); 499 if (section_sp) { 500 sb_section.SetSP(section_sp); 501 } 502 } 503 } 504 return sb_section; 505 } 506 507 lldb::ByteOrder SBModule::GetByteOrder() { 508 ModuleSP module_sp(GetSP()); 509 if (module_sp) 510 return module_sp->GetArchitecture().GetByteOrder(); 511 return eByteOrderInvalid; 512 } 513 514 const char *SBModule::GetTriple() { 515 ModuleSP module_sp(GetSP()); 516 if (module_sp) { 517 std::string triple(module_sp->GetArchitecture().GetTriple().str()); 518 // Unique the string so we don't run into ownership issues since 519 // the const strings put the string into the string pool once and 520 // the strings never comes out 521 ConstString const_triple(triple.c_str()); 522 return const_triple.GetCString(); 523 } 524 return NULL; 525 } 526 527 uint32_t SBModule::GetAddressByteSize() { 528 ModuleSP module_sp(GetSP()); 529 if (module_sp) 530 return module_sp->GetArchitecture().GetAddressByteSize(); 531 return sizeof(void *); 532 } 533 534 uint32_t SBModule::GetVersion(uint32_t *versions, uint32_t num_versions) { 535 ModuleSP module_sp(GetSP()); 536 if (module_sp) 537 return module_sp->GetVersion(versions, num_versions); 538 else { 539 if (versions && num_versions) { 540 for (uint32_t i = 0; i < num_versions; ++i) 541 versions[i] = UINT32_MAX; 542 } 543 return 0; 544 } 545 } 546 547 lldb::SBFileSpec SBModule::GetSymbolFileSpec() const { 548 lldb::SBFileSpec sb_file_spec; 549 ModuleSP module_sp(GetSP()); 550 if (module_sp) { 551 SymbolVendor *symbol_vendor_ptr = module_sp->GetSymbolVendor(); 552 if (symbol_vendor_ptr) 553 sb_file_spec.SetFileSpec(symbol_vendor_ptr->GetMainFileSpec()); 554 } 555 return sb_file_spec; 556 } 557 558 lldb::SBAddress SBModule::GetObjectFileHeaderAddress() const { 559 lldb::SBAddress sb_addr; 560 ModuleSP module_sp(GetSP()); 561 if (module_sp) { 562 ObjectFile *objfile_ptr = module_sp->GetObjectFile(); 563 if (objfile_ptr) 564 sb_addr.ref() = objfile_ptr->GetHeaderAddress(); 565 } 566 return sb_addr; 567 } 568