[ox/std] Make asserts generate stack traces

This commit is contained in:
Gary Talent 2018-09-02 21:10:32 -05:00
parent 72b9437ef5
commit 0ba964a475
5 changed files with 57 additions and 1 deletions

View File

@ -6,6 +6,7 @@ add_library(
byteswap.cpp byteswap.cpp
memops.cpp memops.cpp
random.cpp random.cpp
stacktrace.cpp
strops.cpp strops.cpp
) )
@ -26,6 +27,7 @@ install(
memops.hpp memops.hpp
new.hpp new.hpp
random.hpp random.hpp
stacktrace.hpp
string.hpp string.hpp
strops.hpp strops.hpp
std.hpp std.hpp

View File

@ -13,6 +13,7 @@
#include <ox/__buildinfo/defines.hpp> #include <ox/__buildinfo/defines.hpp>
#include "stacktrace.hpp"
#include "assert.hpp" #include "assert.hpp"
namespace ox { namespace ox {
@ -22,6 +23,7 @@ void _assert<bool>([[maybe_unused]]const char *file, [[maybe_unused]]int line, [
#if defined(OX_USE_STDLIB) #if defined(OX_USE_STDLIB)
if (!pass) { if (!pass) {
std::cerr << "\033[31;1;1mASSERT FAILURE:\033[0m (" << file << ':' << line << "): " << msg << std::endl; std::cerr << "\033[31;1;1mASSERT FAILURE:\033[0m (" << file << ':' << line << "): " << msg << std::endl;
printStackTrace(2);
std::abort(); std::abort();
} }
#endif #endif
@ -37,7 +39,7 @@ void _assert<Error>([[maybe_unused]]const char *file, [[maybe_unused]]int line,
if (ei.file != nullptr) { if (ei.file != nullptr) {
std::cerr << "\tError Location:\t" << reinterpret_cast<const char*>(ei.file) << ':' << ei.line << '\n'; std::cerr << "\tError Location:\t" << reinterpret_cast<const char*>(ei.file) << ':' << ei.line << '\n';
} }
std::cerr << std::flush; printStackTrace(2);
std::abort(); std::abort();
} }
#endif #endif

31
deps/ox/src/ox/std/stacktrace.cpp vendored Normal file
View File

@ -0,0 +1,31 @@
/*
* Copyright 2015 - 2018 gtalent2@gmail.com
*
* 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/.
*/
#if defined(OX_USE_STDLIB)
#include <array>
#include <execinfo.h>
#include <iostream>
#include <unistd.h>
#endif
#include "stacktrace.hpp"
namespace ox {
void printStackTrace([[maybe_unused]]int shave) {
#if defined(OX_USE_STDLIB)
std::array<void*, 100> frames;
auto size = backtrace(frames.data(), frames.size());
if (size > shave) {
std::cout << "\nStacktrace:\n";
backtrace_symbols_fd(frames.data() + shave, size - shave, STDERR_FILENO);
}
#endif
}
}

20
deps/ox/src/ox/std/stacktrace.hpp vendored Normal file
View File

@ -0,0 +1,20 @@
/*
* Copyright 2015 - 2018 gtalent2@gmail.com
*
* 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/.
*/
#pragma once
namespace ox {
/**
* Prints a stack trace to stderr.
*
* @param shave number of call levels to shave off the top
*/
void printStackTrace(int shave = 1);
}

View File

@ -16,6 +16,7 @@
#include "memops.hpp" #include "memops.hpp"
#include "new.hpp" #include "new.hpp"
#include "random.hpp" #include "random.hpp"
#include "stacktrace.hpp"
#include "stddef.hpp" #include "stddef.hpp"
#include "strops.hpp" #include "strops.hpp"
#include "string.hpp" #include "string.hpp"