Continue iteration in C ++ to a specific iteration number in gdb

I am using gdb-7.0.1 and I think I found an error in a specific section of my code that 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 on the first iteration and seems to crash somewhere with the iteration number end/2 .

Since I want to understand the behavior of code with iteration number end/2 , then just stepping and nexting from i=0 until reaching this iteration point is not feasible.

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

I am using gcc-4.5.2 on Ubuntu Linux

+7
source share
5 answers

When you set a breakpoint, it will give you the number of the breakpoint (for the moment, let's say 1). Then you make this breakpoint conditional, for example:

 condition 1 i==end/2 
+5
source

Here is a tutorial on conditional breakpoints with gdb.

I assume you did not know this term, otherwise google would be easy.

+6
source

You must use a conditional breakpoint. More on this: http://www.cs.cmu.edu/~gilpin/tutorial/#3.4

And on SO: How do I set a conditional breakpoint in gdb when char * x points to a string whose value is "hello"?

In your case (not verified):

 break <line_number> if i==end/2 
+5
source

You can place if (i == (end/2 -1)) { Foo; } if (i == (end/2 -1)) { Foo; } there, then set a breakpoint in Foo, which allows you to continue to backtrack from there.

+1
source

If end large (in tens of thousands), then the conditional solution to the breakpoint can be very slow - gdb must evaluate the condition every time in a loop. If this is a problem for you, you can use this trick:

 for (int i=0 ; i< end ; ++i ) { if (i == end/2) i %= end ; // This has no effect, but lets you set a breakpoint here //Code here. } 

I do this all the time :-)

Another solution is to set the skip counter to the breakpoint. I use gdb in a Qt environment, so I cannot give you the gdb syntax. But it is faster than setting conditions.

+1
source

All Articles