how to set dynamic link library path and environment variable for a process in valgrind

c, c++, memory-leaks, valgrind

Solution

I've run into a similar issue, trying to run `valgrind` on programs that need libraries incompatible with the ones `valgrind` uses, and have been using:

`valgrind --trace-children=yes env LD_LIBRARY_PATH=your_library_path OTHER_VAR=foo your_program arg1 arg2...`

env sets up the environment and then execs `your_program`. We need to pass the --trace-children=yes argument to `valgrind` in order for it to continue to trace through the `exec` syscall. Without `--trace-children=yes` set, valgrind will stop tracing at the `exec` and you won't get any useful output from `valgrind` on `your_program`.

One potential downside to this approach is that valgrind might report any memory issues in `env`. I haven't seen any false positives from this source (`env` is not a very complicated program), but it could happen.

I haven't tried this with `LD_PRELOAD` though (it hasn't come up for my use-case yet). `Valgrind` does set `LD_PRELOAD`, so you might have to do something like:

`valgrind --trace-children=yes env LD_PRELOAD=$LD_PRELOAD:your_preload your_program`

Problem

I need to set LD_LIBRARY_PATH, LD_PRELOAD and some environment variables for a process while running and detect memory leaks with Valgrind. Can anyone suggest a way to set or pass these variable for a process in valgrind?.

Original source