2013年1月14日 星期一

valgrind 快速上手

valgrind 是檢查記憶體用的小程式,因為是快速上手就不介紹了,可以自己 google 一下介紹很多 XD




[Install]
FreeBSD ports: /usr/ports/devel/valgrind

[Prerequisite]

gcc/g++ compile 時加上 -g,會輸出行號,人生會過得比較快樂
g++ -g  test.cpp

[Usage]
1. 預設 tool 為 memcheck,不用指定 tool;指定一下 log file 不然噴太多看不完...
valgrind --leak-check=full --log-file=memlog ./a.out
2. 寫 multi-thread/pthread 的時候,用 helgrind 或(和) drd 找 race condition 跟 API misuse
valgrind --tool=helgrind --log-file=hellog ./a.out
valgrind --tool=drd --log-file=drdlog ./a.out

3. 用 callgrind 做 function call 的 profiling 和 branch prediction(用 cachegrind 也可以)
有圖形化的話用 KCachegrind ,人生會比較愉悅一點。
(不過 call trace 本身就不是什麼愉悅的事情就是了...XD)
valgrind --tool=callgrind --log-file=calllog ./a.out
4. 用 cachegrind 模擬 machine cache 跟 branch prediction
valgrind --tool=cachegrind --log-file=cachellog ./a.out
5. 用 massif 做 heap profiling,這個沒有 output 不用指定 log file 了
valgrind --tool=massif ./a.out
好了以後 output 會在 massif.out.<pid>,用 ms_print 看(我傻逼常常忘記阿阿阿)
ms_print massif.out.12345
這邊的參數太重要了,不然出來的東西看不懂 ~_~
參考: http://valgrind.org/docs/manual/ms-manual.html

以前都只有用 1 ,最近寫 multi-thread 把 2-5 都給用上了,找 tutorial 很雷人的阿...

[Reference]
http://valgrind.org/
http://endlesschildhood.blogspot.tw/2010/01/valgrind-tutorial.html

沒有留言:

張貼留言