Its very difficult to find whether your code was in an accidental infinite loop before you received a signal like Ctrl-C
. I can suggest some heuristic approaches. Have a variable of sufficiently long size, preferably a global unsigned long long int
, and keep incrementing this variable inside the loops that you suspect might slip into infinite loops on every iteration of the loop. Now, when you receive a signal, check for the value of this variable in the signal handler against a threshold MAX_NUMBER_OF_ITERATIONS
. If the variable exceeds the user defined threshold then declare an infinite loop and exit
else simply continue.
Something on these lines-
#define MAX_NUMBER_OF_ITERATIONS 100000000
unsigned long long checkForEndlessLoop=0;
bool overflow;
void sigHandler (int signum)
{
signal(sig, SIG_IGN); // Ignore it so that another Ctrl-C doesn't appear any soon
if (overflow || (checkForEndlessLoop > MAX_NUMBER_OF_ITERATIONS) )
{
//Something's fishy in the loop.
exit(0);
}
else
{
signal(SIGINT, sigHandler );
}
}
int main ()
{
signal(SIGINT, sigHandler );
for (checkForEndlessLoop=0; SOME_SLOPPY_CONDITION; )
{
//Some processing here
if (++checkForEndlessLoop == 0 )
overflow=true;
}
checkForEndlessLoop=0;
while (SOME_SLOPPY_CONDITION)
{
//Some processing here
if (++checkForEndlessLoop == 0 )
overflow=true;
}
}
Or even simpler is, just ignore the SIGINT
using SIG_IGN
and break
out of the faulty loop as soon as the faulty condition is detected, print out an error message and exit!