Files
rocm-systems/source/lib/rocprofiler-sdk-codeobj/code_printing.hpp
T
Giovanni Lenzi Baraldi 69b8a43dc6 Gbaraldi/threadtrace2 (#724)
* Added first ATT API

* Finalizing thread trace API

* Fixing more rebase conflicts

* Added codeobj disassembly sample

* Fixing merge issues with rebase [2]

* Adding ATT packets

* Implemented thread trace intercept

* Moved codeobj parser to same repo as rocprofiler

* Moved thread trace to new API

* Fixing merge conflicts

* Fixing more merge conflicts

* Adding thread trace packet reuse

* Merged aql_profile_v2 headers

* Linked ATT sample to aqlprofile

* Updated decoder to include non-loaded codeobjs

* Implemented ISA decoder into ATT sample

* Added marker_id to vaddr

* Updating aql_profile_v2 API to memcpy

* Updating thread trace API to include 64bit markers. Using the result of ISA matching.

* Added instruction type and cycles summary

* Updated sample with selection of kernel by kernel_object

* Added option to copy from memory kernels

* Moved tool_data in thread_trace to dynamic alloc

* Restoring hsa.cpp

* Fixed ATT sample crash. General improvements.

* Moved codeobj library to outside src/

* Updated license header

* Moved codeobj_capture to camelcase

* Solving some more merge conflicts

* Update samples/advanced_thread_trace/CMakeLists.txt

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update samples/advanced_thread_trace/CMakeLists.txt

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update samples/code_object_isa_decode/CMakeLists.txt

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update source/lib/rocprofiler-sdk/thread_trace/CMakeLists.txt

* Removing unused parameter check

* Adding const to isEmpty

* Removing unused warning

* Adding libdw-dev to requirements

* Running clang-format

* Commenting out new aql calls

* Clang format

* Unused variable fix

* Adding codeobj-decoder coverage

* Commenting out threadtrace

* Update samples/CMakeLists.txt

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* P

* WOverloaded

* Addressing clang-tidy

* Virtual destructor on ttracer class

* Corr id

* Fixing code source format

* Update CMakeLists.txt

* Build fixes

* Update source/lib/rocprofiler-sdk-codeobj/code_object_track.cpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Fix shadowing

* Update CMakeLists.txt

* Update samples/CMakeLists.txt

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Ammar ELWazir <ammar.elwazir@amd.com>
Co-authored-by: Ammar ELWazir <aelwazir@amd.com>
Co-authored-by: Benjamin Welton <bewelton@amd.com>
2024-04-08 12:43:02 -07:00

258 строки
8.1 KiB
C++

// 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 <map>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>
#include "disassembly.hpp"
#include "segment.hpp"
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;
uint64_t vaddr;
uint64_t ld_addr;
size_t size;
};
struct DSourceLine
{
uint64_t vaddr;
uint64_t size;
std::string str;
uint64_t begin() const { return vaddr; }
bool inrange(uint64_t addr) const { return addr >= vaddr && addr < vaddr + size; }
};
class CodeobjDecoderComponent
{
public:
CodeobjDecoderComponent(const char* codeobj_data, uint64_t codeobj_size);
~CodeobjDecoderComponent();
std::shared_ptr<Instruction> disassemble_instruction(uint64_t faddr, uint64_t vaddr);
int m_fd;
cached_ordered_vector<DSourceLine> m_line_number_map;
std::map<uint64_t, SymbolInfo> m_symbol_map{};
std::string m_uri;
std::vector<std::shared_ptr<Instruction>> instructions{};
std::unique_ptr<DisassemblyInstance> disassembly{};
};
class LoadedCodeobjDecoder
{
public:
LoadedCodeobjDecoder(const char* filepath, uint64_t load_addr, uint64_t memsize);
LoadedCodeobjDecoder(const void* data, uint64_t size, uint64_t load_addr, size_t memsize);
std::shared_ptr<Instruction> add_to_map(uint64_t ld_addr);
std::shared_ptr<Instruction> get(uint64_t addr);
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;
}
std::vector<std::pair<uint64_t, uint64_t>> elf_segments{};
const uint64_t load_addr;
private:
uint64_t load_end = 0;
std::unordered_map<uint64_t, std::shared_ptr<Instruction>> decoded_map;
std::unique_ptr<CodeobjDecoderComponent> decoder{nullptr};
};
/**
* @brief Maps ID and offsets into instructions
*/
class CodeobjMap
{
public:
CodeobjMap() = default;
virtual void addDecoder(const char* filepath,
codeobj_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,
codeobj_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(codeobj_marker_id_t id) { return decoders.erase(id) != 0; }
std::shared_ptr<Instruction> get(codeobj_marker_id_t id, uint64_t offset)
{
auto& decoder = decoders.at(id);
return decoder->get(decoder->begin() + offset);
}
const char* getSymbolName(codeobj_marker_id_t id, uint64_t offset)
{
auto& decoder = decoders.at(id);
uint64_t vaddr = decoder->begin() + offset;
if(decoder->inrange(vaddr)) return decoder->getSymbolName(vaddr);
return nullptr;
}
protected:
std::unordered_map<codeobj_marker_id_t, std::shared_ptr<LoadedCodeobjDecoder>> decoders{};
};
/**
* @brief Translates virtual addresses to elf file offsets
*/
class CodeobjAddressTranslate : protected CodeobjMap
{
using Super = CodeobjMap;
public:
CodeobjAddressTranslate() = default;
virtual void addDecoder(const char* filepath,
codeobj_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, 0});
}
virtual void addDecoder(const void* data,
size_t memory_size,
codeobj_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, 0});
}
virtual bool removeDecoder(codeobj_marker_id_t id, uint64_t load_addr)
{
return table.remove(load_addr) && this->Super::removeDecoderbyId(id);
}
std::shared_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.vbegin);
}
std::shared_ptr<Instruction> get(codeobj_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;
}
void getSymbolMap(const std::shared_ptr<LoadedCodeobjDecoder>& dec,
std::unordered_map<uint64_t, SymbolInfo>& symbols) const
{
try
{
auto& smap = dec->getSymbolMap();
for(auto& [vaddr, sym] : smap)
symbols[vaddr + dec->load_addr] = sym;
} catch(std::exception& e)
{
return;
};
}
std::unordered_map<uint64_t, SymbolInfo> getSymbolMap() const
{
std::unordered_map<uint64_t, SymbolInfo> symbols;
for(auto& [_, dec] : decoders)
this->getSymbolMap(dec, symbols);
return symbols;
}
std::unordered_map<uint64_t, SymbolInfo> getSymbolMap(codeobj_marker_id_t id) const
{
std::unordered_map<uint64_t, SymbolInfo> symbols;
auto it = decoders.find(id);
if(it == decoders.end()) return symbols;
this->getSymbolMap(it->second, symbols);
return symbols;
}
private:
CodeobjTableTranslator table;
};