Guest User

Untitled

a guest
Nov 23rd, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. //============================================================================
  2. // Name        : SEG-FAULT-HANDLER.hxx
  3. // Author      : Beldyuga Ivan Vikt.
  4. // Version     : 2016/11/19 03:09:24
  5. // Copyright   :
  6. // Description : none.
  7. //============================================================================
  8. #pragma once
  9.  
  10. #include <iostream>
  11.  
  12. #include <sys/types.h>
  13. //#include <sys/wait.h>
  14.          
  15. #include <unistd.h>
  16. #include <signal.h>
  17.  
  18. #include <functional>
  19. std::function<void()> on_exception_callback = [](){};
  20. // void(*on_exception_callback)() = 0;
  21.  
  22. char call_gdb_buff[300];
  23.  
  24. inline void posix_signal(char const* str)
  25. {
  26. //     if( on_exception_callback != 0 )
  27.          on_exception_callback();
  28.      
  29.      std::cerr << std::endl << "\t" << str << ", enter 'y' to launch gdb..." << std::endl;
  30.      
  31.      std::string command;
  32.      std::cin >> command;
  33.  
  34.      pid_t child_pid;
  35.  
  36.      if( command == "y" )
  37.           switch( child_pid = fork() )
  38.           {
  39.                case 0:     // now it child
  40.                    std::cerr << "Child app: forking success, now debug." << std::endl;
  41.                    system( call_gdb_buff );
  42.                    break;
  43.                case -1:     // error in main app
  44.                     std::cerr << "Main app: forking error." << std::endl;
  45.                     break;
  46.                default:     // main app
  47.                     std::cerr << "Main app: child was forked." << std::endl;
  48.                     sleep(3); // it is need
  49.                     break;
  50.           }
  51. }
  52. void sigsegv_handler(int signum)
  53. {
  54.      posix_signal("SIGSEGV");
  55. }
  56. void sigfpe_handler(int signum)
  57. {
  58.      posix_signal("SIGFPE");
  59. }
  60.  
  61. void reg_death_signal(char * Prog_path)
  62. {
  63.      sprintf( call_gdb_buff, "gdb %s %d", Prog_path, getpid() );
  64.      signal(SIGSEGV, sigsegv_handler);
  65.      signal(SIGFPE, sigfpe_handler);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment