Unix Programming - Unix/Linux/Perl Tool to determine if function is defined but not called ?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2006 > Unix/Linux/Perl Tool to determine if function is defined but not called ?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Unix/Linux/Perl Tool to determine if function is defined but not called ?
bakul.joshi@gmail.com

2006-03-17, 2:57 am

Is there a tool to determine whether a function is defined but not
being called in a program?The program can be in any
language(C/Perl/Unix)..I studied the nm tool that does the profiling of
the object file but that gives all the functions that are defined
(whether or not they are called in the file)...

Andrei Voropaev

2006-03-17, 2:57 am

On 2006-03-17, bakul.joshi@gmail.com <bakul.joshi@gmail.com> wrote:
> Is there a tool to determine whether a function is defined but not
> being called in a program?The program can be in any
> language(C/Perl/Unix)..I studied the nm tool that does the profiling of
> the object file but that gives all the functions that are defined
> (whether or not they are called in the file)...


Assuming you are talking about C. Probably you'll like the ctags
program. Not only you can see if some function is used at all, but you
can also see at which specific place(s) it is used.
http://ctags.sourceforge.net/

Well, if you want to find all the functions that are not used, then
probably you'll have to write a script that gets the list of all defined
functions (maybe from nm output) and then uses ctag to do the checking

--
Minds, like parachutes, function best when open
Simon Elliott

2006-03-17, 7:50 am

On 17/03/2006, bakul.joshi@gmail.com wrote:

> Is there a tool to determine whether a function is defined but not
> being called in a program?The program can be in any
> language(C/Perl/Unix)..I studied the nm tool that does the profiling
> of the object file but that gives all the functions that are defined
> (whether or not they are called in the file)...


Lint will do this (eg http://www.gimpel.com/html/flex.htm) but might be
overkill if you only want to find unused functions.

The map files produced by one of the W32 linkers I use (BCB) can be
used to work out which functions have been called. Can you do something
similar with gcc?

--
Simon Elliott http://www.ctsn.co.uk
Ben C

2006-03-17, 5:55 pm

On 2006-03-17, Simon Elliott <> wrote:[vbcol=seagreen]
> On 17/03/2006, bakul.joshi@gmail.com wrote:
>

I use nm, grep for the capital Ts, filter that a bit with something,
feed it to cscope -L -3, and look for empty strings coming back.

Unused static functions will get you a compiler warning on gcc.
Waldek Hebisch

2006-03-17, 8:53 pm

bakul.joshi@gmail.com wrote:
> Is there a tool to determine whether a function is defined but not
> being called in a program?The program can be in any
> language(C/Perl/Unix)..I studied the nm tool that does the profiling of
> the object file but that gives all the functions that are defined
> (whether or not they are called in the file)...
>


There is no general solution, the problem is unsolvable (it is equivalent
to well known halting problem). If you limit yourself to a "static" language
like C, then it is possible to get partial answer which in practice may be
good enough. For example, if you compile program using gcc-4.0 and give
it '-O2' option, then gcc will remove provably "useless" functions (and
variables). So if you compile twice once using '-O0' option and once
using '-O2' and compare what nm gives you you will see unused functions.
Note that this does not work for global functions -- gcc sees only one
file and can not tell if global functions are used.

Another possiblility is to use program like gprof and gcov: they will
tell you which functions were used dureing given program run. While
function not used in one run may be used in another run, this can give
you a list of suspects worth closer examination.

--
Waldek Hebisch
hebisch@math.uni.wroc.pl
Ben C

2006-03-17, 8:53 pm

On 2006-03-17, Ben C <spamspam@spam.eggs> wrote:
> On 2006-03-17, Simon Elliott <> wrote:
>
> I use nm, grep for the capital Ts, filter that a bit with something,
> feed it to cscope -L -3, and look for empty strings coming back.
>
> Unused static functions will get you a compiler warning on gcc.


Oh and I forgot to mention, this will miss functions assigned to
variables and called indirectly. If you think there might be some of
those, it's a bit harder, but you can do something similar but with
cscope -L -0. Deciding if the indirected functions are actually called
is harder still...

There are such things as "dead code removing linkers", I think gcc may
have an option for this, that would probably be the most reliable thing
to use really.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com