SWDEV-423366: Added workaround for comgr file offset. Fixed legacy mode for ATT.

Change-Id: Ib566f3573829b59ae6bd70a5479b9d7fb7fdc6ee
This commit is contained in:
Giovanni LB
2023-09-30 14:15:09 -03:00
committed by Giovanni Baraldi
parent f36568b35c
commit d8951e2018
7 changed files with 146 additions and 72 deletions
+2 -4
View File
@@ -176,10 +176,8 @@ class att_plugin_t {
for (auto& instance : decoder->instructions) {
uint64_t addr = instance.address + symbol.base_address;
if (kernel_begin_addr == addr)
isafile << "; Begin <Kernel> " << kernel_name_mangled << '\n';
else if (decoder->m_symbol_map.find(instance.address) != decoder->m_symbol_map.end())
isafile << "; Begin " << decoder->m_symbol_map[instance.address].first << '\n';
if (decoder->m_symbol_map.find(instance.address) != decoder->m_symbol_map.end())
isafile << "; Begin " << decoder->m_symbol_map[instance.address].name << '\n';
if (instance.cpp_reference) isafile << "; " << instance.cpp_reference << '\n';
isafile << instance.instruction << " // " << std::hex << addr << '\n';
}
+34 -29
View File
@@ -99,12 +99,11 @@ code_object_decoder_t::code_object_decoder_t(const char* codeobj_data, uint64_t
for (size_t i = 0; i < line_count; ++i) {
Dwarf_Addr addr;
int line_number;
Dwarf_Line* line = dwarf_onesrcline(lines, i);
if (!line) continue;
if (Dwarf_Line* line = dwarf_onesrcline(lines, i))
if (!dwarf_lineaddr(line, &addr) && !dwarf_lineno(line, &line_number) && line_number) {
m_line_number_map.emplace(
addr, std::make_pair(dwarf_linesrc(line, nullptr, nullptr), line_number));
}
if (!dwarf_lineaddr(line, &addr) && !dwarf_lineno(line, &line_number) && line_number)
m_line_number_map[addr] = {dwarf_linesrc(line, nullptr, nullptr), line_number};
}
cu_offset = next_offset;
}
@@ -118,38 +117,43 @@ code_object_decoder_t::~code_object_decoder_t() {
if (m_fd) ::close(m_fd);
}
std::optional<code_object_decoder_t::symbol_info_t> code_object_decoder_t::find_symbol(
uint64_t address) {
std::optional<SymbolInfo> code_object_decoder_t::find_symbol(uint64_t vaddr) {
/* Load the symbol table. */
if (auto it = m_symbol_map.upper_bound(address); it != m_symbol_map.begin()) {
if (auto&& [symbol_value, symbol] = *std::prev(it); address < (symbol_value + symbol.second)) {
std::string symbol_name = symbol.first;
auto it = m_symbol_map.upper_bound(vaddr);
if (it == m_symbol_map.begin())
return std::nullopt;
if (int status; auto* demangled_name =
abi::__cxa_demangle(symbol_name.c_str(), nullptr, nullptr, &status)) {
symbol_name = demangled_name;
free(demangled_name);
}
return symbol_info_t{std::move(symbol_name), symbol_value, symbol.second};
}
auto&& [symbol_vaddr, symbol] = *std::prev(it);
if (vaddr >= symbol_vaddr + symbol.mem_size)
return std::nullopt;
std::string symbol_name = symbol.name;
int status = 0;
auto* demangled_name = abi::__cxa_demangle(symbol_name.c_str(), nullptr, nullptr, &status);
if (status == 0 && demangled_name)
{
symbol_name = demangled_name;
free(demangled_name);
}
return {};
return SymbolInfo{symbol_name, symbol.faddr, symbol.mem_size};
}
void code_object_decoder_t::disassemble_kernel(uint64_t addr) {
auto symbol = find_symbol(addr);
void code_object_decoder_t::disassemble_kernel(uint64_t faddr, uint64_t vaddr) {
auto symbol = find_symbol(vaddr);
if (!symbol) {
std::cerr << "No symbol found at address 0x" << std::hex << addr << std::endl;
if (!symbol)
{
std::cerr << "No symbol found at address 0x" << std::hex << faddr << std::endl;
return;
}
std::cout << "Dumping ISA for " << symbol->m_name << std::endl;
std::cout << "Dumping ISA for " << symbol->name << std::endl;
uint64_t end_addr = addr + symbol->m_size;
while (addr < end_addr) {
uint64_t end_addr = faddr + symbol->mem_size;
while (faddr < end_addr) {
char* cpp_line = nullptr;
auto it = m_line_number_map.find(addr);
auto it = m_line_number_map.find(vaddr);
if (it != m_line_number_map.end()) {
const std::string& file_name = it->second.first;
size_t line_number = it->second.second;
@@ -159,8 +163,9 @@ void code_object_decoder_t::disassemble_kernel(uint64_t addr) {
std::memcpy(cpp_line, cpp.data(), cpp.size() * sizeof(char));
}
size_t size = disassembly->ReadInstruction(addr, cpp_line);
addr += size;
size_t size = disassembly->ReadInstruction(faddr, vaddr, cpp_line);
faddr += size;
vaddr += size;
}
}
@@ -168,5 +173,5 @@ void code_object_decoder_t::disassemble_kernels() {
disassembly = std::make_unique<DisassemblyInstance>(*this);
m_symbol_map = disassembly->GetKernelMap();
for (auto& [k, v] : m_symbol_map) disassemble_kernel(k);
for (auto& [vaddr, v] : m_symbol_map) disassemble_kernel(v.faddr, vaddr);
}
+3 -9
View File
@@ -29,25 +29,19 @@
#include "disassembly.hpp"
class code_object_decoder_t {
struct symbol_info_t {
const std::string m_name;
uint64_t m_value;
uint64_t m_size;
};
public:
// void load_symbol_map();
std::optional<symbol_info_t> find_symbol(uint64_t address);
std::optional<SymbolInfo> find_symbol(uint64_t address);
code_object_decoder_t(const char* codeobj_data, uint64_t codeobj_size);
~code_object_decoder_t();
void disassemble_kernel(uint64_t addr);
void disassemble_kernel(uint64_t faddr, uint64_t vaddr);
void disassemble_kernels();
int m_fd;
std::map<uint64_t, std::pair<std::string, size_t>> m_line_number_map;
std::map<uint64_t, std::pair<std::string, uint64_t>> m_symbol_map;
std::map<uint64_t, SymbolInfo> m_symbol_map;
std::string m_uri;
std::vector<char> buffer;
+78 -19
View File
@@ -24,13 +24,20 @@
#include "code_printing.hpp"
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <elf.h>
#include <cxxabi.h>
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <type_traits>
@@ -42,15 +49,8 @@
#include <cstdlib>
#include <cstring>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <hsa/amd_hsa_elf.h>
#include "../utils.h"
#include <cxxabi.h>
#include <elfutils/libdw.h>
#define CHECK_COMGR(call) \
@@ -154,10 +154,10 @@ CodeObjectBinary::CodeObjectBinary(const std::string& uri) : m_uri(uri) {
}
DisassemblyInstance::DisassemblyInstance(code_object_decoder_t& decoder)
: buffer(reinterpret_cast<int64_t>(decoder.buffer.data())),
: buffer(reinterpret_cast<void*>(decoder.buffer.data())),
size(decoder.buffer.size()),
instructions(decoder.instructions) {
CHECK_COMGR(amd_comgr_create_data(AMD_COMGR_DATA_KIND_RELOCATABLE, &data));
CHECK_COMGR(amd_comgr_create_data(AMD_COMGR_DATA_KIND_EXECUTABLE, &data));
CHECK_COMGR(amd_comgr_set_data(data, size, decoder.buffer.data()));
char isa_name[128];
@@ -178,8 +178,8 @@ amd_comgr_status_t DisassemblyInstance::symbol_callback(amd_comgr_symbol_t symbo
if (type != AMD_COMGR_SYMBOL_TYPE_FUNC && type != AMD_COMGR_SYMBOL_TYPE_AMDGPU_HSA_KERNEL)
return AMD_COMGR_STATUS_SUCCESS;
uint64_t addr;
CHECK_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_VALUE, &addr));
uint64_t vaddr;
CHECK_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_VALUE, &vaddr));
uint64_t mem_size;
CHECK_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_SIZE, &mem_size));
@@ -192,12 +192,16 @@ amd_comgr_status_t DisassemblyInstance::symbol_callback(amd_comgr_symbol_t symbo
CHECK_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_NAME, name.data()));
static_cast<DisassemblyInstance*>(user_data)->symbol_map[addr] = {name, mem_size};
DisassemblyInstance& instance = *static_cast<DisassemblyInstance*>(user_data);
std::optional<uint64_t> faddr = va2fo(instance.buffer, vaddr);
if (faddr)
instance.symbol_map[vaddr] = {name, *faddr, mem_size};
return AMD_COMGR_STATUS_SUCCESS;
}
std::map<uint64_t, std::pair<std::string, uint64_t>>& DisassemblyInstance::GetKernelMap() {
symbol_map = std::map<uint64_t, std::pair<std::string, uint64_t>>{};
std::map<uint64_t, SymbolInfo>& DisassemblyInstance::GetKernelMap() {
symbol_map = {};
CHECK_COMGR(amd_comgr_iterate_symbols(data, &DisassemblyInstance::symbol_callback, this));
return symbol_map;
}
@@ -207,11 +211,13 @@ DisassemblyInstance::~DisassemblyInstance() {
CHECK_COMGR(amd_comgr_destroy_disassembly_info(info));
}
uint64_t DisassemblyInstance::ReadInstruction(uint64_t addr, const char* cpp_line) {
uint64_t DisassemblyInstance::ReadInstruction(uint64_t faddr, uint64_t vaddr, const char* cpp_line)
{
uint64_t size_read;
CHECK_COMGR(amd_comgr_disassemble_instruction(info, buffer + addr, (void*)this, &size_read));
uint64_t addr_in_buffer = reinterpret_cast<uint64_t>(buffer) + faddr;
CHECK_COMGR(amd_comgr_disassemble_instruction(info, addr_in_buffer, (void*)this, &size_read));
assert(instructions.size() != 0);
instructions.back().address = addr;
instructions.back().address = vaddr;
instructions.back().cpp_reference = cpp_line;
return size_read;
}
@@ -219,7 +225,8 @@ uint64_t DisassemblyInstance::ReadInstruction(uint64_t addr, const char* cpp_lin
uint64_t DisassemblyInstance::memory_callback(uint64_t from, char* to, uint64_t size,
void* user_data) {
DisassemblyInstance& instance = *static_cast<DisassemblyInstance*>(user_data);
size_t copysize = std::min((int64_t)size, instance.buffer + instance.size - (int64_t)from);
int64_t copysize = reinterpret_cast<int64_t>(instance.buffer) + instance.size - (int64_t)from;
copysize = std::min<int64_t>(size, copysize);
std::memcpy(to, (char*)from, copysize);
return copysize;
}
@@ -228,3 +235,55 @@ void DisassemblyInstance::inst_callback(const char* instruction, void* user_data
DisassemblyInstance& instance = *static_cast<DisassemblyInstance*>(user_data);
instance.instructions.push_back({strdup(instruction), nullptr, 0});
}
#define CHECK_VA2FO(x, msg) if (!(x)) { \
std::cerr << __FILE__ << ' ' << __LINE__ << ' ' << msg << std::endl; \
return std::nullopt; \
}
// mem - input argument, start of the elf
// va - input argument, virtual address
// return file offset, if found
std::optional<uint64_t> DisassemblyInstance::va2fo(void *mem, uint64_t va)
{
CHECK_VA2FO(mem, "mem is nullptr");
uint8_t *e_ident = (uint8_t*)mem;
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 /*ELFOSABI_AMDGPU_HSA*/, "unexpected ei_osabi");
CHECK_VA2FO(
e_ident[EI_ABIVERSION] == 2 /*ELFABIVERSION_AMDGPU_HSA_V4*/ ||
e_ident[EI_ABIVERSION] == 3 /*ELFABIVERSION_AMDGPU_HSA_V5*/ , "unexpected ei_abiversion");
Elf64_Ehdr *ehdr = (Elf64_Ehdr*)mem;
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");
Elf64_Phdr *phdr = (Elf64_Phdr*)((uint8_t*)mem + 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;
}
+12 -4
View File
@@ -38,22 +38,30 @@ class CodeObjectBinary {
std::vector<char> buffer;
};
struct SymbolInfo
{
std::string name;
uint64_t faddr;
uint64_t mem_size;
};
class DisassemblyInstance {
public:
DisassemblyInstance(class code_object_decoder_t& decoder);
~DisassemblyInstance();
uint64_t ReadInstruction(uint64_t addr, const char* cpp_line);
std::map<uint64_t, std::pair<std::string, uint64_t>>& GetKernelMap();
uint64_t ReadInstruction(uint64_t faddr, uint64_t vaddr, const char* cpp_line);
std::map<uint64_t, SymbolInfo>& GetKernelMap();
static uint64_t memory_callback(uint64_t from, char* to, uint64_t size, void* user_data);
static void inst_callback(const char* instruction, void* user_data);
static amd_comgr_status_t symbol_callback(amd_comgr_symbol_t symbol, void* user_data);
static std::optional<uint64_t> va2fo(void *mem, uint64_t va);
int64_t buffer;
void* buffer;
int64_t size;
std::vector<instruction_instance_t>& instructions;
amd_comgr_disassembly_info_t info;
amd_comgr_data_t data;
std::map<uint64_t, std::pair<std::string, uint64_t>> symbol_map;
std::map<uint64_t, SymbolInfo> symbol_map;
};
+13 -7
View File
@@ -196,7 +196,7 @@ def try_match_swapped(insts, code, i, line):
def stitch(insts, raw_code, jumps, gfxv, bIsAuto):
bGFX9 = gfxv == 'vega'
result, i, line, loopCount, N = [], 0, 0, defaultdict(int), len(insts)
result, i, line, loopCount = [], 0, 0, defaultdict(int)
SMEM_INST = [] # scalar memory
VLMEM_INST = [] # vector memory load
@@ -246,15 +246,22 @@ def stitch(insts, raw_code, jumps, gfxv, bIsAuto):
try:
watchlist = PCTranslator(code, insts)
line = watchlist.addrmap[insts[0][2]]
result.append((insts[0][0], PCINFO, 0, 0, 0))
i = 1
except:
return None
else:
watchlist = RegisterWatchList(labels=labels)
if len(insts) and insts[0][1] == PCINFO:
insts = insts[1:]
N = len(insts)
pcsequence = []
while i < N:
if insts[i][1] == PCINFO:
i += 1
N -= 1
continue
loops += 1
if line >= len(code) or loops > MAX_STITCHED_TOKENS \
or num_failed_stitches > MAX_FAILED_STITCHES:
@@ -288,7 +295,7 @@ def stitch(insts, raw_code, jumps, gfxv, bIsAuto):
if bIsAuto:
matched = next >= 0
i += 1
result.append((insts[i][0], PCINFO, 0, 0, 0))
N -= 1
pcsequence.append(insts[i][2])
elif as_line[1] == SWAPPC:
next = watchlist.swappc(as_line[0], line, i)
@@ -296,7 +303,7 @@ def stitch(insts, raw_code, jumps, gfxv, bIsAuto):
if bIsAuto:
matched = next >= 0
i += 1
result.append((insts[i][0], PCINFO, 0, 0, 0))
N -= 1
pcsequence.append(insts[i][2])
elif inst[1] == as_line[1]:
if line in jumps:
@@ -446,5 +453,4 @@ def stitch(insts, raw_code, jumps, gfxv, bIsAuto):
break
line += 1
result = [r for r in result if r[1] != PCINFO]
return result, loopCount, mem_unroll, flight_count, maxline, len(result) if i == N else N
return result, loopCount, mem_unroll, flight_count, maxline, len(result)
+4
View File
@@ -289,6 +289,10 @@ att_parsed_input_t GetATTParams() {
ATT_PARAM_NAMES["BUFFER_SIZE"] = ROCPROFILER_ATT_BUFFER_SIZE;
ATT_PARAM_NAMES["ISA_CAPTURE_MODE"] = ROCPROFILER_ATT_CAPTURE_MODE;
ATT_PARAM_NAMES["LEGACY_ATT_MASK"] = ROCPROFILER_ATT_MASK;
ATT_PARAM_NAMES["LEGACY_TOKEN_MASK"] = ROCPROFILER_ATT_TOKEN_MASK;
ATT_PARAM_NAMES["LEGACY_TOKEN_MASK2"] = ROCPROFILER_ATT_TOKEN_MASK2;
// Default values used for token generation.
std::unordered_map<std::string, uint32_t> default_params = {
{"SE_MASK", 0x111111}, // One every 4 SEs, by default