From d272dce4f855a1819eec4f47e3899a32a8adae67 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 2 Sep 2018 21:10:32 -0500 Subject: [PATCH] [ox/std] Make asserts generate stack traces (synced from 0ba964a475331f6d648a3bf93f64a11a971c9c9a) --- src/ox/std/CMakeLists.txt | 2 ++ src/ox/std/assert.cpp | 4 +++- src/ox/std/stacktrace.cpp | 31 +++++++++++++++++++++++++++++++ src/ox/std/stacktrace.hpp | 20 ++++++++++++++++++++ src/ox/std/std.hpp | 1 + 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/ox/std/stacktrace.cpp create mode 100644 src/ox/std/stacktrace.hpp diff --git a/src/ox/std/CMakeLists.txt b/src/ox/std/CMakeLists.txt index 7674b732f..b0c3140bb 100644 --- a/src/ox/std/CMakeLists.txt +++ b/src/ox/std/CMakeLists.txt @@ -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 diff --git a/src/ox/std/assert.cpp b/src/ox/std/assert.cpp index 5785a0ec1..b52f8fba9 100644 --- a/src/ox/std/assert.cpp +++ b/src/ox/std/assert.cpp @@ -13,6 +13,7 @@ #include +#include "stacktrace.hpp" #include "assert.hpp" namespace ox { @@ -22,6 +23,7 @@ void _assert([[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([[maybe_unused]]const char *file, [[maybe_unused]]int line, if (ei.file != nullptr) { std::cerr << "\tError Location:\t" << reinterpret_cast(ei.file) << ':' << ei.line << '\n'; } - std::cerr << std::flush; + printStackTrace(2); std::abort(); } #endif diff --git a/src/ox/std/stacktrace.cpp b/src/ox/std/stacktrace.cpp new file mode 100644 index 000000000..7657ea6e5 --- /dev/null +++ b/src/ox/std/stacktrace.cpp @@ -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 +#include +#include +#include +#endif + +#include "stacktrace.hpp" + +namespace ox { + +void printStackTrace([[maybe_unused]]int shave) { +#if defined(OX_USE_STDLIB) + std::array 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 +} + +} diff --git a/src/ox/std/stacktrace.hpp b/src/ox/std/stacktrace.hpp new file mode 100644 index 000000000..d101c842c --- /dev/null +++ b/src/ox/std/stacktrace.hpp @@ -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); + +} diff --git a/src/ox/std/std.hpp b/src/ox/std/std.hpp index d69184f39..1da4226bf 100644 --- a/src/ox/std/std.hpp +++ b/src/ox/std/std.hpp @@ -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"