Thread: Why does gdb stop at a different line than "i b" shows while returning from function

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    22

    Why does gdb stop at a different line than "i b" shows while returning from function

    Here is the program I am trying to debug:

    Code:
    #include <stdio.h>
    int i = 5;
    
    int main(void)
    {
        int x = 3;
    
        display(x);
        return 0;
    }
    
    
    void display(int x)
    {
    for ( i=0; i<x; ++i ) {
        printf("i is %d.\n", i);
    }
    }
    This code is coming from here Peter's gdb Tutorial: Stepping And Resuming. Here is the problem:

    Code:
    (gdb) break display 
    Breakpoint 1 at 0x40051e: file try5.c, line 15.
    (gdb) run
    Starting program: /home/ja/gdb/learning/try5 
    
    Breakpoint 1, display (x=3) at try5.c:15
    (gdb) frame 1
    #1  0x000000000040050c in main () at try5.c:8
    (gdb) break 
    Breakpoint 2 at 0x40050c: file try5.c, line 8.
    (gdb) c
    Continuing.
    i is 0.
    i is 1.
    i is 2.
    
    Breakpoint 2, main () at try5.c:9
    (gdb) i b
    Num     Type           Disp Enb Address            What
    1       breakpoint     keep y   0x000000000040051e in display at try5.c:15
        breakpoint already hit 1 time
    2       breakpoint     keep y   0x000000000040050c in main at try5.c:8
        breakpoint already hit 1 time
    (gdb) c
    Continuing.
    
    Program exited normally.
    (gdb) q
    
    Debugger finished
    It was supposed to stop at line 8 in main() but it stopped at line 9 it main(). For me it's misleading. I think it should stop at line 9, because this is what 'break' commands does - sets a break point at the very next instruction. But why "info breakpoints" said that the break point was set at line 8?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    No idea. I agree, it is a bit confusing/misleading though. Personally I've never seen (or at least never noticed) this behavior, but I rarely/never go up several frames and place a breakpoint on that function call. I prefer (as does the author of the tutorial) to use the "finish" command. Honestly, this question would be best asked on a GDB-specific forum, most of us aren't experts on GDB's inner workings. Also, GDB is free and open-source, so you can download the source and find out for yourself.

    I've never delved into the GDB source code, and don't care to at the moment, but my gut feeling is that it's just a little bug of sorts. This is a total guess, but I suspect that, because you are placing a breakpoint that is effectively between two statements (one on line 8 and one on line 9), it get's a little confused when displaying where the breakpoint is. That is probably because GDB doesn't actually store a file and line number for the breakpoint, but rather an address of the instruction to stop at (i.e. the 0x00...0040050c in the "i b" output). When it maps that address back to a file and line number, it probably uses one method for the "info breakpoints" command and another method for displaying where it stopped when a breakpoint was hit when running, and they produce different results.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    22
    This was a gdb bug, here is the patch to fix this: sourceware.org/ml/gdb-patches/2012-08/msg00148.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-13-2012, 09:58 PM
  2. Replies: 5
    Last Post: 10-15-2011, 12:29 AM
  3. second "if" loop doesn't stop looping.
    By RobertD in forum C Programming
    Replies: 8
    Last Post: 05-24-2011, 09:07 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM