tdm-gcc
tdm-gcc copied to clipboard
GDB debugging problems with program containing "cout << endl;" statements
I have found some problems of GDB on a simple program.
Here is the simple program "hello.cpp", it only contains some "cout << endl;" statements:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, world!" << endl;
cout << "Hello, my darling!" << endl;
cout << "Hello, my friends!" << endl;
return 0;
}
- If I compile the program with TDM-GCC 9.2.0, with -g3 option but without any -Ox options, then GDB v7.9.1 cannot debug the executable file "hello.exe" properly (it will halt):
(gdb) file d:/hello.exe Reading symbols from d:/hello.exe...done. (gdb) start Temporary breakpoint 1 at 0x40156d: file D:\hello.cpp, line 4. Starting program: d:\hello.exe [New Thread 4268.0x19e8]
Temporary breakpoint 1, main () at D:\hello.cpp:4 4 cout << "Hello, world!" << endl; (gdb) n Hello, world!0x00000000004767c0 in std::ostream::operator<<(std::ostream& (*)(st d::ostream&)) () (gdb) n Single stepping until exit from function _ZNSolsEPFRSoS_E, which has no line number information. 0x00000000004ade90 in std::basic_ostream<char, std::char_traits
>& std::en dl<char, std::char_traits>(std::basic_ostream<char, std::char_traits (gdb) n Single stepping until exit from function _ZSt4endlIcSt11char_traitsIcEERSt13basi c_ostreamIT_T0_ES6_, which has no line number information.>&) ()
If I use GDB 8.3.1 to debug, it displays the same messages, but it does not halt but go through debugging the executable file.
- Then, I compile the program with TDM-GCC 9.2.0, with -g3 option and with any -Ox options (eg. -Og, -O1, -Ofast), then start debugging, GDB v7.9.1 or v8.3.1 can debug the executable file properly, but calls an extraordinary function.
(gdb) file d:/hello.exe Reading symbols from d:/hello.exe...done. (gdb) start Temporary breakpoint 1 at 0x4015af: file D:\hello.cpp, line 4. Starting program: d:\hello.exe [New Thread 5132.0x1f34]
Temporary breakpoint 1, main () at D:\hello.cpp:4 4 cout << "Hello, world!" << endl; (gdb) n Hello, world! 5 cout << "Hello, my darling!" << endl; (gdb) n Hello, my darling! 6 cout << "Hello, my friends!" << endl; (gdb) n Hello, my friends! 7 return 0; (gdb) n __tmainCRTStartup () at C:/crossdev/src/mingw-w64-v7-git20191109/mingw-w64-crt/crt/crtexe.c:341 341 C:/crossdev/src/mingw-w64-v7-git20191109/mingw-w64-crt/crt/crtexe.c: No such file or directory. (gdb) n 342 in C:/crossdev/src/mingw-w64-v7-git20191109/mingw-w64-crt/crt/crtexe.c (gdb) n [Inferior 1 (process 5132) exited normally]
There are two information need to be noticed: (1) GDB automatically take the first statements as temporary breakpoint and paused at it. (2) after "return 0", it will call **__tmainCRTStartup () at C:/crossdev/src/mingw-w64-v7-git20191109/mingw-w64-crt/crt/crtexe.c **, this is useless and ridiculous!
- And, if I set line 4, 5, or 6 (which contains "cout << endl;" statements) as breakpoints, and run debugging, then GDB v7.9.1 and v8.3.1 ignore these breakpoints and pause at "return 0"!
(gdb) file d:/hello.exe Reading symbols from d:/hello.exe...done. (gdb) b 4 Breakpoint 1 at 0x40161e: file D:\hello.cpp, line 4. (gdb) b 5 Note: breakpoint 1 also set at pc 0x40161e. Breakpoint 2 at 0x40161e: file D:\hello.cpp, line 5. (gdb) b 6 Note: breakpoints 1 and 2 also set at pc 0x40161e. Breakpoint 3 at 0x40161e: file D:\hello.cpp, line 6. (gdb) run Starting program: d:\hello.exe [New Thread 7372.0x3ac] Hello, world! Hello, my darling! Hello, my friends!
Breakpoint 1, main () at D:\hello.cpp:7 7 return 0; (gdb) n __tmainCRTStartup () at C:/crossdev/src/mingw-w64-v7-git20191109/mingw-w64-crt/crt/crtexe.c:341 341 C:/crossdev/src/mingw-w64-v7-git20191109/mingw-w64-crt/crt/crtexe.c: No such file or directory. (gdb) n 342 in C:/crossdev/src/mingw-w64-v7-git20191109/mingw-w64-crt/crt/crtexe.c (gdb) n [Inferior 1 (process 7372) exited normally]
Summary: for the program containing some "cout << endl;" statements,
- if it is compiled without -Ox options, GDB can't debug it properly.
- if it is compiled with any -Ox options, and start debugging with not breakpoints, GDB can debug it. but will call __tmainCRTStartup () at C:/crossdev/src/mingw-w64-v7-git20191109/mingw-w64-crt/crt/crtexe.c
- if it is compiled with any -Ox options, and run debugging with breakpoints at the "cout << endl;" statements, GDB ignores the breakpoints.
- If I compile the program with TDM-GCC 9.2.0, with -g3 option but without any -Ox options, then GDB v7.9.1 cannot debug the executable file "hello.exe" properly (it will halt):
Do you mean 7.6.1? This is the latest available version from MinGW.org. I'm unable to reproduce the problem when I compile using TDM32 GCC-9.2.0 and -g3 and debug using MinGW.org GDB 7.6.1.
If I use GDB 8.3.1 to debug, it displays the same messages, but it does not halt but go through debugging the executable file.
I'm also unable to reproduce with TDM GDB32 8.3.1.
- Then, I compile the program with TDM-GCC 9.2.0, with -g3 option and with any -Ox options (eg. -Og, -O1, -Ofast), then start debugging, GDB v7.9.1 or v8.3.1 can debug the executable file properly, but calls an extraordinary function.
(gdb) file d:/hello.exe Reading symbols from d:/hello.exe...done. (gdb) start Temporary breakpoint 1 at 0x4015af: file D:\hello.cpp, line 4. Starting program: d:\hello.exe [New Thread 5132.0x1f34] Temporary breakpoint 1, main () at D:\hello.cpp:4 4 cout << "Hello, world!" << endl; (gdb) n Hello, world! 5 cout << "Hello, my darling!" << endl; (gdb) n Hello, my darling! 6 cout << "Hello, my friends!" << endl; (gdb) n Hello, my friends! 7 return 0; (gdb) n __tmainCRTStartup () at C:/crossdev/src/mingw-w64-v7-git20191109/mingw-w64-crt/crt/crtexe.c:341 341 C:/crossdev/src/mingw-w64-v7-git20191109/mingw-w64-crt/crt/crtexe.c: No such file or directory. (gdb) n 342 in C:/crossdev/src/mingw-w64-v7-git20191109/mingw-w64-crt/crt/crtexe.c (gdb) n [Inferior 1 (process 5132) exited normally]
There are two information need to be noticed: (1) GDB automatically take the first statements as temporary breakpoint and paused at it.
This is expected behavior when you use start
instead of run
, I believe.
(2) after "return 0", it will call **__tmainCRTStartup () at C:/crossdev/src/mingw-w64-v7-git20191109/mingw-w64-crt/crt/crtexe.c **, this is useless and ridiculous!
In fact, it's exiting into this function which is a hidden wrapper for your main function; this is expected.
The closest I can come to reproducing this behavior is with TDM64 9.2.0 and TDM64 GDB 8.3.1. Then I see the same sequence of functions that you do. I don't see any crash.
- And, if I set line 4, 5, or 6 (which contains "cout << endl;" statements) as breakpoints, and run debugging, then GDB v7.9.1 and v8.3.1 ignore these breakpoints and pause at "return 0"!
You mentioned further down that this happened when you specify optimization flags. This is unfortunately somewhat expected due to GCC's optimizer behavior - you need to use -O0 (no optimization) to avoid it. You can also break earlier, for example break main
.
Regarding the crash, are you operating in a directory with non-ASCII characters? I'd still like to try reproducing this on a non-US-English system.