組み込み装置向けに Linux でプログラムを書いているのですが、メモリリークが気になってデバッグ方法を探していた所、 Valgrind というソフトを見つけたので試しました。これはなかなか良いです。
今回の実機は BeagleBoard-xM 、これへの OS は Ubuntu 10.04 を入れてます。因みに、ドキュメントを読む限りは Android 向けへもコンパイルできる様で期待は持てますが、試していませんのでこれ以上は触れない事にします。
ではまずダウンロードから。
$ cd ~/tmp $ wget http://valgrind.org/downloads/valgrind-3.8.1.tar.bz2 $ tar xvf valgrind-3.8.1.tar.bz2 $ cd valgrind-3.8.1
|
configure ファイルへパッチを当てます。これは Valgrind の configure スクリプトが、 ARM 用の host として “armv7-*” という文字列を期待しているから。 Ubuntu の apt-get でインストールできる ARM クロス開発環境は “arm-*” なので、このままだと configure の時に直接 –host へ指定できなくて困るのです。
*** configure.ORG 2012-09-26 14:39:51.941641211 +0800 --- configure 2012-09-26 14:40:03.981641211 +0800 *************** *** 5322,5328 **** ARCH_MAX="s390x" ;; ! armv7*) { $as_echo "$as_me:${as_lineno-$LINENO}: result: ok (${host_cpu})" >&5 $as_echo "ok (${host_cpu})" >&6; } ARCH_MAX="arm" --- 5322,5328 ---- ARCH_MAX="s390x" ;; ! arm*) { $as_echo "$as_me:${as_lineno-$LINENO}: result: ok (${host_cpu})" >&5 $as_echo "ok (${host_cpu})" >&6; } ARCH_MAX="arm"
|
make する。実機では /opt/valgrind へインストールする事を想定しています。
$ ./configure --prefix=/opt/valgrind --host=arm-linux-gnueabi $ make -j4
|
tarball を作る。
$ sudo DESTDIR=~/tmp/valgrind make install $ cd ~/tmp/valgrind $ tar cvfj valgrind.tar.bz2 ./opt
|
それを実機へ転送。
% ftp 192.168.15.80 Connected to 192.168.15.80. 220 beagleboard101 FTP server (Version 6.4/OpenBSD/Linux-ftpd-0.17) ready. Name (192.168.15.80:kokubu): ubuntu 331 Password required for ubuntu. Password: 230- Linux beagleboard101 2.6.32 #1 Sat May 19 00:00:49 CST 2012 armv7l GNU/Linux 230- Ubuntu 10.04 LTS 230- 230- Welcome to Ubuntu! 230- * Documentation: https://help.ubuntu.com/ 230 User ubuntu logged in. Remote system type is UNIX. Using binary mode to transfer files. ftp> put valgrind.tar.bz2 local: valgrind.tar.bz2 remote: valgrind.tar.bz2 200 PORT command successful. 150 Opening BINARY mode data connection for 'valgrind.tar.bz2'. 226 Transfer complete. 53900340 bytes sent in 12.80 secs (4112.7 kB/s) ftp> quit 221 Goodbye.
|
実機の環境をセットアップ。 /opt/valgrind へ展開して、 ldconfig でライブラリ検索パスを通します。
$ ssh ubuntu@192.168.15.80 ubuntu@beagleboard:~$ sudo -s ubuntu@beagleboard:~# tar xvf valgrind.tar.bz2 -C / ubuntu@beagleboard:~# touch /etc/ld.so.conf.d/valgrind.conf ubuntu@beagleboard:~# echo /opt/valgrind/lib > /etc/ld.so.conf.d/valgrind.conf ubuntu@beagleboard:~# ldconfig ubuntu@beagleboard:~# ^D ubuntu@beagleboard:~$ PATH=$PATH:/opt/valgrind/bin ubuntu@beagleboard:~$ valgrind --version valgrind-3.8.1
|
ここで一度実行してみて、もしエラーが出る様であれば、実機で libc6-dbg をインストール。
ubuntu@beagleboard:~$ cd test_dir ubuntu@beagleboard:~/tfj$ valgrind --leak-check=yes ./test b ==1348== Memcheck, a memory error detector ==1348== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. ==1348== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info ==1348== Command: ./test b ==1348==
valgrind: Fatal error at startup: a function redirection valgrind: which is mandatory for this platform-tool combination valgrind: cannot be set up. Details of the redirection are: valgrind: valgrind: A must-be-redirected function valgrind: whose name matches the pattern: memcpy valgrind: in an object with soname matching: ld-linux.so.3 valgrind: was not found whilst processing valgrind: symbols from the object with soname: ld-linux.so.3 valgrind: valgrind: Possible fixes: (1, short term): install glibc's debuginfo valgrind: package on this machine. (2, longer term): ask the packagers valgrind: for your Linux distribution to please in future ship a non- valgrind: stripped ld.so (or whatever the dynamic linker .so is called) valgrind: that exports the above-named function using the standard valgrind: calling conventions for this platform. The package you need valgrind: to install for fix (1) is called valgrind: valgrind: On Debian, Ubuntu: libc6-dbg valgrind: On SuSE, openSuSE, Fedora, RHEL: glibc-debuginfo valgrind: valgrind: Cannot continue -- exiting now. Sorry.
ubuntu@beagleboard101:~$ sudo apt-get install libc6-dbg
|
これは、 Valgrind の実行が成功した時のサンプル。
ubuntu@beagleboard:~/tfj$ valgrind --leak-check=yes ./test b ==1373== Memcheck, a memory error detector ==1373== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. ==1373== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info ==1373== Command: ./test b ==1373== cmd> q PCL terminate ==1373== ==1373== HEAP SUMMARY: ==1373== in use at exit: 0 bytes in 0 blocks ==1373== total heap usage: 30 allocs, 30 frees, 84,288 bytes allocated ==1373== ==1373== All heap blocks were freed -- no leaks are possible ==1373== ==1373== For counts of detected and suppressed errors, rerun with: -v ==1373== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 21 from 6)
|
よく使うオプションはこんなもの :
- –tool=memcheck
- –leak-check=full
- –show-reachable=yes