[ox/std] Make asserts generate stack traces
This commit is contained in:
parent
72b9437ef5
commit
0ba964a475
2
deps/ox/src/ox/std/CMakeLists.txt
vendored
2
deps/ox/src/ox/std/CMakeLists.txt
vendored
@ -6,6 +6,7 @@ add_library(
|
||||
byteswap.cpp
|
||||
memops.cpp
|
||||
random.cpp
|
||||
stacktrace.cpp
|
||||
strops.cpp
|
||||
)
|
||||
|
||||
@ -26,6 +27,7 @@ install(
|
||||
memops.hpp
|
||||
new.hpp
|
||||
random.hpp
|
||||
stacktrace.hpp
|
||||
string.hpp
|
||||
strops.hpp
|
||||
std.hpp
|
||||
|
4
deps/ox/src/ox/std/assert.cpp
vendored
4
deps/ox/src/ox/std/assert.cpp
vendored
@ -13,6 +13,7 @@
|
||||
|
||||
#include <ox/__buildinfo/defines.hpp>
|
||||
|
||||
#include "stacktrace.hpp"
|
||||
#include "assert.hpp"
|
||||
|
||||
namespace ox {
|
||||
@ -22,6 +23,7 @@ void _assert<bool>([[maybe_unused]]const char *file, [[maybe_unused]]int line, [
|
||||
#if defined(OX_USE_STDLIB)
|
||||
if (!pass) {
|
||||
std::cerr << "\033[31;1;1mASSERT FAILURE:\033[0m (" << file << ':' << line << "): " << msg << std::endl;
|
||||
printStackTrace(2);
|
||||
std::abort();
|
||||
}
|
||||
#endif
|
||||
@ -37,7 +39,7 @@ void _assert<Error>([[maybe_unused]]const char *file, [[maybe_unused]]int line,
|
||||
if (ei.file != nullptr) {
|
||||
std::cerr << "\tError Location:\t" << reinterpret_cast<const char*>(ei.file) << ':' << ei.line << '\n';
|
||||
}
|
||||
std::cerr << std::flush;
|
||||
printStackTrace(2);
|
||||
std::abort();
|
||||
}
|
||||
#endif
|
||||
|
31
deps/ox/src/ox/std/stacktrace.cpp
vendored
Normal file
31
deps/ox/src/ox/std/stacktrace.cpp
vendored
Normal 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
20
deps/ox/src/ox/std/stacktrace.hpp
vendored
Normal 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);
|
||||
|
||||
}
|
1
deps/ox/src/ox/std/std.hpp
vendored
1
deps/ox/src/ox/std/std.hpp
vendored
@ -16,6 +16,7 @@
|
||||
#include "memops.hpp"
|
||||
#include "new.hpp"
|
||||
#include "random.hpp"
|
||||
#include "stacktrace.hpp"
|
||||
#include "stddef.hpp"
|
||||
#include "strops.hpp"
|
||||
#include "string.hpp"
|
||||
|
Loading…
Reference in New Issue
Block a user