/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * Copyright: * 2012-2016, Danny Robson */ #include "backtrace.hpp" #include "win32/error.hpp" #include "win32/handle.hpp" #include "debug.hpp" #include "except.hpp" #include #include #include /////////////////////////////////////////////////////////////////////////////// debug::backtrace::backtrace (void) { m_frames.resize (DEFAULT_DEPTH); auto process = GetCurrentProcess(); if (!SymInitialize (process, NULL, TRUE)) cruft::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; } m_frames.resize (m_frames.size () * 2); } } /////////////////////////////////////////////////////////////////////////////// std::ostream& debug::operator <<(std::ostream &os, const debug::backtrace &rhs) { static auto self = GetCurrentProcess (); static auto ready = SymInitialize (self, nullptr, TRUE); CHECK (ready); static constexpr size_t MAX_LENGTH = 255; struct { SYMBOL_INFO info; char name[MAX_LENGTH + 1]; } symbol; symbol.info.MaxNameLen = MAX_LENGTH; symbol.info.SizeOfStruct = sizeof (SYMBOL_INFO); for (void *frame: rhs.frames ()) { symbol.name[0] = '\0'; SymFromAddr (self, (DWORD64)frame, 0, &symbol.info); symbol.name[MAX_LENGTH] = '\0'; os << self << "\t" << frame << "\t" << symbol.name << "\n"; } return os; }