valgrind improper output location

321 views Asked by At

I have a program that is supposed to run valgind on another program and direct the valgrind output to another file. The code I'm using is:

char* args[] = {"sudo",  //(This is inside a fork)
        "valgrind",
        "--leak-check=full",
        "--show-leak-kinds=definite",
        "--errors-for-leak-kinds=definite",
        "<path to executable>", //placeholders
        "&><path to output file>", //placeholders
        "\0"};
execvp("sudo",args);

The problem I'm getting is that no matter what I do, it doesn't actually direct the output anywhere but the terminal.

I've tried "&>" and ">" with no change. I've tried running it in terminal with redirected output to file, I've also tried running it in eclipse with terminal arguments, etc. I'm not sure what else I can try.

Does anyone have any idea why "&>" isn't redirecting? I have confirmed that typing the args array in the terminal (not as part of the program) does work. It seems to just be something with the c++ program (possibly fork?).

1

There are 1 answers

1
hlscalon On BEST ANSWER

I recommend you to use --log-file=<filename> option.

From manual:

--log-file=<filename>

Specifies that Valgrind should send all of its messages to the specified file. If the file name is empty, it causes an abort. There are three special format specifiers that can be used in the file name.

%p is replaced with the current process ID. This is very useful for program that invoke multiple processes. WARNING: If you use --trace-children=yes and your program invokes multiple processes OR your program forks without calling exec afterwards, and you don't use this specifier (or the %q specifier below), the Valgrind output from all those processes will go into one file, possibly jumbled up, and possibly incomplete.

%q{FOO} is replaced with the contents of the environment variable FOO. If the {FOO} part is malformed, it causes an abort. This specifier is rarely needed, but very useful in certain circumstances (eg. when running MPI programs). The idea is that you specify a variable which will be set differently for each process in the job, for example BPROC_RANK or whatever is applicable in your MPI setup. If the named environment variable is not set, it causes an abort. Note that in some shells, the { and } characters may need to be escaped with a backslash.

%% is replaced with %.

If an % is followed by any other character, it causes an abort.

If the file name specifies a relative file name, it is put in the program's initial working directory : this is the current directory when the program started its execution after the fork or after the exec. If it specifies an absolute file name (ie. starts with '/') then it is put there.