Reputation: 6649
I have a C code base and need to programmatically identify all of the global variables used. I think that gcc can be made to produce a memory map file but don't know how and this might let me see which variables are global? Any help would be appreciated.
Cheers
Upvotes: 3
Views: 1770
Reputation: 2807
I liked the gdb's "info variables" to do this.
Listed here: Global Variable identification in C code base
Upvotes: 0
Reputation: 35816
You can extract such information from your object files with nm
.
nm *.o | grep OBJT | grep GLOB
EDIT
Command above is for Solaris' nm (SUNWbtool package). For portability, nm
has a parameter to choose the output format:
nm -f [posix|bsd|sysv] *.o
Upvotes: 6
Reputation: 34967
If you have dwarfdump
and your binary contains DWARF debug information (not stripped), you can check out content of .debug_pubnames
DWARF section by running
dwarfdump -p mybinary | awk '{print $2}'
It will produce set of lines with names of global symbols, one per line.
Upvotes: 0
Reputation: 15486
The option to output memory map is -M with the linker, so to get it from gcc you have to use gcc .... -Xlinker -M
.
Another way to to get these is to use the ctags
program. It can not only tag the available functions, but also the available global variables (and does not collect the statics, this is different to memory map). As it doesnt compile everything this should also be faster than the gcc approach (if you have to compile, you would get that of course for free).
Upvotes: 1
Reputation: 179799
Why do you need to know this? For a lot of purposes, static data (whether at file or function scope) should be grouped with the globals.
In general, globals do show up as data, not code in the linker map. Stack variables and heap-allocated variables do not. (Of course, the pointer to heap allocated data can be a global; that's just the regular distinction in C between a pointer and what it points to.)
Upvotes: 0