1#include "ggml-impl.h"
 2
 3#include <cstdlib>
 4#include <exception>
 5
 6static std::terminate_handler previous_terminate_handler;
 7
 8GGML_NORETURN static void ggml_uncaught_exception() {
 9    ggml_print_backtrace();
10    if (previous_terminate_handler) {
11        previous_terminate_handler();
12    }
13    abort(); // unreachable unless previous_terminate_handler was nullptr
14}
15
16static bool ggml_uncaught_exception_init = []{
17    const char * GGML_NO_BACKTRACE = getenv("GGML_NO_BACKTRACE");
18    if (GGML_NO_BACKTRACE) {
19        return false;
20    }
21    const auto prev{std::get_terminate()};
22    GGML_ASSERT(prev != ggml_uncaught_exception);
23    previous_terminate_handler = prev;
24    std::set_terminate(ggml_uncaught_exception);
25    return true;
26}();