1 package IPC::Run::Debug;
5 IPC::Run::Debug - debugging routines for IPC::Run
10 ## Environment variable usage
12 ## To force debugging off and shave a bit of CPU and memory
13 ## by compile-time optimizing away all debugging code in IPC::Run
14 ## (debug => ...) options to IPC::Run will be ignored.
15 export IPCRUNDEBUG=none
17 ## To force debugging on (levels are from 0..10)
18 export IPCRUNDEBUG=basic
20 ## Leave unset or set to "" to compile in debugging support and
21 ## allow runtime control of it using the debug option.
25 Controls IPC::Run debugging. Debugging levels are now set by using words,
26 but the numbers shown are still supported for backwards compatability:
28 0 none disabled (special, see below)
29 1 basic what's running
30 2 data what's being sent/recieved
31 3 details what's going on in more detail
32 4 gory way too much detail for most uses
33 10 all use this when submitting bug reports
34 noopts optimizations forbidden due to inherited STDIN
36 The C<none> level is special when the environment variable IPCRUNDEBUG
37 is set to this the first time IPC::Run::Debug is loaded: it prevents
38 the debugging code from being compiled in to the remaining IPC::Run modules,
41 To do this in a script, here's a way that allows it to be overridden:
44 unless ( defined $ENV{IPCRUNDEBUG} ) {
45 eval 'local $ENV{IPCRUNDEBUG} = "none"; require IPC::Run::Debug"'
50 This should force IPC::Run to not be debuggable unless somebody sets
51 the IPCRUNDEBUG flag; modify this formula to grep @ARGV if need be:
54 unless ( grep /^--debug/, @ARGV ) {
55 eval 'local $ENV{IPCRUNDEBUG} = "none"; require IPC::Run::Debug"'
59 Both of those are untested.
63 @ISA = qw( Exporter ) ;
65 ## We use @EXPORT for the end user's convenience: there's only one function
66 ## exported, it's homonymous with the module, it's an unusual name, and
67 ## it can be suppressed by "use IPC::Run () ;".
75 _debugging_gory_details
76 _debugging_not_optimized
89 all => [ @EXPORT, @EXPORT_OK ],
95 my $disable_debugging =
96 defined $ENV{IPCRUNDEBUG}
99 || lc $ENV{IPCRUNDEBUG} eq "none"
102 eval( $disable_debugging ? <<'STUBS' : <<'SUBS' ) or die $@;
103 sub _map_fds() { "" }
105 sub _debug_desc_fd {}
107 sub _set_child_debug_name {}
108 sub _debugging() { 0 }
109 sub _debugging_level() { 0 }
110 sub _debugging_data() { 0 }
111 sub _debugging_details() { 0 }
112 sub _debugging_gory_details() { 0 }
113 sub _debugging_not_optimized() { 0 }
119 use UNIVERSAL qw( isa );
127 ## I'd like a quicker way (less user, cpu & expecially sys and kernal
128 ## calls) to detect open file descriptors. Let me know...
129 ## Hmmm, could do a 0 length read and check for bad file descriptor...
130 ## but that segfaults on Win32
131 my $test_fd = POSIX::dup( $fd ) ;
132 $in_use = defined $test_fd ;
133 POSIX::close $test_fd if $in_use ;
134 $map .= $in_use ? $digit : '-';
135 $digit = 0 if ++$digit > 9 ;
137 warn "No fds open???" unless $map =~ /\d/ ;
138 $map =~ s/(.{1,12})-*$/$1/ ;
142 use vars qw( $parent_pid ) ;
146 ## TODO: move debugging to it's own module and make it compile-time
149 ## Give kid process debugging nice names
152 sub _set_child_debug_name {
156 ## There's a bit of hackery going on here.
158 ## We want to have any code anywhere be able to emit
159 ## debugging statements without knowing what harness the code is
160 ## being called in/from, since we'd need to pass a harness around to
163 ## Thus, $cur_self was born.
181 sub _debugging_level() {
184 $level = $IPC::Run::cur_self->{debug} || 0
185 if $IPC::Run::cur_self
186 && ( $IPC::Run::cur_self->{debug} || 0 ) >= $level ;
188 if ( defined $ENV{IPCRUNDEBUG} ) {
189 my $v = $ENV{IPCRUNDEBUG};
190 $v = $debug_levels{lc $v} if $v =~ /[a-zA-Z]/;
191 unless ( defined $v ) {
192 $warned ||= warn "Unknown debug level $ENV{IPCRUNDEBUG}, assuming 'basic' (1)\n";
195 $level = $v if $v > $level ;
200 sub _debugging_atleast($) {
201 my $min_level = shift || 1 ;
203 my $level = _debugging_level ;
205 return $level >= $min_level ? $level : 0 ;
208 sub _debugging() { _debugging_atleast 1 }
209 sub _debugging_data() { _debugging_atleast 2 }
210 sub _debugging_details() { _debugging_atleast 3 }
211 sub _debugging_gory_details() { _debugging_atleast 4 }
212 sub _debugging_not_optimized() { ( $ENV{IPCRUNDEBUG} || "" ) eq "notopt" }
215 ## This routine is called only in spawned children to fake out the
216 ## debug routines so they'll emit debugging info.
217 $IPC::Run::cur_self = {} ;
220 $IPC::Run::cur_self->{debug},
221 $IPC::Run::cur_self->{DEBUG_FD},
228 # return unless _debugging || _debugging_not_optimized ;
230 my $fd = defined &IPC::Run::_debug_fd
231 ? IPC::Run::_debug_fd()
240 defined $IPC::Run::cur_self ? "#$IPC::Run::cur_self->{ID}" : (),
243 defined $debug_name && length $debug_name ? $debug_name : (),
248 sprintf( " %04d", time - $^T ),
249 ( _debugging_details ? ( " ", _map_fds ) : () ),
250 length $debug_id ? ( " [", $debug_id, "]" ) : (),
254 my $msg = join( '', map defined $_ ? $_ : "<undef>", @_ ) ;
256 $msg =~ s{^}{$prefix}gm ;
258 POSIX::write( $fd, $msg, length $msg ) ;
262 my @fd_descs = ( 'stdin', 'stdout', 'stderr' ) ;
265 return unless _debugging ;
270 Carp::carp join " ", caller(0), $text, $op if defined $op && isa( $op, "IO::Pty" ) ;
277 ? ( $fd_descs[$op->{FD}] )
278 : ( 'fd ', $op->{FD} )
284 ( defined $kid ? ( ' ', $kid->{NUM}, ) : () ),
287 ? $fd_descs[$op->{KFD}]
289 && defined $kid->{DEBUG_FD}
290 && $op->{KFD} == $kid->{DEBUG_FD}
291 ? ( 'debug (', $op->{KFD}, ')' )
292 : ( 'fd ', $op->{KFD} )
307 Barrie Slaymaker <barries@slaysys.com>, with numerous suggestions by p5p.