Changeset e6955b1


Ignore:
Timestamp:
Aug 20, 2016, 7:04:17 PM (8 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
80722d0
Parents:
8b7ee09
git-author:
Peter A. Buhr <pabuhr@…> (08/20/16 13:18:07)
git-committer:
Peter A. Buhr <pabuhr@…> (08/20/16 19:04:17)
Message:

abort on assert rather than exit, print backtrace on cfa-cpp errors

Location:
src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/Common/Assert.cc

    r8b7ee09 re6955b1  
    1010// Created On       : Thu Aug 18 13:26:59 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug 18 23:40:29 2016
    13 // Update Count     : 8
     12// Last Modified On : Fri Aug 19 17:07:08 2016
     13// Update Count     : 10
    1414//
    1515
    1616#include <assert.h>
    17 #include <cstdarg>
    18 #include <cstdio>
    19 #include <cstdlib>
     17#include <cstdarg>                                                                              // varargs
     18#include <cstdio>                                                                               // fprintf
     19#include <cstdlib>                                                                              // abort
    2020
    2121extern const char * __progname;                                                 // global name of running executable (argv[0])
     
    2626void __assert_fail( const char *assertion, const char *file, unsigned int line, const char *function ) {
    2727        fprintf( stderr, CFA_ASSERT_FMT ".\n", __progname, function, line, file );
    28         exit( EXIT_FAILURE );
     28        abort();
    2929}
    3030
     
    3535        va_start( args, fmt );
    3636        vfprintf( stderr, fmt, args );
    37         exit( EXIT_FAILURE );
     37        abort();
    3838}
    3939
  • src/Makefile.am

    r8b7ee09 re6955b1  
    1111## Created On       : Sun May 31 08:51:46 2015
    1212## Last Modified By : Peter A. Buhr
    13 ## Last Modified On : Thu Aug 18 17:47:06 2016
    14 ## Update Count     : 70
     13## Last Modified On : Sat Aug 20 11:13:12 2016
     14## Update Count     : 71
    1515###############################################################################
    1616
     
    4343driver_cfa_cpp_SOURCES = ${SRC}
    4444driver_cfa_cpp_LDADD = ${LEXLIB}                        # yywrap
    45 driver_cfa_cpp_CXXFLAGS = -Wno-deprecated -Wall -DDEBUG_ALL -I${abs_top_srcdir}/src/include
     45driver_cfa_cpp_CXXFLAGS = -Wno-deprecated -Wall -DDEBUG_ALL -rdynamic -I${abs_top_srcdir}/src/include
    4646
    4747MAINTAINERCLEANFILES += ${libdir}/${notdir ${cfa_cpplib_PROGRAMS}}
  • src/Makefile.in

    r8b7ee09 re6955b1  
    427427driver_cfa_cpp_SOURCES = ${SRC}
    428428driver_cfa_cpp_LDADD = ${LEXLIB}                        # yywrap
    429 driver_cfa_cpp_CXXFLAGS = -Wno-deprecated -Wall -DDEBUG_ALL -I${abs_top_srcdir}/src/include
     429driver_cfa_cpp_CXXFLAGS = -Wno-deprecated -Wall -DDEBUG_ALL -rdynamic -I${abs_top_srcdir}/src/include
    430430all: $(BUILT_SOURCES)
    431431        $(MAKE) $(AM_MAKEFLAGS) all-am
  • src/main.cc

    r8b7ee09 re6955b1  
    1010// Created On       : Fri May 15 23:12:02 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Aug 19 08:31:22 2016
    13 // Update Count     : 350
     12// Last Modified On : Sat Aug 20 12:52:22 2016
     13// Update Count     : 403
    1414//
    1515
    1616#include <iostream>
    1717#include <fstream>
    18 #include <getopt.h>
     18#include <signal.h>                                                                             // signal
     19#include <getopt.h>                                                                             // getopt
     20#include <execinfo.h>                                                                   // backtrace, backtrace_symbols_fd
     21#include <cxxabi.h>                                                                             // __cxa_demangle
     22
     23using namespace std;
     24
    1925#include "Parser/lex.h"
    2026#include "Parser/parser.h"
     
    3541#include "InitTweak/FixInit.h"
    3642#include "Common/UnimplementedError.h"
    37 
    3843#include "../config.h"
    3944
    4045using namespace std;
    4146
    42 #define OPTPRINT(x) if ( errorp ) std::cerr << x << std::endl;
     47#define OPTPRINT(x) if ( errorp ) cerr << x << endl;
    4348
    4449
     
    6873static void parse_cmdline( int argc, char *argv[], const char *& filename );
    6974static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false );
    70 static void dump( std::list< Declaration * > & translationUnit, std::ostream & out = std::cout );
     75static void dump( list< Declaration * > & translationUnit, ostream & out = cout );
     76
     77void sigSegvBusHandler( int sig_num ) {
     78        enum { Frames = 50 };
     79        void * array[Frames];
     80        int size = backtrace( array, Frames );
     81
     82        cerr << "*CFA runtime error* program cfa-cpp terminated with "
     83                 <<     (sig_num == SIGSEGV ? "segment fault" : "bus error")
     84                 << " backtrace:" << endl;
     85
     86        char ** messages = backtrace_symbols( array, size );   
     87
     88        // skip first stack frame (points here)
     89        for ( int i = 2; i < size - 2 && messages != nullptr; i += 1 ) {
     90                char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr;
     91                for ( char *p = messages[i]; *p; ++p ) {        // find parantheses and +offset
     92                        if (*p == '(') {
     93                                mangled_name = p;
     94                        } else if (*p == '+') {
     95                                offset_begin = p;
     96                        } else if (*p == ')') {
     97                                offset_end = p;
     98                                break;
     99                        } // if
     100                } // for
     101
     102                // if line contains symbol, attempt to demangle
     103                if ( mangled_name && offset_begin && offset_end && mangled_name < offset_begin ) {
     104                        *mangled_name++ = '\0';
     105                        *offset_begin++ = '\0';
     106                        *offset_end++ = '\0';
     107
     108                        int status;
     109                        char * real_name = __cxxabiv1::__cxa_demangle( mangled_name, 0, 0, &status );
     110                        if ( status == 0 ) {                                            // demangling successful ?
     111                                cerr << "(" << i - 2 << ") " << messages[i] << " : "
     112                                         << real_name << "+" << offset_begin << offset_end << endl;
     113
     114                        } else {                                                                        // otherwise, output mangled name
     115                                cerr << "(" << i - 2 << ") " << messages[i] << " : "
     116                                         << mangled_name << "+" << offset_begin << offset_end << endl;
     117                        } // if
     118                        free( real_name );
     119                } else {                                                                                // otherwise, print the whole line
     120                        cerr << "(" << i - 2 << ") " << messages[i] << endl;
     121                } // if
     122        } // for
     123        free( messages );
     124        exit( EXIT_FAILURE );
     125} // sigSegvBusHandler
    71126
    72127int main( int argc, char * argv[] ) {
    73128        FILE * input;                                                                           // use FILE rather than istream because yyin is FILE
    74         std::ostream *output = & std::cout;
    75         std::list< Declaration * > translationUnit;
     129        ostream *output = & cout;
    76130        const char *filename = nullptr;
     131        list< Declaration * > translationUnit;
     132
     133        signal( SIGSEGV, sigSegvBusHandler );
     134        signal( SIGBUS, sigSegvBusHandler );
    77135
    78136        parse_cmdline( argc, argv, filename );                          // process command-line arguments
     
    122180
    123181                if ( parsep ) {
    124                         parseTree->printList( std::cout );
     182                        parseTree->printList( cout );
    125183                        delete parseTree;
    126184                        return 0;
     
    144202
    145203                if ( expraltp ) {
    146                         ResolvExpr::AlternativePrinter printer( std::cout );
     204                        ResolvExpr::AlternativePrinter printer( cout );
    147205                        acceptAll( translationUnit, printer );
    148206                        return 0;
     
    210268                CodeGen::generate( translationUnit, *output, ! noprotop );
    211269
    212                 if ( output != &std::cout ) {
     270                if ( output != &cout ) {
    213271                        delete output;
    214272                } // if
    215273        } catch ( SemanticError &e ) {
    216274                if ( errorp ) {
    217                         std::cerr << "---AST at error:---" << std::endl;
    218                         dump( translationUnit, std::cerr );
    219                         std::cerr << std::endl << "---End of AST, begin error message:---\n" << std::endl;
    220                 } // if
    221                 e.print( std::cerr );
    222                 if ( output != &std::cout ) {
     275                        cerr << "---AST at error:---" << endl;
     276                        dump( translationUnit, cerr );
     277                        cerr << endl << "---End of AST, begin error message:---\n" << endl;
     278                } // if
     279                e.print( cerr );
     280                if ( output != &cout ) {
    223281                        delete output;
    224282                } // if
    225283                return 1;
    226284        } catch ( UnimplementedError &e ) {
    227                 std::cout << "Sorry, " << e.get_what() << " is not currently implemented" << std::endl;
    228                 if ( output != &std::cout ) {
     285                cout << "Sorry, " << e.get_what() << " is not currently implemented" << endl;
     286                if ( output != &cout ) {
    229287                        delete output;
    230288                } // if
    231289                return 1;
    232290        } catch ( CompilerError &e ) {
    233                 std::cerr << "Compiler Error: " << e.get_what() << std::endl;
    234                 std::cerr << "(please report bugs to " << std::endl;
    235                 if ( output != &std::cout ) {
     291                cerr << "Compiler Error: " << e.get_what() << endl;
     292                cerr << "(please report bugs to " << endl;
     293                if ( output != &cout ) {
    236294                        delete output;
    237295                } // if
     
    369427} // notPrelude
    370428
    371 static void dump( std::list< Declaration * > & translationUnit, std::ostream & out ) {
    372         std::list< Declaration * > decls;
     429static void dump( list< Declaration * > & translationUnit, ostream & out ) {
     430        list< Declaration * > decls;
    373431
    374432        if ( noprotop ) {
    375                 filter( translationUnit.begin(), translationUnit.end(), std::back_inserter( decls ), notPrelude );
     433                filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), notPrelude );
    376434        } else {
    377435                decls = translationUnit;
Note: See TracChangeset for help on using the changeset viewer.