tracy/server/TracySourceView.cpp

602 lines
18 KiB
C++
Raw Normal View History

#include <inttypes.h>
#include <stdio.h>
2020-03-25 21:37:34 +00:00
#include <capstone/capstone.h>
#include "../imgui/imgui.h"
#include "TracyColor.hpp"
2020-03-28 14:06:36 +00:00
#include "TracyFilesystem.hpp"
#include "TracyImGui.hpp"
#include "TracyPrint.hpp"
#include "TracySourceView.hpp"
#include "TracyWorker.hpp"
#include "IconsFontAwesome5.h"
2020-03-25 21:53:05 +00:00
namespace tracy
{
SourceView::SourceView( ImFont* font )
: m_font( font )
, m_file( nullptr )
2020-03-24 23:07:31 +00:00
, m_symAddr( 0 )
2020-03-28 00:22:27 +00:00
, m_currentAddr( 0 )
2020-03-27 22:57:23 +00:00
, m_targetAddr( 0 )
, m_data( nullptr )
, m_dataSize( 0 )
, m_targetLine( 0 )
, m_selectedLine( 0 )
2020-03-25 21:53:05 +00:00
, m_showAsm( false )
, m_codeLen( 0 )
2020-03-28 00:22:27 +00:00
, m_highlightAddr( 0 )
, m_asmRelative( false )
2020-04-01 23:37:56 +00:00
, m_asmShowSourceLocation( true )
{
}
SourceView::~SourceView()
{
delete[] m_data;
}
void SourceView::Open( const char* fileName, int line, uint64_t baseAddr, uint64_t symAddr, const Worker& worker )
{
m_targetLine = line;
m_selectedLine = line;
2020-03-25 21:53:05 +00:00
m_targetAddr = symAddr;
m_baseAddr = baseAddr;
2020-03-24 23:07:31 +00:00
m_symAddr = symAddr;
2020-03-28 00:22:27 +00:00
m_currentAddr = symAddr;
if( m_file != fileName )
{
m_file = fileName;
m_lines.clear();
if( fileName )
{
FILE* f = fopen( fileName, "rb" );
fseek( f, 0, SEEK_END );
const auto sz = ftell( f );
fseek( f, 0, SEEK_SET );
if( sz > m_dataSize )
{
delete[] m_data;
m_data = new char[sz+1];
m_dataSize = sz;
}
fread( m_data, 1, sz, f );
m_data[sz] = '\0';
fclose( f );
auto txt = m_data;
for(;;)
{
auto end = txt;
while( *end != '\n' && *end != '\r' && end - m_data < sz ) end++;
m_lines.emplace_back( Line { txt, end } );
if( *end == '\n' )
{
end++;
if( *end == '\r' ) end++;
}
else if( *end == '\r' )
{
end++;
if( *end == '\n' ) end++;
}
if( *end == '\0' ) break;
txt = end;
}
}
}
if( m_lines.empty() ) m_showAsm = true;
if( !Disassemble( baseAddr, worker ) ) m_showAsm = false;
assert( m_showAsm || !m_lines.empty() );
2020-03-25 21:53:05 +00:00
}
2020-03-25 21:37:34 +00:00
2020-03-25 21:53:05 +00:00
bool SourceView::Disassemble( uint64_t symAddr, const Worker& worker )
{
2020-03-25 21:37:34 +00:00
m_asm.clear();
2020-03-25 21:53:05 +00:00
if( symAddr == 0 ) return false;
2020-03-25 21:37:34 +00:00
const auto arch = worker.GetCpuArch();
2020-03-25 21:53:05 +00:00
if( arch == CpuArchUnknown ) return false;
2020-03-25 21:37:34 +00:00
uint32_t len;
auto code = worker.GetSymbolCode( symAddr, len );
2020-03-25 21:53:05 +00:00
if( !code ) return false;
2020-03-25 21:37:34 +00:00
csh handle;
2020-03-27 22:58:49 +00:00
cs_err rval = CS_ERR_ARCH;
2020-03-25 21:37:34 +00:00
switch( arch )
{
case CpuArchX86:
rval = cs_open( CS_ARCH_X86, CS_MODE_32, &handle );
break;
case CpuArchX64:
rval = cs_open( CS_ARCH_X86, CS_MODE_64, &handle );
break;
case CpuArchArm32:
rval = cs_open( CS_ARCH_ARM, CS_MODE_ARM, &handle );
break;
case CpuArchArm64:
rval = cs_open( CS_ARCH_ARM64, CS_MODE_ARM, &handle );
break;
default:
assert( false );
break;
}
2020-03-25 21:53:05 +00:00
if( rval != CS_ERR_OK ) return false;
2020-03-27 23:53:48 +00:00
cs_option( handle, CS_OPT_DETAIL, CS_OPT_ON );
2020-03-25 21:37:34 +00:00
cs_insn* insn;
size_t cnt = cs_disasm( handle, (const uint8_t*)code, len, symAddr, 0, &insn );
if( cnt > 0 )
{
m_asm.reserve( cnt );
for( size_t i=0; i<cnt; i++ )
{
2020-03-27 23:53:48 +00:00
const auto& op = insn[i];
const auto& detail = *op.detail;
bool hasJump = false;
2020-03-28 13:00:21 +00:00
for( auto j=0; j<detail.groups_count; j++ )
2020-03-27 23:53:48 +00:00
{
2020-03-28 13:00:21 +00:00
if( detail.groups[j] == CS_GRP_JUMP || detail.groups[j] == CS_GRP_CALL )
2020-03-27 23:53:48 +00:00
{
2020-03-28 13:00:21 +00:00
hasJump = true;
break;
2020-03-27 23:53:48 +00:00
}
}
uint64_t jumpAddr = 0;
if( hasJump )
{
switch( arch )
{
case CpuArchX86:
case CpuArchX64:
if( detail.x86.op_count == 1 && detail.x86.operands[0].type == X86_OP_IMM )
{
jumpAddr = (uint64_t)detail.x86.operands[0].imm;
}
break;
case CpuArchArm32:
if( detail.arm.op_count == 1 && detail.arm.operands[0].type == ARM_OP_IMM )
{
jumpAddr = (uint64_t)detail.arm.operands[0].imm;
}
break;
case CpuArchArm64:
if( detail.arm64.op_count == 1 && detail.arm64.operands[0].type == ARM64_OP_IMM )
{
jumpAddr = (uint64_t)detail.arm64.operands[0].imm;
}
break;
default:
assert( false );
break;
}
}
m_asm.emplace_back( AsmLine { op.address, jumpAddr, op.mnemonic, op.op_str } );
2020-03-25 21:37:34 +00:00
}
cs_free( insn, cnt );
}
cs_close( &handle );
m_codeLen = len;
2020-03-25 21:53:05 +00:00
return true;
}
void SourceView::Render( const Worker& worker )
{
2020-03-28 00:22:27 +00:00
m_highlightAddr.Decay( 0 );
if( m_file ) TextFocused( "File:", m_file );
if( !m_asm.empty() && !m_lines.empty() )
{
if( SmallCheckbox( ICON_FA_MICROCHIP " Show assembly", &m_showAsm ) )
{
if( m_showAsm )
{
m_targetAddr = m_symAddr;
}
else
{
m_targetLine = m_selectedLine;
2020-03-25 21:53:05 +00:00
}
}
}
if( !m_asm.empty() )
{
if( !m_lines.empty() )
{
ImGui::SameLine();
ImGui::Spacing();
ImGui::SameLine();
}
TextFocused( "Code size:", MemSizeToString( m_codeLen ) );
}
uint32_t iptotal = 0;
unordered_flat_map<uint64_t, uint32_t> ipcount;
auto ipmap = m_symAddr != 0 ? worker.GetSymbolInstructionPointers( m_symAddr ) : nullptr;
if( ipmap )
{
2020-03-25 21:53:05 +00:00
if( m_showAsm )
{
for( auto& ip : *ipmap )
{
auto addr = worker.GetCanonicalPointer( ip.first );
assert( ipcount.find( addr ) == ipcount.end() );
ipcount.emplace( addr, ip.second );
iptotal += ip.second;
}
}
else
{
for( auto& ip : *ipmap )
{
auto frame = worker.GetCallstackFrame( ip.first );
if( frame )
{
auto ffn = worker.GetString( frame->data[0].file );
if( strcmp( ffn, m_file ) == 0 )
{
2020-03-25 21:53:05 +00:00
const auto line = frame->data[0].line;
auto it = ipcount.find( line );
if( it == ipcount.end() )
{
ipcount.emplace( line, ip.second );
}
else
{
it->second += ip.second;
}
iptotal += ip.second;
}
}
}
}
if( iptotal > 0 )
{
ImGui::SameLine();
2020-03-25 21:53:05 +00:00
ImGui::Spacing();
ImGui::SameLine();
TextFocused( "Samples:", RealToString( iptotal ) );
}
}
auto sym = worker.GetSymbolData( m_symAddr );
if( sym )
{
ImGui::SameLine();
ImGui::Spacing();
ImGui::SameLine();
TextFocused( "Symbol:", worker.GetString( sym->name ) );
if( sym->isInline )
{
auto parent = worker.GetSymbolData( m_baseAddr );
if( parent )
{
ImGui::SameLine();
ImGui::TextDisabled( "(%s)", worker.GetString( parent->name ) );
}
}
}
if( !m_showAsm )
{
TextColoredUnformatted( ImVec4( 1.f, 1.f, 0.2f, 1.f ), ICON_FA_EXCLAMATION_TRIANGLE );
ImGui::SameLine();
TextColoredUnformatted( ImVec4( 1.f, 0.3f, 0.3f, 1.f ), "The source file contents might not reflect the actual profiled code!" );
ImGui::SameLine();
TextColoredUnformatted( ImVec4( 1.f, 1.f, 0.2f, 1.f ), ICON_FA_EXCLAMATION_TRIANGLE );
}
else
{
SmallCheckbox( ICON_FA_SEARCH_LOCATION " Relative locations", &m_asmRelative );
2020-04-01 23:37:56 +00:00
ImGui::SameLine();
ImGui::Spacing();
ImGui::SameLine();
SmallCheckbox( ICON_FA_FILE_IMPORT " Show source locations", &m_asmShowSourceLocation );
}
2020-03-28 13:27:29 +00:00
uint64_t jumpOut = 0;
ImGui::BeginChild( "##sourceView", ImVec2( 0, 0 ), true );
if( m_font ) ImGui::PushFont( m_font );
2020-03-25 21:53:05 +00:00
if( m_showAsm )
{
2020-03-25 21:53:05 +00:00
if( m_targetAddr != 0 )
{
2020-03-25 21:53:05 +00:00
for( auto& line : m_asm )
2020-03-24 17:10:56 +00:00
{
2020-03-25 21:53:05 +00:00
if( m_targetAddr == line.addr )
{
m_targetAddr = 0;
ImGui::SetScrollHereY();
}
2020-03-28 13:27:29 +00:00
RenderAsmLine( line, 0, iptotal, worker, jumpOut );
2020-03-25 21:53:05 +00:00
}
}
else
{
ImGuiListClipper clipper( (int)m_asm.size() );
while( clipper.Step() )
{
if( iptotal == 0 )
{
for( auto i=clipper.DisplayStart; i<clipper.DisplayEnd; i++ )
{
2020-03-28 13:27:29 +00:00
RenderAsmLine( m_asm[i], 0, 0, worker, jumpOut );
2020-03-25 21:53:05 +00:00
}
}
else
{
for( auto i=clipper.DisplayStart; i<clipper.DisplayEnd; i++ )
{
auto& line = m_asm[i];
auto it = ipcount.find( line.addr );
const auto ipcnt = it == ipcount.end() ? 0 : it->second;
2020-03-28 13:27:29 +00:00
RenderAsmLine( line, ipcnt, iptotal, worker, jumpOut );
2020-03-25 21:53:05 +00:00
}
}
2020-03-24 17:10:56 +00:00
}
}
}
else
{
2020-03-25 21:53:05 +00:00
if( m_targetLine != 0 )
2020-03-24 17:10:56 +00:00
{
2020-03-25 21:53:05 +00:00
int lineNum = 1;
for( auto& line : m_lines )
2020-03-24 17:10:56 +00:00
{
2020-03-25 21:53:05 +00:00
if( m_targetLine == lineNum )
{
2020-03-25 21:53:05 +00:00
m_targetLine = 0;
ImGui::SetScrollHereY();
}
2020-03-25 21:53:05 +00:00
RenderLine( line, lineNum++, 0, iptotal );
}
2020-03-25 21:53:05 +00:00
}
else
{
ImGuiListClipper clipper( (int)m_lines.size() );
while( clipper.Step() )
{
2020-03-25 21:53:05 +00:00
if( iptotal == 0 )
{
for( auto i=clipper.DisplayStart; i<clipper.DisplayEnd; i++ )
{
RenderLine( m_lines[i], i+1, 0, 0 );
}
}
else
{
2020-03-25 21:53:05 +00:00
for( auto i=clipper.DisplayStart; i<clipper.DisplayEnd; i++ )
{
auto it = ipcount.find( i+1 );
const auto ipcnt = it == ipcount.end() ? 0 : it->second;
RenderLine( m_lines[i], i+1, ipcnt, iptotal );
}
}
2020-03-24 17:10:56 +00:00
}
}
}
if( m_font ) ImGui::PopFont();
ImGui::EndChild();
2020-03-28 13:27:29 +00:00
if( jumpOut != 0 )
{
auto sym = worker.GetSymbolData( jumpOut );
if( sym )
{
2020-03-28 14:06:36 +00:00
auto line = sym->line;
auto file = line == 0 ? nullptr : worker.GetString( sym->file );
if( file && !SourceFileValid( file, worker.GetCaptureTime() ) )
{
file = nullptr;
line = 0;
}
if( line > 0 || sym->size.Val() > 0 )
{
Open( file, line, jumpOut, jumpOut, worker );
}
2020-03-28 13:27:29 +00:00
}
}
}
2020-03-30 20:26:45 +00:00
static void PrintPercentage( float val )
{
const auto ty = ImGui::GetFontSize();
auto draw = ImGui::GetWindowDrawList();
const auto wpos = ImGui::GetCursorScreenPos();
const auto stw = ImGui::CalcTextSize( " " ).x;
const auto htw = stw / 2;
const auto tw = stw * 8;
2020-03-30 20:26:45 +00:00
char tmp[16];
auto end = PrintFloat( tmp, tmp+16, val, 2 );
memcpy( end, "%", 2 );
end++;
const auto sz = end - tmp;
char buf[16];
memset( buf, ' ', 7-sz );
memcpy( buf + 7 - sz, tmp, sz+1 );
draw->AddRectFilled( wpos, wpos + ImVec2( val * tw / 100, ty+1 ), 0xFF444444 );
DrawTextContrast( draw, wpos + ImVec2( htw, 0 ), 0xFFFFFFFF, buf );
ImGui::ItemSize( ImVec2( stw * 7, ty ), 0 );
2020-03-30 20:26:45 +00:00
}
void SourceView::RenderLine( const Line& line, int lineNum, uint32_t ipcnt, uint32_t iptotal )
{
const auto ty = ImGui::GetFontSize();
auto draw = ImGui::GetWindowDrawList();
const auto w = ImGui::GetWindowWidth();
const auto wpos = ImGui::GetCursorScreenPos();
if( lineNum == m_selectedLine )
{
draw->AddRectFilled( wpos, wpos + ImVec2( w, ty+1 ), 0xFF333322 );
}
if( iptotal != 0 )
{
if( ipcnt == 0 )
{
ImGui::TextUnformatted( " " );
}
else
{
2020-03-30 20:26:45 +00:00
PrintPercentage( 100.f * ipcnt / iptotal );
}
ImGui::SameLine( 0, ty );
}
const auto lineString = RealToString( lineNum );
const auto linesz = strlen( lineString );
char buf[16];
memset( buf, ' ', 7 - linesz );
memcpy( buf + 7 - linesz, lineString, linesz+1 );
TextDisabledUnformatted( buf );
ImGui::SameLine( 0, ty );
ImGui::TextUnformatted( line.begin, line.end );
draw->AddLine( wpos + ImVec2( 0, ty+2 ), wpos + ImVec2( w, ty+2 ), 0x08FFFFFF );
}
2020-03-28 13:27:29 +00:00
void SourceView::RenderAsmLine( const AsmLine& line, uint32_t ipcnt, uint32_t iptotal, const Worker& worker, uint64_t& jumpOut )
2020-03-25 21:53:05 +00:00
{
const auto ty = ImGui::GetFontSize();
auto draw = ImGui::GetWindowDrawList();
const auto w = ImGui::GetWindowWidth();
const auto wpos = ImGui::GetCursorScreenPos();
2020-03-28 00:22:27 +00:00
if( line.addr == m_currentAddr )
2020-03-25 21:53:05 +00:00
{
draw->AddRectFilled( wpos, wpos + ImVec2( w, ty+1 ), 0xFF333322 );
}
2020-03-28 00:22:27 +00:00
if( line.addr == m_highlightAddr )
{
draw->AddRectFilled( wpos, wpos + ImVec2( w, ty+1 ), 0xFF222233 );
}
2020-03-25 21:53:05 +00:00
if( iptotal != 0 )
{
if( ipcnt == 0 )
{
ImGui::TextUnformatted( " " );
}
else
{
2020-03-30 20:26:45 +00:00
PrintPercentage( 100.f * ipcnt / iptotal );
2020-03-25 21:53:05 +00:00
}
ImGui::SameLine( 0, ty );
}
char buf[256];
if( m_asmRelative )
{
sprintf( buf, "+%" PRIu64, line.addr - m_baseAddr );
}
else
{
sprintf( buf, "%" PRIx64, line.addr );
}
2020-03-25 21:53:05 +00:00
const auto asz = strlen( buf );
memset( buf+asz, ' ', 16-asz );
buf[16] = '\0';
TextDisabledUnformatted( buf );
2020-04-01 23:37:56 +00:00
if( m_asmShowSourceLocation )
{
2020-04-01 23:37:56 +00:00
const auto stw = ImGui::CalcTextSize( " " ).x;
ImGui::SameLine();
2020-04-01 23:37:56 +00:00
uint32_t srcline;
const auto srcidx = worker.GetLocationForAddress( line.addr, srcline );
if( srcline != 0 )
{
2020-04-01 23:37:56 +00:00
const auto fileName = worker.GetString( srcidx );
const auto fileColor = GetHsvColor( srcidx.Idx(), 0 );
SmallColorBox( fileColor );
ImGui::SameLine();
const auto lineString = RealToString( srcline );
const auto linesz = strlen( lineString );
char buf[32];
const auto fnsz = strlen( fileName );
if( fnsz < 32 - 8 )
{
sprintf( buf, "%s:%i", fileName, srcline );
}
else
{
sprintf( buf, "...%s:%i", fileName+fnsz-(32-3-1-8), srcline );
}
const auto bufsz = strlen( buf );
TextDisabledUnformatted( buf );
if( ImGui::IsItemHovered() )
{
if( m_font ) ImGui::PopFont();
ImGui::BeginTooltip();
ImGui::Text( "%s:%i", fileName, srcline );
ImGui::EndTooltip();
if( m_font ) ImGui::PushFont( m_font );
if( !m_lines.empty() && m_file == fileName && ImGui::IsItemClicked() )
{
m_currentAddr = line.addr;
m_targetLine = srcline;
m_selectedLine = srcline;
m_showAsm = false;
}
}
ImGui::SameLine( 0, 0 );
ImGui::ItemSize( ImVec2( stw * ( 32 - bufsz ), ty ), 0 );
}
else
{
2020-04-01 23:37:56 +00:00
SmallColorBox( 0 );
ImGui::SameLine( 0, 0 );
ImGui::ItemSize( ImVec2( stw * 32, ty ), 0 );
}
}
2020-03-25 21:53:05 +00:00
ImGui::SameLine( 0, ty );
const auto msz = line.mnemonic.size();
memcpy( buf, line.mnemonic.c_str(), msz );
memset( buf+msz, ' ', 16-msz );
memcpy( buf+16, line.operands.c_str(), line.operands.size() + 1 );
ImGui::TextUnformatted( buf );
2020-03-28 00:22:27 +00:00
if( line.jumpAddr != 0 )
{
uint32_t offset = 0;
const auto base = worker.GetSymbolForAddress( line.jumpAddr, offset );
auto sym = base == 0 ? worker.GetSymbolData( line.jumpAddr ) : worker.GetSymbolData( base );
if( sym )
{
ImGui::SameLine();
ImGui::Spacing();
ImGui::SameLine();
if( base == m_baseAddr )
{
ImGui::TextDisabled( "-> [%s+%" PRIu32"]", worker.GetString( sym->name ), offset );
if( ImGui::IsItemHovered() )
{
m_highlightAddr = line.jumpAddr;
if( ImGui::IsItemClicked() )
{
m_targetAddr = line.jumpAddr;
m_currentAddr = line.jumpAddr;
}
}
}
else
{
ImGui::TextDisabled( "[%s+%" PRIu32"]", worker.GetString( sym->name ), offset );
2020-03-28 13:27:29 +00:00
if( ImGui::IsItemClicked() ) jumpOut = line.jumpAddr;
2020-03-28 00:22:27 +00:00
}
}
}
2020-03-25 21:53:05 +00:00
draw->AddLine( wpos + ImVec2( 0, ty+2 ), wpos + ImVec2( w, ty+2 ), 0x08FFFFFF );
}
}