"step" not ending program at the end of program, but it ending it after 5 more "step"

Viewed 105

I am using Windows 10. I wrote a HelloWorld program in C.

#include <stdio.h>

int main()
{
    printf("Hello World\n");
    return 0;
}

I compiled this with cmd with the following command

gcc -g -o HelloWorld HelloWorld.c

In gdb, I write start. And I typed step quite a few times. At some point it prints

(gdb) step
7       }

Then I typed step again. It prints

(gdb) step
__tmainCRTStartup () at C:/crossdev/src/mingw-w64-v4-git/mingw-w64-crt/crt/crtexe.c:334
334     C:/crossdev/src/mingw-w64-v4-git/mingw-w64-crt/crt/crtexe.c: No such file or directory.

After another step it prints

332     in C:/crossdev/src/mingw-w64-v4-git/mingw-w64-crt/crt/crtexe.c

After that it prints above line every time I wrote step. And eventually sometime it printed

[Thread 1952.0x1628 exited with code 0]
[Inferior 1 (process 1952) exited normally]

I want to know, what I have done wrong that causes the problem.

1 Answers

“step” not ending program at the end of program

The problem is that your notion of where your program begins and ends is quite wrong: your program executes 100s or 1000s of instructions before it reaches main, and 100s or 1000s more after main returns.

These instructions are part of C runtime startup and shutdown. Usually you are not interested in debugging there, but developers of the C runtime do need to debug it, and the debugger doesn't know whether you want to or not.

Can you tell me how i can tell gdb to debug only my code.

GDB doesn't know which code is your own, and which isn't (and I don't believe there is a way to tell it).

If you don't want to debug past the end of main, then don't ask GDB to do so: once you reach the end of "your code", use continue instead of step.

Related