6

This is the structure of my code

if(someFunction())
{
  // Some code
}

where someFunction() evaluates to 0 most of the time

When GDB is at line 1 above, if I do next then // Some code will not be executed.

Is there a way to tell GDB to execute the code inside the if statement?

Alex Ham
  • 151
  • 1
  • 12
Randomblue
  • 112,777
  • 145
  • 353
  • 547
  • 5
    The code might not even exist if the compiler had a minimum of optimizations. – pmg Apr 26 '12 at 09:45

3 Answers3

6

I can just propose you a workaround. Have a temporary variable int i=0 and then do the if as

if(i==1){
  //some code
}

When you reach the desired position with gdb. Set i to 1 using

set i = 1

and then your loop will be executed. Of course after the loop you will have to reset you i if you don't want it executed every time.

Azrael3000
  • 1,847
  • 12
  • 23
  • Yeah, I was thinking of that. I was hoping for something less hacky. – Randomblue Apr 26 '12 at 10:07
  • 1
    @Randomblue And having an `if(0) { }` statement in your code is not hacky already? – HonkyTonk Apr 26 '12 at 11:27
  • 1
    @HonkyTonk: Well actually my `if` statement is not so dumb; it was just for illustration. In real life I have `if(someFunction())` where `someFunction()` evaluates to `0` most of the time. – Randomblue Apr 26 '12 at 12:48
  • 1
    @Randomblue Then why not write _exactly_ that in your question? Why make us guess and spend time trying to solve a problem that's not the real problem? If you call a function and want to change the return value, you step into that function and change it there. When you then continue to step, you'll get the desired behaviour. – HonkyTonk Apr 26 '12 at 14:00
  • 1
    @HonkyTonk: Yes, that's indeed another solution. But even more hacky! ks1322 has the best solution is my opinion. – Randomblue Apr 26 '12 at 14:12
  • @Randomblue My suggestion, step into and changing the return value, changes no code and is done entirely inside the debugger. That's not even a hack. But I do agree that ks1322 provided a better, and easier, solution. – HonkyTonk Apr 26 '12 at 14:55
4

You can jump to // Some code after stopping on if statement in gdb, unless // Some code was not optimized out, see 17.2 Continuing at a Different Address. Assuming you stopped on if, you can:

jump +2
ks1322
  • 33,961
  • 14
  • 109
  • 164
-3

0 means false, so it will not entering into if loop, use

if(1)
Mohanraj
  • 4,056
  • 2
  • 22
  • 24