debug_function.cpp 796 B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright Antony Polukhin, 2016-2019.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //[getting_started_debug_function
  7. #include <signal.h> // ::signal
  8. #include <boost/stacktrace/frame.hpp>
  9. #include <iostream> // std::cerr
  10. #include <cstdlib> // std::exit
  11. void print_signal_handler_and_exit() {
  12. typedef void(*function_t)(int);
  13. function_t old_signal_function = ::signal(SIGSEGV, SIG_DFL);
  14. boost::stacktrace::frame f(old_signal_function);
  15. std::cout << f << std::endl;
  16. std::exit(0);
  17. }
  18. //]
  19. void my_signal_handler(int /*signum*/) {
  20. std::exit(1);
  21. }
  22. int main() {
  23. ::signal(SIGSEGV, &my_signal_handler);
  24. print_signal_handler_and_exit();
  25. }