Debian Codegraph @

lib/Test2/Harness/Stall/Trace.pm

raw
1 package Test2::Harness::Stall::Trace;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use File::Spec();
8
9 use Importer Importer => 'import';
10
11 our @EXPORT_OK = qw/install_trace_handler stack_trace/;
12
13 # Deliberately small. Everything a watcher can see from outside -- /proc, open
14 # files, the run state -- is collected by whoever sends the signal, because a
15 # process wedged in an uninterruptible syscall never runs this at all. The Perl
16 # call stack is the one thing only the process itself can produce, so it is the
17 # only thing gathered here. Nothing in this file may walk harness objects or
18 # take a lock; the process is already believed stuck.
19 sub stack_trace {
20 my @frames;
21
22 my $i = 0;
23 while (my @caller = caller($i++)) {
24 my ($pkg, $file, $line, $sub) = @caller;
25
26 # Plain caller(), never Carp::longmess: longmess formats each frame's
27 # arguments, and those hold the resource objects and task hashrefs
28 # belonging to the code we suspect of being stuck.
29 push @frames => {
30 package => $pkg,
31 file => $file,
32 line => $line,
33 sub => $sub,
34 };
35
36 last if $i > 200;
37 }
38
39 return \@frames;
40 }
41
42 sub _render {
43 my ($frames) = @_;
44
45 my $out = "$$ $0 stack trace:\n";
46 my $i = 0;
47 for my $frame (@$frames) {
48 $out .= sprintf(" [%d] %s at %s line %s\n", $i++, $frame->{sub}, $frame->{file}, $frame->{line});
49 }
50
51 return $out;
52 }
53
54 sub _write_trace {
55 my ($dir, $round) = @_;
56
57 my $frames = stack_trace();
58 my $text = _render($frames);
59
60 # Its own STDERR first, so the trace survives even if whoever asked for it
61 # dies before collecting the files. For the runner and its children that is
62 # error.log, which the collector already forwards.
63 print STDERR "\n$text";
64
65 return unless $dir && -d $dir;
66
67 my $file = File::Spec->catfile($dir, "stack-$$-$round.txt");
68 open(my $fh, '>', $file) or return;
69 print $fh $text;
70
71 return;
72 }
73
74 # Installed in every harness process, never in a test job. A job process sheds
75 # this handler when the runner's Scope::Guard restores the original %SIG, so
76 # SIGUSR1 there takes its default action and kills the test. Whoever signals
77 # must use a whitelist of known harness pids and must never signal a process
78 # group.
79 sub install_trace_handler {
80 my ($dir) = @_;
81
82 my $round = 0;
83
84 $SIG{USR1} = sub {
85 local ($!, $@, $_);
86 $round++;
87 eval { _write_trace($dir, $round); 1 };
88 };
89
90 return;
91 }
92
93 1;
94
95 __END__
96
97 =pod
98
99 =encoding UTF-8
100
101 =head1 NAME
102
103 Test2::Harness::Stall::Trace - Report a process's own Perl call stack on
104 demand.
105
106 =head1 DESCRIPTION
107
108 Deliberately small. Everything a watcher can see from outside a stuck process
109 -- C</proc> state, wait channels, open files, the run state -- is collected by
110 whoever sends the signal, because a process wedged in an uninterruptible
111 syscall never runs a handler at all, and that is the case most in need of
112 description. The Perl call stack is the one thing only the process itself can
113 produce, so it is the only thing gathered here.
114
115 Nothing in this module may walk harness objects or take a lock; by the time it
116 runs, the process is already believed stuck. It uses a plain C<caller> loop
117 rather than C<Carp::longmess>, which formats each frame's arguments -- and
118 those hold the resource objects and task hashrefs belonging to the code under
119 suspicion.
120
121 =head2 NEVER SIGNAL A TEST JOB
122
123 The handler is installed in harness processes only: the runner, the scheduler,
124 the stages, the collector and the auditor. A test job process sheds it when the
125 runner's C<Scope::Guard> restores the original C<%SIG>, so C<SIGUSR1> there
126 takes its default action and B<terminates the test>.
127
128 Whoever sends the signal must therefore use a whitelist of known harness pids,
129 and must never signal a process group.
130
131 =head1 SYNOPSIS
132
133 use Test2::Harness::Stall::Trace qw/install_trace_handler/;
134
135 install_trace_handler(File::Spec->catdir($workdir, 'stall'));
136
137 =head1 EXPORTS
138
139 =over 4
140
141 =item install_trace_handler($dir)
142
143 Installs the C<SIGUSR1> handler. Each signal writes the frames to the process's
144 own STDERR, and to C<$dir/stack-$$-N.txt> where N counts up per process.
145
146 Install it after any snapshot of C<%SIG> the process intends to restore, so a
147 forked test job sheds it.
148
149 =item $arrayref = stack_trace()
150
151 The current call stack as a list of hashrefs with C<package>, C<file>, C<line>
152 and C<sub>. Frame arguments are never touched.
153
154 =back
155
156 =head1 SOURCE
157
158 The source code repository for Test2-Harness can be found at
159 L<https://github.com/Test-More/Test2-Harness/>.
160
161 =head1 MAINTAINERS
162
163 =over 4
164
165 =item Chad Granum E<lt>[email protected]<gt>
166
167 =back
168
169 =head1 AUTHORS
170
171 =over 4
172
173 =item Chad Granum E<lt>[email protected]<gt>
174
175 =back
176
177 =head1 COPYRIGHT
178
179 Copyright Chad Granum E<lt>[email protected]<gt>.
180
181 This program is free software; you can redistribute it and/or
182 modify it under the same terms as Perl itself.
183
184 See L<https://dev.perl.org/licenses/>
185
186 =cut