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