Code coverage for _constructor()/_destructor()'s?

Jay Vaughan
Mon Jun 28 23:21:00 GMT 2010

Hi there gcc-help'ers,

I have a situation where I am currently trying to obtain 100% coverage on my project, 
which consists of a number of individual programs linking against a standardized, internal 

This library (lets call it contains code that *must* be executed 
before the linked-in programs main() function, and any and all exit-/signal- handlers.  To 
that end, I have the following functions defined in my library:

void lib_bg_constructor(void)  __attribute__ ((constructor(101)));
void lib_bg_destructor(void)  __attribute__ ((destructor(101)));

The constructor() does a *bunch* of things before the main() is called, and it is this, 
and the subsequent destructor(), in which I need to attain 100% complete code coverage.

However, I am finding it nearly impossible to get this coverage information from my 
constructor, which is designed to exit() as quickly as possible if certain conditions are 
not met - the purpose of the library is to maintain a policy for how applications are 
launched, and if the policy is not fulfilled, exit() is called for immediate death.  I 
have tried manipulating priority attributes, but it appears that GNU coverage 
functionality does not attend to constructor/destructor priorities (I'm using gcc v4.1.2 

Are there methods of using GNU coverage which will allow me to save my coverage 
information for the cases where death is warranted by my guardian library?  Is there some 
way for me to manually force coverage to be written prior to doing my big exit() call in 
the constructor?  Has anyone else run into this problem and solved it?

Any hints about this issue are greatly appreciated .. thanks in advance for any help you 
can provide.

Jay Vaughan

More information about the Gcc-help mailing list