2016-04-27 16:00:00 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* 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/.
|
2016-04-27 16:00:00 +10:00
|
|
|
*
|
|
|
|
* Copyright 2016 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "backtrace.hpp"
|
2016-04-27 16:00:00 +10:00
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "debug.hpp"
|
|
|
|
#include "types.hpp"
|
2018-08-13 14:51:33 +10:00
|
|
|
#include "win32/except.hpp"
|
2018-08-27 14:16:27 +10:00
|
|
|
#include "win32/windows.hpp"
|
2016-04-27 16:00:00 +10:00
|
|
|
|
|
|
|
#include <dbghelp.h>
|
|
|
|
|
2018-03-22 16:10:06 +11:00
|
|
|
#include <ostream>
|
2016-04-27 16:00:00 +10:00
|
|
|
|
|
|
|
using debug::backtrace;
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
backtrace::backtrace ()
|
|
|
|
{
|
2018-08-20 15:07:35 +10:00
|
|
|
if (!SymInitialize (GetCurrentProcess (), nullptr, TRUE))
|
|
|
|
cruft::win32::error::throw_code ();
|
2016-04-27 16:00:00 +10:00
|
|
|
|
|
|
|
CONTEXT ctx;
|
|
|
|
memset (&ctx, 0, sizeof (ctx));
|
|
|
|
ctx.ContextFlags = CONTEXT_ALL;
|
|
|
|
RtlCaptureContext (&ctx);
|
|
|
|
|
|
|
|
auto image_type = IMAGE_FILE_MACHINE_AMD64;
|
|
|
|
|
|
|
|
STACKFRAME64 frame;
|
|
|
|
memset (&frame, 0, sizeof (frame));
|
|
|
|
frame.AddrPC.Offset = ctx.Rip;
|
|
|
|
frame.AddrPC.Mode = AddrModeFlat;
|
|
|
|
frame.AddrFrame.Offset = ctx.Rsp;
|
|
|
|
frame.AddrFrame.Mode = AddrModeFlat;
|
|
|
|
frame.AddrStack.Offset = ctx.Rsp;
|
|
|
|
frame.AddrStack.Mode = AddrModeFlat;
|
|
|
|
|
|
|
|
auto process = GetCurrentProcess ();
|
|
|
|
auto thread = GetCurrentThread ();
|
|
|
|
|
|
|
|
SetLastError (0);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (!StackWalk64 (image_type,
|
|
|
|
process,
|
|
|
|
thread,
|
|
|
|
&frame,
|
|
|
|
&ctx,
|
|
|
|
nullptr,
|
|
|
|
SymFunctionTableAccess64,
|
|
|
|
SymGetModuleBase64,
|
|
|
|
nullptr))
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::win32::error::throw_code ();
|
2016-04-27 16:00:00 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// we've read the bottom of the stack
|
|
|
|
if (frame.AddrReturn.Offset == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// we've reached the tail of an (apparently) infinite stack. this is a common occurence.
|
|
|
|
if (frame.AddrPC.Offset == frame.AddrReturn.Offset)
|
|
|
|
break;
|
|
|
|
|
2018-08-13 14:51:33 +10:00
|
|
|
m_frames.push_back (reinterpret_cast<void*> (frame.AddrPC.Offset));
|
2016-04-27 16:00:00 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
std::ostream&
|
|
|
|
debug::operator<< (std::ostream &os, const debug::backtrace &b)
|
|
|
|
{
|
|
|
|
const auto self = GetCurrentProcess ();
|
|
|
|
|
2018-08-13 14:51:33 +10:00
|
|
|
struct {
|
2016-04-27 16:00:00 +10:00
|
|
|
IMAGEHLP_SYMBOL64 info;
|
|
|
|
char name[255];
|
|
|
|
} sym;
|
|
|
|
|
|
|
|
sym.info.SizeOfStruct = sizeof (sym.info);
|
2016-11-17 18:06:39 +11:00
|
|
|
sym.info.MaxNameLength = std::size (sym.name);
|
2016-04-27 16:00:00 +10:00
|
|
|
|
|
|
|
for (auto frame: b.frames ()) {
|
|
|
|
// Find the symbol name
|
|
|
|
sym.info.Name[0] = '\0';
|
|
|
|
memset (sym.name, 0, sizeof (sym.name));
|
2018-08-13 14:51:33 +10:00
|
|
|
SymGetSymFromAddr64 (self, reinterpret_cast<uintptr_t> (frame), nullptr, &sym.info);
|
2016-04-27 16:00:00 +10:00
|
|
|
*std::rbegin (sym.name) = '\0';
|
|
|
|
|
|
|
|
os << self << '\t' << frame << '\t' << sym.info.Name << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
return os;
|
|
|
|
}
|