1

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 ...

Michael J. Barber
  • 24,518
  • 9
  • 68
  • 88
bobibob
  • 41
  • 1
  • 5

3 Answers3

3

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!

mac
  • 42,153
  • 26
  • 121
  • 131
  • Hmm well I was thinking about watching process resources ... like CPU. And if the process requires a lot of CPU, suppose that there is an infinite loop and kill it. An idea to watch CPU use for a process ? – bobibob Dec 06 '11 at 11:23
  • @bobibob that would be a solution be a solution for a process that consume much CPU, but not specifically (limited to) for an infinite loop. Maybe you want to ask about this in another (more general) question? – The Student Dec 06 '11 at 11:30
2

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.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
1

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.

jcollado
  • 39,419
  • 8
  • 102
  • 133
  • Exactly whant I want ! Watch CPU usage ! An idea to do it in Python with or without winappdbg ? – bobibob Dec 06 '11 at 11:24
  • 1
    *"Infinite loops usually consume 100% CPU while well-behaving programs don't"* - That really depends from what kind of programs you are writing. Games and numerical simulations also tend to eat up all of your CPU resources... – mac Dec 06 '11 at 11:27
  • In my case, the CPU usage could be a solution. – bobibob Dec 06 '11 at 11:27
  • @bobibob You don't really need winappdbg for that. In linux you can use `top` and in windows the task manager. – jcollado Dec 06 '11 at 11:35
  • I want to do it automaticly ... I allready use Python (and winappdbg) to catch exception, so It would be great to detect CPU usage with Python also. – bobibob Dec 06 '11 at 11:40
  • @bobibob To do that from python, please have look at the answers to this [question](http://stackoverflow.com/q/276281/183066). – jcollado Dec 06 '11 at 11:46
  • 1
    @mac I edited the answer to make clear that 100% CPU isn't useful for CPU intensive tasks. Thanks for your comment. – jcollado Dec 06 '11 at 11:49