backtrace: give consistent outputs for backtrace

This commit is contained in:
Danny Robson 2019-07-02 16:37:43 +10:00
parent d0950a97fa
commit 928cdb4e8b
7 changed files with 81 additions and 63 deletions

View File

@ -3,33 +3,27 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
* *
* Copyright 2010-2014 Danny Robson <danny@nerdcruft.net> * Copyright 2019, Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __UTIL_BACKTRACE_HPP #pragma once
#define __UTIL_BACKTRACE_HPP
#include <string>
#include <vector> #include <vector>
#include <iosfwd> #include <iosfwd>
//----------------------------------------------------------------------------- ///////////////////////////////////////////////////////////////////////////////
namespace debug { namespace cruft {
class backtrace { class backtrace {
protected:
static const unsigned int DEFAULT_DEPTH = 16;
std::vector<void *> m_frames;
public: public:
backtrace (void); backtrace (void);
const auto& frames(void) const { return m_frames; } const auto& frames(void) const { return m_frames; }
private:
std::vector<void *> m_frames;
}; };
std::ostream& std::ostream&
operator<< (std::ostream&, const debug::backtrace&); operator<< (std::ostream&, backtrace const&);
} }
#endif

View File

@ -3,47 +3,53 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
* *
* Copyright 2010-2014 Danny Robson <danny@nerdcruft.net> * Copyright 2019, Danny Robson <danny@nerdcruft.net>
*/ */
#include "backtrace.hpp" #include "backtrace.hpp"
#include "debug/assert.hpp"
#include "exe.hpp"
#include "io.hpp"
#include "cast.hpp" #include "cast.hpp"
#include "iterator/zip.hpp"
#include <sstream> #include <ostream>
#include <cstdio>
#include <cstdlib>
#include <execinfo.h>
#include <memory> #include <memory>
#include <execinfo.h>
using cruft::backtrace;
static constexpr std::size_t DEFAULT_DEPTH = 16;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
debug::backtrace::backtrace (void): backtrace::backtrace (void)
m_frames (DEFAULT_DEPTH) : m_frames (DEFAULT_DEPTH)
{ {
size_t last; do {
size_t size = m_frames.size (); int const target = static_cast<int> (m_frames.size ());
int const last = ::backtrace (
m_frames.data (), target
);
while ((last = ::backtrace (&m_frames[0], cruft::cast::lossless <int> (m_frames.size ()))) == size) if (last < target) {
m_frames.resize (size = m_frames.size () * 2);
CHECK_GT (last, 0u);
m_frames.resize (last); m_frames.resize (last);
break;
}
m_frames.resize (m_frames.size () * 2);
} while (1);
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
std::ostream& std::ostream&
debug::operator <<(std::ostream &os, const debug::backtrace &rhs) { cruft::operator <<(std::ostream &os, backtrace const &rhs) {
os << "[ ";
auto const &frames = rhs.frames (); auto const &frames = rhs.frames ();
// We don't use the array form of unique_ptr as clang fails on ambigious constructors using strings_t = std::unique_ptr<char *, decltype(&std::free)>;
using str_t = std::unique_ptr<char *, decltype(&std::free)>; strings_t const names (
str_t names (
backtrace_symbols ( backtrace_symbols (
frames.data (), frames.data (),
cruft::cast::lossless <int> (frames.size ()) cruft::cast::lossless <int> (frames.size ())
@ -51,8 +57,12 @@ debug::operator <<(std::ostream &os, const debug::backtrace &rhs) {
::free ::free
); );
for (unsigned int i = 0; i < frames.size (); ++i) for (auto const [idx,addr]: cruft::iterator::izip (frames)) {
os << frames[i] << '\t' << names.get()[i] << '\n'; os << "{ addr: " << frames[idx]
<< ", name: '" << names.get()[idx] << '\''
<< " }, ";
}
os << " ]";
return os; return os;
} }

View File

@ -14,12 +14,12 @@
// windows.h needs to be included here so that it defines some symbols that // windows.h needs to be included here so that it defines some symbols that
// dbghelp.h requires. // dbghelp.h requires.
#include <windows.h> #include "win32/windows.hpp"
#include <dbghelp.h> #include <dbghelp.h>
#include <ostream> #include <ostream>
using debug::backtrace; using cruft::backtrace;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -78,27 +78,34 @@ backtrace::backtrace ()
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
std::ostream& std::ostream&
debug::operator<< (std::ostream &os, const debug::backtrace &b) cruft::operator<< (std::ostream &os, backtrace const &obj)
{ {
const auto self = GetCurrentProcess (); const auto process = GetCurrentProcess ();
struct { struct {
IMAGEHLP_SYMBOL64 info; SYMBOL_INFO info;
char name[255]; char name[255];
} sym; char null = '\0';
} sym {};
sym.info.SizeOfStruct = sizeof (sym.info); sym.info.SizeOfStruct = sizeof (sym.info);
sym.info.MaxNameLength = std::size (sym.name); sym.info.MaxNameLen = std::size (sym.name);
os << "[ ";
for (auto const frame: obj.frames ()) {
os << "{ addr: " << frame << ", name: ";
for (auto frame: b.frames ()) {
// Find the symbol name // Find the symbol name
sym.info.Name[0] = '\0'; sym.info.Name[0] = '\0';
memset (sym.name, 0, sizeof (sym.name)); memset (sym.name, 0, sizeof (sym.name));
SymGetSymFromAddr64 (self, reinterpret_cast<uintptr_t> (frame), nullptr, &sym.info); if (FALSE == SymFromAddr (process, reinterpret_cast<uintptr_t> (frame), nullptr, &sym.info)) {
*std::rbegin (sym.name) = '\0'; os << "null }, ";
} else {
os << self << '\t' << frame << '\t' << sym.info.Name << '\n'; os << '\'' << sym.info.Name << "' }, ";
}
} }
os << " ]";
return os; return os;
} }

View File

@ -4,7 +4,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
* *
* Copyright: * Copyright:
* 2012-2016, Danny Robson <danny@nerdcruft.net> * 2012-2019, Danny Robson <danny@nerdcruft.net>
*/ */
#include "backtrace.hpp" #include "backtrace.hpp"
@ -18,9 +18,13 @@
#include <cstdlib> #include <cstdlib>
#include <dbghelp.h> #include <dbghelp.h>
using cruft::backtrace;
static constexpr std::size_t DEFAULT_DEPTH = 16;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
debug::backtrace::backtrace (void) backtrace::backtrace (void)
{ {
m_frames.resize (DEFAULT_DEPTH); m_frames.resize (DEFAULT_DEPTH);
@ -29,8 +33,8 @@ debug::backtrace::backtrace (void)
cruft::win32::error::throw_code (); cruft::win32::error::throw_code ();
while (true) { while (true) {
auto res = CaptureStackBackTrace (1, m_frames.size (), m_frames.data (), NULL); auto const res = CaptureStackBackTrace (1, m_frames.size (), m_frames.data (), NULL);
if (res != m_frames.size ()) { if (res < m_frames.size ()) {
m_frames.resize (res); m_frames.resize (res);
break; break;
} }
@ -42,9 +46,8 @@ debug::backtrace::backtrace (void)
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
std::ostream& std::ostream&
debug::operator <<(std::ostream &os, const debug::backtrace &rhs) { debug::operator <<(std::ostream &os, ::cruft::backtrace const &rhs) {
static auto self = GetCurrentProcess (); static auto process = GetCurrentProcess ();
static auto ready = SymInitialize (self, nullptr, TRUE);
CHECK (ready); CHECK (ready);
static constexpr size_t MAX_LENGTH = 255; static constexpr size_t MAX_LENGTH = 255;
@ -56,13 +59,17 @@ debug::operator <<(std::ostream &os, const debug::backtrace &rhs) {
symbol.info.MaxNameLen = MAX_LENGTH; symbol.info.MaxNameLen = MAX_LENGTH;
symbol.info.SizeOfStruct = sizeof (SYMBOL_INFO); symbol.info.SizeOfStruct = sizeof (SYMBOL_INFO);
os << "[ ";
for (void *frame: rhs.frames ()) { for (void *frame: rhs.frames ()) {
symbol.name[0] = '\0'; symbol.name[0] = '\0';
SymFromAddr (self, (DWORD64)frame, 0, &symbol.info); SymFromAddr (process, (DWORD64)frame, 0, &symbol.info);
symbol.name[MAX_LENGTH] = '\0'; symbol.name[MAX_LENGTH] = '\0';
os << self << "\t" << frame << "\t" << symbol.name << "\n"; os << "{ addr: " << frame
<< ", name: '" << symbol.name '\''
<< " }, ";
} }
os << " ]";
return os; return os;
} }

View File

@ -18,7 +18,7 @@
void void
cruft::debug::detail::panic (const char *msg) cruft::debug::detail::panic (const char *msg)
{ {
std::cerr << "PANIC: " << msg << "\n" << ::debug::backtrace () << std::endl; std::cerr << "PANIC: " << msg << "\n" << ::cruft::backtrace () << std::endl;
breakpoint (); breakpoint ();
abort (); abort ();
} }

View File

@ -33,12 +33,12 @@ static void abort_with_trace (void)
try { try {
std::rethrow_exception (ptr); std::rethrow_exception (ptr);
} catch (std::exception const &x) { } catch (std::exception const &x) {
LOG_EMERGENCY ("unhandled exception: %!\n%!", x.what (), debug::backtrace {}); LOG_EMERGENCY ("unhandled exception: %!\n%!", x.what (), ::cruft::backtrace {});
} catch (...) { } catch (...) {
LOG_EMERGENCY ("unhandled exception: %!\n", debug::backtrace {}); LOG_EMERGENCY ("unhandled exception: %!\n", ::cruft::backtrace {});
} }
} else { } else {
LOG_EMERGENCY ("aborting: %!", debug::backtrace {}); LOG_EMERGENCY ("aborting: %!", ::cruft::backtrace {});
} }
std::abort (); std::abort ();

View File

@ -11,7 +11,7 @@ main (int, char **) {
cruft::TAP::logger tap; cruft::TAP::logger tap;
// Instantiate a backtrace just so we know that linking has worked. // Instantiate a backtrace just so we know that linking has worked.
debug::backtrace foo; ::cruft::backtrace foo;
(void)foo; (void)foo;
tap.noop (); tap.noop ();