c++visual-studioboostcmakestack-trace

boost::stacktrace on Windows MSVS 2017


I'd like a backtrace when my Windows application crashes. Boost stacktrace looks interesting, and I've played with it a bit in Linux; however, I'm getting a zero result from safe_dump_to() in Windows!

I presume I've made a simple oversight, and I'm hoping someone can help.

I'm using the MSVS2017 compiler with ninja and CMake.

I've built Boost v1.67 built with these flags:

link=static address-model=64 variant=release threading=multi

I ran the tests in boost_1_67_0/libs/stacktrace/test/ to make sure I'd built the library correctly.

Here is my CMake file, note I used -DCMAKE_BUILD_TYPE=RelWithDebInfo:

cmake_minimum_required(VERSION 3.0)

set( target_name stacktrace_test)

# boost
set(Boost_USE_STATIC_LIBS ON)
find_package( Boost REQUIRED COMPONENTS date_time filesystem thread 
system stacktrace_windbg)

add_executable(${target_name} main.cpp)

set_property(TARGET ${target_name} PROPERTY CXX_STANDARD 14 )

target_compile_definitions(${target_name} PUBLIC
  BOOST_ALL_NO_LIB=1   # disable pragama inclusion 
  BOOST_STACKTRACE_LINK=1
  BOOST_STACKTRACE_USE_WINDBG=1
  )
target_include_directories(${target_name} SYSTEM PUBLIC
  ${Boost_INCLUDE_DIRS}
  )
target_link_libraries(${target_name}
  ${Boost_LIBRARIES} # boost
  dbgeng
  ole32
)

Here is my code, based on Boost stactrace's getting started page:

#include <iostream>
#include <signal.h>     // ::signal, ::raise
#include <boost/stacktrace/stacktrace.hpp>
#include <boost/filesystem.hpp>

void handler(int signum)
{
   ::signal(signum, SIG_DFL);
   size_t result = boost::stacktrace::safe_dump_to("./backtrace.dump");
   std::cout << " " << result << std::endl;
   ::raise(SIGABRT);
}

void crash()
{
   abort();
}

int main(int,char**)
{
   ::signal(SIGSEGV, &handler);
   ::signal(SIGABRT, &handler);
   if(boost::filesystem::exists("backtrace.dump"))  // existing stacktrace?
   {
      std::ifstream ifs("backtrace.dump");
      boost::stacktrace::stacktrace st = 
                      boost::stacktrace::stacktrace::from_dump(ifs);
      std::cout << "Stacktrace from prior run:\n" << st << std::endl;
      ifs.close();   // cleanup
      boost::filesystem::remove("backtrace.dump");
      return 0;
   }
   crash();
   return 0;
}

And here is the output from my compilation:

C:\SRC\sandbox\boost\stacktrace\win-simple\Build_RWD>ninja -v
[1/2  50%]:C:\PROGRA~2\MICROS~1\2017\BUILDT~1\VC\Tools\MSVC\1413~1.261\bin\Hostx64\x64\cl.exe  /nologo /TP -DBOOST_ALL_NO_LIB=1 -DBOOST_STACKTRACE_LINK=1 -DBOOST_STACKTRACE_USE_WINDBG=1 -IC:\3rd_libs\boost\boost_1_67_0 /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MD /Zi /O2 /Ob1 /DNDEBUG   -std:c++14 /showIncludes /FoCMakeFiles\stacktrace_test.dir\main.cpp.obj /FdCMakeFiles\stacktrace_test.dir\ /FS -c ..\main.cpp
[2/2 100%]:cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\stacktrace_test.dir --manifests  -- C:\PROGRA~2\MICROS~1\2017\BUILDT~1\VC\Tools\MSVC\1413~1.261\bin\Hostx64\x64\link.exe /nologo CMakeFiles\stacktrace_test.dir\main.cpp.obj  /out:stacktrace_test.exe /implib:stacktrace_test.lib /pdb:stacktrace_test.pdb /version:0.0  /machine:x64 /debug /INCREMENTAL /subsystem:console  C:\3rd_libs\boost\boost_1_67_0\stage\lib\libboost_date_time.lib C:\3rd_libs\boost\boost_1_67_0\stage\lib\libboost_filesystem.lib C:\3rd_libs\boost\boost_1_67_0\stage\lib\libboost_thread.lib C:\3rd_libs\boost\boost_1_67_0\stage\lib\libboost_system.lib C:\3rd_libs\boost\boost_1_67_0\stage\lib\libboost_stacktrace_windbg.lib C:\3rd_libs\boost\boost_1_67_0\stage\lib\libboost_chrono.lib C:\3rd_libs\boost\boost_1_67_0\stage\lib\libboost_atomic.lib dbgeng.lib ole32.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
LINK : stacktrace_test.exe not found or not built by the last incremental link; performing full link

I've reviewed the compilation flags and nothing jumps out at me.

Any help is appreciated! Thank you, SB


Solution

  • As it turns out,functionality in boost::stacktrace is disabled for windows as it's reported it may cause deadlocks. Re-enabling the code in stacktrace/detail/safe_dump_win.ipp solved the problem for me.

    Note that I took the github development version of safe_dump_win.ipp, frame_msvc.ipp, and frame_unwind.ipp as my starting point.

    In my limited testing I have not seen the deadlock. Should the software deadlock I will be in no worse shape than if it quietly crashed and, hopefully, I will get more traces than deadlocks!