blib/lib/Devel/MAT/Dumper.pm

filter: Statementstmt 39.7% 33 / 83 Branchbran 10.5% 4 / 38 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 66.6% 8 / 12 Podpod n/a totaltotal 32.6% 45 / 138 CC 20 SCAR 44.0
CC
20
Cov
30%
import
170.5
__ANON__
20.0
END
7.9
CRAP 81.1
↑ Summary blib/lib/Devel/MAT/Dumper/Helper.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2013-2024 -- leonerd@leonerd.org.uk
5  
6 package Devel::MAT::Dumper;
7  
8 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $VERSION = '0.52';
13  
14 2 ▶use File::Basename qw( basename );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 require XSLoader;
19 2 XSLoader::load( __PACKAGE__, $VERSION );
20  
21 =head1 NAME
22  
23 C<Devel::MAT::Dumper> - write a heap dump file for later analysis
24  
25 =head1 SYNOPSIS
26  
27 =for highlighter language=perl
28  
29 use Devel::MAT::Dumper;
30  
31 Devel::MAT::Dumper::dump( "path/to/the/file.pmat" );
32  
33 =head1 DESCRIPTION
34  
35 This module provides the memory-dumping function that creates a heap dump file
36 which can later be read by L<Devel::MAT::Dumpfile>. It provides a single
37 function which is not exported, which writes a file to the given path.
38  
39 The dump file will contain a representation of every SV in Perl's arena,
40 providing information about pointers between them, as well as other
41 information about the state of the process at the time it was created. It
42 contains a snapshot of the process at that moment in time, which can later be
43 loaded and analysed by various tools using C<Devel::MAT::Dumpfile>.
44  
45 This module used to be part of the main L<Devel::MAT> distribution but is now
46 in its own one so that it can be installed independently on servers or other
47 locations where perl processes need to inspected but analysis tools can be run
48 elsewhere.
49  
50 =cut
51  
52 =head1 IMPORT OPTIONS
53  
54 =for highlighter
55  
56 The following C<import> options control the behaviour of the module. They may
57 primarily be useful when used in the C<-M> perl option:
58  
59 =head2 -dump_at_DIE
60  
61 Installs a handler for the special C<__DIE__> signal to write a dump file when
62 C<die()> is about to cause a fatal signal. This is more reliable at catching
63 the callstack and memory state than using an C<END> block.
64  
65 $ perl -MDevel::MAT::Dumper=-dump_at_DIE ...
66  
67 =head2 -dump_at_WARN
68  
69 Installs a handler for the special C<__WARN__> signal to write a dump file
70 when perl prints a warning.
71  
72 $ perl -MDevel::MAT::Dumper=-dump_at_WARN ...
73  
74 It is likely useful to combine this with the C<NNN> numbering feature of the
75 C<-file> argument, to ensure that later warnings don't overwrite a particular
76 file.
77  
78 =head2 -dump_at_END
79  
80 Installs an C<END> block which writes a dump file at C<END> time, just before
81 the interpreter exits.
82  
83 $ perl -MDevel::MAT::Dumper=-dump_at_END ...
84  
85 =head2 -dump_at_SIGQUIT
86  
87 Installs a handler for C<SIGQUIT> to write a dump file if the signal is
88 received. The signal handler will remain in place and can be used several
89 times.
90  
91 $ perl -MDevel::MAT::Dumper=-dump_at_SIGQUIT ...
92  
93 Take care if you are using the C<< <Ctrl-\> >> key combination on a terminal
94 to send this signal to a foreground process, because if it has C<fork()>ed any
95 background workers or similar, the signal will also be delivered to those as
96 well.
97  
98 =head2 -dump_at_SIGI<NAME>
99  
100 Installs a handler for the named signal (e.g. C<SIGABRT>, C<SIGINT>) to write
101 a dump file if the signal is received. After dumping the file, the signal
102 handler is removed and the signal re-raised.
103  
104 $ perl -MDevel::MAT::Dumper=-dump_at_SIGABRT ...
105  
106 Note that C<SIGABRT> uses an "unsafe" signal handler (i.e. not deferred until
107 the next perl op), so it can capture the full context of any ongoing XS or C
108 library operations.
109  
110 =head2 -file $PATH
111  
112 Sets the name of the file which is automatically dumped; defaults to basename
113 F<$0.pmat> if not supplied.
114  
115 $ perl -MDevel::MAT::Dumper=-file,foo.pmat ...
116  
117 In the special case that C<$0> is exactly the string C<-e> or C<-E>, the
118 filename will be prefixed with C<perl> so as not to create files whose names
119 begin with a leading hyphen, as this confuses some commandline parsers.
120  
121 $ perl -MDevel::MAT::Dumper=-dump_at_END -E 'say "hello"'
122 hello
123 Dumping to perl-e.pmat because of END
124  
125 If the pattern contains C<NNN>, this will be replaced by a unique serial
126 number per written file, starting from 0. This may be helpful in the case of
127 C<DIE>, C<WARN> or C<SIGQUIT> handlers, which could be invoked multiple times.
128  
129 The file name is converted to an absolute path immediately, so if the running
130 program later calls C<chdir()>, it will still be generated in the directory
131 the program started from rather than the one it happens to be in at the time.
132  
133 =head2 -max_string
134  
135 Sets the maximum length of string buffer to dump from PVs; defaults to 256 if
136 not supplied. Use a negative size to dump the entire buffer of every PV
137 regardless of size.
138  
139 =head2 -eager_open
140  
141 Opens the dump file immediately at C<import> time, instead of waiting until
142 the time it actually writes the heap dump. This may be useful if the process
143 changes user ID, or to debug problems involving too many open filehandles.
144  
145 =cut
146  
147 2 our $MAX_STRING = 256; # used by XS code
148  
149 2 my $basename = basename( $0 );
150 2 ▶$basename = "perl$basename" if $basename =~ m/^-e$/i; # RT119164
Branch: 1 branch
BranchTrueFalseTotal
if $basename =~ /^-e$/i 0 2 2
151 2 my $dumpfile_name = File::Spec->rel2abs( "$basename.pmat" );
152  
153 2 my $dumpfh;
154 2 my $next_serial = 0;
155  
156 2 my $dump_at_END;
157 2 ▶END {
Subroutine END: called CC=3 SCAR=20.7
158 2 ▶ return unless $dump_at_END;
Branch: 1 branch
BranchTrueFalseTotal
unless $dump_at_END 2 0 2
159  
160 0 ▶ if( $dumpfh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dumpfh) { } 0 0 0
161 0 Devel::MAT::Dumper::dumpfh( $dumpfh );
162 }
163 else {
164 0 ( my $file = $dumpfile_name ) =~ s/NNN/$next_serial++/e;
165 0 print STDERR "Dumping to $file because of END\n";
166 0 Devel::MAT::Dumper::dump( $file );
167 }
168 }
169  
170 sub import
171 2 ▶{
Subroutine import: called CC=14 SCAR=51.4
172 2 my $pkg = shift;
173  
174 2 my $eager_open;
175  
176 2 ▶ while( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 2 2
177 0 my $sym = shift;
178  
179 0 ▶ if( $sym eq "-dump_at_DIE" ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($sym eq '-dump_at_DIE') { } 0 0 0
elsif ($sym eq '-dump_at_WARN') { } 0 0 0
elsif ($sym eq '-dump_at_END') { } 0 0 0
elsif ($sym eq '-dump_at_SIGQUIT') { } 0 0 0
elsif ($sym =~ /^-dump_at_SIG(\S+)$/) { } 0 0 0
elsif ($sym eq '-file') { } 0 0 0
elsif ($sym eq '-max_string') { } 0 0 0
elsif ($sym eq '-eager_open') { } 0 0 0
180 0 my $old_DIE = $SIG{__DIE__};
181 0 ▶ $SIG{__DIE__} = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
182 0 ▶ local $SIG{__DIE__} = $old_DIE if defined $old_DIE;
Branch: 1 branch
BranchTrueFalseTotal
if defined $old_DIE 0 0 0
183 0 ▶ return if $^S or !defined $^S; # only catch real process-fatal errors
Branch: 1 branch
BranchTrueFalseTotal
if $^S or not defined $^S 0 0 0
Condition0%0/3
$^S or not defined $^S
l!l&&r!l&&!r
000
MC/DC0%0/2
$^S or not defined $^S
$^S not defined $^S
Truth table0%0/3
A or not(B)
A= $^SB= not defined $^S
ABresult
1X1
011
000
184  
185 0 ( my $file = $dumpfile_name ) =~ s/NNN/$next_serial++/e;
186 0 print STDERR "Dumping to $file because of DIE\n";
187 0 Devel::MAT::Dumper::dump( $file );
188 0 die @_;
189 0 };
190 }
191 elsif( $sym eq "-dump_at_WARN" ) {
192 0 my $old_WARN = $SIG{__WARN__};
193 0 ▶ $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
194 0 ▶ local $SIG{__WARN__} = $old_WARN if defined $old_WARN;
Branch: 1 branch
BranchTrueFalseTotal
if defined $old_WARN 0 0 0
195  
196 0 ( my $file = $dumpfile_name ) =~ s/NNN/$next_serial++/e;
197 0 print STDERR "Dumping to $file because of WARN\n";
198 0 Devel::MAT::Dumper::dump( $file );
199 0 warn @_;
200 0 };
201 }
202 elsif( $sym eq "-dump_at_END" ) {
203 0 $dump_at_END++;
204 }
205 elsif( $sym eq "-dump_at_SIGQUIT" ) {
206 0 ▶ $SIG{QUIT} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
207 0 ( my $file = $dumpfile_name ) =~ s/NNN/$next_serial++/e;
208 0 print STDERR "Dumping to $file because of SIGQUIT\n";
209 0 Devel::MAT::Dumper::dump( $file );
210 0 };
211 }
212 elsif( $sym =~ m/^-dump_at_SIG(\S+)$/ ) {
213 0 my $signal = $1;
214 0 ▶ exists $SIG{$signal} or die "Unrecognised signal name SIG$signal\n";
Branch: 1 branch
BranchTrueFalseTotal
exists $SIG{$signal} or die "Unrecognised signal name SIG$signal\n" 0 0 0
215  
216 0 ▶ my $handler = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
217 0 ( my $file = $dumpfile_name ) =~ s/NNN/$next_serial++/e;
218 0 print STDERR "Dumping to $file because of SIG$signal\n";
219 0 Devel::MAT::Dumper::dump( $file );
220 0 undef $SIG{$signal};
221 0 kill $signal => $$;
222 0 };
223  
224 0 ▶ if( $signal eq "ABRT" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signal eq 'ABRT') { } 0 0 0
225 # Install SIGABRT handler using unsafe signal so it can see
226 # inner workings of C code properly
227 0 my $sigaction = POSIX::SigAction->new( $handler );
228 0 $sigaction->safe(0);
229  
230 0 POSIX::sigaction( POSIX::SIGABRT, $sigaction );
231 }
232 else {
233 0 $SIG{$signal} = $handler;
234 }
235 }
236 elsif( $sym eq "-file" ) {
237 0 $dumpfile_name = File::Spec->rel2abs( shift );
238 }
239 elsif( $sym eq "-max_string" ) {
240 0 $MAX_STRING = shift;
241 }
242 elsif( $sym eq "-eager_open" ) {
243 0 $eager_open++;
244 }
245 else {
246 0 die "Unrecognised $pkg import symbol $sym\n";
247 }
248 }
249  
250 2 ▶ if( $eager_open ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eager_open) 0 2 2
251 0 ▶ open $dumpfh, ">", $dumpfile_name or
Branch: 1 branch
BranchTrueFalseTotal
open $dumpfh, ">", $dumpfile_name or die "Cannot open $dumpfile_name for writing - $!\n" 0 0 0
252 die "Cannot open $dumpfile_name for writing - $!\n";
253 }
254 }
255  
256 =head1 FUNCTIONS
257  
258 =for highlighter language=perl
259  
260 These functions are not exported, they must be called fully-qualified.
261  
262 =head2 dump
263  
264 dump( $path );
265  
266 Writes a heap dump to the named file
267  
268 =head2 dumpfh
269  
270 dumpfh( $fh );
271  
272 Writes a heap dump to the given filehandle (which must be a plain OS-level
273 filehandle, though does not need to be a regular file, or seekable).
274  
275 =cut
276  
277 =head1 AUTHOR
278  
279 Paul Evans <leonerd@leonerd.org.uk>
280  
281 =cut
282  
283 2 0x55AA;
↑ Summary blib/lib/Devel/MAT/Dumper/Helper.pm »