Skip to content

Instantly share code, notes, and snippets.

@xtetsuji
Created March 2, 2017 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xtetsuji/1726a58807473caa4be22afcd3316263 to your computer and use it in GitHub Desktop.
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.
#!/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