By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,955 Members | 1,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,955 IT Pros & Developers. It's quick & easy.

finding global variables

P: n/a
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
Nov 9 '08 #1
Share this Question
Share on Google+
11 Replies


P: n/a
sinbad wrote:
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

Let's say I have source code with the following global:

....
int myglob = 1;
....

If you perform 'nm' on the binary, the output is

$ nm mybin | grep glob
080489e0 t __do_global_ctors_aux
08048510 t __do_global_dtors_aux
08049c60 D myglob
$

From the man page for nm...

"D" The symbol is in the initialized data section (where globals are kept)

Hope that helps,
Missaka Wijekoon
Nov 9 '08 #2

P: n/a
sinbad <si***********@gmail.comwrites:
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.
Nov 9 '08 #3

P: n/a
Nate Eldredge <na**@vulcan.lanwrites:
sinbad <si***********@gmail.comwrites:
>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
--
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 /.
Nov 9 '08 #4

P: n/a
On Nov 9, 5:37*pm, Phil Carmody <thefatphil_demun...@yahoo.co.uk>
wrote:
Nate Eldredge <n...@vulcan.lanwrites:
sinbad <sinbad.sin...@gmail.comwrites:
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
--
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
Nov 11 '08 #5

P: n/a
sinbad <si***********@gmail.comwrites:
On Nov 9, 5:37*pm, Phil Carmody <thefatphil_demun...@yahoo.co.uk>
wrote:
>Nate Eldredge <n...@vulcan.lanwrites:
>sinbad <sinbad.sin...@gmail.comwrites:
>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.
<snip>
>Phil
--
I tried the Vista speech...
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?

--
Ben.
Nov 11 '08 #6

P: n/a
Ben Bacarisse wrote:
sinbad <si***********@gmail.comwrites:
>On Nov 9, 5:37 pm, Phil Carmody <thefatphil_demun...@yahoo.co.uk>
wrote:
>>Nate Eldredge <n...@vulcan.lanwrites:
sinbad <sinbad.sin...@gmail.comwrites:
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.
....
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.
Nov 11 '08 #7

P: n/a
James Kuyper <ja*********@verizon.netwrites:
Ben Bacarisse wrote:
>sinbad <si***********@gmail.comwrites:
>>On Nov 9, 5:37 pm, Phil Carmody <thefatphil_demun...@yahoo.co.uk>
wrote:
Nate Eldredge <n...@vulcan.lanwrites:
sinbad <sinbad.sin...@gmail.comwrites:
>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.
...
>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.
Ah, OK. Then ctags + the compiler's output!

--
Ben.
Nov 11 '08 #8

P: n/a

"Ben Bacarisse" <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
sinbad <si***********@gmail.comwrites:
On Nov 9, 5:37 pm, Phil Carmody <thefatphil_demun...@yahoo.co.uk>
wrote:
Nate Eldredge <n...@vulcan.lanwrites:
sinbad <sinbad.sin...@gmail.comwrites:

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.
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.
Well, not if you're a C front end implementation.
Certainly writing one is hard.
More
specifically i want to do this by writing a script for example
in perl.
You're not going to do this with Perl. You need a full blown C front
end to parse the source code correctly, and collect the data you need.
Feels like a hard problem to me. For starters, borrow a C
pre-processor so you don't have to do that part.
"Borrow" (for some definition of borrow) a full blown C front end.
You can do this with GNU if you want to spend time hacking it.

An alternative C front end is
http://www.semanticdesigns.com/Produ...CFrontEnd.html

It collects all the information needed to do this task.

--
Ira Baxter, CTO
www.semanticdesigns.com
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?

--
Ben.

Nov 11 '08 #9

P: n/a
On 11 Nov, 13:02, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
sinbad <sinbad.sin...@gmail.comwrites:
On Nov 9, 5:37*pm, Phil Carmody <thefatphil_demun...@yahoo.co.uk>
wrote:
Nate Eldredge <n...@vulcan.lanwrites:
sinbad <sinbad.sin...@gmail.comwrites:
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.
<snip>
Phil
--
I tried the Vista speech...

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.
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.
>*Can't you just
compile and filter the output?

--
Nick Keighley
Nov 12 '08 #10

P: n/a
Nick Keighley wrote:
) On 11 Nov, 13:02, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
)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
Nov 12 '08 #11

P: n/a
On 12 Nov, 10:49, Willem <wil...@stack.nlwrote:
Nick Keighley wrote:

) On 11 Nov, 13:02, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
)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.

--
Nick Keighley

Nov 12 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.