Skip to content

Commit 2e3191b

Browse files
author
Baraldi, Giovanni
authored
Update codeobj disassembly to use comgr va2fo API (#250)
* Update codeobj disassembly to use comgr va2fo API * Format * Tidy fix * Tidy fix * Review comments --------- Co-authored-by: Giovanni Baraldi <gbaraldi@amd.com> [ROCm/rocprofiler-sdk commit: 970beba]
1 parent 985d0ed commit 2e3191b

File tree

3 files changed

+23
-55
lines changed

3 files changed

+23
-55
lines changed

projects/rocprofiler-sdk/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ Full documentation for ROCprofiler-SDK is available at [rocm.docs.amd.com/projec
168168
### Changed
169169

170170
- SDK no longer creates a background thread when every tool returns a nullptr from `rocprofiler_configure`.
171+
- Updated disassembly.hpp's vaddr-to-file-offset mapping to use the dedicated comgr API.
171172

172173
### Resolved issues
173174

projects/rocprofiler-sdk/source/include/rocprofiler-sdk/cxx/codeobj/code_printing.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
#pragma once
2424

25+
#include "disassembly.hpp"
26+
#include "segment.hpp"
27+
2528
#include <elfutils/libdw.h>
2629
#include <hsa/amd_hsa_elf.h>
2730

@@ -35,9 +38,6 @@
3538
#include <unordered_map>
3639
#include <vector>
3740

38-
#include "disassembly.hpp"
39-
#include "segment.hpp"
40-
4141
namespace rocprofiler
4242
{
4343
namespace sdk
@@ -169,7 +169,7 @@ class CodeobjDecoderComponent
169169
std::optional<uint64_t> va2fo(uint64_t vaddr) const
170170
{
171171
if(disassembly) return disassembly->va2fo(vaddr);
172-
return {};
172+
return std::nullopt;
173173
};
174174

175175
std::unique_ptr<Instruction> disassemble_instruction(uint64_t faddr, uint64_t vaddr)

projects/rocprofiler-sdk/source/include/rocprofiler-sdk/cxx/codeobj/disassembly.hpp

Lines changed: 18 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
#pragma once
2424

2525
#include <amd_comgr/amd_comgr.h>
26-
#include <fcntl.h>
2726
#include <hsa/amd_hsa_elf.h>
27+
28+
#include <fcntl.h>
2829
#include <sys/mman.h>
2930
#include <sys/stat.h>
3031
#include <sys/types.h>
3132
#include <unistd.h>
3233

34+
#include <algorithm>
3335
#include <cstring>
3436
#include <fstream>
3537
#include <iostream>
@@ -61,13 +63,6 @@
6163
return AMD_COMGR_STATUS_ERROR; \
6264
}
6365

64-
#define CHECK_VA2FO(x, msg) \
65-
if(!(x)) \
66-
{ \
67-
std::cerr << __FILE__ << ' ' << __LINE__ << ' ' << msg << "\n"; \
68-
return std::nullopt; \
69-
}
70-
7166
namespace rocprofiler
7267
{
7368
namespace sdk
@@ -285,54 +280,26 @@ class DisassemblyInstance
285280
instance.last_instruction = instruction;
286281
}
287282

288-
std::optional<uint64_t> va2fo(uint64_t va)
283+
std::optional<uint64_t> va2fo(uint64_t va) const
289284
{
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");
285+
uint64_t offset = 0;
286+
uint64_t slicesize = 0;
287+
bool nobits = false;
298288

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
289+
auto status = amd_comgr_map_elf_virtual_address_to_code_object_offset(
290+
data, va, &offset, &slicesize, &nobits);
303291

304-
CHECK_VA2FO(e_ident[EI_ABIVERSION] == 2 || // ELFABIVERSION_AMDGPU_HSA_V4
305-
e_ident[EI_ABIVERSION] == 3 || // ELFABIVERSION_AMDGPU_HSA_V5
306-
e_ident[EI_ABIVERSION] == 4, // ELFABIVERSION_AMDGPU_HSA_V6
307-
"unexpected ei_abiversion");
308-
309-
Elf64_Ehdr* ehdr = (Elf64_Ehdr*) buffer.data();
310-
CHECK_VA2FO(ehdr, "ehdr is nullptr");
311-
CHECK_VA2FO(ehdr->e_type == ET_DYN, "unexpected e_type");
312-
CHECK_VA2FO(ehdr->e_machine == ELF::EM_AMDGPU, "unexpected e_machine");
313-
CHECK_VA2FO(ehdr->e_phoff != 0, "unexpected e_phoff");
314-
315-
CHECK_VA2FO(buffer.size() > ehdr->e_phoff + sizeof(Elf64_Phdr),
316-
"buffer is not large enough");
317-
318-
Elf64_Phdr* phdr = (Elf64_Phdr*) ((uint8_t*) buffer.data() + ehdr->e_phoff);
319-
CHECK_VA2FO(phdr, "phdr is nullptr");
320-
321-
for(uint16_t i = 0; i < ehdr->e_phnum; ++i)
322-
{
323-
if(phdr[i].p_type != PT_LOAD) continue;
324-
if(va < phdr[i].p_vaddr || va >= (phdr[i].p_vaddr + phdr[i].p_memsz)) continue;
325-
326-
return va + phdr[i].p_offset - phdr[i].p_vaddr;
327-
}
328-
return std::nullopt;
292+
if(status != AMD_COMGR_STATUS_SUCCESS || nobits)
293+
return std::nullopt;
294+
else
295+
return offset;
329296
}
330297

331-
std::vector<char> buffer;
332-
std::string last_instruction;
333-
amd_comgr_disassembly_info_t info;
334-
amd_comgr_data_t data;
335-
std::map<uint64_t, SymbolInfo> symbol_map;
298+
std::vector<char> buffer{};
299+
std::string last_instruction{};
300+
amd_comgr_disassembly_info_t info{};
301+
amd_comgr_data_t data{};
302+
std::map<uint64_t, SymbolInfo> symbol_map{};
336303
};
337304

338305
} // namespace disassembly

0 commit comments

Comments
 (0)