Re: breaking at for-loop test line

Daniel Jacobowitz
2004-07-29T15:30:59Z

On Wed, Jul 28, 2004 at 04:09:14PM -0700, Allen Hopkins wrote:
> Is there a way to break at the test statement of a for-loop
> on every iteration, the same way a while-loop works? Here's
> what I mean:
>
> 1 #include <iostream.h>
> 2
> 3 int main(int argc, char* argv[])
> 4 {
> 5 int i = 0;
> 6
> 7 for (i = 0; i < 3; i++) {
> 8 cout << i << endl;
> 9 }
> 10
> 11 while (i < 6) {
> 12 cout << i++ << endl;
> 13 }
> 14
> 15 exit(0);
> 16 }
>
> If I set a breakpoint at line 7, and another at line 11,
> and "run" and "continue" until exit, it will only break once
> on the for-loop, but it will break on each iteration of the
> while-loop. How does this make sense? Is there any way to
> get a breakpoint at the top of the for-loop to act like the
> while-loop?

What Atul wrote is basically correct; GDB can only pick one breakpoint
location for a particular line number, and it picks the beginning of
the line.

It would be nice if there were a way to set a breakpoint at the
condition, but we don't have enough information to know for sure where
the "condition" part is; and we don't want to always set breakpoints at
every part of a line, because it makes breakpoints on simple statements
that get broken up by ptimization very awkward to work with.

I've been thinking for a while about a better interface for this, but I
haven't come up with one yet.

In the mean time, you can take a look at the disassembly, figure out
where the condition is, and set a breakpoint there using break *<addr>.
GDB could also make it much easier to figure out where the condition is
than it does now...

--
Daniel Jacobowitz