gizgok
gizgok

Reputation: 7649

How to go at particular value of counter in for loop using gdb?

I have a for loop of about 100 odd values. I want to have a breakpoint where I can set some value for the iterator variable and then directly go on that program execution state.

For example

for(int i=0;i<500;i++)
{ 
  doSomething();
}

Here I want to have a breakpoint on i=100; and step through all values from 0 to 99 in one go. Is this possible in gdb and how do I do it?

Upvotes: 1

Views: 2033

Answers (3)

askmish
askmish

Reputation: 6684

A conditional breakpoint is one that only transfers control to gdb only when a certain condition is true.

This can be useful when you only want gdb control after say 10 iterations of a loop.

To set a condition on a breakpoint, use the condition command with the number of the breakpoint followed by the condition on which to trigger the breakpoint.

Here is an example where setting a conditional breakpoint that will only be triggered when
the "condition (i >= 10) is true":

(gdb) break 28                                  # set breakpoint at line 28
(gdb) info break                                # list breakpoint information
  Num Type           Disp Enb Address    What
   1   breakpoint     keep y   0x080588a3 in loopit at loops.c:28

(gdb) condition 1 (i >= 10)                # set condition on breakpoint 1
(gdb) run   (or write `continue` if already running) 

Upvotes: 2

Kevin
Kevin

Reputation: 56129

In gdb you can set conditions on breakpoints.

break line if i == 100

Where "line" is the appropriate line number.

Upvotes: 8

IronMensan
IronMensan

Reputation: 6831

There is probably a better way, but I have gotten a lot of mileage out of doing things like this:

if (i == 100) {
    int breakpoint = 10;
}

Upvotes: 1

Related Questions