Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Forcing stack trace?

by Anonymous Monk
on Sep 21, 2007 at 09:43 UTC ( #640319=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Can I tell perl to produce a stack trace, even when the program doesn't use carp, cluck, et al? This seems to be the default behaviour in other languages like Ruby and Python.

Actually I was trying to install RT and was getting the fatal error: "Can't locate object method "new" via package "RT::Handle" at ... line 218." It'd be nice if I can trace where this error came from.

Comment on Forcing stack trace?
Re: Forcing stack trace?
by Anonymous Monk on Sep 21, 2007 at 09:50 UTC
    Sorry, forgot to mention that I did manage to install RT, but just still wondering abuot the stack trace thing. :-)
Re: Forcing stack trace?
by fenLisesi (Priest) on Sep 21, 2007 at 10:12 UTC
    For your own modules, it easy to write your own stack trace dumping utility using caller. Also see Exception::Class. I don't know what you can do to get dumps from other people's modules, except to hack their source or use the debugger. Cheers.

      You can always roll you own for everything, but it would be much better if the language facilitates it by default. If I don't want it, i can easily catch it.

Re: Forcing stack trace?
by andreas1234567 (Vicar) on Sep 21, 2007 at 10:25 UTC
    I frequently use something like this:
    use strict; use warnings; use Log::Log4perl qw(:easy); Log::Log4perl->easy_init($WARN); my $log = Log::Log4perl->get_logger('stack'); sub show_call_stack { my ( $path, $line, $subr ); my $max_depth = 30; my $i = 1; if ($log->is_warn()) { $log->warn("--- Begin stack trace ---"); while ( (my @call_details = (caller($i++))) && ($i<$max_depth) ) { $log->warn("$call_details[1] line $call_details[2] in function $ +call_details[3]"); } $log->warn("--- End stack trace ---"); } } sub tze { show_call_stack(); } sub bar { return tze();} sub foo { return bar(); } foo(); __END__ $ perl -w 640319.pl 2007/09/21 12:23:10 --- Begin stack trace --- 2007/09/21 12:23:10 640319.pl line 22 in function main::tze 2007/09/21 12:23:10 640319.pl line 23 in function main::bar 2007/09/21 12:23:10 640319.pl line 25 in function main::foo 2007/09/21 12:23:10 --- End stack trace ---
    --
    Andreas
Re: Forcing stack trace?
by DrHyde (Prior) on Sep 21, 2007 at 10:45 UTC
    print Devel::StackTrace->new()->as_string();
Re: Forcing stack trace?
by tinita (Parson) on Sep 21, 2007 at 11:29 UTC
    Can I tell perl to produce a stack trace, even when the program doesn't use carp, cluck, et al?
    i'm not sure what do you mean by that.
    use Carp qw(longmess); print longmess();
    works for me.
      From what I understand, stack trace can be printed if we use the Carp module (e.g. confess). But suppose other people's code just use die() and not use Carp at all?
        You can override the global __DIE__ handler something like (untested):
        use Carp; $SIG{ __DIE__ } = sub { Carp::confess( @_ ) };

        See perlvar for more info on %SIG.

Re: Forcing stack trace?
by educated_foo (Vicar) on Sep 22, 2007 at 06:11 UTC
    Wow... not many helpful responses. I assume your program is exiting from a call to die? You can get a stack trace by overriding it, like so:
    *CORE::GLOBAL::die = sub { require Carp; Carp::confess };
Re: Forcing stack trace?
by nheinric (Novice) on Jan 30, 2012 at 07:14 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://640319]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2014-07-12 23:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (242 votes), past polls