Work
Travel
Various
C++ のデバッグ
ライブラリの依存関係
- macOS:
otool -L <executable>で、実行ファイルにリンクされている共有ライブラリを一覧表示します。 -
Linux:
ldd <executable>で、実行ファイルにリンクされている共有ライブラリを一覧表示します。 -
nm <options> <executable/library> | grep LZ4_compress_fastで、特定のシンボルがバイナリ/ライブラリに 存在するかどうかを確認します。macOS のオプション:
-g: 外部(グローバル)シンボルのみ-U: 未定義シンボルを非表示にする-m: 詳細な Mach-O 形式の情報
Linux のオプション:
-D: 動的シンボル(エクスポートされたインターフェース)--defined-only: ライブラリが定義するシンボルのみ(未定義のインポートは除く)
シンボルの種類:
T/t: テキスト(関数)が定義されているD/d: データが定義されているU: 未定義
lldb の使用(macOS)
- 実行ファイルを指定して lldb を起動します:
lldb my_executable - ブレークポイントを設定します。引用符やパラメータ、行番号、条件(-c)を使用できます。
関数が共有オブジェクト内にある場合は、先に共有ライブラリを読み込む必要があることがあります(例:
process launchを使用)。break set -n my_function_name break set -n 'hipo_output::writeHeader(outputContainer*, std::map<std::string, double>, gBank)' break set --file hipo_output.cc --line <line_number> break set -n "GOptions::getVerbosityFor" -c "tag.size() == 0" - 引数なしでプログラムを実行します:
run arg1 arg2 - 引数を指定してプログラムを実行します(より柔軟)
process launch -- arg1 arg2 ... - スタックフレーム(特定の関数呼び出し)にフォーカスを移します
frame select 10は、停止時点で選択されているスレッドの、現在実行中の命令ポインタです。 通常、ここが障害の発生箇所(または例外/ブレークポイントによる停止箇所)です。1はフレーム #0 の直接の呼び出し元です。2はフレーム #1 の呼び出し元で、以降スタックを上にたどります。
- プログラムがクラッシュする直前に何をしているかを表示します。
disassemble -f - 変数の値を確認/表示します:
frame variable # show all variables in the current frame p variable_name # print a specific variable value p *variable_name # if they're pointers - コードをステップ実行します:
step # or just 's'. Step into the next line of code next # or just 'n'. Step over the next line of code frame info # Show current frame information list # Show the current source code around the current line bt # Backtrace to see the call stack bt all # Full backtrace with arguments finish # Step out of the current function
gdb の使用(Linux)
- 実行ファイルを指定して gdb を起動します:
gdb my_executable
- ブレークポイントを設定します(関数名、file:line、または条件を使用可能):
break my_function_name
break 'hipo_output::writeHeader(outputContainer*, std::map<std::string, double>, gBank)'
break hipo_output.cc:<line_number>
break GOptions::getVerbosityFor if tag.size() == 0
- ユーティリティを設定します(1つずつ入力する必要がある場合があります)
set pagination off
set print thread-events off
catch throw
catch catch
- 引数を指定してプログラムを実行します:
run arg1 arg2 ...
- バックトレースとプログラムの状態を確認します
backtrace # Show call stack
bt full
thread apply all bt full
info threads
frame N # Change focus to a stack frame
info locals # Show local variables
info args # Show function arguments
print var # Print value of variable
print *ptr # Dereference a pointer
list # Show source around current line
- コードをステップ実行します:
step # Step into the next line (enter function)
next # Step over the next line (skip into function)
finish # Step out of the current function