DisassemblyInstance Class Reference

DisassemblyInstance Class Reference#

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

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

+ Collaboration diagram for rocprofiler::sdk::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 192 of file disassembly.hpp.

Constructor & Destructor Documentation

◆ DisassemblyInstance()

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

Definition at line 195 of file disassembly.hpp.

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(),
212 [](uint64_t, void*) {},
213 &info));
214 }
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::sdk::codeobj::disassembly::DisassemblyInstance::~DisassemblyInstance ( )
inline

Definition at line 215 of file disassembly.hpp.

216 {
217 amd_comgr_release_data(data);
218 amd_comgr_destroy_disassembly_info(info);
219 }

Member Function Documentation

◆ GetKernelMap()

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

Definition at line 231 of file disassembly.hpp.

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

References THROW_COMGR.

◆ inst_callback()

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

Definition at line 277 of file disassembly.hpp.

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 }
DisassemblyInstance(const char *codeobj_data, uint64_t codeobj_size)

References last_instruction.

◆ memory_callback()

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

Definition at line 266 of file disassembly.hpp.

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 }

References buffer.

◆ ReadInstruction()

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

Definition at line 221 of file disassembly.hpp.

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 }

References THROW_COMGR.

◆ symbol_callback()

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

Definition at line 239 of file disassembly.hpp.

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 }
#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::sdk::codeobj::disassembly::DisassemblyInstance::va2fo ( uint64_t  va)
inline

Definition at line 288 of file disassembly.hpp.

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 }
#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::sdk::codeobj::disassembly::DisassemblyInstance::buffer

Definition at line 330 of file disassembly.hpp.

Referenced by memory_callback().

◆ data

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

Definition at line 333 of file disassembly.hpp.

◆ info

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

Definition at line 332 of file disassembly.hpp.

◆ last_instruction

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

Definition at line 331 of file disassembly.hpp.

Referenced by inst_callback().

◆ symbol_map

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

Definition at line 334 of file disassembly.hpp.

Referenced by symbol_callback().


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