pastebin - collaborative debugging tool
rovema.kpaste.net RSS


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

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