R S*_*hko 7 c++ linux crash exec
我试图追踪一个非常奇怪的崩溃.奇怪的是,有人发现了一种我无法解释的解决方法.
解决方法是这个小程序,我将其称为"跑步者":
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main(int argc, char *argv[])
{
if (argc == 1)
{
fprintf(stderr, "Usage: %s prog [args ...]\n", argv[0]);
return 1;
}
execvp(argv[1], argv + 1);
fprintf(stderr, "execv failed: %s\n", strerror(errno));
// If exec returns because the program is not found or we
// don't have the appropriate permission
return 255;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,所有这些程序都execvp用于替换自己的不同程序.
从命令行直接调用程序时程序崩溃:
/path/to/prog args # this crashes
Run Code Online (Sandbox Code Playgroud)
但是当它通过我的跑步垫片间接调用时工作正常:
/path/to/runner /path/to/prog args # works successfully
Run Code Online (Sandbox Code Playgroud)
对于我的生活,我可以弄清楚如何让额外的exec改变正在运行的程序的行为(因为你可以看到该程序不会改变环境).
关于崩溃的一些背景.崩溃本身发生在C++运行时.具体来说,当程序执行a时throw,崩溃版本错误地认为没有匹配的catch(尽管有)和调用terminate.当我通过跑步者调用程序时,异常被正确捕获.
我的问题是任何想法为什么额外的exec改变了exec'ed程序的行为?