Gabriel
Gabriel

Reputation: 387

How to skip a number of iterations of a loop in GDB?

Suppose I have a loop that will iterate 100 times and I want to skip 50 iterations but I want to continue pressing next from there on to see each line.

I don't want to set a breakpoint after the loop, because this way I'll skip all iterations and not only the number I intend to.

Is there a way to do this in GDB? How?

P.S. I don't want keep pressing next from start to finish. It's time consuming...

Upvotes: 22

Views: 27463

Answers (5)

user28098891
user28098891

Reputation: 11

You can use user-defined command. Create file named, let's say, nnext:

touch nnext
vim nnext

with the following content:

define nnext
  set $count = $arg0
  while $count > 0
    next
    set $count = $count - 1
  end
end

Then, use it with gdb as follows:

gdb> source nnext
gdb> nnext 10

Upvotes: 1

Ramkumar lodhi
Ramkumar lodhi

Reputation: 131

You can use the condition breakpoint.

syntax:

b FileName.extension:lineNumber if varname condition

example:

b File.c:112 if i == 50

Upvotes: 0

Uku Loskit
Uku Loskit

Reputation: 42040

You could use conditional break points

break <lineno> if i > 50

where i is the loop index

Upvotes: 12

helloV
helloV

Reputation: 52433

Set a breakpoint in the loop and then call c 50 to continue 50 times

Debugging with GDB

5.2 Continuing and stepping

continue [ignore-count]
c [ignore-count]
fg [ignore-count]
Resume program execution, at the address where your program last stopped; any breakpoints set at that address are bypassed. The optional argument ignore-count allows you to specify a further number of times to ignore a breakpoint at this location; its effect is like that of ignore (see section Break conditions). The argument ignore-count is meaningful only when your program stopped due to a breakpoint. At other times, the argument to continue is ignored.

Upvotes: 26

odesuk
odesuk

Reputation: 1195

In C# for example you can do "continue" to skip iteration. Example of skipping numbers with mod 3 equal 0, so numbers 3, 9, 12, 15 ... will be skipped.

static void Main(string[] args)
    {
        for (int i = 1; i <= 50; i++)
        {
            if (i%3 == 0)
            {
                continue;                    
            }
            Console.WriteLine("{0}", i);
        }
        Console.ReadLine();
    }

Upvotes: -8

Related Questions