| 1 | # Copied from perl5db.pl | ||
| 2 | # FIXME: Not hooked in yet. | ||
| 3 | |||
| 4 | package Devel::Trepan::DB::Sig; | ||
| 5 | |||
| 6 | =head1 DIE AND WARN MANAGEMENT | ||
| 7 | |||
| 8 | =head2 C<diesignal> | ||
| 9 | |||
| 10 | C<diesignal> is a just-drop-dead C<die> handler. It's most useful when trying | ||
| 11 | to debug a debugger problem. | ||
| 12 | |||
| 13 | It does its best to report the error that occurred, and then forces the | ||
| 14 | program, debugger, and everything to die. | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | sub diesignal { | ||
| 19 | |||
| 20 | # No entry/exit messages. | ||
| 21 | local $frame = 0; | ||
| 22 | |||
| 23 | # No return value prints. | ||
| 24 | local $doret = -2; | ||
| 25 | |||
| 26 | # set the abort signal handling to the default (just terminate). | ||
| 27 | $SIG{'ABRT'} = 'DEFAULT'; | ||
| 28 | |||
| 29 | # If we enter the signal handler recursively, kill myself with an | ||
| 30 | # abort signal (so we just terminate). | ||
| 31 | kill 'ABRT', $$ if $panic++; | ||
| 32 | |||
| 33 | # If we can show detailed info, do so. | ||
| 34 | if ( defined &Carp::longmess ) { | ||
| 35 | |||
| 36 | # Don't recursively enter the warn handler, since we're carping. | ||
| 37 | local $SIG{__WARN__} = ''; | ||
| 38 | |||
| 39 | # Skip two levels before reporting traceback: we're skipping | ||
| 40 | # mydie and confess. | ||
| 41 | local $Carp::CarpLevel = 2; # mydie + confess | ||
| 42 | |||
| 43 | # Tell us all about it. | ||
| 44 | &warn( Carp::longmess("Signal @_") ); | ||
| 45 | } | ||
| 46 | |||
| 47 | # No Carp. Tell us about the signal as best we can. | ||
| 48 | else { | ||
| 49 | local $\ = ''; | ||
| 50 | print $DB::OUT "Got signal @_\n"; | ||
| 51 | } | ||
| 52 | |||
| 53 | # Drop dead. | ||
| 54 | kill 'ABRT', $$; | ||
| 55 | } ## end sub diesignal | ||
| 56 | |||
| 57 | =head2 C<dbwarn> | ||
| 58 | |||
| 59 | The debugger's own default C<$SIG{__WARN__}> handler. We load C<Carp> to | ||
| 60 | be able to get a stack trace, and output the warning message vi C<DB::dbwarn()>. | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | sub dbwarn { | ||
| 65 | |||
| 66 | # No entry/exit trace. | ||
| 67 | local $frame = 0; | ||
| 68 | |||
| 69 | # No return value printing. | ||
| 70 | local $doret = -2; | ||
| 71 | |||
| 72 | # Turn off warn and die handling to prevent recursive entries to this | ||
| 73 | # routine. | ||
| 74 | local $SIG{__WARN__} = ''; | ||
| 75 | local $SIG{__DIE__} = ''; | ||
| 76 | |||
| 77 | # Load Carp if we can. If $^S is false (current thing being compiled isn't | ||
| 78 | # done yet), we may not be able to do a require. | ||
| 79 | eval { require Carp } | ||
| 80 | if defined $^S; # If error/warning during compilation, | ||
| 81 | # require may be broken. | ||
| 82 | |||
| 83 | # Use the core warn() unless Carp loaded OK. | ||
| 84 | CORE::warn( @_, | ||
| 85 | "\nCannot print stack trace, load with -MCarp option to see stack" ), | ||
| 86 | return | ||
| 87 | unless defined &Carp::longmess; | ||
| 88 | |||
| 89 | # Save the current values of $single and $trace, and then turn them off. | ||
| 90 | my ( $mysingle, $mytrace ) = ( $single, $trace ); | ||
| 91 | $single = 0; | ||
| 92 | $trace = 0; | ||
| 93 | |||
| 94 | # We can call Carp::longmess without its being "debugged" (which we | ||
| 95 | # don't want - we just want to use it!). Capture this for later. | ||
| 96 | my $mess = Carp::longmess(@_); | ||
| 97 | |||
| 98 | # Restore $single and $trace to their original values. | ||
| 99 | ( $single, $trace ) = ( $mysingle, $mytrace ); | ||
| 100 | |||
| 101 | # Use the debugger's own special way of printing warnings to print | ||
| 102 | # the stack trace message. | ||
| 103 | &warn($mess); | ||
| 104 | } ## end sub dbwarn | ||
| 105 | |||
| 106 | =head2 C<dbdie> | ||
| 107 | |||
| 108 | The debugger's own C<$SIG{__DIE__}> handler. Handles providing a stack trace | ||
| 109 | by loading C<Carp> and calling C<Carp::longmess()> to get it. We turn off | ||
| 110 | single stepping and tracing during the call to C<Carp::longmess> to avoid | ||
| 111 | debugging it - we just want to use it. | ||
| 112 | |||
| 113 | If C<dieLevel> is zero, we let the program being debugged handle the | ||
| 114 | exceptions. If it's 1, you get backtraces for any exception. If it's 2, | ||
| 115 | the debugger takes over all exception handling, printing a backtrace and | ||
| 116 | displaying the exception via its C<dbwarn()> routine. | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub dbdie { | ||
| 121 | local $frame = 0; | ||
| 122 | local $doret = -2; | ||
| 123 | local $SIG{__DIE__} = ''; | ||
| 124 | local $SIG{__WARN__} = ''; | ||
| 125 | my $i = 0; | ||
| 126 | my $ineval = 0; | ||
| 127 | my $sub; | ||
| 128 | if ( $dieLevel > 2 ) { | ||
| 129 | local $SIG{__WARN__} = \&dbwarn; | ||
| 130 | &warn(@_); # Yell no matter what | ||
| 131 | return; | ||
| 132 | } | ||
| 133 | if ( $dieLevel < 2 ) { | ||
| 134 | die @_ if $^S; # in eval propagate | ||
| 135 | } | ||
| 136 | |||
| 137 | # The code used to check $^S to see if compilation of the current thing | ||
| 138 | # hadn't finished. We don't do it anymore, figuring eval is pretty stable. | ||
| 139 | eval { require Carp }; | ||
| 140 | |||
| 141 | die( @_, | ||
| 142 | "\nCannot print stack trace, load with -MCarp option to see stack" ) | ||
| 143 | unless defined &Carp::longmess; | ||
| 144 | |||
| 145 | # We do not want to debug this chunk (automatic disabling works | ||
| 146 | # inside DB::DB, but not in Carp). Save $single and $trace, turn them off, | ||
| 147 | # get the stack trace from Carp::longmess (if possible), restore $signal | ||
| 148 | # and $trace, and then die with the stack trace. | ||
| 149 | my ( $mysingle, $mytrace ) = ( $single, $trace ); | ||
| 150 | $single = 0; | ||
| 151 | $trace = 0; | ||
| 152 | my $mess = "@_"; | ||
| 153 | { | ||
| 154 | |||
| 155 | package # hide me from PAUSE | ||
| 156 | Carp; # Do not include us in the list | ||
| 157 | eval { $mess = Carp::longmess(@_); }; | ||
| 158 | } | ||
| 159 | ( $single, $trace ) = ( $mysingle, $mytrace ); | ||
| 160 | die $mess; | ||
| 161 | } ## end sub dbdie | ||
| 162 | |||
| 163 | =head2 C<warnlevel()> | ||
| 164 | |||
| 165 | Set the C<$DB::warnLevel> variable that stores the value of the | ||
| 166 | C<warnLevel> option. Calling C<warnLevel()> with a positive value | ||
| 167 | results in the debugger taking over all warning handlers. Setting | ||
| 168 | C<warnLevel> to zero leaves any warning handlers set up by the program | ||
| 169 | being debugged in place. | ||
| 170 | |||
| 171 | =cut | ||
| 172 | |||
| 173 | sub warnLevel { | ||
| 174 | if (@_) { | ||
| 175 | $prevwarn = $SIG{__WARN__} unless $warnLevel; | ||
| 176 | $warnLevel = shift; | ||
| 177 | if ($warnLevel) { | ||
| 178 | $SIG{__WARN__} = \&DB::dbwarn; | ||
| 179 | } | ||
| 180 | elsif ($prevwarn) { | ||
| 181 | $SIG{__WARN__} = $prevwarn; | ||
| 182 | } else { | ||
| 183 | undef $SIG{__WARN__}; | ||
| 184 | } | ||
| 185 | } ## end if (@_) | ||
| 186 | $warnLevel; | ||
| 187 | } ## end sub warnLevel | ||
| 188 | |||
| 189 | =head2 C<dielevel> | ||
| 190 | |||
| 191 | Similar to C<warnLevel>. Non-zero values for C<dieLevel> result in the | ||
| 192 | C<DB::dbdie()> function overriding any other C<die()> handler. Setting it to | ||
| 193 | zero lets you use your own C<die()> handler. | ||
| 194 | |||
| 195 | =cut | ||
| 196 | |||
| 197 | sub dieLevel { | ||
| 198 | local $\ = ''; | ||
| 199 | if (@_) { | ||
| 200 | $prevdie = $SIG{__DIE__} unless $dieLevel; | ||
| 201 | $dieLevel = shift; | ||
| 202 | if ($dieLevel) { | ||
| 203 | |||
| 204 | # Always set it to dbdie() for non-zero values. | ||
| 205 | $SIG{__DIE__} = \&DB::dbdie; # if $dieLevel < 2; | ||
| 206 | |||
| 207 | # No longer exists, so don't try to use it. | ||
| 208 | #$SIG{__DIE__} = \&DB::diehard if $dieLevel >= 2; | ||
| 209 | |||
| 210 | # If we've finished initialization, mention that stack dumps | ||
| 211 | # are enabled, If dieLevel is 1, we won't stack dump if we die | ||
| 212 | # in an eval(). | ||
| 213 | print $OUT "Stack dump during die enabled", | ||
| 214 | ( $dieLevel == 1 ? " outside of evals" : "" ), ".\n" | ||
| 215 | if $I_m_init; | ||
| 216 | |||
| 217 | # XXX This is probably obsolete, given that diehard() is gone. | ||
| 218 | print $OUT "Dump printed too.\n" if $dieLevel > 2; | ||
| 219 | } ## end if ($dieLevel) | ||
| 220 | |||
| 221 | # Put the old one back if there was one. | ||
| 222 | elsif ($prevdie) { | ||
| 223 | $SIG{__DIE__} = $prevdie; | ||
| 224 | print $OUT "Default die handler restored.\n"; | ||
| 225 | } else { | ||
| 226 | undef $SIG{__DIE__}; | ||
| 227 | print $OUT "Die handler removed.\n"; | ||
| 228 | } | ||
| 229 | } ## end if (@_) | ||
| 230 | $dieLevel; | ||
| 231 | } ## end sub dieLevel | ||
| 232 | |||
| 233 | =head2 C<signalLevel> | ||
| 234 | |||
| 235 | Number three in a series: set C<signalLevel> to zero to keep your own | ||
| 236 | signal handler for C<SIGSEGV> and/or C<SIGBUS>. Otherwise, the debugger | ||
| 237 | takes over and handles them with C<DB::diesignal()>. | ||
| 238 | |||
| 239 | =cut | ||
| 240 | |||
| 241 | sub signalLevel { | ||
| 242 | if (@_) { | ||
| 243 | $prevsegv = $SIG{SEGV} unless $signalLevel; | ||
| 244 | $prevbus = $SIG{BUS} unless $signalLevel; | ||
| 245 | $signalLevel = shift; | ||
| 246 | if ($signalLevel) { | ||
| 247 | $SIG{SEGV} = \&DB::diesignal; | ||
| 248 | $SIG{BUS} = \&DB::diesignal; | ||
| 249 | } | ||
| 250 | else { | ||
| 251 | $SIG{SEGV} = $prevsegv; | ||
| 252 | $SIG{BUS} = $prevbus; | ||
| 253 | } | ||
| 254 | } ## end if (@_) | ||
| 255 | $signalLevel; | ||
| 256 | } ## end sub signalLevel | ||
| 257 | |||
| 258 | 1; |