backtrace/win32: enable win32 backtrace support

This commit is contained in:
Danny Robson 2016-04-19 16:08:56 +10:00
parent 3a2b33e0df
commit 7605abeb44
5 changed files with 55 additions and 35 deletions

View File

@ -301,12 +301,18 @@ UTIL_FILES += \
win32/registry.cpp win32/registry.cpp
endif endif
if HAVE_EXECINFO
UTIL_FILES += backtrace_execinfo.cpp BACKTRACE_FILES =
else if HAVE_CAPTURESTACKBACKTRACE
UTIL_FILES += backtrace_null.cpp BACKTRACE_FILES += backtrace_win32.cpp
endif endif
if HAVE_EXECINFO
BACKTRACE_FILES += backtrace_execinfo.cpp
endif
UTIL_FILES += $(BACKTRACE_FILES)
############################################################################### ###############################################################################
## Local build rules ## Local build rules

View File

@ -33,8 +33,8 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
debug::backtrace::backtrace (void): debug::backtrace::backtrace (void):
m_frames (DEFAULT_DEPTH) { m_frames (DEFAULT_DEPTH)
{
size_t last; size_t last;
size_t size = m_frames.size (); size_t size = m_frames.size ();

View File

@ -12,14 +12,14 @@
* limitations under the License. * limitations under the License.
* *
* Copyright: * Copyright:
* 2012, Danny Robson <danny@nerdcruft.net> * 2012-2016, Danny Robson <danny@nerdcruft.net>
*/ */
#include "backtrace.hpp" #include "./backtrace.hpp"
#include "debug.hpp" #include "./win32/handle.hpp"
#include "except.hpp" #include "./debug.hpp"
#include "memory.hpp" #include "./except.hpp"
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
@ -27,38 +27,49 @@
#include <dbghelp.h> #include <dbghelp.h>
debug::backtrace::backtrace (void) { ///////////////////////////////////////////////////////////////////////////////
debug::backtrace::backtrace (void)
{
m_frames.resize (DEFAULT_DEPTH); m_frames.resize (DEFAULT_DEPTH);
auto process = GetCurrentProcess(); auto process = GetCurrentProcess();
if (!SymInitialize (process, NULL, TRUE)) if (!SymInitialize (process, NULL, TRUE))
win32_error::throw_code (); util::win32_error::throw_code ();
while (true) {
auto res = CaptureStackBackTrace (1, m_frames.size (), m_frames.data (), NULL);
if (res != m_frames.size ()) {
m_frames.resize (res);
break;
}
while (CaptureStackBackTrace (1, m_frames.size (), m_frames.data (), NULL) == m_frames.size ())
m_frames.resize (m_frames.size () * 2); m_frames.resize (m_frames.size () * 2);
}
} }
///////////////////////////////////////////////////////////////////////////////
std::ostream& std::ostream&
debug::operator <<(std::ostream &os, const debug::backtrace &rhs) { debug::operator <<(std::ostream &os, const debug::backtrace &rhs) {
os << "Fuck Windows and it's stupid backtracing\n"; static auto self = util::win32::handle::current_process ();
return os; static auto ready = SymInitialize (self, nullptr, TRUE);
CHECK (ready);
auto process = GetCurrentProcess (); static constexpr size_t MAX_LENGTH = 255;
if (!process) struct {
win32_error::throw_code (); SYMBOL_INFO info;
char name[MAX_LENGTH + 1];
} symbol;
static const size_t MAX_LENGTH = 255; symbol.info.MaxNameLen = MAX_LENGTH;
scoped_malloc<void> symbol_mem (calloc (sizeof (SYMBOL_INFO) + MAX_LENGTH + 1, 1)); symbol.info.SizeOfStruct = sizeof (SYMBOL_INFO);
SYMBOL_INFO *symbol = static_cast<SYMBOL_INFO*> (symbol_mem.get ());
symbol->MaxNameLen = MAX_LENGTH;
symbol->SizeOfStruct = sizeof (SYMBOL_INFO);
for (void *frame: rhs.frames ()) { for (void *frame: rhs.frames ()) {
std::cerr << process << " " << frame << " " << symbol << "\n"; symbol.name[0] = '\0';
SymFromAddr (process, (DWORD64)frame, 0, symbol); SymFromAddr (self, (DWORD64)frame, 0, &symbol.info);
std::cerr << frame << "\t" << symbol->Name << "\n"; symbol.name[MAX_LENGTH] = '\0';
std::cerr << self << "\t" << frame << "\t" << symbol.name << "\n";
} }
return os; return os;

View File

@ -53,6 +53,9 @@ AC_FUNC_MMAP
AC_CHECK_FUNC([backtrace]) AC_CHECK_FUNC([backtrace])
AM_CONDITIONAL([HAVE_EXECINFO], [test "x$ac_cv_func_backtrace" == "xyes"]) AM_CONDITIONAL([HAVE_EXECINFO], [test "x$ac_cv_func_backtrace" == "xyes"])
AC_CHECK_FUNC([RtlCaptureStackBackTrace])
AM_CONDITIONAL([HAVE_CAPTURESTACKBACKTRACE], [test "x$ac_cv_func_RtlCaptureStackBackTrace" == "xyes"])
AC_MSG_CHECKING([for SymFromAddr]) AC_MSG_CHECKING([for SymFromAddr])
save_LIBS="$LIBS" save_LIBS="$LIBS"
LIBS+=-ldbghelp LIBS+=-ldbghelp