aniztar
aniztar

Reputation: 45

Error while running valgrind

I have a program which creates another process using a fork and almost immedietely calls execv on a executable. I want to check for memory leaks on the child process. Since the main process starts a lot of other executables and runs many more scripts (which are too difficult to keep track of for using --trace-children option), I want to invoke valgrind from inside of the main process using execv and pass the executable as an argument.

My code goes something like this -

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

Where memleak is the program which I want to check for leaks. But when i run this program, I am getting this error.

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.

I did some googling but couldn't find out the reason. Please help!

Upvotes: 2

Views: 1474

Answers (1)

ntshetty
ntshetty

Reputation: 1305

You are not passing your executable path.

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

Replace as

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

Let me know, if you face any issue with this..

Upvotes: 1

Related Questions