From: James Turner Date: Wed, 19 Feb 2014 21:53:52 +0000 (-0800) Subject: Re-factor the FPE handling code X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=39d1b6db389d49a8f0ffd9550aaf4637d0b9513d;p=flightgear.git Re-factor the FPE handling code * make --enable-fpe work on Linux and MSVC * standardise the code paths for different platforms * add an argument finding helper to Options (This is a basic cleanup, contributions from people with more experience in this area are welcome) --- diff --git a/src/Main/bootstrap.cxx b/src/Main/bootstrap.cxx index 326401a75..af643da78 100644 --- a/src/Main/bootstrap.cxx +++ b/src/Main/bootstrap.cxx @@ -29,13 +29,15 @@ #include #endif -#if defined(HAVE_FEENABLEEXCEPT) -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif -#include -#elif defined(__linux__) && defined(__i386__) -# include +#if defined(__linux__) +// set link for setting _GNU_SOURCE before including fenv.h +// http://man7.org/linux/man-pages/man3/fenv.3.html + + #ifndef _GNU_SOURCE + #define _GNU_SOURCE + #endif + + #include #endif #ifndef _WIN32 @@ -46,6 +48,8 @@ #include #include #include +#include +#include #include #include @@ -53,15 +57,11 @@ #include #include -#include -#include -using std::cerr; -using std::endl; - #include #include "main.hxx" #include #include
+#include
#include
#include @@ -78,19 +78,22 @@ bool global_crashRptEnabled = false; #endif +using std::cerr; +using std::endl; + std::string homedir; std::string hostname; // forward declaration. void fgExitCleanup(); -static bool fpeAbort = false; -static void initFPE(); +static void initFPE(bool enableExceptions); + +#if defined(__linux__) -#if defined(HAVE_FEENABLEEXCEPT) static void handleFPE(int); static void -initFPE () +initFPE (bool fpeAbort) { if (fpeAbort) { int except = fegetexcept(); @@ -103,35 +106,27 @@ initFPE () static void handleFPE(int) { feclearexcept(FE_ALL_EXCEPT); + SG_LOG(SG_GENERAL, SG_ALERT, "Floating point interrupt (SIGFPE)"); signal(SIGFPE, handleFPE); } -#elif defined(__linux__) && defined(__i386__) +#elif defined (SG_WINDOWS) -static void handleFPE(int); -static void -initFPE () +static void initFPE(bool fpeAbort) { - fpu_control_t fpe_flags = 0; - _FPU_GETCW(fpe_flags); -// fpe_flags &= ~_FPU_MASK_IM; // invalid operation -// fpe_flags &= ~_FPU_MASK_DM; // denormalized operand -// fpe_flags &= ~_FPU_MASK_ZM; // zero-divide -// fpe_flags &= ~_FPU_MASK_OM; // overflow -// fpe_flags &= ~_FPU_MASK_UM; // underflow -// fpe_flags &= ~_FPU_MASK_PM; // precision (inexact result) - _FPU_SETCW(fpe_flags); - signal(SIGFPE, handleFPE); +// Enable floating-point exceptions for Windows + if (fpeAbort) { + // set following link for what this does (note it does set SSE + // flags too, it's not just for the x87 FPU) + // http://msdn.microsoft.com/en-us/library/e9b52ceh.aspx + _control87( _EM_INEXACT, _MCW_EM ); + } } -static void -handleFPE (int num) -{ - initFPE(); - SG_LOG(SG_GENERAL, SG_ALERT, "Floating point interrupt (SIGFPE)"); -} #else -static void initFPE() +static void initFPE(bool) { + // Ignore floating-point exceptions on FreeBSD, OS-X, other Unices + signal(SIGFPE, SIG_IGN); } #endif @@ -144,11 +139,9 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, } #endif -static void fg_terminate() { - cerr << endl << - "Uncaught Exception: missing exception handler on some thread" - << endl << endl; - abort(); +static void fg_terminate() +{ + flightgear::fatalMessageBox("Fatal exception", "Uncaught exception on some thread"); } int _bootstrap_OSInit; @@ -172,16 +165,6 @@ int main ( int argc, char **argv ) signal(SIGPIPE, SIG_IGN); #endif -#if defined(SG_MAC) - // required so native messages boxes work prior to osgViewer init - // (only needed when not running as a bundled app) - transformToForegroundApp(); -#endif - -#ifdef PTW32_STATIC_LIB - // Initialise static pthread win32 lib - pthread_win32_process_attach_np (); -#endif _bootstrap_OSInit = 0; #if defined(HAVE_CRASHRPT) @@ -228,36 +211,9 @@ int main ( int argc, char **argv ) } #endif -#if defined(__FreeBSD__) - // Ignore floating-point exceptions on FreeBSD - signal(SIGFPE, SIG_IGN); -#else - // Maybe Enable floating-point exceptions on Linux - for (int i = 0; i < argc; ++i) { - if (!strcmp("--enable-fpe", argv[i])) { - fpeAbort = true; - break; - } - } - initFPE(); -#endif - - // Enable floating-point exceptions for Windows -#if defined( _MSC_VER ) && defined( DEBUG ) - // Christian, we should document what this does - _control87( _EM_INEXACT, _MCW_EM ); -#endif - - bool fgviewer = false; - for (int i = 0; i < argc; ++i) { - if (!strcmp("--fgviewer", argv[i])) { - fgviewer = true; - break; - } - } + initFPE(flightgear::Options::checkForArg(argc, argv, "enable-fpe")); - // FIXME: add other, more specific - // exceptions. + bool fgviewer = flightgear::Options::checkForArg(argc, argv, "fgviewer"); try { // http://code.google.com/p/flightgear-bugs/issues/detail?id=1231 // ensure sglog is inited before atexit() is registered, so logging @@ -288,10 +244,10 @@ int main ( int argc, char **argv ) } catch (const std::string &s) { flightgear::fatalMessageBox("Fatal exception", s); } catch (const char *s) { - cerr << "Fatal error (const char*): " << s << endl; + std::cerr << "Fatal error (const char*): " << s << std::endl; } catch (...) { - cerr << "Unknown exception in the main loop. Aborting..." << endl; + std::cerr << "Unknown exception in the main loop. Aborting..." << std::endl; if (errno) perror("Possible cause"); } diff --git a/src/Main/options.cxx b/src/Main/options.cxx index 70477ad2c..a9de344b6 100644 --- a/src/Main/options.cxx +++ b/src/Main/options.cxx @@ -2419,6 +2419,22 @@ bool Options::shouldLoadDefaultConfig() const { return p->shouldLoadDefaultConfig; } - + +bool Options::checkForArg(int argc, char* argv[], const char* checkArg) +{ + for (int i = 0; i < argc; ++i) { + char* arg = argv[i]; + if (!strncmp("--", arg, 2) && !strcmp(arg + 2, checkArg)) { + return true; + } + + if ((arg[0] == '-') && !strcmp(arg + 1, checkArg)) { + return true; + } + } + + return false; +} + } // of namespace flightgear diff --git a/src/Main/options.hxx b/src/Main/options.hxx index d0715cda2..97d71415a 100644 --- a/src/Main/options.hxx +++ b/src/Main/options.hxx @@ -113,6 +113,13 @@ public: * the value of the option here. */ bool shouldLoadDefaultConfig() const; + + /** + * check if the arguments array contains a particular string (with a '--' or + * '-' prefix). + * Used by early startup code before Options object is created + */ + static bool checkForArg(int argc, char* argv[], const char* arg); private: void showUsage() const;