Debugging big input in eclipse - c++

Unfortunately a program I wrote in C++ has a bug (or bugs), yet I can't deduce what it is since only a single output line doesn't match the expected output (the input file has 3K lines of input). I know which input line is the problematic, yet it's over 2K lines into the input file so debugging it manually isn't very efficient.
Is there any way to let the debugger run "alone" with the first 2K lines and stop exactly before trying to execute the problematic input line? I use Windows and eclipse but don't mind switching the IDE or switch to Linux if necessary.
Thanks in advance!

Don't reinvent the wheel!
Eclipse has powerfull feature Enable condition for breakpoint.
Set breakpoint in your code
Right-click on breakpoint - "Breakpoint properties"
Check "Conditional"
Write your condition when breakpoint should be stopped (you have access to scope and global variables)
Here is Eclipse help page. Breakpoint Enable Condition
(with screenshot)

Well, eclipse may well have advanced debugging features, and to tell the truth I do not know the first thing about eclipse, but FWIW you can easily simulate what you want.
What you have is:
for(i = 0; i < lineCount; ++i)
process one line
Add a condition to check the line number and place a breakpoint inside it!:
for(i = 0; i < lineCount; ++i)
if(i == 2000)
{
int x = i*i; //random line, just add your breakpoint here!
}
process one line

Since you know exactly what line is problematic, just a dummy if statement which would check the contents of the line, and if mathces your problem line, do nothing - and put a breakoint at this line.

Related

gdb breakpoint won't set

I'm trying to debug C++ code using gdb. Particularly, I want to check if a certain variable (viscosities[j]) is not NAN and within certain bounds (eta_min and eta_max). I'm doing it using the following code:
if (!crash)
{
if (viscosities[j]>=eta_min && viscosities[j]<=eta_max)
{}
else
{
std::cout<<"viscosities[j]: "<<viscosities[j]<<" ";
crash = true;
}
}
(I need the variable crash because the code part of a loop, it's initialized to 0 before the loop starts).
In gdb, I'm trying to set the breakpoint using the following command:
break nz.cc:238
(line 238 is one with the cout statement)
However, the breakpoint is instead set to line 246:
Breakpoint 3 at 0xaeeb09: file /home/lev/aspect/aspect/source/material_model/nz.cc, line 246. (2 locations)
and the compiler never stops at line 238. I verified the line number through vi and made sure to rebuild the executable. Can anyone please give me an idea of why this might be happening? Thanks in advance!
UPD: solved by disabling optimization during executable compilation

Debugging C++ in an Eclipse-based IDE - is there something like "step over loop/cycle"?

At the moment I'm using an eclipse-like IDE and the corresponding debug perspective, that most of you are probably familiar with. While debugging code I quite often find myself stepping through many lines of code and observing variables and double checking if everything is as it is supposed to be.
But suppose there is something like this:
1. important line, e.g. generating a new object;
2. another important line, e.g. some tricky class method;
3. for (int i = 0; i < some_limit; ++i)
4. some_array[i]++;
5. more important stuff;
Obviously I'm interested in what happens in lines 1,2 and 5 (I know this is a poor example, but please bear with me for a little while longer) but I don't want to step through all hundreds (or even thousands) of iterations of lines 3/4.
So, finally, my question: Is there some way to step directly over the for-cycle? What I do right now is set a new breakpoint at line 5 and let the program run as soon as I hit line 3 and I believe this is not an optimal solution.
edit: The eclipse implementation of what ks1322 proposed is called "Run to line" and is mapped to ctrl-r
Use until command instead of next.
From gdb documentation:
Continue running until a source line past the current line, in the
current stack frame, is reached. This command is used to avoid single
stepping through a loop more than once.
If you will use until instead of next, gdb will step over loops only once, which almost exactly what you want.

Continue an iteration in C++ until a specific iteration number in 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
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.
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
You have to use conditional breakpoint. Here is more about it: 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 equals "hello"?
In your case (not tested):
break <line_number> if i==end/2
You should be able to place an if (i == (end/2 -1)) { Foo; } in there then set a breakpoint at Foo, which would allow you to continue stepping from there.
If end is big (in the tens of thousands), then the conditional breakpoint solution can be very slow - gdb has to evaluate the condition each time round the loop. If this is a problem for you, then 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 a skip-count on the breakpoint. I use gdb in a Qt environment, so I can't give you the gdb syntax. But it's faster than setting a condition.

Is it possible to skip over an abitrary amount of a loop during debug?? Visual Studio

I am trying to find an error in my code. The problem is the error occurs in a loop. But the loop iterates about 500 times. Instead of clicking through the loop. Is it possible to skip over a certain amount of the loop ??
VS allows you to set a condition on a breakpoint in terms of variables that are in scope. So, in your case, you can test against the loop counter.
Here is a crude answer:
if ((iter % 10) == 0) {
int stop = 1;
}
Then place a break-point at "int stop = 1;". Perhaps, there is a better way in VS but this is what I do from time-to-time.
You can assign new values to variables during debug session. Step through the loop statements as many times as you like, then set your loop counter (or whatever other vars maintain loop condition) to terminate the loop.
Just put the breakpoint in the loop like indicated below >>. Use F5 to get to the condition that causes failure so you can loop through the individual pass. How to know where to break is up to you.
for (int i = 0; i < LOOPMAX; i++) {
>>some_proc(i);
some_other_proc(i);
some_third_proc(i);
}
By pressing F5 it'll continue running till it gets to the next breakpoint (the next pass through the code). Sure you'll have to hit it 500 times, but that beats some thousands of times. Combine this with #Troubador code above.
PS: This answer IS really simple, but some people don't know they can do this.

Set Visual Studio (conditional) breakpoint on local variable value

I'm trying to debug a method which among other things, adds items to a list which is local to the method.
However, every so often the list size gets set to zero "midstream". I would like to set the debugger to break when the list size becomes zero, but I don't know how to, and would appreciate any pointers on how to do this.
Thanks.
Why not use conditional breakpoints?
http://blogs.msdn.com/saraford/archive/2008/06/17/did-you-know-you-can-set-conditional-breakpoints-239.aspx
in C#
if(theList.Count == 0){
//do something meaningless here .e.g.
int i = 1; // << set your breakpoint here
}
in VB.NET
If theList.Count = 0 Then
'do something meaningless here .e.g.
Dim i = 1; ' << set your breakpoint here
End If
For completeness sake, here's the C++ version:
if(theList->Count == 0){
//do something meaningless here .e.g.
int i = 1; // << set your breakpoint here
}
I can give a partial answer for Visual Studio 2005. If you open the "Breakpoints" window (Alt + F9) you get a list of breakpoints. Right-click on the breakpoint you want, and choose "Condition." Then put in the condition you want.
You have already got both major options suggested:
1. Conditional breakpoints
2. Code to check for the wrong value, and with a breakpoint if so happens
The first option is the easiest and best, but on large loops it is unfortunately really slow! If you loop 100's of thousands iterations the only real option is #2. In option #1 the cpu break into the debugger on each iteration, then it evaluates the condition and if the condition for breaking is false it just continiues execution of the program. This is slow when it happens thousands of times, it is actually slow if you loop just 1000 times (depending on hardware of course)
As I suspect you really want an "global" breakpoint condition that should break the program if a certain condition is met (array size == 0), unfortunately that does not exist to my knowledge. I have made a debugging function that checks the condition, and if it is true it does something meaningless that I have a breakpoint set to (i.e. option 2), then I call that function frequently where I suspect the original fails. When the system breaks you can use the call stack to identify the faulty location.