3 # This script processes strace -f output. It displays a graph of invoked
4 # subprocesses, and is useful for finding out what complex commands do.
6 # You will probably want to invoke strace with -q as well, and with
7 # -s 100 to get complete filenames.
9 # The script can also handle the output with strace -t, -tt, or -ttt.
10 # It will add elapsed time for each process in that case.
12 # This script is Copyright (C) 1998 by Richard Braakman <dark@xs4all.nl>.
14 # Redistribution and use in source and binary forms, with or without
15 # modification, are permitted provided that the following conditions
17 # 1. Redistributions of source code must retain the above copyright
18 # notice, this list of conditions and the following disclaimer.
19 # 2. Redistributions in binary form must reproduce the above copyright
20 # notice, this list of conditions and the following disclaimer in the
21 # documentation and/or other materials provided with the distribution.
22 # 3. The name of the author may not be used to endorse or promote products
23 # derived from this software without specific prior written permission.
25 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 # Scales for strace slowdown. Make configurable!
41 my $scale_factor = 3.5;
44 my ($pid, $call, $args, $result, $time);
50 if (s/^(\d\d):(\d\d):(\d\d)(?:\.(\d\d\d\d\d\d))? //) {
51 $time = $1 * 3600 + $2 * 60 + $3;
53 $time = $time + $4 / 1000000;
56 } elsif (s/^(\d+)\.(\d\d\d\d\d\d) //) {
57 $time = $1 + ($2 / 1000000);
61 if (s/ <unfinished ...>$//) {
62 $unfinished{$pid} = $_;
66 if (s/^<... \S+ resumed> //) {
67 unless (exists $unfinished{$pid}) {
68 print STDERR "$0: $ARGV: cannot find start of resumed call on line $.";
71 $_ = $unfinished{$pid} . $_;
72 delete $unfinished{$pid};
75 if (/^--- SIG(\S+) \(.*\) ---$/) {
76 # $pid received signal $1
77 # currently we don't do anything with this
81 if (/^\+\+\+ killed by SIG(\S+) \+\+\+$/) {
82 # $pid received signal $1
83 handle_killed($pid, $time);
87 ($call, $args, $result) = /(\S+)\((.*)\)\s+= (.*)$/;
88 unless (defined $result) {
89 print STDERR "$0: $ARGV: $.: cannot parse line.\n";
93 handle_trace($pid, $call, $args, $result, $time);
105 if ($in =~ s/^\\(.)//) {
107 } elsif ($in =~ s/^\"//) {
108 if ($in =~ s/^\.\.\.//) {
109 return ("$result...", $in);
111 return ($result, $in);
112 } elsif ($in =~ s/([^\\\"]*)//) {
123 if ($in =~ s/^\"//) {
124 ($tmp, $in) = parse_str($in);
125 if (not defined $tmp) {
126 print STDERR "$0: $ARGV: $.: cannot parse string.\n";
130 } elsif ($in =~ s/^0x(\x+)//) {
131 return (hex $1, $in);
132 } elsif ($in =~ s/^(\d+)//) {
133 return (int $1, $in);
135 print STDERR "$0: $ARGV: $.: unrecognized element.\n";
146 if ($in =~ s/^\[//) {
148 if ($in =~ s,^/\* (\d+) vars \*/\],,) {
151 while ($in !~ s/^\]//) {
152 ($tmp, $in) = parse_one($in);
153 defined $tmp or return undef;
155 unless ($in =~ /^\]/ or $in =~ s/^, //) {
156 print STDERR "$0: $ARGV: $.: missing comma in array.\n";
159 if ($in =~ s/^\.\.\.//) {
163 push @args, \@subarr;
165 } elsif ($in =~ s/^\{//) {
167 while ($in !~ s/^\}//) {
169 unless ($in =~ s/^(\w+)=//) {
170 print STDERR "$0: $ARGV: $.: struct field expected.\n";
174 ($tmp, $in) = parse_one($in);
175 defined $tmp or return undef;
176 $subhash{$key} = $tmp;
177 unless ($in =~ s/, //) {
178 print STDERR "$0: $ARGV: $.: missing comma in struct.\n";
182 push @args, \%subhash;
184 ($tmp, $in) = parse_one($in);
185 defined $tmp or return undef;
188 unless (length($in) == 0 or $in =~ s/^, //) {
189 print STDERR "$0: $ARGV: $.: missing comma.\n";
199 # process info, indexed by pid.
202 # seq forks and execs for this pid, in sequence (array)
204 # filename and argv (from latest exec)
205 # basename (derived from filename)
206 # argv[0] is modified to add the basename if it differs from the 0th argument.
211 my ($pid, $call, $args, $result, $time) = @_;
214 if (defined $time and not defined $pr{$pid}{start}) {
215 $pr{$pid}{start} = $time;
218 if ($call eq 'execve') {
219 return if $result != 0;
221 my ($filename, $argv) = parseargs($args);
222 ($basename) = $filename =~ m/([^\/]*)$/;
223 if ($basename ne $$argv[0]) {
224 $$argv[0] = "$basename($$argv[0])";
226 my $seq = $pr{$pid}{seq};
227 $seq = [] if not defined $seq;
229 push @$seq, ['EXEC', $filename, $argv];
231 $pr{$pid}{seq} = $seq;
232 } elsif ($call eq 'fork' || $call eq 'clone' || $call eq 'vfork') {
233 return if $result == 0;
235 my $seq = $pr{$pid}{seq};
236 $seq = [] if not defined $seq;
237 push @$seq, ['FORK', $result];
238 $pr{$pid}{seq} = $seq;
239 $pr{$result}{parent} = $pid;
240 } elsif ($call eq '_exit') {
241 $pr{$pid}{end} = $time if defined $time;
246 my ($pid, $time) = @_;
247 $pr{$pid}{end} = $time if defined $time;
252 my $seq = $pr{$pid}{seq};
255 if ($$elem[0] eq 'EXEC') {
256 my $argv = $$elem[2];
257 print "$$elem[0] $$elem[1] @$argv\n";
258 } elsif ($$elem[0] eq 'FORK') {
259 print "$$elem[0] $$elem[1]\n";
268 my $seq = $pr{$pid}{seq};
271 if ($$elem[0] eq 'EXEC') {
278 sub display_pid_trace {
279 my ($pid, $lead) = @_;
281 my @seq = @{$pr{$pid}{seq}};
284 if (not defined first_exec($pid)) {
285 unshift @seq, ['EXEC', '', ['(anon)'] ];
288 if (defined $pr{$pid}{start} and defined $pr{$pid}{end}) {
289 $elapsed = $pr{$pid}{end} - $pr{$pid}{start};
290 $elapsed /= $scale_factor;
292 $elapsed = sprintf("%0.02f", $elapsed);
294 $elapsed = int $elapsed;
300 if ($$elem[0] eq 'EXEC') {
301 my $argv = $$elem[2];
302 if (defined $elapsed) {
303 print "$lead [$elapsed] @$argv\n";
306 print "$lead @$argv\n";
308 } elsif ($$elem[0] eq 'FORK') {
311 display_pid_trace($$elem[1], "$lead--+--");
313 display_pid_trace($$elem[1], "$lead +--");
315 } elsif ($i == @seq) {
316 display_pid_trace($$elem[1], "$lead `--");
318 display_pid_trace($$elem[1], "$lead +--");
332 $startpid = (keys %pr)[0];
333 while ($pr{$startpid}{parent}) {
334 $startpid = $pr{$startpid}{parent};
337 display_pid_trace($startpid, "");