handlerviz.pl
7.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/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 `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-2020 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=<<table border="0" cellspacing="0">
<tr><td align="left" bgcolor="gray" border="0">%label%</td></tr>
EOF
my $node_footer = <<"EOF";
</table>>
]
EOF
my $node_content = <<"EOF";
<tr><td align="left" bgcolor="white" border="0">
<font face="mono" point-size="9">%content%</font>
</td></tr>
EOF
my $anon_nodes_header = <<"EOF";
{
node [ shape="record" ];
EOF
my $anon_nodes_footer = <<"EOF";
}
EOF
my $anon_node = <<"EOF";
"%name%" [ label="%label%", color="gray" ];
EOF
my $pending_nodes_header = <<"EOF";
{
node [ shape="record", color="red" ];
rank = "max";
EOF
my $pending_nodes_footer = <<"EOF";
}
EOF
my $pending_node = <<"EOF";
"%name%";
EOF
my $edges_header = <<"EOF";
{
edge [ style="dashed", arrowhead="open" ];
EOF
my $edges_footer = <<"EOF";
}
EOF
my $edge = <<"EOF";
"%begin%" -> "%end%" [ label="%label%", labeltooltip="%tooltip%" ]
EOF
my $node_order_header = <<"EOF";
{
node [ style="invisible" ];
edge [ style="invis", weight="100" ];
EOF
my $node_order_footer = <<"EOF";
}
EOF
my $node_order = <<"EOF";
{
rank="same"
"%begin%";
"o%begin%";
}
"o%begin%" -> "o%end%";
EOF
#-------------------------------------------------------------------------------
# Generate dot output from the nodes and edges.
sub print_nodes()
{
foreach my $name (sort keys %nodes)
{
my $node = $nodes{$name};
my $entry = $node->{entry};
my $exit = $node->{exit};
my $label = escape($node->{label});
my $header = $node_header;
$header =~ s/%name%/$name/g;
$header =~ s/%label%/$label/g;
print($header);
if (defined($exit) and defined($entry))
{
my $line = $node_content;
my $content = $entry . " + " . sprintf("%.6f", $exit - $entry) . "s";
$line =~ s/%content%/$content/g;
print($line);
}
foreach my $content (@{$node->{content}})
{
$content = escape($content);
$content = " " if length($content) == 0;
my $line = $node_content;
$line =~ s/%content%/$content/g;
print($line);
}
print($node_footer);
}
}
sub print_anon_nodes()
{
print($anon_nodes_header);
foreach my $name (sort keys %anon_nodes)
{
my $label = $anon_nodes{$name};
my $line = $anon_node;
$line =~ s/%name%/$name/g;
$line =~ s/%label%/$label/g;
print($line);
}
print($anon_nodes_footer);
}
sub print_pending_nodes()
{
print($pending_nodes_header);
foreach my $name (sort keys %pending_nodes)
{
my $line = $pending_node;
$line =~ s/%name%/$name/g;
print($line);
}
print($pending_nodes_footer);
}
sub print_edges()
{
print($edges_header);
foreach my $e (@edges)
{
my $begin = $e->{begin};
my $end = $e->{end};
my $label = $e->{label};
my $tooltip = "";
if (exists($locations{($begin,$end)}))
{
for my $line (@{$locations{($begin,$end)}})
{
$tooltip = $tooltip . escape($line) . "\n";
}
}
my $line = $edge;
$line =~ s/%begin%/$begin/g;
$line =~ s/%end%/$end/g;
$line =~ s/%label%/$label/g;
$line =~ s/%tooltip%/$tooltip/g;
print($line);
}
print($edges_footer);
}
sub print_node_order()
{
my $prev = "";
print($node_order_header);
foreach my $name (sort keys %all_nodes)
{
if ($prev ne "")
{
my $begin = $prev;
my $end = $all_nodes{$name};
my $line = $node_order;
$line =~ s/%begin%/$begin/g;
$line =~ s/%end%/$end/g;
print($line);
}
$prev = $all_nodes{$name};
}
foreach my $name (sort keys %pending_nodes)
{
if ($prev ne "")
{
my $begin = $prev;
my $line = $node_order;
$line =~ s/%begin%/$begin/g;
$line =~ s/%end%/$name/g;
print($line);
}
last;
}
print($node_order_footer);
}
sub generate_dot()
{
print($graph_header);
print_nodes();
print_anon_nodes();
print_pending_nodes();
print_edges();
print_node_order();
print($graph_footer);
}
#-------------------------------------------------------------------------------
parse_debug_output();
generate_dot();