Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

code analysis graph

by mostart (Initiate)
on May 20, 2011 at 10:12 UTC ( [id://905898]=perlquestion: print w/replies, xml ) Need Help??

mostart has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have a huge perl Code base (nice bunch of xmlrpc daemons).

Now since some of it became unmaintainable I'm looking around for some Analysis tools which are able to produce some nice graphs of call flows etc. I found some rather old posts on this suggesting tools like B::Xref and GraphViz which I tried but got only Segmentation faults. (perl -MO=Xref,-r myPackage.pm)

Is there any up to date tool for this ?

Thanks for any hint..

Replies are listed 'Best First'.
Re: code analysis graph
by moritz (Cardinal) on May 20, 2011 at 11:25 UTC
Re: code analysis graph
by chrestomanci (Priest) on May 20, 2011 at 12:51 UTC

    Do you want to statically analyse a perl program without running it, and get a call graph for all possible flows of the program, or do you want to run the program normally under some sort of debug environment so that you get back a call graph for your test run?

    As moritz said, if you run a perl program under a profiler, then then you should be able to get a call graph from your profiling tool. The problem is that you only learn about the run you have just done, and not about any exceptional circumstances that you can't simulate. Also if your legacy code has bit rotted to the point where it won't run at all, and you can't easily fix it into a runnable state, then profiling it won't help you.

    Instead it is tempting to want to generate a call graph from static analysis of a perl program without running it. Unfortunately that is a lot harder, not least because (as you will frequency hear) Only perl can parse perl. The meaning of the saying is that only the perl binary (perl.exe) can reliably parse perl, and the only way it can do so is to run the perl program. You can attempt to write a separate parser for perl, but perl.exe is the only definitive perl parser, and the only one guaranteed to be correct. Additionally, if the perl program you are trying to analyse is broken or otherwise buggy, then it's meaning is undefined, so by definition there is no correct outcome to parsing it.

      thanks for your answers monks. NYTProf is a nice tool.
      But still I agree with you what I need is a static analysis without actually running the code.
      I tried B::Xref which caused segfaults only.
      Since compiler backend seems to be the most promising idea aren't there any other tools around ?
        If there are bugs in B::Xref or any other modules, 1) upgrade 2) report bugs
Re: code analysis graph
by koknat (Sexton) on Jul 31, 2021 at 00:31 UTC
    I created a static call graph generation script out of desperation, to untangle an undocumented part of a 30,000-line legacy script, in order to implement an urgent bug fix.

    It reads the source code(s), uses GraphViz to generate a png, and then displays the image on-screen.
    Since it uses simple line-by-line regexes, the formatting must be "sane" so that nesting can be determined (perltidy is useful here if the formatting is in bad shape).
    Also, don't expect miracles such as parsing dynamic function calls, BEGIN blocks, etc.

    The silver lining of a simple regex engine is that it can easily be extended for other languages.
    The tool now supports awk, bash, basic, dart, fortran, go, lua, javascript, kotlin, matlab, pascal, perl, php, python, r, raku, ruby, rust, scala, swift, and tcl.

    https://github.com/koknat/callGraph

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://905898]
Approved by GrandFather
Front-paged by chrestomanci
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (9)
As of 2024-04-23 17:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found