0

我有一个用户空间代码如下,

try
{
some code
...
code that tries accessing forbidden address
...
some code
}
catch (all exceptions)
{
some logs
}

内核是否会向SIGSEGV用户进程发送此无效访问的信号以及行为将是什么default(没有安装任何信号处理程序)。请问系统crash

4

2 回答 2

1

An exception is not generated in this case. You need to set the signal handler. Take a look into man signal how to do it.

For example :

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>

static void hdl (int sig, siginfo_t *siginfo, void *context)
{
    printf ("Sending PID: %ld, UID: %ld\n",
            (long)siginfo->si_pid, (long)siginfo->si_uid);
}

int main (int argc, char *argv[])
{
    struct sigaction act;

    memset (&act, '\0', sizeof(act));

    /* Use the sa_sigaction field because the handles has two additional parameters */
    act.sa_sigaction = &hdl;

    /* The SA_SIGINFO flag tells sigaction() to use the sa_sigaction field, not sa_handler. */
    act.sa_flags = SA_SIGINFO;

    if (sigaction(SIGTERM, &act, NULL) < 0) {
        perror ("sigaction");
        return 1;
    }

    while (1)
        sleep (10);

    return 0;
}
于 2012-09-12T08:58:16.173 回答
1

尝试访问禁止地址的代码

你不能用C++ exceptions. 只有platform-dependent解决方案。

于 2012-09-12T07:55:13.813 回答