Find the number of function/macro calls under a compile time macro
I have this problem that i have to find how many times a specific macro/function is being called in my code? I know you might think why dont i run a find/grep. But my problem is that the function/macro being called is under a specific compile time macro. So i would li开发者_运维知识库ke to find the number of calls only when this specific compile time macro is enabled. And i want to find number of calls in the whole code (static analysis, not runtime). The codebase is quite huge (millions of lines of code) and fully in C on linux. I was trying to use readelf but i could not really extract the needed info. Any help would be greatly appreciated.
Thanks in advance. ~N
Many C compiliers have a flag to output the pre-processor stage intermediate output (with the relevant conditional compile symbols defined). You could then run find/grep on this output.
How about embedding some unique word(like MARKER
in the following)
in the said macro, and counting the number of times that the word appears?
For example, assuming a code a.c
like the following:
#define A f() MARKER
#define B A; A;
B
The count that the marker appears will be obtained with the commands like the following:
gcc -E a.c | sed -r 's/[^A-Za-z0-9_]+/\xa/g' | grep MARKER | wc -l
After the measurement, MARKER
in the macro will need to be removed
or #define
d to empty as:
#define MARKER
EDIT:
If your build system is make
, you may need to add a new rule like the
following to your current makefiles so as to make preprocessed file:
SRCS = a.c
preprocessed: $(SRCS:.c=.i)
%.i: %.c
$(CC) -E $(CPPFLAGS) -o $@ $<
If all the settings have been done properly,
by the use of make preprocessed
and find -type f -name '*.i'
or similar
command, all the preprocessed files will be obtained.
Probably this isn't an easy job.
If you have fallen into difficulties,
I'd suggest posting that as a new question.
精彩评论