1 #include <stdio.h>
2 int main()
3 {
4 int i;
5 for (i=0; i<=10; i )
6 {
7 puts("Hello world\n");
8 }
9 return 0;
10 //test
}
I am trying to debug the code above using gdb. The problem I am facing is that when I use the command break main
, it sets the breakpoint at line 5 rather then setting the breakpoint at line 2 where it should be. Below is the output that I am getting:
(gdb) break main Breakpoint 1 at 0x555555555141: file firstprogram.c, line 5.
I was expecting that the breakpoint would be set at line no 2 rather than line no 5.
CodePudding user response:
You can only set a breakpoint at an address where there's an instruction.
Look at how your compiler turns this C function into asm. (e.g. on Godbolt targeting Linux with current GCC/clang.)
In GCC or clang output, with or without optimization, there are instructions for the function prologue (pushing a call-preserved register or making a stack frame with a frame pointer, respectively). The debug info generated by GCC and clang associates those instructions with the opening {
, line 3.
(The Godbolt compiler explorer's colour syntax highlighting and mouseover highlighting is based on the debug info generated by compilers, the same info GDB uses when mapping source lines to/from addresses.)
The first instruction for anything in the function body is either the i=0
in the unoptimized version, or the mov edi, OFFSET FLAT:.LC0
/ call puts
in the loop body.
(GCC/clang consider the mov ebx, 11
loop counter init to not be associated with a source line, since after optimization they've transformed the loop into counting down to zero for 11 iterations. That mov-to-register instruction is part of the function prologue, according to the debug info.)
CodePudding user response:
I guess its just a simple answer that main actually starts at line 5 and not line 2 for some reason. But I still dont understand what is meant by start of the function. Would the "line 4: int i;" not be the start of the function ?