diff --git a/plugin/att/att.cpp b/plugin/att/att.cpp index 7689d19c0c..caaac5ccd4 100644 --- a/plugin/att/att.cpp +++ b/plugin/att/att.cpp @@ -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_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'; } diff --git a/plugin/att/code_printing.cpp b/plugin/att/code_printing.cpp index ed5db7ab31..87727fd314 100644 --- a/plugin/att/code_printing.cpp +++ b/plugin/att/code_printing.cpp @@ -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::find_symbol( - uint64_t address) { +std::optional 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(*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); } diff --git a/plugin/att/code_printing.hpp b/plugin/att/code_printing.hpp index 050b219dff..ce21f6e30a 100644 --- a/plugin/att/code_printing.hpp +++ b/plugin/att/code_printing.hpp @@ -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 find_symbol(uint64_t address); + std::optional 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> m_line_number_map; - std::map> m_symbol_map; + std::map m_symbol_map; std::string m_uri; std::vector buffer; diff --git a/plugin/att/disassembly.cpp b/plugin/att/disassembly.cpp index d94e22931a..985a64bbd2 100644 --- a/plugin/att/disassembly.cpp +++ b/plugin/att/disassembly.cpp @@ -24,13 +24,20 @@ #include "code_printing.hpp" +#include +#include +#include +#include +#include +#include +#include + #include #include #include #include #include #include -#include #include #include #include @@ -42,15 +49,8 @@ #include #include -#include -#include -#include -#include -#include - #include #include "../utils.h" -#include #include #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(decoder.buffer.data())), + : buffer(reinterpret_cast(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(user_data)->symbol_map[addr] = {name, mem_size}; + DisassemblyInstance& instance = *static_cast(user_data); + std::optional faddr = va2fo(instance.buffer, vaddr); + + if (faddr) + instance.symbol_map[vaddr] = {name, *faddr, mem_size}; return AMD_COMGR_STATUS_SUCCESS; } -std::map>& DisassemblyInstance::GetKernelMap() { - symbol_map = std::map>{}; +std::map& 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(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(user_data); - size_t copysize = std::min((int64_t)size, instance.buffer + instance.size - (int64_t)from); + int64_t copysize = reinterpret_cast(instance.buffer) + instance.size - (int64_t)from; + copysize = std::min(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(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 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; +} diff --git a/plugin/att/disassembly.hpp b/plugin/att/disassembly.hpp index 2344c35bbe..da1eb8201f 100644 --- a/plugin/att/disassembly.hpp +++ b/plugin/att/disassembly.hpp @@ -38,22 +38,30 @@ class CodeObjectBinary { std::vector 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>& GetKernelMap(); + uint64_t ReadInstruction(uint64_t faddr, uint64_t vaddr, const char* cpp_line); + std::map& 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 va2fo(void *mem, uint64_t va); - int64_t buffer; + void* buffer; int64_t size; std::vector& instructions; amd_comgr_disassembly_info_t info; amd_comgr_data_t data; - std::map> symbol_map; + std::map symbol_map; }; diff --git a/plugin/att/stitch.py b/plugin/att/stitch.py index f67485b282..6e7f8f911f 100644 --- a/plugin/att/stitch.py +++ b/plugin/att/stitch.py @@ -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) diff --git a/src/tools/tool.cpp b/src/tools/tool.cpp index 2791490999..95836bf420 100644 --- a/src/tools/tool.cpp +++ b/src/tools/tool.cpp @@ -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 default_params = { {"SE_MASK", 0x111111}, // One every 4 SEs, by default