gdbでマクロ展開情報を確認する方法

Ubuntu 8.04 LTS を入れたついでに、久しぶりに技術っぽいことでも書いておきます。

仕事でCのプログラムを見る時があるんですが、Cのマクロは便利ですけど、
ソースコードを読む時には、結構やっかいなことも多いですよね。
emacsで展開してもいいんですが、GDBで処理を追っているときに確認したい
ということで、下にあるようなことをして私は確認してます。

GCCコンパイルする段階で情報を付加するので、GCCコンパイルオプションを
変更するだけです。

1. -g3 -gdwarf-2 オプション付きでコンパイル
tagui@desktop:~/test$ gcc -g3 -gdwarf-2 test.c 

2. gdb起動
tagui@desktop:~/test$ gdb a.out

GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
(gdb) list
1	#include 
2	#define T
3	
4	#ifdef T
5	#define MINUS(x,y) (x-y)
6	#endif
7	
8	int main() {
9	  printf("%d\n",MINUS(10,1));
10	  return 0;

(gdb) macro expand MINUS(10,1)
expands to: (10-1)

(gdb) info macro MINUS
Defined at /home/tagui/test/test.c:5
#define MINUS(x, y) (x-y)

"macro expand" とか "info macro" とかでマクロ展開の結果がGDBで確認できます。

MINUSっていうマクロが展開される様子が確認できましたね。

ということで自分でもたまに忘れるので、メモしておきます。