2

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!

aniztar
  • 45
  • 4
  • 2
    Take a closer look at how you set up the `arguments` array. Then you need to remember that the array should be terminated with a null pointer. – Some programmer dude Jan 26 '17 at 05:22
  • Possible duplicate of [understanding requirements for execve and setting environment vars](http://stackoverflow.com/questions/7656549/understanding-requirements-for-execve-and-setting-environment-vars) – jww Jan 26 '17 at 08:09

1 Answers1

1

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..

ntshetty
  • 1,293
  • 9
  • 20