Changeset bffcd66 for src


Ignore:
Timestamp:
Dec 16, 2019, 2:31:10 PM (5 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
6f9cc13
Parents:
07de76b
Message:

harmonize signal handling in main.cc with interpose.cfa, consider refactoring all signal code into a separate module

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main.cc

    r07de76b rbffcd66  
    1010// Created On       : Fri May 15 23:12:02 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Aug 23 06:50:08 2019
    13 // Update Count     : 607
     12// Last Modified On : Fri Dec 13 23:47:06 2019
     13// Update Count     : 626
    1414//
    1515
     
    2020#include <cstdio>                           // for fopen, FILE, fclose, stdin
    2121#include <cstdlib>                          // for exit, free, abort, EXIT_F...
    22 #include <csignal>                         // for signal, SIGABRT, SIGSEGV
     22#include <csignal>                          // for signal, SIGABRT, SIGSEGV
    2323#include <cstring>                          // for index
    2424#include <fstream>                          // for ofstream
     
    2828#include <list>                             // for list
    2929#include <string>                           // for char_traits, operator<<
     30
     31using namespace std;
     32
    3033
    3134#include "CompilationState.h"
     
    5356#include "InitTweak/GenInit.h"              // for genInit
    5457#include "MakeLibCfa.h"                     // for makeLibCfa
    55 #include "Parser/LinkageSpec.h"             // for Spec, Cforall, Intrinsic
    5658#include "Parser/ParseNode.h"               // for DeclarationNode, buildList
    5759#include "Parser/TypedefTable.h"            // for TypedefTable
     
    5961#include "ResolvExpr/Resolver.h"            // for resolve
    6062#include "SymTab/Validate.h"                // for validate
     63#include "SynTree/LinkageSpec.h"            // for Spec, Cforall, Intrinsic
    6164#include "SynTree/Declaration.h"            // for Declaration
    6265#include "SynTree/Visitor.h"                // for acceptAll
     
    6467#include "Virtual/ExpandCasts.h"            // for expandCasts
    6568
    66 
    67 using namespace std;
    6869
    6970static void NewPass( const char * const name ) {
     
    9798static bool waiting_for_gdb = false;                                    // flag to set cfa-cpp to wait for gdb on start
    9899
    99 static std::string PreludeDirector = "";
     100static string PreludeDirector = "";
    100101
    101102static void parse_cmdline( int argc, char *argv[] );
     
    153154} // backtrace
    154155
    155 static void sigSegvBusHandler( int sig_num ) {
    156         cerr << "*CFA runtime error* program cfa-cpp terminated with "
    157                  <<     (sig_num == SIGSEGV ? "segment fault" : "bus error")
    158                  << "." << endl;
     156#define SIGPARMS int sig __attribute__(( unused )), siginfo_t * sfp __attribute__(( unused )), ucontext_t * cxt __attribute__(( unused ))
     157
     158static void Signal( int sig, void (*handler)(SIGPARMS), int flags ) {
     159        struct sigaction act;
     160
     161        act.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler;
     162        sigemptyset( &act.sa_mask );
     163        sigaddset( &act.sa_mask, SIGALRM );                                     // disabled during signal handler
     164        sigaddset( &act.sa_mask, SIGUSR1 );
     165        act.sa_flags = flags;
     166
     167        if ( sigaction( sig, &act, nullptr ) == -1 ) {
     168            cerr << "*CFA runtime error* problem installing signal handler, error(" << errno << ") " << strerror( errno ) << endl;
     169            _exit( EXIT_FAILURE );
     170        } // if
     171} // Signal
     172
     173static void sigSegvBusHandler( SIGPARMS ) {
     174        if ( sfp->si_addr == nullptr ) {
     175                cerr << "Null pointer (nullptr) dereference." << endl;
     176        } else {
     177                cerr << (sig == SIGSEGV ? "Segment fault" : "Bus error") << " at memory location " << sfp->si_addr << "." << endl
     178                         << "Possible cause is reading outside the address space or writing to a protected area within the address space with an invalid pointer or subscript." << endl;
     179        } // if
    159180        backtrace( 2 );                                                                         // skip first 2 stack frames
    160         //_exit( EXIT_FAILURE );
    161181        abort();                                                                                        // cause core dump for debugging
    162182} // sigSegvBusHandler
    163183
    164 static void sigAbortHandler( __attribute__((unused)) int sig_num ) {
     184static void sigAbortHandler( SIGPARMS ) {
    165185        backtrace( 6 );                                                                         // skip first 6 stack frames
    166         signal( SIGABRT, SIG_DFL);                                                      // reset default signal handler
     186        Signal( SIGABRT, (void (*)(SIGPARMS))SIG_DFL, SA_SIGINFO );     // reset default signal handler
    167187        raise( SIGABRT );                                                                       // reraise SIGABRT
    168188} // sigAbortHandler
     
    173193        list< Declaration * > translationUnit;
    174194
    175         signal( SIGSEGV, sigSegvBusHandler );
    176         signal( SIGBUS, sigSegvBusHandler );
    177         signal( SIGABRT, sigAbortHandler );
    178 
    179         // std::cout << "main" << std::endl;
     195        Signal( SIGSEGV, sigSegvBusHandler, SA_SIGINFO );
     196        Signal( SIGBUS, sigSegvBusHandler, SA_SIGINFO );
     197        Signal( SIGABRT, sigAbortHandler, SA_SIGINFO );
     198
     199        // cout << "main" << endl;
    180200        // for ( int i = 0; i < argc; i += 1 ) {
    181         //      std::cout << '\t' << argv[i] << std::endl;
     201        //      cout << '\t' << argv[i] << endl;
    182202        // } // for
    183203
     
    186206
    187207        if ( waiting_for_gdb ) {
    188                 std::cerr << "Waiting for gdb" << std::endl;
    189                 std::cerr << "run :" << std::endl;
    190                 std::cerr << "  gdb attach " << getpid() << std::endl;
     208                cerr << "Waiting for gdb" << endl;
     209                cerr << "run :" << endl;
     210                cerr << "  gdb attach " << getpid() << endl;
    191211                raise(SIGSTOP);
    192212        } // if
     
    388408                return EXIT_FAILURE;
    389409        } catch ( ... ) {
    390                 std::exception_ptr eptr = std::current_exception();
     410                exception_ptr eptr = current_exception();
    391411                try {
    392412                        if (eptr) {
    393                                 std::rethrow_exception(eptr);
     413                                rethrow_exception(eptr);
    394414                        } else {
    395                                 std::cerr << "Exception Uncaught and Unknown" << std::endl;
    396                         } // if
    397                 } catch(const std::exception& e) {
    398                         std::cerr << "Uncaught Exception \"" << e.what() << "\"\n";
     415                                cerr << "Exception Uncaught and Unknown" << endl;
     416                        } // if
     417                } catch(const exception& e) {
     418                        cerr << "Uncaught Exception \"" << e.what() << "\"\n";
    399419                } // try
    400420                return EXIT_FAILURE;
Note: See TracChangeset for help on using the changeset viewer.