如何获得有关 SIGFPE 信号的更多信息?

How to get further information on SIGFPE signal?

这来自GNU C 库参考手册

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.

但是没有关于如何访问额外参数的示例。

我做了我的 google 工作,但找不到任何东西。

我怎样才能得到这些额外的信息?

正如 EOF 在评论中提到的那样,更好的方法是使用 sigactionSA_SIGINFO标志,然后在第二个参数(类型siginfo_t)的si_code字段中可以判断发生了哪个浮点错误:

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.

来源:Linux sigaction(2) man page

同样的列表也很容易在 FreeBSD siginfo man page.

上找到

glibc引用的信息是历史机制,不可移植。在 FreeBSD 上,sigvec(2) 手册页包含一个表示它仅在 VAX-11 架构上受支持的符号:

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.

此版本的联机帮助页中实际上并未提供映射列表。它可以在 4.3BSD-Reno signal(3) manpage 中找到。值得注意的是,这篇文章已有25年历史了。

在现代系统上,您应该使用 sigaction 机制,它定义更明确且可移植。

BUGS - This manual page is still confusing.

确实如此。