3
votes

In gdb, I set a breakpoint to make gdb stop when the first if condition is met. But gdb stops in another line and if condition isn't met. I've read gdb breakpoint does not get hit, but it's stll not solved. I think gdb stops only when if (a == 1) is met and just in line 3282. Am I wrong?

#pragma GCC push_options
#pragma GCC optimize("O0")

static void __attribute__ ((noinline)) search(int a, int b) 
{  
    // other code here 

    if (a == 1) {
        printf("condition1\n"); 
        printf("condition1\n"); // line 3282, breakpoint is set here   
    }
    if (b == 1) {              // line 3284,  in fact, gdb stops in this line    
        printf("condition2\n");
        printf("condition2\n");
    }
}
#pragma GCC pop_options

set breakpoint in line 3282 using command b file.c:3282

Breakpoint 1 at 0x40da02: file file.c, line 3282.

info breakpoint shows:

Num Type Disp Enb Address What
1 breakpoint keep y 0x000000000040da02 in search at file.c:3282
breakpoint already hit 1 time

But gdb stops in line 3284, instead of 3282, and a is not equal 1

[Switching to Thread 0x7ffff75b8700 (LWP 3631)]
Breakpoint 1, search at file.c:3284
3284 if (b == 1) {

gcc --version

gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2

1
Have you considered that A is just never greater than 1?StoryTeller - Unslander Monica
No, gdb just print "hello".user7328234
And how does that disprove what I said? It's supposed to print "correct"StoryTeller - Unslander Monica
Many times I debug the project, If I set breakpoint at line 3000, gdb just stops near 3000. gdb even jumps back to front lines when executing n command. I think it's gcc optimisation. Only a>1 can "correct" be printed. If I don't change compiling script, Can I make gdb just stop at the specific line?user7328234

1 Answers

5
votes

I change gcc -g -O2 to gcc -g -O0, then everything goes well. Following is document about -O2 option of gcc command.

-O2 Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff. As compared to -O, this option increases both compilation time and the performance of the generated code.