- /*
- * gdb_catch_c++_throw2.cpp demo
- *
- * Use like this:
- * $ g++ -std=c++11 -g gdb_catch_c++_throw1.cpp
- *
- * Inferior 1 [process 21238] will be killed.
- */
- #include <stdexcept>
- #include <iostream>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <execinfo.h>
- class myclass
- {
- public:
- int death_is_coming = 10;
- /* recursive call to make stack trace bigger */
- void countdown_to_death()
- {
- if (this->death_is_coming-- > 0)
- this->countdown_to_death();
- this->print_and_throw();
- }
- void print_and_throw()
- {
- std::cout << "#mark 1" << std::endl;
- throw std::exception(); /* gdb should catch this */
- }
- };
- /*
- * std::exception will call std::terminate() to terminate the process.
- * NO C++ CODE is allowed at this point, use plain ISO C and ISO C
- * I/O only!!
- */
- void std::terminate()
- {
- #define MAX_SIZE (1024)
- size_t size;
- void *array[MAX_SIZE];
- size = backtrace(array, MAX_SIZE);
- backtrace_symbols_fd(array, size, STDERR_FILENO);
- exit(EXIT_FAILURE);
- }
- int main(int ac, char **av)
- {
- myclass x;
- x.countdown_to_death();
- return EXIT_SUCCESS;
- }
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)
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.