Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > finding global variables

Reply
Thread Tools

finding global variables

 
 
sinbad
Guest
Posts: n/a
 
      11-09-2008
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


 
Reply With Quote
 
 
 
 
Nate Eldredge
Guest
Posts: n/a
 
      11-09-2008
sinbad <> writes:

> 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.
 
Reply With Quote
 
 
 
 
Phil Carmody
Guest
Posts: n/a
 
      11-09-2008
Nate Eldredge <> writes:
> sinbad <> writes:
>
>> 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 /.
 
Reply With Quote
 
sinbad
Guest
Posts: n/a
 
      11-11-2008
On Nov 9, 5:37*pm, Phil Carmody <thefatphil_demun...@yahoo.co.uk>
wrote:
> Nate Eldredge <n...@vulcan.lan> writes:
> >sinbad <sinbad.sin...@gmail.com> writes:

>
> >> 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
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-11-2008
sinbad <> writes:

> On Nov 9, 5:37Â*pm, Phil Carmody <thefatphil_demun...@yahoo.co.uk>
> wrote:
>> Nate Eldredge <n...@vulcan.lan> writes:
>> >sinbad <sinbad.sin...@gmail.com> writes:

>>
>> >> 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.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-11-2008
Ben Bacarisse wrote:
> sinbad <> writes:
>
>> On Nov 9, 5:37 pm, Phil Carmody <thefatphil_demun...@yahoo.co.uk>
>> wrote:
>>> Nate Eldredge <n...@vulcan.lan> writes:
>>>> sinbad <sinbad.sin...@gmail.com> writes:
>>>>> 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.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-11-2008
James Kuyper <> writes:

> Ben Bacarisse wrote:
>> sinbad <> writes:
>>
>>> On Nov 9, 5:37 pm, Phil Carmody <thefatphil_demun...@yahoo.co.uk>
>>> wrote:
>>>> Nate Eldredge <n...@vulcan.lan> writes:
>>>>> sinbad <sinbad.sin...@gmail.com> writes:
>>>>>> 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.
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      11-12-2008
On 11 Nov, 13:02, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> sinbad <sinbad.sin...@gmail.com> writes:
> > On Nov 9, 5:37*pm, Phil Carmody <thefatphil_demun...@yahoo.co.uk>
> > wrote:
> >> Nate Eldredge <n...@vulcan.lan> writes:
> >> >sinbad <sinbad.sin...@gmail.com> writes:

>
> >> >> 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
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      11-12-2008
Nick Keighley wrote:
) On 11 Nov, 13:02, Ben Bacarisse <ben.use...@bsb.me.uk> 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
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      11-12-2008
On 12 Nov, 10:49, Willem <wil...@stack.nl> wrote:
> Nick Keighley wrote:
>
> ) On 11 Nov, 13:02, Ben Bacarisse <ben.use...@bsb.me.uk> 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.

--
Nick Keighley



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initialize global variable before any other global variables jubelbrus C++ 5 07-20-2007 06:38 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
Global variables on par with ASP's global.asa Wayne ASP .Net 2 11-11-2003 10:58 PM
Global variables - application variables vs include file mark4asp ASP General 1 09-03-2003 01:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57