Created
March 2, 2017 09:14
-
-
Save xtetsuji/1726a58807473caa4be22afcd3316263 to your computer and use it in GitHub Desktop.
Getting stacktrace $SIG{__DIE__} hook from outsider code which ignores and trashes all exceptions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use Sys::Syslog; | |
#use Carp; | |
BEGIN { | |
( my $basename = $0 ) =~ s{.*/}{}; | |
openlog "mystacktrace/$basename\[$$]", "ndelay", "user"; | |
$SIG{__DIE__} = sub { | |
my $message = shift; | |
chomp $message; | |
for ( my $i = 1 ; ; $i++ ) { | |
my ($package, $filename, $line, $subroutine) = caller($i) or last; | |
my $syslog_line = "[$i\: $subroutine \@ $filename\:$line]"; | |
$syslog_line .= " " . $message if $i == 1; | |
syslog info => $syslog_line; | |
} | |
die $message . "\n"; | |
}; | |
} | |
### 以下は第三者が書いたコードで、握りつぶされた例外をコード非破壊でスタックトレースを取らないといけないという命題 | |
sub foo { | |
die "something wrong"; | |
} | |
sub bar { | |
foo; | |
} | |
sub baz { | |
bar; | |
} | |
eval { baz(); }; | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment