问题
How can I run valgrind on an embedded Linux box to find memory leaks in my main software?
In the rcS
script, I am running like this:
./main_app
How can I associate the ./main_app
program with valgrind? The main_app process never terminates.
I want to constantly log the data to file. Also, I want to access the log file without terminating the main_app
process. I can do telnet and can access the log file. But the problem is until and unless the handler is closed, how can I open the file i.e. I don't quite understand which valgrind parameters control how memory leaks are logged to file. Please help!
回答1:
You can try to build it by your own for mips, here the steps: download valgrind from here http://valgrind.org/downloads/ - I used Valgrind 3.8.1 unpack archive with valgrind and move to the valgrinds folder execute:
./autogen.sh
./configure --host=mipsel-linux-gnu --prefix=/home/pub/valgrind CFLAGS="-mips32r2" CC=/opt/toolchains/mips-4.3/bin/mips-linux-gnu-gcc CXX=/opt/toolchains/mips-4.3/bin/mips-linux-gnu-c++
./make -j6
./make install
- prefix - folder to install compiled binaries of valgrind;
- CC and CXX - path to compilers;
- CFLAGS - "-mips32r2" and "-mplt" flags should be passed to compilers if it older then gcc (GCC) 4.5.1
on target mips box export path to valgrind lib folder:
export VALGRIND_LIB=/mnt/nfs/lib/valgrind
Now you can use it as usual, for memory checking features you can look here http://valgrind.org/docs/manual/mc-manual.html It works for me, good luck.
回答2:
Valgrind only works on x86. You can still track down your leak if you build your application for x86 and run it under valgrind there. Its unlikely the problem is specific to the target architecture.
回答3:
The answer above describes how to build valgrind but to actually get a full leak check as opposed to just a list of memory problems, your program does have to terminate, I am guessing you never terminate your program.
Assuming your process is some kind of daemon process your best bet is just run it in a loop, monitor memory usage using something like top
and then when you see signs of excessive memory usage send it a shutdown command somehow. If you then run valgrind with the following options, you will get a unique log for each process run, including a dump of leaks at exit:
while true ; do
valgrind --leak-check=yes --log-file=/tmp/log.%p.txt main_app
sleep 1
done
The %p
in the filename inserts the process id.
You can also specify --show-possibly-lost=no
which will reduce the volume of leaks reported to those that valgrind has a much higher degree of confidence are lost.
来源:https://stackoverflow.com/questions/18009140/how-to-run-valgrind-to-find-out-memory-leaks-on-my-embedded-mipsel-linux-box