#!/usr/bin/perl -w # # handlerviz.pl # ~~~~~~~~~~~~~ # # A visualisation tool for post-processing the debug output generated by # Asio-based programs. Programs write this output to the standard error stream # when compiled with the define `BOOST_ASIO_ENABLE_HANDLER_TRACKING'. # # This tool generates output intended for use with the GraphViz tool `dot'. For # example, to convert output to a PNG image, use: # # perl handlerviz.pl < output.txt | dot -Tpng > output.png # # To convert to a PDF file, use: # # perl handlerviz.pl < output.txt | dot -Tpdf > output.pdf # # Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com) # # Distributed under the Boost Software License, Version 1.0. (See accompanying # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) # use strict; my %nodes = (); my @edges = (); my %locations = (); my %anon_nodes = (); my $anon_id = 0; my %all_nodes = (); my %pending_nodes = (); #------------------------------------------------------------------------------- # Parse the debugging output and populate the nodes and edges. sub parse_debug_output() { while (my $line = <>) { chomp($line); if ($line =~ /\@asio\|([^|]*)\|([^|]*)\|(.*)$/) { my $timestamp = $1; my $action = $2; my $description = $3; # Handler creation. if ($action =~ /^([0-9]+)\*([0-9]+)$/) { my $begin = $1; my $end = $2; my $label = $description; $label =~ s/\./\\n/g; if ($begin eq "0") { $begin = "a" . $anon_id++; $anon_nodes{$begin} = $timestamp; $all_nodes{"$timestamp-$begin"} = $begin; } my %edge = ( begin=>$begin, end=>$end, label=>$label ); push(@edges, \%edge); $pending_nodes{$end} = 1; } # Handler location. elsif ($action =~ /^([0-9]+)\^([0-9]+)$/) { if ($1 ne "0") { if (not exists($locations{($1,$2)})) { $locations{($1,$2)} = (); } push(@{$locations{($1,$2)}}, $description); } } # Begin handler invocation. elsif ($action =~ /^>([0-9]+)$/) { my %new_node = ( label=>$description, entry=>$timestamp ); $new_node{content} = (); $nodes{$1} = \%new_node; $all_nodes{"$timestamp-$1"} = $1; delete($pending_nodes{$1}); } # End handler invocation. elsif ($action =~ /^<([0-9]+)$/) { $nodes{$1}->{exit} = $timestamp; } # Handler threw exception. elsif ($action =~ /^!([0-9]+)$/) { push(@{$nodes{$1}->{content}}, "exception"); } # Handler was destroyed without being invoked. elsif ($action =~ /^~([0-9]+)$/) { my %new_node = ( label=>"$timestamp destroyed" ); $new_node{content} = (); $nodes{$1} = \%new_node; $all_nodes{"$timestamp-$1"} = $1; delete($pending_nodes{$1}); } # Handler performed some operation. elsif ($action =~ /^([0-9]+)$/) { if ($1 eq "0") { my $id = "a" . $anon_id++; $anon_nodes{$id} = "$timestamp\\l$description"; $all_nodes{"$timestamp-$id"} = $id; } else { push(@{$nodes{$1}->{content}}, "$description"); } } } } } #------------------------------------------------------------------------------- # Helper function to convert a string to escaped HTML text. sub escape($) { my $text = shift; $text =~ s/&/\&\;/g; $text =~ s/\<\;/g; $text =~ s/>/\>\;/g; $text =~ s/\t/ /g; return $text; } #------------------------------------------------------------------------------- # Templates for dot output. my $graph_header = <<"EOF"; /* Generated by handlerviz.pl */ digraph "handlerviz output" { graph [ nodesep="1" ]; node [ shape="box", fontsize="9" ]; edge [ arrowtail="dot", fontsize="9" ]; EOF my $graph_footer = <<"EOF"; } EOF my $node_header = <<"EOF"; "%name%" [ label=<
%label% |