I'm using Python (winappdbg) to monitor a process (the main feature is to catch the exceptions).
But I would like also to detect infinite loops.
Do you know a way to do that with Python? With or without winappdbg ...
I'm using Python (winappdbg) to monitor a process (the main feature is to catch the exceptions).
But I would like also to detect infinite loops.
Do you know a way to do that with Python? With or without winappdbg ...
The only way to detect an infinite loop is to include in the loop itself a test for those conditions that would bring it to never end.
For example: if your loop is supposed to make a variable decrease until it reaches zero (var == 0
would be the exit condition), you should include a test for what I would call "proper working condition". In this example this would be: var < var_of_previous_iteration
.
Another (less deterministic) way to catch infinite loops could be to include a timer and trigger an exception if the loop last longer than a given time limit [this is a hugly hack though, as execution speed could be affected for example by the system being busy doing something else].
HTH!
For the general case, an infinite loop can not be detected (Halting Problem). Maybe for special cases, and I'll let the other answers discuss that, but if all cases need to be taken into account, you're doomed to failure.
Infinite loops usually consume 100% CPU while well-behaving programs don't, so the first thing I'd do is check CPU usage. Unfortunately, this won't let you identify where the infinite loop is in your code.
To do that, you could use a profiler to record the number of times the code is being executed. If you find a really huge number of executions in an unexpected region, then it's worth at least investigating it.
Edit: As pointed out by mac, monitor CPU usage won't be useful for CPU intensive tasks, so it's not something that can be applied in all cases.