From ae56d79c7e89d74747879825fc0b66c4e3c988d7 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Mon, 11 May 2026 21:11:39 +0200 Subject: [PATCH] Extract disassembly line formatting to a separate function. --- profiler/src/profiler/TracyDisassembly.cpp | 118 +++++++++++++++++++ profiler/src/profiler/TracyDisassembly.hpp | 1 + profiler/src/profiler/TracySourceView.cpp | 126 +-------------------- 3 files changed, 121 insertions(+), 124 deletions(-) diff --git a/profiler/src/profiler/TracyDisassembly.cpp b/profiler/src/profiler/TracyDisassembly.cpp index ec398497..d456d787 100644 --- a/profiler/src/profiler/TracyDisassembly.cpp +++ b/profiler/src/profiler/TracyDisassembly.cpp @@ -589,4 +589,122 @@ DisasmData Disassemble( uint64_t symAddr, const Worker& worker ) return data; } +std::string FormatDisassemblyLine( const AsmLine& opcode, Worker& worker, std::vector& sources, uint64_t symAddr, const AddrStatData& as, const unordered_flat_map& locMap ) +{ + std::string line; + + uint32_t srcline; + const auto srcidx = worker.GetLocationForAddress( opcode.addr, srcline ); + if( srcidx.Active() ) + { + size_t idx; + const auto file = worker.GetString( srcidx ); + auto it = std::ranges::find( sources, file ); + if( it == sources.end() ) + { + idx = sources.size(); + sources.emplace_back( file ); + } + else + { + idx = std::distance( sources.begin(), it ); + } + + line = std::to_string( idx ) + ":" + std::to_string( srcline ) + ":"; + } + else + { + line = "::"; + } + + line += "+" + std::to_string( opcode.addr - symAddr ) + ":"; + + const auto totalCost = as.ipTotalAsm.local + as.ipTotalAsm.ext; + if( totalCost != 0 ) + { + char buf[32]; + auto it = as.ipCountAsm.find( opcode.addr ); + if( it != as.ipCountAsm.end() ) + { + auto& stat = it->second; + if( stat.local != 0 ) + { + snprintf( buf, sizeof(buf), "%.4f%%:", 100.0f * stat.local / totalCost ); + line += buf; + } + else + { + line += ":"; + } + if( stat.ext != 0 ) + { + snprintf( buf, sizeof(buf), "%.4f%%", 100.0f * stat.ext / totalCost ); + line += buf; + } + else + { + line += ":"; + } + } + else + { + line += "::"; + } + } + else + { + line += "::"; + } + + line += opcode.mnemonic; + + const char* jumpName = nullptr; + bool hasJump = false; + if( opcode.jumpAddr != 0 ) + { + auto lit = locMap.find( opcode.jumpAddr ); + if( lit != locMap.end() ) + { + line += " .L" + std::to_string( lit->second ); + hasJump = true; + } + else + { + uint32_t jumpOffset; + uint64_t jumpBase = worker.GetSymbolForAddress( opcode.jumpAddr, jumpOffset ); + if( jumpBase && jumpBase != symAddr ) + { + auto jumpSym = worker.GetSymbolData( jumpBase ); + if( jumpSym ) + { + if( worker.HasInlineSymbolAddresses() ) + { + const auto jumpAddr = worker.GetInlineSymbolForAddress( opcode.jumpAddr ); + if( jumpAddr != 0 ) + { + const auto symData = worker.GetSymbolData( jumpAddr ); + if( symData ) jumpName = worker.GetString( symData->name ); + } + } + if( !jumpName ) jumpName = worker.GetString( jumpSym->name ); + } + } + } + } + if( !hasJump && !opcode.operands.empty() ) line += " " + opcode.operands; + + std::string label; + auto it = locMap.find( opcode.addr ); + if( it != locMap.end() ) label = ".L" + std::to_string( it->second ); + + if( jumpName || !label.empty() ) + { + line += ";"; + if( !label.empty() ) line += " label: " + label; + if( jumpName ) line += " destination: " + std::string( jumpName ); + } + + return line; +} + } diff --git a/profiler/src/profiler/TracyDisassembly.hpp b/profiler/src/profiler/TracyDisassembly.hpp index 0b0d7df4..2c24697a 100644 --- a/profiler/src/profiler/TracyDisassembly.hpp +++ b/profiler/src/profiler/TracyDisassembly.hpp @@ -132,6 +132,7 @@ struct AddrStatData }; DisasmData Disassemble( uint64_t symAddr, const Worker& worker ); +std::string FormatDisassemblyLine( const AsmLine& opcode, Worker& worker, std::vector& sources, uint64_t symAddr, const AddrStatData& as, const unordered_flat_map& locMap ); } diff --git a/profiler/src/profiler/TracySourceView.cpp b/profiler/src/profiler/TracySourceView.cpp index 1d779428..52b73b38 100644 --- a/profiler/src/profiler/TracySourceView.cpp +++ b/profiler/src/profiler/TracySourceView.cpp @@ -2019,131 +2019,9 @@ void SourceView::AttachRangeToLlm( size_t start, size_t stop, Worker& worker, Vi std::string code; const auto end = m_asm.size() < stop ? m_asm.size() : stop; - for( size_t i=start; isecond; - if( stat.local != 0 ) - { - snprintf( buf, sizeof(buf), "%.4f%%:", 100.0f * stat.local / totalCost ); - line += buf; - } - else - { - line += ":"; - } - if( stat.ext != 0 ) - { - snprintf( buf, sizeof(buf), "%.4f%%", 100.0f * stat.ext / totalCost ); - line += buf; - } - else - { - line += ":"; - } - } - else - { - line += "::"; - } - } - else - { - line += "::"; - } - - line += v.mnemonic; - - const char* jumpName = nullptr; - bool hasJump = false; - if( v.jumpAddr != 0 ) - { - auto lit = m_locMap.find( v.jumpAddr ); - if( lit != m_locMap.end() ) - { - line += " .L" + std::to_string( lit->second ); - hasJump = true; - } - else - { - uint32_t jumpOffset; - uint64_t jumpBase = worker.GetSymbolForAddress( v.jumpAddr, jumpOffset ); - if( jumpBase && jumpBase != m_baseAddr ) - { - auto jumpSym = worker.GetSymbolData( jumpBase ); - if( jumpSym ) - { - if( worker.HasInlineSymbolAddresses() ) - { - const auto symAddr = worker.GetInlineSymbolForAddress( v.jumpAddr ); - if( symAddr != 0 ) - { - const auto symData = worker.GetSymbolData( symAddr ); - if( symData ) jumpName = worker.GetString( symData->name ); - } - } - if( !jumpName ) jumpName = worker.GetString( jumpSym->name ); - } - } - } - } - if( !hasJump && !v.operands.empty() ) line += " " + v.operands; - - std::string label; - auto it = m_locMap.find( v.addr ); - if( it != m_locMap.end() ) label = ".L" + std::to_string( it->second ); - - if( jumpName || !label.empty() ) - { - line += ";"; - if( !label.empty() ) line += " label: " + label; - if( jumpName ) line += " destination: " + std::string( jumpName ); - } - - code += line + "\n"; - } - + for( size_t i=start; i