Randomblue
Randomblue

Reputation: 116263

GDB: Force through an if statement

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?

Upvotes: 6

Views: 2253

Answers (3)

ks1322
ks1322

Reputation: 35708

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

Upvotes: 4

Mohanraj
Mohanraj

Reputation: 4200

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

if(1)

Upvotes: -3

Azrael3000
Azrael3000

Reputation: 1897

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.

Upvotes: 5

Related Questions