繁体   English   中英

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

[英]How to recover from segmentation fault on C++?

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

将代码视为

while (true){
   init();
   do_important_things();  //segfault here
   clean();
}

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

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

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

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

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

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;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM