DisassemblyInstance Class Reference

DisassemblyInstance Class Reference#

Rocprofiler SDK Developer API: rocprofiler::codeobj::disassembly::DisassemblyInstance Class Reference
Rocprofiler SDK Developer API 0.4.0
ROCm Profiling API and tools
rocprofiler::codeobj::disassembly::DisassemblyInstance Class Reference

#include "rocprofiler-sdk/amd_detail/rocprofiler-sdk-codeobj/disassembly.hpp"

+ Collaboration diagram for rocprofiler::codeobj::disassembly::DisassemblyInstance:

Public Member Functions

 DisassemblyInstance (const char *codeobj_data, uint64_t codeobj_size)
 
 ~DisassemblyInstance ()
 
std::pair< std::string, unsigned long > ReadInstruction (uint64_t faddr)
 
std::map< uint64_t, SymbolInfo > & GetKernelMap ()
 
std::optional< uint64_t > va2fo (uint64_t va)
 

Static Public Member Functions

static amd_comgr_status_t symbol_callback (amd_comgr_symbol_t symbol, void *user_data)
 
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)
 

Data Fields

std::vector< char > buffer
 
std::string last_instruction
 
amd_comgr_disassembly_info_t info
 
amd_comgr_data_t data
 
std::map< uint64_t, SymbolInfosymbol_map
 

Detailed Description

Definition at line 190 of file disassembly.hpp.

Constructor & Destructor Documentation

◆ DisassemblyInstance()

rocprofiler::codeobj::disassembly::DisassemblyInstance::DisassemblyInstance ( const char *  codeobj_data,
uint64_t  codeobj_size 
)
inline

Definition at line 193 of file disassembly.hpp.

194 {
195 buffer = std::vector<char>(codeobj_size, 0);
196 std::memcpy(buffer.data(), codeobj_data, codeobj_size);
197
198 THROW_COMGR(amd_comgr_create_data(AMD_COMGR_DATA_KIND_EXECUTABLE, &data));
199 THROW_COMGR(amd_comgr_set_data(data, buffer.size(), buffer.data()));
200
201 size_t isa_size = 128;
202 std::string input_isa{};
203 input_isa.resize(isa_size);
204 THROW_COMGR(amd_comgr_get_data_isa_name(data, &isa_size, input_isa.data()));
205
206 THROW_COMGR(amd_comgr_create_disassembly_info(
207 input_isa.data(),
210 [](uint64_t, void*) {},
211 &info));
212 }
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)
#define THROW_COMGR(call)

References THROW_COMGR.

◆ ~DisassemblyInstance()

rocprofiler::codeobj::disassembly::DisassemblyInstance::~DisassemblyInstance ( )
inline

Definition at line 213 of file disassembly.hpp.

214 {
215 amd_comgr_release_data(data);
216 amd_comgr_destroy_disassembly_info(info);
217 }

Member Function Documentation

◆ GetKernelMap()

std::map< uint64_t, SymbolInfo > & rocprofiler::codeobj::disassembly::DisassemblyInstance::GetKernelMap ( )
inline

Definition at line 229 of file disassembly.hpp.

230 {
231 symbol_map = {};
232 THROW_COMGR(amd_comgr_iterate_symbols(data, &DisassemblyInstance::symbol_callback, this));
233
234 return symbol_map;
235 }
static amd_comgr_status_t symbol_callback(amd_comgr_symbol_t symbol, void *user_data)

References THROW_COMGR.

◆ inst_callback()

static void rocprofiler::codeobj::disassembly::DisassemblyInstance::inst_callback ( const char *  instruction,
void *  user_data 
)
inlinestatic

Definition at line 274 of file disassembly.hpp.

275 {
276 DisassemblyInstance& instance = *static_cast<DisassemblyInstance*>(user_data);
277
278 if(!instruction) return;
279
280 while(*instruction == '\t' || *instruction == ' ')
281 instruction++;
282 instance.last_instruction = instruction;
283 }
DisassemblyInstance(const char *codeobj_data, uint64_t codeobj_size)

References last_instruction.

◆ memory_callback()

static uint64_t rocprofiler::codeobj::disassembly::DisassemblyInstance::memory_callback ( uint64_t  from,
char *  to,
uint64_t  size,
void *  user_data 
)
inlinestatic

Definition at line 264 of file disassembly.hpp.

265 {
266 DisassemblyInstance& instance = *static_cast<DisassemblyInstance*>(user_data);
267 int64_t copysize = reinterpret_cast<int64_t>(instance.buffer.data()) +
268 instance.buffer.size() - static_cast<int64_t>(from);
269 copysize = std::min<int64_t>(size, copysize);
270 std::memcpy(to, (char*) from, copysize);
271 return copysize;
272 }

References buffer.

◆ ReadInstruction()

std::pair< std::string, unsigned long > rocprofiler::codeobj::disassembly::DisassemblyInstance::ReadInstruction ( uint64_t  faddr)
inline

Definition at line 219 of file disassembly.hpp.

220 {
221 uint64_t size_read;
222 uint64_t addr_in_buffer = reinterpret_cast<uint64_t>(buffer.data()) + faddr;
223
225 amd_comgr_disassemble_instruction(info, addr_in_buffer, (void*) this, &size_read));
226 return {std::move(this->last_instruction), size_read};
227 }

References THROW_COMGR.

◆ symbol_callback()

static amd_comgr_status_t rocprofiler::codeobj::disassembly::DisassemblyInstance::symbol_callback ( amd_comgr_symbol_t  symbol,
void *  user_data 
)
inlinestatic

Definition at line 237 of file disassembly.hpp.

238 {
239 amd_comgr_symbol_type_t type;
240 RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_TYPE, &type));
241
242 if(type != AMD_COMGR_SYMBOL_TYPE_FUNC) return AMD_COMGR_STATUS_SUCCESS;
243
244 uint64_t vaddr = 0;
245 uint64_t mem_size = 0;
246 uint64_t name_size = 0;
247 RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_VALUE, &vaddr));
248 RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_SIZE, &mem_size));
250 amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_NAME_LENGTH, &name_size));
251
252 std::string name;
253 name.resize(name_size);
254
255 RETURN_COMGR(amd_comgr_symbol_get_info(symbol, AMD_COMGR_SYMBOL_INFO_NAME, name.data()));
256
257 DisassemblyInstance& instance = *static_cast<DisassemblyInstance*>(user_data);
258 std::optional<uint64_t> faddr = instance.va2fo(vaddr);
259
260 if(faddr) instance.symbol_map[vaddr] = {name, *faddr, vaddr, mem_size};
261 return AMD_COMGR_STATUS_SUCCESS;
262 }
#define RETURN_COMGR(call)

References RETURN_COMGR, symbol_map, and va2fo().

+ Here is the call graph for this function:

◆ va2fo()

std::optional< uint64_t > rocprofiler::codeobj::disassembly::DisassemblyInstance::va2fo ( uint64_t  va)
inline

Definition at line 285 of file disassembly.hpp.

286 {
287 CHECK_VA2FO(buffer.size() > sizeof(Elf64_Ehdr), "buffer is not large enough");
288
289 uint8_t* e_ident = (uint8_t*) buffer.data();
290 CHECK_VA2FO(e_ident, "e_ident is nullptr");
291
292 CHECK_VA2FO(e_ident[EI_MAG0] == ELFMAG0 || e_ident[EI_MAG1] == ELFMAG1 ||
293 e_ident[EI_MAG2] == ELFMAG2 || e_ident[EI_MAG3] == ELFMAG3,
294 "unexpected ei_mag");
295
296 CHECK_VA2FO(e_ident[EI_CLASS] == ELFCLASS64, "unexpected ei_class");
297 CHECK_VA2FO(e_ident[EI_DATA] == ELFDATA2LSB, "unexpected ei_data");
298 CHECK_VA2FO(e_ident[EI_VERSION] == EV_CURRENT, "unexpected ei_version");
299 CHECK_VA2FO(e_ident[EI_OSABI] == 64, "unexpected ei_osabi"); // ELFOSABI_AMDGPU_HSA
300
301 CHECK_VA2FO(e_ident[EI_ABIVERSION] == 2 || // ELFABIVERSION_AMDGPU_HSA_V4
302 e_ident[EI_ABIVERSION] == 3,
303 "unexpected ei_abiversion"); // ELFABIVERSION_AMDGPU_HSA_V5
304
305 Elf64_Ehdr* ehdr = (Elf64_Ehdr*) buffer.data();
306 CHECK_VA2FO(ehdr, "ehdr is nullptr");
307 CHECK_VA2FO(ehdr->e_type == ET_DYN, "unexpected e_type");
308 CHECK_VA2FO(ehdr->e_machine == ELF::EM_AMDGPU, "unexpected e_machine");
309 CHECK_VA2FO(ehdr->e_phoff != 0, "unexpected e_phoff");
310
311 CHECK_VA2FO(buffer.size() > ehdr->e_phoff + sizeof(Elf64_Phdr),
312 "buffer is not large enough");
313
314 Elf64_Phdr* phdr = (Elf64_Phdr*) ((uint8_t*) buffer.data() + ehdr->e_phoff);
315 CHECK_VA2FO(phdr, "phdr is nullptr");
316
317 for(uint16_t i = 0; i < ehdr->e_phnum; ++i)
318 {
319 if(phdr[i].p_type != PT_LOAD) continue;
320 if(va < phdr[i].p_vaddr || va >= (phdr[i].p_vaddr + phdr[i].p_memsz)) continue;
321
322 return va + phdr[i].p_offset - phdr[i].p_vaddr;
323 }
324 return std::nullopt;
325 }
#define CHECK_VA2FO(x, msg)

References CHECK_VA2FO.

Referenced by symbol_callback().

+ Here is the caller graph for this function:

Field Documentation

◆ buffer

std::vector<char> rocprofiler::codeobj::disassembly::DisassemblyInstance::buffer

Definition at line 327 of file disassembly.hpp.

Referenced by memory_callback().

◆ data

amd_comgr_data_t rocprofiler::codeobj::disassembly::DisassemblyInstance::data

Definition at line 330 of file disassembly.hpp.

◆ info

amd_comgr_disassembly_info_t rocprofiler::codeobj::disassembly::DisassemblyInstance::info

Definition at line 329 of file disassembly.hpp.

◆ last_instruction

std::string rocprofiler::codeobj::disassembly::DisassemblyInstance::last_instruction

Definition at line 328 of file disassembly.hpp.

Referenced by inst_callback().

◆ symbol_map

std::map<uint64_t, SymbolInfo> rocprofiler::codeobj::disassembly::DisassemblyInstance::symbol_map

Definition at line 331 of file disassembly.hpp.

Referenced by symbol_callback().


The documentation for this class was generated from the following file: