Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continue an iteration in C++ until a specific iteration number in gdb

Tags:

c++

gdb

I am using gdb-7.0.1 and I think I have detected a bug in a certain section of my code, which has a for loop. The for loop looks like

for (int i=0 ; i< end ; ++i )
{
   //Code here.
}

Here end is a very large integer. The code does not crash at the first iteration, and seems to crash somewhere at iteration number end/2.

Since I want to understand the behaviour of the code at iteration number end/2 , just stepping and nexting from i=0 till I reach this iteration point, is unfeasible.

Is there a way to tell gdb to continue through a for loop till i gets the value end/2 and then wait for the user to manually step through iteration number end/2?

I am using gcc-4.5.2 on Ubuntu Linux

like image 516
smilingbuddha Avatar asked Apr 16 '12 18:04

smilingbuddha


2 Answers

Here's a tutorial on conditional breakpoints with gdb.

I'm guessing you didn't know the term for this, otherwise it would have been easy to google.

like image 60
Luchian Grigore Avatar answered Sep 28 '22 07:09

Luchian Grigore


When you set the breakpoint it'll give you a breakpoint number (for the moment, let's assume it's 1). You'll then make that breakpoint conditional, something like:

condition 1 i==end/2
like image 38
Jerry Coffin Avatar answered Sep 28 '22 06:09

Jerry Coffin