Files
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

404 lines
12 KiB
C++

// Copyright (c) Advanced Micro Devices, Inc.
// SPDX-License-Identifier: MIT
#include "core/config.hpp"
#include <spdlog/fmt/fmt.h>
#if !defined(TIMEMORY_USE_BFD)
# error "BFD support not enabled"
#endif
#define PACKAGE "rocprofiler-systems"
#define L_LNNO_SIZE 4
#include <elf.h>
// The RELR (relative-relocation) types were added to glibc's <elf.h> in glibc 2.36.
// Older glibc versions (e.g. RHEL 8/9) lack them, but they are referenced by the
// binutils headers below, so define the typedefs here to avoid compile errors.
#ifndef SHT_RELR
typedef Elf32_Word Elf32_Relr;
typedef Elf64_Xword Elf64_Relr;
#endif
#include <bfd.h>
#include <coff/external.h>
#include <coff/internal.h>
#include <cstddef>
#include <cstdio>
#include <dwarf.h>
#include <elf-bfd.h>
#include <elfutils/libdw.h>
#include <libcoff.h>
#include "common/path.hpp"
#include "core/binary/fwd.hpp"
#include "core/demangler.hpp"
#include "core/timemory.hpp"
#include "core/utility.hpp"
#include "dwarf_entry.hpp"
#include "scope_filter.hpp"
#include "symbol.hpp"
#include <timemory/mpl/concepts.hpp>
namespace rocprofsys
{
namespace binary
{
namespace
{
std::vector<inlined_symbol>
read_inliner_info(bfd* _inp)
{
auto _data = std::vector<inlined_symbol>{};
while(true)
{
const char* _file = nullptr;
const char* _func = nullptr;
unsigned int _line = 0;
if(bfd_find_inliner_info(_inp, &_file, &_func, &_line) != 0)
{
if(_file && _func && _line > 0)
_data.emplace_back(inlined_symbol{ _line, path::realpath(_file), _func });
}
else
{
break;
}
}
return _data;
}
} // namespace
symbol::symbol(const base_type& _v)
: base_type{ _v }
, address{ _v.address, _v.address + _v.symsize + 1 }
, func{ std::string{ base_type::name } }
{}
bool
symbol::operator==(const symbol& _rhs) const
{
return std::tie(address, base_type::name) ==
std::tie(_rhs.address, _rhs.base_type::name);
}
bool
symbol::operator<(const symbol& _rhs) const
{
// if both have non-zero load addresses that are not equal, compare based on load
// addresses
if(load_address > 0 && _rhs.load_address > 0 && load_address != _rhs.load_address)
return (load_address < _rhs.load_address);
// if address is same and name is same, return true if load_address is higher
if(address == _rhs.address && base_type::name == _rhs.base_type::name)
return load_address > _rhs.load_address;
return std::tie(address, base_type::binding, base_type::visibility, base_type::name) <
std::tie(_rhs.address, _rhs.base_type::binding, base_type::visibility,
base_type::name);
}
bool
symbol::operator()(const std::vector<scope_filter>& _filters) const
{
using sf = scope_filter;
// apply filters to the main symbol
return (sf::satisfies_filter(_filters, sf::FUNCTION_FILTER,
rocprofsys::utility::demangle(func)) &&
(sf::satisfies_filter(_filters, sf::SOURCE_FILTER, file) ||
sf::satisfies_filter(_filters, sf::SOURCE_FILTER,
fmt::format("{}:{}", file, line))));
}
symbol&
symbol::operator+=(const symbol& _rhs)
{
if(address.contiguous_with(_rhs.address) &&
std::tie(line, load_address, func, file) ==
std::tie(_rhs.line, _rhs.load_address, _rhs.func, _rhs.file))
{
address += _rhs.address;
utility::combine(inlines, _rhs.inlines);
utility::combine(dwarf_info, _rhs.dwarf_info);
if(_rhs.binding < binding) binding = _rhs.binding;
if(_rhs.visibility < visibility) visibility = _rhs.visibility;
if(load_address == 0 && _rhs.load_address > load_address)
load_address = _rhs.load_address;
}
else
{
throw exception<std::runtime_error>("incompatible symbol+=");
}
return *this;
}
symbol::operator bool() const
{
return address.is_valid() && (file.length() + func.length() + line) > 0;
}
size_t
symbol::read_dwarf_entries(const std::deque<dwarf_entry>& _info)
{
for(const auto& itr : _info)
{
if(address.contains(itr.address)) dwarf_info.emplace_back(itr);
}
// make sure the dwarf info is sorted by address (low to high)
std::sort(dwarf_info.begin(), dwarf_info.end(),
[](const dwarf_entry& _lhs, const dwarf_entry& _rhs) {
return _lhs.address < _rhs.address;
});
// helper for getting the end address
auto _get_next_address = [&](auto nitr, uintptr_t _low) {
while(++nitr != dwarf_info.end())
{
if(nitr->address.low > _low)
{
return nitr->address.low;
}
}
// return the end address of the symbol
return address.high;
};
// convert the single addresses into ranges
for(auto itr = dwarf_info.begin(); itr != dwarf_info.end(); ++itr)
{
// if address is already a range, do not update it
if(!itr->address.is_range())
itr->address = address_range{ itr->address.low,
_get_next_address(itr, itr->address.low) };
}
std::sort(dwarf_info.begin(), dwarf_info.end(),
[](const auto& _lhs, const auto& _rhs) {
return std::tie(_lhs.address, _lhs.file, _lhs.line, _lhs.col) <
std::tie(_rhs.address, _rhs.file, _rhs.line, _rhs.col);
});
dwarf_info.erase(std::unique(dwarf_info.begin(), dwarf_info.end(),
[](const auto& _lhs, const auto& _rhs) {
return std::tie(_lhs.address, _lhs.file,
_lhs.line) ==
std::tie(_rhs.address, _rhs.file, _rhs.line);
}),
dwarf_info.end());
return dwarf_info.size();
}
size_t
symbol::read_dwarf_breakpoints(const std::vector<uintptr_t>& _bkpts)
{
for(const auto& itr : _bkpts)
{
if(address.contains(itr)) breakpoints.emplace_back(itr);
}
// make sure the breakpoints are sorted low to high
std::sort(breakpoints.begin(), breakpoints.end());
return breakpoints.size();
}
bool
symbol::read_bfd_line_info(bfd_file& _bfd)
{
auto* _section = static_cast<asection*>(section);
bfd_vma _vma = bfd_section_vma(_section);
bfd_size_type _size = bfd_section_size(_section);
auto& _pc = address.low;
auto& _pc_end = address.high;
if(_pc < _vma || _pc >= _vma + _size) return false;
// add one to vma + size because address range is exclusive of last address
if(_pc_end > _vma + _size) _pc_end = (_vma + _size);
auto* _inp = static_cast<bfd*>(_bfd.data);
auto* _syms = reinterpret_cast<asymbol**>(_bfd.syms);
{
const char* _file = nullptr;
const char* _func = nullptr;
unsigned int _line = 0;
if(bfd_find_nearest_line(_inp, _section, _syms, _pc - _vma, &_file, &_func,
&_line) != 0)
{
if(_file) file = _file;
if(_func) func = _func;
if(_file && strnlen(_file, 1) > 0)
file = _file;
else if(!_file || strnlen(_file, 1) == 0)
file = bfd_get_filename(_inp);
if(!func.empty())
{
file = path::realpath(file);
line = _line;
inlines = read_inliner_info(_inp);
return true;
}
}
}
return false;
}
symbol
symbol::clone() const
{
auto _sym = symbol{ static_cast<base_type>(*this) };
_sym.line = line;
_sym.load_address = load_address;
_sym.address = address;
_sym.func = func;
_sym.file = file;
return _sym;
}
template <typename Tp>
Tp
symbol::get_inline_symbols(const std::vector<scope_filter>& _filters) const
{
using sf = scope_filter;
using value_type = typename Tp::value_type;
auto _data = Tp{};
for(const auto& itr : inlines)
{
if(sf::satisfies_filter(_filters, sf::FUNCTION_FILTER,
rocprofsys::utility::demangle(itr.func)) &&
(sf::satisfies_filter(_filters, sf::SOURCE_FILTER, itr.file) ||
sf::satisfies_filter(_filters, sf::SOURCE_FILTER,
fmt::format("{}:{}", itr.file, itr.line))))
{
if constexpr(concepts::is_unqualified_same<value_type, symbol>::value)
{
auto _sym = clone();
_sym.func = itr.func;
_sym.line = itr.line;
_sym.file = itr.file;
_data.emplace_back(_sym);
}
else if constexpr(concepts::is_unqualified_same<value_type,
inlined_symbol>::value)
{
_data.emplace_back(itr);
}
}
}
return _data;
}
template <typename Tp>
Tp
symbol::get_debug_line_info(const std::vector<scope_filter>& _filters) const
{
using sf = scope_filter;
using value_type = typename Tp::value_type;
auto _data = Tp{};
if(sf::satisfies_filter(_filters, sf::FUNCTION_FILTER,
rocprofsys::utility::demangle(func)))
{
for(const auto& itr : dwarf_info)
{
if(sf::satisfies_filter(_filters, sf::SOURCE_FILTER, itr.file) ||
sf::satisfies_filter(_filters, sf::SOURCE_FILTER,
fmt::format("{}:{}", itr.file, itr.line)))
{
if constexpr(concepts::is_unqualified_same<value_type, symbol>::value)
{
auto _sym = clone();
_sym.address = itr.address;
_sym.file = itr.file;
_sym.line = itr.line;
_data.emplace_back(_sym);
}
else if constexpr(concepts::is_unqualified_same<value_type,
dwarf_entry>::value)
{
_data.emplace_back(itr);
}
}
}
}
return _data;
}
template <typename ArchiveT>
void
inlined_symbol::serialize(ArchiveT& ar, const unsigned int)
{
using ::tim::cereal::make_nvp;
ar(make_nvp("func", func), make_nvp("file", file), make_nvp("line", line));
}
template <typename ArchiveT>
void
symbol::serialize(ArchiveT& ar, const unsigned int)
{
using ::tim::cereal::make_nvp;
ar(make_nvp("address", address), make_nvp("load_address", load_address),
make_nvp("line", line), make_nvp("func", func), make_nvp("file", file),
make_nvp("inlines", inlines), make_nvp("dwarf_info", dwarf_info));
if constexpr(concepts::is_output_archive<ArchiveT>::value)
ar(cereal::make_nvp("dfunc", rocprofsys::utility::demangle(func)));
}
template void
inlined_symbol::serialize<cereal::JSONInputArchive>(cereal::JSONInputArchive&,
const unsigned int);
template void
inlined_symbol::serialize<cereal::MinimalJSONOutputArchive>(
cereal::MinimalJSONOutputArchive&, const unsigned int);
template void
inlined_symbol::serialize<cereal::PrettyJSONOutputArchive>(
cereal::PrettyJSONOutputArchive&, const unsigned int);
template void
symbol::serialize<cereal::JSONInputArchive>(cereal::JSONInputArchive&,
const unsigned int);
template void
symbol::serialize<cereal::MinimalJSONOutputArchive>(cereal::MinimalJSONOutputArchive&,
const unsigned int);
template void
symbol::serialize<cereal::PrettyJSONOutputArchive>(cereal::PrettyJSONOutputArchive&,
const unsigned int);
template std::deque<symbol>
symbol::get_inline_symbols<std::deque<symbol>>(
const std::vector<scope_filter>& _filters) const;
template std::vector<inlined_symbol>
symbol::get_inline_symbols<std::vector<inlined_symbol>>(
const std::vector<scope_filter>& _filters) const;
template std::deque<symbol>
symbol::get_debug_line_info<std::deque<symbol>>(
const std::vector<scope_filter>& _filters) const;
template std::vector<dwarf_entry>
symbol::get_debug_line_info<std::vector<dwarf_entry>>(
const std::vector<scope_filter>& _filters) const;
} // namespace binary
} // namespace rocprofsys