Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gdb breakpoint gets hit in the wrong line number

Tags:

c

linux

gcc

gdb

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

like image 247
user7328234 Avatar asked Sep 02 '25 16:09

user7328234


1 Answers

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.

like image 172
user7328234 Avatar answered Sep 05 '25 05:09

user7328234