Changes in src/main.cc [74330e7:6559a9d]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main.cc
r74330e7 r6559a9d 10 10 // Created On : Fri May 15 23:12:02 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat Feb 8 08:33:50 202013 // Update Count : 6 3312 // Last Modified On : Fri Aug 23 06:50:08 2019 13 // Update Count : 607 14 14 // 15 15 … … 20 20 #include <cstdio> // for fopen, FILE, fclose, stdin 21 21 #include <cstdlib> // for exit, free, abort, EXIT_F... 22 #include <csignal> 22 #include <csignal> // for signal, SIGABRT, SIGSEGV 23 23 #include <cstring> // for index 24 24 #include <fstream> // for ofstream … … 28 28 #include <list> // for list 29 29 #include <string> // for char_traits, operator<< 30 31 using namespace std;32 33 30 34 31 #include "CompilationState.h" … … 56 53 #include "InitTweak/GenInit.h" // for genInit 57 54 #include "MakeLibCfa.h" // for makeLibCfa 55 #include "Parser/LinkageSpec.h" // for Spec, Cforall, Intrinsic 58 56 #include "Parser/ParseNode.h" // for DeclarationNode, buildList 59 57 #include "Parser/TypedefTable.h" // for TypedefTable … … 61 59 #include "ResolvExpr/Resolver.h" // for resolve 62 60 #include "SymTab/Validate.h" // for validate 63 #include "SynTree/LinkageSpec.h" // for Spec, Cforall, Intrinsic64 61 #include "SynTree/Declaration.h" // for Declaration 65 62 #include "SynTree/Visitor.h" // for acceptAll … … 67 64 #include "Virtual/ExpandCasts.h" // for expandCasts 68 65 66 67 using namespace std; 69 68 70 69 static void NewPass( const char * const name ) { … … 98 97 static bool waiting_for_gdb = false; // flag to set cfa-cpp to wait for gdb on start 99 98 100 static st ring PreludeDirector = "";99 static std::string PreludeDirector = ""; 101 100 102 101 static void parse_cmdline( int argc, char *argv[] ); … … 105 104 106 105 static void backtrace( int start ) { // skip first N stack frames 107 enum { Frames = 50 , }; // maximum number of stack frames106 enum { Frames = 50 }; 108 107 void * array[Frames]; 109 size_t size = ::backtrace( array, Frames );108 int size = ::backtrace( array, Frames ); 110 109 char ** messages = ::backtrace_symbols( array, size ); // does not demangle names 111 110 … … 114 113 115 114 // skip last 2 stack frames after main 116 for ( unsignedint i = start; i < size - 2 && messages != nullptr; i += 1 ) {115 for ( int i = start; i < size - 2 && messages != nullptr; i += 1 ) { 117 116 char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr; 118 119 for ( char * p = messages[i]; *p; p += 1 ) { // find parantheses and +offset 117 for ( char *p = messages[i]; *p; ++p ) { // find parantheses and +offset 120 118 if ( *p == '(' ) { 121 119 mangled_name = p; … … 155 153 } // backtrace 156 154 157 #define SIGPARMS int sig __attribute__(( unused )), siginfo_t * sfp __attribute__(( unused )), ucontext_t * cxt __attribute__(( unused )) 158 159 static void Signal( int sig, void (*handler)(SIGPARMS), int flags ) { 160 struct sigaction act; 161 162 act.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler; 163 act.sa_flags = flags; 164 165 if ( sigaction( sig, &act, nullptr ) == -1 ) { 166 cerr << "*CFA runtime error* problem installing signal handler, error(" << errno << ") " << strerror( errno ) << endl; 167 _exit( EXIT_FAILURE ); 168 } // if 169 } // Signal 170 171 static void sigSegvBusHandler( SIGPARMS ) { 172 if ( sfp->si_addr == nullptr ) { 173 cerr << "Null pointer (nullptr) dereference." << endl; 174 } else { 175 cerr << (sig == SIGSEGV ? "Segment fault" : "Bus error") << " at memory location " << sfp->si_addr << "." << endl 176 << "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; 177 } // if 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; 178 159 backtrace( 2 ); // skip first 2 stack frames 160 //_exit( EXIT_FAILURE ); 179 161 abort(); // cause core dump for debugging 180 162 } // sigSegvBusHandler 181 163 182 static void sigFpeHandler( SIGPARMS ) { 183 const char * msg; 184 185 switch ( sfp->si_code ) { 186 case FPE_INTDIV: case FPE_FLTDIV: msg = "divide by zero"; break; 187 case FPE_FLTOVF: msg = "overflow"; break; 188 case FPE_FLTUND: msg = "underflow"; break; 189 case FPE_FLTRES: msg = "inexact result"; break; 190 case FPE_FLTINV: msg = "invalid operation"; break; 191 default: msg = "unknown"; 192 } // choose 193 cerr << "Computation error " << msg << " at location " << sfp->si_addr << endl 194 << "Possible cause is constant-expression evaluation invalid." << endl; 195 backtrace( 2 ); // skip first 2 stack frames 196 abort(); // cause core dump for debugging 197 } // sigFpeHandler 198 199 static void sigAbortHandler( SIGPARMS ) { 164 static void sigAbortHandler( __attribute__((unused)) int sig_num ) { 200 165 backtrace( 6 ); // skip first 6 stack frames 201 Signal( SIGABRT, (void (*)(SIGPARMS))SIG_DFL, SA_SIGINFO );// reset default signal handler166 signal( SIGABRT, SIG_DFL); // reset default signal handler 202 167 raise( SIGABRT ); // reraise SIGABRT 203 168 } // sigAbortHandler … … 208 173 list< Declaration * > translationUnit; 209 174 210 Signal( SIGSEGV, sigSegvBusHandler, SA_SIGINFO ); 211 Signal( SIGBUS, sigSegvBusHandler, SA_SIGINFO ); 212 Signal( SIGFPE, sigFpeHandler, SA_SIGINFO ); 213 Signal( SIGABRT, sigAbortHandler, SA_SIGINFO ); 214 215 // cout << "main" << endl; 175 signal( SIGSEGV, sigSegvBusHandler ); 176 signal( SIGBUS, sigSegvBusHandler ); 177 signal( SIGABRT, sigAbortHandler ); 178 179 // std::cout << "main" << std::endl; 216 180 // for ( int i = 0; i < argc; i += 1 ) { 217 // cout << '\t' << argv[i] <<endl;181 // std::cout << '\t' << argv[i] << std::endl; 218 182 // } // for 219 183 … … 222 186 223 187 if ( waiting_for_gdb ) { 224 cerr << "Waiting for gdb" <<endl;225 cerr << "run :" <<endl;226 cerr << " gdb attach " << getpid() <<endl;188 std::cerr << "Waiting for gdb" << std::endl; 189 std::cerr << "run :" << std::endl; 190 std::cerr << " gdb attach " << getpid() << std::endl; 227 191 raise(SIGSTOP); 228 192 } // if … … 424 388 return EXIT_FAILURE; 425 389 } catch ( ... ) { 426 exception_ptr eptr =current_exception();390 std::exception_ptr eptr = std::current_exception(); 427 391 try { 428 392 if (eptr) { 429 rethrow_exception(eptr);393 std::rethrow_exception(eptr); 430 394 } else { 431 cerr << "Exception Uncaught and Unknown" <<endl;432 } // if 433 } catch(const exception& e) {434 cerr << "Uncaught Exception \"" << e.what() << "\"\n";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"; 435 399 } // try 436 400 return EXIT_FAILURE; … … 443 407 444 408 445 static const char optstring[] = ": c:ghlLmNnpP:S:twW:D:";409 static const char optstring[] = ":hlLmNnpP:S:tgwW:D:"; 446 410 447 411 enum { PreludeDir = 128 }; 448 412 static struct option long_opts[] = { 449 { "colors", required_argument, nullptr, 'c' },450 { "gdb", no_argument, nullptr, 'g' },451 413 { "help", no_argument, nullptr, 'h' }, 452 414 { "libcfa", no_argument, nullptr, 'l' }, … … 460 422 { "statistics", required_argument, nullptr, 'S' }, 461 423 { "tree", no_argument, nullptr, 't' }, 424 { "gdb", no_argument, nullptr, 'g' }, 462 425 { "", no_argument, nullptr, 0 }, // -w 463 426 { "", no_argument, nullptr, 0 }, // -W … … 467 430 468 431 static const char * description[] = { 469 "diagnostic color: never, always, or auto.", // -c 470 "wait for gdb to attach", // -g 471 "print help message", // -h 472 "generate libcfa.c", // -l 473 "generate line marks", // -L 474 "do not replace main", // -m 475 "do not generate line marks", // -N 476 "do not read prelude", // -n 432 "print help message", // -h 433 "generate libcfa.c", // -l 434 "generate line marks", // -L 435 "do not replace main", // -m 436 "do not generate line marks", // -N 437 "do not read prelude", // -n 477 438 "generate prototypes for prelude functions", // -p 478 "print", 439 "print", // -P 479 440 "<directory> prelude directory for debug/nodebug", // no flag 480 441 "<option-list> enable profiling information:\n counters,heap,time,all,none", // -S 481 "building cfa standard lib", // -t 482 "", // -w 483 "", // -W 484 "", // -D 442 "building cfa standard lib", // -t 443 "wait for gdb to attach", // -g 444 "", // -w 445 "", // -W 446 "", // -D 485 447 }; // description 486 448 … … 550 512 while ( (c = getopt_long( argc, argv, optstring, long_opts, nullptr )) != -1 ) { 551 513 switch ( c ) { 552 case 'c': // diagnostic colors553 if ( strcmp( optarg, "always" ) == 0 ) {554 ErrorHelpers::colors = ErrorHelpers::Colors::Always;555 } else if ( strcmp( optarg, "never" ) == 0 ) {556 ErrorHelpers::colors = ErrorHelpers::Colors::Never;557 } else if ( strcmp( optarg, "auto" ) == 0 ) {558 ErrorHelpers::colors = ErrorHelpers::Colors::Auto;559 } // if560 break;561 514 case 'h': // help message 562 515 usage( argv ); // no return
Note:
See TracChangeset
for help on using the changeset viewer.