Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //============================================================================
- // Name : SEG-FAULT-HANDLER.hxx
- // Author : Beldyuga Ivan Vikt.
- // Version : 2016/11/19 03:09:24
- // Copyright :
- // Description : none.
- //============================================================================
- #pragma once
- #include <iostream>
- #include <sys/types.h>
- //#include <sys/wait.h>
- #include <unistd.h>
- #include <signal.h>
- #include <functional>
- std::function<void()> on_exception_callback = [](){};
- // void(*on_exception_callback)() = 0;
- char call_gdb_buff[300];
- inline void posix_signal(char const* str)
- {
- // if( on_exception_callback != 0 )
- on_exception_callback();
- std::cerr << std::endl << "\t" << str << ", enter 'y' to launch gdb..." << std::endl;
- std::string command;
- std::cin >> command;
- pid_t child_pid;
- if( command == "y" )
- switch( child_pid = fork() )
- {
- case 0: // now it child
- std::cerr << "Child app: forking success, now debug." << std::endl;
- system( call_gdb_buff );
- break;
- case -1: // error in main app
- std::cerr << "Main app: forking error." << std::endl;
- break;
- default: // main app
- std::cerr << "Main app: child was forked." << std::endl;
- sleep(3); // it is need
- break;
- }
- }
- void sigsegv_handler(int signum)
- {
- posix_signal("SIGSEGV");
- }
- void sigfpe_handler(int signum)
- {
- posix_signal("SIGFPE");
- }
- void reg_death_signal(char * Prog_path)
- {
- sprintf( call_gdb_buff, "gdb %s %d", Prog_path, getpid() );
- signal(SIGSEGV, sigsegv_handler);
- signal(SIGFPE, sigfpe_handler);
- }
Advertisement
Add Comment
Please, Sign In to add comment