I have a large application written in C + POSIX there are many functions which a
ID: 660415 • Letter: I
Question
I have a large application written in C + POSIX there are many functions which are never called inside it. However, due to the size of the code it is difficult to track them manually.
Some people have suggested to use gcc with -Wunused and lto, but it didn't returned any used functions, whereas I continue to find and remove some manually.
So I think I need a code coverage tool for analysing the program at run-time. Peoples suggested me gcov or valgrind, but I was unable to find how to use them to print a list of dead functions. gcov alone revealed only 68% of the functions compiled are used, but I don't have any way to list them.
So does someone know a good tool, and if so tell me how exactly I can use it for that purpose (a command line example would be welcome)?
I removed all functions which are not used in the source code. Only functions like this remain inside the source code :
if(conditional statement) {
some stuff;
dead_function();
some_stuff
}
Where conditional statement is never true at run time, and the removal of dead_function() would need to the removal of the statement to avoid undefined errors.
Explanation / Answer
You can use splint with the alluse flag to check for unused functions but personally I would use doxygen to produce a call map - any functions that have no parents are probably unused - just look out for any functions that are in tables of functions that might not be directly called but things like state machines might invoke from the table index.
Doxygen is an invaluable tool for dealing with large code bases and is well worth learning how to use in any case, it is free and available for multiple platforms, it also tends to encourage documenting your code as you go.
In the case of code that is called but only from unreachable code you will have to use a full static analysis tool like LDRA, (costly), that will point you to unreachable code. In this case it is better to remove all unreachable code first then look for uncalled functions. Alternatively you will need a test suite that you are sure exercises 100% of the functionality - then you can use a profiler or coverage tool like gcov on your program while running your test suite. If your test has exercised all of your functionality and you have portions with 0% coverage then they are uncalled but you will then have to find the calls to them that are unreachable and remove that code for the linker not to complain anyway.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.