forked from aosp-mirror/platform_external_strace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strace-graph
executable file
·336 lines (288 loc) · 8.08 KB
/
strace-graph
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
#!/usr/bin/perl
# This script processes strace -f output. It displays a graph of invoked
# subprocesses, and is useful for finding out what complex commands do.
# You will probably want to invoke strace with -q as well, and with
# -s 100 to get complete filenames.
# The script can also handle the output with strace -t, -tt, or -ttt.
# It will add elapsed time for each process in that case.
# This script is Copyright (C) 1998 by Richard Braakman <dark@xs4all.nl>.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
my %unfinished;
# Scales for strace slowdown. Make configurable!
my $scale_factor = 3.5;
while (<>) {
my ($pid, $call, $args, $result, $time);
chop;
s/^(\d+)\s+//;
$pid = $1;
if (s/^(\d\d):(\d\d):(\d\d)(?:\.(\d\d\d\d\d\d))? //) {
$time = $1 * 3600 + $2 * 60 + $3;
if (defined $4) {
$time = $time + $4 / 1000000;
$floatform = 1;
}
} elsif (s/^(\d+)\.(\d\d\d\d\d\d) //) {
$time = $1 + ($2 / 1000000);
$floatform = 1;
}
if (s/ <unfinished ...>$//) {
$unfinished{$pid} = $_;
next;
}
if (s/^<... \S+ resumed> //) {
unless (exists $unfinished{$pid}) {
print STDERR "$0: $ARGV: cannot find start of resumed call on line $.";
next;
}
$_ = $unfinished{$pid} . $_;
delete $unfinished{$pid};
}
if (/^--- SIG(\S+) \(.*\) ---$/) {
# $pid received signal $1
# currently we don't do anything with this
next;
}
if (/^\+\+\+ killed by SIG(\S+) \+\+\+$/) {
# $pid received signal $1
handle_killed($pid, $time);
next;
}
($call, $args, $result) = /(\S+)\((.*)\)\s+= (.*)$/;
unless (defined $result) {
print STDERR "$0: $ARGV: $.: cannot parse line.\n";
next;
}
handle_trace($pid, $call, $args, $result, $time);
}
display_trace();
exit 0;
sub parse_str {
my ($in) = @_;
my $result = "";
while (1) {
if ($in =~ s/^\\(.)//) {
$result .= $1;
} elsif ($in =~ s/^\"//) {
if ($in =~ s/^\.\.\.//) {
return ("$result...", $in);
}
return ($result, $in);
} elsif ($in =~ s/([^\\\"]*)//) {
$result .= $1;
} else {
return (undef, $in);
}
}
}
sub parse_one {
my ($in) = @_;
if ($in =~ s/^\"//) {
($tmp, $in) = parse_str($in);
if (not defined $tmp) {
print STDERR "$0: $ARGV: $.: cannot parse string.\n";
return (undef, $in);
}
return ($tmp, $in);
} elsif ($in =~ s/^0x(\x+)//) {
return (hex $1, $in);
} elsif ($in =~ s/^(\d+)//) {
return (int $1, $in);
} else {
print STDERR "$0: $ARGV: $.: unrecognized element.\n";
return (undef, $in);
}
}
sub parseargs {
my ($in) = @_;
my @args = ();
my $tmp;
while (length $in) {
if ($in =~ s/^\[//) {
my @subarr = ();
if ($in =~ s,^/\* (\d+) vars \*/\],,) {
push @args, $1;
} else {
while ($in !~ s/^\]//) {
($tmp, $in) = parse_one($in);
defined $tmp or return undef;
push @subarr, $tmp;
unless ($in =~ /^\]/ or $in =~ s/^, //) {
print STDERR "$0: $ARGV: $.: missing comma in array.\n";
return undef;
}
if ($in =~ s/^\.\.\.//) {
push @subarr, "...";
}
}
push @args, \@subarr;
}
} elsif ($in =~ s/^\{//) {
my %subhash = ();
while ($in !~ s/^\}//) {
my $key;
unless ($in =~ s/^(\w+)=//) {
print STDERR "$0: $ARGV: $.: struct field expected.\n";
return undef;
}
$key = $1;
($tmp, $in) = parse_one($in);
defined $tmp or return undef;
$subhash{$key} = $tmp;
unless ($in =~ s/, //) {
print STDERR "$0: $ARGV: $.: missing comma in struct.\n";
return undef;
}
}
push @args, \%subhash;
} else {
($tmp, $in) = parse_one($in);
defined $tmp or return undef;
push @args, $tmp;
}
unless (length($in) == 0 or $in =~ s/^, //) {
print STDERR "$0: $ARGV: $.: missing comma.\n";
return undef;
}
}
return @args;
}
my $depth = "";
# process info, indexed by pid.
# fields:
# parent pid number
# seq forks and execs for this pid, in sequence (array)
# filename and argv (from latest exec)
# basename (derived from filename)
# argv[0] is modified to add the basename if it differs from the 0th argument.
my %pr;
sub handle_trace {
my ($pid, $call, $args, $result, $time) = @_;
my $p;
if (defined $time and not defined $pr{$pid}{start}) {
$pr{$pid}{start} = $time;
}
if ($call eq 'execve') {
return if $result != 0;
my ($filename, $argv) = parseargs($args);
($basename) = $filename =~ m/([^\/]*)$/;
if ($basename ne $$argv[0]) {
$$argv[0] = "$basename($$argv[0])";
}
my $seq = $pr{$pid}{seq};
$seq = [] if not defined $seq;
push @$seq, ['EXEC', $filename, $argv];
$pr{$pid}{seq} = $seq;
} elsif ($call eq 'fork' || $call eq 'clone' || $call eq 'vfork') {
return if $result == 0;
my $seq = $pr{$pid}{seq};
$seq = [] if not defined $seq;
push @$seq, ['FORK', $result];
$pr{$pid}{seq} = $seq;
$pr{$result}{parent} = $pid;
} elsif ($call eq '_exit') {
$pr{$pid}{end} = $time if defined $time;
}
}
sub handle_killed {
my ($pid, $time) = @_;
$pr{$pid}{end} = $time if defined $time;
}
sub straight_seq {
my ($pid) = @_;
my $seq = $pr{$pid}{seq};
for $elem (@$seq) {
if ($$elem[0] eq 'EXEC') {
my $argv = $$elem[2];
print "$$elem[0] $$elem[1] @$argv\n";
} elsif ($$elem[0] eq 'FORK') {
print "$$elem[0] $$elem[1]\n";
} else {
print "$$elem[0]\n";
}
}
}
sub first_exec {
my ($pid) = @_;
my $seq = $pr{$pid}{seq};
for $elem (@$seq) {
if ($$elem[0] eq 'EXEC') {
return $elem;
}
}
return undef;
}
sub display_pid_trace {
my ($pid, $lead) = @_;
my $i = 0;
my @seq = @{$pr{$pid}{seq}};
my $elapsed;
if (not defined first_exec($pid)) {
unshift @seq, ['EXEC', '', ['(anon)'] ];
}
if (defined $pr{$pid}{start} and defined $pr{$pid}{end}) {
$elapsed = $pr{$pid}{end} - $pr{$pid}{start};
$elapsed /= $scale_factor;
if ($floatform) {
$elapsed = sprintf("%0.02f", $elapsed);
} else {
$elapsed = int $elapsed;
}
}
for $elem (@seq) {
$i++;
if ($$elem[0] eq 'EXEC') {
my $argv = $$elem[2];
if (defined $elapsed) {
print "$lead [$elapsed] @$argv\n";
undef $elapsed;
} else {
print "$lead @$argv\n";
}
} elsif ($$elem[0] eq 'FORK') {
if ($i == 1) {
if ($lead =~ /-$/) {
display_pid_trace($$elem[1], "$lead--+--");
} else {
display_pid_trace($$elem[1], "$lead +--");
}
} elsif ($i == @seq) {
display_pid_trace($$elem[1], "$lead `--");
} else {
display_pid_trace($$elem[1], "$lead +--");
}
}
if ($i == 1) {
$lead =~ s/\`--/ /g;
$lead =~ s/-/ /g;
$lead =~ s/\+/|/g;
}
}
}
sub display_trace {
my ($startpid) = @_;
$startpid = (keys %pr)[0];
while ($pr{$startpid}{parent}) {
$startpid = $pr{$startpid}{parent};
}
display_pid_trace($startpid, "");
}