Reorganize thread trace codeobj headers (#1001)

* include/rocprofiler-sdk/cxx/codeobj

- Relocated from include/rocprofiler-sdk/amd_detail/rocprofiler-sdk-codeobj

* Update include/rocprofiler-sdk/cxx

- cmake updates
- correct namespace rocprofiler::codeobj rocprofiler::sdk::codeobj

* Update codeobj tests and samples
This commit is contained in:
Jonathan R. Madsen
2024-08-01 00:10:09 -05:00
committed by GitHub
parent 94b5d9be3f
commit 20e07caad4
12 changed files with 37 additions and 48 deletions
@@ -0,0 +1,6 @@
set(ROCPROFILER_CXX_CODEOBJ_HEADERS code_printing.hpp disassembly.hpp segment.hpp)
install(
FILES ${ROCPROFILER_CXX_CODEOBJ_HEADERS}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/rocprofiler-sdk/cxx/codeobj
COMPONENT development)
@@ -0,0 +1,429 @@
// MIT License
//
// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include <elfutils/libdw.h>
#include <hsa/amd_hsa_elf.h>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>
#include "disassembly.hpp"
#include "segment.hpp"
namespace rocprofiler
{
namespace sdk
{
namespace codeobj
{
namespace disassembly
{
using marker_id_t = segment::marker_id_t;
struct Instruction
{
Instruction() = default;
Instruction(std::string&& _inst, size_t _size)
: inst(std::move(_inst))
, size(_size)
{}
std::string inst{};
std::string comment{};
uint64_t faddr{0};
uint64_t vaddr{0};
size_t size{0};
uint64_t ld_addr{0}; // Instruction load address, if from loaded codeobj
marker_id_t codeobj_id{0}; // Instruction code object load id, if from loaded codeobj
};
class CodeobjDecoderComponent
{
struct ProtectedFd
{
ProtectedFd(std::string_view uri)
{
#if defined(_GNU_SOURCE) && defined(MFD_ALLOW_SEALING) && defined(MFD_CLOEXEC)
m_fd = ::memfd_create(uri.data(), MFD_ALLOW_SEALING | MFD_CLOEXEC);
#endif
if(m_fd == -1) m_fd = ::open("/tmp", O_TMPFILE | O_RDWR, 0666);
if(m_fd == -1) throw std::runtime_error("Could not create a file for codeobj!");
}
~ProtectedFd()
{
if(m_fd != -1) ::close(m_fd);
}
int m_fd{-1};
};
public:
CodeobjDecoderComponent(const char* codeobj_data, uint64_t codeobj_size)
{
ProtectedFd prot("");
if(::write(prot.m_fd, codeobj_data, codeobj_size) != static_cast<int64_t>(codeobj_size))
throw std::runtime_error("Could not write to temporary file!");
::lseek(prot.m_fd, 0, SEEK_SET);
fsync(prot.m_fd);
m_line_number_map = {};
std::unique_ptr<Dwarf, void (*)(Dwarf*)> dbg(dwarf_begin(prot.m_fd, DWARF_C_READ),
[](Dwarf* _dbg) { dwarf_end(_dbg); });
if(dbg)
{
Dwarf_Off cu_offset{0}, next_offset;
size_t header_size;
std::map<uint64_t, std::string> line_addrs;
while(!dwarf_nextcu(
dbg.get(), cu_offset, &next_offset, &header_size, nullptr, nullptr, nullptr))
{
Dwarf_Die die;
if(!dwarf_offdie(dbg.get(), cu_offset + header_size, &die)) continue;
Dwarf_Lines* lines;
size_t line_count;
if(dwarf_getsrclines(&die, &lines, &line_count)) continue;
for(size_t i = 0; i < line_count; ++i)
{
Dwarf_Addr addr;
int line_number;
Dwarf_Line* line = dwarf_onesrcline(lines, i);
if(line && !dwarf_lineaddr(line, &addr) && !dwarf_lineno(line, &line_number) &&
line_number)
{
std::string src = dwarf_linesrc(line, nullptr, nullptr);
auto dwarf_line = src + ':' + std::to_string(line_number);
if(line_addrs.find(addr) != line_addrs.end())
{
line_addrs.at(addr) += ' ' + dwarf_line;
continue;
}
line_addrs.emplace(addr, std::move(dwarf_line));
}
}
cu_offset = next_offset;
}
auto it = line_addrs.begin();
if(it != line_addrs.end())
{
while(std::next(it) != line_addrs.end())
{
uint64_t delta = std::next(it)->first - it->first;
auto segment = segment::address_range_t{it->first, delta, 0};
m_line_number_map.emplace(segment, std::move(it->second));
it++;
}
auto segment = segment::address_range_t{it->first, codeobj_size - it->first, 0};
m_line_number_map.emplace(segment, std::move(it->second));
}
}
// Can throw
disassembly = std::make_unique<DisassemblyInstance>(codeobj_data, codeobj_size);
try
{
m_symbol_map = disassembly->GetKernelMap(); // Can throw
} catch(...)
{}
}
~CodeobjDecoderComponent() {}
std::optional<uint64_t> va2fo(uint64_t vaddr)
{
if(disassembly) return disassembly->va2fo(vaddr);
return {};
};
std::unique_ptr<Instruction> disassemble_instruction(uint64_t faddr, uint64_t vaddr)
{
if(!disassembly) throw std::exception();
auto pair = disassembly->ReadInstruction(faddr);
auto inst = std::make_unique<Instruction>(std::move(pair.first), pair.second);
inst->faddr = faddr;
inst->vaddr = vaddr;
auto it = m_line_number_map.find({vaddr, 0, 0});
if(it != m_line_number_map.end()) inst->comment = it->second;
return inst;
}
std::map<uint64_t, SymbolInfo> m_symbol_map{};
std::vector<std::shared_ptr<Instruction>> instructions{};
std::unique_ptr<DisassemblyInstance> disassembly{};
std::map<segment::address_range_t, std::string> m_line_number_map{};
};
class LoadedCodeobjDecoder
{
public:
LoadedCodeobjDecoder(const char* filepath, uint64_t _load_addr, uint64_t _memsize)
: load_addr(_load_addr)
, load_end(_load_addr + _memsize)
{
if(!filepath) throw std::runtime_error("Empty filepath.");
std::string_view fpath(filepath);
if(fpath.rfind(".out") + 4 == fpath.size())
{
std::ifstream file(filepath, std::ios::in | std::ios::binary);
if(!file.is_open()) throw std::runtime_error("Invalid file " + std::string(filepath));
std::vector<char> buffer;
file.seekg(0, file.end);
buffer.resize(file.tellg());
file.seekg(0, file.beg);
file.read(buffer.data(), buffer.size());
decoder = std::make_unique<CodeobjDecoderComponent>(buffer.data(), buffer.size());
}
else
{
std::unique_ptr<CodeObjectBinary> binary = std::make_unique<CodeObjectBinary>(filepath);
auto& buffer = binary->buffer;
decoder = std::make_unique<CodeobjDecoderComponent>(buffer.data(), buffer.size());
}
}
LoadedCodeobjDecoder(const void* data, uint64_t size, uint64_t _load_addr, size_t _memsize)
: load_addr(_load_addr)
, load_end(load_addr + _memsize)
{
decoder =
std::make_unique<CodeobjDecoderComponent>(reinterpret_cast<const char*>(data), size);
}
std::unique_ptr<Instruction> get(uint64_t ld_addr)
{
if(!decoder || ld_addr < load_addr) return nullptr;
uint64_t voffset = ld_addr - load_addr;
auto faddr = decoder->va2fo(voffset);
if(!faddr) return nullptr;
auto unique = decoder->disassemble_instruction(*faddr, voffset);
if(unique == nullptr || unique->size == 0) return nullptr;
unique->ld_addr = ld_addr;
return unique;
}
uint64_t begin() const { return load_addr; };
uint64_t end() const { return load_end; }
uint64_t size() const { return load_end - load_addr; }
bool inrange(uint64_t addr) const { return addr >= begin() && addr < end(); }
const char* getSymbolName(uint64_t addr) const
{
if(!decoder) return nullptr;
auto it = decoder->m_symbol_map.find(addr - load_addr);
if(it != decoder->m_symbol_map.end()) return it->second.name.data();
return nullptr;
}
std::map<uint64_t, SymbolInfo>& getSymbolMap() const
{
if(!decoder) throw std::exception();
return decoder->m_symbol_map;
}
const uint64_t load_addr;
private:
uint64_t load_end{0};
std::unique_ptr<CodeobjDecoderComponent> decoder{nullptr};
};
/**
* @brief Maps ID and offsets into instructions
*/
class CodeobjMap
{
public:
CodeobjMap() = default;
virtual ~CodeobjMap() = default;
virtual void addDecoder(const char* filepath,
marker_id_t id,
uint64_t load_addr,
uint64_t memsize)
{
decoders[id] = std::make_shared<LoadedCodeobjDecoder>(filepath, load_addr, memsize);
}
virtual void addDecoder(const void* data,
size_t memory_size,
marker_id_t id,
uint64_t load_addr,
uint64_t memsize)
{
decoders[id] =
std::make_shared<LoadedCodeobjDecoder>(data, memory_size, load_addr, memsize);
}
virtual bool removeDecoderbyId(marker_id_t id) { return decoders.erase(id) != 0; }
std::unique_ptr<Instruction> get(marker_id_t id, uint64_t offset)
{
try
{
auto& decoder = decoders.at(id);
auto inst = decoder->get(decoder->begin() + offset);
if(inst != nullptr) inst->codeobj_id = id;
return inst;
} catch(std::out_of_range&)
{}
return nullptr;
}
const char* getSymbolName(marker_id_t id, uint64_t offset)
{
try
{
auto& decoder = decoders.at(id);
uint64_t vaddr = decoder->begin() + offset;
if(decoder->inrange(vaddr)) return decoder->getSymbolName(vaddr);
} catch(std::out_of_range&)
{}
return nullptr;
}
protected:
std::unordered_map<marker_id_t, std::shared_ptr<LoadedCodeobjDecoder>> decoders{};
};
/**
* @brief Translates virtual addresses to elf file offsets
*/
class CodeobjAddressTranslate : public CodeobjMap
{
using Super = CodeobjMap;
public:
CodeobjAddressTranslate() = default;
~CodeobjAddressTranslate() override = default;
virtual void addDecoder(const char* filepath,
marker_id_t id,
uint64_t load_addr,
uint64_t memsize) override
{
this->Super::addDecoder(filepath, id, load_addr, memsize);
auto ptr = decoders.at(id);
table.insert({ptr->begin(), ptr->size(), id});
}
virtual void addDecoder(const void* data,
size_t memory_size,
marker_id_t id,
uint64_t load_addr,
uint64_t memsize) override
{
this->Super::addDecoder(data, memory_size, id, load_addr, memsize);
auto ptr = decoders.at(id);
table.insert({ptr->begin(), ptr->size(), id});
}
virtual bool removeDecoder(marker_id_t id, uint64_t load_addr)
{
return table.remove(load_addr) && this->Super::removeDecoderbyId(id);
}
std::unique_ptr<Instruction> get(uint64_t vaddr)
{
auto addr_range = table.find_codeobj_in_range(vaddr);
return this->Super::get(addr_range.id, vaddr - addr_range.addr);
}
std::unique_ptr<Instruction> get(marker_id_t id, uint64_t offset)
{
if(id == 0)
return get(offset);
else
return this->Super::get(id, offset);
}
const char* getSymbolName(uint64_t vaddr)
{
for(auto& [_, decoder] : decoders)
{
if(!decoder->inrange(vaddr)) continue;
return decoder->getSymbolName(vaddr);
}
return nullptr;
}
std::map<uint64_t, SymbolInfo> getSymbolMap() const
{
std::map<uint64_t, SymbolInfo> symbols;
for(auto& [_, dec] : decoders)
{
auto& smap = dec->getSymbolMap();
for(auto& [vaddr, sym] : smap)
symbols[vaddr + dec->load_addr] = sym;
}
return symbols;
}
std::map<uint64_t, SymbolInfo> getSymbolMap(marker_id_t id) const
{
if(decoders.find(id) == decoders.end()) return {};
try
{
return decoders.at(id)->getSymbolMap();
} catch(...)
{
return {};
}
}
private:
segment::CodeobjTableTranslator table{};
};
} // namespace disassembly
} // namespace codeobj
} // namespace sdk
} // namespace rocprofiler
@@ -0,0 +1,339 @@
// MIT License
//
// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include <amd_comgr/amd_comgr.h>
#include <fcntl.h>
#include <hsa/amd_hsa_elf.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <cstring>
#include <fstream>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>
#define THROW_COMGR(call) \
if(amd_comgr_status_s status = call) \
{ \
const char* reason = ""; \
amd_comgr_status_string(status, &reason); \
std::cerr << __FILE__ << ':' << __LINE__ << " code: " << status << " failed: " << reason \
<< std::endl; \
throw std::exception(); \
}
#define RETURN_COMGR(call) \
if(amd_comgr_status_s status = call) \
{ \
const char* reason = ""; \
amd_comgr_status_string(status, &reason); \
std::cerr << __FILE__ << ':' << __LINE__ << " code: " << status << " failed: " << reason \
<< std::endl; \
return AMD_COMGR_STATUS_ERROR; \
}
#define CHECK_VA2FO(x, msg) \
if(!(x)) \
{ \
std::cerr << __FILE__ << ' ' << __LINE__ << ' ' << msg << std::endl; \
return std::nullopt; \
}
namespace rocprofiler
{
namespace sdk
{
namespace codeobj
{
namespace disassembly
{
class CodeObjectBinary
{
public:
CodeObjectBinary(const std::string& _uri)
: m_uri(_uri)
{
const std::string protocol_delim{"://"};
size_t protocol_end = m_uri.find(protocol_delim);
std::string protocol = m_uri.substr(0, protocol_end);
protocol_end += protocol_delim.length();
std::transform(protocol.begin(), protocol.end(), protocol.begin(), [](unsigned char c) {
return std::tolower(c);
});
std::string path;
size_t path_end = m_uri.find_first_of("#?", protocol_end);
if(path_end != std::string::npos)
{
path = m_uri.substr(protocol_end, path_end++ - protocol_end);
}
else
{
path = m_uri.substr(protocol_end);
}
/* %-decode the string. */
std::string decoded_path;
decoded_path.reserve(path.length());
for(size_t i = 0; i < path.length(); ++i)
{
if(path[i] == '%' && std::isxdigit(path[i + 1]) && std::isxdigit(path[i + 2]))
{
decoded_path += std::stoi(path.substr(i + 1, 2), 0, 16);
i += 2;
}
else
{
decoded_path += path[i];
}
}
/* Tokenize the query/fragment. */
std::vector<std::string> tokens;
size_t pos, last = path_end;
while((pos = m_uri.find('&', last)) != std::string::npos)
{
tokens.emplace_back(m_uri.substr(last, pos - last));
last = pos + 1;
}
if(last != std::string::npos)
{
tokens.emplace_back(m_uri.substr(last));
}
/* Create a tag-value map from the tokenized query/fragment. */
std::unordered_map<std::string, std::string> params;
std::for_each(tokens.begin(), tokens.end(), [&](std::string& token) {
size_t delim = token.find('=');
if(delim != std::string::npos)
{
params.emplace(token.substr(0, delim), token.substr(delim + 1));
}
});
buffer = std::vector<char>{};
size_t offset = 0;
size_t size = 0;
if(auto offset_it = params.find("offset"); offset_it != params.end())
{
offset = std::stoul(offset_it->second, nullptr, 0);
}
if(auto size_it = params.find("size"); size_it != params.end())
{
if(!(size = std::stoul(size_it->second, nullptr, 0))) return;
}
if(protocol == "memory") throw std::runtime_error(protocol + " protocol not supported!");
std::ifstream file(decoded_path, std::ios::in | std::ios::binary);
if(!file || !file.is_open()) throw std::runtime_error("could not open " + decoded_path);
if(!size)
{
file.ignore(std::numeric_limits<std::streamsize>::max());
size_t bytes = file.gcount();
file.clear();
if(bytes < offset) throw std::runtime_error("invalid uri " + decoded_path);
size = bytes - offset;
}
file.seekg(offset, std::ios_base::beg);
buffer.resize(size);
file.read(&buffer[0], size);
}
std::string m_uri;
std::vector<char> buffer;
};
struct SymbolInfo
{
std::string name{};
uint64_t faddr = 0;
uint64_t vaddr = 0;
uint64_t mem_size = 0;
};
class DisassemblyInstance
{
public:
DisassemblyInstance(const char* codeobj_data, uint64_t codeobj_size)
{
buffer = std::vector<char>(codeobj_size, 0);
std::memcpy(buffer.data(), codeobj_data, codeobj_size);
THROW_COMGR(amd_comgr_create_data(AMD_COMGR_DATA_KIND_EXECUTABLE, &data));
THROW_COMGR(amd_comgr_set_data(data, buffer.size(), buffer.data()));
size_t isa_size = 128;
std::string input_isa{};
input_isa.resize(isa_size);
THROW_COMGR(amd_comgr_get_data_isa_name(data, &isa_size, input_isa.data()));
THROW_COMGR(amd_comgr_create_disassembly_info(
input_isa.data(),
&DisassemblyInstance::memory_callback,
&DisassemblyInstance::inst_callback,
[](uint64_t, void*) {},
&info));
}
~DisassemblyInstance()
{
amd_comgr_release_data(data);
amd_comgr_destroy_disassembly_info(info);
}
std::pair<std::string, size_t> ReadInstruction(uint64_t faddr)
{
uint64_t size_read;
uint64_t addr_in_buffer = reinterpret_cast<uint64_t>(buffer.data()) + faddr;
THROW_COMGR(
amd_comgr_disassemble_instruction(info, addr_in_buffer, (void*) this, &size_read));
return {std::move(this->last_instruction), size_read};
}
std::map<uint64_t, SymbolInfo>& GetKernelMap()
{
symbol_map = {};
THROW_COMGR(amd_comgr_iterate_symbols(data, &DisassemblyInstance::symbol_callback, this));
return symbol_map;
}
static amd_comgr_status_t symbol_callback(amd_comgr_symbol_t symbol, void* user_data)
{
amd_comgr_symbol_type_t type;
RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_TYPE, &type));
if(type != AMD_COMGR_SYMBOL_TYPE_FUNC) return AMD_COMGR_STATUS_SUCCESS;
uint64_t vaddr = 0;
uint64_t mem_size = 0;
uint64_t name_size = 0;
RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_VALUE, &vaddr));
RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_SIZE, &mem_size));
RETURN_COMGR(
amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_NAME_LENGTH, &name_size));
std::string name;
name.resize(name_size);
RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_NAME, name.data()));
DisassemblyInstance& instance = *static_cast<DisassemblyInstance*>(user_data);
std::optional<uint64_t> faddr = instance.va2fo(vaddr);
if(faddr) instance.symbol_map[vaddr] = {name, *faddr, vaddr, mem_size};
return AMD_COMGR_STATUS_SUCCESS;
}
static uint64_t memory_callback(uint64_t from, char* to, uint64_t size, void* user_data)
{
DisassemblyInstance& instance = *static_cast<DisassemblyInstance*>(user_data);
int64_t copysize = reinterpret_cast<int64_t>(instance.buffer.data()) +
instance.buffer.size() - static_cast<int64_t>(from);
copysize = std::min<int64_t>(size, copysize);
std::memcpy(to, (char*) from, copysize);
return copysize;
}
static void inst_callback(const char* instruction, void* user_data)
{
DisassemblyInstance& instance = *static_cast<DisassemblyInstance*>(user_data);
if(!instruction) return;
while(*instruction == '\t' || *instruction == ' ')
instruction++;
instance.last_instruction = instruction;
}
std::optional<uint64_t> va2fo(uint64_t va)
{
CHECK_VA2FO(buffer.size() > sizeof(Elf64_Ehdr), "buffer is not large enough");
uint8_t* e_ident = (uint8_t*) buffer.data();
CHECK_VA2FO(e_ident, "e_ident is nullptr");
CHECK_VA2FO(e_ident[EI_MAG0] == ELFMAG0 || e_ident[EI_MAG1] == ELFMAG1 ||
e_ident[EI_MAG2] == ELFMAG2 || e_ident[EI_MAG3] == ELFMAG3,
"unexpected ei_mag");
CHECK_VA2FO(e_ident[EI_CLASS] == ELFCLASS64, "unexpected ei_class");
CHECK_VA2FO(e_ident[EI_DATA] == ELFDATA2LSB, "unexpected ei_data");
CHECK_VA2FO(e_ident[EI_VERSION] == EV_CURRENT, "unexpected ei_version");
CHECK_VA2FO(e_ident[EI_OSABI] == 64, "unexpected ei_osabi"); // ELFOSABI_AMDGPU_HSA
CHECK_VA2FO(e_ident[EI_ABIVERSION] == 2 || // ELFABIVERSION_AMDGPU_HSA_V4
e_ident[EI_ABIVERSION] == 3,
"unexpected ei_abiversion"); // ELFABIVERSION_AMDGPU_HSA_V5
Elf64_Ehdr* ehdr = (Elf64_Ehdr*) buffer.data();
CHECK_VA2FO(ehdr, "ehdr is nullptr");
CHECK_VA2FO(ehdr->e_type == ET_DYN, "unexpected e_type");
CHECK_VA2FO(ehdr->e_machine == ELF::EM_AMDGPU, "unexpected e_machine");
CHECK_VA2FO(ehdr->e_phoff != 0, "unexpected e_phoff");
CHECK_VA2FO(buffer.size() > ehdr->e_phoff + sizeof(Elf64_Phdr),
"buffer is not large enough");
Elf64_Phdr* phdr = (Elf64_Phdr*) ((uint8_t*) buffer.data() + ehdr->e_phoff);
CHECK_VA2FO(phdr, "phdr is nullptr");
for(uint16_t i = 0; i < ehdr->e_phnum; ++i)
{
if(phdr[i].p_type != PT_LOAD) continue;
if(va < phdr[i].p_vaddr || va >= (phdr[i].p_vaddr + phdr[i].p_memsz)) continue;
return va + phdr[i].p_offset - phdr[i].p_vaddr;
}
return std::nullopt;
}
std::vector<char> buffer;
std::string last_instruction;
amd_comgr_disassembly_info_t info;
amd_comgr_data_t data;
std::map<uint64_t, SymbolInfo> symbol_map;
};
} // namespace disassembly
} // namespace codeobj
} // namespace sdk
} // namespace rocprofiler
@@ -0,0 +1,95 @@
// MIT License
//
// Copyright (c) 2024 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include <algorithm>
#include <iostream>
#include <random>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>
namespace rocprofiler
{
namespace sdk
{
namespace codeobj
{
namespace segment
{
using marker_id_t = size_t;
struct address_range_t
{
uint64_t addr{0};
uint64_t size{0};
marker_id_t id{0};
bool operator==(const address_range_t& other) const
{
return (addr >= other.addr && addr < other.addr + other.size) ||
(other.addr >= addr && other.addr < addr + size);
}
bool operator<(const address_range_t& other) const
{
if(*this == other) return false;
return addr < other.addr;
}
bool inrange(uint64_t _addr) const { return addr <= _addr && addr + size > _addr; };
};
/**
* @brief Finds a candidate codeobj for the given vaddr
*/
class CodeobjTableTranslator : public std::set<address_range_t>
{
using Super = std::set<address_range_t>;
public:
address_range_t find_codeobj_in_range(uint64_t addr)
{
if(!cached_segment.inrange(addr))
{
auto it = this->find(address_range_t{addr, 0, 0});
if(it == this->end()) throw std::exception();
cached_segment = *it;
}
return cached_segment;
}
void clear_cache() { cached_segment = {}; }
bool remove(const address_range_t& range)
{
clear_cache();
return this->erase(range) != 0;
}
bool remove(uint64_t addr) { return remove(address_range_t{addr, 0, 0}); }
private:
address_range_t cached_segment{};
};
} // namespace segment
} // namespace codeobj
} // namespace sdk
} // namespace rocprofiler