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
220 lines
6.4 KiB
C++
220 lines
6.4 KiB
C++
// Copyright (c) Advanced Micro Devices, Inc.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#include "dwarf_entry.hpp"
|
|
#include "common/path.hpp"
|
|
#include "core/binary/fwd.hpp"
|
|
#include "core/timemory.hpp"
|
|
#include "core/utility.hpp"
|
|
|
|
#include <dwarf.h>
|
|
#include <elfutils/libdw.h>
|
|
|
|
namespace rocprofsys
|
|
{
|
|
namespace binary
|
|
{
|
|
namespace
|
|
{
|
|
using utility::combine;
|
|
|
|
auto
|
|
get_dwarf_address_ranges(Dwarf_Die* _die)
|
|
{
|
|
auto _ranges = std::vector<address_range>{};
|
|
|
|
if(dwarf_tag(_die) != DW_TAG_compile_unit && dwarf_tag(_die) != DW_TAG_subprogram)
|
|
return _ranges;
|
|
|
|
Dwarf_Addr _low_pc;
|
|
Dwarf_Addr _high_pc;
|
|
dwarf_lowpc(_die, &_low_pc);
|
|
dwarf_highpc(_die, &_high_pc);
|
|
|
|
if(_low_pc > _high_pc)
|
|
{
|
|
Dwarf_Addr _entry_pc;
|
|
dwarf_entrypc(_die, &_entry_pc);
|
|
if(_entry_pc < _low_pc) _low_pc = _entry_pc;
|
|
}
|
|
|
|
if(_low_pc < _high_pc) _ranges.emplace_back(_low_pc, _high_pc);
|
|
|
|
Dwarf_Addr _base_addr;
|
|
ptrdiff_t _offset = 0;
|
|
do
|
|
{
|
|
uintptr_t _low = 0;
|
|
uintptr_t _high = 0;
|
|
_offset = dwarf_ranges(_die, _offset, &_base_addr, &_low, &_high);
|
|
if(_low < _high) _ranges.emplace_back(_low, _high);
|
|
} while(_offset > 0);
|
|
|
|
return _ranges;
|
|
}
|
|
|
|
auto
|
|
get_dwarf_breakpoints(Dwarf_Die* _die)
|
|
{
|
|
auto _bkpts = std::vector<uintptr_t>{};
|
|
|
|
if(dwarf_tag(_die) != DW_TAG_subprogram) return _bkpts;
|
|
|
|
Dwarf_Addr* _pts = nullptr;
|
|
auto _npts = dwarf_entry_breakpoints(_die, &_pts);
|
|
|
|
if(_npts > 0 && _pts) _bkpts.assign(_pts, _pts + _npts);
|
|
|
|
return _bkpts;
|
|
}
|
|
|
|
auto
|
|
get_dwarf_entry(Dwarf_Die* _die)
|
|
{
|
|
auto _line_info = std::deque<dwarf_entry>{};
|
|
|
|
if(dwarf_tag(_die) != DW_TAG_compile_unit) return _line_info;
|
|
|
|
Dwarf_Lines* _lines = nullptr;
|
|
size_t _num_lines = 0;
|
|
if(dwarf_getsrclines(_die, &_lines, &_num_lines) == 0)
|
|
{
|
|
_line_info.resize(_num_lines);
|
|
for(size_t j = 0; j < _num_lines; ++j)
|
|
{
|
|
auto& itr = _line_info.at(j);
|
|
auto* _line = dwarf_onesrcline(_lines, j);
|
|
if(_line)
|
|
{
|
|
int _lineno = 0;
|
|
uintptr_t _address = 0;
|
|
dwarf_lineno(_line, &_lineno);
|
|
dwarf_linecol(_line, &itr.col);
|
|
dwarf_linebeginstatement(_line, &itr.begin_statement);
|
|
dwarf_lineendsequence(_line, &itr.end_sequence);
|
|
dwarf_lineblock(_line, &itr.line_block);
|
|
dwarf_lineepiloguebegin(_line, &itr.epilogue_begin);
|
|
dwarf_lineprologueend(_line, &itr.prologue_end);
|
|
dwarf_lineisa(_line, &itr.isa);
|
|
dwarf_linediscriminator(_line, &itr.discriminator);
|
|
dwarf_lineaddr(_line, &_address);
|
|
itr.address = address_range{ _address };
|
|
if(_lineno > 0) itr.line = _lineno;
|
|
const auto* _file = dwarf_linesrc(_line, nullptr, nullptr);
|
|
if(!_file) _file = dwarf_diename(_die);
|
|
itr.file = path::realpath(_file);
|
|
}
|
|
}
|
|
}
|
|
|
|
return _line_info;
|
|
}
|
|
} // namespace
|
|
|
|
bool
|
|
dwarf_entry::operator<(const dwarf_entry& _rhs) const
|
|
{
|
|
return std::tie(address, file, line, col, discriminator) <
|
|
std::tie(_rhs.address, _rhs.file, _rhs.line, _rhs.col, _rhs.discriminator);
|
|
}
|
|
|
|
bool
|
|
dwarf_entry::operator==(const dwarf_entry& _rhs) const
|
|
{
|
|
return std::tie(address, line, col, discriminator, vliw_op_index, isa, file) ==
|
|
std::tie(_rhs.address, _rhs.line, _rhs.col, _rhs.discriminator,
|
|
_rhs.vliw_op_index, _rhs.isa, _rhs.file);
|
|
}
|
|
|
|
bool
|
|
dwarf_entry::operator!=(const dwarf_entry& _rhs) const
|
|
{
|
|
return !(*this == _rhs);
|
|
}
|
|
|
|
bool
|
|
dwarf_entry::is_valid() const
|
|
{
|
|
return (*this != dwarf_entry{} && !file.empty());
|
|
}
|
|
|
|
dwarf_entry::dwarf_tuple_t
|
|
dwarf_entry::process_dwarf(int _fd)
|
|
{
|
|
auto* _dwarf_v = dwarf_begin(_fd, DWARF_C_READ);
|
|
auto _data_v = dwarf_tuple_t{};
|
|
|
|
if(_dwarf_v)
|
|
{
|
|
auto& _entries = std::get<0>(_data_v);
|
|
auto& _ranges = std::get<1>(_data_v);
|
|
auto& _bkpts = std::get<2>(_data_v);
|
|
|
|
size_t cu_header_size = 0;
|
|
Dwarf_Off cu_off = 0;
|
|
Dwarf_Off next_cu_off = 0;
|
|
for(; dwarf_nextcu(_dwarf_v, cu_off, &next_cu_off, &cu_header_size, nullptr,
|
|
nullptr, nullptr) == 0;
|
|
cu_off = next_cu_off)
|
|
{
|
|
auto cu_die_off = cu_off + cu_header_size;
|
|
auto cu_die = Dwarf_Die{};
|
|
if(dwarf_offdie(_dwarf_v, cu_die_off, &cu_die) != nullptr)
|
|
{
|
|
Dwarf_Die* _die = &cu_die;
|
|
if(dwarf_tag(_die) == DW_TAG_compile_unit)
|
|
{
|
|
combine(_entries, get_dwarf_entry(_die));
|
|
combine(_ranges, get_dwarf_address_ranges(_die));
|
|
}
|
|
else if(dwarf_tag(_die) == DW_TAG_subprogram)
|
|
{
|
|
combine(_bkpts, get_dwarf_breakpoints(_die));
|
|
combine(_ranges, get_dwarf_address_ranges(_die));
|
|
}
|
|
}
|
|
}
|
|
|
|
dwarf_end(_dwarf_v);
|
|
utility::filter_sort_unique(_entries);
|
|
utility::filter_sort_unique(_ranges);
|
|
utility::filter_sort_unique(_bkpts);
|
|
}
|
|
|
|
return _data_v;
|
|
}
|
|
|
|
template <typename ArchiveT>
|
|
void
|
|
dwarf_entry::serialize(ArchiveT& ar, const unsigned int)
|
|
{
|
|
#define ROCPROFSYS_SERIALIZE_MEMBER(MEMBER) ar(::tim::cereal::make_nvp(#MEMBER, MEMBER));
|
|
|
|
ROCPROFSYS_SERIALIZE_MEMBER(file)
|
|
ROCPROFSYS_SERIALIZE_MEMBER(line)
|
|
ROCPROFSYS_SERIALIZE_MEMBER(col)
|
|
ROCPROFSYS_SERIALIZE_MEMBER(address)
|
|
ROCPROFSYS_SERIALIZE_MEMBER(discriminator)
|
|
// ROCPROFSYS_SERIALIZE_MEMBER(begin_statement)
|
|
// ROCPROFSYS_SERIALIZE_MEMBER(end_sequence)
|
|
// ROCPROFSYS_SERIALIZE_MEMBER(line_block)
|
|
// ROCPROFSYS_SERIALIZE_MEMBER(prologue_end)
|
|
// ROCPROFSYS_SERIALIZE_MEMBER(epilogue_begin)
|
|
// ROCPROFSYS_SERIALIZE_MEMBER(vliw_op_index)
|
|
// ROCPROFSYS_SERIALIZE_MEMBER(isa)
|
|
}
|
|
|
|
template void
|
|
dwarf_entry::serialize<cereal::JSONInputArchive>(cereal::JSONInputArchive&,
|
|
const unsigned int);
|
|
|
|
template void
|
|
dwarf_entry::serialize<cereal::MinimalJSONOutputArchive>(
|
|
cereal::MinimalJSONOutputArchive&, const unsigned int);
|
|
|
|
template void
|
|
dwarf_entry::serialize<cereal::PrettyJSONOutputArchive>(cereal::PrettyJSONOutputArchive&,
|
|
const unsigned int);
|
|
} // namespace binary
|
|
} // namespace rocprofsys
|