2016-02-26 13:36:01 +11:00
|
|
|
/*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* Copyright:
|
2016-04-19 16:08:56 +10:00
|
|
|
* 2012-2016, Danny Robson <danny@nerdcruft.net>
|
2016-02-26 13:36:01 +11:00
|
|
|
*/
|
|
|
|
|
2016-04-19 16:08:56 +10:00
|
|
|
#include "./backtrace.hpp"
|
2012-05-10 16:53:29 +10:00
|
|
|
|
2016-04-19 16:08:56 +10:00
|
|
|
#include "./win32/handle.hpp"
|
|
|
|
#include "./debug.hpp"
|
|
|
|
#include "./except.hpp"
|
2012-05-10 16:53:29 +10:00
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <dbghelp.h>
|
|
|
|
|
|
|
|
|
2016-04-19 16:08:56 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
debug::backtrace::backtrace (void)
|
|
|
|
{
|
2012-05-10 16:53:29 +10:00
|
|
|
m_frames.resize (DEFAULT_DEPTH);
|
|
|
|
|
|
|
|
auto process = GetCurrentProcess();
|
|
|
|
if (!SymInitialize (process, NULL, TRUE))
|
2016-04-19 16:08:56 +10:00
|
|
|
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;
|
|
|
|
}
|
2012-05-10 16:53:29 +10:00
|
|
|
|
|
|
|
m_frames.resize (m_frames.size () * 2);
|
2016-04-19 16:08:56 +10:00
|
|
|
}
|
2012-05-10 16:53:29 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-19 16:08:56 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-05-10 16:53:29 +10:00
|
|
|
std::ostream&
|
|
|
|
debug::operator <<(std::ostream &os, const debug::backtrace &rhs) {
|
2016-04-19 16:08:56 +10:00
|
|
|
static auto self = util::win32::handle::current_process ();
|
|
|
|
static auto ready = SymInitialize (self, nullptr, TRUE);
|
|
|
|
CHECK (ready);
|
2012-05-10 16:53:29 +10:00
|
|
|
|
2016-04-19 16:08:56 +10:00
|
|
|
static constexpr size_t MAX_LENGTH = 255;
|
|
|
|
struct {
|
|
|
|
SYMBOL_INFO info;
|
|
|
|
char name[MAX_LENGTH + 1];
|
|
|
|
} symbol;
|
2012-05-10 16:53:29 +10:00
|
|
|
|
2016-04-19 16:08:56 +10:00
|
|
|
symbol.info.MaxNameLen = MAX_LENGTH;
|
|
|
|
symbol.info.SizeOfStruct = sizeof (SYMBOL_INFO);
|
2012-05-10 16:53:29 +10:00
|
|
|
|
|
|
|
for (void *frame: rhs.frames ()) {
|
2016-04-19 16:08:56 +10:00
|
|
|
symbol.name[0] = '\0';
|
|
|
|
SymFromAddr (self, (DWORD64)frame, 0, &symbol.info);
|
|
|
|
symbol.name[MAX_LENGTH] = '\0';
|
|
|
|
|
|
|
|
std::cerr << self << "\t" << frame << "\t" << symbol.name << "\n";
|
2012-05-10 16:53:29 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
return os;
|
|
|
|
}
|