rocprofiler-sdk/cxx/codeobj/disassembly.hpp Source File

rocprofiler-sdk/cxx/codeobj/disassembly.hpp Source File#

Rocprofiler SDK Developer API: rocprofiler-sdk/cxx/codeobj/disassembly.hpp Source File
Rocprofiler SDK Developer API 0.6.0
ROCm Profiling API and tools
disassembly.hpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23#pragma once
24
25#include <amd_comgr/amd_comgr.h>
26#include <fcntl.h>
27#include <hsa/amd_hsa_elf.h>
28#include <sys/mman.h>
29#include <sys/stat.h>
30#include <sys/types.h>
31#include <unistd.h>
32
33#include <cstring>
34#include <fstream>
35#include <iostream>
36#include <limits>
37#include <map>
38#include <memory>
39#include <optional>
40#include <string>
41#include <unordered_map>
42#include <vector>
43
44#define THROW_COMGR(call) \
45 if(amd_comgr_status_s status = call) \
46 { \
47 const char* reason = ""; \
48 amd_comgr_status_string(status, &reason); \
49 std::cerr << __FILE__ << ':' << __LINE__ << " code: " << status << " failed: " << reason \
50 << "\n"; \
51 throw std::exception(); \
52 }
53
54#define RETURN_COMGR(call) \
55 if(amd_comgr_status_s status = call) \
56 { \
57 const char* reason = ""; \
58 amd_comgr_status_string(status, &reason); \
59 std::cerr << __FILE__ << ':' << __LINE__ << " code: " << status << " failed: " << reason \
60 << "\n"; \
61 return AMD_COMGR_STATUS_ERROR; \
62 }
63
64#define CHECK_VA2FO(x, msg) \
65 if(!(x)) \
66 { \
67 std::cerr << __FILE__ << ' ' << __LINE__ << ' ' << msg << "\n"; \
68 return std::nullopt; \
69 }
70
71namespace rocprofiler
72{
73namespace sdk
74{
75namespace codeobj
76{
77namespace disassembly
78{
80{
81public:
82 CodeObjectBinary(std::string _uri)
83 : m_uri(std::move(_uri))
84 {
85 const std::string protocol_delim{"://"};
86
87 size_t protocol_end = m_uri.find(protocol_delim);
88 std::string protocol = m_uri.substr(0, protocol_end);
89 protocol_end += protocol_delim.length();
90
91 std::transform(protocol.begin(), protocol.end(), protocol.begin(), [](unsigned char c) {
92 return std::tolower(c);
93 });
94
95 std::string path;
96 size_t path_end = m_uri.find_first_of("#?", protocol_end);
97 if(path_end != std::string::npos)
98 {
99 path = m_uri.substr(protocol_end, path_end++ - protocol_end);
100 }
101 else
102 {
103 path = m_uri.substr(protocol_end);
104 }
105
106 /* %-decode the string. */
107 std::string decoded_path;
108 decoded_path.reserve(path.length());
109 for(size_t i = 0; i < path.length(); ++i)
110 {
111 if(path[i] == '%' && std::isxdigit(path[i + 1]) != 0 && std::isxdigit(path[i + 2]) != 0)
112 {
113 decoded_path += std::stoi(path.substr(i + 1, 2), nullptr, 16);
114 i += 2;
115 }
116 else
117 {
118 decoded_path += path[i];
119 }
120 }
121
122 /* Tokenize the query/fragment. */
123 std::vector<std::string> tokens;
124 size_t pos, last = path_end;
125 while((pos = m_uri.find('&', last)) != std::string::npos)
126 {
127 tokens.emplace_back(m_uri.substr(last, pos - last));
128 last = pos + 1;
129 }
130 if(last != std::string::npos)
131 {
132 tokens.emplace_back(m_uri.substr(last));
133 }
134
135 /* Create a tag-value map from the tokenized query/fragment. */
136 std::unordered_map<std::string, std::string> params;
137 std::for_each(tokens.begin(), tokens.end(), [&](std::string& token) {
138 size_t delim = token.find('=');
139 if(delim != std::string::npos)
140 {
141 params.emplace(token.substr(0, delim), token.substr(delim + 1));
142 }
143 });
144
145 buffer = std::vector<char>{};
146 size_t offset = 0;
147 size_t size = 0;
148
149 if(auto offset_it = params.find("offset"); offset_it != params.end())
150 {
151 offset = std::stoul(offset_it->second, nullptr, 0);
152 }
153
154 if(auto size_it = params.find("size"); size_it != params.end())
155 {
156 if((size = std::stoul(size_it->second, nullptr, 0)) == 0) return;
157 }
158
159 if(protocol == "memory") throw std::runtime_error(protocol + " protocol not supported!");
160
161 std::ifstream file(decoded_path, std::ios::in | std::ios::binary);
162 if(!file || !file.is_open()) throw std::runtime_error("could not open " + decoded_path);
163
164 if(size == 0)
165 {
166 file.ignore(std::numeric_limits<std::streamsize>::max());
167 size_t bytes = file.gcount();
168 file.clear();
169
170 if(bytes < offset) throw std::runtime_error("invalid uri " + decoded_path);
171
172 size = bytes - offset;
173 }
174
175 file.seekg(offset, std::ios_base::beg);
176 buffer.resize(size);
177 file.read(buffer.data(), size);
178 }
179
180 std::string m_uri;
181 std::vector<char> buffer;
182};
183
185{
186 std::string name{};
187 uint64_t faddr = 0;
188 uint64_t vaddr = 0;
189 uint64_t mem_size = 0;
190};
191
193{
194public:
195 DisassemblyInstance(const char* codeobj_data, uint64_t codeobj_size)
196 {
197 buffer = std::vector<char>(codeobj_size, 0);
198 std::memcpy(buffer.data(), codeobj_data, codeobj_size);
199
200 THROW_COMGR(amd_comgr_create_data(AMD_COMGR_DATA_KIND_EXECUTABLE, &data));
201 THROW_COMGR(amd_comgr_set_data(data, buffer.size(), buffer.data()));
202
203 size_t isa_size = 128;
204 std::string input_isa{};
205 input_isa.resize(isa_size);
206 THROW_COMGR(amd_comgr_get_data_isa_name(data, &isa_size, input_isa.data()));
207
208 THROW_COMGR(amd_comgr_create_disassembly_info(
209 input_isa.data(),
210 &DisassemblyInstance::memory_callback,
211 &DisassemblyInstance::inst_callback,
212 [](uint64_t, void*) {},
213 &info));
214 }
216 {
217 amd_comgr_release_data(data);
218 amd_comgr_destroy_disassembly_info(info);
219 }
220
221 std::pair<std::string, size_t> ReadInstruction(uint64_t faddr)
222 {
223 uint64_t size_read;
224 uint64_t addr_in_buffer = reinterpret_cast<uint64_t>(buffer.data()) + faddr;
225
227 amd_comgr_disassemble_instruction(info, addr_in_buffer, (void*) this, &size_read));
228 return {std::move(this->last_instruction), size_read};
229 }
230
231 std::map<uint64_t, SymbolInfo>& GetKernelMap()
232 {
233 symbol_map = {};
234 THROW_COMGR(amd_comgr_iterate_symbols(data, &DisassemblyInstance::symbol_callback, this));
235
236 return symbol_map;
237 }
238
239 static amd_comgr_status_t symbol_callback(amd_comgr_symbol_t symbol, void* user_data)
240 {
241 amd_comgr_symbol_type_t type;
242 RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_TYPE, &type));
243
244 if(type != AMD_COMGR_SYMBOL_TYPE_FUNC) return AMD_COMGR_STATUS_SUCCESS;
245
246 uint64_t vaddr = 0;
247 uint64_t mem_size = 0;
248 uint64_t name_size = 0;
249 RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_VALUE, &vaddr));
250 RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_SIZE, &mem_size));
252 amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_NAME_LENGTH, &name_size));
253
254 std::string name;
255 name.resize(name_size);
256
257 RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_NAME, name.data()));
258
259 DisassemblyInstance& instance = *static_cast<DisassemblyInstance*>(user_data);
260 std::optional<uint64_t> faddr = instance.va2fo(vaddr);
261
262 if(faddr) instance.symbol_map[vaddr] = {name, *faddr, vaddr, mem_size};
263 return AMD_COMGR_STATUS_SUCCESS;
264 }
265
266 static uint64_t memory_callback(uint64_t from, char* to, uint64_t size, void* user_data)
267 {
268 DisassemblyInstance& instance = *static_cast<DisassemblyInstance*>(user_data);
269 int64_t copysize = reinterpret_cast<int64_t>(instance.buffer.data()) +
270 instance.buffer.size() - static_cast<int64_t>(from);
271 copysize = std::min<int64_t>(size, copysize);
272 // NOLINTNEXTLINE(performance-no-int-to-ptr)
273 std::memcpy(to, (char*) from, copysize);
274 return copysize;
275 }
276
277 static void inst_callback(const char* instruction, void* user_data)
278 {
279 DisassemblyInstance& instance = *static_cast<DisassemblyInstance*>(user_data);
280
281 if(!instruction) return;
282
283 while(*instruction == '\t' || *instruction == ' ')
284 instruction++;
285 instance.last_instruction = instruction;
286 }
287
288 std::optional<uint64_t> va2fo(uint64_t va)
289 {
290 CHECK_VA2FO(buffer.size() > sizeof(Elf64_Ehdr), "buffer is not large enough");
291
292 uint8_t* e_ident = (uint8_t*) buffer.data();
293 CHECK_VA2FO(e_ident, "e_ident is nullptr");
294
295 CHECK_VA2FO(e_ident[EI_MAG0] == ELFMAG0 || e_ident[EI_MAG1] == ELFMAG1 ||
296 e_ident[EI_MAG2] == ELFMAG2 || e_ident[EI_MAG3] == ELFMAG3,
297 "unexpected ei_mag");
298
299 CHECK_VA2FO(e_ident[EI_CLASS] == ELFCLASS64, "unexpected ei_class");
300 CHECK_VA2FO(e_ident[EI_DATA] == ELFDATA2LSB, "unexpected ei_data");
301 CHECK_VA2FO(e_ident[EI_VERSION] == EV_CURRENT, "unexpected ei_version");
302 CHECK_VA2FO(e_ident[EI_OSABI] == 64, "unexpected ei_osabi"); // ELFOSABI_AMDGPU_HSA
303
304 CHECK_VA2FO(e_ident[EI_ABIVERSION] == 2 || // ELFABIVERSION_AMDGPU_HSA_V4
305 e_ident[EI_ABIVERSION] == 3,
306 "unexpected ei_abiversion"); // ELFABIVERSION_AMDGPU_HSA_V5
307
308 Elf64_Ehdr* ehdr = (Elf64_Ehdr*) buffer.data();
309 CHECK_VA2FO(ehdr, "ehdr is nullptr");
310 CHECK_VA2FO(ehdr->e_type == ET_DYN, "unexpected e_type");
311 CHECK_VA2FO(ehdr->e_machine == ELF::EM_AMDGPU, "unexpected e_machine");
312 CHECK_VA2FO(ehdr->e_phoff != 0, "unexpected e_phoff");
313
314 CHECK_VA2FO(buffer.size() > ehdr->e_phoff + sizeof(Elf64_Phdr),
315 "buffer is not large enough");
316
317 Elf64_Phdr* phdr = (Elf64_Phdr*) ((uint8_t*) buffer.data() + ehdr->e_phoff);
318 CHECK_VA2FO(phdr, "phdr is nullptr");
319
320 for(uint16_t i = 0; i < ehdr->e_phnum; ++i)
321 {
322 if(phdr[i].p_type != PT_LOAD) continue;
323 if(va < phdr[i].p_vaddr || va >= (phdr[i].p_vaddr + phdr[i].p_memsz)) continue;
324
325 return va + phdr[i].p_offset - phdr[i].p_vaddr;
326 }
327 return std::nullopt;
328 }
329
330 std::vector<char> buffer;
331 std::string last_instruction;
332 amd_comgr_disassembly_info_t info;
333 amd_comgr_data_t data;
334 std::map<uint64_t, SymbolInfo> symbol_map;
335};
336
337} // namespace disassembly
338} // namespace codeobj
339} // namespace sdk
340} // namespace rocprofiler
static uint64_t memory_callback(uint64_t from, char *to, uint64_t size, void *user_data)
static amd_comgr_status_t symbol_callback(amd_comgr_symbol_t symbol, void *user_data)
std::pair< std::string, unsigned long > ReadInstruction(uint64_t faddr)
static void inst_callback(const char *instruction, void *user_data)
DisassemblyInstance(const char *codeobj_data, uint64_t codeobj_size)
#define RETURN_COMGR(call)
#define THROW_COMGR(call)
#define CHECK_VA2FO(x, msg)
STL namespace.