我有一個程序,它使用fork創建另一個進程,並幾乎直接調用可執行文件的execv。我想檢查子進程上的內存泄漏。由於主進程啓動了許多其他可執行文件並運行了更多的腳本(這些腳本太難以跟蹤使用-trace-children選項),我想使用execv從主進程內部調用valgrind,並通過可執行作爲參數。運行valgrind時出錯
我的代碼是這樣的 -
#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.
我做了一些Google搜索,但找不到原因。請幫忙!
快來看看你如何設置'arguments'陣列一探究竟。然後你需要記住數組應該以空指針終止。 –
的可能的複製[理解爲execve的要求和設置環境瓦爾(http://stackoverflow.com/questions/7656549/understanding-requirements-for-execve-and-setting-environment-vars) – jww