pastebin - collaborative debugging tool
rovema.kpaste.net RSS


gdb_catch_c++_throw2.cpp
Posted by Anonymous on Wed 1st Sep 2021 15:25
raw | new post
modification of post by Anonymous (view diff)

  1. /*
  2.  * gdb_catch_c++_throw2.cpp demo
  3.  *
  4.  * Use like this:
  5.  * $ g++ -std=c++11 -g gdb_catch_c++_throw1.cpp
  6.  *
  7.  */
  8. #include <stdexcept>
  9. #include <iostream>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <unistd.h>
  13. #include <execinfo.h>
  14.  
  15. class myclass
  16. {
  17. public:
  18.         int death_is_coming = 10;
  19.        
  20.         /* recursive call to make stack trace bigger */
  21.         void countdown_to_death()
  22.         {
  23.                 if (this->death_is_coming-- > 0)
  24.                         this->countdown_to_death();
  25.                 this->print_and_throw();
  26.         }
  27.         void print_and_throw()
  28.         {
  29.                 std::cout << "#mark 1" << std::endl;
  30.                 throw std::exception(); /* gdb should catch this */
  31.         }
  32. };
  33.  
  34. /*
  35.  * std::exception will call std::terminate() to terminate the process.
  36.  * NO C++ CODE is allowed at this point, use plain ISO C and ISO C
  37.  * I/O only!!
  38.  */
  39. void std::terminate()
  40. {
  41. #define MAX_SIZE (1024)
  42.         size_t size;
  43.         void *array[MAX_SIZE];
  44.         size = backtrace(array, MAX_SIZE);
  45.         backtrace_symbols_fd(array, size, STDERR_FILENO);
  46.         exit(EXIT_FAILURE);
  47. }
  48.  
  49. int main(int ac, char **av)
  50. {
  51.         myclass x;
  52.         x.countdown_to_death();
  53.  
  54.         return EXIT_SUCCESS;
  55. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at