|
| 1 | +// Copyright (C) 2025 Category Labs, Inc. |
| 2 | +// |
| 3 | +// This program is free software: you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU General Public License as published by |
| 5 | +// the Free Software Foundation, either version 3 of the License, or |
| 6 | +// (at your option) any later version. |
| 7 | +// |
| 8 | +// This program is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU General Public License |
| 14 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +#include <category/core/terminate_handler.h> |
| 17 | + |
| 18 | +#include <cxxabi.h> |
| 19 | +#include <exception> |
| 20 | +#include <stdio.h> |
| 21 | +#include <stdlib.h> |
| 22 | +#include <string.h> |
| 23 | +#include <typeinfo> |
| 24 | +#include <unistd.h> |
| 25 | + |
| 26 | +extern char const *__progname; // NOLINT(bugprone-reserved-identifier) |
| 27 | + |
| 28 | +extern "C" void monad_stack_backtrace_capture_and_print( |
| 29 | + char *buffer, size_t size, int fd, unsigned indent, |
| 30 | + bool print_async_unsafe_info); |
| 31 | + |
| 32 | +namespace |
| 33 | +{ |
| 34 | + void monad_terminate_handler_impl() noexcept |
| 35 | + { |
| 36 | + char buffer[16384]; |
| 37 | + ssize_t written = 0; |
| 38 | + |
| 39 | + // Print header |
| 40 | + written = snprintf( |
| 41 | + buffer, |
| 42 | + sizeof(buffer), |
| 43 | + "\n" |
| 44 | + "==================================================================" |
| 45 | + "==============\n" |
| 46 | + "%s: std::terminate() called\n" |
| 47 | + "==================================================================" |
| 48 | + "==============\n", |
| 49 | + __progname); |
| 50 | + |
| 51 | + if (written > 0 && (size_t)written < sizeof(buffer)) { |
| 52 | + if (write(STDERR_FILENO, buffer, (size_t)written) == -1) { |
| 53 | + // Suppress warning |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Try to get exception information |
| 58 | + std::type_info *exception_type = abi::__cxa_current_exception_type(); |
| 59 | + if (exception_type != nullptr) { |
| 60 | + char const *exception_name = exception_type->name(); |
| 61 | + |
| 62 | + // Try to demangle the name |
| 63 | + int status = 0; |
| 64 | + char *demangled = |
| 65 | + abi::__cxa_demangle(exception_name, nullptr, nullptr, &status); |
| 66 | + char const *display_name = (status == 0 && demangled != nullptr) |
| 67 | + ? demangled |
| 68 | + : exception_name; |
| 69 | + |
| 70 | + written = snprintf( |
| 71 | + buffer, |
| 72 | + sizeof(buffer), |
| 73 | + "Reason: Uncaught exception\n" |
| 74 | + "Exception type: %s\n", |
| 75 | + display_name); |
| 76 | + |
| 77 | + if (written > 0 && (size_t)written < sizeof(buffer)) { |
| 78 | + if (write(STDERR_FILENO, buffer, (size_t)written) == -1) { |
| 79 | + // Suppress warning |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Try to get exception message if it's a std::exception |
| 84 | + try { |
| 85 | + std::rethrow_exception(std::current_exception()); |
| 86 | + } |
| 87 | + catch (std::exception const &e) { |
| 88 | + written = snprintf( |
| 89 | + buffer, |
| 90 | + sizeof(buffer), |
| 91 | + "Exception message: %s\n", |
| 92 | + e.what()); |
| 93 | + |
| 94 | + if (written > 0 && (size_t)written < sizeof(buffer)) { |
| 95 | + if (write(STDERR_FILENO, buffer, (size_t)written) == -1) { |
| 96 | + // Suppress warning |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + catch (...) { |
| 101 | + // Not a std::exception, no message available |
| 102 | + char const *msg = "Exception message: <not a std::exception>\n"; |
| 103 | + if (write(STDERR_FILENO, msg, strlen(msg)) == -1) { |
| 104 | + // Suppress warning |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if (demangled != nullptr) { |
| 109 | + free(demangled); |
| 110 | + } |
| 111 | + } |
| 112 | + else { |
| 113 | + // No active exception - std::terminate() was called for another |
| 114 | + // reason. |
| 115 | + char const *msg = "No active exception detected\n"; |
| 116 | + if (write(STDERR_FILENO, msg, strlen(msg)) == -1) { |
| 117 | + // Suppress warning |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + char const *separator = "----------------------------------------------" |
| 122 | + "----------------------------------\n" |
| 123 | + "Stack trace:\n" |
| 124 | + "----------------------------------------------" |
| 125 | + "----------------------------------\n"; |
| 126 | + if (write(STDERR_FILENO, separator, strlen(separator)) == -1) { |
| 127 | + // Suppress warning |
| 128 | + } |
| 129 | + |
| 130 | + monad_stack_backtrace_capture_and_print( |
| 131 | + buffer, sizeof(buffer), STDERR_FILENO, 3, true); |
| 132 | + |
| 133 | + char const *footer = "=================================================" |
| 134 | + "===============================\n" |
| 135 | + "Aborting process...\n" |
| 136 | + "=================================================" |
| 137 | + "===============================\n"; |
| 138 | + if (write(STDERR_FILENO, footer, strlen(footer)) == -1) { |
| 139 | + // Suppress warning |
| 140 | + } |
| 141 | + abort(); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +extern "C" void monad_set_terminate_handler() |
| 146 | +{ |
| 147 | + std::set_terminate(monad_terminate_handler_impl); |
| 148 | +} |
0 commit comments