Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Subroutine Call Stack - For Entire Program

by Anonymous Monk
on Aug 13, 2009 at 21:54 UTC ( #788453=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

My objective is to find out "what subroutine called what subroutine" & accomplish this for the *entire* perl program. To clarify this objective: for each subroutine, I am trying to find the parent & do this for every subroutine that exists. This would allow me to get a subroutine stack of my entire program. My program - including my own modules - is ~ 10,000 lines. The number of subroutines is ~ 100 (one hundred).

I am aware of the "caller" function, which provides access to the call stack. Furthermore, in order to find out who the "parent" is, something like this can be used: $parent = ( caller(1) )3

However, the above requires inserting the aforementioned code snippet into each & every subroutine (in order to achieve the objective).

Question: is there an easy way to obtain my objective without inserting the caller function all over my code? Is there an easier alternative?

Thanks in advance for any help.

Comment on Subroutine Call Stack - For Entire Program
Re: Subroutine Call Stack - For Entire Program
by ikegami (Pope) on Aug 13, 2009 at 22:13 UTC
    What's your goal? A profiler might to do the trick.
Re: Subroutine Call Stack - For Entire Program
by moritz (Cardinal) on Aug 13, 2009 at 22:38 UTC
    If a profiler doesn't cut it, you might want to look at dtrace support in perl (if you platform supports it), maybe you can generate call graphs with it somehow.

    Anyway, this looks like an XY Problem to me - what's your ultimate goal?

Re: Subroutine Call Stack - For Entire Program
by pemungkah (Priest) on Aug 15, 2009 at 21:30 UTC
    You'll want to implement a debugger module. Not a replacement for the debugger, but a module that implements the right subs in the DB namespace to capture your trace data.

    For example, the way the Perl debugger does this: the DB::sub subroutine gets called automatically by the interpreter for every sub call in the progtram being run; DB::sub captures caller()'s output, and then uses an eval() sub in the debugger's namespace (not the core eval!) to run the subroutine in the caller's context. When DB::eval is over, we pop the stack to remove the caller info for this call.

    Meanwhile, DB::DB gets called by the interpreter after every line, so you can set a variable in the DB:: namespace in your main program to signal DB::DB that now's the time to dump the stack.

    if you just need a quick-and-dirty one-time, you can put a $DB::single=1 in your code at the point where you want to stop and do your traceback, and then run your code under the debugger.

    It will stop at the $DB::single, at which point you can issue the T command to get the call trace.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2014-09-02 12:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (22 votes), past polls