rainers / cv2pdb

converter of DMD CodeView/DWARF debug information to PDB files
Artistic License 2.0
466 stars 110 forks source link

Lack of debug symbols in binary built with optimizations enabled #64

Open Poldraunic opened 3 years ago

Poldraunic commented 3 years ago

Hello.

I am developing an app which is currently built with MinGW-W64 5.3.0 on Windows. As it always goes, a need arose to see stacktrace when on crash on consumer machine. Quick Google search led me to "Printing a Stack Trace with MinGW" article. I mindlessly copied code to see if it even works (article is almost 5 years now). Indeed it does and it is quite satisfactorily for my needs.

And so I started tinkering with it. Compiling without optimizations produces satisfactory trace for my needs.

PS F:\Projects\C++\mingw_stacktrace> g++.exe -std=c++11 -g main.cpp -lDbgHelp -o main.exe; ./cv2pdb.exe main.exe; ./main.exe
[0] function_c
[1] function_b
[2] function_a
[3] main
[4] public_all
[5] BaseThreadInitThunk
[6] RtlGetAppContainerNamedObjectPath
[7] RtlGetAppContainerNamedObjectPath

However, the moment I turn on regular optimization level (-O2) all info is lost.

PS F:\Projects\C++\mingw_stacktrace> g++.exe -std=c++11 -O2 -g main.cpp -lDbgHelp -o main.exe; ./cv2pdb.exe main.exe; ./main.exe
[0] public_all
[1] BaseThreadInitThunk
[2] RtlGetAppContainerNamedObjectPath
[3] RtlGetAppContainerNamedObjectPath

After that I decided to see if anything changes if I switch to MinGW-W64 8.1.0. Yes, some things have changed: stack trace without optimizations looks about the same, and with optimizations turned on it is a little bit better:

PS F:\Projects\C++\mingw_stacktrace> g++.exe -std=c++11 -O2 -g main.cpp -lDbgHelp -o main.exe; ./cv2pdb.exe main.exe; ./main.exe
[0] stack_trace
[1] main
[2] public_all
[3] public_all
[4] BaseThreadInitThunk
[5] RtlUserThreadStart

As of this moment, I cannot migrate to a newer version of MinGW-W64 (or to another compiler altogether).


Is it possible to get any kind of info with MinGW-W64 5.3.0? Upgrade to a newer version is planned, but it is a quite a long way away from now on still.

rainers commented 3 years ago

I suspect that your code for functiion_a/b/c is inlined, so there are no stack frames to follow. Maybe using -fno-inline while keeping the rest of the optimizations helps.

Poldraunic commented 3 years ago

Hello.

The moment I noticed lack of function names, I put a no inline attribute (__attribute__ ((noinline))) and it didn't help. Just now tried also with the compile flag and nothing has changed as well.