handlertree.pl
3.52 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
#!/usr/bin/perl -w
#
# handlertree.pl
# ~~~~~~~~~~~~~~
# A tool for post-processing the debug output generated by Asio-based programs
# to print the tree of handlers that resulted in some specified handler ids.
# Programs write this output to the standard error stream when compiled with
# the define `ASIO_ENABLE_HANDLER_TRACKING'.
#
# 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 %target_handlers = ();
my @cached_output = ();
my %outstanding_handlers = ();
my %running_handlers = ();
#-------------------------------------------------------------------------------
# Build the initial list of target handlers from the command line arguments.
sub build_initial_target_handlers()
{
for my $handler (@ARGV)
{
$target_handlers{$handler} = 1;
}
}
#-------------------------------------------------------------------------------
# Parse the debugging output and cache the handler tracking lines.
sub parse_debug_output()
{
while (my $line = <STDIN>)
{
chomp($line);
if ($line =~ /\@asio\|([^|]*)\|([^|]*)\|(.*)$/)
{
push(@cached_output, $line);
}
}
}
#-------------------------------------------------------------------------------
# Iterate over the cached output in revese and build a hash of all target
# handlers' ancestors.
sub build_target_handler_tree()
{
my $i = scalar(@cached_output) - 1;
while ($i >= 0)
{
my $line = $cached_output[$i];
if ($line =~ /\@asio\|([^|]*)\|([^|]*)\|(.*)$/)
{
my $action = $2;
# Handler creation.
if ($action =~ /^([0-9]+)\*([0-9]+)$/)
{
if ($1 ne "0" and exists($target_handlers{$2}))
{
$target_handlers{$1} = 1;
}
}
}
--$i;
}
}
#-------------------------------------------------------------------------------
# Print out all handler tracking records associated with the target handlers.
sub print_target_handler_records()
{
for my $line (@cached_output)
{
if ($line =~ /\@asio\|([^|]*)\|([^|]*)\|(.*)$/)
{
my $action = $2;
# Handler location.
if ($action =~ /^([0-9]+)\^([0-9]+)$/)
{
print("$line\n") if ($1 eq "0" or exists($target_handlers{$1})) and exists($target_handlers{$2});
}
# Handler creation.
if ($action =~ /^([0-9]+)\*([0-9]+)$/)
{
print("$1, $2, $line\n") if ($1 eq "0" or exists($target_handlers{$1})) and exists($target_handlers{$2});
}
# Begin handler invocation.
elsif ($action =~ /^>([0-9]+)$/)
{
print("$line\n") if (exists($target_handlers{$1}));
}
# End handler invocation.
elsif ($action =~ /^<([0-9]+)$/)
{
print("$line\n") if (exists($target_handlers{$1}));
}
# Handler threw exception.
elsif ($action =~ /^!([0-9]+)$/)
{
print("$line\n") if (exists($target_handlers{$1}));
}
# Handler was destroyed without being invoked.
elsif ($action =~ /^~([0-9]+)$/)
{
print("$line\n") if (exists($target_handlers{$1}));
}
# Operation associated with a handler.
elsif ($action =~ /^\.([0-9]+)$/)
{
print("$line\n") if (exists($target_handlers{$1}));
}
}
}
}
#-------------------------------------------------------------------------------
build_initial_target_handlers();
parse_debug_output();
build_target_handler_tree();
print_target_handler_records();