How to get further information on SIGFPE signal?

亡梦爱人 提交于 2021-02-16 20:11:13

问题


This is from The GNU C Library Reference Manual

int SIGFPE

The SIGFPE signal reports a fatal arithmetic error. This signal actually covers all arithmetic errors, including division by zero and overflow.

BSD systems provide the SIGFPE handler with an extra argument that distinguishes various causes of the exception. In order to access this argument, you must define the handler to accept two arguments, which means you must cast it to a one-argument function type in order to establish the handler.

But there is no example on how to access the extra argument.

I did my google work but could not find anything.

How can I get this extra information?


回答1:


As EOF mentioned in the comments, the much better way to do this, that doesn't require formally broken casts, and a bonus is correctly documented, is to install your signal handler using sigaction and the SA_SIGINFO flag, and then in the si_code field of the second parameter (type siginfo_t) you can determine which floating-point error occurred:

The following values can be placed in si_code for a SIGFPE signal:

FPE_INTDIV Integer divide by zero.

FPE_INTOVF Integer overflow.

FPE_FLTDIV Floating-point divide by zero.

FPE_FLTOVF Floating-point overflow.

FPE_FLTUND Floating-point underflow.

FPE_FLTRES Floating-point inexact result.

FPE_FLTINV Floating-point invalid operation.

FPE_FLTSUB Subscript out of range.

Source: Linux sigaction(2) man page

The same list is also readily available on the FreeBSD siginfo man page.




回答2:


The information that glibc refers to is a historic mechanism, and is not portable. On FreeBSD, the sigvec(2) manual page includes a notation that it is only supported on the VAX-11 architecture:

On the VAX-11 The handler routine can be declared:

void  handler(sig, code, scp)
int sig, code;
struct sigcontext *scp;

Here sig is the signal number, into which the hardware faults and traps are mapped as defined below. The code argument is either a constant as given below or, for compatibility mode faults, the code provided by the hardware (Compatibility mode faults are distinguished from the other SIGILL traps by having PSL_CM set in the psl). The scp argument is a pointer to the sigcontext structure (defined in <signal.h>), used to restore the context from before the signal.

The mapping list is not actually provided in this version of the manpage. It can be found in the 4.3BSD-Reno signal(3) manpage. It's worth noting that this text is 25 years old.

On modern systems, you should use the sigaction mechanism, which is much more well-defined and portable.

BUGS - This manual page is still confusing.

Indeed it is.



来源:https://stackoverflow.com/questions/30628167/how-to-get-further-information-on-sigfpe-signal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!