1 #include <config.h> 2 /* This file is part of Jellyfish. 3 4 This work is dual-licensed under 3-Clause BSD License or GPL 3.0. 5 You can choose between one of them if you use this work. 6 7 `SPDX-License-Identifier: BSD-3-Clause OR GPL-3.0` 8 */ 9 10 #ifndef HAVE_EXECINFO_H 11 show_backtrace()12void show_backtrace() {} 13 14 #else 15 16 #include <iostream> 17 #include <stdexcept> 18 #include <execinfo.h> 19 #include <stdlib.h> 20 #include <typeinfo> 21 #include <cxxabi.h> 22 print_backtrace()23void print_backtrace() { 24 void *trace_elems[20]; 25 int trace_elem_count(backtrace(trace_elems, 20)); 26 backtrace_symbols_fd(trace_elems, trace_elem_count, 2); 27 } 28 handler()29static void handler() { 30 // Display message of last thrown exception if any 31 try { throw; } 32 catch(const std::exception& e) { 33 int status; 34 size_t n = 0; 35 char *name = abi::__cxa_demangle(typeid(e).name(), 0, &n, &status); 36 std::cerr << "terminate called after throwing an instance of '" 37 << (status < 0 ? "UNKNOWN" : name) 38 << "'\n what(): " << e.what() << "\n"; 39 if(n) 40 free(name); 41 } 42 catch(...) {} 43 44 print_backtrace(); 45 abort(); 46 } 47 show_backtrace()48void show_backtrace() { 49 std::set_terminate(handler); 50 } 51 52 #endif 53