tags:

views:

79

answers:

1

I have a C code similar to:

int f() {
   for (int i = 0; i < 100; i++) {
   scanf flag;
   if(flag) 
      scanf data1;
   scanf data2;
   }
}

I want to break the execution only when flag == 0. How should I set the breakpoint (using gdb)?

+3  A: 

In the gdb console type

b (some_line) if flag == 0

EDIT:
If you can't print flag while stopped at some-line, then either:
- (A) your code is compiled with optimization (likely), or
- (B) you have a buggy compiler

If it's (A), add -O0 in addition to -g3.

If you can print flag, then you have a buggy version of GDB. Try upgrading to current 7.0.1 release.

KennyTM
yes, i tried that already...I am getting `Error in re-setting breakpoint 17:No symbol flag in current context.`
Lazer
@dksjalk Have you compiled with debug information? gcc -g myfile.c
anon
@dksjalk: or better — gcc -g3
Potatoswatter
yes, I compiled with debug information (using Eclipse though), but I am not sure the debug info is there. Is there some way to check?
Lazer
@dksjalk The error you are getting is exactly the one you get if there is no debug info.
anon
@Neil Butterworth: okay, thanks!
Lazer