Adding inline callstack information to disassembly (#468)
* Adding callstack information to disassembly * changelog * Cleanup * Fix snapshots.json * Clang tidy fixes * Fix infinite recursion * Apply suggestions from code review Co-authored-by: Indic, Vladimir <Vladimir.Indic@amd.com> * Remove sibling transversal * Added docstrings * Apply suggestions from code review * Update source/include/rocprofiler-sdk/cxx/codeobj/code_printing.hpp * Review comments * Format + comments * Fmt * Add class name * Format * Fix static linkage * Making funcs inline --------- Co-authored-by: Giovanni <gbaraldi@amd.com> Co-authored-by: Indic, Vladimir <Vladimir.Indic@amd.com>
이 커밋은 다음에 포함됨:
@@ -213,6 +213,7 @@ Full documentation for ROCprofiler-SDK is available at [rocm.docs.amd.com/projec
|
||||
- Fixed potential data race in rocprofiler-sdk double buffering scheme
|
||||
- Usage of std::regex in core rocprofiler-sdk library which causes segfaults/exceptions when used under dual ABI
|
||||
- Fixed perfetto counter collection by introducing per dispatch accumulation.
|
||||
- Code object disassembly was missing function inlining information
|
||||
- Fixed queue preemption error and HSA_STATUS_ERROR_INVALID_PACKET_FORMAT error for stochastic PC-sampling for MI300X, leading to more stable runs.
|
||||
- Fixed the system hang issue for host-trap PC-sampling on MI300X.
|
||||
|
||||
|
||||
@@ -25,11 +25,13 @@
|
||||
#include "disassembly.hpp"
|
||||
#include "segment.hpp"
|
||||
|
||||
#include <dwarf.h>
|
||||
#include <elfutils/libdw.h>
|
||||
#include <hsa/amd_hsa_elf.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
@@ -62,6 +64,8 @@ struct Instruction
|
||||
size_t size{0};
|
||||
uint64_t ld_addr{0}; // Instruction load address, if from loaded codeobj
|
||||
marker_id_t codeobj_id{0}; // Instruction code object load id, if from loaded codeobj
|
||||
|
||||
static constexpr std::string_view separator = " -> ";
|
||||
};
|
||||
|
||||
class CodeobjDecoderComponent
|
||||
@@ -111,11 +115,19 @@ public:
|
||||
0)
|
||||
{
|
||||
Dwarf_Die die;
|
||||
if(!dwarf_offdie(dbg.get(), cu_offset + header_size, &die)) continue;
|
||||
if(!dwarf_offdie(dbg.get(), cu_offset + header_size, &die))
|
||||
{
|
||||
cu_offset = next_offset;
|
||||
continue;
|
||||
}
|
||||
|
||||
Dwarf_Lines* lines;
|
||||
size_t line_count;
|
||||
if(dwarf_getsrclines(&die, &lines, &line_count) != 0) continue;
|
||||
if(dwarf_getsrclines(&die, &lines, &line_count) != 0)
|
||||
{
|
||||
cu_offset = next_offset;
|
||||
continue;
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < line_count; ++i)
|
||||
{
|
||||
@@ -126,16 +138,22 @@ public:
|
||||
if(line && dwarf_lineaddr(line, &addr) == 0 &&
|
||||
dwarf_lineno(line, &line_number) == 0 && line_number != 0)
|
||||
{
|
||||
std::string src = dwarf_linesrc(line, nullptr, nullptr);
|
||||
auto dwarf_line = src + ':' + std::to_string(line_number);
|
||||
std::string src = dwarf_linesrc(line, nullptr, nullptr);
|
||||
auto dwarf_line = src + ':' + std::to_string(line_number);
|
||||
auto call_stack_info = extractInlinedCallStackInfo(dbg.get(), addr);
|
||||
|
||||
if(line_addrs.find(addr) != line_addrs.end())
|
||||
size_t capacity = dwarf_line.size() +
|
||||
Instruction::separator.size() * call_stack_info.size();
|
||||
for(const auto& call : call_stack_info)
|
||||
capacity += call.size();
|
||||
|
||||
dwarf_line.reserve(capacity);
|
||||
for(const auto& call : call_stack_info)
|
||||
{
|
||||
line_addrs.at(addr) += ' ' + dwarf_line;
|
||||
continue;
|
||||
dwarf_line += Instruction::separator;
|
||||
dwarf_line += call;
|
||||
}
|
||||
|
||||
line_addrs.emplace(addr, std::move(dwarf_line));
|
||||
line_addrs[addr] = std::move(dwarf_line);
|
||||
}
|
||||
}
|
||||
cu_offset = next_offset;
|
||||
@@ -192,6 +210,79 @@ public:
|
||||
std::unique_ptr<DisassemblyInstance> disassembly{};
|
||||
|
||||
std::map<segment::address_range_t, std::string> m_line_number_map{};
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief Extracts inlined function call stack information for a given address
|
||||
*
|
||||
* This function searches through DWARF debug information to find all inlined functions
|
||||
* that contain the specified address, building a complete call stack from the outermost
|
||||
* function down to the innermost inlined function.
|
||||
*
|
||||
* @param dbg DWARF debug information handle
|
||||
* @param addr The address to analyze for inlined function information
|
||||
* @return Vector of strings representing the call stack, formatted as "filename:line"
|
||||
* The stack is ordered from caller to callee (outermost to innermost)
|
||||
*/
|
||||
static std::vector<std::string> extractInlinedCallStackInfo(Dwarf* dbg, Dwarf_Addr addr);
|
||||
|
||||
/**
|
||||
* @brief Checks if a DWARF Debug Information Entry (DIE) contains a specific address
|
||||
*
|
||||
* This function recursively searches through a DIE and its children to determine
|
||||
* if any of them contain the specified address within their address ranges.
|
||||
* Used as an optimization to quickly determine if a compilation unit or function
|
||||
* contains the target address before doing expensive traversal.
|
||||
*
|
||||
* @param die Pointer to the DWARF DIE to check
|
||||
* @param addr The address to search for
|
||||
* @return true if the DIE or any of its children contain the address, false otherwise
|
||||
*/
|
||||
static bool checkDIEContainsAddress(Dwarf_Die* die, Dwarf_Addr addr);
|
||||
|
||||
/**
|
||||
* @brief Recursively traverses all DWARF DIEs to find inlined functions at a specific address
|
||||
*
|
||||
* This function performs a depth-first traversal of the DWARF debug information tree,
|
||||
* checking each DIE for inlined function information that covers the specified address.
|
||||
* It processes both the current DIE and all its children (including siblings at each level)
|
||||
* to ensure comprehensive coverage of all possible inlined function contexts.
|
||||
*
|
||||
* The traversal is necessary because inlined functions can be nested (function A inlines
|
||||
* function B which inlines function C) and multiple inlined functions can exist at the
|
||||
* same scope level as siblings in the DWARF tree.
|
||||
*
|
||||
* @param die Pointer to the current DWARF DIE to examine
|
||||
* @param addr The address to search for inlined function information
|
||||
* @param call_stack Reference to vector that accumulates the call stack information
|
||||
*/
|
||||
static void traverseAllDIEs(Dwarf_Die* die,
|
||||
Dwarf_Addr addr,
|
||||
std::vector<std::string>& call_stack);
|
||||
|
||||
/**
|
||||
* @brief Examines a specific DWARF DIE for inlined function information at an address
|
||||
*
|
||||
* This function checks if a given DIE represents an inlined subroutine that contains
|
||||
* the specified address. If it does, it extracts the call site information (filename
|
||||
* and line number where the function was inlined) and adds it to the call stack.
|
||||
*
|
||||
* The function specifically looks for DW_TAG_inlined_subroutine DIEs and validates
|
||||
* that the address falls within the DIE's address range (either contiguous via
|
||||
* low_pc/high_pc or non-contiguous via ranges attribute). It then extracts the
|
||||
* call site information using DW_AT_call_file and DW_AT_call_line attributes.
|
||||
*
|
||||
* @param die Pointer to the DWARF DIE to examine
|
||||
* @param addr The address to check against the DIE's address ranges
|
||||
* @param call_stack Reference to vector where call site info will be added
|
||||
*
|
||||
* @note Only processes DW_TAG_inlined_subroutine DIEs. Regular functions
|
||||
* (DW_TAG_subprogram) are ignored since this function specifically
|
||||
* extracts inlined function call information.
|
||||
*/
|
||||
static void checkDIEForInlinedFunction(Dwarf_Die* die,
|
||||
Dwarf_Addr addr,
|
||||
std::vector<std::string>& call_stack);
|
||||
};
|
||||
|
||||
class LoadedCodeobjDecoder
|
||||
@@ -230,8 +321,7 @@ public:
|
||||
: load_addr(_load_addr)
|
||||
, load_end(load_addr + _memsize)
|
||||
{
|
||||
decoder =
|
||||
std::make_unique<CodeobjDecoderComponent>(reinterpret_cast<const char*>(data), size);
|
||||
decoder = std::make_unique<CodeobjDecoderComponent>(static_cast<const char*>(data), size);
|
||||
}
|
||||
std::unique_ptr<Instruction> get(uint64_t ld_addr)
|
||||
{
|
||||
@@ -433,6 +523,170 @@ private:
|
||||
segment::CodeobjTableTranslator table{};
|
||||
};
|
||||
|
||||
inline std::vector<std::string>
|
||||
CodeobjDecoderComponent::extractInlinedCallStackInfo(Dwarf* dbg, Dwarf_Addr addr)
|
||||
{
|
||||
std::vector<std::string> call_stack{};
|
||||
|
||||
// Iterate through all compilation units to find the one containing our address
|
||||
Dwarf_Off cu_offset{};
|
||||
Dwarf_Off next_offset{};
|
||||
size_t header_size{};
|
||||
|
||||
while(dwarf_nextcu(dbg, cu_offset, &next_offset, &header_size, nullptr, nullptr, nullptr) == 0)
|
||||
{
|
||||
Dwarf_Die cu_die{};
|
||||
if(!dwarf_offdie(dbg, cu_offset + header_size, &cu_die))
|
||||
{
|
||||
cu_offset = next_offset;
|
||||
continue;
|
||||
}
|
||||
|
||||
bool cu_contains_addr = false;
|
||||
|
||||
// Try to get low_pc and high_pc from CU
|
||||
// If no simple range, check if any child DIE contains this address
|
||||
Dwarf_Addr low_pc{};
|
||||
Dwarf_Addr high_pc{};
|
||||
if(dwarf_lowpc(&cu_die, &low_pc) == 0 && dwarf_highpc(&cu_die, &high_pc) == 0)
|
||||
cu_contains_addr = (addr >= low_pc && addr < high_pc);
|
||||
else
|
||||
cu_contains_addr = checkDIEContainsAddress(&cu_die, addr);
|
||||
|
||||
if(cu_contains_addr)
|
||||
{
|
||||
traverseAllDIEs(&cu_die, addr, call_stack);
|
||||
break;
|
||||
}
|
||||
|
||||
cu_offset = next_offset;
|
||||
}
|
||||
|
||||
// Reverse the call stack to show from caller to callee
|
||||
std::reverse(call_stack.begin(), call_stack.end());
|
||||
|
||||
return call_stack;
|
||||
}
|
||||
|
||||
inline bool
|
||||
CodeobjDecoderComponent::checkDIEContainsAddress(Dwarf_Die* die, Dwarf_Addr addr)
|
||||
{
|
||||
if(die == nullptr) return false;
|
||||
// Check current DIE's address range
|
||||
Dwarf_Addr low_pc{};
|
||||
Dwarf_Addr high_pc{};
|
||||
if(dwarf_lowpc(die, &low_pc) == 0 && dwarf_highpc(die, &high_pc) == 0)
|
||||
{
|
||||
if(addr >= low_pc && addr < high_pc) return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Check ranges attribute for non-contiguous ranges
|
||||
Dwarf_Addr base{};
|
||||
ptrdiff_t offset = 0;
|
||||
while((offset = dwarf_ranges(die, offset, &base, &low_pc, &high_pc)) > 0)
|
||||
if(addr >= low_pc && addr < high_pc) return true;
|
||||
}
|
||||
|
||||
// Check children recursively
|
||||
Dwarf_Die child{};
|
||||
if(dwarf_child(die, &child) == 0)
|
||||
if(checkDIEContainsAddress(&child, addr)) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
inline void
|
||||
CodeobjDecoderComponent::traverseAllDIEs(Dwarf_Die* die,
|
||||
Dwarf_Addr addr,
|
||||
std::vector<std::string>& call_stack)
|
||||
{
|
||||
if(die == nullptr) return;
|
||||
// Check current DIE for inlined function information
|
||||
checkDIEForInlinedFunction(die, addr, call_stack);
|
||||
|
||||
// Traverse children recursively (depth-first)
|
||||
Dwarf_Die child{};
|
||||
if(dwarf_child(die, &child) == 0)
|
||||
{
|
||||
// Check all children AND their siblings at this level
|
||||
// This is crucial because inlined functions can appear as siblings
|
||||
// when multiple functions are inlined at the same scope level
|
||||
do
|
||||
{
|
||||
traverseAllDIEs(&child, addr, call_stack);
|
||||
} while(dwarf_siblingof(&child, &child) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
inline void
|
||||
CodeobjDecoderComponent::checkDIEForInlinedFunction(Dwarf_Die* die,
|
||||
Dwarf_Addr addr,
|
||||
std::vector<std::string>& call_stack)
|
||||
{
|
||||
// Only process inlined subroutines - these are functions that were
|
||||
// expanded inline at compile time and have call site information
|
||||
if(die == nullptr || dwarf_tag(die) != DW_TAG_inlined_subroutine) return;
|
||||
|
||||
Dwarf_Addr low_pc{};
|
||||
Dwarf_Addr high_pc{};
|
||||
bool has_range{false};
|
||||
|
||||
// Check if this inlined subroutine covers the target address
|
||||
// First try simple contiguous range (low_pc to high_pc)
|
||||
|
||||
if(dwarf_lowpc(die, &low_pc) == 0 && dwarf_highpc(die, &high_pc) == 0)
|
||||
{
|
||||
// Simple contiguous range - check if address falls within
|
||||
has_range = (addr >= low_pc && addr < high_pc);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Function may have non-contiguous ranges (optimized code)
|
||||
// Check all address ranges associated with this DIE
|
||||
Dwarf_Addr base{};
|
||||
ptrdiff_t offset{};
|
||||
|
||||
while((offset = dwarf_ranges(die, offset, &base, &low_pc, &high_pc)) > 0)
|
||||
{
|
||||
if(addr >= low_pc && addr < high_pc)
|
||||
{
|
||||
has_range = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If address doesn't fall within this inlined function, skip it
|
||||
if(!has_range) return;
|
||||
|
||||
// Extract call site information - where this function was inlined
|
||||
Dwarf_Attribute call_file_attr{};
|
||||
Dwarf_Attribute call_line_attr{};
|
||||
Dwarf_Word call_file{};
|
||||
Dwarf_Word call_line{};
|
||||
|
||||
// Get the file and line number where this function was called/inlined
|
||||
|
||||
if(!dwarf_attr(die, DW_AT_call_file, &call_file_attr) ||
|
||||
!dwarf_attr(die, DW_AT_call_line, &call_line_attr) ||
|
||||
dwarf_formudata(&call_file_attr, &call_file) != 0 ||
|
||||
dwarf_formudata(&call_line_attr, &call_line) != 0)
|
||||
return; // No call site information available
|
||||
|
||||
// Get the compilation unit to resolve file names
|
||||
Dwarf_Die cu_die{};
|
||||
if(!dwarf_diecu(die, &cu_die, nullptr, nullptr)) return;
|
||||
|
||||
// Get the source files table for this compilation unit
|
||||
Dwarf_Files* files{};
|
||||
size_t nfiles{};
|
||||
if(dwarf_getsrcfiles(&cu_die, &files, &nfiles) == 0 && call_file < nfiles)
|
||||
if(const char* filename = dwarf_filesrc(files, call_file, nullptr, nullptr))
|
||||
// Add "filename:line" to call stack showing where this function was inlined
|
||||
call_stack.push_back(std::string(filename) + ":" + std::to_string(call_line));
|
||||
}
|
||||
|
||||
} // namespace disassembly
|
||||
} // namespace codeobj
|
||||
} // namespace sdk
|
||||
|
||||
@@ -151,13 +151,24 @@ CodeFile::~CodeFile()
|
||||
|
||||
jcode.push_back(nlohmann::json::parse(code.str()));
|
||||
|
||||
size_t lineref = isa.code_line->comment.rfind(':');
|
||||
if(lineref == 0 || lineref == std::string::npos) continue;
|
||||
auto& comment = isa.code_line->comment;
|
||||
size_t lineref = comment.find(':');
|
||||
size_t previous = 0;
|
||||
|
||||
auto source_ref = isa.code_line->comment.substr(0, lineref);
|
||||
// size() + 2 because we need at least ':' and one number after
|
||||
while(lineref != std::string::npos && lineref < comment.size() + 2)
|
||||
{
|
||||
auto source_ref = comment.substr(previous, lineref - previous);
|
||||
|
||||
if(!source_ref.empty() && snapshots.find(source_ref) == snapshots.end())
|
||||
snapshots.insert(std::move(source_ref));
|
||||
if(!source_ref.empty() && snapshots.find(source_ref) == snapshots.end())
|
||||
snapshots.insert(std::move(source_ref));
|
||||
|
||||
previous = comment.find(CodeLine::Instruction::separator, lineref);
|
||||
if(previous == std::string::npos) break;
|
||||
|
||||
previous += CodeLine::Instruction::separator.size();
|
||||
lineref = comment.find(':', previous);
|
||||
}
|
||||
}
|
||||
|
||||
nlohmann::json json;
|
||||
|
||||
새 이슈에서 참조
사용자 차단