3

Is there some way or technique to determine which code cycles in the program? Like if my application gets on an infinite loop and I would like to know exactly where it loops?

9
  • 4
    Tried a debugger? – Igor Skochinsky Nov 16 '13 at 0:32
  • 1
    Or Poor Man's Debugging: Throw in a print "made it to line XXX" after each major piece of code? – Jongware Nov 16 '13 at 3:26
  • 2
    Once in the loop, attach the debugger to the process. gdb can do that (see here) and I suppose that any debugger can do the same as well. – perror Nov 16 '13 at 8:21
  • 1
    Thanks, never knew (noticed) that the debugger points to the infinite loop after attaching. – Wiggler Jtag Nov 16 '13 at 14:53
  • 1
    You need to be a lot more specific. Is this Java, C, assembler, Javascript, on Android, Linux, Windows playstation, System/370? – Ribo Dec 29 '13 at 16:10
2

Answering here in case other folks find this question and need a more detailed answer.

A couple of assumptions up front. OP said "application", so I'm assuming an OS that 1) has kernel/user mode, 2) is not a closed system (ie not an appliance or device), 3) has some notion of "process", and 4) has a debugger that allows you to debug a specific process (either an existing process or one the debugger launches). Think Windows, Linux.

For the sake of simplicity let's assume the app is single threaded. After breaking into the debugger a processor will be executing in the infinite loop (with some caveats).

(If the app is multithreaded then you need to switch to the context of each thread currently running and determine if an infinite loop is being executed by that thread. While unlikely, it is possible for an app to have multiple simultaneous infinite loops. Its also possible to have an infinite loop in a callback function in a thread the app didn't create.)

In many cases an infinite loop is just a few instructions (for the sake of discussion say < 50).

With a small infinite loop you can just single step (stepping over function calls) through the loop and characterize the whole loop. If the loop is more than say 8 instructions, it might help if you write down each chunk of assembly that has a single entry point and a single exit (a so called basic block) on a piece of paper and draw arrows between the basic blocks that represent all the jumps (and returns). Any arrows leading to code that doesn't get executed when you step through the loop are potential loop exits.

If there are no such arrows then the code was written to be an infinite loop - the loop may be intentional or the coder just made a coding error. If there are arrows out then they will (usually) be preceded by some sort of test/compare instructions. You need to investigate why those test/compare instruction never result in the loop being exited.

If the case that you've got a "big" infinite loop, stepping through each instruction becomes impractical (or at least tiresome). To find the reason for the loop you'll need to understand the code involved well enough to understand what the infinite loop is trying to accomplish and why the loop exit condition(s) is not being met. Here you'll use other RE techniques to understand the code making up the loop. Once you have that level of understanding you can focus on understanding why the loop exit conditions aren't being met.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.