3

In GDB, I am trying to set breakpoint on line 4 but it always puts a breakpoint at line 5. Even if I put break main, it puts point on line 5, whereas line 4 is the first line. Why and how can I fix this?

In my coding directory, i have main.c file with the following contents:

#include <stdio.h>

int main() {
    int i;   // line 4
    for (i = 0; i < 10; i++) {
        printf("Hello world!\n");
    }
    return 0;
}

Now I compiled it with gcc for x86 architecture and with extra debugging info... gcc -m32 -g main.c And I am using gdb to examine the a.out: gdb -q ./a.out And when i put break main it sets breakpoint at line 5, the for (i = 0; i < 10; i++) { line. Even if I explicitly put breakpoint on line 4 with break 4, it still sets breakpoint at line 5 with the code for (i = 0; i < 10; i++) {. Here is an example:

$ gdb -q a.out
Reading symbols from a.out...
(gdb) break 4
Breakpoint 1 at 0x11aa: file main.c, line 5.
(gdb)

Why is this not setting a breakpoint at the line 4? What can be the cause? And How do I fix it?

2
  • 4
    Line 4 is int i;. There is nothing to execute there. Commented Aug 30, 2024 at 2:29
  • Line 4 isn't an executable statement. It just allocates space on the stack. If you were to use b *main the asterisk should tell it to stop at the very start of the function before the function prologue. If you step through the assembly code you'd see how the behaviour changes with and without the asterisk (b main vs b *main) Commented Aug 30, 2024 at 2:30

1 Answer 1

4

You can't set a breakpoint on line 4 because it's a declaration with no initializer, not a statement, so there is no assembly instruction associated with it.

If you initialize i, then there will be an assembly instruction that goes with it, so you should be able to set a breakpoint after that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.