MCPcopy Create free account
hub / github.com/bloomberg/pystack / getMemoryLocationFromElf

Method getMemoryLocationFromElf

src/pystack/_pystack/mem.cpp:496–541  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

494}
495
496CorefileRemoteMemoryManager::StatusCode
497CorefileRemoteMemoryManager::getMemoryLocationFromElf(
498 remote_addr_t addr,
499 const std::string** filename,
500 off_t* offset_in_file) const
501{
502 auto shared_libs_it = std::find_if(d_shared_libs.cbegin(), d_shared_libs.cend(), [&](auto& map) {
503 return map.start <= addr && addr < map.end;
504 });
505
506 if (shared_libs_it == d_shared_libs.cend()) {
507 return StatusCode::ERROR;
508 }
509
510 *filename = &shared_libs_it->filename;
511
512 // Check if we have cached segments for this file
513 auto cache_it = d_elf_load_segments_cache.find(**filename);
514 if (cache_it == d_elf_load_segments_cache.end()) {
515 // Initialize segments if not in cache
516 if (initLoadSegments(**filename) != StatusCode::SUCCESS) {
517 return StatusCode::ERROR;
518 }
519 cache_it = d_elf_load_segments_cache.find(**filename);
520 }
521
522 // Get the load address of the elf file from its first segment
523 remote_addr_t elf_load_addr = cache_it->second[0].vaddr;
524
525 // Now relocate the address to the elf file
526 remote_addr_t symbol_vaddr = addr - shared_libs_it->start + elf_load_addr;
527
528 // Find the segment containing this address
529 for (const auto& segment : cache_it->second) {
530 if (symbol_vaddr >= segment.vaddr && symbol_vaddr < segment.vaddr + segment.size) {
531 *offset_in_file = (symbol_vaddr - segment.vaddr) + segment.offset;
532 return StatusCode::SUCCESS;
533 }
534 }
535
536 LOG(ERROR) << "Failed to find the correct segment for address " << std::hex << std::showbase << addr
537 << " (with vaddr offset " << symbol_vaddr << ")"
538 << " in file " << **filename;
539
540 return StatusCode::ERROR;
541}
542
543bool
544CorefileRemoteMemoryManager::isAddressValid(remote_addr_t addr, const VirtualMap& map) const

Callers

nothing calls this directly

Calls 1

LOGClass · 0.85

Tested by

no test coverage detected