bombela / backward-cpp

A beautiful stack trace pretty printer for C++
MIT License
3.77k stars 480 forks source link

Symbols only loaded after second attempt on windows #206

Open rulerofthehuns opened 3 years ago

rulerofthehuns commented 3 years ago

When I run this code on windows:

#include "backward.hpp"
int main (int argc, char *argv[])
{
    using namespace backward;
    // TraceResolver thisIsAWorkaround;
    StackTrace st; st.load_here(32);
    Printer p; p.print(st);
    return 0;
}

some modules are not loaded:

Stack trace (most recent call last):
The specified module could not be found.
#4    Object "", at CCCCCCCCCCCCCCCC, in  ??
The specified module could not be found.
#3    Object "", at CCCCCCCCCCCCCCCC, in  ??
The specified module could not be found.
#2    Object "", at 00000059C1BBFAC0, in  ??
The specified module could not be found.
#1    Object "", at 0000005900000000, in  ??
#0    Source "D:\dev\projects\backtracetest\backward.hpp", line 1130, in backward::StackTraceImpl<backward::system_tag::windows_tag>::load_here
       1127:       RtlCaptureContext(ctx_);
       1128:     }
       1129:
      >1130:     if (!thd_) {
       1131:       thd_ = GetCurrentThread();
       1132:     }

But if I create an unused instance of TraceResolver before creating Printer:

#include "backward.hpp"
int main (int argc, char *argv[])
{
    using namespace backward;
    TraceResolver thisIsAWorkaround;
    StackTrace st; st.load_here(32);
    Printer p; p.print(st);
    return 0;
}

everything works as expected:

Stack trace (most recent call last):
#6    Object "", at 00007FF86DA27034, in BaseThreadInitThunk
#5    Source "d:\agent\_work\4\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp", line 17, in mainCRTStartup
#4    Source "d:\agent\_work\4\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl", line 331, in __scrt_common_main
#3    Source "d:\agent\_work\4\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl", line 288, in __scrt_common_main_seh
#2    Source "d:\agent\_work\4\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl", line 79, in invoke_main
#1    Source "D:\dev\projects\backtracetest\main.cpp", line 222, in main
        219:     using namespace backward;
        220:     TraceResolver thisIsAWorkaround;
        221:     StackTrace st; st.load_here(32);
      > 222:     Printer p; p.print(st);
        223:     return 0;
#0    Source "D:\dev\projects\backtracetest\backward.hpp", line 1130, in backward::StackTraceImpl<backward::system_tag::windows_tag>::load_here
       1127:       RtlCaptureContext(ctx_);
       1128:     }
       1129:
      >1130:     if (!thd_) {
       1131:       thd_ = GetCurrentThread();
       1132:     }

I'm using Visual Studio 2019 community edition, 19.25.28614.0, and backward-cpp 1.5 from Conan.

mingmoe commented 2 years ago

Visual Studio 2022, msvc 19.30.30705, backward-cpp 1.6 ,too.

And for first example,it will print "Moudle Not Found".(Seem from WIN32 API)

cppcooper commented 6 months ago

I am in CLion, using the Visual Studio 2022 toolchain [because the mingw toolchain broke my test executables with backward-cpp integrated/linke]. I was also seeing messages stating "The specified module could not be found."

Thank you for the workaround of adding just before the stack trace object:

TraceResolver thisIsAWorkaround;

I'm wondering if there is any clue as to why this is necessary. It wasn't mentioned in the readme doc. Will there be a fix?

fanselm commented 6 months ago

I see the same both with Visual Studio 2019 and Visual Studio 2022.