3

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

Community
  • 1
  • 1
user7328234
  • 393
  • 7
  • 23
  • Have you considered that `A` is just never greater than 1? – StoryTeller - Unslander Monica Jan 10 '17 at 09:17
  • No, gdb just print "hello". – user7328234 Jan 10 '17 at 09:17
  • And how does that disprove what I said? It's supposed to print `"correct"` – StoryTeller - Unslander Monica Jan 10 '17 at 09:18
  • 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 Jan 10 '17 at 09:22
  • Possible duplicate of [How to prevent gcc optimizing some statements in C?](http://stackoverflow.com/questions/2219829/how-to-prevent-gcc-optimizing-some-statements-in-c) – John Szakmeister Jan 10 '17 at 09:25
  • It's probably best to add `__attribute__((optimize("O0")))` to the function definition. Or if your script has a way of doing a non-optimized build, you can do that instead. – John Szakmeister Jan 10 '17 at 09:31
  • Yeah, I'm trying `__attribute__((optimize("O0")))`. I just want gdb to stop at specific line. – user7328234 Jan 10 '17 at 09:32
  • @jszakmeister It still doesn't hit the breakpoint. – user7328234 Jan 10 '17 at 09:38
  • @user7328234 You might try `__attribute__ ((noinline))` as well. But without more context, it's really difficult to help you here. – John Szakmeister Jan 10 '17 at 09:42
  • @user7328234 there are many ways to do it, see http://stackoverflow.com/q/4326414/72178. – ks1322 Jan 10 '17 at 09:43
  • Possible duplicate of [Set breakpoint in C or C++ code programmatically for gdb on Linux](http://stackoverflow.com/questions/4326414/set-breakpoint-in-c-or-c-code-programmatically-for-gdb-on-linux) – ks1322 Jan 10 '17 at 12:12
  • @jszakmeister I've tried to describe the problem I meet clearly. But I don't know whether it's more clear. – user7328234 Jan 10 '17 at 13:38
  • Post a minimal complete example + compiler flags so we can try to reproduce the problem .... – Karoly Horvath Jan 10 '17 at 13:40
  • @Karoly Horvath I'm learning source code [nDPI](https://github.com/ntop/nDPI), a tool for traffic classfy. But when I add code in ndpi_main.c and debug nDPI , gdb doesn't stop in the right line. I do think it's more complicated to refer to nDPI, so I just point out the specific problem. – user7328234 Jan 10 '17 at 13:45
  • @user7328234: http://stackoverflow.com/help/mcve – Karoly Horvath Jan 10 '17 at 13:58

1 Answers1

6

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.

user7328234
  • 393
  • 7
  • 23
  • For FORTRAN, I tried `ifort -g -O0 -u -c -o levelAS.o levelAS.f` followed by `ifort -g -O0 -u levelAS.o -o level.x`, but the line numbers still don't match. – Nike Dec 12 '22 at 03:55