pastebin - collaborative debugging tool
rovema.kpaste.net RSS


gdb_catch_c++_throw1.cpp
Posted by Anonymous on Wed 1st Sep 2021 13:56
raw | new post
view followups (newest first): gdb_catch_c++_throw2.cpp by Anonymous

  1. /*
  2.  * gdb_catch_c++_throw1.cpp demo
  3.  *
  4.  * Use like this:
  5.  * $ g++ -std=c++11 -g gdb_catch_c++_throw1.cpp
  6.  * $ printf 'catch throw\nrun\nwhere\n' | gdb --args ./a.out 2>&1 | awk '/exception thrown/,EOF { print $0 }'
  7.  * Catchpoint 1 (exception thrown), 0x00007ffff7e80c2e in __cxa_throw ()
  8.  *    from /lib/x86_64-linux-gnu/libstdc++.so.6
  9.  * (gdb) #0  0x00007ffff7e80c2e in __cxa_throw ()
  10.  *    from /lib/x86_64-linux-gnu/libstdc++.so.6
  11.  * #1  0x00005555555552c9 in myclass::print_and_throw (this=0x7fffffffdefc)
  12.  *     at test1.cpp:19
  13.  * #2  0x0000555555555262 in myclass::countdown_to_death (this=0x7fffffffdefc)
  14.  *     at test1.cpp:14
  15.  * #3  0x0000555555555256 in myclass::countdown_to_death (this=0x7fffffffdefc)
  16.  *     at test1.cpp:13
  17.  * #4  0x0000555555555256 in myclass::countdown_to_death (this=0x7fffffffdefc)
  18.  *     at test1.cpp:13
  19.  * #5  0x0000555555555256 in myclass::countdown_to_death (this=0x7fffffffdefc)
  20.  *     at test1.cpp:13
  21.  * #6  0x0000555555555256 in myclass::countdown_to_death (this=0x7fffffffdefc)
  22.  *     at test1.cpp:13
  23.  * #7  0x0000555555555256 in myclass::countdown_to_death (this=0x7fffffffdefc)
  24.  *     at test1.cpp:13
  25.  * #8  0x0000555555555256 in myclass::countdown_to_death (this=0x7fffffffdefc)
  26.  *     at test1.cpp:13
  27.  * #9  0x0000555555555256 in myclass::countdown_to_death (this=0x7fffffffdefc)
  28.  *     at test1.cpp:13
  29.  * #10 0x0000555555555256 in myclass::countdown_to_death (this=0x7fffffffdefc)
  30.  *     at test1.cpp:13
  31.  * #11 0x0000555555555256 in myclass::countdown_to_death (this=0x7fffffffdefc)
  32.  *     at test1.cpp:13
  33.  * #12 0x0000555555555256 in myclass::countdown_to_death (this=0x7fffffffdefc)
  34.  *     at test1.cpp:13
  35.  * #13 0x00005555555551a7 in main (ac=1, av=0x7fffffffdff8) at test1.cpp:26
  36.  * (gdb) quit
  37.  * A debugging session is active.
  38.  *
  39.  *         Inferior 1 [process 21238] will be killed.
  40.  */
  41. #include <stdexcept>
  42. #include <iostream>
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45.  
  46. class myclass
  47. {
  48. public:
  49.         int death_is_coming = 10;
  50.        
  51.         /* recursive call to make stack trace bigger */
  52.         void countdown_to_death()
  53.         {
  54.                 if (this->death_is_coming-- > 0)
  55.                         this->countdown_to_death();
  56.                 this->print_and_throw();
  57.         }
  58.         void print_and_throw()
  59.         {
  60.                 std::cout << "#mark 1" << std::endl;
  61.                 throw std::exception(); /* gdb should catch this */
  62.         }
  63. };
  64.  
  65. int main(int ac, char **av)
  66. {
  67.         myclass x;
  68.         x.countdown_to_death();
  69.  
  70.         return EXIT_SUCCESS;
  71. }

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