运行 valgrind 时出错

Error while running valgrind

我有一个程序,它使用 fork 创建另一个进程,并几乎立即在可执行文件上调用 execv。我想检查子进程的内存泄漏。由于主进程启动了许多其他可执行文件和 运行s 更多脚本(使用 --trace-children 选项很难跟踪),我想从主进程内部调用 valgrind使用 execv 并将可执行文件作为参数传递。

我的代码是这样的 -

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

int main()
{
    char tool[30], logpath[30], executable[30], exepath[30];
    char *arguments[5];
    strcpy(tool, "--leak-check=full");
    strcpy(logpath, "--log-file=valrep.txt");
    strcpy(executable, "./memleak");
    strcpy(exepath, "/usr/bin/valgrind");

    arguments[0] = exepath;
    arguments[1] = tool;
    arguments[2] = logpath;
    arguments[3] = exepath;
    execv("/usr/bin/valgrind", arguments);
    return 0;
}

其中 memleak 是我要检查泄漏的程序。但是当我 运行 这个程序时,我得到这个错误。

Running valgrind using the tool: --leak-check=full.
The report is stored in: --log-file=valrep.txt.
valgrind: You cannot run '/usr/bin/valgrind' directly.
valgrind: You should use $prefix/bin/valgrind.

我进行了一些谷歌搜索,但找不到原因。请帮忙!

您没有传递可执行文件路径。

arguments[0] = exepath;
arguments[1] = tool;
arguments[2] = logpath;
arguments[3] = exepath;

替换为

arguments[0] = exepath;
arguments[1] = tool;
arguments[2] = logpath;
arguments[3] = executable;

如果您遇到任何问题,请告诉我..