CodeobjDecoderComponent Class Reference

CodeobjDecoderComponent Class Reference#

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

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

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

Public Member Functions

 CodeobjDecoderComponent (const char *codeobj_data, uint64_t codeobj_size)
 
 ~CodeobjDecoderComponent ()
 
std::optional< uint64_t > va2fo (uint64_t vaddr)
 
std::unique_ptr< Instructiondisassemble_instruction (uint64_t faddr, uint64_t vaddr)
 

Data Fields

std::map< uint64_t, SymbolInfom_symbol_map {}
 
std::vector< std::shared_ptr< Instruction > > instructions {}
 
std::unique_ptr< DisassemblyInstancedisassembly {}
 
std::map< segment::address_range_t, std::string > m_line_number_map {}
 

Detailed Description

Definition at line 65 of file code_printing.hpp.

Constructor & Destructor Documentation

◆ CodeobjDecoderComponent()

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

Definition at line 85 of file code_printing.hpp.

86 {
87 ProtectedFd prot("");
88 if(::write(prot.m_fd, codeobj_data, codeobj_size) != static_cast<int64_t>(codeobj_size))
89 throw std::runtime_error("Could not write to temporary file!");
90
91 ::lseek(prot.m_fd, 0, SEEK_SET);
92 fsync(prot.m_fd);
93
95
96 std::unique_ptr<Dwarf, void (*)(Dwarf*)> dbg(dwarf_begin(prot.m_fd, DWARF_C_READ),
97 [](Dwarf* _dbg) { dwarf_end(_dbg); });
98
99 if(dbg)
100 {
101 Dwarf_Off cu_offset{0}, next_offset;
102 size_t header_size;
103
104 std::map<uint64_t, std::string> line_addrs;
105
106 while(!dwarf_nextcu(
107 dbg.get(), cu_offset, &next_offset, &header_size, nullptr, nullptr, nullptr))
108 {
109 Dwarf_Die die;
110 if(!dwarf_offdie(dbg.get(), cu_offset + header_size, &die)) continue;
111
112 Dwarf_Lines* lines;
113 size_t line_count;
114 if(dwarf_getsrclines(&die, &lines, &line_count)) continue;
115
116 for(size_t i = 0; i < line_count; ++i)
117 {
118 Dwarf_Addr addr;
119 int line_number;
120 Dwarf_Line* line = dwarf_onesrcline(lines, i);
121
122 if(line && !dwarf_lineaddr(line, &addr) && !dwarf_lineno(line, &line_number) &&
123 line_number)
124 {
125 std::string src = dwarf_linesrc(line, nullptr, nullptr);
126 auto dwarf_line = src + ':' + std::to_string(line_number);
127
128 if(line_addrs.find(addr) != line_addrs.end())
129 {
130 line_addrs.at(addr) += ' ' + dwarf_line;
131 continue;
132 }
133
134 line_addrs.emplace(addr, std::move(dwarf_line));
135 }
136 }
137 cu_offset = next_offset;
138 }
139
140 auto it = line_addrs.begin();
141 if(it != line_addrs.end())
142 {
143 while(std::next(it) != line_addrs.end())
144 {
145 uint64_t delta = std::next(it)->first - it->first;
146 auto segment = segment::address_range_t{it->first, delta, 0};
147 m_line_number_map.emplace(segment, std::move(it->second));
148 it++;
149 }
150 auto segment = segment::address_range_t{it->first, codeobj_size - it->first, 0};
151 m_line_number_map.emplace(segment, std::move(it->second));
152 }
153 }
154
155 // Can throw
156 disassembly = std::make_unique<DisassemblyInstance>(codeobj_data, codeobj_size);
157 try
158 {
159 m_symbol_map = disassembly->GetKernelMap(); // Can throw
160 } catch(...)
161 {}
162 }
std::map< segment::address_range_t, std::string > m_line_number_map

References disassembly, m_line_number_map, and m_symbol_map.

◆ ~CodeobjDecoderComponent()

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

Definition at line 163 of file code_printing.hpp.

163{}

Member Function Documentation

◆ disassemble_instruction()

std::unique_ptr< Instruction > rocprofiler::codeobj::disassembly::CodeobjDecoderComponent::disassemble_instruction ( uint64_t  faddr,
uint64_t  vaddr 
)
inline

Definition at line 171 of file code_printing.hpp.

172 {
173 if(!disassembly) throw std::exception();
174
175 auto pair = disassembly->ReadInstruction(faddr);
176 auto inst = std::make_unique<Instruction>(std::move(pair.first), pair.second);
177 inst->faddr = faddr;
178 inst->vaddr = vaddr;
179
180 auto it = m_line_number_map.find({vaddr, 0, 0});
181 if(it != m_line_number_map.end()) inst->comment = it->second;
182
183 return inst;
184 }

References disassembly, and m_line_number_map.

◆ va2fo()

std::optional< uint64_t > rocprofiler::codeobj::disassembly::CodeobjDecoderComponent::va2fo ( uint64_t  vaddr)
inline

Definition at line 165 of file code_printing.hpp.

166 {
167 if(disassembly) return disassembly->va2fo(vaddr);
168 return {};
169 };

References disassembly.

Field Documentation

◆ disassembly

std::unique_ptr<DisassemblyInstance> rocprofiler::codeobj::disassembly::CodeobjDecoderComponent::disassembly {}

Definition at line 188 of file code_printing.hpp.

188{};

Referenced by CodeobjDecoderComponent(), disassemble_instruction(), and va2fo().

◆ instructions

std::vector<std::shared_ptr<Instruction> > rocprofiler::codeobj::disassembly::CodeobjDecoderComponent::instructions {}

Definition at line 187 of file code_printing.hpp.

187{};

◆ m_line_number_map

std::map<segment::address_range_t, std::string> rocprofiler::codeobj::disassembly::CodeobjDecoderComponent::m_line_number_map {}

Definition at line 190 of file code_printing.hpp.

190{};

Referenced by CodeobjDecoderComponent(), and disassemble_instruction().

◆ m_symbol_map

std::map<uint64_t, SymbolInfo> rocprofiler::codeobj::disassembly::CodeobjDecoderComponent::m_symbol_map {}

Definition at line 186 of file code_printing.hpp.

186{};

Referenced by CodeobjDecoderComponent().


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