CodeobjDecoderComponent Class Reference

CodeobjDecoderComponent Class Reference#

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

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

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

Public Member Functions

 CodeobjDecoderComponent (const char *codeobj_data, uint64_t codeobj_size)
 
 ~CodeobjDecoderComponent ()=default
 
std::optional< uint64_t > va2fo (uint64_t vaddr) const
 
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 67 of file code_printing.hpp.

Constructor & Destructor Documentation

◆ CodeobjDecoderComponent()

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

Definition at line 87 of file code_printing.hpp.

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

References disassembly, m_line_number_map, and m_symbol_map.

◆ ~CodeobjDecoderComponent()

rocprofiler::sdk::codeobj::disassembly::CodeobjDecoderComponent::~CodeobjDecoderComponent ( )
default

Member Function Documentation

◆ disassemble_instruction()

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

Definition at line 175 of file code_printing.hpp.

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

References disassembly, and m_line_number_map.

◆ va2fo()

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

Definition at line 169 of file code_printing.hpp.

170 {
171 if(disassembly) return disassembly->va2fo(vaddr);
172 return {};
173 };

References disassembly.

Field Documentation

◆ disassembly

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

Definition at line 192 of file code_printing.hpp.

192{};

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

◆ instructions

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

Definition at line 191 of file code_printing.hpp.

191{};

◆ m_line_number_map

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

Definition at line 194 of file code_printing.hpp.

194{};

Referenced by CodeobjDecoderComponent(), and disassemble_instruction().

◆ m_symbol_map

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

Definition at line 190 of file code_printing.hpp.

190{};

Referenced by CodeobjDecoderComponent().


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