目次: GCC
去年あたりにGCCのデバッグ環境について書きました(2019年5月17日の日記参照)。GDBでGCCの動作を調べる際に、最適化が効いていると色々デバッグ時に不都合が生じます。例えば、
通常のアプリケーションだと気になりませんが、相手は魔界GCCです。勝手にブレークポイントがずれてもらってはたまったものではないです。こういうときはGCCのビルドオプションを変えて、最適化を全てOFFにしてしまうと見やすいです。
環境はDebian Testingです。GCCは8.3を使っています。
$ mkdir build $ cd build $ ../configure \ --prefix=`pwd`/_install \ --enable-languages=c,c++ \ --disable-libsanitizer \ --disable-bootstrap \ CFLAGS="-O0 -g -fno-inline" \ CXXFLAGS="-O0 -g -fno-inline" $ make -j8 $ make install
私はCとC++ だけ使えれば良いので、--enable-languages=c,c++ を指定して、ついでにビルド時間短縮しています。C以外の言語(Fortranなど)に用事がある場合は、適宜足してください。Debian TestingでGCC 8.3をビルドするとなんでかlibsanitizerがビルドエラーになった(深追いしてません)ので、--disable-libsanitizerを付けて回避しました。環境によっては要らないかも?
また、ブートストラップモードだと、ビルドに時間がかかりすぎるので --disable-bootstrapで無効にしています。ビルドオプションは "-O0 -g -fno-inline" 最適化なし、デバッグ情報あり、インライン展開なし、です。
インストール先はどこでも良いですが、インストールするディレクトリは、build下の _installディレクトリにしています。buildディレクトリと一緒に消せて、間違って古いバイナリを使う心配がほぼないため、最近お気に入りのインストール先です。
オプション -O2 -gでビルドしたバイナリを使って、GDBでインライン関数にブレークポイントを設定すると、こんなふうになります。
/* Set the implicit flag for a builtin function. */
static inline void
set_builtin_decl_implicit_p (enum built_in_function fncode, bool implicit_p)
{
size_t uns_fncode = (size_t)fncode;
gcc_checking_assert (BUILTIN_VALID_P (fncode)
&& builtin_info[uns_fncode].decl != NULL_TREE);
builtin_info[uns_fncode].implicit_p = implicit_p; //★ここにブレークポイントを設定★
}
$ gdb /path/to/gcc/build/_install/libexec/gcc/x86_64-pc-linux-gnu/8.3.0/cc1 (gdb) b tree.h:5245 Breakpoint 1 at 0x7cc7c3: tree.h:5245. (2 locations) (gdb) r -quiet -imultiarch x86_64-linux-gnu a.c -dumpbase a.c -mtune=generic \ -march=x86-64 -auxbase a -g -O2 -Wall -std=c99 -o zzzzzzzz.s Breakpoint 1, set_builtin_decl_implicit_p (implicit_p=true, fncode=12304) at ../../gcc/tree.h:5245 5245 builtin_info[uns_fncode].implicit_p = implicit_p; (gdb) p uns_fncode $1 = <optimized out>
一応ブレークはしますが、表示がおかしいです。引数の順序が逆ですし、fncodeの値もおかしい(16のはず)です。ローカル変数は最適化によって消されてprint不可能です。
#0 set_builtin_decl_implicit_p (implicit_p=true, fncode=12304) at ../../gcc/tree.h:5245 #1 gimplify_addr_expr (expr_p=expr_p@entry=0x7ffff76682e0, pre_p=pre_p@entry=0x7fffffffd600, post_p=post_p@entry=0x7fffffffd190) at ../../gcc/gimplify.c:6051 #2 0x000000000084301d in gimplify_expr (expr_p=0x7ffff76682e0, pre_p=<optimized out>, post_p=<optimized out>, gimple_test_f=<optimized out>, fallback=<optimized out>) at ../../gcc/gimplify.c:11581 #3 0x0000000000846a55 in gimplify_call_expr (expr_p=0x7ffff77eaee0, pre_p=0x7fffffffd600, want_value=<optimized out>) at ../../gcc/gimplify.c:3308 #4 0x00000000008436d6 in gimplify_expr (expr_p=0x7ffff77eaee0, pre_p=<optimized out>, post_p=<optimized out>, gimple_test_f=<optimized out>, fallback=<optimized out>) at ../../gcc/gimplify.c:11506 #5 0x0000000000843c0e in gimplify_stmt (seq_p=<optimized out>, stmt_p=<optimized out>) at ../../gcc/gimplify.c:6690 #6 gimplify_statement_list (pre_p=<optimized out>, expr_p=0x7ffff743ddd0) at ../../gcc/gimplify.c:1764 #7 gimplify_expr (expr_p=0x7ffff743ddd0, pre_p=<optimized out>, post_p=<optimized out>, gimple_test_f=<optimized out>, fallback=<optimized out>) at ../../gcc/gimplify.c:11963 #8 0x0000000000848f42 in gimplify_stmt (seq_p=0x7fffffffd600, stmt_p=0x7ffff743ddd0) at ../../gcc/gimplify.c:6690 #9 gimplify_bind_expr (expr_p=expr_p@entry=0x7ffff744b1c0, pre_p=pre_p@entry=0x7fffffffd7e8) at ../../gcc/gimplify.c:1331 #10 0x000000000084344b in gimplify_expr (expr_p=0x7ffff744b1c0, pre_p=<optimized out>, post_p=<optimized out>, gimple_test_f=<optimized out>, fallback=<optimized out>) at ../../gcc/gimplify.c:11735 #11 0x0000000000847508 in gimplify_stmt (seq_p=0x7fffffffd7e8, stmt_p=0x7ffff744b1c0) at ../../gcc/gimplify.c:6690 #12 gimplify_body (fndecl=0x7ffff744b100, do_parms=<optimized out>) at ../../gcc/gimplify.c:12735 #13 0x00000000008478e6 in gimplify_function_tree (fndecl=fndecl@entry=0x7ffff744b100) at ../../gcc/gimplify.c:12900 #14 0x00000000006f6ab0 in cgraph_node::analyze (this=0x7ffff74472e0) at ../../gcc/cgraphunit.c:670 #15 0x00000000006f8e68 in analyze_functions (first_time=<optimized out>) at ../../gcc/cgraphunit.c:1131 #16 0x00000000006f99c3 in symbol_table::finalize_compilation_unit (this=0x7ffff7658100) at ../../gcc/cgraphunit.c:2691 #17 0x0000000000a689fb in compile_file () at ../../gcc/toplev.c:480 #18 0x00000000005bbe3d in do_compile () at ../../gcc/toplev.c:2132 #19 toplev::main (this=this@entry=0x7fffffffda9e, argc=<optimized out>, argc@entry=17, argv=<optimized out>, argv@entry=0x7fffffffdba8) at ../../gcc/toplev.c:2267 #20 0x00000000005be0cf in main (argc=17, argv=0x7fffffffdba8) at ../../gcc/main.c:39
このケースだとバックトレースに抜けはなさそうですが、表示される引数にoptimized outが多く、何が渡されたのかわかりません。
ビルドオプション -O0 -g -fno-inlineでビルドして、GDBでインライン関数にブレークを設定します。
$ gdb /path/to/gcc/build/_install/libexec/gcc/x86_64-pc-linux-gnu/8.3.0/cc1 (gdb) b tree.h:5245 Breakpoint 1 at 0x7cc7c3: tree.h:5245. (2 locations) (gdb) r -quiet -imultiarch x86_64-linux-gnu a.c -dumpbase a.c -mtune=generic \ -march=x86-64 -auxbase a -g -O2 -Wall -std=c99 -o zzzzzzzz.s Breakpoint 1, set_builtin_decl_implicit_p (fncode=BUILT_IN_ATAN2F, implicit_p=true) at ../../gcc/tree.h:5245 5245 builtin_info[uns_fncode].implicit_p = implicit_p; (gdb) p uns_fncode $1 = 16
引数のenumも名前で出ていますし、ローカル変数も表示できます。
#0 set_builtin_decl_implicit_p (fncode=BUILT_IN_ATAN2F, implicit_p=true) at ../../gcc/tree.h:5245 #1 0x0000000000b55843 in gimplify_addr_expr (expr_p=0x7ffff76682e0, pre_p=0x7fffffffd3f0, post_p=0x7fffffffcc08) at ../../gcc/gimplify.c:6051 #2 0x0000000000b636a5 in gimplify_expr (expr_p=0x7ffff76682e0, pre_p=0x7fffffffd3f0, post_p=0x7fffffffcc08, gimple_test_f=0xb16def <is_gimple_call_addr(tree_node*)>, fallback=1) at ../../gcc/gimplify.c:11581 #3 0x0000000000b4f52c in gimplify_call_expr (expr_p=0x7ffff77eaee0, pre_p=0x7fffffffd3f0, want_value=false) at ../../gcc/gimplify.c:3308 #4 0x0000000000b6336e in gimplify_expr (expr_p=0x7ffff77eaee0, pre_p=0x7fffffffd3f0, post_p=0x7fffffffcf58, gimple_test_f=0xb5411f <is_gimple_stmt(tree)>, fallback=0) at ../../gcc/gimplify.c:11506 #5 0x0000000000b571dd in gimplify_stmt (stmt_p=0x7ffff77eaee0, seq_p=0x7fffffffd3f0) at ../../gcc/gimplify.c:6690 #6 0x0000000000b4bcb9 in gimplify_statement_list (expr_p=0x7ffff743ddd0, pre_p=0x7fffffffd3f0) at ../../gcc/gimplify.c:1764 #7 0x0000000000b647a5 in gimplify_expr (expr_p=0x7ffff743ddd0, pre_p=0x7fffffffd3f0, post_p=0x7fffffffd208, gimple_test_f=0xb5411f <is_gimple_stmt(tree)>, fallback=0) at ../../gcc/gimplify.c:11963 #8 0x0000000000b571dd in gimplify_stmt (stmt_p=0x7ffff743ddd0, seq_p=0x7fffffffd3f0) at ../../gcc/gimplify.c:6690 #9 0x0000000000b4ad3a in gimplify_bind_expr (expr_p=0x7ffff744b1c0, pre_p=0x7fffffffd708) at ../../gcc/gimplify.c:1331 #10 0x0000000000b63d56 in gimplify_expr (expr_p=0x7ffff744b1c0, pre_p=0x7fffffffd708, post_p=0x7fffffffd558, gimple_test_f=0xb5411f <is_gimple_stmt(tree)>, fallback=0) at ../../gcc/gimplify.c:11735 #11 0x0000000000b571dd in gimplify_stmt (stmt_p=0x7ffff744b1c0, seq_p=0x7fffffffd708) at ../../gcc/gimplify.c:6690 #12 0x0000000000b6610a in gimplify_body (fndecl=0x7ffff744b100, do_parms=true) at ../../gcc/gimplify.c:12735 #13 0x0000000000b66740 in gimplify_function_tree (fndecl=0x7ffff744b100) at ../../gcc/gimplify.c:12900 #14 0x00000000009798ad in cgraph_node::analyze (this=0x7ffff74472e0) at ../../gcc/cgraphunit.c:670 #15 0x000000000097aa60 in analyze_functions (first_time=true) at ../../gcc/cgraphunit.c:1131 #16 0x000000000097e71a in symbol_table::finalize_compilation_unit (this=0x7ffff7658100) at ../../gcc/cgraphunit.c:2691 #17 0x0000000000e79db0 in compile_file () at ../../gcc/toplev.c:480 #18 0x0000000000e7c68a in do_compile () at ../../gcc/toplev.c:2132 #19 0x0000000000e7c966 in toplev::main (this=0x7fffffffda7e, argc=18, argv=0x7fffffffdb88) at ../../gcc/toplev.c:2267 #20 0x00000000019e07e6 in main (argc=18, argv=0x7fffffffdb88) at ../../gcc/main.c:39
バックトレースの引数表示もうまくいっているようです。
< | 2021 | > | ||||
<< | < | 03 | > | >> | ||
日 | 月 | 火 | 水 | 木 | 金 | 土 |
- | 1 | 2 | 3 | 4 | 5 | 6 |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 | - | - | - |
合計:
本日:
管理者: Katsuhiro Suzuki(katsuhiro( a t )katsuster.net)
This is Simple Diary 1.0
Copyright(C) Katsuhiro Suzuki 2006-2023.
Powered by PHP 8.2.15.
using GD bundled (2.1.0 compatible)(png support.)