如何从 C++ 上的分段错误中恢复?

Gul*_*zar 3 c++ error-handling signals exception segmentation-fault

我有一些生产关键代码必须继续运行。

将代码视为

while (true){
   init();
   do_important_things();  //segfault here
   clean();
}
Run Code Online (Sandbox Code Playgroud)

我不能相信代码没有错误,并且我需要能够记录问题以便稍后进行调查。

这次,我知道代码中的某个地方抛出了分段错误,我需要至少能够记录该错误,然后重新开始一切。

阅读这里有一些解决方案,但每个解决方案后面都是一场激烈的争论,声称该解决方案实际上弊大于利,没有真正的解释。我还找到了我考虑使用的这个答案,但我不确定它是否适合我的用例。

那么,从 C++ 分段错误中恢复的最佳方法是什么?

Ted*_*gmo 6

我建议您创建一个非常小的程序,使其非常安全,以监视有缺陷的程序。如果有错误的程序以您不喜欢的方式退出,请重新启动该程序。

POSIX 示例:

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#include <cstdio>
#include <iostream>

int main(int argc, char* argv[]) {
    if(argc < 2) {
        std::cerr << "USAGE: " << argv[0] << " program_to_monitor <arguments...>\n";
        return 1;
    }

    while(true) {
        pid_t child = fork();          // create a child process

        if(child == -1) {
            std::perror("fork");
            return 1;
        }

        if(child == 0) {
            execvp(argv[1], argv + 1); // start the buggy program
            perror(argv[1]);           // starting failed
            std::exit(0);              // exit with 0 to not trigger a retry
        }

        // Wait for the buggy program to terminate and check the status
        // to see if it should be restarted.

        if(int wstatus; waitpid(child, &wstatus, 0) != -1) {
            if(WIFEXITED(wstatus)) {
                if(WEXITSTATUS(wstatus) == 0) return 0; // normal exit, terminate

                std::cerr << argv[0] << ": " << argv[1] << " exited with "
                          << WEXITSTATUS(wstatus) << '\n';
            }
            if(WIFSIGNALED(wstatus)) {
                std::cerr << argv[0] << ": " << argv[1]
                          << " terminated by signal " << WTERMSIG(wstatus);
                if(WCOREDUMP(wstatus)) std::cout << " (core dumped)";
                std::cout << '\n';
            }
            std::cout << argv[0] << ": Restarting " << argv[1] << '\n';
        } else {
            std::perror("wait");
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)