Re: breaking at for-loop test line

Andre Ancelin
2004-07-29T16:29:46Z

On Thursday 29 July 2004 11:29, Daniel Jacobowitz wrote:

This works.

7 for (i = 0; i < 3;
8 i++) {
9 cout << i << endl;
10 }

with a breakpoint at 8. It will stop for each pass. If you try

7 for (i = 0;
8 i < 3;
9 i++) {
10 cout << i << endl;
11 }

with breakpoints at 7, 8, & 9, you stop at each line once, then at line 9 two
more times, then out. It does not, however, stop again at line 8 for each
pass or at terminus (?). You can, however, add a breakpoint on line 11 and
you will break at the next statement after line 11 upon terminus.

Some might call this style ugly or wasteful, but I have found it much more
debug friendly to leave all conditional tests on lines by themselves. Another
simple example:

if (i < 6 ||
i > 9)
{
<do_something>
}

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

--
Andre Ancelin
VP/CTO Adtec Digital, Inc
andrea@adtecinc.com