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
のほうが厳密にチェックしてくれている気がする.
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
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_profiler
はtcmalloc
とprofiler
を両方使いたい時に使う
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://iwiwi.hatenadiary.jp/entry/2015/05/13/155600
ヒーププロファイルと同様に,OSXだとアドレスから関数名が引けない。