2017-01-26 102 views
1

我有一個程序,它使用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搜索,但找不到原因。請幫忙!

+2

快來看看你如何設置'arguments'陣列一探究竟。然後你需要記住數組應該以空指針終止。 –

+0

的可能的複製[理解爲execve的要求和設置環境瓦爾(http://stackoverflow.com/questions/7656549/understanding-requirements-for-execve-and-setting-environment-vars) – jww

回答

1

您沒有傳遞可執行文件路徑。

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

更換爲

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

讓我知道,如果你面對任何問題與此..

+1

'參數[4] = NULL「。 – jww

+0

@Thiru謝蒂我忽視了!謝謝! – aniztar

+0

@jww謝謝你也需要!我寫這段代碼時一定是盲目的:) – aniztar