How to create an instance of a particular class earlier than main(), earlier than any other instance (including static and global variables, including in the static and dynamic libraries (including libraries provided by 3rd parties))?
I'm dealing with a project with a number of classes that during construction can cause an error, e.g. access a NULL pointer. Any such error causes a signal to be sent to the app. I have a signal handler that catches the signals, shows the stack trace of the offending thread, and invokes the default signal handler which results in the core dump generated, etc.
However some of such error-causing instances are created as the global variables and static variables of the classes. I.e. they are constructed and cause a signal earlier than main() is entered.
To catch such signals I need to register my signal handler earlier than in main(), i.e. I need to create an instance (that will register the signal handler) also as a global or class-static variable, and I need to guarantee that such an instance is created/constructed earlier than any other instance.
How to get that done?
To register a signal handler I use sigaction().
To show the stack trace I use backtrace(), backtrace_symbols(), abi::__cxa_demangle().