finding global variables

S

sinbad

hi,
I've a very large C program consisting of hundred of files; I want to
know what are all
the global variables defined in the program. how do i do this.

thanks
 
N

Nate Eldredge

sinbad said:
hi,
I've a very large C program consisting of hundred of files; I want to
know what are all
the global variables defined in the program. how do i do this.

One way would be to compile them and then extract this information from
the object files. On a Unix system, this would probably be easiest to
do by parsing the output of nm(1). On my FreeBSD system, I'd do

nm *.o |grep '[0-9A-Fa-f]* [BCDGRS]' |cut -d ' ' -f 3

Testing this, it appears that it will miss global constants, i.e., a
`const' variable with external linkage, because they get stored in the
..text section. Changing the above regexp to '[0-9A-Fa-f]* [BCDGRST]'
would include them, but would also include all functions with external
linkage, which might not be what you want. However, it's a start, I
guess.
 
P

Phil Carmody

Nate Eldredge said:
sinbad said:
hi,
I've a very large C program consisting of hundred of files; I want to
know what are all
the global variables defined in the program. how do i do this.

One way would be to compile them and then extract this information from
the object files. On a Unix system, this would probably be easiest to
do by parsing the output of nm(1). On my FreeBSD system, I'd do

nm *.o |grep '[0-9A-Fa-f]* [BCDGRS]' |cut -d ' ' -f 3

Testing this, it appears that it will miss global constants, i.e., a
`const' variable with external linkage, because they get stored in the
.text section. Changing the above regexp to '[0-9A-Fa-f]* [BCDGRST]'
would include them, but would also include all functions with external
linkage, which might not be what you want. However, it's a start, I
guess.

It also misses the global variables which have not been
included in the particular build configuration. If you're
auditing source code, then this:

#if defined(__MIPS__) && defined(__GCC__) && defined(PROFILING) && (PROFILING>2)
int tally;
#endif

definitely still has a global variable, even if you've never built
for MIPS for years. The solution, however, depends a lot on how
sane and strictly adhered to your coding standards are. Again,
I know that a grep would solve the problem on my own source code
trees, as my global variables are always declared in the same way,
and in a way that functions, function-local variables, function
parameters, struct members, ... aren't,

Phil
 
S

sinbad

One way would be to compile them and then extract this information from
the object files.  On a Unix system, this would probably be easiest to
do by parsing the output of nm(1).  On my FreeBSD system, I'd do
nm *.o |grep '[0-9A-Fa-f]* [BCDGRS]' |cut -d ' ' -f 3
Testing this, it appears that it will miss global constants, i.e., a
`const' variable with external linkage, because they get stored in the
.text section.  Changing the above regexp to '[0-9A-Fa-f]* [BCDGRST]'
would include them, but would also include all functions with external
linkage, which might not be what you want.  However, it's a start, I
guess.

It also misses the global variables which have not been
included in the particular build configuration. If you're
auditing source code, then this:

#if defined(__MIPS__) && defined(__GCC__) && defined(PROFILING) && (PROFILING>2)
int tally;
#endif

definitely still has a global variable, even if you've never built
for MIPS for years. The solution, however, depends a lot on how
sane and strictly adhered to your coding standards are. Again,
I know that a grep would solve the problem on my own source code
trees, as my global variables are always declared in the same way,
and in a way that functions, function-local variables, function
parameters, struct members, ... aren't,

Phil
--
I tried the Vista speech recognition by running the tutorial. I was
amazed, it was awesome, recognised every word I said. Then I said the
wrong word ... and it typed the right one. It was actually just
detecting a sound and printing the expected word! -- pbhj on /.

Hi,
Is there any way to do it by parsing the code statically. More
specifically i want to do this by writing a script for example
in perl.

thanks
 
B

Ben Bacarisse

It is not a good idea to quote a sig (unless you are commenting on
it).
Is there any way to do it by parsing the code statically.

Of course! C implementations do this all the time -- the trouble is
it is hard.
More
specifically i want to do this by writing a script for example
in perl.

Feels like a hard problem to me. For starters, borrow a C
pre-processor so you don't have to do that part.

If your sources are clear and well-structured and you feel you can
take some shortcuts, then the problem might not be too complex.

The odd thing in all this is that a C implementation is obliged to
tell you if you have references to undefined objects. Can't you just
compile and filter the output?
 
J

James Kuyper

Ben said:
....
The odd thing in all this is that a C implementation is obliged to
tell you if you have references to undefined objects. Can't you just
compile and filter the output?

That would tell him which objects aren't defined, but are referenced. It
sounds to me like he wants a list of objects which are defined, whether
or not they've been referenced.
 
B

Ben Bacarisse

James Kuyper said:
That would tell him which objects aren't defined, but are
referenced. It sounds to me like he wants a list of objects which are
defined, whether or not they've been referenced.

Ah, OK. Then ctags + the compiler's output!
 
N

Nick Keighley

It is not a good idea to quote a sig (unless you are commenting on
it).


Of course!  C implementations do this all the time -- the trouble is
it is hard.


Feels like a hard problem to me.  For starters, borrow a C
pre-processor so you don't have to do that part.

If your sources are clear and well-structured and you feel you can
take some shortcuts, then the problem might not be too complex.

The odd thing in all this is that a C implementation is obliged to
tell you if you have references to undefined objects.

I have encountered ones that don't (ok, technically that isn't
a C implementation). Missing dynamically linked functions
were not dectected until run time.
 
W

Willem

Nick Keighley wrote:
)> The odd thing in all this is that a C implementation is obliged to
)> tell you if you have references to undefined objects.
)
) I have encountered ones that don't (ok, technically that isn't
) a C implementation). Missing dynamically linked functions
) were not dectected until run time.

Technically, it *is* a C implementation, it's just that (part of)
the last compilation step is deferred to run time.

^_^


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
N

Nick Keighley

Nick Keighley wrote:

)> The odd thing in all this is that a C implementation is obliged to
)> tell you if you have references to undefined objects.
)
) I have encountered ones that don't (ok, technically that isn't
) a C implementation). Missing dynamically linked functions
) were not dectected until run time.

Technically, it *is* a C implementation, it's just that (part of)
the last compilation step is deferred to run time.

is that actually standard compliant? Translation Phase 8
says

[paraphrase] all references are resolved, all translator output is
collected into a program imagewhich conatins information needed for
execution. [end]

yes if TP8 is deferred until just before the program starts
to run the that's ok. But I think I've seen cases where it wasn't
until the function was actually called that things went wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top