Initial framework for win32 backtrace

This commit is contained in:
Danny Robson 2012-05-10 16:53:29 +10:00
parent 71a1592413
commit ac5a5d3a7d
5 changed files with 62 additions and 6 deletions

View File

@ -96,10 +96,12 @@ UTIL_FILES = \
version.hpp
if HAVE_EXECINFO
if PLATFORM_LINUX
UTIL_FILES += backtrace_execinfo.cpp
else
UTIL_FILES += backtrace_null.cpp
endif
if PLATFORM_WIN32
UTIL_FILES += backtrace_win32.cpp
endif
CLEANFILES = json.cpp version.cpp ip.cpp

View File

@ -15,8 +15,9 @@ namespace debug {
const decltype(m_frames)& frames(void) const
{ return m_frames; }
};
std::ostream&
operator <<(std::ostream&, const debug::backtrace&);
}
std::ostream&
operator <<(std::ostream&, const debug::backtrace&);

View File

@ -23,7 +23,7 @@ debug::backtrace::backtrace (void):
ostream&
operator <<(ostream &os, const debug::backtrace &rhs) {
debug::operator <<(ostream &os, const debug::backtrace &rhs) {
const auto frames = rhs.frames ();
typedef unique_ptr<char *[], decltype(&std::free)> unique_str;

48
backtrace_win32.cpp Normal file
View File

@ -0,0 +1,48 @@
#include "backtrace.hpp"
#include "debug.hpp"
#include "except.hpp"
#include "memory.hpp"
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <dbghelp.h>
debug::backtrace::backtrace (void) {
m_frames.resize (DEFAULT_DEPTH);
auto process = GetCurrentProcess();
if (!SymInitialize (process, NULL, TRUE))
win32_error::throw_code ();
while (CaptureStackBackTrace (1, m_frames.size (), m_frames.data (), NULL) == m_frames.size ())
m_frames.resize (m_frames.size () * 2);
}
std::ostream&
debug::operator <<(std::ostream &os, const debug::backtrace &rhs) {
os << "Fuck Windows and it's stupid backtracing\n";
return os;
auto process = GetCurrentProcess ();
if (!process)
win32_error::throw_code ();
static const size_t MAX_LENGTH = 255;
scoped_malloc<void> symbol_mem (calloc (sizeof (SYMBOL_INFO) + MAX_LENGTH + 1, 1));
SYMBOL_INFO *symbol = static_cast<SYMBOL_INFO*> (symbol_mem.get ());
symbol->MaxNameLen = MAX_LENGTH;
symbol->SizeOfStruct = sizeof (SYMBOL_INFO);
for (void *frame: rhs.frames ()) {
std::cerr << process << " " << frame << " " << symbol << "\n";
SymFromAddr (process, (DWORD64)frame, 0, symbol);
std::cerr << frame << "\t" << symbol->Name << "\n";
}
return os;
}

View File

@ -95,9 +95,14 @@ COMMON_CFLAGS="$COMMON_CFLAGS -D_GNU_SOURCE"
case $host_os in
mingw32)
AM_CONDITIONAL([PLATFORM_WIN32], [true])
AM_CONDITIONAL([PLATFORM_LINUX], [false])
COMMON_LDFLAGS="$COMMON_LDFLAGS -ldbghelp"
;;
linux-gnu)
AM_CONDITIONAL([PLATFORM_WIN32], [false])
AM_CONDITIONAL([PLATFORM_LINUX], [true])
;;
*)