Re: How to force gcc to dump core on FP error

hvaisane@cs.joensuu.fi (H. Vaisanen)
Thu, 6 May 1993 05:46:20 GMT

          From comp.compilers

Related articles
How to force gcc to dump core on FP error raynor@cs.scarolina.edu (1993-04-24)
Re: How to force gcc to dump core on FP error segfault!rfg@uunet.UU.NET (1993-05-05)
Re: How to force gcc to dump core on FP error hvaisane@cs.joensuu.fi (1993-05-06)
| List of all articles for this month |

Newsgroups: comp.compilers,comp.unix.ultrix
From: hvaisane@cs.joensuu.fi (H. Vaisanen)
Keywords: GCC, debug
Organization: University of Joensuu
References: 93-04-097 93-05-022
Date: Thu, 6 May 1993 05:46:20 GMT

segfault!rfg@uunet.UU.NET (Ron Guilmette) writes:
>Anyway, if you figure out how to enable the IEEE traps on a DECstation,
>please let me know how to do it.




/* etest.c Define exception handlers */
/* */
/* Compile: gcc etest.c -lm -o etest */
/* Run: etest -x */
/* */
/* where x is a, b, c, d, e or f */




#include <math.h>
#include <mips/fpu.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>




static char *signals[] = {
    " ", /* No error */
    "SIGHUP 1 Hangup",
    "SIGINT 2 Interrupt",
    "SIGQUIT 3 Quit",
    "SIGILL 4 Illegal instruction",
    "SIGTRAP 5 Trace trap",
    "SIGIOT 6 IOT instruction",
    "SIGEMT 7 EMT instruction",
    "SIGFPE 8 Floating point exception",
    "SIGKILL 9 Kill (cannot be caught or ignored)",
    "SIGBUS 10 Bus error",
    "SIGSEGV 11 Segmentation violation",
    "SIGSYS 12 Bad argument to system call",
    "SIGPIPE 13 write on a pipe with no one to read it",
    "SIGALRM 14 Alarm clock",
    "SIGTERM 15 Software termination signal",
    "SIGURG 16 Urgent condition present on socket",
    "SIGSTOP 17 Stop (cannot be caught or ignored)",
    "SIGTSTP 18 Stop signal generated from keyboard",
    "SIGCONT 19 Continue after stop",
    "SIGCHLD 20 Child status has changed",
    "SIGTTIN 21 Background read attempted from control terminal",
    "SIGTTOU 22 Background write attempted to control terminal",
    "SIGIO 23 I/O is possible on a descriptor (see fcntl(2))",
    "SIGXCPU 24 Cpu time limit exceeded (see setrlimit(2))",
    "SIGXFSZ 25 File size limit exceeded (see setrlimit(2))",
    "SIGVTALRM 26 Virtual time alarm (see setitimer(2))",
    "SIGPROF 27 Profiling timer alarm (see setitimer(2))",
    "SIGWINCH 28 Window size change",
    "SIGLOST 29 lock not reclaimed after server recovery",
    "SIGUSR1 30 User defined signal",
    "SIGUSR2 31 User defined signal",
};






/* Exception handler for Decstation */


int handler (sig, code, scp)
int sig, code;
struct sigcontext *scp;
{
    union fpc_csr r;
    r.fc_word = get_fpc_csr();


    if (sig >= 1 && sig <= 31) {
        printf ("%s\n", signals[sig]);
    }
    else {
        printf ("Exception (sig = %d code = %d)", sig, code);
    }


    if (r.fc_struct.se_invalid) {
        printf ("Invalid instruction\n");
    }
    else if (r.fc_struct.se_divide0) {
        printf ("Divide by zero\n");
    }
    else if (r.fc_struct.se_overflow) {
        printf ("Overflow\n");
    }
    fflush (stdout);
    abort ();
}




void set_exception_handler()
{
    union fpc_csr r;


    r.fc_struct.en_invalid = 1;
    r.fc_struct.en_divide0 = 1;
    r.fc_struct.en_overflow = 1;


    set_fpc_csr(r);


    signal (SIGFPE, handler);
}




#define USAGE() fprintf (stdout, "Usage: etest [-abcdef]\n")




int main (int argc, char **argv)
{
    float a, b = 10.0, c = 0;
    int i;
    extern int opterr;


    set_exception_handler();


    opterr = 0;


    while ((i = getopt (argc, argv, "abcdef")) != EOF) {
        switch (i) {
            case 'a':
                fprintf (stdout, "Test divide by zero\n");
                fflush (stdout);
                a = b / c;
                fprintf (stdout, "a = %f\n", a);
                break;
            case 'b':
                fprintf (stdout, "Test wrong argument to sqrt()\n");
                fflush (stdout);
                a = sqrt (-1.0);
                fprintf (stdout, "a = %f\n", a);
                break;
            case 'c':
                fprintf (stdout, "Test overflow\n");
                fflush (stdout);
                a = pow (10.0, 1.0e200);
                fprintf (stdout, "a = %f\n", a);
                break;
            case 'd':
                fprintf (stdout, "This should return 1\n");
                fflush (stdout);
                a = pow (0.0, 0.0);
                fprintf (stdout, "a = %f\n", a);
                break;
            case 'e':
                fprintf (stdout, "Test wrong argument to pow()\n");;
                fflush (stdout);
                a = pow (-10.0, 0.9);
                fprintf (stdout, "a = %f\n", a);
                break;
            case 'f':
                fprintf (stdout, "This should return -Infinity\n");
                fflush (stdout);
                a = pow (0.0, -2.0);
                fprintf (stdout, "a = %f\n", a);
                break;
            case '?':
            default:
                USAGE();
                exit (1);
                break;
        }
    }


    if (argc == 1)
        USAGE();


    return 0;
}
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.