Debug - arosh/arosh.github.com GitHub Wiki

競プロのデバッグに使えるツールを紹介する.

ICPCで使える

-fsanitize=address

GCC>=4.8でのみ使える.Ubuntuなら14.04以降.

GDB

ICPCで使えない

Valgrind

アクセス違反・メモリリークの検知,キャッシュプロファイラ,ヒーププロファイラ,コールグラフなどの機能が含まれたフレームワーク.

デフォルトで実行されるのは--tool=memcheckである.--leak-checkはメモリリークの情報の表示方法に関するオプションで,--leak-check=fullに設定しているサイトが多いが,競プロではメモリリークにはあまり関心が無いので設定しなくて良いと思う (デフォルトは--leak-check=summaryで,個別のメモリリークの詳細を表示しない)

競プロで一番便利なのはアクセス違反の検知だが,valgrindよりも-fsanitizer=addressのほうが厳密にチェックしてくれている気がする.

https://access.redhat.com/documentation/ja-JP/Red_Hat_Enterprise_Linux/6/html/Performance_Tuning_Guide/s-memory-valgrind.html

callgrind

標準ではcallgrind.out.$pidというファイルに出力されるが,最新の$pidを探すのが面倒なので--callgrind-out-fileで指定している。

$ g++ -O2 -g hello.cpp # -fno-omit-frame-pointerを付けたほうが良い場合もあるのかもしれない
$ valgrind --tool=callgrind --callgrind-out-file=callgrind.out ./a.out
$ kcachegrind callgrind.out # GUIを入れるのが環境によってはめんどい
$ callgrind_annotate --auto=yes callgrind.out # CUIでも見られるが,kcachegrindほどは見やすくない
massif
$ g++ -O2 -g hello.cpp # -fno-omit-frame-pointerを付けたほうが良い場合もあるのかもしれない
$ valgrind --tool=massif ./a.out
$ ms_print massif.out.$pid

https://access.redhat.com/documentation/ja-JP/Red_Hat_Enterprise_Linux/6/html/Performance_Tuning_Guide/ch05s03s03.html

Clang

便利なsanitizerがたくさんある.

http://clang.llvm.org/docs/UsersManual.html

google-perftools

ヒーププロファイルとCPUプロファイルが行える。

sudo apt-get install libgoogle-perftools-dev
ヒーププロファイル

競プロではValgrindのほうが適しているかもしれない。"No nodes to print"と言われることがある。

MacではHomebrewを使ってbrew install gperftoolsでインストール可能。LD_PRELOADの代わりにDYLD_INSERT_LIBRARIESでdylibを指定する。Macではアドレスから関数名が引けない。

libtcmalloc_minimalはヒーププロファイラの機能を取り除いたもの。libtcmalloc_and_profilertcmallocprofilerを両方使いたい時に使う

http://googlejapan.blogspot.jp/2009/05/google-4-performance-tools.html

http://stackoverflow.com/questions/8514783/what-is-the-exact-equivalent-to-ld-preload-on-osx

CPUプロファイル

http://cell.fixstars.com/ps3linux/index.php/Google-perftools%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6CPU%E3%83%97%E3%83%AD%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AA%E3%83%B3%E3%82%B0%E3%82%92%E3%81%A8%E3%82%8B

http://iwiwi.hatenadiary.jp/entry/2015/05/13/155600

ヒーププロファイルと同様に,OSXだとアドレスから関数名が引けない。